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