drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / iio / imu / st_lsm6dsx / st_lsm6dsx_buffer.c
blob0a7cd8c1aa3313a113b9092d6a58d597ba68290c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * STMicroelectronics st_lsm6dsx FIFO buffer library driver
5 * Pattern FIFO:
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 * Supported devices:
18 * - ISM330DLC
19 * - LSM6DS3
20 * - LSM6DS3H
21 * - LSM6DS3TR-C
22 * - LSM6DSL
23 * - LSM6DSM
25 * Tagged FIFO:
26 * The FIFO buffer can be configured to store data from gyroscope and
27 * accelerometer. Each sample is queued with a tag (1B) indicating data
28 * source (gyroscope, accelerometer, hw timer).
30 * Supported devices:
31 * - ASM330LHB
32 * - ASM330LHH
33 * - ASM330LHHX
34 * - ASM330LHHXG1
35 * - ISM330DHCX
36 * - LSM6DSO
37 * - LSM6DSOP
38 * - LSM6DSOX
39 * - LSM6DSR
40 * - LSM6DSRX
41 * - LSM6DST
42 * - LSM6DSTX
43 * - LSM6DSV
45 * FIFO supported modes:
46 * - BYPASS: FIFO disabled
47 * - CONTINUOUS: FIFO enabled. When the buffer is full, the FIFO index
48 * restarts from the beginning and the oldest sample is overwritten
50 * Copyright 2016 STMicroelectronics Inc.
52 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
53 * Denis Ciocca <denis.ciocca@st.com>
55 #include <linux/module.h>
56 #include <linux/iio/kfifo_buf.h>
57 #include <linux/iio/iio.h>
58 #include <linux/iio/buffer.h>
59 #include <linux/regmap.h>
60 #include <linux/bitfield.h>
62 #include <linux/platform_data/st_sensors_pdata.h>
64 #include "st_lsm6dsx.h"
66 #define ST_LSM6DSX_REG_FIFO_MODE_ADDR 0x0a
67 #define ST_LSM6DSX_FIFO_MODE_MASK GENMASK(2, 0)
68 #define ST_LSM6DSX_FIFO_ODR_MASK GENMASK(6, 3)
69 #define ST_LSM6DSX_FIFO_EMPTY_MASK BIT(12)
70 #define ST_LSM6DSX_REG_FIFO_OUTL_ADDR 0x3e
71 #define ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR 0x78
72 #define ST_LSM6DSX_REG_TS_RESET_ADDR 0x42
74 #define ST_LSM6DSX_MAX_FIFO_ODR_VAL 0x08
76 #define ST_LSM6DSX_TS_RESET_VAL 0xaa
78 struct st_lsm6dsx_decimator_entry {
79 u8 decimator;
80 u8 val;
83 enum st_lsm6dsx_fifo_tag {
84 ST_LSM6DSX_GYRO_TAG = 0x01,
85 ST_LSM6DSX_ACC_TAG = 0x02,
86 ST_LSM6DSX_TS_TAG = 0x04,
87 ST_LSM6DSX_EXT0_TAG = 0x0f,
88 ST_LSM6DSX_EXT1_TAG = 0x10,
89 ST_LSM6DSX_EXT2_TAG = 0x11,
92 static const
93 struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = {
94 { 0, 0x0 },
95 { 1, 0x1 },
96 { 2, 0x2 },
97 { 3, 0x3 },
98 { 4, 0x4 },
99 { 8, 0x5 },
100 { 16, 0x6 },
101 { 32, 0x7 },
104 static int
105 st_lsm6dsx_get_decimator_val(struct st_lsm6dsx_sensor *sensor, u32 max_odr)
107 const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
108 u32 decimator = max_odr / sensor->odr;
109 int i;
111 if (decimator > 1)
112 decimator = round_down(decimator, 2);
114 for (i = 0; i < max_size; i++) {
115 if (st_lsm6dsx_decimator_table[i].decimator == decimator)
116 break;
119 sensor->decimator = decimator;
120 return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
123 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
124 u32 *max_odr, u32 *min_odr)
126 struct st_lsm6dsx_sensor *sensor;
127 int i;
129 *max_odr = 0, *min_odr = ~0;
130 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
131 if (!hw->iio_devs[i])
132 continue;
134 sensor = iio_priv(hw->iio_devs[i]);
136 if (!(hw->enable_mask & BIT(sensor->id)))
137 continue;
139 *max_odr = max_t(u32, *max_odr, sensor->odr);
140 *min_odr = min_t(u32, *min_odr, sensor->odr);
144 static u8 st_lsm6dsx_get_sip(struct st_lsm6dsx_sensor *sensor, u32 min_odr)
146 u8 sip = sensor->odr / min_odr;
148 return sip > 1 ? round_down(sip, 2) : sip;
151 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
153 const struct st_lsm6dsx_reg *ts_dec_reg;
154 struct st_lsm6dsx_sensor *sensor;
155 u16 sip = 0, ts_sip = 0;
156 u32 max_odr, min_odr;
157 int err = 0, i;
158 u8 data;
160 st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr);
162 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
163 const struct st_lsm6dsx_reg *dec_reg;
165 if (!hw->iio_devs[i])
166 continue;
168 sensor = iio_priv(hw->iio_devs[i]);
169 /* update fifo decimators and sample in pattern */
170 if (hw->enable_mask & BIT(sensor->id)) {
171 sensor->sip = st_lsm6dsx_get_sip(sensor, min_odr);
172 data = st_lsm6dsx_get_decimator_val(sensor, max_odr);
173 } else {
174 sensor->sip = 0;
175 data = 0;
177 ts_sip = max_t(u16, ts_sip, sensor->sip);
179 dec_reg = &hw->settings->decimator[sensor->id];
180 if (dec_reg->addr) {
181 int val = ST_LSM6DSX_SHIFT_VAL(data, dec_reg->mask);
183 err = st_lsm6dsx_update_bits_locked(hw, dec_reg->addr,
184 dec_reg->mask,
185 val);
186 if (err < 0)
187 return err;
189 sip += sensor->sip;
191 hw->sip = sip + ts_sip;
192 hw->ts_sip = ts_sip;
195 * update hw ts decimator if necessary. Decimator for hw timestamp
196 * is always 1 or 0 in order to have a ts sample for each data
197 * sample in FIFO
199 ts_dec_reg = &hw->settings->ts_settings.decimator;
200 if (ts_dec_reg->addr) {
201 int val, ts_dec = !!hw->ts_sip;
203 val = ST_LSM6DSX_SHIFT_VAL(ts_dec, ts_dec_reg->mask);
204 err = st_lsm6dsx_update_bits_locked(hw, ts_dec_reg->addr,
205 ts_dec_reg->mask, val);
207 return err;
210 static int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
211 enum st_lsm6dsx_fifo_mode fifo_mode)
213 unsigned int data;
215 data = FIELD_PREP(ST_LSM6DSX_FIFO_MODE_MASK, fifo_mode);
216 return st_lsm6dsx_update_bits_locked(hw, ST_LSM6DSX_REG_FIFO_MODE_ADDR,
217 ST_LSM6DSX_FIFO_MODE_MASK, data);
220 static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor,
221 bool enable)
223 struct st_lsm6dsx_hw *hw = sensor->hw;
224 const struct st_lsm6dsx_reg *batch_reg;
225 u8 data;
227 batch_reg = &hw->settings->batch[sensor->id];
228 if (batch_reg->addr) {
229 int val;
231 if (enable) {
232 int err;
234 err = st_lsm6dsx_check_odr(sensor, sensor->odr,
235 &data);
236 if (err < 0)
237 return err;
238 } else {
239 data = 0;
241 val = ST_LSM6DSX_SHIFT_VAL(data, batch_reg->mask);
242 return st_lsm6dsx_update_bits_locked(hw, batch_reg->addr,
243 batch_reg->mask, val);
244 } else {
245 data = hw->enable_mask ? ST_LSM6DSX_MAX_FIFO_ODR_VAL : 0;
246 return st_lsm6dsx_update_bits_locked(hw,
247 ST_LSM6DSX_REG_FIFO_MODE_ADDR,
248 ST_LSM6DSX_FIFO_ODR_MASK,
249 FIELD_PREP(ST_LSM6DSX_FIFO_ODR_MASK,
250 data));
254 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark)
256 u16 fifo_watermark = ~0, cur_watermark, fifo_th_mask;
257 struct st_lsm6dsx_hw *hw = sensor->hw;
258 struct st_lsm6dsx_sensor *cur_sensor;
259 int i, err, data;
260 __le16 wdata;
262 if (!hw->sip)
263 return 0;
265 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
266 if (!hw->iio_devs[i])
267 continue;
269 cur_sensor = iio_priv(hw->iio_devs[i]);
271 if (!(hw->enable_mask & BIT(cur_sensor->id)))
272 continue;
274 cur_watermark = (cur_sensor == sensor) ? watermark
275 : cur_sensor->watermark;
277 fifo_watermark = min_t(u16, fifo_watermark, cur_watermark);
280 fifo_watermark = max_t(u16, fifo_watermark, hw->sip);
281 fifo_watermark = (fifo_watermark / hw->sip) * hw->sip;
282 fifo_watermark = fifo_watermark * hw->settings->fifo_ops.th_wl;
284 mutex_lock(&hw->page_lock);
285 err = regmap_read(hw->regmap, hw->settings->fifo_ops.fifo_th.addr + 1,
286 &data);
287 if (err < 0)
288 goto out;
290 fifo_th_mask = hw->settings->fifo_ops.fifo_th.mask;
291 fifo_watermark = ((data << 8) & ~fifo_th_mask) |
292 (fifo_watermark & fifo_th_mask);
294 wdata = cpu_to_le16(fifo_watermark);
295 err = regmap_bulk_write(hw->regmap,
296 hw->settings->fifo_ops.fifo_th.addr,
297 &wdata, sizeof(wdata));
298 out:
299 mutex_unlock(&hw->page_lock);
300 return err;
303 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw *hw)
305 struct st_lsm6dsx_sensor *sensor;
306 int i, err;
308 /* reset hw ts counter */
309 err = st_lsm6dsx_write_locked(hw, ST_LSM6DSX_REG_TS_RESET_ADDR,
310 ST_LSM6DSX_TS_RESET_VAL);
311 if (err < 0)
312 return err;
314 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
315 if (!hw->iio_devs[i])
316 continue;
318 sensor = iio_priv(hw->iio_devs[i]);
320 * store enable buffer timestamp as reference for
321 * hw timestamp
323 sensor->ts_ref = iio_get_time_ns(hw->iio_devs[i]);
325 return 0;
328 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw)
330 int err;
332 /* reset hw ts counter */
333 err = st_lsm6dsx_reset_hw_ts(hw);
334 if (err < 0)
335 return err;
337 return st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
341 * Set max bulk read to ST_LSM6DSX_MAX_WORD_LEN/ST_LSM6DSX_MAX_TAGGED_WORD_LEN
342 * in order to avoid a kmalloc for each bus access
344 static inline int st_lsm6dsx_read_block(struct st_lsm6dsx_hw *hw, u8 addr,
345 u8 *data, unsigned int data_len,
346 unsigned int max_word_len)
348 unsigned int word_len, read_len = 0;
349 int err;
351 while (read_len < data_len) {
352 word_len = min_t(unsigned int, data_len - read_len,
353 max_word_len);
354 err = st_lsm6dsx_read_locked(hw, addr, data + read_len,
355 word_len);
356 if (err < 0)
357 return err;
358 read_len += word_len;
360 return 0;
363 #define ST_LSM6DSX_IIO_BUFF_SIZE (ALIGN(ST_LSM6DSX_SAMPLE_SIZE, \
364 sizeof(s64)) + sizeof(s64))
366 * st_lsm6dsx_read_fifo() - hw FIFO read routine
367 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
369 * Read samples from the hw FIFO and push them to IIO buffers.
371 * Return: Number of bytes read from the FIFO
373 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
375 struct st_lsm6dsx_sensor *acc_sensor, *gyro_sensor, *ext_sensor = NULL;
376 int err, sip, acc_sip, gyro_sip, ts_sip, ext_sip, read_len, offset;
377 u16 fifo_len, pattern_len = hw->sip * ST_LSM6DSX_SAMPLE_SIZE;
378 u16 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
379 bool reset_ts = false;
380 __le16 fifo_status;
381 s64 ts = 0;
383 err = st_lsm6dsx_read_locked(hw,
384 hw->settings->fifo_ops.fifo_diff.addr,
385 &fifo_status, sizeof(fifo_status));
386 if (err < 0) {
387 dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
388 err);
389 return err;
392 if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
393 return 0;
395 fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
396 ST_LSM6DSX_CHAN_SIZE;
397 fifo_len = (fifo_len / pattern_len) * pattern_len;
399 acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
400 gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
401 if (hw->iio_devs[ST_LSM6DSX_ID_EXT0])
402 ext_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_EXT0]);
404 for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
405 err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
406 hw->buff, pattern_len,
407 ST_LSM6DSX_MAX_WORD_LEN);
408 if (err < 0) {
409 dev_err(hw->dev,
410 "failed to read pattern from fifo (err=%d)\n",
411 err);
412 return err;
416 * Data are written to the FIFO with a specific pattern
417 * depending on the configured ODRs. The first sequence of data
418 * stored in FIFO contains the data of all enabled sensors
419 * (e.g. Gx, Gy, Gz, Ax, Ay, Az, Ts), then data are repeated
420 * depending on the value of the decimation factor set for each
421 * sensor.
423 * Supposing the FIFO is storing data from gyroscope and
424 * accelerometer at different ODRs:
425 * - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
426 * Since the gyroscope ODR is twice the accelerometer one, the
427 * following pattern is repeated every 9 samples:
428 * - Gx, Gy, Gz, Ax, Ay, Az, Ts, Gx, Gy, Gz, Ts, Gx, ..
430 ext_sip = ext_sensor ? ext_sensor->sip : 0;
431 gyro_sip = gyro_sensor->sip;
432 acc_sip = acc_sensor->sip;
433 ts_sip = hw->ts_sip;
434 offset = 0;
435 sip = 0;
437 while (acc_sip > 0 || gyro_sip > 0 || ext_sip > 0) {
438 if (gyro_sip > 0 && !(sip % gyro_sensor->decimator)) {
439 memcpy(hw->scan[ST_LSM6DSX_ID_GYRO].channels,
440 &hw->buff[offset],
441 sizeof(hw->scan[ST_LSM6DSX_ID_GYRO].channels));
442 offset += sizeof(hw->scan[ST_LSM6DSX_ID_GYRO].channels);
444 if (acc_sip > 0 && !(sip % acc_sensor->decimator)) {
445 memcpy(hw->scan[ST_LSM6DSX_ID_ACC].channels,
446 &hw->buff[offset],
447 sizeof(hw->scan[ST_LSM6DSX_ID_ACC].channels));
448 offset += sizeof(hw->scan[ST_LSM6DSX_ID_ACC].channels);
450 if (ext_sip > 0 && !(sip % ext_sensor->decimator)) {
451 memcpy(hw->scan[ST_LSM6DSX_ID_EXT0].channels,
452 &hw->buff[offset],
453 sizeof(hw->scan[ST_LSM6DSX_ID_EXT0].channels));
454 offset += sizeof(hw->scan[ST_LSM6DSX_ID_EXT0].channels);
457 if (ts_sip-- > 0) {
458 u8 data[ST_LSM6DSX_SAMPLE_SIZE];
460 memcpy(data, &hw->buff[offset], sizeof(data));
462 * hw timestamp is 3B long and it is stored
463 * in FIFO using 6B as 4th FIFO data set
464 * according to this schema:
465 * B0 = ts[15:8], B1 = ts[23:16], B3 = ts[7:0]
467 ts = data[1] << 16 | data[0] << 8 | data[3];
469 * check if hw timestamp engine is going to
470 * reset (the sensor generates an interrupt
471 * to signal the hw timestamp will reset in
472 * 1.638s)
474 if (!reset_ts && ts >= 0xff0000)
475 reset_ts = true;
476 ts *= hw->ts_gain;
478 offset += ST_LSM6DSX_SAMPLE_SIZE;
481 if (gyro_sip > 0 && !(sip % gyro_sensor->decimator)) {
483 * We need to discards gyro samples during
484 * filters settling time
486 if (gyro_sensor->samples_to_discard > 0)
487 gyro_sensor->samples_to_discard--;
488 else
489 iio_push_to_buffers_with_timestamp(
490 hw->iio_devs[ST_LSM6DSX_ID_GYRO],
491 &hw->scan[ST_LSM6DSX_ID_GYRO],
492 gyro_sensor->ts_ref + ts);
493 gyro_sip--;
495 if (acc_sip > 0 && !(sip % acc_sensor->decimator)) {
497 * We need to discards accel samples during
498 * filters settling time
500 if (acc_sensor->samples_to_discard > 0)
501 acc_sensor->samples_to_discard--;
502 else
503 iio_push_to_buffers_with_timestamp(
504 hw->iio_devs[ST_LSM6DSX_ID_ACC],
505 &hw->scan[ST_LSM6DSX_ID_ACC],
506 acc_sensor->ts_ref + ts);
507 acc_sip--;
509 if (ext_sip > 0 && !(sip % ext_sensor->decimator)) {
510 iio_push_to_buffers_with_timestamp(
511 hw->iio_devs[ST_LSM6DSX_ID_EXT0],
512 &hw->scan[ST_LSM6DSX_ID_EXT0],
513 ext_sensor->ts_ref + ts);
514 ext_sip--;
516 sip++;
520 if (unlikely(reset_ts)) {
521 err = st_lsm6dsx_reset_hw_ts(hw);
522 if (err < 0) {
523 dev_err(hw->dev, "failed to reset hw ts (err=%d)\n",
524 err);
525 return err;
528 return read_len;
531 #define ST_LSM6DSX_INVALID_SAMPLE 0x7ffd
532 static int
533 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
534 u8 *data, s64 ts)
536 s16 val = le16_to_cpu(*(__le16 *)data);
537 struct st_lsm6dsx_sensor *sensor;
538 struct iio_dev *iio_dev;
540 /* invalid sample during bootstrap phase */
541 if (val >= ST_LSM6DSX_INVALID_SAMPLE)
542 return -EINVAL;
545 * EXT_TAG are managed in FIFO fashion so ST_LSM6DSX_EXT0_TAG
546 * corresponds to the first enabled channel, ST_LSM6DSX_EXT1_TAG
547 * to the second one and ST_LSM6DSX_EXT2_TAG to the last enabled
548 * channel
550 switch (tag) {
551 case ST_LSM6DSX_GYRO_TAG:
552 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_GYRO];
553 break;
554 case ST_LSM6DSX_ACC_TAG:
555 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_ACC];
556 break;
557 case ST_LSM6DSX_EXT0_TAG:
558 if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0))
559 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT0];
560 else if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1))
561 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
562 else
563 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
564 break;
565 case ST_LSM6DSX_EXT1_TAG:
566 if ((hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0)) &&
567 (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1)))
568 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
569 else
570 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
571 break;
572 case ST_LSM6DSX_EXT2_TAG:
573 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
574 break;
575 default:
576 return -EINVAL;
579 sensor = iio_priv(iio_dev);
580 iio_push_to_buffers_with_timestamp(iio_dev, data,
581 ts + sensor->ts_ref);
583 return 0;
587 * st_lsm6dsx_read_tagged_fifo() - tagged hw FIFO read routine
588 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
590 * Read samples from the hw FIFO and push them to IIO buffers.
592 * Return: Number of bytes read from the FIFO
594 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
596 u16 pattern_len = hw->sip * ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
597 u16 fifo_len, fifo_diff_mask;
599 * Alignment needed as this can ultimately be passed to a
600 * call to iio_push_to_buffers_with_timestamp() which
601 * must be passed a buffer that is aligned to 8 bytes so
602 * as to allow insertion of a naturally aligned timestamp.
604 u8 iio_buff[ST_LSM6DSX_IIO_BUFF_SIZE] __aligned(8);
605 u8 tag;
606 bool reset_ts = false;
607 int i, err, read_len;
608 __le16 fifo_status;
609 s64 ts = 0;
611 err = st_lsm6dsx_read_locked(hw,
612 hw->settings->fifo_ops.fifo_diff.addr,
613 &fifo_status, sizeof(fifo_status));
614 if (err < 0) {
615 dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
616 err);
617 return err;
620 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
621 fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
622 ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
623 if (!fifo_len)
624 return 0;
626 for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
627 err = st_lsm6dsx_read_block(hw,
628 ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
629 hw->buff, pattern_len,
630 ST_LSM6DSX_MAX_TAGGED_WORD_LEN);
631 if (err < 0) {
632 dev_err(hw->dev,
633 "failed to read pattern from fifo (err=%d)\n",
634 err);
635 return err;
638 for (i = 0; i < pattern_len;
639 i += ST_LSM6DSX_TAGGED_SAMPLE_SIZE) {
640 memcpy(iio_buff, &hw->buff[i + ST_LSM6DSX_TAG_SIZE],
641 ST_LSM6DSX_SAMPLE_SIZE);
643 tag = hw->buff[i] >> 3;
644 if (tag == ST_LSM6DSX_TS_TAG) {
646 * hw timestamp is 4B long and it is stored
647 * in FIFO according to this schema:
648 * B0 = ts[7:0], B1 = ts[15:8], B2 = ts[23:16],
649 * B3 = ts[31:24]
651 ts = le32_to_cpu(*((__le32 *)iio_buff));
653 * check if hw timestamp engine is going to
654 * reset (the sensor generates an interrupt
655 * to signal the hw timestamp will reset in
656 * 1.638s)
658 if (!reset_ts && ts >= 0xffff0000)
659 reset_ts = true;
660 ts *= hw->ts_gain;
661 } else {
662 st_lsm6dsx_push_tagged_data(hw, tag, iio_buff,
663 ts);
668 if (unlikely(reset_ts)) {
669 err = st_lsm6dsx_reset_hw_ts(hw);
670 if (err < 0)
671 return err;
673 return read_len;
676 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
678 int err;
680 if (!hw->settings->fifo_ops.read_fifo)
681 return -ENOTSUPP;
683 mutex_lock(&hw->fifo_lock);
685 hw->settings->fifo_ops.read_fifo(hw);
686 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
688 mutex_unlock(&hw->fifo_lock);
690 return err;
693 static void
694 st_lsm6dsx_update_samples_to_discard(struct st_lsm6dsx_sensor *sensor)
696 const struct st_lsm6dsx_samples_to_discard *data;
697 struct st_lsm6dsx_hw *hw = sensor->hw;
698 int i;
700 if (sensor->id != ST_LSM6DSX_ID_GYRO &&
701 sensor->id != ST_LSM6DSX_ID_ACC)
702 return;
704 /* check if drdy mask is supported in hw */
705 if (hw->settings->drdy_mask.addr)
706 return;
708 data = &hw->settings->samples_to_discard[sensor->id];
709 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++) {
710 if (data->val[i].milli_hz == sensor->odr) {
711 sensor->samples_to_discard = data->val[i].samples;
712 return;
717 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable)
719 struct st_lsm6dsx_hw *hw = sensor->hw;
720 u8 fifo_mask;
721 int err;
723 mutex_lock(&hw->conf_lock);
725 if (enable)
726 fifo_mask = hw->fifo_mask | BIT(sensor->id);
727 else
728 fifo_mask = hw->fifo_mask & ~BIT(sensor->id);
730 if (hw->fifo_mask) {
731 err = st_lsm6dsx_flush_fifo(hw);
732 if (err < 0)
733 goto out;
736 if (enable)
737 st_lsm6dsx_update_samples_to_discard(sensor);
739 err = st_lsm6dsx_device_set_enable(sensor, enable);
740 if (err < 0)
741 goto out;
743 err = st_lsm6dsx_set_fifo_odr(sensor, enable);
744 if (err < 0)
745 goto out;
747 err = st_lsm6dsx_update_decimators(hw);
748 if (err < 0)
749 goto out;
751 err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
752 if (err < 0)
753 goto out;
755 if (fifo_mask) {
756 err = st_lsm6dsx_resume_fifo(hw);
757 if (err < 0)
758 goto out;
761 hw->fifo_mask = fifo_mask;
763 out:
764 mutex_unlock(&hw->conf_lock);
766 return err;
769 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
771 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
772 struct st_lsm6dsx_hw *hw = sensor->hw;
774 if (!hw->settings->fifo_ops.update_fifo)
775 return -ENOTSUPP;
777 return hw->settings->fifo_ops.update_fifo(sensor, true);
780 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
782 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
783 struct st_lsm6dsx_hw *hw = sensor->hw;
785 if (!hw->settings->fifo_ops.update_fifo)
786 return -ENOTSUPP;
788 return hw->settings->fifo_ops.update_fifo(sensor, false);
791 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
792 .preenable = st_lsm6dsx_buffer_preenable,
793 .postdisable = st_lsm6dsx_buffer_postdisable,
796 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
798 int i, ret;
800 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
801 if (!hw->iio_devs[i])
802 continue;
804 ret = devm_iio_kfifo_buffer_setup(hw->dev, hw->iio_devs[i],
805 &st_lsm6dsx_buffer_ops);
806 if (ret)
807 return ret;
810 return 0;