xref: /linux/drivers/iio/adc/ad_sigma_delta.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 /*
2  * Support code for Analog Devices Sigma-Delta ADCs
3  *
4  * Copyright 2012 Analog Devices Inc.
5  *  Author: Lars-Peter Clausen <lars@metafoo.de>
6  *
7  * Licensed under the GPL-2.
8  */
9 
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
15 #include <linux/err.h>
16 #include <linux/module.h>
17 
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/adc/ad_sigma_delta.h>
25 
26 #include <asm/unaligned.h>
27 
28 
29 #define AD_SD_COMM_CHAN_MASK	0x3
30 
31 #define AD_SD_REG_COMM		0x00
32 #define AD_SD_REG_DATA		0x03
33 
34 /**
35  * ad_sd_set_comm() - Set communications register
36  *
37  * @sigma_delta: The sigma delta device
38  * @comm: New value for the communications register
39  */
40 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm)
41 {
42 	/* Some variants use the lower two bits of the communications register
43 	 * to select the channel */
44 	sigma_delta->comm = comm & AD_SD_COMM_CHAN_MASK;
45 }
46 EXPORT_SYMBOL_GPL(ad_sd_set_comm);
47 
48 /**
49  * ad_sd_write_reg() - Write a register
50  *
51  * @sigma_delta: The sigma delta device
52  * @reg: Address of the register
53  * @size: Size of the register (0-3)
54  * @val: Value to write to the register
55  *
56  * Returns 0 on success, an error code otherwise.
57  **/
58 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
59 	unsigned int size, unsigned int val)
60 {
61 	uint8_t *data = sigma_delta->data;
62 	struct spi_transfer t = {
63 		.tx_buf		= data,
64 		.len		= size + 1,
65 		.cs_change	= sigma_delta->bus_locked,
66 	};
67 	struct spi_message m;
68 	int ret;
69 
70 	data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm;
71 
72 	switch (size) {
73 	case 3:
74 		data[1] = val >> 16;
75 		data[2] = val >> 8;
76 		data[3] = val;
77 		break;
78 	case 2:
79 		put_unaligned_be16(val, &data[1]);
80 		break;
81 	case 1:
82 		data[1] = val;
83 		break;
84 	case 0:
85 		break;
86 	default:
87 		return -EINVAL;
88 	}
89 
90 	spi_message_init(&m);
91 	spi_message_add_tail(&t, &m);
92 
93 	if (sigma_delta->bus_locked)
94 		ret = spi_sync_locked(sigma_delta->spi, &m);
95 	else
96 		ret = spi_sync(sigma_delta->spi, &m);
97 
98 	return ret;
99 }
100 EXPORT_SYMBOL_GPL(ad_sd_write_reg);
101 
102 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
103 	unsigned int reg, unsigned int size, uint8_t *val)
104 {
105 	uint8_t *data = sigma_delta->data;
106 	int ret;
107 	struct spi_transfer t[] = {
108 		{
109 			.tx_buf = data,
110 			.len = 1,
111 		}, {
112 			.rx_buf = val,
113 			.len = size,
114 			.cs_change = sigma_delta->bus_locked,
115 		},
116 	};
117 	struct spi_message m;
118 
119 	spi_message_init(&m);
120 
121 	if (sigma_delta->info->has_registers) {
122 		data[0] = reg << sigma_delta->info->addr_shift;
123 		data[0] |= sigma_delta->info->read_mask;
124 		spi_message_add_tail(&t[0], &m);
125 	}
126 	spi_message_add_tail(&t[1], &m);
127 
128 	if (sigma_delta->bus_locked)
129 		ret = spi_sync_locked(sigma_delta->spi, &m);
130 	else
131 		ret = spi_sync(sigma_delta->spi, &m);
132 
133 	return ret;
134 }
135 
136 /**
137  * ad_sd_read_reg() - Read a register
138  *
139  * @sigma_delta: The sigma delta device
140  * @reg: Address of the register
141  * @size: Size of the register (1-4)
142  * @val: Read value
143  *
144  * Returns 0 on success, an error code otherwise.
145  **/
146 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
147 	unsigned int reg, unsigned int size, unsigned int *val)
148 {
149 	int ret;
150 
151 	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
152 	if (ret < 0)
153 		goto out;
154 
155 	switch (size) {
156 	case 4:
157 		*val = get_unaligned_be32(sigma_delta->data);
158 		break;
159 	case 3:
160 		*val = (sigma_delta->data[0] << 16) |
161 			(sigma_delta->data[1] << 8) |
162 			sigma_delta->data[2];
163 		break;
164 	case 2:
165 		*val = get_unaligned_be16(sigma_delta->data);
166 		break;
167 	case 1:
168 		*val = sigma_delta->data[0];
169 		break;
170 	default:
171 		ret = -EINVAL;
172 		break;
173 	}
174 
175 out:
176 	return ret;
177 }
178 EXPORT_SYMBOL_GPL(ad_sd_read_reg);
179 
180 /**
181  * ad_sd_reset() - Reset the serial interface
182  *
183  * @sigma_delta: The sigma delta device
184  * @reset_length: Number of SCLKs with DIN = 1
185  *
186  * Returns 0 on success, an error code otherwise.
187  **/
188 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
189 	unsigned int reset_length)
190 {
191 	uint8_t *buf;
192 	unsigned int size;
193 	int ret;
194 
195 	size = DIV_ROUND_UP(reset_length, 8);
196 	buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
197 	if (!buf)
198 		return -ENOMEM;
199 
200 	memset(buf, 0xff, size);
201 	ret = spi_write(sigma_delta->spi, buf, size);
202 	kfree(buf);
203 
204 	return ret;
205 }
206 EXPORT_SYMBOL_GPL(ad_sd_reset);
207 
208 static int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
209 	unsigned int mode, unsigned int channel)
210 {
211 	int ret;
212 	unsigned long timeout;
213 
214 	ret = ad_sigma_delta_set_channel(sigma_delta, channel);
215 	if (ret)
216 		return ret;
217 
218 	spi_bus_lock(sigma_delta->spi->master);
219 	sigma_delta->bus_locked = true;
220 	reinit_completion(&sigma_delta->completion);
221 
222 	ret = ad_sigma_delta_set_mode(sigma_delta, mode);
223 	if (ret < 0)
224 		goto out;
225 
226 	sigma_delta->irq_dis = false;
227 	enable_irq(sigma_delta->spi->irq);
228 	timeout = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ);
229 	if (timeout == 0) {
230 		sigma_delta->irq_dis = true;
231 		disable_irq_nosync(sigma_delta->spi->irq);
232 		ret = -EIO;
233 	} else {
234 		ret = 0;
235 	}
236 out:
237 	sigma_delta->bus_locked = false;
238 	spi_bus_unlock(sigma_delta->spi->master);
239 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
240 
241 	return ret;
242 }
243 
244 /**
245  * ad_sd_calibrate_all() - Performs channel calibration
246  * @sigma_delta: The sigma delta device
247  * @cb: Array of channels and calibration type to perform
248  * @n: Number of items in cb
249  *
250  * Returns 0 on success, an error code otherwise.
251  **/
252 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
253 	const struct ad_sd_calib_data *cb, unsigned int n)
254 {
255 	unsigned int i;
256 	int ret;
257 
258 	for (i = 0; i < n; i++) {
259 		ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
260 		if (ret)
261 			return ret;
262 	}
263 
264 	return 0;
265 }
266 EXPORT_SYMBOL_GPL(ad_sd_calibrate_all);
267 
268 /**
269  * ad_sigma_delta_single_conversion() - Performs a single data conversion
270  * @indio_dev: The IIO device
271  * @chan: The conversion is done for this channel
272  * @val: Pointer to the location where to store the read value
273  *
274  * Returns: 0 on success, an error value otherwise.
275  */
276 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
277 	const struct iio_chan_spec *chan, int *val)
278 {
279 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
280 	unsigned int sample, raw_sample;
281 	unsigned int data_reg;
282 	int ret = 0;
283 
284 	if (iio_buffer_enabled(indio_dev))
285 		return -EBUSY;
286 
287 	mutex_lock(&indio_dev->mlock);
288 	ad_sigma_delta_set_channel(sigma_delta, chan->address);
289 
290 	spi_bus_lock(sigma_delta->spi->master);
291 	sigma_delta->bus_locked = true;
292 	reinit_completion(&sigma_delta->completion);
293 
294 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
295 
296 	sigma_delta->irq_dis = false;
297 	enable_irq(sigma_delta->spi->irq);
298 	ret = wait_for_completion_interruptible_timeout(
299 			&sigma_delta->completion, HZ);
300 
301 	sigma_delta->bus_locked = false;
302 	spi_bus_unlock(sigma_delta->spi->master);
303 
304 	if (ret == 0)
305 		ret = -EIO;
306 	if (ret < 0)
307 		goto out;
308 
309 	if (sigma_delta->info->data_reg != 0)
310 		data_reg = sigma_delta->info->data_reg;
311 	else
312 		data_reg = AD_SD_REG_DATA;
313 
314 	ret = ad_sd_read_reg(sigma_delta, data_reg,
315 		DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
316 		&raw_sample);
317 
318 out:
319 	if (!sigma_delta->irq_dis) {
320 		disable_irq_nosync(sigma_delta->spi->irq);
321 		sigma_delta->irq_dis = true;
322 	}
323 
324 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
325 	mutex_unlock(&indio_dev->mlock);
326 
327 	if (ret)
328 		return ret;
329 
330 	sample = raw_sample >> chan->scan_type.shift;
331 	sample &= (1 << chan->scan_type.realbits) - 1;
332 	*val = sample;
333 
334 	ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
335 	if (ret)
336 		return ret;
337 
338 	return IIO_VAL_INT;
339 }
340 EXPORT_SYMBOL_GPL(ad_sigma_delta_single_conversion);
341 
342 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
343 {
344 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
345 	unsigned int channel;
346 	int ret;
347 
348 	ret = iio_triggered_buffer_postenable(indio_dev);
349 	if (ret < 0)
350 		return ret;
351 
352 	channel = find_first_bit(indio_dev->active_scan_mask,
353 				 indio_dev->masklength);
354 	ret = ad_sigma_delta_set_channel(sigma_delta,
355 		indio_dev->channels[channel].address);
356 	if (ret)
357 		goto err_predisable;
358 
359 	spi_bus_lock(sigma_delta->spi->master);
360 	sigma_delta->bus_locked = true;
361 	ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
362 	if (ret)
363 		goto err_unlock;
364 
365 	sigma_delta->irq_dis = false;
366 	enable_irq(sigma_delta->spi->irq);
367 
368 	return 0;
369 
370 err_unlock:
371 	spi_bus_unlock(sigma_delta->spi->master);
372 err_predisable:
373 
374 	return ret;
375 }
376 
377 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
378 {
379 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
380 
381 	reinit_completion(&sigma_delta->completion);
382 	wait_for_completion_timeout(&sigma_delta->completion, HZ);
383 
384 	if (!sigma_delta->irq_dis) {
385 		disable_irq_nosync(sigma_delta->spi->irq);
386 		sigma_delta->irq_dis = true;
387 	}
388 
389 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
390 
391 	sigma_delta->bus_locked = false;
392 	return spi_bus_unlock(sigma_delta->spi->master);
393 }
394 
395 static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
396 {
397 	struct iio_poll_func *pf = p;
398 	struct iio_dev *indio_dev = pf->indio_dev;
399 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
400 	unsigned int reg_size;
401 	unsigned int data_reg;
402 	uint8_t data[16];
403 	int ret;
404 
405 	memset(data, 0x00, 16);
406 
407 	reg_size = indio_dev->channels[0].scan_type.realbits +
408 			indio_dev->channels[0].scan_type.shift;
409 	reg_size = DIV_ROUND_UP(reg_size, 8);
410 
411 	if (sigma_delta->info->data_reg != 0)
412 		data_reg = sigma_delta->info->data_reg;
413 	else
414 		data_reg = AD_SD_REG_DATA;
415 
416 	switch (reg_size) {
417 	case 4:
418 	case 2:
419 	case 1:
420 		ret = ad_sd_read_reg_raw(sigma_delta, data_reg, reg_size,
421 			&data[0]);
422 		break;
423 	case 3:
424 		/* We store 24 bit samples in a 32 bit word. Keep the upper
425 		 * byte set to zero. */
426 		ret = ad_sd_read_reg_raw(sigma_delta, data_reg, reg_size,
427 			&data[1]);
428 		break;
429 	}
430 
431 	iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
432 
433 	iio_trigger_notify_done(indio_dev->trig);
434 	sigma_delta->irq_dis = false;
435 	enable_irq(sigma_delta->spi->irq);
436 
437 	return IRQ_HANDLED;
438 }
439 
440 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
441 	.postenable = &ad_sd_buffer_postenable,
442 	.predisable = &iio_triggered_buffer_predisable,
443 	.postdisable = &ad_sd_buffer_postdisable,
444 	.validate_scan_mask = &iio_validate_scan_mask_onehot,
445 };
446 
447 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
448 {
449 	struct ad_sigma_delta *sigma_delta = private;
450 
451 	complete(&sigma_delta->completion);
452 	disable_irq_nosync(irq);
453 	sigma_delta->irq_dis = true;
454 	iio_trigger_poll(sigma_delta->trig);
455 
456 	return IRQ_HANDLED;
457 }
458 
459 /**
460  * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
461  * @indio_dev: The IIO device
462  * @trig: The new trigger
463  *
464  * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
465  * device, -EINVAL otherwise.
466  */
467 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
468 {
469 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
470 
471 	if (sigma_delta->trig != trig)
472 		return -EINVAL;
473 
474 	return 0;
475 }
476 EXPORT_SYMBOL_GPL(ad_sd_validate_trigger);
477 
478 static const struct iio_trigger_ops ad_sd_trigger_ops = {
479 };
480 
481 static int ad_sd_probe_trigger(struct iio_dev *indio_dev)
482 {
483 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
484 	int ret;
485 
486 	sigma_delta->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
487 						indio_dev->id);
488 	if (sigma_delta->trig == NULL) {
489 		ret = -ENOMEM;
490 		goto error_ret;
491 	}
492 	sigma_delta->trig->ops = &ad_sd_trigger_ops;
493 	init_completion(&sigma_delta->completion);
494 
495 	ret = request_irq(sigma_delta->spi->irq,
496 			  ad_sd_data_rdy_trig_poll,
497 			  IRQF_TRIGGER_LOW,
498 			  indio_dev->name,
499 			  sigma_delta);
500 	if (ret)
501 		goto error_free_trig;
502 
503 	if (!sigma_delta->irq_dis) {
504 		sigma_delta->irq_dis = true;
505 		disable_irq_nosync(sigma_delta->spi->irq);
506 	}
507 	sigma_delta->trig->dev.parent = &sigma_delta->spi->dev;
508 	iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
509 
510 	ret = iio_trigger_register(sigma_delta->trig);
511 	if (ret)
512 		goto error_free_irq;
513 
514 	/* select default trigger */
515 	indio_dev->trig = iio_trigger_get(sigma_delta->trig);
516 
517 	return 0;
518 
519 error_free_irq:
520 	free_irq(sigma_delta->spi->irq, sigma_delta);
521 error_free_trig:
522 	iio_trigger_free(sigma_delta->trig);
523 error_ret:
524 	return ret;
525 }
526 
527 static void ad_sd_remove_trigger(struct iio_dev *indio_dev)
528 {
529 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
530 
531 	iio_trigger_unregister(sigma_delta->trig);
532 	free_irq(sigma_delta->spi->irq, sigma_delta);
533 	iio_trigger_free(sigma_delta->trig);
534 }
535 
536 /**
537  * ad_sd_setup_buffer_and_trigger() -
538  * @indio_dev: The IIO device
539  */
540 int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev)
541 {
542 	int ret;
543 
544 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
545 			&ad_sd_trigger_handler, &ad_sd_buffer_setup_ops);
546 	if (ret)
547 		return ret;
548 
549 	ret = ad_sd_probe_trigger(indio_dev);
550 	if (ret) {
551 		iio_triggered_buffer_cleanup(indio_dev);
552 		return ret;
553 	}
554 
555 	return 0;
556 }
557 EXPORT_SYMBOL_GPL(ad_sd_setup_buffer_and_trigger);
558 
559 /**
560  * ad_sd_cleanup_buffer_and_trigger() -
561  * @indio_dev: The IIO device
562  */
563 void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev)
564 {
565 	ad_sd_remove_trigger(indio_dev);
566 	iio_triggered_buffer_cleanup(indio_dev);
567 }
568 EXPORT_SYMBOL_GPL(ad_sd_cleanup_buffer_and_trigger);
569 
570 /**
571  * ad_sd_init() - Initializes a ad_sigma_delta struct
572  * @sigma_delta: The ad_sigma_delta device
573  * @indio_dev: The IIO device which the Sigma Delta device is used for
574  * @spi: The SPI device for the ad_sigma_delta device
575  * @info: Device specific callbacks and options
576  *
577  * This function needs to be called before any other operations are performed on
578  * the ad_sigma_delta struct.
579  */
580 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
581 	struct spi_device *spi, const struct ad_sigma_delta_info *info)
582 {
583 	sigma_delta->spi = spi;
584 	sigma_delta->info = info;
585 	iio_device_set_drvdata(indio_dev, sigma_delta);
586 
587 	return 0;
588 }
589 EXPORT_SYMBOL_GPL(ad_sd_init);
590 
591 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
592 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
593 MODULE_LICENSE("GPL v2");
594