Skip to contentSkip to conversation

iio: adc: aspeed: Reserve battery sensing channel for on-demand use

PATCH In review Billy Tsai 4 participants 9 messages Mar 20 – Mar 21

Subsystems (2)
lore
Share & tools
lore

Billy Tsai

author reviewer
#0 lore raw

v3 cover iio: adc: aspeed: Reserve battery sensing channel for on-demand use

For controllers with battery sensing capability (AST2600/AST2700), the last channel uses a different circuit design optimized for battery voltage measurement. This channel should not be enabled by default along with other channels to avoid potential interference and power efficiency issues.

Changes made:
- Introduce aspeed_adc_get_active_channels() to return the number of channels that should be enabled by default
- For battery sensing capable controllers, exclude the last channel from the default channel enable mask
- Enable the battery sensing channel only when explicitly accessed via read_raw()
- Replace hardcoded channel numbers with ASPEED_ADC_BATTERY_CHANNEL macro
- Add helper functions for cleaner channel management

This ensures optimal power efficiency for normal ADC operations while maintaining full functionality when battery sensing is needed.

---
Changes in v3:
- Simplify channel mask helper as suggested by Andy
- Use FIELD_MODIFY() when updating ADC channel field instead of open-coded OR
- Move unrelated blank line change to the previous patch
- Replace mdelay(1) with fsleep(1000) since the delay is in sleepable context
- Fix comment typo ("adc" -> "ADC")
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Split the changes into a series of patches for better clarity and review
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Billy Tsai (4):
iio: adc: Add battery channel definition for ADC
iio: adc: Enable multiple consecutive channels based on model data
iio: adc: aspeed: Replace mdelay() with fsleep() for ADC stabilization delay
iio: adc: aspeed: Reserve battery sensing channel for on-demand use

 drivers/iio/adc/aspeed_adc.c | 68 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 57 insertions(+), 11 deletions(-)

---
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20260313-adc-479b0ab09bae

Best regards,

Signature · 1 line
Billy Tsai <[email protected]>
author reviewer
#1 lore raw

PATCH v3 1/4 iio: adc: Add battery channel definition for ADC

Defines a constant for the battery sensing channel, typically the last channel of the ADC. Clarifies channel usage and improves code readability.

---
 drivers/iio/adc/aspeed_adc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/iio/adc/aspeed_adc.c +4 -2
index 4be44c524b4d..8eebaa3dc534 100644
@@ -75,6 +75,8 @@
 
 #define ASPEED_ADC_INIT_POLLING_TIME	500
 #define ASPEED_ADC_INIT_TIMEOUT		500000
+/* Battery sensing is typically on the last channel */
+#define ASPEED_ADC_BATTERY_CHANNEL		7
 /*
  * When the sampling rate is too high, the ADC may not have enough charging
  * time, resulting in a low voltage value. Thus, the default uses a slow
@@ -285,7 +287,7 @@ static int aspeed_adc_read_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		if (data->battery_sensing && chan->channel == 7) {
+		if (data->battery_sensing && chan->channel == ASPEED_ADC_BATTERY_CHANNEL) {
 			adc_engine_control_reg_val =
 				readl(data->base + ASPEED_REG_ENGINE_CONTROL);
 			writel(adc_engine_control_reg_val |
@@ -309,7 +311,7 @@ static int aspeed_adc_read_raw(struct iio_dev *indio_dev,
 		return IIO_VAL_INT;
 
 	case IIO_CHAN_INFO_OFFSET:
-		if (data->battery_sensing && chan->channel == 7)
+		if (data->battery_sensing && chan->channel == ASPEED_ADC_BATTERY_CHANNEL)
 			*val = (data->cv * data->battery_mode_gain.mult) /
 			       data->battery_mode_gain.div;
 		else
Signature · 1 line
2.34.1
author reviewer
#2 lore raw

PATCH v3 2/4 iio: adc: Enable multiple consecutive channels based on model data

In reply to Billy Tsai

Add helpers to generate channel masks and enable multiple ADC channels according to the device model's channel count.

---
 drivers/iio/adc/aspeed_adc.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
drivers/iio/adc/aspeed_adc.c +21 -1
index 8eebaa3dc534..3ff24474f394 100644
@@ -123,6 +123,24 @@ struct aspeed_adc_data {
 	struct adc_gain		battery_mode_gain;
 };
 
+/*
+ * Enable multiple consecutive channels starting from channel 0.
+ * This creates a bitmask for channels 0 to (num_channels - 1).
+ * For example: num_channels=3 creates mask 0x0007 (channels 0,1,2)
+ */
+static inline u32 aspeed_adc_channels_mask(unsigned int num_channels)
+{
+	if (num_channels > 16)
+		return GENMASK(15, 0);
+
+	return BIT(num_channels) - 1;
+}
+
+static inline unsigned int aspeed_adc_get_active_channels(const struct aspeed_adc_data *data)
+{
+	return data->model_data->num_channels;
+}
+
 #define ASPEED_CHAN(_idx, _data_reg_addr) {			\
 	.type = IIO_VOLTAGE,					\
 	.indexed = 1,						\
@@ -612,7 +630,9 @@ static int aspeed_adc_probe(struct platform_device *pdev)
 	/* Start all channels in normal mode. */
 	adc_engine_control_reg_val =
 		readl(data->base + ASPEED_REG_ENGINE_CONTROL);
-	adc_engine_control_reg_val |= ASPEED_ADC_CTRL_CHANNEL;
+	FIELD_MODIFY(ASPEED_ADC_CTRL_CHANNEL, &adc_engine_control_reg_val,
+		     aspeed_adc_channels_mask(aspeed_adc_get_active_channels(data)));
+
 	writel(adc_engine_control_reg_val,
 	       data->base + ASPEED_REG_ENGINE_CONTROL);
 
Signature · 1 line
2.34.1
author reviewer
#3 lore raw

PATCH v3 3/4 iio: adc: aspeed: Replace mdelay() with fsleep() for ADC stabilization delay

In reply to Billy Tsai

The ADC stabilization delays in compensation mode and battery sensing mode do not require atomic context. Using mdelay() here results in unnecessary busy waiting.

Replace mdelay(1) with fsleep(1000) to allow the scheduler to run other tasks while waiting for the ADC to stabilize.

Also fix a minor typo in the comment ("adc" -> "ADC").

---
 drivers/iio/adc/aspeed_adc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/adc/aspeed_adc.c +3 -3
index 3ff24474f394..a1a6296d3003 100644
@@ -259,7 +259,7 @@ static int aspeed_adc_compensation(struct iio_dev *indio_dev)
 	 * After enable compensating sensing mode need to wait some time for ADC stable
 	 * Experiment result is 1ms.
 	 */
-	mdelay(1);
+	fsleep(1000);
 
 	for (index = 0; index < 16; index++) {
 		/*
@@ -314,10 +314,10 @@ static int aspeed_adc_read_raw(struct iio_dev *indio_dev,
 				       ASPEED_ADC_BAT_SENSING_ENABLE,
 			       data->base + ASPEED_REG_ENGINE_CONTROL);
 			/*
-			 * After enable battery sensing mode need to wait some time for adc stable
+			 * After enable battery sensing mode need to wait some time for ADC stable
 			 * Experiment result is 1ms.
 			 */
-			mdelay(1);
+			fsleep(1000);
 			*val = readw(data->base + chan->address);
 			*val = (*val * data->battery_mode_gain.mult) /
 			       data->battery_mode_gain.div;
Signature · 1 line
2.34.1
author reviewer
#6 lore raw

PATCH v3 4/4 iio: adc: aspeed: Reserve battery sensing channel for on-demand use

In reply to Billy Tsai

For controllers with battery sensing capability (AST2600/AST2700), the last channel uses a different circuit design optimized for battery voltage measurement. This channel should not be enabled by default along with other channels to avoid potential interference and power efficiency issues.
This ensures optimal power efficiency for normal ADC operations while maintaining full functionality when battery sensing is needed.

---
 drivers/iio/adc/aspeed_adc.c | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)
drivers/iio/adc/aspeed_adc.c +29 -5
index a1a6296d3003..b3dee172adbf 100644
@@ -138,6 +138,13 @@ static inline u32 aspeed_adc_channels_mask(unsigned int num_channels)
 
 static inline unsigned int aspeed_adc_get_active_channels(const struct aspeed_adc_data *data)
 {
+	/*
+	 * For controllers with battery sensing capability, the last channel
+	 * is reserved for battery sensing and should not be included in
+	 * normal channel operations.
+	 */
+	if (data->model_data->bat_sense_sup)
+		return data->model_data->num_channels - 1;
 	return data->model_data->num_channels;
 }
 
@@ -305,9 +312,26 @@ static int aspeed_adc_read_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
+		adc_engine_control_reg_val = readl(data->base + ASPEED_REG_ENGINE_CONTROL);
+		/*
+		 * For battery sensing capable controllers, we need to enable
+		 * the specific channel before reading. This is required because
+		 * the battery channel may not be enabled by default.
+		 */
+		if (data->model_data->bat_sense_sup &&
+		    chan->channel == ASPEED_ADC_BATTERY_CHANNEL) {
+			u32 ctrl_reg = adc_engine_control_reg_val & ~ASPEED_ADC_CTRL_CHANNEL;
+
+			ctrl_reg |= ASPEED_ADC_CTRL_CHANNEL_ENABLE(chan->channel);
+			writel(ctrl_reg, data->base + ASPEED_REG_ENGINE_CONTROL);
+			/*
+			 * After enable a new channel need to wait some time for ADC stable
+			 * Experiment result is 1ms.
+			 */
+			fsleep(1000);
+		}
+
 		if (data->battery_sensing && chan->channel == ASPEED_ADC_BATTERY_CHANNEL) {
-			adc_engine_control_reg_val =
-				readl(data->base + ASPEED_REG_ENGINE_CONTROL);
 			writel(adc_engine_control_reg_val |
 				       FIELD_PREP(ASPEED_ADC_CH7_MODE,
 						  ASPEED_ADC_CH7_BAT) |
@@ -321,11 +345,11 @@ static int aspeed_adc_read_raw(struct iio_dev *indio_dev,
 			*val = readw(data->base + chan->address);
 			*val = (*val * data->battery_mode_gain.mult) /
 			       data->battery_mode_gain.div;
-			/* Restore control register value */
-			writel(adc_engine_control_reg_val,
-			       data->base + ASPEED_REG_ENGINE_CONTROL);
 		} else
 			*val = readw(data->base + chan->address);
+		/* Restore control register value */
+		writel(adc_engine_control_reg_val,
+				data->base + ASPEED_REG_ENGINE_CONTROL);
 		return IIO_VAL_INT;
 
 	case IIO_CHAN_INFO_OFFSET:
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v3 3/4] iio: adc: aspeed: Replace mdelay() with fsleep() for ADC stabilization delay

In reply to Billy Tsai
Quoted 10 lines · Billy Tsai

The ADC stabilization delays in compensation mode and battery sensing mode do not require atomic context. Using mdelay() here results in unnecessary busy waiting.

Replace mdelay(1) with fsleep(1000) to allow the scheduler to run other tasks while waiting for the ADC to stabilize.

Also fix a minor typo in the comment ("adc" -> "ADC").

I haven't suggested the idea, I only suggested to use fsleep() for the purpose.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
In reply to Billy Tsai
Quoted 7 lines · Billy Tsai

For controllers with battery sensing capability (AST2600/AST2700), the last channel uses a different circuit design optimized for battery voltage measurement. This channel should not be enabled by default along with other channels to avoid potential interference and power efficiency issues.
This ensures optimal power efficiency for normal ADC operations while maintaining full functionality when battery sensing is needed.

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+			/*
+			 * After enable a new channel need to wait some time for ADC stable

Missed period at the end. And you probably wanted to say "...for ADC to stabilize."

Quoted 4 lines
drivers/iio/adc/aspeed_adc.c +4 -0
+			 * Experiment result is 1ms.
+			 */
+			fsleep(1000);
+		}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw

Re: [PATCH v3 3/4] iio: adc: aspeed: Replace mdelay() with fsleep() for ADC stabilization delay

In reply to Andy Shevchenko
Quoted 5 lines · On Fri, 20 Mar 2026 12:31:41 +0200 Andy Shevchenko…
Quoted 4 lines · Billy Tsai

The ADC stabilization delays in compensation mode and battery sensing mode do not require atomic context. Using mdelay() here results in unnecessary busy waiting.

Quoted 8 lines
Quoted 6 lines

Replace mdelay(1) with fsleep(1000) to allow the scheduler to run other tasks while waiting for the ADC to stabilize.

Also fix a minor typo in the comment ("adc" -> "ADC").

I haven't suggested the idea, I only suggested to use fsleep() for the purpose.

Dropped the tag.

Quoted 1 line
maintainer
#8 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · On Fri, 20 Mar 2026 12:33:32 +0200 Andy Shevchenko…
Quoted 7 lines · Billy Tsai

For controllers with battery sensing capability (AST2600/AST2700), the last channel uses a different circuit design optimized for battery voltage measurement. This channel should not be enabled by default along with other channels to avoid potential interference and power efficiency issues.
This ensures optimal power efficiency for normal ADC operations while maintaining full functionality when battery sensing is needed.

Quoted 7 lines

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+			/*
+			 * After enable a new channel need to wait some time for ADC stable  

Missed period at the end. And you probably wanted to say "...for ADC to stabilize."

Tweaked and tidied up line wrap.

Series applied to the togreg branch of iio.git.

thanks,

Jonathan

Quoted 6 lines
Quoted 5 lines
drivers/iio/adc/aspeed_adc.c +4 -0
+			 * Experiment result is 1ms.
+			 */
+			fsleep(1000);
+		}