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 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
44 static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *);
45 static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *,
46 struct gpiobus_softc *, struct gpiobus_pin **);
49 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
52 struct ofw_gpiobus_devinfo *dinfo;
55 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
57 childdev = device_add_child(bus, drvname, -1);
60 dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
62 device_delete_child(bus, childdev);
65 if (device_probe_and_attach(childdev) != 0) {
66 ofw_gpiobus_destroy_devinfo(bus, dinfo);
67 device_delete_child(bus, childdev);
75 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
76 struct gpiobus_pin **pins)
79 return (ofw_gpiobus_parse_gpios_impl(consumer,
80 ofw_bus_get_node(consumer), pname, NULL, pins));
84 ofw_gpiobus_register_provider(device_t provider)
88 node = ofw_bus_get_node(provider);
89 OF_device_register_xref(OF_xref_from_node(node), provider);
93 ofw_gpiobus_unregister_provider(device_t provider)
97 node = ofw_bus_get_node(provider);
98 OF_device_register_xref(OF_xref_from_node(node), NULL);
101 static struct ofw_gpiobus_devinfo *
102 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
105 struct gpiobus_ivar *devi;
106 struct gpiobus_pin *pins;
107 struct gpiobus_softc *sc;
108 struct ofw_gpiobus_devinfo *dinfo;
110 sc = device_get_softc(bus);
111 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
114 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
115 free(dinfo, M_DEVBUF);
118 /* Parse the gpios property for the child. */
119 npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
121 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
122 free(dinfo, M_DEVBUF);
125 /* Initialize the irq resource list. */
126 resource_list_init(&dinfo->opd_dinfo.rl);
127 /* Allocate the child ivars and copy the parsed pin data. */
128 devi = &dinfo->opd_dinfo;
129 devi->npins = (uint32_t)npins;
130 if (gpiobus_alloc_ivars(devi) != 0) {
131 free(pins, M_DEVBUF);
132 ofw_gpiobus_destroy_devinfo(bus, dinfo);
135 for (i = 0; i < devi->npins; i++) {
136 devi->flags[i] = pins[i].flags;
137 devi->pins[i] = pins[i].pin;
139 free(pins, M_DEVBUF);
140 /* Parse the interrupt resources. */
141 if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl) != 0) {
142 ofw_gpiobus_destroy_devinfo(bus, dinfo);
145 device_set_ivars(child, dinfo);
151 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
154 struct gpiobus_ivar *devi;
155 struct gpiobus_softc *sc;
157 sc = device_get_softc(bus);
158 devi = &dinfo->opd_dinfo;
159 for (i = 0; i < devi->npins; i++) {
160 if (devi->pins[i] > sc->sc_npins)
162 sc->sc_pins_mapped[devi->pins[i]] = 0;
164 gpiobus_free_ivars(devi);
165 resource_list_free(&dinfo->opd_dinfo.rl);
166 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
167 free(dinfo, M_DEVBUF);
171 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
172 struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
174 int gpiocells, i, j, ncells, npins;
178 ncells = OF_getencprop_alloc(cnode, pname, sizeof(*gpios),
181 device_printf(consumer,
182 "Warning: No %s specified in fdt data; "
183 "device may not function.\n", pname);
187 * The gpio-specifier is controller independent, the first pcell has
188 * the reference to the GPIO controller phandler.
189 * Count the number of encoded gpio-specifiers on the first pass.
194 /* Allow NULL specifiers. */
200 gpio = OF_node_from_xref(gpios[i]);
201 /* If we have bussc, ignore devices from other gpios. */
203 if (ofw_bus_get_node(bussc->sc_dev) != gpio)
206 * Check for gpio-controller property and read the #gpio-cells
207 * for this GPIO controller.
209 if (!OF_hasprop(gpio, "gpio-controller") ||
210 OF_getencprop(gpio, "#gpio-cells", &gpiocells,
211 sizeof(gpiocells)) < 0) {
212 device_printf(consumer,
213 "gpio reference is not a gpio-controller.\n");
214 free(gpios, M_OFWPROP);
217 if (ncells - i < gpiocells + 1) {
218 device_printf(consumer,
219 "%s cells doesn't match #gpio-cells.\n", pname);
225 if (npins == 0 || pins == NULL) {
227 device_printf(consumer, "no pin specified in %s.\n",
229 free(gpios, M_OFWPROP);
232 *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
235 free(gpios, M_OFWPROP);
238 /* Decode the gpio specifier on the second pass. */
242 /* Allow NULL specifiers. */
248 gpio = OF_node_from_xref(gpios[i]);
249 /* Read gpio-cells property for this GPIO controller. */
250 if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
251 sizeof(gpiocells)) < 0) {
252 device_printf(consumer,
253 "gpio does not have the #gpio-cells property.\n");
256 /* Return the device reference for the GPIO controller. */
257 (*pins)[j].dev = OF_device_from_xref(gpios[i]);
258 if ((*pins)[j].dev == NULL) {
259 device_printf(consumer,
260 "no device registered for the gpio controller.\n");
264 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
265 * retrieve it. The GPIO_GET_BUS() method is only valid after
266 * the child is probed and attached.
269 if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
270 device_printf(consumer,
271 "no gpiobus reference for %s.\n",
272 device_get_nameunit((*pins)[j].dev));
275 bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
277 /* Get the GPIO pin number and flags. */
278 if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
279 &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
280 device_printf(consumer,
281 "cannot map the gpios specifier.\n");
284 /* Reserve the GPIO pin. */
285 if (gpiobus_map_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
290 free(gpios, M_OFWPROP);
295 free(gpios, M_OFWPROP);
296 free(*pins, M_DEVBUF);
301 ofw_gpiobus_probe(device_t dev)
304 if (ofw_bus_get_node(dev) == -1)
306 device_set_desc(dev, "OFW GPIO bus");
312 ofw_gpiobus_attach(device_t dev)
317 err = gpiobus_init_softc(dev);
320 bus_generic_probe(dev);
321 bus_enumerate_hinted_children(dev);
323 * Attach the children represented in the device tree.
325 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
326 child = OF_peer(child)) {
327 if (!OF_hasprop(child, "gpios"))
329 if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
333 return (bus_generic_attach(dev));
337 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
340 struct ofw_gpiobus_devinfo *devi;
342 child = device_add_child_ordered(dev, order, name, unit);
345 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
348 device_delete_child(dev, child);
353 * NULL all the OFW-related parts of the ivars for non-OFW
356 devi->opd_obdinfo.obd_node = -1;
357 devi->opd_obdinfo.obd_name = NULL;
358 devi->opd_obdinfo.obd_compat = NULL;
359 devi->opd_obdinfo.obd_type = NULL;
360 devi->opd_obdinfo.obd_model = NULL;
362 device_set_ivars(child, devi);
367 static const struct ofw_bus_devinfo *
368 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
370 struct ofw_gpiobus_devinfo *dinfo;
372 dinfo = device_get_ivars(dev);
374 return (&dinfo->opd_obdinfo);
377 static device_method_t ofw_gpiobus_methods[] = {
378 /* Device interface */
379 DEVMETHOD(device_probe, ofw_gpiobus_probe),
380 DEVMETHOD(device_attach, ofw_gpiobus_attach),
383 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
384 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child),
386 /* ofw_bus interface */
387 DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo),
388 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
389 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
390 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
391 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
392 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
397 static devclass_t ofwgpiobus_devclass;
399 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
400 sizeof(struct gpiobus_softc), gpiobus_driver);
401 DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0);
402 MODULE_VERSION(ofw_gpiobus, 1);
403 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);