Skip to contentSkip to conversation

iio: adc: ti-ads1015: Remove ads1015_set_power_state()

PATCH In review Archit Anant 3 participants 11 messages Nov 9 – Nov 15 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v6 6/11 iio: adc: ti-ads1015: Remove ads1015_set_power_state()

Remove the unnecessary ads1015_set_power_state() helper and use runtime PM helpers directly at the call sites.

Use PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND() to manage the runtime PM reference in __ads1015_read_info_raw(), ensuring it is automatically released on function exit, including error paths.

For event configuration and buffer operations, retain explicit reference management as the runtime PM reference must remain held beyond the lifetime of the respective function.

Mark the device as last busy before putting it into autosuspend.

---
 drivers/iio/adc/ti-ads1015.c | 59 +++++++++++++++---------------------
 1 file changed, 24 insertions(+), 35 deletions(-)
drivers/iio/adc/ti-ads1015.c +24 -35
index 9fd07ee21ede8..2e6bf45871340 100644
@@ -366,30 +366,6 @@ static const struct iio_chan_spec tla2024_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
 };
 
-
-#ifdef CONFIG_PM
-static int ads1015_set_power_state(struct ads1015_data *data, bool on)
-{
-	int ret;
-	struct device *dev = regmap_get_device(data->regmap);
-
-	if (on)
-		ret = pm_runtime_resume_and_get(dev);
-	else
-		ret = pm_runtime_put_autosuspend(dev);
-
-	return ret < 0 ? ret : 0;
-}
-
-#else /* !CONFIG_PM */
-
-static int ads1015_set_power_state(struct ads1015_data *data, bool on)
-{
-	return 0;
-}
-
-#endif /* !CONFIG_PM */
-
 static
 int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
 {
@@ -532,26 +508,26 @@ static int ads1015_read_avail(struct iio_dev *indio_dev,
 static int __ads1015_read_info_raw(struct ads1015_data *data,
 				   struct iio_chan_spec const *chan, int *val)
 {
+	struct device *dev = regmap_get_device(data->regmap);
 	int ret;
 
 	if (ads1015_event_channel_enabled(data) &&
 	    data->event_channel != chan->address)
 		return -EBUSY;
 
-	ret = ads1015_set_power_state(data, true);
-	if (ret < 0)
+	PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(dev, guard);
+	ret = PM_RUNTIME_ACQUIRE_ERR(&guard);
+	if (ret)
 		return ret;
 
 	ret = ads1015_get_adc_result(data, chan->address, val);
-	if (ret < 0) {
-		ads1015_set_power_state(data, false);
+	if (ret < 0)
 		return ret;
-	}
 
 	*val = sign_extend32(*val >> chan->scan_type.shift,
 			     chan->scan_type.realbits - 1);
 
-	return ads1015_set_power_state(data, false);
+	return 0;
 }
 
 static int ads1015_read_raw(struct iio_dev *indio_dev,
@@ -709,6 +685,7 @@ static int ads1015_enable_event_config(struct ads1015_data *data,
 {
 	int low_thresh = data->thresh_data[chan->address].low_thresh;
 	int high_thresh = data->thresh_data[chan->address].high_thresh;
+	struct device *dev = regmap_get_device(data->regmap);
 	int ret;
 	unsigned int val;
 
@@ -735,7 +712,7 @@ static int ads1015_enable_event_config(struct ads1015_data *data,
 	if (ret)
 		return ret;
 
-	ret = ads1015_set_power_state(data, true);
+	ret = pm_runtime_resume_and_get(dev);
 	if (ret < 0)
 		return ret;
 
@@ -744,7 +721,8 @@ static int ads1015_enable_event_config(struct ads1015_data *data,
 	ret = ads1015_get_adc_result(data, chan->address, &val);
 	if (ret) {
 		ads1015_event_channel_disable(data, chan->address);
-		ads1015_set_power_state(data, false);
+		pm_runtime_mark_last_busy(dev);
+		pm_runtime_put_autosuspend(dev);
 	}
 
 	return ret;
@@ -753,6 +731,7 @@ static int ads1015_enable_event_config(struct ads1015_data *data,
 static int ads1015_disable_event_config(struct ads1015_data *data,
 	const struct iio_chan_spec *chan, int comp_mode)
 {
+	struct device *dev = regmap_get_device(data->regmap);
 	int ret;
 
 	if (!ads1015_event_channel_enabled(data))
@@ -774,7 +753,10 @@ static int ads1015_disable_event_config(struct ads1015_data *data,
 
 	ads1015_event_channel_disable(data, chan->address);
 
-	return ads1015_set_power_state(data, false);
+	pm_runtime_mark_last_busy(dev);
+	pm_runtime_put_autosuspend(dev);
+
+	return 0;
 }
 
 static int ads1015_write_event_config(struct iio_dev *indio_dev,
@@ -830,17 +812,24 @@ static irqreturn_t ads1015_event_handler(int irq, void *priv)
 static int ads1015_buffer_preenable(struct iio_dev *indio_dev)
 {
 	struct ads1015_data *data = iio_priv(indio_dev);
+	struct device *dev = regmap_get_device(data->regmap);
 
 	/* Prevent from enabling both buffer and event at a time */
 	if (ads1015_event_channel_enabled(data))
 		return -EBUSY;
 
-	return ads1015_set_power_state(iio_priv(indio_dev), true);
+	return pm_runtime_resume_and_get(dev);
 }
 
 static int ads1015_buffer_postdisable(struct iio_dev *indio_dev)
 {
-	return ads1015_set_power_state(iio_priv(indio_dev), false);
+	struct ads1015_data *data = iio_priv(indio_dev);
+	struct device *dev = regmap_get_device(data->regmap);
+
+	pm_runtime_mark_last_busy(dev);
+	pm_runtime_put_autosuspend(dev);
+
+	return 0;
 }
 
 static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {
Signature · 1 line
2.39.5
#1 lore raw

PATCH v6 7/11 iio: adc: ti-ads1015: drop unneeded < 0 error checks

The devm_iio_triggered_buffer_setup() function returns 0 on success and a negative error code on failure.

Use a simple boolean check instead of explicitly checking for a negative return value.

---
 drivers/iio/adc/ti-ads1015.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads1015.c +1 -1
index 2e6bf45871340..1924c1b2abeea 100644
@@ -1012,7 +1012,7 @@ static int ads1015_probe(struct i2c_client *client)
 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
 					      ads1015_trigger_handler,
 					      &ads1015_buffer_setup_ops);
-	if (ret < 0)
+	if (ret)
 		return dev_err_probe(dev, ret,
 				     "iio triggered buffer setup failed\n");
 
Signature · 1 line
2.39.5
Reviewed-by on v6 7/11
#2 lore raw

Re: [PATCH v6 07/11] iio: adc: ti-ads1015: drop unneeded < 0 error checks

Quoted 5 lines · Archit Anant

The devm_iio_triggered_buffer_setup() function returns 0 on success and a negative error code on failure.

Use a simple boolean check instead of explicitly checking for a negative return value.

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

PATCH v6 8/11 iio: adc: ti-ads1015: use devm_mutex_init()

In reply to Archit Anant

Convert mutex_init() to devm_mutex_init(). This ensures that the mutex is automatically destroyed when the device is unbound, and simplifies error handling.

---
 drivers/iio/adc/ti-ads1015.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/adc/ti-ads1015.c +3 -1
index 1924c1b2abeea..a65da3742bec2 100644
@@ -970,7 +970,9 @@ static int ads1015_probe(struct i2c_client *client)
 	data = iio_priv(indio_dev);
 	i2c_set_clientdata(client, indio_dev);
 
-	mutex_init(&data->lock);
+	ret = devm_mutex_init(dev, &data->lock);
+	if (ret)
+		return ret;
 
 	indio_dev->name = ADS1015_DRV_NAME;
 	indio_dev->modes = INDIO_DIRECT_MODE;
Signature · 1 line
2.39.5
#4 lore raw

PATCH v6 9/11 iio: adc: ti-ads1015: use managed runtime PM setup

In reply to Archit Anant

Use devm_pm_runtime_set_active_enabled() to combine setting the device active and enabling runtime PM.

Previously, probe explicitly called pm_runtime_set_active() followed by devm_pm_runtime_enable(). The managed helper changes the teardown semantics by associating runtime PM enablement with the device-managed lifetime.

The runtime suspend and resume callbacks only update the conversion-mode bits through regmap. They do not manipulate runtime-PM usage counts or other shared resources such as regulators or clocks, so this change does not introduce an additional runtime-PM reference or resource-balance operation in this driver.

---
 drivers/iio/adc/ti-ads1015.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/adc/ti-ads1015.c +1 -4
index a65da3742bec2..93a8318e950e2 100644
@@ -1064,12 +1064,9 @@ static int ads1015_probe(struct i2c_client *client)
 
 	data->conv_invalid = true;
 
-	ret = pm_runtime_set_active(dev);
-	if (ret)
-		return ret;
 	pm_runtime_set_autosuspend_delay(dev, ADS1015_SLEEP_DELAY_MS);
 	pm_runtime_use_autosuspend(dev);
-	ret = devm_pm_runtime_enable(dev);
+	ret = devm_pm_runtime_set_active_enabled(dev);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.39.5
#5 lore raw

PATCH v6 10/11 iio: adc: ti-ads1015: inline ads1015_set_conv_mode()

In reply to Archit Anant

The ads1015_set_conv_mode() helper function is a single-line wrapper around regmap_update_bits().

Since the function is simple and only called during initialization and power management state transitions, the wrapper is unnecessary. Remove it and inline the regmap_update_bits() logic directly at the call sites to improve code readability and traceabilty.

Where applicable, introduce a local 'map' pointer to keep the inlined function calls concise.

---
 drivers/iio/adc/ti-ads1015.c | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
drivers/iio/adc/ti-ads1015.c +17 -15
index 93a8318e950e2..2c3ba1b91aac4 100644
@@ -932,21 +932,16 @@ static void ads1015_get_channels_config(struct i2c_client *client)
 	}
 }
 
-static int ads1015_set_conv_mode(struct ads1015_data *data, int mode)
-{
-	return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
-				  ADS1015_CFG_MOD_MASK,
-				  mode << ADS1015_CFG_MOD_SHIFT);
-}
-
 static void ads1015_power_down(void *p)
 {
 	struct ads1015_data *data = p;
+	struct regmap *map = data->regmap;
 	int ret;
 
-	ret = ads1015_set_conv_mode(data, ADS1015_SINGLESHOT);
+	ret = regmap_update_bits(map, ADS1015_CFG_REG, ADS1015_CFG_MOD_MASK,
+				 ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
 	if (ret)
-		dev_warn(regmap_get_device(data->regmap),
+		dev_warn(regmap_get_device(map),
 			 "Failed to power down (%pe)\n", ERR_PTR(ret));
 }
 
@@ -956,6 +951,7 @@ static int ads1015_probe(struct i2c_client *client)
 	struct device *dev = &client->dev;
 	struct iio_dev *indio_dev;
 	struct ads1015_data *data;
+	struct regmap *map;
 	int ret;
 	int i;
 
@@ -1007,8 +1003,9 @@ 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))
-		return dev_err_probe(dev, PTR_ERR(data->regmap),
+	map = data->regmap;
+	if (IS_ERR(map))
+		return dev_err_probe(dev, PTR_ERR(map),
 				     "Failed to allocate register map\n");
 
 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
@@ -1041,7 +1038,7 @@ static int ads1015_probe(struct i2c_client *client)
 			return -EINVAL;
 		}
 
-		ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
+		ret = regmap_update_bits(map, ADS1015_CFG_REG,
 					cfg_comp_mask, cfg_comp);
 		if (ret)
 			return ret;
@@ -1054,7 +1051,8 @@ static int ads1015_probe(struct i2c_client *client)
 			return ret;
 	}
 
-	ret = ads1015_set_conv_mode(data, ADS1015_CONTINUOUS);
+	ret = regmap_update_bits(map, ADS1015_CFG_REG, ADS1015_CFG_MOD_MASK,
+				 ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT);
 	if (ret)
 		return ret;
 
@@ -1077,17 +1075,21 @@ static int ads1015_runtime_suspend(struct device *dev)
 {
 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
 	struct ads1015_data *data = iio_priv(indio_dev);
+	struct regmap *map = data->regmap;
 
-	return ads1015_set_conv_mode(data, ADS1015_SINGLESHOT);
+	return regmap_update_bits(map, ADS1015_CFG_REG, ADS1015_CFG_MOD_MASK,
+				  ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
 }
 
 static int ads1015_runtime_resume(struct device *dev)
 {
 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
 	struct ads1015_data *data = iio_priv(indio_dev);
+	struct regmap *map = data->regmap;
 	int ret;
 
-	ret = ads1015_set_conv_mode(data, ADS1015_CONTINUOUS);
+	ret = regmap_update_bits(map, ADS1015_CFG_REG, ADS1015_CFG_MOD_MASK,
+				 ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT);
 	if (!ret)
 		data->conv_invalid = true;
 
Signature · 1 line
2.39.5
maintainer
#6 lore raw

Re: [PATCH v6 10/11] iio: adc: ti-ads1015: inline ads1015_set_conv_mode()

Quoted 21 lines · On Mon, 9 Nov 2026 22:28:04 +0530 Archit Anant <[email protected]> wrote:

The ads1015_set_conv_mode() helper function is a single-line wrapper around regmap_update_bits().

Since the function is simple and only called during initialization and power management state transitions, the wrapper is unnecessary. Remove it and inline the regmap_update_bits() logic directly at the call sites to improve code readability and traceabilty.

Where applicable, introduce a local 'map' pointer to keep the inlined function calls concise.

---
 drivers/iio/adc/ti-ads1015.c | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
drivers/iio/adc/ti-ads1015.c +0 -0
index 93a8318e950e2..2c3ba1b91aac4 100644

...

Quoted 9 lines
drivers/iio/adc/ti-ads1015.c +1 -0
@@ -956,6 +951,7 @@ static int ads1015_probe(struct i2c_client *client)
 	struct device *dev = &client->dev;
 	struct iio_dev *indio_dev;
 	struct ads1015_data *data;
+	struct regmap *map;
 	int ret;
 	int i;
 

drivers/iio/adc/ti-ads1015.c static int ads1015_probe(struct i2c_client *client) · line 1006

Quoted 6 lines
drivers/iio/adc/ti-ads1015.c +1 -2
@@ -1007,8 +1003,9 @@ 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))
-		return dev_err_probe(dev, PTR_ERR(data->regmap),
+	map = data->regmap;

Where it is easy enough to do, don't put error pointers into structures.
So

	map = devm_regmap_init_i2c();
	if (IS_ERR(map))
		return dev_err_probe(dev, PTR_ERR(map),
				     "Failed to allocate register map\n");

	data->regmap = map;

I'm not advocating whole scale cleanup of other drivers for this as it is churn heavy but here it makes the code you are changing anyway better.

Quoted 5 lines
drivers/iio/adc/ti-ads1015.c +2 -0
+	if (IS_ERR(map))
+		return dev_err_probe(dev, PTR_ERR(map),
 				     "Failed to allocate register map\n");
 
 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
#7 lore raw

PATCH v6 11/11 iio: adc: ti-ads1015: Replace custom shift macros with FIELD_PREP/GET

In reply to Archit Anant

The driver currently uses custom _SHIFT macros and manual bitwise operations to read and write to the configuration register.

Modernize the bitwise logic by replacing all manual shifts with the standard FIELD_PREP() and FIELD_GET() macros from <linux/bitfield.h>. This improves readability and prevents potential bitwise errors.

Remove the now-unused _SHIFT macros from the top of the file.

---
 drivers/iio/adc/ti-ads1015.c | 79 ++++++++++++++++++++----------------
 1 file changed, 44 insertions(+), 35 deletions(-)
drivers/iio/adc/ti-ads1015.c +44 -35
index 2c3ba1b91aac4..8f7ec61314363 100644
@@ -11,6 +11,7 @@
  *	* 0x4B - ADDR connected to SCL
  */
 
+#include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
@@ -38,15 +39,6 @@
 #define ADS1015_LO_THRESH_REG	0x02
 #define ADS1015_HI_THRESH_REG	0x03
 
-#define ADS1015_CFG_COMP_QUE_SHIFT	0
-#define ADS1015_CFG_COMP_LAT_SHIFT	2
-#define ADS1015_CFG_COMP_POL_SHIFT	3
-#define ADS1015_CFG_COMP_MODE_SHIFT	4
-#define ADS1015_CFG_DR_SHIFT	5
-#define ADS1015_CFG_MOD_SHIFT	8
-#define ADS1015_CFG_PGA_SHIFT	9
-#define ADS1015_CFG_MUX_SHIFT	12
-
 #define ADS1015_CFG_COMP_QUE_MASK	GENMASK(1, 0)
 #define ADS1015_CFG_COMP_LAT_MASK	BIT(2)
 #define ADS1015_CFG_COMP_POL_MASK	BIT(3)
@@ -382,17 +374,24 @@ int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
 
 	pga = data->channel_data[chan].pga;
 	dr = data->channel_data[chan].data_rate;
-	mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
-		ADS1015_CFG_DR_MASK;
-	cfg = chan << ADS1015_CFG_MUX_SHIFT | pga << ADS1015_CFG_PGA_SHIFT |
-		dr << ADS1015_CFG_DR_SHIFT;
 
 	if (ads1015_event_channel_enabled(data)) {
-		mask |= ADS1015_CFG_COMP_QUE_MASK | ADS1015_CFG_COMP_MODE_MASK;
-		cfg |= data->thresh_data[chan].comp_queue <<
-				ADS1015_CFG_COMP_QUE_SHIFT |
-			data->comp_mode <<
-				ADS1015_CFG_COMP_MODE_SHIFT;
+		mask = ADS1015_CFG_COMP_QUE_MASK | ADS1015_CFG_COMP_MODE_MASK |
+		       ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
+		       ADS1015_CFG_DR_MASK;
+		cfg = FIELD_PREP(ADS1015_CFG_MUX_MASK, chan) |
+		      FIELD_PREP(ADS1015_CFG_PGA_MASK, pga) |
+		      FIELD_PREP(ADS1015_CFG_DR_MASK, dr) |
+		      FIELD_PREP(ADS1015_CFG_COMP_QUE_MASK,
+				 data->thresh_data[chan].comp_queue) |
+		      FIELD_PREP(ADS1015_CFG_COMP_MODE_MASK,
+				 data->comp_mode);
+	} else {
+		mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
+			ADS1015_CFG_DR_MASK;
+		cfg = FIELD_PREP(ADS1015_CFG_MUX_MASK, chan) |
+		      FIELD_PREP(ADS1015_CFG_PGA_MASK, pga) |
+		      FIELD_PREP(ADS1015_CFG_DR_MASK, dr);
 	}
 
 	cfg = (old & ~mask) | (cfg & mask);
@@ -403,7 +402,7 @@ int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
 		data->conv_invalid = true;
 	}
 	if (data->conv_invalid) {
-		dr_old = (old & ADS1015_CFG_DR_MASK) >> ADS1015_CFG_DR_SHIFT;
+		dr_old = FIELD_GET(ADS1015_CFG_DR_MASK, old);
 		conv_time = DIV_ROUND_UP(USEC_PER_SEC, data_rate[dr_old]);
 		conv_time += DIV_ROUND_UP(USEC_PER_SEC, data_rate[dr]);
 		conv_time += conv_time / 10; /* 10% internal clock inaccuracy */
@@ -731,7 +730,9 @@ static int ads1015_enable_event_config(struct ads1015_data *data,
 static int ads1015_disable_event_config(struct ads1015_data *data,
 	const struct iio_chan_spec *chan, int comp_mode)
 {
-	struct device *dev = regmap_get_device(data->regmap);
+	struct regmap *map = data->regmap;
+	struct device *dev = regmap_get_device(map);
+	unsigned int val;
 	int ret;
 
 	if (!ads1015_event_channel_enabled(data))
@@ -744,10 +745,9 @@ static int ads1015_disable_event_config(struct ads1015_data *data,
 			comp_mode == ADS1015_CFG_COMP_MODE_WINDOW)
 		return 0;
 
-	ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
-				ADS1015_CFG_COMP_QUE_MASK,
-				ADS1015_CFG_COMP_DISABLE <<
-					ADS1015_CFG_COMP_QUE_SHIFT);
+	val = FIELD_PREP(ADS1015_CFG_COMP_QUE_MASK, ADS1015_CFG_COMP_DISABLE);
+	ret = regmap_update_bits(map, ADS1015_CFG_REG,
+				 ADS1015_CFG_COMP_QUE_MASK, val);
 	if (ret)
 		return ret;
 
@@ -936,10 +936,12 @@ static void ads1015_power_down(void *p)
 {
 	struct ads1015_data *data = p;
 	struct regmap *map = data->regmap;
+	unsigned int val;
 	int ret;
 
-	ret = regmap_update_bits(map, ADS1015_CFG_REG, ADS1015_CFG_MOD_MASK,
-				 ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
+	val = FIELD_PREP(ADS1015_CFG_MOD_MASK, ADS1015_SINGLESHOT);
+	ret = regmap_update_bits(map, ADS1015_CFG_REG,
+				 ADS1015_CFG_MOD_MASK, val);
 	if (ret)
 		dev_warn(regmap_get_device(map),
 			 "Failed to power down (%pe)\n", ERR_PTR(ret));
@@ -951,6 +953,7 @@ static int ads1015_probe(struct i2c_client *client)
 	struct device *dev = &client->dev;
 	struct iio_dev *indio_dev;
 	struct ads1015_data *data;
+	unsigned int cfg_mode;
 	struct regmap *map;
 	int ret;
 	int i;
@@ -1020,19 +1023,20 @@ static int ads1015_probe(struct i2c_client *client)
 		unsigned int cfg_comp_mask = ADS1015_CFG_COMP_QUE_MASK |
 			ADS1015_CFG_COMP_LAT_MASK | ADS1015_CFG_COMP_POL_MASK;
 		unsigned int cfg_comp =
-			ADS1015_CFG_COMP_DISABLE << ADS1015_CFG_COMP_QUE_SHIFT |
-			1 << ADS1015_CFG_COMP_LAT_SHIFT;
+			FIELD_PREP(ADS1015_CFG_COMP_QUE_MASK,
+				   ADS1015_CFG_COMP_DISABLE) |
+			FIELD_PREP(ADS1015_CFG_COMP_LAT_MASK, 1);
 
 		switch (irq_trig) {
 		case IRQF_TRIGGER_FALLING:
 		case IRQF_TRIGGER_LOW:
-			cfg_comp |= ADS1015_CFG_COMP_POL_LOW <<
-					ADS1015_CFG_COMP_POL_SHIFT;
+			cfg_comp |= FIELD_PREP(ADS1015_CFG_COMP_POL_MASK,
+					       ADS1015_CFG_COMP_POL_LOW);
 			break;
 		case IRQF_TRIGGER_HIGH:
 		case IRQF_TRIGGER_RISING:
-			cfg_comp |= ADS1015_CFG_COMP_POL_HIGH <<
-					ADS1015_CFG_COMP_POL_SHIFT;
+			cfg_comp |= FIELD_PREP(ADS1015_CFG_COMP_POL_MASK,
+					       ADS1015_CFG_COMP_POL_HIGH);
 			break;
 		default:
 			return -EINVAL;
@@ -1051,8 +1055,9 @@ static int ads1015_probe(struct i2c_client *client)
 			return ret;
 	}
 
+	cfg_mode = FIELD_PREP(ADS1015_CFG_MOD_MASK, ADS1015_CONTINUOUS);
 	ret = regmap_update_bits(map, ADS1015_CFG_REG, ADS1015_CFG_MOD_MASK,
-				 ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT);
+				 cfg_mode);
 	if (ret)
 		return ret;
 
@@ -1076,9 +1081,11 @@ static int ads1015_runtime_suspend(struct device *dev)
 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
 	struct ads1015_data *data = iio_priv(indio_dev);
 	struct regmap *map = data->regmap;
+	unsigned int val;
 
+	val = FIELD_PREP(ADS1015_CFG_MOD_MASK, ADS1015_SINGLESHOT);
 	return regmap_update_bits(map, ADS1015_CFG_REG, ADS1015_CFG_MOD_MASK,
-				  ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
+				  val);
 }
 
 static int ads1015_runtime_resume(struct device *dev)
@@ -1086,10 +1093,12 @@ static int ads1015_runtime_resume(struct device *dev)
 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
 	struct ads1015_data *data = iio_priv(indio_dev);
 	struct regmap *map = data->regmap;
+	unsigned int val;
 	int ret;
 
+	val = FIELD_PREP(ADS1015_CFG_MOD_MASK, ADS1015_CONTINUOUS);
 	ret = regmap_update_bits(map, ADS1015_CFG_REG, ADS1015_CFG_MOD_MASK,
-				 ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT);
+				 val);
 	if (!ret)
 		data->conv_invalid = true;
 
Signature · 1 line
2.39.5
Reviewed-by on v6 11/11
#8 lore raw

Re: [PATCH v6 11/11] iio: adc: ti-ads1015: Replace custom shift macros with FIELD_PREP/GET

Quoted 8 lines · Archit Anant

The driver currently uses custom _SHIFT macros and manual bitwise operations to read and write to the configuration register.

Modernize the bitwise logic by replacing all manual shifts with the standard FIELD_PREP() and FIELD_GET() macros from <linux/bitfield.h>. This improves readability and prevents potential bitwise errors.

Remove the now-unused _SHIFT macros from the top of the file.

...

drivers/iio/adc/ti-ads1015.c int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val) · line 391

Quoted 2 lines
drivers/iio/adc/ti-ads1015.c +2 -0
+		mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
+			ADS1015_CFG_DR_MASK;

Indentation is broken here.

Quoted 3 lines
drivers/iio/adc/ti-ads1015.c +3 -0
+		cfg = FIELD_PREP(ADS1015_CFG_MUX_MASK, chan) |
+		      FIELD_PREP(ADS1015_CFG_PGA_MASK, pga) |
+		      FIELD_PREP(ADS1015_CFG_DR_MASK, dr);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#9 lore raw

Re: [PATCH v6 11/11] iio: adc: ti-ads1015: Replace custom shift macros with FIELD_PREP/GET

In reply to Archit Anant
Quoted 45 lines · On Mon, 9 Nov 2026 22:28:05 +0530 Archit Anant <[email protected]> wrote:

The driver currently uses custom _SHIFT macros and manual bitwise operations to read and write to the configuration register.

Modernize the bitwise logic by replacing all manual shifts with the standard FIELD_PREP() and FIELD_GET() macros from <linux/bitfield.h>. This improves readability and prevents potential bitwise errors.

Remove the now-unused _SHIFT macros from the top of the file.

---
 drivers/iio/adc/ti-ads1015.c | 79 ++++++++++++++++++++----------------
 1 file changed, 44 insertions(+), 35 deletions(-)
drivers/iio/adc/ti-ads1015.c +1 -9
index 2c3ba1b91aac4..8f7ec61314363 100644
@@ -11,6 +11,7 @@
  *	* 0x4B - ADDR connected to SCL
  */
 
+#include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
@@ -38,15 +39,6 @@
 #define ADS1015_LO_THRESH_REG	0x02
 #define ADS1015_HI_THRESH_REG	0x03
 
-#define ADS1015_CFG_COMP_QUE_SHIFT	0
-#define ADS1015_CFG_COMP_LAT_SHIFT	2
-#define ADS1015_CFG_COMP_POL_SHIFT	3
-#define ADS1015_CFG_COMP_MODE_SHIFT	4
-#define ADS1015_CFG_DR_SHIFT	5
-#define ADS1015_CFG_MOD_SHIFT	8
-#define ADS1015_CFG_PGA_SHIFT	9
-#define ADS1015_CFG_MUX_SHIFT	12
-
 #define ADS1015_CFG_COMP_QUE_MASK	GENMASK(1, 0)
 #define ADS1015_CFG_COMP_LAT_MASK	BIT(2)
 #define ADS1015_CFG_COMP_POL_MASK	BIT(3)

drivers/iio/adc/ti-ads1015.c int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val) · line 377

Quoted 8 lines
drivers/iio/adc/ti-ads1015.c +0 -4
@@ -382,17 +374,24 @@ int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
 
 	pga = data->channel_data[chan].pga;
 	dr = data->channel_data[chan].data_rate;
-	mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
-		ADS1015_CFG_DR_MASK;
-	cfg = chan << ADS1015_CFG_MUX_SHIFT | pga << ADS1015_CFG_PGA_SHIFT |
-		dr << ADS1015_CFG_DR_SHIFT;
 

I'm probably being inconsistent across versions, but at least looking at it this time, I prefer the building up common cfg and mask first, then adding the extra bits in the following.

Quoted 23 lines
drivers/iio/adc/ti-ads1015.c +16 -5
 	if (ads1015_event_channel_enabled(data)) {
-		mask |= ADS1015_CFG_COMP_QUE_MASK | ADS1015_CFG_COMP_MODE_MASK;
-		cfg |= data->thresh_data[chan].comp_queue <<
-				ADS1015_CFG_COMP_QUE_SHIFT |
-			data->comp_mode <<
-				ADS1015_CFG_COMP_MODE_SHIFT;
+		mask = ADS1015_CFG_COMP_QUE_MASK | ADS1015_CFG_COMP_MODE_MASK |
+		       ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
+		       ADS1015_CFG_DR_MASK;
+		cfg = FIELD_PREP(ADS1015_CFG_MUX_MASK, chan) |
+		      FIELD_PREP(ADS1015_CFG_PGA_MASK, pga) |
+		      FIELD_PREP(ADS1015_CFG_DR_MASK, dr) |
+		      FIELD_PREP(ADS1015_CFG_COMP_QUE_MASK,
+				 data->thresh_data[chan].comp_queue) |
+		      FIELD_PREP(ADS1015_CFG_COMP_MODE_MASK,
+				 data->comp_mode);
+	} else {
+		mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
+			ADS1015_CFG_DR_MASK;
+		cfg = FIELD_PREP(ADS1015_CFG_MUX_MASK, chan) |
+		      FIELD_PREP(ADS1015_CFG_PGA_MASK, pga) |
+		      FIELD_PREP(ADS1015_CFG_DR_MASK, dr);
 	}
maintainer
#10 lore raw
In reply to Archit Anant
Quoted 8 lines · On Mon, 9 Nov 2026 22:28:00 +0530 Archit Anant <[email protected]> wrote:

Remove the unnecessary ads1015_set_power_state() helper and use runtime PM helpers directly at the call sites.

Use PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND() to manage the runtime PM reference in __ads1015_read_info_raw(), ensuring it is automatically released on function exit, including error paths.

Quoted 7 lines

For event configuration and buffer operations, retain explicit reference management as the runtime PM reference must remain held beyond the lifetime of the respective function.

Mark the device as last busy before putting it into autosuspend.

Glad you called this out as otherwise I might have not noticed. See comment below. How this works changed a while back.

drivers/iio/adc/ti-ads1015.c static irqreturn_t ads1015_event_handler(int irq, void *priv) · line 829

Quoted 11 lines

static int ads1015_buffer_postdisable(struct iio_dev *indio_dev) {

drivers/iio/adc/ti-ads1015.c +4 -1
-	return ads1015_set_power_state(iio_priv(indio_dev), false);
+	struct ads1015_data *data = iio_priv(indio_dev);
+	struct device *dev = regmap_get_device(data->regmap);
+
+	pm_runtime_mark_last_busy(dev);

Take a look at what upstream pm_runtime_put_autosuspend() does.

Quoted 6 lines
drivers/iio/adc/ti-ads1015.c +3 -0
+	pm_runtime_put_autosuspend(dev);
+
+	return 0;
 }
 
 static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {