1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics st_lsm6dsx sensor driver
5 * The ST LSM6DSx IMU MEMS series consists of 3D digital accelerometer
6 * and 3D digital gyroscope system-in-package with a digital I2C/SPI serial
7 * interface standard output.
8 * LSM6DSx IMU MEMS series has a dynamic user-selectable full-scale
9 * acceleration range of +-2/+-4/+-8/+-16 g and an angular rate range of
10 * +-125/+-245/+-500/+-1000/+-2000 dps
11 * LSM6DSx series has an integrated First-In-First-Out (FIFO) buffer
12 * allowing dynamic batching of sensor data.
13 * LSM9DSx series is similar but includes an additional magnetometer, handled
14 * by a different driver.
18 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
19 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
20 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
23 * - LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC/LSM6DS3TR-C:
24 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
25 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
26 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
29 * - LSM6DSO/LSM6DSOX/ASM330LHH/LSM6DSR/ISM330DHCX:
30 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
31 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
32 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
36 * - Accelerometer supported ODR [Hz]: 10, 50, 119, 238, 476, 952
37 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
38 * - Gyroscope supported ODR [Hz]: 15, 60, 119, 238, 476, 952
39 * - Gyroscope supported full-scale [dps]: +-245/+-500/+-2000
42 * Copyright 2016 STMicroelectronics Inc.
44 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
45 * Denis Ciocca <denis.ciocca@st.com>
48 #include <linux/kernel.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/iio/iio.h>
52 #include <linux/iio/sysfs.h>
54 #include <linux/regmap.h>
55 #include <linux/bitfield.h>
57 #include <linux/platform_data/st_sensors_pdata.h>
59 #include "st_lsm6dsx.h"
61 #define ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK BIT(3)
62 #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f
63 #define ST_LSM6DSX_REG_RESET_MASK BIT(0)
64 #define ST_LSM6DSX_REG_BOOT_MASK BIT(7)
65 #define ST_LSM6DSX_REG_BDU_ADDR 0x12
66 #define ST_LSM6DSX_REG_BDU_MASK BIT(6)
68 static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
69 ST_LSM6DSX_CHANNEL(IIO_ACCEL, 0x28, IIO_MOD_X, 0),
70 ST_LSM6DSX_CHANNEL(IIO_ACCEL, 0x2a, IIO_MOD_Y, 1),
71 ST_LSM6DSX_CHANNEL(IIO_ACCEL, 0x2c, IIO_MOD_Z, 2),
72 IIO_CHAN_SOFT_TIMESTAMP(3),
75 static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
76 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x22, IIO_MOD_X, 0),
77 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x24, IIO_MOD_Y, 1),
78 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x26, IIO_MOD_Z, 2),
79 IIO_CHAN_SOFT_TIMESTAMP(3),
82 static const struct iio_chan_spec st_lsm6ds0_gyro_channels[] = {
83 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x18, IIO_MOD_X, 0),
84 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x1a, IIO_MOD_Y, 1),
85 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x1c, IIO_MOD_Z, 2),
86 IIO_CHAN_SOFT_TIMESTAMP(3),
89 static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
98 .hw_id = ST_LSM9DS1_ID,
99 .name = ST_LSM9DS1_DEV_NAME,
103 [ST_LSM6DSX_ID_ACC] = {
104 .chan = st_lsm6dsx_acc_channels,
105 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
107 [ST_LSM6DSX_ID_GYRO] = {
108 .chan = st_lsm6ds0_gyro_channels,
109 .len = ARRAY_SIZE(st_lsm6ds0_gyro_channels),
113 [ST_LSM6DSX_ID_ACC] = {
116 .mask = GENMASK(7, 5),
118 .odr_avl[0] = { 10, 0x01 },
119 .odr_avl[1] = { 50, 0x02 },
120 .odr_avl[2] = { 119, 0x03 },
121 .odr_avl[3] = { 238, 0x04 },
122 .odr_avl[4] = { 476, 0x05 },
123 .odr_avl[5] = { 952, 0x06 },
125 [ST_LSM6DSX_ID_GYRO] = {
128 .mask = GENMASK(7, 5),
130 .odr_avl[0] = { 15, 0x01 },
131 .odr_avl[1] = { 60, 0x02 },
132 .odr_avl[2] = { 119, 0x03 },
133 .odr_avl[3] = { 238, 0x04 },
134 .odr_avl[4] = { 476, 0x05 },
135 .odr_avl[5] = { 952, 0x06 },
139 [ST_LSM6DSX_ID_ACC] = {
142 .mask = GENMASK(4, 3),
144 .fs_avl[0] = { IIO_G_TO_M_S_2(61), 0x0 },
145 .fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
146 .fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
147 .fs_avl[3] = { IIO_G_TO_M_S_2(732), 0x1 },
150 [ST_LSM6DSX_ID_GYRO] = {
153 .mask = GENMASK(4, 3),
156 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
157 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
158 .fs_avl[2] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
168 .max_fifo_size = 1365,
171 .hw_id = ST_LSM6DS3_ID,
172 .name = ST_LSM6DS3_DEV_NAME,
176 [ST_LSM6DSX_ID_ACC] = {
177 .chan = st_lsm6dsx_acc_channels,
178 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
180 [ST_LSM6DSX_ID_GYRO] = {
181 .chan = st_lsm6dsx_gyro_channels,
182 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
186 [ST_LSM6DSX_ID_ACC] = {
189 .mask = GENMASK(7, 4),
191 .odr_avl[0] = { 13, 0x01 },
192 .odr_avl[1] = { 26, 0x02 },
193 .odr_avl[2] = { 52, 0x03 },
194 .odr_avl[3] = { 104, 0x04 },
195 .odr_avl[4] = { 208, 0x05 },
196 .odr_avl[5] = { 416, 0x06 },
198 [ST_LSM6DSX_ID_GYRO] = {
201 .mask = GENMASK(7, 4),
203 .odr_avl[0] = { 13, 0x01 },
204 .odr_avl[1] = { 26, 0x02 },
205 .odr_avl[2] = { 52, 0x03 },
206 .odr_avl[3] = { 104, 0x04 },
207 .odr_avl[4] = { 208, 0x05 },
208 .odr_avl[5] = { 416, 0x06 },
212 [ST_LSM6DSX_ID_ACC] = {
215 .mask = GENMASK(3, 2),
217 .fs_avl[0] = { IIO_G_TO_M_S_2(61), 0x0 },
218 .fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
219 .fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
220 .fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
223 [ST_LSM6DSX_ID_GYRO] = {
226 .mask = GENMASK(3, 2),
228 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
229 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
230 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
231 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
236 [ST_LSM6DSX_ID_ACC] = {
238 .mask = GENMASK(2, 0),
240 [ST_LSM6DSX_ID_GYRO] = {
242 .mask = GENMASK(5, 3),
246 .update_fifo = st_lsm6dsx_update_fifo,
247 .read_fifo = st_lsm6dsx_read_fifo,
250 .mask = GENMASK(11, 0),
254 .mask = GENMASK(11, 0),
256 .th_wl = 3, /* 1LSB = 2B */
273 .mask = GENMASK(5, 3),
282 .max_fifo_size = 682,
285 .hw_id = ST_LSM6DS3H_ID,
286 .name = ST_LSM6DS3H_DEV_NAME,
290 [ST_LSM6DSX_ID_ACC] = {
291 .chan = st_lsm6dsx_acc_channels,
292 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
294 [ST_LSM6DSX_ID_GYRO] = {
295 .chan = st_lsm6dsx_gyro_channels,
296 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
300 [ST_LSM6DSX_ID_ACC] = {
303 .mask = GENMASK(7, 4),
305 .odr_avl[0] = { 13, 0x01 },
306 .odr_avl[1] = { 26, 0x02 },
307 .odr_avl[2] = { 52, 0x03 },
308 .odr_avl[3] = { 104, 0x04 },
309 .odr_avl[4] = { 208, 0x05 },
310 .odr_avl[5] = { 416, 0x06 },
312 [ST_LSM6DSX_ID_GYRO] = {
315 .mask = GENMASK(7, 4),
317 .odr_avl[0] = { 13, 0x01 },
318 .odr_avl[1] = { 26, 0x02 },
319 .odr_avl[2] = { 52, 0x03 },
320 .odr_avl[3] = { 104, 0x04 },
321 .odr_avl[4] = { 208, 0x05 },
322 .odr_avl[5] = { 416, 0x06 },
326 [ST_LSM6DSX_ID_ACC] = {
329 .mask = GENMASK(3, 2),
331 .fs_avl[0] = { IIO_G_TO_M_S_2(61), 0x0 },
332 .fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
333 .fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
334 .fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
337 [ST_LSM6DSX_ID_GYRO] = {
340 .mask = GENMASK(3, 2),
342 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
343 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
344 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
345 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
350 [ST_LSM6DSX_ID_ACC] = {
352 .mask = GENMASK(2, 0),
354 [ST_LSM6DSX_ID_GYRO] = {
356 .mask = GENMASK(5, 3),
360 .update_fifo = st_lsm6dsx_update_fifo,
361 .read_fifo = st_lsm6dsx_read_fifo,
364 .mask = GENMASK(11, 0),
368 .mask = GENMASK(11, 0),
370 .th_wl = 3, /* 1LSB = 2B */
387 .mask = GENMASK(5, 3),
396 .max_fifo_size = 682,
399 .hw_id = ST_LSM6DSL_ID,
400 .name = ST_LSM6DSL_DEV_NAME,
402 .hw_id = ST_LSM6DSM_ID,
403 .name = ST_LSM6DSM_DEV_NAME,
405 .hw_id = ST_ISM330DLC_ID,
406 .name = ST_ISM330DLC_DEV_NAME,
408 .hw_id = ST_LSM6DS3TRC_ID,
409 .name = ST_LSM6DS3TRC_DEV_NAME,
413 [ST_LSM6DSX_ID_ACC] = {
414 .chan = st_lsm6dsx_acc_channels,
415 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
417 [ST_LSM6DSX_ID_GYRO] = {
418 .chan = st_lsm6dsx_gyro_channels,
419 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
423 [ST_LSM6DSX_ID_ACC] = {
426 .mask = GENMASK(7, 4),
428 .odr_avl[0] = { 13, 0x01 },
429 .odr_avl[1] = { 26, 0x02 },
430 .odr_avl[2] = { 52, 0x03 },
431 .odr_avl[3] = { 104, 0x04 },
432 .odr_avl[4] = { 208, 0x05 },
433 .odr_avl[5] = { 416, 0x06 },
435 [ST_LSM6DSX_ID_GYRO] = {
438 .mask = GENMASK(7, 4),
440 .odr_avl[0] = { 13, 0x01 },
441 .odr_avl[1] = { 26, 0x02 },
442 .odr_avl[2] = { 52, 0x03 },
443 .odr_avl[3] = { 104, 0x04 },
444 .odr_avl[4] = { 208, 0x05 },
445 .odr_avl[5] = { 416, 0x06 },
449 [ST_LSM6DSX_ID_ACC] = {
452 .mask = GENMASK(3, 2),
454 .fs_avl[0] = { IIO_G_TO_M_S_2(61), 0x0 },
455 .fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
456 .fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
457 .fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
460 [ST_LSM6DSX_ID_GYRO] = {
463 .mask = GENMASK(3, 2),
465 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
466 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
467 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
468 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
473 [ST_LSM6DSX_ID_ACC] = {
475 .mask = GENMASK(2, 0),
477 [ST_LSM6DSX_ID_GYRO] = {
479 .mask = GENMASK(5, 3),
483 .update_fifo = st_lsm6dsx_update_fifo,
484 .read_fifo = st_lsm6dsx_read_fifo,
487 .mask = GENMASK(10, 0),
491 .mask = GENMASK(10, 0),
493 .th_wl = 3, /* 1LSB = 2B */
510 .mask = GENMASK(5, 3),
519 .max_fifo_size = 512,
522 .hw_id = ST_LSM6DSO_ID,
523 .name = ST_LSM6DSO_DEV_NAME,
525 .hw_id = ST_LSM6DSOX_ID,
526 .name = ST_LSM6DSOX_DEV_NAME,
530 [ST_LSM6DSX_ID_ACC] = {
531 .chan = st_lsm6dsx_acc_channels,
532 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
534 [ST_LSM6DSX_ID_GYRO] = {
535 .chan = st_lsm6dsx_gyro_channels,
536 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
540 [ST_LSM6DSX_ID_ACC] = {
543 .mask = GENMASK(7, 4),
545 .odr_avl[0] = { 13, 0x01 },
546 .odr_avl[1] = { 26, 0x02 },
547 .odr_avl[2] = { 52, 0x03 },
548 .odr_avl[3] = { 104, 0x04 },
549 .odr_avl[4] = { 208, 0x05 },
550 .odr_avl[5] = { 416, 0x06 },
552 [ST_LSM6DSX_ID_GYRO] = {
555 .mask = GENMASK(7, 4),
557 .odr_avl[0] = { 13, 0x01 },
558 .odr_avl[1] = { 26, 0x02 },
559 .odr_avl[2] = { 52, 0x03 },
560 .odr_avl[3] = { 104, 0x04 },
561 .odr_avl[4] = { 208, 0x05 },
562 .odr_avl[5] = { 416, 0x06 },
566 [ST_LSM6DSX_ID_ACC] = {
569 .mask = GENMASK(3, 2),
571 .fs_avl[0] = { IIO_G_TO_M_S_2(61), 0x0 },
572 .fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
573 .fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
574 .fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
577 [ST_LSM6DSX_ID_GYRO] = {
580 .mask = GENMASK(3, 2),
582 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
583 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
584 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
585 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
590 [ST_LSM6DSX_ID_ACC] = {
592 .mask = GENMASK(3, 0),
594 [ST_LSM6DSX_ID_GYRO] = {
596 .mask = GENMASK(7, 4),
600 .update_fifo = st_lsm6dsx_update_fifo,
601 .read_fifo = st_lsm6dsx_read_tagged_fifo,
604 .mask = GENMASK(8, 0),
608 .mask = GENMASK(9, 0),
619 .mask = GENMASK(7, 6),
637 .mask = GENMASK(1, 0),
645 .dw_slv0_addr = 0x21,
654 .max_fifo_size = 512,
657 .hw_id = ST_ASM330LHH_ID,
658 .name = ST_ASM330LHH_DEV_NAME,
662 [ST_LSM6DSX_ID_ACC] = {
663 .chan = st_lsm6dsx_acc_channels,
664 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
666 [ST_LSM6DSX_ID_GYRO] = {
667 .chan = st_lsm6dsx_gyro_channels,
668 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
672 [ST_LSM6DSX_ID_ACC] = {
675 .mask = GENMASK(7, 4),
677 .odr_avl[0] = { 13, 0x01 },
678 .odr_avl[1] = { 26, 0x02 },
679 .odr_avl[2] = { 52, 0x03 },
680 .odr_avl[3] = { 104, 0x04 },
681 .odr_avl[4] = { 208, 0x05 },
682 .odr_avl[5] = { 416, 0x06 },
684 [ST_LSM6DSX_ID_GYRO] = {
687 .mask = GENMASK(7, 4),
689 .odr_avl[0] = { 13, 0x01 },
690 .odr_avl[1] = { 26, 0x02 },
691 .odr_avl[2] = { 52, 0x03 },
692 .odr_avl[3] = { 104, 0x04 },
693 .odr_avl[4] = { 208, 0x05 },
694 .odr_avl[5] = { 416, 0x06 },
698 [ST_LSM6DSX_ID_ACC] = {
701 .mask = GENMASK(3, 2),
703 .fs_avl[0] = { IIO_G_TO_M_S_2(61), 0x0 },
704 .fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
705 .fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
706 .fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
709 [ST_LSM6DSX_ID_GYRO] = {
712 .mask = GENMASK(3, 2),
714 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
715 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
716 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
717 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
722 [ST_LSM6DSX_ID_ACC] = {
724 .mask = GENMASK(3, 0),
726 [ST_LSM6DSX_ID_GYRO] = {
728 .mask = GENMASK(7, 4),
732 .update_fifo = st_lsm6dsx_update_fifo,
733 .read_fifo = st_lsm6dsx_read_tagged_fifo,
736 .mask = GENMASK(8, 0),
740 .mask = GENMASK(9, 0),
751 .mask = GENMASK(7, 6),
760 .max_fifo_size = 512,
763 .hw_id = ST_LSM6DSR_ID,
764 .name = ST_LSM6DSR_DEV_NAME,
766 .hw_id = ST_ISM330DHCX_ID,
767 .name = ST_ISM330DHCX_DEV_NAME,
771 [ST_LSM6DSX_ID_ACC] = {
772 .chan = st_lsm6dsx_acc_channels,
773 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
775 [ST_LSM6DSX_ID_GYRO] = {
776 .chan = st_lsm6dsx_gyro_channels,
777 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
781 [ST_LSM6DSX_ID_ACC] = {
784 .mask = GENMASK(7, 4),
786 .odr_avl[0] = { 13, 0x01 },
787 .odr_avl[1] = { 26, 0x02 },
788 .odr_avl[2] = { 52, 0x03 },
789 .odr_avl[3] = { 104, 0x04 },
790 .odr_avl[4] = { 208, 0x05 },
791 .odr_avl[5] = { 416, 0x06 },
793 [ST_LSM6DSX_ID_GYRO] = {
796 .mask = GENMASK(7, 4),
798 .odr_avl[0] = { 13, 0x01 },
799 .odr_avl[1] = { 26, 0x02 },
800 .odr_avl[2] = { 52, 0x03 },
801 .odr_avl[3] = { 104, 0x04 },
802 .odr_avl[4] = { 208, 0x05 },
803 .odr_avl[5] = { 416, 0x06 },
807 [ST_LSM6DSX_ID_ACC] = {
810 .mask = GENMASK(3, 2),
812 .fs_avl[0] = { IIO_G_TO_M_S_2(61), 0x0 },
813 .fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
814 .fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
815 .fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
818 [ST_LSM6DSX_ID_GYRO] = {
821 .mask = GENMASK(3, 2),
823 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
824 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
825 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
826 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
831 [ST_LSM6DSX_ID_ACC] = {
833 .mask = GENMASK(3, 0),
835 [ST_LSM6DSX_ID_GYRO] = {
837 .mask = GENMASK(7, 4),
841 .update_fifo = st_lsm6dsx_update_fifo,
842 .read_fifo = st_lsm6dsx_read_tagged_fifo,
845 .mask = GENMASK(8, 0),
849 .mask = GENMASK(9, 0),
860 .mask = GENMASK(7, 6),
878 .mask = GENMASK(1, 0),
886 .dw_slv0_addr = 0x21,
892 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable)
894 const struct st_lsm6dsx_shub_settings *hub_settings;
898 hub_settings = &hw->settings->shub_settings;
899 data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->page_mux.mask);
900 err = regmap_update_bits(hw->regmap, hub_settings->page_mux.addr,
901 hub_settings->page_mux.mask, data);
902 usleep_range(100, 150);
907 static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
912 for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
913 for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
914 if (st_lsm6dsx_sensor_settings[i].id[j].name &&
915 id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
918 if (j < ST_LSM6DSX_MAX_ID)
922 if (i == ARRAY_SIZE(st_lsm6dsx_sensor_settings)) {
923 dev_err(hw->dev, "unsupported hw id [%02x]\n", id);
927 err = regmap_read(hw->regmap, ST_LSM6DSX_REG_WHOAMI_ADDR, &data);
929 dev_err(hw->dev, "failed to read whoami register\n");
933 if (data != st_lsm6dsx_sensor_settings[i].wai) {
934 dev_err(hw->dev, "unsupported whoami [%02x]\n", data);
938 *name = st_lsm6dsx_sensor_settings[i].id[j].name;
939 hw->settings = &st_lsm6dsx_sensor_settings[i];
944 static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor *sensor,
947 const struct st_lsm6dsx_fs_table_entry *fs_table;
951 fs_table = &sensor->hw->settings->fs_table[sensor->id];
952 for (i = 0; i < fs_table->fs_len; i++) {
953 if (fs_table->fs_avl[i].gain == gain)
957 if (i == fs_table->fs_len)
960 data = ST_LSM6DSX_SHIFT_VAL(fs_table->fs_avl[i].val,
962 err = st_lsm6dsx_update_bits_locked(sensor->hw, fs_table->reg.addr,
963 fs_table->reg.mask, data);
972 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val)
974 const struct st_lsm6dsx_odr_table_entry *odr_table;
977 odr_table = &sensor->hw->settings->odr_table[sensor->id];
978 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
980 * ext devices can run at different odr respect to
983 if (odr_table->odr_avl[i].hz >= odr)
986 if (i == ST_LSM6DSX_ODR_LIST_SIZE)
989 *val = odr_table->odr_avl[i].val;
990 return odr_table->odr_avl[i].hz;
993 static u16 st_lsm6dsx_check_odr_dependency(struct st_lsm6dsx_hw *hw, u16 odr,
994 enum st_lsm6dsx_sensor_id id)
996 struct st_lsm6dsx_sensor *ref = iio_priv(hw->iio_devs[id]);
999 if (hw->enable_mask & BIT(id))
1000 return max_t(u16, ref->odr, odr);
1004 return (hw->enable_mask & BIT(id)) ? ref->odr : 0;
1008 static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u16 req_odr)
1010 struct st_lsm6dsx_sensor *ref_sensor = sensor;
1011 struct st_lsm6dsx_hw *hw = sensor->hw;
1012 const struct st_lsm6dsx_reg *reg;
1017 switch (sensor->id) {
1018 case ST_LSM6DSX_ID_EXT0:
1019 case ST_LSM6DSX_ID_EXT1:
1020 case ST_LSM6DSX_ID_EXT2:
1021 case ST_LSM6DSX_ID_ACC: {
1026 * i2c embedded controller relies on the accelerometer sensor as
1027 * bus read/write trigger so we need to enable accel device
1028 * at odr = max(accel_odr, ext_odr) in order to properly
1029 * communicate with i2c slave devices
1031 ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
1032 for (i = ST_LSM6DSX_ID_ACC; i < ST_LSM6DSX_ID_MAX; i++) {
1033 if (!hw->iio_devs[i] || i == sensor->id)
1036 odr = st_lsm6dsx_check_odr_dependency(hw, req_odr, i);
1038 /* device already configured */
1048 err = st_lsm6dsx_check_odr(ref_sensor, req_odr, &val);
1053 reg = &hw->settings->odr_table[ref_sensor->id].reg;
1054 data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
1055 return st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data);
1058 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
1061 struct st_lsm6dsx_hw *hw = sensor->hw;
1062 u16 odr = enable ? sensor->odr : 0;
1065 err = st_lsm6dsx_set_odr(sensor, odr);
1070 hw->enable_mask |= BIT(sensor->id);
1072 hw->enable_mask &= ~BIT(sensor->id);
1077 static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor *sensor,
1080 struct st_lsm6dsx_hw *hw = sensor->hw;
1084 err = st_lsm6dsx_sensor_set_enable(sensor, true);
1088 delay = 1000000 / sensor->odr;
1089 usleep_range(delay, 2 * delay);
1091 err = st_lsm6dsx_read_locked(hw, addr, &data, sizeof(data));
1095 st_lsm6dsx_sensor_set_enable(sensor, false);
1097 *val = (s16)le16_to_cpu(data);
1102 static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev,
1103 struct iio_chan_spec const *ch,
1104 int *val, int *val2, long mask)
1106 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1110 case IIO_CHAN_INFO_RAW:
1111 ret = iio_device_claim_direct_mode(iio_dev);
1115 ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val);
1116 iio_device_release_direct_mode(iio_dev);
1118 case IIO_CHAN_INFO_SAMP_FREQ:
1122 case IIO_CHAN_INFO_SCALE:
1124 *val2 = sensor->gain;
1125 ret = IIO_VAL_INT_PLUS_MICRO;
1135 static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
1136 struct iio_chan_spec const *chan,
1137 int val, int val2, long mask)
1139 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1142 err = iio_device_claim_direct_mode(iio_dev);
1147 case IIO_CHAN_INFO_SCALE:
1148 err = st_lsm6dsx_set_full_scale(sensor, val2);
1150 case IIO_CHAN_INFO_SAMP_FREQ: {
1153 val = st_lsm6dsx_check_odr(sensor, val, &data);
1165 iio_device_release_direct_mode(iio_dev);
1170 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val)
1172 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1173 struct st_lsm6dsx_hw *hw = sensor->hw;
1176 if (val < 1 || val > hw->settings->max_fifo_size)
1179 mutex_lock(&hw->conf_lock);
1181 err = st_lsm6dsx_update_watermark(sensor, val);
1183 mutex_unlock(&hw->conf_lock);
1188 sensor->watermark = val;
1194 st_lsm6dsx_sysfs_sampling_frequency_avail(struct device *dev,
1195 struct device_attribute *attr,
1198 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
1199 enum st_lsm6dsx_sensor_id id = sensor->id;
1200 struct st_lsm6dsx_hw *hw = sensor->hw;
1203 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
1204 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
1205 hw->settings->odr_table[id].odr_avl[i].hz);
1206 buf[len - 1] = '\n';
1211 static ssize_t st_lsm6dsx_sysfs_scale_avail(struct device *dev,
1212 struct device_attribute *attr,
1215 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
1216 const struct st_lsm6dsx_fs_table_entry *fs_table;
1217 struct st_lsm6dsx_hw *hw = sensor->hw;
1220 fs_table = &hw->settings->fs_table[sensor->id];
1221 for (i = 0; i < fs_table->fs_len; i++)
1222 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ",
1223 fs_table->fs_avl[i].gain);
1224 buf[len - 1] = '\n';
1229 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail);
1230 static IIO_DEVICE_ATTR(in_accel_scale_available, 0444,
1231 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
1232 static IIO_DEVICE_ATTR(in_anglvel_scale_available, 0444,
1233 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
1235 static struct attribute *st_lsm6dsx_acc_attributes[] = {
1236 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1237 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
1241 static const struct attribute_group st_lsm6dsx_acc_attribute_group = {
1242 .attrs = st_lsm6dsx_acc_attributes,
1245 static const struct iio_info st_lsm6dsx_acc_info = {
1246 .attrs = &st_lsm6dsx_acc_attribute_group,
1247 .read_raw = st_lsm6dsx_read_raw,
1248 .write_raw = st_lsm6dsx_write_raw,
1249 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
1252 static struct attribute *st_lsm6dsx_gyro_attributes[] = {
1253 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1254 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
1258 static const struct attribute_group st_lsm6dsx_gyro_attribute_group = {
1259 .attrs = st_lsm6dsx_gyro_attributes,
1262 static const struct iio_info st_lsm6dsx_gyro_info = {
1263 .attrs = &st_lsm6dsx_gyro_attribute_group,
1264 .read_raw = st_lsm6dsx_read_raw,
1265 .write_raw = st_lsm6dsx_write_raw,
1266 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
1269 static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)
1271 struct device_node *np = hw->dev->of_node;
1276 return of_property_read_u32(np, "st,drdy-int-pin", drdy_pin);
1279 static int st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw *hw, u8 *drdy_reg)
1281 int err = 0, drdy_pin;
1283 if (st_lsm6dsx_of_get_drdy_pin(hw, &drdy_pin) < 0) {
1284 struct st_sensors_platform_data *pdata;
1285 struct device *dev = hw->dev;
1287 pdata = (struct st_sensors_platform_data *)dev->platform_data;
1288 drdy_pin = pdata ? pdata->drdy_int_pin : 1;
1293 *drdy_reg = hw->settings->int1_addr;
1296 *drdy_reg = hw->settings->int2_addr;
1299 dev_err(hw->dev, "unsupported data ready pin\n");
1307 static int st_lsm6dsx_init_shub(struct st_lsm6dsx_hw *hw)
1309 const struct st_lsm6dsx_shub_settings *hub_settings;
1310 struct device_node *np = hw->dev->of_node;
1311 struct st_sensors_platform_data *pdata;
1315 hub_settings = &hw->settings->shub_settings;
1317 pdata = (struct st_sensors_platform_data *)hw->dev->platform_data;
1318 if ((np && of_property_read_bool(np, "st,pullups")) ||
1319 (pdata && pdata->pullups)) {
1320 err = st_lsm6dsx_set_page(hw, true);
1324 data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->pullup_en.mask);
1325 err = regmap_update_bits(hw->regmap,
1326 hub_settings->pullup_en.addr,
1327 hub_settings->pullup_en.mask, data);
1329 st_lsm6dsx_set_page(hw, false);
1335 if (hub_settings->aux_sens.addr) {
1336 /* configure aux sensors */
1337 err = st_lsm6dsx_set_page(hw, true);
1341 data = ST_LSM6DSX_SHIFT_VAL(3, hub_settings->aux_sens.mask);
1342 err = regmap_update_bits(hw->regmap,
1343 hub_settings->aux_sens.addr,
1344 hub_settings->aux_sens.mask, data);
1346 st_lsm6dsx_set_page(hw, false);
1352 static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
1354 const struct st_lsm6dsx_hw_ts_settings *ts_settings;
1357 ts_settings = &hw->settings->ts_settings;
1358 /* enable hw timestamp generation if necessary */
1359 if (ts_settings->timer_en.addr) {
1360 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->timer_en.mask);
1361 err = regmap_update_bits(hw->regmap,
1362 ts_settings->timer_en.addr,
1363 ts_settings->timer_en.mask, val);
1368 /* enable high resolution for hw ts timer if necessary */
1369 if (ts_settings->hr_timer.addr) {
1370 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->hr_timer.mask);
1371 err = regmap_update_bits(hw->regmap,
1372 ts_settings->hr_timer.addr,
1373 ts_settings->hr_timer.mask, val);
1378 /* enable ts queueing in FIFO if necessary */
1379 if (ts_settings->fifo_en.addr) {
1380 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->fifo_en.mask);
1381 err = regmap_update_bits(hw->regmap,
1382 ts_settings->fifo_en.addr,
1383 ts_settings->fifo_en.mask, val);
1390 static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
1395 /* device sw reset */
1396 err = regmap_update_bits(hw->regmap, hw->settings->reset_addr,
1397 ST_LSM6DSX_REG_RESET_MASK,
1398 FIELD_PREP(ST_LSM6DSX_REG_RESET_MASK, 1));
1404 /* reload trimming parameter */
1405 err = regmap_update_bits(hw->regmap, hw->settings->reset_addr,
1406 ST_LSM6DSX_REG_BOOT_MASK,
1407 FIELD_PREP(ST_LSM6DSX_REG_BOOT_MASK, 1));
1413 /* enable Block Data Update */
1414 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_BDU_ADDR,
1415 ST_LSM6DSX_REG_BDU_MASK,
1416 FIELD_PREP(ST_LSM6DSX_REG_BDU_MASK, 1));
1420 /* enable FIFO watermak interrupt */
1421 err = st_lsm6dsx_get_drdy_reg(hw, &drdy_int_reg);
1425 err = regmap_update_bits(hw->regmap, drdy_int_reg,
1426 ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK,
1427 FIELD_PREP(ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK,
1432 err = st_lsm6dsx_init_shub(hw);
1436 return st_lsm6dsx_init_hw_timer(hw);
1439 static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw,
1440 enum st_lsm6dsx_sensor_id id,
1443 struct st_lsm6dsx_sensor *sensor;
1444 struct iio_dev *iio_dev;
1446 iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor));
1450 iio_dev->modes = INDIO_DIRECT_MODE;
1451 iio_dev->dev.parent = hw->dev;
1452 iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks;
1453 iio_dev->channels = hw->settings->channels[id].chan;
1454 iio_dev->num_channels = hw->settings->channels[id].len;
1456 sensor = iio_priv(iio_dev);
1459 sensor->odr = hw->settings->odr_table[id].odr_avl[0].hz;
1460 sensor->gain = hw->settings->fs_table[id].fs_avl[0].gain;
1461 sensor->watermark = 1;
1464 case ST_LSM6DSX_ID_ACC:
1465 iio_dev->info = &st_lsm6dsx_acc_info;
1466 scnprintf(sensor->name, sizeof(sensor->name), "%s_accel",
1469 case ST_LSM6DSX_ID_GYRO:
1470 iio_dev->info = &st_lsm6dsx_gyro_info;
1471 scnprintf(sensor->name, sizeof(sensor->name), "%s_gyro",
1477 iio_dev->name = sensor->name;
1482 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
1483 struct regmap *regmap)
1485 const struct st_lsm6dsx_shub_settings *hub_settings;
1486 struct st_lsm6dsx_hw *hw;
1487 const char *name = NULL;
1490 hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
1494 dev_set_drvdata(dev, (void *)hw);
1496 mutex_init(&hw->fifo_lock);
1497 mutex_init(&hw->conf_lock);
1498 mutex_init(&hw->page_lock);
1500 hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL);
1506 hw->regmap = regmap;
1508 err = st_lsm6dsx_check_whoami(hw, hw_id, &name);
1512 for (i = 0; i < ST_LSM6DSX_ID_EXT0; i++) {
1513 hw->iio_devs[i] = st_lsm6dsx_alloc_iiodev(hw, i, name);
1514 if (!hw->iio_devs[i])
1518 err = st_lsm6dsx_init_device(hw);
1522 hub_settings = &hw->settings->shub_settings;
1523 if (hub_settings->master_en.addr) {
1524 err = st_lsm6dsx_shub_probe(hw, name);
1530 err = st_lsm6dsx_fifo_setup(hw);
1535 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
1536 if (!hw->iio_devs[i])
1539 err = devm_iio_device_register(hw->dev, hw->iio_devs[i]);
1546 EXPORT_SYMBOL(st_lsm6dsx_probe);
1548 static int __maybe_unused st_lsm6dsx_suspend(struct device *dev)
1550 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
1551 struct st_lsm6dsx_sensor *sensor;
1554 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
1555 if (!hw->iio_devs[i])
1558 sensor = iio_priv(hw->iio_devs[i]);
1559 if (!(hw->enable_mask & BIT(sensor->id)))
1562 if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
1563 sensor->id == ST_LSM6DSX_ID_EXT1 ||
1564 sensor->id == ST_LSM6DSX_ID_EXT2)
1565 err = st_lsm6dsx_shub_set_enable(sensor, false);
1567 err = st_lsm6dsx_sensor_set_enable(sensor, false);
1571 hw->suspend_mask |= BIT(sensor->id);
1574 if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS)
1575 err = st_lsm6dsx_flush_fifo(hw);
1580 static int __maybe_unused st_lsm6dsx_resume(struct device *dev)
1582 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
1583 struct st_lsm6dsx_sensor *sensor;
1586 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
1587 if (!hw->iio_devs[i])
1590 sensor = iio_priv(hw->iio_devs[i]);
1591 if (!(hw->suspend_mask & BIT(sensor->id)))
1594 if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
1595 sensor->id == ST_LSM6DSX_ID_EXT1 ||
1596 sensor->id == ST_LSM6DSX_ID_EXT2)
1597 err = st_lsm6dsx_shub_set_enable(sensor, true);
1599 err = st_lsm6dsx_sensor_set_enable(sensor, true);
1603 hw->suspend_mask &= ~BIT(sensor->id);
1606 if (hw->enable_mask)
1607 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
1612 const struct dev_pm_ops st_lsm6dsx_pm_ops = {
1613 SET_SYSTEM_SLEEP_PM_OPS(st_lsm6dsx_suspend, st_lsm6dsx_resume)
1615 EXPORT_SYMBOL(st_lsm6dsx_pm_ops);
1617 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
1618 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1619 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver");
1620 MODULE_LICENSE("GPL v2");