Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.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 /* Scales are computed as 2.5/2**16 and 5/2**16 respectively */
30 static const unsigned int scale_avail[2][2] = {
31         {0, 38147}, {0, 76294}
32 };
33
34 static int ad7606_reset(struct ad7606_state *st)
35 {
36         if (st->gpio_reset) {
37                 gpiod_set_value(st->gpio_reset, 1);
38                 ndelay(100); /* t_reset >= 100ns */
39                 gpiod_set_value(st->gpio_reset, 0);
40                 return 0;
41         }
42
43         return -ENODEV;
44 }
45
46 static int ad7606_read_samples(struct ad7606_state *st)
47 {
48         unsigned int num = st->chip_info->num_channels;
49         u16 *data = st->data;
50         int ret;
51
52         /*
53          * The frstdata signal is set to high while and after reading the sample
54          * of the first channel and low for all other channels. This can be used
55          * to check that the incoming data is correctly aligned. During normal
56          * operation the data should never become unaligned, but some glitch or
57          * electrostatic discharge might cause an extra read or clock cycle.
58          * Monitoring the frstdata signal allows to recover from such failure
59          * situations.
60          */
61
62         if (st->gpio_frstdata) {
63                 ret = st->bops->read_block(st->dev, 1, data);
64                 if (ret)
65                         return ret;
66
67                 if (!gpiod_get_value(st->gpio_frstdata)) {
68                         ad7606_reset(st);
69                         return -EIO;
70                 }
71
72                 data++;
73                 num--;
74         }
75
76         return st->bops->read_block(st->dev, num, data);
77 }
78
79 static irqreturn_t ad7606_trigger_handler(int irq, void *p)
80 {
81         struct iio_poll_func *pf = p;
82         struct ad7606_state *st = iio_priv(pf->indio_dev);
83
84         gpiod_set_value(st->gpio_convst, 1);
85
86         return IRQ_HANDLED;
87 }
88
89 /**
90  * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
91  * @work_s:     the work struct through which this was scheduled
92  *
93  * Currently there is no option in this driver to disable the saving of
94  * timestamps within the ring.
95  * I think the one copy of this at a time was to avoid problems if the
96  * trigger was set far too high and the reads then locked up the computer.
97  **/
98 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
99 {
100         struct ad7606_state *st = container_of(work_s, struct ad7606_state,
101                                                 poll_work);
102         struct iio_dev *indio_dev = iio_priv_to_dev(st);
103         int ret;
104
105         ret = ad7606_read_samples(st);
106         if (ret == 0)
107                 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
108                                                    iio_get_time_ns(indio_dev));
109
110         gpiod_set_value(st->gpio_convst, 0);
111         iio_trigger_notify_done(indio_dev->trig);
112 }
113
114 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
115 {
116         struct ad7606_state *st = iio_priv(indio_dev);
117         int ret;
118
119         st->done = false;
120         gpiod_set_value(st->gpio_convst, 1);
121
122         ret = wait_event_interruptible(st->wq_data_avail, st->done);
123         if (ret)
124                 goto error_ret;
125
126         ret = ad7606_read_samples(st);
127         if (ret == 0)
128                 ret = st->data[ch];
129
130 error_ret:
131         gpiod_set_value(st->gpio_convst, 0);
132
133         return ret;
134 }
135
136 static int ad7606_read_raw(struct iio_dev *indio_dev,
137                            struct iio_chan_spec const *chan,
138                            int *val,
139                            int *val2,
140                            long m)
141 {
142         int ret;
143         struct ad7606_state *st = iio_priv(indio_dev);
144
145         switch (m) {
146         case IIO_CHAN_INFO_RAW:
147                 ret = iio_device_claim_direct_mode(indio_dev);
148                 if (ret)
149                         return ret;
150
151                 ret = ad7606_scan_direct(indio_dev, chan->address);
152                 iio_device_release_direct_mode(indio_dev);
153
154                 if (ret < 0)
155                         return ret;
156                 *val = (short)ret;
157                 return IIO_VAL_INT;
158         case IIO_CHAN_INFO_SCALE:
159                 *val = scale_avail[st->range][0];
160                 *val2 = scale_avail[st->range][1];
161                 return IIO_VAL_INT_PLUS_MICRO;
162         case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
163                 *val = st->oversampling;
164                 return IIO_VAL_INT;
165         }
166         return -EINVAL;
167 }
168
169 static ssize_t in_voltage_scale_available_show(struct device *dev,
170                                                struct device_attribute *attr,
171                                                char *buf)
172 {
173         int i, len = 0;
174
175         for (i = 0; i < ARRAY_SIZE(scale_avail); i++)
176                 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06u ",
177                                  scale_avail[i][0], scale_avail[i][1]);
178
179         buf[len - 1] = '\n';
180
181         return len;
182 }
183
184 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
185
186 static int ad7606_oversampling_get_index(unsigned int val)
187 {
188         unsigned char supported[] = {1, 2, 4, 8, 16, 32, 64};
189         int i;
190
191         for (i = 0; i < ARRAY_SIZE(supported); i++)
192                 if (val == supported[i])
193                         return i;
194
195         return -EINVAL;
196 }
197
198 static int ad7606_write_raw(struct iio_dev *indio_dev,
199                             struct iio_chan_spec const *chan,
200                             int val,
201                             int val2,
202                             long mask)
203 {
204         struct ad7606_state *st = iio_priv(indio_dev);
205         int values[3];
206         int ret, i;
207
208         switch (mask) {
209         case IIO_CHAN_INFO_SCALE:
210                 ret = -EINVAL;
211                 mutex_lock(&st->lock);
212                 for (i = 0; i < ARRAY_SIZE(scale_avail); i++)
213                         if (val2 == scale_avail[i][1]) {
214                                 gpiod_set_value(st->gpio_range, i);
215                                 st->range = i;
216
217                                 ret = 0;
218                                 break;
219                         }
220                 mutex_unlock(&st->lock);
221
222                 return ret;
223         case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
224                 if (val2)
225                         return -EINVAL;
226                 ret = ad7606_oversampling_get_index(val);
227                 if (ret < 0)
228                         return ret;
229
230                 values[0] = (ret >> 0) & 1;
231                 values[1] = (ret >> 1) & 1;
232                 values[2] = (ret >> 2) & 1;
233
234                 mutex_lock(&st->lock);
235                 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
236                                       values);
237                 st->oversampling = val;
238                 mutex_unlock(&st->lock);
239
240                 return 0;
241         default:
242                 return -EINVAL;
243         }
244 }
245
246 static IIO_CONST_ATTR(oversampling_ratio_available, "1 2 4 8 16 32 64");
247
248 static struct attribute *ad7606_attributes_os_and_range[] = {
249         &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
250         &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
251         NULL,
252 };
253
254 static const struct attribute_group ad7606_attribute_group_os_and_range = {
255         .attrs = ad7606_attributes_os_and_range,
256 };
257
258 static struct attribute *ad7606_attributes_os[] = {
259         &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
260         NULL,
261 };
262
263 static const struct attribute_group ad7606_attribute_group_os = {
264         .attrs = ad7606_attributes_os,
265 };
266
267 static struct attribute *ad7606_attributes_range[] = {
268         &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
269         NULL,
270 };
271
272 static const struct attribute_group ad7606_attribute_group_range = {
273         .attrs = ad7606_attributes_range,
274 };
275
276 #define AD7606_CHANNEL(num)                                     \
277         {                                                       \
278                 .type = IIO_VOLTAGE,                            \
279                 .indexed = 1,                                   \
280                 .channel = num,                                 \
281                 .address = num,                                 \
282                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),   \
283                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
284                 .info_mask_shared_by_all =                      \
285                         BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),  \
286                 .scan_index = num,                              \
287                 .scan_type = {                                  \
288                         .sign = 's',                            \
289                         .realbits = 16,                         \
290                         .storagebits = 16,                      \
291                         .endianness = IIO_CPU,                  \
292                 },                                              \
293         }
294
295 static const struct iio_chan_spec ad7606_channels[] = {
296         IIO_CHAN_SOFT_TIMESTAMP(8),
297         AD7606_CHANNEL(0),
298         AD7606_CHANNEL(1),
299         AD7606_CHANNEL(2),
300         AD7606_CHANNEL(3),
301         AD7606_CHANNEL(4),
302         AD7606_CHANNEL(5),
303         AD7606_CHANNEL(6),
304         AD7606_CHANNEL(7),
305 };
306
307 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
308         /*
309          * More devices added in future
310          */
311         [ID_AD7606_8] = {
312                 .channels = ad7606_channels,
313                 .num_channels = 9,
314         },
315         [ID_AD7606_6] = {
316                 .channels = ad7606_channels,
317                 .num_channels = 7,
318         },
319         [ID_AD7606_4] = {
320                 .channels = ad7606_channels,
321                 .num_channels = 5,
322         },
323 };
324
325 static int ad7606_request_gpios(struct ad7606_state *st)
326 {
327         struct device *dev = st->dev;
328
329         st->gpio_convst = devm_gpiod_get(dev, "conversion-start",
330                                          GPIOD_OUT_LOW);
331         if (IS_ERR(st->gpio_convst))
332                 return PTR_ERR(st->gpio_convst);
333
334         st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
335         if (IS_ERR(st->gpio_reset))
336                 return PTR_ERR(st->gpio_reset);
337
338         st->gpio_range = devm_gpiod_get_optional(dev, "range", GPIOD_OUT_LOW);
339         if (IS_ERR(st->gpio_range))
340                 return PTR_ERR(st->gpio_range);
341
342         st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
343                                                    GPIOD_OUT_HIGH);
344         if (IS_ERR(st->gpio_standby))
345                 return PTR_ERR(st->gpio_standby);
346
347         st->gpio_frstdata = devm_gpiod_get_optional(dev, "first-data",
348                                                     GPIOD_IN);
349         if (IS_ERR(st->gpio_frstdata))
350                 return PTR_ERR(st->gpio_frstdata);
351
352         st->gpio_os = devm_gpiod_get_array_optional(dev, "oversampling-ratio",
353                         GPIOD_OUT_LOW);
354         return PTR_ERR_OR_ZERO(st->gpio_os);
355 }
356
357 /**
358  *  Interrupt handler
359  */
360 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
361 {
362         struct iio_dev *indio_dev = dev_id;
363         struct ad7606_state *st = iio_priv(indio_dev);
364
365         if (iio_buffer_enabled(indio_dev)) {
366                 schedule_work(&st->poll_work);
367         } else {
368                 st->done = true;
369                 wake_up_interruptible(&st->wq_data_avail);
370         }
371
372         return IRQ_HANDLED;
373 };
374
375 static const struct iio_info ad7606_info_no_os_or_range = {
376         .driver_module = THIS_MODULE,
377         .read_raw = &ad7606_read_raw,
378 };
379
380 static const struct iio_info ad7606_info_os_and_range = {
381         .driver_module = THIS_MODULE,
382         .read_raw = &ad7606_read_raw,
383         .write_raw = &ad7606_write_raw,
384         .attrs = &ad7606_attribute_group_os_and_range,
385 };
386
387 static const struct iio_info ad7606_info_os = {
388         .driver_module = THIS_MODULE,
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         .driver_module = THIS_MODULE,
396         .read_raw = &ad7606_read_raw,
397         .write_raw = &ad7606_write_raw,
398         .attrs = &ad7606_attribute_group_range,
399 };
400
401 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
402                  const char *name, unsigned int id,
403                  const struct ad7606_bus_ops *bops)
404 {
405         struct ad7606_state *st;
406         int ret;
407         struct iio_dev *indio_dev;
408
409         indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
410         if (!indio_dev)
411                 return -ENOMEM;
412
413         st = iio_priv(indio_dev);
414
415         st->dev = dev;
416         mutex_init(&st->lock);
417         st->bops = bops;
418         st->base_address = base_address;
419         /* tied to logic low, analog input range is +/- 5V */
420         st->range = 0;
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);
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");