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 device
*dev
);
34 * adis16240_spi_write_reg_8() - write single byte to a register
35 * @dev: device associated with child of actual device (iio_dev or iio_trig)
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 device
*dev
,
44 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
45 struct adis16240_state
*st
= iio_dev_get_devdata(indio_dev
);
47 mutex_lock(&st
->buf_lock
);
48 st
->tx
[0] = ADIS16240_WRITE_REG(reg_address
);
51 ret
= spi_write(st
->us
, st
->tx
, 2);
52 mutex_unlock(&st
->buf_lock
);
58 * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers
59 * @dev: device associated with child of actual device (iio_dev or iio_trig)
60 * @reg_address: the address of the lower of the two registers. Second register
61 * is assumed to have address one greater.
62 * @val: value to be written
64 static int adis16240_spi_write_reg_16(struct device
*dev
,
69 struct spi_message msg
;
70 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
71 struct adis16240_state
*st
= iio_dev_get_devdata(indio_dev
);
72 struct spi_transfer xfers
[] = {
88 mutex_lock(&st
->buf_lock
);
89 st
->tx
[0] = ADIS16240_WRITE_REG(lower_reg_address
);
90 st
->tx
[1] = value
& 0xFF;
91 st
->tx
[2] = ADIS16240_WRITE_REG(lower_reg_address
+ 1);
92 st
->tx
[3] = (value
>> 8) & 0xFF;
94 spi_message_init(&msg
);
95 spi_message_add_tail(&xfers
[0], &msg
);
96 spi_message_add_tail(&xfers
[1], &msg
);
97 ret
= spi_sync(st
->us
, &msg
);
98 mutex_unlock(&st
->buf_lock
);
104 * adis16240_spi_read_reg_16() - read 2 bytes from a 16-bit register
105 * @dev: device associated with child of actual device (iio_dev or iio_trig)
106 * @reg_address: the address of the lower of the two registers. Second register
107 * is assumed to have address one greater.
108 * @val: somewhere to pass back the value read
110 static int adis16240_spi_read_reg_16(struct device
*dev
,
111 u8 lower_reg_address
,
114 struct spi_message msg
;
115 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
116 struct adis16240_state
*st
= iio_dev_get_devdata(indio_dev
);
118 struct spi_transfer xfers
[] = {
134 mutex_lock(&st
->buf_lock
);
135 st
->tx
[0] = ADIS16240_READ_REG(lower_reg_address
);
140 spi_message_init(&msg
);
141 spi_message_add_tail(&xfers
[0], &msg
);
142 spi_message_add_tail(&xfers
[1], &msg
);
143 ret
= spi_sync(st
->us
, &msg
);
145 dev_err(&st
->us
->dev
,
146 "problem when reading 16 bit register 0x%02X",
150 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
153 mutex_unlock(&st
->buf_lock
);
157 static ssize_t
adis16240_spi_read_signed(struct device
*dev
,
158 struct device_attribute
*attr
,
164 unsigned shift
= 16 - bits
;
165 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
167 ret
= adis16240_spi_read_reg_16(dev
, this_attr
->address
, (u16
*)&val
);
171 if (val
& ADIS16240_ERROR_ACTIVE
)
172 adis16240_check_status(dev
);
174 val
= ((s16
)(val
<< shift
) >> shift
);
175 return sprintf(buf
, "%d\n", val
);
178 static ssize_t
adis16240_read_10bit_unsigned(struct device
*dev
,
179 struct device_attribute
*attr
,
184 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
186 ret
= adis16240_spi_read_reg_16(dev
, this_attr
->address
, &val
);
190 if (val
& ADIS16240_ERROR_ACTIVE
)
191 adis16240_check_status(dev
);
193 return sprintf(buf
, "%u\n", val
& 0x03FF);
196 static ssize_t
adis16240_read_10bit_signed(struct device
*dev
,
197 struct device_attribute
*attr
,
200 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
203 /* Take the iio_dev status lock */
204 mutex_lock(&indio_dev
->mlock
);
205 ret
= adis16240_spi_read_signed(dev
, attr
, buf
, 10);
206 mutex_unlock(&indio_dev
->mlock
);
211 static ssize_t
adis16240_read_12bit_signed(struct device
*dev
,
212 struct device_attribute
*attr
,
215 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
218 /* Take the iio_dev status lock */
219 mutex_lock(&indio_dev
->mlock
);
220 ret
= adis16240_spi_read_signed(dev
, attr
, buf
, 12);
221 mutex_unlock(&indio_dev
->mlock
);
226 static ssize_t
adis16240_write_16bit(struct device
*dev
,
227 struct device_attribute
*attr
,
231 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
235 ret
= strict_strtol(buf
, 10, &val
);
238 ret
= adis16240_spi_write_reg_16(dev
, this_attr
->address
, val
);
241 return ret
? ret
: len
;
244 static int adis16240_reset(struct device
*dev
)
247 ret
= adis16240_spi_write_reg_8(dev
,
249 ADIS16240_GLOB_CMD_SW_RESET
);
251 dev_err(dev
, "problem resetting device");
256 static ssize_t
adis16240_write_reset(struct device
*dev
,
257 struct device_attribute
*attr
,
258 const char *buf
, size_t len
)
266 return adis16240_reset(dev
);
271 int adis16240_set_irq(struct device
*dev
, bool enable
)
276 ret
= adis16240_spi_read_reg_16(dev
, ADIS16240_MSC_CTRL
, &msc
);
280 msc
|= ADIS16240_MSC_CTRL_ACTIVE_HIGH
;
281 msc
&= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2
;
283 msc
|= ADIS16240_MSC_CTRL_DATA_RDY_EN
;
285 msc
&= ~ADIS16240_MSC_CTRL_DATA_RDY_EN
;
287 ret
= adis16240_spi_write_reg_16(dev
, ADIS16240_MSC_CTRL
, msc
);
293 static int adis16240_self_test(struct device
*dev
)
296 ret
= adis16240_spi_write_reg_16(dev
,
298 ADIS16240_MSC_CTRL_SELF_TEST_EN
);
300 dev_err(dev
, "problem starting self test");
304 msleep(ADIS16240_STARTUP_DELAY
);
306 adis16240_check_status(dev
);
312 static int adis16240_check_status(struct device
*dev
)
317 ret
= adis16240_spi_read_reg_16(dev
, ADIS16240_DIAG_STAT
, &status
);
320 dev_err(dev
, "Reading status failed\n");
325 if (status
& ADIS16240_DIAG_STAT_PWRON_FAIL
)
326 dev_err(dev
, "Power-on, self-test fail\n");
327 if (status
& ADIS16240_DIAG_STAT_SPI_FAIL
)
328 dev_err(dev
, "SPI failure\n");
329 if (status
& ADIS16240_DIAG_STAT_FLASH_UPT
)
330 dev_err(dev
, "Flash update failed\n");
331 if (status
& ADIS16240_DIAG_STAT_POWER_HIGH
)
332 dev_err(dev
, "Power supply above 3.625V\n");
333 if (status
& ADIS16240_DIAG_STAT_POWER_LOW
)
334 dev_err(dev
, "Power supply below 2.225V\n");
340 static int adis16240_initial_setup(struct adis16240_state
*st
)
343 struct device
*dev
= &st
->indio_dev
->dev
;
346 ret
= adis16240_set_irq(dev
, false);
348 dev_err(dev
, "disable irq failed");
353 ret
= adis16240_self_test(dev
);
355 dev_err(dev
, "self test failure");
359 /* Read status register to check the result */
360 ret
= adis16240_check_status(dev
);
362 adis16240_reset(dev
);
363 dev_err(dev
, "device not playing ball -> reset");
364 msleep(ADIS16240_STARTUP_DELAY
);
365 ret
= adis16240_check_status(dev
);
367 dev_err(dev
, "giving up");
372 printk(KERN_INFO DRIVER_NAME
": at CS%d (irq %d)\n",
373 st
->us
->chip_select
, st
->us
->irq
);
379 static IIO_DEV_ATTR_IN_NAMED_RAW(supply
, adis16240_read_10bit_unsigned
,
380 ADIS16240_SUPPLY_OUT
);
381 static IIO_DEV_ATTR_IN_RAW(0, adis16240_read_10bit_signed
,
383 static IIO_CONST_ATTR(in_supply_scale
, "0.00488");
384 static IIO_DEV_ATTR_ACCEL_X(adis16240_read_10bit_signed
,
385 ADIS16240_XACCL_OUT
);
386 static IIO_DEVICE_ATTR(accel_x_peak_raw
, S_IRUGO
,
387 adis16240_read_10bit_signed
, NULL
,
388 ADIS16240_XPEAK_OUT
);
389 static IIO_DEV_ATTR_ACCEL_Y(adis16240_read_10bit_signed
,
390 ADIS16240_YACCL_OUT
);
391 static IIO_DEVICE_ATTR(accel_y_peak_raw
, S_IRUGO
,
392 adis16240_read_10bit_signed
, NULL
,
393 ADIS16240_YPEAK_OUT
);
394 static IIO_DEV_ATTR_ACCEL_Z(adis16240_read_10bit_signed
,
395 ADIS16240_ZACCL_OUT
);
396 static IIO_DEVICE_ATTR(accel_z_peak_raw
, S_IRUGO
,
397 adis16240_read_10bit_signed
, NULL
,
398 ADIS16240_ZPEAK_OUT
);
400 static IIO_DEVICE_ATTR(accel_xyz_squared_peak_raw
, S_IRUGO
,
401 adis16240_read_12bit_signed
, NULL
,
402 ADIS16240_XYZPEAK_OUT
);
403 static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR
| S_IRUGO
,
404 adis16240_read_10bit_signed
,
405 adis16240_write_16bit
,
406 ADIS16240_XACCL_OFF
);
407 static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR
| S_IRUGO
,
408 adis16240_read_10bit_signed
,
409 adis16240_write_16bit
,
410 ADIS16240_YACCL_OFF
);
411 static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR
| S_IRUGO
,
412 adis16240_read_10bit_signed
,
413 adis16240_write_16bit
,
414 ADIS16240_ZACCL_OFF
);
415 static IIO_DEV_ATTR_TEMP_RAW(adis16240_read_10bit_unsigned
);
416 static IIO_CONST_ATTR(temp_scale
, "0.244");
418 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, adis16240_write_reset
, 0);
420 static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("4096");
422 static IIO_CONST_ATTR(name
, "adis16240");
424 static struct attribute
*adis16240_event_attributes
[] = {
428 static struct attribute_group adis16240_event_attribute_group
= {
429 .attrs
= adis16240_event_attributes
,
432 static struct attribute
*adis16240_attributes
[] = {
433 &iio_dev_attr_in_supply_raw
.dev_attr
.attr
,
434 &iio_const_attr_in_supply_scale
.dev_attr
.attr
,
435 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
436 &iio_dev_attr_accel_x_raw
.dev_attr
.attr
,
437 &iio_dev_attr_accel_x_offset
.dev_attr
.attr
,
438 &iio_dev_attr_accel_x_peak_raw
.dev_attr
.attr
,
439 &iio_dev_attr_accel_y_raw
.dev_attr
.attr
,
440 &iio_dev_attr_accel_y_offset
.dev_attr
.attr
,
441 &iio_dev_attr_accel_y_peak_raw
.dev_attr
.attr
,
442 &iio_dev_attr_accel_z_raw
.dev_attr
.attr
,
443 &iio_dev_attr_accel_z_offset
.dev_attr
.attr
,
444 &iio_dev_attr_accel_z_peak_raw
.dev_attr
.attr
,
445 &iio_dev_attr_accel_xyz_squared_peak_raw
.dev_attr
.attr
,
446 &iio_dev_attr_temp_raw
.dev_attr
.attr
,
447 &iio_const_attr_temp_scale
.dev_attr
.attr
,
448 &iio_const_attr_available_sampling_frequency
.dev_attr
.attr
,
449 &iio_dev_attr_reset
.dev_attr
.attr
,
450 &iio_const_attr_name
.dev_attr
.attr
,
454 static const struct attribute_group adis16240_attribute_group
= {
455 .attrs
= adis16240_attributes
,
458 static int __devinit
adis16240_probe(struct spi_device
*spi
)
460 int ret
, regdone
= 0;
461 struct adis16240_state
*st
= kzalloc(sizeof *st
, GFP_KERNEL
);
466 /* this is only used for removal purposes */
467 spi_set_drvdata(spi
, st
);
469 /* Allocate the comms buffers */
470 st
->rx
= kzalloc(sizeof(*st
->rx
)*ADIS16240_MAX_RX
, GFP_KERNEL
);
471 if (st
->rx
== NULL
) {
475 st
->tx
= kzalloc(sizeof(*st
->tx
)*ADIS16240_MAX_TX
, GFP_KERNEL
);
476 if (st
->tx
== NULL
) {
481 mutex_init(&st
->buf_lock
);
482 /* setup the industrialio driver allocated elements */
483 st
->indio_dev
= iio_allocate_device();
484 if (st
->indio_dev
== NULL
) {
489 st
->indio_dev
->dev
.parent
= &spi
->dev
;
490 st
->indio_dev
->num_interrupt_lines
= 1;
491 st
->indio_dev
->event_attrs
= &adis16240_event_attribute_group
;
492 st
->indio_dev
->attrs
= &adis16240_attribute_group
;
493 st
->indio_dev
->dev_data
= (void *)(st
);
494 st
->indio_dev
->driver_module
= THIS_MODULE
;
495 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
497 ret
= adis16240_configure_ring(st
->indio_dev
);
501 ret
= iio_device_register(st
->indio_dev
);
503 goto error_unreg_ring_funcs
;
506 ret
= iio_ring_buffer_register(st
->indio_dev
->ring
, 0);
508 printk(KERN_ERR
"failed to initialize the ring\n");
509 goto error_unreg_ring_funcs
;
513 ret
= iio_register_interrupt_line(spi
->irq
,
519 goto error_uninitialize_ring
;
521 ret
= adis16240_probe_trigger(st
->indio_dev
);
523 goto error_unregister_line
;
526 /* Get the device into a sane initial state */
527 ret
= adis16240_initial_setup(st
);
529 goto error_remove_trigger
;
532 error_remove_trigger
:
533 adis16240_remove_trigger(st
->indio_dev
);
534 error_unregister_line
:
536 iio_unregister_interrupt_line(st
->indio_dev
, 0);
537 error_uninitialize_ring
:
538 iio_ring_buffer_unregister(st
->indio_dev
->ring
);
539 error_unreg_ring_funcs
:
540 adis16240_unconfigure_ring(st
->indio_dev
);
543 iio_device_unregister(st
->indio_dev
);
545 iio_free_device(st
->indio_dev
);
556 static int adis16240_remove(struct spi_device
*spi
)
558 struct adis16240_state
*st
= spi_get_drvdata(spi
);
559 struct iio_dev
*indio_dev
= st
->indio_dev
;
561 flush_scheduled_work();
563 adis16240_remove_trigger(indio_dev
);
565 iio_unregister_interrupt_line(indio_dev
, 0);
567 iio_ring_buffer_unregister(indio_dev
->ring
);
568 iio_device_unregister(indio_dev
);
569 adis16240_unconfigure_ring(indio_dev
);
577 static struct spi_driver adis16240_driver
= {
580 .owner
= THIS_MODULE
,
582 .probe
= adis16240_probe
,
583 .remove
= __devexit_p(adis16240_remove
),
586 static __init
int adis16240_init(void)
588 return spi_register_driver(&adis16240_driver
);
590 module_init(adis16240_init
);
592 static __exit
void adis16240_exit(void)
594 spi_unregister_driver(&adis16240_driver
);
596 module_exit(adis16240_exit
);
598 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
599 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
600 MODULE_LICENSE("GPL v2");