Skip to contentSkip to conversation

Use devm helpers for regulator get and enable

PATCH Applied Matti Vaittinen 10 participants 66 messages Aug 19, 2022 – Oct 17, 2022

Subsystems (17)

v3 of 4 Applied Latest is v4 →

lore
Share & tools
lore

Matti Vaittinen

author maintainer
#0 lore raw

v3 cover Use devm helpers for regulator get and enable

Use devm helpers for regulator get and enable

NOTE: The series depends on commit
ee94aff2628b ("Devm helpers for regulator get and enable") which currently sits in Mark's regulator/for-next

A few* drivers seem to pattern demonstrated by pseudocode:

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

devm helpers for this pattern were added to remove bunch of code from drivers. Typically 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.

This series reowrks a few drivers. 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!).

Revision history:

v3:
- Drop already applied helper patches
- Add a few more drivers

RFCv1 => v2:
- Add devm_regulator_bulk_get_enable() and devm_regulator_bulk_put()
- Convert a couple of drivers to use the new devm_regulator_bulk_get_enable().
- Squash all IIO patches into one.

Patch 1:
Add new devm-helper APIs to docs.
Patch 2:
simplified CLK driver(s)
Patch 3:
simplified GPU driver(s)
Patch 4 - 5:
simplified hwmon driver(s)
Patch 6 - 14:
simplified IIO driver(s)

---

Matti Vaittinen (14):
docs: devres: regulator: Add new get_enable functions to devres.rst
clk: cdce925: simplify using devm_regulator_get_enable()
gpu: drm: simplify drivers using devm_regulator_*get_enable*()
hwmon: lm90: simplify using devm_regulator_get_enable()
hwmon: adm1177: simplify using devm_regulator_get_enable()
iio: ad7192: Simplify using devm_regulator_get_enable()
iio: ltc2688: Simplify using devm_regulator_*get_enable()
iio: bmg160_core: Simplify using devm_regulator_*get_enable()
iio: st_lsm6dsx: Simplify using devm_regulator_*get_enable()
iio: ad7476: simplify using devm_regulator_get_enable()
iio: ad7606: simplify using devm_regulator_get_enable()
iio: max1241: simplify using devm_regulator_get_enable()
iio: max1363: simplify using devm_regulator_get_enable()
iio: hmc425a: simplify using devm_regulator_get_enable()

 .../driver-api/driver-model/devres.rst        |  4 +++
 drivers/clk/clk-cdce925.c                     | 21 +++----------
 drivers/gpu/drm/bridge/sii902x.c              | 22 ++------------
 drivers/gpu/drm/meson/meson_dw_hdmi.c         | 23 ++------------
 drivers/hwmon/adm1177.c                       | 27 ++---------------
 drivers/hwmon/lm90.c                          | 15 ++--------
 drivers/iio/adc/ad7192.c                      | 15 ++--------
 drivers/iio/adc/ad7476.c                      | 11 +------
 drivers/iio/adc/ad7606.c                      | 22 ++------------
 drivers/iio/adc/ad7606.h                      |  1 -
 drivers/iio/adc/max1241.c                     | 28 ++---------------
 drivers/iio/adc/max1363.c                     | 11 +------
 drivers/iio/amplifiers/hmc425a.c              | 17 +----------
 drivers/iio/dac/ltc2688.c                     | 23 ++------------
 drivers/iio/gyro/bmg160_core.c                | 24 ++-------------
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  2 --
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  | 30 ++++---------------
 17 files changed, 41 insertions(+), 255 deletions(-)
Signature · 14 lines
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 =] 
author maintainer
#1 lore raw

PATCH v3 1/14 docs: devres: regulator: Add new get_enable functions to devres.rst

Add the new devm_regulator_get_enable() and devm_regulator_get_enable_optional() to devres.rst

---
v2 => v3
No changes

RFCv1 => v2:
- Add devm_regulator_bulk_put() and devm_regulator_bulk_get_enable()

---
 Documentation/driver-api/driver-model/devres.rst | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/driver-api/driver-model/devres.rst +4 -0
index 882b14089454..f667b33bbda5 100644
@@ -407,7 +407,11 @@ PWM
 REGULATOR
   devm_regulator_bulk_register_supply_alias()
   devm_regulator_bulk_get()
+  devm_regulator_bulk_get_enable()
+  devm_regulator_bulk_put()
   devm_regulator_get()
+  devm_regulator_get_enable()
+  devm_regulator_get_enable_optional()
   devm_regulator_get_exclusive()
   devm_regulator_get_optional()
   devm_regulator_irq_helper()
Signature · 14 lines
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 =] 
author maintainer
#2 lore raw

PATCH v3 2/14 clk: cdce925: simplify using devm_regulator_get_enable()

In reply to Matti Vaittinen

Simplify the driver using devm_regulator_get_enable() instead of open-coding the devm_add_action_or_reset().

A (minor?) functional change is that we don't print an error in case of a deferred probe. Now we also print the error no matter which of the involved calls caused the failure.

---
RFCv1 => onwards:
- No changes

---
 drivers/clk/clk-cdce925.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/clk/clk-cdce925.c +4 -17
index ef9a2d44e40c..6350682f7e6d 100644
@@ -603,28 +603,15 @@ of_clk_cdce925_get(struct of_phandle_args *clkspec, void *_data)
 	return &data->clk[idx].hw;
 }
 
-static void cdce925_regulator_disable(void *regulator)
-{
-	regulator_disable(regulator);
-}
-
 static int cdce925_regulator_enable(struct device *dev, const char *name)
 {
-	struct regulator *regulator;
 	int err;
 
-	regulator = devm_regulator_get(dev, name);
-	if (IS_ERR(regulator))
-		return PTR_ERR(regulator);
-
-	err = regulator_enable(regulator);
-	if (err) {
-		dev_err(dev, "Failed to enable %s: %d\n", name, err);
-		return err;
-	}
+	err = devm_regulator_get_enable(dev, name);
+	if (err)
+		dev_err_probe(dev, err, "Failed to enable %s:\n", name);
 
-	return devm_add_action_or_reset(dev, cdce925_regulator_disable,
-					regulator);
+	return err;
 }
 
 /* The CDCE925 uses a funky way to read/write registers. Bulk mode is
Signature · 14 lines
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 =] 
maintainer Applied
#3 lore raw

Re: [PATCH v3 02/14] clk: cdce925: simplify using devm_regulator_get_enable()

Quoting Matti Vaittinen (2022-08-19 12:17:53)

Quoted 10 lines

Simplify the driver using devm_regulator_get_enable() instead of open-coding the devm_add_action_or_reset().

A (minor?) functional change is that we don't print an error in case of a deferred probe. Now we also print the error no matter which of the involved calls caused the failure.

---

Applied to clk-next

author maintainer
#4 lore raw

PATCH v3 3/14 gpu: drm: simplify drivers using devm_regulator_*get_enable*()

In reply to Matti Vaittinen

Simplify drivers using managed "regulator get and enable".

meson:
Use the devm_regulator_get_enable_optional(). Also drop the seemingly unused struct member 'hdmi_supply'.

sii902x:
Simplify using devm_regulator_bulk_get_enable()

---
v2 => v3:
No changes

RFCv1 => v2:
- Change also sii902x to use devm_regulator_bulk_get_enable()

Please note - this is only compile-tested due to the lack of HW. Careful review and testing is _highly_ appreciated.

---
 drivers/gpu/drm/bridge/sii902x.c      | 22 +++-------------------
 drivers/gpu/drm/meson/meson_dw_hdmi.c | 23 +++--------------------
 2 files changed, 6 insertions(+), 39 deletions(-)
drivers/gpu/drm/bridge/sii902x.c +3 -19
index 7ab38d734ad6..162f9c87eeb2 100644
@@ -171,7 +171,6 @@ struct sii902x {
 	struct drm_connector connector;
 	struct gpio_desc *reset_gpio;
 	struct i2c_mux_core *i2cmux;
-	struct regulator_bulk_data supplies[2];
 	bool sink_is_hdmi;
 	/*
 	 * Mutex protects audio and video functions from interfering
@@ -1072,6 +1071,7 @@ static int sii902x_probe(struct i2c_client *client,
 	struct device *dev = &client->dev;
 	struct device_node *endpoint;
 	struct sii902x *sii902x;
+	static const char * const supplies[] = {"iovcc", "cvcc12"};
 	int ret;
 
 	ret = i2c_check_functionality(client->adapter,
@@ -1122,27 +1122,13 @@ static int sii902x_probe(struct i2c_client *client,
 
 	mutex_init(&sii902x->mutex);
 
-	sii902x->supplies[0].supply = "iovcc";
-	sii902x->supplies[1].supply = "cvcc12";
-	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(sii902x->supplies),
-				      sii902x->supplies);
-	if (ret < 0)
-		return ret;
-
-	ret = regulator_bulk_enable(ARRAY_SIZE(sii902x->supplies),
-				    sii902x->supplies);
+	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(supplies), supplies);
 	if (ret < 0) {
 		dev_err_probe(dev, ret, "Failed to enable supplies");
 		return ret;
 	}
 
-	ret = sii902x_init(sii902x);
-	if (ret < 0) {
-		regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
-				       sii902x->supplies);
-	}
-
-	return ret;
+	return sii902x_init(sii902x);
 }
 
 static int sii902x_remove(struct i2c_client *client)
@@ -1152,8 +1138,6 @@ static int sii902x_remove(struct i2c_client *client)
 
 	i2c_mux_del_adapters(sii902x->i2cmux);
 	drm_bridge_remove(&sii902x->bridge);
-	regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
-			       sii902x->supplies);
 
 	return 0;
 }
drivers/gpu/drm/meson/meson_dw_hdmi.c +3 -20
index 5cd2b2ebbbd3..7642f740272b 100644
@@ -140,7 +140,6 @@ struct meson_dw_hdmi {
 	struct reset_control *hdmitx_apb;
 	struct reset_control *hdmitx_ctrl;
 	struct reset_control *hdmitx_phy;
-	struct regulator *hdmi_supply;
 	u32 irq_stat;
 	struct dw_hdmi *hdmi;
 	struct drm_bridge *bridge;
@@ -665,11 +664,6 @@ static void meson_dw_hdmi_init(struct meson_dw_hdmi *meson_dw_hdmi)
 
 }
 
-static void meson_disable_regulator(void *data)
-{
-	regulator_disable(data);
-}
-
 static void meson_disable_clk(void *data)
 {
 	clk_disable_unprepare(data);
@@ -723,20 +717,9 @@ static int meson_dw_hdmi_bind(struct device *dev, struct device *master,
 	meson_dw_hdmi->data = match;
 	dw_plat_data = &meson_dw_hdmi->dw_plat_data;
 
-	meson_dw_hdmi->hdmi_supply = devm_regulator_get_optional(dev, "hdmi");
-	if (IS_ERR(meson_dw_hdmi->hdmi_supply)) {
-		if (PTR_ERR(meson_dw_hdmi->hdmi_supply) == -EPROBE_DEFER)
-			return -EPROBE_DEFER;
-		meson_dw_hdmi->hdmi_supply = NULL;
-	} else {
-		ret = regulator_enable(meson_dw_hdmi->hdmi_supply);
-		if (ret)
-			return ret;
-		ret = devm_add_action_or_reset(dev, meson_disable_regulator,
-					       meson_dw_hdmi->hdmi_supply);
-		if (ret)
-			return ret;
-	}
+	ret = devm_regulator_get_enable_optional(dev, "hdmi");
+	if (ret != -ENODEV)
+		return ret;
 
 	meson_dw_hdmi->hdmitx_apb = devm_reset_control_get_exclusive(dev,
 						"hdmitx_apb");
Signature · 14 lines
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 =] 
Reviewed-by on v3 3/14
#5 lore raw

Re: [PATCH v3 03/14] gpu: drm: simplify drivers using devm_regulator_*get_enable*()

Hi Matti,

Quoted 77 lines · Matti Vaittinen

Simplify drivers using managed "regulator get and enable".

meson:
Use the devm_regulator_get_enable_optional(). Also drop the seemingly unused struct member 'hdmi_supply'.

sii902x:
Simplify using devm_regulator_bulk_get_enable()

---
v2 => v3:
No changes

RFCv1 => v2:
- Change also sii902x to use devm_regulator_bulk_get_enable()

Please note - this is only compile-tested due to the lack of HW. Careful review and testing is _highly_ appreciated.

---
 drivers/gpu/drm/bridge/sii902x.c      | 22 +++-------------------
 drivers/gpu/drm/meson/meson_dw_hdmi.c | 23 +++--------------------
 2 files changed, 6 insertions(+), 39 deletions(-)
drivers/gpu/drm/bridge/sii902x.c +3 -17
index 7ab38d734ad6..162f9c87eeb2 100644
@@ -171,7 +171,6 @@ struct sii902x {
        struct drm_connector connector;
        struct gpio_desc *reset_gpio;
        struct i2c_mux_core *i2cmux;
-       struct regulator_bulk_data supplies[2];
        bool sink_is_hdmi;
        /*
         * Mutex protects audio and video functions from interfering
@@ -1072,6 +1071,7 @@ static int sii902x_probe(struct i2c_client *client,
        struct device *dev = &client->dev;
        struct device_node *endpoint;
        struct sii902x *sii902x;
+       static const char * const supplies[] = {"iovcc", "cvcc12"};
        int ret;
 
        ret = i2c_check_functionality(client->adapter,
@@ -1122,27 +1122,13 @@ static int sii902x_probe(struct i2c_client *client,
 
        mutex_init(&sii902x->mutex);
 
-       sii902x->supplies[0].supply = "iovcc";
-       sii902x->supplies[1].supply = "cvcc12";
-       ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(sii902x->supplies),
-                                     sii902x->supplies);
-       if (ret < 0)
-               return ret;
-
-       ret = regulator_bulk_enable(ARRAY_SIZE(sii902x->supplies),
-                                   sii902x->supplies);
+       ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(supplies), supplies);
        if (ret < 0) {
                dev_err_probe(dev, ret, "Failed to enable supplies");
                return ret;
        }
 
-       ret = sii902x_init(sii902x);
-       if (ret < 0) {
-               regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
-                                      sii902x->supplies);
-       }
-
-       return ret;
+       return sii902x_init(sii902x);
 }
 
 static int sii902x_remove(struct i2c_client *client)

drivers/gpu/drm/bridge/sii902x.c static int sii902x_remove(struct i2c_client *client) · line 1143

Quoted 8 lines
drivers/gpu/drm/bridge/sii902x.c +0 -2
@@ -1152,8 +1138,6 @@ static int sii902x_remove(struct i2c_client *client)
 
        i2c_mux_del_adapters(sii902x->i2cmux);
        drm_bridge_remove(&sii902x->bridge);
-       regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
-                              sii902x->supplies);
 
        return 0;
 }

Ideally this patch would be split into two parts here, due to maintainership boundaries.

For the sii902x part, please add my r-b.

Quoted 50 lines
drivers/gpu/drm/meson/meson_dw_hdmi.c +3 -21
index 5cd2b2ebbbd3..7642f740272b 100644
@@ -140,7 +140,6 @@ struct meson_dw_hdmi {
        struct reset_control *hdmitx_apb;
        struct reset_control *hdmitx_ctrl;
        struct reset_control *hdmitx_phy;
-       struct regulator *hdmi_supply;
        u32 irq_stat;
        struct dw_hdmi *hdmi;
        struct drm_bridge *bridge;
@@ -665,11 +664,6 @@ static void meson_dw_hdmi_init(struct meson_dw_hdmi *meson_dw_hdmi)
 
 }
 
-static void meson_disable_regulator(void *data)
-{
-       regulator_disable(data);
-}
-
 static void meson_disable_clk(void *data)
 {
        clk_disable_unprepare(data);
@@ -723,20 +717,9 @@ static int meson_dw_hdmi_bind(struct device *dev, struct device *master,
        meson_dw_hdmi->data = match;
        dw_plat_data = &meson_dw_hdmi->dw_plat_data;
 
-       meson_dw_hdmi->hdmi_supply = devm_regulator_get_optional(dev, "hdmi");
-       if (IS_ERR(meson_dw_hdmi->hdmi_supply)) {
-               if (PTR_ERR(meson_dw_hdmi->hdmi_supply) == -EPROBE_DEFER)
-                       return -EPROBE_DEFER;
-               meson_dw_hdmi->hdmi_supply = NULL;
-       } else {
-               ret = regulator_enable(meson_dw_hdmi->hdmi_supply);
-               if (ret)
-                       return ret;
-               ret = devm_add_action_or_reset(dev, meson_disable_regulator,
-                                              meson_dw_hdmi->hdmi_supply);
-               if (ret)
-                       return ret;
-       }
+       ret = devm_regulator_get_enable_optional(dev, "hdmi");
+       if (ret != -ENODEV)
+               return ret;
 
        meson_dw_hdmi->hdmitx_apb = devm_reset_control_get_exclusive(dev,
                                                "hdmitx_apb");
--

2.37.1

author maintainer
#6 lore raw

Re: [PATCH v3 03/14] gpu: drm: simplify drivers using devm_regulator_*get_enable*()

On 8/29/22 17:25, Robert Foss wrote:

Thanks for the review Robert.

Quoted 86 lines

Hi Matti,

Quoted 83 lines · Matti Vaittinen

Simplify drivers using managed "regulator get and enable".

meson:
Use the devm_regulator_get_enable_optional(). Also drop the seemingly unused struct member 'hdmi_supply'.

sii902x:
Simplify using devm_regulator_bulk_get_enable()

---
v2 => v3:
No changes

RFCv1 => v2:
- Change also sii902x to use devm_regulator_bulk_get_enable()

Please note - this is only compile-tested due to the lack of HW. Careful review and testing is _highly_ appreciated.

---
  drivers/gpu/drm/bridge/sii902x.c      | 22 +++-------------------
  drivers/gpu/drm/meson/meson_dw_hdmi.c | 23 +++--------------------
  2 files changed, 6 insertions(+), 39 deletions(-)
drivers/gpu/drm/bridge/sii902x.c +3 -19
index 7ab38d734ad6..162f9c87eeb2 100644
@@ -171,7 +171,6 @@ struct sii902x {
         struct drm_connector connector;
         struct gpio_desc *reset_gpio;
         struct i2c_mux_core *i2cmux;
-       struct regulator_bulk_data supplies[2];
         bool sink_is_hdmi;
         /*
          * Mutex protects audio and video functions from interfering
@@ -1072,6 +1071,7 @@ static int sii902x_probe(struct i2c_client *client,
         struct device *dev = &client->dev;
         struct device_node *endpoint;
         struct sii902x *sii902x;
+       static const char * const supplies[] = {"iovcc", "cvcc12"};
         int ret;
 
         ret = i2c_check_functionality(client->adapter,
@@ -1122,27 +1122,13 @@ static int sii902x_probe(struct i2c_client *client,
 
         mutex_init(&sii902x->mutex);
 
-       sii902x->supplies[0].supply = "iovcc";
-       sii902x->supplies[1].supply = "cvcc12";
-       ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(sii902x->supplies),
-                                     sii902x->supplies);
-       if (ret < 0)
-               return ret;
-
-       ret = regulator_bulk_enable(ARRAY_SIZE(sii902x->supplies),
-                                   sii902x->supplies);
+       ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(supplies), supplies);
         if (ret < 0) {
                 dev_err_probe(dev, ret, "Failed to enable supplies");
                 return ret;
         }
 
-       ret = sii902x_init(sii902x);
-       if (ret < 0) {
-               regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
-                                      sii902x->supplies);
-       }
-
-       return ret;
+       return sii902x_init(sii902x);
  }
 
  static int sii902x_remove(struct i2c_client *client)
@@ -1152,8 +1138,6 @@ static int sii902x_remove(struct i2c_client *client)
 
         i2c_mux_del_adapters(sii902x->i2cmux);
         drm_bridge_remove(&sii902x->bridge);
-       regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
-                              sii902x->supplies);
 
Quoted 5 lines

drivers/gpu/drm/bridge/sii902x.c static int sii902x_remove(struct i2c_client *client) · line 1143

Quoted 2 lines
drivers/gpu/drm/bridge/sii902x.c +0 -0
         return 0;
  }

Ideally this patch would be split into two parts here, due to maintainership boundaries.

Ok. I will in any case respin this series when the dependency patches from Mark's tree have been merged to the -rc1. I can split this to patch/driver if it is preferred. I just though I'll decrease amount of mails by squashing these almost trivial changes.

Quoted 4 lines

For the sii902x part, please add my r-b.

Thanks.

Best Regards
-- Matti

Signature · 8 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~

Discuss - Estimate - Plan - Report and finally accomplish this:
void do_work(int time) __attribute__ ((const));
author maintainer
#7 lore raw

PATCH v3 4/14 hwmon: lm90: simplify using devm_regulator_get_enable()

In reply to Matti Vaittinen

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

---
RFCv1 => onwards:
- No changes

---
 drivers/hwmon/lm90.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/hwmon/lm90.c +2 -13
index 221de01a327a..6356d92547e0 100644
@@ -2749,24 +2749,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)
Signature · 14 lines
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 =] 
author maintainer
#8 lore raw

PATCH v3 5/14 hwmon: adm1177: simplify using devm_regulator_get_enable()

In reply to Matti Vaittinen

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

---
v2 => v3:
New patch

---
 drivers/hwmon/adm1177.c | 27 +++------------------------
 1 file changed, 3 insertions(+), 24 deletions(-)
drivers/hwmon/adm1177.c +3 -24
index 0c5dbc5e33b4..be17a26a84f1 100644
@@ -26,14 +26,12 @@
 /**
  * struct adm1177_state - driver instance specific data
  * @client:		pointer to i2c client
- * @reg:		regulator info for the power supply of the device
  * @r_sense_uohm:	current sense resistor value
  * @alert_threshold_ua:	current limit for shutdown
  * @vrange_high:	internal voltage divider
  */
 struct adm1177_state {
 	struct i2c_client	*client;
-	struct regulator	*reg;
 	u32			r_sense_uohm;
 	u32			alert_threshold_ua;
 	bool			vrange_high;
@@ -189,13 +187,6 @@ static const struct hwmon_chip_info adm1177_chip_info = {
 	.info = adm1177_info,
 };
 
-static void adm1177_remove(void *data)
-{
-	struct adm1177_state *st = data;
-
-	regulator_disable(st->reg);
-}
-
 static int adm1177_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -210,21 +201,9 @@ static int adm1177_probe(struct i2c_client *client)
 
 	st->client = client;
 
-	st->reg = devm_regulator_get_optional(&client->dev, "vref");
-	if (IS_ERR(st->reg)) {
-		if (PTR_ERR(st->reg) == -EPROBE_DEFER)
-			return -EPROBE_DEFER;
-
-		st->reg = NULL;
-	} else {
-		ret = regulator_enable(st->reg);
-		if (ret)
-			return ret;
-		ret = devm_add_action_or_reset(&client->dev, adm1177_remove,
-					       st);
-		if (ret)
-			return ret;
-	}
+	ret = devm_regulator_get_enable_optional(&client->dev, "vref");
+	if (ret == -EPROBE_DEFER)
+		return -EPROBE_DEFER;
 
 	if (device_property_read_u32(dev, "shunt-resistor-micro-ohms",
 				     &st->r_sense_uohm))
Signature · 14 lines
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 =] 
maintainer Acked-by on v3 5/14
#9 lore raw

Re: [PATCH v3 05/14] hwmon: adm1177: simplify using devm_regulator_get_enable()

Quoted 7 lines · Matti Vaittinen

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 81 lines

---
v2 => v3:
New patch

---
 drivers/hwmon/adm1177.c | 27 +++------------------------
 1 file changed, 3 insertions(+), 24 deletions(-)
drivers/hwmon/adm1177.c +3 -24
index 0c5dbc5e33b4..be17a26a84f1 100644
@@ -26,14 +26,12 @@
 /**
  * struct adm1177_state - driver instance specific data
  * @client:		pointer to i2c client
- * @reg:		regulator info for the power supply of the device
  * @r_sense_uohm:	current sense resistor value
  * @alert_threshold_ua:	current limit for shutdown
  * @vrange_high:	internal voltage divider
  */
 struct adm1177_state {
 	struct i2c_client	*client;
-	struct regulator	*reg;
 	u32			r_sense_uohm;
 	u32			alert_threshold_ua;
 	bool			vrange_high;
@@ -189,13 +187,6 @@ static const struct hwmon_chip_info adm1177_chip_info = {
 	.info = adm1177_info,
 };
 
-static void adm1177_remove(void *data)
-{
-	struct adm1177_state *st = data;
-
-	regulator_disable(st->reg);
-}
-
 static int adm1177_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -210,21 +201,9 @@ static int adm1177_probe(struct i2c_client *client)
 
 	st->client = client;
 
-	st->reg = devm_regulator_get_optional(&client->dev, "vref");
-	if (IS_ERR(st->reg)) {
-		if (PTR_ERR(st->reg) == -EPROBE_DEFER)
-			return -EPROBE_DEFER;
-
-		st->reg = NULL;
-	} else {
-		ret = regulator_enable(st->reg);
-		if (ret)
-			return ret;
-		ret = devm_add_action_or_reset(&client->dev, adm1177_remove,
-					       st);
-		if (ret)
-			return ret;
-	}
+	ret = devm_regulator_get_enable_optional(&client->dev, "vref");
+	if (ret == -EPROBE_DEFER)
+		return -EPROBE_DEFER;
 
 	if (device_property_read_u32(dev, "shunt-resistor-micro-ohms",
 				     &st->r_sense_uohm))
Signature · 14 lines
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 =] 
author maintainer
#10 lore raw

PATCH v3 6/14 iio: ad7192: Simplify using devm_regulator_get_enable()

In reply to Matti Vaittinen

Use devm_regulator_get_enable() instead of open coded get, enable, add-action-to-disable-at-detach - pattern. Also drop the seemingly unused struct member 'dvdd'.

---
v2 => v3
Split to own patch.

---
 drivers/iio/adc/ad7192.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/iio/adc/ad7192.c +2 -13
index d71977be7d22..8a52c0dec3f9 100644
@@ -177,7 +177,6 @@ struct ad7192_chip_info {
 struct ad7192_state {
 	const struct ad7192_chip_info	*chip_info;
 	struct regulator		*avdd;
-	struct regulator		*dvdd;
 	struct clk			*mclk;
 	u16				int_vref_mv;
 	u32				fclk;
@@ -1015,19 +1014,9 @@ static int ad7192_probe(struct spi_device *spi)
 	if (ret)
 		return ret;
 
-	st->dvdd = devm_regulator_get(&spi->dev, "dvdd");
-	if (IS_ERR(st->dvdd))
-		return PTR_ERR(st->dvdd);
-
-	ret = regulator_enable(st->dvdd);
-	if (ret) {
-		dev_err(&spi->dev, "Failed to enable specified DVdd supply\n");
-		return ret;
-	}
-
-	ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, st->dvdd);
+	ret = devm_regulator_get_enable(&spi->dev, "dvdd");
 	if (ret)
-		return ret;
+		return dev_err_probe(&spi->dev, ret, "Failed to enable specified DVdd supply\n");
 
 	ret = regulator_get_voltage(st->avdd);
 	if (ret < 0) {
Signature · 14 lines
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 =] 
maintainer Applied
#11 lore raw

Re: [PATCH v3 06/14] iio: ad7192: Simplify using devm_regulator_get_enable()

Quoted 5 lines · On Fri, 19 Aug 2022 22:19:01 +0300 Matti Vaittinen <[email protected]>…

Use devm_regulator_get_enable() instead of open coded get, enable, add-action-to-disable-at-detach - pattern. Also drop the seemingly unused struct member 'dvdd'.

Now the required dependencies are upstream...

Applied to the togreg branch of iio.git.

Thanks,

Jonathan

Quoted 42 lines

---
v2 => v3
Split to own patch.

---
 drivers/iio/adc/ad7192.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/iio/adc/ad7192.c +2 -13
index d71977be7d22..8a52c0dec3f9 100644
@@ -177,7 +177,6 @@ struct ad7192_chip_info {
 struct ad7192_state {
 	const struct ad7192_chip_info	*chip_info;
 	struct regulator		*avdd;
-	struct regulator		*dvdd;
 	struct clk			*mclk;
 	u16				int_vref_mv;
 	u32				fclk;
@@ -1015,19 +1014,9 @@ static int ad7192_probe(struct spi_device *spi)
 	if (ret)
 		return ret;
 
-	st->dvdd = devm_regulator_get(&spi->dev, "dvdd");
-	if (IS_ERR(st->dvdd))
-		return PTR_ERR(st->dvdd);
-
-	ret = regulator_enable(st->dvdd);
-	if (ret) {
-		dev_err(&spi->dev, "Failed to enable specified DVdd supply\n");
-		return ret;
-	}
-
-	ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, st->dvdd);
+	ret = devm_regulator_get_enable(&spi->dev, "dvdd");
 	if (ret)
-		return ret;
+		return dev_err_probe(&spi->dev, ret, "Failed to enable specified DVdd supply\n");
 
 	ret = regulator_get_voltage(st->avdd);
 	if (ret < 0) {
author maintainer
#12 lore raw

PATCH v3 7/14 iio: ltc2688: Simplify using devm_regulator_*get_enable()

In reply to Matti Vaittinen

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

---
v2 => v3
Split to own patch.

---
 drivers/iio/dac/ltc2688.c | 23 +++--------------------
 1 file changed, 3 insertions(+), 20 deletions(-)
drivers/iio/dac/ltc2688.c +3 -20
index 28bdde2d3088..fcad3efe62ea 100644
@@ -84,7 +84,6 @@ struct ltc2688_chan {
 struct ltc2688_state {
 	struct spi_device *spi;
 	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
 	struct ltc2688_chan channels[LTC2688_DAC_CHANNELS];
 	struct iio_chan_spec *iio_chan;
 	/* lock to protect against multiple access to the device and shared data */
@@ -902,13 +901,6 @@ static int ltc2688_setup(struct ltc2688_state *st, struct regulator *vref)
 			       LTC2688_CONFIG_EXT_REF);
 }
 
-static void ltc2688_disable_regulators(void *data)
-{
-	struct ltc2688_state *st = data;
-
-	regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators);
-}
-
 static void ltc2688_disable_regulator(void *regulator)
 {
 	regulator_disable(regulator);
@@ -970,6 +962,7 @@ static int ltc2688_probe(struct spi_device *spi)
 	struct regulator *vref_reg;
 	struct device *dev = &spi->dev;
 	int ret;
+	static const char * const regulators[] = {"vcc", "iovcc"};
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
 	if (!indio_dev)
@@ -988,21 +981,11 @@ static int ltc2688_probe(struct spi_device *spi)
 		return dev_err_probe(dev, PTR_ERR(st->regmap),
 				     "Failed to init regmap");
 
-	st->regulators[0].supply = "vcc";
-	st->regulators[1].supply = "iovcc";
-	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st->regulators),
-				      st->regulators);
-	if (ret)
-		return dev_err_probe(dev, ret, "Failed to get regulators\n");
-
-	ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators);
+	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators),
+					     regulators);
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to enable regulators\n");
 
-	ret = devm_add_action_or_reset(dev, ltc2688_disable_regulators, st);
-	if (ret)
-		return ret;
-
 	vref_reg = devm_regulator_get_optional(dev, "vref");
 	if (IS_ERR(vref_reg)) {
 		if (PTR_ERR(vref_reg) != -ENODEV)
Signature · 14 lines
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 =] 
maintainer
#13 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Quoted 39 lines · On Fri, 19 Aug 2022 22:19:17 +0300 Matti Vaittinen <[email protected]>…

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

---
v2 => v3
Split to own patch.

---
 drivers/iio/dac/ltc2688.c | 23 +++--------------------
 1 file changed, 3 insertions(+), 20 deletions(-)
drivers/iio/dac/ltc2688.c +0 -8
index 28bdde2d3088..fcad3efe62ea 100644
@@ -84,7 +84,6 @@ struct ltc2688_chan {
 struct ltc2688_state {
 	struct spi_device *spi;
 	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
 	struct ltc2688_chan channels[LTC2688_DAC_CHANNELS];
 	struct iio_chan_spec *iio_chan;
 	/* lock to protect against multiple access to the device and shared data */
@@ -902,13 +901,6 @@ static int ltc2688_setup(struct ltc2688_state *st, struct regulator *vref)
 			       LTC2688_CONFIG_EXT_REF);
 }
 
-static void ltc2688_disable_regulators(void *data)
-{
-	struct ltc2688_state *st = data;
-
-	regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators);
-}
-
 static void ltc2688_disable_regulator(void *regulator)
 {
 	regulator_disable(regulator);

drivers/iio/dac/ltc2688.c static int ltc2688_probe(struct spi_device *spi) · line 965

Quoted 4 lines
drivers/iio/dac/ltc2688.c +1 -0
@@ -970,6 +962,7 @@ static int ltc2688_probe(struct spi_device *spi)
 	struct regulator *vref_reg;
 	struct device *dev = &spi->dev;
 	int ret;
+	static const char * const regulators[] = {"vcc", "iovcc"};

trivial - slight preference for
{ "vcc", "iovcc" };

This isn't as important as for numeric values as we get some readability from the quotes but still nice to have.

For the whole static / vs non static. My personal preference is not to have the static marking but I don't care that much.

Quoted 27 lines
drivers/iio/dac/ltc2688.c +2 -12
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
 	if (!indio_dev)
@@ -988,21 +981,11 @@ static int ltc2688_probe(struct spi_device *spi)
 		return dev_err_probe(dev, PTR_ERR(st->regmap),
 				     "Failed to init regmap");
 
-	st->regulators[0].supply = "vcc";
-	st->regulators[1].supply = "iovcc";
-	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st->regulators),
-				      st->regulators);
-	if (ret)
-		return dev_err_probe(dev, ret, "Failed to get regulators\n");
-
-	ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators);
+	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators),
+					     regulators);
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to enable regulators\n");
 
-	ret = devm_add_action_or_reset(dev, ltc2688_disable_regulators, st);
-	if (ret)
-		return ret;
-
 	vref_reg = devm_regulator_get_optional(dev, "vref");
 	if (IS_ERR(vref_reg)) {
 		if (PTR_ERR(vref_reg) != -ENODEV)
author maintainer
#14 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Quoted 49 lines · Jonathan Cameron
Quoted 39 lines · On Fri, 19 Aug 2022 22:19:17 +0300 Matti Vaittinen <[email protected]>…

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

---
v2 => v3
Split to own patch.

---
  drivers/iio/dac/ltc2688.c | 23 +++--------------------
  1 file changed, 3 insertions(+), 20 deletions(-)
drivers/iio/dac/ltc2688.c +0 -8
index 28bdde2d3088..fcad3efe62ea 100644
@@ -84,7 +84,6 @@ struct ltc2688_chan {
  struct ltc2688_state {
  	struct spi_device *spi;
  	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
  	struct ltc2688_chan channels[LTC2688_DAC_CHANNELS];
  	struct iio_chan_spec *iio_chan;
  	/* lock to protect against multiple access to the device and shared data */
@@ -902,13 +901,6 @@ static int ltc2688_setup(struct ltc2688_state *st, struct regulator *vref)
  			       LTC2688_CONFIG_EXT_REF);
  }
 
-static void ltc2688_disable_regulators(void *data)
-{
-	struct ltc2688_state *st = data;
-
-	regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators);
-}
-
  static void ltc2688_disable_regulator(void *regulator)
  {
  	regulator_disable(regulator);

drivers/iio/dac/ltc2688.c static int ltc2688_probe(struct spi_device *spi) · line 965

Quoted 4 lines
drivers/iio/dac/ltc2688.c +1 -0
@@ -970,6 +962,7 @@ static int ltc2688_probe(struct spi_device *spi)
  	struct regulator *vref_reg;
  	struct device *dev = &spi->dev;
  	int ret;
+	static const char * const regulators[] = {"vcc", "iovcc"};

trivial - slight preference for
{ "vcc", "iovcc" };

Quoted 2 lines

This isn't as important as for numeric values as we get some readability from the quotes but still nice to have.

Right. I'll fix it.

Quoted 3 lines

For the whole static / vs non static. My personal preference is not to have the static marking but I don't care that much.

I'd like to stick with the static here. I know this one particular array does not have much of a footprint - but I'd like to encourage the habit of considering the memory usage. This discussion serves as an example of how unknown the impact of making const data static is. I didn't know this myself until Sebastian educated me :) Hence my strong preference on keeping this 'static' as an example for others who are as ignorant as I were ;) After all, having const data arrays static is quite an easy way of improving things - and it really does matter when there is many of arrays - or when they contain large data.

Yours
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#15 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Quoted 3 lines · Matti Vaittinen
Quoted 2 lines · Jonathan Cameron

On Fri, 19 Aug 2022 22:19:17 +0300
Matti Vaittinen <[email protected]> wrote:

...

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

+ static const char * const regulators[] = {"vcc", "iovcc"};

trivial - slight preference for
{ "vcc", "iovcc" };

This isn't as important as for numeric values as we get some readability from the quotes but still nice to have.

Right. I'll fix it.

And also make it a reversed xmas tree order.

Quoted 12 lines
Quoted 2 lines

For the whole static / vs non static. My personal preference is not to have the static marking but I don't care that much.

I'd like to stick with the static here. I know this one particular array does not have much of a footprint - but I'd like to encourage the habit of considering the memory usage. This discussion serves as an example of how unknown the impact of making const data static is. I didn't know this myself until Sebastian educated me :) Hence my strong preference on keeping this 'static' as an example for others who are as ignorant as I were ;) After all, having const data arrays static is quite an easy way of improving things - and it really does matter when there is many of arrays - or when they contain large data.

But still the same comment about global scope of the variable is applied.

As I explained before, hiding global variables inside a function is a bad code practice.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#16 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Quoted 12 lines · Andy Shevchenko
Quoted 3 lines · Matti Vaittinen
Quoted 2 lines · Jonathan Cameron

On Fri, 19 Aug 2022 22:19:17 +0300
Matti Vaittinen <[email protected]> wrote:

...

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

+ static const char * const regulators[] = {"vcc", "iovcc"};

trivial - slight preference for
{ "vcc", "iovcc" };

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

This isn't as important as for numeric values as we get some readability from the quotes but still nice to have.

Right. I'll fix it.

And also make it a reversed xmas tree order.

can do.

Quoted 14 lines
Quoted 4 lines
Quoted 3 lines

For the whole static / vs non static. My personal preference is not to have the static marking but I don't care that much.

Quoted 9 lines

I'd like to stick with the static here. I know this one particular array does not have much of a footprint - but I'd like to encourage the habit of considering the memory usage. This discussion serves as an example of how unknown the impact of making const data static is. I didn't know this myself until Sebastian educated me :) Hence my strong preference on keeping this 'static' as an example for others who are as ignorant as I were ;) After all, having const data arrays static is quite an easy way of improving things - and it really does matter when there is many of arrays - or when they contain large data.

Quoted 1 line

But still the same comment about global scope of the variable is applied.

I don't understand why you keep claiming the variable is global when it is not?

Quoted 2 lines

As I explained before, hiding global variables inside a function is a bad code practice.

I don't really get what you mean here. And I definitely don't see any improvement if we would really use a global variable instead of a local one.

--Matti

Signature · 8 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~

Discuss - Estimate - Plan - Report and finally accomplish this:
void do_work(int time) __attribute__ ((const));
#17 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Quoted 7 lines · Matti Vaittinen
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Matti Vaittinen
Quoted 2 lines · Jonathan Cameron

On Fri, 19 Aug 2022 22:19:17 +0300
Matti Vaittinen <[email protected]> wrote:

...

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 2 lines

For the whole static / vs non static. My personal preference is not to have the static marking but I don't care that much.

I'd like to stick with the static here. I know this one particular array does not have much of a footprint - but I'd like to encourage the habit of considering the memory usage. This discussion serves as an example of how unknown the impact of making const data static is. I didn't know this myself until Sebastian educated me :) Hence my strong preference on keeping this 'static' as an example for others who are as ignorant as I were ;) After all, having const data arrays static is quite an easy way of improving things - and it really does matter when there is many of arrays - or when they contain large data.

Quoted 4 lines
Quoted 1 line

But still the same comment about global scope of the variable is applied.

I don't understand why you keep claiming the variable is global when it is not?

It is. The static keyword makes it global, but putting the entire definition into the function is asking for troubles.

I guess some C standard chapter describes that in non-understandable language.

Quoted 5 lines
Quoted 2 lines

As I explained before, hiding global variables inside a function is a bad code practice.

I don't really get what you mean here. And I definitely don't see any improvement if we would really use a global variable instead of a local one.

The improvement is avoid hiding the global variable to the local namespace.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#18 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Quoted 25 lines · Andy Shevchenko
Quoted 7 lines · Matti Vaittinen
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Matti Vaittinen
Quoted 2 lines · Jonathan Cameron

On Fri, 19 Aug 2022 22:19:17 +0300
Matti Vaittinen <[email protected]> wrote:

...

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 2 lines

For the whole static / vs non static. My personal preference is not to have the static marking but I don't care that much.

I'd like to stick with the static here. I know this one particular array does not have much of a footprint - but I'd like to encourage the habit of considering the memory usage. This discussion serves as an example of how unknown the impact of making const data static is. I didn't know this myself until Sebastian educated me :) Hence my strong preference on keeping this 'static' as an example for others who are as ignorant as I were ;) After all, having const data arrays static is quite an easy way of improving things - and it really does matter when there is many of arrays - or when they contain large data.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

But still the same comment about global scope of the variable is applied.

I don't understand why you keep claiming the variable is global when it is not?

It is. The static keyword makes it global, but putting the entire definition into the function is asking for troubles.

Please, describe the trouble we can get with a local static const variable? A real concrete threat there is. I have explained the benefit. I have also explained the concrete possibility of name collision when we really do a global out of local.

Quoted 9 lines

I guess some C standard chapter describes that in non-understandable language.

Quoted 5 lines
Quoted 2 lines

As I explained before, hiding global variables inside a function is a bad code practice.

I don't really get what you mean here. And I definitely don't see any improvement if we would really use a global variable instead of a local one.

The improvement is avoid hiding the global variable to the local namespace.

I guess you mean that you may miss the fact that a variable stays there even after execution exits the function, right? Ok, let's assume someone misses this point when reading the code. Now, please describe me the potential issues this can cause knowing our static is const and doesn't change the value.

Best Regards
-- Matti

Signature · 8 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~

Discuss - Estimate - Plan - Report and finally accomplish this:
void do_work(int time) __attribute__ ((const));
#19 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Quoted 30 lines · Matti Vaittinen
Quoted 29 lines · Andy Shevchenko
Quoted 6 lines · Matti Vaittinen
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Matti Vaittinen
Quoted 2 lines · Jonathan Cameron

On Fri, 19 Aug 2022 22:19:17 +0300
Matti Vaittinen <[email protected]> wrote:

...

Quoted 17 lines
Quoted 13 lines
Quoted 12 lines
Quoted 2 lines

For the whole static / vs non static. My personal preference is not to have the static marking but I don't care that much.

I'd like to stick with the static here. I know this one particular array does not have much of a footprint - but I'd like to encourage the habit of considering the memory usage. This discussion serves as an example of how unknown the impact of making const data static is. I didn't know this myself until Sebastian educated me :) Hence my strong preference on keeping this 'static' as an example for others who are as ignorant as I were ;) After all, having const data arrays static is quite an easy way of improving things - and it really does matter when there is many of arrays - or when they contain large data.

Quoted 1 line

But still the same comment about global scope of the variable is applied.

I don't understand why you keep claiming the variable is global when it is not?

Quoted 7 lines
Quoted 2 lines

It is. The static keyword makes it global, but putting the entire definition into the function is asking for troubles.

Please, describe the trouble we can get with a local static const variable? A real concrete threat there is. I have explained the benefit. I have also explained the concrete possibility of name collision when we really do a global out of local.

I told you, the benefit is not to play dirty tricks on developers, maintainers and reviewers. It's simply harder to read the code and get the usage of the variable that lifetime is out of scope of the function.

Quoted 8 lines
Quoted 8 lines

I guess some C standard chapter describes that in non-understandable language.

Quoted 5 lines
Quoted 2 lines

As I explained before, hiding global variables inside a function is a bad code practice.

I don't really get what you mean here. And I definitely don't see any improvement if we would really use a global variable instead of a local one.

Quoted 7 lines
Quoted 1 line

The improvement is avoid hiding the global variable to the local namespace.

I guess you mean that you may miss the fact that a variable stays there even after execution exits the function, right? Ok, let's assume someone misses this point when reading the code. Now, please describe me the potential issues this can cause knowing our static is const and doesn't change the value.

When you hide the static variable inside the function, you simply narrow visibility to the compiler, but the variable stays all the time the module is in.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#20 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Quoted 35 lines · Andy Shevchenko
Quoted 32 lines · Matti Vaittinen
Quoted 31 lines · Andy Shevchenko
Quoted 6 lines · Matti Vaittinen
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Matti Vaittinen
Quoted 2 lines · Jonathan Cameron

On Fri, 19 Aug 2022 22:19:17 +0300
Matti Vaittinen <[email protected]> wrote:

...

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 2 lines

For the whole static / vs non static. My personal preference is not to have the static marking but I don't care that much.

I'd like to stick with the static here. I know this one particular array does not have much of a footprint - but I'd like to encourage the habit of considering the memory usage. This discussion serves as an example of how unknown the impact of making const data static is. I didn't know this myself until Sebastian educated me :) Hence my strong preference on keeping this 'static' as an example for others who are as ignorant as I were ;) After all, having const data arrays static is quite an easy way of improving things - and it really does matter when there is many of arrays - or when they contain large data.

Quoted 4 lines
Quoted 1 line

But still the same comment about global scope of the variable is applied.

I don't understand why you keep claiming the variable is global when it is not?

It is. The static keyword makes it global, but putting the entire definition into the function is asking for troubles.

Quoted 7 lines
Quoted 4 lines

Please, describe the trouble we can get with a local static const variable? A real concrete threat there is. I have explained the benefit. I have also explained the concrete possibility of name collision when we really do a global out of local.

I told you, the benefit is not to play dirty tricks on developers, maintainers and reviewers.

I see nothing concrete in that statement.

Quoted 3 lines

It's simply harder to read the code and get
the usage of the variable that lifetime is out of scope of the function.

This still makes no sense to me. Lifetime is the same even if we put the variable out of the function as you suggested. It does not change. And a reviewer missing that fact for a const data does really not matter. Variable is there when function is entered, it has always the same value
- it just is not in the stack. I agree that a variable which value may change between function calls is more difficult to track when it is static. This is not the case here.

For a reviewer or code reader it is actually _much simpler_ to see where the variable is used when we put it in the block where it is used. If I did as you suggested and pulled it out of the function then every code reader should grep the whole file to detect the use. Now readers only need to check if a pointer to the variable is returned out of the function. Oh, and this same check should be done for truly global variables too - as users can store the pointer to something which does not match the grep.

So, again. Putting the variables (also static ones) in the blocks do make code reading and reviewing _easier_.

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines

I guess some C standard chapter describes that in non-understandable language.

Quoted 5 lines
Quoted 2 lines

As I explained before, hiding global variables inside a function is a bad code practice.

I don't really get what you mean here. And I definitely don't see any improvement if we would really use a global variable instead of a local one.

Quoted 7 lines
Quoted 1 line

The improvement is avoid hiding the global variable to the local namespace.

I guess you mean that you may miss the fact that a variable stays there even after execution exits the function, right? Ok, let's assume someone misses this point when reading the code. Now, please describe me the potential issues this can cause knowing our static is const and doesn't change the value.

Quoted 3 lines

When you hide the static variable inside the function, you simply narrow visibility to the compiler, but the variable stays all the time the module is in.

Yes. The constant, unchanging data stays there all the time. How does it make your reviewing harder? Why do you care whether the data stays in the same place or not? What you should be interested is where and how the data is accessed - and this is where having the variable local will actually help you.

More I think of this, less I can see the problem you see :(

Best Regards
-- Matti

Signature · 9 lines
The Linux Kernel guy at ROHM Semiconductors

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

~~ this year is the year of a signature writers block ~~
maintainer
#21 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Interesting to see the different mental models used when reading code.

Overall I'm fine with either static within function or static outside function depending on author preference + for short cases like this I'm also fine with them on the stack.

Obviously can only speak for what I'll take into IIO!

Jonathan

maintainer Reviewed-by on v3 7/14
#22 lore raw

RE: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

In reply to Matti Vaittinen
Quoted 10 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:19 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Sa, Nuno <[email protected]>; Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; linux- [email protected]; [email protected]
Subject: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

Quoted 8 lines

[External]

Use devm_regulator_bulk_get_enable() instead of open coded bulk- get,
bulk-enable, add-action-to-disable-at-detach - pattern.

FWIW, I also don't care that much about the whole variable scope discussion... Hence, with the change requested by Jonathan:

maintainer
#23 lore raw

Re: [PATCH v3 07/14] iio: ltc2688: Simplify using devm_regulator_*get_enable()

In reply to Matti Vaittinen
Quoted 4 lines · On Fri, 19 Aug 2022 22:19:17 +0300 Matti Vaittinen <[email protected]>…

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

Applied with the ordering tweaked as per Andy's review. Also tweaked the patch title to iio: dac: ltc2688:
just to make it a bit easier to go from patch title to driver.

Quoted 66 lines

---
v2 => v3
Split to own patch.

---
 drivers/iio/dac/ltc2688.c | 23 +++--------------------
 1 file changed, 3 insertions(+), 20 deletions(-)
drivers/iio/dac/ltc2688.c +3 -20
index 28bdde2d3088..fcad3efe62ea 100644
@@ -84,7 +84,6 @@ struct ltc2688_chan {
 struct ltc2688_state {
 	struct spi_device *spi;
 	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
 	struct ltc2688_chan channels[LTC2688_DAC_CHANNELS];
 	struct iio_chan_spec *iio_chan;
 	/* lock to protect against multiple access to the device and shared data */
@@ -902,13 +901,6 @@ static int ltc2688_setup(struct ltc2688_state *st, struct regulator *vref)
 			       LTC2688_CONFIG_EXT_REF);
 }
 
-static void ltc2688_disable_regulators(void *data)
-{
-	struct ltc2688_state *st = data;
-
-	regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators);
-}
-
 static void ltc2688_disable_regulator(void *regulator)
 {
 	regulator_disable(regulator);
@@ -970,6 +962,7 @@ static int ltc2688_probe(struct spi_device *spi)
 	struct regulator *vref_reg;
 	struct device *dev = &spi->dev;
 	int ret;
+	static const char * const regulators[] = {"vcc", "iovcc"};
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
 	if (!indio_dev)
@@ -988,21 +981,11 @@ static int ltc2688_probe(struct spi_device *spi)
 		return dev_err_probe(dev, PTR_ERR(st->regmap),
 				     "Failed to init regmap");
 
-	st->regulators[0].supply = "vcc";
-	st->regulators[1].supply = "iovcc";
-	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st->regulators),
-				      st->regulators);
-	if (ret)
-		return dev_err_probe(dev, ret, "Failed to get regulators\n");
-
-	ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators);
+	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators),
+					     regulators);
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to enable regulators\n");
 
-	ret = devm_add_action_or_reset(dev, ltc2688_disable_regulators, st);
-	if (ret)
-		return ret;
-
 	vref_reg = devm_regulator_get_optional(dev, "vref");
 	if (IS_ERR(vref_reg)) {
 		if (PTR_ERR(vref_reg) != -ENODEV)
author maintainer
#24 lore raw

PATCH v3 8/14 iio: bmg160_core: Simplify using devm_regulator_*get_enable()

In reply to Matti Vaittinen

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

---
v2 => v3
Split to own patch.

---
 drivers/iio/gyro/bmg160_core.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)
drivers/iio/gyro/bmg160_core.c +3 -21
index cedd9f02ea21..baa80980c99f 100644
@@ -93,7 +93,6 @@
 
 struct bmg160_data {
 	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
 	struct iio_trigger *dready_trig;
 	struct iio_trigger *motion_trig;
 	struct iio_mount_matrix orientation;
@@ -1067,19 +1066,13 @@ static const char *bmg160_match_acpi_device(struct device *dev)
 	return dev_name(dev);
 }
 
-static void bmg160_disable_regulators(void *d)
-{
-	struct bmg160_data *data = d;
-
-	regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
-}
-
 int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
 		      const char *name)
 {
 	struct bmg160_data *data;
 	struct iio_dev *indio_dev;
 	int ret;
+	static const char * const regulators[] = {"vdd", "vddio"};
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
 	if (!indio_dev)
@@ -1090,22 +1083,11 @@ int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
 	data->irq = irq;
 	data->regmap = regmap;
 
-	data->regulators[0].supply = "vdd";
-	data->regulators[1].supply = "vddio";
-	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
-				      data->regulators);
+	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators),
+					     regulators);
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to get regulators\n");
 
-	ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
-				    data->regulators);
-	if (ret)
-		return ret;
-
-	ret = devm_add_action_or_reset(dev, bmg160_disable_regulators, data);
-	if (ret)
-		return ret;
-
 	ret = iio_read_mount_matrix(dev, &data->orientation);
 	if (ret)
 		return ret;
Signature · 14 lines
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 =] 
#25 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 3 lines · Matti Vaittinen

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

...

Quoted 7 lines

int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
const char *name)

 {
        struct bmg160_data *data;
        struct iio_dev *indio_dev;
        int ret;

+ static const char * const regulators[] = {"vdd", "vddio"};

Please, keep this following the "longest line first" rule. Note, in this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

P.S. Same applies for the rest of the similar places in your series.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#26 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Thanks for the review Andy

Quoted 16 lines · Andy Shevchenko
Quoted 3 lines · Matti Vaittinen

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

...

Quoted 7 lines

int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
const char *name)

  {
         struct bmg160_data *data;
         struct iio_dev *indio_dev;
         int ret;

+ static const char * const regulators[] = {"vdd", "vddio"};

Quoted 1 line

Please, keep this following the "longest line first" rule. Note, in

This was not following the (IMO slightly silly) rule even prior my patch. I can for sure move my line up - but that won't give you the "reverse X-mas tree".

I don't have any real objections on changing the styling though - I don't expect this to be merged before the dependency is in rc1 - so I guess I will anyways need to respin this for next cycle. I can do the styling then.

Quoted 2 lines

this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

Here I do disagree with you. Moving the array out of the function makes it _much_ less obvious it is not used outside this function. Reason for making is "static const" is to allow the data be placed in read-only area (thanks to Guenter who originally gave me this tip).

Quoted 2 lines

P.S. Same applies for the rest of the similar places in your series.

Br,
-- Matti

Signature · 9 lines
The Linux Kernel guy at ROHM Semiconductors

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

~~ this year is the year of a signature writers block ~~
#27 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 3 lines · Matti
Quoted 2 lines · Andy Shevchenko

On Fri, Aug 19, 2022 at 10:21 PM Matti Vaittinen
<[email protected]> wrote:

...

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
         struct bmg160_data *data;
         struct iio_dev *indio_dev;
         int ret;

+ static const char * const regulators[] = {"vdd", "vddio"};

Please, keep this following the "longest line first" rule. Note, in

This was not following the (IMO slightly silly) rule even prior my patch. I can for sure move my line up - but that won't give you the "reverse X-mas tree".

What do you mean by this? In the above case the rule does exactly give you "reversed xmas tree order". What did I miss?

Quoted 4 lines

I don't have any real objections on changing the styling though - I don't expect this to be merged before the dependency is in rc1 - so I guess I will anyways need to respin this for next cycle. I can do the styling then.

Fine with me.

Quoted 7 lines
Quoted 2 lines

this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

Here I do disagree with you. Moving the array out of the function makes it _much_ less obvious it is not used outside this function. Reason for making is "static const" is to allow the data be placed in read-only area (thanks to Guenter who originally gave me this tip).

"static" in C language means two things (that's what come to my mind):
- for functions this tells that a function is not used outside of the module;
- for variables that it is a _global_ variable.

Hiding static inside functions is not a good coding practice since it hides scope of the variable. And if you look into the kernel code, I believe the use you are proposing is in minority.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#28 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 10 lines · Andy Shevchenko
Quoted 3 lines · Matti
Quoted 2 lines · Andy Shevchenko

On Fri, Aug 19, 2022 at 10:21 PM Matti Vaittinen
<[email protected]> wrote:

What did I miss?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
          struct bmg160_data *data;
          struct iio_dev *indio_dev;

This does already violate the rule.

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

Here I do disagree with you. Moving the array out of the function makes it _much_ less obvious it is not used outside this function. Reason for making is "static const" is to allow the data be placed in read-only area (thanks to Guenter who originally gave me this tip).

Quoted 6 lines

"static" in C language means two things (that's what come to my mind):
- for functions this tells that a function is not used outside of the module;
- for variables that it is a _global_ variable.

Hiding static inside functions is not a good coding practice since it hides scope of the variable.

For const arrays the static in function does make sense. Being able to place the data in read-only areas do help with the memory on limited systems.

Quoted 2 lines

And if you look into the kernel code, I
believe the use you are proposing is in minority.

I don't know about the statistics. What I know is that we do have a technical benefits when we use static const arrays instead of non static ones in the functions. I do also believe placing the variables in blocks is a good practice.

I tend to agree with you that using local, non const statics has pitfalls - but the pitfalls do not really apply with const ones. You know the value and have no races. Benefit is that just by seeing that no pointer is returned you can be sure that no "sane code" uses the data outside the function it resides.

Best Regards
-- Matti

Signature · 9 lines
The Linux Kernel guy at ROHM Semiconductors

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

~~ this year is the year of a signature writers block ~~
#29 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 7 lines · Matti
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Matti
Quoted 2 lines · Andy Shevchenko

On Fri, Aug 19, 2022 at 10:21 PM Matti Vaittinen
<[email protected]> wrote:

Quoted 6 lines
Quoted 4 lines

What did I miss?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
          struct bmg160_data *data;
          struct iio_dev *indio_dev;

This does already violate the rule.

Indeed, I am reading this with an MTA that has True Type fonts, and I can't see it at the first glance. But this breaks that rule slightly while your added line breaks it significantly.

Quoted 12 lines
Quoted 12 lines
Quoted 7 lines
Quoted 2 lines

this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

Here I do disagree with you. Moving the array out of the function makes it _much_ less obvious it is not used outside this function. Reason for making is "static const" is to allow the data be placed in read-only area (thanks to Guenter who originally gave me this tip).

"static" in C language means two things (that's what come to my mind):
- for functions this tells that a function is not used outside of the module;
- for variables that it is a _global_ variable.

Quoted 6 lines
Quoted 2 lines

Hiding static inside functions is not a good coding practice since it hides scope of the variable.

For const arrays the static in function does make sense. Being able to place the data in read-only areas do help with the memory on limited systems.

I'm not sure we are on the same page. I do not object to the "const" part and we are _not_ talking about that.

Quoted 7 lines
Quoted 2 lines

And if you look into the kernel code, I
believe the use you are proposing is in minority.

I don't know about the statistics. What I know is that we do have a technical benefits when we use static const arrays instead of non static ones in the functions. I do also believe placing the variables in blocks is a good practice.

Yes, and global variables are better to be seen as global variables.

Quoted 5 lines

I tend to agree with you that using local, non const statics has pitfalls - but the pitfalls do not really apply with const ones. You know the value and have no races. Benefit is that just by seeing that no pointer is returned you can be sure that no "sane code" uses the data outside the function it resides.

Putting a global variable (const or non-const) to the function will hide its scope and it is prone to getting two variables with the same or very similar names with quite different semantics. That's why it's really not good practice. I would rather see it outside of the function _esp_ because it's static const.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#30 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 11 lines · Andy Shevchenko
Quoted 9 lines · Matti
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Matti
Quoted 2 lines · Andy Shevchenko

On Fri, Aug 19, 2022 at 10:21 PM Matti Vaittinen
<[email protected]> wrote:

What did I miss?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
          struct bmg160_data *data;
          struct iio_dev *indio_dev;

This does already violate the rule.

Indeed, I am reading this with an MTA that has True Type fonts, and I can't see it at the first glance. But this breaks that rule slightly while your added line breaks it significantly.

Yes. As I said, I think the reverse xmas tree rule is not too well justified. Bunch of the subsystems do not really follow it, nor did this function. Yet, as I said, I can move the array to the first line in the function when I respin the series..

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

Here I do disagree with you. Moving the array out of the function makes it _much_ less obvious it is not used outside this function. Reason for making is "static const" is to allow the data be placed in read-only area (thanks to Guenter who originally gave me this tip).

Just wanted to correct - it was Sebastian Reichel, not Guenter who explained me why doing local static const arrays is better than plain const.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

"static" in C language means two things (that's what come to my mind):
- for functions this tells that a function is not used outside of the module;
- for variables that it is a _global_ variable.

Hiding static inside functions is not a good coding practice since it hides scope of the variable.

Quoted 7 lines
Quoted 3 lines

For const arrays the static in function does make sense. Being able to place the data in read-only areas do help with the memory on limited systems.

I'm not sure we are on the same page. I do not object to the "const" part and we are _not_ talking about that.

Maybe the explanation by Sebastian here can put us on the same page:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

Quoted 16 lines
Quoted 7 lines
Quoted 2 lines

And if you look into the kernel code, I
believe the use you are proposing is in minority.

I don't know about the statistics. What I know is that we do have a technical benefits when we use static const arrays instead of non static ones in the functions. I do also believe placing the variables in blocks is a good practice.

Yes, and global variables are better to be seen as global variables.

Quoted 5 lines

I tend to agree with you that using local, non const statics has pitfalls - but the pitfalls do not really apply with const ones. You know the value and have no races. Benefit is that just by seeing that no pointer is returned you can be sure that no "sane code" uses the data outside the function it resides.

Quoted 3 lines

Putting a global variable (const or non-const) to the function will hide its scope and it is prone to getting two variables with the same or very similar names with quite different semantics.

I don't see how moving something from a local block to a global scope does make conflicts less of an issue? On the contrary, it makes things worse as then the moved variable will collide with any other variable in any of the functions in the whole file. Having the array as function local static makes the naming collisions to be issue only if another global variable has the same name. And if that happened - the chances are code would still be correct as the function here is clearly intended to use the local one. If someone really later adds a global with the same name - and uses the global in this function - then he should have noted we have local variable with same name. Additionally - such user would be using terribly bad name for a global variable.

Please note that scope of the function local static variable is limited to function even if the life-time is not just the life-time of a function.

Quoted 3 lines

That's why it's
really not good practice. I would rather see it outside of the function _esp_ because it's static const.

Sorry, I really don't agree with your reasoning here. :(

Signature · 8 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~

Discuss - Estimate - Plan - Report and finally accomplish this:
void do_work(int time) __attribute__ ((const));
#31 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 17 lines · Matti Vaittinen
Quoted 16 lines · Andy Shevchenko
Quoted 13 lines · Matti
Quoted 10 lines · Andy Shevchenko
Quoted 3 lines · Matti
Quoted 2 lines · Andy Shevchenko

On Fri, Aug 19, 2022 at 10:21 PM Matti Vaittinen
<[email protected]> wrote:

What did I miss?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
          struct bmg160_data *data;
          struct iio_dev *indio_dev;

This does already violate the rule.

Quoted 8 lines
Quoted 3 lines

Indeed, I am reading this with an MTA that has True Type fonts, and I can't see it at the first glance. But this breaks that rule slightly while your added line breaks it significantly.

Yes. As I said, I think the reverse xmas tree rule is not too well justified. Bunch of the subsystems do not really follow it, nor did this function. Yet, as I said, I can move the array to the first line in the function when I respin the series..

You still can do better in _your_ series, right?

Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

Here I do disagree with you. Moving the array out of the function makes it _much_ less obvious it is not used outside this function. Reason for making is "static const" is to allow the data be placed in read-only area (thanks to Guenter who originally gave me this tip).

Just wanted to correct - it was Sebastian Reichel, not Guenter who explained me why doing local static const arrays is better than plain const.

Did he suggest putting it inside the function?

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

"static" in C language means two things (that's what come to my mind):
- for functions this tells that a function is not used outside of the module;
- for variables that it is a _global_ variable.

Hiding static inside functions is not a good coding practice since it hides scope of the variable.

For const arrays the static in function does make sense. Being able to place the data in read-only areas do help with the memory on limited systems.

Quoted 6 lines
Quoted 2 lines

I'm not sure we are on the same page. I do not object to the "const" part and we are _not_ talking about that.

Maybe the explanation by Sebastian here can put us on the same page:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

Again, you are too focused on "const", I'm talking about "static". The above doesn't clear a bit regarding why you hide the global variable inside a function. I don't see either Sebastian's clear point on this.

Quoted 16 lines
Quoted 16 lines
Quoted 7 lines
Quoted 2 lines

And if you look into the kernel code, I
believe the use you are proposing is in minority.

I don't know about the statistics. What I know is that we do have a technical benefits when we use static const arrays instead of non static ones in the functions. I do also believe placing the variables in blocks is a good practice.

Yes, and global variables are better to be seen as global variables.

Quoted 5 lines

I tend to agree with you that using local, non const statics has pitfalls - but the pitfalls do not really apply with const ones. You know the value and have no races. Benefit is that just by seeing that no pointer is returned you can be sure that no "sane code" uses the data outside the function it resides.

Quoted 6 lines
Quoted 3 lines

Putting a global variable (const or non-const) to the function will hide its scope and it is prone to getting two variables with the same or very similar names with quite different semantics.

I don't see how moving something from a local block to a global scope does make conflicts less of an issue?

You may add a static variable inside each function in the same module and name it "foo" and there will be no conflict, but when you read the code your brain will be spoiled. This is simply _bad code practice_. I don't know how else I can explain this to you.

Quoted 5 lines

On the contrary, it makes things
worse as then the moved variable will collide with any other variable in any of the functions in the whole file. Having the array as function local static makes the naming collisions to be issue only if another global variable has the same name.

Again, you missed my point. I'm talking about reading and analysing the code. Otherwise (good) compiler should spill a lot of warnings in case you have global vs. local naming collision.

Quoted 9 lines

And if that happened - the chances
are code would still be correct as the function here is clearly intended to use the local one. If someone really later adds a global with the same name - and uses the global in this function - then he should have noted we have local variable with same name. Additionally - such user would be using terribly bad name for a global variable.

Please note that scope of the function local static variable is limited to function even if the life-time is not just the life-time of a function.

Nope. The RO section might be very well flashed into ROM, so...

Quoted 5 lines
Quoted 3 lines

That's why it's
really not good practice. I would rather see it outside of the function _esp_ because it's static const.

Sorry, I really don't agree with your reasoning here. :(

So, whom should we listen to here? Because bad code is bad code. And this is code above.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#32 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 23 lines · Andy Shevchenko
Quoted 21 lines · Matti Vaittinen
Quoted 19 lines · Andy Shevchenko
Quoted 7 lines · Matti
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Matti
Quoted 2 lines · Andy Shevchenko

On Fri, Aug 19, 2022 at 10:21 PM Matti Vaittinen
<[email protected]> wrote:

Quoted 6 lines
Quoted 4 lines

What did I miss?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
          struct bmg160_data *data;
          struct iio_dev *indio_dev;

This does already violate the rule.

Indeed, I am reading this with an MTA that has True Type fonts, and I can't see it at the first glance. But this breaks that rule slightly while your added line breaks it significantly.

Quoted 6 lines
Quoted 4 lines

Yes. As I said, I think the reverse xmas tree rule is not too well justified. Bunch of the subsystems do not really follow it, nor did this function. Yet, as I said, I can move the array to the first line in the function when I respin the series..

You still can do better in _your_ series, right?

I don't see the benefit of the reverse xmas tree. We have discussed this already in the past :) I definitely have no need to start using reverse xmas tree thingee somewhere it has not been previously used. It may be better in _your_ opinion.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

Here I do disagree with you. Moving the array out of the function makes it _much_ less obvious it is not used outside this function. Reason for making is "static const" is to allow the data be placed in read-only area (thanks to Guenter who originally gave me this tip).

Quoted 4 lines
Quoted 2 lines

Just wanted to correct - it was Sebastian Reichel, not Guenter who explained me why doing local static const arrays is better than plain const.

Did he suggest putting it inside the function?

He asked me to convert a local array to static const. I though like you do now that the local array should not be static but just const - and he corrected me in his reply. This can be seen in the discussion I linked below.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 10 lines
Quoted 6 lines

"static" in C language means two things (that's what come to my mind):
- for functions this tells that a function is not used outside of the module;
- for variables that it is a _global_ variable.

Hiding static inside functions is not a good coding practice since it hides scope of the variable.

For const arrays the static in function does make sense. Being able to place the data in read-only areas do help with the memory on limited systems.

I'm not sure we are on the same page. I do not object to the "const" part and we are _not_ talking about that.

Quoted 7 lines
Quoted 3 lines

Maybe the explanation by Sebastian here can put us on the same page:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

Again, you are too focused on "const", I'm talking about "static". The above doesn't clear a bit regarding why you hide the global variable inside a function. I don't see either Sebastian's clear point on this.

I don't really see why you talk about "hiding a global variable in a function"? A static variable which is declared in function is not global. It is local. It causes no more name collisions than a regular local variable does so I really don't understand your reasoning.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 7 lines
Quoted 2 lines

And if you look into the kernel code, I
believe the use you are proposing is in minority.

I don't know about the statistics. What I know is that we do have a technical benefits when we use static const arrays instead of non static ones in the functions. I do also believe placing the variables in blocks is a good practice.

Yes, and global variables are better to be seen as global variables.

Quoted 5 lines

I tend to agree with you that using local, non const statics has pitfalls - but the pitfalls do not really apply with const ones. You know the value and have no races. Benefit is that just by seeing that no pointer is returned you can be sure that no "sane code" uses the data outside the function it resides.

Putting a global variable (const or non-const) to the function will hide its scope and it is prone to getting two variables with the same or very similar names with quite different semantics.

Quoted 6 lines
Quoted 2 lines

I don't see how moving something from a local block to a global scope does make conflicts less of an issue?

You may add a static variable inside each function in the same module and name it "foo" and there will be no conflict, but when you read the code your brain will be spoiled.

And how is it different from reading functions where the regular variables have identical names? I _really_ can't follow your reasoning.

Quoted 11 lines

This is simply _bad code practice_. I
don't know how else I can explain this to you.

Quoted 5 lines

On the contrary, it makes things
worse as then the moved variable will collide with any other variable in any of the functions in the whole file. Having the array as function local static makes the naming collisions to be issue only if another global variable has the same name.

Again, you missed my point. I'm talking about reading and analysing the code.

I _definitely_ miss your point here. I have zero problems reading code where static const variables are used in a function. I think it is pretty much as hard as seeing a #defined value - difference being that one can point to the variable.

I admit that static variables whose value is changed can be more of a problem especially when access to function is not serialized.

Quoted 10 lines

Otherwise (good) compiler should spill a lot of warnings in case you have global vs. local naming collision.

Quoted 7 lines

And if that happened - the chances
are code would still be correct as the function here is clearly intended to use the local one. If someone really later adds a global with the same name - and uses the global in this function - then he should have noted we have local variable with same name. Additionally - such user would be using terribly bad name for a global variable.

Quoted 4 lines
Quoted 2 lines

Please note that scope of the function local static variable is limited to function even if the life-time is not just the life-time of a function.

Nope. The RO section might be very well flashed into ROM, so...

...so?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

That's why it's
really not good practice. I would rather see it outside of the function _esp_ because it's static const.

Sorry, I really don't agree with your reasoning here. :(

So, whom should we listen to here? Because bad code is bad code. And this is code above.

Bad is a subjective concept. I'd say the code gets much worse if we make the local variable a global one.

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#33 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 29 lines · Matti Vaittinen
Quoted 28 lines · Andy Shevchenko
Quoted 25 lines · Matti Vaittinen
Quoted 11 lines · Andy Shevchenko
Quoted 9 lines · Matti
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Matti
Quoted 2 lines · Andy Shevchenko

On Fri, Aug 19, 2022 at 10:21 PM Matti Vaittinen
<[email protected]> wrote:

What did I miss?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
          struct bmg160_data *data;
          struct iio_dev *indio_dev;

This does already violate the rule.

Indeed, I am reading this with an MTA that has True Type fonts, and I can't see it at the first glance. But this breaks that rule slightly while your added line breaks it significantly.

Yes. As I said, I think the reverse xmas tree rule is not too well justified. Bunch of the subsystems do not really follow it, nor did this function. Yet, as I said, I can move the array to the first line in the function when I respin the series..

Quoted 6 lines
Quoted 1 line

You still can do better in _your_ series, right?

I don't see the benefit of the reverse xmas tree. We have discussed this already in the past :) I definitely have no need to start using reverse xmas tree thingee somewhere it has not been previously used. It may be better in _your_ opinion.

Yes, this is a style issue and not a real coding problem. As a reviewer with some of a background I feel that reversed xmas tree ordering is better to read, maintain, and review. So, as a reviewer I have an opinion, as a maintainer I can speak for the IIO subsystem, since it's not on my watch.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

Here I do disagree with you. Moving the array out of the function makes it _much_ less obvious it is not used outside this function. Reason for making is "static const" is to allow the data be placed in read-only area (thanks to Guenter who originally gave me this tip).

Just wanted to correct - it was Sebastian Reichel, not Guenter who explained me why doing local static const arrays is better than plain const.

Quoted 6 lines
Quoted 1 line

Did he suggest putting it inside the function?

He asked me to convert a local array to static const. I though like you do now that the local array should not be static but just const - and he corrected me in his reply. This can be seen in the discussion I linked below.

Yes, and it's irrelevant to what I'm trying to tell you.

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

"static" in C language means two things (that's what come to my mind):
- for functions this tells that a function is not used outside of the module;
- for variables that it is a _global_ variable.

Hiding static inside functions is not a good coding practice since it hides scope of the variable.

Quoted 6 lines
Quoted 3 lines

For const arrays the static in function does make sense. Being able to place the data in read-only areas do help with the memory on limited systems.

I'm not sure we are on the same page. I do not object to the "const" part and we are _not_ talking about that.

Maybe the explanation by Sebastian here can put us on the same page:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 3 lines

Again, you are too focused on "const", I'm talking about "static". The above doesn't clear a bit regarding why you hide the global variable inside a function. I don't see either Sebastian's clear point on this.

I don't really see why you talk about "hiding a global variable in a function"? A static variable which is declared in function is not global. It is local.

SInce it's static it's global by nature, but local by namespace.

Quoted 2 lines

It causes no more name collisions than a regular
local variable does so I really don't understand your reasoning.

And I have no other words to explain it to you. You are using a global variable in the scope of function. This is not good for the maintenance and development as it's prone to get an issue in the future.

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 16 lines
Quoted 7 lines
Quoted 2 lines

And if you look into the kernel code, I
believe the use you are proposing is in minority.

I don't know about the statistics. What I know is that we do have a technical benefits when we use static const arrays instead of non static ones in the functions. I do also believe placing the variables in blocks is a good practice.

Yes, and global variables are better to be seen as global variables.

Quoted 5 lines

I tend to agree with you that using local, non const statics has pitfalls - but the pitfalls do not really apply with const ones. You know the value and have no races. Benefit is that just by seeing that no pointer is returned you can be sure that no "sane code" uses the data outside the function it resides.

Quoted 3 lines

Putting a global variable (const or non-const) to the function will hide its scope and it is prone to getting two variables with the same or very similar names with quite different semantics.

I don't see how moving something from a local block to a global scope does make conflicts less of an issue?

Quoted 6 lines
Quoted 3 lines

You may add a static variable inside each function in the same module and name it "foo" and there will be no conflict, but when you read the code your brain will be spoiled.

And how is it different from reading functions where the regular variables have identical names? I _really_ can't follow your reasoning.

Because they are on the stack and not one per module. When you read the code it's very easy to miss that the variable is static if you have a lot of other variables defined.

Quoted 9 lines
Quoted 9 lines

This is simply _bad code practice_. I
don't know how else I can explain this to you.

Quoted 5 lines

On the contrary, it makes things
worse as then the moved variable will collide with any other variable in any of the functions in the whole file. Having the array as function local static makes the naming collisions to be issue only if another global variable has the same name.

Quoted 7 lines
Quoted 2 lines

Again, you missed my point. I'm talking about reading and analysing the code.

I _definitely_ miss your point here. I have zero problems reading code where static const variables are used in a function. I think it is pretty much as hard as seeing a #defined value - difference being that one can point to the variable.

Good for you.

Quoted 13 lines

I admit that static variables whose value is changed can be more of a problem especially when access to function is not serialized.

Quoted 10 lines

Otherwise (good) compiler should spill a lot of warnings in case you have global vs. local naming collision.

Quoted 7 lines

And if that happened - the chances
are code would still be correct as the function here is clearly intended to use the local one. If someone really later adds a global with the same name - and uses the global in this function - then he should have noted we have local variable with same name. Additionally - such user would be using terribly bad name for a global variable.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Please note that scope of the function local static variable is limited to function even if the life-time is not just the life-time of a function.

Nope. The RO section might be very well flashed into ROM, so...

...so?

It won't be created by function, it's created by the compiler / linker. It won't gone if function gone. e.g.

__init foo()
{
static bar ...;
}

is nonsense. And it takes a ROM space.

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

That's why it's
really not good practice. I would rather see it outside of the function _esp_ because it's static const.

Sorry, I really don't agree with your reasoning here. :(

So, whom should we listen to here? Because bad code is bad code. And this is code above.

Bad is a subjective concept. I'd say the code gets much worse if we make the local variable a global one.

...

To summarize, we have a huge disagreement on the placement of the static variables. Not sure we ever get into compromize here, so I leave it up to maintainers, but my opinion that it is simply a bad code practice.

--
With Best Regards,
Andy Shevchenko

#34 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Matti Vaittinen
Quoted 3 lines · Andy Shevchenko
Quoted 1 line · Matti Vaittinen

On 8/20/22 10:18, Andy Shevchenko wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines · Matti
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Matti
Quoted 2 lines · Andy Shevchenko

On Fri, Aug 19, 2022 at 10:21 PM Matti Vaittinen
<[email protected]> wrote:

//snip

Quoted 1 line

SInce it's static it's global by nature, but local by namespace.

Which is an _improvement_ over having it in global namespace?

Quoted 7 lines
Quoted 2 lines

It causes no more name collisions than a regular
local variable does so I really don't understand your reasoning.

And I have no other words to explain it to you. You are using a global variable in the scope of function. This is not good for the maintenance and development as it's prone to get an issue in the future.

If you foresee some issues, please describe them as I don't see one single problem with a local static const data. I have seen you telling me that "static const" variables are _harder_ for you to review. Could you please explain what are the potential mistake(s) a reviewer can do, and what is the 'issue' that mistake can cause?

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

So, whom should we listen to here? Because bad code is bad code. And this is code above.

Bad is a subjective concept. I'd say the code gets much worse if we make the local variable a global one.

Quoted 7 lines

...

To summarize, we have a huge disagreement on the placement of the static variables. Not sure we ever get into compromize here, so I leave it up to maintainers, but my opinion that it is simply a bad code practice.

Bad and good are labels we can place on things. We however need to have the reason for those labels to be meaningful. I am sorry but I don't want to label the local _const_ static variables bad without reason. This discussion starts to remind me on statements like "using goto is always bad" or "one must never use macros in C".

Yeah - ultimately it is a maintainer decision.

Best Regards
-- Matti

Signature · 9 lines
The Linux Kernel guy at ROHM Semiconductors

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

~~ this year is the year of a signature writers block ~~
maintainer
#35 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

In reply to Vaittinen, Matti
Quoted 21 lines · Matti

Thanks for the review Andy

Quoted 16 lines · Andy Shevchenko
Quoted 3 lines · Matti Vaittinen

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

...

Quoted 7 lines

int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
const char *name)

  {
         struct bmg160_data *data;
         struct iio_dev *indio_dev;
         int ret;

+ static const char * const regulators[] = {"vdd", "vddio"};

Quoted 1 line

Please, keep this following the "longest line first" rule. Note, in

Quoted 8 lines

This was not following the (IMO slightly silly) rule even prior my patch. I can for sure move my line up - but that won't give you the "reverse X-mas tree".

I don't have any real objections on changing the styling though - I don't expect this to be merged before the dependency is in rc1 - so I guess I will anyways need to respin this for next cycle. I can do the styling then.

I was a bit surprised Mark didn't do an immutable branch for this, but indeed looks like it's going to be a multiple cycle thing - so we'll probably have a bunch of new cases introduced in the meantime that we need to tidy up. Ah well.

Quoted 15 lines
Quoted 3 lines

this case you even can move it out of the function, so we will see clearly that this is (not a hidden) global variable.

Here I do disagree with you. Moving the array out of the function makes it _much_ less obvious it is not used outside this function. Reason for making is "static const" is to allow the data be placed in read-only area (thanks to Guenter who originally gave me this tip).

Quoted 2 lines

P.S. Same applies for the rest of the similar places in your series.

Br,
-- Matti

author maintainer
#36 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · Matti

Thanks for the review Andy

Quoted 2 lines · Andy Shevchenko

On Fri, Aug 19, 2022 at 10:21 PM Matti Vaittinen
<[email protected]> wrote:

Quoted 9 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

I

don't expect this to be merged before the dependency is in rc1 - so I guess I will anyways need to respin this for next cycle. I can do the styling then.

I was a bit surprised Mark didn't do an immutable branch for this, but indeed looks like it's going to be a multiple cycle thing - so we'll probably have a bunch of new cases introduced in the meantime that we need to tidy up. Ah well.

I guess we can ask Mark what he thinks of the immutable branch ;) I admit I am not too keen on rebasing this - especially if I find the time to go through more drivers.

Signature · 8 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~

Discuss - Estimate - Plan - Report and finally accomplish this:
void do_work(int time) __attribute__ ((const));
maintainer
#37 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

In reply to Matti Vaittinen
Quoted 32 lines · On Fri, 19 Aug 2022 22:19:31 +0300 Matti Vaittinen <[email protected]>…

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

---
v2 => v3
Split to own patch.

---
 drivers/iio/gyro/bmg160_core.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)
drivers/iio/gyro/bmg160_core.c +0 -5
index cedd9f02ea21..baa80980c99f 100644
@@ -93,7 +93,6 @@
 
 struct bmg160_data {
 	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
 	struct iio_trigger *dready_trig;
 	struct iio_trigger *motion_trig;
 	struct iio_mount_matrix orientation;
@@ -1067,19 +1066,13 @@ static const char *bmg160_match_acpi_device(struct device *dev)
 	return dev_name(dev);
 }
 
-static void bmg160_disable_regulators(void *d)
-{
-	struct bmg160_data *data = d;
-

drivers/iio/gyro/bmg160_core.c static const char *bmg160_match_acpi_device(struct device *dev) · line 1075

Quoted 10 lines
drivers/iio/gyro/bmg160_core.c +1 -3
-	regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
-}
-
 int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
 		      const char *name)
 {
 	struct bmg160_data *data;
 	struct iio_dev *indio_dev;
 	int ret;
+	static const char * const regulators[] = {"vdd", "vddio"};

As in previous, small preference for spaces after { and before }

Quoted 28 lines
drivers/iio/gyro/bmg160_core.c +2 -13
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
 	if (!indio_dev)
@@ -1090,22 +1083,11 @@ int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
 	data->irq = irq;
 	data->regmap = regmap;
 
-	data->regulators[0].supply = "vdd";
-	data->regulators[1].supply = "vddio";
-	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
-				      data->regulators);
+	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators),
+					     regulators);
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to get regulators\n");
 
-	ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
-				    data->regulators);
-	if (ret)
-		return ret;
-
-	ret = devm_add_action_or_reset(dev, bmg160_disable_regulators, data);
-	if (ret)
-		return ret;
-
 	ret = iio_read_mount_matrix(dev, &data->orientation);
 	if (ret)
 		return ret;
author maintainer
#38 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 31 lines · Jonathan Cameron
Quoted 28 lines · On Fri, 19 Aug 2022 22:19:31 +0300 Matti Vaittinen <[email protected]>…

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

---
v2 => v3
Split to own patch.

---
  drivers/iio/gyro/bmg160_core.c | 24 +++---------------------
  1 file changed, 3 insertions(+), 21 deletions(-)
drivers/iio/gyro/bmg160_core.c +0 -1
index cedd9f02ea21..baa80980c99f 100644
@@ -93,7 +93,6 @@
 
  struct bmg160_data {
  	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
  	struct iio_trigger *dready_trig;
  	struct iio_trigger *motion_trig;
  	struct iio_mount_matrix orientation;
@@ -1067,19 +1066,13 @@ static const char *bmg160_match_acpi_device(struct device *dev)
  	return dev_name(dev);
  }
 
Quoted 16 lines
Quoted 4 lines
drivers/iio/gyro/bmg160_core.c +0 -4
-static void bmg160_disable_regulators(void *d)
-{
-	struct bmg160_data *data = d;
-

drivers/iio/gyro/bmg160_core.c static const char *bmg160_match_acpi_device(struct device *dev) · line 1075

Quoted 10 lines
drivers/iio/gyro/bmg160_core.c +1 -3
-	regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
-}
-
  int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
  		      const char *name)
  {
  	struct bmg160_data *data;
  	struct iio_dev *indio_dev;
  	int ret;
+	static const char * const regulators[] = {"vdd", "vddio"};

As in previous, small preference for spaces after { and before }

Thanks. I'll fix it when I respin.

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#39 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

In reply to Matti Vaittinen
Quoted 4 lines · On Fri, 19 Aug 2022 22:19:31 +0300 Matti Vaittinen <[email protected]>…

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

Applied with tweaks:
- patch title includes gyro:
- ordering as Andy suggested
- spaces after { and before }

Quoted 66 lines

---
v2 => v3
Split to own patch.

---
 drivers/iio/gyro/bmg160_core.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)
drivers/iio/gyro/bmg160_core.c +3 -21
index cedd9f02ea21..baa80980c99f 100644
@@ -93,7 +93,6 @@
 
 struct bmg160_data {
 	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
 	struct iio_trigger *dready_trig;
 	struct iio_trigger *motion_trig;
 	struct iio_mount_matrix orientation;
@@ -1067,19 +1066,13 @@ static const char *bmg160_match_acpi_device(struct device *dev)
 	return dev_name(dev);
 }
 
-static void bmg160_disable_regulators(void *d)
-{
-	struct bmg160_data *data = d;
-
-	regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
-}
-
 int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
 		      const char *name)
 {
 	struct bmg160_data *data;
 	struct iio_dev *indio_dev;
 	int ret;
+	static const char * const regulators[] = {"vdd", "vddio"};
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
 	if (!indio_dev)
@@ -1090,22 +1083,11 @@ int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
 	data->irq = irq;
 	data->regmap = regmap;
 
-	data->regulators[0].supply = "vdd";
-	data->regulators[1].supply = "vddio";
-	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
-				      data->regulators);
+	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators),
+					     regulators);
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to get regulators\n");
 
-	ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
-				    data->regulators);
-	if (ret)
-		return ret;
-
-	ret = devm_add_action_or_reset(dev, bmg160_disable_regulators, data);
-	if (ret)
-		return ret;
-
 	ret = iio_read_mount_matrix(dev, &data->orientation);
 	if (ret)
 		return ret;
author maintainer
#40 lore raw

Re: [PATCH v3 08/14] iio: bmg160_core: Simplify using devm_regulator_*get_enable()

Quoted 6 lines · Jonathan Cameron
Quoted 3 lines · On Fri, 19 Aug 2022 22:19:31 +0300 Matti Vaittinen <[email protected]>…

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

Quoted 6 lines
Quoted 1 line

Applied with tweaks:
- patch title includes gyro:
- ordering as Andy suggested
- spaces after { and before }

Thanks Jonathan for saving me from respin the series :) Much apprecited!

Yours
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
author maintainer
#41 lore raw

PATCH v3 9/14 iio: st_lsm6dsx: Simplify using devm_regulator_*get_enable()

In reply to Matti Vaittinen

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

A functional change (which seems like a bugfix) is that if regulator_bulk_get fails, the enable is not attempted.

---
v2 => v3
Split to own patch.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  2 --
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 30 ++++----------------
 2 files changed, 5 insertions(+), 27 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +0 -2
index a86dd29a4738..03238c64c777 100644
@@ -372,7 +372,6 @@ struct st_lsm6dsx_sensor {
  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
  * @dev: Pointer to instance of struct device (I2C or SPI).
  * @regmap: Register map of the device.
- * @regulators: VDD/VDDIO voltage regulators.
  * @irq: Device interrupt line (I2C or SPI).
  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
  * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
@@ -395,7 +394,6 @@ struct st_lsm6dsx_sensor {
 struct st_lsm6dsx_hw {
 	struct device *dev;
 	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
 	int irq;
 
 	struct mutex fifo_lock;
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +5 -25
index b5e4a4113652..c328d1a36d34 100644
@@ -2172,36 +2172,20 @@ static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
 
 static int st_lsm6dsx_init_regulators(struct device *dev)
 {
-	struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
 	int err;
-
 	/* vdd-vddio power regulators */
-	hw->regulators[0].supply = "vdd";
-	hw->regulators[1].supply = "vddio";
-	err = devm_regulator_bulk_get(dev, ARRAY_SIZE(hw->regulators),
-				      hw->regulators);
-	if (err)
-		return dev_err_probe(dev, err, "failed to get regulators\n");
+	static const char * const regulators[] = {"vdd", "vddio"};
 
-	err = regulator_bulk_enable(ARRAY_SIZE(hw->regulators),
-				    hw->regulators);
-	if (err) {
-		dev_err(dev, "failed to enable regulators: %d\n", err);
-		return err;
-	}
+	err = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators),
+					     regulators);
+	if (err)
+		return dev_err_probe(dev, err, "failed to enable regulators\n");
 
 	msleep(50);
 
 	return 0;
 }
 
-static void st_lsm6dsx_chip_uninit(void *data)
-{
-	struct st_lsm6dsx_hw *hw = data;
-
-	regulator_bulk_disable(ARRAY_SIZE(hw->regulators), hw->regulators);
-}
-
 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
 		     struct regmap *regmap)
 {
@@ -2225,10 +2209,6 @@ int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
 	if (err)
 		return err;
 
-	err = devm_add_action_or_reset(dev, st_lsm6dsx_chip_uninit, hw);
-	if (err)
-		return err;
-
 	hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL);
 	if (!hw->buff)
 		return -ENOMEM;
Signature · 14 lines
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 =] 
maintainer
#42 lore raw

Re: [PATCH v3 09/14] iio: st_lsm6dsx: Simplify using devm_regulator_*get_enable()

Quoted 7 lines · On Fri, 19 Aug 2022 22:19:46 +0300 Matti Vaittinen <[email protected]>…

Use devm_regulator_bulk_get_enable() instead of open coded bulk-get, bulk-enable, add-action-to-disable-at-detach - pattern.

A functional change (which seems like a bugfix) is that if regulator_bulk_get fails, the enable is not attempted.

Applied with similar ordering, patch naming and spacing tweaks to earlier patches.

Quoted 86 lines

---
v2 => v3
Split to own patch.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  2 --
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 30 ++++----------------
 2 files changed, 5 insertions(+), 27 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +0 -2
index a86dd29a4738..03238c64c777 100644
@@ -372,7 +372,6 @@ struct st_lsm6dsx_sensor {
  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
  * @dev: Pointer to instance of struct device (I2C or SPI).
  * @regmap: Register map of the device.
- * @regulators: VDD/VDDIO voltage regulators.
  * @irq: Device interrupt line (I2C or SPI).
  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
  * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
@@ -395,7 +394,6 @@ struct st_lsm6dsx_sensor {
 struct st_lsm6dsx_hw {
 	struct device *dev;
 	struct regmap *regmap;
-	struct regulator_bulk_data regulators[2];
 	int irq;
 
 	struct mutex fifo_lock;
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +5 -25
index b5e4a4113652..c328d1a36d34 100644
@@ -2172,36 +2172,20 @@ static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
 
 static int st_lsm6dsx_init_regulators(struct device *dev)
 {
-	struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
 	int err;
-
 	/* vdd-vddio power regulators */
-	hw->regulators[0].supply = "vdd";
-	hw->regulators[1].supply = "vddio";
-	err = devm_regulator_bulk_get(dev, ARRAY_SIZE(hw->regulators),
-				      hw->regulators);
-	if (err)
-		return dev_err_probe(dev, err, "failed to get regulators\n");
+	static const char * const regulators[] = {"vdd", "vddio"};
 
-	err = regulator_bulk_enable(ARRAY_SIZE(hw->regulators),
-				    hw->regulators);
-	if (err) {
-		dev_err(dev, "failed to enable regulators: %d\n", err);
-		return err;
-	}
+	err = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators),
+					     regulators);
+	if (err)
+		return dev_err_probe(dev, err, "failed to enable regulators\n");
 
 	msleep(50);
 
 	return 0;
 }
 
-static void st_lsm6dsx_chip_uninit(void *data)
-{
-	struct st_lsm6dsx_hw *hw = data;
-
-	regulator_bulk_disable(ARRAY_SIZE(hw->regulators), hw->regulators);
-}
-
 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
 		     struct regmap *regmap)
 {
@@ -2225,10 +2209,6 @@ int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
 	if (err)
 		return err;
 
-	err = devm_add_action_or_reset(dev, st_lsm6dsx_chip_uninit, hw);
-	if (err)
-		return err;
-
 	hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL);
 	if (!hw->buff)
 		return -ENOMEM;
author maintainer
#43 lore raw

PATCH v3 10/14 iio: ad7476: simplify using devm_regulator_get_enable()

In reply to Matti Vaittinen

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

---
v2 => v3:
New patch

---
 drivers/iio/adc/ad7476.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/iio/adc/ad7476.c +1 -10
index 94776f696290..80aebed47d1f 100644
@@ -368,16 +368,7 @@ static int ad7476_probe(struct spi_device *spi)
 	}
 
 	if (st->chip_info->has_vdrive) {
-		reg = devm_regulator_get(&spi->dev, "vdrive");
-		if (IS_ERR(reg))
-			return PTR_ERR(reg);
-
-		ret = regulator_enable(reg);
-		if (ret)
-			return ret;
-
-		ret = devm_add_action_or_reset(&spi->dev, ad7476_reg_disable,
-					       reg);
+		ret = devm_regulator_get_enable(&spi->dev, "vdrive");
 		if (ret)
 			return ret;
 	}
Signature · 14 lines
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 =] 
maintainer Acked-by on v3 10/14
#44 lore raw

RE: [PATCH v3 10/14] iio: ad7476: simplify using devm_regulator_get_enable()

Quoted 16 lines

-----Original Message-----
From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:20 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 10/14] iio: ad7476: simplify using devm_regulator_get_enable()

[External]

Quoted 7 lines

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

---

maintainer Applied
#45 lore raw

Re: [PATCH v3 10/14] iio: ad7476: simplify using devm_regulator_get_enable()

Quoted 18 lines · Nuno
Quoted 18 lines

-----Original Message-----
From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:20 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 10/14] iio: ad7476: simplify using devm_regulator_get_enable()

[External]

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

Quoted 5 lines
Quoted 3 lines

---

Applied

author maintainer
#46 lore raw

PATCH v3 11/14 iio: ad7606: simplify using devm_regulator_get_enable()

In reply to Matti Vaittinen

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

---
v2 => v3:
New patch

---
 drivers/iio/adc/ad7606.c | 22 +++-------------------
 drivers/iio/adc/ad7606.h |  1 -
 2 files changed, 3 insertions(+), 20 deletions(-)
drivers/iio/adc/ad7606.c +3 -19
index ba24f99523e0..dd6b603f65ea 100644
@@ -557,13 +557,6 @@ static const struct iio_trigger_ops ad7606_trigger_ops = {
 	.validate_device = iio_trigger_validate_own_device,
 };
 
-static void ad7606_regulator_disable(void *data)
-{
-	struct ad7606_state *st = data;
-
-	regulator_disable(st->reg);
-}
-
 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
 		 const char *name, unsigned int id,
 		 const struct ad7606_bus_ops *bops)
@@ -589,19 +582,10 @@ int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
 	st->scale_avail = ad7606_scale_avail;
 	st->num_scales = ARRAY_SIZE(ad7606_scale_avail);
 
-	st->reg = devm_regulator_get(dev, "avcc");
-	if (IS_ERR(st->reg))
-		return PTR_ERR(st->reg);
-
-	ret = regulator_enable(st->reg);
-	if (ret) {
-		dev_err(dev, "Failed to enable specified AVcc supply\n");
-		return ret;
-	}
-
-	ret = devm_add_action_or_reset(dev, ad7606_regulator_disable, st);
+	ret = devm_regulator_get_enable(dev, "avcc");
 	if (ret)
-		return ret;
+		return dev_err_probe(dev, ret,
+				     "Failed to enable specified AVcc supply\n");
 
 	st->chip_info = &ad7606_chip_info_tbl[id];
 
drivers/iio/adc/ad7606.h +0 -1
index 2dc4f599f9df..924a2d1a6d94 100644
@@ -92,7 +92,6 @@ struct ad7606_chip_info {
 struct ad7606_state {
 	struct device			*dev;
 	const struct ad7606_chip_info	*chip_info;
-	struct regulator		*reg;
 	const struct ad7606_bus_ops	*bops;
 	unsigned int			range[16];
 	unsigned int			oversampling;
Signature · 14 lines
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 =] 
maintainer Acked-by on v3 11/14
#47 lore raw

RE: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

Quoted 19 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:20 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 3 lines

---

The commit message could state that while doing the change, dev_err_probe() was also introduced. Bah, anyways:

author maintainer
#48 lore raw

Re: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

Thanks for the review(s) Nuno!

Quoted 19 lines · Nuno
Quoted 19 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:20 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 8 lines
Quoted 3 lines

---

The commit message could state that while doing the change, dev_err_probe() was also introduced. Bah, anyways:

Good point. I have few other changes to the series pending - and I probably need to rebase/respin when -rc1 is out (and dependency patches are merged from Mark's tree) => I may as well alter the commit message.

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#49 lore raw

Re: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

Quoted 26 lines · On Tue, 30 Aug 2022 15:54:07 +0300 Matti Vaittinen <[email protected]>…

Thanks for the review(s) Nuno!

Quoted 23 lines · Nuno
Quoted 22 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:20 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

---

Quoted 9 lines
Quoted 4 lines

The commit message could state that while doing the change, dev_err_probe() was also introduced. Bah, anyways:

Good point. I have few other changes to the series pending - and I probably need to rebase/respin when -rc1 is out (and dependency patches are merged from Mark's tree) => I may as well alter the commit message.

I tweaked it and applied.

Not I'm grabbing these early because I forgot you'd sent them and found myself writing the same patches. Memory of a goldfish :)

Jonathan

Quoted 3 lines

Yours,
-- Matti

maintainer
#50 lore raw

Re: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

Quoted 34 lines · On Sun, 16 Oct 2022 17:15:29 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 31 lines · On Tue, 30 Aug 2022 15:54:07 +0300 Matti Vaittinen <[email protected]>…

Thanks for the review(s) Nuno!

Quoted 27 lines · Nuno
Quoted 19 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:20 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 3 lines

---

The commit message could state that while doing the change, dev_err_probe() was also introduced. Bah, anyways:

Quoted 8 lines
Quoted 4 lines

Good point. I have few other changes to the series pending - and I probably need to rebase/respin when -rc1 is out (and dependency patches are merged from Mark's tree) => I may as well alter the commit message.

I tweaked it and applied.

Not I'm grabbing these early because I forgot you'd sent them and found myself writing the same patches. Memory of a goldfish :)

Tweaked a little more - you missed that the struct in the header had kernel-doc for the struct regulator *. Dropped that.

Quoted 7 lines

Jonathan

Quoted 3 lines

Yours,
-- Matti

author maintainer
#51 lore raw

Re: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

Quoted 41 lines · Jonathan Cameron
Quoted 38 lines · On Sun, 16 Oct 2022 17:15:29 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 35 lines · On Tue, 30 Aug 2022 15:54:07 +0300 Matti Vaittinen <[email protected]>…

Thanks for the review(s) Nuno!

Quoted 19 lines · Nuno
Quoted 19 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:20 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 11/14] iio: ad7606: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 8 lines
Quoted 3 lines

---

The commit message could state that while doing the change, dev_err_probe() was also introduced. Bah, anyways:

Good point. I have few other changes to the series pending - and I probably need to rebase/respin when -rc1 is out (and dependency patches are merged from Mark's tree) => I may as well alter the commit message.

Quoted 7 lines
Quoted 4 lines

I tweaked it and applied.

Not I'm grabbing these early because I forgot you'd sent them and found myself writing the same patches. Memory of a goldfish :)

Tweaked a little more - you missed that the struct in the header had kernel-doc for the struct regulator *. Dropped that.

Thanks a bunch!

Yours,
Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
author maintainer
#52 lore raw

PATCH v3 12/14 iio: max1241: simplify using devm_regulator_get_enable()

In reply to Matti Vaittinen

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

---
v2 => v3:
New patch

---
 drivers/iio/adc/max1241.c | 28 +++-------------------------
 1 file changed, 3 insertions(+), 25 deletions(-)
drivers/iio/adc/max1241.c +3 -25
index a815ad1f6913..500bb09ab19b 100644
@@ -22,7 +22,6 @@ enum max1241_id {
 struct max1241 {
 	struct spi_device *spi;
 	struct mutex lock;
-	struct regulator *vdd;
 	struct regulator *vref;
 	struct gpio_desc *shutdown;
 
@@ -110,17 +109,6 @@ static const struct iio_info max1241_info = {
 	.read_raw = max1241_read_raw,
 };
 
-static void max1241_disable_vdd_action(void *data)
-{
-	struct max1241 *adc = data;
-	struct device *dev = &adc->spi->dev;
-	int err;
-
-	err = regulator_disable(adc->vdd);
-	if (err)
-		dev_err(dev, "could not disable vdd regulator.\n");
-}
-
 static void max1241_disable_vref_action(void *data)
 {
 	struct max1241 *adc = data;
@@ -147,20 +135,10 @@ static int max1241_probe(struct spi_device *spi)
 	adc->spi = spi;
 	mutex_init(&adc->lock);
 
-	adc->vdd = devm_regulator_get(dev, "vdd");
-	if (IS_ERR(adc->vdd))
-		return dev_err_probe(dev, PTR_ERR(adc->vdd),
-				     "failed to get vdd regulator\n");
-
-	ret = regulator_enable(adc->vdd);
+	ret = devm_regulator_get_enable(dev, "vdd");
 	if (ret)
-		return ret;
-
-	ret = devm_add_action_or_reset(dev, max1241_disable_vdd_action, adc);
-	if (ret) {
-		dev_err(dev, "could not set up vdd regulator cleanup action\n");
-		return ret;
-	}
+		return dev_err_probe(dev, ret,
+				     "failed to get/enable vdd regulator\n");
 
 	adc->vref = devm_regulator_get(dev, "vref");
 	if (IS_ERR(adc->vref))
Signature · 14 lines
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 =] 
Acked-by on v3 12/14
#53 lore raw

Re: [PATCH v3 12/14] iio: max1241: simplify using devm_regulator_get_enable()

Heya,

I don't have the hardware at hand (long story, lots of smoke), so all I can do for this patch at the moment is:

That being said, IIRC the MAX1241 has no special requirements in this regard, so if other SPI devices work, this one ought to work as well. If anyone's concerned, I can test it, but probably not sooner than a week or so :-(.

FWIW I really like this, the straightforward regulator boilerplate always felt a tad wrong.

All the best,
Alex

Quoted 69 lines · Matti Vaittinen

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

---
v2 => v3:
New patch

---
  drivers/iio/adc/max1241.c | 28 +++-------------------------
  1 file changed, 3 insertions(+), 25 deletions(-)
drivers/iio/adc/max1241.c +3 -25
index a815ad1f6913..500bb09ab19b 100644
@@ -22,7 +22,6 @@ enum max1241_id {
  struct max1241 {
  	struct spi_device *spi;
  	struct mutex lock;
-	struct regulator *vdd;
  	struct regulator *vref;
  	struct gpio_desc *shutdown;
 
@@ -110,17 +109,6 @@ static const struct iio_info max1241_info = {
  	.read_raw = max1241_read_raw,
  };
 
-static void max1241_disable_vdd_action(void *data)
-{
-	struct max1241 *adc = data;
-	struct device *dev = &adc->spi->dev;
-	int err;
-
-	err = regulator_disable(adc->vdd);
-	if (err)
-		dev_err(dev, "could not disable vdd regulator.\n");
-}
-
  static void max1241_disable_vref_action(void *data)
  {
  	struct max1241 *adc = data;
@@ -147,20 +135,10 @@ static int max1241_probe(struct spi_device *spi)
  	adc->spi = spi;
  	mutex_init(&adc->lock);
 
-	adc->vdd = devm_regulator_get(dev, "vdd");
-	if (IS_ERR(adc->vdd))
-		return dev_err_probe(dev, PTR_ERR(adc->vdd),
-				     "failed to get vdd regulator\n");
-
-	ret = regulator_enable(adc->vdd);
+	ret = devm_regulator_get_enable(dev, "vdd");
  	if (ret)
-		return ret;
-
-	ret = devm_add_action_or_reset(dev, max1241_disable_vdd_action, adc);
-	if (ret) {
-		dev_err(dev, "could not set up vdd regulator cleanup action\n");
-		return ret;
-	}
+		return dev_err_probe(dev, ret,
+				     "failed to get/enable vdd regulator\n");
 
  	adc->vref = devm_regulator_get(dev, "vref");
  	if (IS_ERR(adc->vref))
maintainer
#54 lore raw

Re: [PATCH v3 12/14] iio: max1241: simplify using devm_regulator_get_enable()

Quoted 12 lines · On Fri, 19 Aug 2022 22:58:45 +0300 Alexandru Lazar <[email protected]> wrote:

Heya,

I don't have the hardware at hand (long story, lots of smoke), so all I can do for this patch at the moment is:

That being said, IIRC the MAX1241 has no special requirements in this regard, so if other SPI devices work, this one ought to work as well. If anyone's concerned, I can test it, but probably not sooner than a week or so :-(.

Quoted 5 lines

FWIW I really like this, the straightforward regulator boilerplate always felt a tad wrong.

All the best,
Alex

Now the dependencies are upstream... Applied to the togreg branch of iio.git and pushed out as testing so that 0-day can play with it and because I plan to rebase that tree on rc1 sometime shortly.

Jonathan

Quoted 71 lines · Matti Vaittinen
Quoted 69 lines

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

---
v2 => v3:
New patch

---
  drivers/iio/adc/max1241.c | 28 +++-------------------------
  1 file changed, 3 insertions(+), 25 deletions(-)
drivers/iio/adc/max1241.c +3 -25
index a815ad1f6913..500bb09ab19b 100644
@@ -22,7 +22,6 @@ enum max1241_id {
  struct max1241 {
  	struct spi_device *spi;
  	struct mutex lock;
-	struct regulator *vdd;
  	struct regulator *vref;
  	struct gpio_desc *shutdown;
 
@@ -110,17 +109,6 @@ static const struct iio_info max1241_info = {
  	.read_raw = max1241_read_raw,
  };
 
-static void max1241_disable_vdd_action(void *data)
-{
-	struct max1241 *adc = data;
-	struct device *dev = &adc->spi->dev;
-	int err;
-
-	err = regulator_disable(adc->vdd);
-	if (err)
-		dev_err(dev, "could not disable vdd regulator.\n");
-}
-
  static void max1241_disable_vref_action(void *data)
  {
  	struct max1241 *adc = data;
@@ -147,20 +135,10 @@ static int max1241_probe(struct spi_device *spi)
  	adc->spi = spi;
  	mutex_init(&adc->lock);
 
-	adc->vdd = devm_regulator_get(dev, "vdd");
-	if (IS_ERR(adc->vdd))
-		return dev_err_probe(dev, PTR_ERR(adc->vdd),
-				     "failed to get vdd regulator\n");
-
-	ret = regulator_enable(adc->vdd);
+	ret = devm_regulator_get_enable(dev, "vdd");
  	if (ret)
-		return ret;
-
-	ret = devm_add_action_or_reset(dev, max1241_disable_vdd_action, adc);
-	if (ret) {
-		dev_err(dev, "could not set up vdd regulator cleanup action\n");
-		return ret;
-	}
+		return dev_err_probe(dev, ret,
+				     "failed to get/enable vdd regulator\n");
 
  	adc->vref = devm_regulator_get(dev, "vref");
  	if (IS_ERR(adc->vref))  
author maintainer
#55 lore raw

PATCH v3 13/14 iio: max1363: simplify using devm_regulator_get_enable()

In reply to Matti Vaittinen

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

---
v2 => v3:
New patch

---
 drivers/iio/adc/max1363.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/iio/adc/max1363.c +1 -10
index eef55ed4814a..d900e29d6cb7 100644
@@ -169,7 +169,6 @@ struct max1363_state {
 	const struct max1363_chip_info	*chip_info;
 	const struct max1363_mode	*current_mode;
 	u32				requestedmask;
-	struct regulator		*reg;
 	struct mutex			lock;
 
 	/* Using monitor modes and buffer at the same time is
@@ -1603,15 +1602,7 @@ static int max1363_probe(struct i2c_client *client,
 	st = iio_priv(indio_dev);
 
 	mutex_init(&st->lock);
-	st->reg = devm_regulator_get(&client->dev, "vcc");
-	if (IS_ERR(st->reg))
-		return PTR_ERR(st->reg);
-
-	ret = regulator_enable(st->reg);
-	if (ret)
-		return ret;
-
-	ret = devm_add_action_or_reset(&client->dev, max1363_reg_disable, st->reg);
+	ret = devm_regulator_get_enable(&client->dev, "vcc");
 	if (ret)
 		return ret;
 
Signature · 14 lines
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 =] 
maintainer Acked-by on v3 13/14
#56 lore raw

RE: [PATCH v3 13/14] iio: max1363: simplify using devm_regulator_get_enable()

Quoted 22 lines

-----Original Message-----
From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:21 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Matti Vaittinen <[email protected]>; Alexandru Ardelean <[email protected]>; linux- [email protected]; [email protected]
Subject: [PATCH v3 13/14] iio: max1363: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 3 lines

---

maintainer Applied
#57 lore raw

Re: [PATCH v3 13/14] iio: max1363: simplify using devm_regulator_get_enable()

Quoted 20 lines · Nuno
Quoted 20 lines

-----Original Message-----
From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:21 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Matti Vaittinen <[email protected]>; Alexandru Ardelean <[email protected]>; linux- [email protected]; [email protected]
Subject: [PATCH v3 13/14] iio: max1363: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 5 lines
Quoted 3 lines

---

Applied.

maintainer
#58 lore raw

Re: [PATCH v3 13/14] iio: max1363: simplify using devm_regulator_get_enable()

Quoted 23 lines · On Sun, 16 Oct 2022 17:18:38 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 20 lines · Nuno
Quoted 20 lines

-----Original Message-----
From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:21 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Matti Vaittinen <[email protected]>; Alexandru Ardelean <[email protected]>; linux- [email protected]; [email protected]
Subject: [PATCH v3 13/14] iio: max1363: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

---

Applied.

This one was missing cleaning up the docs as well. fixed up.

author maintainer
#59 lore raw

PATCH v3 14/14 iio: hmc425a: simplify using devm_regulator_get_enable()

In reply to Matti Vaittinen

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

---
v2 => v3:
New patch

---
 drivers/iio/amplifiers/hmc425a.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
drivers/iio/amplifiers/hmc425a.c +1 -16
index ce80e0c916f4..108f0f1685ef 100644
@@ -34,7 +34,6 @@ struct hmc425a_chip_info {
 };
 
 struct hmc425a_state {
-	struct	regulator *reg;
 	struct	mutex lock; /* protect sensor state */
 	struct	hmc425a_chip_info *chip_info;
 	struct	gpio_descs *gpios;
@@ -162,13 +161,6 @@ static const struct of_device_id hmc425a_of_match[] = {
 };
 MODULE_DEVICE_TABLE(of, hmc425a_of_match);
 
-static void hmc425a_reg_disable(void *data)
-{
-	struct hmc425a_state *st = data;
-
-	regulator_disable(st->reg);
-}
-
 static struct hmc425a_chip_info hmc425a_chip_info_tbl[] = {
 	[ID_HMC425A] = {
 		.name = "hmc425a",
@@ -211,14 +203,7 @@ static int hmc425a_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
-	st->reg = devm_regulator_get(&pdev->dev, "vcc-supply");
-	if (IS_ERR(st->reg))
-		return PTR_ERR(st->reg);
-
-	ret = regulator_enable(st->reg);
-	if (ret)
-		return ret;
-	ret = devm_add_action_or_reset(&pdev->dev, hmc425a_reg_disable, st);
+	ret = devm_regulator_get_enable(&pdev->dev, "vcc-supply");
 	if (ret)
 		return ret;
 
Signature · 14 lines
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 =] 
maintainer Acked-by on v3 14/14
#60 lore raw

RE: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

Quoted 20 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:21 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 3 lines

---

(I see that in this patch you are not using dev_err_probe(). Maybe some consistency in the series and where applicable would be appropriate :))

author maintainer
#61 lore raw

Re: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

Quoted 20 lines · Nuno
Quoted 20 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:21 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

[External]

Drop open-coded pattern: 'devm_regulator_get(), regulator_enable(), add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the regulator. This simplifies code and makes it less tempting to add manual control for the regulator which is also controlled by devm.

Quoted 8 lines
Quoted 3 lines

---

(I see that in this patch you are not using dev_err_probe(). Maybe some consistency in the series and where applicable would be appropriate :))

I don't think the driver did originally print an error if regulator get or enable failed. I didn't want to add any new prints - just converted the existing ones to use dev_err_probe(). I believe adding new prints would've been somewhat unrelated change :)

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#62 lore raw

RE: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

Quoted 47 lines

-----Original Message-----
From: Matti Vaittinen <[email protected]>
Sent: Tuesday, August 30, 2022 3:00 PM
To: Sa, Nuno <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

[External]

Quoted 12 lines · Nuno
Quoted 12 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:21 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

Quoted 3 lines
Quoted 3 lines

[External]

Drop open-coded pattern: 'devm_regulator_get(),

regulator_enable(),

Quoted 2 lines
Quoted 2 lines

add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the

regulator.

Quoted 1 line
Quoted 1 line

This simplifies code and makes it less tempting to add manual

control

Quoted 9 lines
Quoted 5 lines

for the regulator which is also controlled by devm.

---

(I see that in this patch you are not using dev_err_probe(). Maybe

Quoted 9 lines

some

Quoted 1 line

consistency in the series and where applicable would be appropriate

:))

I don't think the driver did originally print an error if regulator get or enable failed. I didn't want to add any new prints - just converted the existing ones to use dev_err_probe(). I believe adding new prints would've been somewhat unrelated change :)

Ahh that makes sense. I failed to see the print that you were replacing in the ad7606 patch...

- Nuno Sá

maintainer Applied
#63 lore raw

Re: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

Quoted 49 lines · Nuno
Quoted 49 lines

-----Original Message-----
From: Matti Vaittinen <[email protected]>
Sent: Tuesday, August 30, 2022 3:00 PM
To: Sa, Nuno <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

[External]

Quoted 12 lines · Nuno
Quoted 12 lines

From: Matti Vaittinen <[email protected]>
Sent: Friday, August 19, 2022 9:21 PM
To: Matti Vaittinen <[email protected]>; Matti Vaittinen <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; [email protected]; linux- [email protected]
Subject: [PATCH v3 14/14] iio: hmc425a: simplify using devm_regulator_get_enable()

Quoted 3 lines
Quoted 3 lines

[External]

Drop open-coded pattern: 'devm_regulator_get(),

regulator_enable(),

Quoted 2 lines
Quoted 2 lines

add_action_or_reset(regulator_disable)' and use the devm_regulator_get_enable() and drop the pointer to the

regulator.

Quoted 1 line
Quoted 1 line

This simplifies code and makes it less tempting to add manual

control

Quoted 9 lines
Quoted 5 lines

for the regulator which is also controlled by devm.

---

(I see that in this patch you are not using dev_err_probe(). Maybe

some

Quoted 1 line

consistency in the series and where applicable would be appropriate

:))

Quoted 9 lines
Quoted 5 lines

I don't think the driver did originally print an error if regulator get or enable failed. I didn't want to add any new prints - just converted the existing ones to use dev_err_probe(). I believe adding new prints would've been somewhat unrelated change :)

Ahh that makes sense. I failed to see the print that you were replacing in the ad7606 patch...

Applied to the togreg branch of iio.git though for now that's just pushed out as testing.

As I mentioned on an earlier patch I forgot these existed and was about to duplicate some of the work.. Anyhow, we only overlapped on a few patches so I'll send the rest of my set out shortly.

There are probably other cases still in IIO but they are fiddly to grep for!

Jonathan

Quoted 1 line

- Nuno Sá

#64 lore raw
In reply to Matti Vaittinen
Quoted 7 lines · Matti Vaittinen

Use devm helpers for regulator get and enable

NOTE: The series depends on commit
ee94aff2628b ("Devm helpers for regulator get and enable") which currently sits in Mark's regulator/for-next

Quoted 7 lines

A few* drivers seem to pattern demonstrated by pseudocode:

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

devm helpers for this pattern were added to remove bunch of code from

remove a bunch

Quoted 8 lines

drivers. Typically 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.

duplicated

Quoted 1 line

This series reowrks a few drivers. There is still plenty of fish in the

reworks

Quoted 5 lines

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!).

...

Quoted 5 lines

docs: devres: regulator: Add new get_enable functions to devres.rst
clk: cdce925: simplify using devm_regulator_get_enable()
gpu: drm: simplify drivers using devm_regulator_*get_enable*()
hwmon: lm90: simplify using devm_regulator_get_enable()
hwmon: adm1177: simplify using devm_regulator_get_enable()

hwmon uses a different pattern for the Subject line.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#65 lore raw

Re: (subset) [PATCH v3 00/14] Use devm helpers for regulator get and enable

In reply to Matti Vaittinen
Quoted 9 lines · Matti Vaittinen

Use devm helpers for regulator get and enable

NOTE: The series depends on commit
ee94aff2628b ("Devm helpers for regulator get and enable") which currently sits in Mark's regulator/for-next

A few* drivers seem to pattern demonstrated by pseudocode:

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next

Thanks!

[01/14] docs: devres: regulator: Add new get_enable functions to devres.rst

        commit: 9048b9912f767ea9609e520678def446af804771

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark