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(0, supply
, adis16209_read_14bit_unsigned
,
395 ADIS16209_SUPPLY_OUT
);
396 static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply
, "0.30518");
397 static IIO_DEV_ATTR_IN_RAW(1, adis16209_read_12bit_unsigned
,
399 static IIO_CONST_ATTR(in1_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_CALIBBIAS(S_IWUSR
| S_IRUGO
,
406 adis16209_read_14bit_signed
,
407 adis16209_write_16bit
,
408 ADIS16209_XACCL_NULL
);
409 static IIO_DEV_ATTR_ACCEL_Y_CALIBBIAS(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.002394195531");
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_CONST_ATTR(incli_scale
, "0.00043633231");
421 static IIO_DEVICE_ATTR(rot_raw
, S_IRUGO
, adis16209_read_14bit_signed
,
422 NULL
, ADIS16209_ROT_OUT
);
424 static IIO_DEV_ATTR_TEMP_RAW(adis16209_read_temp
);
425 static IIO_CONST_ATTR_TEMP_OFFSET("25");
426 static IIO_CONST_ATTR_TEMP_SCALE("-0.47");
428 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16209_write_reset
, 0);
430 static struct attribute
*adis16209_attributes
[] = {
431 &iio_dev_attr_in0_supply_raw
.dev_attr
.attr
,
432 &iio_const_attr_in0_supply_scale
.dev_attr
.attr
,
433 &iio_dev_attr_temp_raw
.dev_attr
.attr
,
434 &iio_const_attr_temp_offset
.dev_attr
.attr
,
435 &iio_const_attr_temp_scale
.dev_attr
.attr
,
436 &iio_dev_attr_reset
.dev_attr
.attr
,
437 &iio_dev_attr_in1_raw
.dev_attr
.attr
,
438 &iio_const_attr_in1_scale
.dev_attr
.attr
,
439 &iio_dev_attr_accel_x_raw
.dev_attr
.attr
,
440 &iio_dev_attr_accel_y_raw
.dev_attr
.attr
,
441 &iio_dev_attr_accel_x_calibbias
.dev_attr
.attr
,
442 &iio_dev_attr_accel_y_calibbias
.dev_attr
.attr
,
443 &iio_const_attr_accel_scale
.dev_attr
.attr
,
444 &iio_dev_attr_incli_x_raw
.dev_attr
.attr
,
445 &iio_dev_attr_incli_y_raw
.dev_attr
.attr
,
446 &iio_const_attr_incli_scale
.dev_attr
.attr
,
447 &iio_dev_attr_rot_raw
.dev_attr
.attr
,
451 static const struct attribute_group adis16209_attribute_group
= {
452 .attrs
= adis16209_attributes
,
455 static int __devinit
adis16209_probe(struct spi_device
*spi
)
457 int ret
, regdone
= 0;
458 struct adis16209_state
*st
= kzalloc(sizeof *st
, GFP_KERNEL
);
463 /* this is only used for removal purposes */
464 spi_set_drvdata(spi
, st
);
466 /* Allocate the comms buffers */
467 st
->rx
= kzalloc(sizeof(*st
->rx
)*ADIS16209_MAX_RX
, GFP_KERNEL
);
468 if (st
->rx
== NULL
) {
472 st
->tx
= kzalloc(sizeof(*st
->tx
)*ADIS16209_MAX_TX
, GFP_KERNEL
);
473 if (st
->tx
== NULL
) {
478 mutex_init(&st
->buf_lock
);
479 /* setup the industrialio driver allocated elements */
480 st
->indio_dev
= iio_allocate_device(0);
481 if (st
->indio_dev
== NULL
) {
486 st
->indio_dev
->name
= spi
->dev
.driver
->name
;
487 st
->indio_dev
->dev
.parent
= &spi
->dev
;
488 st
->indio_dev
->attrs
= &adis16209_attribute_group
;
489 st
->indio_dev
->dev_data
= (void *)(st
);
490 st
->indio_dev
->driver_module
= THIS_MODULE
;
491 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
493 ret
= adis16209_configure_ring(st
->indio_dev
);
497 ret
= iio_device_register(st
->indio_dev
);
499 goto error_unreg_ring_funcs
;
502 ret
= iio_ring_buffer_register(st
->indio_dev
->ring
, 0);
504 printk(KERN_ERR
"failed to initialize the ring\n");
505 goto error_unreg_ring_funcs
;
509 ret
= adis16209_probe_trigger(st
->indio_dev
);
511 goto error_uninitialize_ring
;
514 /* Get the device into a sane initial state */
515 ret
= adis16209_initial_setup(st
);
517 goto error_remove_trigger
;
520 error_remove_trigger
:
521 adis16209_remove_trigger(st
->indio_dev
);
522 error_uninitialize_ring
:
523 iio_ring_buffer_unregister(st
->indio_dev
->ring
);
524 error_unreg_ring_funcs
:
525 adis16209_unconfigure_ring(st
->indio_dev
);
528 iio_device_unregister(st
->indio_dev
);
530 iio_free_device(st
->indio_dev
);
541 static int adis16209_remove(struct spi_device
*spi
)
543 struct adis16209_state
*st
= spi_get_drvdata(spi
);
544 struct iio_dev
*indio_dev
= st
->indio_dev
;
546 flush_scheduled_work();
548 adis16209_remove_trigger(indio_dev
);
549 iio_ring_buffer_unregister(indio_dev
->ring
);
550 iio_device_unregister(indio_dev
);
551 adis16209_unconfigure_ring(indio_dev
);
559 static struct spi_driver adis16209_driver
= {
562 .owner
= THIS_MODULE
,
564 .probe
= adis16209_probe
,
565 .remove
= __devexit_p(adis16209_remove
),
568 static __init
int adis16209_init(void)
570 return spi_register_driver(&adis16209_driver
);
572 module_init(adis16209_init
);
574 static __exit
void adis16209_exit(void)
576 spi_unregister_driver(&adis16209_driver
);
578 module_exit(adis16209_exit
);
580 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
581 MODULE_DESCRIPTION("Analog Devices ADIS16209 Digital Vibration Sensor driver");
582 MODULE_LICENSE("GPL v2");