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 <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/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 IIO_CHAN_INFO_RAW:
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_to_iio_dev(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_to_iio_dev(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_to_iio_dev(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_to_iio_dev(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_separate = BIT(IIO_CHAN_INFO_RAW), \
239 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
240 .scan_index = num, \
241 .scan_type = IIO_ST('s', 16, 16, 0), \
242 }
243
244 static const struct iio_chan_spec ad7606_8_channels[] = {
245 AD7606_CHANNEL(0),
246 AD7606_CHANNEL(1),
247 AD7606_CHANNEL(2),
248 AD7606_CHANNEL(3),
249 AD7606_CHANNEL(4),
250 AD7606_CHANNEL(5),
251 AD7606_CHANNEL(6),
252 AD7606_CHANNEL(7),
253 IIO_CHAN_SOFT_TIMESTAMP(8),
254 };
255
256 static const struct iio_chan_spec ad7606_6_channels[] = {
257 AD7606_CHANNEL(0),
258 AD7606_CHANNEL(1),
259 AD7606_CHANNEL(2),
260 AD7606_CHANNEL(3),
261 AD7606_CHANNEL(4),
262 AD7606_CHANNEL(5),
263 IIO_CHAN_SOFT_TIMESTAMP(6),
264 };
265
266 static const struct iio_chan_spec ad7606_4_channels[] = {
267 AD7606_CHANNEL(0),
268 AD7606_CHANNEL(1),
269 AD7606_CHANNEL(2),
270 AD7606_CHANNEL(3),
271 IIO_CHAN_SOFT_TIMESTAMP(4),
272 };
273
274 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
275 /*
276 * More devices added in future
277 */
278 [ID_AD7606_8] = {
279 .name = "ad7606",
280 .int_vref_mv = 2500,
281 .channels = ad7606_8_channels,
282 .num_channels = 8,
283 },
284 [ID_AD7606_6] = {
285 .name = "ad7606-6",
286 .int_vref_mv = 2500,
287 .channels = ad7606_6_channels,
288 .num_channels = 6,
289 },
290 [ID_AD7606_4] = {
291 .name = "ad7606-4",
292 .int_vref_mv = 2500,
293 .channels = ad7606_4_channels,
294 .num_channels = 4,
295 },
296 };
297
ad7606_request_gpios(struct ad7606_state * st)298 static int ad7606_request_gpios(struct ad7606_state *st)
299 {
300 struct gpio gpio_array[3] = {
301 [0] = {
302 .gpio = st->pdata->gpio_os0,
303 .flags = GPIOF_DIR_OUT | ((st->oversampling & 1) ?
304 GPIOF_INIT_HIGH : GPIOF_INIT_LOW),
305 .label = "AD7606_OS0",
306 },
307 [1] = {
308 .gpio = st->pdata->gpio_os1,
309 .flags = GPIOF_DIR_OUT | ((st->oversampling & 2) ?
310 GPIOF_INIT_HIGH : GPIOF_INIT_LOW),
311 .label = "AD7606_OS1",
312 },
313 [2] = {
314 .gpio = st->pdata->gpio_os2,
315 .flags = GPIOF_DIR_OUT | ((st->oversampling & 4) ?
316 GPIOF_INIT_HIGH : GPIOF_INIT_LOW),
317 .label = "AD7606_OS2",
318 },
319 };
320 int ret;
321
322 if (gpio_is_valid(st->pdata->gpio_convst)) {
323 ret = gpio_request_one(st->pdata->gpio_convst,
324 GPIOF_OUT_INIT_LOW,
325 "AD7606_CONVST");
326 if (ret) {
327 dev_err(st->dev, "failed to request GPIO CONVST\n");
328 goto error_ret;
329 }
330 } else {
331 ret = -EIO;
332 goto error_ret;
333 }
334
335 if (gpio_is_valid(st->pdata->gpio_os0) &&
336 gpio_is_valid(st->pdata->gpio_os1) &&
337 gpio_is_valid(st->pdata->gpio_os2)) {
338 ret = gpio_request_array(gpio_array, ARRAY_SIZE(gpio_array));
339 if (ret < 0)
340 goto error_free_convst;
341 }
342
343 if (gpio_is_valid(st->pdata->gpio_reset)) {
344 ret = gpio_request_one(st->pdata->gpio_reset,
345 GPIOF_OUT_INIT_LOW,
346 "AD7606_RESET");
347 if (ret < 0)
348 goto error_free_os;
349 }
350
351 if (gpio_is_valid(st->pdata->gpio_range)) {
352 ret = gpio_request_one(st->pdata->gpio_range, GPIOF_DIR_OUT |
353 ((st->range == 10000) ? GPIOF_INIT_HIGH :
354 GPIOF_INIT_LOW), "AD7606_RANGE");
355 if (ret < 0)
356 goto error_free_reset;
357 }
358 if (gpio_is_valid(st->pdata->gpio_stby)) {
359 ret = gpio_request_one(st->pdata->gpio_stby,
360 GPIOF_OUT_INIT_HIGH,
361 "AD7606_STBY");
362 if (ret < 0)
363 goto error_free_range;
364 }
365
366 if (gpio_is_valid(st->pdata->gpio_frstdata)) {
367 ret = gpio_request_one(st->pdata->gpio_frstdata, GPIOF_IN,
368 "AD7606_FRSTDATA");
369 if (ret < 0)
370 goto error_free_stby;
371 }
372
373 return 0;
374
375 error_free_stby:
376 if (gpio_is_valid(st->pdata->gpio_stby))
377 gpio_free(st->pdata->gpio_stby);
378 error_free_range:
379 if (gpio_is_valid(st->pdata->gpio_range))
380 gpio_free(st->pdata->gpio_range);
381 error_free_reset:
382 if (gpio_is_valid(st->pdata->gpio_reset))
383 gpio_free(st->pdata->gpio_reset);
384 error_free_os:
385 if (gpio_is_valid(st->pdata->gpio_os0) &&
386 gpio_is_valid(st->pdata->gpio_os1) &&
387 gpio_is_valid(st->pdata->gpio_os2))
388 gpio_free_array(gpio_array, ARRAY_SIZE(gpio_array));
389 error_free_convst:
390 gpio_free(st->pdata->gpio_convst);
391 error_ret:
392 return ret;
393 }
394
ad7606_free_gpios(struct ad7606_state * st)395 static void ad7606_free_gpios(struct ad7606_state *st)
396 {
397 if (gpio_is_valid(st->pdata->gpio_frstdata))
398 gpio_free(st->pdata->gpio_frstdata);
399 if (gpio_is_valid(st->pdata->gpio_stby))
400 gpio_free(st->pdata->gpio_stby);
401 if (gpio_is_valid(st->pdata->gpio_range))
402 gpio_free(st->pdata->gpio_range);
403 if (gpio_is_valid(st->pdata->gpio_reset))
404 gpio_free(st->pdata->gpio_reset);
405 if (gpio_is_valid(st->pdata->gpio_os0) &&
406 gpio_is_valid(st->pdata->gpio_os1) &&
407 gpio_is_valid(st->pdata->gpio_os2)) {
408 gpio_free(st->pdata->gpio_os2);
409 gpio_free(st->pdata->gpio_os1);
410 gpio_free(st->pdata->gpio_os0);
411 }
412 gpio_free(st->pdata->gpio_convst);
413 }
414
415 /**
416 * Interrupt handler
417 */
ad7606_interrupt(int irq,void * dev_id)418 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
419 {
420 struct iio_dev *indio_dev = dev_id;
421 struct ad7606_state *st = iio_priv(indio_dev);
422
423 if (iio_buffer_enabled(indio_dev)) {
424 if (!work_pending(&st->poll_work))
425 schedule_work(&st->poll_work);
426 } else {
427 st->done = true;
428 wake_up_interruptible(&st->wq_data_avail);
429 }
430
431 return IRQ_HANDLED;
432 };
433
434 static const struct iio_info ad7606_info_no_os_or_range = {
435 .driver_module = THIS_MODULE,
436 .read_raw = &ad7606_read_raw,
437 };
438
439 static const struct iio_info ad7606_info_os_and_range = {
440 .driver_module = THIS_MODULE,
441 .read_raw = &ad7606_read_raw,
442 .attrs = &ad7606_attribute_group_os_and_range,
443 };
444
445 static const struct iio_info ad7606_info_os = {
446 .driver_module = THIS_MODULE,
447 .read_raw = &ad7606_read_raw,
448 .attrs = &ad7606_attribute_group_os,
449 };
450
451 static const struct iio_info ad7606_info_range = {
452 .driver_module = THIS_MODULE,
453 .read_raw = &ad7606_read_raw,
454 .attrs = &ad7606_attribute_group_range,
455 };
456
ad7606_probe(struct device * dev,int irq,void __iomem * base_address,unsigned id,const struct ad7606_bus_ops * bops)457 struct iio_dev *ad7606_probe(struct device *dev, int irq,
458 void __iomem *base_address,
459 unsigned id,
460 const struct ad7606_bus_ops *bops)
461 {
462 struct ad7606_platform_data *pdata = dev->platform_data;
463 struct ad7606_state *st;
464 int ret;
465 struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
466
467 if (indio_dev == NULL) {
468 ret = -ENOMEM;
469 goto error_ret;
470 }
471
472 st = iio_priv(indio_dev);
473
474 st->dev = dev;
475 st->bops = bops;
476 st->base_address = base_address;
477 st->range = pdata->default_range == 10000 ? 10000 : 5000;
478
479 ret = ad7606_oversampling_get_index(pdata->default_os);
480 if (ret < 0) {
481 dev_warn(dev, "oversampling %d is not supported\n",
482 pdata->default_os);
483 st->oversampling = 0;
484 } else {
485 st->oversampling = pdata->default_os;
486 }
487
488 st->reg = regulator_get(dev, "vcc");
489 if (!IS_ERR(st->reg)) {
490 ret = regulator_enable(st->reg);
491 if (ret)
492 goto error_put_reg;
493 }
494
495 st->pdata = pdata;
496 st->chip_info = &ad7606_chip_info_tbl[id];
497
498 indio_dev->dev.parent = dev;
499 if (gpio_is_valid(st->pdata->gpio_os0) &&
500 gpio_is_valid(st->pdata->gpio_os1) &&
501 gpio_is_valid(st->pdata->gpio_os2)) {
502 if (gpio_is_valid(st->pdata->gpio_range))
503 indio_dev->info = &ad7606_info_os_and_range;
504 else
505 indio_dev->info = &ad7606_info_os;
506 } else {
507 if (gpio_is_valid(st->pdata->gpio_range))
508 indio_dev->info = &ad7606_info_range;
509 else
510 indio_dev->info = &ad7606_info_no_os_or_range;
511 }
512 indio_dev->modes = INDIO_DIRECT_MODE;
513 indio_dev->name = st->chip_info->name;
514 indio_dev->channels = st->chip_info->channels;
515 indio_dev->num_channels = st->chip_info->num_channels;
516
517 init_waitqueue_head(&st->wq_data_avail);
518
519 ret = ad7606_request_gpios(st);
520 if (ret)
521 goto error_disable_reg;
522
523 ret = ad7606_reset(st);
524 if (ret)
525 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
526
527 ret = request_irq(irq, ad7606_interrupt,
528 IRQF_TRIGGER_FALLING, st->chip_info->name, indio_dev);
529 if (ret)
530 goto error_free_gpios;
531
532 ret = ad7606_register_ring_funcs_and_init(indio_dev);
533 if (ret)
534 goto error_free_irq;
535
536 ret = iio_device_register(indio_dev);
537 if (ret)
538 goto error_unregister_ring;
539
540 return indio_dev;
541 error_unregister_ring:
542 ad7606_ring_cleanup(indio_dev);
543
544 error_free_irq:
545 free_irq(irq, indio_dev);
546
547 error_free_gpios:
548 ad7606_free_gpios(st);
549
550 error_disable_reg:
551 if (!IS_ERR(st->reg))
552 regulator_disable(st->reg);
553 error_put_reg:
554 if (!IS_ERR(st->reg))
555 regulator_put(st->reg);
556 iio_device_free(indio_dev);
557 error_ret:
558 return ERR_PTR(ret);
559 }
560
ad7606_remove(struct iio_dev * indio_dev,int irq)561 int ad7606_remove(struct iio_dev *indio_dev, int irq)
562 {
563 struct ad7606_state *st = iio_priv(indio_dev);
564
565 iio_device_unregister(indio_dev);
566 ad7606_ring_cleanup(indio_dev);
567
568 free_irq(irq, indio_dev);
569 if (!IS_ERR(st->reg)) {
570 regulator_disable(st->reg);
571 regulator_put(st->reg);
572 }
573
574 ad7606_free_gpios(st);
575 iio_device_free(indio_dev);
576
577 return 0;
578 }
579
ad7606_suspend(struct iio_dev * indio_dev)580 void ad7606_suspend(struct iio_dev *indio_dev)
581 {
582 struct ad7606_state *st = iio_priv(indio_dev);
583
584 if (gpio_is_valid(st->pdata->gpio_stby)) {
585 if (gpio_is_valid(st->pdata->gpio_range))
586 gpio_set_value(st->pdata->gpio_range, 1);
587 gpio_set_value(st->pdata->gpio_stby, 0);
588 }
589 }
590
ad7606_resume(struct iio_dev * indio_dev)591 void ad7606_resume(struct iio_dev *indio_dev)
592 {
593 struct ad7606_state *st = iio_priv(indio_dev);
594
595 if (gpio_is_valid(st->pdata->gpio_stby)) {
596 if (gpio_is_valid(st->pdata->gpio_range))
597 gpio_set_value(st->pdata->gpio_range,
598 st->range == 10000);
599
600 gpio_set_value(st->pdata->gpio_stby, 1);
601 ad7606_reset(st);
602 }
603 }
604
605 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
606 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
607 MODULE_LICENSE("GPL v2");
608