From: Jonathan Cameron <jic23@kernel.org>
To: "Duje Mihanović" <duje@dujemihanovic.xyz>
Cc: "David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Karel Balej" <balejk@matfyz.cz>, "Lee Jones" <lee@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"David Wronek" <david@mainlining.org>,
phone-devel@vger.kernel.org,
~postmarketos/upstreaming@lists.sr.ht,
linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org,
devicetree@vger.kernel.org
Subject: Re: [PATCH v2 2/3] iio: adc: Add driver for Marvell 88PM886 PMIC ADC
Date: Mon, 1 Sep 2025 19:18:00 +0100 [thread overview]
Message-ID: <20250901191800.6a7674fe@jic23-huawei> (raw)
In-Reply-To: <20250831-88pm886-gpadc-v2-2-759c1e14d95f@dujemihanovic.xyz>
On Sun, 31 Aug 2025 12:33:05 +0200
Duje Mihanović <duje@dujemihanovic.xyz> wrote:
> Marvell's 88PM886 PMIC has a so-called General Purpose ADC used for
> monitoring various system voltages and temperatures. Add the relevant
> register definitions to the MFD header and a driver for the ADC.
>
> Signed-off-by: Duje Mihanović <duje@dujemihanovic.xyz>
A few additional comments from me inline.
Thanks,
Jonathan
> diff --git a/drivers/iio/adc/88pm886-gpadc.c b/drivers/iio/adc/88pm886-gpadc.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..4622d2525e0edeed89c6e6d43336b177590aa885
> --- /dev/null
> +++ b/drivers/iio/adc/88pm886-gpadc.c
> +
> +#include <linux/mfd/88pm886.h>
> +
> +struct pm886_gpadc {
> + struct regmap *map;
> +};
> +
> +static const int pm886_gpadc_regs[] = {
> + PM886_REG_GPADC_VSC,
> + PM886_REG_GPADC_VCHG_PWR,
> + PM886_REG_GPADC_VCF_OUT,
> + PM886_REG_GPADC_VBAT,
> + PM886_REG_GPADC_VBAT_SLP,
> + PM886_REG_GPADC_VBUS,
> +
> + PM886_REG_GPADC_GPADC0,
> + PM886_REG_GPADC_GPADC1,
> + PM886_REG_GPADC_GPADC2,
> + PM886_REG_GPADC_GPADC3,
> +
> + PM886_REG_GPADC_GND_DET1,
> + PM886_REG_GPADC_GND_DET2,
> + PM886_REG_GPADC_MIC_DET,
> +
> + PM886_REG_GPADC_TINT,
> +};
> +
> +/* Must be kept in sync with the table above */
Define this first and use it to assign the table entries
[VSC_CHAN] = PM886_REG_GPADC_VSC,
etc and then no need for the comment as they will naturally
> +enum pm886_gpadc_channel {
> + VSC_CHAN,
> + VCHG_PWR_CHAN,
> + VCF_OUT_CHAN,
> + VBAT_CHAN,
> + VBAT_SLP_CHAN,
> + VBUS_CHAN,
> +
> + GPADC0_CHAN,
> + GPADC1_CHAN,
> + GPADC2_CHAN,
> + GPADC3_CHAN,
> +
> + GND_DET1_CHAN,
> + GND_DET2_CHAN,
> + MIC_DET_CHAN,
> +
> + TINT_CHAN,
> +};
> +static int gpadc_get_raw(struct iio_dev *iio, enum pm886_gpadc_channel chan)
> +{
> + struct pm886_gpadc *gpadc = iio_priv(iio);
> + __be16 buf;
> + int ret;
> +
> + ret = regmap_bulk_read(gpadc->map, pm886_gpadc_regs[chan], &buf, sizeof(buf));
> + return !ret ? be16_to_cpu(buf) >> 4 : ret;
At very least flip the logic, probably better not using a ternary at all though.
> +}
>
> +static int
> +__pm886_gpadc_read_raw(struct iio_dev *iio, struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
> +{
> + unsigned long lsb = chan->address;
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_RAW:
> + *val = gpadc_get_raw(iio, chan->channel);
> + if (*val < 0)
> + return *val;
> +
> + dev_dbg(&iio->dev, "chan: %d, raw: %d\n", chan->channel, *val);
> + return IIO_VAL_INT;
> + case IIO_CHAN_INFO_SCALE:
> + *val = lsb;
> + *val2 = (MICRO / MILLI);
> + return chan->type == IIO_VOLTAGE
> + ? IIO_VAL_FRACTIONAL
> + : IIO_VAL_INT;
Split it so that in the IIO_VAL_INT case you don't set val2.
This is a case where burning a few lines for clearer code is a good idea.
Similar to the other case Andy pointed out.
> +
> +static int pm886_gpadc_setup(struct regmap *map, bool enable)
I wonder if you should just split this as only 1-2 lines are actually shared
static int pm886_gpadc_enable(struct regmap *map)
{
const u8 config[] = {
PM886_GPADC_CONFIG1_EN_ALL,
PM886_GPADC_CONFIG2_EN_ALL,
PM886_GPADC_GND_DET2_EN
};
int ret;
/* Enable the ADC block */
ret = regmap_set_bits(map, PM886_REG_GPADC_CONFIG(0x6), BIT(0));
if (ret)
return ret;
/* Enable all channels */
return regmap_bulk_write(map, PM886_REG_GPADC_CONFIG(0x1), config, ARRAY_SIZE(config));
}
static int pm886_gpadc_disable(struct regmap *map)
{
return regmap_clear_bits(map, PM886_REG_GPADC_CONFIG(0x6), BIT(0));
}
Which I think ends up only 1 line longer than the more complex combined function.
> +{
> + const u8 config[] = {
> + PM886_GPADC_CONFIG1_EN_ALL,
> + PM886_GPADC_CONFIG2_EN_ALL,
> + PM886_GPADC_GND_DET2_EN
> + };
> + int ret;
> +
> + /* Enable/disable the ADC block */
> + ret = regmap_assign_bits(map, PM886_REG_GPADC_CONFIG(0x6), BIT(0), enable);
> + if (ret)
> + return ret;
> +
> + if (!enable)
> + return 0;
> +
> + /* If enabling, enable all channels */
> + return regmap_bulk_write(map, PM886_REG_GPADC_CONFIG(0x1), config, ARRAY_SIZE(config));
> +}
> +static int pm886_gpadc_runtime_resume(struct device *dev)
> +{
> + struct iio_dev *iio = dev_get_drvdata(dev);
> + struct pm886_gpadc *gpadc = iio_priv(iio);
> +
> + return pm886_gpadc_setup(gpadc->map, true);
> +}
> +
> +static int pm886_gpadc_runtime_suspend(struct device *dev)
> +{
> + struct iio_dev *iio = dev_get_drvdata(dev);
> + struct pm886_gpadc *gpadc = iio_priv(iio);
> +
> + return pm886_gpadc_setup(gpadc->map, false);
> +}
next prev parent reply other threads:[~2025-09-01 18:18 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-31 10:33 [PATCH v2 0/3] Marvell 88PM886 PMIC GPADC driver Duje Mihanović
2025-08-31 10:33 ` [PATCH v2 1/3] dt-bindings: mfd: 88pm886: Add #io-channel-cells Duje Mihanović
2025-08-31 16:49 ` Krzysztof Kozlowski
2025-08-31 18:40 ` Karel Balej
2025-08-31 10:33 ` [PATCH v2 2/3] iio: adc: Add driver for Marvell 88PM886 PMIC ADC Duje Mihanović
2025-08-31 19:24 ` Karel Balej
2025-08-31 20:19 ` Duje Mihanović
2025-09-01 12:43 ` Andy Shevchenko
2025-09-01 18:04 ` Jonathan Cameron
2025-09-01 15:35 ` Andy Shevchenko
2025-09-01 16:51 ` Duje Mihanović
2025-09-02 9:50 ` Andy Shevchenko
2025-09-01 18:18 ` Jonathan Cameron [this message]
2025-08-31 10:33 ` [PATCH v2 3/3] mfd: 88pm886: Add GPADC cell Duje Mihanović
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=20250901191800.6a7674fe@jic23-huawei \
--to=jic23@kernel.org \
--cc=andy@kernel.org \
--cc=balejk@matfyz.cz \
--cc=conor+dt@kernel.org \
--cc=david@mainlining.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=duje@dujemihanovic.xyz \
--cc=krzk+dt@kernel.org \
--cc=lee@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=phone-devel@vger.kernel.org \
--cc=robh@kernel.org \
--cc=~postmarketos/upstreaming@lists.sr.ht \
/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