From b0ed8e099b8fc14d4c3920f487262215b5165f50 Mon Sep 17 00:00:00 2001 From: tobhe Date: Tue, 8 Nov 2022 19:06:57 +0000 Subject: [PATCH] Add gpiobl(4), a driver for gpio controlled display backlights. This will allow us to turn off the screen on Apple Silicon laptops until we have a proper display controller driver. ok kettenis@ patrick@ --- sys/dev/fdt/files.fdt | 6 +- sys/dev/fdt/gpiobl.c | 127 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 sys/dev/fdt/gpiobl.c diff --git a/sys/dev/fdt/files.fdt b/sys/dev/fdt/files.fdt index 2855c761ece..f01a350b87a 100644 --- a/sys/dev/fdt/files.fdt +++ b/sys/dev/fdt/files.fdt @@ -1,4 +1,4 @@ -# $OpenBSD: files.fdt,v 1.167 2022/11/06 15:36:13 patrick Exp $ +# $OpenBSD: files.fdt,v 1.168 2022/11/08 19:06:57 tobhe Exp $ # # Config file and device description for machine-independent FDT code. # Included by ports that need it. @@ -601,6 +601,10 @@ device dapmic attach dapmic at i2c file dev/fdt/dapmic.c dapmic +device gpiobl +attach gpiobl at fdt +file dev/fdt/gpiobl.c gpiobl + device gpiocharger attach gpiocharger at fdt file dev/fdt/gpiocharger.c gpiocharger diff --git a/sys/dev/fdt/gpiobl.c b/sys/dev/fdt/gpiobl.c new file mode 100644 index 00000000000..9c218694ccb --- /dev/null +++ b/sys/dev/fdt/gpiobl.c @@ -0,0 +1,127 @@ +/* $OpenBSD: gpiobl.c,v 1.1 2022/11/08 19:06:57 tobhe Exp $ */ +/* + * Copyright (c) 2022 Tobias Heider + * + * 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 + +#include +#include + +struct gpiobl_softc { + struct device sc_dev; + int sc_on; + uint32_t sc_gpio[3]; + struct task sc_task; +}; + +struct gpiobl_softc *sc_gpiobl; + +int gpiobl_match(struct device *, void *, void *); +void gpiobl_attach(struct device *, struct device *, void *); + +const struct cfattach gpiobl_ca = { + sizeof(struct gpiobl_softc), gpiobl_match, gpiobl_attach +}; + +struct cfdriver gpiobl_cd = { + NULL, "gpiobl", DV_DULL +}; + +void gpiobl_task(void *); +int gpiobl_get_param(struct wsdisplay_param *); +int gpiobl_set_param(struct wsdisplay_param *); + +int +gpiobl_match(struct device *parent, void *match, void *aux) +{ + struct fdt_attach_args *faa = aux; + + return OF_is_compatible(faa->fa_node, "gpio-backlight"); +} + + +void +gpiobl_attach(struct device *parent, struct device *self, void *aux) +{ + struct gpiobl_softc *sc = (struct gpiobl_softc *)self; + struct fdt_attach_args *faa = aux; + size_t len; + + len = OF_getproplen(faa->fa_node, "gpios"); + if (len <= 0) + return; + OF_getpropintarray(faa->fa_node, "gpios", sc->sc_gpio, len); + gpio_controller_config_pin(sc->sc_gpio, GPIO_CONFIG_OUTPUT); + + sc->sc_on = OF_getpropbool(faa->fa_node, "default-on"); + sc_gpiobl = sc; + + task_set(&sc->sc_task, gpiobl_task, sc); + ws_get_param = gpiobl_get_param; + ws_set_param = gpiobl_set_param; + printf("\n"); +} + +void +gpiobl_task(void *args) +{ + struct gpiobl_softc *sc = args; + gpio_controller_set_pin(&sc->sc_gpio[0], sc->sc_on); +} + +int +gpiobl_get_param(struct wsdisplay_param *dp) +{ + struct gpiobl_softc *sc = (struct gpiobl_softc *)sc_gpiobl; + + switch (dp->param) { + case WSDISPLAYIO_PARAM_BRIGHTNESS: + dp->min = 0; + dp->max = 1; + dp->curval = sc->sc_on; + return 0; + default: + return -1; + } +} + +int +gpiobl_set_param(struct wsdisplay_param *dp) +{ + struct gpiobl_softc *sc = (struct gpiobl_softc *)sc_gpiobl; + + switch (dp->param) { + case WSDISPLAYIO_PARAM_BRIGHTNESS: + if (dp->curval == sc->sc_on) + return 0; + sc->sc_on = !sc->sc_on; + task_add(systq, &sc->sc_task); + return 0; + default: + return -1; + } +} -- 2.20.1