Home | History | Annotate | Line # | Download | only in isa
aps.c revision 1.3
      1 /*	$NetBSD: aps.c,v 1.3 2007/11/16 08:00:15 xtraeme Exp $	*/
      2 /*	$OpenBSD: aps.c,v 1.15 2007/05/19 19:14:11 tedu Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2005 Jonathan Gray <jsg (at) openbsd.org>
      6  *
      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.
     10  *
     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.
     18  */
     19 
     20 /*
     21  * A driver for the ThinkPad Active Protection System based on notes from
     22  * http://www.almaden.ibm.com/cs/people/marksmith/tpaps.html
     23  */
     24 
     25 #include <sys/cdefs.h>
     26 __KERNEL_RCSID(0, "$NetBSD: aps.c,v 1.3 2007/11/16 08:00:15 xtraeme Exp $");
     27 
     28 #include <sys/param.h>
     29 #include <sys/systm.h>
     30 #include <sys/device.h>
     31 #include <sys/kernel.h>
     32 #include <sys/callout.h>
     33 
     34 #include <sys/bus.h>
     35 
     36 #include <dev/sysmon/sysmonvar.h>
     37 
     38 #include <dev/isa/isareg.h>
     39 #include <dev/isa/isavar.h>
     40 
     41 #if defined(APSDEBUG)
     42 #define DPRINTF(x)		do { printf x; } while (0)
     43 #else
     44 #define DPRINTF(x)
     45 #endif
     46 
     47 #define APS_ACCEL_STATE		0x04
     48 #define APS_INIT		0x10
     49 #define APS_STATE		0x11
     50 #define	APS_XACCEL		0x12
     51 #define APS_YACCEL		0x14
     52 #define APS_TEMP		0x16
     53 #define	APS_XVAR		0x17
     54 #define APS_YVAR		0x19
     55 #define APS_TEMP2		0x1b
     56 #define APS_UNKNOWN		0x1c
     57 #define APS_INPUT		0x1d
     58 #define APS_CMD			0x1f
     59 
     60 #define	APS_STATE_NEWDATA	0x50
     61 
     62 #define APS_CMD_START		0x01
     63 
     64 #define APS_INPUT_KB		(1 << 5)
     65 #define APS_INPUT_MS		(1 << 6)
     66 #define APS_INPUT_LIDOPEN	(1 << 7)
     67 
     68 #define APS_ADDR_SIZE		0x1f
     69 
     70 struct sensor_rec {
     71 	uint8_t 	state;
     72 	uint16_t	x_accel;
     73 	uint16_t	y_accel;
     74 	uint8_t 	temp1;
     75 	uint16_t	x_var;
     76 	uint16_t	y_var;
     77 	uint8_t 	temp2;
     78 	uint8_t 	unk;
     79 	uint8_t 	input;
     80 };
     81 
     82 enum aps_sensors {
     83         APS_SENSOR_XACCEL = 0,
     84         APS_SENSOR_YACCEL,
     85         APS_SENSOR_XVAR,
     86         APS_SENSOR_YVAR,
     87         APS_SENSOR_TEMP1,
     88         APS_SENSOR_TEMP2,
     89         APS_SENSOR_KBACT,
     90         APS_SENSOR_MSACT,
     91         APS_SENSOR_LIDOPEN,
     92         APS_NUM_SENSORS
     93 };
     94 
     95 struct aps_softc {
     96 	struct device sc_dev;
     97 
     98 	bus_space_tag_t sc_iot;
     99 	bus_space_handle_t sc_ioh;
    100 
    101 	struct sysmon_envsys *sc_sme;
    102 	envsys_data_t sc_sensor[APS_NUM_SENSORS];
    103 	struct callout sc_callout;
    104 
    105 	struct sensor_rec aps_data;
    106 	void *sc_powerhook;
    107 };
    108 
    109 static int 	aps_match(struct device *, struct cfdata *, void *);
    110 static void 	aps_attach(struct device *, struct device *, void *);
    111 static int	aps_detach(struct device *, int);
    112 
    113 static int 	aps_init(struct aps_softc *);
    114 static uint8_t  aps_mem_read_1(bus_space_tag_t, bus_space_handle_t,
    115 			       int, uint8_t);
    116 static void 	aps_refresh_sensor_data(struct aps_softc *sc);
    117 static void 	aps_refresh(void *);
    118 static void 	aps_power(int, void *);
    119 
    120 CFATTACH_DECL(aps, sizeof(struct aps_softc),
    121 	      aps_match, aps_attach, aps_detach, NULL);
    122 
    123 int
    124 aps_match(struct device *parent, struct cfdata *match, void *aux)
    125 {
    126 	struct isa_attach_args *ia = aux;
    127 	bus_space_tag_t iot = ia->ia_iot;
    128 	bus_space_handle_t ioh;
    129 	int iobase, i;
    130 	uint8_t cr;
    131 
    132 	/* Must supply an address */
    133 	if (ia->ia_nio < 1)
    134 		return 0;
    135 
    136 	if (ISA_DIRECT_CONFIG(ia))
    137 		return 0;
    138 
    139 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    140 		return 0;
    141 
    142 	iobase = ia->ia_io[0].ir_addr;
    143 
    144 	if (bus_space_map(iot, iobase, APS_ADDR_SIZE, 0, &ioh)) {
    145 		aprint_error("aps: can't map i/o space\n");
    146 		return 0;
    147 	}
    148 
    149 	/* See if this machine has APS */
    150 	bus_space_write_1(iot, ioh, APS_INIT, 0x13);
    151 	bus_space_write_1(iot, ioh, APS_CMD, 0x01);
    152 
    153 	/* ask again as the X40 is slightly deaf in one ear */
    154 	bus_space_read_1(iot, ioh, APS_CMD);
    155 	bus_space_write_1(iot, ioh, APS_INIT, 0x13);
    156 	bus_space_write_1(iot, ioh, APS_CMD, 0x01);
    157 
    158 	if (!aps_mem_read_1(iot, ioh, APS_CMD, 0x00)) {
    159 		bus_space_unmap(iot, ioh, APS_ADDR_SIZE);
    160 		return 0;
    161 	}
    162 
    163 	/*
    164 	 * Observed values from Linux driver:
    165 	 * 0x01: T42
    166 	 * 0x02: chip already initialised
    167 	 * 0x03: T41
    168 	 */
    169 	for (i = 0; i < 10; i++) {
    170 		cr = bus_space_read_1(iot, ioh, APS_STATE);
    171 		if (cr > 0 && cr < 6)
    172 			break;
    173 		delay(5 * 1000);
    174 	}
    175 
    176 	bus_space_unmap(iot, ioh, APS_ADDR_SIZE);
    177 	DPRINTF(("aps: state register 0x%x\n", cr));
    178 	if (cr < 1 || cr > 5) {
    179 		DPRINTF(("aps0: unsupported state %d\n", cr));
    180 		return 0;
    181 	}
    182 
    183 	ia->ia_nio = 1;
    184 	ia->ia_io[0].ir_size = APS_ADDR_SIZE;
    185 	ia->ia_niomem = 0;
    186 	ia->ia_nirq = 0;
    187 	ia->ia_ndrq = 0;
    188 
    189 	return 1;
    190 }
    191 
    192 void
    193 aps_attach(struct device *parent, struct device *self, void *aux)
    194 {
    195 	struct aps_softc *sc = (void *)self;
    196 	struct isa_attach_args *ia = aux;
    197 	int iobase, i;
    198 
    199 	sc->sc_iot = ia->ia_iot;
    200 	iobase = ia->ia_io[0].ir_addr;
    201 
    202 	if (bus_space_map(sc->sc_iot, iobase, APS_ADDR_SIZE, 0, &sc->sc_ioh)) {
    203 		aprint_error(": can't map i/o space\n");
    204 		return;
    205 	}
    206 
    207 	aprint_naive("\n");
    208 	aprint_normal("\n");
    209 
    210 	if (!aps_init(sc)) {
    211 		aprint_error("%s: failed to initialise\n",
    212 		    device_xname(&sc->sc_dev));
    213 		return;
    214 	}
    215 
    216 	/* Initialize sensors */
    217 #define INITDATA(idx, unit, string)					\
    218 	sc->sc_sensor[idx].units = unit;					\
    219 	snprintf(sc->sc_sensor[idx].desc, sizeof(sc->sc_sensor[idx].desc),	\
    220 	    "%s %s", sc->sc_dev.dv_xname, string);
    221 
    222 	INITDATA(APS_SENSOR_XACCEL, ENVSYS_INTEGER, "X_ACCEL");
    223 	INITDATA(APS_SENSOR_YACCEL, ENVSYS_INTEGER, "Y_ACCEL");
    224 	INITDATA(APS_SENSOR_TEMP1, ENVSYS_STEMP, "TEMP_1");
    225 	INITDATA(APS_SENSOR_TEMP2, ENVSYS_STEMP, "TEMP_2");
    226 	INITDATA(APS_SENSOR_XVAR, ENVSYS_INTEGER, "X_VAR");
    227 	INITDATA(APS_SENSOR_YVAR, ENVSYS_INTEGER, "Y_VAR");
    228 	INITDATA(APS_SENSOR_KBACT, ENVSYS_INDICATOR, "Keyboard Active");
    229 	INITDATA(APS_SENSOR_MSACT, ENVSYS_INDICATOR, "Mouse Active");
    230 	INITDATA(APS_SENSOR_LIDOPEN, ENVSYS_INDICATOR, "Lid Open");
    231 
    232 	sc->sc_sme = sysmon_envsys_create();
    233 	for (i = 0; i < APS_NUM_SENSORS; i++) {
    234 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    235 						&sc->sc_sensor[i])) {
    236 			sysmon_envsys_destroy(sc->sc_sme);
    237 			return;
    238 		}
    239 	}
    240         /*
    241          * Register with the sysmon_envsys(9) framework.
    242          */
    243 	sc->sc_sme->sme_name = sc->sc_dev.dv_xname;
    244 	sc->sc_sme->sme_flags |= SME_DISABLE_REFRESH;
    245 
    246 	if ((i = sysmon_envsys_register(sc->sc_sme))) {
    247 		aprint_error("%s: unable to register with sysmon (%d)\n",
    248 		    device_xname(&sc->sc_dev), i);
    249 		sysmon_envsys_destroy(sc->sc_sme);
    250 		return;
    251 	}
    252 
    253 	sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
    254 					       aps_power,
    255 					       sc);
    256 	if (sc->sc_powerhook == NULL)
    257 		aprint_error("%s: can't establish powerhook\n",
    258 		    device_xname(&sc->sc_dev));
    259 
    260 	/* Refresh sensor data every 0.5 seconds */
    261 	callout_init(&sc->sc_callout, 0);
    262 	callout_setfunc(&sc->sc_callout, aps_refresh, sc);
    263 	callout_schedule(&sc->sc_callout, (hz) / 2);
    264 
    265         aprint_normal("%s: Thinkpad Active Protection System\n",
    266 	    device_xname(&sc->sc_dev));
    267 }
    268 
    269 static int
    270 aps_init(struct aps_softc *sc)
    271 {
    272 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_INIT, 0x17);
    273 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_STATE, 0x81);
    274 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x01);
    275 	if (!aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x00))
    276 		return 0;
    277 	if (!aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_STATE, 0x00))
    278 		return 0;
    279 	if (!aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_XACCEL, 0x60))
    280 		return 0;
    281 	if (!aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_XACCEL + 1, 0x00))
    282 		return 0;
    283 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_INIT, 0x14);
    284 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_STATE, 0x01);
    285 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x01);
    286 	if (!aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x00))
    287 		return 0;
    288 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_INIT, 0x10);
    289 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_STATE, 0xc8);
    290 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_XACCEL, 0x00);
    291 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_XACCEL + 1, 0x02);
    292 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x01);
    293 	if (!aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x00))
    294 		return 0;
    295 	/* refresh data */
    296 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_INIT, 0x11);
    297 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x01);
    298 	if (!aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_ACCEL_STATE, 0x50))
    299 		return 0;
    300 	if (!aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_STATE, 0x00))
    301 		return 0;
    302 
    303 	return 1;
    304 }
    305 
    306 static int
    307 aps_detach(struct device *self, int flags)
    308 {
    309 	struct aps_softc *sc = device_private(self);
    310 
    311         callout_stop(&sc->sc_callout);
    312         callout_destroy(&sc->sc_callout);
    313 	sysmon_envsys_unregister(sc->sc_sme);
    314 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, APS_ADDR_SIZE);
    315 
    316 	return 0;
    317 }
    318 
    319 static uint8_t
    320 aps_mem_read_1(bus_space_tag_t iot, bus_space_handle_t ioh, int reg,
    321 	       uint8_t val)
    322 {
    323 	int i;
    324 	uint8_t cr;
    325 	/* should take no longer than 50 microseconds */
    326 	for (i = 0; i < 10; i++) {
    327 		cr = bus_space_read_1(iot, ioh, reg);
    328 		if (cr == val)
    329 			return 1;
    330 		delay(5 * 1000);
    331 	}
    332 
    333 	DPRINTF(("aps: reg 0x%x not val 0x%x!\n", reg, val));
    334 	return 0;
    335 }
    336 
    337 static void
    338 aps_refresh_sensor_data(struct aps_softc *sc)
    339 {
    340 	int64_t temp;
    341 
    342 	/* ask for new data */
    343 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_INIT, 0x11);
    344 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x01);
    345 	if (!aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_ACCEL_STATE, 0x50))
    346 		return;
    347 
    348 	sc->aps_data.state =
    349 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, APS_STATE);
    350 	sc->aps_data.x_accel =
    351 	    bus_space_read_2(sc->sc_iot, sc->sc_ioh, APS_XACCEL);
    352 	sc->aps_data.y_accel =
    353 	    bus_space_read_2(sc->sc_iot, sc->sc_ioh, APS_YACCEL);
    354 	sc->aps_data.temp1 =
    355 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, APS_TEMP);
    356 	sc->aps_data.x_var =
    357 	    bus_space_read_2(sc->sc_iot, sc->sc_ioh, APS_XVAR);
    358 	sc->aps_data.y_var =
    359 	    bus_space_read_2(sc->sc_iot, sc->sc_ioh, APS_YVAR);
    360 	sc->aps_data.temp2 =
    361 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, APS_TEMP2);
    362 	sc->aps_data.input =
    363 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, APS_INPUT);
    364 
    365 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_INIT, 0x11);
    366 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x01);
    367 
    368 	/* tell accelerometer we're done reading from it */
    369 	bus_space_read_1(sc->sc_iot, sc->sc_ioh, APS_CMD);
    370 	bus_space_read_1(sc->sc_iot, sc->sc_ioh, APS_ACCEL_STATE);
    371 
    372 	sc->sc_sensor[APS_SENSOR_XACCEL].value_cur = sc->aps_data.x_accel;
    373 	sc->sc_sensor[APS_SENSOR_YACCEL].value_cur = sc->aps_data.y_accel;
    374 
    375 	/* convert to micro (mu) degrees */
    376 	temp = sc->aps_data.temp1 * 1000000;
    377 	/* convert to kelvin */
    378 	temp += 273150000;
    379 	sc->sc_sensor[APS_SENSOR_TEMP1].value_cur = temp;
    380 
    381 	/* convert to micro (mu) degrees */
    382 	temp = sc->aps_data.temp2 * 1000000;
    383 	/* convert to kelvin */
    384 	temp += 273150000;
    385 	sc->sc_sensor[APS_SENSOR_TEMP2].value_cur = temp;
    386 
    387 	sc->sc_sensor[APS_SENSOR_XVAR].value_cur = sc->aps_data.x_var;
    388 	sc->sc_sensor[APS_SENSOR_YVAR].value_cur = sc->aps_data.y_var;
    389 	sc->sc_sensor[APS_SENSOR_KBACT].value_cur =
    390 	    (sc->aps_data.input &  APS_INPUT_KB) ? 1 : 0;
    391 	sc->sc_sensor[APS_SENSOR_MSACT].value_cur =
    392 	    (sc->aps_data.input & APS_INPUT_MS) ? 1 : 0;
    393 	sc->sc_sensor[APS_SENSOR_LIDOPEN].value_cur =
    394 	    (sc->aps_data.input & APS_INPUT_LIDOPEN) ? 1 : 0;
    395 }
    396 
    397 static void
    398 aps_refresh(void *arg)
    399 {
    400 	struct aps_softc *sc = (struct aps_softc *)arg;
    401 
    402 	aps_refresh_sensor_data(sc);
    403 	callout_schedule(&sc->sc_callout, (hz) / 2);
    404 }
    405 
    406 static void
    407 aps_power(int why, void *arg)
    408 {
    409 	struct aps_softc *sc = (struct aps_softc *)arg;
    410 
    411 	if (why != PWR_RESUME) {
    412 		callout_stop(&sc->sc_callout);
    413 	} else {
    414 		/*
    415 		 * Redo the init sequence on resume, because APS is
    416 		 * as forgetful as it is deaf.
    417 		 */
    418 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_INIT, 0x13);
    419 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x01);
    420 		bus_space_read_1(sc->sc_iot, sc->sc_ioh, APS_CMD);
    421 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_INIT, 0x13);
    422 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x01);
    423 
    424 		if (aps_mem_read_1(sc->sc_iot, sc->sc_ioh, APS_CMD, 0x00) &&
    425 		    aps_init(sc))
    426 			callout_schedule(&sc->sc_callout, (hz) / 2);
    427 		else
    428 			printf("aps: failed to wake up\n");
    429 	}
    430 }
    431