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