2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
5 * Copyright (c) 2013, Luiz Otavio O Souza <loos@FreeBSD.org>
6 * Copyright (c) 2013 The FreeBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
41 #include <dev/gpio/gpiobusvar.h>
42 #include <dev/ofw/ofw_bus.h>
44 #include "gpiobus_if.h"
46 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
48 static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *);
49 static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *,
50 struct gpiobus_softc *, struct gpiobus_pin **);
53 * Utility functions for easier handling of OFW GPIO pins.
56 * GPIOBUS uses children's IVARs, so we cannot use this interface for cross
61 gpio_pin_get_by_ofw_propidx(device_t consumer, phandle_t cnode,
62 char *prop_name, int idx, gpio_pin_t *out_pin)
67 struct gpiobus_pin pin;
70 KASSERT(consumer != NULL && cnode > 0,
71 ("both consumer and cnode required"));
73 rv = ofw_bus_parse_xref_list_alloc(cnode, prop_name, "#gpio-cells",
74 idx, &xref, &ncells, &cells);
78 /* Translate provider to device. */
79 pin.dev = OF_device_from_xref(xref);
80 if (pin.dev == NULL) {
85 /* Test if GPIO bus already exist. */
86 busdev = GPIO_GET_BUS(pin.dev);
93 rv = gpio_map_gpios(pin.dev, cnode, OF_node_from_xref(xref), ncells,
94 cells, &pin.pin, &pin.flags);
99 /* Reserve GPIO pin. */
100 rv = gpiobus_acquire_pin(busdev, pin.pin);
104 *out_pin = malloc(sizeof(struct gpiobus_pin), M_DEVBUF,
111 gpio_pin_get_by_ofw_idx(device_t consumer, phandle_t node,
112 int idx, gpio_pin_t *pin)
115 return (gpio_pin_get_by_ofw_propidx(consumer, node, "gpios", idx, pin));
119 gpio_pin_get_by_ofw_property(device_t consumer, phandle_t node,
120 char *name, gpio_pin_t *pin)
123 return (gpio_pin_get_by_ofw_propidx(consumer, node, name, 0, pin));
127 gpio_pin_get_by_ofw_name(device_t consumer, phandle_t node,
128 char *name, gpio_pin_t *pin)
132 KASSERT(consumer != NULL && node > 0,
133 ("both consumer and node required"));
135 rv = ofw_bus_find_string_index(node, "gpio-names", name, &idx);
138 return (gpio_pin_get_by_ofw_idx(consumer, node, idx, pin));
142 * OFW_GPIOBUS driver.
145 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
149 struct gpiobus_ivar *devi;
150 struct ofw_gpiobus_devinfo *dinfo;
153 * Check to see if we already have a child for @p child, and if so
156 childdev = ofw_bus_find_child_device_by_phandle(bus, child);
157 if (childdev != NULL)
161 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
163 childdev = device_add_child(bus, drvname, -1);
164 if (childdev == NULL)
166 dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
168 device_delete_child(bus, childdev);
171 if (device_probe_and_attach(childdev) != 0) {
172 ofw_gpiobus_destroy_devinfo(bus, dinfo);
173 device_delete_child(bus, childdev);
176 /* Use the child name as pin name. */
177 devi = &dinfo->opd_dinfo;
178 for (i = 0; i < devi->npins; i++)
179 GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
180 device_get_nameunit(childdev));
186 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
187 struct gpiobus_pin **pins)
190 return (ofw_gpiobus_parse_gpios_impl(consumer,
191 ofw_bus_get_node(consumer), pname, NULL, pins));
195 ofw_gpiobus_register_provider(device_t provider)
199 node = ofw_bus_get_node(provider);
201 OF_device_register_xref(OF_xref_from_node(node), provider);
205 ofw_gpiobus_unregister_provider(device_t provider)
209 node = ofw_bus_get_node(provider);
211 OF_device_register_xref(OF_xref_from_node(node), NULL);
214 static struct ofw_gpiobus_devinfo *
215 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
218 struct gpiobus_ivar *devi;
219 struct gpiobus_pin *pins;
220 struct gpiobus_softc *sc;
221 struct ofw_gpiobus_devinfo *dinfo;
223 sc = device_get_softc(bus);
224 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
227 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
228 free(dinfo, M_DEVBUF);
231 /* Parse the gpios property for the child. */
232 npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
234 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
235 free(dinfo, M_DEVBUF);
238 /* Initialize the irq resource list. */
239 resource_list_init(&dinfo->opd_dinfo.rl);
240 /* Allocate the child ivars and copy the parsed pin data. */
241 devi = &dinfo->opd_dinfo;
242 devi->npins = (uint32_t)npins;
243 if (gpiobus_alloc_ivars(devi) != 0) {
244 free(pins, M_DEVBUF);
245 ofw_gpiobus_destroy_devinfo(bus, dinfo);
248 for (i = 0; i < devi->npins; i++)
249 devi->pins[i] = pins[i].pin;
250 free(pins, M_DEVBUF);
251 /* Parse the interrupt resources. */
252 if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) {
253 ofw_gpiobus_destroy_devinfo(bus, dinfo);
256 device_set_ivars(child, dinfo);
262 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
265 struct gpiobus_ivar *devi;
266 struct gpiobus_softc *sc;
268 sc = device_get_softc(bus);
269 devi = &dinfo->opd_dinfo;
270 for (i = 0; i < devi->npins; i++) {
271 if (devi->pins[i] > sc->sc_npins)
273 sc->sc_pins[devi->pins[i]].mapped = 0;
275 gpiobus_free_ivars(devi);
276 resource_list_free(&dinfo->opd_dinfo.rl);
277 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
278 free(dinfo, M_DEVBUF);
282 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
283 struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
285 int gpiocells, i, j, ncells, npins;
289 ncells = OF_getencprop_alloc_multi(cnode, pname, sizeof(*gpios),
292 device_printf(consumer,
293 "Warning: No %s specified in fdt data; "
294 "device may not function.\n", pname);
298 * The gpio-specifier is controller independent, the first pcell has
299 * the reference to the GPIO controller phandler.
300 * Count the number of encoded gpio-specifiers on the first pass.
305 /* Allow NULL specifiers. */
311 gpio = OF_node_from_xref(gpios[i]);
312 /* If we have bussc, ignore devices from other gpios. */
314 if (ofw_bus_get_node(bussc->sc_dev) != gpio)
317 * Check for gpio-controller property and read the #gpio-cells
318 * for this GPIO controller.
320 if (!OF_hasprop(gpio, "gpio-controller") ||
321 OF_getencprop(gpio, "#gpio-cells", &gpiocells,
322 sizeof(gpiocells)) < 0) {
323 device_printf(consumer,
324 "gpio reference is not a gpio-controller.\n");
328 if (ncells - i < gpiocells + 1) {
329 device_printf(consumer,
330 "%s cells doesn't match #gpio-cells.\n", pname);
336 if (npins == 0 || pins == NULL) {
338 device_printf(consumer, "no pin specified in %s.\n",
343 *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
349 /* Decode the gpio specifier on the second pass. */
353 /* Allow NULL specifiers. */
359 gpio = OF_node_from_xref(gpios[i]);
360 /* Read gpio-cells property for this GPIO controller. */
361 if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
362 sizeof(gpiocells)) < 0) {
363 device_printf(consumer,
364 "gpio does not have the #gpio-cells property.\n");
367 /* Return the device reference for the GPIO controller. */
368 (*pins)[j].dev = OF_device_from_xref(gpios[i]);
369 if ((*pins)[j].dev == NULL) {
370 device_printf(consumer,
371 "no device registered for the gpio controller.\n");
375 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
376 * retrieve it. The GPIO_GET_BUS() method is only valid after
377 * the child is probed and attached.
380 if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
381 device_printf(consumer,
382 "no gpiobus reference for %s.\n",
383 device_get_nameunit((*pins)[j].dev));
386 bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
388 /* Get the GPIO pin number and flags. */
389 if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
390 &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
391 device_printf(consumer,
392 "cannot map the gpios specifier.\n");
395 /* Reserve the GPIO pin. */
396 if (gpiobus_acquire_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
407 free(*pins, M_DEVBUF);
412 ofw_gpiobus_probe(device_t dev)
415 if (ofw_bus_get_node(dev) == -1)
417 device_set_desc(dev, "OFW GPIO bus");
419 return (BUS_PROBE_DEFAULT);
423 ofw_gpiobus_attach(device_t dev)
428 err = gpiobus_init_softc(dev);
431 bus_generic_probe(dev);
432 bus_enumerate_hinted_children(dev);
434 * Attach the children represented in the device tree.
436 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
437 child = OF_peer(child)) {
438 if (OF_hasprop(child, "gpio-hog"))
440 if (!OF_hasprop(child, "gpios"))
442 if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
446 return (bus_generic_attach(dev));
450 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
453 struct ofw_gpiobus_devinfo *devi;
455 child = device_add_child_ordered(dev, order, name, unit);
458 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
461 device_delete_child(dev, child);
466 * NULL all the OFW-related parts of the ivars for non-OFW
469 devi->opd_obdinfo.obd_node = -1;
470 devi->opd_obdinfo.obd_name = NULL;
471 devi->opd_obdinfo.obd_compat = NULL;
472 devi->opd_obdinfo.obd_type = NULL;
473 devi->opd_obdinfo.obd_model = NULL;
475 device_set_ivars(child, devi);
480 static const struct ofw_bus_devinfo *
481 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
483 struct ofw_gpiobus_devinfo *dinfo;
485 dinfo = device_get_ivars(dev);
487 return (&dinfo->opd_obdinfo);
490 static device_method_t ofw_gpiobus_methods[] = {
491 /* Device interface */
492 DEVMETHOD(device_probe, ofw_gpiobus_probe),
493 DEVMETHOD(device_attach, ofw_gpiobus_attach),
496 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
497 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child),
499 /* ofw_bus interface */
500 DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo),
501 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
502 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
503 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
504 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
505 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
510 devclass_t ofwgpiobus_devclass;
512 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
513 sizeof(struct gpiobus_softc), gpiobus_driver);
514 EARLY_DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass,
516 MODULE_VERSION(ofw_gpiobus, 1);
517 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);