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