2 * ADIS16209 Programmable Digital Vibration 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"
25 #include "inclinometer.h"
26 #include "../gyro/gyro.h"
27 #include "../adc/adc.h"
29 #include "adis16209.h"
31 #define DRIVER_NAME "adis16209"
33 static int adis16209_check_status(struct device
*dev
);
36 * adis16209_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 adis16209_spi_write_reg_8(struct device
*dev
,
46 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
47 struct adis16209_state
*st
= iio_dev_get_devdata(indio_dev
);
49 mutex_lock(&st
->buf_lock
);
50 st
->tx
[0] = ADIS16209_WRITE_REG(reg_address
);
53 ret
= spi_write(st
->us
, st
->tx
, 2);
54 mutex_unlock(&st
->buf_lock
);
60 * adis16209_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 adis16209_spi_write_reg_16(struct device
*dev
,
71 struct spi_message msg
;
72 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
73 struct adis16209_state
*st
= iio_dev_get_devdata(indio_dev
);
74 struct spi_transfer xfers
[] = {
90 mutex_lock(&st
->buf_lock
);
91 st
->tx
[0] = ADIS16209_WRITE_REG(lower_reg_address
);
92 st
->tx
[1] = value
& 0xFF;
93 st
->tx
[2] = ADIS16209_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 * adis16209_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 adis16209_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 adis16209_state
*st
= iio_dev_get_devdata(indio_dev
);
120 struct spi_transfer xfers
[] = {
136 mutex_lock(&st
->buf_lock
);
137 st
->tx
[0] = ADIS16209_READ_REG(lower_reg_address
);
140 spi_message_init(&msg
);
141 spi_message_add_tail(&xfers
[0], &msg
);
142 spi_message_add_tail(&xfers
[1], &msg
);
143 ret
= spi_sync(st
->us
, &msg
);
145 dev_err(&st
->us
->dev
,
146 "problem when reading 16 bit register 0x%02X",
150 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
153 mutex_unlock(&st
->buf_lock
);
157 static ssize_t
adis16209_read_12bit_unsigned(struct device
*dev
,
158 struct device_attribute
*attr
,
163 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
165 ret
= adis16209_spi_read_reg_16(dev
, this_attr
->address
, &val
);
169 if (val
& ADIS16209_ERROR_ACTIVE
)
170 adis16209_check_status(dev
);
172 return sprintf(buf
, "%u\n", val
& 0x0FFF);
175 static ssize_t
adis16209_read_14bit_unsigned(struct device
*dev
,
176 struct device_attribute
*attr
,
181 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
183 ret
= adis16209_spi_read_reg_16(dev
, this_attr
->address
, &val
);
187 if (val
& ADIS16209_ERROR_ACTIVE
)
188 adis16209_check_status(dev
);
190 return sprintf(buf
, "%u\n", val
& 0x3FFF);
193 static ssize_t
adis16209_read_temp(struct device
*dev
,
194 struct device_attribute
*attr
,
197 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
201 /* Take the iio_dev status lock */
202 mutex_lock(&indio_dev
->mlock
);
204 ret
= adis16209_spi_read_reg_16(dev
, ADIS16209_TEMP_OUT
, (u16
*)&val
);
208 if (val
& ADIS16209_ERROR_ACTIVE
)
209 adis16209_check_status(dev
);
212 ret
= sprintf(buf
, "%d\n", val
);
215 mutex_unlock(&indio_dev
->mlock
);
219 static ssize_t
adis16209_read_14bit_signed(struct device
*dev
,
220 struct device_attribute
*attr
,
223 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
224 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
228 mutex_lock(&indio_dev
->mlock
);
230 ret
= adis16209_spi_read_reg_16(dev
, this_attr
->address
, (u16
*)&val
);
232 if (val
& ADIS16209_ERROR_ACTIVE
)
233 adis16209_check_status(dev
);
235 val
= ((s16
)(val
<< 2) >> 2);
236 ret
= sprintf(buf
, "%d\n", val
);
239 mutex_unlock(&indio_dev
->mlock
);
244 static ssize_t
adis16209_write_16bit(struct device
*dev
,
245 struct device_attribute
*attr
,
249 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
253 ret
= strict_strtol(buf
, 10, &val
);
256 ret
= adis16209_spi_write_reg_16(dev
, this_attr
->address
, val
);
259 return ret
? ret
: len
;
262 static int adis16209_reset(struct device
*dev
)
265 ret
= adis16209_spi_write_reg_8(dev
,
267 ADIS16209_GLOB_CMD_SW_RESET
);
269 dev_err(dev
, "problem resetting device");
274 static ssize_t
adis16209_write_reset(struct device
*dev
,
275 struct device_attribute
*attr
,
276 const char *buf
, size_t len
)
284 return adis16209_reset(dev
);
289 int adis16209_set_irq(struct device
*dev
, bool enable
)
294 ret
= adis16209_spi_read_reg_16(dev
, ADIS16209_MSC_CTRL
, &msc
);
298 msc
|= ADIS16209_MSC_CTRL_ACTIVE_HIGH
;
299 msc
&= ~ADIS16209_MSC_CTRL_DATA_RDY_DIO2
;
301 msc
|= ADIS16209_MSC_CTRL_DATA_RDY_EN
;
303 msc
&= ~ADIS16209_MSC_CTRL_DATA_RDY_EN
;
305 ret
= adis16209_spi_write_reg_16(dev
, ADIS16209_MSC_CTRL
, msc
);
311 static int adis16209_check_status(struct device
*dev
)
316 ret
= adis16209_spi_read_reg_16(dev
, ADIS16209_DIAG_STAT
, &status
);
318 dev_err(dev
, "Reading status failed\n");
323 if (status
& ADIS16209_DIAG_STAT_SELFTEST_FAIL
)
324 dev_err(dev
, "Self test failure\n");
325 if (status
& ADIS16209_DIAG_STAT_SPI_FAIL
)
326 dev_err(dev
, "SPI failure\n");
327 if (status
& ADIS16209_DIAG_STAT_FLASH_UPT
)
328 dev_err(dev
, "Flash update failed\n");
329 if (status
& ADIS16209_DIAG_STAT_POWER_HIGH
)
330 dev_err(dev
, "Power supply above 3.625V\n");
331 if (status
& ADIS16209_DIAG_STAT_POWER_LOW
)
332 dev_err(dev
, "Power supply below 3.15V\n");
338 static int adis16209_self_test(struct device
*dev
)
341 ret
= adis16209_spi_write_reg_16(dev
,
343 ADIS16209_MSC_CTRL_SELF_TEST_EN
);
345 dev_err(dev
, "problem starting self test");
349 adis16209_check_status(dev
);
355 static int adis16209_initial_setup(struct adis16209_state
*st
)
358 struct device
*dev
= &st
->indio_dev
->dev
;
361 ret
= adis16209_set_irq(dev
, false);
363 dev_err(dev
, "disable irq failed");
368 ret
= adis16209_self_test(dev
);
370 dev_err(dev
, "self test failure");
374 /* Read status register to check the result */
375 ret
= adis16209_check_status(dev
);
377 adis16209_reset(dev
);
378 dev_err(dev
, "device not playing ball -> reset");
379 msleep(ADIS16209_STARTUP_DELAY
);
380 ret
= adis16209_check_status(dev
);
382 dev_err(dev
, "giving up");
387 printk(KERN_INFO DRIVER_NAME
": at CS%d (irq %d)\n",
388 st
->us
->chip_select
, st
->us
->irq
);
394 static IIO_DEV_ATTR_IN_NAMED_RAW(supply
, adis16209_read_14bit_unsigned
,
395 ADIS16209_SUPPLY_OUT
);
396 static IIO_CONST_ATTR(in_supply_scale
, "0.30518");
397 static IIO_DEV_ATTR_IN_RAW(0, adis16209_read_12bit_unsigned
,
399 static IIO_CONST_ATTR(in0_scale
, "0.6105");
401 static IIO_DEV_ATTR_ACCEL_X(adis16209_read_14bit_signed
,
402 ADIS16209_XACCL_OUT
);
403 static IIO_DEV_ATTR_ACCEL_Y(adis16209_read_14bit_signed
,
404 ADIS16209_YACCL_OUT
);
405 static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR
| S_IRUGO
,
406 adis16209_read_14bit_signed
,
407 adis16209_write_16bit
,
408 ADIS16209_XACCL_NULL
);
409 static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR
| S_IRUGO
,
410 adis16209_read_14bit_signed
,
411 adis16209_write_16bit
,
412 ADIS16209_YACCL_NULL
);
413 static IIO_CONST_ATTR(accel_scale
, "0.24414");
415 static IIO_DEV_ATTR_INCLI_X(adis16209_read_14bit_signed
,
416 ADIS16209_XINCL_OUT
);
417 static IIO_DEV_ATTR_INCLI_Y(adis16209_read_14bit_signed
,
418 ADIS16209_YINCL_OUT
);
419 static IIO_DEV_ATTR_INCLI_X_OFFSET(S_IWUSR
| S_IRUGO
,
420 adis16209_read_14bit_signed
,
421 adis16209_write_16bit
,
422 ADIS16209_XACCL_NULL
);
423 static IIO_DEV_ATTR_INCLI_Y_OFFSET(S_IWUSR
| S_IRUGO
,
424 adis16209_read_14bit_signed
,
425 adis16209_write_16bit
,
426 ADIS16209_YACCL_NULL
);
427 static IIO_CONST_ATTR(incli_scale
, "0.025");
429 static IIO_DEVICE_ATTR(rot_raw
, S_IRUGO
, adis16209_read_14bit_signed
,
430 NULL
, ADIS16209_ROT_OUT
);
432 static IIO_DEV_ATTR_TEMP(adis16209_read_temp
);
433 static IIO_CONST_ATTR(temp_offset
, "25");
434 static IIO_CONST_ATTR(temp_scale
, "-0.47");
436 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16209_write_reset
, 0);
438 static IIO_CONST_ATTR(name
, "adis16209");
440 static struct attribute
*adis16209_event_attributes
[] = {
444 static struct attribute_group adis16209_event_attribute_group
= {
445 .attrs
= adis16209_event_attributes
,
448 static struct attribute
*adis16209_attributes
[] = {
449 &iio_dev_attr_in_supply_raw
.dev_attr
.attr
,
450 &iio_const_attr_in_supply_scale
.dev_attr
.attr
,
451 &iio_dev_attr_temp
.dev_attr
.attr
,
452 &iio_const_attr_temp_offset
.dev_attr
.attr
,
453 &iio_const_attr_temp_scale
.dev_attr
.attr
,
454 &iio_dev_attr_reset
.dev_attr
.attr
,
455 &iio_const_attr_name
.dev_attr
.attr
,
456 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
457 &iio_const_attr_in0_scale
.dev_attr
.attr
,
458 &iio_dev_attr_accel_x_raw
.dev_attr
.attr
,
459 &iio_dev_attr_accel_y_raw
.dev_attr
.attr
,
460 &iio_dev_attr_accel_x_offset
.dev_attr
.attr
,
461 &iio_dev_attr_accel_y_offset
.dev_attr
.attr
,
462 &iio_const_attr_accel_scale
.dev_attr
.attr
,
463 &iio_dev_attr_incli_x_raw
.dev_attr
.attr
,
464 &iio_dev_attr_incli_y_raw
.dev_attr
.attr
,
465 &iio_dev_attr_incli_x_offset
.dev_attr
.attr
,
466 &iio_dev_attr_incli_y_offset
.dev_attr
.attr
,
467 &iio_const_attr_incli_scale
.dev_attr
.attr
,
468 &iio_dev_attr_rot_raw
.dev_attr
.attr
,
472 static const struct attribute_group adis16209_attribute_group
= {
473 .attrs
= adis16209_attributes
,
476 static int __devinit
adis16209_probe(struct spi_device
*spi
)
478 int ret
, regdone
= 0;
479 struct adis16209_state
*st
= kzalloc(sizeof *st
, GFP_KERNEL
);
484 /* this is only used for removal purposes */
485 spi_set_drvdata(spi
, st
);
487 /* Allocate the comms buffers */
488 st
->rx
= kzalloc(sizeof(*st
->rx
)*ADIS16209_MAX_RX
, GFP_KERNEL
);
489 if (st
->rx
== NULL
) {
493 st
->tx
= kzalloc(sizeof(*st
->tx
)*ADIS16209_MAX_TX
, GFP_KERNEL
);
494 if (st
->tx
== NULL
) {
499 mutex_init(&st
->buf_lock
);
500 /* setup the industrialio driver allocated elements */
501 st
->indio_dev
= iio_allocate_device();
502 if (st
->indio_dev
== NULL
) {
507 st
->indio_dev
->dev
.parent
= &spi
->dev
;
508 st
->indio_dev
->num_interrupt_lines
= 1;
509 st
->indio_dev
->event_attrs
= &adis16209_event_attribute_group
;
510 st
->indio_dev
->attrs
= &adis16209_attribute_group
;
511 st
->indio_dev
->dev_data
= (void *)(st
);
512 st
->indio_dev
->driver_module
= THIS_MODULE
;
513 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
515 ret
= adis16209_configure_ring(st
->indio_dev
);
519 ret
= iio_device_register(st
->indio_dev
);
521 goto error_unreg_ring_funcs
;
524 ret
= iio_ring_buffer_register(st
->indio_dev
->ring
, 0);
526 printk(KERN_ERR
"failed to initialize the ring\n");
527 goto error_unreg_ring_funcs
;
531 ret
= iio_register_interrupt_line(spi
->irq
,
537 goto error_uninitialize_ring
;
539 ret
= adis16209_probe_trigger(st
->indio_dev
);
541 goto error_unregister_line
;
544 /* Get the device into a sane initial state */
545 ret
= adis16209_initial_setup(st
);
547 goto error_remove_trigger
;
550 error_remove_trigger
:
551 adis16209_remove_trigger(st
->indio_dev
);
552 error_unregister_line
:
554 iio_unregister_interrupt_line(st
->indio_dev
, 0);
555 error_uninitialize_ring
:
556 iio_ring_buffer_unregister(st
->indio_dev
->ring
);
557 error_unreg_ring_funcs
:
558 adis16209_unconfigure_ring(st
->indio_dev
);
561 iio_device_unregister(st
->indio_dev
);
563 iio_free_device(st
->indio_dev
);
574 static int adis16209_remove(struct spi_device
*spi
)
576 struct adis16209_state
*st
= spi_get_drvdata(spi
);
577 struct iio_dev
*indio_dev
= st
->indio_dev
;
579 flush_scheduled_work();
581 adis16209_remove_trigger(indio_dev
);
583 iio_unregister_interrupt_line(indio_dev
, 0);
585 iio_ring_buffer_unregister(indio_dev
->ring
);
586 iio_device_unregister(indio_dev
);
587 adis16209_unconfigure_ring(indio_dev
);
595 static struct spi_driver adis16209_driver
= {
598 .owner
= THIS_MODULE
,
600 .probe
= adis16209_probe
,
601 .remove
= __devexit_p(adis16209_remove
),
604 static __init
int adis16209_init(void)
606 return spi_register_driver(&adis16209_driver
);
608 module_init(adis16209_init
);
610 static __exit
void adis16209_exit(void)
612 spi_unregister_driver(&adis16209_driver
);
614 module_exit(adis16209_exit
);
616 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
617 MODULE_DESCRIPTION("Analog Devices ADIS16209 Digital Vibration Sensor driver");
618 MODULE_LICENSE("GPL v2");