2 * ADIS16204 Programmable High-g Digital Impact Sensor and Recorder
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>
20 #include <linux/module.h>
24 #include "../ring_generic.h"
26 #include "adis16204.h"
28 #define DRIVER_NAME "adis16204"
31 * adis16204_spi_write_reg_8() - write single byte to a register
32 * @dev: device associated with child of actual device (iio_dev or iio_trig)
33 * @reg_address: the address of the register to be written
34 * @val: the value to write
36 static int adis16204_spi_write_reg_8(struct iio_dev
*indio_dev
,
41 struct adis16204_state
*st
= iio_priv(indio_dev
);
43 mutex_lock(&st
->buf_lock
);
44 st
->tx
[0] = ADIS16204_WRITE_REG(reg_address
);
47 ret
= spi_write(st
->us
, st
->tx
, 2);
48 mutex_unlock(&st
->buf_lock
);
54 * adis16204_spi_write_reg_16() - write 2 bytes to a pair of registers
55 * @indio_dev: iio device associated with child of actual device
56 * @reg_address: the address of the lower of the two registers. Second register
57 * is assumed to have address one greater.
58 * @val: value to be written
60 static int adis16204_spi_write_reg_16(struct iio_dev
*indio_dev
,
65 struct spi_message msg
;
66 struct adis16204_state
*st
= iio_priv(indio_dev
);
67 struct spi_transfer xfers
[] = {
81 mutex_lock(&st
->buf_lock
);
82 st
->tx
[0] = ADIS16204_WRITE_REG(lower_reg_address
);
83 st
->tx
[1] = value
& 0xFF;
84 st
->tx
[2] = ADIS16204_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 * adis16204_spi_read_reg_16() - read 2 bytes from a 16-bit register
98 * @indio_dev: iio device associated with child of actual 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 adis16204_spi_read_reg_16(struct iio_dev
*indio_dev
,
104 u8 lower_reg_address
,
107 struct spi_message msg
;
108 struct adis16204_state
*st
= iio_priv(indio_dev
);
110 struct spi_transfer xfers
[] = {
125 mutex_lock(&st
->buf_lock
);
126 st
->tx
[0] = ADIS16204_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
, "problem when reading 16 bit register 0x%02X",
138 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
141 mutex_unlock(&st
->buf_lock
);
145 static int adis16204_check_status(struct iio_dev
*indio_dev
)
150 ret
= adis16204_spi_read_reg_16(indio_dev
,
151 ADIS16204_DIAG_STAT
, &status
);
153 dev_err(&indio_dev
->dev
, "Reading status failed\n");
158 if (status
& ADIS16204_DIAG_STAT_SELFTEST_FAIL
)
159 dev_err(&indio_dev
->dev
, "Self test failure\n");
160 if (status
& ADIS16204_DIAG_STAT_SPI_FAIL
)
161 dev_err(&indio_dev
->dev
, "SPI failure\n");
162 if (status
& ADIS16204_DIAG_STAT_FLASH_UPT
)
163 dev_err(&indio_dev
->dev
, "Flash update failed\n");
164 if (status
& ADIS16204_DIAG_STAT_POWER_HIGH
)
165 dev_err(&indio_dev
->dev
, "Power supply above 3.625V\n");
166 if (status
& ADIS16204_DIAG_STAT_POWER_LOW
)
167 dev_err(&indio_dev
->dev
, "Power supply below 2.975V\n");
173 static ssize_t
adis16204_read_14bit_signed(struct device
*dev
,
174 struct device_attribute
*attr
,
177 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
178 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
182 mutex_lock(&indio_dev
->mlock
);
184 ret
= adis16204_spi_read_reg_16(indio_dev
,
185 this_attr
->address
, (u16
*)&val
);
187 if (val
& ADIS16204_ERROR_ACTIVE
)
188 adis16204_check_status(indio_dev
);
190 val
= ((s16
)(val
<< 2) >> 2);
191 ret
= sprintf(buf
, "%d\n", val
);
194 mutex_unlock(&indio_dev
->mlock
);
199 static int adis16204_reset(struct iio_dev
*indio_dev
)
202 ret
= adis16204_spi_write_reg_8(indio_dev
,
204 ADIS16204_GLOB_CMD_SW_RESET
);
206 dev_err(&indio_dev
->dev
, "problem resetting device");
211 static ssize_t
adis16204_write_reset(struct device
*dev
,
212 struct device_attribute
*attr
,
213 const char *buf
, size_t len
)
215 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
223 return adis16204_reset(indio_dev
);
228 int adis16204_set_irq(struct iio_dev
*indio_dev
, bool enable
)
233 ret
= adis16204_spi_read_reg_16(indio_dev
, ADIS16204_MSC_CTRL
, &msc
);
237 msc
|= ADIS16204_MSC_CTRL_ACTIVE_HIGH
;
238 msc
&= ~ADIS16204_MSC_CTRL_DATA_RDY_DIO2
;
240 msc
|= ADIS16204_MSC_CTRL_DATA_RDY_EN
;
242 msc
&= ~ADIS16204_MSC_CTRL_DATA_RDY_EN
;
244 ret
= adis16204_spi_write_reg_16(indio_dev
, ADIS16204_MSC_CTRL
, msc
);
250 static int adis16204_self_test(struct iio_dev
*indio_dev
)
253 ret
= adis16204_spi_write_reg_16(indio_dev
,
255 ADIS16204_MSC_CTRL_SELF_TEST_EN
);
257 dev_err(&indio_dev
->dev
, "problem starting self test");
261 adis16204_check_status(indio_dev
);
267 static int adis16204_initial_setup(struct iio_dev
*indio_dev
)
272 ret
= adis16204_set_irq(indio_dev
, false);
274 dev_err(&indio_dev
->dev
, "disable irq failed");
279 ret
= adis16204_self_test(indio_dev
);
281 dev_err(&indio_dev
->dev
, "self test failure");
285 /* Read status register to check the result */
286 ret
= adis16204_check_status(indio_dev
);
288 adis16204_reset(indio_dev
);
289 dev_err(&indio_dev
->dev
, "device not playing ball -> reset");
290 msleep(ADIS16204_STARTUP_DELAY
);
291 ret
= adis16204_check_status(indio_dev
);
293 dev_err(&indio_dev
->dev
, "giving up");
302 /* Unique to this driver currently */
303 #define IIO_DEV_ATTR_ACCEL_XY(_show, _addr) \
304 IIO_DEVICE_ATTR(accel_xy, S_IRUGO, _show, NULL, _addr)
305 #define IIO_DEV_ATTR_ACCEL_XYPEAK(_show, _addr) \
306 IIO_DEVICE_ATTR(accel_xypeak, S_IRUGO, _show, NULL, _addr)
308 static IIO_DEV_ATTR_ACCEL_XY(adis16204_read_14bit_signed
,
309 ADIS16204_XY_RSS_OUT
);
310 static IIO_DEV_ATTR_ACCEL_XYPEAK(adis16204_read_14bit_signed
,
311 ADIS16204_XY_PEAK_OUT
);
312 static IIO_CONST_ATTR(accel_xy_scale
, "0.017125");
314 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16204_write_reset
, 0);
316 enum adis16204_channel
{
324 static u8 adis16204_addresses
[5][3] = {
325 [in_supply
] = { ADIS16204_SUPPLY_OUT
},
326 [in_aux
] = { ADIS16204_AUX_ADC
},
327 [temp
] = { ADIS16204_TEMP_OUT
},
328 [accel_x
] = { ADIS16204_XACCL_OUT
, ADIS16204_XACCL_NULL
,
329 ADIS16204_X_PEAK_OUT
},
330 [accel_y
] = { ADIS16204_XACCL_OUT
, ADIS16204_YACCL_NULL
,
331 ADIS16204_Y_PEAK_OUT
},
334 static int adis16204_read_raw(struct iio_dev
*indio_dev
,
335 struct iio_chan_spec
const *chan
,
347 mutex_lock(&indio_dev
->mlock
);
348 addr
= adis16204_addresses
[chan
->address
][0];
349 ret
= adis16204_spi_read_reg_16(indio_dev
, addr
, &val16
);
351 mutex_unlock(&indio_dev
->mlock
);
355 if (val16
& ADIS16204_ERROR_ACTIVE
) {
356 ret
= adis16204_check_status(indio_dev
);
358 mutex_unlock(&indio_dev
->mlock
);
362 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
363 if (chan
->scan_type
.sign
== 's')
364 val16
= (s16
)(val16
<<
365 (16 - chan
->scan_type
.realbits
)) >>
366 (16 - chan
->scan_type
.realbits
);
368 mutex_unlock(&indio_dev
->mlock
);
370 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
371 switch (chan
->type
) {
374 if (chan
->channel
== 0)
378 return IIO_VAL_INT_PLUS_MICRO
;
382 return IIO_VAL_INT_PLUS_MICRO
;
385 if (chan
->channel
== 'x')
389 return IIO_VAL_INT_PLUS_MICRO
;
394 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
):
397 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
398 case (1 << IIO_CHAN_INFO_PEAK_SEPARATE
):
399 if (mask
== (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
)) {
402 } else { /* PEAK_SEPARATE */
406 mutex_lock(&indio_dev
->mlock
);
407 addr
= adis16204_addresses
[chan
->address
][addrind
];
408 ret
= adis16204_spi_read_reg_16(indio_dev
, addr
, &val16
);
410 mutex_unlock(&indio_dev
->mlock
);
413 val16
&= (1 << bits
) - 1;
414 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
416 mutex_unlock(&indio_dev
->mlock
);
422 static int adis16204_write_raw(struct iio_dev
*indio_dev
,
423 struct iio_chan_spec
const *chan
,
432 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
433 switch (chan
->type
) {
440 val16
= val
& ((1 << bits
) - 1);
441 addr
= adis16204_addresses
[chan
->address
][1];
442 return adis16204_spi_write_reg_16(indio_dev
, addr
, val16
);
447 static struct iio_chan_spec adis16204_channels
[] = {
448 IIO_CHAN(IIO_IN
, 0, 0, 0, "supply", 0, 0,
449 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
450 in_supply
, ADIS16204_SCAN_SUPPLY
,
451 IIO_ST('u', 12, 16, 0), 0),
452 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
453 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
454 in_aux
, ADIS16204_SCAN_AUX_ADC
,
455 IIO_ST('u', 12, 16, 0), 0),
456 IIO_CHAN(IIO_TEMP
, 0, 1, 0, NULL
, 0, 0,
457 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
) |
458 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
),
459 temp
, ADIS16204_SCAN_TEMP
,
460 IIO_ST('u', 12, 16, 0), 0),
461 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_X
,
462 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
) |
463 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
) |
464 (1 << IIO_CHAN_INFO_PEAK_SEPARATE
),
465 accel_x
, ADIS16204_SCAN_ACC_X
,
466 IIO_ST('s', 14, 16, 0), 0),
467 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_Y
,
468 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
) |
469 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
) |
470 (1 << IIO_CHAN_INFO_PEAK_SEPARATE
),
471 accel_y
, ADIS16204_SCAN_ACC_Y
,
472 IIO_ST('s', 14, 16, 0), 0),
473 IIO_CHAN_SOFT_TIMESTAMP(5),
476 static struct attribute
*adis16204_attributes
[] = {
477 &iio_dev_attr_reset
.dev_attr
.attr
,
478 &iio_dev_attr_accel_xy
.dev_attr
.attr
,
479 &iio_dev_attr_accel_xypeak
.dev_attr
.attr
,
480 &iio_const_attr_accel_xy_scale
.dev_attr
.attr
,
484 static const struct attribute_group adis16204_attribute_group
= {
485 .attrs
= adis16204_attributes
,
488 static const struct iio_info adis16204_info
= {
489 .attrs
= &adis16204_attribute_group
,
490 .read_raw
= &adis16204_read_raw
,
491 .write_raw
= &adis16204_write_raw
,
492 .driver_module
= THIS_MODULE
,
495 static int __devinit
adis16204_probe(struct spi_device
*spi
)
497 int ret
, regdone
= 0;
498 struct adis16204_state
*st
;
499 struct iio_dev
*indio_dev
;
501 /* setup the industrialio driver allocated elements */
502 indio_dev
= iio_allocate_device(sizeof(*st
));
503 if (indio_dev
== NULL
) {
507 st
= iio_priv(indio_dev
);
508 /* this is only used for removal purposes */
509 spi_set_drvdata(spi
, indio_dev
);
511 mutex_init(&st
->buf_lock
);
513 indio_dev
->name
= spi
->dev
.driver
->name
;
514 indio_dev
->dev
.parent
= &spi
->dev
;
515 indio_dev
->info
= &adis16204_info
;
516 indio_dev
->channels
= adis16204_channels
;
517 indio_dev
->num_channels
= ARRAY_SIZE(adis16204_channels
);
518 indio_dev
->modes
= INDIO_DIRECT_MODE
;
520 ret
= adis16204_configure_ring(indio_dev
);
524 ret
= iio_device_register(indio_dev
);
526 goto error_unreg_ring_funcs
;
529 ret
= iio_ring_buffer_register_ex(indio_dev
, 0,
531 ARRAY_SIZE(adis16204_channels
));
533 printk(KERN_ERR
"failed to initialize the ring\n");
534 goto error_unreg_ring_funcs
;
538 ret
= adis16204_probe_trigger(indio_dev
);
540 goto error_uninitialize_ring
;
543 /* Get the device into a sane initial state */
544 ret
= adis16204_initial_setup(indio_dev
);
546 goto error_remove_trigger
;
549 error_remove_trigger
:
550 adis16204_remove_trigger(indio_dev
);
551 error_uninitialize_ring
:
552 iio_ring_buffer_unregister(indio_dev
);
553 error_unreg_ring_funcs
:
554 adis16204_unconfigure_ring(indio_dev
);
557 iio_device_unregister(indio_dev
);
559 iio_free_device(indio_dev
);
564 static int adis16204_remove(struct spi_device
*spi
)
566 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
568 adis16204_remove_trigger(indio_dev
);
569 iio_ring_buffer_unregister(indio_dev
);
570 iio_device_unregister(indio_dev
);
571 adis16204_unconfigure_ring(indio_dev
);
576 static struct spi_driver adis16204_driver
= {
579 .owner
= THIS_MODULE
,
581 .probe
= adis16204_probe
,
582 .remove
= __devexit_p(adis16204_remove
),
585 static __init
int adis16204_init(void)
587 return spi_register_driver(&adis16204_driver
);
589 module_init(adis16204_init
);
591 static __exit
void adis16204_exit(void)
593 spi_unregister_driver(&adis16204_driver
);
595 module_exit(adis16204_exit
);
597 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
598 MODULE_DESCRIPTION("ADIS16204 High-g Digital Impact Sensor and Recorder");
599 MODULE_LICENSE("GPL v2");