diff mbox series

[RFC,3/7] hwmon: (da9052) Use devm_regulator_get_enable_get_voltage()

Message ID 20240327-regulator-get-enable-get-votlage-v1-3-5f4517faa059@baylibre.com (mailing list archive)
State Changes Requested
Headers show
Series regulator: new APIs for voltage reference supplies | expand

Commit Message

David Lechner March 27, 2024, 11:18 p.m. UTC
We can reduce boilerplate code by using
devm_regulator_get_enable_get_voltage().

Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 drivers/hwmon/da9052-hwmon.c | 33 +++++++--------------------------
 1 file changed, 7 insertions(+), 26 deletions(-)

Comments

Jonathan Cameron March 28, 2024, 2:20 p.m. UTC | #1
On Wed, 27 Mar 2024 18:18:52 -0500
David Lechner <dlechner@baylibre.com> wrote:

> We can reduce boilerplate code by using
> devm_regulator_get_enable_get_voltage().
> 
> Signed-off-by: David Lechner <dlechner@baylibre.com>

A few comments inline, but nothing substantial.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> ---
>  drivers/hwmon/da9052-hwmon.c | 33 +++++++--------------------------
>  1 file changed, 7 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/hwmon/da9052-hwmon.c b/drivers/hwmon/da9052-hwmon.c
> index 2bd7ae8100d7..70e7bc72e980 100644
> --- a/drivers/hwmon/da9052-hwmon.c
> +++ b/drivers/hwmon/da9052-hwmon.c
> @@ -26,7 +26,6 @@ struct da9052_hwmon {
>  	struct mutex		hwmon_lock;
>  	bool			tsi_as_adc;
>  	int			tsiref_mv;
> -	struct regulator	*tsiref;
>  	struct completion	tsidone;
>  };
>  
> @@ -414,32 +413,19 @@ static int da9052_hwmon_probe(struct platform_device *pdev)
>  		device_property_read_bool(pdev->dev.parent, "dlg,tsi-as-adc");
>  
>  	if (hwmon->tsi_as_adc) {
> -		hwmon->tsiref = devm_regulator_get(pdev->dev.parent, "tsiref");
> -		if (IS_ERR(hwmon->tsiref)) {
> -			err = PTR_ERR(hwmon->tsiref);
> -			dev_err(&pdev->dev, "failed to get tsiref: %d", err);
> +		err = devm_regulator_get_enable_get_voltage(pdev->dev.parent,
> +							    "tsiref");
> +		if (err < 0)
>  			return err;
> -		}
> -
> -		err = regulator_enable(hwmon->tsiref);
> -		if (err)
> -			return err;
> -
> -		hwmon->tsiref_mv = regulator_get_voltage(hwmon->tsiref);
> -		if (hwmon->tsiref_mv < 0) {
> -			err = hwmon->tsiref_mv;
> -			goto exit_regulator;
> -		}
>  
>  		/* convert from microvolt (DT) to millivolt (hwmon) */
> -		hwmon->tsiref_mv /= 1000;
> +		hwmon->tsiref_mv = err / 1000;
>

Using a variable called err for a good value is a bit ugly but fair enough if that
is precedence in this driver.

>  }
> @@ -483,10 +466,8 @@ static void da9052_hwmon_remove(struct platform_device *pdev)
>  {
>  	struct da9052_hwmon *hwmon = platform_get_drvdata(pdev);
>  
> -	if (hwmon->tsi_as_adc) {
> +	if (hwmon->tsi_as_adc)
>  		da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
Superficially looks like devm_da9052_request_irq could be added that
uses devm_request_threaded_irq() to allow dropping this remaining handling.

Thanks,

Jonathan

> -		regulator_disable(hwmon->tsiref);
> -	}
>  }
>  
>  static struct platform_driver da9052_hwmon_driver = {
>
Guenter Roeck March 28, 2024, 3:20 p.m. UTC | #2
On 3/28/24 07:20, Jonathan Cameron wrote:
> On Wed, 27 Mar 2024 18:18:52 -0500
> David Lechner <dlechner@baylibre.com> wrote:
> 
>> We can reduce boilerplate code by using
>> devm_regulator_get_enable_get_voltage().
>>
>> Signed-off-by: David Lechner <dlechner@baylibre.com>
> 
> A few comments inline, but nothing substantial.
> 
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>> ---
>>   drivers/hwmon/da9052-hwmon.c | 33 +++++++--------------------------
>>   1 file changed, 7 insertions(+), 26 deletions(-)
>>
>> diff --git a/drivers/hwmon/da9052-hwmon.c b/drivers/hwmon/da9052-hwmon.c
>> index 2bd7ae8100d7..70e7bc72e980 100644
>> --- a/drivers/hwmon/da9052-hwmon.c
>> +++ b/drivers/hwmon/da9052-hwmon.c
>> @@ -26,7 +26,6 @@ struct da9052_hwmon {
>>   	struct mutex		hwmon_lock;
>>   	bool			tsi_as_adc;
>>   	int			tsiref_mv;
>> -	struct regulator	*tsiref;
>>   	struct completion	tsidone;
>>   };
>>   
>> @@ -414,32 +413,19 @@ static int da9052_hwmon_probe(struct platform_device *pdev)
>>   		device_property_read_bool(pdev->dev.parent, "dlg,tsi-as-adc");
>>   
>>   	if (hwmon->tsi_as_adc) {
>> -		hwmon->tsiref = devm_regulator_get(pdev->dev.parent, "tsiref");
>> -		if (IS_ERR(hwmon->tsiref)) {
>> -			err = PTR_ERR(hwmon->tsiref);
>> -			dev_err(&pdev->dev, "failed to get tsiref: %d", err);
>> +		err = devm_regulator_get_enable_get_voltage(pdev->dev.parent,
>> +							    "tsiref");
>> +		if (err < 0)
>>   			return err;
>> -		}
>> -
>> -		err = regulator_enable(hwmon->tsiref);
>> -		if (err)
>> -			return err;
>> -
>> -		hwmon->tsiref_mv = regulator_get_voltage(hwmon->tsiref);
>> -		if (hwmon->tsiref_mv < 0) {
>> -			err = hwmon->tsiref_mv;
>> -			goto exit_regulator;
>> -		}
>>   
>>   		/* convert from microvolt (DT) to millivolt (hwmon) */
>> -		hwmon->tsiref_mv /= 1000;
>> +		hwmon->tsiref_mv = err / 1000;
>>
> 
> Using a variable called err for a good value is a bit ugly but fair enough if that
> is precedence in this driver.
> 

It isn't. The existing code assigns the return value from regulator_get_voltage()
to hwmon->tsiref_mv and then evaluates it. I would not oppose introducing a variable
such as tsiref_uv, but not the misuse of 'err'. I am not going to accept the code
as suggested. It is bad style, and it would invite others to use it as precedent
when trying to introduce similar code.

>>   }
>> @@ -483,10 +466,8 @@ static void da9052_hwmon_remove(struct platform_device *pdev)
>>   {
>>   	struct da9052_hwmon *hwmon = platform_get_drvdata(pdev);
>>   
>> -	if (hwmon->tsi_as_adc) {
>> +	if (hwmon->tsi_as_adc)
>>   		da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
> Superficially looks like devm_da9052_request_irq could be added that
> uses devm_request_threaded_irq() to allow dropping this remaining handling.
> 

That should be a separate series of patches. A local solution might be
to use devm_add_action_or_reset(), but that should also be a separate patch.

Thanks,
Guenter
Jonathan Cameron March 28, 2024, 3:53 p.m. UTC | #3
On Thu, 28 Mar 2024 08:20:00 -0700
Guenter Roeck <linux@roeck-us.net> wrote:

> On 3/28/24 07:20, Jonathan Cameron wrote:
> > On Wed, 27 Mar 2024 18:18:52 -0500
> > David Lechner <dlechner@baylibre.com> wrote:
> >   
> >> We can reduce boilerplate code by using
> >> devm_regulator_get_enable_get_voltage().
> >>
> >> Signed-off-by: David Lechner <dlechner@baylibre.com>  
> > 
> > A few comments inline, but nothing substantial.
> > 
> > Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>  
> >> ---
> >>   drivers/hwmon/da9052-hwmon.c | 33 +++++++--------------------------
> >>   1 file changed, 7 insertions(+), 26 deletions(-)
> >>
> >> diff --git a/drivers/hwmon/da9052-hwmon.c b/drivers/hwmon/da9052-hwmon.c
> >> index 2bd7ae8100d7..70e7bc72e980 100644
> >> --- a/drivers/hwmon/da9052-hwmon.c
> >> +++ b/drivers/hwmon/da9052-hwmon.c
> >> @@ -26,7 +26,6 @@ struct da9052_hwmon {
> >>   	struct mutex		hwmon_lock;
> >>   	bool			tsi_as_adc;
> >>   	int			tsiref_mv;
> >> -	struct regulator	*tsiref;
> >>   	struct completion	tsidone;
> >>   };
> >>   
> >> @@ -414,32 +413,19 @@ static int da9052_hwmon_probe(struct platform_device *pdev)
> >>   		device_property_read_bool(pdev->dev.parent, "dlg,tsi-as-adc");
> >>   
> >>   	if (hwmon->tsi_as_adc) {
> >> -		hwmon->tsiref = devm_regulator_get(pdev->dev.parent, "tsiref");
> >> -		if (IS_ERR(hwmon->tsiref)) {
> >> -			err = PTR_ERR(hwmon->tsiref);
> >> -			dev_err(&pdev->dev, "failed to get tsiref: %d", err);
> >> +		err = devm_regulator_get_enable_get_voltage(pdev->dev.parent,
> >> +							    "tsiref");
> >> +		if (err < 0)
> >>   			return err;
> >> -		}
> >> -
> >> -		err = regulator_enable(hwmon->tsiref);
> >> -		if (err)
> >> -			return err;
> >> -
> >> -		hwmon->tsiref_mv = regulator_get_voltage(hwmon->tsiref);
> >> -		if (hwmon->tsiref_mv < 0) {
> >> -			err = hwmon->tsiref_mv;
> >> -			goto exit_regulator;
> >> -		}
> >>   
> >>   		/* convert from microvolt (DT) to millivolt (hwmon) */
> >> -		hwmon->tsiref_mv /= 1000;
> >> +		hwmon->tsiref_mv = err / 1000;
> >>  
> > 
> > Using a variable called err for a good value is a bit ugly but fair enough if that
> > is precedence in this driver.
> >   
> 
> It isn't. The existing code assigns the return value from regulator_get_voltage()
> to hwmon->tsiref_mv and then evaluates it. I would not oppose introducing a variable
> such as tsiref_uv, but not the misuse of 'err'. I am not going to accept the code
> as suggested. It is bad style, and it would invite others to use it as precedent
> when trying to introduce similar code.

I was too lazy to look and see if there were existing cases :) Local variable indeed
the right way to go.

> 
> >>   }
> >> @@ -483,10 +466,8 @@ static void da9052_hwmon_remove(struct platform_device *pdev)
> >>   {
> >>   	struct da9052_hwmon *hwmon = platform_get_drvdata(pdev);
> >>   
> >> -	if (hwmon->tsi_as_adc) {
> >> +	if (hwmon->tsi_as_adc)
> >>   		da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);  
> > Superficially looks like devm_da9052_request_irq could be added that
> > uses devm_request_threaded_irq() to allow dropping this remaining handling.
> >   
> 
> That should be a separate series of patches. A local solution might be
> to use devm_add_action_or_reset(), but that should also be a separate patch.


Agreed.  Just a passing comment whilst the code was in front of me.

Jonathan

> 
> Thanks,
> Guenter
>
diff mbox series

Patch

diff --git a/drivers/hwmon/da9052-hwmon.c b/drivers/hwmon/da9052-hwmon.c
index 2bd7ae8100d7..70e7bc72e980 100644
--- a/drivers/hwmon/da9052-hwmon.c
+++ b/drivers/hwmon/da9052-hwmon.c
@@ -26,7 +26,6 @@  struct da9052_hwmon {
 	struct mutex		hwmon_lock;
 	bool			tsi_as_adc;
 	int			tsiref_mv;
-	struct regulator	*tsiref;
 	struct completion	tsidone;
 };
 
@@ -414,32 +413,19 @@  static int da9052_hwmon_probe(struct platform_device *pdev)
 		device_property_read_bool(pdev->dev.parent, "dlg,tsi-as-adc");
 
 	if (hwmon->tsi_as_adc) {
-		hwmon->tsiref = devm_regulator_get(pdev->dev.parent, "tsiref");
-		if (IS_ERR(hwmon->tsiref)) {
-			err = PTR_ERR(hwmon->tsiref);
-			dev_err(&pdev->dev, "failed to get tsiref: %d", err);
+		err = devm_regulator_get_enable_get_voltage(pdev->dev.parent,
+							    "tsiref");
+		if (err < 0)
 			return err;
-		}
-
-		err = regulator_enable(hwmon->tsiref);
-		if (err)
-			return err;
-
-		hwmon->tsiref_mv = regulator_get_voltage(hwmon->tsiref);
-		if (hwmon->tsiref_mv < 0) {
-			err = hwmon->tsiref_mv;
-			goto exit_regulator;
-		}
 
 		/* convert from microvolt (DT) to millivolt (hwmon) */
-		hwmon->tsiref_mv /= 1000;
+		hwmon->tsiref_mv = err / 1000;
 
 		/* TSIREF limits from datasheet */
 		if (hwmon->tsiref_mv < 1800 || hwmon->tsiref_mv > 2600) {
 			dev_err(hwmon->da9052->dev, "invalid TSIREF voltage: %d",
 				hwmon->tsiref_mv);
-			err = -ENXIO;
-			goto exit_regulator;
+			return -ENXIO;
 		}
 
 		/* disable touchscreen features */
@@ -456,7 +442,7 @@  static int da9052_hwmon_probe(struct platform_device *pdev)
 		if (err) {
 			dev_err(&pdev->dev, "Failed to register TSIRDY IRQ: %d",
 				err);
-			goto exit_regulator;
+			return err;
 		}
 	}
 
@@ -472,9 +458,6 @@  static int da9052_hwmon_probe(struct platform_device *pdev)
 exit_irq:
 	if (hwmon->tsi_as_adc)
 		da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
-exit_regulator:
-	if (hwmon->tsiref)
-		regulator_disable(hwmon->tsiref);
 
 	return err;
 }
@@ -483,10 +466,8 @@  static void da9052_hwmon_remove(struct platform_device *pdev)
 {
 	struct da9052_hwmon *hwmon = platform_get_drvdata(pdev);
 
-	if (hwmon->tsi_as_adc) {
+	if (hwmon->tsi_as_adc)
 		da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
-		regulator_disable(hwmon->tsiref);
-	}
 }
 
 static struct platform_driver da9052_hwmon_driver = {