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 u16 fifo_len, pattern_len = hw->sip * ST_LSM6DSX_SAMPLE_SIZE;
340 u16 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
341 int err, acc_sip, gyro_sip, ts_sip, read_len, offset;
342 struct st_lsm6dsx_sensor *acc_sensor, *gyro_sensor;
343 u8 gyro_buff[ST_LSM6DSX_IIO_BUFF_SIZE];
344 u8 acc_buff[ST_LSM6DSX_IIO_BUFF_SIZE];
345 bool reset_ts = false;
349 err = st_lsm6dsx_read_locked(hw,
350 hw->settings->fifo_ops.fifo_diff.addr,
351 &fifo_status, sizeof(fifo_status));
353 dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
358 if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
361 fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
362 ST_LSM6DSX_CHAN_SIZE;
363 fifo_len = (fifo_len / pattern_len) * pattern_len;
365 acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
366 gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
368 for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
369 err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
370 hw->buff, pattern_len,
371 ST_LSM6DSX_MAX_WORD_LEN);
374 "failed to read pattern from fifo (err=%d)\n",
380 * Data are written to the FIFO with a specific pattern
381 * depending on the configured ODRs. The first sequence of data
382 * stored in FIFO contains the data of all enabled sensors
383 * (e.g. Gx, Gy, Gz, Ax, Ay, Az, Ts), then data are repeated
384 * depending on the value of the decimation factor set for each
387 * Supposing the FIFO is storing data from gyroscope and
388 * accelerometer at different ODRs:
389 * - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
390 * Since the gyroscope ODR is twice the accelerometer one, the
391 * following pattern is repeated every 9 samples:
392 * - Gx, Gy, Gz, Ax, Ay, Az, Ts, Gx, Gy, Gz, Ts, Gx, ..
394 gyro_sip = gyro_sensor->sip;
395 acc_sip = acc_sensor->sip;
399 while (acc_sip > 0 || gyro_sip > 0) {
401 memcpy(gyro_buff, &hw->buff[offset],
402 ST_LSM6DSX_SAMPLE_SIZE);
403 offset += ST_LSM6DSX_SAMPLE_SIZE;
406 memcpy(acc_buff, &hw->buff[offset],
407 ST_LSM6DSX_SAMPLE_SIZE);
408 offset += ST_LSM6DSX_SAMPLE_SIZE;
412 u8 data[ST_LSM6DSX_SAMPLE_SIZE];
414 memcpy(data, &hw->buff[offset], sizeof(data));
416 * hw timestamp is 3B long and it is stored
417 * in FIFO using 6B as 4th FIFO data set
418 * according to this schema:
419 * B0 = ts[15:8], B1 = ts[23:16], B3 = ts[7:0]
421 ts = data[1] << 16 | data[0] << 8 | data[3];
423 * check if hw timestamp engine is going to
424 * reset (the sensor generates an interrupt
425 * to signal the hw timestamp will reset in
428 if (!reset_ts && ts >= 0xff0000)
432 offset += ST_LSM6DSX_SAMPLE_SIZE;
436 iio_push_to_buffers_with_timestamp(
437 hw->iio_devs[ST_LSM6DSX_ID_GYRO],
438 gyro_buff, gyro_sensor->ts_ref + ts);
440 iio_push_to_buffers_with_timestamp(
441 hw->iio_devs[ST_LSM6DSX_ID_ACC],
442 acc_buff, acc_sensor->ts_ref + ts);
446 if (unlikely(reset_ts)) {
447 err = st_lsm6dsx_reset_hw_ts(hw);
449 dev_err(hw->dev, "failed to reset hw ts (err=%d)\n",
457 #define ST_LSM6DSX_INVALID_SAMPLE 0x7ffd
459 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
462 s16 val = le16_to_cpu(*(__le16 *)data);
463 struct st_lsm6dsx_sensor *sensor;
464 struct iio_dev *iio_dev;
466 /* invalid sample during bootstrap phase */
467 if (val >= ST_LSM6DSX_INVALID_SAMPLE)
471 * EXT_TAG are managed in FIFO fashion so ST_LSM6DSX_EXT0_TAG
472 * corresponds to the first enabled channel, ST_LSM6DSX_EXT1_TAG
473 * to the second one and ST_LSM6DSX_EXT2_TAG to the last enabled
477 case ST_LSM6DSX_GYRO_TAG:
478 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_GYRO];
480 case ST_LSM6DSX_ACC_TAG:
481 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_ACC];
483 case ST_LSM6DSX_EXT0_TAG:
484 if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0))
485 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT0];
486 else if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1))
487 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
489 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
491 case ST_LSM6DSX_EXT1_TAG:
492 if ((hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0)) &&
493 (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1)))
494 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
496 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
498 case ST_LSM6DSX_EXT2_TAG:
499 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
505 sensor = iio_priv(iio_dev);
506 iio_push_to_buffers_with_timestamp(iio_dev, data,
507 ts + sensor->ts_ref);
513 * st_lsm6dsx_read_tagged_fifo() - tagged hw FIFO read routine
514 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
516 * Read samples from the hw FIFO and push them to IIO buffers.
518 * Return: Number of bytes read from the FIFO
520 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
522 u16 pattern_len = hw->sip * ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
523 u16 fifo_len, fifo_diff_mask;
524 u8 iio_buff[ST_LSM6DSX_IIO_BUFF_SIZE], tag;
525 bool reset_ts = false;
526 int i, err, read_len;
530 err = st_lsm6dsx_read_locked(hw,
531 hw->settings->fifo_ops.fifo_diff.addr,
532 &fifo_status, sizeof(fifo_status));
534 dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
539 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
540 fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
541 ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
545 for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
546 err = st_lsm6dsx_read_block(hw,
547 ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
548 hw->buff, pattern_len,
549 ST_LSM6DSX_MAX_TAGGED_WORD_LEN);
552 "failed to read pattern from fifo (err=%d)\n",
557 for (i = 0; i < pattern_len;
558 i += ST_LSM6DSX_TAGGED_SAMPLE_SIZE) {
559 memcpy(iio_buff, &hw->buff[i + ST_LSM6DSX_TAG_SIZE],
560 ST_LSM6DSX_SAMPLE_SIZE);
562 tag = hw->buff[i] >> 3;
563 if (tag == ST_LSM6DSX_TS_TAG) {
565 * hw timestamp is 4B long and it is stored
566 * in FIFO according to this schema:
567 * B0 = ts[7:0], B1 = ts[15:8], B2 = ts[23:16],
570 ts = le32_to_cpu(*((__le32 *)iio_buff));
572 * check if hw timestamp engine is going to
573 * reset (the sensor generates an interrupt
574 * to signal the hw timestamp will reset in
577 if (!reset_ts && ts >= 0xffff0000)
581 st_lsm6dsx_push_tagged_data(hw, tag, iio_buff,
587 if (unlikely(reset_ts)) {
588 err = st_lsm6dsx_reset_hw_ts(hw);
595 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
599 if (!hw->settings->fifo_ops.read_fifo)
602 mutex_lock(&hw->fifo_lock);
604 hw->settings->fifo_ops.read_fifo(hw);
605 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
607 mutex_unlock(&hw->fifo_lock);
612 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable)
614 struct st_lsm6dsx_hw *hw = sensor->hw;
618 mutex_lock(&hw->conf_lock);
621 fifo_mask = hw->fifo_mask | BIT(sensor->id);
623 fifo_mask = hw->fifo_mask & ~BIT(sensor->id);
626 err = st_lsm6dsx_flush_fifo(hw);
631 if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
632 sensor->id == ST_LSM6DSX_ID_EXT1 ||
633 sensor->id == ST_LSM6DSX_ID_EXT2) {
634 err = st_lsm6dsx_shub_set_enable(sensor, enable);
638 err = st_lsm6dsx_sensor_set_enable(sensor, enable);
642 err = st_lsm6dsx_set_fifo_odr(sensor, enable);
647 err = st_lsm6dsx_update_decimators(hw);
651 err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
656 /* reset hw ts counter */
657 err = st_lsm6dsx_reset_hw_ts(hw);
661 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
666 hw->fifo_mask = fifo_mask;
669 mutex_unlock(&hw->conf_lock);
674 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
676 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
677 struct st_lsm6dsx_hw *hw = sensor->hw;
679 if (!hw->settings->fifo_ops.update_fifo)
682 return hw->settings->fifo_ops.update_fifo(sensor, true);
685 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
687 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
688 struct st_lsm6dsx_hw *hw = sensor->hw;
690 if (!hw->settings->fifo_ops.update_fifo)
693 return hw->settings->fifo_ops.update_fifo(sensor, false);
696 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
697 .preenable = st_lsm6dsx_buffer_preenable,
698 .postdisable = st_lsm6dsx_buffer_postdisable,
701 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
703 struct iio_buffer *buffer;
706 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
707 if (!hw->iio_devs[i])
710 buffer = devm_iio_kfifo_allocate(hw->dev);
714 iio_device_attach_buffer(hw->iio_devs[i], buffer);
715 hw->iio_devs[i]->modes |= INDIO_BUFFER_SOFTWARE;
716 hw->iio_devs[i]->setup_ops = &st_lsm6dsx_buffer_ops;