2 * Copyright (c) 2018 Stormshield
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
27 #include <sys/param.h> /* defines used in kernel.h */
28 #include <sys/module.h>
29 #include <sys/systm.h>
30 #include <sys/errno.h>
31 #include <sys/kernel.h> /* types used in module initialization */
32 #include <sys/conf.h> /* cdevsw struct */
33 #include <sys/uio.h> /* uio struct */
34 #include <sys/malloc.h>
35 #include <sys/bus.h> /* structs, prototypes for pci bus stuff and DEVMETHOD macros! */
38 #include <machine/bus.h>
40 #include <machine/resource.h>
42 #include <dev/gpio/gpiobusvar.h>
46 #include "lewisburg_gpiocm.h"
48 #define P2SB_GROUP_GPIO_MAX_PINS 24
56 struct gpio_pin gpio_setup[P2SB_GROUP_GPIO_MAX_PINS];
60 lbggpio_get_bus(device_t dev)
62 struct lbggpio_softc *sc;
64 sc = device_get_softc(dev);
66 return (sc->sc_busdev);
70 lbggpio_pin_max(device_t dev, int *maxpin)
72 struct lbggpio_softc *sc;
77 sc = device_get_softc(dev);
79 *maxpin = sc->npins - 1;
85 lbggpio_pin_getname(device_t dev, uint32_t pin, char *name)
87 struct lbggpio_softc *sc = device_get_softc(dev);
95 strlcpy(name, sc->gpio_setup[pin].gp_name, GPIOMAXNAME);
101 lbggpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
103 struct lbggpio_softc *sc = device_get_softc(dev);
108 if (pin >= sc->npins)
111 *flags = sc->gpio_setup[pin].gp_flags;
117 lbggpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
119 struct lbggpio_softc *sc = device_get_softc(dev);
124 if (pin >= sc->npins)
127 *caps = sc->gpio_setup[pin].gp_caps;
133 lbggpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
135 struct lbggpio_softc *sc = device_get_softc(dev);
137 if (pin >= sc->npins)
140 /* Check for unwanted flags. */
141 if ((flags & sc->gpio_setup[pin].gp_caps) != flags)
144 lbggpiocm_pin_setflags(device_get_parent(dev), dev, pin, flags);
145 sc->gpio_setup[pin].gp_flags = flags;
151 lbggpio_pin_get(device_t dev, uint32_t pin, uint32_t *value)
153 struct lbggpio_softc *sc = device_get_softc(dev);
158 if (pin >= sc->npins)
161 return (lbggpiocm_pin_get(device_get_parent(dev), dev, pin, value));
165 lbggpio_pin_set(device_t dev, uint32_t pin, uint32_t value)
167 struct lbggpio_softc *sc = device_get_softc(dev);
169 if (pin >= sc->npins)
172 return (lbggpiocm_pin_set(device_get_parent(dev), dev, pin, value));
176 lbggpio_pin_toggle(device_t dev, uint32_t pin)
178 struct lbggpio_softc *sc = device_get_softc(dev);
180 if (pin >= sc->npins)
183 return (lbggpiocm_pin_toggle(device_get_parent(dev), dev, pin));
187 lbggpio_probe(device_t dev)
189 struct lbggpio_softc *sc = device_get_softc(dev);
190 /* X is a placeholder for the actual one letter group name. */
191 static char desc[] = "LewisBurg GPIO Group X";
193 sc->npins = lbggpiocm_get_group_npins(device_get_parent(dev), dev);
194 sc->grpname = lbggpiocm_get_group_name(device_get_parent(dev), dev);
198 desc[sizeof(desc)-2] = sc->grpname;
199 device_set_desc_copy(dev, desc);
200 return (BUS_PROBE_DEFAULT);
204 lbggpio_attach(device_t dev)
207 struct lbggpio_softc *sc;
209 sc = device_get_softc(dev);
211 for (i = 0; i < sc->npins; ++i) {
212 sc->gpio_setup[i].gp_pin = i;
213 snprintf(sc->gpio_setup[i].gp_name,
214 sizeof(sc->gpio_setup[i].gp_name),
215 "GPIO %c%u", sc->grpname, i);
216 sc->gpio_setup[i].gp_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
220 sc->sc_busdev = gpiobus_attach_bus(dev);
221 if (sc->sc_busdev == NULL)
228 lbggpio_detach(device_t dev)
230 struct lbggpio_softc *sc;
232 sc = device_get_softc(dev);
235 gpiobus_detach_bus(dev);
240 static device_method_t lbggpio_methods[] = {
241 /* Device interface */
242 DEVMETHOD(device_probe, lbggpio_probe),
243 DEVMETHOD(device_attach, lbggpio_attach),
244 DEVMETHOD(device_detach, lbggpio_detach),
247 DEVMETHOD(gpio_get_bus, lbggpio_get_bus),
248 DEVMETHOD(gpio_pin_max, lbggpio_pin_max),
249 DEVMETHOD(gpio_pin_getcaps, lbggpio_pin_getcaps),
250 DEVMETHOD(gpio_pin_getflags, lbggpio_pin_getflags),
251 DEVMETHOD(gpio_pin_setflags, lbggpio_pin_setflags),
252 DEVMETHOD(gpio_pin_getname, lbggpio_pin_getname),
253 DEVMETHOD(gpio_pin_set, lbggpio_pin_set),
254 DEVMETHOD(gpio_pin_get, lbggpio_pin_get),
255 DEVMETHOD(gpio_pin_toggle, lbggpio_pin_toggle),
260 static driver_t lbggpio_driver = {
263 sizeof(struct lbggpio_softc)
266 static devclass_t lbggpio_devclass;
268 DRIVER_MODULE(lbggpio, lbggpiocm, lbggpio_driver, lbggpio_devclass, NULL, NULL);
269 MODULE_DEPEND(lbggpio, gpiobus, 1, 1, 1);