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