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 	sensor->decimator = decimator;
97 	return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
98 }
99 
100 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
101 				       u32 *max_odr, u32 *min_odr)
102 {
103 	struct st_lsm6dsx_sensor *sensor;
104 	int i;
105 
106 	*max_odr = 0, *min_odr = ~0;
107 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
108 		if (!hw->iio_devs[i])
109 			continue;
110 
111 		sensor = iio_priv(hw->iio_devs[i]);
112 
113 		if (!(hw->enable_mask & BIT(sensor->id)))
114 			continue;
115 
116 		*max_odr = max_t(u32, *max_odr, sensor->odr);
117 		*min_odr = min_t(u32, *min_odr, sensor->odr);
118 	}
119 }
120 
121 static u8 st_lsm6dsx_get_sip(struct st_lsm6dsx_sensor *sensor, u32 min_odr)
122 {
123 	u8 sip = sensor->odr / min_odr;
124 
125 	return sip > 1 ? round_down(sip, 2) : sip;
126 }
127 
128 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
129 {
130 	const struct st_lsm6dsx_reg *ts_dec_reg;
131 	struct st_lsm6dsx_sensor *sensor;
132 	u16 sip = 0, ts_sip = 0;
133 	u32 max_odr, min_odr;
134 	int err = 0, i;
135 	u8 data;
136 
137 	st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr);
138 
139 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
140 		const struct st_lsm6dsx_reg *dec_reg;
141 
142 		if (!hw->iio_devs[i])
143 			continue;
144 
145 		sensor = iio_priv(hw->iio_devs[i]);
146 		/* update fifo decimators and sample in pattern */
147 		if (hw->enable_mask & BIT(sensor->id)) {
148 			sensor->sip = st_lsm6dsx_get_sip(sensor, min_odr);
149 			data = st_lsm6dsx_get_decimator_val(sensor, max_odr);
150 		} else {
151 			sensor->sip = 0;
152 			data = 0;
153 		}
154 		ts_sip = max_t(u16, ts_sip, sensor->sip);
155 
156 		dec_reg = &hw->settings->decimator[sensor->id];
157 		if (dec_reg->addr) {
158 			int val = ST_LSM6DSX_SHIFT_VAL(data, dec_reg->mask);
159 
160 			err = st_lsm6dsx_update_bits_locked(hw, dec_reg->addr,
161 							    dec_reg->mask,
162 							    val);
163 			if (err < 0)
164 				return err;
165 		}
166 		sip += sensor->sip;
167 	}
168 	hw->sip = sip + ts_sip;
169 	hw->ts_sip = ts_sip;
170 
171 	/*
172 	 * update hw ts decimator if necessary. Decimator for hw timestamp
173 	 * is always 1 or 0 in order to have a ts sample for each data
174 	 * sample in FIFO
175 	 */
176 	ts_dec_reg = &hw->settings->ts_settings.decimator;
177 	if (ts_dec_reg->addr) {
178 		int val, ts_dec = !!hw->ts_sip;
179 
180 		val = ST_LSM6DSX_SHIFT_VAL(ts_dec, ts_dec_reg->mask);
181 		err = st_lsm6dsx_update_bits_locked(hw, ts_dec_reg->addr,
182 						    ts_dec_reg->mask, val);
183 	}
184 	return err;
185 }
186 
187 static int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
188 				    enum st_lsm6dsx_fifo_mode fifo_mode)
189 {
190 	unsigned int data;
191 
192 	data = FIELD_PREP(ST_LSM6DSX_FIFO_MODE_MASK, fifo_mode);
193 	return st_lsm6dsx_update_bits_locked(hw, ST_LSM6DSX_REG_FIFO_MODE_ADDR,
194 					     ST_LSM6DSX_FIFO_MODE_MASK, data);
195 }
196 
197 static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor,
198 				   bool enable)
199 {
200 	struct st_lsm6dsx_hw *hw = sensor->hw;
201 	const struct st_lsm6dsx_reg *batch_reg;
202 	u8 data;
203 
204 	batch_reg = &hw->settings->batch[sensor->id];
205 	if (batch_reg->addr) {
206 		int val;
207 
208 		if (enable) {
209 			int err;
210 
211 			err = st_lsm6dsx_check_odr(sensor, sensor->odr,
212 						   &data);
213 			if (err < 0)
214 				return err;
215 		} else {
216 			data = 0;
217 		}
218 		val = ST_LSM6DSX_SHIFT_VAL(data, batch_reg->mask);
219 		return st_lsm6dsx_update_bits_locked(hw, batch_reg->addr,
220 						     batch_reg->mask, val);
221 	} else {
222 		data = hw->enable_mask ? ST_LSM6DSX_MAX_FIFO_ODR_VAL : 0;
223 		return st_lsm6dsx_update_bits_locked(hw,
224 					ST_LSM6DSX_REG_FIFO_MODE_ADDR,
225 					ST_LSM6DSX_FIFO_ODR_MASK,
226 					FIELD_PREP(ST_LSM6DSX_FIFO_ODR_MASK,
227 						   data));
228 	}
229 }
230 
231 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark)
232 {
233 	u16 fifo_watermark = ~0, cur_watermark, fifo_th_mask;
234 	struct st_lsm6dsx_hw *hw = sensor->hw;
235 	struct st_lsm6dsx_sensor *cur_sensor;
236 	int i, err, data;
237 	__le16 wdata;
238 
239 	if (!hw->sip)
240 		return 0;
241 
242 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
243 		if (!hw->iio_devs[i])
244 			continue;
245 
246 		cur_sensor = iio_priv(hw->iio_devs[i]);
247 
248 		if (!(hw->enable_mask & BIT(cur_sensor->id)))
249 			continue;
250 
251 		cur_watermark = (cur_sensor == sensor) ? watermark
252 						       : cur_sensor->watermark;
253 
254 		fifo_watermark = min_t(u16, fifo_watermark, cur_watermark);
255 	}
256 
257 	fifo_watermark = max_t(u16, fifo_watermark, hw->sip);
258 	fifo_watermark = (fifo_watermark / hw->sip) * hw->sip;
259 	fifo_watermark = fifo_watermark * hw->settings->fifo_ops.th_wl;
260 
261 	mutex_lock(&hw->page_lock);
262 	err = regmap_read(hw->regmap, hw->settings->fifo_ops.fifo_th.addr + 1,
263 			  &data);
264 	if (err < 0)
265 		goto out;
266 
267 	fifo_th_mask = hw->settings->fifo_ops.fifo_th.mask;
268 	fifo_watermark = ((data << 8) & ~fifo_th_mask) |
269 			 (fifo_watermark & fifo_th_mask);
270 
271 	wdata = cpu_to_le16(fifo_watermark);
272 	err = regmap_bulk_write(hw->regmap,
273 				hw->settings->fifo_ops.fifo_th.addr,
274 				&wdata, sizeof(wdata));
275 out:
276 	mutex_unlock(&hw->page_lock);
277 	return err;
278 }
279 
280 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw *hw)
281 {
282 	struct st_lsm6dsx_sensor *sensor;
283 	int i, err;
284 
285 	/* reset hw ts counter */
286 	err = st_lsm6dsx_write_locked(hw, ST_LSM6DSX_REG_TS_RESET_ADDR,
287 				      ST_LSM6DSX_TS_RESET_VAL);
288 	if (err < 0)
289 		return err;
290 
291 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
292 		if (!hw->iio_devs[i])
293 			continue;
294 
295 		sensor = iio_priv(hw->iio_devs[i]);
296 		/*
297 		 * store enable buffer timestamp as reference for
298 		 * hw timestamp
299 		 */
300 		sensor->ts_ref = iio_get_time_ns(hw->iio_devs[i]);
301 	}
302 	return 0;
303 }
304 
305 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw)
306 {
307 	int err;
308 
309 	/* reset hw ts counter */
310 	err = st_lsm6dsx_reset_hw_ts(hw);
311 	if (err < 0)
312 		return err;
313 
314 	return st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
315 }
316 
317 /*
318  * Set max bulk read to ST_LSM6DSX_MAX_WORD_LEN/ST_LSM6DSX_MAX_TAGGED_WORD_LEN
319  * in order to avoid a kmalloc for each bus access
320  */
321 static inline int st_lsm6dsx_read_block(struct st_lsm6dsx_hw *hw, u8 addr,
322 					u8 *data, unsigned int data_len,
323 					unsigned int max_word_len)
324 {
325 	unsigned int word_len, read_len = 0;
326 	int err;
327 
328 	while (read_len < data_len) {
329 		word_len = min_t(unsigned int, data_len - read_len,
330 				 max_word_len);
331 		err = st_lsm6dsx_read_locked(hw, addr, data + read_len,
332 					     word_len);
333 		if (err < 0)
334 			return err;
335 		read_len += word_len;
336 	}
337 	return 0;
338 }
339 
340 #define ST_LSM6DSX_IIO_BUFF_SIZE	(ALIGN(ST_LSM6DSX_SAMPLE_SIZE, \
341 					       sizeof(s64)) + sizeof(s64))
342 /**
343  * st_lsm6dsx_read_fifo() - hw FIFO read routine
344  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
345  *
346  * Read samples from the hw FIFO and push them to IIO buffers.
347  *
348  * Return: Number of bytes read from the FIFO
349  */
350 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
351 {
352 	struct st_lsm6dsx_sensor *acc_sensor, *gyro_sensor, *ext_sensor = NULL;
353 	int err, sip, acc_sip, gyro_sip, ts_sip, ext_sip, read_len, offset;
354 	u16 fifo_len, pattern_len = hw->sip * ST_LSM6DSX_SAMPLE_SIZE;
355 	u16 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
356 	u8 gyro_buff[ST_LSM6DSX_IIO_BUFF_SIZE];
357 	u8 acc_buff[ST_LSM6DSX_IIO_BUFF_SIZE];
358 	u8 ext_buff[ST_LSM6DSX_IIO_BUFF_SIZE];
359 	bool reset_ts = false;
360 	__le16 fifo_status;
361 	s64 ts = 0;
362 
363 	err = st_lsm6dsx_read_locked(hw,
364 				     hw->settings->fifo_ops.fifo_diff.addr,
365 				     &fifo_status, sizeof(fifo_status));
366 	if (err < 0) {
367 		dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
368 			err);
369 		return err;
370 	}
371 
372 	if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
373 		return 0;
374 
375 	fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
376 		   ST_LSM6DSX_CHAN_SIZE;
377 	fifo_len = (fifo_len / pattern_len) * pattern_len;
378 
379 	acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
380 	gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
381 	if (hw->iio_devs[ST_LSM6DSX_ID_EXT0])
382 		ext_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_EXT0]);
383 
384 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
385 		err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
386 					    hw->buff, pattern_len,
387 					    ST_LSM6DSX_MAX_WORD_LEN);
388 		if (err < 0) {
389 			dev_err(hw->dev,
390 				"failed to read pattern from fifo (err=%d)\n",
391 				err);
392 			return err;
393 		}
394 
395 		/*
396 		 * Data are written to the FIFO with a specific pattern
397 		 * depending on the configured ODRs. The first sequence of data
398 		 * stored in FIFO contains the data of all enabled sensors
399 		 * (e.g. Gx, Gy, Gz, Ax, Ay, Az, Ts), then data are repeated
400 		 * depending on the value of the decimation factor set for each
401 		 * sensor.
402 		 *
403 		 * Supposing the FIFO is storing data from gyroscope and
404 		 * accelerometer at different ODRs:
405 		 *   - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
406 		 * Since the gyroscope ODR is twice the accelerometer one, the
407 		 * following pattern is repeated every 9 samples:
408 		 *   - Gx, Gy, Gz, Ax, Ay, Az, Ts, Gx, Gy, Gz, Ts, Gx, ..
409 		 */
410 		ext_sip = ext_sensor ? ext_sensor->sip : 0;
411 		gyro_sip = gyro_sensor->sip;
412 		acc_sip = acc_sensor->sip;
413 		ts_sip = hw->ts_sip;
414 		offset = 0;
415 		sip = 0;
416 
417 		while (acc_sip > 0 || gyro_sip > 0 || ext_sip > 0) {
418 			if (gyro_sip > 0 && !(sip % gyro_sensor->decimator)) {
419 				memcpy(gyro_buff, &hw->buff[offset],
420 				       ST_LSM6DSX_SAMPLE_SIZE);
421 				offset += ST_LSM6DSX_SAMPLE_SIZE;
422 			}
423 			if (acc_sip > 0 && !(sip % acc_sensor->decimator)) {
424 				memcpy(acc_buff, &hw->buff[offset],
425 				       ST_LSM6DSX_SAMPLE_SIZE);
426 				offset += ST_LSM6DSX_SAMPLE_SIZE;
427 			}
428 			if (ext_sip > 0 && !(sip % ext_sensor->decimator)) {
429 				memcpy(ext_buff, &hw->buff[offset],
430 				       ST_LSM6DSX_SAMPLE_SIZE);
431 				offset += ST_LSM6DSX_SAMPLE_SIZE;
432 			}
433 
434 			if (ts_sip-- > 0) {
435 				u8 data[ST_LSM6DSX_SAMPLE_SIZE];
436 
437 				memcpy(data, &hw->buff[offset], sizeof(data));
438 				/*
439 				 * hw timestamp is 3B long and it is stored
440 				 * in FIFO using 6B as 4th FIFO data set
441 				 * according to this schema:
442 				 * B0 = ts[15:8], B1 = ts[23:16], B3 = ts[7:0]
443 				 */
444 				ts = data[1] << 16 | data[0] << 8 | data[3];
445 				/*
446 				 * check if hw timestamp engine is going to
447 				 * reset (the sensor generates an interrupt
448 				 * to signal the hw timestamp will reset in
449 				 * 1.638s)
450 				 */
451 				if (!reset_ts && ts >= 0xff0000)
452 					reset_ts = true;
453 				ts *= hw->ts_gain;
454 
455 				offset += ST_LSM6DSX_SAMPLE_SIZE;
456 			}
457 
458 			if (gyro_sip > 0 && !(sip % gyro_sensor->decimator)) {
459 				iio_push_to_buffers_with_timestamp(
460 					hw->iio_devs[ST_LSM6DSX_ID_GYRO],
461 					gyro_buff, gyro_sensor->ts_ref + ts);
462 				gyro_sip--;
463 			}
464 			if (acc_sip > 0 && !(sip % acc_sensor->decimator)) {
465 				iio_push_to_buffers_with_timestamp(
466 					hw->iio_devs[ST_LSM6DSX_ID_ACC],
467 					acc_buff, acc_sensor->ts_ref + ts);
468 				acc_sip--;
469 			}
470 			if (ext_sip > 0 && !(sip % ext_sensor->decimator)) {
471 				iio_push_to_buffers_with_timestamp(
472 					hw->iio_devs[ST_LSM6DSX_ID_EXT0],
473 					ext_buff, ext_sensor->ts_ref + ts);
474 				ext_sip--;
475 			}
476 			sip++;
477 		}
478 	}
479 
480 	if (unlikely(reset_ts)) {
481 		err = st_lsm6dsx_reset_hw_ts(hw);
482 		if (err < 0) {
483 			dev_err(hw->dev, "failed to reset hw ts (err=%d)\n",
484 				err);
485 			return err;
486 		}
487 	}
488 	return read_len;
489 }
490 
491 #define ST_LSM6DSX_INVALID_SAMPLE	0x7ffd
492 static int
493 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
494 			    u8 *data, s64 ts)
495 {
496 	s16 val = le16_to_cpu(*(__le16 *)data);
497 	struct st_lsm6dsx_sensor *sensor;
498 	struct iio_dev *iio_dev;
499 
500 	/* invalid sample during bootstrap phase */
501 	if (val >= ST_LSM6DSX_INVALID_SAMPLE)
502 		return -EINVAL;
503 
504 	/*
505 	 * EXT_TAG are managed in FIFO fashion so ST_LSM6DSX_EXT0_TAG
506 	 * corresponds to the first enabled channel, ST_LSM6DSX_EXT1_TAG
507 	 * to the second one and ST_LSM6DSX_EXT2_TAG to the last enabled
508 	 * channel
509 	 */
510 	switch (tag) {
511 	case ST_LSM6DSX_GYRO_TAG:
512 		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_GYRO];
513 		break;
514 	case ST_LSM6DSX_ACC_TAG:
515 		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_ACC];
516 		break;
517 	case ST_LSM6DSX_EXT0_TAG:
518 		if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0))
519 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT0];
520 		else if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1))
521 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
522 		else
523 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
524 		break;
525 	case ST_LSM6DSX_EXT1_TAG:
526 		if ((hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0)) &&
527 		    (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1)))
528 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
529 		else
530 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
531 		break;
532 	case ST_LSM6DSX_EXT2_TAG:
533 		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
534 		break;
535 	default:
536 		return -EINVAL;
537 	}
538 
539 	sensor = iio_priv(iio_dev);
540 	iio_push_to_buffers_with_timestamp(iio_dev, data,
541 					   ts + sensor->ts_ref);
542 
543 	return 0;
544 }
545 
546 /**
547  * st_lsm6dsx_read_tagged_fifo() - tagged hw FIFO read routine
548  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
549  *
550  * Read samples from the hw FIFO and push them to IIO buffers.
551  *
552  * Return: Number of bytes read from the FIFO
553  */
554 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
555 {
556 	u16 pattern_len = hw->sip * ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
557 	u16 fifo_len, fifo_diff_mask;
558 	u8 iio_buff[ST_LSM6DSX_IIO_BUFF_SIZE], tag;
559 	bool reset_ts = false;
560 	int i, err, read_len;
561 	__le16 fifo_status;
562 	s64 ts = 0;
563 
564 	err = st_lsm6dsx_read_locked(hw,
565 				     hw->settings->fifo_ops.fifo_diff.addr,
566 				     &fifo_status, sizeof(fifo_status));
567 	if (err < 0) {
568 		dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
569 			err);
570 		return err;
571 	}
572 
573 	fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
574 	fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
575 		   ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
576 	if (!fifo_len)
577 		return 0;
578 
579 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
580 		err = st_lsm6dsx_read_block(hw,
581 					    ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
582 					    hw->buff, pattern_len,
583 					    ST_LSM6DSX_MAX_TAGGED_WORD_LEN);
584 		if (err < 0) {
585 			dev_err(hw->dev,
586 				"failed to read pattern from fifo (err=%d)\n",
587 				err);
588 			return err;
589 		}
590 
591 		for (i = 0; i < pattern_len;
592 		     i += ST_LSM6DSX_TAGGED_SAMPLE_SIZE) {
593 			memcpy(iio_buff, &hw->buff[i + ST_LSM6DSX_TAG_SIZE],
594 			       ST_LSM6DSX_SAMPLE_SIZE);
595 
596 			tag = hw->buff[i] >> 3;
597 			if (tag == ST_LSM6DSX_TS_TAG) {
598 				/*
599 				 * hw timestamp is 4B long and it is stored
600 				 * in FIFO according to this schema:
601 				 * B0 = ts[7:0], B1 = ts[15:8], B2 = ts[23:16],
602 				 * B3 = ts[31:24]
603 				 */
604 				ts = le32_to_cpu(*((__le32 *)iio_buff));
605 				/*
606 				 * check if hw timestamp engine is going to
607 				 * reset (the sensor generates an interrupt
608 				 * to signal the hw timestamp will reset in
609 				 * 1.638s)
610 				 */
611 				if (!reset_ts && ts >= 0xffff0000)
612 					reset_ts = true;
613 				ts *= hw->ts_gain;
614 			} else {
615 				st_lsm6dsx_push_tagged_data(hw, tag, iio_buff,
616 							    ts);
617 			}
618 		}
619 	}
620 
621 	if (unlikely(reset_ts)) {
622 		err = st_lsm6dsx_reset_hw_ts(hw);
623 		if (err < 0)
624 			return err;
625 	}
626 	return read_len;
627 }
628 
629 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
630 {
631 	int err;
632 
633 	if (!hw->settings->fifo_ops.read_fifo)
634 		return -ENOTSUPP;
635 
636 	mutex_lock(&hw->fifo_lock);
637 
638 	hw->settings->fifo_ops.read_fifo(hw);
639 	err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
640 
641 	mutex_unlock(&hw->fifo_lock);
642 
643 	return err;
644 }
645 
646 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable)
647 {
648 	struct st_lsm6dsx_hw *hw = sensor->hw;
649 	u8 fifo_mask;
650 	int err;
651 
652 	mutex_lock(&hw->conf_lock);
653 
654 	if (enable)
655 		fifo_mask = hw->fifo_mask | BIT(sensor->id);
656 	else
657 		fifo_mask = hw->fifo_mask & ~BIT(sensor->id);
658 
659 	if (hw->fifo_mask) {
660 		err = st_lsm6dsx_flush_fifo(hw);
661 		if (err < 0)
662 			goto out;
663 	}
664 
665 	if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
666 	    sensor->id == ST_LSM6DSX_ID_EXT1 ||
667 	    sensor->id == ST_LSM6DSX_ID_EXT2) {
668 		err = st_lsm6dsx_shub_set_enable(sensor, enable);
669 		if (err < 0)
670 			goto out;
671 	} else {
672 		err = st_lsm6dsx_sensor_set_enable(sensor, enable);
673 		if (err < 0)
674 			goto out;
675 	}
676 
677 	err = st_lsm6dsx_set_fifo_odr(sensor, enable);
678 	if (err < 0)
679 		goto out;
680 
681 	err = st_lsm6dsx_update_decimators(hw);
682 	if (err < 0)
683 		goto out;
684 
685 	err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
686 	if (err < 0)
687 		goto out;
688 
689 	if (fifo_mask) {
690 		err = st_lsm6dsx_resume_fifo(hw);
691 		if (err < 0)
692 			goto out;
693 	}
694 
695 	hw->fifo_mask = fifo_mask;
696 
697 out:
698 	mutex_unlock(&hw->conf_lock);
699 
700 	return err;
701 }
702 
703 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
704 {
705 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
706 	struct st_lsm6dsx_hw *hw = sensor->hw;
707 
708 	if (!hw->settings->fifo_ops.update_fifo)
709 		return -ENOTSUPP;
710 
711 	return hw->settings->fifo_ops.update_fifo(sensor, true);
712 }
713 
714 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
715 {
716 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
717 	struct st_lsm6dsx_hw *hw = sensor->hw;
718 
719 	if (!hw->settings->fifo_ops.update_fifo)
720 		return -ENOTSUPP;
721 
722 	return hw->settings->fifo_ops.update_fifo(sensor, false);
723 }
724 
725 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
726 	.preenable = st_lsm6dsx_buffer_preenable,
727 	.postdisable = st_lsm6dsx_buffer_postdisable,
728 };
729 
730 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
731 {
732 	struct iio_buffer *buffer;
733 	int i;
734 
735 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
736 		if (!hw->iio_devs[i])
737 			continue;
738 
739 		buffer = devm_iio_kfifo_allocate(hw->dev);
740 		if (!buffer)
741 			return -ENOMEM;
742 
743 		iio_device_attach_buffer(hw->iio_devs[i], buffer);
744 		hw->iio_devs[i]->modes |= INDIO_BUFFER_SOFTWARE;
745 		hw->iio_devs[i]->setup_ops = &st_lsm6dsx_buffer_ops;
746 	}
747 
748 	return 0;
749 }
750