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 		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 
ad_sd_read_reg_raw(struct ad_sigma_delta * sigma_delta,unsigned int reg,unsigned int size,uint8_t * val)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  **/
ad_sd_read_reg(struct ad_sigma_delta * sigma_delta,unsigned int reg,unsigned int size,unsigned int * val)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  **/
ad_sd_reset(struct ad_sigma_delta * sigma_delta,unsigned int reset_length)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 
ad_sd_calibrate(struct ad_sigma_delta * sigma_delta,unsigned int mode,unsigned int channel)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 	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 
246 /**
247  * ad_sd_calibrate_all() - Performs channel calibration
248  * @sigma_delta: The sigma delta device
249  * @cb: Array of channels and calibration type to perform
250  * @n: Number of items in cb
251  *
252  * Returns 0 on success, an error code otherwise.
253  **/
ad_sd_calibrate_all(struct ad_sigma_delta * sigma_delta,const struct ad_sd_calib_data * cb,unsigned int n)254 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
255 	const struct ad_sd_calib_data *cb, unsigned int n)
256 {
257 	unsigned int i;
258 	int ret;
259 
260 	for (i = 0; i < n; i++) {
261 		ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
262 		if (ret)
263 			return ret;
264 	}
265 
266 	return 0;
267 }
268 EXPORT_SYMBOL_GPL(ad_sd_calibrate_all);
269 
270 /**
271  * ad_sigma_delta_single_conversion() - Performs a single data conversion
272  * @indio_dev: The IIO device
273  * @chan: The conversion is done for this channel
274  * @val: Pointer to the location where to store the read value
275  *
276  * Returns: 0 on success, an error value otherwise.
277  */
ad_sigma_delta_single_conversion(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val)278 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
279 	const struct iio_chan_spec *chan, int *val)
280 {
281 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
282 	unsigned int sample, raw_sample;
283 	unsigned int data_reg;
284 	int ret = 0;
285 
286 	if (iio_buffer_enabled(indio_dev))
287 		return -EBUSY;
288 
289 	mutex_lock(&indio_dev->mlock);
290 	ad_sigma_delta_set_channel(sigma_delta, chan->address);
291 
292 	spi_bus_lock(sigma_delta->spi->master);
293 	sigma_delta->bus_locked = true;
294 	sigma_delta->keep_cs_asserted = true;
295 	reinit_completion(&sigma_delta->completion);
296 
297 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
298 
299 	sigma_delta->irq_dis = false;
300 	enable_irq(sigma_delta->spi->irq);
301 	ret = wait_for_completion_interruptible_timeout(
302 			&sigma_delta->completion, HZ);
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 	sigma_delta->keep_cs_asserted = false;
325 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
326 	sigma_delta->bus_locked = false;
327 	spi_bus_unlock(sigma_delta->spi->master);
328 	mutex_unlock(&indio_dev->mlock);
329 
330 	if (ret)
331 		return ret;
332 
333 	sample = raw_sample >> chan->scan_type.shift;
334 	sample &= (1 << chan->scan_type.realbits) - 1;
335 	*val = sample;
336 
337 	ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
338 	if (ret)
339 		return ret;
340 
341 	return IIO_VAL_INT;
342 }
343 EXPORT_SYMBOL_GPL(ad_sigma_delta_single_conversion);
344 
ad_sd_buffer_postenable(struct iio_dev * indio_dev)345 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
346 {
347 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
348 	unsigned int channel;
349 	int ret;
350 
351 	ret = iio_triggered_buffer_postenable(indio_dev);
352 	if (ret < 0)
353 		return ret;
354 
355 	channel = find_first_bit(indio_dev->active_scan_mask,
356 				 indio_dev->masklength);
357 	ret = ad_sigma_delta_set_channel(sigma_delta,
358 		indio_dev->channels[channel].address);
359 	if (ret)
360 		return ret;
361 
362 	spi_bus_lock(sigma_delta->spi->master);
363 	sigma_delta->bus_locked = true;
364 	sigma_delta->keep_cs_asserted = true;
365 
366 	ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
367 	if (ret)
368 		goto err_unlock;
369 
370 	sigma_delta->irq_dis = false;
371 	enable_irq(sigma_delta->spi->irq);
372 
373 	return 0;
374 
375 err_unlock:
376 	spi_bus_unlock(sigma_delta->spi->master);
377 
378 	return ret;
379 }
380 
ad_sd_buffer_postdisable(struct iio_dev * indio_dev)381 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
382 {
383 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
384 
385 	reinit_completion(&sigma_delta->completion);
386 	wait_for_completion_timeout(&sigma_delta->completion, HZ);
387 
388 	if (!sigma_delta->irq_dis) {
389 		disable_irq_nosync(sigma_delta->spi->irq);
390 		sigma_delta->irq_dis = true;
391 	}
392 
393 	sigma_delta->keep_cs_asserted = false;
394 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
395 
396 	sigma_delta->bus_locked = false;
397 	return spi_bus_unlock(sigma_delta->spi->master);
398 }
399 
ad_sd_trigger_handler(int irq,void * p)400 static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
401 {
402 	struct iio_poll_func *pf = p;
403 	struct iio_dev *indio_dev = pf->indio_dev;
404 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
405 	unsigned int reg_size;
406 	unsigned int data_reg;
407 	uint8_t data[16];
408 	int ret;
409 
410 	memset(data, 0x00, 16);
411 
412 	reg_size = indio_dev->channels[0].scan_type.realbits +
413 			indio_dev->channels[0].scan_type.shift;
414 	reg_size = DIV_ROUND_UP(reg_size, 8);
415 
416 	if (sigma_delta->info->data_reg != 0)
417 		data_reg = sigma_delta->info->data_reg;
418 	else
419 		data_reg = AD_SD_REG_DATA;
420 
421 	switch (reg_size) {
422 	case 4:
423 	case 2:
424 	case 1:
425 		ret = ad_sd_read_reg_raw(sigma_delta, data_reg, reg_size,
426 			&data[0]);
427 		break;
428 	case 3:
429 		/* We store 24 bit samples in a 32 bit word. Keep the upper
430 		 * byte set to zero. */
431 		ret = ad_sd_read_reg_raw(sigma_delta, data_reg, reg_size,
432 			&data[1]);
433 		break;
434 	}
435 
436 	iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
437 
438 	iio_trigger_notify_done(indio_dev->trig);
439 	sigma_delta->irq_dis = false;
440 	enable_irq(sigma_delta->spi->irq);
441 
442 	return IRQ_HANDLED;
443 }
444 
445 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
446 	.postenable = &ad_sd_buffer_postenable,
447 	.predisable = &iio_triggered_buffer_predisable,
448 	.postdisable = &ad_sd_buffer_postdisable,
449 	.validate_scan_mask = &iio_validate_scan_mask_onehot,
450 };
451 
ad_sd_data_rdy_trig_poll(int irq,void * private)452 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
453 {
454 	struct ad_sigma_delta *sigma_delta = private;
455 
456 	complete(&sigma_delta->completion);
457 	disable_irq_nosync(irq);
458 	sigma_delta->irq_dis = true;
459 	iio_trigger_poll(sigma_delta->trig);
460 
461 	return IRQ_HANDLED;
462 }
463 
464 /**
465  * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
466  * @indio_dev: The IIO device
467  * @trig: The new trigger
468  *
469  * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
470  * device, -EINVAL otherwise.
471  */
ad_sd_validate_trigger(struct iio_dev * indio_dev,struct iio_trigger * trig)472 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
473 {
474 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
475 
476 	if (sigma_delta->trig != trig)
477 		return -EINVAL;
478 
479 	return 0;
480 }
481 EXPORT_SYMBOL_GPL(ad_sd_validate_trigger);
482 
483 static const struct iio_trigger_ops ad_sd_trigger_ops = {
484 };
485 
ad_sd_probe_trigger(struct iio_dev * indio_dev)486 static int ad_sd_probe_trigger(struct iio_dev *indio_dev)
487 {
488 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
489 	int ret;
490 
491 	sigma_delta->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
492 						indio_dev->id);
493 	if (sigma_delta->trig == NULL) {
494 		ret = -ENOMEM;
495 		goto error_ret;
496 	}
497 	sigma_delta->trig->ops = &ad_sd_trigger_ops;
498 	init_completion(&sigma_delta->completion);
499 
500 	ret = request_irq(sigma_delta->spi->irq,
501 			  ad_sd_data_rdy_trig_poll,
502 			  IRQF_TRIGGER_LOW,
503 			  indio_dev->name,
504 			  sigma_delta);
505 	if (ret)
506 		goto error_free_trig;
507 
508 	if (!sigma_delta->irq_dis) {
509 		sigma_delta->irq_dis = true;
510 		disable_irq_nosync(sigma_delta->spi->irq);
511 	}
512 	sigma_delta->trig->dev.parent = &sigma_delta->spi->dev;
513 	iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
514 
515 	ret = iio_trigger_register(sigma_delta->trig);
516 	if (ret)
517 		goto error_free_irq;
518 
519 	/* select default trigger */
520 	indio_dev->trig = iio_trigger_get(sigma_delta->trig);
521 
522 	return 0;
523 
524 error_free_irq:
525 	free_irq(sigma_delta->spi->irq, sigma_delta);
526 error_free_trig:
527 	iio_trigger_free(sigma_delta->trig);
528 error_ret:
529 	return ret;
530 }
531 
ad_sd_remove_trigger(struct iio_dev * indio_dev)532 static void ad_sd_remove_trigger(struct iio_dev *indio_dev)
533 {
534 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
535 
536 	iio_trigger_unregister(sigma_delta->trig);
537 	free_irq(sigma_delta->spi->irq, sigma_delta);
538 	iio_trigger_free(sigma_delta->trig);
539 }
540 
541 /**
542  * ad_sd_setup_buffer_and_trigger() -
543  * @indio_dev: The IIO device
544  */
ad_sd_setup_buffer_and_trigger(struct iio_dev * indio_dev)545 int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev)
546 {
547 	int ret;
548 
549 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
550 			&ad_sd_trigger_handler, &ad_sd_buffer_setup_ops);
551 	if (ret)
552 		return ret;
553 
554 	ret = ad_sd_probe_trigger(indio_dev);
555 	if (ret) {
556 		iio_triggered_buffer_cleanup(indio_dev);
557 		return ret;
558 	}
559 
560 	return 0;
561 }
562 EXPORT_SYMBOL_GPL(ad_sd_setup_buffer_and_trigger);
563 
564 /**
565  * ad_sd_cleanup_buffer_and_trigger() -
566  * @indio_dev: The IIO device
567  */
ad_sd_cleanup_buffer_and_trigger(struct iio_dev * indio_dev)568 void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev)
569 {
570 	ad_sd_remove_trigger(indio_dev);
571 	iio_triggered_buffer_cleanup(indio_dev);
572 }
573 EXPORT_SYMBOL_GPL(ad_sd_cleanup_buffer_and_trigger);
574 
575 /**
576  * ad_sd_init() - Initializes a ad_sigma_delta struct
577  * @sigma_delta: The ad_sigma_delta device
578  * @indio_dev: The IIO device which the Sigma Delta device is used for
579  * @spi: The SPI device for the ad_sigma_delta device
580  * @info: Device specific callbacks and options
581  *
582  * This function needs to be called before any other operations are performed on
583  * the ad_sigma_delta struct.
584  */
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)585 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
586 	struct spi_device *spi, const struct ad_sigma_delta_info *info)
587 {
588 	sigma_delta->spi = spi;
589 	sigma_delta->info = info;
590 	iio_device_set_drvdata(indio_dev, sigma_delta);
591 
592 	return 0;
593 }
594 EXPORT_SYMBOL_GPL(ad_sd_init);
595 
596 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
597 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
598 MODULE_LICENSE("GPL v2");
599