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_propidx(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_propidx(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_propidx(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->pins[i] = pins[i].pin;
326 free(pins, M_DEVBUF);
327 /* Parse the interrupt resources. */
328 if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) {
329 ofw_gpiobus_destroy_devinfo(bus, dinfo);
332 device_set_ivars(child, dinfo);
338 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
341 struct gpiobus_ivar *devi;
342 struct gpiobus_softc *sc;
344 sc = device_get_softc(bus);
345 devi = &dinfo->opd_dinfo;
346 for (i = 0; i < devi->npins; i++) {
347 if (devi->pins[i] > sc->sc_npins)
349 sc->sc_pins[devi->pins[i]].mapped = 0;
351 gpiobus_free_ivars(devi);
352 resource_list_free(&dinfo->opd_dinfo.rl);
353 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
354 free(dinfo, M_DEVBUF);
358 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
359 struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
361 int gpiocells, i, j, ncells, npins;
365 ncells = OF_getencprop_alloc_multi(cnode, pname, sizeof(*gpios),
368 device_printf(consumer,
369 "Warning: No %s specified in fdt data; "
370 "device may not function.\n", pname);
374 * The gpio-specifier is controller independent, the first pcell has
375 * the reference to the GPIO controller phandler.
376 * Count the number of encoded gpio-specifiers on the first pass.
381 /* Allow NULL specifiers. */
387 gpio = OF_node_from_xref(gpios[i]);
388 /* If we have bussc, ignore devices from other gpios. */
390 if (ofw_bus_get_node(bussc->sc_dev) != gpio)
393 * Check for gpio-controller property and read the #gpio-cells
394 * for this GPIO controller.
396 if (!OF_hasprop(gpio, "gpio-controller") ||
397 OF_getencprop(gpio, "#gpio-cells", &gpiocells,
398 sizeof(gpiocells)) < 0) {
399 device_printf(consumer,
400 "gpio reference is not a gpio-controller.\n");
404 if (ncells - i < gpiocells + 1) {
405 device_printf(consumer,
406 "%s cells doesn't match #gpio-cells.\n", pname);
412 if (npins == 0 || pins == NULL) {
414 device_printf(consumer, "no pin specified in %s.\n",
419 *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
425 /* Decode the gpio specifier on the second pass. */
429 /* Allow NULL specifiers. */
435 gpio = OF_node_from_xref(gpios[i]);
436 /* Read gpio-cells property for this GPIO controller. */
437 if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
438 sizeof(gpiocells)) < 0) {
439 device_printf(consumer,
440 "gpio does not have the #gpio-cells property.\n");
443 /* Return the device reference for the GPIO controller. */
444 (*pins)[j].dev = OF_device_from_xref(gpios[i]);
445 if ((*pins)[j].dev == NULL) {
446 device_printf(consumer,
447 "no device registered for the gpio controller.\n");
451 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
452 * retrieve it. The GPIO_GET_BUS() method is only valid after
453 * the child is probed and attached.
456 if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
457 device_printf(consumer,
458 "no gpiobus reference for %s.\n",
459 device_get_nameunit((*pins)[j].dev));
462 bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
464 /* Get the GPIO pin number and flags. */
465 if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
466 &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
467 device_printf(consumer,
468 "cannot map the gpios specifier.\n");
471 /* Reserve the GPIO pin. */
472 if (gpiobus_acquire_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
483 free(*pins, M_DEVBUF);
488 ofw_gpiobus_probe(device_t dev)
491 if (ofw_bus_get_node(dev) == -1)
493 device_set_desc(dev, "OFW GPIO bus");
499 ofw_gpiobus_attach(device_t dev)
504 err = gpiobus_init_softc(dev);
507 bus_generic_probe(dev);
508 bus_enumerate_hinted_children(dev);
510 * Attach the children represented in the device tree.
512 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
513 child = OF_peer(child)) {
514 if (!OF_hasprop(child, "gpios"))
516 if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
520 return (bus_generic_attach(dev));
524 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
527 struct ofw_gpiobus_devinfo *devi;
529 child = device_add_child_ordered(dev, order, name, unit);
532 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
535 device_delete_child(dev, child);
540 * NULL all the OFW-related parts of the ivars for non-OFW
543 devi->opd_obdinfo.obd_node = -1;
544 devi->opd_obdinfo.obd_name = NULL;
545 devi->opd_obdinfo.obd_compat = NULL;
546 devi->opd_obdinfo.obd_type = NULL;
547 devi->opd_obdinfo.obd_model = NULL;
549 device_set_ivars(child, devi);
554 static const struct ofw_bus_devinfo *
555 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
557 struct ofw_gpiobus_devinfo *dinfo;
559 dinfo = device_get_ivars(dev);
561 return (&dinfo->opd_obdinfo);
564 static device_method_t ofw_gpiobus_methods[] = {
565 /* Device interface */
566 DEVMETHOD(device_probe, ofw_gpiobus_probe),
567 DEVMETHOD(device_attach, ofw_gpiobus_attach),
570 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
571 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child),
573 /* ofw_bus interface */
574 DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo),
575 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
576 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
577 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
578 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
579 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
584 devclass_t ofwgpiobus_devclass;
586 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
587 sizeof(struct gpiobus_softc), gpiobus_driver);
588 EARLY_DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass,
590 MODULE_VERSION(ofw_gpiobus, 1);
591 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);