2 * STMicroelectronics accelerometers driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/acpi.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/mutex.h>
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/gpio.h>
21 #include <linux/irq.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/buffer.h>
27 #include <linux/iio/common/st_sensors.h>
30 #define ST_ACCEL_NUMBER_DATA_CHANNELS 3
32 /* DEFAULT VALUE FOR SENSORS */
33 #define ST_ACCEL_DEFAULT_OUT_X_L_ADDR 0x28
34 #define ST_ACCEL_DEFAULT_OUT_Y_L_ADDR 0x2a
35 #define ST_ACCEL_DEFAULT_OUT_Z_L_ADDR 0x2c
38 #define ST_ACCEL_FS_AVL_2G 2
39 #define ST_ACCEL_FS_AVL_4G 4
40 #define ST_ACCEL_FS_AVL_6G 6
41 #define ST_ACCEL_FS_AVL_8G 8
42 #define ST_ACCEL_FS_AVL_16G 16
43 #define ST_ACCEL_FS_AVL_100G 100
44 #define ST_ACCEL_FS_AVL_200G 200
45 #define ST_ACCEL_FS_AVL_400G 400
47 static const struct iio_chan_spec st_accel_8bit_channels
[] = {
48 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
49 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
50 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 8, 8,
51 ST_ACCEL_DEFAULT_OUT_X_L_ADDR
+1),
52 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
53 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
54 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 8, 8,
55 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR
+1),
56 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
57 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
58 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 8, 8,
59 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR
+1),
60 IIO_CHAN_SOFT_TIMESTAMP(3)
63 static const struct iio_chan_spec st_accel_12bit_channels
[] = {
64 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
65 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
66 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 12, 16,
67 ST_ACCEL_DEFAULT_OUT_X_L_ADDR
),
68 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
69 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
70 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 12, 16,
71 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR
),
72 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
73 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
74 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 12, 16,
75 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR
),
76 IIO_CHAN_SOFT_TIMESTAMP(3)
79 static const struct iio_chan_spec st_accel_16bit_channels
[] = {
80 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
81 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
82 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
83 ST_ACCEL_DEFAULT_OUT_X_L_ADDR
),
84 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
85 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
86 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
87 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR
),
88 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL
,
89 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
90 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
91 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR
),
92 IIO_CHAN_SOFT_TIMESTAMP(3)
95 static const struct st_sensor_settings st_accel_sensors_settings
[] = {
98 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
99 .sensors_supported
= {
100 [0] = LIS3DH_ACCEL_DEV_NAME
,
101 [1] = LSM303DLHC_ACCEL_DEV_NAME
,
102 [2] = LSM330D_ACCEL_DEV_NAME
,
103 [3] = LSM330DL_ACCEL_DEV_NAME
,
104 [4] = LSM330DLC_ACCEL_DEV_NAME
,
105 [5] = LSM303AGR_ACCEL_DEV_NAME
,
106 [6] = LIS2DH12_ACCEL_DEV_NAME
,
107 [7] = LIS3DE_ACCEL_DEV_NAME
,
109 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
114 { .hz
= 1, .value
= 0x01, },
115 { .hz
= 10, .value
= 0x02, },
116 { .hz
= 25, .value
= 0x03, },
117 { .hz
= 50, .value
= 0x04, },
118 { .hz
= 100, .value
= 0x05, },
119 { .hz
= 200, .value
= 0x06, },
120 { .hz
= 400, .value
= 0x07, },
121 { .hz
= 1600, .value
= 0x08, },
127 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
130 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
131 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
138 .num
= ST_ACCEL_FS_AVL_2G
,
140 .gain
= IIO_G_TO_M_S_2(1000),
143 .num
= ST_ACCEL_FS_AVL_4G
,
145 .gain
= IIO_G_TO_M_S_2(2000),
148 .num
= ST_ACCEL_FS_AVL_8G
,
150 .gain
= IIO_G_TO_M_S_2(4000),
153 .num
= ST_ACCEL_FS_AVL_16G
,
155 .gain
= IIO_G_TO_M_S_2(12000),
171 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
179 .multi_read_bit
= true,
184 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
185 .sensors_supported
= {
186 [0] = LIS331DLH_ACCEL_DEV_NAME
,
187 [1] = LSM303DL_ACCEL_DEV_NAME
,
188 [2] = LSM303DLH_ACCEL_DEV_NAME
,
189 [3] = LSM303DLM_ACCEL_DEV_NAME
,
191 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
196 { .hz
= 50, .value
= 0x00, },
197 { .hz
= 100, .value
= 0x01, },
198 { .hz
= 400, .value
= 0x02, },
199 { .hz
= 1000, .value
= 0x03, },
205 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
206 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
209 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
210 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
217 .num
= ST_ACCEL_FS_AVL_2G
,
219 .gain
= IIO_G_TO_M_S_2(1000),
222 .num
= ST_ACCEL_FS_AVL_4G
,
224 .gain
= IIO_G_TO_M_S_2(2000),
227 .num
= ST_ACCEL_FS_AVL_8G
,
229 .gain
= IIO_G_TO_M_S_2(3900),
253 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
261 .multi_read_bit
= true,
266 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
267 .sensors_supported
= {
268 [0] = LSM330_ACCEL_DEV_NAME
,
270 .ch
= (struct iio_chan_spec
*)st_accel_16bit_channels
,
275 { .hz
= 3, .value
= 0x01, },
276 { .hz
= 6, .value
= 0x02, },
277 { .hz
= 12, .value
= 0x03, },
278 { .hz
= 25, .value
= 0x04, },
279 { .hz
= 50, .value
= 0x05, },
280 { .hz
= 100, .value
= 0x06, },
281 { .hz
= 200, .value
= 0x07, },
282 { .hz
= 400, .value
= 0x08, },
283 { .hz
= 800, .value
= 0x09, },
284 { .hz
= 1600, .value
= 0x0a, },
290 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
293 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
294 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
301 .num
= ST_ACCEL_FS_AVL_2G
,
303 .gain
= IIO_G_TO_M_S_2(61),
306 .num
= ST_ACCEL_FS_AVL_4G
,
308 .gain
= IIO_G_TO_M_S_2(122),
311 .num
= ST_ACCEL_FS_AVL_6G
,
313 .gain
= IIO_G_TO_M_S_2(183),
316 .num
= ST_ACCEL_FS_AVL_8G
,
318 .gain
= IIO_G_TO_M_S_2(244),
321 .num
= ST_ACCEL_FS_AVL_16G
,
323 .gain
= IIO_G_TO_M_S_2(732),
339 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
351 .multi_read_bit
= false,
356 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
357 .sensors_supported
= {
358 [0] = LIS3LV02DL_ACCEL_DEV_NAME
,
360 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
363 .mask
= 0x30, /* DF1 and DF0 */
365 { .hz
= 40, .value
= 0x00, },
366 { .hz
= 160, .value
= 0x01, },
367 { .hz
= 640, .value
= 0x02, },
368 { .hz
= 2560, .value
= 0x03, },
374 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
375 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
378 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
379 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
386 .num
= ST_ACCEL_FS_AVL_2G
,
388 .gain
= IIO_G_TO_M_S_2(1000),
391 .num
= ST_ACCEL_FS_AVL_6G
,
393 .gain
= IIO_G_TO_M_S_2(3000),
402 * Data Alignment Setting - needs to be set to get
403 * left-justified data like all other sensors.
415 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
423 .multi_read_bit
= true,
424 .bootime
= 2, /* guess */
428 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
429 .sensors_supported
= {
430 [0] = LIS331DL_ACCEL_DEV_NAME
,
432 .ch
= (struct iio_chan_spec
*)st_accel_8bit_channels
,
437 { .hz
= 100, .value
= 0x00, },
438 { .hz
= 400, .value
= 0x01, },
444 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
445 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
448 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
449 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
455 * TODO: check these resulting gain settings, these are
456 * not in the datsheet
460 .num
= ST_ACCEL_FS_AVL_2G
,
462 .gain
= IIO_G_TO_M_S_2(18000),
465 .num
= ST_ACCEL_FS_AVL_8G
,
467 .gain
= IIO_G_TO_M_S_2(72000),
487 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
495 .multi_read_bit
= false,
496 .bootime
= 2, /* guess */
500 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
501 .sensors_supported
= {
502 [0] = H3LIS331DL_ACCEL_DEV_NAME
,
504 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
509 { .hz
= 50, .value
= 0x00, },
510 { .hz
= 100, .value
= 0x01, },
511 { .hz
= 400, .value
= 0x02, },
512 { .hz
= 1000, .value
= 0x03, },
518 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
519 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
522 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
523 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
530 .num
= ST_ACCEL_FS_AVL_100G
,
532 .gain
= IIO_G_TO_M_S_2(49000),
535 .num
= ST_ACCEL_FS_AVL_200G
,
537 .gain
= IIO_G_TO_M_S_2(98000),
540 .num
= ST_ACCEL_FS_AVL_400G
,
542 .gain
= IIO_G_TO_M_S_2(195000),
566 .multi_read_bit
= true,
570 /* No WAI register present */
571 .sensors_supported
= {
572 [0] = LIS3L02DQ_ACCEL_DEV_NAME
,
574 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
579 { .hz
= 280, .value
= 0x00, },
580 { .hz
= 560, .value
= 0x01, },
581 { .hz
= 1120, .value
= 0x02, },
582 { .hz
= 4480, .value
= 0x03, },
588 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
589 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
592 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
593 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
598 .num
= ST_ACCEL_FS_AVL_2G
,
599 .gain
= IIO_G_TO_M_S_2(488),
604 * The part has a BDU bit but if set the data is never
605 * updated so don't set it.
615 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
623 .multi_read_bit
= false,
628 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
629 .sensors_supported
= {
630 [0] = LNG2DM_ACCEL_DEV_NAME
,
632 .ch
= (struct iio_chan_spec
*)st_accel_8bit_channels
,
637 { .hz
= 1, .value
= 0x01, },
638 { .hz
= 10, .value
= 0x02, },
639 { .hz
= 25, .value
= 0x03, },
640 { .hz
= 50, .value
= 0x04, },
641 { .hz
= 100, .value
= 0x05, },
642 { .hz
= 200, .value
= 0x06, },
643 { .hz
= 400, .value
= 0x07, },
644 { .hz
= 1600, .value
= 0x08, },
650 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
653 .addr
= ST_SENSORS_DEFAULT_AXIS_ADDR
,
654 .mask
= ST_SENSORS_DEFAULT_AXIS_MASK
,
661 .num
= ST_ACCEL_FS_AVL_2G
,
663 .gain
= IIO_G_TO_M_S_2(15600),
666 .num
= ST_ACCEL_FS_AVL_4G
,
668 .gain
= IIO_G_TO_M_S_2(31200),
671 .num
= ST_ACCEL_FS_AVL_8G
,
673 .gain
= IIO_G_TO_M_S_2(62500),
676 .num
= ST_ACCEL_FS_AVL_16G
,
678 .gain
= IIO_G_TO_M_S_2(187500),
690 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
698 .multi_read_bit
= true,
703 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
704 .sensors_supported
= {
705 [0] = LIS2DW12_ACCEL_DEV_NAME
,
707 .ch
= (struct iio_chan_spec
*)st_accel_12bit_channels
,
712 { .hz
= 1, .value
= 0x01, },
713 { .hz
= 12, .value
= 0x02, },
714 { .hz
= 25, .value
= 0x03, },
715 { .hz
= 50, .value
= 0x04, },
716 { .hz
= 100, .value
= 0x05, },
717 { .hz
= 200, .value
= 0x06, },
723 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
730 .num
= ST_ACCEL_FS_AVL_2G
,
732 .gain
= IIO_G_TO_M_S_2(976),
735 .num
= ST_ACCEL_FS_AVL_4G
,
737 .gain
= IIO_G_TO_M_S_2(1952),
740 .num
= ST_ACCEL_FS_AVL_8G
,
742 .gain
= IIO_G_TO_M_S_2(3904),
745 .num
= ST_ACCEL_FS_AVL_16G
,
747 .gain
= IIO_G_TO_M_S_2(7808),
771 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
779 .multi_read_bit
= false,
784 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
785 .sensors_supported
= {
786 [0] = LIS3DHH_ACCEL_DEV_NAME
,
788 .ch
= (struct iio_chan_spec
*)st_accel_16bit_channels
,
790 /* just ODR = 1100Hz available */
792 { .hz
= 1100, .value
= 0x00, },
798 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
799 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
804 .num
= ST_ACCEL_FS_AVL_2G
,
805 .gain
= IIO_G_TO_M_S_2(76),
827 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
831 .multi_read_bit
= false,
836 static int st_accel_read_raw(struct iio_dev
*indio_dev
,
837 struct iio_chan_spec
const *ch
, int *val
,
838 int *val2
, long mask
)
841 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
844 case IIO_CHAN_INFO_RAW
:
845 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
850 case IIO_CHAN_INFO_SCALE
:
851 *val
= adata
->current_fullscale
->gain
/ 1000000;
852 *val2
= adata
->current_fullscale
->gain
% 1000000;
853 return IIO_VAL_INT_PLUS_MICRO
;
854 case IIO_CHAN_INFO_SAMP_FREQ
:
865 static int st_accel_write_raw(struct iio_dev
*indio_dev
,
866 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
871 case IIO_CHAN_INFO_SCALE
: {
874 gain
= val
* 1000000 + val2
;
875 err
= st_sensors_set_fullscale_by_gain(indio_dev
, gain
);
878 case IIO_CHAN_INFO_SAMP_FREQ
:
881 mutex_lock(&indio_dev
->mlock
);
882 err
= st_sensors_set_odr(indio_dev
, val
);
883 mutex_unlock(&indio_dev
->mlock
);
892 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
893 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available
);
895 static struct attribute
*st_accel_attributes
[] = {
896 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
897 &iio_dev_attr_in_accel_scale_available
.dev_attr
.attr
,
901 static const struct attribute_group st_accel_attribute_group
= {
902 .attrs
= st_accel_attributes
,
905 static const struct iio_info accel_info
= {
906 .attrs
= &st_accel_attribute_group
,
907 .read_raw
= &st_accel_read_raw
,
908 .write_raw
= &st_accel_write_raw
,
909 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
912 #ifdef CONFIG_IIO_TRIGGER
913 static const struct iio_trigger_ops st_accel_trigger_ops
= {
914 .set_trigger_state
= ST_ACCEL_TRIGGER_SET_STATE
,
915 .validate_device
= st_sensors_validate_device
,
917 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops)
919 #define ST_ACCEL_TRIGGER_OPS NULL
922 static const struct iio_mount_matrix
*
923 get_mount_matrix(const struct iio_dev
*indio_dev
,
924 const struct iio_chan_spec
*chan
)
926 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
928 return adata
->mount_matrix
;
931 static const struct iio_chan_spec_ext_info mount_matrix_ext_info
[] = {
932 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL
, get_mount_matrix
),
936 /* Read ST-specific _ONT orientation data from ACPI and generate an
937 * appropriate mount matrix.
939 static int apply_acpi_orientation(struct iio_dev
*indio_dev
,
940 struct iio_chan_spec
*channels
)
943 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
944 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
945 struct acpi_device
*adev
;
946 union acpi_object
*ont
;
947 union acpi_object
*elements
;
952 int final_ont
[3][3] = { { 0 }, };
954 /* For some reason, ST's _ONT translation does not apply directly
955 * to the data read from the sensor. Another translation must be
956 * performed first, as described by the matrix below. Perhaps
957 * ST required this specific translation for the first product
958 * where the device was mounted?
960 const int default_ont
[3][3] = {
967 adev
= ACPI_COMPANION(adata
->dev
);
971 /* Read _ONT data, which should be a package of 6 integers. */
972 status
= acpi_evaluate_object(adev
->handle
, "_ONT", NULL
, &buffer
);
973 if (status
== AE_NOT_FOUND
) {
975 } else if (ACPI_FAILURE(status
)) {
976 dev_warn(&indio_dev
->dev
, "failed to execute _ONT: %d\n",
981 ont
= buffer
.pointer
;
982 if (ont
->type
!= ACPI_TYPE_PACKAGE
|| ont
->package
.count
!= 6)
985 /* The first 3 integers provide axis order information.
986 * e.g. 0 1 2 would indicate normal X,Y,Z ordering.
987 * e.g. 1 0 2 indicates that data arrives in order Y,X,Z.
989 elements
= ont
->package
.elements
;
990 for (i
= 0; i
< 3; i
++) {
991 if (elements
[i
].type
!= ACPI_TYPE_INTEGER
)
994 val
= elements
[i
].integer
.value
;
995 if (val
< 0 || val
> 2)
998 /* Avoiding full matrix multiplication, we simply reorder the
999 * columns in the default_ont matrix according to the
1000 * ordering provided by _ONT.
1002 final_ont
[0][i
] = default_ont
[0][val
];
1003 final_ont
[1][i
] = default_ont
[1][val
];
1004 final_ont
[2][i
] = default_ont
[2][val
];
1007 /* The final 3 integers provide sign flip information.
1008 * 0 means no change, 1 means flip.
1009 * e.g. 0 0 1 means that Z data should be sign-flipped.
1010 * This is applied after the axis reordering from above.
1013 for (i
= 0; i
< 3; i
++) {
1014 if (elements
[i
].type
!= ACPI_TYPE_INTEGER
)
1017 val
= elements
[i
].integer
.value
;
1018 if (val
!= 0 && val
!= 1)
1023 /* Flip the values in the indicated column */
1024 final_ont
[0][i
] *= -1;
1025 final_ont
[1][i
] *= -1;
1026 final_ont
[2][i
] *= -1;
1029 /* Convert our integer matrix to a string-based iio_mount_matrix */
1030 adata
->mount_matrix
= devm_kmalloc(&indio_dev
->dev
,
1031 sizeof(*adata
->mount_matrix
),
1033 if (!adata
->mount_matrix
) {
1038 for (i
= 0; i
< 3; i
++) {
1039 for (j
= 0; j
< 3; j
++) {
1040 int matrix_val
= final_ont
[i
][j
];
1043 switch (matrix_val
) {
1056 adata
->mount_matrix
->rotation
[i
* 3 + j
] = str_value
;
1060 /* Expose the mount matrix via ext_info */
1061 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
1062 channels
[i
].ext_info
= mount_matrix_ext_info
;
1065 dev_info(&indio_dev
->dev
, "computed mount matrix from ACPI\n");
1068 kfree(buffer
.pointer
);
1070 #else /* !CONFIG_ACPI */
1075 int st_accel_common_probe(struct iio_dev
*indio_dev
)
1077 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
1078 struct st_sensors_platform_data
*pdata
=
1079 (struct st_sensors_platform_data
*)adata
->dev
->platform_data
;
1080 int irq
= adata
->get_irq_data_ready(indio_dev
);
1081 struct iio_chan_spec
*channels
;
1082 size_t channels_size
;
1085 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1086 indio_dev
->info
= &accel_info
;
1087 mutex_init(&adata
->tb
.buf_lock
);
1089 err
= st_sensors_power_enable(indio_dev
);
1093 err
= st_sensors_check_device_support(indio_dev
,
1094 ARRAY_SIZE(st_accel_sensors_settings
),
1095 st_accel_sensors_settings
);
1097 goto st_accel_power_off
;
1099 adata
->num_data_channels
= ST_ACCEL_NUMBER_DATA_CHANNELS
;
1100 adata
->multiread_bit
= adata
->sensor_settings
->multi_read_bit
;
1101 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
1103 channels_size
= indio_dev
->num_channels
* sizeof(struct iio_chan_spec
);
1104 channels
= devm_kmemdup(&indio_dev
->dev
,
1105 adata
->sensor_settings
->ch
,
1106 channels_size
, GFP_KERNEL
);
1109 goto st_accel_power_off
;
1112 if (apply_acpi_orientation(indio_dev
, channels
))
1113 dev_warn(&indio_dev
->dev
,
1114 "failed to apply ACPI orientation data: %d\n", err
);
1116 indio_dev
->channels
= channels
;
1117 adata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
1118 &adata
->sensor_settings
->fs
.fs_avl
[0];
1119 adata
->odr
= adata
->sensor_settings
->odr
.odr_avl
[0].hz
;
1122 pdata
= (struct st_sensors_platform_data
*)&default_accel_pdata
;
1124 err
= st_sensors_init_sensor(indio_dev
, pdata
);
1126 goto st_accel_power_off
;
1128 err
= st_accel_allocate_ring(indio_dev
);
1130 goto st_accel_power_off
;
1133 err
= st_sensors_allocate_trigger(indio_dev
,
1134 ST_ACCEL_TRIGGER_OPS
);
1136 goto st_accel_probe_trigger_error
;
1139 err
= iio_device_register(indio_dev
);
1141 goto st_accel_device_register_error
;
1143 dev_info(&indio_dev
->dev
, "registered accelerometer %s\n",
1148 st_accel_device_register_error
:
1150 st_sensors_deallocate_trigger(indio_dev
);
1151 st_accel_probe_trigger_error
:
1152 st_accel_deallocate_ring(indio_dev
);
1154 st_sensors_power_disable(indio_dev
);
1158 EXPORT_SYMBOL(st_accel_common_probe
);
1160 void st_accel_common_remove(struct iio_dev
*indio_dev
)
1162 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
1164 st_sensors_power_disable(indio_dev
);
1166 iio_device_unregister(indio_dev
);
1167 if (adata
->get_irq_data_ready(indio_dev
) > 0)
1168 st_sensors_deallocate_trigger(indio_dev
);
1170 st_accel_deallocate_ring(indio_dev
);
1172 EXPORT_SYMBOL(st_accel_common_remove
);
1174 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1175 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
1176 MODULE_LICENSE("GPL v2");