[PATCH 4/4] regulator: ltm8054: Support output current limit control

Romain Gantois posted 4 patches 2 weeks, 2 days ago
There is a newer version of this series
[PATCH 4/4] regulator: ltm8054: Support output current limit control
Posted by Romain Gantois 2 weeks, 2 days ago
The LTM8054 supports setting a fixed output current limit using a sense
resistor connected to a dedicated pin. This limit can then be lowered
dynamically by varying the voltage level of the CTL pin.

Support controlling the LTM8054's output current limit.

Signed-off-by: Romain Gantois <romain.gantois@bootlin.com>
---
 drivers/regulator/Kconfig             |   1 +
 drivers/regulator/ltm8054-regulator.c | 109 ++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)

diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
index 15fb71193b67d0b2daa631b69778dde9323aedd2..22cf0e980351f21e3ef5b6611a39cb48aeb503ea 100644
--- a/drivers/regulator/Kconfig
+++ b/drivers/regulator/Kconfig
@@ -579,6 +579,7 @@ config REGULATOR_LTC3676
 
 config REGULATOR_LTM8054
 	tristate "LTM8054 Buck-Boost voltage regulator"
+	depends on IIO
 	help
 	  This driver provides support for the Linear Technology LTM8054
 	  Buck-Boost micromodule regulator. The LTM8054 has an adjustable
diff --git a/drivers/regulator/ltm8054-regulator.c b/drivers/regulator/ltm8054-regulator.c
index e41bd95da55fb87912e2cdf70bae231133c25745..3b7b826e29cfb37415a7fb7cab678cc33494d184 100644
--- a/drivers/regulator/ltm8054-regulator.c
+++ b/drivers/regulator/ltm8054-regulator.c
@@ -11,12 +11,27 @@
 #include <linux/regulator/driver.h>
 #include <linux/regulator/of_regulator.h>
 #include <linux/platform_device.h>
+#include <linux/iio/consumer.h>
+#include <linux/iio/types.h>
 #include <linux/gpio/consumer.h>
 
+/* Threshold voltage between the Vout and Iout pins which triggers current
+ * limiting, in microvolts
+ */
+#define LTM8054_VOUT_IOUT_MAX 58000
+
+#define LTM8054_MAX_CTL_V 1200000
+#define LTM8054_MIN_CTL_V 50000
+
 /* The LTM8054 regulates its FB pin to 1.2V */
 #define LTM8054_FB_V 1200000
 
 struct ltm8054_priv {
+	struct iio_channel *ctl_dac;
+
+	int min_uA;
+	int max_uA;
+
 	struct regulator_desc rdesc;
 };
 
@@ -30,18 +45,105 @@ static int ltm8054_scale(unsigned int uV, u32 r1, u32 r2)
 	return uV + (unsigned int)tmp;
 }
 
+static int ltm8054_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA)
+{
+	struct ltm8054_priv *priv = rdev_get_drvdata(rdev);
+	u64 vdac_uV;
+
+	min_uA = clamp_t(int, min_uA, priv->min_uA, priv->max_uA);
+
+	/* adjusted current limit = Rsense current limit * CTL pin voltage / max CTL pin voltage */
+	vdac_uV = (u64)min_uA * LTM8054_MAX_CTL_V;
+	do_div(vdac_uV, priv->max_uA);
+
+	dev_dbg(&rdev->dev,
+		"Setting current limit to %duA, CTL pin to %duV\n", min_uA, (int)vdac_uV);
+
+	/* Standard IIO voltage unit is mV, scale accordingly. */
+	return iio_write_channel_processed_scale(priv->ctl_dac, vdac_uV, 1000);
+}
+
+static int ltm8054_get_current_limit(struct regulator_dev *rdev)
+{
+	struct ltm8054_priv *priv = rdev_get_drvdata(rdev);
+	int ret, vdac_uv;
+	u64 uA;
+
+	ret = iio_read_channel_processed_scale(priv->ctl_dac, &vdac_uv, 1000);
+	if (ret < 0) {
+		dev_err(&rdev->dev, "failed to read CTL DAC voltage, err %d\n", ret);
+		return ret;
+	}
+
+	uA = (u64)vdac_uv * priv->max_uA;
+	do_div(uA, LTM8054_MAX_CTL_V);
+
+	return uA;
+}
+
 static const struct regulator_ops ltm8054_regulator_ops = {
+	.set_current_limit = ltm8054_set_current_limit,
+	.get_current_limit = ltm8054_get_current_limit,
 };
 
+static int ltm8054_init_ctl_dac(struct platform_device *pdev, struct ltm8054_priv *priv)
+{
+	struct iio_channel *ctl_dac;
+	enum iio_chan_type type;
+	int ret;
+
+	ctl_dac = devm_iio_channel_get(&pdev->dev, "ctl");
+	if (IS_ERR(ctl_dac))
+		return PTR_ERR(ctl_dac);
+
+	ret = iio_get_channel_type(ctl_dac, &type);
+	if (ret < 0)
+		return ret;
+
+	if (type != IIO_VOLTAGE)
+		return -EINVAL;
+
+	priv->ctl_dac = ctl_dac;
+
+	return 0;
+}
+
 static int ltm8054_of_parse(struct device *dev, struct ltm8054_priv *priv,
 			    struct regulator_config *config)
 {
 	struct device_node *np = dev->of_node;
+	u32 rsense;
 	u32 r[2];
+	u64 tmp;
 	int ret;
 
 	config->of_node = np;
 
+	ret = of_property_read_u32(np, "lltc,iout-rsense-micro-ohms", &rsense);
+	if (ret < 0) {
+		dev_err(dev, "failed to get sense resistor value\n");
+		return ret;
+	}
+
+	if (rsense == 0) {
+		dev_err(dev, "invalid value zero for sense resistor\n");
+		return -EINVAL;
+	}
+
+	/* The maximum output current limit is the one set by the Rsense resistor */
+	tmp = 1000000 * (u64)LTM8054_VOUT_IOUT_MAX;
+	do_div(tmp, rsense);
+	priv->max_uA = tmp;
+
+	/* Applying a voltage below LTM8054_MAX_CTL_V on the CTL pin reduces
+	 * the output current limit. If this level drops below
+	 * LTM8054_MIN_CTL_V the regulator stops switching
+	 */
+
+	tmp = LTM8054_MIN_CTL_V * (u64)priv->max_uA;
+	do_div(tmp, (u32)LTM8054_MAX_CTL_V);
+	priv->min_uA = tmp;
+
 	ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider", r, 2);
 	if (ret) {
 		dev_err(dev, "Failed to parse voltage divider\n");
@@ -52,6 +154,9 @@ static int ltm8054_of_parse(struct device *dev, struct ltm8054_priv *priv,
 	priv->rdesc.min_uV = priv->rdesc.fixed_uV;
 	priv->rdesc.n_voltages = 1;
 
+	dev_dbg(dev, "max_uA: %d min_uA: %d fixed_uV: %d\n",
+		priv->max_uA, priv->min_uA, priv->rdesc.fixed_uV);
+
 	config->init_data = of_get_regulator_init_data(dev,
 						       np,
 						       &priv->rdesc);
@@ -92,6 +197,10 @@ static int ltm8054_probe(struct platform_device *pdev)
 	if (ret)
 		return dev_err_probe(&pdev->dev, ret, "failed to parse device tree\n");
 
+	ret = ltm8054_init_ctl_dac(pdev, priv);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "failed to initialize CTL DAC\n");
+
 	rdev = devm_regulator_register(&pdev->dev, &priv->rdesc, &config);
 	if (IS_ERR(rdev))
 		return dev_err_probe(&pdev->dev, PTR_ERR(rdev), "failed to register regulator\n");

-- 
2.51.0
Re: [PATCH 4/4] regulator: ltm8054: Support output current limit control
Posted by Andy Shevchenko 2 weeks, 1 day ago
On Tue, Sep 16, 2025 at 12:24:09PM +0200, Romain Gantois wrote:
> The LTM8054 supports setting a fixed output current limit using a sense
> resistor connected to a dedicated pin. This limit can then be lowered
> dynamically by varying the voltage level of the CTL pin.
> 
> Support controlling the LTM8054's output current limit.

...

> in microvolts

Yeah, using _mV postfix will make it visible that those are in micro-Volts.

...

> +static int ltm8054_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA)
> +{
> +	struct ltm8054_priv *priv = rdev_get_drvdata(rdev);
> +	u64 vdac_uV;
> +
> +	min_uA = clamp_t(int, min_uA, priv->min_uA, priv->max_uA);
> +
> +	/* adjusted current limit = Rsense current limit * CTL pin voltage / max CTL pin voltage */
> +	vdac_uV = (u64)min_uA * LTM8054_MAX_CTL_V;
> +	do_div(vdac_uV, priv->max_uA);
> +
> +	dev_dbg(&rdev->dev,
> +		"Setting current limit to %duA, CTL pin to %duV\n", min_uA, (int)vdac_uV);

Why casting?

> +	/* Standard IIO voltage unit is mV, scale accordingly. */
> +	return iio_write_channel_processed_scale(priv->ctl_dac, vdac_uV, 1000);
> +}

...

> +	ret = of_property_read_u32(np, "lltc,iout-rsense-micro-ohms", &rsense);

device_property_read_u32()

> +	if (ret < 0) {

Be consistent with a style, in the previous patch it was 'if (ret)'.

> +		dev_err(dev, "failed to get sense resistor value\n");
> +		return ret;
> +	}
> +
> +	if (rsense == 0) {
> +		dev_err(dev, "invalid value zero for sense resistor\n");
> +		return -EINVAL;
> +	}
> +
> +	/* The maximum output current limit is the one set by the Rsense resistor */
> +	tmp = 1000000 * (u64)LTM8054_VOUT_IOUT_MAX;

Yo may use MICRO and drop the casting.

> +	do_div(tmp, rsense);
> +	priv->max_uA = tmp;
> +
> +	/* Applying a voltage below LTM8054_MAX_CTL_V on the CTL pin reduces
> +	 * the output current limit. If this level drops below
> +	 * LTM8054_MIN_CTL_V the regulator stops switching
> +	 */

/*
 * Besides missing period at the end this is not correct multi-line style of
 * the comments. Use this example.
 */

> +	tmp = LTM8054_MIN_CTL_V * (u64)priv->max_uA;
> +	do_div(tmp, (u32)LTM8054_MAX_CTL_V);

Why casting?

> +	priv->min_uA = tmp;

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH 4/4] regulator: ltm8054: Support output current limit control
Posted by Romain Gantois 2 weeks, 1 day ago
On Tuesday, 16 September 2025 15:19:16 CEST Andy Shevchenko wrote:
> On Tue, Sep 16, 2025 at 12:24:09PM +0200, Romain Gantois wrote:
> > The LTM8054 supports setting a fixed output current limit using a sense
> > resistor connected to a dedicated pin. This limit can then be lowered
> > dynamically by varying the voltage level of the CTL pin.
> > 
> > Support controlling the LTM8054's output current limit.
> 
> ...
> 
> > in microvolts
> 
> Yeah, using _mV postfix will make it visible that those are in micro-Volts.
> 
> ...
> 
> > +static int ltm8054_set_current_limit(struct regulator_dev *rdev, int
> > min_uA, int max_uA) +{
> > +	struct ltm8054_priv *priv = rdev_get_drvdata(rdev);
> > +	u64 vdac_uV;
> > +
> > +	min_uA = clamp_t(int, min_uA, priv->min_uA, priv->max_uA);
> > +
> > +	/* adjusted current limit = Rsense current limit * CTL pin voltage / 
max
> > CTL pin voltage */ +	vdac_uV = (u64)min_uA * LTM8054_MAX_CTL_V;
> > +	do_div(vdac_uV, priv->max_uA);
> > +
> > +	dev_dbg(&rdev->dev,
> > +		"Setting current limit to %duA, CTL pin to %duV\n", min_uA,
> > (int)vdac_uV);
> Why casting?
> 

This one is indeed unnecessary.

> > +	/* Standard IIO voltage unit is mV, scale accordingly. */
> > +	return iio_write_channel_processed_scale(priv->ctl_dac, vdac_uV, 
1000);
> > +}
> 
> ...
> 
> > +	ret = of_property_read_u32(np, "lltc,iout-rsense-micro-ohms", 
&rsense);
> 
> device_property_read_u32()
> 
> > +	if (ret < 0) {
> 
> Be consistent with a style, in the previous patch it was 'if (ret)'.
> 
> > +		dev_err(dev, "failed to get sense resistor value\n");
> > +		return ret;
> > +	}
> > +
> > +	if (rsense == 0) {
> > +		dev_err(dev, "invalid value zero for sense resistor\n");
> > +		return -EINVAL;
> > +	}
> > +
> > +	/* The maximum output current limit is the one set by the Rsense
> > resistor */ +	tmp = 1000000 * (u64)LTM8054_VOUT_IOUT_MAX;
> 
> Yo may use MICRO and drop the casting.
> 
> > +	do_div(tmp, rsense);
> > +	priv->max_uA = tmp;
> > +
> > +	/* Applying a voltage below LTM8054_MAX_CTL_V on the CTL pin reduces
> > +	 * the output current limit. If this level drops below
> > +	 * LTM8054_MIN_CTL_V the regulator stops switching
> > +	 */
> 
> /*
>  * Besides missing period at the end this is not correct multi-line style of
> * the comments. Use this example.
>  */
> 
> > +	tmp = LTM8054_MIN_CTL_V * (u64)priv->max_uA;

This cast avoids an overflow of the multiplication, since the result may 
easily exceed 32 bytes in size.

> > +	do_div(tmp, (u32)LTM8054_MAX_CTL_V);
> 
> Why casting?

Since do_div() is a macro, I casted the second argument just to be safe, but 
it seems that do_div() already does this internally, so I'll just drop the 
cast.

Thanks,

-- 
Romain Gantois, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Re: [PATCH 4/4] regulator: ltm8054: Support output current limit control
Posted by Andy Shevchenko 2 weeks, 1 day ago
On Tue, Sep 16, 2025 at 04:27:25PM +0200, Romain Gantois wrote:
> On Tuesday, 16 September 2025 15:19:16 CEST Andy Shevchenko wrote:
> > On Tue, Sep 16, 2025 at 12:24:09PM +0200, Romain Gantois wrote:

First of all, please remove unneeded context (which is assumed you agree with)
in the replies. It makes reviewers' life much easier.

...

> > /*
> >  * Besides missing period at the end this is not correct multi-line style of
> > * the comments. Use this example.
> >  */
> > 
> > > +	tmp = LTM8054_MIN_CTL_V * (u64)priv->max_uA;
> 
> This cast avoids an overflow of the multiplication, since the result may
> easily exceed 32 bytes in size.

It's better to read in a way of

	tmp = (u64)priv->max_uA * LTM8054_MIN_CTL_mV;

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH 4/4] regulator: ltm8054: Support output current limit control
Posted by Andy Shevchenko 2 weeks, 1 day ago
On Wed, Sep 17, 2025 at 10:02:03AM +0300, Andy Shevchenko wrote:
> On Tue, Sep 16, 2025 at 04:27:25PM +0200, Romain Gantois wrote:
> > On Tuesday, 16 September 2025 15:19:16 CEST Andy Shevchenko wrote:
> > > On Tue, Sep 16, 2025 at 12:24:09PM +0200, Romain Gantois wrote:

...

> > > /*
> > >  * Besides missing period at the end this is not correct multi-line style of
> > > * the comments. Use this example.
> > >  */
> > > 
> > > > +	tmp = LTM8054_MIN_CTL_V * (u64)priv->max_uA;
> > 
> > This cast avoids an overflow of the multiplication, since the result may
> > easily exceed 32 bytes in size.
> 
> It's better to read in a way of
> 
> 	tmp = (u64)priv->max_uA * LTM8054_MIN_CTL_mV;

I just realised that in previous mails and here I meant _uV postfix for the
predefined voltage thresholds. Sorry for the confusion.

-- 
With Best Regards,
Andy Shevchenko