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 #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 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
55 struct gpiobus_ivar *devi;
56 struct ofw_gpiobus_devinfo *dinfo;
59 * Check to see if we already have a child for @p child, and if so
62 childdev = ofw_bus_find_child_device_by_phandle(bus, child);
67 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
69 childdev = device_add_child(bus, drvname, -1);
72 dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
74 device_delete_child(bus, childdev);
77 if (device_probe_and_attach(childdev) != 0) {
78 ofw_gpiobus_destroy_devinfo(bus, dinfo);
79 device_delete_child(bus, childdev);
82 /* Use the child name as pin name. */
83 devi = &dinfo->opd_dinfo;
84 for (i = 0; i < devi->npins; i++)
85 GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
86 device_get_nameunit(childdev));
92 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
93 struct gpiobus_pin **pins)
96 return (ofw_gpiobus_parse_gpios_impl(consumer,
97 ofw_bus_get_node(consumer), pname, NULL, pins));
101 ofw_gpiobus_register_provider(device_t provider)
105 node = ofw_bus_get_node(provider);
106 OF_device_register_xref(OF_xref_from_node(node), provider);
110 ofw_gpiobus_unregister_provider(device_t provider)
114 node = ofw_bus_get_node(provider);
115 OF_device_register_xref(OF_xref_from_node(node), NULL);
118 static struct ofw_gpiobus_devinfo *
119 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
122 struct gpiobus_ivar *devi;
123 struct gpiobus_pin *pins;
124 struct gpiobus_softc *sc;
125 struct ofw_gpiobus_devinfo *dinfo;
127 sc = device_get_softc(bus);
128 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
131 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
132 free(dinfo, M_DEVBUF);
135 /* Parse the gpios property for the child. */
136 npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
138 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
139 free(dinfo, M_DEVBUF);
142 /* Initialize the irq resource list. */
143 resource_list_init(&dinfo->opd_dinfo.rl);
144 /* Allocate the child ivars and copy the parsed pin data. */
145 devi = &dinfo->opd_dinfo;
146 devi->npins = (uint32_t)npins;
147 if (gpiobus_alloc_ivars(devi) != 0) {
148 free(pins, M_DEVBUF);
149 ofw_gpiobus_destroy_devinfo(bus, dinfo);
152 for (i = 0; i < devi->npins; i++) {
153 devi->flags[i] = pins[i].flags;
154 devi->pins[i] = pins[i].pin;
156 free(pins, M_DEVBUF);
157 /* Parse the interrupt resources. */
158 if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) {
159 ofw_gpiobus_destroy_devinfo(bus, dinfo);
162 device_set_ivars(child, dinfo);
168 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
171 struct gpiobus_ivar *devi;
172 struct gpiobus_softc *sc;
174 sc = device_get_softc(bus);
175 devi = &dinfo->opd_dinfo;
176 for (i = 0; i < devi->npins; i++) {
177 if (devi->pins[i] > sc->sc_npins)
179 sc->sc_pins[devi->pins[i]].mapped = 0;
181 gpiobus_free_ivars(devi);
182 resource_list_free(&dinfo->opd_dinfo.rl);
183 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
184 free(dinfo, M_DEVBUF);
188 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
189 struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
191 int gpiocells, i, j, ncells, npins;
195 ncells = OF_getencprop_alloc(cnode, pname, sizeof(*gpios),
198 device_printf(consumer,
199 "Warning: No %s specified in fdt data; "
200 "device may not function.\n", pname);
204 * The gpio-specifier is controller independent, the first pcell has
205 * the reference to the GPIO controller phandler.
206 * Count the number of encoded gpio-specifiers on the first pass.
211 /* Allow NULL specifiers. */
217 gpio = OF_node_from_xref(gpios[i]);
218 /* If we have bussc, ignore devices from other gpios. */
220 if (ofw_bus_get_node(bussc->sc_dev) != gpio)
223 * Check for gpio-controller property and read the #gpio-cells
224 * for this GPIO controller.
226 if (!OF_hasprop(gpio, "gpio-controller") ||
227 OF_getencprop(gpio, "#gpio-cells", &gpiocells,
228 sizeof(gpiocells)) < 0) {
229 device_printf(consumer,
230 "gpio reference is not a gpio-controller.\n");
231 free(gpios, M_OFWPROP);
234 if (ncells - i < gpiocells + 1) {
235 device_printf(consumer,
236 "%s cells doesn't match #gpio-cells.\n", pname);
242 if (npins == 0 || pins == NULL) {
244 device_printf(consumer, "no pin specified in %s.\n",
246 free(gpios, M_OFWPROP);
249 *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
252 free(gpios, M_OFWPROP);
255 /* Decode the gpio specifier on the second pass. */
259 /* Allow NULL specifiers. */
265 gpio = OF_node_from_xref(gpios[i]);
266 /* Read gpio-cells property for this GPIO controller. */
267 if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
268 sizeof(gpiocells)) < 0) {
269 device_printf(consumer,
270 "gpio does not have the #gpio-cells property.\n");
273 /* Return the device reference for the GPIO controller. */
274 (*pins)[j].dev = OF_device_from_xref(gpios[i]);
275 if ((*pins)[j].dev == NULL) {
276 device_printf(consumer,
277 "no device registered for the gpio controller.\n");
281 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
282 * retrieve it. The GPIO_GET_BUS() method is only valid after
283 * the child is probed and attached.
286 if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
287 device_printf(consumer,
288 "no gpiobus reference for %s.\n",
289 device_get_nameunit((*pins)[j].dev));
292 bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
294 /* Get the GPIO pin number and flags. */
295 if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
296 &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
297 device_printf(consumer,
298 "cannot map the gpios specifier.\n");
301 /* Reserve the GPIO pin. */
302 if (gpiobus_map_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
307 free(gpios, M_OFWPROP);
312 free(gpios, M_OFWPROP);
313 free(*pins, M_DEVBUF);
318 ofw_gpiobus_probe(device_t dev)
321 if (ofw_bus_get_node(dev) == -1)
323 device_set_desc(dev, "OFW GPIO bus");
329 ofw_gpiobus_attach(device_t dev)
334 err = gpiobus_init_softc(dev);
337 bus_generic_probe(dev);
338 bus_enumerate_hinted_children(dev);
340 * Attach the children represented in the device tree.
342 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
343 child = OF_peer(child)) {
344 if (!OF_hasprop(child, "gpios"))
346 if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
350 return (bus_generic_attach(dev));
354 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
357 struct ofw_gpiobus_devinfo *devi;
359 child = device_add_child_ordered(dev, order, name, unit);
362 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
365 device_delete_child(dev, child);
370 * NULL all the OFW-related parts of the ivars for non-OFW
373 devi->opd_obdinfo.obd_node = -1;
374 devi->opd_obdinfo.obd_name = NULL;
375 devi->opd_obdinfo.obd_compat = NULL;
376 devi->opd_obdinfo.obd_type = NULL;
377 devi->opd_obdinfo.obd_model = NULL;
379 device_set_ivars(child, devi);
384 static const struct ofw_bus_devinfo *
385 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
387 struct ofw_gpiobus_devinfo *dinfo;
389 dinfo = device_get_ivars(dev);
391 return (&dinfo->opd_obdinfo);
394 static device_method_t ofw_gpiobus_methods[] = {
395 /* Device interface */
396 DEVMETHOD(device_probe, ofw_gpiobus_probe),
397 DEVMETHOD(device_attach, ofw_gpiobus_attach),
400 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
401 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child),
403 /* ofw_bus interface */
404 DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo),
405 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
406 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
407 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
408 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
409 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
414 static devclass_t ofwgpiobus_devclass;
416 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
417 sizeof(struct gpiobus_softc), gpiobus_driver);
418 DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0);
419 MODULE_VERSION(ofw_gpiobus, 1);
420 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);