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>
20 #include <linux/module.h>
24 #include "../ring_generic.h"
26 #include "adis16260.h"
28 #define DRIVER_NAME "adis16260"
30 static int adis16260_check_status(struct iio_dev
*indio_dev
);
33 * adis16260_spi_write_reg_8() - write single byte to a register
34 * @indio_dev: iio_dev for the device
35 * @reg_address: the address of the register to be written
36 * @val: the value to write
38 static int adis16260_spi_write_reg_8(struct iio_dev
*indio_dev
,
43 struct adis16260_state
*st
= iio_priv(indio_dev
);
45 mutex_lock(&st
->buf_lock
);
46 st
->tx
[0] = ADIS16260_WRITE_REG(reg_address
);
49 ret
= spi_write(st
->us
, st
->tx
, 2);
50 mutex_unlock(&st
->buf_lock
);
56 * adis16260_spi_write_reg_16() - write 2 bytes to a pair of registers
57 * @indio_dev: iio_dev for the device
58 * @reg_address: the address of the lower of the two registers. Second register
59 * is assumed to have address one greater.
60 * @val: value to be written
62 static int adis16260_spi_write_reg_16(struct iio_dev
*indio_dev
,
67 struct spi_message msg
;
68 struct adis16260_state
*st
= iio_priv(indio_dev
);
69 struct spi_transfer xfers
[] = {
84 mutex_lock(&st
->buf_lock
);
85 st
->tx
[0] = ADIS16260_WRITE_REG(lower_reg_address
);
86 st
->tx
[1] = value
& 0xFF;
87 st
->tx
[2] = ADIS16260_WRITE_REG(lower_reg_address
+ 1);
88 st
->tx
[3] = (value
>> 8) & 0xFF;
90 spi_message_init(&msg
);
91 spi_message_add_tail(&xfers
[0], &msg
);
92 spi_message_add_tail(&xfers
[1], &msg
);
93 ret
= spi_sync(st
->us
, &msg
);
94 mutex_unlock(&st
->buf_lock
);
100 * adis16260_spi_read_reg_16() - read 2 bytes from a 16-bit register
101 * @indio_dev: iio_dev for the device
102 * @reg_address: the address of the lower of the two registers. Second register
103 * is assumed to have address one greater.
104 * @val: somewhere to pass back the value read
106 static int adis16260_spi_read_reg_16(struct iio_dev
*indio_dev
,
107 u8 lower_reg_address
,
110 struct spi_message msg
;
111 struct adis16260_state
*st
= iio_priv(indio_dev
);
113 struct spi_transfer xfers
[] = {
128 mutex_lock(&st
->buf_lock
);
129 st
->tx
[0] = ADIS16260_READ_REG(lower_reg_address
);
132 spi_message_init(&msg
);
133 spi_message_add_tail(&xfers
[0], &msg
);
134 spi_message_add_tail(&xfers
[1], &msg
);
135 ret
= spi_sync(st
->us
, &msg
);
137 dev_err(&st
->us
->dev
,
138 "problem when reading 16 bit register 0x%02X",
142 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
145 mutex_unlock(&st
->buf_lock
);
149 static ssize_t
adis16260_read_frequency_available(struct device
*dev
,
150 struct device_attribute
*attr
,
153 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
154 struct adis16260_state
*st
= iio_priv(indio_dev
);
155 if (spi_get_device_id(st
->us
)->driver_data
)
156 return sprintf(buf
, "%s\n", "0.129 ~ 256");
158 return sprintf(buf
, "%s\n", "256 2048");
161 static ssize_t
adis16260_read_frequency(struct device
*dev
,
162 struct device_attribute
*attr
,
165 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
166 struct adis16260_state
*st
= iio_priv(indio_dev
);
170 ret
= adis16260_spi_read_reg_16(indio_dev
,
176 if (spi_get_device_id(st
->us
)->driver_data
) /* If an adis16251 */
177 sps
= (t
& ADIS16260_SMPL_PRD_TIME_BASE
) ? 8 : 256;
179 sps
= (t
& ADIS16260_SMPL_PRD_TIME_BASE
) ? 66 : 2048;
180 sps
/= (t
& ADIS16260_SMPL_PRD_DIV_MASK
) + 1;
181 len
= sprintf(buf
, "%d SPS\n", sps
);
185 static ssize_t
adis16260_write_frequency(struct device
*dev
,
186 struct device_attribute
*attr
,
190 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
191 struct adis16260_state
*st
= iio_priv(indio_dev
);
196 ret
= strict_strtol(buf
, 10, &val
);
200 mutex_lock(&indio_dev
->mlock
);
201 if (spi_get_device_id(st
->us
)) {
205 t
&= ADIS16260_SMPL_PRD_DIV_MASK
;
210 t
&= ADIS16260_SMPL_PRD_DIV_MASK
;
212 if ((t
& ADIS16260_SMPL_PRD_DIV_MASK
) >= 0x0A)
213 st
->us
->max_speed_hz
= ADIS16260_SPI_SLOW
;
215 st
->us
->max_speed_hz
= ADIS16260_SPI_FAST
;
216 ret
= adis16260_spi_write_reg_8(indio_dev
,
220 mutex_unlock(&indio_dev
->mlock
);
222 return ret
? ret
: len
;
225 static int adis16260_reset(struct iio_dev
*indio_dev
)
228 ret
= adis16260_spi_write_reg_8(indio_dev
,
230 ADIS16260_GLOB_CMD_SW_RESET
);
232 dev_err(&indio_dev
->dev
, "problem resetting device");
237 static ssize_t
adis16260_write_reset(struct device
*dev
,
238 struct device_attribute
*attr
,
239 const char *buf
, size_t len
)
241 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
248 return adis16260_reset(indio_dev
);
253 int adis16260_set_irq(struct iio_dev
*indio_dev
, bool enable
)
257 ret
= adis16260_spi_read_reg_16(indio_dev
, ADIS16260_MSC_CTRL
, &msc
);
261 msc
|= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH
;
263 msc
|= ADIS16260_MSC_CTRL_DATA_RDY_EN
;
265 msc
&= ~ADIS16260_MSC_CTRL_DATA_RDY_EN
;
267 ret
= adis16260_spi_write_reg_16(indio_dev
, ADIS16260_MSC_CTRL
, msc
);
275 /* Power down the device */
276 static int adis16260_stop_device(struct iio_dev
*indio_dev
)
279 u16 val
= ADIS16260_SLP_CNT_POWER_OFF
;
281 ret
= adis16260_spi_write_reg_16(indio_dev
, ADIS16260_SLP_CNT
, val
);
283 dev_err(&indio_dev
->dev
, "problem with turning device off: SLP_CNT");
288 static int adis16260_self_test(struct iio_dev
*indio_dev
)
291 ret
= adis16260_spi_write_reg_16(indio_dev
,
293 ADIS16260_MSC_CTRL_MEM_TEST
);
295 dev_err(&indio_dev
->dev
, "problem starting self test");
299 adis16260_check_status(indio_dev
);
305 static int adis16260_check_status(struct iio_dev
*indio_dev
)
309 struct device
*dev
= &indio_dev
->dev
;
311 ret
= adis16260_spi_read_reg_16(indio_dev
,
316 dev_err(dev
, "Reading status failed\n");
320 if (status
& ADIS16260_DIAG_STAT_FLASH_CHK
)
321 dev_err(dev
, "Flash checksum error\n");
322 if (status
& ADIS16260_DIAG_STAT_SELF_TEST
)
323 dev_err(dev
, "Self test error\n");
324 if (status
& ADIS16260_DIAG_STAT_OVERFLOW
)
325 dev_err(dev
, "Sensor overrange\n");
326 if (status
& ADIS16260_DIAG_STAT_SPI_FAIL
)
327 dev_err(dev
, "SPI failure\n");
328 if (status
& ADIS16260_DIAG_STAT_FLASH_UPT
)
329 dev_err(dev
, "Flash update failed\n");
330 if (status
& ADIS16260_DIAG_STAT_POWER_HIGH
)
331 dev_err(dev
, "Power supply above 5.25V\n");
332 if (status
& ADIS16260_DIAG_STAT_POWER_LOW
)
333 dev_err(dev
, "Power supply below 4.75V\n");
339 static int adis16260_initial_setup(struct iio_dev
*indio_dev
)
342 struct device
*dev
= &indio_dev
->dev
;
345 ret
= adis16260_set_irq(indio_dev
, false);
347 dev_err(dev
, "disable irq failed");
352 ret
= adis16260_self_test(indio_dev
);
354 dev_err(dev
, "self test failure");
358 /* Read status register to check the result */
359 ret
= adis16260_check_status(indio_dev
);
361 adis16260_reset(indio_dev
);
362 dev_err(dev
, "device not playing ball -> reset");
363 msleep(ADIS16260_STARTUP_DELAY
);
364 ret
= adis16260_check_status(indio_dev
);
366 dev_err(dev
, "giving up");
375 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
376 adis16260_read_frequency
,
377 adis16260_write_frequency
);
379 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16260_write_reset
, 0);
381 static IIO_DEVICE_ATTR(sampling_frequency_available
,
382 S_IRUGO
, adis16260_read_frequency_available
, NULL
, 0);
384 enum adis16260_channel
{
391 #define ADIS16260_GYRO_CHANNEL_SET(axis, mod) \
392 struct iio_chan_spec adis16260_channels_##axis[] = { \
393 IIO_CHAN(IIO_GYRO, 1, 0, 0, NULL, 0, mod, \
394 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) | \
395 (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) | \
396 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
397 gyro, ADIS16260_SCAN_GYRO, \
398 IIO_ST('s', 14, 16, 0), 0), \
399 IIO_CHAN(IIO_ANGL, 1, 0, 0, NULL, 0, mod, \
401 angle, ADIS16260_SCAN_ANGL, \
402 IIO_ST('u', 14, 16, 0), 0), \
403 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0, \
404 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) | \
405 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
406 temp, ADIS16260_SCAN_TEMP, \
407 IIO_ST('u', 12, 16, 0), 0), \
408 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0, \
409 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
410 in_supply, ADIS16260_SCAN_SUPPLY, \
411 IIO_ST('u', 12, 16, 0), 0), \
412 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0, \
413 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
414 in_aux, ADIS16260_SCAN_AUX_ADC, \
415 IIO_ST('u', 12, 16, 0), 0), \
416 IIO_CHAN_SOFT_TIMESTAMP(5) \
419 static const ADIS16260_GYRO_CHANNEL_SET(x
, IIO_MOD_X
);
420 static const ADIS16260_GYRO_CHANNEL_SET(y
, IIO_MOD_Y
);
421 static const ADIS16260_GYRO_CHANNEL_SET(z
, IIO_MOD_Z
);
423 static const u8 adis16260_addresses
[5][3] = {
424 [gyro
] = { ADIS16260_GYRO_OUT
,
426 ADIS16260_GYRO_SCALE
},
427 [angle
] = { ADIS16260_ANGL_OUT
},
428 [in_supply
] = { ADIS16260_SUPPLY_OUT
},
429 [in_aux
] = { ADIS16260_AUX_ADC
},
430 [temp
] = { ADIS16260_TEMP_OUT
},
432 static int adis16260_read_raw(struct iio_dev
*indio_dev
,
433 struct iio_chan_spec
const *chan
,
437 struct adis16260_state
*st
= iio_priv(indio_dev
);
445 mutex_lock(&indio_dev
->mlock
);
446 addr
= adis16260_addresses
[chan
->address
][0];
447 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
449 mutex_unlock(&indio_dev
->mlock
);
453 if (val16
& ADIS16260_ERROR_ACTIVE
) {
454 ret
= adis16260_check_status(indio_dev
);
456 mutex_unlock(&indio_dev
->mlock
);
460 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
461 if (chan
->scan_type
.sign
== 's')
462 val16
= (s16
)(val16
<<
463 (16 - chan
->scan_type
.realbits
)) >>
464 (16 - chan
->scan_type
.realbits
);
466 mutex_unlock(&indio_dev
->mlock
);
468 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
469 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
470 switch (chan
->type
) {
473 if (spi_get_device_id(st
->us
)->driver_data
)
477 return IIO_VAL_INT_PLUS_MICRO
;
480 if (chan
->channel
== 0)
484 return IIO_VAL_INT_PLUS_MICRO
;
488 return IIO_VAL_INT_PLUS_MICRO
;
493 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
):
496 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
497 switch (chan
->type
) {
504 mutex_lock(&indio_dev
->mlock
);
505 addr
= adis16260_addresses
[chan
->address
][1];
506 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
508 mutex_unlock(&indio_dev
->mlock
);
511 val16
&= (1 << bits
) - 1;
512 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
514 mutex_unlock(&indio_dev
->mlock
);
516 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE
):
517 switch (chan
->type
) {
524 mutex_lock(&indio_dev
->mlock
);
525 addr
= adis16260_addresses
[chan
->address
][2];
526 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
528 mutex_unlock(&indio_dev
->mlock
);
531 *val
= (1 << bits
) - 1;
532 mutex_unlock(&indio_dev
->mlock
);
538 static int adis16260_write_raw(struct iio_dev
*indio_dev
,
539 struct iio_chan_spec
const *chan
,
548 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
549 val16
= val
& ((1 << bits
) - 1);
550 addr
= adis16260_addresses
[chan
->address
][1];
551 return adis16260_spi_write_reg_16(indio_dev
, addr
, val16
);
552 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE
):
553 val16
= val
& ((1 << bits
) - 1);
554 addr
= adis16260_addresses
[chan
->address
][2];
555 return adis16260_spi_write_reg_16(indio_dev
, addr
, val16
);
560 static struct attribute
*adis16260_attributes
[] = {
561 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
562 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
563 &iio_dev_attr_reset
.dev_attr
.attr
,
567 static const struct attribute_group adis16260_attribute_group
= {
568 .attrs
= adis16260_attributes
,
571 static const struct iio_info adis16260_info
= {
572 .attrs
= &adis16260_attribute_group
,
573 .read_raw
= &adis16260_read_raw
,
574 .write_raw
= &adis16260_write_raw
,
575 .driver_module
= THIS_MODULE
,
578 static int __devinit
adis16260_probe(struct spi_device
*spi
)
580 int ret
, regdone
= 0;
581 struct adis16260_platform_data
*pd
= spi
->dev
.platform_data
;
582 struct adis16260_state
*st
;
583 struct iio_dev
*indio_dev
;
585 /* setup the industrialio driver allocated elements */
586 indio_dev
= iio_allocate_device(sizeof(*st
));
587 if (indio_dev
== NULL
) {
591 st
= iio_priv(indio_dev
);
593 st
->negate
= pd
->negate
;
594 /* this is only used for removal purposes */
595 spi_set_drvdata(spi
, st
);
598 mutex_init(&st
->buf_lock
);
600 indio_dev
->name
= spi_get_device_id(st
->us
)->name
;
601 indio_dev
->dev
.parent
= &spi
->dev
;
602 indio_dev
->info
= &adis16260_info
;
603 indio_dev
->num_channels
604 = ARRAY_SIZE(adis16260_channels_x
);
605 if (pd
&& pd
->direction
)
606 switch (pd
->direction
) {
608 indio_dev
->channels
= adis16260_channels_x
;
611 indio_dev
->channels
= adis16260_channels_y
;
614 indio_dev
->channels
= adis16260_channels_z
;
620 indio_dev
->channels
= adis16260_channels_x
;
621 indio_dev
->num_channels
= ARRAY_SIZE(adis16260_channels_x
);
622 indio_dev
->modes
= INDIO_DIRECT_MODE
;
624 ret
= adis16260_configure_ring(indio_dev
);
628 ret
= iio_device_register(indio_dev
);
630 goto error_unreg_ring_funcs
;
632 ret
= iio_ring_buffer_register_ex(indio_dev
, 0,
634 ARRAY_SIZE(adis16260_channels_x
));
636 printk(KERN_ERR
"failed to initialize the ring\n");
637 goto error_unreg_ring_funcs
;
641 ret
= adis16260_probe_trigger(indio_dev
);
643 goto error_uninitialize_ring
;
646 /* Get the device into a sane initial state */
647 ret
= adis16260_initial_setup(indio_dev
);
649 goto error_remove_trigger
;
652 error_remove_trigger
:
653 adis16260_remove_trigger(indio_dev
);
654 error_uninitialize_ring
:
655 iio_ring_buffer_unregister(indio_dev
);
656 error_unreg_ring_funcs
:
657 adis16260_unconfigure_ring(indio_dev
);
660 iio_device_unregister(indio_dev
);
662 iio_free_device(indio_dev
);
667 static int adis16260_remove(struct spi_device
*spi
)
670 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
672 ret
= adis16260_stop_device(indio_dev
);
676 flush_scheduled_work();
678 adis16260_remove_trigger(indio_dev
);
679 iio_ring_buffer_unregister(indio_dev
);
680 iio_device_unregister(indio_dev
);
681 adis16260_unconfigure_ring(indio_dev
);
688 * These parts do not need to be differentiated until someone adds
689 * support for the on chip filtering.
691 static const struct spi_device_id adis16260_id
[] = {
700 static struct spi_driver adis16260_driver
= {
703 .owner
= THIS_MODULE
,
705 .probe
= adis16260_probe
,
706 .remove
= __devexit_p(adis16260_remove
),
707 .id_table
= adis16260_id
,
710 static __init
int adis16260_init(void)
712 return spi_register_driver(&adis16260_driver
);
714 module_init(adis16260_init
);
716 static __exit
void adis16260_exit(void)
718 spi_unregister_driver(&adis16260_driver
);
720 module_exit(adis16260_exit
);
722 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
723 MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
724 MODULE_LICENSE("GPL v2");