drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / iio / adc / ad_sigma_delta.c
blobea4aabd3960a086db8562e99a1f9a8e71750e8d2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Support code for Analog Devices Sigma-Delta ADCs
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 */
9 #include <linux/align.h>
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>
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>
26 #include <linux/unaligned.h>
29 #define AD_SD_COMM_CHAN_MASK 0x3
31 #define AD_SD_REG_COMM 0x00
32 #define AD_SD_REG_DATA 0x03
34 /**
35 * ad_sd_set_comm() - Set communications register
37 * @sigma_delta: The sigma delta device
38 * @comm: New value for the communications register
40 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm)
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;
46 EXPORT_SYMBOL_NS_GPL(ad_sd_set_comm, IIO_AD_SIGMA_DELTA);
48 /**
49 * ad_sd_write_reg() - Write a register
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
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)
61 uint8_t *data = sigma_delta->tx_buf;
62 struct spi_transfer t = {
63 .tx_buf = data,
64 .len = size + 1,
65 .cs_change = sigma_delta->keep_cs_asserted,
67 struct spi_message m;
68 int ret;
70 data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm;
72 switch (size) {
73 case 3:
74 put_unaligned_be24(val, &data[1]);
75 break;
76 case 2:
77 put_unaligned_be16(val, &data[1]);
78 break;
79 case 1:
80 data[1] = val;
81 break;
82 case 0:
83 break;
84 default:
85 return -EINVAL;
88 spi_message_init(&m);
89 spi_message_add_tail(&t, &m);
91 if (sigma_delta->bus_locked)
92 ret = spi_sync_locked(sigma_delta->spi, &m);
93 else
94 ret = spi_sync(sigma_delta->spi, &m);
96 return ret;
98 EXPORT_SYMBOL_NS_GPL(ad_sd_write_reg, IIO_AD_SIGMA_DELTA);
100 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
101 unsigned int reg, unsigned int size, uint8_t *val)
103 uint8_t *data = sigma_delta->tx_buf;
104 int ret;
105 struct spi_transfer t[] = {
107 .tx_buf = data,
108 .len = 1,
109 }, {
110 .rx_buf = val,
111 .len = size,
112 .cs_change = sigma_delta->bus_locked,
115 struct spi_message m;
117 spi_message_init(&m);
119 if (sigma_delta->info->has_registers) {
120 data[0] = reg << sigma_delta->info->addr_shift;
121 data[0] |= sigma_delta->info->read_mask;
122 data[0] |= sigma_delta->comm;
123 spi_message_add_tail(&t[0], &m);
125 spi_message_add_tail(&t[1], &m);
127 if (sigma_delta->bus_locked)
128 ret = spi_sync_locked(sigma_delta->spi, &m);
129 else
130 ret = spi_sync(sigma_delta->spi, &m);
132 return ret;
136 * ad_sd_read_reg() - Read a register
138 * @sigma_delta: The sigma delta device
139 * @reg: Address of the register
140 * @size: Size of the register (1-4)
141 * @val: Read value
143 * Returns 0 on success, an error code otherwise.
145 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
146 unsigned int reg, unsigned int size, unsigned int *val)
148 int ret;
150 ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->rx_buf);
151 if (ret < 0)
152 goto out;
154 switch (size) {
155 case 4:
156 *val = get_unaligned_be32(sigma_delta->rx_buf);
157 break;
158 case 3:
159 *val = get_unaligned_be24(sigma_delta->rx_buf);
160 break;
161 case 2:
162 *val = get_unaligned_be16(sigma_delta->rx_buf);
163 break;
164 case 1:
165 *val = sigma_delta->rx_buf[0];
166 break;
167 default:
168 ret = -EINVAL;
169 break;
172 out:
173 return ret;
175 EXPORT_SYMBOL_NS_GPL(ad_sd_read_reg, IIO_AD_SIGMA_DELTA);
178 * ad_sd_reset() - Reset the serial interface
180 * @sigma_delta: The sigma delta device
181 * @reset_length: Number of SCLKs with DIN = 1
183 * Returns 0 on success, an error code otherwise.
185 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
186 unsigned int reset_length)
188 uint8_t *buf;
189 unsigned int size;
190 int ret;
192 size = DIV_ROUND_UP(reset_length, 8);
193 buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
194 if (!buf)
195 return -ENOMEM;
197 memset(buf, 0xff, size);
198 ret = spi_write(sigma_delta->spi, buf, size);
199 kfree(buf);
201 return ret;
203 EXPORT_SYMBOL_NS_GPL(ad_sd_reset, IIO_AD_SIGMA_DELTA);
205 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
206 unsigned int mode, unsigned int channel)
208 int ret;
209 unsigned long time_left;
211 ret = ad_sigma_delta_set_channel(sigma_delta, channel);
212 if (ret)
213 return ret;
215 spi_bus_lock(sigma_delta->spi->controller);
216 sigma_delta->bus_locked = true;
217 sigma_delta->keep_cs_asserted = true;
218 reinit_completion(&sigma_delta->completion);
220 ret = ad_sigma_delta_set_mode(sigma_delta, mode);
221 if (ret < 0)
222 goto out;
224 sigma_delta->irq_dis = false;
225 enable_irq(sigma_delta->irq_line);
226 time_left = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ);
227 if (time_left == 0) {
228 sigma_delta->irq_dis = true;
229 disable_irq_nosync(sigma_delta->irq_line);
230 ret = -EIO;
231 } else {
232 ret = 0;
234 out:
235 sigma_delta->keep_cs_asserted = false;
236 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
237 sigma_delta->bus_locked = false;
238 spi_bus_unlock(sigma_delta->spi->controller);
240 return ret;
242 EXPORT_SYMBOL_NS_GPL(ad_sd_calibrate, IIO_AD_SIGMA_DELTA);
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
250 * Returns 0 on success, an error code otherwise.
252 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
253 const struct ad_sd_calib_data *cb, unsigned int n)
255 unsigned int i;
256 int ret;
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;
264 return 0;
266 EXPORT_SYMBOL_NS_GPL(ad_sd_calibrate_all, IIO_AD_SIGMA_DELTA);
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
274 * Returns: 0 on success, an error value otherwise.
276 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
277 const struct iio_chan_spec *chan, int *val)
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;
284 ret = iio_device_claim_direct_mode(indio_dev);
285 if (ret)
286 return ret;
288 ad_sigma_delta_set_channel(sigma_delta, chan->address);
290 spi_bus_lock(sigma_delta->spi->controller);
291 sigma_delta->bus_locked = true;
292 sigma_delta->keep_cs_asserted = true;
293 reinit_completion(&sigma_delta->completion);
295 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
297 sigma_delta->irq_dis = false;
298 enable_irq(sigma_delta->irq_line);
299 ret = wait_for_completion_interruptible_timeout(
300 &sigma_delta->completion, HZ);
302 if (ret == 0)
303 ret = -EIO;
304 if (ret < 0)
305 goto out;
307 if (sigma_delta->info->data_reg != 0)
308 data_reg = sigma_delta->info->data_reg;
309 else
310 data_reg = AD_SD_REG_DATA;
312 ret = ad_sd_read_reg(sigma_delta, data_reg,
313 DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
314 &raw_sample);
316 out:
317 if (!sigma_delta->irq_dis) {
318 disable_irq_nosync(sigma_delta->irq_line);
319 sigma_delta->irq_dis = true;
322 sigma_delta->keep_cs_asserted = false;
323 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
324 ad_sigma_delta_disable_one(sigma_delta, chan->address);
325 sigma_delta->bus_locked = false;
326 spi_bus_unlock(sigma_delta->spi->controller);
327 iio_device_release_direct_mode(indio_dev);
329 if (ret)
330 return ret;
332 sample = raw_sample >> chan->scan_type.shift;
333 sample &= (1 << chan->scan_type.realbits) - 1;
334 *val = sample;
336 ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
337 if (ret)
338 return ret;
340 return IIO_VAL_INT;
342 EXPORT_SYMBOL_NS_GPL(ad_sigma_delta_single_conversion, IIO_AD_SIGMA_DELTA);
344 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
346 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
347 unsigned int i, slot, samples_buf_size;
348 unsigned int channel;
349 uint8_t *samples_buf;
350 int ret;
352 if (sigma_delta->num_slots == 1) {
353 channel = find_first_bit(indio_dev->active_scan_mask,
354 iio_get_masklength(indio_dev));
355 ret = ad_sigma_delta_set_channel(sigma_delta,
356 indio_dev->channels[channel].address);
357 if (ret)
358 return ret;
359 slot = 1;
360 } else {
362 * At this point update_scan_mode already enabled the required channels.
363 * For sigma-delta sequencer drivers with multiple slots, an update_scan_mode
364 * implementation is mandatory.
366 slot = 0;
367 iio_for_each_active_channel(indio_dev, i) {
368 sigma_delta->slots[slot] = indio_dev->channels[i].address;
369 slot++;
373 sigma_delta->active_slots = slot;
374 sigma_delta->current_slot = 0;
376 if (sigma_delta->active_slots > 1) {
377 ret = ad_sigma_delta_append_status(sigma_delta, true);
378 if (ret)
379 return ret;
382 samples_buf_size = ALIGN(slot * indio_dev->channels[0].scan_type.storagebits, 8);
383 samples_buf_size += sizeof(int64_t);
384 samples_buf = devm_krealloc(&sigma_delta->spi->dev, sigma_delta->samples_buf,
385 samples_buf_size, GFP_KERNEL);
386 if (!samples_buf)
387 return -ENOMEM;
389 sigma_delta->samples_buf = samples_buf;
391 spi_bus_lock(sigma_delta->spi->controller);
392 sigma_delta->bus_locked = true;
393 sigma_delta->keep_cs_asserted = true;
395 ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
396 if (ret)
397 goto err_unlock;
399 sigma_delta->irq_dis = false;
400 enable_irq(sigma_delta->irq_line);
402 return 0;
404 err_unlock:
405 spi_bus_unlock(sigma_delta->spi->controller);
407 return ret;
410 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
412 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
414 reinit_completion(&sigma_delta->completion);
415 wait_for_completion_timeout(&sigma_delta->completion, HZ);
417 if (!sigma_delta->irq_dis) {
418 disable_irq_nosync(sigma_delta->irq_line);
419 sigma_delta->irq_dis = true;
422 sigma_delta->keep_cs_asserted = false;
423 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
425 if (sigma_delta->status_appended)
426 ad_sigma_delta_append_status(sigma_delta, false);
428 ad_sigma_delta_disable_all(sigma_delta);
429 sigma_delta->bus_locked = false;
430 return spi_bus_unlock(sigma_delta->spi->controller);
433 static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
435 struct iio_poll_func *pf = p;
436 struct iio_dev *indio_dev = pf->indio_dev;
437 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
438 uint8_t *data = sigma_delta->rx_buf;
439 unsigned int transfer_size;
440 unsigned int sample_size;
441 unsigned int sample_pos;
442 unsigned int status_pos;
443 unsigned int reg_size;
444 unsigned int data_reg;
446 reg_size = indio_dev->channels[0].scan_type.realbits +
447 indio_dev->channels[0].scan_type.shift;
448 reg_size = DIV_ROUND_UP(reg_size, 8);
450 if (sigma_delta->info->data_reg != 0)
451 data_reg = sigma_delta->info->data_reg;
452 else
453 data_reg = AD_SD_REG_DATA;
455 /* Status word will be appended to the sample during transfer */
456 if (sigma_delta->status_appended)
457 transfer_size = reg_size + 1;
458 else
459 transfer_size = reg_size;
461 switch (reg_size) {
462 case 4:
463 case 2:
464 case 1:
465 status_pos = reg_size;
466 ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[0]);
467 break;
468 case 3:
470 * Data array after transfer will look like (if status is appended):
471 * data[] = { [0][sample][sample][sample][status] }
472 * Keeping the first byte 0 shifts the status postion by 1 byte to the right.
474 status_pos = reg_size + 1;
476 /* We store 24 bit samples in a 32 bit word. Keep the upper
477 * byte set to zero. */
478 ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[1]);
479 break;
483 * For devices sampling only one channel at
484 * once, there is no need for sample number tracking.
486 if (sigma_delta->active_slots == 1) {
487 iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
488 goto irq_handled;
491 if (sigma_delta->status_appended) {
492 u8 converted_channel;
494 converted_channel = data[status_pos] & sigma_delta->info->status_ch_mask;
495 if (converted_channel != sigma_delta->slots[sigma_delta->current_slot]) {
497 * Desync occurred during continuous sampling of multiple channels.
498 * Drop this incomplete sample and start from first channel again.
501 sigma_delta->current_slot = 0;
502 goto irq_handled;
506 sample_size = indio_dev->channels[0].scan_type.storagebits / 8;
507 sample_pos = sample_size * sigma_delta->current_slot;
508 memcpy(&sigma_delta->samples_buf[sample_pos], data, sample_size);
509 sigma_delta->current_slot++;
511 if (sigma_delta->current_slot == sigma_delta->active_slots) {
512 sigma_delta->current_slot = 0;
513 iio_push_to_buffers_with_timestamp(indio_dev, sigma_delta->samples_buf,
514 pf->timestamp);
517 irq_handled:
518 iio_trigger_notify_done(indio_dev->trig);
519 sigma_delta->irq_dis = false;
520 enable_irq(sigma_delta->irq_line);
522 return IRQ_HANDLED;
525 static bool ad_sd_validate_scan_mask(struct iio_dev *indio_dev, const unsigned long *mask)
527 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
529 return bitmap_weight(mask, iio_get_masklength(indio_dev)) <= sigma_delta->num_slots;
532 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
533 .postenable = &ad_sd_buffer_postenable,
534 .postdisable = &ad_sd_buffer_postdisable,
535 .validate_scan_mask = &ad_sd_validate_scan_mask,
538 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
540 struct ad_sigma_delta *sigma_delta = private;
542 complete(&sigma_delta->completion);
543 disable_irq_nosync(irq);
544 sigma_delta->irq_dis = true;
545 iio_trigger_poll(sigma_delta->trig);
547 return IRQ_HANDLED;
551 * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
552 * @indio_dev: The IIO device
553 * @trig: The new trigger
555 * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
556 * device, -EINVAL otherwise.
558 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
560 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
562 if (sigma_delta->trig != trig)
563 return -EINVAL;
565 return 0;
567 EXPORT_SYMBOL_NS_GPL(ad_sd_validate_trigger, IIO_AD_SIGMA_DELTA);
569 static int devm_ad_sd_probe_trigger(struct device *dev, struct iio_dev *indio_dev)
571 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
572 unsigned long irq_flags = irq_get_trigger_type(sigma_delta->irq_line);
573 int ret;
575 if (dev != &sigma_delta->spi->dev) {
576 dev_err(dev, "Trigger parent should be '%s', got '%s'\n",
577 dev_name(dev), dev_name(&sigma_delta->spi->dev));
578 return -EFAULT;
581 sigma_delta->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
582 iio_device_id(indio_dev));
583 if (sigma_delta->trig == NULL)
584 return -ENOMEM;
586 init_completion(&sigma_delta->completion);
588 sigma_delta->irq_dis = true;
590 /* the IRQ core clears IRQ_DISABLE_UNLAZY flag when freeing an IRQ */
591 irq_set_status_flags(sigma_delta->irq_line, IRQ_DISABLE_UNLAZY);
593 /* Allow overwriting the flags from firmware */
594 if (!irq_flags)
595 irq_flags = sigma_delta->info->irq_flags;
597 ret = devm_request_irq(dev, sigma_delta->irq_line,
598 ad_sd_data_rdy_trig_poll,
599 irq_flags | IRQF_NO_AUTOEN,
600 indio_dev->name,
601 sigma_delta);
602 if (ret)
603 return ret;
605 iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
607 ret = devm_iio_trigger_register(dev, sigma_delta->trig);
608 if (ret)
609 return ret;
611 /* select default trigger */
612 indio_dev->trig = iio_trigger_get(sigma_delta->trig);
614 return 0;
618 * devm_ad_sd_setup_buffer_and_trigger() - Device-managed buffer & trigger setup
619 * @dev: Device object to which to bind the life-time of the resources attached
620 * @indio_dev: The IIO device
622 int devm_ad_sd_setup_buffer_and_trigger(struct device *dev, struct iio_dev *indio_dev)
624 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
625 int ret;
627 sigma_delta->slots = devm_kcalloc(dev, sigma_delta->num_slots,
628 sizeof(*sigma_delta->slots), GFP_KERNEL);
629 if (!sigma_delta->slots)
630 return -ENOMEM;
632 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
633 &iio_pollfunc_store_time,
634 &ad_sd_trigger_handler,
635 &ad_sd_buffer_setup_ops);
636 if (ret)
637 return ret;
639 return devm_ad_sd_probe_trigger(dev, indio_dev);
641 EXPORT_SYMBOL_NS_GPL(devm_ad_sd_setup_buffer_and_trigger, IIO_AD_SIGMA_DELTA);
644 * ad_sd_init() - Initializes a ad_sigma_delta struct
645 * @sigma_delta: The ad_sigma_delta device
646 * @indio_dev: The IIO device which the Sigma Delta device is used for
647 * @spi: The SPI device for the ad_sigma_delta device
648 * @info: Device specific callbacks and options
650 * This function needs to be called before any other operations are performed on
651 * the ad_sigma_delta struct.
653 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
654 struct spi_device *spi, const struct ad_sigma_delta_info *info)
656 sigma_delta->spi = spi;
657 sigma_delta->info = info;
659 /* If the field is unset in ad_sigma_delta_info, asume there can only be 1 slot. */
660 if (!info->num_slots)
661 sigma_delta->num_slots = 1;
662 else
663 sigma_delta->num_slots = info->num_slots;
665 if (sigma_delta->num_slots > 1) {
666 if (!indio_dev->info->update_scan_mode) {
667 dev_err(&spi->dev, "iio_dev lacks update_scan_mode().\n");
668 return -EINVAL;
671 if (!info->disable_all) {
672 dev_err(&spi->dev, "ad_sigma_delta_info lacks disable_all().\n");
673 return -EINVAL;
677 if (info->irq_line)
678 sigma_delta->irq_line = info->irq_line;
679 else
680 sigma_delta->irq_line = spi->irq;
682 iio_device_set_drvdata(indio_dev, sigma_delta);
684 return 0;
686 EXPORT_SYMBOL_NS_GPL(ad_sd_init, IIO_AD_SIGMA_DELTA);
688 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
689 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
690 MODULE_LICENSE("GPL v2");