xref: /linux/drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
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),
60*70788d26SDavid Lechner 	{ }
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 	s16 temp;
8127e6ddf2SJonathan Cameron 	aligned_s64 timestamp;
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 */
inv_icm42600_gyro_update_scan_mode(struct iio_dev * indio_dev,const unsigned long * scan_mask)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);
1027f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
1037f85e42aSJean-Baptiste Maneyrol 	unsigned int fifo_en = 0;
1047f85e42aSJean-Baptiste Maneyrol 	unsigned int sleep_gyro = 0;
1057f85e42aSJean-Baptiste Maneyrol 	unsigned int sleep_temp = 0;
1067f85e42aSJean-Baptiste Maneyrol 	unsigned int sleep;
1077f85e42aSJean-Baptiste Maneyrol 	int ret;
1087f85e42aSJean-Baptiste Maneyrol 
1097f85e42aSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
1107f85e42aSJean-Baptiste Maneyrol 
1117f85e42aSJean-Baptiste Maneyrol 	if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
1127f85e42aSJean-Baptiste Maneyrol 		/* enable temp sensor */
1137f85e42aSJean-Baptiste Maneyrol 		ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
1147f85e42aSJean-Baptiste Maneyrol 		if (ret)
1157f85e42aSJean-Baptiste Maneyrol 			goto out_unlock;
1167f85e42aSJean-Baptiste Maneyrol 		fifo_en |= INV_ICM42600_SENSOR_TEMP;
1177f85e42aSJean-Baptiste Maneyrol 	}
1187f85e42aSJean-Baptiste Maneyrol 
1197f85e42aSJean-Baptiste Maneyrol 	if (*scan_mask & INV_ICM42600_SCAN_MASK_GYRO_3AXIS) {
1207f85e42aSJean-Baptiste Maneyrol 		/* enable gyro sensor */
1217f85e42aSJean-Baptiste Maneyrol 		conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
1227f85e42aSJean-Baptiste Maneyrol 		ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_gyro);
1237f85e42aSJean-Baptiste Maneyrol 		if (ret)
1247f85e42aSJean-Baptiste Maneyrol 			goto out_unlock;
1257f85e42aSJean-Baptiste Maneyrol 		fifo_en |= INV_ICM42600_SENSOR_GYRO;
1267f85e42aSJean-Baptiste Maneyrol 	}
1277f85e42aSJean-Baptiste Maneyrol 
1287f85e42aSJean-Baptiste Maneyrol 	/* update data FIFO write */
1297f85e42aSJean-Baptiste Maneyrol 	ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
1307f85e42aSJean-Baptiste Maneyrol 
1317f85e42aSJean-Baptiste Maneyrol out_unlock:
1327f85e42aSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
1337f85e42aSJean-Baptiste Maneyrol 	/* sleep maximum required time */
134c788b9e5SBragatheswaran Manickavel 	sleep = max(sleep_gyro, sleep_temp);
1357f85e42aSJean-Baptiste Maneyrol 	if (sleep)
1367f85e42aSJean-Baptiste Maneyrol 		msleep(sleep);
1377f85e42aSJean-Baptiste Maneyrol 	return ret;
1387f85e42aSJean-Baptiste Maneyrol }
1397f85e42aSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_sensor(struct inv_icm42600_state * st,struct iio_chan_spec const * chan,s16 * val)140a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
141a095fadbSJean-Baptiste Maneyrol 					 struct iio_chan_spec const *chan,
142a095fadbSJean-Baptiste Maneyrol 					 s16 *val)
143a095fadbSJean-Baptiste Maneyrol {
144a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
145a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
146a095fadbSJean-Baptiste Maneyrol 	unsigned int reg;
147a095fadbSJean-Baptiste Maneyrol 	__be16 *data;
148a095fadbSJean-Baptiste Maneyrol 	int ret;
149a095fadbSJean-Baptiste Maneyrol 
150a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
151a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
152a095fadbSJean-Baptiste Maneyrol 
153a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
154a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
155a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_GYRO_DATA_X;
156a095fadbSJean-Baptiste Maneyrol 		break;
157a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
158a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_GYRO_DATA_Y;
159a095fadbSJean-Baptiste Maneyrol 		break;
160a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
161a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_GYRO_DATA_Z;
162a095fadbSJean-Baptiste Maneyrol 		break;
163a095fadbSJean-Baptiste Maneyrol 	default:
164a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
165a095fadbSJean-Baptiste Maneyrol 	}
166a095fadbSJean-Baptiste Maneyrol 
167a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
168a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
169a095fadbSJean-Baptiste Maneyrol 
170a095fadbSJean-Baptiste Maneyrol 	/* enable gyro sensor */
171a095fadbSJean-Baptiste Maneyrol 	conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
172a095fadbSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
173a095fadbSJean-Baptiste Maneyrol 	if (ret)
174a095fadbSJean-Baptiste Maneyrol 		goto exit;
175a095fadbSJean-Baptiste Maneyrol 
176a095fadbSJean-Baptiste Maneyrol 	/* read gyro register data */
177a095fadbSJean-Baptiste Maneyrol 	data = (__be16 *)&st->buffer[0];
178a095fadbSJean-Baptiste Maneyrol 	ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
179a095fadbSJean-Baptiste Maneyrol 	if (ret)
180a095fadbSJean-Baptiste Maneyrol 		goto exit;
181a095fadbSJean-Baptiste Maneyrol 
182a095fadbSJean-Baptiste Maneyrol 	*val = (s16)be16_to_cpup(data);
183a095fadbSJean-Baptiste Maneyrol 	if (*val == INV_ICM42600_DATA_INVALID)
184a095fadbSJean-Baptiste Maneyrol 		ret = -EINVAL;
185a095fadbSJean-Baptiste Maneyrol exit:
186a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
187a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
188a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
189a095fadbSJean-Baptiste Maneyrol 	return ret;
190a095fadbSJean-Baptiste Maneyrol }
191a095fadbSJean-Baptiste Maneyrol 
192a095fadbSJean-Baptiste Maneyrol /* IIO format int + nano */
193a095fadbSJean-Baptiste Maneyrol static const int inv_icm42600_gyro_scale[] = {
194a095fadbSJean-Baptiste Maneyrol 	/* +/- 2000dps => 0.001065264 rad/s */
195a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_2000DPS] = 0,
196a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_2000DPS + 1] = 1065264,
197a095fadbSJean-Baptiste Maneyrol 	/* +/- 1000dps => 0.000532632 rad/s */
198a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_1000DPS] = 0,
199a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_1000DPS + 1] = 532632,
200a095fadbSJean-Baptiste Maneyrol 	/* +/- 500dps => 0.000266316 rad/s */
201a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_500DPS] = 0,
202a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_500DPS + 1] = 266316,
203a095fadbSJean-Baptiste Maneyrol 	/* +/- 250dps => 0.000133158 rad/s */
204a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_250DPS] = 0,
205a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_250DPS + 1] = 133158,
206a095fadbSJean-Baptiste Maneyrol 	/* +/- 125dps => 0.000066579 rad/s */
207a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_125DPS] = 0,
208a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_125DPS + 1] = 66579,
209a095fadbSJean-Baptiste Maneyrol 	/* +/- 62.5dps => 0.000033290 rad/s */
210a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_62_5DPS] = 0,
211a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_62_5DPS + 1] = 33290,
212a095fadbSJean-Baptiste Maneyrol 	/* +/- 31.25dps => 0.000016645 rad/s */
213a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_31_25DPS] = 0,
214a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_31_25DPS + 1] = 16645,
215a095fadbSJean-Baptiste Maneyrol 	/* +/- 15.625dps => 0.000008322 rad/s */
216a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_15_625DPS] = 0,
217a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_15_625DPS + 1] = 8322,
218a095fadbSJean-Baptiste Maneyrol };
219a1432b5bSJean-Baptiste Maneyrol static const int inv_icm42686_gyro_scale[] = {
220a1432b5bSJean-Baptiste Maneyrol 	/* +/- 4000dps => 0.002130529 rad/s */
221a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_4000DPS] = 0,
222a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_4000DPS + 1] = 2130529,
223a1432b5bSJean-Baptiste Maneyrol 	/* +/- 2000dps => 0.001065264 rad/s */
224a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_2000DPS] = 0,
225a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_2000DPS + 1] = 1065264,
226a1432b5bSJean-Baptiste Maneyrol 	/* +/- 1000dps => 0.000532632 rad/s */
227a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_1000DPS] = 0,
228a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_1000DPS + 1] = 532632,
229a1432b5bSJean-Baptiste Maneyrol 	/* +/- 500dps => 0.000266316 rad/s */
230a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_500DPS] = 0,
231a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_500DPS + 1] = 266316,
232a1432b5bSJean-Baptiste Maneyrol 	/* +/- 250dps => 0.000133158 rad/s */
233a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_250DPS] = 0,
234a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_250DPS + 1] = 133158,
235a1432b5bSJean-Baptiste Maneyrol 	/* +/- 125dps => 0.000066579 rad/s */
236a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_125DPS] = 0,
237a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_125DPS + 1] = 66579,
238a1432b5bSJean-Baptiste Maneyrol 	/* +/- 62.5dps => 0.000033290 rad/s */
239a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_62_5DPS] = 0,
240a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_62_5DPS + 1] = 33290,
241a1432b5bSJean-Baptiste Maneyrol 	/* +/- 31.25dps => 0.000016645 rad/s */
242a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_31_25DPS] = 0,
243a1432b5bSJean-Baptiste Maneyrol 	[2 * INV_ICM42686_GYRO_FS_31_25DPS + 1] = 16645,
244a1432b5bSJean-Baptiste Maneyrol };
245a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_scale(struct iio_dev * indio_dev,int * val,int * val2)246a1432b5bSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_scale(struct iio_dev *indio_dev,
247a095fadbSJean-Baptiste Maneyrol 					int *val, int *val2)
248a095fadbSJean-Baptiste Maneyrol {
249a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
250a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
251a095fadbSJean-Baptiste Maneyrol 	unsigned int idx;
252a095fadbSJean-Baptiste Maneyrol 
253a095fadbSJean-Baptiste Maneyrol 	idx = st->conf.gyro.fs;
254a095fadbSJean-Baptiste Maneyrol 
255a1432b5bSJean-Baptiste Maneyrol 	*val = gyro_st->scales[2 * idx];
256a1432b5bSJean-Baptiste Maneyrol 	*val2 = gyro_st->scales[2 * idx + 1];
257a095fadbSJean-Baptiste Maneyrol 	return IIO_VAL_INT_PLUS_NANO;
258a095fadbSJean-Baptiste Maneyrol }
259a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_scale(struct iio_dev * indio_dev,int val,int val2)260a1432b5bSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_scale(struct iio_dev *indio_dev,
261a095fadbSJean-Baptiste Maneyrol 					 int val, int val2)
262a095fadbSJean-Baptiste Maneyrol {
263a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
264a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
265a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
266a095fadbSJean-Baptiste Maneyrol 	unsigned int idx;
267a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
268a095fadbSJean-Baptiste Maneyrol 	int ret;
269a095fadbSJean-Baptiste Maneyrol 
270a1432b5bSJean-Baptiste Maneyrol 	for (idx = 0; idx < gyro_st->scales_len; idx += 2) {
271a1432b5bSJean-Baptiste Maneyrol 		if (val == gyro_st->scales[idx] &&
272a1432b5bSJean-Baptiste Maneyrol 		    val2 == gyro_st->scales[idx + 1])
273a095fadbSJean-Baptiste Maneyrol 			break;
274a095fadbSJean-Baptiste Maneyrol 	}
275a1432b5bSJean-Baptiste Maneyrol 	if (idx >= gyro_st->scales_len)
276a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
277a095fadbSJean-Baptiste Maneyrol 
278a095fadbSJean-Baptiste Maneyrol 	conf.fs = idx / 2;
279a095fadbSJean-Baptiste Maneyrol 
280a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
281a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
282a095fadbSJean-Baptiste Maneyrol 
283a095fadbSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
284a095fadbSJean-Baptiste Maneyrol 
285a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
286a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
287a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
288a095fadbSJean-Baptiste Maneyrol 
289a095fadbSJean-Baptiste Maneyrol 	return ret;
290a095fadbSJean-Baptiste Maneyrol }
291a095fadbSJean-Baptiste Maneyrol 
292a095fadbSJean-Baptiste Maneyrol /* IIO format int + micro */
293a095fadbSJean-Baptiste Maneyrol static const int inv_icm42600_gyro_odr[] = {
294a095fadbSJean-Baptiste Maneyrol 	/* 12.5Hz */
295a095fadbSJean-Baptiste Maneyrol 	12, 500000,
296a095fadbSJean-Baptiste Maneyrol 	/* 25Hz */
297a095fadbSJean-Baptiste Maneyrol 	25, 0,
298a095fadbSJean-Baptiste Maneyrol 	/* 50Hz */
299a095fadbSJean-Baptiste Maneyrol 	50, 0,
300a095fadbSJean-Baptiste Maneyrol 	/* 100Hz */
301a095fadbSJean-Baptiste Maneyrol 	100, 0,
302a095fadbSJean-Baptiste Maneyrol 	/* 200Hz */
303a095fadbSJean-Baptiste Maneyrol 	200, 0,
304a095fadbSJean-Baptiste Maneyrol 	/* 1kHz */
305a095fadbSJean-Baptiste Maneyrol 	1000, 0,
306a095fadbSJean-Baptiste Maneyrol 	/* 2kHz */
307a095fadbSJean-Baptiste Maneyrol 	2000, 0,
308a095fadbSJean-Baptiste Maneyrol 	/* 4kHz */
309a095fadbSJean-Baptiste Maneyrol 	4000, 0,
310a095fadbSJean-Baptiste Maneyrol };
311a095fadbSJean-Baptiste Maneyrol 
312a095fadbSJean-Baptiste Maneyrol static const int inv_icm42600_gyro_odr_conv[] = {
313a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_12_5HZ,
314a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_25HZ,
315a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_50HZ,
316a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_100HZ,
317a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_200HZ,
318a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_1KHZ_LN,
319a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_2KHZ_LN,
320a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_4KHZ_LN,
321a095fadbSJean-Baptiste Maneyrol };
322a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_odr(struct inv_icm42600_state * st,int * val,int * val2)323a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st,
324a095fadbSJean-Baptiste Maneyrol 				      int *val, int *val2)
325a095fadbSJean-Baptiste Maneyrol {
326a095fadbSJean-Baptiste Maneyrol 	unsigned int odr;
327a095fadbSJean-Baptiste Maneyrol 	unsigned int i;
328a095fadbSJean-Baptiste Maneyrol 
329a095fadbSJean-Baptiste Maneyrol 	odr = st->conf.gyro.odr;
330a095fadbSJean-Baptiste Maneyrol 
331a095fadbSJean-Baptiste Maneyrol 	for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) {
332a095fadbSJean-Baptiste Maneyrol 		if (inv_icm42600_gyro_odr_conv[i] == odr)
333a095fadbSJean-Baptiste Maneyrol 			break;
334a095fadbSJean-Baptiste Maneyrol 	}
335a095fadbSJean-Baptiste Maneyrol 	if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv))
336a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
337a095fadbSJean-Baptiste Maneyrol 
338a095fadbSJean-Baptiste Maneyrol 	*val = inv_icm42600_gyro_odr[2 * i];
339a095fadbSJean-Baptiste Maneyrol 	*val2 = inv_icm42600_gyro_odr[2 * i + 1];
340a095fadbSJean-Baptiste Maneyrol 
341a095fadbSJean-Baptiste Maneyrol 	return IIO_VAL_INT_PLUS_MICRO;
342a095fadbSJean-Baptiste Maneyrol }
343a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_odr(struct iio_dev * indio_dev,int val,int val2)344ec74ae9fSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev,
345a095fadbSJean-Baptiste Maneyrol 				       int val, int val2)
346a095fadbSJean-Baptiste Maneyrol {
347ec74ae9fSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
348a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
349a1432b5bSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp *ts = &gyro_st->ts;
350a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
351a095fadbSJean-Baptiste Maneyrol 	unsigned int idx;
352a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
353a095fadbSJean-Baptiste Maneyrol 	int ret;
354a095fadbSJean-Baptiste Maneyrol 
355a095fadbSJean-Baptiste Maneyrol 	for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_odr); idx += 2) {
356a095fadbSJean-Baptiste Maneyrol 		if (val == inv_icm42600_gyro_odr[idx] &&
357a095fadbSJean-Baptiste Maneyrol 		    val2 == inv_icm42600_gyro_odr[idx + 1])
358a095fadbSJean-Baptiste Maneyrol 			break;
359a095fadbSJean-Baptiste Maneyrol 	}
360a095fadbSJean-Baptiste Maneyrol 	if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr))
361a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
362a095fadbSJean-Baptiste Maneyrol 
363a095fadbSJean-Baptiste Maneyrol 	conf.odr = inv_icm42600_gyro_odr_conv[idx / 2];
364a095fadbSJean-Baptiste Maneyrol 
365a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
366a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
367a095fadbSJean-Baptiste Maneyrol 
3680ecc363cSJean-Baptiste Maneyrol 	ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr),
369ec74ae9fSJean-Baptiste Maneyrol 					       iio_buffer_enabled(indio_dev));
370ec74ae9fSJean-Baptiste Maneyrol 	if (ret)
371ec74ae9fSJean-Baptiste Maneyrol 		goto out_unlock;
372ec74ae9fSJean-Baptiste Maneyrol 
373a095fadbSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
3747f85e42aSJean-Baptiste Maneyrol 	if (ret)
3757f85e42aSJean-Baptiste Maneyrol 		goto out_unlock;
3767f85e42aSJean-Baptiste Maneyrol 	inv_icm42600_buffer_update_fifo_period(st);
3777f85e42aSJean-Baptiste Maneyrol 	inv_icm42600_buffer_update_watermark(st);
378a095fadbSJean-Baptiste Maneyrol 
3797f85e42aSJean-Baptiste Maneyrol out_unlock:
380a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
381a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
382a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
383a095fadbSJean-Baptiste Maneyrol 
384a095fadbSJean-Baptiste Maneyrol 	return ret;
385a095fadbSJean-Baptiste Maneyrol }
386a095fadbSJean-Baptiste Maneyrol 
387a095fadbSJean-Baptiste Maneyrol /*
388a095fadbSJean-Baptiste Maneyrol  * Calibration bias values, IIO range format int + nano.
389a095fadbSJean-Baptiste Maneyrol  * Value is limited to +/-64dps coded on 12 bits signed. Step is 1/32 dps.
390a095fadbSJean-Baptiste Maneyrol  */
391a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_calibbias[] = {
392a095fadbSJean-Baptiste Maneyrol 	-1, 117010721,		/* min: -1.117010721 rad/s */
393a095fadbSJean-Baptiste Maneyrol 	0, 545415,		/* step: 0.000545415 rad/s */
394a095fadbSJean-Baptiste Maneyrol 	1, 116465306,		/* max: 1.116465306 rad/s */
395a095fadbSJean-Baptiste Maneyrol };
396a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_offset(struct inv_icm42600_state * st,struct iio_chan_spec const * chan,int * val,int * val2)397a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st,
398a095fadbSJean-Baptiste Maneyrol 					 struct iio_chan_spec const *chan,
399a095fadbSJean-Baptiste Maneyrol 					 int *val, int *val2)
400a095fadbSJean-Baptiste Maneyrol {
401a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
402a095fadbSJean-Baptiste Maneyrol 	s64 val64;
403a095fadbSJean-Baptiste Maneyrol 	s32 bias;
404a095fadbSJean-Baptiste Maneyrol 	unsigned int reg;
405a095fadbSJean-Baptiste Maneyrol 	s16 offset;
406a095fadbSJean-Baptiste Maneyrol 	u8 data[2];
407a095fadbSJean-Baptiste Maneyrol 	int ret;
408a095fadbSJean-Baptiste Maneyrol 
409a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
410a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
411a095fadbSJean-Baptiste Maneyrol 
412a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
413a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
414a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER0;
415a095fadbSJean-Baptiste Maneyrol 		break;
416a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
417a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER1;
418a095fadbSJean-Baptiste Maneyrol 		break;
419a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
420a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER3;
421a095fadbSJean-Baptiste Maneyrol 		break;
422a095fadbSJean-Baptiste Maneyrol 	default:
423a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
424a095fadbSJean-Baptiste Maneyrol 	}
425a095fadbSJean-Baptiste Maneyrol 
426a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
427a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
428a095fadbSJean-Baptiste Maneyrol 
429a095fadbSJean-Baptiste Maneyrol 	ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data));
430a095fadbSJean-Baptiste Maneyrol 	memcpy(data, st->buffer, sizeof(data));
431a095fadbSJean-Baptiste Maneyrol 
432a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
433a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
434a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
435a095fadbSJean-Baptiste Maneyrol 	if (ret)
436a095fadbSJean-Baptiste Maneyrol 		return ret;
437a095fadbSJean-Baptiste Maneyrol 
438a095fadbSJean-Baptiste Maneyrol 	/* 12 bits signed value */
439a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
440a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
441a095fadbSJean-Baptiste Maneyrol 		offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
442a095fadbSJean-Baptiste Maneyrol 		break;
443a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
444a095fadbSJean-Baptiste Maneyrol 		offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
445a095fadbSJean-Baptiste Maneyrol 		break;
446a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
447a095fadbSJean-Baptiste Maneyrol 		offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
448a095fadbSJean-Baptiste Maneyrol 		break;
449a095fadbSJean-Baptiste Maneyrol 	default:
450a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
451a095fadbSJean-Baptiste Maneyrol 	}
452a095fadbSJean-Baptiste Maneyrol 
453a095fadbSJean-Baptiste Maneyrol 	/*
454a095fadbSJean-Baptiste Maneyrol 	 * convert raw offset to dps then to rad/s
455a095fadbSJean-Baptiste Maneyrol 	 * 12 bits signed raw max 64 to dps: 64 / 2048
456a095fadbSJean-Baptiste Maneyrol 	 * dps to rad: Pi / 180
457a095fadbSJean-Baptiste Maneyrol 	 * result in nano (1000000000)
458a095fadbSJean-Baptiste Maneyrol 	 * (offset * 64 * Pi * 1000000000) / (2048 * 180)
459a095fadbSJean-Baptiste Maneyrol 	 */
460a095fadbSJean-Baptiste Maneyrol 	val64 = (s64)offset * 64LL * 3141592653LL;
461a095fadbSJean-Baptiste Maneyrol 	/* for rounding, add + or - divisor (2048 * 180) divided by 2 */
462a095fadbSJean-Baptiste Maneyrol 	if (val64 >= 0)
463a095fadbSJean-Baptiste Maneyrol 		val64 += 2048 * 180 / 2;
464a095fadbSJean-Baptiste Maneyrol 	else
465a095fadbSJean-Baptiste Maneyrol 		val64 -= 2048 * 180 / 2;
466a095fadbSJean-Baptiste Maneyrol 	bias = div_s64(val64, 2048 * 180);
467a095fadbSJean-Baptiste Maneyrol 	*val = bias / 1000000000L;
468a095fadbSJean-Baptiste Maneyrol 	*val2 = bias % 1000000000L;
469a095fadbSJean-Baptiste Maneyrol 
470a095fadbSJean-Baptiste Maneyrol 	return IIO_VAL_INT_PLUS_NANO;
471a095fadbSJean-Baptiste Maneyrol }
472a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_offset(struct inv_icm42600_state * st,struct iio_chan_spec const * chan,int val,int val2)473a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st,
474a095fadbSJean-Baptiste Maneyrol 					  struct iio_chan_spec const *chan,
475a095fadbSJean-Baptiste Maneyrol 					  int val, int val2)
476a095fadbSJean-Baptiste Maneyrol {
477a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
478a095fadbSJean-Baptiste Maneyrol 	s64 val64, min, max;
479a095fadbSJean-Baptiste Maneyrol 	unsigned int reg, regval;
480a095fadbSJean-Baptiste Maneyrol 	s16 offset;
481a095fadbSJean-Baptiste Maneyrol 	int ret;
482a095fadbSJean-Baptiste Maneyrol 
483a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
484a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
485a095fadbSJean-Baptiste Maneyrol 
486a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
487a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
488a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER0;
489a095fadbSJean-Baptiste Maneyrol 		break;
490a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
491a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER1;
492a095fadbSJean-Baptiste Maneyrol 		break;
493a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
494a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER3;
495a095fadbSJean-Baptiste Maneyrol 		break;
496a095fadbSJean-Baptiste Maneyrol 	default:
497a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
498a095fadbSJean-Baptiste Maneyrol 	}
499a095fadbSJean-Baptiste Maneyrol 
500a095fadbSJean-Baptiste Maneyrol 	/* inv_icm42600_gyro_calibbias: min - step - max in nano */
501a095fadbSJean-Baptiste Maneyrol 	min = (s64)inv_icm42600_gyro_calibbias[0] * 1000000000LL +
502a095fadbSJean-Baptiste Maneyrol 	      (s64)inv_icm42600_gyro_calibbias[1];
503a095fadbSJean-Baptiste Maneyrol 	max = (s64)inv_icm42600_gyro_calibbias[4] * 1000000000LL +
504a095fadbSJean-Baptiste Maneyrol 	      (s64)inv_icm42600_gyro_calibbias[5];
505a095fadbSJean-Baptiste Maneyrol 	val64 = (s64)val * 1000000000LL + (s64)val2;
506a095fadbSJean-Baptiste Maneyrol 	if (val64 < min || val64 > max)
507a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
508a095fadbSJean-Baptiste Maneyrol 
509a095fadbSJean-Baptiste Maneyrol 	/*
510a095fadbSJean-Baptiste Maneyrol 	 * convert rad/s to dps then to raw value
511a095fadbSJean-Baptiste Maneyrol 	 * rad to dps: 180 / Pi
512a095fadbSJean-Baptiste Maneyrol 	 * dps to raw 12 bits signed, max 64: 2048 / 64
513a095fadbSJean-Baptiste Maneyrol 	 * val in nano (1000000000)
514a095fadbSJean-Baptiste Maneyrol 	 * val * 180 * 2048 / (Pi * 1000000000 * 64)
515a095fadbSJean-Baptiste Maneyrol 	 */
516a095fadbSJean-Baptiste Maneyrol 	val64 = val64 * 180LL * 2048LL;
517a095fadbSJean-Baptiste Maneyrol 	/* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */
518a095fadbSJean-Baptiste Maneyrol 	if (val64 >= 0)
519a095fadbSJean-Baptiste Maneyrol 		val64 += 3141592653LL * 64LL / 2LL;
520a095fadbSJean-Baptiste Maneyrol 	else
521a095fadbSJean-Baptiste Maneyrol 		val64 -= 3141592653LL * 64LL / 2LL;
522a095fadbSJean-Baptiste Maneyrol 	offset = div64_s64(val64, 3141592653LL * 64LL);
523a095fadbSJean-Baptiste Maneyrol 
524a095fadbSJean-Baptiste Maneyrol 	/* clamp value limited to 12 bits signed */
525a095fadbSJean-Baptiste Maneyrol 	if (offset < -2048)
526a095fadbSJean-Baptiste Maneyrol 		offset = -2048;
527a095fadbSJean-Baptiste Maneyrol 	else if (offset > 2047)
528a095fadbSJean-Baptiste Maneyrol 		offset = 2047;
529a095fadbSJean-Baptiste Maneyrol 
530a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
531a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
532a095fadbSJean-Baptiste Maneyrol 
533a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
534a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
535a095fadbSJean-Baptiste Maneyrol 		/* OFFSET_USER1 register is shared */
536a095fadbSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
537a095fadbSJean-Baptiste Maneyrol 				  &regval);
538a095fadbSJean-Baptiste Maneyrol 		if (ret)
539a095fadbSJean-Baptiste Maneyrol 			goto out_unlock;
540a095fadbSJean-Baptiste Maneyrol 		st->buffer[0] = offset & 0xFF;
541a095fadbSJean-Baptiste Maneyrol 		st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
542a095fadbSJean-Baptiste Maneyrol 		break;
543a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
544a095fadbSJean-Baptiste Maneyrol 		/* OFFSET_USER1 register is shared */
545a095fadbSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
546a095fadbSJean-Baptiste Maneyrol 				  &regval);
547a095fadbSJean-Baptiste Maneyrol 		if (ret)
548a095fadbSJean-Baptiste Maneyrol 			goto out_unlock;
549a095fadbSJean-Baptiste Maneyrol 		st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
550a095fadbSJean-Baptiste Maneyrol 		st->buffer[1] = offset & 0xFF;
551a095fadbSJean-Baptiste Maneyrol 		break;
552a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
553a095fadbSJean-Baptiste Maneyrol 		/* OFFSET_USER4 register is shared */
554a095fadbSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
555a095fadbSJean-Baptiste Maneyrol 				  &regval);
556a095fadbSJean-Baptiste Maneyrol 		if (ret)
557a095fadbSJean-Baptiste Maneyrol 			goto out_unlock;
558a095fadbSJean-Baptiste Maneyrol 		st->buffer[0] = offset & 0xFF;
559a095fadbSJean-Baptiste Maneyrol 		st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
560a095fadbSJean-Baptiste Maneyrol 		break;
561a095fadbSJean-Baptiste Maneyrol 	default:
562a095fadbSJean-Baptiste Maneyrol 		ret = -EINVAL;
563a095fadbSJean-Baptiste Maneyrol 		goto out_unlock;
564a095fadbSJean-Baptiste Maneyrol 	}
565a095fadbSJean-Baptiste Maneyrol 
566a095fadbSJean-Baptiste Maneyrol 	ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
567a095fadbSJean-Baptiste Maneyrol 
568a095fadbSJean-Baptiste Maneyrol out_unlock:
569a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
570a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
571a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
572a095fadbSJean-Baptiste Maneyrol 	return ret;
573a095fadbSJean-Baptiste Maneyrol }
574a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)575a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_raw(struct iio_dev *indio_dev,
576a095fadbSJean-Baptiste Maneyrol 				      struct iio_chan_spec const *chan,
577a095fadbSJean-Baptiste Maneyrol 				      int *val, int *val2, long mask)
578a095fadbSJean-Baptiste Maneyrol {
579a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
580a095fadbSJean-Baptiste Maneyrol 	s16 data;
581a095fadbSJean-Baptiste Maneyrol 	int ret;
582a095fadbSJean-Baptiste Maneyrol 
583bc3eb020SJean-Baptiste Maneyrol 	switch (chan->type) {
584bc3eb020SJean-Baptiste Maneyrol 	case IIO_ANGL_VEL:
585bc3eb020SJean-Baptiste Maneyrol 		break;
586bc3eb020SJean-Baptiste Maneyrol 	case IIO_TEMP:
587bc3eb020SJean-Baptiste Maneyrol 		return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
588bc3eb020SJean-Baptiste Maneyrol 	default:
589a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
590bc3eb020SJean-Baptiste Maneyrol 	}
591a095fadbSJean-Baptiste Maneyrol 
592a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
593a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_RAW:
594cbff19a3SJonathan Cameron 		if (!iio_device_claim_direct(indio_dev))
595cbff19a3SJonathan Cameron 			return -EBUSY;
596a095fadbSJean-Baptiste Maneyrol 		ret = inv_icm42600_gyro_read_sensor(st, chan, &data);
597cbff19a3SJonathan Cameron 		iio_device_release_direct(indio_dev);
598a095fadbSJean-Baptiste Maneyrol 		if (ret)
599a095fadbSJean-Baptiste Maneyrol 			return ret;
600a095fadbSJean-Baptiste Maneyrol 		*val = data;
601a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT;
602a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
603a1432b5bSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_read_scale(indio_dev, val, val2);
604a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
605a095fadbSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_read_odr(st, val, val2);
606a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
607a095fadbSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_read_offset(st, chan, val, val2);
608a095fadbSJean-Baptiste Maneyrol 	default:
609a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
610a095fadbSJean-Baptiste Maneyrol 	}
611a095fadbSJean-Baptiste Maneyrol }
612a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)613a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev,
614a095fadbSJean-Baptiste Maneyrol 					struct iio_chan_spec const *chan,
615a095fadbSJean-Baptiste Maneyrol 					const int **vals,
616a095fadbSJean-Baptiste Maneyrol 					int *type, int *length, long mask)
617a095fadbSJean-Baptiste Maneyrol {
618a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
619a1432b5bSJean-Baptiste Maneyrol 
620a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
621a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
622a095fadbSJean-Baptiste Maneyrol 
623a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
624a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
625a1432b5bSJean-Baptiste Maneyrol 		*vals = gyro_st->scales;
626a095fadbSJean-Baptiste Maneyrol 		*type = IIO_VAL_INT_PLUS_NANO;
627a1432b5bSJean-Baptiste Maneyrol 		*length = gyro_st->scales_len;
628a095fadbSJean-Baptiste Maneyrol 		return IIO_AVAIL_LIST;
629a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
630a095fadbSJean-Baptiste Maneyrol 		*vals = inv_icm42600_gyro_odr;
631a095fadbSJean-Baptiste Maneyrol 		*type = IIO_VAL_INT_PLUS_MICRO;
632a095fadbSJean-Baptiste Maneyrol 		*length = ARRAY_SIZE(inv_icm42600_gyro_odr);
633a095fadbSJean-Baptiste Maneyrol 		return IIO_AVAIL_LIST;
634a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
635a095fadbSJean-Baptiste Maneyrol 		*vals = inv_icm42600_gyro_calibbias;
636a095fadbSJean-Baptiste Maneyrol 		*type = IIO_VAL_INT_PLUS_NANO;
637a095fadbSJean-Baptiste Maneyrol 		return IIO_AVAIL_RANGE;
638a095fadbSJean-Baptiste Maneyrol 	default:
639a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
640a095fadbSJean-Baptiste Maneyrol 	}
641a095fadbSJean-Baptiste Maneyrol }
642a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)643a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev,
644a095fadbSJean-Baptiste Maneyrol 				       struct iio_chan_spec const *chan,
645a095fadbSJean-Baptiste Maneyrol 				       int val, int val2, long mask)
646a095fadbSJean-Baptiste Maneyrol {
647a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
648a095fadbSJean-Baptiste Maneyrol 	int ret;
649a095fadbSJean-Baptiste Maneyrol 
650a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
651a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
652a095fadbSJean-Baptiste Maneyrol 
653a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
654a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
655cbff19a3SJonathan Cameron 		if (!iio_device_claim_direct(indio_dev))
656cbff19a3SJonathan Cameron 			return -EBUSY;
657a1432b5bSJean-Baptiste Maneyrol 		ret = inv_icm42600_gyro_write_scale(indio_dev, val, val2);
658cbff19a3SJonathan Cameron 		iio_device_release_direct(indio_dev);
659a095fadbSJean-Baptiste Maneyrol 		return ret;
660a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
661ec74ae9fSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_write_odr(indio_dev, val, val2);
662a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
663cbff19a3SJonathan Cameron 		if (!iio_device_claim_direct(indio_dev))
664cbff19a3SJonathan Cameron 			return -EBUSY;
665a095fadbSJean-Baptiste Maneyrol 		ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
666cbff19a3SJonathan Cameron 		iio_device_release_direct(indio_dev);
667a095fadbSJean-Baptiste Maneyrol 		return ret;
668a095fadbSJean-Baptiste Maneyrol 	default:
669a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
670a095fadbSJean-Baptiste Maneyrol 	}
671a095fadbSJean-Baptiste Maneyrol }
672a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)673a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
674a095fadbSJean-Baptiste Maneyrol 					       struct iio_chan_spec const *chan,
675a095fadbSJean-Baptiste Maneyrol 					       long mask)
676a095fadbSJean-Baptiste Maneyrol {
677a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
678a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
679a095fadbSJean-Baptiste Maneyrol 
680a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
681a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
682a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT_PLUS_NANO;
683a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
684a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT_PLUS_MICRO;
685a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
686a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT_PLUS_NANO;
687a095fadbSJean-Baptiste Maneyrol 	default:
688a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
689a095fadbSJean-Baptiste Maneyrol 	}
690a095fadbSJean-Baptiste Maneyrol }
691a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev * indio_dev,unsigned int val)6927f85e42aSJean-Baptiste Maneyrol static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
6937f85e42aSJean-Baptiste Maneyrol 						  unsigned int val)
6947f85e42aSJean-Baptiste Maneyrol {
6957f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
6967f85e42aSJean-Baptiste Maneyrol 	int ret;
6977f85e42aSJean-Baptiste Maneyrol 
6987f85e42aSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
6997f85e42aSJean-Baptiste Maneyrol 
7007f85e42aSJean-Baptiste Maneyrol 	st->fifo.watermark.gyro = val;
7017f85e42aSJean-Baptiste Maneyrol 	ret = inv_icm42600_buffer_update_watermark(st);
7027f85e42aSJean-Baptiste Maneyrol 
7037f85e42aSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
7047f85e42aSJean-Baptiste Maneyrol 
7057f85e42aSJean-Baptiste Maneyrol 	return ret;
7067f85e42aSJean-Baptiste Maneyrol }
7077f85e42aSJean-Baptiste Maneyrol 
inv_icm42600_gyro_hwfifo_flush(struct iio_dev * indio_dev,unsigned int count)7087f85e42aSJean-Baptiste Maneyrol static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
7097f85e42aSJean-Baptiste Maneyrol 					  unsigned int count)
7107f85e42aSJean-Baptiste Maneyrol {
7117f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
7127f85e42aSJean-Baptiste Maneyrol 	int ret;
7137f85e42aSJean-Baptiste Maneyrol 
7147f85e42aSJean-Baptiste Maneyrol 	if (count == 0)
7157f85e42aSJean-Baptiste Maneyrol 		return 0;
7167f85e42aSJean-Baptiste Maneyrol 
7177f85e42aSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
7187f85e42aSJean-Baptiste Maneyrol 
7197f85e42aSJean-Baptiste Maneyrol 	ret = inv_icm42600_buffer_hwfifo_flush(st, count);
7207f85e42aSJean-Baptiste Maneyrol 	if (!ret)
7217f85e42aSJean-Baptiste Maneyrol 		ret = st->fifo.nb.gyro;
7227f85e42aSJean-Baptiste Maneyrol 
7237f85e42aSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
7247f85e42aSJean-Baptiste Maneyrol 
7257f85e42aSJean-Baptiste Maneyrol 	return ret;
7267f85e42aSJean-Baptiste Maneyrol }
7277f85e42aSJean-Baptiste Maneyrol 
728a095fadbSJean-Baptiste Maneyrol static const struct iio_info inv_icm42600_gyro_info = {
729a095fadbSJean-Baptiste Maneyrol 	.read_raw = inv_icm42600_gyro_read_raw,
730a095fadbSJean-Baptiste Maneyrol 	.read_avail = inv_icm42600_gyro_read_avail,
731a095fadbSJean-Baptiste Maneyrol 	.write_raw = inv_icm42600_gyro_write_raw,
732a095fadbSJean-Baptiste Maneyrol 	.write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt,
733a095fadbSJean-Baptiste Maneyrol 	.debugfs_reg_access = inv_icm42600_debugfs_reg,
7347f85e42aSJean-Baptiste Maneyrol 	.update_scan_mode = inv_icm42600_gyro_update_scan_mode,
7357f85e42aSJean-Baptiste Maneyrol 	.hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark,
7367f85e42aSJean-Baptiste Maneyrol 	.hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush,
737a095fadbSJean-Baptiste Maneyrol };
738a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_init(struct inv_icm42600_state * st)739a095fadbSJean-Baptiste Maneyrol struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
740a095fadbSJean-Baptiste Maneyrol {
741a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
742a095fadbSJean-Baptiste Maneyrol 	const char *name;
743a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st;
7440ecc363cSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp_chip ts_chip;
745a095fadbSJean-Baptiste Maneyrol 	struct iio_dev *indio_dev;
746a095fadbSJean-Baptiste Maneyrol 	int ret;
747a095fadbSJean-Baptiste Maneyrol 
748a095fadbSJean-Baptiste Maneyrol 	name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
749a095fadbSJean-Baptiste Maneyrol 	if (!name)
750a095fadbSJean-Baptiste Maneyrol 		return ERR_PTR(-ENOMEM);
751a095fadbSJean-Baptiste Maneyrol 
752a1432b5bSJean-Baptiste Maneyrol 	indio_dev = devm_iio_device_alloc(dev, sizeof(*gyro_st));
753a095fadbSJean-Baptiste Maneyrol 	if (!indio_dev)
754a095fadbSJean-Baptiste Maneyrol 		return ERR_PTR(-ENOMEM);
755a1432b5bSJean-Baptiste Maneyrol 	gyro_st = iio_priv(indio_dev);
756a1432b5bSJean-Baptiste Maneyrol 
757a1432b5bSJean-Baptiste Maneyrol 	switch (st->chip) {
758a1432b5bSJean-Baptiste Maneyrol 	case INV_CHIP_ICM42686:
759a1432b5bSJean-Baptiste Maneyrol 		gyro_st->scales = inv_icm42686_gyro_scale;
760a1432b5bSJean-Baptiste Maneyrol 		gyro_st->scales_len = ARRAY_SIZE(inv_icm42686_gyro_scale);
761a1432b5bSJean-Baptiste Maneyrol 		break;
762a1432b5bSJean-Baptiste Maneyrol 	default:
763a1432b5bSJean-Baptiste Maneyrol 		gyro_st->scales = inv_icm42600_gyro_scale;
764a1432b5bSJean-Baptiste Maneyrol 		gyro_st->scales_len = ARRAY_SIZE(inv_icm42600_gyro_scale);
765a1432b5bSJean-Baptiste Maneyrol 		break;
766a1432b5bSJean-Baptiste Maneyrol 	}
767a095fadbSJean-Baptiste Maneyrol 
7680ecc363cSJean-Baptiste Maneyrol 	/*
7690ecc363cSJean-Baptiste Maneyrol 	 * clock period is 32kHz (31250ns)
7700ecc363cSJean-Baptiste Maneyrol 	 * jitter is +/- 2% (20 per mille)
7710ecc363cSJean-Baptiste Maneyrol 	 */
7720ecc363cSJean-Baptiste Maneyrol 	ts_chip.clock_period = 31250;
7730ecc363cSJean-Baptiste Maneyrol 	ts_chip.jitter = 20;
7740ecc363cSJean-Baptiste Maneyrol 	ts_chip.init_period = inv_icm42600_odr_to_period(st->conf.accel.odr);
775a1432b5bSJean-Baptiste Maneyrol 	inv_sensors_timestamp_init(&gyro_st->ts, &ts_chip);
776ec74ae9fSJean-Baptiste Maneyrol 
777a095fadbSJean-Baptiste Maneyrol 	iio_device_set_drvdata(indio_dev, st);
778a095fadbSJean-Baptiste Maneyrol 	indio_dev->name = name;
779a095fadbSJean-Baptiste Maneyrol 	indio_dev->info = &inv_icm42600_gyro_info;
78017395ce2SAlexandru Ardelean 	indio_dev->modes = INDIO_DIRECT_MODE;
781a095fadbSJean-Baptiste Maneyrol 	indio_dev->channels = inv_icm42600_gyro_channels;
782a095fadbSJean-Baptiste Maneyrol 	indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels);
7837f85e42aSJean-Baptiste Maneyrol 	indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks;
7847f85e42aSJean-Baptiste Maneyrol 	indio_dev->setup_ops = &inv_icm42600_buffer_ops;
7857f85e42aSJean-Baptiste Maneyrol 
78617395ce2SAlexandru Ardelean 	ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
78717395ce2SAlexandru Ardelean 					  &inv_icm42600_buffer_ops);
78817395ce2SAlexandru Ardelean 	if (ret)
78917395ce2SAlexandru Ardelean 		return ERR_PTR(ret);
790a095fadbSJean-Baptiste Maneyrol 
791a095fadbSJean-Baptiste Maneyrol 	ret = devm_iio_device_register(dev, indio_dev);
792a095fadbSJean-Baptiste Maneyrol 	if (ret)
793a095fadbSJean-Baptiste Maneyrol 		return ERR_PTR(ret);
794a095fadbSJean-Baptiste Maneyrol 
795a095fadbSJean-Baptiste Maneyrol 	return indio_dev;
796a095fadbSJean-Baptiste Maneyrol }
7977f85e42aSJean-Baptiste Maneyrol 
inv_icm42600_gyro_parse_fifo(struct iio_dev * indio_dev)7987f85e42aSJean-Baptiste Maneyrol int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev)
7997f85e42aSJean-Baptiste Maneyrol {
8007f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
801a1432b5bSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
802a1432b5bSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp *ts = &gyro_st->ts;
8037f85e42aSJean-Baptiste Maneyrol 	ssize_t i, size;
804ec74ae9fSJean-Baptiste Maneyrol 	unsigned int no;
8057f85e42aSJean-Baptiste Maneyrol 	const void *accel, *gyro, *timestamp;
8067f85e42aSJean-Baptiste Maneyrol 	const s8 *temp;
8077f85e42aSJean-Baptiste Maneyrol 	unsigned int odr;
808ec74ae9fSJean-Baptiste Maneyrol 	s64 ts_val;
8097f85e42aSJean-Baptiste Maneyrol 	/* buffer is copied to userspace, zeroing it to avoid any data leak */
8107f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_gyro_buffer buffer = { };
8117f85e42aSJean-Baptiste Maneyrol 
812ec74ae9fSJean-Baptiste Maneyrol 	/* parse all fifo packets */
8137f85e42aSJean-Baptiste Maneyrol 	for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) {
8147f85e42aSJean-Baptiste Maneyrol 		size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
8157f85e42aSJean-Baptiste Maneyrol 				&accel, &gyro, &temp, &timestamp, &odr);
8167f85e42aSJean-Baptiste Maneyrol 		/* quit if error or FIFO is empty */
8177f85e42aSJean-Baptiste Maneyrol 		if (size <= 0)
8187f85e42aSJean-Baptiste Maneyrol 			return size;
8197f85e42aSJean-Baptiste Maneyrol 
8207f85e42aSJean-Baptiste Maneyrol 		/* skip packet if no gyro data or data is invalid */
8217f85e42aSJean-Baptiste Maneyrol 		if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
8227f85e42aSJean-Baptiste Maneyrol 			continue;
823ec74ae9fSJean-Baptiste Maneyrol 
824ec74ae9fSJean-Baptiste Maneyrol 		/* update odr */
8250ecc363cSJean-Baptiste Maneyrol 		if (odr & INV_ICM42600_SENSOR_GYRO)
826ec74ae9fSJean-Baptiste Maneyrol 			inv_sensors_timestamp_apply_odr(ts, st->fifo.period,
827ec74ae9fSJean-Baptiste Maneyrol 							st->fifo.nb.total, no);
8287f85e42aSJean-Baptiste Maneyrol 
8297f85e42aSJean-Baptiste Maneyrol 		memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro));
8307f85e42aSJean-Baptiste Maneyrol 		/* convert 8 bits FIFO temperature in high resolution format */
8317f85e42aSJean-Baptiste Maneyrol 		buffer.temp = temp ? (*temp * 64) : 0;
8327f85e42aSJean-Baptiste Maneyrol 		ts_val = inv_sensors_timestamp_pop(ts);
8330ecc363cSJean-Baptiste Maneyrol 		iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val);
834ec74ae9fSJean-Baptiste Maneyrol 	}
8357f85e42aSJean-Baptiste Maneyrol 
8367f85e42aSJean-Baptiste Maneyrol 	return 0;
8377f85e42aSJean-Baptiste Maneyrol }
8387f85e42aSJean-Baptiste Maneyrol