2 * ADIS16260/ADIS16265 Programmable Digital Gyroscope Sensor Driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/gpio.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/list.h>
23 #include "../ring_generic.h"
25 #include "adis16260.h"
27 #define DRIVER_NAME "adis16260"
29 static int adis16260_check_status(struct iio_dev
*indio_dev
);
32 * adis16260_spi_write_reg_8() - write single byte to a register
33 * @indio_dev: iio_dev for the device
34 * @reg_address: the address of the register to be written
35 * @val: the value to write
37 static int adis16260_spi_write_reg_8(struct iio_dev
*indio_dev
,
42 struct adis16260_state
*st
= iio_priv(indio_dev
);
44 mutex_lock(&st
->buf_lock
);
45 st
->tx
[0] = ADIS16260_WRITE_REG(reg_address
);
48 ret
= spi_write(st
->us
, st
->tx
, 2);
49 mutex_unlock(&st
->buf_lock
);
55 * adis16260_spi_write_reg_16() - write 2 bytes to a pair of registers
56 * @indio_dev: iio_dev for the device
57 * @reg_address: the address of the lower of the two registers. Second register
58 * is assumed to have address one greater.
59 * @val: value to be written
61 static int adis16260_spi_write_reg_16(struct iio_dev
*indio_dev
,
66 struct spi_message msg
;
67 struct adis16260_state
*st
= iio_priv(indio_dev
);
68 struct spi_transfer xfers
[] = {
83 mutex_lock(&st
->buf_lock
);
84 st
->tx
[0] = ADIS16260_WRITE_REG(lower_reg_address
);
85 st
->tx
[1] = value
& 0xFF;
86 st
->tx
[2] = ADIS16260_WRITE_REG(lower_reg_address
+ 1);
87 st
->tx
[3] = (value
>> 8) & 0xFF;
89 spi_message_init(&msg
);
90 spi_message_add_tail(&xfers
[0], &msg
);
91 spi_message_add_tail(&xfers
[1], &msg
);
92 ret
= spi_sync(st
->us
, &msg
);
93 mutex_unlock(&st
->buf_lock
);
99 * adis16260_spi_read_reg_16() - read 2 bytes from a 16-bit register
100 * @indio_dev: iio_dev for the device
101 * @reg_address: the address of the lower of the two registers. Second register
102 * is assumed to have address one greater.
103 * @val: somewhere to pass back the value read
105 static int adis16260_spi_read_reg_16(struct iio_dev
*indio_dev
,
106 u8 lower_reg_address
,
109 struct spi_message msg
;
110 struct adis16260_state
*st
= iio_priv(indio_dev
);
112 struct spi_transfer xfers
[] = {
127 mutex_lock(&st
->buf_lock
);
128 st
->tx
[0] = ADIS16260_READ_REG(lower_reg_address
);
131 spi_message_init(&msg
);
132 spi_message_add_tail(&xfers
[0], &msg
);
133 spi_message_add_tail(&xfers
[1], &msg
);
134 ret
= spi_sync(st
->us
, &msg
);
136 dev_err(&st
->us
->dev
,
137 "problem when reading 16 bit register 0x%02X",
141 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
144 mutex_unlock(&st
->buf_lock
);
148 static ssize_t
adis16260_read_frequency_available(struct device
*dev
,
149 struct device_attribute
*attr
,
152 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
153 struct adis16260_state
*st
= iio_priv(indio_dev
);
154 if (spi_get_device_id(st
->us
)->driver_data
)
155 return sprintf(buf
, "%s\n", "0.129 ~ 256");
157 return sprintf(buf
, "%s\n", "256 2048");
160 static ssize_t
adis16260_read_frequency(struct device
*dev
,
161 struct device_attribute
*attr
,
164 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
165 struct adis16260_state
*st
= iio_priv(indio_dev
);
169 ret
= adis16260_spi_read_reg_16(indio_dev
,
175 if (spi_get_device_id(st
->us
)->driver_data
) /* If an adis16251 */
176 sps
= (t
& ADIS16260_SMPL_PRD_TIME_BASE
) ? 8 : 256;
178 sps
= (t
& ADIS16260_SMPL_PRD_TIME_BASE
) ? 66 : 2048;
179 sps
/= (t
& ADIS16260_SMPL_PRD_DIV_MASK
) + 1;
180 len
= sprintf(buf
, "%d SPS\n", sps
);
184 static ssize_t
adis16260_write_frequency(struct device
*dev
,
185 struct device_attribute
*attr
,
189 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
190 struct adis16260_state
*st
= iio_priv(indio_dev
);
195 ret
= strict_strtol(buf
, 10, &val
);
199 mutex_lock(&indio_dev
->mlock
);
200 if (spi_get_device_id(st
->us
)) {
204 t
&= ADIS16260_SMPL_PRD_DIV_MASK
;
209 t
&= ADIS16260_SMPL_PRD_DIV_MASK
;
211 if ((t
& ADIS16260_SMPL_PRD_DIV_MASK
) >= 0x0A)
212 st
->us
->max_speed_hz
= ADIS16260_SPI_SLOW
;
214 st
->us
->max_speed_hz
= ADIS16260_SPI_FAST
;
215 ret
= adis16260_spi_write_reg_8(indio_dev
,
219 mutex_unlock(&indio_dev
->mlock
);
221 return ret
? ret
: len
;
224 static int adis16260_reset(struct iio_dev
*indio_dev
)
227 ret
= adis16260_spi_write_reg_8(indio_dev
,
229 ADIS16260_GLOB_CMD_SW_RESET
);
231 dev_err(&indio_dev
->dev
, "problem resetting device");
236 static ssize_t
adis16260_write_reset(struct device
*dev
,
237 struct device_attribute
*attr
,
238 const char *buf
, size_t len
)
240 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
247 return adis16260_reset(indio_dev
);
252 int adis16260_set_irq(struct iio_dev
*indio_dev
, bool enable
)
256 ret
= adis16260_spi_read_reg_16(indio_dev
, ADIS16260_MSC_CTRL
, &msc
);
260 msc
|= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH
;
262 msc
|= ADIS16260_MSC_CTRL_DATA_RDY_EN
;
264 msc
&= ~ADIS16260_MSC_CTRL_DATA_RDY_EN
;
266 ret
= adis16260_spi_write_reg_16(indio_dev
, ADIS16260_MSC_CTRL
, msc
);
274 /* Power down the device */
275 static int adis16260_stop_device(struct iio_dev
*indio_dev
)
278 u16 val
= ADIS16260_SLP_CNT_POWER_OFF
;
280 ret
= adis16260_spi_write_reg_16(indio_dev
, ADIS16260_SLP_CNT
, val
);
282 dev_err(&indio_dev
->dev
, "problem with turning device off: SLP_CNT");
287 static int adis16260_self_test(struct iio_dev
*indio_dev
)
290 ret
= adis16260_spi_write_reg_16(indio_dev
,
292 ADIS16260_MSC_CTRL_MEM_TEST
);
294 dev_err(&indio_dev
->dev
, "problem starting self test");
298 adis16260_check_status(indio_dev
);
304 static int adis16260_check_status(struct iio_dev
*indio_dev
)
308 struct device
*dev
= &indio_dev
->dev
;
310 ret
= adis16260_spi_read_reg_16(indio_dev
,
315 dev_err(dev
, "Reading status failed\n");
319 if (status
& ADIS16260_DIAG_STAT_FLASH_CHK
)
320 dev_err(dev
, "Flash checksum error\n");
321 if (status
& ADIS16260_DIAG_STAT_SELF_TEST
)
322 dev_err(dev
, "Self test error\n");
323 if (status
& ADIS16260_DIAG_STAT_OVERFLOW
)
324 dev_err(dev
, "Sensor overrange\n");
325 if (status
& ADIS16260_DIAG_STAT_SPI_FAIL
)
326 dev_err(dev
, "SPI failure\n");
327 if (status
& ADIS16260_DIAG_STAT_FLASH_UPT
)
328 dev_err(dev
, "Flash update failed\n");
329 if (status
& ADIS16260_DIAG_STAT_POWER_HIGH
)
330 dev_err(dev
, "Power supply above 5.25V\n");
331 if (status
& ADIS16260_DIAG_STAT_POWER_LOW
)
332 dev_err(dev
, "Power supply below 4.75V\n");
338 static int adis16260_initial_setup(struct iio_dev
*indio_dev
)
341 struct device
*dev
= &indio_dev
->dev
;
344 ret
= adis16260_set_irq(indio_dev
, false);
346 dev_err(dev
, "disable irq failed");
351 ret
= adis16260_self_test(indio_dev
);
353 dev_err(dev
, "self test failure");
357 /* Read status register to check the result */
358 ret
= adis16260_check_status(indio_dev
);
360 adis16260_reset(indio_dev
);
361 dev_err(dev
, "device not playing ball -> reset");
362 msleep(ADIS16260_STARTUP_DELAY
);
363 ret
= adis16260_check_status(indio_dev
);
365 dev_err(dev
, "giving up");
374 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
375 adis16260_read_frequency
,
376 adis16260_write_frequency
);
378 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16260_write_reset
, 0);
380 static IIO_DEVICE_ATTR(sampling_frequency_available
,
381 S_IRUGO
, adis16260_read_frequency_available
, NULL
, 0);
383 enum adis16260_channel
{
390 #define ADIS16260_GYRO_CHANNEL_SET(axis, mod) \
391 struct iio_chan_spec adis16260_channels_##axis[] = { \
392 IIO_CHAN(IIO_GYRO, 1, 0, 0, NULL, 0, mod, \
393 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) | \
394 (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) | \
395 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
396 gyro, ADIS16260_SCAN_GYRO, \
397 IIO_ST('s', 14, 16, 0), 0), \
398 IIO_CHAN(IIO_ANGL, 1, 0, 0, NULL, 0, mod, \
400 angle, ADIS16260_SCAN_ANGL, \
401 IIO_ST('u', 14, 16, 0), 0), \
402 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0, \
403 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) | \
404 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
405 temp, ADIS16260_SCAN_TEMP, \
406 IIO_ST('u', 12, 16, 0), 0), \
407 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0, \
408 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
409 in_supply, ADIS16260_SCAN_SUPPLY, \
410 IIO_ST('u', 12, 16, 0), 0), \
411 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0, \
412 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
413 in_aux, ADIS16260_SCAN_AUX_ADC, \
414 IIO_ST('u', 12, 16, 0), 0), \
415 IIO_CHAN_SOFT_TIMESTAMP(5) \
418 static const ADIS16260_GYRO_CHANNEL_SET(x
, IIO_MOD_X
);
419 static const ADIS16260_GYRO_CHANNEL_SET(y
, IIO_MOD_Y
);
420 static const ADIS16260_GYRO_CHANNEL_SET(z
, IIO_MOD_Z
);
422 static const u8 adis16260_addresses
[5][3] = {
423 [gyro
] = { ADIS16260_GYRO_OUT
,
425 ADIS16260_GYRO_SCALE
},
426 [angle
] = { ADIS16260_ANGL_OUT
},
427 [in_supply
] = { ADIS16260_SUPPLY_OUT
},
428 [in_aux
] = { ADIS16260_AUX_ADC
},
429 [temp
] = { ADIS16260_TEMP_OUT
},
431 static int adis16260_read_raw(struct iio_dev
*indio_dev
,
432 struct iio_chan_spec
const *chan
,
436 struct adis16260_state
*st
= iio_priv(indio_dev
);
444 mutex_lock(&indio_dev
->mlock
);
445 addr
= adis16260_addresses
[chan
->address
][0];
446 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
448 mutex_unlock(&indio_dev
->mlock
);
452 if (val16
& ADIS16260_ERROR_ACTIVE
) {
453 ret
= adis16260_check_status(indio_dev
);
455 mutex_unlock(&indio_dev
->mlock
);
459 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
460 if (chan
->scan_type
.sign
== 's')
461 val16
= (s16
)(val16
<<
462 (16 - chan
->scan_type
.realbits
)) >>
463 (16 - chan
->scan_type
.realbits
);
465 mutex_unlock(&indio_dev
->mlock
);
467 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
468 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
469 switch (chan
->type
) {
472 if (spi_get_device_id(st
->us
)->driver_data
)
476 return IIO_VAL_INT_PLUS_MICRO
;
479 if (chan
->channel
== 0)
483 return IIO_VAL_INT_PLUS_MICRO
;
487 return IIO_VAL_INT_PLUS_MICRO
;
492 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
):
495 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
496 switch (chan
->type
) {
503 mutex_lock(&indio_dev
->mlock
);
504 addr
= adis16260_addresses
[chan
->address
][1];
505 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
507 mutex_unlock(&indio_dev
->mlock
);
510 val16
&= (1 << bits
) - 1;
511 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
513 mutex_unlock(&indio_dev
->mlock
);
515 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE
):
516 switch (chan
->type
) {
523 mutex_lock(&indio_dev
->mlock
);
524 addr
= adis16260_addresses
[chan
->address
][2];
525 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
527 mutex_unlock(&indio_dev
->mlock
);
530 *val
= (1 << bits
) - 1;
531 mutex_unlock(&indio_dev
->mlock
);
537 static int adis16260_write_raw(struct iio_dev
*indio_dev
,
538 struct iio_chan_spec
const *chan
,
547 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
548 val16
= val
& ((1 << bits
) - 1);
549 addr
= adis16260_addresses
[chan
->address
][1];
550 return adis16260_spi_write_reg_16(indio_dev
, addr
, val16
);
551 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE
):
552 val16
= val
& ((1 << bits
) - 1);
553 addr
= adis16260_addresses
[chan
->address
][2];
554 return adis16260_spi_write_reg_16(indio_dev
, addr
, val16
);
559 static struct attribute
*adis16260_attributes
[] = {
560 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
561 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
562 &iio_dev_attr_reset
.dev_attr
.attr
,
566 static const struct attribute_group adis16260_attribute_group
= {
567 .attrs
= adis16260_attributes
,
570 static const struct iio_info adis16260_info
= {
571 .attrs
= &adis16260_attribute_group
,
572 .read_raw
= &adis16260_read_raw
,
573 .write_raw
= &adis16260_write_raw
,
574 .driver_module
= THIS_MODULE
,
577 static int __devinit
adis16260_probe(struct spi_device
*spi
)
579 int ret
, regdone
= 0;
580 struct adis16260_platform_data
*pd
= spi
->dev
.platform_data
;
581 struct adis16260_state
*st
;
582 struct iio_dev
*indio_dev
;
584 /* setup the industrialio driver allocated elements */
585 indio_dev
= iio_allocate_device(sizeof(*st
));
586 if (indio_dev
== NULL
) {
590 st
= iio_priv(indio_dev
);
592 st
->negate
= pd
->negate
;
593 /* this is only used for removal purposes */
594 spi_set_drvdata(spi
, st
);
597 mutex_init(&st
->buf_lock
);
599 indio_dev
->name
= spi_get_device_id(st
->us
)->name
;
600 indio_dev
->dev
.parent
= &spi
->dev
;
601 indio_dev
->info
= &adis16260_info
;
602 indio_dev
->num_channels
603 = ARRAY_SIZE(adis16260_channels_x
);
604 if (pd
&& pd
->direction
)
605 switch (pd
->direction
) {
607 indio_dev
->channels
= adis16260_channels_x
;
610 indio_dev
->channels
= adis16260_channels_y
;
613 indio_dev
->channels
= adis16260_channels_z
;
619 indio_dev
->channels
= adis16260_channels_x
;
620 indio_dev
->num_channels
= ARRAY_SIZE(adis16260_channels_x
);
621 indio_dev
->modes
= INDIO_DIRECT_MODE
;
623 ret
= adis16260_configure_ring(indio_dev
);
627 ret
= iio_device_register(indio_dev
);
629 goto error_unreg_ring_funcs
;
631 ret
= iio_ring_buffer_register_ex(indio_dev
->ring
, 0,
633 ARRAY_SIZE(adis16260_channels_x
));
635 printk(KERN_ERR
"failed to initialize the ring\n");
636 goto error_unreg_ring_funcs
;
640 ret
= adis16260_probe_trigger(indio_dev
);
642 goto error_uninitialize_ring
;
645 /* Get the device into a sane initial state */
646 ret
= adis16260_initial_setup(indio_dev
);
648 goto error_remove_trigger
;
651 error_remove_trigger
:
652 adis16260_remove_trigger(indio_dev
);
653 error_uninitialize_ring
:
654 iio_ring_buffer_unregister(indio_dev
->ring
);
655 error_unreg_ring_funcs
:
656 adis16260_unconfigure_ring(indio_dev
);
659 iio_device_unregister(indio_dev
);
661 iio_free_device(indio_dev
);
666 static int adis16260_remove(struct spi_device
*spi
)
669 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
671 ret
= adis16260_stop_device(indio_dev
);
675 flush_scheduled_work();
677 adis16260_remove_trigger(indio_dev
);
678 iio_ring_buffer_unregister(indio_dev
->ring
);
679 iio_device_unregister(indio_dev
);
680 adis16260_unconfigure_ring(indio_dev
);
687 * These parts do not need to be differentiated until someone adds
688 * support for the on chip filtering.
690 static const struct spi_device_id adis16260_id
[] = {
699 static struct spi_driver adis16260_driver
= {
702 .owner
= THIS_MODULE
,
704 .probe
= adis16260_probe
,
705 .remove
= __devexit_p(adis16260_remove
),
706 .id_table
= adis16260_id
,
709 static __init
int adis16260_init(void)
711 return spi_register_driver(&adis16260_driver
);
713 module_init(adis16260_init
);
715 static __exit
void adis16260_exit(void)
717 spi_unregister_driver(&adis16260_driver
);
719 module_exit(adis16260_exit
);
721 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
722 MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
723 MODULE_LICENSE("GPL v2");