1/*	$OpenBSD: gpioleds.c,v 1.4 2022/04/06 18:59:28 naddy Exp $	*/
2/*
3 * Copyright (c) 2021 Klemens Nanni <kn@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/param.h>
19#include <sys/systm.h>
20#include <sys/device.h>
21#include <sys/gpio.h>
22#include <sys/malloc.h>
23
24#include <machine/bus.h>
25#include <machine/fdt.h>
26
27#include <dev/gpio/gpiovar.h>
28#include <dev/ofw/ofw_gpio.h>
29#include <dev/ofw/ofw_pinctrl.h>
30#include <dev/ofw/openfirm.h>
31#include <dev/ofw/fdt.h>
32
33struct gpioleds_softc {
34	struct device	 sc_dev;
35	int		 sc_node;
36};
37
38int	gpioleds_match(struct device *, void *, void *);
39void	gpioleds_attach(struct device *, struct device *, void *);
40
41const struct cfattach gpioleds_ca = {
42	sizeof (struct gpioleds_softc), gpioleds_match, gpioleds_attach
43};
44
45struct cfdriver gpioleds_cd = {
46	NULL, "gpioleds", DV_DULL
47};
48
49int
50gpioleds_match(struct device *parent, void *match, void *aux)
51{
52	const struct fdt_attach_args	*faa = aux;
53
54	return OF_is_compatible(faa->fa_node, "gpio-leds");
55}
56
57void
58gpioleds_attach(struct device *parent, struct device *self, void *aux)
59{
60	struct fdt_attach_args	*faa = aux;
61	uint32_t		*led_pin;
62	char			*function, *default_state;
63	char			*function_prop = "function";
64	int			 function_len, default_state_len, gpios_len;
65	int			 node, leds = 0;
66
67	pinctrl_byname(faa->fa_node, "default");
68
69	for (node = OF_child(faa->fa_node); node; node = OF_peer(node)) {
70		function_len = OF_getproplen(node, function_prop);
71		if (function_len <= 0) {
72			function_prop = "label";
73			function_len = OF_getproplen(node, function_prop);
74			if (function_len <= 0)
75				continue;
76		}
77		default_state_len = OF_getproplen(node, "default-state");
78		if (default_state_len <= 0)
79			continue;
80		gpios_len = OF_getproplen(node, "gpios");
81		if (gpios_len <= 0)
82			continue;
83
84		function = malloc(function_len, M_TEMP, M_WAITOK);
85		OF_getprop(node, function_prop, function, function_len);
86		default_state = malloc(default_state_len, M_TEMP, M_WAITOK);
87		OF_getprop(node, "default-state", default_state, default_state_len);
88		led_pin = malloc(gpios_len, M_TEMP, M_WAITOK);
89		OF_getpropintarray(node, "gpios", led_pin, gpios_len);
90		gpio_controller_config_pin(led_pin, GPIO_CONFIG_OUTPUT);
91		if (strcmp(default_state, "on") == 0)
92			gpio_controller_set_pin(led_pin, 1);
93		else if (strcmp(default_state, "off") == 0)
94			gpio_controller_set_pin(led_pin, 0);
95
96		printf("%s \"%s\"", leds++ ? "," : ":", function);
97		free(function, M_TEMP, function_len);
98		free(default_state, M_TEMP, default_state_len);
99		free(led_pin, M_TEMP, gpios_len);
100	}
101
102	if (leds == 0)
103		printf(": no LEDs");
104	printf("\n");
105}
106