Skip to contentSkip to conversation

Add LVTS thermal architecture

PATCH Superseded [email protected] 4 participants 30 messages Aug 17, 2022 – Sep 21, 2022

Subsystems (3)

v9 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v9 cover Add LVTS thermal architecture

From: Balsam CHIHI <[email protected]>

Move thermal files related to MediaTek to the mediatek folder.
Introduce the new thermal architecture LVTS (Low Voltage Thermal Sensor) driver to report the highest temperature in the SoC and record the highest temperature sensor, each sensor as a thermal zone.
The LVTS body is divided into two parts, the LVTS controller and the LVTS device, the LVTS controller can connect up to 4 LVTS devices, and each LVTS device can connect up to 7 TSMCUs.

The architecture will be first used on mt8192 and mt8195.

Changelog:
Changes in v9 :
- Rebase on top of 6.0.0-rc1
- Fix coding style issues
- Fix commit titles and commit messages
- Update dt-bindings :
- Add "allOf:if:then:"
- Use mt8192 as example (instead of mt8195)
- Fix dt-binding errors
- Fix DTS errors

Changes in v8 :
- Fix coding style issues
- Rebase on top of next-20220803
- Add multi-instance support :
- Rewrite DT-binding and DTS :
- Add DT-binding and DTS for LVTS_v4 (MT8192 and MT8195)
- One LVTS node for each HW Domain (AP and MCU)
- One SW Instance for each HW Domain
- Add a Kconfig sub-menu entry for LVTS and LVTS_v4 SoCs
- Replace platform_get_resource by platform_get_mem_or_io to get Base Address
- Replace platform_get_resource by platform_get_irq to get Interrupt Number
- Add "lvts_" prefix to functions and structs

Changes in v7 :
- Fix coding style issues
- Rewrite dt bindings
- was not accurate
- Use mt8195 for example (instead of mt8192)
- Rename mt6873 to mt8192
- Remove clock name
- Rebased on top of to series:
- https://patchwork.kernel.org/project/linux-mediatek/list/?series=637849
- https://patchwork.kernel.org/project/linux-pm/list/?series=639386

Changes in v6 :
- Remove temperature aggregation (it will be added in another series)
- Update the way to read the temperature (read one sensor instead of all)
- Add support of mt8195

Changes in v5 :
- Use 'git mv' for the relocated file.

Changes in v4 :
- Rebase to kernel-v5.13-rc1
- Resend

Changes in v3 :
- change the expression in the lvts_temp_to_raw to dev_s64.

Changes in v2 :
- Rebase to kernel-5.11-rc1.
- sort headers
- remove initial value 0 of msr_raw in the lvts_temp_to_raw.
- disconstruct the api of lvts_read_tc_msr_raw.
- add the initial value max_temp = 0 and compare e.q. in the lvts_read_all_tc_temperature.
- add the return with an invalid number in the lvts_init.

Alexandre Bailon (2):
dt-bindings: thermal: Add dt-binding document for LVTS thermal
controllers
arm64: dts: mt8195: Add efuse node to mt8195

Balsam CHIHI (1):
arm64: dts: mt8192: Add thermal zones and thermal nodes

Michael Kao (3):
thermal: mediatek: Relocate driver to mediatek folder
thermal: mediatek: Add LVTS driver for mt8192 thermal zones
thermal: mediatek: Add LVTS driver settings for mt8195 thermal zones

Tinghan Shen (1):
arm64: dts: mt8195: Add thermal zones and thermal nodes

 .../thermal/mediatek,lvts-thermal.yaml        | 152 ++++
 arch/arm64/boot/dts/mediatek/mt8192.dtsi      | 111 +++
 arch/arm64/boot/dts/mediatek/mt8195.dtsi      | 123 +++
 drivers/thermal/Kconfig                       |  14 +-
 drivers/thermal/Makefile                      |   2 +-
 drivers/thermal/mediatek/Kconfig              |  43 +
 drivers/thermal/mediatek/Makefile             |   3 +
 .../auxadc_thermal.c}                         |   2 +-
 drivers/thermal/mediatek/lvts_thermal.c       | 861 ++++++++++++++++++
 drivers/thermal/mediatek/lvts_thermal.h       | 386 ++++++++
 drivers/thermal/mediatek/lvts_v4.c            | 479 ++++++++++
 11 files changed, 2164 insertions(+), 12 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml
 create mode 100644 drivers/thermal/mediatek/Kconfig
 create mode 100644 drivers/thermal/mediatek/Makefile
 rename drivers/thermal/{mtk_thermal.c => mediatek/auxadc_thermal.c} (99%)
 create mode 100644 drivers/thermal/mediatek/lvts_thermal.c
 create mode 100644 drivers/thermal/mediatek/lvts_thermal.h
 create mode 100644 drivers/thermal/mediatek/lvts_v4.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v9 1/7 thermal: mediatek: Relocate driver to mediatek folder

From: Michael Kao <[email protected]>

Add MediaTek proprietary folder to upstream more thermal zone and cooler drivers, relocate the original thermal controller driver to it, and rename it as "auxadc_thermal.c" to show its purpose more clearly.

---
 drivers/thermal/Kconfig                       | 14 ++++--------
 drivers/thermal/Makefile                      |  2 +-
 drivers/thermal/mediatek/Kconfig              | 22 +++++++++++++++++++
 drivers/thermal/mediatek/Makefile             |  1 +
 .../auxadc_thermal.c}                         |  2 +-
 5 files changed, 29 insertions(+), 12 deletions(-)
 create mode 100644 drivers/thermal/mediatek/Kconfig
 create mode 100644 drivers/thermal/mediatek/Makefile
 rename drivers/thermal/{mtk_thermal.c => mediatek/auxadc_thermal.c} (99%)
drivers/thermal/Kconfig +4 -10
index e052dae614eb..d35f63daca3b 100644
@@ -412,16 +412,10 @@ config DA9062_THERMAL
 	  zone.
 	  Compatible with the DA9062 and DA9061 PMICs.
 
-config MTK_THERMAL
-	tristate "Temperature sensor driver for mediatek SoCs"
-	depends on ARCH_MEDIATEK || COMPILE_TEST
-	depends on HAS_IOMEM
-	depends on NVMEM || NVMEM=n
-	depends on RESET_CONTROLLER
-	default y
-	help
-	  Enable this option if you want to have support for thermal management
-	  controller present in Mediatek SoCs
+menu "Mediatek thermal drivers"
+depends on ARCH_MEDIATEK || COMPILE_TEST
+source "drivers/thermal/mediatek/Kconfig"
+endmenu
 
 config AMLOGIC_THERMAL
 	tristate "Amlogic Thermal Support"
drivers/thermal/Makefile +1 -1
index def8e1a0399c..f7daeb8056c4 100644
@@ -55,7 +55,7 @@ obj-y				+= st/
 obj-$(CONFIG_QCOM_TSENS)	+= qcom/
 obj-y				+= tegra/
 obj-$(CONFIG_HISI_THERMAL)     += hisi_thermal.o
-obj-$(CONFIG_MTK_THERMAL)	+= mtk_thermal.o
+obj-y				+= mediatek/
 obj-$(CONFIG_GENERIC_ADC_THERMAL)	+= thermal-generic-adc.o
 obj-$(CONFIG_UNIPHIER_THERMAL)	+= uniphier_thermal.o
 obj-$(CONFIG_AMLOGIC_THERMAL)     += amlogic_thermal.o
drivers/thermal/mediatek/Kconfig new file +22 -0
new file mode 100644
index 000000000000..8ff32370b591
@@ -0,0 +1,22 @@
+config MTK_THERMAL
+	tristate "MediaTek thermal drivers"
+	depends on THERMAL_OF
+	help
+	  This is the option for MediaTek thermal software solutions.
+	  Please enable corresponding options to get temperature
+	  information from thermal sensors or turn on throttle
+	  mechaisms for thermal mitigation.
+
+if MTK_THERMAL
+
+config MTK_SOC_THERMAL
+	tristate "AUXADC temperature sensor driver for MediaTek SoCs"
+	depends on HAS_IOMEM
+	depends on NVMEM
+	depends on RESET_CONTROLLER
+	help
+	  Enable this option if you want to get SoC temperature
+	  information for MediaTek platforms. This driver configures
+	  thermal controllers to collect temperature via AUXADC interface.
+
+endif
drivers/thermal/mediatek/Makefile new file +1 -0
new file mode 100644
index 000000000000..bec325f06a46
@@ -0,0 +1 @@
+obj-$(CONFIG_MTK_SOC_THERMAL)		+= auxadc_thermal.o
drivers/thermal/mediatek/auxadc_thermal.c renamed from drivers/thermal/mtk_thermal.c +1 -1
similarity index 99%
rename from drivers/thermal/mtk_thermal.c
rename to drivers/thermal/mediatek/auxadc_thermal.c
index ede94eadddda..60924f8f98e9 100644
@@ -23,7 +23,7 @@
 #include <linux/reset.h>
 #include <linux/types.h>
 
-#include "thermal_hwmon.h"
+#include "../thermal_hwmon.h"
 
 /* AUXADC Registers */
 #define AUXADC_CON1_SET_V	0x008
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v9,1/7] thermal: mediatek: Relocate driver to mediatek folder

Il 17/08/22 10:07, [email protected] ha scritto:

Quoted 71 lines

From: Michael Kao <[email protected]>

Add MediaTek proprietary folder to upstream more thermal zone and cooler drivers, relocate the original thermal controller driver to it, and rename it as "auxadc_thermal.c" to show its purpose more clearly.

---
  drivers/thermal/Kconfig                       | 14 ++++--------
  drivers/thermal/Makefile                      |  2 +-
  drivers/thermal/mediatek/Kconfig              | 22 +++++++++++++++++++
  drivers/thermal/mediatek/Makefile             |  1 +
  .../auxadc_thermal.c}                         |  2 +-
  5 files changed, 29 insertions(+), 12 deletions(-)

create mode 100644 drivers/thermal/mediatek/Kconfig
create mode 100644 drivers/thermal/mediatek/Makefile
rename drivers/thermal/{mtk_thermal.c => mediatek/auxadc_thermal.c} (99%)

drivers/thermal/Kconfig +4 -10
index e052dae614eb..d35f63daca3b 100644
@@ -412,16 +412,10 @@ config DA9062_THERMAL
  	  zone.
  	  Compatible with the DA9062 and DA9061 PMICs.
 
-config MTK_THERMAL
-	tristate "Temperature sensor driver for mediatek SoCs"
-	depends on ARCH_MEDIATEK || COMPILE_TEST
-	depends on HAS_IOMEM
-	depends on NVMEM || NVMEM=n
-	depends on RESET_CONTROLLER
-	default y
-	help
-	  Enable this option if you want to have support for thermal management
-	  controller present in Mediatek SoCs
+menu "Mediatek thermal drivers"
+depends on ARCH_MEDIATEK || COMPILE_TEST
+source "drivers/thermal/mediatek/Kconfig"
+endmenu
 
  config AMLOGIC_THERMAL
  	tristate "Amlogic Thermal Support"
drivers/thermal/Makefile +1 -1
index def8e1a0399c..f7daeb8056c4 100644
@@ -55,7 +55,7 @@ obj-y				+= st/
  obj-$(CONFIG_QCOM_TSENS)	+= qcom/
  obj-y				+= tegra/
  obj-$(CONFIG_HISI_THERMAL)     += hisi_thermal.o
-obj-$(CONFIG_MTK_THERMAL)	+= mtk_thermal.o
+obj-y				+= mediatek/
  obj-$(CONFIG_GENERIC_ADC_THERMAL)	+= thermal-generic-adc.o
  obj-$(CONFIG_UNIPHIER_THERMAL)	+= uniphier_thermal.o
  obj-$(CONFIG_AMLOGIC_THERMAL)     += amlogic_thermal.o
drivers/thermal/mediatek/Kconfig new file +5 -0
new file mode 100644
index 000000000000..8ff32370b591
@@ -0,0 +1,22 @@
+config MTK_THERMAL
+	tristate "MediaTek thermal drivers"
+	depends on THERMAL_OF
+	help
+	  This is the option for MediaTek thermal software solutions.

drivers/thermal/mediatek/Kconfig line 15

Quoted 10 lines
drivers/thermal/mediatek/Kconfig new file +10 -0
+	  Please enable corresponding options to get temperature
+	  information from thermal sensors or turn on throttle
+	  mechaisms for thermal mitigation.
+
+if MTK_THERMAL
+
+config MTK_SOC_THERMAL
+	tristate "AUXADC temperature sensor driver for MediaTek SoCs"
+	depends on HAS_IOMEM
+	depends on NVMEM

That's not true. This driver doesn't depend on NVMEM, as in function mtk_thermal_get_calibration_data(), the calibration params are initialized with default values and if no "calibration-data" cell was found, it will return 0 and simply go on with probing... and this is because not all SoCs need calibration data from eFuses.
If you check carefully, the original entry says "NVMEM || NVMEM=n".

Please drop the NVMEM dependency from this config entirely.

drivers/thermal/mediatek/Kconfig line 16

Quoted 1 line
drivers/thermal/mediatek/Kconfig new file +1 -0
+	depends on RESET_CONTROLLER

While at it, can you please also drop the dependency for RESET_CONTROLLER? I know it was present on the original config entry, but that's wrong, as the driver doesn't strictly require resets, as it uses device_reset_optional().

Regards,
Angelo

#3 lore raw

Re: [PATCH v9,1/7] thermal: mediatek: Relocate driver to mediatek folder

Hi Angelo,

Nice to hear from you again.
Understood. The changes you asked for will be implemented in the v10 of this series.
It will be sent very soon, I'm still working on Daniel's review too.

Thank you for the review!

Best regards,
Balsam

Quoted 103 lines · AngeloGioacchino Del Regno

Il 17/08/22 10:07, [email protected] ha scritto:

Quoted 71 lines

From: Michael Kao <[email protected]>

Add MediaTek proprietary folder to upstream more thermal zone and cooler drivers, relocate the original thermal controller driver to it, and rename it as "auxadc_thermal.c" to show its purpose more clearly.

---
  drivers/thermal/Kconfig                       | 14 ++++--------
  drivers/thermal/Makefile                      |  2 +-
  drivers/thermal/mediatek/Kconfig              | 22 +++++++++++++++++++
  drivers/thermal/mediatek/Makefile             |  1 +
  .../auxadc_thermal.c}                         |  2 +-
  5 files changed, 29 insertions(+), 12 deletions(-)

create mode 100644 drivers/thermal/mediatek/Kconfig
create mode 100644 drivers/thermal/mediatek/Makefile
rename drivers/thermal/{mtk_thermal.c => mediatek/auxadc_thermal.c} (99%)

drivers/thermal/Kconfig +4 -10
index e052dae614eb..d35f63daca3b 100644
@@ -412,16 +412,10 @@ config DA9062_THERMAL
        zone.
        Compatible with the DA9062 and DA9061 PMICs.
 
-config MTK_THERMAL
-     tristate "Temperature sensor driver for mediatek SoCs"
-     depends on ARCH_MEDIATEK || COMPILE_TEST
-     depends on HAS_IOMEM
-     depends on NVMEM || NVMEM=n
-     depends on RESET_CONTROLLER
-     default y
-     help
-       Enable this option if you want to have support for thermal management
-       controller present in Mediatek SoCs
+menu "Mediatek thermal drivers"
+depends on ARCH_MEDIATEK || COMPILE_TEST
+source "drivers/thermal/mediatek/Kconfig"
+endmenu
 
  config AMLOGIC_THERMAL
      tristate "Amlogic Thermal Support"
drivers/thermal/Makefile +1 -1
index def8e1a0399c..f7daeb8056c4 100644
@@ -55,7 +55,7 @@ obj-y                               += st/
  obj-$(CONFIG_QCOM_TSENS)    += qcom/
  obj-y                               += tegra/
  obj-$(CONFIG_HISI_THERMAL)     += hisi_thermal.o
-obj-$(CONFIG_MTK_THERMAL)    += mtk_thermal.o
+obj-y                                += mediatek/
  obj-$(CONFIG_GENERIC_ADC_THERMAL)   += thermal-generic-adc.o
  obj-$(CONFIG_UNIPHIER_THERMAL)      += uniphier_thermal.o
  obj-$(CONFIG_AMLOGIC_THERMAL)     += amlogic_thermal.o
drivers/thermal/mediatek/Kconfig new file +5 -0
new file mode 100644
index 000000000000..8ff32370b591
@@ -0,0 +1,22 @@
+config MTK_THERMAL
+     tristate "MediaTek thermal drivers"
+     depends on THERMAL_OF
+     help
+       This is the option for MediaTek thermal software solutions.

drivers/thermal/mediatek/Kconfig line 15

Quoted 10 lines
drivers/thermal/mediatek/Kconfig new file +10 -0
+       Please enable corresponding options to get temperature
+       information from thermal sensors or turn on throttle
+       mechaisms for thermal mitigation.
+
+if MTK_THERMAL
+
+config MTK_SOC_THERMAL
+     tristate "AUXADC temperature sensor driver for MediaTek SoCs"
+     depends on HAS_IOMEM
+     depends on NVMEM

That's not true. This driver doesn't depend on NVMEM, as in function mtk_thermal_get_calibration_data(), the calibration params are initialized with default values and if no "calibration-data" cell was found, it will return 0 and simply go on with probing... and this is because not all SoCs need calibration data from eFuses.
If you check carefully, the original entry says "NVMEM || NVMEM=n".

Please drop the NVMEM dependency from this config entirely.

drivers/thermal/mediatek/Kconfig line 16

Quoted 1 line
drivers/thermal/mediatek/Kconfig new file +1 -0
+     depends on RESET_CONTROLLER

While at it, can you please also drop the dependency for RESET_CONTROLLER? I know it was present on the original config entry, but that's wrong, as the driver doesn't strictly require resets, as it uses device_reset_optional().

Regards,
Angelo

#4 lore raw

PATCH v9 2/7 dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

In reply to [email protected]

From: Alexandre Bailon <[email protected]>

Add dt-binding document for mt8192 and mt8195 LVTS thermal controllers.

---
 .../thermal/mediatek,lvts-thermal.yaml        | 152 ++++++++++++++++++
 1 file changed, 152 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +152 -0
new file mode 100644
index 000000000000..31d9e220513a
@@ -0,0 +1,152 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/thermal/mediatek,lvts-thermal.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek SoC LVTS thermal controller
+
+maintainers:
+  - Yu-Chia Chang <[email protected]>
+  - Ben Tseng <[email protected]>
+
+description: |
+  LVTS (Low Voltage Thermal Sensor).
+  The architecture will be first used on mt8192 and mt8195.
+
+properties:
+  compatible:
+    enum:
+      - mediatek,mt8192-lvts-ap
+      - mediatek,mt8192-lvts-mcu
+      - mediatek,mt8195-lvts-ap
+      - mediatek,mt8195-lvts-mcu
+
+  "#thermal-sensor-cells":
+    const: 1
+
+  reg:
+    maxItems: 1
+    description: LVTS instance registers.
+
+  interrupts:
+    maxItems: 1
+    description: LVTS instance interrupts.
+
+  clocks:
+    maxItems: 1
+    description: LVTS instance clock.
+
+  resets:
+    maxItems: 1
+    description: |
+      LVTS instance SW reset for HW AP/MCU domain to clean temporary data
+      on HW initialization/resume.
+
+  nvmem-cells:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse data for LVTS
+
+  nvmem-cell-names:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse cell names for LVTS
+
+allOf:
+  - $ref: thermal-sensor.yaml#
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - mediatek,mt8192-lvts-ap
+              - mediatek,mt8192-lvts-mcu
+    then:
+      properties:
+        nvmem-cells:
+          items:
+            - description: Calibration efuse data for LVTS
+
+        nvmem-cell-names:
+          items:
+            - const: lvts_calib_data1
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - mediatek,mt8195-lvts-ap
+              - mediatek,mt8195-lvts-mcu
+    then:
+      properties:
+        nvmem-cells:
+          items:
+            - description: Calibration efuse data 1 for LVTS
+            - description: Calibration efuse data 2 for LVTS
+
+        nvmem-cell-names:
+          items:
+            - const: lvts_calib_data1
+            - const: lvts_calib_data2
+
+required:
+  - compatible
+  - '#thermal-sensor-cells'
+  - reg
+  - interrupts
+  - clocks
+  - resets
+  - nvmem-cells
+  - nvmem-cell-names
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/mt8192-clk.h>
+    #include <dt-bindings/reset/mt8192-resets.h>
+
+    soc {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      lvts_ap: thermal-sensor@1100b000 {
+        compatible = "mediatek,mt8192-lvts-ap";
+        #thermal-sensor-cells = <1>;
+        reg = <0 0x1100b000 0 0x1000>;
+        interrupts = <GIC_SPI 169 IRQ_TYPE_LEVEL_HIGH 0>;
+        clocks = <&infracfg CLK_INFRA_THERM>;
+        resets = <&infracfg MT8192_INFRA_RST0_THERM_CTRL_SWRST>;
+        nvmem-cells = <&lvts_e_data1>;
+        nvmem-cell-names = "lvts_calib_data1";
+      };
+
+      lvts_mcu: thermal-sensor@11278000 {
+        compatible = "mediatek,mt8192-lvts-mcu";
+        #thermal-sensor-cells = <1>;
+        reg = <0 0x11278000 0 0x1000>;
+        interrupts = <GIC_SPI 170 IRQ_TYPE_LEVEL_HIGH 0>;
+        clocks = <&infracfg CLK_INFRA_THERM>;
+        resets = <&infracfg MT8192_INFRA_RST4_THERM_CTRL_MCU_SWRST>;
+        nvmem-cells = <&lvts_e_data1>;
+        nvmem-cell-names = "lvts_calib_data1";
+      };
+    };
+
+    thermal_zones: thermal-zones {
+      cpu0-thermal {
+        polling-delay = <0>;
+        polling-delay-passive = <0>;
+        thermal-sensors = <&lvts_mcu 0>;
+      };
+
+      vpu1-thermal {
+        polling-delay = <0>;
+        polling-delay-passive = <0>;
+        thermal-sensors = <&lvts_ap 0>;
+      };
+    };
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v9,2/7] dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

Quoted 3 lines · On 17/08/2022 11:07, [email protected] wrote:

From: Alexandre Bailon <[email protected]>

Add dt-binding document for mt8192 and mt8195 LVTS thermal controllers.

Rebase your patchset on decent kernel tree. You seem to use something a bit old.

Quoted 43 lines
---
 .../thermal/mediatek,lvts-thermal.yaml        | 152 ++++++++++++++++++
 1 file changed, 152 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +28 -0
new file mode 100644
index 000000000000..31d9e220513a
@@ -0,0 +1,152 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/thermal/mediatek,lvts-thermal.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek SoC LVTS thermal controller
+
+maintainers:
+  - Yu-Chia Chang <[email protected]>
+  - Ben Tseng <[email protected]>
+
+description: |
+  LVTS (Low Voltage Thermal Sensor).
+  The architecture will be first used on mt8192 and mt8195.
+
+properties:
+  compatible:
+    enum:
+      - mediatek,mt8192-lvts-ap
+      - mediatek,mt8192-lvts-mcu
+      - mediatek,mt8195-lvts-ap
+      - mediatek,mt8195-lvts-mcu
+
+  "#thermal-sensor-cells":
+    const: 1
+
+  reg:

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 38

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+    maxItems: 1
+    description: LVTS instance registers.
+
+  interrupts:
+    maxItems: 1
+    description: LVTS instance interrupts.
+
+  clocks:
+    maxItems: 1
+    description: LVTS instance clock.

Skip all these three descriptions. They are obvious.

Quoted 49 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +49 -0
+
+  resets:
+    maxItems: 1
+    description: |
+      LVTS instance SW reset for HW AP/MCU domain to clean temporary data
+      on HW initialization/resume.
+
+  nvmem-cells:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse data for LVTS
+
+  nvmem-cell-names:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse cell names for LVTS
+
+allOf:
+  - $ref: thermal-sensor.yaml#
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - mediatek,mt8192-lvts-ap
+              - mediatek,mt8192-lvts-mcu
+    then:
+      properties:
+        nvmem-cells:
+          items:
+            - description: Calibration efuse data for LVTS
+
+        nvmem-cell-names:
+          items:
+            - const: lvts_calib_data1
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - mediatek,mt8195-lvts-ap
+              - mediatek,mt8195-lvts-mcu
+    then:
+      properties:
+        nvmem-cells:
+          items:
+            - description: Calibration efuse data 1 for LVTS

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 97

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+            - description: Calibration efuse data 2 for LVTS
+
+        nvmem-cell-names:
+          items:
+            - const: lvts_calib_data1
+            - const: lvts_calib_data2
+
+required:
+  - compatible
+  - '#thermal-sensor-cells'

Use consistent quotes: either ' or "

Quoted 13 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +13 -0
+  - reg
+  - interrupts
+  - clocks
+  - resets
+  - nvmem-cells
+  - nvmem-cell-names
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/mt8192-clk.h>

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 120

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+    #include <dt-bindings/reset/mt8192-resets.h>
+
+    soc {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      lvts_ap: thermal-sensor@1100b000 {
+        compatible = "mediatek,mt8192-lvts-ap";
+        #thermal-sensor-cells = <1>;
+        reg = <0 0x1100b000 0 0x1000>;

Convention is: compatible, then reg, then the rest of properties

Quoted 8 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +8 -0
+        interrupts = <GIC_SPI 169 IRQ_TYPE_LEVEL_HIGH 0>;
+        clocks = <&infracfg CLK_INFRA_THERM>;
+        resets = <&infracfg MT8192_INFRA_RST0_THERM_CTRL_SWRST>;
+        nvmem-cells = <&lvts_e_data1>;
+        nvmem-cell-names = "lvts_calib_data1";
+      };
+
+      lvts_mcu: thermal-sensor@11278000 {

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 138

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+        compatible = "mediatek,mt8192-lvts-mcu";
+        #thermal-sensor-cells = <1>;
+        reg = <0 0x11278000 0 0x1000>;
+        interrupts = <GIC_SPI 170 IRQ_TYPE_LEVEL_HIGH 0>;
+        clocks = <&infracfg CLK_INFRA_THERM>;
+        resets = <&infracfg MT8192_INFRA_RST4_THERM_CTRL_MCU_SWRST>;
+        nvmem-cells = <&lvts_e_data1>;
+        nvmem-cell-names = "lvts_calib_data1";
+      };
+    };

This part is the same as previous, so just skip it or replace with an example which is different somehow.

Best regards,
Krzysztof

#6 lore raw

Re: [PATCH v9,2/7] dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

Hi Krzysztof,

Thank you for the reviews.
Would you please explain the meaning of "Rebase your patchset on decent kernel tree. You seem to use something a bit old"? It is rebased on top of linux-6.0.0-rc1.
Am I missing something?

Best regards,
Balsam

Quoted 177 lines · Krzysztof Kozlowski
Quoted 3 lines · On 17/08/2022 11:07, [email protected] wrote:

From: Alexandre Bailon <[email protected]>

Add dt-binding document for mt8192 and mt8195 LVTS thermal controllers.

Rebase your patchset on decent kernel tree. You seem to use something a bit old.

Quoted 43 lines
---
 .../thermal/mediatek,lvts-thermal.yaml        | 152 ++++++++++++++++++
 1 file changed, 152 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +28 -0
new file mode 100644
index 000000000000..31d9e220513a
@@ -0,0 +1,152 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/thermal/mediatek,lvts-thermal.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek SoC LVTS thermal controller
+
+maintainers:
+  - Yu-Chia Chang <[email protected]>
+  - Ben Tseng <[email protected]>
+
+description: |
+  LVTS (Low Voltage Thermal Sensor).
+  The architecture will be first used on mt8192 and mt8195.
+
+properties:
+  compatible:
+    enum:
+      - mediatek,mt8192-lvts-ap
+      - mediatek,mt8192-lvts-mcu
+      - mediatek,mt8195-lvts-ap
+      - mediatek,mt8195-lvts-mcu
+
+  "#thermal-sensor-cells":
+    const: 1
+
+  reg:

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 38

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+    maxItems: 1
+    description: LVTS instance registers.
+
+  interrupts:
+    maxItems: 1
+    description: LVTS instance interrupts.
+
+  clocks:
+    maxItems: 1
+    description: LVTS instance clock.

Skip all these three descriptions. They are obvious.

Quoted 49 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +49 -0
+
+  resets:
+    maxItems: 1
+    description: |
+      LVTS instance SW reset for HW AP/MCU domain to clean temporary data
+      on HW initialization/resume.
+
+  nvmem-cells:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse data for LVTS
+
+  nvmem-cell-names:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse cell names for LVTS
+
+allOf:
+  - $ref: thermal-sensor.yaml#
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - mediatek,mt8192-lvts-ap
+              - mediatek,mt8192-lvts-mcu
+    then:
+      properties:
+        nvmem-cells:
+          items:
+            - description: Calibration efuse data for LVTS
+
+        nvmem-cell-names:
+          items:
+            - const: lvts_calib_data1
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - mediatek,mt8195-lvts-ap
+              - mediatek,mt8195-lvts-mcu
+    then:
+      properties:
+        nvmem-cells:
+          items:
+            - description: Calibration efuse data 1 for LVTS

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 97

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+            - description: Calibration efuse data 2 for LVTS
+
+        nvmem-cell-names:
+          items:
+            - const: lvts_calib_data1
+            - const: lvts_calib_data2
+
+required:
+  - compatible
+  - '#thermal-sensor-cells'

Use consistent quotes: either ' or "

Quoted 13 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +13 -0
+  - reg
+  - interrupts
+  - clocks
+  - resets
+  - nvmem-cells
+  - nvmem-cell-names
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/mt8192-clk.h>

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 120

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+    #include <dt-bindings/reset/mt8192-resets.h>
+
+    soc {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      lvts_ap: thermal-sensor@1100b000 {
+        compatible = "mediatek,mt8192-lvts-ap";
+        #thermal-sensor-cells = <1>;
+        reg = <0 0x1100b000 0 0x1000>;

Convention is: compatible, then reg, then the rest of properties

Quoted 8 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +8 -0
+        interrupts = <GIC_SPI 169 IRQ_TYPE_LEVEL_HIGH 0>;
+        clocks = <&infracfg CLK_INFRA_THERM>;
+        resets = <&infracfg MT8192_INFRA_RST0_THERM_CTRL_SWRST>;
+        nvmem-cells = <&lvts_e_data1>;
+        nvmem-cell-names = "lvts_calib_data1";
+      };
+
+      lvts_mcu: thermal-sensor@11278000 {

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 138

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+        compatible = "mediatek,mt8192-lvts-mcu";
+        #thermal-sensor-cells = <1>;
+        reg = <0 0x11278000 0 0x1000>;
+        interrupts = <GIC_SPI 170 IRQ_TYPE_LEVEL_HIGH 0>;
+        clocks = <&infracfg CLK_INFRA_THERM>;
+        resets = <&infracfg MT8192_INFRA_RST4_THERM_CTRL_MCU_SWRST>;
+        nvmem-cells = <&lvts_e_data1>;
+        nvmem-cell-names = "lvts_calib_data1";
+      };
+    };

This part is the same as previous, so just skip it or replace with an example which is different somehow.

Best regards,
Krzysztof

#7 lore raw

Re: [PATCH v9,2/7] dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

Quoted 7 lines · Balsam CHIHI

Hi Krzysztof,

Thank you for the reviews.
Would you please explain the meaning of "Rebase your patchset on decent kernel tree. You seem to use something a bit old"? It is rebased on top of linux-6.0.0-rc1.
Am I missing something?

You Cc-ed me based on old maintainers entry, so either you did not use scripts/get_maintainer.pl or you used that tool on some old kernel If you used that tool on decent kernel, you would get different email address. That's why I asked.

Best regards,
Krzysztof

#8 lore raw

Re: [PATCH v9,2/7] dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

Quoted 10 lines · Krzysztof Kozlowski
Quoted 7 lines · Balsam CHIHI

Hi Krzysztof,

Thank you for the reviews.
Would you please explain the meaning of "Rebase your patchset on decent kernel tree. You seem to use something a bit old"? It is rebased on top of linux-6.0.0-rc1.
Am I missing something?

Quoted 4 lines

You Cc-ed me based on old maintainers entry, so either you did not use scripts/get_maintainer.pl or you used that tool on some old kernel If you used that tool on decent kernel, you would get different email address. That's why I asked.

Thank you for pointing this out.
And I will do the requested changes.

Quoted 3 lines

Best regards,
Krzysztof

#9 lore raw

Re: [PATCH v9,2/7] dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

In reply to [email protected]

Il 17/08/22 10:07, [email protected] ha scritto:

Quoted 18 lines

From: Alexandre Bailon <[email protected]>

Add dt-binding document for mt8192 and mt8195 LVTS thermal controllers.

---
  .../thermal/mediatek,lvts-thermal.yaml        | 152 ++++++++++++++++++
  1 file changed, 152 insertions(+)

create mode 100644 Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +0 -0
new file mode 100644
index 000000000000..31d9e220513a

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 7

Quoted 7 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +7 -0
@@ -0,0 +1,152 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/thermal/mediatek,lvts-thermal.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek SoC LVTS thermal controller

title: MediaTek SoC Low Voltage Thermal Sensor (LVTS)

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 13

Quoted 6 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +6 -0
+
+maintainers:
+  - Yu-Chia Chang <[email protected]>
+  - Ben Tseng <[email protected]>
+
+description: |

description:
LVTS is a thermal management architecture composed of three subsystems, a Sensing device - Thermal Sensing Micro Circuit Unit (TSMCU), a Convertor - Low Voltage Thermal Sensor convertor (LVTS), and a Digital controller (LVTS_CTRL).

Quoted 7 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +7 -0
+  LVTS (Low Voltage Thermal Sensor).
+  The architecture will be first used on mt8192 and mt8195.
+
+properties:
+  compatible:
+    enum:
+      - mediatek,mt8192-lvts-ap

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 30

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+      - mediatek,mt8192-lvts-mcu
+      - mediatek,mt8195-lvts-ap
+      - mediatek,mt8195-lvts-mcu
+
+  "#thermal-sensor-cells":
+    const: 1
+
+  reg:
+    maxItems: 1
+    description: LVTS instance registers.

This description looks obvious, as it doesn't really say anything "new"... I would rather drop it.

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 34

Quoted 4 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +4 -0
+
+  interrupts:
+    maxItems: 1
+    description: LVTS instance interrupts.

Same here

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 38

Quoted 4 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +4 -0
+
+  clocks:
+    maxItems: 1
+    description: LVTS instance clock.

and here.

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 44

Quoted 6 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +6 -0
+
+  resets:
+    maxItems: 1
+    description: |
+      LVTS instance SW reset for HW AP/MCU domain to clean temporary data
+      on HW initialization/resume.

What about something like...

resets:
items:
- description: LVTS reset for clearing temporary data on AP/MCU

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 49

Quoted 5 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +5 -0
+
+  nvmem-cells:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse data for LVTS

nvmem-cells:
minItems: 1
items:
- description: Calibration eFuse data for LVTS
- description: Additional eFuse data (?)

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 54

Quoted 5 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +5 -0
+
+  nvmem-cell-names:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse cell names for LVTS

Actually, maxItems is not really two, but it depends on how many eFuse arrays / nvmem cells we have for each SoC, so I was thinking...

...what about doing something like

nvmem-cell-names:

    minItems: 1
    items:
      pattern: 'lvts-calib-data[0-9]+$'

and then,
if:
properties:
compatible:
contains:
enum:
- mediatek,blahblah-something
then:

    properties:
      nvmem-cell-names:
        maxItems: 2 (or 3, 4, 5...)

P.S.: I haven't tried any binding check on the proposed lines.

Krzysztof, any opinions on that?

Regards,
Angelo

#10 lore raw

Re: [PATCH v9,2/7] dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

Hi Angelo,

I've got the following errors after implementing these changes :
[...]
nvmem-cells:
minItems: 1
description: Calibration eFuse data for LVTS

nvmem-cell-names:

    minItems: 1
    items:
      pattern: 'lvts-calib-data[0-9]+$'

"#thermal-sensor-cells":
const: 1

allOf:
- $ref: thermal-sensor.yaml#

- if:
properties:
compatible:
contains:
enum:
- mediatek,mt8192-lvts-ap
- mediatek,mt8192-lvts-mcu
then:
properties:
nvmem-cells:
maxItems: 1

nvmem-cell-names:
maxItems: 1

- if:
properties:
compatible:
contains:
enum:
- mediatek,mt8195-lvts-ap
- mediatek,mt8195-lvts-mcu
then:
properties:
nvmem-cells:
maxItems: 2

nvmem-cell-names:
maxItems: 2
[...]

$ make DT_CHECKER_FLAGS=-m dt_binding_check
DT_SCHEMA_FILES=Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml
  LINT    Documentation/devicetree/bindings
  CHKDT   Documentation/devicetree/bindings/processed-schema.json
/home/balsam/src/linux-mtk-lvts-newThermalOF/Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml:
properties:nvmem-cell-names:items: {'pattern':
'lvts-calib-data[0-9]+$'} is not of type 'array'
from schema $id: http://devicetree.org/meta-schemas/string-array.yaml#
  SCHEMA  Documentation/devicetree/bindings/processed-schema.json
/home/balsam/src/linux-mtk-lvts-newThermalOF/Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml:
ignoring, error in schema: properties: nvmem-cell-names: items
  DTEX    Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.example.dts
  DTC     Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.example.dtb
  CHECK   Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.example.dtb
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.example.dtb:0:0:
/example-0/soc/thermal-sensor@1100b000: failed to match any schema
with compatible: ['mediatek,mt8192-lvts-ap']

am I missing something?

Best regards,
Balsam

Quoted 141 lines · AngeloGioacchino Del Regno

Il 17/08/22 10:07, [email protected] ha scritto:

Quoted 18 lines

From: Alexandre Bailon <[email protected]>

Add dt-binding document for mt8192 and mt8195 LVTS thermal controllers.

---
  .../thermal/mediatek,lvts-thermal.yaml        | 152 ++++++++++++++++++
  1 file changed, 152 insertions(+)

create mode 100644 Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +0 -0
new file mode 100644
index 000000000000..31d9e220513a

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 7

Quoted 7 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +7 -0
@@ -0,0 +1,152 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/thermal/mediatek,lvts-thermal.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek SoC LVTS thermal controller

title: MediaTek SoC Low Voltage Thermal Sensor (LVTS)

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 13

Quoted 6 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +6 -0
+
+maintainers:
+  - Yu-Chia Chang <[email protected]>
+  - Ben Tseng <[email protected]>
+
+description: |

description:
LVTS is a thermal management architecture composed of three subsystems, a Sensing device - Thermal Sensing Micro Circuit Unit (TSMCU), a Convertor - Low Voltage Thermal Sensor convertor (LVTS), and a Digital controller (LVTS_CTRL).

Quoted 7 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +7 -0
+  LVTS (Low Voltage Thermal Sensor).
+  The architecture will be first used on mt8192 and mt8195.
+
+properties:
+  compatible:
+    enum:
+      - mediatek,mt8192-lvts-ap

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 30

Quoted 10 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +10 -0
+      - mediatek,mt8192-lvts-mcu
+      - mediatek,mt8195-lvts-ap
+      - mediatek,mt8195-lvts-mcu
+
+  "#thermal-sensor-cells":
+    const: 1
+
+  reg:
+    maxItems: 1
+    description: LVTS instance registers.

This description looks obvious, as it doesn't really say anything "new"... I would rather drop it.

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 34

Quoted 4 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +4 -0
+
+  interrupts:
+    maxItems: 1
+    description: LVTS instance interrupts.

Same here

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 38

Quoted 4 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +4 -0
+
+  clocks:
+    maxItems: 1
+    description: LVTS instance clock.

and here.

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 44

Quoted 6 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +6 -0
+
+  resets:
+    maxItems: 1
+    description: |
+      LVTS instance SW reset for HW AP/MCU domain to clean temporary data
+      on HW initialization/resume.

What about something like...

resets:
items:
- description: LVTS reset for clearing temporary data on AP/MCU

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 49

Quoted 5 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +5 -0
+
+  nvmem-cells:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse data for LVTS

nvmem-cells:
minItems: 1
items:
- description: Calibration eFuse data for LVTS
- description: Additional eFuse data (?)

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 54

Quoted 5 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +5 -0
+
+  nvmem-cell-names:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse cell names for LVTS

Actually, maxItems is not really two, but it depends on how many eFuse arrays / nvmem cells we have for each SoC, so I was thinking...

...what about doing something like

   nvmem-cell-names:
     minItems: 1
     items:
       pattern: 'lvts-calib-data[0-9]+$'

and then,
if:
properties:
compatible:
contains:
enum:
- mediatek,blahblah-something
then:

     properties:
       nvmem-cell-names:
         maxItems: 2 (or 3, 4, 5...)

P.S.: I haven't tried any binding check on the proposed lines.

Krzysztof, any opinions on that?

Regards,
Angelo

#11 lore raw

Re: [PATCH v9,2/7] dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

Quoted 68 lines · Balsam CHIHI

Hi Angelo,

I've got the following errors after implementing these changes :
[...]
nvmem-cells:
minItems: 1
description: Calibration eFuse data for LVTS

nvmem-cell-names:

    minItems: 1
    items:
      pattern: 'lvts-calib-data[0-9]+$'

"#thermal-sensor-cells":
const: 1

allOf:
- $ref: thermal-sensor.yaml#

- if:
properties:
compatible:
contains:
enum:
- mediatek,mt8192-lvts-ap
- mediatek,mt8192-lvts-mcu
then:
properties:
nvmem-cells:
maxItems: 1

nvmem-cell-names:
maxItems: 1

- if:
properties:
compatible:
contains:
enum:
- mediatek,mt8195-lvts-ap
- mediatek,mt8195-lvts-mcu
then:
properties:
nvmem-cells:
maxItems: 2

nvmem-cell-names:
maxItems: 2
[...]

$ make DT_CHECKER_FLAGS=-m dt_binding_check
DT_SCHEMA_FILES=Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml
  LINT    Documentation/devicetree/bindings
  CHKDT   Documentation/devicetree/bindings/processed-schema.json
/home/balsam/src/linux-mtk-lvts-newThermalOF/Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml:
properties:nvmem-cell-names:items: {'pattern':
'lvts-calib-data[0-9]+$'} is not of type 'array'
from schema $id: http://devicetree.org/meta-schemas/string-array.yaml#
  SCHEMA  Documentation/devicetree/bindings/processed-schema.json
/home/balsam/src/linux-mtk-lvts-newThermalOF/Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml:
ignoring, error in schema: properties: nvmem-cell-names: items
  DTEX    Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.example.dts
  DTC     Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.example.dtb
  CHECK   Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.example.dtb
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.example.dtb:0:0:
/example-0/soc/thermal-sensor@1100b000: failed to match any schema
with compatible: ['mediatek,mt8192-lvts-ap']
Quoted 1 line

am I missing something?

Maybe maxItems: 2 in top-level property?

Best regards,
Krzysztof

#12 lore raw

Re: [PATCH v9,2/7] dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

Hi Krzysztof, Angelo,

Thank you for the suggestions.

I tested this (below) and it worked without any problem.
"make DT_CHECKER_FLAGS=-m dt_binding_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml" "make dtbs_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml" Should I keep it like this and submit it in v10?

[...]
nvmem-cells:
minItems: 1
items:
- description: Calibration eFuse data 1 for LVTS
- description: Calibration eFuse data 2 for LVTS

nvmem-cell-names:
minItems: 1
items:
- const: lvts-calib-data-1
- const: lvts-calib-data-2

"#thermal-sensor-cells":
const: 1

allOf:
- $ref: thermal-sensor.yaml#

- if:
properties:
compatible:
contains:
enum:
- mediatek,mt8192-lvts-ap
- mediatek,mt8192-lvts-mcu
then:
properties:
nvmem-cells:
maxItems: 1

nvmem-cell-names:
maxItems: 1

- if:
properties:
compatible:
contains:
enum:
- mediatek,mt8195-lvts-ap
- mediatek,mt8195-lvts-mcu
then:
properties:
nvmem-cells:
maxItems: 2

nvmem-cell-names:
maxItems: 2
[...]

Best regards,
Balsam

#13 lore raw

Re: [PATCH v9,2/7] dt-bindings: thermal: Add dt-binding document for LVTS thermal controllers

In reply to AngeloGioacchino Del Regno
Quoted 25 lines · AngeloGioacchino Del Regno

Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml line 54

Quoted 6 lines
Documentation/devicetree/bindings/thermal/mediatek,lvts-thermal.yaml new file +5 -0
+
+  nvmem-cell-names:
+    minItems: 1
+    maxItems: 2
+    description: Calibration efuse cell names for LVTS

Actually, maxItems is not really two, but it depends on how many eFuse arrays / nvmem cells we have for each SoC, so I was thinking...

...what about doing something like

   nvmem-cell-names:
     minItems: 1
     items:
       pattern: 'lvts-calib-data[0-9]+$'

and then,
if:
properties:
compatible:
contains:
enum:
- mediatek,blahblah-something
then:

Quoted 6 lines
     properties:
       nvmem-cell-names:
         maxItems: 2 (or 3, 4, 5...)

P.S.: I haven't tried any binding check on the proposed lines.

Should work, but does not enforce the order, so I would rather propose something longer:

properties:
nvmem-cell-names:
items:
- lvts-calib-data0
- lvts-calib-data1
minItems: 1

and then in allOf:if:then set minItems:2 or maxItems:1

Best regards,
Krzysztof

#14 lore raw

PATCH v9 3/7 arm64: dts: mt8192: Add thermal zones and thermal nodes

In reply to [email protected]

From: Balsam CHIHI <[email protected]>

Add thermal zones and thermal nodes for the mt8192.

---
 arch/arm64/boot/dts/mediatek/mt8192.dtsi | 111 +++++++++++++++++++++++
 1 file changed, 111 insertions(+)
arch/arm64/boot/dts/mediatek/mt8192.dtsi +111 -0
index cbae5a5ee4a0..59ef4da06a70 100644
@@ -12,6 +12,7 @@
 #include <dt-bindings/pinctrl/mt8192-pinfunc.h>
 #include <dt-bindings/phy/phy.h>
 #include <dt-bindings/power/mt8192-power.h>
+#include <dt-bindings/reset/mt8192-resets.h>
 
 / {
 	compatible = "mediatek,mt8192";
@@ -599,6 +600,28 @@ spi0: spi@1100a000 {
 			status = "disabled";
 		};
 
+		lvts_ap: thermal-sensor@1100b000 {
+			compatible = "mediatek,mt8192-lvts-ap";
+			#thermal-sensor-cells = <1>;
+			reg = <0 0x1100b000 0 0x1000>;
+			interrupts = <GIC_SPI 169 IRQ_TYPE_LEVEL_HIGH 0>;
+			clocks = <&infracfg CLK_INFRA_THERM>;
+			resets = <&infracfg MT8192_INFRA_RST0_THERM_CTRL_SWRST>;
+			nvmem-cells = <&lvts_e_data1>;
+			nvmem-cell-names = "lvts_calib_data1";
+		};
+
+		lvts_mcu: thermal-sensor@11278000 {
+			compatible = "mediatek,mt8192-lvts-mcu";
+			#thermal-sensor-cells = <1>;
+			reg = <0 0x11278000 0 0x1000>;
+			interrupts = <GIC_SPI 170 IRQ_TYPE_LEVEL_HIGH 0>;
+			clocks = <&infracfg CLK_INFRA_THERM>;
+			resets = <&infracfg MT8192_INFRA_RST4_THERM_CTRL_MCU_SWRST>;
+			nvmem-cells = <&lvts_e_data1>;
+			nvmem-cell-names = "lvts_calib_data1";
+		};
+
 		spi1: spi@11010000 {
 			compatible = "mediatek,mt8192-spi",
 				     "mediatek,mt6765-spi";
@@ -1457,4 +1480,92 @@ larb2: larb@1f002000 {
 			power-domains = <&spm MT8192_POWER_DOMAIN_MDP>;
 		};
 	};
+
+	thermal_zones: thermal-zones {
+		cpu0-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 0>;
+		};
+		cpu1-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 1>;
+		};
+		cpu2-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 2>;
+		};
+		cpu3-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 3>;
+		};
+		cpu4-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 4>;
+		};
+		cpu5-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 5>;
+		};
+		cpu6-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 6>;
+		};
+		cpu7-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 7>;
+		};
+		vpu1-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 0>;
+		};
+		vpu2-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 1>;
+		};
+		gpu1-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 2>;
+		};
+		gpu2-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 3>;
+		};
+		infra-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 4>;
+		};
+		cam-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 5>;
+		};
+		md1-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 6>;
+		};
+		md2-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 7>;
+		};
+		md3-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 8>;
+		};
+	};
 };
Signature · 1 line
2.34.1
#15 lore raw

Re: [PATCH v9,3/7] arm64: dts: mt8192: Add thermal zones and thermal nodes

Il 17/08/22 10:07, [email protected] ha scritto:

Quoted 27 lines

From: Balsam CHIHI <[email protected]>

Add thermal zones and thermal nodes for the mt8192.

---
  arch/arm64/boot/dts/mediatek/mt8192.dtsi | 111 +++++++++++++++++++++++
  1 file changed, 111 insertions(+)
arch/arm64/boot/dts/mediatek/mt8192.dtsi +3 -0
index cbae5a5ee4a0..59ef4da06a70 100644
@@ -12,6 +12,7 @@
  #include <dt-bindings/pinctrl/mt8192-pinfunc.h>
  #include <dt-bindings/phy/phy.h>
  #include <dt-bindings/power/mt8192-power.h>
+#include <dt-bindings/reset/mt8192-resets.h>
 
  / {
  	compatible = "mediatek,mt8192";
@@ -599,6 +600,28 @@ spi0: spi@1100a000 {
  			status = "disabled";
  		};
 
+		lvts_ap: thermal-sensor@1100b000 {
+			compatible = "mediatek,mt8192-lvts-ap";

arch/arm64/boot/dts/mediatek/mt8192.dtsi spi0: spi@1100a000 { · line 614

Quoted 10 lines
arch/arm64/boot/dts/mediatek/mt8192.dtsi +10 -0
+			#thermal-sensor-cells = <1>;
+			reg = <0 0x1100b000 0 0x1000>;
+			interrupts = <GIC_SPI 169 IRQ_TYPE_LEVEL_HIGH 0>;
+			clocks = <&infracfg CLK_INFRA_THERM>;
+			resets = <&infracfg MT8192_INFRA_RST0_THERM_CTRL_SWRST>;
+			nvmem-cells = <&lvts_e_data1>;
+			nvmem-cell-names = "lvts_calib_data1";
+		};
+
+		lvts_mcu: thermal-sensor@11278000 {

Please keep the nodes ordered by reg start.

Regards,
Angelo

Quoted 13 lines
arch/arm64/boot/dts/mediatek/mt8192.dtsi +10 -0
+			compatible = "mediatek,mt8192-lvts-mcu";
+			#thermal-sensor-cells = <1>;
+			reg = <0 0x11278000 0 0x1000>;
+			interrupts = <GIC_SPI 170 IRQ_TYPE_LEVEL_HIGH 0>;
+			clocks = <&infracfg CLK_INFRA_THERM>;
+			resets = <&infracfg MT8192_INFRA_RST4_THERM_CTRL_MCU_SWRST>;
+			nvmem-cells = <&lvts_e_data1>;
+			nvmem-cell-names = "lvts_calib_data1";
+		};
+
  		spi1: spi@11010000 {
  			compatible = "mediatek,mt8192-spi",
  				     "mediatek,mt6765-spi";
#16 lore raw

PATCH v9 4/7 thermal: mediatek: Add LVTS driver for mt8192 thermal zones

In reply to [email protected]

From: Michael Kao <[email protected]>

Add LVTS v4 (Low Voltage Thermal Sensor) driver to report junction temperatures in MediaTek SoC mt8192 and register the maximum temperature of sensors and each sensor as a thermal zone.

---
 drivers/thermal/mediatek/Kconfig        |  21 +
 drivers/thermal/mediatek/Makefile       |   2 +
 drivers/thermal/mediatek/lvts_thermal.c | 861 ++++++++++++++++++++++++
 drivers/thermal/mediatek/lvts_thermal.h | 385 +++++++++++
 drivers/thermal/mediatek/lvts_v4.c      | 249 +++++++
 5 files changed, 1518 insertions(+)
 create mode 100644 drivers/thermal/mediatek/lvts_thermal.c
 create mode 100644 drivers/thermal/mediatek/lvts_thermal.h
 create mode 100644 drivers/thermal/mediatek/lvts_v4.c
drivers/thermal/mediatek/Kconfig +21 -0
index 8ff32370b591..02a1b3b42ce0 100644
@@ -19,4 +19,25 @@ config MTK_SOC_THERMAL
 	  information for MediaTek platforms. This driver configures
 	  thermal controllers to collect temperature via AUXADC interface.
 
+config MTK_LVTS_THERMAL
+	tristate "LVTS temperature sensor driver for MediaTek SoCs"
+	depends on HAS_IOMEM
+	depends on NVMEM
+	depends on RESET_CONTROLLER
+	help
+	  Enable this option if you want to get SoC temperature information
+	  for MediaTek platforms. This driver configures LVTS
+	  (Low Voltage Thermal Sensor) thermal controllers to collect
+	  temperatures via ASIF (Analog Serial Interface).
+
+if MTK_LVTS_THERMAL
+
+config MTK_LVTS_V4
+	tristate "LVTS v4 driver for MediaTek SoCs"
+	help
+	  Enable this option if you want to get temperature information
+	  for LVTS v4 SoCs.
+
+endif
+
 endif
drivers/thermal/mediatek/Makefile +2 -0
index bec325f06a46..e4e62d726776 100644
@@ -1 +1,3 @@
 obj-$(CONFIG_MTK_SOC_THERMAL)		+= auxadc_thermal.o
+obj-$(CONFIG_MTK_LVTS_THERMAL)		+= lvts_thermal.o
+obj-$(CONFIG_MTK_LVTS_V4)			+= lvts_v4.o
drivers/thermal/mediatek/lvts_thermal.c new file +861 -0

861 lines not on this page · raw message

drivers/thermal/mediatek/lvts_thermal.h new file +385 -0

385 lines not on this page · raw message

drivers/thermal/mediatek/lvts_v4.c new file +249 -0

249 lines not on this page · raw message

Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH v9,4/7] thermal: mediatek: Add LVTS driver for mt8192 thermal zones

Hi Balsam,

Quoted 5 lines · On 17/08/2022 10:07, [email protected] wrote:

From: Michael Kao <[email protected]>

Add LVTS v4 (Low Voltage Thermal Sensor) driver to report junction temperatures in MediaTek SoC mt8192 and register the maximum temperature of sensors and each sensor as a thermal zone.

Thanks for your work

First of all, the patch is way too big.

The organization of the data is hard to understand.

Could you give a description of the sensors, how they are organized ?

I can see the there are 'tc' and each have a group of sensing points? Is that correct? Do have the 'tc's a shared clock? etc ...

I have another email with the comments inline but without more insights on the hardware it is difficult to review accurately. This driver looks more complex than the other ones I've reviewed. At least that is what looks like with the different macros names found.

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#18 lore raw

Re: [PATCH v9,4/7] thermal: mediatek: Add LVTS driver for mt8192 thermal zones

Hello Daniel,

Thank you for the feedback.

I hope this information will explain better the LVTS Driver. I will add this like the following.
Is this ok for you?

[v9,7/7] thermal: mediatek: Add LVTS driver settings for mt8195 thermal zones One Clock and one Reset for Thermal.
Thermal have two domain : CPU related (mcu) and non-CPU related (ap). TC : Thermal Controller to control the thermal sensor's HW behavior. TS : Thermal Sensor for measuring temperature the HW module. thermal

├── ap
│ ├── tc_0
│ │ ├── TS4_0 - vpu1
│ │ └── TS4_1 - vpu2
│ ├── tc_1
│ │ ├── TS5_0 - gpu1
│ │ └── TS5_1 - gpu2
│ ├── tc_2
│ │ ├── TS6_0 - vdec
│ │ ├── TS6_1 - img
│ │ └── TS6_2 - infra
│ └── tc_3
│ ├── TS7_0 - cam1
│ └── TS7_1 - cam2
└── mcu
├── tc_0
│ ├── TS1_0 - cpu_big1
│ └── TS1_1 - cpu_big2
├── tc_1
│ ├── TS2_0 - cpu_big3
│ └── TS2_1 - cpu_big4
└── tc_2
├── TS3_0 - cpu_little1
├── TS3_1 - cpu_little2
├── TS3_2 - cpu_little3
└── TS3_3 - cpu_little4

[v9,4/7] thermal: mediatek: Add LVTS driver for mt8192 thermal zones One Clock and one Reset for Thermal.
Thermal have two domain : CPU related (mcu) and non-CPU related (ap). TC : Thermal Controller to control the thermal sensor's HW behavior. TS : Thermal Sensor for measuring temperature the HW module. thermal

├── ap
│ ├── tc_0
│ │ ├── TS4_0 - vpu1
│ │ └── TS4_1 - vpu2
│ ├── tc_1
│ │ ├── TS5_0 - gpu1
│ │ └── TS5_1 - gpu2
│ ├── tc_2
│ │ ├── TS6_0 - infra
│ │ └── TS6_1 - cam
│ └── tc_3
│ ├── TS7_0 - md2
│ └── TS7_1 - md3
| └── TS6_2 - md1
└── mcu
├── tc_0
│ ├── TS1_0 - cpu_big1
│ └── TS1_1 - cpu_big2
├── tc_1
│ ├── TS2_0 - cpu_big3
│ └── TS2_1 - cpu_big4
└── tc_2
├── TS3_0 - cpu_little1
├── TS3_1 - cpu_little2
├── TS3_2 - cpu_little3
└── TS3_3 - cpu_little4

Best regards,
Balsam.

Quoted 35 lines · Daniel Lezcano

Hi Balsam,

Quoted 5 lines · On 17/08/2022 10:07, [email protected] wrote:

From: Michael Kao <[email protected]>

Add LVTS v4 (Low Voltage Thermal Sensor) driver to report junction temperatures in MediaTek SoC mt8192 and register the maximum temperature of sensors and each sensor as a thermal zone.

Thanks for your work

First of all, the patch is way too big.

The organization of the data is hard to understand.

Could you give a description of the sensors, how they are organized ?

I can see the there are 'tc' and each have a group of sensing points? Is that correct? Do have the 'tc's a shared clock? etc ...

I have another email with the comments inline but without more insights on the hardware it is difficult to review accurately. This driver looks more complex than the other ones I've reviewed. At least that is what looks like with the different macros names found.

--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

#19 lore raw

Re: [PATCH v9,4/7] thermal: mediatek: Add LVTS driver for mt8192 thermal zones

In reply to [email protected]

Hi Balsam,

Quoted 5 lines · On 17/08/2022 10:07, [email protected] wrote:

From: Michael Kao <[email protected]>

Add LVTS v4 (Low Voltage Thermal Sensor) driver to report junction temperatures in MediaTek SoC mt8192 and register the maximum temperature of sensors and each sensor as a thermal zone.

As it is a new driver submission, a more detailed hardware description of the sensors may help to have a better understanding of the driver implementation

I think also the patch size is too big and the driver should be submitted with a reduced number of changes.

The header file should be simplified as much as possible. As well as the structures.

The driver initialization should be lvts_v4_init -> lvts_init, that means one function to init and one to exit. No other functions being called from lvts_v4 to lvts

Overall the driver is very complex and hard to review. I tried to review as much as possible but there is too much code. So I gave up the review after digging into too many details to be reworked.

See below

Quoted 28 lines
---
  drivers/thermal/mediatek/Kconfig        |  21 +
  drivers/thermal/mediatek/Makefile       |   2 +
  drivers/thermal/mediatek/lvts_thermal.c | 861 ++++++++++++++++++++++++
  drivers/thermal/mediatek/lvts_thermal.h | 385 +++++++++++
  drivers/thermal/mediatek/lvts_v4.c      | 249 +++++++
  5 files changed, 1518 insertions(+)

create mode 100644 drivers/thermal/mediatek/lvts_thermal.c create mode 100644 drivers/thermal/mediatek/lvts_thermal.h create mode 100644 drivers/thermal/mediatek/lvts_v4.c

drivers/thermal/mediatek/Kconfig +0 -0
index 8ff32370b591..02a1b3b42ce0 100644
@@ -19,4 +19,25 @@ config MTK_SOC_THERMAL
  	  information for MediaTek platforms. This driver configures
  	  thermal controllers to collect temperature via AUXADC interface.
 

drivers/thermal/mediatek/Kconfig config MTK_SOC_THERMAL · line 31

Quoted 10 lines
drivers/thermal/mediatek/Kconfig +10 -0
+config MTK_LVTS_THERMAL
+	tristate "LVTS temperature sensor driver for MediaTek SoCs"
+	depends on HAS_IOMEM
+	depends on NVMEM
+	depends on RESET_CONTROLLER
+	help
+	  Enable this option if you want to get SoC temperature information
+	  for MediaTek platforms. This driver configures LVTS
+	  (Low Voltage Thermal Sensor) thermal controllers to collect
+	  temperatures via ASIF (Analog Serial Interface).

lvts_thermal.c is some kindof library and should be compiled unconditionnaly if MTK_LVTS_V4

So the block above should disappear. The help moved below and the option 'if MTK_LVTS_THERMAL' removed.

When MTK_LVTS_V4 is set, then it selects MTK_LVTS_THERMAL. All the export symbols can be removed from lvts_thermal

Quoted 42 lines
drivers/thermal/mediatek/Kconfig +9 -0
+if MTK_LVTS_THERMAL > +
+config MTK_LVTS_V4
+	tristate "LVTS v4 driver for MediaTek SoCs"
+	help
+	  Enable this option if you want to get temperature information
+	  for LVTS v4 SoCs.
+
+endif
+
  endif
drivers/thermal/mediatek/Makefile +2 -0
index bec325f06a46..e4e62d726776 100644
@@ -1 +1,3 @@
  obj-$(CONFIG_MTK_SOC_THERMAL)		+= auxadc_thermal.o
+obj-$(CONFIG_MTK_LVTS_THERMAL)		+= lvts_thermal.o
+obj-$(CONFIG_MTK_LVTS_V4)			+= lvts_v4.o
drivers/thermal/mediatek/lvts_thermal.c new file +18 -0
new file mode 100644
index 000000000000..a1681b914c69
@@ -0,0 +1,861 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/bits.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/nvmem-consumer.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_irq.h>

drivers/thermal/mediatek/lvts_thermal.c line 28

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/thermal.h>
+#include "lvts_thermal.h"
+
+static int lvts_raw_to_temp(struct lvts_formula_coeff *co, unsigned int msr_raw)
+{
+	/* This function returns degree mC */

nit: extra line

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+	int temp;
+
+	temp = (co->a * ((unsigned long long)msr_raw)) >> 14;
+	temp = temp + co->golden_temp * 500 + co->b;
+
+	return temp;

drivers/thermal/mediatek/lvts_thermal.c line 45

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+}
+
+static unsigned int lvts_temp_to_raw(struct lvts_formula_coeff *co, int temp)
+{
+	unsigned int msr_raw;
+
+	msr_raw = div_s64((s64)((co->golden_temp * 500 + co->b - temp)) << 14, (-1 * co->a));
+
+	return msr_raw;
+}

Please explain and document the formulas raw <--> temp above

Quoted 13 lines
drivers/thermal/mediatek/lvts_thermal.c new file +13 -0
+
+static int soc_temp_lvts_read_temp(void *data, int *temperature)
+{
+	struct soc_temp_tz *lvts_tz = (struct soc_temp_tz *)data;
+	struct lvts_data *lvts_data = lvts_tz->lvts_data;
+	struct device *dev = lvts_data->dev;
+	unsigned int msr_raw;
+
+	msr_raw = readl(lvts_data->reg[lvts_tz->id]) & MRS_RAW_MASK;
+	if (msr_raw == 0) {
+		/* Prevents a false critical temperature trap */
+		*temperature = 0;
+		dev_dbg(dev, "LVTS not yet ready\n");

drivers/thermal/mediatek/lvts_thermal.c line 68

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+
+	} else
+		*temperature = lvts_raw_to_temp(&lvts_data->coeff, msr_raw);
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops soc_temp_lvts_ops = {
+	.get_temp = soc_temp_lvts_read_temp,
+};

[ ... ] ^^

drivers/thermal/mediatek/lvts_thermal.c line 72

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+static void lvts_write_device(struct lvts_data *lvts_data, unsigned int data, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);

Here every call is computing the base + addr_offset. But addr_offset is only used in the structure to compute this base address.

Pre-compute the base address for the slot and remove the GET_BASE_ADDR macro.

drivers/thermal/mediatek/lvts_thermal.c line 72

Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.c new file +2 -0
+	writel(DEVICE_WRITE(lvts_data) | data, LVTS_CONFIG_0 + base);
+	usleep_range(20, 30);

Why this delay is needed? There are a lot of calls to this function, these delays will sum.

IMO, a good understanding of why this is needed, will help to find out when to use it.

drivers/thermal/mediatek/lvts_thermal.c line 80

Quoted 5 lines
drivers/thermal/mediatek/lvts_thermal.c new file +5 -0
+}
+
+static unsigned int lvts_read_device(struct lvts_data *lvts_data, unsigned int reg_idx, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);

ditto

drivers/thermal/mediatek/lvts_thermal.c line 86

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+	struct device *dev = lvts_data->dev;
+	unsigned int data;
+	int ret;
+
+	writel(READ_DEVICE_REG(lvts_data, reg_idx), LVTS_CONFIG_0 + base);
+	usleep_range(20, 30);

ditto

drivers/thermal/mediatek/lvts_thermal.c line 89

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+	ret = readl_poll_timeout(LVTS_CONFIG_0 + base, data, !(data & DEVICE_ACCESS_STARTUS), 2, 200);
+	if (ret)
+		dev_err(dev, "LVTS_TC_%d DEVICE_ACCESS_START is not ready\n", tc_id);

Should it return?

Quoted 7 lines
drivers/thermal/mediatek/lvts_thermal.c new file +7 -0
+
+	data = readl(LVTSRDATA0_0 + base);
+
+	return data;
+}
+
+static const char * const lvts_error_table[] = {

drivers/thermal/mediatek/lvts_thermal.c line 106

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+	"Idle",
+	"Write transaction",
+	"Waiting for read after write",
+	"Disable continue fetching on device",
+	"Read transaction",
+	"Set device special register for voltage threshold",
+	"Set TSMCU number for fetch"
+};
+
+static void wait_all_tc_sensing_point_idle(struct lvts_data *lvts_data)

return an error

drivers/thermal/mediatek/lvts_thermal.c line 113

Quoted 7 lines
drivers/thermal/mediatek/lvts_thermal.c new file +7 -0
+{
+	void __iomem *base;
+	struct device *dev = lvts_data->dev;
+	unsigned int error_code, is_error;
+	int i, cnt, ret;
+
+	for (cnt = 0; cnt < 2; cnt++) {

Why 2 loops? It should be documented.

No litterals please.

drivers/thermal/mediatek/lvts_thermal.c line 119

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+		is_error = 0;
+
+		for (i = 0; i < lvts_data->num_tc; i++) {
+			base = GET_BASE_ADDR(lvts_data, i);
+			ret = readl_poll_timeout(LVTSMSRCTL1_0 + base, error_code,
+				!(error_code & ALL_TC_SENSING_POINT_STATUS), 2, 200);

readl_poll_timeout() can fail, return ret

Replace 2, 200 by macros

drivers/thermal/mediatek/lvts_thermal.c line 121

Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.c new file +2 -0
+			error_code = ((error_code & TC_SENSING_POINT_10) >> 8) +
+				((error_code & TC_SENSING_POINT_7) >> 6) + (error_code & TC_SENSING_POINT_0);

Please document de decoding of the error code and move it after the check of 'ret'

drivers/thermal/mediatek/lvts_thermal.c line 120

Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.c new file +2 -0
+			if (ret)
+				dev_err(dev, "LVTS_TC_%d Error Code : %s\n", i, lvts_error_table[error_code]);

The error is coming from the read_poll_timeout() no from error_code, so if error_code is positive, then return something:

something:
- "Write transaction", "Waiting for read after write", "Read
transaction" -> -EBUSY ?

- Other -> -EIO ?

I suggest to remove the lvts_error_table, just write a comment for the errors and write the number in the trace

drivers/thermal/mediatek/lvts_thermal.c line 126

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+
+			if (error_code != 0)
+				is_error = 1;

return the error

drivers/thermal/mediatek/lvts_thermal.c line 131

Quoted 5 lines
drivers/thermal/mediatek/lvts_thermal.c new file +5 -0
+		}
+
+		if (is_error == 0)
+			break;
+	}

return 0;

drivers/thermal/mediatek/lvts_thermal.c line 134

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+}
+
+static void lvts_reset(struct lvts_data *lvts_data)

error handling: static int ...

drivers/thermal/mediatek/lvts_thermal.c line 137

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+{
+	if (lvts_data->reset)
+		reset_control_assert(lvts_data->reset);

error handling

drivers/thermal/mediatek/lvts_thermal.c line 140

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+
+	if (lvts_data->reset)
+		reset_control_deassert(lvts_data->reset);

error handling

It is the same condition, both calls can go under the same block.

BTW, why assert + deassert ?

drivers/thermal/mediatek/lvts_thermal.c line 143

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+}
+
+static void device_identification(struct lvts_data *lvts_data)

error handling: static int ...

drivers/thermal/mediatek/lvts_thermal.c line 151

Quoted 8 lines
drivers/thermal/mediatek/lvts_thermal.c new file +8 -0
+{
+	void __iomem *base;
+	struct device *dev = lvts_data->dev;
+	unsigned int i, data;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		writel(ENABLE_LVTS_CTRL_CLK, LVTSCLKEN_0 + base);

Isn't it already done with the clk framework ?

drivers/thermal/mediatek/lvts_thermal.c line 149

Quoted 1 line
drivers/thermal/mediatek/lvts_thermal.c new file +1 -0
+		lvts_write_device(lvts_data, RESET_ALL_DEVICES(lvts_data), i);

Why a call to RESET_*ALL*_DEVICES in the loop? I'm confused, it is resetting all the devices but with a 'tc_id' passed as parameter

Is it possible to use the assert reset for that ?

drivers/thermal/mediatek/lvts_thermal.c line 153

Quoted 1 line
drivers/thermal/mediatek/lvts_thermal.c new file +1 -0
+		writel(READ_BACK_DEVICE_ID(lvts_data), LVTS_CONFIG_0 + base);

What means READ_BACK?

drivers/thermal/mediatek/lvts_thermal.c line 151

Quoted 1 line
drivers/thermal/mediatek/lvts_thermal.c new file +1 -0
+		usleep_range(20, 30);

Why is that needed ?

drivers/thermal/mediatek/lvts_thermal.c line 159

Quoted 5 lines
drivers/thermal/mediatek/lvts_thermal.c new file +5 -0
+		/* Check LVTS device ID */
+		data = (readl(LVTS_ID_0 + base) & DEVICE_REG_DATA);
+		if (data != (lvts_data->tc->dev_id + i))
+			dev_err(dev, "LVTS_TC_%d, Device ID should be 0x%x, but 0x%x\n",
+				i, (lvts_data->tc->dev_id + i), data);

I'm confused, what is the purpose of the device_identification() function if you already know the id? When this situation can happen?

Quoted 46 lines
drivers/thermal/mediatek/lvts_thermal.c new file +46 -0
+	}
+}
+
+static void disable_all_sensing_points(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		writel(DISABLE_SENSING_POINT, LVTSMONCTL0_0 + base);
+	}
+}
+
+static void enable_all_sensing_points(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	struct device *dev = lvts_data->dev;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	unsigned int i, num;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		num = tc[i].num_sensor;
+		if (num > ALL_SENSING_POINTS) {
+			dev_err(dev, "LVTS_TC_%d, illegal number of sensors: %d\n", i, tc[i].num_sensor);
+
+			continue;
+		}
+
+		if ((tc[i].ts_offset == 1) && (num == 1))
+			writel(LVTS_SINGLE_SENSE | (0x1 << tc[i].ts_offset), LVTSMONCTL0_0 + base);
+
+		else
+			writel(ENABLE_SENSING_POINT(num), LVTSMONCTL0_0 + base);
+	}
+}
+
+static void set_polling_speed(struct lvts_data *lvts_data, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+	struct device *dev = lvts_data->dev;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	unsigned int lvts_mon_ctl_1, lvts_mon_ctl_2;
+
+	lvts_mon_ctl_1 = ((tc[tc_id].tc_speed->group_interval_delay << 20) &

drivers/thermal/mediatek/lvts_thermal.c line 215

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+		GROUP_INTERVAL_DELAY_MASK) | (tc[tc_id].tc_speed->period_unit & PERIOD_UNIT_MASK);
+	lvts_mon_ctl_2 = ((tc[tc_id].tc_speed->filter_interval_delay << 16) &
+		FILTER_INTERVAL_DELAY_MASK) | (tc[tc_id].tc_speed->sensor_interval_delay &
+		SENSOR_INTERVAL_DELAY_MASK);
+	/*
+	 * Clock source of LVTS thermal controller is 26MHz.
+	 * Period unit is a base for all interval delays
+	 * All interval delays must multiply it to convert a setting to time.
+	 * Filter interval delay is a delay between two samples of the same sensor
+	 * Sensor interval delay is a delay between two samples of differnet sensors

s/differnet/different/

Quoted 28 lines
drivers/thermal/mediatek/lvts_thermal.c new file +28 -0
+	 * Group interval delay is a delay between different rounds.
+	 * For example:
+	 *     If Period unit = C, filter delay = 1, sensor delay = 2, group delay = 1,
+	 *     and two sensors, TS1 and TS2, are in a LVTS thermal controller
+	 *     and then
+	 *     Period unit = C * 1/26M * 256 = 12 * 38.46ns * 256 = 118.149us
+	 *     Filter interval delay = 1 * Period unit = 118.149us
+	 *     Sensor interval delay = 2 * Period unit = 236.298us
+	 *     Group interval delay = 1 * Period unit = 118.149us
+	 *
+	 *     TS1    TS1 ... TS1    TS2    TS2 ... TS2    TS1...
+	 *        <--> Filter interval delay
+	 *                       <--> Sensor interval delay
+	 *                                             <--> Group interval delay
+	 */
+	writel(lvts_mon_ctl_1, LVTSMONCTL1_0 + base);
+	writel(lvts_mon_ctl_2, LVTSMONCTL2_0 + base);
+	dev_dbg(dev, "lvts_tc_%d, LVTSMONCTL1_0= 0x%x, LVTSMONCTL2_0= 0x%x\n",
+		 tc_id, readl(LVTSMONCTL1_0 + base), readl(LVTSMONCTL2_0 + base));
+}
+
+static void set_hw_filter(struct lvts_data *lvts_data, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+	struct device *dev = lvts_data->dev;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	unsigned int option = tc[tc_id].hw_filter & 0x7;
+

drivers/thermal/mediatek/lvts_thermal.c line 253

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+	/*
+	 * hw filter
+	 * 000: Get one sample
+	 * 001: Get 2 samples and average them
+	 * 010: Get 4 samples, drop max and min, then average the rest of 2 samples
+	 * 011: Get 6 samples, drop max and min, then average the rest of 4 samples
+	 * 100: Get 10 samples, drop max and min, then average the rest of 8 samples
+	 * 101: Get 18 samples, drop max and min, then average the rest of 16 samples
+	 */
+	option = (option << 9) | (option << 6) | (option << 3) | option;

I'm missing to understand, can you explain this bit changes?

drivers/thermal/mediatek/lvts_thermal.c line 257

Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.c new file +4 -0
+	writel(option, LVTSMSRCTL0_0 + base);
+	dev_dbg(dev, "lvts_tc_%d, LVTSMSRCTL0_0= 0x%x\n", tc_id, readl(LVTSMSRCTL0_0 + base));
+}
+

This function deserves an explanation. Why a dominator index exists?

Quoted 22 lines
drivers/thermal/mediatek/lvts_thermal.c new file +22 -0
+static int get_dominator_index(struct lvts_data *lvts_data, int tc_id)
+{
+	struct device *dev = lvts_data->dev;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	int d_index;
+
+	if (tc[tc_id].dominator_sensing_point == ALL_SENSING_POINTS) {
+		d_index = ALL_SENSING_POINTS;
+
+	} else if ((tc[tc_id].dominator_sensing_point < tc[tc_id].num_sensor) ||
+		(tc[tc_id].ts_offset != 0)) {
+		d_index = tc[tc_id].dominator_sensing_point;
+
+	} else {
+		dev_err(dev, "LVTS_TC_%d: dominator sensing point = %d. \
+			It should be smaller than num_sensor %d\n",
+			tc_id, tc[tc_id].dominator_sensing_point, tc[tc_id].num_sensor);
+		dev_err(dev, "Using the sensing point 0 as the dominated sensor\n");
+		d_index = SENSING_POINT0;
+	}
+
+	return d_index;

drivers/thermal/mediatek/lvts_thermal.c line 289

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+}
+
+static void disable_hw_reboot_interrupt(struct lvts_data *lvts_data, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+	unsigned int temp;
+
+	/*
+	* LVTS thermal controller has two interrupts for thermal HW reboot.
+	* One is for AP SW and the other is for RGU.

May be reword "thermal controller" to something like "thermal domain" or whatever. It is confusing with the thermal controller contained in lvts_data.

drivers/thermal/mediatek/lvts_thermal.c line 295

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+	* The interrupt of AP SW can be turned off by a bit of a register,
+	* but the other for RGU cannot.
+	* To prevent rebooting device accidentally, we are going to add
+	* a huge offset 0x3FFF to LVTS and make it always report extremely low temperature.
+	* LVTS always adds the offset 0x3FFF to MSR_RAW.
+	* When MSR_RAW is larger, SW will convert lower temperature.

I'm not sure to fully understand the explanation and the goal.

Quoted 34 lines
drivers/thermal/mediatek/lvts_thermal.c new file +34 -0
+	*/ > +	temp = readl(LVTSPROTCTL_0 + base);
+	writel(temp | 0x3FFF, LVTSPROTCTL_0 + base);
+
+	/* Disable the interrupt of AP SW */
+	temp = readl(LVTSMONINT_0 + base);
+	writel(temp & ~(STAGE3_INT_EN), LVTSMONINT_0 + base);
+}
+
+static void enable_hw_reboot_interrupt(struct lvts_data *lvts_data, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+	unsigned int temp;
+
+	/* Enable the interrupt of AP SW */
+	temp = readl(LVTSMONINT_0 + base);
+	writel(temp | STAGE3_INT_EN, LVTSMONINT_0 + base);
+
+	/* Clear the offset */
+	temp = readl(LVTSPROTCTL_0 + base);
+	writel(temp & ~PROTOFFSET, LVTSPROTCTL_0 + base);
+}
+
+static void set_tc_hw_reboot_threshold(struct lvts_data *lvts_data, int trip_point, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+	struct device *dev = lvts_data->dev;
+	unsigned int msr_raw, temp, config, d_index;
+
+	d_index = get_dominator_index(lvts_data, tc_id);
+
+	dev_dbg(dev, "lvts_tc_%d: dominator sensing point = %d\n", tc_id, d_index);
+
+	disable_hw_reboot_interrupt(lvts_data, tc_id);
+	temp = readl(LVTSPROTCTL_0 + base);

drivers/thermal/mediatek/lvts_thermal.c line 340

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+	if (d_index == ALL_SENSING_POINTS) {
+		/* Maximum of 4 sensing points */
+		config = (0x1 << 16);
+		writel(config | temp, LVTSPROTCTL_0 + base);
+
+	} else {
+		/* Select protection sensor */
+		config = ((d_index << 2) + 0x2) << 16;
+		writel(config | temp, LVTSPROTCTL_0 + base);
+	}

So if ALL_SENSING_POINTS is set, the hardware is able to reboot on the first sensors reaching the hw reboot temp, otherwise it is the specified sensor which triggers the hw reboot? Do I read it correctly?

Why not use the ALL_SENSING_POINTS for all the cases?

Quoted 8 lines
drivers/thermal/mediatek/lvts_thermal.c new file +8 -0
+	msr_raw = lvts_temp_to_raw(&lvts_data->coeff, trip_point);
+	writel(msr_raw, LVTSPROTTC_0 + base);
+	enable_hw_reboot_interrupt(lvts_data, tc_id);
+}
+
+static void set_all_tc_hw_reboot(struct lvts_data *lvts_data)
+{
+	const struct lvts_tc_settings *tc = lvts_data->tc;

drivers/thermal/mediatek/lvts_thermal.c line 359

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+	int i, trip_point;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		trip_point = tc[i].hw_reboot_trip_point;
+
+		if (tc[i].num_sensor == 0)
+			continue;
+
+		if (trip_point == THERMAL_TEMP_INVALID)
+			continue;

This initialization happens nowhere AFAICT.

Quoted 23 lines
drivers/thermal/mediatek/lvts_thermal.c new file +23 -0
+		set_tc_hw_reboot_threshold(lvts_data, trip_point, i);
+	}
+}
+
+static int lvts_init(struct lvts_data *lvts_data)
+{
+	struct platform_ops *ops = &lvts_data->ops;
+	struct device *dev = lvts_data->dev;
+	int ret;
+
+	ret = clk_prepare_enable(lvts_data->clk);
+	if (ret) {
+		dev_err(dev, "Failed to enable lvts controller clock: %d\n", ret);
+
+		return ret;
+	}
+
+	lvts_reset(lvts_data);
+	device_identification(lvts_data);
+	if (ops->device_enable_and_init)
+		ops->device_enable_and_init(lvts_data);
+
+	if (HAS_FEATURE(lvts_data, FEATURE_DEVICE_AUTO_RCK) && (ops->device_enable_auto_rck))

drivers/thermal/mediatek/lvts_thermal.c line 393

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+		ops->device_enable_auto_rck(lvts_data);
+
+	else if (ops->device_read_count_rc_n)
+		ops->device_read_count_rc_n(lvts_data);
+
+	if (ops->set_cal_data)
+		ops->set_cal_data(lvts_data);
+
+	disable_all_sensing_points(lvts_data);
+	wait_all_tc_sensing_point_idle(lvts_data);

Check the return code from wait_all_tc_sensing_point_idle() and if it is -EBUSY retry, if it fails again, return ret.

drivers/thermal/mediatek/lvts_thermal.c line 398

Quoted 5 lines
drivers/thermal/mediatek/lvts_thermal.c new file +5 -0
+	if (ops->init_controller)
+		ops->init_controller(lvts_data);
+
+	enable_all_sensing_points(lvts_data);
+	set_all_tc_hw_reboot(lvts_data);

I'm not seeing so many error handling. Please check the errors, that will probably help removing all the dev_dbg around

Quoted 186 lines
drivers/thermal/mediatek/lvts_thermal.c new file +186 -0
+	return 0;
+}
+
+static int prepare_calibration_data(struct lvts_data *lvts_data)
+{
+	struct device *dev = lvts_data->dev;
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	struct platform_ops *ops = &lvts_data->ops;
+	int i;
+
+	cal_data->count_r = devm_kcalloc(dev, lvts_data->num_sensor,
+		sizeof(*cal_data->count_r), GFP_KERNEL);
+	if (!cal_data->count_r)
+		return -ENOMEM;
+
+	cal_data->count_rc = devm_kcalloc(dev, lvts_data->num_sensor,
+		sizeof(*cal_data->count_rc), GFP_KERNEL);
+	if (!cal_data->count_rc)
+		return -ENOMEM;
+
+	if (ops->efuse_to_cal_data && !cal_data->use_fake_efuse)
+		ops->efuse_to_cal_data(lvts_data);
+
+	if (cal_data->golden_temp == 0 || cal_data->golden_temp > GOLDEN_TEMP_MAX)
+		cal_data->use_fake_efuse = 1;
+
+	if (cal_data->use_fake_efuse) {
+		/* It means all efuse data are equal to 0 */
+		dev_err(dev, "This sample is not calibrated, fake !!\n");
+		cal_data->golden_temp = cal_data->default_golden_temp;
+		for (i = 0; i < lvts_data->num_sensor; i++) {
+			cal_data->count_r[i] = cal_data->default_count_r;
+			cal_data->count_rc[i] = cal_data->default_count_rc;
+		}
+	}
+
+	lvts_data->coeff.golden_temp = cal_data->golden_temp;
+	dev_dbg(dev, "golden_temp = %d\n", cal_data->golden_temp);
+
+	return 0;
+}
+
+static int get_calibration_data(struct lvts_data *lvts_data)
+{
+	struct device *dev = lvts_data->dev;
+	char cell_name[32];
+	struct nvmem_cell *cell;
+	u32 *buf;
+	size_t len;
+	int i, j, index = 0, ret;
+
+	lvts_data->efuse = devm_kcalloc(dev, lvts_data->num_efuse_addr,
+		sizeof(*lvts_data->efuse), GFP_KERNEL);
+	if (!lvts_data->efuse)
+		return -ENOMEM;
+
+	for (i = 0; i < lvts_data->num_efuse_block; i++) {
+		snprintf(cell_name, sizeof(cell_name), "lvts_calib_data%d", i + 1);
+		cell = nvmem_cell_get(dev, cell_name);
+		if (IS_ERR(cell)) {
+			dev_err(dev, "Failed to get nvmem cell %s\n", cell_name);
+
+			return PTR_ERR(cell);
+		}
+
+		buf = (u32 *)nvmem_cell_read(cell, &len);
+		nvmem_cell_put(cell);
+		if (IS_ERR(buf))
+			return PTR_ERR(buf);
+
+		for (j = 0; j < (len / sizeof(u32)); j++) {
+			if (index >= lvts_data->num_efuse_addr) {
+				dev_err(dev, "Array efuse is going to overflow");
+				kfree(buf);
+
+				return -EINVAL;
+			}
+
+			lvts_data->efuse[index] = buf[j];
+			index++;
+		}
+
+		kfree(buf);
+	}
+
+	ret = prepare_calibration_data(lvts_data);
+
+	return ret;
+}
+
+static int lvts_init_tc_regs(struct device *dev, struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	unsigned int i, j, s_index, x;
+
+	lvts_data->reg = devm_kcalloc(dev, lvts_data->num_sensor,
+		sizeof(*lvts_data->reg), GFP_KERNEL);
+	if (!lvts_data->reg)
+		return -ENOMEM;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		for (j = 0; j < tc[i].num_sensor; j++) {
+			s_index = tc[i].sensor_map[j];
+			x = j + tc[i].ts_offset;
+			lvts_data->reg[s_index] = LVTSMSR0_0 + base + 0x4 * x;
+		}
+	}
+
+	return 0;
+}
+
+static int of_update_lvts_data(struct lvts_data *lvts_data, struct platform_device *pdev)
+{
+	struct device *dev = lvts_data->dev;
+	struct resource *res;
+	int ret;
+
+	lvts_data->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(lvts_data->clk))
+		return PTR_ERR(lvts_data->clk);
+
+	/* Get base address */
+	res = platform_get_mem_or_io(pdev, 0);
+	if (!res) {
+		dev_err(dev, "No IO resource\n");
+
+		return -ENXIO;
+	}
+
+	lvts_data->base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(lvts_data->base)) {
+		dev_err(dev, "Failed to remap io\n");
+
+		return PTR_ERR(lvts_data->base);
+	}
+
+	/* Get interrupt number */
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0) {
+		dev_err(dev, "No irq resource\n");
+
+		return ret;
+	}
+
+	lvts_data->irq_num = ret;
+	/* Get reset control */
+	lvts_data->reset = devm_reset_control_get_by_index(dev, 0);
+	if (IS_ERR(lvts_data->reset)) {
+		dev_err(dev, "Failed to get reset control\n");
+
+		return PTR_ERR(lvts_data->reset);
+	}
+
+	ret = lvts_init_tc_regs(dev, lvts_data);
+	if (ret)
+		return ret;
+
+	ret = get_calibration_data(lvts_data);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static void lvts_device_close(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		lvts_write_device(lvts_data, RESET_ALL_DEVICES(lvts_data), i);
+		writel(DISABLE_LVTS_CTRL_CLK, LVTSCLKEN_0 + base);
+	}
+}
+
+static void lvts_close(struct lvts_data *lvts_data)
+{
+	disable_all_sensing_points(lvts_data);
+	wait_all_tc_sensing_point_idle(lvts_data);
+	lvts_device_close(lvts_data);
+	clk_disable_unprepare(lvts_data->clk);
+}
+

drivers/thermal/mediatek/lvts_thermal.c line 595

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+static void tc_irq_handler(struct lvts_data *lvts_data, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+	const struct device *dev = lvts_data->dev;
+	unsigned int ret = readl(LVTSMONINTSTS_0 + base);
+
+	/* Write back to clear interrupt status */
+	writel(ret, LVTSMONINTSTS_0 + base);
+	dev_dbg(dev, "LVTS thermal controller %d, LVTSMONINTSTS=0x%08x\n", tc_id, ret);
+	if (ret & THERMAL_PROTECTION_STAGE_3)

That means ?

drivers/thermal/mediatek/lvts_thermal.c line 596

Quoted 1 line
drivers/thermal/mediatek/lvts_thermal.c new file +1 -0
+		dev_dbg(dev, "Thermal protection stage 3 interrupt triggered\n");

When do we notify the thermal zone an interrupt happened because of a trip point?

drivers/thermal/mediatek/lvts_thermal.c line 609

Quoted 13 lines
drivers/thermal/mediatek/lvts_thermal.c new file +13 -0
+}
+
+static irqreturn_t irq_handler(int irq, void *dev_id)
+{
+	void __iomem *base;
+	struct lvts_data *lvts_data = (struct lvts_data *)dev_id;
+	struct device *dev = lvts_data->dev;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	unsigned int i, irq_bitmap;
+
+	base = lvts_data->base;
+	irq_bitmap = readl(THERMINTST + base);
+	dev_dbg(dev, "THERMINTST = 0x%x\n", irq_bitmap);

IIUC, there is one interrupt happening when a temperature threshold is crossed but we don't know which sensor it is. So we retrieve the bit mask of interrupt pending, right ? ...

drivers/thermal/mediatek/lvts_thermal.c line 604

Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.c new file +2 -0
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		if (tc[i].irq_bit == 0)

... but then what do we do with this bitmap ?

drivers/thermal/mediatek/lvts_thermal.c line 618

Quoted 7 lines
drivers/thermal/mediatek/lvts_thermal.c new file +7 -0
+			tc_irq_handler(lvts_data, i);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static int lvts_register_irq_handler(struct lvts_data *lvts_data)

You should use lvts_register_irq_handler(struct device *dev, int irq, void *data)

drivers/thermal/mediatek/lvts_thermal.c line 624

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+{
+	struct device *dev = lvts_data->dev;
+	int ret;
+
+	ret = devm_request_irq(dev, lvts_data->irq_num, irq_handler, IRQF_TRIGGER_NONE,
+		"mtk_lvts", lvts_data);

Change the name in order to prevent duplicate names, eg. dev_name(dev)

drivers/thermal/mediatek/lvts_thermal.c line 628

Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Failed to register LVTS IRQ, ret %d, irq_num %d\n",
+			ret, lvts_data->irq_num);
+		lvts_close(lvts_data);

lvts_close() should be called from the call site of lvts_register_irq_handler() if this one fails

Quoted 501 lines
drivers/thermal/mediatek/lvts_thermal.c new file +233 -0
+
+		return ret;
+	}
+
+	return 0;
+}
+
+static int lvts_register_thermal_zones(struct lvts_data *lvts_data)
+{
+	struct device *dev = lvts_data->dev;
+	struct thermal_zone_device *tzdev;
+	struct soc_temp_tz *lvts_tz;
+	int i, ret;
+
+	for (i = 0; i < lvts_data->num_sensor; i++) {
+		lvts_tz = devm_kzalloc(dev, sizeof(*lvts_tz), GFP_KERNEL);
+		if (!lvts_tz) {
+			lvts_close(lvts_data);
+
+			return -ENOMEM;
+		}
+
+		lvts_tz->id = i;
+		lvts_tz->lvts_data = lvts_data;
+		tzdev = devm_thermal_zone_of_sensor_register(dev, lvts_tz->id, lvts_tz,
+			&soc_temp_lvts_ops);
+		if (IS_ERR(tzdev)) {
+			if (lvts_tz->id != 0)
+				return 0;
+
+			ret = PTR_ERR(tzdev);
+			dev_err(dev, "Failed to register lvts tz %d, ret = %d\n", lvts_tz->id, ret);
+			lvts_close(lvts_data);
+
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+void lvts_device_enable_and_init(struct lvts_data *lvts_data)
+{
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		lvts_write_device(lvts_data, STOP_COUNTING_V4, i);
+		lvts_write_device(lvts_data, SET_RG_TSFM_LPDLY_V4, i);
+		lvts_write_device(lvts_data, SET_COUNTING_WINDOW_20US1_V4, i);
+		lvts_write_device(lvts_data, SET_COUNTING_WINDOW_20US2_V4, i);
+		lvts_write_device(lvts_data, TSV2F_CHOP_CKSEL_AND_TSV2F_EN_V4, i);
+		lvts_write_device(lvts_data, TSBG_DEM_CKSEL_X_TSBG_CHOP_EN_V4, i);
+		lvts_write_device(lvts_data, SET_TS_RSV_V4, i);
+		lvts_write_device(lvts_data, SET_TS_EN_V4, i);
+		lvts_write_device(lvts_data, TOGGLE_RG_TSV2F_VCO_RST1_V4, i);
+		lvts_write_device(lvts_data, TOGGLE_RG_TSV2F_VCO_RST2_V4, i);
+	}
+
+	lvts_data->counting_window_us = 20;
+}
+EXPORT_SYMBOL_GPL(lvts_device_enable_and_init);
+
+void lvts_device_enable_auto_rck_v4(struct lvts_data *lvts_data)
+{
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++)
+		lvts_write_device(lvts_data, SET_LVTS_AUTO_RCK_V4, i);
+}
+EXPORT_SYMBOL_GPL(lvts_device_enable_auto_rck_v4);
+
+int lvts_device_read_count_rc_n_v4(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	struct device *dev = lvts_data->dev;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	unsigned int s_index, data;
+	int ret, i, j;
+
+	cal_data->count_rc_now = devm_kcalloc(dev, lvts_data->num_sensor,
+		sizeof(*cal_data->count_rc_now), GFP_KERNEL);
+	if (!cal_data->count_rc_now)
+		return -ENOMEM;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		for (j = 0; j < tc[i].num_sensor; j++) {
+			s_index = tc[i].sensor_map[j];
+			lvts_write_device(lvts_data, SELECT_SENSOR_RCK_V4(j), i);
+			lvts_write_device(lvts_data, SET_DEVICE_SINGLE_MODE_V4, i);
+			lvts_write_device(lvts_data, KICK_OFF_RCK_COUNTING_V4, i);
+			ret = readl_poll_timeout(LVTS_CONFIG_0 + base, data,
+				!(data & DEVICE_SENSING_STATUS), 2, 200);
+			if (ret)
+				dev_err(dev, "LVTS_TC_%d DEVICE_SENSING_STATUS didn't ready\n", i);
+
+			data = lvts_read_device(lvts_data, 0x00, i);
+			cal_data->count_rc_now[s_index] = (data & COUNT_RC_NOW_MASK);
+		}
+
+		/* Recover Setting for Normal Access on
+		 * temperature fetch
+		 */
+		lvts_write_device(lvts_data, SET_SENSOR_NO_RCK_V4, i);
+		lvts_write_device(lvts_data, SET_DEVICE_LOW_POWER_SINGLE_MODE_V4, i);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(lvts_device_read_count_rc_n_v4);
+
+void lvts_set_calibration_data_v4(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	unsigned int i, j, s_index, x;
+	u32 lvts_calib_data;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		for (j = 0; j < tc[i].num_sensor; j++) {
+			s_index = tc[i].sensor_map[j];
+			x = j + tc[i].ts_offset;
+			if (HAS_FEATURE(lvts_data, FEATURE_DEVICE_AUTO_RCK))
+				lvts_calib_data = cal_data->count_r[s_index];
+
+			else
+				lvts_calib_data = (((u32)cal_data->count_rc_now[s_index]) *
+					cal_data->count_r[s_index]) >> 14;
+
+			writel(lvts_calib_data, LVTSEDATA00_0 + base + 0x4 * x);
+		}
+	}
+}
+EXPORT_SYMBOL_GPL(lvts_set_calibration_data_v4);
+
+void lvts_init_controller_v4(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	struct device *dev = lvts_data->dev;
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		lvts_write_device(lvts_data, SET_DEVICE_LOW_POWER_SINGLE_MODE_V4, i);
+		writel(SET_SENSOR_INDEX, LVTSTSSEL_0 + base);
+		writel(SET_CALC_SCALE_RULES, LVTSCALSCALE_0 + base);
+		set_polling_speed(lvts_data, i);
+		set_hw_filter(lvts_data, i);
+		dev_dbg(dev, "lvts_tc_%d: read all %d sensors in %d us, one in %d us\n",
+			i, GET_TC_SENSOR_NUM(lvts_data, i), GROUP_LATENCY_US(lvts_data, i),
+			SENSOR_LATENCY_US(lvts_data, i));
+	}
+}
+EXPORT_SYMBOL_GPL(lvts_init_controller_v4);
+
+int lvts_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct lvts_data *lvts_data;
+	int ret;
+
+	lvts_data = (struct lvts_data *)of_device_get_match_data(dev);
+	if (!lvts_data)	{
+		dev_err(dev, "Failed to get lvts platform data\n");
+
+		return -ENODATA;
+	}
+
+	lvts_data->dev = &pdev->dev;
+	ret = of_update_lvts_data(lvts_data, pdev);
+	if (ret)
+		return ret;
+
+	platform_set_drvdata(pdev, lvts_data);
+	ret = lvts_init(lvts_data);
+	if (ret)
+		return ret;
+
+	ret = lvts_register_irq_handler(lvts_data);
+	if (ret)
+		return ret;
+
+	ret = lvts_register_thermal_zones(lvts_data);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+int lvts_remove(struct platform_device *pdev)
+{
+	struct lvts_data *lvts_data;
+
+	lvts_data = (struct lvts_data *)platform_get_drvdata(pdev);
+	lvts_close(lvts_data);
+
+	return 0;
+}
+
+int lvts_suspend(struct platform_device *pdev, pm_message_t state)
+{
+	int ret;
+
+	ret = lvts_remove(pdev);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+int lvts_resume(struct platform_device *pdev)
+{
+	int ret;
+
+	ret = lvts_probe(pdev);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+void lvts_shutdown(struct platform_device *pdev)
+{
+	lvts_remove(pdev);
+}
+
+MODULE_AUTHOR("Yu-Chia Chang <[email protected]>");
+MODULE_AUTHOR("Michael Kao <[email protected]>");
+MODULE_DESCRIPTION("MediaTek LVTS Thermal Driver");
+MODULE_LICENSE("GPL");
drivers/thermal/mediatek/lvts_thermal.h new file +262 -0
new file mode 100644
index 000000000000..a94ce46acccd
@@ -0,0 +1,385 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#ifndef __MTK_SOC_TEMP_LVTS_H__
+#define __MTK_SOC_TEMP_LVTS_H__
+
+#define PERIOD_UNIT				12
+#define GROUP_INTERVAL_DELAY	1
+#define FILTER_INTERVAL_DELAY	1
+#define SENSOR_INTERVAL_DELAY	1
+
+#define HW_REBOOT_TRIP_POINT	117000
+
+#define FEATURE_DEVICE_AUTO_RCK	BIT(0)
+#define NUM_EFUSE_ADDR			22
+#define NUM_EFUSE_BLOCK_MT8192	1
+#define DEFAULT_GOLDEN_TEMP		50
+#define DEFAULT_CUONT_R			35000
+#define DEFAULT_CUONT_RC		2750
+#define COEFF_A					-250460
+#define COEFF_B					250460
+
+#define CLOCK_26MHZ_CYCLE_NS	38
+#define BUS_ACCESS_US			2
+#define GOLDEN_TEMP_MAX			62
+
+#define LVTS_FILTER_SAMPLES_1	1
+#define LVTS_FILTER_SAMPLES_2	2
+#define LVTS_FILTER_SAMPLES_4	4
+#define LVTS_FILTER_SAMPLES_6	6
+#define LVTS_FILTER_SAMPLES_10	10
+#define LVTS_FILTER_SAMPLES_18	18
+
+#define TC_SENSING_POINT_0			BIT(0)
+#define TC_SENSING_POINT_7			BIT(7)
+#define TC_SENSING_POINT_10			BIT(10)
+#define ALL_TC_SENSING_POINT_STATUS	(BIT(10) | BIT(7) | BIT(0))
+#define COUNT_RC_NOW_MASK			GENMASK(23, 0)
+#define PERIOD_UNIT_MASK			GENMASK(9, 0)
+#define GROUP_INTERVAL_DELAY_MASK	GENMASK(29, 20)
+#define FILTER_INTERVAL_DELAY_MASK	GENMASK(25, 16)
+#define SENSOR_INTERVAL_DELAY_MASK	GENMASK(9, 0)
+
+/* LVTS device register */
+#define RG_TSFM_DATA_0			0x00
+#define RG_TSFM_DATA_1			0x01
+#define RG_TSFM_DATA_2			0x02
+#define RG_TSFM_CTRL_0			0x03
+#define RG_TSFM_CTRL_1			0x04
+#define RG_TSFM_CTRL_2			0x05
+#define RG_TSFM_CTRL_3			0x06
+#define RG_TSFM_CTRL_4			0x07
+#define RG_TSV2F_CTRL_0			0x08
+#define RG_TSV2F_CTRL_1			0x09
+#define RG_TSV2F_CTRL_2			0x0A
+#define RG_TSV2F_CTRL_3			0x0B
+#define RG_TSV2F_CTRL_4			0x0C
+#define RG_TSV2F_CTRL_5			0x0D
+#define RG_TSV2F_CTRL_6			0x0E
+#define RG_TEMP_DATA_0			0x10
+#define RG_TEMP_DATA_1			0x11
+#define RG_TEMP_DATA_2			0x12
+#define RG_TEMP_DATA_3			0x13
+#define RG_RC_DATA_0			0x14
+#define RG_RC_DATA_1			0x15
+#define RG_RC_DATA_2			0x16
+#define RG_RC_DATA_3			0x17
+#define RG_DIV_DATA_0			0x18
+#define RG_DIV_DATA_1			0x19
+#define RG_DIV_DATA_2			0x1A
+#define RG_DIV_DATA_3			0x1B
+#define RG_TST_DATA_0			0x70
+#define RG_TST_DATA_1			0x71
+#define RG_TST_DATA_2			0x72
+#define RG_TST_CTRL				0x73
+#define RG_DBG_FQMTR			0xF0
+#define RG_DBG_LPSEQ			0xF1
+#define RG_DBG_STATE			0xF2
+#define RG_DBG_CHKSUM			0xF3
+#define RG_DID_LVTS				0xFC
+#define RG_DID_REV				0xFD
+#define RG_TSFM_RST				0xFF
+
+/* LVTS controller register */
+#define LVTSMONCTL0_0				0x000
+#define ENABLE_SENSING_POINT(num)	(LVTS_SINGLE_SENSE | GENMASK(((num) - 1), 0))
+#define DISABLE_SENSING_POINT		(LVTS_SINGLE_SENSE | 0x0)
+#define LVTSMONCTL1_0				0x004
+#define LVTSMONCTL2_0				0x008
+#define LVTSMONINT_0				0x00C
+#define STAGE3_INT_EN				BIT(31)
+#define LVTSMONINTSTS_0				0x010
+#define LVTSMONIDET0_0				0x014
+#define LVTSMONIDET1_0				0x018
+#define LVTSMONIDET2_0				0x01C
+#define LVTSMONIDET3_0				0x020
+#define LVTSH2NTHRE_0				0x024
+#define LVTSHTHRE_0					0x028
+#define LVTSCTHRE_0					0x02C
+#define LVTSOFFSETH_0				0x030
+#define LVTSOFFSETL_0				0x034
+#define LVTSMSRCTL0_0				0x038
+#define LVTSMSRCTL1_0				0x03C
+#define LVTSTSSEL_0					0x040
+#define SET_SENSOR_INDEX			0x13121110
+#define LVTSDEVICETO_0				0x044
+#define LVTSCALSCALE_0				0x048
+#define SET_CALC_SCALE_RULES		0x00000300
+#define LVTS_ID_0					0x04C
+#define LVTS_CONFIG_0				0x050
+
+#define SCK_ONLY					BIT(31)
+#define BROADCAST_ID_UPDATE			BIT(26)
+#define DEVICE_SENSING_STATUS		BIT(25)
+#define DEVICE_ACCESS_STARTUS		BIT(24)
+#define READ_32BIT_ACCESS			BIT(17)
+#define WRITE_ACCESS				BIT(16)
+#define LVTS_SINGLE_SENSE			BIT(9)
+#define FEATURE_CK26M_ACTIVE		BIT(1)
+#define DEVICE_REG_DATA				GENMASK(7, 0)
+
+#define LVTSEDATA00_0				0x054
+#define LVTSEDATA01_0				0x058
+#define LVTSEDATA02_0				0x05C
+#define LVTSEDATA03_0				0x060
+#define LVTSMSR0_0					0x090
+#define MRS_RAW_MASK				GENMASK(15, 0)
+#define MRS_RAW_VALID_BIT			BIT(16)
+#define LVTSMSR1_0					0x094
+#define LVTSMSR2_0					0x098
+#define LVTSMSR3_0					0x09C
+#define LVTSIMMD0_0					0x0A0
+#define LVTSIMMD1_0					0x0A4
+#define LVTSIMMD2_0					0x0A8
+#define LVTSIMMD3_0					0x0AC
+#define LVTSRDATA0_0				0x0B0
+#define LVTSRDATA1_0				0x0B4
+#define LVTSRDATA2_0				0x0B8
+#define LVTSRDATA3_0				0x0BC
+#define LVTSPROTCTL_0				0x0C0
+#define PROTOFFSET					GENMASK(15, 0)
+#define LVTSPROTTA_0				0x0C4
+#define LVTSPROTTB_0				0x0C8
+#define LVTSPROTTC_0				0x0CC
+#define LVTSCLKEN_0					0x0E4
+#define ENABLE_LVTS_CTRL_CLK		(1)
+#define DISABLE_LVTS_CTRL_CLK		(0)
+#define LVTSDBGSEL_0				0x0E8
+#define LVTSDBGSIG_0				0x0EC
+#define LVTSSPARE0_0				0x0F0
+#define LVTSSPARE1_0				0x0F4
+#define LVTSSPARE2_0				0x0F8
+#define LVTSSPARE3_0				0x0FC
+#define THERMINTST					0xF04
+
+/* LVTS register mask */
+#define THERMAL_COLD_INTERRUPT_0			BIT(0)
+#define THERMAL_HOT_INTERRUPT_0				BIT(1)
+#define THERMAL_LOW_OFFSET_INTERRUPT_0		BIT(2)
+#define THERMAL_HIGH_OFFSET_INTERRUPT_0		BIT(3)
+#define THERMAL_HOT2NORMAL_INTERRUPT_0		BIT(4)
+#define THERMAL_COLD_INTERRUPT_1			BIT(5)
+#define THERMAL_HOT_INTERRUPT_1				BIT(6)
+#define THERMAL_LOW_OFFSET_INTERRUPT_1		BIT(7)
+#define THERMAL_HIGH_OFFSET_INTERRUPT_1		BIT(8)
+#define THERMAL_HOT2NORMAL_INTERRUPT_1		BIT(9)
+#define THERMAL_COLD_INTERRUPT_2			BIT(10)
+#define THERMAL_HOT_INTERRUPT_2				BIT(11)
+#define THERMAL_LOW_OFFSET_INTERRUPT_2		BIT(12)
+#define THERMAL_HIGH_OFFSET_INTERRUPT_2		BIT(13)
+#define THERMAL_HOT2NORMAL_INTERRUPT_2		BIT(14)
+#define THERMAL_AHB_TIMEOUT_INTERRUPT		BIT(15)
+#define THERMAL_DEVICE_TIMEOUT_INTERRUPT	BIT(15)
+#define THERMAL_IMMEDIATE_INTERRUPT_0		BIT(16)
+#define THERMAL_IMMEDIATE_INTERRUPT_1		BIT(17)
+#define THERMAL_IMMEDIATE_INTERRUPT_2		BIT(18)
+#define THERMAL_FILTER_INTERRUPT_0			BIT(19)
+#define THERMAL_FILTER_INTERRUPT_1			BIT(20)
+#define THERMAL_FILTER_INTERRUPT_2			BIT(21)
+#define THERMAL_COLD_INTERRUPT_3			BIT(22)
+#define THERMAL_HOT_INTERRUPT_3				BIT(23)
+#define THERMAL_LOW_OFFSET_INTERRUPT_3		BIT(24)
+#define THERMAL_HIGH_OFFSET_INTERRUPT_3		BIT(25)
+#define THERMAL_HOT2NORMAL_INTERRUPT_3		BIT(26)
+#define THERMAL_IMMEDIATE_INTERRUPT_3		BIT(27)
+#define THERMAL_FILTER_INTERRUPT_3			BIT(28)
+#define THERMAL_PROTECTION_STAGE_1			BIT(29)
+#define THERMAL_PROTECTION_STAGE_2			BIT(30)
+#define THERMAL_PROTECTION_STAGE_3			BIT(31)
+
+#define CFG_REGISTER(reg, value)			(reg << 8 | value)
+#define STOP_COUNTING_V4					CFG_REGISTER(RG_TSFM_CTRL_0, 0x00)
+#define SET_RG_TSFM_LPDLY_V4				CFG_REGISTER(RG_TSFM_CTRL_4, 0xA6)
+#define SET_COUNTING_WINDOW_20US1_V4		CFG_REGISTER(RG_TSFM_CTRL_2, 0x00)
+#define SET_COUNTING_WINDOW_20US2_V4		CFG_REGISTER(RG_TSFM_CTRL_1, 0x20)
+#define TSV2F_CHOP_CKSEL_AND_TSV2F_EN_V4	CFG_REGISTER(RG_TSV2F_CTRL_2, 0x84)
+#define TSBG_DEM_CKSEL_X_TSBG_CHOP_EN_V4	CFG_REGISTER(RG_TSV2F_CTRL_4, 0x7C)
+#define SET_TS_RSV_V4						CFG_REGISTER(RG_TSV2F_CTRL_1, 0x8D)
+#define SET_TS_EN_V4						CFG_REGISTER(RG_TSV2F_CTRL_0, 0xF4)
+#define TOGGLE_RG_TSV2F_VCO_RST1_V4			CFG_REGISTER(RG_TSV2F_CTRL_0, 0xFC)
+#define TOGGLE_RG_TSV2F_VCO_RST2_V4			CFG_REGISTER(RG_TSV2F_CTRL_0, 0xF4)
+
+#define SET_LVTS_AUTO_RCK_V4				CFG_REGISTER(RG_TSV2F_CTRL_6, 0x01)
+#define SELECT_SENSOR_RCK_V4(id)			CFG_REGISTER(RG_TSV2F_CTRL_5, (id))
+#define SET_DEVICE_SINGLE_MODE_V4			CFG_REGISTER(RG_TSFM_CTRL_3, 0x78)
+#define KICK_OFF_RCK_COUNTING_V4			CFG_REGISTER(RG_TSFM_CTRL_0, 0x02)
+#define SET_SENSOR_NO_RCK_V4				CFG_REGISTER(RG_TSV2F_CTRL_5, 0x10)
+#define SET_DEVICE_LOW_POWER_SINGLE_MODE_V4	CFG_REGISTER(RG_TSFM_CTRL_3, 0xB8)
+
+#define HAS_FEATURE(lvts_data, feature)		(lvts_data->feature_bitmap & (feature))
+#define GET_BASE_ADDR(lvts_data, tc_id)		(lvts_data->base + lvts_data->tc[tc_id].addr_offset)
+#define GET_CAL_DATA_BITMASK(index, lvts_data, h, l)	(((index) < lvts_data->num_efuse_addr) ? \
+	((lvts_data->efuse[(index)] & GENMASK(h, l)) >> l) : 0)
+
+#define GET_TC_SENSOR_NUM(lvts_data, tc_id)	(lvts_data->tc[tc_id].num_sensor)
+#define ONE_SAMPLE(lvts_data)				(lvts_data->counting_window_us + 2 * BUS_ACCESS_US)
+#define NUM_OF_SAMPLE(lvts_data, tc_id)		((lvts_data->tc[tc_id].hw_filter < LVTS_FILTER_2) ? \
+	LVTS_FILTER_SAMPLES_1 : ((lvts_data->tc[tc_id].hw_filter > LVTS_FILTER_16_OF_18) ? \
+	LVTS_FILTER_SAMPLES_1 : ((lvts_data->tc[tc_id].hw_filter == LVTS_FILTER_16_OF_18) ? \
+	LVTS_FILTER_SAMPLES_18 : ((lvts_data->tc[tc_id].hw_filter == LVTS_FILTER_8_OF_10) ? \
+	LVTS_FILTER_SAMPLES_10 : (lvts_data->tc[tc_id].hw_filter * 2)))))
+
+#define PERIOD_UNIT_US(lvts_data, tc_id)   ((lvts_data->tc[tc_id].tc_speed->period_unit * 256 \
+	* CLOCK_26MHZ_CYCLE_NS) / 1000)
+#define FILTER_INT_US(lvts_data, tc_id)    (lvts_data->tc[tc_id].tc_speed->filter_interval_delay \
+	* PERIOD_UNIT_US(lvts_data, tc_id))
+#define SENSOR_INT_US(lvts_data, tc_id)    (lvts_data->tc[tc_id].tc_speed->sensor_interval_delay \
+	* PERIOD_UNIT_US(lvts_data, tc_id))
+#define GROUP_INT_US(lvts_data, tc_id)     (lvts_data->tc[tc_id].tc_speed->group_interval_delay \
+	* PERIOD_UNIT_US(lvts_data, tc_id))
+#define SENSOR_LATENCY_US(lvts_data, tc_id)	((NUM_OF_SAMPLE(lvts_data, tc_id) - 1) * \
+	FILTER_INT_US(lvts_data, tc_id) + NUM_OF_SAMPLE(lvts_data, tc_id) * ONE_SAMPLE(lvts_data))
+#define GROUP_LATENCY_US(lvts_data, tc_id)	(GET_TC_SENSOR_NUM(lvts_data, tc_id) * \
+	SENSOR_LATENCY_US(lvts_data, tc_id) + (GET_TC_SENSOR_NUM(lvts_data, tc_id) - 1) * \
+	SENSOR_INT_US(lvts_data, tc_id) + GROUP_INT_US(lvts_data, tc_id))
+
+#define CK26M_ACTIVE(lvts_data)				(((lvts_data->feature_bitmap & FEATURE_CK26M_ACTIVE) \
+	? 1 : 0) << 30)
+#define DEVICE_ACCESS					  (SCK_ONLY | DEVICE_ACCESS_STARTUS | READ_32BIT_ACCESS)
+#define DEVICE_READ(lvts_data)			  (CK26M_ACTIVE(lvts_data) | DEVICE_ACCESS)
+#define DEVICE_WRITE(lvts_data)			  (CK26M_ACTIVE(lvts_data) | DEVICE_ACCESS | WRITE_ACCESS)
+#define RESET_ALL_DEVICES(lvts_data)	  (DEVICE_WRITE(lvts_data) | RG_TSFM_RST << 8 | 0xFF)
+#define READ_DEVICE_REG(lvts_data, reg_id) (DEVICE_READ(lvts_data) | (reg_id) << 8 | 0x00)
+#define READ_BACK_DEVICE_ID(lvts_data)	   (CK26M_ACTIVE(lvts_data) | DEVICE_ACCESS | \
+	BROADCAST_ID_UPDATE | RG_DID_LVTS << 8)
+
+/*
+ * LVTS HW filter settings
+ * 000: Get one sample
+ * 001: Get 2 samples and average them
+ * 010: Get 4 samples, drop max and min, then average the rest of 2 samples
+ * 011: Get 6 samples, drop max and min, then average the rest of 4 samples
+ * 100: Get 10 samples, drop max and min, then average the rest of 8 samples
+ * 101: Get 18 samples, drop max and min, then average the rest of 16 samples
+ */
+enum lvts_hw_filter {
+	LVTS_FILTER_1,
+	LVTS_FILTER_2,
+	LVTS_FILTER_2_OF_4,
+	LVTS_FILTER_4_OF_6,

drivers/thermal/mediatek/lvts_thermal.h line 272

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.h new file +10 -0
+	LVTS_FILTER_8_OF_10,
+	LVTS_FILTER_16_OF_18
+};
+
+enum lvts_sensing_point {
+	SENSING_POINT0,
+	SENSING_POINT1,
+	SENSING_POINT2,
+	SENSING_POINT3,
+	ALL_SENSING_POINTS

Usually, the naming convention is NUM_SENSING_POINTS

drivers/thermal/mediatek/lvts_thermal.h line 275

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.h new file +3 -0
+};
+
+struct lvts_data;

This forward declaration should not be needed

Quoted 14 lines
drivers/thermal/mediatek/lvts_thermal.h new file +14 -0
+/**
+ * struct lvts_speed_settings - A structure to hold the data related to polling rate
+ * @period_unit: Period unit is a base for all interval delays
+ * @group_interval_delay:  Delay between different rounds
+ * @filter_interval_delay: Delay between two samples of the same sensor
+ * @sensor_interval_delay: Delay between two samples of differnet sensors
+ *
+ * Calculation is achieved with the following equations:
+ * For the period unit: (period_us * 1000) / (256 * clock_26mhz_cycle_ns)
+ * For the interval delays: delay / period_us
+ */
+struct lvts_speed_settings {
+	unsigned int period_unit;
+	unsigned int group_interval_delay;

drivers/thermal/mediatek/lvts_thermal.h line 300

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.h new file +10 -0
+	unsigned int filter_interval_delay;
+	unsigned int sensor_interval_delay;
+};
+
+struct lvts_tc_settings {
+	unsigned int dev_id;
+	unsigned int addr_offset;
+	unsigned int num_sensor;
+	unsigned int ts_offset;
+	unsigned int sensor_map[ALL_SENSING_POINTS];	/* In sensor ID */

Can you explain what is for the sensor_map?

drivers/thermal/mediatek/lvts_thermal.h line 311

Quoted 11 lines
drivers/thermal/mediatek/lvts_thermal.h new file +11 -0
+	struct lvts_speed_settings *tc_speed;
+	/*
+	 * HW filter setting
+	 * 000: Get one sample
+	 * 001: Get 2 samples and average them
+	 * 010: Get 4 samples, drop max and min, then average the rest of 2 samples
+	 * 011: Get 6 samples, drop max and min, then average the rest of 4 samples
+	 * 100: Get 10 samples, drop max and min, then average the rest of 8 samples
+	 * 101: Get 18 samples, drop max and min, then average the rest of 16 samples
+	 */
+	unsigned int hw_filter;

Isn't possible to specify a latency constraint instead of a hw_filter and deduce this one?

drivers/thermal/mediatek/lvts_thermal.h line 317

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.h new file +6 -0
+	/*
+	 * Dominator_sensing point is used to select a sensing point
+	 * and reference its temperature to trigger Thermal HW Reboot
+	 * When it is ALL_SENSING_POINTS, it will select all sensing points
+	 */
+	int dominator_sensing_point;

replace int by the enum, hopefully 'clang' can detect enum mismatches

drivers/thermal/mediatek/lvts_thermal.h line 318

Quoted 1 line
drivers/thermal/mediatek/lvts_thermal.h new file +1 -0
+	int hw_reboot_trip_point;		/* -274000: Disable HW reboot */

It is not necessary to specify the hw_reboot_trip_point, it is all the same in the declaration.

Quoted 20 lines
drivers/thermal/mediatek/lvts_thermal.h new file +20 -0
+	unsigned int irq_bit;
+};
+
+struct lvts_formula_coeff {
+	int a;
+	int b;
+	unsigned int golden_temp;
+};
+
+struct lvts_sensor_cal_data {
+	int use_fake_efuse;				/* 1: Use fake efuse, 0: Use real efuse */
+	unsigned int golden_temp;
+	unsigned int *count_r;
+	unsigned int *count_rc;
+	unsigned int *count_rc_now;
+	unsigned int default_golden_temp;
+	unsigned int default_count_r;
+	unsigned int default_count_rc;
+};
+

drivers/thermal/mediatek/lvts_thermal.h line 348

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.h new file +10 -0
+struct platform_ops {
+	void (*efuse_to_cal_data)(struct lvts_data *lvts_data);
+	void (*device_enable_and_init)(struct lvts_data *lvts_data);
+	void (*device_enable_auto_rck)(struct lvts_data *lvts_data);
+	int (*device_read_count_rc_n)(struct lvts_data *lvts_data);
+	void (*set_cal_data)(struct lvts_data *lvts_data);
+	void (*init_controller)(struct lvts_data *lvts_data);
+};
+
+struct lvts_data {

IMO it would make sense to rename the lvts_data to lvts_thermal_domain

drivers/thermal/mediatek/lvts_thermal.h line 352

Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.h new file +4 -0
+	struct device *dev;
+	struct clk *clk;
+	void __iomem *base;				/* LVTS base addresses */
+	unsigned int irq_num;			/* LVTS interrupt numbers */

The probe function can be reworked to get the irq and the pass it the register function. No need to store it in this structure.

drivers/thermal/mediatek/lvts_thermal.h line 364

Quoted 12 lines
drivers/thermal/mediatek/lvts_thermal.h new file +12 -0
+	struct reset_control *reset;
+	int num_tc;						/* Number of LVTS thermal controllers */
+	const struct lvts_tc_settings *tc;
+	int counting_window_us;			/* LVTS device counting window */
+	int num_sensor;					/* Number of sensors in this platform */
+	void __iomem **reg;
+	struct platform_ops ops;
+	int feature_bitmap;				/* Show what features are enabled */
+	unsigned int num_efuse_addr;
+	unsigned int *efuse;
+	unsigned int num_efuse_block;	/* Number of contiguous efuse indexes */
+	struct lvts_sensor_cal_data cal_data;

Everything related to the initialization must be somehow removed from this structure.

Quoted 276 lines
drivers/thermal/mediatek/lvts_thermal.h new file +21 -0
+	struct lvts_formula_coeff coeff;
+};
+
+struct soc_temp_tz {
+	unsigned int id;
+	struct lvts_data *lvts_data;
+};
+
+extern void lvts_device_enable_and_init(struct lvts_data *lvts_data);
+extern void lvts_device_enable_auto_rck_v4(struct lvts_data *lvts_data);
+extern int lvts_device_read_count_rc_n_v4(struct lvts_data *lvts_data);
+extern void lvts_set_calibration_data_v4(struct lvts_data *lvts_data);
+extern void lvts_init_controller_v4(struct lvts_data *lvts_data);
+
+extern int lvts_probe(struct platform_device *pdev);
+extern int lvts_remove(struct platform_device *pdev);
+extern int lvts_suspend(struct platform_device *pdev, pm_message_t state);
+extern int lvts_resume(struct platform_device *pdev);
+extern void lvts_shutdown(struct platform_device *pdev);
+
+#endif /* __MTK_SOC_TEMP_LVTS_H__ */
drivers/thermal/mediatek/lvts_v4.c new file +249 -0
new file mode 100644
index 000000000000..3dc00d2589f9
@@ -0,0 +1,249 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/of_irq.h>
+#include <linux/platform_device.h>
+#include "lvts_thermal.h"
+
+enum mt8192_lvts_mcu_sensor_enum {
+	MT8192_TS1_0,
+	MT8192_TS1_1,
+	MT8192_TS2_0,
+	MT8192_TS2_1,
+	MT8192_TS3_0,
+	MT8192_TS3_1,
+	MT8192_TS3_2,
+	MT8192_TS3_3,
+	MT8192_NUM_TS_MCU
+};
+
+enum mt8192_lvts_ap_sensor_enum {
+	MT8192_TS4_0,
+	MT8192_TS4_1,
+	MT8192_TS5_0,
+	MT8192_TS5_1,
+	MT8192_TS6_0,
+	MT8192_TS6_1,
+	MT8192_TS7_0,
+	MT8192_TS7_1,
+	MT8192_TS7_2,
+	MT8192_NUM_TS_AP
+};
+
+static void mt8192_mcu_efuse_to_cal_data(struct lvts_data *lvts_data)
+{
+	const unsigned int mt8192_ts[] = { MT8192_TS2_0, MT8192_TS3_0 };
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	unsigned int i, j;
+
+	cal_data->golden_temp = GET_CAL_DATA_BITMASK(0, lvts_data, 31, 24);
+
+	for (i = 0; i < MT8192_NUM_TS_MCU; i++)
+		cal_data->count_r[i] = GET_CAL_DATA_BITMASK(i + 1, lvts_data, 23, 0);
+
+	cal_data->count_rc[MT8192_TS1_0] = GET_CAL_DATA_BITMASK(21, lvts_data, 23, 0);
+
+	for (i = 0; i < (ARRAY_SIZE(mt8192_ts)); i++) {
+		for (j = 1; j <= 18; j++) {
+			cal_data->count_rc[mt8192_ts[i]] = (GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24)
+				<< 16) + (GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24) << 8) +
+				GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24);
+		}
+	}
+}
+
+static void mt8192_ap_efuse_to_cal_data(struct lvts_data *lvts_data)
+{
+	const unsigned int mt8192_ts[] = { MT8192_TS4_0, MT8192_TS5_0, MT8192_TS6_0, MT8192_TS7_0 };
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	unsigned int i, j;
+
+	cal_data->golden_temp = GET_CAL_DATA_BITMASK(0, lvts_data, 31, 24);
+
+	for (i = 0; i < MT8192_NUM_TS_AP; i++)
+		cal_data->count_r[i] = GET_CAL_DATA_BITMASK(i + 1, lvts_data, 23, 0);
+
+	for (i = 0; i < (ARRAY_SIZE(mt8192_ts)); i++) {
+		for (j = 1; j <= 18; j++) {
+			cal_data->count_rc[mt8192_ts[i]] = (GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24)
+				<< 16) + (GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24) << 8) +
+				GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24);
+		}
+	}
+}
+
+static struct lvts_speed_settings tc_speed_mt8192 = {
+	.period_unit = PERIOD_UNIT,
+	.group_interval_delay = GROUP_INTERVAL_DELAY,
+	.filter_interval_delay = FILTER_INTERVAL_DELAY,
+	.sensor_interval_delay = SENSOR_INTERVAL_DELAY,
+};
+
+static const struct lvts_tc_settings mt8192_tc_mcu_settings[] = {
+	[0] = {
+		.dev_id = 0x81,
+		.addr_offset = 0x0,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS1_0, MT8192_TS1_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT1,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(3),
+	},
+	[1] = {
+		.dev_id = 0x82,
+		.addr_offset = 0x100,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS2_0, MT8192_TS2_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT0,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(4),
+	},
+	[2] = {
+		.dev_id = 0x83,
+		.addr_offset = 0x200,
+		.num_sensor = 4,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS3_0, MT8192_TS3_1, MT8192_TS3_2, MT8192_TS3_3 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT0,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(5),
+	}
+};
+
+static const struct lvts_tc_settings mt8192_tc_ap_settings[] = {
+	[0] = {
+		.dev_id = 0x84,
+		.addr_offset = 0x0,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS4_0, MT8192_TS4_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT0,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(3),
+	},
+	[1] = {
+		.dev_id = 0x85,
+		.addr_offset = 0x100,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS5_0, MT8192_TS5_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT1,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(4),
+	},
+	[2] = {
+		.dev_id = 0x86,
+		.addr_offset = 0x200,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS6_0, MT8192_TS6_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT1,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(5),
+	},
+	[3] = {
+		.dev_id = 0x87,
+		.addr_offset = 0x300,
+		.num_sensor = 3,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS7_0, MT8192_TS7_1, MT8192_TS7_2 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT2,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(6),
+	}
+};
+
+static const struct lvts_data mt8192_lvts_mcu_data = {
+	.num_tc = (ARRAY_SIZE(mt8192_tc_mcu_settings)),
+	.tc = mt8192_tc_mcu_settings,
+	.num_sensor = MT8192_NUM_TS_MCU,
+	.ops = {
+		.efuse_to_cal_data = mt8192_mcu_efuse_to_cal_data,
+		.device_enable_and_init = lvts_device_enable_and_init,
+		.device_enable_auto_rck = lvts_device_enable_auto_rck_v4,
+		.device_read_count_rc_n = lvts_device_read_count_rc_n_v4,
+		.set_cal_data = lvts_set_calibration_data_v4,
+		.init_controller = lvts_init_controller_v4,
+	},
+	.feature_bitmap = FEATURE_DEVICE_AUTO_RCK,
+	.num_efuse_addr = NUM_EFUSE_ADDR,
+	.num_efuse_block = NUM_EFUSE_BLOCK_MT8192,
+	.cal_data = {
+		.default_golden_temp = DEFAULT_GOLDEN_TEMP,
+		.default_count_r = DEFAULT_CUONT_R,
+		.default_count_rc = DEFAULT_CUONT_RC,
+	},
+	.coeff = {
+		.a = COEFF_A,
+		.b = COEFF_B,
+	},
+};
+
+static const struct lvts_data mt8192_lvts_ap_data = {
+	.num_tc = (ARRAY_SIZE(mt8192_tc_ap_settings)),
+	.tc = mt8192_tc_ap_settings,
+	.num_sensor = MT8192_NUM_TS_AP,
+	.ops = {
+		.efuse_to_cal_data = mt8192_ap_efuse_to_cal_data,
+		.device_enable_and_init = lvts_device_enable_and_init,
+		.device_enable_auto_rck = lvts_device_enable_auto_rck_v4,
+		.device_read_count_rc_n = lvts_device_read_count_rc_n_v4,
+		.set_cal_data = lvts_set_calibration_data_v4,
+		.init_controller = lvts_init_controller_v4,
+	},
+	.feature_bitmap = FEATURE_DEVICE_AUTO_RCK,
+	.num_efuse_addr = NUM_EFUSE_ADDR,
+	.num_efuse_block = NUM_EFUSE_BLOCK_MT8192,
+	.cal_data = {
+		.default_golden_temp = DEFAULT_GOLDEN_TEMP,
+		.default_count_r = DEFAULT_CUONT_R,
+		.default_count_rc = DEFAULT_CUONT_RC,
+	},
+	.coeff = {
+		.a = COEFF_A,
+		.b = COEFF_B,
+	},
+};
+
+static const struct of_device_id lvts_of_match[] = {
+	{ .compatible = "mediatek,mt8192-lvts-mcu", .data = &mt8192_lvts_mcu_data, },
+	{ .compatible = "mediatek,mt8192-lvts-ap", .data = &mt8192_lvts_ap_data, },
+	{},
+};
+MODULE_DEVICE_TABLE(of, lvts_of_match);
+
+static struct platform_driver soc_temp_lvts = {
+	.probe = lvts_probe,
+	.remove = lvts_remove,
+	.suspend = lvts_suspend,
+	.resume = lvts_resume,
+	.shutdown = lvts_shutdown,
+	.driver = {
+		.name = "mtk-lvts-thermal-v4",
+		.of_match_table = lvts_of_match,
+	},
+};
+module_platform_driver(soc_temp_lvts);
+
+MODULE_AUTHOR("Yu-Chia Chang <[email protected]>");
+MODULE_AUTHOR("Michael Kao <[email protected]>");
+MODULE_DESCRIPTION("MediaTek LVTS V4 Thermal Driver");
+MODULE_LICENSE("GPL");
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#20 lore raw

Re: [PATCH v9,4/7] thermal: mediatek: Add LVTS driver for mt8192 thermal zones

Hi Daniel,

Thank you very much for your time to do this review.
You will find below answers to your questions. And some questions from my side too.
The comments that are not present in this email, are already implemented. Please let me know if you have any additional questions.

Quoted 11 lines
Quoted 3 lines

Add LVTS v4 (Low Voltage Thermal Sensor) driver to report junction temperatures in MediaTek SoC mt8192 and register the maximum temperature of sensors and each sensor as a thermal zone.

As it is a new driver submission, a more detailed hardware description of the sensors may help to have a better understanding of the driver implementation

I think also the patch size is too big and the driver should be submitted with a reduced number of changes.

Quoted 7 lines

The header file should be simplified as much as possible. As well as the structures.

The driver initialization should be lvts_v4_init -> lvts_init, that means one function to init and one to exit. No other functions being called from lvts_v4 to lvts

Would you please explain this point further?

Quoted 6 lines

Overall the driver is very complex and hard to review. I tried to review as much as possible but there is too much code. So I gave up the review after digging into too many details to be reworked.

See below

[...]

Quoted 10 lines
Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +4 -0
new file mode 100644
index 000000000000..a1681b914c69
@@ -0,0 +1,861 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */

[...]

Quoted 14 lines
Quoted 1 line

+static void lvts_reset(struct lvts_data *lvts_data)

error handling: static int ...

drivers/thermal/mediatek/lvts_thermal.c line 137

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+{
+     if (lvts_data->reset)
+             reset_control_assert(lvts_data->reset);

error handling

drivers/thermal/mediatek/lvts_thermal.c line 140

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+
+     if (lvts_data->reset)
+             reset_control_deassert(lvts_data->reset);
Quoted 7 lines

error handling

It is the same condition, both calls can go under the same block.

BTW, why assert + deassert ?

For more than one reset signal.

Quoted 6 lines

drivers/thermal/mediatek/lvts_thermal.c line 143

Quoted 3 lines
drivers/thermal/mediatek/lvts_thermal.c new file +3 -0
+}
+
+static void device_identification(struct lvts_data *lvts_data)

error handling: static int ...

Quoted 11 lines

drivers/thermal/mediatek/lvts_thermal.c line 151

Quoted 8 lines
drivers/thermal/mediatek/lvts_thermal.c new file +8 -0
+{
+     void __iomem *base;
+     struct device *dev = lvts_data->dev;
+     unsigned int i, data;
+
+     for (i = 0; i < lvts_data->num_tc; i++) {
+             base = GET_BASE_ADDR(lvts_data, i);
+             writel(ENABLE_LVTS_CTRL_CLK, LVTSCLKEN_0 + base);

Isn't it already done with the clk framework ?

No. this is the LVTS hardware internal clock :
The clocks of the Thermal Controllers.

Quoted 7 lines
Quoted 1 line

+ lvts_write_device(lvts_data, RESET_ALL_DEVICES(lvts_data), i);

Why a call to RESET_*ALL*_DEVICES in the loop? I'm confused, it is resetting all the devices but with a 'tc_id' passed as parameter

Is it possible to use the assert reset for that ?

“ALL” means all sensors (TS) in this Thermal Controller (TC).

Quoted 4 lines
Quoted 1 line

+ writel(READ_BACK_DEVICE_ID(lvts_data), LVTS_CONFIG_0 + base);

What means READ_BACK?

Read and update the sensor in the TC.

Quoted 4 lines
Quoted 1 line

+ usleep_range(20, 30);

Why is that needed ?

Wait for the hardware to be ready.
it has been reduced to the lowest possible value "usleep_range(2, 4)", but it can't be removed : lower than that the hardware malfunctions.

Quoted 9 lines

drivers/thermal/mediatek/lvts_thermal.c line 159

Quoted 5 lines
drivers/thermal/mediatek/lvts_thermal.c new file +5 -0
+             /* Check LVTS device ID */
+             data = (readl(LVTS_ID_0 + base) & DEVICE_REG_DATA);
+             if (data != (lvts_data->tc->dev_id + i))
+                     dev_err(dev, "LVTS_TC_%d, Device ID should be 0x%x, but 0x%x\n",
+                             i, (lvts_data->tc->dev_id + i), data);

I'm confused, what is the purpose of the device_identification() function if you already know the id? When this situation can happen?

It’s a hardware initialization flow to identify if the correct hardware module exists and is ready for initialization.

Quoted 2 lines
Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.c new file +2 -0
+     }
+}

[...]

Quoted 11 lines
Quoted 11 lines
drivers/thermal/mediatek/lvts_thermal.c new file +11 -0
+static void set_hw_filter(struct lvts_data *lvts_data, int tc_id)
+{
+     void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+     struct device *dev = lvts_data->dev;
+     const struct lvts_tc_settings *tc = lvts_data->tc;
+     unsigned int option = tc[tc_id].hw_filter & 0x7;
+
+     /*
+      * hw filter
+      * 000: Get one sample
+      * 001: Get 2 samples and average them
Quoted 9 lines

drivers/thermal/mediatek/lvts_thermal.c line 253

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+      * 010: Get 4 samples, drop max and min, then average the rest of 2 samples
+      * 011: Get 6 samples, drop max and min, then average the rest of 4 samples
+      * 100: Get 10 samples, drop max and min, then average the rest of 8 samples
+      * 101: Get 18 samples, drop max and min, then average the rest of 16 samples
+      */
+     option = (option << 9) | (option << 6) | (option << 3) | option;

I'm missing to understand, can you explain this bit changes?

The 3 bits are one set defined by hardware usage.

Quoted 7 lines

drivers/thermal/mediatek/lvts_thermal.c line 257

Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.c new file +4 -0
+     writel(option, LVTSMSRCTL0_0 + base);
+     dev_dbg(dev, "lvts_tc_%d, LVTSMSRCTL0_0= 0x%x\n", tc_id, readl(LVTSMSRCTL0_0 + base));
+}
+

This function deserves an explanation. Why a dominator index exists?

The dominator index is the Thermal Sensor that will be used as a sensing point to reboot the HW in critical temperature. This function sets a thermal sensor for that purpose.

[...]

Quoted 13 lines

drivers/thermal/mediatek/lvts_thermal.c line 289

Quoted 8 lines
drivers/thermal/mediatek/lvts_thermal.c new file +8 -0
+static void disable_hw_reboot_interrupt(struct lvts_data *lvts_data, int tc_id)
+{
+     void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+     unsigned int temp;
+
+     /*
+     * LVTS thermal controller has two interrupts for thermal HW reboot.
+     * One is for AP SW and the other is for RGU.

May be reword "thermal controller" to something like "thermal domain" or whatever. It is confusing with the thermal controller contained in lvts_data.

Quoted 9 lines

drivers/thermal/mediatek/lvts_thermal.c line 295

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+     * The interrupt of AP SW can be turned off by a bit of a register,
+     * but the other for RGU cannot.
+     * To prevent rebooting device accidentally, we are going to add
+     * a huge offset 0x3FFF to LVTS and make it always report extremely low temperature.
+     * LVTS always adds the offset 0x3FFF to MSR_RAW.
+     * When MSR_RAW is larger, SW will convert lower temperature.

I'm not sure to fully understand the explanation and the goal.

Hardware constraint.

Quoted 7 lines
Quoted 7 lines
drivers/thermal/mediatek/lvts_thermal.c new file +7 -0
+     */ > +  temp = readl(LVTSPROTCTL_0 + base);
+     writel(temp | 0x3FFF, LVTSPROTCTL_0 + base);
+
+     /* Disable the interrupt of AP SW */
+     temp = readl(LVTSMONINT_0 + base);
+     writel(temp & ~(STAGE3_INT_EN), LVTSMONINT_0 + base);
+}

[...]

Quoted 18 lines
Quoted 18 lines
drivers/thermal/mediatek/lvts_thermal.c new file +18 -0
+static void set_tc_hw_reboot_threshold(struct lvts_data *lvts_data, int trip_point, int tc_id)
+{
+     void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+     struct device *dev = lvts_data->dev;
+     unsigned int msr_raw, temp, config, d_index;
+
+     d_index = get_dominator_index(lvts_data, tc_id);
+
+     dev_dbg(dev, "lvts_tc_%d: dominator sensing point = %d\n", tc_id, d_index);
+
+     disable_hw_reboot_interrupt(lvts_data, tc_id);
+     temp = readl(LVTSPROTCTL_0 + base);
+     if (d_index == ALL_SENSING_POINTS) {
+             /* Maximum of 4 sensing points */
+             config = (0x1 << 16);
+             writel(config | temp, LVTSPROTCTL_0 + base);
+
+     } else {
Quoted 9 lines

drivers/thermal/mediatek/lvts_thermal.c line 340

Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.c new file +4 -0
+             /* Select protection sensor */
+             config = ((d_index << 2) + 0x2) << 16;
+             writel(config | temp, LVTSPROTCTL_0 + base);
+     }

So if ALL_SENSING_POINTS is set, the hardware is able to reboot on the first sensors reaching the hw reboot temp, otherwise it is the specified sensor which triggers the hw reboot? Do I read it correctly?

Yes. Will reboot on the first sensors reaching the hardware reboot temp. But tc need have 4 sensors.

Quoted 2 lines

Why not use the ALL_SENSING_POINTS for all the cases?

Not all TCs have 4 sensors.
And ALL_SENSING_POINTS need more hardware resource/time to check 4 sensors. So we use one sensor for usage.

Quoted 4 lines
Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.c new file +4 -0
+     msr_raw = lvts_temp_to_raw(&lvts_data->coeff, trip_point);
+     writel(msr_raw, LVTSPROTTC_0 + base);
+     enable_hw_reboot_interrupt(lvts_data, tc_id);
+}

[...]

Quoted 4 lines
Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.c new file +4 -0
+static void tc_irq_handler(struct lvts_data *lvts_data, int tc_id)
+{
+     void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+     const struct device *dev = lvts_data->dev;
Quoted 9 lines

drivers/thermal/mediatek/lvts_thermal.c line 595

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+     unsigned int ret = readl(LVTSMONINTSTS_0 + base);
+
+     /* Write back to clear interrupt status */
+     writel(ret, LVTSMONINTSTS_0 + base);
+     dev_dbg(dev, "LVTS thermal controller %d, LVTSMONINTSTS=0x%08x\n", tc_id, ret);
+     if (ret & THERMAL_PROTECTION_STAGE_3)

That means ?

It’s hardware design for clearing the interrupt status. (write register to clear interrupt status)

Quoted 5 lines
Quoted 1 line

+ dev_dbg(dev, "Thermal protection stage 3 interrupt triggered\n");

When do we notify the thermal zone an interrupt happened because of a trip point?

Just to print log to let the software know that the highest/reboot temperature has happened.

Quoted 9 lines
Quoted 9 lines
drivers/thermal/mediatek/lvts_thermal.c new file +9 -0
+}
+
+static irqreturn_t irq_handler(int irq, void *dev_id)
+{
+     void __iomem *base;
+     struct lvts_data *lvts_data = (struct lvts_data *)dev_id;
+     struct device *dev = lvts_data->dev;
+     const struct lvts_tc_settings *tc = lvts_data->tc;
+     unsigned int i, irq_bitmap;
Quoted 9 lines

drivers/thermal/mediatek/lvts_thermal.c line 609

Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.c new file +4 -0
+
+     base = lvts_data->base;
+     irq_bitmap = readl(THERMINTST + base);
+     dev_dbg(dev, "THERMINTST = 0x%x\n", irq_bitmap);

IIUC, there is one interrupt happening when a temperature threshold is crossed but we don't know which sensor it is. So we retrieve the bit mask of interrupt pending, right ? ...

The interrupt belongs to TC.

Quoted 5 lines

drivers/thermal/mediatek/lvts_thermal.c

Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.c new file +2 -0
+     for (i = 0; i < lvts_data->num_tc; i++) {
+             if (tc[i].irq_bit == 0)

... but then what do we do with this bitmap ?

The bitmap is used to store values of AP and MCU.

Quoted 5 lines
Quoted 5 lines
drivers/thermal/mediatek/lvts_thermal.c new file +5 -0
+                     tc_irq_handler(lvts_data, i);
+     }
+
+     return IRQ_HANDLED;
+}

[...]

Quoted 10 lines
Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.h new file +4 -0
new file mode 100644
index 000000000000..a94ce46acccd
@@ -0,0 +1,385 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */

[...]

Quoted 5 lines

drivers/thermal/mediatek/lvts_thermal.h line 275

Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.h new file +2 -0
+
+struct lvts_data;

This forward declaration should not be needed

This is needed because "struct platform_ops" uses "struct lvts_data" as args. removing it causes a compilation error.
and "struct lvts_data" also uses "struct platform_ops" as a member. Do you have any suggestions on removing this forward declaration, if it must be removed?

[...]

Quoted 9 lines

drivers/thermal/mediatek/lvts_thermal.h line 300

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.h new file +6 -0
+struct lvts_tc_settings {
+     unsigned int dev_id;
+     unsigned int addr_offset;
+     unsigned int num_sensor;
+     unsigned int ts_offset;
+     unsigned int sensor_map[ALL_SENSING_POINTS];    /* In sensor ID */

Can you explain what is for the sensor_map?

A controller contains up to 4 sensors.
Example :
...
.sensor_map = {MT8195_TS1_0, MT8195_TS1_1},
...
.sensor_map = {MT8195_TS3_0, MT8195_TS3_1, MT8195_TS3_2, MT8195_TS3_3},

Quoted 12 lines

drivers/thermal/mediatek/lvts_thermal.h line 311

Quoted 11 lines
drivers/thermal/mediatek/lvts_thermal.h new file +11 -0
+     struct lvts_speed_settings *tc_speed;
+     /*
+      * HW filter setting
+      * 000: Get one sample
+      * 001: Get 2 samples and average them
+      * 010: Get 4 samples, drop max and min, then average the rest of 2 samples
+      * 011: Get 6 samples, drop max and min, then average the rest of 4 samples
+      * 100: Get 10 samples, drop max and min, then average the rest of 8 samples
+      * 101: Get 18 samples, drop max and min, then average the rest of 16 samples
+      */
+     unsigned int hw_filter;
Quoted 3 lines

Isn't possible to specify a latency constraint instead of a hw_filter and deduce this one?

These are hardware pre-defined settings.

Quoted 7 lines

drivers/thermal/mediatek/lvts_thermal.h line 317

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.h new file +6 -0
+     /*
+      * Dominator_sensing point is used to select a sensing point
+      * and reference its temperature to trigger Thermal HW Reboot
+      * When it is ALL_SENSING_POINTS, it will select all sensing points
+      */
+     int dominator_sensing_point;
Quoted 7 lines

replace int by the enum, hopefully 'clang' can detect enum mismatches

Quoted 1 line

+ int hw_reboot_trip_point; /* -274000: Disable HW reboot */

It is not necessary to specify the hw_reboot_trip_point, it is all the same in the declaration.

Because new chips in the future may have different settings for different modules,
in order to maintain flexibility, it is recommended to keep hw_reboot_trip_point in the struct

Quoted 2 lines
Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.h new file +2 -0
+     unsigned int irq_bit;
+};

[...]

Quoted 5 lines

drivers/thermal/mediatek/lvts_thermal.h line 348

Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.h new file +2 -0
+
+struct lvts_data {

IMO it would make sense to rename the lvts_data to lvts_thermal_domain

Quoted 8 lines

drivers/thermal/mediatek/lvts_thermal.h line 352

Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.h new file +4 -0
+     struct device *dev;
+     struct clk *clk;
+     void __iomem *base;                             /* LVTS base addresses */
+     unsigned int irq_num;                   /* LVTS interrupt numbers */

The probe function can be reworked to get the irq and the pass it the register function. No need to store it in this structure.

Still working on it...
But, it will be good to give me a little help.

Quoted 13 lines

drivers/thermal/mediatek/lvts_thermal.h line 364

Quoted 12 lines
drivers/thermal/mediatek/lvts_thermal.h new file +12 -0
+     struct reset_control *reset;
+     int num_tc;                                             /* Number of LVTS thermal controllers */
+     const struct lvts_tc_settings *tc;
+     int counting_window_us;                 /* LVTS device counting window */
+     int num_sensor;                                 /* Number of sensors in this platform */
+     void __iomem **reg;
+     struct platform_ops ops;
+     int feature_bitmap;                             /* Show what features are enabled */
+     unsigned int num_efuse_addr;
+     unsigned int *efuse;
+     unsigned int num_efuse_block;   /* Number of contiguous efuse indexes */
+     struct lvts_sensor_cal_data cal_data;
Quoted 3 lines

Everything related to the initialization must be somehow removed from this structure.

Still working on it...
But, it will be good to give me a little help.

Quoted 2 lines

drivers/thermal/mediatek/lvts_thermal.h

Quoted 2 lines
drivers/thermal/mediatek/lvts_thermal.h new file +2 -0
+     struct lvts_formula_coeff coeff;
+};

[...]

Best regards,
Balsam

#21 lore raw

Re: [PATCH v9,4/7] thermal: mediatek: Add LVTS driver for mt8192 thermal zones

In reply to [email protected]

Il 17/08/22 10:07, [email protected] ha scritto:

Quoted 5 lines

From: Michael Kao <[email protected]>

Add LVTS v4 (Low Voltage Thermal Sensor) driver to report junction temperatures in MediaTek SoC mt8192 and register the maximum temperature of sensors and each sensor as a thermal zone.

Hello Balsam,
thanks for the patch! However, there's something to improve...

Quoted 113 lines
---
  drivers/thermal/mediatek/Kconfig        |  21 +
  drivers/thermal/mediatek/Makefile       |   2 +
  drivers/thermal/mediatek/lvts_thermal.c | 861 ++++++++++++++++++++++++
  drivers/thermal/mediatek/lvts_thermal.h | 385 +++++++++++
  drivers/thermal/mediatek/lvts_v4.c      | 249 +++++++
  5 files changed, 1518 insertions(+)

create mode 100644 drivers/thermal/mediatek/lvts_thermal.c create mode 100644 drivers/thermal/mediatek/lvts_thermal.h create mode 100644 drivers/thermal/mediatek/lvts_v4.c

drivers/thermal/mediatek/Kconfig +21 -0
index 8ff32370b591..02a1b3b42ce0 100644
@@ -19,4 +19,25 @@ config MTK_SOC_THERMAL
  	  information for MediaTek platforms. This driver configures
  	  thermal controllers to collect temperature via AUXADC interface.
 
+config MTK_LVTS_THERMAL
+	tristate "LVTS temperature sensor driver for MediaTek SoCs"
+	depends on HAS_IOMEM
+	depends on NVMEM
+	depends on RESET_CONTROLLER
+	help
+	  Enable this option if you want to get SoC temperature information
+	  for MediaTek platforms. This driver configures LVTS
+	  (Low Voltage Thermal Sensor) thermal controllers to collect
+	  temperatures via ASIF (Analog Serial Interface).
+
+if MTK_LVTS_THERMAL
+
+config MTK_LVTS_V4
+	tristate "LVTS v4 driver for MediaTek SoCs"
+	help
+	  Enable this option if you want to get temperature information
+	  for LVTS v4 SoCs.
+
+endif
+
  endif
drivers/thermal/mediatek/Makefile +2 -0
index bec325f06a46..e4e62d726776 100644
@@ -1 +1,3 @@
  obj-$(CONFIG_MTK_SOC_THERMAL)		+= auxadc_thermal.o
+obj-$(CONFIG_MTK_LVTS_THERMAL)		+= lvts_thermal.o
+obj-$(CONFIG_MTK_LVTS_V4)			+= lvts_v4.o
drivers/thermal/mediatek/lvts_thermal.c new file +48 -0
new file mode 100644
index 000000000000..a1681b914c69
@@ -0,0 +1,861 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/bits.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/nvmem-consumer.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_irq.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/thermal.h>
+#include "lvts_thermal.h"
+
+static int lvts_raw_to_temp(struct lvts_formula_coeff *co, unsigned int msr_raw)
+{
+	/* This function returns degree mC */
+
+	int temp;
+
+	temp = (co->a * ((unsigned long long)msr_raw)) >> 14;
+	temp = temp + co->golden_temp * 500 + co->b;
+
+	return temp;
+}
+
+static unsigned int lvts_temp_to_raw(struct lvts_formula_coeff *co, int temp)
+{
+	unsigned int msr_raw;
+
+	msr_raw = div_s64((s64)((co->golden_temp * 500 + co->b - temp)) << 14, (-1 * co->a));
+
+	return msr_raw;
+}
+
+static int soc_temp_lvts_read_temp(void *data, int *temperature)
+{

drivers/thermal/mediatek/lvts_thermal.c line 58

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+	struct soc_temp_tz *lvts_tz = (struct soc_temp_tz *)data;
+	struct lvts_data *lvts_data = lvts_tz->lvts_data;
+	struct device *dev = lvts_data->dev;
+	unsigned int msr_raw;
+
+	msr_raw = readl(lvts_data->reg[lvts_tz->id]) & MRS_RAW_MASK;
+	if (msr_raw == 0) {
+		/* Prevents a false critical temperature trap */
+		*temperature = 0;
+		dev_dbg(dev, "LVTS not yet ready\n");

...and you're not returning an error code, despite this function was called while LVTS is still not ready? :-)

Quoted 23 lines
drivers/thermal/mediatek/lvts_thermal.c new file +23 -0
+
+	} else
+		*temperature = lvts_raw_to_temp(&lvts_data->coeff, msr_raw);
+
+	return 0;
+}
+
+static const struct thermal_zone_of_device_ops soc_temp_lvts_ops = {
+	.get_temp = soc_temp_lvts_read_temp,
+};
+
+static void lvts_write_device(struct lvts_data *lvts_data, unsigned int data, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+
+	writel(DEVICE_WRITE(lvts_data) | data, LVTS_CONFIG_0 + base);
+	usleep_range(20, 30);
+}
+
+static unsigned int lvts_read_device(struct lvts_data *lvts_data, unsigned int reg_idx, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+	struct device *dev = lvts_data->dev;

drivers/thermal/mediatek/lvts_thermal.c line 91

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+	unsigned int data;
+	int ret;
+
+	writel(READ_DEVICE_REG(lvts_data, reg_idx), LVTS_CONFIG_0 + base);
+	usleep_range(20, 30);
+	ret = readl_poll_timeout(LVTS_CONFIG_0 + base, data, !(data & DEVICE_ACCESS_STARTUS), 2, 200);
+	if (ret)
+		dev_err(dev, "LVTS_TC_%d DEVICE_ACCESS_START is not ready\n", tc_id);
+
+	data = readl(LVTSRDATA0_0 + base);

If DEVICE_ACCESS_START is not ready, you're not caring at all and still going on with your register read, which I expect to fail by returning invalid data during LVTS setup, which could produce wrong temperature readouts later: even though in function soc_temp_lvts_read_temp() you're checking if msr_raw is zero, that may not be enough as, if the setup is wrong, you may get a temperature offset that will not necessarily make us read zero at msr_raw.

Think about if this leads to a CPU temperature readout of 200°C: the system will shutdown for over-temperature, but that situation wouldn't be true. Of course, the opposite could happen, with a reading of sub-zero temperatures, for which the thermal framework (if configured to do so) will limit the frequency of CPU/GPU/whatever else to the minimum.
I'm sure that you know the implications, anyway, so you will agree with me that such condition would be definitely not ideal.

Please fix that.

Quoted 9 lines
drivers/thermal/mediatek/lvts_thermal.c new file +9 -0
+
+	return data;
+}
+
+static const char * const lvts_error_table[] = {
+	"Idle",
+	"Write transaction",
+	"Waiting for read after write",
+	"Disable continue fetching on device",

drivers/thermal/mediatek/lvts_thermal.c line 110

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+	"Read transaction",
+	"Set device special register for voltage threshold",
+	"Set TSMCU number for fetch"
+};
+
+static void wait_all_tc_sensing_point_idle(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	struct device *dev = lvts_data->dev;
+	unsigned int error_code, is_error;

You don't need the is_error variable.

drivers/thermal/mediatek/lvts_thermal.c line 123

Quoted 13 lines
drivers/thermal/mediatek/lvts_thermal.c new file +13 -0
+	int i, cnt, ret;
+
+	for (cnt = 0; cnt < 2; cnt++) {
+		is_error = 0;
+
+		for (i = 0; i < lvts_data->num_tc; i++) {
+			base = GET_BASE_ADDR(lvts_data, i);
+			ret = readl_poll_timeout(LVTSMSRCTL1_0 + base, error_code,
+				!(error_code & ALL_TC_SENSING_POINT_STATUS), 2, 200);
+			error_code = ((error_code & TC_SENSING_POINT_10) >> 8) +
+				((error_code & TC_SENSING_POINT_7) >> 6) + (error_code & TC_SENSING_POINT_0);
+			if (ret)
+				dev_err(dev, "LVTS_TC_%d Error Code : %s\n", i, lvts_error_table[error_code]);

you're not printing the error code, so just "LVTS_TC%d Error: %s\n"

drivers/thermal/mediatek/lvts_thermal.c line 131

Quoted 8 lines
drivers/thermal/mediatek/lvts_thermal.c new file +8 -0
+
+			if (error_code != 0)
+				is_error = 1;
+		}
+
+		if (is_error == 0)
+			break;
+	}

...if anything goes wrong, this function should return an error for the caller to take action (recovery, reset, retry or whatever else).

return ret; (and please, set ret as the right error for error_code).

drivers/thermal/mediatek/lvts_thermal.c line 135

Quoted 4 lines
drivers/thermal/mediatek/lvts_thermal.c new file +4 -0
+}
+
+static void lvts_reset(struct lvts_data *lvts_data)
+{
	if (IS_ERR_OR_NULL(lvts_data->reset))
		return;

	reset_control_assert(lvts_data->reset);
	reset_control_deassert(lvts_data->reset);

That's better... :-)

Quoted 14 lines
drivers/thermal/mediatek/lvts_thermal.c new file +14 -0
+	if (lvts_data->reset)
+		reset_control_assert(lvts_data->reset);
+
+	if (lvts_data->reset)
+		reset_control_deassert(lvts_data->reset);
+}
+
+static void device_identification(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	struct device *dev = lvts_data->dev;
+	unsigned int i, data;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {

drivers/thermal/mediatek/lvts_thermal.c line 159

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+		base = GET_BASE_ADDR(lvts_data, i);
+		writel(ENABLE_LVTS_CTRL_CLK, LVTSCLKEN_0 + base);
+		lvts_write_device(lvts_data, RESET_ALL_DEVICES(lvts_data), i);
+		writel(READ_BACK_DEVICE_ID(lvts_data), LVTS_CONFIG_0 + base);
+		usleep_range(20, 30);
+		/* Check LVTS device ID */
+		data = (readl(LVTS_ID_0 + base) & DEVICE_REG_DATA);
+		if (data != (lvts_data->tc->dev_id + i))
+			dev_err(dev, "LVTS_TC_%d, Device ID should be 0x%x, but 0x%x\n",
+				i, (lvts_data->tc->dev_id + i), data);

Judging by the error message, this function should return an error number when the dev_id is unknown, as this driver is likely to malfunction if this happens.

Quoted 17 lines
drivers/thermal/mediatek/lvts_thermal.c new file +17 -0
+	}
+}
+
+static void disable_all_sensing_points(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		writel(DISABLE_SENSING_POINT, LVTSMONCTL0_0 + base);
+	}
+}
+
+static void enable_all_sensing_points(struct lvts_data *lvts_data)
+{
+	void __iomem *base;

drivers/thermal/mediatek/lvts_thermal.c line 186

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+	struct device *dev = lvts_data->dev;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	unsigned int i, num;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		num = tc[i].num_sensor;
+		if (num > ALL_SENSING_POINTS) {
+			dev_err(dev, "LVTS_TC_%d, illegal number of sensors: %d\n", i, tc[i].num_sensor);
+

Please remove this extra white line.

Quoted 11 lines
drivers/thermal/mediatek/lvts_thermal.c new file +11 -0
+			continue;
+		}
+
+		if ((tc[i].ts_offset == 1) && (num == 1))
+			writel(LVTS_SINGLE_SENSE | (0x1 << tc[i].ts_offset), LVTSMONCTL0_0 + base);
+
+		else
+			writel(ENABLE_SENSING_POINT(num), LVTSMONCTL0_0 + base);
+	}
+}
+

..snip..

drivers/thermal/mediatek/lvts_thermal.c line 374

Quoted 11 lines
drivers/thermal/mediatek/lvts_thermal.c new file +11 -0
+
+static int lvts_init(struct lvts_data *lvts_data)
+{
+	struct platform_ops *ops = &lvts_data->ops;
+	struct device *dev = lvts_data->dev;
+	int ret;
+
+	ret = clk_prepare_enable(lvts_data->clk);
+	if (ret) {
+		dev_err(dev, "Failed to enable lvts controller clock: %d\n", ret);
+

same here

Quoted 73 lines
drivers/thermal/mediatek/lvts_thermal.c new file +73 -0
+		return ret;
+	}
+
+	lvts_reset(lvts_data);
+	device_identification(lvts_data);
+	if (ops->device_enable_and_init)
+		ops->device_enable_and_init(lvts_data);
+
+	if (HAS_FEATURE(lvts_data, FEATURE_DEVICE_AUTO_RCK) && (ops->device_enable_auto_rck))
+		ops->device_enable_auto_rck(lvts_data);
+
+	else if (ops->device_read_count_rc_n)
+		ops->device_read_count_rc_n(lvts_data);
+
+	if (ops->set_cal_data)
+		ops->set_cal_data(lvts_data);
+
+	disable_all_sensing_points(lvts_data);
+	wait_all_tc_sensing_point_idle(lvts_data);
+	if (ops->init_controller)
+		ops->init_controller(lvts_data);
+
+	enable_all_sensing_points(lvts_data);
+	set_all_tc_hw_reboot(lvts_data);
+
+	return 0;
+}
+
+static int prepare_calibration_data(struct lvts_data *lvts_data)
+{
+	struct device *dev = lvts_data->dev;
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	struct platform_ops *ops = &lvts_data->ops;
+	int i;
+
+	cal_data->count_r = devm_kcalloc(dev, lvts_data->num_sensor,
+		sizeof(*cal_data->count_r), GFP_KERNEL);
+	if (!cal_data->count_r)
+		return -ENOMEM;
+
+	cal_data->count_rc = devm_kcalloc(dev, lvts_data->num_sensor,
+		sizeof(*cal_data->count_rc), GFP_KERNEL);
+	if (!cal_data->count_rc)
+		return -ENOMEM;
+
+	if (ops->efuse_to_cal_data && !cal_data->use_fake_efuse)
+		ops->efuse_to_cal_data(lvts_data);
+
+	if (cal_data->golden_temp == 0 || cal_data->golden_temp > GOLDEN_TEMP_MAX)
+		cal_data->use_fake_efuse = 1;
+
+	if (cal_data->use_fake_efuse) {
+		/* It means all efuse data are equal to 0 */
+		dev_err(dev, "This sample is not calibrated, fake !!\n");
+		cal_data->golden_temp = cal_data->default_golden_temp;
+		for (i = 0; i < lvts_data->num_sensor; i++) {
+			cal_data->count_r[i] = cal_data->default_count_r;
+			cal_data->count_rc[i] = cal_data->default_count_rc;
+		}
+	}
+
+	lvts_data->coeff.golden_temp = cal_data->golden_temp;
+	dev_dbg(dev, "golden_temp = %d\n", cal_data->golden_temp);
+
+	return 0;
+}
+
+static int get_calibration_data(struct lvts_data *lvts_data)
+{
+	struct device *dev = lvts_data->dev;
+	char cell_name[32];
+	struct nvmem_cell *cell;
+	u32 *buf;

drivers/thermal/mediatek/lvts_thermal.c line 457

Quoted 10 lines
drivers/thermal/mediatek/lvts_thermal.c new file +10 -0
+	size_t len;
+	int i, j, index = 0, ret;
+
+	lvts_data->efuse = devm_kcalloc(dev, lvts_data->num_efuse_addr,
+		sizeof(*lvts_data->efuse), GFP_KERNEL);
+	if (!lvts_data->efuse)
+		return -ENOMEM;
+
+	for (i = 0; i < lvts_data->num_efuse_block; i++) {
+		snprintf(cell_name, sizeof(cell_name), "lvts_calib_data%d", i + 1);

Can we please avoid using underscores? lvts-calib-data%d looks a bit better, especially in devicetree.

Quoted 1,050 lines
drivers/thermal/mediatek/lvts_thermal.c new file +404 -0
+		cell = nvmem_cell_get(dev, cell_name);
+		if (IS_ERR(cell)) {
+			dev_err(dev, "Failed to get nvmem cell %s\n", cell_name);
+
+			return PTR_ERR(cell);
+		}
+
+		buf = (u32 *)nvmem_cell_read(cell, &len);
+		nvmem_cell_put(cell);
+		if (IS_ERR(buf))
+			return PTR_ERR(buf);
+
+		for (j = 0; j < (len / sizeof(u32)); j++) {
+			if (index >= lvts_data->num_efuse_addr) {
+				dev_err(dev, "Array efuse is going to overflow");
+				kfree(buf);
+
+				return -EINVAL;
+			}
+
+			lvts_data->efuse[index] = buf[j];
+			index++;
+		}
+
+		kfree(buf);
+	}
+
+	ret = prepare_calibration_data(lvts_data);
+
+	return ret;
+}
+
+static int lvts_init_tc_regs(struct device *dev, struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	unsigned int i, j, s_index, x;
+
+	lvts_data->reg = devm_kcalloc(dev, lvts_data->num_sensor,
+		sizeof(*lvts_data->reg), GFP_KERNEL);
+	if (!lvts_data->reg)
+		return -ENOMEM;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		for (j = 0; j < tc[i].num_sensor; j++) {
+			s_index = tc[i].sensor_map[j];
+			x = j + tc[i].ts_offset;
+			lvts_data->reg[s_index] = LVTSMSR0_0 + base + 0x4 * x;
+		}
+	}
+
+	return 0;
+}
+
+static int of_update_lvts_data(struct lvts_data *lvts_data, struct platform_device *pdev)
+{
+	struct device *dev = lvts_data->dev;
+	struct resource *res;
+	int ret;
+
+	lvts_data->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(lvts_data->clk))
+		return PTR_ERR(lvts_data->clk);
+
+	/* Get base address */
+	res = platform_get_mem_or_io(pdev, 0);
+	if (!res) {
+		dev_err(dev, "No IO resource\n");
+
+		return -ENXIO;
+	}
+
+	lvts_data->base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(lvts_data->base)) {
+		dev_err(dev, "Failed to remap io\n");
+
+		return PTR_ERR(lvts_data->base);
+	}
+
+	/* Get interrupt number */
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0) {
+		dev_err(dev, "No irq resource\n");
+
+		return ret;
+	}
+
+	lvts_data->irq_num = ret;
+	/* Get reset control */
+	lvts_data->reset = devm_reset_control_get_by_index(dev, 0);
+	if (IS_ERR(lvts_data->reset)) {
+		dev_err(dev, "Failed to get reset control\n");
+
+		return PTR_ERR(lvts_data->reset);
+	}
+
+	ret = lvts_init_tc_regs(dev, lvts_data);
+	if (ret)
+		return ret;
+
+	ret = get_calibration_data(lvts_data);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static void lvts_device_close(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		lvts_write_device(lvts_data, RESET_ALL_DEVICES(lvts_data), i);
+		writel(DISABLE_LVTS_CTRL_CLK, LVTSCLKEN_0 + base);
+	}
+}
+
+static void lvts_close(struct lvts_data *lvts_data)
+{
+	disable_all_sensing_points(lvts_data);
+	wait_all_tc_sensing_point_idle(lvts_data);
+	lvts_device_close(lvts_data);
+	clk_disable_unprepare(lvts_data->clk);
+}
+
+static void tc_irq_handler(struct lvts_data *lvts_data, int tc_id)
+{
+	void __iomem *base = GET_BASE_ADDR(lvts_data, tc_id);
+	const struct device *dev = lvts_data->dev;
+	unsigned int ret = readl(LVTSMONINTSTS_0 + base);
+
+	/* Write back to clear interrupt status */
+	writel(ret, LVTSMONINTSTS_0 + base);
+	dev_dbg(dev, "LVTS thermal controller %d, LVTSMONINTSTS=0x%08x\n", tc_id, ret);
+	if (ret & THERMAL_PROTECTION_STAGE_3)
+		dev_dbg(dev, "Thermal protection stage 3 interrupt triggered\n");
+}
+
+static irqreturn_t irq_handler(int irq, void *dev_id)
+{
+	void __iomem *base;
+	struct lvts_data *lvts_data = (struct lvts_data *)dev_id;
+	struct device *dev = lvts_data->dev;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	unsigned int i, irq_bitmap;
+
+	base = lvts_data->base;
+	irq_bitmap = readl(THERMINTST + base);
+	dev_dbg(dev, "THERMINTST = 0x%x\n", irq_bitmap);
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		if (tc[i].irq_bit == 0)
+			tc_irq_handler(lvts_data, i);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static int lvts_register_irq_handler(struct lvts_data *lvts_data)
+{
+	struct device *dev = lvts_data->dev;
+	int ret;
+
+	ret = devm_request_irq(dev, lvts_data->irq_num, irq_handler, IRQF_TRIGGER_NONE,
+		"mtk_lvts", lvts_data);
+	if (ret) {
+		dev_err(dev, "Failed to register LVTS IRQ, ret %d, irq_num %d\n",
+			ret, lvts_data->irq_num);
+		lvts_close(lvts_data);
+
+		return ret;
+	}
+
+	return 0;
+}
+
+static int lvts_register_thermal_zones(struct lvts_data *lvts_data)
+{
+	struct device *dev = lvts_data->dev;
+	struct thermal_zone_device *tzdev;
+	struct soc_temp_tz *lvts_tz;
+	int i, ret;
+
+	for (i = 0; i < lvts_data->num_sensor; i++) {
+		lvts_tz = devm_kzalloc(dev, sizeof(*lvts_tz), GFP_KERNEL);
+		if (!lvts_tz) {
+			lvts_close(lvts_data);
+
+			return -ENOMEM;
+		}
+
+		lvts_tz->id = i;
+		lvts_tz->lvts_data = lvts_data;
+		tzdev = devm_thermal_zone_of_sensor_register(dev, lvts_tz->id, lvts_tz,
+			&soc_temp_lvts_ops);
+		if (IS_ERR(tzdev)) {
+			if (lvts_tz->id != 0)
+				return 0;
+
+			ret = PTR_ERR(tzdev);
+			dev_err(dev, "Failed to register lvts tz %d, ret = %d\n", lvts_tz->id, ret);
+			lvts_close(lvts_data);
+
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+void lvts_device_enable_and_init(struct lvts_data *lvts_data)
+{
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		lvts_write_device(lvts_data, STOP_COUNTING_V4, i);
+		lvts_write_device(lvts_data, SET_RG_TSFM_LPDLY_V4, i);
+		lvts_write_device(lvts_data, SET_COUNTING_WINDOW_20US1_V4, i);
+		lvts_write_device(lvts_data, SET_COUNTING_WINDOW_20US2_V4, i);
+		lvts_write_device(lvts_data, TSV2F_CHOP_CKSEL_AND_TSV2F_EN_V4, i);
+		lvts_write_device(lvts_data, TSBG_DEM_CKSEL_X_TSBG_CHOP_EN_V4, i);
+		lvts_write_device(lvts_data, SET_TS_RSV_V4, i);
+		lvts_write_device(lvts_data, SET_TS_EN_V4, i);
+		lvts_write_device(lvts_data, TOGGLE_RG_TSV2F_VCO_RST1_V4, i);
+		lvts_write_device(lvts_data, TOGGLE_RG_TSV2F_VCO_RST2_V4, i);
+	}
+
+	lvts_data->counting_window_us = 20;
+}
+EXPORT_SYMBOL_GPL(lvts_device_enable_and_init);
+
+void lvts_device_enable_auto_rck_v4(struct lvts_data *lvts_data)
+{
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++)
+		lvts_write_device(lvts_data, SET_LVTS_AUTO_RCK_V4, i);
+}
+EXPORT_SYMBOL_GPL(lvts_device_enable_auto_rck_v4);
+
+int lvts_device_read_count_rc_n_v4(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	struct device *dev = lvts_data->dev;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	unsigned int s_index, data;
+	int ret, i, j;
+
+	cal_data->count_rc_now = devm_kcalloc(dev, lvts_data->num_sensor,
+		sizeof(*cal_data->count_rc_now), GFP_KERNEL);
+	if (!cal_data->count_rc_now)
+		return -ENOMEM;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		for (j = 0; j < tc[i].num_sensor; j++) {
+			s_index = tc[i].sensor_map[j];
+			lvts_write_device(lvts_data, SELECT_SENSOR_RCK_V4(j), i);
+			lvts_write_device(lvts_data, SET_DEVICE_SINGLE_MODE_V4, i);
+			lvts_write_device(lvts_data, KICK_OFF_RCK_COUNTING_V4, i);
+			ret = readl_poll_timeout(LVTS_CONFIG_0 + base, data,
+				!(data & DEVICE_SENSING_STATUS), 2, 200);
+			if (ret)
+				dev_err(dev, "LVTS_TC_%d DEVICE_SENSING_STATUS didn't ready\n", i);
+
+			data = lvts_read_device(lvts_data, 0x00, i);
+			cal_data->count_rc_now[s_index] = (data & COUNT_RC_NOW_MASK);
+		}
+
+		/* Recover Setting for Normal Access on
+		 * temperature fetch
+		 */
+		lvts_write_device(lvts_data, SET_SENSOR_NO_RCK_V4, i);
+		lvts_write_device(lvts_data, SET_DEVICE_LOW_POWER_SINGLE_MODE_V4, i);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(lvts_device_read_count_rc_n_v4);
+
+void lvts_set_calibration_data_v4(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	const struct lvts_tc_settings *tc = lvts_data->tc;
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	unsigned int i, j, s_index, x;
+	u32 lvts_calib_data;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		for (j = 0; j < tc[i].num_sensor; j++) {
+			s_index = tc[i].sensor_map[j];
+			x = j + tc[i].ts_offset;
+			if (HAS_FEATURE(lvts_data, FEATURE_DEVICE_AUTO_RCK))
+				lvts_calib_data = cal_data->count_r[s_index];
+
+			else
+				lvts_calib_data = (((u32)cal_data->count_rc_now[s_index]) *
+					cal_data->count_r[s_index]) >> 14;
+
+			writel(lvts_calib_data, LVTSEDATA00_0 + base + 0x4 * x);
+		}
+	}
+}
+EXPORT_SYMBOL_GPL(lvts_set_calibration_data_v4);
+
+void lvts_init_controller_v4(struct lvts_data *lvts_data)
+{
+	void __iomem *base;
+	struct device *dev = lvts_data->dev;
+	unsigned int i;
+
+	for (i = 0; i < lvts_data->num_tc; i++) {
+		base = GET_BASE_ADDR(lvts_data, i);
+		lvts_write_device(lvts_data, SET_DEVICE_LOW_POWER_SINGLE_MODE_V4, i);
+		writel(SET_SENSOR_INDEX, LVTSTSSEL_0 + base);
+		writel(SET_CALC_SCALE_RULES, LVTSCALSCALE_0 + base);
+		set_polling_speed(lvts_data, i);
+		set_hw_filter(lvts_data, i);
+		dev_dbg(dev, "lvts_tc_%d: read all %d sensors in %d us, one in %d us\n",
+			i, GET_TC_SENSOR_NUM(lvts_data, i), GROUP_LATENCY_US(lvts_data, i),
+			SENSOR_LATENCY_US(lvts_data, i));
+	}
+}
+EXPORT_SYMBOL_GPL(lvts_init_controller_v4);
+
+int lvts_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct lvts_data *lvts_data;
+	int ret;
+
+	lvts_data = (struct lvts_data *)of_device_get_match_data(dev);
+	if (!lvts_data)	{
+		dev_err(dev, "Failed to get lvts platform data\n");
+
+		return -ENODATA;
+	}
+
+	lvts_data->dev = &pdev->dev;
+	ret = of_update_lvts_data(lvts_data, pdev);
+	if (ret)
+		return ret;
+
+	platform_set_drvdata(pdev, lvts_data);
+	ret = lvts_init(lvts_data);
+	if (ret)
+		return ret;
+
+	ret = lvts_register_irq_handler(lvts_data);
+	if (ret)
+		return ret;
+
+	ret = lvts_register_thermal_zones(lvts_data);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+int lvts_remove(struct platform_device *pdev)
+{
+	struct lvts_data *lvts_data;
+
+	lvts_data = (struct lvts_data *)platform_get_drvdata(pdev);
+	lvts_close(lvts_data);
+
+	return 0;
+}
+
+int lvts_suspend(struct platform_device *pdev, pm_message_t state)
+{
+	int ret;
+
+	ret = lvts_remove(pdev);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+int lvts_resume(struct platform_device *pdev)
+{
+	int ret;
+
+	ret = lvts_probe(pdev);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+void lvts_shutdown(struct platform_device *pdev)
+{
+	lvts_remove(pdev);
+}
+
+MODULE_AUTHOR("Yu-Chia Chang <[email protected]>");
+MODULE_AUTHOR("Michael Kao <[email protected]>");
+MODULE_DESCRIPTION("MediaTek LVTS Thermal Driver");
+MODULE_LICENSE("GPL");
drivers/thermal/mediatek/lvts_thermal.h new file +385 -0
new file mode 100644
index 000000000000..a94ce46acccd
@@ -0,0 +1,385 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#ifndef __MTK_SOC_TEMP_LVTS_H__
+#define __MTK_SOC_TEMP_LVTS_H__
+
+#define PERIOD_UNIT				12
+#define GROUP_INTERVAL_DELAY	1
+#define FILTER_INTERVAL_DELAY	1
+#define SENSOR_INTERVAL_DELAY	1
+
+#define HW_REBOOT_TRIP_POINT	117000
+
+#define FEATURE_DEVICE_AUTO_RCK	BIT(0)
+#define NUM_EFUSE_ADDR			22
+#define NUM_EFUSE_BLOCK_MT8192	1
+#define DEFAULT_GOLDEN_TEMP		50
+#define DEFAULT_CUONT_R			35000
+#define DEFAULT_CUONT_RC		2750
+#define COEFF_A					-250460
+#define COEFF_B					250460
+
+#define CLOCK_26MHZ_CYCLE_NS	38
+#define BUS_ACCESS_US			2
+#define GOLDEN_TEMP_MAX			62
+
+#define LVTS_FILTER_SAMPLES_1	1
+#define LVTS_FILTER_SAMPLES_2	2
+#define LVTS_FILTER_SAMPLES_4	4
+#define LVTS_FILTER_SAMPLES_6	6
+#define LVTS_FILTER_SAMPLES_10	10
+#define LVTS_FILTER_SAMPLES_18	18
+
+#define TC_SENSING_POINT_0			BIT(0)
+#define TC_SENSING_POINT_7			BIT(7)
+#define TC_SENSING_POINT_10			BIT(10)
+#define ALL_TC_SENSING_POINT_STATUS	(BIT(10) | BIT(7) | BIT(0))
+#define COUNT_RC_NOW_MASK			GENMASK(23, 0)
+#define PERIOD_UNIT_MASK			GENMASK(9, 0)
+#define GROUP_INTERVAL_DELAY_MASK	GENMASK(29, 20)
+#define FILTER_INTERVAL_DELAY_MASK	GENMASK(25, 16)
+#define SENSOR_INTERVAL_DELAY_MASK	GENMASK(9, 0)
+
+/* LVTS device register */
+#define RG_TSFM_DATA_0			0x00
+#define RG_TSFM_DATA_1			0x01
+#define RG_TSFM_DATA_2			0x02
+#define RG_TSFM_CTRL_0			0x03
+#define RG_TSFM_CTRL_1			0x04
+#define RG_TSFM_CTRL_2			0x05
+#define RG_TSFM_CTRL_3			0x06
+#define RG_TSFM_CTRL_4			0x07
+#define RG_TSV2F_CTRL_0			0x08
+#define RG_TSV2F_CTRL_1			0x09
+#define RG_TSV2F_CTRL_2			0x0A
+#define RG_TSV2F_CTRL_3			0x0B
+#define RG_TSV2F_CTRL_4			0x0C
+#define RG_TSV2F_CTRL_5			0x0D
+#define RG_TSV2F_CTRL_6			0x0E
+#define RG_TEMP_DATA_0			0x10
+#define RG_TEMP_DATA_1			0x11
+#define RG_TEMP_DATA_2			0x12
+#define RG_TEMP_DATA_3			0x13
+#define RG_RC_DATA_0			0x14
+#define RG_RC_DATA_1			0x15
+#define RG_RC_DATA_2			0x16
+#define RG_RC_DATA_3			0x17
+#define RG_DIV_DATA_0			0x18
+#define RG_DIV_DATA_1			0x19
+#define RG_DIV_DATA_2			0x1A
+#define RG_DIV_DATA_3			0x1B
+#define RG_TST_DATA_0			0x70
+#define RG_TST_DATA_1			0x71
+#define RG_TST_DATA_2			0x72
+#define RG_TST_CTRL				0x73
+#define RG_DBG_FQMTR			0xF0
+#define RG_DBG_LPSEQ			0xF1
+#define RG_DBG_STATE			0xF2
+#define RG_DBG_CHKSUM			0xF3
+#define RG_DID_LVTS				0xFC
+#define RG_DID_REV				0xFD
+#define RG_TSFM_RST				0xFF
+
+/* LVTS controller register */
+#define LVTSMONCTL0_0				0x000
+#define ENABLE_SENSING_POINT(num)	(LVTS_SINGLE_SENSE | GENMASK(((num) - 1), 0))
+#define DISABLE_SENSING_POINT		(LVTS_SINGLE_SENSE | 0x0)
+#define LVTSMONCTL1_0				0x004
+#define LVTSMONCTL2_0				0x008
+#define LVTSMONINT_0				0x00C
+#define STAGE3_INT_EN				BIT(31)
+#define LVTSMONINTSTS_0				0x010
+#define LVTSMONIDET0_0				0x014
+#define LVTSMONIDET1_0				0x018
+#define LVTSMONIDET2_0				0x01C
+#define LVTSMONIDET3_0				0x020
+#define LVTSH2NTHRE_0				0x024
+#define LVTSHTHRE_0					0x028
+#define LVTSCTHRE_0					0x02C
+#define LVTSOFFSETH_0				0x030
+#define LVTSOFFSETL_0				0x034
+#define LVTSMSRCTL0_0				0x038
+#define LVTSMSRCTL1_0				0x03C
+#define LVTSTSSEL_0					0x040
+#define SET_SENSOR_INDEX			0x13121110
+#define LVTSDEVICETO_0				0x044
+#define LVTSCALSCALE_0				0x048
+#define SET_CALC_SCALE_RULES		0x00000300
+#define LVTS_ID_0					0x04C
+#define LVTS_CONFIG_0				0x050
+
+#define SCK_ONLY					BIT(31)
+#define BROADCAST_ID_UPDATE			BIT(26)
+#define DEVICE_SENSING_STATUS		BIT(25)
+#define DEVICE_ACCESS_STARTUS		BIT(24)
+#define READ_32BIT_ACCESS			BIT(17)
+#define WRITE_ACCESS				BIT(16)
+#define LVTS_SINGLE_SENSE			BIT(9)
+#define FEATURE_CK26M_ACTIVE		BIT(1)
+#define DEVICE_REG_DATA				GENMASK(7, 0)
+
+#define LVTSEDATA00_0				0x054
+#define LVTSEDATA01_0				0x058
+#define LVTSEDATA02_0				0x05C
+#define LVTSEDATA03_0				0x060
+#define LVTSMSR0_0					0x090
+#define MRS_RAW_MASK				GENMASK(15, 0)
+#define MRS_RAW_VALID_BIT			BIT(16)
+#define LVTSMSR1_0					0x094
+#define LVTSMSR2_0					0x098
+#define LVTSMSR3_0					0x09C
+#define LVTSIMMD0_0					0x0A0
+#define LVTSIMMD1_0					0x0A4
+#define LVTSIMMD2_0					0x0A8
+#define LVTSIMMD3_0					0x0AC
+#define LVTSRDATA0_0				0x0B0
+#define LVTSRDATA1_0				0x0B4
+#define LVTSRDATA2_0				0x0B8
+#define LVTSRDATA3_0				0x0BC
+#define LVTSPROTCTL_0				0x0C0
+#define PROTOFFSET					GENMASK(15, 0)
+#define LVTSPROTTA_0				0x0C4
+#define LVTSPROTTB_0				0x0C8
+#define LVTSPROTTC_0				0x0CC
+#define LVTSCLKEN_0					0x0E4
+#define ENABLE_LVTS_CTRL_CLK		(1)
+#define DISABLE_LVTS_CTRL_CLK		(0)
+#define LVTSDBGSEL_0				0x0E8
+#define LVTSDBGSIG_0				0x0EC
+#define LVTSSPARE0_0				0x0F0
+#define LVTSSPARE1_0				0x0F4
+#define LVTSSPARE2_0				0x0F8
+#define LVTSSPARE3_0				0x0FC
+#define THERMINTST					0xF04
+
+/* LVTS register mask */
+#define THERMAL_COLD_INTERRUPT_0			BIT(0)
+#define THERMAL_HOT_INTERRUPT_0				BIT(1)
+#define THERMAL_LOW_OFFSET_INTERRUPT_0		BIT(2)
+#define THERMAL_HIGH_OFFSET_INTERRUPT_0		BIT(3)
+#define THERMAL_HOT2NORMAL_INTERRUPT_0		BIT(4)
+#define THERMAL_COLD_INTERRUPT_1			BIT(5)
+#define THERMAL_HOT_INTERRUPT_1				BIT(6)
+#define THERMAL_LOW_OFFSET_INTERRUPT_1		BIT(7)
+#define THERMAL_HIGH_OFFSET_INTERRUPT_1		BIT(8)
+#define THERMAL_HOT2NORMAL_INTERRUPT_1		BIT(9)
+#define THERMAL_COLD_INTERRUPT_2			BIT(10)
+#define THERMAL_HOT_INTERRUPT_2				BIT(11)
+#define THERMAL_LOW_OFFSET_INTERRUPT_2		BIT(12)
+#define THERMAL_HIGH_OFFSET_INTERRUPT_2		BIT(13)
+#define THERMAL_HOT2NORMAL_INTERRUPT_2		BIT(14)
+#define THERMAL_AHB_TIMEOUT_INTERRUPT		BIT(15)
+#define THERMAL_DEVICE_TIMEOUT_INTERRUPT	BIT(15)
+#define THERMAL_IMMEDIATE_INTERRUPT_0		BIT(16)
+#define THERMAL_IMMEDIATE_INTERRUPT_1		BIT(17)
+#define THERMAL_IMMEDIATE_INTERRUPT_2		BIT(18)
+#define THERMAL_FILTER_INTERRUPT_0			BIT(19)
+#define THERMAL_FILTER_INTERRUPT_1			BIT(20)
+#define THERMAL_FILTER_INTERRUPT_2			BIT(21)
+#define THERMAL_COLD_INTERRUPT_3			BIT(22)
+#define THERMAL_HOT_INTERRUPT_3				BIT(23)
+#define THERMAL_LOW_OFFSET_INTERRUPT_3		BIT(24)
+#define THERMAL_HIGH_OFFSET_INTERRUPT_3		BIT(25)
+#define THERMAL_HOT2NORMAL_INTERRUPT_3		BIT(26)
+#define THERMAL_IMMEDIATE_INTERRUPT_3		BIT(27)
+#define THERMAL_FILTER_INTERRUPT_3			BIT(28)
+#define THERMAL_PROTECTION_STAGE_1			BIT(29)
+#define THERMAL_PROTECTION_STAGE_2			BIT(30)
+#define THERMAL_PROTECTION_STAGE_3			BIT(31)
+
+#define CFG_REGISTER(reg, value)			(reg << 8 | value)
+#define STOP_COUNTING_V4					CFG_REGISTER(RG_TSFM_CTRL_0, 0x00)
+#define SET_RG_TSFM_LPDLY_V4				CFG_REGISTER(RG_TSFM_CTRL_4, 0xA6)
+#define SET_COUNTING_WINDOW_20US1_V4		CFG_REGISTER(RG_TSFM_CTRL_2, 0x00)
+#define SET_COUNTING_WINDOW_20US2_V4		CFG_REGISTER(RG_TSFM_CTRL_1, 0x20)
+#define TSV2F_CHOP_CKSEL_AND_TSV2F_EN_V4	CFG_REGISTER(RG_TSV2F_CTRL_2, 0x84)
+#define TSBG_DEM_CKSEL_X_TSBG_CHOP_EN_V4	CFG_REGISTER(RG_TSV2F_CTRL_4, 0x7C)
+#define SET_TS_RSV_V4						CFG_REGISTER(RG_TSV2F_CTRL_1, 0x8D)
+#define SET_TS_EN_V4						CFG_REGISTER(RG_TSV2F_CTRL_0, 0xF4)
+#define TOGGLE_RG_TSV2F_VCO_RST1_V4			CFG_REGISTER(RG_TSV2F_CTRL_0, 0xFC)
+#define TOGGLE_RG_TSV2F_VCO_RST2_V4			CFG_REGISTER(RG_TSV2F_CTRL_0, 0xF4)
+
+#define SET_LVTS_AUTO_RCK_V4				CFG_REGISTER(RG_TSV2F_CTRL_6, 0x01)
+#define SELECT_SENSOR_RCK_V4(id)			CFG_REGISTER(RG_TSV2F_CTRL_5, (id))
+#define SET_DEVICE_SINGLE_MODE_V4			CFG_REGISTER(RG_TSFM_CTRL_3, 0x78)
+#define KICK_OFF_RCK_COUNTING_V4			CFG_REGISTER(RG_TSFM_CTRL_0, 0x02)
+#define SET_SENSOR_NO_RCK_V4				CFG_REGISTER(RG_TSV2F_CTRL_5, 0x10)
+#define SET_DEVICE_LOW_POWER_SINGLE_MODE_V4	CFG_REGISTER(RG_TSFM_CTRL_3, 0xB8)
+
+#define HAS_FEATURE(lvts_data, feature)		(lvts_data->feature_bitmap & (feature))
+#define GET_BASE_ADDR(lvts_data, tc_id)		(lvts_data->base + lvts_data->tc[tc_id].addr_offset)
+#define GET_CAL_DATA_BITMASK(index, lvts_data, h, l)	(((index) < lvts_data->num_efuse_addr) ? \
+	((lvts_data->efuse[(index)] & GENMASK(h, l)) >> l) : 0)
+
+#define GET_TC_SENSOR_NUM(lvts_data, tc_id)	(lvts_data->tc[tc_id].num_sensor)
+#define ONE_SAMPLE(lvts_data)				(lvts_data->counting_window_us + 2 * BUS_ACCESS_US)
+#define NUM_OF_SAMPLE(lvts_data, tc_id)		((lvts_data->tc[tc_id].hw_filter < LVTS_FILTER_2) ? \
+	LVTS_FILTER_SAMPLES_1 : ((lvts_data->tc[tc_id].hw_filter > LVTS_FILTER_16_OF_18) ? \
+	LVTS_FILTER_SAMPLES_1 : ((lvts_data->tc[tc_id].hw_filter == LVTS_FILTER_16_OF_18) ? \
+	LVTS_FILTER_SAMPLES_18 : ((lvts_data->tc[tc_id].hw_filter == LVTS_FILTER_8_OF_10) ? \
+	LVTS_FILTER_SAMPLES_10 : (lvts_data->tc[tc_id].hw_filter * 2)))))
+
+#define PERIOD_UNIT_US(lvts_data, tc_id)   ((lvts_data->tc[tc_id].tc_speed->period_unit * 256 \
+	* CLOCK_26MHZ_CYCLE_NS) / 1000)
+#define FILTER_INT_US(lvts_data, tc_id)    (lvts_data->tc[tc_id].tc_speed->filter_interval_delay \
+	* PERIOD_UNIT_US(lvts_data, tc_id))
+#define SENSOR_INT_US(lvts_data, tc_id)    (lvts_data->tc[tc_id].tc_speed->sensor_interval_delay \
+	* PERIOD_UNIT_US(lvts_data, tc_id))
+#define GROUP_INT_US(lvts_data, tc_id)     (lvts_data->tc[tc_id].tc_speed->group_interval_delay \
+	* PERIOD_UNIT_US(lvts_data, tc_id))
+#define SENSOR_LATENCY_US(lvts_data, tc_id)	((NUM_OF_SAMPLE(lvts_data, tc_id) - 1) * \
+	FILTER_INT_US(lvts_data, tc_id) + NUM_OF_SAMPLE(lvts_data, tc_id) * ONE_SAMPLE(lvts_data))
+#define GROUP_LATENCY_US(lvts_data, tc_id)	(GET_TC_SENSOR_NUM(lvts_data, tc_id) * \
+	SENSOR_LATENCY_US(lvts_data, tc_id) + (GET_TC_SENSOR_NUM(lvts_data, tc_id) - 1) * \
+	SENSOR_INT_US(lvts_data, tc_id) + GROUP_INT_US(lvts_data, tc_id))
+
+#define CK26M_ACTIVE(lvts_data)				(((lvts_data->feature_bitmap & FEATURE_CK26M_ACTIVE) \
+	? 1 : 0) << 30)
+#define DEVICE_ACCESS					  (SCK_ONLY | DEVICE_ACCESS_STARTUS | READ_32BIT_ACCESS)
+#define DEVICE_READ(lvts_data)			  (CK26M_ACTIVE(lvts_data) | DEVICE_ACCESS)
+#define DEVICE_WRITE(lvts_data)			  (CK26M_ACTIVE(lvts_data) | DEVICE_ACCESS | WRITE_ACCESS)
+#define RESET_ALL_DEVICES(lvts_data)	  (DEVICE_WRITE(lvts_data) | RG_TSFM_RST << 8 | 0xFF)
+#define READ_DEVICE_REG(lvts_data, reg_id) (DEVICE_READ(lvts_data) | (reg_id) << 8 | 0x00)
+#define READ_BACK_DEVICE_ID(lvts_data)	   (CK26M_ACTIVE(lvts_data) | DEVICE_ACCESS | \
+	BROADCAST_ID_UPDATE | RG_DID_LVTS << 8)
+
+/*
+ * LVTS HW filter settings
+ * 000: Get one sample
+ * 001: Get 2 samples and average them
+ * 010: Get 4 samples, drop max and min, then average the rest of 2 samples
+ * 011: Get 6 samples, drop max and min, then average the rest of 4 samples
+ * 100: Get 10 samples, drop max and min, then average the rest of 8 samples
+ * 101: Get 18 samples, drop max and min, then average the rest of 16 samples
+ */
+enum lvts_hw_filter {
+	LVTS_FILTER_1,
+	LVTS_FILTER_2,
+	LVTS_FILTER_2_OF_4,
+	LVTS_FILTER_4_OF_6,
+	LVTS_FILTER_8_OF_10,
+	LVTS_FILTER_16_OF_18
+};
+
+enum lvts_sensing_point {
+	SENSING_POINT0,
+	SENSING_POINT1,
+	SENSING_POINT2,
+	SENSING_POINT3,
+	ALL_SENSING_POINTS
+};
+
+struct lvts_data;
+
+/**
+ * struct lvts_speed_settings - A structure to hold the data related to polling rate
+ * @period_unit: Period unit is a base for all interval delays
+ * @group_interval_delay:  Delay between different rounds
+ * @filter_interval_delay: Delay between two samples of the same sensor
+ * @sensor_interval_delay: Delay between two samples of differnet sensors
+ *
+ * Calculation is achieved with the following equations:
+ * For the period unit: (period_us * 1000) / (256 * clock_26mhz_cycle_ns)
+ * For the interval delays: delay / period_us
+ */
+struct lvts_speed_settings {
+	unsigned int period_unit;
+	unsigned int group_interval_delay;
+	unsigned int filter_interval_delay;
+	unsigned int sensor_interval_delay;
+};
+
+struct lvts_tc_settings {
+	unsigned int dev_id;
+	unsigned int addr_offset;
+	unsigned int num_sensor;
+	unsigned int ts_offset;
+	unsigned int sensor_map[ALL_SENSING_POINTS];	/* In sensor ID */
+	struct lvts_speed_settings *tc_speed;
+	/*
+	 * HW filter setting
+	 * 000: Get one sample
+	 * 001: Get 2 samples and average them
+	 * 010: Get 4 samples, drop max and min, then average the rest of 2 samples
+	 * 011: Get 6 samples, drop max and min, then average the rest of 4 samples
+	 * 100: Get 10 samples, drop max and min, then average the rest of 8 samples
+	 * 101: Get 18 samples, drop max and min, then average the rest of 16 samples
+	 */
+	unsigned int hw_filter;
+	/*
+	 * Dominator_sensing point is used to select a sensing point
+	 * and reference its temperature to trigger Thermal HW Reboot
+	 * When it is ALL_SENSING_POINTS, it will select all sensing points
+	 */
+	int dominator_sensing_point;
+	int hw_reboot_trip_point;		/* -274000: Disable HW reboot */
+	unsigned int irq_bit;
+};
+
+struct lvts_formula_coeff {
+	int a;
+	int b;
+	unsigned int golden_temp;
+};
+
+struct lvts_sensor_cal_data {
+	int use_fake_efuse;				/* 1: Use fake efuse, 0: Use real efuse */
+	unsigned int golden_temp;
+	unsigned int *count_r;
+	unsigned int *count_rc;
+	unsigned int *count_rc_now;
+	unsigned int default_golden_temp;
+	unsigned int default_count_r;
+	unsigned int default_count_rc;
+};
+
+struct platform_ops {
+	void (*efuse_to_cal_data)(struct lvts_data *lvts_data);
+	void (*device_enable_and_init)(struct lvts_data *lvts_data);
+	void (*device_enable_auto_rck)(struct lvts_data *lvts_data);
+	int (*device_read_count_rc_n)(struct lvts_data *lvts_data);
+	void (*set_cal_data)(struct lvts_data *lvts_data);
+	void (*init_controller)(struct lvts_data *lvts_data);
+};
+
+struct lvts_data {
+	struct device *dev;
+	struct clk *clk;
+	void __iomem *base;				/* LVTS base addresses */
+	unsigned int irq_num;			/* LVTS interrupt numbers */
+	struct reset_control *reset;
+	int num_tc;						/* Number of LVTS thermal controllers */
+	const struct lvts_tc_settings *tc;
+	int counting_window_us;			/* LVTS device counting window */
+	int num_sensor;					/* Number of sensors in this platform */
+	void __iomem **reg;
+	struct platform_ops ops;
+	int feature_bitmap;				/* Show what features are enabled */
+	unsigned int num_efuse_addr;
+	unsigned int *efuse;
+	unsigned int num_efuse_block;	/* Number of contiguous efuse indexes */
+	struct lvts_sensor_cal_data cal_data;
+	struct lvts_formula_coeff coeff;
+};
+
+struct soc_temp_tz {
+	unsigned int id;
+	struct lvts_data *lvts_data;
+};
+
+extern void lvts_device_enable_and_init(struct lvts_data *lvts_data);
+extern void lvts_device_enable_auto_rck_v4(struct lvts_data *lvts_data);
+extern int lvts_device_read_count_rc_n_v4(struct lvts_data *lvts_data);
+extern void lvts_set_calibration_data_v4(struct lvts_data *lvts_data);
+extern void lvts_init_controller_v4(struct lvts_data *lvts_data);
+
+extern int lvts_probe(struct platform_device *pdev);
+extern int lvts_remove(struct platform_device *pdev);
+extern int lvts_suspend(struct platform_device *pdev, pm_message_t state);
+extern int lvts_resume(struct platform_device *pdev);
+extern void lvts_shutdown(struct platform_device *pdev);
+
+#endif /* __MTK_SOC_TEMP_LVTS_H__ */
drivers/thermal/mediatek/lvts_v4.c new file +249 -0
new file mode 100644
index 000000000000..3dc00d2589f9
@@ -0,0 +1,249 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/of_irq.h>
+#include <linux/platform_device.h>
+#include "lvts_thermal.h"
+
+enum mt8192_lvts_mcu_sensor_enum {
+	MT8192_TS1_0,
+	MT8192_TS1_1,
+	MT8192_TS2_0,
+	MT8192_TS2_1,
+	MT8192_TS3_0,
+	MT8192_TS3_1,
+	MT8192_TS3_2,
+	MT8192_TS3_3,
+	MT8192_NUM_TS_MCU
+};
+
+enum mt8192_lvts_ap_sensor_enum {
+	MT8192_TS4_0,
+	MT8192_TS4_1,
+	MT8192_TS5_0,
+	MT8192_TS5_1,
+	MT8192_TS6_0,
+	MT8192_TS6_1,
+	MT8192_TS7_0,
+	MT8192_TS7_1,
+	MT8192_TS7_2,
+	MT8192_NUM_TS_AP
+};
+
+static void mt8192_mcu_efuse_to_cal_data(struct lvts_data *lvts_data)
+{
+	const unsigned int mt8192_ts[] = { MT8192_TS2_0, MT8192_TS3_0 };
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	unsigned int i, j;
+
+	cal_data->golden_temp = GET_CAL_DATA_BITMASK(0, lvts_data, 31, 24);
+
+	for (i = 0; i < MT8192_NUM_TS_MCU; i++)
+		cal_data->count_r[i] = GET_CAL_DATA_BITMASK(i + 1, lvts_data, 23, 0);
+
+	cal_data->count_rc[MT8192_TS1_0] = GET_CAL_DATA_BITMASK(21, lvts_data, 23, 0);
+
+	for (i = 0; i < (ARRAY_SIZE(mt8192_ts)); i++) {
+		for (j = 1; j <= 18; j++) {
+			cal_data->count_rc[mt8192_ts[i]] = (GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24)
+				<< 16) + (GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24) << 8) +
+				GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24);
+		}
+	}
+}
+
+static void mt8192_ap_efuse_to_cal_data(struct lvts_data *lvts_data)
+{
+	const unsigned int mt8192_ts[] = { MT8192_TS4_0, MT8192_TS5_0, MT8192_TS6_0, MT8192_TS7_0 };
+	struct lvts_sensor_cal_data *cal_data = &lvts_data->cal_data;
+	unsigned int i, j;
+
+	cal_data->golden_temp = GET_CAL_DATA_BITMASK(0, lvts_data, 31, 24);
+
+	for (i = 0; i < MT8192_NUM_TS_AP; i++)
+		cal_data->count_r[i] = GET_CAL_DATA_BITMASK(i + 1, lvts_data, 23, 0);
+
+	for (i = 0; i < (ARRAY_SIZE(mt8192_ts)); i++) {
+		for (j = 1; j <= 18; j++) {
+			cal_data->count_rc[mt8192_ts[i]] = (GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24)
+				<< 16) + (GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24) << 8) +
+				GET_CAL_DATA_BITMASK(j, lvts_data, 31, 24);
+		}
+	}
+}
+
+static struct lvts_speed_settings tc_speed_mt8192 = {
+	.period_unit = PERIOD_UNIT,
+	.group_interval_delay = GROUP_INTERVAL_DELAY,
+	.filter_interval_delay = FILTER_INTERVAL_DELAY,
+	.sensor_interval_delay = SENSOR_INTERVAL_DELAY,
+};
+
+static const struct lvts_tc_settings mt8192_tc_mcu_settings[] = {
+	[0] = {
+		.dev_id = 0x81,
+		.addr_offset = 0x0,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS1_0, MT8192_TS1_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT1,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(3),
+	},
+	[1] = {
+		.dev_id = 0x82,
+		.addr_offset = 0x100,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS2_0, MT8192_TS2_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT0,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(4),
+	},
+	[2] = {
+		.dev_id = 0x83,
+		.addr_offset = 0x200,
+		.num_sensor = 4,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS3_0, MT8192_TS3_1, MT8192_TS3_2, MT8192_TS3_3 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT0,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(5),
+	}
+};
+
+static const struct lvts_tc_settings mt8192_tc_ap_settings[] = {
+	[0] = {
+		.dev_id = 0x84,
+		.addr_offset = 0x0,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS4_0, MT8192_TS4_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT0,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(3),
+	},
+	[1] = {
+		.dev_id = 0x85,
+		.addr_offset = 0x100,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS5_0, MT8192_TS5_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT1,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(4),
+	},
+	[2] = {
+		.dev_id = 0x86,
+		.addr_offset = 0x200,
+		.num_sensor = 2,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS6_0, MT8192_TS6_1 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT1,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(5),
+	},
+	[3] = {
+		.dev_id = 0x87,
+		.addr_offset = 0x300,
+		.num_sensor = 3,
+		.ts_offset = 0,
+		.sensor_map = { MT8192_TS7_0, MT8192_TS7_1, MT8192_TS7_2 },
+		.tc_speed = &tc_speed_mt8192,
+		.hw_filter = LVTS_FILTER_2_OF_4,
+		.dominator_sensing_point = SENSING_POINT2,
+		.hw_reboot_trip_point = HW_REBOOT_TRIP_POINT,
+		.irq_bit = BIT(6),
+	}
+};
+
+static const struct lvts_data mt8192_lvts_mcu_data = {
+	.num_tc = (ARRAY_SIZE(mt8192_tc_mcu_settings)),
+	.tc = mt8192_tc_mcu_settings,
+	.num_sensor = MT8192_NUM_TS_MCU,
+	.ops = {
+		.efuse_to_cal_data = mt8192_mcu_efuse_to_cal_data,
+		.device_enable_and_init = lvts_device_enable_and_init,
+		.device_enable_auto_rck = lvts_device_enable_auto_rck_v4,
+		.device_read_count_rc_n = lvts_device_read_count_rc_n_v4,
+		.set_cal_data = lvts_set_calibration_data_v4,
+		.init_controller = lvts_init_controller_v4,
+	},
+	.feature_bitmap = FEATURE_DEVICE_AUTO_RCK,
+	.num_efuse_addr = NUM_EFUSE_ADDR,
+	.num_efuse_block = NUM_EFUSE_BLOCK_MT8192,
+	.cal_data = {
+		.default_golden_temp = DEFAULT_GOLDEN_TEMP,
+		.default_count_r = DEFAULT_CUONT_R,
+		.default_count_rc = DEFAULT_CUONT_RC,
+	},
+	.coeff = {
+		.a = COEFF_A,
+		.b = COEFF_B,
+	},
+};
+
+static const struct lvts_data mt8192_lvts_ap_data = {
+	.num_tc = (ARRAY_SIZE(mt8192_tc_ap_settings)),
+	.tc = mt8192_tc_ap_settings,
+	.num_sensor = MT8192_NUM_TS_AP,
+	.ops = {
+		.efuse_to_cal_data = mt8192_ap_efuse_to_cal_data,
+		.device_enable_and_init = lvts_device_enable_and_init,
+		.device_enable_auto_rck = lvts_device_enable_auto_rck_v4,
+		.device_read_count_rc_n = lvts_device_read_count_rc_n_v4,
+		.set_cal_data = lvts_set_calibration_data_v4,
+		.init_controller = lvts_init_controller_v4,
+	},
+	.feature_bitmap = FEATURE_DEVICE_AUTO_RCK,
+	.num_efuse_addr = NUM_EFUSE_ADDR,
+	.num_efuse_block = NUM_EFUSE_BLOCK_MT8192,
+	.cal_data = {
+		.default_golden_temp = DEFAULT_GOLDEN_TEMP,
+		.default_count_r = DEFAULT_CUONT_R,
+		.default_count_rc = DEFAULT_CUONT_RC,
+	},
+	.coeff = {
+		.a = COEFF_A,
+		.b = COEFF_B,
+	},
+};
+
+static const struct of_device_id lvts_of_match[] = {
+	{ .compatible = "mediatek,mt8192-lvts-mcu", .data = &mt8192_lvts_mcu_data, },
+	{ .compatible = "mediatek,mt8192-lvts-ap", .data = &mt8192_lvts_ap_data, },
+	{},
+};
+MODULE_DEVICE_TABLE(of, lvts_of_match);
+
+static struct platform_driver soc_temp_lvts = {
+	.probe = lvts_probe,
+	.remove = lvts_remove,
+	.suspend = lvts_suspend,
+	.resume = lvts_resume,
+	.shutdown = lvts_shutdown,
+	.driver = {
+		.name = "mtk-lvts-thermal-v4",
+		.of_match_table = lvts_of_match,
+	},
+};
+module_platform_driver(soc_temp_lvts);
+
+MODULE_AUTHOR("Yu-Chia Chang <[email protected]>");
+MODULE_AUTHOR("Michael Kao <[email protected]>");
+MODULE_DESCRIPTION("MediaTek LVTS V4 Thermal Driver");
+MODULE_LICENSE("GPL");
#22 lore raw

Re: [PATCH v9,4/7] thermal: mediatek: Add LVTS driver for mt8192 thermal zones

Hi Angelo,

Quoted 7 lines
Quoted 7 lines
drivers/thermal/mediatek/lvts_thermal.c new file +7 -0
+static int soc_temp_lvts_read_temp(void *data, int *temperature)
+{
+     struct soc_temp_tz *lvts_tz = (struct soc_temp_tz *)data;
+     struct lvts_data *lvts_data = lvts_tz->lvts_data;
+     struct device *dev = lvts_data->dev;
+     unsigned int msr_raw;
+
Quoted 9 lines

drivers/thermal/mediatek/lvts_thermal.c line 58

Quoted 5 lines
drivers/thermal/mediatek/lvts_thermal.c new file +5 -0
+     msr_raw = readl(lvts_data->reg[lvts_tz->id]) & MRS_RAW_MASK;
+     if (msr_raw == 0) {
+             /* Prevents a false critical temperature trap */
+             *temperature = 0;
+             dev_dbg(dev, "LVTS not yet ready\n");

...and you're not returning an error code, despite this function was called while LVTS is still not ready? :-)

if I add "return -ENAVAIL;" here, I will get the following errors on boot :

[    0.292094] thermal thermal_zone0: failed to read out thermal zone (-119)
[    0.293019] thermal thermal_zone1: failed to read out thermal zone (-119)
[    0.294158] thermal thermal_zone6: failed to read out thermal zone (-119)
[    0.295697] thermal thermal_zone9: failed to read out thermal zone (-119)
[    0.296600] thermal thermal_zone10: failed to read out thermal zone (-119)
[    0.297698] thermal thermal_zone15: failed to read out thermal zone (-119)
[    0.298625] thermal thermal_zone16: failed to read out thermal zone (-119)

I just preferred : *temperature = 0; to avoid a false critical temperature trap, and "return 0;" to suppress these errors.
After that (first read at boot when LVTS is not fully ready), "msr_raw" will be always != 0.
And I did not want to add a "delay" or a "sleep".
Please let me know if you have any suggestions that could fix it in a better way.

Quoted 6 lines

drivers/thermal/mediatek/lvts_thermal.c line 64

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+
+     } else
+             *temperature = lvts_raw_to_temp(&lvts_data->coeff, msr_raw);
+
+     return 0;
+}

Best regards,
Balsam

#23 lore raw

Re: [PATCH v9,4/7] thermal: mediatek: Add LVTS driver for mt8192 thermal zones

Il 15/09/22 18:58, Balsam CHIHI ha scritto:

Quoted 28 lines

Hi Angelo,

Quoted 7 lines
Quoted 7 lines
drivers/thermal/mediatek/lvts_thermal.c new file +7 -0
+static int soc_temp_lvts_read_temp(void *data, int *temperature)
+{
+     struct soc_temp_tz *lvts_tz = (struct soc_temp_tz *)data;
+     struct lvts_data *lvts_data = lvts_tz->lvts_data;
+     struct device *dev = lvts_data->dev;
+     unsigned int msr_raw;
+
Quoted 9 lines

drivers/thermal/mediatek/lvts_thermal.c line 58

Quoted 5 lines
drivers/thermal/mediatek/lvts_thermal.c new file +5 -0
+     msr_raw = readl(lvts_data->reg[lvts_tz->id]) & MRS_RAW_MASK;
+     if (msr_raw == 0) {
+             /* Prevents a false critical temperature trap */
+             *temperature = 0;
+             dev_dbg(dev, "LVTS not yet ready\n");

...and you're not returning an error code, despite this function was called while LVTS is still not ready? :-)

if I add "return -ENAVAIL;" here, I will get the following errors on boot :

[    0.292094] thermal thermal_zone0: failed to read out thermal zone (-119)
[    0.293019] thermal thermal_zone1: failed to read out thermal zone (-119)
[    0.294158] thermal thermal_zone6: failed to read out thermal zone (-119)
[    0.295697] thermal thermal_zone9: failed to read out thermal zone (-119)
[    0.296600] thermal thermal_zone10: failed to read out thermal zone (-119)
[    0.297698] thermal thermal_zone15: failed to read out thermal zone (-119)
[    0.298625] thermal thermal_zone16: failed to read out thermal zone (-119)
Quoted 8 lines

I just preferred : *temperature = 0; to avoid a false critical temperature trap, and "return 0;" to suppress these errors.
After that (first read at boot when LVTS is not fully ready), "msr_raw" will be always != 0.
And I did not want to add a "delay" or a "sleep".
Please let me know if you have any suggestions that could fix it in a better way.

It's the wrong error number. You have to return -EAGAIN :-)

Quoted 9 lines
Quoted 6 lines

drivers/thermal/mediatek/lvts_thermal.c line 64

Quoted 6 lines
drivers/thermal/mediatek/lvts_thermal.c new file +6 -0
+
+     } else
+             *temperature = lvts_raw_to_temp(&lvts_data->coeff, msr_raw);
+
+     return 0;
+}

Best regards,
Balsam

#24 lore raw

Re: [PATCH v9,4/7] thermal: mediatek: Add LVTS driver for mt8192 thermal zones

Quoted 1 line

It's the wrong error number. You have to return -EAGAIN :-)

Got it, thanks! :D

#25 lore raw

PATCH v9 5/7 arm64: dts: mt8195: Add efuse node to mt8195

In reply to [email protected]

From: Alexandre Bailon <[email protected]>

Add efuse node.
This will be required by the thermal driver to get the calibration data.

---
 arch/arm64/boot/dts/mediatek/mt8195.dtsi | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/boot/dts/mediatek/mt8195.dtsi +12 -0
index 066c14989708..95967a0196d8 100644
@@ -733,6 +733,18 @@ u2_intr_p3: usb2-intr-p3@189,2 {
 				reg = <0x189 0x2>;
 				bits = <7 5>;
 			};
+
+			lvts_efuse_data1: lvts1-calib@1bc {
+				reg = <0x1bc 0x14>;
+			};
+
+			lvts_efuse_data2: lvts2-calib@1d0 {
+				reg = <0x1d0 0x38>;
+			};
+
+			svs_calibration: calib@580 {
+				reg = <0x580 0x64>;
+			};
 		};
 
 		u3phy2: t-phy@11c40000 {
Signature · 1 line
2.34.1
#26 lore raw

Re: [PATCH v9,5/7] arm64: dts: mt8195: Add efuse node to mt8195

Il 17/08/22 10:07, [email protected] ha scritto:

Quoted 20 lines

From: Alexandre Bailon <[email protected]>

Add efuse node.
This will be required by the thermal driver to get the calibration data.

---
  arch/arm64/boot/dts/mediatek/mt8195.dtsi | 12 ++++++++++++
  1 file changed, 12 insertions(+)
arch/arm64/boot/dts/mediatek/mt8195.dtsi +0 -0
index 066c14989708..95967a0196d8 100644
@@ -733,6 +733,18 @@ u2_intr_p3: usb2-intr-p3@189,2 {
  				reg = <0x189 0x2>;
  				bits = <7 5>;
  			};

arch/arm64/boot/dts/mediatek/mt8195.dtsi u2_intr_p3: usb2-intr-p3@189,2 { · line 745

Quoted 10 lines
arch/arm64/boot/dts/mediatek/mt8195.dtsi +10 -0
+
+			lvts_efuse_data1: lvts1-calib@1bc {
+				reg = <0x1bc 0x14>;
+			};
+
+			lvts_efuse_data2: lvts2-calib@1d0 {
+				reg = <0x1d0 0x38>;
+			};
+
+			svs_calibration: calib@580 {

The SVS calibration efuse is for the SVS driver, not for the LVTS. Moreover, you're not using it later when you add the LVTS nodes, so, please drop this one.

Regards,
Angelo

#27 lore raw

PATCH v9 6/7 arm64: dts: mt8195: Add thermal zones and thermal nodes

In reply to [email protected]

From: Tinghan Shen <[email protected]>

Add thermal zones and thermal nodes for the mt8195.

---
 arch/arm64/boot/dts/mediatek/mt8195.dtsi | 111 +++++++++++++++++++++++
 1 file changed, 111 insertions(+)
arch/arm64/boot/dts/mediatek/mt8195.dtsi +111 -0
index 95967a0196d8..aafbbe83e6ba 100644
@@ -10,6 +10,7 @@
 #include <dt-bindings/interrupt-controller/irq.h>
 #include <dt-bindings/phy/phy.h>
 #include <dt-bindings/pinctrl/mt8195-pinfunc.h>
+#include <dt-bindings/reset/mt8195-resets.h>
 
 / {
 	compatible = "mediatek,mt8195";
@@ -452,6 +453,28 @@ spi0: spi@1100a000 {
 			status = "disabled";
 		};
 
+		lvts_ap: thermal-sensor@1100b000 {
+			compatible = "mediatek,mt8195-lvts-ap";
+			#thermal-sensor-cells = <1>;
+			reg = <0 0x1100b000 0 0x1000>;
+			interrupts = <GIC_SPI 169 IRQ_TYPE_LEVEL_HIGH 0>;
+			clocks = <&infracfg_ao CLK_INFRA_AO_THERM>;
+			resets = <&infracfg_ao MT8195_INFRA_RST0_THERM_CTRL_SWRST>;
+			nvmem-cells = <&lvts_efuse_data1 &lvts_efuse_data2>;
+			nvmem-cell-names = "lvts_calib_data1", "lvts_calib_data2";
+		};
+
+		lvts_mcu: thermal-sensor@11278000 {
+			compatible = "mediatek,mt8195-lvts-mcu";
+			#thermal-sensor-cells = <1>;
+			reg = <0 0x11278000 0 0x1000>;
+			interrupts = <GIC_SPI 170 IRQ_TYPE_LEVEL_HIGH 0>;
+			clocks = <&infracfg_ao CLK_INFRA_AO_THERM>;
+			resets = <&infracfg_ao MT8195_INFRA_RST4_THERM_CTRL_MCU_SWRST>;
+			nvmem-cells = <&lvts_efuse_data1 &lvts_efuse_data2>;
+			nvmem-cell-names = "lvts_calib_data1", "lvts_calib_data2";
+		};
+
 		spi1: spi@11010000 {
 			compatible = "mediatek,mt8195-spi",
 				     "mediatek,mt6765-spi";
@@ -1106,4 +1129,92 @@ vencsys_core1: clock-controller@1b000000 {
 			#clock-cells = <1>;
 		};
 	};
+
+	thermal_zones: thermal-zones {
+		cpu0-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 0>;
+		};
+		cpu1-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 1>;
+		};
+		cpu2-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 2>;
+		};
+		cpu3-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 3>;
+		};
+		cpu4-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 4>;
+		};
+		cpu5-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 5>;
+		};
+		cpu6-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 6>;
+		};
+		cpu7-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_mcu 7>;
+		};
+		vpu1-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 0>;
+		};
+		vpu2-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 1>;
+		};
+		gpu1-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 2>;
+		};
+		gpu2-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 3>;
+		};
+		vdec-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 4>;
+		};
+		img-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 5>;
+		};
+		infra-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 6>;
+		};
+		cam1-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 7>;
+		};
+		cam2-thermal {
+			polling-delay = <0>;
+			polling-delay-passive = <0>;
+			thermal-sensors = <&lvts_ap 8>;
+		};
+	};
 };
Signature · 1 line
2.34.1
#28 lore raw

Re: [PATCH v9,6/7] arm64: dts: mt8195: Add thermal zones and thermal nodes

Il 17/08/22 10:07, [email protected] ha scritto:

Quoted 33 lines

From: Tinghan Shen <[email protected]>

Add thermal zones and thermal nodes for the mt8195.

---
  arch/arm64/boot/dts/mediatek/mt8195.dtsi | 111 +++++++++++++++++++++++
  1 file changed, 111 insertions(+)
arch/arm64/boot/dts/mediatek/mt8195.dtsi +3 -0
index 95967a0196d8..aafbbe83e6ba 100644
@@ -10,6 +10,7 @@
  #include <dt-bindings/interrupt-controller/irq.h>
  #include <dt-bindings/phy/phy.h>
  #include <dt-bindings/pinctrl/mt8195-pinfunc.h>
+#include <dt-bindings/reset/mt8195-resets.h>
 
  / {
  	compatible = "mediatek,mt8195";
@@ -452,6 +453,28 @@ spi0: spi@1100a000 {
  			status = "disabled";
  		};
 
+		lvts_ap: thermal-sensor@1100b000 {
+			compatible = "mediatek,mt8195-lvts-ap";

arch/arm64/boot/dts/mediatek/mt8195.dtsi spi0: spi@1100a000 { · line 467

Quoted 10 lines
arch/arm64/boot/dts/mediatek/mt8195.dtsi +10 -0
+			#thermal-sensor-cells = <1>;
+			reg = <0 0x1100b000 0 0x1000>;
+			interrupts = <GIC_SPI 169 IRQ_TYPE_LEVEL_HIGH 0>;
+			clocks = <&infracfg_ao CLK_INFRA_AO_THERM>;
+			resets = <&infracfg_ao MT8195_INFRA_RST0_THERM_CTRL_SWRST>;
+			nvmem-cells = <&lvts_efuse_data1 &lvts_efuse_data2>;
+			nvmem-cell-names = "lvts_calib_data1", "lvts_calib_data2";
+		};
+
+		lvts_mcu: thermal-sensor@11278000 {

Please keep the devicetree nodes ordered by address. This one must be moved between mmc@1125000 and usb@11290000.

Regards,
Angelo

Quoted 13 lines
arch/arm64/boot/dts/mediatek/mt8195.dtsi +10 -0
+			compatible = "mediatek,mt8195-lvts-mcu";
+			#thermal-sensor-cells = <1>;
+			reg = <0 0x11278000 0 0x1000>;
+			interrupts = <GIC_SPI 170 IRQ_TYPE_LEVEL_HIGH 0>;
+			clocks = <&infracfg_ao CLK_INFRA_AO_THERM>;
+			resets = <&infracfg_ao MT8195_INFRA_RST4_THERM_CTRL_MCU_SWRST>;
+			nvmem-cells = <&lvts_efuse_data1 &lvts_efuse_data2>;
+			nvmem-cell-names = "lvts_calib_data1", "lvts_calib_data2";
+		};
+
  		spi1: spi@11010000 {
  			compatible = "mediatek,mt8195-spi",
  				     "mediatek,mt6765-spi";
#29 lore raw

PATCH v9 7/7 thermal: mediatek: Add LVTS driver settings for mt8195 thermal zones

In reply to [email protected]

From: Michael Kao <[email protected]>

Add LVTS v4 (Low Voltage Thermal Sensor) driver settings to report junction temperatures in MediaTek SoC mt8195 and register the maximum temperature of sensors and each sensor as a thermal zone.

---
 drivers/thermal/mediatek/lvts_thermal.h |   1 +
 drivers/thermal/mediatek/lvts_v4.c      | 230 ++++++++++++++++++++++++
 2 files changed, 231 insertions(+)
drivers/thermal/mediatek/lvts_thermal.h +1 -0
index a94ce46acccd..059b1eb98b1c 100644
@@ -16,6 +16,7 @@
 #define FEATURE_DEVICE_AUTO_RCK	BIT(0)
 #define NUM_EFUSE_ADDR			22
 #define NUM_EFUSE_BLOCK_MT8192	1
+#define NUM_EFUSE_BLOCK_MT8195	2
 #define DEFAULT_GOLDEN_TEMP		50
 #define DEFAULT_CUONT_R			35000
 #define DEFAULT_CUONT_RC		2750
drivers/thermal/mediatek/lvts_v4.c +230 -0

263 lines not on this page · raw message

Signature · 1 line
2.34.1