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