2 * Copyright (c) 2012 Damjan Marion <dmarion@FreeBSD.org>
3 * Copyright (c) 2014 Andrew Turner <andrew@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
36 #include <sys/resource.h>
39 #include <sys/malloc.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <machine/intr.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include <arm/ti/ti_cpuid.h>
50 #include <arm/ti/ti_gpio.h>
51 #include <arm/ti/ti_pinmux.h>
53 #include <arm/ti/am335x/am335x_scm_padconf.h>
55 #include "ti_gpio_if.h"
57 static struct ofw_compat_data compat_data[] = {
58 {"ti,am335x-gpio", 1},
59 /* Linux uses ti,omap4-gpio on am335x so we need to support it */
66 am335x_gpio_probe(device_t dev)
69 if (!ofw_bus_status_okay(dev))
72 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
75 if (ti_chip() != CHIP_AM335X)
78 device_set_desc(dev, "TI AM335x General Purpose I/O (GPIO)");
84 am335x_gpio_set_flags(device_t dev, uint32_t gpio, uint32_t flags)
86 unsigned int state = 0;
87 struct ti_gpio_softc *sc = device_get_softc(dev);
89 if (flags & GPIO_PIN_OUTPUT) {
90 if (flags & GPIO_PIN_PULLUP)
91 state = PADCONF_OUTPUT_PULLUP;
93 state = PADCONF_OUTPUT;
94 } else if (flags & GPIO_PIN_INPUT) {
95 if (flags & GPIO_PIN_PULLUP)
96 state = PADCONF_INPUT_PULLUP;
97 else if (flags & GPIO_PIN_PULLDOWN)
98 state = PADCONF_INPUT_PULLDOWN;
100 state = PADCONF_INPUT;
102 return ti_pinmux_padconf_set_gpiomode(sc->sc_bank*32 + gpio, state);
106 am335x_gpio_get_flags(device_t dev, uint32_t gpio, uint32_t *flags)
109 struct ti_gpio_softc *sc = device_get_softc(dev);
111 if (ti_pinmux_padconf_get_gpiomode(sc->sc_bank*32 + gpio, &state) != 0) {
117 *flags = GPIO_PIN_OUTPUT;
119 case PADCONF_OUTPUT_PULLUP:
120 *flags = GPIO_PIN_OUTPUT | GPIO_PIN_PULLUP;
123 *flags = GPIO_PIN_INPUT;
125 case PADCONF_INPUT_PULLUP:
126 *flags = GPIO_PIN_INPUT | GPIO_PIN_PULLUP;
128 case PADCONF_INPUT_PULLDOWN:
129 *flags = GPIO_PIN_INPUT | GPIO_PIN_PULLDOWN;
140 static device_method_t am335x_gpio_methods[] = {
142 DEVMETHOD(device_probe, am335x_gpio_probe),
144 /* ti_gpio interface */
145 DEVMETHOD(ti_gpio_set_flags, am335x_gpio_set_flags),
146 DEVMETHOD(ti_gpio_get_flags, am335x_gpio_get_flags),
151 extern driver_t ti_gpio_driver;
152 static devclass_t am335x_gpio_devclass;
154 DEFINE_CLASS_1(gpio, am335x_gpio_driver, am335x_gpio_methods,
155 sizeof(struct ti_gpio_softc), ti_gpio_driver);
156 DRIVER_MODULE(am335x_gpio, simplebus, am335x_gpio_driver, am335x_gpio_devclass,