2 * STMicroelectronics st_lsm6dsx sensor driver
4 * The ST LSM6DSx IMU MEMS series consists of 3D digital accelerometer
5 * and 3D digital gyroscope system-in-package with a digital I2C/SPI serial
6 * interface standard output.
7 * LSM6DSx IMU MEMS series has a dynamic user-selectable full-scale
8 * acceleration range of +-2/+-4/+-8/+-16 g and an angular rate range of
9 * +-125/+-245/+-500/+-1000/+-2000 dps
10 * LSM6DSx series has an integrated First-In-First-Out (FIFO) buffer
11 * allowing dynamic batching of sensor data.
15 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
16 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
17 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
20 * - LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC:
21 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
22 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
23 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
27 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
28 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
29 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
32 * Copyright 2016 STMicroelectronics Inc.
34 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
35 * Denis Ciocca <denis.ciocca@st.com>
37 * Licensed under the GPL-2.
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/delay.h>
43 #include <linux/iio/iio.h>
44 #include <linux/iio/sysfs.h>
46 #include <linux/regmap.h>
47 #include <linux/bitfield.h>
49 #include <linux/platform_data/st_sensors_pdata.h>
51 #include "st_lsm6dsx.h"
53 #define ST_LSM6DSX_REG_INT1_ADDR 0x0d
54 #define ST_LSM6DSX_REG_INT2_ADDR 0x0e
55 #define ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK BIT(3)
56 #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f
57 #define ST_LSM6DSX_REG_RESET_ADDR 0x12
58 #define ST_LSM6DSX_REG_RESET_MASK BIT(0)
59 #define ST_LSM6DSX_REG_BOOT_MASK BIT(7)
60 #define ST_LSM6DSX_REG_BDU_ADDR 0x12
61 #define ST_LSM6DSX_REG_BDU_MASK BIT(6)
62 #define ST_LSM6DSX_REG_INT2_ON_INT1_ADDR 0x13
63 #define ST_LSM6DSX_REG_INT2_ON_INT1_MASK BIT(5)
65 #define ST_LSM6DSX_REG_ACC_ODR_ADDR 0x10
66 #define ST_LSM6DSX_REG_ACC_ODR_MASK GENMASK(7, 4)
67 #define ST_LSM6DSX_REG_ACC_FS_ADDR 0x10
68 #define ST_LSM6DSX_REG_ACC_FS_MASK GENMASK(3, 2)
69 #define ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR 0x28
70 #define ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR 0x2a
71 #define ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR 0x2c
73 #define ST_LSM6DSX_REG_GYRO_ODR_ADDR 0x11
74 #define ST_LSM6DSX_REG_GYRO_ODR_MASK GENMASK(7, 4)
75 #define ST_LSM6DSX_REG_GYRO_FS_ADDR 0x11
76 #define ST_LSM6DSX_REG_GYRO_FS_MASK GENMASK(3, 2)
77 #define ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR 0x22
78 #define ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR 0x24
79 #define ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR 0x26
81 #define ST_LSM6DSX_ACC_FS_2G_GAIN IIO_G_TO_M_S_2(61)
82 #define ST_LSM6DSX_ACC_FS_4G_GAIN IIO_G_TO_M_S_2(122)
83 #define ST_LSM6DSX_ACC_FS_8G_GAIN IIO_G_TO_M_S_2(244)
84 #define ST_LSM6DSX_ACC_FS_16G_GAIN IIO_G_TO_M_S_2(488)
86 #define ST_LSM6DSX_GYRO_FS_245_GAIN IIO_DEGREE_TO_RAD(8750)
87 #define ST_LSM6DSX_GYRO_FS_500_GAIN IIO_DEGREE_TO_RAD(17500)
88 #define ST_LSM6DSX_GYRO_FS_1000_GAIN IIO_DEGREE_TO_RAD(35000)
89 #define ST_LSM6DSX_GYRO_FS_2000_GAIN IIO_DEGREE_TO_RAD(70000)
91 struct st_lsm6dsx_odr {
96 #define ST_LSM6DSX_ODR_LIST_SIZE 6
97 struct st_lsm6dsx_odr_table_entry {
98 struct st_lsm6dsx_reg reg;
99 struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
102 static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table[] = {
103 [ST_LSM6DSX_ID_ACC] = {
105 .addr = ST_LSM6DSX_REG_ACC_ODR_ADDR,
106 .mask = ST_LSM6DSX_REG_ACC_ODR_MASK,
108 .odr_avl[0] = { 13, 0x01 },
109 .odr_avl[1] = { 26, 0x02 },
110 .odr_avl[2] = { 52, 0x03 },
111 .odr_avl[3] = { 104, 0x04 },
112 .odr_avl[4] = { 208, 0x05 },
113 .odr_avl[5] = { 416, 0x06 },
115 [ST_LSM6DSX_ID_GYRO] = {
117 .addr = ST_LSM6DSX_REG_GYRO_ODR_ADDR,
118 .mask = ST_LSM6DSX_REG_GYRO_ODR_MASK,
120 .odr_avl[0] = { 13, 0x01 },
121 .odr_avl[1] = { 26, 0x02 },
122 .odr_avl[2] = { 52, 0x03 },
123 .odr_avl[3] = { 104, 0x04 },
124 .odr_avl[4] = { 208, 0x05 },
125 .odr_avl[5] = { 416, 0x06 },
129 struct st_lsm6dsx_fs {
134 #define ST_LSM6DSX_FS_LIST_SIZE 4
135 struct st_lsm6dsx_fs_table_entry {
136 struct st_lsm6dsx_reg reg;
137 struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
140 static const struct st_lsm6dsx_fs_table_entry st_lsm6dsx_fs_table[] = {
141 [ST_LSM6DSX_ID_ACC] = {
143 .addr = ST_LSM6DSX_REG_ACC_FS_ADDR,
144 .mask = ST_LSM6DSX_REG_ACC_FS_MASK,
146 .fs_avl[0] = { ST_LSM6DSX_ACC_FS_2G_GAIN, 0x0 },
147 .fs_avl[1] = { ST_LSM6DSX_ACC_FS_4G_GAIN, 0x2 },
148 .fs_avl[2] = { ST_LSM6DSX_ACC_FS_8G_GAIN, 0x3 },
149 .fs_avl[3] = { ST_LSM6DSX_ACC_FS_16G_GAIN, 0x1 },
151 [ST_LSM6DSX_ID_GYRO] = {
153 .addr = ST_LSM6DSX_REG_GYRO_FS_ADDR,
154 .mask = ST_LSM6DSX_REG_GYRO_FS_MASK,
156 .fs_avl[0] = { ST_LSM6DSX_GYRO_FS_245_GAIN, 0x0 },
157 .fs_avl[1] = { ST_LSM6DSX_GYRO_FS_500_GAIN, 0x1 },
158 .fs_avl[2] = { ST_LSM6DSX_GYRO_FS_1000_GAIN, 0x2 },
159 .fs_avl[3] = { ST_LSM6DSX_GYRO_FS_2000_GAIN, 0x3 },
163 static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
166 .max_fifo_size = 1365,
171 [ST_LSM6DSX_ID_ACC] = {
173 .mask = GENMASK(2, 0),
175 [ST_LSM6DSX_ID_GYRO] = {
177 .mask = GENMASK(5, 3),
181 .read_fifo = st_lsm6dsx_read_fifo,
184 .mask = GENMASK(11, 0),
188 .mask = GENMASK(11, 0),
190 .th_wl = 3, /* 1LSB = 2B */
207 .mask = GENMASK(5, 3),
213 .max_fifo_size = 682,
215 [0] = ST_LSM6DS3H_ID,
218 [ST_LSM6DSX_ID_ACC] = {
220 .mask = GENMASK(2, 0),
222 [ST_LSM6DSX_ID_GYRO] = {
224 .mask = GENMASK(5, 3),
228 .read_fifo = st_lsm6dsx_read_fifo,
231 .mask = GENMASK(11, 0),
235 .mask = GENMASK(11, 0),
237 .th_wl = 3, /* 1LSB = 2B */
254 .mask = GENMASK(5, 3),
260 .max_fifo_size = 682,
264 [2] = ST_ISM330DLC_ID,
267 [ST_LSM6DSX_ID_ACC] = {
269 .mask = GENMASK(2, 0),
271 [ST_LSM6DSX_ID_GYRO] = {
273 .mask = GENMASK(5, 3),
277 .read_fifo = st_lsm6dsx_read_fifo,
280 .mask = GENMASK(10, 0),
284 .mask = GENMASK(10, 0),
286 .th_wl = 3, /* 1LSB = 2B */
303 .mask = GENMASK(5, 3),
309 .max_fifo_size = 512,
314 [ST_LSM6DSX_ID_ACC] = {
316 .mask = GENMASK(3, 0),
318 [ST_LSM6DSX_ID_GYRO] = {
320 .mask = GENMASK(7, 4),
324 .read_fifo = st_lsm6dsx_read_tagged_fifo,
327 .mask = GENMASK(8, 0),
331 .mask = GENMASK(8, 0),
342 .mask = GENMASK(7, 6),
348 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
354 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
355 BIT(IIO_CHAN_INFO_SCALE), \
356 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
357 .scan_index = scan_idx, \
362 .endianness = IIO_LE, \
366 static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
367 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR,
369 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR,
371 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR,
373 IIO_CHAN_SOFT_TIMESTAMP(3),
376 static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
377 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR,
379 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR,
381 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR,
383 IIO_CHAN_SOFT_TIMESTAMP(3),
386 static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id)
390 for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
391 for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
392 if (id == st_lsm6dsx_sensor_settings[i].id[j])
395 if (j < ST_LSM6DSX_MAX_ID)
399 if (i == ARRAY_SIZE(st_lsm6dsx_sensor_settings)) {
400 dev_err(hw->dev, "unsupported hw id [%02x]\n", id);
404 err = regmap_read(hw->regmap, ST_LSM6DSX_REG_WHOAMI_ADDR, &data);
406 dev_err(hw->dev, "failed to read whoami register\n");
410 if (data != st_lsm6dsx_sensor_settings[i].wai) {
411 dev_err(hw->dev, "unsupported whoami [%02x]\n", data);
415 hw->settings = &st_lsm6dsx_sensor_settings[i];
420 static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor *sensor,
423 struct st_lsm6dsx_hw *hw = sensor->hw;
424 const struct st_lsm6dsx_reg *reg;
429 for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++)
430 if (st_lsm6dsx_fs_table[sensor->id].fs_avl[i].gain == gain)
433 if (i == ST_LSM6DSX_FS_LIST_SIZE)
436 val = st_lsm6dsx_fs_table[sensor->id].fs_avl[i].val;
437 reg = &st_lsm6dsx_fs_table[sensor->id].reg;
438 data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
439 err = st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data);
448 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val)
452 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
454 * ext devices can run at different odr respect to
457 if (st_lsm6dsx_odr_table[sensor->id].odr_avl[i].hz >= odr)
460 if (i == ST_LSM6DSX_ODR_LIST_SIZE)
463 *val = st_lsm6dsx_odr_table[sensor->id].odr_avl[i].val;
468 static u16 st_lsm6dsx_check_odr_dependency(struct st_lsm6dsx_hw *hw, u16 odr,
469 enum st_lsm6dsx_sensor_id id)
471 struct st_lsm6dsx_sensor *ref = iio_priv(hw->iio_devs[id]);
474 if (hw->enable_mask & BIT(id))
475 return max_t(u16, ref->odr, odr);
479 return (hw->enable_mask & BIT(id)) ? ref->odr : 0;
483 static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u16 req_odr)
485 struct st_lsm6dsx_sensor *ref_sensor = sensor;
486 struct st_lsm6dsx_hw *hw = sensor->hw;
487 const struct st_lsm6dsx_reg *reg;
492 switch (sensor->id) {
493 case ST_LSM6DSX_ID_EXT0:
494 case ST_LSM6DSX_ID_EXT1:
495 case ST_LSM6DSX_ID_EXT2:
496 case ST_LSM6DSX_ID_ACC: {
501 * i2c embedded controller relies on the accelerometer sensor as
502 * bus read/write trigger so we need to enable accel device
503 * at odr = max(accel_odr, ext_odr) in order to properly
504 * communicate with i2c slave devices
506 ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
507 for (i = ST_LSM6DSX_ID_ACC; i < ST_LSM6DSX_ID_MAX; i++) {
508 if (!hw->iio_devs[i] || i == sensor->id)
511 odr = st_lsm6dsx_check_odr_dependency(hw, req_odr, i);
513 /* device already configured */
523 err = st_lsm6dsx_check_odr(ref_sensor, req_odr, &val);
528 reg = &st_lsm6dsx_odr_table[ref_sensor->id].reg;
529 data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
530 return st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data);
533 int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor)
537 err = st_lsm6dsx_set_odr(sensor, sensor->odr);
541 sensor->hw->enable_mask |= BIT(sensor->id);
546 int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor)
548 struct st_lsm6dsx_hw *hw = sensor->hw;
549 const struct st_lsm6dsx_reg *reg;
553 reg = &st_lsm6dsx_odr_table[sensor->id].reg;
554 data = ST_LSM6DSX_SHIFT_VAL(0, reg->mask);
555 err = st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data);
559 sensor->hw->enable_mask &= ~BIT(sensor->id);
564 static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor *sensor,
567 struct st_lsm6dsx_hw *hw = sensor->hw;
571 err = st_lsm6dsx_sensor_enable(sensor);
575 delay = 1000000 / sensor->odr;
576 usleep_range(delay, 2 * delay);
578 err = st_lsm6dsx_read_locked(hw, addr, &data, sizeof(data));
582 st_lsm6dsx_sensor_disable(sensor);
584 *val = (s16)le16_to_cpu(data);
589 static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev,
590 struct iio_chan_spec const *ch,
591 int *val, int *val2, long mask)
593 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
597 case IIO_CHAN_INFO_RAW:
598 ret = iio_device_claim_direct_mode(iio_dev);
602 ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val);
603 iio_device_release_direct_mode(iio_dev);
605 case IIO_CHAN_INFO_SAMP_FREQ:
609 case IIO_CHAN_INFO_SCALE:
611 *val2 = sensor->gain;
612 ret = IIO_VAL_INT_PLUS_MICRO;
622 static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
623 struct iio_chan_spec const *chan,
624 int val, int val2, long mask)
626 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
629 err = iio_device_claim_direct_mode(iio_dev);
634 case IIO_CHAN_INFO_SCALE:
635 err = st_lsm6dsx_set_full_scale(sensor, val2);
637 case IIO_CHAN_INFO_SAMP_FREQ: {
640 err = st_lsm6dsx_check_odr(sensor, val, &data);
650 iio_device_release_direct_mode(iio_dev);
655 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val)
657 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
658 struct st_lsm6dsx_hw *hw = sensor->hw;
661 if (val < 1 || val > hw->settings->max_fifo_size)
664 mutex_lock(&hw->conf_lock);
666 err = st_lsm6dsx_update_watermark(sensor, val);
668 mutex_unlock(&hw->conf_lock);
673 sensor->watermark = val;
679 st_lsm6dsx_sysfs_sampling_frequency_avail(struct device *dev,
680 struct device_attribute *attr,
683 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
684 enum st_lsm6dsx_sensor_id id = sensor->id;
687 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
688 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
689 st_lsm6dsx_odr_table[id].odr_avl[i].hz);
695 static ssize_t st_lsm6dsx_sysfs_scale_avail(struct device *dev,
696 struct device_attribute *attr,
699 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
700 enum st_lsm6dsx_sensor_id id = sensor->id;
703 for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++)
704 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ",
705 st_lsm6dsx_fs_table[id].fs_avl[i].gain);
711 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail);
712 static IIO_DEVICE_ATTR(in_accel_scale_available, 0444,
713 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
714 static IIO_DEVICE_ATTR(in_anglvel_scale_available, 0444,
715 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
717 static struct attribute *st_lsm6dsx_acc_attributes[] = {
718 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
719 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
723 static const struct attribute_group st_lsm6dsx_acc_attribute_group = {
724 .attrs = st_lsm6dsx_acc_attributes,
727 static const struct iio_info st_lsm6dsx_acc_info = {
728 .attrs = &st_lsm6dsx_acc_attribute_group,
729 .read_raw = st_lsm6dsx_read_raw,
730 .write_raw = st_lsm6dsx_write_raw,
731 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
734 static struct attribute *st_lsm6dsx_gyro_attributes[] = {
735 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
736 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
740 static const struct attribute_group st_lsm6dsx_gyro_attribute_group = {
741 .attrs = st_lsm6dsx_gyro_attributes,
744 static const struct iio_info st_lsm6dsx_gyro_info = {
745 .attrs = &st_lsm6dsx_gyro_attribute_group,
746 .read_raw = st_lsm6dsx_read_raw,
747 .write_raw = st_lsm6dsx_write_raw,
748 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
751 static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
753 static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)
755 struct device_node *np = hw->dev->of_node;
760 return of_property_read_u32(np, "st,drdy-int-pin", drdy_pin);
763 static int st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw *hw, u8 *drdy_reg)
765 int err = 0, drdy_pin;
767 if (st_lsm6dsx_of_get_drdy_pin(hw, &drdy_pin) < 0) {
768 struct st_sensors_platform_data *pdata;
769 struct device *dev = hw->dev;
771 pdata = (struct st_sensors_platform_data *)dev->platform_data;
772 drdy_pin = pdata ? pdata->drdy_int_pin : 1;
777 *drdy_reg = ST_LSM6DSX_REG_INT1_ADDR;
780 *drdy_reg = ST_LSM6DSX_REG_INT2_ADDR;
783 dev_err(hw->dev, "unsupported data ready pin\n");
791 static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
793 const struct st_lsm6dsx_hw_ts_settings *ts_settings;
796 ts_settings = &hw->settings->ts_settings;
797 /* enable hw timestamp generation if necessary */
798 if (ts_settings->timer_en.addr) {
799 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->timer_en.mask);
800 err = regmap_update_bits(hw->regmap,
801 ts_settings->timer_en.addr,
802 ts_settings->timer_en.mask, val);
807 /* enable high resolution for hw ts timer if necessary */
808 if (ts_settings->hr_timer.addr) {
809 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->hr_timer.mask);
810 err = regmap_update_bits(hw->regmap,
811 ts_settings->hr_timer.addr,
812 ts_settings->hr_timer.mask, val);
817 /* enable ts queueing in FIFO if necessary */
818 if (ts_settings->fifo_en.addr) {
819 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->fifo_en.mask);
820 err = regmap_update_bits(hw->regmap,
821 ts_settings->fifo_en.addr,
822 ts_settings->fifo_en.mask, val);
829 static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
834 /* device sw reset */
835 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_RESET_ADDR,
836 ST_LSM6DSX_REG_RESET_MASK,
837 FIELD_PREP(ST_LSM6DSX_REG_RESET_MASK, 1));
843 /* reload trimming parameter */
844 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_RESET_ADDR,
845 ST_LSM6DSX_REG_BOOT_MASK,
846 FIELD_PREP(ST_LSM6DSX_REG_BOOT_MASK, 1));
852 /* enable Block Data Update */
853 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_BDU_ADDR,
854 ST_LSM6DSX_REG_BDU_MASK,
855 FIELD_PREP(ST_LSM6DSX_REG_BDU_MASK, 1));
859 /* enable FIFO watermak interrupt */
860 err = st_lsm6dsx_get_drdy_reg(hw, &drdy_int_reg);
864 err = regmap_update_bits(hw->regmap, drdy_int_reg,
865 ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK,
866 FIELD_PREP(ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK,
871 return st_lsm6dsx_init_hw_timer(hw);
874 static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw,
875 enum st_lsm6dsx_sensor_id id,
878 struct st_lsm6dsx_sensor *sensor;
879 struct iio_dev *iio_dev;
881 iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor));
885 iio_dev->modes = INDIO_DIRECT_MODE;
886 iio_dev->dev.parent = hw->dev;
887 iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks;
889 sensor = iio_priv(iio_dev);
892 sensor->odr = st_lsm6dsx_odr_table[id].odr_avl[0].hz;
893 sensor->gain = st_lsm6dsx_fs_table[id].fs_avl[0].gain;
894 sensor->watermark = 1;
897 case ST_LSM6DSX_ID_ACC:
898 iio_dev->channels = st_lsm6dsx_acc_channels;
899 iio_dev->num_channels = ARRAY_SIZE(st_lsm6dsx_acc_channels);
900 iio_dev->info = &st_lsm6dsx_acc_info;
902 scnprintf(sensor->name, sizeof(sensor->name), "%s_accel",
905 case ST_LSM6DSX_ID_GYRO:
906 iio_dev->channels = st_lsm6dsx_gyro_channels;
907 iio_dev->num_channels = ARRAY_SIZE(st_lsm6dsx_gyro_channels);
908 iio_dev->info = &st_lsm6dsx_gyro_info;
910 scnprintf(sensor->name, sizeof(sensor->name), "%s_gyro",
916 iio_dev->name = sensor->name;
921 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
922 struct regmap *regmap)
924 struct st_lsm6dsx_hw *hw;
927 hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
931 dev_set_drvdata(dev, (void *)hw);
933 mutex_init(&hw->fifo_lock);
934 mutex_init(&hw->conf_lock);
935 mutex_init(&hw->page_lock);
937 hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL);
945 err = st_lsm6dsx_check_whoami(hw, hw_id);
949 for (i = 0; i < ST_LSM6DSX_ID_EXT0; i++) {
950 hw->iio_devs[i] = st_lsm6dsx_alloc_iiodev(hw, i, name);
951 if (!hw->iio_devs[i])
955 err = st_lsm6dsx_init_device(hw);
960 err = st_lsm6dsx_fifo_setup(hw);
965 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
966 if (!hw->iio_devs[i])
969 err = devm_iio_device_register(hw->dev, hw->iio_devs[i]);
976 EXPORT_SYMBOL(st_lsm6dsx_probe);
978 static int __maybe_unused st_lsm6dsx_suspend(struct device *dev)
980 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
981 struct st_lsm6dsx_sensor *sensor;
982 const struct st_lsm6dsx_reg *reg;
986 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
987 if (!hw->iio_devs[i])
990 sensor = iio_priv(hw->iio_devs[i]);
991 if (!(hw->enable_mask & BIT(sensor->id)))
994 reg = &st_lsm6dsx_odr_table[sensor->id].reg;
995 data = ST_LSM6DSX_SHIFT_VAL(0, reg->mask);
996 err = st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask,
1002 if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS)
1003 err = st_lsm6dsx_flush_fifo(hw);
1008 static int __maybe_unused st_lsm6dsx_resume(struct device *dev)
1010 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
1011 struct st_lsm6dsx_sensor *sensor;
1014 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
1015 if (!hw->iio_devs[i])
1018 sensor = iio_priv(hw->iio_devs[i]);
1019 if (!(hw->enable_mask & BIT(sensor->id)))
1022 err = st_lsm6dsx_set_odr(sensor, sensor->odr);
1027 if (hw->enable_mask)
1028 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
1033 const struct dev_pm_ops st_lsm6dsx_pm_ops = {
1034 SET_SYSTEM_SLEEP_PM_OPS(st_lsm6dsx_suspend, st_lsm6dsx_resume)
1036 EXPORT_SYMBOL(st_lsm6dsx_pm_ops);
1038 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
1039 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1040 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver");
1041 MODULE_LICENSE("GPL v2");