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/slab.h>
13 #include <linux/acpi.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/mutex.h>
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/gpio.h>
20 #include <linux/irq.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/buffer.h>
26 #include <linux/iio/common/st_sensors.h>
29 #define ST_ACCEL_NUMBER_DATA_CHANNELS 3
31 /* DEFAULT VALUE FOR SENSORS */
32 #define ST_ACCEL_DEFAULT_OUT_X_L_ADDR 0x28
33 #define ST_ACCEL_DEFAULT_OUT_Y_L_ADDR 0x2a
34 #define ST_ACCEL_DEFAULT_OUT_Z_L_ADDR 0x2c
37 #define ST_ACCEL_FS_AVL_2G 2
38 #define ST_ACCEL_FS_AVL_4G 4
39 #define ST_ACCEL_FS_AVL_6G 6
40 #define ST_ACCEL_FS_AVL_8G 8
41 #define ST_ACCEL_FS_AVL_16G 16
42 #define ST_ACCEL_FS_AVL_100G 100
43 #define ST_ACCEL_FS_AVL_200G 200
44 #define ST_ACCEL_FS_AVL_400G 400
46 static const struct iio_chan_spec st_accel_8bit_channels
[] = {
47 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
48 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
49 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 8, 8,
50 ST_ACCEL_DEFAULT_OUT_X_L_ADDR
+1),
51 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
52 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
53 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 8, 8,
54 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR
+1),
55 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
56 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
57 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 8, 8,
58 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR
+1),
59 IIO_CHAN_SOFT_TIMESTAMP(3)
62 static const struct iio_chan_spec st_accel_12bit_channels
[] = {
63 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
64 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
65 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 12, 16,
66 ST_ACCEL_DEFAULT_OUT_X_L_ADDR
),
67 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
68 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
69 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 12, 16,
70 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR
),
71 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
72 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
73 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 12, 16,
74 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR
),
75 IIO_CHAN_SOFT_TIMESTAMP(3)
78 static const struct iio_chan_spec st_accel_16bit_channels
[] = {
79 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
80 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
81 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
82 ST_ACCEL_DEFAULT_OUT_X_L_ADDR
),
83 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
84 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
85 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
86 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR
),
87 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
88 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
89 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
90 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR
),
91 IIO_CHAN_SOFT_TIMESTAMP(3)
94 static const struct st_sensor_settings st_accel_sensors_settings
[] = {
97 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
98 .sensors_supported
= {
99 [0] = LIS3DH_ACCEL_DEV_NAME
,
100 [1] = LSM303DLHC_ACCEL_DEV_NAME
,
101 [2] = LSM330D_ACCEL_DEV_NAME
,
102 [3] = LSM330DL_ACCEL_DEV_NAME
,
103 [4] = LSM330DLC_ACCEL_DEV_NAME
,
104 [5] = LSM303AGR_ACCEL_DEV_NAME
,
105 [6] = LIS2DH12_ACCEL_DEV_NAME
,
106 [7] = LIS3DE_ACCEL_DEV_NAME
,
108 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
113 { .hz
= 1, .value
= 0x01, },
114 { .hz
= 10, .value
= 0x02, },
115 { .hz
= 25, .value
= 0x03, },
116 { .hz
= 50, .value
= 0x04, },
117 { .hz
= 100, .value
= 0x05, },
118 { .hz
= 200, .value
= 0x06, },
119 { .hz
= 400, .value
= 0x07, },
120 { .hz
= 1600, .value
= 0x08, },
126 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
129 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
130 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
137 .num
= ST_ACCEL_FS_AVL_2G
,
139 .gain
= IIO_G_TO_M_S_2(1000),
142 .num
= ST_ACCEL_FS_AVL_4G
,
144 .gain
= IIO_G_TO_M_S_2(2000),
147 .num
= ST_ACCEL_FS_AVL_8G
,
149 .gain
= IIO_G_TO_M_S_2(4000),
152 .num
= ST_ACCEL_FS_AVL_16G
,
154 .gain
= IIO_G_TO_M_S_2(12000),
170 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
178 .multi_read_bit
= true,
183 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
184 .sensors_supported
= {
185 [0] = LIS331DLH_ACCEL_DEV_NAME
,
186 [1] = LSM303DL_ACCEL_DEV_NAME
,
187 [2] = LSM303DLH_ACCEL_DEV_NAME
,
188 [3] = LSM303DLM_ACCEL_DEV_NAME
,
190 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
195 { .hz
= 50, .value
= 0x00, },
196 { .hz
= 100, .value
= 0x01, },
197 { .hz
= 400, .value
= 0x02, },
198 { .hz
= 1000, .value
= 0x03, },
204 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
205 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
208 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
209 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
216 .num
= ST_ACCEL_FS_AVL_2G
,
218 .gain
= IIO_G_TO_M_S_2(1000),
221 .num
= ST_ACCEL_FS_AVL_4G
,
223 .gain
= IIO_G_TO_M_S_2(2000),
226 .num
= ST_ACCEL_FS_AVL_8G
,
228 .gain
= IIO_G_TO_M_S_2(3900),
252 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
260 .multi_read_bit
= true,
265 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
266 .sensors_supported
= {
267 [0] = LSM330_ACCEL_DEV_NAME
,
269 .ch
= (struct iio_chan_spec
*)st_accel_16bit_channels
,
274 { .hz
= 3, .value
= 0x01, },
275 { .hz
= 6, .value
= 0x02, },
276 { .hz
= 12, .value
= 0x03, },
277 { .hz
= 25, .value
= 0x04, },
278 { .hz
= 50, .value
= 0x05, },
279 { .hz
= 100, .value
= 0x06, },
280 { .hz
= 200, .value
= 0x07, },
281 { .hz
= 400, .value
= 0x08, },
282 { .hz
= 800, .value
= 0x09, },
283 { .hz
= 1600, .value
= 0x0a, },
289 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
292 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
293 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
300 .num
= ST_ACCEL_FS_AVL_2G
,
302 .gain
= IIO_G_TO_M_S_2(61),
305 .num
= ST_ACCEL_FS_AVL_4G
,
307 .gain
= IIO_G_TO_M_S_2(122),
310 .num
= ST_ACCEL_FS_AVL_6G
,
312 .gain
= IIO_G_TO_M_S_2(183),
315 .num
= ST_ACCEL_FS_AVL_8G
,
317 .gain
= IIO_G_TO_M_S_2(244),
320 .num
= ST_ACCEL_FS_AVL_16G
,
322 .gain
= IIO_G_TO_M_S_2(732),
338 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
350 .multi_read_bit
= false,
355 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
356 .sensors_supported
= {
357 [0] = LIS3LV02DL_ACCEL_DEV_NAME
,
359 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
362 .mask
= 0x30, /* DF1 and DF0 */
364 { .hz
= 40, .value
= 0x00, },
365 { .hz
= 160, .value
= 0x01, },
366 { .hz
= 640, .value
= 0x02, },
367 { .hz
= 2560, .value
= 0x03, },
373 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
374 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
377 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
378 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
385 .num
= ST_ACCEL_FS_AVL_2G
,
387 .gain
= IIO_G_TO_M_S_2(1000),
390 .num
= ST_ACCEL_FS_AVL_6G
,
392 .gain
= IIO_G_TO_M_S_2(3000),
401 * Data Alignment Setting - needs to be set to get
402 * left-justified data like all other sensors.
414 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
422 .multi_read_bit
= true,
423 .bootime
= 2, /* guess */
427 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
428 .sensors_supported
= {
429 [0] = LIS331DL_ACCEL_DEV_NAME
,
431 .ch
= (struct iio_chan_spec
*)st_accel_8bit_channels
,
436 { .hz
= 100, .value
= 0x00, },
437 { .hz
= 400, .value
= 0x01, },
443 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
444 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
447 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
448 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
454 * TODO: check these resulting gain settings, these are
455 * not in the datsheet
459 .num
= ST_ACCEL_FS_AVL_2G
,
461 .gain
= IIO_G_TO_M_S_2(18000),
464 .num
= ST_ACCEL_FS_AVL_8G
,
466 .gain
= IIO_G_TO_M_S_2(72000),
486 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
494 .multi_read_bit
= false,
495 .bootime
= 2, /* guess */
499 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
500 .sensors_supported
= {
501 [0] = H3LIS331DL_ACCEL_DEV_NAME
,
503 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
508 { .hz
= 50, .value
= 0x00, },
509 { .hz
= 100, .value
= 0x01, },
510 { .hz
= 400, .value
= 0x02, },
511 { .hz
= 1000, .value
= 0x03, },
517 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
518 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
521 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
522 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
529 .num
= ST_ACCEL_FS_AVL_100G
,
531 .gain
= IIO_G_TO_M_S_2(49000),
534 .num
= ST_ACCEL_FS_AVL_200G
,
536 .gain
= IIO_G_TO_M_S_2(98000),
539 .num
= ST_ACCEL_FS_AVL_400G
,
541 .gain
= IIO_G_TO_M_S_2(195000),
565 .multi_read_bit
= true,
569 /* No WAI register present */
570 .sensors_supported
= {
571 [0] = LIS3L02DQ_ACCEL_DEV_NAME
,
573 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
578 { .hz
= 280, .value
= 0x00, },
579 { .hz
= 560, .value
= 0x01, },
580 { .hz
= 1120, .value
= 0x02, },
581 { .hz
= 4480, .value
= 0x03, },
587 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
588 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
591 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
592 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
597 .num
= ST_ACCEL_FS_AVL_2G
,
598 .gain
= IIO_G_TO_M_S_2(488),
603 * The part has a BDU bit but if set the data is never
604 * updated so don't set it.
614 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
622 .multi_read_bit
= false,
627 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
628 .sensors_supported
= {
629 [0] = LNG2DM_ACCEL_DEV_NAME
,
631 .ch
= (struct iio_chan_spec
*)st_accel_8bit_channels
,
636 { .hz
= 1, .value
= 0x01, },
637 { .hz
= 10, .value
= 0x02, },
638 { .hz
= 25, .value
= 0x03, },
639 { .hz
= 50, .value
= 0x04, },
640 { .hz
= 100, .value
= 0x05, },
641 { .hz
= 200, .value
= 0x06, },
642 { .hz
= 400, .value
= 0x07, },
643 { .hz
= 1600, .value
= 0x08, },
649 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
652 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
653 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
660 .num
= ST_ACCEL_FS_AVL_2G
,
662 .gain
= IIO_G_TO_M_S_2(15600),
665 .num
= ST_ACCEL_FS_AVL_4G
,
667 .gain
= IIO_G_TO_M_S_2(31200),
670 .num
= ST_ACCEL_FS_AVL_8G
,
672 .gain
= IIO_G_TO_M_S_2(62500),
675 .num
= ST_ACCEL_FS_AVL_16G
,
677 .gain
= IIO_G_TO_M_S_2(187500),
689 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
697 .multi_read_bit
= true,
702 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
703 .sensors_supported
= {
704 [0] = LIS2DW12_ACCEL_DEV_NAME
,
706 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
711 { .hz
= 1, .value
= 0x01, },
712 { .hz
= 12, .value
= 0x02, },
713 { .hz
= 25, .value
= 0x03, },
714 { .hz
= 50, .value
= 0x04, },
715 { .hz
= 100, .value
= 0x05, },
716 { .hz
= 200, .value
= 0x06, },
722 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
729 .num
= ST_ACCEL_FS_AVL_2G
,
731 .gain
= IIO_G_TO_M_S_2(976),
734 .num
= ST_ACCEL_FS_AVL_4G
,
736 .gain
= IIO_G_TO_M_S_2(1952),
739 .num
= ST_ACCEL_FS_AVL_8G
,
741 .gain
= IIO_G_TO_M_S_2(3904),
744 .num
= ST_ACCEL_FS_AVL_16G
,
746 .gain
= IIO_G_TO_M_S_2(7808),
770 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
778 .multi_read_bit
= false,
783 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
784 .sensors_supported
= {
785 [0] = LIS3DHH_ACCEL_DEV_NAME
,
787 .ch
= (struct iio_chan_spec
*)st_accel_16bit_channels
,
789 /* just ODR = 1100Hz available */
791 { .hz
= 1100, .value
= 0x00, },
797 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
798 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
803 .num
= ST_ACCEL_FS_AVL_2G
,
804 .gain
= IIO_G_TO_M_S_2(76),
826 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
830 .multi_read_bit
= false,
835 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
836 .sensors_supported
= {
837 [0] = LIS2DE12_ACCEL_DEV_NAME
,
839 .ch
= (struct iio_chan_spec
*)st_accel_8bit_channels
,
844 { .hz
= 1, .value
= 0x01, },
845 { .hz
= 10, .value
= 0x02, },
846 { .hz
= 25, .value
= 0x03, },
847 { .hz
= 50, .value
= 0x04, },
848 { .hz
= 100, .value
= 0x05, },
849 { .hz
= 200, .value
= 0x06, },
850 { .hz
= 400, .value
= 0x07, },
851 { .hz
= 1620, .value
= 0x08, },
852 { .hz
= 5376, .value
= 0x09, },
858 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
861 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
862 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
869 .num
= ST_ACCEL_FS_AVL_2G
,
871 .gain
= IIO_G_TO_M_S_2(15600),
874 .num
= ST_ACCEL_FS_AVL_4G
,
876 .gain
= IIO_G_TO_M_S_2(31200),
879 .num
= ST_ACCEL_FS_AVL_8G
,
881 .gain
= IIO_G_TO_M_S_2(62500),
884 .num
= ST_ACCEL_FS_AVL_16G
,
886 .gain
= IIO_G_TO_M_S_2(187500),
898 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
906 .multi_read_bit
= true,
911 static int st_accel_read_raw(struct iio_dev
*indio_dev
,
912 struct iio_chan_spec
const *ch
, int *val
,
913 int *val2
, long mask
)
916 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
919 case IIO_CHAN_INFO_RAW
:
920 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
925 case IIO_CHAN_INFO_SCALE
:
926 *val
= adata
->current_fullscale
->gain
/ 1000000;
927 *val2
= adata
->current_fullscale
->gain
% 1000000;
928 return IIO_VAL_INT_PLUS_MICRO
;
929 case IIO_CHAN_INFO_SAMP_FREQ
:
940 static int st_accel_write_raw(struct iio_dev
*indio_dev
,
941 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
946 case IIO_CHAN_INFO_SCALE
: {
949 gain
= val
* 1000000 + val2
;
950 err
= st_sensors_set_fullscale_by_gain(indio_dev
, gain
);
953 case IIO_CHAN_INFO_SAMP_FREQ
:
956 mutex_lock(&indio_dev
->mlock
);
957 err
= st_sensors_set_odr(indio_dev
, val
);
958 mutex_unlock(&indio_dev
->mlock
);
967 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
968 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available
);
970 static struct attribute
*st_accel_attributes
[] = {
971 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
972 &iio_dev_attr_in_accel_scale_available
.dev_attr
.attr
,
976 static const struct attribute_group st_accel_attribute_group
= {
977 .attrs
= st_accel_attributes
,
980 static const struct iio_info accel_info
= {
981 .attrs
= &st_accel_attribute_group
,
982 .read_raw
= &st_accel_read_raw
,
983 .write_raw
= &st_accel_write_raw
,
984 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
987 #ifdef CONFIG_IIO_TRIGGER
988 static const struct iio_trigger_ops st_accel_trigger_ops
= {
989 .set_trigger_state
= ST_ACCEL_TRIGGER_SET_STATE
,
990 .validate_device
= st_sensors_validate_device
,
992 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
994 #define ST_ACCEL_TRIGGER_OPS NULL
997 static const struct iio_mount_matrix
*
998 get_mount_matrix(const struct iio_dev
*indio_dev
,
999 const struct iio_chan_spec
*chan
)
1001 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
1003 return adata
->mount_matrix
;
1006 static const struct iio_chan_spec_ext_info mount_matrix_ext_info
[] = {
1007 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL
, get_mount_matrix
),
1011 /* Read ST-specific _ONT orientation data from ACPI and generate an
1012 * appropriate mount matrix.
1014 static int apply_acpi_orientation(struct iio_dev
*indio_dev
,
1015 struct iio_chan_spec
*channels
)
1018 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
1019 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
1020 struct acpi_device
*adev
;
1021 union acpi_object
*ont
;
1022 union acpi_object
*elements
;
1027 int final_ont
[3][3] = { { 0 }, };
1029 /* For some reason, ST's _ONT translation does not apply directly
1030 * to the data read from the sensor. Another translation must be
1031 * performed first, as described by the matrix below. Perhaps
1032 * ST required this specific translation for the first product
1033 * where the device was mounted?
1035 const int default_ont
[3][3] = {
1042 adev
= ACPI_COMPANION(adata
->dev
);
1046 /* Read _ONT data, which should be a package of 6 integers. */
1047 status
= acpi_evaluate_object(adev
->handle
, "_ONT", NULL
, &buffer
);
1048 if (status
== AE_NOT_FOUND
) {
1050 } else if (ACPI_FAILURE(status
)) {
1051 dev_warn(&indio_dev
->dev
, "failed to execute _ONT: %d\n",
1056 ont
= buffer
.pointer
;
1057 if (ont
->type
!= ACPI_TYPE_PACKAGE
|| ont
->package
.count
!= 6)
1060 /* The first 3 integers provide axis order information.
1061 * e.g. 0 1 2 would indicate normal X,Y,Z ordering.
1062 * e.g. 1 0 2 indicates that data arrives in order Y,X,Z.
1064 elements
= ont
->package
.elements
;
1065 for (i
= 0; i
< 3; i
++) {
1066 if (elements
[i
].type
!= ACPI_TYPE_INTEGER
)
1069 val
= elements
[i
].integer
.value
;
1073 /* Avoiding full matrix multiplication, we simply reorder the
1074 * columns in the default_ont matrix according to the
1075 * ordering provided by _ONT.
1077 final_ont
[0][i
] = default_ont
[0][val
];
1078 final_ont
[1][i
] = default_ont
[1][val
];
1079 final_ont
[2][i
] = default_ont
[2][val
];
1082 /* The final 3 integers provide sign flip information.
1083 * 0 means no change, 1 means flip.
1084 * e.g. 0 0 1 means that Z data should be sign-flipped.
1085 * This is applied after the axis reordering from above.
1088 for (i
= 0; i
< 3; i
++) {
1089 if (elements
[i
].type
!= ACPI_TYPE_INTEGER
)
1092 val
= elements
[i
].integer
.value
;
1093 if (val
!= 0 && val
!= 1)
1098 /* Flip the values in the indicated column */
1099 final_ont
[0][i
] *= -1;
1100 final_ont
[1][i
] *= -1;
1101 final_ont
[2][i
] *= -1;
1104 /* Convert our integer matrix to a string-based iio_mount_matrix */
1105 adata
->mount_matrix
= devm_kmalloc(&indio_dev
->dev
,
1106 sizeof(*adata
->mount_matrix
),
1108 if (!adata
->mount_matrix
) {
1113 for (i
= 0; i
< 3; i
++) {
1114 for (j
= 0; j
< 3; j
++) {
1115 int matrix_val
= final_ont
[i
][j
];
1118 switch (matrix_val
) {
1131 adata
->mount_matrix
->rotation
[i
* 3 + j
] = str_value
;
1135 /* Expose the mount matrix via ext_info */
1136 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
1137 channels
[i
].ext_info
= mount_matrix_ext_info
;
1140 dev_info(&indio_dev
->dev
, "computed mount matrix from ACPI\n");
1143 kfree(buffer
.pointer
);
1145 #else /* !CONFIG_ACPI */
1150 int st_accel_common_probe(struct iio_dev
*indio_dev
)
1152 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
1153 struct st_sensors_platform_data
*pdata
=
1154 (struct st_sensors_platform_data
*)adata
->dev
->platform_data
;
1155 int irq
= adata
->get_irq_data_ready(indio_dev
);
1156 struct iio_chan_spec
*channels
;
1157 size_t channels_size
;
1160 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1161 indio_dev
->info
= &accel_info
;
1162 mutex_init(&adata
->tb
.buf_lock
);
1164 err
= st_sensors_power_enable(indio_dev
);
1168 err
= st_sensors_check_device_support(indio_dev
,
1169 ARRAY_SIZE(st_accel_sensors_settings
),
1170 st_accel_sensors_settings
);
1172 goto st_accel_power_off
;
1174 adata
->num_data_channels
= ST_ACCEL_NUMBER_DATA_CHANNELS
;
1175 adata
->multiread_bit
= adata
->sensor_settings
->multi_read_bit
;
1176 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
1178 channels_size
= indio_dev
->num_channels
* sizeof(struct iio_chan_spec
);
1179 channels
= devm_kmemdup(&indio_dev
->dev
,
1180 adata
->sensor_settings
->ch
,
1181 channels_size
, GFP_KERNEL
);
1184 goto st_accel_power_off
;
1187 if (apply_acpi_orientation(indio_dev
, channels
))
1188 dev_warn(&indio_dev
->dev
,
1189 "failed to apply ACPI orientation data: %d\n", err
);
1191 indio_dev
->channels
= channels
;
1192 adata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
1193 &adata
->sensor_settings
->fs
.fs_avl
[0];
1194 adata
->odr
= adata
->sensor_settings
->odr
.odr_avl
[0].hz
;
1197 pdata
= (struct st_sensors_platform_data
*)&default_accel_pdata
;
1199 err
= st_sensors_init_sensor(indio_dev
, pdata
);
1201 goto st_accel_power_off
;
1203 err
= st_accel_allocate_ring(indio_dev
);
1205 goto st_accel_power_off
;
1208 err
= st_sensors_allocate_trigger(indio_dev
,
1209 ST_ACCEL_TRIGGER_OPS
);
1211 goto st_accel_probe_trigger_error
;
1214 err
= iio_device_register(indio_dev
);
1216 goto st_accel_device_register_error
;
1218 dev_info(&indio_dev
->dev
, "registered accelerometer %s\n",
1223 st_accel_device_register_error
:
1225 st_sensors_deallocate_trigger(indio_dev
);
1226 st_accel_probe_trigger_error
:
1227 st_accel_deallocate_ring(indio_dev
);
1229 st_sensors_power_disable(indio_dev
);
1233 EXPORT_SYMBOL(st_accel_common_probe
);
1235 void st_accel_common_remove(struct iio_dev
*indio_dev
)
1237 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
1239 st_sensors_power_disable(indio_dev
);
1241 iio_device_unregister(indio_dev
);
1242 if (adata
->get_irq_data_ready(indio_dev
) > 0)
1243 st_sensors_deallocate_trigger(indio_dev
);
1245 st_accel_deallocate_ring(indio_dev
);
1247 EXPORT_SYMBOL(st_accel_common_remove
);
1249 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1250 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1251 MODULE_LICENSE("GPL v2");