1 /*
2  * STMicroelectronics st_lsm6dsx FIFO buffer library driver
3  *
4  * LSM6DS3/LSM6DS3H/LSM6DSL/LSM6DSM: The FIFO buffer can be configured
5  * to store data from gyroscope and accelerometer. Samples are queued
6  * without any tag according to a specific pattern based on 'FIFO data sets'
7  * (6 bytes each):
8  *  - 1st data set is reserved for gyroscope data
9  *  - 2nd data set is reserved for accelerometer data
10  * The FIFO pattern changes depending on the ODRs and decimation factors
11  * assigned to the FIFO data sets. The first sequence of data stored in FIFO
12  * buffer contains the data of all the enabled FIFO data sets
13  * (e.g. Gx, Gy, Gz, Ax, Ay, Az), then data are repeated depending on the
14  * value of the decimation factor and ODR set for each FIFO data set.
15  * FIFO supported modes:
16  *  - BYPASS: FIFO disabled
17  *  - CONTINUOUS: FIFO enabled. When the buffer is full, the FIFO index
18  *    restarts from the beginning and the oldest sample is overwritten
19  *
20  * Copyright 2016 STMicroelectronics Inc.
21  *
22  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
23  * Denis Ciocca <denis.ciocca@st.com>
24  *
25  * Licensed under the GPL-2.
26  */
27 #include <linux/module.h>
28 #include <linux/interrupt.h>
29 #include <linux/irq.h>
30 #include <linux/iio/kfifo_buf.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/buffer.h>
33 
34 #include "st_lsm6dsx.h"
35 
36 #define ST_LSM6DSX_REG_FIFO_THL_ADDR		0x06
37 #define ST_LSM6DSX_REG_FIFO_THH_ADDR		0x07
38 #define ST_LSM6DSX_FIFO_TH_MASK			GENMASK(11, 0)
39 #define ST_LSM6DSX_REG_FIFO_DEC_GXL_ADDR	0x08
40 #define ST_LSM6DSX_REG_HLACTIVE_ADDR		0x12
41 #define ST_LSM6DSX_REG_HLACTIVE_MASK		BIT(5)
42 #define ST_LSM6DSX_REG_FIFO_MODE_ADDR		0x0a
43 #define ST_LSM6DSX_FIFO_MODE_MASK		GENMASK(2, 0)
44 #define ST_LSM6DSX_FIFO_ODR_MASK		GENMASK(6, 3)
45 #define ST_LSM6DSX_REG_FIFO_DIFFL_ADDR		0x3a
46 #define ST_LSM6DSX_FIFO_DIFF_MASK		GENMASK(11, 0)
47 #define ST_LSM6DSX_FIFO_EMPTY_MASK		BIT(12)
48 #define ST_LSM6DSX_REG_FIFO_OUTL_ADDR		0x3e
49 
50 #define ST_LSM6DSX_MAX_FIFO_ODR_VAL		0x08
51 
52 struct st_lsm6dsx_decimator_entry {
53 	u8 decimator;
54 	u8 val;
55 };
56 
57 static const
58 struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = {
59 	{  0, 0x0 },
60 	{  1, 0x1 },
61 	{  2, 0x2 },
62 	{  3, 0x3 },
63 	{  4, 0x4 },
64 	{  8, 0x5 },
65 	{ 16, 0x6 },
66 	{ 32, 0x7 },
67 };
68 
69 static int st_lsm6dsx_get_decimator_val(u8 val)
70 {
71 	const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
72 	int i;
73 
74 	for (i = 0; i < max_size; i++)
75 		if (st_lsm6dsx_decimator_table[i].decimator == val)
76 			break;
77 
78 	return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
79 }
80 
81 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
82 				       u16 *max_odr, u16 *min_odr)
83 {
84 	struct st_lsm6dsx_sensor *sensor;
85 	int i;
86 
87 	*max_odr = 0, *min_odr = ~0;
88 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
89 		sensor = iio_priv(hw->iio_devs[i]);
90 
91 		if (!(hw->enable_mask & BIT(sensor->id)))
92 			continue;
93 
94 		*max_odr = max_t(u16, *max_odr, sensor->odr);
95 		*min_odr = min_t(u16, *min_odr, sensor->odr);
96 	}
97 }
98 
99 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
100 {
101 	struct st_lsm6dsx_sensor *sensor;
102 	u16 max_odr, min_odr, sip = 0;
103 	int err, i;
104 	u8 data;
105 
106 	st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr);
107 
108 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
109 		sensor = iio_priv(hw->iio_devs[i]);
110 
111 		/* update fifo decimators and sample in pattern */
112 		if (hw->enable_mask & BIT(sensor->id)) {
113 			sensor->sip = sensor->odr / min_odr;
114 			sensor->decimator = max_odr / sensor->odr;
115 			data = st_lsm6dsx_get_decimator_val(sensor->decimator);
116 		} else {
117 			sensor->sip = 0;
118 			sensor->decimator = 0;
119 			data = 0;
120 		}
121 
122 		err = st_lsm6dsx_write_with_mask(hw,
123 					ST_LSM6DSX_REG_FIFO_DEC_GXL_ADDR,
124 					sensor->decimator_mask, data);
125 		if (err < 0)
126 			return err;
127 
128 		sip += sensor->sip;
129 	}
130 	hw->sip = sip;
131 
132 	return 0;
133 }
134 
135 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
136 			     enum st_lsm6dsx_fifo_mode fifo_mode)
137 {
138 	u8 data;
139 	int err;
140 
141 	switch (fifo_mode) {
142 	case ST_LSM6DSX_FIFO_BYPASS:
143 		data = fifo_mode;
144 		break;
145 	case ST_LSM6DSX_FIFO_CONT:
146 		data = (ST_LSM6DSX_MAX_FIFO_ODR_VAL <<
147 			__ffs(ST_LSM6DSX_FIFO_ODR_MASK)) | fifo_mode;
148 		break;
149 	default:
150 		return -EINVAL;
151 	}
152 
153 	err = hw->tf->write(hw->dev, ST_LSM6DSX_REG_FIFO_MODE_ADDR,
154 			    sizeof(data), &data);
155 	if (err < 0)
156 		return err;
157 
158 	hw->fifo_mode = fifo_mode;
159 
160 	return 0;
161 }
162 
163 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark)
164 {
165 	u16 fifo_watermark = ~0, cur_watermark, sip = 0;
166 	struct st_lsm6dsx_hw *hw = sensor->hw;
167 	struct st_lsm6dsx_sensor *cur_sensor;
168 	__le16 wdata;
169 	int i, err;
170 	u8 data;
171 
172 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
173 		cur_sensor = iio_priv(hw->iio_devs[i]);
174 
175 		if (!(hw->enable_mask & BIT(cur_sensor->id)))
176 			continue;
177 
178 		cur_watermark = (cur_sensor == sensor) ? watermark
179 						       : cur_sensor->watermark;
180 
181 		fifo_watermark = min_t(u16, fifo_watermark, cur_watermark);
182 		sip += cur_sensor->sip;
183 	}
184 
185 	if (!sip)
186 		return 0;
187 
188 	fifo_watermark = max_t(u16, fifo_watermark, sip);
189 	fifo_watermark = (fifo_watermark / sip) * sip;
190 	fifo_watermark = fifo_watermark * ST_LSM6DSX_SAMPLE_DEPTH;
191 
192 	mutex_lock(&hw->lock);
193 
194 	err = hw->tf->read(hw->dev, ST_LSM6DSX_REG_FIFO_THH_ADDR,
195 			   sizeof(data), &data);
196 	if (err < 0)
197 		goto out;
198 
199 	fifo_watermark = ((data << 8) & ~ST_LSM6DSX_FIFO_TH_MASK) |
200 			 (fifo_watermark & ST_LSM6DSX_FIFO_TH_MASK);
201 
202 	wdata = cpu_to_le16(fifo_watermark);
203 	err = hw->tf->write(hw->dev, ST_LSM6DSX_REG_FIFO_THL_ADDR,
204 			    sizeof(wdata), (u8 *)&wdata);
205 out:
206 	mutex_unlock(&hw->lock);
207 
208 	return err < 0 ? err : 0;
209 }
210 
211 /**
212  * st_lsm6dsx_read_fifo() - LSM6DS3-LSM6DS3H-LSM6DSL-LSM6DSM read FIFO routine
213  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
214  *
215  * Read samples from the hw FIFO and push them to IIO buffers.
216  *
217  * Return: Number of bytes read from the FIFO
218  */
219 static int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
220 {
221 	u16 fifo_len, pattern_len = hw->sip * ST_LSM6DSX_SAMPLE_SIZE;
222 	int err, acc_sip, gyro_sip, read_len, samples, offset;
223 	struct st_lsm6dsx_sensor *acc_sensor, *gyro_sensor;
224 	s64 acc_ts, acc_delta_ts, gyro_ts, gyro_delta_ts;
225 	u8 iio_buff[ALIGN(ST_LSM6DSX_SAMPLE_SIZE, sizeof(s64)) + sizeof(s64)];
226 	u8 buff[pattern_len];
227 	__le16 fifo_status;
228 
229 	err = hw->tf->read(hw->dev, ST_LSM6DSX_REG_FIFO_DIFFL_ADDR,
230 			   sizeof(fifo_status), (u8 *)&fifo_status);
231 	if (err < 0)
232 		return err;
233 
234 	if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
235 		return 0;
236 
237 	fifo_len = (le16_to_cpu(fifo_status) & ST_LSM6DSX_FIFO_DIFF_MASK) *
238 		   ST_LSM6DSX_CHAN_SIZE;
239 	samples = fifo_len / ST_LSM6DSX_SAMPLE_SIZE;
240 	fifo_len = (fifo_len / pattern_len) * pattern_len;
241 
242 	/*
243 	 * compute delta timestamp between two consecutive samples
244 	 * in order to estimate queueing time of data generated
245 	 * by the sensor
246 	 */
247 	acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
248 	acc_ts = acc_sensor->ts - acc_sensor->delta_ts;
249 	acc_delta_ts = div_s64(acc_sensor->delta_ts * acc_sensor->decimator,
250 			       samples);
251 
252 	gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
253 	gyro_ts = gyro_sensor->ts - gyro_sensor->delta_ts;
254 	gyro_delta_ts = div_s64(gyro_sensor->delta_ts * gyro_sensor->decimator,
255 				samples);
256 
257 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
258 		err = hw->tf->read(hw->dev, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
259 				   sizeof(buff), buff);
260 		if (err < 0)
261 			return err;
262 
263 		/*
264 		 * Data are written to the FIFO with a specific pattern
265 		 * depending on the configured ODRs. The first sequence of data
266 		 * stored in FIFO contains the data of all enabled sensors
267 		 * (e.g. Gx, Gy, Gz, Ax, Ay, Az), then data are repeated
268 		 * depending on the value of the decimation factor set for each
269 		 * sensor.
270 		 *
271 		 * Supposing the FIFO is storing data from gyroscope and
272 		 * accelerometer at different ODRs:
273 		 *   - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
274 		 * Since the gyroscope ODR is twice the accelerometer one, the
275 		 * following pattern is repeated every 9 samples:
276 		 *   - Gx, Gy, Gz, Ax, Ay, Az, Gx, Gy, Gz
277 		 */
278 		gyro_sip = gyro_sensor->sip;
279 		acc_sip = acc_sensor->sip;
280 		offset = 0;
281 
282 		while (acc_sip > 0 || gyro_sip > 0) {
283 			if (gyro_sip-- > 0) {
284 				memcpy(iio_buff, &buff[offset],
285 				       ST_LSM6DSX_SAMPLE_SIZE);
286 				iio_push_to_buffers_with_timestamp(
287 					hw->iio_devs[ST_LSM6DSX_ID_GYRO],
288 					iio_buff, gyro_ts);
289 				offset += ST_LSM6DSX_SAMPLE_SIZE;
290 				gyro_ts += gyro_delta_ts;
291 			}
292 
293 			if (acc_sip-- > 0) {
294 				memcpy(iio_buff, &buff[offset],
295 				       ST_LSM6DSX_SAMPLE_SIZE);
296 				iio_push_to_buffers_with_timestamp(
297 					hw->iio_devs[ST_LSM6DSX_ID_ACC],
298 					iio_buff, acc_ts);
299 				offset += ST_LSM6DSX_SAMPLE_SIZE;
300 				acc_ts += acc_delta_ts;
301 			}
302 		}
303 	}
304 
305 	return read_len;
306 }
307 
308 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
309 {
310 	int err;
311 
312 	mutex_lock(&hw->fifo_lock);
313 
314 	st_lsm6dsx_read_fifo(hw);
315 	err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
316 
317 	mutex_unlock(&hw->fifo_lock);
318 
319 	return err;
320 }
321 
322 static int st_lsm6dsx_update_fifo(struct iio_dev *iio_dev, bool enable)
323 {
324 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
325 	struct st_lsm6dsx_hw *hw = sensor->hw;
326 	int err;
327 
328 	if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS) {
329 		err = st_lsm6dsx_flush_fifo(hw);
330 		if (err < 0)
331 			return err;
332 	}
333 
334 	if (enable) {
335 		err = st_lsm6dsx_sensor_enable(sensor);
336 		if (err < 0)
337 			return err;
338 	} else {
339 		err = st_lsm6dsx_sensor_disable(sensor);
340 		if (err < 0)
341 			return err;
342 	}
343 
344 	err = st_lsm6dsx_update_decimators(hw);
345 	if (err < 0)
346 		return err;
347 
348 	err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
349 	if (err < 0)
350 		return err;
351 
352 	if (hw->enable_mask) {
353 		err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
354 		if (err < 0)
355 			return err;
356 
357 		/*
358 		 * store enable buffer timestamp as reference to compute
359 		 * first delta timestamp
360 		 */
361 		sensor->ts = iio_get_time_ns(iio_dev);
362 	}
363 
364 	return 0;
365 }
366 
367 static irqreturn_t st_lsm6dsx_handler_irq(int irq, void *private)
368 {
369 	struct st_lsm6dsx_hw *hw = private;
370 	struct st_lsm6dsx_sensor *sensor;
371 	int i;
372 
373 	if (!hw->sip)
374 		return IRQ_NONE;
375 
376 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
377 		sensor = iio_priv(hw->iio_devs[i]);
378 
379 		if (sensor->sip > 0) {
380 			s64 timestamp;
381 
382 			timestamp = iio_get_time_ns(hw->iio_devs[i]);
383 			sensor->delta_ts = timestamp - sensor->ts;
384 			sensor->ts = timestamp;
385 		}
386 	}
387 
388 	return IRQ_WAKE_THREAD;
389 }
390 
391 static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
392 {
393 	struct st_lsm6dsx_hw *hw = private;
394 	int count;
395 
396 	mutex_lock(&hw->fifo_lock);
397 	count = st_lsm6dsx_read_fifo(hw);
398 	mutex_unlock(&hw->fifo_lock);
399 
400 	return !count ? IRQ_NONE : IRQ_HANDLED;
401 }
402 
403 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
404 {
405 	return st_lsm6dsx_update_fifo(iio_dev, true);
406 }
407 
408 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
409 {
410 	return st_lsm6dsx_update_fifo(iio_dev, false);
411 }
412 
413 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
414 	.preenable = st_lsm6dsx_buffer_preenable,
415 	.postdisable = st_lsm6dsx_buffer_postdisable,
416 };
417 
418 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
419 {
420 	struct iio_buffer *buffer;
421 	unsigned long irq_type;
422 	bool irq_active_low;
423 	int i, err;
424 
425 	irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq));
426 
427 	switch (irq_type) {
428 	case IRQF_TRIGGER_HIGH:
429 	case IRQF_TRIGGER_RISING:
430 		irq_active_low = false;
431 		break;
432 	case IRQF_TRIGGER_LOW:
433 	case IRQF_TRIGGER_FALLING:
434 		irq_active_low = true;
435 		break;
436 	default:
437 		dev_info(hw->dev, "mode %lx unsupported\n", irq_type);
438 		return -EINVAL;
439 	}
440 
441 	err = st_lsm6dsx_write_with_mask(hw, ST_LSM6DSX_REG_HLACTIVE_ADDR,
442 					 ST_LSM6DSX_REG_HLACTIVE_MASK,
443 					 irq_active_low);
444 	if (err < 0)
445 		return err;
446 
447 	err = devm_request_threaded_irq(hw->dev, hw->irq,
448 					st_lsm6dsx_handler_irq,
449 					st_lsm6dsx_handler_thread,
450 					irq_type | IRQF_ONESHOT,
451 					"lsm6dsx", hw);
452 	if (err) {
453 		dev_err(hw->dev, "failed to request trigger irq %d\n",
454 			hw->irq);
455 		return err;
456 	}
457 
458 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
459 		buffer = devm_iio_kfifo_allocate(hw->dev);
460 		if (!buffer)
461 			return -ENOMEM;
462 
463 		iio_device_attach_buffer(hw->iio_devs[i], buffer);
464 		hw->iio_devs[i]->modes |= INDIO_BUFFER_SOFTWARE;
465 		hw->iio_devs[i]->setup_ops = &st_lsm6dsx_buffer_ops;
466 	}
467 
468 	return 0;
469 }
470