adis16400_core.c 22.8 KB
Newer Older
1 2 3 4 5 6 7
/*
 * adis16400.c	support Analog Devices ADIS16400/5
 *		3d 2g Linear Accelerometers,
 *		3d Gyroscopes,
 *		3d Magnetometers via SPI
 *
 * 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 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/spi/spi.h>
24
#include <linux/slab.h>
25 26
#include <linux/sysfs.h>
#include <linux/list.h>
27
#include <linux/module.h>
28

29 30 31
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/buffer.h>
32

33 34
#include "adis16400.h"

35
enum adis16400_chip_variant {
36
	ADIS16300,
37
	ADIS16334,
38 39 40 41 42 43 44
	ADIS16350,
	ADIS16360,
	ADIS16362,
	ADIS16364,
	ADIS16400,
};

45
static int adis16334_get_freq(struct adis16400_state *st)
46
{
47
	int ret;
48
	uint16_t t;
49

50
	ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
51 52 53 54 55
	if (ret < 0)
		return ret;

	t >>= ADIS16334_RATE_DIV_SHIFT;

56
	return 819200 >> t;
57 58
}

59
static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
60 61 62
{
	unsigned int t;

63 64
	if (freq < 819200)
		t = ilog2(819200 / freq);
65 66
	else
		t = 0;
67 68 69 70 71 72 73

	if (t > 0x31)
		t = 0x31;

	t <<= ADIS16334_RATE_DIV_SHIFT;
	t |= ADIS16334_RATE_INT_CLK;

74
	return adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
75 76
}

77
static int adis16400_get_freq(struct adis16400_state *st)
78
{
79
	int sps, ret;
80
	uint16_t t;
81

82
	ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
83 84
	if (ret < 0)
		return ret;
85

86
	sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
87 88 89 90 91
	sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;

	return sps;
}

92
static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
93 94
{
	unsigned int t;
95
	uint8_t val = 0;
96

97
	t = 1638404 / freq;
98 99 100 101 102 103
	if (t >= 128) {
		val |= ADIS16400_SMPL_PRD_TIME_BASE;
		t = 52851 / freq;
		if (t >= 128)
			t = 127;
	} else if (t != 0) {
104
		t--;
105
	}
106

107 108 109
	val |= t;

	if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
110
		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
111
	else
112
		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
113

114
	return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
115 116
}

117 118 119 120
static ssize_t adis16400_read_frequency(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
121
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
122
	struct adis16400_state *st = iio_priv(indio_dev);
123
	int ret;
124

125
	ret = st->variant->get_freq(st);
126
	if (ret < 0)
127
		return ret;
128

129
	return sprintf(buf, "%d.%.3d\n", ret / 1000, ret % 1000);
130 131
}

132 133
static const unsigned adis16400_3db_divisors[] = {
	[0] = 2, /* Special case */
134 135 136 137 138 139 140
	[1] = 6,
	[2] = 12,
	[3] = 25,
	[4] = 50,
	[5] = 100,
	[6] = 200,
	[7] = 200, /* Not a valid setting */
141 142 143 144
};

static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
{
145
	struct adis16400_state *st = iio_priv(indio_dev);
146
	uint16_t val16;
147
	int i, ret;
148 149 150

	for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
		if (sps / adis16400_3db_divisors[i] >= val)
151
			break;
152 153
	}

154
	ret = adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
155 156
	if (ret < 0)
		return ret;
157

158
	ret = adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
159
					 (val16 & ~0x07) | i);
160 161 162
	return ret;
}

163
static ssize_t adis16400_write_frequency(struct device *dev,
164
	struct device_attribute *attr, const char *buf, size_t len)
165
{
166
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
167
	struct adis16400_state *st = iio_priv(indio_dev);
168
	int i, f, val;
169 170
	int ret;

171
	ret = iio_str_to_fixpoint(buf, 100, &i, &f);
172 173
	if (ret)
		return ret;
174

175 176 177
	val = i * 1000 + f;

	if (val <= 0)
178
		return -EINVAL;
179 180

	mutex_lock(&indio_dev->mlock);
181
	st->variant->set_freq(st, val);
182 183 184 185 186 187
	mutex_unlock(&indio_dev->mlock);

	return ret ? ret : len;
}

/* Power down the device */
188
static int adis16400_stop_device(struct iio_dev *indio_dev)
189
{
190
	struct adis16400_state *st = iio_priv(indio_dev);
191 192
	int ret;

193 194
	ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
			ADIS16400_SLP_CNT_POWER_OFF);
195
	if (ret)
196 197
		dev_err(&indio_dev->dev,
			"problem with turning device off: SLP_CNT");
198 199 200 201

	return ret;
}

202
static int adis16400_initial_setup(struct iio_dev *indio_dev)
203
{
204
	struct adis16400_state *st = iio_priv(indio_dev);
205 206 207
	uint16_t prod_id, smp_prd;
	unsigned int device_id;
	int ret;
208

209 210
	/* use low spi speed for init if the device has a slow mode */
	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
211
		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
212
	else
213 214 215
		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
	st->adis.spi->mode = SPI_MODE_3;
	spi_setup(st->adis.spi);
216

217 218 219
	ret = adis_initial_startup(&st->adis);
	if (ret)
		return ret;
220

221
	if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
222
		ret = adis_read_reg_16(&st->adis,
223 224 225
						ADIS16400_PRODUCT_ID, &prod_id);
		if (ret)
			goto err_ret;
226

227 228 229 230 231
		sscanf(indio_dev->name, "adis%u\n", &device_id);

		if (prod_id != device_id)
			dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
					device_id, prod_id);
232

233
		dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
234 235
			indio_dev->name, prod_id,
			st->adis.spi->chip_select, st->adis.spi->irq);
236
	}
237
	/* use high spi speed if possible */
238
	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
239
		ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
240 241 242 243
		if (ret)
			goto err_ret;

		if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
244 245
			st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
			spi_setup(st->adis.spi);
246
		}
247 248 249 250 251 252 253
	}

err_ret:
	return ret;
}

static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
254 255
			      adis16400_read_frequency,
			      adis16400_write_frequency);
256

257
static const uint8_t adis16400_addresses[] = {
258 259 260 261 262 263
	[ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
	[ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
	[ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
	[ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
	[ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
	[ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
264 265 266
};

static int adis16400_write_raw(struct iio_dev *indio_dev,
267
	struct iio_chan_spec const *chan, int val, int val2, long info)
268
{
269 270
	struct adis16400_state *st = iio_priv(indio_dev);
	int ret, sps;
271

272
	switch (info) {
273
	case IIO_CHAN_INFO_CALIBBIAS:
274
		mutex_lock(&indio_dev->mlock);
275 276
		ret = adis_write_reg_16(&st->adis,
				adis16400_addresses[chan->scan_index], val);
277 278
		mutex_unlock(&indio_dev->mlock);
		return ret;
279
	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
280 281 282 283
		/*
		 * Need to cache values so we can update if the frequency
		 * changes.
		 */
284 285 286
		mutex_lock(&indio_dev->mlock);
		st->filt_int = val;
		/* Work out update to current value */
287
		sps = st->variant->get_freq(st);
288 289 290 291 292
		if (sps < 0) {
			mutex_unlock(&indio_dev->mlock);
			return sps;
		}

293 294
		ret = adis16400_set_filter(indio_dev, sps,
			val * 1000 + val2 / 1000);
295 296
		mutex_unlock(&indio_dev->mlock);
		return ret;
297 298 299 300 301 302
	default:
		return -EINVAL;
	}
}

static int adis16400_read_raw(struct iio_dev *indio_dev,
303
	struct iio_chan_spec const *chan, int *val, int *val2, long info)
304
{
305
	struct adis16400_state *st = iio_priv(indio_dev);
306
	int16_t val16;
307
	int ret;
308

309
	switch (info) {
310
	case IIO_CHAN_INFO_RAW:
311
		return adis_single_conversion(indio_dev, chan, 0, val);
312
	case IIO_CHAN_INFO_SCALE:
313
		switch (chan->type) {
314
		case IIO_ANGL_VEL:
315
			*val = 0;
316
			*val2 = st->variant->gyro_scale_micro;
317
			return IIO_VAL_INT_PLUS_MICRO;
318
		case IIO_VOLTAGE:
319
			*val = 0;
320 321 322 323 324 325 326
			if (chan->channel == 0) {
				*val = 2;
				*val2 = 418000; /* 2.418 mV */
			} else {
				*val = 0;
				*val2 = 805800; /* 805.8 uV */
			}
327 328 329
			return IIO_VAL_INT_PLUS_MICRO;
		case IIO_ACCEL:
			*val = 0;
330
			*val2 = st->variant->accel_scale_micro;
331 332 333
			return IIO_VAL_INT_PLUS_MICRO;
		case IIO_MAGN:
			*val = 0;
334
			*val2 = 500; /* 0.5 mgauss */
335 336
			return IIO_VAL_INT_PLUS_MICRO;
		case IIO_TEMP:
337 338
			*val = st->variant->temp_scale_nano / 1000000;
			*val2 = (st->variant->temp_scale_nano % 1000000);
339 340 341 342
			return IIO_VAL_INT_PLUS_MICRO;
		default:
			return -EINVAL;
		}
343
	case IIO_CHAN_INFO_CALIBBIAS:
344
		mutex_lock(&indio_dev->mlock);
345 346
		ret = adis_read_reg_16(&st->adis,
				adis16400_addresses[chan->scan_index], &val16);
347 348
		mutex_unlock(&indio_dev->mlock);
		if (ret)
349 350 351 352
			return ret;
		val16 = ((val16 & 0xFFF) << 4) >> 4;
		*val = val16;
		return IIO_VAL_INT;
353
	case IIO_CHAN_INFO_OFFSET:
354
		/* currently only temperature */
355
		*val = st->variant->temp_offset;
356
		return IIO_VAL_INT;
357 358 359
	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
		mutex_lock(&indio_dev->mlock);
		/* Need both the number of taps and the sampling frequency */
360
		ret = adis_read_reg_16(&st->adis,
361 362 363 364 365 366
						ADIS16400_SENS_AVG,
						&val16);
		if (ret < 0) {
			mutex_unlock(&indio_dev->mlock);
			return ret;
		}
367
		ret = st->variant->get_freq(st);
368 369 370 371 372
		if (ret >= 0) {
			ret /= adis16400_3db_divisors[val16 & 0x07];
			*val = ret / 1000;
			*val2 = (ret % 1000) * 1000;
		}
373 374 375 376
		mutex_unlock(&indio_dev->mlock);
		if (ret < 0)
			return ret;
		return IIO_VAL_INT_PLUS_MICRO;
377 378 379 380 381
	default:
		return -EINVAL;
	}
}

382 383 384 385 386 387 388 389 390
#define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si) { \
	.type = IIO_VOLTAGE, \
	.indexed = 1, \
	.channel = 0, \
	.extend_name = name, \
	.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
		IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
	.address = (addr), \
	.scan_index = (si), \
391 392 393 394 395 396 397
	.scan_type = { \
		.sign = 'u', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
}

#define ADIS16400_SUPPLY_CHAN(addr, bits) \
	ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY)

#define ADIS16400_AUX_ADC_CHAN(addr, bits) \
	ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC)

#define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
	.type = IIO_ANGL_VEL, \
	.modified = 1, \
	.channel2 = IIO_MOD_ ## mod, \
	.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
		IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT | \
		IIO_CHAN_INFO_SCALE_SHARED_BIT | \
		IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT, \
	.address = addr, \
	.scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
416 417 418 419 420 421 422
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
423 424 425 426 427 428 429 430 431 432 433 434
}

#define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
	.type = IIO_ACCEL, \
	.modified = 1, \
	.channel2 = IIO_MOD_ ## mod, \
	.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
		IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT | \
		IIO_CHAN_INFO_SCALE_SHARED_BIT | \
		IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT, \
	.address = (addr), \
	.scan_index = ADIS16400_SCAN_ACC_ ## mod, \
435 436 437 438 439 440 441
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
442 443 444 445 446 447 448 449 450 451 452
}

#define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
	.type = IIO_MAGN, \
	.modified = 1, \
	.channel2 = IIO_MOD_ ## mod, \
	.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
		IIO_CHAN_INFO_SCALE_SHARED_BIT | \
		IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT, \
	.address = (addr), \
	.scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
453 454 455 456 457 458 459
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
}

#define ADIS16400_MOD_TEMP_NAME_X "x"
#define ADIS16400_MOD_TEMP_NAME_Y "y"
#define ADIS16400_MOD_TEMP_NAME_Z "z"

#define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
	.type = IIO_TEMP, \
	.indexed = 1, \
	.channel = 0, \
	.extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
	.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
		IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
		IIO_CHAN_INFO_SCALE_SEPARATE_BIT | \
		IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT, \
	.address = (addr), \
	.scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
477 478 479 480 481 482 483
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
484 485 486 487 488 489 490 491 492 493 494
}

#define ADIS16400_TEMP_CHAN(addr, bits) { \
	.type = IIO_TEMP, \
	.indexed = 1, \
	.channel = 0, \
	.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
		IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
		IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
	.address = (addr), \
	.scan_index = ADIS16350_SCAN_TEMP_X, \
495 496 497 498 499 500 501
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
502 503 504 505 506 507 508 509 510 511
}

#define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
	.type = IIO_INCLI, \
	.modified = 1, \
	.channel2 = IIO_MOD_ ## mod, \
	.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
		IIO_CHAN_INFO_SCALE_SHARED_BIT, \
	.address = (addr), \
	.scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
512 513 514 515 516 517 518
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
519 520
}

521
static const struct iio_chan_spec adis16400_channels[] = {
522 523 524 525 526 527 528 529 530 531 532 533
	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14),
	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
	ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12),
	ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12),
534 535 536
	IIO_CHAN_SOFT_TIMESTAMP(12)
};

537
static const struct iio_chan_spec adis16350_channels[] = {
538 539 540 541 542 543 544 545 546 547 548 549 550 551
	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
	ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
	ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12),
	ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12),
	ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12),
552 553 554
	IIO_CHAN_SOFT_TIMESTAMP(11)
};

555
static const struct iio_chan_spec adis16300_channels[] = {
556 557 558 559 560 561 562 563 564
	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
	ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
	ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
	ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13),
	ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13),
565 566 567
	IIO_CHAN_SOFT_TIMESTAMP(14)
};

568
static const struct iio_chan_spec adis16334_channels[] = {
569 570 571 572 573 574 575 576
	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
	ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
	IIO_CHAN_SOFT_TIMESTAMP(8)
577 578
};

579 580 581 582 583 584 585 586 587
static struct attribute *adis16400_attributes[] = {
	&iio_dev_attr_sampling_frequency.dev_attr.attr,
	NULL
};

static const struct attribute_group adis16400_attribute_group = {
	.attrs = adis16400_attributes,
};

588
static struct adis16400_chip_info adis16400_chips[] = {
589 590 591
	[ADIS16300] = {
		.channels = adis16300_channels,
		.num_channels = ARRAY_SIZE(adis16300_channels),
592
		.flags = ADIS16400_HAS_SLOW_MODE,
593
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
594
		.accel_scale_micro = 5884,
595 596
		.temp_scale_nano = 140000000, /* 0.14 C */
		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
597 598
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
599
	},
600 601 602
	[ADIS16334] = {
		.channels = adis16334_channels,
		.num_channels = ARRAY_SIZE(adis16334_channels),
603
		.flags = ADIS16400_HAS_PROD_ID,
604 605
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
606 607
		.temp_scale_nano = 67850000, /* 0.06785 C */
		.temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
608 609
		.set_freq = adis16334_set_freq,
		.get_freq = adis16334_get_freq,
610
	},
611 612 613
	[ADIS16350] = {
		.channels = adis16350_channels,
		.num_channels = ARRAY_SIZE(adis16350_channels),
614 615
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
		.accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
616 617
		.temp_scale_nano = 145300000, /* 0.1453 C */
		.temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
618 619 620
		.flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
621 622 623 624
	},
	[ADIS16360] = {
		.channels = adis16350_channels,
		.num_channels = ARRAY_SIZE(adis16350_channels),
625
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
626 627
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
628 629
		.temp_scale_nano = 136000000, /* 0.136 C */
		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
630 631
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
632 633 634 635
	},
	[ADIS16362] = {
		.channels = adis16350_channels,
		.num_channels = ARRAY_SIZE(adis16350_channels),
636
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
637 638
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
		.accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
639 640
		.temp_scale_nano = 136000000, /* 0.136 C */
		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
641 642
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
643 644 645 646
	},
	[ADIS16364] = {
		.channels = adis16350_channels,
		.num_channels = ARRAY_SIZE(adis16350_channels),
647
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
648 649
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
650 651
		.temp_scale_nano = 136000000, /* 0.136 C */
		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
652 653
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
654 655 656 657
	},
	[ADIS16400] = {
		.channels = adis16400_channels,
		.num_channels = ARRAY_SIZE(adis16400_channels),
658
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
659 660
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
661 662
		.temp_scale_nano = 140000000, /* 0.14 C */
		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
663 664
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
665 666 667
	}
};

668 669 670 671 672
static const struct iio_info adis16400_info = {
	.driver_module = THIS_MODULE,
	.read_raw = &adis16400_read_raw,
	.write_raw = &adis16400_write_raw,
	.attrs = &adis16400_attribute_group,
673 674 675 676 677 678
	.update_scan_mode = adis16400_update_scan_mode,
};

static const unsigned long adis16400_burst_scan_mask[] = {
	~0UL,
	0,
679
};
680

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
static const char * const adis16400_status_error_msgs[] = {
	[ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
	[ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
	[ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
	[ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
	[ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
	[ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
	[ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
	[ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
	[ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
	[ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
	[ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
	[ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
	[ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
	[ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
	[ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
};

static const struct adis_data adis16400_data = {
	.msc_ctrl_reg = ADIS16400_MSC_CTRL,
	.glob_cmd_reg = ADIS16400_GLOB_CMD,
	.diag_stat_reg = ADIS16400_DIAG_STAT,

	.read_delay = 50,
	.write_delay = 50,

	.self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,
	.startup_delay = ADIS16400_STARTUP_DELAY,

	.status_error_msgs = adis16400_status_error_msgs,
	.status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |
		BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |
		BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |
		BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |
		BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |
		BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |
		BIT(ADIS16400_DIAG_STAT_ALARM2) |
		BIT(ADIS16400_DIAG_STAT_ALARM1) |
		BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |
		BIT(ADIS16400_DIAG_STAT_SELF_TEST) |
		BIT(ADIS16400_DIAG_STAT_OVERFLOW) |
		BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |
		BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |
		BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |
		BIT(ADIS16400_DIAG_STAT_POWER_LOW),
};

728
static int adis16400_probe(struct spi_device *spi)
729
{
730
	struct adis16400_state *st;
731 732 733 734 735 736 737
	struct iio_dev *indio_dev;
	int ret;

	indio_dev = iio_device_alloc(sizeof(*st));
	if (indio_dev == NULL)
		return -ENOMEM;

738
	st = iio_priv(indio_dev);
739
	/* this is only used for removal purposes */
740
	spi_set_drvdata(spi, indio_dev);
741 742

	/* setup the industrialio driver allocated elements */
743
	st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
744 745 746 747
	indio_dev->dev.parent = &spi->dev;
	indio_dev->name = spi_get_device_id(spi)->name;
	indio_dev->channels = st->variant->channels;
	indio_dev->num_channels = st->variant->num_channels;
748
	indio_dev->info = &adis16400_info;
749 750
	indio_dev->modes = INDIO_DIRECT_MODE;

751 752 753
	if (!(st->variant->flags & ADIS16400_NO_BURST))
		indio_dev->available_scan_masks = adis16400_burst_scan_mask;

754 755 756 757
	ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
	if (ret)
		goto error_free_dev;

758 759
	ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
			adis16400_trigger_handler);
760 761 762 763
	if (ret)
		goto error_free_dev;

	/* Get the device into a sane initial state */
764
	ret = adis16400_initial_setup(indio_dev);
765
	if (ret)
766
		goto error_cleanup_buffer;
767 768
	ret = iio_device_register(indio_dev);
	if (ret)
769
		goto error_cleanup_buffer;
770

771 772
	return 0;

773 774
error_cleanup_buffer:
	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
775
error_free_dev:
776
	iio_device_free(indio_dev);
777 778 779
	return ret;
}

780
static int adis16400_remove(struct spi_device *spi)
781
{
782
	struct iio_dev *indio_dev = spi_get_drvdata(spi);
783
	struct adis16400_state *st = iio_priv(indio_dev);
784

785
	iio_device_unregister(indio_dev);
786
	adis16400_stop_device(indio_dev);
787

788
	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
789

790
	iio_device_free(indio_dev);
791

792 793 794
	return 0;
}

795
static const struct spi_device_id adis16400_id[] = {
796
	{"adis16300", ADIS16300},
797
	{"adis16334", ADIS16334},
798 799 800 801 802 803
	{"adis16350", ADIS16350},
	{"adis16354", ADIS16350},
	{"adis16355", ADIS16350},
	{"adis16360", ADIS16360},
	{"adis16362", ADIS16362},
	{"adis16364", ADIS16364},
804
	{"adis16365", ADIS16360},
805 806 807 808
	{"adis16400", ADIS16400},
	{"adis16405", ADIS16400},
	{}
};
809
MODULE_DEVICE_TABLE(spi, adis16400_id);
810

811 812 813 814 815
static struct spi_driver adis16400_driver = {
	.driver = {
		.name = "adis16400",
		.owner = THIS_MODULE,
	},
816
	.id_table = adis16400_id,
817
	.probe = adis16400_probe,
818
	.remove = adis16400_remove,
819
};
820
module_spi_driver(adis16400_driver);
821 822 823 824

MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
MODULE_LICENSE("GPL v2");