blob: b76d8482bbd5c5de71f322ffa9c5d7862ccb5c3b [file] [log] [blame]
Nuno Sáfff73522020-04-13 10:24:44 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * ADIS16475 IMU driver
4 *
5 * Copyright 2019 Analog Devices Inc.
6 */
7#include <linux/bitfield.h>
8#include <linux/bitops.h>
9#include <linux/clk.h>
10#include <linux/debugfs.h>
11#include <linux/delay.h>
12#include <linux/device.h>
13#include <linux/kernel.h>
14#include <linux/iio/buffer.h>
15#include <linux/iio/iio.h>
16#include <linux/iio/imu/adis.h>
Nuno Sáfff73522020-04-13 10:24:44 +020017#include <linux/iio/trigger_consumer.h>
18#include <linux/irq.h>
Nuno Sa39c024b2021-02-18 12:40:37 +010019#include <linux/lcm.h>
20#include <linux/math.h>
Nuno Sáfff73522020-04-13 10:24:44 +020021#include <linux/module.h>
22#include <linux/mod_devicetable.h>
23#include <linux/property.h>
24#include <linux/spi/spi.h>
25
26#define ADIS16475_REG_DIAG_STAT 0x02
27#define ADIS16475_REG_X_GYRO_L 0x04
28#define ADIS16475_REG_Y_GYRO_L 0x08
29#define ADIS16475_REG_Z_GYRO_L 0x0C
30#define ADIS16475_REG_X_ACCEL_L 0x10
31#define ADIS16475_REG_Y_ACCEL_L 0x14
32#define ADIS16475_REG_Z_ACCEL_L 0x18
33#define ADIS16475_REG_TEMP_OUT 0x1c
34#define ADIS16475_REG_X_GYRO_BIAS_L 0x40
35#define ADIS16475_REG_Y_GYRO_BIAS_L 0x44
36#define ADIS16475_REG_Z_GYRO_BIAS_L 0x48
37#define ADIS16475_REG_X_ACCEL_BIAS_L 0x4c
38#define ADIS16475_REG_Y_ACCEL_BIAS_L 0x50
39#define ADIS16475_REG_Z_ACCEL_BIAS_L 0x54
40#define ADIS16475_REG_FILT_CTRL 0x5c
41#define ADIS16475_FILT_CTRL_MASK GENMASK(2, 0)
42#define ADIS16475_FILT_CTRL(x) FIELD_PREP(ADIS16475_FILT_CTRL_MASK, x)
43#define ADIS16475_REG_MSG_CTRL 0x60
44#define ADIS16475_MSG_CTRL_DR_POL_MASK BIT(0)
45#define ADIS16475_MSG_CTRL_DR_POL(x) \
46 FIELD_PREP(ADIS16475_MSG_CTRL_DR_POL_MASK, x)
47#define ADIS16475_SYNC_MODE_MASK GENMASK(4, 2)
48#define ADIS16475_SYNC_MODE(x) FIELD_PREP(ADIS16475_SYNC_MODE_MASK, x)
49#define ADIS16475_REG_UP_SCALE 0x62
50#define ADIS16475_REG_DEC_RATE 0x64
51#define ADIS16475_REG_GLOB_CMD 0x68
52#define ADIS16475_REG_FIRM_REV 0x6c
53#define ADIS16475_REG_FIRM_DM 0x6e
54#define ADIS16475_REG_FIRM_Y 0x70
55#define ADIS16475_REG_PROD_ID 0x72
56#define ADIS16475_REG_SERIAL_NUM 0x74
57#define ADIS16475_REG_FLASH_CNT 0x7c
58#define ADIS16500_BURST32_MASK BIT(9)
59#define ADIS16500_BURST32(x) FIELD_PREP(ADIS16500_BURST32_MASK, x)
60/* number of data elements in burst mode */
61#define ADIS16475_BURST32_MAX_DATA 32
62#define ADIS16475_BURST_MAX_DATA 20
63#define ADIS16475_MAX_SCAN_DATA 20
64/* spi max speed in brust mode */
65#define ADIS16475_BURST_MAX_SPEED 1000000
66#define ADIS16475_LSB_DEC_MASK BIT(0)
67#define ADIS16475_LSB_FIR_MASK BIT(1)
68
69enum {
70 ADIS16475_SYNC_DIRECT = 1,
71 ADIS16475_SYNC_SCALED,
72 ADIS16475_SYNC_OUTPUT,
73 ADIS16475_SYNC_PULSE = 5,
74};
75
76struct adis16475_sync {
77 u16 sync_mode;
78 u16 min_rate;
79 u16 max_rate;
80};
81
82struct adis16475_chip_info {
83 const struct iio_chan_spec *channels;
84 const struct adis16475_sync *sync;
85 const struct adis_data adis_data;
86 const char *name;
87 u32 num_channels;
88 u32 gyro_max_val;
89 u32 gyro_max_scale;
90 u32 accel_max_val;
91 u32 accel_max_scale;
92 u32 temp_scale;
93 u32 int_clk;
94 u16 max_dec;
95 u8 num_sync;
96 bool has_burst32;
97};
98
99struct adis16475 {
100 const struct adis16475_chip_info *info;
101 struct adis adis;
102 u32 clk_freq;
103 bool burst32;
104 unsigned long lsb_flag;
Nuno Sa39c024b2021-02-18 12:40:37 +0100105 u16 sync_mode;
Nuno Sáfff73522020-04-13 10:24:44 +0200106 /* Alignment needed for the timestamp */
107 __be16 data[ADIS16475_MAX_SCAN_DATA] __aligned(8);
108};
109
110enum {
111 ADIS16475_SCAN_GYRO_X,
112 ADIS16475_SCAN_GYRO_Y,
113 ADIS16475_SCAN_GYRO_Z,
114 ADIS16475_SCAN_ACCEL_X,
115 ADIS16475_SCAN_ACCEL_Y,
116 ADIS16475_SCAN_ACCEL_Z,
117 ADIS16475_SCAN_TEMP,
118 ADIS16475_SCAN_DIAG_S_FLAGS,
119 ADIS16475_SCAN_CRC_FAILURE,
120};
121
Nuno Sa39c024b2021-02-18 12:40:37 +0100122static bool low_rate_allow;
123module_param(low_rate_allow, bool, 0444);
124MODULE_PARM_DESC(low_rate_allow,
125 "Allow IMU rates below the minimum advisable when external clk is used in SCALED mode (default: N)");
126
Nuno Sáfff73522020-04-13 10:24:44 +0200127#ifdef CONFIG_DEBUG_FS
128static ssize_t adis16475_show_firmware_revision(struct file *file,
129 char __user *userbuf,
130 size_t count, loff_t *ppos)
131{
132 struct adis16475 *st = file->private_data;
133 char buf[7];
134 size_t len;
135 u16 rev;
136 int ret;
137
138 ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FIRM_REV, &rev);
139 if (ret)
140 return ret;
141
142 len = scnprintf(buf, sizeof(buf), "%x.%x\n", rev >> 8, rev & 0xff);
143
144 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
145}
146
147static const struct file_operations adis16475_firmware_revision_fops = {
148 .open = simple_open,
149 .read = adis16475_show_firmware_revision,
150 .llseek = default_llseek,
151 .owner = THIS_MODULE,
152};
153
154static ssize_t adis16475_show_firmware_date(struct file *file,
155 char __user *userbuf,
156 size_t count, loff_t *ppos)
157{
158 struct adis16475 *st = file->private_data;
159 u16 md, year;
160 char buf[12];
161 size_t len;
162 int ret;
163
164 ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FIRM_Y, &year);
165 if (ret)
166 return ret;
167
168 ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FIRM_DM, &md);
169 if (ret)
170 return ret;
171
172 len = snprintf(buf, sizeof(buf), "%.2x-%.2x-%.4x\n", md >> 8, md & 0xff,
173 year);
174
175 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
176}
177
178static const struct file_operations adis16475_firmware_date_fops = {
179 .open = simple_open,
180 .read = adis16475_show_firmware_date,
181 .llseek = default_llseek,
182 .owner = THIS_MODULE,
183};
184
185static int adis16475_show_serial_number(void *arg, u64 *val)
186{
187 struct adis16475 *st = arg;
188 u16 serial;
189 int ret;
190
191 ret = adis_read_reg_16(&st->adis, ADIS16475_REG_SERIAL_NUM, &serial);
192 if (ret)
193 return ret;
194
195 *val = serial;
196
197 return 0;
198}
199DEFINE_DEBUGFS_ATTRIBUTE(adis16475_serial_number_fops,
200 adis16475_show_serial_number, NULL, "0x%.4llx\n");
201
202static int adis16475_show_product_id(void *arg, u64 *val)
203{
204 struct adis16475 *st = arg;
205 u16 prod_id;
206 int ret;
207
208 ret = adis_read_reg_16(&st->adis, ADIS16475_REG_PROD_ID, &prod_id);
209 if (ret)
210 return ret;
211
212 *val = prod_id;
213
214 return 0;
215}
216DEFINE_DEBUGFS_ATTRIBUTE(adis16475_product_id_fops,
217 adis16475_show_product_id, NULL, "%llu\n");
218
219static int adis16475_show_flash_count(void *arg, u64 *val)
220{
221 struct adis16475 *st = arg;
222 u32 flash_count;
223 int ret;
224
225 ret = adis_read_reg_32(&st->adis, ADIS16475_REG_FLASH_CNT,
226 &flash_count);
227 if (ret)
228 return ret;
229
230 *val = flash_count;
231
232 return 0;
233}
234DEFINE_DEBUGFS_ATTRIBUTE(adis16475_flash_count_fops,
235 adis16475_show_flash_count, NULL, "%lld\n");
236
237static void adis16475_debugfs_init(struct iio_dev *indio_dev)
238{
239 struct adis16475 *st = iio_priv(indio_dev);
Alexandru Ardeleanb7190852020-04-30 14:04:22 +0300240 struct dentry *d = iio_get_debugfs_dentry(indio_dev);
Nuno Sáfff73522020-04-13 10:24:44 +0200241
242 debugfs_create_file_unsafe("serial_number", 0400,
Alexandru Ardeleanb7190852020-04-30 14:04:22 +0300243 d, st, &adis16475_serial_number_fops);
Nuno Sáfff73522020-04-13 10:24:44 +0200244 debugfs_create_file_unsafe("product_id", 0400,
Alexandru Ardeleanb7190852020-04-30 14:04:22 +0300245 d, st, &adis16475_product_id_fops);
Nuno Sáfff73522020-04-13 10:24:44 +0200246 debugfs_create_file_unsafe("flash_count", 0400,
Alexandru Ardeleanb7190852020-04-30 14:04:22 +0300247 d, st, &adis16475_flash_count_fops);
Nuno Sáfff73522020-04-13 10:24:44 +0200248 debugfs_create_file("firmware_revision", 0400,
Alexandru Ardeleanb7190852020-04-30 14:04:22 +0300249 d, st, &adis16475_firmware_revision_fops);
250 debugfs_create_file("firmware_date", 0400, d,
Nuno Sáfff73522020-04-13 10:24:44 +0200251 st, &adis16475_firmware_date_fops);
252}
253#else
254static void adis16475_debugfs_init(struct iio_dev *indio_dev)
255{
256}
257#endif
258
259static int adis16475_get_freq(struct adis16475 *st, u32 *freq)
260{
261 int ret;
262 u16 dec;
Nuno Sa39c024b2021-02-18 12:40:37 +0100263 u32 sample_rate = st->clk_freq;
Nuno Sáfff73522020-04-13 10:24:44 +0200264
Nuno Sa15aacc92021-02-18 12:40:39 +0100265 adis_dev_lock(&st->adis);
Nuno Sa39c024b2021-02-18 12:40:37 +0100266
267 if (st->sync_mode == ADIS16475_SYNC_SCALED) {
268 u16 sync_scale;
269
270 ret = __adis_read_reg_16(&st->adis, ADIS16475_REG_UP_SCALE, &sync_scale);
271 if (ret)
272 goto error;
273
274 sample_rate = st->clk_freq * sync_scale;
275 }
276
277 ret = __adis_read_reg_16(&st->adis, ADIS16475_REG_DEC_RATE, &dec);
Nuno Sáfff73522020-04-13 10:24:44 +0200278 if (ret)
Nuno Sa39c024b2021-02-18 12:40:37 +0100279 goto error;
Nuno Sáfff73522020-04-13 10:24:44 +0200280
Nuno Sa15aacc92021-02-18 12:40:39 +0100281 adis_dev_unlock(&st->adis);
Nuno Sa39c024b2021-02-18 12:40:37 +0100282
283 *freq = DIV_ROUND_CLOSEST(sample_rate, dec + 1);
Nuno Sáfff73522020-04-13 10:24:44 +0200284
285 return 0;
Nuno Sa39c024b2021-02-18 12:40:37 +0100286error:
Nuno Sa15aacc92021-02-18 12:40:39 +0100287 adis_dev_unlock(&st->adis);
Nuno Sa39c024b2021-02-18 12:40:37 +0100288 return ret;
Nuno Sáfff73522020-04-13 10:24:44 +0200289}
290
291static int adis16475_set_freq(struct adis16475 *st, const u32 freq)
292{
293 u16 dec;
294 int ret;
Nuno Sa39c024b2021-02-18 12:40:37 +0100295 u32 sample_rate = st->clk_freq;
Nuno Sáfff73522020-04-13 10:24:44 +0200296
297 if (!freq)
298 return -EINVAL;
299
Nuno Sa15aacc92021-02-18 12:40:39 +0100300 adis_dev_lock(&st->adis);
Nuno Sa39c024b2021-02-18 12:40:37 +0100301 /*
302 * When using sync scaled mode, the input clock needs to be scaled so that we have
303 * an IMU sample rate between (optimally) 1900 and 2100. After this, we can use the
304 * decimation filter to lower the sampling rate in order to get what the user wants.
305 * Optimally, the user sample rate is a multiple of both the IMU sample rate and
306 * the input clock. Hence, calculating the sync_scale dynamically gives us better
307 * chances of achieving a perfect/integer value for DEC_RATE. The math here is:
308 * 1. lcm of the input clock and the desired output rate.
309 * 2. get the highest multiple of the previous result lower than the adis max rate.
310 * 3. The last result becomes the IMU sample rate. Use that to calculate SYNC_SCALE
311 * and DEC_RATE (to get the user output rate)
312 */
313 if (st->sync_mode == ADIS16475_SYNC_SCALED) {
314 unsigned long scaled_rate = lcm(st->clk_freq, freq);
315 int sync_scale;
316
317 /*
318 * If lcm is bigger than the IMU maximum sampling rate there's no perfect
319 * solution. In this case, we get the highest multiple of the input clock
320 * lower than the IMU max sample rate.
321 */
322 if (scaled_rate > 2100000)
323 scaled_rate = 2100000 / st->clk_freq * st->clk_freq;
324 else
325 scaled_rate = 2100000 / scaled_rate * scaled_rate;
326
327 /*
328 * This is not an hard requirement but it's not advised to run the IMU
329 * with a sample rate lower than 4000Hz due to possible undersampling
330 * issues. However, there are users that might really want to take the risk.
331 * Hence, we provide a module parameter for them. If set, we allow sample
332 * rates lower than 4KHz. By default, we won't allow this and we just roundup
333 * the rate to the next multiple of the input clock bigger than 4KHz. This
334 * is done like this as in some cases (when DEC_RATE is 0) might give
335 * us the closest value to the one desired by the user...
336 */
337 if (scaled_rate < 1900000 && !low_rate_allow)
338 scaled_rate = roundup(1900000, st->clk_freq);
339
340 sync_scale = scaled_rate / st->clk_freq;
341 ret = __adis_write_reg_16(&st->adis, ADIS16475_REG_UP_SCALE, sync_scale);
342 if (ret)
343 goto error;
344
345 sample_rate = scaled_rate;
346 }
347
348 dec = DIV_ROUND_CLOSEST(sample_rate, freq);
Nuno Sáfff73522020-04-13 10:24:44 +0200349
350 if (dec)
351 dec--;
352
353 if (dec > st->info->max_dec)
354 dec = st->info->max_dec;
355
356 ret = adis_write_reg_16(&st->adis, ADIS16475_REG_DEC_RATE, dec);
357 if (ret)
Nuno Sa39c024b2021-02-18 12:40:37 +0100358 goto error;
Nuno Sáfff73522020-04-13 10:24:44 +0200359
360 /*
361 * If decimation is used, then gyro and accel data will have meaningful
362 * bits on the LSB registers. This info is used on the trigger handler.
363 */
364 assign_bit(ADIS16475_LSB_DEC_MASK, &st->lsb_flag, dec);
365
366 return 0;
Nuno Sa39c024b2021-02-18 12:40:37 +0100367error:
Nuno Sa15aacc92021-02-18 12:40:39 +0100368 adis_dev_unlock(&st->adis);
Nuno Sa39c024b2021-02-18 12:40:37 +0100369 return ret;
Nuno Sáfff73522020-04-13 10:24:44 +0200370}
371
372/* The values are approximated. */
373static const u32 adis16475_3db_freqs[] = {
374 [0] = 720, /* Filter disabled, full BW (~720Hz) */
375 [1] = 360,
376 [2] = 164,
377 [3] = 80,
378 [4] = 40,
379 [5] = 20,
380 [6] = 10,
381};
382
383static int adis16475_get_filter(struct adis16475 *st, u32 *filter)
384{
385 u16 filter_sz;
386 int ret;
387 const int mask = ADIS16475_FILT_CTRL_MASK;
388
389 ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FILT_CTRL, &filter_sz);
390 if (ret)
391 return ret;
392
393 *filter = adis16475_3db_freqs[filter_sz & mask];
394
395 return 0;
396}
397
398static int adis16475_set_filter(struct adis16475 *st, const u32 filter)
399{
400 int i = ARRAY_SIZE(adis16475_3db_freqs);
401 int ret;
402
403 while (--i) {
404 if (adis16475_3db_freqs[i] >= filter)
405 break;
406 }
407
408 ret = adis_write_reg_16(&st->adis, ADIS16475_REG_FILT_CTRL,
409 ADIS16475_FILT_CTRL(i));
410 if (ret)
411 return ret;
412
413 /*
414 * If FIR is used, then gyro and accel data will have meaningful
415 * bits on the LSB registers. This info is used on the trigger handler.
416 */
417 assign_bit(ADIS16475_LSB_FIR_MASK, &st->lsb_flag, i);
418
419 return 0;
420}
421
422static const u32 adis16475_calib_regs[] = {
423 [ADIS16475_SCAN_GYRO_X] = ADIS16475_REG_X_GYRO_BIAS_L,
424 [ADIS16475_SCAN_GYRO_Y] = ADIS16475_REG_Y_GYRO_BIAS_L,
425 [ADIS16475_SCAN_GYRO_Z] = ADIS16475_REG_Z_GYRO_BIAS_L,
426 [ADIS16475_SCAN_ACCEL_X] = ADIS16475_REG_X_ACCEL_BIAS_L,
427 [ADIS16475_SCAN_ACCEL_Y] = ADIS16475_REG_Y_ACCEL_BIAS_L,
428 [ADIS16475_SCAN_ACCEL_Z] = ADIS16475_REG_Z_ACCEL_BIAS_L,
429};
430
431static int adis16475_read_raw(struct iio_dev *indio_dev,
432 const struct iio_chan_spec *chan,
433 int *val, int *val2, long info)
434{
435 struct adis16475 *st = iio_priv(indio_dev);
436 int ret;
437 u32 tmp;
438
439 switch (info) {
440 case IIO_CHAN_INFO_RAW:
441 return adis_single_conversion(indio_dev, chan, 0, val);
442 case IIO_CHAN_INFO_SCALE:
443 switch (chan->type) {
444 case IIO_ANGL_VEL:
445 *val = st->info->gyro_max_val;
446 *val2 = st->info->gyro_max_scale;
447 return IIO_VAL_FRACTIONAL;
448 case IIO_ACCEL:
449 *val = st->info->accel_max_val;
450 *val2 = st->info->accel_max_scale;
451 return IIO_VAL_FRACTIONAL;
452 case IIO_TEMP:
453 *val = st->info->temp_scale;
454 return IIO_VAL_INT;
455 default:
456 return -EINVAL;
457 }
458 case IIO_CHAN_INFO_CALIBBIAS:
459 ret = adis_read_reg_32(&st->adis,
460 adis16475_calib_regs[chan->scan_index],
461 val);
462 if (ret)
463 return ret;
464
465 return IIO_VAL_INT;
466 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
467 ret = adis16475_get_filter(st, val);
468 if (ret)
469 return ret;
470
471 return IIO_VAL_INT;
472 case IIO_CHAN_INFO_SAMP_FREQ:
473 ret = adis16475_get_freq(st, &tmp);
474 if (ret)
475 return ret;
476
477 *val = tmp / 1000;
478 *val2 = (tmp % 1000) * 1000;
479 return IIO_VAL_INT_PLUS_MICRO;
480 default:
481 return -EINVAL;
482 }
483}
484
485static int adis16475_write_raw(struct iio_dev *indio_dev,
486 const struct iio_chan_spec *chan,
487 int val, int val2, long info)
488{
489 struct adis16475 *st = iio_priv(indio_dev);
490 u32 tmp;
491
492 switch (info) {
493 case IIO_CHAN_INFO_SAMP_FREQ:
494 tmp = val * 1000 + val2 / 1000;
495 return adis16475_set_freq(st, tmp);
496 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
497 return adis16475_set_filter(st, val);
498 case IIO_CHAN_INFO_CALIBBIAS:
499 return adis_write_reg_32(&st->adis,
500 adis16475_calib_regs[chan->scan_index],
501 val);
502 default:
503 return -EINVAL;
504 }
505}
506
507#define ADIS16475_MOD_CHAN(_type, _mod, _address, _si, _r_bits, _s_bits) \
508 { \
509 .type = (_type), \
510 .modified = 1, \
511 .channel2 = (_mod), \
512 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
513 BIT(IIO_CHAN_INFO_CALIBBIAS), \
514 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
515 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
516 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
517 .address = (_address), \
518 .scan_index = (_si), \
519 .scan_type = { \
520 .sign = 's', \
521 .realbits = (_r_bits), \
522 .storagebits = (_s_bits), \
523 .endianness = IIO_BE, \
524 }, \
525 }
526
527#define ADIS16475_GYRO_CHANNEL(_mod) \
528 ADIS16475_MOD_CHAN(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
529 ADIS16475_REG_ ## _mod ## _GYRO_L, \
530 ADIS16475_SCAN_GYRO_ ## _mod, 32, 32)
531
532#define ADIS16475_ACCEL_CHANNEL(_mod) \
533 ADIS16475_MOD_CHAN(IIO_ACCEL, IIO_MOD_ ## _mod, \
534 ADIS16475_REG_ ## _mod ## _ACCEL_L, \
535 ADIS16475_SCAN_ACCEL_ ## _mod, 32, 32)
536
537#define ADIS16475_TEMP_CHANNEL() { \
538 .type = IIO_TEMP, \
539 .indexed = 1, \
540 .channel = 0, \
541 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
542 BIT(IIO_CHAN_INFO_SCALE), \
543 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
544 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
545 .address = ADIS16475_REG_TEMP_OUT, \
546 .scan_index = ADIS16475_SCAN_TEMP, \
547 .scan_type = { \
548 .sign = 's', \
549 .realbits = 16, \
550 .storagebits = 16, \
551 .endianness = IIO_BE, \
552 }, \
553 }
554
555static const struct iio_chan_spec adis16475_channels[] = {
556 ADIS16475_GYRO_CHANNEL(X),
557 ADIS16475_GYRO_CHANNEL(Y),
558 ADIS16475_GYRO_CHANNEL(Z),
559 ADIS16475_ACCEL_CHANNEL(X),
560 ADIS16475_ACCEL_CHANNEL(Y),
561 ADIS16475_ACCEL_CHANNEL(Z),
562 ADIS16475_TEMP_CHANNEL(),
563 IIO_CHAN_SOFT_TIMESTAMP(7)
564};
565
566enum adis16475_variant {
567 ADIS16470,
568 ADIS16475_1,
569 ADIS16475_2,
570 ADIS16475_3,
571 ADIS16477_1,
572 ADIS16477_2,
573 ADIS16477_3,
574 ADIS16465_1,
575 ADIS16465_2,
576 ADIS16465_3,
577 ADIS16467_1,
578 ADIS16467_2,
579 ADIS16467_3,
580 ADIS16500,
581 ADIS16505_1,
582 ADIS16505_2,
583 ADIS16505_3,
584 ADIS16507_1,
585 ADIS16507_2,
586 ADIS16507_3,
587};
588
589enum {
590 ADIS16475_DIAG_STAT_DATA_PATH = 1,
591 ADIS16475_DIAG_STAT_FLASH_MEM,
592 ADIS16475_DIAG_STAT_SPI,
593 ADIS16475_DIAG_STAT_STANDBY,
594 ADIS16475_DIAG_STAT_SENSOR,
595 ADIS16475_DIAG_STAT_MEMORY,
596 ADIS16475_DIAG_STAT_CLK,
597};
598
599static const char * const adis16475_status_error_msgs[] = {
600 [ADIS16475_DIAG_STAT_DATA_PATH] = "Data Path Overrun",
601 [ADIS16475_DIAG_STAT_FLASH_MEM] = "Flash memory update failure",
602 [ADIS16475_DIAG_STAT_SPI] = "SPI communication error",
603 [ADIS16475_DIAG_STAT_STANDBY] = "Standby mode",
604 [ADIS16475_DIAG_STAT_SENSOR] = "Sensor failure",
605 [ADIS16475_DIAG_STAT_MEMORY] = "Memory failure",
606 [ADIS16475_DIAG_STAT_CLK] = "Clock error",
607};
608
Nuno Sáfff73522020-04-13 10:24:44 +0200609#define ADIS16475_DATA(_prod_id, _timeouts) \
610{ \
611 .msc_ctrl_reg = ADIS16475_REG_MSG_CTRL, \
612 .glob_cmd_reg = ADIS16475_REG_GLOB_CMD, \
613 .diag_stat_reg = ADIS16475_REG_DIAG_STAT, \
614 .prod_id_reg = ADIS16475_REG_PROD_ID, \
615 .prod_id = (_prod_id), \
616 .self_test_mask = BIT(2), \
617 .self_test_reg = ADIS16475_REG_GLOB_CMD, \
618 .cs_change_delay = 16, \
619 .read_delay = 5, \
620 .write_delay = 5, \
621 .status_error_msgs = adis16475_status_error_msgs, \
622 .status_error_mask = BIT(ADIS16475_DIAG_STAT_DATA_PATH) | \
623 BIT(ADIS16475_DIAG_STAT_FLASH_MEM) | \
624 BIT(ADIS16475_DIAG_STAT_SPI) | \
625 BIT(ADIS16475_DIAG_STAT_STANDBY) | \
626 BIT(ADIS16475_DIAG_STAT_SENSOR) | \
627 BIT(ADIS16475_DIAG_STAT_MEMORY) | \
628 BIT(ADIS16475_DIAG_STAT_CLK), \
Nuno Sácab85ea2021-09-03 16:14:21 +0200629 .unmasked_drdy = true, \
Nuno Sáfff73522020-04-13 10:24:44 +0200630 .timeouts = (_timeouts), \
Nuno Sá0dfaa462020-09-17 17:52:22 +0200631 .burst_reg_cmd = ADIS16475_REG_GLOB_CMD, \
632 .burst_len = ADIS16475_BURST_MAX_DATA, \
Nuno Sa256e69a2021-04-27 10:54:53 +0200633 .burst_max_len = ADIS16475_BURST32_MAX_DATA, \
634 .burst_max_speed_hz = ADIS16475_BURST_MAX_SPEED \
Nuno Sáfff73522020-04-13 10:24:44 +0200635}
636
637static const struct adis16475_sync adis16475_sync_mode[] = {
638 { ADIS16475_SYNC_OUTPUT },
639 { ADIS16475_SYNC_DIRECT, 1900, 2100 },
640 { ADIS16475_SYNC_SCALED, 1, 128 },
641 { ADIS16475_SYNC_PULSE, 1000, 2100 },
642};
643
644static const struct adis_timeout adis16475_timeouts = {
645 .reset_ms = 200,
646 .sw_reset_ms = 200,
647 .self_test_ms = 20,
648};
649
650static const struct adis_timeout adis1650x_timeouts = {
651 .reset_ms = 260,
652 .sw_reset_ms = 260,
653 .self_test_ms = 30,
654};
655
656static const struct adis16475_chip_info adis16475_chip_info[] = {
657 [ADIS16470] = {
658 .name = "adis16470",
659 .num_channels = ARRAY_SIZE(adis16475_channels),
660 .channels = adis16475_channels,
661 .gyro_max_val = 1,
662 .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
663 .accel_max_val = 1,
664 .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
665 .temp_scale = 100,
666 .int_clk = 2000,
667 .max_dec = 1999,
668 .sync = adis16475_sync_mode,
669 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
670 .adis_data = ADIS16475_DATA(16470, &adis16475_timeouts),
671 },
672 [ADIS16475_1] = {
673 .name = "adis16475-1",
674 .num_channels = ARRAY_SIZE(adis16475_channels),
675 .channels = adis16475_channels,
676 .gyro_max_val = 1,
677 .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
678 .accel_max_val = 1,
679 .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
680 .temp_scale = 100,
681 .int_clk = 2000,
682 .max_dec = 1999,
683 .sync = adis16475_sync_mode,
684 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
685 .adis_data = ADIS16475_DATA(16475, &adis16475_timeouts),
686 },
687 [ADIS16475_2] = {
688 .name = "adis16475-2",
689 .num_channels = ARRAY_SIZE(adis16475_channels),
690 .channels = adis16475_channels,
691 .gyro_max_val = 1,
692 .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
693 .accel_max_val = 1,
694 .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
695 .temp_scale = 100,
696 .int_clk = 2000,
697 .max_dec = 1999,
698 .sync = adis16475_sync_mode,
699 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
700 .adis_data = ADIS16475_DATA(16475, &adis16475_timeouts),
701 },
702 [ADIS16475_3] = {
703 .name = "adis16475-3",
704 .num_channels = ARRAY_SIZE(adis16475_channels),
705 .channels = adis16475_channels,
706 .gyro_max_val = 1,
707 .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
708 .accel_max_val = 1,
709 .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
710 .temp_scale = 100,
711 .int_clk = 2000,
712 .max_dec = 1999,
713 .sync = adis16475_sync_mode,
714 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
715 .adis_data = ADIS16475_DATA(16475, &adis16475_timeouts),
716 },
717 [ADIS16477_1] = {
718 .name = "adis16477-1",
719 .num_channels = ARRAY_SIZE(adis16475_channels),
720 .channels = adis16475_channels,
721 .gyro_max_val = 1,
722 .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
723 .accel_max_val = 1,
724 .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
725 .temp_scale = 100,
726 .int_clk = 2000,
727 .max_dec = 1999,
728 .sync = adis16475_sync_mode,
729 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
730 .adis_data = ADIS16475_DATA(16477, &adis16475_timeouts),
731 },
732 [ADIS16477_2] = {
733 .name = "adis16477-2",
734 .num_channels = ARRAY_SIZE(adis16475_channels),
735 .channels = adis16475_channels,
736 .gyro_max_val = 1,
737 .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
738 .accel_max_val = 1,
739 .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
740 .temp_scale = 100,
741 .int_clk = 2000,
742 .max_dec = 1999,
743 .sync = adis16475_sync_mode,
744 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
745 .adis_data = ADIS16475_DATA(16477, &adis16475_timeouts),
746 },
747 [ADIS16477_3] = {
748 .name = "adis16477-3",
749 .num_channels = ARRAY_SIZE(adis16475_channels),
750 .channels = adis16475_channels,
751 .gyro_max_val = 1,
752 .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
753 .accel_max_val = 1,
754 .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
755 .temp_scale = 100,
756 .int_clk = 2000,
757 .max_dec = 1999,
758 .sync = adis16475_sync_mode,
759 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
760 .adis_data = ADIS16475_DATA(16477, &adis16475_timeouts),
761 },
762 [ADIS16465_1] = {
763 .name = "adis16465-1",
764 .num_channels = ARRAY_SIZE(adis16475_channels),
765 .channels = adis16475_channels,
766 .gyro_max_val = 1,
767 .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
768 .accel_max_val = 1,
769 .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
770 .temp_scale = 100,
771 .int_clk = 2000,
772 .max_dec = 1999,
773 .sync = adis16475_sync_mode,
774 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
775 .adis_data = ADIS16475_DATA(16465, &adis16475_timeouts),
776 },
777 [ADIS16465_2] = {
778 .name = "adis16465-2",
779 .num_channels = ARRAY_SIZE(adis16475_channels),
780 .channels = adis16475_channels,
781 .gyro_max_val = 1,
782 .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
783 .accel_max_val = 1,
784 .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
785 .temp_scale = 100,
786 .int_clk = 2000,
787 .max_dec = 1999,
788 .sync = adis16475_sync_mode,
789 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
790 .adis_data = ADIS16475_DATA(16465, &adis16475_timeouts),
791 },
792 [ADIS16465_3] = {
793 .name = "adis16465-3",
794 .num_channels = ARRAY_SIZE(adis16475_channels),
795 .channels = adis16475_channels,
796 .gyro_max_val = 1,
797 .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
798 .accel_max_val = 1,
799 .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16),
800 .temp_scale = 100,
801 .int_clk = 2000,
802 .max_dec = 1999,
803 .sync = adis16475_sync_mode,
804 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
805 .adis_data = ADIS16475_DATA(16465, &adis16475_timeouts),
806 },
807 [ADIS16467_1] = {
808 .name = "adis16467-1",
809 .num_channels = ARRAY_SIZE(adis16475_channels),
810 .channels = adis16475_channels,
811 .gyro_max_val = 1,
812 .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
813 .accel_max_val = 1,
814 .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
815 .temp_scale = 100,
816 .int_clk = 2000,
817 .max_dec = 1999,
818 .sync = adis16475_sync_mode,
819 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
820 .adis_data = ADIS16475_DATA(16467, &adis16475_timeouts),
821 },
822 [ADIS16467_2] = {
823 .name = "adis16467-2",
824 .num_channels = ARRAY_SIZE(adis16475_channels),
825 .channels = adis16475_channels,
826 .gyro_max_val = 1,
827 .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
828 .accel_max_val = 1,
829 .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
830 .temp_scale = 100,
831 .int_clk = 2000,
832 .max_dec = 1999,
833 .sync = adis16475_sync_mode,
834 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
835 .adis_data = ADIS16475_DATA(16467, &adis16475_timeouts),
836 },
837 [ADIS16467_3] = {
838 .name = "adis16467-3",
839 .num_channels = ARRAY_SIZE(adis16475_channels),
840 .channels = adis16475_channels,
841 .gyro_max_val = 1,
842 .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
843 .accel_max_val = 1,
844 .accel_max_scale = IIO_M_S_2_TO_G(800 << 16),
845 .temp_scale = 100,
846 .int_clk = 2000,
847 .max_dec = 1999,
848 .sync = adis16475_sync_mode,
849 .num_sync = ARRAY_SIZE(adis16475_sync_mode),
850 .adis_data = ADIS16475_DATA(16467, &adis16475_timeouts),
851 },
852 [ADIS16500] = {
853 .name = "adis16500",
854 .num_channels = ARRAY_SIZE(adis16475_channels),
855 .channels = adis16475_channels,
856 .gyro_max_val = 1,
857 .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
858 .accel_max_val = 392,
859 .accel_max_scale = 32000 << 16,
860 .temp_scale = 100,
861 .int_clk = 2000,
862 .max_dec = 1999,
863 .sync = adis16475_sync_mode,
864 /* pulse sync not supported */
865 .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
866 .has_burst32 = true,
867 .adis_data = ADIS16475_DATA(16500, &adis1650x_timeouts),
868 },
869 [ADIS16505_1] = {
870 .name = "adis16505-1",
871 .num_channels = ARRAY_SIZE(adis16475_channels),
872 .channels = adis16475_channels,
873 .gyro_max_val = 1,
874 .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
875 .accel_max_val = 78,
876 .accel_max_scale = 32000 << 16,
877 .temp_scale = 100,
878 .int_clk = 2000,
879 .max_dec = 1999,
880 .sync = adis16475_sync_mode,
881 /* pulse sync not supported */
882 .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
883 .has_burst32 = true,
884 .adis_data = ADIS16475_DATA(16505, &adis1650x_timeouts),
885 },
886 [ADIS16505_2] = {
887 .name = "adis16505-2",
888 .num_channels = ARRAY_SIZE(adis16475_channels),
889 .channels = adis16475_channels,
890 .gyro_max_val = 1,
891 .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
892 .accel_max_val = 78,
893 .accel_max_scale = 32000 << 16,
894 .temp_scale = 100,
895 .int_clk = 2000,
896 .max_dec = 1999,
897 .sync = adis16475_sync_mode,
898 /* pulse sync not supported */
899 .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
900 .has_burst32 = true,
901 .adis_data = ADIS16475_DATA(16505, &adis1650x_timeouts),
902 },
903 [ADIS16505_3] = {
904 .name = "adis16505-3",
905 .num_channels = ARRAY_SIZE(adis16475_channels),
906 .channels = adis16475_channels,
907 .gyro_max_val = 1,
908 .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
909 .accel_max_val = 78,
910 .accel_max_scale = 32000 << 16,
911 .temp_scale = 100,
912 .int_clk = 2000,
913 .max_dec = 1999,
914 .sync = adis16475_sync_mode,
915 /* pulse sync not supported */
916 .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
917 .has_burst32 = true,
918 .adis_data = ADIS16475_DATA(16505, &adis1650x_timeouts),
919 },
920 [ADIS16507_1] = {
921 .name = "adis16507-1",
922 .num_channels = ARRAY_SIZE(adis16475_channels),
923 .channels = adis16475_channels,
924 .gyro_max_val = 1,
925 .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16),
926 .accel_max_val = 392,
927 .accel_max_scale = 32000 << 16,
928 .temp_scale = 100,
929 .int_clk = 2000,
930 .max_dec = 1999,
931 .sync = adis16475_sync_mode,
932 /* pulse sync not supported */
933 .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
934 .has_burst32 = true,
935 .adis_data = ADIS16475_DATA(16507, &adis1650x_timeouts),
936 },
937 [ADIS16507_2] = {
938 .name = "adis16507-2",
939 .num_channels = ARRAY_SIZE(adis16475_channels),
940 .channels = adis16475_channels,
941 .gyro_max_val = 1,
942 .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16),
943 .accel_max_val = 392,
944 .accel_max_scale = 32000 << 16,
945 .temp_scale = 100,
946 .int_clk = 2000,
947 .max_dec = 1999,
948 .sync = adis16475_sync_mode,
949 /* pulse sync not supported */
950 .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
951 .has_burst32 = true,
952 .adis_data = ADIS16475_DATA(16507, &adis1650x_timeouts),
953 },
954 [ADIS16507_3] = {
955 .name = "adis16507-3",
956 .num_channels = ARRAY_SIZE(adis16475_channels),
957 .channels = adis16475_channels,
958 .gyro_max_val = 1,
959 .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16),
960 .accel_max_val = 392,
961 .accel_max_scale = 32000 << 16,
962 .temp_scale = 100,
963 .int_clk = 2000,
964 .max_dec = 1999,
965 .sync = adis16475_sync_mode,
966 /* pulse sync not supported */
967 .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1,
968 .has_burst32 = true,
969 .adis_data = ADIS16475_DATA(16507, &adis1650x_timeouts),
970 },
971};
972
973static const struct iio_info adis16475_info = {
974 .read_raw = &adis16475_read_raw,
975 .write_raw = &adis16475_write_raw,
976 .update_scan_mode = adis_update_scan_mode,
977 .debugfs_reg_access = adis_debugfs_reg_access,
978};
979
Nuno Sáfff73522020-04-13 10:24:44 +0200980static bool adis16475_validate_crc(const u8 *buffer, u16 crc,
981 const bool burst32)
982{
983 int i;
984 /* extra 6 elements for low gyro and accel */
985 const u16 sz = burst32 ? ADIS16475_BURST32_MAX_DATA :
986 ADIS16475_BURST_MAX_DATA;
987
988 for (i = 0; i < sz - 2; i++)
989 crc -= buffer[i];
990
991 return crc == 0;
992}
993
994static void adis16475_burst32_check(struct adis16475 *st)
995{
996 int ret;
997 struct adis *adis = &st->adis;
998
999 if (!st->info->has_burst32)
1000 return;
1001
1002 if (st->lsb_flag && !st->burst32) {
1003 const u16 en = ADIS16500_BURST32(1);
1004
1005 ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL,
1006 ADIS16500_BURST32_MASK, en);
1007 if (ret)
1008 return;
1009
1010 st->burst32 = true;
1011
1012 /*
1013 * In 32-bit mode we need extra 2 bytes for all gyro
1014 * and accel channels.
1015 */
1016 adis->burst_extra_len = 6 * sizeof(u16);
1017 adis->xfer[1].len += 6 * sizeof(u16);
1018 dev_dbg(&adis->spi->dev, "Enable burst32 mode, xfer:%d",
1019 adis->xfer[1].len);
1020
1021 } else if (!st->lsb_flag && st->burst32) {
1022 const u16 en = ADIS16500_BURST32(0);
1023
1024 ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL,
1025 ADIS16500_BURST32_MASK, en);
1026 if (ret)
1027 return;
1028
1029 st->burst32 = false;
1030
1031 /* Remove the extra bits */
1032 adis->burst_extra_len = 0;
1033 adis->xfer[1].len -= 6 * sizeof(u16);
1034 dev_dbg(&adis->spi->dev, "Disable burst32 mode, xfer:%d\n",
1035 adis->xfer[1].len);
1036 }
1037}
1038
1039static irqreturn_t adis16475_trigger_handler(int irq, void *p)
1040{
1041 struct iio_poll_func *pf = p;
1042 struct iio_dev *indio_dev = pf->indio_dev;
1043 struct adis16475 *st = iio_priv(indio_dev);
1044 struct adis *adis = &st->adis;
1045 int ret, bit, i = 0;
1046 __be16 *buffer;
1047 u16 crc;
1048 bool valid;
1049 /* offset until the first element after gyro and accel */
1050 const u8 offset = st->burst32 ? 13 : 7;
Nuno Sáfff73522020-04-13 10:24:44 +02001051
1052 ret = spi_sync(adis->spi, &adis->msg);
1053 if (ret)
Nuno Sa00a72db2021-04-27 10:54:49 +02001054 goto check_burst32;
Nuno Sáfff73522020-04-13 10:24:44 +02001055
Nuno Sáfff73522020-04-13 10:24:44 +02001056 buffer = adis->buffer;
1057
1058 crc = be16_to_cpu(buffer[offset + 2]);
1059 valid = adis16475_validate_crc(adis->buffer, crc, st->burst32);
1060 if (!valid) {
1061 dev_err(&adis->spi->dev, "Invalid crc\n");
1062 goto check_burst32;
1063 }
1064
1065 for_each_set_bit(bit, indio_dev->active_scan_mask,
1066 indio_dev->masklength) {
1067 /*
1068 * When burst mode is used, system flags is the first data
1069 * channel in the sequence, but the scan index is 7.
1070 */
1071 switch (bit) {
1072 case ADIS16475_SCAN_TEMP:
1073 st->data[i++] = buffer[offset];
1074 break;
1075 case ADIS16475_SCAN_GYRO_X ... ADIS16475_SCAN_ACCEL_Z:
1076 /*
1077 * The first 2 bytes on the received data are the
1078 * DIAG_STAT reg, hence the +1 offset here...
1079 */
1080 if (st->burst32) {
1081 /* upper 16 */
1082 st->data[i++] = buffer[bit * 2 + 2];
1083 /* lower 16 */
1084 st->data[i++] = buffer[bit * 2 + 1];
1085 } else {
1086 st->data[i++] = buffer[bit + 1];
1087 /*
1088 * Don't bother in doing the manual read if the
1089 * device supports burst32. burst32 will be
1090 * enabled in the next call to
1091 * adis16475_burst32_check()...
1092 */
1093 if (st->lsb_flag && !st->info->has_burst32) {
1094 u16 val = 0;
1095 const u32 reg = ADIS16475_REG_X_GYRO_L +
1096 bit * 4;
1097
1098 adis_read_reg_16(adis, reg, &val);
1099 st->data[i++] = cpu_to_be16(val);
1100 } else {
1101 /* lower not used */
1102 st->data[i++] = 0;
1103 }
1104 }
1105 break;
1106 }
1107 }
1108
1109 iio_push_to_buffers_with_timestamp(indio_dev, st->data, pf->timestamp);
1110check_burst32:
1111 /*
1112 * We only check the burst mode at the end of the current capture since
1113 * it takes a full data ready cycle for the device to update the burst
1114 * array.
1115 */
1116 adis16475_burst32_check(st);
1117 iio_trigger_notify_done(indio_dev->trig);
1118
1119 return IRQ_HANDLED;
1120}
1121
1122static void adis16475_disable_clk(void *data)
1123{
1124 clk_disable_unprepare((struct clk *)data);
1125}
1126
1127static int adis16475_config_sync_mode(struct adis16475 *st)
1128{
1129 int ret;
1130 struct device *dev = &st->adis.spi->dev;
1131 const struct adis16475_sync *sync;
1132 u32 sync_mode;
1133
1134 /* default to internal clk */
1135 st->clk_freq = st->info->int_clk * 1000;
1136
1137 ret = device_property_read_u32(dev, "adi,sync-mode", &sync_mode);
1138 if (ret)
1139 return 0;
1140
1141 if (sync_mode >= st->info->num_sync) {
1142 dev_err(dev, "Invalid sync mode: %u for %s\n", sync_mode,
1143 st->info->name);
1144 return -EINVAL;
1145 }
1146
1147 sync = &st->info->sync[sync_mode];
Nuno Sa39c024b2021-02-18 12:40:37 +01001148 st->sync_mode = sync->sync_mode;
Nuno Sáfff73522020-04-13 10:24:44 +02001149
1150 /* All the other modes require external input signal */
1151 if (sync->sync_mode != ADIS16475_SYNC_OUTPUT) {
1152 struct clk *clk = devm_clk_get(dev, NULL);
1153
1154 if (IS_ERR(clk))
1155 return PTR_ERR(clk);
1156
1157 ret = clk_prepare_enable(clk);
1158 if (ret)
1159 return ret;
1160
1161 ret = devm_add_action_or_reset(dev, adis16475_disable_clk, clk);
1162 if (ret)
1163 return ret;
1164
1165 st->clk_freq = clk_get_rate(clk);
1166 if (st->clk_freq < sync->min_rate ||
1167 st->clk_freq > sync->max_rate) {
1168 dev_err(dev,
1169 "Clk rate:%u not in a valid range:[%u %u]\n",
1170 st->clk_freq, sync->min_rate, sync->max_rate);
1171 return -EINVAL;
1172 }
1173
1174 if (sync->sync_mode == ADIS16475_SYNC_SCALED) {
1175 u16 up_scale;
Nuno Sáfff73522020-04-13 10:24:44 +02001176
Nuno Sa39c024b2021-02-18 12:40:37 +01001177 /*
1178 * In sync scaled mode, the IMU sample rate is the clk_freq * sync_scale.
1179 * Hence, default the IMU sample rate to the highest multiple of the input
1180 * clock lower than the IMU max sample rate. The optimal range is
1181 * 1900-2100 sps...
1182 */
1183 up_scale = 2100 / st->clk_freq;
Nuno Sáfff73522020-04-13 10:24:44 +02001184
1185 ret = __adis_write_reg_16(&st->adis,
1186 ADIS16475_REG_UP_SCALE,
1187 up_scale);
1188 if (ret)
1189 return ret;
Nuno Sáfff73522020-04-13 10:24:44 +02001190 }
1191
1192 st->clk_freq *= 1000;
1193 }
1194 /*
1195 * Keep in mind that the mask for the clk modes in adis1650*
1196 * chips is different (1100 instead of 11100). However, we
1197 * are not configuring BIT(4) in these chips and the default
1198 * value is 0, so we are fine in doing the below operations.
1199 * I'm keeping this for simplicity and avoiding extra variables
1200 * in chip_info.
1201 */
1202 ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL,
1203 ADIS16475_SYNC_MODE_MASK, sync->sync_mode);
1204 if (ret)
1205 return ret;
1206
1207 usleep_range(250, 260);
1208
1209 return 0;
1210}
1211
1212static int adis16475_config_irq_pin(struct adis16475 *st)
1213{
1214 int ret;
1215 struct irq_data *desc;
1216 u32 irq_type;
1217 u16 val = 0;
1218 u8 polarity;
1219 struct spi_device *spi = st->adis.spi;
1220
1221 desc = irq_get_irq_data(spi->irq);
1222 if (!desc) {
1223 dev_err(&spi->dev, "Could not find IRQ %d\n", spi->irq);
1224 return -EINVAL;
1225 }
1226 /*
1227 * It is possible to configure the data ready polarity. Furthermore, we
1228 * need to update the adis struct if we want data ready as active low.
1229 */
1230 irq_type = irqd_get_trigger_type(desc);
1231 if (irq_type == IRQ_TYPE_EDGE_RISING) {
1232 polarity = 1;
1233 st->adis.irq_flag = IRQF_TRIGGER_RISING;
1234 } else if (irq_type == IRQ_TYPE_EDGE_FALLING) {
1235 polarity = 0;
1236 st->adis.irq_flag = IRQF_TRIGGER_FALLING;
1237 } else {
1238 dev_err(&spi->dev, "Invalid interrupt type 0x%x specified\n",
1239 irq_type);
1240 return -EINVAL;
1241 }
1242
1243 val = ADIS16475_MSG_CTRL_DR_POL(polarity);
1244 ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL,
1245 ADIS16475_MSG_CTRL_DR_POL_MASK, val);
1246 if (ret)
1247 return ret;
1248 /*
1249 * There is a delay writing to any bits written to the MSC_CTRL
1250 * register. It should not be bigger than 200us, so 250 should be more
1251 * than enough!
1252 */
1253 usleep_range(250, 260);
1254
1255 return 0;
1256}
1257
1258static const struct of_device_id adis16475_of_match[] = {
1259 { .compatible = "adi,adis16470",
1260 .data = &adis16475_chip_info[ADIS16470] },
1261 { .compatible = "adi,adis16475-1",
1262 .data = &adis16475_chip_info[ADIS16475_1] },
1263 { .compatible = "adi,adis16475-2",
1264 .data = &adis16475_chip_info[ADIS16475_2] },
1265 { .compatible = "adi,adis16475-3",
1266 .data = &adis16475_chip_info[ADIS16475_3] },
1267 { .compatible = "adi,adis16477-1",
1268 .data = &adis16475_chip_info[ADIS16477_1] },
1269 { .compatible = "adi,adis16477-2",
1270 .data = &adis16475_chip_info[ADIS16477_2] },
1271 { .compatible = "adi,adis16477-3",
1272 .data = &adis16475_chip_info[ADIS16477_3] },
1273 { .compatible = "adi,adis16465-1",
1274 .data = &adis16475_chip_info[ADIS16465_1] },
1275 { .compatible = "adi,adis16465-2",
1276 .data = &adis16475_chip_info[ADIS16465_2] },
1277 { .compatible = "adi,adis16465-3",
1278 .data = &adis16475_chip_info[ADIS16465_3] },
1279 { .compatible = "adi,adis16467-1",
1280 .data = &adis16475_chip_info[ADIS16467_1] },
1281 { .compatible = "adi,adis16467-2",
1282 .data = &adis16475_chip_info[ADIS16467_2] },
1283 { .compatible = "adi,adis16467-3",
1284 .data = &adis16475_chip_info[ADIS16467_3] },
1285 { .compatible = "adi,adis16500",
1286 .data = &adis16475_chip_info[ADIS16500] },
1287 { .compatible = "adi,adis16505-1",
1288 .data = &adis16475_chip_info[ADIS16505_1] },
1289 { .compatible = "adi,adis16505-2",
1290 .data = &adis16475_chip_info[ADIS16505_2] },
1291 { .compatible = "adi,adis16505-3",
1292 .data = &adis16475_chip_info[ADIS16505_3] },
1293 { .compatible = "adi,adis16507-1",
1294 .data = &adis16475_chip_info[ADIS16507_1] },
1295 { .compatible = "adi,adis16507-2",
1296 .data = &adis16475_chip_info[ADIS16507_2] },
1297 { .compatible = "adi,adis16507-3",
1298 .data = &adis16475_chip_info[ADIS16507_3] },
1299 { },
1300};
1301MODULE_DEVICE_TABLE(of, adis16475_of_match);
1302
1303static int adis16475_probe(struct spi_device *spi)
1304{
1305 struct iio_dev *indio_dev;
1306 struct adis16475 *st;
1307 int ret;
1308
1309 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1310 if (!indio_dev)
1311 return -ENOMEM;
1312
1313 st = iio_priv(indio_dev);
Nuno Sáfff73522020-04-13 10:24:44 +02001314
1315 st->info = device_get_match_data(&spi->dev);
1316 if (!st->info)
1317 return -EINVAL;
1318
1319 ret = adis_init(&st->adis, indio_dev, spi, &st->info->adis_data);
1320 if (ret)
1321 return ret;
1322
Nuno Sáfff73522020-04-13 10:24:44 +02001323 indio_dev->name = st->info->name;
1324 indio_dev->channels = st->info->channels;
1325 indio_dev->num_channels = st->info->num_channels;
1326 indio_dev->info = &adis16475_info;
1327 indio_dev->modes = INDIO_DIRECT_MODE;
1328
1329 ret = __adis_initial_startup(&st->adis);
1330 if (ret)
1331 return ret;
1332
1333 ret = adis16475_config_irq_pin(st);
1334 if (ret)
1335 return ret;
1336
1337 ret = adis16475_config_sync_mode(st);
1338 if (ret)
1339 return ret;
1340
1341 ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev,
1342 adis16475_trigger_handler);
1343 if (ret)
1344 return ret;
1345
Nuno Sáfff73522020-04-13 10:24:44 +02001346 ret = devm_iio_device_register(&spi->dev, indio_dev);
1347 if (ret)
1348 return ret;
1349
1350 adis16475_debugfs_init(indio_dev);
1351
1352 return 0;
1353}
1354
1355static struct spi_driver adis16475_driver = {
1356 .driver = {
1357 .name = "adis16475",
1358 .of_match_table = adis16475_of_match,
1359 },
1360 .probe = adis16475_probe,
1361};
1362module_spi_driver(adis16475_driver);
1363
1364MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1365MODULE_DESCRIPTION("Analog Devices ADIS16475 IMU driver");
1366MODULE_LICENSE("GPL");