adis16400_core.c 27.5 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
#include <linux/debugfs.h>
29
#include <linux/bitops.h>
30

31 32 33
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/buffer.h>
34

35 36
#include "adis16400.h"

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#ifdef CONFIG_DEBUG_FS

static ssize_t adis16400_show_serial_number(struct file *file,
		char __user *userbuf, size_t count, loff_t *ppos)
{
	struct adis16400_state *st = file->private_data;
	u16 lot1, lot2, serial_number;
	char buf[16];
	size_t len;
	int ret;

	ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1);
	if (ret < 0)
		return ret;

	ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2);
	if (ret < 0)
		return ret;

	ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER,
			&serial_number);
	if (ret < 0)
		return ret;

	len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2,
			serial_number);

	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
}

static const struct file_operations adis16400_serial_number_fops = {
	.open = simple_open,
	.read = adis16400_show_serial_number,
	.llseek = default_llseek,
	.owner = THIS_MODULE,
};

static int adis16400_show_product_id(void *arg, u64 *val)
{
	struct adis16400_state *st = arg;
	uint16_t prod_id;
	int ret;

	ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id);
	if (ret < 0)
		return ret;

	*val = prod_id;

	return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(adis16400_product_id_fops,
	adis16400_show_product_id, NULL, "%lld\n");

static int adis16400_show_flash_count(void *arg, u64 *val)
{
	struct adis16400_state *st = arg;
	uint16_t flash_count;
	int ret;

	ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count);
	if (ret < 0)
		return ret;

	*val = flash_count;

	return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(adis16400_flash_count_fops,
	adis16400_show_flash_count, NULL, "%lld\n");

static int adis16400_debugfs_init(struct iio_dev *indio_dev)
{
	struct adis16400_state *st = iio_priv(indio_dev);

	if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER)
		debugfs_create_file("serial_number", 0400,
			indio_dev->debugfs_dentry, st,
			&adis16400_serial_number_fops);
	if (st->variant->flags & ADIS16400_HAS_PROD_ID)
		debugfs_create_file("product_id", 0400,
			indio_dev->debugfs_dentry, st,
			&adis16400_product_id_fops);
	debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
		st, &adis16400_flash_count_fops);

	return 0;
}

#else

static int adis16400_debugfs_init(struct iio_dev *indio_dev)
{
	return 0;
}

#endif

135
enum adis16400_chip_variant {
136
	ADIS16300,
137
	ADIS16334,
138 139 140 141
	ADIS16350,
	ADIS16360,
	ADIS16362,
	ADIS16364,
142
	ADIS16367,
143
	ADIS16400,
144
	ADIS16448,
145 146
};

147
static int adis16334_get_freq(struct adis16400_state *st)
148
{
149
	int ret;
150
	uint16_t t;
151

152
	ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
153 154 155 156 157
	if (ret < 0)
		return ret;

	t >>= ADIS16334_RATE_DIV_SHIFT;

158
	return 819200 >> t;
159 160
}

161
static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
162 163 164
{
	unsigned int t;

165 166
	if (freq < 819200)
		t = ilog2(819200 / freq);
167 168
	else
		t = 0;
169 170 171 172 173 174 175

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

	t <<= ADIS16334_RATE_DIV_SHIFT;
	t |= ADIS16334_RATE_INT_CLK;

176
	return adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
177 178
}

179
static int adis16400_get_freq(struct adis16400_state *st)
180
{
181
	int sps, ret;
182
	uint16_t t;
183

184
	ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
185 186
	if (ret < 0)
		return ret;
187

188
	sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
189 190 191 192 193
	sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;

	return sps;
}

194
static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
195 196
{
	unsigned int t;
197
	uint8_t val = 0;
198

199
	t = 1638404 / freq;
200 201 202 203 204 205
	if (t >= 128) {
		val |= ADIS16400_SMPL_PRD_TIME_BASE;
		t = 52851 / freq;
		if (t >= 128)
			t = 127;
	} else if (t != 0) {
206
		t--;
207
	}
208

209 210 211
	val |= t;

	if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
212
		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
213
	else
214
		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
215

216
	return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
217 218
}

219 220
static const unsigned adis16400_3db_divisors[] = {
	[0] = 2, /* Special case */
221 222 223 224 225 226 227
	[1] = 6,
	[2] = 12,
	[3] = 25,
	[4] = 50,
	[5] = 100,
	[6] = 200,
	[7] = 200, /* Not a valid setting */
228 229 230 231
};

static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
{
232
	struct adis16400_state *st = iio_priv(indio_dev);
233
	uint16_t val16;
234
	int i, ret;
235 236 237

	for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
		if (sps / adis16400_3db_divisors[i] >= val)
238
			break;
239 240
	}

241
	ret = adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
242 243
	if (ret < 0)
		return ret;
244

245
	ret = adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
246
					 (val16 & ~0x07) | i);
247 248 249
	return ret;
}

250
/* Power down the device */
251
static int adis16400_stop_device(struct iio_dev *indio_dev)
252
{
253
	struct adis16400_state *st = iio_priv(indio_dev);
254 255
	int ret;

256 257
	ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
			ADIS16400_SLP_CNT_POWER_OFF);
258
	if (ret)
259 260
		dev_err(&indio_dev->dev,
			"problem with turning device off: SLP_CNT");
261 262 263 264

	return ret;
}

265
static int adis16400_initial_setup(struct iio_dev *indio_dev)
266
{
267
	struct adis16400_state *st = iio_priv(indio_dev);
268 269 270
	uint16_t prod_id, smp_prd;
	unsigned int device_id;
	int ret;
271

272 273
	/* use low spi speed for init if the device has a slow mode */
	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
274
		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
275
	else
276 277 278
		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
	st->adis.spi->mode = SPI_MODE_3;
	spi_setup(st->adis.spi);
279

280 281 282
	ret = adis_initial_startup(&st->adis);
	if (ret)
		return ret;
283

284
	if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
285
		ret = adis_read_reg_16(&st->adis,
286 287 288
						ADIS16400_PRODUCT_ID, &prod_id);
		if (ret)
			goto err_ret;
289

290 291 292 293 294
		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);
295

296
		dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
297 298
			indio_dev->name, prod_id,
			st->adis.spi->chip_select, st->adis.spi->irq);
299
	}
300
	/* use high spi speed if possible */
301
	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
302
		ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
303 304 305 306
		if (ret)
			goto err_ret;

		if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
307 308
			st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
			spi_setup(st->adis.spi);
309
		}
310 311 312 313 314 315
	}

err_ret:
	return ret;
}

316
static const uint8_t adis16400_addresses[] = {
317 318 319 320 321 322
	[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,
323 324 325
};

static int adis16400_write_raw(struct iio_dev *indio_dev,
326
	struct iio_chan_spec const *chan, int val, int val2, long info)
327
{
328 329
	struct adis16400_state *st = iio_priv(indio_dev);
	int ret, sps;
330

331
	switch (info) {
332
	case IIO_CHAN_INFO_CALIBBIAS:
333
		mutex_lock(&indio_dev->mlock);
334 335
		ret = adis_write_reg_16(&st->adis,
				adis16400_addresses[chan->scan_index], val);
336 337
		mutex_unlock(&indio_dev->mlock);
		return ret;
338
	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
339 340 341 342
		/*
		 * Need to cache values so we can update if the frequency
		 * changes.
		 */
343 344 345
		mutex_lock(&indio_dev->mlock);
		st->filt_int = val;
		/* Work out update to current value */
346
		sps = st->variant->get_freq(st);
347 348 349 350 351
		if (sps < 0) {
			mutex_unlock(&indio_dev->mlock);
			return sps;
		}

352 353
		ret = adis16400_set_filter(indio_dev, sps,
			val * 1000 + val2 / 1000);
354 355
		mutex_unlock(&indio_dev->mlock);
		return ret;
356 357 358 359 360 361 362 363 364 365
	case IIO_CHAN_INFO_SAMP_FREQ:
		sps = val * 1000 + val2 / 1000;

		if (sps <= 0)
			return -EINVAL;

		mutex_lock(&indio_dev->mlock);
		ret = st->variant->set_freq(st, sps);
		mutex_unlock(&indio_dev->mlock);
		return ret;
366 367 368 369 370 371
	default:
		return -EINVAL;
	}
}

static int adis16400_read_raw(struct iio_dev *indio_dev,
372
	struct iio_chan_spec const *chan, int *val, int *val2, long info)
373
{
374
	struct adis16400_state *st = iio_priv(indio_dev);
375
	int16_t val16;
376
	int ret;
377

378
	switch (info) {
379
	case IIO_CHAN_INFO_RAW:
380
		return adis_single_conversion(indio_dev, chan, 0, val);
381
	case IIO_CHAN_INFO_SCALE:
382
		switch (chan->type) {
383
		case IIO_ANGL_VEL:
384
			*val = 0;
385
			*val2 = st->variant->gyro_scale_micro;
386
			return IIO_VAL_INT_PLUS_MICRO;
387
		case IIO_VOLTAGE:
388
			*val = 0;
389 390 391 392 393 394 395
			if (chan->channel == 0) {
				*val = 2;
				*val2 = 418000; /* 2.418 mV */
			} else {
				*val = 0;
				*val2 = 805800; /* 805.8 uV */
			}
396 397 398
			return IIO_VAL_INT_PLUS_MICRO;
		case IIO_ACCEL:
			*val = 0;
399
			*val2 = st->variant->accel_scale_micro;
400 401 402
			return IIO_VAL_INT_PLUS_MICRO;
		case IIO_MAGN:
			*val = 0;
403
			*val2 = 500; /* 0.5 mgauss */
404 405
			return IIO_VAL_INT_PLUS_MICRO;
		case IIO_TEMP:
406 407
			*val = st->variant->temp_scale_nano / 1000000;
			*val2 = (st->variant->temp_scale_nano % 1000000);
408
			return IIO_VAL_INT_PLUS_MICRO;
409 410 411 412 413
		case IIO_PRESSURE:
			/* 20 uBar = 0.002kPascal */
			*val = 0;
			*val2 = 2000;
			return IIO_VAL_INT_PLUS_MICRO;
414 415 416
		default:
			return -EINVAL;
		}
417
	case IIO_CHAN_INFO_CALIBBIAS:
418
		mutex_lock(&indio_dev->mlock);
419 420
		ret = adis_read_reg_16(&st->adis,
				adis16400_addresses[chan->scan_index], &val16);
421 422
		mutex_unlock(&indio_dev->mlock);
		if (ret)
423
			return ret;
424
		val16 = sign_extend32(val16, 11);
425 426
		*val = val16;
		return IIO_VAL_INT;
427
	case IIO_CHAN_INFO_OFFSET:
428
		/* currently only temperature */
429
		*val = st->variant->temp_offset;
430
		return IIO_VAL_INT;
431 432 433
	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
		mutex_lock(&indio_dev->mlock);
		/* Need both the number of taps and the sampling frequency */
434
		ret = adis_read_reg_16(&st->adis,
435 436 437 438 439 440
						ADIS16400_SENS_AVG,
						&val16);
		if (ret < 0) {
			mutex_unlock(&indio_dev->mlock);
			return ret;
		}
441
		ret = st->variant->get_freq(st);
442 443 444 445 446
		if (ret >= 0) {
			ret /= adis16400_3db_divisors[val16 & 0x07];
			*val = ret / 1000;
			*val2 = (ret % 1000) * 1000;
		}
447 448 449 450
		mutex_unlock(&indio_dev->mlock);
		if (ret < 0)
			return ret;
		return IIO_VAL_INT_PLUS_MICRO;
451 452 453 454 455 456 457
	case IIO_CHAN_INFO_SAMP_FREQ:
		ret = st->variant->get_freq(st);
		if (ret < 0)
			return ret;
		*val = ret / 1000;
		*val2 = (ret % 1000) * 1000;
		return IIO_VAL_INT_PLUS_MICRO;
458 459 460 461 462
	default:
		return -EINVAL;
	}
}

463
#define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \
464 465
	.type = IIO_VOLTAGE, \
	.indexed = 1, \
466
	.channel = chn, \
467
	.extend_name = name, \
468 469
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
		BIT(IIO_CHAN_INFO_SCALE), \
470
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
471 472
	.address = (addr), \
	.scan_index = (si), \
473 474 475 476 477 478 479
	.scan_type = { \
		.sign = 'u', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
480 481 482
}

#define ADIS16400_SUPPLY_CHAN(addr, bits) \
483
	ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY, 0)
484 485

#define ADIS16400_AUX_ADC_CHAN(addr, bits) \
486
	ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC, 1)
487 488 489 490 491

#define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
	.type = IIO_ANGL_VEL, \
	.modified = 1, \
	.channel2 = IIO_MOD_ ## mod, \
492 493 494 495
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
		BIT(IIO_CHAN_INFO_CALIBBIAS),		  \
	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
496
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
497 498
	.address = addr, \
	.scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
499 500 501 502 503 504 505
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
506 507 508 509 510 511
}

#define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
	.type = IIO_ACCEL, \
	.modified = 1, \
	.channel2 = IIO_MOD_ ## mod, \
512 513 514 515
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
		BIT(IIO_CHAN_INFO_CALIBBIAS), \
	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
516
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
517 518
	.address = (addr), \
	.scan_index = ADIS16400_SCAN_ACC_ ## mod, \
519 520 521 522 523 524 525
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
526 527 528 529 530 531
}

#define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
	.type = IIO_MAGN, \
	.modified = 1, \
	.channel2 = IIO_MOD_ ## mod, \
532 533 534
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
535
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
536 537
	.address = (addr), \
	.scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
538 539 540 541 542 543 544
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
545 546 547 548 549 550 551 552 553 554 555
}

#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, \
556 557 558 559 560
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
		BIT(IIO_CHAN_INFO_OFFSET) | \
		BIT(IIO_CHAN_INFO_SCALE), \
	.info_mask_shared_by_type = \
		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
561
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
562 563
	.address = (addr), \
	.scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
564 565 566 567 568 569 570
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
571 572 573 574 575 576
}

#define ADIS16400_TEMP_CHAN(addr, bits) { \
	.type = IIO_TEMP, \
	.indexed = 1, \
	.channel = 0, \
577 578 579
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
		BIT(IIO_CHAN_INFO_OFFSET) | \
		BIT(IIO_CHAN_INFO_SCALE), \
580
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
581 582
	.address = (addr), \
	.scan_index = ADIS16350_SCAN_TEMP_X, \
583 584 585 586 587 588 589
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
590 591 592 593 594 595
}

#define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
	.type = IIO_INCLI, \
	.modified = 1, \
	.channel2 = IIO_MOD_ ## mod, \
596 597
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
598
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
599 600
	.address = (addr), \
	.scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
601 602 603 604 605 606 607
	.scan_type = { \
		.sign = 's', \
		.realbits = (bits), \
		.storagebits = 16, \
		.shift = 0, \
		.endianness = IIO_BE, \
	}, \
608 609
}

610
static const struct iio_chan_spec adis16400_channels[] = {
611 612 613 614 615 616 617 618 619 620 621 622
	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),
623
	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
624 625
};

626 627 628 629 630 631 632 633 634 635 636 637
static const struct iio_chan_spec adis16448_channels[] = {
	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16),
	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16),
	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16),
	{
		.type = IIO_PRESSURE,
638 639
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
640
		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
641 642
		.address = ADIS16448_BARO_OUT,
		.scan_index = ADIS16400_SCAN_BARO,
643 644 645 646 647 648
		.scan_type = {
			.sign = 's',
			.realbits = 16,
			.storagebits = 16,
			.endianness = IIO_BE,
		},
649 650
	},
	ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
651
	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
652 653
};

654
static const struct iio_chan_spec adis16350_channels[] = {
655 656 657 658 659 660 661 662 663 664 665 666 667 668
	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),
669
	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
670 671
};

672
static const struct iio_chan_spec adis16300_channels[] = {
673 674 675 676 677 678 679 680 681
	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),
682
	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
683 684
};

685
static const struct iio_chan_spec adis16334_channels[] = {
686 687 688 689 690 691 692
	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),
693
	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
694 695
};

696
static struct adis16400_chip_info adis16400_chips[] = {
697 698 699
	[ADIS16300] = {
		.channels = adis16300_channels,
		.num_channels = ARRAY_SIZE(adis16300_channels),
700 701
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
				ADIS16400_HAS_SERIAL_NUMBER,
702
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
703
		.accel_scale_micro = 5884,
704 705
		.temp_scale_nano = 140000000, /* 0.14 C */
		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
706 707
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
708
	},
709 710 711
	[ADIS16334] = {
		.channels = adis16334_channels,
		.num_channels = ARRAY_SIZE(adis16334_channels),
712 713
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
				ADIS16400_HAS_SERIAL_NUMBER,
714 715
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
716 717
		.temp_scale_nano = 67850000, /* 0.06785 C */
		.temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
718 719
		.set_freq = adis16334_set_freq,
		.get_freq = adis16334_get_freq,
720
	},
721 722 723
	[ADIS16350] = {
		.channels = adis16350_channels,
		.num_channels = ARRAY_SIZE(adis16350_channels),
724 725
		.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 */
726 727
		.temp_scale_nano = 145300000, /* 0.1453 C */
		.temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
728 729 730
		.flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
731 732 733 734
	},
	[ADIS16360] = {
		.channels = adis16350_channels,
		.num_channels = ARRAY_SIZE(adis16350_channels),
735 736
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
				ADIS16400_HAS_SERIAL_NUMBER,
737 738
		.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 */
739 740
		.temp_scale_nano = 136000000, /* 0.136 C */
		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
741 742
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
743 744 745 746
	},
	[ADIS16362] = {
		.channels = adis16350_channels,
		.num_channels = ARRAY_SIZE(adis16350_channels),
747 748
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
				ADIS16400_HAS_SERIAL_NUMBER,
749 750
		.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 */
751 752
		.temp_scale_nano = 136000000, /* 0.136 C */
		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
753 754
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
755 756 757 758
	},
	[ADIS16364] = {
		.channels = adis16350_channels,
		.num_channels = ARRAY_SIZE(adis16350_channels),
759 760
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
				ADIS16400_HAS_SERIAL_NUMBER,
761 762
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
763 764
		.temp_scale_nano = 136000000, /* 0.136 C */
		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
765 766
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
767
	},
768 769 770 771 772 773 774 775 776 777 778 779
	[ADIS16367] = {
		.channels = adis16350_channels,
		.num_channels = ARRAY_SIZE(adis16350_channels),
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
				ADIS16400_HAS_SERIAL_NUMBER,
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */
		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
		.temp_scale_nano = 136000000, /* 0.136 C */
		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
	},
780 781 782
	[ADIS16400] = {
		.channels = adis16400_channels,
		.num_channels = ARRAY_SIZE(adis16400_channels),
783
		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
784 785
		.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 */
786 787
		.temp_scale_nano = 140000000, /* 0.14 C */
		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
788 789
		.set_freq = adis16400_set_freq,
		.get_freq = adis16400_get_freq,
790 791 792 793 794
	},
	[ADIS16448] = {
		.channels = adis16448_channels,
		.num_channels = ARRAY_SIZE(adis16448_channels),
		.flags = ADIS16400_HAS_PROD_ID |
795 796
				ADIS16400_HAS_SERIAL_NUMBER |
				ADIS16400_BURST_DIAG_STAT,
797
		.gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */
798 799 800 801 802
		.accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
		.temp_scale_nano = 73860000, /* 0.07386 C */
		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
		.set_freq = adis16334_set_freq,
		.get_freq = adis16334_get_freq,
803 804 805
	}
};

806 807 808 809
static const struct iio_info adis16400_info = {
	.driver_module = THIS_MODULE,
	.read_raw = &adis16400_read_raw,
	.write_raw = &adis16400_write_raw,
810
	.update_scan_mode = adis16400_update_scan_mode,
811
	.debugfs_reg_access = adis_debugfs_reg_access,
812 813
};

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
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),
};

861 862 863 864 865 866 867 868 869 870 871 872 873 874
static void adis16400_setup_chan_mask(struct adis16400_state *st)
{
	const struct adis16400_chip_info *chip_info = st->variant;
	unsigned i;

	for (i = 0; i < chip_info->num_channels; i++) {
		const struct iio_chan_spec *ch = &chip_info->channels[i];

		if (ch->scan_index >= 0 &&
		    ch->scan_index != ADIS16400_SCAN_TIMESTAMP)
			st->avail_scan_mask[0] |= BIT(ch->scan_index);
	}
}

875
static int adis16400_probe(struct spi_device *spi)
876
{
877
	struct adis16400_state *st;
878 879 880
	struct iio_dev *indio_dev;
	int ret;

881
	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
882 883 884
	if (indio_dev == NULL)
		return -ENOMEM;

885
	st = iio_priv(indio_dev);
886
	/* this is only used for removal purposes */
887
	spi_set_drvdata(spi, indio_dev);
888 889

	/* setup the industrialio driver allocated elements */
890
	st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
891 892 893 894
	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;
895
	indio_dev->info = &adis16400_info;
896 897
	indio_dev->modes = INDIO_DIRECT_MODE;

898 899 900 901
	if (!(st->variant->flags & ADIS16400_NO_BURST)) {
		adis16400_setup_chan_mask(st);
		indio_dev->available_scan_masks = st->avail_scan_mask;
	}
902

903 904
	ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
	if (ret)
905
		return ret;
906

907 908
	ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
			adis16400_trigger_handler);
909
	if (ret)
910
		return ret;
911 912

	/* Get the device into a sane initial state */
913
	ret = adis16400_initial_setup(indio_dev);
914
	if (ret)
915
		goto error_cleanup_buffer;
916 917
	ret = iio_device_register(indio_dev);
	if (ret)
918
		goto error_cleanup_buffer;
919

920
	adis16400_debugfs_init(indio_dev);
921 922
	return 0;

923 924
error_cleanup_buffer:
	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
925 926 927
	return ret;
}

928
static int adis16400_remove(struct spi_device *spi)
929
{
930
	struct iio_dev *indio_dev = spi_get_drvdata(spi);
931
	struct adis16400_state *st = iio_priv(indio_dev);
932

933
	iio_device_unregister(indio_dev);
934
	adis16400_stop_device(indio_dev);
935

936
	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
937

938 939 940
	return 0;
}

941
static const struct spi_device_id adis16400_id[] = {
942
	{"adis16300", ADIS16300},
943
	{"adis16305", ADIS16300},
944
	{"adis16334", ADIS16334},
945 946 947 948 949 950
	{"adis16350", ADIS16350},
	{"adis16354", ADIS16350},
	{"adis16355", ADIS16350},
	{"adis16360", ADIS16360},
	{"adis16362", ADIS16362},
	{"adis16364", ADIS16364},
951
	{"adis16365", ADIS16360},
952
	{"adis16367", ADIS16367},
953 954
	{"adis16400", ADIS16400},
	{"adis16405", ADIS16400},
955
	{"adis16448", ADIS16448},
956 957
	{}
};
958
MODULE_DEVICE_TABLE(spi, adis16400_id);
959

960 961 962 963 964
static struct spi_driver adis16400_driver = {
	.driver = {
		.name = "adis16400",
		.owner = THIS_MODULE,
	},
965
	.id_table = adis16400_id,
966
	.probe = adis16400_probe,
967
	.remove = adis16400_remove,
968
};
969
module_spi_driver(adis16400_driver);
970 971 972 973

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