2 * ADIS16220 Programmable Digital Vibration Sensor driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/delay.h>
10 #include <linux/mutex.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/module.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 #include "adis16220.h"
23 static ssize_t
adis16220_read_16bit(struct device
*dev
,
24 struct device_attribute
*attr
,
27 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
28 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
29 struct adis16220_state
*st
= iio_priv(indio_dev
);
33 /* Take the iio_dev status lock */
34 mutex_lock(&indio_dev
->mlock
);
35 ret
= adis_read_reg_16(&st
->adis
, this_attr
->address
,
37 mutex_unlock(&indio_dev
->mlock
);
40 return sprintf(buf
, "%d\n", val
);
43 static ssize_t
adis16220_write_16bit(struct device
*dev
,
44 struct device_attribute
*attr
,
48 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
49 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
50 struct adis16220_state
*st
= iio_priv(indio_dev
);
54 ret
= kstrtou16(buf
, 10, &val
);
57 ret
= adis_write_reg_16(&st
->adis
, this_attr
->address
, val
);
60 return ret
? ret
: len
;
63 static int adis16220_capture(struct iio_dev
*indio_dev
)
65 struct adis16220_state
*st
= iio_priv(indio_dev
);
68 /* initiates a manual data capture */
69 ret
= adis_write_reg_16(&st
->adis
, ADIS16220_GLOB_CMD
, 0xBF08);
71 dev_err(&indio_dev
->dev
, "problem beginning capture");
73 msleep(10); /* delay for capture to finish */
78 static ssize_t
adis16220_write_capture(struct device
*dev
,
79 struct device_attribute
*attr
,
80 const char *buf
, size_t len
)
82 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
86 ret
= strtobool(buf
, &val
);
91 ret
= adis16220_capture(indio_dev
);
98 static ssize_t
adis16220_capture_buffer_read(struct iio_dev
*indio_dev
,
104 struct adis16220_state
*st
= iio_priv(indio_dev
);
105 struct spi_message msg
;
106 struct spi_transfer xfers
[] = {
124 if (unlikely(!count
))
127 if ((off
>= ADIS16220_CAPTURE_SIZE
) || (count
& 1) || (off
& 1))
130 if (off
+ count
> ADIS16220_CAPTURE_SIZE
)
131 count
= ADIS16220_CAPTURE_SIZE
- off
;
133 /* write the begin position of capture buffer */
134 ret
= adis_write_reg_16(&st
->adis
,
140 /* read count/2 values from capture buffer */
141 mutex_lock(&st
->buf_lock
);
144 for (i
= 0; i
< count
; i
+= 2) {
145 st
->tx
[i
] = ADIS_READ_REG(addr
);
148 xfers
[1].len
= count
;
150 spi_message_init(&msg
);
151 spi_message_add_tail(&xfers
[0], &msg
);
152 spi_message_add_tail(&xfers
[1], &msg
);
153 ret
= spi_sync(st
->adis
.spi
, &msg
);
156 mutex_unlock(&st
->buf_lock
);
160 memcpy(buf
, st
->rx
, count
);
162 mutex_unlock(&st
->buf_lock
);
166 static ssize_t
adis16220_accel_bin_read(struct file
*filp
, struct kobject
*kobj
,
167 struct bin_attribute
*attr
,
172 struct iio_dev
*indio_dev
= dev_to_iio_dev(kobj_to_dev(kobj
));
174 return adis16220_capture_buffer_read(indio_dev
, buf
,
176 ADIS16220_CAPT_BUFA
);
179 static struct bin_attribute accel_bin
= {
184 .read
= adis16220_accel_bin_read
,
185 .size
= ADIS16220_CAPTURE_SIZE
,
188 static ssize_t
adis16220_adc1_bin_read(struct file
*filp
, struct kobject
*kobj
,
189 struct bin_attribute
*attr
,
190 char *buf
, loff_t off
,
193 struct iio_dev
*indio_dev
= dev_to_iio_dev(kobj_to_dev(kobj
));
195 return adis16220_capture_buffer_read(indio_dev
, buf
,
197 ADIS16220_CAPT_BUF1
);
200 static struct bin_attribute adc1_bin
= {
205 .read
= adis16220_adc1_bin_read
,
206 .size
= ADIS16220_CAPTURE_SIZE
,
209 static ssize_t
adis16220_adc2_bin_read(struct file
*filp
, struct kobject
*kobj
,
210 struct bin_attribute
*attr
,
211 char *buf
, loff_t off
,
214 struct iio_dev
*indio_dev
= dev_to_iio_dev(kobj_to_dev(kobj
));
216 return adis16220_capture_buffer_read(indio_dev
, buf
,
218 ADIS16220_CAPT_BUF2
);
222 static struct bin_attribute adc2_bin
= {
227 .read
= adis16220_adc2_bin_read
,
228 .size
= ADIS16220_CAPTURE_SIZE
,
231 #define IIO_DEV_ATTR_CAPTURE(_store) \
232 IIO_DEVICE_ATTR(capture, S_IWUSR, NULL, _store, 0)
234 static IIO_DEV_ATTR_CAPTURE(adis16220_write_capture
);
236 #define IIO_DEV_ATTR_CAPTURE_COUNT(_mode, _show, _store, _addr) \
237 IIO_DEVICE_ATTR(capture_count, _mode, _show, _store, _addr)
239 static IIO_DEV_ATTR_CAPTURE_COUNT(S_IWUSR
| S_IRUGO
,
240 adis16220_read_16bit
,
241 adis16220_write_16bit
,
242 ADIS16220_CAPT_PNTR
);
244 enum adis16220_channel
{
245 in_supply
, in_1
, in_2
, accel
, temp
248 struct adis16220_address_spec
{
254 /* Address / bits / signed */
255 static const struct adis16220_address_spec adis16220_addresses
[][3] = {
256 [in_supply
] = { { ADIS16220_CAPT_SUPPLY
, 12, 0 }, },
257 [in_1
] = { { ADIS16220_CAPT_BUF1
, 16, 1 },
258 { ADIS16220_AIN1_NULL
, 16, 1 },
259 { ADIS16220_CAPT_PEAK1
, 16, 1 }, },
260 [in_2
] = { { ADIS16220_CAPT_BUF2
, 16, 1 },
261 { ADIS16220_AIN2_NULL
, 16, 1 },
262 { ADIS16220_CAPT_PEAK2
, 16, 1 }, },
263 [accel
] = { { ADIS16220_CAPT_BUFA
, 16, 1 },
264 { ADIS16220_ACCL_NULL
, 16, 1 },
265 { ADIS16220_CAPT_PEAKA
, 16, 1 }, },
266 [temp
] = { { ADIS16220_CAPT_TEMP
, 12, 0 }, }
269 static int adis16220_read_raw(struct iio_dev
*indio_dev
,
270 struct iio_chan_spec
const *chan
,
274 struct adis16220_state
*st
= iio_priv(indio_dev
);
275 const struct adis16220_address_spec
*addr
;
283 case IIO_CHAN_INFO_RAW
:
286 case IIO_CHAN_INFO_OFFSET
:
287 if (chan
->type
== IIO_TEMP
) {
288 *val
= 25000 / -470 - 1278; /* 25 C = 1278 */
293 case IIO_CHAN_INFO_PEAK
:
296 case IIO_CHAN_INFO_SCALE
:
297 switch (chan
->type
) {
299 *val
= -470; /* -0.47 C */
301 return IIO_VAL_INT_PLUS_MICRO
;
303 *val2
= IIO_G_TO_M_S_2(19073); /* 19.073 g */
304 return IIO_VAL_INT_PLUS_MICRO
;
306 if (chan
->channel
== 0) {
308 *val2
= 220700; /* 1.2207 mV */
310 /* Should really be dependent on VDD */
311 *val2
= 305180; /* 305.18 uV */
313 return IIO_VAL_INT_PLUS_MICRO
;
320 addr
= &adis16220_addresses
[chan
->address
][addrind
];
322 ret
= adis_read_reg_16(&st
->adis
, addr
->addr
, &sval
);
326 sval
&= (1 << bits
) - 1;
327 sval
= (s16
)(sval
<< (16 - bits
)) >> (16 - bits
);
331 ret
= adis_read_reg_16(&st
->adis
, addr
->addr
, &uval
);
335 uval
&= (1 << bits
) - 1;
341 static const struct iio_chan_spec adis16220_channels
[] = {
346 .extend_name
= "supply",
347 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
348 IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
349 .address
= in_supply
,
352 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
353 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT
|
354 IIO_CHAN_INFO_SCALE_SEPARATE_BIT
|
355 IIO_CHAN_INFO_PEAK_SEPARATE_BIT
,
361 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
362 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT
|
363 IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
369 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
370 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT
|
371 IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
377 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
,
382 static struct attribute
*adis16220_attributes
[] = {
383 &iio_dev_attr_capture
.dev_attr
.attr
,
384 &iio_dev_attr_capture_count
.dev_attr
.attr
,
388 static const struct attribute_group adis16220_attribute_group
= {
389 .attrs
= adis16220_attributes
,
392 static const struct iio_info adis16220_info
= {
393 .attrs
= &adis16220_attribute_group
,
394 .driver_module
= THIS_MODULE
,
395 .read_raw
= &adis16220_read_raw
,
398 static const char * const adis16220_status_error_msgs
[] = {
399 [ADIS16220_DIAG_STAT_VIOLATION_BIT
] = "Capture period violation/interruption",
400 [ADIS16220_DIAG_STAT_SPI_FAIL_BIT
] = "SPI failure",
401 [ADIS16220_DIAG_STAT_FLASH_UPT_BIT
] = "Flash update failed",
402 [ADIS16220_DIAG_STAT_POWER_HIGH_BIT
] = "Power supply above 3.625V",
403 [ADIS16220_DIAG_STAT_POWER_LOW_BIT
] = "Power supply below 3.15V",
406 static const struct adis_data adis16220_data
= {
409 .msc_ctrl_reg
= ADIS16220_MSC_CTRL
,
410 .glob_cmd_reg
= ADIS16220_GLOB_CMD
,
411 .diag_stat_reg
= ADIS16220_DIAG_STAT
,
413 .self_test_mask
= ADIS16220_MSC_CTRL_SELF_TEST_EN
,
414 .startup_delay
= ADIS16220_STARTUP_DELAY
,
416 .status_error_msgs
= adis16220_status_error_msgs
,
417 .status_error_mask
= BIT(ADIS16220_DIAG_STAT_VIOLATION_BIT
) |
418 BIT(ADIS16220_DIAG_STAT_SPI_FAIL_BIT
) |
419 BIT(ADIS16220_DIAG_STAT_FLASH_UPT_BIT
) |
420 BIT(ADIS16220_DIAG_STAT_POWER_HIGH_BIT
) |
421 BIT(ADIS16220_DIAG_STAT_POWER_LOW_BIT
),
424 static int adis16220_probe(struct spi_device
*spi
)
427 struct adis16220_state
*st
;
428 struct iio_dev
*indio_dev
;
430 /* setup the industrialio driver allocated elements */
431 indio_dev
= iio_device_alloc(sizeof(*st
));
432 if (indio_dev
== NULL
) {
437 st
= iio_priv(indio_dev
);
438 /* this is only used for removal purposes */
439 spi_set_drvdata(spi
, indio_dev
);
441 indio_dev
->name
= spi
->dev
.driver
->name
;
442 indio_dev
->dev
.parent
= &spi
->dev
;
443 indio_dev
->info
= &adis16220_info
;
444 indio_dev
->modes
= INDIO_DIRECT_MODE
;
445 indio_dev
->channels
= adis16220_channels
;
446 indio_dev
->num_channels
= ARRAY_SIZE(adis16220_channels
);
448 ret
= iio_device_register(indio_dev
);
452 ret
= sysfs_create_bin_file(&indio_dev
->dev
.kobj
, &accel_bin
);
454 goto error_unregister_dev
;
456 ret
= sysfs_create_bin_file(&indio_dev
->dev
.kobj
, &adc1_bin
);
458 goto error_rm_accel_bin
;
460 ret
= sysfs_create_bin_file(&indio_dev
->dev
.kobj
, &adc2_bin
);
462 goto error_rm_adc1_bin
;
464 ret
= adis_init(&st
->adis
, indio_dev
, spi
, &adis16220_data
);
466 goto error_rm_adc2_bin
;
467 /* Get the device into a sane initial state */
468 ret
= adis_initial_startup(&st
->adis
);
470 goto error_rm_adc2_bin
;
474 sysfs_remove_bin_file(&indio_dev
->dev
.kobj
, &adc2_bin
);
476 sysfs_remove_bin_file(&indio_dev
->dev
.kobj
, &adc1_bin
);
478 sysfs_remove_bin_file(&indio_dev
->dev
.kobj
, &accel_bin
);
479 error_unregister_dev
:
480 iio_device_unregister(indio_dev
);
482 iio_device_free(indio_dev
);
487 static int adis16220_remove(struct spi_device
*spi
)
489 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
491 sysfs_remove_bin_file(&indio_dev
->dev
.kobj
, &adc2_bin
);
492 sysfs_remove_bin_file(&indio_dev
->dev
.kobj
, &adc1_bin
);
493 sysfs_remove_bin_file(&indio_dev
->dev
.kobj
, &accel_bin
);
494 iio_device_unregister(indio_dev
);
495 iio_device_free(indio_dev
);
500 static struct spi_driver adis16220_driver
= {
503 .owner
= THIS_MODULE
,
505 .probe
= adis16220_probe
,
506 .remove
= adis16220_remove
,
508 module_spi_driver(adis16220_driver
);
510 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
511 MODULE_DESCRIPTION("Analog Devices ADIS16220 Digital Vibration Sensor");
512 MODULE_LICENSE("GPL v2");
513 MODULE_ALIAS("spi:adis16220");