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
);
376 if (val16
& ADIS16240_ERROR_ACTIVE
) {
377 ret
= adis16240_check_status(indio_dev
);
381 val16
= val16
& ((1 << chan
->scan_type
.realbits
) - 1);
382 if (chan
->scan_type
.sign
== 's')
383 val16
= (s16
)(val16
<<
384 (16 - chan
->scan_type
.realbits
)) >>
385 (16 - chan
->scan_type
.realbits
);
387 mutex_unlock(&indio_dev
->mlock
);
389 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
390 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
391 switch (chan
->type
) {
394 if (chan
->channel
== 0)
398 return IIO_VAL_INT_PLUS_MICRO
;
402 return IIO_VAL_INT_PLUS_MICRO
;
406 return IIO_VAL_INT_PLUS_MICRO
;
411 case (1 << IIO_CHAN_INFO_PEAK_SCALE_SHARED
):
414 return IIO_VAL_INT_PLUS_MICRO
;
415 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
):
418 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
420 mutex_lock(&indio_dev
->mlock
);
421 addr
= adis16240_addresses
[chan
->address
][1];
422 ret
= adis16240_spi_read_reg_16(indio_dev
, addr
, &val16
);
424 mutex_unlock(&indio_dev
->mlock
);
427 val16
&= (1 << bits
) - 1;
428 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
430 mutex_unlock(&indio_dev
->mlock
);
432 case (1 << IIO_CHAN_INFO_PEAK_SEPARATE
):
434 mutex_lock(&indio_dev
->mlock
);
435 addr
= adis16240_addresses
[chan
->address
][2];
436 ret
= adis16240_spi_read_reg_16(indio_dev
, addr
, &val16
);
438 mutex_unlock(&indio_dev
->mlock
);
441 val16
&= (1 << bits
) - 1;
442 val16
= (s16
)(val16
<< (16 - bits
)) >> (16 - bits
);
444 mutex_unlock(&indio_dev
->mlock
);
450 static int adis16240_write_raw(struct iio_dev
*indio_dev
,
451 struct iio_chan_spec
const *chan
,
460 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
461 val16
= val
& ((1 << bits
) - 1);
462 addr
= adis16240_addresses
[chan
->address
][1];
463 return adis16240_spi_write_reg_16(indio_dev
, addr
, val16
);
468 static struct iio_chan_spec adis16240_channels
[] = {
469 IIO_CHAN(IIO_IN
, 0, 1, 0, "supply", 0, 0,
470 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
471 in_supply
, ADIS16240_SCAN_SUPPLY
,
472 IIO_ST('u', 10, 16, 0), 0),
473 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
475 in_aux
, ADIS16240_SCAN_AUX_ADC
,
476 IIO_ST('u', 10, 16, 0), 0),
477 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_X
,
478 (1 << IIO_CHAN_INFO_SCALE_SHARED
) |
479 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
480 accel_x
, ADIS16240_SCAN_ACC_X
,
481 IIO_ST('s', 10, 16, 0), 0),
482 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_Y
,
483 (1 << IIO_CHAN_INFO_SCALE_SHARED
) |
484 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
485 accel_y
, ADIS16240_SCAN_ACC_Y
,
486 IIO_ST('s', 10, 16, 0), 0),
487 IIO_CHAN(IIO_ACCEL
, 1, 0, 0, NULL
, 0, IIO_MOD_Z
,
488 (1 << IIO_CHAN_INFO_SCALE_SHARED
) |
489 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
490 accel_z
, ADIS16240_SCAN_ACC_Z
,
491 IIO_ST('s', 10, 16, 0), 0),
492 IIO_CHAN(IIO_TEMP
, 0, 1, 0, NULL
, 0, 0,
493 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
494 temp
, ADIS16240_SCAN_TEMP
,
495 IIO_ST('u', 10, 16, 0), 0),
496 IIO_CHAN_SOFT_TIMESTAMP(6)
499 static struct attribute
*adis16240_attributes
[] = {
500 &iio_dev_attr_accel_xyz_squared_peak_raw
.dev_attr
.attr
,
501 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
502 &iio_dev_attr_reset
.dev_attr
.attr
,
506 static const struct attribute_group adis16240_attribute_group
= {
507 .attrs
= adis16240_attributes
,
510 static const struct iio_info adis16240_info
= {
511 .attrs
= &adis16240_attribute_group
,
512 .read_raw
= &adis16240_read_raw
,
513 .write_raw
= &adis16240_write_raw
,
514 .driver_module
= THIS_MODULE
,
517 static int __devinit
adis16240_probe(struct spi_device
*spi
)
519 int ret
, regdone
= 0;
520 struct adis16240_state
*st
;
521 struct iio_dev
*indio_dev
;
523 /* setup the industrialio driver allocated elements */
524 indio_dev
= iio_allocate_device(sizeof(*st
));
525 if (indio_dev
== NULL
) {
529 st
= iio_priv(indio_dev
);
530 /* this is only used for removal purposes */
531 spi_set_drvdata(spi
, indio_dev
);
534 mutex_init(&st
->buf_lock
);
536 indio_dev
->name
= spi
->dev
.driver
->name
;
537 indio_dev
->dev
.parent
= &spi
->dev
;
538 indio_dev
->info
= &adis16240_info
;
539 indio_dev
->channels
= adis16240_channels
;
540 indio_dev
->num_channels
= ARRAY_SIZE(adis16240_channels
);
541 indio_dev
->modes
= INDIO_DIRECT_MODE
;
543 ret
= adis16240_configure_ring(indio_dev
);
547 ret
= iio_device_register(indio_dev
);
549 goto error_unreg_ring_funcs
;
552 ret
= iio_ring_buffer_register_ex(indio_dev
->ring
, 0,
554 ARRAY_SIZE(adis16240_channels
));
556 printk(KERN_ERR
"failed to initialize the ring\n");
557 goto error_unreg_ring_funcs
;
561 ret
= adis16240_probe_trigger(indio_dev
);
563 goto error_uninitialize_ring
;
566 /* Get the device into a sane initial state */
567 ret
= adis16240_initial_setup(indio_dev
);
569 goto error_remove_trigger
;
572 error_remove_trigger
:
573 adis16240_remove_trigger(indio_dev
);
574 error_uninitialize_ring
:
575 iio_ring_buffer_unregister(indio_dev
->ring
);
576 error_unreg_ring_funcs
:
577 adis16240_unconfigure_ring(indio_dev
);
580 iio_device_unregister(indio_dev
);
582 iio_free_device(indio_dev
);
587 static int adis16240_remove(struct spi_device
*spi
)
590 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
592 flush_scheduled_work();
594 adis16240_remove_trigger(indio_dev
);
595 iio_ring_buffer_unregister(indio_dev
->ring
);
596 iio_device_unregister(indio_dev
);
597 adis16240_unconfigure_ring(indio_dev
);
602 static struct spi_driver adis16240_driver
= {
605 .owner
= THIS_MODULE
,
607 .probe
= adis16240_probe
,
608 .remove
= __devexit_p(adis16240_remove
),
611 static __init
int adis16240_init(void)
613 return spi_register_driver(&adis16240_driver
);
615 module_init(adis16240_init
);
617 static __exit
void adis16240_exit(void)
619 spi_unregister_driver(&adis16240_driver
);
621 module_exit(adis16240_exit
);
623 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
624 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
625 MODULE_LICENSE("GPL v2");