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