blob: beb6919e7180cee7bdfddc84c2ad1897529a5a7a [file] [log] [blame]
Barry Songa9d26f02010-05-04 14:43:15 +01001/*
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>
Jonathan Cameron0f8c9622012-09-02 21:34:59 +01008 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
Michael Hennerich6a6ec622011-03-21 16:44:40 +01009 * Copyright (c) 2011 Analog Devices Inc.
Barry Songa9d26f02010-05-04 14:43:15 +010010 *
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>
Barry Songa9d26f02010-05-04 14:43:15 +010019#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>
Mike Frysinger1cb6c1f2010-05-23 03:10:35 -040024#include <linux/slab.h>
Barry Songa9d26f02010-05-04 14:43:15 +010025#include <linux/sysfs.h>
26#include <linux/list.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040027#include <linux/module.h>
Lars-Peter Clausen1db18bb2013-01-16 12:48:00 +000028#include <linux/debugfs.h>
Rasmus Villemoes19e353f2015-01-23 00:34:02 +010029#include <linux/bitops.h>
Barry Songa9d26f02010-05-04 14:43:15 +010030
Jonathan Cameron06458e22012-04-25 15:54:58 +010031#include <linux/iio/iio.h>
32#include <linux/iio/sysfs.h>
33#include <linux/iio/buffer.h>
Alexandru Ardelean74878d42019-03-22 22:44:38 +020034#include <linux/iio/trigger_consumer.h>
Alexandru Ardelean3cb51612019-03-22 22:44:42 +020035#include <linux/iio/imu/adis.h>
Lars-Peter Clausencd888a172013-01-16 12:48:00 +000036
Alexandru Ardelean3cb51612019-03-22 22:44:42 +020037#define ADIS16400_STARTUP_DELAY 290 /* ms */
38#define ADIS16400_MTEST_DELAY 90 /* ms */
39
40#define ADIS16400_FLASH_CNT 0x00 /* Flash memory write count */
41#define ADIS16400_SUPPLY_OUT 0x02 /* Power supply measurement */
42#define ADIS16400_XGYRO_OUT 0x04 /* X-axis gyroscope output */
43#define ADIS16400_YGYRO_OUT 0x06 /* Y-axis gyroscope output */
44#define ADIS16400_ZGYRO_OUT 0x08 /* Z-axis gyroscope output */
45#define ADIS16400_XACCL_OUT 0x0A /* X-axis accelerometer output */
46#define ADIS16400_YACCL_OUT 0x0C /* Y-axis accelerometer output */
47#define ADIS16400_ZACCL_OUT 0x0E /* Z-axis accelerometer output */
48#define ADIS16400_XMAGN_OUT 0x10 /* X-axis magnetometer measurement */
49#define ADIS16400_YMAGN_OUT 0x12 /* Y-axis magnetometer measurement */
50#define ADIS16400_ZMAGN_OUT 0x14 /* Z-axis magnetometer measurement */
51#define ADIS16400_TEMP_OUT 0x16 /* Temperature output */
52#define ADIS16400_AUX_ADC 0x18 /* Auxiliary ADC measurement */
53
54#define ADIS16350_XTEMP_OUT 0x10 /* X-axis gyroscope temperature measurement */
55#define ADIS16350_YTEMP_OUT 0x12 /* Y-axis gyroscope temperature measurement */
56#define ADIS16350_ZTEMP_OUT 0x14 /* Z-axis gyroscope temperature measurement */
57
58#define ADIS16300_PITCH_OUT 0x12 /* X axis inclinometer output measurement */
59#define ADIS16300_ROLL_OUT 0x14 /* Y axis inclinometer output measurement */
60#define ADIS16300_AUX_ADC 0x16 /* Auxiliary ADC measurement */
61
62#define ADIS16448_BARO_OUT 0x16 /* Barometric pressure output */
63#define ADIS16448_TEMP_OUT 0x18 /* Temperature output */
64
65/* Calibration parameters */
66#define ADIS16400_XGYRO_OFF 0x1A /* X-axis gyroscope bias offset factor */
67#define ADIS16400_YGYRO_OFF 0x1C /* Y-axis gyroscope bias offset factor */
68#define ADIS16400_ZGYRO_OFF 0x1E /* Z-axis gyroscope bias offset factor */
69#define ADIS16400_XACCL_OFF 0x20 /* X-axis acceleration bias offset factor */
70#define ADIS16400_YACCL_OFF 0x22 /* Y-axis acceleration bias offset factor */
71#define ADIS16400_ZACCL_OFF 0x24 /* Z-axis acceleration bias offset factor */
72#define ADIS16400_XMAGN_HIF 0x26 /* X-axis magnetometer, hard-iron factor */
73#define ADIS16400_YMAGN_HIF 0x28 /* Y-axis magnetometer, hard-iron factor */
74#define ADIS16400_ZMAGN_HIF 0x2A /* Z-axis magnetometer, hard-iron factor */
75#define ADIS16400_XMAGN_SIF 0x2C /* X-axis magnetometer, soft-iron factor */
76#define ADIS16400_YMAGN_SIF 0x2E /* Y-axis magnetometer, soft-iron factor */
77#define ADIS16400_ZMAGN_SIF 0x30 /* Z-axis magnetometer, soft-iron factor */
78
79#define ADIS16400_GPIO_CTRL 0x32 /* Auxiliary digital input/output control */
80#define ADIS16400_MSC_CTRL 0x34 /* Miscellaneous control */
81#define ADIS16400_SMPL_PRD 0x36 /* Internal sample period (rate) control */
82#define ADIS16400_SENS_AVG 0x38 /* Dynamic range and digital filter control */
83#define ADIS16400_SLP_CNT 0x3A /* Sleep mode control */
84#define ADIS16400_DIAG_STAT 0x3C /* System status */
85
86/* Alarm functions */
87#define ADIS16400_GLOB_CMD 0x3E /* System command */
88#define ADIS16400_ALM_MAG1 0x40 /* Alarm 1 amplitude threshold */
89#define ADIS16400_ALM_MAG2 0x42 /* Alarm 2 amplitude threshold */
90#define ADIS16400_ALM_SMPL1 0x44 /* Alarm 1 sample size */
91#define ADIS16400_ALM_SMPL2 0x46 /* Alarm 2 sample size */
92#define ADIS16400_ALM_CTRL 0x48 /* Alarm control */
93#define ADIS16400_AUX_DAC 0x4A /* Auxiliary DAC data */
94
95#define ADIS16334_LOT_ID1 0x52 /* Lot identification code 1 */
96#define ADIS16334_LOT_ID2 0x54 /* Lot identification code 2 */
97#define ADIS16400_PRODUCT_ID 0x56 /* Product identifier */
98#define ADIS16334_SERIAL_NUMBER 0x58 /* Serial number, lot specific */
99
100#define ADIS16400_ERROR_ACTIVE (1<<14)
101#define ADIS16400_NEW_DATA (1<<14)
102
103/* MSC_CTRL */
104#define ADIS16400_MSC_CTRL_MEM_TEST (1<<11)
105#define ADIS16400_MSC_CTRL_INT_SELF_TEST (1<<10)
106#define ADIS16400_MSC_CTRL_NEG_SELF_TEST (1<<9)
107#define ADIS16400_MSC_CTRL_POS_SELF_TEST (1<<8)
108#define ADIS16400_MSC_CTRL_GYRO_BIAS (1<<7)
109#define ADIS16400_MSC_CTRL_ACCL_ALIGN (1<<6)
110#define ADIS16400_MSC_CTRL_DATA_RDY_EN (1<<2)
111#define ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH (1<<1)
112#define ADIS16400_MSC_CTRL_DATA_RDY_DIO2 (1<<0)
113
114/* SMPL_PRD */
115#define ADIS16400_SMPL_PRD_TIME_BASE (1<<7)
116#define ADIS16400_SMPL_PRD_DIV_MASK 0x7F
117
118/* DIAG_STAT */
119#define ADIS16400_DIAG_STAT_ZACCL_FAIL 15
120#define ADIS16400_DIAG_STAT_YACCL_FAIL 14
121#define ADIS16400_DIAG_STAT_XACCL_FAIL 13
122#define ADIS16400_DIAG_STAT_XGYRO_FAIL 12
123#define ADIS16400_DIAG_STAT_YGYRO_FAIL 11
124#define ADIS16400_DIAG_STAT_ZGYRO_FAIL 10
125#define ADIS16400_DIAG_STAT_ALARM2 9
126#define ADIS16400_DIAG_STAT_ALARM1 8
127#define ADIS16400_DIAG_STAT_FLASH_CHK 6
128#define ADIS16400_DIAG_STAT_SELF_TEST 5
129#define ADIS16400_DIAG_STAT_OVERFLOW 4
130#define ADIS16400_DIAG_STAT_SPI_FAIL 3
131#define ADIS16400_DIAG_STAT_FLASH_UPT 2
132#define ADIS16400_DIAG_STAT_POWER_HIGH 1
133#define ADIS16400_DIAG_STAT_POWER_LOW 0
134
135/* GLOB_CMD */
136#define ADIS16400_GLOB_CMD_SW_RESET (1<<7)
137#define ADIS16400_GLOB_CMD_P_AUTO_NULL (1<<4)
138#define ADIS16400_GLOB_CMD_FLASH_UPD (1<<3)
139#define ADIS16400_GLOB_CMD_DAC_LATCH (1<<2)
140#define ADIS16400_GLOB_CMD_FAC_CALIB (1<<1)
141#define ADIS16400_GLOB_CMD_AUTO_NULL (1<<0)
142
143/* SLP_CNT */
144#define ADIS16400_SLP_CNT_POWER_OFF (1<<8)
145
146#define ADIS16334_RATE_DIV_SHIFT 8
147#define ADIS16334_RATE_INT_CLK BIT(0)
148
149#define ADIS16400_SPI_SLOW (u32)(300 * 1000)
150#define ADIS16400_SPI_BURST (u32)(1000 * 1000)
151#define ADIS16400_SPI_FAST (u32)(2000 * 1000)
152
153#define ADIS16400_HAS_PROD_ID BIT(0)
154#define ADIS16400_NO_BURST BIT(1)
155#define ADIS16400_HAS_SLOW_MODE BIT(2)
156#define ADIS16400_HAS_SERIAL_NUMBER BIT(3)
157#define ADIS16400_BURST_DIAG_STAT BIT(4)
158
159struct adis16400_state;
160
161struct adis16400_chip_info {
162 const struct iio_chan_spec *channels;
163 const int num_channels;
164 const long flags;
165 unsigned int gyro_scale_micro;
166 unsigned int accel_scale_micro;
167 int temp_scale_nano;
168 int temp_offset;
169 int (*set_freq)(struct adis16400_state *st, unsigned int freq);
170 int (*get_freq)(struct adis16400_state *st);
171};
172
173/**
174 * struct adis16400_state - device instance specific data
175 * @variant: chip variant info
176 * @filt_int: integer part of requested filter frequency
177 * @adis: adis device
178 **/
179struct adis16400_state {
180 struct adis16400_chip_info *variant;
181 int filt_int;
182
183 struct adis adis;
184 unsigned long avail_scan_mask[2];
185};
186
187/* At the moment triggers are only used for ring buffer
188 * filling. This may change!
189 */
190
191enum {
192 ADIS16400_SCAN_SUPPLY,
193 ADIS16400_SCAN_GYRO_X,
194 ADIS16400_SCAN_GYRO_Y,
195 ADIS16400_SCAN_GYRO_Z,
196 ADIS16400_SCAN_ACC_X,
197 ADIS16400_SCAN_ACC_Y,
198 ADIS16400_SCAN_ACC_Z,
199 ADIS16400_SCAN_MAGN_X,
200 ADIS16400_SCAN_MAGN_Y,
201 ADIS16400_SCAN_MAGN_Z,
202 ADIS16400_SCAN_BARO,
203 ADIS16350_SCAN_TEMP_X,
204 ADIS16350_SCAN_TEMP_Y,
205 ADIS16350_SCAN_TEMP_Z,
206 ADIS16300_SCAN_INCLI_X,
207 ADIS16300_SCAN_INCLI_Y,
208 ADIS16400_SCAN_ADC,
209 ADIS16400_SCAN_TIMESTAMP,
210};
Barry Songa9d26f02010-05-04 14:43:15 +0100211
Lars-Peter Clausen1db18bb2013-01-16 12:48:00 +0000212#ifdef CONFIG_DEBUG_FS
213
214static ssize_t adis16400_show_serial_number(struct file *file,
215 char __user *userbuf, size_t count, loff_t *ppos)
216{
217 struct adis16400_state *st = file->private_data;
218 u16 lot1, lot2, serial_number;
219 char buf[16];
220 size_t len;
221 int ret;
222
223 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1);
224 if (ret < 0)
225 return ret;
226
227 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2);
228 if (ret < 0)
229 return ret;
230
231 ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER,
232 &serial_number);
233 if (ret < 0)
234 return ret;
235
236 len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2,
237 serial_number);
238
239 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
240}
241
242static const struct file_operations adis16400_serial_number_fops = {
243 .open = simple_open,
244 .read = adis16400_show_serial_number,
245 .llseek = default_llseek,
246 .owner = THIS_MODULE,
247};
248
249static int adis16400_show_product_id(void *arg, u64 *val)
250{
251 struct adis16400_state *st = arg;
252 uint16_t prod_id;
253 int ret;
254
255 ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id);
256 if (ret < 0)
257 return ret;
258
259 *val = prod_id;
260
261 return 0;
262}
263DEFINE_SIMPLE_ATTRIBUTE(adis16400_product_id_fops,
264 adis16400_show_product_id, NULL, "%lld\n");
265
266static int adis16400_show_flash_count(void *arg, u64 *val)
267{
268 struct adis16400_state *st = arg;
269 uint16_t flash_count;
270 int ret;
271
272 ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count);
273 if (ret < 0)
274 return ret;
275
276 *val = flash_count;
277
278 return 0;
279}
280DEFINE_SIMPLE_ATTRIBUTE(adis16400_flash_count_fops,
281 adis16400_show_flash_count, NULL, "%lld\n");
282
283static int adis16400_debugfs_init(struct iio_dev *indio_dev)
284{
285 struct adis16400_state *st = iio_priv(indio_dev);
286
287 if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER)
288 debugfs_create_file("serial_number", 0400,
289 indio_dev->debugfs_dentry, st,
290 &adis16400_serial_number_fops);
291 if (st->variant->flags & ADIS16400_HAS_PROD_ID)
292 debugfs_create_file("product_id", 0400,
293 indio_dev->debugfs_dentry, st,
294 &adis16400_product_id_fops);
295 debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
296 st, &adis16400_flash_count_fops);
297
298 return 0;
299}
300
301#else
302
303static int adis16400_debugfs_init(struct iio_dev *indio_dev)
304{
305 return 0;
306}
307
308#endif
309
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100310enum adis16400_chip_variant {
Jonathan Cameron8e886e62011-05-18 14:41:30 +0100311 ADIS16300,
Jonathan Cameron85da5052011-09-30 10:05:45 +0100312 ADIS16334,
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100313 ADIS16350,
314 ADIS16360,
315 ADIS16362,
316 ADIS16364,
Lars-Peter Clausendc8615c2015-08-05 15:38:19 +0200317 ADIS16367,
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100318 ADIS16400,
Lars-Peter Clausen72d9c982015-08-05 15:38:20 +0200319 ADIS16445,
Lars-Peter Clausen76ada522013-01-16 12:48:00 +0000320 ADIS16448,
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100321};
322
Alexandru Ardelean5075e072019-03-22 22:44:39 +0200323static struct adis_burst adis16400_burst = {
324 .en = true,
325 .reg_cmd = ADIS16400_GLOB_CMD,
326};
327
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000328static int adis16334_get_freq(struct adis16400_state *st)
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000329{
330 int ret;
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000331 uint16_t t;
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000332
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000333 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000334 if (ret < 0)
335 return ret;
336
337 t >>= ADIS16334_RATE_DIV_SHIFT;
338
Lars-Peter Clausenb24150e2013-01-16 12:48:00 +0000339 return 819200 >> t;
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000340}
341
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000342static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000343{
344 unsigned int t;
345
Lars-Peter Clausenb24150e2013-01-16 12:48:00 +0000346 if (freq < 819200)
347 t = ilog2(819200 / freq);
Lars-Peter Clausen06220b82013-01-16 12:48:00 +0000348 else
349 t = 0;
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000350
351 if (t > 0x31)
352 t = 0x31;
353
354 t <<= ADIS16334_RATE_DIV_SHIFT;
355 t |= ADIS16334_RATE_INT_CLK;
356
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000357 return adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000358}
359
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000360static int adis16400_get_freq(struct adis16400_state *st)
Jonathan Cameron98c93732011-11-27 11:39:13 +0000361{
Jonathan Cameron98c93732011-11-27 11:39:13 +0000362 int sps, ret;
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000363 uint16_t t;
Jonathan Cameron98c93732011-11-27 11:39:13 +0000364
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000365 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
Jonathan Cameron98c93732011-11-27 11:39:13 +0000366 if (ret < 0)
367 return ret;
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000368
Lars-Peter Clausenb24150e2013-01-16 12:48:00 +0000369 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
Jonathan Cameron98c93732011-11-27 11:39:13 +0000370 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
371
372 return sps;
373}
374
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000375static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000376{
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000377 unsigned int t;
Lars-Peter Clausen7ba8a042013-01-16 12:48:00 +0000378 uint8_t val = 0;
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000379
Lars-Peter Clausenb24150e2013-01-16 12:48:00 +0000380 t = 1638404 / freq;
Lars-Peter Clausen7ba8a042013-01-16 12:48:00 +0000381 if (t >= 128) {
382 val |= ADIS16400_SMPL_PRD_TIME_BASE;
383 t = 52851 / freq;
384 if (t >= 128)
385 t = 127;
386 } else if (t != 0) {
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000387 t--;
Lars-Peter Clausen7ba8a042013-01-16 12:48:00 +0000388 }
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000389
Lars-Peter Clausen7ba8a042013-01-16 12:48:00 +0000390 val |= t;
391
392 if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000393 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000394 else
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000395 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000396
Lars-Peter Clausen7ba8a042013-01-16 12:48:00 +0000397 return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000398}
399
Reno Farnesiaab498b2017-07-01 17:54:08 -0400400static const unsigned int adis16400_3db_divisors[] = {
Jonathan Cameron98c93732011-11-27 11:39:13 +0000401 [0] = 2, /* Special case */
Lars-Peter Clausenbdb20bd2013-01-16 12:48:00 +0000402 [1] = 6,
403 [2] = 12,
404 [3] = 25,
405 [4] = 50,
406 [5] = 100,
407 [6] = 200,
408 [7] = 200, /* Not a valid setting */
Jonathan Cameron98c93732011-11-27 11:39:13 +0000409};
410
411static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
412{
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000413 struct adis16400_state *st = iio_priv(indio_dev);
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000414 uint16_t val16;
Jonathan Cameron98c93732011-11-27 11:39:13 +0000415 int i, ret;
Jonathan Cameron98c93732011-11-27 11:39:13 +0000416
Lars-Peter Clausenbdb20bd2013-01-16 12:48:00 +0000417 for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
418 if (sps / adis16400_3db_divisors[i] >= val)
419 break;
Jonathan Cameron98c93732011-11-27 11:39:13 +0000420 }
Lars-Peter Clausenbdb20bd2013-01-16 12:48:00 +0000421
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000422 ret = adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
Lars-Peter Clausenbdb20bd2013-01-16 12:48:00 +0000423 if (ret < 0)
424 return ret;
425
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000426 ret = adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
Lars-Peter Clausenbdb20bd2013-01-16 12:48:00 +0000427 (val16 & ~0x07) | i);
Jonathan Cameron98c93732011-11-27 11:39:13 +0000428 return ret;
429}
430
Barry Songa9d26f02010-05-04 14:43:15 +0100431/* Power down the device */
Jonathan Camerone7854842011-05-18 14:41:27 +0100432static int adis16400_stop_device(struct iio_dev *indio_dev)
Barry Songa9d26f02010-05-04 14:43:15 +0100433{
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000434 struct adis16400_state *st = iio_priv(indio_dev);
Barry Songa9d26f02010-05-04 14:43:15 +0100435 int ret;
Barry Songa9d26f02010-05-04 14:43:15 +0100436
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000437 ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
438 ADIS16400_SLP_CNT_POWER_OFF);
Barry Songa9d26f02010-05-04 14:43:15 +0100439 if (ret)
Jonathan Camerone7854842011-05-18 14:41:27 +0100440 dev_err(&indio_dev->dev,
441 "problem with turning device off: SLP_CNT");
Barry Songa9d26f02010-05-04 14:43:15 +0100442
443 return ret;
444}
445
Jonathan Cameron38d15f02011-05-18 14:42:23 +0100446static int adis16400_initial_setup(struct iio_dev *indio_dev)
Barry Songa9d26f02010-05-04 14:43:15 +0100447{
Jonathan Cameron38d15f02011-05-18 14:42:23 +0100448 struct adis16400_state *st = iio_priv(indio_dev);
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000449 uint16_t prod_id, smp_prd;
450 unsigned int device_id;
451 int ret;
Barry Songa9d26f02010-05-04 14:43:15 +0100452
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000453 /* use low spi speed for init if the device has a slow mode */
454 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000455 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000456 else
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000457 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
458 st->adis.spi->mode = SPI_MODE_3;
459 spi_setup(st->adis.spi);
Barry Songa9d26f02010-05-04 14:43:15 +0100460
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000461 ret = adis_initial_startup(&st->adis);
462 if (ret)
463 return ret;
Barry Songa9d26f02010-05-04 14:43:15 +0100464
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100465 if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000466 ret = adis_read_reg_16(&st->adis,
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100467 ADIS16400_PRODUCT_ID, &prod_id);
468 if (ret)
469 goto err_ret;
Barry Songa9d26f02010-05-04 14:43:15 +0100470
Ioana Ciornei72a868b2015-11-01 14:58:45 +0200471 ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
472 if (ret != 1) {
473 ret = -EINVAL;
474 goto err_ret;
475 }
Lars-Peter Clausenfc8850c2012-10-30 10:26:00 +0000476
477 if (prod_id != device_id)
478 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
479 device_id, prod_id);
Barry Songa9d26f02010-05-04 14:43:15 +0100480
Michael Hennerich69d80ba2011-09-02 17:25:34 +0100481 dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000482 indio_dev->name, prod_id,
483 st->adis.spi->chip_select, st->adis.spi->irq);
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100484 }
Barry Songa9d26f02010-05-04 14:43:15 +0100485 /* use high spi speed if possible */
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000486 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000487 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000488 if (ret)
489 goto err_ret;
490
491 if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000492 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
493 spi_setup(st->adis.spi);
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000494 }
Barry Songa9d26f02010-05-04 14:43:15 +0100495 }
496
Barry Songa9d26f02010-05-04 14:43:15 +0100497err_ret:
Barry Songa9d26f02010-05-04 14:43:15 +0100498 return ret;
499}
500
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000501static const uint8_t adis16400_addresses[] = {
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000502 [ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
503 [ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
504 [ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
505 [ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
506 [ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
507 [ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
Jonathan Camerone7854842011-05-18 14:41:27 +0100508};
509
Jonathan Camerone7854842011-05-18 14:41:27 +0100510static int adis16400_write_raw(struct iio_dev *indio_dev,
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000511 struct iio_chan_spec const *chan, int val, int val2, long info)
Jonathan Camerone7854842011-05-18 14:41:27 +0100512{
Jonathan Cameron98c93732011-11-27 11:39:13 +0000513 struct adis16400_state *st = iio_priv(indio_dev);
514 int ret, sps;
Jonathan Cameron521de512011-09-02 17:25:33 +0100515
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000516 switch (info) {
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100517 case IIO_CHAN_INFO_CALIBBIAS:
Jonathan Camerone7854842011-05-18 14:41:27 +0100518 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000519 ret = adis_write_reg_16(&st->adis,
520 adis16400_addresses[chan->scan_index], val);
Jonathan Camerone7854842011-05-18 14:41:27 +0100521 mutex_unlock(&indio_dev->mlock);
522 return ret;
Jonathan Cameron98c93732011-11-27 11:39:13 +0000523 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000524 /*
525 * Need to cache values so we can update if the frequency
526 * changes.
527 */
Jonathan Cameron98c93732011-11-27 11:39:13 +0000528 mutex_lock(&indio_dev->mlock);
529 st->filt_int = val;
530 /* Work out update to current value */
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000531 sps = st->variant->get_freq(st);
Jonathan Cameron98c93732011-11-27 11:39:13 +0000532 if (sps < 0) {
533 mutex_unlock(&indio_dev->mlock);
534 return sps;
535 }
536
Lars-Peter Clausenb24150e2013-01-16 12:48:00 +0000537 ret = adis16400_set_filter(indio_dev, sps,
538 val * 1000 + val2 / 1000);
Jonathan Cameron98c93732011-11-27 11:39:13 +0000539 mutex_unlock(&indio_dev->mlock);
540 return ret;
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100541 case IIO_CHAN_INFO_SAMP_FREQ:
542 sps = val * 1000 + val2 / 1000;
543
544 if (sps <= 0)
545 return -EINVAL;
546
547 mutex_lock(&indio_dev->mlock);
548 ret = st->variant->set_freq(st, sps);
549 mutex_unlock(&indio_dev->mlock);
550 return ret;
Jonathan Camerone7854842011-05-18 14:41:27 +0100551 default:
552 return -EINVAL;
553 }
554}
555
556static int adis16400_read_raw(struct iio_dev *indio_dev,
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000557 struct iio_chan_spec const *chan, int *val, int *val2, long info)
Jonathan Camerone7854842011-05-18 14:41:27 +0100558{
Jonathan Cameron38d15f02011-05-18 14:42:23 +0100559 struct adis16400_state *st = iio_priv(indio_dev);
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000560 int16_t val16;
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000561 int ret;
Jonathan Camerone7854842011-05-18 14:41:27 +0100562
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +0000563 switch (info) {
Jonathan Camerona5d016d2012-04-15 17:41:21 +0100564 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000565 return adis_single_conversion(indio_dev, chan, 0, val);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100566 case IIO_CHAN_INFO_SCALE:
Jonathan Camerone7854842011-05-18 14:41:27 +0100567 switch (chan->type) {
Jonathan Cameron41ea0402011-10-05 15:27:59 +0100568 case IIO_ANGL_VEL:
Jonathan Camerone7854842011-05-18 14:41:27 +0100569 *val = 0;
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100570 *val2 = st->variant->gyro_scale_micro;
Jonathan Camerone7854842011-05-18 14:41:27 +0100571 return IIO_VAL_INT_PLUS_MICRO;
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100572 case IIO_VOLTAGE:
Jonathan Camerone7854842011-05-18 14:41:27 +0100573 *val = 0;
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +0100574 if (chan->channel == 0) {
575 *val = 2;
576 *val2 = 418000; /* 2.418 mV */
577 } else {
578 *val = 0;
579 *val2 = 805800; /* 805.8 uV */
580 }
Jonathan Camerone7854842011-05-18 14:41:27 +0100581 return IIO_VAL_INT_PLUS_MICRO;
582 case IIO_ACCEL:
583 *val = 0;
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100584 *val2 = st->variant->accel_scale_micro;
Jonathan Camerone7854842011-05-18 14:41:27 +0100585 return IIO_VAL_INT_PLUS_MICRO;
586 case IIO_MAGN:
587 *val = 0;
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +0100588 *val2 = 500; /* 0.5 mgauss */
Jonathan Camerone7854842011-05-18 14:41:27 +0100589 return IIO_VAL_INT_PLUS_MICRO;
590 case IIO_TEMP:
Lars-Peter Clausen760ebc02012-10-15 10:35:00 +0100591 *val = st->variant->temp_scale_nano / 1000000;
592 *val2 = (st->variant->temp_scale_nano % 1000000);
Jonathan Camerone7854842011-05-18 14:41:27 +0100593 return IIO_VAL_INT_PLUS_MICRO;
Lars-Peter Clausen69ca2d72015-05-15 17:18:34 +0200594 case IIO_PRESSURE:
595 /* 20 uBar = 0.002kPascal */
596 *val = 0;
597 *val2 = 2000;
598 return IIO_VAL_INT_PLUS_MICRO;
Jonathan Camerone7854842011-05-18 14:41:27 +0100599 default:
600 return -EINVAL;
601 }
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100602 case IIO_CHAN_INFO_CALIBBIAS:
Jonathan Camerone7854842011-05-18 14:41:27 +0100603 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000604 ret = adis_read_reg_16(&st->adis,
605 adis16400_addresses[chan->scan_index], &val16);
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100606 mutex_unlock(&indio_dev->mlock);
607 if (ret)
Jonathan Camerone7854842011-05-18 14:41:27 +0100608 return ret;
Rasmus Villemoes19e353f2015-01-23 00:34:02 +0100609 val16 = sign_extend32(val16, 11);
Jonathan Camerone7854842011-05-18 14:41:27 +0100610 *val = val16;
611 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100612 case IIO_CHAN_INFO_OFFSET:
Jonathan Camerone7854842011-05-18 14:41:27 +0100613 /* currently only temperature */
Lars-Peter Clausen760ebc02012-10-15 10:35:00 +0100614 *val = st->variant->temp_offset;
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +0100615 return IIO_VAL_INT;
Jonathan Cameron98c93732011-11-27 11:39:13 +0000616 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
617 mutex_lock(&indio_dev->mlock);
618 /* Need both the number of taps and the sampling frequency */
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000619 ret = adis_read_reg_16(&st->adis,
Jonathan Cameron98c93732011-11-27 11:39:13 +0000620 ADIS16400_SENS_AVG,
621 &val16);
622 if (ret < 0) {
623 mutex_unlock(&indio_dev->mlock);
624 return ret;
625 }
Lars-Peter Clausencd888a172013-01-16 12:48:00 +0000626 ret = st->variant->get_freq(st);
Lars-Peter Clausenb24150e2013-01-16 12:48:00 +0000627 if (ret >= 0) {
628 ret /= adis16400_3db_divisors[val16 & 0x07];
629 *val = ret / 1000;
630 *val2 = (ret % 1000) * 1000;
631 }
Jonathan Cameron98c93732011-11-27 11:39:13 +0000632 mutex_unlock(&indio_dev->mlock);
633 if (ret < 0)
634 return ret;
635 return IIO_VAL_INT_PLUS_MICRO;
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100636 case IIO_CHAN_INFO_SAMP_FREQ:
637 ret = st->variant->get_freq(st);
638 if (ret < 0)
639 return ret;
640 *val = ret / 1000;
641 *val2 = (ret % 1000) * 1000;
642 return IIO_VAL_INT_PLUS_MICRO;
Jonathan Camerone7854842011-05-18 14:41:27 +0100643 default:
644 return -EINVAL;
645 }
646}
647
Alexandru Ardelean74878d42019-03-22 22:44:38 +0200648#if IS_ENABLED(CONFIG_IIO_BUFFER)
649static irqreturn_t adis16400_trigger_handler(int irq, void *p)
650{
651 struct iio_poll_func *pf = p;
652 struct iio_dev *indio_dev = pf->indio_dev;
653 struct adis16400_state *st = iio_priv(indio_dev);
654 struct adis *adis = &st->adis;
655 u32 old_speed_hz = st->adis.spi->max_speed_hz;
656 void *buffer;
657 int ret;
658
659 if (!adis->buffer)
660 return -ENOMEM;
661
662 if (!(st->variant->flags & ADIS16400_NO_BURST) &&
663 st->adis.spi->max_speed_hz > ADIS16400_SPI_BURST) {
664 st->adis.spi->max_speed_hz = ADIS16400_SPI_BURST;
665 spi_setup(st->adis.spi);
666 }
667
668 ret = spi_sync(adis->spi, &adis->msg);
669 if (ret)
670 dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret);
671
672 if (!(st->variant->flags & ADIS16400_NO_BURST)) {
673 st->adis.spi->max_speed_hz = old_speed_hz;
674 spi_setup(st->adis.spi);
675 }
676
677 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
678 buffer = adis->buffer + sizeof(u16);
679 else
680 buffer = adis->buffer;
681
682 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
683 pf->timestamp);
684
685 iio_trigger_notify_done(indio_dev->trig);
686
687 return IRQ_HANDLED;
688}
689#else
690#define adis16400_trigger_handler NULL
691#endif /* IS_ENABLED(CONFIG_IIO_BUFFER) */
692
Paul Cercueil7323d592015-05-15 17:18:35 +0200693#define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000694 .type = IIO_VOLTAGE, \
695 .indexed = 1, \
Paul Cercueil7323d592015-05-15 17:18:35 +0200696 .channel = chn, \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000697 .extend_name = name, \
Jonathan Cameron19a7c882013-02-27 19:31:17 +0000698 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
699 BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100700 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000701 .address = (addr), \
702 .scan_index = (si), \
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +0000703 .scan_type = { \
704 .sign = 'u', \
705 .realbits = (bits), \
706 .storagebits = 16, \
707 .shift = 0, \
708 .endianness = IIO_BE, \
709 }, \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000710}
711
712#define ADIS16400_SUPPLY_CHAN(addr, bits) \
Paul Cercueil7323d592015-05-15 17:18:35 +0200713 ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY, 0)
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000714
715#define ADIS16400_AUX_ADC_CHAN(addr, bits) \
Paul Cercueil7323d592015-05-15 17:18:35 +0200716 ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC, 1)
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000717
718#define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
719 .type = IIO_ANGL_VEL, \
720 .modified = 1, \
721 .channel2 = IIO_MOD_ ## mod, \
Jonathan Cameron19a7c882013-02-27 19:31:17 +0000722 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
723 BIT(IIO_CHAN_INFO_CALIBBIAS), \
724 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
725 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100726 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000727 .address = addr, \
728 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +0000729 .scan_type = { \
730 .sign = 's', \
731 .realbits = (bits), \
732 .storagebits = 16, \
733 .shift = 0, \
734 .endianness = IIO_BE, \
735 }, \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000736}
737
738#define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
739 .type = IIO_ACCEL, \
740 .modified = 1, \
741 .channel2 = IIO_MOD_ ## mod, \
Jonathan Cameron19a7c882013-02-27 19:31:17 +0000742 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
743 BIT(IIO_CHAN_INFO_CALIBBIAS), \
744 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
745 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100746 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000747 .address = (addr), \
748 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +0000749 .scan_type = { \
750 .sign = 's', \
751 .realbits = (bits), \
752 .storagebits = 16, \
753 .shift = 0, \
754 .endianness = IIO_BE, \
755 }, \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000756}
757
758#define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
759 .type = IIO_MAGN, \
760 .modified = 1, \
761 .channel2 = IIO_MOD_ ## mod, \
Jonathan Cameron19a7c882013-02-27 19:31:17 +0000762 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
763 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
764 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100765 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000766 .address = (addr), \
767 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +0000768 .scan_type = { \
769 .sign = 's', \
770 .realbits = (bits), \
771 .storagebits = 16, \
772 .shift = 0, \
773 .endianness = IIO_BE, \
774 }, \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000775}
776
777#define ADIS16400_MOD_TEMP_NAME_X "x"
778#define ADIS16400_MOD_TEMP_NAME_Y "y"
779#define ADIS16400_MOD_TEMP_NAME_Z "z"
780
781#define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
782 .type = IIO_TEMP, \
783 .indexed = 1, \
784 .channel = 0, \
785 .extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
Jonathan Cameron19a7c882013-02-27 19:31:17 +0000786 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
787 BIT(IIO_CHAN_INFO_OFFSET) | \
788 BIT(IIO_CHAN_INFO_SCALE), \
789 .info_mask_shared_by_type = \
790 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100791 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000792 .address = (addr), \
793 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +0000794 .scan_type = { \
795 .sign = 's', \
796 .realbits = (bits), \
797 .storagebits = 16, \
798 .shift = 0, \
799 .endianness = IIO_BE, \
800 }, \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000801}
802
803#define ADIS16400_TEMP_CHAN(addr, bits) { \
804 .type = IIO_TEMP, \
805 .indexed = 1, \
806 .channel = 0, \
Jonathan Cameron19a7c882013-02-27 19:31:17 +0000807 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
808 BIT(IIO_CHAN_INFO_OFFSET) | \
809 BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100810 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000811 .address = (addr), \
812 .scan_index = ADIS16350_SCAN_TEMP_X, \
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +0000813 .scan_type = { \
814 .sign = 's', \
815 .realbits = (bits), \
816 .storagebits = 16, \
817 .shift = 0, \
818 .endianness = IIO_BE, \
819 }, \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000820}
821
822#define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
823 .type = IIO_INCLI, \
824 .modified = 1, \
825 .channel2 = IIO_MOD_ ## mod, \
Jonathan Cameron19a7c882013-02-27 19:31:17 +0000826 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
827 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100828 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000829 .address = (addr), \
830 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +0000831 .scan_type = { \
832 .sign = 's', \
833 .realbits = (bits), \
834 .storagebits = 16, \
835 .shift = 0, \
836 .endianness = IIO_BE, \
837 }, \
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000838}
839
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100840static const struct iio_chan_spec adis16400_channels[] = {
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000841 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14),
842 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
843 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
844 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
845 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
846 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
847 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
848 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
849 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
850 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
851 ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12),
852 ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12),
Marcus Folkessonc76782d2014-01-24 11:24:00 +0000853 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
Jonathan Camerone7854842011-05-18 14:41:27 +0100854};
855
Lars-Peter Clausen72d9c982015-08-05 15:38:20 +0200856static const struct iio_chan_spec adis16445_channels[] = {
857 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
858 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
859 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
860 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
861 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
862 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
863 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
864 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
865};
866
Lars-Peter Clausen76ada522013-01-16 12:48:00 +0000867static const struct iio_chan_spec adis16448_channels[] = {
868 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
869 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
870 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
871 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
872 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
873 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
874 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16),
875 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16),
876 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16),
877 {
878 .type = IIO_PRESSURE,
Jonathan Cameron19a7c882013-02-27 19:31:17 +0000879 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
880 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
Jonathan Camerona07a97d2014-06-22 20:59:00 +0100881 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Lars-Peter Clausen76ada522013-01-16 12:48:00 +0000882 .address = ADIS16448_BARO_OUT,
883 .scan_index = ADIS16400_SCAN_BARO,
Jonathan Cameron3425c0f2013-12-11 18:45:00 +0000884 .scan_type = {
885 .sign = 's',
886 .realbits = 16,
887 .storagebits = 16,
888 .endianness = IIO_BE,
889 },
Lars-Peter Clausen76ada522013-01-16 12:48:00 +0000890 },
891 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
Marcus Folkessonc76782d2014-01-24 11:24:00 +0000892 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
Lars-Peter Clausen76ada522013-01-16 12:48:00 +0000893};
894
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100895static const struct iio_chan_spec adis16350_channels[] = {
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000896 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
897 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
898 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
899 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
900 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
901 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
902 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
903 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
904 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
905 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
906 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
907 ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12),
908 ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12),
909 ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12),
Marcus Folkessonc76782d2014-01-24 11:24:00 +0000910 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100911};
912
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100913static const struct iio_chan_spec adis16300_channels[] = {
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000914 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
915 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
916 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
917 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
918 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
919 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
920 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
921 ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13),
922 ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13),
Marcus Folkessonc76782d2014-01-24 11:24:00 +0000923 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
Jonathan Cameron8e886e62011-05-18 14:41:30 +0100924};
925
Jonathan Cameron85da5052011-09-30 10:05:45 +0100926static const struct iio_chan_spec adis16334_channels[] = {
Lars-Peter Clausen599acfb2013-01-16 12:48:00 +0000927 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
928 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
929 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
930 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
931 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
932 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
933 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
Marcus Folkessonc76782d2014-01-24 11:24:00 +0000934 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
Jonathan Cameron88b42f32011-08-12 18:04:37 +0100935};
936
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100937static struct adis16400_chip_info adis16400_chips[] = {
Jonathan Cameron8e886e62011-05-18 14:41:30 +0100938 [ADIS16300] = {
939 .channels = adis16300_channels,
940 .num_channels = ARRAY_SIZE(adis16300_channels),
Lars-Peter Clausenbd95a892015-08-05 15:38:16 +0200941 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
942 ADIS16400_HAS_SERIAL_NUMBER,
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +0100943 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
Jonathan Cameron8e886e62011-05-18 14:41:30 +0100944 .accel_scale_micro = 5884,
Lars-Peter Clausen760ebc02012-10-15 10:35:00 +0100945 .temp_scale_nano = 140000000, /* 0.14 C */
946 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000947 .set_freq = adis16400_set_freq,
948 .get_freq = adis16400_get_freq,
Jonathan Cameron8e886e62011-05-18 14:41:30 +0100949 },
Jonathan Cameron85da5052011-09-30 10:05:45 +0100950 [ADIS16334] = {
951 .channels = adis16334_channels,
952 .num_channels = ARRAY_SIZE(adis16334_channels),
Lars-Peter Clausen1db18bb2013-01-16 12:48:00 +0000953 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
954 ADIS16400_HAS_SERIAL_NUMBER,
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +0100955 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
956 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
Lars-Peter Clausen760ebc02012-10-15 10:35:00 +0100957 .temp_scale_nano = 67850000, /* 0.06785 C */
958 .temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000959 .set_freq = adis16334_set_freq,
960 .get_freq = adis16334_get_freq,
Jonathan Cameron88b42f32011-08-12 18:04:37 +0100961 },
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100962 [ADIS16350] = {
963 .channels = adis16350_channels,
964 .num_channels = ARRAY_SIZE(adis16350_channels),
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +0100965 .gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
966 .accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
Lars-Peter Clausen760ebc02012-10-15 10:35:00 +0100967 .temp_scale_nano = 145300000, /* 0.1453 C */
968 .temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000969 .flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
970 .set_freq = adis16400_set_freq,
971 .get_freq = adis16400_get_freq,
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100972 },
973 [ADIS16360] = {
974 .channels = adis16350_channels,
975 .num_channels = ARRAY_SIZE(adis16350_channels),
Lars-Peter Clausen1db18bb2013-01-16 12:48:00 +0000976 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
977 ADIS16400_HAS_SERIAL_NUMBER,
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +0100978 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
979 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
Lars-Peter Clausen760ebc02012-10-15 10:35:00 +0100980 .temp_scale_nano = 136000000, /* 0.136 C */
981 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000982 .set_freq = adis16400_set_freq,
983 .get_freq = adis16400_get_freq,
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100984 },
985 [ADIS16362] = {
986 .channels = adis16350_channels,
987 .num_channels = ARRAY_SIZE(adis16350_channels),
Lars-Peter Clausen1db18bb2013-01-16 12:48:00 +0000988 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
989 ADIS16400_HAS_SERIAL_NUMBER,
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +0100990 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
991 .accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
Lars-Peter Clausen760ebc02012-10-15 10:35:00 +0100992 .temp_scale_nano = 136000000, /* 0.136 C */
993 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +0000994 .set_freq = adis16400_set_freq,
995 .get_freq = adis16400_get_freq,
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100996 },
997 [ADIS16364] = {
998 .channels = adis16350_channels,
999 .num_channels = ARRAY_SIZE(adis16350_channels),
Lars-Peter Clausen1db18bb2013-01-16 12:48:00 +00001000 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1001 ADIS16400_HAS_SERIAL_NUMBER,
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +01001002 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1003 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
Lars-Peter Clausen760ebc02012-10-15 10:35:00 +01001004 .temp_scale_nano = 136000000, /* 0.136 C */
1005 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +00001006 .set_freq = adis16400_set_freq,
1007 .get_freq = adis16400_get_freq,
Jonathan Cameron2a29a902011-05-18 14:41:28 +01001008 },
Lars-Peter Clausendc8615c2015-08-05 15:38:19 +02001009 [ADIS16367] = {
1010 .channels = adis16350_channels,
1011 .num_channels = ARRAY_SIZE(adis16350_channels),
1012 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1013 ADIS16400_HAS_SERIAL_NUMBER,
1014 .gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */
1015 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1016 .temp_scale_nano = 136000000, /* 0.136 C */
1017 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1018 .set_freq = adis16400_set_freq,
1019 .get_freq = adis16400_get_freq,
1020 },
Jonathan Cameron2a29a902011-05-18 14:41:28 +01001021 [ADIS16400] = {
1022 .channels = adis16400_channels,
1023 .num_channels = ARRAY_SIZE(adis16400_channels),
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +00001024 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
Lars-Peter Clausen1cf8c972012-10-15 10:35:00 +01001025 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1026 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
Lars-Peter Clausen760ebc02012-10-15 10:35:00 +01001027 .temp_scale_nano = 140000000, /* 0.14 C */
1028 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
Lars-Peter Clausenea2ccb32012-10-30 10:26:00 +00001029 .set_freq = adis16400_set_freq,
1030 .get_freq = adis16400_get_freq,
Lars-Peter Clausen76ada522013-01-16 12:48:00 +00001031 },
Lars-Peter Clausen72d9c982015-08-05 15:38:20 +02001032 [ADIS16445] = {
1033 .channels = adis16445_channels,
1034 .num_channels = ARRAY_SIZE(adis16445_channels),
1035 .flags = ADIS16400_HAS_PROD_ID |
1036 ADIS16400_HAS_SERIAL_NUMBER |
1037 ADIS16400_BURST_DIAG_STAT,
1038 .gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
1039 .accel_scale_micro = IIO_G_TO_M_S_2(250), /* 1/4000 g */
1040 .temp_scale_nano = 73860000, /* 0.07386 C */
1041 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1042 .set_freq = adis16334_set_freq,
1043 .get_freq = adis16334_get_freq,
1044 },
Lars-Peter Clausen76ada522013-01-16 12:48:00 +00001045 [ADIS16448] = {
1046 .channels = adis16448_channels,
1047 .num_channels = ARRAY_SIZE(adis16448_channels),
1048 .flags = ADIS16400_HAS_PROD_ID |
Lars-Peter Clausend046ba22015-05-15 17:18:38 +02001049 ADIS16400_HAS_SERIAL_NUMBER |
1050 ADIS16400_BURST_DIAG_STAT,
Lars-Peter Clausen81665372015-08-05 15:38:13 +02001051 .gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */
Lars-Peter Clausen76ada522013-01-16 12:48:00 +00001052 .accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
1053 .temp_scale_nano = 73860000, /* 0.07386 C */
1054 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1055 .set_freq = adis16334_set_freq,
1056 .get_freq = adis16334_get_freq,
Jonathan Cameron2a29a902011-05-18 14:41:28 +01001057 }
1058};
1059
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001060static const struct iio_info adis16400_info = {
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001061 .read_raw = &adis16400_read_raw,
1062 .write_raw = &adis16400_write_raw,
Alexandru Ardelean0e92e2d2019-03-22 22:44:40 +02001063 .update_scan_mode = adis_update_scan_mode,
Lars-Peter Clausen1db18bb2013-01-16 12:48:00 +00001064 .debugfs_reg_access = adis_debugfs_reg_access,
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +00001065};
1066
Lars-Peter Clausencd888a172013-01-16 12:48:00 +00001067static const char * const adis16400_status_error_msgs[] = {
1068 [ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
1069 [ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
1070 [ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
1071 [ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
1072 [ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
1073 [ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
1074 [ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
1075 [ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
1076 [ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
1077 [ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
1078 [ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
1079 [ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
1080 [ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
1081 [ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
1082 [ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
1083};
1084
1085static const struct adis_data adis16400_data = {
1086 .msc_ctrl_reg = ADIS16400_MSC_CTRL,
1087 .glob_cmd_reg = ADIS16400_GLOB_CMD,
1088 .diag_stat_reg = ADIS16400_DIAG_STAT,
1089
1090 .read_delay = 50,
1091 .write_delay = 50,
1092
1093 .self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,
1094 .startup_delay = ADIS16400_STARTUP_DELAY,
1095
1096 .status_error_msgs = adis16400_status_error_msgs,
1097 .status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |
1098 BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |
1099 BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |
1100 BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |
1101 BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |
1102 BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |
1103 BIT(ADIS16400_DIAG_STAT_ALARM2) |
1104 BIT(ADIS16400_DIAG_STAT_ALARM1) |
1105 BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |
1106 BIT(ADIS16400_DIAG_STAT_SELF_TEST) |
1107 BIT(ADIS16400_DIAG_STAT_OVERFLOW) |
1108 BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |
1109 BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |
1110 BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |
1111 BIT(ADIS16400_DIAG_STAT_POWER_LOW),
1112};
1113
Paul Cercueilc2a8b622015-05-15 17:18:36 +02001114static void adis16400_setup_chan_mask(struct adis16400_state *st)
1115{
1116 const struct adis16400_chip_info *chip_info = st->variant;
Reno Farnesiaab498b2017-07-01 17:54:08 -04001117 unsigned int i;
Paul Cercueilc2a8b622015-05-15 17:18:36 +02001118
1119 for (i = 0; i < chip_info->num_channels; i++) {
1120 const struct iio_chan_spec *ch = &chip_info->channels[i];
1121
1122 if (ch->scan_index >= 0 &&
1123 ch->scan_index != ADIS16400_SCAN_TIMESTAMP)
1124 st->avail_scan_mask[0] |= BIT(ch->scan_index);
1125 }
1126}
1127
Bill Pemberton4ae1c612012-11-19 13:21:57 -05001128static int adis16400_probe(struct spi_device *spi)
Barry Songa9d26f02010-05-04 14:43:15 +01001129{
Jonathan Cameron38d15f02011-05-18 14:42:23 +01001130 struct adis16400_state *st;
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +00001131 struct iio_dev *indio_dev;
1132 int ret;
1133
Sachin Kamat5b778b92013-07-30 09:44:00 +01001134 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +00001135 if (indio_dev == NULL)
1136 return -ENOMEM;
1137
Jonathan Cameron38d15f02011-05-18 14:42:23 +01001138 st = iio_priv(indio_dev);
Barry Songa9d26f02010-05-04 14:43:15 +01001139 /* this is only used for removal purposes */
Jonathan Cameron38d15f02011-05-18 14:42:23 +01001140 spi_set_drvdata(spi, indio_dev);
Barry Songa9d26f02010-05-04 14:43:15 +01001141
Jonathan Cameron38d15f02011-05-18 14:42:23 +01001142 /* setup the industrialio driver allocated elements */
1143 st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
1144 indio_dev->dev.parent = &spi->dev;
1145 indio_dev->name = spi_get_device_id(spi)->name;
Jonathan Cameron38d15f02011-05-18 14:42:23 +01001146 indio_dev->channels = st->variant->channels;
1147 indio_dev->num_channels = st->variant->num_channels;
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001148 indio_dev->info = &adis16400_info;
Jonathan Cameron38d15f02011-05-18 14:42:23 +01001149 indio_dev->modes = INDIO_DIRECT_MODE;
1150
Paul Cercueilc2a8b622015-05-15 17:18:36 +02001151 if (!(st->variant->flags & ADIS16400_NO_BURST)) {
1152 adis16400_setup_chan_mask(st);
1153 indio_dev->available_scan_masks = st->avail_scan_mask;
Alexandru Ardelean5075e072019-03-22 22:44:39 +02001154 st->adis.burst = &adis16400_burst;
1155 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
1156 st->adis.burst->extra_len = sizeof(u16);
Paul Cercueilc2a8b622015-05-15 17:18:36 +02001157 }
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +00001158
Lars-Peter Clausencd888a172013-01-16 12:48:00 +00001159 ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
1160 if (ret)
Sachin Kamat5b778b92013-07-30 09:44:00 +01001161 return ret;
Lars-Peter Clausencd888a172013-01-16 12:48:00 +00001162
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +00001163 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
1164 adis16400_trigger_handler);
Barry Songa9d26f02010-05-04 14:43:15 +01001165 if (ret)
Sachin Kamat5b778b92013-07-30 09:44:00 +01001166 return ret;
Barry Songa9d26f02010-05-04 14:43:15 +01001167
Barry Songa9d26f02010-05-04 14:43:15 +01001168 /* Get the device into a sane initial state */
Jonathan Cameron38d15f02011-05-18 14:42:23 +01001169 ret = adis16400_initial_setup(indio_dev);
Barry Songa9d26f02010-05-04 14:43:15 +01001170 if (ret)
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +00001171 goto error_cleanup_buffer;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001172 ret = iio_device_register(indio_dev);
1173 if (ret)
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +00001174 goto error_cleanup_buffer;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001175
Lars-Peter Clausen1db18bb2013-01-16 12:48:00 +00001176 adis16400_debugfs_init(indio_dev);
Barry Songa9d26f02010-05-04 14:43:15 +01001177 return 0;
1178
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +00001179error_cleanup_buffer:
1180 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
Barry Songa9d26f02010-05-04 14:43:15 +01001181 return ret;
1182}
1183
Bill Pemberton447d4f22012-11-19 13:26:37 -05001184static int adis16400_remove(struct spi_device *spi)
Barry Songa9d26f02010-05-04 14:43:15 +01001185{
Lars-Peter Clausenf4c6d642013-01-16 12:48:00 +00001186 struct iio_dev *indio_dev = spi_get_drvdata(spi);
Lars-Peter Clausencd888a172013-01-16 12:48:00 +00001187 struct adis16400_state *st = iio_priv(indio_dev);
Barry Songa9d26f02010-05-04 14:43:15 +01001188
Jonathan Camerond2fffd62011-10-14 14:46:58 +01001189 iio_device_unregister(indio_dev);
Lars-Peter Clausen0b4ac3d2012-09-22 09:56:00 +01001190 adis16400_stop_device(indio_dev);
Barry Songa9d26f02010-05-04 14:43:15 +01001191
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +00001192 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
Lars-Peter Clausencd888a172013-01-16 12:48:00 +00001193
Barry Songa9d26f02010-05-04 14:43:15 +01001194 return 0;
Barry Songa9d26f02010-05-04 14:43:15 +01001195}
1196
Jonathan Cameron2a29a902011-05-18 14:41:28 +01001197static const struct spi_device_id adis16400_id[] = {
Jonathan Cameron8e886e62011-05-18 14:41:30 +01001198 {"adis16300", ADIS16300},
Lars-Peter Clausen3c386762015-08-05 15:38:17 +02001199 {"adis16305", ADIS16300},
Jonathan Cameron85da5052011-09-30 10:05:45 +01001200 {"adis16334", ADIS16334},
Jonathan Cameron2a29a902011-05-18 14:41:28 +01001201 {"adis16350", ADIS16350},
1202 {"adis16354", ADIS16350},
1203 {"adis16355", ADIS16350},
1204 {"adis16360", ADIS16360},
1205 {"adis16362", ADIS16362},
1206 {"adis16364", ADIS16364},
Lars-Peter Clausena7462e62012-10-30 10:26:00 +00001207 {"adis16365", ADIS16360},
Lars-Peter Clausendc8615c2015-08-05 15:38:19 +02001208 {"adis16367", ADIS16367},
Jonathan Cameron2a29a902011-05-18 14:41:28 +01001209 {"adis16400", ADIS16400},
1210 {"adis16405", ADIS16400},
Lars-Peter Clausen72d9c982015-08-05 15:38:20 +02001211 {"adis16445", ADIS16445},
Lars-Peter Clausen76ada522013-01-16 12:48:00 +00001212 {"adis16448", ADIS16448},
Jonathan Cameron2a29a902011-05-18 14:41:28 +01001213 {}
1214};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +01001215MODULE_DEVICE_TABLE(spi, adis16400_id);
Jonathan Cameron2a29a902011-05-18 14:41:28 +01001216
Barry Songa9d26f02010-05-04 14:43:15 +01001217static struct spi_driver adis16400_driver = {
1218 .driver = {
1219 .name = "adis16400",
Barry Songa9d26f02010-05-04 14:43:15 +01001220 },
Jonathan Cameron2a29a902011-05-18 14:41:28 +01001221 .id_table = adis16400_id,
Barry Songa9d26f02010-05-04 14:43:15 +01001222 .probe = adis16400_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -05001223 .remove = adis16400_remove,
Barry Songa9d26f02010-05-04 14:43:15 +01001224};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +01001225module_spi_driver(adis16400_driver);
Barry Songa9d26f02010-05-04 14:43:15 +01001226
1227MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
1228MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
1229MODULE_LICENSE("GPL v2");