IT8772F: Add switch to enable HWM(Hardware Monitor)

Setup External Temperature to read via thermal diode/resistor
into TMPINx register by setting thermal_mode switch.

BUG=none
TEST=Compiled, verified
Signed-off-by: Ted Kuo <tedkuo@ami.com.tw>

Change-Id: I0e8621b92faa5c6246e009d2f852c8d4db484034
Reviewed-on: https://chromium-review.googlesource.com/260545
Reviewed-by: Shawn N <shawnn@chromium.org>
Commit-Queue: Ted Kuo <tedkuo@ami.com.tw>
Tested-by: Ted Kuo <tedkuo@ami.com.tw>
(cherry picked from commit 973e2d393f2595b756f8aa20f6fbe3b6e045621a)
Reviewed-on: https://chromium-review.googlesource.com/262340
diff --git a/src/superio/ite/it8772f/chip.h b/src/superio/ite/it8772f/chip.h
index 010c59a..4cd78e2 100644
--- a/src/superio/ite/it8772f/chip.h
+++ b/src/superio/ite/it8772f/chip.h
@@ -45,6 +45,12 @@
 	u8 peci_tmpin;
 
 	/*
+	 * Enable thermal mode on tmpinx.
+	 */
+	u8 tmpin1_mode;
+	u8 tmpin2_mode;
+
+	/*
 	 * Enable a FAN for sofware control.
 	 */
 	u8 fan1_enable;
diff --git a/src/superio/ite/it8772f/it8772f.h b/src/superio/ite/it8772f/it8772f.h
index f881d7a..9a43915 100644
--- a/src/superio/ite/it8772f/it8772f.h
+++ b/src/superio/ite/it8772f/it8772f.h
@@ -21,6 +21,12 @@
 #ifndef SUPERIO_ITE_IT8772F_IT8772F_H
 #define SUPERIO_ITE_IT8772F_IT8772F_H
 
+/* Supported thermal mode on TMPINx */
+enum thermal_mode {
+	THERMAL_DIODE		= 1,
+	THERMAL_RESISTOR,
+};
+
 #define IT8772F_BASE 0x2e
 
 #define IT8772F_FDC  0x00 /* Floppy disk controller */
@@ -32,6 +38,7 @@
 #define IT8772F_IR   0x0a /* Consumer IR */
 
 /* Environmental Controller interface */
+#define IT8772F_CONFIGURATION			0x00
 #define IT8772F_INTERFACE_SELECT		0x0a
 #define  IT8772F_INTERFACE_PSEUDO_EOC		(1 << 7)
 #define  IT8772F_INTERFACE_SMB_ENABLE		(1 << 6)
diff --git a/src/superio/ite/it8772f/superio.c b/src/superio/ite/it8772f/superio.c
index b4b54e9..5b9fbd7 100644
--- a/src/superio/ite/it8772f/superio.c
+++ b/src/superio/ite/it8772f/superio.c
@@ -20,6 +20,7 @@
 
 #include <device/device.h>
 #include <device/pnp.h>
+#include <console/console.h>
 #include <uart8250.h>
 #include <pc80/keyboard.h>
 #include <arch/io.h>
@@ -86,6 +87,43 @@
 }
 
 /*
+ * Setup External Temperature to read via thermal diode/resistor
+ * into TMPINx register
+ */
+static void it8772f_enable_tmpin(struct resource *res, int tmpin,
+				enum thermal_mode mode)
+{
+	u8 reg;
+
+	if (tmpin != 1 && tmpin != 2)
+		return;
+
+	reg = it8772f_envc_read(res, IT8772F_ADC_TEMP_CHANNEL_ENABLE);
+
+	switch (mode) {
+	case THERMAL_DIODE:
+		/* Thermal Diode Mode */
+		it8772f_envc_write(res, IT8772F_ADC_TEMP_CHANNEL_ENABLE,
+				   reg | tmpin);
+		break;
+	case THERMAL_RESISTOR:
+		/* Thermal Resistor Mode */
+		it8772f_envc_write(res, IT8772F_ADC_TEMP_CHANNEL_ENABLE,
+				   reg | (tmpin << 3));
+		break;
+	default:
+		printk(BIOS_ERR, "Unsupported thermal mode 0x%x on TMPIN%d\n",
+			mode, tmpin);
+		return;
+	}
+
+	reg = it8772f_envc_read(res, IT8772F_CONFIGURATION);
+
+	/* Enable the startup of monitoring operation */
+	it8772f_envc_write(res, IT8772F_CONFIGURATION, reg | 0x01);
+}
+
+/*
  * Setup a FAN PWM interface for software control
  */
 static void it8772f_enable_fan(struct resource *res, int fan)
@@ -149,6 +187,12 @@
 		/* Enable PECI if configured */
 		it8772f_enable_peci(res, conf->peci_tmpin);
 
+		/* Enable HWM if configured */
+		if (conf->tmpin1_mode)
+			it8772f_enable_tmpin(res, 1, conf->tmpin1_mode);
+		if (conf->tmpin2_mode)
+			it8772f_enable_tmpin(res, 2, conf->tmpin2_mode);
+
 		/* Enable FANx if configured */
 		if (conf->fan1_enable)
 			it8772f_enable_fan(res, 1);