2 * Copyright 2013-2015 John Wehle <john@feith.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Amlogic aml8726 GPIO driver.
30 * Note: The OEN register is active * low *. Setting a bit to zero
31 * enables the output driver, setting a bit to one disables the driver.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/resource.h>
50 #include <machine/bus.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/gpio/gpiobusvar.h>
60 struct aml8726_gpio_softc {
62 struct resource *res[3];
68 static struct resource_spec aml8726_gpio_spec[] = {
69 { SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE }, /* oen */
70 { SYS_RES_MEMORY, 1, RF_ACTIVE | RF_SHAREABLE }, /* output */
71 { SYS_RES_MEMORY, 2, RF_ACTIVE }, /* input */
75 #define AML_GPIO_LOCK(sc) mtx_lock(&(sc)->mtx)
76 #define AML_GPIO_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
77 #define AML_GPIO_LOCK_INIT(sc) \
78 mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), \
80 #define AML_GPIO_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx);
82 #define AML_GPIO_OE_N_REG 0
83 #define AML_GPIO_OUT_REG 1
84 #define AML_GPIO_IN_REG 2
86 #define CSR_WRITE_4(sc, reg, val) bus_write_4((sc)->res[reg], 0, (val))
87 #define CSR_READ_4(sc, reg) bus_read_4((sc)->res[reg], 0)
90 aml8726_gpio_probe(device_t dev)
93 if (!ofw_bus_status_okay(dev))
96 if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-gpio"))
99 device_set_desc(dev, "Amlogic aml8726 GPIO");
101 return (BUS_PROBE_DEFAULT);
105 aml8726_gpio_attach(device_t dev)
107 struct aml8726_gpio_softc *sc = device_get_softc(dev);
113 node = ofw_bus_get_node(dev);
115 if (OF_getencprop(node, "pin-count",
116 &prop, sizeof(prop)) <= 0) {
117 device_printf(dev, "missing pin-count attribute in FDT\n");
125 if (bus_alloc_resources(dev, aml8726_gpio_spec, sc->res)) {
126 device_printf(dev, "can not allocate resources for device\n");
131 * The GPIOAO OUT bits occupy the upper word of the OEN register.
133 if (rman_get_start(sc->res[1]) == rman_get_start(sc->res[0]))
134 if (sc->npins > 16) {
136 "too many pins for overlapping OEN and OUT\n");
137 bus_release_resources(dev, aml8726_gpio_spec, sc->res);
141 AML_GPIO_LOCK_INIT(sc);
143 sc->busdev = gpiobus_attach_bus(dev);
144 if (sc->busdev == NULL) {
145 AML_GPIO_LOCK_DESTROY(sc);
146 bus_release_resources(dev, aml8726_gpio_spec, sc->res);
154 aml8726_gpio_detach(device_t dev)
156 struct aml8726_gpio_softc *sc = device_get_softc(dev);
158 gpiobus_detach_bus(dev);
160 AML_GPIO_LOCK_DESTROY(sc);
162 bus_release_resources(dev, aml8726_gpio_spec, sc->res);
168 aml8726_gpio_get_bus(device_t dev)
170 struct aml8726_gpio_softc *sc = device_get_softc(dev);
176 aml8726_gpio_pin_max(device_t dev, int *maxpin)
178 struct aml8726_gpio_softc *sc = device_get_softc(dev);
180 *maxpin = (int)sc->npins;
185 /* Get a specific pin's capabilities. */
187 aml8726_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
189 struct aml8726_gpio_softc *sc = device_get_softc(dev);
191 if (pin >= sc->npins)
194 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
199 /* Get a specific pin's name. */
201 aml8726_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
203 struct aml8726_gpio_softc *sc = device_get_softc(dev);
205 if (pin >= sc->npins)
208 snprintf(name, GPIOMAXNAME, "%s.%u", ofw_bus_get_name(dev), pin);
213 /* Get a specific pin's current in/out state. */
215 aml8726_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
217 struct aml8726_gpio_softc *sc = device_get_softc(dev);
218 uint32_t mask = 1U << pin;
220 if (pin >= sc->npins)
223 if ((CSR_READ_4(sc, AML_GPIO_OE_N_REG) & mask) == 0) {
225 *flags = GPIO_PIN_OUTPUT;
228 *flags = GPIO_PIN_INPUT;
233 /* Set a specific pin's in/out state. */
235 aml8726_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
237 struct aml8726_gpio_softc *sc = device_get_softc(dev);
238 uint32_t mask = 1U << pin;
240 if (pin >= sc->npins)
245 if ((flags & GPIO_PIN_OUTPUT) != 0) {
246 /* Output. Turn on driver. */
247 CSR_WRITE_4(sc, AML_GPIO_OE_N_REG,
248 (CSR_READ_4(sc, AML_GPIO_OE_N_REG) & ~mask));
250 /* Input. Turn off driver. */
251 CSR_WRITE_4(sc, AML_GPIO_OE_N_REG,
252 (CSR_READ_4(sc, AML_GPIO_OE_N_REG) | mask));
260 /* Set a specific output pin's value. */
262 aml8726_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
264 struct aml8726_gpio_softc *sc = device_get_softc(dev);
267 if (pin >= sc->npins || value > 1)
271 * The GPIOAO OUT bits occupy the upper word of the OEN register.
273 if (rman_get_start(sc->res[1]) == rman_get_start(sc->res[0]))
280 CSR_WRITE_4(sc, AML_GPIO_OUT_REG,
281 ((CSR_READ_4(sc, AML_GPIO_OUT_REG) & ~mask) | (value << pin)));
288 /* Get a specific pin's input value. */
290 aml8726_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
292 struct aml8726_gpio_softc *sc = device_get_softc(dev);
293 uint32_t mask = 1U << pin;
295 if (pin >= sc->npins)
298 *value = (CSR_READ_4(sc, AML_GPIO_IN_REG) & mask) ? 1 : 0;
303 /* Toggle a pin's output value. */
305 aml8726_gpio_pin_toggle(device_t dev, uint32_t pin)
307 struct aml8726_gpio_softc *sc = device_get_softc(dev);
310 if (pin >= sc->npins)
314 * The GPIOAO OUT bits occupy the upper word of the OEN register.
316 if (rman_get_start(sc->res[1]) == rman_get_start(sc->res[0]))
323 CSR_WRITE_4(sc, AML_GPIO_OUT_REG,
324 CSR_READ_4(sc, AML_GPIO_OUT_REG) ^ mask);
332 aml8726_gpio_get_node(device_t bus, device_t dev)
335 /* We only have one child, the GPIO bus, which needs our own node. */
336 return (ofw_bus_get_node(bus));
339 static device_method_t aml8726_gpio_methods[] = {
340 /* Device interface */
341 DEVMETHOD(device_probe, aml8726_gpio_probe),
342 DEVMETHOD(device_attach, aml8726_gpio_attach),
343 DEVMETHOD(device_detach, aml8726_gpio_detach),
346 DEVMETHOD(gpio_get_bus, aml8726_gpio_get_bus),
347 DEVMETHOD(gpio_pin_max, aml8726_gpio_pin_max),
348 DEVMETHOD(gpio_pin_getname, aml8726_gpio_pin_getname),
349 DEVMETHOD(gpio_pin_getflags, aml8726_gpio_pin_getflags),
350 DEVMETHOD(gpio_pin_getcaps, aml8726_gpio_pin_getcaps),
351 DEVMETHOD(gpio_pin_setflags, aml8726_gpio_pin_setflags),
352 DEVMETHOD(gpio_pin_get, aml8726_gpio_pin_get),
353 DEVMETHOD(gpio_pin_set, aml8726_gpio_pin_set),
354 DEVMETHOD(gpio_pin_toggle, aml8726_gpio_pin_toggle),
356 /* ofw_bus interface */
357 DEVMETHOD(ofw_bus_get_node, aml8726_gpio_get_node),
362 static driver_t aml8726_gpio_driver = {
364 aml8726_gpio_methods,
365 sizeof(struct aml8726_gpio_softc),
368 static devclass_t aml8726_gpio_devclass;
370 DRIVER_MODULE(aml8726_gpio, simplebus, aml8726_gpio_driver,
371 aml8726_gpio_devclass, 0, 0);