* [PATCH linux dev-6.18 0/4] iio: adc: aspeed: Backport battery sensing patches
@ 2026-08-05 6:13 Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 1/4] iio: adc: Add battery channel definition for ADC Cosmo Chou
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Cosmo Chou @ 2026-08-05 6:13 UTC (permalink / raw)
To: andrew, openbmc; +Cc: chou.cosmo, cosmo.chou
Backport the ASPEED ADC battery sensing fix series to dev-6.18.
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 | 72 +++++++++++++++++++++++++++++-------
1 file changed, 59 insertions(+), 13 deletions(-)
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH linux dev-6.18 1/4] iio: adc: Add battery channel definition for ADC
2026-08-05 6:13 [PATCH linux dev-6.18 0/4] iio: adc: aspeed: Backport battery sensing patches Cosmo Chou
@ 2026-08-05 6:13 ` Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 2/4] iio: adc: Enable multiple consecutive channels based on model data Cosmo Chou
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Cosmo Chou @ 2026-08-05 6:13 UTC (permalink / raw)
To: andrew, openbmc; +Cc: chou.cosmo, cosmo.chou, Billy Tsai
From: Billy Tsai <billy_tsai@aspeedtech.com>
Defines a constant for the battery sensing channel, typically the last
channel of the ADC. Clarifies channel usage and improves code
readability.
Signed-off-by: Billy Tsai <billy_tsai@aspeedtech.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
drivers/iio/adc/aspeed_adc.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/adc/aspeed_adc.c b/drivers/iio/adc/aspeed_adc.c
index 4be44c524b4d..8eebaa3dc534 100644
--- a/drivers/iio/adc/aspeed_adc.c
+++ b/drivers/iio/adc/aspeed_adc.c
@@ -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
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH linux dev-6.18 2/4] iio: adc: Enable multiple consecutive channels based on model data
2026-08-05 6:13 [PATCH linux dev-6.18 0/4] iio: adc: aspeed: Backport battery sensing patches Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 1/4] iio: adc: Add battery channel definition for ADC Cosmo Chou
@ 2026-08-05 6:13 ` Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 3/4] iio: adc: aspeed: Replace mdelay() with fsleep() for ADC stabilization delay Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 4/4] iio: adc: aspeed: Reserve battery sensing channel for on-demand use Cosmo Chou
3 siblings, 0 replies; 5+ messages in thread
From: Cosmo Chou @ 2026-08-05 6:13 UTC (permalink / raw)
To: andrew, openbmc; +Cc: chou.cosmo, cosmo.chou, Billy Tsai
From: Billy Tsai <billy_tsai@aspeedtech.com>
Add helpers to generate channel masks and enable multiple ADC channels
according to the device model's channel count.
Signed-off-by: Billy Tsai <billy_tsai@aspeedtech.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
drivers/iio/adc/aspeed_adc.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/adc/aspeed_adc.c b/drivers/iio/adc/aspeed_adc.c
index 8eebaa3dc534..3ff24474f394 100644
--- a/drivers/iio/adc/aspeed_adc.c
+++ b/drivers/iio/adc/aspeed_adc.c
@@ -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, \
@@ -617,7 +635,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);
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH linux dev-6.18 3/4] iio: adc: aspeed: Replace mdelay() with fsleep() for ADC stabilization delay
2026-08-05 6:13 [PATCH linux dev-6.18 0/4] iio: adc: aspeed: Backport battery sensing patches Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 1/4] iio: adc: Add battery channel definition for ADC Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 2/4] iio: adc: Enable multiple consecutive channels based on model data Cosmo Chou
@ 2026-08-05 6:13 ` Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 4/4] iio: adc: aspeed: Reserve battery sensing channel for on-demand use Cosmo Chou
3 siblings, 0 replies; 5+ messages in thread
From: Cosmo Chou @ 2026-08-05 6:13 UTC (permalink / raw)
To: andrew, openbmc; +Cc: chou.cosmo, cosmo.chou, Billy Tsai
From: Billy Tsai <billy_tsai@aspeedtech.com>
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").
Signed-off-by: Billy Tsai <billy_tsai@aspeedtech.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
drivers/iio/adc/aspeed_adc.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/iio/adc/aspeed_adc.c b/drivers/iio/adc/aspeed_adc.c
index 3ff24474f394..a1a6296d3003 100644
--- a/drivers/iio/adc/aspeed_adc.c
+++ b/drivers/iio/adc/aspeed_adc.c
@@ -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;
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH linux dev-6.18 4/4] iio: adc: aspeed: Reserve battery sensing channel for on-demand use
2026-08-05 6:13 [PATCH linux dev-6.18 0/4] iio: adc: aspeed: Backport battery sensing patches Cosmo Chou
` (2 preceding siblings ...)
2026-08-05 6:13 ` [PATCH linux dev-6.18 3/4] iio: adc: aspeed: Replace mdelay() with fsleep() for ADC stabilization delay Cosmo Chou
@ 2026-08-05 6:13 ` Cosmo Chou
3 siblings, 0 replies; 5+ messages in thread
From: Cosmo Chou @ 2026-08-05 6:13 UTC (permalink / raw)
To: andrew, openbmc; +Cc: chou.cosmo, cosmo.chou, Billy Tsai
From: Billy Tsai <billy_tsai@aspeedtech.com>
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.
Signed-off-by: Billy Tsai <billy_tsai@aspeedtech.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
drivers/iio/adc/aspeed_adc.c | 38 +++++++++++++++++++++++++++++-------
1 file changed, 31 insertions(+), 7 deletions(-)
diff --git a/drivers/iio/adc/aspeed_adc.c b/drivers/iio/adc/aspeed_adc.c
index a1a6296d3003..9b828a3c91da 100644
--- a/drivers/iio/adc/aspeed_adc.c
+++ b/drivers/iio/adc/aspeed_adc.c
@@ -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;
}
@@ -256,8 +263,8 @@ static int aspeed_adc_compensation(struct iio_dev *indio_dev)
ASPEED_ADC_CTRL_CHANNEL_ENABLE(0),
data->base + ASPEED_REG_ENGINE_CONTROL);
/*
- * After enable compensating sensing mode need to wait some time for ADC stable
- * Experiment result is 1ms.
+ * After enable compensating sensing mode need to wait some time for the
+ * ADC stablize. Experiment result is 1ms.
*/
fsleep(1000);
@@ -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:
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-08-05 6:14 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-05 6:13 [PATCH linux dev-6.18 0/4] iio: adc: aspeed: Backport battery sensing patches Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 1/4] iio: adc: Add battery channel definition for ADC Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 2/4] iio: adc: Enable multiple consecutive channels based on model data Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 3/4] iio: adc: aspeed: Replace mdelay() with fsleep() for ADC stabilization delay Cosmo Chou
2026-08-05 6:13 ` [PATCH linux dev-6.18 4/4] iio: adc: aspeed: Reserve battery sensing channel for on-demand use Cosmo Chou
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox