1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2020 Invensense, Inc.
6 #include <linux/kernel.h>
7 #include <linux/device.h>
8 #include <linux/mutex.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/regmap.h>
11 #include <linux/delay.h>
12 #include <linux/math64.h>
14 #include <linux/iio/buffer.h>
15 #include <linux/iio/common/inv_sensors_timestamp.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/kfifo_buf.h>
19 #include "inv_icm42600.h"
20 #include "inv_icm42600_temp.h"
21 #include "inv_icm42600_buffer.h"
23 #define INV_ICM42600_GYRO_CHAN(_modifier, _index, _ext_info) \
25 .type = IIO_ANGL_VEL, \
27 .channel2 = _modifier, \
28 .info_mask_separate = \
29 BIT(IIO_CHAN_INFO_RAW) | \
30 BIT(IIO_CHAN_INFO_CALIBBIAS), \
31 .info_mask_shared_by_type = \
32 BIT(IIO_CHAN_INFO_SCALE), \
33 .info_mask_shared_by_type_available = \
34 BIT(IIO_CHAN_INFO_SCALE) | \
35 BIT(IIO_CHAN_INFO_CALIBBIAS), \
36 .info_mask_shared_by_all = \
37 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
38 .info_mask_shared_by_all_available = \
39 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
40 .scan_index = _index, \
45 .endianness = IIO_BE, \
47 .ext_info = _ext_info, \
50 enum inv_icm42600_gyro_scan {
51 INV_ICM42600_GYRO_SCAN_X,
52 INV_ICM42600_GYRO_SCAN_Y,
53 INV_ICM42600_GYRO_SCAN_Z,
54 INV_ICM42600_GYRO_SCAN_TEMP,
55 INV_ICM42600_GYRO_SCAN_TIMESTAMP,
58 static const struct iio_chan_spec_ext_info inv_icm42600_gyro_ext_infos[] = {
59 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, inv_icm42600_get_mount_matrix),
63 static const struct iio_chan_spec inv_icm42600_gyro_channels[] = {
64 INV_ICM42600_GYRO_CHAN(IIO_MOD_X, INV_ICM42600_GYRO_SCAN_X,
65 inv_icm42600_gyro_ext_infos),
66 INV_ICM42600_GYRO_CHAN(IIO_MOD_Y, INV_ICM42600_GYRO_SCAN_Y,
67 inv_icm42600_gyro_ext_infos),
68 INV_ICM42600_GYRO_CHAN(IIO_MOD_Z, INV_ICM42600_GYRO_SCAN_Z,
69 inv_icm42600_gyro_ext_infos),
70 INV_ICM42600_TEMP_CHAN(INV_ICM42600_GYRO_SCAN_TEMP),
71 IIO_CHAN_SOFT_TIMESTAMP(INV_ICM42600_GYRO_SCAN_TIMESTAMP),
75 * IIO buffer data: size must be a power of 2 and timestamp aligned
76 * 16 bytes: 6 bytes angular velocity, 2 bytes temperature, 8 bytes timestamp
78 struct inv_icm42600_gyro_buffer {
79 struct inv_icm42600_fifo_sensor_data gyro;
81 int64_t timestamp __aligned(8);
84 #define INV_ICM42600_SCAN_MASK_GYRO_3AXIS \
85 (BIT(INV_ICM42600_GYRO_SCAN_X) | \
86 BIT(INV_ICM42600_GYRO_SCAN_Y) | \
87 BIT(INV_ICM42600_GYRO_SCAN_Z))
89 #define INV_ICM42600_SCAN_MASK_TEMP BIT(INV_ICM42600_GYRO_SCAN_TEMP)
91 static const unsigned long inv_icm42600_gyro_scan_masks[] = {
92 /* 3-axis gyro + temperature */
93 INV_ICM42600_SCAN_MASK_GYRO_3AXIS | INV_ICM42600_SCAN_MASK_TEMP,
97 /* enable gyroscope sensor and FIFO write */
98 static int inv_icm42600_gyro_update_scan_mode(struct iio_dev *indio_dev,
99 const unsigned long *scan_mask)
101 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
102 struct inv_sensors_timestamp *ts = iio_priv(indio_dev);
103 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
104 unsigned int fifo_en = 0;
105 unsigned int sleep_gyro = 0;
106 unsigned int sleep_temp = 0;
110 mutex_lock(&st->lock);
112 if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
113 /* enable temp sensor */
114 ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
117 fifo_en |= INV_ICM42600_SENSOR_TEMP;
120 if (*scan_mask & INV_ICM42600_SCAN_MASK_GYRO_3AXIS) {
121 /* enable gyro sensor */
122 conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
123 ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_gyro);
126 fifo_en |= INV_ICM42600_SENSOR_GYRO;
129 /* update data FIFO write */
130 inv_sensors_timestamp_apply_odr(ts, 0, 0, 0);
131 ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
135 ret = inv_icm42600_buffer_update_watermark(st);
138 mutex_unlock(&st->lock);
139 /* sleep maximum required time */
140 if (sleep_gyro > sleep_temp)
149 static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
150 struct iio_chan_spec const *chan,
153 struct device *dev = regmap_get_device(st->map);
154 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
159 if (chan->type != IIO_ANGL_VEL)
162 switch (chan->channel2) {
164 reg = INV_ICM42600_REG_GYRO_DATA_X;
167 reg = INV_ICM42600_REG_GYRO_DATA_Y;
170 reg = INV_ICM42600_REG_GYRO_DATA_Z;
176 pm_runtime_get_sync(dev);
177 mutex_lock(&st->lock);
179 /* enable gyro sensor */
180 conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
181 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
185 /* read gyro register data */
186 data = (__be16 *)&st->buffer[0];
187 ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
191 *val = (int16_t)be16_to_cpup(data);
192 if (*val == INV_ICM42600_DATA_INVALID)
195 mutex_unlock(&st->lock);
196 pm_runtime_mark_last_busy(dev);
197 pm_runtime_put_autosuspend(dev);
201 /* IIO format int + nano */
202 static const int inv_icm42600_gyro_scale[] = {
203 /* +/- 2000dps => 0.001065264 rad/s */
204 [2 * INV_ICM42600_GYRO_FS_2000DPS] = 0,
205 [2 * INV_ICM42600_GYRO_FS_2000DPS + 1] = 1065264,
206 /* +/- 1000dps => 0.000532632 rad/s */
207 [2 * INV_ICM42600_GYRO_FS_1000DPS] = 0,
208 [2 * INV_ICM42600_GYRO_FS_1000DPS + 1] = 532632,
209 /* +/- 500dps => 0.000266316 rad/s */
210 [2 * INV_ICM42600_GYRO_FS_500DPS] = 0,
211 [2 * INV_ICM42600_GYRO_FS_500DPS + 1] = 266316,
212 /* +/- 250dps => 0.000133158 rad/s */
213 [2 * INV_ICM42600_GYRO_FS_250DPS] = 0,
214 [2 * INV_ICM42600_GYRO_FS_250DPS + 1] = 133158,
215 /* +/- 125dps => 0.000066579 rad/s */
216 [2 * INV_ICM42600_GYRO_FS_125DPS] = 0,
217 [2 * INV_ICM42600_GYRO_FS_125DPS + 1] = 66579,
218 /* +/- 62.5dps => 0.000033290 rad/s */
219 [2 * INV_ICM42600_GYRO_FS_62_5DPS] = 0,
220 [2 * INV_ICM42600_GYRO_FS_62_5DPS + 1] = 33290,
221 /* +/- 31.25dps => 0.000016645 rad/s */
222 [2 * INV_ICM42600_GYRO_FS_31_25DPS] = 0,
223 [2 * INV_ICM42600_GYRO_FS_31_25DPS + 1] = 16645,
224 /* +/- 15.625dps => 0.000008322 rad/s */
225 [2 * INV_ICM42600_GYRO_FS_15_625DPS] = 0,
226 [2 * INV_ICM42600_GYRO_FS_15_625DPS + 1] = 8322,
229 static int inv_icm42600_gyro_read_scale(struct inv_icm42600_state *st,
234 idx = st->conf.gyro.fs;
236 *val = inv_icm42600_gyro_scale[2 * idx];
237 *val2 = inv_icm42600_gyro_scale[2 * idx + 1];
238 return IIO_VAL_INT_PLUS_NANO;
241 static int inv_icm42600_gyro_write_scale(struct inv_icm42600_state *st,
244 struct device *dev = regmap_get_device(st->map);
246 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
249 for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_scale); idx += 2) {
250 if (val == inv_icm42600_gyro_scale[idx] &&
251 val2 == inv_icm42600_gyro_scale[idx + 1])
254 if (idx >= ARRAY_SIZE(inv_icm42600_gyro_scale))
259 pm_runtime_get_sync(dev);
260 mutex_lock(&st->lock);
262 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
264 mutex_unlock(&st->lock);
265 pm_runtime_mark_last_busy(dev);
266 pm_runtime_put_autosuspend(dev);
271 /* IIO format int + micro */
272 static const int inv_icm42600_gyro_odr[] = {
291 static const int inv_icm42600_gyro_odr_conv[] = {
292 INV_ICM42600_ODR_12_5HZ,
293 INV_ICM42600_ODR_25HZ,
294 INV_ICM42600_ODR_50HZ,
295 INV_ICM42600_ODR_100HZ,
296 INV_ICM42600_ODR_200HZ,
297 INV_ICM42600_ODR_1KHZ_LN,
298 INV_ICM42600_ODR_2KHZ_LN,
299 INV_ICM42600_ODR_4KHZ_LN,
302 static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st,
308 odr = st->conf.gyro.odr;
310 for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) {
311 if (inv_icm42600_gyro_odr_conv[i] == odr)
314 if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv))
317 *val = inv_icm42600_gyro_odr[2 * i];
318 *val2 = inv_icm42600_gyro_odr[2 * i + 1];
320 return IIO_VAL_INT_PLUS_MICRO;
323 static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev,
326 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
327 struct inv_sensors_timestamp *ts = iio_priv(indio_dev);
328 struct device *dev = regmap_get_device(st->map);
330 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
333 for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_odr); idx += 2) {
334 if (val == inv_icm42600_gyro_odr[idx] &&
335 val2 == inv_icm42600_gyro_odr[idx + 1])
338 if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr))
341 conf.odr = inv_icm42600_gyro_odr_conv[idx / 2];
343 pm_runtime_get_sync(dev);
344 mutex_lock(&st->lock);
346 ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr),
347 iio_buffer_enabled(indio_dev));
351 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
354 inv_icm42600_buffer_update_fifo_period(st);
355 inv_icm42600_buffer_update_watermark(st);
358 mutex_unlock(&st->lock);
359 pm_runtime_mark_last_busy(dev);
360 pm_runtime_put_autosuspend(dev);
366 * Calibration bias values, IIO range format int + nano.
367 * Value is limited to +/-64dps coded on 12 bits signed. Step is 1/32 dps.
369 static int inv_icm42600_gyro_calibbias[] = {
370 -1, 117010721, /* min: -1.117010721 rad/s */
371 0, 545415, /* step: 0.000545415 rad/s */
372 1, 116465306, /* max: 1.116465306 rad/s */
375 static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st,
376 struct iio_chan_spec const *chan,
379 struct device *dev = regmap_get_device(st->map);
387 if (chan->type != IIO_ANGL_VEL)
390 switch (chan->channel2) {
392 reg = INV_ICM42600_REG_OFFSET_USER0;
395 reg = INV_ICM42600_REG_OFFSET_USER1;
398 reg = INV_ICM42600_REG_OFFSET_USER3;
404 pm_runtime_get_sync(dev);
405 mutex_lock(&st->lock);
407 ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data));
408 memcpy(data, st->buffer, sizeof(data));
410 mutex_unlock(&st->lock);
411 pm_runtime_mark_last_busy(dev);
412 pm_runtime_put_autosuspend(dev);
416 /* 12 bits signed value */
417 switch (chan->channel2) {
419 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
422 offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
425 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
432 * convert raw offset to dps then to rad/s
433 * 12 bits signed raw max 64 to dps: 64 / 2048
434 * dps to rad: Pi / 180
435 * result in nano (1000000000)
436 * (offset * 64 * Pi * 1000000000) / (2048 * 180)
438 val64 = (int64_t)offset * 64LL * 3141592653LL;
439 /* for rounding, add + or - divisor (2048 * 180) divided by 2 */
441 val64 += 2048 * 180 / 2;
443 val64 -= 2048 * 180 / 2;
444 bias = div_s64(val64, 2048 * 180);
445 *val = bias / 1000000000L;
446 *val2 = bias % 1000000000L;
448 return IIO_VAL_INT_PLUS_NANO;
451 static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st,
452 struct iio_chan_spec const *chan,
455 struct device *dev = regmap_get_device(st->map);
456 int64_t val64, min, max;
457 unsigned int reg, regval;
461 if (chan->type != IIO_ANGL_VEL)
464 switch (chan->channel2) {
466 reg = INV_ICM42600_REG_OFFSET_USER0;
469 reg = INV_ICM42600_REG_OFFSET_USER1;
472 reg = INV_ICM42600_REG_OFFSET_USER3;
478 /* inv_icm42600_gyro_calibbias: min - step - max in nano */
479 min = (int64_t)inv_icm42600_gyro_calibbias[0] * 1000000000LL +
480 (int64_t)inv_icm42600_gyro_calibbias[1];
481 max = (int64_t)inv_icm42600_gyro_calibbias[4] * 1000000000LL +
482 (int64_t)inv_icm42600_gyro_calibbias[5];
483 val64 = (int64_t)val * 1000000000LL + (int64_t)val2;
484 if (val64 < min || val64 > max)
488 * convert rad/s to dps then to raw value
489 * rad to dps: 180 / Pi
490 * dps to raw 12 bits signed, max 64: 2048 / 64
491 * val in nano (1000000000)
492 * val * 180 * 2048 / (Pi * 1000000000 * 64)
494 val64 = val64 * 180LL * 2048LL;
495 /* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */
497 val64 += 3141592653LL * 64LL / 2LL;
499 val64 -= 3141592653LL * 64LL / 2LL;
500 offset = div64_s64(val64, 3141592653LL * 64LL);
502 /* clamp value limited to 12 bits signed */
505 else if (offset > 2047)
508 pm_runtime_get_sync(dev);
509 mutex_lock(&st->lock);
511 switch (chan->channel2) {
513 /* OFFSET_USER1 register is shared */
514 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
518 st->buffer[0] = offset & 0xFF;
519 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
522 /* OFFSET_USER1 register is shared */
523 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
527 st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
528 st->buffer[1] = offset & 0xFF;
531 /* OFFSET_USER4 register is shared */
532 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
536 st->buffer[0] = offset & 0xFF;
537 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
544 ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
547 mutex_unlock(&st->lock);
548 pm_runtime_mark_last_busy(dev);
549 pm_runtime_put_autosuspend(dev);
553 static int inv_icm42600_gyro_read_raw(struct iio_dev *indio_dev,
554 struct iio_chan_spec const *chan,
555 int *val, int *val2, long mask)
557 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
561 switch (chan->type) {
565 return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
571 case IIO_CHAN_INFO_RAW:
572 ret = iio_device_claim_direct_mode(indio_dev);
575 ret = inv_icm42600_gyro_read_sensor(st, chan, &data);
576 iio_device_release_direct_mode(indio_dev);
581 case IIO_CHAN_INFO_SCALE:
582 return inv_icm42600_gyro_read_scale(st, val, val2);
583 case IIO_CHAN_INFO_SAMP_FREQ:
584 return inv_icm42600_gyro_read_odr(st, val, val2);
585 case IIO_CHAN_INFO_CALIBBIAS:
586 return inv_icm42600_gyro_read_offset(st, chan, val, val2);
592 static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev,
593 struct iio_chan_spec const *chan,
595 int *type, int *length, long mask)
597 if (chan->type != IIO_ANGL_VEL)
601 case IIO_CHAN_INFO_SCALE:
602 *vals = inv_icm42600_gyro_scale;
603 *type = IIO_VAL_INT_PLUS_NANO;
604 *length = ARRAY_SIZE(inv_icm42600_gyro_scale);
605 return IIO_AVAIL_LIST;
606 case IIO_CHAN_INFO_SAMP_FREQ:
607 *vals = inv_icm42600_gyro_odr;
608 *type = IIO_VAL_INT_PLUS_MICRO;
609 *length = ARRAY_SIZE(inv_icm42600_gyro_odr);
610 return IIO_AVAIL_LIST;
611 case IIO_CHAN_INFO_CALIBBIAS:
612 *vals = inv_icm42600_gyro_calibbias;
613 *type = IIO_VAL_INT_PLUS_NANO;
614 return IIO_AVAIL_RANGE;
620 static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev,
621 struct iio_chan_spec const *chan,
622 int val, int val2, long mask)
624 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
627 if (chan->type != IIO_ANGL_VEL)
631 case IIO_CHAN_INFO_SCALE:
632 ret = iio_device_claim_direct_mode(indio_dev);
635 ret = inv_icm42600_gyro_write_scale(st, val, val2);
636 iio_device_release_direct_mode(indio_dev);
638 case IIO_CHAN_INFO_SAMP_FREQ:
639 return inv_icm42600_gyro_write_odr(indio_dev, val, val2);
640 case IIO_CHAN_INFO_CALIBBIAS:
641 ret = iio_device_claim_direct_mode(indio_dev);
644 ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
645 iio_device_release_direct_mode(indio_dev);
652 static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
653 struct iio_chan_spec const *chan,
656 if (chan->type != IIO_ANGL_VEL)
660 case IIO_CHAN_INFO_SCALE:
661 return IIO_VAL_INT_PLUS_NANO;
662 case IIO_CHAN_INFO_SAMP_FREQ:
663 return IIO_VAL_INT_PLUS_MICRO;
664 case IIO_CHAN_INFO_CALIBBIAS:
665 return IIO_VAL_INT_PLUS_NANO;
671 static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
674 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
677 mutex_lock(&st->lock);
679 st->fifo.watermark.gyro = val;
680 ret = inv_icm42600_buffer_update_watermark(st);
682 mutex_unlock(&st->lock);
687 static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
690 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
696 mutex_lock(&st->lock);
698 ret = inv_icm42600_buffer_hwfifo_flush(st, count);
700 ret = st->fifo.nb.gyro;
702 mutex_unlock(&st->lock);
707 static const struct iio_info inv_icm42600_gyro_info = {
708 .read_raw = inv_icm42600_gyro_read_raw,
709 .read_avail = inv_icm42600_gyro_read_avail,
710 .write_raw = inv_icm42600_gyro_write_raw,
711 .write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt,
712 .debugfs_reg_access = inv_icm42600_debugfs_reg,
713 .update_scan_mode = inv_icm42600_gyro_update_scan_mode,
714 .hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark,
715 .hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush,
718 struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
720 struct device *dev = regmap_get_device(st->map);
722 struct inv_sensors_timestamp_chip ts_chip;
723 struct inv_sensors_timestamp *ts;
724 struct iio_dev *indio_dev;
727 name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
729 return ERR_PTR(-ENOMEM);
731 indio_dev = devm_iio_device_alloc(dev, sizeof(*ts));
733 return ERR_PTR(-ENOMEM);
736 * clock period is 32kHz (31250ns)
737 * jitter is +/- 2% (20 per mille)
739 ts_chip.clock_period = 31250;
741 ts_chip.init_period = inv_icm42600_odr_to_period(st->conf.accel.odr);
742 ts = iio_priv(indio_dev);
743 inv_sensors_timestamp_init(ts, &ts_chip);
745 iio_device_set_drvdata(indio_dev, st);
746 indio_dev->name = name;
747 indio_dev->info = &inv_icm42600_gyro_info;
748 indio_dev->modes = INDIO_DIRECT_MODE;
749 indio_dev->channels = inv_icm42600_gyro_channels;
750 indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels);
751 indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks;
752 indio_dev->setup_ops = &inv_icm42600_buffer_ops;
754 ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
755 &inv_icm42600_buffer_ops);
759 ret = devm_iio_device_register(dev, indio_dev);
766 int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev)
768 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
769 struct inv_sensors_timestamp *ts = iio_priv(indio_dev);
772 const void *accel, *gyro, *timestamp;
776 struct inv_icm42600_gyro_buffer buffer;
778 /* parse all fifo packets */
779 for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) {
780 size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
781 &accel, &gyro, &temp, ×tamp, &odr);
782 /* quit if error or FIFO is empty */
786 /* skip packet if no gyro data or data is invalid */
787 if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
791 if (odr & INV_ICM42600_SENSOR_GYRO)
792 inv_sensors_timestamp_apply_odr(ts, st->fifo.period,
793 st->fifo.nb.total, no);
795 /* buffer is copied to userspace, zeroing it to avoid any data leak */
796 memset(&buffer, 0, sizeof(buffer));
797 memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro));
798 /* convert 8 bits FIFO temperature in high resolution format */
799 buffer.temp = temp ? (*temp * 64) : 0;
800 ts_val = inv_sensors_timestamp_pop(ts);
801 iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val);