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/iio/iio.h>
26 #include <linux/debugfs.h>
28 #define ADIS16136_REG_FLASH_CNT 0x00
29 #define ADIS16136_REG_TEMP_OUT 0x02
30 #define ADIS16136_REG_GYRO_OUT2 0x04
31 #define ADIS16136_REG_GYRO_OUT 0x06
32 #define ADIS16136_REG_GYRO_OFF2 0x08
33 #define ADIS16136_REG_GYRO_OFF 0x0A
34 #define ADIS16136_REG_ALM_MAG1 0x10
35 #define ADIS16136_REG_ALM_MAG2 0x12
36 #define ADIS16136_REG_ALM_SAMPL1 0x14
37 #define ADIS16136_REG_ALM_SAMPL2 0x16
38 #define ADIS16136_REG_ALM_CTRL 0x18
39 #define ADIS16136_REG_GPIO_CTRL 0x1A
40 #define ADIS16136_REG_MSC_CTRL 0x1C
41 #define ADIS16136_REG_SMPL_PRD 0x1E
42 #define ADIS16136_REG_AVG_CNT 0x20
43 #define ADIS16136_REG_DEC_RATE 0x22
44 #define ADIS16136_REG_SLP_CTRL 0x24
45 #define ADIS16136_REG_DIAG_STAT 0x26
46 #define ADIS16136_REG_GLOB_CMD 0x28
47 #define ADIS16136_REG_LOT1 0x32
48 #define ADIS16136_REG_LOT2 0x34
49 #define ADIS16136_REG_LOT3 0x36
50 #define ADIS16136_REG_PROD_ID 0x38
51 #define ADIS16136_REG_SERIAL_NUM 0x3A
53 #define ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL 2
54 #define ADIS16136_DIAG_STAT_SPI_FAIL 3
55 #define ADIS16136_DIAG_STAT_SELF_TEST_FAIL 5
56 #define ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL 6
58 #define ADIS16136_MSC_CTRL_MEMORY_TEST BIT(11)
59 #define ADIS16136_MSC_CTRL_SELF_TEST BIT(10)
61 struct adis16136_chip_info
{
62 unsigned int precision
;
63 unsigned int fullscale
;
67 const struct adis16136_chip_info
*chip_info
;
72 #ifdef CONFIG_DEBUG_FS
74 static ssize_t
adis16136_show_serial(struct file
*file
,
75 char __user
*userbuf
, size_t count
, loff_t
*ppos
)
77 struct adis16136
*adis16136
= file
->private_data
;
78 uint16_t lot1
, lot2
, lot3
, serial
;
83 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_SERIAL_NUM
,
88 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_LOT1
, &lot1
);
92 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_LOT2
, &lot2
);
96 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_LOT3
, &lot3
);
100 len
= snprintf(buf
, sizeof(buf
), "%.4x%.4x%.4x-%.4x\n", lot1
, lot2
,
103 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
106 static const struct file_operations adis16136_serial_fops
= {
108 .read
= adis16136_show_serial
,
109 .llseek
= default_llseek
,
110 .owner
= THIS_MODULE
,
113 static int adis16136_show_product_id(void *arg
, u64
*val
)
115 struct adis16136
*adis16136
= arg
;
119 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_PROD_ID
,
128 DEFINE_SIMPLE_ATTRIBUTE(adis16136_product_id_fops
,
129 adis16136_show_product_id
, NULL
, "%llu\n");
131 static int adis16136_show_flash_count(void *arg
, u64
*val
)
133 struct adis16136
*adis16136
= arg
;
134 uint16_t flash_count
;
137 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_FLASH_CNT
,
146 DEFINE_SIMPLE_ATTRIBUTE(adis16136_flash_count_fops
,
147 adis16136_show_flash_count
, NULL
, "%lld\n");
149 static int adis16136_debugfs_init(struct iio_dev
*indio_dev
)
151 struct adis16136
*adis16136
= iio_priv(indio_dev
);
153 debugfs_create_file("serial_number", 0400, indio_dev
->debugfs_dentry
,
154 adis16136
, &adis16136_serial_fops
);
155 debugfs_create_file("product_id", 0400, indio_dev
->debugfs_dentry
,
156 adis16136
, &adis16136_product_id_fops
);
157 debugfs_create_file("flash_count", 0400, indio_dev
->debugfs_dentry
,
158 adis16136
, &adis16136_flash_count_fops
);
165 static int adis16136_debugfs_init(struct iio_dev
*indio_dev
)
172 static int adis16136_set_freq(struct adis16136
*adis16136
, unsigned int freq
)
184 return adis_write_reg_16(&adis16136
->adis
, ADIS16136_REG_SMPL_PRD
, t
);
187 static int adis16136_get_freq(struct adis16136
*adis16136
, unsigned int *freq
)
192 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_SMPL_PRD
, &t
);
196 *freq
= 32768 / (t
+ 1);
201 static ssize_t
adis16136_write_frequency(struct device
*dev
,
202 struct device_attribute
*attr
, const char *buf
, size_t len
)
204 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
205 struct adis16136
*adis16136
= iio_priv(indio_dev
);
209 ret
= kstrtouint(buf
, 10, &val
);
216 ret
= adis16136_set_freq(adis16136
, val
);
218 return ret
? ret
: len
;
221 static ssize_t
adis16136_read_frequency(struct device
*dev
,
222 struct device_attribute
*attr
, char *buf
)
224 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
225 struct adis16136
*adis16136
= iio_priv(indio_dev
);
229 ret
= adis16136_get_freq(adis16136
, &freq
);
233 return sprintf(buf
, "%d\n", freq
);
236 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
237 adis16136_read_frequency
,
238 adis16136_write_frequency
);
240 static const unsigned adis16136_3db_divisors
[] = {
241 [0] = 2, /* Special case */
248 [7] = 200, /* Not a valid setting */
251 static int adis16136_set_filter(struct iio_dev
*indio_dev
, int val
)
253 struct adis16136
*adis16136
= iio_priv(indio_dev
);
257 ret
= adis16136_get_freq(adis16136
, &freq
);
261 for (i
= ARRAY_SIZE(adis16136_3db_divisors
) - 1; i
>= 1; i
--) {
262 if (freq
/ adis16136_3db_divisors
[i
] >= val
)
266 return adis_write_reg_16(&adis16136
->adis
, ADIS16136_REG_AVG_CNT
, i
);
269 static int adis16136_get_filter(struct iio_dev
*indio_dev
, int *val
)
271 struct adis16136
*adis16136
= iio_priv(indio_dev
);
276 mutex_lock(&indio_dev
->mlock
);
278 ret
= adis_read_reg_16(&adis16136
->adis
, ADIS16136_REG_AVG_CNT
, &val16
);
282 ret
= adis16136_get_freq(adis16136
, &freq
);
286 *val
= freq
/ adis16136_3db_divisors
[val16
& 0x07];
289 mutex_unlock(&indio_dev
->mlock
);
291 return ret
? ret
: IIO_VAL_INT
;
294 static int adis16136_read_raw(struct iio_dev
*indio_dev
,
295 const struct iio_chan_spec
*chan
, int *val
, int *val2
, long info
)
297 struct adis16136
*adis16136
= iio_priv(indio_dev
);
302 case IIO_CHAN_INFO_RAW
:
303 return adis_single_conversion(indio_dev
, chan
, 0, val
);
304 case IIO_CHAN_INFO_SCALE
:
305 switch (chan
->type
) {
307 *val
= adis16136
->chip_info
->precision
;
308 *val2
= (adis16136
->chip_info
->fullscale
<< 16);
309 return IIO_VAL_FRACTIONAL
;
312 *val2
= 697000; /* 0.010697 degree Celsius */
313 return IIO_VAL_INT_PLUS_MICRO
;
317 case IIO_CHAN_INFO_CALIBBIAS
:
318 ret
= adis_read_reg_32(&adis16136
->adis
,
319 ADIS16136_REG_GYRO_OFF2
, &val32
);
323 *val
= sign_extend32(val32
, 31);
326 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
327 return adis16136_get_filter(indio_dev
, val
);
333 static int adis16136_write_raw(struct iio_dev
*indio_dev
,
334 const struct iio_chan_spec
*chan
, int val
, int val2
, long info
)
336 struct adis16136
*adis16136
= iio_priv(indio_dev
);
339 case IIO_CHAN_INFO_CALIBBIAS
:
340 return adis_write_reg_32(&adis16136
->adis
,
341 ADIS16136_REG_GYRO_OFF2
, val
);
342 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
343 return adis16136_set_filter(indio_dev
, val
);
356 static const struct iio_chan_spec adis16136_channels
[] = {
358 .type
= IIO_ANGL_VEL
,
360 .channel2
= IIO_MOD_X
,
361 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
362 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT
|
363 IIO_CHAN_INFO_SCALE_SHARED_BIT
|
364 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SEPARATE_BIT
,
365 .address
= ADIS16136_REG_GYRO_OUT2
,
366 .scan_index
= ADIS16136_SCAN_GYRO
,
371 .endianness
= IIO_BE
,
377 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
378 IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
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 sscanf(indio_dev
->name
, "adis%u\n", &device_id
);
440 if (prod_id
!= device_id
)
441 dev_warn(&indio_dev
->dev
, "Device ID(%u) and product ID(%u) do not match.",
447 static const char * const adis16136_status_error_msgs
[] = {
448 [ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL
] = "Flash update failed",
449 [ADIS16136_DIAG_STAT_SPI_FAIL
] = "SPI failure",
450 [ADIS16136_DIAG_STAT_SELF_TEST_FAIL
] = "Self test error",
451 [ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL
] = "Flash checksum error",
454 static const struct adis_data adis16136_data
= {
455 .diag_stat_reg
= ADIS16136_REG_DIAG_STAT
,
456 .glob_cmd_reg
= ADIS16136_REG_GLOB_CMD
,
457 .msc_ctrl_reg
= ADIS16136_REG_MSC_CTRL
,
459 .self_test_mask
= ADIS16136_MSC_CTRL_SELF_TEST
,
465 .status_error_msgs
= adis16136_status_error_msgs
,
466 .status_error_mask
= BIT(ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL
) |
467 BIT(ADIS16136_DIAG_STAT_SPI_FAIL
) |
468 BIT(ADIS16136_DIAG_STAT_SELF_TEST_FAIL
) |
469 BIT(ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL
),
478 static const struct adis16136_chip_info adis16136_chip_info
[] = {
480 .precision
= IIO_DEGREE_TO_RAD(1200),
484 .precision
= IIO_DEGREE_TO_RAD(300),
488 .precision
= IIO_DEGREE_TO_RAD(450),
493 static int adis16136_probe(struct spi_device
*spi
)
495 const struct spi_device_id
*id
= spi_get_device_id(spi
);
496 struct adis16136
*adis16136
;
497 struct iio_dev
*indio_dev
;
500 indio_dev
= iio_device_alloc(sizeof(*adis16136
));
501 if (indio_dev
== NULL
)
504 spi_set_drvdata(spi
, indio_dev
);
506 adis16136
= iio_priv(indio_dev
);
508 adis16136
->chip_info
= &adis16136_chip_info
[id
->driver_data
];
509 indio_dev
->dev
.parent
= &spi
->dev
;
510 indio_dev
->name
= spi_get_device_id(spi
)->name
;
511 indio_dev
->channels
= adis16136_channels
;
512 indio_dev
->num_channels
= ARRAY_SIZE(adis16136_channels
);
513 indio_dev
->info
= &adis16136_info
;
514 indio_dev
->modes
= INDIO_DIRECT_MODE
;
516 ret
= adis_init(&adis16136
->adis
, indio_dev
, spi
, &adis16136_data
);
520 ret
= adis_setup_buffer_and_trigger(&adis16136
->adis
, indio_dev
, NULL
);
524 ret
= adis16136_initial_setup(indio_dev
);
526 goto error_cleanup_buffer
;
528 ret
= iio_device_register(indio_dev
);
530 goto error_stop_device
;
532 adis16136_debugfs_init(indio_dev
);
537 adis16136_stop_device(indio_dev
);
538 error_cleanup_buffer
:
539 adis_cleanup_buffer_and_trigger(&adis16136
->adis
, indio_dev
);
541 iio_device_free(indio_dev
);
545 static int adis16136_remove(struct spi_device
*spi
)
547 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
548 struct adis16136
*adis16136
= iio_priv(indio_dev
);
550 iio_device_unregister(indio_dev
);
551 adis16136_stop_device(indio_dev
);
553 adis_cleanup_buffer_and_trigger(&adis16136
->adis
, indio_dev
);
555 iio_device_free(indio_dev
);
560 static const struct spi_device_id adis16136_ids
[] = {
561 { "adis16133", ID_ADIS16133
},
562 { "adis16135", ID_ADIS16135
},
563 { "adis16136", ID_ADIS16136
},
566 MODULE_DEVICE_TABLE(spi
, adis16136_ids
);
568 static struct spi_driver adis16136_driver
= {
571 .owner
= THIS_MODULE
,
573 .id_table
= adis16136_ids
,
574 .probe
= adis16136_probe
,
575 .remove
= adis16136_remove
,
577 module_spi_driver(adis16136_driver
);
579 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
580 MODULE_DESCRIPTION("Analog Devices ADIS16133/ADIS16135/ADIS16136 gyroscope driver");
581 MODULE_LICENSE("GPL v2");