Skip to contentSkip to conversation

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

PATCH Superseded Billy Tsai 2 participants 8 messages Mar 16 – Mar 20

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Billy Tsai

author reviewer
#0 lore raw

v2 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 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 (3):
iio: adc: Add battery channel definition for ADC
iio: adc: Enable multiple consecutive channels based on model data
iio: adc: aspeed: Reserve battery sensing channel for on-demand use

 drivers/iio/adc/aspeed_adc.c | 75 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 65 insertions(+), 10 deletions(-)

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

Best regards,

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

PATCH v2 1/3 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..af9a95d31d81 100644
@@ -72,6 +72,8 @@
 #define ASPEED_ADC_BAT_SENSING_ENABLE		BIT(13)
 #define ASPEED_ADC_CTRL_CHANNEL			GENMASK(31, 16)
 #define ASPEED_ADC_CTRL_CHANNEL_ENABLE(ch)	FIELD_PREP(ASPEED_ADC_CTRL_CHANNEL, BIT(ch))
+/* Battery sensing is typically on the last channel */
+#define ASPEED_ADC_BATTERY_CHANNEL		7
 
 #define ASPEED_ADC_INIT_POLLING_TIME	500
 #define ASPEED_ADC_INIT_TIMEOUT		500000
@@ -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 v2 2/3 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 | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
drivers/iio/adc/aspeed_adc.c +32 -3
index af9a95d31d81..81a2dd752541 100644
@@ -72,6 +72,29 @@
 #define ASPEED_ADC_BAT_SENSING_ENABLE		BIT(13)
 #define ASPEED_ADC_CTRL_CHANNEL			GENMASK(31, 16)
 #define ASPEED_ADC_CTRL_CHANNEL_ENABLE(ch)	FIELD_PREP(ASPEED_ADC_CTRL_CHANNEL, BIT(ch))
+
+/*
+ * 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 == 0)
+		return 0;
+	if (num_channels >= 16)
+		return GENMASK(15, 0);
+	return GENMASK(num_channels - 1, 0);
+}
+
+/*
+ * Helper function to enable multiple channels in the control register
+ */
+static inline u32 aspeed_adc_enable_channels(unsigned int num_channels)
+{
+	return FIELD_PREP(ASPEED_ADC_CTRL_CHANNEL, aspeed_adc_channels_mask(num_channels));
+}
+
 /* Battery sensing is typically on the last channel */
 #define ASPEED_ADC_BATTERY_CHANNEL		7
 
@@ -123,6 +146,11 @@ struct aspeed_adc_data {
 	struct adc_gain		battery_mode_gain;
 };
 
+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,						\
@@ -610,9 +638,10 @@ static int aspeed_adc_probe(struct platform_device *pdev)
 
 	aspeed_adc_compensation(indio_dev);
 	/* 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;
+	adc_engine_control_reg_val = readl(data->base + ASPEED_REG_ENGINE_CONTROL);
+	adc_engine_control_reg_val |=
+		aspeed_adc_enable_channels(aspeed_adc_get_active_channels(data));
+
 	writel(adc_engine_control_reg_val,
 	       data->base + ASPEED_REG_ENGINE_CONTROL);
 
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v2 2/3] iio: adc: Enable multiple consecutive channels based on model data

Quoted 2 lines · Billy Tsai

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

...

Quoted 1 line

+

(1)

drivers/iio/adc/aspeed_adc.c

Quoted 13 lines
drivers/iio/adc/aspeed_adc.c +12 -0
+/*
+ * 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 == 0)
+		return 0;
 
+	if (num_channels >= 16)
+		return GENMASK(15, 0);
+	return GENMASK(num_channels - 1, 0);

This entire function can be folded into

	return BIT(min(num_channels, 16U)) - 1;

Or

	if (num_channels > 16)
		return GENMASK(15, 0);

	return BIT(num_channels) - 1;

drivers/iio/adc/aspeed_adc.c

Quoted 11 lines
drivers/iio/adc/aspeed_adc.c +9 -0
+}
 
+/*
+ * Helper function to enable multiple channels in the control register
+ */
+static inline u32 aspeed_adc_enable_channels(unsigned int num_channels)
+{
+	return FIELD_PREP(ASPEED_ADC_CTRL_CHANNEL, aspeed_adc_channels_mask(num_channels));
+}
 
+

One of these (see 1 above) new blank lines should be rather added in the previous patch.

Quoted 2 lines

/* Battery sensing is typically on the last channel */
#define ASPEED_ADC_BATTERY_CHANNEL 7

...

drivers/iio/adc/aspeed_adc.c static int aspeed_adc_probe(struct platform_device *pdev)

Quoted 8 lines
drivers/iio/adc/aspeed_adc.c +3 -3
 	/* 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;
+	adc_engine_control_reg_val = readl(data->base + ASPEED_REG_ENGINE_CONTROL);
 
+	adc_engine_control_reg_val |=
+		aspeed_adc_enable_channels(aspeed_adc_get_active_channels(data));

Why not FIELD_MODIFY()?

Quoted 2 lines
drivers/iio/adc/aspeed_adc.c +0 -0
 	writel(adc_engine_control_reg_val,
 	       data->base + ASPEED_REG_ENGINE_CONTROL);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author reviewer
#4 lore raw

Re: [PATCH v2 2/3] iio: adc: Enable multiple consecutive channels based on model data

Quoted 25 lines · Billy Tsai
Quoted 2 lines

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

...

Quoted 1 line

+

(1)

drivers/iio/adc/aspeed_adc.c

Quoted 13 lines
drivers/iio/adc/aspeed_adc.c +12 -0
+/*
+ * 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 == 0)
+             return 0;
 
+     if (num_channels >= 16)
+             return GENMASK(15, 0);
+     return GENMASK(num_channels - 1, 0);

This entire function can be folded into

Quoted 8 lines
        return BIT(min(num_channels, 16U)) - 1;

Or

        if (num_channels > 16)
                return GENMASK(15, 0);

        return BIT(num_channels) - 1;

Got it. I will replace it to later one.

Quoted 10 lines
Quoted 9 lines
drivers/iio/adc/aspeed_adc.c +8 -0
+}
 
+/*
+ * Helper function to enable multiple channels in the control register
+ */
+static inline u32 aspeed_adc_enable_channels(unsigned int num_channels)
+{
+     return FIELD_PREP(ASPEED_ADC_CTRL_CHANNEL, aspeed_adc_channels_mask(num_channels));
+}
Quoted 3 lines
Quoted 1 line

+

One of these (see 1 above) new blank lines should be rather added in the previous patch.

Good catch, I’ll avoid this blank lines problem.

Quoted 2 lines
Quoted 2 lines

/* Battery sensing is typically on the last channel */
#define ASPEED_ADC_BATTERY_CHANNEL 7

...

Quoted 10 lines

drivers/iio/adc/aspeed_adc.c static int aspeed_adc_probe(struct platform_device *pdev)

Quoted 8 lines
      /* Start all channels in normal mode. */
drivers/iio/adc/aspeed_adc.c +3 -3
-     adc_engine_control_reg_val =
-             readl(data->base + ASPEED_REG_ENGINE_CONTROL);
-     adc_engine_control_reg_val |= ASPEED_ADC_CTRL_CHANNEL;
+     adc_engine_control_reg_val = readl(data->base + ASPEED_REG_ENGINE_CONTROL);
 
+     adc_engine_control_reg_val |=
+             aspeed_adc_enable_channels(aspeed_adc_get_active_channels(data));

Why not FIELD_MODIFY()?

Use FIELD_MODIFY() for the channel field update is more better.

Quoted 2 lines
Quoted 2 lines
      writel(adc_engine_control_reg_val,
             data->base + ASPEED_REG_ENGINE_CONTROL);

Thanks

Billy Tsai

author reviewer
#5 lore raw

PATCH v2 3/3 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 81a2dd752541..ab173d8542c6 100644
@@ -148,6 +148,13 @@ struct aspeed_adc_data {
 
 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;
 }
 
@@ -315,9 +322,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.
+			 */
+			mdelay(1);
+		}
+
 		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) |
@@ -331,11 +355,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
#6 lore raw
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

ADC

drivers/iio/adc/aspeed_adc.c static int aspeed_adc_read_raw(struct iio_dev *indio_dev, · line 341

Quoted 3 lines
drivers/iio/adc/aspeed_adc.c +3 -0
+			 * Experiment result is 1ms.
+			 */
+			mdelay(1);

Why atomic? If not required, use fsleep(). Otherwise explain.

Quoted 1 line
drivers/iio/adc/aspeed_adc.c +1 -0
+		}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author reviewer
#7 lore raw
Quoted 8 lines · Billy Tsai
Quoted 7 lines

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 6 lines

...

Review

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

ADC

Got it.

Quoted 5 lines

drivers/iio/adc/aspeed_adc.c static int aspeed_adc_read_raw(struct iio_dev *indio_dev, · line 341

Quoted 3 lines
drivers/iio/adc/aspeed_adc.c +3 -0
+                      * Experiment result is 1ms.
+                      */
+                     mdelay(1);

Why atomic? If not required, use fsleep(). Otherwise explain.

Good point.

This path is sleepable, so mdelay() is not required. I’ll switch it to fsleep(1000) in v2.

I’ll also take a look at other mdelay() users in the driver and clean them up in a separate patch if appropriate.

Quoted 1 line
Quoted 1 line

+ }

Thanks

Billy Tsai