1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
// SPDX-License-Identifier: GPL-2.0+
/*
* ADIS16460 IMU driver
*
* Copyright 2019 Analog Devices Inc.
*/
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/spi/spi.h>
#include <linux/iio/iio.h>
#include <linux/iio/imu/adis.h>
#include <linux/debugfs.h>
#define ADIS16460_REG_FLASH_CNT 0x00
#define ADIS16460_REG_DIAG_STAT 0x02
#define ADIS16460_REG_X_GYRO_LOW 0x04
#define ADIS16460_REG_X_GYRO_OUT 0x06
#define ADIS16460_REG_Y_GYRO_LOW 0x08
#define ADIS16460_REG_Y_GYRO_OUT 0x0A
#define ADIS16460_REG_Z_GYRO_LOW 0x0C
#define ADIS16460_REG_Z_GYRO_OUT 0x0E
#define ADIS16460_REG_X_ACCL_LOW 0x10
#define ADIS16460_REG_X_ACCL_OUT 0x12
#define ADIS16460_REG_Y_ACCL_LOW 0x14
#define ADIS16460_REG_Y_ACCL_OUT 0x16
#define ADIS16460_REG_Z_ACCL_LOW 0x18
#define ADIS16460_REG_Z_ACCL_OUT 0x1A
#define ADIS16460_REG_SMPL_CNTR 0x1C
#define ADIS16460_REG_TEMP_OUT 0x1E
#define ADIS16460_REG_X_DELT_ANG 0x24
#define ADIS16460_REG_Y_DELT_ANG 0x26
#define ADIS16460_REG_Z_DELT_ANG 0x28
#define ADIS16460_REG_X_DELT_VEL 0x2A
#define ADIS16460_REG_Y_DELT_VEL 0x2C
#define ADIS16460_REG_Z_DELT_VEL 0x2E
#define ADIS16460_REG_MSC_CTRL 0x32
#define ADIS16460_REG_SYNC_SCAL 0x34
#define ADIS16460_REG_DEC_RATE 0x36
#define ADIS16460_REG_FLTR_CTRL 0x38
#define ADIS16460_REG_GLOB_CMD 0x3E
#define ADIS16460_REG_X_GYRO_OFF 0x40
#define ADIS16460_REG_Y_GYRO_OFF 0x42
#define ADIS16460_REG_Z_GYRO_OFF 0x44
#define ADIS16460_REG_X_ACCL_OFF 0x46
#define ADIS16460_REG_Y_ACCL_OFF 0x48
#define ADIS16460_REG_Z_ACCL_OFF 0x4A
#define ADIS16460_REG_LOT_ID1 0x52
#define ADIS16460_REG_LOT_ID2 0x54
#define ADIS16460_REG_PROD_ID 0x56
#define ADIS16460_REG_SERIAL_NUM 0x58
#define ADIS16460_REG_CAL_SGNTR 0x60
#define ADIS16460_REG_CAL_CRC 0x62
#define ADIS16460_REG_CODE_SGNTR 0x64
#define ADIS16460_REG_CODE_CRC 0x66
struct adis16460_chip_info {
unsigned int num_channels;
const struct iio_chan_spec *channels;
unsigned int gyro_max_val;
unsigned int gyro_max_scale;
unsigned int accel_max_val;
unsigned int accel_max_scale;
};
struct adis16460 {
const struct adis16460_chip_info *chip_info;
struct adis adis;
};
#ifdef CONFIG_DEBUG_FS
static int adis16460_show_serial_number(void *arg, u64 *val)
{
struct adis16460 *adis16460 = arg;
u16 serial;
int ret;
ret = adis_read_reg_16(&adis16460->adis, ADIS16460_REG_SERIAL_NUM,
&serial);
if (ret)
return ret;
*val = serial;
return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(adis16460_serial_number_fops,
adis16460_show_serial_number, NULL, "0x%.4llx\n");
static int adis16460_show_product_id(void *arg, u64 *val)
{
struct adis16460 *adis16460 = arg;
u16 prod_id;
int ret;
ret = adis_read_reg_16(&adis16460->adis, ADIS16460_REG_PROD_ID,
&prod_id);
if (ret)
return ret;
*val = prod_id;
return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(adis16460_product_id_fops,
adis16460_show_product_id, NULL, "%llu\n");
static int adis16460_show_flash_count(void *arg, u64 *val)
{
struct adis16460 *adis16460 = arg;
u32 flash_count;
int ret;
ret = adis_read_reg_32(&adis16460->adis, ADIS16460_REG_FLASH_CNT,
&flash_count);
if (ret)
return ret;
*val = flash_count;
return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(adis16460_flash_count_fops,
adis16460_show_flash_count, NULL, "%lld\n");
static int adis16460_debugfs_init(struct iio_dev *indio_dev)
{
struct adis16460 *adis16460 = iio_priv(indio_dev);
struct dentry *d = iio_get_debugfs_dentry(indio_dev);
debugfs_create_file_unsafe("serial_number", 0400,
d, adis16460, &adis16460_serial_number_fops);
debugfs_create_file_unsafe("product_id", 0400,
d, adis16460, &adis16460_product_id_fops);
debugfs_create_file_unsafe("flash_count", 0400,
d, adis16460, &adis16460_flash_count_fops);
return 0;
}
#else
static int adis16460_debugfs_init(struct iio_dev *indio_dev)
{
return 0;
}
#endif
static int adis16460_set_freq(struct iio_dev *indio_dev, int val, int val2)
{
struct adis16460 *st = iio_priv(indio_dev);
int t;
t = val * 1000 + val2 / 1000;
if (t <= 0)
return -EINVAL;
t = 2048000 / t;
if (t > 2048)
t = 2048;
if (t != 0)
t--;
return adis_write_reg_16(&st->adis, ADIS16460_REG_DEC_RATE, t);
}
static int adis16460_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
{
struct adis16460 *st = iio_priv(indio_dev);
uint16_t t;
int ret;
unsigned int freq;
ret = adis_read_reg_16(&st->adis, ADIS16460_REG_DEC_RATE, &t);
if (ret)
return ret;
freq = 2048000 / (t + 1);
*val = freq / 1000;
*val2 = (freq % 1000) * 1000;
return IIO_VAL_INT_PLUS_MICRO;
}
static int adis16460_read_raw(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, int *val, int *val2, long info)
{
struct adis16460 *st = iio_priv(indio_dev);
switch (info) {
case IIO_CHAN_INFO_RAW:
return adis_single_conversion(indio_dev, chan, 0, val);
case IIO_CHAN_INFO_SCALE:
switch (chan->type) {
case IIO_ANGL_VEL:
*val = st->chip_info->gyro_max_scale;
*val2 = st->chip_info->gyro_max_val;
return IIO_VAL_FRACTIONAL;
case IIO_ACCEL:
*val = st->chip_info->accel_max_scale;
*val2 = st->chip_info->accel_max_val;
return IIO_VAL_FRACTIONAL;
case IIO_TEMP:
*val = 50; /* 50 milli degrees Celsius/LSB */
return IIO_VAL_INT;
default:
return -EINVAL;
}
case IIO_CHAN_INFO_OFFSET:
*val = 500; /* 25 degrees Celsius = 0x0000 */
return IIO_VAL_INT;
case IIO_CHAN_INFO_SAMP_FREQ:
return adis16460_get_freq(indio_dev, val, val2);
default:
return -EINVAL;
}
}
static int adis16460_write_raw(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, int val, int val2, long info)
{
switch (info) {
case IIO_CHAN_INFO_SAMP_FREQ:
return adis16460_set_freq(indio_dev, val, val2);
default:
return -EINVAL;
}
}
enum {
ADIS16460_SCAN_GYRO_X,
ADIS16460_SCAN_GYRO_Y,
ADIS16460_SCAN_GYRO_Z,
ADIS16460_SCAN_ACCEL_X,
ADIS16460_SCAN_ACCEL_Y,
ADIS16460_SCAN_ACCEL_Z,
ADIS16460_SCAN_TEMP,
};
#define ADIS16460_MOD_CHANNEL(_type, _mod, _address, _si, _bits) \
{ \
.type = (_type), \
.modified = 1, \
.channel2 = (_mod), \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
.address = (_address), \
.scan_index = (_si), \
.scan_type = { \
.sign = 's', \
.realbits = (_bits), \
.storagebits = (_bits), \
.endianness = IIO_BE, \
}, \
}
#define ADIS16460_GYRO_CHANNEL(_mod) \
ADIS16460_MOD_CHANNEL(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
ADIS16460_REG_ ## _mod ## _GYRO_LOW, ADIS16460_SCAN_GYRO_ ## _mod, \
32)
#define ADIS16460_ACCEL_CHANNEL(_mod) \
ADIS16460_MOD_CHANNEL(IIO_ACCEL, IIO_MOD_ ## _mod, \
ADIS16460_REG_ ## _mod ## _ACCL_LOW, ADIS16460_SCAN_ACCEL_ ## _mod, \
32)
#define ADIS16460_TEMP_CHANNEL() { \
.type = IIO_TEMP, \
.indexed = 1, \
.channel = 0, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
BIT(IIO_CHAN_INFO_SCALE) | \
BIT(IIO_CHAN_INFO_OFFSET), \
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
.address = ADIS16460_REG_TEMP_OUT, \
.scan_index = ADIS16460_SCAN_TEMP, \
.scan_type = { \
.sign = 's', \
.realbits = 16, \
.storagebits = 16, \
.endianness = IIO_BE, \
}, \
}
static const struct iio_chan_spec adis16460_channels[] = {
ADIS16460_GYRO_CHANNEL(X),
ADIS16460_GYRO_CHANNEL(Y),
ADIS16460_GYRO_CHANNEL(Z),
ADIS16460_ACCEL_CHANNEL(X),
ADIS16460_ACCEL_CHANNEL(Y),
ADIS16460_ACCEL_CHANNEL(Z),
ADIS16460_TEMP_CHANNEL(),
IIO_CHAN_SOFT_TIMESTAMP(7)
};
static const struct adis16460_chip_info adis16460_chip_info = {
.channels = adis16460_channels,
.num_channels = ARRAY_SIZE(adis16460_channels),
/*
* storing the value in rad/degree and the scale in degree
* gives us the result in rad and better precession than
* storing the scale directly in rad.
*/
.gyro_max_val = IIO_RAD_TO_DEGREE(200 << 16),
.gyro_max_scale = 1,
.accel_max_val = IIO_M_S_2_TO_G(20000 << 16),
.accel_max_scale = 5,
};
static const struct iio_info adis16460_info = {
.read_raw = &adis16460_read_raw,
.write_raw = &adis16460_write_raw,
.update_scan_mode = adis_update_scan_mode,
.debugfs_reg_access = adis_debugfs_reg_access,
};
static int adis16460_enable_irq(struct adis *adis, bool enable)
{
/*
* There is no way to gate the data-ready signal internally inside the
* ADIS16460 :(
*/
if (enable)
enable_irq(adis->spi->irq);
else
disable_irq
|