1 /* $OpenBSD: acpials.c,v 1.2 2016/12/23 19:55:02 kettenis Exp $ */ 2 /* 3 * Ambient Light Sensor device driver 4 * ACPI 5.0 spec section 9.2 5 * 6 * Copyright (c) 2016 joshua stein <jcs@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/param.h> 22 #include <sys/systm.h> 23 #include <sys/device.h> 24 25 #include <dev/acpi/acpireg.h> 26 #include <dev/acpi/acpivar.h> 27 #include <dev/acpi/acpidev.h> 28 #include <dev/acpi/amltypes.h> 29 #include <dev/acpi/dsdt.h> 30 31 #include <sys/sensors.h> 32 33 /* #define ACPIALS_DEBUG */ 34 35 #ifdef ACPIALS_DEBUG 36 #define DPRINTF(x) printf x 37 #else 38 #define DPRINTF(x) 39 #endif 40 41 struct acpials_softc { 42 struct device sc_dev; 43 44 bus_space_tag_t sc_iot; 45 bus_space_handle_t sc_ioh; 46 47 struct acpi_softc *sc_acpi; 48 struct aml_node *sc_devnode; 49 50 struct ksensor sc_sensor; 51 struct ksensordev sc_sensordev; 52 struct sensor_task *sc_sensor_task; 53 }; 54 55 int acpials_match(struct device *, void *, void *); 56 void acpials_attach(struct device *, struct device *, void *); 57 int acpials_read(struct acpials_softc *); 58 int acpials_notify(struct aml_node *, int, void *); 59 void acpials_addtask(void *); 60 void acpials_update(void *, int); 61 62 struct cfattach acpials_ca = { 63 sizeof(struct acpials_softc), 64 acpials_match, 65 acpials_attach, 66 }; 67 68 struct cfdriver acpials_cd = { 69 NULL, "acpials", DV_DULL 70 }; 71 72 const char *acpials_hids[] = { 73 "ACPI0008", 74 NULL 75 }; 76 77 int 78 acpials_match(struct device *parent, void *match, void *aux) 79 { 80 struct acpi_attach_args *aa = aux; 81 struct cfdata *cf = match; 82 83 return (acpi_matchhids(aa, acpials_hids, cf->cf_driver->cd_name)); 84 } 85 86 void 87 acpials_attach(struct device *parent, struct device *self, void *aux) 88 { 89 struct acpials_softc *sc = (struct acpials_softc *)self; 90 struct acpi_attach_args *aa = aux; 91 int64_t st; 92 93 sc->sc_acpi = (struct acpi_softc *)parent; 94 sc->sc_devnode = aa->aaa_node; 95 96 printf(": %s\n", sc->sc_devnode->name); 97 98 if (aml_evalinteger(sc->sc_acpi, sc->sc_devnode, "_STA", 0, NULL, &st)) 99 st = STA_PRESENT | STA_ENABLED | STA_DEV_OK; 100 if ((st & (STA_PRESENT | STA_ENABLED | STA_DEV_OK)) != 101 (STA_PRESENT | STA_ENABLED | STA_DEV_OK)) 102 return; 103 104 if (acpials_read(sc)) 105 return; 106 107 strlcpy(sc->sc_sensordev.xname, DEVNAME(sc), 108 sizeof(sc->sc_sensordev.xname)); 109 strlcpy(sc->sc_sensor.desc, "ambient light sensor", 110 sizeof(sc->sc_sensor.desc)); 111 sc->sc_sensor.type = SENSOR_LUX; 112 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor); 113 114 /* 115 * aml_register_notify with ACPIDEV_POLL is too slow (10 second 116 * intervals), so register the task with sensors so we can specify the 117 * interval, which will then just inject an acpi task and tell it to 118 * wakeup to handle the task. 119 */ 120 if (!(sc->sc_sensor_task = sensor_task_register(sc, acpials_addtask, 121 1))) { 122 printf("%s: unable to register task\n", sc->sc_dev.dv_xname); 123 return; 124 } 125 126 /* 127 * But also install an event handler in case AML Notify()s us of any 128 * large changes - 9.2.7 129 */ 130 aml_register_notify(sc->sc_devnode, aa->aaa_dev, acpials_notify, 131 sc, ACPIDEV_NOPOLL); 132 133 sensordev_install(&sc->sc_sensordev); 134 } 135 136 int 137 acpials_read(struct acpials_softc *sc) 138 { 139 int64_t ali = 0; 140 141 /* 9.2.2 - "Current ambient light illuminance reading in lux" */ 142 if (aml_evalinteger(sc->sc_acpi, sc->sc_devnode, "_ALI", 0, NULL, 143 &ali)) 144 return 1; 145 146 sc->sc_sensor.value = (ali * 1000000); 147 148 return 0; 149 } 150 151 int 152 acpials_notify(struct aml_node *node, int notify_type, void *arg) 153 { 154 struct acpials_softc *sc = arg; 155 156 DPRINTF(("%s: %s: %d\n", sc->sc_dev.dv_xname, __func__, notify_type)); 157 158 if (notify_type == 0x80) 159 acpials_read(sc); 160 161 return 0; 162 } 163 164 void 165 acpials_addtask(void *arg) 166 { 167 struct acpials_softc *sc = arg; 168 169 acpi_addtask(sc->sc_acpi, acpials_update, sc, 0); 170 acpi_wakeup(sc->sc_acpi); 171 } 172 173 void 174 acpials_update(void *arg0, int arg1) 175 { 176 struct acpials_softc *sc = arg0; 177 178 if (acpials_read(sc) == 0) { 179 DPRINTF(("%s: %s: %lld\n", sc->sc_dev.dv_xname, __func__, 180 sc->sc_sensor.value)); 181 sc->sc_sensor.flags &= ~SENSOR_FINVALID; 182 } else 183 sc->sc_sensor.flags |= SENSOR_FINVALID; 184 } 185