On Thu, Sep 16, 2021 at 11:07:20AM +0200, Mark Kettenis wrote:
> > Date: Thu, 16 Sep 2021 06:14:39 +0000
> > From: Klemens Nanni <k...@openbsd.org>
> > 
> > On 5 September 2021 01:22:53 GMT+05:00, Klemens Nanni <k...@openbsd.org> 
> > wrote:
> > >Read a single GPIO pin indicating whether AC is plugged in or not.
> > >
> > >This gives me a sensor on my Pinebook Pro.
> > >cwfg(4) already provides battery information but not the charger bits.
> > >
> > >apm(4) integration can follow separately.
> > >
> > >Feedback? OK?
> > 
> > Ping.
> > The diff applies after the "new gpioleds driver" one.

New diff.  OK for after unlock modulo the gpioleds bits after being
committed separately (CVS diffs juggling is annoying)?

> > >+          gpios_len = OF_getproplen(node,
> > >+              gpios_property = "charger-status-gpios");
> 
> No, please don't hide assignment statements as function arguments.

Fixed, thanks.

Index: share/man/man4/gpiocharger.4
===================================================================
RCS file: share/man/man4/gpiocharger.4
diff -N share/man/man4/gpiocharger.4
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ share/man/man4/gpiocharger.4        24 Sep 2021 22:33:26 -0000
@@ -0,0 +1,51 @@
+.\"    $OpenBSD: $
+.\"
+.\" Copyright (c) 2021 Klemens Nanni <k...@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.
+.\"
+.Dd $Mdocdate: September 04 2021 $
+.Dt GPIOCHARGER 4
+.Os
+.Sh NAME
+.Nm gpiocharger
+.Nd GPIO battery charger
+.Sh SYNOPSIS
+.Cd "gpiocharger* at fdt?"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides support for battery chargers connected to GPIO pins.
+Currently, only power supply status events are supported.
+.Pp
+The power supply status (connected or disconnected) is set up as a sensor
+and can be monitored using
+.Xr sysctl 8
+or
+.Xr sensorsd 8 .
+.Sh SEE ALSO
+.Xr gpio 4 ,
+.Xr intro 4 ,
+.Xr sensorsd 8 ,
+.Xr sysctl 8
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Ox 7.1 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Klemens Nanni Aq Mt k...@openbsd.org .
Index: share/man/man4/Makefile
===================================================================
RCS file: /cvs/src/share/man/man4/Makefile,v
retrieving revision 1.806
diff -u -p -r1.806 Makefile
--- share/man/man4/Makefile     4 Sep 2021 12:11:45 -0000       1.806
+++ share/man/man4/Makefile     24 Sep 2021 22:27:59 -0000
@@ -35,8 +35,9 @@ MAN=  aac.4 abcrtc.4 abl.4 ac97.4 acphy.4
        eso.4 ess.4 et.4 etherip.4 etphy.4 ex.4 exphy.4 exrtc.4 \
        fanpwr.4 fd.4 fdc.4 fec.4 fido.4 fins.4 fintek.4 fms.4 fusbtc.4 \
        fuse.4 fxp.4 \
-       gdt.4 gentbi.4 gem.4 gfrtc.4 gif.4 glenv.4 glkgpio.4 gpio.4 gpiodcf.4 \
-       gpioiic.4 gpioow.4 graphaudio.4 gre.4 gscsio.4 \
+       gdt.4 gentbi.4 gem.4 gfrtc.4 gif.4 glenv.4 glkgpio.4 gpio.4 \
+       gpiocharger.4 gpiodcf.4 \
+       gpioiic.4 gpioleds.4 gpioow.4 graphaudio.4 gre.4 gscsio.4 \
        hds.4 hiclock.4 hidwusb.4 hifn.4 hil.4 hilid.4 hilkbd.4 hilms.4 \
        hireset.4 hitemp.4 hme.4 hotplug.4 hsq.4 \
        hvn.4 hvs.4 hyperv.4 \
Index: sys/dev/fdt/gpiocharger.c
===================================================================
RCS file: sys/dev/fdt/gpiocharger.c
diff -N sys/dev/fdt/gpiocharger.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ sys/dev/fdt/gpiocharger.c   24 Sep 2021 22:30:44 -0000
@@ -0,0 +1,117 @@
+/*     $OpenBSD: $     */
+/*
+ * Copyright (c) 2021 Klemens Nanni <k...@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>
+
+#include <sys/sensors.h>
+
+struct gpiocharger_softc {
+       struct device            sc_dev;
+       int                      sc_node;
+       uint32_t                *sc_charger_pin;
+       struct ksensor           sc_sensor;
+       struct ksensordev        sc_sensordev;
+};
+
+int     gpiocharger_match(struct device *, void *, void *);
+void    gpiocharger_attach(struct device *, struct device *, void *);
+
+struct cfattach        gpiocharger_ca = {
+       sizeof (struct gpiocharger_softc), gpiocharger_match, gpiocharger_attach
+};
+
+struct cfdriver gpiocharger_cd = {
+       NULL, "gpiocharger", DV_DULL
+};
+
+void    gpiocharger_update_charger(void *);
+
+int
+gpiocharger_match(struct device *parent, void *match, void *aux)
+{
+       const struct fdt_attach_args    *faa = aux;
+
+       return OF_is_compatible(faa->fa_node, "gpio-charger");
+}
+
+void
+gpiocharger_attach(struct device *parent, struct device *self, void *aux)
+{
+       struct gpiocharger_softc        *sc = (struct gpiocharger_softc *)self;
+       struct fdt_attach_args          *faa = aux;
+       char                            *charger_type, *gpios_property;
+       int                              charger_type_len, gpios_len;
+       int                              node = faa->fa_node;
+
+       pinctrl_byname(node, "default");
+
+       charger_type_len = OF_getproplen(node, "charger-type");
+       if (charger_type_len <= 0)
+               goto nocharger;
+       gpios_property = "gpios";
+       gpios_len = OF_getproplen(node, gpios_property);
+       if (gpios_len <= 0) {
+               gpios_property = "charger-status-gpios");
+               gpios_len = OF_getproplen(node, gpios_property);
+               if (gpios_len <= 0)
+                       goto nocharger;
+       }
+
+       charger_type = malloc(charger_type_len, M_TEMP, M_WAITOK);
+       OF_getprop(node, "charger-type", charger_type, charger_type_len);
+       sc->sc_charger_pin = malloc(gpios_len, M_DEVBUF, M_WAITOK);
+       OF_getpropintarray(node, gpios_property, sc->sc_charger_pin, gpios_len);
+       gpio_controller_config_pin(sc->sc_charger_pin, GPIO_CONFIG_INPUT);
+
+       strlcpy(sc->sc_sensor.desc, charger_type, sizeof(sc->sc_sensor.desc));
+       strlcat(sc->sc_sensor.desc, " power supply",
+           sizeof(sc->sc_sensor.desc));
+       sc->sc_sensor.type = SENSOR_INDICATOR;
+       sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
+       strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
+           sizeof(sc->sc_sensordev.xname));
+       sensordev_install(&sc->sc_sensordev);
+       sensor_task_register(sc, gpiocharger_update_charger, 5);
+
+       printf(": \"%s\"\n", charger_type);
+       free(charger_type, M_TEMP, charger_type_len);
+       return;
+
+nocharger:
+       printf(": no charger\n");
+}
+
+void
+gpiocharger_update_charger(void *arg)
+{
+       struct gpiocharger_softc        *sc = arg;
+
+       sc->sc_sensor.value = gpio_controller_get_pin(sc->sc_charger_pin);
+}
Index: sys/dev/fdt/files.fdt
===================================================================
RCS file: /cvs/src/sys/dev/fdt/files.fdt,v
retrieving revision 1.155
diff -u -p -r1.155 files.fdt
--- sys/dev/fdt/files.fdt       29 Jun 2021 12:43:09 -0000      1.155
+++ sys/dev/fdt/files.fdt       24 Sep 2021 22:27:59 -0000
@@ -588,3 +588,11 @@ file       dev/fdt/cwfg.c                  cwfg
 device dapmic
 attach dapmic at i2c
 file   dev/fdt/dapmic.c                dapmic
+
+device gpioleds
+attach gpioleds at fdt
+file   dev/fdt/gpioleds.c              gpioleds
+
+device gpiocharger
+attach gpiocharger at fdt
+file   dev/fdt/gpiocharger.c           gpiocharger
Index: sys/arch/arm64/conf/GENERIC
===================================================================
RCS file: /cvs/src/sys/arch/arm64/conf/GENERIC,v
retrieving revision 1.206
diff -u -p -r1.206 GENERIC
--- sys/arch/arm64/conf/GENERIC 3 Sep 2021 14:53:09 -0000       1.206
+++ sys/arch/arm64/conf/GENERIC 24 Sep 2021 22:27:59 -0000
@@ -131,6 +131,9 @@ amdgpu*             at pci?
 drm*           at amdgpu?
 wsdisplay*     at amdgpu?
 
+gpioleds*      at fdt?
+gpiocharger*   at fdt?
+
 # Apple
 apldart*       at fdt?
 apldog*                at fdt? early 1
Index: distrib/sets/lists/man/mi
===================================================================
RCS file: /cvs/src/distrib/sets/lists/man/mi,v
retrieving revision 1.1639
diff -u -p -r1.1639 mi
--- distrib/sets/lists/man/mi   21 Sep 2021 21:19:22 -0000      1.1639
+++ distrib/sets/lists/man/mi   24 Sep 2021 22:27:59 -0000
@@ -1417,8 +1417,10 @@
 ./usr/share/man/man4/glenv.4
 ./usr/share/man/man4/glkgpio.4
 ./usr/share/man/man4/gpio.4
+./usr/share/man/man4/gpiocharger.4
 ./usr/share/man/man4/gpiodcf.4
 ./usr/share/man/man4/gpioiic.4
+./usr/share/man/man4/gpioleds.4
 ./usr/share/man/man4/gpioow.4
 ./usr/share/man/man4/graphaudio.4
 ./usr/share/man/man4/gre.4

Reply via email to