]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/iio/accel/adis16204_core.c
Merge branch '3.1-fixes-for-rmk' of git://linux-arm.org/linux-2.6-wd into fixes
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / iio / accel / adis16204_core.c
CommitLineData
bb6f19ea
BS
1/*
2 * ADIS16204 Programmable High-g Digital Impact Sensor and Recorder
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"
ad3eb9ab 23#include "../ring_generic.h"
bb6f19ea 24#include "accel.h"
bb6f19ea
BS
25#include "../adc/adc.h"
26
27#include "adis16204.h"
28
29#define DRIVER_NAME "adis16204"
30
bb6f19ea
BS
31/**
32 * adis16204_spi_write_reg_8() - write single byte to a register
33 * @dev: device associated with child of actual device (iio_dev or iio_trig)
34 * @reg_address: the address of the register to be written
35 * @val: the value to write
36 **/
ad3eb9ab 37static int adis16204_spi_write_reg_8(struct iio_dev *indio_dev,
bb6f19ea
BS
38 u8 reg_address,
39 u8 val)
40{
41 int ret;
4de66bbb 42 struct adis16204_state *st = iio_priv(indio_dev);
bb6f19ea
BS
43
44 mutex_lock(&st->buf_lock);
45 st->tx[0] = ADIS16204_WRITE_REG(reg_address);
46 st->tx[1] = val;
47
48 ret = spi_write(st->us, st->tx, 2);
49 mutex_unlock(&st->buf_lock);
50
51 return ret;
52}
53
54/**
55 * adis16204_spi_write_reg_16() - write 2 bytes to a pair of registers
ad3eb9ab 56 * @indio_dev: iio device associated with child of actual device
bb6f19ea
BS
57 * @reg_address: the address of the lower of the two registers. Second register
58 * is assumed to have address one greater.
59 * @val: value to be written
60 **/
ad3eb9ab 61static int adis16204_spi_write_reg_16(struct iio_dev *indio_dev,
bb6f19ea
BS
62 u8 lower_reg_address,
63 u16 value)
64{
65 int ret;
66 struct spi_message msg;
4de66bbb 67 struct adis16204_state *st = iio_priv(indio_dev);
bb6f19ea
BS
68 struct spi_transfer xfers[] = {
69 {
70 .tx_buf = st->tx,
71 .bits_per_word = 8,
72 .len = 2,
73 .cs_change = 1,
74 }, {
75 .tx_buf = st->tx + 2,
76 .bits_per_word = 8,
77 .len = 2,
78 .cs_change = 1,
79 },
80 };
81
82 mutex_lock(&st->buf_lock);
83 st->tx[0] = ADIS16204_WRITE_REG(lower_reg_address);
84 st->tx[1] = value & 0xFF;
85 st->tx[2] = ADIS16204_WRITE_REG(lower_reg_address + 1);
86 st->tx[3] = (value >> 8) & 0xFF;
87
88 spi_message_init(&msg);
89 spi_message_add_tail(&xfers[0], &msg);
90 spi_message_add_tail(&xfers[1], &msg);
91 ret = spi_sync(st->us, &msg);
92 mutex_unlock(&st->buf_lock);
93
94 return ret;
95}
96
97/**
98 * adis16204_spi_read_reg_16() - read 2 bytes from a 16-bit register
ad3eb9ab 99 * @indio_dev: iio device associated with child of actual device
bb6f19ea
BS
100 * @reg_address: the address of the lower of the two registers. Second register
101 * is assumed to have address one greater.
102 * @val: somewhere to pass back the value read
103 **/
ad3eb9ab
JC
104static int adis16204_spi_read_reg_16(struct iio_dev *indio_dev,
105 u8 lower_reg_address,
106 u16 *val)
bb6f19ea
BS
107{
108 struct spi_message msg;
4de66bbb 109 struct adis16204_state *st = iio_priv(indio_dev);
bb6f19ea
BS
110 int ret;
111 struct spi_transfer xfers[] = {
112 {
113 .tx_buf = st->tx,
114 .bits_per_word = 8,
115 .len = 2,
116 .cs_change = 1,
117 .delay_usecs = 20,
118 }, {
119 .rx_buf = st->rx,
120 .bits_per_word = 8,
121 .len = 2,
bb6f19ea
BS
122 .delay_usecs = 20,
123 },
124 };
125
126 mutex_lock(&st->buf_lock);
127 st->tx[0] = ADIS16204_READ_REG(lower_reg_address);
128 st->tx[1] = 0;
129
130 spi_message_init(&msg);
131 spi_message_add_tail(&xfers[0], &msg);
132 spi_message_add_tail(&xfers[1], &msg);
133 ret = spi_sync(st->us, &msg);
134 if (ret) {
135 dev_err(&st->us->dev, "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
141error_ret:
142 mutex_unlock(&st->buf_lock);
143 return ret;
144}
145
ad3eb9ab 146static int adis16204_check_status(struct iio_dev *indio_dev)
bb6f19ea 147{
ad3eb9ab 148 u16 status;
bb6f19ea 149 int ret;
bb6f19ea 150
ad3eb9ab
JC
151 ret = adis16204_spi_read_reg_16(indio_dev,
152 ADIS16204_DIAG_STAT, &status);
153 if (ret < 0) {
154 dev_err(&indio_dev->dev, "Reading status failed\n");
bb6f19ea 155 goto error_ret;
bb6f19ea 156 }
ad3eb9ab 157 ret = status & 0x1F;
bb6f19ea 158
ad3eb9ab
JC
159 if (status & ADIS16204_DIAG_STAT_SELFTEST_FAIL)
160 dev_err(&indio_dev->dev, "Self test failure\n");
161 if (status & ADIS16204_DIAG_STAT_SPI_FAIL)
162 dev_err(&indio_dev->dev, "SPI failure\n");
163 if (status & ADIS16204_DIAG_STAT_FLASH_UPT)
164 dev_err(&indio_dev->dev, "Flash update failed\n");
165 if (status & ADIS16204_DIAG_STAT_POWER_HIGH)
166 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
167 if (status & ADIS16204_DIAG_STAT_POWER_LOW)
168 dev_err(&indio_dev->dev, "Power supply below 2.975V\n");
bb6f19ea 169
ad3eb9ab 170error_ret:
bb6f19ea
BS
171 return ret;
172}
173
174static ssize_t adis16204_read_14bit_signed(struct device *dev,
175 struct device_attribute *attr,
176 char *buf)
177{
178 struct iio_dev *indio_dev = dev_get_drvdata(dev);
179 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
180 s16 val = 0;
181 ssize_t ret;
182
183 mutex_lock(&indio_dev->mlock);
184
ad3eb9ab
JC
185 ret = adis16204_spi_read_reg_16(indio_dev,
186 this_attr->address, (u16 *)&val);
bb6f19ea
BS
187 if (!ret) {
188 if (val & ADIS16204_ERROR_ACTIVE)
ad3eb9ab 189 adis16204_check_status(indio_dev);
bb6f19ea
BS
190
191 val = ((s16)(val << 2) >> 2);
192 ret = sprintf(buf, "%d\n", val);
193 }
194
195 mutex_unlock(&indio_dev->mlock);
196
197 return ret;
198}
199
ad3eb9ab 200static int adis16204_reset(struct iio_dev *indio_dev)
bb6f19ea 201{
bb6f19ea 202 int ret;
ad3eb9ab 203 ret = adis16204_spi_write_reg_8(indio_dev,
bb6f19ea
BS
204 ADIS16204_GLOB_CMD,
205 ADIS16204_GLOB_CMD_SW_RESET);
206 if (ret)
ad3eb9ab 207 dev_err(&indio_dev->dev, "problem resetting device");
bb6f19ea
BS
208
209 return ret;
210}
211
212static ssize_t adis16204_write_reset(struct device *dev,
213 struct device_attribute *attr,
214 const char *buf, size_t len)
215{
ad3eb9ab
JC
216 struct iio_dev *indio_dev = dev_get_drvdata(dev);
217
bb6f19ea
BS
218 if (len < 1)
219 return -EINVAL;
220 switch (buf[0]) {
221 case '1':
222 case 'y':
223 case 'Y':
ad3eb9ab 224 return adis16204_reset(indio_dev);
bb6f19ea
BS
225 }
226 return -EINVAL;
227}
228
ad3eb9ab 229int adis16204_set_irq(struct iio_dev *indio_dev, bool enable)
bb6f19ea
BS
230{
231 int ret = 0;
232 u16 msc;
233
ad3eb9ab 234 ret = adis16204_spi_read_reg_16(indio_dev, ADIS16204_MSC_CTRL, &msc);
bb6f19ea
BS
235 if (ret)
236 goto error_ret;
237
238 msc |= ADIS16204_MSC_CTRL_ACTIVE_HIGH;
239 msc &= ~ADIS16204_MSC_CTRL_DATA_RDY_DIO2;
240 if (enable)
241 msc |= ADIS16204_MSC_CTRL_DATA_RDY_EN;
242 else
243 msc &= ~ADIS16204_MSC_CTRL_DATA_RDY_EN;
244
ad3eb9ab 245 ret = adis16204_spi_write_reg_16(indio_dev, ADIS16204_MSC_CTRL, msc);
bb6f19ea
BS
246
247error_ret:
248 return ret;
249}
250
ad3eb9ab 251static int adis16204_self_test(struct iio_dev *indio_dev)
bb6f19ea 252{
bb6f19ea 253 int ret;
ad3eb9ab 254 ret = adis16204_spi_write_reg_16(indio_dev,
bb6f19ea
BS
255 ADIS16204_MSC_CTRL,
256 ADIS16204_MSC_CTRL_SELF_TEST_EN);
257 if (ret) {
ad3eb9ab 258 dev_err(&indio_dev->dev, "problem starting self test");
bb6f19ea
BS
259 goto err_ret;
260 }
261
ad3eb9ab 262 adis16204_check_status(indio_dev);
bb6f19ea
BS
263
264err_ret:
265 return ret;
266}
267
ad3eb9ab 268static int adis16204_initial_setup(struct iio_dev *indio_dev)
bb6f19ea
BS
269{
270 int ret;
bb6f19ea
BS
271
272 /* Disable IRQ */
ad3eb9ab 273 ret = adis16204_set_irq(indio_dev, false);
bb6f19ea 274 if (ret) {
ad3eb9ab 275 dev_err(&indio_dev->dev, "disable irq failed");
bb6f19ea
BS
276 goto err_ret;
277 }
278
279 /* Do self test */
ad3eb9ab 280 ret = adis16204_self_test(indio_dev);
bb6f19ea 281 if (ret) {
ad3eb9ab 282 dev_err(&indio_dev->dev, "self test failure");
bb6f19ea
BS
283 goto err_ret;
284 }
285
286 /* Read status register to check the result */
ad3eb9ab 287 ret = adis16204_check_status(indio_dev);
bb6f19ea 288 if (ret) {
ad3eb9ab
JC
289 adis16204_reset(indio_dev);
290 dev_err(&indio_dev->dev, "device not playing ball -> reset");
bb6f19ea 291 msleep(ADIS16204_STARTUP_DELAY);
ad3eb9ab 292 ret = adis16204_check_status(indio_dev);
bb6f19ea 293 if (ret) {
ad3eb9ab 294 dev_err(&indio_dev->dev, "giving up");
bb6f19ea
BS
295 goto err_ret;
296 }
297 }
298
bb6f19ea
BS
299err_ret:
300 return ret;
301}
bb6f19ea
BS
302static IIO_DEV_ATTR_ACCEL_XY(adis16204_read_14bit_signed,
303 ADIS16204_XY_RSS_OUT);
304static IIO_DEV_ATTR_ACCEL_XPEAK(adis16204_read_14bit_signed,
305 ADIS16204_X_PEAK_OUT);
306static IIO_DEV_ATTR_ACCEL_YPEAK(adis16204_read_14bit_signed,
307 ADIS16204_Y_PEAK_OUT);
308static IIO_DEV_ATTR_ACCEL_XYPEAK(adis16204_read_14bit_signed,
309 ADIS16204_XY_PEAK_OUT);
bb6f19ea
BS
310static IIO_CONST_ATTR(accel_xy_scale, "0.017125");
311
bb6f19ea
BS
312static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16204_write_reset, 0);
313
ad3eb9ab
JC
314enum adis16204_channel {
315 in_supply,
316 in_aux,
317 temp,
318 accel_x,
319 accel_y,
320};
321
322static u8 adis16204_addresses[5][2] = {
323 [in_supply] = { ADIS16204_SUPPLY_OUT },
324 [in_aux] = { ADIS16204_AUX_ADC },
325 [temp] = { ADIS16204_TEMP_OUT },
326 [accel_x] = { ADIS16204_XACCL_OUT, ADIS16204_XACCL_NULL },
327 [accel_y] = { ADIS16204_XACCL_OUT, ADIS16204_YACCL_NULL },
328};
329static int adis16204_read_raw(struct iio_dev *indio_dev,
330 struct iio_chan_spec const *chan,
331 int *val, int *val2,
332 long mask)
333{
334 int ret;
335 int bits;
336 u8 addr;
337 s16 val16;
338
339 switch (mask) {
340 case 0:
341 mutex_lock(&indio_dev->mlock);
342 addr = adis16204_addresses[chan->address][0];
343 ret = adis16204_spi_read_reg_16(indio_dev, addr, &val16);
8f896155
DC
344 if (ret) {
345 mutex_unlock(&indio_dev->mlock);
ad3eb9ab 346 return ret;
8f896155 347 }
ad3eb9ab
JC
348
349 if (val16 & ADIS16204_ERROR_ACTIVE) {
350 ret = adis16204_check_status(indio_dev);
8f896155
DC
351 if (ret) {
352 mutex_unlock(&indio_dev->mlock);
ad3eb9ab 353 return ret;
8f896155 354 }
ad3eb9ab
JC
355 }
356 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
357 if (chan->scan_type.sign == 's')
358 val16 = (s16)(val16 <<
359 (16 - chan->scan_type.realbits)) >>
360 (16 - chan->scan_type.realbits);
361 *val = val16;
362 mutex_unlock(&indio_dev->mlock);
363 return IIO_VAL_INT;
364 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
365 switch (chan->type) {
366 case IIO_IN:
367 *val = 0;
368 if (chan->channel == 0)
369 *val2 = 1220;
370 else
371 *val2 = 610;
372 return IIO_VAL_INT_PLUS_MICRO;
373 case IIO_TEMP:
374 *val = 0;
375 *val2 = -470000;
376 return IIO_VAL_INT_PLUS_MICRO;
377 case IIO_ACCEL:
378 *val = 0;
379 if (chan->channel == 'x')
380 *val2 = 17125;
381 else
382 *val2 = 8407;
383 return IIO_VAL_INT_PLUS_MICRO;
384 default:
385 return -EINVAL;
386 }
387 break;
388 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
389 *val = 25;
390 return IIO_VAL_INT;
391 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
392 switch (chan->type) {
393 case IIO_ACCEL:
394 bits = 12;
395 break;
396 default:
397 return -EINVAL;
398 };
399 mutex_lock(&indio_dev->mlock);
400 addr = adis16204_addresses[chan->address][1];
401 ret = adis16204_spi_read_reg_16(indio_dev, addr, &val16);
402 if (ret) {
403 mutex_unlock(&indio_dev->mlock);
404 return ret;
405 }
406 val16 &= (1 << bits) - 1;
407 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
408 *val = val16;
409 mutex_unlock(&indio_dev->mlock);
410 return IIO_VAL_INT;
411 }
412 return -EINVAL;
413}
414
415static int adis16204_write_raw(struct iio_dev *indio_dev,
416 struct iio_chan_spec const *chan,
417 int val,
418 int val2,
419 long mask)
420{
421 int bits;
422 s16 val16;
423 u8 addr;
424 switch (mask) {
425 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
426 switch (chan->type) {
427 case IIO_ACCEL:
428 bits = 12;
429 break;
430 default:
431 return -EINVAL;
432 };
433 val16 = val & ((1 << bits) - 1);
434 addr = adis16204_addresses[chan->address][1];
435 return adis16204_spi_write_reg_16(indio_dev, addr, val16);
436 }
437 return -EINVAL;
438}
439
440static struct iio_chan_spec adis16204_channels[] = {
441 IIO_CHAN(IIO_IN, 0, 0, 0, "supply", 0, 0,
442 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
443 in_supply, ADIS16204_SCAN_SUPPLY,
444 IIO_ST('u', 12, 16, 0), 0),
445 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
446 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
447 in_aux, ADIS16204_SCAN_AUX_ADC,
448 IIO_ST('u', 12, 16, 0), 0),
449 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
450 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
451 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE),
452 temp, ADIS16204_SCAN_TEMP,
453 IIO_ST('u', 12, 16, 0), 0),
454 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
455 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
456 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
457 accel_x, ADIS16204_SCAN_ACC_X,
458 IIO_ST('s', 14, 16, 0), 0),
459 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
460 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
461 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
462 accel_y, ADIS16204_SCAN_ACC_Y,
463 IIO_ST('s', 14, 16, 0), 0),
464 IIO_CHAN_SOFT_TIMESTAMP(5),
465};
bb6f19ea 466static struct attribute *adis16204_attributes[] = {
bb6f19ea 467 &iio_dev_attr_reset.dev_attr.attr,
bb6f19ea
BS
468 &iio_dev_attr_accel_xy.dev_attr.attr,
469 &iio_dev_attr_accel_xpeak.dev_attr.attr,
470 &iio_dev_attr_accel_ypeak.dev_attr.attr,
471 &iio_dev_attr_accel_xypeak.dev_attr.attr,
bb6f19ea
BS
472 &iio_const_attr_accel_xy_scale.dev_attr.attr,
473 NULL
474};
475
476static const struct attribute_group adis16204_attribute_group = {
477 .attrs = adis16204_attributes,
478};
479
6fe8135f
JC
480static const struct iio_info adis16204_info = {
481 .attrs = &adis16204_attribute_group,
482 .read_raw = &adis16204_read_raw,
483 .write_raw = &adis16204_write_raw,
484 .driver_module = THIS_MODULE,
485};
486
bb6f19ea
BS
487static int __devinit adis16204_probe(struct spi_device *spi)
488{
489 int ret, regdone = 0;
4de66bbb
JC
490 struct adis16204_state *st;
491 struct iio_dev *indio_dev;
bb6f19ea 492
4de66bbb
JC
493 /* setup the industrialio driver allocated elements */
494 indio_dev = iio_allocate_device(sizeof(*st));
495 if (indio_dev == NULL) {
bb6f19ea 496 ret = -ENOMEM;
4de66bbb 497 goto error_ret;
bb6f19ea 498 }
4de66bbb
JC
499 st = iio_priv(indio_dev);
500 /* this is only used for removal purposes */
501 spi_set_drvdata(spi, indio_dev);
bb6f19ea
BS
502 st->us = spi;
503 mutex_init(&st->buf_lock);
bb6f19ea 504
4de66bbb
JC
505 indio_dev->name = spi->dev.driver->name;
506 indio_dev->dev.parent = &spi->dev;
507 indio_dev->info = &adis16204_info;
508 indio_dev->channels = adis16204_channels;
509 indio_dev->num_channels = ARRAY_SIZE(adis16204_channels);
510 indio_dev->modes = INDIO_DIRECT_MODE;
bb6f19ea 511
4de66bbb 512 ret = adis16204_configure_ring(indio_dev);
bb6f19ea
BS
513 if (ret)
514 goto error_free_dev;
515
4de66bbb 516 ret = iio_device_register(indio_dev);
bb6f19ea
BS
517 if (ret)
518 goto error_unreg_ring_funcs;
519 regdone = 1;
520
4de66bbb 521 ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
ad3eb9ab
JC
522 adis16204_channels,
523 ARRAY_SIZE(adis16204_channels));
bb6f19ea
BS
524 if (ret) {
525 printk(KERN_ERR "failed to initialize the ring\n");
526 goto error_unreg_ring_funcs;
527 }
528
529 if (spi->irq) {
4de66bbb 530 ret = adis16204_probe_trigger(indio_dev);
bb6f19ea 531 if (ret)
37f9d271 532 goto error_uninitialize_ring;
bb6f19ea
BS
533 }
534
535 /* Get the device into a sane initial state */
4de66bbb 536 ret = adis16204_initial_setup(indio_dev);
bb6f19ea
BS
537 if (ret)
538 goto error_remove_trigger;
539 return 0;
540
541error_remove_trigger:
4de66bbb 542 adis16204_remove_trigger(indio_dev);
bb6f19ea 543error_uninitialize_ring:
4de66bbb 544 iio_ring_buffer_unregister(indio_dev->ring);
bb6f19ea 545error_unreg_ring_funcs:
4de66bbb 546 adis16204_unconfigure_ring(indio_dev);
bb6f19ea
BS
547error_free_dev:
548 if (regdone)
4de66bbb 549 iio_device_unregister(indio_dev);
bb6f19ea 550 else
4de66bbb 551 iio_free_device(indio_dev);
bb6f19ea
BS
552error_ret:
553 return ret;
554}
555
556static int adis16204_remove(struct spi_device *spi)
557{
4de66bbb 558 struct iio_dev *indio_dev = spi_get_drvdata(spi);
bb6f19ea 559
bb6f19ea 560 adis16204_remove_trigger(indio_dev);
4de66bbb 561 iio_ring_buffer_unregister(indio_dev->ring);
bb6f19ea
BS
562 iio_device_unregister(indio_dev);
563 adis16204_unconfigure_ring(indio_dev);
bb6f19ea
BS
564
565 return 0;
566}
567
568static struct spi_driver adis16204_driver = {
569 .driver = {
570 .name = "adis16204",
571 .owner = THIS_MODULE,
572 },
573 .probe = adis16204_probe,
574 .remove = __devexit_p(adis16204_remove),
575};
576
577static __init int adis16204_init(void)
578{
579 return spi_register_driver(&adis16204_driver);
580}
581module_init(adis16204_init);
582
583static __exit void adis16204_exit(void)
584{
585 spi_unregister_driver(&adis16204_driver);
586}
587module_exit(adis16204_exit);
588
589MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
ad3eb9ab 590MODULE_DESCRIPTION("ADIS16204 High-g Digital Impact Sensor and Recorder");
bb6f19ea 591MODULE_LICENSE("GPL v2");