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/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>
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
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
[] = {
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
,
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, },
124 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
127 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
128 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
135 .num
= ST_ACCEL_FS_AVL_2G
,
137 .gain
= IIO_G_TO_M_S_2(1000),
140 .num
= ST_ACCEL_FS_AVL_4G
,
142 .gain
= IIO_G_TO_M_S_2(2000),
145 .num
= ST_ACCEL_FS_AVL_8G
,
147 .gain
= IIO_G_TO_M_S_2(4000),
150 .num
= ST_ACCEL_FS_AVL_16G
,
152 .gain
= IIO_G_TO_M_S_2(12000),
168 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
176 .multi_read_bit
= true,
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
,
193 { .hz
= 50, .value
= 0x00, },
194 { .hz
= 100, .value
= 0x01, },
195 { .hz
= 400, .value
= 0x02, },
196 { .hz
= 1000, .value
= 0x03, },
202 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
203 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
206 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
207 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
214 .num
= ST_ACCEL_FS_AVL_2G
,
216 .gain
= IIO_G_TO_M_S_2(1000),
219 .num
= ST_ACCEL_FS_AVL_4G
,
221 .gain
= IIO_G_TO_M_S_2(2000),
224 .num
= ST_ACCEL_FS_AVL_8G
,
226 .gain
= IIO_G_TO_M_S_2(3900),
250 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
258 .multi_read_bit
= true,
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
,
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, },
287 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
290 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
291 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
298 .num
= ST_ACCEL_FS_AVL_2G
,
300 .gain
= IIO_G_TO_M_S_2(61),
303 .num
= ST_ACCEL_FS_AVL_4G
,
305 .gain
= IIO_G_TO_M_S_2(122),
308 .num
= ST_ACCEL_FS_AVL_6G
,
310 .gain
= IIO_G_TO_M_S_2(183),
313 .num
= ST_ACCEL_FS_AVL_8G
,
315 .gain
= IIO_G_TO_M_S_2(244),
318 .num
= ST_ACCEL_FS_AVL_16G
,
320 .gain
= IIO_G_TO_M_S_2(732),
336 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
348 .multi_read_bit
= false,
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
,
360 .mask
= 0x30, /* DF1 and DF0 */
362 { .hz
= 40, .value
= 0x00, },
363 { .hz
= 160, .value
= 0x01, },
364 { .hz
= 640, .value
= 0x02, },
365 { .hz
= 2560, .value
= 0x03, },
371 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
372 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
375 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
376 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
383 .num
= ST_ACCEL_FS_AVL_2G
,
385 .gain
= IIO_G_TO_M_S_2(1000),
388 .num
= ST_ACCEL_FS_AVL_6G
,
390 .gain
= IIO_G_TO_M_S_2(3000),
399 * Data Alignment Setting - needs to be set to get
400 * left-justified data like all other sensors.
412 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
420 .multi_read_bit
= true,
421 .bootime
= 2, /* guess */
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
,
434 { .hz
= 100, .value
= 0x00, },
435 { .hz
= 400, .value
= 0x01, },
441 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
442 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
445 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
446 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
452 * TODO: check these resulting gain settings, these are
453 * not in the datsheet
457 .num
= ST_ACCEL_FS_AVL_2G
,
459 .gain
= IIO_G_TO_M_S_2(18000),
462 .num
= ST_ACCEL_FS_AVL_8G
,
464 .gain
= IIO_G_TO_M_S_2(72000),
484 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
492 .multi_read_bit
= false,
493 .bootime
= 2, /* guess */
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
,
506 { .hz
= 50, .value
= 0x00, },
507 { .hz
= 100, .value
= 0x01, },
508 { .hz
= 400, .value
= 0x02, },
509 { .hz
= 1000, .value
= 0x03, },
515 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
516 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
519 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
520 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
527 .num
= ST_ACCEL_FS_AVL_100G
,
529 .gain
= IIO_G_TO_M_S_2(49000),
532 .num
= ST_ACCEL_FS_AVL_200G
,
534 .gain
= IIO_G_TO_M_S_2(98000),
537 .num
= ST_ACCEL_FS_AVL_400G
,
539 .gain
= IIO_G_TO_M_S_2(195000),
563 .multi_read_bit
= true,
567 /* No WAI register present */
568 .sensors_supported
= {
569 [0] = LIS3L02DQ_ACCEL_DEV_NAME
,
571 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
576 { .hz
= 280, .value
= 0x00, },
577 { .hz
= 560, .value
= 0x01, },
578 { .hz
= 1120, .value
= 0x02, },
579 { .hz
= 4480, .value
= 0x03, },
585 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
586 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
589 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
590 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
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.
612 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
620 .multi_read_bit
= false,
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
,
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, },
647 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
650 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
651 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
658 .num
= ST_ACCEL_FS_AVL_2G
,
660 .gain
= IIO_G_TO_M_S_2(15600),
663 .num
= ST_ACCEL_FS_AVL_4G
,
665 .gain
= IIO_G_TO_M_S_2(31200),
668 .num
= ST_ACCEL_FS_AVL_8G
,
670 .gain
= IIO_G_TO_M_S_2(62500),
673 .num
= ST_ACCEL_FS_AVL_16G
,
675 .gain
= IIO_G_TO_M_S_2(187500),
687 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
695 .multi_read_bit
= true,
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
,
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, },
720 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
727 .num
= ST_ACCEL_FS_AVL_2G
,
729 .gain
= IIO_G_TO_M_S_2(976),
732 .num
= ST_ACCEL_FS_AVL_4G
,
734 .gain
= IIO_G_TO_M_S_2(1952),
737 .num
= ST_ACCEL_FS_AVL_8G
,
739 .gain
= IIO_G_TO_M_S_2(3904),
742 .num
= ST_ACCEL_FS_AVL_16G
,
744 .gain
= IIO_G_TO_M_S_2(7808),
768 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
776 .multi_read_bit
= false,
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
,
787 /* just ODR = 1100Hz available */
789 { .hz
= 1100, .value
= 0x00, },
795 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
796 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
801 .num
= ST_ACCEL_FS_AVL_2G
,
802 .gain
= IIO_G_TO_M_S_2(76),
824 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
828 .multi_read_bit
= false,
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
,
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, },
856 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
859 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
860 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
867 .num
= ST_ACCEL_FS_AVL_2G
,
869 .gain
= IIO_G_TO_M_S_2(15600),
872 .num
= ST_ACCEL_FS_AVL_4G
,
874 .gain
= IIO_G_TO_M_S_2(31200),
877 .num
= ST_ACCEL_FS_AVL_8G
,
879 .gain
= IIO_G_TO_M_S_2(62500),
882 .num
= ST_ACCEL_FS_AVL_16G
,
884 .gain
= IIO_G_TO_M_S_2(187500),
896 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
904 .multi_read_bit
= true,
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
)
914 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
917 case IIO_CHAN_INFO_RAW
:
918 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
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
:
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
)
944 case IIO_CHAN_INFO_SCALE
: {
947 gain
= val
* 1000000 + val2
;
948 err
= st_sensors_set_fullscale_by_gain(indio_dev
, gain
);
951 case IIO_CHAN_INFO_SAMP_FREQ
:
954 mutex_lock(&indio_dev
->mlock
);
955 err
= st_sensors_set_odr(indio_dev
, val
);
956 mutex_unlock(&indio_dev
->mlock
);
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
,
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)
992 #define ST_ACCEL_TRIGGER_OPS NULL
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
),
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
;
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] = {
1040 adev
= ACPI_COMPANION(adata
->dev
);
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
) {
1048 } else if (ACPI_FAILURE(status
)) {
1049 dev_warn(&indio_dev
->dev
, "failed to execute _ONT: %d\n",
1054 ont
= buffer
.pointer
;
1055 if (ont
->type
!= ACPI_TYPE_PACKAGE
|| ont
->package
.count
!= 6)
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
)
1067 val
= elements
[i
].integer
.value
;
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.
1086 for (i
= 0; i
< 3; i
++) {
1087 if (elements
[i
].type
!= ACPI_TYPE_INTEGER
)
1090 val
= elements
[i
].integer
.value
;
1091 if (val
!= 0 && val
!= 1)
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
),
1106 if (!adata
->mount_matrix
) {
1111 for (i
= 0; i
< 3; i
++) {
1112 for (j
= 0; j
< 3; j
++) {
1113 int matrix_val
= final_ont
[i
][j
];
1116 switch (matrix_val
) {
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
;
1138 dev_info(&indio_dev
->dev
, "computed mount matrix from ACPI\n");
1141 kfree(buffer
.pointer
);
1144 #else /* !CONFIG_ACPI */
1145 static int apply_acpi_orientation(struct iio_dev
*indio_dev
,
1146 struct iio_chan_spec
*channels
)
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
));
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
;
1179 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1180 indio_dev
->info
= &accel_info
;
1182 err
= st_sensors_power_enable(indio_dev
);
1186 err
= st_sensors_verify_id(indio_dev
);
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
);
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
;
1212 pdata
= (struct st_sensors_platform_data
*)&default_accel_pdata
;
1214 err
= st_sensors_init_sensor(indio_dev
, pdata
);
1216 goto st_accel_power_off
;
1218 err
= st_accel_allocate_ring(indio_dev
);
1220 goto st_accel_power_off
;
1222 if (adata
->irq
> 0) {
1223 err
= st_sensors_allocate_trigger(indio_dev
,
1224 ST_ACCEL_TRIGGER_OPS
);
1226 goto st_accel_probe_trigger_error
;
1229 err
= iio_device_register(indio_dev
);
1231 goto st_accel_device_register_error
;
1233 dev_info(&indio_dev
->dev
, "registered accelerometer %s\n",
1238 st_accel_device_register_error
:
1240 st_sensors_deallocate_trigger(indio_dev
);
1241 st_accel_probe_trigger_error
:
1242 st_accel_deallocate_ring(indio_dev
);
1244 st_sensors_power_disable(indio_dev
);
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
);
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");