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>
20 #include <linux/module.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
26 #include "adis16240.h"
28 #define DRIVER_NAME "adis16240"
30 static int adis16240_check_status(struct iio_dev *indio_dev);
33 * adis16240_spi_write_reg_8() - write single byte to a register
34 * @indio_dev: iio_dev associated with device
35 * @reg_address: the address of the register to be written
36 * @val: the value to write
38 static int adis16240_spi_write_reg_8(struct iio_dev *indio_dev,
43 struct adis16240_state *st = iio_priv(indio_dev);
45 mutex_lock(&st->buf_lock);
46 st->tx[0] = ADIS16240_WRITE_REG(reg_address);
49 ret = spi_write(st->us, st->tx, 2);
50 mutex_unlock(&st->buf_lock);
56 * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers
57 * @indio_dev: iio_dev for this device
58 * @reg_address: the address of the lower of the two registers. Second register
59 * is assumed to have address one greater.
60 * @val: value to be written
62 static int adis16240_spi_write_reg_16(struct iio_dev *indio_dev,
67 struct spi_message msg;
68 struct adis16240_state *st = iio_priv(indio_dev);
69 struct spi_transfer xfers[] = {
84 mutex_lock(&st->buf_lock);
85 st->tx[0] = ADIS16240_WRITE_REG(lower_reg_address);
86 st->tx[1] = value & 0xFF;
87 st->tx[2] = ADIS16240_WRITE_REG(lower_reg_address + 1);
88 st->tx[3] = (value >> 8) & 0xFF;
90 spi_message_init(&msg);
91 spi_message_add_tail(&xfers[0], &msg);
92 spi_message_add_tail(&xfers[1], &msg);
93 ret = spi_sync(st->us, &msg);
94 mutex_unlock(&st->buf_lock);
100 * adis16240_spi_read_reg_16() - read 2 bytes from a 16-bit register
101 * @indio_dev: iio_dev for this device
102 * @reg_address: the address of the lower of the two registers. Second register
103 * is assumed to have address one greater.
104 * @val: somewhere to pass back the value read
106 static int adis16240_spi_read_reg_16(struct iio_dev *indio_dev,
107 u8 lower_reg_address,
110 struct spi_message msg;
111 struct adis16240_state *st = iio_priv(indio_dev);
113 struct spi_transfer xfers[] = {
129 mutex_lock(&st->buf_lock);
130 st->tx[0] = ADIS16240_READ_REG(lower_reg_address);
135 spi_message_init(&msg);
136 spi_message_add_tail(&xfers[0], &msg);
137 spi_message_add_tail(&xfers[1], &msg);
138 ret = spi_sync(st->us, &msg);
140 dev_err(&st->us->dev,
141 "problem when reading 16 bit register 0x%02X",
145 *val = (st->rx[0] << 8) | st->rx[1];
148 mutex_unlock(&st->buf_lock);
152 static ssize_t adis16240_spi_read_signed(struct device *dev,
153 struct device_attribute *attr,
157 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
160 unsigned shift = 16 - bits;
161 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
163 ret = adis16240_spi_read_reg_16(indio_dev,
164 this_attr->address, (u16 *)&val);
168 if (val & ADIS16240_ERROR_ACTIVE)
169 adis16240_check_status(indio_dev);
171 val = ((s16)(val << shift) >> shift);
172 return sprintf(buf, "%d\n", val);
175 static ssize_t adis16240_read_12bit_signed(struct device *dev,
176 struct device_attribute *attr,
180 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
182 /* Take the iio_dev status lock */
183 mutex_lock(&indio_dev->mlock);
184 ret = adis16240_spi_read_signed(dev, attr, buf, 12);
185 mutex_unlock(&indio_dev->mlock);
190 static int adis16240_reset(struct iio_dev *indio_dev)
193 ret = adis16240_spi_write_reg_8(indio_dev,
195 ADIS16240_GLOB_CMD_SW_RESET);
197 dev_err(&indio_dev->dev, "problem resetting device");
202 int adis16240_set_irq(struct iio_dev *indio_dev, bool enable)
207 ret = adis16240_spi_read_reg_16(indio_dev,
208 ADIS16240_MSC_CTRL, &msc);
212 msc |= ADIS16240_MSC_CTRL_ACTIVE_HIGH;
213 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2;
215 msc |= ADIS16240_MSC_CTRL_DATA_RDY_EN;
217 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_EN;
219 ret = adis16240_spi_write_reg_16(indio_dev,
220 ADIS16240_MSC_CTRL, msc);
226 static int adis16240_self_test(struct iio_dev *indio_dev)
229 ret = adis16240_spi_write_reg_16(indio_dev,
231 ADIS16240_MSC_CTRL_SELF_TEST_EN);
233 dev_err(&indio_dev->dev, "problem starting self test");
237 msleep(ADIS16240_STARTUP_DELAY);
239 adis16240_check_status(indio_dev);
245 static int adis16240_check_status(struct iio_dev *indio_dev)
249 struct device *dev = &indio_dev->dev;
251 ret = adis16240_spi_read_reg_16(indio_dev,
252 ADIS16240_DIAG_STAT, &status);
255 dev_err(dev, "Reading status failed\n");
260 if (status & ADIS16240_DIAG_STAT_PWRON_FAIL)
261 dev_err(dev, "Power-on, self-test fail\n");
262 if (status & ADIS16240_DIAG_STAT_SPI_FAIL)
263 dev_err(dev, "SPI failure\n");
264 if (status & ADIS16240_DIAG_STAT_FLASH_UPT)
265 dev_err(dev, "Flash update failed\n");
266 if (status & ADIS16240_DIAG_STAT_POWER_HIGH)
267 dev_err(dev, "Power supply above 3.625V\n");
268 if (status & ADIS16240_DIAG_STAT_POWER_LOW)
269 dev_err(dev, "Power supply below 2.225V\n");
275 static int adis16240_initial_setup(struct iio_dev *indio_dev)
278 struct device *dev = &indio_dev->dev;
281 ret = adis16240_set_irq(indio_dev, false);
283 dev_err(dev, "disable irq failed");
288 ret = adis16240_self_test(indio_dev);
290 dev_err(dev, "self test failure");
294 /* Read status register to check the result */
295 ret = adis16240_check_status(indio_dev);
297 adis16240_reset(indio_dev);
298 dev_err(dev, "device not playing ball -> reset");
299 msleep(ADIS16240_STARTUP_DELAY);
300 ret = adis16240_check_status(indio_dev);
302 dev_err(dev, "giving up");
311 static IIO_DEVICE_ATTR(in_accel_xyz_squared_peak_raw, S_IRUGO,
312 adis16240_read_12bit_signed, NULL,
313 ADIS16240_XYZPEAK_OUT);
315 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
317 enum adis16240_chan {
326 static const u8 adis16240_addresses[6][3] = {
327 [in_supply] = { ADIS16240_SUPPLY_OUT },
328 [in_aux] = { ADIS16240_AUX_ADC },
329 [accel_x] = { ADIS16240_XACCL_OUT, ADIS16240_XACCL_OFF,
330 ADIS16240_XPEAK_OUT },
331 [accel_y] = { ADIS16240_YACCL_OUT, ADIS16240_YACCL_OFF,
332 ADIS16240_YPEAK_OUT },
333 [accel_z] = { ADIS16240_ZACCL_OUT, ADIS16240_ZACCL_OFF,
334 ADIS16240_ZPEAK_OUT },
335 [temp] = { ADIS16240_TEMP_OUT },
338 static int adis16240_read_raw(struct iio_dev *indio_dev,
339 struct iio_chan_spec const *chan,
349 case IIO_CHAN_INFO_RAW:
350 mutex_lock(&indio_dev->mlock);
351 addr = adis16240_addresses[chan->address][0];
352 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
354 mutex_unlock(&indio_dev->mlock);
358 if (val16 & ADIS16240_ERROR_ACTIVE) {
359 ret = adis16240_check_status(indio_dev);
361 mutex_unlock(&indio_dev->mlock);
365 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
366 if (chan->scan_type.sign == 's')
367 val16 = (s16)(val16 <<
368 (16 - chan->scan_type.realbits)) >>
369 (16 - chan->scan_type.realbits);
371 mutex_unlock(&indio_dev->mlock);
373 case IIO_CHAN_INFO_SCALE:
374 switch (chan->type) {
377 if (chan->channel == 0)
381 return IIO_VAL_INT_PLUS_MICRO;
385 return IIO_VAL_INT_PLUS_MICRO;
389 return IIO_VAL_INT_PLUS_MICRO;
394 case IIO_CHAN_INFO_PEAK_SCALE:
397 return IIO_VAL_INT_PLUS_MICRO;
398 case IIO_CHAN_INFO_OFFSET:
401 case IIO_CHAN_INFO_CALIBBIAS:
403 mutex_lock(&indio_dev->mlock);
404 addr = adis16240_addresses[chan->address][1];
405 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
407 mutex_unlock(&indio_dev->mlock);
410 val16 &= (1 << bits) - 1;
411 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
413 mutex_unlock(&indio_dev->mlock);
415 case IIO_CHAN_INFO_PEAK:
417 mutex_lock(&indio_dev->mlock);
418 addr = adis16240_addresses[chan->address][2];
419 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
421 mutex_unlock(&indio_dev->mlock);
424 val16 &= (1 << bits) - 1;
425 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
427 mutex_unlock(&indio_dev->mlock);
433 static int adis16240_write_raw(struct iio_dev *indio_dev,
434 struct iio_chan_spec const *chan,
443 case IIO_CHAN_INFO_CALIBBIAS:
444 val16 = val & ((1 << bits) - 1);
445 addr = adis16240_addresses[chan->address][1];
446 return adis16240_spi_write_reg_16(indio_dev, addr, val16);
451 static struct iio_chan_spec adis16240_channels[] = {
456 .extend_name = "supply",
457 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
458 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
459 .address = in_supply,
460 .scan_index = ADIS16240_SCAN_SUPPLY,
470 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
472 .scan_index = ADIS16240_SCAN_AUX_ADC,
481 .channel2 = IIO_MOD_X,
482 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
483 IIO_CHAN_INFO_SCALE_SHARED_BIT |
484 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |
485 IIO_CHAN_INFO_PEAK_SEPARATE_BIT,
487 .scan_index = ADIS16240_SCAN_ACC_X,
496 .channel2 = IIO_MOD_Y,
497 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
498 IIO_CHAN_INFO_SCALE_SHARED_BIT |
499 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |
500 IIO_CHAN_INFO_PEAK_SEPARATE_BIT,
502 .scan_index = ADIS16240_SCAN_ACC_Y,
511 .channel2 = IIO_MOD_Z,
512 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
513 IIO_CHAN_INFO_SCALE_SHARED_BIT |
514 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |
515 IIO_CHAN_INFO_PEAK_SEPARATE_BIT,
517 .scan_index = ADIS16240_SCAN_ACC_Z,
527 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
528 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
530 .scan_index = ADIS16240_SCAN_TEMP,
537 IIO_CHAN_SOFT_TIMESTAMP(6)
540 static struct attribute *adis16240_attributes[] = {
541 &iio_dev_attr_in_accel_xyz_squared_peak_raw.dev_attr.attr,
542 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
546 static const struct attribute_group adis16240_attribute_group = {
547 .attrs = adis16240_attributes,
550 static const struct iio_info adis16240_info = {
551 .attrs = &adis16240_attribute_group,
552 .read_raw = &adis16240_read_raw,
553 .write_raw = &adis16240_write_raw,
554 .driver_module = THIS_MODULE,
557 static int __devinit adis16240_probe(struct spi_device *spi)
560 struct adis16240_state *st;
561 struct iio_dev *indio_dev;
563 /* setup the industrialio driver allocated elements */
564 indio_dev = iio_device_alloc(sizeof(*st));
565 if (indio_dev == NULL) {
569 st = iio_priv(indio_dev);
570 /* this is only used for removal purposes */
571 spi_set_drvdata(spi, indio_dev);
574 mutex_init(&st->buf_lock);
576 indio_dev->name = spi->dev.driver->name;
577 indio_dev->dev.parent = &spi->dev;
578 indio_dev->info = &adis16240_info;
579 indio_dev->channels = adis16240_channels;
580 indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
581 indio_dev->modes = INDIO_DIRECT_MODE;
583 ret = adis16240_configure_ring(indio_dev);
587 ret = iio_buffer_register(indio_dev,
589 ARRAY_SIZE(adis16240_channels));
591 printk(KERN_ERR "failed to initialize the ring\n");
592 goto error_unreg_ring_funcs;
596 ret = adis16240_probe_trigger(indio_dev);
598 goto error_uninitialize_ring;
601 /* Get the device into a sane initial state */
602 ret = adis16240_initial_setup(indio_dev);
604 goto error_remove_trigger;
605 ret = iio_device_register(indio_dev);
607 goto error_remove_trigger;
610 error_remove_trigger:
611 adis16240_remove_trigger(indio_dev);
612 error_uninitialize_ring:
613 iio_buffer_unregister(indio_dev);
614 error_unreg_ring_funcs:
615 adis16240_unconfigure_ring(indio_dev);
617 iio_device_free(indio_dev);
622 static int adis16240_remove(struct spi_device *spi)
625 struct iio_dev *indio_dev = spi_get_drvdata(spi);
627 iio_device_unregister(indio_dev);
628 adis16240_remove_trigger(indio_dev);
629 iio_buffer_unregister(indio_dev);
630 adis16240_unconfigure_ring(indio_dev);
631 iio_device_free(indio_dev);
636 static struct spi_driver adis16240_driver = {
639 .owner = THIS_MODULE,
641 .probe = adis16240_probe,
642 .remove = __devexit_p(adis16240_remove),
644 module_spi_driver(adis16240_driver);
646 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
647 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
648 MODULE_LICENSE("GPL v2");
649 MODULE_ALIAS("spi:adis16240");