mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Chi-Wen Weng <cwweng.linux@gmail.com>
Cc: 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: Sun, 22 Nov 2026 03:06:47 +0000	[thread overview]
Message-ID: <20261122030647.70b41b61@jic23-hlaptop> (raw)
In-Reply-To: <20261117082832.1551574-4-cwweng.linux@gmail.com>

On Tue, 17 Nov 2026 16:28:32 +0800
Chi-Wen Weng <cwweng.linux@gmail.com> wrote:

> From: Chi-Wen Weng <cwweng@nuvoton.com>
> 
> 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.
> 
> Signed-off-by: Chi-Wen Weng <cwweng@nuvoton.com>
Various comments inline to add to Andy's.

Thanks,

Jonathan

> diff --git a/drivers/iio/adc/ma35d1_eadc.c b/drivers/iio/adc/ma35d1_eadc.c
> new file mode 100644
> index 000000000000..d9a715efcc6b
> --- /dev/null
> +++ b/drivers/iio/adc/ma35d1_eadc.c
...

> +
> +/*
> + * Keep the register map uncached because status and conversion data are
> + * updated by hardware, and some status bits use write-one-to-clear semantics.

That's a reason to set volatile and precious but not general one to say
don't cache anything.

> + */
> +static const struct regmap_config ma35d1_adc_regmap_config = {
> +	.reg_bits = 32,
> +	.val_bits = 32,
> +	.reg_stride = 4,
> +	.max_register = MA35D1_EADC_REFADJCTL,
> +	.rd_table = &ma35d1_adc_readable_table,
> +	.wr_table = &ma35d1_adc_writable_table,
> +	.precious_table = &ma35d1_adc_precious_table,
> +};



> +
> +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);

Check line wraps. That one fits under 80 chars.

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

> +
> +static int ma35d1_adc_parse_channels(struct iio_dev *indio_dev,
> +				     struct device *dev)
> +{
> +	DECLARE_BITMAP(used_single_inputs, MA35D1_EADC_MAX_EXT_CHANNELS) = { };
> +	DECLARE_BITMAP(used_diff_pairs, MA35D1_EADC_NUM_DIFF_PAIRS) = { };
> +	DECLARE_BITMAP(used_regs, MA35D1_EADC_MAX_EXT_CHANNELS) = { };
> +	struct iio_chan_spec *channels;
> +	unsigned int scan_index = 0;
> +	int num_channels;
> +	int ret;
> +
> +	num_channels = device_get_child_node_count(dev);
> +	if (!num_channels)
> +		return dev_err_probe(dev, -ENOENT, "no ADC channels configured\n");
> +
> +	if (num_channels > MA35D1_EADC_MAX_EXT_CHANNELS)
> +		return dev_err_probe(dev, -ENOSPC, "too many ADC channels\n");
> +
> +	channels = devm_kcalloc(dev, size_add(num_channels, 1),
> +				sizeof(*channels), GFP_KERNEL);

You can now use devm_kzalloc_objs()
A patch to apply that across the kernel went in during last week
and the code is there since rc1.

I don't mind either way as there will inevitably be a second round
of conversions next cycle.


> +	if (!channels)
> +		return -ENOMEM;
> +
> +	device_for_each_child_node_scoped(dev, child) {
> +		bool has_diff, has_single;
> +		u32 reg;
> +
> +		ret = fwnode_property_read_u32(child, "reg", &reg);
> +		if (ret)
> +			return dev_err_probe(dev, ret,
> +					     "missing channel reg property\n");
> +
> +		if (reg >= MA35D1_EADC_MAX_EXT_CHANNELS)
> +			return dev_err_probe(dev, -ENOSPC,
> +					     "invalid ADC channel index %u\n", reg);
> +
> +		if (test_and_set_bit(reg, used_regs))
> +			return dev_err_probe(dev, -EEXIST,
> +					     "duplicate ADC channel index %u\n", reg);
> +
> +		has_single = fwnode_property_present(child, "single-channel");
> +		has_diff = fwnode_property_present(child, "diff-channels");
> +		if (has_single && has_diff)
> +			return dev_err_probe(dev, -EINVAL,
> +					     "channel %u has conflicting input properties\n",
> +					     reg);
> +
> +		if (has_diff)
> +			ret = ma35d1_adc_parse_diff_channel(dev, child,
> +							    &channels[scan_index],
> +							    used_diff_pairs, reg,
> +							    scan_index);
> +		else
> +			ret = ma35d1_adc_parse_single_channel(dev, child,
> +							      &channels[scan_index],
> +							      used_single_inputs,
> +							      has_single, reg,
> +							      scan_index);
> +		if (ret)
> +			return ret;
> +
> +		scan_index++;
> +	}
> +
> +	channels[scan_index] = IIO_CHAN_SOFT_TIMESTAMP(scan_index);
> +	indio_dev->channels = channels;
> +	indio_dev->num_channels = scan_index + 1;
> +
> +	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");

This is just 0. The calc for real values above will end up with 0 in vref_mv if
there is less than 1 mV so not sure handling this separately brings much value.
you have to assume vref_mv might be zero anyway.


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

> +
> +static int ma35d1_adc_suspend(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = dev_get_drvdata(dev);
> +	struct ma35d1_adc *adc = iio_priv(indio_dev);
> +	bool buffer_enabled;
> +	int ret;
> +
> +	/* A failed resume leaves the device and its triggering suspended. */
> +	scoped_guard(mutex, &adc->lock) {
> +		if (adc->suspended)
> +			return 0;
> +	}
> +
> +	/*
> +	 * pollfunc->irq is not initialized until the pollfunc is first
> +	 * attached to a trigger, so only suspend and resume triggering
> +	 * while the buffer is enabled.

True enough but isn't it also guarded by if indio_dev->pollfunc
inside the iio_device_suspend_triggering()?

I think in the case you are avoiding it is a noop anyway.

disable_irq() nests fine so it shouldn't even matter if anyone turned
it on yet.


> +	 */
> +	buffer_enabled = iio_buffer_enabled(indio_dev);
> +	if (buffer_enabled) {
> +		ret = iio_device_suspend_triggering(indio_dev);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	ret = ma35d1_adc_suspend_hw(adc);
> +	if (ret && buffer_enabled)
> +		iio_device_resume_triggering(indio_dev);
> +
> +	return ret;
> +}
> +
> +static int ma35d1_adc_resume(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = dev_get_drvdata(dev);
> +	struct ma35d1_adc *adc = iio_priv(indio_dev);
> +	int ret;
> +
> +	scoped_guard(mutex, &adc->lock) {
> +		if (!adc->suspended)
> +			return 0;
> +	}
> +
> +	ret = ma35d1_adc_resume_hw(adc);
> +	if (ret)
> +		return ret;
> +
> +	if (!iio_buffer_enabled(indio_dev))
> +		return 0;

As above, I'm missing why we need to protect the call
to resume_triggering.  The pollfunc can't change between
suspend and resume so there is either one there or not
in both cases and the guards inside that call should take care
of the rest.  If I'm missing something we should look to
fix it in those calls!

> +
> +	return iio_device_resume_triggering(indio_dev);
> +}
> +
> +static DEFINE_SIMPLE_DEV_PM_OPS(ma35d1_adc_pm_ops,
> +				ma35d1_adc_suspend, ma35d1_adc_resume);
> +
> +static const struct of_device_id ma35d1_adc_of_match[] = {
> +	{ .compatible = "nuvoton,ma35d1-eadc" },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, ma35d1_adc_of_match);
> +
> +static struct platform_driver ma35d1_adc_driver = {
> +	.probe = ma35d1_adc_probe,
> +	.driver = {
> +		.name = "ma35d1-eadc",
> +		.of_match_table = ma35d1_adc_of_match,
> +		.pm = pm_sleep_ptr(&ma35d1_adc_pm_ops),
> +	},
> +};
> +module_platform_driver(ma35d1_adc_driver);
> +
> +MODULE_AUTHOR("Chi-Wen Weng <cwweng@nuvoton.com>");
> +MODULE_DESCRIPTION("Nuvoton MA35D1 EADC driver");
> +MODULE_LICENSE("GPL");


      parent reply	other threads:[~2026-11-22  3:06 UTC|newest]

Thread overview: 8+ 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-18 16:31   ` Rob Herring (Arm)
2026-11-17  8:28 ` [PATCH v5 2/3] dt-bindings: iio: adc: Add Nuvoton MA35D1 EADC Chi-Wen Weng
2026-11-18 16:33   ` Rob Herring (Arm)
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
2026-11-22  3:06   ` Jonathan Cameron [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=20261122030647.70b41b61@jic23-hlaptop \
    --to=jic23@kernel.org \
    --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=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

all inboxes | Powered by JetHome®