xref: /freebsd/sys/dev/gpio/ofw_gpiobus.c (revision 7d536dc855c85c15bf45f033d108a61b1f3cecc3)
1 /*-
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
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 
39 #include <dev/gpio/gpiobusvar.h>
40 #include <dev/ofw/ofw_bus.h>
41 
42 #include "gpiobus_if.h"
43 
44 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
45 	device_t, phandle_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 **);
49 
50 /*
51  * Utility functions for easier handling of OFW GPIO pins.
52  *
53  * !!! BEWARE !!!
54  * GPIOBUS uses children's IVARs, so we cannot use this interface for cross
55  * tree consumers.
56  *
57  */
58 static int
59 gpio_pin_get_by_ofw_impl(device_t consumer_dev, char *prop_name, int idx,
60     gpio_pin_t *out_pin)
61 {
62 	phandle_t cnode, xref;
63 	pcell_t *cells;
64 	device_t busdev;
65 	struct gpiobus_pin pin;
66 	int ncells, rv;
67 
68 	cnode = ofw_bus_get_node(consumer_dev);
69 	if (cnode <= 0) {
70 		device_printf(consumer_dev,
71 		    "%s called on not ofw based device\n", __func__);
72 		return (ENXIO);
73 	}
74 
75 	rv = ofw_bus_parse_xref_list_alloc(cnode, prop_name, "#gpio-cells",
76 	    idx, &xref, &ncells, &cells);
77 	if (rv != 0)
78 		return (rv);
79 
80 	/* Translate provider to device. */
81 	pin.dev = OF_device_from_xref(xref);
82 	if (pin.dev == NULL) {
83 		free(cells, M_OFWPROP);
84 		return (ENODEV);
85 	}
86 
87 	/* Test if GPIO bus already exist. */
88 	busdev = GPIO_GET_BUS(pin.dev);
89 	if (busdev == NULL) {
90 		free(cells, M_OFWPROP);
91 		return (ENODEV);
92 	}
93 
94 	/* Map GPIO pin. */
95 	rv = gpio_map_gpios(pin.dev, cnode, OF_node_from_xref(xref), ncells,
96 	    cells, &pin.pin, &pin.flags);
97 	free(cells, M_OFWPROP);
98 	if (rv != 0) {
99 		device_printf(consumer_dev, "Cannot map the gpio property.\n");
100 		return (ENXIO);
101 	}
102 
103 	/* Reserve GPIO pin. */
104 	rv = gpiobus_map_pin(busdev, pin.pin);
105 	if (rv != 0) {
106 		device_printf(consumer_dev, "Cannot reserve gpio pin.\n");
107 		return (EBUSY);
108 	}
109 
110 	*out_pin = malloc(sizeof(struct gpiobus_pin), M_DEVBUF,
111 	    M_WAITOK | M_ZERO);
112 	**out_pin = pin;
113 	return (0);
114 }
115 
116 int
117 gpio_pin_get_by_ofw_idx(device_t consumer_dev, int idx, gpio_pin_t *pin)
118 {
119 
120 	return (gpio_pin_get_by_ofw_impl(consumer_dev, "gpios", idx, pin));
121 }
122 
123 int
124 gpio_pin_get_by_ofw_property(device_t consumer_dev, char *name, gpio_pin_t *pin)
125 {
126 
127 	return (gpio_pin_get_by_ofw_impl(consumer_dev, name, 0, pin));
128 }
129 
130 int
131 gpio_pin_get_by_ofw_name(device_t consumer_dev, char *name, gpio_pin_t *pin)
132 {
133 	int rv, idx;
134 	phandle_t cnode;
135 
136 	cnode = ofw_bus_get_node(consumer_dev);
137 	if (cnode <= 0) {
138 		device_printf(consumer_dev,
139 		    "%s called on not ofw based device\n",  __func__);
140 		return (ENXIO);
141 	}
142 	rv = ofw_bus_find_string_index(cnode, "gpio-names", name, &idx);
143 	if (rv != 0)
144 		return (rv);
145 	return (gpio_pin_get_by_ofw_idx(consumer_dev, idx, pin));
146 }
147 
148 void
149 gpio_pin_release(gpio_pin_t gpio)
150 {
151 
152 	if (gpio == NULL)
153 		return;
154 
155 	/* XXXX Unreserve pin. */
156 	free(gpio, M_DEVBUF);
157 }
158 
159 int
160 gpio_pin_is_active(gpio_pin_t pin, bool *active)
161 {
162 	int rv;
163 	uint32_t tmp;
164 
165 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
166 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
167 	rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp);
168 	if (rv  != 0) {
169 		return (rv);
170 	}
171 
172 	*active = tmp != 0;
173 	if (pin->flags & GPIO_ACTIVE_LOW)
174 		*active = !(*active);
175 	return (0);
176 }
177 
178 int
179 gpio_pin_set_active(gpio_pin_t pin, bool active)
180 {
181 	int rv;
182 	uint32_t tmp;
183 
184 	if (pin->flags & GPIO_ACTIVE_LOW)
185 		tmp = active ? 0 : 1;
186 	else
187 		tmp = active ? 1 : 0;
188 
189 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
190 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
191 	rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp);
192 	return (rv);
193 }
194 
195 int
196 gpio_pin_setflags(gpio_pin_t pin, uint32_t flags)
197 {
198 	int rv;
199 
200 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
201 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
202 
203 	rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
204 	return (rv);
205 }
206 
207 /*
208  * OFW_GPIOBUS driver.
209  */
210 device_t
211 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
212 {
213 	device_t childdev;
214 	int i;
215 	struct gpiobus_ivar *devi;
216 	struct ofw_gpiobus_devinfo *dinfo;
217 
218 	/*
219 	 * Check to see if we already have a child for @p child, and if so
220 	 * return it.
221 	 */
222 	childdev = ofw_bus_find_child_device_by_phandle(bus, child);
223 	if (childdev != NULL)
224 		return (childdev);
225 
226 	/*
227 	 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
228 	 */
229 	childdev = device_add_child(bus, drvname, -1);
230 	if (childdev == NULL)
231 		return (NULL);
232 	dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
233 	if (dinfo == NULL) {
234 		device_delete_child(bus, childdev);
235 		return (NULL);
236 	}
237 	if (device_probe_and_attach(childdev) != 0) {
238 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
239 		device_delete_child(bus, childdev);
240 		return (NULL);
241 	}
242 	/* Use the child name as pin name. */
243 	devi = &dinfo->opd_dinfo;
244 	for (i = 0; i < devi->npins; i++)
245 		GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
246 		    device_get_nameunit(childdev));
247 
248 	return (childdev);
249 }
250 
251 int
252 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
253 	struct gpiobus_pin **pins)
254 {
255 
256 	return (ofw_gpiobus_parse_gpios_impl(consumer,
257 	    ofw_bus_get_node(consumer), pname, NULL, pins));
258 }
259 
260 void
261 ofw_gpiobus_register_provider(device_t provider)
262 {
263 	phandle_t node;
264 
265 	node = ofw_bus_get_node(provider);
266 	OF_device_register_xref(OF_xref_from_node(node), provider);
267 }
268 
269 void
270 ofw_gpiobus_unregister_provider(device_t provider)
271 {
272 	phandle_t node;
273 
274 	node = ofw_bus_get_node(provider);
275 	OF_device_register_xref(OF_xref_from_node(node), NULL);
276 }
277 
278 static struct ofw_gpiobus_devinfo *
279 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
280 {
281 	int i, npins;
282 	struct gpiobus_ivar *devi;
283 	struct gpiobus_pin *pins;
284 	struct gpiobus_softc *sc;
285 	struct ofw_gpiobus_devinfo *dinfo;
286 
287 	sc = device_get_softc(bus);
288 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
289 	if (dinfo == NULL)
290 		return (NULL);
291 	if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
292 		free(dinfo, M_DEVBUF);
293 		return (NULL);
294 	}
295 	/* Parse the gpios property for the child. */
296 	npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
297 	if (npins <= 0) {
298 		ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
299 		free(dinfo, M_DEVBUF);
300 		return (NULL);
301 	}
302 	/* Initialize the irq resource list. */
303 	resource_list_init(&dinfo->opd_dinfo.rl);
304 	/* Allocate the child ivars and copy the parsed pin data. */
305 	devi = &dinfo->opd_dinfo;
306 	devi->npins = (uint32_t)npins;
307 	if (gpiobus_alloc_ivars(devi) != 0) {
308 		free(pins, M_DEVBUF);
309 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
310 		return (NULL);
311 	}
312 	for (i = 0; i < devi->npins; i++) {
313 		devi->flags[i] = pins[i].flags;
314 		devi->pins[i] = pins[i].pin;
315 	}
316 	free(pins, M_DEVBUF);
317 	/* Parse the interrupt resources. */
318 	if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) {
319 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
320 		return (NULL);
321 	}
322 	device_set_ivars(child, dinfo);
323 
324 	return (dinfo);
325 }
326 
327 static void
328 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
329 {
330 	int i;
331 	struct gpiobus_ivar *devi;
332 	struct gpiobus_softc *sc;
333 
334 	sc = device_get_softc(bus);
335 	devi = &dinfo->opd_dinfo;
336 	for (i = 0; i < devi->npins; i++) {
337 		if (devi->pins[i] > sc->sc_npins)
338 			continue;
339 		sc->sc_pins[devi->pins[i]].mapped = 0;
340 	}
341 	gpiobus_free_ivars(devi);
342 	resource_list_free(&dinfo->opd_dinfo.rl);
343 	ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
344 	free(dinfo, M_DEVBUF);
345 }
346 
347 static int
348 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
349 	struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
350 {
351 	int gpiocells, i, j, ncells, npins;
352 	pcell_t *gpios;
353 	phandle_t gpio;
354 
355 	ncells = OF_getencprop_alloc(cnode, pname, sizeof(*gpios),
356             (void **)&gpios);
357 	if (ncells == -1) {
358 		device_printf(consumer,
359 		    "Warning: No %s specified in fdt data; "
360 		    "device may not function.\n", pname);
361 		return (-1);
362 	}
363 	/*
364 	 * The gpio-specifier is controller independent, the first pcell has
365 	 * the reference to the GPIO controller phandler.
366 	 * Count the number of encoded gpio-specifiers on the first pass.
367 	 */
368 	i = 0;
369 	npins = 0;
370 	while (i < ncells) {
371 		/* Allow NULL specifiers. */
372 		if (gpios[i] == 0) {
373 			npins++;
374 			i++;
375 			continue;
376 		}
377 		gpio = OF_node_from_xref(gpios[i]);
378 		/* If we have bussc, ignore devices from other gpios. */
379 		if (bussc != NULL)
380 			if (ofw_bus_get_node(bussc->sc_dev) != gpio)
381 				return (0);
382 		/*
383 		 * Check for gpio-controller property and read the #gpio-cells
384 		 * for this GPIO controller.
385 		 */
386 		if (!OF_hasprop(gpio, "gpio-controller") ||
387 		    OF_getencprop(gpio, "#gpio-cells", &gpiocells,
388 		    sizeof(gpiocells)) < 0) {
389 			device_printf(consumer,
390 			    "gpio reference is not a gpio-controller.\n");
391 			free(gpios, M_OFWPROP);
392 			return (-1);
393 		}
394 		if (ncells - i < gpiocells + 1) {
395 			device_printf(consumer,
396 			    "%s cells doesn't match #gpio-cells.\n", pname);
397 			return (-1);
398 		}
399 		npins++;
400 		i += gpiocells + 1;
401 	}
402 	if (npins == 0 || pins == NULL) {
403 		if (npins == 0)
404 			device_printf(consumer, "no pin specified in %s.\n",
405 			    pname);
406 		free(gpios, M_OFWPROP);
407 		return (npins);
408 	}
409 	*pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
410 	    M_NOWAIT | M_ZERO);
411 	if (*pins == NULL) {
412 		free(gpios, M_OFWPROP);
413 		return (-1);
414 	}
415 	/* Decode the gpio specifier on the second pass. */
416 	i = 0;
417 	j = 0;
418 	while (i < ncells) {
419 		/* Allow NULL specifiers. */
420 		if (gpios[i] == 0) {
421 			j++;
422 			i++;
423 			continue;
424 		}
425 		gpio = OF_node_from_xref(gpios[i]);
426 		/* Read gpio-cells property for this GPIO controller. */
427 		if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
428 		    sizeof(gpiocells)) < 0) {
429 			device_printf(consumer,
430 			    "gpio does not have the #gpio-cells property.\n");
431 			goto fail;
432 		}
433 		/* Return the device reference for the GPIO controller. */
434 		(*pins)[j].dev = OF_device_from_xref(gpios[i]);
435 		if ((*pins)[j].dev == NULL) {
436 			device_printf(consumer,
437 			    "no device registered for the gpio controller.\n");
438 			goto fail;
439 		}
440 		/*
441 		 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
442 		 * retrieve it.  The GPIO_GET_BUS() method is only valid after
443 		 * the child is probed and attached.
444 		 */
445 		if (bussc == NULL) {
446 			if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
447 				device_printf(consumer,
448 				    "no gpiobus reference for %s.\n",
449 				    device_get_nameunit((*pins)[j].dev));
450 				goto fail;
451 			}
452 			bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
453 		}
454 		/* Get the GPIO pin number and flags. */
455 		if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
456 		    &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
457 			device_printf(consumer,
458 			    "cannot map the gpios specifier.\n");
459 			goto fail;
460 		}
461 		/* Reserve the GPIO pin. */
462 		if (gpiobus_map_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
463 			goto fail;
464 		j++;
465 		i += gpiocells + 1;
466 	}
467 	free(gpios, M_OFWPROP);
468 
469 	return (npins);
470 
471 fail:
472 	free(gpios, M_OFWPROP);
473 	free(*pins, M_DEVBUF);
474 	return (-1);
475 }
476 
477 static int
478 ofw_gpiobus_probe(device_t dev)
479 {
480 
481 	if (ofw_bus_get_node(dev) == -1)
482 		return (ENXIO);
483 	device_set_desc(dev, "OFW GPIO bus");
484 
485 	return (0);
486 }
487 
488 static int
489 ofw_gpiobus_attach(device_t dev)
490 {
491 	int err;
492 	phandle_t child;
493 
494 	err = gpiobus_init_softc(dev);
495 	if (err != 0)
496 		return (err);
497 	bus_generic_probe(dev);
498 	bus_enumerate_hinted_children(dev);
499 	/*
500 	 * Attach the children represented in the device tree.
501 	 */
502 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
503 	    child = OF_peer(child)) {
504 		if (!OF_hasprop(child, "gpios"))
505 			continue;
506 		if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
507 			continue;
508 	}
509 
510 	return (bus_generic_attach(dev));
511 }
512 
513 static device_t
514 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
515 {
516 	device_t child;
517 	struct ofw_gpiobus_devinfo *devi;
518 
519 	child = device_add_child_ordered(dev, order, name, unit);
520 	if (child == NULL)
521 		return (child);
522 	devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
523 	    M_NOWAIT | M_ZERO);
524 	if (devi == NULL) {
525 		device_delete_child(dev, child);
526 		return (0);
527 	}
528 
529 	/*
530 	 * NULL all the OFW-related parts of the ivars for non-OFW
531 	 * children.
532 	 */
533 	devi->opd_obdinfo.obd_node = -1;
534 	devi->opd_obdinfo.obd_name = NULL;
535 	devi->opd_obdinfo.obd_compat = NULL;
536 	devi->opd_obdinfo.obd_type = NULL;
537 	devi->opd_obdinfo.obd_model = NULL;
538 
539 	device_set_ivars(child, devi);
540 
541 	return (child);
542 }
543 
544 static const struct ofw_bus_devinfo *
545 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
546 {
547 	struct ofw_gpiobus_devinfo *dinfo;
548 
549 	dinfo = device_get_ivars(dev);
550 
551 	return (&dinfo->opd_obdinfo);
552 }
553 
554 static device_method_t ofw_gpiobus_methods[] = {
555 	/* Device interface */
556 	DEVMETHOD(device_probe,		ofw_gpiobus_probe),
557 	DEVMETHOD(device_attach,	ofw_gpiobus_attach),
558 
559 	/* Bus interface */
560 	DEVMETHOD(bus_child_pnpinfo_str,	ofw_bus_gen_child_pnpinfo_str),
561 	DEVMETHOD(bus_add_child,	ofw_gpiobus_add_child),
562 
563 	/* ofw_bus interface */
564 	DEVMETHOD(ofw_bus_get_devinfo,	ofw_gpiobus_get_devinfo),
565 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
566 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
567 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
568 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
569 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
570 
571 	DEVMETHOD_END
572 };
573 
574 static devclass_t ofwgpiobus_devclass;
575 
576 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
577     sizeof(struct gpiobus_softc), gpiobus_driver);
578 DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0);
579 MODULE_VERSION(ofw_gpiobus, 1);
580 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);
581