treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / imu / st_lsm6dsx / st_lsm6dsx_buffer.c
blobbb899345f2bbace1ff7655d246af9fc573de5e62
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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 {
56 u8 decimator;
57 u8 val;
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,
69 static const
70 struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = {
71 { 0, 0x0 },
72 { 1, 0x1 },
73 { 2, 0x2 },
74 { 3, 0x3 },
75 { 4, 0x4 },
76 { 8, 0x5 },
77 { 16, 0x6 },
78 { 32, 0x7 },
81 static int
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;
86 int i;
88 if (decimator > 1)
89 decimator = round_down(decimator, 2);
91 for (i = 0; i < max_size; i++) {
92 if (st_lsm6dsx_decimator_table[i].decimator == decimator)
93 break;
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;
103 int i;
105 *max_odr = 0, *min_odr = ~0;
106 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
107 if (!hw->iio_devs[i])
108 continue;
110 sensor = iio_priv(hw->iio_devs[i]);
112 if (!(hw->enable_mask & BIT(sensor->id)))
113 continue;
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;
133 int err = 0, i;
134 u8 data;
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])
142 continue;
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);
149 } else {
150 sensor->sip = 0;
151 data = 0;
153 ts_sip = max_t(u16, ts_sip, sensor->sip);
155 dec_reg = &hw->settings->decimator[sensor->id];
156 if (dec_reg->addr) {
157 int val = ST_LSM6DSX_SHIFT_VAL(data, dec_reg->mask);
159 err = st_lsm6dsx_update_bits_locked(hw, dec_reg->addr,
160 dec_reg->mask,
161 val);
162 if (err < 0)
163 return err;
165 sip += sensor->sip;
167 hw->sip = sip + ts_sip;
168 hw->ts_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
173 * sample in FIFO
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);
183 return err;
186 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
187 enum st_lsm6dsx_fifo_mode fifo_mode)
189 unsigned int data;
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,
197 bool enable)
199 struct st_lsm6dsx_hw *hw = sensor->hw;
200 const struct st_lsm6dsx_reg *batch_reg;
201 u8 data;
203 batch_reg = &hw->settings->batch[sensor->id];
204 if (batch_reg->addr) {
205 int val;
207 if (enable) {
208 int err;
210 err = st_lsm6dsx_check_odr(sensor, sensor->odr,
211 &data);
212 if (err < 0)
213 return err;
214 } else {
215 data = 0;
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);
220 } else {
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,
226 data));
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;
235 int i, err, data;
236 __le16 wdata;
238 if (!hw->sip)
239 return 0;
241 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
242 if (!hw->iio_devs[i])
243 continue;
245 cur_sensor = iio_priv(hw->iio_devs[i]);
247 if (!(hw->enable_mask & BIT(cur_sensor->id)))
248 continue;
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,
262 &data);
263 if (err < 0)
264 goto out;
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));
274 out:
275 mutex_unlock(&hw->page_lock);
276 return err;
279 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw *hw)
281 struct st_lsm6dsx_sensor *sensor;
282 int i, err;
284 /* reset hw ts counter */
285 err = st_lsm6dsx_write_locked(hw, ST_LSM6DSX_REG_TS_RESET_ADDR,
286 ST_LSM6DSX_TS_RESET_VAL);
287 if (err < 0)
288 return err;
290 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
291 if (!hw->iio_devs[i])
292 continue;
294 sensor = iio_priv(hw->iio_devs[i]);
296 * store enable buffer timestamp as reference for
297 * hw timestamp
299 sensor->ts_ref = iio_get_time_ns(hw->iio_devs[i]);
301 return 0;
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;
313 int err;
315 while (read_len < data_len) {
316 word_len = min_t(unsigned int, data_len - read_len,
317 max_word_len);
318 err = st_lsm6dsx_read_locked(hw, addr, data + read_len,
319 word_len);
320 if (err < 0)
321 return err;
322 read_len += word_len;
324 return 0;
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;
347 __le16 fifo_status;
348 s64 ts = 0;
350 err = st_lsm6dsx_read_locked(hw,
351 hw->settings->fifo_ops.fifo_diff.addr,
352 &fifo_status, sizeof(fifo_status));
353 if (err < 0) {
354 dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
355 err);
356 return err;
359 if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
360 return 0;
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);
375 if (err < 0) {
376 dev_err(hw->dev,
377 "failed to read pattern from fifo (err=%d)\n",
378 err);
379 return err;
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
388 * sensor.
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;
400 ts_sip = hw->ts_sip;
401 offset = 0;
403 while (acc_sip > 0 || gyro_sip > 0 || ext_sip > 0) {
404 if (gyro_sip > 0) {
405 memcpy(gyro_buff, &hw->buff[offset],
406 ST_LSM6DSX_SAMPLE_SIZE);
407 offset += ST_LSM6DSX_SAMPLE_SIZE;
409 if (acc_sip > 0) {
410 memcpy(acc_buff, &hw->buff[offset],
411 ST_LSM6DSX_SAMPLE_SIZE);
412 offset += ST_LSM6DSX_SAMPLE_SIZE;
414 if (ext_sip > 0) {
415 memcpy(ext_buff, &hw->buff[offset],
416 ST_LSM6DSX_SAMPLE_SIZE);
417 offset += ST_LSM6DSX_SAMPLE_SIZE;
420 if (ts_sip-- > 0) {
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
435 * 1.638s)
437 if (!reset_ts && ts >= 0xff0000)
438 reset_ts = true;
439 ts *= hw->ts_gain;
441 offset += ST_LSM6DSX_SAMPLE_SIZE;
444 if (gyro_sip-- > 0)
445 iio_push_to_buffers_with_timestamp(
446 hw->iio_devs[ST_LSM6DSX_ID_GYRO],
447 gyro_buff, gyro_sensor->ts_ref + ts);
448 if (acc_sip-- > 0)
449 iio_push_to_buffers_with_timestamp(
450 hw->iio_devs[ST_LSM6DSX_ID_ACC],
451 acc_buff, acc_sensor->ts_ref + ts);
452 if (ext_sip-- > 0)
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);
461 if (err < 0) {
462 dev_err(hw->dev, "failed to reset hw ts (err=%d)\n",
463 err);
464 return err;
467 return read_len;
470 #define ST_LSM6DSX_INVALID_SAMPLE 0x7ffd
471 static int
472 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
473 u8 *data, s64 ts)
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)
481 return -EINVAL;
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
487 * channel
489 switch (tag) {
490 case ST_LSM6DSX_GYRO_TAG:
491 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_GYRO];
492 break;
493 case ST_LSM6DSX_ACC_TAG:
494 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_ACC];
495 break;
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];
501 else
502 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
503 break;
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];
508 else
509 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
510 break;
511 case ST_LSM6DSX_EXT2_TAG:
512 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
513 break;
514 default:
515 return -EINVAL;
518 sensor = iio_priv(iio_dev);
519 iio_push_to_buffers_with_timestamp(iio_dev, data,
520 ts + sensor->ts_ref);
522 return 0;
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;
540 __le16 fifo_status;
541 s64 ts = 0;
543 err = st_lsm6dsx_read_locked(hw,
544 hw->settings->fifo_ops.fifo_diff.addr,
545 &fifo_status, sizeof(fifo_status));
546 if (err < 0) {
547 dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
548 err);
549 return err;
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;
555 if (!fifo_len)
556 return 0;
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);
563 if (err < 0) {
564 dev_err(hw->dev,
565 "failed to read pattern from fifo (err=%d)\n",
566 err);
567 return err;
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],
581 * B3 = ts[31:24]
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
588 * 1.638s)
590 if (!reset_ts && ts >= 0xffff0000)
591 reset_ts = true;
592 ts *= hw->ts_gain;
593 } else {
594 st_lsm6dsx_push_tagged_data(hw, tag, iio_buff,
595 ts);
600 if (unlikely(reset_ts)) {
601 err = st_lsm6dsx_reset_hw_ts(hw);
602 if (err < 0)
603 return err;
605 return read_len;
608 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
610 int err;
612 if (!hw->settings->fifo_ops.read_fifo)
613 return -ENOTSUPP;
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);
622 return err;
625 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable)
627 struct st_lsm6dsx_hw *hw = sensor->hw;
628 u8 fifo_mask;
629 int err;
631 mutex_lock(&hw->conf_lock);
633 if (enable)
634 fifo_mask = hw->fifo_mask | BIT(sensor->id);
635 else
636 fifo_mask = hw->fifo_mask & ~BIT(sensor->id);
638 if (hw->fifo_mask) {
639 err = st_lsm6dsx_flush_fifo(hw);
640 if (err < 0)
641 goto out;
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);
648 if (err < 0)
649 goto out;
650 } else {
651 err = st_lsm6dsx_sensor_set_enable(sensor, enable);
652 if (err < 0)
653 goto out;
656 err = st_lsm6dsx_set_fifo_odr(sensor, enable);
657 if (err < 0)
658 goto out;
660 err = st_lsm6dsx_update_decimators(hw);
661 if (err < 0)
662 goto out;
664 err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
665 if (err < 0)
666 goto out;
668 if (fifo_mask) {
669 /* reset hw ts counter */
670 err = st_lsm6dsx_reset_hw_ts(hw);
671 if (err < 0)
672 goto out;
674 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
675 if (err < 0)
676 goto out;
679 hw->fifo_mask = fifo_mask;
681 out:
682 mutex_unlock(&hw->conf_lock);
684 return err;
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)
693 return -ENOTSUPP;
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)
704 return -ENOTSUPP;
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;
717 int i;
719 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
720 if (!hw->iio_devs[i])
721 continue;
723 buffer = devm_iio_kfifo_allocate(hw->dev);
724 if (!buffer)
725 return -ENOMEM;
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;
732 return 0;