1 /* $OpenBSD: gpioleds.c,v 1.1 2021/09/25 10:43:24 kn 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 33 struct gpioleds_softc { 34 struct device sc_dev; 35 int sc_node; 36 }; 37 38 int gpioleds_match(struct device *, void *, void *); 39 void gpioleds_attach(struct device *, struct device *, void *); 40 41 struct cfattach gpioleds_ca = { 42 sizeof (struct gpioleds_softc), gpioleds_match, gpioleds_attach 43 }; 44 45 struct cfdriver gpioleds_cd = { 46 NULL, "gpioleds", DV_DULL 47 }; 48 49 int 50 gpioleds_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 57 void 58 gpioleds_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 int function_len, default_state_len, gpios_len; 64 int node, leds = 0; 65 66 pinctrl_byname(faa->fa_node, "default"); 67 68 for (node = OF_child(faa->fa_node); node; node = OF_peer(node)) { 69 function_len = OF_getproplen(node, "function"); 70 if (function_len <= 0) 71 continue; 72 default_state_len = OF_getproplen(node, "default-state"); 73 if (default_state_len <= 0) 74 continue; 75 gpios_len = OF_getproplen(node, "gpios"); 76 if (gpios_len <= 0) 77 continue; 78 79 function = malloc(function_len, M_TEMP, M_WAITOK); 80 OF_getprop(node, "function", function, function_len); 81 default_state = malloc(default_state_len, M_TEMP, M_WAITOK); 82 OF_getprop(node, "default-state", default_state, default_state_len); 83 led_pin = malloc(gpios_len, M_TEMP, M_WAITOK); 84 OF_getpropintarray(node, "gpios", led_pin, gpios_len); 85 gpio_controller_config_pin(led_pin, GPIO_CONFIG_OUTPUT); 86 if (strcmp(default_state, "on") == 0) 87 gpio_controller_set_pin(led_pin, 1); 88 else if (strcmp(default_state, "off") == 0) 89 gpio_controller_set_pin(led_pin, 0); 90 91 printf("%s \"%s\"", leds++ ? "," : ":", function); 92 free(function, M_TEMP, function_len); 93 free(default_state, M_TEMP, default_state_len); 94 free(led_pin, M_TEMP, gpios_len); 95 } 96 97 if (leds == 0) { 98 printf(": no LEDs\n"); 99 return; 100 } 101 printf("\n"); 102 } 103