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, &val);
36 mutex_unlock(&indio_dev->mlock);
39 return sprintf(buf, "%u\n", val);
42 static ssize_t adis16220_write_16bit(struct device *dev,
43 struct device_attribute *attr,
47 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
48 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
49 struct adis16220_state *st = iio_priv(indio_dev);
53 ret = kstrtou16(buf, 10, &val);
56 ret = adis_write_reg_16(&st->adis, this_attr->address, val);
59 return ret ? ret : len;
62 static int adis16220_capture(struct iio_dev *indio_dev)
64 struct adis16220_state *st = iio_priv(indio_dev);
67 /* initiates a manual data capture */
68 ret = adis_write_reg_16(&st->adis, ADIS16220_GLOB_CMD, 0xBF08);
70 dev_err(&indio_dev->dev, "problem beginning capture");
72 usleep_range(10000, 11000); /* delay for capture to finish */
77 static ssize_t adis16220_write_capture(struct device *dev,
78 struct device_attribute *attr,
79 const char *buf, size_t len)
81 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
85 ret = strtobool(buf, &val);
90 ret = adis16220_capture(indio_dev);
97 static ssize_t adis16220_capture_buffer_read(struct iio_dev *indio_dev,
103 struct adis16220_state *st = iio_priv(indio_dev);
104 struct spi_transfer xfers[] = {
122 if (unlikely(!count))
125 if ((off >= ADIS16220_CAPTURE_SIZE) || (count & 1) || (off & 1))
128 if (off + count > ADIS16220_CAPTURE_SIZE)
129 count = ADIS16220_CAPTURE_SIZE - off;
131 /* write the begin position of capture buffer */
132 ret = adis_write_reg_16(&st->adis,
138 /* read count/2 values from capture buffer */
139 mutex_lock(&st->buf_lock);
141 for (i = 0; i < count; i += 2) {
142 st->tx[i] = ADIS_READ_REG(addr);
145 xfers[1].len = count;
147 ret = spi_sync_transfer(st->adis.spi, xfers, ARRAY_SIZE(xfers));
149 mutex_unlock(&st->buf_lock);
153 memcpy(buf, st->rx, count);
155 mutex_unlock(&st->buf_lock);
159 static ssize_t adis16220_accel_bin_read(struct file *filp, struct kobject *kobj,
160 struct bin_attribute *attr,
165 struct iio_dev *indio_dev = dev_to_iio_dev(kobj_to_dev(kobj));
167 return adis16220_capture_buffer_read(indio_dev, buf,
169 ADIS16220_CAPT_BUFA);
172 static struct bin_attribute accel_bin = {
177 .read = adis16220_accel_bin_read,
178 .size = ADIS16220_CAPTURE_SIZE,
181 static ssize_t adis16220_adc1_bin_read(struct file *filp, struct kobject *kobj,
182 struct bin_attribute *attr,
183 char *buf, loff_t off,
186 struct iio_dev *indio_dev = dev_to_iio_dev(kobj_to_dev(kobj));
188 return adis16220_capture_buffer_read(indio_dev, buf,
190 ADIS16220_CAPT_BUF1);
193 static struct bin_attribute adc1_bin = {
198 .read = adis16220_adc1_bin_read,
199 .size = ADIS16220_CAPTURE_SIZE,
202 static ssize_t adis16220_adc2_bin_read(struct file *filp, struct kobject *kobj,
203 struct bin_attribute *attr,
204 char *buf, loff_t off,
207 struct iio_dev *indio_dev = dev_to_iio_dev(kobj_to_dev(kobj));
209 return adis16220_capture_buffer_read(indio_dev, buf,
211 ADIS16220_CAPT_BUF2);
214 static struct bin_attribute adc2_bin = {
219 .read = adis16220_adc2_bin_read,
220 .size = ADIS16220_CAPTURE_SIZE,
223 #define IIO_DEV_ATTR_CAPTURE(_store) \
224 IIO_DEVICE_ATTR(capture, S_IWUSR, NULL, _store, 0)
226 static IIO_DEV_ATTR_CAPTURE(adis16220_write_capture);
228 #define IIO_DEV_ATTR_CAPTURE_COUNT(_mode, _show, _store, _addr) \
229 IIO_DEVICE_ATTR(capture_count, _mode, _show, _store, _addr)
231 static IIO_DEV_ATTR_CAPTURE_COUNT(S_IWUSR | S_IRUGO,
232 adis16220_read_16bit,
233 adis16220_write_16bit,
234 ADIS16220_CAPT_PNTR);
236 enum adis16220_channel {
237 in_supply, in_1, in_2, accel, temp
240 struct adis16220_address_spec {
246 /* Address / bits / signed */
247 static const struct adis16220_address_spec adis16220_addresses[][3] = {
248 [in_supply] = { { ADIS16220_CAPT_SUPPLY, 12, 0 }, },
249 [in_1] = { { ADIS16220_CAPT_BUF1, 16, 1 },
250 { ADIS16220_AIN1_NULL, 16, 1 },
251 { ADIS16220_CAPT_PEAK1, 16, 1 }, },
252 [in_2] = { { ADIS16220_CAPT_BUF2, 16, 1 },
253 { ADIS16220_AIN2_NULL, 16, 1 },
254 { ADIS16220_CAPT_PEAK2, 16, 1 }, },
255 [accel] = { { ADIS16220_CAPT_BUFA, 16, 1 },
256 { ADIS16220_ACCL_NULL, 16, 1 },
257 { ADIS16220_CAPT_PEAKA, 16, 1 }, },
258 [temp] = { { ADIS16220_CAPT_TEMP, 12, 0 }, }
261 static int adis16220_read_raw(struct iio_dev *indio_dev,
262 struct iio_chan_spec const *chan,
266 struct adis16220_state *st = iio_priv(indio_dev);
267 const struct adis16220_address_spec *addr;
275 case IIO_CHAN_INFO_RAW:
278 case IIO_CHAN_INFO_OFFSET:
279 if (chan->type == IIO_TEMP) {
280 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
285 case IIO_CHAN_INFO_PEAK:
288 case IIO_CHAN_INFO_SCALE:
289 switch (chan->type) {
291 *val = -470; /* -0.47 C */
293 return IIO_VAL_INT_PLUS_MICRO;
295 *val2 = IIO_G_TO_M_S_2(19073); /* 19.073 g */
296 return IIO_VAL_INT_PLUS_MICRO;
298 if (chan->channel == 0) {
300 *val2 = 220700; /* 1.2207 mV */
302 /* Should really be dependent on VDD */
303 *val2 = 305180; /* 305.18 uV */
305 return IIO_VAL_INT_PLUS_MICRO;
312 addr = &adis16220_addresses[chan->address][addrind];
314 ret = adis_read_reg_16(&st->adis, addr->addr, &sval);
318 sval &= (1 << bits) - 1;
319 sval = (s16)(sval << (16 - bits)) >> (16 - bits);
323 ret = adis_read_reg_16(&st->adis, addr->addr, &uval);
327 uval &= (1 << bits) - 1;
332 static const struct iio_chan_spec adis16220_channels[] = {
337 .extend_name = "supply",
338 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
339 BIT(IIO_CHAN_INFO_SCALE),
340 .address = in_supply,
343 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
344 BIT(IIO_CHAN_INFO_OFFSET) |
345 BIT(IIO_CHAN_INFO_SCALE) |
346 BIT(IIO_CHAN_INFO_PEAK),
352 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
353 BIT(IIO_CHAN_INFO_OFFSET) |
354 BIT(IIO_CHAN_INFO_SCALE),
360 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
361 BIT(IIO_CHAN_INFO_OFFSET) |
362 BIT(IIO_CHAN_INFO_SCALE),
368 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
373 static struct attribute *adis16220_attributes[] = {
374 &iio_dev_attr_capture.dev_attr.attr,
375 &iio_dev_attr_capture_count.dev_attr.attr,
379 static const struct attribute_group adis16220_attribute_group = {
380 .attrs = adis16220_attributes,
383 static const struct iio_info adis16220_info = {
384 .attrs = &adis16220_attribute_group,
385 .driver_module = THIS_MODULE,
386 .read_raw = &adis16220_read_raw,
389 static const char * const adis16220_status_error_msgs[] = {
390 [ADIS16220_DIAG_STAT_VIOLATION_BIT] = "Capture period violation/interruption",
391 [ADIS16220_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
392 [ADIS16220_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
393 [ADIS16220_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
394 [ADIS16220_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
397 static const struct adis_data adis16220_data = {
400 .msc_ctrl_reg = ADIS16220_MSC_CTRL,
401 .glob_cmd_reg = ADIS16220_GLOB_CMD,
402 .diag_stat_reg = ADIS16220_DIAG_STAT,
404 .self_test_mask = ADIS16220_MSC_CTRL_SELF_TEST_EN,
405 .startup_delay = ADIS16220_STARTUP_DELAY,
407 .status_error_msgs = adis16220_status_error_msgs,
408 .status_error_mask = BIT(ADIS16220_DIAG_STAT_VIOLATION_BIT) |
409 BIT(ADIS16220_DIAG_STAT_SPI_FAIL_BIT) |
410 BIT(ADIS16220_DIAG_STAT_FLASH_UPT_BIT) |
411 BIT(ADIS16220_DIAG_STAT_POWER_HIGH_BIT) |
412 BIT(ADIS16220_DIAG_STAT_POWER_LOW_BIT),
415 static int adis16220_probe(struct spi_device *spi)
418 struct adis16220_state *st;
419 struct iio_dev *indio_dev;
421 /* setup the industrialio driver allocated elements */
422 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
426 st = iio_priv(indio_dev);
427 /* this is only used for removal purposes */
428 spi_set_drvdata(spi, indio_dev);
430 indio_dev->name = spi->dev.driver->name;
431 indio_dev->dev.parent = &spi->dev;
432 indio_dev->info = &adis16220_info;
433 indio_dev->modes = INDIO_DIRECT_MODE;
434 indio_dev->channels = adis16220_channels;
435 indio_dev->num_channels = ARRAY_SIZE(adis16220_channels);
437 ret = devm_iio_device_register(&spi->dev, indio_dev);
441 ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &accel_bin);
445 ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc1_bin);
447 goto error_rm_accel_bin;
449 ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc2_bin);
451 goto error_rm_adc1_bin;
453 ret = adis_init(&st->adis, indio_dev, spi, &adis16220_data);
455 goto error_rm_adc2_bin;
456 /* Get the device into a sane initial state */
457 ret = adis_initial_startup(&st->adis);
459 goto error_rm_adc2_bin;
463 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
465 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
467 sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
471 static int adis16220_remove(struct spi_device *spi)
473 struct iio_dev *indio_dev = spi_get_drvdata(spi);
475 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
476 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
477 sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
482 static struct spi_driver adis16220_driver = {
485 .owner = THIS_MODULE,
487 .probe = adis16220_probe,
488 .remove = adis16220_remove,
490 module_spi_driver(adis16220_driver);
492 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
493 MODULE_DESCRIPTION("Analog Devices ADIS16220 Digital Vibration Sensor");
494 MODULE_LICENSE("GPL v2");
495 MODULE_ALIAS("spi:adis16220");