Devicetree
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Chi-Wen Weng <cwweng.linux@gmail.com>
Cc: jic23@kernel.org, robh@kernel.org, krzk+dt@kernel.org,
	conor+dt@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
	andy@kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, chernyakin.victor.j@outlook.com,
	cwweng@nuvoton.com
Subject: Re: [PATCH v5 3/3] iio: adc: Add Nuvoton MA35D1 EADC driver
Date: Tue, 17 Nov 2026 11:43:10 +0200	[thread overview]
Message-ID: <avwhriewntd6_F8u@ashevche-desk.local> (raw)
In-Reply-To: <20261117082832.1551574-4-cwweng.linux@gmail.com>

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



      reply	other threads:[~2026-11-17  9:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  8:28 [PATCH v5 0/3] iio: adc: Add Nuvoton MA35D1 EADC support Chi-Wen Weng
2026-11-17  8:28 ` [PATCH v5 1/3] dt-bindings: iio: adc: Fix channel property validation Chi-Wen Weng
2026-11-17  8:28 ` [PATCH v5 2/3] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng
2026-11-17  8:42   ` sashiko-bot
2026-11-17  8:28 ` [PATCH v5 3/3] iio: adc: Add Nuvoton MA35D1 EADC driver Chi-Wen Weng
2026-11-17  9:43   ` Andy Shevchenko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=avwhriewntd6_F8u@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=chernyakin.victor.j@outlook.com \
    --cc=conor+dt@kernel.org \
    --cc=cwweng.linux@gmail.com \
    --cc=cwweng@nuvoton.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox