xref: /openbmc/linux/drivers/iio/imu/adis16480.c (revision 588b48ca)
1 /*
2  * ADIS16480 and similar IMUs driver
3  *
4  * Copyright 2012 Analog Devices Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11 
12 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/mutex.h>
15 #include <linux/device.h>
16 #include <linux/kernel.h>
17 #include <linux/spi/spi.h>
18 #include <linux/slab.h>
19 #include <linux/sysfs.h>
20 #include <linux/module.h>
21 
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/imu/adis.h>
26 
27 #include <linux/debugfs.h>
28 
29 #define ADIS16480_PAGE_SIZE 0x80
30 
31 #define ADIS16480_REG(page, reg) ((page) * ADIS16480_PAGE_SIZE + (reg))
32 
33 #define ADIS16480_REG_PAGE_ID 0x00 /* Same address on each page */
34 #define ADIS16480_REG_SEQ_CNT			ADIS16480_REG(0x00, 0x06)
35 #define ADIS16480_REG_SYS_E_FLA			ADIS16480_REG(0x00, 0x08)
36 #define ADIS16480_REG_DIAG_STS			ADIS16480_REG(0x00, 0x0A)
37 #define ADIS16480_REG_ALM_STS			ADIS16480_REG(0x00, 0x0C)
38 #define ADIS16480_REG_TEMP_OUT			ADIS16480_REG(0x00, 0x0E)
39 #define ADIS16480_REG_X_GYRO_OUT		ADIS16480_REG(0x00, 0x10)
40 #define ADIS16480_REG_Y_GYRO_OUT		ADIS16480_REG(0x00, 0x14)
41 #define ADIS16480_REG_Z_GYRO_OUT		ADIS16480_REG(0x00, 0x18)
42 #define ADIS16480_REG_X_ACCEL_OUT		ADIS16480_REG(0x00, 0x1C)
43 #define ADIS16480_REG_Y_ACCEL_OUT		ADIS16480_REG(0x00, 0x20)
44 #define ADIS16480_REG_Z_ACCEL_OUT		ADIS16480_REG(0x00, 0x24)
45 #define ADIS16480_REG_X_MAGN_OUT		ADIS16480_REG(0x00, 0x28)
46 #define ADIS16480_REG_Y_MAGN_OUT		ADIS16480_REG(0x00, 0x2A)
47 #define ADIS16480_REG_Z_MAGN_OUT		ADIS16480_REG(0x00, 0x2C)
48 #define ADIS16480_REG_BAROM_OUT			ADIS16480_REG(0x00, 0x2E)
49 #define ADIS16480_REG_X_DELTAANG_OUT		ADIS16480_REG(0x00, 0x40)
50 #define ADIS16480_REG_Y_DELTAANG_OUT		ADIS16480_REG(0x00, 0x44)
51 #define ADIS16480_REG_Z_DELTAANG_OUT		ADIS16480_REG(0x00, 0x48)
52 #define ADIS16480_REG_X_DELTAVEL_OUT		ADIS16480_REG(0x00, 0x4C)
53 #define ADIS16480_REG_Y_DELTAVEL_OUT		ADIS16480_REG(0x00, 0x50)
54 #define ADIS16480_REG_Z_DELTAVEL_OUT		ADIS16480_REG(0x00, 0x54)
55 #define ADIS16480_REG_PROD_ID			ADIS16480_REG(0x00, 0x7E)
56 
57 #define ADIS16480_REG_X_GYRO_SCALE		ADIS16480_REG(0x02, 0x04)
58 #define ADIS16480_REG_Y_GYRO_SCALE		ADIS16480_REG(0x02, 0x06)
59 #define ADIS16480_REG_Z_GYRO_SCALE		ADIS16480_REG(0x02, 0x08)
60 #define ADIS16480_REG_X_ACCEL_SCALE		ADIS16480_REG(0x02, 0x0A)
61 #define ADIS16480_REG_Y_ACCEL_SCALE		ADIS16480_REG(0x02, 0x0C)
62 #define ADIS16480_REG_Z_ACCEL_SCALE		ADIS16480_REG(0x02, 0x0E)
63 #define ADIS16480_REG_X_GYRO_BIAS		ADIS16480_REG(0x02, 0x10)
64 #define ADIS16480_REG_Y_GYRO_BIAS		ADIS16480_REG(0x02, 0x14)
65 #define ADIS16480_REG_Z_GYRO_BIAS		ADIS16480_REG(0x02, 0x18)
66 #define ADIS16480_REG_X_ACCEL_BIAS		ADIS16480_REG(0x02, 0x1C)
67 #define ADIS16480_REG_Y_ACCEL_BIAS		ADIS16480_REG(0x02, 0x20)
68 #define ADIS16480_REG_Z_ACCEL_BIAS		ADIS16480_REG(0x02, 0x24)
69 #define ADIS16480_REG_X_HARD_IRON		ADIS16480_REG(0x02, 0x28)
70 #define ADIS16480_REG_Y_HARD_IRON		ADIS16480_REG(0x02, 0x2A)
71 #define ADIS16480_REG_Z_HARD_IRON		ADIS16480_REG(0x02, 0x2C)
72 #define ADIS16480_REG_BAROM_BIAS		ADIS16480_REG(0x02, 0x40)
73 #define ADIS16480_REG_FLASH_CNT			ADIS16480_REG(0x02, 0x7C)
74 
75 #define ADIS16480_REG_GLOB_CMD			ADIS16480_REG(0x03, 0x02)
76 #define ADIS16480_REG_FNCTIO_CTRL		ADIS16480_REG(0x03, 0x06)
77 #define ADIS16480_REG_GPIO_CTRL			ADIS16480_REG(0x03, 0x08)
78 #define ADIS16480_REG_CONFIG			ADIS16480_REG(0x03, 0x0A)
79 #define ADIS16480_REG_DEC_RATE			ADIS16480_REG(0x03, 0x0C)
80 #define ADIS16480_REG_SLP_CNT			ADIS16480_REG(0x03, 0x10)
81 #define ADIS16480_REG_FILTER_BNK0		ADIS16480_REG(0x03, 0x16)
82 #define ADIS16480_REG_FILTER_BNK1		ADIS16480_REG(0x03, 0x18)
83 #define ADIS16480_REG_ALM_CNFG0			ADIS16480_REG(0x03, 0x20)
84 #define ADIS16480_REG_ALM_CNFG1			ADIS16480_REG(0x03, 0x22)
85 #define ADIS16480_REG_ALM_CNFG2			ADIS16480_REG(0x03, 0x24)
86 #define ADIS16480_REG_XG_ALM_MAGN		ADIS16480_REG(0x03, 0x28)
87 #define ADIS16480_REG_YG_ALM_MAGN		ADIS16480_REG(0x03, 0x2A)
88 #define ADIS16480_REG_ZG_ALM_MAGN		ADIS16480_REG(0x03, 0x2C)
89 #define ADIS16480_REG_XA_ALM_MAGN		ADIS16480_REG(0x03, 0x2E)
90 #define ADIS16480_REG_YA_ALM_MAGN		ADIS16480_REG(0x03, 0x30)
91 #define ADIS16480_REG_ZA_ALM_MAGN		ADIS16480_REG(0x03, 0x32)
92 #define ADIS16480_REG_XM_ALM_MAGN		ADIS16480_REG(0x03, 0x34)
93 #define ADIS16480_REG_YM_ALM_MAGN		ADIS16480_REG(0x03, 0x36)
94 #define ADIS16480_REG_ZM_ALM_MAGN		ADIS16480_REG(0x03, 0x38)
95 #define ADIS16480_REG_BR_ALM_MAGN		ADIS16480_REG(0x03, 0x3A)
96 #define ADIS16480_REG_FIRM_REV			ADIS16480_REG(0x03, 0x78)
97 #define ADIS16480_REG_FIRM_DM			ADIS16480_REG(0x03, 0x7A)
98 #define ADIS16480_REG_FIRM_Y			ADIS16480_REG(0x03, 0x7C)
99 
100 #define ADIS16480_REG_SERIAL_NUM		ADIS16480_REG(0x04, 0x20)
101 
102 /* Each filter coefficent bank spans two pages */
103 #define ADIS16480_FIR_COEF(page) (x < 60 ? ADIS16480_REG(page, (x) + 8) : \
104 		ADIS16480_REG((page) + 1, (x) - 60 + 8))
105 #define ADIS16480_FIR_COEF_A(x)			ADIS16480_FIR_COEF(0x05, (x))
106 #define ADIS16480_FIR_COEF_B(x)			ADIS16480_FIR_COEF(0x07, (x))
107 #define ADIS16480_FIR_COEF_C(x)			ADIS16480_FIR_COEF(0x09, (x))
108 #define ADIS16480_FIR_COEF_D(x)			ADIS16480_FIR_COEF(0x0B, (x))
109 
110 struct adis16480_chip_info {
111 	unsigned int num_channels;
112 	const struct iio_chan_spec *channels;
113 };
114 
115 struct adis16480 {
116 	const struct adis16480_chip_info *chip_info;
117 
118 	struct adis adis;
119 };
120 
121 #ifdef CONFIG_DEBUG_FS
122 
123 static ssize_t adis16480_show_firmware_revision(struct file *file,
124 		char __user *userbuf, size_t count, loff_t *ppos)
125 {
126 	struct adis16480 *adis16480 = file->private_data;
127 	char buf[7];
128 	size_t len;
129 	u16 rev;
130 	int ret;
131 
132 	ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_REV, &rev);
133 	if (ret < 0)
134 		return ret;
135 
136 	len = scnprintf(buf, sizeof(buf), "%x.%x\n", rev >> 8, rev & 0xff);
137 
138 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
139 }
140 
141 static const struct file_operations adis16480_firmware_revision_fops = {
142 	.open = simple_open,
143 	.read = adis16480_show_firmware_revision,
144 	.llseek = default_llseek,
145 	.owner = THIS_MODULE,
146 };
147 
148 static ssize_t adis16480_show_firmware_date(struct file *file,
149 		char __user *userbuf, size_t count, loff_t *ppos)
150 {
151 	struct adis16480 *adis16480 = file->private_data;
152 	u16 md, year;
153 	char buf[12];
154 	size_t len;
155 	int ret;
156 
157 	ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_Y, &year);
158 	if (ret < 0)
159 		return ret;
160 
161 	ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_DM, &md);
162 	if (ret < 0)
163 		return ret;
164 
165 	len = snprintf(buf, sizeof(buf), "%.2x-%.2x-%.4x\n",
166 			md >> 8, md & 0xff, year);
167 
168 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
169 }
170 
171 static const struct file_operations adis16480_firmware_date_fops = {
172 	.open = simple_open,
173 	.read = adis16480_show_firmware_date,
174 	.llseek = default_llseek,
175 	.owner = THIS_MODULE,
176 };
177 
178 static int adis16480_show_serial_number(void *arg, u64 *val)
179 {
180 	struct adis16480 *adis16480 = arg;
181 	u16 serial;
182 	int ret;
183 
184 	ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_SERIAL_NUM,
185 		&serial);
186 	if (ret < 0)
187 		return ret;
188 
189 	*val = serial;
190 
191 	return 0;
192 }
193 DEFINE_SIMPLE_ATTRIBUTE(adis16480_serial_number_fops,
194 	adis16480_show_serial_number, NULL, "0x%.4llx\n");
195 
196 static int adis16480_show_product_id(void *arg, u64 *val)
197 {
198 	struct adis16480 *adis16480 = arg;
199 	u16 prod_id;
200 	int ret;
201 
202 	ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_PROD_ID,
203 		&prod_id);
204 	if (ret < 0)
205 		return ret;
206 
207 	*val = prod_id;
208 
209 	return 0;
210 }
211 DEFINE_SIMPLE_ATTRIBUTE(adis16480_product_id_fops,
212 	adis16480_show_product_id, NULL, "%llu\n");
213 
214 static int adis16480_show_flash_count(void *arg, u64 *val)
215 {
216 	struct adis16480 *adis16480 = arg;
217 	u32 flash_count;
218 	int ret;
219 
220 	ret = adis_read_reg_32(&adis16480->adis, ADIS16480_REG_FLASH_CNT,
221 		&flash_count);
222 	if (ret < 0)
223 		return ret;
224 
225 	*val = flash_count;
226 
227 	return 0;
228 }
229 DEFINE_SIMPLE_ATTRIBUTE(adis16480_flash_count_fops,
230 	adis16480_show_flash_count, NULL, "%lld\n");
231 
232 static int adis16480_debugfs_init(struct iio_dev *indio_dev)
233 {
234 	struct adis16480 *adis16480 = iio_priv(indio_dev);
235 
236 	debugfs_create_file("firmware_revision", 0400,
237 		indio_dev->debugfs_dentry, adis16480,
238 		&adis16480_firmware_revision_fops);
239 	debugfs_create_file("firmware_date", 0400, indio_dev->debugfs_dentry,
240 		adis16480, &adis16480_firmware_date_fops);
241 	debugfs_create_file("serial_number", 0400, indio_dev->debugfs_dentry,
242 		adis16480, &adis16480_serial_number_fops);
243 	debugfs_create_file("product_id", 0400, indio_dev->debugfs_dentry,
244 		adis16480, &adis16480_product_id_fops);
245 	debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
246 		adis16480, &adis16480_flash_count_fops);
247 
248 	return 0;
249 }
250 
251 #else
252 
253 static int adis16480_debugfs_init(struct iio_dev *indio_dev)
254 {
255 	return 0;
256 }
257 
258 #endif
259 
260 static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2)
261 {
262 	struct adis16480 *st = iio_priv(indio_dev);
263 	unsigned int t;
264 
265 	t =  val * 1000 + val2 / 1000;
266 	if (t <= 0)
267 		return -EINVAL;
268 
269 	t = 2460000 / t;
270 	if (t > 2048)
271 		t = 2048;
272 
273 	if (t != 0)
274 		t--;
275 
276 	return adis_write_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, t);
277 }
278 
279 static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
280 {
281 	struct adis16480 *st = iio_priv(indio_dev);
282 	uint16_t t;
283 	int ret;
284 	unsigned freq;
285 
286 	ret = adis_read_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, &t);
287 	if (ret < 0)
288 		return ret;
289 
290 	freq = 2460000 / (t + 1);
291 	*val = freq / 1000;
292 	*val2 = (freq % 1000) * 1000;
293 
294 	return IIO_VAL_INT_PLUS_MICRO;
295 }
296 
297 enum {
298 	ADIS16480_SCAN_GYRO_X,
299 	ADIS16480_SCAN_GYRO_Y,
300 	ADIS16480_SCAN_GYRO_Z,
301 	ADIS16480_SCAN_ACCEL_X,
302 	ADIS16480_SCAN_ACCEL_Y,
303 	ADIS16480_SCAN_ACCEL_Z,
304 	ADIS16480_SCAN_MAGN_X,
305 	ADIS16480_SCAN_MAGN_Y,
306 	ADIS16480_SCAN_MAGN_Z,
307 	ADIS16480_SCAN_BARO,
308 	ADIS16480_SCAN_TEMP,
309 };
310 
311 static const unsigned int adis16480_calibbias_regs[] = {
312 	[ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_BIAS,
313 	[ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_BIAS,
314 	[ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_BIAS,
315 	[ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_BIAS,
316 	[ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_BIAS,
317 	[ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_BIAS,
318 	[ADIS16480_SCAN_MAGN_X] = ADIS16480_REG_X_HARD_IRON,
319 	[ADIS16480_SCAN_MAGN_Y] = ADIS16480_REG_Y_HARD_IRON,
320 	[ADIS16480_SCAN_MAGN_Z] = ADIS16480_REG_Z_HARD_IRON,
321 	[ADIS16480_SCAN_BARO] = ADIS16480_REG_BAROM_BIAS,
322 };
323 
324 static const unsigned int adis16480_calibscale_regs[] = {
325 	[ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_SCALE,
326 	[ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_SCALE,
327 	[ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_SCALE,
328 	[ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_SCALE,
329 	[ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_SCALE,
330 	[ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_SCALE,
331 };
332 
333 static int adis16480_set_calibbias(struct iio_dev *indio_dev,
334 	const struct iio_chan_spec *chan, int bias)
335 {
336 	unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
337 	struct adis16480 *st = iio_priv(indio_dev);
338 
339 	switch (chan->type) {
340 	case IIO_MAGN:
341 	case IIO_PRESSURE:
342 		if (bias < -0x8000 || bias >= 0x8000)
343 			return -EINVAL;
344 		return adis_write_reg_16(&st->adis, reg, bias);
345 	case IIO_ANGL_VEL:
346 	case IIO_ACCEL:
347 		return adis_write_reg_32(&st->adis, reg, bias);
348 	default:
349 		break;
350 	}
351 
352 	return -EINVAL;
353 }
354 
355 static int adis16480_get_calibbias(struct iio_dev *indio_dev,
356 	const struct iio_chan_spec *chan, int *bias)
357 {
358 	unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
359 	struct adis16480 *st = iio_priv(indio_dev);
360 	uint16_t val16;
361 	uint32_t val32;
362 	int ret;
363 
364 	switch (chan->type) {
365 	case IIO_MAGN:
366 	case IIO_PRESSURE:
367 		ret = adis_read_reg_16(&st->adis, reg, &val16);
368 		*bias = sign_extend32(val16, 15);
369 		break;
370 	case IIO_ANGL_VEL:
371 	case IIO_ACCEL:
372 		ret = adis_read_reg_32(&st->adis, reg, &val32);
373 		*bias = sign_extend32(val32, 31);
374 		break;
375 	default:
376 			ret = -EINVAL;
377 	}
378 
379 	if (ret < 0)
380 		return ret;
381 
382 	return IIO_VAL_INT;
383 }
384 
385 static int adis16480_set_calibscale(struct iio_dev *indio_dev,
386 	const struct iio_chan_spec *chan, int scale)
387 {
388 	unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
389 	struct adis16480 *st = iio_priv(indio_dev);
390 
391 	if (scale < -0x8000 || scale >= 0x8000)
392 		return -EINVAL;
393 
394 	return adis_write_reg_16(&st->adis, reg, scale);
395 }
396 
397 static int adis16480_get_calibscale(struct iio_dev *indio_dev,
398 	const struct iio_chan_spec *chan, int *scale)
399 {
400 	unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
401 	struct adis16480 *st = iio_priv(indio_dev);
402 	uint16_t val16;
403 	int ret;
404 
405 	ret = adis_read_reg_16(&st->adis, reg, &val16);
406 	if (ret < 0)
407 		return ret;
408 
409 	*scale = sign_extend32(val16, 15);
410 	return IIO_VAL_INT;
411 }
412 
413 static const unsigned int adis16480_def_filter_freqs[] = {
414 	310,
415 	55,
416 	275,
417 	63,
418 };
419 
420 static const unsigned int ad16480_filter_data[][2] = {
421 	[ADIS16480_SCAN_GYRO_X]		= { ADIS16480_REG_FILTER_BNK0, 0 },
422 	[ADIS16480_SCAN_GYRO_Y]		= { ADIS16480_REG_FILTER_BNK0, 3 },
423 	[ADIS16480_SCAN_GYRO_Z]		= { ADIS16480_REG_FILTER_BNK0, 6 },
424 	[ADIS16480_SCAN_ACCEL_X]	= { ADIS16480_REG_FILTER_BNK0, 9 },
425 	[ADIS16480_SCAN_ACCEL_Y]	= { ADIS16480_REG_FILTER_BNK0, 12 },
426 	[ADIS16480_SCAN_ACCEL_Z]	= { ADIS16480_REG_FILTER_BNK1, 0 },
427 	[ADIS16480_SCAN_MAGN_X]		= { ADIS16480_REG_FILTER_BNK1, 3 },
428 	[ADIS16480_SCAN_MAGN_Y]		= { ADIS16480_REG_FILTER_BNK1, 6 },
429 	[ADIS16480_SCAN_MAGN_Z]		= { ADIS16480_REG_FILTER_BNK1, 9 },
430 };
431 
432 static int adis16480_get_filter_freq(struct iio_dev *indio_dev,
433 	const struct iio_chan_spec *chan, int *freq)
434 {
435 	struct adis16480 *st = iio_priv(indio_dev);
436 	unsigned int enable_mask, offset, reg;
437 	uint16_t val;
438 	int ret;
439 
440 	reg = ad16480_filter_data[chan->scan_index][0];
441 	offset = ad16480_filter_data[chan->scan_index][1];
442 	enable_mask = BIT(offset + 2);
443 
444 	ret = adis_read_reg_16(&st->adis, reg, &val);
445 	if (ret < 0)
446 		return ret;
447 
448 	if (!(val & enable_mask))
449 		*freq = 0;
450 	else
451 		*freq = adis16480_def_filter_freqs[(val >> offset) & 0x3];
452 
453 	return IIO_VAL_INT;
454 }
455 
456 static int adis16480_set_filter_freq(struct iio_dev *indio_dev,
457 	const struct iio_chan_spec *chan, unsigned int freq)
458 {
459 	struct adis16480 *st = iio_priv(indio_dev);
460 	unsigned int enable_mask, offset, reg;
461 	unsigned int diff, best_diff;
462 	unsigned int i, best_freq;
463 	uint16_t val;
464 	int ret;
465 
466 	reg = ad16480_filter_data[chan->scan_index][0];
467 	offset = ad16480_filter_data[chan->scan_index][1];
468 	enable_mask = BIT(offset + 2);
469 
470 	ret = adis_read_reg_16(&st->adis, reg, &val);
471 	if (ret < 0)
472 		return ret;
473 
474 	if (freq == 0) {
475 		val &= ~enable_mask;
476 	} else {
477 		best_freq = 0;
478 		best_diff = 310;
479 		for (i = 0; i < ARRAY_SIZE(adis16480_def_filter_freqs); i++) {
480 			if (adis16480_def_filter_freqs[i] >= freq) {
481 				diff = adis16480_def_filter_freqs[i] - freq;
482 				if (diff < best_diff) {
483 					best_diff = diff;
484 					best_freq = i;
485 				}
486 			}
487 		}
488 
489 		val &= ~(0x3 << offset);
490 		val |= best_freq << offset;
491 		val |= enable_mask;
492 	}
493 
494 	return adis_write_reg_16(&st->adis, reg, val);
495 }
496 
497 static int adis16480_read_raw(struct iio_dev *indio_dev,
498 	const struct iio_chan_spec *chan, int *val, int *val2, long info)
499 {
500 	switch (info) {
501 	case IIO_CHAN_INFO_RAW:
502 		return adis_single_conversion(indio_dev, chan, 0, val);
503 	case IIO_CHAN_INFO_SCALE:
504 		switch (chan->type) {
505 		case IIO_ANGL_VEL:
506 			*val = 0;
507 			*val2 = IIO_DEGREE_TO_RAD(20000); /* 0.02 degree/sec */
508 			return IIO_VAL_INT_PLUS_MICRO;
509 		case IIO_ACCEL:
510 			*val = 0;
511 			*val2 = IIO_G_TO_M_S_2(800); /* 0.8 mg */
512 			return IIO_VAL_INT_PLUS_MICRO;
513 		case IIO_MAGN:
514 			*val = 0;
515 			*val2 = 100; /* 0.0001 gauss */
516 			return IIO_VAL_INT_PLUS_MICRO;
517 		case IIO_TEMP:
518 			*val = 5;
519 			*val2 = 650000; /* 5.65 milli degree Celsius */
520 			return IIO_VAL_INT_PLUS_MICRO;
521 		case IIO_PRESSURE:
522 			*val = 0;
523 			*val2 = 4000; /* 40ubar = 0.004 kPa */
524 			return IIO_VAL_INT_PLUS_MICRO;
525 		default:
526 			return -EINVAL;
527 		}
528 	case IIO_CHAN_INFO_OFFSET:
529 		/* Only the temperature channel has a offset */
530 		*val = 4425; /* 25 degree Celsius = 0x0000 */
531 		return IIO_VAL_INT;
532 	case IIO_CHAN_INFO_CALIBBIAS:
533 		return adis16480_get_calibbias(indio_dev, chan, val);
534 	case IIO_CHAN_INFO_CALIBSCALE:
535 		return adis16480_get_calibscale(indio_dev, chan, val);
536 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
537 		return adis16480_get_filter_freq(indio_dev, chan, val);
538 	case IIO_CHAN_INFO_SAMP_FREQ:
539 		return adis16480_get_freq(indio_dev, val, val2);
540 	default:
541 		return -EINVAL;
542 	}
543 }
544 
545 static int adis16480_write_raw(struct iio_dev *indio_dev,
546 	const struct iio_chan_spec *chan, int val, int val2, long info)
547 {
548 	switch (info) {
549 	case IIO_CHAN_INFO_CALIBBIAS:
550 		return adis16480_set_calibbias(indio_dev, chan, val);
551 	case IIO_CHAN_INFO_CALIBSCALE:
552 		return adis16480_set_calibscale(indio_dev, chan, val);
553 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
554 		return adis16480_set_filter_freq(indio_dev, chan, val);
555 	case IIO_CHAN_INFO_SAMP_FREQ:
556 		return adis16480_set_freq(indio_dev, val, val2);
557 
558 	default:
559 		return -EINVAL;
560 	}
561 }
562 
563 #define ADIS16480_MOD_CHANNEL(_type, _mod, _address, _si, _info_sep, _bits) \
564 	{ \
565 		.type = (_type), \
566 		.modified = 1, \
567 		.channel2 = (_mod), \
568 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
569 			BIT(IIO_CHAN_INFO_CALIBBIAS) | \
570 			_info_sep, \
571 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
572 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
573 		.address = (_address), \
574 		.scan_index = (_si), \
575 		.scan_type = { \
576 			.sign = 's', \
577 			.realbits = (_bits), \
578 			.storagebits = (_bits), \
579 			.endianness = IIO_BE, \
580 		}, \
581 	}
582 
583 #define ADIS16480_GYRO_CHANNEL(_mod) \
584 	ADIS16480_MOD_CHANNEL(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
585 	ADIS16480_REG_ ## _mod ## _GYRO_OUT, ADIS16480_SCAN_GYRO_ ## _mod, \
586 	BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
587 	BIT(IIO_CHAN_INFO_CALIBSCALE), \
588 	32)
589 
590 #define ADIS16480_ACCEL_CHANNEL(_mod) \
591 	ADIS16480_MOD_CHANNEL(IIO_ACCEL, IIO_MOD_ ## _mod, \
592 	ADIS16480_REG_ ## _mod ## _ACCEL_OUT, ADIS16480_SCAN_ACCEL_ ## _mod, \
593 	BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
594 	BIT(IIO_CHAN_INFO_CALIBSCALE), \
595 	32)
596 
597 #define ADIS16480_MAGN_CHANNEL(_mod) \
598 	ADIS16480_MOD_CHANNEL(IIO_MAGN, IIO_MOD_ ## _mod, \
599 	ADIS16480_REG_ ## _mod ## _MAGN_OUT, ADIS16480_SCAN_MAGN_ ## _mod, \
600 	BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
601 	16)
602 
603 #define ADIS16480_PRESSURE_CHANNEL() \
604 	{ \
605 		.type = IIO_PRESSURE, \
606 		.indexed = 1, \
607 		.channel = 0, \
608 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
609 			BIT(IIO_CHAN_INFO_CALIBBIAS) | \
610 			BIT(IIO_CHAN_INFO_SCALE), \
611 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
612 		.address = ADIS16480_REG_BAROM_OUT, \
613 		.scan_index = ADIS16480_SCAN_BARO, \
614 		.scan_type = { \
615 			.sign = 's', \
616 			.realbits = 32, \
617 			.storagebits = 32, \
618 			.endianness = IIO_BE, \
619 		}, \
620 	}
621 
622 #define ADIS16480_TEMP_CHANNEL() { \
623 		.type = IIO_TEMP, \
624 		.indexed = 1, \
625 		.channel = 0, \
626 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
627 			BIT(IIO_CHAN_INFO_SCALE) | \
628 			BIT(IIO_CHAN_INFO_OFFSET), \
629 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
630 		.address = ADIS16480_REG_TEMP_OUT, \
631 		.scan_index = ADIS16480_SCAN_TEMP, \
632 		.scan_type = { \
633 			.sign = 's', \
634 			.realbits = 16, \
635 			.storagebits = 16, \
636 			.endianness = IIO_BE, \
637 		}, \
638 	}
639 
640 static const struct iio_chan_spec adis16480_channels[] = {
641 	ADIS16480_GYRO_CHANNEL(X),
642 	ADIS16480_GYRO_CHANNEL(Y),
643 	ADIS16480_GYRO_CHANNEL(Z),
644 	ADIS16480_ACCEL_CHANNEL(X),
645 	ADIS16480_ACCEL_CHANNEL(Y),
646 	ADIS16480_ACCEL_CHANNEL(Z),
647 	ADIS16480_MAGN_CHANNEL(X),
648 	ADIS16480_MAGN_CHANNEL(Y),
649 	ADIS16480_MAGN_CHANNEL(Z),
650 	ADIS16480_PRESSURE_CHANNEL(),
651 	ADIS16480_TEMP_CHANNEL(),
652 	IIO_CHAN_SOFT_TIMESTAMP(11)
653 };
654 
655 static const struct iio_chan_spec adis16485_channels[] = {
656 	ADIS16480_GYRO_CHANNEL(X),
657 	ADIS16480_GYRO_CHANNEL(Y),
658 	ADIS16480_GYRO_CHANNEL(Z),
659 	ADIS16480_ACCEL_CHANNEL(X),
660 	ADIS16480_ACCEL_CHANNEL(Y),
661 	ADIS16480_ACCEL_CHANNEL(Z),
662 	ADIS16480_TEMP_CHANNEL(),
663 	IIO_CHAN_SOFT_TIMESTAMP(7)
664 };
665 
666 enum adis16480_variant {
667 	ADIS16375,
668 	ADIS16480,
669 	ADIS16485,
670 	ADIS16488,
671 };
672 
673 static const struct adis16480_chip_info adis16480_chip_info[] = {
674 	[ADIS16375] = {
675 		.channels = adis16485_channels,
676 		.num_channels = ARRAY_SIZE(adis16485_channels),
677 	},
678 	[ADIS16480] = {
679 		.channels = adis16480_channels,
680 		.num_channels = ARRAY_SIZE(adis16480_channels),
681 	},
682 	[ADIS16485] = {
683 		.channels = adis16485_channels,
684 		.num_channels = ARRAY_SIZE(adis16485_channels),
685 	},
686 	[ADIS16488] = {
687 		.channels = adis16480_channels,
688 		.num_channels = ARRAY_SIZE(adis16480_channels),
689 	},
690 };
691 
692 static const struct iio_info adis16480_info = {
693 	.read_raw = &adis16480_read_raw,
694 	.write_raw = &adis16480_write_raw,
695 	.update_scan_mode = adis_update_scan_mode,
696 	.driver_module = THIS_MODULE,
697 };
698 
699 static int adis16480_stop_device(struct iio_dev *indio_dev)
700 {
701 	struct adis16480 *st = iio_priv(indio_dev);
702 	int ret;
703 
704 	ret = adis_write_reg_16(&st->adis, ADIS16480_REG_SLP_CNT, BIT(9));
705 	if (ret)
706 		dev_err(&indio_dev->dev,
707 			"Could not power down device: %d\n", ret);
708 
709 	return ret;
710 }
711 
712 static int adis16480_enable_irq(struct adis *adis, bool enable)
713 {
714 	return adis_write_reg_16(adis, ADIS16480_REG_FNCTIO_CTRL,
715 		enable ? BIT(3) : 0);
716 }
717 
718 static int adis16480_initial_setup(struct iio_dev *indio_dev)
719 {
720 	struct adis16480 *st = iio_priv(indio_dev);
721 	uint16_t prod_id;
722 	unsigned int device_id;
723 	int ret;
724 
725 	adis_reset(&st->adis);
726 	msleep(70);
727 
728 	ret = adis_write_reg_16(&st->adis, ADIS16480_REG_GLOB_CMD, BIT(1));
729 	if (ret)
730 		return ret;
731 	msleep(30);
732 
733 	ret = adis_check_status(&st->adis);
734 	if (ret)
735 		return ret;
736 
737 	ret = adis_read_reg_16(&st->adis, ADIS16480_REG_PROD_ID, &prod_id);
738 	if (ret)
739 		return ret;
740 
741 	sscanf(indio_dev->name, "adis%u\n", &device_id);
742 
743 	if (prod_id != device_id)
744 		dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
745 				device_id, prod_id);
746 
747 	return 0;
748 }
749 
750 #define ADIS16480_DIAG_STAT_XGYRO_FAIL 0
751 #define ADIS16480_DIAG_STAT_YGYRO_FAIL 1
752 #define ADIS16480_DIAG_STAT_ZGYRO_FAIL 2
753 #define ADIS16480_DIAG_STAT_XACCL_FAIL 3
754 #define ADIS16480_DIAG_STAT_YACCL_FAIL 4
755 #define ADIS16480_DIAG_STAT_ZACCL_FAIL 5
756 #define ADIS16480_DIAG_STAT_XMAGN_FAIL 8
757 #define ADIS16480_DIAG_STAT_YMAGN_FAIL 9
758 #define ADIS16480_DIAG_STAT_ZMAGN_FAIL 10
759 #define ADIS16480_DIAG_STAT_BARO_FAIL 11
760 
761 static const char * const adis16480_status_error_msgs[] = {
762 	[ADIS16480_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
763 	[ADIS16480_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
764 	[ADIS16480_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
765 	[ADIS16480_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
766 	[ADIS16480_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
767 	[ADIS16480_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
768 	[ADIS16480_DIAG_STAT_XMAGN_FAIL] = "X-axis magnetometer self-test failure",
769 	[ADIS16480_DIAG_STAT_YMAGN_FAIL] = "Y-axis magnetometer self-test failure",
770 	[ADIS16480_DIAG_STAT_ZMAGN_FAIL] = "Z-axis magnetometer self-test failure",
771 	[ADIS16480_DIAG_STAT_BARO_FAIL] = "Barometer self-test failure",
772 };
773 
774 static const struct adis_data adis16480_data = {
775 	.diag_stat_reg = ADIS16480_REG_DIAG_STS,
776 	.glob_cmd_reg = ADIS16480_REG_GLOB_CMD,
777 	.has_paging = true,
778 
779 	.read_delay = 5,
780 	.write_delay = 5,
781 
782 	.status_error_msgs = adis16480_status_error_msgs,
783 	.status_error_mask = BIT(ADIS16480_DIAG_STAT_XGYRO_FAIL) |
784 		BIT(ADIS16480_DIAG_STAT_YGYRO_FAIL) |
785 		BIT(ADIS16480_DIAG_STAT_ZGYRO_FAIL) |
786 		BIT(ADIS16480_DIAG_STAT_XACCL_FAIL) |
787 		BIT(ADIS16480_DIAG_STAT_YACCL_FAIL) |
788 		BIT(ADIS16480_DIAG_STAT_ZACCL_FAIL) |
789 		BIT(ADIS16480_DIAG_STAT_XMAGN_FAIL) |
790 		BIT(ADIS16480_DIAG_STAT_YMAGN_FAIL) |
791 		BIT(ADIS16480_DIAG_STAT_ZMAGN_FAIL) |
792 		BIT(ADIS16480_DIAG_STAT_BARO_FAIL),
793 
794 	.enable_irq = adis16480_enable_irq,
795 };
796 
797 static int adis16480_probe(struct spi_device *spi)
798 {
799 	const struct spi_device_id *id = spi_get_device_id(spi);
800 	struct iio_dev *indio_dev;
801 	struct adis16480 *st;
802 	int ret;
803 
804 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
805 	if (indio_dev == NULL)
806 		return -ENOMEM;
807 
808 	spi_set_drvdata(spi, indio_dev);
809 
810 	st = iio_priv(indio_dev);
811 
812 	st->chip_info = &adis16480_chip_info[id->driver_data];
813 	indio_dev->dev.parent = &spi->dev;
814 	indio_dev->name = spi_get_device_id(spi)->name;
815 	indio_dev->channels = st->chip_info->channels;
816 	indio_dev->num_channels = st->chip_info->num_channels;
817 	indio_dev->info = &adis16480_info;
818 	indio_dev->modes = INDIO_DIRECT_MODE;
819 
820 	ret = adis_init(&st->adis, indio_dev, spi, &adis16480_data);
821 	if (ret)
822 		return ret;
823 
824 	ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev, NULL);
825 	if (ret)
826 		return ret;
827 
828 	ret = adis16480_initial_setup(indio_dev);
829 	if (ret)
830 		goto error_cleanup_buffer;
831 
832 	ret = iio_device_register(indio_dev);
833 	if (ret)
834 		goto error_stop_device;
835 
836 	adis16480_debugfs_init(indio_dev);
837 
838 	return 0;
839 
840 error_stop_device:
841 	adis16480_stop_device(indio_dev);
842 error_cleanup_buffer:
843 	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
844 	return ret;
845 }
846 
847 static int adis16480_remove(struct spi_device *spi)
848 {
849 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
850 	struct adis16480 *st = iio_priv(indio_dev);
851 
852 	iio_device_unregister(indio_dev);
853 	adis16480_stop_device(indio_dev);
854 
855 	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
856 
857 	return 0;
858 }
859 
860 static const struct spi_device_id adis16480_ids[] = {
861 	{ "adis16375", ADIS16375 },
862 	{ "adis16480", ADIS16480 },
863 	{ "adis16485", ADIS16485 },
864 	{ "adis16488", ADIS16488 },
865 	{ }
866 };
867 MODULE_DEVICE_TABLE(spi, adis16480_ids);
868 
869 static struct spi_driver adis16480_driver = {
870 	.driver = {
871 		.name = "adis16480",
872 		.owner = THIS_MODULE,
873 	},
874 	.id_table = adis16480_ids,
875 	.probe = adis16480_probe,
876 	.remove = adis16480_remove,
877 };
878 module_spi_driver(adis16480_driver);
879 
880 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
881 MODULE_DESCRIPTION("Analog Devices ADIS16480 IMU driver");
882 MODULE_LICENSE("GPL v2");
883