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>
15*0ecc363cSJean-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 */
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);
102*0ecc363cSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp *ts = iio_priv(indio_dev);
1037f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
1047f85e42aSJean-Baptiste Maneyrol 	unsigned int fifo_en = 0;
1057f85e42aSJean-Baptiste Maneyrol 	unsigned int sleep_gyro = 0;
1067f85e42aSJean-Baptiste Maneyrol 	unsigned int sleep_temp = 0;
1077f85e42aSJean-Baptiste Maneyrol 	unsigned int sleep;
1087f85e42aSJean-Baptiste Maneyrol 	int ret;
1097f85e42aSJean-Baptiste Maneyrol 
1107f85e42aSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
1117f85e42aSJean-Baptiste Maneyrol 
1127f85e42aSJean-Baptiste Maneyrol 	if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
1137f85e42aSJean-Baptiste Maneyrol 		/* enable temp sensor */
1147f85e42aSJean-Baptiste Maneyrol 		ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
1157f85e42aSJean-Baptiste Maneyrol 		if (ret)
1167f85e42aSJean-Baptiste Maneyrol 			goto out_unlock;
1177f85e42aSJean-Baptiste Maneyrol 		fifo_en |= INV_ICM42600_SENSOR_TEMP;
1187f85e42aSJean-Baptiste Maneyrol 	}
1197f85e42aSJean-Baptiste Maneyrol 
1207f85e42aSJean-Baptiste Maneyrol 	if (*scan_mask & INV_ICM42600_SCAN_MASK_GYRO_3AXIS) {
1217f85e42aSJean-Baptiste Maneyrol 		/* enable gyro sensor */
1227f85e42aSJean-Baptiste Maneyrol 		conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
1237f85e42aSJean-Baptiste Maneyrol 		ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_gyro);
1247f85e42aSJean-Baptiste Maneyrol 		if (ret)
1257f85e42aSJean-Baptiste Maneyrol 			goto out_unlock;
1267f85e42aSJean-Baptiste Maneyrol 		fifo_en |= INV_ICM42600_SENSOR_GYRO;
1277f85e42aSJean-Baptiste Maneyrol 	}
1287f85e42aSJean-Baptiste Maneyrol 
1297f85e42aSJean-Baptiste Maneyrol 	/* update data FIFO write */
130*0ecc363cSJean-Baptiste Maneyrol 	inv_sensors_timestamp_apply_odr(ts, 0, 0, 0);
1317f85e42aSJean-Baptiste Maneyrol 	ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
1327f85e42aSJean-Baptiste Maneyrol 
1337f85e42aSJean-Baptiste Maneyrol out_unlock:
1347f85e42aSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
1357f85e42aSJean-Baptiste Maneyrol 	/* sleep maximum required time */
1367f85e42aSJean-Baptiste Maneyrol 	if (sleep_gyro > sleep_temp)
1377f85e42aSJean-Baptiste Maneyrol 		sleep = sleep_gyro;
1387f85e42aSJean-Baptiste Maneyrol 	else
1397f85e42aSJean-Baptiste Maneyrol 		sleep = sleep_temp;
1407f85e42aSJean-Baptiste Maneyrol 	if (sleep)
1417f85e42aSJean-Baptiste Maneyrol 		msleep(sleep);
1427f85e42aSJean-Baptiste Maneyrol 	return ret;
1437f85e42aSJean-Baptiste Maneyrol }
1447f85e42aSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_sensor(struct inv_icm42600_state * st,struct iio_chan_spec const * chan,int16_t * val)145a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
146a095fadbSJean-Baptiste Maneyrol 					 struct iio_chan_spec const *chan,
147a095fadbSJean-Baptiste Maneyrol 					 int16_t *val)
148a095fadbSJean-Baptiste Maneyrol {
149a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
150a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
151a095fadbSJean-Baptiste Maneyrol 	unsigned int reg;
152a095fadbSJean-Baptiste Maneyrol 	__be16 *data;
153a095fadbSJean-Baptiste Maneyrol 	int ret;
154a095fadbSJean-Baptiste Maneyrol 
155a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
156a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
157a095fadbSJean-Baptiste Maneyrol 
158a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
159a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
160a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_GYRO_DATA_X;
161a095fadbSJean-Baptiste Maneyrol 		break;
162a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
163a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_GYRO_DATA_Y;
164a095fadbSJean-Baptiste Maneyrol 		break;
165a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
166a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_GYRO_DATA_Z;
167a095fadbSJean-Baptiste Maneyrol 		break;
168a095fadbSJean-Baptiste Maneyrol 	default:
169a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
170a095fadbSJean-Baptiste Maneyrol 	}
171a095fadbSJean-Baptiste Maneyrol 
172a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
173a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
174a095fadbSJean-Baptiste Maneyrol 
175a095fadbSJean-Baptiste Maneyrol 	/* enable gyro sensor */
176a095fadbSJean-Baptiste Maneyrol 	conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
177a095fadbSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
178a095fadbSJean-Baptiste Maneyrol 	if (ret)
179a095fadbSJean-Baptiste Maneyrol 		goto exit;
180a095fadbSJean-Baptiste Maneyrol 
181a095fadbSJean-Baptiste Maneyrol 	/* read gyro register data */
182a095fadbSJean-Baptiste Maneyrol 	data = (__be16 *)&st->buffer[0];
183a095fadbSJean-Baptiste Maneyrol 	ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
184a095fadbSJean-Baptiste Maneyrol 	if (ret)
185a095fadbSJean-Baptiste Maneyrol 		goto exit;
186a095fadbSJean-Baptiste Maneyrol 
187a095fadbSJean-Baptiste Maneyrol 	*val = (int16_t)be16_to_cpup(data);
188a095fadbSJean-Baptiste Maneyrol 	if (*val == INV_ICM42600_DATA_INVALID)
189a095fadbSJean-Baptiste Maneyrol 		ret = -EINVAL;
190a095fadbSJean-Baptiste Maneyrol exit:
191a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
192a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
193a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
194a095fadbSJean-Baptiste Maneyrol 	return ret;
195a095fadbSJean-Baptiste Maneyrol }
196a095fadbSJean-Baptiste Maneyrol 
197a095fadbSJean-Baptiste Maneyrol /* IIO format int + nano */
198a095fadbSJean-Baptiste Maneyrol static const int inv_icm42600_gyro_scale[] = {
199a095fadbSJean-Baptiste Maneyrol 	/* +/- 2000dps => 0.001065264 rad/s */
200a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_2000DPS] = 0,
201a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_2000DPS + 1] = 1065264,
202a095fadbSJean-Baptiste Maneyrol 	/* +/- 1000dps => 0.000532632 rad/s */
203a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_1000DPS] = 0,
204a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_1000DPS + 1] = 532632,
205a095fadbSJean-Baptiste Maneyrol 	/* +/- 500dps => 0.000266316 rad/s */
206a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_500DPS] = 0,
207a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_500DPS + 1] = 266316,
208a095fadbSJean-Baptiste Maneyrol 	/* +/- 250dps => 0.000133158 rad/s */
209a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_250DPS] = 0,
210a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_250DPS + 1] = 133158,
211a095fadbSJean-Baptiste Maneyrol 	/* +/- 125dps => 0.000066579 rad/s */
212a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_125DPS] = 0,
213a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_125DPS + 1] = 66579,
214a095fadbSJean-Baptiste Maneyrol 	/* +/- 62.5dps => 0.000033290 rad/s */
215a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_62_5DPS] = 0,
216a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_62_5DPS + 1] = 33290,
217a095fadbSJean-Baptiste Maneyrol 	/* +/- 31.25dps => 0.000016645 rad/s */
218a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_31_25DPS] = 0,
219a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_31_25DPS + 1] = 16645,
220a095fadbSJean-Baptiste Maneyrol 	/* +/- 15.625dps => 0.000008322 rad/s */
221a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_15_625DPS] = 0,
222a095fadbSJean-Baptiste Maneyrol 	[2 * INV_ICM42600_GYRO_FS_15_625DPS + 1] = 8322,
223a095fadbSJean-Baptiste Maneyrol };
224a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_scale(struct inv_icm42600_state * st,int * val,int * val2)225a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_scale(struct inv_icm42600_state *st,
226a095fadbSJean-Baptiste Maneyrol 					int *val, int *val2)
227a095fadbSJean-Baptiste Maneyrol {
228a095fadbSJean-Baptiste Maneyrol 	unsigned int idx;
229a095fadbSJean-Baptiste Maneyrol 
230a095fadbSJean-Baptiste Maneyrol 	idx = st->conf.gyro.fs;
231a095fadbSJean-Baptiste Maneyrol 
232a095fadbSJean-Baptiste Maneyrol 	*val = inv_icm42600_gyro_scale[2 * idx];
233a095fadbSJean-Baptiste Maneyrol 	*val2 = inv_icm42600_gyro_scale[2 * idx + 1];
234a095fadbSJean-Baptiste Maneyrol 	return IIO_VAL_INT_PLUS_NANO;
235a095fadbSJean-Baptiste Maneyrol }
236a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_scale(struct inv_icm42600_state * st,int val,int val2)237a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_scale(struct inv_icm42600_state *st,
238a095fadbSJean-Baptiste Maneyrol 					 int val, int val2)
239a095fadbSJean-Baptiste Maneyrol {
240a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
241a095fadbSJean-Baptiste Maneyrol 	unsigned int idx;
242a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
243a095fadbSJean-Baptiste Maneyrol 	int ret;
244a095fadbSJean-Baptiste Maneyrol 
245a095fadbSJean-Baptiste Maneyrol 	for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_scale); idx += 2) {
246a095fadbSJean-Baptiste Maneyrol 		if (val == inv_icm42600_gyro_scale[idx] &&
247a095fadbSJean-Baptiste Maneyrol 		    val2 == inv_icm42600_gyro_scale[idx + 1])
248a095fadbSJean-Baptiste Maneyrol 			break;
249a095fadbSJean-Baptiste Maneyrol 	}
250a095fadbSJean-Baptiste Maneyrol 	if (idx >= ARRAY_SIZE(inv_icm42600_gyro_scale))
251a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
252a095fadbSJean-Baptiste Maneyrol 
253a095fadbSJean-Baptiste Maneyrol 	conf.fs = idx / 2;
254a095fadbSJean-Baptiste Maneyrol 
255a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
256a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
257a095fadbSJean-Baptiste Maneyrol 
258a095fadbSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
259a095fadbSJean-Baptiste Maneyrol 
260a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
261a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
262a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
263a095fadbSJean-Baptiste Maneyrol 
264a095fadbSJean-Baptiste Maneyrol 	return ret;
265a095fadbSJean-Baptiste Maneyrol }
266a095fadbSJean-Baptiste Maneyrol 
267a095fadbSJean-Baptiste Maneyrol /* IIO format int + micro */
268a095fadbSJean-Baptiste Maneyrol static const int inv_icm42600_gyro_odr[] = {
269a095fadbSJean-Baptiste Maneyrol 	/* 12.5Hz */
270a095fadbSJean-Baptiste Maneyrol 	12, 500000,
271a095fadbSJean-Baptiste Maneyrol 	/* 25Hz */
272a095fadbSJean-Baptiste Maneyrol 	25, 0,
273a095fadbSJean-Baptiste Maneyrol 	/* 50Hz */
274a095fadbSJean-Baptiste Maneyrol 	50, 0,
275a095fadbSJean-Baptiste Maneyrol 	/* 100Hz */
276a095fadbSJean-Baptiste Maneyrol 	100, 0,
277a095fadbSJean-Baptiste Maneyrol 	/* 200Hz */
278a095fadbSJean-Baptiste Maneyrol 	200, 0,
279a095fadbSJean-Baptiste Maneyrol 	/* 1kHz */
280a095fadbSJean-Baptiste Maneyrol 	1000, 0,
281a095fadbSJean-Baptiste Maneyrol 	/* 2kHz */
282a095fadbSJean-Baptiste Maneyrol 	2000, 0,
283a095fadbSJean-Baptiste Maneyrol 	/* 4kHz */
284a095fadbSJean-Baptiste Maneyrol 	4000, 0,
285a095fadbSJean-Baptiste Maneyrol };
286a095fadbSJean-Baptiste Maneyrol 
287a095fadbSJean-Baptiste Maneyrol static const int inv_icm42600_gyro_odr_conv[] = {
288a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_12_5HZ,
289a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_25HZ,
290a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_50HZ,
291a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_100HZ,
292a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_200HZ,
293a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_1KHZ_LN,
294a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_2KHZ_LN,
295a095fadbSJean-Baptiste Maneyrol 	INV_ICM42600_ODR_4KHZ_LN,
296a095fadbSJean-Baptiste Maneyrol };
297a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_odr(struct inv_icm42600_state * st,int * val,int * val2)298a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st,
299a095fadbSJean-Baptiste Maneyrol 				      int *val, int *val2)
300a095fadbSJean-Baptiste Maneyrol {
301a095fadbSJean-Baptiste Maneyrol 	unsigned int odr;
302a095fadbSJean-Baptiste Maneyrol 	unsigned int i;
303a095fadbSJean-Baptiste Maneyrol 
304a095fadbSJean-Baptiste Maneyrol 	odr = st->conf.gyro.odr;
305a095fadbSJean-Baptiste Maneyrol 
306a095fadbSJean-Baptiste Maneyrol 	for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) {
307a095fadbSJean-Baptiste Maneyrol 		if (inv_icm42600_gyro_odr_conv[i] == odr)
308a095fadbSJean-Baptiste Maneyrol 			break;
309a095fadbSJean-Baptiste Maneyrol 	}
310a095fadbSJean-Baptiste Maneyrol 	if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv))
311a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
312a095fadbSJean-Baptiste Maneyrol 
313a095fadbSJean-Baptiste Maneyrol 	*val = inv_icm42600_gyro_odr[2 * i];
314a095fadbSJean-Baptiste Maneyrol 	*val2 = inv_icm42600_gyro_odr[2 * i + 1];
315a095fadbSJean-Baptiste Maneyrol 
316a095fadbSJean-Baptiste Maneyrol 	return IIO_VAL_INT_PLUS_MICRO;
317a095fadbSJean-Baptiste Maneyrol }
318a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_odr(struct iio_dev * indio_dev,int val,int val2)319ec74ae9fSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev,
320a095fadbSJean-Baptiste Maneyrol 				       int val, int val2)
321a095fadbSJean-Baptiste Maneyrol {
322ec74ae9fSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
323*0ecc363cSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp *ts = iio_priv(indio_dev);
324a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
325a095fadbSJean-Baptiste Maneyrol 	unsigned int idx;
326a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
327a095fadbSJean-Baptiste Maneyrol 	int ret;
328a095fadbSJean-Baptiste Maneyrol 
329a095fadbSJean-Baptiste Maneyrol 	for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_odr); idx += 2) {
330a095fadbSJean-Baptiste Maneyrol 		if (val == inv_icm42600_gyro_odr[idx] &&
331a095fadbSJean-Baptiste Maneyrol 		    val2 == inv_icm42600_gyro_odr[idx + 1])
332a095fadbSJean-Baptiste Maneyrol 			break;
333a095fadbSJean-Baptiste Maneyrol 	}
334a095fadbSJean-Baptiste Maneyrol 	if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr))
335a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
336a095fadbSJean-Baptiste Maneyrol 
337a095fadbSJean-Baptiste Maneyrol 	conf.odr = inv_icm42600_gyro_odr_conv[idx / 2];
338a095fadbSJean-Baptiste Maneyrol 
339a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
340a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
341a095fadbSJean-Baptiste Maneyrol 
342*0ecc363cSJean-Baptiste Maneyrol 	ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr),
343ec74ae9fSJean-Baptiste Maneyrol 					       iio_buffer_enabled(indio_dev));
344ec74ae9fSJean-Baptiste Maneyrol 	if (ret)
345ec74ae9fSJean-Baptiste Maneyrol 		goto out_unlock;
346ec74ae9fSJean-Baptiste Maneyrol 
347a095fadbSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
3487f85e42aSJean-Baptiste Maneyrol 	if (ret)
3497f85e42aSJean-Baptiste Maneyrol 		goto out_unlock;
3507f85e42aSJean-Baptiste Maneyrol 	inv_icm42600_buffer_update_fifo_period(st);
3517f85e42aSJean-Baptiste Maneyrol 	inv_icm42600_buffer_update_watermark(st);
352a095fadbSJean-Baptiste Maneyrol 
3537f85e42aSJean-Baptiste Maneyrol out_unlock:
354a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
355a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
356a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
357a095fadbSJean-Baptiste Maneyrol 
358a095fadbSJean-Baptiste Maneyrol 	return ret;
359a095fadbSJean-Baptiste Maneyrol }
360a095fadbSJean-Baptiste Maneyrol 
361a095fadbSJean-Baptiste Maneyrol /*
362a095fadbSJean-Baptiste Maneyrol  * Calibration bias values, IIO range format int + nano.
363a095fadbSJean-Baptiste Maneyrol  * Value is limited to +/-64dps coded on 12 bits signed. Step is 1/32 dps.
364a095fadbSJean-Baptiste Maneyrol  */
365a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_calibbias[] = {
366a095fadbSJean-Baptiste Maneyrol 	-1, 117010721,		/* min: -1.117010721 rad/s */
367a095fadbSJean-Baptiste Maneyrol 	0, 545415,		/* step: 0.000545415 rad/s */
368a095fadbSJean-Baptiste Maneyrol 	1, 116465306,		/* max: 1.116465306 rad/s */
369a095fadbSJean-Baptiste Maneyrol };
370a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_offset(struct inv_icm42600_state * st,struct iio_chan_spec const * chan,int * val,int * val2)371a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st,
372a095fadbSJean-Baptiste Maneyrol 					 struct iio_chan_spec const *chan,
373a095fadbSJean-Baptiste Maneyrol 					 int *val, int *val2)
374a095fadbSJean-Baptiste Maneyrol {
375a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
376a095fadbSJean-Baptiste Maneyrol 	int64_t val64;
377a095fadbSJean-Baptiste Maneyrol 	int32_t bias;
378a095fadbSJean-Baptiste Maneyrol 	unsigned int reg;
379a095fadbSJean-Baptiste Maneyrol 	int16_t offset;
380a095fadbSJean-Baptiste Maneyrol 	uint8_t data[2];
381a095fadbSJean-Baptiste Maneyrol 	int ret;
382a095fadbSJean-Baptiste Maneyrol 
383a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
384a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
385a095fadbSJean-Baptiste Maneyrol 
386a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
387a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
388a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER0;
389a095fadbSJean-Baptiste Maneyrol 		break;
390a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
391a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER1;
392a095fadbSJean-Baptiste Maneyrol 		break;
393a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
394a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER3;
395a095fadbSJean-Baptiste Maneyrol 		break;
396a095fadbSJean-Baptiste Maneyrol 	default:
397a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
398a095fadbSJean-Baptiste Maneyrol 	}
399a095fadbSJean-Baptiste Maneyrol 
400a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
401a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
402a095fadbSJean-Baptiste Maneyrol 
403a095fadbSJean-Baptiste Maneyrol 	ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data));
404a095fadbSJean-Baptiste Maneyrol 	memcpy(data, st->buffer, sizeof(data));
405a095fadbSJean-Baptiste Maneyrol 
406a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
407a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
408a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
409a095fadbSJean-Baptiste Maneyrol 	if (ret)
410a095fadbSJean-Baptiste Maneyrol 		return ret;
411a095fadbSJean-Baptiste Maneyrol 
412a095fadbSJean-Baptiste Maneyrol 	/* 12 bits signed value */
413a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
414a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
415a095fadbSJean-Baptiste Maneyrol 		offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
416a095fadbSJean-Baptiste Maneyrol 		break;
417a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
418a095fadbSJean-Baptiste Maneyrol 		offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
419a095fadbSJean-Baptiste Maneyrol 		break;
420a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
421a095fadbSJean-Baptiste Maneyrol 		offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
422a095fadbSJean-Baptiste Maneyrol 		break;
423a095fadbSJean-Baptiste Maneyrol 	default:
424a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
425a095fadbSJean-Baptiste Maneyrol 	}
426a095fadbSJean-Baptiste Maneyrol 
427a095fadbSJean-Baptiste Maneyrol 	/*
428a095fadbSJean-Baptiste Maneyrol 	 * convert raw offset to dps then to rad/s
429a095fadbSJean-Baptiste Maneyrol 	 * 12 bits signed raw max 64 to dps: 64 / 2048
430a095fadbSJean-Baptiste Maneyrol 	 * dps to rad: Pi / 180
431a095fadbSJean-Baptiste Maneyrol 	 * result in nano (1000000000)
432a095fadbSJean-Baptiste Maneyrol 	 * (offset * 64 * Pi * 1000000000) / (2048 * 180)
433a095fadbSJean-Baptiste Maneyrol 	 */
434a095fadbSJean-Baptiste Maneyrol 	val64 = (int64_t)offset * 64LL * 3141592653LL;
435a095fadbSJean-Baptiste Maneyrol 	/* for rounding, add + or - divisor (2048 * 180) divided by 2 */
436a095fadbSJean-Baptiste Maneyrol 	if (val64 >= 0)
437a095fadbSJean-Baptiste Maneyrol 		val64 += 2048 * 180 / 2;
438a095fadbSJean-Baptiste Maneyrol 	else
439a095fadbSJean-Baptiste Maneyrol 		val64 -= 2048 * 180 / 2;
440a095fadbSJean-Baptiste Maneyrol 	bias = div_s64(val64, 2048 * 180);
441a095fadbSJean-Baptiste Maneyrol 	*val = bias / 1000000000L;
442a095fadbSJean-Baptiste Maneyrol 	*val2 = bias % 1000000000L;
443a095fadbSJean-Baptiste Maneyrol 
444a095fadbSJean-Baptiste Maneyrol 	return IIO_VAL_INT_PLUS_NANO;
445a095fadbSJean-Baptiste Maneyrol }
446a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_offset(struct inv_icm42600_state * st,struct iio_chan_spec const * chan,int val,int val2)447a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st,
448a095fadbSJean-Baptiste Maneyrol 					  struct iio_chan_spec const *chan,
449a095fadbSJean-Baptiste Maneyrol 					  int val, int val2)
450a095fadbSJean-Baptiste Maneyrol {
451a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
452a095fadbSJean-Baptiste Maneyrol 	int64_t val64, min, max;
453a095fadbSJean-Baptiste Maneyrol 	unsigned int reg, regval;
454a095fadbSJean-Baptiste Maneyrol 	int16_t offset;
455a095fadbSJean-Baptiste Maneyrol 	int ret;
456a095fadbSJean-Baptiste Maneyrol 
457a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
458a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
459a095fadbSJean-Baptiste Maneyrol 
460a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
461a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
462a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER0;
463a095fadbSJean-Baptiste Maneyrol 		break;
464a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
465a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER1;
466a095fadbSJean-Baptiste Maneyrol 		break;
467a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
468a095fadbSJean-Baptiste Maneyrol 		reg = INV_ICM42600_REG_OFFSET_USER3;
469a095fadbSJean-Baptiste Maneyrol 		break;
470a095fadbSJean-Baptiste Maneyrol 	default:
471a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
472a095fadbSJean-Baptiste Maneyrol 	}
473a095fadbSJean-Baptiste Maneyrol 
474a095fadbSJean-Baptiste Maneyrol 	/* inv_icm42600_gyro_calibbias: min - step - max in nano */
475a095fadbSJean-Baptiste Maneyrol 	min = (int64_t)inv_icm42600_gyro_calibbias[0] * 1000000000LL +
476a095fadbSJean-Baptiste Maneyrol 	      (int64_t)inv_icm42600_gyro_calibbias[1];
477a095fadbSJean-Baptiste Maneyrol 	max = (int64_t)inv_icm42600_gyro_calibbias[4] * 1000000000LL +
478a095fadbSJean-Baptiste Maneyrol 	      (int64_t)inv_icm42600_gyro_calibbias[5];
479a095fadbSJean-Baptiste Maneyrol 	val64 = (int64_t)val * 1000000000LL + (int64_t)val2;
480a095fadbSJean-Baptiste Maneyrol 	if (val64 < min || val64 > max)
481a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
482a095fadbSJean-Baptiste Maneyrol 
483a095fadbSJean-Baptiste Maneyrol 	/*
484a095fadbSJean-Baptiste Maneyrol 	 * convert rad/s to dps then to raw value
485a095fadbSJean-Baptiste Maneyrol 	 * rad to dps: 180 / Pi
486a095fadbSJean-Baptiste Maneyrol 	 * dps to raw 12 bits signed, max 64: 2048 / 64
487a095fadbSJean-Baptiste Maneyrol 	 * val in nano (1000000000)
488a095fadbSJean-Baptiste Maneyrol 	 * val * 180 * 2048 / (Pi * 1000000000 * 64)
489a095fadbSJean-Baptiste Maneyrol 	 */
490a095fadbSJean-Baptiste Maneyrol 	val64 = val64 * 180LL * 2048LL;
491a095fadbSJean-Baptiste Maneyrol 	/* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */
492a095fadbSJean-Baptiste Maneyrol 	if (val64 >= 0)
493a095fadbSJean-Baptiste Maneyrol 		val64 += 3141592653LL * 64LL / 2LL;
494a095fadbSJean-Baptiste Maneyrol 	else
495a095fadbSJean-Baptiste Maneyrol 		val64 -= 3141592653LL * 64LL / 2LL;
496a095fadbSJean-Baptiste Maneyrol 	offset = div64_s64(val64, 3141592653LL * 64LL);
497a095fadbSJean-Baptiste Maneyrol 
498a095fadbSJean-Baptiste Maneyrol 	/* clamp value limited to 12 bits signed */
499a095fadbSJean-Baptiste Maneyrol 	if (offset < -2048)
500a095fadbSJean-Baptiste Maneyrol 		offset = -2048;
501a095fadbSJean-Baptiste Maneyrol 	else if (offset > 2047)
502a095fadbSJean-Baptiste Maneyrol 		offset = 2047;
503a095fadbSJean-Baptiste Maneyrol 
504a095fadbSJean-Baptiste Maneyrol 	pm_runtime_get_sync(dev);
505a095fadbSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
506a095fadbSJean-Baptiste Maneyrol 
507a095fadbSJean-Baptiste Maneyrol 	switch (chan->channel2) {
508a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_X:
509a095fadbSJean-Baptiste Maneyrol 		/* OFFSET_USER1 register is shared */
510a095fadbSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
511a095fadbSJean-Baptiste Maneyrol 				  &regval);
512a095fadbSJean-Baptiste Maneyrol 		if (ret)
513a095fadbSJean-Baptiste Maneyrol 			goto out_unlock;
514a095fadbSJean-Baptiste Maneyrol 		st->buffer[0] = offset & 0xFF;
515a095fadbSJean-Baptiste Maneyrol 		st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
516a095fadbSJean-Baptiste Maneyrol 		break;
517a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Y:
518a095fadbSJean-Baptiste Maneyrol 		/* OFFSET_USER1 register is shared */
519a095fadbSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
520a095fadbSJean-Baptiste Maneyrol 				  &regval);
521a095fadbSJean-Baptiste Maneyrol 		if (ret)
522a095fadbSJean-Baptiste Maneyrol 			goto out_unlock;
523a095fadbSJean-Baptiste Maneyrol 		st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
524a095fadbSJean-Baptiste Maneyrol 		st->buffer[1] = offset & 0xFF;
525a095fadbSJean-Baptiste Maneyrol 		break;
526a095fadbSJean-Baptiste Maneyrol 	case IIO_MOD_Z:
527a095fadbSJean-Baptiste Maneyrol 		/* OFFSET_USER4 register is shared */
528a095fadbSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
529a095fadbSJean-Baptiste Maneyrol 				  &regval);
530a095fadbSJean-Baptiste Maneyrol 		if (ret)
531a095fadbSJean-Baptiste Maneyrol 			goto out_unlock;
532a095fadbSJean-Baptiste Maneyrol 		st->buffer[0] = offset & 0xFF;
533a095fadbSJean-Baptiste Maneyrol 		st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
534a095fadbSJean-Baptiste Maneyrol 		break;
535a095fadbSJean-Baptiste Maneyrol 	default:
536a095fadbSJean-Baptiste Maneyrol 		ret = -EINVAL;
537a095fadbSJean-Baptiste Maneyrol 		goto out_unlock;
538a095fadbSJean-Baptiste Maneyrol 	}
539a095fadbSJean-Baptiste Maneyrol 
540a095fadbSJean-Baptiste Maneyrol 	ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
541a095fadbSJean-Baptiste Maneyrol 
542a095fadbSJean-Baptiste Maneyrol out_unlock:
543a095fadbSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
544a095fadbSJean-Baptiste Maneyrol 	pm_runtime_mark_last_busy(dev);
545a095fadbSJean-Baptiste Maneyrol 	pm_runtime_put_autosuspend(dev);
546a095fadbSJean-Baptiste Maneyrol 	return ret;
547a095fadbSJean-Baptiste Maneyrol }
548a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)549a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_raw(struct iio_dev *indio_dev,
550a095fadbSJean-Baptiste Maneyrol 				      struct iio_chan_spec const *chan,
551a095fadbSJean-Baptiste Maneyrol 				      int *val, int *val2, long mask)
552a095fadbSJean-Baptiste Maneyrol {
553a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
554a095fadbSJean-Baptiste Maneyrol 	int16_t data;
555a095fadbSJean-Baptiste Maneyrol 	int ret;
556a095fadbSJean-Baptiste Maneyrol 
557bc3eb020SJean-Baptiste Maneyrol 	switch (chan->type) {
558bc3eb020SJean-Baptiste Maneyrol 	case IIO_ANGL_VEL:
559bc3eb020SJean-Baptiste Maneyrol 		break;
560bc3eb020SJean-Baptiste Maneyrol 	case IIO_TEMP:
561bc3eb020SJean-Baptiste Maneyrol 		return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
562bc3eb020SJean-Baptiste Maneyrol 	default:
563a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
564bc3eb020SJean-Baptiste Maneyrol 	}
565a095fadbSJean-Baptiste Maneyrol 
566a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
567a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_RAW:
568a095fadbSJean-Baptiste Maneyrol 		ret = iio_device_claim_direct_mode(indio_dev);
569a095fadbSJean-Baptiste Maneyrol 		if (ret)
570a095fadbSJean-Baptiste Maneyrol 			return ret;
571a095fadbSJean-Baptiste Maneyrol 		ret = inv_icm42600_gyro_read_sensor(st, chan, &data);
572a095fadbSJean-Baptiste Maneyrol 		iio_device_release_direct_mode(indio_dev);
573a095fadbSJean-Baptiste Maneyrol 		if (ret)
574a095fadbSJean-Baptiste Maneyrol 			return ret;
575a095fadbSJean-Baptiste Maneyrol 		*val = data;
576a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT;
577a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
578a095fadbSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_read_scale(st, val, val2);
579a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
580a095fadbSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_read_odr(st, val, val2);
581a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
582a095fadbSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_read_offset(st, chan, val, val2);
583a095fadbSJean-Baptiste Maneyrol 	default:
584a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
585a095fadbSJean-Baptiste Maneyrol 	}
586a095fadbSJean-Baptiste Maneyrol }
587a095fadbSJean-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)588a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev,
589a095fadbSJean-Baptiste Maneyrol 					struct iio_chan_spec const *chan,
590a095fadbSJean-Baptiste Maneyrol 					const int **vals,
591a095fadbSJean-Baptiste Maneyrol 					int *type, int *length, long mask)
592a095fadbSJean-Baptiste Maneyrol {
593a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
594a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
595a095fadbSJean-Baptiste Maneyrol 
596a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
597a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
598a095fadbSJean-Baptiste Maneyrol 		*vals = inv_icm42600_gyro_scale;
599a095fadbSJean-Baptiste Maneyrol 		*type = IIO_VAL_INT_PLUS_NANO;
600a095fadbSJean-Baptiste Maneyrol 		*length = ARRAY_SIZE(inv_icm42600_gyro_scale);
601a095fadbSJean-Baptiste Maneyrol 		return IIO_AVAIL_LIST;
602a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
603a095fadbSJean-Baptiste Maneyrol 		*vals = inv_icm42600_gyro_odr;
604a095fadbSJean-Baptiste Maneyrol 		*type = IIO_VAL_INT_PLUS_MICRO;
605a095fadbSJean-Baptiste Maneyrol 		*length = ARRAY_SIZE(inv_icm42600_gyro_odr);
606a095fadbSJean-Baptiste Maneyrol 		return IIO_AVAIL_LIST;
607a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
608a095fadbSJean-Baptiste Maneyrol 		*vals = inv_icm42600_gyro_calibbias;
609a095fadbSJean-Baptiste Maneyrol 		*type = IIO_VAL_INT_PLUS_NANO;
610a095fadbSJean-Baptiste Maneyrol 		return IIO_AVAIL_RANGE;
611a095fadbSJean-Baptiste Maneyrol 	default:
612a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
613a095fadbSJean-Baptiste Maneyrol 	}
614a095fadbSJean-Baptiste Maneyrol }
615a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)616a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev,
617a095fadbSJean-Baptiste Maneyrol 				       struct iio_chan_spec const *chan,
618a095fadbSJean-Baptiste Maneyrol 				       int val, int val2, long mask)
619a095fadbSJean-Baptiste Maneyrol {
620a095fadbSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
621a095fadbSJean-Baptiste Maneyrol 	int ret;
622a095fadbSJean-Baptiste Maneyrol 
623a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
624a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
625a095fadbSJean-Baptiste Maneyrol 
626a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
627a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
628a095fadbSJean-Baptiste Maneyrol 		ret = iio_device_claim_direct_mode(indio_dev);
629a095fadbSJean-Baptiste Maneyrol 		if (ret)
630a095fadbSJean-Baptiste Maneyrol 			return ret;
631a095fadbSJean-Baptiste Maneyrol 		ret = inv_icm42600_gyro_write_scale(st, val, val2);
632a095fadbSJean-Baptiste Maneyrol 		iio_device_release_direct_mode(indio_dev);
633a095fadbSJean-Baptiste Maneyrol 		return ret;
634a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
635ec74ae9fSJean-Baptiste Maneyrol 		return inv_icm42600_gyro_write_odr(indio_dev, val, val2);
636a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
637a095fadbSJean-Baptiste Maneyrol 		ret = iio_device_claim_direct_mode(indio_dev);
638a095fadbSJean-Baptiste Maneyrol 		if (ret)
639a095fadbSJean-Baptiste Maneyrol 			return ret;
640a095fadbSJean-Baptiste Maneyrol 		ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
641a095fadbSJean-Baptiste Maneyrol 		iio_device_release_direct_mode(indio_dev);
642a095fadbSJean-Baptiste Maneyrol 		return ret;
643a095fadbSJean-Baptiste Maneyrol 	default:
644a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
645a095fadbSJean-Baptiste Maneyrol 	}
646a095fadbSJean-Baptiste Maneyrol }
647a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)648a095fadbSJean-Baptiste Maneyrol static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
649a095fadbSJean-Baptiste Maneyrol 					       struct iio_chan_spec const *chan,
650a095fadbSJean-Baptiste Maneyrol 					       long mask)
651a095fadbSJean-Baptiste Maneyrol {
652a095fadbSJean-Baptiste Maneyrol 	if (chan->type != IIO_ANGL_VEL)
653a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
654a095fadbSJean-Baptiste Maneyrol 
655a095fadbSJean-Baptiste Maneyrol 	switch (mask) {
656a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SCALE:
657a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT_PLUS_NANO;
658a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_SAMP_FREQ:
659a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT_PLUS_MICRO;
660a095fadbSJean-Baptiste Maneyrol 	case IIO_CHAN_INFO_CALIBBIAS:
661a095fadbSJean-Baptiste Maneyrol 		return IIO_VAL_INT_PLUS_NANO;
662a095fadbSJean-Baptiste Maneyrol 	default:
663a095fadbSJean-Baptiste Maneyrol 		return -EINVAL;
664a095fadbSJean-Baptiste Maneyrol 	}
665a095fadbSJean-Baptiste Maneyrol }
666a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev * indio_dev,unsigned int val)6677f85e42aSJean-Baptiste Maneyrol static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
6687f85e42aSJean-Baptiste Maneyrol 						  unsigned int val)
6697f85e42aSJean-Baptiste Maneyrol {
6707f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
6717f85e42aSJean-Baptiste Maneyrol 	int ret;
6727f85e42aSJean-Baptiste Maneyrol 
6737f85e42aSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
6747f85e42aSJean-Baptiste Maneyrol 
6757f85e42aSJean-Baptiste Maneyrol 	st->fifo.watermark.gyro = val;
6767f85e42aSJean-Baptiste Maneyrol 	ret = inv_icm42600_buffer_update_watermark(st);
6777f85e42aSJean-Baptiste Maneyrol 
6787f85e42aSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
6797f85e42aSJean-Baptiste Maneyrol 
6807f85e42aSJean-Baptiste Maneyrol 	return ret;
6817f85e42aSJean-Baptiste Maneyrol }
6827f85e42aSJean-Baptiste Maneyrol 
inv_icm42600_gyro_hwfifo_flush(struct iio_dev * indio_dev,unsigned int count)6837f85e42aSJean-Baptiste Maneyrol static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
6847f85e42aSJean-Baptiste Maneyrol 					  unsigned int count)
6857f85e42aSJean-Baptiste Maneyrol {
6867f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
6877f85e42aSJean-Baptiste Maneyrol 	int ret;
6887f85e42aSJean-Baptiste Maneyrol 
6897f85e42aSJean-Baptiste Maneyrol 	if (count == 0)
6907f85e42aSJean-Baptiste Maneyrol 		return 0;
6917f85e42aSJean-Baptiste Maneyrol 
6927f85e42aSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
6937f85e42aSJean-Baptiste Maneyrol 
6947f85e42aSJean-Baptiste Maneyrol 	ret = inv_icm42600_buffer_hwfifo_flush(st, count);
6957f85e42aSJean-Baptiste Maneyrol 	if (!ret)
6967f85e42aSJean-Baptiste Maneyrol 		ret = st->fifo.nb.gyro;
6977f85e42aSJean-Baptiste Maneyrol 
6987f85e42aSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
6997f85e42aSJean-Baptiste Maneyrol 
7007f85e42aSJean-Baptiste Maneyrol 	return ret;
7017f85e42aSJean-Baptiste Maneyrol }
7027f85e42aSJean-Baptiste Maneyrol 
703a095fadbSJean-Baptiste Maneyrol static const struct iio_info inv_icm42600_gyro_info = {
704a095fadbSJean-Baptiste Maneyrol 	.read_raw = inv_icm42600_gyro_read_raw,
705a095fadbSJean-Baptiste Maneyrol 	.read_avail = inv_icm42600_gyro_read_avail,
706a095fadbSJean-Baptiste Maneyrol 	.write_raw = inv_icm42600_gyro_write_raw,
707a095fadbSJean-Baptiste Maneyrol 	.write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt,
708a095fadbSJean-Baptiste Maneyrol 	.debugfs_reg_access = inv_icm42600_debugfs_reg,
7097f85e42aSJean-Baptiste Maneyrol 	.update_scan_mode = inv_icm42600_gyro_update_scan_mode,
7107f85e42aSJean-Baptiste Maneyrol 	.hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark,
7117f85e42aSJean-Baptiste Maneyrol 	.hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush,
712a095fadbSJean-Baptiste Maneyrol };
713a095fadbSJean-Baptiste Maneyrol 
inv_icm42600_gyro_init(struct inv_icm42600_state * st)714a095fadbSJean-Baptiste Maneyrol struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
715a095fadbSJean-Baptiste Maneyrol {
716a095fadbSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(st->map);
717a095fadbSJean-Baptiste Maneyrol 	const char *name;
718*0ecc363cSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp_chip ts_chip;
719*0ecc363cSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp *ts;
720a095fadbSJean-Baptiste Maneyrol 	struct iio_dev *indio_dev;
721a095fadbSJean-Baptiste Maneyrol 	int ret;
722a095fadbSJean-Baptiste Maneyrol 
723a095fadbSJean-Baptiste Maneyrol 	name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
724a095fadbSJean-Baptiste Maneyrol 	if (!name)
725a095fadbSJean-Baptiste Maneyrol 		return ERR_PTR(-ENOMEM);
726a095fadbSJean-Baptiste Maneyrol 
727ec74ae9fSJean-Baptiste Maneyrol 	indio_dev = devm_iio_device_alloc(dev, sizeof(*ts));
728a095fadbSJean-Baptiste Maneyrol 	if (!indio_dev)
729a095fadbSJean-Baptiste Maneyrol 		return ERR_PTR(-ENOMEM);
730a095fadbSJean-Baptiste Maneyrol 
731*0ecc363cSJean-Baptiste Maneyrol 	/*
732*0ecc363cSJean-Baptiste Maneyrol 	 * clock period is 32kHz (31250ns)
733*0ecc363cSJean-Baptiste Maneyrol 	 * jitter is +/- 2% (20 per mille)
734*0ecc363cSJean-Baptiste Maneyrol 	 */
735*0ecc363cSJean-Baptiste Maneyrol 	ts_chip.clock_period = 31250;
736*0ecc363cSJean-Baptiste Maneyrol 	ts_chip.jitter = 20;
737*0ecc363cSJean-Baptiste Maneyrol 	ts_chip.init_period = inv_icm42600_odr_to_period(st->conf.accel.odr);
738ec74ae9fSJean-Baptiste Maneyrol 	ts = iio_priv(indio_dev);
739*0ecc363cSJean-Baptiste Maneyrol 	inv_sensors_timestamp_init(ts, &ts_chip);
740ec74ae9fSJean-Baptiste Maneyrol 
741a095fadbSJean-Baptiste Maneyrol 	iio_device_set_drvdata(indio_dev, st);
742a095fadbSJean-Baptiste Maneyrol 	indio_dev->name = name;
743a095fadbSJean-Baptiste Maneyrol 	indio_dev->info = &inv_icm42600_gyro_info;
74417395ce2SAlexandru Ardelean 	indio_dev->modes = INDIO_DIRECT_MODE;
745a095fadbSJean-Baptiste Maneyrol 	indio_dev->channels = inv_icm42600_gyro_channels;
746a095fadbSJean-Baptiste Maneyrol 	indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels);
7477f85e42aSJean-Baptiste Maneyrol 	indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks;
7487f85e42aSJean-Baptiste Maneyrol 	indio_dev->setup_ops = &inv_icm42600_buffer_ops;
7497f85e42aSJean-Baptiste Maneyrol 
75017395ce2SAlexandru Ardelean 	ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
75117395ce2SAlexandru Ardelean 					  &inv_icm42600_buffer_ops);
75217395ce2SAlexandru Ardelean 	if (ret)
75317395ce2SAlexandru Ardelean 		return ERR_PTR(ret);
754a095fadbSJean-Baptiste Maneyrol 
755a095fadbSJean-Baptiste Maneyrol 	ret = devm_iio_device_register(dev, indio_dev);
756a095fadbSJean-Baptiste Maneyrol 	if (ret)
757a095fadbSJean-Baptiste Maneyrol 		return ERR_PTR(ret);
758a095fadbSJean-Baptiste Maneyrol 
759a095fadbSJean-Baptiste Maneyrol 	return indio_dev;
760a095fadbSJean-Baptiste Maneyrol }
7617f85e42aSJean-Baptiste Maneyrol 
inv_icm42600_gyro_parse_fifo(struct iio_dev * indio_dev)7627f85e42aSJean-Baptiste Maneyrol int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev)
7637f85e42aSJean-Baptiste Maneyrol {
7647f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
765*0ecc363cSJean-Baptiste Maneyrol 	struct inv_sensors_timestamp *ts = iio_priv(indio_dev);
7667f85e42aSJean-Baptiste Maneyrol 	ssize_t i, size;
767ec74ae9fSJean-Baptiste Maneyrol 	unsigned int no;
7687f85e42aSJean-Baptiste Maneyrol 	const void *accel, *gyro, *timestamp;
7697f85e42aSJean-Baptiste Maneyrol 	const int8_t *temp;
7707f85e42aSJean-Baptiste Maneyrol 	unsigned int odr;
771ec74ae9fSJean-Baptiste Maneyrol 	int64_t ts_val;
7727f85e42aSJean-Baptiste Maneyrol 	struct inv_icm42600_gyro_buffer buffer;
7737f85e42aSJean-Baptiste Maneyrol 
7747f85e42aSJean-Baptiste Maneyrol 	/* parse all fifo packets */
775ec74ae9fSJean-Baptiste Maneyrol 	for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) {
7767f85e42aSJean-Baptiste Maneyrol 		size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
7777f85e42aSJean-Baptiste Maneyrol 				&accel, &gyro, &temp, &timestamp, &odr);
7787f85e42aSJean-Baptiste Maneyrol 		/* quit if error or FIFO is empty */
7797f85e42aSJean-Baptiste Maneyrol 		if (size <= 0)
7807f85e42aSJean-Baptiste Maneyrol 			return size;
7817f85e42aSJean-Baptiste Maneyrol 
7827f85e42aSJean-Baptiste Maneyrol 		/* skip packet if no gyro data or data is invalid */
7837f85e42aSJean-Baptiste Maneyrol 		if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
7847f85e42aSJean-Baptiste Maneyrol 			continue;
7857f85e42aSJean-Baptiste Maneyrol 
786ec74ae9fSJean-Baptiste Maneyrol 		/* update odr */
787ec74ae9fSJean-Baptiste Maneyrol 		if (odr & INV_ICM42600_SENSOR_GYRO)
788*0ecc363cSJean-Baptiste Maneyrol 			inv_sensors_timestamp_apply_odr(ts, st->fifo.period,
789ec74ae9fSJean-Baptiste Maneyrol 							st->fifo.nb.total, no);
790ec74ae9fSJean-Baptiste Maneyrol 
7917f85e42aSJean-Baptiste Maneyrol 		/* buffer is copied to userspace, zeroing it to avoid any data leak */
7927f85e42aSJean-Baptiste Maneyrol 		memset(&buffer, 0, sizeof(buffer));
7937f85e42aSJean-Baptiste Maneyrol 		memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro));
7947f85e42aSJean-Baptiste Maneyrol 		/* convert 8 bits FIFO temperature in high resolution format */
7957f85e42aSJean-Baptiste Maneyrol 		buffer.temp = temp ? (*temp * 64) : 0;
796*0ecc363cSJean-Baptiste Maneyrol 		ts_val = inv_sensors_timestamp_pop(ts);
797ec74ae9fSJean-Baptiste Maneyrol 		iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val);
7987f85e42aSJean-Baptiste Maneyrol 	}
7997f85e42aSJean-Baptiste Maneyrol 
8007f85e42aSJean-Baptiste Maneyrol 	return 0;
8017f85e42aSJean-Baptiste Maneyrol }
802