ec/google/wilco/acpi: Add DPTF support

Add the support needed for DPTF.  This includes the methods to
write trip point values, read temperatures, and handle events.

This was tested on a sarien board by inspecting AML debug output
with the kernel while monitoring temperatures and trip points in
sysfs and controlling temperatures with a fan to ensure that when
a trip point is crossed an SCI is generated and the event is
handled properly.

Change-Id: I8d8570d176c0896fa709a6c782b319f58d3c1e52
Signed-off-by: Duncan Laurie <dlaurie@google.com>
Reviewed-on: https://review.coreboot.org/c/29761
Reviewed-by: Furquan Shaikh <furquan@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/src/ec/google/wilco/acpi/dptf.asl b/src/ec/google/wilco/acpi/dptf.asl
new file mode 100644
index 0000000..f5545d0
--- /dev/null
+++ b/src/ec/google/wilco/acpi/dptf.asl
@@ -0,0 +1,134 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 2018 Google LLC
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 of
+ * the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * Dynamic Platform Thermal Framework support
+ */
+
+External (\_SB.DPTF.CTOK, MethodObj)
+External (\_SB.DPTF.KTOC, MethodObj)
+External (\_SB.DPTF.TEVT, MethodObj)
+
+/* Mutex for EC PAT interface */
+Mutex (PATM, 1)
+
+/* Read requested temperature sensor */
+Method (TSRD, 1, Serialized)
+{
+	If (Acquire (^PATM, 1000)) {
+		Return (0)
+	}
+
+	/* Set sensor ID */
+	W (DWTI, ToInteger (Arg0))
+
+	Local0 = R (DRTV)
+
+	Release (^PATM)
+	Return (\_SB.DPTF.CTOK (Local0))
+}
+
+/*
+ * Set Aux Trip Point 0
+ *   Arg0 = Temp Sensor ID
+ *   Arg1 = Value to set
+ */
+Method (PAT0, 2, Serialized)
+{
+	If (Acquire (^PATM, 1000)) {
+		Return (0)
+	}
+
+	/* Set sensor ID */
+	W (DWTI, ToInteger (Arg0))
+
+	/* Set LOW trip point for this sensor */
+	W (DWTL, \_SB.DPTF.KTOC (Arg1))
+
+	Release (^PATM)
+	Return (1)
+}
+
+/*
+ * Set Aux Trip Point 1
+ *   Arg0 = Temp Sensor ID
+ *   Arg1 = Value to set
+ */
+Method (PAT1, 2, Serialized)
+{
+	If (Acquire (^PATM, 1000)) {
+		Return (0)
+	}
+
+	/* Set sensor ID */
+	W (DWTI, ToInteger (Arg0))
+
+	/* Set HIGH trip point for this sensor */
+	W (DWTH, \_SB.DPTF.KTOC (Arg1))
+
+	Release (^PATM)
+	Return (1)
+}
+
+/*
+ * Disable Aux Trip Points
+ *   Arg0 = Temp Sensor ID
+ */
+Method (PATD, 1, Serialized)
+{
+	If (Acquire (^PATM, 1000)) {
+		Return (0)
+	}
+
+	/* Set sensor ID */
+	W (DWTI, ToInteger (Arg0))
+
+	/* Disable LOW and HIGH trip points */
+	W (DWTL, 0xff)
+	W (DWTH, 0xff)
+
+	Release (^PATM)
+	Return (1)
+}
+
+/*
+ * Handle sensor trip events
+ */
+Method (PATX, 0, Serialized)
+{
+	Local0 = R (DRTQ)
+	Local1 = Local0
+
+	Printf ("Sensor trip mask: %o", Local0)
+
+	If (LNot (Acquire (^PATM, 1000))) {
+
+		/* Handle bits that are set */
+		While (FindSetRightBit (Local1, Local2))
+		{
+			/* DPTF will Notify sensor devices */
+			\_SB.DPTF.TEVT (Local2)
+
+			/* Clear current sensor number */
+			Local1 &= ~(1 << (Local2 - 1))
+		}
+
+		Release (^PATM)
+	}
+
+	/* Clear sensor events */
+	W (DWTQ, Local0)
+}
diff --git a/src/ec/google/wilco/acpi/ec.asl b/src/ec/google/wilco/acpi/ec.asl
index 67a698f..ff8fccc 100644
--- a/src/ec/google/wilco/acpi/ec.asl
+++ b/src/ec/google/wilco/acpi/ec.asl
@@ -49,6 +49,11 @@
 
 		/* Tell EC to stop emulating PS/2 mouse */
 		W (PS2M, Zero)
+
+		/* Enable DPTF support if enabled in devicetree */
+		If (\DPTE == One) {
+			W (DWST, Arg1)
+		}
 	}
 
 	/*
@@ -142,4 +147,5 @@
 	#include "event.asl"
 	#include "lid.asl"
 	#include "platform.asl"
+	#include "dptf.asl"
 }
diff --git a/src/ec/google/wilco/acpi/ec_ram.asl b/src/ec/google/wilco/acpi/ec_ram.asl
index 6ea2366..1563a3c 100644
--- a/src/ec/google/wilco/acpi/ec_ram.asl
+++ b/src/ec/google/wilco/acpi/ec_ram.asl
@@ -106,6 +106,14 @@
 Name (QSEC, Package () { 0x2b, 0xff, RD })	/* QS Event Count */
 Name (QSEB, Package () { 0x2c, 0xff, RD })	/* QS Event Byte */
 
+Name (DRST, Package () { 0x32, 0xff, RD })	/* DPTF: Read State */
+Name (DRTI, Package () { 0x33, 0xff, RD })	/* DPTF: Read Thermal Index */
+Name (DRTV, Package () { 0x34, 0xff, RD })	/* DPTF: Read Thermal Value */
+Name (DRTL, Package () { 0x35, 0xff, RD })	/* DPTF: Read Trip Low */
+Name (DRTH, Package () { 0x36, 0xff, RD })	/* DPTF: Read Trip High */
+Name (DRHY, Package () { 0x37, 0xff, RD })	/* DPTF: Read Hysteresis */
+Name (DRTQ, Package () { 0x38, 0xff, RD })	/* DPTF: Read Trip Query */
+
 Name (ORST, Package () { 0x39, 0xff, RD })	/* Orientation State */
 Name (OREV, Package () { 0x3a, 0xff, RD })	/* Orientation Events */
 Name (OECH, Package () { 0x3a, 0x01, RD })	/* Event: Orientation */
@@ -128,3 +136,9 @@
 Name (ERDY, Package () { 0x05, 0xff, WR })	/* EC Ready */
 Name (FWAK, Package () { 0x06, 0xff, WR })	/* EC _WAK */
 Name (PS2M, Package () { 0x20, 0xff, WR })	/* EC PS/2 Mouse Emulation */
+Name (DWST, Package () { 0x32, 0xff, WR })	/* DPTF: Write State */
+Name (DWTI, Package () { 0x33, 0xff, WR })	/* DPTF: Write Thermal Index */
+Name (DWTL, Package () { 0x35, 0xff, WR })	/* DPTF: Write Trip Low */
+Name (DWTH, Package () { 0x36, 0xff, WR })	/* DPTF: Write Trip High */
+Name (DWHY, Package () { 0x37, 0xff, WR })	/* DPTF: Write Hysteresis */
+Name (DWTQ, Package () { 0x38, 0xff, WR })	/* DPTF: Write Trip Query */
diff --git a/src/ec/google/wilco/acpi/event.asl b/src/ec/google/wilco/acpi/event.asl
index 4d796b5..21721f2 100644
--- a/src/ec/google/wilco/acpi/event.asl
+++ b/src/ec/google/wilco/acpi/event.asl
@@ -87,6 +87,11 @@
 Method (ECQ3, 1, Serialized)
 {
 	Printf ("EVT3: %o", Arg0)
+
+	/* Theraml Events */
+	If (EBIT (E3TH, Arg0)) {
+		^PATX ()
+	}
 }
 
 /* Handle events in PmEv4 */