Skip to contentSkip to conversation

thermal: k3_j72xx_bandgap: Add the bandgap driver support

PATCH Superseded Keerthy 3 participants 8 messages Nov 3, 2021 – Nov 12, 2021

Subsystems (3)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Keerthy

author
#0 lore raw

v4 cover thermal: k3_j72xx_bandgap: Add the bandgap driver support

Add VTM thermal support. In the Voltage Thermal Management Module(VTM), K3 J72XX supplies a voltage reference and a temperature sensor feature that are gathered in the band gap voltage and temperature sensor (VBGAPTS) module. The band gap provides current and voltage reference for its internal circuits and other analog IP blocks. The analog-to-digital converter (ADC) produces an output value that is proportional to the silicon temperature.

Currently reading temperatures only is supported. There are no active/passive cooling agent supported.

J721e SoCs have errata i2128: https://www.ti.com/lit/pdf/sprz455

The series also incorporates workaround for Errata i2128.

Changes in v4:

* Fixed compilation warning with W=1.

Changes in v3:

* Removed static look up tables & added functions to dynamically generate them.

Changes in v2:

* Fixed DT binding errors.

Keerthy (4):
dt-bindings: thermal: k3-j72xx: Add VTM bindings documentation
arm64: dts: ti: j721e: Add VTM node
arm64: dts: ti: j7200: Add VTM node
thermal: k3_j72xx_bandgap: Add the bandgap driver support

 .../bindings/thermal/ti,j72xx-thermal.yaml    |  62 ++
 .../boot/dts/ti/k3-j7200-mcu-wakeup.dtsi      |   9 +
 arch/arm64/boot/dts/ti/k3-j7200-thermal.dtsi  |  45 ++
 arch/arm64/boot/dts/ti/k3-j7200.dtsi          |   4 +
 .../boot/dts/ti/k3-j721e-mcu-wakeup.dtsi      |   9 +
 arch/arm64/boot/dts/ti/k3-j721e-thermal.dtsi  |  73 ++
 arch/arm64/boot/dts/ti/k3-j721e.dtsi          |   4 +
 drivers/thermal/Makefile                      |   2 +-
 drivers/thermal/k3_j72xx_bandgap.c            | 634 ++++++++++++++++++
 9 files changed, 841 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml
 create mode 100644 arch/arm64/boot/dts/ti/k3-j7200-thermal.dtsi
 create mode 100644 arch/arm64/boot/dts/ti/k3-j721e-thermal.dtsi
 create mode 100644 drivers/thermal/k3_j72xx_bandgap.c
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v4 1/4 dt-bindings: thermal: k3-j72xx: Add VTM bindings documentation

Add VTM bindings documentation. In the Voltage Thermal Management Module(VTM), K3 J72XX supplies a voltage reference and a temperature sensor feature that are gathered in the band gap voltage and temperature sensor (VBGAPTS) module. The band gap provides current and voltage reference for its internal circuits and other analog IP blocks. The analog-to-digital converter (ADC) produces an output value that is proportional to the silicon temperature.

---
 .../bindings/thermal/ti,j72xx-thermal.yaml    | 62 +++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml
Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml new file +62 -0
new file mode 100644
index 000000000000..8483c495cb9a
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/thermal/ti,j72xx-thermal.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Texas Instruments J72XX VTM (DTS) binding
+
+maintainers:
+  - Keerthy <[email protected]>
+
+properties:
+  compatible:
+    const: ti,j721e-vtm
+    oneOf:
+      - enum:
+          - ti,j721e-vtm
+          - ti,j7200-vtm
+
+  reg:
+    maxItems: 3
+
+  power-domains:
+    maxItems: 1
+
+  "#thermal-sensor-cells":
+    const: 1
+
+required:
+  - compatible
+  - reg
+  - power-domains
+  - "#thermal-sensor-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/soc/ti,sci_pm_domain.h>
+    wkup_vtm0: wkup_vtm0@42040000 {
+        compatible = "ti,j721e-vtm";
+        reg = <0x42040000 0x350>,
+            <0x42050000 0x350>,
+            <0x43000300 0x10>;
+        power-domains = <&k3_pds 154 TI_SCI_PD_EXCLUSIVE>;
+        #thermal-sensor-cells = <1>;
+    };
+
+    mpu_thermal: mpu_thermal {
+        polling-delay-passive = <250>; /* milliseconds */
+        polling-delay = <500>; /* milliseconds */
+        thermal-sensors = <&wkup_vtm0 0>;
+
+        trips {
+                mpu_crit: mpu_crit {
+                        temperature = <125000>; /* milliCelsius */
+                        hysteresis = <2000>; /* milliCelsius */
+                        type = "critical";
+                };
+        };
+    };
+...
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH v4 1/4] dt-bindings: thermal: k3-j72xx: Add VTM bindings documentation

Quoted 29 lines · Keerthy

Add VTM bindings documentation. In the Voltage Thermal Management Module(VTM), K3 J72XX supplies a voltage reference and a temperature sensor feature that are gathered in the band gap voltage and temperature sensor (VBGAPTS) module. The band gap provides current and voltage reference for its internal circuits and other analog IP blocks. The analog-to-digital converter (ADC) produces an output value that is proportional to the silicon temperature.

---
 .../bindings/thermal/ti,j72xx-thermal.yaml    | 62 +++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml
Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml new file +8 -0
new file mode 100644
index 000000000000..8483c495cb9a
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/thermal/ti,j72xx-thermal.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Texas Instruments J72XX VTM (DTS) binding
+

Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml line 18

Quoted 10 lines
Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml new file +10 -0
+maintainers:
+  - Keerthy <[email protected]>
+
+properties:
+  compatible:
+    const: ti,j721e-vtm
+    oneOf:
+      - enum:
+          - ti,j721e-vtm
+          - ti,j7200-vtm

Again, this isn't correct. In C like logic, it is saying compatible is (ti,j721e-vtm && (ti,j721e-vtm || ti,j7200-vtm)) which is just 'ti,j721e-vtm'. I think you want just the 'enum'.

Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml line 21

Quoted 3 lines
Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml new file +3 -0
+
+  reg:
+    maxItems: 3

What is each entry?

Quoted 9 lines
Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml new file +9 -0
+
+  power-domains:
+    maxItems: 1
+
+  "#thermal-sensor-cells":
+    const: 1
+
+required:
+  - compatible

Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml line 40

Quoted 10 lines
Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml new file +10 -0
+  - reg
+  - power-domains
+  - "#thermal-sensor-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/soc/ti,sci_pm_domain.h>
+    wkup_vtm0: wkup_vtm0@42040000 {

thermal-sensor@...

Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml line 49

Quoted 9 lines
Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml new file +9 -0
+        compatible = "ti,j721e-vtm";
+        reg = <0x42040000 0x350>,
+            <0x42050000 0x350>,
+            <0x43000300 0x10>;
+        power-domains = <&k3_pds 154 TI_SCI_PD_EXCLUSIVE>;
+        #thermal-sensor-cells = <1>;
+    };
+
+    mpu_thermal: mpu_thermal {

mpu-thermal

Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml line 55

Quoted 6 lines
Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml new file +6 -0
+        polling-delay-passive = <250>; /* milliseconds */
+        polling-delay = <500>; /* milliseconds */
+        thermal-sensors = <&wkup_vtm0 0>;
+
+        trips {
+                mpu_crit: mpu_crit {

mpu-crit

Quoted 11 lines
Documentation/devicetree/bindings/thermal/ti,j72xx-thermal.yaml new file +7 -0
+                        temperature = <125000>; /* milliCelsius */
+                        hysteresis = <2000>; /* milliCelsius */
+                        type = "critical";
+                };
+        };
+    };
+...
Signature · 1 line
2.17.1
author
#3 lore raw

PATCH v4 2/4 arm64: dts: ti: j721e: Add VTM node

In reply to Keerthy

VTM stands for Voltage Thermal Management

---
 .../boot/dts/ti/k3-j721e-mcu-wakeup.dtsi      |  9 +++
 arch/arm64/boot/dts/ti/k3-j721e-thermal.dtsi  | 73 +++++++++++++++++++
 arch/arm64/boot/dts/ti/k3-j721e.dtsi          |  4 +
 3 files changed, 86 insertions(+)
 create mode 100644 arch/arm64/boot/dts/ti/k3-j721e-thermal.dtsi
arch/arm64/boot/dts/ti/k3-j721e-mcu-wakeup.dtsi +9 -0
index d2dceda72fe9..fafc07848f86 100644
@@ -390,4 +390,13 @@
 			ti,loczrama = <1>;
 		};
 	};
+
+	wkup_vtm0: wkup_vtm0@42040000 {
+		compatible = "ti,j721e-vtm";
+		reg = <0x0 0x42040000 0x0 0x350>,
+			<0x0 0x42050000 0x0 0x350>,
+			<0x0 0x43000300 0x0 0x10>;
+		power-domains = <&k3_pds 154 TI_SCI_PD_EXCLUSIVE>;
+		#thermal-sensor-cells = <1>;
+	};
 };
arch/arm64/boot/dts/ti/k3-j721e-thermal.dtsi new file +73 -0

73 lines not on this page · raw message

arch/arm64/boot/dts/ti/k3-j721e.dtsi +4 -0
index f0587fde147e..20e26a359aaf 100644
@@ -177,6 +177,10 @@
 				 <0x07 0x00000000 0x07 0x00000000 0x01 0x00000000>; /* FSS OSPI1 data region 3*/
 		};
 	};
+
+	thermal_zones: thermal-zones {
+		#include "k3-j721e-thermal.dtsi"
+	};
 };
 
 /* Now include the peripherals for each bus segments */
Signature · 1 line
2.17.1
author
#4 lore raw

PATCH v4 3/4 arm64: dts: ti: j7200: Add VTM node

In reply to Keerthy

VTM stands for Voltage Thermal Management

---
 .../boot/dts/ti/k3-j7200-mcu-wakeup.dtsi      |  9 ++++
 arch/arm64/boot/dts/ti/k3-j7200-thermal.dtsi  | 45 +++++++++++++++++++
 arch/arm64/boot/dts/ti/k3-j7200.dtsi          |  4 ++
 3 files changed, 58 insertions(+)
 create mode 100644 arch/arm64/boot/dts/ti/k3-j7200-thermal.dtsi
arch/arm64/boot/dts/ti/k3-j7200-mcu-wakeup.dtsi +9 -0
index 1044ec6c4b0d..2b5c570253cc 100644
@@ -375,4 +375,13 @@
 			ti,loczrama = <1>;
 		};
 	};
+
+	wkup_vtm0: wkup_vtm0@42040000 {
+		compatible = "ti,j7200-vtm";
+		reg = <0x0 0x42040000 0x0 0x350>,
+			<0x0 0x42050000 0x0 0x350>,
+			<0x0 0x43000300 0x0 0x10>;
+		power-domains = <&k3_pds 154 TI_SCI_PD_EXCLUSIVE>;
+		#thermal-sensor-cells = <1>;
+	};
 };
arch/arm64/boot/dts/ti/k3-j7200-thermal.dtsi new file +45 -0

45 lines not on this page · raw message

arch/arm64/boot/dts/ti/k3-j7200.dtsi +4 -0
index b7005b803149..649d1d60f5c7 100644
@@ -165,6 +165,10 @@
 				 <0x07 0x00000000 0x07 0x00000000 0x01 0x00000000>; /* FSS OSPI1 data region 3 */
 		};
 	};
+
+	thermal_zones: thermal-zones {
+		#include "k3-j7200-thermal.dtsi"
+	};
 };
 
 /* Now include the peripherals for each bus segments */
Signature · 1 line
2.17.1
author
#5 lore raw

PATCH v4 4/4 thermal: k3_j72xx_bandgap: Add the bandgap driver support

In reply to Keerthy

Add VTM thermal support. In the Voltage Thermal Management Module(VTM), K3 J72XX supplies a voltage reference and a temperature sensor feature that are gathered in the band gap voltage and temperature sensor (VBGAPTS) module. The band gap provides current and voltage reference for its internal circuits and other analog IP blocks. The analog-to-digital converter (ADC) produces an output value that is proportional to the silicon temperature.

Currently reading temperatures only is supported. There are no active/passive cooling agent supported.

J721e SoCs have errata i2128: https://www.ti.com/lit/pdf/sprz455

The VTM Temperature Monitors (TEMPSENSORs) are trimmed during production, with the resulting values stored in software-readable registers. Software should use these register values when translating the Temperature Monitor output codes to temperature values.

It has an involved workaround. Software needs to read the error codes for -40C, 30C, 125C from the efuse for each device & derive a new look up table for adc to temperature conversion. Involved calculating slopes & constants using 3 different straight line equations with adc refernce codes as the y-axis & error codes in the x-axis.

-40C to 30C
30C to 125C
125C to 150C

With the above 2 line equations we derive the full look-up table to workaround the errata i2128 for j721e SoC.

Tested temperature reading on J721e SoC & J7200 SoC.

[[email protected]: Generate look-up tables run-time]

---

Changes in v4:

* Fixed warnings with w=1 during compilation.

Changes in v3:

* Removed static look up tables & added functions to dynamically generate them. Thanks Daniel! Saved 12KB of static data & almost 200 lines of driver code.

 drivers/thermal/Makefile           |   2 +-
 drivers/thermal/k3_j72xx_bandgap.c | 634 +++++++++++++++++++++++++++++
 2 files changed, 635 insertions(+), 1 deletion(-)
 create mode 100644 drivers/thermal/k3_j72xx_bandgap.c
drivers/thermal/Makefile +1 -1
index 82fc3e616e54..6dcaf2028ddc 100644
@@ -28,7 +28,7 @@ thermal_sys-$(CONFIG_CPU_IDLE_THERMAL)	+= cpuidle_cooling.o
 # devfreq cooling
 thermal_sys-$(CONFIG_DEVFREQ_THERMAL) += devfreq_cooling.o
 
-obj-$(CONFIG_K3_THERMAL)	+= k3_bandgap.o
+obj-$(CONFIG_K3_THERMAL)	+= k3_bandgap.o k3_j72xx_bandgap.o
 # platform thermal drivers
 obj-y				+= broadcom/
 obj-$(CONFIG_THERMAL_MMIO)		+= thermal_mmio.o
drivers/thermal/k3_j72xx_bandgap.c new file +634 -0

634 lines not on this page · raw message

Signature · 1 line
2.17.1
#6 lore raw
Quoted 24 lines · Keerthy

Add VTM thermal support. In the Voltage Thermal Management Module(VTM), K3 J72XX supplies a voltage reference and a temperature sensor feature that are gathered in the band gap voltage and temperature sensor (VBGAPTS) module. The band gap provides current and voltage reference for its internal circuits and other analog IP blocks. The analog-to-digital converter (ADC) produces an output value that is proportional to the silicon temperature.

Currently reading temperatures only is supported. There are no active/passive cooling agent supported.

J721e SoCs have errata i2128: https://www.ti.com/lit/pdf/sprz455

The VTM Temperature Monitors (TEMPSENSORs) are trimmed during production, with the resulting values stored in software-readable registers. Software should use these register values when translating the Temperature Monitor output codes to temperature values.

It has an involved workaround. Software needs to read the error codes for -40C, 30C, 125C from the efuse for each device & derive a new look up table for adc to temperature conversion. Involved calculating slopes & constants using 3 different straight line equations with adc refernce codes as the y-axis & error codes in the x-axis.

Quoted 8 lines

-40C to 30C
30C to 125C
125C to 150C

With the above 2 line equations we derive the full look-up table to workaround the errata i2128 for j721e SoC.

Tested temperature reading on J721e SoC & J7200 SoC.

I made a bunch of comments, but this driver needs some reworking to be simplified.

The driver is very similar to the k3_bandgap.c and we found exactly the same table+3values-avg approach. After simplifying this driver, it should merge easily with k3_bandgap.c.

Quoted 82 lines
[[email protected]: Generate look-up tables run-time]

---

Changes in v4:

* Fixed warnings with w=1 during compilation.

Changes in v3:

* Removed static look up tables & added functions to dynamically generate them. Thanks Daniel! Saved 12KB of static data & almost 200 lines of driver code.

 drivers/thermal/Makefile           |   2 +-
 drivers/thermal/k3_j72xx_bandgap.c | 634 +++++++++++++++++++++++++++++
 2 files changed, 635 insertions(+), 1 deletion(-)
 create mode 100644 drivers/thermal/k3_j72xx_bandgap.c
drivers/thermal/Makefile +1 -1
index 82fc3e616e54..6dcaf2028ddc 100644
@@ -28,7 +28,7 @@ thermal_sys-$(CONFIG_CPU_IDLE_THERMAL)	+= cpuidle_cooling.o
 # devfreq cooling
 thermal_sys-$(CONFIG_DEVFREQ_THERMAL) += devfreq_cooling.o
 
-obj-$(CONFIG_K3_THERMAL)	+= k3_bandgap.o
+obj-$(CONFIG_K3_THERMAL)	+= k3_bandgap.o k3_j72xx_bandgap.o
 # platform thermal drivers
 obj-y				+= broadcom/
 obj-$(CONFIG_THERMAL_MMIO)		+= thermal_mmio.o
drivers/thermal/k3_j72xx_bandgap.c new file +43 -0
new file mode 100644
index 000000000000..3d3786f5058d
@@ -0,0 +1,634 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * TI Bandgap temperature sensor driver for J72XX SoC Family
+ *
+ * Copyright (C) 2021 Texas Instruments Incorporated - http://www.ti.com/
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/pm_runtime.h>
+#include <linux/err.h>
+#include <linux/types.h>
+#include <linux/spinlock.h>
+#include <linux/of_platform.h>
+#include <linux/io.h>
+#include <linux/thermal.h>
+#include <linux/of.h>
+#include <linux/delay.h>
+#include <linux/math.h>
+#include <linux/slab.h>
+
+#define K3_VTM_DEVINFO_PWR0_OFFSET		0x4
+#define K3_VTM_DEVINFO_PWR0_CVD_CT_MASK		0xf
+#define K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK	0xf0
+#define K3_VTM_TMPSENS0_CTRL_OFFSET		0x300
+#define K3_VTM_MISC_CTRL_OFFSET			0xc
+#define K3_VTM_TMPSENS_STAT_OFFSET		0x8
+#define K3_VTM_ANYMAXT_OUTRG_ALERT_EN		0x1
+#define K3_VTM_MISC_CTRL2_OFFSET		0x10
+#define K3_VTM_REGS_PER_TS			0x10
+#define K3_VTM_TS_STAT_DTEMP_MASK		0x3ff
+#define K3_VTM_MAX_NUM_TS			8
+#define K3_VTM_TMPSENS_CTRL_CBIASSEL		BIT(0)
+#define K3_VTM_TMPSENS_CTRL_SOC			BIT(5)
+#define K3_VTM_TMPSENS_CTRL_CLRZ		BIT(6)
+#define K3_VTM_TMPSENS_CTRL_CLKON_REQ		BIT(7)
+#define K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN	BIT(11)
+
+#define K3_VTM_CORRECTION_TEMP_CNT		3
+
+#define K3_VTM_ADC_BEGIN_VAL			6
+#define K3_VTM_ADC_END_VAL			944

drivers/thermal/k3_j72xx_bandgap.c line 53

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+
+#define MINUS40CREF				5
+#define PLUS30CREF				253
+#define PLUS125CREF				730
+#define PLUS150CREF				940
+
+#define MAX_TEMP				123000
+#define COOL_DOWN_TEMP				105000
+
+//#define DEBUG_VTM

I suggest to remove DEBUG_VTM and replace the dev_info by dev_dbg in the print lookup table. There are also messages to reduce the verbosity level to debug (commented inline).

drivers/thermal/k3_j72xx_bandgap.c line 56

Quoted 3 lines
drivers/thermal/k3_j72xx_bandgap.c new file +3 -0
+
+static int *ref_table;
+static int *derived_table;

These tables could be passed around via function parameters and their life cycle is just during the probe. No need to declare them globally.

Quoted 18 lines
drivers/thermal/k3_j72xx_bandgap.c new file +18 -0
+
+s64 golden_factors[] = {
+	-490019999999999936,
+	3251200000000000,
+	-1705800000000,
+	603730000,
+	-92627,
+};
+
+s64 pvt_wa_factors[] = {
+	-415230000000000000,
+	3126600000000000,
+	-1157800000000,
+};
+
+static s64 int_power(unsigned long long base, unsigned int exp)
+{
+	s64 result = 1;

drivers/thermal/k3_j72xx_bandgap.c line 84

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+
+	while (exp) {
+		if (exp & 1)
+			result *= base;
+		exp >>= 1;
+		base *= base;
+	}
+
+	return result;
+}

This function is already implemented.

include/linux/math.h:u64 int_pow(u64 base, unsigned int exp);

drivers/thermal/k3_j72xx_bandgap.c line 97

Quoted 12 lines
drivers/thermal/k3_j72xx_bandgap.c new file +12 -0
+static int compute_value(int index, s64 *factors, int nr_factors, int reduction)
+{
+	s64 value = 0;
+	int i;
+
+	for (i = 0; i < nr_factors; i++)
+		value += factors[i] * int_power(index, i);
+
+	return (int)(value / int_power(10, reduction));
+}
+
+static void init_table(bool wa, int *mytable)

s/mytable/table/

Instead of passing a boolean, let the caller pass the right parameters, that makes the code nicer.

static void init_table(int *table, int size, s64 *factors, int nr_factors)

{
	int i;

	for (i = 0; i < size; i++)
		table[i] = polynomial(factors, nr_factors,
					FACTORS_REDUCTION);
}
Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+{
+	s64 *factors;
+	const int size = 1024;
+	int factors_size;
+	int i;
+
+	if (wa) {
+		factors_size = 3;
+		factors = pvt_wa_factors;
+	} else {

drivers/thermal/k3_j72xx_bandgap.c line 117

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+		factors_size = 5;
+		factors = golden_factors;
+	}
+
+	for (i = 0; i < size; i++)
+		mytable[i] = compute_value(i, factors, factors_size, 13);
+}
+
+static void fill_table(int err1, int err2, int ref1, int ref2, int idx1,
+		       int idx2)

I suggest to create a self explanatory structure and pass a couple of them as parameter with the derived and ref tables.

Given what does this function, it would make sense rename it to something more explicit along with a short explanation of why this is needed.

drivers/thermal/k3_j72xx_bandgap.c line 121

Quoted 4 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+{
+	int m, c, num, den, i, err;
+
+	/**

'/**' is for kernel documentation, should be replaced by /*

drivers/thermal/k3_j72xx_bandgap.c line 128

Quoted 7 lines
drivers/thermal/k3_j72xx_bandgap.c new file +7 -0
+	 * Calculate the slope with adc values read from the register
+	 * as the y-axis param and err in adc value as x-axis param
+	 */
+	if (err1 != err2) {
+		num = ref2 - ref1;
+		den = err2 - err1;
+		m = num / den;

if den > num then 'm' is zero and err = (i - c) / m will crash if ref1 and ref2 are equal, 'm' will be zero also

drivers/thermal/k3_j72xx_bandgap.c line 137

Quoted 9 lines
drivers/thermal/k3_j72xx_bandgap.c new file +9 -0
+		c = ref2 - m * err2;
+		for (i = idx1; i <= idx2; i++) {
+			err = (i - c) / m;
+			if (((i + err1) < 0) || ((i + err1) > 1023))
+				continue;
+			derived_table[i] = ref_table[i + err];
+		}
+	} else { /* Constant error take care of divide by zero */
+		for (i = idx1; i < idx2; i++) {

Why the first loop has i<=idx2 and this one is i<idx2 ?

drivers/thermal/k3_j72xx_bandgap.c line 141

Quoted 4 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+			if (((i + err1) < 0) || ((i + err1) > 1023))
+				continue;
+			derived_table[i] = ref_table[i + err1];
+		}

Can be this loop put outside of the if the else ?

Quoted 12 lines
drivers/thermal/k3_j72xx_bandgap.c new file +12 -0
+	}
+}
+
+static int prep_lookup_table(int minus40cerr, int plus30cerr, int plus125cerr,
+			     int plus150cerr)
+{
+	int inc, i;
+
+	/* Fill up the lookup table region -40C to +30C */
+	fill_table(minus40cerr, plus30cerr, MINUS40CREF, PLUS30CREF, 0,
+		   PLUS30CREF);
+	/* Fill up the lookup table region +30C to +125C */

drivers/thermal/k3_j72xx_bandgap.c line 163

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+	fill_table(plus30cerr, plus125cerr, PLUS30CREF, PLUS125CREF, PLUS30CREF,
+		   PLUS125CREF);
+	/* Fill up the lookup table region +125C to +150C */
+	fill_table(plus125cerr, plus150cerr, PLUS125CREF, PLUS150CREF, PLUS125CREF,
+		   PLUS150CREF);
+
+	/* Get to the first valid temperature */
+	i = 0;
+	while (!derived_table[i])
+		i++;

zero is not a valid temperature ?

Quoted 8 lines
drivers/thermal/k3_j72xx_bandgap.c new file +8 -0
+
+	/*
+	 * Get to the last zero index and back fill the temperature for
+	 * sake of continuity
+	 */
+	if (i) {
+		/* 300 milli celsius steps */
+		while (i--)

drivers/thermal/k3_j72xx_bandgap.c line 181

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+			derived_table[i] = derived_table[i + 1] - 300;
+		/* case 0 */
+		derived_table[i] = derived_table[i + 1] - 300;
+	}
+
+	/*
+	 * Fill the last trailing 0s which are unfilled with increments of
+	 * 100 milli celsius till 1023 code
+	 */
+	i = 1023;

Please, give the table and const table size in parameters.

drivers/thermal/k3_j72xx_bandgap.c line 190

Quoted 9 lines
drivers/thermal/k3_j72xx_bandgap.c new file +9 -0
+	while (!derived_table[i])
+		i--;
+
+	i++;
+	inc = 1;
+	while (i < 1024) {
+		derived_table[i] = derived_table[i - 1] + inc * 100;
+		i++;
+	}

Can you explain why the above is done ?

drivers/thermal/k3_j72xx_bandgap.c line 195

Quoted 4 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+	return 0;
+}
+
+struct k3_thermal_data;

not necessary

Below, "not necessary" == the field is assigned but then never used, "not used" == the field is never used at all.

drivers/thermal/k3_j72xx_bandgap.c line 198

Quoted 2 lines
drivers/thermal/k3_j72xx_bandgap.c new file +2 -0
+struct k3_j72xx_bandgap {
+	struct device *dev;

not necessary

drivers/thermal/k3_j72xx_bandgap.c line 203

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+	void __iomem *base;
+	void __iomem *cfg2_base;
+	void __iomem *fuse_base;
+	const struct k3_j72xx_bandgap_data	*conf;
+	spinlock_t lock; /* shields this struct */

not used

drivers/thermal/k3_j72xx_bandgap.c line 204

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int ts_cnt;

not used

drivers/thermal/k3_j72xx_bandgap.c line 205

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	struct k3_thermal_data *ts_data[K3_VTM_MAX_NUM_TS];

not used

drivers/thermal/k3_j72xx_bandgap.c line 210

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+};
+
+/* common data structures */
+struct k3_thermal_data {
+	struct thermal_zone_device *ti_thermal;

not necessary

drivers/thermal/k3_j72xx_bandgap.c line 211

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	struct thermal_cooling_device *cool_dev;

not used

drivers/thermal/k3_j72xx_bandgap.c line 213

Quoted 2 lines
drivers/thermal/k3_j72xx_bandgap.c new file +2 -0
+	struct k3_j72xx_bandgap *bgp;
+	enum thermal_device_mode mode;

not used

drivers/thermal/k3_j72xx_bandgap.c line 214

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int sensor_id;

not used

drivers/thermal/k3_j72xx_bandgap.c line 217

Quoted 3 lines
drivers/thermal/k3_j72xx_bandgap.c new file +3 -0
+	u32 ctrl_offset;
+	u32 stat_offset;
+	int prev_temp;

not necessary

drivers/thermal/k3_j72xx_bandgap.c line 219

Quoted 2 lines
drivers/thermal/k3_j72xx_bandgap.c new file +2 -0
+	int ct_offsets[K3_VTM_CORRECTION_TEMP_CNT];
+	int ct_bm[K3_VTM_CORRECTION_TEMP_CNT];

Both are only local to the function using it

drivers/thermal/k3_j72xx_bandgap.c line 220

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int ref_adc_val[3];

not used

drivers/thermal/k3_j72xx_bandgap.c line 221

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int act_adc_val[3];

not used

drivers/thermal/k3_j72xx_bandgap.c line 222

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int slope[2];

not used

drivers/thermal/k3_j72xx_bandgap.c line 223

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int coeff[2];

not used

drivers/thermal/k3_j72xx_bandgap.c line 234

Quoted 11 lines
drivers/thermal/k3_j72xx_bandgap.c new file +11 -0
+};
+
+static int two_cmp(int tmp, int mask)
+{
+	tmp = ~(tmp);
+	tmp &= mask;
+	tmp += 1;
+
+	/* Return negative value */
+	return (0 - tmp);
+}

Can you explain ?

Quoted 50 lines
drivers/thermal/k3_j72xx_bandgap.c new file +50 -0
+
+static unsigned int vtm_get_best_value(unsigned int s0, unsigned int s1,
+				       unsigned int s2)
+{
+	int d01 = abs(s0 - s1);
+	int d02 = abs(s0 - s2);
+	int d12 = abs(s1 - s2);
+
+	if (d01 <= d02 && d01 <= d12)
+		return (s0 + s1) / 2;
+
+	if (d02 <= d01 && d02 <= d12)
+		return (s0 + s2) / 2;
+
+	return (s1 + s2) / 2;
+}
+
+static inline int k3_bgp_read_temp(struct k3_thermal_data *devdata,
+				   int *temp)
+{
+	struct k3_j72xx_bandgap *bgp;
+	unsigned int dtemp, s0, s1, s2;
+
+	bgp = devdata->bgp;
+	/*
+	 * Errata is applicable for am654 pg 1.0 silicon/J7ES. There
+	 * is a variation of the order for certain degree centigrade on AM654.
+	 * Work around that by getting the average of two closest
+	 * readings out of three readings everytime we want to
+	 * report temperatures.
+	 *
+	 * Errata workaround.
+	 */
+	s0 = readl(bgp->base + devdata->stat_offset) &
+		K3_VTM_TS_STAT_DTEMP_MASK;
+	s1 = readl(bgp->base + devdata->stat_offset) &
+		K3_VTM_TS_STAT_DTEMP_MASK;
+	s2 = readl(bgp->base + devdata->stat_offset) &
+		K3_VTM_TS_STAT_DTEMP_MASK;
+	dtemp = vtm_get_best_value(s0, s1, s2);
+
+	if (dtemp < 0 || dtemp > 1023)
+		return -EINVAL;
+
+	*temp = derived_table[dtemp];
+
+	return 0;
+}
+
+/* Get temperature callback function for thermal zone */

drivers/thermal/k3_j72xx_bandgap.c line 294

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+static int k3_thermal_get_temp(void *devdata, int *temp)
+{
+	struct k3_thermal_data *data = devdata;
+	int ret = 0;
+
+	ret = k3_bgp_read_temp(data, temp);
+	if (ret)
+		return ret;
+
+	data->prev_temp = *temp;

where is it used ?

Quoted 4 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+
+	return ret;
+}
+

drivers/thermal/k3_j72xx_bandgap.c line 308

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+static const struct thermal_zone_of_device_ops k3_of_thermal_ops = {
+	.get_temp = k3_thermal_get_temp,
+};
+
+static int k3_j72xx_bandgap_temp_to_adc_code(int temp)
+{
+	int low = 0, high = 1023, mid;
+
+	if (temp > 160000 || temp < -50000)
+		return -EINVAL;

How the caller does the difference between a negative temperature and this error ?

Quoted 84 lines
drivers/thermal/k3_j72xx_bandgap.c new file +84 -0
+	/* Binary search to find the adc code */
+	while (low < (high - 1)) {
+		mid = (low + high) / 2;
+		if (temp <= derived_table[mid])
+			high = mid;
+		else
+			low = mid;
+	}
+
+	return mid;
+}
+
+static void get_efuse_values(int id, struct k3_thermal_data *data, int *err,
+			     struct k3_j72xx_bandgap *bgp)
+{
+	int i, tmp, pow;
+
+	/* Populate efuse reg offsets & Bit masks for -40C, 30C, 125C */
+	switch (id) {
+	case 0:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = 0x8;
+		data->ct_offsets[2] = 0x4;
+		data->ct_bm[0] = 0x3f;
+		data->ct_bm[1] = 0x1fe000;
+		data->ct_bm[2] = 0x1ff;
+		break;
+
+	case 1:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = 0x8;
+		data->ct_offsets[2] = 0x4;
+		data->ct_bm[0] = 0xfc0;
+		data->ct_bm[1] = 0x1fe00000;
+		data->ct_bm[2] = 0x3fe00;
+		break;
+
+	case 2:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = -1;
+		data->ct_offsets[2] = 0x4;
+		data->ct_bm[0] = 0x3f000;
+		data->ct_bm[1] = 0x7f800000;
+		data->ct_bm[2] = 0x7fc0000;
+		break;
+
+	case 3:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = 0xC;
+		data->ct_offsets[2] = -1; /* Spread across 2 registers */
+		data->ct_bm[0] = 0xfc0000;
+		data->ct_bm[1] = 0x1fe0;
+		data->ct_bm[2] = 0x1f800000;
+		break;
+
+	case 4:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = 0xc;
+		data->ct_offsets[2] = 0x8;
+		data->ct_bm[0] = 0x3f000000;
+		data->ct_bm[1] = 0x1fe000;
+		data->ct_bm[2] = 0x1ff0;
+		break;
+	}
+
+	for (i = 0; i < 3; i++) {
+		/* Extract the offset value using bit-mask */
+		if (data->ct_offsets[i] == -1 && i == 1) {
+			/* 25C offset Case of Sensor 2 split between 2 regs */
+			tmp = (readl(bgp->fuse_base + 0x8) & 0xE0000000) >> (29);
+			tmp |= ((readl(bgp->fuse_base + 0xC) & 0x1F) << 3);
+			pow = tmp & 0x80;
+		} else if (data->ct_offsets[i] == -1 && i == 2) {
+			/* 125C Case of Sensor 3 split between 2 regs */
+			tmp = (readl(bgp->fuse_base + 0x4) & 0xF8000000) >> (27);
+			tmp |= ((readl(bgp->fuse_base + 0x8) & 0xF) << 5);
+			pow = tmp & 0x100;
+		} else {
+			tmp = readl(bgp->fuse_base + data->ct_offsets[i]);
+			tmp &= data->ct_bm[i];
+			tmp = tmp >> __ffs(data->ct_bm[i]);
+
+			/* Obtain the sign bit pow*/
+			pow = data->ct_bm[i] >> __ffs(data->ct_bm[i]);

drivers/thermal/k3_j72xx_bandgap.c line 403

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+			pow += 1;
+			pow /= 2;
+		}
+
+		/* Check for negative value */
+		if (tmp & pow) {
+			/* 2's complement value */
+			tmp = two_cmp(tmp, data->ct_bm[i] >> __ffs(data->ct_bm[i]));
+		}
+		err[i] = tmp;

Can you explain what is doing the code above ?

drivers/thermal/k3_j72xx_bandgap.c line 407

Quoted 4 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+	}
+}
+
+#ifdef DEBUG_VTM

Remove

drivers/thermal/k3_j72xx_bandgap.c line 412

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+static void print_look_up_table(struct device *dev)
+{
+	int i;
+
+	dev_info(dev, "The contents of derived array\n");

dev_dbg

drivers/thermal/k3_j72xx_bandgap.c line 413

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	dev_info(dev, "Code   Temperaturei\n");

dev_dbg

drivers/thermal/k3_j72xx_bandgap.c line 415

Quoted 2 lines
drivers/thermal/k3_j72xx_bandgap.c new file +2 -0
+	for (i = 0; i <= 1023; i++)
+		dev_info(dev, "%d       %d\n", i, derived_table[i]);

dev_dbg

drivers/thermal/k3_j72xx_bandgap.c line 421

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+}
+#endif
+
+static const struct of_device_id of_k3_j72xx_bandgap_match[];
+
+struct k3_j72xx_bandgap_j72xx_data {

Fix name please

drivers/thermal/k3_j72xx_bandgap.c line 422

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	unsigned int	workaround;

s/workaround/has_errata_<num>/

Quoted 29 lines
drivers/thermal/k3_j72xx_bandgap.c new file +29 -0
+};
+
+static int k3_j72xx_bandgap_probe(struct platform_device *pdev)
+{
+	int ret = 0, cnt, val, id, reg_cnt = 0, j, table_size = 1024;
+	int err[3], high_max, low_max;
+	struct resource *res;
+	struct device *dev = &pdev->dev;
+	struct k3_j72xx_bandgap *bgp;
+	struct k3_thermal_data *data;
+	int workaround_needed = 0;
+	const struct of_device_id *of_id;
+	const struct k3_j72xx_bandgap_j72xx_data *driver_data;
+
+	bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
+	if (!bgp)
+		return -ENOMEM;
+
+	bgp->dev = dev;
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	bgp->base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(bgp->base))
+		return PTR_ERR(bgp->base);
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	bgp->cfg2_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(bgp->cfg2_base))
+		return PTR_ERR(bgp->cfg2_base);
+

drivers/thermal/k3_j72xx_bandgap.c line 461

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
+	bgp->fuse_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(bgp->fuse_base))
+		return PTR_ERR(bgp->fuse_base);
+
+	of_id = of_match_device(of_k3_j72xx_bandgap_match, &pdev->dev);
+	if (of_id) {
+		driver_data = of_id->data;
+		workaround_needed = driver_data->workaround;
+	}

of_device_get_match_data(dev) ?

Quoted 27 lines
drivers/thermal/k3_j72xx_bandgap.c new file +27 -0
+	pm_runtime_enable(dev);
+	ret = pm_runtime_get_sync(dev);
+	if (ret < 0) {
+		pm_runtime_put_noidle(dev);
+		pm_runtime_disable(dev);
+		return ret;
+	}
+
+	/* Get the sensor count in the VTM */
+	val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET);
+	cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK;
+	cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK);
+	bgp->ts_cnt = cnt;
+
+	data = devm_kcalloc(bgp->dev, cnt, sizeof(*data), GFP_KERNEL);
+	if (!data) {
+		ret = -ENOMEM;
+		goto err_alloc;
+	}
+
+	ref_table = devm_kzalloc(bgp->dev, sizeof(*ref_table) * table_size,
+				 GFP_KERNEL);
+	if (!ref_table) {
+		ret = -ENOMEM;
+		goto err_alloc;
+	}
+

drivers/thermal/k3_j72xx_bandgap.c line 499

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+	derived_table = devm_kzalloc(bgp->dev, sizeof(*derived_table) * table_size,
+				     GFP_KERNEL);
+	if (!derived_table) {
+		ret = -ENOMEM;
+		goto err_alloc;
+	}
+
+	/* Workaround not needed if bit30/bit31 is set even for J721e */
+	if (workaround_needed && (readl(bgp->fuse_base + 0x0) & 0xc0000000) == 0xc0000000)
+		workaround_needed = false;

Does it mean the same hardware may have or not the errata? Is there a way to distinguish them from the compatible string ?

drivers/thermal/k3_j72xx_bandgap.c line 507

Quoted 8 lines
drivers/thermal/k3_j72xx_bandgap.c new file +8 -0
+
+	if (!workaround_needed) {
+		dev_info(bgp->dev, "work around NOT needed!!\n");
+		init_table(false, ref_table);
+	} else {
+		dev_info(bgp->dev, "work around needed!!\n");
+		init_table(true, ref_table);
+	}
	dev_dbg(bgp->dev, "Work around %sneeded\n",
		workaround_needed ? "not " : "");

drivers/thermal/k3_j72xx_bandgap.c line 518

Quoted 11 lines
drivers/thermal/k3_j72xx_bandgap.c new file +11 -0
+
+	/* Register the thermal sensors */
+	for (id = 0; id < cnt; id++) {
+		data[id].sensor_id = id;
+		data[id].bgp = bgp;
+		data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET + id * 0x20;
+		data[id].stat_offset = data[id].ctrl_offset +
+					K3_VTM_TMPSENS_STAT_OFFSET;
+
+		if (!workaround_needed)
+			goto prepare_lookup;
		if (workaround_needed) {

drivers/thermal/k3_j72xx_bandgap.c line 523

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+		/* ref adc values for -40C, 30C & 125C respectively */
+		data[id].ref_adc_val[0] = MINUS40CREF;
+		data[id].ref_adc_val[1] = PLUS30CREF;
+		data[id].ref_adc_val[2] = PLUS125CREF;
+		get_efuse_values(id, &data[id], err, bgp);
		}

drivers/thermal/k3_j72xx_bandgap.c line 530

Quoted 7 lines
drivers/thermal/k3_j72xx_bandgap.c new file +7 -0
+
+prepare_lookup:
+		if (id == 0 && workaround_needed) {
+			prep_lookup_table(err[0], err[1], err[2], 0);
+		} else if (id == 0 && !workaround_needed) {
+			for (j = 0; j < table_size; j++)
+				derived_table[j] = ref_table[j];

memcpy ?

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+		}
+
+		val = readl(data[id].bgp->cfg2_base + data[id].ctrl_offset);
+		val |= (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN |
+			K3_VTM_TMPSENS_CTRL_SOC |
+			K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4));
+		writel(val, data[id].bgp->cfg2_base + data[id].ctrl_offset);
+
+		bgp->ts_data[id] = &data[id];
+		data[id].ti_thermal =

drivers/thermal/k3_j72xx_bandgap.c line 550

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+		devm_thermal_zone_of_sensor_register(bgp->dev, id,
+						     &data[id],
+						     &k3_of_thermal_ops);
+		if (IS_ERR(data[id].ti_thermal)) {
+			dev_err(bgp->dev, "thermal zone device is NULL\n");
+			ret = PTR_ERR(data[id].ti_thermal);
+			goto err_alloc;
+		}
+
+		reg_cnt++;

not used

drivers/thermal/k3_j72xx_bandgap.c line 553

Quoted 3 lines
drivers/thermal/k3_j72xx_bandgap.c new file +3 -0
+
+		/* Initialize Previous temp */
+		k3_thermal_get_temp(&data[id], &data[id].prev_temp);

why ? where is used prev_temp ?

drivers/thermal/k3_j72xx_bandgap.c line 564

Quoted 11 lines
drivers/thermal/k3_j72xx_bandgap.c new file +11 -0
+	}
+
+	/*
+	 * Program TSHUT thresholds
+	 * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2
+	 * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN  bit
+	 *         This is already taken care as per of init
+	 * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN  bit
+	 */
+	high_max = k3_j72xx_bandgap_temp_to_adc_code(MAX_TEMP);
+	low_max = k3_j72xx_bandgap_temp_to_adc_code(COOL_DOWN_TEMP);

I guess high_max is the hardware shutdown temperature, right ?

But what is low_max ?

drivers/thermal/k3_j72xx_bandgap.c line 574

Quoted 9 lines
drivers/thermal/k3_j72xx_bandgap.c new file +9 -0
+	writel((low_max << 16) | high_max, data[0].bgp->cfg2_base +
+	       K3_VTM_MISC_CTRL2_OFFSET);
+	mdelay(100);
+	writel(K3_VTM_ANYMAXT_OUTRG_ALERT_EN, data[0].bgp->cfg2_base +
+	       K3_VTM_MISC_CTRL_OFFSET);
+
+	platform_set_drvdata(pdev, bgp);
+
+#ifdef DEBUG_VTM

Remove ifdef

Quoted 61 lines
drivers/thermal/k3_j72xx_bandgap.c new file +60 -0
+	print_look_up_table(dev);
+#endif
+	/*
+	 * Now that the derived_table has the appropriate look up values
+	 * Free up the ref_table
+	 */
+	kfree(ref_table);
+
+	return 0;
+
+err_alloc:
+	pm_runtime_put_sync(&pdev->dev);
+	pm_runtime_disable(&pdev->dev);
+
+	return ret;
+}
+
+static int k3_j72xx_bandgap_remove(struct platform_device *pdev)
+{
+	pm_runtime_put_sync(&pdev->dev);
+	pm_runtime_disable(&pdev->dev);
+
+	return 0;
+}
+
+const struct k3_j72xx_bandgap_j72xx_data k3_j72xx_bandgap_j721e_data = {
+	.workaround = 1,
+};
+
+const struct k3_j72xx_bandgap_j72xx_data k3_j72xx_bandgap_j7200_data = {
+	.workaround = 0,
+};
+
+static const struct of_device_id of_k3_j72xx_bandgap_match[] = {
+	{
+		.compatible = "ti,j721e-vtm",
+		.data = (void *)&k3_j72xx_bandgap_j721e_data,
+	},
+	{
+		.compatible = "ti,j7200-vtm",
+		.data = (void *)&k3_j72xx_bandgap_j7200_data,
+	},
+	{ /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, of_k3_j72xx_bandgap_match);
+
+static struct platform_driver k3_j72xx_bandgap_sensor_driver = {
+	.probe = k3_j72xx_bandgap_probe,
+	.remove = k3_j72xx_bandgap_remove,
+	.driver = {
+		.name = "k3-j72xx-soc-thermal",
+		.of_match_table	= of_k3_j72xx_bandgap_match,
+	},
+};
+
+module_platform_driver(k3_j72xx_bandgap_sensor_driver);
+
+MODULE_DESCRIPTION("K3 bandgap temperature sensor driver");
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("J Keerthy <[email protected]>");
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
author
#7 lore raw
Quoted 32 lines · Daniel Lezcano
Quoted 31 lines · Keerthy

Add VTM thermal support. In the Voltage Thermal Management Module(VTM), K3 J72XX supplies a voltage reference and a temperature sensor feature that are gathered in the band gap voltage and temperature sensor (VBGAPTS) module. The band gap provides current and voltage reference for its internal circuits and other analog IP blocks. The analog-to-digital converter (ADC) produces an output value that is proportional to the silicon temperature.

Currently reading temperatures only is supported. There are no active/passive cooling agent supported.

J721e SoCs have errata i2128: https://www.ti.com/lit/pdf/sprz455

The VTM Temperature Monitors (TEMPSENSORs) are trimmed during production, with the resulting values stored in software-readable registers. Software should use these register values when translating the Temperature Monitor output codes to temperature values.

It has an involved workaround. Software needs to read the error codes for -40C, 30C, 125C from the efuse for each device & derive a new look up table for adc to temperature conversion. Involved calculating slopes & constants using 3 different straight line equations with adc refernce codes as the y-axis & error codes in the x-axis.

-40C to 30C
30C to 125C
125C to 150C

With the above 2 line equations we derive the full look-up table to workaround the errata i2128 for j721e SoC.

Quoted 8 lines
Quoted 1 line

Tested temperature reading on J721e SoC & J7200 SoC.

I made a bunch of comments, but this driver needs some reworking to be simplified.

The driver is very similar to the k3_bandgap.c and we found exactly the same table+3values-avg approach. After simplifying this driver, it should merge easily with k3_bandgap.c.

k3_bandgap.c was my first choice as well but did not want to clutter that up with a newer errata implementation. Hence chose to have a new driver for j72xx. I am okay either ways.

Quoted 89 lines
Quoted 89 lines
[[email protected]: Generate look-up tables run-time]

---

Changes in v4:

* Fixed warnings with w=1 during compilation.

Changes in v3:

* Removed static look up tables & added functions to dynamically generate them. Thanks Daniel! Saved 12KB of static data & almost 200 lines of driver code.

  drivers/thermal/Makefile           |   2 +-
  drivers/thermal/k3_j72xx_bandgap.c | 634 +++++++++++++++++++++++++++++
  2 files changed, 635 insertions(+), 1 deletion(-)

create mode 100644 drivers/thermal/k3_j72xx_bandgap.c

drivers/thermal/Makefile +1 -1
index 82fc3e616e54..6dcaf2028ddc 100644
@@ -28,7 +28,7 @@ thermal_sys-$(CONFIG_CPU_IDLE_THERMAL)	+= cpuidle_cooling.o
  # devfreq cooling
  thermal_sys-$(CONFIG_DEVFREQ_THERMAL) += devfreq_cooling.o
 
-obj-$(CONFIG_K3_THERMAL)	+= k3_bandgap.o
+obj-$(CONFIG_K3_THERMAL)	+= k3_bandgap.o k3_j72xx_bandgap.o
  # platform thermal drivers
  obj-y				+= broadcom/
  obj-$(CONFIG_THERMAL_MMIO)		+= thermal_mmio.o
drivers/thermal/k3_j72xx_bandgap.c new file +49 -0
new file mode 100644
index 000000000000..3d3786f5058d
@@ -0,0 +1,634 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * TI Bandgap temperature sensor driver for J72XX SoC Family
+ *
+ * Copyright (C) 2021 Texas Instruments Incorporated - http://www.ti.com/
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/pm_runtime.h>
+#include <linux/err.h>
+#include <linux/types.h>
+#include <linux/spinlock.h>
+#include <linux/of_platform.h>
+#include <linux/io.h>
+#include <linux/thermal.h>
+#include <linux/of.h>
+#include <linux/delay.h>
+#include <linux/math.h>
+#include <linux/slab.h>
+
+#define K3_VTM_DEVINFO_PWR0_OFFSET		0x4
+#define K3_VTM_DEVINFO_PWR0_CVD_CT_MASK		0xf
+#define K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK	0xf0
+#define K3_VTM_TMPSENS0_CTRL_OFFSET		0x300
+#define K3_VTM_MISC_CTRL_OFFSET			0xc
+#define K3_VTM_TMPSENS_STAT_OFFSET		0x8
+#define K3_VTM_ANYMAXT_OUTRG_ALERT_EN		0x1
+#define K3_VTM_MISC_CTRL2_OFFSET		0x10
+#define K3_VTM_REGS_PER_TS			0x10
+#define K3_VTM_TS_STAT_DTEMP_MASK		0x3ff
+#define K3_VTM_MAX_NUM_TS			8
+#define K3_VTM_TMPSENS_CTRL_CBIASSEL		BIT(0)
+#define K3_VTM_TMPSENS_CTRL_SOC			BIT(5)
+#define K3_VTM_TMPSENS_CTRL_CLRZ		BIT(6)
+#define K3_VTM_TMPSENS_CTRL_CLKON_REQ		BIT(7)
+#define K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN	BIT(11)
+
+#define K3_VTM_CORRECTION_TEMP_CNT		3
+
+#define K3_VTM_ADC_BEGIN_VAL			6
+#define K3_VTM_ADC_END_VAL			944
+
+#define MINUS40CREF				5
+#define PLUS30CREF				253
+#define PLUS125CREF				730
+#define PLUS150CREF				940
+
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 53

Quoted 4 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+#define MAX_TEMP				123000
+#define COOL_DOWN_TEMP				105000
+
+//#define DEBUG_VTM

I suggest to remove DEBUG_VTM and replace the dev_info by dev_dbg in the print lookup table. There are also messages to reduce the verbosity level to debug (commented inline).

Okay.

Quoted 7 lines

drivers/thermal/k3_j72xx_bandgap.c line 56

Quoted 4 lines
drivers/thermal/k3_j72xx_bandgap.c new file +3 -0
+
+static int *ref_table;
+static int *derived_table;

These tables could be passed around via function parameters and their life cycle is just during the probe. No need to declare them globally.

Okay.

Quoted 32 lines
Quoted 19 lines
drivers/thermal/k3_j72xx_bandgap.c new file +18 -0
+
+s64 golden_factors[] = {
+	-490019999999999936,
+	3251200000000000,
+	-1705800000000,
+	603730000,
+	-92627,
+};
+
+s64 pvt_wa_factors[] = {
+	-415230000000000000,
+	3126600000000000,
+	-1157800000000,
+};
+
+static s64 int_power(unsigned long long base, unsigned int exp)
+{
+	s64 result = 1;

drivers/thermal/k3_j72xx_bandgap.c line 84

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+
+	while (exp) {
+		if (exp & 1)
+			result *= base;
+		exp >>= 1;
+		base *= base;
+	}
+
+	return result;
+}

This function is already implemented.

Quoted 1 line

include/linux/math.h:u64 int_pow(u64 base, unsigned int exp);

I will pick that up.

Quoted 19 lines

drivers/thermal/k3_j72xx_bandgap.c line 97

Quoted 13 lines
drivers/thermal/k3_j72xx_bandgap.c new file +12 -0
+static int compute_value(int index, s64 *factors, int nr_factors, int reduction)
+{
+	s64 value = 0;
+	int i;
+
+	for (i = 0; i < nr_factors; i++)
+		value += factors[i] * int_power(index, i);
+
+	return (int)(value / int_power(10, reduction));
+}
+
+static void init_table(bool wa, int *mytable)

s/mytable/table/

Instead of passing a boolean, let the caller pass the right parameters, that makes the code nicer.

Quoted 8 lines

static void init_table(int *table, int size, s64 *factors, int nr_factors)

{
	int i;

	for (i = 0; i < size; i++)
		table[i] = polynomial(factors, nr_factors,
					FACTORS_REDUCTION);
}

Okay.

Quoted 25 lines
Quoted 11 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+{
+	s64 *factors;
+	const int size = 1024;
+	int factors_size;
+	int i;
+
+	if (wa) {
+		factors_size = 3;
+		factors = pvt_wa_factors;
+	} else {

drivers/thermal/k3_j72xx_bandgap.c line 117

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +10 -0
+		factors_size = 5;
+		factors = golden_factors;
+	}
+
+	for (i = 0; i < size; i++)
+		mytable[i] = compute_value(i, factors, factors_size, 13);
+}
+
+static void fill_table(int err1, int err2, int ref1, int ref2, int idx1,
+		       int idx2)

I suggest to create a self explanatory structure and pass a couple of them as parameter with the derived and ref tables.

Quoted 3 lines

Given what does this function, it would make sense rename it to something more explicit along with a short explanation of why this is needed.

Okay

Quoted 7 lines

drivers/thermal/k3_j72xx_bandgap.c line 121

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+{
+	int m, c, num, den, i, err;
+
+	/**

'/**' is for kernel documentation, should be replaced by /*

Okay

Quoted 11 lines

drivers/thermal/k3_j72xx_bandgap.c line 128

Quoted 8 lines
drivers/thermal/k3_j72xx_bandgap.c new file +7 -0
+	 * Calculate the slope with adc values read from the register
+	 * as the y-axis param and err in adc value as x-axis param
+	 */
+	if (err1 != err2) {
+		num = ref2 - ref1;
+		den = err2 - err1;
+		m = num / den;

if den > num then 'm' is zero and err = (i - c) / m will crash if ref1 and ref2 are equal, 'm' will be zero also

Thanks for catching. I will add them as conditions in the if.

Quoted 12 lines

drivers/thermal/k3_j72xx_bandgap.c line 137

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +9 -0
+		c = ref2 - m * err2;
+		for (i = idx1; i <= idx2; i++) {
+			err = (i - c) / m;
+			if (((i + err1) < 0) || ((i + err1) > 1023))
+				continue;
+			derived_table[i] = ref_table[i + err];
+		}
+	} else { /* Constant error take care of divide by zero */
+		for (i = idx1; i < idx2; i++) {

Why the first loop has i<=idx2 and this one is i<idx2 ?

I will make them <=.

Quoted 7 lines

drivers/thermal/k3_j72xx_bandgap.c line 141

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+			if (((i + err1) < 0) || ((i + err1) > 1023))
+				continue;
+			derived_table[i] = ref_table[i + err1];
+		}

Can be this loop put outside of the if the else ?

Okay. I can add a return in the if & else can be removed.

Quoted 17 lines
Quoted 17 lines
drivers/thermal/k3_j72xx_bandgap.c new file +16 -0
+	}
+}
+
+static int prep_lookup_table(int minus40cerr, int plus30cerr, int plus125cerr,
+			     int plus150cerr)
+{
+	int inc, i;
+
+	/* Fill up the lookup table region -40C to +30C */
+	fill_table(minus40cerr, plus30cerr, MINUS40CREF, PLUS30CREF, 0,
+		   PLUS30CREF);
+	/* Fill up the lookup table region +30C to +125C */
+	fill_table(plus30cerr, plus125cerr, PLUS30CREF, PLUS125CREF, PLUS30CREF,
+		   PLUS125CREF);
+	/* Fill up the lookup table region +125C to +150C */
+	fill_table(plus125cerr, plus150cerr, PLUS125CREF, PLUS150CREF, PLUS125CREF,
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 163

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+		   PLUS150CREF);
+
+	/* Get to the first valid temperature */
+	i = 0;
+	while (!derived_table[i])
+		i++;

zero is not a valid temperature ?

Basically assumption is we start somewhere near -40C. If there are zeros it means those indicies need to b backfilled with 300 milli degree steps.

Quoted 13 lines
Quoted 13 lines
drivers/thermal/k3_j72xx_bandgap.c new file +12 -0
+
+	/*
+	 * Get to the last zero index and back fill the temperature for
+	 * sake of continuity
+	 */
+	if (i) {
+		/* 300 milli celsius steps */
+		while (i--)
+			derived_table[i] = derived_table[i + 1] - 300;
+		/* case 0 */
+		derived_table[i] = derived_table[i + 1] - 300;
+	}
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 181

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+
+	/*
+	 * Fill the last trailing 0s which are unfilled with increments of
+	 * 100 milli celsius till 1023 code
+	 */
+	i = 1023;

Please, give the table and const table size in parameters.

Okay

Quoted 12 lines

drivers/thermal/k3_j72xx_bandgap.c line 190

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +9 -0
+	while (!derived_table[i])
+		i--;
+
+	i++;
+	inc = 1;
+	while (i < 1024) {
+		derived_table[i] = derived_table[i - 1] + inc * 100;
+		i++;
+	}

Can you explain why the above is done ?

This is the ending section of the table if some of the indicies are not populated then as per errata implementation fill the last remaining idicies with 100 milli degree centigrade increments to have the table complete.

Quoted 70 lines

drivers/thermal/k3_j72xx_bandgap.c line 195

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+	return 0;
+}
+
+struct k3_thermal_data;

not necessary

Below, "not necessary" == the field is assigned but then never used, "not used" == the field is never used at all.

drivers/thermal/k3_j72xx_bandgap.c line 198

Quoted 2 lines
drivers/thermal/k3_j72xx_bandgap.c new file +2 -0
+struct k3_j72xx_bandgap {
+	struct device *dev;

not necessary

drivers/thermal/k3_j72xx_bandgap.c line 203

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+	void __iomem *base;
+	void __iomem *cfg2_base;
+	void __iomem *fuse_base;
+	const struct k3_j72xx_bandgap_data	*conf;
+	spinlock_t lock; /* shields this struct */

not used

drivers/thermal/k3_j72xx_bandgap.c line 204

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int ts_cnt;

not used

drivers/thermal/k3_j72xx_bandgap.c line 205

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	struct k3_thermal_data *ts_data[K3_VTM_MAX_NUM_TS];

not used

drivers/thermal/k3_j72xx_bandgap.c line 210

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+};
+
+/* common data structures */
+struct k3_thermal_data {
+	struct thermal_zone_device *ti_thermal;

not necessary

drivers/thermal/k3_j72xx_bandgap.c line 211

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	struct thermal_cooling_device *cool_dev;

not used

drivers/thermal/k3_j72xx_bandgap.c line 213

Quoted 2 lines
drivers/thermal/k3_j72xx_bandgap.c new file +2 -0
+	struct k3_j72xx_bandgap *bgp;
+	enum thermal_device_mode mode;

not used

drivers/thermal/k3_j72xx_bandgap.c line 214

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int sensor_id;

not used

drivers/thermal/k3_j72xx_bandgap.c line 217

Quoted 3 lines
drivers/thermal/k3_j72xx_bandgap.c new file +3 -0
+	u32 ctrl_offset;
+	u32 stat_offset;
+	int prev_temp;

not necessary

drivers/thermal/k3_j72xx_bandgap.c line 219

Quoted 2 lines
drivers/thermal/k3_j72xx_bandgap.c new file +2 -0
+	int ct_offsets[K3_VTM_CORRECTION_TEMP_CNT];
+	int ct_bm[K3_VTM_CORRECTION_TEMP_CNT];

Both are only local to the function using it

drivers/thermal/k3_j72xx_bandgap.c line 220

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int ref_adc_val[3];

not used

drivers/thermal/k3_j72xx_bandgap.c line 221

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int act_adc_val[3];

not used

Quoted 9 lines

drivers/thermal/k3_j72xx_bandgap.c line 222

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int slope[2];

not used

drivers/thermal/k3_j72xx_bandgap.c line 223

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	int coeff[2];

not used

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+};

Okay for all the above params. I will optimize them.

Quoted 4 lines
Quoted 4 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+
+static int two_cmp(int tmp, int mask)
+{
+	tmp = ~(tmp);
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 234

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+	tmp &= mask;
+	tmp += 1;
+
+	/* Return negative value */
+	return (0 - tmp);
+}

Can you explain ?

Two's compliment implementation. Some of the error values in the Efuse are in 2's complement notation & hence the above function is used to convert.

Quoted 55 lines
Quoted 55 lines
drivers/thermal/k3_j72xx_bandgap.c new file +54 -0
+
+static unsigned int vtm_get_best_value(unsigned int s0, unsigned int s1,
+				       unsigned int s2)
+{
+	int d01 = abs(s0 - s1);
+	int d02 = abs(s0 - s2);
+	int d12 = abs(s1 - s2);
+
+	if (d01 <= d02 && d01 <= d12)
+		return (s0 + s1) / 2;
+
+	if (d02 <= d01 && d02 <= d12)
+		return (s0 + s2) / 2;
+
+	return (s1 + s2) / 2;
+}
+
+static inline int k3_bgp_read_temp(struct k3_thermal_data *devdata,
+				   int *temp)
+{
+	struct k3_j72xx_bandgap *bgp;
+	unsigned int dtemp, s0, s1, s2;
+
+	bgp = devdata->bgp;
+	/*
+	 * Errata is applicable for am654 pg 1.0 silicon/J7ES. There
+	 * is a variation of the order for certain degree centigrade on AM654.
+	 * Work around that by getting the average of two closest
+	 * readings out of three readings everytime we want to
+	 * report temperatures.
+	 *
+	 * Errata workaround.
+	 */
+	s0 = readl(bgp->base + devdata->stat_offset) &
+		K3_VTM_TS_STAT_DTEMP_MASK;
+	s1 = readl(bgp->base + devdata->stat_offset) &
+		K3_VTM_TS_STAT_DTEMP_MASK;
+	s2 = readl(bgp->base + devdata->stat_offset) &
+		K3_VTM_TS_STAT_DTEMP_MASK;
+	dtemp = vtm_get_best_value(s0, s1, s2);
+
+	if (dtemp < 0 || dtemp > 1023)
+		return -EINVAL;
+
+	*temp = derived_table[dtemp];
+
+	return 0;
+}
+
+/* Get temperature callback function for thermal zone */
+static int k3_thermal_get_temp(void *devdata, int *temp)
+{
+	struct k3_thermal_data *data = devdata;
+	int ret = 0;
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 294

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+
+	ret = k3_bgp_read_temp(data, temp);
+	if (ret)
+		return ret;
+
+	data->prev_temp = *temp;

where is it used ?

Okay this will be needed if trend computation was needed. I will optimize this one as well.

Quoted 10 lines
Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +9 -0
+
+	return ret;
+}
+
+static const struct thermal_zone_of_device_ops k3_of_thermal_ops = {
+	.get_temp = k3_thermal_get_temp,
+};
+
+static int k3_j72xx_bandgap_temp_to_adc_code(int temp)
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 308

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+{
+	int low = 0, high = 1023, mid;
+
+	if (temp > 160000 || temp < -50000)
+		return -EINVAL;

How the caller does the difference between a negative temperature and this error ?

The converted ADC code has to be between 0 - 1023. Any negative value returned is an error. Does that make sense?

Quoted 89 lines
Quoted 89 lines
drivers/thermal/k3_j72xx_bandgap.c new file +88 -0
+	/* Binary search to find the adc code */
+	while (low < (high - 1)) {
+		mid = (low + high) / 2;
+		if (temp <= derived_table[mid])
+			high = mid;
+		else
+			low = mid;
+	}
+
+	return mid;
+}
+
+static void get_efuse_values(int id, struct k3_thermal_data *data, int *err,
+			     struct k3_j72xx_bandgap *bgp)
+{
+	int i, tmp, pow;
+
+	/* Populate efuse reg offsets & Bit masks for -40C, 30C, 125C */
+	switch (id) {
+	case 0:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = 0x8;
+		data->ct_offsets[2] = 0x4;
+		data->ct_bm[0] = 0x3f;
+		data->ct_bm[1] = 0x1fe000;
+		data->ct_bm[2] = 0x1ff;
+		break;
+
+	case 1:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = 0x8;
+		data->ct_offsets[2] = 0x4;
+		data->ct_bm[0] = 0xfc0;
+		data->ct_bm[1] = 0x1fe00000;
+		data->ct_bm[2] = 0x3fe00;
+		break;
+
+	case 2:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = -1;
+		data->ct_offsets[2] = 0x4;
+		data->ct_bm[0] = 0x3f000;
+		data->ct_bm[1] = 0x7f800000;
+		data->ct_bm[2] = 0x7fc0000;
+		break;
+
+	case 3:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = 0xC;
+		data->ct_offsets[2] = -1; /* Spread across 2 registers */
+		data->ct_bm[0] = 0xfc0000;
+		data->ct_bm[1] = 0x1fe0;
+		data->ct_bm[2] = 0x1f800000;
+		break;
+
+	case 4:
+		data->ct_offsets[0] = 0x0;
+		data->ct_offsets[1] = 0xc;
+		data->ct_offsets[2] = 0x8;
+		data->ct_bm[0] = 0x3f000000;
+		data->ct_bm[1] = 0x1fe000;
+		data->ct_bm[2] = 0x1ff0;
+		break;
+	}
+
+	for (i = 0; i < 3; i++) {
+		/* Extract the offset value using bit-mask */
+		if (data->ct_offsets[i] == -1 && i == 1) {
+			/* 25C offset Case of Sensor 2 split between 2 regs */
+			tmp = (readl(bgp->fuse_base + 0x8) & 0xE0000000) >> (29);
+			tmp |= ((readl(bgp->fuse_base + 0xC) & 0x1F) << 3);
+			pow = tmp & 0x80;
+		} else if (data->ct_offsets[i] == -1 && i == 2) {
+			/* 125C Case of Sensor 3 split between 2 regs */
+			tmp = (readl(bgp->fuse_base + 0x4) & 0xF8000000) >> (27);
+			tmp |= ((readl(bgp->fuse_base + 0x8) & 0xF) << 5);
+			pow = tmp & 0x100;
+		} else {
+			tmp = readl(bgp->fuse_base + data->ct_offsets[i]);
+			tmp &= data->ct_bm[i];
+			tmp = tmp >> __ffs(data->ct_bm[i]);
+
+			/* Obtain the sign bit pow*/
+			pow = data->ct_bm[i] >> __ffs(data->ct_bm[i]);
+			pow += 1;
+			pow /= 2;
+		}
+
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 403

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+		/* Check for negative value */
+		if (tmp & pow) {
+			/* 2's complement value */
+			tmp = two_cmp(tmp, data->ct_bm[i] >> __ffs(data->ct_bm[i]));
+		}
+		err[i] = tmp;

Can you explain what is doing the code above ?

The efuse will have error values in 2's complement notation. The above it to get the MSB which will decide the sign.

Quoted 7 lines

drivers/thermal/k3_j72xx_bandgap.c line 407

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +4 -0
+	}
+}
+
+#ifdef DEBUG_VTM

Remove

Okay.

Quoted 9 lines

drivers/thermal/k3_j72xx_bandgap.c line 412

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+static void print_look_up_table(struct device *dev)
+{
+	int i;
+
+	dev_info(dev, "The contents of derived array\n");

dev_dbg

Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 413

Quoted 1 line
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	dev_info(dev, "Code   Temperaturei\n");

dev_dbg

drivers/thermal/k3_j72xx_bandgap.c line 415

Quoted 2 lines
drivers/thermal/k3_j72xx_bandgap.c new file +2 -0
+	for (i = 0; i <= 1023; i++)
+		dev_info(dev, "%d       %d\n", i, derived_table[i]);

dev_dbg

Okay for all the above devv_info --> dev_dbg

Quoted 9 lines

drivers/thermal/k3_j72xx_bandgap.c line 421

Quoted 7 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+}
+#endif
+
+static const struct of_device_id of_k3_j72xx_bandgap_match[];
+
+struct k3_j72xx_bandgap_j72xx_data {

Fix name please

Ah okay. I will fix.

Quoted 4 lines

drivers/thermal/k3_j72xx_bandgap.c line 422

Quoted 2 lines
drivers/thermal/k3_j72xx_bandgap.c new file +1 -0
+	unsigned int	workaround;

s/workaround/has_errata_<num>/

okay

Quoted 34 lines
Quoted 34 lines
drivers/thermal/k3_j72xx_bandgap.c new file +33 -0
+};
+
+static int k3_j72xx_bandgap_probe(struct platform_device *pdev)
+{
+	int ret = 0, cnt, val, id, reg_cnt = 0, j, table_size = 1024;
+	int err[3], high_max, low_max;
+	struct resource *res;
+	struct device *dev = &pdev->dev;
+	struct k3_j72xx_bandgap *bgp;
+	struct k3_thermal_data *data;
+	int workaround_needed = 0;
+	const struct of_device_id *of_id;
+	const struct k3_j72xx_bandgap_j72xx_data *driver_data;
+
+	bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
+	if (!bgp)
+		return -ENOMEM;
+
+	bgp->dev = dev;
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	bgp->base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(bgp->base))
+		return PTR_ERR(bgp->base);
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	bgp->cfg2_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(bgp->cfg2_base))
+		return PTR_ERR(bgp->cfg2_base);
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
+	bgp->fuse_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(bgp->fuse_base))
+		return PTR_ERR(bgp->fuse_base);
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 461

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+
+	of_id = of_match_device(of_k3_j72xx_bandgap_match, &pdev->dev);
+	if (of_id) {
+		driver_data = of_id->data;
+		workaround_needed = driver_data->workaround;
+	}

of_device_get_match_data(dev) ?

okay

Quoted 33 lines
Quoted 33 lines
drivers/thermal/k3_j72xx_bandgap.c new file +32 -0
+	pm_runtime_enable(dev);
+	ret = pm_runtime_get_sync(dev);
+	if (ret < 0) {
+		pm_runtime_put_noidle(dev);
+		pm_runtime_disable(dev);
+		return ret;
+	}
+
+	/* Get the sensor count in the VTM */
+	val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET);
+	cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK;
+	cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK);
+	bgp->ts_cnt = cnt;
+
+	data = devm_kcalloc(bgp->dev, cnt, sizeof(*data), GFP_KERNEL);
+	if (!data) {
+		ret = -ENOMEM;
+		goto err_alloc;
+	}
+
+	ref_table = devm_kzalloc(bgp->dev, sizeof(*ref_table) * table_size,
+				 GFP_KERNEL);
+	if (!ref_table) {
+		ret = -ENOMEM;
+		goto err_alloc;
+	}
+
+	derived_table = devm_kzalloc(bgp->dev, sizeof(*derived_table) * table_size,
+				     GFP_KERNEL);
+	if (!derived_table) {
+		ret = -ENOMEM;
+		goto err_alloc;
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 499

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+	}
+
+	/* Workaround not needed if bit30/bit31 is set even for J721e */
+	if (workaround_needed && (readl(bgp->fuse_base + 0x0) & 0xc0000000) == 0xc0000000)
+		workaround_needed = false;

Does it mean the same hardware may have or not the errata? Is there a way to distinguish them from the compatible string ?

Yes Same hardware same silicon revision some may have & some might not. This is the prescribed way to check if existance of errata.

Quoted 12 lines

drivers/thermal/k3_j72xx_bandgap.c line 507

Quoted 9 lines
drivers/thermal/k3_j72xx_bandgap.c new file +8 -0
+
+	if (!workaround_needed) {
+		dev_info(bgp->dev, "work around NOT needed!!\n");
+		init_table(false, ref_table);
+	} else {
+		dev_info(bgp->dev, "work around needed!!\n");
+		init_table(true, ref_table);
+	}
	dev_dbg(bgp->dev, "Work around %sneeded\n",
		workaround_needed ? "not " : "");

okay

Quoted 15 lines

drivers/thermal/k3_j72xx_bandgap.c line 518

Quoted 12 lines
drivers/thermal/k3_j72xx_bandgap.c new file +11 -0
+
+	/* Register the thermal sensors */
+	for (id = 0; id < cnt; id++) {
+		data[id].sensor_id = id;
+		data[id].bgp = bgp;
+		data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET + id * 0x20;
+		data[id].stat_offset = data[id].ctrl_offset +
+					K3_VTM_TMPSENS_STAT_OFFSET;
+
+		if (!workaround_needed)
+			goto prepare_lookup;
		if (workaround_needed) {
Quoted 7 lines

drivers/thermal/k3_j72xx_bandgap.c line 523

Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+		/* ref adc values for -40C, 30C & 125C respectively */
+		data[id].ref_adc_val[0] = MINUS40CREF;
+		data[id].ref_adc_val[1] = PLUS30CREF;
+		data[id].ref_adc_val[2] = PLUS125CREF;
+		get_efuse_values(id, &data[id], err, bgp);
		}

okay

Quoted 9 lines

drivers/thermal/k3_j72xx_bandgap.c line 530

Quoted 7 lines
drivers/thermal/k3_j72xx_bandgap.c new file +7 -0
+
+prepare_lookup:
+		if (id == 0 && workaround_needed) {
+			prep_lookup_table(err[0], err[1], err[2], 0);
+		} else if (id == 0 && !workaround_needed) {
+			for (j = 0; j < table_size; j++)
+				derived_table[j] = ref_table[j];

memcpy ?

okay

Quoted 15 lines
Quoted 15 lines
drivers/thermal/k3_j72xx_bandgap.c new file +14 -0
+		}
+
+		val = readl(data[id].bgp->cfg2_base + data[id].ctrl_offset);
+		val |= (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN |
+			K3_VTM_TMPSENS_CTRL_SOC |
+			K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4));
+		writel(val, data[id].bgp->cfg2_base + data[id].ctrl_offset);
+
+		bgp->ts_data[id] = &data[id];
+		data[id].ti_thermal =
+		devm_thermal_zone_of_sensor_register(bgp->dev, id,
+						     &data[id],
+						     &k3_of_thermal_ops);
+		if (IS_ERR(data[id].ti_thermal)) {
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 550

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+			dev_err(bgp->dev, "thermal zone device is NULL\n");
+			ret = PTR_ERR(data[id].ti_thermal);
+			goto err_alloc;
+		}
+
+		reg_cnt++;

not used

okay

Quoted 5 lines

drivers/thermal/k3_j72xx_bandgap.c line 553

Quoted 3 lines
drivers/thermal/k3_j72xx_bandgap.c new file +3 -0
+
+		/* Initialize Previous temp */
+		k3_thermal_get_temp(&data[id], &data[id].prev_temp);

why ? where is used prev_temp ?

This can be otimized.

Quoted 5 lines
Quoted 5 lines
drivers/thermal/k3_j72xx_bandgap.c new file +5 -0
+	}
+
+	/*
+	 * Program TSHUT thresholds
+	 * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2
Quoted 8 lines

drivers/thermal/k3_j72xx_bandgap.c line 564

Quoted 6 lines
drivers/thermal/k3_j72xx_bandgap.c new file +6 -0
+	 * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN  bit
+	 *         This is already taken care as per of init
+	 * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN  bit
+	 */
+	high_max = k3_j72xx_bandgap_temp_to_adc_code(MAX_TEMP);
+	low_max = k3_j72xx_bandgap_temp_to_adc_code(COOL_DOWN_TEMP);

I guess high_max is the hardware shutdown temperature, right ?

correct.

Quoted 2 lines

But what is low_max ?

It is high_max - hysteresis. May be i should change to high_max_hyst?

Quoted 12 lines

drivers/thermal/k3_j72xx_bandgap.c line 574

Quoted 10 lines
drivers/thermal/k3_j72xx_bandgap.c new file +9 -0
+	writel((low_max << 16) | high_max, data[0].bgp->cfg2_base +
+	       K3_VTM_MISC_CTRL2_OFFSET);
+	mdelay(100);
+	writel(K3_VTM_ANYMAXT_OUTRG_ALERT_EN, data[0].bgp->cfg2_base +
+	       K3_VTM_MISC_CTRL_OFFSET);
+
+	platform_set_drvdata(pdev, bgp);
+
+#ifdef DEBUG_VTM

Remove ifdef

okay

Thanks for the detailed review. I will work on the comments.

Regards,
Keerthy

Quoted 64 lines
Quoted 62 lines
drivers/thermal/k3_j72xx_bandgap.c new file +60 -0
+	print_look_up_table(dev);
+#endif
+	/*
+	 * Now that the derived_table has the appropriate look up values
+	 * Free up the ref_table
+	 */
+	kfree(ref_table);
+
+	return 0;
+
+err_alloc:
+	pm_runtime_put_sync(&pdev->dev);
+	pm_runtime_disable(&pdev->dev);
+
+	return ret;
+}
+
+static int k3_j72xx_bandgap_remove(struct platform_device *pdev)
+{
+	pm_runtime_put_sync(&pdev->dev);
+	pm_runtime_disable(&pdev->dev);
+
+	return 0;
+}
+
+const struct k3_j72xx_bandgap_j72xx_data k3_j72xx_bandgap_j721e_data = {
+	.workaround = 1,
+};
+
+const struct k3_j72xx_bandgap_j72xx_data k3_j72xx_bandgap_j7200_data = {
+	.workaround = 0,
+};
+
+static const struct of_device_id of_k3_j72xx_bandgap_match[] = {
+	{
+		.compatible = "ti,j721e-vtm",
+		.data = (void *)&k3_j72xx_bandgap_j721e_data,
+	},
+	{
+		.compatible = "ti,j7200-vtm",
+		.data = (void *)&k3_j72xx_bandgap_j7200_data,
+	},
+	{ /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, of_k3_j72xx_bandgap_match);
+
+static struct platform_driver k3_j72xx_bandgap_sensor_driver = {
+	.probe = k3_j72xx_bandgap_probe,
+	.remove = k3_j72xx_bandgap_remove,
+	.driver = {
+		.name = "k3-j72xx-soc-thermal",
+		.of_match_table	= of_k3_j72xx_bandgap_match,
+	},
+};
+
+module_platform_driver(k3_j72xx_bandgap_sensor_driver);
+
+MODULE_DESCRIPTION("K3 bandgap temperature sensor driver");
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("J Keerthy <[email protected]>");