diff mbox series

[14/16] hwmon: (mr75203) parse thermal coefficients from device-tree

Message ID 20220816082757.11990-15-farbere@amazon.com (mailing list archive)
State Superseded
Headers show
Series Variety of fixes and new features for mr75203 driver | expand

Commit Message

Farber, Eliav Aug. 16, 2022, 8:27 a.m. UTC
Use thermal coefficients from the device tree if they exist.
Otherwise, use default values.

The equation used in the driver is:
  T = G + H * (n / cal5 - 0.5) + J * F

With this change we can support also Mode 1 Conversion, which
uses A instead of G, and B instead of H.

We can also support the series 6 equation that has different
coefficients and has a slightly different format:
  T = G + H * (n / cal5 - 0.5)
by setting J to 0.

Signed-off-by: Eliav Farber <farbere@amazon.com>
---
 drivers/hwmon/mr75203.c | 44 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/drivers/hwmon/mr75203.c b/drivers/hwmon/mr75203.c
index 59e2dc8fa333..79831a0d5dca 100644
--- a/drivers/hwmon/mr75203.c
+++ b/drivers/hwmon/mr75203.c
@@ -129,6 +129,10 @@  struct pvt_device {
 	u32			p_num;
 	u32			v_num;
 	u32			ip_freq;
+	u32			ts_coeff_h;
+	u32			ts_coeff_g;
+	s32			ts_coeff_j;
+	u32			ts_coeff_cal5;
 	u8			vm_ch_max;
 	u8			vm_ch_total;
 };
@@ -177,10 +181,10 @@  static int pvt_read_temp(struct device *dev, u32 attr, int channel, long *val)
 		 * Convert the register value to degrees centigrade temperature:
 		 * T = G + H * (n / cal5 - 0.5) + J * F
 		 */
-		*val = PVT_G_CONST;
-		*val += PVT_H_CONST * nbs / PVT_CAL5_CONST;
-		*val -= PVT_H_CONST / 2;
-		*val += PVT_J_CONST * pvt->ip_freq / HZ_PER_MHZ;
+		*val = pvt->ts_coeff_g;
+		*val += pvt->ts_coeff_h * nbs / pvt->ts_coeff_cal5;
+		*val -= pvt->ts_coeff_h / 2;
+		*val += pvt->ts_coeff_j * pvt->ip_freq / HZ_PER_MHZ;
 
 		return 0;
 	default:
@@ -617,6 +621,38 @@  static int mr75203_probe(struct platform_device *pdev)
 		memset32(temp_config, HWMON_T_INPUT, ts_num);
 		pvt_temp.config = temp_config;
 		pvt_info[index++] = &pvt_temp;
+
+		/*
+		 * Incase ts-coeff-h/g/j/cal5 property is not defined, use
+		 * default value.
+		 */
+		ret = of_property_read_u32(np, "ts-coeff-h", &pvt->ts_coeff_h);
+		if (ret)
+			pvt->ts_coeff_h = PVT_H_CONST;
+
+		ret = of_property_read_u32(np, "ts-coeff-g", &pvt->ts_coeff_g);
+		if (ret)
+			pvt->ts_coeff_g = PVT_G_CONST;
+
+		ret = of_property_read_s32(np, "ts-coeff-j", &pvt->ts_coeff_j);
+		if (ret)
+			pvt->ts_coeff_j = PVT_J_CONST;
+
+		ret = of_property_read_u32(np, "ts-coeff-cal5",
+					   &pvt->ts_coeff_cal5);
+		if (ret) {
+			pvt->ts_coeff_cal5 = PVT_CAL5_CONST;
+		} else {
+			if (pvt->ts_coeff_cal5 == 0) {
+				dev_err(dev, "invalid ts-coeff-cal5 (%u)\n",
+					pvt->ts_coeff_cal5);
+				return -EINVAL;
+			}
+		}
+
+		dev_dbg(dev, "ts-coeff: h = %u, g = %u, j = %d, cal5 = %u\n",
+			pvt->ts_coeff_h, pvt->ts_coeff_g, pvt->ts_coeff_j,
+			pvt->ts_coeff_cal5);
 	}
 
 	if (pd_num) {