]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/iio/accel/adis16201_core.c
Merge tag 'mmc-fixes-for-3.7-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / iio / accel / adis16201_core.c
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/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/module.h>
17
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21
22 #include "adis16201.h"
23
24 enum adis16201_chan {
25 in_supply,
26 temp,
27 accel_x,
28 accel_y,
29 incli_x,
30 incli_y,
31 in_aux,
32 };
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 **/
40 static int adis16201_spi_write_reg_8(struct iio_dev *indio_dev,
41 u8 reg_address,
42 u8 val)
43 {
44 int ret;
45 struct adis16201_state *st = iio_priv(indio_dev);
46
47 mutex_lock(&st->buf_lock);
48 st->tx[0] = ADIS16201_WRITE_REG(reg_address);
49 st->tx[1] = val;
50
51 ret = spi_write(st->us, st->tx, 2);
52 mutex_unlock(&st->buf_lock);
53
54 return ret;
55 }
56
57 /**
58 * adis16201_spi_write_reg_16() - write 2 bytes to a pair of registers
59 * @indio_dev: iio device associated with child of actual device
60 * @reg_address: the address of the lower of the two registers. Second register
61 * is assumed to have address one greater.
62 * @val: value to be written
63 **/
64 static int adis16201_spi_write_reg_16(struct iio_dev *indio_dev,
65 u8 lower_reg_address,
66 u16 value)
67 {
68 int ret;
69 struct spi_message msg;
70 struct adis16201_state *st = iio_priv(indio_dev);
71 struct spi_transfer xfers[] = {
72 {
73 .tx_buf = st->tx,
74 .bits_per_word = 8,
75 .len = 2,
76 .cs_change = 1,
77 }, {
78 .tx_buf = st->tx + 2,
79 .bits_per_word = 8,
80 .len = 2,
81 },
82 };
83
84 mutex_lock(&st->buf_lock);
85 st->tx[0] = ADIS16201_WRITE_REG(lower_reg_address);
86 st->tx[1] = value & 0xFF;
87 st->tx[2] = ADIS16201_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 * adis16201_spi_read_reg_16() - read 2 bytes from a 16-bit register
101 * @indio_dev: iio device associated with child of actual 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 adis16201_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 adis16201_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 = 20,
120 }, {
121 .rx_buf = st->rx,
122 .bits_per_word = 8,
123 .len = 2,
124 .delay_usecs = 20,
125 },
126 };
127
128 mutex_lock(&st->buf_lock);
129 st->tx[0] = ADIS16201_READ_REG(lower_reg_address);
130 st->tx[1] = 0;
131
132 spi_message_init(&msg);
133 spi_message_add_tail(&xfers[0], &msg);
134 spi_message_add_tail(&xfers[1], &msg);
135 ret = spi_sync(st->us, &msg);
136 if (ret) {
137 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
138 lower_reg_address);
139 goto error_ret;
140 }
141 *val = (st->rx[0] << 8) | st->rx[1];
142
143 error_ret:
144 mutex_unlock(&st->buf_lock);
145 return ret;
146 }
147
148 static int adis16201_reset(struct iio_dev *indio_dev)
149 {
150 int ret;
151 struct adis16201_state *st = iio_priv(indio_dev);
152
153 ret = adis16201_spi_write_reg_8(indio_dev,
154 ADIS16201_GLOB_CMD,
155 ADIS16201_GLOB_CMD_SW_RESET);
156 if (ret)
157 dev_err(&st->us->dev, "problem resetting device");
158
159 return ret;
160 }
161
162 int adis16201_set_irq(struct iio_dev *indio_dev, bool enable)
163 {
164 int ret = 0;
165 u16 msc;
166
167 ret = adis16201_spi_read_reg_16(indio_dev, ADIS16201_MSC_CTRL, &msc);
168 if (ret)
169 goto error_ret;
170
171 msc |= ADIS16201_MSC_CTRL_ACTIVE_HIGH;
172 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_DIO1;
173 if (enable)
174 msc |= ADIS16201_MSC_CTRL_DATA_RDY_EN;
175 else
176 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_EN;
177
178 ret = adis16201_spi_write_reg_16(indio_dev, ADIS16201_MSC_CTRL, msc);
179
180 error_ret:
181 return ret;
182 }
183
184 static int adis16201_check_status(struct iio_dev *indio_dev)
185 {
186 u16 status;
187 int ret;
188
189 ret = adis16201_spi_read_reg_16(indio_dev,
190 ADIS16201_DIAG_STAT, &status);
191 if (ret < 0) {
192 dev_err(&indio_dev->dev, "Reading status failed\n");
193 goto error_ret;
194 }
195 ret = status & 0xF;
196 if (ret)
197 ret = -EFAULT;
198
199 if (status & ADIS16201_DIAG_STAT_SPI_FAIL)
200 dev_err(&indio_dev->dev, "SPI failure\n");
201 if (status & ADIS16201_DIAG_STAT_FLASH_UPT)
202 dev_err(&indio_dev->dev, "Flash update failed\n");
203 if (status & ADIS16201_DIAG_STAT_POWER_HIGH)
204 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
205 if (status & ADIS16201_DIAG_STAT_POWER_LOW)
206 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
207
208 error_ret:
209 return ret;
210 }
211
212 static int adis16201_self_test(struct iio_dev *indio_dev)
213 {
214 int ret;
215 ret = adis16201_spi_write_reg_16(indio_dev,
216 ADIS16201_MSC_CTRL,
217 ADIS16201_MSC_CTRL_SELF_TEST_EN);
218 if (ret) {
219 dev_err(&indio_dev->dev, "problem starting self test");
220 goto err_ret;
221 }
222
223 ret = adis16201_check_status(indio_dev);
224
225 err_ret:
226 return ret;
227 }
228
229 static int adis16201_initial_setup(struct iio_dev *indio_dev)
230 {
231 int ret;
232 struct device *dev = &indio_dev->dev;
233
234 /* Disable IRQ */
235 ret = adis16201_set_irq(indio_dev, false);
236 if (ret) {
237 dev_err(dev, "disable irq failed");
238 goto err_ret;
239 }
240
241 /* Do self test */
242 ret = adis16201_self_test(indio_dev);
243 if (ret) {
244 dev_err(dev, "self test failure");
245 goto err_ret;
246 }
247
248 /* Read status register to check the result */
249 ret = adis16201_check_status(indio_dev);
250 if (ret) {
251 adis16201_reset(indio_dev);
252 dev_err(dev, "device not playing ball -> reset");
253 msleep(ADIS16201_STARTUP_DELAY);
254 ret = adis16201_check_status(indio_dev);
255 if (ret) {
256 dev_err(dev, "giving up");
257 goto err_ret;
258 }
259 }
260
261 err_ret:
262 return ret;
263 }
264
265 static u8 adis16201_addresses[7][2] = {
266 [in_supply] = { ADIS16201_SUPPLY_OUT, },
267 [temp] = { ADIS16201_TEMP_OUT },
268 [accel_x] = { ADIS16201_XACCL_OUT, ADIS16201_XACCL_OFFS },
269 [accel_y] = { ADIS16201_YACCL_OUT, ADIS16201_YACCL_OFFS },
270 [in_aux] = { ADIS16201_AUX_ADC },
271 [incli_x] = { ADIS16201_XINCL_OUT },
272 [incli_y] = { ADIS16201_YINCL_OUT },
273 };
274
275 static int adis16201_read_raw(struct iio_dev *indio_dev,
276 struct iio_chan_spec const *chan,
277 int *val, int *val2,
278 long mask)
279 {
280 int ret;
281 int bits;
282 u8 addr;
283 s16 val16;
284
285 switch (mask) {
286 case IIO_CHAN_INFO_RAW:
287 mutex_lock(&indio_dev->mlock);
288 addr = adis16201_addresses[chan->address][0];
289 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
290 if (ret) {
291 mutex_unlock(&indio_dev->mlock);
292 return ret;
293 }
294
295 if (val16 & ADIS16201_ERROR_ACTIVE) {
296 ret = adis16201_check_status(indio_dev);
297 if (ret) {
298 mutex_unlock(&indio_dev->mlock);
299 return ret;
300 }
301 }
302 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
303 if (chan->scan_type.sign == 's')
304 val16 = (s16)(val16 <<
305 (16 - chan->scan_type.realbits)) >>
306 (16 - chan->scan_type.realbits);
307 *val = val16;
308 mutex_unlock(&indio_dev->mlock);
309 return IIO_VAL_INT;
310 case IIO_CHAN_INFO_SCALE:
311 switch (chan->type) {
312 case IIO_VOLTAGE:
313 if (chan->channel == 0) {
314 *val = 1;
315 *val2 = 220000; /* 1.22 mV */
316 } else {
317 *val = 0;
318 *val2 = 610000; /* 0.610 mV */
319 }
320 return IIO_VAL_INT_PLUS_MICRO;
321 case IIO_TEMP:
322 *val = -470; /* 0.47 C */
323 *val2 = 0;
324 return IIO_VAL_INT_PLUS_MICRO;
325 case IIO_ACCEL:
326 *val = 0;
327 *val2 = IIO_G_TO_M_S_2(462400); /* 0.4624 mg */
328 return IIO_VAL_INT_PLUS_NANO;
329 case IIO_INCLI:
330 *val = 0;
331 *val2 = 100000; /* 0.1 degree */
332 return IIO_VAL_INT_PLUS_MICRO;
333 default:
334 return -EINVAL;
335 }
336 break;
337 case IIO_CHAN_INFO_OFFSET:
338 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
339 return IIO_VAL_INT;
340 case IIO_CHAN_INFO_CALIBBIAS:
341 switch (chan->type) {
342 case IIO_ACCEL:
343 bits = 12;
344 break;
345 case IIO_INCLI:
346 bits = 9;
347 break;
348 default:
349 return -EINVAL;
350 };
351 mutex_lock(&indio_dev->mlock);
352 addr = adis16201_addresses[chan->address][1];
353 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
354 if (ret) {
355 mutex_unlock(&indio_dev->mlock);
356 return ret;
357 }
358 val16 &= (1 << bits) - 1;
359 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
360 *val = val16;
361 mutex_unlock(&indio_dev->mlock);
362 return IIO_VAL_INT;
363 }
364 return -EINVAL;
365 }
366
367 static int adis16201_write_raw(struct iio_dev *indio_dev,
368 struct iio_chan_spec const *chan,
369 int val,
370 int val2,
371 long mask)
372 {
373 int bits;
374 s16 val16;
375 u8 addr;
376 switch (mask) {
377 case IIO_CHAN_INFO_CALIBBIAS:
378 switch (chan->type) {
379 case IIO_ACCEL:
380 bits = 12;
381 break;
382 case IIO_INCLI:
383 bits = 9;
384 break;
385 default:
386 return -EINVAL;
387 };
388 val16 = val & ((1 << bits) - 1);
389 addr = adis16201_addresses[chan->address][1];
390 return adis16201_spi_write_reg_16(indio_dev, addr, val16);
391 }
392 return -EINVAL;
393 }
394
395 static const struct iio_chan_spec adis16201_channels[] = {
396 {
397 .type = IIO_VOLTAGE,
398 .indexed = 1,
399 .channel = 0,
400 .extend_name = "supply",
401 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
402 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
403 .address = in_supply,
404 .scan_index = ADIS16201_SCAN_SUPPLY,
405 .scan_type = {
406 .sign = 'u',
407 .realbits = 12,
408 .storagebits = 16,
409 },
410 }, {
411 .type = IIO_TEMP,
412 .indexed = 1,
413 .channel = 0,
414 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
415 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
416 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
417 .address = temp,
418 .scan_index = ADIS16201_SCAN_TEMP,
419 .scan_type = {
420 .sign = 'u',
421 .realbits = 12,
422 .storagebits = 16,
423 },
424 }, {
425 .type = IIO_ACCEL,
426 .modified = 1,
427 .channel2 = IIO_MOD_X,
428 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
429 IIO_CHAN_INFO_SCALE_SHARED_BIT |
430 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
431 .address = accel_x,
432 .scan_index = ADIS16201_SCAN_ACC_X,
433 .scan_type = {
434 .sign = 's',
435 .realbits = 14,
436 .storagebits = 16,
437 },
438 }, {
439 .type = IIO_ACCEL,
440 .modified = 1,
441 .channel2 = IIO_MOD_Y,
442 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
443 IIO_CHAN_INFO_SCALE_SHARED_BIT |
444 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
445 .address = accel_y,
446 .scan_index = ADIS16201_SCAN_ACC_Y,
447 .scan_type = {
448 .sign = 's',
449 .realbits = 14,
450 .storagebits = 16,
451 },
452 }, {
453 .type = IIO_VOLTAGE,
454 .indexed = 1,
455 .channel = 1,
456 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
457 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
458 .address = in_aux,
459 .scan_index = ADIS16201_SCAN_AUX_ADC,
460 .scan_type = {
461 .sign = 'u',
462 .realbits = 12,
463 .storagebits = 16,
464 },
465 }, {
466 .type = IIO_INCLI,
467 .modified = 1,
468 .channel2 = IIO_MOD_X,
469 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
470 IIO_CHAN_INFO_SCALE_SHARED_BIT |
471 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
472 .address = incli_x,
473 .scan_index = ADIS16201_SCAN_INCLI_X,
474 .scan_type = {
475 .sign = 's',
476 .realbits = 14,
477 .storagebits = 16,
478 },
479 }, {
480 .type = IIO_INCLI,
481 .modified = 1,
482 .channel2 = IIO_MOD_Y,
483 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
484 IIO_CHAN_INFO_SCALE_SHARED_BIT |
485 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
486 .address = incli_y,
487 .scan_index = ADIS16201_SCAN_INCLI_Y,
488 .scan_type = {
489 .sign = 's',
490 .realbits = 14,
491 .storagebits = 16,
492 },
493 },
494 IIO_CHAN_SOFT_TIMESTAMP(7)
495 };
496
497 static const struct iio_info adis16201_info = {
498 .read_raw = &adis16201_read_raw,
499 .write_raw = &adis16201_write_raw,
500 .driver_module = THIS_MODULE,
501 };
502
503 static int __devinit adis16201_probe(struct spi_device *spi)
504 {
505 int ret;
506 struct adis16201_state *st;
507 struct iio_dev *indio_dev;
508
509 /* setup the industrialio driver allocated elements */
510 indio_dev = iio_device_alloc(sizeof(*st));
511 if (indio_dev == NULL) {
512 ret = -ENOMEM;
513 goto error_ret;
514 }
515 st = iio_priv(indio_dev);
516 /* this is only used for removal purposes */
517 spi_set_drvdata(spi, indio_dev);
518
519 st->us = spi;
520 mutex_init(&st->buf_lock);
521
522 indio_dev->name = spi->dev.driver->name;
523 indio_dev->dev.parent = &spi->dev;
524 indio_dev->info = &adis16201_info;
525
526 indio_dev->channels = adis16201_channels;
527 indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
528 indio_dev->modes = INDIO_DIRECT_MODE;
529
530 ret = adis16201_configure_ring(indio_dev);
531 if (ret)
532 goto error_free_dev;
533
534 ret = iio_buffer_register(indio_dev,
535 adis16201_channels,
536 ARRAY_SIZE(adis16201_channels));
537 if (ret) {
538 printk(KERN_ERR "failed to initialize the ring\n");
539 goto error_unreg_ring_funcs;
540 }
541
542 if (spi->irq) {
543 ret = adis16201_probe_trigger(indio_dev);
544 if (ret)
545 goto error_uninitialize_ring;
546 }
547
548 /* Get the device into a sane initial state */
549 ret = adis16201_initial_setup(indio_dev);
550 if (ret)
551 goto error_remove_trigger;
552
553 ret = iio_device_register(indio_dev);
554 if (ret < 0)
555 goto error_remove_trigger;
556 return 0;
557
558 error_remove_trigger:
559 adis16201_remove_trigger(indio_dev);
560 error_uninitialize_ring:
561 iio_buffer_unregister(indio_dev);
562 error_unreg_ring_funcs:
563 adis16201_unconfigure_ring(indio_dev);
564 error_free_dev:
565 iio_device_free(indio_dev);
566 error_ret:
567 return ret;
568 }
569
570 static int __devexit adis16201_remove(struct spi_device *spi)
571 {
572 struct iio_dev *indio_dev = spi_get_drvdata(spi);
573
574 iio_device_unregister(indio_dev);
575 adis16201_remove_trigger(indio_dev);
576 iio_buffer_unregister(indio_dev);
577 adis16201_unconfigure_ring(indio_dev);
578 iio_device_free(indio_dev);
579
580 return 0;
581 }
582
583 static struct spi_driver adis16201_driver = {
584 .driver = {
585 .name = "adis16201",
586 .owner = THIS_MODULE,
587 },
588 .probe = adis16201_probe,
589 .remove = __devexit_p(adis16201_remove),
590 };
591 module_spi_driver(adis16201_driver);
592
593 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
594 MODULE_DESCRIPTION("Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver");
595 MODULE_LICENSE("GPL v2");
596 MODULE_ALIAS("spi:adis16201");