xref: /openbsd-src/sys/dev/acpi/acpials.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /* $OpenBSD: acpials.c,v 1.1 2016/07/30 16:25:04 jcs 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 };
75 
76 int
77 acpials_match(struct device *parent, void *match, void *aux)
78 {
79 	struct acpi_attach_args *aa = aux;
80 	struct cfdata *cf = match;
81 
82 	return (acpi_matchhids(aa, acpials_hids, cf->cf_driver->cd_name));
83 }
84 
85 void
86 acpials_attach(struct device *parent, struct device *self, void *aux)
87 {
88 	struct acpials_softc *sc = (struct acpials_softc *)self;
89 	struct acpi_attach_args *aa = aux;
90 	int64_t st;
91 
92 	sc->sc_acpi = (struct acpi_softc *)parent;
93 	sc->sc_devnode = aa->aaa_node;
94 
95 	printf(": %s\n", sc->sc_devnode->name);
96 
97 	if (aml_evalinteger(sc->sc_acpi, sc->sc_devnode, "_STA", 0, NULL, &st))
98 		st = STA_PRESENT | STA_ENABLED | STA_DEV_OK;
99 	if ((st & (STA_PRESENT | STA_ENABLED | STA_DEV_OK)) !=
100 	    (STA_PRESENT | STA_ENABLED | STA_DEV_OK))
101 		return;
102 
103 	if (acpials_read(sc))
104 		return;
105 
106 	strlcpy(sc->sc_sensordev.xname, DEVNAME(sc),
107 	    sizeof(sc->sc_sensordev.xname));
108 	strlcpy(sc->sc_sensor.desc, "ambient light sensor",
109 	    sizeof(sc->sc_sensor.desc));
110 	sc->sc_sensor.type = SENSOR_LUX;
111 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
112 
113 	/*
114 	 * aml_register_notify with ACPIDEV_POLL is too slow (10 second
115 	 * intervals), so register the task with sensors so we can specify the
116 	 * interval, which will then just inject an acpi task and tell it to
117 	 * wakeup to handle the task.
118 	 */
119 	if (!(sc->sc_sensor_task = sensor_task_register(sc, acpials_addtask,
120 	    1))) {
121 		printf("%s: unable to register task\n", sc->sc_dev.dv_xname);
122 		return;
123 	}
124 
125 	/*
126 	 * But also install an event handler in case AML Notify()s us of any
127 	 * large changes - 9.2.7
128 	 */
129 	aml_register_notify(sc->sc_devnode, aa->aaa_dev, acpials_notify,
130 	    sc, ACPIDEV_NOPOLL);
131 
132 	sensordev_install(&sc->sc_sensordev);
133 }
134 
135 int
136 acpials_read(struct acpials_softc *sc)
137 {
138 	int64_t	ali = 0;
139 
140 	/* 9.2.2 - "Current ambient light illuminance reading in lux" */
141 	if (aml_evalinteger(sc->sc_acpi, sc->sc_devnode, "_ALI", 0, NULL,
142 	    &ali))
143 		return 1;
144 
145 	sc->sc_sensor.value = (ali * 1000000);
146 
147 	return 0;
148 }
149 
150 int
151 acpials_notify(struct aml_node *node, int notify_type, void *arg)
152 {
153 	struct acpials_softc *sc = arg;
154 
155 	DPRINTF(("%s: %s: %d\n", sc->sc_dev.dv_xname, __func__, notify_type));
156 
157 	if (notify_type == 0x80)
158 		acpials_read(sc);
159 
160 	return 0;
161 }
162 
163 void
164 acpials_addtask(void *arg)
165 {
166 	struct acpials_softc *sc = arg;
167 
168 	acpi_addtask(sc->sc_acpi, acpials_update, sc, 0);
169 	acpi_wakeup(sc->sc_acpi);
170 }
171 
172 void
173 acpials_update(void *arg0, int arg1)
174 {
175 	struct acpials_softc *sc = arg0;
176 
177 	if (acpials_read(sc) == 0) {
178 		DPRINTF(("%s: %s: %lld\n", sc->sc_dev.dv_xname, __func__,
179 		    sc->sc_sensor.value));
180 		sc->sc_sensor.flags &= ~SENSOR_FINVALID;
181 	} else
182 		sc->sc_sensor.flags |= SENSOR_FINVALID;
183 }
184