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