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/LSM6DSR/LSM6DSRX/ISM330DHCX:
18 * The FIFO buffer can be configured to store data from gyroscope and
19 * accelerometer. Each sample is queued with a tag (1B) indicating data
20 * source (gyroscope, accelerometer, hw timer).
22 * FIFO supported modes:
23 * - BYPASS: FIFO disabled
24 * - CONTINUOUS: FIFO enabled. When the buffer is full, the FIFO index
25 * restarts from the beginning and the oldest sample is overwritten
27 * Copyright 2016 STMicroelectronics Inc.
29 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
30 * Denis Ciocca <denis.ciocca@st.com>
32 #include <linux/module.h>
33 #include <linux/iio/kfifo_buf.h>
34 #include <linux/iio/iio.h>
35 #include <linux/iio/buffer.h>
36 #include <linux/regmap.h>
37 #include <linux/bitfield.h>
39 #include <linux/platform_data/st_sensors_pdata.h>
41 #include "st_lsm6dsx.h"
43 #define ST_LSM6DSX_REG_FIFO_MODE_ADDR 0x0a
44 #define ST_LSM6DSX_FIFO_MODE_MASK GENMASK(2, 0)
45 #define ST_LSM6DSX_FIFO_ODR_MASK GENMASK(6, 3)
46 #define ST_LSM6DSX_FIFO_EMPTY_MASK BIT(12)
47 #define ST_LSM6DSX_REG_FIFO_OUTL_ADDR 0x3e
48 #define ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR 0x78
49 #define ST_LSM6DSX_REG_TS_RESET_ADDR 0x42
51 #define ST_LSM6DSX_MAX_FIFO_ODR_VAL 0x08
53 #define ST_LSM6DSX_TS_RESET_VAL 0xaa
55 struct st_lsm6dsx_decimator_entry
{
60 enum st_lsm6dsx_fifo_tag
{
61 ST_LSM6DSX_GYRO_TAG
= 0x01,
62 ST_LSM6DSX_ACC_TAG
= 0x02,
63 ST_LSM6DSX_TS_TAG
= 0x04,
64 ST_LSM6DSX_EXT0_TAG
= 0x0f,
65 ST_LSM6DSX_EXT1_TAG
= 0x10,
66 ST_LSM6DSX_EXT2_TAG
= 0x11,
70 struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table
[] = {
82 st_lsm6dsx_get_decimator_val(struct st_lsm6dsx_sensor
*sensor
, u32 max_odr
)
84 const int max_size
= ARRAY_SIZE(st_lsm6dsx_decimator_table
);
85 u32 decimator
= max_odr
/ sensor
->odr
;
89 decimator
= round_down(decimator
, 2);
91 for (i
= 0; i
< max_size
; i
++) {
92 if (st_lsm6dsx_decimator_table
[i
].decimator
== decimator
)
96 return i
== max_size
? 0 : st_lsm6dsx_decimator_table
[i
].val
;
99 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw
*hw
,
100 u32
*max_odr
, u32
*min_odr
)
102 struct st_lsm6dsx_sensor
*sensor
;
105 *max_odr
= 0, *min_odr
= ~0;
106 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
107 if (!hw
->iio_devs
[i
])
110 sensor
= iio_priv(hw
->iio_devs
[i
]);
112 if (!(hw
->enable_mask
& BIT(sensor
->id
)))
115 *max_odr
= max_t(u32
, *max_odr
, sensor
->odr
);
116 *min_odr
= min_t(u32
, *min_odr
, sensor
->odr
);
120 static u8
st_lsm6dsx_get_sip(struct st_lsm6dsx_sensor
*sensor
, u32 min_odr
)
122 u8 sip
= sensor
->odr
/ min_odr
;
124 return sip
> 1 ? round_down(sip
, 2) : sip
;
127 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw
*hw
)
129 const struct st_lsm6dsx_reg
*ts_dec_reg
;
130 struct st_lsm6dsx_sensor
*sensor
;
131 u16 sip
= 0, ts_sip
= 0;
132 u32 max_odr
, min_odr
;
136 st_lsm6dsx_get_max_min_odr(hw
, &max_odr
, &min_odr
);
138 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
139 const struct st_lsm6dsx_reg
*dec_reg
;
141 if (!hw
->iio_devs
[i
])
144 sensor
= iio_priv(hw
->iio_devs
[i
]);
145 /* update fifo decimators and sample in pattern */
146 if (hw
->enable_mask
& BIT(sensor
->id
)) {
147 sensor
->sip
= st_lsm6dsx_get_sip(sensor
, min_odr
);
148 data
= st_lsm6dsx_get_decimator_val(sensor
, max_odr
);
153 ts_sip
= max_t(u16
, ts_sip
, sensor
->sip
);
155 dec_reg
= &hw
->settings
->decimator
[sensor
->id
];
157 int val
= ST_LSM6DSX_SHIFT_VAL(data
, dec_reg
->mask
);
159 err
= st_lsm6dsx_update_bits_locked(hw
, dec_reg
->addr
,
167 hw
->sip
= sip
+ ts_sip
;
171 * update hw ts decimator if necessary. Decimator for hw timestamp
172 * is always 1 or 0 in order to have a ts sample for each data
175 ts_dec_reg
= &hw
->settings
->ts_settings
.decimator
;
176 if (ts_dec_reg
->addr
) {
177 int val
, ts_dec
= !!hw
->ts_sip
;
179 val
= ST_LSM6DSX_SHIFT_VAL(ts_dec
, ts_dec_reg
->mask
);
180 err
= st_lsm6dsx_update_bits_locked(hw
, ts_dec_reg
->addr
,
181 ts_dec_reg
->mask
, val
);
186 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw
*hw
,
187 enum st_lsm6dsx_fifo_mode fifo_mode
)
191 data
= FIELD_PREP(ST_LSM6DSX_FIFO_MODE_MASK
, fifo_mode
);
192 return st_lsm6dsx_update_bits_locked(hw
, ST_LSM6DSX_REG_FIFO_MODE_ADDR
,
193 ST_LSM6DSX_FIFO_MODE_MASK
, data
);
196 static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor
*sensor
,
199 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
200 const struct st_lsm6dsx_reg
*batch_reg
;
203 batch_reg
= &hw
->settings
->batch
[sensor
->id
];
204 if (batch_reg
->addr
) {
210 err
= st_lsm6dsx_check_odr(sensor
, sensor
->odr
,
217 val
= ST_LSM6DSX_SHIFT_VAL(data
, batch_reg
->mask
);
218 return st_lsm6dsx_update_bits_locked(hw
, batch_reg
->addr
,
219 batch_reg
->mask
, val
);
221 data
= hw
->enable_mask
? ST_LSM6DSX_MAX_FIFO_ODR_VAL
: 0;
222 return st_lsm6dsx_update_bits_locked(hw
,
223 ST_LSM6DSX_REG_FIFO_MODE_ADDR
,
224 ST_LSM6DSX_FIFO_ODR_MASK
,
225 FIELD_PREP(ST_LSM6DSX_FIFO_ODR_MASK
,
230 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor
*sensor
, u16 watermark
)
232 u16 fifo_watermark
= ~0, cur_watermark
, fifo_th_mask
;
233 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
234 struct st_lsm6dsx_sensor
*cur_sensor
;
241 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
242 if (!hw
->iio_devs
[i
])
245 cur_sensor
= iio_priv(hw
->iio_devs
[i
]);
247 if (!(hw
->enable_mask
& BIT(cur_sensor
->id
)))
250 cur_watermark
= (cur_sensor
== sensor
) ? watermark
251 : cur_sensor
->watermark
;
253 fifo_watermark
= min_t(u16
, fifo_watermark
, cur_watermark
);
256 fifo_watermark
= max_t(u16
, fifo_watermark
, hw
->sip
);
257 fifo_watermark
= (fifo_watermark
/ hw
->sip
) * hw
->sip
;
258 fifo_watermark
= fifo_watermark
* hw
->settings
->fifo_ops
.th_wl
;
260 mutex_lock(&hw
->page_lock
);
261 err
= regmap_read(hw
->regmap
, hw
->settings
->fifo_ops
.fifo_th
.addr
+ 1,
266 fifo_th_mask
= hw
->settings
->fifo_ops
.fifo_th
.mask
;
267 fifo_watermark
= ((data
<< 8) & ~fifo_th_mask
) |
268 (fifo_watermark
& fifo_th_mask
);
270 wdata
= cpu_to_le16(fifo_watermark
);
271 err
= regmap_bulk_write(hw
->regmap
,
272 hw
->settings
->fifo_ops
.fifo_th
.addr
,
273 &wdata
, sizeof(wdata
));
275 mutex_unlock(&hw
->page_lock
);
279 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw
*hw
)
281 struct st_lsm6dsx_sensor
*sensor
;
284 /* reset hw ts counter */
285 err
= st_lsm6dsx_write_locked(hw
, ST_LSM6DSX_REG_TS_RESET_ADDR
,
286 ST_LSM6DSX_TS_RESET_VAL
);
290 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
291 if (!hw
->iio_devs
[i
])
294 sensor
= iio_priv(hw
->iio_devs
[i
]);
296 * store enable buffer timestamp as reference for
299 sensor
->ts_ref
= iio_get_time_ns(hw
->iio_devs
[i
]);
305 * Set max bulk read to ST_LSM6DSX_MAX_WORD_LEN/ST_LSM6DSX_MAX_TAGGED_WORD_LEN
306 * in order to avoid a kmalloc for each bus access
308 static inline int st_lsm6dsx_read_block(struct st_lsm6dsx_hw
*hw
, u8 addr
,
309 u8
*data
, unsigned int data_len
,
310 unsigned int max_word_len
)
312 unsigned int word_len
, read_len
= 0;
315 while (read_len
< data_len
) {
316 word_len
= min_t(unsigned int, data_len
- read_len
,
318 err
= st_lsm6dsx_read_locked(hw
, addr
, data
+ read_len
,
322 read_len
+= word_len
;
327 #define ST_LSM6DSX_IIO_BUFF_SIZE (ALIGN(ST_LSM6DSX_SAMPLE_SIZE, \
328 sizeof(s64)) + sizeof(s64))
330 * st_lsm6dsx_read_fifo() - hw FIFO read routine
331 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
333 * Read samples from the hw FIFO and push them to IIO buffers.
335 * Return: Number of bytes read from the FIFO
337 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw
*hw
)
339 struct st_lsm6dsx_sensor
*acc_sensor
, *gyro_sensor
, *ext_sensor
= NULL
;
340 int err
, acc_sip
, gyro_sip
, ts_sip
, ext_sip
, read_len
, offset
;
341 u16 fifo_len
, pattern_len
= hw
->sip
* ST_LSM6DSX_SAMPLE_SIZE
;
342 u16 fifo_diff_mask
= hw
->settings
->fifo_ops
.fifo_diff
.mask
;
343 u8 gyro_buff
[ST_LSM6DSX_IIO_BUFF_SIZE
];
344 u8 acc_buff
[ST_LSM6DSX_IIO_BUFF_SIZE
];
345 u8 ext_buff
[ST_LSM6DSX_IIO_BUFF_SIZE
];
346 bool reset_ts
= false;
350 err
= st_lsm6dsx_read_locked(hw
,
351 hw
->settings
->fifo_ops
.fifo_diff
.addr
,
352 &fifo_status
, sizeof(fifo_status
));
354 dev_err(hw
->dev
, "failed to read fifo status (err=%d)\n",
359 if (fifo_status
& cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK
))
362 fifo_len
= (le16_to_cpu(fifo_status
) & fifo_diff_mask
) *
363 ST_LSM6DSX_CHAN_SIZE
;
364 fifo_len
= (fifo_len
/ pattern_len
) * pattern_len
;
366 acc_sensor
= iio_priv(hw
->iio_devs
[ST_LSM6DSX_ID_ACC
]);
367 gyro_sensor
= iio_priv(hw
->iio_devs
[ST_LSM6DSX_ID_GYRO
]);
368 if (hw
->iio_devs
[ST_LSM6DSX_ID_EXT0
])
369 ext_sensor
= iio_priv(hw
->iio_devs
[ST_LSM6DSX_ID_EXT0
]);
371 for (read_len
= 0; read_len
< fifo_len
; read_len
+= pattern_len
) {
372 err
= st_lsm6dsx_read_block(hw
, ST_LSM6DSX_REG_FIFO_OUTL_ADDR
,
373 hw
->buff
, pattern_len
,
374 ST_LSM6DSX_MAX_WORD_LEN
);
377 "failed to read pattern from fifo (err=%d)\n",
383 * Data are written to the FIFO with a specific pattern
384 * depending on the configured ODRs. The first sequence of data
385 * stored in FIFO contains the data of all enabled sensors
386 * (e.g. Gx, Gy, Gz, Ax, Ay, Az, Ts), then data are repeated
387 * depending on the value of the decimation factor set for each
390 * Supposing the FIFO is storing data from gyroscope and
391 * accelerometer at different ODRs:
392 * - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
393 * Since the gyroscope ODR is twice the accelerometer one, the
394 * following pattern is repeated every 9 samples:
395 * - Gx, Gy, Gz, Ax, Ay, Az, Ts, Gx, Gy, Gz, Ts, Gx, ..
397 ext_sip
= ext_sensor
? ext_sensor
->sip
: 0;
398 gyro_sip
= gyro_sensor
->sip
;
399 acc_sip
= acc_sensor
->sip
;
403 while (acc_sip
> 0 || gyro_sip
> 0 || ext_sip
> 0) {
405 memcpy(gyro_buff
, &hw
->buff
[offset
],
406 ST_LSM6DSX_SAMPLE_SIZE
);
407 offset
+= ST_LSM6DSX_SAMPLE_SIZE
;
410 memcpy(acc_buff
, &hw
->buff
[offset
],
411 ST_LSM6DSX_SAMPLE_SIZE
);
412 offset
+= ST_LSM6DSX_SAMPLE_SIZE
;
415 memcpy(ext_buff
, &hw
->buff
[offset
],
416 ST_LSM6DSX_SAMPLE_SIZE
);
417 offset
+= ST_LSM6DSX_SAMPLE_SIZE
;
421 u8 data
[ST_LSM6DSX_SAMPLE_SIZE
];
423 memcpy(data
, &hw
->buff
[offset
], sizeof(data
));
425 * hw timestamp is 3B long and it is stored
426 * in FIFO using 6B as 4th FIFO data set
427 * according to this schema:
428 * B0 = ts[15:8], B1 = ts[23:16], B3 = ts[7:0]
430 ts
= data
[1] << 16 | data
[0] << 8 | data
[3];
432 * check if hw timestamp engine is going to
433 * reset (the sensor generates an interrupt
434 * to signal the hw timestamp will reset in
437 if (!reset_ts
&& ts
>= 0xff0000)
441 offset
+= ST_LSM6DSX_SAMPLE_SIZE
;
445 iio_push_to_buffers_with_timestamp(
446 hw
->iio_devs
[ST_LSM6DSX_ID_GYRO
],
447 gyro_buff
, gyro_sensor
->ts_ref
+ ts
);
449 iio_push_to_buffers_with_timestamp(
450 hw
->iio_devs
[ST_LSM6DSX_ID_ACC
],
451 acc_buff
, acc_sensor
->ts_ref
+ ts
);
453 iio_push_to_buffers_with_timestamp(
454 hw
->iio_devs
[ST_LSM6DSX_ID_EXT0
],
455 ext_buff
, ext_sensor
->ts_ref
+ ts
);
459 if (unlikely(reset_ts
)) {
460 err
= st_lsm6dsx_reset_hw_ts(hw
);
462 dev_err(hw
->dev
, "failed to reset hw ts (err=%d)\n",
470 #define ST_LSM6DSX_INVALID_SAMPLE 0x7ffd
472 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw
*hw
, u8 tag
,
475 s16 val
= le16_to_cpu(*(__le16
*)data
);
476 struct st_lsm6dsx_sensor
*sensor
;
477 struct iio_dev
*iio_dev
;
479 /* invalid sample during bootstrap phase */
480 if (val
>= ST_LSM6DSX_INVALID_SAMPLE
)
484 * EXT_TAG are managed in FIFO fashion so ST_LSM6DSX_EXT0_TAG
485 * corresponds to the first enabled channel, ST_LSM6DSX_EXT1_TAG
486 * to the second one and ST_LSM6DSX_EXT2_TAG to the last enabled
490 case ST_LSM6DSX_GYRO_TAG
:
491 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_GYRO
];
493 case ST_LSM6DSX_ACC_TAG
:
494 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_ACC
];
496 case ST_LSM6DSX_EXT0_TAG
:
497 if (hw
->enable_mask
& BIT(ST_LSM6DSX_ID_EXT0
))
498 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_EXT0
];
499 else if (hw
->enable_mask
& BIT(ST_LSM6DSX_ID_EXT1
))
500 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_EXT1
];
502 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_EXT2
];
504 case ST_LSM6DSX_EXT1_TAG
:
505 if ((hw
->enable_mask
& BIT(ST_LSM6DSX_ID_EXT0
)) &&
506 (hw
->enable_mask
& BIT(ST_LSM6DSX_ID_EXT1
)))
507 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_EXT1
];
509 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_EXT2
];
511 case ST_LSM6DSX_EXT2_TAG
:
512 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_EXT2
];
518 sensor
= iio_priv(iio_dev
);
519 iio_push_to_buffers_with_timestamp(iio_dev
, data
,
520 ts
+ sensor
->ts_ref
);
526 * st_lsm6dsx_read_tagged_fifo() - tagged hw FIFO read routine
527 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
529 * Read samples from the hw FIFO and push them to IIO buffers.
531 * Return: Number of bytes read from the FIFO
533 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw
*hw
)
535 u16 pattern_len
= hw
->sip
* ST_LSM6DSX_TAGGED_SAMPLE_SIZE
;
536 u16 fifo_len
, fifo_diff_mask
;
537 u8 iio_buff
[ST_LSM6DSX_IIO_BUFF_SIZE
], tag
;
538 bool reset_ts
= false;
539 int i
, err
, read_len
;
543 err
= st_lsm6dsx_read_locked(hw
,
544 hw
->settings
->fifo_ops
.fifo_diff
.addr
,
545 &fifo_status
, sizeof(fifo_status
));
547 dev_err(hw
->dev
, "failed to read fifo status (err=%d)\n",
552 fifo_diff_mask
= hw
->settings
->fifo_ops
.fifo_diff
.mask
;
553 fifo_len
= (le16_to_cpu(fifo_status
) & fifo_diff_mask
) *
554 ST_LSM6DSX_TAGGED_SAMPLE_SIZE
;
558 for (read_len
= 0; read_len
< fifo_len
; read_len
+= pattern_len
) {
559 err
= st_lsm6dsx_read_block(hw
,
560 ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR
,
561 hw
->buff
, pattern_len
,
562 ST_LSM6DSX_MAX_TAGGED_WORD_LEN
);
565 "failed to read pattern from fifo (err=%d)\n",
570 for (i
= 0; i
< pattern_len
;
571 i
+= ST_LSM6DSX_TAGGED_SAMPLE_SIZE
) {
572 memcpy(iio_buff
, &hw
->buff
[i
+ ST_LSM6DSX_TAG_SIZE
],
573 ST_LSM6DSX_SAMPLE_SIZE
);
575 tag
= hw
->buff
[i
] >> 3;
576 if (tag
== ST_LSM6DSX_TS_TAG
) {
578 * hw timestamp is 4B long and it is stored
579 * in FIFO according to this schema:
580 * B0 = ts[7:0], B1 = ts[15:8], B2 = ts[23:16],
583 ts
= le32_to_cpu(*((__le32
*)iio_buff
));
585 * check if hw timestamp engine is going to
586 * reset (the sensor generates an interrupt
587 * to signal the hw timestamp will reset in
590 if (!reset_ts
&& ts
>= 0xffff0000)
594 st_lsm6dsx_push_tagged_data(hw
, tag
, iio_buff
,
600 if (unlikely(reset_ts
)) {
601 err
= st_lsm6dsx_reset_hw_ts(hw
);
608 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw
*hw
)
612 if (!hw
->settings
->fifo_ops
.read_fifo
)
615 mutex_lock(&hw
->fifo_lock
);
617 hw
->settings
->fifo_ops
.read_fifo(hw
);
618 err
= st_lsm6dsx_set_fifo_mode(hw
, ST_LSM6DSX_FIFO_BYPASS
);
620 mutex_unlock(&hw
->fifo_lock
);
625 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor
*sensor
, bool enable
)
627 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
631 mutex_lock(&hw
->conf_lock
);
634 fifo_mask
= hw
->fifo_mask
| BIT(sensor
->id
);
636 fifo_mask
= hw
->fifo_mask
& ~BIT(sensor
->id
);
639 err
= st_lsm6dsx_flush_fifo(hw
);
644 if (sensor
->id
== ST_LSM6DSX_ID_EXT0
||
645 sensor
->id
== ST_LSM6DSX_ID_EXT1
||
646 sensor
->id
== ST_LSM6DSX_ID_EXT2
) {
647 err
= st_lsm6dsx_shub_set_enable(sensor
, enable
);
651 err
= st_lsm6dsx_sensor_set_enable(sensor
, enable
);
656 err
= st_lsm6dsx_set_fifo_odr(sensor
, enable
);
660 err
= st_lsm6dsx_update_decimators(hw
);
664 err
= st_lsm6dsx_update_watermark(sensor
, sensor
->watermark
);
669 /* reset hw ts counter */
670 err
= st_lsm6dsx_reset_hw_ts(hw
);
674 err
= st_lsm6dsx_set_fifo_mode(hw
, ST_LSM6DSX_FIFO_CONT
);
679 hw
->fifo_mask
= fifo_mask
;
682 mutex_unlock(&hw
->conf_lock
);
687 static int st_lsm6dsx_buffer_preenable(struct iio_dev
*iio_dev
)
689 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
690 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
692 if (!hw
->settings
->fifo_ops
.update_fifo
)
695 return hw
->settings
->fifo_ops
.update_fifo(sensor
, true);
698 static int st_lsm6dsx_buffer_postdisable(struct iio_dev
*iio_dev
)
700 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
701 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
703 if (!hw
->settings
->fifo_ops
.update_fifo
)
706 return hw
->settings
->fifo_ops
.update_fifo(sensor
, false);
709 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops
= {
710 .preenable
= st_lsm6dsx_buffer_preenable
,
711 .postdisable
= st_lsm6dsx_buffer_postdisable
,
714 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw
*hw
)
716 struct iio_buffer
*buffer
;
719 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
720 if (!hw
->iio_devs
[i
])
723 buffer
= devm_iio_kfifo_allocate(hw
->dev
);
727 iio_device_attach_buffer(hw
->iio_devs
[i
], buffer
);
728 hw
->iio_devs
[i
]->modes
|= INDIO_BUFFER_SOFTWARE
;
729 hw
->iio_devs
[i
]->setup_ops
= &st_lsm6dsx_buffer_ops
;