aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/iio/accel/adis16203_core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/staging/iio/accel/adis16203_core.c')
-rw-r--r--drivers/staging/iio/accel/adis16203_core.c432
1 files changed, 60 insertions, 372 deletions
diff --git a/drivers/staging/iio/accel/adis16203_core.c b/drivers/staging/iio/accel/adis16203_core.c
index e7b3441115a..8c235273ff1 100644
--- a/drivers/staging/iio/accel/adis16203_core.c
+++ b/drivers/staging/iio/accel/adis16203_core.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * ADIS16203 Programmable Digital Vibration Sensor driver 2 * ADIS16203 Programmable Digital Vibration Sensor driver
3 * 3 *
4 * Copyright 2010 Analog Devices Inc. 4 * Copyright 2030 Analog Devices Inc.
5 * 5 *
6 * Licensed under the GPL-2 or later. 6 * Licensed under the GPL-2 or later.
7 */ 7 */
@@ -18,254 +18,14 @@
18#include <linux/iio/iio.h> 18#include <linux/iio/iio.h>
19#include <linux/iio/sysfs.h> 19#include <linux/iio/sysfs.h>
20#include <linux/iio/buffer.h> 20#include <linux/iio/buffer.h>
21#include <linux/iio/imu/adis.h>
21 22
22#include "adis16203.h" 23#include "adis16203.h"
23 24
24#define DRIVER_NAME "adis16203" 25#define DRIVER_NAME "adis16203"
25 26
26/** 27static const u8 adis16203_addresses[] = {
27 * adis16203_spi_write_reg_8() - write single byte to a register 28 [ADIS16203_SCAN_INCLI_X] = ADIS16203_INCL_NULL,
28 * @indio_dev: iio device associated with child of actual device
29 * @reg_address: the address of the register to be written
30 * @val: the value to write
31 **/
32static int adis16203_spi_write_reg_8(struct iio_dev *indio_dev,
33 u8 reg_address,
34 u8 val)
35{
36 int ret;
37 struct adis16203_state *st = iio_priv(indio_dev);
38
39 mutex_lock(&st->buf_lock);
40 st->tx[0] = ADIS16203_WRITE_REG(reg_address);
41 st->tx[1] = val;
42
43 ret = spi_write(st->us, st->tx, 2);
44 mutex_unlock(&st->buf_lock);
45
46 return ret;
47}
48
49/**
50 * adis16203_spi_write_reg_16() - write 2 bytes to a pair of registers
51 * @indio_dev: iio device associated with child of actual device
52 * @reg_address: the address of the lower of the two registers. Second register
53 * is assumed to have address one greater.
54 * @val: value to be written
55 **/
56static int adis16203_spi_write_reg_16(struct iio_dev *indio_dev,
57 u8 lower_reg_address,
58 u16 value)
59{
60 int ret;
61 struct spi_message msg;
62 struct adis16203_state *st = iio_priv(indio_dev);
63 struct spi_transfer xfers[] = {
64 {
65 .tx_buf = st->tx,
66 .bits_per_word = 8,
67 .len = 2,
68 .cs_change = 1,
69 }, {
70 .tx_buf = st->tx + 2,
71 .bits_per_word = 8,
72 .len = 2,
73 },
74 };
75
76 mutex_lock(&st->buf_lock);
77 st->tx[0] = ADIS16203_WRITE_REG(lower_reg_address);
78 st->tx[1] = value & 0xFF;
79 st->tx[2] = ADIS16203_WRITE_REG(lower_reg_address + 1);
80 st->tx[3] = (value >> 8) & 0xFF;
81
82 spi_message_init(&msg);
83 spi_message_add_tail(&xfers[0], &msg);
84 spi_message_add_tail(&xfers[1], &msg);
85 ret = spi_sync(st->us, &msg);
86 mutex_unlock(&st->buf_lock);
87
88 return ret;
89}
90
91/**
92 * adis16203_spi_read_reg_16() - read 2 bytes from a 16-bit register
93 * @indio_dev: iio device associated with child of actual device
94 * @reg_address: the address of the lower of the two registers. Second register
95 * is assumed to have address one greater.
96 * @val: somewhere to pass back the value read
97 **/
98static int adis16203_spi_read_reg_16(struct iio_dev *indio_dev,
99 u8 lower_reg_address,
100 u16 *val)
101{
102 struct spi_message msg;
103 struct adis16203_state *st = iio_priv(indio_dev);
104 int ret;
105 struct spi_transfer xfers[] = {
106 {
107 .tx_buf = st->tx,
108 .bits_per_word = 8,
109 .len = 2,
110 .cs_change = 1,
111 .delay_usecs = 20,
112 }, {
113 .rx_buf = st->rx,
114 .bits_per_word = 8,
115 .len = 2,
116 .delay_usecs = 20,
117 },
118 };
119
120 mutex_lock(&st->buf_lock);
121 st->tx[0] = ADIS16203_READ_REG(lower_reg_address);
122 st->tx[1] = 0;
123
124 spi_message_init(&msg);
125 spi_message_add_tail(&xfers[0], &msg);
126 spi_message_add_tail(&xfers[1], &msg);
127 ret = spi_sync(st->us, &msg);
128 if (ret) {
129 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
130 lower_reg_address);
131 goto error_ret;
132 }
133 *val = (st->rx[0] << 8) | st->rx[1];
134
135error_ret:
136 mutex_unlock(&st->buf_lock);
137 return ret;
138}
139
140static int adis16203_check_status(struct iio_dev *indio_dev)
141{
142 u16 status;
143 int ret;
144
145 ret = adis16203_spi_read_reg_16(indio_dev,
146 ADIS16203_DIAG_STAT,
147 &status);
148 if (ret < 0) {
149 dev_err(&indio_dev->dev, "Reading status failed\n");
150 goto error_ret;
151 }
152 ret = status & 0x1F;
153
154 if (status & ADIS16203_DIAG_STAT_SELFTEST_FAIL)
155 dev_err(&indio_dev->dev, "Self test failure\n");
156 if (status & ADIS16203_DIAG_STAT_SPI_FAIL)
157 dev_err(&indio_dev->dev, "SPI failure\n");
158 if (status & ADIS16203_DIAG_STAT_FLASH_UPT)
159 dev_err(&indio_dev->dev, "Flash update failed\n");
160 if (status & ADIS16203_DIAG_STAT_POWER_HIGH)
161 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
162 if (status & ADIS16203_DIAG_STAT_POWER_LOW)
163 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
164
165error_ret:
166 return ret;
167}
168
169static int adis16203_reset(struct iio_dev *indio_dev)
170{
171 int ret;
172 ret = adis16203_spi_write_reg_8(indio_dev,
173 ADIS16203_GLOB_CMD,
174 ADIS16203_GLOB_CMD_SW_RESET);
175 if (ret)
176 dev_err(&indio_dev->dev, "problem resetting device");
177
178 return ret;
179}
180
181int adis16203_set_irq(struct iio_dev *indio_dev, bool enable)
182{
183 int ret = 0;
184 u16 msc;
185
186 ret = adis16203_spi_read_reg_16(indio_dev, ADIS16203_MSC_CTRL, &msc);
187 if (ret)
188 goto error_ret;
189
190 msc |= ADIS16203_MSC_CTRL_ACTIVE_HIGH;
191 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_DIO1;
192 if (enable)
193 msc |= ADIS16203_MSC_CTRL_DATA_RDY_EN;
194 else
195 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_EN;
196
197 ret = adis16203_spi_write_reg_16(indio_dev, ADIS16203_MSC_CTRL, msc);
198
199error_ret:
200 return ret;
201}
202
203static int adis16203_self_test(struct iio_dev *indio_dev)
204{
205 int ret;
206 ret = adis16203_spi_write_reg_16(indio_dev,
207 ADIS16203_MSC_CTRL,
208 ADIS16203_MSC_CTRL_SELF_TEST_EN);
209 if (ret) {
210 dev_err(&indio_dev->dev, "problem starting self test");
211 goto err_ret;
212 }
213
214 adis16203_check_status(indio_dev);
215
216err_ret:
217 return ret;
218}
219
220static int adis16203_initial_setup(struct iio_dev *indio_dev)
221{
222 int ret;
223
224 /* Disable IRQ */
225 ret = adis16203_set_irq(indio_dev, false);
226 if (ret) {
227 dev_err(&indio_dev->dev, "disable irq failed");
228 goto err_ret;
229 }
230
231 /* Do self test */
232 ret = adis16203_self_test(indio_dev);
233 if (ret) {
234 dev_err(&indio_dev->dev, "self test failure");
235 goto err_ret;
236 }
237
238 /* Read status register to check the result */
239 ret = adis16203_check_status(indio_dev);
240 if (ret) {
241 adis16203_reset(indio_dev);
242 dev_err(&indio_dev->dev, "device not playing ball -> reset");
243 msleep(ADIS16203_STARTUP_DELAY);
244 ret = adis16203_check_status(indio_dev);
245 if (ret) {
246 dev_err(&indio_dev->dev, "giving up");
247 goto err_ret;
248 }
249 }
250
251err_ret:
252 return ret;
253}
254
255enum adis16203_chan {
256 in_supply,
257 in_aux,
258 incli_x,
259 incli_y,
260 temp,
261};
262
263static u8 adis16203_addresses[5][2] = {
264 [in_supply] = { ADIS16203_SUPPLY_OUT },
265 [in_aux] = { ADIS16203_AUX_ADC },
266 [incli_x] = { ADIS16203_XINCL_OUT, ADIS16203_INCL_NULL},
267 [incli_y] = { ADIS16203_YINCL_OUT },
268 [temp] = { ADIS16203_TEMP_OUT }
269}; 29};
270 30
271static int adis16203_write_raw(struct iio_dev *indio_dev, 31static int adis16203_write_raw(struct iio_dev *indio_dev,
@@ -274,9 +34,10 @@ static int adis16203_write_raw(struct iio_dev *indio_dev,
274 int val2, 34 int val2,
275 long mask) 35 long mask)
276{ 36{
37 struct adis *st = iio_priv(indio_dev);
277 /* currently only one writable parameter which keeps this simple */ 38 /* currently only one writable parameter which keeps this simple */
278 u8 addr = adis16203_addresses[chan->address][1]; 39 u8 addr = adis16203_addresses[chan->scan_index];
279 return adis16203_spi_write_reg_16(indio_dev, addr, val & 0x3FFF); 40 return adis_write_reg_16(st, addr, val & 0x3FFF);
280} 41}
281 42
282static int adis16203_read_raw(struct iio_dev *indio_dev, 43static int adis16203_read_raw(struct iio_dev *indio_dev,
@@ -284,35 +45,15 @@ static int adis16203_read_raw(struct iio_dev *indio_dev,
284 int *val, int *val2, 45 int *val, int *val2,
285 long mask) 46 long mask)
286{ 47{
48 struct adis *st = iio_priv(indio_dev);
287 int ret; 49 int ret;
288 int bits; 50 int bits;
289 u8 addr; 51 u8 addr;
290 s16 val16; 52 s16 val16;
291 switch (mask) { 53 switch (mask) {
292 case IIO_CHAN_INFO_RAW: 54 case IIO_CHAN_INFO_RAW:
293 mutex_lock(&indio_dev->mlock); 55 return adis_single_conversion(indio_dev, chan,
294 addr = adis16203_addresses[chan->address][0]; 56 ADIS16203_ERROR_ACTIVE, val);
295 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16);
296 if (ret) {
297 mutex_unlock(&indio_dev->mlock);
298 return ret;
299 }
300
301 if (val16 & ADIS16203_ERROR_ACTIVE) {
302 ret = adis16203_check_status(indio_dev);
303 if (ret) {
304 mutex_unlock(&indio_dev->mlock);
305 return ret;
306 }
307 }
308 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
309 if (chan->scan_type.sign == 's')
310 val16 = (s16)(val16 <<
311 (16 - chan->scan_type.realbits)) >>
312 (16 - chan->scan_type.realbits);
313 *val = val16;
314 mutex_unlock(&indio_dev->mlock);
315 return IIO_VAL_INT;
316 case IIO_CHAN_INFO_SCALE: 57 case IIO_CHAN_INFO_SCALE:
317 switch (chan->type) { 58 switch (chan->type) {
318 case IIO_VOLTAGE: 59 case IIO_VOLTAGE:
@@ -341,8 +82,8 @@ static int adis16203_read_raw(struct iio_dev *indio_dev,
341 case IIO_CHAN_INFO_CALIBBIAS: 82 case IIO_CHAN_INFO_CALIBBIAS:
342 bits = 14; 83 bits = 14;
343 mutex_lock(&indio_dev->mlock); 84 mutex_lock(&indio_dev->mlock);
344 addr = adis16203_addresses[chan->address][1]; 85 addr = adis16203_addresses[chan->scan_index];
345 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16); 86 ret = adis_read_reg_16(st, addr, &val16);
346 if (ret) { 87 if (ret) {
347 mutex_unlock(&indio_dev->mlock); 88 mutex_unlock(&indio_dev->mlock);
348 return ret; 89 return ret;
@@ -358,89 +99,53 @@ static int adis16203_read_raw(struct iio_dev *indio_dev,
358} 99}
359 100
360static const struct iio_chan_spec adis16203_channels[] = { 101static const struct iio_chan_spec adis16203_channels[] = {
361 { 102 ADIS_SUPPLY_CHAN(ADIS16203_SUPPLY_OUT, ADIS16203_SCAN_SUPPLY, 12),
362 .type = IIO_VOLTAGE, 103 ADIS_AUX_ADC_CHAN(ADIS16203_AUX_ADC, ADIS16203_SCAN_AUX_ADC, 12),
363 .indexed = 1, 104 ADIS_INCLI_CHAN(X, ADIS16203_XINCL_OUT, ADIS16203_SCAN_INCLI_X,
364 .channel = 0, 105 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT, 14),
365 .extend_name = "supply", 106 /* Fixme: Not what it appears to be - see data sheet */
366 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | 107 ADIS_INCLI_CHAN(Y, ADIS16203_YINCL_OUT, ADIS16203_SCAN_INCLI_Y, 0, 14),
367 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, 108 ADIS_TEMP_CHAN(ADIS16203_TEMP_OUT, ADIS16203_SCAN_TEMP, 12),
368 .address = in_supply,
369 .scan_index = ADIS16203_SCAN_SUPPLY,
370 .scan_type = {
371 .sign = 'u',
372 .realbits = 12,
373 .storagebits = 16,
374 },
375 }, {
376 .type = IIO_VOLTAGE,
377 .indexed = 1,
378 .channel = 1,
379 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
380 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
381 .address = in_aux,
382 .scan_index = ADIS16203_SCAN_AUX_ADC,
383 .scan_type = {
384 .sign = 'u',
385 .realbits = 12,
386 .storagebits = 16,
387 },
388 }, {
389 .type = IIO_INCLI,
390 .modified = 1,
391 .channel2 = IIO_MOD_X,
392 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
393 IIO_CHAN_INFO_SCALE_SHARED_BIT |
394 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
395 .address = incli_x,
396 .scan_index = ADIS16203_SCAN_INCLI_X,
397 .scan_type = {
398 .sign = 's',
399 .realbits = 14,
400 .storagebits = 16,
401 },
402 }, { /* Fixme: Not what it appears to be - see data sheet */
403 .type = IIO_INCLI,
404 .modified = 1,
405 .channel2 = IIO_MOD_Y,
406 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
407 IIO_CHAN_INFO_SCALE_SHARED_BIT,
408 .address = incli_y,
409 .scan_index = ADIS16203_SCAN_INCLI_Y,
410 .scan_type = {
411 .sign = 's',
412 .realbits = 14,
413 .storagebits = 16,
414 },
415 }, {
416 .type = IIO_TEMP,
417 .indexed = 1,
418 .channel = 0,
419 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
420 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
421 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
422 .address = temp,
423 .scan_index = ADIS16203_SCAN_TEMP,
424 .scan_type = {
425 .sign = 'u',
426 .realbits = 12,
427 .storagebits = 16,
428 },
429 },
430 IIO_CHAN_SOFT_TIMESTAMP(5), 109 IIO_CHAN_SOFT_TIMESTAMP(5),
431}; 110};
432 111
433static const struct iio_info adis16203_info = { 112static const struct iio_info adis16203_info = {
434 .read_raw = &adis16203_read_raw, 113 .read_raw = &adis16203_read_raw,
435 .write_raw = &adis16203_write_raw, 114 .write_raw = &adis16203_write_raw,
115 .update_scan_mode = adis_update_scan_mode,
436 .driver_module = THIS_MODULE, 116 .driver_module = THIS_MODULE,
437}; 117};
438 118
439static int __devinit adis16203_probe(struct spi_device *spi) 119static const char * const adis16203_status_error_msgs[] = {
120 [ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT] = "Self test failure",
121 [ADIS16203_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
122 [ADIS16203_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
123 [ADIS16203_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
124 [ADIS16203_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
125};
126
127static const struct adis_data adis16203_data = {
128 .read_delay = 20,
129 .msc_ctrl_reg = ADIS16203_MSC_CTRL,
130 .glob_cmd_reg = ADIS16203_GLOB_CMD,
131 .diag_stat_reg = ADIS16203_DIAG_STAT,
132
133 .self_test_mask = ADIS16203_MSC_CTRL_SELF_TEST_EN,
134 .startup_delay = ADIS16203_STARTUP_DELAY,
135
136 .status_error_msgs = adis16203_status_error_msgs,
137 .status_error_mask = BIT(ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT) |
138 BIT(ADIS16203_DIAG_STAT_SPI_FAIL_BIT) |
139 BIT(ADIS16203_DIAG_STAT_FLASH_UPT_BIT) |
140 BIT(ADIS16203_DIAG_STAT_POWER_HIGH_BIT) |
141 BIT(ADIS16203_DIAG_STAT_POWER_LOW_BIT),
142};
143
144static int adis16203_probe(struct spi_device *spi)
440{ 145{
441 int ret; 146 int ret;
442 struct iio_dev *indio_dev; 147 struct iio_dev *indio_dev;
443 struct adis16203_state *st; 148 struct adis *st;
444 149
445 /* setup the industrialio driver allocated elements */ 150 /* setup the industrialio driver allocated elements */
446 indio_dev = iio_device_alloc(sizeof(*st)); 151 indio_dev = iio_device_alloc(sizeof(*st));
@@ -451,8 +156,6 @@ static int __devinit adis16203_probe(struct spi_device *spi)
451 st = iio_priv(indio_dev); 156 st = iio_priv(indio_dev);
452 /* this is only used for removal purposes */ 157 /* this is only used for removal purposes */
453 spi_set_drvdata(spi, indio_dev); 158 spi_set_drvdata(spi, indio_dev);
454 st->us = spi;
455 mutex_init(&st->buf_lock);
456 159
457 indio_dev->name = spi->dev.driver->name; 160 indio_dev->name = spi->dev.driver->name;
458 indio_dev->dev.parent = &spi->dev; 161 indio_dev->dev.parent = &spi->dev;
@@ -461,55 +164,40 @@ static int __devinit adis16203_probe(struct spi_device *spi)
461 indio_dev->info = &adis16203_info; 164 indio_dev->info = &adis16203_info;
462 indio_dev->modes = INDIO_DIRECT_MODE; 165 indio_dev->modes = INDIO_DIRECT_MODE;
463 166
464 ret = adis16203_configure_ring(indio_dev); 167 ret = adis_init(st, indio_dev, spi, &adis16203_data);
465 if (ret) 168 if (ret)
466 goto error_free_dev; 169 goto error_free_dev;
467 170
468 ret = iio_buffer_register(indio_dev, 171 ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
469 adis16203_channels, 172 if (ret)
470 ARRAY_SIZE(adis16203_channels)); 173 goto error_free_dev;
471 if (ret) {
472 printk(KERN_ERR "failed to initialize the ring\n");
473 goto error_unreg_ring_funcs;
474 }
475
476 if (spi->irq) {
477 ret = adis16203_probe_trigger(indio_dev);
478 if (ret)
479 goto error_uninitialize_ring;
480 }
481 174
482 /* Get the device into a sane initial state */ 175 /* Get the device into a sane initial state */
483 ret = adis16203_initial_setup(indio_dev); 176 ret = adis_initial_startup(st);
484 if (ret) 177 if (ret)
485 goto error_remove_trigger; 178 goto error_cleanup_buffer_trigger;
486 179
487 ret = iio_device_register(indio_dev); 180 ret = iio_device_register(indio_dev);
488 if (ret) 181 if (ret)
489 goto error_remove_trigger; 182 goto error_cleanup_buffer_trigger;
490 183
491 return 0; 184 return 0;
492 185
493error_remove_trigger: 186error_cleanup_buffer_trigger:
494 adis16203_remove_trigger(indio_dev); 187 adis_cleanup_buffer_and_trigger(st, indio_dev);
495error_uninitialize_ring:
496 iio_buffer_unregister(indio_dev);
497error_unreg_ring_funcs:
498 adis16203_unconfigure_ring(indio_dev);
499error_free_dev: 188error_free_dev:
500 iio_device_free(indio_dev); 189 iio_device_free(indio_dev);
501error_ret: 190error_ret:
502 return ret; 191 return ret;
503} 192}
504 193
505static int __devexit adis16203_remove(struct spi_device *spi) 194static int adis16203_remove(struct spi_device *spi)
506{ 195{
507 struct iio_dev *indio_dev = spi_get_drvdata(spi); 196 struct iio_dev *indio_dev = spi_get_drvdata(spi);
197 struct adis *st = iio_priv(indio_dev);
508 198
509 iio_device_unregister(indio_dev); 199 iio_device_unregister(indio_dev);
510 adis16203_remove_trigger(indio_dev); 200 adis_cleanup_buffer_and_trigger(st, indio_dev);
511 iio_buffer_unregister(indio_dev);
512 adis16203_unconfigure_ring(indio_dev);
513 iio_device_free(indio_dev); 201 iio_device_free(indio_dev);
514 202
515 return 0; 203 return 0;
@@ -521,7 +209,7 @@ static struct spi_driver adis16203_driver = {
521 .owner = THIS_MODULE, 209 .owner = THIS_MODULE,
522 }, 210 },
523 .probe = adis16203_probe, 211 .probe = adis16203_probe,
524 .remove = __devexit_p(adis16203_remove), 212 .remove = adis16203_remove,
525}; 213};
526module_spi_driver(adis16203_driver); 214module_spi_driver(adis16203_driver);
527 215