Linux-libre 4.10.3-gnu
[librecmc/linux-libre.git] / drivers / staging / iio / adc / ad7606.c
1 /*
2  * AD7606 SPI ADC driver
3  *
4  * Copyright 2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/err.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
20
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
26
27 #include "ad7606.h"
28
29 static int ad7606_reset(struct ad7606_state *st)
30 {
31         if (st->gpio_reset) {
32                 gpiod_set_value(st->gpio_reset, 1);
33                 ndelay(100); /* t_reset >= 100ns */
34                 gpiod_set_value(st->gpio_reset, 0);
35                 return 0;
36         }
37
38         return -ENODEV;
39 }
40
41 static int ad7606_read_samples(struct ad7606_state *st)
42 {
43         unsigned int num = st->chip_info->num_channels;
44         u16 *data = st->data;
45         int ret;
46
47         /*
48          * The frstdata signal is set to high while and after reading the sample
49          * of the first channel and low for all other channels. This can be used
50          * to check that the incoming data is correctly aligned. During normal
51          * operation the data should never become unaligned, but some glitch or
52          * electrostatic discharge might cause an extra read or clock cycle.
53          * Monitoring the frstdata signal allows to recover from such failure
54          * situations.
55          */
56
57         if (st->gpio_frstdata) {
58                 ret = st->bops->read_block(st->dev, 1, data);
59                 if (ret)
60                         return ret;
61
62                 if (!gpiod_get_value(st->gpio_frstdata)) {
63                         ad7606_reset(st);
64                         return -EIO;
65                 }
66
67                 data++;
68                 num--;
69         }
70
71         return st->bops->read_block(st->dev, num, data);
72 }
73
74 static irqreturn_t ad7606_trigger_handler(int irq, void *p)
75 {
76         struct iio_poll_func *pf = p;
77         struct ad7606_state *st = iio_priv(pf->indio_dev);
78
79         gpiod_set_value(st->gpio_convst, 1);
80
81         return IRQ_HANDLED;
82 }
83
84 /**
85  * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
86  * @work_s:     the work struct through which this was scheduled
87  *
88  * Currently there is no option in this driver to disable the saving of
89  * timestamps within the ring.
90  * I think the one copy of this at a time was to avoid problems if the
91  * trigger was set far too high and the reads then locked up the computer.
92  **/
93 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
94 {
95         struct ad7606_state *st = container_of(work_s, struct ad7606_state,
96                                                 poll_work);
97         struct iio_dev *indio_dev = iio_priv_to_dev(st);
98         int ret;
99
100         ret = ad7606_read_samples(st);
101         if (ret == 0)
102                 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
103                                                    iio_get_time_ns(indio_dev));
104
105         gpiod_set_value(st->gpio_convst, 0);
106         iio_trigger_notify_done(indio_dev->trig);
107 }
108
109 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
110 {
111         struct ad7606_state *st = iio_priv(indio_dev);
112         int ret;
113
114         st->done = false;
115         gpiod_set_value(st->gpio_convst, 1);
116
117         ret = wait_event_interruptible(st->wq_data_avail, st->done);
118         if (ret)
119                 goto error_ret;
120
121         ret = ad7606_read_samples(st);
122         if (ret == 0)
123                 ret = st->data[ch];
124
125 error_ret:
126         gpiod_set_value(st->gpio_convst, 0);
127
128         return ret;
129 }
130
131 static int ad7606_read_raw(struct iio_dev *indio_dev,
132                            struct iio_chan_spec const *chan,
133                            int *val,
134                            int *val2,
135                            long m)
136 {
137         int ret;
138         struct ad7606_state *st = iio_priv(indio_dev);
139
140         switch (m) {
141         case IIO_CHAN_INFO_RAW:
142                 ret = iio_device_claim_direct_mode(indio_dev);
143                 if (ret)
144                         return ret;
145
146                 ret = ad7606_scan_direct(indio_dev, chan->address);
147                 iio_device_release_direct_mode(indio_dev);
148
149                 if (ret < 0)
150                         return ret;
151                 *val = (short)ret;
152                 return IIO_VAL_INT;
153         case IIO_CHAN_INFO_SCALE:
154                 *val = st->range * 2;
155                 *val2 = st->chip_info->channels[0].scan_type.realbits;
156                 return IIO_VAL_FRACTIONAL_LOG2;
157         case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
158                 *val = st->oversampling;
159                 return IIO_VAL_INT;
160         }
161         return -EINVAL;
162 }
163
164 static ssize_t ad7606_show_range(struct device *dev,
165                                  struct device_attribute *attr, char *buf)
166 {
167         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
168         struct ad7606_state *st = iio_priv(indio_dev);
169
170         return sprintf(buf, "%u\n", st->range);
171 }
172
173 static ssize_t ad7606_store_range(struct device *dev,
174                                   struct device_attribute *attr,
175                                   const char *buf, size_t count)
176 {
177         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
178         struct ad7606_state *st = iio_priv(indio_dev);
179         unsigned long lval;
180         int ret;
181
182         ret = kstrtoul(buf, 10, &lval);
183         if (ret)
184                 return ret;
185
186         if (!(lval == 5000 || lval == 10000))
187                 return -EINVAL;
188
189         mutex_lock(&indio_dev->mlock);
190         gpiod_set_value(st->gpio_range, lval == 10000);
191         st->range = lval;
192         mutex_unlock(&indio_dev->mlock);
193
194         return count;
195 }
196
197 static IIO_DEVICE_ATTR(in_voltage_range, S_IRUGO | S_IWUSR,
198                        ad7606_show_range, ad7606_store_range, 0);
199 static IIO_CONST_ATTR(in_voltage_range_available, "5000 10000");
200
201 static int ad7606_oversampling_get_index(unsigned int val)
202 {
203         unsigned char supported[] = {1, 2, 4, 8, 16, 32, 64};
204         int i;
205
206         for (i = 0; i < ARRAY_SIZE(supported); i++)
207                 if (val == supported[i])
208                         return i;
209
210         return -EINVAL;
211 }
212
213 static int ad7606_write_raw(struct iio_dev *indio_dev,
214                             struct iio_chan_spec const *chan,
215                             int val,
216                             int val2,
217                             long mask)
218 {
219         struct ad7606_state *st = iio_priv(indio_dev);
220         int values[3];
221         int ret;
222
223         switch (mask) {
224         case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
225                 if (val2)
226                         return -EINVAL;
227                 ret = ad7606_oversampling_get_index(val);
228                 if (ret < 0)
229                         return ret;
230
231                 values[0] = (ret >> 0) & 1;
232                 values[1] = (ret >> 1) & 1;
233                 values[2] = (ret >> 2) & 1;
234
235                 mutex_lock(&indio_dev->mlock);
236                 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
237                                       values);
238                 st->oversampling = val;
239                 mutex_unlock(&indio_dev->mlock);
240
241                 return 0;
242         default:
243                 return -EINVAL;
244         }
245 }
246
247 static IIO_CONST_ATTR(oversampling_ratio_available, "1 2 4 8 16 32 64");
248
249 static struct attribute *ad7606_attributes_os_and_range[] = {
250         &iio_dev_attr_in_voltage_range.dev_attr.attr,
251         &iio_const_attr_in_voltage_range_available.dev_attr.attr,
252         &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
253         NULL,
254 };
255
256 static const struct attribute_group ad7606_attribute_group_os_and_range = {
257         .attrs = ad7606_attributes_os_and_range,
258 };
259
260 static struct attribute *ad7606_attributes_os[] = {
261         &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
262         NULL,
263 };
264
265 static const struct attribute_group ad7606_attribute_group_os = {
266         .attrs = ad7606_attributes_os,
267 };
268
269 static struct attribute *ad7606_attributes_range[] = {
270         &iio_dev_attr_in_voltage_range.dev_attr.attr,
271         &iio_const_attr_in_voltage_range_available.dev_attr.attr,
272         NULL,
273 };
274
275 static const struct attribute_group ad7606_attribute_group_range = {
276         .attrs = ad7606_attributes_range,
277 };
278
279 #define AD7606_CHANNEL(num)                                     \
280         {                                                       \
281                 .type = IIO_VOLTAGE,                            \
282                 .indexed = 1,                                   \
283                 .channel = num,                                 \
284                 .address = num,                                 \
285                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),   \
286                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
287                 .info_mask_shared_by_all =                      \
288                         BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),  \
289                 .scan_index = num,                              \
290                 .scan_type = {                                  \
291                         .sign = 's',                            \
292                         .realbits = 16,                         \
293                         .storagebits = 16,                      \
294                         .endianness = IIO_CPU,                  \
295                 },                                              \
296         }
297
298 static const struct iio_chan_spec ad7606_channels[] = {
299         IIO_CHAN_SOFT_TIMESTAMP(8),
300         AD7606_CHANNEL(0),
301         AD7606_CHANNEL(1),
302         AD7606_CHANNEL(2),
303         AD7606_CHANNEL(3),
304         AD7606_CHANNEL(4),
305         AD7606_CHANNEL(5),
306         AD7606_CHANNEL(6),
307         AD7606_CHANNEL(7),
308 };
309
310 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
311         /*
312          * More devices added in future
313          */
314         [ID_AD7606_8] = {
315                 .channels = ad7606_channels,
316                 .num_channels = 9,
317         },
318         [ID_AD7606_6] = {
319                 .channels = ad7606_channels,
320                 .num_channels = 7,
321         },
322         [ID_AD7606_4] = {
323                 .channels = ad7606_channels,
324                 .num_channels = 5,
325         },
326 };
327
328 static int ad7606_request_gpios(struct ad7606_state *st)
329 {
330         struct device *dev = st->dev;
331
332         st->gpio_convst = devm_gpiod_get(dev, "conversion-start",
333                                          GPIOD_OUT_LOW);
334         if (IS_ERR(st->gpio_convst))
335                 return PTR_ERR(st->gpio_convst);
336
337         st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
338         if (IS_ERR(st->gpio_reset))
339                 return PTR_ERR(st->gpio_reset);
340
341         st->gpio_range = devm_gpiod_get_optional(dev, "range", GPIOD_OUT_LOW);
342         if (IS_ERR(st->gpio_range))
343                 return PTR_ERR(st->gpio_range);
344
345         st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
346                                                    GPIOD_OUT_HIGH);
347         if (IS_ERR(st->gpio_standby))
348                 return PTR_ERR(st->gpio_standby);
349
350         st->gpio_frstdata = devm_gpiod_get_optional(dev, "first-data",
351                                                     GPIOD_IN);
352         if (IS_ERR(st->gpio_frstdata))
353                 return PTR_ERR(st->gpio_frstdata);
354
355         st->gpio_os = devm_gpiod_get_array_optional(dev, "oversampling-ratio",
356                         GPIOD_OUT_LOW);
357         return PTR_ERR_OR_ZERO(st->gpio_os);
358 }
359
360 /**
361  *  Interrupt handler
362  */
363 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
364 {
365         struct iio_dev *indio_dev = dev_id;
366         struct ad7606_state *st = iio_priv(indio_dev);
367
368         if (iio_buffer_enabled(indio_dev)) {
369                 schedule_work(&st->poll_work);
370         } else {
371                 st->done = true;
372                 wake_up_interruptible(&st->wq_data_avail);
373         }
374
375         return IRQ_HANDLED;
376 };
377
378 static const struct iio_info ad7606_info_no_os_or_range = {
379         .driver_module = THIS_MODULE,
380         .read_raw = &ad7606_read_raw,
381 };
382
383 static const struct iio_info ad7606_info_os_and_range = {
384         .driver_module = THIS_MODULE,
385         .read_raw = &ad7606_read_raw,
386         .write_raw = &ad7606_write_raw,
387         .attrs = &ad7606_attribute_group_os_and_range,
388 };
389
390 static const struct iio_info ad7606_info_os = {
391         .driver_module = THIS_MODULE,
392         .read_raw = &ad7606_read_raw,
393         .write_raw = &ad7606_write_raw,
394         .attrs = &ad7606_attribute_group_os,
395 };
396
397 static const struct iio_info ad7606_info_range = {
398         .driver_module = THIS_MODULE,
399         .read_raw = &ad7606_read_raw,
400         .attrs = &ad7606_attribute_group_range,
401 };
402
403 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
404                  const char *name, unsigned int id,
405                  const struct ad7606_bus_ops *bops)
406 {
407         struct ad7606_state *st;
408         int ret;
409         struct iio_dev *indio_dev;
410
411         indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
412         if (!indio_dev)
413                 return -ENOMEM;
414
415         st = iio_priv(indio_dev);
416
417         st->dev = dev;
418         st->bops = bops;
419         st->base_address = base_address;
420         st->range = 5000;
421         st->oversampling = 1;
422         INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
423
424         st->reg = devm_regulator_get(dev, "avcc");
425         if (IS_ERR(st->reg))
426                 return PTR_ERR(st->reg);
427
428         ret = regulator_enable(st->reg);
429         if (ret) {
430                 dev_err(dev, "Failed to enable specified AVcc supply\n");
431                 return ret;
432         }
433
434         ret = ad7606_request_gpios(st);
435         if (ret)
436                 goto error_disable_reg;
437
438         st->chip_info = &ad7606_chip_info_tbl[id];
439
440         indio_dev->dev.parent = dev;
441         if (st->gpio_os) {
442                 if (st->gpio_range)
443                         indio_dev->info = &ad7606_info_os_and_range;
444                 else
445                         indio_dev->info = &ad7606_info_os;
446         } else {
447                 if (st->gpio_range)
448                         indio_dev->info = &ad7606_info_range;
449                 else
450                         indio_dev->info = &ad7606_info_no_os_or_range;
451         }
452         indio_dev->modes = INDIO_DIRECT_MODE;
453         indio_dev->name = name;
454         indio_dev->channels = st->chip_info->channels;
455         indio_dev->num_channels = st->chip_info->num_channels;
456
457         init_waitqueue_head(&st->wq_data_avail);
458
459         ret = ad7606_reset(st);
460         if (ret)
461                 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
462
463         ret = request_irq(irq, ad7606_interrupt, IRQF_TRIGGER_FALLING, name,
464                           indio_dev);
465         if (ret)
466                 goto error_disable_reg;
467
468         ret = iio_triggered_buffer_setup(indio_dev, &ad7606_trigger_handler,
469                                          NULL, NULL);
470         if (ret)
471                 goto error_free_irq;
472
473         ret = iio_device_register(indio_dev);
474         if (ret)
475                 goto error_unregister_ring;
476
477         dev_set_drvdata(dev, indio_dev);
478
479         return 0;
480 error_unregister_ring:
481         iio_triggered_buffer_cleanup(indio_dev);
482
483 error_free_irq:
484         free_irq(irq, indio_dev);
485
486 error_disable_reg:
487         regulator_disable(st->reg);
488         return ret;
489 }
490 EXPORT_SYMBOL_GPL(ad7606_probe);
491
492 int ad7606_remove(struct device *dev, int irq)
493 {
494         struct iio_dev *indio_dev = dev_get_drvdata(dev);
495         struct ad7606_state *st = iio_priv(indio_dev);
496
497         iio_device_unregister(indio_dev);
498         iio_triggered_buffer_cleanup(indio_dev);
499
500         free_irq(irq, indio_dev);
501         regulator_disable(st->reg);
502
503         return 0;
504 }
505 EXPORT_SYMBOL_GPL(ad7606_remove);
506
507 #ifdef CONFIG_PM_SLEEP
508
509 static int ad7606_suspend(struct device *dev)
510 {
511         struct iio_dev *indio_dev = dev_get_drvdata(dev);
512         struct ad7606_state *st = iio_priv(indio_dev);
513
514         if (st->gpio_standby) {
515                 gpiod_set_value(st->gpio_range, 1);
516                 gpiod_set_value(st->gpio_standby, 0);
517         }
518
519         return 0;
520 }
521
522 static int ad7606_resume(struct device *dev)
523 {
524         struct iio_dev *indio_dev = dev_get_drvdata(dev);
525         struct ad7606_state *st = iio_priv(indio_dev);
526
527         if (st->gpio_standby) {
528                 gpiod_set_value(st->gpio_range, st->range == 10000);
529                 gpiod_set_value(st->gpio_standby, 1);
530                 ad7606_reset(st);
531         }
532
533         return 0;
534 }
535
536 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
537 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
538
539 #endif
540
541 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
542 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
543 MODULE_LICENSE("GPL v2");