1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics accelerometers driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/sysfs.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/trigger.h>
20 #include <linux/iio/common/st_sensors.h>
23 #define ST_ACCEL_NUMBER_DATA_CHANNELS 3
25 /* DEFAULT VALUE FOR SENSORS */
26 #define ST_ACCEL_DEFAULT_OUT_X_L_ADDR 0x28
27 #define ST_ACCEL_DEFAULT_OUT_Y_L_ADDR 0x2a
28 #define ST_ACCEL_DEFAULT_OUT_Z_L_ADDR 0x2c
31 #define ST_ACCEL_FS_AVL_2G 2
32 #define ST_ACCEL_FS_AVL_4G 4
33 #define ST_ACCEL_FS_AVL_6G 6
34 #define ST_ACCEL_FS_AVL_8G 8
35 #define ST_ACCEL_FS_AVL_16G 16
36 #define ST_ACCEL_FS_AVL_100G 100
37 #define ST_ACCEL_FS_AVL_200G 200
38 #define ST_ACCEL_FS_AVL_400G 400
40 static const struct iio_mount_matrix
*
41 st_accel_get_mount_matrix(const struct iio_dev
*indio_dev
,
42 const struct iio_chan_spec
*chan
)
44 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
46 return &adata
->mount_matrix
;
49 static const struct iio_chan_spec_ext_info st_accel_mount_matrix_ext_info
[] = {
50 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL
, st_accel_get_mount_matrix
),
54 static const struct iio_chan_spec st_accel_8bit_channels
[] = {
55 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL
,
56 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
57 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 8, 8,
58 ST_ACCEL_DEFAULT_OUT_X_L_ADDR
+1,
59 st_accel_mount_matrix_ext_info
),
60 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL
,
61 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
62 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 8, 8,
63 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR
+1,
64 st_accel_mount_matrix_ext_info
),
65 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL
,
66 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
67 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 8, 8,
68 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR
+1,
69 st_accel_mount_matrix_ext_info
),
70 IIO_CHAN_SOFT_TIMESTAMP(3)
73 static const struct iio_chan_spec st_accel_12bit_channels
[] = {
74 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL
,
75 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
76 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 12, 16,
77 ST_ACCEL_DEFAULT_OUT_X_L_ADDR
,
78 st_accel_mount_matrix_ext_info
),
79 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL
,
80 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
81 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 12, 16,
82 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR
,
83 st_accel_mount_matrix_ext_info
),
84 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL
,
85 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
86 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 12, 16,
87 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR
,
88 st_accel_mount_matrix_ext_info
),
89 IIO_CHAN_SOFT_TIMESTAMP(3)
92 static const struct iio_chan_spec st_accel_16bit_channels
[] = {
93 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL
,
94 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
95 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
96 ST_ACCEL_DEFAULT_OUT_X_L_ADDR
,
97 st_accel_mount_matrix_ext_info
),
98 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL
,
99 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
100 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
101 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR
,
102 st_accel_mount_matrix_ext_info
),
103 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL
,
104 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
105 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
106 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR
,
107 st_accel_mount_matrix_ext_info
),
108 IIO_CHAN_SOFT_TIMESTAMP(3)
111 static const struct st_sensor_settings st_accel_sensors_settings
[] = {
114 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
115 .sensors_supported
= {
116 [0] = LIS3DH_ACCEL_DEV_NAME
,
117 [1] = LSM303DLHC_ACCEL_DEV_NAME
,
118 [2] = LSM330D_ACCEL_DEV_NAME
,
119 [3] = LSM330DL_ACCEL_DEV_NAME
,
120 [4] = LSM330DLC_ACCEL_DEV_NAME
,
121 [5] = LSM303AGR_ACCEL_DEV_NAME
,
122 [6] = LIS2DH12_ACCEL_DEV_NAME
,
123 [7] = LIS3DE_ACCEL_DEV_NAME
,
125 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
130 { .hz
= 1, .value
= 0x01, },
131 { .hz
= 10, .value
= 0x02, },
132 { .hz
= 25, .value
= 0x03, },
133 { .hz
= 50, .value
= 0x04, },
134 { .hz
= 100, .value
= 0x05, },
135 { .hz
= 200, .value
= 0x06, },
136 { .hz
= 400, .value
= 0x07, },
137 { .hz
= 1600, .value
= 0x08, },
143 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
146 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
147 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
154 .num
= ST_ACCEL_FS_AVL_2G
,
156 .gain
= IIO_G_TO_M_S_2(1000),
159 .num
= ST_ACCEL_FS_AVL_4G
,
161 .gain
= IIO_G_TO_M_S_2(2000),
164 .num
= ST_ACCEL_FS_AVL_8G
,
166 .gain
= IIO_G_TO_M_S_2(4000),
169 .num
= ST_ACCEL_FS_AVL_16G
,
171 .gain
= IIO_G_TO_M_S_2(12000),
187 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
195 .multi_read_bit
= true,
200 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
201 .sensors_supported
= {
202 [0] = LIS331DLH_ACCEL_DEV_NAME
,
203 [1] = LSM303DL_ACCEL_DEV_NAME
,
204 [2] = LSM303DLH_ACCEL_DEV_NAME
,
205 [3] = LSM303DLM_ACCEL_DEV_NAME
,
207 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
212 { .hz
= 50, .value
= 0x00, },
213 { .hz
= 100, .value
= 0x01, },
214 { .hz
= 400, .value
= 0x02, },
215 { .hz
= 1000, .value
= 0x03, },
221 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
222 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
225 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
226 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
233 .num
= ST_ACCEL_FS_AVL_2G
,
235 .gain
= IIO_G_TO_M_S_2(1000),
238 .num
= ST_ACCEL_FS_AVL_4G
,
240 .gain
= IIO_G_TO_M_S_2(2000),
243 .num
= ST_ACCEL_FS_AVL_8G
,
245 .gain
= IIO_G_TO_M_S_2(3900),
269 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
277 .multi_read_bit
= true,
282 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
283 .sensors_supported
= {
284 [0] = LSM330_ACCEL_DEV_NAME
,
286 .ch
= (struct iio_chan_spec
*)st_accel_16bit_channels
,
291 { .hz
= 3, .value
= 0x01, },
292 { .hz
= 6, .value
= 0x02, },
293 { .hz
= 12, .value
= 0x03, },
294 { .hz
= 25, .value
= 0x04, },
295 { .hz
= 50, .value
= 0x05, },
296 { .hz
= 100, .value
= 0x06, },
297 { .hz
= 200, .value
= 0x07, },
298 { .hz
= 400, .value
= 0x08, },
299 { .hz
= 800, .value
= 0x09, },
300 { .hz
= 1600, .value
= 0x0a, },
306 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
309 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
310 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
317 .num
= ST_ACCEL_FS_AVL_2G
,
319 .gain
= IIO_G_TO_M_S_2(61),
322 .num
= ST_ACCEL_FS_AVL_4G
,
324 .gain
= IIO_G_TO_M_S_2(122),
327 .num
= ST_ACCEL_FS_AVL_6G
,
329 .gain
= IIO_G_TO_M_S_2(183),
332 .num
= ST_ACCEL_FS_AVL_8G
,
334 .gain
= IIO_G_TO_M_S_2(244),
337 .num
= ST_ACCEL_FS_AVL_16G
,
339 .gain
= IIO_G_TO_M_S_2(732),
355 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
367 .multi_read_bit
= false,
372 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
373 .sensors_supported
= {
374 [0] = LIS3LV02DL_ACCEL_DEV_NAME
,
376 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
379 .mask
= 0x30, /* DF1 and DF0 */
381 { .hz
= 40, .value
= 0x00, },
382 { .hz
= 160, .value
= 0x01, },
383 { .hz
= 640, .value
= 0x02, },
384 { .hz
= 2560, .value
= 0x03, },
390 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
391 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
394 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
395 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
402 .num
= ST_ACCEL_FS_AVL_2G
,
404 .gain
= IIO_G_TO_M_S_2(1000),
407 .num
= ST_ACCEL_FS_AVL_6G
,
409 .gain
= IIO_G_TO_M_S_2(3000),
418 * Data Alignment Setting - needs to be set to get
419 * left-justified data like all other sensors.
431 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
439 .multi_read_bit
= true,
440 .bootime
= 2, /* guess */
444 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
445 .sensors_supported
= {
446 [0] = LIS331DL_ACCEL_DEV_NAME
,
447 [1] = LIS302DL_ACCEL_DEV_NAME
,
449 .ch
= (struct iio_chan_spec
*)st_accel_8bit_channels
,
454 { .hz
= 100, .value
= 0x00, },
455 { .hz
= 400, .value
= 0x01, },
461 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
462 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
465 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
466 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
472 * TODO: check these resulting gain settings, these are
473 * not in the datsheet
477 .num
= ST_ACCEL_FS_AVL_2G
,
479 .gain
= IIO_G_TO_M_S_2(18000),
482 .num
= ST_ACCEL_FS_AVL_8G
,
484 .gain
= IIO_G_TO_M_S_2(72000),
504 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
512 .multi_read_bit
= false,
513 .bootime
= 2, /* guess */
517 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
518 .sensors_supported
= {
519 [0] = H3LIS331DL_ACCEL_DEV_NAME
,
520 [1] = IIS328DQ_ACCEL_DEV_NAME
,
522 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
527 { .hz
= 50, .value
= 0x00, },
528 { .hz
= 100, .value
= 0x01, },
529 { .hz
= 400, .value
= 0x02, },
530 { .hz
= 1000, .value
= 0x03, },
536 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
537 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
540 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
541 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
548 .num
= ST_ACCEL_FS_AVL_100G
,
550 .gain
= IIO_G_TO_M_S_2(49000),
553 .num
= ST_ACCEL_FS_AVL_200G
,
555 .gain
= IIO_G_TO_M_S_2(98000),
558 .num
= ST_ACCEL_FS_AVL_400G
,
560 .gain
= IIO_G_TO_M_S_2(195000),
584 .multi_read_bit
= true,
588 /* No WAI register present */
589 .sensors_supported
= {
590 [0] = LIS3L02DQ_ACCEL_DEV_NAME
,
592 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
597 { .hz
= 280, .value
= 0x00, },
598 { .hz
= 560, .value
= 0x01, },
599 { .hz
= 1120, .value
= 0x02, },
600 { .hz
= 4480, .value
= 0x03, },
606 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
607 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
610 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
611 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
616 .num
= ST_ACCEL_FS_AVL_2G
,
617 .gain
= IIO_G_TO_M_S_2(488),
622 * The part has a BDU bit but if set the data is never
623 * updated so don't set it.
633 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
641 .multi_read_bit
= false,
646 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
647 .sensors_supported
= {
648 [0] = LNG2DM_ACCEL_DEV_NAME
,
650 .ch
= (struct iio_chan_spec
*)st_accel_8bit_channels
,
655 { .hz
= 1, .value
= 0x01, },
656 { .hz
= 10, .value
= 0x02, },
657 { .hz
= 25, .value
= 0x03, },
658 { .hz
= 50, .value
= 0x04, },
659 { .hz
= 100, .value
= 0x05, },
660 { .hz
= 200, .value
= 0x06, },
661 { .hz
= 400, .value
= 0x07, },
662 { .hz
= 1600, .value
= 0x08, },
668 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
671 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
672 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
679 .num
= ST_ACCEL_FS_AVL_2G
,
681 .gain
= IIO_G_TO_M_S_2(15600),
684 .num
= ST_ACCEL_FS_AVL_4G
,
686 .gain
= IIO_G_TO_M_S_2(31200),
689 .num
= ST_ACCEL_FS_AVL_8G
,
691 .gain
= IIO_G_TO_M_S_2(62500),
694 .num
= ST_ACCEL_FS_AVL_16G
,
696 .gain
= IIO_G_TO_M_S_2(187500),
708 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
716 .multi_read_bit
= true,
721 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
722 .sensors_supported
= {
723 [0] = LIS2DW12_ACCEL_DEV_NAME
,
725 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
730 { .hz
= 1, .value
= 0x01, },
731 { .hz
= 12, .value
= 0x02, },
732 { .hz
= 25, .value
= 0x03, },
733 { .hz
= 50, .value
= 0x04, },
734 { .hz
= 100, .value
= 0x05, },
735 { .hz
= 200, .value
= 0x06, },
741 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
748 .num
= ST_ACCEL_FS_AVL_2G
,
750 .gain
= IIO_G_TO_M_S_2(976),
753 .num
= ST_ACCEL_FS_AVL_4G
,
755 .gain
= IIO_G_TO_M_S_2(1952),
758 .num
= ST_ACCEL_FS_AVL_8G
,
760 .gain
= IIO_G_TO_M_S_2(3904),
763 .num
= ST_ACCEL_FS_AVL_16G
,
765 .gain
= IIO_G_TO_M_S_2(7808),
789 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
797 .multi_read_bit
= false,
802 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
803 .sensors_supported
= {
804 [0] = LIS3DHH_ACCEL_DEV_NAME
,
806 .ch
= (struct iio_chan_spec
*)st_accel_16bit_channels
,
808 /* just ODR = 1100Hz available */
810 { .hz
= 1100, .value
= 0x00, },
816 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
817 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
822 .num
= ST_ACCEL_FS_AVL_2G
,
823 .gain
= IIO_G_TO_M_S_2(76),
845 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
849 .multi_read_bit
= false,
854 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
855 .sensors_supported
= {
856 [0] = LIS2DE12_ACCEL_DEV_NAME
,
858 .ch
= (struct iio_chan_spec
*)st_accel_8bit_channels
,
863 { .hz
= 1, .value
= 0x01, },
864 { .hz
= 10, .value
= 0x02, },
865 { .hz
= 25, .value
= 0x03, },
866 { .hz
= 50, .value
= 0x04, },
867 { .hz
= 100, .value
= 0x05, },
868 { .hz
= 200, .value
= 0x06, },
869 { .hz
= 400, .value
= 0x07, },
870 { .hz
= 1620, .value
= 0x08, },
871 { .hz
= 5376, .value
= 0x09, },
877 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
880 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
881 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
888 .num
= ST_ACCEL_FS_AVL_2G
,
890 .gain
= IIO_G_TO_M_S_2(15600),
893 .num
= ST_ACCEL_FS_AVL_4G
,
895 .gain
= IIO_G_TO_M_S_2(31200),
898 .num
= ST_ACCEL_FS_AVL_8G
,
900 .gain
= IIO_G_TO_M_S_2(62500),
903 .num
= ST_ACCEL_FS_AVL_16G
,
905 .gain
= IIO_G_TO_M_S_2(187500),
917 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
925 .multi_read_bit
= true,
930 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
931 .sensors_supported
= {
932 [0] = LIS2DS12_ACCEL_DEV_NAME
,
934 .ch
= (struct iio_chan_spec
*)st_accel_16bit_channels
,
939 { .hz
= 10, .value
= 0x01, },
940 { .hz
= 50, .value
= 0x02, },
941 { .hz
= 100, .value
= 0x03, },
942 { .hz
= 200, .value
= 0x04, },
943 { .hz
= 400, .value
= 0x05, },
944 { .hz
= 800, .value
= 0x06, },
950 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
953 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
954 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
961 .num
= ST_ACCEL_FS_AVL_2G
,
963 .gain
= IIO_G_TO_M_S_2(61),
966 .num
= ST_ACCEL_FS_AVL_4G
,
968 .gain
= IIO_G_TO_M_S_2(122),
971 .num
= ST_ACCEL_FS_AVL_8G
,
973 .gain
= IIO_G_TO_M_S_2(244),
976 .num
= ST_ACCEL_FS_AVL_16G
,
978 .gain
= IIO_G_TO_M_S_2(488),
998 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
1006 .multi_read_bit
= true,
1011 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
1012 .sensors_supported
= {
1013 [0] = LIS2HH12_ACCEL_DEV_NAME
,
1014 [1] = LSM303C_ACCEL_DEV_NAME
,
1016 .ch
= (struct iio_chan_spec
*)st_accel_16bit_channels
,
1021 { .hz
= 10, .value
= 0x01, },
1022 { .hz
= 50, .value
= 0x02, },
1023 { .hz
= 100, .value
= 0x03, },
1024 { .hz
= 200, .value
= 0x04, },
1025 { .hz
= 400, .value
= 0x05, },
1026 { .hz
= 800, .value
= 0x06, },
1032 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
1035 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
1036 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
1043 .num
= ST_ACCEL_FS_AVL_2G
,
1045 .gain
= IIO_G_TO_M_S_2(61),
1048 .num
= ST_ACCEL_FS_AVL_4G
,
1050 .gain
= IIO_G_TO_M_S_2(122),
1053 .num
= ST_ACCEL_FS_AVL_8G
,
1055 .gain
= IIO_G_TO_M_S_2(244),
1075 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
1083 .multi_read_bit
= true,
1088 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
1089 .sensors_supported
= {
1090 [0] = LSM9DS0_IMU_DEV_NAME
,
1091 [1] = LSM303D_IMU_DEV_NAME
,
1093 .ch
= (struct iio_chan_spec
*)st_accel_16bit_channels
,
1096 .mask
= GENMASK(7, 4),
1112 .mask
= GENMASK(7, 4),
1113 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
1116 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
1117 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
1121 .mask
= GENMASK(5, 3),
1124 .num
= ST_ACCEL_FS_AVL_2G
,
1126 .gain
= IIO_G_TO_M_S_2(61),
1129 .num
= ST_ACCEL_FS_AVL_4G
,
1131 .gain
= IIO_G_TO_M_S_2(122),
1134 .num
= ST_ACCEL_FS_AVL_6G
,
1136 .gain
= IIO_G_TO_M_S_2(183),
1139 .num
= ST_ACCEL_FS_AVL_8G
,
1141 .gain
= IIO_G_TO_M_S_2(244),
1144 .num
= ST_ACCEL_FS_AVL_16G
,
1146 .gain
= IIO_G_TO_M_S_2(732),
1164 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
1165 .mask
= GENMASK(2, 0),
1172 .multi_read_bit
= true,
1177 * Not an ST part. Register-compatible with the LIS2DH, even
1178 * though the WAI value is different.
1181 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
1182 .sensors_supported
= {
1183 [0] = SC7A20_ACCEL_DEV_NAME
,
1185 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
1190 { .hz
= 1, .value
= 0x01, },
1191 { .hz
= 10, .value
= 0x02, },
1192 { .hz
= 25, .value
= 0x03, },
1193 { .hz
= 50, .value
= 0x04, },
1194 { .hz
= 100, .value
= 0x05, },
1195 { .hz
= 200, .value
= 0x06, },
1196 { .hz
= 400, .value
= 0x07, },
1197 { .hz
= 1600, .value
= 0x08, },
1203 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
1206 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
1207 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
1214 .num
= ST_ACCEL_FS_AVL_2G
,
1216 .gain
= IIO_G_TO_M_S_2(1000),
1219 .num
= ST_ACCEL_FS_AVL_4G
,
1221 .gain
= IIO_G_TO_M_S_2(2000),
1224 .num
= ST_ACCEL_FS_AVL_8G
,
1226 .gain
= IIO_G_TO_M_S_2(4000),
1229 .num
= ST_ACCEL_FS_AVL_16G
,
1231 .gain
= IIO_G_TO_M_S_2(12000),
1247 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
1255 .multi_read_bit
= true,
1260 /* Default accel DRDY is available on INT1 pin */
1261 static const struct st_sensors_platform_data default_accel_pdata
= {
1265 static int st_accel_read_raw(struct iio_dev
*indio_dev
,
1266 struct iio_chan_spec
const *ch
, int *val
,
1267 int *val2
, long mask
)
1270 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
1273 case IIO_CHAN_INFO_RAW
:
1274 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
1279 case IIO_CHAN_INFO_SCALE
:
1280 *val
= adata
->current_fullscale
->gain
/ 1000000;
1281 *val2
= adata
->current_fullscale
->gain
% 1000000;
1282 return IIO_VAL_INT_PLUS_MICRO
;
1283 case IIO_CHAN_INFO_SAMP_FREQ
:
1294 static int st_accel_write_raw(struct iio_dev
*indio_dev
,
1295 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
1298 case IIO_CHAN_INFO_SCALE
: {
1301 gain
= val
* 1000000 + val2
;
1302 return st_sensors_set_fullscale_by_gain(indio_dev
, gain
);
1304 case IIO_CHAN_INFO_SAMP_FREQ
:
1308 return st_sensors_set_odr(indio_dev
, val
);
1314 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
1315 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available
);
1317 static struct attribute
*st_accel_attributes
[] = {
1318 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
1319 &iio_dev_attr_in_accel_scale_available
.dev_attr
.attr
,
1323 static const struct attribute_group st_accel_attribute_group
= {
1324 .attrs
= st_accel_attributes
,
1327 static const struct iio_info accel_info
= {
1328 .attrs
= &st_accel_attribute_group
,
1329 .read_raw
= &st_accel_read_raw
,
1330 .write_raw
= &st_accel_write_raw
,
1331 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
1334 #ifdef CONFIG_IIO_TRIGGER
1335 static const struct iio_trigger_ops st_accel_trigger_ops
= {
1336 .set_trigger_state
= ST_ACCEL_TRIGGER_SET_STATE
,
1337 .validate_device
= st_sensors_validate_device
,
1339 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
1341 #define ST_ACCEL_TRIGGER_OPS NULL
1345 /* Read ST-specific _ONT orientation data from ACPI and generate an
1346 * appropriate mount matrix.
1348 static int apply_acpi_orientation(struct iio_dev
*indio_dev
)
1350 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
1351 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
1352 struct acpi_device
*adev
;
1353 union acpi_object
*ont
;
1354 union acpi_object
*elements
;
1359 int final_ont
[3][3] = { { 0 }, };
1361 /* For some reason, ST's _ONT translation does not apply directly
1362 * to the data read from the sensor. Another translation must be
1363 * performed first, as described by the matrix below. Perhaps
1364 * ST required this specific translation for the first product
1365 * where the device was mounted?
1367 const int default_ont
[3][3] = {
1374 adev
= ACPI_COMPANION(indio_dev
->dev
.parent
);
1378 /* Read _ONT data, which should be a package of 6 integers. */
1379 status
= acpi_evaluate_object(adev
->handle
, "_ONT", NULL
, &buffer
);
1380 if (status
== AE_NOT_FOUND
) {
1382 } else if (ACPI_FAILURE(status
)) {
1383 dev_warn(&indio_dev
->dev
, "failed to execute _ONT: %d\n",
1388 ont
= buffer
.pointer
;
1389 if (ont
->type
!= ACPI_TYPE_PACKAGE
|| ont
->package
.count
!= 6)
1392 /* The first 3 integers provide axis order information.
1393 * e.g. 0 1 2 would indicate normal X,Y,Z ordering.
1394 * e.g. 1 0 2 indicates that data arrives in order Y,X,Z.
1396 elements
= ont
->package
.elements
;
1397 for (i
= 0; i
< 3; i
++) {
1398 if (elements
[i
].type
!= ACPI_TYPE_INTEGER
)
1401 val
= elements
[i
].integer
.value
;
1405 /* Avoiding full matrix multiplication, we simply reorder the
1406 * columns in the default_ont matrix according to the
1407 * ordering provided by _ONT.
1409 final_ont
[0][i
] = default_ont
[0][val
];
1410 final_ont
[1][i
] = default_ont
[1][val
];
1411 final_ont
[2][i
] = default_ont
[2][val
];
1414 /* The final 3 integers provide sign flip information.
1415 * 0 means no change, 1 means flip.
1416 * e.g. 0 0 1 means that Z data should be sign-flipped.
1417 * This is applied after the axis reordering from above.
1420 for (i
= 0; i
< 3; i
++) {
1421 if (elements
[i
].type
!= ACPI_TYPE_INTEGER
)
1424 val
= elements
[i
].integer
.value
;
1425 if (val
!= 0 && val
!= 1)
1430 /* Flip the values in the indicated column */
1431 final_ont
[0][i
] *= -1;
1432 final_ont
[1][i
] *= -1;
1433 final_ont
[2][i
] *= -1;
1436 /* Convert our integer matrix to a string-based iio_mount_matrix */
1437 for (i
= 0; i
< 3; i
++) {
1438 for (j
= 0; j
< 3; j
++) {
1439 int matrix_val
= final_ont
[i
][j
];
1442 switch (matrix_val
) {
1455 adata
->mount_matrix
.rotation
[i
* 3 + j
] = str_value
;
1460 dev_info(&indio_dev
->dev
, "computed mount matrix from ACPI\n");
1463 kfree(buffer
.pointer
);
1465 dev_dbg(&indio_dev
->dev
,
1466 "failed to apply ACPI orientation data: %d\n", ret
);
1470 #else /* !CONFIG_ACPI */
1471 static int apply_acpi_orientation(struct iio_dev
*indio_dev
)
1478 * st_accel_get_settings() - get sensor settings from device name
1479 * @name: device name buffer reference.
1481 * Return: valid reference on success, NULL otherwise.
1483 const struct st_sensor_settings
*st_accel_get_settings(const char *name
)
1485 int index
= st_sensors_get_settings_index(name
,
1486 st_accel_sensors_settings
,
1487 ARRAY_SIZE(st_accel_sensors_settings
));
1491 return &st_accel_sensors_settings
[index
];
1493 EXPORT_SYMBOL_NS(st_accel_get_settings
, "IIO_ST_SENSORS");
1495 int st_accel_common_probe(struct iio_dev
*indio_dev
)
1497 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
1498 struct device
*parent
= indio_dev
->dev
.parent
;
1499 struct st_sensors_platform_data
*pdata
= dev_get_platdata(parent
);
1502 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1503 indio_dev
->info
= &accel_info
;
1505 err
= st_sensors_verify_id(indio_dev
);
1509 adata
->num_data_channels
= ST_ACCEL_NUMBER_DATA_CHANNELS
;
1510 indio_dev
->channels
= adata
->sensor_settings
->ch
;
1511 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
1514 * First try specific ACPI methods to retrieve orientation then try the
1517 err
= apply_acpi_orientation(indio_dev
);
1519 err
= iio_read_mount_matrix(parent
, &adata
->mount_matrix
);
1524 adata
->current_fullscale
= &adata
->sensor_settings
->fs
.fs_avl
[0];
1525 adata
->odr
= adata
->sensor_settings
->odr
.odr_avl
[0].hz
;
1528 pdata
= (struct st_sensors_platform_data
*)&default_accel_pdata
;
1530 err
= st_sensors_init_sensor(indio_dev
, pdata
);
1534 err
= st_accel_allocate_ring(indio_dev
);
1538 if (adata
->irq
> 0) {
1539 err
= st_sensors_allocate_trigger(indio_dev
,
1540 ST_ACCEL_TRIGGER_OPS
);
1545 return devm_iio_device_register(parent
, indio_dev
);
1547 EXPORT_SYMBOL_NS(st_accel_common_probe
, "IIO_ST_SENSORS");
1549 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1550 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1551 MODULE_LICENSE("GPL v2");
1552 MODULE_IMPORT_NS("IIO_ST_SENSORS");