dragon: sensor: Limit interrupt rate to 50Hz

To allow Ryu to suspend while sensors are set to max frequency, do
not allow the interrupt rate to go above 50Hz.

BUG=b:25425420
TEST=pass cts test.

Change-Id: Ia638a36334298c9e18f146f2500ce900f97b4098
Signed-off-by: Gwendal Grignou <gwendal@google.com>
diff --git a/sensor_hub/cros_ec_sensors.cpp b/sensor_hub/cros_ec_sensors.cpp
index 04b4ee3..6e8eecc 100644
--- a/sensor_hub/cros_ec_sensors.cpp
+++ b/sensor_hub/cros_ec_sensors.cpp
@@ -39,6 +39,10 @@
     return (a < b) ? a : b;
 }
 
+static int max(int a, int b) {
+    return (a < b) ? b : a;
+}
+
 /*
  * Constructor.
  *
@@ -165,10 +169,12 @@
 
         long ec_period = nanoseconds_to_milliseconds(info->max_report_latency_ns);
 
-        if (enabled)
+        if (enabled) {
             ec_period = min(CROS_EC_MAX_SAMPLING_PERIOD, ec_period);
-        else
+            ec_period = max(CROS_EC_MIN_SAMPLING_PERIOD, ec_period);
+        } else {
             ec_period = 0;
+        }
 
         /* Sampling is encoded on a 16bit so, so the maximal period is ~65s. */
         err = cros_ec_sysfs_set_input_attr_by_int(
diff --git a/sensor_hub/cros_ec_sensors.h b/sensor_hub/cros_ec_sensors.h
index d28311d..6849230 100644
--- a/sensor_hub/cros_ec_sensors.h
+++ b/sensor_hub/cros_ec_sensors.h
@@ -34,6 +34,9 @@
 
 #define CROS_EC_MAX_SAMPLING_PERIOD ((1 << 16) - 2)
 
+// Prevent EC interrupt to happen to often.
+#define CROS_EC_MIN_SAMPLING_PERIOD 20
+
 enum {X, Y, Z, MAX_AXIS};
 
 extern const char *cros_ec_sensor_names[];