[PATCH v5 3/3] iio: adc: Add Nuvoton MA35D1 EADC driver

Andy Shevchenko andriy.shevchenko at intel.com
Tue Nov 17 01:43:10 PST 2026


On Tue, Nov 17, 2026 at 04:28:32PM +0800, Chi-Wen Weng wrote:
> 
> Add an IIO driver for the Enhanced Analog-to-Digital Converter found on
> Nuvoton MA35D1 SoCs.
> 
> Support single-ended and fixed differential inputs, direct conversion,
> and triggered buffered capture. This initial implementation uses sample
> module 0 and therefore supports one active voltage channel per buffered
> scan.

...

> +static int ma35d1_adc_read_raw(struct iio_dev *indio_dev,
> +			       const struct iio_chan_spec *chan,
> +			       int *val, int *val2, long mask)
> +{
> +	struct ma35d1_adc *adc = iio_priv(indio_dev);
> +	u16 raw;
> +	int ret;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW: {
> +		IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
> +		if (IIO_DEV_ACQUIRE_FAILED(claim))
> +			return -EBUSY;
> +
> +		guard(mutex)(&adc->lock);
> +
> +		if (adc->suspended)
> +			return -EBUSY;
> +
> +		ret = ma35d1_adc_read_conversion(adc, chan, &raw);
> +		if (ret)
> +			return ret;
> +
> +		if (chan->differential)
> +			*val = sign_extend32(raw, MA35D1_EADC_RESOLUTION - 1);
> +		else
> +			*val = raw;
> +
> +		return IIO_VAL_INT;
> +	}
> +	case IIO_CHAN_INFO_SCALE:
> +		*val = adc->vref_mV;
> +		/* Differential mode has a VREF / 2048 LSB; single-ended uses /4096. */
> +		*val2 = chan->differential ? MA35D1_EADC_DIFF_SCALE_BITS : MA35D1_EADC_RESOLUTION;

+ blank line as it's done elsewhere.

> +		return IIO_VAL_FRACTIONAL_LOG2;
> +	default:
> +		return -EINVAL;
> +	}
> +}

...

> +static int ma35d1_adc_update_scan_mode(struct iio_dev *indio_dev,
> +				       const unsigned long *scan_mask)
> +{
> +	struct ma35d1_adc *adc = iio_priv(indio_dev);
> +	const struct iio_chan_spec *scan_chan = NULL;
> +	int bit;
> +
> +	if (!iio_validate_scan_mask_onehot(indio_dev, scan_mask))
> +		return -EINVAL;
> +
> +	iio_for_each_active_channel(indio_dev, bit) {
> +		const struct iio_chan_spec *chan = &indio_dev->channels[bit];
> +
> +		if (chan->type != IIO_VOLTAGE)
> +			return -EINVAL;
> +
> +		scan_chan = chan;
> +		break;
> +	}
> +
> +	if (!scan_chan)
> +		return -EINVAL;

> +	adc->scan_chan = scan_chan;

So, this is only valid when scan_chan is not NULL, and this is the only case
inside the loop, hence temporary 'scan_chan' variable is not required at all.

	iio_for_each_active_channel(indio_dev, bit) {
		const struct iio_chan_spec *chan = &indio_dev->channels[bit];

		if (chan->type != IIO_VOLTAGE)
			return -EINVAL;

		adc->scan_chan = chan;

		return 0;
	}

	return -EINVAL;

> +	return 0;
> +}

...

> +static void ma35d1_adc_trigger_capture(struct iio_poll_func *pf)
> +{
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +	struct ma35d1_adc *adc = iio_priv(indio_dev);
> +	struct {
> +		u16 channel;
> +		aligned_s64 timestamp;
> +	} scan = { };
> +	int ret;
> +
> +	guard(mutex)(&adc->lock);
> +
> +	if (adc->suspended || !adc->scan_chan)
> +		return;
> +
> +	ret = ma35d1_adc_read_conversion(adc, adc->scan_chan,
> +					 &scan.channel);

This fits a single line.

> +	if (ret)
> +		return;
> +
> +	iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), pf->timestamp);
> +}

...

> +static int ma35d1_adc_parse_single_channel(struct device *dev,
> +					   struct fwnode_handle *child,
> +					   struct iio_chan_spec *chan,
> +					   unsigned long *used_single_inputs,
> +					   bool has_single, u32 reg,
> +					   unsigned int scan_index)
> +{

	const char *propname = "single-channel";

> +	u32 input = reg;
> +	int ret;
> +
> +	if (has_single) {
> +		ret = fwnode_property_read_u32(child, "single-channel", &input);

		ret = fwnode_property_read_u32(child, propname, &input);

> +		if (ret)
> +			return dev_err_probe(dev, ret,
> +					     "invalid single-channel for index %u\n",
> +					     reg);

			return dev_err_probe(dev, ret, "invalid %s for index %u\n",
					     propname, reg);

Similar approach can be used for differential channel parser.

Or even supply that as a parameter, so you will have defined that only once
in _parse_channels().

> +	}
> +
> +	if (input >= MA35D1_EADC_MAX_EXT_CHANNELS)
> +		return dev_err_probe(dev, -ECHRNG, "invalid ADC input %u\n", input);
> +
> +	if (test_and_set_bit(input, used_single_inputs))
> +		return dev_err_probe(dev, -EEXIST, "duplicate ADC input %u\n", input);
> +
> +	*chan = (struct iio_chan_spec) {
> +		.type = IIO_VOLTAGE,
> +		.indexed = 1,
> +		.channel = input,
> +		.address = reg,
> +		.scan_index = scan_index,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				      BIT(IIO_CHAN_INFO_SCALE),
> +		.scan_type = {
> +			.format = IIO_SCAN_FORMAT_UNSIGNED_INT,
> +			.realbits = MA35D1_EADC_RESOLUTION,
> +			.storagebits = 16,
> +			.endianness = IIO_CPU,
> +		},
> +	};
> +
> +	return 0;
> +}

...

> +static int ma35d1_adc_init_vref(struct ma35d1_adc *adc, struct device *dev)
> +{
> +	int ret;
> +
> +	ret = devm_regulator_get_enable_read_voltage(dev, "vref");
> +	if (ret == -ENODEV) {
> +		adc->vref_mV = MA35D1_EADC_INTERNAL_VREF_mV;
> +	} else if (ret < 0) {
> +		return dev_err_probe(dev, ret, "failed to get VREF supply\n");
> +	} else if (ret > 0) {
> +		adc->external_vref = true;
> +		adc->vref_mV = ret / (MICRO / MILLI);
> +	} else {
> +		return dev_err_probe(dev, -EINVAL, "invalid VREF voltage\n");

What to do, if ret is < 1000 and > 0 and the above will simply make vref_mV == 0?
Wouldn't the same constraint be applied and respective error message issued?

> +	}
> +
> +	return 0;
> +}

-- 
With Best Regards,
Andy Shevchenko





More information about the linux-arm-kernel mailing list