]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/iio/accel/adis16240_core.c
Merge commit 'v3.1-rc1' into imx-fixes
[mirror_ubuntu-bionic-kernel.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
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "../ring_generic.h"
24 #include "accel.h"
25 #include "../adc/adc.h"
26
27 #include "adis16240.h"
28
29 #define DRIVER_NAME "adis16240"
30
31 static int adis16240_check_status(struct iio_dev *indio_dev);
32
33 /**
34 * adis16240_spi_write_reg_8() - write single byte to a register
35 * @indio_dev: iio_dev associated with device
36 * @reg_address: the address of the register to be written
37 * @val: the value to write
38 **/
39 static int adis16240_spi_write_reg_8(struct iio_dev *indio_dev,
40 u8 reg_address,
41 u8 val)
42 {
43 int ret;
44 struct adis16240_state *st = iio_priv(indio_dev);
45
46 mutex_lock(&st->buf_lock);
47 st->tx[0] = ADIS16240_WRITE_REG(reg_address);
48 st->tx[1] = val;
49
50 ret = spi_write(st->us, st->tx, 2);
51 mutex_unlock(&st->buf_lock);
52
53 return ret;
54 }
55
56 /**
57 * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers
58 * @indio_dev: iio_dev for this device
59 * @reg_address: the address of the lower of the two registers. Second register
60 * is assumed to have address one greater.
61 * @val: value to be written
62 **/
63 static int adis16240_spi_write_reg_16(struct iio_dev *indio_dev,
64 u8 lower_reg_address,
65 u16 value)
66 {
67 int ret;
68 struct spi_message msg;
69 struct adis16240_state *st = iio_priv(indio_dev);
70 struct spi_transfer xfers[] = {
71 {
72 .tx_buf = st->tx,
73 .bits_per_word = 8,
74 .len = 2,
75 .cs_change = 1,
76 .delay_usecs = 35,
77 }, {
78 .tx_buf = st->tx + 2,
79 .bits_per_word = 8,
80 .len = 2,
81 .delay_usecs = 35,
82 },
83 };
84
85 mutex_lock(&st->buf_lock);
86 st->tx[0] = ADIS16240_WRITE_REG(lower_reg_address);
87 st->tx[1] = value & 0xFF;
88 st->tx[2] = ADIS16240_WRITE_REG(lower_reg_address + 1);
89 st->tx[3] = (value >> 8) & 0xFF;
90
91 spi_message_init(&msg);
92 spi_message_add_tail(&xfers[0], &msg);
93 spi_message_add_tail(&xfers[1], &msg);
94 ret = spi_sync(st->us, &msg);
95 mutex_unlock(&st->buf_lock);
96
97 return ret;
98 }
99
100 /**
101 * adis16240_spi_read_reg_16() - read 2 bytes from a 16-bit register
102 * @indio_dev: iio_dev for this device
103 * @reg_address: the address of the lower of the two registers. Second register
104 * is assumed to have address one greater.
105 * @val: somewhere to pass back the value read
106 **/
107 static int adis16240_spi_read_reg_16(struct iio_dev *indio_dev,
108 u8 lower_reg_address,
109 u16 *val)
110 {
111 struct spi_message msg;
112 struct adis16240_state *st = iio_priv(indio_dev);
113 int ret;
114 struct spi_transfer xfers[] = {
115 {
116 .tx_buf = st->tx,
117 .bits_per_word = 8,
118 .len = 2,
119 .cs_change = 1,
120 .delay_usecs = 35,
121 }, {
122 .rx_buf = st->rx,
123 .bits_per_word = 8,
124 .len = 2,
125 .cs_change = 1,
126 .delay_usecs = 35,
127 },
128 };
129
130 mutex_lock(&st->buf_lock);
131 st->tx[0] = ADIS16240_READ_REG(lower_reg_address);
132 st->tx[1] = 0;
133 st->tx[2] = 0;
134 st->tx[3] = 0;
135
136 spi_message_init(&msg);
137 spi_message_add_tail(&xfers[0], &msg);
138 spi_message_add_tail(&xfers[1], &msg);
139 ret = spi_sync(st->us, &msg);
140 if (ret) {
141 dev_err(&st->us->dev,
142 "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
148 error_ret:
149 mutex_unlock(&st->buf_lock);
150 return ret;
151 }
152
153 static ssize_t adis16240_spi_read_signed(struct device *dev,
154 struct device_attribute *attr,
155 char *buf,
156 unsigned bits)
157 {
158 struct iio_dev *indio_dev = dev_get_drvdata(dev);
159 int ret;
160 s16 val = 0;
161 unsigned shift = 16 - bits;
162 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
163
164 ret = adis16240_spi_read_reg_16(indio_dev,
165 this_attr->address, (u16 *)&val);
166 if (ret)
167 return ret;
168
169 if (val & ADIS16240_ERROR_ACTIVE)
170 adis16240_check_status(indio_dev);
171
172 val = ((s16)(val << shift) >> shift);
173 return sprintf(buf, "%d\n", val);
174 }
175
176 static ssize_t adis16240_read_12bit_signed(struct device *dev,
177 struct device_attribute *attr,
178 char *buf)
179 {
180 ssize_t ret;
181 struct iio_dev *indio_dev = dev_get_drvdata(dev);
182
183 /* Take the iio_dev status lock */
184 mutex_lock(&indio_dev->mlock);
185 ret = adis16240_spi_read_signed(dev, attr, buf, 12);
186 mutex_unlock(&indio_dev->mlock);
187
188 return ret;
189 }
190
191 static int adis16240_reset(struct iio_dev *indio_dev)
192 {
193 int ret;
194 ret = adis16240_spi_write_reg_8(indio_dev,
195 ADIS16240_GLOB_CMD,
196 ADIS16240_GLOB_CMD_SW_RESET);
197 if (ret)
198 dev_err(&indio_dev->dev, "problem resetting device");
199
200 return ret;
201 }
202
203 static ssize_t adis16240_write_reset(struct device *dev,
204 struct device_attribute *attr,
205 const char *buf, size_t len)
206 {
207 struct iio_dev *indio_dev = dev_get_drvdata(dev);
208
209 if (len < 1)
210 return -EINVAL;
211 switch (buf[0]) {
212 case '1':
213 case 'y':
214 case 'Y':
215 return adis16240_reset(indio_dev);
216 }
217 return -EINVAL;
218 }
219
220 int adis16240_set_irq(struct iio_dev *indio_dev, bool enable)
221 {
222 int ret = 0;
223 u16 msc;
224
225 ret = adis16240_spi_read_reg_16(indio_dev,
226 ADIS16240_MSC_CTRL, &msc);
227 if (ret)
228 goto error_ret;
229
230 msc |= ADIS16240_MSC_CTRL_ACTIVE_HIGH;
231 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2;
232 if (enable)
233 msc |= ADIS16240_MSC_CTRL_DATA_RDY_EN;
234 else
235 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_EN;
236
237 ret = adis16240_spi_write_reg_16(indio_dev,
238 ADIS16240_MSC_CTRL, msc);
239
240 error_ret:
241 return ret;
242 }
243
244 static int adis16240_self_test(struct iio_dev *indio_dev)
245 {
246 int ret;
247 ret = adis16240_spi_write_reg_16(indio_dev,
248 ADIS16240_MSC_CTRL,
249 ADIS16240_MSC_CTRL_SELF_TEST_EN);
250 if (ret) {
251 dev_err(&indio_dev->dev, "problem starting self test");
252 goto err_ret;
253 }
254
255 msleep(ADIS16240_STARTUP_DELAY);
256
257 adis16240_check_status(indio_dev);
258
259 err_ret:
260 return ret;
261 }
262
263 static int adis16240_check_status(struct iio_dev *indio_dev)
264 {
265 u16 status;
266 int ret;
267 struct device *dev = &indio_dev->dev;
268
269 ret = adis16240_spi_read_reg_16(indio_dev,
270 ADIS16240_DIAG_STAT, &status);
271
272 if (ret < 0) {
273 dev_err(dev, "Reading status failed\n");
274 goto error_ret;
275 }
276
277 ret = status & 0x2F;
278 if (status & ADIS16240_DIAG_STAT_PWRON_FAIL)
279 dev_err(dev, "Power-on, self-test fail\n");
280 if (status & ADIS16240_DIAG_STAT_SPI_FAIL)
281 dev_err(dev, "SPI failure\n");
282 if (status & ADIS16240_DIAG_STAT_FLASH_UPT)
283 dev_err(dev, "Flash update failed\n");
284 if (status & ADIS16240_DIAG_STAT_POWER_HIGH)
285 dev_err(dev, "Power supply above 3.625V\n");
286 if (status & ADIS16240_DIAG_STAT_POWER_LOW)
287 dev_err(dev, "Power supply below 2.225V\n");
288
289 error_ret:
290 return ret;
291 }
292
293 static int adis16240_initial_setup(struct iio_dev *indio_dev)
294 {
295 int ret;
296 struct device *dev = &indio_dev->dev;
297
298 /* Disable IRQ */
299 ret = adis16240_set_irq(indio_dev, false);
300 if (ret) {
301 dev_err(dev, "disable irq failed");
302 goto err_ret;
303 }
304
305 /* Do self test */
306 ret = adis16240_self_test(indio_dev);
307 if (ret) {
308 dev_err(dev, "self test failure");
309 goto err_ret;
310 }
311
312 /* Read status register to check the result */
313 ret = adis16240_check_status(indio_dev);
314 if (ret) {
315 adis16240_reset(indio_dev);
316 dev_err(dev, "device not playing ball -> reset");
317 msleep(ADIS16240_STARTUP_DELAY);
318 ret = adis16240_check_status(indio_dev);
319 if (ret) {
320 dev_err(dev, "giving up");
321 goto err_ret;
322 }
323 }
324
325 err_ret:
326 return ret;
327 }
328
329 static IIO_DEVICE_ATTR(accel_xyz_squared_peak_raw, S_IRUGO,
330 adis16240_read_12bit_signed, NULL,
331 ADIS16240_XYZPEAK_OUT);
332
333 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16240_write_reset, 0);
334
335 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
336
337 enum adis16240_chan {
338 in_supply,
339 in_aux,
340 accel_x,
341 accel_y,
342 accel_z,
343 temp,
344 };
345
346 static const u8 adis16240_addresses[6][3] = {
347 [in_supply] = { ADIS16240_SUPPLY_OUT },
348 [in_aux] = { ADIS16240_AUX_ADC },
349 [accel_x] = { ADIS16240_XACCL_OUT, ADIS16240_XACCL_OFF,
350 ADIS16240_XPEAK_OUT },
351 [accel_y] = { ADIS16240_YACCL_OUT, ADIS16240_YACCL_OFF,
352 ADIS16240_YPEAK_OUT },
353 [accel_z] = { ADIS16240_ZACCL_OUT, ADIS16240_ZACCL_OFF,
354 ADIS16240_ZPEAK_OUT },
355 [temp] = { ADIS16240_TEMP_OUT },
356 };
357
358 static int adis16240_read_raw(struct iio_dev *indio_dev,
359 struct iio_chan_spec const *chan,
360 int *val, int *val2,
361 long mask)
362 {
363 int ret;
364 int bits;
365 u8 addr;
366 s16 val16;
367
368 switch (mask) {
369 case 0:
370 mutex_lock(&indio_dev->mlock);
371 addr = adis16240_addresses[chan->address][0];
372 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
373 if (ret)
374 return ret;
375
376 if (val16 & ADIS16240_ERROR_ACTIVE) {
377 ret = adis16240_check_status(indio_dev);
378 if (ret)
379 return ret;
380 }
381 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
382 if (chan->scan_type.sign == 's')
383 val16 = (s16)(val16 <<
384 (16 - chan->scan_type.realbits)) >>
385 (16 - chan->scan_type.realbits);
386 *val = val16;
387 mutex_unlock(&indio_dev->mlock);
388 return IIO_VAL_INT;
389 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
390 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
391 switch (chan->type) {
392 case IIO_IN:
393 *val = 0;
394 if (chan->channel == 0)
395 *val2 = 4880;
396 else
397 return -EINVAL;
398 return IIO_VAL_INT_PLUS_MICRO;
399 case IIO_TEMP:
400 *val = 0;
401 *val2 = 244000;
402 return IIO_VAL_INT_PLUS_MICRO;
403 case IIO_ACCEL:
404 *val = 0;
405 *val2 = 504062;
406 return IIO_VAL_INT_PLUS_MICRO;
407 default:
408 return -EINVAL;
409 }
410 break;
411 case (1 << IIO_CHAN_INFO_PEAK_SCALE_SHARED):
412 *val = 6;
413 *val2 = 629295;
414 return IIO_VAL_INT_PLUS_MICRO;
415 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
416 *val = 25;
417 return IIO_VAL_INT;
418 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
419 bits = 10;
420 mutex_lock(&indio_dev->mlock);
421 addr = adis16240_addresses[chan->address][1];
422 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
423 if (ret) {
424 mutex_unlock(&indio_dev->mlock);
425 return ret;
426 }
427 val16 &= (1 << bits) - 1;
428 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
429 *val = val16;
430 mutex_unlock(&indio_dev->mlock);
431 return IIO_VAL_INT;
432 case (1 << IIO_CHAN_INFO_PEAK_SEPARATE):
433 bits = 10;
434 mutex_lock(&indio_dev->mlock);
435 addr = adis16240_addresses[chan->address][2];
436 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
437 if (ret) {
438 mutex_unlock(&indio_dev->mlock);
439 return ret;
440 }
441 val16 &= (1 << bits) - 1;
442 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
443 *val = val16;
444 mutex_unlock(&indio_dev->mlock);
445 return IIO_VAL_INT;
446 }
447 return -EINVAL;
448 }
449
450 static int adis16240_write_raw(struct iio_dev *indio_dev,
451 struct iio_chan_spec const *chan,
452 int val,
453 int val2,
454 long mask)
455 {
456 int bits = 10;
457 s16 val16;
458 u8 addr;
459 switch (mask) {
460 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
461 val16 = val & ((1 << bits) - 1);
462 addr = adis16240_addresses[chan->address][1];
463 return adis16240_spi_write_reg_16(indio_dev, addr, val16);
464 }
465 return -EINVAL;
466 }
467
468 static struct iio_chan_spec adis16240_channels[] = {
469 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0,
470 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
471 in_supply, ADIS16240_SCAN_SUPPLY,
472 IIO_ST('u', 10, 16, 0), 0),
473 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
474 0,
475 in_aux, ADIS16240_SCAN_AUX_ADC,
476 IIO_ST('u', 10, 16, 0), 0),
477 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
478 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
479 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
480 accel_x, ADIS16240_SCAN_ACC_X,
481 IIO_ST('s', 10, 16, 0), 0),
482 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
483 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
484 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
485 accel_y, ADIS16240_SCAN_ACC_Y,
486 IIO_ST('s', 10, 16, 0), 0),
487 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Z,
488 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
489 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
490 accel_z, ADIS16240_SCAN_ACC_Z,
491 IIO_ST('s', 10, 16, 0), 0),
492 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
493 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
494 temp, ADIS16240_SCAN_TEMP,
495 IIO_ST('u', 10, 16, 0), 0),
496 IIO_CHAN_SOFT_TIMESTAMP(6)
497 };
498
499 static struct attribute *adis16240_attributes[] = {
500 &iio_dev_attr_accel_xyz_squared_peak_raw.dev_attr.attr,
501 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
502 &iio_dev_attr_reset.dev_attr.attr,
503 NULL
504 };
505
506 static const struct attribute_group adis16240_attribute_group = {
507 .attrs = adis16240_attributes,
508 };
509
510 static const struct iio_info adis16240_info = {
511 .attrs = &adis16240_attribute_group,
512 .read_raw = &adis16240_read_raw,
513 .write_raw = &adis16240_write_raw,
514 .driver_module = THIS_MODULE,
515 };
516
517 static int __devinit adis16240_probe(struct spi_device *spi)
518 {
519 int ret, regdone = 0;
520 struct adis16240_state *st;
521 struct iio_dev *indio_dev;
522
523 /* setup the industrialio driver allocated elements */
524 indio_dev = iio_allocate_device(sizeof(*st));
525 if (indio_dev == NULL) {
526 ret = -ENOMEM;
527 goto error_ret;
528 }
529 st = iio_priv(indio_dev);
530 /* this is only used for removal purposes */
531 spi_set_drvdata(spi, indio_dev);
532
533 st->us = spi;
534 mutex_init(&st->buf_lock);
535
536 indio_dev->name = spi->dev.driver->name;
537 indio_dev->dev.parent = &spi->dev;
538 indio_dev->info = &adis16240_info;
539 indio_dev->channels = adis16240_channels;
540 indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
541 indio_dev->modes = INDIO_DIRECT_MODE;
542
543 ret = adis16240_configure_ring(indio_dev);
544 if (ret)
545 goto error_free_dev;
546
547 ret = iio_device_register(indio_dev);
548 if (ret)
549 goto error_unreg_ring_funcs;
550 regdone = 1;
551
552 ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
553 adis16240_channels,
554 ARRAY_SIZE(adis16240_channels));
555 if (ret) {
556 printk(KERN_ERR "failed to initialize the ring\n");
557 goto error_unreg_ring_funcs;
558 }
559
560 if (spi->irq) {
561 ret = adis16240_probe_trigger(indio_dev);
562 if (ret)
563 goto error_uninitialize_ring;
564 }
565
566 /* Get the device into a sane initial state */
567 ret = adis16240_initial_setup(indio_dev);
568 if (ret)
569 goto error_remove_trigger;
570 return 0;
571
572 error_remove_trigger:
573 adis16240_remove_trigger(indio_dev);
574 error_uninitialize_ring:
575 iio_ring_buffer_unregister(indio_dev->ring);
576 error_unreg_ring_funcs:
577 adis16240_unconfigure_ring(indio_dev);
578 error_free_dev:
579 if (regdone)
580 iio_device_unregister(indio_dev);
581 else
582 iio_free_device(indio_dev);
583 error_ret:
584 return ret;
585 }
586
587 static int adis16240_remove(struct spi_device *spi)
588 {
589
590 struct iio_dev *indio_dev = spi_get_drvdata(spi);
591
592 flush_scheduled_work();
593
594 adis16240_remove_trigger(indio_dev);
595 iio_ring_buffer_unregister(indio_dev->ring);
596 iio_device_unregister(indio_dev);
597 adis16240_unconfigure_ring(indio_dev);
598
599 return 0;
600 }
601
602 static struct spi_driver adis16240_driver = {
603 .driver = {
604 .name = "adis16240",
605 .owner = THIS_MODULE,
606 },
607 .probe = adis16240_probe,
608 .remove = __devexit_p(adis16240_remove),
609 };
610
611 static __init int adis16240_init(void)
612 {
613 return spi_register_driver(&adis16240_driver);
614 }
615 module_init(adis16240_init);
616
617 static __exit void adis16240_exit(void)
618 {
619 spi_unregister_driver(&adis16240_driver);
620 }
621 module_exit(adis16240_exit);
622
623 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
624 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
625 MODULE_LICENSE("GPL v2");