staging:iio:light:tsl2563: chan_spec based channel setup.
[deliverable/linux.git] / drivers / staging / iio / accel / adis16201_core.c
CommitLineData
f7fe1d1d
BS
1/*
2 * ADIS16201 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/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
21#include "../iio.h"
22#include "../sysfs.h"
23#include "accel.h"
24#include "inclinometer.h"
25#include "../gyro/gyro.h"
26#include "../adc/adc.h"
27
28#include "adis16201.h"
29
30#define DRIVER_NAME "adis16201"
31
32static int adis16201_check_status(struct device *dev);
33
34/**
35 * adis16201_spi_write_reg_8() - write single byte to a register
36 * @dev: device associated with child of actual device (iio_dev or iio_trig)
37 * @reg_address: the address of the register to be written
38 * @val: the value to write
39 **/
40static int adis16201_spi_write_reg_8(struct device *dev,
41 u8 reg_address,
42 u8 val)
43{
44 int ret;
45 struct iio_dev *indio_dev = dev_get_drvdata(dev);
46 struct adis16201_state *st = iio_dev_get_devdata(indio_dev);
47
48 mutex_lock(&st->buf_lock);
49 st->tx[0] = ADIS16201_WRITE_REG(reg_address);
50 st->tx[1] = val;
51
52 ret = spi_write(st->us, st->tx, 2);
53 mutex_unlock(&st->buf_lock);
54
55 return ret;
56}
57
58/**
59 * adis16201_spi_write_reg_16() - write 2 bytes to a pair of registers
60 * @dev: device associated with child of actual device (iio_dev or iio_trig)
61 * @reg_address: the address of the lower of the two registers. Second register
62 * is assumed to have address one greater.
63 * @val: value to be written
64 **/
65static int adis16201_spi_write_reg_16(struct device *dev,
66 u8 lower_reg_address,
67 u16 value)
68{
69 int ret;
70 struct spi_message msg;
71 struct iio_dev *indio_dev = dev_get_drvdata(dev);
72 struct adis16201_state *st = iio_dev_get_devdata(indio_dev);
73 struct spi_transfer xfers[] = {
74 {
75 .tx_buf = st->tx,
76 .bits_per_word = 8,
77 .len = 2,
78 .cs_change = 1,
79 }, {
80 .tx_buf = st->tx + 2,
81 .bits_per_word = 8,
82 .len = 2,
83 .cs_change = 1,
84 },
85 };
86
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;
92
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);
98
99 return ret;
100}
101
102/**
103 * adis16201_spi_read_reg_16() - read 2 bytes from a 16-bit register
104 * @dev: device associated with child of actual device (iio_dev or iio_trig)
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
108 **/
109static int adis16201_spi_read_reg_16(struct device *dev,
110 u8 lower_reg_address,
111 u16 *val)
112{
113 struct spi_message msg;
114 struct iio_dev *indio_dev = dev_get_drvdata(dev);
115 struct adis16201_state *st = iio_dev_get_devdata(indio_dev);
116 int ret;
117 struct spi_transfer xfers[] = {
118 {
119 .tx_buf = st->tx,
120 .bits_per_word = 8,
121 .len = 2,
122 .cs_change = 1,
123 .delay_usecs = 20,
124 }, {
125 .rx_buf = st->rx,
126 .bits_per_word = 8,
127 .len = 2,
128 .cs_change = 1,
129 .delay_usecs = 20,
130 },
131 };
132
133 mutex_lock(&st->buf_lock);
134 st->tx[0] = ADIS16201_READ_REG(lower_reg_address);
135 st->tx[1] = 0;
136
137 spi_message_init(&msg);
138 spi_message_add_tail(&xfers[0], &msg);
139 spi_message_add_tail(&xfers[1], &msg);
140 ret = spi_sync(st->us, &msg);
141 if (ret) {
142 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
143 lower_reg_address);
144 goto error_ret;
145 }
146 *val = (st->rx[0] << 8) | st->rx[1];
147
148error_ret:
149 mutex_unlock(&st->buf_lock);
150 return ret;
151}
152
153static ssize_t adis16201_read_12bit_unsigned(struct device *dev,
154 struct device_attribute *attr,
155 char *buf)
156{
157 int ret;
158 u16 val = 0;
159 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
160
161 ret = adis16201_spi_read_reg_16(dev, this_attr->address, &val);
162 if (ret)
163 return ret;
164
165 if (val & ADIS16201_ERROR_ACTIVE) {
166 ret = adis16201_check_status(dev);
167 if (ret)
168 return ret;
169 }
170
171 return sprintf(buf, "%u\n", val & 0x0FFF);
172}
173
174static ssize_t adis16201_read_temp(struct device *dev,
175 struct device_attribute *attr,
176 char *buf)
177{
178 struct iio_dev *indio_dev = dev_get_drvdata(dev);
179 ssize_t ret;
180 u16 val;
181
182 /* Take the iio_dev status lock */
183 mutex_lock(&indio_dev->mlock);
184
185 ret = adis16201_spi_read_reg_16(dev, ADIS16201_TEMP_OUT, (u16 *)&val);
186 if (ret)
187 goto error_ret;
188
189 if (val & ADIS16201_ERROR_ACTIVE) {
190 ret = adis16201_check_status(dev);
191 if (ret)
192 goto error_ret;
193 }
194
195 val &= 0xFFF;
196 ret = sprintf(buf, "%d\n", val);
197
198error_ret:
199 mutex_unlock(&indio_dev->mlock);
200 return ret;
201}
202
203static ssize_t adis16201_read_9bit_signed(struct device *dev,
204 struct device_attribute *attr,
205 char *buf)
206{
207 struct iio_dev *indio_dev = dev_get_drvdata(dev);
208 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
209 s16 val = 0;
210 ssize_t ret;
211
212 mutex_lock(&indio_dev->mlock);
213
214 ret = adis16201_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
215 if (!ret) {
216 if (val & ADIS16201_ERROR_ACTIVE) {
217 ret = adis16201_check_status(dev);
218 if (ret)
219 goto error_ret;
220 }
221 val = ((s16)(val << 7) >> 7);
222 ret = sprintf(buf, "%d\n", val);
223 }
224
225error_ret:
226 mutex_unlock(&indio_dev->mlock);
227
228 return ret;
229}
230
231static ssize_t adis16201_read_12bit_signed(struct device *dev,
232 struct device_attribute *attr,
233 char *buf)
234{
235 struct iio_dev *indio_dev = dev_get_drvdata(dev);
236 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
237 s16 val = 0;
238 ssize_t ret;
239
240 mutex_lock(&indio_dev->mlock);
241
242 ret = adis16201_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
243 if (!ret) {
244 if (val & ADIS16201_ERROR_ACTIVE) {
245 ret = adis16201_check_status(dev);
246 if (ret)
247 goto error_ret;
248 }
249
250 val = ((s16)(val << 4) >> 4);
251 ret = sprintf(buf, "%d\n", val);
252 }
253
254error_ret:
255 mutex_unlock(&indio_dev->mlock);
256
257 return ret;
258}
259
260static ssize_t adis16201_read_14bit_signed(struct device *dev,
261 struct device_attribute *attr,
262 char *buf)
263{
264 struct iio_dev *indio_dev = dev_get_drvdata(dev);
265 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
266 s16 val = 0;
267 ssize_t ret;
268
269 mutex_lock(&indio_dev->mlock);
270
271 ret = adis16201_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
272 if (!ret) {
273 if (val & ADIS16201_ERROR_ACTIVE) {
274 ret = adis16201_check_status(dev);
275 if (ret)
276 goto error_ret;
277 }
278
279 val = ((s16)(val << 2) >> 2);
280 ret = sprintf(buf, "%d\n", val);
281 }
282
283error_ret:
284 mutex_unlock(&indio_dev->mlock);
285
286 return ret;
287}
288
289static ssize_t adis16201_write_16bit(struct device *dev,
290 struct device_attribute *attr,
291 const char *buf,
292 size_t len)
293{
294 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
295 int ret;
296 long val;
297
298 ret = strict_strtol(buf, 10, &val);
299 if (ret)
300 goto error_ret;
301 ret = adis16201_spi_write_reg_16(dev, this_attr->address, val);
302
303error_ret:
304 return ret ? ret : len;
305}
306
307static int adis16201_reset(struct device *dev)
308{
309 int ret;
310 ret = adis16201_spi_write_reg_8(dev,
311 ADIS16201_GLOB_CMD,
312 ADIS16201_GLOB_CMD_SW_RESET);
313 if (ret)
314 dev_err(dev, "problem resetting device");
315
316 return ret;
317}
318
319static ssize_t adis16201_write_reset(struct device *dev,
320 struct device_attribute *attr,
321 const char *buf, size_t len)
322{
323 if (len < 1)
324 return -EINVAL;
325 switch (buf[0]) {
326 case '1':
327 case 'y':
328 case 'Y':
329 return adis16201_reset(dev);
330 }
331 return -EINVAL;
332}
333
334int adis16201_set_irq(struct device *dev, bool enable)
335{
336 int ret = 0;
337 u16 msc;
338
339 ret = adis16201_spi_read_reg_16(dev, ADIS16201_MSC_CTRL, &msc);
340 if (ret)
341 goto error_ret;
342
343 msc |= ADIS16201_MSC_CTRL_ACTIVE_HIGH;
344 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_DIO1;
345 if (enable)
346 msc |= ADIS16201_MSC_CTRL_DATA_RDY_EN;
347 else
348 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_EN;
349
350 ret = adis16201_spi_write_reg_16(dev, ADIS16201_MSC_CTRL, msc);
351
352error_ret:
353 return ret;
354}
355
356static int adis16201_check_status(struct device *dev)
357{
358 u16 status;
359 int ret;
360
361 ret = adis16201_spi_read_reg_16(dev, ADIS16201_DIAG_STAT, &status);
362 if (ret < 0) {
363 dev_err(dev, "Reading status failed\n");
364 goto error_ret;
365 }
366 ret = status & 0xF;
367 if (ret)
368 ret = -EFAULT;
369
370 if (status & ADIS16201_DIAG_STAT_SPI_FAIL)
371 dev_err(dev, "SPI failure\n");
372 if (status & ADIS16201_DIAG_STAT_FLASH_UPT)
373 dev_err(dev, "Flash update failed\n");
374 if (status & ADIS16201_DIAG_STAT_POWER_HIGH)
375 dev_err(dev, "Power supply above 3.625V\n");
376 if (status & ADIS16201_DIAG_STAT_POWER_LOW)
377 dev_err(dev, "Power supply below 3.15V\n");
378
379error_ret:
380 return ret;
381}
382
383static int adis16201_self_test(struct device *dev)
384{
385 int ret;
386 ret = adis16201_spi_write_reg_16(dev,
387 ADIS16201_MSC_CTRL,
388 ADIS16201_MSC_CTRL_SELF_TEST_EN);
389 if (ret) {
390 dev_err(dev, "problem starting self test");
391 goto err_ret;
392 }
393
394 ret = adis16201_check_status(dev);
395
396err_ret:
397 return ret;
398}
399
400static int adis16201_initial_setup(struct adis16201_state *st)
401{
402 int ret;
403 struct device *dev = &st->indio_dev->dev;
404
405 /* Disable IRQ */
406 ret = adis16201_set_irq(dev, false);
407 if (ret) {
408 dev_err(dev, "disable irq failed");
409 goto err_ret;
410 }
411
412 /* Do self test */
413 ret = adis16201_self_test(dev);
414 if (ret) {
415 dev_err(dev, "self test failure");
416 goto err_ret;
417 }
418
419 /* Read status register to check the result */
420 ret = adis16201_check_status(dev);
421 if (ret) {
422 adis16201_reset(dev);
423 dev_err(dev, "device not playing ball -> reset");
424 msleep(ADIS16201_STARTUP_DELAY);
425 ret = adis16201_check_status(dev);
426 if (ret) {
427 dev_err(dev, "giving up");
428 goto err_ret;
429 }
430 }
431
432 printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
433 st->us->chip_select, st->us->irq);
434
435err_ret:
436 return ret;
437}
438
439static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16201_read_12bit_unsigned,
440 ADIS16201_SUPPLY_OUT);
441static IIO_CONST_ATTR(in0_supply_scale, "0.00122");
442static IIO_DEV_ATTR_IN_RAW(1, adis16201_read_12bit_unsigned,
443 ADIS16201_AUX_ADC);
444static IIO_CONST_ATTR(in1_scale, "0.00061");
445
446static IIO_DEV_ATTR_ACCEL_X(adis16201_read_14bit_signed,
447 ADIS16201_XACCL_OUT);
448static IIO_DEV_ATTR_ACCEL_Y(adis16201_read_14bit_signed,
449 ADIS16201_YACCL_OUT);
450static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO,
451 adis16201_read_12bit_signed,
452 adis16201_write_16bit,
453 ADIS16201_XACCL_OFFS);
454static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO,
455 adis16201_read_12bit_signed,
456 adis16201_write_16bit,
457 ADIS16201_YACCL_OFFS);
458static IIO_CONST_ATTR(accel_scale, "0.4625");
459
460static IIO_DEV_ATTR_INCLI_X(adis16201_read_14bit_signed,
461 ADIS16201_XINCL_OUT);
462static IIO_DEV_ATTR_INCLI_Y(adis16201_read_14bit_signed,
463 ADIS16201_YINCL_OUT);
464static IIO_DEV_ATTR_INCLI_X_OFFSET(S_IWUSR | S_IRUGO,
465 adis16201_read_9bit_signed,
466 adis16201_write_16bit,
467 ADIS16201_XACCL_OFFS);
468static IIO_DEV_ATTR_INCLI_Y_OFFSET(S_IWUSR | S_IRUGO,
469 adis16201_read_9bit_signed,
470 adis16201_write_16bit,
471 ADIS16201_YACCL_OFFS);
472static IIO_CONST_ATTR(incli_scale, "0.1");
473
474static IIO_DEV_ATTR_TEMP_RAW(adis16201_read_temp);
475static IIO_CONST_ATTR(temp_offset, "25");
476static IIO_CONST_ATTR(temp_scale, "-0.47");
477
478static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16201_write_reset, 0);
479
f7fe1d1d
BS
480static struct attribute *adis16201_attributes[] = {
481 &iio_dev_attr_in0_supply_raw.dev_attr.attr,
482 &iio_const_attr_in0_supply_scale.dev_attr.attr,
483 &iio_dev_attr_temp_raw.dev_attr.attr,
484 &iio_const_attr_temp_offset.dev_attr.attr,
485 &iio_const_attr_temp_scale.dev_attr.attr,
486 &iio_dev_attr_reset.dev_attr.attr,
f7fe1d1d
BS
487 &iio_dev_attr_in1_raw.dev_attr.attr,
488 &iio_const_attr_in1_scale.dev_attr.attr,
489 &iio_dev_attr_accel_x_raw.dev_attr.attr,
490 &iio_dev_attr_accel_y_raw.dev_attr.attr,
491 &iio_dev_attr_accel_x_offset.dev_attr.attr,
492 &iio_dev_attr_accel_y_offset.dev_attr.attr,
493 &iio_const_attr_accel_scale.dev_attr.attr,
494 &iio_dev_attr_incli_x_raw.dev_attr.attr,
495 &iio_dev_attr_incli_y_raw.dev_attr.attr,
496 &iio_dev_attr_incli_x_offset.dev_attr.attr,
497 &iio_dev_attr_incli_y_offset.dev_attr.attr,
498 &iio_const_attr_incli_scale.dev_attr.attr,
499 NULL
500};
501
502static const struct attribute_group adis16201_attribute_group = {
503 .attrs = adis16201_attributes,
504};
505
506static int __devinit adis16201_probe(struct spi_device *spi)
507{
508 int ret, regdone = 0;
509 struct adis16201_state *st = kzalloc(sizeof *st, GFP_KERNEL);
510 if (!st) {
511 ret = -ENOMEM;
512 goto error_ret;
513 }
514 /* this is only used for removal purposes */
515 spi_set_drvdata(spi, st);
516
517 /* Allocate the comms buffers */
518 st->rx = kzalloc(sizeof(*st->rx)*ADIS16201_MAX_RX, GFP_KERNEL);
519 if (st->rx == NULL) {
520 ret = -ENOMEM;
521 goto error_free_st;
522 }
523 st->tx = kzalloc(sizeof(*st->tx)*ADIS16201_MAX_TX, GFP_KERNEL);
524 if (st->tx == NULL) {
525 ret = -ENOMEM;
526 goto error_free_rx;
527 }
528 st->us = spi;
529 mutex_init(&st->buf_lock);
530 /* setup the industrialio driver allocated elements */
6f7c8ee5 531 st->indio_dev = iio_allocate_device(0);
f7fe1d1d
BS
532 if (st->indio_dev == NULL) {
533 ret = -ENOMEM;
534 goto error_free_tx;
535 }
536
845bd12a 537 st->indio_dev->name = spi->dev.driver->name;
f7fe1d1d 538 st->indio_dev->dev.parent = &spi->dev;
f7fe1d1d
BS
539 st->indio_dev->attrs = &adis16201_attribute_group;
540 st->indio_dev->dev_data = (void *)(st);
541 st->indio_dev->driver_module = THIS_MODULE;
542 st->indio_dev->modes = INDIO_DIRECT_MODE;
543
544 ret = adis16201_configure_ring(st->indio_dev);
545 if (ret)
546 goto error_free_dev;
547
548 ret = iio_device_register(st->indio_dev);
549 if (ret)
550 goto error_unreg_ring_funcs;
551 regdone = 1;
552
553 ret = adis16201_initialize_ring(st->indio_dev->ring);
554 if (ret) {
555 printk(KERN_ERR "failed to initialize the ring\n");
556 goto error_unreg_ring_funcs;
557 }
558
559 if (spi->irq) {
f7fe1d1d
BS
560 ret = adis16201_probe_trigger(st->indio_dev);
561 if (ret)
80782446 562 goto error_uninitialize_ring;
f7fe1d1d
BS
563 }
564
565 /* Get the device into a sane initial state */
566 ret = adis16201_initial_setup(st);
567 if (ret)
568 goto error_remove_trigger;
569 return 0;
570
571error_remove_trigger:
572 adis16201_remove_trigger(st->indio_dev);
f7fe1d1d
BS
573error_uninitialize_ring:
574 adis16201_uninitialize_ring(st->indio_dev->ring);
575error_unreg_ring_funcs:
576 adis16201_unconfigure_ring(st->indio_dev);
577error_free_dev:
578 if (regdone)
579 iio_device_unregister(st->indio_dev);
580 else
581 iio_free_device(st->indio_dev);
582error_free_tx:
583 kfree(st->tx);
584error_free_rx:
585 kfree(st->rx);
586error_free_st:
587 kfree(st);
588error_ret:
589 return ret;
590}
591
592static int adis16201_remove(struct spi_device *spi)
593{
594 struct adis16201_state *st = spi_get_drvdata(spi);
595 struct iio_dev *indio_dev = st->indio_dev;
596
597 flush_scheduled_work();
598
599 adis16201_remove_trigger(indio_dev);
f7fe1d1d
BS
600 adis16201_uninitialize_ring(indio_dev->ring);
601 iio_device_unregister(indio_dev);
602 adis16201_unconfigure_ring(indio_dev);
603 kfree(st->tx);
604 kfree(st->rx);
605 kfree(st);
606
607 return 0;
608}
609
610static struct spi_driver adis16201_driver = {
611 .driver = {
612 .name = "adis16201",
613 .owner = THIS_MODULE,
614 },
615 .probe = adis16201_probe,
616 .remove = __devexit_p(adis16201_remove),
617};
618
619static __init int adis16201_init(void)
620{
621 return spi_register_driver(&adis16201_driver);
622}
623module_init(adis16201_init);
624
625static __exit void adis16201_exit(void)
626{
627 spi_unregister_driver(&adis16201_driver);
628}
629module_exit(adis16201_exit);
630
631MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
632MODULE_DESCRIPTION("Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver");
633MODULE_LICENSE("GPL v2");
This page took 0.103164 seconds and 5 git commands to generate.