drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / iio / imu / inv_icm42600 / inv_icm42600_gyro.c
blobc6bb68bf5e1449d4b961ac962311cbc5aa3c0a97
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2020 Invensense, Inc.
4 */
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) \
24 { \
25 .type = IIO_ANGL_VEL, \
26 .modified = 1, \
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, \
41 .scan_type = { \
42 .sign = 's', \
43 .realbits = 16, \
44 .storagebits = 16, \
45 .endianness = IIO_BE, \
46 }, \
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),
60 {},
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;
80 int16_t temp;
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;
106 unsigned int sleep;
107 int ret;
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);
114 if (ret)
115 goto out_unlock;
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);
123 if (ret)
124 goto out_unlock;
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);
131 out_unlock:
132 mutex_unlock(&st->lock);
133 /* sleep maximum required time */
134 sleep = max(sleep_gyro, sleep_temp);
135 if (sleep)
136 msleep(sleep);
137 return ret;
140 static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
141 struct iio_chan_spec const *chan,
142 int16_t *val)
144 struct device *dev = regmap_get_device(st->map);
145 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
146 unsigned int reg;
147 __be16 *data;
148 int ret;
150 if (chan->type != IIO_ANGL_VEL)
151 return -EINVAL;
153 switch (chan->channel2) {
154 case IIO_MOD_X:
155 reg = INV_ICM42600_REG_GYRO_DATA_X;
156 break;
157 case IIO_MOD_Y:
158 reg = INV_ICM42600_REG_GYRO_DATA_Y;
159 break;
160 case IIO_MOD_Z:
161 reg = INV_ICM42600_REG_GYRO_DATA_Z;
162 break;
163 default:
164 return -EINVAL;
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);
173 if (ret)
174 goto exit;
176 /* read gyro register data */
177 data = (__be16 *)&st->buffer[0];
178 ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
179 if (ret)
180 goto exit;
182 *val = (int16_t)be16_to_cpup(data);
183 if (*val == INV_ICM42600_DATA_INVALID)
184 ret = -EINVAL;
185 exit:
186 mutex_unlock(&st->lock);
187 pm_runtime_mark_last_busy(dev);
188 pm_runtime_put_autosuspend(dev);
189 return ret;
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,
247 int *val, int *val2)
249 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
250 struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
251 unsigned int idx;
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,
261 int val, int val2)
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);
266 unsigned int idx;
267 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
268 int ret;
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])
273 break;
275 if (idx >= gyro_st->scales_len)
276 return -EINVAL;
278 conf.fs = idx / 2;
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);
289 return ret;
292 /* IIO format int + micro */
293 static const int inv_icm42600_gyro_odr[] = {
294 /* 12.5Hz */
295 12, 500000,
296 /* 25Hz */
297 25, 0,
298 /* 50Hz */
299 50, 0,
300 /* 100Hz */
301 100, 0,
302 /* 200Hz */
303 200, 0,
304 /* 1kHz */
305 1000, 0,
306 /* 2kHz */
307 2000, 0,
308 /* 4kHz */
309 4000, 0,
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,
324 int *val, int *val2)
326 unsigned int odr;
327 unsigned int i;
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)
333 break;
335 if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv))
336 return -EINVAL;
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,
345 int val, int val2)
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);
351 unsigned int idx;
352 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
353 int ret;
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])
358 break;
360 if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr))
361 return -EINVAL;
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));
370 if (ret)
371 goto out_unlock;
373 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
374 if (ret)
375 goto out_unlock;
376 inv_icm42600_buffer_update_fifo_period(st);
377 inv_icm42600_buffer_update_watermark(st);
379 out_unlock:
380 mutex_unlock(&st->lock);
381 pm_runtime_mark_last_busy(dev);
382 pm_runtime_put_autosuspend(dev);
384 return ret;
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,
399 int *val, int *val2)
401 struct device *dev = regmap_get_device(st->map);
402 int64_t val64;
403 int32_t bias;
404 unsigned int reg;
405 int16_t offset;
406 uint8_t data[2];
407 int ret;
409 if (chan->type != IIO_ANGL_VEL)
410 return -EINVAL;
412 switch (chan->channel2) {
413 case IIO_MOD_X:
414 reg = INV_ICM42600_REG_OFFSET_USER0;
415 break;
416 case IIO_MOD_Y:
417 reg = INV_ICM42600_REG_OFFSET_USER1;
418 break;
419 case IIO_MOD_Z:
420 reg = INV_ICM42600_REG_OFFSET_USER3;
421 break;
422 default:
423 return -EINVAL;
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);
435 if (ret)
436 return ret;
438 /* 12 bits signed value */
439 switch (chan->channel2) {
440 case IIO_MOD_X:
441 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
442 break;
443 case IIO_MOD_Y:
444 offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
445 break;
446 case IIO_MOD_Z:
447 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
448 break;
449 default:
450 return -EINVAL;
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 */
462 if (val64 >= 0)
463 val64 += 2048 * 180 / 2;
464 else
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,
475 int val, int val2)
477 struct device *dev = regmap_get_device(st->map);
478 int64_t val64, min, max;
479 unsigned int reg, regval;
480 int16_t offset;
481 int ret;
483 if (chan->type != IIO_ANGL_VEL)
484 return -EINVAL;
486 switch (chan->channel2) {
487 case IIO_MOD_X:
488 reg = INV_ICM42600_REG_OFFSET_USER0;
489 break;
490 case IIO_MOD_Y:
491 reg = INV_ICM42600_REG_OFFSET_USER1;
492 break;
493 case IIO_MOD_Z:
494 reg = INV_ICM42600_REG_OFFSET_USER3;
495 break;
496 default:
497 return -EINVAL;
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)
507 return -EINVAL;
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 */
518 if (val64 >= 0)
519 val64 += 3141592653LL * 64LL / 2LL;
520 else
521 val64 -= 3141592653LL * 64LL / 2LL;
522 offset = div64_s64(val64, 3141592653LL * 64LL);
524 /* clamp value limited to 12 bits signed */
525 if (offset < -2048)
526 offset = -2048;
527 else if (offset > 2047)
528 offset = 2047;
530 pm_runtime_get_sync(dev);
531 mutex_lock(&st->lock);
533 switch (chan->channel2) {
534 case IIO_MOD_X:
535 /* OFFSET_USER1 register is shared */
536 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
537 &regval);
538 if (ret)
539 goto out_unlock;
540 st->buffer[0] = offset & 0xFF;
541 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
542 break;
543 case IIO_MOD_Y:
544 /* OFFSET_USER1 register is shared */
545 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
546 &regval);
547 if (ret)
548 goto out_unlock;
549 st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
550 st->buffer[1] = offset & 0xFF;
551 break;
552 case IIO_MOD_Z:
553 /* OFFSET_USER4 register is shared */
554 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
555 &regval);
556 if (ret)
557 goto out_unlock;
558 st->buffer[0] = offset & 0xFF;
559 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
560 break;
561 default:
562 ret = -EINVAL;
563 goto out_unlock;
566 ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
568 out_unlock:
569 mutex_unlock(&st->lock);
570 pm_runtime_mark_last_busy(dev);
571 pm_runtime_put_autosuspend(dev);
572 return ret;
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);
580 int16_t data;
581 int ret;
583 switch (chan->type) {
584 case IIO_ANGL_VEL:
585 break;
586 case IIO_TEMP:
587 return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
588 default:
589 return -EINVAL;
592 switch (mask) {
593 case IIO_CHAN_INFO_RAW:
594 ret = iio_device_claim_direct_mode(indio_dev);
595 if (ret)
596 return ret;
597 ret = inv_icm42600_gyro_read_sensor(st, chan, &data);
598 iio_device_release_direct_mode(indio_dev);
599 if (ret)
600 return ret;
601 *val = data;
602 return IIO_VAL_INT;
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);
609 default:
610 return -EINVAL;
614 static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev,
615 struct iio_chan_spec const *chan,
616 const int **vals,
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)
622 return -EINVAL;
624 switch (mask) {
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;
639 default:
640 return -EINVAL;
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);
649 int ret;
651 if (chan->type != IIO_ANGL_VEL)
652 return -EINVAL;
654 switch (mask) {
655 case IIO_CHAN_INFO_SCALE:
656 ret = iio_device_claim_direct_mode(indio_dev);
657 if (ret)
658 return ret;
659 ret = inv_icm42600_gyro_write_scale(indio_dev, val, val2);
660 iio_device_release_direct_mode(indio_dev);
661 return ret;
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);
666 if (ret)
667 return ret;
668 ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
669 iio_device_release_direct_mode(indio_dev);
670 return ret;
671 default:
672 return -EINVAL;
676 static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
677 struct iio_chan_spec const *chan,
678 long mask)
680 if (chan->type != IIO_ANGL_VEL)
681 return -EINVAL;
683 switch (mask) {
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;
690 default:
691 return -EINVAL;
695 static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
696 unsigned int val)
698 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
699 int ret;
701 mutex_lock(&st->lock);
703 st->fifo.watermark.gyro = val;
704 ret = inv_icm42600_buffer_update_watermark(st);
706 mutex_unlock(&st->lock);
708 return ret;
711 static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
712 unsigned int count)
714 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
715 int ret;
717 if (count == 0)
718 return 0;
720 mutex_lock(&st->lock);
722 ret = inv_icm42600_buffer_hwfifo_flush(st, count);
723 if (!ret)
724 ret = st->fifo.nb.gyro;
726 mutex_unlock(&st->lock);
728 return ret;
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);
745 const char *name;
746 struct inv_icm42600_sensor_state *gyro_st;
747 struct inv_sensors_timestamp_chip ts_chip;
748 struct iio_dev *indio_dev;
749 int ret;
751 name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
752 if (!name)
753 return ERR_PTR(-ENOMEM);
755 indio_dev = devm_iio_device_alloc(dev, sizeof(*gyro_st));
756 if (!indio_dev)
757 return ERR_PTR(-ENOMEM);
758 gyro_st = iio_priv(indio_dev);
760 switch (st->chip) {
761 case INV_CHIP_ICM42686:
762 gyro_st->scales = inv_icm42686_gyro_scale;
763 gyro_st->scales_len = ARRAY_SIZE(inv_icm42686_gyro_scale);
764 break;
765 default:
766 gyro_st->scales = inv_icm42600_gyro_scale;
767 gyro_st->scales_len = ARRAY_SIZE(inv_icm42600_gyro_scale);
768 break;
772 * clock period is 32kHz (31250ns)
773 * jitter is +/- 2% (20 per mille)
775 ts_chip.clock_period = 31250;
776 ts_chip.jitter = 20;
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);
791 if (ret)
792 return ERR_PTR(ret);
794 ret = devm_iio_device_register(dev, indio_dev);
795 if (ret)
796 return ERR_PTR(ret);
798 return 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;
806 ssize_t i, size;
807 unsigned int no;
808 const void *accel, *gyro, *timestamp;
809 const int8_t *temp;
810 unsigned int odr;
811 int64_t ts_val;
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, &timestamp, &odr);
818 /* quit if error or FIFO is empty */
819 if (size <= 0)
820 return size;
822 /* skip packet if no gyro data or data is invalid */
823 if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
824 continue;
826 /* update odr */
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);
840 return 0;