xref: /linux/drivers/iio/adc/ad7606.c (revision be709d48329a500621d2a05835283150ae137b45)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * AD7606 SPI ADC driver
4  *
5  * Copyright 2011 Analog Devices Inc.
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/util_macros.h>
20 
21 #include <linux/iio/iio.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/iio/trigger_consumer.h>
27 
28 #include "ad7606.h"
29 
30 /*
31  * Scales are computed as 5000/32768 and 10000/32768 respectively,
32  * so that when applied to the raw values they provide mV values
33  */
34 static const unsigned int scale_avail[2] = {
35 	152588, 305176
36 };
37 
38 static const unsigned int ad7606_oversampling_avail[7] = {
39 	1, 2, 4, 8, 16, 32, 64,
40 };
41 
42 static int ad7606_reset(struct ad7606_state *st)
43 {
44 	if (st->gpio_reset) {
45 		gpiod_set_value(st->gpio_reset, 1);
46 		ndelay(100); /* t_reset >= 100ns */
47 		gpiod_set_value(st->gpio_reset, 0);
48 		return 0;
49 	}
50 
51 	return -ENODEV;
52 }
53 
54 static int ad7606_read_samples(struct ad7606_state *st)
55 {
56 	unsigned int num = st->chip_info->num_channels;
57 	u16 *data = st->data;
58 	int ret;
59 
60 	/*
61 	 * The frstdata signal is set to high while and after reading the sample
62 	 * of the first channel and low for all other channels. This can be used
63 	 * to check that the incoming data is correctly aligned. During normal
64 	 * operation the data should never become unaligned, but some glitch or
65 	 * electrostatic discharge might cause an extra read or clock cycle.
66 	 * Monitoring the frstdata signal allows to recover from such failure
67 	 * situations.
68 	 */
69 
70 	if (st->gpio_frstdata) {
71 		ret = st->bops->read_block(st->dev, 1, data);
72 		if (ret)
73 			return ret;
74 
75 		if (!gpiod_get_value(st->gpio_frstdata)) {
76 			ad7606_reset(st);
77 			return -EIO;
78 		}
79 
80 		data++;
81 		num--;
82 	}
83 
84 	return st->bops->read_block(st->dev, num, data);
85 }
86 
87 static irqreturn_t ad7606_trigger_handler(int irq, void *p)
88 {
89 	struct iio_poll_func *pf = p;
90 	struct iio_dev *indio_dev = pf->indio_dev;
91 	struct ad7606_state *st = iio_priv(indio_dev);
92 	int ret;
93 
94 	mutex_lock(&st->lock);
95 
96 	ret = ad7606_read_samples(st);
97 	if (ret == 0)
98 		iio_push_to_buffers_with_timestamp(indio_dev, st->data,
99 						   iio_get_time_ns(indio_dev));
100 
101 	iio_trigger_notify_done(indio_dev->trig);
102 	/* The rising edge of the CONVST signal starts a new conversion. */
103 	gpiod_set_value(st->gpio_convst, 1);
104 
105 	mutex_unlock(&st->lock);
106 
107 	return IRQ_HANDLED;
108 }
109 
110 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
111 {
112 	struct ad7606_state *st = iio_priv(indio_dev);
113 	int ret;
114 
115 	gpiod_set_value(st->gpio_convst, 1);
116 	ret = wait_for_completion_timeout(&st->completion,
117 					  msecs_to_jiffies(1000));
118 	if (!ret) {
119 		ret = -ETIMEDOUT;
120 		goto error_ret;
121 	}
122 
123 	ret = ad7606_read_samples(st);
124 	if (ret == 0)
125 		ret = st->data[ch];
126 
127 error_ret:
128 	gpiod_set_value(st->gpio_convst, 0);
129 
130 	return ret;
131 }
132 
133 static int ad7606_read_raw(struct iio_dev *indio_dev,
134 			   struct iio_chan_spec const *chan,
135 			   int *val,
136 			   int *val2,
137 			   long m)
138 {
139 	int ret;
140 	struct ad7606_state *st = iio_priv(indio_dev);
141 
142 	switch (m) {
143 	case IIO_CHAN_INFO_RAW:
144 		ret = iio_device_claim_direct_mode(indio_dev);
145 		if (ret)
146 			return ret;
147 
148 		ret = ad7606_scan_direct(indio_dev, chan->address);
149 		iio_device_release_direct_mode(indio_dev);
150 
151 		if (ret < 0)
152 			return ret;
153 		*val = (short)ret;
154 		return IIO_VAL_INT;
155 	case IIO_CHAN_INFO_SCALE:
156 		*val = 0;
157 		*val2 = scale_avail[st->range];
158 		return IIO_VAL_INT_PLUS_MICRO;
159 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
160 		*val = st->oversampling;
161 		return IIO_VAL_INT;
162 	}
163 	return -EINVAL;
164 }
165 
166 static ssize_t in_voltage_scale_available_show(struct device *dev,
167 					       struct device_attribute *attr,
168 					       char *buf)
169 {
170 	int i, len = 0;
171 
172 	for (i = 0; i < ARRAY_SIZE(scale_avail); i++)
173 		len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ",
174 				 scale_avail[i]);
175 
176 	buf[len - 1] = '\n';
177 
178 	return len;
179 }
180 
181 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
182 
183 static int ad7606_write_raw(struct iio_dev *indio_dev,
184 			    struct iio_chan_spec const *chan,
185 			    int val,
186 			    int val2,
187 			    long mask)
188 {
189 	struct ad7606_state *st = iio_priv(indio_dev);
190 	DECLARE_BITMAP(values, 3);
191 	int i;
192 
193 	switch (mask) {
194 	case IIO_CHAN_INFO_SCALE:
195 		mutex_lock(&st->lock);
196 		i = find_closest(val2, scale_avail, ARRAY_SIZE(scale_avail));
197 		gpiod_set_value(st->gpio_range, i);
198 		st->range = i;
199 		mutex_unlock(&st->lock);
200 
201 		return 0;
202 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
203 		if (val2)
204 			return -EINVAL;
205 		i = find_closest(val, ad7606_oversampling_avail,
206 				 ARRAY_SIZE(ad7606_oversampling_avail));
207 
208 		values[0] = i;
209 
210 		mutex_lock(&st->lock);
211 		gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
212 				      st->gpio_os->info, values);
213 		st->oversampling = ad7606_oversampling_avail[i];
214 		mutex_unlock(&st->lock);
215 
216 		return 0;
217 	default:
218 		return -EINVAL;
219 	}
220 }
221 
222 static IIO_CONST_ATTR(oversampling_ratio_available, "1 2 4 8 16 32 64");
223 
224 static struct attribute *ad7606_attributes_os_and_range[] = {
225 	&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
226 	&iio_const_attr_oversampling_ratio_available.dev_attr.attr,
227 	NULL,
228 };
229 
230 static const struct attribute_group ad7606_attribute_group_os_and_range = {
231 	.attrs = ad7606_attributes_os_and_range,
232 };
233 
234 static struct attribute *ad7606_attributes_os[] = {
235 	&iio_const_attr_oversampling_ratio_available.dev_attr.attr,
236 	NULL,
237 };
238 
239 static const struct attribute_group ad7606_attribute_group_os = {
240 	.attrs = ad7606_attributes_os,
241 };
242 
243 static struct attribute *ad7606_attributes_range[] = {
244 	&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
245 	NULL,
246 };
247 
248 static const struct attribute_group ad7606_attribute_group_range = {
249 	.attrs = ad7606_attributes_range,
250 };
251 
252 #define AD760X_CHANNEL(num, mask) {				\
253 		.type = IIO_VOLTAGE,				\
254 		.indexed = 1,					\
255 		.channel = num,					\
256 		.address = num,					\
257 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
258 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
259 		.info_mask_shared_by_all = mask,		\
260 		.scan_index = num,				\
261 		.scan_type = {					\
262 			.sign = 's',				\
263 			.realbits = 16,				\
264 			.storagebits = 16,			\
265 			.endianness = IIO_CPU,			\
266 		},						\
267 }
268 
269 #define AD7605_CHANNEL(num)	\
270 	AD760X_CHANNEL(num, 0)
271 
272 #define AD7606_CHANNEL(num)	\
273 	AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO))
274 
275 static const struct iio_chan_spec ad7605_channels[] = {
276 	IIO_CHAN_SOFT_TIMESTAMP(4),
277 	AD7605_CHANNEL(0),
278 	AD7605_CHANNEL(1),
279 	AD7605_CHANNEL(2),
280 	AD7605_CHANNEL(3),
281 };
282 
283 static const struct iio_chan_spec ad7606_channels[] = {
284 	IIO_CHAN_SOFT_TIMESTAMP(8),
285 	AD7606_CHANNEL(0),
286 	AD7606_CHANNEL(1),
287 	AD7606_CHANNEL(2),
288 	AD7606_CHANNEL(3),
289 	AD7606_CHANNEL(4),
290 	AD7606_CHANNEL(5),
291 	AD7606_CHANNEL(6),
292 	AD7606_CHANNEL(7),
293 };
294 
295 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
296 	/* More devices added in future */
297 	[ID_AD7605_4] = {
298 		.channels = ad7605_channels,
299 		.num_channels = 5,
300 	},
301 	[ID_AD7606_8] = {
302 		.channels = ad7606_channels,
303 		.num_channels = 9,
304 		.has_oversampling = true,
305 	},
306 	[ID_AD7606_6] = {
307 		.channels = ad7606_channels,
308 		.num_channels = 7,
309 		.has_oversampling = true,
310 	},
311 	[ID_AD7606_4] = {
312 		.channels = ad7606_channels,
313 		.num_channels = 5,
314 		.has_oversampling = true,
315 	},
316 };
317 
318 static int ad7606_request_gpios(struct ad7606_state *st)
319 {
320 	struct device *dev = st->dev;
321 
322 	st->gpio_convst = devm_gpiod_get(dev, "adi,conversion-start",
323 					 GPIOD_OUT_LOW);
324 	if (IS_ERR(st->gpio_convst))
325 		return PTR_ERR(st->gpio_convst);
326 
327 	st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
328 	if (IS_ERR(st->gpio_reset))
329 		return PTR_ERR(st->gpio_reset);
330 
331 	st->gpio_range = devm_gpiod_get_optional(dev, "adi,range",
332 						 GPIOD_OUT_LOW);
333 	if (IS_ERR(st->gpio_range))
334 		return PTR_ERR(st->gpio_range);
335 
336 	st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
337 						   GPIOD_OUT_HIGH);
338 	if (IS_ERR(st->gpio_standby))
339 		return PTR_ERR(st->gpio_standby);
340 
341 	st->gpio_frstdata = devm_gpiod_get_optional(dev, "adi,first-data",
342 						    GPIOD_IN);
343 	if (IS_ERR(st->gpio_frstdata))
344 		return PTR_ERR(st->gpio_frstdata);
345 
346 	if (!st->chip_info->has_oversampling)
347 		return 0;
348 
349 	st->gpio_os = devm_gpiod_get_array_optional(dev,
350 						    "adi,oversampling-ratio",
351 						    GPIOD_OUT_LOW);
352 	return PTR_ERR_OR_ZERO(st->gpio_os);
353 }
354 
355 /*
356  * The BUSY signal indicates when conversions are in progress, so when a rising
357  * edge of CONVST is applied, BUSY goes logic high and transitions low at the
358  * end of the entire conversion process. The falling edge of the BUSY signal
359  * triggers this interrupt.
360  */
361 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
362 {
363 	struct iio_dev *indio_dev = dev_id;
364 	struct ad7606_state *st = iio_priv(indio_dev);
365 
366 	if (iio_buffer_enabled(indio_dev)) {
367 		gpiod_set_value(st->gpio_convst, 0);
368 		iio_trigger_poll_chained(st->trig);
369 	} else {
370 		complete(&st->completion);
371 	}
372 
373 	return IRQ_HANDLED;
374 };
375 
376 static int ad7606_validate_trigger(struct iio_dev *indio_dev,
377 				   struct iio_trigger *trig)
378 {
379 	struct ad7606_state *st = iio_priv(indio_dev);
380 
381 	if (st->trig != trig)
382 		return -EINVAL;
383 
384 	return 0;
385 }
386 
387 static int ad7606_buffer_postenable(struct iio_dev *indio_dev)
388 {
389 	struct ad7606_state *st = iio_priv(indio_dev);
390 
391 	iio_triggered_buffer_postenable(indio_dev);
392 	gpiod_set_value(st->gpio_convst, 1);
393 
394 	return 0;
395 }
396 
397 static int ad7606_buffer_predisable(struct iio_dev *indio_dev)
398 {
399 	struct ad7606_state *st = iio_priv(indio_dev);
400 
401 	gpiod_set_value(st->gpio_convst, 0);
402 
403 	return iio_triggered_buffer_predisable(indio_dev);
404 }
405 
406 static const struct iio_buffer_setup_ops ad7606_buffer_ops = {
407 	.postenable = &ad7606_buffer_postenable,
408 	.predisable = &ad7606_buffer_predisable,
409 };
410 
411 static const struct iio_info ad7606_info_no_os_or_range = {
412 	.read_raw = &ad7606_read_raw,
413 	.validate_trigger = &ad7606_validate_trigger,
414 };
415 
416 static const struct iio_info ad7606_info_os_and_range = {
417 	.read_raw = &ad7606_read_raw,
418 	.write_raw = &ad7606_write_raw,
419 	.attrs = &ad7606_attribute_group_os_and_range,
420 	.validate_trigger = &ad7606_validate_trigger,
421 };
422 
423 static const struct iio_info ad7606_info_os = {
424 	.read_raw = &ad7606_read_raw,
425 	.write_raw = &ad7606_write_raw,
426 	.attrs = &ad7606_attribute_group_os,
427 	.validate_trigger = &ad7606_validate_trigger,
428 };
429 
430 static const struct iio_info ad7606_info_range = {
431 	.read_raw = &ad7606_read_raw,
432 	.write_raw = &ad7606_write_raw,
433 	.attrs = &ad7606_attribute_group_range,
434 	.validate_trigger = &ad7606_validate_trigger,
435 };
436 
437 static const struct iio_trigger_ops ad7606_trigger_ops = {
438 	.validate_device = iio_trigger_validate_own_device,
439 };
440 
441 static void ad7606_regulator_disable(void *data)
442 {
443 	struct ad7606_state *st = data;
444 
445 	regulator_disable(st->reg);
446 }
447 
448 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
449 		 const char *name, unsigned int id,
450 		 const struct ad7606_bus_ops *bops)
451 {
452 	struct ad7606_state *st;
453 	int ret;
454 	struct iio_dev *indio_dev;
455 
456 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
457 	if (!indio_dev)
458 		return -ENOMEM;
459 
460 	st = iio_priv(indio_dev);
461 	dev_set_drvdata(dev, indio_dev);
462 
463 	st->dev = dev;
464 	mutex_init(&st->lock);
465 	st->bops = bops;
466 	st->base_address = base_address;
467 	/* tied to logic low, analog input range is +/- 5V */
468 	st->range = 0;
469 	st->oversampling = 1;
470 
471 	st->reg = devm_regulator_get(dev, "avcc");
472 	if (IS_ERR(st->reg))
473 		return PTR_ERR(st->reg);
474 
475 	ret = regulator_enable(st->reg);
476 	if (ret) {
477 		dev_err(dev, "Failed to enable specified AVcc supply\n");
478 		return ret;
479 	}
480 
481 	ret = devm_add_action_or_reset(dev, ad7606_regulator_disable, st);
482 	if (ret)
483 		return ret;
484 
485 	st->chip_info = &ad7606_chip_info_tbl[id];
486 
487 	ret = ad7606_request_gpios(st);
488 	if (ret)
489 		return ret;
490 
491 	indio_dev->dev.parent = dev;
492 	if (st->gpio_os) {
493 		if (st->gpio_range)
494 			indio_dev->info = &ad7606_info_os_and_range;
495 		else
496 			indio_dev->info = &ad7606_info_os;
497 	} else {
498 		if (st->gpio_range)
499 			indio_dev->info = &ad7606_info_range;
500 		else
501 			indio_dev->info = &ad7606_info_no_os_or_range;
502 	}
503 	indio_dev->modes = INDIO_DIRECT_MODE;
504 	indio_dev->name = name;
505 	indio_dev->channels = st->chip_info->channels;
506 	indio_dev->num_channels = st->chip_info->num_channels;
507 
508 	init_completion(&st->completion);
509 
510 	ret = ad7606_reset(st);
511 	if (ret)
512 		dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
513 
514 	st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
515 					  indio_dev->name, indio_dev->id);
516 	if (!st->trig)
517 		return -ENOMEM;
518 
519 	st->trig->ops = &ad7606_trigger_ops;
520 	st->trig->dev.parent = dev;
521 	iio_trigger_set_drvdata(st->trig, indio_dev);
522 	ret = devm_iio_trigger_register(dev, st->trig);
523 	if (ret)
524 		return ret;
525 
526 	indio_dev->trig = iio_trigger_get(st->trig);
527 
528 	ret = devm_request_threaded_irq(dev, irq,
529 					NULL,
530 					&ad7606_interrupt,
531 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
532 					name, indio_dev);
533 	if (ret)
534 		return ret;
535 
536 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
537 					      &iio_pollfunc_store_time,
538 					      &ad7606_trigger_handler,
539 					      &ad7606_buffer_ops);
540 	if (ret)
541 		return ret;
542 
543 	return devm_iio_device_register(dev, indio_dev);
544 }
545 EXPORT_SYMBOL_GPL(ad7606_probe);
546 
547 #ifdef CONFIG_PM_SLEEP
548 
549 static int ad7606_suspend(struct device *dev)
550 {
551 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
552 	struct ad7606_state *st = iio_priv(indio_dev);
553 
554 	if (st->gpio_standby) {
555 		gpiod_set_value(st->gpio_range, 1);
556 		gpiod_set_value(st->gpio_standby, 0);
557 	}
558 
559 	return 0;
560 }
561 
562 static int ad7606_resume(struct device *dev)
563 {
564 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
565 	struct ad7606_state *st = iio_priv(indio_dev);
566 
567 	if (st->gpio_standby) {
568 		gpiod_set_value(st->gpio_range, st->range);
569 		gpiod_set_value(st->gpio_standby, 1);
570 		ad7606_reset(st);
571 	}
572 
573 	return 0;
574 }
575 
576 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
577 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
578 
579 #endif
580 
581 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
582 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
583 MODULE_LICENSE("GPL v2");
584