2 * ADIS16300 Four Degrees of Freedom Inertial 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 "../accel/accel.h"
25 #include "../accel/inclinometer.h"
26 #include "../gyro/gyro.h"
27 #include "../adc/adc.h"
29 #include "adis16300.h"
31 #define DRIVER_NAME "adis16300"
33 static int adis16300_check_status(struct device
*dev
);
36 * adis16300_spi_write_reg_8() - write single byte to a register
37 * @dev: device associated with child of actual device (iio_dev or iio_trig)
38 * @reg_address: the address of the register to be written
39 * @val: the value to write
41 static int adis16300_spi_write_reg_8(struct device
*dev
,
46 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
47 struct adis16300_state
*st
= iio_dev_get_devdata(indio_dev
);
49 mutex_lock(&st
->buf_lock
);
50 st
->tx
[0] = ADIS16300_WRITE_REG(reg_address
);
53 ret
= spi_write(st
->us
, st
->tx
, 2);
54 mutex_unlock(&st
->buf_lock
);
60 * adis16300_spi_write_reg_16() - write 2 bytes to a pair of registers
61 * @dev: device associated with child of actual device (iio_dev or iio_trig)
62 * @reg_address: the address of the lower of the two registers. Second register
63 * is assumed to have address one greater.
64 * @val: value to be written
66 static int adis16300_spi_write_reg_16(struct device
*dev
,
71 struct spi_message msg
;
72 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
73 struct adis16300_state
*st
= iio_dev_get_devdata(indio_dev
);
74 struct spi_transfer xfers
[] = {
90 mutex_lock(&st
->buf_lock
);
91 st
->tx
[0] = ADIS16300_WRITE_REG(lower_reg_address
);
92 st
->tx
[1] = value
& 0xFF;
93 st
->tx
[2] = ADIS16300_WRITE_REG(lower_reg_address
+ 1);
94 st
->tx
[3] = (value
>> 8) & 0xFF;
96 spi_message_init(&msg
);
97 spi_message_add_tail(&xfers
[0], &msg
);
98 spi_message_add_tail(&xfers
[1], &msg
);
99 ret
= spi_sync(st
->us
, &msg
);
100 mutex_unlock(&st
->buf_lock
);
106 * adis16300_spi_read_reg_16() - read 2 bytes from a 16-bit register
107 * @dev: device associated with child of actual device (iio_dev or iio_trig)
108 * @reg_address: the address of the lower of the two registers. Second register
109 * is assumed to have address one greater.
110 * @val: somewhere to pass back the value read
112 static int adis16300_spi_read_reg_16(struct device
*dev
,
113 u8 lower_reg_address
,
116 struct spi_message msg
;
117 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
118 struct adis16300_state
*st
= iio_dev_get_devdata(indio_dev
);
120 struct spi_transfer xfers
[] = {
136 mutex_lock(&st
->buf_lock
);
137 st
->tx
[0] = ADIS16300_READ_REG(lower_reg_address
);
142 spi_message_init(&msg
);
143 spi_message_add_tail(&xfers
[0], &msg
);
144 spi_message_add_tail(&xfers
[1], &msg
);
145 ret
= spi_sync(st
->us
, &msg
);
147 dev_err(&st
->us
->dev
,
148 "problem when reading 16 bit register 0x%02X",
152 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
155 mutex_unlock(&st
->buf_lock
);
159 static ssize_t
adis16300_spi_read_signed(struct device
*dev
,
160 struct device_attribute
*attr
,
166 unsigned shift
= 16 - bits
;
167 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
169 ret
= adis16300_spi_read_reg_16(dev
, this_attr
->address
, (u16
*)&val
);
173 if (val
& ADIS16300_ERROR_ACTIVE
)
174 adis16300_check_status(dev
);
175 val
= ((s16
)(val
<< shift
) >> shift
);
176 return sprintf(buf
, "%d\n", val
);
179 static ssize_t
adis16300_read_12bit_unsigned(struct device
*dev
,
180 struct device_attribute
*attr
,
185 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
187 ret
= adis16300_spi_read_reg_16(dev
, this_attr
->address
, &val
);
191 if (val
& ADIS16300_ERROR_ACTIVE
)
192 adis16300_check_status(dev
);
194 return sprintf(buf
, "%u\n", val
& 0x0FFF);
197 static ssize_t
adis16300_read_14bit_unsigned(struct device
*dev
,
198 struct device_attribute
*attr
,
203 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
205 ret
= adis16300_spi_read_reg_16(dev
, this_attr
->address
, &val
);
209 if (val
& ADIS16300_ERROR_ACTIVE
)
210 adis16300_check_status(dev
);
212 return sprintf(buf
, "%u\n", val
& 0x3FFF);
215 static ssize_t
adis16300_read_14bit_signed(struct device
*dev
,
216 struct device_attribute
*attr
,
219 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
222 /* Take the iio_dev status lock */
223 mutex_lock(&indio_dev
->mlock
);
224 ret
= adis16300_spi_read_signed(dev
, attr
, buf
, 14);
225 mutex_unlock(&indio_dev
->mlock
);
230 static ssize_t
adis16300_read_12bit_signed(struct device
*dev
,
231 struct device_attribute
*attr
,
234 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
237 /* Take the iio_dev status lock */
238 mutex_lock(&indio_dev
->mlock
);
239 ret
= adis16300_spi_read_signed(dev
, attr
, buf
, 12);
240 mutex_unlock(&indio_dev
->mlock
);
245 static ssize_t
adis16300_read_13bit_signed(struct device
*dev
,
246 struct device_attribute
*attr
,
249 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
252 /* Take the iio_dev status lock */
253 mutex_lock(&indio_dev
->mlock
);
254 ret
= adis16300_spi_read_signed(dev
, attr
, buf
, 13);
255 mutex_unlock(&indio_dev
->mlock
);
260 static ssize_t
adis16300_write_16bit(struct device
*dev
,
261 struct device_attribute
*attr
,
265 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
269 ret
= strict_strtol(buf
, 10, &val
);
272 ret
= adis16300_spi_write_reg_16(dev
, this_attr
->address
, val
);
275 return ret
? ret
: len
;
278 static ssize_t
adis16300_read_frequency(struct device
*dev
,
279 struct device_attribute
*attr
,
285 ret
= adis16300_spi_read_reg_16(dev
,
290 sps
= (t
& ADIS16300_SMPL_PRD_TIME_BASE
) ? 53 : 1638;
291 sps
/= (t
& ADIS16300_SMPL_PRD_DIV_MASK
) + 1;
292 len
= sprintf(buf
, "%d SPS\n", sps
);
296 static ssize_t
adis16300_write_frequency(struct device
*dev
,
297 struct device_attribute
*attr
,
301 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
302 struct adis16300_state
*st
= iio_dev_get_devdata(indio_dev
);
307 ret
= strict_strtol(buf
, 10, &val
);
311 mutex_lock(&indio_dev
->mlock
);
316 t
&= ADIS16300_SMPL_PRD_DIV_MASK
;
317 if ((t
& ADIS16300_SMPL_PRD_DIV_MASK
) >= 0x0A)
318 st
->us
->max_speed_hz
= ADIS16300_SPI_SLOW
;
320 st
->us
->max_speed_hz
= ADIS16300_SPI_FAST
;
322 ret
= adis16300_spi_write_reg_8(dev
,
326 mutex_unlock(&indio_dev
->mlock
);
328 return ret
? ret
: len
;
331 static int adis16300_reset(struct device
*dev
)
334 ret
= adis16300_spi_write_reg_8(dev
,
336 ADIS16300_GLOB_CMD_SW_RESET
);
338 dev_err(dev
, "problem resetting device");
343 static ssize_t
adis16300_write_reset(struct device
*dev
,
344 struct device_attribute
*attr
,
345 const char *buf
, size_t len
)
353 return adis16300_reset(dev
);
358 int adis16300_set_irq(struct device
*dev
, bool enable
)
362 ret
= adis16300_spi_read_reg_16(dev
, ADIS16300_MSC_CTRL
, &msc
);
366 msc
|= ADIS16300_MSC_CTRL_DATA_RDY_POL_HIGH
;
367 msc
&= ~ADIS16300_MSC_CTRL_DATA_RDY_DIO2
;
369 msc
|= ADIS16300_MSC_CTRL_DATA_RDY_EN
;
371 msc
&= ~ADIS16300_MSC_CTRL_DATA_RDY_EN
;
373 ret
= adis16300_spi_write_reg_16(dev
, ADIS16300_MSC_CTRL
, msc
);
381 /* Power down the device */
382 static int adis16300_stop_device(struct device
*dev
)
385 u16 val
= ADIS16300_SLP_CNT_POWER_OFF
;
387 ret
= adis16300_spi_write_reg_16(dev
, ADIS16300_SLP_CNT
, val
);
389 dev_err(dev
, "problem with turning device off: SLP_CNT");
394 static int adis16300_self_test(struct device
*dev
)
397 ret
= adis16300_spi_write_reg_16(dev
,
399 ADIS16300_MSC_CTRL_MEM_TEST
);
401 dev_err(dev
, "problem starting self test");
405 adis16300_check_status(dev
);
411 static int adis16300_check_status(struct device
*dev
)
416 ret
= adis16300_spi_read_reg_16(dev
, ADIS16300_DIAG_STAT
, &status
);
419 dev_err(dev
, "Reading status failed\n");
423 if (status
& ADIS16300_DIAG_STAT_ZACCL_FAIL
)
424 dev_err(dev
, "Z-axis accelerometer self-test failure\n");
425 if (status
& ADIS16300_DIAG_STAT_YACCL_FAIL
)
426 dev_err(dev
, "Y-axis accelerometer self-test failure\n");
427 if (status
& ADIS16300_DIAG_STAT_XACCL_FAIL
)
428 dev_err(dev
, "X-axis accelerometer self-test failure\n");
429 if (status
& ADIS16300_DIAG_STAT_XGYRO_FAIL
)
430 dev_err(dev
, "X-axis gyroscope self-test failure\n");
431 if (status
& ADIS16300_DIAG_STAT_ALARM2
)
432 dev_err(dev
, "Alarm 2 active\n");
433 if (status
& ADIS16300_DIAG_STAT_ALARM1
)
434 dev_err(dev
, "Alarm 1 active\n");
435 if (status
& ADIS16300_DIAG_STAT_FLASH_CHK
)
436 dev_err(dev
, "Flash checksum error\n");
437 if (status
& ADIS16300_DIAG_STAT_SELF_TEST
)
438 dev_err(dev
, "Self test error\n");
439 if (status
& ADIS16300_DIAG_STAT_OVERFLOW
)
440 dev_err(dev
, "Sensor overrange\n");
441 if (status
& ADIS16300_DIAG_STAT_SPI_FAIL
)
442 dev_err(dev
, "SPI failure\n");
443 if (status
& ADIS16300_DIAG_STAT_FLASH_UPT
)
444 dev_err(dev
, "Flash update failed\n");
445 if (status
& ADIS16300_DIAG_STAT_POWER_HIGH
)
446 dev_err(dev
, "Power supply above 5.25V\n");
447 if (status
& ADIS16300_DIAG_STAT_POWER_LOW
)
448 dev_err(dev
, "Power supply below 4.75V\n");
454 static int adis16300_initial_setup(struct adis16300_state
*st
)
458 struct device
*dev
= &st
->indio_dev
->dev
;
460 /* use low spi speed for init */
461 st
->us
->max_speed_hz
= ADIS16300_SPI_SLOW
;
462 st
->us
->mode
= SPI_MODE_3
;
466 ret
= adis16300_set_irq(dev
, false);
468 dev_err(dev
, "disable irq failed");
473 ret
= adis16300_self_test(dev
);
475 dev_err(dev
, "self test failure");
479 /* Read status register to check the result */
480 ret
= adis16300_check_status(dev
);
482 adis16300_reset(dev
);
483 dev_err(dev
, "device not playing ball -> reset");
484 msleep(ADIS16300_STARTUP_DELAY
);
485 ret
= adis16300_check_status(dev
);
487 dev_err(dev
, "giving up");
492 printk(KERN_INFO DRIVER_NAME
": at CS%d (irq %d)\n",
493 st
->us
->chip_select
, st
->us
->irq
);
495 /* use high spi speed if possible */
496 ret
= adis16300_spi_read_reg_16(dev
, ADIS16300_SMPL_PRD
, &smp_prd
);
497 if (!ret
&& (smp_prd
& ADIS16300_SMPL_PRD_DIV_MASK
) < 0x0A) {
498 st
->us
->max_speed_hz
= ADIS16300_SPI_SLOW
;
506 static IIO_DEV_ATTR_GYRO_X_CALIBBIAS(S_IWUSR
| S_IRUGO
,
507 adis16300_read_12bit_signed
,
508 adis16300_write_16bit
,
509 ADIS16300_XGYRO_OFF
);
511 static IIO_DEV_ATTR_ACCEL_X_CALIBBIAS(S_IWUSR
| S_IRUGO
,
512 adis16300_read_12bit_signed
,
513 adis16300_write_16bit
,
514 ADIS16300_XACCL_OFF
);
516 static IIO_DEV_ATTR_ACCEL_Y_CALIBBIAS(S_IWUSR
| S_IRUGO
,
517 adis16300_read_12bit_signed
,
518 adis16300_write_16bit
,
519 ADIS16300_YACCL_OFF
);
521 static IIO_DEV_ATTR_ACCEL_Z_CALIBBIAS(S_IWUSR
| S_IRUGO
,
522 adis16300_read_12bit_signed
,
523 adis16300_write_16bit
,
524 ADIS16300_ZACCL_OFF
);
526 static IIO_DEV_ATTR_IN_NAMED_RAW(supply
, adis16300_read_14bit_unsigned
,
527 ADIS16300_SUPPLY_OUT
);
528 static IIO_CONST_ATTR_IN_NAMED_SCALE(supply
, "0.00242");
530 static IIO_DEV_ATTR_GYRO_X(adis16300_read_14bit_signed
,
531 ADIS16300_XGYRO_OUT
);
532 static IIO_CONST_ATTR_GYRO_SCALE("0.000872664");
534 static IIO_DEV_ATTR_ACCEL_X(adis16300_read_14bit_signed
,
535 ADIS16300_XACCL_OUT
);
536 static IIO_DEV_ATTR_ACCEL_Y(adis16300_read_14bit_signed
,
537 ADIS16300_YACCL_OUT
);
538 static IIO_DEV_ATTR_ACCEL_Z(adis16300_read_14bit_signed
,
539 ADIS16300_ZACCL_OUT
);
540 static IIO_CONST_ATTR_ACCEL_SCALE("0.00588399");
542 static IIO_DEV_ATTR_INCLI_X(adis16300_read_13bit_signed
,
543 ADIS16300_XINCLI_OUT
);
544 static IIO_DEV_ATTR_INCLI_Y(adis16300_read_13bit_signed
,
545 ADIS16300_YINCLI_OUT
);
546 static IIO_CONST_ATTR_INCLI_SCALE("0.00076794487");
548 static IIO_DEV_ATTR_TEMP_RAW(adis16300_read_12bit_unsigned
);
549 static IIO_CONST_ATTR_TEMP_OFFSET("198.16");
550 static IIO_CONST_ATTR_TEMP_SCALE("0.14");
552 static IIO_DEV_ATTR_IN_RAW(0, adis16300_read_12bit_unsigned
,
554 static IIO_CONST_ATTR(in0_scale
, "0.000806");
556 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
557 adis16300_read_frequency
,
558 adis16300_write_frequency
);
560 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16300_write_reset
, 0);
562 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("409 546 819 1638");
564 static IIO_CONST_ATTR_NAME("adis16300");
566 static struct attribute
*adis16300_event_attributes
[] = {
570 static struct attribute_group adis16300_event_attribute_group
= {
571 .attrs
= adis16300_event_attributes
,
574 static struct attribute
*adis16300_attributes
[] = {
575 &iio_dev_attr_gyro_x_calibbias
.dev_attr
.attr
,
576 &iio_dev_attr_accel_x_calibbias
.dev_attr
.attr
,
577 &iio_dev_attr_accel_y_calibbias
.dev_attr
.attr
,
578 &iio_dev_attr_accel_z_calibbias
.dev_attr
.attr
,
579 &iio_dev_attr_in_supply_raw
.dev_attr
.attr
,
580 &iio_const_attr_in_supply_scale
.dev_attr
.attr
,
581 &iio_dev_attr_gyro_x_raw
.dev_attr
.attr
,
582 &iio_const_attr_gyro_scale
.dev_attr
.attr
,
583 &iio_dev_attr_accel_x_raw
.dev_attr
.attr
,
584 &iio_dev_attr_accel_y_raw
.dev_attr
.attr
,
585 &iio_dev_attr_accel_z_raw
.dev_attr
.attr
,
586 &iio_const_attr_accel_scale
.dev_attr
.attr
,
587 &iio_dev_attr_incli_x_raw
.dev_attr
.attr
,
588 &iio_dev_attr_incli_y_raw
.dev_attr
.attr
,
589 &iio_const_attr_incli_scale
.dev_attr
.attr
,
590 &iio_dev_attr_temp_raw
.dev_attr
.attr
,
591 &iio_const_attr_temp_offset
.dev_attr
.attr
,
592 &iio_const_attr_temp_scale
.dev_attr
.attr
,
593 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
594 &iio_const_attr_in0_scale
.dev_attr
.attr
,
595 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
596 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
597 &iio_dev_attr_reset
.dev_attr
.attr
,
598 &iio_const_attr_name
.dev_attr
.attr
,
602 static const struct attribute_group adis16300_attribute_group
= {
603 .attrs
= adis16300_attributes
,
606 static int __devinit
adis16300_probe(struct spi_device
*spi
)
608 int ret
, regdone
= 0;
609 struct adis16300_state
*st
= kzalloc(sizeof *st
, GFP_KERNEL
);
614 /* this is only used for removal purposes */
615 spi_set_drvdata(spi
, st
);
617 /* Allocate the comms buffers */
618 st
->rx
= kzalloc(sizeof(*st
->rx
)*ADIS16300_MAX_RX
, GFP_KERNEL
);
619 if (st
->rx
== NULL
) {
623 st
->tx
= kzalloc(sizeof(*st
->tx
)*ADIS16300_MAX_TX
, GFP_KERNEL
);
624 if (st
->tx
== NULL
) {
629 mutex_init(&st
->buf_lock
);
630 /* setup the industrialio driver allocated elements */
631 st
->indio_dev
= iio_allocate_device();
632 if (st
->indio_dev
== NULL
) {
637 st
->indio_dev
->dev
.parent
= &spi
->dev
;
638 st
->indio_dev
->num_interrupt_lines
= 1;
639 st
->indio_dev
->event_attrs
= &adis16300_event_attribute_group
;
640 st
->indio_dev
->attrs
= &adis16300_attribute_group
;
641 st
->indio_dev
->dev_data
= (void *)(st
);
642 st
->indio_dev
->driver_module
= THIS_MODULE
;
643 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
645 ret
= adis16300_configure_ring(st
->indio_dev
);
649 ret
= iio_device_register(st
->indio_dev
);
651 goto error_unreg_ring_funcs
;
654 ret
= iio_ring_buffer_register(st
->indio_dev
->ring
, 0);
656 printk(KERN_ERR
"failed to initialize the ring\n");
657 goto error_unreg_ring_funcs
;
661 ret
= iio_register_interrupt_line(spi
->irq
,
667 goto error_uninitialize_ring
;
669 ret
= adis16300_probe_trigger(st
->indio_dev
);
671 goto error_unregister_line
;
674 /* Get the device into a sane initial state */
675 ret
= adis16300_initial_setup(st
);
677 goto error_remove_trigger
;
680 error_remove_trigger
:
681 adis16300_remove_trigger(st
->indio_dev
);
682 error_unregister_line
:
684 iio_unregister_interrupt_line(st
->indio_dev
, 0);
685 error_uninitialize_ring
:
686 iio_ring_buffer_unregister(st
->indio_dev
->ring
);
687 error_unreg_ring_funcs
:
688 adis16300_unconfigure_ring(st
->indio_dev
);
691 iio_device_unregister(st
->indio_dev
);
693 iio_free_device(st
->indio_dev
);
704 static int adis16300_remove(struct spi_device
*spi
)
707 struct adis16300_state
*st
= spi_get_drvdata(spi
);
708 struct iio_dev
*indio_dev
= st
->indio_dev
;
710 ret
= adis16300_stop_device(&(indio_dev
->dev
));
714 flush_scheduled_work();
716 adis16300_remove_trigger(indio_dev
);
718 iio_unregister_interrupt_line(indio_dev
, 0);
720 iio_ring_buffer_unregister(indio_dev
->ring
);
721 iio_device_unregister(indio_dev
);
722 adis16300_unconfigure_ring(indio_dev
);
733 static struct spi_driver adis16300_driver
= {
736 .owner
= THIS_MODULE
,
738 .probe
= adis16300_probe
,
739 .remove
= __devexit_p(adis16300_remove
),
742 static __init
int adis16300_init(void)
744 return spi_register_driver(&adis16300_driver
);
746 module_init(adis16300_init
);
748 static __exit
void adis16300_exit(void)
750 spi_unregister_driver(&adis16300_driver
);
752 module_exit(adis16300_exit
);
754 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
755 MODULE_DESCRIPTION("Analog Devices ADIS16300 IMU SPI driver");
756 MODULE_LICENSE("GPL v2");