1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics st_lsm6dsx FIFO buffer library driver
4  *
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.
16  *
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).
21  *
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
26  *
27  * Copyright 2016 STMicroelectronics Inc.
28  *
29  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
30  * Denis Ciocca <denis.ciocca@st.com>
31  */
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>
38 
39 #include <linux/platform_data/st_sensors_pdata.h>
40 
41 #include "st_lsm6dsx.h"
42 
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
50 
51 #define ST_LSM6DSX_MAX_FIFO_ODR_VAL		0x08
52 
53 #define ST_LSM6DSX_TS_RESET_VAL			0xaa
54 
55 struct st_lsm6dsx_decimator_entry {
56 	u8 decimator;
57 	u8 val;
58 };
59 
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,
67 };
68 
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 },
79 };
80 
81 static int
82 st_lsm6dsx_get_decimator_val(struct st_lsm6dsx_sensor *sensor, u32 max_odr)
83 {
84 	const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
85 	u32 decimator =  max_odr / sensor->odr;
86 	int i;
87 
88 	if (decimator > 1)
89 		decimator = round_down(decimator, 2);
90 
91 	for (i = 0; i < max_size; i++) {
92 		if (st_lsm6dsx_decimator_table[i].decimator == decimator)
93 			break;
94 	}
95 
96 	return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
97 }
98 
99 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
100 				       u32 *max_odr, u32 *min_odr)
101 {
102 	struct st_lsm6dsx_sensor *sensor;
103 	int i;
104 
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;
109 
110 		sensor = iio_priv(hw->iio_devs[i]);
111 
112 		if (!(hw->enable_mask & BIT(sensor->id)))
113 			continue;
114 
115 		*max_odr = max_t(u32, *max_odr, sensor->odr);
116 		*min_odr = min_t(u32, *min_odr, sensor->odr);
117 	}
118 }
119 
120 static u8 st_lsm6dsx_get_sip(struct st_lsm6dsx_sensor *sensor, u32 min_odr)
121 {
122 	u8 sip = sensor->odr / min_odr;
123 
124 	return sip > 1 ? round_down(sip, 2) : sip;
125 }
126 
127 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
128 {
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;
135 
136 	st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr);
137 
138 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
139 		const struct st_lsm6dsx_reg *dec_reg;
140 
141 		if (!hw->iio_devs[i])
142 			continue;
143 
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;
152 		}
153 		ts_sip = max_t(u16, ts_sip, sensor->sip);
154 
155 		dec_reg = &hw->settings->decimator[sensor->id];
156 		if (dec_reg->addr) {
157 			int val = ST_LSM6DSX_SHIFT_VAL(data, dec_reg->mask);
158 
159 			err = st_lsm6dsx_update_bits_locked(hw, dec_reg->addr,
160 							    dec_reg->mask,
161 							    val);
162 			if (err < 0)
163 				return err;
164 		}
165 		sip += sensor->sip;
166 	}
167 	hw->sip = sip + ts_sip;
168 	hw->ts_sip = ts_sip;
169 
170 	/*
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
174 	 */
175 	ts_dec_reg = &hw->settings->ts_settings.decimator;
176 	if (ts_dec_reg->addr) {
177 		int val, ts_dec = !!hw->ts_sip;
178 
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);
182 	}
183 	return err;
184 }
185 
186 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
187 			     enum st_lsm6dsx_fifo_mode fifo_mode)
188 {
189 	unsigned int data;
190 
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);
194 }
195 
196 static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor,
197 				   bool enable)
198 {
199 	struct st_lsm6dsx_hw *hw = sensor->hw;
200 	const struct st_lsm6dsx_reg *batch_reg;
201 	u8 data;
202 
203 	batch_reg = &hw->settings->batch[sensor->id];
204 	if (batch_reg->addr) {
205 		int val;
206 
207 		if (enable) {
208 			int err;
209 
210 			err = st_lsm6dsx_check_odr(sensor, sensor->odr,
211 						   &data);
212 			if (err < 0)
213 				return err;
214 		} else {
215 			data = 0;
216 		}
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));
227 	}
228 }
229 
230 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark)
231 {
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;
237 
238 	if (!hw->sip)
239 		return 0;
240 
241 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
242 		if (!hw->iio_devs[i])
243 			continue;
244 
245 		cur_sensor = iio_priv(hw->iio_devs[i]);
246 
247 		if (!(hw->enable_mask & BIT(cur_sensor->id)))
248 			continue;
249 
250 		cur_watermark = (cur_sensor == sensor) ? watermark
251 						       : cur_sensor->watermark;
252 
253 		fifo_watermark = min_t(u16, fifo_watermark, cur_watermark);
254 	}
255 
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;
259 
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;
265 
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);
269 
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;
277 }
278 
279 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw *hw)
280 {
281 	struct st_lsm6dsx_sensor *sensor;
282 	int i, err;
283 
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;
289 
290 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
291 		if (!hw->iio_devs[i])
292 			continue;
293 
294 		sensor = iio_priv(hw->iio_devs[i]);
295 		/*
296 		 * store enable buffer timestamp as reference for
297 		 * hw timestamp
298 		 */
299 		sensor->ts_ref = iio_get_time_ns(hw->iio_devs[i]);
300 	}
301 	return 0;
302 }
303 
304 /*
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
307  */
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)
311 {
312 	unsigned int word_len, read_len = 0;
313 	int err;
314 
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;
323 	}
324 	return 0;
325 }
326 
327 #define ST_LSM6DSX_IIO_BUFF_SIZE	(ALIGN(ST_LSM6DSX_SAMPLE_SIZE, \
328 					       sizeof(s64)) + sizeof(s64))
329 /**
330  * st_lsm6dsx_read_fifo() - hw FIFO read routine
331  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
332  *
333  * Read samples from the hw FIFO and push them to IIO buffers.
334  *
335  * Return: Number of bytes read from the FIFO
336  */
337 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
338 {
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;
349 
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;
357 	}
358 
359 	if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
360 		return 0;
361 
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;
365 
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]);
370 
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;
380 		}
381 
382 		/*
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.
389 		 *
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, ..
396 		 */
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;
402 
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;
408 			}
409 			if (acc_sip > 0) {
410 				memcpy(acc_buff, &hw->buff[offset],
411 				       ST_LSM6DSX_SAMPLE_SIZE);
412 				offset += ST_LSM6DSX_SAMPLE_SIZE;
413 			}
414 			if (ext_sip > 0) {
415 				memcpy(ext_buff, &hw->buff[offset],
416 				       ST_LSM6DSX_SAMPLE_SIZE);
417 				offset += ST_LSM6DSX_SAMPLE_SIZE;
418 			}
419 
420 			if (ts_sip-- > 0) {
421 				u8 data[ST_LSM6DSX_SAMPLE_SIZE];
422 
423 				memcpy(data, &hw->buff[offset], sizeof(data));
424 				/*
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]
429 				 */
430 				ts = data[1] << 16 | data[0] << 8 | data[3];
431 				/*
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)
436 				 */
437 				if (!reset_ts && ts >= 0xff0000)
438 					reset_ts = true;
439 				ts *= hw->ts_gain;
440 
441 				offset += ST_LSM6DSX_SAMPLE_SIZE;
442 			}
443 
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);
456 		}
457 	}
458 
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;
465 		}
466 	}
467 	return read_len;
468 }
469 
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)
474 {
475 	s16 val = le16_to_cpu(*(__le16 *)data);
476 	struct st_lsm6dsx_sensor *sensor;
477 	struct iio_dev *iio_dev;
478 
479 	/* invalid sample during bootstrap phase */
480 	if (val >= ST_LSM6DSX_INVALID_SAMPLE)
481 		return -EINVAL;
482 
483 	/*
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
488 	 */
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;
516 	}
517 
518 	sensor = iio_priv(iio_dev);
519 	iio_push_to_buffers_with_timestamp(iio_dev, data,
520 					   ts + sensor->ts_ref);
521 
522 	return 0;
523 }
524 
525 /**
526  * st_lsm6dsx_read_tagged_fifo() - tagged hw FIFO read routine
527  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
528  *
529  * Read samples from the hw FIFO and push them to IIO buffers.
530  *
531  * Return: Number of bytes read from the FIFO
532  */
533 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
534 {
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;
542 
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;
550 	}
551 
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;
557 
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;
568 		}
569 
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);
574 
575 			tag = hw->buff[i] >> 3;
576 			if (tag == ST_LSM6DSX_TS_TAG) {
577 				/*
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]
582 				 */
583 				ts = le32_to_cpu(*((__le32 *)iio_buff));
584 				/*
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)
589 				 */
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);
596 			}
597 		}
598 	}
599 
600 	if (unlikely(reset_ts)) {
601 		err = st_lsm6dsx_reset_hw_ts(hw);
602 		if (err < 0)
603 			return err;
604 	}
605 	return read_len;
606 }
607 
608 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
609 {
610 	int err;
611 
612 	if (!hw->settings->fifo_ops.read_fifo)
613 		return -ENOTSUPP;
614 
615 	mutex_lock(&hw->fifo_lock);
616 
617 	hw->settings->fifo_ops.read_fifo(hw);
618 	err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
619 
620 	mutex_unlock(&hw->fifo_lock);
621 
622 	return err;
623 }
624 
625 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable)
626 {
627 	struct st_lsm6dsx_hw *hw = sensor->hw;
628 	u8 fifo_mask;
629 	int err;
630 
631 	mutex_lock(&hw->conf_lock);
632 
633 	if (enable)
634 		fifo_mask = hw->fifo_mask | BIT(sensor->id);
635 	else
636 		fifo_mask = hw->fifo_mask & ~BIT(sensor->id);
637 
638 	if (hw->fifo_mask) {
639 		err = st_lsm6dsx_flush_fifo(hw);
640 		if (err < 0)
641 			goto out;
642 	}
643 
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;
654 	}
655 
656 	err = st_lsm6dsx_set_fifo_odr(sensor, enable);
657 	if (err < 0)
658 		goto out;
659 
660 	err = st_lsm6dsx_update_decimators(hw);
661 	if (err < 0)
662 		goto out;
663 
664 	err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
665 	if (err < 0)
666 		goto out;
667 
668 	if (fifo_mask) {
669 		/* reset hw ts counter */
670 		err = st_lsm6dsx_reset_hw_ts(hw);
671 		if (err < 0)
672 			goto out;
673 
674 		err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
675 		if (err < 0)
676 			goto out;
677 	}
678 
679 	hw->fifo_mask = fifo_mask;
680 
681 out:
682 	mutex_unlock(&hw->conf_lock);
683 
684 	return err;
685 }
686 
687 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
688 {
689 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
690 	struct st_lsm6dsx_hw *hw = sensor->hw;
691 
692 	if (!hw->settings->fifo_ops.update_fifo)
693 		return -ENOTSUPP;
694 
695 	return hw->settings->fifo_ops.update_fifo(sensor, true);
696 }
697 
698 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
699 {
700 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
701 	struct st_lsm6dsx_hw *hw = sensor->hw;
702 
703 	if (!hw->settings->fifo_ops.update_fifo)
704 		return -ENOTSUPP;
705 
706 	return hw->settings->fifo_ops.update_fifo(sensor, false);
707 }
708 
709 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
710 	.preenable = st_lsm6dsx_buffer_preenable,
711 	.postdisable = st_lsm6dsx_buffer_postdisable,
712 };
713 
714 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
715 {
716 	struct iio_buffer *buffer;
717 	int i;
718 
719 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
720 		if (!hw->iio_devs[i])
721 			continue;
722 
723 		buffer = devm_iio_kfifo_allocate(hw->dev);
724 		if (!buffer)
725 			return -ENOMEM;
726 
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;
730 	}
731 
732 	return 0;
733 }
734