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_icm42600_sensor_conf conf
= INV_ICM42600_SENSOR_CONF_INIT
;
103 unsigned int fifo_en
= 0;
104 unsigned int sleep_gyro
= 0;
105 unsigned int sleep_temp
= 0;
109 mutex_lock(&st
->lock
);
111 if (*scan_mask
& INV_ICM42600_SCAN_MASK_TEMP
) {
112 /* enable temp sensor */
113 ret
= inv_icm42600_set_temp_conf(st
, true, &sleep_temp
);
116 fifo_en
|= INV_ICM42600_SENSOR_TEMP
;
119 if (*scan_mask
& INV_ICM42600_SCAN_MASK_GYRO_3AXIS
) {
120 /* enable gyro sensor */
121 conf
.mode
= INV_ICM42600_SENSOR_MODE_LOW_NOISE
;
122 ret
= inv_icm42600_set_gyro_conf(st
, &conf
, &sleep_gyro
);
125 fifo_en
|= INV_ICM42600_SENSOR_GYRO
;
128 /* update data FIFO write */
129 ret
= inv_icm42600_buffer_set_fifo_en(st
, fifo_en
| st
->fifo
.en
);
132 mutex_unlock(&st
->lock
);
133 /* sleep maximum required time */
134 sleep
= max(sleep_gyro
, sleep_temp
);
140 static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state
*st
,
141 struct iio_chan_spec
const *chan
,
144 struct device
*dev
= regmap_get_device(st
->map
);
145 struct inv_icm42600_sensor_conf conf
= INV_ICM42600_SENSOR_CONF_INIT
;
150 if (chan
->type
!= IIO_ANGL_VEL
)
153 switch (chan
->channel2
) {
155 reg
= INV_ICM42600_REG_GYRO_DATA_X
;
158 reg
= INV_ICM42600_REG_GYRO_DATA_Y
;
161 reg
= INV_ICM42600_REG_GYRO_DATA_Z
;
167 pm_runtime_get_sync(dev
);
168 mutex_lock(&st
->lock
);
170 /* enable gyro sensor */
171 conf
.mode
= INV_ICM42600_SENSOR_MODE_LOW_NOISE
;
172 ret
= inv_icm42600_set_gyro_conf(st
, &conf
, NULL
);
176 /* read gyro register data */
177 data
= (__be16
*)&st
->buffer
[0];
178 ret
= regmap_bulk_read(st
->map
, reg
, data
, sizeof(*data
));
182 *val
= (int16_t)be16_to_cpup(data
);
183 if (*val
== INV_ICM42600_DATA_INVALID
)
186 mutex_unlock(&st
->lock
);
187 pm_runtime_mark_last_busy(dev
);
188 pm_runtime_put_autosuspend(dev
);
192 /* IIO format int + nano */
193 static const int inv_icm42600_gyro_scale
[] = {
194 /* +/- 2000dps => 0.001065264 rad/s */
195 [2 * INV_ICM42600_GYRO_FS_2000DPS
] = 0,
196 [2 * INV_ICM42600_GYRO_FS_2000DPS
+ 1] = 1065264,
197 /* +/- 1000dps => 0.000532632 rad/s */
198 [2 * INV_ICM42600_GYRO_FS_1000DPS
] = 0,
199 [2 * INV_ICM42600_GYRO_FS_1000DPS
+ 1] = 532632,
200 /* +/- 500dps => 0.000266316 rad/s */
201 [2 * INV_ICM42600_GYRO_FS_500DPS
] = 0,
202 [2 * INV_ICM42600_GYRO_FS_500DPS
+ 1] = 266316,
203 /* +/- 250dps => 0.000133158 rad/s */
204 [2 * INV_ICM42600_GYRO_FS_250DPS
] = 0,
205 [2 * INV_ICM42600_GYRO_FS_250DPS
+ 1] = 133158,
206 /* +/- 125dps => 0.000066579 rad/s */
207 [2 * INV_ICM42600_GYRO_FS_125DPS
] = 0,
208 [2 * INV_ICM42600_GYRO_FS_125DPS
+ 1] = 66579,
209 /* +/- 62.5dps => 0.000033290 rad/s */
210 [2 * INV_ICM42600_GYRO_FS_62_5DPS
] = 0,
211 [2 * INV_ICM42600_GYRO_FS_62_5DPS
+ 1] = 33290,
212 /* +/- 31.25dps => 0.000016645 rad/s */
213 [2 * INV_ICM42600_GYRO_FS_31_25DPS
] = 0,
214 [2 * INV_ICM42600_GYRO_FS_31_25DPS
+ 1] = 16645,
215 /* +/- 15.625dps => 0.000008322 rad/s */
216 [2 * INV_ICM42600_GYRO_FS_15_625DPS
] = 0,
217 [2 * INV_ICM42600_GYRO_FS_15_625DPS
+ 1] = 8322,
219 static const int inv_icm42686_gyro_scale
[] = {
220 /* +/- 4000dps => 0.002130529 rad/s */
221 [2 * INV_ICM42686_GYRO_FS_4000DPS
] = 0,
222 [2 * INV_ICM42686_GYRO_FS_4000DPS
+ 1] = 2130529,
223 /* +/- 2000dps => 0.001065264 rad/s */
224 [2 * INV_ICM42686_GYRO_FS_2000DPS
] = 0,
225 [2 * INV_ICM42686_GYRO_FS_2000DPS
+ 1] = 1065264,
226 /* +/- 1000dps => 0.000532632 rad/s */
227 [2 * INV_ICM42686_GYRO_FS_1000DPS
] = 0,
228 [2 * INV_ICM42686_GYRO_FS_1000DPS
+ 1] = 532632,
229 /* +/- 500dps => 0.000266316 rad/s */
230 [2 * INV_ICM42686_GYRO_FS_500DPS
] = 0,
231 [2 * INV_ICM42686_GYRO_FS_500DPS
+ 1] = 266316,
232 /* +/- 250dps => 0.000133158 rad/s */
233 [2 * INV_ICM42686_GYRO_FS_250DPS
] = 0,
234 [2 * INV_ICM42686_GYRO_FS_250DPS
+ 1] = 133158,
235 /* +/- 125dps => 0.000066579 rad/s */
236 [2 * INV_ICM42686_GYRO_FS_125DPS
] = 0,
237 [2 * INV_ICM42686_GYRO_FS_125DPS
+ 1] = 66579,
238 /* +/- 62.5dps => 0.000033290 rad/s */
239 [2 * INV_ICM42686_GYRO_FS_62_5DPS
] = 0,
240 [2 * INV_ICM42686_GYRO_FS_62_5DPS
+ 1] = 33290,
241 /* +/- 31.25dps => 0.000016645 rad/s */
242 [2 * INV_ICM42686_GYRO_FS_31_25DPS
] = 0,
243 [2 * INV_ICM42686_GYRO_FS_31_25DPS
+ 1] = 16645,
246 static int inv_icm42600_gyro_read_scale(struct iio_dev
*indio_dev
,
249 struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
250 struct inv_icm42600_sensor_state
*gyro_st
= iio_priv(indio_dev
);
253 idx
= st
->conf
.gyro
.fs
;
255 *val
= gyro_st
->scales
[2 * idx
];
256 *val2
= gyro_st
->scales
[2 * idx
+ 1];
257 return IIO_VAL_INT_PLUS_NANO
;
260 static int inv_icm42600_gyro_write_scale(struct iio_dev
*indio_dev
,
263 struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
264 struct inv_icm42600_sensor_state
*gyro_st
= iio_priv(indio_dev
);
265 struct device
*dev
= regmap_get_device(st
->map
);
267 struct inv_icm42600_sensor_conf conf
= INV_ICM42600_SENSOR_CONF_INIT
;
270 for (idx
= 0; idx
< gyro_st
->scales_len
; idx
+= 2) {
271 if (val
== gyro_st
->scales
[idx
] &&
272 val2
== gyro_st
->scales
[idx
+ 1])
275 if (idx
>= gyro_st
->scales_len
)
280 pm_runtime_get_sync(dev
);
281 mutex_lock(&st
->lock
);
283 ret
= inv_icm42600_set_gyro_conf(st
, &conf
, NULL
);
285 mutex_unlock(&st
->lock
);
286 pm_runtime_mark_last_busy(dev
);
287 pm_runtime_put_autosuspend(dev
);
292 /* IIO format int + micro */
293 static const int inv_icm42600_gyro_odr
[] = {
312 static const int inv_icm42600_gyro_odr_conv
[] = {
313 INV_ICM42600_ODR_12_5HZ
,
314 INV_ICM42600_ODR_25HZ
,
315 INV_ICM42600_ODR_50HZ
,
316 INV_ICM42600_ODR_100HZ
,
317 INV_ICM42600_ODR_200HZ
,
318 INV_ICM42600_ODR_1KHZ_LN
,
319 INV_ICM42600_ODR_2KHZ_LN
,
320 INV_ICM42600_ODR_4KHZ_LN
,
323 static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state
*st
,
329 odr
= st
->conf
.gyro
.odr
;
331 for (i
= 0; i
< ARRAY_SIZE(inv_icm42600_gyro_odr_conv
); ++i
) {
332 if (inv_icm42600_gyro_odr_conv
[i
] == odr
)
335 if (i
>= ARRAY_SIZE(inv_icm42600_gyro_odr_conv
))
338 *val
= inv_icm42600_gyro_odr
[2 * i
];
339 *val2
= inv_icm42600_gyro_odr
[2 * i
+ 1];
341 return IIO_VAL_INT_PLUS_MICRO
;
344 static int inv_icm42600_gyro_write_odr(struct iio_dev
*indio_dev
,
347 struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
348 struct inv_icm42600_sensor_state
*gyro_st
= iio_priv(indio_dev
);
349 struct inv_sensors_timestamp
*ts
= &gyro_st
->ts
;
350 struct device
*dev
= regmap_get_device(st
->map
);
352 struct inv_icm42600_sensor_conf conf
= INV_ICM42600_SENSOR_CONF_INIT
;
355 for (idx
= 0; idx
< ARRAY_SIZE(inv_icm42600_gyro_odr
); idx
+= 2) {
356 if (val
== inv_icm42600_gyro_odr
[idx
] &&
357 val2
== inv_icm42600_gyro_odr
[idx
+ 1])
360 if (idx
>= ARRAY_SIZE(inv_icm42600_gyro_odr
))
363 conf
.odr
= inv_icm42600_gyro_odr_conv
[idx
/ 2];
365 pm_runtime_get_sync(dev
);
366 mutex_lock(&st
->lock
);
368 ret
= inv_sensors_timestamp_update_odr(ts
, inv_icm42600_odr_to_period(conf
.odr
),
369 iio_buffer_enabled(indio_dev
));
373 ret
= inv_icm42600_set_gyro_conf(st
, &conf
, NULL
);
376 inv_icm42600_buffer_update_fifo_period(st
);
377 inv_icm42600_buffer_update_watermark(st
);
380 mutex_unlock(&st
->lock
);
381 pm_runtime_mark_last_busy(dev
);
382 pm_runtime_put_autosuspend(dev
);
388 * Calibration bias values, IIO range format int + nano.
389 * Value is limited to +/-64dps coded on 12 bits signed. Step is 1/32 dps.
391 static int inv_icm42600_gyro_calibbias
[] = {
392 -1, 117010721, /* min: -1.117010721 rad/s */
393 0, 545415, /* step: 0.000545415 rad/s */
394 1, 116465306, /* max: 1.116465306 rad/s */
397 static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state
*st
,
398 struct iio_chan_spec
const *chan
,
401 struct device
*dev
= regmap_get_device(st
->map
);
409 if (chan
->type
!= IIO_ANGL_VEL
)
412 switch (chan
->channel2
) {
414 reg
= INV_ICM42600_REG_OFFSET_USER0
;
417 reg
= INV_ICM42600_REG_OFFSET_USER1
;
420 reg
= INV_ICM42600_REG_OFFSET_USER3
;
426 pm_runtime_get_sync(dev
);
427 mutex_lock(&st
->lock
);
429 ret
= regmap_bulk_read(st
->map
, reg
, st
->buffer
, sizeof(data
));
430 memcpy(data
, st
->buffer
, sizeof(data
));
432 mutex_unlock(&st
->lock
);
433 pm_runtime_mark_last_busy(dev
);
434 pm_runtime_put_autosuspend(dev
);
438 /* 12 bits signed value */
439 switch (chan
->channel2
) {
441 offset
= sign_extend32(((data
[1] & 0x0F) << 8) | data
[0], 11);
444 offset
= sign_extend32(((data
[0] & 0xF0) << 4) | data
[1], 11);
447 offset
= sign_extend32(((data
[1] & 0x0F) << 8) | data
[0], 11);
454 * convert raw offset to dps then to rad/s
455 * 12 bits signed raw max 64 to dps: 64 / 2048
456 * dps to rad: Pi / 180
457 * result in nano (1000000000)
458 * (offset * 64 * Pi * 1000000000) / (2048 * 180)
460 val64
= (int64_t)offset
* 64LL * 3141592653LL;
461 /* for rounding, add + or - divisor (2048 * 180) divided by 2 */
463 val64
+= 2048 * 180 / 2;
465 val64
-= 2048 * 180 / 2;
466 bias
= div_s64(val64
, 2048 * 180);
467 *val
= bias
/ 1000000000L;
468 *val2
= bias
% 1000000000L;
470 return IIO_VAL_INT_PLUS_NANO
;
473 static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state
*st
,
474 struct iio_chan_spec
const *chan
,
477 struct device
*dev
= regmap_get_device(st
->map
);
478 int64_t val64
, min
, max
;
479 unsigned int reg
, regval
;
483 if (chan
->type
!= IIO_ANGL_VEL
)
486 switch (chan
->channel2
) {
488 reg
= INV_ICM42600_REG_OFFSET_USER0
;
491 reg
= INV_ICM42600_REG_OFFSET_USER1
;
494 reg
= INV_ICM42600_REG_OFFSET_USER3
;
500 /* inv_icm42600_gyro_calibbias: min - step - max in nano */
501 min
= (int64_t)inv_icm42600_gyro_calibbias
[0] * 1000000000LL +
502 (int64_t)inv_icm42600_gyro_calibbias
[1];
503 max
= (int64_t)inv_icm42600_gyro_calibbias
[4] * 1000000000LL +
504 (int64_t)inv_icm42600_gyro_calibbias
[5];
505 val64
= (int64_t)val
* 1000000000LL + (int64_t)val2
;
506 if (val64
< min
|| val64
> max
)
510 * convert rad/s to dps then to raw value
511 * rad to dps: 180 / Pi
512 * dps to raw 12 bits signed, max 64: 2048 / 64
513 * val in nano (1000000000)
514 * val * 180 * 2048 / (Pi * 1000000000 * 64)
516 val64
= val64
* 180LL * 2048LL;
517 /* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */
519 val64
+= 3141592653LL * 64LL / 2LL;
521 val64
-= 3141592653LL * 64LL / 2LL;
522 offset
= div64_s64(val64
, 3141592653LL * 64LL);
524 /* clamp value limited to 12 bits signed */
527 else if (offset
> 2047)
530 pm_runtime_get_sync(dev
);
531 mutex_lock(&st
->lock
);
533 switch (chan
->channel2
) {
535 /* OFFSET_USER1 register is shared */
536 ret
= regmap_read(st
->map
, INV_ICM42600_REG_OFFSET_USER1
,
540 st
->buffer
[0] = offset
& 0xFF;
541 st
->buffer
[1] = (regval
& 0xF0) | ((offset
& 0xF00) >> 8);
544 /* OFFSET_USER1 register is shared */
545 ret
= regmap_read(st
->map
, INV_ICM42600_REG_OFFSET_USER1
,
549 st
->buffer
[0] = ((offset
& 0xF00) >> 4) | (regval
& 0x0F);
550 st
->buffer
[1] = offset
& 0xFF;
553 /* OFFSET_USER4 register is shared */
554 ret
= regmap_read(st
->map
, INV_ICM42600_REG_OFFSET_USER4
,
558 st
->buffer
[0] = offset
& 0xFF;
559 st
->buffer
[1] = (regval
& 0xF0) | ((offset
& 0xF00) >> 8);
566 ret
= regmap_bulk_write(st
->map
, reg
, st
->buffer
, 2);
569 mutex_unlock(&st
->lock
);
570 pm_runtime_mark_last_busy(dev
);
571 pm_runtime_put_autosuspend(dev
);
575 static int inv_icm42600_gyro_read_raw(struct iio_dev
*indio_dev
,
576 struct iio_chan_spec
const *chan
,
577 int *val
, int *val2
, long mask
)
579 struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
583 switch (chan
->type
) {
587 return inv_icm42600_temp_read_raw(indio_dev
, chan
, val
, val2
, mask
);
593 case IIO_CHAN_INFO_RAW
:
594 ret
= iio_device_claim_direct_mode(indio_dev
);
597 ret
= inv_icm42600_gyro_read_sensor(st
, chan
, &data
);
598 iio_device_release_direct_mode(indio_dev
);
603 case IIO_CHAN_INFO_SCALE
:
604 return inv_icm42600_gyro_read_scale(indio_dev
, val
, val2
);
605 case IIO_CHAN_INFO_SAMP_FREQ
:
606 return inv_icm42600_gyro_read_odr(st
, val
, val2
);
607 case IIO_CHAN_INFO_CALIBBIAS
:
608 return inv_icm42600_gyro_read_offset(st
, chan
, val
, val2
);
614 static int inv_icm42600_gyro_read_avail(struct iio_dev
*indio_dev
,
615 struct iio_chan_spec
const *chan
,
617 int *type
, int *length
, long mask
)
619 struct inv_icm42600_sensor_state
*gyro_st
= iio_priv(indio_dev
);
621 if (chan
->type
!= IIO_ANGL_VEL
)
625 case IIO_CHAN_INFO_SCALE
:
626 *vals
= gyro_st
->scales
;
627 *type
= IIO_VAL_INT_PLUS_NANO
;
628 *length
= gyro_st
->scales_len
;
629 return IIO_AVAIL_LIST
;
630 case IIO_CHAN_INFO_SAMP_FREQ
:
631 *vals
= inv_icm42600_gyro_odr
;
632 *type
= IIO_VAL_INT_PLUS_MICRO
;
633 *length
= ARRAY_SIZE(inv_icm42600_gyro_odr
);
634 return IIO_AVAIL_LIST
;
635 case IIO_CHAN_INFO_CALIBBIAS
:
636 *vals
= inv_icm42600_gyro_calibbias
;
637 *type
= IIO_VAL_INT_PLUS_NANO
;
638 return IIO_AVAIL_RANGE
;
644 static int inv_icm42600_gyro_write_raw(struct iio_dev
*indio_dev
,
645 struct iio_chan_spec
const *chan
,
646 int val
, int val2
, long mask
)
648 struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
651 if (chan
->type
!= IIO_ANGL_VEL
)
655 case IIO_CHAN_INFO_SCALE
:
656 ret
= iio_device_claim_direct_mode(indio_dev
);
659 ret
= inv_icm42600_gyro_write_scale(indio_dev
, val
, val2
);
660 iio_device_release_direct_mode(indio_dev
);
662 case IIO_CHAN_INFO_SAMP_FREQ
:
663 return inv_icm42600_gyro_write_odr(indio_dev
, val
, val2
);
664 case IIO_CHAN_INFO_CALIBBIAS
:
665 ret
= iio_device_claim_direct_mode(indio_dev
);
668 ret
= inv_icm42600_gyro_write_offset(st
, chan
, val
, val2
);
669 iio_device_release_direct_mode(indio_dev
);
676 static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev
*indio_dev
,
677 struct iio_chan_spec
const *chan
,
680 if (chan
->type
!= IIO_ANGL_VEL
)
684 case IIO_CHAN_INFO_SCALE
:
685 return IIO_VAL_INT_PLUS_NANO
;
686 case IIO_CHAN_INFO_SAMP_FREQ
:
687 return IIO_VAL_INT_PLUS_MICRO
;
688 case IIO_CHAN_INFO_CALIBBIAS
:
689 return IIO_VAL_INT_PLUS_NANO
;
695 static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev
*indio_dev
,
698 struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
701 mutex_lock(&st
->lock
);
703 st
->fifo
.watermark
.gyro
= val
;
704 ret
= inv_icm42600_buffer_update_watermark(st
);
706 mutex_unlock(&st
->lock
);
711 static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev
*indio_dev
,
714 struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
720 mutex_lock(&st
->lock
);
722 ret
= inv_icm42600_buffer_hwfifo_flush(st
, count
);
724 ret
= st
->fifo
.nb
.gyro
;
726 mutex_unlock(&st
->lock
);
731 static const struct iio_info inv_icm42600_gyro_info
= {
732 .read_raw
= inv_icm42600_gyro_read_raw
,
733 .read_avail
= inv_icm42600_gyro_read_avail
,
734 .write_raw
= inv_icm42600_gyro_write_raw
,
735 .write_raw_get_fmt
= inv_icm42600_gyro_write_raw_get_fmt
,
736 .debugfs_reg_access
= inv_icm42600_debugfs_reg
,
737 .update_scan_mode
= inv_icm42600_gyro_update_scan_mode
,
738 .hwfifo_set_watermark
= inv_icm42600_gyro_hwfifo_set_watermark
,
739 .hwfifo_flush_to_buffer
= inv_icm42600_gyro_hwfifo_flush
,
742 struct iio_dev
*inv_icm42600_gyro_init(struct inv_icm42600_state
*st
)
744 struct device
*dev
= regmap_get_device(st
->map
);
746 struct inv_icm42600_sensor_state
*gyro_st
;
747 struct inv_sensors_timestamp_chip ts_chip
;
748 struct iio_dev
*indio_dev
;
751 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s-gyro", st
->name
);
753 return ERR_PTR(-ENOMEM
);
755 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*gyro_st
));
757 return ERR_PTR(-ENOMEM
);
758 gyro_st
= iio_priv(indio_dev
);
761 case INV_CHIP_ICM42686
:
762 gyro_st
->scales
= inv_icm42686_gyro_scale
;
763 gyro_st
->scales_len
= ARRAY_SIZE(inv_icm42686_gyro_scale
);
766 gyro_st
->scales
= inv_icm42600_gyro_scale
;
767 gyro_st
->scales_len
= ARRAY_SIZE(inv_icm42600_gyro_scale
);
772 * clock period is 32kHz (31250ns)
773 * jitter is +/- 2% (20 per mille)
775 ts_chip
.clock_period
= 31250;
777 ts_chip
.init_period
= inv_icm42600_odr_to_period(st
->conf
.accel
.odr
);
778 inv_sensors_timestamp_init(&gyro_st
->ts
, &ts_chip
);
780 iio_device_set_drvdata(indio_dev
, st
);
781 indio_dev
->name
= name
;
782 indio_dev
->info
= &inv_icm42600_gyro_info
;
783 indio_dev
->modes
= INDIO_DIRECT_MODE
;
784 indio_dev
->channels
= inv_icm42600_gyro_channels
;
785 indio_dev
->num_channels
= ARRAY_SIZE(inv_icm42600_gyro_channels
);
786 indio_dev
->available_scan_masks
= inv_icm42600_gyro_scan_masks
;
787 indio_dev
->setup_ops
= &inv_icm42600_buffer_ops
;
789 ret
= devm_iio_kfifo_buffer_setup(dev
, indio_dev
,
790 &inv_icm42600_buffer_ops
);
794 ret
= devm_iio_device_register(dev
, indio_dev
);
801 int inv_icm42600_gyro_parse_fifo(struct iio_dev
*indio_dev
)
803 struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
804 struct inv_icm42600_sensor_state
*gyro_st
= iio_priv(indio_dev
);
805 struct inv_sensors_timestamp
*ts
= &gyro_st
->ts
;
808 const void *accel
, *gyro
, *timestamp
;
812 struct inv_icm42600_gyro_buffer buffer
;
814 /* parse all fifo packets */
815 for (i
= 0, no
= 0; i
< st
->fifo
.count
; i
+= size
, ++no
) {
816 size
= inv_icm42600_fifo_decode_packet(&st
->fifo
.data
[i
],
817 &accel
, &gyro
, &temp
, ×tamp
, &odr
);
818 /* quit if error or FIFO is empty */
822 /* skip packet if no gyro data or data is invalid */
823 if (gyro
== NULL
|| !inv_icm42600_fifo_is_data_valid(gyro
))
827 if (odr
& INV_ICM42600_SENSOR_GYRO
)
828 inv_sensors_timestamp_apply_odr(ts
, st
->fifo
.period
,
829 st
->fifo
.nb
.total
, no
);
831 /* buffer is copied to userspace, zeroing it to avoid any data leak */
832 memset(&buffer
, 0, sizeof(buffer
));
833 memcpy(&buffer
.gyro
, gyro
, sizeof(buffer
.gyro
));
834 /* convert 8 bits FIFO temperature in high resolution format */
835 buffer
.temp
= temp
? (*temp
* 64) : 0;
836 ts_val
= inv_sensors_timestamp_pop(ts
);
837 iio_push_to_buffers_with_timestamp(indio_dev
, &buffer
, ts_val
);