1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics st_lsm6dsx FIFO buffer library driver
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.
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).
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
{
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,
93 struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table
[] = {
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
;
112 decimator
= round_down(decimator
, 2);
114 for (i
= 0; i
< max_size
; i
++) {
115 if (st_lsm6dsx_decimator_table
[i
].decimator
== decimator
)
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
;
129 *max_odr
= 0, *min_odr
= ~0;
130 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
131 if (!hw
->iio_devs
[i
])
134 sensor
= iio_priv(hw
->iio_devs
[i
]);
136 if (!(hw
->enable_mask
& BIT(sensor
->id
)))
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
;
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
])
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
);
177 ts_sip
= max_t(u16
, ts_sip
, sensor
->sip
);
179 dec_reg
= &hw
->settings
->decimator
[sensor
->id
];
181 int val
= ST_LSM6DSX_SHIFT_VAL(data
, dec_reg
->mask
);
183 err
= st_lsm6dsx_update_bits_locked(hw
, dec_reg
->addr
,
191 hw
->sip
= 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
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
);
210 static int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw
*hw
,
211 enum st_lsm6dsx_fifo_mode fifo_mode
)
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
,
223 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
224 const struct st_lsm6dsx_reg
*batch_reg
;
227 batch_reg
= &hw
->settings
->batch
[sensor
->id
];
228 if (batch_reg
->addr
) {
234 err
= st_lsm6dsx_check_odr(sensor
, sensor
->odr
,
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
);
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
,
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
;
265 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
266 if (!hw
->iio_devs
[i
])
269 cur_sensor
= iio_priv(hw
->iio_devs
[i
]);
271 if (!(hw
->enable_mask
& BIT(cur_sensor
->id
)))
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,
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
));
299 mutex_unlock(&hw
->page_lock
);
303 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw
*hw
)
305 struct st_lsm6dsx_sensor
*sensor
;
308 /* reset hw ts counter */
309 err
= st_lsm6dsx_write_locked(hw
, ST_LSM6DSX_REG_TS_RESET_ADDR
,
310 ST_LSM6DSX_TS_RESET_VAL
);
314 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
315 if (!hw
->iio_devs
[i
])
318 sensor
= iio_priv(hw
->iio_devs
[i
]);
320 * store enable buffer timestamp as reference for
323 sensor
->ts_ref
= iio_get_time_ns(hw
->iio_devs
[i
]);
328 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw
*hw
)
332 /* reset hw ts counter */
333 err
= st_lsm6dsx_reset_hw_ts(hw
);
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;
351 while (read_len
< data_len
) {
352 word_len
= min_t(unsigned int, data_len
- read_len
,
354 err
= st_lsm6dsx_read_locked(hw
, addr
, data
+ read_len
,
358 read_len
+= word_len
;
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;
383 err
= st_lsm6dsx_read_locked(hw
,
384 hw
->settings
->fifo_ops
.fifo_diff
.addr
,
385 &fifo_status
, sizeof(fifo_status
));
387 dev_err(hw
->dev
, "failed to read fifo status (err=%d)\n",
392 if (fifo_status
& cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK
))
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
);
410 "failed to read pattern from fifo (err=%d)\n",
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
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
;
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
,
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
,
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
,
453 sizeof(hw
->scan
[ST_LSM6DSX_ID_EXT0
].channels
));
454 offset
+= sizeof(hw
->scan
[ST_LSM6DSX_ID_EXT0
].channels
);
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
474 if (!reset_ts
&& ts
>= 0xff0000)
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
--;
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
);
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
--;
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
);
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
);
520 if (unlikely(reset_ts
)) {
521 err
= st_lsm6dsx_reset_hw_ts(hw
);
523 dev_err(hw
->dev
, "failed to reset hw ts (err=%d)\n",
531 #define ST_LSM6DSX_INVALID_SAMPLE 0x7ffd
533 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw
*hw
, u8 tag
,
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
)
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
551 case ST_LSM6DSX_GYRO_TAG
:
552 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_GYRO
];
554 case ST_LSM6DSX_ACC_TAG
:
555 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_ACC
];
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
];
563 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_EXT2
];
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
];
570 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_EXT2
];
572 case ST_LSM6DSX_EXT2_TAG
:
573 iio_dev
= hw
->iio_devs
[ST_LSM6DSX_ID_EXT2
];
579 sensor
= iio_priv(iio_dev
);
580 iio_push_to_buffers_with_timestamp(iio_dev
, data
,
581 ts
+ sensor
->ts_ref
);
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);
606 bool reset_ts
= false;
607 int i
, err
, read_len
;
611 err
= st_lsm6dsx_read_locked(hw
,
612 hw
->settings
->fifo_ops
.fifo_diff
.addr
,
613 &fifo_status
, sizeof(fifo_status
));
615 dev_err(hw
->dev
, "failed to read fifo status (err=%d)\n",
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
;
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
);
633 "failed to read pattern from fifo (err=%d)\n",
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],
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
658 if (!reset_ts
&& ts
>= 0xffff0000)
662 st_lsm6dsx_push_tagged_data(hw
, tag
, iio_buff
,
668 if (unlikely(reset_ts
)) {
669 err
= st_lsm6dsx_reset_hw_ts(hw
);
676 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw
*hw
)
680 if (!hw
->settings
->fifo_ops
.read_fifo
)
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
);
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
;
700 if (sensor
->id
!= ST_LSM6DSX_ID_GYRO
&&
701 sensor
->id
!= ST_LSM6DSX_ID_ACC
)
704 /* check if drdy mask is supported in hw */
705 if (hw
->settings
->drdy_mask
.addr
)
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
;
717 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor
*sensor
, bool enable
)
719 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
723 mutex_lock(&hw
->conf_lock
);
726 fifo_mask
= hw
->fifo_mask
| BIT(sensor
->id
);
728 fifo_mask
= hw
->fifo_mask
& ~BIT(sensor
->id
);
731 err
= st_lsm6dsx_flush_fifo(hw
);
737 st_lsm6dsx_update_samples_to_discard(sensor
);
739 err
= st_lsm6dsx_device_set_enable(sensor
, enable
);
743 err
= st_lsm6dsx_set_fifo_odr(sensor
, enable
);
747 err
= st_lsm6dsx_update_decimators(hw
);
751 err
= st_lsm6dsx_update_watermark(sensor
, sensor
->watermark
);
756 err
= st_lsm6dsx_resume_fifo(hw
);
761 hw
->fifo_mask
= fifo_mask
;
764 mutex_unlock(&hw
->conf_lock
);
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
)
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
)
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
)
800 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
801 if (!hw
->iio_devs
[i
])
804 ret
= devm_iio_kfifo_buffer_setup(hw
->dev
, hw
->iio_devs
[i
],
805 &st_lsm6dsx_buffer_ops
);