2 * SPDX-License-Identifier: BSD-2-Clause
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 #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 #include "gpiobus_if.h"
44 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
46 static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *);
47 static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *,
48 struct gpiobus_softc *, struct gpiobus_pin **);
51 * Utility functions for easier handling of OFW GPIO pins.
54 * GPIOBUS uses children's IVARs, so we cannot use this interface for cross
59 gpio_pin_get_by_ofw_propidx(device_t consumer, phandle_t cnode,
60 char *prop_name, int idx, gpio_pin_t *out_pin)
65 struct gpiobus_pin pin;
68 KASSERT(consumer != NULL && cnode > 0,
69 ("both consumer and cnode required"));
71 rv = ofw_bus_parse_xref_list_alloc(cnode, prop_name, "#gpio-cells",
72 idx, &xref, &ncells, &cells);
76 /* Translate provider to device. */
77 pin.dev = OF_device_from_xref(xref);
78 if (pin.dev == NULL) {
83 /* Test if GPIO bus already exist. */
84 busdev = GPIO_GET_BUS(pin.dev);
91 rv = gpio_map_gpios(pin.dev, cnode, OF_node_from_xref(xref), ncells,
92 cells, &pin.pin, &pin.flags);
97 /* Reserve GPIO pin. */
98 rv = gpiobus_acquire_pin(busdev, pin.pin);
102 *out_pin = malloc(sizeof(struct gpiobus_pin), M_DEVBUF,
109 gpio_pin_get_by_ofw_idx(device_t consumer, phandle_t node,
110 int idx, gpio_pin_t *pin)
113 return (gpio_pin_get_by_ofw_propidx(consumer, node, "gpios", idx, pin));
117 gpio_pin_get_by_ofw_property(device_t consumer, phandle_t node,
118 char *name, gpio_pin_t *pin)
121 return (gpio_pin_get_by_ofw_propidx(consumer, node, name, 0, pin));
125 gpio_pin_get_by_ofw_name(device_t consumer, phandle_t node,
126 char *name, gpio_pin_t *pin)
130 KASSERT(consumer != NULL && node > 0,
131 ("both consumer and node required"));
133 rv = ofw_bus_find_string_index(node, "gpio-names", name, &idx);
136 return (gpio_pin_get_by_ofw_idx(consumer, node, idx, pin));
140 * OFW_GPIOBUS driver.
143 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
147 struct gpiobus_ivar *devi;
148 struct ofw_gpiobus_devinfo *dinfo;
151 * Check to see if we already have a child for @p child, and if so
154 childdev = ofw_bus_find_child_device_by_phandle(bus, child);
155 if (childdev != NULL)
159 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
161 childdev = device_add_child(bus, drvname, -1);
162 if (childdev == NULL)
164 dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
166 device_delete_child(bus, childdev);
169 if (device_probe_and_attach(childdev) != 0) {
170 ofw_gpiobus_destroy_devinfo(bus, dinfo);
171 device_delete_child(bus, childdev);
174 /* Use the child name as pin name. */
175 devi = &dinfo->opd_dinfo;
176 for (i = 0; i < devi->npins; i++)
177 GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
178 device_get_nameunit(childdev));
184 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
185 struct gpiobus_pin **pins)
188 return (ofw_gpiobus_parse_gpios_impl(consumer,
189 ofw_bus_get_node(consumer), pname, NULL, pins));
193 ofw_gpiobus_register_provider(device_t provider)
197 node = ofw_bus_get_node(provider);
199 OF_device_register_xref(OF_xref_from_node(node), provider);
203 ofw_gpiobus_unregister_provider(device_t provider)
207 node = ofw_bus_get_node(provider);
209 OF_device_register_xref(OF_xref_from_node(node), NULL);
212 static struct ofw_gpiobus_devinfo *
213 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
216 struct gpiobus_ivar *devi;
217 struct gpiobus_pin *pins;
218 struct gpiobus_softc *sc;
219 struct ofw_gpiobus_devinfo *dinfo;
221 sc = device_get_softc(bus);
222 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
225 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
226 free(dinfo, M_DEVBUF);
229 /* Parse the gpios property for the child. */
230 npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
232 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
233 free(dinfo, M_DEVBUF);
236 /* Initialize the irq resource list. */
237 resource_list_init(&dinfo->opd_dinfo.rl);
238 /* Allocate the child ivars and copy the parsed pin data. */
239 devi = &dinfo->opd_dinfo;
240 devi->npins = (uint32_t)npins;
241 if (gpiobus_alloc_ivars(devi) != 0) {
242 free(pins, M_DEVBUF);
243 ofw_gpiobus_destroy_devinfo(bus, dinfo);
246 for (i = 0; i < devi->npins; i++)
247 devi->pins[i] = pins[i].pin;
248 free(pins, M_DEVBUF);
249 /* Parse the interrupt resources. */
250 if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) {
251 ofw_gpiobus_destroy_devinfo(bus, dinfo);
254 device_set_ivars(child, dinfo);
260 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
263 struct gpiobus_ivar *devi;
264 struct gpiobus_softc *sc;
266 sc = device_get_softc(bus);
267 devi = &dinfo->opd_dinfo;
268 for (i = 0; i < devi->npins; i++) {
269 if (devi->pins[i] > sc->sc_npins)
271 sc->sc_pins[devi->pins[i]].mapped = 0;
273 gpiobus_free_ivars(devi);
274 resource_list_free(&dinfo->opd_dinfo.rl);
275 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
276 free(dinfo, M_DEVBUF);
280 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
281 struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
283 int gpiocells, i, j, ncells, npins;
287 ncells = OF_getencprop_alloc_multi(cnode, pname, sizeof(*gpios),
290 device_printf(consumer,
291 "Warning: No %s specified in fdt data; "
292 "device may not function.\n", pname);
296 * The gpio-specifier is controller independent, the first pcell has
297 * the reference to the GPIO controller phandler.
298 * Count the number of encoded gpio-specifiers on the first pass.
303 /* Allow NULL specifiers. */
309 gpio = OF_node_from_xref(gpios[i]);
310 /* If we have bussc, ignore devices from other gpios. */
312 if (ofw_bus_get_node(bussc->sc_dev) != gpio)
315 * Check for gpio-controller property and read the #gpio-cells
316 * for this GPIO controller.
318 if (!OF_hasprop(gpio, "gpio-controller") ||
319 OF_getencprop(gpio, "#gpio-cells", &gpiocells,
320 sizeof(gpiocells)) < 0) {
321 device_printf(consumer,
322 "gpio reference is not a gpio-controller.\n");
326 if (ncells - i < gpiocells + 1) {
327 device_printf(consumer,
328 "%s cells doesn't match #gpio-cells.\n", pname);
334 if (npins == 0 || pins == NULL) {
336 device_printf(consumer, "no pin specified in %s.\n",
341 *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
347 /* Decode the gpio specifier on the second pass. */
351 /* Allow NULL specifiers. */
357 gpio = OF_node_from_xref(gpios[i]);
358 /* Read gpio-cells property for this GPIO controller. */
359 if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
360 sizeof(gpiocells)) < 0) {
361 device_printf(consumer,
362 "gpio does not have the #gpio-cells property.\n");
365 /* Return the device reference for the GPIO controller. */
366 (*pins)[j].dev = OF_device_from_xref(gpios[i]);
367 if ((*pins)[j].dev == NULL) {
368 device_printf(consumer,
369 "no device registered for the gpio controller.\n");
373 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
374 * retrieve it. The GPIO_GET_BUS() method is only valid after
375 * the child is probed and attached.
378 if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
379 device_printf(consumer,
380 "no gpiobus reference for %s.\n",
381 device_get_nameunit((*pins)[j].dev));
384 bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
386 /* Get the GPIO pin number and flags. */
387 if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
388 &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
389 device_printf(consumer,
390 "cannot map the gpios specifier.\n");
393 /* Reserve the GPIO pin. */
394 if (gpiobus_acquire_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
405 free(*pins, M_DEVBUF);
410 ofw_gpiobus_probe(device_t dev)
413 if (ofw_bus_get_node(dev) == -1)
415 device_set_desc(dev, "OFW GPIO bus");
417 return (BUS_PROBE_DEFAULT);
421 ofw_gpiobus_attach(device_t dev)
426 err = gpiobus_init_softc(dev);
429 bus_generic_probe(dev);
430 bus_enumerate_hinted_children(dev);
432 * Attach the children represented in the device tree.
434 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
435 child = OF_peer(child)) {
436 if (OF_hasprop(child, "gpio-hog"))
438 if (!OF_hasprop(child, "gpios"))
440 if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
444 return (bus_generic_attach(dev));
448 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
451 struct ofw_gpiobus_devinfo *devi;
453 child = device_add_child_ordered(dev, order, name, unit);
456 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
459 device_delete_child(dev, child);
464 * NULL all the OFW-related parts of the ivars for non-OFW
467 devi->opd_obdinfo.obd_node = -1;
468 devi->opd_obdinfo.obd_name = NULL;
469 devi->opd_obdinfo.obd_compat = NULL;
470 devi->opd_obdinfo.obd_type = NULL;
471 devi->opd_obdinfo.obd_model = NULL;
473 device_set_ivars(child, devi);
478 static const struct ofw_bus_devinfo *
479 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
481 struct ofw_gpiobus_devinfo *dinfo;
483 dinfo = device_get_ivars(dev);
485 return (&dinfo->opd_obdinfo);
488 static device_method_t ofw_gpiobus_methods[] = {
489 /* Device interface */
490 DEVMETHOD(device_probe, ofw_gpiobus_probe),
491 DEVMETHOD(device_attach, ofw_gpiobus_attach),
494 DEVMETHOD(bus_child_pnpinfo, ofw_bus_gen_child_pnpinfo),
495 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child),
497 /* ofw_bus interface */
498 DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo),
499 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
500 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
501 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
502 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
503 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
508 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
509 sizeof(struct gpiobus_softc), gpiobus_driver);
510 EARLY_DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, 0, 0, BUS_PASS_BUS);
511 MODULE_VERSION(ofw_gpiobus, 1);
512 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);