Commit f2bdea86 authored by Nuno Sá's avatar Nuno Sá Committed by Jonathan Cameron
Browse files

iio: adc: vf610_adc: add helper function to read samples



This is a precursor change to make it simpler to remove the 'mlock'
usage. Having the code in it's own helper function, also makes it easier
to read the error paths.

Signed-off-by: default avatarNuno Sá <nuno.sa@analog.com>
Reviewed-by: default avatarHaibo Chen <haibo.chen@nxp.com>
Link: https://lore.kernel.org/r/20221004134909.1692021-10-nuno.sa@analog.com


Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 8433aa35
Loading
Loading
Loading
Loading
+54 −40
Original line number Diff line number Diff line
@@ -622,40 +622,33 @@ static const struct attribute_group vf610_attribute_group = {
	.attrs = vf610_attributes,
};

static int vf610_read_raw(struct iio_dev *indio_dev,
			struct iio_chan_spec const *chan,
			int *val,
			int *val2,
			long mask)
static int vf610_read_sample(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan, int *val)
{
	struct vf610_adc *info = iio_priv(indio_dev);
	unsigned int hc_cfg;
	long ret;
	int ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
	case IIO_CHAN_INFO_PROCESSED:
	mutex_lock(&indio_dev->mlock);
	if (iio_buffer_enabled(indio_dev)) {
			mutex_unlock(&indio_dev->mlock);
			return -EBUSY;
		ret = -EBUSY;
		goto out_unlock;
	}

	reinit_completion(&info->completion);
	hc_cfg = VF610_ADC_ADCHC(chan->channel);
	hc_cfg |= VF610_ADC_AIEN;
	writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
		ret = wait_for_completion_interruptible_timeout
				(&info->completion, VF610_ADC_TIMEOUT);
	ret = wait_for_completion_interruptible_timeout(&info->completion,
							VF610_ADC_TIMEOUT);
	if (ret == 0) {
			mutex_unlock(&indio_dev->mlock);
			return -ETIMEDOUT;
		}
		if (ret < 0) {
			mutex_unlock(&indio_dev->mlock);
			return ret;
		ret = -ETIMEDOUT;
		goto out_unlock;
	}

	if (ret < 0)
		goto out_unlock;

	switch (chan->type) {
	case IIO_VOLTAGE:
		*val = info->value;
@@ -671,11 +664,32 @@ static int vf610_read_raw(struct iio_dev *indio_dev,

		break;
	default:
			mutex_unlock(&indio_dev->mlock);
			return -EINVAL;
		ret = -EINVAL;
		break;
	}

out_unlock:
	mutex_unlock(&indio_dev->mlock);

	return ret;
}

static int vf610_read_raw(struct iio_dev *indio_dev,
			struct iio_chan_spec const *chan,
			int *val,
			int *val2,
			long mask)
{
	struct vf610_adc *info = iio_priv(indio_dev);
	long ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
	case IIO_CHAN_INFO_PROCESSED:
		ret = vf610_read_sample(indio_dev, chan, val);
		if (ret < 0)
			return ret;

		return IIO_VAL_INT;

	case IIO_CHAN_INFO_SCALE: