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