Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[deliverable/linux.git] / drivers / staging / iio / accel / adis16209_core.c
1 /*
2 * ADIS16209 Programmable Digital Vibration Sensor driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
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/list.h>
17
18 #include "../iio.h"
19 #include "../sysfs.h"
20 #include "../ring_generic.h"
21 #include "accel.h"
22 #include "inclinometer.h"
23 #include "../adc/adc.h"
24
25 #include "adis16209.h"
26
27 #define DRIVER_NAME "adis16209"
28
29 /**
30 * adis16209_spi_write_reg_8() - write single byte to a register
31 * @indio_dev: iio device associated with actual device
32 * @reg_address: the address of the register to be written
33 * @val: the value to write
34 **/
35 static int adis16209_spi_write_reg_8(struct iio_dev *indio_dev,
36 u8 reg_address,
37 u8 val)
38 {
39 int ret;
40 struct adis16209_state *st = iio_priv(indio_dev);
41
42 mutex_lock(&st->buf_lock);
43 st->tx[0] = ADIS16209_WRITE_REG(reg_address);
44 st->tx[1] = val;
45
46 ret = spi_write(st->us, st->tx, 2);
47 mutex_unlock(&st->buf_lock);
48
49 return ret;
50 }
51
52 /**
53 * adis16209_spi_write_reg_16() - write 2 bytes to a pair of registers
54 * @indio_dev: iio device associated actual device
55 * @reg_address: the address of the lower of the two registers. Second register
56 * is assumed to have address one greater.
57 * @val: value to be written
58 **/
59 static int adis16209_spi_write_reg_16(struct iio_dev *indio_dev,
60 u8 lower_reg_address,
61 u16 value)
62 {
63 int ret;
64 struct spi_message msg;
65 struct adis16209_state *st = iio_priv(indio_dev);
66 struct spi_transfer xfers[] = {
67 {
68 .tx_buf = st->tx,
69 .bits_per_word = 8,
70 .len = 2,
71 .cs_change = 1,
72 .delay_usecs = 30,
73 }, {
74 .tx_buf = st->tx + 2,
75 .bits_per_word = 8,
76 .len = 2,
77 .delay_usecs = 30,
78 },
79 };
80
81 mutex_lock(&st->buf_lock);
82 st->tx[0] = ADIS16209_WRITE_REG(lower_reg_address);
83 st->tx[1] = value & 0xFF;
84 st->tx[2] = ADIS16209_WRITE_REG(lower_reg_address + 1);
85 st->tx[3] = (value >> 8) & 0xFF;
86
87 spi_message_init(&msg);
88 spi_message_add_tail(&xfers[0], &msg);
89 spi_message_add_tail(&xfers[1], &msg);
90 ret = spi_sync(st->us, &msg);
91 mutex_unlock(&st->buf_lock);
92
93 return ret;
94 }
95
96 /**
97 * adis16209_spi_read_reg_16() - read 2 bytes from a 16-bit register
98 * @indio_dev: iio device associated with device
99 * @reg_address: the address of the lower of the two registers. Second register
100 * is assumed to have address one greater.
101 * @val: somewhere to pass back the value read
102 **/
103 static int adis16209_spi_read_reg_16(struct iio_dev *indio_dev,
104 u8 lower_reg_address,
105 u16 *val)
106 {
107 struct spi_message msg;
108 struct adis16209_state *st = iio_priv(indio_dev);
109 int ret;
110 struct spi_transfer xfers[] = {
111 {
112 .tx_buf = st->tx,
113 .bits_per_word = 8,
114 .len = 2,
115 .cs_change = 1,
116 .delay_usecs = 30,
117 }, {
118 .rx_buf = st->rx,
119 .bits_per_word = 8,
120 .len = 2,
121 .delay_usecs = 30,
122 },
123 };
124
125 mutex_lock(&st->buf_lock);
126 st->tx[0] = ADIS16209_READ_REG(lower_reg_address);
127 st->tx[1] = 0;
128
129 spi_message_init(&msg);
130 spi_message_add_tail(&xfers[0], &msg);
131 spi_message_add_tail(&xfers[1], &msg);
132 ret = spi_sync(st->us, &msg);
133 if (ret) {
134 dev_err(&st->us->dev,
135 "problem when reading 16 bit register 0x%02X",
136 lower_reg_address);
137 goto error_ret;
138 }
139 *val = (st->rx[0] << 8) | st->rx[1];
140
141 error_ret:
142 mutex_unlock(&st->buf_lock);
143 return ret;
144 }
145
146 static int adis16209_reset(struct iio_dev *indio_dev)
147 {
148 int ret;
149 ret = adis16209_spi_write_reg_8(indio_dev,
150 ADIS16209_GLOB_CMD,
151 ADIS16209_GLOB_CMD_SW_RESET);
152 if (ret)
153 dev_err(&indio_dev->dev, "problem resetting device");
154
155 return ret;
156 }
157
158 static ssize_t adis16209_write_reset(struct device *dev,
159 struct device_attribute *attr,
160 const char *buf, size_t len)
161 {
162 struct iio_dev *indio_dev = dev_get_drvdata(dev);
163
164 if (len < 1)
165 return -EINVAL;
166 switch (buf[0]) {
167 case '1':
168 case 'y':
169 case 'Y':
170 return adis16209_reset(indio_dev);
171 }
172 return -EINVAL;
173 }
174
175 int adis16209_set_irq(struct iio_dev *indio_dev, bool enable)
176 {
177 int ret = 0;
178 u16 msc;
179
180 ret = adis16209_spi_read_reg_16(indio_dev, ADIS16209_MSC_CTRL, &msc);
181 if (ret)
182 goto error_ret;
183
184 msc |= ADIS16209_MSC_CTRL_ACTIVE_HIGH;
185 msc &= ~ADIS16209_MSC_CTRL_DATA_RDY_DIO2;
186 if (enable)
187 msc |= ADIS16209_MSC_CTRL_DATA_RDY_EN;
188 else
189 msc &= ~ADIS16209_MSC_CTRL_DATA_RDY_EN;
190
191 ret = adis16209_spi_write_reg_16(indio_dev, ADIS16209_MSC_CTRL, msc);
192
193 error_ret:
194 return ret;
195 }
196
197 static int adis16209_check_status(struct iio_dev *indio_dev)
198 {
199 u16 status;
200 int ret;
201
202 ret = adis16209_spi_read_reg_16(indio_dev,
203 ADIS16209_DIAG_STAT, &status);
204 if (ret < 0) {
205 dev_err(&indio_dev->dev, "Reading status failed\n");
206 goto error_ret;
207 }
208 ret = status & 0x1F;
209
210 if (status & ADIS16209_DIAG_STAT_SELFTEST_FAIL)
211 dev_err(&indio_dev->dev, "Self test failure\n");
212 if (status & ADIS16209_DIAG_STAT_SPI_FAIL)
213 dev_err(&indio_dev->dev, "SPI failure\n");
214 if (status & ADIS16209_DIAG_STAT_FLASH_UPT)
215 dev_err(&indio_dev->dev, "Flash update failed\n");
216 if (status & ADIS16209_DIAG_STAT_POWER_HIGH)
217 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
218 if (status & ADIS16209_DIAG_STAT_POWER_LOW)
219 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
220
221 error_ret:
222 return ret;
223 }
224
225 static int adis16209_self_test(struct iio_dev *indio_dev)
226 {
227 int ret;
228 ret = adis16209_spi_write_reg_16(indio_dev,
229 ADIS16209_MSC_CTRL,
230 ADIS16209_MSC_CTRL_SELF_TEST_EN);
231 if (ret) {
232 dev_err(&indio_dev->dev, "problem starting self test");
233 goto err_ret;
234 }
235
236 adis16209_check_status(indio_dev);
237
238 err_ret:
239 return ret;
240 }
241
242 static int adis16209_initial_setup(struct iio_dev *indio_dev)
243 {
244 int ret;
245
246 /* Disable IRQ */
247 ret = adis16209_set_irq(indio_dev, false);
248 if (ret) {
249 dev_err(&indio_dev->dev, "disable irq failed");
250 goto err_ret;
251 }
252
253 /* Do self test */
254 ret = adis16209_self_test(indio_dev);
255 if (ret) {
256 dev_err(&indio_dev->dev, "self test failure");
257 goto err_ret;
258 }
259
260 /* Read status register to check the result */
261 ret = adis16209_check_status(indio_dev);
262 if (ret) {
263 adis16209_reset(indio_dev);
264 dev_err(&indio_dev->dev, "device not playing ball -> reset");
265 msleep(ADIS16209_STARTUP_DELAY);
266 ret = adis16209_check_status(indio_dev);
267 if (ret) {
268 dev_err(&indio_dev->dev, "giving up");
269 goto err_ret;
270 }
271 }
272
273 err_ret:
274 return ret;
275 }
276
277 enum adis16209_chan {
278 in_supply,
279 temp,
280 accel_x,
281 accel_y,
282 incli_x,
283 incli_y,
284 in_aux,
285 rot,
286 };
287
288 static const u8 adis16209_addresses[8][2] = {
289 [in_supply] = { ADIS16209_SUPPLY_OUT },
290 [in_aux] = { ADIS16209_AUX_ADC },
291 [accel_x] = { ADIS16209_XACCL_OUT, ADIS16209_XACCL_NULL },
292 [accel_y] = { ADIS16209_YACCL_OUT, ADIS16209_YACCL_NULL },
293 [incli_x] = { ADIS16209_XINCL_OUT, ADIS16209_XINCL_NULL },
294 [incli_y] = { ADIS16209_YINCL_OUT, ADIS16209_YINCL_NULL },
295 [rot] = { ADIS16209_ROT_OUT },
296 [temp] = { ADIS16209_TEMP_OUT },
297 };
298
299 static int adis16209_write_raw(struct iio_dev *indio_dev,
300 struct iio_chan_spec const *chan,
301 int val,
302 int val2,
303 long mask)
304 {
305 int bits;
306 s16 val16;
307 u8 addr;
308 switch (mask) {
309 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
310 switch (chan->type) {
311 case IIO_ACCEL:
312 case IIO_INCLI:
313 bits = 14;
314 break;
315 default:
316 return -EINVAL;
317 };
318 val16 = val & ((1 << bits) - 1);
319 addr = adis16209_addresses[chan->address][1];
320 return adis16209_spi_write_reg_16(indio_dev, addr, val16);
321 }
322 return -EINVAL;
323 }
324
325 static int adis16209_read_raw(struct iio_dev *indio_dev,
326 struct iio_chan_spec const *chan,
327 int *val, int *val2,
328 long mask)
329 {
330 int ret;
331 int bits;
332 u8 addr;
333 s16 val16;
334
335 switch (mask) {
336 case 0:
337 mutex_lock(&indio_dev->mlock);
338 addr = adis16209_addresses[chan->address][0];
339 ret = adis16209_spi_read_reg_16(indio_dev, addr, &val16);
340 if (ret) {
341 mutex_unlock(&indio_dev->mlock);
342 return ret;
343 }
344
345 if (val16 & ADIS16209_ERROR_ACTIVE) {
346 ret = adis16209_check_status(indio_dev);
347 if (ret) {
348 mutex_unlock(&indio_dev->mlock);
349 return ret;
350 }
351 }
352 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
353 if (chan->scan_type.sign == 's')
354 val16 = (s16)(val16 <<
355 (16 - chan->scan_type.realbits)) >>
356 (16 - chan->scan_type.realbits);
357 *val = val16;
358 mutex_unlock(&indio_dev->mlock);
359 return IIO_VAL_INT;
360 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
361 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
362 switch (chan->type) {
363 case IIO_IN:
364 *val = 0;
365 if (chan->channel == 0)
366 *val2 = 305180;
367 else
368 *val2 = 610500;
369 return IIO_VAL_INT_PLUS_MICRO;
370 case IIO_TEMP:
371 *val = 0;
372 *val2 = -470000;
373 return IIO_VAL_INT_PLUS_MICRO;
374 case IIO_ACCEL:
375 *val = 0;
376 *val2 = 2394;
377 return IIO_VAL_INT_PLUS_MICRO;
378 case IIO_INCLI:
379 *val = 0;
380 *val2 = 436;
381 return IIO_VAL_INT_PLUS_MICRO;
382 default:
383 return -EINVAL;
384 }
385 break;
386 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
387 *val = 25;
388 return IIO_VAL_INT;
389 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
390 switch (chan->type) {
391 case IIO_ACCEL:
392 bits = 14;
393 break;
394 default:
395 return -EINVAL;
396 };
397 mutex_lock(&indio_dev->mlock);
398 addr = adis16209_addresses[chan->address][1];
399 ret = adis16209_spi_read_reg_16(indio_dev, addr, &val16);
400 if (ret) {
401 mutex_unlock(&indio_dev->mlock);
402 return ret;
403 }
404 val16 &= (1 << bits) - 1;
405 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
406 *val = val16;
407 mutex_unlock(&indio_dev->mlock);
408 return IIO_VAL_INT;
409 }
410 return -EINVAL;
411 }
412
413 static struct iio_chan_spec adis16209_channels[] = {
414 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
415 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
416 in_supply, ADIS16209_SCAN_SUPPLY,
417 IIO_ST('u', 14, 16, 0), 0),
418 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
419 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
420 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE),
421 temp, ADIS16209_SCAN_TEMP,
422 IIO_ST('u', 12, 16, 0), 0),
423 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
424 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
425 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
426 accel_x, ADIS16209_SCAN_ACC_X,
427 IIO_ST('s', 14, 16, 0), 0),
428 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
429 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
430 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
431 accel_y, ADIS16209_SCAN_ACC_Y,
432 IIO_ST('s', 14, 16, 0), 0),
433 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
434 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
435 in_aux, ADIS16209_SCAN_AUX_ADC,
436 IIO_ST('u', 12, 16, 0), 0),
437 IIO_CHAN(IIO_INCLI, 0, 1, 0, NULL, 0, IIO_MOD_X,
438 (1 << IIO_CHAN_INFO_SCALE_SHARED),
439 incli_x, ADIS16209_SCAN_INCLI_X,
440 IIO_ST('s', 14, 16, 0), 0),
441 IIO_CHAN(IIO_INCLI, 0, 1, 0, NULL, 0, IIO_MOD_Y,
442 (1 << IIO_CHAN_INFO_SCALE_SHARED),
443 incli_y, ADIS16209_SCAN_INCLI_Y,
444 IIO_ST('s', 14, 16, 0), 0),
445 IIO_CHAN(IIO_ROT, 0, 1, 0, NULL, 0, IIO_MOD_X,
446 0,
447 rot, ADIS16209_SCAN_ROT,
448 IIO_ST('s', 14, 16, 0), 0),
449 IIO_CHAN_SOFT_TIMESTAMP(8)
450 };
451
452 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16209_write_reset, 0);
453
454 static struct attribute *adis16209_attributes[] = {
455 &iio_dev_attr_reset.dev_attr.attr,
456 NULL
457 };
458
459 static const struct attribute_group adis16209_attribute_group = {
460 .attrs = adis16209_attributes,
461 };
462
463 static const struct iio_info adis16209_info = {
464 .attrs = &adis16209_attribute_group,
465 .read_raw = &adis16209_read_raw,
466 .write_raw = &adis16209_write_raw,
467 .driver_module = THIS_MODULE,
468 };
469
470 static int __devinit adis16209_probe(struct spi_device *spi)
471 {
472 int ret, regdone = 0;
473 struct adis16209_state *st;
474 struct iio_dev *indio_dev;
475
476 /* setup the industrialio driver allocated elements */
477 indio_dev = iio_allocate_device(sizeof(*st));
478 if (indio_dev == NULL) {
479 ret = -ENOMEM;
480 goto error_ret;
481 }
482 st = iio_priv(indio_dev);
483 /* this is only used for removal purposes */
484 spi_set_drvdata(spi, indio_dev);
485 st->us = spi;
486 mutex_init(&st->buf_lock);
487
488 indio_dev->name = spi->dev.driver->name;
489 indio_dev->dev.parent = &spi->dev;
490 indio_dev->info = &adis16209_info;
491 indio_dev->channels = adis16209_channels;
492 indio_dev->num_channels = ARRAY_SIZE(adis16209_channels);
493 indio_dev->modes = INDIO_DIRECT_MODE;
494
495 ret = adis16209_configure_ring(indio_dev);
496 if (ret)
497 goto error_free_dev;
498
499 ret = iio_device_register(indio_dev);
500 if (ret)
501 goto error_unreg_ring_funcs;
502 regdone = 1;
503
504 ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
505 adis16209_channels,
506 ARRAY_SIZE(adis16209_channels));
507 if (ret) {
508 printk(KERN_ERR "failed to initialize the ring\n");
509 goto error_unreg_ring_funcs;
510 }
511
512 if (spi->irq) {
513 ret = adis16209_probe_trigger(indio_dev);
514 if (ret)
515 goto error_uninitialize_ring;
516 }
517
518 /* Get the device into a sane initial state */
519 ret = adis16209_initial_setup(indio_dev);
520 if (ret)
521 goto error_remove_trigger;
522 return 0;
523
524 error_remove_trigger:
525 adis16209_remove_trigger(indio_dev);
526 error_uninitialize_ring:
527 iio_ring_buffer_unregister(indio_dev->ring);
528 error_unreg_ring_funcs:
529 adis16209_unconfigure_ring(indio_dev);
530 error_free_dev:
531 if (regdone)
532 iio_device_unregister(indio_dev);
533 else
534 iio_free_device(indio_dev);
535 error_ret:
536 return ret;
537 }
538
539 static int adis16209_remove(struct spi_device *spi)
540 {
541 struct iio_dev *indio_dev = spi_get_drvdata(spi);
542
543 flush_scheduled_work();
544
545 adis16209_remove_trigger(indio_dev);
546 iio_ring_buffer_unregister(indio_dev->ring);
547 iio_device_unregister(indio_dev);
548 adis16209_unconfigure_ring(indio_dev);
549
550 return 0;
551 }
552
553 static struct spi_driver adis16209_driver = {
554 .driver = {
555 .name = "adis16209",
556 .owner = THIS_MODULE,
557 },
558 .probe = adis16209_probe,
559 .remove = __devexit_p(adis16209_remove),
560 };
561
562 static __init int adis16209_init(void)
563 {
564 return spi_register_driver(&adis16209_driver);
565 }
566 module_init(adis16209_init);
567
568 static __exit void adis16209_exit(void)
569 {
570 spi_unregister_driver(&adis16209_driver);
571 }
572 module_exit(adis16209_exit);
573
574 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
575 MODULE_DESCRIPTION("Analog Devices ADIS16209 Digital Vibration Sensor driver");
576 MODULE_LICENSE("GPL v2");
This page took 0.045646 seconds and 6 git commands to generate.