Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / imu / st_lsm6dsx / st_lsm6dsx_buffer.c
blob631360b14ca71c398e67a9425dff363de1eb255a
1 /*
2 * STMicroelectronics st_lsm6dsx FIFO buffer library driver
4 * LSM6DS3/LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC: The FIFO buffer can be
5 * configured to store data from gyroscope and accelerometer. Samples are
6 * queued without any tag according to a specific pattern based on
7 * 'FIFO data sets' (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
20 * Copyright 2016 STMicroelectronics Inc.
22 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
23 * Denis Ciocca <denis.ciocca@st.com>
25 * Licensed under the GPL-2.
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 #include <linux/regmap.h>
34 #include <linux/bitfield.h>
36 #include <linux/platform_data/st_sensors_pdata.h>
38 #include "st_lsm6dsx.h"
40 #define ST_LSM6DSX_REG_HLACTIVE_ADDR 0x12
41 #define ST_LSM6DSX_REG_HLACTIVE_MASK BIT(5)
42 #define ST_LSM6DSX_REG_PP_OD_ADDR 0x12
43 #define ST_LSM6DSX_REG_PP_OD_MASK BIT(4)
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_TS_RESET_ADDR 0x42
51 #define ST_LSM6DSX_MAX_FIFO_ODR_VAL 0x08
53 #define ST_LSM6DSX_TS_SENSITIVITY 25000UL /* 25us */
54 #define ST_LSM6DSX_TS_RESET_VAL 0xaa
56 struct st_lsm6dsx_decimator_entry {
57 u8 decimator;
58 u8 val;
61 static const
62 struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = {
63 { 0, 0x0 },
64 { 1, 0x1 },
65 { 2, 0x2 },
66 { 3, 0x3 },
67 { 4, 0x4 },
68 { 8, 0x5 },
69 { 16, 0x6 },
70 { 32, 0x7 },
73 static int st_lsm6dsx_get_decimator_val(u8 val)
75 const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
76 int i;
78 for (i = 0; i < max_size; i++)
79 if (st_lsm6dsx_decimator_table[i].decimator == val)
80 break;
82 return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
85 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
86 u16 *max_odr, u16 *min_odr)
88 struct st_lsm6dsx_sensor *sensor;
89 int i;
91 *max_odr = 0, *min_odr = ~0;
92 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
93 sensor = iio_priv(hw->iio_devs[i]);
95 if (!(hw->enable_mask & BIT(sensor->id)))
96 continue;
98 *max_odr = max_t(u16, *max_odr, sensor->odr);
99 *min_odr = min_t(u16, *min_odr, sensor->odr);
103 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
105 u16 max_odr, min_odr, sip = 0, ts_sip = 0;
106 const struct st_lsm6dsx_reg *ts_dec_reg;
107 struct st_lsm6dsx_sensor *sensor;
108 int err = 0, i;
109 u8 data;
111 st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr);
113 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
114 const struct st_lsm6dsx_reg *dec_reg;
116 sensor = iio_priv(hw->iio_devs[i]);
117 /* update fifo decimators and sample in pattern */
118 if (hw->enable_mask & BIT(sensor->id)) {
119 sensor->sip = sensor->odr / min_odr;
120 sensor->decimator = max_odr / sensor->odr;
121 data = st_lsm6dsx_get_decimator_val(sensor->decimator);
122 } else {
123 sensor->sip = 0;
124 sensor->decimator = 0;
125 data = 0;
127 ts_sip = max_t(u16, ts_sip, sensor->sip);
129 dec_reg = &hw->settings->decimator[sensor->id];
130 if (dec_reg->addr) {
131 int val = ST_LSM6DSX_SHIFT_VAL(data, dec_reg->mask);
133 err = regmap_update_bits(hw->regmap, dec_reg->addr,
134 dec_reg->mask, val);
135 if (err < 0)
136 return err;
138 sip += sensor->sip;
140 hw->sip = sip + ts_sip;
141 hw->ts_sip = ts_sip;
144 * update hw ts decimator if necessary. Decimator for hw timestamp
145 * is always 1 or 0 in order to have a ts sample for each data
146 * sample in FIFO
148 ts_dec_reg = &hw->settings->ts_settings.decimator;
149 if (ts_dec_reg->addr) {
150 int val, ts_dec = !!hw->ts_sip;
152 val = ST_LSM6DSX_SHIFT_VAL(ts_dec, ts_dec_reg->mask);
153 err = regmap_update_bits(hw->regmap, ts_dec_reg->addr,
154 ts_dec_reg->mask, val);
156 return err;
159 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
160 enum st_lsm6dsx_fifo_mode fifo_mode)
162 int err;
164 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_FIFO_MODE_ADDR,
165 ST_LSM6DSX_FIFO_MODE_MASK,
166 FIELD_PREP(ST_LSM6DSX_FIFO_MODE_MASK,
167 fifo_mode));
168 if (err < 0)
169 return err;
171 hw->fifo_mode = fifo_mode;
173 return 0;
176 static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor,
177 bool enable)
179 struct st_lsm6dsx_hw *hw = sensor->hw;
180 u8 data;
182 data = hw->enable_mask ? ST_LSM6DSX_MAX_FIFO_ODR_VAL : 0;
183 return regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_FIFO_MODE_ADDR,
184 ST_LSM6DSX_FIFO_ODR_MASK,
185 FIELD_PREP(ST_LSM6DSX_FIFO_ODR_MASK, data));
188 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark)
190 u16 fifo_watermark = ~0, cur_watermark, fifo_th_mask;
191 struct st_lsm6dsx_hw *hw = sensor->hw;
192 struct st_lsm6dsx_sensor *cur_sensor;
193 int i, err, data;
194 __le16 wdata;
196 if (!hw->sip)
197 return 0;
199 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
200 cur_sensor = iio_priv(hw->iio_devs[i]);
202 if (!(hw->enable_mask & BIT(cur_sensor->id)))
203 continue;
205 cur_watermark = (cur_sensor == sensor) ? watermark
206 : cur_sensor->watermark;
208 fifo_watermark = min_t(u16, fifo_watermark, cur_watermark);
211 fifo_watermark = max_t(u16, fifo_watermark, hw->sip);
212 fifo_watermark = (fifo_watermark / hw->sip) * hw->sip;
213 fifo_watermark = fifo_watermark * hw->settings->fifo_ops.th_wl;
215 err = regmap_read(hw->regmap, hw->settings->fifo_ops.fifo_th.addr + 1,
216 &data);
217 if (err < 0)
218 return err;
220 fifo_th_mask = hw->settings->fifo_ops.fifo_th.mask;
221 fifo_watermark = ((data << 8) & ~fifo_th_mask) |
222 (fifo_watermark & fifo_th_mask);
224 wdata = cpu_to_le16(fifo_watermark);
225 return regmap_bulk_write(hw->regmap,
226 hw->settings->fifo_ops.fifo_th.addr,
227 &wdata, sizeof(wdata));
230 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw *hw)
232 struct st_lsm6dsx_sensor *sensor;
233 int i, err;
235 /* reset hw ts counter */
236 err = regmap_write(hw->regmap, ST_LSM6DSX_REG_TS_RESET_ADDR,
237 ST_LSM6DSX_TS_RESET_VAL);
238 if (err < 0)
239 return err;
241 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
242 sensor = iio_priv(hw->iio_devs[i]);
244 * store enable buffer timestamp as reference for
245 * hw timestamp
247 sensor->ts_ref = iio_get_time_ns(hw->iio_devs[i]);
249 return 0;
253 * Set max bulk read to ST_LSM6DSX_MAX_WORD_LEN in order to avoid
254 * a kmalloc for each bus access
256 static inline int st_lsm6dsx_read_block(struct st_lsm6dsx_hw *hw, u8 *data,
257 unsigned int data_len)
259 unsigned int word_len, read_len = 0;
260 int err;
262 while (read_len < data_len) {
263 word_len = min_t(unsigned int, data_len - read_len,
264 ST_LSM6DSX_MAX_WORD_LEN);
265 err = regmap_bulk_read(hw->regmap,
266 ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
267 data + read_len, word_len);
268 if (err < 0)
269 return err;
270 read_len += word_len;
272 return 0;
275 #define ST_LSM6DSX_IIO_BUFF_SIZE (ALIGN(ST_LSM6DSX_SAMPLE_SIZE, \
276 sizeof(s64)) + sizeof(s64))
278 * st_lsm6dsx_read_fifo() - hw FIFO read routine
279 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
281 * Read samples from the hw FIFO and push them to IIO buffers.
283 * Return: Number of bytes read from the FIFO
285 static int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
287 u16 fifo_len, pattern_len = hw->sip * ST_LSM6DSX_SAMPLE_SIZE;
288 u16 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
289 int err, acc_sip, gyro_sip, ts_sip, read_len, offset;
290 struct st_lsm6dsx_sensor *acc_sensor, *gyro_sensor;
291 u8 gyro_buff[ST_LSM6DSX_IIO_BUFF_SIZE];
292 u8 acc_buff[ST_LSM6DSX_IIO_BUFF_SIZE];
293 bool reset_ts = false;
294 __le16 fifo_status;
295 s64 ts = 0;
297 err = regmap_bulk_read(hw->regmap,
298 hw->settings->fifo_ops.fifo_diff.addr,
299 &fifo_status, sizeof(fifo_status));
300 if (err < 0) {
301 dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
302 err);
303 return err;
306 if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
307 return 0;
309 fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
310 ST_LSM6DSX_CHAN_SIZE;
311 fifo_len = (fifo_len / pattern_len) * pattern_len;
313 acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
314 gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
316 for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
317 err = st_lsm6dsx_read_block(hw, hw->buff, pattern_len);
318 if (err < 0) {
319 dev_err(hw->dev,
320 "failed to read pattern from fifo (err=%d)\n",
321 err);
322 return err;
326 * Data are written to the FIFO with a specific pattern
327 * depending on the configured ODRs. The first sequence of data
328 * stored in FIFO contains the data of all enabled sensors
329 * (e.g. Gx, Gy, Gz, Ax, Ay, Az, Ts), then data are repeated
330 * depending on the value of the decimation factor set for each
331 * sensor.
333 * Supposing the FIFO is storing data from gyroscope and
334 * accelerometer at different ODRs:
335 * - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
336 * Since the gyroscope ODR is twice the accelerometer one, the
337 * following pattern is repeated every 9 samples:
338 * - Gx, Gy, Gz, Ax, Ay, Az, Ts, Gx, Gy, Gz, Ts, Gx, ..
340 gyro_sip = gyro_sensor->sip;
341 acc_sip = acc_sensor->sip;
342 ts_sip = hw->ts_sip;
343 offset = 0;
345 while (acc_sip > 0 || gyro_sip > 0) {
346 if (gyro_sip > 0) {
347 memcpy(gyro_buff, &hw->buff[offset],
348 ST_LSM6DSX_SAMPLE_SIZE);
349 offset += ST_LSM6DSX_SAMPLE_SIZE;
351 if (acc_sip > 0) {
352 memcpy(acc_buff, &hw->buff[offset],
353 ST_LSM6DSX_SAMPLE_SIZE);
354 offset += ST_LSM6DSX_SAMPLE_SIZE;
357 if (ts_sip-- > 0) {
358 u8 data[ST_LSM6DSX_SAMPLE_SIZE];
360 memcpy(data, &hw->buff[offset], sizeof(data));
362 * hw timestamp is 3B long and it is stored
363 * in FIFO using 6B as 4th FIFO data set
364 * according to this schema:
365 * B0 = ts[15:8], B1 = ts[23:16], B3 = ts[7:0]
367 ts = data[1] << 16 | data[0] << 8 | data[3];
369 * check if hw timestamp engine is going to
370 * reset (the sensor generates an interrupt
371 * to signal the hw timestamp will reset in
372 * 1.638s)
374 if (!reset_ts && ts >= 0xff0000)
375 reset_ts = true;
376 ts *= ST_LSM6DSX_TS_SENSITIVITY;
378 offset += ST_LSM6DSX_SAMPLE_SIZE;
381 if (gyro_sip-- > 0)
382 iio_push_to_buffers_with_timestamp(
383 hw->iio_devs[ST_LSM6DSX_ID_GYRO],
384 gyro_buff, gyro_sensor->ts_ref + ts);
385 if (acc_sip-- > 0)
386 iio_push_to_buffers_with_timestamp(
387 hw->iio_devs[ST_LSM6DSX_ID_ACC],
388 acc_buff, acc_sensor->ts_ref + ts);
392 if (unlikely(reset_ts)) {
393 err = st_lsm6dsx_reset_hw_ts(hw);
394 if (err < 0) {
395 dev_err(hw->dev, "failed to reset hw ts (err=%d)\n",
396 err);
397 return err;
400 return read_len;
403 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
405 int err;
407 mutex_lock(&hw->fifo_lock);
409 st_lsm6dsx_read_fifo(hw);
410 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
412 mutex_unlock(&hw->fifo_lock);
414 return err;
417 static int st_lsm6dsx_update_fifo(struct iio_dev *iio_dev, bool enable)
419 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
420 struct st_lsm6dsx_hw *hw = sensor->hw;
421 int err;
423 mutex_lock(&hw->conf_lock);
425 if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS) {
426 err = st_lsm6dsx_flush_fifo(hw);
427 if (err < 0)
428 goto out;
431 if (enable) {
432 err = st_lsm6dsx_sensor_enable(sensor);
433 if (err < 0)
434 goto out;
435 } else {
436 err = st_lsm6dsx_sensor_disable(sensor);
437 if (err < 0)
438 goto out;
441 err = st_lsm6dsx_set_fifo_odr(sensor, enable);
442 if (err < 0)
443 goto out;
445 err = st_lsm6dsx_update_decimators(hw);
446 if (err < 0)
447 goto out;
449 err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
450 if (err < 0)
451 goto out;
453 if (hw->enable_mask) {
454 /* reset hw ts counter */
455 err = st_lsm6dsx_reset_hw_ts(hw);
456 if (err < 0)
457 goto out;
459 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
462 out:
463 mutex_unlock(&hw->conf_lock);
465 return err;
468 static irqreturn_t st_lsm6dsx_handler_irq(int irq, void *private)
470 struct st_lsm6dsx_hw *hw = private;
472 return hw->sip > 0 ? IRQ_WAKE_THREAD : IRQ_NONE;
475 static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
477 struct st_lsm6dsx_hw *hw = private;
478 int count;
480 mutex_lock(&hw->fifo_lock);
481 count = st_lsm6dsx_read_fifo(hw);
482 mutex_unlock(&hw->fifo_lock);
484 return !count ? IRQ_NONE : IRQ_HANDLED;
487 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
489 return st_lsm6dsx_update_fifo(iio_dev, true);
492 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
494 return st_lsm6dsx_update_fifo(iio_dev, false);
497 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
498 .preenable = st_lsm6dsx_buffer_preenable,
499 .postdisable = st_lsm6dsx_buffer_postdisable,
502 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
504 struct device_node *np = hw->dev->of_node;
505 struct st_sensors_platform_data *pdata;
506 struct iio_buffer *buffer;
507 unsigned long irq_type;
508 bool irq_active_low;
509 int i, err;
511 irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq));
513 switch (irq_type) {
514 case IRQF_TRIGGER_HIGH:
515 case IRQF_TRIGGER_RISING:
516 irq_active_low = false;
517 break;
518 case IRQF_TRIGGER_LOW:
519 case IRQF_TRIGGER_FALLING:
520 irq_active_low = true;
521 break;
522 default:
523 dev_info(hw->dev, "mode %lx unsupported\n", irq_type);
524 return -EINVAL;
527 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_HLACTIVE_ADDR,
528 ST_LSM6DSX_REG_HLACTIVE_MASK,
529 FIELD_PREP(ST_LSM6DSX_REG_HLACTIVE_MASK,
530 irq_active_low));
531 if (err < 0)
532 return err;
534 pdata = (struct st_sensors_platform_data *)hw->dev->platform_data;
535 if ((np && of_property_read_bool(np, "drive-open-drain")) ||
536 (pdata && pdata->open_drain)) {
537 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_PP_OD_ADDR,
538 ST_LSM6DSX_REG_PP_OD_MASK,
539 FIELD_PREP(ST_LSM6DSX_REG_PP_OD_MASK,
540 1));
541 if (err < 0)
542 return err;
544 irq_type |= IRQF_SHARED;
547 err = devm_request_threaded_irq(hw->dev, hw->irq,
548 st_lsm6dsx_handler_irq,
549 st_lsm6dsx_handler_thread,
550 irq_type | IRQF_ONESHOT,
551 "lsm6dsx", hw);
552 if (err) {
553 dev_err(hw->dev, "failed to request trigger irq %d\n",
554 hw->irq);
555 return err;
558 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
559 buffer = devm_iio_kfifo_allocate(hw->dev);
560 if (!buffer)
561 return -ENOMEM;
563 iio_device_attach_buffer(hw->iio_devs[i], buffer);
564 hw->iio_devs[i]->modes |= INDIO_BUFFER_SOFTWARE;
565 hw->iio_devs[i]->setup_ops = &st_lsm6dsx_buffer_ops;
568 return 0;