summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/gpioleds.c
blob: 6a58344ac8b30d0256f605965238acd8c181975a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*	$OpenBSD: gpioleds.c,v 1.3 2021/11/07 16:43:12 kn Exp $	*/
/*
 * Copyright (c) 2021 Klemens Nanni <kn@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/gpio.h>
#include <sys/malloc.h>

#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/gpio/gpiovar.h>
#include <dev/ofw/ofw_gpio.h>
#include <dev/ofw/ofw_pinctrl.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>

struct gpioleds_softc {
	struct device	 sc_dev;
	int		 sc_node;
};

int	gpioleds_match(struct device *, void *, void *);
void	gpioleds_attach(struct device *, struct device *, void *);

struct cfattach gpioleds_ca = {
	sizeof (struct gpioleds_softc), gpioleds_match, gpioleds_attach
};

struct cfdriver gpioleds_cd = {
	NULL, "gpioleds", DV_DULL
};

int
gpioleds_match(struct device *parent, void *match, void *aux)
{
	const struct fdt_attach_args	*faa = aux;

	return OF_is_compatible(faa->fa_node, "gpio-leds");
}

void
gpioleds_attach(struct device *parent, struct device *self, void *aux)
{
	struct fdt_attach_args	*faa = aux;
	uint32_t		*led_pin;
	char			*function, *default_state;
	char			*function_prop = "function";
	int			 function_len, default_state_len, gpios_len;
	int			 node, leds = 0;

	pinctrl_byname(faa->fa_node, "default");

	for (node = OF_child(faa->fa_node); node; node = OF_peer(node)) {
		function_len = OF_getproplen(node, function_prop);
		if (function_len <= 0) {
			function_prop = "label";
			function_len = OF_getproplen(node, function_prop);
			if (function_len <= 0)
				continue;
		}
		default_state_len = OF_getproplen(node, "default-state");
		if (default_state_len <= 0)
			continue;
		gpios_len = OF_getproplen(node, "gpios");
		if (gpios_len <= 0)
			continue;

		function = malloc(function_len, M_TEMP, M_WAITOK);
		OF_getprop(node, function_prop, function, function_len);
		default_state = malloc(default_state_len, M_TEMP, M_WAITOK);
		OF_getprop(node, "default-state", default_state, default_state_len);
		led_pin = malloc(gpios_len, M_TEMP, M_WAITOK);
		OF_getpropintarray(node, "gpios", led_pin, gpios_len);
		gpio_controller_config_pin(led_pin, GPIO_CONFIG_OUTPUT);
		if (strcmp(default_state, "on") == 0)
			gpio_controller_set_pin(led_pin, 1);
		else if (strcmp(default_state, "off") == 0)
			gpio_controller_set_pin(led_pin, 0);

		printf("%s \"%s\"", leds++ ? "," : ":", function);
		free(function, M_TEMP, function_len);
		free(default_state, M_TEMP, default_state_len);
		free(led_pin, M_TEMP, gpios_len);
	}

	if (leds == 0)
		printf(": no LEDs");
	printf("\n");
}