From 4ca5c7ed9f37730d18c4041db1de9365b1dd52d0 Mon Sep 17 00:00:00 2001 From: kettenis Date: Wed, 23 Nov 2022 23:43:08 +0000 Subject: [PATCH] Add pwmleds(4), a driver for PWM controlled LEDs. For now this only implements keyboard backlight support. ok kn@ --- sys/dev/fdt/files.fdt | 6 +- sys/dev/fdt/pwmleds.c | 161 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 sys/dev/fdt/pwmleds.c diff --git a/sys/dev/fdt/files.fdt b/sys/dev/fdt/files.fdt index 7e0781ca413..93c8f7de922 100644 --- a/sys/dev/fdt/files.fdt +++ b/sys/dev/fdt/files.fdt @@ -1,4 +1,4 @@ -# $OpenBSD: files.fdt,v 1.171 2022/11/10 12:12:53 patrick Exp $ +# $OpenBSD: files.fdt,v 1.172 2022/11/23 23:43:08 kettenis Exp $ # # Config file and device description for machine-independent FDT code. # Included by ports that need it. @@ -231,6 +231,10 @@ device pwmfan attach pwmfan at fdt file dev/fdt/pwmfan.c pwmfan +device pwmleds +attach pwmleds at fdt +file dev/fdt/pwmleds.c pwmleds + device pwmreg attach pwmreg at fdt file dev/fdt/pwmreg.c pwmreg diff --git a/sys/dev/fdt/pwmleds.c b/sys/dev/fdt/pwmleds.c new file mode 100644 index 00000000000..24d2fe054c6 --- /dev/null +++ b/sys/dev/fdt/pwmleds.c @@ -0,0 +1,161 @@ +/* $OpenBSD: pwmleds.c,v 1.1 2022/11/23 23:43:08 kettenis Exp $ */ +/* + * Copyright (c) 2022 Mark Kettenis + * + * 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 +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include + +extern int (*wskbd_get_backlight)(struct wskbd_backlight *); +extern int (*wskbd_set_backlight)(struct wskbd_backlight *); + +struct pwmleds_softc { + struct device sc_dev; + + /* Keyboard backlight. */ + uint32_t *sc_pwm; + int sc_pwm_len; + uint32_t sc_max_brightness; +}; + +int pwmleds_match(struct device *, void *, void *); +void pwmleds_attach(struct device *, struct device *, void *); + +const struct cfattach pwmleds_ca = { + sizeof (struct pwmleds_softc), pwmleds_match, pwmleds_attach +}; + +struct cfdriver pwmleds_cd = { + NULL, "pwmleds", DV_DULL +}; + +int pwmleds_get_kbd_backlight(struct wskbd_backlight *); +int pwmleds_set_kbd_backlight(struct wskbd_backlight *); + +int +pwmleds_match(struct device *parent, void *match, void *aux) +{ + const struct fdt_attach_args *faa = aux; + + return OF_is_compatible(faa->fa_node, "pwm-leds"); +} + +void +pwmleds_attach(struct device *parent, struct device *self, void *aux) +{ + struct pwmleds_softc *sc = (struct pwmleds_softc *)self; + struct fdt_attach_args *faa = aux; + char *function; + int len, node; + + printf("\n"); + + for (node = OF_child(faa->fa_node); node; node = OF_peer(node)) { + len = OF_getproplen(node, "function"); + if (len <= 0) + continue; + + function = malloc(len, M_TEMP, M_WAITOK); + OF_getprop(node, "function", function, len); + if (strcmp(function, "kbd_backlight") != 0) { + free(function, M_TEMP, len); + continue; + } + free(function, M_TEMP, len); + + len = OF_getproplen(node, "pwms"); + if (len <= 0) + continue; + + sc->sc_pwm = malloc(len, M_DEVBUF, M_WAITOK); + OF_getpropintarray(node, "pwms", sc->sc_pwm, len); + sc->sc_pwm_len = len; + + sc->sc_max_brightness = + OF_getpropint(node, "max-brightness", 0); + wskbd_get_backlight = pwmleds_get_kbd_backlight; + wskbd_set_backlight = pwmleds_set_kbd_backlight; + } +} + +struct pwmleds_softc * +pwmleds_kbd_backlight(void) +{ + struct pwmleds_softc *sc; + int i; + + for (i = 0; i < pwmleds_cd.cd_ndevs; i++) { + sc = pwmleds_cd.cd_devs[i]; + if (sc == NULL) + continue; + if (sc->sc_max_brightness > 0) + return sc; + } + + return NULL; +} + +int +pwmleds_get_kbd_backlight(struct wskbd_backlight *kbl) +{ + struct pwmleds_softc *sc; + struct pwm_state ps; + int error; + + sc = pwmleds_kbd_backlight(); + if (sc == NULL) + return ENOTTY; + + error = pwm_get_state(sc->sc_pwm, &ps); + if (error) + return error; + + kbl->min = 0; + kbl->max = sc->sc_max_brightness; + kbl->curval = (ps.ps_enabled) ? + ((uint64_t)ps.ps_pulse_width * kbl->max) / ps.ps_period : 0; + return 0; +} + +int +pwmleds_set_kbd_backlight(struct wskbd_backlight *kbl) +{ + struct pwmleds_softc *sc; + struct pwm_state ps; + + sc = pwmleds_kbd_backlight(); + if (sc == NULL) + return ENOTTY; + + if (kbl->curval < 0 || kbl->curval > sc->sc_max_brightness) + return EINVAL; + + pwm_init_state(sc->sc_pwm, &ps); + ps.ps_pulse_width = + ((uint64_t)kbl->curval * ps.ps_period) / sc->sc_max_brightness; + ps.ps_enabled = (ps.ps_pulse_width > 0); + return pwm_set_state(sc->sc_pwm, &ps); +} -- 2.20.1