mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Ryan Brue <ryanbrue.dev@gmail.com>
Cc: "David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Matthias Brugger" <matthias.bgg@gmail.com>,
	"AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	"Lee Jones" <lee@kernel.org>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-mediatek@lists.infradead.org, mfd@lists.linux.dev,
	"Roman Vivchar" <rva333@protonmail.com>,
	"Luca Leonardo Scorcia" <l.scorcia@gmail.com>,
	"Andy Shevchenko" <andriy.shevchenko@intel.com>
Subject: Re: [PATCH v3 2/3] iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver
Date: Fri, 25 Sep 2026 03:04:23 +0100	[thread overview]
Message-ID: <20260925030423.18d48456@jic23-hlaptop> (raw)
In-Reply-To: <20260920-rbrue-suez-upstreaming-mt6397-auxadc-v3-2-c00edacbee64@gmail.com>

On Sun, 20 Sep 2026 00:31:52 -0500
Ryan Brue <ryanbrue.dev@gmail.com> wrote:

> The mt6397 AUXADC is a 10-bit ADC behind the SoC's PMIC wrapper. On boards
> built around this PMIC it is the only way to read the battery: the SoC's
> AUXADC is wired to board thermistors and the charger ICs these boards use
> have no ADC of their own.
> 
> Add a driver exposing the battery voltage and battery temperature
> channels. Only those two are described, so a channel ID in the device tree
> indexes the driver's channel array rather than the PMIC's channel number,
> as mt6323-auxadc does. The ready bit lives in a channel's raw result
> register, but the value comes from the chip's factory-calibrated copy.
> 
> The battery voltage is measured through ISENSE, because a board with a
> switching charger in the power path leaves BATSNS on the charger's system
> rail instead of on the pack. The thermistor only reads correctly with the
> PMIC's battery-detect bias and input buffer enabled, which take 20 ms to
> settle. Both are switched back off afterwards.
> 
> Reads average sixteen conversions in software; the chip's sample
> accumulator makes no measurable difference at any setting, so it is left
> at one sample per conversion.
> 
> Assisted-by: LLM
> Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
> 
Hi Ryan,

A few things related to code flow and readability inline.

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


> +static int mt6397_auxadc_isense_enable(struct mt6397_auxadc *adc)
> +{
> +	struct regmap *map = adc->regmap;
> +	int ret;
> +
> +	/*
> +	 * Two of the five independent input enables, not a select field:
> +	 * BATSNS is cleared first so that the two inputs are never enabled
> +	 * onto the battery channel at the same time.

I'm not following this comment. Perhaps consider a rewrite?

> +	 */
> +	ret = regmap_clear_bits(map, MT6397_CHR_CON16, MT6397_CHR_CON16_ADCIN_VBAT_EN);
> +	if (ret)
> +		return ret;
> +
> +	ret = regmap_set_bits(map, MT6397_CHR_CON16, MT6397_CHR_CON16_ADCIN_VSEN_EN);
> +	if (ret)
> +		return ret;
> +
> +	return regmap_set_bits(map, MT6397_AUXADC_CON14,
> +			       MT6397_AUXADC_CON14_CH0_NORM_SEL |
> +			       MT6397_AUXADC_CON14_CH0_LBAT_SEL);
> +}


> +static int mt6397_auxadc_read_channel(struct iio_dev *indio_dev,
> +				      const struct iio_chan_spec *chan,
> +				      int *val)
> +{
> +	struct mt6397_auxadc *adc = iio_priv(indio_dev);
> +	bool isense = chan->channel == MT6397_AUXADC_ISENSE;
> +	int ret, stop_err, release_err;
> +
> +	guard(mutex)(&adc->lock);
Go back to conventional mutex_lock() / mutex_unlock() or wrap
everything under the guard so you can avoid mixing goto and 
guards() (see cleanup.h docs for some of the reason why).

One option would be to just move them up in to the switch statement
from which this is called with scope carefully defined there.
It's not ideal but having a wrapper for just taking a lock
isn't pretty either and you need to do one or the other if you
don't want to fallback to non guard() approaches.

> +
> +	if (isense) {
> +		ret = mt6397_auxadc_isense_enable(adc);
> +		if (!ret)
> +			fsleep(MT6397_AUXADC_ISENSE_SETTLE_US);

Keep to errors out of line 
		if (ret)
			goto err_stop;

		fsleep(MT6397_AUXADC_ISENSE_SETTLE_US);

> +	} else {
> +		ret = mt6397_auxadc_battemp_bias_on(adc);
> +		if (!ret)
> +			fsleep(MT6397_AUXADC_BATTEMP_SETTLE_US);
> +	}
> +
> +	if (!ret)

With changes above you won't get here with ret not 0 so this can go.

> +		ret = mt6397_auxadc_read_burst(adc, chan, val);
> +
> +	/*
> +	 * The teardown is unconditional: once any part of the setup has been
> +	 * written it has to run, even if the setup itself failed part-way.
> +	 * Lowering START also leaves the converter unarmed between reads.
> +	 * A caller has nothing to do with a teardown error, so it is logged
> +	 * rather than returned.
> +	 */
> +	stop_err = regmap_clear_bits(adc->regmap, MT6397_AUXADC_CON1,
> +				     MT6397_AUXADC_CON1_START);
> +
> +	if (isense)
> +		release_err = mt6397_auxadc_isense_disable(adc);
> +	else
> +		release_err = mt6397_auxadc_battemp_bias_off(adc);
> +
> +	if (stop_err || release_err)
> +		dev_err(&indio_dev->dev, "failed to release channel %d\n",
> +			chan->channel);

I would make a little helper for the stuff that has to run.
So everything from the comment onwards. Then

	return mt6397_read_channel_tear_down();

err_stop:
	mt6397_read_channel_tear_down();

	return ret;
(with locks if you don't move those up into a wrapper.

> +
> +	return ret;
> +}

  parent reply	other threads:[~2026-09-25  2:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-20  5:31 [PATCH v3 0/3] iio: adc: add mt6397 PMIC AUXADC support Ryan Brue
2026-09-20  5:31 ` [PATCH v3 1/3] dt-bindings: iio: adc: mediatek,mt6359-auxadc: add mt6397 PMIC AUXADC Ryan Brue
2026-09-21 10:16   ` AngeloGioacchino Del Regno
2026-09-24 12:06   ` Krzysztof Kozlowski
2026-09-20  5:31 ` [PATCH v3 2/3] iio: adc: mt6397-auxadc: add mt6397 PMIC AUXADC driver Ryan Brue
2026-09-21 10:15   ` AngeloGioacchino Del Regno
2026-09-25  2:04   ` Jonathan Cameron [this message]
2026-09-20  5:31 ` [PATCH v3 3/3] mfd: mt6397-core: Add mt6397 AUXADC support Ryan Brue
2026-09-21 10:16   ` AngeloGioacchino Del Regno
2026-09-20 23:17 ` [PATCH v3 0/3] iio: adc: add mt6397 PMIC " Jonathan Cameron
2026-09-21  0:22   ` Ryan Brue

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=20260925030423.18d48456@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=krzk+dt@kernel.org \
    --cc=l.scorcia@gmail.com \
    --cc=lee@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=mfd@lists.linux.dev \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=rva333@protonmail.com \
    --cc=ryanbrue.dev@gmail.com \
    /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®