GNU Linux-libre 4.14.290-gnu1
[releases.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 /*
30  * Scales are computed as 5000/32768 and 10000/32768 respectively,
31  * so that when applied to the raw values they provide mV values
32  */
33 static const unsigned int scale_avail[2][2] = {
34         {0, 152588}, {0, 305176}
35 };
36
37 static int ad7606_reset(struct ad7606_state *st)
38 {
39         if (st->gpio_reset) {
40                 gpiod_set_value(st->gpio_reset, 1);
41                 ndelay(100); /* t_reset >= 100ns */
42                 gpiod_set_value(st->gpio_reset, 0);
43                 return 0;
44         }
45
46         return -ENODEV;
47 }
48
49 static int ad7606_read_samples(struct ad7606_state *st)
50 {
51         unsigned int num = st->chip_info->num_channels;
52         u16 *data = st->data;
53         int ret;
54
55         /*
56          * The frstdata signal is set to high while and after reading the sample
57          * of the first channel and low for all other channels. This can be used
58          * to check that the incoming data is correctly aligned. During normal
59          * operation the data should never become unaligned, but some glitch or
60          * electrostatic discharge might cause an extra read or clock cycle.
61          * Monitoring the frstdata signal allows to recover from such failure
62          * situations.
63          */
64
65         if (st->gpio_frstdata) {
66                 ret = st->bops->read_block(st->dev, 1, data);
67                 if (ret)
68                         return ret;
69
70                 if (!gpiod_get_value(st->gpio_frstdata)) {
71                         ad7606_reset(st);
72                         return -EIO;
73                 }
74
75                 data++;
76                 num--;
77         }
78
79         return st->bops->read_block(st->dev, num, data);
80 }
81
82 static irqreturn_t ad7606_trigger_handler(int irq, void *p)
83 {
84         struct iio_poll_func *pf = p;
85         struct ad7606_state *st = iio_priv(pf->indio_dev);
86
87         gpiod_set_value(st->gpio_convst, 1);
88
89         return IRQ_HANDLED;
90 }
91
92 /**
93  * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
94  * @work_s:     the work struct through which this was scheduled
95  *
96  * Currently there is no option in this driver to disable the saving of
97  * timestamps within the ring.
98  * I think the one copy of this at a time was to avoid problems if the
99  * trigger was set far too high and the reads then locked up the computer.
100  **/
101 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
102 {
103         struct ad7606_state *st = container_of(work_s, struct ad7606_state,
104                                                 poll_work);
105         struct iio_dev *indio_dev = iio_priv_to_dev(st);
106         int ret;
107
108         ret = ad7606_read_samples(st);
109         if (ret == 0)
110                 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
111                                                    iio_get_time_ns(indio_dev));
112
113         gpiod_set_value(st->gpio_convst, 0);
114         iio_trigger_notify_done(indio_dev->trig);
115 }
116
117 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
118 {
119         struct ad7606_state *st = iio_priv(indio_dev);
120         int ret;
121
122         st->done = false;
123         gpiod_set_value(st->gpio_convst, 1);
124
125         ret = wait_event_interruptible(st->wq_data_avail, st->done);
126         if (ret)
127                 goto error_ret;
128
129         ret = ad7606_read_samples(st);
130         if (ret == 0)
131                 ret = st->data[ch];
132
133 error_ret:
134         gpiod_set_value(st->gpio_convst, 0);
135
136         return ret;
137 }
138
139 static int ad7606_read_raw(struct iio_dev *indio_dev,
140                            struct iio_chan_spec const *chan,
141                            int *val,
142                            int *val2,
143                            long m)
144 {
145         int ret;
146         struct ad7606_state *st = iio_priv(indio_dev);
147
148         switch (m) {
149         case IIO_CHAN_INFO_RAW:
150                 ret = iio_device_claim_direct_mode(indio_dev);
151                 if (ret)
152                         return ret;
153
154                 ret = ad7606_scan_direct(indio_dev, chan->address);
155                 iio_device_release_direct_mode(indio_dev);
156
157                 if (ret < 0)
158                         return ret;
159                 *val = (short)ret;
160                 return IIO_VAL_INT;
161         case IIO_CHAN_INFO_SCALE:
162                 *val = scale_avail[st->range][0];
163                 *val2 = scale_avail[st->range][1];
164                 return IIO_VAL_INT_PLUS_MICRO;
165         case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
166                 *val = st->oversampling;
167                 return IIO_VAL_INT;
168         }
169         return -EINVAL;
170 }
171
172 static ssize_t in_voltage_scale_available_show(struct device *dev,
173                                                struct device_attribute *attr,
174                                                char *buf)
175 {
176         int i, len = 0;
177
178         for (i = 0; i < ARRAY_SIZE(scale_avail); i++)
179                 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06u ",
180                                  scale_avail[i][0], scale_avail[i][1]);
181
182         buf[len - 1] = '\n';
183
184         return len;
185 }
186
187 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
188
189 static int ad7606_oversampling_get_index(unsigned int val)
190 {
191         unsigned char supported[] = {1, 2, 4, 8, 16, 32, 64};
192         int i;
193
194         for (i = 0; i < ARRAY_SIZE(supported); i++)
195                 if (val == supported[i])
196                         return i;
197
198         return -EINVAL;
199 }
200
201 static int ad7606_write_raw(struct iio_dev *indio_dev,
202                             struct iio_chan_spec const *chan,
203                             int val,
204                             int val2,
205                             long mask)
206 {
207         struct ad7606_state *st = iio_priv(indio_dev);
208         int values[3];
209         int ret, i;
210
211         switch (mask) {
212         case IIO_CHAN_INFO_SCALE:
213                 ret = -EINVAL;
214                 mutex_lock(&st->lock);
215                 for (i = 0; i < ARRAY_SIZE(scale_avail); i++)
216                         if (val2 == scale_avail[i][1]) {
217                                 gpiod_set_value(st->gpio_range, i);
218                                 st->range = i;
219
220                                 ret = 0;
221                                 break;
222                         }
223                 mutex_unlock(&st->lock);
224
225                 return ret;
226         case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
227                 if (val2)
228                         return -EINVAL;
229                 ret = ad7606_oversampling_get_index(val);
230                 if (ret < 0)
231                         return ret;
232
233                 values[0] = (ret >> 0) & 1;
234                 values[1] = (ret >> 1) & 1;
235                 values[2] = (ret >> 2) & 1;
236
237                 mutex_lock(&st->lock);
238                 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
239                                       values);
240                 st->oversampling = val;
241                 mutex_unlock(&st->lock);
242
243                 return 0;
244         default:
245                 return -EINVAL;
246         }
247 }
248
249 static IIO_CONST_ATTR(oversampling_ratio_available, "1 2 4 8 16 32 64");
250
251 static struct attribute *ad7606_attributes_os_and_range[] = {
252         &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
253         &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
254         NULL,
255 };
256
257 static const struct attribute_group ad7606_attribute_group_os_and_range = {
258         .attrs = ad7606_attributes_os_and_range,
259 };
260
261 static struct attribute *ad7606_attributes_os[] = {
262         &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
263         NULL,
264 };
265
266 static const struct attribute_group ad7606_attribute_group_os = {
267         .attrs = ad7606_attributes_os,
268 };
269
270 static struct attribute *ad7606_attributes_range[] = {
271         &iio_dev_attr_in_voltage_scale_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         .write_raw = &ad7606_write_raw,
401         .attrs = &ad7606_attribute_group_range,
402 };
403
404 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
405                  const char *name, unsigned int id,
406                  const struct ad7606_bus_ops *bops)
407 {
408         struct ad7606_state *st;
409         int ret;
410         struct iio_dev *indio_dev;
411
412         indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
413         if (!indio_dev)
414                 return -ENOMEM;
415
416         st = iio_priv(indio_dev);
417
418         st->dev = dev;
419         mutex_init(&st->lock);
420         st->bops = bops;
421         st->base_address = base_address;
422         /* tied to logic low, analog input range is +/- 5V */
423         st->range = 0;
424         st->oversampling = 1;
425         INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
426
427         st->reg = devm_regulator_get(dev, "avcc");
428         if (IS_ERR(st->reg))
429                 return PTR_ERR(st->reg);
430
431         ret = regulator_enable(st->reg);
432         if (ret) {
433                 dev_err(dev, "Failed to enable specified AVcc supply\n");
434                 return ret;
435         }
436
437         ret = ad7606_request_gpios(st);
438         if (ret)
439                 goto error_disable_reg;
440
441         st->chip_info = &ad7606_chip_info_tbl[id];
442
443         indio_dev->dev.parent = dev;
444         if (st->gpio_os) {
445                 if (st->gpio_range)
446                         indio_dev->info = &ad7606_info_os_and_range;
447                 else
448                         indio_dev->info = &ad7606_info_os;
449         } else {
450                 if (st->gpio_range)
451                         indio_dev->info = &ad7606_info_range;
452                 else
453                         indio_dev->info = &ad7606_info_no_os_or_range;
454         }
455         indio_dev->modes = INDIO_DIRECT_MODE;
456         indio_dev->name = name;
457         indio_dev->channels = st->chip_info->channels;
458         indio_dev->num_channels = st->chip_info->num_channels;
459
460         init_waitqueue_head(&st->wq_data_avail);
461
462         ret = ad7606_reset(st);
463         if (ret)
464                 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
465
466         ret = request_irq(irq, ad7606_interrupt, IRQF_TRIGGER_FALLING, name,
467                           indio_dev);
468         if (ret)
469                 goto error_disable_reg;
470
471         ret = iio_triggered_buffer_setup(indio_dev, &ad7606_trigger_handler,
472                                          NULL, NULL);
473         if (ret)
474                 goto error_free_irq;
475
476         ret = iio_device_register(indio_dev);
477         if (ret)
478                 goto error_unregister_ring;
479
480         dev_set_drvdata(dev, indio_dev);
481
482         return 0;
483 error_unregister_ring:
484         iio_triggered_buffer_cleanup(indio_dev);
485
486 error_free_irq:
487         free_irq(irq, indio_dev);
488
489 error_disable_reg:
490         regulator_disable(st->reg);
491         return ret;
492 }
493 EXPORT_SYMBOL_GPL(ad7606_probe);
494
495 int ad7606_remove(struct device *dev, int irq)
496 {
497         struct iio_dev *indio_dev = dev_get_drvdata(dev);
498         struct ad7606_state *st = iio_priv(indio_dev);
499
500         iio_device_unregister(indio_dev);
501         iio_triggered_buffer_cleanup(indio_dev);
502
503         free_irq(irq, indio_dev);
504         regulator_disable(st->reg);
505
506         return 0;
507 }
508 EXPORT_SYMBOL_GPL(ad7606_remove);
509
510 #ifdef CONFIG_PM_SLEEP
511
512 static int ad7606_suspend(struct device *dev)
513 {
514         struct iio_dev *indio_dev = dev_get_drvdata(dev);
515         struct ad7606_state *st = iio_priv(indio_dev);
516
517         if (st->gpio_standby) {
518                 gpiod_set_value(st->gpio_range, 1);
519                 gpiod_set_value(st->gpio_standby, 0);
520         }
521
522         return 0;
523 }
524
525 static int ad7606_resume(struct device *dev)
526 {
527         struct iio_dev *indio_dev = dev_get_drvdata(dev);
528         struct ad7606_state *st = iio_priv(indio_dev);
529
530         if (st->gpio_standby) {
531                 gpiod_set_value(st->gpio_range, st->range);
532                 gpiod_set_value(st->gpio_standby, 1);
533                 ad7606_reset(st);
534         }
535
536         return 0;
537 }
538
539 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
540 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
541
542 #endif
543
544 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
545 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
546 MODULE_LICENSE("GPL v2");