xref: /linux/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c (revision 914a1fe5f818bff5abb0e792bc0addf0c7c274f4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics st_lsm6dsx FIFO buffer library driver
4  *
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.
16  *
17  * Supported devices:
18  * - ISM330DLC
19  * - LSM6DS3
20  * - LSM6DS3H
21  * - LSM6DS3TR-C
22  * - LSM6DSL
23  * - LSM6DSM
24  *
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).
29  *
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
44  *
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
49  *
50  * Copyright 2016 STMicroelectronics Inc.
51  *
52  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
53  * Denis Ciocca <denis.ciocca@st.com>
54  */
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>
61 
62 #include <linux/platform_data/st_sensors_pdata.h>
63 
64 #include "st_lsm6dsx.h"
65 
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
73 
74 #define ST_LSM6DSX_MAX_FIFO_ODR_VAL		0x08
75 
76 #define ST_LSM6DSX_TS_RESET_VAL			0xaa
77 
78 struct st_lsm6dsx_decimator_entry {
79 	u8 decimator;
80 	u8 val;
81 };
82 
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,
90 };
91 
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 },
102 };
103 
104 static int
st_lsm6dsx_get_decimator_val(struct st_lsm6dsx_sensor * sensor,u32 max_odr)105 st_lsm6dsx_get_decimator_val(struct st_lsm6dsx_sensor *sensor, u32 max_odr)
106 {
107 	const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
108 	u32 decimator =  max_odr / sensor->odr;
109 	int i;
110 
111 	if (decimator > 1)
112 		decimator = round_down(decimator, 2);
113 
114 	for (i = 0; i < max_size; i++) {
115 		if (st_lsm6dsx_decimator_table[i].decimator == decimator)
116 			break;
117 	}
118 
119 	sensor->decimator = decimator;
120 	return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
121 }
122 
st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw * hw,u32 * max_odr,u32 * min_odr)123 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
124 				       u32 *max_odr, u32 *min_odr)
125 {
126 	struct st_lsm6dsx_sensor *sensor;
127 	int i;
128 
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;
133 
134 		sensor = iio_priv(hw->iio_devs[i]);
135 
136 		if (!(hw->enable_mask & BIT(sensor->id)))
137 			continue;
138 
139 		*max_odr = max_t(u32, *max_odr, sensor->odr);
140 		*min_odr = min_t(u32, *min_odr, sensor->odr);
141 	}
142 }
143 
st_lsm6dsx_get_sip(struct st_lsm6dsx_sensor * sensor,u32 min_odr)144 static u8 st_lsm6dsx_get_sip(struct st_lsm6dsx_sensor *sensor, u32 min_odr)
145 {
146 	u8 sip = sensor->odr / min_odr;
147 
148 	return sip > 1 ? round_down(sip, 2) : sip;
149 }
150 
st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw * hw)151 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
152 {
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;
159 
160 	st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr);
161 
162 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
163 		const struct st_lsm6dsx_reg *dec_reg;
164 
165 		if (!hw->iio_devs[i])
166 			continue;
167 
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;
176 		}
177 		ts_sip = max_t(u16, ts_sip, sensor->sip);
178 
179 		dec_reg = &hw->settings->decimator[sensor->id];
180 		if (dec_reg->addr) {
181 			int val = ST_LSM6DSX_SHIFT_VAL(data, dec_reg->mask);
182 
183 			err = st_lsm6dsx_update_bits_locked(hw, dec_reg->addr,
184 							    dec_reg->mask,
185 							    val);
186 			if (err < 0)
187 				return err;
188 		}
189 		sip += sensor->sip;
190 	}
191 	hw->sip = sip + ts_sip;
192 	hw->ts_sip = ts_sip;
193 
194 	/*
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
198 	 */
199 	ts_dec_reg = &hw->settings->ts_settings.decimator;
200 	if (ts_dec_reg->addr) {
201 		int val, ts_dec = !!hw->ts_sip;
202 
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);
206 	}
207 	return err;
208 }
209 
st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw * hw,enum st_lsm6dsx_fifo_mode fifo_mode)210 static int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
211 				    enum st_lsm6dsx_fifo_mode fifo_mode)
212 {
213 	unsigned int data;
214 
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);
218 }
219 
st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor * sensor,bool enable)220 static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor,
221 				   bool enable)
222 {
223 	struct st_lsm6dsx_hw *hw = sensor->hw;
224 	const struct st_lsm6dsx_reg *batch_reg;
225 	u8 data;
226 
227 	batch_reg = &hw->settings->batch[sensor->id];
228 	if (batch_reg->addr) {
229 		int val;
230 
231 		if (enable) {
232 			int err;
233 
234 			err = st_lsm6dsx_check_odr(sensor, sensor->odr,
235 						   &data);
236 			if (err < 0)
237 				return err;
238 		} else {
239 			data = 0;
240 		}
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));
251 	}
252 }
253 
st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor * sensor,u16 watermark)254 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark)
255 {
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;
261 
262 	if (!hw->sip)
263 		return 0;
264 
265 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
266 		if (!hw->iio_devs[i])
267 			continue;
268 
269 		cur_sensor = iio_priv(hw->iio_devs[i]);
270 
271 		if (!(hw->enable_mask & BIT(cur_sensor->id)))
272 			continue;
273 
274 		cur_watermark = (cur_sensor == sensor) ? watermark
275 						       : cur_sensor->watermark;
276 
277 		fifo_watermark = min_t(u16, fifo_watermark, cur_watermark);
278 	}
279 
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;
283 
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;
289 
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);
293 
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;
301 }
302 
st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw * hw)303 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw *hw)
304 {
305 	struct st_lsm6dsx_sensor *sensor;
306 	int i, err;
307 
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;
313 
314 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
315 		if (!hw->iio_devs[i])
316 			continue;
317 
318 		sensor = iio_priv(hw->iio_devs[i]);
319 		/*
320 		 * store enable buffer timestamp as reference for
321 		 * hw timestamp
322 		 */
323 		sensor->ts_ref = iio_get_time_ns(hw->iio_devs[i]);
324 	}
325 	return 0;
326 }
327 
st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw * hw)328 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw)
329 {
330 	int err;
331 
332 	/* reset hw ts counter */
333 	err = st_lsm6dsx_reset_hw_ts(hw);
334 	if (err < 0)
335 		return err;
336 
337 	return st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
338 }
339 
340 /*
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
343  */
st_lsm6dsx_read_block(struct st_lsm6dsx_hw * hw,u8 addr,u8 * data,unsigned int data_len,unsigned int max_word_len)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)
347 {
348 	unsigned int word_len, read_len = 0;
349 	int err;
350 
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;
359 	}
360 	return 0;
361 }
362 
363 #define ST_LSM6DSX_IIO_BUFF_SIZE	(ALIGN(ST_LSM6DSX_SAMPLE_SIZE, \
364 					       sizeof(s64)) + sizeof(s64))
365 /**
366  * st_lsm6dsx_read_fifo() - hw FIFO read routine
367  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
368  *
369  * Read samples from the hw FIFO and push them to IIO buffers.
370  *
371  * Return: Number of bytes read from the FIFO
372  */
st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw * hw)373 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
374 {
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;
382 
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;
390 	}
391 
392 	if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
393 		return 0;
394 
395 	if (!pattern_len)
396 		pattern_len = ST_LSM6DSX_SAMPLE_SIZE;
397 
398 	fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
399 		   ST_LSM6DSX_CHAN_SIZE;
400 	fifo_len = (fifo_len / pattern_len) * pattern_len;
401 
402 	acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
403 	gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
404 	if (hw->iio_devs[ST_LSM6DSX_ID_EXT0])
405 		ext_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_EXT0]);
406 
407 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
408 		err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
409 					    hw->buff, pattern_len,
410 					    ST_LSM6DSX_MAX_WORD_LEN);
411 		if (err < 0) {
412 			dev_err(hw->dev,
413 				"failed to read pattern from fifo (err=%d)\n",
414 				err);
415 			return err;
416 		}
417 
418 		/*
419 		 * Data are written to the FIFO with a specific pattern
420 		 * depending on the configured ODRs. The first sequence of data
421 		 * stored in FIFO contains the data of all enabled sensors
422 		 * (e.g. Gx, Gy, Gz, Ax, Ay, Az, Ts), then data are repeated
423 		 * depending on the value of the decimation factor set for each
424 		 * sensor.
425 		 *
426 		 * Supposing the FIFO is storing data from gyroscope and
427 		 * accelerometer at different ODRs:
428 		 *   - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
429 		 * Since the gyroscope ODR is twice the accelerometer one, the
430 		 * following pattern is repeated every 9 samples:
431 		 *   - Gx, Gy, Gz, Ax, Ay, Az, Ts, Gx, Gy, Gz, Ts, Gx, ..
432 		 */
433 		ext_sip = ext_sensor ? ext_sensor->sip : 0;
434 		gyro_sip = gyro_sensor->sip;
435 		acc_sip = acc_sensor->sip;
436 		ts_sip = hw->ts_sip;
437 		offset = 0;
438 		sip = 0;
439 
440 		while (acc_sip > 0 || gyro_sip > 0 || ext_sip > 0) {
441 			if (gyro_sip > 0 && !(sip % gyro_sensor->decimator)) {
442 				memcpy(hw->scan[ST_LSM6DSX_ID_GYRO].channels,
443 				       &hw->buff[offset],
444 				       sizeof(hw->scan[ST_LSM6DSX_ID_GYRO].channels));
445 				offset += sizeof(hw->scan[ST_LSM6DSX_ID_GYRO].channels);
446 			}
447 			if (acc_sip > 0 && !(sip % acc_sensor->decimator)) {
448 				memcpy(hw->scan[ST_LSM6DSX_ID_ACC].channels,
449 				       &hw->buff[offset],
450 				       sizeof(hw->scan[ST_LSM6DSX_ID_ACC].channels));
451 				offset += sizeof(hw->scan[ST_LSM6DSX_ID_ACC].channels);
452 			}
453 			if (ext_sip > 0 && !(sip % ext_sensor->decimator)) {
454 				memcpy(hw->scan[ST_LSM6DSX_ID_EXT0].channels,
455 				       &hw->buff[offset],
456 				       sizeof(hw->scan[ST_LSM6DSX_ID_EXT0].channels));
457 				offset += sizeof(hw->scan[ST_LSM6DSX_ID_EXT0].channels);
458 			}
459 
460 			if (ts_sip-- > 0) {
461 				u8 data[ST_LSM6DSX_SAMPLE_SIZE];
462 
463 				memcpy(data, &hw->buff[offset], sizeof(data));
464 				/*
465 				 * hw timestamp is 3B long and it is stored
466 				 * in FIFO using 6B as 4th FIFO data set
467 				 * according to this schema:
468 				 * B0 = ts[15:8], B1 = ts[23:16], B3 = ts[7:0]
469 				 */
470 				ts = data[1] << 16 | data[0] << 8 | data[3];
471 				/*
472 				 * check if hw timestamp engine is going to
473 				 * reset (the sensor generates an interrupt
474 				 * to signal the hw timestamp will reset in
475 				 * 1.638s)
476 				 */
477 				if (!reset_ts && ts >= 0xff0000)
478 					reset_ts = true;
479 				ts *= hw->ts_gain;
480 
481 				offset += ST_LSM6DSX_SAMPLE_SIZE;
482 			}
483 
484 			if (gyro_sip > 0 && !(sip % gyro_sensor->decimator)) {
485 				/*
486 				 * We need to discards gyro samples during
487 				 * filters settling time
488 				 */
489 				if (gyro_sensor->samples_to_discard > 0)
490 					gyro_sensor->samples_to_discard--;
491 				else
492 					iio_push_to_buffers_with_timestamp(
493 						hw->iio_devs[ST_LSM6DSX_ID_GYRO],
494 						&hw->scan[ST_LSM6DSX_ID_GYRO],
495 						gyro_sensor->ts_ref + ts);
496 				gyro_sip--;
497 			}
498 			if (acc_sip > 0 && !(sip % acc_sensor->decimator)) {
499 				/*
500 				 * We need to discards accel samples during
501 				 * filters settling time
502 				 */
503 				if (acc_sensor->samples_to_discard > 0)
504 					acc_sensor->samples_to_discard--;
505 				else
506 					iio_push_to_buffers_with_timestamp(
507 						hw->iio_devs[ST_LSM6DSX_ID_ACC],
508 						&hw->scan[ST_LSM6DSX_ID_ACC],
509 						acc_sensor->ts_ref + ts);
510 				acc_sip--;
511 			}
512 			if (ext_sip > 0 && !(sip % ext_sensor->decimator)) {
513 				iio_push_to_buffers_with_timestamp(
514 					hw->iio_devs[ST_LSM6DSX_ID_EXT0],
515 					&hw->scan[ST_LSM6DSX_ID_EXT0],
516 					ext_sensor->ts_ref + ts);
517 				ext_sip--;
518 			}
519 			sip++;
520 		}
521 	}
522 
523 	if (unlikely(reset_ts)) {
524 		err = st_lsm6dsx_reset_hw_ts(hw);
525 		if (err < 0) {
526 			dev_err(hw->dev, "failed to reset hw ts (err=%d)\n",
527 				err);
528 			return err;
529 		}
530 	}
531 	return read_len;
532 }
533 
534 #define ST_LSM6DSX_INVALID_SAMPLE	0x7ffd
535 static int
st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw * hw,u8 tag,u8 * data,s64 ts)536 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
537 			    u8 *data, s64 ts)
538 {
539 	s16 val = le16_to_cpu(*(__le16 *)data);
540 	struct st_lsm6dsx_sensor *sensor;
541 	struct iio_dev *iio_dev;
542 
543 	/* invalid sample during bootstrap phase */
544 	if (val >= ST_LSM6DSX_INVALID_SAMPLE)
545 		return -EINVAL;
546 
547 	/*
548 	 * EXT_TAG are managed in FIFO fashion so ST_LSM6DSX_EXT0_TAG
549 	 * corresponds to the first enabled channel, ST_LSM6DSX_EXT1_TAG
550 	 * to the second one and ST_LSM6DSX_EXT2_TAG to the last enabled
551 	 * channel
552 	 */
553 	switch (tag) {
554 	case ST_LSM6DSX_GYRO_TAG:
555 		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_GYRO];
556 		break;
557 	case ST_LSM6DSX_ACC_TAG:
558 		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_ACC];
559 		break;
560 	case ST_LSM6DSX_EXT0_TAG:
561 		if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0))
562 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT0];
563 		else if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1))
564 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
565 		else
566 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
567 		break;
568 	case ST_LSM6DSX_EXT1_TAG:
569 		if ((hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0)) &&
570 		    (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1)))
571 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1];
572 		else
573 			iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
574 		break;
575 	case ST_LSM6DSX_EXT2_TAG:
576 		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
577 		break;
578 	default:
579 		return -EINVAL;
580 	}
581 
582 	sensor = iio_priv(iio_dev);
583 	iio_push_to_buffers_with_timestamp(iio_dev, data,
584 					   ts + sensor->ts_ref);
585 
586 	return 0;
587 }
588 
589 /**
590  * st_lsm6dsx_read_tagged_fifo() - tagged hw FIFO read routine
591  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
592  *
593  * Read samples from the hw FIFO and push them to IIO buffers.
594  *
595  * Return: Number of bytes read from the FIFO
596  */
st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw * hw)597 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
598 {
599 	u16 pattern_len = hw->sip * ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
600 	u16 fifo_len, fifo_diff_mask;
601 	/*
602 	 * Alignment needed as this can ultimately be passed to a
603 	 * call to iio_push_to_buffers_with_timestamp() which
604 	 * must be passed a buffer that is aligned to 8 bytes so
605 	 * as to allow insertion of a naturally aligned timestamp.
606 	 */
607 	u8 iio_buff[ST_LSM6DSX_IIO_BUFF_SIZE] __aligned(8);
608 	u8 tag;
609 	bool reset_ts = false;
610 	int i, err, read_len;
611 	__le16 fifo_status;
612 	s64 ts = 0;
613 
614 	err = st_lsm6dsx_read_locked(hw,
615 				     hw->settings->fifo_ops.fifo_diff.addr,
616 				     &fifo_status, sizeof(fifo_status));
617 	if (err < 0) {
618 		dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
619 			err);
620 		return err;
621 	}
622 
623 	fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
624 	fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
625 		   ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
626 	if (!fifo_len)
627 		return 0;
628 
629 	if (!pattern_len)
630 		pattern_len = ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
631 
632 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
633 		err = st_lsm6dsx_read_block(hw,
634 					    ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
635 					    hw->buff, pattern_len,
636 					    ST_LSM6DSX_MAX_TAGGED_WORD_LEN);
637 		if (err < 0) {
638 			dev_err(hw->dev,
639 				"failed to read pattern from fifo (err=%d)\n",
640 				err);
641 			return err;
642 		}
643 
644 		for (i = 0; i < pattern_len;
645 		     i += ST_LSM6DSX_TAGGED_SAMPLE_SIZE) {
646 			memcpy(iio_buff, &hw->buff[i + ST_LSM6DSX_TAG_SIZE],
647 			       ST_LSM6DSX_SAMPLE_SIZE);
648 
649 			tag = hw->buff[i] >> 3;
650 			if (tag == ST_LSM6DSX_TS_TAG) {
651 				/*
652 				 * hw timestamp is 4B long and it is stored
653 				 * in FIFO according to this schema:
654 				 * B0 = ts[7:0], B1 = ts[15:8], B2 = ts[23:16],
655 				 * B3 = ts[31:24]
656 				 */
657 				ts = le32_to_cpu(*((__le32 *)iio_buff));
658 				/*
659 				 * check if hw timestamp engine is going to
660 				 * reset (the sensor generates an interrupt
661 				 * to signal the hw timestamp will reset in
662 				 * 1.638s)
663 				 */
664 				if (!reset_ts && ts >= 0xffff0000)
665 					reset_ts = true;
666 				ts *= hw->ts_gain;
667 			} else {
668 				st_lsm6dsx_push_tagged_data(hw, tag, iio_buff,
669 							    ts);
670 			}
671 		}
672 	}
673 
674 	if (unlikely(reset_ts)) {
675 		err = st_lsm6dsx_reset_hw_ts(hw);
676 		if (err < 0)
677 			return err;
678 	}
679 	return read_len;
680 }
681 
st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw * hw)682 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
683 {
684 	int err;
685 
686 	if (!hw->settings->fifo_ops.read_fifo)
687 		return -ENOTSUPP;
688 
689 	mutex_lock(&hw->fifo_lock);
690 
691 	hw->settings->fifo_ops.read_fifo(hw);
692 	err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
693 
694 	mutex_unlock(&hw->fifo_lock);
695 
696 	return err;
697 }
698 
699 static void
st_lsm6dsx_update_samples_to_discard(struct st_lsm6dsx_sensor * sensor)700 st_lsm6dsx_update_samples_to_discard(struct st_lsm6dsx_sensor *sensor)
701 {
702 	const struct st_lsm6dsx_samples_to_discard *data;
703 	struct st_lsm6dsx_hw *hw = sensor->hw;
704 	int i;
705 
706 	if (sensor->id != ST_LSM6DSX_ID_GYRO &&
707 	    sensor->id != ST_LSM6DSX_ID_ACC)
708 		return;
709 
710 	/* check if drdy mask is supported in hw */
711 	if (hw->settings->drdy_mask.addr)
712 		return;
713 
714 	data = &hw->settings->samples_to_discard[sensor->id];
715 	for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++) {
716 		if (data->val[i].milli_hz == sensor->odr) {
717 			sensor->samples_to_discard = data->val[i].samples;
718 			return;
719 		}
720 	}
721 }
722 
st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor * sensor,bool enable)723 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable)
724 {
725 	struct st_lsm6dsx_hw *hw = sensor->hw;
726 	u8 fifo_mask;
727 	int err;
728 
729 	mutex_lock(&hw->conf_lock);
730 
731 	if (enable)
732 		fifo_mask = hw->fifo_mask | BIT(sensor->id);
733 	else
734 		fifo_mask = hw->fifo_mask & ~BIT(sensor->id);
735 
736 	if (hw->fifo_mask) {
737 		err = st_lsm6dsx_flush_fifo(hw);
738 		if (err < 0)
739 			goto out;
740 	}
741 
742 	if (enable)
743 		st_lsm6dsx_update_samples_to_discard(sensor);
744 
745 	err = st_lsm6dsx_device_set_enable(sensor, enable);
746 	if (err < 0)
747 		goto out;
748 
749 	err = st_lsm6dsx_set_fifo_odr(sensor, enable);
750 	if (err < 0)
751 		goto out;
752 
753 	err = st_lsm6dsx_update_decimators(hw);
754 	if (err < 0)
755 		goto out;
756 
757 	err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
758 	if (err < 0)
759 		goto out;
760 
761 	if (fifo_mask) {
762 		err = st_lsm6dsx_resume_fifo(hw);
763 		if (err < 0)
764 			goto out;
765 	}
766 
767 	hw->fifo_mask = fifo_mask;
768 
769 out:
770 	mutex_unlock(&hw->conf_lock);
771 
772 	return err;
773 }
774 
st_lsm6dsx_buffer_preenable(struct iio_dev * iio_dev)775 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
776 {
777 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
778 	struct st_lsm6dsx_hw *hw = sensor->hw;
779 
780 	if (!hw->settings->fifo_ops.update_fifo)
781 		return -ENOTSUPP;
782 
783 	return hw->settings->fifo_ops.update_fifo(sensor, true);
784 }
785 
st_lsm6dsx_buffer_postdisable(struct iio_dev * iio_dev)786 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
787 {
788 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
789 	struct st_lsm6dsx_hw *hw = sensor->hw;
790 
791 	if (!hw->settings->fifo_ops.update_fifo)
792 		return -ENOTSUPP;
793 
794 	return hw->settings->fifo_ops.update_fifo(sensor, false);
795 }
796 
797 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
798 	.preenable = st_lsm6dsx_buffer_preenable,
799 	.postdisable = st_lsm6dsx_buffer_postdisable,
800 };
801 
st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw * hw)802 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
803 {
804 	int i, ret;
805 
806 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
807 		if (!hw->iio_devs[i])
808 			continue;
809 
810 		ret = devm_iio_kfifo_buffer_setup(hw->dev, hw->iio_devs[i],
811 						  &st_lsm6dsx_buffer_ops);
812 		if (ret)
813 			return ret;
814 	}
815 
816 	return 0;
817 }
818