2 * ADIS16209 Programmable Digital Vibration Sensor driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/delay.h>
10 #include <linux/mutex.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/list.h>
20 #include "../ring_generic.h"
22 #include "inclinometer.h"
23 #include "../adc/adc.h"
25 #include "adis16209.h"
27 #define DRIVER_NAME "adis16209"
30 * adis16209_spi_write_reg_8() - write single byte to a register
31 * @indio_dev: iio device associated with actual device
32 * @reg_address: the address of the register to be written
33 * @val: the value to write
35 static int adis16209_spi_write_reg_8(struct iio_dev
*indio_dev
,
40 struct adis16209_state
*st
= iio_priv(indio_dev
);
42 mutex_lock(&st
->buf_lock
);
43 st
->tx
[0] = ADIS16209_WRITE_REG(reg_address
);
46 ret
= spi_write(st
->us
, st
->tx
, 2);
47 mutex_unlock(&st
->buf_lock
);
53 * adis16209_spi_write_reg_16() - write 2 bytes to a pair of registers
54 * @indio_dev: iio device associated actual device
55 * @reg_address: the address of the lower of the two registers. Second register
56 * is assumed to have address one greater.
57 * @val: value to be written
59 static int adis16209_spi_write_reg_16(struct iio_dev
*indio_dev
,
64 struct spi_message msg
;
65 struct adis16209_state
*st
= iio_priv(indio_dev
);
66 struct spi_transfer xfers
[] = {
81 mutex_lock(&st
->buf_lock
);
82 st
->tx
[0] = ADIS16209_WRITE_REG(lower_reg_address
);
83 st
->tx
[1] = value
& 0xFF;
84 st
->tx
[2] = ADIS16209_WRITE_REG(lower_reg_address
+ 1);
85 st
->tx
[3] = (value
>> 8) & 0xFF;
87 spi_message_init(&msg
);
88 spi_message_add_tail(&xfers
[0], &msg
);
89 spi_message_add_tail(&xfers
[1], &msg
);
90 ret
= spi_sync(st
->us
, &msg
);
91 mutex_unlock(&st
->buf_lock
);
97 * adis16209_spi_read_reg_16() - read 2 bytes from a 16-bit register
98 * @indio_dev: iio device associated with device
99 * @reg_address: the address of the lower of the two registers. Second register
100 * is assumed to have address one greater.
101 * @val: somewhere to pass back the value read
103 static int adis16209_spi_read_reg_16(struct iio_dev
*indio_dev
,
104 u8 lower_reg_address
,
107 struct spi_message msg
;
108 struct adis16209_state
*st
= iio_priv(indio_dev
);
110 struct spi_transfer xfers
[] = {
125 mutex_lock(&st
->buf_lock
);
126 st
->tx
[0] = ADIS16209_READ_REG(lower_reg_address
);
129 spi_message_init(&msg
);
130 spi_message_add_tail(&xfers
[0], &msg
);
131 spi_message_add_tail(&xfers
[1], &msg
);
132 ret
= spi_sync(st
->us
, &msg
);
134 dev_err(&st
->us
->dev
,
135 "problem when reading 16 bit register 0x%02X",
139 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
142 mutex_unlock(&st
->buf_lock
);
146 static int adis16209_reset(struct iio_dev
*indio_dev
)
149 ret
= adis16209_spi_write_reg_8(indio_dev
,
151 ADIS16209_GLOB_CMD_SW_RESET
);
153 dev_err(&indio_dev
->dev
, "problem resetting device");
158 static ssize_t
adis16209_write_reset(struct device
*dev
,
159 struct device_attribute
*attr
,
160 const char *buf
, size_t len
)
162 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
170 return adis16209_reset(indio_dev
);
175 int adis16209_set_irq(struct iio_dev
*indio_dev
, bool enable
)
180 ret
= adis16209_spi_read_reg_16(indio_dev
, ADIS16209_MSC_CTRL
, &msc
);
184 msc
|= ADIS16209_MSC_CTRL_ACTIVE_HIGH
;
185 msc
&= ~ADIS16209_MSC_CTRL_DATA_RDY_DIO2
;
187 msc
|= ADIS16209_MSC_CTRL_DATA_RDY_EN
;
189 msc
&= ~ADIS16209_MSC_CTRL_DATA_RDY_EN
;
191 ret
= adis16209_spi_write_reg_16(indio_dev
, ADIS16209_MSC_CTRL
, msc
);
197 static int adis16209_check_status(struct iio_dev
*indio_dev
)
202 ret
= adis16209_spi_read_reg_16(indio_dev
,
203 ADIS16209_DIAG_STAT
, &status
);
205 dev_err(&indio_dev
->dev
, "Reading status failed\n");
210 if (status
& ADIS16209_DIAG_STAT_SELFTEST_FAIL
)
211 dev_err(&indio_dev
->dev
, "Self test failure\n");
212 if (status
& ADIS16209_DIAG_STAT_SPI_FAIL
)
213 dev_err(&indio_dev
->dev
, "SPI failure\n");
214 if (status
& ADIS16209_DIAG_STAT_FLASH_UPT
)
215 dev_err(&indio_dev
->dev
, "Flash update failed\n");
216 if (status
& ADIS16209_DIAG_STAT_POWER_HIGH
)
217 dev_err(&indio_dev
->dev
, "Power supply above 3.625V\n");
218 if (status
& ADIS16209_DIAG_STAT_POWER_LOW
)
219 dev_err(&indio_dev
->dev
, "Power supply below 3.15V\n");
225 static int adis16209_self_test(struct iio_dev
*indio_dev
)
228 ret
= adis16209_spi_write_reg_16(indio_dev
,
230 ADIS16209_MSC_CTRL_SELF_TEST_EN
);
232 dev_err(&indio_dev
->dev
, "problem starting self test");
236 adis16209_check_status(indio_dev
);
242 static int adis16209_initial_setup(struct iio_dev
*indio_dev
)
247 ret
= adis16209_set_irq(indio_dev
, false);
249 dev_err(&indio_dev
->dev
, "disable irq failed");
254 ret
= adis16209_self_test(indio_dev
);
256 dev_err(&indio_dev
->dev
, "self test failure");
260 /* Read status register to check the result */
261 ret
= adis16209_check_status(indio_dev
);
263 adis16209_reset(indio_dev
);
264 dev_err(&indio_dev
->dev
, "device not playing ball -> reset");
265 msleep(ADIS16209_STARTUP_DELAY
);
266 ret
= adis16209_check_status(indio_dev
);
268 dev_err(&indio_dev
->dev
, "giving up");
277 enum adis16209_chan
{
288 static const u8 adis16209_addresses
[8][2] = {
289 [in_supply
] = { ADIS16209_SUPPLY_OUT
},
290 [in_aux
] = { ADIS16209_AUX_ADC
},
291 [accel_x
] = { ADIS16209_XACCL_OUT
, ADIS16209_XACCL_NULL
},
292 [accel_y
] = { ADIS16209_YACCL_OUT
, ADIS16209_YACCL_NULL
},
293 [incli_x
] = { ADIS16209_XINCL_OUT
, ADIS16209_XINCL_NULL
},
294 [incli_y
] = { ADIS16209_YINCL_OUT
, ADIS16209_YINCL_NULL
},
295 [rot
] = { ADIS16209_ROT_OUT
},
296 [temp
] = { ADIS16209_TEMP_OUT
},
299 static int adis16209_write_raw(struct iio_dev
*indio_dev
,
300 struct iio_chan_spec
const *chan
,
309 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
310 switch (chan
->type
) {
318 val16
= val
& ((1 << bits
) - 1);
319 addr
= adis16209_addresses
[chan
->address
][1];
320 return adis16209_spi_write_reg_16(indio_dev
, addr
, val16
);
325 static int adis16209_read_raw(struct iio_dev
*indio_dev
,
326 struct iio_chan_spec
const *chan
,
337 mutex_lock(&indio_dev
->mlock
);
338 addr
= adis16209_addresses
[chan
->address
][0];
339 ret
= adis16209_spi_read_reg_16(indio_dev
, addr
, &val16
);
343 if (val16
& ADIS16209_ERROR_ACTIVE
) {
344 ret
= adis16209_check_status(indio_dev
);
348 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
349 if (chan
->scan_type
.sign
== 's')
350 val16
= (s16
)(val16
<<
351 (16 - chan
->scan_type
.realbits
)) >>
352 (16 - chan
->scan_type
.realbits
);
354 mutex_unlock(&indio_dev
->mlock
);
356 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
357 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
358 switch (chan
->type
) {
361 if (chan
->channel
== 0)
365 return IIO_VAL_INT_PLUS_MICRO
;
369 return IIO_VAL_INT_PLUS_MICRO
;
373 return IIO_VAL_INT_PLUS_MICRO
;
377 return IIO_VAL_INT_PLUS_MICRO
;
382 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
):
385 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
386 switch (chan
->type
) {
393 mutex_lock(&indio_dev
->mlock
);
394 addr
= adis16209_addresses
[chan
->address
][1];
395 ret
= adis16209_spi_read_reg_16(indio_dev
, addr
, &val16
);
397 mutex_unlock(&indio_dev
->mlock
);
400 val16
&= (1 << bits
) - 1;
401 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
403 mutex_unlock(&indio_dev
->mlock
);
409 static struct iio_chan_spec adis16209_channels
[] = {
410 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
411 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
412 in_supply
, ADIS16209_SCAN_SUPPLY
,
413 IIO_ST('u', 14, 16, 0), 0),
414 IIO_CHAN(IIO_TEMP
, 0, 1, 0, NULL
, 0, 0,
415 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
) |
416 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
),
417 temp
, ADIS16209_SCAN_TEMP
,
418 IIO_ST('u', 12, 16, 0), 0),
419 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_X
,
420 (1 << IIO_CHAN_INFO_SCALE_SHARED
) |
421 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
422 accel_x
, ADIS16209_SCAN_ACC_X
,
423 IIO_ST('s', 14, 16, 0), 0),
424 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_Y
,
425 (1 << IIO_CHAN_INFO_SCALE_SHARED
) |
426 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
427 accel_y
, ADIS16209_SCAN_ACC_Y
,
428 IIO_ST('s', 14, 16, 0), 0),
429 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
430 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
431 in_aux
, ADIS16209_SCAN_AUX_ADC
,
432 IIO_ST('u', 12, 16, 0), 0),
433 IIO_CHAN(IIO_INCLI
, 0, 1, 0, NULL
, 0, IIO_MOD_X
,
434 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
435 incli_x
, ADIS16209_SCAN_INCLI_X
,
436 IIO_ST('s', 14, 16, 0), 0),
437 IIO_CHAN(IIO_INCLI
, 0, 1, 0, NULL
, 0, IIO_MOD_Y
,
438 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
439 incli_y
, ADIS16209_SCAN_INCLI_Y
,
440 IIO_ST('s', 14, 16, 0), 0),
441 IIO_CHAN(IIO_ROT
, 0, 1, 0, NULL
, 0, IIO_MOD_X
,
443 rot
, ADIS16209_SCAN_ROT
,
444 IIO_ST('s', 14, 16, 0), 0),
445 IIO_CHAN_SOFT_TIMESTAMP(8)
448 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16209_write_reset
, 0);
450 static struct attribute
*adis16209_attributes
[] = {
451 &iio_dev_attr_reset
.dev_attr
.attr
,
455 static const struct attribute_group adis16209_attribute_group
= {
456 .attrs
= adis16209_attributes
,
459 static const struct iio_info adis16209_info
= {
460 .attrs
= &adis16209_attribute_group
,
461 .read_raw
= &adis16209_read_raw
,
462 .write_raw
= &adis16209_write_raw
,
463 .driver_module
= THIS_MODULE
,
466 static int __devinit
adis16209_probe(struct spi_device
*spi
)
468 int ret
, regdone
= 0;
469 struct adis16209_state
*st
;
470 struct iio_dev
*indio_dev
;
472 /* setup the industrialio driver allocated elements */
473 indio_dev
= iio_allocate_device(sizeof(*st
));
474 if (indio_dev
== NULL
) {
478 st
= iio_priv(indio_dev
);
479 /* this is only used for removal purposes */
480 spi_set_drvdata(spi
, indio_dev
);
482 mutex_init(&st
->buf_lock
);
484 indio_dev
->name
= spi
->dev
.driver
->name
;
485 indio_dev
->dev
.parent
= &spi
->dev
;
486 indio_dev
->info
= &adis16209_info
;
487 indio_dev
->channels
= adis16209_channels
;
488 indio_dev
->num_channels
= ARRAY_SIZE(adis16209_channels
);
489 indio_dev
->modes
= INDIO_DIRECT_MODE
;
491 ret
= adis16209_configure_ring(indio_dev
);
495 ret
= iio_device_register(indio_dev
);
497 goto error_unreg_ring_funcs
;
500 ret
= iio_ring_buffer_register_ex(indio_dev
->ring
, 0,
502 ARRAY_SIZE(adis16209_channels
));
504 printk(KERN_ERR
"failed to initialize the ring\n");
505 goto error_unreg_ring_funcs
;
509 ret
= adis16209_probe_trigger(indio_dev
);
511 goto error_uninitialize_ring
;
514 /* Get the device into a sane initial state */
515 ret
= adis16209_initial_setup(indio_dev
);
517 goto error_remove_trigger
;
520 error_remove_trigger
:
521 adis16209_remove_trigger(indio_dev
);
522 error_uninitialize_ring
:
523 iio_ring_buffer_unregister(indio_dev
->ring
);
524 error_unreg_ring_funcs
:
525 adis16209_unconfigure_ring(indio_dev
);
528 iio_device_unregister(indio_dev
);
530 iio_free_device(indio_dev
);
535 static int adis16209_remove(struct spi_device
*spi
)
537 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
539 flush_scheduled_work();
541 adis16209_remove_trigger(indio_dev
);
542 iio_ring_buffer_unregister(indio_dev
->ring
);
543 iio_device_unregister(indio_dev
);
544 adis16209_unconfigure_ring(indio_dev
);
549 static struct spi_driver adis16209_driver
= {
552 .owner
= THIS_MODULE
,
554 .probe
= adis16209_probe
,
555 .remove
= __devexit_p(adis16209_remove
),
558 static __init
int adis16209_init(void)
560 return spi_register_driver(&adis16209_driver
);
562 module_init(adis16209_init
);
564 static __exit
void adis16209_exit(void)
566 spi_unregister_driver(&adis16209_driver
);
568 module_exit(adis16209_exit
);
570 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
571 MODULE_DESCRIPTION("Analog Devices ADIS16209 Digital Vibration Sensor driver");
572 MODULE_LICENSE("GPL v2");