linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH 0/7] Devm helpers for regulator get and enable
@ 2022-08-10 11:21 Matti Vaittinen
  2022-08-10 11:32 ` [RFC PATCH 6/7] hwmon: lm90: simplify using devm_regulator_get_enable() Matti Vaittinen
  0 siblings, 1 reply; 3+ messages in thread
From: Matti Vaittinen @ 2022-08-10 11:21 UTC (permalink / raw)
  To: matti.vaittinen, mazziesaccount
  Cc: Jonathan Corbet, Michael Turquette, Stephen Boyd, Neil Armstrong,
	David Airlie, Daniel Vetter, Kevin Hilman, Jerome Brunet,
	Martin Blumenstingl, Jean Delvare, Guenter Roeck,
	Lars-Peter Clausen, Michael Hennerich, Alexandru Tachici,
	Jonathan Cameron, Liam Girdwood, Mark Brown, Matti Vaittinen,
	Greg Kroah-Hartman, Alexandru Ardelean, Peter Rosin,
	Aswath Govindraju, Johan Hovold, linux-doc, linux-kernel,
	linux-clk, dri-devel, linux-amlogic, linux-arm-kernel,
	linux-hwmon, linux-iio

[-- Attachment #1: Type: text/plain, Size: 4833 bytes --]

Devm helpers for regulator get and enable

First patch in the series is actually just a simple documentation fix
which could be taken in as it is now (even though the rest of the series
is a RFC).

A few* drivers seem to pattern demonstrated by pseudocode:

- devm_regulator_get()
- regulator_enable()
- devm_add_action_or_reset(regulator_disable())

(*) A rough idea what 'a few' means in this context can be get by issuing:
"git grep -In -A10 devm_regulator_get |grep -B5 -A5 add_action |less"
and then further checking some of the reported drivers. This is what I did
when I realized I needed to enable a regulator for accelerometer and
thought I'd go with devm-action...

Introducing devm helpers for this pattern would remove bunch of code from
drivers. Typically at least following:

- replace 3 calls (devm_regulator_get[_optional](), regulator_enable(),
  devm_add_action_or_reset()) with just one
  (devm_regulator_get_enable[_optional]()).
- drop disable callback.

I believe this simplifies things by removing some dublicated code.

The other RFC aspect besides the question if this actually is useful, is
whether the devm_regulator_get_enable[_optional]() should return a pointer
to the obtained regulator or not. This RFC version does not return the
pointer for user because any call to regulator_disable() may lead to
regulator enable count imbalance upon device detach. (Eg, if someone calls
regulator_disable() and the device is then detached before user has
re-enabled the regulator). Not returning the pointer to obtained regulator
to caller is a good hint that the enable/disable should not be manually
handled when this API is used.

OTOH, not returning the pointer reduces the use-cases by not allowing
the consumers to perform other regulator actions. For example request the
voltages. A few drivers which used the "get, enable,
devm_action_to_disable" did also query the voltages. The suggested form of
the API does not suit needs of such users. The new API in its current form
really allows to only cover the very dummy cases where regulator is only
enabled for a lifetime of the driver. I am unsure if this is really
beneficial (well, there seems to be bunch of drivers doing just this) - or
if we should go with a version returning the struct regulator *

Some drivers did also manually disable the regulator (even though they had
registered the devm-action for disable) for PM functionality. I am unsure
if such use for suspend is actually safe(?) I didn't check if we can
guarantee that the driver is not detached after the PM suspend has disabled
the regulator(?)

This RFC converts only few a drivers to demonstrate benefits. This makes it
easier to rework the series if people thinks returning the pointer to
struct regulator should be done. I can't promise I'll convert all drivers
so, there is still plenty of fish in the sea for people who like to improve
the code (or count the beans ;]).

Finally - most of the converted drivers have not been tested (other than
compile-tested) due to lack of HW. All reviews and testing is _highly_
appreciated (as always!). I have the driver changes in individual patches
to make reviewing easier. I will squash the driver changes into one patch /
subsystem when I'll drop the "RFC" from the series.

Patch 1:
	Fix docmentation (devres API list) for regulator APIs
Patch 2:
	The devm helpers.
Patch 3:
	Add new devm-helper APIs to docs.
Patches 4 ... 7:
	Example drivers.

---

Matti Vaittinen (7):
  docs: devres: regulator: Add missing devm_* functions to devres.rst
  regulator: Add devm helpers for get and enable
  docs: devres: regulator: Add new get_enable functions to devres.rst
  clk: cdce925: simplify using devm_regulator_get_enable()
  gpu: drm: meson: simplify using devm_regulator_get_enable_optional()
  hwmon: lm90: simplify using devm_regulator_get_enable()
  adc: ad7192: simplify using devm_regulator_get_enable()

 .../driver-api/driver-model/devres.rst        |  9 +++
 drivers/clk/clk-cdce925.c                     | 21 ++-----
 drivers/gpu/drm/meson/meson_dw_hdmi.c         | 23 +-------
 drivers/hwmon/lm90.c                          | 21 +------
 drivers/iio/adc/ad7192.c                      | 15 +----
 drivers/regulator/devres.c                    | 59 +++++++++++++++++++
 include/linux/regulator/consumer.h            | 13 ++++
 7 files changed, 92 insertions(+), 69 deletions(-)

-- 
2.37.1


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RFC PATCH 6/7] hwmon: lm90: simplify using devm_regulator_get_enable()
  2022-08-10 11:21 [RFC PATCH 0/7] Devm helpers for regulator get and enable Matti Vaittinen
@ 2022-08-10 11:32 ` Matti Vaittinen
  2022-08-10 12:52   ` Guenter Roeck
  0 siblings, 1 reply; 3+ messages in thread
From: Matti Vaittinen @ 2022-08-10 11:32 UTC (permalink / raw)
  To: matti.vaittinen, mazziesaccount
  Cc: Jean Delvare, Guenter Roeck, linux-hwmon, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1996 bytes --]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(),
add_action_or_reset(regulator_disable)' and use the
devm_regulator_get_enable().

Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
---
 drivers/hwmon/lm90.c | 21 ++-------------------
 1 file changed, 2 insertions(+), 19 deletions(-)

diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c
index 3820f0e61510..2ab561ec367c 100644
--- a/drivers/hwmon/lm90.c
+++ b/drivers/hwmon/lm90.c
@@ -1848,12 +1848,6 @@ static void lm90_remove_pec(void *dev)
 	device_remove_file(dev, &dev_attr_pec);
 }
 
-static void lm90_regulator_disable(void *regulator)
-{
-	regulator_disable(regulator);
-}
-
-
 static const struct hwmon_ops lm90_ops = {
 	.is_visible = lm90_is_visible,
 	.read = lm90_read,
@@ -1865,24 +1859,13 @@ static int lm90_probe(struct i2c_client *client)
 	struct device *dev = &client->dev;
 	struct i2c_adapter *adapter = client->adapter;
 	struct hwmon_channel_info *info;
-	struct regulator *regulator;
 	struct device *hwmon_dev;
 	struct lm90_data *data;
 	int err;
 
-	regulator = devm_regulator_get(dev, "vcc");
-	if (IS_ERR(regulator))
-		return PTR_ERR(regulator);
-
-	err = regulator_enable(regulator);
-	if (err < 0) {
-		dev_err(dev, "Failed to enable regulator: %d\n", err);
-		return err;
-	}
-
-	err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
+	err = devm_regulator_get_enable(dev, "vcc");
 	if (err)
-		return err;
+		return dev_err_probe(dev, err, "Failed to enable regulator\n");
 
 	data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
 	if (!data)
-- 
2.37.1


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [RFC PATCH 6/7] hwmon: lm90: simplify using devm_regulator_get_enable()
  2022-08-10 11:32 ` [RFC PATCH 6/7] hwmon: lm90: simplify using devm_regulator_get_enable() Matti Vaittinen
@ 2022-08-10 12:52   ` Guenter Roeck
  0 siblings, 0 replies; 3+ messages in thread
From: Guenter Roeck @ 2022-08-10 12:52 UTC (permalink / raw)
  To: Matti Vaittinen, matti.vaittinen; +Cc: Jean Delvare, linux-hwmon, linux-kernel

On 8/10/22 04:32, Matti Vaittinen wrote:
> Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(),
> add_action_or_reset(regulator_disable)' and use the
> devm_regulator_get_enable().
> 
> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>

Acked-by: Guenter Roeck <linux@roeck-us.net>

> ---
>   drivers/hwmon/lm90.c | 21 ++-------------------
>   1 file changed, 2 insertions(+), 19 deletions(-)
> 
> diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c
> index 3820f0e61510..2ab561ec367c 100644
> --- a/drivers/hwmon/lm90.c
> +++ b/drivers/hwmon/lm90.c
> @@ -1848,12 +1848,6 @@ static void lm90_remove_pec(void *dev)
>   	device_remove_file(dev, &dev_attr_pec);
>   }
>   
> -static void lm90_regulator_disable(void *regulator)
> -{
> -	regulator_disable(regulator);
> -}
> -
> -
>   static const struct hwmon_ops lm90_ops = {
>   	.is_visible = lm90_is_visible,
>   	.read = lm90_read,
> @@ -1865,24 +1859,13 @@ static int lm90_probe(struct i2c_client *client)
>   	struct device *dev = &client->dev;
>   	struct i2c_adapter *adapter = client->adapter;
>   	struct hwmon_channel_info *info;
> -	struct regulator *regulator;
>   	struct device *hwmon_dev;
>   	struct lm90_data *data;
>   	int err;
>   
> -	regulator = devm_regulator_get(dev, "vcc");
> -	if (IS_ERR(regulator))
> -		return PTR_ERR(regulator);
> -
> -	err = regulator_enable(regulator);
> -	if (err < 0) {
> -		dev_err(dev, "Failed to enable regulator: %d\n", err);
> -		return err;
> -	}
> -
> -	err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
> +	err = devm_regulator_get_enable(dev, "vcc");
>   	if (err)
> -		return err;
> +		return dev_err_probe(dev, err, "Failed to enable regulator\n");
>   
>   	data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
>   	if (!data)


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-10 12:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-10 11:21 [RFC PATCH 0/7] Devm helpers for regulator get and enable Matti Vaittinen
2022-08-10 11:32 ` [RFC PATCH 6/7] hwmon: lm90: simplify using devm_regulator_get_enable() Matti Vaittinen
2022-08-10 12:52   ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).