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