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
26 * Copyright 2016 STMicroelectronics Inc.
28 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
29 * Denis Ciocca <denis.ciocca@st.com>
31 * Licensed under the GPL-2.
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/delay.h>
37 #include <linux/iio/iio.h>
38 #include <linux/iio/sysfs.h>
40 #include <linux/regmap.h>
41 #include <linux/bitfield.h>
43 #include <linux/platform_data/st_sensors_pdata.h>
45 #include "st_lsm6dsx.h"
47 #define ST_LSM6DSX_REG_INT1_ADDR 0x0d
48 #define ST_LSM6DSX_REG_INT2_ADDR 0x0e
49 #define ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK BIT(3)
50 #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f
51 #define ST_LSM6DSX_REG_RESET_ADDR 0x12
52 #define ST_LSM6DSX_REG_RESET_MASK BIT(0)
53 #define ST_LSM6DSX_REG_BDU_ADDR 0x12
54 #define ST_LSM6DSX_REG_BDU_MASK BIT(6)
55 #define ST_LSM6DSX_REG_INT2_ON_INT1_ADDR 0x13
56 #define ST_LSM6DSX_REG_INT2_ON_INT1_MASK BIT(5)
58 #define ST_LSM6DSX_REG_ACC_ODR_ADDR 0x10
59 #define ST_LSM6DSX_REG_ACC_ODR_MASK GENMASK(7, 4)
60 #define ST_LSM6DSX_REG_ACC_FS_ADDR 0x10
61 #define ST_LSM6DSX_REG_ACC_FS_MASK GENMASK(3, 2)
62 #define ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR 0x28
63 #define ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR 0x2a
64 #define ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR 0x2c
66 #define ST_LSM6DSX_REG_GYRO_ODR_ADDR 0x11
67 #define ST_LSM6DSX_REG_GYRO_ODR_MASK GENMASK(7, 4)
68 #define ST_LSM6DSX_REG_GYRO_FS_ADDR 0x11
69 #define ST_LSM6DSX_REG_GYRO_FS_MASK GENMASK(3, 2)
70 #define ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR 0x22
71 #define ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR 0x24
72 #define ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR 0x26
74 #define ST_LSM6DSX_ACC_FS_2G_GAIN IIO_G_TO_M_S_2(61)
75 #define ST_LSM6DSX_ACC_FS_4G_GAIN IIO_G_TO_M_S_2(122)
76 #define ST_LSM6DSX_ACC_FS_8G_GAIN IIO_G_TO_M_S_2(244)
77 #define ST_LSM6DSX_ACC_FS_16G_GAIN IIO_G_TO_M_S_2(488)
79 #define ST_LSM6DSX_GYRO_FS_245_GAIN IIO_DEGREE_TO_RAD(8750)
80 #define ST_LSM6DSX_GYRO_FS_500_GAIN IIO_DEGREE_TO_RAD(17500)
81 #define ST_LSM6DSX_GYRO_FS_1000_GAIN IIO_DEGREE_TO_RAD(35000)
82 #define ST_LSM6DSX_GYRO_FS_2000_GAIN IIO_DEGREE_TO_RAD(70000)
84 struct st_lsm6dsx_odr {
89 #define ST_LSM6DSX_ODR_LIST_SIZE 6
90 struct st_lsm6dsx_odr_table_entry {
91 struct st_lsm6dsx_reg reg;
92 struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
95 static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table[] = {
96 [ST_LSM6DSX_ID_ACC] = {
98 .addr = ST_LSM6DSX_REG_ACC_ODR_ADDR,
99 .mask = ST_LSM6DSX_REG_ACC_ODR_MASK,
101 .odr_avl[0] = { 13, 0x01 },
102 .odr_avl[1] = { 26, 0x02 },
103 .odr_avl[2] = { 52, 0x03 },
104 .odr_avl[3] = { 104, 0x04 },
105 .odr_avl[4] = { 208, 0x05 },
106 .odr_avl[5] = { 416, 0x06 },
108 [ST_LSM6DSX_ID_GYRO] = {
110 .addr = ST_LSM6DSX_REG_GYRO_ODR_ADDR,
111 .mask = ST_LSM6DSX_REG_GYRO_ODR_MASK,
113 .odr_avl[0] = { 13, 0x01 },
114 .odr_avl[1] = { 26, 0x02 },
115 .odr_avl[2] = { 52, 0x03 },
116 .odr_avl[3] = { 104, 0x04 },
117 .odr_avl[4] = { 208, 0x05 },
118 .odr_avl[5] = { 416, 0x06 },
122 struct st_lsm6dsx_fs {
127 #define ST_LSM6DSX_FS_LIST_SIZE 4
128 struct st_lsm6dsx_fs_table_entry {
129 struct st_lsm6dsx_reg reg;
130 struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
133 static const struct st_lsm6dsx_fs_table_entry st_lsm6dsx_fs_table[] = {
134 [ST_LSM6DSX_ID_ACC] = {
136 .addr = ST_LSM6DSX_REG_ACC_FS_ADDR,
137 .mask = ST_LSM6DSX_REG_ACC_FS_MASK,
139 .fs_avl[0] = { ST_LSM6DSX_ACC_FS_2G_GAIN, 0x0 },
140 .fs_avl[1] = { ST_LSM6DSX_ACC_FS_4G_GAIN, 0x2 },
141 .fs_avl[2] = { ST_LSM6DSX_ACC_FS_8G_GAIN, 0x3 },
142 .fs_avl[3] = { ST_LSM6DSX_ACC_FS_16G_GAIN, 0x1 },
144 [ST_LSM6DSX_ID_GYRO] = {
146 .addr = ST_LSM6DSX_REG_GYRO_FS_ADDR,
147 .mask = ST_LSM6DSX_REG_GYRO_FS_MASK,
149 .fs_avl[0] = { ST_LSM6DSX_GYRO_FS_245_GAIN, 0x0 },
150 .fs_avl[1] = { ST_LSM6DSX_GYRO_FS_500_GAIN, 0x1 },
151 .fs_avl[2] = { ST_LSM6DSX_GYRO_FS_1000_GAIN, 0x2 },
152 .fs_avl[3] = { ST_LSM6DSX_GYRO_FS_2000_GAIN, 0x3 },
156 static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
159 .max_fifo_size = 1365,
164 [ST_LSM6DSX_ID_ACC] = {
166 .mask = GENMASK(2, 0),
168 [ST_LSM6DSX_ID_GYRO] = {
170 .mask = GENMASK(5, 3),
176 .mask = GENMASK(11, 0),
180 .mask = GENMASK(11, 0),
182 .th_wl = 3, /* 1LSB = 2B */
199 .mask = GENMASK(5, 3),
205 .max_fifo_size = 682,
207 [0] = ST_LSM6DS3H_ID,
210 [ST_LSM6DSX_ID_ACC] = {
212 .mask = GENMASK(2, 0),
214 [ST_LSM6DSX_ID_GYRO] = {
216 .mask = GENMASK(5, 3),
222 .mask = GENMASK(11, 0),
226 .mask = GENMASK(11, 0),
228 .th_wl = 3, /* 1LSB = 2B */
245 .mask = GENMASK(5, 3),
251 .max_fifo_size = 682,
255 [2] = ST_ISM330DLC_ID,
258 [ST_LSM6DSX_ID_ACC] = {
260 .mask = GENMASK(2, 0),
262 [ST_LSM6DSX_ID_GYRO] = {
264 .mask = GENMASK(5, 3),
270 .mask = GENMASK(10, 0),
274 .mask = GENMASK(10, 0),
276 .th_wl = 3, /* 1LSB = 2B */
293 .mask = GENMASK(5, 3),
299 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
305 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
306 BIT(IIO_CHAN_INFO_SCALE), \
307 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
308 .scan_index = scan_idx, \
313 .endianness = IIO_LE, \
317 static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
318 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR,
320 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR,
322 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR,
324 IIO_CHAN_SOFT_TIMESTAMP(3),
327 static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
328 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR,
330 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR,
332 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR,
334 IIO_CHAN_SOFT_TIMESTAMP(3),
337 static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id)
341 for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
342 for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
343 if (id == st_lsm6dsx_sensor_settings[i].id[j])
346 if (j < ST_LSM6DSX_MAX_ID)
350 if (i == ARRAY_SIZE(st_lsm6dsx_sensor_settings)) {
351 dev_err(hw->dev, "unsupported hw id [%02x]\n", id);
355 err = regmap_read(hw->regmap, ST_LSM6DSX_REG_WHOAMI_ADDR, &data);
357 dev_err(hw->dev, "failed to read whoami register\n");
361 if (data != st_lsm6dsx_sensor_settings[i].wai) {
362 dev_err(hw->dev, "unsupported whoami [%02x]\n", data);
366 hw->settings = &st_lsm6dsx_sensor_settings[i];
371 static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor *sensor,
374 struct st_lsm6dsx_hw *hw = sensor->hw;
375 const struct st_lsm6dsx_reg *reg;
379 for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++)
380 if (st_lsm6dsx_fs_table[sensor->id].fs_avl[i].gain == gain)
383 if (i == ST_LSM6DSX_FS_LIST_SIZE)
386 val = st_lsm6dsx_fs_table[sensor->id].fs_avl[i].val;
387 reg = &st_lsm6dsx_fs_table[sensor->id].reg;
388 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
389 ST_LSM6DSX_SHIFT_VAL(val, reg->mask));
398 static int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr,
403 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
404 if (st_lsm6dsx_odr_table[sensor->id].odr_avl[i].hz == odr)
407 if (i == ST_LSM6DSX_ODR_LIST_SIZE)
410 *val = st_lsm6dsx_odr_table[sensor->id].odr_avl[i].val;
415 static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u16 odr)
417 struct st_lsm6dsx_hw *hw = sensor->hw;
418 const struct st_lsm6dsx_reg *reg;
422 err = st_lsm6dsx_check_odr(sensor, odr, &val);
426 reg = &st_lsm6dsx_odr_table[sensor->id].reg;
427 return regmap_update_bits(hw->regmap, reg->addr, reg->mask,
428 ST_LSM6DSX_SHIFT_VAL(val, reg->mask));
431 int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor)
435 err = st_lsm6dsx_set_odr(sensor, sensor->odr);
439 sensor->hw->enable_mask |= BIT(sensor->id);
444 int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor)
446 struct st_lsm6dsx_hw *hw = sensor->hw;
447 const struct st_lsm6dsx_reg *reg;
450 reg = &st_lsm6dsx_odr_table[sensor->id].reg;
451 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
452 ST_LSM6DSX_SHIFT_VAL(0, reg->mask));
456 sensor->hw->enable_mask &= ~BIT(sensor->id);
461 static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor *sensor,
464 struct st_lsm6dsx_hw *hw = sensor->hw;
468 err = st_lsm6dsx_sensor_enable(sensor);
472 delay = 1000000 / sensor->odr;
473 usleep_range(delay, 2 * delay);
475 err = regmap_bulk_read(hw->regmap, addr, &data, sizeof(data));
479 st_lsm6dsx_sensor_disable(sensor);
481 *val = (s16)le16_to_cpu(data);
486 static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev,
487 struct iio_chan_spec const *ch,
488 int *val, int *val2, long mask)
490 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
494 case IIO_CHAN_INFO_RAW:
495 ret = iio_device_claim_direct_mode(iio_dev);
499 ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val);
500 iio_device_release_direct_mode(iio_dev);
502 case IIO_CHAN_INFO_SAMP_FREQ:
506 case IIO_CHAN_INFO_SCALE:
508 *val2 = sensor->gain;
509 ret = IIO_VAL_INT_PLUS_MICRO;
519 static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
520 struct iio_chan_spec const *chan,
521 int val, int val2, long mask)
523 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
526 err = iio_device_claim_direct_mode(iio_dev);
531 case IIO_CHAN_INFO_SCALE:
532 err = st_lsm6dsx_set_full_scale(sensor, val2);
534 case IIO_CHAN_INFO_SAMP_FREQ: {
537 err = st_lsm6dsx_check_odr(sensor, val, &data);
547 iio_device_release_direct_mode(iio_dev);
552 static int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val)
554 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
555 struct st_lsm6dsx_hw *hw = sensor->hw;
558 if (val < 1 || val > hw->settings->max_fifo_size)
561 mutex_lock(&hw->conf_lock);
563 err = st_lsm6dsx_update_watermark(sensor, val);
565 mutex_unlock(&hw->conf_lock);
570 sensor->watermark = val;
576 st_lsm6dsx_sysfs_sampling_frequency_avail(struct device *dev,
577 struct device_attribute *attr,
580 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
581 enum st_lsm6dsx_sensor_id id = sensor->id;
584 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
585 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
586 st_lsm6dsx_odr_table[id].odr_avl[i].hz);
592 static ssize_t st_lsm6dsx_sysfs_scale_avail(struct device *dev,
593 struct device_attribute *attr,
596 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
597 enum st_lsm6dsx_sensor_id id = sensor->id;
600 for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++)
601 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ",
602 st_lsm6dsx_fs_table[id].fs_avl[i].gain);
608 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail);
609 static IIO_DEVICE_ATTR(in_accel_scale_available, 0444,
610 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
611 static IIO_DEVICE_ATTR(in_anglvel_scale_available, 0444,
612 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
614 static struct attribute *st_lsm6dsx_acc_attributes[] = {
615 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
616 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
620 static const struct attribute_group st_lsm6dsx_acc_attribute_group = {
621 .attrs = st_lsm6dsx_acc_attributes,
624 static const struct iio_info st_lsm6dsx_acc_info = {
625 .attrs = &st_lsm6dsx_acc_attribute_group,
626 .read_raw = st_lsm6dsx_read_raw,
627 .write_raw = st_lsm6dsx_write_raw,
628 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
631 static struct attribute *st_lsm6dsx_gyro_attributes[] = {
632 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
633 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
637 static const struct attribute_group st_lsm6dsx_gyro_attribute_group = {
638 .attrs = st_lsm6dsx_gyro_attributes,
641 static const struct iio_info st_lsm6dsx_gyro_info = {
642 .attrs = &st_lsm6dsx_gyro_attribute_group,
643 .read_raw = st_lsm6dsx_read_raw,
644 .write_raw = st_lsm6dsx_write_raw,
645 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
648 static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
650 static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)
652 struct device_node *np = hw->dev->of_node;
657 return of_property_read_u32(np, "st,drdy-int-pin", drdy_pin);
660 static int st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw *hw, u8 *drdy_reg)
662 int err = 0, drdy_pin;
664 if (st_lsm6dsx_of_get_drdy_pin(hw, &drdy_pin) < 0) {
665 struct st_sensors_platform_data *pdata;
666 struct device *dev = hw->dev;
668 pdata = (struct st_sensors_platform_data *)dev->platform_data;
669 drdy_pin = pdata ? pdata->drdy_int_pin : 1;
674 *drdy_reg = ST_LSM6DSX_REG_INT1_ADDR;
677 *drdy_reg = ST_LSM6DSX_REG_INT2_ADDR;
680 dev_err(hw->dev, "unsupported data ready pin\n");
688 static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
690 const struct st_lsm6dsx_hw_ts_settings *ts_settings;
693 ts_settings = &hw->settings->ts_settings;
694 /* enable hw timestamp generation if necessary */
695 if (ts_settings->timer_en.addr) {
696 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->timer_en.mask);
697 err = regmap_update_bits(hw->regmap,
698 ts_settings->timer_en.addr,
699 ts_settings->timer_en.mask, val);
704 /* enable high resolution for hw ts timer if necessary */
705 if (ts_settings->hr_timer.addr) {
706 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->hr_timer.mask);
707 err = regmap_update_bits(hw->regmap,
708 ts_settings->hr_timer.addr,
709 ts_settings->hr_timer.mask, val);
714 /* enable ts queueing in FIFO if necessary */
715 if (ts_settings->fifo_en.addr) {
716 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->fifo_en.mask);
717 err = regmap_update_bits(hw->regmap,
718 ts_settings->fifo_en.addr,
719 ts_settings->fifo_en.mask, val);
726 static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
731 err = regmap_write(hw->regmap, ST_LSM6DSX_REG_RESET_ADDR,
732 ST_LSM6DSX_REG_RESET_MASK);
738 /* enable Block Data Update */
739 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_BDU_ADDR,
740 ST_LSM6DSX_REG_BDU_MASK,
741 FIELD_PREP(ST_LSM6DSX_REG_BDU_MASK, 1));
745 /* enable FIFO watermak interrupt */
746 err = st_lsm6dsx_get_drdy_reg(hw, &drdy_int_reg);
750 err = regmap_update_bits(hw->regmap, drdy_int_reg,
751 ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK,
752 FIELD_PREP(ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK,
757 return st_lsm6dsx_init_hw_timer(hw);
760 static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw,
761 enum st_lsm6dsx_sensor_id id,
764 struct st_lsm6dsx_sensor *sensor;
765 struct iio_dev *iio_dev;
767 iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor));
771 iio_dev->modes = INDIO_DIRECT_MODE;
772 iio_dev->dev.parent = hw->dev;
773 iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks;
775 sensor = iio_priv(iio_dev);
778 sensor->odr = st_lsm6dsx_odr_table[id].odr_avl[0].hz;
779 sensor->gain = st_lsm6dsx_fs_table[id].fs_avl[0].gain;
780 sensor->watermark = 1;
783 case ST_LSM6DSX_ID_ACC:
784 iio_dev->channels = st_lsm6dsx_acc_channels;
785 iio_dev->num_channels = ARRAY_SIZE(st_lsm6dsx_acc_channels);
786 iio_dev->info = &st_lsm6dsx_acc_info;
788 scnprintf(sensor->name, sizeof(sensor->name), "%s_accel",
791 case ST_LSM6DSX_ID_GYRO:
792 iio_dev->channels = st_lsm6dsx_gyro_channels;
793 iio_dev->num_channels = ARRAY_SIZE(st_lsm6dsx_gyro_channels);
794 iio_dev->info = &st_lsm6dsx_gyro_info;
796 scnprintf(sensor->name, sizeof(sensor->name), "%s_gyro",
802 iio_dev->name = sensor->name;
807 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
808 struct regmap *regmap)
810 struct st_lsm6dsx_hw *hw;
813 hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
817 dev_set_drvdata(dev, (void *)hw);
819 mutex_init(&hw->fifo_lock);
820 mutex_init(&hw->conf_lock);
822 hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL);
830 err = st_lsm6dsx_check_whoami(hw, hw_id);
834 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
835 hw->iio_devs[i] = st_lsm6dsx_alloc_iiodev(hw, i, name);
836 if (!hw->iio_devs[i])
840 err = st_lsm6dsx_init_device(hw);
845 err = st_lsm6dsx_fifo_setup(hw);
850 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
851 err = devm_iio_device_register(hw->dev, hw->iio_devs[i]);
858 EXPORT_SYMBOL(st_lsm6dsx_probe);
860 static int __maybe_unused st_lsm6dsx_suspend(struct device *dev)
862 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
863 struct st_lsm6dsx_sensor *sensor;
864 const struct st_lsm6dsx_reg *reg;
867 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
868 sensor = iio_priv(hw->iio_devs[i]);
869 if (!(hw->enable_mask & BIT(sensor->id)))
872 reg = &st_lsm6dsx_odr_table[sensor->id].reg;
873 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
874 ST_LSM6DSX_SHIFT_VAL(0, reg->mask));
879 if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS)
880 err = st_lsm6dsx_flush_fifo(hw);
885 static int __maybe_unused st_lsm6dsx_resume(struct device *dev)
887 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
888 struct st_lsm6dsx_sensor *sensor;
891 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
892 sensor = iio_priv(hw->iio_devs[i]);
893 if (!(hw->enable_mask & BIT(sensor->id)))
896 err = st_lsm6dsx_set_odr(sensor, sensor->odr);
902 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
907 const struct dev_pm_ops st_lsm6dsx_pm_ops = {
908 SET_SYSTEM_SLEEP_PM_OPS(st_lsm6dsx_suspend, st_lsm6dsx_resume)
910 EXPORT_SYMBOL(st_lsm6dsx_pm_ops);
912 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
913 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
914 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver");
915 MODULE_LICENSE("GPL v2");