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/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/list.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.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_to_iio_dev(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_to_iio_dev(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_to_iio_dev(dev
);
190 struct adis16260_state
*st
= iio_priv(indio_dev
);
195 ret
= strict_strtol(buf
, 10, &val
);
201 mutex_lock(&indio_dev
->mlock
);
202 if (spi_get_device_id(st
->us
)) {
206 t
&= ADIS16260_SMPL_PRD_DIV_MASK
;
211 t
&= ADIS16260_SMPL_PRD_DIV_MASK
;
213 if ((t
& ADIS16260_SMPL_PRD_DIV_MASK
) >= 0x0A)
214 st
->us
->max_speed_hz
= ADIS16260_SPI_SLOW
;
216 st
->us
->max_speed_hz
= ADIS16260_SPI_FAST
;
217 ret
= adis16260_spi_write_reg_8(indio_dev
,
221 mutex_unlock(&indio_dev
->mlock
);
223 return ret
? ret
: len
;
226 static int adis16260_reset(struct iio_dev
*indio_dev
)
229 ret
= adis16260_spi_write_reg_8(indio_dev
,
231 ADIS16260_GLOB_CMD_SW_RESET
);
233 dev_err(&indio_dev
->dev
, "problem resetting device");
238 int adis16260_set_irq(struct iio_dev
*indio_dev
, bool enable
)
242 ret
= adis16260_spi_read_reg_16(indio_dev
, ADIS16260_MSC_CTRL
, &msc
);
246 msc
|= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH
;
248 msc
|= ADIS16260_MSC_CTRL_DATA_RDY_EN
;
250 msc
&= ~ADIS16260_MSC_CTRL_DATA_RDY_EN
;
252 ret
= adis16260_spi_write_reg_16(indio_dev
, ADIS16260_MSC_CTRL
, msc
);
260 /* Power down the device */
261 static int adis16260_stop_device(struct iio_dev
*indio_dev
)
264 u16 val
= ADIS16260_SLP_CNT_POWER_OFF
;
266 ret
= adis16260_spi_write_reg_16(indio_dev
, ADIS16260_SLP_CNT
, val
);
268 dev_err(&indio_dev
->dev
, "problem with turning device off: SLP_CNT");
273 static int adis16260_self_test(struct iio_dev
*indio_dev
)
276 ret
= adis16260_spi_write_reg_16(indio_dev
,
278 ADIS16260_MSC_CTRL_MEM_TEST
);
280 dev_err(&indio_dev
->dev
, "problem starting self test");
284 adis16260_check_status(indio_dev
);
290 static int adis16260_check_status(struct iio_dev
*indio_dev
)
294 struct device
*dev
= &indio_dev
->dev
;
296 ret
= adis16260_spi_read_reg_16(indio_dev
,
301 dev_err(dev
, "Reading status failed\n");
305 if (status
& ADIS16260_DIAG_STAT_FLASH_CHK
)
306 dev_err(dev
, "Flash checksum error\n");
307 if (status
& ADIS16260_DIAG_STAT_SELF_TEST
)
308 dev_err(dev
, "Self test error\n");
309 if (status
& ADIS16260_DIAG_STAT_OVERFLOW
)
310 dev_err(dev
, "Sensor overrange\n");
311 if (status
& ADIS16260_DIAG_STAT_SPI_FAIL
)
312 dev_err(dev
, "SPI failure\n");
313 if (status
& ADIS16260_DIAG_STAT_FLASH_UPT
)
314 dev_err(dev
, "Flash update failed\n");
315 if (status
& ADIS16260_DIAG_STAT_POWER_HIGH
)
316 dev_err(dev
, "Power supply above 5.25V\n");
317 if (status
& ADIS16260_DIAG_STAT_POWER_LOW
)
318 dev_err(dev
, "Power supply below 4.75V\n");
324 static int adis16260_initial_setup(struct iio_dev
*indio_dev
)
327 struct device
*dev
= &indio_dev
->dev
;
330 ret
= adis16260_set_irq(indio_dev
, false);
332 dev_err(dev
, "disable irq failed");
337 ret
= adis16260_self_test(indio_dev
);
339 dev_err(dev
, "self test failure");
343 /* Read status register to check the result */
344 ret
= adis16260_check_status(indio_dev
);
346 adis16260_reset(indio_dev
);
347 dev_err(dev
, "device not playing ball -> reset");
348 msleep(ADIS16260_STARTUP_DELAY
);
349 ret
= adis16260_check_status(indio_dev
);
351 dev_err(dev
, "giving up");
360 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
361 adis16260_read_frequency
,
362 adis16260_write_frequency
);
364 static IIO_DEVICE_ATTR(sampling_frequency_available
,
365 S_IRUGO
, adis16260_read_frequency_available
, NULL
, 0);
367 enum adis16260_channel
{
374 #define ADIS16260_GYRO_CHANNEL_SET(axis, mod) \
375 struct iio_chan_spec adis16260_channels_##axis[] = { \
377 .type = IIO_ANGL_VEL, \
380 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
381 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT | \
382 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
383 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
385 .scan_index = ADIS16260_SCAN_GYRO, \
395 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT, \
397 .scan_index = ADIS16260_SCAN_ANGL, \
407 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
408 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
409 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
411 .scan_index = ADIS16260_SCAN_TEMP, \
418 .type = IIO_VOLTAGE, \
421 .extend_name = "supply", \
422 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
423 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
424 .address = in_supply, \
425 .scan_index = ADIS16260_SCAN_SUPPLY, \
432 .type = IIO_VOLTAGE, \
435 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
436 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
438 .scan_index = ADIS16260_SCAN_AUX_ADC, \
445 IIO_CHAN_SOFT_TIMESTAMP(5), \
448 static const ADIS16260_GYRO_CHANNEL_SET(x
, IIO_MOD_X
);
449 static const ADIS16260_GYRO_CHANNEL_SET(y
, IIO_MOD_Y
);
450 static const ADIS16260_GYRO_CHANNEL_SET(z
, IIO_MOD_Z
);
452 static const u8 adis16260_addresses
[5][3] = {
453 [gyro
] = { ADIS16260_GYRO_OUT
,
455 ADIS16260_GYRO_SCALE
},
456 [angle
] = { ADIS16260_ANGL_OUT
},
457 [in_supply
] = { ADIS16260_SUPPLY_OUT
},
458 [in_aux
] = { ADIS16260_AUX_ADC
},
459 [temp
] = { ADIS16260_TEMP_OUT
},
461 static int adis16260_read_raw(struct iio_dev
*indio_dev
,
462 struct iio_chan_spec
const *chan
,
466 struct adis16260_state
*st
= iio_priv(indio_dev
);
473 case IIO_CHAN_INFO_RAW
:
474 mutex_lock(&indio_dev
->mlock
);
475 addr
= adis16260_addresses
[chan
->address
][0];
476 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
478 mutex_unlock(&indio_dev
->mlock
);
482 if (val16
& ADIS16260_ERROR_ACTIVE
) {
483 ret
= adis16260_check_status(indio_dev
);
485 mutex_unlock(&indio_dev
->mlock
);
489 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
490 if (chan
->scan_type
.sign
== 's')
491 val16
= (s16
)(val16
<<
492 (16 - chan
->scan_type
.realbits
)) >>
493 (16 - chan
->scan_type
.realbits
);
495 mutex_unlock(&indio_dev
->mlock
);
497 case IIO_CHAN_INFO_SCALE
:
498 switch (chan
->type
) {
501 if (spi_get_device_id(st
->us
)->driver_data
)
505 return IIO_VAL_INT_PLUS_MICRO
;
508 if (chan
->channel
== 0)
512 return IIO_VAL_INT_PLUS_MICRO
;
516 return IIO_VAL_INT_PLUS_MICRO
;
521 case IIO_CHAN_INFO_OFFSET
:
524 case IIO_CHAN_INFO_CALIBBIAS
:
525 switch (chan
->type
) {
532 mutex_lock(&indio_dev
->mlock
);
533 addr
= adis16260_addresses
[chan
->address
][1];
534 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
536 mutex_unlock(&indio_dev
->mlock
);
539 val16
&= (1 << bits
) - 1;
540 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
542 mutex_unlock(&indio_dev
->mlock
);
544 case IIO_CHAN_INFO_CALIBSCALE
:
545 switch (chan
->type
) {
552 mutex_lock(&indio_dev
->mlock
);
553 addr
= adis16260_addresses
[chan
->address
][2];
554 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
556 mutex_unlock(&indio_dev
->mlock
);
559 *val
= (1 << bits
) - 1;
560 mutex_unlock(&indio_dev
->mlock
);
566 static int adis16260_write_raw(struct iio_dev
*indio_dev
,
567 struct iio_chan_spec
const *chan
,
576 case IIO_CHAN_INFO_CALIBBIAS
:
577 val16
= val
& ((1 << bits
) - 1);
578 addr
= adis16260_addresses
[chan
->address
][1];
579 return adis16260_spi_write_reg_16(indio_dev
, addr
, val16
);
580 case IIO_CHAN_INFO_CALIBSCALE
:
581 val16
= val
& ((1 << bits
) - 1);
582 addr
= adis16260_addresses
[chan
->address
][2];
583 return adis16260_spi_write_reg_16(indio_dev
, addr
, val16
);
588 static struct attribute
*adis16260_attributes
[] = {
589 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
590 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
594 static const struct attribute_group adis16260_attribute_group
= {
595 .attrs
= adis16260_attributes
,
598 static const struct iio_info adis16260_info
= {
599 .attrs
= &adis16260_attribute_group
,
600 .read_raw
= &adis16260_read_raw
,
601 .write_raw
= &adis16260_write_raw
,
602 .driver_module
= THIS_MODULE
,
605 static int __devinit
adis16260_probe(struct spi_device
*spi
)
608 struct adis16260_platform_data
*pd
= spi
->dev
.platform_data
;
609 struct adis16260_state
*st
;
610 struct iio_dev
*indio_dev
;
612 /* setup the industrialio driver allocated elements */
613 indio_dev
= iio_device_alloc(sizeof(*st
));
614 if (indio_dev
== NULL
) {
618 st
= iio_priv(indio_dev
);
620 st
->negate
= pd
->negate
;
621 /* this is only used for removal purposes */
622 spi_set_drvdata(spi
, indio_dev
);
625 mutex_init(&st
->buf_lock
);
627 indio_dev
->name
= spi_get_device_id(st
->us
)->name
;
628 indio_dev
->dev
.parent
= &spi
->dev
;
629 indio_dev
->info
= &adis16260_info
;
630 indio_dev
->num_channels
631 = ARRAY_SIZE(adis16260_channels_x
);
632 if (pd
&& pd
->direction
)
633 switch (pd
->direction
) {
635 indio_dev
->channels
= adis16260_channels_x
;
638 indio_dev
->channels
= adis16260_channels_y
;
641 indio_dev
->channels
= adis16260_channels_z
;
647 indio_dev
->channels
= adis16260_channels_x
;
648 indio_dev
->num_channels
= ARRAY_SIZE(adis16260_channels_x
);
649 indio_dev
->modes
= INDIO_DIRECT_MODE
;
651 ret
= adis16260_configure_ring(indio_dev
);
655 ret
= iio_buffer_register(indio_dev
,
657 ARRAY_SIZE(adis16260_channels_x
));
659 printk(KERN_ERR
"failed to initialize the ring\n");
660 goto error_unreg_ring_funcs
;
662 if (indio_dev
->buffer
) {
663 /* Set default scan mode */
664 iio_scan_mask_set(indio_dev
, indio_dev
->buffer
,
665 ADIS16260_SCAN_SUPPLY
);
666 iio_scan_mask_set(indio_dev
, indio_dev
->buffer
,
667 ADIS16260_SCAN_GYRO
);
668 iio_scan_mask_set(indio_dev
, indio_dev
->buffer
,
669 ADIS16260_SCAN_AUX_ADC
);
670 iio_scan_mask_set(indio_dev
, indio_dev
->buffer
,
671 ADIS16260_SCAN_TEMP
);
672 iio_scan_mask_set(indio_dev
, indio_dev
->buffer
,
673 ADIS16260_SCAN_ANGL
);
676 ret
= adis16260_probe_trigger(indio_dev
);
678 goto error_uninitialize_ring
;
681 /* Get the device into a sane initial state */
682 ret
= adis16260_initial_setup(indio_dev
);
684 goto error_remove_trigger
;
685 ret
= iio_device_register(indio_dev
);
687 goto error_remove_trigger
;
691 error_remove_trigger
:
692 adis16260_remove_trigger(indio_dev
);
693 error_uninitialize_ring
:
694 iio_buffer_unregister(indio_dev
);
695 error_unreg_ring_funcs
:
696 adis16260_unconfigure_ring(indio_dev
);
698 iio_device_free(indio_dev
);
703 static int adis16260_remove(struct spi_device
*spi
)
706 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
708 iio_device_unregister(indio_dev
);
710 ret
= adis16260_stop_device(indio_dev
);
714 adis16260_remove_trigger(indio_dev
);
715 iio_buffer_unregister(indio_dev
);
716 adis16260_unconfigure_ring(indio_dev
);
717 iio_device_free(indio_dev
);
724 * These parts do not need to be differentiated until someone adds
725 * support for the on chip filtering.
727 static const struct spi_device_id adis16260_id
[] = {
735 MODULE_DEVICE_TABLE(spi
, adis16260_id
);
737 static struct spi_driver adis16260_driver
= {
740 .owner
= THIS_MODULE
,
742 .probe
= adis16260_probe
,
743 .remove
= __devexit_p(adis16260_remove
),
744 .id_table
= adis16260_id
,
746 module_spi_driver(adis16260_driver
);
748 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
749 MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
750 MODULE_LICENSE("GPL v2");