meta-quanta: runbmc-nuvoton: pid: enable PID to control FAN tach for RunBMC

Enable phosphor-pid-control to control FAN tach according eight temperature sensors.
There are six FANs in RunBMC and all controlled by PWM1, thus we only define one fan type sensor in .json file.

Consider FAN tach might be stopped then cause RunBMC sever hang, FAN tach will turn into full speed if swampd daemon dead unexpected.

(From meta-quanta rev: 1689d1e7e4ed67560f73eee673a06b25cb346e60)

Signed-off-by: Tim Lee <timlee660101@gmail.com>
Change-Id: I52db7eeff8abd1573ee9b4a61bd6af4a1b624d9a
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/config-runbmc-nuvoton.json b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/config-runbmc-nuvoton.json
new file mode 100644
index 0000000..5a78db6
--- /dev/null
+++ b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/config-runbmc-nuvoton.json
@@ -0,0 +1,400 @@
+{
+    "sensors" : [
+        {
+            "name": "fan1",
+            "type": "fan",
+            "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1",
+            "writePath": "/sys/devices/platform/ahb/ahb:apb/f0103000.pwm-fan-controller/hwmon/**/pwm1",
+            "min": 0,
+            "max": 255
+        },
+        {
+            "name": "Core_0_CPU0",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_0_CPU0",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_0_CPU1",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_0_CPU1",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_1_CPU0",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_1_CPU0",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_1_CPU1",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_1_CPU1",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_2_CPU0",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_2_CPU0",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_2_CPU1",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_2_CPU1",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_3_CPU0",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_3_CPU0",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_3_CPU1",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_3_CPU1",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_4_CPU0",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_4_CPU0",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_4_CPU1",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_4_CPU1",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_5_CPU0",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_5_CPU0",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        },
+        {
+            "name": "Core_5_CPU1",
+            "type": "temp",
+            "readPath": "/xyz/openbmc_project/sensors/temperature/Core_5_CPU1",
+            "writePath": "",
+            "min": 0,
+            "max": 0,
+            "timeout": 0
+        }
+    ],
+    "zones" : [
+        {
+            "id": 0,
+            "minThermalOutput": 0.0,
+            "failsafePercent": 100.0,
+            "pids": [
+                {
+                    "name": "fan1",
+                    "type": "fan",
+                    "inputs": ["fan1"],
+                    "setpoint": 40.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": 0.0,
+                        "integralCoeff": 0.0,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 1.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 3.0,
+                        "outLim_max": 100.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0
+                    }
+                },
+                {
+                    "name": "Core_0_CPU0",
+                    "type": "temp",
+                    "inputs": ["Core_0_CPU0"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_0_CPU1",
+                    "type": "temp",
+                    "inputs": ["Core_0_CPU1"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_1_CPU0",
+                    "type": "temp",
+                    "inputs": ["Core_1_CPU0"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_1_CPU1",
+                    "type": "temp",
+                    "inputs": ["Core_1_CPU1"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_2_CPU0",
+                    "type": "temp",
+                    "inputs": ["Core_2_CPU0"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_2_CPU1",
+                    "type": "temp",
+                    "inputs": ["Core_2_CPU1"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_3_CPU0",
+                    "type": "temp",
+                    "inputs": ["Core_3_CPU0"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_3_CPU1",
+                    "type": "temp",
+                    "inputs": ["Core_3_CPU1"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_4_CPU0",
+                    "type": "temp",
+                    "inputs": ["Core_4_CPU0"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_4_CPU1",
+                    "type": "temp",
+                    "inputs": ["Core_4_CPU1"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_5_CPU0",
+                    "type": "temp",
+                    "inputs": ["Core_5_CPU0"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                },
+                {
+                    "name": "Core_5_CPU1",
+                    "type": "temp",
+                    "inputs": ["Core_5_CPU1"],
+                    "setpoint": 30.0,
+                    "pid": {
+                        "samplePeriod": 1.0,
+                        "proportionalCoeff": -20.0,
+                        "integralCoeff": -0.1,
+                        "feedFwdOffsetCoeff": 0.0,
+                        "feedFwdGainCoeff": 0.0,
+                        "integralLimit_min": 0.0,
+                        "integralLimit_max": 0.0,
+                        "outLim_min": 0.0,
+                        "outLim_max": 16000.0,
+                        "slewNeg": 0.0,
+                        "slewPos": 0.0,
+                        "positiveHysteresis": 1.0,
+                        "negativeHysteresis": 1.0
+                    }
+                }
+            ]
+        }
+    ]
+}
diff --git a/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/fan-full-speed.sh b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/fan-full-speed.sh
new file mode 100644
index 0000000..9a0d8e2
--- /dev/null
+++ b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/fan-full-speed.sh
@@ -0,0 +1,3 @@
+#!bin/bash
+
+echo 255 > /sys/class/hwmon/*/pwm1
diff --git a/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/fan-reboot-control.service b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/fan-reboot-control.service
new file mode 100644
index 0000000..3ce2e01
--- /dev/null
+++ b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/fan-reboot-control.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Set Fan to Full Speed as Rebooting
+DefaultDependencies=no
+After=shutdown.target
+
+[Service]
+Type=oneshot
+RemainAfterExit=true
+ExecStart=/usr/bin/fan-full-speed.sh
+
+[Install]
+WantedBy=shutdown.target
diff --git a/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/phosphor-pid-control.service b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/phosphor-pid-control.service
new file mode 100644
index 0000000..57ae69c
--- /dev/null
+++ b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control/phosphor-pid-control.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=OpenBMC Fan Control Daemon
+
+[Service]
+Type=simple
+ExecStart=/usr/bin/swampd
+Restart=always
+RestartSec=5
+StartLimitInterval=0
+ExecStopPost=/usr/bin/fan-full-speed.sh
+
+[Install]
+WantedBy=basic.target
diff --git a/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control_%.bbappend b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control_%.bbappend
new file mode 100644
index 0000000..ead72a1
--- /dev/null
+++ b/meta-quanta/meta-runbmc-nuvoton/recipes-phosphor/fans/phosphor-pid-control_%.bbappend
@@ -0,0 +1,29 @@
+FILESEXTRAPATHS_prepend_runbmc-nuvoton += "${THISDIR}/${PN}:"
+
+SRC_URI_append_runbmc-nuvoton = " file://config-runbmc-nuvoton.json"
+SRC_URI_append_runbmc-nuvoton = " file://fan-full-speed.sh"
+SRC_URI_append_runbmc-nuvoton = " file://phosphor-pid-control.service"
+SRC_URI_append_runbmc-nuvoton = " file://fan-reboot-control.service"
+
+FILES_${PN}_append_runbmc-nuvoton = " ${bindir}/fan-full-speed.sh"
+FILES_${PN}_append_runbmc-nuvoton = " ${datadir}/swampd/config.json"
+
+RDEPENDS_${PN} += "bash"
+
+SYSTEMD_SERVICE_${PN}_append_runbmc-nuvoton = " phosphor-pid-control.service"
+SYSTEMD_SERVICE_${PN}_append_runbmc-nuvoton = " fan-reboot-control.service"
+
+do_install_append_runbmc-nuvoton() {
+    install -d ${D}/${bindir}
+    install -m 0755 ${WORKDIR}/fan-full-speed.sh ${D}/${bindir}
+
+    install -d ${D}${datadir}/swampd
+    install -m 0644 -D ${WORKDIR}/config-runbmc-nuvoton.json \
+        ${D}${datadir}/swampd/config.json
+
+    install -d ${D}${systemd_unitdir}/system/
+    install -m 0644 ${WORKDIR}/phosphor-pid-control.service \
+        ${D}${systemd_unitdir}/system
+    install -m 0644 ${WORKDIR}/fan-reboot-control.service \
+        ${D}${systemd_unitdir}/system
+}