Merge branch 'samsung/pinctrl' into next/drivers
[deliverable/linux.git] / drivers / staging / iio / accel / adis16240_core.c
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>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
25
26 #include "adis16240.h"
27
28 #define DRIVER_NAME "adis16240"
29
30 static int adis16240_check_status(struct iio_dev *indio_dev);
31
32 /**
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
37 **/
38 static int adis16240_spi_write_reg_8(struct iio_dev *indio_dev,
39 u8 reg_address,
40 u8 val)
41 {
42 int ret;
43 struct adis16240_state *st = iio_priv(indio_dev);
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
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
61 **/
62 static int adis16240_spi_write_reg_16(struct iio_dev *indio_dev,
63 u8 lower_reg_address,
64 u16 value)
65 {
66 int ret;
67 struct spi_message msg;
68 struct adis16240_state *st = iio_priv(indio_dev);
69 struct spi_transfer xfers[] = {
70 {
71 .tx_buf = st->tx,
72 .bits_per_word = 8,
73 .len = 2,
74 .cs_change = 1,
75 .delay_usecs = 35,
76 }, {
77 .tx_buf = st->tx + 2,
78 .bits_per_word = 8,
79 .len = 2,
80 .delay_usecs = 35,
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
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
105 **/
106 static int adis16240_spi_read_reg_16(struct iio_dev *indio_dev,
107 u8 lower_reg_address,
108 u16 *val)
109 {
110 struct spi_message msg;
111 struct adis16240_state *st = iio_priv(indio_dev);
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,
119 .delay_usecs = 35,
120 }, {
121 .rx_buf = st->rx,
122 .bits_per_word = 8,
123 .len = 2,
124 .cs_change = 1,
125 .delay_usecs = 35,
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 {
157 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
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
163 ret = adis16240_spi_read_reg_16(indio_dev,
164 this_attr->address, (u16 *)&val);
165 if (ret)
166 return ret;
167
168 if (val & ADIS16240_ERROR_ACTIVE)
169 adis16240_check_status(indio_dev);
170
171 val = ((s16)(val << shift) >> shift);
172 return sprintf(buf, "%d\n", val);
173 }
174
175 static ssize_t adis16240_read_12bit_signed(struct device *dev,
176 struct device_attribute *attr,
177 char *buf)
178 {
179 ssize_t ret;
180 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
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
190 static int adis16240_reset(struct iio_dev *indio_dev)
191 {
192 int ret;
193 ret = adis16240_spi_write_reg_8(indio_dev,
194 ADIS16240_GLOB_CMD,
195 ADIS16240_GLOB_CMD_SW_RESET);
196 if (ret)
197 dev_err(&indio_dev->dev, "problem resetting device");
198
199 return ret;
200 }
201
202 int adis16240_set_irq(struct iio_dev *indio_dev, bool enable)
203 {
204 int ret = 0;
205 u16 msc;
206
207 ret = adis16240_spi_read_reg_16(indio_dev,
208 ADIS16240_MSC_CTRL, &msc);
209 if (ret)
210 goto error_ret;
211
212 msc |= ADIS16240_MSC_CTRL_ACTIVE_HIGH;
213 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2;
214 if (enable)
215 msc |= ADIS16240_MSC_CTRL_DATA_RDY_EN;
216 else
217 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_EN;
218
219 ret = adis16240_spi_write_reg_16(indio_dev,
220 ADIS16240_MSC_CTRL, msc);
221
222 error_ret:
223 return ret;
224 }
225
226 static int adis16240_self_test(struct iio_dev *indio_dev)
227 {
228 int ret;
229 ret = adis16240_spi_write_reg_16(indio_dev,
230 ADIS16240_MSC_CTRL,
231 ADIS16240_MSC_CTRL_SELF_TEST_EN);
232 if (ret) {
233 dev_err(&indio_dev->dev, "problem starting self test");
234 goto err_ret;
235 }
236
237 msleep(ADIS16240_STARTUP_DELAY);
238
239 adis16240_check_status(indio_dev);
240
241 err_ret:
242 return ret;
243 }
244
245 static int adis16240_check_status(struct iio_dev *indio_dev)
246 {
247 u16 status;
248 int ret;
249 struct device *dev = &indio_dev->dev;
250
251 ret = adis16240_spi_read_reg_16(indio_dev,
252 ADIS16240_DIAG_STAT, &status);
253
254 if (ret < 0) {
255 dev_err(dev, "Reading status failed\n");
256 goto error_ret;
257 }
258
259 ret = status & 0x2F;
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");
270
271 error_ret:
272 return ret;
273 }
274
275 static int adis16240_initial_setup(struct iio_dev *indio_dev)
276 {
277 int ret;
278 struct device *dev = &indio_dev->dev;
279
280 /* Disable IRQ */
281 ret = adis16240_set_irq(indio_dev, false);
282 if (ret) {
283 dev_err(dev, "disable irq failed");
284 goto err_ret;
285 }
286
287 /* Do self test */
288 ret = adis16240_self_test(indio_dev);
289 if (ret) {
290 dev_err(dev, "self test failure");
291 goto err_ret;
292 }
293
294 /* Read status register to check the result */
295 ret = adis16240_check_status(indio_dev);
296 if (ret) {
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);
301 if (ret) {
302 dev_err(dev, "giving up");
303 goto err_ret;
304 }
305 }
306
307 err_ret:
308 return ret;
309 }
310
311 static IIO_DEVICE_ATTR(in_accel_xyz_squared_peak_raw, S_IRUGO,
312 adis16240_read_12bit_signed, NULL,
313 ADIS16240_XYZPEAK_OUT);
314
315 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
316
317 enum adis16240_chan {
318 in_supply,
319 in_aux,
320 accel_x,
321 accel_y,
322 accel_z,
323 temp,
324 };
325
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 },
336 };
337
338 static int adis16240_read_raw(struct iio_dev *indio_dev,
339 struct iio_chan_spec const *chan,
340 int *val, int *val2,
341 long mask)
342 {
343 int ret;
344 int bits;
345 u8 addr;
346 s16 val16;
347
348 switch (mask) {
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);
353 if (ret) {
354 mutex_unlock(&indio_dev->mlock);
355 return ret;
356 }
357
358 if (val16 & ADIS16240_ERROR_ACTIVE) {
359 ret = adis16240_check_status(indio_dev);
360 if (ret) {
361 mutex_unlock(&indio_dev->mlock);
362 return ret;
363 }
364 }
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);
370 *val = val16;
371 mutex_unlock(&indio_dev->mlock);
372 return IIO_VAL_INT;
373 case IIO_CHAN_INFO_SCALE:
374 switch (chan->type) {
375 case IIO_VOLTAGE:
376 *val = 0;
377 if (chan->channel == 0)
378 *val2 = 4880;
379 else
380 return -EINVAL;
381 return IIO_VAL_INT_PLUS_MICRO;
382 case IIO_TEMP:
383 *val = 0;
384 *val2 = 244000;
385 return IIO_VAL_INT_PLUS_MICRO;
386 case IIO_ACCEL:
387 *val = 0;
388 *val2 = 504062;
389 return IIO_VAL_INT_PLUS_MICRO;
390 default:
391 return -EINVAL;
392 }
393 break;
394 case IIO_CHAN_INFO_PEAK_SCALE:
395 *val = 6;
396 *val2 = 629295;
397 return IIO_VAL_INT_PLUS_MICRO;
398 case IIO_CHAN_INFO_OFFSET:
399 *val = 25;
400 return IIO_VAL_INT;
401 case IIO_CHAN_INFO_CALIBBIAS:
402 bits = 10;
403 mutex_lock(&indio_dev->mlock);
404 addr = adis16240_addresses[chan->address][1];
405 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
406 if (ret) {
407 mutex_unlock(&indio_dev->mlock);
408 return ret;
409 }
410 val16 &= (1 << bits) - 1;
411 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
412 *val = val16;
413 mutex_unlock(&indio_dev->mlock);
414 return IIO_VAL_INT;
415 case IIO_CHAN_INFO_PEAK:
416 bits = 10;
417 mutex_lock(&indio_dev->mlock);
418 addr = adis16240_addresses[chan->address][2];
419 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
420 if (ret) {
421 mutex_unlock(&indio_dev->mlock);
422 return ret;
423 }
424 val16 &= (1 << bits) - 1;
425 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
426 *val = val16;
427 mutex_unlock(&indio_dev->mlock);
428 return IIO_VAL_INT;
429 }
430 return -EINVAL;
431 }
432
433 static int adis16240_write_raw(struct iio_dev *indio_dev,
434 struct iio_chan_spec const *chan,
435 int val,
436 int val2,
437 long mask)
438 {
439 int bits = 10;
440 s16 val16;
441 u8 addr;
442 switch (mask) {
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);
447 }
448 return -EINVAL;
449 }
450
451 static struct iio_chan_spec adis16240_channels[] = {
452 {
453 .type = IIO_VOLTAGE,
454 .indexed = 1,
455 .channel = 0,
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,
461 .scan_type = {
462 .sign = 'u',
463 .realbits = 10,
464 .storagebits = 16,
465 },
466 }, {
467 .type = IIO_VOLTAGE,
468 .indexed = 1,
469 .channel = 1,
470 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
471 .address = in_aux,
472 .scan_index = ADIS16240_SCAN_AUX_ADC,
473 .scan_type = {
474 .sign = 'u',
475 .realbits = 10,
476 .storagebits = 16,
477 },
478 }, {
479 .type = IIO_ACCEL,
480 .modified = 1,
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,
486 .address = accel_x,
487 .scan_index = ADIS16240_SCAN_ACC_X,
488 .scan_type = {
489 .sign = 's',
490 .realbits = 10,
491 .storagebits = 16,
492 },
493 }, {
494 .type = IIO_ACCEL,
495 .modified = 1,
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,
501 .address = accel_y,
502 .scan_index = ADIS16240_SCAN_ACC_Y,
503 .scan_type = {
504 .sign = 's',
505 .realbits = 10,
506 .storagebits = 16,
507 },
508 }, {
509 .type = IIO_ACCEL,
510 .modified = 1,
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,
516 .address = accel_z,
517 .scan_index = ADIS16240_SCAN_ACC_Z,
518 .scan_type = {
519 .sign = 's',
520 .realbits = 10,
521 .storagebits = 16,
522 },
523 }, {
524 .type = IIO_TEMP,
525 .indexed = 1,
526 .channel = 0,
527 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
528 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
529 .address = temp,
530 .scan_index = ADIS16240_SCAN_TEMP,
531 .scan_type = {
532 .sign = 'u',
533 .realbits = 10,
534 .storagebits = 16,
535 },
536 },
537 IIO_CHAN_SOFT_TIMESTAMP(6)
538 };
539
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,
543 NULL
544 };
545
546 static const struct attribute_group adis16240_attribute_group = {
547 .attrs = adis16240_attributes,
548 };
549
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,
555 };
556
557 static int __devinit adis16240_probe(struct spi_device *spi)
558 {
559 int ret;
560 struct adis16240_state *st;
561 struct iio_dev *indio_dev;
562
563 /* setup the industrialio driver allocated elements */
564 indio_dev = iio_device_alloc(sizeof(*st));
565 if (indio_dev == NULL) {
566 ret = -ENOMEM;
567 goto error_ret;
568 }
569 st = iio_priv(indio_dev);
570 /* this is only used for removal purposes */
571 spi_set_drvdata(spi, indio_dev);
572
573 st->us = spi;
574 mutex_init(&st->buf_lock);
575
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;
582
583 ret = adis16240_configure_ring(indio_dev);
584 if (ret)
585 goto error_free_dev;
586
587 ret = iio_buffer_register(indio_dev,
588 adis16240_channels,
589 ARRAY_SIZE(adis16240_channels));
590 if (ret) {
591 printk(KERN_ERR "failed to initialize the ring\n");
592 goto error_unreg_ring_funcs;
593 }
594
595 if (spi->irq) {
596 ret = adis16240_probe_trigger(indio_dev);
597 if (ret)
598 goto error_uninitialize_ring;
599 }
600
601 /* Get the device into a sane initial state */
602 ret = adis16240_initial_setup(indio_dev);
603 if (ret)
604 goto error_remove_trigger;
605 ret = iio_device_register(indio_dev);
606 if (ret)
607 goto error_remove_trigger;
608 return 0;
609
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);
616 error_free_dev:
617 iio_device_free(indio_dev);
618 error_ret:
619 return ret;
620 }
621
622 static int adis16240_remove(struct spi_device *spi)
623 {
624
625 struct iio_dev *indio_dev = spi_get_drvdata(spi);
626
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);
632
633 return 0;
634 }
635
636 static struct spi_driver adis16240_driver = {
637 .driver = {
638 .name = "adis16240",
639 .owner = THIS_MODULE,
640 },
641 .probe = adis16240_probe,
642 .remove = __devexit_p(adis16240_remove),
643 };
644 module_spi_driver(adis16240_driver);
645
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");
This page took 0.045894 seconds and 5 git commands to generate.