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>
17 #include <linux/module.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
23 #include "adis16209.h"
25 #define DRIVER_NAME "adis16209"
28 * adis16209_spi_write_reg_8() - write single byte to a register
29 * @indio_dev: iio device associated with actual device
30 * @reg_address: the address of the register to be written
31 * @val: the value to write
33 static int adis16209_spi_write_reg_8(struct iio_dev
*indio_dev
,
38 struct adis16209_state
*st
= iio_priv(indio_dev
);
40 mutex_lock(&st
->buf_lock
);
41 st
->tx
[0] = ADIS16209_WRITE_REG(reg_address
);
44 ret
= spi_write(st
->us
, st
->tx
, 2);
45 mutex_unlock(&st
->buf_lock
);
51 * adis16209_spi_write_reg_16() - write 2 bytes to a pair of registers
52 * @indio_dev: iio device associated actual device
53 * @reg_address: the address of the lower of the two registers. Second register
54 * is assumed to have address one greater.
55 * @val: value to be written
57 static int adis16209_spi_write_reg_16(struct iio_dev
*indio_dev
,
62 struct spi_message msg
;
63 struct adis16209_state
*st
= iio_priv(indio_dev
);
64 struct spi_transfer xfers
[] = {
79 mutex_lock(&st
->buf_lock
);
80 st
->tx
[0] = ADIS16209_WRITE_REG(lower_reg_address
);
81 st
->tx
[1] = value
& 0xFF;
82 st
->tx
[2] = ADIS16209_WRITE_REG(lower_reg_address
+ 1);
83 st
->tx
[3] = (value
>> 8) & 0xFF;
85 spi_message_init(&msg
);
86 spi_message_add_tail(&xfers
[0], &msg
);
87 spi_message_add_tail(&xfers
[1], &msg
);
88 ret
= spi_sync(st
->us
, &msg
);
89 mutex_unlock(&st
->buf_lock
);
95 * adis16209_spi_read_reg_16() - read 2 bytes from a 16-bit register
96 * @indio_dev: iio device associated with device
97 * @reg_address: the address of the lower of the two registers. Second register
98 * is assumed to have address one greater.
99 * @val: somewhere to pass back the value read
101 static int adis16209_spi_read_reg_16(struct iio_dev
*indio_dev
,
102 u8 lower_reg_address
,
105 struct spi_message msg
;
106 struct adis16209_state
*st
= iio_priv(indio_dev
);
108 struct spi_transfer xfers
[] = {
123 mutex_lock(&st
->buf_lock
);
124 st
->tx
[0] = ADIS16209_READ_REG(lower_reg_address
);
127 spi_message_init(&msg
);
128 spi_message_add_tail(&xfers
[0], &msg
);
129 spi_message_add_tail(&xfers
[1], &msg
);
130 ret
= spi_sync(st
->us
, &msg
);
132 dev_err(&st
->us
->dev
,
133 "problem when reading 16 bit register 0x%02X",
137 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
140 mutex_unlock(&st
->buf_lock
);
144 static int adis16209_reset(struct iio_dev
*indio_dev
)
147 ret
= adis16209_spi_write_reg_8(indio_dev
,
149 ADIS16209_GLOB_CMD_SW_RESET
);
151 dev_err(&indio_dev
->dev
, "problem resetting device");
156 static ssize_t
adis16209_write_reset(struct device
*dev
,
157 struct device_attribute
*attr
,
158 const char *buf
, size_t len
)
160 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
168 return adis16209_reset(indio_dev
);
173 int adis16209_set_irq(struct iio_dev
*indio_dev
, bool enable
)
178 ret
= adis16209_spi_read_reg_16(indio_dev
, ADIS16209_MSC_CTRL
, &msc
);
182 msc
|= ADIS16209_MSC_CTRL_ACTIVE_HIGH
;
183 msc
&= ~ADIS16209_MSC_CTRL_DATA_RDY_DIO2
;
185 msc
|= ADIS16209_MSC_CTRL_DATA_RDY_EN
;
187 msc
&= ~ADIS16209_MSC_CTRL_DATA_RDY_EN
;
189 ret
= adis16209_spi_write_reg_16(indio_dev
, ADIS16209_MSC_CTRL
, msc
);
195 static int adis16209_check_status(struct iio_dev
*indio_dev
)
200 ret
= adis16209_spi_read_reg_16(indio_dev
,
201 ADIS16209_DIAG_STAT
, &status
);
203 dev_err(&indio_dev
->dev
, "Reading status failed\n");
208 if (status
& ADIS16209_DIAG_STAT_SELFTEST_FAIL
)
209 dev_err(&indio_dev
->dev
, "Self test failure\n");
210 if (status
& ADIS16209_DIAG_STAT_SPI_FAIL
)
211 dev_err(&indio_dev
->dev
, "SPI failure\n");
212 if (status
& ADIS16209_DIAG_STAT_FLASH_UPT
)
213 dev_err(&indio_dev
->dev
, "Flash update failed\n");
214 if (status
& ADIS16209_DIAG_STAT_POWER_HIGH
)
215 dev_err(&indio_dev
->dev
, "Power supply above 3.625V\n");
216 if (status
& ADIS16209_DIAG_STAT_POWER_LOW
)
217 dev_err(&indio_dev
->dev
, "Power supply below 3.15V\n");
223 static int adis16209_self_test(struct iio_dev
*indio_dev
)
226 ret
= adis16209_spi_write_reg_16(indio_dev
,
228 ADIS16209_MSC_CTRL_SELF_TEST_EN
);
230 dev_err(&indio_dev
->dev
, "problem starting self test");
234 adis16209_check_status(indio_dev
);
240 static int adis16209_initial_setup(struct iio_dev
*indio_dev
)
245 ret
= adis16209_set_irq(indio_dev
, false);
247 dev_err(&indio_dev
->dev
, "disable irq failed");
252 ret
= adis16209_self_test(indio_dev
);
254 dev_err(&indio_dev
->dev
, "self test failure");
258 /* Read status register to check the result */
259 ret
= adis16209_check_status(indio_dev
);
261 adis16209_reset(indio_dev
);
262 dev_err(&indio_dev
->dev
, "device not playing ball -> reset");
263 msleep(ADIS16209_STARTUP_DELAY
);
264 ret
= adis16209_check_status(indio_dev
);
266 dev_err(&indio_dev
->dev
, "giving up");
275 enum adis16209_chan
{
286 static const u8 adis16209_addresses
[8][2] = {
287 [in_supply
] = { ADIS16209_SUPPLY_OUT
},
288 [in_aux
] = { ADIS16209_AUX_ADC
},
289 [accel_x
] = { ADIS16209_XACCL_OUT
, ADIS16209_XACCL_NULL
},
290 [accel_y
] = { ADIS16209_YACCL_OUT
, ADIS16209_YACCL_NULL
},
291 [incli_x
] = { ADIS16209_XINCL_OUT
, ADIS16209_XINCL_NULL
},
292 [incli_y
] = { ADIS16209_YINCL_OUT
, ADIS16209_YINCL_NULL
},
293 [rot
] = { ADIS16209_ROT_OUT
},
294 [temp
] = { ADIS16209_TEMP_OUT
},
297 static int adis16209_write_raw(struct iio_dev
*indio_dev
,
298 struct iio_chan_spec
const *chan
,
307 case IIO_CHAN_INFO_CALIBBIAS
:
308 switch (chan
->type
) {
316 val16
= val
& ((1 << bits
) - 1);
317 addr
= adis16209_addresses
[chan
->address
][1];
318 return adis16209_spi_write_reg_16(indio_dev
, addr
, val16
);
323 static int adis16209_read_raw(struct iio_dev
*indio_dev
,
324 struct iio_chan_spec
const *chan
,
334 case IIO_CHAN_INFO_RAW
:
335 mutex_lock(&indio_dev
->mlock
);
336 addr
= adis16209_addresses
[chan
->address
][0];
337 ret
= adis16209_spi_read_reg_16(indio_dev
, addr
, &val16
);
339 mutex_unlock(&indio_dev
->mlock
);
343 if (val16
& ADIS16209_ERROR_ACTIVE
) {
344 ret
= adis16209_check_status(indio_dev
);
346 mutex_unlock(&indio_dev
->mlock
);
350 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
351 if (chan
->scan_type
.sign
== 's')
352 val16
= (s16
)(val16
<<
353 (16 - chan
->scan_type
.realbits
)) >>
354 (16 - chan
->scan_type
.realbits
);
356 mutex_unlock(&indio_dev
->mlock
);
358 case IIO_CHAN_INFO_SCALE
:
359 switch (chan
->type
) {
362 if (chan
->channel
== 0)
366 return IIO_VAL_INT_PLUS_MICRO
;
370 return IIO_VAL_INT_PLUS_MICRO
;
374 return IIO_VAL_INT_PLUS_MICRO
;
378 return IIO_VAL_INT_PLUS_MICRO
;
383 case IIO_CHAN_INFO_OFFSET
:
386 case IIO_CHAN_INFO_CALIBBIAS
:
387 switch (chan
->type
) {
394 mutex_lock(&indio_dev
->mlock
);
395 addr
= adis16209_addresses
[chan
->address
][1];
396 ret
= adis16209_spi_read_reg_16(indio_dev
, addr
, &val16
);
398 mutex_unlock(&indio_dev
->mlock
);
401 val16
&= (1 << bits
) - 1;
402 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
404 mutex_unlock(&indio_dev
->mlock
);
410 static struct iio_chan_spec adis16209_channels
[] = {
415 .extend_name
= "supply",
416 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
417 IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
418 .address
= in_supply
,
419 .scan_index
= ADIS16209_SCAN_SUPPLY
,
429 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
430 IIO_CHAN_INFO_SCALE_SEPARATE_BIT
|
431 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT
,
433 .scan_index
= ADIS16209_SCAN_TEMP
,
442 .channel2
= IIO_MOD_X
,
443 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
444 IIO_CHAN_INFO_SCALE_SHARED_BIT
|
445 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT
,
447 .scan_index
= ADIS16209_SCAN_ACC_X
,
456 .channel2
= IIO_MOD_Y
,
457 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
458 IIO_CHAN_INFO_SCALE_SHARED_BIT
|
459 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT
,
461 .scan_index
= ADIS16209_SCAN_ACC_Y
,
471 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
472 IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
474 .scan_index
= ADIS16209_SCAN_AUX_ADC
,
483 .channel2
= IIO_MOD_X
,
484 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
485 IIO_CHAN_INFO_SCALE_SHARED_BIT
,
487 .scan_index
= ADIS16209_SCAN_INCLI_X
,
496 .channel2
= IIO_MOD_Y
,
497 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
498 IIO_CHAN_INFO_SCALE_SHARED_BIT
,
500 .scan_index
= ADIS16209_SCAN_INCLI_Y
,
509 .channel2
= IIO_MOD_X
,
510 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
,
512 .scan_index
= ADIS16209_SCAN_ROT
,
519 IIO_CHAN_SOFT_TIMESTAMP(8)
522 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16209_write_reset
, 0);
524 static struct attribute
*adis16209_attributes
[] = {
525 &iio_dev_attr_reset
.dev_attr
.attr
,
529 static const struct attribute_group adis16209_attribute_group
= {
530 .attrs
= adis16209_attributes
,
533 static const struct iio_info adis16209_info
= {
534 .attrs
= &adis16209_attribute_group
,
535 .read_raw
= &adis16209_read_raw
,
536 .write_raw
= &adis16209_write_raw
,
537 .driver_module
= THIS_MODULE
,
540 static int __devinit
adis16209_probe(struct spi_device
*spi
)
543 struct adis16209_state
*st
;
544 struct iio_dev
*indio_dev
;
546 /* setup the industrialio driver allocated elements */
547 indio_dev
= iio_allocate_device(sizeof(*st
));
548 if (indio_dev
== NULL
) {
552 st
= iio_priv(indio_dev
);
553 /* this is only used for removal purposes */
554 spi_set_drvdata(spi
, indio_dev
);
556 mutex_init(&st
->buf_lock
);
558 indio_dev
->name
= spi
->dev
.driver
->name
;
559 indio_dev
->dev
.parent
= &spi
->dev
;
560 indio_dev
->info
= &adis16209_info
;
561 indio_dev
->channels
= adis16209_channels
;
562 indio_dev
->num_channels
= ARRAY_SIZE(adis16209_channels
);
563 indio_dev
->modes
= INDIO_DIRECT_MODE
;
565 ret
= adis16209_configure_ring(indio_dev
);
569 ret
= iio_buffer_register(indio_dev
,
571 ARRAY_SIZE(adis16209_channels
));
573 printk(KERN_ERR
"failed to initialize the ring\n");
574 goto error_unreg_ring_funcs
;
578 ret
= adis16209_probe_trigger(indio_dev
);
580 goto error_uninitialize_ring
;
583 /* Get the device into a sane initial state */
584 ret
= adis16209_initial_setup(indio_dev
);
586 goto error_remove_trigger
;
587 ret
= iio_device_register(indio_dev
);
589 goto error_remove_trigger
;
593 error_remove_trigger
:
594 adis16209_remove_trigger(indio_dev
);
595 error_uninitialize_ring
:
596 iio_buffer_unregister(indio_dev
);
597 error_unreg_ring_funcs
:
598 adis16209_unconfigure_ring(indio_dev
);
600 iio_free_device(indio_dev
);
605 static int adis16209_remove(struct spi_device
*spi
)
607 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
609 flush_scheduled_work();
611 iio_device_unregister(indio_dev
);
612 adis16209_remove_trigger(indio_dev
);
613 iio_buffer_unregister(indio_dev
);
614 adis16209_unconfigure_ring(indio_dev
);
615 iio_free_device(indio_dev
);
620 static struct spi_driver adis16209_driver
= {
623 .owner
= THIS_MODULE
,
625 .probe
= adis16209_probe
,
626 .remove
= __devexit_p(adis16209_remove
),
628 module_spi_driver(adis16209_driver
);
630 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
631 MODULE_DESCRIPTION("Analog Devices ADIS16209 Digital Vibration Sensor driver");
632 MODULE_LICENSE("GPL v2");
633 MODULE_ALIAS("spi:adis16209");