treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / accel / st_accel_core.c
blob7320275c7e56dee7ee0a6bb3af4461dc0c9826ab
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * STMicroelectronics accelerometers driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
8 */
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/irq.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/buffer.h>
24 #include <linux/iio/common/st_sensors.h>
25 #include "st_accel.h"
27 #define ST_ACCEL_NUMBER_DATA_CHANNELS 3
29 /* DEFAULT VALUE FOR SENSORS */
30 #define ST_ACCEL_DEFAULT_OUT_X_L_ADDR 0x28
31 #define ST_ACCEL_DEFAULT_OUT_Y_L_ADDR 0x2a
32 #define ST_ACCEL_DEFAULT_OUT_Z_L_ADDR 0x2c
34 /* FULLSCALE */
35 #define ST_ACCEL_FS_AVL_2G 2
36 #define ST_ACCEL_FS_AVL_4G 4
37 #define ST_ACCEL_FS_AVL_6G 6
38 #define ST_ACCEL_FS_AVL_8G 8
39 #define ST_ACCEL_FS_AVL_16G 16
40 #define ST_ACCEL_FS_AVL_100G 100
41 #define ST_ACCEL_FS_AVL_200G 200
42 #define ST_ACCEL_FS_AVL_400G 400
44 static const struct iio_chan_spec st_accel_8bit_channels[] = {
45 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
46 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
47 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 8, 8,
48 ST_ACCEL_DEFAULT_OUT_X_L_ADDR+1),
49 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
50 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
51 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 8, 8,
52 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR+1),
53 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
54 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
55 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 8, 8,
56 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR+1),
57 IIO_CHAN_SOFT_TIMESTAMP(3)
60 static const struct iio_chan_spec st_accel_12bit_channels[] = {
61 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
62 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
63 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16,
64 ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
65 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
66 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
67 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 12, 16,
68 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
69 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
70 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
71 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 12, 16,
72 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
73 IIO_CHAN_SOFT_TIMESTAMP(3)
76 static const struct iio_chan_spec st_accel_16bit_channels[] = {
77 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
78 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
79 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
80 ST_ACCEL_DEFAULT_OUT_X_L_ADDR),
81 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
82 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
83 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
84 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR),
85 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
86 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
87 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
88 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR),
89 IIO_CHAN_SOFT_TIMESTAMP(3)
92 static const struct st_sensor_settings st_accel_sensors_settings[] = {
94 .wai = 0x33,
95 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
96 .sensors_supported = {
97 [0] = LIS3DH_ACCEL_DEV_NAME,
98 [1] = LSM303DLHC_ACCEL_DEV_NAME,
99 [2] = LSM330D_ACCEL_DEV_NAME,
100 [3] = LSM330DL_ACCEL_DEV_NAME,
101 [4] = LSM330DLC_ACCEL_DEV_NAME,
102 [5] = LSM303AGR_ACCEL_DEV_NAME,
103 [6] = LIS2DH12_ACCEL_DEV_NAME,
104 [7] = LIS3DE_ACCEL_DEV_NAME,
106 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
107 .odr = {
108 .addr = 0x20,
109 .mask = 0xf0,
110 .odr_avl = {
111 { .hz = 1, .value = 0x01, },
112 { .hz = 10, .value = 0x02, },
113 { .hz = 25, .value = 0x03, },
114 { .hz = 50, .value = 0x04, },
115 { .hz = 100, .value = 0x05, },
116 { .hz = 200, .value = 0x06, },
117 { .hz = 400, .value = 0x07, },
118 { .hz = 1600, .value = 0x08, },
121 .pw = {
122 .addr = 0x20,
123 .mask = 0xf0,
124 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
126 .enable_axis = {
127 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
128 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
130 .fs = {
131 .addr = 0x23,
132 .mask = 0x30,
133 .fs_avl = {
134 [0] = {
135 .num = ST_ACCEL_FS_AVL_2G,
136 .value = 0x00,
137 .gain = IIO_G_TO_M_S_2(1000),
139 [1] = {
140 .num = ST_ACCEL_FS_AVL_4G,
141 .value = 0x01,
142 .gain = IIO_G_TO_M_S_2(2000),
144 [2] = {
145 .num = ST_ACCEL_FS_AVL_8G,
146 .value = 0x02,
147 .gain = IIO_G_TO_M_S_2(4000),
149 [3] = {
150 .num = ST_ACCEL_FS_AVL_16G,
151 .value = 0x03,
152 .gain = IIO_G_TO_M_S_2(12000),
156 .bdu = {
157 .addr = 0x23,
158 .mask = 0x80,
160 .drdy_irq = {
161 .int1 = {
162 .addr = 0x22,
163 .mask = 0x10,
165 .addr_ihl = 0x25,
166 .mask_ihl = 0x02,
167 .stat_drdy = {
168 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
169 .mask = 0x07,
172 .sim = {
173 .addr = 0x23,
174 .value = BIT(0),
176 .multi_read_bit = true,
177 .bootime = 2,
180 .wai = 0x32,
181 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
182 .sensors_supported = {
183 [0] = LIS331DLH_ACCEL_DEV_NAME,
184 [1] = LSM303DL_ACCEL_DEV_NAME,
185 [2] = LSM303DLH_ACCEL_DEV_NAME,
186 [3] = LSM303DLM_ACCEL_DEV_NAME,
188 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
189 .odr = {
190 .addr = 0x20,
191 .mask = 0x18,
192 .odr_avl = {
193 { .hz = 50, .value = 0x00, },
194 { .hz = 100, .value = 0x01, },
195 { .hz = 400, .value = 0x02, },
196 { .hz = 1000, .value = 0x03, },
199 .pw = {
200 .addr = 0x20,
201 .mask = 0xe0,
202 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
203 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
205 .enable_axis = {
206 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
207 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
209 .fs = {
210 .addr = 0x23,
211 .mask = 0x30,
212 .fs_avl = {
213 [0] = {
214 .num = ST_ACCEL_FS_AVL_2G,
215 .value = 0x00,
216 .gain = IIO_G_TO_M_S_2(1000),
218 [1] = {
219 .num = ST_ACCEL_FS_AVL_4G,
220 .value = 0x01,
221 .gain = IIO_G_TO_M_S_2(2000),
223 [2] = {
224 .num = ST_ACCEL_FS_AVL_8G,
225 .value = 0x03,
226 .gain = IIO_G_TO_M_S_2(3900),
230 .bdu = {
231 .addr = 0x23,
232 .mask = 0x80,
234 .drdy_irq = {
235 .int1 = {
236 .addr = 0x22,
237 .mask = 0x02,
238 .addr_od = 0x22,
239 .mask_od = 0x40,
241 .int2 = {
242 .addr = 0x22,
243 .mask = 0x10,
244 .addr_od = 0x22,
245 .mask_od = 0x40,
247 .addr_ihl = 0x22,
248 .mask_ihl = 0x80,
249 .stat_drdy = {
250 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
251 .mask = 0x07,
254 .sim = {
255 .addr = 0x23,
256 .value = BIT(0),
258 .multi_read_bit = true,
259 .bootime = 2,
262 .wai = 0x40,
263 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
264 .sensors_supported = {
265 [0] = LSM330_ACCEL_DEV_NAME,
267 .ch = (struct iio_chan_spec *)st_accel_16bit_channels,
268 .odr = {
269 .addr = 0x20,
270 .mask = 0xf0,
271 .odr_avl = {
272 { .hz = 3, .value = 0x01, },
273 { .hz = 6, .value = 0x02, },
274 { .hz = 12, .value = 0x03, },
275 { .hz = 25, .value = 0x04, },
276 { .hz = 50, .value = 0x05, },
277 { .hz = 100, .value = 0x06, },
278 { .hz = 200, .value = 0x07, },
279 { .hz = 400, .value = 0x08, },
280 { .hz = 800, .value = 0x09, },
281 { .hz = 1600, .value = 0x0a, },
284 .pw = {
285 .addr = 0x20,
286 .mask = 0xf0,
287 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
289 .enable_axis = {
290 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
291 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
293 .fs = {
294 .addr = 0x24,
295 .mask = 0x38,
296 .fs_avl = {
297 [0] = {
298 .num = ST_ACCEL_FS_AVL_2G,
299 .value = 0x00,
300 .gain = IIO_G_TO_M_S_2(61),
302 [1] = {
303 .num = ST_ACCEL_FS_AVL_4G,
304 .value = 0x01,
305 .gain = IIO_G_TO_M_S_2(122),
307 [2] = {
308 .num = ST_ACCEL_FS_AVL_6G,
309 .value = 0x02,
310 .gain = IIO_G_TO_M_S_2(183),
312 [3] = {
313 .num = ST_ACCEL_FS_AVL_8G,
314 .value = 0x03,
315 .gain = IIO_G_TO_M_S_2(244),
317 [4] = {
318 .num = ST_ACCEL_FS_AVL_16G,
319 .value = 0x04,
320 .gain = IIO_G_TO_M_S_2(732),
324 .bdu = {
325 .addr = 0x20,
326 .mask = 0x08,
328 .drdy_irq = {
329 .int1 = {
330 .addr = 0x23,
331 .mask = 0x80,
333 .addr_ihl = 0x23,
334 .mask_ihl = 0x40,
335 .stat_drdy = {
336 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
337 .mask = 0x07,
339 .ig1 = {
340 .en_addr = 0x23,
341 .en_mask = 0x08,
344 .sim = {
345 .addr = 0x24,
346 .value = BIT(0),
348 .multi_read_bit = false,
349 .bootime = 2,
352 .wai = 0x3a,
353 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
354 .sensors_supported = {
355 [0] = LIS3LV02DL_ACCEL_DEV_NAME,
357 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
358 .odr = {
359 .addr = 0x20,
360 .mask = 0x30, /* DF1 and DF0 */
361 .odr_avl = {
362 { .hz = 40, .value = 0x00, },
363 { .hz = 160, .value = 0x01, },
364 { .hz = 640, .value = 0x02, },
365 { .hz = 2560, .value = 0x03, },
368 .pw = {
369 .addr = 0x20,
370 .mask = 0xc0,
371 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
372 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
374 .enable_axis = {
375 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
376 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
378 .fs = {
379 .addr = 0x21,
380 .mask = 0x80,
381 .fs_avl = {
382 [0] = {
383 .num = ST_ACCEL_FS_AVL_2G,
384 .value = 0x00,
385 .gain = IIO_G_TO_M_S_2(1000),
387 [1] = {
388 .num = ST_ACCEL_FS_AVL_6G,
389 .value = 0x01,
390 .gain = IIO_G_TO_M_S_2(3000),
394 .bdu = {
395 .addr = 0x21,
396 .mask = 0x40,
399 * Data Alignment Setting - needs to be set to get
400 * left-justified data like all other sensors.
402 .das = {
403 .addr = 0x21,
404 .mask = 0x01,
406 .drdy_irq = {
407 .int1 = {
408 .addr = 0x21,
409 .mask = 0x04,
411 .stat_drdy = {
412 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
413 .mask = 0x07,
416 .sim = {
417 .addr = 0x21,
418 .value = BIT(1),
420 .multi_read_bit = true,
421 .bootime = 2, /* guess */
424 .wai = 0x3b,
425 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
426 .sensors_supported = {
427 [0] = LIS331DL_ACCEL_DEV_NAME,
429 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
430 .odr = {
431 .addr = 0x20,
432 .mask = 0x80,
433 .odr_avl = {
434 { .hz = 100, .value = 0x00, },
435 { .hz = 400, .value = 0x01, },
438 .pw = {
439 .addr = 0x20,
440 .mask = 0x40,
441 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
442 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
444 .enable_axis = {
445 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
446 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
448 .fs = {
449 .addr = 0x20,
450 .mask = 0x20,
452 * TODO: check these resulting gain settings, these are
453 * not in the datsheet
455 .fs_avl = {
456 [0] = {
457 .num = ST_ACCEL_FS_AVL_2G,
458 .value = 0x00,
459 .gain = IIO_G_TO_M_S_2(18000),
461 [1] = {
462 .num = ST_ACCEL_FS_AVL_8G,
463 .value = 0x01,
464 .gain = IIO_G_TO_M_S_2(72000),
468 .drdy_irq = {
469 .int1 = {
470 .addr = 0x22,
471 .mask = 0x04,
472 .addr_od = 0x22,
473 .mask_od = 0x40,
475 .int2 = {
476 .addr = 0x22,
477 .mask = 0x20,
478 .addr_od = 0x22,
479 .mask_od = 0x40,
481 .addr_ihl = 0x22,
482 .mask_ihl = 0x80,
483 .stat_drdy = {
484 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
485 .mask = 0x07,
488 .sim = {
489 .addr = 0x21,
490 .value = BIT(7),
492 .multi_read_bit = false,
493 .bootime = 2, /* guess */
496 .wai = 0x32,
497 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
498 .sensors_supported = {
499 [0] = H3LIS331DL_ACCEL_DEV_NAME,
501 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
502 .odr = {
503 .addr = 0x20,
504 .mask = 0x18,
505 .odr_avl = {
506 { .hz = 50, .value = 0x00, },
507 { .hz = 100, .value = 0x01, },
508 { .hz = 400, .value = 0x02, },
509 { .hz = 1000, .value = 0x03, },
512 .pw = {
513 .addr = 0x20,
514 .mask = 0x20,
515 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
516 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
518 .enable_axis = {
519 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
520 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
522 .fs = {
523 .addr = 0x23,
524 .mask = 0x30,
525 .fs_avl = {
526 [0] = {
527 .num = ST_ACCEL_FS_AVL_100G,
528 .value = 0x00,
529 .gain = IIO_G_TO_M_S_2(49000),
531 [1] = {
532 .num = ST_ACCEL_FS_AVL_200G,
533 .value = 0x01,
534 .gain = IIO_G_TO_M_S_2(98000),
536 [2] = {
537 .num = ST_ACCEL_FS_AVL_400G,
538 .value = 0x03,
539 .gain = IIO_G_TO_M_S_2(195000),
543 .bdu = {
544 .addr = 0x23,
545 .mask = 0x80,
547 .drdy_irq = {
548 .int1 = {
549 .addr = 0x22,
550 .mask = 0x02,
552 .int2 = {
553 .addr = 0x22,
554 .mask = 0x10,
556 .addr_ihl = 0x22,
557 .mask_ihl = 0x80,
559 .sim = {
560 .addr = 0x23,
561 .value = BIT(0),
563 .multi_read_bit = true,
564 .bootime = 2,
567 /* No WAI register present */
568 .sensors_supported = {
569 [0] = LIS3L02DQ_ACCEL_DEV_NAME,
571 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
572 .odr = {
573 .addr = 0x20,
574 .mask = 0x30,
575 .odr_avl = {
576 { .hz = 280, .value = 0x00, },
577 { .hz = 560, .value = 0x01, },
578 { .hz = 1120, .value = 0x02, },
579 { .hz = 4480, .value = 0x03, },
582 .pw = {
583 .addr = 0x20,
584 .mask = 0xc0,
585 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
586 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
588 .enable_axis = {
589 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
590 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
592 .fs = {
593 .fs_avl = {
594 [0] = {
595 .num = ST_ACCEL_FS_AVL_2G,
596 .gain = IIO_G_TO_M_S_2(488),
601 * The part has a BDU bit but if set the data is never
602 * updated so don't set it.
604 .bdu = {
606 .drdy_irq = {
607 .int1 = {
608 .addr = 0x21,
609 .mask = 0x04,
611 .stat_drdy = {
612 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
613 .mask = 0x07,
616 .sim = {
617 .addr = 0x21,
618 .value = BIT(1),
620 .multi_read_bit = false,
621 .bootime = 2,
624 .wai = 0x33,
625 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
626 .sensors_supported = {
627 [0] = LNG2DM_ACCEL_DEV_NAME,
629 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
630 .odr = {
631 .addr = 0x20,
632 .mask = 0xf0,
633 .odr_avl = {
634 { .hz = 1, .value = 0x01, },
635 { .hz = 10, .value = 0x02, },
636 { .hz = 25, .value = 0x03, },
637 { .hz = 50, .value = 0x04, },
638 { .hz = 100, .value = 0x05, },
639 { .hz = 200, .value = 0x06, },
640 { .hz = 400, .value = 0x07, },
641 { .hz = 1600, .value = 0x08, },
644 .pw = {
645 .addr = 0x20,
646 .mask = 0xf0,
647 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
649 .enable_axis = {
650 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
651 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
653 .fs = {
654 .addr = 0x23,
655 .mask = 0x30,
656 .fs_avl = {
657 [0] = {
658 .num = ST_ACCEL_FS_AVL_2G,
659 .value = 0x00,
660 .gain = IIO_G_TO_M_S_2(15600),
662 [1] = {
663 .num = ST_ACCEL_FS_AVL_4G,
664 .value = 0x01,
665 .gain = IIO_G_TO_M_S_2(31200),
667 [2] = {
668 .num = ST_ACCEL_FS_AVL_8G,
669 .value = 0x02,
670 .gain = IIO_G_TO_M_S_2(62500),
672 [3] = {
673 .num = ST_ACCEL_FS_AVL_16G,
674 .value = 0x03,
675 .gain = IIO_G_TO_M_S_2(187500),
679 .drdy_irq = {
680 .int1 = {
681 .addr = 0x22,
682 .mask = 0x10,
684 .addr_ihl = 0x25,
685 .mask_ihl = 0x02,
686 .stat_drdy = {
687 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
688 .mask = 0x07,
691 .sim = {
692 .addr = 0x23,
693 .value = BIT(0),
695 .multi_read_bit = true,
696 .bootime = 2,
699 .wai = 0x44,
700 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
701 .sensors_supported = {
702 [0] = LIS2DW12_ACCEL_DEV_NAME,
704 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
705 .odr = {
706 .addr = 0x20,
707 .mask = 0xf0,
708 .odr_avl = {
709 { .hz = 1, .value = 0x01, },
710 { .hz = 12, .value = 0x02, },
711 { .hz = 25, .value = 0x03, },
712 { .hz = 50, .value = 0x04, },
713 { .hz = 100, .value = 0x05, },
714 { .hz = 200, .value = 0x06, },
717 .pw = {
718 .addr = 0x20,
719 .mask = 0xf0,
720 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
722 .fs = {
723 .addr = 0x25,
724 .mask = 0x30,
725 .fs_avl = {
726 [0] = {
727 .num = ST_ACCEL_FS_AVL_2G,
728 .value = 0x00,
729 .gain = IIO_G_TO_M_S_2(976),
731 [1] = {
732 .num = ST_ACCEL_FS_AVL_4G,
733 .value = 0x01,
734 .gain = IIO_G_TO_M_S_2(1952),
736 [2] = {
737 .num = ST_ACCEL_FS_AVL_8G,
738 .value = 0x02,
739 .gain = IIO_G_TO_M_S_2(3904),
741 [3] = {
742 .num = ST_ACCEL_FS_AVL_16G,
743 .value = 0x03,
744 .gain = IIO_G_TO_M_S_2(7808),
748 .bdu = {
749 .addr = 0x21,
750 .mask = 0x08,
752 .drdy_irq = {
753 .int1 = {
754 .addr = 0x23,
755 .mask = 0x01,
756 .addr_od = 0x22,
757 .mask_od = 0x20,
759 .int2 = {
760 .addr = 0x24,
761 .mask = 0x01,
762 .addr_od = 0x22,
763 .mask_od = 0x20,
765 .addr_ihl = 0x22,
766 .mask_ihl = 0x08,
767 .stat_drdy = {
768 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
769 .mask = 0x01,
772 .sim = {
773 .addr = 0x21,
774 .value = BIT(0),
776 .multi_read_bit = false,
777 .bootime = 2,
780 .wai = 0x11,
781 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
782 .sensors_supported = {
783 [0] = LIS3DHH_ACCEL_DEV_NAME,
785 .ch = (struct iio_chan_spec *)st_accel_16bit_channels,
786 .odr = {
787 /* just ODR = 1100Hz available */
788 .odr_avl = {
789 { .hz = 1100, .value = 0x00, },
792 .pw = {
793 .addr = 0x20,
794 .mask = 0x80,
795 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
796 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
798 .fs = {
799 .fs_avl = {
800 [0] = {
801 .num = ST_ACCEL_FS_AVL_2G,
802 .gain = IIO_G_TO_M_S_2(76),
806 .bdu = {
807 .addr = 0x20,
808 .mask = 0x01,
810 .drdy_irq = {
811 .int1 = {
812 .addr = 0x21,
813 .mask = 0x80,
814 .addr_od = 0x23,
815 .mask_od = 0x04,
817 .int2 = {
818 .addr = 0x22,
819 .mask = 0x80,
820 .addr_od = 0x23,
821 .mask_od = 0x08,
823 .stat_drdy = {
824 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
825 .mask = 0x07,
828 .multi_read_bit = false,
829 .bootime = 2,
832 .wai = 0x33,
833 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
834 .sensors_supported = {
835 [0] = LIS2DE12_ACCEL_DEV_NAME,
837 .ch = (struct iio_chan_spec *)st_accel_8bit_channels,
838 .odr = {
839 .addr = 0x20,
840 .mask = 0xf0,
841 .odr_avl = {
842 { .hz = 1, .value = 0x01, },
843 { .hz = 10, .value = 0x02, },
844 { .hz = 25, .value = 0x03, },
845 { .hz = 50, .value = 0x04, },
846 { .hz = 100, .value = 0x05, },
847 { .hz = 200, .value = 0x06, },
848 { .hz = 400, .value = 0x07, },
849 { .hz = 1620, .value = 0x08, },
850 { .hz = 5376, .value = 0x09, },
853 .pw = {
854 .addr = 0x20,
855 .mask = 0xf0,
856 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
858 .enable_axis = {
859 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
860 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
862 .fs = {
863 .addr = 0x23,
864 .mask = 0x30,
865 .fs_avl = {
866 [0] = {
867 .num = ST_ACCEL_FS_AVL_2G,
868 .value = 0x00,
869 .gain = IIO_G_TO_M_S_2(15600),
871 [1] = {
872 .num = ST_ACCEL_FS_AVL_4G,
873 .value = 0x01,
874 .gain = IIO_G_TO_M_S_2(31200),
876 [2] = {
877 .num = ST_ACCEL_FS_AVL_8G,
878 .value = 0x02,
879 .gain = IIO_G_TO_M_S_2(62500),
881 [3] = {
882 .num = ST_ACCEL_FS_AVL_16G,
883 .value = 0x03,
884 .gain = IIO_G_TO_M_S_2(187500),
888 .drdy_irq = {
889 .int1 = {
890 .addr = 0x22,
891 .mask = 0x10,
893 .addr_ihl = 0x25,
894 .mask_ihl = 0x02,
895 .stat_drdy = {
896 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
897 .mask = 0x07,
900 .sim = {
901 .addr = 0x23,
902 .value = BIT(0),
904 .multi_read_bit = true,
905 .bootime = 2,
909 static int st_accel_read_raw(struct iio_dev *indio_dev,
910 struct iio_chan_spec const *ch, int *val,
911 int *val2, long mask)
913 int err;
914 struct st_sensor_data *adata = iio_priv(indio_dev);
916 switch (mask) {
917 case IIO_CHAN_INFO_RAW:
918 err = st_sensors_read_info_raw(indio_dev, ch, val);
919 if (err < 0)
920 goto read_error;
922 return IIO_VAL_INT;
923 case IIO_CHAN_INFO_SCALE:
924 *val = adata->current_fullscale->gain / 1000000;
925 *val2 = adata->current_fullscale->gain % 1000000;
926 return IIO_VAL_INT_PLUS_MICRO;
927 case IIO_CHAN_INFO_SAMP_FREQ:
928 *val = adata->odr;
929 return IIO_VAL_INT;
930 default:
931 return -EINVAL;
934 read_error:
935 return err;
938 static int st_accel_write_raw(struct iio_dev *indio_dev,
939 struct iio_chan_spec const *chan, int val, int val2, long mask)
941 int err;
943 switch (mask) {
944 case IIO_CHAN_INFO_SCALE: {
945 int gain;
947 gain = val * 1000000 + val2;
948 err = st_sensors_set_fullscale_by_gain(indio_dev, gain);
949 break;
951 case IIO_CHAN_INFO_SAMP_FREQ:
952 if (val2)
953 return -EINVAL;
954 mutex_lock(&indio_dev->mlock);
955 err = st_sensors_set_odr(indio_dev, val);
956 mutex_unlock(&indio_dev->mlock);
957 return err;
958 default:
959 return -EINVAL;
962 return err;
965 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
966 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
968 static struct attribute *st_accel_attributes[] = {
969 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
970 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
971 NULL,
974 static const struct attribute_group st_accel_attribute_group = {
975 .attrs = st_accel_attributes,
978 static const struct iio_info accel_info = {
979 .attrs = &st_accel_attribute_group,
980 .read_raw = &st_accel_read_raw,
981 .write_raw = &st_accel_write_raw,
982 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
985 #ifdef CONFIG_IIO_TRIGGER
986 static const struct iio_trigger_ops st_accel_trigger_ops = {
987 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE,
988 .validate_device = st_sensors_validate_device,
990 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
991 #else
992 #define ST_ACCEL_TRIGGER_OPS NULL
993 #endif
995 #ifdef CONFIG_ACPI
996 static const struct iio_mount_matrix *
997 get_mount_matrix(const struct iio_dev *indio_dev,
998 const struct iio_chan_spec *chan)
1000 struct st_sensor_data *adata = iio_priv(indio_dev);
1002 return adata->mount_matrix;
1005 static const struct iio_chan_spec_ext_info mount_matrix_ext_info[] = {
1006 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, get_mount_matrix),
1007 { },
1010 /* Read ST-specific _ONT orientation data from ACPI and generate an
1011 * appropriate mount matrix.
1013 static int apply_acpi_orientation(struct iio_dev *indio_dev,
1014 struct iio_chan_spec *channels)
1016 struct st_sensor_data *adata = iio_priv(indio_dev);
1017 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1018 struct acpi_device *adev;
1019 union acpi_object *ont;
1020 union acpi_object *elements;
1021 acpi_status status;
1022 int ret = -EINVAL;
1023 unsigned int val;
1024 int i, j;
1025 int final_ont[3][3] = { { 0 }, };
1027 /* For some reason, ST's _ONT translation does not apply directly
1028 * to the data read from the sensor. Another translation must be
1029 * performed first, as described by the matrix below. Perhaps
1030 * ST required this specific translation for the first product
1031 * where the device was mounted?
1033 const int default_ont[3][3] = {
1034 { 0, 1, 0 },
1035 { -1, 0, 0 },
1036 { 0, 0, -1 },
1040 adev = ACPI_COMPANION(adata->dev);
1041 if (!adev)
1042 return 0;
1044 /* Read _ONT data, which should be a package of 6 integers. */
1045 status = acpi_evaluate_object(adev->handle, "_ONT", NULL, &buffer);
1046 if (status == AE_NOT_FOUND) {
1047 return 0;
1048 } else if (ACPI_FAILURE(status)) {
1049 dev_warn(&indio_dev->dev, "failed to execute _ONT: %d\n",
1050 status);
1051 return status;
1054 ont = buffer.pointer;
1055 if (ont->type != ACPI_TYPE_PACKAGE || ont->package.count != 6)
1056 goto out;
1058 /* The first 3 integers provide axis order information.
1059 * e.g. 0 1 2 would indicate normal X,Y,Z ordering.
1060 * e.g. 1 0 2 indicates that data arrives in order Y,X,Z.
1062 elements = ont->package.elements;
1063 for (i = 0; i < 3; i++) {
1064 if (elements[i].type != ACPI_TYPE_INTEGER)
1065 goto out;
1067 val = elements[i].integer.value;
1068 if (val > 2)
1069 goto out;
1071 /* Avoiding full matrix multiplication, we simply reorder the
1072 * columns in the default_ont matrix according to the
1073 * ordering provided by _ONT.
1075 final_ont[0][i] = default_ont[0][val];
1076 final_ont[1][i] = default_ont[1][val];
1077 final_ont[2][i] = default_ont[2][val];
1080 /* The final 3 integers provide sign flip information.
1081 * 0 means no change, 1 means flip.
1082 * e.g. 0 0 1 means that Z data should be sign-flipped.
1083 * This is applied after the axis reordering from above.
1085 elements += 3;
1086 for (i = 0; i < 3; i++) {
1087 if (elements[i].type != ACPI_TYPE_INTEGER)
1088 goto out;
1090 val = elements[i].integer.value;
1091 if (val != 0 && val != 1)
1092 goto out;
1093 if (!val)
1094 continue;
1096 /* Flip the values in the indicated column */
1097 final_ont[0][i] *= -1;
1098 final_ont[1][i] *= -1;
1099 final_ont[2][i] *= -1;
1102 /* Convert our integer matrix to a string-based iio_mount_matrix */
1103 adata->mount_matrix = devm_kmalloc(&indio_dev->dev,
1104 sizeof(*adata->mount_matrix),
1105 GFP_KERNEL);
1106 if (!adata->mount_matrix) {
1107 ret = -ENOMEM;
1108 goto out;
1111 for (i = 0; i < 3; i++) {
1112 for (j = 0; j < 3; j++) {
1113 int matrix_val = final_ont[i][j];
1114 char *str_value;
1116 switch (matrix_val) {
1117 case -1:
1118 str_value = "-1";
1119 break;
1120 case 0:
1121 str_value = "0";
1122 break;
1123 case 1:
1124 str_value = "1";
1125 break;
1126 default:
1127 goto out;
1129 adata->mount_matrix->rotation[i * 3 + j] = str_value;
1133 /* Expose the mount matrix via ext_info */
1134 for (i = 0; i < indio_dev->num_channels; i++)
1135 channels[i].ext_info = mount_matrix_ext_info;
1137 ret = 0;
1138 dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
1140 out:
1141 kfree(buffer.pointer);
1142 return ret;
1144 #else /* !CONFIG_ACPI */
1145 static int apply_acpi_orientation(struct iio_dev *indio_dev,
1146 struct iio_chan_spec *channels)
1148 return 0;
1150 #endif
1153 * st_accel_get_settings() - get sensor settings from device name
1154 * @name: device name buffer reference.
1156 * Return: valid reference on success, NULL otherwise.
1158 const struct st_sensor_settings *st_accel_get_settings(const char *name)
1160 int index = st_sensors_get_settings_index(name,
1161 st_accel_sensors_settings,
1162 ARRAY_SIZE(st_accel_sensors_settings));
1163 if (index < 0)
1164 return NULL;
1166 return &st_accel_sensors_settings[index];
1168 EXPORT_SYMBOL(st_accel_get_settings);
1170 int st_accel_common_probe(struct iio_dev *indio_dev)
1172 struct st_sensor_data *adata = iio_priv(indio_dev);
1173 struct st_sensors_platform_data *pdata =
1174 (struct st_sensors_platform_data *)adata->dev->platform_data;
1175 struct iio_chan_spec *channels;
1176 size_t channels_size;
1177 int err;
1179 indio_dev->modes = INDIO_DIRECT_MODE;
1180 indio_dev->info = &accel_info;
1182 err = st_sensors_power_enable(indio_dev);
1183 if (err)
1184 return err;
1186 err = st_sensors_verify_id(indio_dev);
1187 if (err < 0)
1188 goto st_accel_power_off;
1190 adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
1191 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
1193 channels_size = indio_dev->num_channels * sizeof(struct iio_chan_spec);
1194 channels = devm_kmemdup(&indio_dev->dev,
1195 adata->sensor_settings->ch,
1196 channels_size, GFP_KERNEL);
1197 if (!channels) {
1198 err = -ENOMEM;
1199 goto st_accel_power_off;
1202 if (apply_acpi_orientation(indio_dev, channels))
1203 dev_warn(&indio_dev->dev,
1204 "failed to apply ACPI orientation data: %d\n", err);
1206 indio_dev->channels = channels;
1207 adata->current_fullscale = (struct st_sensor_fullscale_avl *)
1208 &adata->sensor_settings->fs.fs_avl[0];
1209 adata->odr = adata->sensor_settings->odr.odr_avl[0].hz;
1211 if (!pdata)
1212 pdata = (struct st_sensors_platform_data *)&default_accel_pdata;
1214 err = st_sensors_init_sensor(indio_dev, pdata);
1215 if (err < 0)
1216 goto st_accel_power_off;
1218 err = st_accel_allocate_ring(indio_dev);
1219 if (err < 0)
1220 goto st_accel_power_off;
1222 if (adata->irq > 0) {
1223 err = st_sensors_allocate_trigger(indio_dev,
1224 ST_ACCEL_TRIGGER_OPS);
1225 if (err < 0)
1226 goto st_accel_probe_trigger_error;
1229 err = iio_device_register(indio_dev);
1230 if (err)
1231 goto st_accel_device_register_error;
1233 dev_info(&indio_dev->dev, "registered accelerometer %s\n",
1234 indio_dev->name);
1236 return 0;
1238 st_accel_device_register_error:
1239 if (adata->irq > 0)
1240 st_sensors_deallocate_trigger(indio_dev);
1241 st_accel_probe_trigger_error:
1242 st_accel_deallocate_ring(indio_dev);
1243 st_accel_power_off:
1244 st_sensors_power_disable(indio_dev);
1246 return err;
1248 EXPORT_SYMBOL(st_accel_common_probe);
1250 void st_accel_common_remove(struct iio_dev *indio_dev)
1252 struct st_sensor_data *adata = iio_priv(indio_dev);
1254 st_sensors_power_disable(indio_dev);
1256 iio_device_unregister(indio_dev);
1257 if (adata->irq > 0)
1258 st_sensors_deallocate_trigger(indio_dev);
1260 st_accel_deallocate_ring(indio_dev);
1262 EXPORT_SYMBOL(st_accel_common_remove);
1264 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1265 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1266 MODULE_LICENSE("GPL v2");