mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/5] thermal/drivers/mediatek/lvts: CALSCALE, overflow, trip, and IRQ fixes
@ 2026-12-03  2:19 Ilia Snezhkov via B4 Relay
  2026-12-03  2:19 ` [PATCH v2 1/5] thermal/drivers/mediatek/lvts: write LVTS_CALSCALE in configure Ilia Snezhkov via B4 Relay
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-12-03  2:19 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, bchihi, nfraprado,
	linux-pm, linux-mediatek, linux-arm-kernel, phone-devel,
	linux-kernel

This five-patch series fixes bugs in
drivers/thermal/mediatek/lvts_thermal.c:

1. Write LVTS_CALSCALE_CONF (0x300) to LVTS_CALSCALE instead of
   overwriting a local variable and discarding it.
2. Avoid 32-bit overflow, negative left shift, and unsigned wraparound
   in lvts_temp_to_raw_mt7988().
3. Serialize lvts_set_trips() with a per-controller spinlock_t so
   concurrent updates across sibling thermal zones cannot race on
   shared thresholds or LVTS_MONINT.
4. Recompute shared controller thresholds safely across sibling sensors
   (excluding INT_MAX from winning high_thresh, falling back to the
   next highest finite sibling on cooldown, matching low/high IRQ masks
   independently, skipping lvts_temp_to_raw(INT_MAX), and masking
   offset IRQs before writing LVTS_OFFSETL/LVTS_OFFSETH).
5. Check lvts_ctrl->sensors[i].tz before calling
   thermal_zone_device_update() in lvts_ctrl_irq_handler().

---
v1: https://lore.kernel.org/r/20261126005321.71799-1-iasnezhkov@gmail.com

Changes in v2:
- Split the INT_MAX trip handling out of patch 2/5 so patch 2/5 only
  fixes lvts_temp_to_raw_mt7988() (now returning 0 early when
  golden_temp_offset - temperature <= 0 to avoid shifting a negative
  s64) and corrected the golden_temp boundary example in its commit
  message (LVTS_GOLDEN_TEMP_MAX is 62, not 64).
- Added patch 4/5 combining the INT_MAX trip handling with full sibling
  threshold recomputation (lvts_recompute_thresh()), independent
  low/high IRQ mask matching, leaving LVTS_OFFSETH untouched when
  new_high == INT_MAX, and masking offset IRQs before writing
  LVTS_OFFSETL/LVTS_OFFSETH.
- Shortened patch subjects and normalized prefix to
  "thermal/drivers/mediatek/lvts:".

Testing note:
- Patches 1, 2, 3, and 5 were regression-tested on MT6789 hardware
  (iKKO MindOne, factory golden_temp = 60) in an out-of-tree port of
  lvts_thermal.c on ACK 6.18 (the unpopulated-tz IRQ path in patch 5
  was not directly triggered on this hardware).
- The exact lvts_temp_to_raw_mt7988() boundary cases and the patch 4/5
  lvts_recompute_thresh() / MMIO write-ordering state machine were
  verified in a host userspace test harness under ASan and UBSan, not
  directly on mainline hardware.

Found and drafted with an AI coding assistant.

Ilia Snezhkov (5):
  thermal/drivers/mediatek/lvts: write LVTS_CALSCALE in configure
  thermal/drivers/mediatek/lvts: fix 32-bit overflow in temp_to_raw
  thermal/drivers/mediatek/lvts: serialize lvts_set_trips() per ctrl
  thermal/drivers/mediatek/lvts: recompute shared trips and mask first
  thermal/drivers/mediatek/lvts: skip sensors without tz in IRQ

 drivers/thermal/mediatek/lvts_thermal.c | 220 +++++++++++++++---------
 1 file changed, 142 insertions(+), 78 deletions(-)

-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/5] thermal/drivers/mediatek/lvts: write LVTS_CALSCALE in configure
  2026-12-03  2:19 [PATCH v2 0/5] thermal/drivers/mediatek/lvts: CALSCALE, overflow, trip, and IRQ fixes Ilia Snezhkov via B4 Relay
@ 2026-12-03  2:19 ` Ilia Snezhkov via B4 Relay
  2026-12-03  2:19 ` [PATCH v2 2/5] thermal/drivers/mediatek/lvts: fix 32-bit overflow in temp_to_raw Ilia Snezhkov via B4 Relay
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-12-03  2:19 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, bchihi, nfraprado,
	linux-pm, linux-mediatek, linux-arm-kernel, phone-devel,
	linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

lvts_ctrl_configure() assigns 0x300 and LVTS_CALSCALE_CONF (0x300) to
the local variable `value`, then immediately overwrites `value` with
the LVTS_MSRCTL0 filter setting without ever writing LVTS_CALSCALE
(0x0048).

Remove the dead duplicate assignment and write LVTS_CALSCALE_CONF to
LVTS_CALSCALE(lvts_ctrl->base), matching the comment above it and
MediaTek's downstream initialization sequence.

Fixes: f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
No code changes since v1.
Found and drafted with an AI coding assistant.

 drivers/thermal/mediatek/lvts_thermal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index 350fffd..e034c6c 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -1197,8 +1197,8 @@ static int lvts_ctrl_configure(struct device *dev, struct lvts_ctrl *lvts_ctrl)
 	/*
 	 * LVTS_CALSCALE : ADC voltage round
 	 */
-	value = 0x300;
 	value = LVTS_CALSCALE_CONF;
+	writel(value, LVTS_CALSCALE(lvts_ctrl->base));
 
 	/*
 	 * LVTS_MSRCTL0 : Sensor filtering strategy
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 2/5] thermal/drivers/mediatek/lvts: fix 32-bit overflow in temp_to_raw
  2026-12-03  2:19 [PATCH v2 0/5] thermal/drivers/mediatek/lvts: CALSCALE, overflow, trip, and IRQ fixes Ilia Snezhkov via B4 Relay
  2026-12-03  2:19 ` [PATCH v2 1/5] thermal/drivers/mediatek/lvts: write LVTS_CALSCALE in configure Ilia Snezhkov via B4 Relay
@ 2026-12-03  2:19 ` Ilia Snezhkov via B4 Relay
  2026-12-03  2:19 ` [PATCH v2 3/5] thermal/drivers/mediatek/lvts: serialize lvts_set_trips() per ctrl Ilia Snezhkov via B4 Relay
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-12-03  2:19 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, bchihi, nfraprado,
	linux-pm, linux-mediatek, linux-arm-kernel, phone-devel,
	linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

lvts_temp_to_raw_mt7988() subtracts two 32-bit ints before casting to
s64, shifts left by 14 bits, and truncates the shifted numerator to
u32 before calling div_s64().

Whenever golden_temp_offset - temperature >= 262144, the shifted value
exceeds U32_MAX and wraps modulo 2^32, producing a small raw value
that represents a much higher temperature than requested. With
LVTS_COEFF_B_MT8195 (250460), this happens for any temperature <=
13316 mC at the default golden_temp of 50 (golden_temp_offset = 275460)
and up to 18816 mC at golden_temp = 61 (just below
LVTS_GOLDEN_TEMP_MAX = 62). In addition, temperatures at or above
golden_temp_offset produce a negative difference, which causes a
negative left shift and unsigned wraparound.

Promote golden_temp_offset to s64 before subtracting, return 0 early
when the difference is non-positive, and pass the s64 shifted value
directly to div_s64().

Fixes: f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes in v2:
- Narrowed to lvts_temp_to_raw_mt7988() only (INT_MAX trip handling
  moved to patch 4/5).
- Return 0 early when golden_temp_offset - temperature <= 0 to avoid
  left-shifting a negative s64.
- Corrected the golden_temp boundary example (LVTS_GOLDEN_TEMP_MAX=62).

Found and drafted with an AI coding assistant.

 drivers/thermal/mediatek/lvts_thermal.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index e034c6c..6d39025 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -322,11 +322,12 @@ static int lvts_raw_to_temp_mt7988(u32 raw_temp, int temp_factor)
 
 static u32 lvts_temp_to_raw_mt7988(int temperature, int temp_factor)
 {
-	u32 raw_temp = ((s64)(golden_temp_offset - temperature)) << 14;
+	s64 diff = (s64)golden_temp_offset - temperature;
 
-	raw_temp = div_s64(raw_temp, -temp_factor);
+	if (diff <= 0)
+		return 0;
 
-	return raw_temp;
+	return div_s64(diff << 14, -temp_factor);
 }
 
 static u32 lvts_temp_to_raw_mt8196(int temperature, int temp_factor)
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 3/5] thermal/drivers/mediatek/lvts: serialize lvts_set_trips() per ctrl
  2026-12-03  2:19 [PATCH v2 0/5] thermal/drivers/mediatek/lvts: CALSCALE, overflow, trip, and IRQ fixes Ilia Snezhkov via B4 Relay
  2026-12-03  2:19 ` [PATCH v2 1/5] thermal/drivers/mediatek/lvts: write LVTS_CALSCALE in configure Ilia Snezhkov via B4 Relay
  2026-12-03  2:19 ` [PATCH v2 2/5] thermal/drivers/mediatek/lvts: fix 32-bit overflow in temp_to_raw Ilia Snezhkov via B4 Relay
@ 2026-12-03  2:19 ` Ilia Snezhkov via B4 Relay
  2026-12-03  2:20 ` [PATCH v2 4/5] thermal/drivers/mediatek/lvts: recompute shared trips and mask first Ilia Snezhkov via B4 Relay
  2026-12-03  2:20 ` [PATCH v2 5/5] thermal/drivers/mediatek/lvts: skip sensors without tz in IRQ Ilia Snezhkov via B4 Relay
  4 siblings, 0 replies; 6+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-12-03  2:19 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, bchihi, nfraprado,
	linux-pm, linux-mediatek, linux-arm-kernel, phone-devel,
	linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

Each struct lvts_ctrl manages up to four sensors, each registered as an
independent thermal_zone_device with its own tz->lock. Concurrent
lvts_set_trips() calls for sensors sharing a controller therefore run
without mutual exclusion while updating lvts_ctrl->{low,high}_thresh,
performing a read-modify-write on LVTS_MONINT in lvts_update_irq_mask(),
and writing LVTS_OFFSETL and LVTS_OFFSETH.

Two concurrent callers can lose each other's LVTS_MONINT enable bits or
leave the programmed threshold registers inconsistent with the stored
controller thresholds.

Add a per-controller spinlock_t to struct lvts_ctrl and serialize
lvts_set_trips() with guard(spinlock)(&lvts_ctrl->lock).

Fixes: 2bba1acf7a4c ("thermal/drivers/mediatek/lvts_thermal: Manage threshold between sensors")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
No code changes since v1.
Found and drafted with an AI coding assistant.

 drivers/thermal/mediatek/lvts_thermal.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index 6d39025..aaadc00 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -4,6 +4,7 @@
  * Author: Balsam CHIHI <bchihi@baylibre.com>
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
 #include <linux/delay.h>
@@ -16,6 +17,7 @@
 #include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/reset.h>
+#include <linux/spinlock.h>
 #include <linux/thermal.h>
 #include <dt-bindings/thermal/mediatek,lvts-thermal.h>
 
@@ -181,6 +183,7 @@ struct lvts_ctrl {
 	void __iomem *base;
 	int low_thresh;
 	int high_thresh;
+	spinlock_t lock; /* protects thresholds and LVTS_MONINT */
 };
 
 struct lvts_domain {
@@ -451,6 +454,8 @@ static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
 	u32 raw_high = lvts_temp_to_raw(high, lvts_data);
 	bool should_update_thresh;
 
+	guard(spinlock)(&lvts_ctrl->lock);
+
 	lvts_sensor->low_thresh = low;
 	lvts_sensor->high_thresh = high;
 
@@ -987,6 +992,7 @@ static int lvts_ctrl_init(struct device *dev, struct lvts_domain *lvts_td,
 
 		lvts_ctrl[i].low_thresh = INT_MIN;
 		lvts_ctrl[i].high_thresh = INT_MIN;
+		spin_lock_init(&lvts_ctrl[i].lock);
 	}
 
 	/*
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 4/5] thermal/drivers/mediatek/lvts: recompute shared trips and mask first
  2026-12-03  2:19 [PATCH v2 0/5] thermal/drivers/mediatek/lvts: CALSCALE, overflow, trip, and IRQ fixes Ilia Snezhkov via B4 Relay
                   ` (2 preceding siblings ...)
  2026-12-03  2:19 ` [PATCH v2 3/5] thermal/drivers/mediatek/lvts: serialize lvts_set_trips() per ctrl Ilia Snezhkov via B4 Relay
@ 2026-12-03  2:20 ` Ilia Snezhkov via B4 Relay
  2026-12-03  2:20 ` [PATCH v2 5/5] thermal/drivers/mediatek/lvts: skip sensors without tz in IRQ Ilia Snezhkov via B4 Relay
  4 siblings, 0 replies; 6+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-12-03  2:20 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, bchihi, nfraprado,
	linux-pm, linux-mediatek, linux-arm-kernel, phone-devel,
	linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

Multi-sensor threshold management in lvts_set_trips() has four issues:

1. lvts_should_update_thresh() compares `high > lvts_ctrl->high_thresh`
   without excluding INT_MAX (no upper trip). When any sensor on a
   controller has no active upper trip, INT_MAX wins the comparison,
   overwrites lvts_ctrl->high_thresh with INT_MAX, passes INT_MAX to
   lvts_temp_to_raw(), and leaves sibling sensors with finite trips
   masked while high_offset_inten stays enabled for the INT_MAX sensor.

2. When the sensor currently holding lvts_ctrl->high_thresh cools down
   to a lower high threshold, lvts_should_update_thresh() sees no
   sensor matching the old threshold and installs the cooling sensor's
   threshold instead of scanning siblings for the next highest finite
   threshold, leaving hotter siblings masked.

3. lvts_update_irq_mask() requires both low_thresh and high_thresh to
   match simultaneously, masking the high-offset interrupt of sibling
   sensors that share the same high_thresh with a different low_thresh.

4. lvts_set_trips() enables bits in LVTS_MONINT via
   lvts_update_irq_mask() before writing LVTS_OFFSETL and LVTS_OFFSETH,
   exposing a window where interrupts are enabled against stale
   threshold registers.

Replace lvts_should_update_thresh() with lvts_recompute_thresh(), which
selects the highest finite high threshold across valid sensors (breaking
ties by highest low threshold so upward trips remain armed on cooler
siblings, and falling back to the highest finite low threshold only when
no sensor has a finite upper trip). Match low and high interrupt masks
independently, skip converting new_high == INT_MAX (leaving
LVTS_OFFSETH untouched with high_offset_inten masked), and mask all
offset interrupts before updating LVTS_OFFSETL and LVTS_OFFSETH.

Fixes: 2bba1acf7a4c ("thermal/drivers/mediatek/lvts_thermal: Manage threshold between sensors")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes in v2:
- New consolidated patch combining v1 patch 2/4's INT_MAX trip handling
  with lvts_recompute_thresh(), independent low/high IRQ mask matching,
  and masking offset IRQs before writing LVTS_OFFSETL/LVTS_OFFSETH.

Found and drafted with an AI coding assistant.

 drivers/thermal/mediatek/lvts_thermal.c | 200 +++++++++++++++---------
 1 file changed, 128 insertions(+), 72 deletions(-)

diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index aaadc00..7b4ede8 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -385,61 +385,108 @@ static int lvts_get_temp(struct thermal_zone_device *tz, int *temp)
 	return 0;
 }
 
+static const u32 lvts_high_offset_inten_masks[] = {
+	LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR0,
+	LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR1,
+	LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR2,
+	LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR3,
+};
+
+static const u32 lvts_low_offset_inten_masks[] = {
+	LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR0,
+	LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR1,
+	LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR2,
+	LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR3,
+};
+
+static void lvts_mask_offset_irqs(struct lvts_ctrl *lvts_ctrl)
+{
+	u32 value;
+	int i;
+
+	value = readl(LVTS_MONINT(lvts_ctrl->base));
+	lvts_for_each_valid_sensor(i, lvts_ctrl)
+		value &= ~(lvts_low_offset_inten_masks[i] |
+			   lvts_high_offset_inten_masks[i]);
+	writel(value, LVTS_MONINT(lvts_ctrl->base));
+}
+
 static void lvts_update_irq_mask(struct lvts_ctrl *lvts_ctrl)
 {
-	static const u32 high_offset_inten_masks[] = {
-		LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR0,
-		LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR1,
-		LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR2,
-		LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR3,
-	};
-	static const u32 low_offset_inten_masks[] = {
-		LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR0,
-		LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR1,
-		LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR2,
-		LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR3,
-	};
-	u32 value = 0;
+	bool low_valid = lvts_ctrl->low_thresh != -INT_MAX &&
+			 lvts_ctrl->low_thresh != INT_MIN;
+	bool high_valid = lvts_ctrl->high_thresh != INT_MAX &&
+			  lvts_ctrl->high_thresh != INT_MIN;
+	u32 value;
 	int i;
 
 	value = readl(LVTS_MONINT(lvts_ctrl->base));
 
 	lvts_for_each_valid_sensor(i, lvts_ctrl) {
-		if (lvts_ctrl->sensors[i].high_thresh == lvts_ctrl->high_thresh
-		    && lvts_ctrl->sensors[i].low_thresh == lvts_ctrl->low_thresh) {
-			/*
-			 * The minimum threshold needs to be configured in the
-			 * OFFSETL register to get working interrupts, but we
-			 * don't actually want to generate interrupts when
-			 * crossing it.
-			 */
-			if (lvts_ctrl->low_thresh == -INT_MAX) {
-				value &= ~low_offset_inten_masks[i];
-				value |= high_offset_inten_masks[i];
-			} else {
-				value |= low_offset_inten_masks[i] | high_offset_inten_masks[i];
-			}
-		} else {
-			value &= ~(low_offset_inten_masks[i] | high_offset_inten_masks[i]);
-		}
+		/*
+		 * The minimum threshold needs to be configured in the
+		 * OFFSETL register to get working interrupts, but we
+		 * don't actually want to generate interrupts when
+		 * crossing it.
+		 */
+		if (low_valid &&
+		    lvts_ctrl->sensors[i].low_thresh == lvts_ctrl->low_thresh)
+			value |= lvts_low_offset_inten_masks[i];
+		else
+			value &= ~lvts_low_offset_inten_masks[i];
+
+		if (high_valid &&
+		    lvts_ctrl->sensors[i].high_thresh == lvts_ctrl->high_thresh)
+			value |= lvts_high_offset_inten_masks[i];
+		else
+			value &= ~lvts_high_offset_inten_masks[i];
 	}
 
 	writel(value, LVTS_MONINT(lvts_ctrl->base));
 }
 
-static bool lvts_should_update_thresh(struct lvts_ctrl *lvts_ctrl, int high)
+static void lvts_recompute_thresh(struct lvts_ctrl *lvts_ctrl,
+				  int *low, int *high)
 {
+	bool has_high = false;
+	bool has_low = false;
+	int best_high = INT_MIN;
+	int best_high_low = -INT_MAX;
+	int best_low = -INT_MAX;
 	int i;
 
-	if (high > lvts_ctrl->high_thresh)
-		return true;
+	lvts_for_each_valid_sensor(i, lvts_ctrl) {
+		int s_low = lvts_ctrl->sensors[i].low_thresh;
+		int s_high = lvts_ctrl->sensors[i].high_thresh;
 
-	lvts_for_each_valid_sensor(i, lvts_ctrl)
-		if (lvts_ctrl->sensors[i].high_thresh == lvts_ctrl->high_thresh
-		    && lvts_ctrl->sensors[i].low_thresh == lvts_ctrl->low_thresh)
-			return false;
+		if (s_high == INT_MIN && s_low == INT_MIN)
+			continue;
+
+		if (s_high != INT_MAX && s_high != INT_MIN) {
+			if (!has_high || s_high > best_high ||
+			    (s_high == best_high && s_low > best_high_low)) {
+				has_high = true;
+				best_high = s_high;
+				best_high_low = s_low;
+			}
+		} else if (s_low != -INT_MAX && s_low != INT_MIN) {
+			if (!has_low || s_low > best_low) {
+				has_low = true;
+				best_low = s_low;
+			}
+		}
+	}
 
-	return true;
+	if (has_high) {
+		*low = best_high_low;
+		*high = best_high;
+	} else if (has_low) {
+		*low = best_low;
+		*high = INT_MAX;
+	} else {
+		*low = -INT_MAX;
+		*high = INT_MAX;
+	}
 }
 
 static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
@@ -449,51 +496,60 @@ static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
 						   sensors[lvts_sensor->id]);
 	const struct lvts_data *lvts_data = lvts_ctrl->lvts_data;
 	void __iomem *base = lvts_sensor->base;
-	u32 raw_low = lvts_temp_to_raw(low != -INT_MAX ? low : LVTS_MINIMUM_THRESHOLD,
-				       lvts_data);
-	u32 raw_high = lvts_temp_to_raw(high, lvts_data);
 	bool should_update_thresh;
+	int new_low, new_high;
 
 	guard(spinlock)(&lvts_ctrl->lock);
 
 	lvts_sensor->low_thresh = low;
 	lvts_sensor->high_thresh = high;
 
-	should_update_thresh = lvts_should_update_thresh(lvts_ctrl, high);
+	lvts_recompute_thresh(lvts_ctrl, &new_low, &new_high);
+	should_update_thresh = new_low != lvts_ctrl->low_thresh ||
+			       new_high != lvts_ctrl->high_thresh;
+
 	if (should_update_thresh) {
-		lvts_ctrl->high_thresh = high;
-		lvts_ctrl->low_thresh = low;
-	}
-	lvts_update_irq_mask(lvts_ctrl);
+		u32 raw_low = lvts_temp_to_raw(new_low != -INT_MAX ? new_low :
+					       LVTS_MINIMUM_THRESHOLD,
+					       lvts_data);
 
-	if (!should_update_thresh)
-		return 0;
+		lvts_mask_offset_irqs(lvts_ctrl);
 
-	/*
-	 * Low offset temperature threshold
-	 *
-	 * LVTS_OFFSETL
-	 *
-	 * Bits:
-	 *
-	 * 14-0 : Raw temperature for threshold
-	 */
-	pr_debug("%s: Setting low limit temperature interrupt: %d\n",
-		 thermal_zone_device_type(tz), low);
-	writel(raw_low, LVTS_OFFSETL(base));
+		lvts_ctrl->low_thresh = new_low;
+		lvts_ctrl->high_thresh = new_high;
 
-	/*
-	 * High offset temperature threshold
-	 *
-	 * LVTS_OFFSETH
-	 *
-	 * Bits:
-	 *
-	 * 14-0 : Raw temperature for threshold
-	 */
-	pr_debug("%s: Setting high limit temperature interrupt: %d\n",
-		 thermal_zone_device_type(tz), high);
-	writel(raw_high, LVTS_OFFSETH(base));
+		/*
+		 * Low offset temperature threshold
+		 *
+		 * LVTS_OFFSETL
+		 *
+		 * Bits:
+		 *
+		 * 14-0 : Raw temperature for threshold
+		 */
+		pr_debug("%s: Setting low limit temperature interrupt: %d\n",
+			 thermal_zone_device_type(tz), new_low);
+		writel(raw_low, LVTS_OFFSETL(base));
+
+		if (new_high != INT_MAX) {
+			u32 raw_high = lvts_temp_to_raw(new_high, lvts_data);
+
+			/*
+			 * High offset temperature threshold
+			 *
+			 * LVTS_OFFSETH
+			 *
+			 * Bits:
+			 *
+			 * 14-0 : Raw temperature for threshold
+			 */
+			pr_debug("%s: Setting high limit temperature interrupt: %d\n",
+				 thermal_zone_device_type(tz), new_high);
+			writel(raw_high, LVTS_OFFSETH(base));
+		}
+	}
+
+	lvts_update_irq_mask(lvts_ctrl);
 
 	return 0;
 }
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 5/5] thermal/drivers/mediatek/lvts: skip sensors without tz in IRQ
  2026-12-03  2:19 [PATCH v2 0/5] thermal/drivers/mediatek/lvts: CALSCALE, overflow, trip, and IRQ fixes Ilia Snezhkov via B4 Relay
                   ` (3 preceding siblings ...)
  2026-12-03  2:20 ` [PATCH v2 4/5] thermal/drivers/mediatek/lvts: recompute shared trips and mask first Ilia Snezhkov via B4 Relay
@ 2026-12-03  2:20 ` Ilia Snezhkov via B4 Relay
  4 siblings, 0 replies; 6+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-12-03  2:20 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, bchihi, nfraprado,
	linux-pm, linux-mediatek, linux-arm-kernel, phone-devel,
	linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

lvts_ctrl_start() skips sensors absent from valid_sensor_mask as well
as sensors whose thermal zone is not present in the device tree
(-ENODEV), leaving lvts_ctrl->sensors[i].tz as NULL.

lvts_ctrl_irq_handler(), however, loops over all four sensor slots and
passes lvts_ctrl->sensors[i].tz unconditionally to
thermal_zone_device_update(), which dereferences tz immediately if a
status bit for an unpopulated sensor slot is set in LVTS_MONINTSTS.

Check lvts_ctrl->sensors[i].tz before calling
thermal_zone_device_update() while still acknowledging the status bit
with W1C.

Fixes: f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
No code changes since v1.
Found and drafted with an AI coding assistant.

 drivers/thermal/mediatek/lvts_thermal.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index 7b4ede8..82707be 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -642,8 +642,9 @@ static irqreturn_t lvts_ctrl_irq_handler(struct lvts_ctrl *lvts_ctrl)
 		if (!(value & masks[i]))
 			continue;
 
-		thermal_zone_device_update(lvts_ctrl->sensors[i].tz,
-					   THERMAL_TRIP_VIOLATED);
+		if (lvts_ctrl->sensors[i].tz)
+			thermal_zone_device_update(lvts_ctrl->sensors[i].tz,
+						   THERMAL_TRIP_VIOLATED);
 		iret = IRQ_HANDLED;
 	}
 
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-03  2:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  2:19 [PATCH v2 0/5] thermal/drivers/mediatek/lvts: CALSCALE, overflow, trip, and IRQ fixes Ilia Snezhkov via B4 Relay
2026-12-03  2:19 ` [PATCH v2 1/5] thermal/drivers/mediatek/lvts: write LVTS_CALSCALE in configure Ilia Snezhkov via B4 Relay
2026-12-03  2:19 ` [PATCH v2 2/5] thermal/drivers/mediatek/lvts: fix 32-bit overflow in temp_to_raw Ilia Snezhkov via B4 Relay
2026-12-03  2:19 ` [PATCH v2 3/5] thermal/drivers/mediatek/lvts: serialize lvts_set_trips() per ctrl Ilia Snezhkov via B4 Relay
2026-12-03  2:20 ` [PATCH v2 4/5] thermal/drivers/mediatek/lvts: recompute shared trips and mask first Ilia Snezhkov via B4 Relay
2026-12-03  2:20 ` [PATCH v2 5/5] thermal/drivers/mediatek/lvts: skip sensors without tz in IRQ Ilia Snezhkov via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®