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"
24 #include "../adc/adc.h"
27 #include "adis16260.h"
29 #define DRIVER_NAME "adis16260"
31 static int adis16260_check_status(struct iio_dev
*indio_dev
);
34 * adis16260_spi_write_reg_8() - write single byte to a register
35 * @indio_dev: iio_dev for the device
36 * @reg_address: the address of the register to be written
37 * @val: the value to write
39 static int adis16260_spi_write_reg_8(struct iio_dev
*indio_dev
,
44 struct adis16260_state
*st
= iio_priv(indio_dev
);
46 mutex_lock(&st
->buf_lock
);
47 st
->tx
[0] = ADIS16260_WRITE_REG(reg_address
);
50 ret
= spi_write(st
->us
, st
->tx
, 2);
51 mutex_unlock(&st
->buf_lock
);
57 * adis16260_spi_write_reg_16() - write 2 bytes to a pair of registers
58 * @indio_dev: iio_dev for the device
59 * @reg_address: the address of the lower of the two registers. Second register
60 * is assumed to have address one greater.
61 * @val: value to be written
63 static int adis16260_spi_write_reg_16(struct iio_dev
*indio_dev
,
68 struct spi_message msg
;
69 struct adis16260_state
*st
= iio_priv(indio_dev
);
70 struct spi_transfer xfers
[] = {
85 mutex_lock(&st
->buf_lock
);
86 st
->tx
[0] = ADIS16260_WRITE_REG(lower_reg_address
);
87 st
->tx
[1] = value
& 0xFF;
88 st
->tx
[2] = ADIS16260_WRITE_REG(lower_reg_address
+ 1);
89 st
->tx
[3] = (value
>> 8) & 0xFF;
91 spi_message_init(&msg
);
92 spi_message_add_tail(&xfers
[0], &msg
);
93 spi_message_add_tail(&xfers
[1], &msg
);
94 ret
= spi_sync(st
->us
, &msg
);
95 mutex_unlock(&st
->buf_lock
);
101 * adis16260_spi_read_reg_16() - read 2 bytes from a 16-bit register
102 * @indio_dev: iio_dev for the device
103 * @reg_address: the address of the lower of the two registers. Second register
104 * is assumed to have address one greater.
105 * @val: somewhere to pass back the value read
107 static int adis16260_spi_read_reg_16(struct iio_dev
*indio_dev
,
108 u8 lower_reg_address
,
111 struct spi_message msg
;
112 struct adis16260_state
*st
= iio_priv(indio_dev
);
114 struct spi_transfer xfers
[] = {
129 mutex_lock(&st
->buf_lock
);
130 st
->tx
[0] = ADIS16260_READ_REG(lower_reg_address
);
133 spi_message_init(&msg
);
134 spi_message_add_tail(&xfers
[0], &msg
);
135 spi_message_add_tail(&xfers
[1], &msg
);
136 ret
= spi_sync(st
->us
, &msg
);
138 dev_err(&st
->us
->dev
,
139 "problem when reading 16 bit register 0x%02X",
143 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
146 mutex_unlock(&st
->buf_lock
);
150 static ssize_t
adis16260_read_frequency_available(struct device
*dev
,
151 struct device_attribute
*attr
,
154 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
155 struct adis16260_state
*st
= iio_priv(indio_dev
);
156 if (spi_get_device_id(st
->us
)->driver_data
)
157 return sprintf(buf
, "%s\n", "0.129 ~ 256");
159 return sprintf(buf
, "%s\n", "256 2048");
162 static ssize_t
adis16260_read_frequency(struct device
*dev
,
163 struct device_attribute
*attr
,
166 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
167 struct adis16260_state
*st
= iio_priv(indio_dev
);
171 ret
= adis16260_spi_read_reg_16(indio_dev
,
177 if (spi_get_device_id(st
->us
)->driver_data
) /* If an adis16251 */
178 sps
= (t
& ADIS16260_SMPL_PRD_TIME_BASE
) ? 8 : 256;
180 sps
= (t
& ADIS16260_SMPL_PRD_TIME_BASE
) ? 66 : 2048;
181 sps
/= (t
& ADIS16260_SMPL_PRD_DIV_MASK
) + 1;
182 len
= sprintf(buf
, "%d SPS\n", sps
);
186 static ssize_t
adis16260_write_frequency(struct device
*dev
,
187 struct device_attribute
*attr
,
191 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
192 struct adis16260_state
*st
= iio_priv(indio_dev
);
197 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 static ssize_t
adis16260_write_reset(struct device
*dev
,
239 struct device_attribute
*attr
,
240 const char *buf
, size_t len
)
242 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
249 return adis16260_reset(indio_dev
);
254 int adis16260_set_irq(struct iio_dev
*indio_dev
, bool enable
)
258 ret
= adis16260_spi_read_reg_16(indio_dev
, ADIS16260_MSC_CTRL
, &msc
);
262 msc
|= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH
;
264 msc
|= ADIS16260_MSC_CTRL_DATA_RDY_EN
;
266 msc
&= ~ADIS16260_MSC_CTRL_DATA_RDY_EN
;
268 ret
= adis16260_spi_write_reg_16(indio_dev
, ADIS16260_MSC_CTRL
, msc
);
276 /* Power down the device */
277 static int adis16260_stop_device(struct iio_dev
*indio_dev
)
280 u16 val
= ADIS16260_SLP_CNT_POWER_OFF
;
282 ret
= adis16260_spi_write_reg_16(indio_dev
, ADIS16260_SLP_CNT
, val
);
284 dev_err(&indio_dev
->dev
, "problem with turning device off: SLP_CNT");
289 static int adis16260_self_test(struct iio_dev
*indio_dev
)
292 ret
= adis16260_spi_write_reg_16(indio_dev
,
294 ADIS16260_MSC_CTRL_MEM_TEST
);
296 dev_err(&indio_dev
->dev
, "problem starting self test");
300 adis16260_check_status(indio_dev
);
306 static int adis16260_check_status(struct iio_dev
*indio_dev
)
310 struct device
*dev
= &indio_dev
->dev
;
312 ret
= adis16260_spi_read_reg_16(indio_dev
,
317 dev_err(dev
, "Reading status failed\n");
321 if (status
& ADIS16260_DIAG_STAT_FLASH_CHK
)
322 dev_err(dev
, "Flash checksum error\n");
323 if (status
& ADIS16260_DIAG_STAT_SELF_TEST
)
324 dev_err(dev
, "Self test error\n");
325 if (status
& ADIS16260_DIAG_STAT_OVERFLOW
)
326 dev_err(dev
, "Sensor overrange\n");
327 if (status
& ADIS16260_DIAG_STAT_SPI_FAIL
)
328 dev_err(dev
, "SPI failure\n");
329 if (status
& ADIS16260_DIAG_STAT_FLASH_UPT
)
330 dev_err(dev
, "Flash update failed\n");
331 if (status
& ADIS16260_DIAG_STAT_POWER_HIGH
)
332 dev_err(dev
, "Power supply above 5.25V\n");
333 if (status
& ADIS16260_DIAG_STAT_POWER_LOW
)
334 dev_err(dev
, "Power supply below 4.75V\n");
340 static int adis16260_initial_setup(struct iio_dev
*indio_dev
)
343 struct device
*dev
= &indio_dev
->dev
;
346 ret
= adis16260_set_irq(indio_dev
, false);
348 dev_err(dev
, "disable irq failed");
353 ret
= adis16260_self_test(indio_dev
);
355 dev_err(dev
, "self test failure");
359 /* Read status register to check the result */
360 ret
= adis16260_check_status(indio_dev
);
362 adis16260_reset(indio_dev
);
363 dev_err(dev
, "device not playing ball -> reset");
364 msleep(ADIS16260_STARTUP_DELAY
);
365 ret
= adis16260_check_status(indio_dev
);
367 dev_err(dev
, "giving up");
376 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
377 adis16260_read_frequency
,
378 adis16260_write_frequency
);
380 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16260_write_reset
, 0);
382 static IIO_DEVICE_ATTR(sampling_frequency_available
,
383 S_IRUGO
, adis16260_read_frequency_available
, NULL
, 0);
385 enum adis16260_channel
{
392 #define ADIS16260_GYRO_CHANNEL_SET(axis, mod) \
393 struct iio_chan_spec adis16260_channels_##axis[] = { \
394 IIO_CHAN(IIO_GYRO, 1, 0, 0, NULL, 0, mod, \
395 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) | \
396 (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) | \
397 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
398 gyro, ADIS16260_SCAN_GYRO, \
399 IIO_ST('s', 14, 16, 0), 0), \
400 IIO_CHAN(IIO_ANGL, 1, 0, 0, NULL, 0, mod, \
402 angle, ADIS16260_SCAN_ANGL, \
403 IIO_ST('u', 14, 16, 0), 0), \
404 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0, \
405 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) | \
406 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
407 temp, ADIS16260_SCAN_TEMP, \
408 IIO_ST('u', 12, 16, 0), 0), \
409 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0, \
410 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
411 in_supply, ADIS16260_SCAN_SUPPLY, \
412 IIO_ST('u', 12, 16, 0), 0), \
413 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0, \
414 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
415 in_aux, ADIS16260_SCAN_AUX_ADC, \
416 IIO_ST('u', 12, 16, 0), 0), \
417 IIO_CHAN_SOFT_TIMESTAMP(5) \
420 static const ADIS16260_GYRO_CHANNEL_SET(x
, IIO_MOD_X
);
421 static const ADIS16260_GYRO_CHANNEL_SET(y
, IIO_MOD_Y
);
422 static const ADIS16260_GYRO_CHANNEL_SET(z
, IIO_MOD_Z
);
424 static const u8 adis16260_addresses
[5][3] = {
425 [gyro
] = { ADIS16260_GYRO_OUT
,
427 ADIS16260_GYRO_SCALE
},
428 [angle
] = { ADIS16260_ANGL_OUT
},
429 [in_supply
] = { ADIS16260_SUPPLY_OUT
},
430 [in_aux
] = { ADIS16260_AUX_ADC
},
431 [temp
] = { ADIS16260_TEMP_OUT
},
433 static int adis16260_read_raw(struct iio_dev
*indio_dev
,
434 struct iio_chan_spec
const *chan
,
438 struct adis16260_state
*st
= iio_priv(indio_dev
);
446 mutex_lock(&indio_dev
->mlock
);
447 addr
= adis16260_addresses
[chan
->address
][0];
448 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
452 if (val16
& ADIS16260_ERROR_ACTIVE
) {
453 ret
= adis16260_check_status(indio_dev
);
457 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
458 if (chan
->scan_type
.sign
== 's')
459 val16
= (s16
)(val16
<<
460 (16 - chan
->scan_type
.realbits
)) >>
461 (16 - chan
->scan_type
.realbits
);
463 mutex_unlock(&indio_dev
->mlock
);
465 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
466 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
467 switch (chan
->type
) {
470 if (spi_get_device_id(st
->us
)->driver_data
)
474 return IIO_VAL_INT_PLUS_MICRO
;
477 if (chan
->channel
== 0)
481 return IIO_VAL_INT_PLUS_MICRO
;
485 return IIO_VAL_INT_PLUS_MICRO
;
490 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
):
493 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
494 switch (chan
->type
) {
501 mutex_lock(&indio_dev
->mlock
);
502 addr
= adis16260_addresses
[chan
->address
][1];
503 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
505 mutex_unlock(&indio_dev
->mlock
);
508 val16
&= (1 << bits
) - 1;
509 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
511 mutex_unlock(&indio_dev
->mlock
);
513 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE
):
514 switch (chan
->type
) {
521 mutex_lock(&indio_dev
->mlock
);
522 addr
= adis16260_addresses
[chan
->address
][2];
523 ret
= adis16260_spi_read_reg_16(indio_dev
, addr
, &val16
);
525 mutex_unlock(&indio_dev
->mlock
);
528 *val
= (1 << bits
) - 1;
529 mutex_unlock(&indio_dev
->mlock
);
535 static int adis16260_write_raw(struct iio_dev
*indio_dev
,
536 struct iio_chan_spec
const *chan
,
545 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
546 val16
= val
& ((1 << bits
) - 1);
547 addr
= adis16260_addresses
[chan
->address
][1];
548 return adis16260_spi_write_reg_16(indio_dev
, addr
, val16
);
549 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE
):
550 val16
= val
& ((1 << bits
) - 1);
551 addr
= adis16260_addresses
[chan
->address
][2];
552 return adis16260_spi_write_reg_16(indio_dev
, addr
, val16
);
557 static struct attribute
*adis16260_attributes
[] = {
558 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
559 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
560 &iio_dev_attr_reset
.dev_attr
.attr
,
564 static const struct attribute_group adis16260_attribute_group
= {
565 .attrs
= adis16260_attributes
,
568 static const struct iio_info adis16260_info
= {
569 .attrs
= &adis16260_attribute_group
,
570 .read_raw
= &adis16260_read_raw
,
571 .write_raw
= &adis16260_write_raw
,
572 .driver_module
= THIS_MODULE
,
575 static int __devinit
adis16260_probe(struct spi_device
*spi
)
577 int ret
, regdone
= 0;
578 struct adis16260_platform_data
*pd
= spi
->dev
.platform_data
;
579 struct adis16260_state
*st
;
580 struct iio_dev
*indio_dev
;
582 /* setup the industrialio driver allocated elements */
583 indio_dev
= iio_allocate_device(sizeof(*st
));
584 if (indio_dev
== NULL
) {
588 st
= iio_priv(indio_dev
);
590 st
->negate
= pd
->negate
;
591 /* this is only used for removal purposes */
592 spi_set_drvdata(spi
, st
);
595 mutex_init(&st
->buf_lock
);
597 indio_dev
->name
= spi_get_device_id(st
->us
)->name
;
598 indio_dev
->dev
.parent
= &spi
->dev
;
599 indio_dev
->info
= &adis16260_info
;
600 indio_dev
->num_channels
601 = ARRAY_SIZE(adis16260_channels_x
);
602 if (pd
&& pd
->direction
)
603 switch (pd
->direction
) {
605 indio_dev
->channels
= adis16260_channels_x
;
608 indio_dev
->channels
= adis16260_channels_y
;
611 indio_dev
->channels
= adis16260_channels_z
;
617 indio_dev
->channels
= adis16260_channels_x
;
618 indio_dev
->num_channels
= ARRAY_SIZE(adis16260_channels_x
);
619 indio_dev
->modes
= INDIO_DIRECT_MODE
;
621 ret
= adis16260_configure_ring(indio_dev
);
625 ret
= iio_device_register(indio_dev
);
627 goto error_unreg_ring_funcs
;
629 ret
= iio_ring_buffer_register_ex(indio_dev
->ring
, 0,
631 ARRAY_SIZE(adis16260_channels_x
));
633 printk(KERN_ERR
"failed to initialize the ring\n");
634 goto error_unreg_ring_funcs
;
638 ret
= adis16260_probe_trigger(indio_dev
);
640 goto error_uninitialize_ring
;
643 /* Get the device into a sane initial state */
644 ret
= adis16260_initial_setup(indio_dev
);
646 goto error_remove_trigger
;
649 error_remove_trigger
:
650 adis16260_remove_trigger(indio_dev
);
651 error_uninitialize_ring
:
652 iio_ring_buffer_unregister(indio_dev
->ring
);
653 error_unreg_ring_funcs
:
654 adis16260_unconfigure_ring(indio_dev
);
657 iio_device_unregister(indio_dev
);
659 iio_free_device(indio_dev
);
664 static int adis16260_remove(struct spi_device
*spi
)
667 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
669 ret
= adis16260_stop_device(indio_dev
);
673 flush_scheduled_work();
675 adis16260_remove_trigger(indio_dev
);
676 iio_ring_buffer_unregister(indio_dev
->ring
);
677 iio_device_unregister(indio_dev
);
678 adis16260_unconfigure_ring(indio_dev
);
685 * These parts do not need to be differentiated until someone adds
686 * support for the on chip filtering.
688 static const struct spi_device_id adis16260_id
[] = {
697 static struct spi_driver adis16260_driver
= {
700 .owner
= THIS_MODULE
,
702 .probe
= adis16260_probe
,
703 .remove
= __devexit_p(adis16260_remove
),
704 .id_table
= adis16260_id
,
707 static __init
int adis16260_init(void)
709 return spi_register_driver(&adis16260_driver
);
711 module_init(adis16260_init
);
713 static __exit
void adis16260_exit(void)
715 spi_unregister_driver(&adis16260_driver
);
717 module_exit(adis16260_exit
);
719 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
720 MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
721 MODULE_LICENSE("GPL v2");