thermal/drivers/airoha: Fix AN7583 mux mapping and expose all sensors
Subsystems (2)
cover thermal/drivers/airoha: Fix AN7583 mux mapping and expose all sensors
The AN7583 support merged for 7.3-rc1 reads the wrong mux positions: the mux field names are swapped, and v5 of the series swapped the call arguments to match them. Patch 1 fixes the field definitions.
Patch 2 serialises mux selection and ADC sampling.
Patches 3 and 4 add one thermal sensor cell for AN7583 and a zone per sensor. Device trees without cells keep reading the bandgap sensor.
Tested on a Nokia XG-040G-MF (AN7583) with this driver on a 6.18 kernel in OpenWrt. Without the series the board powers off 2.6 s into boot. With it the three zones match direct ADC reads, and concurrent readers during CPU frequency changes gave no failed read and no shutdown. EN7581 was tested with equivalent changes on a Nokia XG-040G-MD. Build-tested on arm64 with W=1; the binding passes dt_binding_check.
Vitaliy Sochnev (4):
thermal/drivers/airoha: Fix AN7583 ADC mux field mapping thermal/drivers/airoha: Serialise access to the shared ADC
dt-bindings: arm: airoha: Allow one thermal sensor cell for AN7583 thermal/drivers/airoha: Register a thermal zone per AN7583 sensor
.../bindings/arm/airoha,en7581-chip-scu.yaml | 5 +- drivers/thermal/airoha_thermal.c | 159 ++++++++++++++---- 2 files changed, 128 insertions(+), 36 deletions(-)
base-commit: 5ad0af4f4367202b1bc71813052fe39b5116cdb9
Signature · 1 line
2.55.0
PATCH 1/4 thermal/drivers/airoha: Fix AN7583 ADC mux field mapping
On AN7583, bits 3:1 of 0x2e4 select the sensor (0 bandgap, 5 GbE, 6 CPU) and bits 3:2 of 0x2a0 select the diode (0 D0, 1 ZERO, 2 D1). The driver names them the other way round: MUX_TADC is 0x2e4, MUX_SENSOR is 0x2a0.
v5 of the AN7583 series swapped the airoha_set_thermal_mux() arguments to match these names. The diode index now goes to the sensor selector, so the zone reads mux positions 1, 0 and 2 (PAD_AVS, bandgap, core voltage) instead of the bandgap diodes. On a Nokia XG-040G-MF the result crosses the critical trip and the board powers off during boot.
Swap the field definitions. MUX_TADC then selects the diode, as on EN7581, and the existing call order is correct.
--- drivers/thermal/airoha_thermal.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/thermal/airoha_thermal.c +5 -5
@@ -18,10 +18,10 @@#define EN7581_DOUT_TADC 0x2f8#define EN7581_DOUT_TADC_MASK GENMASK(15, 0) -#define AN7583_MUX_SENSOR 0x2a0 +#define AN7583_MUX_TADC 0x2a0#define AN7583_LOAD_ADJ GENMASK(3, 2) -#define AN7583_MUX_TADC 0x2e4 -#define AN7583_MUX_TADC_MASK GENMASK(3, 1) +#define AN7583_MUX_SENSOR 0x2e4 +#define AN7583_MUX_SENSOR_MASK GENMASK(3, 1)#define AN7583_DOUT_TADC 0x2f0/* PTP_THERMAL regs */
@@ -634,8 +634,8 @@ static const struct thermal_zone_device_ops an7583_tz_ops = {static const struct reg_field an7583_chip_scu_fields[AIROHA_THERMAL_FIELD_MAX ] = { [AIROHA_THERMAL_DOUT_TADC ] =REG_FIELD (AN7583_DOUT_TADC ,0 ,31 ), - [AIROHA_THERMAL_MUX_TADC ] =REG_FIELD (AN7583_MUX_TADC ,1 ,3 ), - [AIROHA_THERMAL_MUX_SENSOR ] =REG_FIELD (AN7583_MUX_SENSOR ,2 ,3 ), + [AIROHA_THERMAL_MUX_TADC ] =REG_FIELD (AN7583_MUX_TADC ,2 ,3 ), + [AIROHA_THERMAL_MUX_SENSOR ] =REG_FIELD (AN7583_MUX_SENSOR ,1 ,3 ), }; static int an7583_thermal_probe(struct platform_device *pdev,
Signature · 1 line
2.55.0
PATCH 2/4 thermal/drivers/airoha: Serialise access to the shared ADC
In reply to Vitaliy SochnevA read selects the sensor and walks three diodes with a 10 ms settle on each. Nothing serialises this, so concurrent reads interleave and convert samples taken at another mux position.
Mux writes are dropped silently unless PLLRG_PROTECT is lifted, and its save and restore do not nest. The cached TADC value also skips a write after another reader has moved the mux.
With the AN7583 sensors read as parallel zones during CPU frequency changes, a zone crossed its critical trip on a chip at 55 C and the board shut down.
Serialise the sequence, drop the cache, check the selection after writing and after sampling, and return -EAGAIN if it does not hold.
--- drivers/thermal/airoha_thermal.c | 85 +++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 17 deletions(-)
drivers/thermal/airoha_thermal.c +68 -17
@@ -5,6 +5,7 @@#include <linux/delay.h> #include <linux/interrupt.h> #include <linux/mfd/syscon.h> +#include <linux/mutex.h> #include <linux/of.h> #include <linux/of_address.h> #include <linux/platform_device.h>
@@ -191,6 +192,7 @@#define AN7583_NUM_SENSOR 3#define AIROHA_THERMAL_NO_MUX_SENSOR -1 +#define AIROHA_THERMAL_MUX_TRIES 3/* Convert temp to raw value as read from ADC ((((temp / 100) - init) * slope) / 1000) + offset */ #define TEMP_TO_RAW (priv, temp) ((((((temp) / 100) - (priv)->init_temp) * \
@@ -250,7 +252,8 @@ struct airoha_thermal_priv {struct resource scu_adc_res;u32 pllrg_protect; -int current_adc; +/* Serialises mux selection and ADC sampling */ +struct mutex lock;struct thermal_zone_device *tz;int init_temp;
@@ -294,9 +297,26 @@ static int airoha_get_thermal_ADC(struct airoha_thermal_priv *priv)return val; } +static bool airoha_thermal_mux_holds (struct airoha_thermal_priv *priv, +int tdac_idx,int sensor_idx) +{ +unsigned int val; + +if (regmap_field_read (priv->chip_scu_fields[AIROHA_THERMAL_MUX_TADC ], &val) || + val != tdac_idx) +return false; + +if (sensor_idx ==AIROHA_THERMAL_NO_MUX_SENSOR ) +return true; + +return !regmap_field_read (priv->chip_scu_fields[AIROHA_THERMAL_MUX_SENSOR ], &val) && + val == sensor_idx; +} +static void airoha_set_thermal_mux (struct airoha_thermal_priv *priv,int tdac_idx,int sensor_idx) { +int tries =AIROHA_THERMAL_MUX_TRIES ;u32 pllrg;/* Save PLLRG current value */
@@ -307,19 +327,17 @@ static void airoha_set_thermal_mux(struct airoha_thermal_priv *priv, priv->pllrg_protect);/* -* Configure Thermal Sensor mux to sensor_idx. +* Configure Thermal Sensor mux to sensor_idx and Thermal ADC mux to +* tdac_idx. Writes can be dropped silently, so read them back. * (if not supported, sensor_idx is AIROHA_THERMAL_NO_MUX_SENSOR) */ -if (sensor_idx !=AIROHA_THERMAL_NO_MUX_SENSOR ) -regmap_field_write (priv->chip_scu_fields[AIROHA_THERMAL_MUX_SENSOR ], - sensor_idx); - -/* Configure Thermal ADC mux to tdac_idx */ -if (priv->current_adc != tdac_idx) { +do { +if (sensor_idx !=AIROHA_THERMAL_NO_MUX_SENSOR ) +regmap_field_write (priv->chip_scu_fields[AIROHA_THERMAL_MUX_SENSOR ], + sensor_idx);regmap_field_write (priv->chip_scu_fields[AIROHA_THERMAL_MUX_TADC ], tdac_idx); - priv->current_adc = tdac_idx; - } + }while (!airoha_thermal_mux_holds (priv, tdac_idx, sensor_idx) && --tries);/* Restore PLLRG value on exit */ regmap_write (priv->chip_scu,EN7581_PLLRG_PROTECT , pllrg);
@@ -594,12 +612,40 @@ static int en7581_thermal_post_probe(struct platform_device *pdev)return 0 ; } +static int an7583_thermal_read_diode (struct airoha_thermal_priv *priv, +int diode,int sensor_idx,int *val) +{ +airoha_set_thermal_mux (priv, diode, sensor_idx); + *val =airoha_get_thermal_ADC (priv); + +return airoha_thermal_mux_holds (priv, diode, sensor_idx) ?0 : -EAGAIN ; +} + +static int an7583_thermal_read_diodes (struct airoha_thermal_priv *priv, +int sensor_idx,int *zero,int *d0, +int *d1) +{ +int ret; + + ret =an7583_thermal_read_diode (priv,AN7583_ZERO_TADC , sensor_idx, zero); +if (ret) +return ret; + + ret =an7583_thermal_read_diode (priv,AN7583_D0_TADC , sensor_idx, d0); +if (ret) +return ret; + +return an7583_thermal_read_diode (priv,AN7583_D1_TADC , sensor_idx, d1); +} +static int an7583_thermal_get_temp (struct thermal_zone_device *tz,int *temp) {struct airoha_thermal_priv *priv =thermal_zone_device_priv (tz);int sensor_idx;int delta_diode, delta_gain;int coeff, slope, offset; +int tries =AIROHA_THERMAL_MUX_TRIES ; +int ret;int diode_zero, diode_d0, diode_d1;
@@ -610,12 +656,14 @@ static int an7583_thermal_get_temp(struct thermal_zone_device *tz, int *temp) slope = an7583_thermal_slope[sensor_idx]; offset = an7583_thermal_offset[sensor_idx]; -airoha_set_thermal_mux (priv,AN7583_ZERO_TADC , sensor_idx); - diode_zero =airoha_get_thermal_ADC (priv); -airoha_set_thermal_mux (priv,AN7583_D0_TADC , sensor_idx); - diode_d0 =airoha_get_thermal_ADC (priv); -airoha_set_thermal_mux (priv,AN7583_D1_TADC , sensor_idx); - diode_d1 =airoha_get_thermal_ADC (priv); +mutex_lock (&priv->lock); +do { + ret =an7583_thermal_read_diodes (priv, sensor_idx, &diode_zero, + &diode_d0, &diode_d1); + }while (ret == -EAGAIN && --tries); +mutex_unlock (&priv->lock); +if (ret) +return ret; delta_diode = diode_d1 - diode_d0; delta_gain = (delta_diode * coeff) /100 + (diode_zero - diode_d1);
@@ -676,7 +724,10 @@ static int airoha_thermal_probe(struct platform_device *pdev)return -ENOMEM ; priv->pllrg_protect = soc_data->pllrg_protect; - priv->current_adc =-1 ; + + ret =devm_mutex_init (dev, &priv->lock); +if (ret) +return ret;if (!soc_data->probe)return -EINVAL ;
Signature · 1 line
2.55.0
PATCH 3/4 dt-bindings: arm: airoha: Allow one thermal sensor cell for AN7583
In reply to Vitaliy SochnevAN7583 has three temperature sensors behind the chip SCU: bandgap, GbE and CPU. Allow one cell to select the sensor. Zero cells stay valid and refer to the bandgap sensor.
--- .../devicetree/bindings/arm/airoha,en7581-chip-scu.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/arm/airoha,en7581-chip-scu.yaml +4 -1
@@ -26,7 +26,10 @@ properties: maxItems: 1 '#thermal-sensor-cells': - const: 0 + description: + 0 refers to the bandgap sensor. 1 selects the sensor with one cell, + 0 bandgap, 1 GbE, 2 CPU. + enum: [0, 1] required: - compatible
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 3/4] dt-bindings: arm: airoha: Allow one thermal sensor cell for AN7583
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `#thermal-sensor-cells` property uses an `enum` constraint instead of a `const` constraint. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The #thermal-sensor-cells property uses an enum constraint instead of a const constraint. --
commit a2ea4a9a05333c50b5a23468b0906bdb46e74f5f
Author: Vitaliy Sochnev <[email protected]>
dt-bindings: arm: airoha: Allow one thermal sensor cell for AN7583
This commit modifies the AN7583 chip SCU device tree binding to allow the #thermal-sensor-cells property to accept either 0 or 1. It updates the validation constraints to use an enum instead of a strict constant.
Documentation/devicetree/bindings/arm/airoha,en7581-chip-scu.yaml properties: · line 32
Quoted 13 lines
Documentation/devicetree/bindings/arm/airoha,en7581-chip-scu.yaml +4
-1@@ -26,7 +26,10 @@ properties: maxItems: 1 '#thermal-sensor-cells': - const: 0 + description: + 0 refers to the bandgap sensor. 1 selects the sensor with one cell, + 0 bandgap, 1 GbE, 2 CPU. + enum: [0, 1]
[Severity: Medium]
Does using an enum for this cell-count property violate the subsystem architectural rules?
The Device Tree binding subsystem guidelines explicitly mandate that every cell-count property must have a strict const constraint matching the hardware.
Using an enum here allows variable cell counts for the same compatible string, which causes schema validation to accept ambiguous hardware configurations rather than enforcing a single hardware-matching constant size.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/4 thermal/drivers/airoha: Register a thermal zone per AN7583 sensor
In reply to Vitaliy SochnevAN7583 has three temperature sensors and the driver has constants for all of them, but it registers one zone and reads only the bandgap sensor.
Register a zone per sensor. With one thermal sensor cell the cell selects the sensor. Device trees without cells describe only zone 0 and keep reading the bandgap sensor. EN7581 still registers one zone.
Idle and with both cores loaded on a Nokia XG-040G-MF:
bandgap 56.9 C -> 59.3 C GbE 58.7 C -> 61.5 C CPU 57.0 C -> 61.1 C
--- drivers/thermal/airoha_thermal.c | 64 +++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 13 deletions(-)
drivers/thermal/airoha_thermal.c +51 -13
@@ -245,6 +245,11 @@ enum airoha_thermal_chip_scu_field { AIROHA_THERMAL_FIELD_MAX, }; +struct airoha_thermal_zone { +struct airoha_thermal_priv *priv; +int sensor; +}; +struct airoha_thermal_priv {struct regmap *map;struct regmap *chip_scu;
@@ -263,6 +268,9 @@ struct airoha_thermal_priv {struct airoha_thermal_soc_data {u32 pllrg_protect; +/* Sensor mux value per zone, NULL without a sensor mux */ +const int *sensors; +int num_sensors;const struct thermal_zone_device_ops *thdev_ops; int (*probe)(struct platform_device *pdev,
@@ -348,7 +356,8 @@ static void airoha_set_thermal_mux(struct airoha_thermal_priv *priv,static int en7581_thermal_get_temp (struct thermal_zone_device *tz,int *temp) { -struct airoha_thermal_priv *priv =thermal_zone_device_priv (tz); +struct airoha_thermal_zone *zone =thermal_zone_device_priv (tz); +struct airoha_thermal_priv *priv = zone->priv;int min_value, max_value, avg_value, value;int i;
@@ -374,7 +383,8 @@ static int en7581_thermal_get_temp(struct thermal_zone_device *tz, int *temp)static int en7581_thermal_set_trips (struct thermal_zone_device *tz,int low,int high) { -struct airoha_thermal_priv *priv =thermal_zone_device_priv (tz); +struct airoha_thermal_zone *zone =thermal_zone_device_priv (tz); +struct airoha_thermal_priv *priv = zone->priv;bool enable_monitor = false;if (high !=INT_MAX ) {
@@ -640,8 +650,9 @@ static int an7583_thermal_read_diodes(struct airoha_thermal_priv *priv,static int an7583_thermal_get_temp (struct thermal_zone_device *tz,int *temp) { -struct airoha_thermal_priv *priv =thermal_zone_device_priv (tz); -int sensor_idx; +struct airoha_thermal_zone *zone =thermal_zone_device_priv (tz); +struct airoha_thermal_priv *priv = zone->priv; +int sensor_idx = zone->sensor;int delta_diode, delta_gain;int coeff, slope, offset;int tries =AIROHA_THERMAL_MUX_TRIES ;
@@ -649,9 +660,6 @@ static int an7583_thermal_get_temp(struct thermal_zone_device *tz, int *temp)int diode_zero, diode_d0, diode_d1; -/* Always read sensor AN7583_BGP_TEMP_SENSOR */ - sensor_idx =AN7583_BGP_TEMP_SENSOR ; - coeff = an7583_thermal_coeff[sensor_idx]; slope = an7583_thermal_slope[sensor_idx]; offset = an7583_thermal_offset[sensor_idx];
@@ -715,7 +723,7 @@ static int airoha_thermal_probe(struct platform_device *pdev)const struct airoha_thermal_soc_data *soc_data;struct airoha_thermal_priv *priv;struct device *dev = &pdev->dev; -int ret; +int ret, i; soc_data =device_get_match_data (dev);
@@ -737,11 +745,31 @@ static int airoha_thermal_probe(struct platform_device *pdev)return ret;/* register of thermal sensor and get info from DT */ - priv->tz =devm_thermal_of_zone_register (dev,0 , priv, - soc_data->thdev_ops); -if (IS_ERR (priv->tz)) { -dev_err (dev,"register thermal zone sensor failed\n" ); -return PTR_ERR (priv->tz); +for (i =0 ; i < soc_data->num_sensors; i++) { +struct airoha_thermal_zone *zone; +struct thermal_zone_device *tz; + + zone =devm_kzalloc (dev,sizeof (*zone),GFP_KERNEL ); +if (!zone) +return -ENOMEM ; + + zone->priv = priv; + zone->sensor = soc_data->sensors ? soc_data->sensors[i] : +AIROHA_THERMAL_NO_MUX_SENSOR ; + + tz =devm_thermal_of_zone_register (dev, i, zone, + soc_data->thdev_ops); +if (IS_ERR (tz)) { +/* A DT may describe fewer zones than sensors */ +if (i &&PTR_ERR (tz) == -ENODEV ) +continue ; + +dev_err (dev,"register thermal zone %d failed\n" , i); +return PTR_ERR (tz); + } + +if (!i) + priv->tz = tz; }platform_set_drvdata (pdev, priv);
@@ -749,7 +777,15 @@ static int airoha_thermal_probe(struct platform_device *pdev)return soc_data->post_probe ? soc_data->post_probe (pdev) :0 ; } +/* Zone order; the bandgap sensor stays zone 0 */ +static const int an7583_zone_sensors[AN7583_NUM_SENSOR ] = { +AN7583_BGP_TEMP_SENSOR , +AN7583_GBE_TEMP_SENSOR , +AN7583_CPU_TEMP_SENSOR , +}; +static const struct airoha_thermal_soc_data en7581_data = { + .num_sensors =1 , .pllrg_protect =EN7581_SCU_THERMAL_PROTECT_KEY , .thdev_ops = &en7581_thdev_ops, .probe = &en7581_thermal_probe,
@@ -757,6 +793,8 @@ static const struct airoha_thermal_soc_data en7581_data = { };static const struct airoha_thermal_soc_data an7583_data = { + .sensors = an7583_zone_sensors, + .num_sensors =AN7583_NUM_SENSOR , .pllrg_protect =AN7583_SCU_THERMAL_PROTECT_KEY , .thdev_ops = &an7583_tz_ops, .probe = &an7583_thermal_probe,
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 4/4] thermal/drivers/airoha: Register a thermal zone per AN7583 sensor
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] IRQ requested before thermal zone is registered and freed after thermal zone is destroyed, causing a race window that can result in a NULL pointer dereference or Use-After-Free. --
commit 749e5e99054c0cc567360c9cf2985ce4702c7297
Author: Vitaliy Sochnev <[email protected]>
thermal/drivers/airoha: Register a thermal zone per AN7583 sensor
AN7583 has three temperature sensors and the driver has constants for all of them, but it registers one zone and reads only the bandgap sensor.
Register a zone per sensor. With one thermal sensor cell the cell selects the sensor. Device trees without cells describe only zone 0 and keep reading the bandgap sensor. EN7581 still registers one zone.
Quoted 4 lines
drivers/thermal/airoha_thermal.c +0
-0
[ ... ]
Quoted 24 lines
drivers/thermal/airoha_thermal.c +15
-5@@ -737,11 +745,31 @@ static int airoha_thermal_probe(struct platform_device *pdev)return ret;/* register of thermal sensor and get info from DT */ - priv->tz =devm_thermal_of_zone_register (dev,0 , priv, - soc_data->thdev_ops); -if (IS_ERR (priv->tz)) { -dev_err (dev,"register thermal zone sensor failed\n" ); -return PTR_ERR (priv->tz); +for (i =0 ; i < soc_data->num_sensors; i++) { +struct airoha_thermal_zone *zone; +struct thermal_zone_device *tz; + + zone =devm_kzalloc (dev,sizeof (*zone),GFP_KERNEL ); +if (!zone) +return -ENOMEM ; + + zone->priv = priv; + zone->sensor = soc_data->sensors ? soc_data->sensors[i] : +AIROHA_THERMAL_NO_MUX_SENSOR ; + + tz =devm_thermal_of_zone_register (dev, i, zone, + soc_data->thdev_ops); +if (IS_ERR (tz)) {
drivers/thermal/airoha_thermal.c static int airoha_thermal_probe(struct platform_device *pdev) · line 772
Quoted 10 lines
drivers/thermal/airoha_thermal.c +10
-0+/* A DT may describe fewer zones than sensors */ +if (i &&PTR_ERR (tz) == -ENODEV ) +continue ; + +dev_err (dev,"register thermal zone %d failed\n" , i); +return PTR_ERR (tz); + } + +if (!i) + priv->tz = tz;
[Severity: High]
This isn't a bug introduced by this patch, but is it possible for the IRQ handler to dereference priv->tz when it is NULL or freed?
Looking at airoha_thermal_probe(), soc_data->probe() is called before priv->tz is initialized here. For EN7581, en7581_thermal_probe() requests the threaded IRQ:
ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, en7581_thermal_irq, IRQF_ONESHOT, pdev->name, priv);
This creates a race window during probe where the IRQ could fire before priv->tz is assigned, causing a NULL pointer dereference in en7581_thermal_irq():
if (update) thermal_zone_device_update(priv->tz, event);
Furthermore, because devm_request_threaded_irq() is called before devm_thermal_of_zone_register(), devres teardown will destroy the thermal zone before freeing the IRQ. Unbinding the driver via sysfs or encountering a probe error path could leave the IRQ active while priv->tz points to freed memory, leading to a Use-After-Free.
Quoted 3 lines
drivers/thermal/airoha_thermal.c +0
-0}platform_set_drvdata (pdev, priv);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4