Staging: iio: release locks on error paths
[linux-2.6.git] / drivers / staging / iio / accel / adis16201_core.c
blob2fd01aecdf96ea5a30d75fde61f8a8add31617ae
1 /*
2 * ADIS16201 Programmable Digital Vibration Sensor driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
17 #include "../iio.h"
18 #include "../sysfs.h"
19 #include "../ring_generic.h"
21 #include "accel.h"
22 #include "inclinometer.h"
23 #include "../adc/adc.h"
25 #include "adis16201.h"
27 enum adis16201_chan {
28 in_supply,
29 temp,
30 accel_x,
31 accel_y,
32 incli_x,
33 incli_y,
34 in_aux,
37 /**
38 * adis16201_spi_write_reg_8() - write single byte to a register
39 * @dev: device associated with child of actual device (iio_dev or iio_trig)
40 * @reg_address: the address of the register to be written
41 * @val: the value to write
42 **/
43 static int adis16201_spi_write_reg_8(struct iio_dev *indio_dev,
44 u8 reg_address,
45 u8 val)
47 int ret;
48 struct adis16201_state *st = iio_priv(indio_dev);
50 mutex_lock(&st->buf_lock);
51 st->tx[0] = ADIS16201_WRITE_REG(reg_address);
52 st->tx[1] = val;
54 ret = spi_write(st->us, st->tx, 2);
55 mutex_unlock(&st->buf_lock);
57 return ret;
60 /**
61 * adis16201_spi_write_reg_16() - write 2 bytes to a pair of registers
62 * @indio_dev: iio device associated with child of actual device
63 * @reg_address: the address of the lower of the two registers. Second register
64 * is assumed to have address one greater.
65 * @val: value to be written
66 **/
67 static int adis16201_spi_write_reg_16(struct iio_dev *indio_dev,
68 u8 lower_reg_address,
69 u16 value)
71 int ret;
72 struct spi_message msg;
73 struct adis16201_state *st = iio_priv(indio_dev);
74 struct spi_transfer xfers[] = {
76 .tx_buf = st->tx,
77 .bits_per_word = 8,
78 .len = 2,
79 .cs_change = 1,
80 }, {
81 .tx_buf = st->tx + 2,
82 .bits_per_word = 8,
83 .len = 2,
87 mutex_lock(&st->buf_lock);
88 st->tx[0] = ADIS16201_WRITE_REG(lower_reg_address);
89 st->tx[1] = value & 0xFF;
90 st->tx[2] = ADIS16201_WRITE_REG(lower_reg_address + 1);
91 st->tx[3] = (value >> 8) & 0xFF;
93 spi_message_init(&msg);
94 spi_message_add_tail(&xfers[0], &msg);
95 spi_message_add_tail(&xfers[1], &msg);
96 ret = spi_sync(st->us, &msg);
97 mutex_unlock(&st->buf_lock);
99 return ret;
103 * adis16201_spi_read_reg_16() - read 2 bytes from a 16-bit register
104 * @indio_dev: iio device associated with child of actual device
105 * @reg_address: the address of the lower of the two registers. Second register
106 * is assumed to have address one greater.
107 * @val: somewhere to pass back the value read
109 static int adis16201_spi_read_reg_16(struct iio_dev *indio_dev,
110 u8 lower_reg_address,
111 u16 *val)
113 struct spi_message msg;
114 struct adis16201_state *st = iio_priv(indio_dev);
115 int ret;
116 struct spi_transfer xfers[] = {
118 .tx_buf = st->tx,
119 .bits_per_word = 8,
120 .len = 2,
121 .cs_change = 1,
122 .delay_usecs = 20,
123 }, {
124 .rx_buf = st->rx,
125 .bits_per_word = 8,
126 .len = 2,
127 .delay_usecs = 20,
131 mutex_lock(&st->buf_lock);
132 st->tx[0] = ADIS16201_READ_REG(lower_reg_address);
133 st->tx[1] = 0;
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, "problem when reading 16 bit register 0x%02X",
141 lower_reg_address);
142 goto error_ret;
144 *val = (st->rx[0] << 8) | st->rx[1];
146 error_ret:
147 mutex_unlock(&st->buf_lock);
148 return ret;
151 static int adis16201_reset(struct iio_dev *indio_dev)
153 int ret;
154 struct adis16201_state *st = iio_priv(indio_dev);
156 ret = adis16201_spi_write_reg_8(indio_dev,
157 ADIS16201_GLOB_CMD,
158 ADIS16201_GLOB_CMD_SW_RESET);
159 if (ret)
160 dev_err(&st->us->dev, "problem resetting device");
162 return ret;
165 static ssize_t adis16201_write_reset(struct device *dev,
166 struct device_attribute *attr,
167 const char *buf, size_t len)
169 int ret;
170 bool res;
172 if (len < 1)
173 return -EINVAL;
174 ret = strtobool(buf, &res);
175 if (ret || !res)
176 return ret;
177 return adis16201_reset(dev_get_drvdata(dev));
180 int adis16201_set_irq(struct iio_dev *indio_dev, bool enable)
182 int ret = 0;
183 u16 msc;
185 ret = adis16201_spi_read_reg_16(indio_dev, ADIS16201_MSC_CTRL, &msc);
186 if (ret)
187 goto error_ret;
189 msc |= ADIS16201_MSC_CTRL_ACTIVE_HIGH;
190 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_DIO1;
191 if (enable)
192 msc |= ADIS16201_MSC_CTRL_DATA_RDY_EN;
193 else
194 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_EN;
196 ret = adis16201_spi_write_reg_16(indio_dev, ADIS16201_MSC_CTRL, msc);
198 error_ret:
199 return ret;
202 static int adis16201_check_status(struct iio_dev *indio_dev)
204 u16 status;
205 int ret;
207 ret = adis16201_spi_read_reg_16(indio_dev,
208 ADIS16201_DIAG_STAT, &status);
209 if (ret < 0) {
210 dev_err(&indio_dev->dev, "Reading status failed\n");
211 goto error_ret;
213 ret = status & 0xF;
214 if (ret)
215 ret = -EFAULT;
217 if (status & ADIS16201_DIAG_STAT_SPI_FAIL)
218 dev_err(&indio_dev->dev, "SPI failure\n");
219 if (status & ADIS16201_DIAG_STAT_FLASH_UPT)
220 dev_err(&indio_dev->dev, "Flash update failed\n");
221 if (status & ADIS16201_DIAG_STAT_POWER_HIGH)
222 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
223 if (status & ADIS16201_DIAG_STAT_POWER_LOW)
224 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
226 error_ret:
227 return ret;
230 static int adis16201_self_test(struct iio_dev *indio_dev)
232 int ret;
233 ret = adis16201_spi_write_reg_16(indio_dev,
234 ADIS16201_MSC_CTRL,
235 ADIS16201_MSC_CTRL_SELF_TEST_EN);
236 if (ret) {
237 dev_err(&indio_dev->dev, "problem starting self test");
238 goto err_ret;
241 ret = adis16201_check_status(indio_dev);
243 err_ret:
244 return ret;
247 static int adis16201_initial_setup(struct iio_dev *indio_dev)
249 int ret;
250 struct device *dev = &indio_dev->dev;
252 /* Disable IRQ */
253 ret = adis16201_set_irq(indio_dev, false);
254 if (ret) {
255 dev_err(dev, "disable irq failed");
256 goto err_ret;
259 /* Do self test */
260 ret = adis16201_self_test(indio_dev);
261 if (ret) {
262 dev_err(dev, "self test failure");
263 goto err_ret;
266 /* Read status register to check the result */
267 ret = adis16201_check_status(indio_dev);
268 if (ret) {
269 adis16201_reset(indio_dev);
270 dev_err(dev, "device not playing ball -> reset");
271 msleep(ADIS16201_STARTUP_DELAY);
272 ret = adis16201_check_status(indio_dev);
273 if (ret) {
274 dev_err(dev, "giving up");
275 goto err_ret;
279 err_ret:
280 return ret;
283 static u8 adis16201_addresses[7][2] = {
284 [in_supply] = { ADIS16201_SUPPLY_OUT, },
285 [temp] = { ADIS16201_TEMP_OUT },
286 [accel_x] = { ADIS16201_XACCL_OUT, ADIS16201_XACCL_OFFS },
287 [accel_y] = { ADIS16201_YACCL_OUT, ADIS16201_YACCL_OFFS },
288 [in_aux] = { ADIS16201_AUX_ADC },
289 [incli_x] = { ADIS16201_XINCL_OUT },
290 [incli_y] = { ADIS16201_YINCL_OUT },
293 static int adis16201_read_raw(struct iio_dev *indio_dev,
294 struct iio_chan_spec const *chan,
295 int *val, int *val2,
296 long mask)
298 int ret;
299 int bits;
300 u8 addr;
301 s16 val16;
303 switch (mask) {
304 case 0:
305 mutex_lock(&indio_dev->mlock);
306 addr = adis16201_addresses[chan->address][0];
307 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
308 if (ret) {
309 mutex_unlock(&indio_dev->mlock);
310 return ret;
313 if (val16 & ADIS16201_ERROR_ACTIVE) {
314 ret = adis16201_check_status(indio_dev);
315 if (ret) {
316 mutex_unlock(&indio_dev->mlock);
317 return ret;
320 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
321 if (chan->scan_type.sign == 's')
322 val16 = (s16)(val16 <<
323 (16 - chan->scan_type.realbits)) >>
324 (16 - chan->scan_type.realbits);
325 *val = val16;
326 mutex_unlock(&indio_dev->mlock);
327 return IIO_VAL_INT;
328 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
329 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
330 switch (chan->type) {
331 case IIO_IN:
332 *val = 0;
333 if (chan->channel == 0)
334 *val2 = 1220;
335 else
336 *val2 = 610;
337 return IIO_VAL_INT_PLUS_MICRO;
338 case IIO_TEMP:
339 *val = 0;
340 *val2 = -470000;
341 return IIO_VAL_INT_PLUS_MICRO;
342 case IIO_ACCEL:
343 *val = 0;
344 *val2 = 462500;
345 return IIO_VAL_INT_PLUS_MICRO;
346 case IIO_INCLI:
347 *val = 0;
348 *val2 = 100000;
349 return IIO_VAL_INT_PLUS_MICRO;
350 default:
351 return -EINVAL;
353 break;
354 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
355 *val = 25;
356 return IIO_VAL_INT;
357 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
358 switch (chan->type) {
359 case IIO_ACCEL:
360 bits = 12;
361 break;
362 case IIO_INCLI:
363 bits = 9;
364 break;
365 default:
366 return -EINVAL;
368 mutex_lock(&indio_dev->mlock);
369 addr = adis16201_addresses[chan->address][1];
370 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
371 if (ret) {
372 mutex_unlock(&indio_dev->mlock);
373 return ret;
375 val16 &= (1 << bits) - 1;
376 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
377 *val = val16;
378 mutex_unlock(&indio_dev->mlock);
379 return IIO_VAL_INT;
381 return -EINVAL;
384 static int adis16201_write_raw(struct iio_dev *indio_dev,
385 struct iio_chan_spec const *chan,
386 int val,
387 int val2,
388 long mask)
390 int bits;
391 s16 val16;
392 u8 addr;
393 switch (mask) {
394 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
395 switch (chan->type) {
396 case IIO_ACCEL:
397 bits = 12;
398 break;
399 case IIO_INCLI:
400 bits = 9;
401 break;
402 default:
403 return -EINVAL;
405 val16 = val & ((1 << bits) - 1);
406 addr = adis16201_addresses[chan->address][1];
407 return adis16201_spi_write_reg_16(indio_dev, addr, val16);
409 return -EINVAL;
412 static struct iio_chan_spec adis16201_channels[] = {
413 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0,
414 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
415 in_supply, ADIS16201_SCAN_SUPPLY,
416 IIO_ST('u', 12, 16, 0), 0),
417 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
418 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
419 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE),
420 temp, ADIS16201_SCAN_TEMP,
421 IIO_ST('u', 12, 16, 0), 0),
422 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
423 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
424 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
425 accel_x, ADIS16201_SCAN_ACC_X,
426 IIO_ST('s', 14, 16, 0), 0),
427 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
428 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
429 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
430 accel_y, ADIS16201_SCAN_ACC_Y,
431 IIO_ST('s', 14, 16, 0), 0),
432 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
433 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
434 in_aux, ADIS16201_SCAN_AUX_ADC,
435 IIO_ST('u', 12, 16, 0), 0),
436 IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_X,
437 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
438 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
439 incli_x, ADIS16201_SCAN_INCLI_X,
440 IIO_ST('s', 14, 16, 0), 0),
441 IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_Y,
442 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
443 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
444 incli_y, ADIS16201_SCAN_INCLI_Y,
445 IIO_ST('s', 14, 16, 0), 0),
446 IIO_CHAN_SOFT_TIMESTAMP(7)
449 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16201_write_reset, 0);
451 static struct attribute *adis16201_attributes[] = {
452 &iio_dev_attr_reset.dev_attr.attr,
453 NULL
456 static const struct attribute_group adis16201_attribute_group = {
457 .attrs = adis16201_attributes,
460 static const struct iio_info adis16201_info = {
461 .attrs = &adis16201_attribute_group,
462 .read_raw = &adis16201_read_raw,
463 .write_raw = &adis16201_write_raw,
464 .driver_module = THIS_MODULE,
467 static int __devinit adis16201_probe(struct spi_device *spi)
469 int ret, regdone = 0;
470 struct adis16201_state *st;
471 struct iio_dev *indio_dev;
473 /* setup the industrialio driver allocated elements */
474 indio_dev = iio_allocate_device(sizeof(*st));
475 if (indio_dev == NULL) {
476 ret = -ENOMEM;
477 goto error_ret;
479 st = iio_priv(indio_dev);
480 /* this is only used for removal purposes */
481 spi_set_drvdata(spi, indio_dev);
483 st->us = spi;
484 mutex_init(&st->buf_lock);
486 indio_dev->name = spi->dev.driver->name;
487 indio_dev->dev.parent = &spi->dev;
488 indio_dev->info = &adis16201_info;
490 indio_dev->channels = adis16201_channels;
491 indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
492 indio_dev->modes = INDIO_DIRECT_MODE;
494 ret = adis16201_configure_ring(indio_dev);
495 if (ret)
496 goto error_free_dev;
498 ret = iio_device_register(indio_dev);
499 if (ret)
500 goto error_unreg_ring_funcs;
501 regdone = 1;
503 ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
504 adis16201_channels,
505 ARRAY_SIZE(adis16201_channels));
506 if (ret) {
507 printk(KERN_ERR "failed to initialize the ring\n");
508 goto error_unreg_ring_funcs;
511 if (spi->irq) {
512 ret = adis16201_probe_trigger(indio_dev);
513 if (ret)
514 goto error_uninitialize_ring;
517 /* Get the device into a sane initial state */
518 ret = adis16201_initial_setup(indio_dev);
519 if (ret)
520 goto error_remove_trigger;
521 return 0;
523 error_remove_trigger:
524 adis16201_remove_trigger(indio_dev);
525 error_uninitialize_ring:
526 iio_ring_buffer_unregister(indio_dev->ring);
527 error_unreg_ring_funcs:
528 adis16201_unconfigure_ring(indio_dev);
529 error_free_dev:
530 if (regdone)
531 iio_device_unregister(indio_dev);
532 else
533 iio_free_device(indio_dev);
534 error_ret:
535 return ret;
538 static int adis16201_remove(struct spi_device *spi)
540 struct iio_dev *indio_dev = spi_get_drvdata(spi);
542 adis16201_remove_trigger(indio_dev);
543 iio_ring_buffer_unregister(indio_dev->ring);
544 iio_device_unregister(indio_dev);
545 adis16201_unconfigure_ring(indio_dev);
547 return 0;
550 static struct spi_driver adis16201_driver = {
551 .driver = {
552 .name = "adis16201",
553 .owner = THIS_MODULE,
555 .probe = adis16201_probe,
556 .remove = __devexit_p(adis16201_remove),
559 static __init int adis16201_init(void)
561 return spi_register_driver(&adis16201_driver);
563 module_init(adis16201_init);
565 static __exit void adis16201_exit(void)
567 spi_unregister_driver(&adis16201_driver);
569 module_exit(adis16201_exit);
571 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
572 MODULE_DESCRIPTION("Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver");
573 MODULE_LICENSE("GPL v2");