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>
23 #include "../ring_generic.h"
25 #include "../adc/adc.h"
27 #include "adis16204.h"
29 #define DRIVER_NAME "adis16204"
32 * adis16204_spi_write_reg_8() - write single byte to a register
33 * @dev: device associated with child of actual device (iio_dev or iio_trig)
34 * @reg_address: the address of the register to be written
35 * @val: the value to write
37 static int adis16204_spi_write_reg_8(struct iio_dev
*indio_dev
,
42 struct adis16204_state
*st
= iio_priv(indio_dev
);
44 mutex_lock(&st
->buf_lock
);
45 st
->tx
[0] = ADIS16204_WRITE_REG(reg_address
);
48 ret
= spi_write(st
->us
, st
->tx
, 2);
49 mutex_unlock(&st
->buf_lock
);
55 * adis16204_spi_write_reg_16() - write 2 bytes to a pair of registers
56 * @indio_dev: iio device associated with child of actual device
57 * @reg_address: the address of the lower of the two registers. Second register
58 * is assumed to have address one greater.
59 * @val: value to be written
61 static int adis16204_spi_write_reg_16(struct iio_dev
*indio_dev
,
66 struct spi_message msg
;
67 struct adis16204_state
*st
= iio_priv(indio_dev
);
68 struct spi_transfer xfers
[] = {
82 mutex_lock(&st
->buf_lock
);
83 st
->tx
[0] = ADIS16204_WRITE_REG(lower_reg_address
);
84 st
->tx
[1] = value
& 0xFF;
85 st
->tx
[2] = ADIS16204_WRITE_REG(lower_reg_address
+ 1);
86 st
->tx
[3] = (value
>> 8) & 0xFF;
88 spi_message_init(&msg
);
89 spi_message_add_tail(&xfers
[0], &msg
);
90 spi_message_add_tail(&xfers
[1], &msg
);
91 ret
= spi_sync(st
->us
, &msg
);
92 mutex_unlock(&st
->buf_lock
);
98 * adis16204_spi_read_reg_16() - read 2 bytes from a 16-bit register
99 * @indio_dev: iio device associated with child of actual device
100 * @reg_address: the address of the lower of the two registers. Second register
101 * is assumed to have address one greater.
102 * @val: somewhere to pass back the value read
104 static int adis16204_spi_read_reg_16(struct iio_dev
*indio_dev
,
105 u8 lower_reg_address
,
108 struct spi_message msg
;
109 struct adis16204_state
*st
= iio_priv(indio_dev
);
111 struct spi_transfer xfers
[] = {
126 mutex_lock(&st
->buf_lock
);
127 st
->tx
[0] = ADIS16204_READ_REG(lower_reg_address
);
130 spi_message_init(&msg
);
131 spi_message_add_tail(&xfers
[0], &msg
);
132 spi_message_add_tail(&xfers
[1], &msg
);
133 ret
= spi_sync(st
->us
, &msg
);
135 dev_err(&st
->us
->dev
, "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 adis16204_check_status(struct iio_dev
*indio_dev
)
151 ret
= adis16204_spi_read_reg_16(indio_dev
,
152 ADIS16204_DIAG_STAT
, &status
);
154 dev_err(&indio_dev
->dev
, "Reading status failed\n");
159 if (status
& ADIS16204_DIAG_STAT_SELFTEST_FAIL
)
160 dev_err(&indio_dev
->dev
, "Self test failure\n");
161 if (status
& ADIS16204_DIAG_STAT_SPI_FAIL
)
162 dev_err(&indio_dev
->dev
, "SPI failure\n");
163 if (status
& ADIS16204_DIAG_STAT_FLASH_UPT
)
164 dev_err(&indio_dev
->dev
, "Flash update failed\n");
165 if (status
& ADIS16204_DIAG_STAT_POWER_HIGH
)
166 dev_err(&indio_dev
->dev
, "Power supply above 3.625V\n");
167 if (status
& ADIS16204_DIAG_STAT_POWER_LOW
)
168 dev_err(&indio_dev
->dev
, "Power supply below 2.975V\n");
174 static ssize_t
adis16204_read_14bit_signed(struct device
*dev
,
175 struct device_attribute
*attr
,
178 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
179 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
183 mutex_lock(&indio_dev
->mlock
);
185 ret
= adis16204_spi_read_reg_16(indio_dev
,
186 this_attr
->address
, (u16
*)&val
);
188 if (val
& ADIS16204_ERROR_ACTIVE
)
189 adis16204_check_status(indio_dev
);
191 val
= ((s16
)(val
<< 2) >> 2);
192 ret
= sprintf(buf
, "%d\n", val
);
195 mutex_unlock(&indio_dev
->mlock
);
200 static int adis16204_reset(struct iio_dev
*indio_dev
)
203 ret
= adis16204_spi_write_reg_8(indio_dev
,
205 ADIS16204_GLOB_CMD_SW_RESET
);
207 dev_err(&indio_dev
->dev
, "problem resetting device");
212 static ssize_t
adis16204_write_reset(struct device
*dev
,
213 struct device_attribute
*attr
,
214 const char *buf
, size_t len
)
216 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
224 return adis16204_reset(indio_dev
);
229 int adis16204_set_irq(struct iio_dev
*indio_dev
, bool enable
)
234 ret
= adis16204_spi_read_reg_16(indio_dev
, ADIS16204_MSC_CTRL
, &msc
);
238 msc
|= ADIS16204_MSC_CTRL_ACTIVE_HIGH
;
239 msc
&= ~ADIS16204_MSC_CTRL_DATA_RDY_DIO2
;
241 msc
|= ADIS16204_MSC_CTRL_DATA_RDY_EN
;
243 msc
&= ~ADIS16204_MSC_CTRL_DATA_RDY_EN
;
245 ret
= adis16204_spi_write_reg_16(indio_dev
, ADIS16204_MSC_CTRL
, msc
);
251 static int adis16204_self_test(struct iio_dev
*indio_dev
)
254 ret
= adis16204_spi_write_reg_16(indio_dev
,
256 ADIS16204_MSC_CTRL_SELF_TEST_EN
);
258 dev_err(&indio_dev
->dev
, "problem starting self test");
262 adis16204_check_status(indio_dev
);
268 static int adis16204_initial_setup(struct iio_dev
*indio_dev
)
273 ret
= adis16204_set_irq(indio_dev
, false);
275 dev_err(&indio_dev
->dev
, "disable irq failed");
280 ret
= adis16204_self_test(indio_dev
);
282 dev_err(&indio_dev
->dev
, "self test failure");
286 /* Read status register to check the result */
287 ret
= adis16204_check_status(indio_dev
);
289 adis16204_reset(indio_dev
);
290 dev_err(&indio_dev
->dev
, "device not playing ball -> reset");
291 msleep(ADIS16204_STARTUP_DELAY
);
292 ret
= adis16204_check_status(indio_dev
);
294 dev_err(&indio_dev
->dev
, "giving up");
302 static IIO_DEV_ATTR_ACCEL_XY(adis16204_read_14bit_signed
,
303 ADIS16204_XY_RSS_OUT
);
304 static IIO_DEV_ATTR_ACCEL_XPEAK(adis16204_read_14bit_signed
,
305 ADIS16204_X_PEAK_OUT
);
306 static IIO_DEV_ATTR_ACCEL_YPEAK(adis16204_read_14bit_signed
,
307 ADIS16204_Y_PEAK_OUT
);
308 static IIO_DEV_ATTR_ACCEL_XYPEAK(adis16204_read_14bit_signed
,
309 ADIS16204_XY_PEAK_OUT
);
310 static IIO_CONST_ATTR(accel_xy_scale
, "0.017125");
312 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16204_write_reset
, 0);
314 enum adis16204_channel
{
322 static u8 adis16204_addresses
[5][2] = {
323 [in_supply
] = { ADIS16204_SUPPLY_OUT
},
324 [in_aux
] = { ADIS16204_AUX_ADC
},
325 [temp
] = { ADIS16204_TEMP_OUT
},
326 [accel_x
] = { ADIS16204_XACCL_OUT
, ADIS16204_XACCL_NULL
},
327 [accel_y
] = { ADIS16204_XACCL_OUT
, ADIS16204_YACCL_NULL
},
329 static int adis16204_read_raw(struct iio_dev
*indio_dev
,
330 struct iio_chan_spec
const *chan
,
341 mutex_lock(&indio_dev
->mlock
);
342 addr
= adis16204_addresses
[chan
->address
][0];
343 ret
= adis16204_spi_read_reg_16(indio_dev
, addr
, &val16
);
347 if (val16
& ADIS16204_ERROR_ACTIVE
) {
348 ret
= adis16204_check_status(indio_dev
);
352 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
353 if (chan
->scan_type
.sign
== 's')
354 val16
= (s16
)(val16
<<
355 (16 - chan
->scan_type
.realbits
)) >>
356 (16 - chan
->scan_type
.realbits
);
358 mutex_unlock(&indio_dev
->mlock
);
360 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
361 switch (chan
->type
) {
364 if (chan
->channel
== 0)
368 return IIO_VAL_INT_PLUS_MICRO
;
372 return IIO_VAL_INT_PLUS_MICRO
;
375 if (chan
->channel
== 'x')
379 return IIO_VAL_INT_PLUS_MICRO
;
384 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
):
387 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
388 switch (chan
->type
) {
395 mutex_lock(&indio_dev
->mlock
);
396 addr
= adis16204_addresses
[chan
->address
][1];
397 ret
= adis16204_spi_read_reg_16(indio_dev
, addr
, &val16
);
399 mutex_unlock(&indio_dev
->mlock
);
402 val16
&= (1 << bits
) - 1;
403 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
405 mutex_unlock(&indio_dev
->mlock
);
411 static int adis16204_write_raw(struct iio_dev
*indio_dev
,
412 struct iio_chan_spec
const *chan
,
421 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
422 switch (chan
->type
) {
429 val16
= val
& ((1 << bits
) - 1);
430 addr
= adis16204_addresses
[chan
->address
][1];
431 return adis16204_spi_write_reg_16(indio_dev
, addr
, val16
);
436 static struct iio_chan_spec adis16204_channels
[] = {
437 IIO_CHAN(IIO_IN
, 0, 0, 0, "supply", 0, 0,
438 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
439 in_supply
, ADIS16204_SCAN_SUPPLY
,
440 IIO_ST('u', 12, 16, 0), 0),
441 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
442 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
443 in_aux
, ADIS16204_SCAN_AUX_ADC
,
444 IIO_ST('u', 12, 16, 0), 0),
445 IIO_CHAN(IIO_TEMP
, 0, 1, 0, NULL
, 0, 0,
446 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
) |
447 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
),
448 temp
, ADIS16204_SCAN_TEMP
,
449 IIO_ST('u', 12, 16, 0), 0),
450 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_X
,
451 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
) |
452 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
453 accel_x
, ADIS16204_SCAN_ACC_X
,
454 IIO_ST('s', 14, 16, 0), 0),
455 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_Y
,
456 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
) |
457 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
458 accel_y
, ADIS16204_SCAN_ACC_Y
,
459 IIO_ST('s', 14, 16, 0), 0),
460 IIO_CHAN_SOFT_TIMESTAMP(5),
462 static struct attribute
*adis16204_attributes
[] = {
463 &iio_dev_attr_reset
.dev_attr
.attr
,
464 &iio_dev_attr_accel_xy
.dev_attr
.attr
,
465 &iio_dev_attr_accel_xpeak
.dev_attr
.attr
,
466 &iio_dev_attr_accel_ypeak
.dev_attr
.attr
,
467 &iio_dev_attr_accel_xypeak
.dev_attr
.attr
,
468 &iio_const_attr_accel_xy_scale
.dev_attr
.attr
,
472 static const struct attribute_group adis16204_attribute_group
= {
473 .attrs
= adis16204_attributes
,
476 static const struct iio_info adis16204_info
= {
477 .attrs
= &adis16204_attribute_group
,
478 .read_raw
= &adis16204_read_raw
,
479 .write_raw
= &adis16204_write_raw
,
480 .driver_module
= THIS_MODULE
,
483 static int __devinit
adis16204_probe(struct spi_device
*spi
)
485 int ret
, regdone
= 0;
486 struct adis16204_state
*st
;
487 struct iio_dev
*indio_dev
;
489 /* setup the industrialio driver allocated elements */
490 indio_dev
= iio_allocate_device(sizeof(*st
));
491 if (indio_dev
== NULL
) {
495 st
= iio_priv(indio_dev
);
496 /* this is only used for removal purposes */
497 spi_set_drvdata(spi
, indio_dev
);
499 mutex_init(&st
->buf_lock
);
501 indio_dev
->name
= spi
->dev
.driver
->name
;
502 indio_dev
->dev
.parent
= &spi
->dev
;
503 indio_dev
->info
= &adis16204_info
;
504 indio_dev
->channels
= adis16204_channels
;
505 indio_dev
->num_channels
= ARRAY_SIZE(adis16204_channels
);
506 indio_dev
->modes
= INDIO_DIRECT_MODE
;
508 ret
= adis16204_configure_ring(indio_dev
);
512 ret
= iio_device_register(indio_dev
);
514 goto error_unreg_ring_funcs
;
517 ret
= iio_ring_buffer_register_ex(indio_dev
->ring
, 0,
519 ARRAY_SIZE(adis16204_channels
));
521 printk(KERN_ERR
"failed to initialize the ring\n");
522 goto error_unreg_ring_funcs
;
526 ret
= adis16204_probe_trigger(indio_dev
);
528 goto error_uninitialize_ring
;
531 /* Get the device into a sane initial state */
532 ret
= adis16204_initial_setup(indio_dev
);
534 goto error_remove_trigger
;
537 error_remove_trigger
:
538 adis16204_remove_trigger(indio_dev
);
539 error_uninitialize_ring
:
540 iio_ring_buffer_unregister(indio_dev
->ring
);
541 error_unreg_ring_funcs
:
542 adis16204_unconfigure_ring(indio_dev
);
545 iio_device_unregister(indio_dev
);
547 iio_free_device(indio_dev
);
552 static int adis16204_remove(struct spi_device
*spi
)
554 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
556 adis16204_remove_trigger(indio_dev
);
557 iio_ring_buffer_unregister(indio_dev
->ring
);
558 iio_device_unregister(indio_dev
);
559 adis16204_unconfigure_ring(indio_dev
);
564 static struct spi_driver adis16204_driver
= {
567 .owner
= THIS_MODULE
,
569 .probe
= adis16204_probe
,
570 .remove
= __devexit_p(adis16204_remove
),
573 static __init
int adis16204_init(void)
575 return spi_register_driver(&adis16204_driver
);
577 module_init(adis16204_init
);
579 static __exit
void adis16204_exit(void)
581 spi_unregister_driver(&adis16204_driver
);
583 module_exit(adis16204_exit
);
585 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
586 MODULE_DESCRIPTION("ADIS16204 High-g Digital Impact Sensor and Recorder");
587 MODULE_LICENSE("GPL v2");