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