]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/accel/adis16220_core.c
Merge 3.12-rc6 into staging-next.
[karo-tx-linux.git] / drivers / staging / iio / accel / adis16220_core.c
1 /*
2  * ADIS16220 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
21 #include "adis16220.h"
22
23 static ssize_t adis16220_read_16bit(struct device *dev,
24                 struct device_attribute *attr,
25                 char *buf)
26 {
27         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
28         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
29         struct adis16220_state *st = iio_priv(indio_dev);
30         ssize_t ret;
31         s16 val = 0;
32
33         /* Take the iio_dev status lock */
34         mutex_lock(&indio_dev->mlock);
35         ret = adis_read_reg_16(&st->adis, this_attr->address,
36                                         (u16 *)&val);
37         mutex_unlock(&indio_dev->mlock);
38         if (ret)
39                 return ret;
40         return sprintf(buf, "%d\n", val);
41 }
42
43 static ssize_t adis16220_write_16bit(struct device *dev,
44                 struct device_attribute *attr,
45                 const char *buf,
46                 size_t len)
47 {
48         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
49         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
50         struct adis16220_state *st = iio_priv(indio_dev);
51         int ret;
52         u16 val;
53
54         ret = kstrtou16(buf, 10, &val);
55         if (ret)
56                 goto error_ret;
57         ret = adis_write_reg_16(&st->adis, this_attr->address, val);
58
59 error_ret:
60         return ret ? ret : len;
61 }
62
63 static int adis16220_capture(struct iio_dev *indio_dev)
64 {
65         struct adis16220_state *st = iio_priv(indio_dev);
66         int ret;
67
68          /* initiates a manual data capture */
69         ret = adis_write_reg_16(&st->adis, ADIS16220_GLOB_CMD, 0xBF08);
70         if (ret)
71                 dev_err(&indio_dev->dev, "problem beginning capture");
72
73         msleep(10); /* delay for capture to finish */
74
75         return ret;
76 }
77
78 static ssize_t adis16220_write_capture(struct device *dev,
79                 struct device_attribute *attr,
80                 const char *buf, size_t len)
81 {
82         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
83         bool val;
84         int ret;
85
86         ret = strtobool(buf, &val);
87         if (ret)
88                 return ret;
89         if (!val)
90                 return -EINVAL;
91         ret = adis16220_capture(indio_dev);
92         if (ret)
93                 return ret;
94
95         return len;
96 }
97
98 static ssize_t adis16220_capture_buffer_read(struct iio_dev *indio_dev,
99                                         char *buf,
100                                         loff_t off,
101                                         size_t count,
102                                         int addr)
103 {
104         struct adis16220_state *st = iio_priv(indio_dev);
105         struct spi_transfer xfers[] = {
106                 {
107                         .tx_buf = st->tx,
108                         .bits_per_word = 8,
109                         .len = 2,
110                         .cs_change = 1,
111                         .delay_usecs = 25,
112                 }, {
113                         .tx_buf = st->tx,
114                         .rx_buf = st->rx,
115                         .bits_per_word = 8,
116                         .cs_change = 1,
117                         .delay_usecs = 25,
118                 },
119         };
120         int ret;
121         int i;
122
123         if (unlikely(!count))
124                 return count;
125
126         if ((off >= ADIS16220_CAPTURE_SIZE) || (count & 1) || (off & 1))
127                 return -EINVAL;
128
129         if (off + count > ADIS16220_CAPTURE_SIZE)
130                 count = ADIS16220_CAPTURE_SIZE - off;
131
132         /* write the begin position of capture buffer */
133         ret = adis_write_reg_16(&st->adis,
134                                         ADIS16220_CAPT_PNTR,
135                                         off > 1);
136         if (ret)
137                 return -EIO;
138
139         /* read count/2 values from capture buffer */
140         mutex_lock(&st->buf_lock);
141
142
143         for (i = 0; i < count; i += 2) {
144                 st->tx[i] = ADIS_READ_REG(addr);
145                 st->tx[i + 1] = 0;
146         }
147         xfers[1].len = count;
148
149         ret = spi_sync_transfer(st->adis.spi, xfers, ARRAY_SIZE(xfers));
150         if (ret) {
151
152                 mutex_unlock(&st->buf_lock);
153                 return -EIO;
154         }
155
156         memcpy(buf, st->rx, count);
157
158         mutex_unlock(&st->buf_lock);
159         return count;
160 }
161
162 static ssize_t adis16220_accel_bin_read(struct file *filp, struct kobject *kobj,
163                                         struct bin_attribute *attr,
164                                         char *buf,
165                                         loff_t off,
166                                         size_t count)
167 {
168         struct iio_dev *indio_dev = dev_to_iio_dev(kobj_to_dev(kobj));
169
170         return adis16220_capture_buffer_read(indio_dev, buf,
171                                         off, count,
172                                         ADIS16220_CAPT_BUFA);
173 }
174
175 static struct bin_attribute accel_bin = {
176         .attr = {
177                 .name = "accel_bin",
178                 .mode = S_IRUGO,
179         },
180         .read = adis16220_accel_bin_read,
181         .size = ADIS16220_CAPTURE_SIZE,
182 };
183
184 static ssize_t adis16220_adc1_bin_read(struct file *filp, struct kobject *kobj,
185                                 struct bin_attribute *attr,
186                                 char *buf, loff_t off,
187                                 size_t count)
188 {
189         struct iio_dev *indio_dev = dev_to_iio_dev(kobj_to_dev(kobj));
190
191         return adis16220_capture_buffer_read(indio_dev, buf,
192                                         off, count,
193                                         ADIS16220_CAPT_BUF1);
194 }
195
196 static struct bin_attribute adc1_bin = {
197         .attr = {
198                 .name = "in0_bin",
199                 .mode = S_IRUGO,
200         },
201         .read =  adis16220_adc1_bin_read,
202         .size = ADIS16220_CAPTURE_SIZE,
203 };
204
205 static ssize_t adis16220_adc2_bin_read(struct file *filp, struct kobject *kobj,
206                                 struct bin_attribute *attr,
207                                 char *buf, loff_t off,
208                                 size_t count)
209 {
210         struct iio_dev *indio_dev = dev_to_iio_dev(kobj_to_dev(kobj));
211
212         return adis16220_capture_buffer_read(indio_dev, buf,
213                                         off, count,
214                                         ADIS16220_CAPT_BUF2);
215 }
216
217
218 static struct bin_attribute adc2_bin = {
219         .attr = {
220                 .name = "in1_bin",
221                 .mode = S_IRUGO,
222         },
223         .read =  adis16220_adc2_bin_read,
224         .size = ADIS16220_CAPTURE_SIZE,
225 };
226
227 #define IIO_DEV_ATTR_CAPTURE(_store)                            \
228         IIO_DEVICE_ATTR(capture, S_IWUSR, NULL, _store, 0)
229
230 static IIO_DEV_ATTR_CAPTURE(adis16220_write_capture);
231
232 #define IIO_DEV_ATTR_CAPTURE_COUNT(_mode, _show, _store, _addr)         \
233         IIO_DEVICE_ATTR(capture_count, _mode, _show, _store, _addr)
234
235 static IIO_DEV_ATTR_CAPTURE_COUNT(S_IWUSR | S_IRUGO,
236                 adis16220_read_16bit,
237                 adis16220_write_16bit,
238                 ADIS16220_CAPT_PNTR);
239
240 enum adis16220_channel {
241         in_supply, in_1, in_2, accel, temp
242 };
243
244 struct adis16220_address_spec {
245         u8 addr;
246         u8 bits;
247         bool sign;
248 };
249
250 /* Address / bits / signed */
251 static const struct adis16220_address_spec adis16220_addresses[][3] = {
252         [in_supply] =   { { ADIS16220_CAPT_SUPPLY,      12, 0 }, },
253         [in_1] =        { { ADIS16220_CAPT_BUF1,        16, 1 },
254                           { ADIS16220_AIN1_NULL,        16, 1 },
255                           { ADIS16220_CAPT_PEAK1,       16, 1 }, },
256         [in_2] =        { { ADIS16220_CAPT_BUF2,        16, 1 },
257                           { ADIS16220_AIN2_NULL,        16, 1 },
258                           { ADIS16220_CAPT_PEAK2,       16, 1 }, },
259         [accel] =       { { ADIS16220_CAPT_BUFA,        16, 1 },
260                           { ADIS16220_ACCL_NULL,        16, 1 },
261                           { ADIS16220_CAPT_PEAKA,       16, 1 }, },
262         [temp] =        { { ADIS16220_CAPT_TEMP,        12, 0 }, }
263 };
264
265 static int adis16220_read_raw(struct iio_dev *indio_dev,
266                               struct iio_chan_spec const *chan,
267                               int *val, int *val2,
268                               long mask)
269 {
270         struct adis16220_state *st = iio_priv(indio_dev);
271         const struct adis16220_address_spec *addr;
272         int ret = -EINVAL;
273         int addrind = 0;
274         u16 uval;
275         s16 sval;
276         u8 bits;
277
278         switch (mask) {
279         case IIO_CHAN_INFO_RAW:
280                 addrind = 0;
281                 break;
282         case IIO_CHAN_INFO_OFFSET:
283                 if (chan->type == IIO_TEMP) {
284                         *val = 25000 / -470 - 1278; /* 25 C = 1278 */
285                         return IIO_VAL_INT;
286                 }
287                 addrind = 1;
288                 break;
289         case IIO_CHAN_INFO_PEAK:
290                 addrind = 2;
291                 break;
292         case IIO_CHAN_INFO_SCALE:
293                 switch (chan->type) {
294                 case IIO_TEMP:
295                         *val = -470; /* -0.47 C */
296                         *val2 = 0;
297                         return IIO_VAL_INT_PLUS_MICRO;
298                 case IIO_ACCEL:
299                         *val2 = IIO_G_TO_M_S_2(19073); /* 19.073 g */
300                         return IIO_VAL_INT_PLUS_MICRO;
301                 case IIO_VOLTAGE:
302                         if (chan->channel == 0) {
303                                 *val = 1;
304                                 *val2 = 220700; /* 1.2207 mV */
305                         } else {
306                                 /* Should really be dependent on VDD */
307                                 *val2 = 305180; /* 305.18 uV */
308                         }
309                         return IIO_VAL_INT_PLUS_MICRO;
310                 default:
311                         return -EINVAL;
312                 }
313         default:
314                 return -EINVAL;
315         }
316         addr = &adis16220_addresses[chan->address][addrind];
317         if (addr->sign) {
318                 ret = adis_read_reg_16(&st->adis, addr->addr, &sval);
319                 if (ret)
320                         return ret;
321                 bits = addr->bits;
322                 sval &= (1 << bits) - 1;
323                 sval = (s16)(sval << (16 - bits)) >> (16 - bits);
324                 *val = sval;
325                 return IIO_VAL_INT;
326         } else {
327                 ret = adis_read_reg_16(&st->adis, addr->addr, &uval);
328                 if (ret)
329                         return ret;
330                 bits = addr->bits;
331                 uval &= (1 << bits) - 1;
332                 *val = uval;
333                 return IIO_VAL_INT;
334         }
335 }
336
337 static const struct iio_chan_spec adis16220_channels[] = {
338         {
339                 .type = IIO_VOLTAGE,
340                 .indexed = 1,
341                 .channel = 0,
342                 .extend_name = "supply",
343                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
344                         BIT(IIO_CHAN_INFO_SCALE),
345                 .address = in_supply,
346         }, {
347                 .type = IIO_ACCEL,
348                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
349                         BIT(IIO_CHAN_INFO_OFFSET) |
350                         BIT(IIO_CHAN_INFO_SCALE) |
351                         BIT(IIO_CHAN_INFO_PEAK),
352                 .address = accel,
353         }, {
354                 .type = IIO_TEMP,
355                 .indexed = 1,
356                 .channel = 0,
357                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
358                         BIT(IIO_CHAN_INFO_OFFSET) |
359                         BIT(IIO_CHAN_INFO_SCALE),
360                 .address = temp,
361         }, {
362                 .type = IIO_VOLTAGE,
363                 .indexed = 1,
364                 .channel = 1,
365                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
366                         BIT(IIO_CHAN_INFO_OFFSET) |
367                         BIT(IIO_CHAN_INFO_SCALE),
368                 .address = in_1,
369         }, {
370                 .type = IIO_VOLTAGE,
371                 .indexed = 1,
372                 .channel = 2,
373                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
374                 .address = in_2,
375         }
376 };
377
378 static struct attribute *adis16220_attributes[] = {
379         &iio_dev_attr_capture.dev_attr.attr,
380         &iio_dev_attr_capture_count.dev_attr.attr,
381         NULL
382 };
383
384 static const struct attribute_group adis16220_attribute_group = {
385         .attrs = adis16220_attributes,
386 };
387
388 static const struct iio_info adis16220_info = {
389         .attrs = &adis16220_attribute_group,
390         .driver_module = THIS_MODULE,
391         .read_raw = &adis16220_read_raw,
392 };
393
394 static const char * const adis16220_status_error_msgs[] = {
395         [ADIS16220_DIAG_STAT_VIOLATION_BIT] = "Capture period violation/interruption",
396         [ADIS16220_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
397         [ADIS16220_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
398         [ADIS16220_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
399         [ADIS16220_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
400 };
401
402 static const struct adis_data adis16220_data = {
403         .read_delay = 35,
404         .write_delay = 35,
405         .msc_ctrl_reg = ADIS16220_MSC_CTRL,
406         .glob_cmd_reg = ADIS16220_GLOB_CMD,
407         .diag_stat_reg = ADIS16220_DIAG_STAT,
408
409         .self_test_mask = ADIS16220_MSC_CTRL_SELF_TEST_EN,
410         .startup_delay = ADIS16220_STARTUP_DELAY,
411
412         .status_error_msgs = adis16220_status_error_msgs,
413         .status_error_mask = BIT(ADIS16220_DIAG_STAT_VIOLATION_BIT) |
414                 BIT(ADIS16220_DIAG_STAT_SPI_FAIL_BIT) |
415                 BIT(ADIS16220_DIAG_STAT_FLASH_UPT_BIT) |
416                 BIT(ADIS16220_DIAG_STAT_POWER_HIGH_BIT) |
417                 BIT(ADIS16220_DIAG_STAT_POWER_LOW_BIT),
418 };
419
420 static int adis16220_probe(struct spi_device *spi)
421 {
422         int ret;
423         struct adis16220_state *st;
424         struct iio_dev *indio_dev;
425
426         /* setup the industrialio driver allocated elements */
427         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
428         if (!indio_dev)
429                 return -ENOMEM;
430
431         st = iio_priv(indio_dev);
432         /* this is only used for removal purposes */
433         spi_set_drvdata(spi, indio_dev);
434
435         indio_dev->name = spi->dev.driver->name;
436         indio_dev->dev.parent = &spi->dev;
437         indio_dev->info = &adis16220_info;
438         indio_dev->modes = INDIO_DIRECT_MODE;
439         indio_dev->channels = adis16220_channels;
440         indio_dev->num_channels = ARRAY_SIZE(adis16220_channels);
441
442         ret = iio_device_register(indio_dev);
443         if (ret)
444                 return ret;
445
446         ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &accel_bin);
447         if (ret)
448                 goto error_unregister_dev;
449
450         ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc1_bin);
451         if (ret)
452                 goto error_rm_accel_bin;
453
454         ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc2_bin);
455         if (ret)
456                 goto error_rm_adc1_bin;
457
458         ret = adis_init(&st->adis, indio_dev, spi, &adis16220_data);
459         if (ret)
460                 goto error_rm_adc2_bin;
461         /* Get the device into a sane initial state */
462         ret = adis_initial_startup(&st->adis);
463         if (ret)
464                 goto error_rm_adc2_bin;
465         return 0;
466
467 error_rm_adc2_bin:
468         sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
469 error_rm_adc1_bin:
470         sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
471 error_rm_accel_bin:
472         sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
473 error_unregister_dev:
474         iio_device_unregister(indio_dev);
475         return ret;
476 }
477
478 static int adis16220_remove(struct spi_device *spi)
479 {
480         struct iio_dev *indio_dev = spi_get_drvdata(spi);
481
482         sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
483         sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
484         sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
485         iio_device_unregister(indio_dev);
486
487         return 0;
488 }
489
490 static struct spi_driver adis16220_driver = {
491         .driver = {
492                 .name = "adis16220",
493                 .owner = THIS_MODULE,
494         },
495         .probe = adis16220_probe,
496         .remove = adis16220_remove,
497 };
498 module_spi_driver(adis16220_driver);
499
500 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
501 MODULE_DESCRIPTION("Analog Devices ADIS16220 Digital Vibration Sensor");
502 MODULE_LICENSE("GPL v2");
503 MODULE_ALIAS("spi:adis16220");