hpacel_acpi.c revision 1.5 1 /* $NetBSD: hpacel_acpi.c,v 1.5 2015/04/23 23:23:00 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2009, 2011 Jukka Ruohonen <jruohonen (at) iki.fi>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: hpacel_acpi.c,v 1.5 2015/04/23 23:23:00 pgoyette Exp $");
31
32 #include <sys/param.h>
33 #include <sys/module.h>
34
35 #include <dev/acpi/acpireg.h>
36 #include <dev/acpi/acpivar.h>
37 #include <dev/acpi/acpi_power.h>
38
39 #include <dev/sysmon/sysmonvar.h>
40
41 #define _COMPONENT ACPI_RESOURCE_COMPONENT
42 ACPI_MODULE_NAME ("hpacel_acpi")
43
44 /*
45 * An ACPI driver for Hewlett-Packard 3D DriveGuard accelerometer.
46 *
47 * The supported chipset is LIS3LV02DL from STMicroelectronics:
48 *
49 * http://www.st.com/stonline/products/literature/anp/12441.pdf
50 *
51 * (Obtained on Sat Apr 25 00:32:04 EEST 2009.)
52 *
53 * The chip is a three axes digital output linear accelerometer
54 * that is controllable through I2C / SPI serial interface. This
55 * implementation however supports only indirect connection through
56 * ACPI. Other chips from the same family, such as LIS3LV02DQ, may
57 * also work with the driver, provided that there is a suitable DSDT.
58 *
59 * The chip can generate wake-up, direction detection and free-fall
60 * interrupts. The latter could be used to evoke emergency action.
61 * None of this is however supported. Only sysmon_envsys(9) is used.
62 */
63 enum {
64 HPACEL_SENSOR_X = 0,
65 HPACEL_SENSOR_Y,
66 HPACEL_SENSOR_Z,
67 HPACEL_SENSOR_COUNT
68 };
69
70 #define LIS3LV02DL_ID 0x3A
71
72 enum lis3lv02dl_reg {
73 WHO_AM_I = 0x0F, /* r */
74 OFFSET_X = 0x16, /* rw */
75 OFFSET_Y = 0x17, /* rw */
76 OFFSET_Z = 0x18, /* rw */
77 GAIN_X = 0x19, /* rw */
78 GAIN_Y = 0x1A, /* rw */
79 GAIN_Z = 0x1B, /* rw */
80 CTRL_REG1 = 0x20, /* rw */
81 CTRL_REG2 = 0x21, /* rw */
82 CTRL_REG3 = 0x22, /* rw */
83 HP_FILTER_RESET = 0x23, /* r */
84 STATUS_REG = 0x27, /* rw */
85 OUTX_L = 0x28, /* r */
86 OUTX_H = 0x29, /* r */
87 OUTY_L = 0x2A, /* r */
88 OUTY_H = 0x2B, /* r */
89 OUTZ_L = 0x2C, /* r */
90 OUTZ_H = 0x2D, /* r */
91 FF_WU_CFG = 0x30, /* r */
92 FF_WU_SRC = 0x31, /* rw */
93 FF_WU_ACK = 0x32, /* r */
94 FF_WU_THS_L = 0x34, /* rw */
95 FF_WU_THS_H = 0x35, /* rw */
96 FF_WU_DURATION = 0x36, /* rw */
97 DD_CFG = 0x38, /* rw */
98 DD_SRC = 0x39, /* rw */
99 DD_ACK = 0x3A, /* r */
100 DD_THSI_L = 0x3C, /* rw */
101 DD_THSI_H = 0x3D, /* rw */
102 DD_THSE_L = 0x3E, /* rw */
103 DD_THSE_H = 0x3F /* rw */
104 };
105
106 enum lis3lv02dl_ctrl1 {
107 CTRL1_Xen = (1 << 0), /* X-axis enable */
108 CTRL1_Yen = (1 << 1), /* Y-axis enable */
109 CTRL1_Zen = (1 << 2), /* Z-axis enable */
110 CTRL1_ST = (1 << 3), /* Self test enable */
111 CTRL1_DF0 = (1 << 4), /* Decimation factor control */
112 CTRL1_DF1 = (1 << 5), /* Decimation factor control */
113 CTRL1_PD0 = (1 << 6), /* Power down control */
114 CTRL1_PD1 = (1 << 7) /* Power down control */
115 };
116
117 enum lis3lv02dl_ctrl2 {
118 CTRL2_DAS = (1 << 0), /* Data alignment selection */
119 CTRL2_SIM = (1 << 1), /* SPI serial interface mode */
120 CTRL2_DRDY = (1 << 2), /* Enable data-ready generation */
121 CTRL2_IEN = (1 << 3), /* Enable interrupt mode */
122 CTRL2_BOOT = (1 << 4), /* Reboot memory contents */
123 CTRL2_BLE = (1 << 5), /* Endian mode */
124 CTRL2_BDU = (1 << 6), /* Block data update */
125 CTRL2_FS = (1 << 7) /* Full scale selection */
126 };
127
128 enum lis3lv02dl_ctrl3 {
129 CTRL3_CFS0 = (1 << 0), /* High-pass filter cut-off frequency */
130 CTRL3_CFS1 = (1 << 1), /* High-pass filter cut-off frequency */
131 CTRL3_FDS = (1 << 4), /* Filtered data selection */
132 CTRL3_HPFF = (1 << 5), /* High pass filter for free-fall */
133 CTRL3_HPDD = (1 << 6), /* High pass filter for DD */
134 CTRL3_ECK = (1 << 7) /* External clock */
135 };
136
137 struct hpacel_softc {
138 device_t sc_dev;
139 struct acpi_devnode *sc_node;
140 struct sysmon_envsys *sc_sme;
141 bool sc_state;
142 uint8_t sc_whoami;
143 uint8_t sc_ctrl[3];
144 envsys_data_t sc_sensor[HPACEL_SENSOR_COUNT];
145 };
146
147 const char * const hpacel_ids[] = {
148 "HPQ0004",
149 NULL
150 };
151
152 static int hpacel_match(device_t, cfdata_t, void *);
153 static void hpacel_attach(device_t, device_t, void *);
154 static int hpacel_detach(device_t, int);
155 static bool hpacel_reg_init(device_t);
156 static bool hpacel_suspend(device_t, const pmf_qual_t *);
157 static bool hpacel_resume(device_t, const pmf_qual_t *);
158 static ACPI_STATUS hpacel_reg_info(device_t);
159 static ACPI_STATUS hpacel_reg_read(ACPI_HANDLE, ACPI_INTEGER, uint8_t *);
160 static ACPI_STATUS hpacel_reg_write(ACPI_HANDLE, ACPI_INTEGER, uint8_t);
161 static ACPI_STATUS hpacel_reg_xyz(ACPI_HANDLE, const int, int16_t *);
162 static ACPI_STATUS hpacel_power(device_t, bool);
163 static bool hpacel_sensor_init(device_t);
164 static void hpacel_sensor_refresh(struct sysmon_envsys *,
165 envsys_data_t *);
166
167 CFATTACH_DECL_NEW(hpacel, sizeof(struct hpacel_softc),
168 hpacel_match, hpacel_attach, hpacel_detach, NULL);
169
170 static int
171 hpacel_match(device_t parent, cfdata_t match, void *aux)
172 {
173 struct acpi_attach_args *aa = aux;
174
175 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
176 return 0;
177
178 return acpi_match_hid(aa->aa_node->ad_devinfo, hpacel_ids);
179 }
180
181 static void
182 hpacel_attach(device_t parent, device_t self, void *aux)
183 {
184 struct hpacel_softc *sc = device_private(self);
185 struct acpi_attach_args *aa = aux;
186
187 sc->sc_sme = NULL;
188 sc->sc_dev = self;
189 sc->sc_state = false;
190 sc->sc_node = aa->aa_node;
191
192 aprint_naive("\n");
193 aprint_normal(": HP 3D DriveGuard accelerometer\n");
194
195 if (hpacel_reg_init(self) != true)
196 return;
197
198 (void)pmf_device_register(self, hpacel_suspend, hpacel_resume);
199
200 if (hpacel_sensor_init(self) != false)
201 (void)hpacel_power(self, true);
202
203 sc->sc_state = true;
204 }
205
206 static int
207 hpacel_detach(device_t self, int flags)
208 {
209 struct hpacel_softc *sc = device_private(self);
210
211 if (sc->sc_state != false)
212 (void)hpacel_power(self, false);
213
214 if (sc->sc_sme != NULL)
215 sysmon_envsys_unregister(sc->sc_sme);
216
217 return 0;
218 }
219
220 static bool
221 hpacel_suspend(device_t self, const pmf_qual_t *qual)
222 {
223 struct hpacel_softc *sc = device_private(self);
224
225 if (sc->sc_state != false)
226 (void)hpacel_power(self, false);
227
228 return true;
229 }
230
231 static bool
232 hpacel_resume(device_t self, const pmf_qual_t *qual)
233 {
234 struct hpacel_softc *sc = device_private(self);
235
236 if (sc->sc_state != false)
237 (void)hpacel_power(self, true);
238
239 return true;
240 }
241
242 static bool
243 hpacel_reg_init(device_t self)
244 {
245 struct hpacel_softc *sc = device_private(self);
246 ACPI_HANDLE hdl = sc->sc_node->ad_handle;
247 ACPI_STATUS rv;
248 uint8_t val;
249
250 rv = AcpiEvaluateObject(hdl, "_INI", NULL, NULL);
251
252 if (ACPI_FAILURE(rv))
253 goto out;
254
255 /*
256 * Since the "_INI" is practically
257 * a black box, it is better to verify
258 * the control registers manually.
259 */
260 rv = hpacel_reg_info(self);
261
262 if (ACPI_FAILURE(rv))
263 goto out;
264
265 val = sc->sc_ctrl[0];
266
267 if ((sc->sc_ctrl[0] & CTRL1_Xen) == 0)
268 val |= CTRL1_Xen;
269
270 if ((sc->sc_ctrl[0] & CTRL1_Yen) == 0)
271 val |= CTRL1_Yen;
272
273 if ((sc->sc_ctrl[0] & CTRL1_Zen) == 0)
274 val |= CTRL1_Zen;
275
276 if (val != sc->sc_ctrl[0]) {
277
278 rv = hpacel_reg_write(hdl, CTRL_REG1, val);
279
280 if (ACPI_FAILURE(rv))
281 return rv;
282 }
283
284 val = sc->sc_ctrl[1];
285
286 if ((sc->sc_ctrl[1] & CTRL2_BDU) == 0)
287 val |= CTRL2_BDU;
288
289 if ((sc->sc_ctrl[1] & CTRL2_BLE) != 0)
290 val &= ~CTRL2_BLE;
291
292 if ((sc->sc_ctrl[1] & CTRL2_DAS) != 0)
293 val &= ~CTRL2_DAS;
294
295 /*
296 * Given the use of sysmon_envsys(9),
297 * there is no need for the data-ready pin.
298 */
299 if ((sc->sc_ctrl[1] & CTRL2_DRDY) != 0)
300 val &= ~CTRL2_DRDY;
301
302 /*
303 * Disable interrupt mode.
304 */
305 if ((sc->sc_ctrl[1] & CTRL2_IEN) != 0)
306 val &= ~CTRL2_IEN;
307
308 if (val != sc->sc_ctrl[1]) {
309
310 rv = hpacel_reg_write(hdl, CTRL_REG2, val);
311
312 if (ACPI_FAILURE(rv))
313 return rv;
314 }
315
316 /*
317 * Clear possible interrupt setups from
318 * the direction-detection register and
319 * from the free-fall-wake-up register.
320 */
321 (void)hpacel_reg_write(hdl, DD_CFG, 0x00);
322 (void)hpacel_reg_write(hdl, FF_WU_CFG, 0x00);
323
324 /*
325 * Update the register information.
326 */
327 (void)hpacel_reg_info(self);
328
329 out:
330 if (ACPI_FAILURE(rv))
331 aprint_error_dev(self, "failed to initialize "
332 "device: %s\n", AcpiFormatException(rv));
333
334 return (rv != AE_OK) ? false : true;
335 }
336
337 static ACPI_STATUS
338 hpacel_reg_info(device_t self)
339 {
340 struct hpacel_softc *sc = device_private(self);
341 ACPI_HANDLE hdl = sc->sc_node->ad_handle;
342 ACPI_STATUS rv;
343 size_t i;
344
345 rv = hpacel_reg_read(hdl, WHO_AM_I, &sc->sc_whoami);
346
347 if (ACPI_FAILURE(rv))
348 return rv;
349
350 for (i = 0; i < __arraycount(sc->sc_sensor); i++) {
351
352 rv = hpacel_reg_read(hdl, CTRL_REG1 + i, &sc->sc_ctrl[i]);
353
354 if (ACPI_FAILURE(rv))
355 return rv;
356 }
357
358 return AE_OK;
359 }
360
361 static ACPI_STATUS
362 hpacel_reg_read(ACPI_HANDLE hdl, ACPI_INTEGER reg, uint8_t *valp)
363 {
364 ACPI_OBJECT_LIST arg;
365 ACPI_OBJECT obj, val;
366 ACPI_BUFFER buf;
367 ACPI_STATUS rv;
368
369 obj.Type = ACPI_TYPE_INTEGER;
370 obj.Integer.Value = reg;
371
372 buf.Pointer = &val;
373 buf.Length = sizeof(val);
374
375 arg.Count = 1;
376 arg.Pointer = &obj;
377
378 rv = AcpiEvaluateObjectTyped(hdl, "ALRD",
379 &arg, &buf, ACPI_TYPE_INTEGER);
380
381 if (ACPI_FAILURE(rv))
382 return rv;
383
384 if (val.Integer.Value > UINT8_MAX)
385 return AE_AML_NUMERIC_OVERFLOW;
386
387 *valp = val.Integer.Value;
388
389 return AE_OK;
390 }
391
392 static ACPI_STATUS
393 hpacel_reg_write(ACPI_HANDLE hdl, ACPI_INTEGER reg, uint8_t val)
394 {
395 ACPI_OBJECT_LIST arg;
396 ACPI_OBJECT obj[2];
397
398 obj[0].Type = obj[1].Type = ACPI_TYPE_INTEGER;
399
400 obj[0].Integer.Value = reg;
401 obj[1].Integer.Value = val;
402
403 arg.Count = 2;
404 arg.Pointer = obj;
405
406 return AcpiEvaluateObject(hdl, "ALWR", &arg, NULL);
407 }
408
409 static ACPI_STATUS
410 hpacel_reg_xyz(ACPI_HANDLE hdl, const int xyz, int16_t *out)
411 {
412 ACPI_INTEGER reg[2];
413 ACPI_STATUS rv[2];
414 uint8_t hi, lo;
415
416 switch (xyz) {
417
418 case HPACEL_SENSOR_X:
419 reg[0] = OUTX_L;
420 reg[1] = OUTX_H;
421 break;
422
423 case HPACEL_SENSOR_Y:
424 reg[0] = OUTY_L;
425 reg[1] = OUTY_H;
426 break;
427
428 case HPACEL_SENSOR_Z:
429 reg[0] = OUTZ_L;
430 reg[1] = OUTZ_H;
431 break;
432
433 default:
434 return AE_BAD_PARAMETER;
435 }
436
437 rv[0] = hpacel_reg_read(hdl, reg[0], &lo);
438 rv[1] = hpacel_reg_read(hdl, reg[1], &hi);
439
440 if (ACPI_FAILURE(rv[0]) || ACPI_FAILURE(rv[1]))
441 return AE_ERROR;
442
443 /*
444 * These registers are read in "12 bit right
445 * justified mode", meaning that the four
446 * most significant bits are replaced with
447 * the value of bit 12. Note the signed type.
448 */
449 hi = (hi & 0x10) ? hi | 0xE0 : hi & ~0xE0;
450
451 *out = (hi << 8) | lo;
452
453 return AE_OK;
454 }
455
456 static ACPI_STATUS
457 hpacel_power(device_t self, bool enable)
458 {
459 struct hpacel_softc *sc = device_private(self);
460 ACPI_HANDLE hdl = sc->sc_node->ad_handle;
461 ACPI_OBJECT_LIST arg;
462 ACPI_OBJECT obj;
463 ACPI_STATUS rv;
464 uint8_t val;
465
466 rv = hpacel_reg_info(self);
467
468 if (ACPI_FAILURE(rv))
469 return rv;
470
471 val = sc->sc_ctrl[0];
472
473 if (enable != false)
474 val |= CTRL1_PD0 | CTRL1_PD1;
475 else {
476 val &= ~(CTRL1_PD0 | CTRL1_PD1);
477 }
478
479 if (val != sc->sc_ctrl[0]) {
480
481 rv = hpacel_reg_write(hdl, CTRL_REG1, val);
482
483 if (ACPI_FAILURE(rv))
484 return rv;
485 }
486
487 obj.Type = ACPI_TYPE_INTEGER;
488 obj.Integer.Value = enable;
489
490 arg.Count = 1;
491 arg.Pointer = &obj;
492
493 /*
494 * This should turn on/off a led, if available.
495 */
496 (void)AcpiEvaluateObject(hdl, "ALED", &arg, NULL);
497
498 return rv;
499 }
500
501 static bool
502 hpacel_sensor_init(device_t self)
503 {
504 const char zyx[HPACEL_SENSOR_COUNT] = { 'x', 'y', 'z' };
505 struct hpacel_softc *sc = device_private(self);
506 size_t i;
507 int rv;
508
509 CTASSERT(HPACEL_SENSOR_X == 0);
510 CTASSERT(HPACEL_SENSOR_Y == 1);
511 CTASSERT(HPACEL_SENSOR_Z == 2);
512
513 sc->sc_sme = sysmon_envsys_create();
514
515 for (i = 0; i < __arraycount(sc->sc_sensor); i++) {
516
517 sc->sc_sensor[i].units = ENVSYS_INTEGER;
518 sc->sc_sensor[i].state = ENVSYS_SINVALID;
519 sc->sc_sensor[i].flags = ENVSYS_FHAS_ENTROPY;
520
521 (void)snprintf(sc->sc_sensor[i].desc,
522 ENVSYS_DESCLEN, "%c-acceleration", zyx[i]);
523
524 rv = sysmon_envsys_sensor_attach(sc->sc_sme,&sc->sc_sensor[i]);
525
526 if (rv != 0)
527 goto fail;
528 }
529
530 /*
531 * We only do polling, given the hopelessly
532 * slow way of reading registers with ACPI.
533 */
534 sc->sc_sme->sme_cookie = sc;
535 sc->sc_sme->sme_flags = SME_POLL_ONLY;
536 sc->sc_sme->sme_name = device_xname(self);
537 sc->sc_sme->sme_refresh = hpacel_sensor_refresh;
538
539 rv = sysmon_envsys_register(sc->sc_sme);
540
541 if (rv != 0)
542 goto fail;
543
544 return true;
545
546 fail:
547 aprint_error_dev(self, "failed to initialize sensors\n");
548
549 sysmon_envsys_destroy(sc->sc_sme);
550 sc->sc_sme = NULL;
551
552 return false;
553 }
554
555 static void
556 hpacel_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
557 {
558 struct hpacel_softc *sc = sme->sme_cookie;
559 ACPI_STATUS rv;
560 int16_t val;
561 size_t i;
562
563 for (i = 0; i < __arraycount(sc->sc_sensor); i++) {
564
565 rv = hpacel_reg_xyz(sc->sc_node->ad_handle, i, &val);
566
567 if (ACPI_SUCCESS(rv)) {
568 sc->sc_sensor[i].value_cur = val;
569 sc->sc_sensor[i].state = ENVSYS_SVALID;
570 continue;
571 }
572
573 sc->sc_sensor[i].state = ENVSYS_SINVALID;
574 }
575 }
576
577 MODULE(MODULE_CLASS_DRIVER, hpacel, "sysmon_envsys");
578
579 #ifdef _MODULE
580 #include "ioconf.c"
581 #endif
582
583 static int
584 hpacel_modcmd(modcmd_t cmd, void *aux)
585 {
586 int rv = 0;
587
588 switch (cmd) {
589
590 case MODULE_CMD_INIT:
591
592 #ifdef _MODULE
593 rv = config_init_component(cfdriver_ioconf_hpacel,
594 cfattach_ioconf_hpacel, cfdata_ioconf_hpacel);
595 #endif
596 break;
597
598 case MODULE_CMD_FINI:
599
600 #ifdef _MODULE
601 rv = config_fini_component(cfdriver_ioconf_hpacel,
602 cfattach_ioconf_hpacel, cfdata_ioconf_hpacel);
603 #endif
604 break;
605
606 default:
607 rv = ENOTTY;
608 }
609
610 return rv;
611 }
612