All of lore.kernel.org
 help / color / mirror / Atom feed
From: Liam Beguin <liambeguin@gmail.com>
To: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Cc: Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>, Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Conor Dooley <conor+dt@kernel.org>,
	linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org,
	devicetree@vger.kernel.org
Subject: Re: [PATCH 1/3] iio: adc: add ltc2309 support
Date: Thu, 24 Aug 2023 15:32:54 -0400	[thread overview]
Message-ID: <20230824193254.GB3659959@shaak> (raw)
In-Reply-To: <fe4a3bf9-2260-fb41-a20b-2bf05b6c02e9@linaro.org>

On Thu, Aug 24, 2023 at 08:00:11PM +0200, Krzysztof Kozlowski wrote:
> On 24/08/2023 18:55, Liam Beguin wrote:
> > The LTC2309 is an 8-Channel, 12-Bit SAR ADC with an I2C Interface.
> > 
> > This implements support for all single-ended and differential channels,
> > in unipolar mode only.
> > 
> > Signed-off-by: Liam Beguin <liambeguin@gmail.com>
> > ---
> >  drivers/iio/adc/Kconfig   |  10 ++
> >  drivers/iio/adc/Makefile  |   1 +
> >  drivers/iio/adc/ltc2309.c | 232 ++++++++++++++++++++++++++++++++++++++++++++++
> >  3 files changed, 243 insertions(+)
> > 
> 
> 
> 
> > +static int ltc2309_read_raw(struct iio_dev *indio_dev,
> > +			    struct iio_chan_spec const *chan, int *val,
> > +			    int *val2, long mask)
> > +{
> > +	struct ltc2309 *ltc2309 = iio_priv(indio_dev);
> > +	u16 buf;
> > +	int ret;
> > +	u8 din;
> > +
> > +	mutex_lock(&ltc2309->lock);
> > +
> > +	switch (mask) {
> > +	case IIO_CHAN_INFO_RAW:
> > +		din = FIELD_PREP(LTC2309_DIN_CH_MASK, chan->address & 0x0f) |
> > +			FIELD_PREP(LTC2309_DIN_UNI, 1) |
> > +			FIELD_PREP(LTC2309_DIN_SLEEP, 0);
> > +
> > +		ret = i2c_smbus_write_byte(ltc2309->client, din);
> > +		if (ret < 0) {
> > +			dev_err(ltc2309->dev, "i2c command failed: %pe\n",
> > +				ERR_PTR(ret));
> > +			goto out;
> > +		}
> > +
> > +		ret = i2c_master_recv(ltc2309->client, (char *)&buf, 2);
> > +		if (ret < 0) {
> > +			dev_err(ltc2309->dev, "i2c read failed: %pe\n",
> > +				ERR_PTR(ret));
> > +			goto out;
> > +		}
> > +
> > +		*val = be16_to_cpu(buf) >> 4;
> > +
> > +		ret = IIO_VAL_INT;
> > +		break;
> > +	case IIO_CHAN_INFO_SCALE:
> > +		*val = ltc2309->vref_mv;
> > +		*val2 = LTC2309_ADC_RESOLUTION;
> > +		ret = IIO_VAL_FRACTIONAL_LOG2;
> 
> Why this case is in critical section?
> 

my bad, I'll reduce it to INFO_RAW.

> > +		break;
> > +	default:
> > +		ret = -EINVAL;
> > +		break;
> > +	}
> > +
> > +out:
> > +	mutex_unlock(&ltc2309->lock);
> > +	return ret;
> > +}
> > +
> > +static const struct iio_info ltc2309_info = {
> > +	.read_raw = ltc2309_read_raw,
> > +};
> > +
> > +static int ltc2309_probe(struct i2c_client *client,
> > +			 const struct i2c_device_id *id)
> > +{
> > +	struct iio_dev *indio_dev;
> > +	struct ltc2309 *ltc2309;
> > +	int ret = 0;
> > +
> > +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*ltc2309));
> > +	if (!indio_dev)
> > +		return -ENOMEM;
> > +
> > +	i2c_set_clientdata(client, indio_dev);
> > +
> > +	ltc2309 = iio_priv(indio_dev);
> > +	ltc2309->dev = &indio_dev->dev;
> > +	ltc2309->client = client;
> > +	ltc2309->vref_mv = 4096; /* Default to the internal ref */
> > +
> > +	indio_dev->name = DRIVER_NAME;
> > +	indio_dev->dev.parent = &client->dev;
> > +	indio_dev->modes = INDIO_DIRECT_MODE;
> > +	indio_dev->channels = ltc2309_channels;
> > +	indio_dev->num_channels = ARRAY_SIZE(ltc2309_channels);
> > +	indio_dev->info = &ltc2309_info;
> > +
> > +	ltc2309->refcomp = devm_regulator_get_optional(&client->dev, "refcomp");
> > +	if (!IS_ERR_OR_NULL(ltc2309->refcomp)) {
> > +		ret = regulator_enable(ltc2309->refcomp);
> > +		if (ret) {
> > +			dev_err(ltc2309->dev, "failed to enable REFCOMP\n");
> > +			return ret;
> > +		}
> > +
> > +		ret = regulator_get_voltage(ltc2309->refcomp);
> > +		if (ret < 0)
> 
> You have unbalanced regulator. Same in all further error paths.
> 

Right, will fix.

I was going to add an action with devm_add_action_or_reset(), and
noticed a lot of duplicate code adding a custom disable action. Does
adding something like this make sense?

-- >8 --

diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c
index 90bb0d178885..ff94f35fad87 100644
--- a/drivers/regulator/devres.c
+++ b/drivers/regulator/devres.c
@@ -70,12 +70,17 @@ struct regulator *devm_regulator_get_exclusive(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);

-static void regulator_action_disable(void *d)
+/**
+ * regulator_action_disable - Generic disable action for managed resource
+ * @d: regulator to disable
+ */
+void regulator_action_disable(void *d)
 {
 	struct regulator *r = (struct regulator *)d;

 	regulator_disable(r);
 }
+EXPORT_SYMBOL_GPL(regulator_action_disable);

 static int _devm_regulator_get_enable(struct device *dev, const char *id,
 				      int get_type)
diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/consumer.h
index 39b666b40ea6..4c018af5d008 100644
--- a/include/linux/regulator/consumer.h
+++ b/include/linux/regulator/consumer.h
@@ -207,6 +207,8 @@ struct regulator *__must_check regulator_get_optional(struct device *dev,
 						      const char *id);
 struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
 							   const char *id);
+
+void regulator_action_disable(void *d);
 int devm_regulator_get_enable(struct device *dev, const char *id);
 int devm_regulator_get_enable_optional(struct device *dev, const char *id);
 void regulator_put(struct regulator *regulator);

-- >8 --

This would let consumers reuse it directly with something like:

	devm_add_action_or_reset(ltc2309->dev,
				 regulator_action_disable,
				 ltc2309->vref);


Maybe it should be a separate series, including the cleanup?

> > +			return ret;
> > +
> > +		ltc2309->vref_mv = ret / 1000;
> > +		if (ret)
> > +			return ret;

I just noticed this extra if. will remove too.

> > +	}
> > +
> > +	mutex_init(&ltc2309->lock);
> > +
> > +	return devm_iio_device_register(&client->dev, indio_dev);
> > +}
> > +
> 
> 
> Best regards,
> Krzysztof
> 

Thanks,
Liam

  reply	other threads:[~2023-08-24 19:33 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-24 16:55 [PATCH 0/3] iio: adc: add LTC2309 support Liam Beguin
2023-08-24 16:55 ` [PATCH 1/3] iio: adc: add ltc2309 support Liam Beguin
2023-08-24 18:00   ` Krzysztof Kozlowski
2023-08-24 19:32     ` Liam Beguin [this message]
2023-08-24 16:55 ` [PATCH 2/3] iio: adc: ltc2309: switch to new .probe() Liam Beguin
2023-08-24 18:01   ` Krzysztof Kozlowski
2023-08-24 19:38     ` Liam Beguin
2023-09-05 20:44   ` kernel test robot
2023-08-24 16:55 ` [PATCH 3/3] dt-bindings: iio: adc: add lltc,ltc2309 bindings Liam Beguin
2023-08-24 17:56   ` Krzysztof Kozlowski
2023-08-24 18:50     ` Liam Beguin
2023-08-25  6:15       ` Krzysztof Kozlowski
2023-08-25 15:53         ` Liam Beguin
2023-08-26  8:36           ` Krzysztof Kozlowski

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=20230824193254.GB3659959@shaak \
    --to=liambeguin@gmail.com \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=krzysztof.kozlowski@linaro.org \
    --cc=lars@metafoo.de \
    --cc=lgirdwood@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh+dt@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.