GNU Linux-libre 6.7.9-gnu
[releases.git] / drivers / iio / imu / inv_icm42600 / inv_icm42600_gyro.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2020 Invensense, Inc.
4  */
5
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>
13
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>
18
19 #include "inv_icm42600.h"
20 #include "inv_icm42600_temp.h"
21 #include "inv_icm42600_buffer.h"
22
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,                                  \
48         }
49
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,
56 };
57
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         {},
61 };
62
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),
72 };
73
74 /*
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
77  */
78 struct inv_icm42600_gyro_buffer {
79         struct inv_icm42600_fifo_sensor_data gyro;
80         int16_t temp;
81         int64_t timestamp __aligned(8);
82 };
83
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))
88
89 #define INV_ICM42600_SCAN_MASK_TEMP     BIT(INV_ICM42600_GYRO_SCAN_TEMP)
90
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,
94         0,
95 };
96
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)
100 {
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;
107         unsigned int sleep;
108         int ret;
109
110         mutex_lock(&st->lock);
111
112         if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
113                 /* enable temp sensor */
114                 ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
115                 if (ret)
116                         goto out_unlock;
117                 fifo_en |= INV_ICM42600_SENSOR_TEMP;
118         }
119
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);
124                 if (ret)
125                         goto out_unlock;
126                 fifo_en |= INV_ICM42600_SENSOR_GYRO;
127         }
128
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);
132         if (ret)
133                 goto out_unlock;
134
135         ret = inv_icm42600_buffer_update_watermark(st);
136
137 out_unlock:
138         mutex_unlock(&st->lock);
139         /* sleep maximum required time */
140         if (sleep_gyro > sleep_temp)
141                 sleep = sleep_gyro;
142         else
143                 sleep = sleep_temp;
144         if (sleep)
145                 msleep(sleep);
146         return ret;
147 }
148
149 static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
150                                          struct iio_chan_spec const *chan,
151                                          int16_t *val)
152 {
153         struct device *dev = regmap_get_device(st->map);
154         struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
155         unsigned int reg;
156         __be16 *data;
157         int ret;
158
159         if (chan->type != IIO_ANGL_VEL)
160                 return -EINVAL;
161
162         switch (chan->channel2) {
163         case IIO_MOD_X:
164                 reg = INV_ICM42600_REG_GYRO_DATA_X;
165                 break;
166         case IIO_MOD_Y:
167                 reg = INV_ICM42600_REG_GYRO_DATA_Y;
168                 break;
169         case IIO_MOD_Z:
170                 reg = INV_ICM42600_REG_GYRO_DATA_Z;
171                 break;
172         default:
173                 return -EINVAL;
174         }
175
176         pm_runtime_get_sync(dev);
177         mutex_lock(&st->lock);
178
179         /* enable gyro sensor */
180         conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
181         ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
182         if (ret)
183                 goto exit;
184
185         /* read gyro register data */
186         data = (__be16 *)&st->buffer[0];
187         ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
188         if (ret)
189                 goto exit;
190
191         *val = (int16_t)be16_to_cpup(data);
192         if (*val == INV_ICM42600_DATA_INVALID)
193                 ret = -EINVAL;
194 exit:
195         mutex_unlock(&st->lock);
196         pm_runtime_mark_last_busy(dev);
197         pm_runtime_put_autosuspend(dev);
198         return ret;
199 }
200
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,
227 };
228
229 static int inv_icm42600_gyro_read_scale(struct inv_icm42600_state *st,
230                                         int *val, int *val2)
231 {
232         unsigned int idx;
233
234         idx = st->conf.gyro.fs;
235
236         *val = inv_icm42600_gyro_scale[2 * idx];
237         *val2 = inv_icm42600_gyro_scale[2 * idx + 1];
238         return IIO_VAL_INT_PLUS_NANO;
239 }
240
241 static int inv_icm42600_gyro_write_scale(struct inv_icm42600_state *st,
242                                          int val, int val2)
243 {
244         struct device *dev = regmap_get_device(st->map);
245         unsigned int idx;
246         struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
247         int ret;
248
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])
252                         break;
253         }
254         if (idx >= ARRAY_SIZE(inv_icm42600_gyro_scale))
255                 return -EINVAL;
256
257         conf.fs = idx / 2;
258
259         pm_runtime_get_sync(dev);
260         mutex_lock(&st->lock);
261
262         ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
263
264         mutex_unlock(&st->lock);
265         pm_runtime_mark_last_busy(dev);
266         pm_runtime_put_autosuspend(dev);
267
268         return ret;
269 }
270
271 /* IIO format int + micro */
272 static const int inv_icm42600_gyro_odr[] = {
273         /* 12.5Hz */
274         12, 500000,
275         /* 25Hz */
276         25, 0,
277         /* 50Hz */
278         50, 0,
279         /* 100Hz */
280         100, 0,
281         /* 200Hz */
282         200, 0,
283         /* 1kHz */
284         1000, 0,
285         /* 2kHz */
286         2000, 0,
287         /* 4kHz */
288         4000, 0,
289 };
290
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,
300 };
301
302 static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st,
303                                       int *val, int *val2)
304 {
305         unsigned int odr;
306         unsigned int i;
307
308         odr = st->conf.gyro.odr;
309
310         for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) {
311                 if (inv_icm42600_gyro_odr_conv[i] == odr)
312                         break;
313         }
314         if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv))
315                 return -EINVAL;
316
317         *val = inv_icm42600_gyro_odr[2 * i];
318         *val2 = inv_icm42600_gyro_odr[2 * i + 1];
319
320         return IIO_VAL_INT_PLUS_MICRO;
321 }
322
323 static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev,
324                                        int val, int val2)
325 {
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);
329         unsigned int idx;
330         struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
331         int ret;
332
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])
336                         break;
337         }
338         if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr))
339                 return -EINVAL;
340
341         conf.odr = inv_icm42600_gyro_odr_conv[idx / 2];
342
343         pm_runtime_get_sync(dev);
344         mutex_lock(&st->lock);
345
346         ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr),
347                                                iio_buffer_enabled(indio_dev));
348         if (ret)
349                 goto out_unlock;
350
351         ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
352         if (ret)
353                 goto out_unlock;
354         inv_icm42600_buffer_update_fifo_period(st);
355         inv_icm42600_buffer_update_watermark(st);
356
357 out_unlock:
358         mutex_unlock(&st->lock);
359         pm_runtime_mark_last_busy(dev);
360         pm_runtime_put_autosuspend(dev);
361
362         return ret;
363 }
364
365 /*
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.
368  */
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 */
373 };
374
375 static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st,
376                                          struct iio_chan_spec const *chan,
377                                          int *val, int *val2)
378 {
379         struct device *dev = regmap_get_device(st->map);
380         int64_t val64;
381         int32_t bias;
382         unsigned int reg;
383         int16_t offset;
384         uint8_t data[2];
385         int ret;
386
387         if (chan->type != IIO_ANGL_VEL)
388                 return -EINVAL;
389
390         switch (chan->channel2) {
391         case IIO_MOD_X:
392                 reg = INV_ICM42600_REG_OFFSET_USER0;
393                 break;
394         case IIO_MOD_Y:
395                 reg = INV_ICM42600_REG_OFFSET_USER1;
396                 break;
397         case IIO_MOD_Z:
398                 reg = INV_ICM42600_REG_OFFSET_USER3;
399                 break;
400         default:
401                 return -EINVAL;
402         }
403
404         pm_runtime_get_sync(dev);
405         mutex_lock(&st->lock);
406
407         ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data));
408         memcpy(data, st->buffer, sizeof(data));
409
410         mutex_unlock(&st->lock);
411         pm_runtime_mark_last_busy(dev);
412         pm_runtime_put_autosuspend(dev);
413         if (ret)
414                 return ret;
415
416         /* 12 bits signed value */
417         switch (chan->channel2) {
418         case IIO_MOD_X:
419                 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
420                 break;
421         case IIO_MOD_Y:
422                 offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
423                 break;
424         case IIO_MOD_Z:
425                 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
426                 break;
427         default:
428                 return -EINVAL;
429         }
430
431         /*
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)
437          */
438         val64 = (int64_t)offset * 64LL * 3141592653LL;
439         /* for rounding, add + or - divisor (2048 * 180) divided by 2 */
440         if (val64 >= 0)
441                 val64 += 2048 * 180 / 2;
442         else
443                 val64 -= 2048 * 180 / 2;
444         bias = div_s64(val64, 2048 * 180);
445         *val = bias / 1000000000L;
446         *val2 = bias % 1000000000L;
447
448         return IIO_VAL_INT_PLUS_NANO;
449 }
450
451 static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st,
452                                           struct iio_chan_spec const *chan,
453                                           int val, int val2)
454 {
455         struct device *dev = regmap_get_device(st->map);
456         int64_t val64, min, max;
457         unsigned int reg, regval;
458         int16_t offset;
459         int ret;
460
461         if (chan->type != IIO_ANGL_VEL)
462                 return -EINVAL;
463
464         switch (chan->channel2) {
465         case IIO_MOD_X:
466                 reg = INV_ICM42600_REG_OFFSET_USER0;
467                 break;
468         case IIO_MOD_Y:
469                 reg = INV_ICM42600_REG_OFFSET_USER1;
470                 break;
471         case IIO_MOD_Z:
472                 reg = INV_ICM42600_REG_OFFSET_USER3;
473                 break;
474         default:
475                 return -EINVAL;
476         }
477
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)
485                 return -EINVAL;
486
487         /*
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)
493          */
494         val64 = val64 * 180LL * 2048LL;
495         /* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */
496         if (val64 >= 0)
497                 val64 += 3141592653LL * 64LL / 2LL;
498         else
499                 val64 -= 3141592653LL * 64LL / 2LL;
500         offset = div64_s64(val64, 3141592653LL * 64LL);
501
502         /* clamp value limited to 12 bits signed */
503         if (offset < -2048)
504                 offset = -2048;
505         else if (offset > 2047)
506                 offset = 2047;
507
508         pm_runtime_get_sync(dev);
509         mutex_lock(&st->lock);
510
511         switch (chan->channel2) {
512         case IIO_MOD_X:
513                 /* OFFSET_USER1 register is shared */
514                 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
515                                   &regval);
516                 if (ret)
517                         goto out_unlock;
518                 st->buffer[0] = offset & 0xFF;
519                 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
520                 break;
521         case IIO_MOD_Y:
522                 /* OFFSET_USER1 register is shared */
523                 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
524                                   &regval);
525                 if (ret)
526                         goto out_unlock;
527                 st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
528                 st->buffer[1] = offset & 0xFF;
529                 break;
530         case IIO_MOD_Z:
531                 /* OFFSET_USER4 register is shared */
532                 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
533                                   &regval);
534                 if (ret)
535                         goto out_unlock;
536                 st->buffer[0] = offset & 0xFF;
537                 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
538                 break;
539         default:
540                 ret = -EINVAL;
541                 goto out_unlock;
542         }
543
544         ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
545
546 out_unlock:
547         mutex_unlock(&st->lock);
548         pm_runtime_mark_last_busy(dev);
549         pm_runtime_put_autosuspend(dev);
550         return ret;
551 }
552
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)
556 {
557         struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
558         int16_t data;
559         int ret;
560
561         switch (chan->type) {
562         case IIO_ANGL_VEL:
563                 break;
564         case IIO_TEMP:
565                 return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
566         default:
567                 return -EINVAL;
568         }
569
570         switch (mask) {
571         case IIO_CHAN_INFO_RAW:
572                 ret = iio_device_claim_direct_mode(indio_dev);
573                 if (ret)
574                         return ret;
575                 ret = inv_icm42600_gyro_read_sensor(st, chan, &data);
576                 iio_device_release_direct_mode(indio_dev);
577                 if (ret)
578                         return ret;
579                 *val = data;
580                 return IIO_VAL_INT;
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);
587         default:
588                 return -EINVAL;
589         }
590 }
591
592 static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev,
593                                         struct iio_chan_spec const *chan,
594                                         const int **vals,
595                                         int *type, int *length, long mask)
596 {
597         if (chan->type != IIO_ANGL_VEL)
598                 return -EINVAL;
599
600         switch (mask) {
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;
615         default:
616                 return -EINVAL;
617         }
618 }
619
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)
623 {
624         struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
625         int ret;
626
627         if (chan->type != IIO_ANGL_VEL)
628                 return -EINVAL;
629
630         switch (mask) {
631         case IIO_CHAN_INFO_SCALE:
632                 ret = iio_device_claim_direct_mode(indio_dev);
633                 if (ret)
634                         return ret;
635                 ret = inv_icm42600_gyro_write_scale(st, val, val2);
636                 iio_device_release_direct_mode(indio_dev);
637                 return ret;
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);
642                 if (ret)
643                         return ret;
644                 ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
645                 iio_device_release_direct_mode(indio_dev);
646                 return ret;
647         default:
648                 return -EINVAL;
649         }
650 }
651
652 static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
653                                                struct iio_chan_spec const *chan,
654                                                long mask)
655 {
656         if (chan->type != IIO_ANGL_VEL)
657                 return -EINVAL;
658
659         switch (mask) {
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;
666         default:
667                 return -EINVAL;
668         }
669 }
670
671 static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
672                                                   unsigned int val)
673 {
674         struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
675         int ret;
676
677         mutex_lock(&st->lock);
678
679         st->fifo.watermark.gyro = val;
680         ret = inv_icm42600_buffer_update_watermark(st);
681
682         mutex_unlock(&st->lock);
683
684         return ret;
685 }
686
687 static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
688                                           unsigned int count)
689 {
690         struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
691         int ret;
692
693         if (count == 0)
694                 return 0;
695
696         mutex_lock(&st->lock);
697
698         ret = inv_icm42600_buffer_hwfifo_flush(st, count);
699         if (!ret)
700                 ret = st->fifo.nb.gyro;
701
702         mutex_unlock(&st->lock);
703
704         return ret;
705 }
706
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,
716 };
717
718 struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
719 {
720         struct device *dev = regmap_get_device(st->map);
721         const char *name;
722         struct inv_sensors_timestamp_chip ts_chip;
723         struct inv_sensors_timestamp *ts;
724         struct iio_dev *indio_dev;
725         int ret;
726
727         name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
728         if (!name)
729                 return ERR_PTR(-ENOMEM);
730
731         indio_dev = devm_iio_device_alloc(dev, sizeof(*ts));
732         if (!indio_dev)
733                 return ERR_PTR(-ENOMEM);
734
735         /*
736          * clock period is 32kHz (31250ns)
737          * jitter is +/- 2% (20 per mille)
738          */
739         ts_chip.clock_period = 31250;
740         ts_chip.jitter = 20;
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);
744
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;
753
754         ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
755                                           &inv_icm42600_buffer_ops);
756         if (ret)
757                 return ERR_PTR(ret);
758
759         ret = devm_iio_device_register(dev, indio_dev);
760         if (ret)
761                 return ERR_PTR(ret);
762
763         return indio_dev;
764 }
765
766 int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev)
767 {
768         struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
769         struct inv_sensors_timestamp *ts = iio_priv(indio_dev);
770         ssize_t i, size;
771         unsigned int no;
772         const void *accel, *gyro, *timestamp;
773         const int8_t *temp;
774         unsigned int odr;
775         int64_t ts_val;
776         struct inv_icm42600_gyro_buffer buffer;
777
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, &timestamp, &odr);
782                 /* quit if error or FIFO is empty */
783                 if (size <= 0)
784                         return size;
785
786                 /* skip packet if no gyro data or data is invalid */
787                 if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
788                         continue;
789
790                 /* update odr */
791                 if (odr & INV_ICM42600_SENSOR_GYRO)
792                         inv_sensors_timestamp_apply_odr(ts, st->fifo.period,
793                                                         st->fifo.nb.total, no);
794
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);
802         }
803
804         return 0;
805 }