This source file includes following definitions.
- st_accel_read_raw
- st_accel_write_raw
- get_mount_matrix
- apply_acpi_orientation
- apply_acpi_orientation
- st_accel_get_settings
- st_accel_common_probe
- st_accel_common_remove
1
2
3
4
5
6
7
8
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/acpi.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/gpio.h>
19 #include <linux/irq.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/buffer.h>
24
25 #include <linux/iio/common/st_sensors.h>
26 #include "st_accel.h"
27
28 #define ST_ACCEL_NUMBER_DATA_CHANNELS 3
29
30
31 #define ST_ACCEL_DEFAULT_OUT_X_L_ADDR 0x28
32 #define ST_ACCEL_DEFAULT_OUT_Y_L_ADDR 0x2a
33 #define ST_ACCEL_DEFAULT_OUT_Z_L_ADDR 0x2c
34
35
36 #define ST_ACCEL_FS_AVL_2G 2
37 #define ST_ACCEL_FS_AVL_4G 4
38 #define ST_ACCEL_FS_AVL_6G 6
39 #define ST_ACCEL_FS_AVL_8G 8
40 #define ST_ACCEL_FS_AVL_16G 16
41 #define ST_ACCEL_FS_AVL_100G 100
42 #define ST_ACCEL_FS_AVL_200G 200
43 #define ST_ACCEL_FS_AVL_400G 400
44
45 static const struct iio_chan_spec st_accel_8bit_channels[] = {
46 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
47 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
48 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 8, 8,
49 ST_ACCEL_DEFAULT_OUT_X_L_ADDR+1),
50 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
51 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
52 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 8, 8,
53 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR+1),
54 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
55 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
56 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 8, 8,
57 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR+1),
58 IIO_CHAN_SOFT_TIMESTAMP(3)
59 };
60
61 static const struct iio_chan_spec st_accel_12bit_channels[] = {
62 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
63 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
64 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16,
65 ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
66 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
67 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
68 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 12, 16,
69 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
70 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
71 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
72 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 12, 16,
73 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
74 IIO_CHAN_SOFT_TIMESTAMP(3)
75 };
76
77 static const struct iio_chan_spec st_accel_16bit_channels[] = {
78 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
79 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
80 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
81 ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
82 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
83 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
84 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
85 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
86 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
87 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
88 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
89 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
90 IIO_CHAN_SOFT_TIMESTAMP(3)
91 };
92
93 static const struct st_sensor_settings st_accel_sensors_settings[] = {
94 {
95 .wai = 0x33,
96 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
97 .sensors_supported = {
98 [0] = LIS3DH_ACCEL_DEV_NAME,
99 [1] = LSM303DLHC_ACCEL_DEV_NAME,
100 [2] = LSM330D_ACCEL_DEV_NAME,
101 [3] = LSM330DL_ACCEL_DEV_NAME,
102 [4] = LSM330DLC_ACCEL_DEV_NAME,
103 [5] = LSM303AGR_ACCEL_DEV_NAME,
104 [6] = LIS2DH12_ACCEL_DEV_NAME,
105 [7] = LIS3DE_ACCEL_DEV_NAME,
106 },
107 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
108 .odr = {
109 .addr = 0x20,
110 .mask = 0xf0,
111 .odr_avl = {
112 { .hz = 1, .value = 0x01, },
113 { .hz = 10, .value = 0x02, },
114 { .hz = 25, .value = 0x03, },
115 { .hz = 50, .value = 0x04, },
116 { .hz = 100, .value = 0x05, },
117 { .hz = 200, .value = 0x06, },
118 { .hz = 400, .value = 0x07, },
119 { .hz = 1600, .value = 0x08, },
120 },
121 },
122 .pw = {
123 .addr = 0x20,
124 .mask = 0xf0,
125 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
126 },
127 .enable_axis = {
128 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
129 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
130 },
131 .fs = {
132 .addr = 0x23,
133 .mask = 0x30,
134 .fs_avl = {
135 [0] = {
136 .num = ST_ACCEL_FS_AVL_2G,
137 .value = 0x00,
138 .gain = IIO_G_TO_M_S_2(1000),
139 },
140 [1] = {
141 .num = ST_ACCEL_FS_AVL_4G,
142 .value = 0x01,
143 .gain = IIO_G_TO_M_S_2(2000),
144 },
145 [2] = {
146 .num = ST_ACCEL_FS_AVL_8G,
147 .value = 0x02,
148 .gain = IIO_G_TO_M_S_2(4000),
149 },
150 [3] = {
151 .num = ST_ACCEL_FS_AVL_16G,
152 .value = 0x03,
153 .gain = IIO_G_TO_M_S_2(12000),
154 },
155 },
156 },
157 .bdu = {
158 .addr = 0x23,
159 .mask = 0x80,
160 },
161 .drdy_irq = {
162 .int1 = {
163 .addr = 0x22,
164 .mask = 0x10,
165 },
166 .addr_ihl = 0x25,
167 .mask_ihl = 0x02,
168 .stat_drdy = {
169 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
170 .mask = 0x07,
171 },
172 },
173 .sim = {
174 .addr = 0x23,
175 .value = BIT(0),
176 },
177 .multi_read_bit = true,
178 .bootime = 2,
179 },
180 {
181 .wai = 0x32,
182 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
183 .sensors_supported = {
184 [0] = LIS331DLH_ACCEL_DEV_NAME,
185 [1] = LSM303DL_ACCEL_DEV_NAME,
186 [2] = LSM303DLH_ACCEL_DEV_NAME,
187 [3] = LSM303DLM_ACCEL_DEV_NAME,
188 },
189 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
190 .odr = {
191 .addr = 0x20,
192 .mask = 0x18,
193 .odr_avl = {
194 { .hz = 50, .value = 0x00, },
195 { .hz = 100, .value = 0x01, },
196 { .hz = 400, .value = 0x02, },
197 { .hz = 1000, .value = 0x03, },
198 },
199 },
200 .pw = {
201 .addr = 0x20,
202 .mask = 0xe0,
203 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
204 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
205 },
206 .enable_axis = {
207 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
208 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
209 },
210 .fs = {
211 .addr = 0x23,
212 .mask = 0x30,
213 .fs_avl = {
214 [0] = {
215 .num = ST_ACCEL_FS_AVL_2G,
216 .value = 0x00,
217 .gain = IIO_G_TO_M_S_2(1000),
218 },
219 [1] = {
220 .num = ST_ACCEL_FS_AVL_4G,
221 .value = 0x01,
222 .gain = IIO_G_TO_M_S_2(2000),
223 },
224 [2] = {
225 .num = ST_ACCEL_FS_AVL_8G,
226 .value = 0x03,
227 .gain = IIO_G_TO_M_S_2(3900),
228 },
229 },
230 },
231 .bdu = {
232 .addr = 0x23,
233 .mask = 0x80,
234 },
235 .drdy_irq = {
236 .int1 = {
237 .addr = 0x22,
238 .mask = 0x02,
239 .addr_od = 0x22,
240 .mask_od = 0x40,
241 },
242 .int2 = {
243 .addr = 0x22,
244 .mask = 0x10,
245 .addr_od = 0x22,
246 .mask_od = 0x40,
247 },
248 .addr_ihl = 0x22,
249 .mask_ihl = 0x80,
250 .stat_drdy = {
251 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
252 .mask = 0x07,
253 },
254 },
255 .sim = {
256 .addr = 0x23,
257 .value = BIT(0),
258 },
259 .multi_read_bit = true,
260 .bootime = 2,
261 },
262 {
263 .wai = 0x40,
264 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
265 .sensors_supported = {
266 [0] = LSM330_ACCEL_DEV_NAME,
267 },
268 .ch = (struct iio_chan_spec *)st_accel_16bit_channels,
269 .odr = {
270 .addr = 0x20,
271 .mask = 0xf0,
272 .odr_avl = {
273 { .hz = 3, .value = 0x01, },
274 { .hz = 6, .value = 0x02, },
275 { .hz = 12, .value = 0x03, },
276 { .hz = 25, .value = 0x04, },
277 { .hz = 50, .value = 0x05, },
278 { .hz = 100, .value = 0x06, },
279 { .hz = 200, .value = 0x07, },
280 { .hz = 400, .value = 0x08, },
281 { .hz = 800, .value = 0x09, },
282 { .hz = 1600, .value = 0x0a, },
283 },
284 },
285 .pw = {
286 .addr = 0x20,
287 .mask = 0xf0,
288 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
289 },
290 .enable_axis = {
291 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
292 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
293 },
294 .fs = {
295 .addr = 0x24,
296 .mask = 0x38,
297 .fs_avl = {
298 [0] = {
299 .num = ST_ACCEL_FS_AVL_2G,
300 .value = 0x00,
301 .gain = IIO_G_TO_M_S_2(61),
302 },
303 [1] = {
304 .num = ST_ACCEL_FS_AVL_4G,
305 .value = 0x01,
306 .gain = IIO_G_TO_M_S_2(122),
307 },
308 [2] = {
309 .num = ST_ACCEL_FS_AVL_6G,
310 .value = 0x02,
311 .gain = IIO_G_TO_M_S_2(183),
312 },
313 [3] = {
314 .num = ST_ACCEL_FS_AVL_8G,
315 .value = 0x03,
316 .gain = IIO_G_TO_M_S_2(244),
317 },
318 [4] = {
319 .num = ST_ACCEL_FS_AVL_16G,
320 .value = 0x04,
321 .gain = IIO_G_TO_M_S_2(732),
322 },
323 },
324 },
325 .bdu = {
326 .addr = 0x20,
327 .mask = 0x08,
328 },
329 .drdy_irq = {
330 .int1 = {
331 .addr = 0x23,
332 .mask = 0x80,
333 },
334 .addr_ihl = 0x23,
335 .mask_ihl = 0x40,
336 .stat_drdy = {
337 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
338 .mask = 0x07,
339 },
340 .ig1 = {
341 .en_addr = 0x23,
342 .en_mask = 0x08,
343 },
344 },
345 .sim = {
346 .addr = 0x24,
347 .value = BIT(0),
348 },
349 .multi_read_bit = false,
350 .bootime = 2,
351 },
352 {
353 .wai = 0x3a,
354 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
355 .sensors_supported = {
356 [0] = LIS3LV02DL_ACCEL_DEV_NAME,
357 },
358 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
359 .odr = {
360 .addr = 0x20,
361 .mask = 0x30,
362 .odr_avl = {
363 { .hz = 40, .value = 0x00, },
364 { .hz = 160, .value = 0x01, },
365 { .hz = 640, .value = 0x02, },
366 { .hz = 2560, .value = 0x03, },
367 },
368 },
369 .pw = {
370 .addr = 0x20,
371 .mask = 0xc0,
372 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
373 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
374 },
375 .enable_axis = {
376 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
377 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
378 },
379 .fs = {
380 .addr = 0x21,
381 .mask = 0x80,
382 .fs_avl = {
383 [0] = {
384 .num = ST_ACCEL_FS_AVL_2G,
385 .value = 0x00,
386 .gain = IIO_G_TO_M_S_2(1000),
387 },
388 [1] = {
389 .num = ST_ACCEL_FS_AVL_6G,
390 .value = 0x01,
391 .gain = IIO_G_TO_M_S_2(3000),
392 },
393 },
394 },
395 .bdu = {
396 .addr = 0x21,
397 .mask = 0x40,
398 },
399
400
401
402
403 .das = {
404 .addr = 0x21,
405 .mask = 0x01,
406 },
407 .drdy_irq = {
408 .int1 = {
409 .addr = 0x21,
410 .mask = 0x04,
411 },
412 .stat_drdy = {
413 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
414 .mask = 0x07,
415 },
416 },
417 .sim = {
418 .addr = 0x21,
419 .value = BIT(1),
420 },
421 .multi_read_bit = true,
422 .bootime = 2,
423 },
424 {
425 .wai = 0x3b,
426 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
427 .sensors_supported = {
428 [0] = LIS331DL_ACCEL_DEV_NAME,
429 },
430 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
431 .odr = {
432 .addr = 0x20,
433 .mask = 0x80,
434 .odr_avl = {
435 { .hz = 100, .value = 0x00, },
436 { .hz = 400, .value = 0x01, },
437 },
438 },
439 .pw = {
440 .addr = 0x20,
441 .mask = 0x40,
442 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
443 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
444 },
445 .enable_axis = {
446 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
447 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
448 },
449 .fs = {
450 .addr = 0x20,
451 .mask = 0x20,
452
453
454
455
456 .fs_avl = {
457 [0] = {
458 .num = ST_ACCEL_FS_AVL_2G,
459 .value = 0x00,
460 .gain = IIO_G_TO_M_S_2(18000),
461 },
462 [1] = {
463 .num = ST_ACCEL_FS_AVL_8G,
464 .value = 0x01,
465 .gain = IIO_G_TO_M_S_2(72000),
466 },
467 },
468 },
469 .drdy_irq = {
470 .int1 = {
471 .addr = 0x22,
472 .mask = 0x04,
473 .addr_od = 0x22,
474 .mask_od = 0x40,
475 },
476 .int2 = {
477 .addr = 0x22,
478 .mask = 0x20,
479 .addr_od = 0x22,
480 .mask_od = 0x40,
481 },
482 .addr_ihl = 0x22,
483 .mask_ihl = 0x80,
484 .stat_drdy = {
485 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
486 .mask = 0x07,
487 },
488 },
489 .sim = {
490 .addr = 0x21,
491 .value = BIT(7),
492 },
493 .multi_read_bit = false,
494 .bootime = 2,
495 },
496 {
497 .wai = 0x32,
498 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
499 .sensors_supported = {
500 [0] = H3LIS331DL_ACCEL_DEV_NAME,
501 },
502 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
503 .odr = {
504 .addr = 0x20,
505 .mask = 0x18,
506 .odr_avl = {
507 { .hz = 50, .value = 0x00, },
508 { .hz = 100, .value = 0x01, },
509 { .hz = 400, .value = 0x02, },
510 { .hz = 1000, .value = 0x03, },
511 },
512 },
513 .pw = {
514 .addr = 0x20,
515 .mask = 0x20,
516 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
517 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
518 },
519 .enable_axis = {
520 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
521 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
522 },
523 .fs = {
524 .addr = 0x23,
525 .mask = 0x30,
526 .fs_avl = {
527 [0] = {
528 .num = ST_ACCEL_FS_AVL_100G,
529 .value = 0x00,
530 .gain = IIO_G_TO_M_S_2(49000),
531 },
532 [1] = {
533 .num = ST_ACCEL_FS_AVL_200G,
534 .value = 0x01,
535 .gain = IIO_G_TO_M_S_2(98000),
536 },
537 [2] = {
538 .num = ST_ACCEL_FS_AVL_400G,
539 .value = 0x03,
540 .gain = IIO_G_TO_M_S_2(195000),
541 },
542 },
543 },
544 .bdu = {
545 .addr = 0x23,
546 .mask = 0x80,
547 },
548 .drdy_irq = {
549 .int1 = {
550 .addr = 0x22,
551 .mask = 0x02,
552 },
553 .int2 = {
554 .addr = 0x22,
555 .mask = 0x10,
556 },
557 .addr_ihl = 0x22,
558 .mask_ihl = 0x80,
559 },
560 .sim = {
561 .addr = 0x23,
562 .value = BIT(0),
563 },
564 .multi_read_bit = true,
565 .bootime = 2,
566 },
567 {
568
569 .sensors_supported = {
570 [0] = LIS3L02DQ_ACCEL_DEV_NAME,
571 },
572 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
573 .odr = {
574 .addr = 0x20,
575 .mask = 0x30,
576 .odr_avl = {
577 { .hz = 280, .value = 0x00, },
578 { .hz = 560, .value = 0x01, },
579 { .hz = 1120, .value = 0x02, },
580 { .hz = 4480, .value = 0x03, },
581 },
582 },
583 .pw = {
584 .addr = 0x20,
585 .mask = 0xc0,
586 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
587 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
588 },
589 .enable_axis = {
590 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
591 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
592 },
593 .fs = {
594 .fs_avl = {
595 [0] = {
596 .num = ST_ACCEL_FS_AVL_2G,
597 .gain = IIO_G_TO_M_S_2(488),
598 },
599 },
600 },
601
602
603
604
605 .bdu = {
606 },
607 .drdy_irq = {
608 .int1 = {
609 .addr = 0x21,
610 .mask = 0x04,
611 },
612 .stat_drdy = {
613 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
614 .mask = 0x07,
615 },
616 },
617 .sim = {
618 .addr = 0x21,
619 .value = BIT(1),
620 },
621 .multi_read_bit = false,
622 .bootime = 2,
623 },
624 {
625 .wai = 0x33,
626 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
627 .sensors_supported = {
628 [0] = LNG2DM_ACCEL_DEV_NAME,
629 },
630 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
631 .odr = {
632 .addr = 0x20,
633 .mask = 0xf0,
634 .odr_avl = {
635 { .hz = 1, .value = 0x01, },
636 { .hz = 10, .value = 0x02, },
637 { .hz = 25, .value = 0x03, },
638 { .hz = 50, .value = 0x04, },
639 { .hz = 100, .value = 0x05, },
640 { .hz = 200, .value = 0x06, },
641 { .hz = 400, .value = 0x07, },
642 { .hz = 1600, .value = 0x08, },
643 },
644 },
645 .pw = {
646 .addr = 0x20,
647 .mask = 0xf0,
648 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
649 },
650 .enable_axis = {
651 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
652 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
653 },
654 .fs = {
655 .addr = 0x23,
656 .mask = 0x30,
657 .fs_avl = {
658 [0] = {
659 .num = ST_ACCEL_FS_AVL_2G,
660 .value = 0x00,
661 .gain = IIO_G_TO_M_S_2(15600),
662 },
663 [1] = {
664 .num = ST_ACCEL_FS_AVL_4G,
665 .value = 0x01,
666 .gain = IIO_G_TO_M_S_2(31200),
667 },
668 [2] = {
669 .num = ST_ACCEL_FS_AVL_8G,
670 .value = 0x02,
671 .gain = IIO_G_TO_M_S_2(62500),
672 },
673 [3] = {
674 .num = ST_ACCEL_FS_AVL_16G,
675 .value = 0x03,
676 .gain = IIO_G_TO_M_S_2(187500),
677 },
678 },
679 },
680 .drdy_irq = {
681 .int1 = {
682 .addr = 0x22,
683 .mask = 0x10,
684 },
685 .addr_ihl = 0x25,
686 .mask_ihl = 0x02,
687 .stat_drdy = {
688 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
689 .mask = 0x07,
690 },
691 },
692 .sim = {
693 .addr = 0x23,
694 .value = BIT(0),
695 },
696 .multi_read_bit = true,
697 .bootime = 2,
698 },
699 {
700 .wai = 0x44,
701 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
702 .sensors_supported = {
703 [0] = LIS2DW12_ACCEL_DEV_NAME,
704 },
705 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
706 .odr = {
707 .addr = 0x20,
708 .mask = 0xf0,
709 .odr_avl = {
710 { .hz = 1, .value = 0x01, },
711 { .hz = 12, .value = 0x02, },
712 { .hz = 25, .value = 0x03, },
713 { .hz = 50, .value = 0x04, },
714 { .hz = 100, .value = 0x05, },
715 { .hz = 200, .value = 0x06, },
716 },
717 },
718 .pw = {
719 .addr = 0x20,
720 .mask = 0xf0,
721 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
722 },
723 .fs = {
724 .addr = 0x25,
725 .mask = 0x30,
726 .fs_avl = {
727 [0] = {
728 .num = ST_ACCEL_FS_AVL_2G,
729 .value = 0x00,
730 .gain = IIO_G_TO_M_S_2(976),
731 },
732 [1] = {
733 .num = ST_ACCEL_FS_AVL_4G,
734 .value = 0x01,
735 .gain = IIO_G_TO_M_S_2(1952),
736 },
737 [2] = {
738 .num = ST_ACCEL_FS_AVL_8G,
739 .value = 0x02,
740 .gain = IIO_G_TO_M_S_2(3904),
741 },
742 [3] = {
743 .num = ST_ACCEL_FS_AVL_16G,
744 .value = 0x03,
745 .gain = IIO_G_TO_M_S_2(7808),
746 },
747 },
748 },
749 .bdu = {
750 .addr = 0x21,
751 .mask = 0x08,
752 },
753 .drdy_irq = {
754 .int1 = {
755 .addr = 0x23,
756 .mask = 0x01,
757 .addr_od = 0x22,
758 .mask_od = 0x20,
759 },
760 .int2 = {
761 .addr = 0x24,
762 .mask = 0x01,
763 .addr_od = 0x22,
764 .mask_od = 0x20,
765 },
766 .addr_ihl = 0x22,
767 .mask_ihl = 0x08,
768 .stat_drdy = {
769 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
770 .mask = 0x01,
771 },
772 },
773 .sim = {
774 .addr = 0x21,
775 .value = BIT(0),
776 },
777 .multi_read_bit = false,
778 .bootime = 2,
779 },
780 {
781 .wai = 0x11,
782 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
783 .sensors_supported = {
784 [0] = LIS3DHH_ACCEL_DEV_NAME,
785 },
786 .ch = (struct iio_chan_spec *)st_accel_16bit_channels,
787 .odr = {
788
789 .odr_avl = {
790 { .hz = 1100, .value = 0x00, },
791 },
792 },
793 .pw = {
794 .addr = 0x20,
795 .mask = 0x80,
796 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
797 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
798 },
799 .fs = {
800 .fs_avl = {
801 [0] = {
802 .num = ST_ACCEL_FS_AVL_2G,
803 .gain = IIO_G_TO_M_S_2(76),
804 },
805 },
806 },
807 .bdu = {
808 .addr = 0x20,
809 .mask = 0x01,
810 },
811 .drdy_irq = {
812 .int1 = {
813 .addr = 0x21,
814 .mask = 0x80,
815 .addr_od = 0x23,
816 .mask_od = 0x04,
817 },
818 .int2 = {
819 .addr = 0x22,
820 .mask = 0x80,
821 .addr_od = 0x23,
822 .mask_od = 0x08,
823 },
824 .stat_drdy = {
825 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
826 .mask = 0x07,
827 },
828 },
829 .multi_read_bit = false,
830 .bootime = 2,
831 },
832 {
833 .wai = 0x33,
834 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
835 .sensors_supported = {
836 [0] = LIS2DE12_ACCEL_DEV_NAME,
837 },
838 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
839 .odr = {
840 .addr = 0x20,
841 .mask = 0xf0,
842 .odr_avl = {
843 { .hz = 1, .value = 0x01, },
844 { .hz = 10, .value = 0x02, },
845 { .hz = 25, .value = 0x03, },
846 { .hz = 50, .value = 0x04, },
847 { .hz = 100, .value = 0x05, },
848 { .hz = 200, .value = 0x06, },
849 { .hz = 400, .value = 0x07, },
850 { .hz = 1620, .value = 0x08, },
851 { .hz = 5376, .value = 0x09, },
852 },
853 },
854 .pw = {
855 .addr = 0x20,
856 .mask = 0xf0,
857 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
858 },
859 .enable_axis = {
860 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
861 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
862 },
863 .fs = {
864 .addr = 0x23,
865 .mask = 0x30,
866 .fs_avl = {
867 [0] = {
868 .num = ST_ACCEL_FS_AVL_2G,
869 .value = 0x00,
870 .gain = IIO_G_TO_M_S_2(15600),
871 },
872 [1] = {
873 .num = ST_ACCEL_FS_AVL_4G,
874 .value = 0x01,
875 .gain = IIO_G_TO_M_S_2(31200),
876 },
877 [2] = {
878 .num = ST_ACCEL_FS_AVL_8G,
879 .value = 0x02,
880 .gain = IIO_G_TO_M_S_2(62500),
881 },
882 [3] = {
883 .num = ST_ACCEL_FS_AVL_16G,
884 .value = 0x03,
885 .gain = IIO_G_TO_M_S_2(187500),
886 },
887 },
888 },
889 .drdy_irq = {
890 .int1 = {
891 .addr = 0x22,
892 .mask = 0x10,
893 },
894 .addr_ihl = 0x25,
895 .mask_ihl = 0x02,
896 .stat_drdy = {
897 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
898 .mask = 0x07,
899 },
900 },
901 .sim = {
902 .addr = 0x23,
903 .value = BIT(0),
904 },
905 .multi_read_bit = true,
906 .bootime = 2,
907 },
908 };
909
910 static int st_accel_read_raw(struct iio_dev *indio_dev,
911 struct iio_chan_spec const *ch, int *val,
912 int *val2, long mask)
913 {
914 int err;
915 struct st_sensor_data *adata = iio_priv(indio_dev);
916
917 switch (mask) {
918 case IIO_CHAN_INFO_RAW:
919 err = st_sensors_read_info_raw(indio_dev, ch, val);
920 if (err < 0)
921 goto read_error;
922
923 return IIO_VAL_INT;
924 case IIO_CHAN_INFO_SCALE:
925 *val = adata->current_fullscale->gain / 1000000;
926 *val2 = adata->current_fullscale->gain % 1000000;
927 return IIO_VAL_INT_PLUS_MICRO;
928 case IIO_CHAN_INFO_SAMP_FREQ:
929 *val = adata->odr;
930 return IIO_VAL_INT;
931 default:
932 return -EINVAL;
933 }
934
935 read_error:
936 return err;
937 }
938
939 static int st_accel_write_raw(struct iio_dev *indio_dev,
940 struct iio_chan_spec const *chan, int val, int val2, long mask)
941 {
942 int err;
943
944 switch (mask) {
945 case IIO_CHAN_INFO_SCALE: {
946 int gain;
947
948 gain = val * 1000000 + val2;
949 err = st_sensors_set_fullscale_by_gain(indio_dev, gain);
950 break;
951 }
952 case IIO_CHAN_INFO_SAMP_FREQ:
953 if (val2)
954 return -EINVAL;
955 mutex_lock(&indio_dev->mlock);
956 err = st_sensors_set_odr(indio_dev, val);
957 mutex_unlock(&indio_dev->mlock);
958 return err;
959 default:
960 return -EINVAL;
961 }
962
963 return err;
964 }
965
966 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
967 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
968
969 static struct attribute *st_accel_attributes[] = {
970 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
971 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
972 NULL,
973 };
974
975 static const struct attribute_group st_accel_attribute_group = {
976 .attrs = st_accel_attributes,
977 };
978
979 static const struct iio_info accel_info = {
980 .attrs = &st_accel_attribute_group,
981 .read_raw = &st_accel_read_raw,
982 .write_raw = &st_accel_write_raw,
983 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
984 };
985
986 #ifdef CONFIG_IIO_TRIGGER
987 static const struct iio_trigger_ops st_accel_trigger_ops = {
988 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
989 .validate_device = st_sensors_validate_device,
990 };
991 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
992 #else
993 #define ST_ACCEL_TRIGGER_OPS NULL
994 #endif
995
996 #ifdef CONFIG_ACPI
997 static const struct iio_mount_matrix *
998 get_mount_matrix(const struct iio_dev *indio_dev,
999 const struct iio_chan_spec *chan)
1000 {
1001 struct st_sensor_data *adata = iio_priv(indio_dev);
1002
1003 return adata->mount_matrix;
1004 }
1005
1006 static const struct iio_chan_spec_ext_info mount_matrix_ext_info[] = {
1007 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, get_mount_matrix),
1008 { },
1009 };
1010
1011
1012
1013
1014 static int apply_acpi_orientation(struct iio_dev *indio_dev,
1015 struct iio_chan_spec *channels)
1016 {
1017 struct st_sensor_data *adata = iio_priv(indio_dev);
1018 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1019 struct acpi_device *adev;
1020 union acpi_object *ont;
1021 union acpi_object *elements;
1022 acpi_status status;
1023 int ret = -EINVAL;
1024 unsigned int val;
1025 int i, j;
1026 int final_ont[3][3] = { { 0 }, };
1027
1028
1029
1030
1031
1032
1033
1034 const int default_ont[3][3] = {
1035 { 0, 1, 0 },
1036 { -1, 0, 0 },
1037 { 0, 0, -1 },
1038 };
1039
1040
1041 adev = ACPI_COMPANION(adata->dev);
1042 if (!adev)
1043 return 0;
1044
1045
1046 status = acpi_evaluate_object(adev->handle, "_ONT", NULL, &buffer);
1047 if (status == AE_NOT_FOUND) {
1048 return 0;
1049 } else if (ACPI_FAILURE(status)) {
1050 dev_warn(&indio_dev->dev, "failed to execute _ONT: %d\n",
1051 status);
1052 return status;
1053 }
1054
1055 ont = buffer.pointer;
1056 if (ont->type != ACPI_TYPE_PACKAGE || ont->package.count != 6)
1057 goto out;
1058
1059
1060
1061
1062
1063 elements = ont->package.elements;
1064 for (i = 0; i < 3; i++) {
1065 if (elements[i].type != ACPI_TYPE_INTEGER)
1066 goto out;
1067
1068 val = elements[i].integer.value;
1069 if (val > 2)
1070 goto out;
1071
1072
1073
1074
1075
1076 final_ont[0][i] = default_ont[0][val];
1077 final_ont[1][i] = default_ont[1][val];
1078 final_ont[2][i] = default_ont[2][val];
1079 }
1080
1081
1082
1083
1084
1085
1086 elements += 3;
1087 for (i = 0; i < 3; i++) {
1088 if (elements[i].type != ACPI_TYPE_INTEGER)
1089 goto out;
1090
1091 val = elements[i].integer.value;
1092 if (val != 0 && val != 1)
1093 goto out;
1094 if (!val)
1095 continue;
1096
1097
1098 final_ont[0][i] *= -1;
1099 final_ont[1][i] *= -1;
1100 final_ont[2][i] *= -1;
1101 }
1102
1103
1104 adata->mount_matrix = devm_kmalloc(&indio_dev->dev,
1105 sizeof(*adata->mount_matrix),
1106 GFP_KERNEL);
1107 if (!adata->mount_matrix) {
1108 ret = -ENOMEM;
1109 goto out;
1110 }
1111
1112 for (i = 0; i < 3; i++) {
1113 for (j = 0; j < 3; j++) {
1114 int matrix_val = final_ont[i][j];
1115 char *str_value;
1116
1117 switch (matrix_val) {
1118 case -1:
1119 str_value = "-1";
1120 break;
1121 case 0:
1122 str_value = "0";
1123 break;
1124 case 1:
1125 str_value = "1";
1126 break;
1127 default:
1128 goto out;
1129 }
1130 adata->mount_matrix->rotation[i * 3 + j] = str_value;
1131 }
1132 }
1133
1134
1135 for (i = 0; i < indio_dev->num_channels; i++)
1136 channels[i].ext_info = mount_matrix_ext_info;
1137
1138 ret = 0;
1139 dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
1140
1141 out:
1142 kfree(buffer.pointer);
1143 return ret;
1144 }
1145 #else
1146 static int apply_acpi_orientation(struct iio_dev *indio_dev,
1147 struct iio_chan_spec *channels)
1148 {
1149 return 0;
1150 }
1151 #endif
1152
1153
1154
1155
1156
1157
1158
1159 const struct st_sensor_settings *st_accel_get_settings(const char *name)
1160 {
1161 int index = st_sensors_get_settings_index(name,
1162 st_accel_sensors_settings,
1163 ARRAY_SIZE(st_accel_sensors_settings));
1164 if (index < 0)
1165 return NULL;
1166
1167 return &st_accel_sensors_settings[index];
1168 }
1169 EXPORT_SYMBOL(st_accel_get_settings);
1170
1171 int st_accel_common_probe(struct iio_dev *indio_dev)
1172 {
1173 struct st_sensor_data *adata = iio_priv(indio_dev);
1174 struct st_sensors_platform_data *pdata =
1175 (struct st_sensors_platform_data *)adata->dev->platform_data;
1176 struct iio_chan_spec *channels;
1177 size_t channels_size;
1178 int err;
1179
1180 indio_dev->modes = INDIO_DIRECT_MODE;
1181 indio_dev->info = &accel_info;
1182
1183 err = st_sensors_power_enable(indio_dev);
1184 if (err)
1185 return err;
1186
1187 err = st_sensors_verify_id(indio_dev);
1188 if (err < 0)
1189 goto st_accel_power_off;
1190
1191 adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
1192 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
1193
1194 channels_size = indio_dev->num_channels * sizeof(struct iio_chan_spec);
1195 channels = devm_kmemdup(&indio_dev->dev,
1196 adata->sensor_settings->ch,
1197 channels_size, GFP_KERNEL);
1198 if (!channels) {
1199 err = -ENOMEM;
1200 goto st_accel_power_off;
1201 }
1202
1203 if (apply_acpi_orientation(indio_dev, channels))
1204 dev_warn(&indio_dev->dev,
1205 "failed to apply ACPI orientation data: %d\n", err);
1206
1207 indio_dev->channels = channels;
1208 adata->current_fullscale = (struct st_sensor_fullscale_avl *)
1209 &adata->sensor_settings->fs.fs_avl[0];
1210 adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
1211
1212 if (!pdata)
1213 pdata = (struct st_sensors_platform_data *)&default_accel_pdata;
1214
1215 err = st_sensors_init_sensor(indio_dev, pdata);
1216 if (err < 0)
1217 goto st_accel_power_off;
1218
1219 err = st_accel_allocate_ring(indio_dev);
1220 if (err < 0)
1221 goto st_accel_power_off;
1222
1223 if (adata->irq > 0) {
1224 err = st_sensors_allocate_trigger(indio_dev,
1225 ST_ACCEL_TRIGGER_OPS);
1226 if (err < 0)
1227 goto st_accel_probe_trigger_error;
1228 }
1229
1230 err = iio_device_register(indio_dev);
1231 if (err)
1232 goto st_accel_device_register_error;
1233
1234 dev_info(&indio_dev->dev, "registered accelerometer %s\n",
1235 indio_dev->name);
1236
1237 return 0;
1238
1239 st_accel_device_register_error:
1240 if (adata->irq > 0)
1241 st_sensors_deallocate_trigger(indio_dev);
1242 st_accel_probe_trigger_error:
1243 st_accel_deallocate_ring(indio_dev);
1244 st_accel_power_off:
1245 st_sensors_power_disable(indio_dev);
1246
1247 return err;
1248 }
1249 EXPORT_SYMBOL(st_accel_common_probe);
1250
1251 void st_accel_common_remove(struct iio_dev *indio_dev)
1252 {
1253 struct st_sensor_data *adata = iio_priv(indio_dev);
1254
1255 st_sensors_power_disable(indio_dev);
1256
1257 iio_device_unregister(indio_dev);
1258 if (adata->irq > 0)
1259 st_sensors_deallocate_trigger(indio_dev);
1260
1261 st_accel_deallocate_ring(indio_dev);
1262 }
1263 EXPORT_SYMBOL(st_accel_common_remove);
1264
1265 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1266 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1267 MODULE_LICENSE("GPL v2");