2 * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
3 * Copyright (c) 2013, Luiz Otavio O Souza <loos@FreeBSD.org>
4 * Copyright (c) 2013 The FreeBSD Foundation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <dev/gpio/gpiobusvar.h>
40 #include <dev/ofw/ofw_bus.h>
42 static int ofw_gpiobus_parse_gpios(struct gpiobus_softc *,
43 struct gpiobus_ivar *, phandle_t);
44 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
46 static void ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *);
49 ofw_gpiobus_add_fdt_child(device_t bus, phandle_t child)
51 struct ofw_gpiobus_devinfo *dinfo;
55 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
57 dinfo = ofw_gpiobus_setup_devinfo(bus, child);
60 childdev = device_add_child(bus, NULL, -1);
61 if (childdev == NULL) {
62 device_printf(bus, "could not add child: %s\n",
63 dinfo->opd_obdinfo.obd_name);
64 ofw_gpiobus_destroy_devinfo(dinfo);
67 device_set_ivars(childdev, dinfo);
73 ofw_gpiobus_alloc_ivars(struct gpiobus_ivar *dinfo)
76 /* Allocate pins and flags memory. */
77 dinfo->pins = malloc(sizeof(uint32_t) * dinfo->npins, M_DEVBUF,
79 if (dinfo->pins == NULL)
81 dinfo->flags = malloc(sizeof(uint32_t) * dinfo->npins, M_DEVBUF,
83 if (dinfo->flags == NULL) {
84 free(dinfo->pins, M_DEVBUF);
92 ofw_gpiobus_free_ivars(struct gpiobus_ivar *dinfo)
95 free(dinfo->flags, M_DEVBUF);
96 free(dinfo->pins, M_DEVBUF);
100 ofw_gpiobus_parse_gpios(struct gpiobus_softc *sc, struct gpiobus_ivar *dinfo,
103 int cells, i, j, len;
107 /* Retrieve the gpios property. */
108 if ((len = OF_getproplen(child, "gpios")) < 0)
110 gpios = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
113 if (OF_getencprop(child, "gpios", gpios, len) < 0) {
114 free(gpios, M_DEVBUF);
119 * The gpio-specifier is controller independent, but the first pcell
120 * has the reference to the GPIO controller phandler.
121 * One the first pass we count the number of encoded gpio-specifiers.
124 len /= sizeof(pcell_t);
126 /* Allow NULL specifiers. */
132 gpio = OF_xref_phandle(gpios[i]);
133 /* Verify if we're attaching to the correct GPIO controller. */
134 if (!OF_hasprop(gpio, "gpio-controller") ||
135 gpio != ofw_bus_get_node(sc->sc_dev)) {
136 free(gpios, M_DEVBUF);
139 /* Read gpio-cells property for this GPIO controller. */
140 if (OF_getencprop(gpio, "#gpio-cells", &cells,
141 sizeof(cells)) < 0) {
142 free(gpios, M_DEVBUF);
149 if (dinfo->npins == 0) {
150 free(gpios, M_DEVBUF);
154 /* Allocate the child resources. */
155 if (ofw_gpiobus_alloc_ivars(dinfo) != 0) {
156 free(gpios, M_DEVBUF);
160 /* Decode the gpio specifier on the second pass. */
164 /* Allow NULL specifiers. */
171 gpio = OF_xref_phandle(gpios[i]);
172 /* Read gpio-cells property for this GPIO controller. */
173 if (OF_getencprop(gpio, "#gpio-cells", &cells,
174 sizeof(cells)) < 0) {
175 ofw_gpiobus_free_ivars(dinfo);
176 free(gpios, M_DEVBUF);
180 /* Get the GPIO pin number and flags. */
181 if (gpio_map_gpios(sc->sc_dev, child, gpio, cells,
182 &gpios[i + 1], &dinfo->pins[j], &dinfo->flags[j]) != 0) {
183 ofw_gpiobus_free_ivars(dinfo);
184 free(gpios, M_DEVBUF);
188 /* Consistency check. */
189 if (dinfo->pins[j] > sc->sc_npins) {
190 device_printf(sc->sc_busdev,
191 "invalid pin %d, max: %d\n",
192 dinfo->pins[j], sc->sc_npins - 1);
193 ofw_gpiobus_free_ivars(dinfo);
194 free(gpios, M_DEVBUF);
199 * Mark pin as mapped and give warning if it's already mapped.
201 if (sc->sc_pins_mapped[dinfo->pins[j]]) {
202 device_printf(sc->sc_busdev,
203 "warning: pin %d is already mapped\n",
205 ofw_gpiobus_free_ivars(dinfo);
206 free(gpios, M_DEVBUF);
209 sc->sc_pins_mapped[dinfo->pins[j]] = 1;
215 free(gpios, M_DEVBUF);
220 static struct ofw_gpiobus_devinfo *
221 ofw_gpiobus_setup_devinfo(device_t dev, phandle_t node)
223 struct gpiobus_softc *sc;
224 struct ofw_gpiobus_devinfo *dinfo;
226 sc = device_get_softc(dev);
227 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
230 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
231 free(dinfo, M_DEVBUF);
235 /* Parse the gpios property for the child. */
236 if (ofw_gpiobus_parse_gpios(sc, &dinfo->opd_dinfo, node) != 0) {
237 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
238 free(dinfo, M_DEVBUF);
246 ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *dinfo)
249 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
250 free(dinfo, M_DEVBUF);
254 ofw_gpiobus_probe(device_t dev)
257 if (ofw_bus_get_node(dev) == -1)
259 device_set_desc(dev, "OFW GPIO bus");
265 ofw_gpiobus_attach(device_t dev)
267 struct gpiobus_softc *sc;
270 sc = GPIOBUS_SOFTC(dev);
272 sc->sc_dev = device_get_parent(dev);
274 /* Read the pin max. value */
275 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0)
278 KASSERT(sc->sc_npins != 0, ("GPIO device with no pins"));
281 * Increase to get number of pins.
285 sc->sc_pins_mapped = malloc(sizeof(int) * sc->sc_npins, M_DEVBUF,
288 if (!sc->sc_pins_mapped)
291 /* Init the bus lock. */
292 GPIOBUS_LOCK_INIT(sc);
294 bus_generic_probe(dev);
295 bus_enumerate_hinted_children(dev);
298 * Attach the children represented in the device tree.
300 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
301 child = OF_peer(child))
302 if (ofw_gpiobus_add_fdt_child(dev, child) == NULL)
305 return (bus_generic_attach(dev));
309 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
312 struct ofw_gpiobus_devinfo *devi;
314 child = device_add_child_ordered(dev, order, name, unit);
317 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
320 device_delete_child(dev, child);
325 * NULL all the OFW-related parts of the ivars for non-OFW
328 devi->opd_obdinfo.obd_node = -1;
329 devi->opd_obdinfo.obd_name = NULL;
330 devi->opd_obdinfo.obd_compat = NULL;
331 devi->opd_obdinfo.obd_type = NULL;
332 devi->opd_obdinfo.obd_model = NULL;
334 device_set_ivars(child, devi);
340 ofw_gpiobus_print_child(device_t dev, device_t child)
342 struct ofw_gpiobus_devinfo *devi;
345 devi = device_get_ivars(child);
346 retval += bus_print_child_header(dev, child);
347 retval += printf(" at pin(s) ");
348 gpiobus_print_pins(&devi->opd_dinfo);
349 retval += bus_print_child_footer(dev, child);
354 static const struct ofw_bus_devinfo *
355 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
357 struct ofw_gpiobus_devinfo *dinfo;
359 dinfo = device_get_ivars(dev);
361 return (&dinfo->opd_obdinfo);
364 static device_method_t ofw_gpiobus_methods[] = {
365 /* Device interface */
366 DEVMETHOD(device_probe, ofw_gpiobus_probe),
367 DEVMETHOD(device_attach, ofw_gpiobus_attach),
370 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
371 DEVMETHOD(bus_print_child, ofw_gpiobus_print_child),
372 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child),
374 /* ofw_bus interface */
375 DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo),
376 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
377 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
378 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
379 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
380 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
385 static devclass_t ofwgpiobus_devclass;
387 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
388 sizeof(struct gpiobus_softc), gpiobus_driver);
389 DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0);
390 MODULE_VERSION(ofw_gpiobus, 1);
391 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);