From: jcs Date: Sat, 30 Jul 2016 16:25:04 +0000 (+0000) Subject: add acpials(4), an acpi driver for ambient light sensors X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=50e2d8d7e42aeb2591772f9abd816fb8d3525612;p=openbsd add acpials(4), an acpi driver for ambient light sensors hw.sensors.acpials0.illuminance0=11.00 lx (ambient light sensor) --- diff --git a/share/man/man4/acpi.4 b/share/man/man4/acpi.4 index 0a74c3fab4f..269bc0a7a36 100644 --- a/share/man/man4/acpi.4 +++ b/share/man/man4/acpi.4 @@ -1,4 +1,4 @@ -.\" $OpenBSD: acpi.4,v 1.45 2016/05/07 23:16:09 kettenis Exp $ +.\" $OpenBSD: acpi.4,v 1.46 2016/07/30 16:25:04 jcs Exp $ .\" .\" Copyright (c) 2006 Alexander Yurchenko .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: May 7 2016 $ +.Dd $Mdocdate: July 30 2016 $ .Dt ACPI 4 .Os .Sh NAME @@ -40,6 +40,8 @@ The following devices can attach to .Bl -tag -width "acpithinkpad(4)XXX" -offset indent -compact .It Xr acpiac 4 ACPI AC adapter +.It Xr acpials 4 +ACPI ambient light sensor .It Xr acpiasus 4 ASUS ACPI hotkeys .It Xr acpibat 4 diff --git a/share/man/man4/acpials.4 b/share/man/man4/acpials.4 new file mode 100644 index 00000000000..55d8d7f7bc0 --- /dev/null +++ b/share/man/man4/acpials.4 @@ -0,0 +1,50 @@ +.\" $OpenBSD: acpials.4,v 1.1 2016/07/30 16:25:04 jcs Exp $ +.\" +.\" Copyright (c) 2016 joshua stein +.\" +.\" 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: July 30 2016 $ +.Dt ACPIALS 4 +.Os +.Sh NAME +.Nm acpials +.Nd ACPI ambient light sensor +.Sh SYNOPSIS +.Cd "acpials* at acpi?" +.Sh DESCRIPTION +The +.Nm +driver supports ACPI ambient light sensor devices and provides +information about the ambient light environment in which the system is +currently operating. +Ambient light illuminance is read every second and can be monitored +using +.Xr sysctl 8 +or +.Xr sensorsd 8 . +.Sh SEE ALSO +.Xr acpi 4 , +.Xr sensorsd 8 , +.Xr sysctl 8 +.Sh HISTORY +The +.Nm +driver first appeared in +.Ox 6.1 . +.Sh AUTHORS +.An -nosplit +The +.Nm +driver was written by +.An joshua stein Aq Mt jcs@openbsd.org . diff --git a/sys/arch/amd64/conf/GENERIC b/sys/arch/amd64/conf/GENERIC index 8eb1b13388b..f6ccd404b81 100644 --- a/sys/arch/amd64/conf/GENERIC +++ b/sys/arch/amd64/conf/GENERIC @@ -1,4 +1,4 @@ -# $OpenBSD: GENERIC,v 1.425 2016/07/15 14:40:15 jcs Exp $ +# $OpenBSD: GENERIC,v 1.426 2016/07/30 16:25:04 jcs Exp $ # # For further information on compiling OpenBSD kernels, see the config(8) # man page. @@ -65,6 +65,7 @@ bytgpio* at acpi? chvgpio* at acpi? sdhc* at acpi? #acpicbkbd* at acpi? +acpials* at acpi? mpbios0 at bios0 diff --git a/sys/dev/acpi/acpials.c b/sys/dev/acpi/acpials.c new file mode 100644 index 00000000000..2d877d6bc06 --- /dev/null +++ b/sys/dev/acpi/acpials.c @@ -0,0 +1,183 @@ +/* $OpenBSD: acpials.c,v 1.1 2016/07/30 16:25:04 jcs Exp $ */ +/* + * Ambient Light Sensor device driver + * ACPI 5.0 spec section 9.2 + * + * Copyright (c) 2016 joshua stein + * + * 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 + +/* #define ACPIALS_DEBUG */ + +#ifdef ACPIALS_DEBUG +#define DPRINTF(x) printf x +#else +#define DPRINTF(x) +#endif + +struct acpials_softc { + struct device sc_dev; + + bus_space_tag_t sc_iot; + bus_space_handle_t sc_ioh; + + struct acpi_softc *sc_acpi; + struct aml_node *sc_devnode; + + struct ksensor sc_sensor; + struct ksensordev sc_sensordev; + struct sensor_task *sc_sensor_task; +}; + +int acpials_match(struct device *, void *, void *); +void acpials_attach(struct device *, struct device *, void *); +int acpials_read(struct acpials_softc *); +int acpials_notify(struct aml_node *, int, void *); +void acpials_addtask(void *); +void acpials_update(void *, int); + +struct cfattach acpials_ca = { + sizeof(struct acpials_softc), + acpials_match, + acpials_attach, +}; + +struct cfdriver acpials_cd = { + NULL, "acpials", DV_DULL +}; + +const char *acpials_hids[] = { + "ACPI0008", +}; + +int +acpials_match(struct device *parent, void *match, void *aux) +{ + struct acpi_attach_args *aa = aux; + struct cfdata *cf = match; + + return (acpi_matchhids(aa, acpials_hids, cf->cf_driver->cd_name)); +} + +void +acpials_attach(struct device *parent, struct device *self, void *aux) +{ + struct acpials_softc *sc = (struct acpials_softc *)self; + struct acpi_attach_args *aa = aux; + int64_t st; + + sc->sc_acpi = (struct acpi_softc *)parent; + sc->sc_devnode = aa->aaa_node; + + printf(": %s\n", sc->sc_devnode->name); + + if (aml_evalinteger(sc->sc_acpi, sc->sc_devnode, "_STA", 0, NULL, &st)) + st = STA_PRESENT | STA_ENABLED | STA_DEV_OK; + if ((st & (STA_PRESENT | STA_ENABLED | STA_DEV_OK)) != + (STA_PRESENT | STA_ENABLED | STA_DEV_OK)) + return; + + if (acpials_read(sc)) + return; + + strlcpy(sc->sc_sensordev.xname, DEVNAME(sc), + sizeof(sc->sc_sensordev.xname)); + strlcpy(sc->sc_sensor.desc, "ambient light sensor", + sizeof(sc->sc_sensor.desc)); + sc->sc_sensor.type = SENSOR_LUX; + sensor_attach(&sc->sc_sensordev, &sc->sc_sensor); + + /* + * aml_register_notify with ACPIDEV_POLL is too slow (10 second + * intervals), so register the task with sensors so we can specify the + * interval, which will then just inject an acpi task and tell it to + * wakeup to handle the task. + */ + if (!(sc->sc_sensor_task = sensor_task_register(sc, acpials_addtask, + 1))) { + printf("%s: unable to register task\n", sc->sc_dev.dv_xname); + return; + } + + /* + * But also install an event handler in case AML Notify()s us of any + * large changes - 9.2.7 + */ + aml_register_notify(sc->sc_devnode, aa->aaa_dev, acpials_notify, + sc, ACPIDEV_NOPOLL); + + sensordev_install(&sc->sc_sensordev); +} + +int +acpials_read(struct acpials_softc *sc) +{ + int64_t ali = 0; + + /* 9.2.2 - "Current ambient light illuminance reading in lux" */ + if (aml_evalinteger(sc->sc_acpi, sc->sc_devnode, "_ALI", 0, NULL, + &ali)) + return 1; + + sc->sc_sensor.value = (ali * 1000000); + + return 0; +} + +int +acpials_notify(struct aml_node *node, int notify_type, void *arg) +{ + struct acpials_softc *sc = arg; + + DPRINTF(("%s: %s: %d\n", sc->sc_dev.dv_xname, __func__, notify_type)); + + if (notify_type == 0x80) + acpials_read(sc); + + return 0; +} + +void +acpials_addtask(void *arg) +{ + struct acpials_softc *sc = arg; + + acpi_addtask(sc->sc_acpi, acpials_update, sc, 0); + acpi_wakeup(sc->sc_acpi); +} + +void +acpials_update(void *arg0, int arg1) +{ + struct acpials_softc *sc = arg0; + + if (acpials_read(sc) == 0) { + DPRINTF(("%s: %s: %lld\n", sc->sc_dev.dv_xname, __func__, + sc->sc_sensor.value)); + sc->sc_sensor.flags &= ~SENSOR_FINVALID; + } else + sc->sc_sensor.flags |= SENSOR_FINVALID; +} diff --git a/sys/dev/acpi/files.acpi b/sys/dev/acpi/files.acpi index 076596a3848..7031408b5ae 100644 --- a/sys/dev/acpi/files.acpi +++ b/sys/dev/acpi/files.acpi @@ -1,4 +1,4 @@ -# $OpenBSD: files.acpi,v 1.33 2016/07/01 15:02:49 jcs Exp $ +# $OpenBSD: files.acpi,v 1.34 2016/07/30 16:25:04 jcs Exp $ # # Config file and device description for machine-independent ACPI code. # Included by ports that need it. @@ -135,3 +135,8 @@ file dev/acpi/dwiic.c dwiic device acpicbkbd attach acpicbkbd at acpi file dev/acpi/acpicbkbd.c acpicbkbd + +# Ambient Light Sensor +device acpials +attach acpials at acpi +file dev/acpi/acpials.c acpials