1 /* $OpenBSD: aps.c,v 1.19 2009/05/24 16:40:18 jsg Exp $ */
3 * Copyright (c) 2005 Jonathan Gray <jsg@openbsd.org>
4 * Copyright (c) 2008 Can Erkin Acar <canacar@openbsd.org>
5 * Copyright (c) 2010 Constantine A. Murenin <cnst++@dragonflybsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * A driver for the ThinkPad Active Protection System based on notes from
22 * http://www.almaden.ibm.com/cs/people/marksmith/tpaps.html
25 #include <sys/param.h>
26 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/module.h>
31 #include <sys/sensors.h>
33 #include <bus/isa/isavar.h>
36 #define DPRINTF(x) do { kprintf x; } while (0)
43 * EC interface on Thinkpad Laptops, from Linux HDAPS driver notes.
44 * From Renesans H8S/2140B Group Hardware Manual
45 * http://documentation.renesas.com/eng/products/mpumcu/rej09b0300_2140bhm.pdf
47 * EC uses LPC Channel 3 registers TWR0..15
50 /* STR3 status register */
53 #define APS_STR3_IBF3B 0x80 /* Input buffer full (host->slave) */
54 #define APS_STR3_OBF3B 0x40 /* Output buffer full (slave->host)*/
55 #define APS_STR3_MWMF 0x20 /* Master write mode */
56 #define APS_STR3_SWMF 0x10 /* Slave write mode */
59 /* Base address of TWR registers */
60 #define APS_TWR_BASE 0x10
61 #define APS_TWR_RET 0x1f
71 #define APS_STATE 0x01
72 #define APS_XACCEL 0x02
73 #define APS_YACCEL 0x04
77 #define APS_TEMP2 0x0b
78 #define APS_UNKNOWN 0x0c
79 #define APS_INPUT 0x0d
81 /* write masks for I/O, send command + 0-3 arguments*/
82 #define APS_WRITE_0 0x0001
83 #define APS_WRITE_1 0x0003
84 #define APS_WRITE_2 0x0007
85 #define APS_WRITE_3 0x000f
87 /* read masks for I/O, read 0-3 values (skip command byte) */
88 #define APS_READ_0 0x0000
89 #define APS_READ_1 0x0002
90 #define APS_READ_2 0x0006
91 #define APS_READ_3 0x000e
93 #define APS_READ_RET 0x8000
94 #define APS_READ_ALL 0xffff
96 /* Bit definitions for APS_INPUT value */
97 #define APS_INPUT_KB (1 << 5)
98 #define APS_INPUT_MS (1 << 6)
99 #define APS_INPUT_LIDOPEN (1 << 7)
101 #define APS_ADDR_BASE 0x1600
102 #define APS_ADDR_SIZE 0x1f
104 struct aps_sensor_rec {
116 #define APS_NUM_SENSORS 9
118 #define APS_SENSOR_XACCEL 0
119 #define APS_SENSOR_YACCEL 1
120 #define APS_SENSOR_XVAR 2
121 #define APS_SENSOR_YVAR 3
122 #define APS_SENSOR_TEMP1 4
123 #define APS_SENSOR_TEMP2 5
124 #define APS_SENSOR_KBACT 6
125 #define APS_SENSOR_MSACT 7
126 #define APS_SENSOR_LIDOPEN 8
129 struct device *sc_dev;
131 struct resource *sc_iores;
134 struct ksensor sensors[APS_NUM_SENSORS];
135 struct ksensordev sensordev;
137 struct aps_sensor_rec aps_data;
140 static void aps_identify(driver_t *, struct device *);
141 static int aps_probe(struct device *);
142 static int aps_attach(struct device *);
143 static int aps_detach(struct device *);
145 static int aps_resume(struct device *);
146 static int aps_suspend(struct device *);
148 static int aps_init(struct resource *);
149 static int aps_read_data(struct aps_softc *);
150 static void aps_refresh_sensor_data(struct aps_softc *);
151 static void aps_refresh(void *);
152 static int aps_do_io(struct resource *, unsigned char *, int, int);
154 static device_method_t aps_methods[] = {
155 DEVMETHOD(device_identify, aps_identify),
156 DEVMETHOD(device_probe, aps_probe),
157 DEVMETHOD(device_attach, aps_attach),
158 DEVMETHOD(device_detach, aps_detach),
160 DEVMETHOD(device_resume, aps_resume),
161 DEVMETHOD(device_suspend, aps_suspend),
165 static driver_t aps_driver = {
168 sizeof(struct aps_softc)
171 static devclass_t aps_devclass;
173 DRIVER_MODULE(aps, isa, aps_driver, aps_devclass, NULL, NULL);
177 /* properly communicate with the controller, writing a set of memory
178 * locations and reading back another set */
180 aps_do_io(struct resource *iores, unsigned char *buf, int wmask, int rmask)
182 bus_space_tag_t iot = rman_get_bustag(iores);
183 bus_space_handle_t ioh = rman_get_bushandle(iores);
186 DPRINTF(("aps_do_io: CMD: 0x%02x, wmask: 0x%04x, rmask: 0x%04x\n",
187 buf[0], wmask, rmask));
189 /* write init byte using arbitration */
190 for (n = 0; n < 100; n++) {
191 stat = bus_space_read_1(iot, ioh, APS_STR3);
192 if (stat & (APS_STR3_OBF3B | APS_STR3_SWMF)) {
193 bus_space_read_1(iot, ioh, APS_TWR_RET);
196 bus_space_write_1(iot, ioh, APS_TWR_BASE, buf[0]);
197 stat = bus_space_read_1(iot, ioh, APS_STR3);
198 if (stat & (APS_STR3_MWMF))
204 DPRINTF(("aps_do_io: Failed to get bus\n"));
208 /* write data bytes, init already sent */
209 /* make sure last bye is always written as this will trigger slave */
210 wmask |= APS_READ_RET;
213 for (n = 1, bp = 2; n < 16; bp <<= 1, n++) {
215 bus_space_write_1(iot, ioh, APS_TWR_BASE + n, buf[n]);
216 DPRINTF(("aps_do_io: write %2d 0x%02x\n", n, buf[n]));
220 for (n = 0; n < 100; n++) {
221 stat = bus_space_read_1(iot, ioh, APS_STR3);
222 if (stat & (APS_STR3_OBF3B))
228 DPRINTF(("aps_do_io: timeout waiting response\n"));
231 /* wait for data available */
232 /* make sure to read the final byte to clear status */
233 rmask |= APS_READ_RET;
235 /* read cmd and data bytes */
236 for (n = 0, bp = 1; n < 16; bp <<= 1, n++) {
238 buf[n] = bus_space_read_1(iot, ioh, APS_TWR_BASE + n);
239 DPRINTF(("aps_do_io: read %2d 0x%02x\n", n, buf[n]));
246 /* for hints, see /sys/bus/isa/isahint.c */
249 aps_identify(driver_t *driver, struct device *parent)
251 struct device *child;
253 child = device_find_child(parent, driver->name, -1);
255 if (isa_get_portsize(child) == 0) {
256 // aps(4) must have been compiled into the kernel
258 kprintf("%s: will specify the port\n",
260 } else if (isa_get_port(child) != APS_ADDR_BASE)
261 kprintf("%s: will overwrite specified port\n",
264 if (isa_get_portsize(child) == APS_ADDR_SIZE) {
265 // aps.ko must have been reloaded
266 kprintf("%s: already have been invoked\n",
270 kprintf("%s: will amend the portsize\n",
274 // first invocation of `kldload aps.ko`
275 kprintf("%s: creating a new %s\n",
276 __func__, driver->name);
277 child = BUS_ADD_CHILD(parent, parent, ISA_ORDER_PNP,
280 kprintf("%s: cannot add child\n", __func__);
284 if (bus_set_resource(child, SYS_RES_IOPORT, 0,
285 APS_ADDR_BASE, APS_ADDR_SIZE, -1))
286 kprintf("%s: cannot set resource\n", __func__);
290 aps_probe(struct device *dev)
292 struct resource *iores;
295 unsigned char iobuf[16];
297 #if defined(APSDEBUG) || defined(KLD_MODULE)
298 device_printf(dev, "%s: port 0x%x\n", __func__, isa_get_port(dev));
301 if (device_get_unit(dev) != 0)
304 iores = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &iorid, RF_ACTIVE);
306 DPRINTF(("aps: can't map i/o space\n"));
311 /* See if this machine has APS */
314 iobuf[APS_CMD] = 0x13;
315 if (aps_do_io(iores, iobuf, APS_WRITE_0, APS_READ_1)) {
316 bus_release_resource(dev, SYS_RES_IOPORT, iorid, iores);
321 * Observed values from Linux driver:
323 * 0x02: chip already initialised
328 cr = iobuf[APS_ARG1];
330 bus_release_resource(dev, SYS_RES_IOPORT, iorid, iores);
331 DPRINTF(("aps: state register 0x%x\n", cr));
333 if (iobuf[APS_RET] != 0 || cr < 1 || cr > 5) {
334 DPRINTF(("aps: unsupported state %d\n", cr));
337 device_set_desc(dev, "ThinkPad Active Protection System");
342 aps_attach(struct device *dev)
344 struct aps_softc *sc = device_get_softc(dev);
347 sc->sc_iores = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
348 &sc->sc_iorid, RF_ACTIVE);
349 if (sc->sc_iores == NULL) {
350 device_printf(dev, "can't map i/o space\n");
354 if (aps_init(sc->sc_iores)) {
355 device_printf(dev, "failed to initialise\n");
356 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_iorid, sc->sc_iores);
360 sc->sensors[APS_SENSOR_XACCEL].type = SENSOR_INTEGER;
361 ksnprintf(sc->sensors[APS_SENSOR_XACCEL].desc,
362 sizeof(sc->sensors[APS_SENSOR_XACCEL].desc), "X_ACCEL");
364 sc->sensors[APS_SENSOR_YACCEL].type = SENSOR_INTEGER;
365 ksnprintf(sc->sensors[APS_SENSOR_YACCEL].desc,
366 sizeof(sc->sensors[APS_SENSOR_YACCEL].desc), "Y_ACCEL");
368 sc->sensors[APS_SENSOR_TEMP1].type = SENSOR_TEMP;
369 sc->sensors[APS_SENSOR_TEMP2].type = SENSOR_TEMP;
371 sc->sensors[APS_SENSOR_XVAR].type = SENSOR_INTEGER;
372 ksnprintf(sc->sensors[APS_SENSOR_XVAR].desc,
373 sizeof(sc->sensors[APS_SENSOR_XVAR].desc), "X_VAR");
375 sc->sensors[APS_SENSOR_YVAR].type = SENSOR_INTEGER;
376 ksnprintf(sc->sensors[APS_SENSOR_YVAR].desc,
377 sizeof(sc->sensors[APS_SENSOR_YVAR].desc), "Y_VAR");
379 sc->sensors[APS_SENSOR_KBACT].type = SENSOR_INDICATOR;
380 ksnprintf(sc->sensors[APS_SENSOR_KBACT].desc,
381 sizeof(sc->sensors[APS_SENSOR_KBACT].desc), "Keyboard Active");
383 sc->sensors[APS_SENSOR_MSACT].type = SENSOR_INDICATOR;
384 ksnprintf(sc->sensors[APS_SENSOR_MSACT].desc,
385 sizeof(sc->sensors[APS_SENSOR_MSACT].desc), "Mouse Active");
387 sc->sensors[APS_SENSOR_LIDOPEN].type = SENSOR_INDICATOR;
388 ksnprintf(sc->sensors[APS_SENSOR_LIDOPEN].desc,
389 sizeof(sc->sensors[APS_SENSOR_LIDOPEN].desc), "Lid Open");
391 /* stop hiding and report to the authorities */
392 strlcpy(sc->sensordev.xname, device_get_nameunit(dev),
393 sizeof(sc->sensordev.xname));
394 for (int i = 0; i < APS_NUM_SENSORS ; i++)
395 sensor_attach(&sc->sensordev, &sc->sensors[i]);
397 /* Refresh sensor data every 1 second */
398 /* XXX: a more frequent refresh might be appropriate */
399 if (sensor_task_register(sc, aps_refresh, 1)) {
400 device_printf(dev, "unable to register update task\n");
401 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_iorid, sc->sc_iores);
405 sensordev_install(&sc->sensordev);
410 aps_detach(struct device *dev)
412 struct aps_softc *sc = device_get_softc(dev);
414 sensordev_deinstall(&sc->sensordev);
415 sensor_task_unregister(sc);
416 return bus_release_resource(dev, SYS_RES_IOPORT,
417 sc->sc_iorid, sc->sc_iores);
421 aps_init(struct resource *iores)
423 unsigned char iobuf[16];
425 /* command 0x17/0x81: check EC */
426 iobuf[APS_CMD] = 0x17;
427 iobuf[APS_ARG1] = 0x81;
429 if (aps_do_io(iores, iobuf, APS_WRITE_1, APS_READ_3))
432 if (iobuf[APS_RET] != 0 ||iobuf[APS_ARG3] != 0)
435 /* Test values from the Linux driver */
436 if ((iobuf[APS_ARG1] != 0 || iobuf[APS_ARG2] != 0x60) &&
437 (iobuf[APS_ARG1] != 1 || iobuf[APS_ARG2] != 0))
440 /* command 0x14: set power */
441 iobuf[APS_CMD] = 0x14;
442 iobuf[APS_ARG1] = 0x01;
444 if (aps_do_io(iores, iobuf, APS_WRITE_1, APS_READ_0))
447 if (iobuf[APS_RET] != 0)
450 /* command 0x10: set config (sample rate and order) */
451 iobuf[APS_CMD] = 0x10;
452 iobuf[APS_ARG1] = 0xc8;
453 iobuf[APS_ARG2] = 0x00;
454 iobuf[APS_ARG3] = 0x02;
456 if (aps_do_io(iores, iobuf, APS_WRITE_3, APS_READ_0))
459 if (iobuf[APS_RET] != 0)
462 /* command 0x11: refresh data */
463 iobuf[APS_CMD] = 0x11;
464 if (aps_do_io(iores, iobuf, APS_WRITE_0, APS_READ_1))
466 if (iobuf[APS_ARG1] != 0)
473 aps_read_data(struct aps_softc *sc)
475 unsigned char iobuf[16];
477 /* command 0x11: refresh data */
478 iobuf[APS_CMD] = 0x11;
479 if (aps_do_io(sc->sc_iores, iobuf, APS_WRITE_0, APS_READ_ALL))
482 sc->aps_data.state = iobuf[APS_STATE];
483 sc->aps_data.x_accel = iobuf[APS_XACCEL] + 256 * iobuf[APS_XACCEL + 1];
484 sc->aps_data.y_accel = iobuf[APS_YACCEL] + 256 * iobuf[APS_YACCEL + 1];
485 sc->aps_data.temp1 = iobuf[APS_TEMP];
486 sc->aps_data.x_var = iobuf[APS_XVAR] + 256 * iobuf[APS_XVAR + 1];
487 sc->aps_data.y_var = iobuf[APS_YVAR] + 256 * iobuf[APS_YVAR + 1];
488 sc->aps_data.temp2 = iobuf[APS_TEMP2];
489 sc->aps_data.input = iobuf[APS_INPUT];
495 aps_refresh_sensor_data(struct aps_softc *sc)
499 if (aps_read_data(sc)) {
500 for (int i = 0; i < APS_NUM_SENSORS; i++)
501 sc->sensors[i].flags |= SENSOR_FINVALID;
505 sc->sensors[APS_SENSOR_XACCEL].value = sc->aps_data.x_accel;
506 sc->sensors[APS_SENSOR_YACCEL].value = sc->aps_data.y_accel;
508 /* convert to micro (mu) degrees */
509 temp = sc->aps_data.temp1 * 1000000;
510 /* convert to kelvin */
512 sc->sensors[APS_SENSOR_TEMP1].value = temp;
514 /* convert to micro (mu) degrees */
515 temp = sc->aps_data.temp2 * 1000000;
516 /* convert to kelvin */
518 sc->sensors[APS_SENSOR_TEMP2].value = temp;
520 sc->sensors[APS_SENSOR_XVAR].value = sc->aps_data.x_var;
521 sc->sensors[APS_SENSOR_YVAR].value = sc->aps_data.y_var;
522 sc->sensors[APS_SENSOR_KBACT].value =
523 (sc->aps_data.input & APS_INPUT_KB) ? 1 : 0;
524 sc->sensors[APS_SENSOR_MSACT].value =
525 (sc->aps_data.input & APS_INPUT_MS) ? 1 : 0;
526 sc->sensors[APS_SENSOR_LIDOPEN].value =
527 (sc->aps_data.input & APS_INPUT_LIDOPEN) ? 1 : 0;
529 for (int i = 0; i < APS_NUM_SENSORS; i++)
530 sc->sensors[i].flags &= ~SENSOR_FINVALID;
534 aps_refresh(void *arg)
536 struct aps_softc *sc = (struct aps_softc *)arg;
538 aps_refresh_sensor_data(sc);
542 aps_resume(struct device *dev)
544 struct aps_softc *sc = device_get_softc(dev);
545 unsigned char iobuf[16];
548 * Redo the init sequence on resume, because APS is
549 * as forgetful as it is deaf.
553 iobuf[APS_CMD] = 0x13;
554 if (aps_do_io(sc->sc_iores, iobuf, APS_WRITE_0, APS_READ_1)
555 || aps_init(sc->sc_iores)) {
556 device_printf(sc->sc_dev, "failed to wake up\n");
560 sensor_task_register(sc, aps_refresh, 1);
565 aps_suspend(struct device *dev)
567 struct aps_softc *sc = device_get_softc(dev);
569 for (int i = 0; i < APS_NUM_SENSORS; i++)
570 sc->sensors[i].flags |= SENSOR_FINVALID;
571 sensor_task_unregister(sc);