xref: /linux/drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c (revision ed7171ff9fabc49ae6ed42fbd082a576473836fc)
1a095fadbSJean-Baptiste Maneyrol // SPDX-License-Identifier: GPL-2.0-or-later
2a095fadbSJean-Baptiste Maneyrol /*
3a095fadbSJean-Baptiste Maneyrol  * Copyright (C) 2020 Invensense, Inc.
4a095fadbSJean-Baptiste Maneyrol  */
5a095fadbSJean-Baptiste Maneyrol 
6a095fadbSJean-Baptiste Maneyrol #include <linux/kernel.h>
7a095fadbSJean-Baptiste Maneyrol #include <linux/device.h>
8a095fadbSJean-Baptiste Maneyrol #include <linux/mutex.h>
9a095fadbSJean-Baptiste Maneyrol #include <linux/pm_runtime.h>
10a095fadbSJean-Baptiste Maneyrol #include <linux/regmap.h>
11a095fadbSJean-Baptiste Maneyrol #include <linux/delay.h>
12a095fadbSJean-Baptiste Maneyrol #include <linux/math64.h>
13d99ff463SJean-Baptiste Maneyrol 
147f85e42aSJean-Baptiste Maneyrol #include <linux/iio/buffer.h>
150ecc363cSJean-Baptiste Maneyrol #include <linux/iio/common/inv_sensors_timestamp.h>
16d99ff463SJean-Baptiste Maneyrol #include <linux/iio/iio.h>
177f85e42aSJean-Baptiste Maneyrol #include <linux/iio/kfifo_buf.h>
18a095fadbSJean-Baptiste Maneyrol 
19a095fadbSJean-Baptiste Maneyrol #include "inv_icm42600.h"
20bc3eb020SJean-Baptiste Maneyrol #include "inv_icm42600_temp.h"
217f85e42aSJean-Baptiste Maneyrol #include "inv_icm42600_buffer.h"
22a095fadbSJean-Baptiste Maneyrol 
23a095fadbSJean-Baptiste Maneyrol #define INV_ICM42600_GYRO_CHAN(_modifier, _index, _ext_info)		\
24a095fadbSJean-Baptiste Maneyrol 	{								\
25a095fadbSJean-Baptiste Maneyrol 		.type = IIO_ANGL_VEL,					\
26a095fadbSJean-Baptiste Maneyrol 		.modified = 1,						\
27a095fadbSJean-Baptiste Maneyrol 		.channel2 = _modifier,					\
28a095fadbSJean-Baptiste Maneyrol 		.info_mask_separate =					\
29a095fadbSJean-Baptiste Maneyrol 			BIT(IIO_CHAN_INFO_RAW) |			\
30a095fadbSJean-Baptiste Maneyrol 			BIT(IIO_CHAN_INFO_CALIBBIAS),			\
31a095fadbSJean-Baptiste Maneyrol 		.info_mask_shared_by_type =				\
32a095fadbSJean-Baptiste Maneyrol 			BIT(IIO_CHAN_INFO_SCALE),			\
33a095fadbSJean-Baptiste Maneyrol 		.info_mask_shared_by_type_available =			\
34a095fadbSJean-Baptiste Maneyrol 			BIT(IIO_CHAN_INFO_SCALE) |			\
35a095fadbSJean-Baptiste Maneyrol 			BIT(IIO_CHAN_INFO_CALIBBIAS),			\
36a095fadbSJean-Baptiste Maneyrol 		.info_mask_shared_by_all =				\
37a095fadbSJean-Baptiste Maneyrol 			BIT(IIO_CHAN_INFO_SAMP_FREQ),			\
38a095fadbSJean-Baptiste Maneyrol 		.info_mask_shared_by_all_available =			\
39a095fadbSJean-Baptiste Maneyrol 			BIT(IIO_CHAN_INFO_SAMP_FREQ),			\
40a095fadbSJean-Baptiste Maneyrol 		.scan_index = _index,					\
41a095fadbSJean-Baptiste Maneyrol 		.scan_type = {						\
42a095fadbSJean-Baptiste Maneyrol 			.sign = 's',					\
43a095fadbSJean-Baptiste Maneyrol 			.realbits = 16,					\
44a095fadbSJean-Baptiste Maneyrol 			.storagebits = 16,				\
45a095fadbSJean-Baptiste Maneyrol 			.endianness = IIO_BE,				\
46a095fadbSJean-Baptiste Maneyrol 		},							\
47a095fadbSJean-Baptiste Maneyrol 		.ext_info = _ext_info,					\
48a095fadbSJean-Baptiste Maneyrol 	}
49a095fadbSJean-Baptiste Maneyrol 
50a095fadbSJean-Baptiste Maneyrol enum inv_icm42600_gyro_scan {
51a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_GYRO_SCAN_X,
52a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_GYRO_SCAN_Y,
53a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_GYRO_SCAN_Z,
54bc3eb020SJean-Baptiste Maneyrol 	INV_ICM42600_GYRO_SCAN_TEMP,
55ec74ae9fSJean-Baptiste Maneyrol 	INV_ICM42600_GYRO_SCAN_TIMESTAMP,
56a095fadbSJean-Baptiste Maneyrol };
57a095fadbSJean-Baptiste Maneyrol 
58a095fadbSJean-Baptiste Maneyrol static const struct iio_chan_spec_ext_info inv_icm42600_gyro_ext_infos[] = {
59a095fadbSJean-Baptiste Maneyrol 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, inv_icm42600_get_mount_matrix),
60a095fadbSJean-Baptiste Maneyrol 	{},
61a095fadbSJean-Baptiste Maneyrol };
62a095fadbSJean-Baptiste Maneyrol 
63a095fadbSJean-Baptiste Maneyrol static const struct iio_chan_spec inv_icm42600_gyro_channels[] = {
64a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_GYRO_CHAN(IIO_MOD_X, INV_ICM42600_GYRO_SCAN_X,
65a095fadbSJean-Baptiste Maneyrol 			       inv_icm42600_gyro_ext_infos),
66a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_GYRO_CHAN(IIO_MOD_Y, INV_ICM42600_GYRO_SCAN_Y,
67a095fadbSJean-Baptiste Maneyrol 			       inv_icm42600_gyro_ext_infos),
68a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_GYRO_CHAN(IIO_MOD_Z, INV_ICM42600_GYRO_SCAN_Z,
69a095fadbSJean-Baptiste Maneyrol 			       inv_icm42600_gyro_ext_infos),
70bc3eb020SJean-Baptiste Maneyrol 	INV_ICM42600_TEMP_CHAN(INV_ICM42600_GYRO_SCAN_TEMP),
71ec74ae9fSJean-Baptiste Maneyrol 	IIO_CHAN_SOFT_TIMESTAMP(INV_ICM42600_GYRO_SCAN_TIMESTAMP),
72a095fadbSJean-Baptiste Maneyrol };
73a095fadbSJean-Baptiste Maneyrol 
747f85e42aSJean-Baptiste Maneyrol /*
75ec74ae9fSJean-Baptiste Maneyrol  * IIO buffer data: size must be a power of 2 and timestamp aligned
76ec74ae9fSJean-Baptiste Maneyrol  * 16 bytes: 6 bytes angular velocity, 2 bytes temperature, 8 bytes timestamp
777f85e42aSJean-Baptiste Maneyrol  */
787f85e42aSJean-Baptiste Maneyrol struct inv_icm42600_gyro_buffer {
797f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_fifo_sensor_data gyro;
807f85e42aSJean-Baptiste Maneyrol 	int16_t temp;
81ec74ae9fSJean-Baptiste Maneyrol 	int64_t timestamp __aligned(8);
827f85e42aSJean-Baptiste Maneyrol };
837f85e42aSJean-Baptiste Maneyrol 
847f85e42aSJean-Baptiste Maneyrol #define INV_ICM42600_SCAN_MASK_GYRO_3AXIS				\
857f85e42aSJean-Baptiste Maneyrol 	(BIT(INV_ICM42600_GYRO_SCAN_X) |				\
867f85e42aSJean-Baptiste Maneyrol 	BIT(INV_ICM42600_GYRO_SCAN_Y) |					\
877f85e42aSJean-Baptiste Maneyrol 	BIT(INV_ICM42600_GYRO_SCAN_Z))
887f85e42aSJean-Baptiste Maneyrol 
897f85e42aSJean-Baptiste Maneyrol #define INV_ICM42600_SCAN_MASK_TEMP	BIT(INV_ICM42600_GYRO_SCAN_TEMP)
907f85e42aSJean-Baptiste Maneyrol 
917f85e42aSJean-Baptiste Maneyrol static const unsigned long inv_icm42600_gyro_scan_masks[] = {
927f85e42aSJean-Baptiste Maneyrol 	/* 3-axis gyro + temperature */
937f85e42aSJean-Baptiste Maneyrol 	INV_ICM42600_SCAN_MASK_GYRO_3AXIS | INV_ICM42600_SCAN_MASK_TEMP,
947f85e42aSJean-Baptiste Maneyrol 	0,
957f85e42aSJean-Baptiste Maneyrol };
967f85e42aSJean-Baptiste Maneyrol 
977f85e42aSJean-Baptiste Maneyrol /* enable gyroscope sensor and FIFO write */
987f85e42aSJean-Baptiste Maneyrol static int inv_icm42600_gyro_update_scan_mode(struct iio_dev *indio_dev,
997f85e42aSJean-Baptiste Maneyrol 					      const unsigned long *scan_mask)
1007f85e42aSJean-Baptiste Maneyrol {
1017f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
102*a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
103*a1432b5bSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp *ts = &gyro_st->ts;
1047f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
1057f85e42aSJean-Baptiste Maneyrol 	unsigned int fifo_en = 0;
1067f85e42aSJean-Baptiste Maneyrol 	unsigned int sleep_gyro = 0;
1077f85e42aSJean-Baptiste Maneyrol 	unsigned int sleep_temp = 0;
1087f85e42aSJean-Baptiste Maneyrol 	unsigned int sleep;
1097f85e42aSJean-Baptiste Maneyrol 	int ret;
1107f85e42aSJean-Baptiste Maneyrol 
1117f85e42aSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
1127f85e42aSJean-Baptiste Maneyrol 
1137f85e42aSJean-Baptiste Maneyrol 	if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
1147f85e42aSJean-Baptiste Maneyrol 		/* enable temp sensor */
1157f85e42aSJean-Baptiste Maneyrol 		ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
1167f85e42aSJean-Baptiste Maneyrol 		if (ret)
1177f85e42aSJean-Baptiste Maneyrol 			goto out_unlock;
1187f85e42aSJean-Baptiste Maneyrol 		fifo_en |= INV_ICM42600_SENSOR_TEMP;
1197f85e42aSJean-Baptiste Maneyrol 	}
1207f85e42aSJean-Baptiste Maneyrol 
1217f85e42aSJean-Baptiste Maneyrol 	if (*scan_mask & INV_ICM42600_SCAN_MASK_GYRO_3AXIS) {
1227f85e42aSJean-Baptiste Maneyrol 		/* enable gyro sensor */
1237f85e42aSJean-Baptiste Maneyrol 		conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
1247f85e42aSJean-Baptiste Maneyrol 		ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_gyro);
1257f85e42aSJean-Baptiste Maneyrol 		if (ret)
1267f85e42aSJean-Baptiste Maneyrol 			goto out_unlock;
1277f85e42aSJean-Baptiste Maneyrol 		fifo_en |= INV_ICM42600_SENSOR_GYRO;
1287f85e42aSJean-Baptiste Maneyrol 	}
1297f85e42aSJean-Baptiste Maneyrol 
1307f85e42aSJean-Baptiste Maneyrol 	/* update data FIFO write */
1310ecc363cSJean-Baptiste Maneyrol 	inv_sensors_timestamp_apply_odr(ts, 0, 0, 0);
1327f85e42aSJean-Baptiste Maneyrol 	ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
1337f85e42aSJean-Baptiste Maneyrol 
1347f85e42aSJean-Baptiste Maneyrol out_unlock:
1357f85e42aSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
1367f85e42aSJean-Baptiste Maneyrol 	/* sleep maximum required time */
137c788b9e5SBragatheswaran Manickavel 	sleep = max(sleep_gyro, sleep_temp);
1387f85e42aSJean-Baptiste Maneyrol 	if (sleep)
1397f85e42aSJean-Baptiste Maneyrol 		msleep(sleep);
1407f85e42aSJean-Baptiste Maneyrol 	return ret;
1417f85e42aSJean-Baptiste Maneyrol }
1427f85e42aSJean-Baptiste Maneyrol 
143a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
144a095fadbSJean-Baptiste Maneyrol 					 struct iio_chan_spec const *chan,
145a095fadbSJean-Baptiste Maneyrol 					 int16_t *val)
146a095fadbSJean-Baptiste Maneyrol {
147a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
148a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
149a095fadbSJean-Baptiste Maneyrol 	unsigned int reg;
150a095fadbSJean-Baptiste Maneyrol 	__be16 *data;
151a095fadbSJean-Baptiste Maneyrol 	int ret;
152a095fadbSJean-Baptiste Maneyrol 
153a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
154a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
155a095fadbSJean-Baptiste Maneyrol 
156a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
157a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
158a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_GYRO_DATA_X;
159a095fadbSJean-Baptiste Maneyrol 		break;
160a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
161a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_GYRO_DATA_Y;
162a095fadbSJean-Baptiste Maneyrol 		break;
163a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
164a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_GYRO_DATA_Z;
165a095fadbSJean-Baptiste Maneyrol 		break;
166a095fadbSJean-Baptiste Maneyrol 	default:
167a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
168a095fadbSJean-Baptiste Maneyrol 	}
169a095fadbSJean-Baptiste Maneyrol 
170a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
171a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
172a095fadbSJean-Baptiste Maneyrol 
173a095fadbSJean-Baptiste Maneyrol 	/* enable gyro sensor */
174a095fadbSJean-Baptiste Maneyrol 	conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
175a095fadbSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
176a095fadbSJean-Baptiste Maneyrol 	if (ret)
177a095fadbSJean-Baptiste Maneyrol 		goto exit;
178a095fadbSJean-Baptiste Maneyrol 
179a095fadbSJean-Baptiste Maneyrol 	/* read gyro register data */
180a095fadbSJean-Baptiste Maneyrol 	data = (__be16 *)&st->buffer[0];
181a095fadbSJean-Baptiste Maneyrol 	ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
182a095fadbSJean-Baptiste Maneyrol 	if (ret)
183a095fadbSJean-Baptiste Maneyrol 		goto exit;
184a095fadbSJean-Baptiste Maneyrol 
185a095fadbSJean-Baptiste Maneyrol 	*val = (int16_t)be16_to_cpup(data);
186a095fadbSJean-Baptiste Maneyrol 	if (*val == INV_ICM42600_DATA_INVALID)
187a095fadbSJean-Baptiste Maneyrol 		ret = -EINVAL;
188a095fadbSJean-Baptiste Maneyrol exit:
189a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
190a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
191a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
192a095fadbSJean-Baptiste Maneyrol 	return ret;
193a095fadbSJean-Baptiste Maneyrol }
194a095fadbSJean-Baptiste Maneyrol 
195a095fadbSJean-Baptiste Maneyrol /* IIO format int + nano */
196a095fadbSJean-Baptiste Maneyrol static const int inv_icm42600_gyro_scale[] = {
197a095fadbSJean-Baptiste Maneyrol 	/* +/- 2000dps => 0.001065264 rad/s */
198a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_2000DPS] = 0,
199a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_2000DPS + 1] = 1065264,
200a095fadbSJean-Baptiste Maneyrol 	/* +/- 1000dps => 0.000532632 rad/s */
201a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_1000DPS] = 0,
202a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_1000DPS + 1] = 532632,
203a095fadbSJean-Baptiste Maneyrol 	/* +/- 500dps => 0.000266316 rad/s */
204a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_500DPS] = 0,
205a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_500DPS + 1] = 266316,
206a095fadbSJean-Baptiste Maneyrol 	/* +/- 250dps => 0.000133158 rad/s */
207a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_250DPS] = 0,
208a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_250DPS + 1] = 133158,
209a095fadbSJean-Baptiste Maneyrol 	/* +/- 125dps => 0.000066579 rad/s */
210a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_125DPS] = 0,
211a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_125DPS + 1] = 66579,
212a095fadbSJean-Baptiste Maneyrol 	/* +/- 62.5dps => 0.000033290 rad/s */
213a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_62_5DPS] = 0,
214a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_62_5DPS + 1] = 33290,
215a095fadbSJean-Baptiste Maneyrol 	/* +/- 31.25dps => 0.000016645 rad/s */
216a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_31_25DPS] = 0,
217a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_31_25DPS + 1] = 16645,
218a095fadbSJean-Baptiste Maneyrol 	/* +/- 15.625dps => 0.000008322 rad/s */
219a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_15_625DPS] = 0,
220a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_15_625DPS + 1] = 8322,
221a095fadbSJean-Baptiste Maneyrol };
222*a1432b5bSJean-Baptiste Maneyrol static const int inv_icm42686_gyro_scale[] = {
223*a1432b5bSJean-Baptiste Maneyrol 	/* +/- 4000dps => 0.002130529 rad/s */
224*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_4000DPS] = 0,
225*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_4000DPS + 1] = 2130529,
226*a1432b5bSJean-Baptiste Maneyrol 	/* +/- 2000dps => 0.001065264 rad/s */
227*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_2000DPS] = 0,
228*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_2000DPS + 1] = 1065264,
229*a1432b5bSJean-Baptiste Maneyrol 	/* +/- 1000dps => 0.000532632 rad/s */
230*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_1000DPS] = 0,
231*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_1000DPS + 1] = 532632,
232*a1432b5bSJean-Baptiste Maneyrol 	/* +/- 500dps => 0.000266316 rad/s */
233*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_500DPS] = 0,
234*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_500DPS + 1] = 266316,
235*a1432b5bSJean-Baptiste Maneyrol 	/* +/- 250dps => 0.000133158 rad/s */
236*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_250DPS] = 0,
237*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_250DPS + 1] = 133158,
238*a1432b5bSJean-Baptiste Maneyrol 	/* +/- 125dps => 0.000066579 rad/s */
239*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_125DPS] = 0,
240*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_125DPS + 1] = 66579,
241*a1432b5bSJean-Baptiste Maneyrol 	/* +/- 62.5dps => 0.000033290 rad/s */
242*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_62_5DPS] = 0,
243*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_62_5DPS + 1] = 33290,
244*a1432b5bSJean-Baptiste Maneyrol 	/* +/- 31.25dps => 0.000016645 rad/s */
245*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_31_25DPS] = 0,
246*a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_31_25DPS + 1] = 16645,
247*a1432b5bSJean-Baptiste Maneyrol };
248a095fadbSJean-Baptiste Maneyrol 
249*a1432b5bSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_scale(struct iio_dev *indio_dev,
250a095fadbSJean-Baptiste Maneyrol 					int *val, int *val2)
251a095fadbSJean-Baptiste Maneyrol {
252*a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
253*a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
254a095fadbSJean-Baptiste Maneyrol 	unsigned int idx;
255a095fadbSJean-Baptiste Maneyrol 
256a095fadbSJean-Baptiste Maneyrol 	idx = st->conf.gyro.fs;
257a095fadbSJean-Baptiste Maneyrol 
258*a1432b5bSJean-Baptiste Maneyrol 	*val = gyro_st->scales[2 * idx];
259*a1432b5bSJean-Baptiste Maneyrol 	*val2 = gyro_st->scales[2 * idx + 1];
260a095fadbSJean-Baptiste Maneyrol 	return IIO_VAL_INT_PLUS_NANO;
261a095fadbSJean-Baptiste Maneyrol }
262a095fadbSJean-Baptiste Maneyrol 
263*a1432b5bSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_scale(struct iio_dev *indio_dev,
264a095fadbSJean-Baptiste Maneyrol 					 int val, int val2)
265a095fadbSJean-Baptiste Maneyrol {
266*a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
267*a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
268a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
269a095fadbSJean-Baptiste Maneyrol 	unsigned int idx;
270a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
271a095fadbSJean-Baptiste Maneyrol 	int ret;
272a095fadbSJean-Baptiste Maneyrol 
273*a1432b5bSJean-Baptiste Maneyrol 	for (idx = 0; idx < gyro_st->scales_len; idx += 2) {
274*a1432b5bSJean-Baptiste Maneyrol 		if (val == gyro_st->scales[idx] &&
275*a1432b5bSJean-Baptiste Maneyrol 		    val2 == gyro_st->scales[idx + 1])
276a095fadbSJean-Baptiste Maneyrol 			break;
277a095fadbSJean-Baptiste Maneyrol 	}
278*a1432b5bSJean-Baptiste Maneyrol 	if (idx >= gyro_st->scales_len)
279a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
280a095fadbSJean-Baptiste Maneyrol 
281a095fadbSJean-Baptiste Maneyrol 	conf.fs = idx / 2;
282a095fadbSJean-Baptiste Maneyrol 
283a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
284a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
285a095fadbSJean-Baptiste Maneyrol 
286a095fadbSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
287a095fadbSJean-Baptiste Maneyrol 
288a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
289a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
290a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
291a095fadbSJean-Baptiste Maneyrol 
292a095fadbSJean-Baptiste Maneyrol 	return ret;
293a095fadbSJean-Baptiste Maneyrol }
294a095fadbSJean-Baptiste Maneyrol 
295a095fadbSJean-Baptiste Maneyrol /* IIO format int + micro */
296a095fadbSJean-Baptiste Maneyrol static const int inv_icm42600_gyro_odr[] = {
297a095fadbSJean-Baptiste Maneyrol 	/* 12.5Hz */
298a095fadbSJean-Baptiste Maneyrol 	12, 500000,
299a095fadbSJean-Baptiste Maneyrol 	/* 25Hz */
300a095fadbSJean-Baptiste Maneyrol 	25, 0,
301a095fadbSJean-Baptiste Maneyrol 	/* 50Hz */
302a095fadbSJean-Baptiste Maneyrol 	50, 0,
303a095fadbSJean-Baptiste Maneyrol 	/* 100Hz */
304a095fadbSJean-Baptiste Maneyrol 	100, 0,
305a095fadbSJean-Baptiste Maneyrol 	/* 200Hz */
306a095fadbSJean-Baptiste Maneyrol 	200, 0,
307a095fadbSJean-Baptiste Maneyrol 	/* 1kHz */
308a095fadbSJean-Baptiste Maneyrol 	1000, 0,
309a095fadbSJean-Baptiste Maneyrol 	/* 2kHz */
310a095fadbSJean-Baptiste Maneyrol 	2000, 0,
311a095fadbSJean-Baptiste Maneyrol 	/* 4kHz */
312a095fadbSJean-Baptiste Maneyrol 	4000, 0,
313a095fadbSJean-Baptiste Maneyrol };
314a095fadbSJean-Baptiste Maneyrol 
315a095fadbSJean-Baptiste Maneyrol static const int inv_icm42600_gyro_odr_conv[] = {
316a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_12_5HZ,
317a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_25HZ,
318a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_50HZ,
319a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_100HZ,
320a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_200HZ,
321a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_1KHZ_LN,
322a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_2KHZ_LN,
323a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_4KHZ_LN,
324a095fadbSJean-Baptiste Maneyrol };
325a095fadbSJean-Baptiste Maneyrol 
326a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st,
327a095fadbSJean-Baptiste Maneyrol 				      int *val, int *val2)
328a095fadbSJean-Baptiste Maneyrol {
329a095fadbSJean-Baptiste Maneyrol 	unsigned int odr;
330a095fadbSJean-Baptiste Maneyrol 	unsigned int i;
331a095fadbSJean-Baptiste Maneyrol 
332a095fadbSJean-Baptiste Maneyrol 	odr = st->conf.gyro.odr;
333a095fadbSJean-Baptiste Maneyrol 
334a095fadbSJean-Baptiste Maneyrol 	for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) {
335a095fadbSJean-Baptiste Maneyrol 		if (inv_icm42600_gyro_odr_conv[i] == odr)
336a095fadbSJean-Baptiste Maneyrol 			break;
337a095fadbSJean-Baptiste Maneyrol 	}
338a095fadbSJean-Baptiste Maneyrol 	if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv))
339a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
340a095fadbSJean-Baptiste Maneyrol 
341a095fadbSJean-Baptiste Maneyrol 	*val = inv_icm42600_gyro_odr[2 * i];
342a095fadbSJean-Baptiste Maneyrol 	*val2 = inv_icm42600_gyro_odr[2 * i + 1];
343a095fadbSJean-Baptiste Maneyrol 
344a095fadbSJean-Baptiste Maneyrol 	return IIO_VAL_INT_PLUS_MICRO;
345a095fadbSJean-Baptiste Maneyrol }
346a095fadbSJean-Baptiste Maneyrol 
347ec74ae9fSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev,
348a095fadbSJean-Baptiste Maneyrol 				       int val, int val2)
349a095fadbSJean-Baptiste Maneyrol {
350ec74ae9fSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
351*a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
352*a1432b5bSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp *ts = &gyro_st->ts;
353a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
354a095fadbSJean-Baptiste Maneyrol 	unsigned int idx;
355a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
356a095fadbSJean-Baptiste Maneyrol 	int ret;
357a095fadbSJean-Baptiste Maneyrol 
358a095fadbSJean-Baptiste Maneyrol 	for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_odr); idx += 2) {
359a095fadbSJean-Baptiste Maneyrol 		if (val == inv_icm42600_gyro_odr[idx] &&
360a095fadbSJean-Baptiste Maneyrol 		    val2 == inv_icm42600_gyro_odr[idx + 1])
361a095fadbSJean-Baptiste Maneyrol 			break;
362a095fadbSJean-Baptiste Maneyrol 	}
363a095fadbSJean-Baptiste Maneyrol 	if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr))
364a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
365a095fadbSJean-Baptiste Maneyrol 
366a095fadbSJean-Baptiste Maneyrol 	conf.odr = inv_icm42600_gyro_odr_conv[idx / 2];
367a095fadbSJean-Baptiste Maneyrol 
368a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
369a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
370a095fadbSJean-Baptiste Maneyrol 
3710ecc363cSJean-Baptiste Maneyrol 	ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr),
372ec74ae9fSJean-Baptiste Maneyrol 					       iio_buffer_enabled(indio_dev));
373ec74ae9fSJean-Baptiste Maneyrol 	if (ret)
374ec74ae9fSJean-Baptiste Maneyrol 		goto out_unlock;
375ec74ae9fSJean-Baptiste Maneyrol 
376a095fadbSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
3777f85e42aSJean-Baptiste Maneyrol 	if (ret)
3787f85e42aSJean-Baptiste Maneyrol 		goto out_unlock;
3797f85e42aSJean-Baptiste Maneyrol 	inv_icm42600_buffer_update_fifo_period(st);
3807f85e42aSJean-Baptiste Maneyrol 	inv_icm42600_buffer_update_watermark(st);
381a095fadbSJean-Baptiste Maneyrol 
3827f85e42aSJean-Baptiste Maneyrol out_unlock:
383a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
384a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
385a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
386a095fadbSJean-Baptiste Maneyrol 
387a095fadbSJean-Baptiste Maneyrol 	return ret;
388a095fadbSJean-Baptiste Maneyrol }
389a095fadbSJean-Baptiste Maneyrol 
390a095fadbSJean-Baptiste Maneyrol /*
391a095fadbSJean-Baptiste Maneyrol  * Calibration bias values, IIO range format int + nano.
392a095fadbSJean-Baptiste Maneyrol  * Value is limited to +/-64dps coded on 12 bits signed. Step is 1/32 dps.
393a095fadbSJean-Baptiste Maneyrol  */
394a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_calibbias[] = {
395a095fadbSJean-Baptiste Maneyrol 	-1, 117010721,		/* min: -1.117010721 rad/s */
396a095fadbSJean-Baptiste Maneyrol 	0, 545415,		/* step: 0.000545415 rad/s */
397a095fadbSJean-Baptiste Maneyrol 	1, 116465306,		/* max: 1.116465306 rad/s */
398a095fadbSJean-Baptiste Maneyrol };
399a095fadbSJean-Baptiste Maneyrol 
400a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st,
401a095fadbSJean-Baptiste Maneyrol 					 struct iio_chan_spec const *chan,
402a095fadbSJean-Baptiste Maneyrol 					 int *val, int *val2)
403a095fadbSJean-Baptiste Maneyrol {
404a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
405a095fadbSJean-Baptiste Maneyrol 	int64_t val64;
406a095fadbSJean-Baptiste Maneyrol 	int32_t bias;
407a095fadbSJean-Baptiste Maneyrol 	unsigned int reg;
408a095fadbSJean-Baptiste Maneyrol 	int16_t offset;
409a095fadbSJean-Baptiste Maneyrol 	uint8_t data[2];
410a095fadbSJean-Baptiste Maneyrol 	int ret;
411a095fadbSJean-Baptiste Maneyrol 
412a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
413a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
414a095fadbSJean-Baptiste Maneyrol 
415a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
416a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
417a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER0;
418a095fadbSJean-Baptiste Maneyrol 		break;
419a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
420a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER1;
421a095fadbSJean-Baptiste Maneyrol 		break;
422a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
423a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER3;
424a095fadbSJean-Baptiste Maneyrol 		break;
425a095fadbSJean-Baptiste Maneyrol 	default:
426a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
427a095fadbSJean-Baptiste Maneyrol 	}
428a095fadbSJean-Baptiste Maneyrol 
429a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
430a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
431a095fadbSJean-Baptiste Maneyrol 
432a095fadbSJean-Baptiste Maneyrol 	ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data));
433a095fadbSJean-Baptiste Maneyrol 	memcpy(data, st->buffer, sizeof(data));
434a095fadbSJean-Baptiste Maneyrol 
435a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
436a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
437a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
438a095fadbSJean-Baptiste Maneyrol 	if (ret)
439a095fadbSJean-Baptiste Maneyrol 		return ret;
440a095fadbSJean-Baptiste Maneyrol 
441a095fadbSJean-Baptiste Maneyrol 	/* 12 bits signed value */
442a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
443a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
444a095fadbSJean-Baptiste Maneyrol 		offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
445a095fadbSJean-Baptiste Maneyrol 		break;
446a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
447a095fadbSJean-Baptiste Maneyrol 		offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
448a095fadbSJean-Baptiste Maneyrol 		break;
449a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
450a095fadbSJean-Baptiste Maneyrol 		offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
451a095fadbSJean-Baptiste Maneyrol 		break;
452a095fadbSJean-Baptiste Maneyrol 	default:
453a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
454a095fadbSJean-Baptiste Maneyrol 	}
455a095fadbSJean-Baptiste Maneyrol 
456a095fadbSJean-Baptiste Maneyrol 	/*
457a095fadbSJean-Baptiste Maneyrol 	 * convert raw offset to dps then to rad/s
458a095fadbSJean-Baptiste Maneyrol 	 * 12 bits signed raw max 64 to dps: 64 / 2048
459a095fadbSJean-Baptiste Maneyrol 	 * dps to rad: Pi / 180
460a095fadbSJean-Baptiste Maneyrol 	 * result in nano (1000000000)
461a095fadbSJean-Baptiste Maneyrol 	 * (offset * 64 * Pi * 1000000000) / (2048 * 180)
462a095fadbSJean-Baptiste Maneyrol 	 */
463a095fadbSJean-Baptiste Maneyrol 	val64 = (int64_t)offset * 64LL * 3141592653LL;
464a095fadbSJean-Baptiste Maneyrol 	/* for rounding, add + or - divisor (2048 * 180) divided by 2 */
465a095fadbSJean-Baptiste Maneyrol 	if (val64 >= 0)
466a095fadbSJean-Baptiste Maneyrol 		val64 += 2048 * 180 / 2;
467a095fadbSJean-Baptiste Maneyrol 	else
468a095fadbSJean-Baptiste Maneyrol 		val64 -= 2048 * 180 / 2;
469a095fadbSJean-Baptiste Maneyrol 	bias = div_s64(val64, 2048 * 180);
470a095fadbSJean-Baptiste Maneyrol 	*val = bias / 1000000000L;
471a095fadbSJean-Baptiste Maneyrol 	*val2 = bias % 1000000000L;
472a095fadbSJean-Baptiste Maneyrol 
473a095fadbSJean-Baptiste Maneyrol 	return IIO_VAL_INT_PLUS_NANO;
474a095fadbSJean-Baptiste Maneyrol }
475a095fadbSJean-Baptiste Maneyrol 
476a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st,
477a095fadbSJean-Baptiste Maneyrol 					  struct iio_chan_spec const *chan,
478a095fadbSJean-Baptiste Maneyrol 					  int val, int val2)
479a095fadbSJean-Baptiste Maneyrol {
480a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
481a095fadbSJean-Baptiste Maneyrol 	int64_t val64, min, max;
482a095fadbSJean-Baptiste Maneyrol 	unsigned int reg, regval;
483a095fadbSJean-Baptiste Maneyrol 	int16_t offset;
484a095fadbSJean-Baptiste Maneyrol 	int ret;
485a095fadbSJean-Baptiste Maneyrol 
486a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
487a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
488a095fadbSJean-Baptiste Maneyrol 
489a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
490a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
491a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER0;
492a095fadbSJean-Baptiste Maneyrol 		break;
493a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
494a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER1;
495a095fadbSJean-Baptiste Maneyrol 		break;
496a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
497a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER3;
498a095fadbSJean-Baptiste Maneyrol 		break;
499a095fadbSJean-Baptiste Maneyrol 	default:
500a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
501a095fadbSJean-Baptiste Maneyrol 	}
502a095fadbSJean-Baptiste Maneyrol 
503a095fadbSJean-Baptiste Maneyrol 	/* inv_icm42600_gyro_calibbias: min - step - max in nano */
504a095fadbSJean-Baptiste Maneyrol 	min = (int64_t)inv_icm42600_gyro_calibbias[0] * 1000000000LL +
505a095fadbSJean-Baptiste Maneyrol 	      (int64_t)inv_icm42600_gyro_calibbias[1];
506a095fadbSJean-Baptiste Maneyrol 	max = (int64_t)inv_icm42600_gyro_calibbias[4] * 1000000000LL +
507a095fadbSJean-Baptiste Maneyrol 	      (int64_t)inv_icm42600_gyro_calibbias[5];
508a095fadbSJean-Baptiste Maneyrol 	val64 = (int64_t)val * 1000000000LL + (int64_t)val2;
509a095fadbSJean-Baptiste Maneyrol 	if (val64 < min || val64 > max)
510a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
511a095fadbSJean-Baptiste Maneyrol 
512a095fadbSJean-Baptiste Maneyrol 	/*
513a095fadbSJean-Baptiste Maneyrol 	 * convert rad/s to dps then to raw value
514a095fadbSJean-Baptiste Maneyrol 	 * rad to dps: 180 / Pi
515a095fadbSJean-Baptiste Maneyrol 	 * dps to raw 12 bits signed, max 64: 2048 / 64
516a095fadbSJean-Baptiste Maneyrol 	 * val in nano (1000000000)
517a095fadbSJean-Baptiste Maneyrol 	 * val * 180 * 2048 / (Pi * 1000000000 * 64)
518a095fadbSJean-Baptiste Maneyrol 	 */
519a095fadbSJean-Baptiste Maneyrol 	val64 = val64 * 180LL * 2048LL;
520a095fadbSJean-Baptiste Maneyrol 	/* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */
521a095fadbSJean-Baptiste Maneyrol 	if (val64 >= 0)
522a095fadbSJean-Baptiste Maneyrol 		val64 += 3141592653LL * 64LL / 2LL;
523a095fadbSJean-Baptiste Maneyrol 	else
524a095fadbSJean-Baptiste Maneyrol 		val64 -= 3141592653LL * 64LL / 2LL;
525a095fadbSJean-Baptiste Maneyrol 	offset = div64_s64(val64, 3141592653LL * 64LL);
526a095fadbSJean-Baptiste Maneyrol 
527a095fadbSJean-Baptiste Maneyrol 	/* clamp value limited to 12 bits signed */
528a095fadbSJean-Baptiste Maneyrol 	if (offset < -2048)
529a095fadbSJean-Baptiste Maneyrol 		offset = -2048;
530a095fadbSJean-Baptiste Maneyrol 	else if (offset > 2047)
531a095fadbSJean-Baptiste Maneyrol 		offset = 2047;
532a095fadbSJean-Baptiste Maneyrol 
533a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
534a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
535a095fadbSJean-Baptiste Maneyrol 
536a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
537a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
538a095fadbSJean-Baptiste Maneyrol 		/* OFFSET_USER1 register is shared */
539a095fadbSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
540a095fadbSJean-Baptiste Maneyrol 				  &regval);
541a095fadbSJean-Baptiste Maneyrol 		if (ret)
542a095fadbSJean-Baptiste Maneyrol 			goto out_unlock;
543a095fadbSJean-Baptiste Maneyrol 		st->buffer[0] = offset & 0xFF;
544a095fadbSJean-Baptiste Maneyrol 		st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
545a095fadbSJean-Baptiste Maneyrol 		break;
546a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
547a095fadbSJean-Baptiste Maneyrol 		/* OFFSET_USER1 register is shared */
548a095fadbSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
549a095fadbSJean-Baptiste Maneyrol 				  &regval);
550a095fadbSJean-Baptiste Maneyrol 		if (ret)
551a095fadbSJean-Baptiste Maneyrol 			goto out_unlock;
552a095fadbSJean-Baptiste Maneyrol 		st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
553a095fadbSJean-Baptiste Maneyrol 		st->buffer[1] = offset & 0xFF;
554a095fadbSJean-Baptiste Maneyrol 		break;
555a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
556a095fadbSJean-Baptiste Maneyrol 		/* OFFSET_USER4 register is shared */
557a095fadbSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
558a095fadbSJean-Baptiste Maneyrol 				  &regval);
559a095fadbSJean-Baptiste Maneyrol 		if (ret)
560a095fadbSJean-Baptiste Maneyrol 			goto out_unlock;
561a095fadbSJean-Baptiste Maneyrol 		st->buffer[0] = offset & 0xFF;
562a095fadbSJean-Baptiste Maneyrol 		st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
563a095fadbSJean-Baptiste Maneyrol 		break;
564a095fadbSJean-Baptiste Maneyrol 	default:
565a095fadbSJean-Baptiste Maneyrol 		ret = -EINVAL;
566a095fadbSJean-Baptiste Maneyrol 		goto out_unlock;
567a095fadbSJean-Baptiste Maneyrol 	}
568a095fadbSJean-Baptiste Maneyrol 
569a095fadbSJean-Baptiste Maneyrol 	ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
570a095fadbSJean-Baptiste Maneyrol 
571a095fadbSJean-Baptiste Maneyrol out_unlock:
572a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
573a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
574a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
575a095fadbSJean-Baptiste Maneyrol 	return ret;
576a095fadbSJean-Baptiste Maneyrol }
577a095fadbSJean-Baptiste Maneyrol 
578a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_raw(struct iio_dev *indio_dev,
579a095fadbSJean-Baptiste Maneyrol 				      struct iio_chan_spec const *chan,
580a095fadbSJean-Baptiste Maneyrol 				      int *val, int *val2, long mask)
581a095fadbSJean-Baptiste Maneyrol {
582a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
583a095fadbSJean-Baptiste Maneyrol 	int16_t data;
584a095fadbSJean-Baptiste Maneyrol 	int ret;
585a095fadbSJean-Baptiste Maneyrol 
586bc3eb020SJean-Baptiste Maneyrol 	switch (chan->type) {
587bc3eb020SJean-Baptiste Maneyrol 	case IIO_ANGL_VEL:
588bc3eb020SJean-Baptiste Maneyrol 		break;
589bc3eb020SJean-Baptiste Maneyrol 	case IIO_TEMP:
590bc3eb020SJean-Baptiste Maneyrol 		return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
591bc3eb020SJean-Baptiste Maneyrol 	default:
592a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
593bc3eb020SJean-Baptiste Maneyrol 	}
594a095fadbSJean-Baptiste Maneyrol 
595a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
596a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_RAW:
597a095fadbSJean-Baptiste Maneyrol 		ret = iio_device_claim_direct_mode(indio_dev);
598a095fadbSJean-Baptiste Maneyrol 		if (ret)
599a095fadbSJean-Baptiste Maneyrol 			return ret;
600a095fadbSJean-Baptiste Maneyrol 		ret = inv_icm42600_gyro_read_sensor(st, chan, &data);
601a095fadbSJean-Baptiste Maneyrol 		iio_device_release_direct_mode(indio_dev);
602a095fadbSJean-Baptiste Maneyrol 		if (ret)
603a095fadbSJean-Baptiste Maneyrol 			return ret;
604a095fadbSJean-Baptiste Maneyrol 		*val = data;
605a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT;
606a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
607*a1432b5bSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_read_scale(indio_dev, val, val2);
608a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
609a095fadbSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_read_odr(st, val, val2);
610a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
611a095fadbSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_read_offset(st, chan, val, val2);
612a095fadbSJean-Baptiste Maneyrol 	default:
613a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
614a095fadbSJean-Baptiste Maneyrol 	}
615a095fadbSJean-Baptiste Maneyrol }
616a095fadbSJean-Baptiste Maneyrol 
617a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev,
618a095fadbSJean-Baptiste Maneyrol 					struct iio_chan_spec const *chan,
619a095fadbSJean-Baptiste Maneyrol 					const int **vals,
620a095fadbSJean-Baptiste Maneyrol 					int *type, int *length, long mask)
621a095fadbSJean-Baptiste Maneyrol {
622*a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
623*a1432b5bSJean-Baptiste Maneyrol 
624a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
625a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
626a095fadbSJean-Baptiste Maneyrol 
627a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
628a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
629*a1432b5bSJean-Baptiste Maneyrol 		*vals = gyro_st->scales;
630a095fadbSJean-Baptiste Maneyrol 		*type = IIO_VAL_INT_PLUS_NANO;
631*a1432b5bSJean-Baptiste Maneyrol 		*length = gyro_st->scales_len;
632a095fadbSJean-Baptiste Maneyrol 		return IIO_AVAIL_LIST;
633a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
634a095fadbSJean-Baptiste Maneyrol 		*vals = inv_icm42600_gyro_odr;
635a095fadbSJean-Baptiste Maneyrol 		*type = IIO_VAL_INT_PLUS_MICRO;
636a095fadbSJean-Baptiste Maneyrol 		*length = ARRAY_SIZE(inv_icm42600_gyro_odr);
637a095fadbSJean-Baptiste Maneyrol 		return IIO_AVAIL_LIST;
638a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
639a095fadbSJean-Baptiste Maneyrol 		*vals = inv_icm42600_gyro_calibbias;
640a095fadbSJean-Baptiste Maneyrol 		*type = IIO_VAL_INT_PLUS_NANO;
641a095fadbSJean-Baptiste Maneyrol 		return IIO_AVAIL_RANGE;
642a095fadbSJean-Baptiste Maneyrol 	default:
643a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
644a095fadbSJean-Baptiste Maneyrol 	}
645a095fadbSJean-Baptiste Maneyrol }
646a095fadbSJean-Baptiste Maneyrol 
647a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev,
648a095fadbSJean-Baptiste Maneyrol 				       struct iio_chan_spec const *chan,
649a095fadbSJean-Baptiste Maneyrol 				       int val, int val2, long mask)
650a095fadbSJean-Baptiste Maneyrol {
651a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
652a095fadbSJean-Baptiste Maneyrol 	int ret;
653a095fadbSJean-Baptiste Maneyrol 
654a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
655a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
656a095fadbSJean-Baptiste Maneyrol 
657a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
658a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
659a095fadbSJean-Baptiste Maneyrol 		ret = iio_device_claim_direct_mode(indio_dev);
660a095fadbSJean-Baptiste Maneyrol 		if (ret)
661a095fadbSJean-Baptiste Maneyrol 			return ret;
662*a1432b5bSJean-Baptiste Maneyrol 		ret = inv_icm42600_gyro_write_scale(indio_dev, val, val2);
663a095fadbSJean-Baptiste Maneyrol 		iio_device_release_direct_mode(indio_dev);
664a095fadbSJean-Baptiste Maneyrol 		return ret;
665a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
666ec74ae9fSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_write_odr(indio_dev, val, val2);
667a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
668a095fadbSJean-Baptiste Maneyrol 		ret = iio_device_claim_direct_mode(indio_dev);
669a095fadbSJean-Baptiste Maneyrol 		if (ret)
670a095fadbSJean-Baptiste Maneyrol 			return ret;
671a095fadbSJean-Baptiste Maneyrol 		ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
672a095fadbSJean-Baptiste Maneyrol 		iio_device_release_direct_mode(indio_dev);
673a095fadbSJean-Baptiste Maneyrol 		return ret;
674a095fadbSJean-Baptiste Maneyrol 	default:
675a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
676a095fadbSJean-Baptiste Maneyrol 	}
677a095fadbSJean-Baptiste Maneyrol }
678a095fadbSJean-Baptiste Maneyrol 
679a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
680a095fadbSJean-Baptiste Maneyrol 					       struct iio_chan_spec const *chan,
681a095fadbSJean-Baptiste Maneyrol 					       long mask)
682a095fadbSJean-Baptiste Maneyrol {
683a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
684a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
685a095fadbSJean-Baptiste Maneyrol 
686a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
687a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
688a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT_PLUS_NANO;
689a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
690a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT_PLUS_MICRO;
691a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
692a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT_PLUS_NANO;
693a095fadbSJean-Baptiste Maneyrol 	default:
694a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
695a095fadbSJean-Baptiste Maneyrol 	}
696a095fadbSJean-Baptiste Maneyrol }
697a095fadbSJean-Baptiste Maneyrol 
6987f85e42aSJean-Baptiste Maneyrol static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
6997f85e42aSJean-Baptiste Maneyrol 						  unsigned int val)
7007f85e42aSJean-Baptiste Maneyrol {
7017f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
7027f85e42aSJean-Baptiste Maneyrol 	int ret;
7037f85e42aSJean-Baptiste Maneyrol 
7047f85e42aSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
7057f85e42aSJean-Baptiste Maneyrol 
7067f85e42aSJean-Baptiste Maneyrol 	st->fifo.watermark.gyro = val;
7077f85e42aSJean-Baptiste Maneyrol 	ret = inv_icm42600_buffer_update_watermark(st);
7087f85e42aSJean-Baptiste Maneyrol 
7097f85e42aSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
7107f85e42aSJean-Baptiste Maneyrol 
7117f85e42aSJean-Baptiste Maneyrol 	return ret;
7127f85e42aSJean-Baptiste Maneyrol }
7137f85e42aSJean-Baptiste Maneyrol 
7147f85e42aSJean-Baptiste Maneyrol static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
7157f85e42aSJean-Baptiste Maneyrol 					  unsigned int count)
7167f85e42aSJean-Baptiste Maneyrol {
7177f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
7187f85e42aSJean-Baptiste Maneyrol 	int ret;
7197f85e42aSJean-Baptiste Maneyrol 
7207f85e42aSJean-Baptiste Maneyrol 	if (count == 0)
7217f85e42aSJean-Baptiste Maneyrol 		return 0;
7227f85e42aSJean-Baptiste Maneyrol 
7237f85e42aSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
7247f85e42aSJean-Baptiste Maneyrol 
7257f85e42aSJean-Baptiste Maneyrol 	ret = inv_icm42600_buffer_hwfifo_flush(st, count);
7267f85e42aSJean-Baptiste Maneyrol 	if (!ret)
7277f85e42aSJean-Baptiste Maneyrol 		ret = st->fifo.nb.gyro;
7287f85e42aSJean-Baptiste Maneyrol 
7297f85e42aSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
7307f85e42aSJean-Baptiste Maneyrol 
7317f85e42aSJean-Baptiste Maneyrol 	return ret;
7327f85e42aSJean-Baptiste Maneyrol }
7337f85e42aSJean-Baptiste Maneyrol 
734a095fadbSJean-Baptiste Maneyrol static const struct iio_info inv_icm42600_gyro_info = {
735a095fadbSJean-Baptiste Maneyrol 	.read_raw = inv_icm42600_gyro_read_raw,
736a095fadbSJean-Baptiste Maneyrol 	.read_avail = inv_icm42600_gyro_read_avail,
737a095fadbSJean-Baptiste Maneyrol 	.write_raw = inv_icm42600_gyro_write_raw,
738a095fadbSJean-Baptiste Maneyrol 	.write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt,
739a095fadbSJean-Baptiste Maneyrol 	.debugfs_reg_access = inv_icm42600_debugfs_reg,
7407f85e42aSJean-Baptiste Maneyrol 	.update_scan_mode = inv_icm42600_gyro_update_scan_mode,
7417f85e42aSJean-Baptiste Maneyrol 	.hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark,
7427f85e42aSJean-Baptiste Maneyrol 	.hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush,
743a095fadbSJean-Baptiste Maneyrol };
744a095fadbSJean-Baptiste Maneyrol 
745a095fadbSJean-Baptiste Maneyrol struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
746a095fadbSJean-Baptiste Maneyrol {
747a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
748a095fadbSJean-Baptiste Maneyrol 	const char *name;
749*a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st;
7500ecc363cSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp_chip ts_chip;
751a095fadbSJean-Baptiste Maneyrol 	struct iio_dev *indio_dev;
752a095fadbSJean-Baptiste Maneyrol 	int ret;
753a095fadbSJean-Baptiste Maneyrol 
754a095fadbSJean-Baptiste Maneyrol 	name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
755a095fadbSJean-Baptiste Maneyrol 	if (!name)
756a095fadbSJean-Baptiste Maneyrol 		return ERR_PTR(-ENOMEM);
757a095fadbSJean-Baptiste Maneyrol 
758*a1432b5bSJean-Baptiste Maneyrol 	indio_dev = devm_iio_device_alloc(dev, sizeof(*gyro_st));
759a095fadbSJean-Baptiste Maneyrol 	if (!indio_dev)
760a095fadbSJean-Baptiste Maneyrol 		return ERR_PTR(-ENOMEM);
761*a1432b5bSJean-Baptiste Maneyrol 	gyro_st = iio_priv(indio_dev);
762*a1432b5bSJean-Baptiste Maneyrol 
763*a1432b5bSJean-Baptiste Maneyrol 	switch (st->chip) {
764*a1432b5bSJean-Baptiste Maneyrol 	case INV_CHIP_ICM42686:
765*a1432b5bSJean-Baptiste Maneyrol 		gyro_st->scales = inv_icm42686_gyro_scale;
766*a1432b5bSJean-Baptiste Maneyrol 		gyro_st->scales_len = ARRAY_SIZE(inv_icm42686_gyro_scale);
767*a1432b5bSJean-Baptiste Maneyrol 		break;
768*a1432b5bSJean-Baptiste Maneyrol 	default:
769*a1432b5bSJean-Baptiste Maneyrol 		gyro_st->scales = inv_icm42600_gyro_scale;
770*a1432b5bSJean-Baptiste Maneyrol 		gyro_st->scales_len = ARRAY_SIZE(inv_icm42600_gyro_scale);
771*a1432b5bSJean-Baptiste Maneyrol 		break;
772*a1432b5bSJean-Baptiste Maneyrol 	}
773a095fadbSJean-Baptiste Maneyrol 
7740ecc363cSJean-Baptiste Maneyrol 	/*
7750ecc363cSJean-Baptiste Maneyrol 	 * clock period is 32kHz (31250ns)
7760ecc363cSJean-Baptiste Maneyrol 	 * jitter is +/- 2% (20 per mille)
7770ecc363cSJean-Baptiste Maneyrol 	 */
7780ecc363cSJean-Baptiste Maneyrol 	ts_chip.clock_period = 31250;
7790ecc363cSJean-Baptiste Maneyrol 	ts_chip.jitter = 20;
7800ecc363cSJean-Baptiste Maneyrol 	ts_chip.init_period = inv_icm42600_odr_to_period(st->conf.accel.odr);
781*a1432b5bSJean-Baptiste Maneyrol 	inv_sensors_timestamp_init(&gyro_st->ts, &ts_chip);
782ec74ae9fSJean-Baptiste Maneyrol 
783a095fadbSJean-Baptiste Maneyrol 	iio_device_set_drvdata(indio_dev, st);
784a095fadbSJean-Baptiste Maneyrol 	indio_dev->name = name;
785a095fadbSJean-Baptiste Maneyrol 	indio_dev->info = &inv_icm42600_gyro_info;
78617395ce2SAlexandru Ardelean 	indio_dev->modes = INDIO_DIRECT_MODE;
787a095fadbSJean-Baptiste Maneyrol 	indio_dev->channels = inv_icm42600_gyro_channels;
788a095fadbSJean-Baptiste Maneyrol 	indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels);
7897f85e42aSJean-Baptiste Maneyrol 	indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks;
7907f85e42aSJean-Baptiste Maneyrol 	indio_dev->setup_ops = &inv_icm42600_buffer_ops;
7917f85e42aSJean-Baptiste Maneyrol 
79217395ce2SAlexandru Ardelean 	ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
79317395ce2SAlexandru Ardelean 					  &inv_icm42600_buffer_ops);
79417395ce2SAlexandru Ardelean 	if (ret)
79517395ce2SAlexandru Ardelean 		return ERR_PTR(ret);
796a095fadbSJean-Baptiste Maneyrol 
797a095fadbSJean-Baptiste Maneyrol 	ret = devm_iio_device_register(dev, indio_dev);
798a095fadbSJean-Baptiste Maneyrol 	if (ret)
799a095fadbSJean-Baptiste Maneyrol 		return ERR_PTR(ret);
800a095fadbSJean-Baptiste Maneyrol 
801a095fadbSJean-Baptiste Maneyrol 	return indio_dev;
802a095fadbSJean-Baptiste Maneyrol }
8037f85e42aSJean-Baptiste Maneyrol 
8047f85e42aSJean-Baptiste Maneyrol int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev)
8057f85e42aSJean-Baptiste Maneyrol {
8067f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
807*a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
808*a1432b5bSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp *ts = &gyro_st->ts;
8097f85e42aSJean-Baptiste Maneyrol 	ssize_t i, size;
810ec74ae9fSJean-Baptiste Maneyrol 	unsigned int no;
8117f85e42aSJean-Baptiste Maneyrol 	const void *accel, *gyro, *timestamp;
8127f85e42aSJean-Baptiste Maneyrol 	const int8_t *temp;
8137f85e42aSJean-Baptiste Maneyrol 	unsigned int odr;
814ec74ae9fSJean-Baptiste Maneyrol 	int64_t ts_val;
8157f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_gyro_buffer buffer;
8167f85e42aSJean-Baptiste Maneyrol 
8177f85e42aSJean-Baptiste Maneyrol 	/* parse all fifo packets */
818ec74ae9fSJean-Baptiste Maneyrol 	for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) {
8197f85e42aSJean-Baptiste Maneyrol 		size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
8207f85e42aSJean-Baptiste Maneyrol 				&accel, &gyro, &temp, &timestamp, &odr);
8217f85e42aSJean-Baptiste Maneyrol 		/* quit if error or FIFO is empty */
8227f85e42aSJean-Baptiste Maneyrol 		if (size <= 0)
8237f85e42aSJean-Baptiste Maneyrol 			return size;
8247f85e42aSJean-Baptiste Maneyrol 
8257f85e42aSJean-Baptiste Maneyrol 		/* skip packet if no gyro data or data is invalid */
8267f85e42aSJean-Baptiste Maneyrol 		if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
8277f85e42aSJean-Baptiste Maneyrol 			continue;
8287f85e42aSJean-Baptiste Maneyrol 
829ec74ae9fSJean-Baptiste Maneyrol 		/* update odr */
830ec74ae9fSJean-Baptiste Maneyrol 		if (odr & INV_ICM42600_SENSOR_GYRO)
8310ecc363cSJean-Baptiste Maneyrol 			inv_sensors_timestamp_apply_odr(ts, st->fifo.period,
832ec74ae9fSJean-Baptiste Maneyrol 							st->fifo.nb.total, no);
833ec74ae9fSJean-Baptiste Maneyrol 
8347f85e42aSJean-Baptiste Maneyrol 		/* buffer is copied to userspace, zeroing it to avoid any data leak */
8357f85e42aSJean-Baptiste Maneyrol 		memset(&buffer, 0, sizeof(buffer));
8367f85e42aSJean-Baptiste Maneyrol 		memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro));
8377f85e42aSJean-Baptiste Maneyrol 		/* convert 8 bits FIFO temperature in high resolution format */
8387f85e42aSJean-Baptiste Maneyrol 		buffer.temp = temp ? (*temp * 64) : 0;
8390ecc363cSJean-Baptiste Maneyrol 		ts_val = inv_sensors_timestamp_pop(ts);
840ec74ae9fSJean-Baptiste Maneyrol 		iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val);
8417f85e42aSJean-Baptiste Maneyrol 	}
8427f85e42aSJean-Baptiste Maneyrol 
8437f85e42aSJean-Baptiste Maneyrol 	return 0;
8447f85e42aSJean-Baptiste Maneyrol }
845