gnss: Use devm_regulator_get_enable_optional()
Commit Message
Use devm_regulator_get_enable_optional() instead of hand writing it. It
saves some line of code.
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
Note that regulator_disable() is now called after gnss_serial_free() in
the error handling path of the probe and in the remove function, but it
looks harmless to me.
---
drivers/gnss/mtk.c | 26 ++++----------------------
1 file changed, 4 insertions(+), 22 deletions(-)
Comments
Il 17/06/23 11:57, Christophe JAILLET ha scritto:
> Use devm_regulator_get_enable_optional() instead of hand writing it. It
> saves some line of code.
>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
On Sat, Jun 17, 2023 at 11:57:09AM +0200, Christophe JAILLET wrote:
> Use devm_regulator_get_enable_optional() instead of hand writing it. It
> saves some line of code.
>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> Note that regulator_disable() is now called after gnss_serial_free() in
> the error handling path of the probe and in the remove function, but it
> looks harmless to me.
Yeah, that bit should be fine.
> ---
> drivers/gnss/mtk.c | 26 ++++----------------------
> 1 file changed, 4 insertions(+), 22 deletions(-)
>
> diff --git a/drivers/gnss/mtk.c b/drivers/gnss/mtk.c
> index c62b1211f4fe..d3d31295d4e0 100644
> --- a/drivers/gnss/mtk.c
> +++ b/drivers/gnss/mtk.c
> @@ -17,7 +17,6 @@
> #include "serial.h"
>
> struct mtk_data {
> - struct regulator *vbackup;
> struct regulator *vcc;
> };
>
> @@ -87,30 +86,16 @@ static int mtk_probe(struct serdev_device *serdev)
> goto err_free_gserial;
> }
>
> - data->vbackup = devm_regulator_get_optional(&serdev->dev, "vbackup");
> - if (IS_ERR(data->vbackup)) {
> - ret = PTR_ERR(data->vbackup);
> - if (ret == -ENODEV)
> - data->vbackup = NULL;
> - else
> - goto err_free_gserial;
> - }
> -
> - if (data->vbackup) {
> - ret = regulator_enable(data->vbackup);
> - if (ret)
> - goto err_free_gserial;
> - }
> + ret = devm_regulator_get_enable_optional(&serdev->dev, "vbackup");
> + if (ret)
> + goto err_free_gserial;
But this breaks the driver as the new helper still returns -ENODEV when
the optional is resource is not present.
Wolfram already suggested using this new helper here:
https://lore.kernel.org/lkml/20230523064310.3005-3-wsa+renesas@sang-engineering.com
and also got the error handling right even if that patch will require a
respin for other reasons.
As I mentioned in my reply to Wolfram, I'm generally sceptical of
helpers like this one, but in this case where there are no dependencies
on other resources I guess it's ok.
Johan
Le 20/06/2023 à 10:57, Johan Hovold a écrit :
> On Sat, Jun 17, 2023 at 11:57:09AM +0200, Christophe JAILLET wrote:
>> Use devm_regulator_get_enable_optional() instead of hand writing it. It
>> saves some line of code.
>>
>> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
>> ---
>> Note that regulator_disable() is now called after gnss_serial_free() in
>> the error handling path of the probe and in the remove function, but it
>> looks harmless to me.
>
> Yeah, that bit should be fine.
>
>> ---
>> drivers/gnss/mtk.c | 26 ++++----------------------
>> 1 file changed, 4 insertions(+), 22 deletions(-)
>>
>> diff --git a/drivers/gnss/mtk.c b/drivers/gnss/mtk.c
>> index c62b1211f4fe..d3d31295d4e0 100644
>> --- a/drivers/gnss/mtk.c
>> +++ b/drivers/gnss/mtk.c
>> @@ -17,7 +17,6 @@
>> #include "serial.h"
>>
>> struct mtk_data {
>> - struct regulator *vbackup;
>> struct regulator *vcc;
>> };
>>
>> @@ -87,30 +86,16 @@ static int mtk_probe(struct serdev_device *serdev)
>> goto err_free_gserial;
>> }
>>
>> - data->vbackup = devm_regulator_get_optional(&serdev->dev, "vbackup");
>> - if (IS_ERR(data->vbackup)) {
>> - ret = PTR_ERR(data->vbackup);
>> - if (ret == -ENODEV)
>> - data->vbackup = NULL;
>> - else
>> - goto err_free_gserial;
>> - }
>> -
>> - if (data->vbackup) {
>> - ret = regulator_enable(data->vbackup);
>> - if (ret)
>> - goto err_free_gserial;
>> - }
>> + ret = devm_regulator_get_enable_optional(&serdev->dev, "vbackup");
>> + if (ret)
>> + goto err_free_gserial;
>
> But this breaks the driver as the new helper still returns -ENODEV when
> the optional is resource is not present.
Ouch!
Thanks for the feedback. I urgently need to double check some other
similar proposals I've made.
Sorry for the broken patch :(
CJ
>
> Wolfram already suggested using this new helper here:
>
> https://lore.kernel.org/lkml/20230523064310.3005-3-wsa+renesas@sang-engineering.com
>
> and also got the error handling right even if that patch will require a
> respin for other reasons.
>
> As I mentioned in my reply to Wolfram, I'm generally sceptical of
> helpers like this one, but in this case where there are no dependencies
> on other resources I guess it's ok.
>
> Johan
>
@@ -17,7 +17,6 @@
#include "serial.h"
struct mtk_data {
- struct regulator *vbackup;
struct regulator *vcc;
};
@@ -87,30 +86,16 @@ static int mtk_probe(struct serdev_device *serdev)
goto err_free_gserial;
}
- data->vbackup = devm_regulator_get_optional(&serdev->dev, "vbackup");
- if (IS_ERR(data->vbackup)) {
- ret = PTR_ERR(data->vbackup);
- if (ret == -ENODEV)
- data->vbackup = NULL;
- else
- goto err_free_gserial;
- }
-
- if (data->vbackup) {
- ret = regulator_enable(data->vbackup);
- if (ret)
- goto err_free_gserial;
- }
+ ret = devm_regulator_get_enable_optional(&serdev->dev, "vbackup");
+ if (ret)
+ goto err_free_gserial;
ret = gnss_serial_register(gserial);
if (ret)
- goto err_disable_vbackup;
+ goto err_free_gserial;
return 0;
-err_disable_vbackup:
- if (data->vbackup)
- regulator_disable(data->vbackup);
err_free_gserial:
gnss_serial_free(gserial);
@@ -120,11 +105,8 @@ static int mtk_probe(struct serdev_device *serdev)
static void mtk_remove(struct serdev_device *serdev)
{
struct gnss_serial *gserial = serdev_device_get_drvdata(serdev);
- struct mtk_data *data = gnss_serial_get_drvdata(gserial);
gnss_serial_deregister(gserial);
- if (data->vbackup)
- regulator_disable(data->vbackup);
gnss_serial_free(gserial);
}