GNU Linux-libre 4.19.295-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         .read_raw = &ad7606_read_raw,
380 };
381
382 static const struct iio_info ad7606_info_os_and_range = {
383         .read_raw = &ad7606_read_raw,
384         .write_raw = &ad7606_write_raw,
385         .attrs = &ad7606_attribute_group_os_and_range,
386 };
387
388 static const struct iio_info ad7606_info_os = {
389         .read_raw = &ad7606_read_raw,
390         .write_raw = &ad7606_write_raw,
391         .attrs = &ad7606_attribute_group_os,
392 };
393
394 static const struct iio_info ad7606_info_range = {
395         .read_raw = &ad7606_read_raw,
396         .write_raw = &ad7606_write_raw,
397         .attrs = &ad7606_attribute_group_range,
398 };
399
400 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
401                  const char *name, unsigned int id,
402                  const struct ad7606_bus_ops *bops)
403 {
404         struct ad7606_state *st;
405         int ret;
406         struct iio_dev *indio_dev;
407
408         indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
409         if (!indio_dev)
410                 return -ENOMEM;
411
412         st = iio_priv(indio_dev);
413
414         st->dev = dev;
415         mutex_init(&st->lock);
416         st->bops = bops;
417         st->base_address = base_address;
418         /* tied to logic low, analog input range is +/- 5V */
419         st->range = 0;
420         st->oversampling = 1;
421         INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
422
423         st->reg = devm_regulator_get(dev, "avcc");
424         if (IS_ERR(st->reg))
425                 return PTR_ERR(st->reg);
426
427         ret = regulator_enable(st->reg);
428         if (ret) {
429                 dev_err(dev, "Failed to enable specified AVcc supply\n");
430                 return ret;
431         }
432
433         ret = ad7606_request_gpios(st);
434         if (ret)
435                 goto error_disable_reg;
436
437         st->chip_info = &ad7606_chip_info_tbl[id];
438
439         indio_dev->dev.parent = dev;
440         if (st->gpio_os) {
441                 if (st->gpio_range)
442                         indio_dev->info = &ad7606_info_os_and_range;
443                 else
444                         indio_dev->info = &ad7606_info_os;
445         } else {
446                 if (st->gpio_range)
447                         indio_dev->info = &ad7606_info_range;
448                 else
449                         indio_dev->info = &ad7606_info_no_os_or_range;
450         }
451         indio_dev->modes = INDIO_DIRECT_MODE;
452         indio_dev->name = name;
453         indio_dev->channels = st->chip_info->channels;
454         indio_dev->num_channels = st->chip_info->num_channels;
455
456         init_waitqueue_head(&st->wq_data_avail);
457
458         ret = ad7606_reset(st);
459         if (ret)
460                 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
461
462         ret = request_irq(irq, ad7606_interrupt, IRQF_TRIGGER_FALLING, name,
463                           indio_dev);
464         if (ret)
465                 goto error_disable_reg;
466
467         ret = iio_triggered_buffer_setup(indio_dev, &ad7606_trigger_handler,
468                                          NULL, NULL);
469         if (ret)
470                 goto error_free_irq;
471
472         ret = iio_device_register(indio_dev);
473         if (ret)
474                 goto error_unregister_ring;
475
476         dev_set_drvdata(dev, indio_dev);
477
478         return 0;
479 error_unregister_ring:
480         iio_triggered_buffer_cleanup(indio_dev);
481
482 error_free_irq:
483         free_irq(irq, indio_dev);
484
485 error_disable_reg:
486         regulator_disable(st->reg);
487         return ret;
488 }
489 EXPORT_SYMBOL_GPL(ad7606_probe);
490
491 int ad7606_remove(struct device *dev, int irq)
492 {
493         struct iio_dev *indio_dev = dev_get_drvdata(dev);
494         struct ad7606_state *st = iio_priv(indio_dev);
495
496         iio_device_unregister(indio_dev);
497         iio_triggered_buffer_cleanup(indio_dev);
498
499         free_irq(irq, indio_dev);
500         regulator_disable(st->reg);
501
502         return 0;
503 }
504 EXPORT_SYMBOL_GPL(ad7606_remove);
505
506 #ifdef CONFIG_PM_SLEEP
507
508 static int ad7606_suspend(struct device *dev)
509 {
510         struct iio_dev *indio_dev = dev_get_drvdata(dev);
511         struct ad7606_state *st = iio_priv(indio_dev);
512
513         if (st->gpio_standby) {
514                 gpiod_set_value(st->gpio_range, 1);
515                 gpiod_set_value(st->gpio_standby, 0);
516         }
517
518         return 0;
519 }
520
521 static int ad7606_resume(struct device *dev)
522 {
523         struct iio_dev *indio_dev = dev_get_drvdata(dev);
524         struct ad7606_state *st = iio_priv(indio_dev);
525
526         if (st->gpio_standby) {
527                 gpiod_set_value(st->gpio_range, st->range);
528                 gpiod_set_value(st->gpio_standby, 1);
529                 ad7606_reset(st);
530         }
531
532         return 0;
533 }
534
535 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
536 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
537
538 #endif
539
540 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
541 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
542 MODULE_LICENSE("GPL v2");