blob: a4c967a5fc5c6c90eb0f44acd2008775adea7185 [file] [log] [blame]
Thomas Gleixnerfda8d262019-05-28 09:57:06 -07001// SPDX-License-Identifier: GPL-2.0-only
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +00002/*
3 * ADIS16133/ADIS16135/ADIS16136 gyroscope driver
4 *
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +00007 */
8
9#include <linux/interrupt.h>
10#include <linux/delay.h>
11#include <linux/mutex.h>
12#include <linux/device.h>
13#include <linux/kernel.h>
14#include <linux/spi/spi.h>
15#include <linux/slab.h>
16#include <linux/sysfs.h>
17#include <linux/module.h>
18
19#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
21#include <linux/iio/buffer.h>
22#include <linux/iio/imu/adis.h>
23
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +000024#include <linux/debugfs.h>
25
26#define ADIS16136_REG_FLASH_CNT 0x00
27#define ADIS16136_REG_TEMP_OUT 0x02
28#define ADIS16136_REG_GYRO_OUT2 0x04
29#define ADIS16136_REG_GYRO_OUT 0x06
30#define ADIS16136_REG_GYRO_OFF2 0x08
31#define ADIS16136_REG_GYRO_OFF 0x0A
32#define ADIS16136_REG_ALM_MAG1 0x10
33#define ADIS16136_REG_ALM_MAG2 0x12
34#define ADIS16136_REG_ALM_SAMPL1 0x14
35#define ADIS16136_REG_ALM_SAMPL2 0x16
36#define ADIS16136_REG_ALM_CTRL 0x18
37#define ADIS16136_REG_GPIO_CTRL 0x1A
38#define ADIS16136_REG_MSC_CTRL 0x1C
39#define ADIS16136_REG_SMPL_PRD 0x1E
40#define ADIS16136_REG_AVG_CNT 0x20
41#define ADIS16136_REG_DEC_RATE 0x22
42#define ADIS16136_REG_SLP_CTRL 0x24
43#define ADIS16136_REG_DIAG_STAT 0x26
44#define ADIS16136_REG_GLOB_CMD 0x28
45#define ADIS16136_REG_LOT1 0x32
46#define ADIS16136_REG_LOT2 0x34
47#define ADIS16136_REG_LOT3 0x36
48#define ADIS16136_REG_PROD_ID 0x38
49#define ADIS16136_REG_SERIAL_NUM 0x3A
50
51#define ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL 2
52#define ADIS16136_DIAG_STAT_SPI_FAIL 3
53#define ADIS16136_DIAG_STAT_SELF_TEST_FAIL 5
54#define ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL 6
55
56#define ADIS16136_MSC_CTRL_MEMORY_TEST BIT(11)
57#define ADIS16136_MSC_CTRL_SELF_TEST BIT(10)
58
59struct adis16136_chip_info {
60 unsigned int precision;
61 unsigned int fullscale;
Alexandru Ardeleane914cfd2020-02-10 15:26:00 +020062 const struct adis_data adis_data;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +000063};
64
65struct adis16136 {
66 const struct adis16136_chip_info *chip_info;
67
68 struct adis adis;
69};
70
71#ifdef CONFIG_DEBUG_FS
72
73static ssize_t adis16136_show_serial(struct file *file,
74 char __user *userbuf, size_t count, loff_t *ppos)
75{
76 struct adis16136 *adis16136 = file->private_data;
77 uint16_t lot1, lot2, lot3, serial;
78 char buf[20];
79 size_t len;
80 int ret;
81
82 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SERIAL_NUM,
83 &serial);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +020084 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +000085 return ret;
86
87 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT1, &lot1);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +020088 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +000089 return ret;
90
91 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT2, &lot2);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +020092 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +000093 return ret;
94
95 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT3, &lot3);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +020096 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +000097 return ret;
98
99 len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
100 lot3, serial);
101
102 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
103}
104
105static const struct file_operations adis16136_serial_fops = {
106 .open = simple_open,
107 .read = adis16136_show_serial,
108 .llseek = default_llseek,
109 .owner = THIS_MODULE,
110};
111
112static int adis16136_show_product_id(void *arg, u64 *val)
113{
114 struct adis16136 *adis16136 = arg;
115 u16 prod_id;
116 int ret;
117
118 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID,
119 &prod_id);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +0200120 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000121 return ret;
122
123 *val = prod_id;
124
125 return 0;
126}
Venkat Prashanth B U9aaea092018-01-07 14:31:57 +0530127DEFINE_DEBUGFS_ATTRIBUTE(adis16136_product_id_fops,
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000128 adis16136_show_product_id, NULL, "%llu\n");
129
130static int adis16136_show_flash_count(void *arg, u64 *val)
131{
132 struct adis16136 *adis16136 = arg;
133 uint16_t flash_count;
134 int ret;
135
136 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_FLASH_CNT,
137 &flash_count);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +0200138 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000139 return ret;
140
141 *val = flash_count;
142
143 return 0;
144}
Venkat Prashanth B U9aaea092018-01-07 14:31:57 +0530145DEFINE_DEBUGFS_ATTRIBUTE(adis16136_flash_count_fops,
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000146 adis16136_show_flash_count, NULL, "%lld\n");
147
148static int adis16136_debugfs_init(struct iio_dev *indio_dev)
149{
150 struct adis16136 *adis16136 = iio_priv(indio_dev);
151
Venkat Prashanth B U9aaea092018-01-07 14:31:57 +0530152 debugfs_create_file_unsafe("serial_number", 0400,
153 indio_dev->debugfs_dentry, adis16136,
154 &adis16136_serial_fops);
155 debugfs_create_file_unsafe("product_id", 0400,
156 indio_dev->debugfs_dentry,
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000157 adis16136, &adis16136_product_id_fops);
Venkat Prashanth B U9aaea092018-01-07 14:31:57 +0530158 debugfs_create_file_unsafe("flash_count", 0400,
159 indio_dev->debugfs_dentry,
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000160 adis16136, &adis16136_flash_count_fops);
161
162 return 0;
163}
164
165#else
166
167static int adis16136_debugfs_init(struct iio_dev *indio_dev)
168{
169 return 0;
170}
171
172#endif
173
174static int adis16136_set_freq(struct adis16136 *adis16136, unsigned int freq)
175{
176 unsigned int t;
177
178 t = 32768 / freq;
179 if (t < 0xf)
180 t = 0xf;
181 else if (t > 0xffff)
182 t = 0xffff;
183 else
184 t--;
185
186 return adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, t);
187}
188
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200189static int __adis16136_get_freq(struct adis16136 *adis16136, unsigned int *freq)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000190{
191 uint16_t t;
192 int ret;
193
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200194 ret = __adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, &t);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +0200195 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000196 return ret;
197
198 *freq = 32768 / (t + 1);
199
200 return 0;
201}
202
203static ssize_t adis16136_write_frequency(struct device *dev,
204 struct device_attribute *attr, const char *buf, size_t len)
205{
206 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
207 struct adis16136 *adis16136 = iio_priv(indio_dev);
Dan Carpenter12660132012-11-27 07:24:00 +0000208 unsigned int val;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000209 int ret;
210
Dan Carpenter12660132012-11-27 07:24:00 +0000211 ret = kstrtouint(buf, 10, &val);
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000212 if (ret)
213 return ret;
214
215 if (val == 0)
216 return -EINVAL;
217
218 ret = adis16136_set_freq(adis16136, val);
219
220 return ret ? ret : len;
221}
222
223static ssize_t adis16136_read_frequency(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
226 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
227 struct adis16136 *adis16136 = iio_priv(indio_dev);
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200228 struct mutex *slock = &adis16136->adis.state_lock;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000229 unsigned int freq;
230 int ret;
231
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200232 mutex_lock(slock);
233 ret = __adis16136_get_freq(adis16136, &freq);
234 mutex_unlock(slock);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +0200235 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000236 return ret;
237
238 return sprintf(buf, "%d\n", freq);
239}
240
241static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
242 adis16136_read_frequency,
243 adis16136_write_frequency);
244
245static const unsigned adis16136_3db_divisors[] = {
246 [0] = 2, /* Special case */
247 [1] = 6,
248 [2] = 12,
249 [3] = 25,
250 [4] = 50,
251 [5] = 100,
252 [6] = 200,
253 [7] = 200, /* Not a valid setting */
254};
255
256static int adis16136_set_filter(struct iio_dev *indio_dev, int val)
257{
258 struct adis16136 *adis16136 = iio_priv(indio_dev);
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200259 struct mutex *slock = &adis16136->adis.state_lock;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000260 unsigned int freq;
261 int i, ret;
262
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200263 mutex_lock(slock);
264 ret = __adis16136_get_freq(adis16136, &freq);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +0200265 if (ret)
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200266 goto out_unlock;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000267
268 for (i = ARRAY_SIZE(adis16136_3db_divisors) - 1; i >= 1; i--) {
269 if (freq / adis16136_3db_divisors[i] >= val)
270 break;
271 }
272
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200273 ret = __adis_write_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, i);
274out_unlock:
275 mutex_unlock(slock);
276
277 return ret;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000278}
279
280static int adis16136_get_filter(struct iio_dev *indio_dev, int *val)
281{
282 struct adis16136 *adis16136 = iio_priv(indio_dev);
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200283 struct mutex *slock = &adis16136->adis.state_lock;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000284 unsigned int freq;
285 uint16_t val16;
286 int ret;
287
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200288 mutex_lock(slock);
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000289
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200290 ret = __adis_read_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT,
291 &val16);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +0200292 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000293 goto err_unlock;
294
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200295 ret = __adis16136_get_freq(adis16136, &freq);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +0200296 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000297 goto err_unlock;
298
299 *val = freq / adis16136_3db_divisors[val16 & 0x07];
300
301err_unlock:
Alexandru Ardelean0aee99a12019-11-22 15:24:19 +0200302 mutex_unlock(slock);
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000303
304 return ret ? ret : IIO_VAL_INT;
305}
306
307static int adis16136_read_raw(struct iio_dev *indio_dev,
308 const struct iio_chan_spec *chan, int *val, int *val2, long info)
309{
310 struct adis16136 *adis16136 = iio_priv(indio_dev);
311 uint32_t val32;
312 int ret;
313
314 switch (info) {
315 case IIO_CHAN_INFO_RAW:
316 return adis_single_conversion(indio_dev, chan, 0, val);
317 case IIO_CHAN_INFO_SCALE:
318 switch (chan->type) {
319 case IIO_ANGL_VEL:
320 *val = adis16136->chip_info->precision;
321 *val2 = (adis16136->chip_info->fullscale << 16);
322 return IIO_VAL_FRACTIONAL;
323 case IIO_TEMP:
324 *val = 10;
325 *val2 = 697000; /* 0.010697 degree Celsius */
326 return IIO_VAL_INT_PLUS_MICRO;
327 default:
328 return -EINVAL;
329 }
330 case IIO_CHAN_INFO_CALIBBIAS:
331 ret = adis_read_reg_32(&adis16136->adis,
332 ADIS16136_REG_GYRO_OFF2, &val32);
Alexandru Ardelean26ba6db2019-11-01 11:34:56 +0200333 if (ret)
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000334 return ret;
335
336 *val = sign_extend32(val32, 31);
337
338 return IIO_VAL_INT;
339 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
340 return adis16136_get_filter(indio_dev, val);
341 default:
342 return -EINVAL;
343 }
344}
345
346static int adis16136_write_raw(struct iio_dev *indio_dev,
347 const struct iio_chan_spec *chan, int val, int val2, long info)
348{
349 struct adis16136 *adis16136 = iio_priv(indio_dev);
350
351 switch (info) {
352 case IIO_CHAN_INFO_CALIBBIAS:
353 return adis_write_reg_32(&adis16136->adis,
354 ADIS16136_REG_GYRO_OFF2, val);
355 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
356 return adis16136_set_filter(indio_dev, val);
357 default:
358 break;
359 }
360
361 return -EINVAL;
362}
363
364enum {
365 ADIS16136_SCAN_GYRO,
366 ADIS16136_SCAN_TEMP,
367};
368
369static const struct iio_chan_spec adis16136_channels[] = {
370 {
371 .type = IIO_ANGL_VEL,
372 .modified = 1,
373 .channel2 = IIO_MOD_X,
Jonathan Cameron606f9062013-02-27 19:29:52 +0000374 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
375 BIT(IIO_CHAN_INFO_CALIBBIAS) |
376 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
377 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
378
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000379 .address = ADIS16136_REG_GYRO_OUT2,
380 .scan_index = ADIS16136_SCAN_GYRO,
381 .scan_type = {
382 .sign = 's',
383 .realbits = 32,
384 .storagebits = 32,
385 .endianness = IIO_BE,
386 },
387 }, {
388 .type = IIO_TEMP,
389 .indexed = 1,
390 .channel = 0,
Jonathan Cameron606f9062013-02-27 19:29:52 +0000391 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
392 BIT(IIO_CHAN_INFO_SCALE),
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000393 .address = ADIS16136_REG_TEMP_OUT,
394 .scan_index = ADIS16136_SCAN_TEMP,
395 .scan_type = {
396 .sign = 's',
397 .realbits = 16,
398 .storagebits = 16,
399 .endianness = IIO_BE,
400 },
401 },
402 IIO_CHAN_SOFT_TIMESTAMP(2),
403};
404
405static struct attribute *adis16136_attributes[] = {
406 &iio_dev_attr_sampling_frequency.dev_attr.attr,
407 NULL
408};
409
410static const struct attribute_group adis16136_attribute_group = {
411 .attrs = adis16136_attributes,
412};
413
414static const struct iio_info adis16136_info = {
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000415 .attrs = &adis16136_attribute_group,
416 .read_raw = &adis16136_read_raw,
417 .write_raw = &adis16136_write_raw,
418 .update_scan_mode = adis_update_scan_mode,
419 .debugfs_reg_access = adis_debugfs_reg_access,
420};
421
422static int adis16136_stop_device(struct iio_dev *indio_dev)
423{
424 struct adis16136 *adis16136 = iio_priv(indio_dev);
425 int ret;
426
427 ret = adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SLP_CTRL, 0xff);
428 if (ret)
429 dev_err(&indio_dev->dev,
430 "Could not power down device: %d\n", ret);
431
432 return ret;
433}
434
435static int adis16136_initial_setup(struct iio_dev *indio_dev)
436{
437 struct adis16136 *adis16136 = iio_priv(indio_dev);
438 unsigned int device_id;
439 uint16_t prod_id;
440 int ret;
441
442 ret = adis_initial_startup(&adis16136->adis);
443 if (ret)
444 return ret;
445
446 ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID,
447 &prod_id);
448 if (ret)
449 return ret;
450
Ioana Ciorneia106b472015-11-01 14:58:44 +0200451 ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
452 if (ret != 1)
453 return -EINVAL;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000454
455 if (prod_id != device_id)
456 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
457 device_id, prod_id);
458
459 return 0;
460}
461
462static const char * const adis16136_status_error_msgs[] = {
463 [ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL] = "Flash update failed",
464 [ADIS16136_DIAG_STAT_SPI_FAIL] = "SPI failure",
465 [ADIS16136_DIAG_STAT_SELF_TEST_FAIL] = "Self test error",
466 [ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL] = "Flash checksum error",
467};
468
Alexandru Ardeleane914cfd2020-02-10 15:26:00 +0200469#define ADIS16136_DATA(_timeouts) \
470{ \
471 .diag_stat_reg = ADIS16136_REG_DIAG_STAT, \
472 .glob_cmd_reg = ADIS16136_REG_GLOB_CMD, \
473 .msc_ctrl_reg = ADIS16136_REG_MSC_CTRL, \
Nuno Sáfdcf6bb2020-02-10 15:26:02 +0200474 .self_test_reg = ADIS16136_REG_MSC_CTRL, \
Alexandru Ardeleane914cfd2020-02-10 15:26:00 +0200475 .self_test_mask = ADIS16136_MSC_CTRL_SELF_TEST, \
476 .read_delay = 10, \
477 .write_delay = 10, \
478 .status_error_msgs = adis16136_status_error_msgs, \
479 .status_error_mask = BIT(ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL) | \
480 BIT(ADIS16136_DIAG_STAT_SPI_FAIL) | \
481 BIT(ADIS16136_DIAG_STAT_SELF_TEST_FAIL) | \
482 BIT(ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL), \
483 .timeouts = (_timeouts), \
484}
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000485
486enum adis16136_id {
487 ID_ADIS16133,
488 ID_ADIS16135,
489 ID_ADIS16136,
Lars-Peter Clausen5450c362015-08-05 15:38:21 +0200490 ID_ADIS16137,
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000491};
492
Nuno Sá380b1072020-01-07 13:17:04 +0200493static const struct adis_timeout adis16133_timeouts = {
494 .reset_ms = 75,
495 .sw_reset_ms = 75,
496 .self_test_ms = 50,
497};
498
499static const struct adis_timeout adis16136_timeouts = {
500 .reset_ms = 128,
501 .sw_reset_ms = 75,
502 .self_test_ms = 245,
503};
504
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000505static const struct adis16136_chip_info adis16136_chip_info[] = {
506 [ID_ADIS16133] = {
507 .precision = IIO_DEGREE_TO_RAD(1200),
508 .fullscale = 24000,
Alexandru Ardeleane914cfd2020-02-10 15:26:00 +0200509 .adis_data = ADIS16136_DATA(&adis16133_timeouts),
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000510 },
511 [ID_ADIS16135] = {
512 .precision = IIO_DEGREE_TO_RAD(300),
513 .fullscale = 24000,
Alexandru Ardeleane914cfd2020-02-10 15:26:00 +0200514 .adis_data = ADIS16136_DATA(&adis16133_timeouts),
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000515 },
516 [ID_ADIS16136] = {
517 .precision = IIO_DEGREE_TO_RAD(450),
518 .fullscale = 24623,
Alexandru Ardeleane914cfd2020-02-10 15:26:00 +0200519 .adis_data = ADIS16136_DATA(&adis16136_timeouts),
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000520 },
Lars-Peter Clausen5450c362015-08-05 15:38:21 +0200521 [ID_ADIS16137] = {
522 .precision = IIO_DEGREE_TO_RAD(1000),
523 .fullscale = 24609,
Alexandru Ardeleane914cfd2020-02-10 15:26:00 +0200524 .adis_data = ADIS16136_DATA(&adis16136_timeouts),
Lars-Peter Clausen5450c362015-08-05 15:38:21 +0200525 },
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000526};
527
528static int adis16136_probe(struct spi_device *spi)
529{
530 const struct spi_device_id *id = spi_get_device_id(spi);
531 struct adis16136 *adis16136;
532 struct iio_dev *indio_dev;
Nuno Sá380b1072020-01-07 13:17:04 +0200533 const struct adis_data *adis16136_data;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000534 int ret;
535
Sachin Kamatc0ca6d32013-08-13 07:34:00 +0100536 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis16136));
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000537 if (indio_dev == NULL)
538 return -ENOMEM;
539
540 spi_set_drvdata(spi, indio_dev);
541
542 adis16136 = iio_priv(indio_dev);
543
544 adis16136->chip_info = &adis16136_chip_info[id->driver_data];
545 indio_dev->dev.parent = &spi->dev;
546 indio_dev->name = spi_get_device_id(spi)->name;
547 indio_dev->channels = adis16136_channels;
548 indio_dev->num_channels = ARRAY_SIZE(adis16136_channels);
549 indio_dev->info = &adis16136_info;
550 indio_dev->modes = INDIO_DIRECT_MODE;
551
Alexandru Ardeleane914cfd2020-02-10 15:26:00 +0200552 adis16136_data = &adis16136->chip_info->adis_data;
Nuno Sá380b1072020-01-07 13:17:04 +0200553
554 ret = adis_init(&adis16136->adis, indio_dev, spi, adis16136_data);
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000555 if (ret)
Sachin Kamatc0ca6d32013-08-13 07:34:00 +0100556 return ret;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000557
558 ret = adis_setup_buffer_and_trigger(&adis16136->adis, indio_dev, NULL);
559 if (ret)
Sachin Kamatc0ca6d32013-08-13 07:34:00 +0100560 return ret;
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000561
562 ret = adis16136_initial_setup(indio_dev);
563 if (ret)
564 goto error_cleanup_buffer;
565
566 ret = iio_device_register(indio_dev);
567 if (ret)
568 goto error_stop_device;
569
570 adis16136_debugfs_init(indio_dev);
571
572 return 0;
573
574error_stop_device:
575 adis16136_stop_device(indio_dev);
576error_cleanup_buffer:
577 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev);
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000578 return ret;
579}
580
581static int adis16136_remove(struct spi_device *spi)
582{
583 struct iio_dev *indio_dev = spi_get_drvdata(spi);
584 struct adis16136 *adis16136 = iio_priv(indio_dev);
585
586 iio_device_unregister(indio_dev);
587 adis16136_stop_device(indio_dev);
588
589 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev);
590
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000591 return 0;
592}
593
594static const struct spi_device_id adis16136_ids[] = {
595 { "adis16133", ID_ADIS16133 },
596 { "adis16135", ID_ADIS16135 },
597 { "adis16136", ID_ADIS16136 },
Lars-Peter Clausen5450c362015-08-05 15:38:21 +0200598 { "adis16137", ID_ADIS16137 },
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000599 { }
600};
601MODULE_DEVICE_TABLE(spi, adis16136_ids);
602
603static struct spi_driver adis16136_driver = {
604 .driver = {
605 .name = "adis16136",
Lars-Peter Clausen9caed0d2012-11-20 13:36:00 +0000606 },
607 .id_table = adis16136_ids,
608 .probe = adis16136_probe,
609 .remove = adis16136_remove,
610};
611module_spi_driver(adis16136_driver);
612
613MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
614MODULE_DESCRIPTION("Analog Devices ADIS16133/ADIS16135/ADIS16136 gyroscope driver");
615MODULE_LICENSE("GPL v2");