[go: nahoru, domu]

1/*
2 * adis16400.c	support Analog Devices ADIS16400/5
3 *		3d 2g Linear Accelerometers,
4 *		3d Gyroscopes,
5 *		3d Magnetometers via SPI
6 *
7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
9 * Copyright (c) 2011 Analog Devices Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/delay.h>
20#include <linux/mutex.h>
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/spi/spi.h>
24#include <linux/slab.h>
25#include <linux/sysfs.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/debugfs.h>
29
30#include <linux/iio/iio.h>
31#include <linux/iio/sysfs.h>
32#include <linux/iio/buffer.h>
33
34#include "adis16400.h"
35
36#ifdef CONFIG_DEBUG_FS
37
38static ssize_t adis16400_show_serial_number(struct file *file,
39		char __user *userbuf, size_t count, loff_t *ppos)
40{
41	struct adis16400_state *st = file->private_data;
42	u16 lot1, lot2, serial_number;
43	char buf[16];
44	size_t len;
45	int ret;
46
47	ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1);
48	if (ret < 0)
49		return ret;
50
51	ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2);
52	if (ret < 0)
53		return ret;
54
55	ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER,
56			&serial_number);
57	if (ret < 0)
58		return ret;
59
60	len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2,
61			serial_number);
62
63	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
64}
65
66static const struct file_operations adis16400_serial_number_fops = {
67	.open = simple_open,
68	.read = adis16400_show_serial_number,
69	.llseek = default_llseek,
70	.owner = THIS_MODULE,
71};
72
73static int adis16400_show_product_id(void *arg, u64 *val)
74{
75	struct adis16400_state *st = arg;
76	uint16_t prod_id;
77	int ret;
78
79	ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id);
80	if (ret < 0)
81		return ret;
82
83	*val = prod_id;
84
85	return 0;
86}
87DEFINE_SIMPLE_ATTRIBUTE(adis16400_product_id_fops,
88	adis16400_show_product_id, NULL, "%lld\n");
89
90static int adis16400_show_flash_count(void *arg, u64 *val)
91{
92	struct adis16400_state *st = arg;
93	uint16_t flash_count;
94	int ret;
95
96	ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count);
97	if (ret < 0)
98		return ret;
99
100	*val = flash_count;
101
102	return 0;
103}
104DEFINE_SIMPLE_ATTRIBUTE(adis16400_flash_count_fops,
105	adis16400_show_flash_count, NULL, "%lld\n");
106
107static int adis16400_debugfs_init(struct iio_dev *indio_dev)
108{
109	struct adis16400_state *st = iio_priv(indio_dev);
110
111	if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER)
112		debugfs_create_file("serial_number", 0400,
113			indio_dev->debugfs_dentry, st,
114			&adis16400_serial_number_fops);
115	if (st->variant->flags & ADIS16400_HAS_PROD_ID)
116		debugfs_create_file("product_id", 0400,
117			indio_dev->debugfs_dentry, st,
118			&adis16400_product_id_fops);
119	debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
120		st, &adis16400_flash_count_fops);
121
122	return 0;
123}
124
125#else
126
127static int adis16400_debugfs_init(struct iio_dev *indio_dev)
128{
129	return 0;
130}
131
132#endif
133
134enum adis16400_chip_variant {
135	ADIS16300,
136	ADIS16334,
137	ADIS16350,
138	ADIS16360,
139	ADIS16362,
140	ADIS16364,
141	ADIS16400,
142	ADIS16448,
143};
144
145static int adis16334_get_freq(struct adis16400_state *st)
146{
147	int ret;
148	uint16_t t;
149
150	ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
151	if (ret < 0)
152		return ret;
153
154	t >>= ADIS16334_RATE_DIV_SHIFT;
155
156	return 819200 >> t;
157}
158
159static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
160{
161	unsigned int t;
162
163	if (freq < 819200)
164		t = ilog2(819200 / freq);
165	else
166		t = 0;
167
168	if (t > 0x31)
169		t = 0x31;
170
171	t <<= ADIS16334_RATE_DIV_SHIFT;
172	t |= ADIS16334_RATE_INT_CLK;
173
174	return adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
175}
176
177static int adis16400_get_freq(struct adis16400_state *st)
178{
179	int sps, ret;
180	uint16_t t;
181
182	ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
183	if (ret < 0)
184		return ret;
185
186	sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
187	sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
188
189	return sps;
190}
191
192static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
193{
194	unsigned int t;
195	uint8_t val = 0;
196
197	t = 1638404 / freq;
198	if (t >= 128) {
199		val |= ADIS16400_SMPL_PRD_TIME_BASE;
200		t = 52851 / freq;
201		if (t >= 128)
202			t = 127;
203	} else if (t != 0) {
204		t--;
205	}
206
207	val |= t;
208
209	if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
210		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
211	else
212		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
213
214	return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
215}
216
217static const unsigned adis16400_3db_divisors[] = {
218	[0] = 2, /* Special case */
219	[1] = 6,
220	[2] = 12,
221	[3] = 25,
222	[4] = 50,
223	[5] = 100,
224	[6] = 200,
225	[7] = 200, /* Not a valid setting */
226};
227
228static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
229{
230	struct adis16400_state *st = iio_priv(indio_dev);
231	uint16_t val16;
232	int i, ret;
233
234	for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
235		if (sps / adis16400_3db_divisors[i] >= val)
236			break;
237	}
238
239	ret = adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
240	if (ret < 0)
241		return ret;
242
243	ret = adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
244					 (val16 & ~0x07) | i);
245	return ret;
246}
247
248/* Power down the device */
249static int adis16400_stop_device(struct iio_dev *indio_dev)
250{
251	struct adis16400_state *st = iio_priv(indio_dev);
252	int ret;
253
254	ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
255			ADIS16400_SLP_CNT_POWER_OFF);
256	if (ret)
257		dev_err(&indio_dev->dev,
258			"problem with turning device off: SLP_CNT");
259
260	return ret;
261}
262
263static int adis16400_initial_setup(struct iio_dev *indio_dev)
264{
265	struct adis16400_state *st = iio_priv(indio_dev);
266	uint16_t prod_id, smp_prd;
267	unsigned int device_id;
268	int ret;
269
270	/* use low spi speed for init if the device has a slow mode */
271	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
272		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
273	else
274		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
275	st->adis.spi->mode = SPI_MODE_3;
276	spi_setup(st->adis.spi);
277
278	ret = adis_initial_startup(&st->adis);
279	if (ret)
280		return ret;
281
282	if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
283		ret = adis_read_reg_16(&st->adis,
284						ADIS16400_PRODUCT_ID, &prod_id);
285		if (ret)
286			goto err_ret;
287
288		sscanf(indio_dev->name, "adis%u\n", &device_id);
289
290		if (prod_id != device_id)
291			dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
292					device_id, prod_id);
293
294		dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
295			indio_dev->name, prod_id,
296			st->adis.spi->chip_select, st->adis.spi->irq);
297	}
298	/* use high spi speed if possible */
299	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
300		ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
301		if (ret)
302			goto err_ret;
303
304		if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
305			st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
306			spi_setup(st->adis.spi);
307		}
308	}
309
310err_ret:
311	return ret;
312}
313
314static const uint8_t adis16400_addresses[] = {
315	[ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
316	[ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
317	[ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
318	[ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
319	[ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
320	[ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
321};
322
323static int adis16400_write_raw(struct iio_dev *indio_dev,
324	struct iio_chan_spec const *chan, int val, int val2, long info)
325{
326	struct adis16400_state *st = iio_priv(indio_dev);
327	int ret, sps;
328
329	switch (info) {
330	case IIO_CHAN_INFO_CALIBBIAS:
331		mutex_lock(&indio_dev->mlock);
332		ret = adis_write_reg_16(&st->adis,
333				adis16400_addresses[chan->scan_index], val);
334		mutex_unlock(&indio_dev->mlock);
335		return ret;
336	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
337		/*
338		 * Need to cache values so we can update if the frequency
339		 * changes.
340		 */
341		mutex_lock(&indio_dev->mlock);
342		st->filt_int = val;
343		/* Work out update to current value */
344		sps = st->variant->get_freq(st);
345		if (sps < 0) {
346			mutex_unlock(&indio_dev->mlock);
347			return sps;
348		}
349
350		ret = adis16400_set_filter(indio_dev, sps,
351			val * 1000 + val2 / 1000);
352		mutex_unlock(&indio_dev->mlock);
353		return ret;
354	case IIO_CHAN_INFO_SAMP_FREQ:
355		sps = val * 1000 + val2 / 1000;
356
357		if (sps <= 0)
358			return -EINVAL;
359
360		mutex_lock(&indio_dev->mlock);
361		ret = st->variant->set_freq(st, sps);
362		mutex_unlock(&indio_dev->mlock);
363		return ret;
364	default:
365		return -EINVAL;
366	}
367}
368
369static int adis16400_read_raw(struct iio_dev *indio_dev,
370	struct iio_chan_spec const *chan, int *val, int *val2, long info)
371{
372	struct adis16400_state *st = iio_priv(indio_dev);
373	int16_t val16;
374	int ret;
375
376	switch (info) {
377	case IIO_CHAN_INFO_RAW:
378		return adis_single_conversion(indio_dev, chan, 0, val);
379	case IIO_CHAN_INFO_SCALE:
380		switch (chan->type) {
381		case IIO_ANGL_VEL:
382			*val = 0;
383			*val2 = st->variant->gyro_scale_micro;
384			return IIO_VAL_INT_PLUS_MICRO;
385		case IIO_VOLTAGE:
386			*val = 0;
387			if (chan->channel == 0) {
388				*val = 2;
389				*val2 = 418000; /* 2.418 mV */
390			} else {
391				*val = 0;
392				*val2 = 805800; /* 805.8 uV */
393			}
394			return IIO_VAL_INT_PLUS_MICRO;
395		case IIO_ACCEL:
396			*val = 0;
397			*val2 = st->variant->accel_scale_micro;
398			return IIO_VAL_INT_PLUS_MICRO;
399		case IIO_MAGN:
400			*val = 0;
401			*val2 = 500; /* 0.5 mgauss */
402			return IIO_VAL_INT_PLUS_MICRO;
403		case IIO_TEMP:
404			*val = st->variant->temp_scale_nano / 1000000;
405			*val2 = (st->variant->temp_scale_nano % 1000000);
406			return IIO_VAL_INT_PLUS_MICRO;
407		default:
408			return -EINVAL;
409		}
410	case IIO_CHAN_INFO_CALIBBIAS:
411		mutex_lock(&indio_dev->mlock);
412		ret = adis_read_reg_16(&st->adis,
413				adis16400_addresses[chan->scan_index], &val16);
414		mutex_unlock(&indio_dev->mlock);
415		if (ret)
416			return ret;
417		val16 = ((val16 & 0xFFF) << 4) >> 4;
418		*val = val16;
419		return IIO_VAL_INT;
420	case IIO_CHAN_INFO_OFFSET:
421		/* currently only temperature */
422		*val = st->variant->temp_offset;
423		return IIO_VAL_INT;
424	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
425		mutex_lock(&indio_dev->mlock);
426		/* Need both the number of taps and the sampling frequency */
427		ret = adis_read_reg_16(&st->adis,
428						ADIS16400_SENS_AVG,
429						&val16);
430		if (ret < 0) {
431			mutex_unlock(&indio_dev->mlock);
432			return ret;
433		}
434		ret = st->variant->get_freq(st);
435		if (ret >= 0) {
436			ret /= adis16400_3db_divisors[val16 & 0x07];
437			*val = ret / 1000;
438			*val2 = (ret % 1000) * 1000;
439		}
440		mutex_unlock(&indio_dev->mlock);
441		if (ret < 0)
442			return ret;
443		return IIO_VAL_INT_PLUS_MICRO;
444	case IIO_CHAN_INFO_SAMP_FREQ:
445		ret = st->variant->get_freq(st);
446		if (ret < 0)
447			return ret;
448		*val = ret / 1000;
449		*val2 = (ret % 1000) * 1000;
450		return IIO_VAL_INT_PLUS_MICRO;
451	default:
452		return -EINVAL;
453	}
454}
455
456#define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si) { \
457	.type = IIO_VOLTAGE, \
458	.indexed = 1, \
459	.channel = 0, \
460	.extend_name = name, \
461	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
462		BIT(IIO_CHAN_INFO_SCALE), \
463	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
464	.address = (addr), \
465	.scan_index = (si), \
466	.scan_type = { \
467		.sign = 'u', \
468		.realbits = (bits), \
469		.storagebits = 16, \
470		.shift = 0, \
471		.endianness = IIO_BE, \
472	}, \
473}
474
475#define ADIS16400_SUPPLY_CHAN(addr, bits) \
476	ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY)
477
478#define ADIS16400_AUX_ADC_CHAN(addr, bits) \
479	ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC)
480
481#define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
482	.type = IIO_ANGL_VEL, \
483	.modified = 1, \
484	.channel2 = IIO_MOD_ ## mod, \
485	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
486		BIT(IIO_CHAN_INFO_CALIBBIAS),		  \
487	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
488		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
489	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
490	.address = addr, \
491	.scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
492	.scan_type = { \
493		.sign = 's', \
494		.realbits = (bits), \
495		.storagebits = 16, \
496		.shift = 0, \
497		.endianness = IIO_BE, \
498	}, \
499}
500
501#define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
502	.type = IIO_ACCEL, \
503	.modified = 1, \
504	.channel2 = IIO_MOD_ ## mod, \
505	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
506		BIT(IIO_CHAN_INFO_CALIBBIAS), \
507	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
508		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
509	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
510	.address = (addr), \
511	.scan_index = ADIS16400_SCAN_ACC_ ## mod, \
512	.scan_type = { \
513		.sign = 's', \
514		.realbits = (bits), \
515		.storagebits = 16, \
516		.shift = 0, \
517		.endianness = IIO_BE, \
518	}, \
519}
520
521#define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
522	.type = IIO_MAGN, \
523	.modified = 1, \
524	.channel2 = IIO_MOD_ ## mod, \
525	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
526	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
527		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
528	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
529	.address = (addr), \
530	.scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
531	.scan_type = { \
532		.sign = 's', \
533		.realbits = (bits), \
534		.storagebits = 16, \
535		.shift = 0, \
536		.endianness = IIO_BE, \
537	}, \
538}
539
540#define ADIS16400_MOD_TEMP_NAME_X "x"
541#define ADIS16400_MOD_TEMP_NAME_Y "y"
542#define ADIS16400_MOD_TEMP_NAME_Z "z"
543
544#define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
545	.type = IIO_TEMP, \
546	.indexed = 1, \
547	.channel = 0, \
548	.extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
549	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
550		BIT(IIO_CHAN_INFO_OFFSET) | \
551		BIT(IIO_CHAN_INFO_SCALE), \
552	.info_mask_shared_by_type = \
553		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
554	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
555	.address = (addr), \
556	.scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
557	.scan_type = { \
558		.sign = 's', \
559		.realbits = (bits), \
560		.storagebits = 16, \
561		.shift = 0, \
562		.endianness = IIO_BE, \
563	}, \
564}
565
566#define ADIS16400_TEMP_CHAN(addr, bits) { \
567	.type = IIO_TEMP, \
568	.indexed = 1, \
569	.channel = 0, \
570	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
571		BIT(IIO_CHAN_INFO_OFFSET) | \
572		BIT(IIO_CHAN_INFO_SCALE), \
573	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
574	.address = (addr), \
575	.scan_index = ADIS16350_SCAN_TEMP_X, \
576	.scan_type = { \
577		.sign = 's', \
578		.realbits = (bits), \
579		.storagebits = 16, \
580		.shift = 0, \
581		.endianness = IIO_BE, \
582	}, \
583}
584
585#define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
586	.type = IIO_INCLI, \
587	.modified = 1, \
588	.channel2 = IIO_MOD_ ## mod, \
589	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
590	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
591	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
592	.address = (addr), \
593	.scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
594	.scan_type = { \
595		.sign = 's', \
596		.realbits = (bits), \
597		.storagebits = 16, \
598		.shift = 0, \
599		.endianness = IIO_BE, \
600	}, \
601}
602
603static const struct iio_chan_spec adis16400_channels[] = {
604	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14),
605	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
606	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
607	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
608	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
609	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
610	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
611	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
612	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
613	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
614	ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12),
615	ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12),
616	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
617};
618
619static const struct iio_chan_spec adis16448_channels[] = {
620	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
621	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
622	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
623	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
624	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
625	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
626	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16),
627	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16),
628	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16),
629	{
630		.type = IIO_PRESSURE,
631		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
632		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
633		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
634		.address = ADIS16448_BARO_OUT,
635		.scan_index = ADIS16400_SCAN_BARO,
636		.scan_type = {
637			.sign = 's',
638			.realbits = 16,
639			.storagebits = 16,
640			.endianness = IIO_BE,
641		},
642	},
643	ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
644	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
645};
646
647static const struct iio_chan_spec adis16350_channels[] = {
648	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
649	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
650	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
651	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
652	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
653	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
654	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
655	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
656	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
657	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
658	ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
659	ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12),
660	ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12),
661	ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12),
662	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
663};
664
665static const struct iio_chan_spec adis16300_channels[] = {
666	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
667	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
668	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
669	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
670	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
671	ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
672	ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
673	ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13),
674	ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13),
675	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
676};
677
678static const struct iio_chan_spec adis16334_channels[] = {
679	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
680	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
681	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
682	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
683	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
684	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
685	ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
686	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
687};
688
689static struct adis16400_chip_info adis16400_chips[] = {
690	[ADIS16300] = {
691		.channels = adis16300_channels,
692		.num_channels = ARRAY_SIZE(adis16300_channels),
693		.flags = ADIS16400_HAS_SLOW_MODE,
694		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
695		.accel_scale_micro = 5884,
696		.temp_scale_nano = 140000000, /* 0.14 C */
697		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
698		.set_freq = adis16400_set_freq,
699		.get_freq = adis16400_get_freq,
700	},
701	[ADIS16334] = {
702		.channels = adis16334_channels,
703		.num_channels = ARRAY_SIZE(adis16334_channels),
704		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
705				ADIS16400_HAS_SERIAL_NUMBER,
706		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
707		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
708		.temp_scale_nano = 67850000, /* 0.06785 C */
709		.temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
710		.set_freq = adis16334_set_freq,
711		.get_freq = adis16334_get_freq,
712	},
713	[ADIS16350] = {
714		.channels = adis16350_channels,
715		.num_channels = ARRAY_SIZE(adis16350_channels),
716		.gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
717		.accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
718		.temp_scale_nano = 145300000, /* 0.1453 C */
719		.temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
720		.flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
721		.set_freq = adis16400_set_freq,
722		.get_freq = adis16400_get_freq,
723	},
724	[ADIS16360] = {
725		.channels = adis16350_channels,
726		.num_channels = ARRAY_SIZE(adis16350_channels),
727		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
728				ADIS16400_HAS_SERIAL_NUMBER,
729		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
730		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
731		.temp_scale_nano = 136000000, /* 0.136 C */
732		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
733		.set_freq = adis16400_set_freq,
734		.get_freq = adis16400_get_freq,
735	},
736	[ADIS16362] = {
737		.channels = adis16350_channels,
738		.num_channels = ARRAY_SIZE(adis16350_channels),
739		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
740				ADIS16400_HAS_SERIAL_NUMBER,
741		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
742		.accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
743		.temp_scale_nano = 136000000, /* 0.136 C */
744		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
745		.set_freq = adis16400_set_freq,
746		.get_freq = adis16400_get_freq,
747	},
748	[ADIS16364] = {
749		.channels = adis16350_channels,
750		.num_channels = ARRAY_SIZE(adis16350_channels),
751		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
752				ADIS16400_HAS_SERIAL_NUMBER,
753		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
754		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
755		.temp_scale_nano = 136000000, /* 0.136 C */
756		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
757		.set_freq = adis16400_set_freq,
758		.get_freq = adis16400_get_freq,
759	},
760	[ADIS16400] = {
761		.channels = adis16400_channels,
762		.num_channels = ARRAY_SIZE(adis16400_channels),
763		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
764		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
765		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
766		.temp_scale_nano = 140000000, /* 0.14 C */
767		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
768		.set_freq = adis16400_set_freq,
769		.get_freq = adis16400_get_freq,
770	},
771	[ADIS16448] = {
772		.channels = adis16448_channels,
773		.num_channels = ARRAY_SIZE(adis16448_channels),
774		.flags = ADIS16400_HAS_PROD_ID |
775				ADIS16400_HAS_SERIAL_NUMBER,
776		.gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
777		.accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
778		.temp_scale_nano = 73860000, /* 0.07386 C */
779		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
780		.set_freq = adis16334_set_freq,
781		.get_freq = adis16334_get_freq,
782	}
783};
784
785static const struct iio_info adis16400_info = {
786	.driver_module = THIS_MODULE,
787	.read_raw = &adis16400_read_raw,
788	.write_raw = &adis16400_write_raw,
789	.update_scan_mode = adis16400_update_scan_mode,
790	.debugfs_reg_access = adis_debugfs_reg_access,
791};
792
793static const unsigned long adis16400_burst_scan_mask[] = {
794	~0UL,
795	0,
796};
797
798static const char * const adis16400_status_error_msgs[] = {
799	[ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
800	[ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
801	[ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
802	[ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
803	[ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
804	[ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
805	[ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
806	[ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
807	[ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
808	[ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
809	[ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
810	[ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
811	[ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
812	[ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
813	[ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
814};
815
816static const struct adis_data adis16400_data = {
817	.msc_ctrl_reg = ADIS16400_MSC_CTRL,
818	.glob_cmd_reg = ADIS16400_GLOB_CMD,
819	.diag_stat_reg = ADIS16400_DIAG_STAT,
820
821	.read_delay = 50,
822	.write_delay = 50,
823
824	.self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,
825	.startup_delay = ADIS16400_STARTUP_DELAY,
826
827	.status_error_msgs = adis16400_status_error_msgs,
828	.status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |
829		BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |
830		BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |
831		BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |
832		BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |
833		BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |
834		BIT(ADIS16400_DIAG_STAT_ALARM2) |
835		BIT(ADIS16400_DIAG_STAT_ALARM1) |
836		BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |
837		BIT(ADIS16400_DIAG_STAT_SELF_TEST) |
838		BIT(ADIS16400_DIAG_STAT_OVERFLOW) |
839		BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |
840		BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |
841		BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |
842		BIT(ADIS16400_DIAG_STAT_POWER_LOW),
843};
844
845static int adis16400_probe(struct spi_device *spi)
846{
847	struct adis16400_state *st;
848	struct iio_dev *indio_dev;
849	int ret;
850
851	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
852	if (indio_dev == NULL)
853		return -ENOMEM;
854
855	st = iio_priv(indio_dev);
856	/* this is only used for removal purposes */
857	spi_set_drvdata(spi, indio_dev);
858
859	/* setup the industrialio driver allocated elements */
860	st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
861	indio_dev->dev.parent = &spi->dev;
862	indio_dev->name = spi_get_device_id(spi)->name;
863	indio_dev->channels = st->variant->channels;
864	indio_dev->num_channels = st->variant->num_channels;
865	indio_dev->info = &adis16400_info;
866	indio_dev->modes = INDIO_DIRECT_MODE;
867
868	if (!(st->variant->flags & ADIS16400_NO_BURST))
869		indio_dev->available_scan_masks = adis16400_burst_scan_mask;
870
871	ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
872	if (ret)
873		return ret;
874
875	ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
876			adis16400_trigger_handler);
877	if (ret)
878		return ret;
879
880	/* Get the device into a sane initial state */
881	ret = adis16400_initial_setup(indio_dev);
882	if (ret)
883		goto error_cleanup_buffer;
884	ret = iio_device_register(indio_dev);
885	if (ret)
886		goto error_cleanup_buffer;
887
888	adis16400_debugfs_init(indio_dev);
889	return 0;
890
891error_cleanup_buffer:
892	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
893	return ret;
894}
895
896static int adis16400_remove(struct spi_device *spi)
897{
898	struct iio_dev *indio_dev = spi_get_drvdata(spi);
899	struct adis16400_state *st = iio_priv(indio_dev);
900
901	iio_device_unregister(indio_dev);
902	adis16400_stop_device(indio_dev);
903
904	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
905
906	return 0;
907}
908
909static const struct spi_device_id adis16400_id[] = {
910	{"adis16300", ADIS16300},
911	{"adis16334", ADIS16334},
912	{"adis16350", ADIS16350},
913	{"adis16354", ADIS16350},
914	{"adis16355", ADIS16350},
915	{"adis16360", ADIS16360},
916	{"adis16362", ADIS16362},
917	{"adis16364", ADIS16364},
918	{"adis16365", ADIS16360},
919	{"adis16400", ADIS16400},
920	{"adis16405", ADIS16400},
921	{"adis16448", ADIS16448},
922	{}
923};
924MODULE_DEVICE_TABLE(spi, adis16400_id);
925
926static struct spi_driver adis16400_driver = {
927	.driver = {
928		.name = "adis16400",
929		.owner = THIS_MODULE,
930	},
931	.id_table = adis16400_id,
932	.probe = adis16400_probe,
933	.remove = adis16400_remove,
934};
935module_spi_driver(adis16400_driver);
936
937MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
938MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
939MODULE_LICENSE("GPL v2");
940