Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ADIS16240 Programmable Impact Sensor and Recorder driver |
| 3 | * |
| 4 | * Copyright 2010 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 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> |
Mike Frysinger | 1cb6c1f | 2010-05-23 03:10:35 -0400 | [diff] [blame] | 17 | #include <linux/slab.h> |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 18 | #include <linux/sysfs.h> |
| 19 | #include <linux/list.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 20 | #include <linux/module.h> |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 21 | |
| 22 | #include "../iio.h" |
| 23 | #include "../sysfs.h" |
Jonathan Cameron | af5046a | 2011-10-26 17:41:32 +0100 | [diff] [blame^] | 24 | #include "../buffer.h" |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 25 | |
| 26 | #include "adis16240.h" |
| 27 | |
| 28 | #define DRIVER_NAME "adis16240" |
| 29 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 30 | static int adis16240_check_status(struct iio_dev *indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * adis16240_spi_write_reg_8() - write single byte to a register |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 34 | * @indio_dev: iio_dev associated with device |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 35 | * @reg_address: the address of the register to be written |
| 36 | * @val: the value to write |
| 37 | **/ |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 38 | static int adis16240_spi_write_reg_8(struct iio_dev *indio_dev, |
| 39 | u8 reg_address, |
| 40 | u8 val) |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 41 | { |
| 42 | int ret; |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 43 | struct adis16240_state *st = iio_priv(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 44 | |
| 45 | mutex_lock(&st->buf_lock); |
| 46 | st->tx[0] = ADIS16240_WRITE_REG(reg_address); |
| 47 | st->tx[1] = val; |
| 48 | |
| 49 | ret = spi_write(st->us, st->tx, 2); |
| 50 | mutex_unlock(&st->buf_lock); |
| 51 | |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 57 | * @indio_dev: iio_dev for this device |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 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 |
| 61 | **/ |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 62 | static int adis16240_spi_write_reg_16(struct iio_dev *indio_dev, |
| 63 | u8 lower_reg_address, |
| 64 | u16 value) |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 65 | { |
| 66 | int ret; |
| 67 | struct spi_message msg; |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 68 | struct adis16240_state *st = iio_priv(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 69 | struct spi_transfer xfers[] = { |
| 70 | { |
| 71 | .tx_buf = st->tx, |
| 72 | .bits_per_word = 8, |
| 73 | .len = 2, |
| 74 | .cs_change = 1, |
Barry Song | 00ae794 | 2010-06-04 17:19:54 +0800 | [diff] [blame] | 75 | .delay_usecs = 35, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 76 | }, { |
| 77 | .tx_buf = st->tx + 2, |
| 78 | .bits_per_word = 8, |
| 79 | .len = 2, |
Barry Song | 00ae794 | 2010-06-04 17:19:54 +0800 | [diff] [blame] | 80 | .delay_usecs = 35, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 81 | }, |
| 82 | }; |
| 83 | |
| 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; |
| 89 | |
| 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); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * adis16240_spi_read_reg_16() - read 2 bytes from a 16-bit register |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 101 | * @indio_dev: iio_dev for this device |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 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 |
| 105 | **/ |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 106 | static int adis16240_spi_read_reg_16(struct iio_dev *indio_dev, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 107 | u8 lower_reg_address, |
| 108 | u16 *val) |
| 109 | { |
| 110 | struct spi_message msg; |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 111 | struct adis16240_state *st = iio_priv(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 112 | int ret; |
| 113 | struct spi_transfer xfers[] = { |
| 114 | { |
| 115 | .tx_buf = st->tx, |
| 116 | .bits_per_word = 8, |
| 117 | .len = 2, |
| 118 | .cs_change = 1, |
Barry Song | 00ae794 | 2010-06-04 17:19:54 +0800 | [diff] [blame] | 119 | .delay_usecs = 35, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 120 | }, { |
| 121 | .rx_buf = st->rx, |
| 122 | .bits_per_word = 8, |
| 123 | .len = 2, |
| 124 | .cs_change = 1, |
Barry Song | 00ae794 | 2010-06-04 17:19:54 +0800 | [diff] [blame] | 125 | .delay_usecs = 35, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 126 | }, |
| 127 | }; |
| 128 | |
| 129 | mutex_lock(&st->buf_lock); |
| 130 | st->tx[0] = ADIS16240_READ_REG(lower_reg_address); |
| 131 | st->tx[1] = 0; |
| 132 | st->tx[2] = 0; |
| 133 | st->tx[3] = 0; |
| 134 | |
| 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); |
| 139 | if (ret) { |
| 140 | dev_err(&st->us->dev, |
| 141 | "problem when reading 16 bit register 0x%02X", |
| 142 | lower_reg_address); |
| 143 | goto error_ret; |
| 144 | } |
| 145 | *val = (st->rx[0] << 8) | st->rx[1]; |
| 146 | |
| 147 | error_ret: |
| 148 | mutex_unlock(&st->buf_lock); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | static ssize_t adis16240_spi_read_signed(struct device *dev, |
| 153 | struct device_attribute *attr, |
| 154 | char *buf, |
| 155 | unsigned bits) |
| 156 | { |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 157 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 158 | int ret; |
| 159 | s16 val = 0; |
| 160 | unsigned shift = 16 - bits; |
| 161 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 162 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 163 | ret = adis16240_spi_read_reg_16(indio_dev, |
| 164 | this_attr->address, (u16 *)&val); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 165 | if (ret) |
| 166 | return ret; |
| 167 | |
| 168 | if (val & ADIS16240_ERROR_ACTIVE) |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 169 | adis16240_check_status(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 170 | |
| 171 | val = ((s16)(val << shift) >> shift); |
| 172 | return sprintf(buf, "%d\n", val); |
| 173 | } |
| 174 | |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 175 | static ssize_t adis16240_read_12bit_signed(struct device *dev, |
| 176 | struct device_attribute *attr, |
| 177 | char *buf) |
| 178 | { |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 179 | ssize_t ret; |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 180 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 181 | |
| 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); |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 190 | static int adis16240_reset(struct iio_dev *indio_dev) |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 191 | { |
| 192 | int ret; |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 193 | ret = adis16240_spi_write_reg_8(indio_dev, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 194 | ADIS16240_GLOB_CMD, |
| 195 | ADIS16240_GLOB_CMD_SW_RESET); |
| 196 | if (ret) |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 197 | dev_err(&indio_dev->dev, "problem resetting device"); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static ssize_t adis16240_write_reset(struct device *dev, |
| 203 | struct device_attribute *attr, |
| 204 | const char *buf, size_t len) |
| 205 | { |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 206 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 207 | |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 208 | if (len < 1) |
| 209 | return -EINVAL; |
| 210 | switch (buf[0]) { |
| 211 | case '1': |
| 212 | case 'y': |
| 213 | case 'Y': |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 214 | return adis16240_reset(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 215 | } |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 219 | int adis16240_set_irq(struct iio_dev *indio_dev, bool enable) |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 220 | { |
| 221 | int ret = 0; |
| 222 | u16 msc; |
| 223 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 224 | ret = adis16240_spi_read_reg_16(indio_dev, |
| 225 | ADIS16240_MSC_CTRL, &msc); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 226 | if (ret) |
| 227 | goto error_ret; |
| 228 | |
| 229 | msc |= ADIS16240_MSC_CTRL_ACTIVE_HIGH; |
| 230 | msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2; |
| 231 | if (enable) |
| 232 | msc |= ADIS16240_MSC_CTRL_DATA_RDY_EN; |
| 233 | else |
| 234 | msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_EN; |
| 235 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 236 | ret = adis16240_spi_write_reg_16(indio_dev, |
| 237 | ADIS16240_MSC_CTRL, msc); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 238 | |
| 239 | error_ret: |
| 240 | return ret; |
| 241 | } |
| 242 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 243 | static int adis16240_self_test(struct iio_dev *indio_dev) |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 244 | { |
| 245 | int ret; |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 246 | ret = adis16240_spi_write_reg_16(indio_dev, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 247 | ADIS16240_MSC_CTRL, |
| 248 | ADIS16240_MSC_CTRL_SELF_TEST_EN); |
| 249 | if (ret) { |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 250 | dev_err(&indio_dev->dev, "problem starting self test"); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 251 | goto err_ret; |
| 252 | } |
| 253 | |
| 254 | msleep(ADIS16240_STARTUP_DELAY); |
| 255 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 256 | adis16240_check_status(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 257 | |
| 258 | err_ret: |
| 259 | return ret; |
| 260 | } |
| 261 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 262 | static int adis16240_check_status(struct iio_dev *indio_dev) |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 263 | { |
| 264 | u16 status; |
| 265 | int ret; |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 266 | struct device *dev = &indio_dev->dev; |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 267 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 268 | ret = adis16240_spi_read_reg_16(indio_dev, |
| 269 | ADIS16240_DIAG_STAT, &status); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 270 | |
| 271 | if (ret < 0) { |
| 272 | dev_err(dev, "Reading status failed\n"); |
| 273 | goto error_ret; |
| 274 | } |
| 275 | |
| 276 | ret = status & 0x2F; |
| 277 | if (status & ADIS16240_DIAG_STAT_PWRON_FAIL) |
| 278 | dev_err(dev, "Power-on, self-test fail\n"); |
| 279 | if (status & ADIS16240_DIAG_STAT_SPI_FAIL) |
| 280 | dev_err(dev, "SPI failure\n"); |
| 281 | if (status & ADIS16240_DIAG_STAT_FLASH_UPT) |
| 282 | dev_err(dev, "Flash update failed\n"); |
| 283 | if (status & ADIS16240_DIAG_STAT_POWER_HIGH) |
| 284 | dev_err(dev, "Power supply above 3.625V\n"); |
| 285 | if (status & ADIS16240_DIAG_STAT_POWER_LOW) |
| 286 | dev_err(dev, "Power supply below 2.225V\n"); |
| 287 | |
| 288 | error_ret: |
| 289 | return ret; |
| 290 | } |
| 291 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 292 | static int adis16240_initial_setup(struct iio_dev *indio_dev) |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 293 | { |
| 294 | int ret; |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 295 | struct device *dev = &indio_dev->dev; |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 296 | |
| 297 | /* Disable IRQ */ |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 298 | ret = adis16240_set_irq(indio_dev, false); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 299 | if (ret) { |
| 300 | dev_err(dev, "disable irq failed"); |
| 301 | goto err_ret; |
| 302 | } |
| 303 | |
| 304 | /* Do self test */ |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 305 | ret = adis16240_self_test(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 306 | if (ret) { |
| 307 | dev_err(dev, "self test failure"); |
| 308 | goto err_ret; |
| 309 | } |
| 310 | |
| 311 | /* Read status register to check the result */ |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 312 | ret = adis16240_check_status(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 313 | if (ret) { |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 314 | adis16240_reset(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 315 | dev_err(dev, "device not playing ball -> reset"); |
| 316 | msleep(ADIS16240_STARTUP_DELAY); |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 317 | ret = adis16240_check_status(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 318 | if (ret) { |
| 319 | dev_err(dev, "giving up"); |
| 320 | goto err_ret; |
| 321 | } |
| 322 | } |
| 323 | |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 324 | err_ret: |
| 325 | return ret; |
| 326 | } |
| 327 | |
Jonathan Cameron | 322c956 | 2011-09-14 13:01:23 +0100 | [diff] [blame] | 328 | static IIO_DEVICE_ATTR(in_accel_xyz_squared_peak_raw, S_IRUGO, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 329 | adis16240_read_12bit_signed, NULL, |
| 330 | ADIS16240_XYZPEAK_OUT); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 331 | |
| 332 | static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16240_write_reset, 0); |
| 333 | |
Manuel Stahl | 51a0a5b | 2010-08-31 11:32:54 +0200 | [diff] [blame] | 334 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096"); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 335 | |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 336 | enum adis16240_chan { |
| 337 | in_supply, |
| 338 | in_aux, |
| 339 | accel_x, |
| 340 | accel_y, |
| 341 | accel_z, |
| 342 | temp, |
| 343 | }; |
| 344 | |
| 345 | static const u8 adis16240_addresses[6][3] = { |
| 346 | [in_supply] = { ADIS16240_SUPPLY_OUT }, |
| 347 | [in_aux] = { ADIS16240_AUX_ADC }, |
| 348 | [accel_x] = { ADIS16240_XACCL_OUT, ADIS16240_XACCL_OFF, |
| 349 | ADIS16240_XPEAK_OUT }, |
| 350 | [accel_y] = { ADIS16240_YACCL_OUT, ADIS16240_YACCL_OFF, |
| 351 | ADIS16240_YPEAK_OUT }, |
| 352 | [accel_z] = { ADIS16240_ZACCL_OUT, ADIS16240_ZACCL_OFF, |
| 353 | ADIS16240_ZPEAK_OUT }, |
| 354 | [temp] = { ADIS16240_TEMP_OUT }, |
| 355 | }; |
| 356 | |
| 357 | static int adis16240_read_raw(struct iio_dev *indio_dev, |
| 358 | struct iio_chan_spec const *chan, |
| 359 | int *val, int *val2, |
| 360 | long mask) |
| 361 | { |
| 362 | int ret; |
| 363 | int bits; |
| 364 | u8 addr; |
| 365 | s16 val16; |
| 366 | |
| 367 | switch (mask) { |
| 368 | case 0: |
| 369 | mutex_lock(&indio_dev->mlock); |
| 370 | addr = adis16240_addresses[chan->address][0]; |
| 371 | ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16); |
Dan Carpenter | 8f89615 | 2011-07-30 11:45:09 +0300 | [diff] [blame] | 372 | if (ret) { |
| 373 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 374 | return ret; |
Dan Carpenter | 8f89615 | 2011-07-30 11:45:09 +0300 | [diff] [blame] | 375 | } |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 376 | |
| 377 | if (val16 & ADIS16240_ERROR_ACTIVE) { |
| 378 | ret = adis16240_check_status(indio_dev); |
Dan Carpenter | 8f89615 | 2011-07-30 11:45:09 +0300 | [diff] [blame] | 379 | if (ret) { |
| 380 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 381 | return ret; |
Dan Carpenter | 8f89615 | 2011-07-30 11:45:09 +0300 | [diff] [blame] | 382 | } |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 383 | } |
| 384 | val16 = val16 & ((1 << chan->scan_type.realbits) - 1); |
| 385 | if (chan->scan_type.sign == 's') |
| 386 | val16 = (s16)(val16 << |
| 387 | (16 - chan->scan_type.realbits)) >> |
| 388 | (16 - chan->scan_type.realbits); |
| 389 | *val = val16; |
| 390 | mutex_unlock(&indio_dev->mlock); |
| 391 | return IIO_VAL_INT; |
| 392 | case (1 << IIO_CHAN_INFO_SCALE_SEPARATE): |
| 393 | case (1 << IIO_CHAN_INFO_SCALE_SHARED): |
| 394 | switch (chan->type) { |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 395 | case IIO_VOLTAGE: |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 396 | *val = 0; |
| 397 | if (chan->channel == 0) |
| 398 | *val2 = 4880; |
| 399 | else |
| 400 | return -EINVAL; |
| 401 | return IIO_VAL_INT_PLUS_MICRO; |
| 402 | case IIO_TEMP: |
| 403 | *val = 0; |
| 404 | *val2 = 244000; |
| 405 | return IIO_VAL_INT_PLUS_MICRO; |
| 406 | case IIO_ACCEL: |
| 407 | *val = 0; |
| 408 | *val2 = 504062; |
| 409 | return IIO_VAL_INT_PLUS_MICRO; |
| 410 | default: |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | break; |
| 414 | case (1 << IIO_CHAN_INFO_PEAK_SCALE_SHARED): |
| 415 | *val = 6; |
| 416 | *val2 = 629295; |
| 417 | return IIO_VAL_INT_PLUS_MICRO; |
| 418 | case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE): |
| 419 | *val = 25; |
| 420 | return IIO_VAL_INT; |
| 421 | case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE): |
| 422 | bits = 10; |
| 423 | mutex_lock(&indio_dev->mlock); |
| 424 | addr = adis16240_addresses[chan->address][1]; |
| 425 | ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16); |
| 426 | if (ret) { |
| 427 | mutex_unlock(&indio_dev->mlock); |
| 428 | return ret; |
| 429 | } |
| 430 | val16 &= (1 << bits) - 1; |
| 431 | val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); |
| 432 | *val = val16; |
| 433 | mutex_unlock(&indio_dev->mlock); |
| 434 | return IIO_VAL_INT; |
| 435 | case (1 << IIO_CHAN_INFO_PEAK_SEPARATE): |
| 436 | bits = 10; |
| 437 | mutex_lock(&indio_dev->mlock); |
| 438 | addr = adis16240_addresses[chan->address][2]; |
| 439 | ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16); |
| 440 | if (ret) { |
| 441 | mutex_unlock(&indio_dev->mlock); |
| 442 | return ret; |
| 443 | } |
| 444 | val16 &= (1 << bits) - 1; |
| 445 | val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); |
| 446 | *val = val16; |
| 447 | mutex_unlock(&indio_dev->mlock); |
| 448 | return IIO_VAL_INT; |
| 449 | } |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | |
| 453 | static int adis16240_write_raw(struct iio_dev *indio_dev, |
| 454 | struct iio_chan_spec const *chan, |
| 455 | int val, |
| 456 | int val2, |
| 457 | long mask) |
| 458 | { |
| 459 | int bits = 10; |
| 460 | s16 val16; |
| 461 | u8 addr; |
| 462 | switch (mask) { |
| 463 | case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE): |
| 464 | val16 = val & ((1 << bits) - 1); |
| 465 | addr = adis16240_addresses[chan->address][1]; |
| 466 | return adis16240_spi_write_reg_16(indio_dev, addr, val16); |
| 467 | } |
| 468 | return -EINVAL; |
| 469 | } |
| 470 | |
| 471 | static struct iio_chan_spec adis16240_channels[] = { |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 472 | IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, "supply", 0, 0, |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 473 | (1 << IIO_CHAN_INFO_SCALE_SEPARATE), |
| 474 | in_supply, ADIS16240_SCAN_SUPPLY, |
| 475 | IIO_ST('u', 10, 16, 0), 0), |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 476 | IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 1, 0, |
Jonathan Cameron | cd69f57 | 2011-05-18 14:41:58 +0100 | [diff] [blame] | 477 | 0, |
| 478 | in_aux, ADIS16240_SCAN_AUX_ADC, |
| 479 | IIO_ST('u', 10, 16, 0), 0), |
| 480 | IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X, |
| 481 | (1 << IIO_CHAN_INFO_SCALE_SHARED) | |
| 482 | (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE), |
| 483 | accel_x, ADIS16240_SCAN_ACC_X, |
| 484 | IIO_ST('s', 10, 16, 0), 0), |
| 485 | IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y, |
| 486 | (1 << IIO_CHAN_INFO_SCALE_SHARED) | |
| 487 | (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE), |
| 488 | accel_y, ADIS16240_SCAN_ACC_Y, |
| 489 | IIO_ST('s', 10, 16, 0), 0), |
| 490 | IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Z, |
| 491 | (1 << IIO_CHAN_INFO_SCALE_SHARED) | |
| 492 | (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE), |
| 493 | accel_z, ADIS16240_SCAN_ACC_Z, |
| 494 | IIO_ST('s', 10, 16, 0), 0), |
| 495 | IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0, |
| 496 | (1 << IIO_CHAN_INFO_SCALE_SEPARATE), |
| 497 | temp, ADIS16240_SCAN_TEMP, |
| 498 | IIO_ST('u', 10, 16, 0), 0), |
| 499 | IIO_CHAN_SOFT_TIMESTAMP(6) |
| 500 | }; |
| 501 | |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 502 | static struct attribute *adis16240_attributes[] = { |
Jonathan Cameron | 322c956 | 2011-09-14 13:01:23 +0100 | [diff] [blame] | 503 | &iio_dev_attr_in_accel_xyz_squared_peak_raw.dev_attr.attr, |
Manuel Stahl | 51a0a5b | 2010-08-31 11:32:54 +0200 | [diff] [blame] | 504 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 505 | &iio_dev_attr_reset.dev_attr.attr, |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 506 | NULL |
| 507 | }; |
| 508 | |
| 509 | static const struct attribute_group adis16240_attribute_group = { |
| 510 | .attrs = adis16240_attributes, |
| 511 | }; |
| 512 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 513 | static const struct iio_info adis16240_info = { |
| 514 | .attrs = &adis16240_attribute_group, |
| 515 | .read_raw = &adis16240_read_raw, |
| 516 | .write_raw = &adis16240_write_raw, |
| 517 | .driver_module = THIS_MODULE, |
| 518 | }; |
| 519 | |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 520 | static int __devinit adis16240_probe(struct spi_device *spi) |
| 521 | { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 522 | int ret; |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 523 | struct adis16240_state *st; |
| 524 | struct iio_dev *indio_dev; |
| 525 | |
| 526 | /* setup the industrialio driver allocated elements */ |
| 527 | indio_dev = iio_allocate_device(sizeof(*st)); |
| 528 | if (indio_dev == NULL) { |
| 529 | ret = -ENOMEM; |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 530 | goto error_ret; |
| 531 | } |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 532 | st = iio_priv(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 533 | /* this is only used for removal purposes */ |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 534 | spi_set_drvdata(spi, indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 535 | |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 536 | st->us = spi; |
| 537 | mutex_init(&st->buf_lock); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 538 | |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 539 | indio_dev->name = spi->dev.driver->name; |
| 540 | indio_dev->dev.parent = &spi->dev; |
| 541 | indio_dev->info = &adis16240_info; |
| 542 | indio_dev->channels = adis16240_channels; |
| 543 | indio_dev->num_channels = ARRAY_SIZE(adis16240_channels); |
| 544 | indio_dev->modes = INDIO_DIRECT_MODE; |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 545 | |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 546 | ret = adis16240_configure_ring(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 547 | if (ret) |
| 548 | goto error_free_dev; |
| 549 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 550 | ret = iio_buffer_register(indio_dev, |
| 551 | adis16240_channels, |
| 552 | ARRAY_SIZE(adis16240_channels)); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 553 | if (ret) { |
| 554 | printk(KERN_ERR "failed to initialize the ring\n"); |
| 555 | goto error_unreg_ring_funcs; |
| 556 | } |
| 557 | |
| 558 | if (spi->irq) { |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 559 | ret = adis16240_probe_trigger(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 560 | if (ret) |
Jonathan Cameron | bdd560c | 2011-04-18 12:59:04 +0100 | [diff] [blame] | 561 | goto error_uninitialize_ring; |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* Get the device into a sane initial state */ |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 565 | ret = adis16240_initial_setup(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 566 | if (ret) |
| 567 | goto error_remove_trigger; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 568 | ret = iio_device_register(indio_dev); |
| 569 | if (ret) |
| 570 | goto error_remove_trigger; |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 571 | return 0; |
| 572 | |
| 573 | error_remove_trigger: |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 574 | adis16240_remove_trigger(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 575 | error_uninitialize_ring: |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 576 | iio_buffer_unregister(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 577 | error_unreg_ring_funcs: |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 578 | adis16240_unconfigure_ring(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 579 | error_free_dev: |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 580 | iio_free_device(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 581 | error_ret: |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | static int adis16240_remove(struct spi_device *spi) |
| 586 | { |
Jonathan Cameron | a22ff70 | 2011-06-27 13:07:14 +0100 | [diff] [blame] | 587 | |
| 588 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 589 | |
| 590 | flush_scheduled_work(); |
| 591 | |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 592 | iio_device_unregister(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 593 | adis16240_remove_trigger(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 594 | iio_buffer_unregister(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 595 | adis16240_unconfigure_ring(indio_dev); |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 596 | iio_free_device(indio_dev); |
Barry Song | 2c834b4 | 2010-05-07 15:39:00 +0100 | [diff] [blame] | 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | static struct spi_driver adis16240_driver = { |
| 602 | .driver = { |
| 603 | .name = "adis16240", |
| 604 | .owner = THIS_MODULE, |
| 605 | }, |
| 606 | .probe = adis16240_probe, |
| 607 | .remove = __devexit_p(adis16240_remove), |
| 608 | }; |
| 609 | |
| 610 | static __init int adis16240_init(void) |
| 611 | { |
| 612 | return spi_register_driver(&adis16240_driver); |
| 613 | } |
| 614 | module_init(adis16240_init); |
| 615 | |
| 616 | static __exit void adis16240_exit(void) |
| 617 | { |
| 618 | spi_unregister_driver(&adis16240_driver); |
| 619 | } |
| 620 | module_exit(adis16240_exit); |
| 621 | |
| 622 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 623 | MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder"); |
| 624 | MODULE_LICENSE("GPL v2"); |