iio: adc: ti-ads1015: modernize resource management and driver setup
v6 cover iio: adc: ti-ads1015: modernize resource management and driver setup
This series modernizes the ti-ads1015 driver by updating its runtime PM handling, error handling, resource management, and register bitfield operations.
This series builds on top of Stepan Ionichev's devm error path fix [1] and:
- Replaces SET_RUNTIME_PM_OPS() with DEFINE_RUNTIME_DEV_PM_OPS().
- Sorts the driver headers alphabetically.
- Returns -ENODATA when match data is missing.
- Uses a local device pointer in probe.
- Converts suitable error paths to dev_err_probe().
- Drops unnecessary '< 0' error checks for functions returning 0 or an error.
- Converts mutex initialization to devm_mutex_init().
- Transitions runtime PM setup to devm_pm_runtime_set_active_enabled().
- Removes the unnecessary ads1015_set_power_state() helper and uses runtime PM helpers directly at the call sites.
- Inlines the redundant ads1015_set_conv_mode() helper.
- Replaces custom shift macros with FIELD_PREP()/FIELD_GET().
[1] https://lore.kernel.org/all/[email protected]/
---
Changes in v6:
- Added a patch to remove the unnecessary ads1015_set_power_state() helper.
- Incorporated review feedback into the existing patches, including the FIELD_PREP()/FIELD_GET() conversion.
Changes in v5:
https://lore.kernel.org/linux-iio/[email protected]/
Archit Anant (11):
iio: adc: ti-ads1015: use DEFINE_RUNTIME_DEV_PM_OPS()
iio: adc: ti-ads1015: Sort headers alphabetically
iio: adc: ti-ads1015: use local device pointer in probe
iio: adc: ti-ads1015: return -ENODATA when match data is missing
iio: adc: ti-ads1015: use dev_err_probe() for error handling
iio: adc: ti-ads1015: Remove ads1015_set_power_state()
iio: adc: ti-ads1015: drop unneeded < 0 error checks
iio: adc: ti-ads1015: use devm_mutex_init()
iio: adc: ti-ads1015: use managed runtime PM setup
iio: adc: ti-ads1015: inline ads1015_set_conv_mode()
iio: adc: ti-ads1015: Replace custom shift macros with FIELD_PREP/GET
drivers/iio/adc/ti-ads1015.c | 237 +++++++++++++++++------------------ 1 file changed, 115 insertions(+), 122 deletions(-)
Signature · 1 line
2.39.5
PATCH v6 1/11 iio: adc: ti-ads1015: use DEFINE_RUNTIME_DEV_PM_OPS()
Replace the deprecated SET_RUNTIME_PM_OPS() withDEFINE_RUNTIME_DEV_PM_OPS().
This also configures the system sleep PM operations to use the runtime suspend and resume callbacks, ensuring that the device is properly suspended and resumed during system sleep transitions.
The new macro also handles dropping unused functions when CONFIG_PM is disabled, eliminating the need for explicit preprocessor conditionals.
--- drivers/iio/adc/ti-ads1015.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/iio/adc/ti-ads1015.c +5 -7
@@ -1089,7 +1089,6 @@ static int ads1015_probe(struct i2c_client *client)return devm_iio_device_register (&client->dev, indio_dev); } -#ifdef CONFIG_PM static int ads1015_runtime_suspend (struct device *dev) {struct iio_dev *indio_dev =i2c_get_clientdata (to_i2c_client (dev));
@@ -1110,12 +1109,11 @@ static int ads1015_runtime_resume(struct device *dev)return ret; } -#endif -static const struct dev_pm_ops ads1015_pm_ops = { -SET_RUNTIME_PM_OPS (ads1015_runtime_suspend, - ads1015_runtime_resume,NULL ) -}; +static DEFINE_RUNTIME_DEV_PM_OPS(ads1015_pm_ops, + ads1015_runtime_suspend, + ads1015_runtime_resume, + NULL);static const struct ads1015_chip_data ads1015_data = { .channels = ads1015_channels,
@@ -1170,7 +1168,7 @@ static struct i2c_driver ads1015_driver = { .driver = { .name = ADS1015_DRV_NAME, .of_match_table = ads1015_of_match, - .pm = &ads1015_pm_ops, + .pm = pm_ptr(&ads1015_pm_ops), }, .probe = ads1015_probe, .id_table = ads1015_id,
Signature · 1 line
2.39.5
PATCH v6 2/11 iio: adc: ti-ads1015: Sort headers alphabetically
In reply to Archit AnantReorder include headers alphabetically to comply with the preferred coding style and improve maintainability.
Drop the redundant linux/iio/types.h include, as its definitions are already provided through linux/iio/iio.h.
--- drivers/iio/adc/ti-ads1015.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/iio/adc/ti-ads1015.c +8 -9
@@ -11,24 +11,23 @@* * 0x4B - ADDR connected to SCL */ -#include <linux/module.h> #include <linux/cleanup.h> +#include <linux/delay.h> +#include <linux/i2c.h> #include <linux/init.h> #include <linux/irq.h> -#include <linux/i2c.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/pm_runtime.h> #include <linux/property.h> #include <linux/regmap.h> -#include <linux/pm_runtime.h> -#include <linux/mutex.h> -#include <linux/delay.h> +#include <linux/iio/buffer.h> +#include <linux/iio/events.h> #include <linux/iio/iio.h> -#include <linux/iio/types.h> #include <linux/iio/sysfs.h> -#include <linux/iio/events.h> -#include <linux/iio/buffer.h> -#include <linux/iio/triggered_buffer.h> #include <linux/iio/trigger_consumer.h> +#include <linux/iio/triggered_buffer.h> #define ADS1015_DRV_NAME "ads1015"
Signature · 1 line
2.39.5
PATCH v6 3/11 iio: adc: ti-ads1015: use local device pointer in probe
In reply to Archit AnantIntroduce a local device pointer 'dev' in ads1015_probe() to avoid accessing &client->dev repeatedly.
--- drivers/iio/adc/ti-ads1015.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/iio/adc/ti-ads1015.c +10 -9
@@ -966,6 +966,7 @@ static void ads1015_power_down(void *p)static int ads1015_probe (struct i2c_client *client) {const struct ads1015_chip_data *chip; +struct device *dev = &client->dev;struct iio_dev *indio_dev;struct ads1015_data *data;int ret;
@@ -975,7 +976,7 @@ static int ads1015_probe(struct i2c_client *client)if (!chip)return dev_err_probe (&client->dev, -EINVAL ,"Unknown chip\n" ); - indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data)); + indio_dev =devm_iio_device_alloc (dev,sizeof (*data));if (!indio_dev)return -ENOMEM ;
@@ -1022,7 +1023,7 @@ static int ads1015_probe(struct i2c_client *client)return PTR_ERR (data->regmap); } - ret =devm_iio_triggered_buffer_setup (&client->dev, indio_dev,NULL , + ret =devm_iio_triggered_buffer_setup (dev, indio_dev,NULL , ads1015_trigger_handler, &ads1015_buffer_setup_ops);if (ret <0 ) {
@@ -1058,7 +1059,7 @@ static int ads1015_probe(struct i2c_client *client)if (ret)return ret; - ret =devm_request_threaded_irq (&client->dev, client->irq, + ret =devm_request_threaded_irq (dev, client->irq,NULL , ads1015_event_handler, irq_trig |IRQF_ONESHOT , client->name, indio_dev);
@@ -1070,22 +1071,22 @@ static int ads1015_probe(struct i2c_client *client)if (ret)return ret; - ret =devm_add_action_or_reset (&client->dev, ads1015_power_down, data); + ret =devm_add_action_or_reset (dev, ads1015_power_down, data);if (ret)return ret; data->conv_invalid = true; - ret =pm_runtime_set_active (&client->dev); + ret =pm_runtime_set_active (dev);if (ret)return ret; -pm_runtime_set_autosuspend_delay (&client->dev,ADS1015_SLEEP_DELAY_MS ); -pm_runtime_use_autosuspend (&client->dev); - ret =devm_pm_runtime_enable (&client->dev); +pm_runtime_set_autosuspend_delay (dev,ADS1015_SLEEP_DELAY_MS ); +pm_runtime_use_autosuspend (dev); + ret =devm_pm_runtime_enable (dev);if (ret)return ret; -return devm_iio_device_register (&client->dev, indio_dev); +return devm_iio_device_register (dev, indio_dev); }static int ads1015_runtime_suspend (struct device *dev)
Signature · 1 line
2.39.5
PATCH v6 4/11 iio: adc: ti-ads1015: return -ENODATA when match data is missing
In reply to Archit AnantWhen i2c_get_match_data() returns NULL, it indicates that driver match data is absent rather than an invalid argument being provided.
Return -ENODATA instead of -EINVAL to more accurately reflect the failure.
--- drivers/iio/adc/ti-ads1015.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads1015.c +1 -1
@@ -974,7 +974,7 @@ static int ads1015_probe(struct i2c_client *client) chip =i2c_get_match_data (client);if (!chip) -return dev_err_probe (&client->dev, -EINVAL ,"Unknown chip\n" ); +return dev_err_probe (dev, -ENODATA ,"Unknown chip\n" ); indio_dev =devm_iio_device_alloc (dev,sizeof (*data));if (!indio_dev)
Signature · 1 line
2.39.5
PATCH v6 5/11 iio: adc: ti-ads1015: use dev_err_probe() for error handling
In reply to Archit AnantSimplify the error handling paths in ads1015_probe() and ads1015_client_get_channels_config() by converting dev_err() calls that are immediately followed by a return statement over to the modern dev_err_probe() helper.
--- drivers/iio/adc/ti-ads1015.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-)
drivers/iio/adc/ti-ads1015.c +12 -16
@@ -896,18 +896,16 @@ static int ads1015_client_get_channels_config(struct i2c_client *client)if (!fwnode_property_read_u32 (node,"ti,gain" , &pval)) { pga = pval; -if (pga >5 ) { -dev_err (dev,"invalid gain on %pfw\n" , node); -return -EINVAL ; - } +if (pga >5 ) +return dev_err_probe (dev, -EINVAL , +"invalid gain on %pfw\n" , node); }if (!fwnode_property_read_u32 (node,"ti,datarate" , &pval)) { data_rate = pval; -if (data_rate >7 ) { -dev_err (dev,"invalid data_rate on %pfw\n" , node); -return -EINVAL ; - } +if (data_rate >7 ) +return dev_err_probe (dev, -EINVAL , +"invalid data_rate on %pfw\n" , node); }if (fwnode_property_present (node,"label" )) {
@@ -1018,18 +1016,16 @@ static int ads1015_probe(struct i2c_client *client) data->regmap =devm_regmap_init_i2c (client, chip->has_comparator ? &ads1015_regmap_config : &tla2024_regmap_config); -if (IS_ERR (data->regmap)) { -dev_err (&client->dev,"Failed to allocate register map\n" ); -return PTR_ERR (data->regmap); - } +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to allocate register map\n" ); ret =devm_iio_triggered_buffer_setup (dev, indio_dev,NULL , ads1015_trigger_handler, &ads1015_buffer_setup_ops); -if (ret <0 ) { -dev_err (&client->dev,"iio triggered buffer setup failed\n" ); -return ret; - } +if (ret <0 ) +return dev_err_probe (dev, ret, +"iio triggered buffer setup failed\n" );if (client->irq && chip->has_comparator) {unsigned long irq_trig =irq_get_trigger_type (client->irq);
Signature · 1 line
2.39.5