1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics st_lsm6dsx FIFO buffer library driver
5 * LSM6DS3/LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC/LSM6DS3TR-C:
6 * The FIFO buffer can be configured to store data from gyroscope and
7 * accelerometer. Samples are queued without any tag according to a
8 * specific pattern based on 'FIFO data sets' (6 bytes each):
9 * - 1st data set is reserved for gyroscope data
10 * - 2nd data set is reserved for accelerometer data
11 * The FIFO pattern changes depending on the ODRs and decimation factors
12 * assigned to the FIFO data sets. The first sequence of data stored in FIFO
13 * buffer contains the data of all the enabled FIFO data sets
14 * (e.g. Gx, Gy, Gz, Ax, Ay, Az), then data are repeated depending on the
15 * value of the decimation factor and ODR set for each FIFO data set.
17 * LSM6DSO/LSM6DSOX/ASM330LHH/ASM330LHHX/LSM6DSR/LSM6DSRX/ISM330DHCX/
18 * LSM6DST/LSM6DSOP/LSM6DSTX/LSM6DSV/ASM330LHB:
19 * The FIFO buffer can be configured to store data from gyroscope and
20 * accelerometer. Each sample is queued with a tag (1B) indicating data
21 * source (gyroscope, accelerometer, hw timer).
23 * FIFO supported modes:
24 * - BYPASS: FIFO disabled
25 * - CONTINUOUS: FIFO enabled. When the buffer is full, the FIFO index
26 * restarts from the beginning and the oldest sample is overwritten
28 * Copyright 2016 STMicroelectronics Inc.
30 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
31 * Denis Ciocca <denis.ciocca@st.com>
33 #include <linux/module.h>
34 #include <linux/iio/kfifo_buf.h>
35 #include <linux/iio/iio.h>
36 #include <linux/iio/buffer.h>
37 #include <linux/regmap.h>
38 #include <linux/bitfield.h>
40 #include <linux/platform_data/st_sensors_pdata.h>
42 #include "st_lsm6dsx.h"
44 #define ST_LSM6DSX_REG_FIFO_MODE_ADDR 0x0a
45 #define ST_LSM6DSX_FIFO_MODE_MASK GENMASK(2, 0)
46 #define ST_LSM6DSX_FIFO_ODR_MASK GENMASK(6, 3)
47 #define ST_LSM6DSX_FIFO_EMPTY_MASK BIT(12)
48 #define ST_LSM6DSX_REG_FIFO_OUTL_ADDR 0x3e
49 #define ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR 0x78
50 #define ST_LSM6DSX_REG_TS_RESET_ADDR 0x42
52 #define ST_LSM6DSX_MAX_FIFO_ODR_VAL 0x08
54 #define ST_LSM6DSX_TS_RESET_VAL 0xaa
56 struct st_lsm6dsx_decimator_entry {
61 enum st_lsm6dsx_fifo_tag {
62 ST_LSM6DSX_GYRO_TAG = 0x01,
63 ST_LSM6DSX_ACC_TAG = 0x02,
64 ST_LSM6DSX_TS_TAG = 0x04,
65 ST_LSM6DSX_EXT0_TAG = 0x0f,
66 ST_LSM6DSX_EXT1_TAG = 0x10,
67 ST_LSM6DSX_EXT2_TAG = 0x11,
71 struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = {
83 st_lsm6dsx_get_decimator_val(struct st_lsm6dsx_sensor *sensor, u32 max_odr)
85 const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
86 u32 decimator = max_odr / sensor->odr;
90 decimator = round_down(decimator, 2);
92 for (i = 0; i < max_size; i++) {
93 if (st_lsm6dsx_decimator_table[i].decimator == decimator)
97 sensor->decimator = decimator;
98 return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
101 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
102 u32 *max_odr, u32 *min_odr)
104 struct st_lsm6dsx_sensor *sensor;
107 *max_odr = 0, *min_odr = ~0;
108 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
109 if (!hw->iio_devs[i])
112 sensor = iio_priv(hw->iio_devs[i]);
114 if (!(hw->enable_mask & BIT(sensor->id)))
117 *max_odr = max_t(u32, *max_odr, sensor->odr);
118 *min_odr = min_t(u32, *min_odr, sensor->odr);
122 static u8 st_lsm6dsx_get_sip(struct st_lsm6dsx_sensor *sensor, u32 min_odr)
124 u8 sip = sensor->odr / min_odr;
126 return sip > 1 ? round_down(sip, 2) : sip;
129 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
131 const struct st_lsm6dsx_reg *ts_dec_reg;
132 struct st_lsm6dsx_sensor *sensor;
133 u16 sip = 0, ts_sip = 0;
134 u32 max_odr, min_odr;
138 st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr);
140 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
141 const struct st_lsm6dsx_reg *dec_reg;
143 if (!hw->iio_devs[i])
146 sensor = iio_priv(hw->iio_devs[i]);
147 /* update fifo decimators and sample in pattern */
148 if (hw->enable_mask & BIT(sensor->id)) {
149 sensor->sip = st_lsm6dsx_get_sip(sensor, min_odr);
150 data = st_lsm6dsx_get_decimator_val(sensor, max_odr);
155 ts_sip = max_t(u16, ts_sip, sensor->sip);
157 dec_reg = &hw->settings->decimator[sensor->id];
159 int val = ST_LSM6DSX_SHIFT_VAL(data, dec_reg->mask);
161 err = st_lsm6dsx_update_bits_locked(hw, dec_reg->addr,
169 hw->sip = sip + ts_sip;
173 * update hw ts decimator if necessary. Decimator for hw timestamp
174 * is always 1 or 0 in order to have a ts sample for each data
177 ts_dec_reg = &hw->settings->ts_settings.decimator;
178 if (ts_dec_reg->addr) {
179 int val, ts_dec = !!hw->ts_sip;
181 val = ST_LSM6DSX_SHIFT_VAL(ts_dec, ts_dec_reg->mask);
182 err = st_lsm6dsx_update_bits_locked(hw, ts_dec_reg->addr,
183 ts_dec_reg->mask, val);
188 static int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
189 enum st_lsm6dsx_fifo_mode fifo_mode)
193 data = FIELD_PREP(ST_LSM6DSX_FIFO_MODE_MASK, fifo_mode);
194 return st_lsm6dsx_update_bits_locked(hw, ST_LSM6DSX_REG_FIFO_MODE_ADDR,
195 ST_LSM6DSX_FIFO_MODE_MASK, data);
198 static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor,
201 struct st_lsm6dsx_hw *hw = sensor->hw;
202 const struct st_lsm6dsx_reg *batch_reg;
205 batch_reg = &hw->settings->batch[sensor->id];
206 if (batch_reg->addr) {
212 err = st_lsm6dsx_check_odr(sensor, sensor->odr,
219 val = ST_LSM6DSX_SHIFT_VAL(data, batch_reg->mask);
220 return st_lsm6dsx_update_bits_locked(hw, batch_reg->addr,
221 batch_reg->mask, val);
223 data = hw->enable_mask ? ST_LSM6DSX_MAX_FIFO_ODR_VAL : 0;
224 return st_lsm6dsx_update_bits_locked(hw,
225 ST_LSM6DSX_REG_FIFO_MODE_ADDR,
226 ST_LSM6DSX_FIFO_ODR_MASK,
227 FIELD_PREP(ST_LSM6DSX_FIFO_ODR_MASK,
232 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark)
234 u16 fifo_watermark = ~0, cur_watermark, fifo_th_mask;
235 struct st_lsm6dsx_hw *hw = sensor->hw;
236 struct st_lsm6dsx_sensor *cur_sensor;
243 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
244 if (!hw->iio_devs[i])
247 cur_sensor = iio_priv(hw->iio_devs[i]);
249 if (!(hw->enable_mask & BIT(cur_sensor->id)))
252 cur_watermark = (cur_sensor == sensor) ? watermark
253 : cur_sensor->watermark;
255 fifo_watermark = min_t(u16, fifo_watermark, cur_watermark);
258 fifo_watermark = max_t(u16, fifo_watermark, hw->sip);
259 fifo_watermark = (fifo_watermark / hw->sip) * hw->sip;
260 fifo_watermark = fifo_watermark * hw->settings->fifo_ops.th_wl;
262 mutex_lock(&hw->page_lock);
263 err = regmap_read(hw->regmap, hw->settings->fifo_ops.fifo_th.addr + 1,
268 fifo_th_mask = hw->settings->fifo_ops.fifo_th.mask;
269 fifo_watermark = ((data << 8) & ~fifo_th_mask) |
270 (fifo_watermark & fifo_th_mask);
272 wdata = cpu_to_le16(fifo_watermark);
273 err = regmap_bulk_write(hw->regmap,
274 hw->settings->fifo_ops.fifo_th.addr,
275 &wdata, sizeof(wdata));
277 mutex_unlock(&hw->page_lock);
281 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw *hw)
283 struct st_lsm6dsx_sensor *sensor;
286 /* reset hw ts counter */
287 err = st_lsm6dsx_write_locked(hw, ST_LSM6DSX_REG_TS_RESET_ADDR,
288 ST_LSM6DSX_TS_RESET_VAL);
292 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
293 if (!hw->iio_devs[i])
296 sensor = iio_priv(hw->iio_devs[i]);
298 * store enable buffer timestamp as reference for
301 sensor->ts_ref = iio_get_time_ns(hw->iio_devs[i]);
306 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw)
310 /* reset hw ts counter */
311 err = st_lsm6dsx_reset_hw_ts(hw);
315 return st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
319 * Set max bulk read to ST_LSM6DSX_MAX_WORD_LEN/ST_LSM6DSX_MAX_TAGGED_WORD_LEN
320 * in order to avoid a kmalloc for each bus access
322 static inline int st_lsm6dsx_read_block(struct st_lsm6dsx_hw *hw, u8 addr,
323 u8 *data, unsigned int data_len,
324 unsigned int max_word_len)
326 unsigned int word_len, read_len = 0;
329 while (read_len < data_len) {
330 word_len = min_t(unsigned int, data_len - read_len,
332 err = st_lsm6dsx_read_locked(hw, addr, data + read_len,
336 read_len += word_len;
341 #define ST_LSM6DSX_IIO_BUFF_SIZE (ALIGN(ST_LSM6DSX_SAMPLE_SIZE, \
342 sizeof(s64)) + sizeof(s64))
344 * st_lsm6dsx_read_fifo() - hw FIFO read routine
345 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
347 * Read samples from the hw FIFO and push them to IIO buffers.
349 * Return: Number of bytes read from the FIFO
351 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
353 struct st_lsm6dsx_sensor *acc_sensor, *gyro_sensor, *ext_sensor = NULL;
354 int err, sip, acc_sip, gyro_sip, ts_sip, ext_sip, read_len, offset;
355 u16 fifo_len, pattern_len = hw->sip * ST_LSM6DSX_SAMPLE_SIZE;
356 u16 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
357 bool reset_ts = false;
361 err = st_lsm6dsx_read_locked(hw,
362 hw->settings->fifo_ops.fifo_diff.addr,
363 &fifo_status, sizeof(fifo_status));
365 dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
370 if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
373 fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
374 ST_LSM6DSX_CHAN_SIZE;
375 fifo_len = (fifo_len / pattern_len) * pattern_len;
377 acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
378 gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
379 if (hw->iio_devs[ST_LSM6DSX_ID_EXT0])
380 ext_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_EXT0]);
382 for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
383 err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
384 hw->buff, pattern_len,
385 ST_LSM6DSX_MAX_WORD_LEN);
388 "failed to read pattern from fifo (err=%d)\n",
394 * Data are written to the FIFO with a specific pattern
395 * depending on the configured ODRs. The first sequence of data
396 * stored in FIFO contains the data of all enabled sensors
397 * (e.g. Gx, Gy, Gz, Ax, Ay, Az, Ts), then data are repeated
398 * depending on the value of the decimation factor set for each
401 * Supposing the FIFO is storing data from gyroscope and
402 * accelerometer at different ODRs:
403 * - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
404 * Since the gyroscope ODR is twice the accelerometer one, the
405 * following pattern is repeated every 9 samples:
406 * - Gx, Gy, Gz, Ax, Ay, Az, Ts, Gx, Gy, Gz, Ts, Gx, ..
408 ext_sip = ext_sensor ? ext_sensor->sip : 0;
409 gyro_sip = gyro_sensor->sip;
410 acc_sip = acc_sensor->sip;
415 while (acc_sip > 0 || gyro_sip > 0 || ext_sip > 0) {
416 if (gyro_sip > 0 && !(sip % gyro_sensor->decimator)) {
417 memcpy(hw->scan[ST_LSM6DSX_ID_GYRO].channels,
419 sizeof(hw->scan[ST_LSM6DSX_ID_GYRO].channels));
420 offset += sizeof(hw->scan[ST_LSM6DSX_ID_GYRO].channels);
422 if (acc_sip > 0 && !(sip % acc_sensor->decimator)) {
423 memcpy(hw->scan[ST_LSM6DSX_ID_ACC].channels,
425 sizeof(hw->scan[ST_LSM6DSX_ID_ACC].channels));
426 offset += sizeof(hw->scan[ST_LSM6DSX_ID_ACC].channels);
428 if (ext_sip > 0 && !(sip % ext_sensor->decimator)) {
429 memcpy(hw->scan[ST_LSM6DSX_ID_EXT0].channels,
431 sizeof(hw->scan[ST_LSM6DSX_ID_EXT0].channels));
432 offset += sizeof(hw->scan[ST_LSM6DSX_ID_EXT0].channels);
436 u8 data[ST_LSM6DSX_SAMPLE_SIZE];
438 memcpy(data, &hw->buff[offset], sizeof(data));
440 * hw timestamp is 3B long and it is stored
441 * in FIFO using 6B as 4th FIFO data set
442 * according to this schema:
443 * B0 = ts[15:8], B1 = ts[23:16], B3 = ts[7:0]
445 ts = data[1] << 16 | data[0] << 8 | data[3];
447 * check if hw timestamp engine is going to
448 * reset (the sensor generates an interrupt
449 * to signal the hw timestamp will reset in
452 if (!reset_ts && ts >= 0xff0000)
456 offset += ST_LSM6DSX_SAMPLE_SIZE;
459 if (gyro_sip > 0 && !(sip % gyro_sensor->decimator)) {
461 * We need to discards gyro samples during
462 * filters settling time
464 if (gyro_sensor->samples_to_discard > 0)
465 gyro_sensor->samples_to_discard--;
467 iio_push_to_buffers_with_timestamp(
468 hw->iio_devs[ST_LSM6DSX_ID_GYRO],
469 &hw->scan[ST_LSM6DSX_ID_GYRO],
470 gyro_sensor->ts_ref + ts);
473 if (acc_sip > 0 && !(sip % acc_sensor->decimator)) {
475 * We need to discards accel samples during
476 * filters settling time
478 if (acc_sensor->samples_to_discard > 0)
479 acc_sensor->samples_to_discard--;
481 iio_push_to_buffers_with_timestamp(
482 hw->iio_devs[ST_LSM6DSX_ID_ACC],
483 &hw->scan[ST_LSM6DSX_ID_ACC],
484 acc_sensor->ts_ref + ts);
487 if (ext_sip > 0 && !(sip % ext_sensor->decimator)) {
488 iio_push_to_buffers_with_timestamp(
489 hw->iio_devs[ST_LSM6DSX_ID_EXT0],
490 &hw->scan[ST_LSM6DSX_ID_EXT0],
491 ext_sensor->ts_ref + ts);
498 if (unlikely(reset_ts)) {
499 err = st_lsm6dsx_reset_hw_ts(hw);
501 dev_err(hw->dev, "failed to reset hw ts (err=%d)\n",
509 #define ST_LSM6DSX_INVALID_SAMPLE 0x7ffd
511 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
514 s16 val = le16_to_cpu(*(__le16 *)data);
515 struct st_lsm6dsx_sensor *sensor;
516 struct iio_dev *iio_dev;
518 /* invalid sample during bootstrap phase */
519 if (val >= ST_LSM6DSX_INVALID_SAMPLE)
523 * EXT_TAG are managed in FIFO fashion so ST_LSM6DSX_EXT0_TAG
524 * corresponds to the first enabled channel, ST_LSM6DSX_EXT1_TAG
525 * to the second one and ST_LSM6DSX_EXT2_TAG to the last enabled
529 case ST_LSM6DSX_GYRO_TAG:
530 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_GYRO];
532 case ST_LSM6DSX_ACC_TAG:
533 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_ACC];
535 case ST_LSM6DSX_EXT0_TAG:
536 if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0))
537 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT0];
538 else if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1))
539 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
541 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
543 case ST_LSM6DSX_EXT1_TAG:
544 if ((hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0)) &&
545 (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1)))
546 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
548 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
550 case ST_LSM6DSX_EXT2_TAG:
551 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
557 sensor = iio_priv(iio_dev);
558 iio_push_to_buffers_with_timestamp(iio_dev, data,
559 ts + sensor->ts_ref);
565 * st_lsm6dsx_read_tagged_fifo() - tagged hw FIFO read routine
566 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
568 * Read samples from the hw FIFO and push them to IIO buffers.
570 * Return: Number of bytes read from the FIFO
572 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
574 u16 pattern_len = hw->sip * ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
575 u16 fifo_len, fifo_diff_mask;
577 * Alignment needed as this can ultimately be passed to a
578 * call to iio_push_to_buffers_with_timestamp() which
579 * must be passed a buffer that is aligned to 8 bytes so
580 * as to allow insertion of a naturally aligned timestamp.
582 u8 iio_buff[ST_LSM6DSX_IIO_BUFF_SIZE] __aligned(8);
584 bool reset_ts = false;
585 int i, err, read_len;
589 err = st_lsm6dsx_read_locked(hw,
590 hw->settings->fifo_ops.fifo_diff.addr,
591 &fifo_status, sizeof(fifo_status));
593 dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
598 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
599 fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
600 ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
604 for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
605 err = st_lsm6dsx_read_block(hw,
606 ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
607 hw->buff, pattern_len,
608 ST_LSM6DSX_MAX_TAGGED_WORD_LEN);
611 "failed to read pattern from fifo (err=%d)\n",
616 for (i = 0; i < pattern_len;
617 i += ST_LSM6DSX_TAGGED_SAMPLE_SIZE) {
618 memcpy(iio_buff, &hw->buff[i + ST_LSM6DSX_TAG_SIZE],
619 ST_LSM6DSX_SAMPLE_SIZE);
621 tag = hw->buff[i] >> 3;
622 if (tag == ST_LSM6DSX_TS_TAG) {
624 * hw timestamp is 4B long and it is stored
625 * in FIFO according to this schema:
626 * B0 = ts[7:0], B1 = ts[15:8], B2 = ts[23:16],
629 ts = le32_to_cpu(*((__le32 *)iio_buff));
631 * check if hw timestamp engine is going to
632 * reset (the sensor generates an interrupt
633 * to signal the hw timestamp will reset in
636 if (!reset_ts && ts >= 0xffff0000)
640 st_lsm6dsx_push_tagged_data(hw, tag, iio_buff,
646 if (unlikely(reset_ts)) {
647 err = st_lsm6dsx_reset_hw_ts(hw);
654 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
658 if (!hw->settings->fifo_ops.read_fifo)
661 mutex_lock(&hw->fifo_lock);
663 hw->settings->fifo_ops.read_fifo(hw);
664 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
666 mutex_unlock(&hw->fifo_lock);
672 st_lsm6dsx_update_samples_to_discard(struct st_lsm6dsx_sensor *sensor)
674 const struct st_lsm6dsx_samples_to_discard *data;
675 struct st_lsm6dsx_hw *hw = sensor->hw;
678 if (sensor->id != ST_LSM6DSX_ID_GYRO &&
679 sensor->id != ST_LSM6DSX_ID_ACC)
682 /* check if drdy mask is supported in hw */
683 if (hw->settings->drdy_mask.addr)
686 data = &hw->settings->samples_to_discard[sensor->id];
687 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++) {
688 if (data->val[i].milli_hz == sensor->odr) {
689 sensor->samples_to_discard = data->val[i].samples;
695 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable)
697 struct st_lsm6dsx_hw *hw = sensor->hw;
701 mutex_lock(&hw->conf_lock);
704 fifo_mask = hw->fifo_mask | BIT(sensor->id);
706 fifo_mask = hw->fifo_mask & ~BIT(sensor->id);
709 err = st_lsm6dsx_flush_fifo(hw);
715 st_lsm6dsx_update_samples_to_discard(sensor);
717 err = st_lsm6dsx_device_set_enable(sensor, enable);
721 err = st_lsm6dsx_set_fifo_odr(sensor, enable);
725 err = st_lsm6dsx_update_decimators(hw);
729 err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
734 err = st_lsm6dsx_resume_fifo(hw);
739 hw->fifo_mask = fifo_mask;
742 mutex_unlock(&hw->conf_lock);
747 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
749 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
750 struct st_lsm6dsx_hw *hw = sensor->hw;
752 if (!hw->settings->fifo_ops.update_fifo)
755 return hw->settings->fifo_ops.update_fifo(sensor, true);
758 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
760 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
761 struct st_lsm6dsx_hw *hw = sensor->hw;
763 if (!hw->settings->fifo_ops.update_fifo)
766 return hw->settings->fifo_ops.update_fifo(sensor, false);
769 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
770 .preenable = st_lsm6dsx_buffer_preenable,
771 .postdisable = st_lsm6dsx_buffer_postdisable,
774 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
778 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
779 if (!hw->iio_devs[i])
782 ret = devm_iio_kfifo_buffer_setup(hw->dev, hw->iio_devs[i],
783 &st_lsm6dsx_buffer_ops);