Merge tag 'iio-for-4.21a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23...
[linux-2.6-block.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         DECLARE_BITMAP(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;
234
235                 mutex_lock(&st->lock);
236                 gpiod_set_array_value(3, st->gpio_os->desc, st->gpio_os->info,
237                                       values);
238                 st->oversampling = val;
239                 mutex_unlock(&st->lock);
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_scale_available.dev_attr.attr,
251         &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
252         NULL,
253 };
254
255 static const struct attribute_group ad7606_attribute_group_os_and_range = {
256         .attrs = ad7606_attributes_os_and_range,
257 };
258
259 static struct attribute *ad7606_attributes_os[] = {
260         &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
261         NULL,
262 };
263
264 static const struct attribute_group ad7606_attribute_group_os = {
265         .attrs = ad7606_attributes_os,
266 };
267
268 static struct attribute *ad7606_attributes_range[] = {
269         &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
270         NULL,
271 };
272
273 static const struct attribute_group ad7606_attribute_group_range = {
274         .attrs = ad7606_attributes_range,
275 };
276
277 #define AD760X_CHANNEL(num, mask)                               \
278         {                                                       \
279                 .type = IIO_VOLTAGE,                            \
280                 .indexed = 1,                                   \
281                 .channel = num,                                 \
282                 .address = num,                                 \
283                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),   \
284                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
285                 .info_mask_shared_by_all = mask,                \
286                 .scan_index = num,                              \
287                 .scan_type = {                                  \
288                         .sign = 's',                            \
289                         .realbits = 16,                         \
290                         .storagebits = 16,                      \
291                         .endianness = IIO_CPU,                  \
292                 },                                              \
293         }
294
295 #define AD7605_CHANNEL(num)     \
296         AD760X_CHANNEL(num, 0)
297
298 #define AD7606_CHANNEL(num)     \
299         AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO))
300
301 static const struct iio_chan_spec ad7605_channels[] = {
302         IIO_CHAN_SOFT_TIMESTAMP(4),
303         AD7605_CHANNEL(0),
304         AD7605_CHANNEL(1),
305         AD7605_CHANNEL(2),
306         AD7605_CHANNEL(3),
307 };
308
309 static const struct iio_chan_spec ad7606_channels[] = {
310         IIO_CHAN_SOFT_TIMESTAMP(8),
311         AD7606_CHANNEL(0),
312         AD7606_CHANNEL(1),
313         AD7606_CHANNEL(2),
314         AD7606_CHANNEL(3),
315         AD7606_CHANNEL(4),
316         AD7606_CHANNEL(5),
317         AD7606_CHANNEL(6),
318         AD7606_CHANNEL(7),
319 };
320
321 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
322         /*
323          * More devices added in future
324          */
325         [ID_AD7605_4] = {
326                 .channels = ad7605_channels,
327                 .num_channels = 5,
328         },
329         [ID_AD7606_8] = {
330                 .channels = ad7606_channels,
331                 .num_channels = 9,
332                 .has_oversampling = true,
333         },
334         [ID_AD7606_6] = {
335                 .channels = ad7606_channels,
336                 .num_channels = 7,
337                 .has_oversampling = true,
338         },
339         [ID_AD7606_4] = {
340                 .channels = ad7606_channels,
341                 .num_channels = 5,
342                 .has_oversampling = true,
343         },
344 };
345
346 static int ad7606_request_gpios(struct ad7606_state *st)
347 {
348         struct device *dev = st->dev;
349
350         st->gpio_convst = devm_gpiod_get(dev, "conversion-start",
351                                          GPIOD_OUT_LOW);
352         if (IS_ERR(st->gpio_convst))
353                 return PTR_ERR(st->gpio_convst);
354
355         st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
356         if (IS_ERR(st->gpio_reset))
357                 return PTR_ERR(st->gpio_reset);
358
359         st->gpio_range = devm_gpiod_get_optional(dev, "range", GPIOD_OUT_LOW);
360         if (IS_ERR(st->gpio_range))
361                 return PTR_ERR(st->gpio_range);
362
363         st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
364                                                    GPIOD_OUT_HIGH);
365         if (IS_ERR(st->gpio_standby))
366                 return PTR_ERR(st->gpio_standby);
367
368         st->gpio_frstdata = devm_gpiod_get_optional(dev, "first-data",
369                                                     GPIOD_IN);
370         if (IS_ERR(st->gpio_frstdata))
371                 return PTR_ERR(st->gpio_frstdata);
372
373         if (!st->chip_info->has_oversampling)
374                 return 0;
375
376         st->gpio_os = devm_gpiod_get_array_optional(dev, "oversampling-ratio",
377                                                     GPIOD_OUT_LOW);
378         return PTR_ERR_OR_ZERO(st->gpio_os);
379 }
380
381 /**
382  *  Interrupt handler
383  */
384 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
385 {
386         struct iio_dev *indio_dev = dev_id;
387         struct ad7606_state *st = iio_priv(indio_dev);
388
389         if (iio_buffer_enabled(indio_dev)) {
390                 schedule_work(&st->poll_work);
391         } else {
392                 st->done = true;
393                 wake_up_interruptible(&st->wq_data_avail);
394         }
395
396         return IRQ_HANDLED;
397 };
398
399 static const struct iio_info ad7606_info_no_os_or_range = {
400         .read_raw = &ad7606_read_raw,
401 };
402
403 static const struct iio_info ad7606_info_os_and_range = {
404         .read_raw = &ad7606_read_raw,
405         .write_raw = &ad7606_write_raw,
406         .attrs = &ad7606_attribute_group_os_and_range,
407 };
408
409 static const struct iio_info ad7606_info_os = {
410         .read_raw = &ad7606_read_raw,
411         .write_raw = &ad7606_write_raw,
412         .attrs = &ad7606_attribute_group_os,
413 };
414
415 static const struct iio_info ad7606_info_range = {
416         .read_raw = &ad7606_read_raw,
417         .write_raw = &ad7606_write_raw,
418         .attrs = &ad7606_attribute_group_range,
419 };
420
421 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
422                  const char *name, unsigned int id,
423                  const struct ad7606_bus_ops *bops)
424 {
425         struct ad7606_state *st;
426         int ret;
427         struct iio_dev *indio_dev;
428
429         indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
430         if (!indio_dev)
431                 return -ENOMEM;
432
433         st = iio_priv(indio_dev);
434
435         st->dev = dev;
436         mutex_init(&st->lock);
437         st->bops = bops;
438         st->base_address = base_address;
439         /* tied to logic low, analog input range is +/- 5V */
440         st->range = 0;
441         st->oversampling = 1;
442         INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
443
444         st->reg = devm_regulator_get(dev, "avcc");
445         if (IS_ERR(st->reg))
446                 return PTR_ERR(st->reg);
447
448         ret = regulator_enable(st->reg);
449         if (ret) {
450                 dev_err(dev, "Failed to enable specified AVcc supply\n");
451                 return ret;
452         }
453
454         st->chip_info = &ad7606_chip_info_tbl[id];
455
456         ret = ad7606_request_gpios(st);
457         if (ret)
458                 goto error_disable_reg;
459
460         indio_dev->dev.parent = dev;
461         if (st->gpio_os) {
462                 if (st->gpio_range)
463                         indio_dev->info = &ad7606_info_os_and_range;
464                 else
465                         indio_dev->info = &ad7606_info_os;
466         } else {
467                 if (st->gpio_range)
468                         indio_dev->info = &ad7606_info_range;
469                 else
470                         indio_dev->info = &ad7606_info_no_os_or_range;
471         }
472         indio_dev->modes = INDIO_DIRECT_MODE;
473         indio_dev->name = name;
474         indio_dev->channels = st->chip_info->channels;
475         indio_dev->num_channels = st->chip_info->num_channels;
476
477         init_waitqueue_head(&st->wq_data_avail);
478
479         ret = ad7606_reset(st);
480         if (ret)
481                 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
482
483         ret = request_irq(irq, ad7606_interrupt, IRQF_TRIGGER_FALLING, name,
484                           indio_dev);
485         if (ret)
486                 goto error_disable_reg;
487
488         ret = iio_triggered_buffer_setup(indio_dev, &ad7606_trigger_handler,
489                                          NULL, NULL);
490         if (ret)
491                 goto error_free_irq;
492
493         ret = iio_device_register(indio_dev);
494         if (ret)
495                 goto error_unregister_ring;
496
497         dev_set_drvdata(dev, indio_dev);
498
499         return 0;
500 error_unregister_ring:
501         iio_triggered_buffer_cleanup(indio_dev);
502
503 error_free_irq:
504         free_irq(irq, indio_dev);
505
506 error_disable_reg:
507         regulator_disable(st->reg);
508         return ret;
509 }
510 EXPORT_SYMBOL_GPL(ad7606_probe);
511
512 int ad7606_remove(struct device *dev, int irq)
513 {
514         struct iio_dev *indio_dev = dev_get_drvdata(dev);
515         struct ad7606_state *st = iio_priv(indio_dev);
516
517         iio_device_unregister(indio_dev);
518         iio_triggered_buffer_cleanup(indio_dev);
519
520         free_irq(irq, indio_dev);
521         regulator_disable(st->reg);
522
523         return 0;
524 }
525 EXPORT_SYMBOL_GPL(ad7606_remove);
526
527 #ifdef CONFIG_PM_SLEEP
528
529 static int ad7606_suspend(struct device *dev)
530 {
531         struct iio_dev *indio_dev = dev_get_drvdata(dev);
532         struct ad7606_state *st = iio_priv(indio_dev);
533
534         if (st->gpio_standby) {
535                 gpiod_set_value(st->gpio_range, 1);
536                 gpiod_set_value(st->gpio_standby, 0);
537         }
538
539         return 0;
540 }
541
542 static int ad7606_resume(struct device *dev)
543 {
544         struct iio_dev *indio_dev = dev_get_drvdata(dev);
545         struct ad7606_state *st = iio_priv(indio_dev);
546
547         if (st->gpio_standby) {
548                 gpiod_set_value(st->gpio_range, st->range);
549                 gpiod_set_value(st->gpio_standby, 1);
550                 ad7606_reset(st);
551         }
552
553         return 0;
554 }
555
556 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
557 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
558
559 #endif
560
561 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
562 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
563 MODULE_LICENSE("GPL v2");