Battery temperature ADC plumbing on Qualcomm platforms
Subsystems (5)
v4 cover Battery temperature ADC plumbing on Qualcomm platforms
This implements a solution to get battery temperature readings working on (for example) smartphones with Qualcomm SoCs.
The solution chosen in downstream Qualcomm kernels is exposing ADC_BAT_THERM_PU* in the ADC driver as temperature channels with the lookup table ("struct vadc_map_pt") for the specific NTC found in a device's battery patched to adjust the lookup table. Patching a kernel per-device is obviously nothing we can put upstream.
The high level solution proposed here:
* ADC driver provides temperature channel in (milli)volt as IIO channel
* generic-adc-thermal driver converts voltage to temperature based on provided lookup table from DT (driver has one IIO channel input, one IIO channel output)
* The fuel gauge driver can use that temperature IIO channel to expose battery temperature via the power supply device
See RFC for other variants or alternatives considered.
---
Changes in v4:
- spmi-adc5/vadc-common: Fix inconsistency micro vs milli (Sashiko)
- spmi-adc5/vadc-common: use 'MICRO / MILLI' instead of 1000, use _mV (Andy)
- thermal-generic-adc: simplify devm_thermal_of_zone_register return (Andy)
- thermal-generic-adc: move iio read + convert into gadc_thermal_iio_to_temp() function (Jonathan)
- thermal-generic-adc: convert 'register failed' dev_info to dev_dbg (Jonathan)
- Link to v3: https://patch.msgid.link/[email protected]
Changes in v3:
- Expand commit message on dt-bindings patch (Rob)
- Some commit message improvements in generic-adc patch (Andy)
- Fix dt node location, move higher up (Konrad)
- Pick up tags
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Rebase on linux-next (a different patch to add IIO to
generic-adc-thermal has already landed upstream)
- Un-mark as RFC
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Luca Weiss (5):
iio: adc: qcom-spmi-adc5: Add battery thermal channels
dt-bindings: thermal: generic-adc: Document #io-channel-cells thermal/drivers/generic-adc: Allow probe without TZ registration
arm64: dts: qcom: pm7250b: Define battery temperature ADC channels
arm64: dts: qcom: sm7225-fairphone-fp4: Add battery temperature node
.../bindings/thermal/generic-adc-thermal.yaml | 4 ++ arch/arm64/boot/dts/qcom/pm7250b.dtsi | 24 +++++++ arch/arm64/boot/dts/qcom/sm7225-fairphone-fp4.dts | 83 ++++++++++++++++++++++ drivers/iio/adc/qcom-spmi-adc5.c | 6 ++ drivers/iio/adc/qcom-vadc-common.c | 16 +++++ drivers/thermal/thermal-generic-adc.c | 27 +++---- include/linux/iio/adc/qcom-vadc-common.h | 3 + 7 files changed, 151 insertions(+), 12 deletions(-)
---
base-commit: 6aa7dbf8ad5ebd3babf53a4d6209018f86939f17
change-id: 20251010-bat-temp-adc-8539bf0b85bc
Best regards,
Signature · 1 line
Luca Weiss <[email protected]>
PATCH v4 1/5 iio: adc: qcom-spmi-adc5: Add battery thermal channels
Add the BAT_THERM channels with millivolt as output, which can then be used along with information regarding the NTC in the battery, to get the actual battery temperature.
A static lookup table in the ADC driver won't work since different devices have different Ohm and Beta values in their battery NTCs, for example a NTC can be 10kOhm & B=3435K.
--- drivers/iio/adc/qcom-spmi-adc5.c | 6 ++++++ drivers/iio/adc/qcom-vadc-common.c | 16 ++++++++++++++++ include/linux/iio/adc/qcom-vadc-common.h | 3 +++ 3 files changed, 25 insertions(+)
drivers/iio/adc/qcom-spmi-adc5.c +6 -0
@@ -542,6 +542,12 @@ static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = { SCALE_HW_CALIB_DEFAULT) [ADC5_XO_THERM_100K_PU ] =ADC5_CHAN_TEMP ("xo_therm" ,0 ,SCALE_HW_CALIB_XOTHERM ) + [ADC5_BAT_THERM_30K_PU ] =ADC5_CHAN_VOLT ("bat_therm_30k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_100K_PU ] =ADC5_CHAN_VOLT ("bat_therm_100k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_400K_PU ] =ADC5_CHAN_VOLT ("bat_therm_400k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) [ADC5_BAT_ID_100K_PU ] =ADC5_CHAN_TEMP ("bat_id" ,0 ,SCALE_HW_CALIB_DEFAULT ) [ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
drivers/iio/adc/qcom-vadc-common.c +16 -0
@@ -309,6 +309,10 @@ static int qcom_vadc_scale_hw_calib_therm(const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV); static int qcom_vadc7_scale_hw_calib_therm( const struct u32_fract *prescale, const struct adc5_data *data,
@@ -333,6 +337,7 @@ static int qcom_vadc7_scale_hw_calib_die_temp(static const struct qcom_adc5_scale_type scale_adc5_fn[] = { [SCALE_HW_CALIB_DEFAULT ] = {qcom_vadc_scale_hw_calib_volt}, [SCALE_HW_CALIB_THERM_100K_PULLUP ] = {qcom_vadc_scale_hw_calib_therm}, + [SCALE_HW_CALIB_THERM_VOLT ] = {qcom_vadc_scale_hw_calib_therm_volt}, [SCALE_HW_CALIB_XOTHERM ] = {qcom_vadc_scale_hw_calib_therm}, [SCALE_HW_CALIB_THERM_100K_PU_PM7 ] = { qcom_vadc7_scale_hw_calib_therm},
@@ -583,6 +588,17 @@ static int qcom_vadc_scale_hw_calib_therm( voltage, result_mdec); } +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV) +{ + *result_mV =qcom_vadc_scale_code_voltage_factor (adc_code, + prescale, data,MICRO /MILLI ); + +return 0 ; +} + static int qcom_vadc_scale_hw_calib_die_temp( const struct u32_fract *prescale, const struct adc5_data *data,
include/linux/iio/adc/qcom-vadc-common.h +3 -0
@@ -93,6 +93,8 @@ struct vadc_linear_graph {* voltage (uV) with hardware applied offset/slope values to adc code. * @SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using * lookup table. The hardware applies offset/slope to adc code. +* @SCALE_HW_CALIB_THERM_VOLT: Returns voltage in mV of a temperature channel. +* The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using * 100k pullup. The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_THERM_100K_PU_PM7: Returns temperature in millidegC using
@@ -114,6 +116,7 @@ enum vadc_scale_fn_type {SCALE_PMI_CHG_TEMP ,SCALE_HW_CALIB_DEFAULT ,SCALE_HW_CALIB_THERM_100K_PULLUP , +SCALE_HW_CALIB_THERM_VOLT ,SCALE_HW_CALIB_XOTHERM ,SCALE_HW_CALIB_THERM_100K_PU_PM7 , SCALE_HW_CALIB_PMIC_THERM,
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerRe: [PATCH v4 1/5] iio: adc: qcom-spmi-adc5: Add battery thermal channels
Quoted 9 lines · On Tue, 17 Nov 2026 09:23:11 +0100 Luca Weiss <[email protected]> wrote:
Add the
BAT_THERMchannels with millivolt as output, which can then be used along with information regarding the NTC in the battery, to get the actual battery temperature.A static lookup table in the ADC driver won't work since different devices have different Ohm and Beta values in their battery NTCs, for example a NTC can be 10kOhm & B=3435K.
Signed-off-by Luca Weiss <[email protected]>
Given this is independent of the following elements other than the dtsi changes, do you want me to pick it up now?
You may need to use numeric values in dtsi until the header catches up then chase with a patch flipping to the defines next cycle. That's how we normally avoid that dependency slowing things down.
Thanks,
Jonathan
Quoted 86 lines
--- drivers/iio/adc/qcom-spmi-adc5.c | 6 ++++++ drivers/iio/adc/qcom-vadc-common.c | 16 ++++++++++++++++ include/linux/iio/adc/qcom-vadc-common.h | 3 +++ 3 files changed, 25 insertions(+)drivers/iio/adc/qcom-spmi-adc5.c +6
-0@@ -542,6 +542,12 @@ static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = { SCALE_HW_CALIB_DEFAULT) [ADC5_XO_THERM_100K_PU ] =ADC5_CHAN_TEMP ("xo_therm" ,0 ,SCALE_HW_CALIB_XOTHERM ) + [ADC5_BAT_THERM_30K_PU ] =ADC5_CHAN_VOLT ("bat_therm_30k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_100K_PU ] =ADC5_CHAN_VOLT ("bat_therm_100k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_400K_PU ] =ADC5_CHAN_VOLT ("bat_therm_400k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) [ADC5_BAT_ID_100K_PU ] =ADC5_CHAN_TEMP ("bat_id" ,0 ,SCALE_HW_CALIB_DEFAULT ) [ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,drivers/iio/adc/qcom-vadc-common.c +16
-0@@ -309,6 +309,10 @@ static int qcom_vadc_scale_hw_calib_therm(const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV); static int qcom_vadc7_scale_hw_calib_therm( const struct u32_fract *prescale, const struct adc5_data *data,@@ -333,6 +337,7 @@ static int qcom_vadc7_scale_hw_calib_die_temp(static const struct qcom_adc5_scale_type scale_adc5_fn[] = { [SCALE_HW_CALIB_DEFAULT ] = {qcom_vadc_scale_hw_calib_volt}, [SCALE_HW_CALIB_THERM_100K_PULLUP ] = {qcom_vadc_scale_hw_calib_therm}, + [SCALE_HW_CALIB_THERM_VOLT ] = {qcom_vadc_scale_hw_calib_therm_volt}, [SCALE_HW_CALIB_XOTHERM ] = {qcom_vadc_scale_hw_calib_therm}, [SCALE_HW_CALIB_THERM_100K_PU_PM7 ] = { qcom_vadc7_scale_hw_calib_therm},@@ -583,6 +588,17 @@ static int qcom_vadc_scale_hw_calib_therm( voltage, result_mdec); } +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV) +{ + *result_mV =qcom_vadc_scale_code_voltage_factor (adc_code, + prescale, data,MICRO /MILLI ); + +return 0 ; +} + static int qcom_vadc_scale_hw_calib_die_temp( const struct u32_fract *prescale, const struct adc5_data *data,include/linux/iio/adc/qcom-vadc-common.h +3
-0@@ -93,6 +93,8 @@ struct vadc_linear_graph {* voltage (uV) with hardware applied offset/slope values to adc code. * @SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using * lookup table. The hardware applies offset/slope to adc code. +* @SCALE_HW_CALIB_THERM_VOLT: Returns voltage in mV of a temperature channel. +* The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using * 100k pullup. The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_THERM_100K_PU_PM7: Returns temperature in millidegC using @@ -114,6 +116,7 @@ enum vadc_scale_fn_type {SCALE_PMI_CHG_TEMP ,SCALE_HW_CALIB_DEFAULT ,SCALE_HW_CALIB_THERM_100K_PULLUP , +SCALE_HW_CALIB_THERM_VOLT ,SCALE_HW_CALIB_XOTHERM ,SCALE_HW_CALIB_THERM_100K_PU_PM7 , SCALE_HW_CALIB_PMIC_THERM,
Re: [PATCH v4 1/5] iio: adc: qcom-spmi-adc5: Add battery thermal channels
Hi Jonathan,
Quoted 11 lines · Jonathan Cameron
Quoted 8 lines · On Tue, 17 Nov 2026 09:23:11 +0100 Luca Weiss <[email protected]> wrote:
Add the
BAT_THERMchannels with millivolt as output, which can then be used along with information regarding the NTC in the battery, to get the actual battery temperature.A static lookup table in the ADC driver won't work since different devices have different Ohm and Beta values in their battery NTCs, for example a NTC can be 10kOhm & B=3435K.
Quoted 7 lines
Quoted 1 line
Signed-off-by Luca Weiss <[email protected]>Given this is independent of the following elements other than the dtsi changes, do you want me to pick it up now?
You may need to use numeric values in dtsi until the header catches up then chase with a patch flipping to the defines next cycle. That's how we normally avoid that dependency slowing things down.
Sounds good from my side. Thank you!
I think realistically, there's no other option than what's implemented in this patch, even if PATCH 2/5 (dt-bindings change for generic-adc) is not reviewed yet. But I hope the dt maintainers will be okay with that as well.
Regards
Luca
Quoted 91 lines
Thanks,
Jonathan
Quoted 86 lines
--- drivers/iio/adc/qcom-spmi-adc5.c | 6 ++++++ drivers/iio/adc/qcom-vadc-common.c | 16 ++++++++++++++++ include/linux/iio/adc/qcom-vadc-common.h | 3 +++ 3 files changed, 25 insertions(+)drivers/iio/adc/qcom-spmi-adc5.c +6
-0@@ -542,6 +542,12 @@ static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = { SCALE_HW_CALIB_DEFAULT) [ADC5_XO_THERM_100K_PU ] =ADC5_CHAN_TEMP ("xo_therm" ,0 ,SCALE_HW_CALIB_XOTHERM ) + [ADC5_BAT_THERM_30K_PU ] =ADC5_CHAN_VOLT ("bat_therm_30k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_100K_PU ] =ADC5_CHAN_VOLT ("bat_therm_100k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_400K_PU ] =ADC5_CHAN_VOLT ("bat_therm_400k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) [ADC5_BAT_ID_100K_PU ] =ADC5_CHAN_TEMP ("bat_id" ,0 ,SCALE_HW_CALIB_DEFAULT ) [ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,drivers/iio/adc/qcom-vadc-common.c +16
-0@@ -309,6 +309,10 @@ static int qcom_vadc_scale_hw_calib_therm(const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV); static int qcom_vadc7_scale_hw_calib_therm( const struct u32_fract *prescale, const struct adc5_data *data,@@ -333,6 +337,7 @@ static int qcom_vadc7_scale_hw_calib_die_temp(static const struct qcom_adc5_scale_type scale_adc5_fn[] = { [SCALE_HW_CALIB_DEFAULT ] = {qcom_vadc_scale_hw_calib_volt}, [SCALE_HW_CALIB_THERM_100K_PULLUP ] = {qcom_vadc_scale_hw_calib_therm}, + [SCALE_HW_CALIB_THERM_VOLT ] = {qcom_vadc_scale_hw_calib_therm_volt}, [SCALE_HW_CALIB_XOTHERM ] = {qcom_vadc_scale_hw_calib_therm}, [SCALE_HW_CALIB_THERM_100K_PU_PM7 ] = { qcom_vadc7_scale_hw_calib_therm},@@ -583,6 +588,17 @@ static int qcom_vadc_scale_hw_calib_therm( voltage, result_mdec); } +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV) +{ + *result_mV =qcom_vadc_scale_code_voltage_factor (adc_code, + prescale, data,MICRO /MILLI ); + +return 0 ; +} + static int qcom_vadc_scale_hw_calib_die_temp( const struct u32_fract *prescale, const struct adc5_data *data,include/linux/iio/adc/qcom-vadc-common.h +3
-0@@ -93,6 +93,8 @@ struct vadc_linear_graph {* voltage (uV) with hardware applied offset/slope values to adc code. * @SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using * lookup table. The hardware applies offset/slope to adc code. +* @SCALE_HW_CALIB_THERM_VOLT: Returns voltage in mV of a temperature channel. +* The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using * 100k pullup. The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_THERM_100K_PU_PM7: Returns temperature in millidegC using @@ -114,6 +116,7 @@ enum vadc_scale_fn_type {SCALE_PMI_CHG_TEMP ,SCALE_HW_CALIB_DEFAULT ,SCALE_HW_CALIB_THERM_100K_PULLUP , +SCALE_HW_CALIB_THERM_VOLT ,SCALE_HW_CALIB_XOTHERM ,SCALE_HW_CALIB_THERM_100K_PU_PM7 , SCALE_HW_CALIB_PMIC_THERM,
Jonathan Cameron
maintainerRe: [PATCH v4 1/5] iio: adc: qcom-spmi-adc5: Add battery thermal channels
Quoted 22 lines · On Mon, 23 Nov 2026 10:17:21 +0100 "Luca Weiss" <[email protected]>…
Hi Jonathan,
Quoted 11 lines · Jonathan Cameron
Quoted 8 lines · On Tue, 17 Nov 2026 09:23:11 +0100 Luca Weiss <[email protected]> wrote:
Add the
BAT_THERMchannels with millivolt as output, which can then be used along with information regarding the NTC in the battery, to get the actual battery temperature.A static lookup table in the ADC driver won't work since different devices have different Ohm and Beta values in their battery NTCs, for example a NTC can be 10kOhm & B=3435K.
Quoted 7 lines
Quoted 1 line
Signed-off-by Luca Weiss <[email protected]>Given this is independent of the following elements other than the dtsi changes, do you want me to pick it up now?
You may need to use numeric values in dtsi until the header catches up then chase with a patch flipping to the defines next cycle. That's how we normally avoid that dependency slowing things down.
Quoted 7 lines
Sounds good from my side. Thank you!
I think realistically, there's no other option than what's implemented in this patch, even if PATCH 2/5 (dt-bindings change for generic-adc) is not reviewed yet. But I hope the dt maintainers will be okay with that as well.
I'm going to hold off on this until patch 2 discussion resolves.
J
Quoted 97 lines
Regards
LucaQuoted 91 lines
Thanks,
Jonathan
Quoted 86 lines
--- drivers/iio/adc/qcom-spmi-adc5.c | 6 ++++++ drivers/iio/adc/qcom-vadc-common.c | 16 ++++++++++++++++ include/linux/iio/adc/qcom-vadc-common.h | 3 +++ 3 files changed, 25 insertions(+)drivers/iio/adc/qcom-spmi-adc5.c +6
-0@@ -542,6 +542,12 @@ static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = { SCALE_HW_CALIB_DEFAULT) [ADC5_XO_THERM_100K_PU ] =ADC5_CHAN_TEMP ("xo_therm" ,0 ,SCALE_HW_CALIB_XOTHERM ) + [ADC5_BAT_THERM_30K_PU ] =ADC5_CHAN_VOLT ("bat_therm_30k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_100K_PU ] =ADC5_CHAN_VOLT ("bat_therm_100k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_400K_PU ] =ADC5_CHAN_VOLT ("bat_therm_400k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) [ADC5_BAT_ID_100K_PU ] =ADC5_CHAN_TEMP ("bat_id" ,0 ,SCALE_HW_CALIB_DEFAULT ) [ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,drivers/iio/adc/qcom-vadc-common.c +16
-0@@ -309,6 +309,10 @@ static int qcom_vadc_scale_hw_calib_therm(const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV); static int qcom_vadc7_scale_hw_calib_therm( const struct u32_fract *prescale, const struct adc5_data *data,@@ -333,6 +337,7 @@ static int qcom_vadc7_scale_hw_calib_die_temp(static const struct qcom_adc5_scale_type scale_adc5_fn[] = { [SCALE_HW_CALIB_DEFAULT ] = {qcom_vadc_scale_hw_calib_volt}, [SCALE_HW_CALIB_THERM_100K_PULLUP ] = {qcom_vadc_scale_hw_calib_therm}, + [SCALE_HW_CALIB_THERM_VOLT ] = {qcom_vadc_scale_hw_calib_therm_volt}, [SCALE_HW_CALIB_XOTHERM ] = {qcom_vadc_scale_hw_calib_therm}, [SCALE_HW_CALIB_THERM_100K_PU_PM7 ] = { qcom_vadc7_scale_hw_calib_therm},@@ -583,6 +588,17 @@ static int qcom_vadc_scale_hw_calib_therm( voltage, result_mdec); } +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV) +{ + *result_mV =qcom_vadc_scale_code_voltage_factor (adc_code, + prescale, data,MICRO /MILLI ); + +return 0 ; +} + static int qcom_vadc_scale_hw_calib_die_temp( const struct u32_fract *prescale, const struct adc5_data *data,include/linux/iio/adc/qcom-vadc-common.h +3
-0@@ -93,6 +93,8 @@ struct vadc_linear_graph {* voltage (uV) with hardware applied offset/slope values to adc code. * @SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using * lookup table. The hardware applies offset/slope to adc code. +* @SCALE_HW_CALIB_THERM_VOLT: Returns voltage in mV of a temperature channel. +* The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using * 100k pullup. The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_THERM_100K_PU_PM7: Returns temperature in millidegC using @@ -114,6 +116,7 @@ enum vadc_scale_fn_type {SCALE_PMI_CHG_TEMP ,SCALE_HW_CALIB_DEFAULT ,SCALE_HW_CALIB_THERM_100K_PULLUP , +SCALE_HW_CALIB_THERM_VOLT ,SCALE_HW_CALIB_XOTHERM ,SCALE_HW_CALIB_THERM_100K_PU_PM7 , SCALE_HW_CALIB_PMIC_THERM,
Jonathan Cameron
maintainerRe: [PATCH v4 1/5] iio: adc: qcom-spmi-adc5: Add battery thermal channels
Quoted 25 lines · On Sun, 29 Nov 2026 01:24:56 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 22 lines · On Mon, 23 Nov 2026 10:17:21 +0100 "Luca Weiss" <[email protected]>…
Hi Jonathan,
Quoted 18 lines · Jonathan Cameron
Quoted 9 lines · On Tue, 17 Nov 2026 09:23:11 +0100 Luca Weiss <[email protected]> wrote:
Add the
BAT_THERMchannels with millivolt as output, which can then be used along with information regarding the NTC in the battery, to get the actual battery temperature.A static lookup table in the ADC driver won't work since different devices have different Ohm and Beta values in their battery NTCs, for example a NTC can be 10kOhm & B=3435K.
Signed-off-by Luca Weiss <[email protected]>Given this is independent of the following elements other than the dtsi changes, do you want me to pick it up now?
You may need to use numeric values in dtsi until the header catches up then chase with a patch flipping to the defines next cycle. That's how we normally avoid that dependency slowing things down.
Quoted 9 lines
Quoted 7 lines
Sounds good from my side. Thank you!
I think realistically, there's no other option than what's implemented in this patch, even if PATCH 2/5 (dt-bindings change for generic-adc) is not reviewed yet. But I hope the dt maintainers will be okay with that as well.
I'm going to hold off on this until patch 2 discussion resolves.
Applied to the testing branch of iio.git.
Thanks,
Jonathan
Quoted 100 lines
J
Quoted 97 lines
Regards
LucaQuoted 91 lines
Thanks,
Jonathan
Quoted 86 lines
--- drivers/iio/adc/qcom-spmi-adc5.c | 6 ++++++ drivers/iio/adc/qcom-vadc-common.c | 16 ++++++++++++++++ include/linux/iio/adc/qcom-vadc-common.h | 3 +++ 3 files changed, 25 insertions(+)drivers/iio/adc/qcom-spmi-adc5.c +6
-0@@ -542,6 +542,12 @@ static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = { SCALE_HW_CALIB_DEFAULT) [ADC5_XO_THERM_100K_PU ] =ADC5_CHAN_TEMP ("xo_therm" ,0 ,SCALE_HW_CALIB_XOTHERM ) + [ADC5_BAT_THERM_30K_PU ] =ADC5_CHAN_VOLT ("bat_therm_30k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_100K_PU ] =ADC5_CHAN_VOLT ("bat_therm_100k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) + [ADC5_BAT_THERM_400K_PU ] =ADC5_CHAN_VOLT ("bat_therm_400k_pu" ,0 , +SCALE_HW_CALIB_THERM_VOLT ) [ADC5_BAT_ID_100K_PU ] =ADC5_CHAN_TEMP ("bat_id" ,0 ,SCALE_HW_CALIB_DEFAULT ) [ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,drivers/iio/adc/qcom-vadc-common.c +16
-0@@ -309,6 +309,10 @@ static int qcom_vadc_scale_hw_calib_therm(const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV); static int qcom_vadc7_scale_hw_calib_therm( const struct u32_fract *prescale, const struct adc5_data *data,@@ -333,6 +337,7 @@ static int qcom_vadc7_scale_hw_calib_die_temp(static const struct qcom_adc5_scale_type scale_adc5_fn[] = { [SCALE_HW_CALIB_DEFAULT ] = {qcom_vadc_scale_hw_calib_volt}, [SCALE_HW_CALIB_THERM_100K_PULLUP ] = {qcom_vadc_scale_hw_calib_therm}, + [SCALE_HW_CALIB_THERM_VOLT ] = {qcom_vadc_scale_hw_calib_therm_volt}, [SCALE_HW_CALIB_XOTHERM ] = {qcom_vadc_scale_hw_calib_therm}, [SCALE_HW_CALIB_THERM_100K_PU_PM7 ] = { qcom_vadc7_scale_hw_calib_therm},@@ -583,6 +588,17 @@ static int qcom_vadc_scale_hw_calib_therm( voltage, result_mdec); } +static int qcom_vadc_scale_hw_calib_therm_volt ( +const struct u32_fract *prescale, +const struct adc5_data *data, +u16 adc_code,int *result_mV) +{ + *result_mV =qcom_vadc_scale_code_voltage_factor (adc_code, + prescale, data,MICRO /MILLI ); + +return 0 ; +} + static int qcom_vadc_scale_hw_calib_die_temp( const struct u32_fract *prescale, const struct adc5_data *data,include/linux/iio/adc/qcom-vadc-common.h +3
-0@@ -93,6 +93,8 @@ struct vadc_linear_graph {* voltage (uV) with hardware applied offset/slope values to adc code. * @SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using * lookup table. The hardware applies offset/slope to adc code. +* @SCALE_HW_CALIB_THERM_VOLT: Returns voltage in mV of a temperature channel. +* The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using * 100k pullup. The hardware applies offset/slope to adc code. * @SCALE_HW_CALIB_THERM_100K_PU_PM7: Returns temperature in millidegC using @@ -114,6 +116,7 @@ enum vadc_scale_fn_type {SCALE_PMI_CHG_TEMP ,SCALE_HW_CALIB_DEFAULT ,SCALE_HW_CALIB_THERM_100K_PULLUP , +SCALE_HW_CALIB_THERM_VOLT ,SCALE_HW_CALIB_XOTHERM ,SCALE_HW_CALIB_THERM_100K_PU_PM7 , SCALE_HW_CALIB_PMIC_THERM,
PATCH v4 2/5 dt-bindings: thermal: generic-adc: Document #io-channel-cells
In reply to Luca WeissDocument the property to allow using this sensor with io-channels.
This means that the output of generic-adc-thermal can be used as an input into a different node.
As a specific example, generic-adc-thermal takes in an ADC io-channel in Volt, outputs temperature in degC, which can then be consumed by a fuel gauge driver to provide the battery temperature readings to user space.
--- Documentation/devicetree/bindings/thermal/generic-adc-thermal.yaml | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/thermal/generic-adc-thermal.yaml +4 -0
@@ -24,6 +24,9 @@ properties: '#thermal-sensor-cells': const: 0 + "#io-channel-cells": + const: 0 + io-channels: maxItems: 1
@@ -58,6 +61,7 @@ examples: thermal-sensor { compatible = "generic-adc-thermal"; #thermal-sensor-cells = <0>; + #io-channel-cells = <0>; io-channels = <&ads1015 1>; io-channel-names = "sensor-channel"; temperature-lookup-table = <
Signature · 1 line
2.56.1
Rob Herring
maintainerRe: [PATCH v4 2/5] dt-bindings: thermal: generic-adc: Document #io-channel-cells
Quoted 8 lines · Luca Weiss
Document the property to allow using this sensor with io-channels.
This means that the output of generic-adc-thermal can be used as an input into a different node.
As a specific example, generic-adc-thermal takes in an ADC io-channel in Volt, outputs temperature in degC, which can then be consumed by a fuel gauge driver to provide the battery temperature readings to user space.
But this is already a thermal-sensor provider, why wouldn't the fuel gauge use that provider?
Seems like this is letting current Linux driver design dictate the binding.
Rob
Re: [PATCH v4 2/5] dt-bindings: thermal: generic-adc: Document #io-channel-cells
Quoted 6 lines · Rob Herring
Quoted 5 lines · Luca Weiss
Document the property to allow using this sensor with io-channels.
This means that the output of generic-adc-thermal can be used as an input into a different node.
Quoted 6 lines
Quoted 3 lines
As a specific example, generic-adc-thermal takes in an ADC io-channel in Volt, outputs temperature in degC, which can then be consumed by a fuel gauge driver to provide the battery temperature readings to user space.
But this is already a thermal-sensor provider, why wouldn't the fuel gauge use that provider?
thermal-sensors = <&my_generic_adc_thermal>; could work, yes, but IIO is way more flexible and can support the required use case very well.
From what I can see no device binding (or Linux) uses thermal-sensors apart from thermal zones, so actually doing this for another device feels like re-inventing the wheel when IIO already exists.
Please also see the cover letter from the RFC[0] under "1. Do not implement IIO device in generic-adc-thermal"
[0] https://lore.kernel.org/linux-arm-msm/[email protected]/
Regards
Luca
Quoted 4 lines
Seems like this is letting current Linux driver design dictate the binding.
Rob
Re: [PATCH v4 2/5] dt-bindings: thermal: generic-adc: Document #io-channel-cells
Hi Rob,
Quoted 17 lines · Luca Weiss
Quoted 6 lines · Rob Herring
Quoted 5 lines · Luca Weiss
Document the property to allow using this sensor with io-channels.
This means that the output of generic-adc-thermal can be used as an input into a different node.
Quoted 6 lines
Quoted 3 lines
As a specific example, generic-adc-thermal takes in an ADC io-channel in Volt, outputs temperature in degC, which can then be consumed by a fuel gauge driver to provide the battery temperature readings to user space.
But this is already a thermal-sensor provider, why wouldn't the fuel gauge use that provider?
thermal-sensors = <
&my_generic_adc_thermal>; could work, yes, but IIO is way more flexible and can support the required use case very well.
Quoted 8 lines
From what I can see no device binding (or Linux) uses thermal-sensors apart from thermal zones, so actually doing this for another device feels like re-inventing the wheel when IIO already exists.
Please also see the cover letter from the RFC[0] under "1. Do not implement IIO device in generic-adc-thermal"
[0] https://lore.kernel.org/linux-arm-msm/[email protected]/
Do you have any more feedback for this?
Regards
Luca
Re: [PATCH v4 2/5] dt-bindings: thermal: generic-adc: Document #io-channel-cells
In reply to Luca WeissQuoted 5 lines · Luca Weiss
Document the property to allow using this sensor with io-channels.
This means that the output of generic-adc-thermal can be used as an input into a different node.
Quoted 8 lines
As a specific example, generic-adc-thermal takes in an ADC io-channel in Volt, outputs temperature in degC, which can then be consumed by a fuel gauge driver to provide the battery temperature readings to user space.
Signed-off-by Luca Weiss <[email protected]>--- Documentation/devicetree/bindings/thermal/generic-adc-thermal.yaml | 4 ++++ 1 file changed, 4 insertions(+)
PATCH v4 3/5 thermal/drivers/generic-adc: Allow probe without TZ registration
In reply to Luca WeissIf the devicetree node is not referenced in a zone under /thermal-zones, devm_thermal_of_zone_register() will fail with -ENODEV.
Since the driver is now also registering as an IIO device[0], allow the probe to continue without the thermal zone.
We also can't use gadc_thermal_get_temp() anymore because we haven't necessarily initialized tz_dev.
--- drivers/thermal/thermal-generic-adc.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-)
drivers/thermal/thermal-generic-adc.c +15 -12
@@ -55,9 +55,8 @@ static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)return temp; } -static int gadc_thermal_get_temp (struct thermal_zone_device *tz,int *temp) +static int gadc_thermal_iio_to_temp (struct gadc_thermal_info *gti,int *temp) { -struct gadc_thermal_info *gti =thermal_zone_device_priv (tz);int val;int ret;
@@ -70,6 +69,13 @@ static int gadc_thermal_get_temp(struct thermal_zone_device *tz, int *temp)return 0 ; } +static int gadc_thermal_get_temp (struct thermal_zone_device *tz,int *temp) +{ +struct gadc_thermal_info *gti =thermal_zone_device_priv (tz); + +return gadc_thermal_iio_to_temp (gti, temp); +} +static const struct thermal_zone_device_ops gadc_thermal_ops = { .get_temp = gadc_thermal_get_temp, };
@@ -90,7 +96,7 @@ static int gadc_thermal_read_raw(struct iio_dev *indio_dev,switch (mask) {case IIO_CHAN_INFO_PROCESSED : - ret =gadc_thermal_get_temp (gtinfo->tz_dev, val); + ret =gadc_thermal_iio_to_temp (gtinfo, val);if (ret)return ret;
@@ -193,16 +199,13 @@ static int gadc_thermal_probe(struct platform_device *pdev) gti->tz_dev =devm_thermal_of_zone_register (dev,0 , gti, &gadc_thermal_ops); -if (IS_ERR (gti->tz_dev)) { - ret =PTR_ERR (gti->tz_dev); -if (ret != -EPROBE_DEFER ) -dev_err (dev, -"Thermal zone sensor register failed: %d\n" , - ret); + ret =PTR_ERR_OR_ZERO (gti->tz_dev); +if (ret == -ENODEV ) +dev_dbg (dev,"Thermal zone sensor register failed: %d\n" , ret); +else if (ret)return ret; - } - -devm_thermal_add_hwmon_sysfs (dev, gti->tz_dev); +else +devm_thermal_add_hwmon_sysfs (dev, gti->tz_dev);return gadc_iio_register (&pdev->dev, gti); }
Signature · 1 line
2.56.1
PATCH v4 4/5 arm64: dts: qcom: pm7250b: Define battery temperature ADC channels
In reply to Luca WeissAdd the definition for these ADC channels to the PM7250B definition.
--- arch/arm64/boot/dts/qcom/pm7250b.dtsi | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
arch/arm64/boot/dts/qcom/pm7250b.dtsi +24 -0
@@ -155,6 +155,22 @@ channel@1e { label = "chg_mid"; }; + channel@2a { + reg = <ADC5_BAT_THERM_30K_PU>; + qcom,hw-settle-time = <200>; + qcom,pre-scaling = <1 1>; + qcom,ratiometric; + label = "bat_therm_30k"; + }; + + channel@4a { + reg = <ADC5_BAT_THERM_100K_PU>; + qcom,hw-settle-time = <200>; + qcom,pre-scaling = <1 1>; + qcom,ratiometric; + label = "bat_therm_100k"; + }; + channel@4b { reg = <ADC5_BAT_ID_100K_PU>; qcom,hw-settle-time = <200>;
@@ -163,6 +179,14 @@ channel@4b { label = "bat_id"; }; + channel@6a { + reg = <ADC5_BAT_THERM_400K_PU>; + qcom,hw-settle-time = <200>; + qcom,pre-scaling = <1 1>; + qcom,ratiometric; + label = "bat_therm_400k"; + }; + channel@83 { reg = <ADC5_VPH_PWR>; qcom,pre-scaling = <1 3>;
Signature · 1 line
2.56.1
PATCH v4 5/5 arm64: dts: qcom: sm7225-fairphone-fp4: Add battery temperature node
In reply to Luca WeissAdd a generic-adc-thermal node to convert the voltage (in millivolt) read by the battery temperature ADC into degree Celsius (in millicelsius) using the provided lookup table.
This will later be used as input for the fuel gauge node (QGauge on the PM7250B).
The lookup table is generated using a Python script:
https://codeberg.org/lucaweiss/batt-ntc-calc
--- arch/arm64/boot/dts/qcom/sm7225-fairphone-fp4.dts | 83 +++++++++++++++++++++++ 1 file changed, 83 insertions(+)
arch/arm64/boot/dts/qcom/sm7225-fairphone-fp4.dts +83 -0
@@ -96,6 +96,89 @@ memory@efe01000 { }; }; + bat_therm_sensor: thermal-sensor-bat-therm { + compatible = "generic-adc-thermal"; + #thermal-sensor-cells = <0>; + #io-channel-cells = <0>; + io-channels = <&pm7250b_adc ADC5_BAT_THERM_30K_PU>; + io-channel-names = "sensor-channel"; + /* + * Voltage to temperature table for 10kΩ (B=3435K) NTC with a + * 1.875V reference and 30kΩ pull-up. + */ + temperature-lookup-table = < + (-40000) 1673 + (-38000) 1649 + (-36000) 1623 + (-34000) 1596 + (-32000) 1566 + (-30000) 1535 + (-28000) 1502 + (-26000) 1467 + (-24000) 1430 + (-22000) 1392 + (-20000) 1352 + (-18000) 1311 + (-16000) 1269 + (-14000) 1226 + (-12000) 1182 + (-10000) 1138 + (-8000) 1093 + (-6000) 1049 + (-4000) 1004 + (-2000) 960 + 0 917 + 2000 874 + 4000 832 + 6000 791 + 8000 752 + 10000 713 + 12000 676 + 14000 640 + 16000 606 + 18000 573 + 20000 541 + 22000 511 + 24000 483 + 26000 455 + 28000 430 + 30000 405 + 32000 382 + 34000 360 + 36000 340 + 38000 320 + 40000 302 + 42000 285 + 44000 269 + 46000 253 + 48000 239 + 50000 225 + 52000 213 + 54000 201 + 56000 190 + 58000 179 + 60000 169 + 62000 160 + 64000 152 + 66000 143 + 68000 136 + 70000 128 + 72000 122 + 74000 115 + 76000 109 + 78000 104 + 80000 98 + 82000 93 + 84000 89 + 86000 84 + 88000 80 + 90000 76 + 92000 73 + 94000 69 + 96000 66 + 98000 63>; + }; + msm_therm_sensor: thermal-sensor-msm { compatible = "generic-adc-thermal"; #thermal-sensor-cells = <0>;
Signature · 1 line
2.56.1