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.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
20
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "../buffer.h"
24
25 #include "ad7606.h"
26
ad7606_reset(struct ad7606_state * st)27 int ad7606_reset(struct ad7606_state *st)
28 {
29 if (gpio_is_valid(st->pdata->gpio_reset)) {
30 gpio_set_value(st->pdata->gpio_reset, 1);
31 ndelay(100); /* t_reset >= 100ns */
32 gpio_set_value(st->pdata->gpio_reset, 0);
33 return 0;
34 }
35
36 return -ENODEV;
37 }
38
ad7606_scan_direct(struct iio_dev * indio_dev,unsigned ch)39 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned ch)
40 {
41 struct ad7606_state *st = iio_priv(indio_dev);
42 int ret;
43
44 st->done = false;
45 gpio_set_value(st->pdata->gpio_convst, 1);
46
47 ret = wait_event_interruptible(st->wq_data_avail, st->done);
48 if (ret)
49 goto error_ret;
50
51 if (gpio_is_valid(st->pdata->gpio_frstdata)) {
52 ret = st->bops->read_block(st->dev, 1, st->data);
53 if (ret)
54 goto error_ret;
55 if (!gpio_get_value(st->pdata->gpio_frstdata)) {
56 /* This should never happen */
57 ad7606_reset(st);
58 ret = -EIO;
59 goto error_ret;
60 }
61 ret = st->bops->read_block(st->dev,
62 st->chip_info->num_channels - 1, &st->data[1]);
63 if (ret)
64 goto error_ret;
65 } else {
66 ret = st->bops->read_block(st->dev,
67 st->chip_info->num_channels, st->data);
68 if (ret)
69 goto error_ret;
70 }
71
72 ret = st->data[ch];
73
74 error_ret:
75 gpio_set_value(st->pdata->gpio_convst, 0);
76
77 return ret;
78 }
79
ad7606_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)80 static int ad7606_read_raw(struct iio_dev *indio_dev,
81 struct iio_chan_spec const *chan,
82 int *val,
83 int *val2,
84 long m)
85 {
86 int ret;
87 struct ad7606_state *st = iio_priv(indio_dev);
88 unsigned int scale_uv;
89
90 switch (m) {
91 case 0:
92 mutex_lock(&indio_dev->mlock);
93 if (iio_buffer_enabled(indio_dev))
94 ret = -EBUSY;
95 else
96 ret = ad7606_scan_direct(indio_dev, chan->address);
97 mutex_unlock(&indio_dev->mlock);
98
99 if (ret < 0)
100 return ret;
101 *val = (short) ret;
102 return IIO_VAL_INT;
103 case IIO_CHAN_INFO_SCALE:
104 scale_uv = (st->range * 1000 * 2)
105 >> st->chip_info->channels[0].scan_type.realbits;
106 *val = scale_uv / 1000;
107 *val2 = (scale_uv % 1000) * 1000;
108 return IIO_VAL_INT_PLUS_MICRO;
109 }
110 return -EINVAL;
111 }
112
ad7606_show_range(struct device * dev,struct device_attribute * attr,char * buf)113 static ssize_t ad7606_show_range(struct device *dev,
114 struct device_attribute *attr, char *buf)
115 {
116 struct iio_dev *indio_dev = dev_get_drvdata(dev);
117 struct ad7606_state *st = iio_priv(indio_dev);
118
119 return sprintf(buf, "%u\n", st->range);
120 }
121
ad7606_store_range(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)122 static ssize_t ad7606_store_range(struct device *dev,
123 struct device_attribute *attr, const char *buf, size_t count)
124 {
125 struct iio_dev *indio_dev = dev_get_drvdata(dev);
126 struct ad7606_state *st = iio_priv(indio_dev);
127 unsigned long lval;
128
129 if (strict_strtoul(buf, 10, &lval))
130 return -EINVAL;
131 if (!(lval == 5000 || lval == 10000)) {
132 dev_err(dev, "range is not supported\n");
133 return -EINVAL;
134 }
135 mutex_lock(&indio_dev->mlock);
136 gpio_set_value(st->pdata->gpio_range, lval == 10000);
137 st->range = lval;
138 mutex_unlock(&indio_dev->mlock);
139
140 return count;
141 }
142
143 static IIO_DEVICE_ATTR(in_voltage_range, S_IRUGO | S_IWUSR, \
144 ad7606_show_range, ad7606_store_range, 0);
145 static IIO_CONST_ATTR(in_voltage_range_available, "5000 10000");
146
ad7606_show_oversampling_ratio(struct device * dev,struct device_attribute * attr,char * buf)147 static ssize_t ad7606_show_oversampling_ratio(struct device *dev,
148 struct device_attribute *attr, char *buf)
149 {
150 struct iio_dev *indio_dev = dev_get_drvdata(dev);
151 struct ad7606_state *st = iio_priv(indio_dev);
152
153 return sprintf(buf, "%u\n", st->oversampling);
154 }
155
ad7606_oversampling_get_index(unsigned val)156 static int ad7606_oversampling_get_index(unsigned val)
157 {
158 unsigned char supported[] = {0, 2, 4, 8, 16, 32, 64};
159 int i;
160
161 for (i = 0; i < ARRAY_SIZE(supported); i++)
162 if (val == supported[i])
163 return i;
164
165 return -EINVAL;
166 }
167
ad7606_store_oversampling_ratio(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)168 static ssize_t ad7606_store_oversampling_ratio(struct device *dev,
169 struct device_attribute *attr, const char *buf, size_t count)
170 {
171 struct iio_dev *indio_dev = dev_get_drvdata(dev);
172 struct ad7606_state *st = iio_priv(indio_dev);
173 unsigned long lval;
174 int ret;
175
176 if (strict_strtoul(buf, 10, &lval))
177 return -EINVAL;
178
179 ret = ad7606_oversampling_get_index(lval);
180 if (ret < 0) {
181 dev_err(dev, "oversampling %lu is not supported\n", lval);
182 return ret;
183 }
184
185 mutex_lock(&indio_dev->mlock);
186 gpio_set_value(st->pdata->gpio_os0, (ret >> 0) & 1);
187 gpio_set_value(st->pdata->gpio_os1, (ret >> 1) & 1);
188 gpio_set_value(st->pdata->gpio_os1, (ret >> 2) & 1);
189 st->oversampling = lval;
190 mutex_unlock(&indio_dev->mlock);
191
192 return count;
193 }
194
195 static IIO_DEVICE_ATTR(oversampling_ratio, S_IRUGO | S_IWUSR,
196 ad7606_show_oversampling_ratio,
197 ad7606_store_oversampling_ratio, 0);
198 static IIO_CONST_ATTR(oversampling_ratio_available, "0 2 4 8 16 32 64");
199
200 static struct attribute *ad7606_attributes_os_and_range[] = {
201 &iio_dev_attr_in_voltage_range.dev_attr.attr,
202 &iio_const_attr_in_voltage_range_available.dev_attr.attr,
203 &iio_dev_attr_oversampling_ratio.dev_attr.attr,
204 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
205 NULL,
206 };
207
208 static const struct attribute_group ad7606_attribute_group_os_and_range = {
209 .attrs = ad7606_attributes_os_and_range,
210 };
211
212 static struct attribute *ad7606_attributes_os[] = {
213 &iio_dev_attr_oversampling_ratio.dev_attr.attr,
214 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
215 NULL,
216 };
217
218 static const struct attribute_group ad7606_attribute_group_os = {
219 .attrs = ad7606_attributes_os,
220 };
221
222 static struct attribute *ad7606_attributes_range[] = {
223 &iio_dev_attr_in_voltage_range.dev_attr.attr,
224 &iio_const_attr_in_voltage_range_available.dev_attr.attr,
225 NULL,
226 };
227
228 static const struct attribute_group ad7606_attribute_group_range = {
229 .attrs = ad7606_attributes_range,
230 };
231
232 #define AD7606_CHANNEL(num) \
233 { \
234 .type = IIO_VOLTAGE, \
235 .indexed = 1, \
236 .channel = num, \
237 .address = num, \
238 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \
239 .scan_index = num, \
240 .scan_type = IIO_ST('s', 16, 16, 0), \
241 }
242
243 static struct iio_chan_spec ad7606_8_channels[] = {
244 AD7606_CHANNEL(0),
245 AD7606_CHANNEL(1),
246 AD7606_CHANNEL(2),
247 AD7606_CHANNEL(3),
248 AD7606_CHANNEL(4),
249 AD7606_CHANNEL(5),
250 AD7606_CHANNEL(6),
251 AD7606_CHANNEL(7),
252 IIO_CHAN_SOFT_TIMESTAMP(8),
253 };
254
255 static struct iio_chan_spec ad7606_6_channels[] = {
256 AD7606_CHANNEL(0),
257 AD7606_CHANNEL(1),
258 AD7606_CHANNEL(2),
259 AD7606_CHANNEL(3),
260 AD7606_CHANNEL(4),
261 AD7606_CHANNEL(5),
262 IIO_CHAN_SOFT_TIMESTAMP(6),
263 };
264
265 static struct iio_chan_spec ad7606_4_channels[] = {
266 AD7606_CHANNEL(0),
267 AD7606_CHANNEL(1),
268 AD7606_CHANNEL(2),
269 AD7606_CHANNEL(3),
270 IIO_CHAN_SOFT_TIMESTAMP(4),
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 .name = "ad7606",
279 .int_vref_mv = 2500,
280 .channels = ad7606_8_channels,
281 .num_channels = 8,
282 },
283 [ID_AD7606_6] = {
284 .name = "ad7606-6",
285 .int_vref_mv = 2500,
286 .channels = ad7606_6_channels,
287 .num_channels = 6,
288 },
289 [ID_AD7606_4] = {
290 .name = "ad7606-4",
291 .int_vref_mv = 2500,
292 .channels = ad7606_4_channels,
293 .num_channels = 4,
294 },
295 };
296
ad7606_request_gpios(struct ad7606_state * st)297 static int ad7606_request_gpios(struct ad7606_state *st)
298 {
299 struct gpio gpio_array[3] = {
300 [0] = {
301 .gpio = st->pdata->gpio_os0,
302 .flags = GPIOF_DIR_OUT | ((st->oversampling & 1) ?
303 GPIOF_INIT_HIGH : GPIOF_INIT_LOW),
304 .label = "AD7606_OS0",
305 },
306 [1] = {
307 .gpio = st->pdata->gpio_os1,
308 .flags = GPIOF_DIR_OUT | ((st->oversampling & 2) ?
309 GPIOF_INIT_HIGH : GPIOF_INIT_LOW),
310 .label = "AD7606_OS1",
311 },
312 [2] = {
313 .gpio = st->pdata->gpio_os2,
314 .flags = GPIOF_DIR_OUT | ((st->oversampling & 4) ?
315 GPIOF_INIT_HIGH : GPIOF_INIT_LOW),
316 .label = "AD7606_OS2",
317 },
318 };
319 int ret;
320
321 if (gpio_is_valid(st->pdata->gpio_convst)) {
322 ret = gpio_request_one(st->pdata->gpio_convst,
323 GPIOF_OUT_INIT_LOW,
324 "AD7606_CONVST");
325 if (ret) {
326 dev_err(st->dev, "failed to request GPIO CONVST\n");
327 goto error_ret;
328 }
329 } else {
330 ret = -EIO;
331 goto error_ret;
332 }
333
334 if (gpio_is_valid(st->pdata->gpio_os0) &&
335 gpio_is_valid(st->pdata->gpio_os1) &&
336 gpio_is_valid(st->pdata->gpio_os2)) {
337 ret = gpio_request_array(gpio_array, ARRAY_SIZE(gpio_array));
338 if (ret < 0)
339 goto error_free_convst;
340 }
341
342 if (gpio_is_valid(st->pdata->gpio_reset)) {
343 ret = gpio_request_one(st->pdata->gpio_reset,
344 GPIOF_OUT_INIT_LOW,
345 "AD7606_RESET");
346 if (ret < 0)
347 goto error_free_os;
348 }
349
350 if (gpio_is_valid(st->pdata->gpio_range)) {
351 ret = gpio_request_one(st->pdata->gpio_range, GPIOF_DIR_OUT |
352 ((st->range == 10000) ? GPIOF_INIT_HIGH :
353 GPIOF_INIT_LOW), "AD7606_RANGE");
354 if (ret < 0)
355 goto error_free_reset;
356 }
357 if (gpio_is_valid(st->pdata->gpio_stby)) {
358 ret = gpio_request_one(st->pdata->gpio_stby,
359 GPIOF_OUT_INIT_HIGH,
360 "AD7606_STBY");
361 if (ret < 0)
362 goto error_free_range;
363 }
364
365 if (gpio_is_valid(st->pdata->gpio_frstdata)) {
366 ret = gpio_request_one(st->pdata->gpio_frstdata, GPIOF_IN,
367 "AD7606_FRSTDATA");
368 if (ret < 0)
369 goto error_free_stby;
370 }
371
372 return 0;
373
374 error_free_stby:
375 if (gpio_is_valid(st->pdata->gpio_stby))
376 gpio_free(st->pdata->gpio_stby);
377 error_free_range:
378 if (gpio_is_valid(st->pdata->gpio_range))
379 gpio_free(st->pdata->gpio_range);
380 error_free_reset:
381 if (gpio_is_valid(st->pdata->gpio_reset))
382 gpio_free(st->pdata->gpio_reset);
383 error_free_os:
384 if (gpio_is_valid(st->pdata->gpio_os0) &&
385 gpio_is_valid(st->pdata->gpio_os1) &&
386 gpio_is_valid(st->pdata->gpio_os2))
387 gpio_free_array(gpio_array, ARRAY_SIZE(gpio_array));
388 error_free_convst:
389 gpio_free(st->pdata->gpio_convst);
390 error_ret:
391 return ret;
392 }
393
ad7606_free_gpios(struct ad7606_state * st)394 static void ad7606_free_gpios(struct ad7606_state *st)
395 {
396 if (gpio_is_valid(st->pdata->gpio_frstdata))
397 gpio_free(st->pdata->gpio_frstdata);
398 if (gpio_is_valid(st->pdata->gpio_stby))
399 gpio_free(st->pdata->gpio_stby);
400 if (gpio_is_valid(st->pdata->gpio_range))
401 gpio_free(st->pdata->gpio_range);
402 if (gpio_is_valid(st->pdata->gpio_reset))
403 gpio_free(st->pdata->gpio_reset);
404 if (gpio_is_valid(st->pdata->gpio_os0) &&
405 gpio_is_valid(st->pdata->gpio_os1) &&
406 gpio_is_valid(st->pdata->gpio_os2)) {
407 gpio_free(st->pdata->gpio_os2);
408 gpio_free(st->pdata->gpio_os1);
409 gpio_free(st->pdata->gpio_os0);
410 }
411 gpio_free(st->pdata->gpio_convst);
412 }
413
414 /**
415 * Interrupt handler
416 */
ad7606_interrupt(int irq,void * dev_id)417 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
418 {
419 struct iio_dev *indio_dev = dev_id;
420 struct ad7606_state *st = iio_priv(indio_dev);
421
422 if (iio_buffer_enabled(indio_dev)) {
423 if (!work_pending(&st->poll_work))
424 schedule_work(&st->poll_work);
425 } else {
426 st->done = true;
427 wake_up_interruptible(&st->wq_data_avail);
428 }
429
430 return IRQ_HANDLED;
431 };
432
433 static const struct iio_info ad7606_info_no_os_or_range = {
434 .driver_module = THIS_MODULE,
435 .read_raw = &ad7606_read_raw,
436 };
437
438 static const struct iio_info ad7606_info_os_and_range = {
439 .driver_module = THIS_MODULE,
440 .read_raw = &ad7606_read_raw,
441 .attrs = &ad7606_attribute_group_os_and_range,
442 };
443
444 static const struct iio_info ad7606_info_os = {
445 .driver_module = THIS_MODULE,
446 .read_raw = &ad7606_read_raw,
447 .attrs = &ad7606_attribute_group_os,
448 };
449
450 static const struct iio_info ad7606_info_range = {
451 .driver_module = THIS_MODULE,
452 .read_raw = &ad7606_read_raw,
453 .attrs = &ad7606_attribute_group_range,
454 };
455
ad7606_probe(struct device * dev,int irq,void __iomem * base_address,unsigned id,const struct ad7606_bus_ops * bops)456 struct iio_dev *ad7606_probe(struct device *dev, int irq,
457 void __iomem *base_address,
458 unsigned id,
459 const struct ad7606_bus_ops *bops)
460 {
461 struct ad7606_platform_data *pdata = dev->platform_data;
462 struct ad7606_state *st;
463 int ret;
464 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
465
466 if (indio_dev == NULL) {
467 ret = -ENOMEM;
468 goto error_ret;
469 }
470
471 st = iio_priv(indio_dev);
472
473 st->dev = dev;
474 st->bops = bops;
475 st->base_address = base_address;
476 st->range = pdata->default_range == 10000 ? 10000 : 5000;
477
478 ret = ad7606_oversampling_get_index(pdata->default_os);
479 if (ret < 0) {
480 dev_warn(dev, "oversampling %d is not supported\n",
481 pdata->default_os);
482 st->oversampling = 0;
483 } else {
484 st->oversampling = pdata->default_os;
485 }
486
487 st->reg = regulator_get(dev, "vcc");
488 if (!IS_ERR(st->reg)) {
489 ret = regulator_enable(st->reg);
490 if (ret)
491 goto error_put_reg;
492 }
493
494 st->pdata = pdata;
495 st->chip_info = &ad7606_chip_info_tbl[id];
496
497 indio_dev->dev.parent = dev;
498 if (gpio_is_valid(st->pdata->gpio_os0) &&
499 gpio_is_valid(st->pdata->gpio_os1) &&
500 gpio_is_valid(st->pdata->gpio_os2)) {
501 if (gpio_is_valid(st->pdata->gpio_range))
502 indio_dev->info = &ad7606_info_os_and_range;
503 else
504 indio_dev->info = &ad7606_info_os;
505 } else {
506 if (gpio_is_valid(st->pdata->gpio_range))
507 indio_dev->info = &ad7606_info_range;
508 else
509 indio_dev->info = &ad7606_info_no_os_or_range;
510 }
511 indio_dev->modes = INDIO_DIRECT_MODE;
512 indio_dev->name = st->chip_info->name;
513 indio_dev->channels = st->chip_info->channels;
514 indio_dev->num_channels = st->chip_info->num_channels;
515
516 init_waitqueue_head(&st->wq_data_avail);
517
518 ret = ad7606_request_gpios(st);
519 if (ret)
520 goto error_disable_reg;
521
522 ret = ad7606_reset(st);
523 if (ret)
524 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
525
526 ret = request_irq(irq, ad7606_interrupt,
527 IRQF_TRIGGER_FALLING, st->chip_info->name, indio_dev);
528 if (ret)
529 goto error_free_gpios;
530
531 ret = ad7606_register_ring_funcs_and_init(indio_dev);
532 if (ret)
533 goto error_free_irq;
534
535 ret = iio_buffer_register(indio_dev,
536 indio_dev->channels,
537 indio_dev->num_channels);
538 if (ret)
539 goto error_cleanup_ring;
540 ret = iio_device_register(indio_dev);
541 if (ret)
542 goto error_unregister_ring;
543
544 return indio_dev;
545 error_unregister_ring:
546 iio_buffer_unregister(indio_dev);
547
548 error_cleanup_ring:
549 ad7606_ring_cleanup(indio_dev);
550
551 error_free_irq:
552 free_irq(irq, indio_dev);
553
554 error_free_gpios:
555 ad7606_free_gpios(st);
556
557 error_disable_reg:
558 if (!IS_ERR(st->reg))
559 regulator_disable(st->reg);
560 error_put_reg:
561 if (!IS_ERR(st->reg))
562 regulator_put(st->reg);
563 iio_free_device(indio_dev);
564 error_ret:
565 return ERR_PTR(ret);
566 }
567
ad7606_remove(struct iio_dev * indio_dev,int irq)568 int ad7606_remove(struct iio_dev *indio_dev, int irq)
569 {
570 struct ad7606_state *st = iio_priv(indio_dev);
571
572 iio_device_unregister(indio_dev);
573 iio_buffer_unregister(indio_dev);
574 ad7606_ring_cleanup(indio_dev);
575
576 free_irq(irq, indio_dev);
577 if (!IS_ERR(st->reg)) {
578 regulator_disable(st->reg);
579 regulator_put(st->reg);
580 }
581
582 ad7606_free_gpios(st);
583 iio_free_device(indio_dev);
584
585 return 0;
586 }
587
ad7606_suspend(struct iio_dev * indio_dev)588 void ad7606_suspend(struct iio_dev *indio_dev)
589 {
590 struct ad7606_state *st = iio_priv(indio_dev);
591
592 if (gpio_is_valid(st->pdata->gpio_stby)) {
593 if (gpio_is_valid(st->pdata->gpio_range))
594 gpio_set_value(st->pdata->gpio_range, 1);
595 gpio_set_value(st->pdata->gpio_stby, 0);
596 }
597 }
598
ad7606_resume(struct iio_dev * indio_dev)599 void ad7606_resume(struct iio_dev *indio_dev)
600 {
601 struct ad7606_state *st = iio_priv(indio_dev);
602
603 if (gpio_is_valid(st->pdata->gpio_stby)) {
604 if (gpio_is_valid(st->pdata->gpio_range))
605 gpio_set_value(st->pdata->gpio_range,
606 st->range == 10000);
607
608 gpio_set_value(st->pdata->gpio_stby, 1);
609 ad7606_reset(st);
610 }
611 }
612
613 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
614 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
615 MODULE_LICENSE("GPL v2");
616