2 * ADIS16350/54/55/60/62/64/65 high precision tri-axis inertial sensor
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 "../adc/adc.h"
26 #include "../gyro/gyro.h"
28 #include "adis16350.h"
30 #define DRIVER_NAME "adis16350"
32 static int adis16350_check_status(struct device
*dev
);
35 * adis16350_spi_write_reg_8() - write single byte to a register
36 * @dev: device associated with child of actual device (iio_dev or iio_trig)
37 * @reg_address: the address of the register to be written
38 * @val: the value to write
40 static int adis16350_spi_write_reg_8(struct device
*dev
,
45 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
46 struct adis16350_state
*st
= iio_dev_get_devdata(indio_dev
);
48 mutex_lock(&st
->buf_lock
);
49 st
->tx
[0] = ADIS16350_WRITE_REG(reg_address
);
52 ret
= spi_write(st
->us
, st
->tx
, 2);
53 mutex_unlock(&st
->buf_lock
);
59 * adis16350_spi_write_reg_16() - write 2 bytes to a pair of registers
60 * @dev: device associated with child of actual device (iio_dev or iio_trig)
61 * @reg_address: the address of the lower of the two registers. Second register
62 * is assumed to have address one greater.
63 * @val: value to be written
65 static int adis16350_spi_write_reg_16(struct device
*dev
,
70 struct spi_message msg
;
71 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
72 struct adis16350_state
*st
= iio_dev_get_devdata(indio_dev
);
73 struct spi_transfer xfers
[] = {
89 mutex_lock(&st
->buf_lock
);
90 st
->tx
[0] = ADIS16350_WRITE_REG(lower_reg_address
);
91 st
->tx
[1] = value
& 0xFF;
92 st
->tx
[2] = ADIS16350_WRITE_REG(lower_reg_address
+ 1);
93 st
->tx
[3] = (value
>> 8) & 0xFF;
95 spi_message_init(&msg
);
96 spi_message_add_tail(&xfers
[0], &msg
);
97 spi_message_add_tail(&xfers
[1], &msg
);
98 ret
= spi_sync(st
->us
, &msg
);
99 mutex_unlock(&st
->buf_lock
);
105 * adis16350_spi_read_reg_16() - read 2 bytes from a 16-bit register
106 * @dev: device associated with child of actual device (iio_dev or iio_trig)
107 * @reg_address: the address of the lower of the two registers. Second register
108 * is assumed to have address one greater.
109 * @val: somewhere to pass back the value read
111 static int adis16350_spi_read_reg_16(struct device
*dev
,
112 u8 lower_reg_address
,
115 struct spi_message msg
;
116 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
117 struct adis16350_state
*st
= iio_dev_get_devdata(indio_dev
);
119 struct spi_transfer xfers
[] = {
135 mutex_lock(&st
->buf_lock
);
136 st
->tx
[0] = ADIS16350_READ_REG(lower_reg_address
);
141 spi_message_init(&msg
);
142 spi_message_add_tail(&xfers
[0], &msg
);
143 spi_message_add_tail(&xfers
[1], &msg
);
144 ret
= spi_sync(st
->us
, &msg
);
146 dev_err(&st
->us
->dev
,
147 "problem when reading 16 bit register 0x%02X",
151 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
154 mutex_unlock(&st
->buf_lock
);
159 static ssize_t
adis16350_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
= adis16350_spi_read_reg_16(dev
, this_attr
->address
, (u16
*)&val
);
173 if (val
& ADIS16350_ERROR_ACTIVE
)
174 adis16350_check_status(dev
);
175 val
= ((s16
)(val
<< shift
) >> shift
);
176 return sprintf(buf
, "%d\n", val
);
179 static ssize_t
adis16350_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
= adis16350_spi_read_reg_16(dev
, this_attr
->address
, &val
);
191 if (val
& ADIS16350_ERROR_ACTIVE
)
192 adis16350_check_status(dev
);
194 return sprintf(buf
, "%u\n", val
& 0x0FFF);
197 static ssize_t
adis16350_read_14bit_signed(struct device
*dev
,
198 struct device_attribute
*attr
,
201 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
204 /* Take the iio_dev status lock */
205 mutex_lock(&indio_dev
->mlock
);
206 ret
= adis16350_spi_read_signed(dev
, attr
, buf
, 14);
207 mutex_unlock(&indio_dev
->mlock
);
212 static ssize_t
adis16350_read_12bit_signed(struct device
*dev
,
213 struct device_attribute
*attr
,
216 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
219 /* Take the iio_dev status lock */
220 mutex_lock(&indio_dev
->mlock
);
221 ret
= adis16350_spi_read_signed(dev
, attr
, buf
, 12);
222 mutex_unlock(&indio_dev
->mlock
);
227 static ssize_t
adis16350_write_16bit(struct device
*dev
,
228 struct device_attribute
*attr
,
232 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
236 ret
= strict_strtol(buf
, 10, &val
);
239 ret
= adis16350_spi_write_reg_16(dev
, this_attr
->address
, val
);
242 return ret
? ret
: len
;
245 static ssize_t
adis16350_read_frequency(struct device
*dev
,
246 struct device_attribute
*attr
,
252 ret
= adis16350_spi_read_reg_16(dev
,
257 sps
= (t
& ADIS16350_SMPL_PRD_TIME_BASE
) ? 53 : 1638;
258 sps
/= (t
& ADIS16350_SMPL_PRD_DIV_MASK
) + 1;
259 len
= sprintf(buf
, "%d SPS\n", sps
);
263 static ssize_t
adis16350_write_frequency(struct device
*dev
,
264 struct device_attribute
*attr
,
268 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
269 struct adis16350_state
*st
= iio_dev_get_devdata(indio_dev
);
274 ret
= strict_strtol(buf
, 10, &val
);
278 mutex_lock(&indio_dev
->mlock
);
283 t
&= ADIS16350_SMPL_PRD_DIV_MASK
;
284 if ((t
& ADIS16350_SMPL_PRD_DIV_MASK
) >= 0x0A)
285 st
->us
->max_speed_hz
= ADIS16350_SPI_SLOW
;
287 st
->us
->max_speed_hz
= ADIS16350_SPI_FAST
;
289 ret
= adis16350_spi_write_reg_8(dev
,
293 mutex_unlock(&indio_dev
->mlock
);
295 return ret
? ret
: len
;
298 static int adis16350_reset(struct device
*dev
)
301 ret
= adis16350_spi_write_reg_8(dev
,
303 ADIS16350_GLOB_CMD_SW_RESET
);
305 dev_err(dev
, "problem resetting device");
310 static ssize_t
adis16350_write_reset(struct device
*dev
,
311 struct device_attribute
*attr
,
312 const char *buf
, size_t len
)
320 return adis16350_reset(dev
);
325 int adis16350_set_irq(struct device
*dev
, bool enable
)
329 ret
= adis16350_spi_read_reg_16(dev
, ADIS16350_MSC_CTRL
, &msc
);
333 msc
|= ADIS16350_MSC_CTRL_DATA_RDY_POL_HIGH
;
334 msc
&= ~ADIS16350_MSC_CTRL_DATA_RDY_DIO2
;
337 msc
|= ADIS16350_MSC_CTRL_DATA_RDY_EN
;
339 msc
&= ~ADIS16350_MSC_CTRL_DATA_RDY_EN
;
341 ret
= adis16350_spi_write_reg_16(dev
, ADIS16350_MSC_CTRL
, msc
);
349 /* Power down the device */
350 static int adis16350_stop_device(struct device
*dev
)
353 u16 val
= ADIS16350_SLP_CNT_POWER_OFF
;
355 ret
= adis16350_spi_write_reg_16(dev
, ADIS16350_SLP_CNT
, val
);
357 dev_err(dev
, "problem with turning device off: SLP_CNT");
362 static int adis16350_self_test(struct device
*dev
)
365 ret
= adis16350_spi_write_reg_16(dev
,
367 ADIS16350_MSC_CTRL_MEM_TEST
);
369 dev_err(dev
, "problem starting self test");
373 adis16350_check_status(dev
);
379 static int adis16350_check_status(struct device
*dev
)
384 ret
= adis16350_spi_read_reg_16(dev
, ADIS16350_DIAG_STAT
, &status
);
387 dev_err(dev
, "Reading status failed\n");
391 if (status
& ADIS16350_DIAG_STAT_ZACCL_FAIL
)
392 dev_err(dev
, "Z-axis accelerometer self-test failure\n");
393 if (status
& ADIS16350_DIAG_STAT_YACCL_FAIL
)
394 dev_err(dev
, "Y-axis accelerometer self-test failure\n");
395 if (status
& ADIS16350_DIAG_STAT_XACCL_FAIL
)
396 dev_err(dev
, "X-axis accelerometer self-test failure\n");
397 if (status
& ADIS16350_DIAG_STAT_XGYRO_FAIL
)
398 dev_err(dev
, "X-axis gyroscope self-test failure\n");
399 if (status
& ADIS16350_DIAG_STAT_YGYRO_FAIL
)
400 dev_err(dev
, "Y-axis gyroscope self-test failure\n");
401 if (status
& ADIS16350_DIAG_STAT_ZGYRO_FAIL
)
402 dev_err(dev
, "Z-axis gyroscope self-test failure\n");
403 if (status
& ADIS16350_DIAG_STAT_ALARM2
)
404 dev_err(dev
, "Alarm 2 active\n");
405 if (status
& ADIS16350_DIAG_STAT_ALARM1
)
406 dev_err(dev
, "Alarm 1 active\n");
407 if (status
& ADIS16350_DIAG_STAT_FLASH_CHK
)
408 dev_err(dev
, "Flash checksum error\n");
409 if (status
& ADIS16350_DIAG_STAT_SELF_TEST
)
410 dev_err(dev
, "Self test error\n");
411 if (status
& ADIS16350_DIAG_STAT_OVERFLOW
)
412 dev_err(dev
, "Sensor overrange\n");
413 if (status
& ADIS16350_DIAG_STAT_SPI_FAIL
)
414 dev_err(dev
, "SPI failure\n");
415 if (status
& ADIS16350_DIAG_STAT_FLASH_UPT
)
416 dev_err(dev
, "Flash update failed\n");
417 if (status
& ADIS16350_DIAG_STAT_POWER_HIGH
)
418 dev_err(dev
, "Power supply above 5.25V\n");
419 if (status
& ADIS16350_DIAG_STAT_POWER_LOW
)
420 dev_err(dev
, "Power supply below 4.75V\n");
426 static int adis16350_initial_setup(struct adis16350_state
*st
)
430 struct device
*dev
= &st
->indio_dev
->dev
;
432 /* use low spi speed for init */
433 st
->us
->max_speed_hz
= ADIS16350_SPI_SLOW
;
434 st
->us
->mode
= SPI_MODE_3
;
438 ret
= adis16350_set_irq(dev
, false);
440 dev_err(dev
, "disable irq failed");
445 ret
= adis16350_self_test(dev
);
447 dev_err(dev
, "self test failure");
451 /* Read status register to check the result */
452 ret
= adis16350_check_status(dev
);
454 adis16350_reset(dev
);
455 dev_err(dev
, "device not playing ball -> reset");
456 msleep(ADIS16350_STARTUP_DELAY
);
457 ret
= adis16350_check_status(dev
);
459 dev_err(dev
, "giving up");
464 printk(KERN_INFO DRIVER_NAME
": at CS%d (irq %d)\n",
465 st
->us
->chip_select
, st
->us
->irq
);
467 /* use high spi speed if possible */
468 ret
= adis16350_spi_read_reg_16(dev
, ADIS16350_SMPL_PRD
, &smp_prd
);
469 if (!ret
&& (smp_prd
& ADIS16350_SMPL_PRD_DIV_MASK
) < 0x0A) {
470 st
->us
->max_speed_hz
= ADIS16350_SPI_SLOW
;
478 static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR
| S_IRUGO
,
479 adis16350_read_12bit_signed
,
480 adis16350_write_16bit
,
481 ADIS16350_XACCL_OFF
);
483 static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR
| S_IRUGO
,
484 adis16350_read_12bit_signed
,
485 adis16350_write_16bit
,
486 ADIS16350_YACCL_OFF
);
488 static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR
| S_IRUGO
,
489 adis16350_read_12bit_signed
,
490 adis16350_write_16bit
,
491 ADIS16350_ZACCL_OFF
);
493 static IIO_DEV_ATTR_IN_NAMED_RAW(supply
, adis16350_read_12bit_unsigned
,
494 ADIS16350_SUPPLY_OUT
);
495 static IIO_CONST_ATTR(in_supply_scale
, "0.002418");
497 static IIO_DEV_ATTR_GYRO_X(adis16350_read_14bit_signed
,
498 ADIS16350_XGYRO_OUT
);
499 static IIO_DEV_ATTR_GYRO_Y(adis16350_read_14bit_signed
,
500 ADIS16350_YGYRO_OUT
);
501 static IIO_DEV_ATTR_GYRO_Z(adis16350_read_14bit_signed
,
502 ADIS16350_ZGYRO_OUT
);
503 static IIO_CONST_ATTR(gyro_scale
, "0.05");
505 static IIO_DEV_ATTR_ACCEL_X(adis16350_read_14bit_signed
,
506 ADIS16350_XACCL_OUT
);
507 static IIO_DEV_ATTR_ACCEL_Y(adis16350_read_14bit_signed
,
508 ADIS16350_YACCL_OUT
);
509 static IIO_DEV_ATTR_ACCEL_Z(adis16350_read_14bit_signed
,
510 ADIS16350_ZACCL_OUT
);
511 static IIO_CONST_ATTR(accel_scale
, "0.00333");
513 static IIO_DEVICE_ATTR(temp_x_raw
, S_IRUGO
, adis16350_read_12bit_signed
,
514 NULL
, ADIS16350_XTEMP_OUT
);
515 static IIO_DEVICE_ATTR(temp_y_raw
, S_IRUGO
, adis16350_read_12bit_signed
,
516 NULL
, ADIS16350_YTEMP_OUT
);
517 static IIO_DEVICE_ATTR(temp_z_raw
, S_IRUGO
, adis16350_read_12bit_signed
,
518 NULL
, ADIS16350_ZTEMP_OUT
);
519 static IIO_CONST_ATTR(temp_scale
, "0.0005");
521 static IIO_DEV_ATTR_IN_RAW(0, adis16350_read_12bit_unsigned
,
523 static IIO_CONST_ATTR(in0_scale
, "0.000806");
525 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
526 adis16350_read_frequency
,
527 adis16350_write_frequency
);
529 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
,
530 adis16350_write_reset
, 0);
532 static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("409 546 819 1638");
534 static IIO_CONST_ATTR(name
, "adis16350");
536 static struct attribute
*adis16350_attributes
[] = {
537 &iio_dev_attr_accel_x_offset
.dev_attr
.attr
,
538 &iio_dev_attr_accel_y_offset
.dev_attr
.attr
,
539 &iio_dev_attr_accel_z_offset
.dev_attr
.attr
,
540 &iio_dev_attr_in_supply_raw
.dev_attr
.attr
,
541 &iio_const_attr_in_supply_scale
.dev_attr
.attr
,
542 &iio_dev_attr_gyro_x_raw
.dev_attr
.attr
,
543 &iio_dev_attr_gyro_y_raw
.dev_attr
.attr
,
544 &iio_dev_attr_gyro_z_raw
.dev_attr
.attr
,
545 &iio_const_attr_gyro_scale
.dev_attr
.attr
,
546 &iio_dev_attr_accel_x_raw
.dev_attr
.attr
,
547 &iio_dev_attr_accel_y_raw
.dev_attr
.attr
,
548 &iio_dev_attr_accel_z_raw
.dev_attr
.attr
,
549 &iio_const_attr_accel_scale
.dev_attr
.attr
,
550 &iio_dev_attr_temp_x_raw
.dev_attr
.attr
,
551 &iio_dev_attr_temp_y_raw
.dev_attr
.attr
,
552 &iio_dev_attr_temp_z_raw
.dev_attr
.attr
,
553 &iio_const_attr_temp_scale
.dev_attr
.attr
,
554 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
555 &iio_const_attr_in0_scale
.dev_attr
.attr
,
556 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
557 &iio_const_attr_available_sampling_frequency
.dev_attr
.attr
,
558 &iio_dev_attr_reset
.dev_attr
.attr
,
559 &iio_const_attr_name
.dev_attr
.attr
,
563 static const struct attribute_group adis16350_attribute_group
= {
564 .attrs
= adis16350_attributes
,
567 static struct attribute
*adis16350_event_attributes
[] = {
571 static struct attribute_group adis16350_event_attribute_group
= {
572 .attrs
= adis16350_event_attributes
,
575 static int __devinit
adis16350_probe(struct spi_device
*spi
)
577 int ret
, regdone
= 0;
578 struct adis16350_state
*st
= kzalloc(sizeof *st
, GFP_KERNEL
);
583 /* this is only used for removal purposes */
584 spi_set_drvdata(spi
, st
);
586 /* Allocate the comms buffers */
587 st
->rx
= kzalloc(sizeof(*st
->rx
)*ADIS16350_MAX_RX
, GFP_KERNEL
);
588 if (st
->rx
== NULL
) {
592 st
->tx
= kzalloc(sizeof(*st
->tx
)*ADIS16350_MAX_TX
, GFP_KERNEL
);
593 if (st
->tx
== NULL
) {
598 mutex_init(&st
->buf_lock
);
599 /* setup the industrialio driver allocated elements */
600 st
->indio_dev
= iio_allocate_device();
601 if (st
->indio_dev
== NULL
) {
606 st
->indio_dev
->dev
.parent
= &spi
->dev
;
607 st
->indio_dev
->num_interrupt_lines
= 1;
608 st
->indio_dev
->event_attrs
= &adis16350_event_attribute_group
;
609 st
->indio_dev
->attrs
= &adis16350_attribute_group
;
610 st
->indio_dev
->dev_data
= (void *)(st
);
611 st
->indio_dev
->driver_module
= THIS_MODULE
;
612 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
614 ret
= adis16350_configure_ring(st
->indio_dev
);
618 ret
= iio_device_register(st
->indio_dev
);
620 goto error_unreg_ring_funcs
;
623 ret
= iio_ring_buffer_register(st
->indio_dev
->ring
, 0);
625 printk(KERN_ERR
"failed to initialize the ring\n");
626 goto error_unreg_ring_funcs
;
630 ret
= iio_register_interrupt_line(spi
->irq
,
636 goto error_uninitialize_ring
;
638 ret
= adis16350_probe_trigger(st
->indio_dev
);
640 goto error_unregister_line
;
643 /* Get the device into a sane initial state */
644 ret
= adis16350_initial_setup(st
);
646 goto error_remove_trigger
;
649 error_remove_trigger
:
650 adis16350_remove_trigger(st
->indio_dev
);
651 error_unregister_line
:
653 iio_unregister_interrupt_line(st
->indio_dev
, 0);
654 error_uninitialize_ring
:
655 iio_ring_buffer_unregister(st
->indio_dev
->ring
);
656 error_unreg_ring_funcs
:
657 adis16350_unconfigure_ring(st
->indio_dev
);
660 iio_device_unregister(st
->indio_dev
);
662 iio_free_device(st
->indio_dev
);
673 static int adis16350_remove(struct spi_device
*spi
)
676 struct adis16350_state
*st
= spi_get_drvdata(spi
);
677 struct iio_dev
*indio_dev
= st
->indio_dev
;
679 ret
= adis16350_stop_device(&(indio_dev
->dev
));
683 flush_scheduled_work();
685 adis16350_remove_trigger(indio_dev
);
687 iio_unregister_interrupt_line(indio_dev
, 0);
689 iio_ring_buffer_unregister(indio_dev
->ring
);
690 iio_device_unregister(indio_dev
);
691 adis16350_unconfigure_ring(indio_dev
);
702 static const struct spi_device_id adis16350_id
[] = {
713 static struct spi_driver adis16350_driver
= {
716 .owner
= THIS_MODULE
,
718 .probe
= adis16350_probe
,
719 .remove
= __devexit_p(adis16350_remove
),
720 .id_table
= adis16350_id
,
723 static __init
int adis16350_init(void)
725 return spi_register_driver(&adis16350_driver
);
727 module_init(adis16350_init
);
729 static __exit
void adis16350_exit(void)
731 spi_unregister_driver(&adis16350_driver
);
733 module_exit(adis16350_exit
);
735 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
736 MODULE_DESCRIPTION("Analog Devices ADIS16350/54/55/60/62/64/65 IMU SPI driver");
737 MODULE_LICENSE("GPL v2");