Skip to contentSkip to conversation

iio: dac: max5821: convert device register to device managed function

PATCH Superseded Théo Borém Fabris 3 participants 8 messages Jul 18, 2021 – Jul 24, 2021 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: dac: max5821: convert device register to device managed function

Add a device managed hook, via devm_add_action_or_reset() and max5821_regulator_disable(), to disable voltage regulator on device detach.
Replace iio_device_register() by devm_iio_device_register() and remove the max5821_remove() function used to unregister the device and disable the voltage regulator.
Remove i2c_set_clientdata() from the probe function, since i2c_get_clientdata() is not used anymore.

---
 drivers/iio/dac/max5821.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
drivers/iio/dac/max5821.c +16 -14
index bd6e75699a63..44c04ae70b32 100644
@@ -294,6 +294,13 @@ static const struct iio_info max5821_info = {
 	.write_raw = max5821_write_raw,
 };
 
+static void max5821_regulator_disable(void *data)
+{
+	struct regulator *rdata = data;
+
+	regulator_disable(rdata);
+}
+
 static int max5821_probe(struct i2c_client *client,
 			const struct i2c_device_id *id)
 {
@@ -306,7 +313,6 @@ static int max5821_probe(struct i2c_client *client,
 	if (!indio_dev)
 		return -ENOMEM;
 	data = iio_priv(indio_dev);
-	i2c_set_clientdata(client, indio_dev);
 	data->client = client;
 	mutex_init(&data->lock);
 
@@ -331,6 +337,14 @@ static int max5821_probe(struct i2c_client *client,
 		goto error_free_reg;
 	}
 
+	ret = devm_add_action_or_reset(&client->dev, max5821_regulator_disable,
+				       data->vref_reg);
+	if (ret) {
+		dev_err(&client->dev,
+			"Failed to add action to managed regulator: %d\n", ret);
+		goto error_disable_reg;
+	}
+
 	ret = regulator_get_voltage(data->vref_reg);
 	if (ret < 0) {
 		dev_err(&client->dev,
@@ -346,7 +360,7 @@ static int max5821_probe(struct i2c_client *client,
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->info = &max5821_info;
 
-	return iio_device_register(indio_dev);
+	return devm_iio_device_register(&client->dev, indio_dev);
 
 error_disable_reg:
 	regulator_disable(data->vref_reg);
@@ -356,17 +370,6 @@ static int max5821_probe(struct i2c_client *client,
 	return ret;
 }
 
-static int max5821_remove(struct i2c_client *client)
-{
-	struct iio_dev *indio_dev = i2c_get_clientdata(client);
-	struct max5821_data *data = iio_priv(indio_dev);
-
-	iio_device_unregister(indio_dev);
-	regulator_disable(data->vref_reg);
-
-	return 0;
-}
-
 static const struct i2c_device_id max5821_id[] = {
 	{ "max5821", ID_MAX5821 },
 	{ }
@@ -386,7 +389,6 @@ static struct i2c_driver max5821_driver = {
 		.pm     = &max5821_pm_ops,
 	},
 	.probe		= max5821_probe,
-	.remove		= max5821_remove,
 	.id_table	= max5821_id,
 };
 module_i2c_driver(max5821_driver);
Signature · 1 line
2.20.1
#1 lore raw
Quoted 9 lines · Théo Borém Fabris

Add a device managed hook, via devm_add_action_or_reset() and max5821_regulator_disable(), to disable voltage regulator on device detach.
Replace iio_device_register() by devm_iio_device_register() and remove the max5821_remove() function used to unregister the device and disable the voltage regulator.
Remove i2c_set_clientdata() from the probe function, since i2c_get_clientdata() is not used anymore.

Looks good overall.
A few comments inline.

Quoted 11 lines
---
 drivers/iio/dac/max5821.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
drivers/iio/dac/max5821.c +0 -0
index bd6e75699a63..44c04ae70b32 100644

drivers/iio/dac/max5821.c static const struct iio_info max5821_info = { · line 301

Quoted 8 lines
drivers/iio/dac/max5821.c +5 -0
@@ -294,6 +294,13 @@ static const struct iio_info max5821_info = {
        .write_raw = max5821_write_raw,
 };
 
+static void max5821_regulator_disable(void *data)
+{
+       struct regulator *rdata = data;
+
+       regulator_disable(rdata);

This can be simplified a bit:

static void max5821_regulator_disable(void *reg)

{
      regulator_disable(reg);
}

I used to do explicit casting, but then I also figured that it's not necessary.

Quoted 14 lines
drivers/iio/dac/max5821.c +2 -1
+}
+
 static int max5821_probe(struct i2c_client *client,
                        const struct i2c_device_id *id)
 {
@@ -306,7 +313,6 @@ static int max5821_probe(struct i2c_client *client,
        if (!indio_dev)
                return -ENOMEM;
        data = iio_priv(indio_dev);
-       i2c_set_clientdata(client, indio_dev);
        data->client = client;
        mutex_init(&data->lock);
 

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 345

Quoted 9 lines
drivers/iio/dac/max5821.c +6 -0
@@ -331,6 +337,14 @@ static int max5821_probe(struct i2c_client *client,
                goto error_free_reg;
        }
 
+       ret = devm_add_action_or_reset(&client->dev, max5821_regulator_disable,
+                                      data->vref_reg);
+       if (ret) {
+               dev_err(&client->dev,
+                       "Failed to add action to managed regulator: %d\n", ret);
+               goto error_disable_reg;

return ret;

devm_add_action_or_reset() should call max5821_regulator_disable() in case of error

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 365

Quoted 13 lines
drivers/iio/dac/max5821.c +3 -1
+       }
+
        ret = regulator_get_voltage(data->vref_reg);
        if (ret < 0) {
                dev_err(&client->dev,
@@ -346,7 +360,7 @@ static int max5821_probe(struct i2c_client *client,
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->info = &max5821_info;
 
-       return iio_device_register(indio_dev);
+       return devm_iio_device_register(&client->dev, indio_dev);
 
 error_disable_reg:

This entire goto block should be removed.
The idea of using only devm_ functions is to not have these goto statements.

Quoted 30 lines
drivers/iio/dac/max5821.c +0 -13
        regulator_disable(data->vref_reg);
@@ -356,17 +370,6 @@ static int max5821_probe(struct i2c_client *client,
        return ret;
 }
 
-static int max5821_remove(struct i2c_client *client)
-{
-       struct iio_dev *indio_dev = i2c_get_clientdata(client);
-       struct max5821_data *data = iio_priv(indio_dev);
-
-       iio_device_unregister(indio_dev);
-       regulator_disable(data->vref_reg);
-
-       return 0;
-}
-
 static const struct i2c_device_id max5821_id[] = {
        { "max5821", ID_MAX5821 },
        { }
@@ -386,7 +389,6 @@ static struct i2c_driver max5821_driver = {
                .pm     = &max5821_pm_ops,
        },
        .probe          = max5821_probe,
-       .remove         = max5821_remove,
        .id_table       = max5821_id,
 };
 module_i2c_driver(max5821_driver);
--

2.20.1

#2 lore raw

Hi, Alexandru.

Em seg., 19 de jul. de 2021 às 04:33, Alexandru Ardelean <[email protected]> escreveu:

Quoted 37 lines · Théo Borém Fabris
Quoted 9 lines

Add a device managed hook, via devm_add_action_or_reset() and max5821_regulator_disable(), to disable voltage regulator on device detach.
Replace iio_device_register() by devm_iio_device_register() and remove the max5821_remove() function used to unregister the device and disable the voltage regulator.
Remove i2c_set_clientdata() from the probe function, since i2c_get_clientdata() is not used anymore.

Looks good overall.
A few comments inline.

Quoted 11 lines
---
 drivers/iio/dac/max5821.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
drivers/iio/dac/max5821.c +0 -0
index bd6e75699a63..44c04ae70b32 100644

drivers/iio/dac/max5821.c static const struct iio_info max5821_info = { · line 301

Quoted 8 lines
drivers/iio/dac/max5821.c +5 -0
@@ -294,6 +294,13 @@ static const struct iio_info max5821_info = {
        .write_raw = max5821_write_raw,
 };
 
+static void max5821_regulator_disable(void *data)
+{
+       struct regulator *rdata = data;
+
+       regulator_disable(rdata);

This can be simplified a bit:

Quoted 7 lines

static void max5821_regulator_disable(void *reg)

{
      regulator_disable(reg);
}

I used to do explicit casting, but then I also figured that it's not necessary.

Ok.

Quoted 26 lines
Quoted 14 lines
drivers/iio/dac/max5821.c +2 -1
+}
+
 static int max5821_probe(struct i2c_client *client,
                        const struct i2c_device_id *id)
 {
@@ -306,7 +313,6 @@ static int max5821_probe(struct i2c_client *client,
        if (!indio_dev)
                return -ENOMEM;
        data = iio_priv(indio_dev);
-       i2c_set_clientdata(client, indio_dev);
        data->client = client;
        mutex_init(&data->lock);
 

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 345

Quoted 9 lines
drivers/iio/dac/max5821.c +6 -0
@@ -331,6 +337,14 @@ static int max5821_probe(struct i2c_client *client,
                goto error_free_reg;
        }
 
+       ret = devm_add_action_or_reset(&client->dev, max5821_regulator_disable,
+                                      data->vref_reg);
+       if (ret) {
+               dev_err(&client->dev,
+                       "Failed to add action to managed regulator: %d\n", ret);
+               goto error_disable_reg;

return ret;

Quoted 3 lines

devm_add_action_or_reset() should call max5821_regulator_disable() in case of error

Ok.

Quoted 9 lines
Quoted 9 lines
drivers/iio/dac/max5821.c +2 -0
+       }
+
        ret = regulator_get_voltage(data->vref_reg);
        if (ret < 0) {
                dev_err(&client->dev,
@@ -346,7 +360,7 @@ static int max5821_probe(struct i2c_client *client,
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->info = &max5821_info;
 
Quoted 8 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 365

Quoted 4 lines
drivers/iio/dac/max5821.c +1 -1
-       return iio_device_register(indio_dev);
+       return devm_iio_device_register(&client->dev, indio_dev);
 
 error_disable_reg:

This entire goto block should be removed.
The idea of using only devm_ functions is to not have these goto statements.

I thought the action added via devm_add_action (and devres_add) was called only on driver detach, thus the error_disable_reg label would be necessary to handle the
possible error on regulator_get_voltage. Could you please clarify for me when does
a driver detach happen?

Thanks for your reply,
Théo

Quoted 30 lines
Quoted 30 lines
drivers/iio/dac/max5821.c +0 -13
        regulator_disable(data->vref_reg);
@@ -356,17 +370,6 @@ static int max5821_probe(struct i2c_client *client,
        return ret;
 }
 
-static int max5821_remove(struct i2c_client *client)
-{
-       struct iio_dev *indio_dev = i2c_get_clientdata(client);
-       struct max5821_data *data = iio_priv(indio_dev);
-
-       iio_device_unregister(indio_dev);
-       regulator_disable(data->vref_reg);
-
-       return 0;
-}
-
 static const struct i2c_device_id max5821_id[] = {
        { "max5821", ID_MAX5821 },
        { }
@@ -386,7 +389,6 @@ static struct i2c_driver max5821_driver = {
                .pm     = &max5821_pm_ops,
        },
        .probe          = max5821_probe,
-       .remove         = max5821_remove,
        .id_table       = max5821_id,
 };
 module_i2c_driver(max5821_driver);
--

2.20.1

#3 lore raw
Quoted 94 lines · Théo Borém Fabris

Hi, Alexandru.

Em seg., 19 de jul. de 2021 às 04:33, Alexandru Ardelean <[email protected]> escreveu:

Quoted 37 lines · Théo Borém Fabris
Quoted 9 lines

Add a device managed hook, via devm_add_action_or_reset() and max5821_regulator_disable(), to disable voltage regulator on device detach.
Replace iio_device_register() by devm_iio_device_register() and remove the max5821_remove() function used to unregister the device and disable the voltage regulator.
Remove i2c_set_clientdata() from the probe function, since i2c_get_clientdata() is not used anymore.

Looks good overall.
A few comments inline.

Quoted 11 lines
---
 drivers/iio/dac/max5821.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
drivers/iio/dac/max5821.c +0 -0
index bd6e75699a63..44c04ae70b32 100644

drivers/iio/dac/max5821.c static const struct iio_info max5821_info = { · line 301

Quoted 8 lines
drivers/iio/dac/max5821.c +5 -0
@@ -294,6 +294,13 @@ static const struct iio_info max5821_info = {
        .write_raw = max5821_write_raw,
 };
 
+static void max5821_regulator_disable(void *data)
+{
+       struct regulator *rdata = data;
+
+       regulator_disable(rdata);

This can be simplified a bit:

Quoted 7 lines

static void max5821_regulator_disable(void *reg)

{
      regulator_disable(reg);
}

I used to do explicit casting, but then I also figured that it's not necessary.

Ok.

Quoted 26 lines
Quoted 14 lines
drivers/iio/dac/max5821.c +2 -1
+}
+
 static int max5821_probe(struct i2c_client *client,
                        const struct i2c_device_id *id)
 {
@@ -306,7 +313,6 @@ static int max5821_probe(struct i2c_client *client,
        if (!indio_dev)
                return -ENOMEM;
        data = iio_priv(indio_dev);
-       i2c_set_clientdata(client, indio_dev);
        data->client = client;
        mutex_init(&data->lock);
 

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 345

Quoted 9 lines
drivers/iio/dac/max5821.c +6 -0
@@ -331,6 +337,14 @@ static int max5821_probe(struct i2c_client *client,
                goto error_free_reg;
        }
 
+       ret = devm_add_action_or_reset(&client->dev, max5821_regulator_disable,
+                                      data->vref_reg);
+       if (ret) {
+               dev_err(&client->dev,
+                       "Failed to add action to managed regulator: %d\n", ret);
+               goto error_disable_reg;

return ret;

Quoted 3 lines

devm_add_action_or_reset() should call max5821_regulator_disable() in case of error

Ok.

Quoted 12 lines
Quoted 12 lines
drivers/iio/dac/max5821.c +3 -1
+       }
+
        ret = regulator_get_voltage(data->vref_reg);
        if (ret < 0) {
                dev_err(&client->dev,
@@ -346,7 +360,7 @@ static int max5821_probe(struct i2c_client *client,
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->info = &max5821_info;
 
-       return iio_device_register(indio_dev);
+       return devm_iio_device_register(&client->dev, indio_dev);
 
Quoted 8 lines
Quoted 5 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 365

Quoted 1 line
drivers/iio/dac/max5821.c +0 -0
 error_disable_reg:

This entire goto block should be removed.
The idea of using only devm_ functions is to not have these goto statements.

I thought the action added via devm_add_action (and devres_add) was called only on driver detach, thus the error_disable_reg label would be necessary to handle the

devm_add_action() yes
this is devm_add_action_or_reset() which looks like this:

static inline int devm_add_action_or_reset(struct device *dev,

                       void (*action)(void *), void *data)
{
    int ret;

    ret = devm_add_action(dev, action, data);
    if (ret)
        action(data);

    return ret;
}

it can be found in "include/linux/device.h"

Quoted 3 lines

possible error on regulator_get_voltage. Could you please clarify for me when does
a driver detach happen?

a driver detach happens when:
* the kmod is unloaded (assuming the driver is running as a kmod)
* manually unbinding the driver from sysfs ;
a quick article about this [it's for USB, but other interfaces use the same mechanism]:
https://lwn.net/Articles/143397/
there should be something under /sys/bus/spi/drivers/xxxx/unbind
* when the system powers down (reboots)
* maybe there is some other new method to do this [that I don't know about]

Quoted 34 lines

Thanks for your reply,
Théo

Quoted 30 lines
Quoted 30 lines
drivers/iio/dac/max5821.c +0 -13
        regulator_disable(data->vref_reg);
@@ -356,17 +370,6 @@ static int max5821_probe(struct i2c_client *client,
        return ret;
 }
 
-static int max5821_remove(struct i2c_client *client)
-{
-       struct iio_dev *indio_dev = i2c_get_clientdata(client);
-       struct max5821_data *data = iio_priv(indio_dev);
-
-       iio_device_unregister(indio_dev);
-       regulator_disable(data->vref_reg);
-
-       return 0;
-}
-
 static const struct i2c_device_id max5821_id[] = {
        { "max5821", ID_MAX5821 },
        { }
@@ -386,7 +389,6 @@ static struct i2c_driver max5821_driver = {
                .pm     = &max5821_pm_ops,
        },
        .probe          = max5821_probe,
-       .remove         = max5821_remove,
        .id_table       = max5821_id,
 };
 module_i2c_driver(max5821_driver);
--

2.20.1

reviewer
#4 lore raw
Quoted 159 lines

From: Alexandru Ardelean <[email protected]>
Sent: Tuesday, July 20, 2021 9:23 AM
To: Théo Borém Fabris <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; linux-iio <[email protected]>; LKML <[email protected]>
Subject: Re: [PATCH] iio: dac: max5821: convert device register to device managed function

Quoted 7 lines · Théo Borém Fabris

Hi, Alexandru.

Em seg., 19 de jul. de 2021 às 04:33, Alexandru Ardelean <[email protected]> escreveu:

Quoted 2 lines

On Sun, Jul 18, 2021 at 11:42 PM Théo Borém Fabris

Quoted 2 lines · <[email protected]> wrote:
Quoted 2 lines
Quoted 2 lines

Add a device managed hook, via devm_add_action_or_reset()

and

Quoted 1 line
Quoted 1 line
Quoted 1 line

max5821_regulator_disable(), to disable voltage regulator on

device

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

detach.
Replace iio_device_register() by devm_iio_device_register() and

remove

Quoted 1 line
Quoted 1 line
Quoted 1 line

the max5821_remove() function used to unregister the device

and disable the

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

voltage regulator.
Remove i2c_set_clientdata() from the probe function, since i2c_get_clientdata() is not used anymore.

Looks good overall.
A few comments inline.

Quoted 1 line
Quoted 6 lines
Quoted 6 lines

Review

Quoted 6 lines
---
 drivers/iio/dac/max5821.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/dac/max5821.c

Quoted 4 lines
Quoted 4 lines

drivers/iio/dac/max5821.c static const struct iio_info max5821_info

Quoted 4 lines

index bd6e75699a63..44c04ae70b32 100644

drivers/iio/dac/max5821.c +0 -0
@@ -294,6 +294,13 @@ static const struct iio_info max5821_info

= {

Quoted 11 lines
Quoted 11 lines

drivers/iio/dac/max5821.c static const struct iio_info max5821_info = { · line 301

Quoted 8 lines
drivers/iio/dac/max5821.c +5 -0
        .write_raw = max5821_write_raw,
 };
 
+static void max5821_regulator_disable(void *data)
+{
+       struct regulator *rdata = data;
+
+       regulator_disable(rdata);

This can be simplified a bit:

Quoted 6 lines
Quoted 6 lines

static void max5821_regulator_disable(void *reg)

{
      regulator_disable(reg);
}

I used to do explicit casting, but then I also figured that it's not

necessary.

Quoted 9 lines
Quoted 1 line

Ok.

Quoted 6 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client

Quoted 6 lines
drivers/iio/dac/max5821.c +2 -0
+}
+
 static int max5821_probe(struct i2c_client *client,
                        const struct i2c_device_id *id)
 {
@@ -306,7 +313,6 @@ static int max5821_probe(struct i2c_client

*client,

Quoted 8 lines
Quoted 8 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct

Quoted 8 lines
drivers/iio/dac/max5821.c +0 -1
        if (!indio_dev)
                return -ENOMEM;
        data = iio_priv(indio_dev);
-       i2c_set_clientdata(client, indio_dev);
        data->client = client;
        mutex_init(&data->lock);
 
@@ -331,6 +337,14 @@ static int max5821_probe(struct

i2c_client *client,

Quoted 4 lines
Quoted 4 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client,

Quoted 4 lines
drivers/iio/dac/max5821.c +1 -0
                goto error_free_reg;
        }
 
+       ret = devm_add_action_or_reset(&client->dev,

max5821_regulator_disable,

Quoted 4 lines
Quoted 4 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client,

Quoted 4 lines
drivers/iio/dac/max5821.c +4 -0
+                                      data->vref_reg);
+       if (ret) {
+               dev_err(&client->dev,
+                       "Failed to add action to managed regulator: %d\n",

ret);

Quoted 5 lines
Quoted 5 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 345

Quoted 1 line
drivers/iio/dac/max5821.c +1 -0
+               goto error_disable_reg;

return ret;

devm_add_action_or_reset() should call

max5821_regulator_disable() in

Quoted 10 lines
Quoted 2 lines

case of error

Ok.

Quoted 6 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client

Quoted 6 lines
drivers/iio/dac/max5821.c +2 -0
+       }
+
        ret = regulator_get_voltage(data->vref_reg);
        if (ret < 0) {
                dev_err(&client->dev,
@@ -346,7 +360,7 @@ static int max5821_probe(struct i2c_client

*client,

Quoted 10 lines
Quoted 10 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 365

Quoted 7 lines
drivers/iio/dac/max5821.c +1 -1
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->info = &max5821_info;
 
-       return iio_device_register(indio_dev);
+       return devm_iio_device_register(&client->dev, indio_dev);
 
 error_disable_reg:

This entire goto block should be removed.
The idea of using only devm_ functions is to not have these goto

statements.

Quoted 2 lines
Quoted 1 line

I thought the action added via devm_add_action (and devres_add)

was called only

Quoted 1 line

on driver detach, thus the error_disable_reg label would be

necessary

Quoted 1 line

to handle the

devm_add_action() yes
this is devm_add_action_or_reset() which looks like this:

static inline int devm_add_action_or_reset(struct device *dev,

                       void (*action)(void *), void *data)
{
    int ret;

    ret = devm_add_action(dev, action, data);
    if (ret)
        action(data);

    return ret;
}

it can be found in "include/linux/device.h"

Quoted 3 lines

possible error on regulator_get_voltage. Could you please clarify for me when does
a driver detach happen?

a driver detach happens when:
* the kmod is unloaded (assuming the driver is running as a kmod)
* manually unbinding the driver from sysfs ;
a quick article about this [it's for USB, but other interfaces use the same mechanism]:

Quoted 8 lines

https://urldefense.com/v3/__https://lwn.net/Articles/143397/__;!!A
3Ni8CS0y2Y!rw- qxijVlohZWCG9q_wWFPGASqW7JWcfS48uJSwY8coKiBPoA9Z0G2WSCZ clCw$
there should be something under /sys/bus/spi/drivers/xxxx/unbind
* when the system powers down (reboots)
* maybe there is some other new method to do this [that I don't know about]

If the device get's deleted with 'device_del()'...

- Nuno Sá

#5 lore raw
In reply to Alexandru Ardelean

Em ter., 20 de jul. de 2021 às 04:22, Alexandru Ardelean <[email protected]> escreveu:

Quoted 126 lines · Théo Borém Fabris
Quoted 94 lines

Hi, Alexandru.

Em seg., 19 de jul. de 2021 às 04:33, Alexandru Ardelean <[email protected]> escreveu:

Quoted 37 lines · Théo Borém Fabris
Quoted 9 lines

Add a device managed hook, via devm_add_action_or_reset() and max5821_regulator_disable(), to disable voltage regulator on device detach.
Replace iio_device_register() by devm_iio_device_register() and remove the max5821_remove() function used to unregister the device and disable the voltage regulator.
Remove i2c_set_clientdata() from the probe function, since i2c_get_clientdata() is not used anymore.

Looks good overall.
A few comments inline.

Quoted 11 lines
---
 drivers/iio/dac/max5821.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
drivers/iio/dac/max5821.c +0 -0
index bd6e75699a63..44c04ae70b32 100644

drivers/iio/dac/max5821.c static const struct iio_info max5821_info = { · line 301

Quoted 8 lines
drivers/iio/dac/max5821.c +5 -0
@@ -294,6 +294,13 @@ static const struct iio_info max5821_info = {
        .write_raw = max5821_write_raw,
 };
 
+static void max5821_regulator_disable(void *data)
+{
+       struct regulator *rdata = data;
+
+       regulator_disable(rdata);

This can be simplified a bit:

Quoted 7 lines

static void max5821_regulator_disable(void *reg)

{
      regulator_disable(reg);
}

I used to do explicit casting, but then I also figured that it's not necessary.

Ok.

Quoted 26 lines
Quoted 14 lines
drivers/iio/dac/max5821.c +2 -1
+}
+
 static int max5821_probe(struct i2c_client *client,
                        const struct i2c_device_id *id)
 {
@@ -306,7 +313,6 @@ static int max5821_probe(struct i2c_client *client,
        if (!indio_dev)
                return -ENOMEM;
        data = iio_priv(indio_dev);
-       i2c_set_clientdata(client, indio_dev);
        data->client = client;
        mutex_init(&data->lock);
 

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 345

Quoted 9 lines
drivers/iio/dac/max5821.c +6 -0
@@ -331,6 +337,14 @@ static int max5821_probe(struct i2c_client *client,
                goto error_free_reg;
        }
 
+       ret = devm_add_action_or_reset(&client->dev, max5821_regulator_disable,
+                                      data->vref_reg);
+       if (ret) {
+               dev_err(&client->dev,
+                       "Failed to add action to managed regulator: %d\n", ret);
+               goto error_disable_reg;

return ret;

Quoted 3 lines

devm_add_action_or_reset() should call max5821_regulator_disable() in case of error

Ok.

Quoted 12 lines
Quoted 12 lines
drivers/iio/dac/max5821.c +3 -1
+       }
+
        ret = regulator_get_voltage(data->vref_reg);
        if (ret < 0) {
                dev_err(&client->dev,
@@ -346,7 +360,7 @@ static int max5821_probe(struct i2c_client *client,
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->info = &max5821_info;
 
-       return iio_device_register(indio_dev);
+       return devm_iio_device_register(&client->dev, indio_dev);
 
Quoted 8 lines
Quoted 5 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 365

Quoted 1 line
drivers/iio/dac/max5821.c +0 -0
 error_disable_reg:

This entire goto block should be removed.
The idea of using only devm_ functions is to not have these goto statements.

I thought the action added via devm_add_action (and devres_add) was called only on driver detach, thus the error_disable_reg label would be necessary to handle the

devm_add_action() yes
this is devm_add_action_or_reset() which looks like this:

static inline int devm_add_action_or_reset(struct device *dev,

                       void (*action)(void *), void *data)
{
    int ret;

    ret = devm_add_action(dev, action, data);
    if (ret)
        action(data);

    return ret;
}

it can be found in "include/linux/device.h"

Quoted 3 lines

possible error on regulator_get_voltage. Could you please clarify for me when does
a driver detach happen?

Quoted 10 lines

a driver detach happens when:
* the kmod is unloaded (assuming the driver is running as a kmod)
* manually unbinding the driver from sysfs ;
a quick article about this [it's for USB, but other interfaces use the same mechanism]:
https://lwn.net/Articles/143397/
there should be something under /sys/bus/spi/drivers/xxxx/unbind
* when the system powers down (reboots)
* maybe there is some other new method to do this [that I don't know about]

Thank you so much.

So, should this goto error_disable_reg be replaced by "regulator_disable(.); return ret;"?

ret = regulator_get_voltage(data->vref_reg);
    if (ret < 0) {
        dev_err(&client->dev,
             "Failed to get voltage on regulator: %d\n", ret);
        goto error_disable_reg;
    }
Quoted 34 lines
Quoted 34 lines

Thanks for your reply,
Théo

Quoted 30 lines
Quoted 30 lines
drivers/iio/dac/max5821.c +0 -13
        regulator_disable(data->vref_reg);
@@ -356,17 +370,6 @@ static int max5821_probe(struct i2c_client *client,
        return ret;
 }
 
-static int max5821_remove(struct i2c_client *client)
-{
-       struct iio_dev *indio_dev = i2c_get_clientdata(client);
-       struct max5821_data *data = iio_priv(indio_dev);
-
-       iio_device_unregister(indio_dev);
-       regulator_disable(data->vref_reg);
-
-       return 0;
-}
-
 static const struct i2c_device_id max5821_id[] = {
        { "max5821", ID_MAX5821 },
        { }
@@ -386,7 +389,6 @@ static struct i2c_driver max5821_driver = {
                .pm     = &max5821_pm_ops,
        },
        .probe          = max5821_probe,
-       .remove         = max5821_remove,
        .id_table       = max5821_id,
 };
 module_i2c_driver(max5821_driver);
--

2.20.1

#6 lore raw
Quoted 141 lines · Théo Borém Fabris

Em ter., 20 de jul. de 2021 às 04:22, Alexandru Ardelean <[email protected]> escreveu:

Quoted 126 lines · Théo Borém Fabris
Quoted 94 lines

Hi, Alexandru.

Em seg., 19 de jul. de 2021 às 04:33, Alexandru Ardelean <[email protected]> escreveu:

Quoted 37 lines · Théo Borém Fabris
Quoted 9 lines

Add a device managed hook, via devm_add_action_or_reset() and max5821_regulator_disable(), to disable voltage regulator on device detach.
Replace iio_device_register() by devm_iio_device_register() and remove the max5821_remove() function used to unregister the device and disable the voltage regulator.
Remove i2c_set_clientdata() from the probe function, since i2c_get_clientdata() is not used anymore.

Looks good overall.
A few comments inline.

Quoted 11 lines
---
 drivers/iio/dac/max5821.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
drivers/iio/dac/max5821.c +0 -0
index bd6e75699a63..44c04ae70b32 100644

drivers/iio/dac/max5821.c static const struct iio_info max5821_info = { · line 301

Quoted 8 lines
drivers/iio/dac/max5821.c +5 -0
@@ -294,6 +294,13 @@ static const struct iio_info max5821_info = {
        .write_raw = max5821_write_raw,
 };
 
+static void max5821_regulator_disable(void *data)
+{
+       struct regulator *rdata = data;
+
+       regulator_disable(rdata);

This can be simplified a bit:

Quoted 7 lines

static void max5821_regulator_disable(void *reg)

{
      regulator_disable(reg);
}

I used to do explicit casting, but then I also figured that it's not necessary.

Ok.

Quoted 26 lines
Quoted 14 lines
drivers/iio/dac/max5821.c +2 -1
+}
+
 static int max5821_probe(struct i2c_client *client,
                        const struct i2c_device_id *id)
 {
@@ -306,7 +313,6 @@ static int max5821_probe(struct i2c_client *client,
        if (!indio_dev)
                return -ENOMEM;
        data = iio_priv(indio_dev);
-       i2c_set_clientdata(client, indio_dev);
        data->client = client;
        mutex_init(&data->lock);
 

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 345

Quoted 9 lines
drivers/iio/dac/max5821.c +6 -0
@@ -331,6 +337,14 @@ static int max5821_probe(struct i2c_client *client,
                goto error_free_reg;
        }
 
+       ret = devm_add_action_or_reset(&client->dev, max5821_regulator_disable,
+                                      data->vref_reg);
+       if (ret) {
+               dev_err(&client->dev,
+                       "Failed to add action to managed regulator: %d\n", ret);
+               goto error_disable_reg;

return ret;

Quoted 3 lines

devm_add_action_or_reset() should call max5821_regulator_disable() in case of error

Ok.

Quoted 12 lines
Quoted 12 lines
drivers/iio/dac/max5821.c +3 -1
+       }
+
        ret = regulator_get_voltage(data->vref_reg);
        if (ret < 0) {
                dev_err(&client->dev,
@@ -346,7 +360,7 @@ static int max5821_probe(struct i2c_client *client,
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->info = &max5821_info;
 
-       return iio_device_register(indio_dev);
+       return devm_iio_device_register(&client->dev, indio_dev);
 
Quoted 8 lines
Quoted 5 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 365

Quoted 1 line
drivers/iio/dac/max5821.c +0 -0
 error_disable_reg:

This entire goto block should be removed.
The idea of using only devm_ functions is to not have these goto statements.

I thought the action added via devm_add_action (and devres_add) was called only on driver detach, thus the error_disable_reg label would be necessary to handle the

devm_add_action() yes
this is devm_add_action_or_reset() which looks like this:

static inline int devm_add_action_or_reset(struct device *dev,

                       void (*action)(void *), void *data)
{
    int ret;

    ret = devm_add_action(dev, action, data);
    if (ret)
        action(data);

    return ret;
}

it can be found in "include/linux/device.h"

Quoted 3 lines

possible error on regulator_get_voltage. Could you please clarify for me when does
a driver detach happen?

Quoted 10 lines

a driver detach happens when:
* the kmod is unloaded (assuming the driver is running as a kmod)
* manually unbinding the driver from sysfs ;
a quick article about this [it's for USB, but other interfaces use the same mechanism]:
https://lwn.net/Articles/143397/
there should be something under /sys/bus/spi/drivers/xxxx/unbind
* when the system powers down (reboots)
* maybe there is some other new method to do this [that I don't know about]

Thank you so much.

Quoted 8 lines

So, should this goto error_disable_reg be replaced by "regulator_disable(.); return ret;"?

ret = regulator_get_voltage(data->vref_reg);
    if (ret < 0) {
        dev_err(&client->dev,
             "Failed to get voltage on regulator: %d\n", ret);
        goto error_disable_reg;
    }

This is sufficient:
[ No extra regulator_disable() is required; regulator_disable() gets called via max5821_regulator_disable() on the error path ]

       ret = devm_add_action_or_reset(&client->dev, max5821_regulator_disable,
                                      data->vref_reg);
       if (ret) {
               dev_err(&client->dev,
                       "Failed to add action to managed regulator: %d\n", ret);
               return ret;
       }
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

Thanks for your reply,
Théo

Quoted 30 lines
Quoted 30 lines
drivers/iio/dac/max5821.c +0 -13
        regulator_disable(data->vref_reg);
@@ -356,17 +370,6 @@ static int max5821_probe(struct i2c_client *client,
        return ret;
 }
 
-static int max5821_remove(struct i2c_client *client)
-{
-       struct iio_dev *indio_dev = i2c_get_clientdata(client);
-       struct max5821_data *data = iio_priv(indio_dev);
-
-       iio_device_unregister(indio_dev);
-       regulator_disable(data->vref_reg);
-
-       return 0;
-}
-
 static const struct i2c_device_id max5821_id[] = {
        { "max5821", ID_MAX5821 },
        { }
@@ -386,7 +389,6 @@ static struct i2c_driver max5821_driver = {
                .pm     = &max5821_pm_ops,
        },
        .probe          = max5821_probe,
-       .remove         = max5821_remove,
        .id_table       = max5821_id,
 };
 module_i2c_driver(max5821_driver);
--

2.20.1

#7 lore raw

Thank you, Alexandru and Numo, for your help. I have submitted the second version.

Théo

Em qua., 21 de jul. de 2021 às 04:23, Alexandru Ardelean <[email protected]> escreveu:

Quoted 200 lines · Théo Borém Fabris
Quoted 141 lines

Em ter., 20 de jul. de 2021 às 04:22, Alexandru Ardelean <[email protected]> escreveu:

Quoted 126 lines · Théo Borém Fabris
Quoted 94 lines

Hi, Alexandru.

Em seg., 19 de jul. de 2021 às 04:33, Alexandru Ardelean <[email protected]> escreveu:

Quoted 37 lines · Théo Borém Fabris
Quoted 9 lines

Add a device managed hook, via devm_add_action_or_reset() and max5821_regulator_disable(), to disable voltage regulator on device detach.
Replace iio_device_register() by devm_iio_device_register() and remove the max5821_remove() function used to unregister the device and disable the voltage regulator.
Remove i2c_set_clientdata() from the probe function, since i2c_get_clientdata() is not used anymore.

Looks good overall.
A few comments inline.

Quoted 11 lines
---
 drivers/iio/dac/max5821.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
drivers/iio/dac/max5821.c +0 -0
index bd6e75699a63..44c04ae70b32 100644

drivers/iio/dac/max5821.c static const struct iio_info max5821_info = { · line 301

Quoted 8 lines
drivers/iio/dac/max5821.c +5 -0
@@ -294,6 +294,13 @@ static const struct iio_info max5821_info = {
        .write_raw = max5821_write_raw,
 };
 
+static void max5821_regulator_disable(void *data)
+{
+       struct regulator *rdata = data;
+
+       regulator_disable(rdata);

This can be simplified a bit:

Quoted 7 lines

static void max5821_regulator_disable(void *reg)

{
      regulator_disable(reg);
}

I used to do explicit casting, but then I also figured that it's not necessary.

Ok.

Quoted 26 lines
Quoted 14 lines
drivers/iio/dac/max5821.c +2 -1
+}
+
 static int max5821_probe(struct i2c_client *client,
                        const struct i2c_device_id *id)
 {
@@ -306,7 +313,6 @@ static int max5821_probe(struct i2c_client *client,
        if (!indio_dev)
                return -ENOMEM;
        data = iio_priv(indio_dev);
-       i2c_set_clientdata(client, indio_dev);
        data->client = client;
        mutex_init(&data->lock);
 

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 345

Quoted 9 lines
drivers/iio/dac/max5821.c +6 -0
@@ -331,6 +337,14 @@ static int max5821_probe(struct i2c_client *client,
                goto error_free_reg;
        }
 
+       ret = devm_add_action_or_reset(&client->dev, max5821_regulator_disable,
+                                      data->vref_reg);
+       if (ret) {
+               dev_err(&client->dev,
+                       "Failed to add action to managed regulator: %d\n", ret);
+               goto error_disable_reg;

return ret;

Quoted 3 lines

devm_add_action_or_reset() should call max5821_regulator_disable() in case of error

Ok.

Quoted 12 lines
Quoted 12 lines
drivers/iio/dac/max5821.c +3 -1
+       }
+
        ret = regulator_get_voltage(data->vref_reg);
        if (ret < 0) {
                dev_err(&client->dev,
@@ -346,7 +360,7 @@ static int max5821_probe(struct i2c_client *client,
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->info = &max5821_info;
 
-       return iio_device_register(indio_dev);
+       return devm_iio_device_register(&client->dev, indio_dev);
 
Quoted 8 lines
Quoted 5 lines

drivers/iio/dac/max5821.c static int max5821_probe(struct i2c_client *client, · line 365

Quoted 1 line
drivers/iio/dac/max5821.c +0 -0
 error_disable_reg:

This entire goto block should be removed.
The idea of using only devm_ functions is to not have these goto statements.

I thought the action added via devm_add_action (and devres_add) was called only on driver detach, thus the error_disable_reg label would be necessary to handle the

devm_add_action() yes
this is devm_add_action_or_reset() which looks like this:

static inline int devm_add_action_or_reset(struct device *dev,

                       void (*action)(void *), void *data)
{
    int ret;

    ret = devm_add_action(dev, action, data);
    if (ret)
        action(data);

    return ret;
}

it can be found in "include/linux/device.h"

Quoted 3 lines

possible error on regulator_get_voltage. Could you please clarify for me when does
a driver detach happen?

Quoted 10 lines

a driver detach happens when:
* the kmod is unloaded (assuming the driver is running as a kmod)
* manually unbinding the driver from sysfs ;
a quick article about this [it's for USB, but other interfaces use the same mechanism]:
https://lwn.net/Articles/143397/
there should be something under /sys/bus/spi/drivers/xxxx/unbind
* when the system powers down (reboots)
* maybe there is some other new method to do this [that I don't know about]

Thank you so much.

Quoted 8 lines

So, should this goto error_disable_reg be replaced by "regulator_disable(.); return ret;"?

ret = regulator_get_voltage(data->vref_reg);
    if (ret < 0) {
        dev_err(&client->dev,
             "Failed to get voltage on regulator: %d\n", ret);
        goto error_disable_reg;
    }

This is sufficient:
[ No extra regulator_disable() is required; regulator_disable() gets called via max5821_regulator_disable() on the error path ]

       ret = devm_add_action_or_reset(&client->dev, max5821_regulator_disable,
                                      data->vref_reg);
       if (ret) {
               dev_err(&client->dev,
                       "Failed to add action to managed regulator: %d\n", ret);
               return ret;
       }
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

Thanks for your reply,
Théo

Quoted 30 lines
Quoted 30 lines
drivers/iio/dac/max5821.c +0 -13
        regulator_disable(data->vref_reg);
@@ -356,17 +370,6 @@ static int max5821_probe(struct i2c_client *client,
        return ret;
 }
 
-static int max5821_remove(struct i2c_client *client)
-{
-       struct iio_dev *indio_dev = i2c_get_clientdata(client);
-       struct max5821_data *data = iio_priv(indio_dev);
-
-       iio_device_unregister(indio_dev);
-       regulator_disable(data->vref_reg);
-
-       return 0;
-}
-
 static const struct i2c_device_id max5821_id[] = {
        { "max5821", ID_MAX5821 },
        { }
@@ -386,7 +389,6 @@ static struct i2c_driver max5821_driver = {
                .pm     = &max5821_pm_ops,
        },
        .probe          = max5821_probe,
-       .remove         = max5821_remove,
        .id_table       = max5821_id,
 };
 module_i2c_driver(max5821_driver);
--

2.20.1