2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * Copyright (c) 2009, Luiz Otavio O Souza.
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 unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * GPIO driver for AR71xx
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
44 #include <sys/mutex.h>
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <mips/atheros/ar71xxreg.h>
50 #include <mips/atheros/ar71xx_gpiovar.h>
54 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)
56 struct ar71xx_gpio_pin {
62 static struct ar71xx_gpio_pin ar71xx_gpio_pins[] = {
63 { "RFled", 2, GPIO_PIN_OUTPUT},
64 { "SW4", 8, GPIO_PIN_INPUT},
71 static void ar71xx_gpio_function_enable(struct ar71xx_gpio_softc *sc,
73 static void ar71xx_gpio_function_disable(struct ar71xx_gpio_softc *sc,
75 static void ar71xx_gpio_pin_configure(struct ar71xx_gpio_softc *sc,
76 struct gpio_pin *pin, uint32_t flags);
81 static int ar71xx_gpio_probe(device_t dev);
82 static int ar71xx_gpio_attach(device_t dev);
83 static int ar71xx_gpio_detach(device_t dev);
84 static int ar71xx_gpio_filter(void *arg);
85 static void ar71xx_gpio_intr(void *arg);
90 static int ar71xx_gpio_pin_max(device_t dev, int *maxpin);
91 static int ar71xx_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
92 static int ar71xx_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
94 static int ar71xx_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
95 static int ar71xx_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
96 static int ar71xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
97 static int ar71xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
98 static int ar71xx_gpio_pin_toggle(device_t dev, uint32_t pin);
101 ar71xx_gpio_function_enable(struct ar71xx_gpio_softc *sc, uint32_t mask)
104 GPIO_SET_BITS(sc, AR71XX_GPIO_FUNCTION, mask);
109 ar71xx_gpio_function_disable(struct ar71xx_gpio_softc *sc, uint32_t mask)
112 GPIO_CLEAR_BITS(sc, AR71XX_GPIO_FUNCTION, mask);
117 ar71xx_gpio_pin_configure(struct ar71xx_gpio_softc *sc, struct gpio_pin *pin,
122 mask = 1 << pin->gp_pin;
126 * Manage input/output
128 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
129 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
130 if (flags & GPIO_PIN_OUTPUT) {
131 pin->gp_flags |= GPIO_PIN_OUTPUT;
132 GPIO_SET_BITS(sc, AR71XX_GPIO_OE, mask);
135 pin->gp_flags |= GPIO_PIN_INPUT;
136 GPIO_CLEAR_BITS(sc, AR71XX_GPIO_OE, mask);
144 ar71xx_gpio_pin_max(device_t dev, int *maxpin)
147 *maxpin = AR71XX_GPIO_PINS - 1;
152 ar71xx_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
154 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
157 for (i = 0; i < sc->gpio_npins; i++) {
158 if (sc->gpio_pins[i].gp_pin == pin)
162 if (i >= sc->gpio_npins)
166 *caps = sc->gpio_pins[i].gp_caps;
173 ar71xx_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
175 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
178 for (i = 0; i < sc->gpio_npins; i++) {
179 if (sc->gpio_pins[i].gp_pin == pin)
183 if (i >= sc->gpio_npins)
187 *flags = sc->gpio_pins[i].gp_flags;
194 ar71xx_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
196 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
199 for (i = 0; i < sc->gpio_npins; i++) {
200 if (sc->gpio_pins[i].gp_pin == pin)
204 if (i >= sc->gpio_npins)
208 memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
215 ar71xx_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
218 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
220 for (i = 0; i < sc->gpio_npins; i++) {
221 if (sc->gpio_pins[i].gp_pin == pin)
225 if (i >= sc->gpio_npins)
228 /* Filter out unwanted flags */
229 if ((flags &= sc->gpio_pins[i].gp_caps) != flags)
232 /* Can't mix input/output together */
233 if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) ==
234 (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT))
237 ar71xx_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
242 ar71xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
244 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
247 for (i = 0; i < sc->gpio_npins; i++) {
248 if (sc->gpio_pins[i].gp_pin == pin)
252 if (i >= sc->gpio_npins)
257 GPIO_WRITE(sc, AR71XX_GPIO_SET, (1 << pin));
259 GPIO_WRITE(sc, AR71XX_GPIO_CLEAR, (1 << pin));
266 ar71xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
268 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
271 for (i = 0; i < sc->gpio_npins; i++) {
272 if (sc->gpio_pins[i].gp_pin == pin)
276 if (i >= sc->gpio_npins)
280 *val = (GPIO_READ(sc, AR71XX_GPIO_IN) & (1 << pin)) ? 1 : 0;
287 ar71xx_gpio_pin_toggle(device_t dev, uint32_t pin)
290 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
292 for (i = 0; i < sc->gpio_npins; i++) {
293 if (sc->gpio_pins[i].gp_pin == pin)
297 if (i >= sc->gpio_npins)
301 res = (GPIO_READ(sc, AR71XX_GPIO_IN) & (1 << pin)) ? 1 : 0;
303 GPIO_WRITE(sc, AR71XX_GPIO_CLEAR, (1 << pin));
305 GPIO_WRITE(sc, AR71XX_GPIO_SET, (1 << pin));
312 ar71xx_gpio_filter(void *arg)
315 /* TODO: something useful */
316 return (FILTER_STRAY);
322 ar71xx_gpio_intr(void *arg)
324 struct ar71xx_gpio_softc *sc = arg;
326 /* TODO: something useful */
331 ar71xx_gpio_probe(device_t dev)
334 device_set_desc(dev, "Atheros AR71XX GPIO driver");
339 ar71xx_gpio_attach(device_t dev)
341 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
343 struct ar71xx_gpio_pin *pinp;
346 KASSERT((device_get_unit(dev) == 0),
347 ("ar71xx_gpio: Only one gpio module supported"));
349 mtx_init(&sc->gpio_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
352 /* Map control/status registers. */
353 sc->gpio_mem_rid = 0;
354 sc->gpio_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
355 &sc->gpio_mem_rid, RF_ACTIVE);
357 if (sc->gpio_mem_res == NULL) {
358 device_printf(dev, "couldn't map memory\n");
360 ar71xx_gpio_detach(dev);
364 if ((sc->gpio_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
365 &sc->gpio_irq_rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
366 device_printf(dev, "unable to allocate IRQ resource\n");
370 if ((bus_setup_intr(dev, sc->gpio_irq_res, INTR_TYPE_MISC,
371 ar71xx_gpio_filter, ar71xx_gpio_intr, sc, &sc->gpio_ih))) {
373 "WARNING: unable to register interrupt handler\n");
378 ar71xx_gpio_function_enable(sc, GPIO_FUNC_SPI_CS1_EN);
379 ar71xx_gpio_function_enable(sc, GPIO_FUNC_SPI_CS2_EN);
380 /* Configure all pins as input */
381 /* disable interrupts for all pins */
382 GPIO_WRITE(sc, AR71XX_GPIO_INT_MASK, 0);
383 pinp = ar71xx_gpio_pins;
386 strncpy(sc->gpio_pins[i].gp_name, pinp->name, GPIOMAXNAME);
387 sc->gpio_pins[i].gp_pin = pinp->pin;
388 sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
389 sc->gpio_pins[i].gp_flags = 0;
390 ar71xx_gpio_pin_configure(sc, &sc->gpio_pins[i], pinp->flags);
397 device_add_child(dev, "gpioc", device_get_unit(dev));
398 device_add_child(dev, "gpiobus", device_get_unit(dev));
399 return (bus_generic_attach(dev));
403 ar71xx_gpio_detach(device_t dev)
405 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
407 KASSERT(mtx_initialized(&sc->gpio_mtx), ("gpio mutex not initialized"));
409 ar71xx_gpio_function_disable(sc, GPIO_FUNC_SPI_CS1_EN);
410 ar71xx_gpio_function_disable(sc, GPIO_FUNC_SPI_CS2_EN);
411 bus_generic_detach(dev);
413 if (sc->gpio_mem_res)
414 bus_release_resource(dev, SYS_RES_MEMORY, sc->gpio_mem_rid,
417 mtx_destroy(&sc->gpio_mtx);
422 static device_method_t ar71xx_gpio_methods[] = {
423 DEVMETHOD(device_probe, ar71xx_gpio_probe),
424 DEVMETHOD(device_attach, ar71xx_gpio_attach),
425 DEVMETHOD(device_detach, ar71xx_gpio_detach),
428 DEVMETHOD(gpio_pin_max, ar71xx_gpio_pin_max),
429 DEVMETHOD(gpio_pin_getname, ar71xx_gpio_pin_getname),
430 DEVMETHOD(gpio_pin_getflags, ar71xx_gpio_pin_getflags),
431 DEVMETHOD(gpio_pin_getcaps, ar71xx_gpio_pin_getcaps),
432 DEVMETHOD(gpio_pin_setflags, ar71xx_gpio_pin_setflags),
433 DEVMETHOD(gpio_pin_get, ar71xx_gpio_pin_get),
434 DEVMETHOD(gpio_pin_set, ar71xx_gpio_pin_set),
435 DEVMETHOD(gpio_pin_toggle, ar71xx_gpio_pin_toggle),
439 static driver_t ar71xx_gpio_driver = {
442 sizeof(struct ar71xx_gpio_softc),
444 static devclass_t ar71xx_gpio_devclass;
446 DRIVER_MODULE(ar71xx_gpio, apb, ar71xx_gpio_driver, ar71xx_gpio_devclass, 0, 0);