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 #define GPIO_ACTIVE_LOW 1
48 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
50 static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *);
51 static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *,
52 struct gpiobus_softc *, struct gpiobus_pin **);
55 * Utility functions for easier handling of OFW GPIO pins.
58 * GPIOBUS uses children's IVARs, so we cannot use this interface for cross
63 gpio_pin_get_by_ofw_impl(device_t consumer, phandle_t cnode,
64 char *prop_name, int idx, gpio_pin_t *out_pin)
69 struct gpiobus_pin pin;
72 KASSERT(consumer != NULL && cnode > 0,
73 ("both consumer and cnode required"));
75 rv = ofw_bus_parse_xref_list_alloc(cnode, prop_name, "#gpio-cells",
76 idx, &xref, &ncells, &cells);
80 /* Translate provider to device. */
81 pin.dev = OF_device_from_xref(xref);
82 if (pin.dev == NULL) {
87 /* Test if GPIO bus already exist. */
88 busdev = GPIO_GET_BUS(pin.dev);
95 rv = gpio_map_gpios(pin.dev, cnode, OF_node_from_xref(xref), ncells,
96 cells, &pin.pin, &pin.flags);
101 /* Reserve GPIO pin. */
102 rv = gpiobus_acquire_pin(busdev, pin.pin);
106 *out_pin = malloc(sizeof(struct gpiobus_pin), M_DEVBUF,
113 gpio_pin_get_by_ofw_idx(device_t consumer, phandle_t node,
114 int idx, gpio_pin_t *pin)
117 return (gpio_pin_get_by_ofw_impl(consumer, node, "gpios", idx, pin));
121 gpio_pin_get_by_ofw_property(device_t consumer, phandle_t node,
122 char *name, gpio_pin_t *pin)
125 return (gpio_pin_get_by_ofw_impl(consumer, node, name, 0, pin));
129 gpio_pin_get_by_ofw_name(device_t consumer, phandle_t node,
130 char *name, gpio_pin_t *pin)
134 KASSERT(consumer != NULL && node > 0,
135 ("both consumer and node required"));
137 rv = ofw_bus_find_string_index(node, "gpio-names", name, &idx);
140 return (gpio_pin_get_by_ofw_idx(consumer, node, idx, pin));
144 gpio_pin_release(gpio_pin_t gpio)
151 KASSERT(gpio->dev != NULL, ("invalid pin state"));
153 busdev = GPIO_GET_BUS(gpio->dev);
155 gpiobus_release_pin(busdev, gpio->pin);
157 /* XXXX Unreserve pin. */
158 free(gpio, M_DEVBUF);
162 gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps)
165 KASSERT(pin != NULL, ("GPIO pin is NULL."));
166 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
167 return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps));
171 gpio_pin_is_active(gpio_pin_t pin, bool *active)
176 KASSERT(pin != NULL, ("GPIO pin is NULL."));
177 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
178 rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp);
183 if (pin->flags & GPIO_ACTIVE_LOW)
191 gpio_pin_set_active(gpio_pin_t pin, bool active)
196 if (pin->flags & GPIO_ACTIVE_LOW)
197 tmp = active ? 0 : 1;
199 tmp = active ? 1 : 0;
201 KASSERT(pin != NULL, ("GPIO pin is NULL."));
202 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
203 rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp);
208 gpio_pin_setflags(gpio_pin_t pin, uint32_t flags)
212 KASSERT(pin != NULL, ("GPIO pin is NULL."));
213 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
215 rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
220 * OFW_GPIOBUS driver.
223 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
227 struct gpiobus_ivar *devi;
228 struct ofw_gpiobus_devinfo *dinfo;
231 * Check to see if we already have a child for @p child, and if so
234 childdev = ofw_bus_find_child_device_by_phandle(bus, child);
235 if (childdev != NULL)
239 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
241 childdev = device_add_child(bus, drvname, -1);
242 if (childdev == NULL)
244 dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
246 device_delete_child(bus, childdev);
249 if (device_probe_and_attach(childdev) != 0) {
250 ofw_gpiobus_destroy_devinfo(bus, dinfo);
251 device_delete_child(bus, childdev);
254 /* Use the child name as pin name. */
255 devi = &dinfo->opd_dinfo;
256 for (i = 0; i < devi->npins; i++)
257 GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
258 device_get_nameunit(childdev));
264 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
265 struct gpiobus_pin **pins)
268 return (ofw_gpiobus_parse_gpios_impl(consumer,
269 ofw_bus_get_node(consumer), pname, NULL, pins));
273 ofw_gpiobus_register_provider(device_t provider)
277 node = ofw_bus_get_node(provider);
278 OF_device_register_xref(OF_xref_from_node(node), provider);
282 ofw_gpiobus_unregister_provider(device_t provider)
286 node = ofw_bus_get_node(provider);
287 OF_device_register_xref(OF_xref_from_node(node), NULL);
290 static struct ofw_gpiobus_devinfo *
291 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
294 struct gpiobus_ivar *devi;
295 struct gpiobus_pin *pins;
296 struct gpiobus_softc *sc;
297 struct ofw_gpiobus_devinfo *dinfo;
299 sc = device_get_softc(bus);
300 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
303 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
304 free(dinfo, M_DEVBUF);
307 /* Parse the gpios property for the child. */
308 npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
310 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
311 free(dinfo, M_DEVBUF);
314 /* Initialize the irq resource list. */
315 resource_list_init(&dinfo->opd_dinfo.rl);
316 /* Allocate the child ivars and copy the parsed pin data. */
317 devi = &dinfo->opd_dinfo;
318 devi->npins = (uint32_t)npins;
319 if (gpiobus_alloc_ivars(devi) != 0) {
320 free(pins, M_DEVBUF);
321 ofw_gpiobus_destroy_devinfo(bus, dinfo);
324 for (i = 0; i < devi->npins; i++) {
325 devi->flags[i] = pins[i].flags;
326 devi->pins[i] = pins[i].pin;
328 free(pins, M_DEVBUF);
329 /* Parse the interrupt resources. */
330 if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) {
331 ofw_gpiobus_destroy_devinfo(bus, dinfo);
334 device_set_ivars(child, dinfo);
340 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
343 struct gpiobus_ivar *devi;
344 struct gpiobus_softc *sc;
346 sc = device_get_softc(bus);
347 devi = &dinfo->opd_dinfo;
348 for (i = 0; i < devi->npins; i++) {
349 if (devi->pins[i] > sc->sc_npins)
351 sc->sc_pins[devi->pins[i]].mapped = 0;
353 gpiobus_free_ivars(devi);
354 resource_list_free(&dinfo->opd_dinfo.rl);
355 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
356 free(dinfo, M_DEVBUF);
360 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
361 struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
363 int gpiocells, i, j, ncells, npins;
367 ncells = OF_getencprop_alloc(cnode, pname, sizeof(*gpios),
370 device_printf(consumer,
371 "Warning: No %s specified in fdt data; "
372 "device may not function.\n", pname);
376 * The gpio-specifier is controller independent, the first pcell has
377 * the reference to the GPIO controller phandler.
378 * Count the number of encoded gpio-specifiers on the first pass.
383 /* Allow NULL specifiers. */
389 gpio = OF_node_from_xref(gpios[i]);
390 /* If we have bussc, ignore devices from other gpios. */
392 if (ofw_bus_get_node(bussc->sc_dev) != gpio)
395 * Check for gpio-controller property and read the #gpio-cells
396 * for this GPIO controller.
398 if (!OF_hasprop(gpio, "gpio-controller") ||
399 OF_getencprop(gpio, "#gpio-cells", &gpiocells,
400 sizeof(gpiocells)) < 0) {
401 device_printf(consumer,
402 "gpio reference is not a gpio-controller.\n");
406 if (ncells - i < gpiocells + 1) {
407 device_printf(consumer,
408 "%s cells doesn't match #gpio-cells.\n", pname);
414 if (npins == 0 || pins == NULL) {
416 device_printf(consumer, "no pin specified in %s.\n",
421 *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
427 /* Decode the gpio specifier on the second pass. */
431 /* Allow NULL specifiers. */
437 gpio = OF_node_from_xref(gpios[i]);
438 /* Read gpio-cells property for this GPIO controller. */
439 if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
440 sizeof(gpiocells)) < 0) {
441 device_printf(consumer,
442 "gpio does not have the #gpio-cells property.\n");
445 /* Return the device reference for the GPIO controller. */
446 (*pins)[j].dev = OF_device_from_xref(gpios[i]);
447 if ((*pins)[j].dev == NULL) {
448 device_printf(consumer,
449 "no device registered for the gpio controller.\n");
453 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
454 * retrieve it. The GPIO_GET_BUS() method is only valid after
455 * the child is probed and attached.
458 if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
459 device_printf(consumer,
460 "no gpiobus reference for %s.\n",
461 device_get_nameunit((*pins)[j].dev));
464 bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
466 /* Get the GPIO pin number and flags. */
467 if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
468 &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
469 device_printf(consumer,
470 "cannot map the gpios specifier.\n");
473 /* Reserve the GPIO pin. */
474 if (gpiobus_acquire_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
485 free(*pins, M_DEVBUF);
490 ofw_gpiobus_probe(device_t dev)
493 if (ofw_bus_get_node(dev) == -1)
495 device_set_desc(dev, "OFW GPIO bus");
501 ofw_gpiobus_attach(device_t dev)
506 err = gpiobus_init_softc(dev);
509 bus_generic_probe(dev);
510 bus_enumerate_hinted_children(dev);
512 * Attach the children represented in the device tree.
514 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
515 child = OF_peer(child)) {
516 if (!OF_hasprop(child, "gpios"))
518 if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
522 return (bus_generic_attach(dev));
526 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
529 struct ofw_gpiobus_devinfo *devi;
531 child = device_add_child_ordered(dev, order, name, unit);
534 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
537 device_delete_child(dev, child);
542 * NULL all the OFW-related parts of the ivars for non-OFW
545 devi->opd_obdinfo.obd_node = -1;
546 devi->opd_obdinfo.obd_name = NULL;
547 devi->opd_obdinfo.obd_compat = NULL;
548 devi->opd_obdinfo.obd_type = NULL;
549 devi->opd_obdinfo.obd_model = NULL;
551 device_set_ivars(child, devi);
556 static const struct ofw_bus_devinfo *
557 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
559 struct ofw_gpiobus_devinfo *dinfo;
561 dinfo = device_get_ivars(dev);
563 return (&dinfo->opd_obdinfo);
566 static device_method_t ofw_gpiobus_methods[] = {
567 /* Device interface */
568 DEVMETHOD(device_probe, ofw_gpiobus_probe),
569 DEVMETHOD(device_attach, ofw_gpiobus_attach),
572 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
573 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child),
575 /* ofw_bus interface */
576 DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo),
577 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
578 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
579 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
580 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
581 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
586 devclass_t ofwgpiobus_devclass;
588 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
589 sizeof(struct gpiobus_softc), gpiobus_driver);
590 EARLY_DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass,
592 MODULE_VERSION(ofw_gpiobus, 1);
593 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);