2 * ADIS16133/ADIS16135/ADIS16136 gyroscope driver
4 * Copyright 2012 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 * Licensed under the GPL-2.
10 #include <linux/interrupt.h>
11 #include <linux/delay.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/module.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/imu/adis.h>
25 #include <linux/debugfs.h>
27 #define ADIS16136_REG_FLASH_CNT 0x00
28 #define ADIS16136_REG_TEMP_OUT 0x02
29 #define ADIS16136_REG_GYRO_OUT2 0x04
30 #define ADIS16136_REG_GYRO_OUT 0x06
31 #define ADIS16136_REG_GYRO_OFF2 0x08
32 #define ADIS16136_REG_GYRO_OFF 0x0A
33 #define ADIS16136_REG_ALM_MAG1 0x10
34 #define ADIS16136_REG_ALM_MAG2 0x12
35 #define ADIS16136_REG_ALM_SAMPL1 0x14
36 #define ADIS16136_REG_ALM_SAMPL2 0x16
37 #define ADIS16136_REG_ALM_CTRL 0x18
38 #define ADIS16136_REG_GPIO_CTRL 0x1A
39 #define ADIS16136_REG_MSC_CTRL 0x1C
40 #define ADIS16136_REG_SMPL_PRD 0x1E
41 #define ADIS16136_REG_AVG_CNT 0x20
42 #define ADIS16136_REG_DEC_RATE 0x22
43 #define ADIS16136_REG_SLP_CTRL 0x24
44 #define ADIS16136_REG_DIAG_STAT 0x26
45 #define ADIS16136_REG_GLOB_CMD 0x28
46 #define ADIS16136_REG_LOT1 0x32
47 #define ADIS16136_REG_LOT2 0x34
48 #define ADIS16136_REG_LOT3 0x36
49 #define ADIS16136_REG_PROD_ID 0x38
50 #define ADIS16136_REG_SERIAL_NUM 0x3A
52 #define ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL 2
53 #define ADIS16136_DIAG_STAT_SPI_FAIL 3
54 #define ADIS16136_DIAG_STAT_SELF_TEST_FAIL 5
55 #define ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL 6
57 #define ADIS16136_MSC_CTRL_MEMORY_TEST BIT(11)
58 #define ADIS16136_MSC_CTRL_SELF_TEST BIT(10)
60 struct adis16136_chip_info
{
61 unsigned int precision
;
62 unsigned int fullscale
;
66 const struct adis16136_chip_info
*chip_info
;
71 #ifdef CONFIG_DEBUG_FS
73 static ssize_t
adis16136_show_serial(struct file
*file
,
74 char __user
*userbuf
, size_t count
, loff_t
*ppos
)
76 struct adis16136
*adis16136
= file
->private_data
;
77 uint16_t lot1
, lot2
, lot3
, serial
;
82 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_SERIAL_NUM
,
87 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_LOT1
, &lot1
);
91 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_LOT2
, &lot2
);
95 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_LOT3
, &lot3
);
99 len
= snprintf(buf
, sizeof(buf
), "%.4x%.4x%.4x-%.4x\n", lot1
, lot2
,
102 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
105 static const struct file_operations adis16136_serial_fops
= {
107 .read
= adis16136_show_serial
,
108 .llseek
= default_llseek
,
109 .owner
= THIS_MODULE
,
112 static int adis16136_show_product_id(void *arg
, u64
*val
)
114 struct adis16136
*adis16136
= arg
;
118 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_PROD_ID
,
127 DEFINE_SIMPLE_ATTRIBUTE(adis16136_product_id_fops
,
128 adis16136_show_product_id
, NULL
, "%llu\n");
130 static int adis16136_show_flash_count(void *arg
, u64
*val
)
132 struct adis16136
*adis16136
= arg
;
133 uint16_t flash_count
;
136 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_FLASH_CNT
,
145 DEFINE_SIMPLE_ATTRIBUTE(adis16136_flash_count_fops
,
146 adis16136_show_flash_count
, NULL
, "%lld\n");
148 static int adis16136_debugfs_init(struct iio_dev
*indio_dev
)
150 struct adis16136
*adis16136
= iio_priv(indio_dev
);
152 debugfs_create_file("serial_number", 0400, indio_dev
->debugfs_dentry
,
153 adis16136
, &adis16136_serial_fops
);
154 debugfs_create_file("product_id", 0400, indio_dev
->debugfs_dentry
,
155 adis16136
, &adis16136_product_id_fops
);
156 debugfs_create_file("flash_count", 0400, indio_dev
->debugfs_dentry
,
157 adis16136
, &adis16136_flash_count_fops
);
164 static int adis16136_debugfs_init(struct iio_dev
*indio_dev
)
171 static int adis16136_set_freq(struct adis16136
*adis16136
, unsigned int freq
)
183 return adis_write_reg_16(&adis16136
->adis
, ADIS16136_REG_SMPL_PRD
, t
);
186 static int adis16136_get_freq(struct adis16136
*adis16136
, unsigned int *freq
)
191 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_SMPL_PRD
, &t
);
195 *freq
= 32768 / (t
+ 1);
200 static ssize_t
adis16136_write_frequency(struct device
*dev
,
201 struct device_attribute
*attr
, const char *buf
, size_t len
)
203 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
204 struct adis16136
*adis16136
= iio_priv(indio_dev
);
208 ret
= kstrtouint(buf
, 10, &val
);
215 ret
= adis16136_set_freq(adis16136
, val
);
217 return ret
? ret
: len
;
220 static ssize_t
adis16136_read_frequency(struct device
*dev
,
221 struct device_attribute
*attr
, char *buf
)
223 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
224 struct adis16136
*adis16136
= iio_priv(indio_dev
);
228 ret
= adis16136_get_freq(adis16136
, &freq
);
232 return sprintf(buf
, "%d\n", freq
);
235 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
236 adis16136_read_frequency
,
237 adis16136_write_frequency
);
239 static const unsigned adis16136_3db_divisors
[] = {
240 [0] = 2, /* Special case */
247 [7] = 200, /* Not a valid setting */
250 static int adis16136_set_filter(struct iio_dev
*indio_dev
, int val
)
252 struct adis16136
*adis16136
= iio_priv(indio_dev
);
256 ret
= adis16136_get_freq(adis16136
, &freq
);
260 for (i
= ARRAY_SIZE(adis16136_3db_divisors
) - 1; i
>= 1; i
--) {
261 if (freq
/ adis16136_3db_divisors
[i
] >= val
)
265 return adis_write_reg_16(&adis16136
->adis
, ADIS16136_REG_AVG_CNT
, i
);
268 static int adis16136_get_filter(struct iio_dev
*indio_dev
, int *val
)
270 struct adis16136
*adis16136
= iio_priv(indio_dev
);
275 mutex_lock(&indio_dev
->mlock
);
277 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_AVG_CNT
, &val16
);
281 ret
= adis16136_get_freq(adis16136
, &freq
);
285 *val
= freq
/ adis16136_3db_divisors
[val16
& 0x07];
288 mutex_unlock(&indio_dev
->mlock
);
290 return ret
? ret
: IIO_VAL_INT
;
293 static int adis16136_read_raw(struct iio_dev
*indio_dev
,
294 const struct iio_chan_spec
*chan
, int *val
, int *val2
, long info
)
296 struct adis16136
*adis16136
= iio_priv(indio_dev
);
301 case IIO_CHAN_INFO_RAW
:
302 return adis_single_conversion(indio_dev
, chan
, 0, val
);
303 case IIO_CHAN_INFO_SCALE
:
304 switch (chan
->type
) {
306 *val
= adis16136
->chip_info
->precision
;
307 *val2
= (adis16136
->chip_info
->fullscale
<< 16);
308 return IIO_VAL_FRACTIONAL
;
311 *val2
= 697000; /* 0.010697 degree Celsius */
312 return IIO_VAL_INT_PLUS_MICRO
;
316 case IIO_CHAN_INFO_CALIBBIAS
:
317 ret
= adis_read_reg_32(&adis16136
->adis
,
318 ADIS16136_REG_GYRO_OFF2
, &val32
);
322 *val
= sign_extend32(val32
, 31);
325 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
326 return adis16136_get_filter(indio_dev
, val
);
332 static int adis16136_write_raw(struct iio_dev
*indio_dev
,
333 const struct iio_chan_spec
*chan
, int val
, int val2
, long info
)
335 struct adis16136
*adis16136
= iio_priv(indio_dev
);
338 case IIO_CHAN_INFO_CALIBBIAS
:
339 return adis_write_reg_32(&adis16136
->adis
,
340 ADIS16136_REG_GYRO_OFF2
, val
);
341 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
342 return adis16136_set_filter(indio_dev
, val
);
355 static const struct iio_chan_spec adis16136_channels
[] = {
357 .type
= IIO_ANGL_VEL
,
359 .channel2
= IIO_MOD_X
,
360 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
361 BIT(IIO_CHAN_INFO_CALIBBIAS
) |
362 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
),
363 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
365 .address
= ADIS16136_REG_GYRO_OUT2
,
366 .scan_index
= ADIS16136_SCAN_GYRO
,
371 .endianness
= IIO_BE
,
377 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
378 BIT(IIO_CHAN_INFO_SCALE
),
379 .address
= ADIS16136_REG_TEMP_OUT
,
380 .scan_index
= ADIS16136_SCAN_TEMP
,
385 .endianness
= IIO_BE
,
388 IIO_CHAN_SOFT_TIMESTAMP(2),
391 static struct attribute
*adis16136_attributes
[] = {
392 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
396 static const struct attribute_group adis16136_attribute_group
= {
397 .attrs
= adis16136_attributes
,
400 static const struct iio_info adis16136_info
= {
401 .driver_module
= THIS_MODULE
,
402 .attrs
= &adis16136_attribute_group
,
403 .read_raw
= &adis16136_read_raw
,
404 .write_raw
= &adis16136_write_raw
,
405 .update_scan_mode
= adis_update_scan_mode
,
406 .debugfs_reg_access
= adis_debugfs_reg_access
,
409 static int adis16136_stop_device(struct iio_dev
*indio_dev
)
411 struct adis16136
*adis16136
= iio_priv(indio_dev
);
414 ret
= adis_write_reg_16(&adis16136
->adis
, ADIS16136_REG_SLP_CTRL
, 0xff);
416 dev_err(&indio_dev
->dev
,
417 "Could not power down device: %d\n", ret
);
422 static int adis16136_initial_setup(struct iio_dev
*indio_dev
)
424 struct adis16136
*adis16136
= iio_priv(indio_dev
);
425 unsigned int device_id
;
429 ret
= adis_initial_startup(&adis16136
->adis
);
433 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_PROD_ID
,
438 ret
= sscanf(indio_dev
->name
, "adis%u\n", &device_id
);
442 if (prod_id
!= device_id
)
443 dev_warn(&indio_dev
->dev
, "Device ID(%u) and product ID(%u) do not match.",
449 static const char * const adis16136_status_error_msgs
[] = {
450 [ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL
] = "Flash update failed",
451 [ADIS16136_DIAG_STAT_SPI_FAIL
] = "SPI failure",
452 [ADIS16136_DIAG_STAT_SELF_TEST_FAIL
] = "Self test error",
453 [ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL
] = "Flash checksum error",
456 static const struct adis_data adis16136_data
= {
457 .diag_stat_reg
= ADIS16136_REG_DIAG_STAT
,
458 .glob_cmd_reg
= ADIS16136_REG_GLOB_CMD
,
459 .msc_ctrl_reg
= ADIS16136_REG_MSC_CTRL
,
461 .self_test_mask
= ADIS16136_MSC_CTRL_SELF_TEST
,
467 .status_error_msgs
= adis16136_status_error_msgs
,
468 .status_error_mask
= BIT(ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL
) |
469 BIT(ADIS16136_DIAG_STAT_SPI_FAIL
) |
470 BIT(ADIS16136_DIAG_STAT_SELF_TEST_FAIL
) |
471 BIT(ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL
),
481 static const struct adis16136_chip_info adis16136_chip_info
[] = {
483 .precision
= IIO_DEGREE_TO_RAD(1200),
487 .precision
= IIO_DEGREE_TO_RAD(300),
491 .precision
= IIO_DEGREE_TO_RAD(450),
495 .precision
= IIO_DEGREE_TO_RAD(1000),
500 static int adis16136_probe(struct spi_device
*spi
)
502 const struct spi_device_id
*id
= spi_get_device_id(spi
);
503 struct adis16136
*adis16136
;
504 struct iio_dev
*indio_dev
;
507 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adis16136
));
508 if (indio_dev
== NULL
)
511 spi_set_drvdata(spi
, indio_dev
);
513 adis16136
= iio_priv(indio_dev
);
515 adis16136
->chip_info
= &adis16136_chip_info
[id
->driver_data
];
516 indio_dev
->dev
.parent
= &spi
->dev
;
517 indio_dev
->name
= spi_get_device_id(spi
)->name
;
518 indio_dev
->channels
= adis16136_channels
;
519 indio_dev
->num_channels
= ARRAY_SIZE(adis16136_channels
);
520 indio_dev
->info
= &adis16136_info
;
521 indio_dev
->modes
= INDIO_DIRECT_MODE
;
523 ret
= adis_init(&adis16136
->adis
, indio_dev
, spi
, &adis16136_data
);
527 ret
= adis_setup_buffer_and_trigger(&adis16136
->adis
, indio_dev
, NULL
);
531 ret
= adis16136_initial_setup(indio_dev
);
533 goto error_cleanup_buffer
;
535 ret
= iio_device_register(indio_dev
);
537 goto error_stop_device
;
539 adis16136_debugfs_init(indio_dev
);
544 adis16136_stop_device(indio_dev
);
545 error_cleanup_buffer
:
546 adis_cleanup_buffer_and_trigger(&adis16136
->adis
, indio_dev
);
550 static int adis16136_remove(struct spi_device
*spi
)
552 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
553 struct adis16136
*adis16136
= iio_priv(indio_dev
);
555 iio_device_unregister(indio_dev
);
556 adis16136_stop_device(indio_dev
);
558 adis_cleanup_buffer_and_trigger(&adis16136
->adis
, indio_dev
);
563 static const struct spi_device_id adis16136_ids
[] = {
564 { "adis16133", ID_ADIS16133
},
565 { "adis16135", ID_ADIS16135
},
566 { "adis16136", ID_ADIS16136
},
567 { "adis16137", ID_ADIS16137
},
570 MODULE_DEVICE_TABLE(spi
, adis16136_ids
);
572 static struct spi_driver adis16136_driver
= {
576 .id_table
= adis16136_ids
,
577 .probe
= adis16136_probe
,
578 .remove
= adis16136_remove
,
580 module_spi_driver(adis16136_driver
);
582 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
583 MODULE_DESCRIPTION("Analog Devices ADIS16133/ADIS16135/ADIS16136 gyroscope driver");
584 MODULE_LICENSE("GPL v2");