2 * ADIS16240 Programmable Impact Sensor and Recorder 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 "../adc/adc.h"
27 #include "adis16240.h"
29 #define DRIVER_NAME "adis16240"
31 static int adis16240_check_status(struct iio_dev
*indio_dev
);
34 * adis16240_spi_write_reg_8() - write single byte to a register
35 * @indio_dev: iio_dev associated with device
36 * @reg_address: the address of the register to be written
37 * @val: the value to write
39 static int adis16240_spi_write_reg_8(struct iio_dev
*indio_dev
,
44 struct adis16240_state
*st
= iio_priv(indio_dev
);
46 mutex_lock(&st
->buf_lock
);
47 st
->tx
[0] = ADIS16240_WRITE_REG(reg_address
);
50 ret
= spi_write(st
->us
, st
->tx
, 2);
51 mutex_unlock(&st
->buf_lock
);
57 * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers
58 * @indio_dev: iio_dev for this device
59 * @reg_address: the address of the lower of the two registers. Second register
60 * is assumed to have address one greater.
61 * @val: value to be written
63 static int adis16240_spi_write_reg_16(struct iio_dev
*indio_dev
,
68 struct spi_message msg
;
69 struct adis16240_state
*st
= iio_priv(indio_dev
);
70 struct spi_transfer xfers
[] = {
85 mutex_lock(&st
->buf_lock
);
86 st
->tx
[0] = ADIS16240_WRITE_REG(lower_reg_address
);
87 st
->tx
[1] = value
& 0xFF;
88 st
->tx
[2] = ADIS16240_WRITE_REG(lower_reg_address
+ 1);
89 st
->tx
[3] = (value
>> 8) & 0xFF;
91 spi_message_init(&msg
);
92 spi_message_add_tail(&xfers
[0], &msg
);
93 spi_message_add_tail(&xfers
[1], &msg
);
94 ret
= spi_sync(st
->us
, &msg
);
95 mutex_unlock(&st
->buf_lock
);
101 * adis16240_spi_read_reg_16() - read 2 bytes from a 16-bit register
102 * @indio_dev: iio_dev for this device
103 * @reg_address: the address of the lower of the two registers. Second register
104 * is assumed to have address one greater.
105 * @val: somewhere to pass back the value read
107 static int adis16240_spi_read_reg_16(struct iio_dev
*indio_dev
,
108 u8 lower_reg_address
,
111 struct spi_message msg
;
112 struct adis16240_state
*st
= iio_priv(indio_dev
);
114 struct spi_transfer xfers
[] = {
130 mutex_lock(&st
->buf_lock
);
131 st
->tx
[0] = ADIS16240_READ_REG(lower_reg_address
);
136 spi_message_init(&msg
);
137 spi_message_add_tail(&xfers
[0], &msg
);
138 spi_message_add_tail(&xfers
[1], &msg
);
139 ret
= spi_sync(st
->us
, &msg
);
141 dev_err(&st
->us
->dev
,
142 "problem when reading 16 bit register 0x%02X",
146 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
149 mutex_unlock(&st
->buf_lock
);
153 static ssize_t
adis16240_spi_read_signed(struct device
*dev
,
154 struct device_attribute
*attr
,
158 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
161 unsigned shift
= 16 - bits
;
162 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
164 ret
= adis16240_spi_read_reg_16(indio_dev
,
165 this_attr
->address
, (u16
*)&val
);
169 if (val
& ADIS16240_ERROR_ACTIVE
)
170 adis16240_check_status(indio_dev
);
172 val
= ((s16
)(val
<< shift
) >> shift
);
173 return sprintf(buf
, "%d\n", val
);
176 static ssize_t
adis16240_read_12bit_signed(struct device
*dev
,
177 struct device_attribute
*attr
,
181 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
183 /* Take the iio_dev status lock */
184 mutex_lock(&indio_dev
->mlock
);
185 ret
= adis16240_spi_read_signed(dev
, attr
, buf
, 12);
186 mutex_unlock(&indio_dev
->mlock
);
191 static int adis16240_reset(struct iio_dev
*indio_dev
)
194 ret
= adis16240_spi_write_reg_8(indio_dev
,
196 ADIS16240_GLOB_CMD_SW_RESET
);
198 dev_err(&indio_dev
->dev
, "problem resetting device");
203 static ssize_t
adis16240_write_reset(struct device
*dev
,
204 struct device_attribute
*attr
,
205 const char *buf
, size_t len
)
207 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
215 return adis16240_reset(indio_dev
);
220 int adis16240_set_irq(struct iio_dev
*indio_dev
, bool enable
)
225 ret
= adis16240_spi_read_reg_16(indio_dev
,
226 ADIS16240_MSC_CTRL
, &msc
);
230 msc
|= ADIS16240_MSC_CTRL_ACTIVE_HIGH
;
231 msc
&= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2
;
233 msc
|= ADIS16240_MSC_CTRL_DATA_RDY_EN
;
235 msc
&= ~ADIS16240_MSC_CTRL_DATA_RDY_EN
;
237 ret
= adis16240_spi_write_reg_16(indio_dev
,
238 ADIS16240_MSC_CTRL
, msc
);
244 static int adis16240_self_test(struct iio_dev
*indio_dev
)
247 ret
= adis16240_spi_write_reg_16(indio_dev
,
249 ADIS16240_MSC_CTRL_SELF_TEST_EN
);
251 dev_err(&indio_dev
->dev
, "problem starting self test");
255 msleep(ADIS16240_STARTUP_DELAY
);
257 adis16240_check_status(indio_dev
);
263 static int adis16240_check_status(struct iio_dev
*indio_dev
)
267 struct device
*dev
= &indio_dev
->dev
;
269 ret
= adis16240_spi_read_reg_16(indio_dev
,
270 ADIS16240_DIAG_STAT
, &status
);
273 dev_err(dev
, "Reading status failed\n");
278 if (status
& ADIS16240_DIAG_STAT_PWRON_FAIL
)
279 dev_err(dev
, "Power-on, self-test fail\n");
280 if (status
& ADIS16240_DIAG_STAT_SPI_FAIL
)
281 dev_err(dev
, "SPI failure\n");
282 if (status
& ADIS16240_DIAG_STAT_FLASH_UPT
)
283 dev_err(dev
, "Flash update failed\n");
284 if (status
& ADIS16240_DIAG_STAT_POWER_HIGH
)
285 dev_err(dev
, "Power supply above 3.625V\n");
286 if (status
& ADIS16240_DIAG_STAT_POWER_LOW
)
287 dev_err(dev
, "Power supply below 2.225V\n");
293 static int adis16240_initial_setup(struct iio_dev
*indio_dev
)
296 struct device
*dev
= &indio_dev
->dev
;
299 ret
= adis16240_set_irq(indio_dev
, false);
301 dev_err(dev
, "disable irq failed");
306 ret
= adis16240_self_test(indio_dev
);
308 dev_err(dev
, "self test failure");
312 /* Read status register to check the result */
313 ret
= adis16240_check_status(indio_dev
);
315 adis16240_reset(indio_dev
);
316 dev_err(dev
, "device not playing ball -> reset");
317 msleep(ADIS16240_STARTUP_DELAY
);
318 ret
= adis16240_check_status(indio_dev
);
320 dev_err(dev
, "giving up");
329 static IIO_DEVICE_ATTR(accel_xyz_squared_peak_raw
, S_IRUGO
,
330 adis16240_read_12bit_signed
, NULL
,
331 ADIS16240_XYZPEAK_OUT
);
333 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16240_write_reset
, 0);
335 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
337 enum adis16240_chan
{
346 static const u8 adis16240_addresses
[6][3] = {
347 [in_supply
] = { ADIS16240_SUPPLY_OUT
},
348 [in_aux
] = { ADIS16240_AUX_ADC
},
349 [accel_x
] = { ADIS16240_XACCL_OUT
, ADIS16240_XACCL_OFF
,
350 ADIS16240_XPEAK_OUT
},
351 [accel_y
] = { ADIS16240_YACCL_OUT
, ADIS16240_YACCL_OFF
,
352 ADIS16240_YPEAK_OUT
},
353 [accel_z
] = { ADIS16240_ZACCL_OUT
, ADIS16240_ZACCL_OFF
,
354 ADIS16240_ZPEAK_OUT
},
355 [temp
] = { ADIS16240_TEMP_OUT
},
358 static int adis16240_read_raw(struct iio_dev
*indio_dev
,
359 struct iio_chan_spec
const *chan
,
370 mutex_lock(&indio_dev
->mlock
);
371 addr
= adis16240_addresses
[chan
->address
][0];
372 ret
= adis16240_spi_read_reg_16(indio_dev
, addr
, &val16
);
374 mutex_unlock(&indio_dev
->mlock
);
378 if (val16
& ADIS16240_ERROR_ACTIVE
) {
379 ret
= adis16240_check_status(indio_dev
);
381 mutex_unlock(&indio_dev
->mlock
);
385 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
386 if (chan
->scan_type
.sign
== 's')
387 val16
= (s16
)(val16
<<
388 (16 - chan
->scan_type
.realbits
)) >>
389 (16 - chan
->scan_type
.realbits
);
391 mutex_unlock(&indio_dev
->mlock
);
393 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
394 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
395 switch (chan
->type
) {
398 if (chan
->channel
== 0)
402 return IIO_VAL_INT_PLUS_MICRO
;
406 return IIO_VAL_INT_PLUS_MICRO
;
410 return IIO_VAL_INT_PLUS_MICRO
;
415 case (1 << IIO_CHAN_INFO_PEAK_SCALE_SHARED
):
418 return IIO_VAL_INT_PLUS_MICRO
;
419 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
):
422 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
424 mutex_lock(&indio_dev
->mlock
);
425 addr
= adis16240_addresses
[chan
->address
][1];
426 ret
= adis16240_spi_read_reg_16(indio_dev
, addr
, &val16
);
428 mutex_unlock(&indio_dev
->mlock
);
431 val16
&= (1 << bits
) - 1;
432 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
434 mutex_unlock(&indio_dev
->mlock
);
436 case (1 << IIO_CHAN_INFO_PEAK_SEPARATE
):
438 mutex_lock(&indio_dev
->mlock
);
439 addr
= adis16240_addresses
[chan
->address
][2];
440 ret
= adis16240_spi_read_reg_16(indio_dev
, addr
, &val16
);
442 mutex_unlock(&indio_dev
->mlock
);
445 val16
&= (1 << bits
) - 1;
446 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
448 mutex_unlock(&indio_dev
->mlock
);
454 static int adis16240_write_raw(struct iio_dev
*indio_dev
,
455 struct iio_chan_spec
const *chan
,
464 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
465 val16
= val
& ((1 << bits
) - 1);
466 addr
= adis16240_addresses
[chan
->address
][1];
467 return adis16240_spi_write_reg_16(indio_dev
, addr
, val16
);
472 static struct iio_chan_spec adis16240_channels
[] = {
473 IIO_CHAN(IIO_IN
, 0, 1, 0, "supply", 0, 0,
474 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
475 in_supply
, ADIS16240_SCAN_SUPPLY
,
476 IIO_ST('u', 10, 16, 0), 0),
477 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
479 in_aux
, ADIS16240_SCAN_AUX_ADC
,
480 IIO_ST('u', 10, 16, 0), 0),
481 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_X
,
482 (1 << IIO_CHAN_INFO_SCALE_SHARED
) |
483 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
484 accel_x
, ADIS16240_SCAN_ACC_X
,
485 IIO_ST('s', 10, 16, 0), 0),
486 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_Y
,
487 (1 << IIO_CHAN_INFO_SCALE_SHARED
) |
488 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
489 accel_y
, ADIS16240_SCAN_ACC_Y
,
490 IIO_ST('s', 10, 16, 0), 0),
491 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_Z
,
492 (1 << IIO_CHAN_INFO_SCALE_SHARED
) |
493 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
494 accel_z
, ADIS16240_SCAN_ACC_Z
,
495 IIO_ST('s', 10, 16, 0), 0),
496 IIO_CHAN(IIO_TEMP
, 0, 1, 0, NULL
, 0, 0,
497 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
498 temp
, ADIS16240_SCAN_TEMP
,
499 IIO_ST('u', 10, 16, 0), 0),
500 IIO_CHAN_SOFT_TIMESTAMP(6)
503 static struct attribute
*adis16240_attributes
[] = {
504 &iio_dev_attr_accel_xyz_squared_peak_raw
.dev_attr
.attr
,
505 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
506 &iio_dev_attr_reset
.dev_attr
.attr
,
510 static const struct attribute_group adis16240_attribute_group
= {
511 .attrs
= adis16240_attributes
,
514 static const struct iio_info adis16240_info
= {
515 .attrs
= &adis16240_attribute_group
,
516 .read_raw
= &adis16240_read_raw
,
517 .write_raw
= &adis16240_write_raw
,
518 .driver_module
= THIS_MODULE
,
521 static int __devinit
adis16240_probe(struct spi_device
*spi
)
523 int ret
, regdone
= 0;
524 struct adis16240_state
*st
;
525 struct iio_dev
*indio_dev
;
527 /* setup the industrialio driver allocated elements */
528 indio_dev
= iio_allocate_device(sizeof(*st
));
529 if (indio_dev
== NULL
) {
533 st
= iio_priv(indio_dev
);
534 /* this is only used for removal purposes */
535 spi_set_drvdata(spi
, indio_dev
);
538 mutex_init(&st
->buf_lock
);
540 indio_dev
->name
= spi
->dev
.driver
->name
;
541 indio_dev
->dev
.parent
= &spi
->dev
;
542 indio_dev
->info
= &adis16240_info
;
543 indio_dev
->channels
= adis16240_channels
;
544 indio_dev
->num_channels
= ARRAY_SIZE(adis16240_channels
);
545 indio_dev
->modes
= INDIO_DIRECT_MODE
;
547 ret
= adis16240_configure_ring(indio_dev
);
551 ret
= iio_device_register(indio_dev
);
553 goto error_unreg_ring_funcs
;
556 ret
= iio_ring_buffer_register_ex(indio_dev
->ring
, 0,
558 ARRAY_SIZE(adis16240_channels
));
560 printk(KERN_ERR
"failed to initialize the ring\n");
561 goto error_unreg_ring_funcs
;
565 ret
= adis16240_probe_trigger(indio_dev
);
567 goto error_uninitialize_ring
;
570 /* Get the device into a sane initial state */
571 ret
= adis16240_initial_setup(indio_dev
);
573 goto error_remove_trigger
;
576 error_remove_trigger
:
577 adis16240_remove_trigger(indio_dev
);
578 error_uninitialize_ring
:
579 iio_ring_buffer_unregister(indio_dev
->ring
);
580 error_unreg_ring_funcs
:
581 adis16240_unconfigure_ring(indio_dev
);
584 iio_device_unregister(indio_dev
);
586 iio_free_device(indio_dev
);
591 static int adis16240_remove(struct spi_device
*spi
)
594 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
596 flush_scheduled_work();
598 adis16240_remove_trigger(indio_dev
);
599 iio_ring_buffer_unregister(indio_dev
->ring
);
600 iio_device_unregister(indio_dev
);
601 adis16240_unconfigure_ring(indio_dev
);
606 static struct spi_driver adis16240_driver
= {
609 .owner
= THIS_MODULE
,
611 .probe
= adis16240_probe
,
612 .remove
= __devexit_p(adis16240_remove
),
615 static __init
int adis16240_init(void)
617 return spi_register_driver(&adis16240_driver
);
619 module_init(adis16240_init
);
621 static __exit
void adis16240_exit(void)
623 spi_unregister_driver(&adis16240_driver
);
625 module_exit(adis16240_exit
);
627 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
628 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
629 MODULE_LICENSE("GPL v2");