stepwise: Add ceiling type

Add a stepwise ceiling type, this is used as a
upper clipping curve to limit the max output based
on a temperature sensor. This is commonly used for
quiet fan mode where CPU throttling is allowed to
preserve a max fan noise.

Change-Id: I181d5913c92e5498a34e6d3f67cf99b67471479c
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/pid/ec/stepwise.hpp b/pid/ec/stepwise.hpp
index bc7c204..3ce847a 100644
--- a/pid/ec/stepwise.hpp
+++ b/pid/ec/stepwise.hpp
@@ -31,6 +31,7 @@
     double output[maxStepwisePoints];
     double positiveHysteresis;
     double negativeHysteresis;
+    bool isCeiling;
 };
 
 double stepwise(const ec::StepwiseInfo& info, double value);
diff --git a/pid/pidthread.cpp b/pid/pidthread.cpp
index 44c5e86..08fb513 100644
--- a/pid/pidthread.cpp
+++ b/pid/pidthread.cpp
@@ -31,6 +31,7 @@
     zone->updateSensors();
     // Zero out the RPM set point goals.
     zone->clearRPMSetPoints();
+    zone->clearRPMCeilings();
     // Run the margin PIDs.
     zone->processThermals();
     // Get the maximum RPM setpoint.
diff --git a/pid/stepwisecontroller.cpp b/pid/stepwisecontroller.cpp
index fee25ab..2408502 100644
--- a/pid/stepwisecontroller.cpp
+++ b/pid/stepwisecontroller.cpp
@@ -95,9 +95,13 @@
 
 void StepwiseController::outputProc(double value)
 {
-    // values are 10 for 10%
-    value *= 100;
-    _owner->addRPMSetPoint(value);
-
+    if (get_stepwise_info().isCeiling)
+    {
+        _owner->addRPMCeiling(value);
+    }
+    else
+    {
+        _owner->addRPMSetPoint(value);
+    }
     return;
 }
diff --git a/pid/zone.cpp b/pid/zone.cpp
index fc0b40b..065e6d7 100644
--- a/pid/zone.cpp
+++ b/pid/zone.cpp
@@ -65,6 +65,16 @@
     _RPMSetPoints.push_back(setpoint);
 }
 
+void PIDZone::addRPMCeiling(double ceiling)
+{
+    _RPMCeilings.push_back(ceiling);
+}
+
+void PIDZone::clearRPMCeilings(void)
+{
+    _RPMCeilings.clear();
+}
+
 void PIDZone::clearRPMSetPoints(void)
 {
     _RPMSetPoints.clear();
@@ -116,6 +126,12 @@
         max = *result;
     }
 
+    if (_RPMCeilings.size() > 0)
+    {
+        result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
+        max = std::min(max, *result);
+    }
+
     /*
      * If the maximum RPM setpoint output is below the minimum RPM
      * setpoint, set it to the minimum.
diff --git a/pid/zone.hpp b/pid/zone.hpp
index 223f3cb..077d272 100644
--- a/pid/zone.hpp
+++ b/pid/zone.hpp
@@ -28,6 +28,7 @@
 
     virtual double getCachedValue(const std::string& name) = 0;
     virtual void addRPMSetPoint(double setpoint) = 0;
+    virtual void addRPMCeiling(double ceiling) = 0;
     virtual double getMaxRPMRequest() const = 0;
     virtual bool getFailSafeMode() const = 0;
     virtual double getFailSafePercent() const = 0;
@@ -64,7 +65,9 @@
     bool getFailSafeMode(void) const override;
     int64_t getZoneID(void) const;
     void addRPMSetPoint(double setpoint) override;
+    void addRPMCeiling(double ceiling) override;
     void clearRPMSetPoints(void);
+    void clearRPMCeilings(void);
     double getFailSafePercent(void) const override;
     double getMinThermalRPMSetpoint(void) const;
 
@@ -107,6 +110,7 @@
     std::set<std::string> _failSafeSensors;
 
     std::vector<double> _RPMSetPoints;
+    std::vector<double> _RPMCeilings;
     std::vector<std::string> _fanInputs;
     std::vector<std::string> _thermalInputs;
     std::map<std::string, double> _cachedValuesByName;