Skip to contentSkip to conversation

drivers: iio: cros_ec_sensors: Flush changing the FIFO timeout

PATCH In review Gwendal Grignou 1 participant 2 messages Jun 23, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers: iio: cros_ec_sensors: Flush changing the FIFO timeout

From: Gwendal Grignou <[email protected]>

fifo_timeout is used by the EC firmware only when a new sample is available.
When the timeout changes, espcially when the new timeout is shorter than the current one, we need to send the samples waiting in the FIFO.

Change-Id: Iec40367472bd301123c22e38d0ad8aca4ed41379

---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 51 ++++++++++++-------
 1 file changed, 33 insertions(+), 18 deletions(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +33 -18
index bd8483866d046..339faee6ab44b 100644
@@ -105,22 +105,6 @@ static void get_default_min_max_freq(enum motionsensor_type type,
 	}
 }
 
-static int cros_ec_sensor_set_ec_rate(struct cros_ec_sensors_core_state *st,
-				      int rate)
-{
-	int ret;
-
-	if (rate > U16_MAX)
-		rate = U16_MAX;
-
-	mutex_lock(&st->cmd_lock);
-	st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
-	st->param.ec_rate.data = rate;
-	ret = cros_ec_motion_send_host_cmd(st, 0);
-	mutex_unlock(&st->cmd_lock);
-	return ret;
-}
-
 static ssize_t cros_ec_sensor_set_report_latency(struct device *dev,
 						 struct device_attribute *attr,
 						 const char *buf, size_t len)
@@ -136,7 +120,25 @@ static ssize_t cros_ec_sensor_set_report_latency(struct device *dev,
 
 	/* EC rate is in ms. */
 	latency = integer * 1000 + fract / 1000;
-	ret = cros_ec_sensor_set_ec_rate(st, latency);
+
+	mutex_lock(&st->cmd_lock);
+	st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
+	st->param.ec_rate.data = min(U16_MAX, latency);
+	ret = cros_ec_motion_send_host_cmd(st, 0);
+	mutex_unlock(&st->cmd_lock);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Flush samples currently in the FIFO, expecially when the new latency
+	 * is shorter than the old one: new timeout value is only considered when
+	 * there is a new sample available. It can take a while for a slow
+	 * sensor.
+	 */
+	mutex_lock(&st->cmd_lock);
+	st->param.cmd = MOTIONSENSE_CMD_FIFO_FLUSH;
+	ret = cros_ec_motion_send_host_cmd(st, 0);
+	mutex_unlock(&st->cmd_lock);
 	if (ret < 0)
 		return ret;
 
@@ -154,7 +156,6 @@ static ssize_t cros_ec_sensor_get_report_latency(struct device *dev,
 	mutex_lock(&st->cmd_lock);
 	st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
 	st->param.ec_rate.data = EC_MOTION_SENSE_NO_VALUE;
-
 	ret = cros_ec_motion_send_host_cmd(st, 0);
 	latency = st->resp->ec_rate.ret;
 	mutex_unlock(&st->cmd_lock);
@@ -769,6 +770,8 @@ EXPORT_SYMBOL_GPL(cros_ec_sensors_capture);
  * @mask:	specifies which values to be requested
  *
  * Return:	the type of value returned by the device
+ *
+ * cmd_lock mutex held.
  */
 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
 			  struct iio_chan_spec const *chan,
@@ -841,6 +844,8 @@ EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read_avail);
  * @mask:	specifies which values to write
  *
  * Return:	the type of value returned by the device
+ *
+ * cmd_lock mutex held.
  */
 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
 			       struct iio_chan_spec const *chan,
@@ -858,6 +863,16 @@ int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
 		st->param.sensor_odr.roundup = 1;
 
 		ret = cros_ec_motion_send_host_cmd(st, 0);
+
+		/* Flush the FIFO in case we are stopping a sensor.
+		 * If the FIFO has just been emptied, pending samples will be
+		 * stuck until new samples are available. It will not happen
+		 * when all the sensors are stopped.
+		 */
+		if (frequency == 0) {
+			st->param.cmd = MOTIONSENSE_CMD_FIFO_FLUSH;
+			cros_ec_motion_send_host_cmd(st, 0);
+		}
 		break;
 	default:
 		ret = -EINVAL;
Signature · 1 line
2.49.0.472.ge94155a9ec-goog
#1 lore raw

Ignore this patch, I sent the wrong file.

Gwendal.

Quoted 113 lines · Gwendal Grignou

From: Gwendal Grignou <[email protected]>

fifo_timeout is used by the EC firmware only when a new sample is available.
When the timeout changes, espcially when the new timeout is shorter than the current one, we need to send the samples waiting in the FIFO.

Change-Id: Iec40367472bd301123c22e38d0ad8aca4ed41379

---
 .../cros_ec_sensors/cros_ec_sensors_core.c    | 51 ++++++++++++-------
 1 file changed, 33 insertions(+), 18 deletions(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +33 -19
index bd8483866d046..339faee6ab44b 100644
@@ -105,22 +105,6 @@ static void get_default_min_max_freq(enum motionsensor_type type,
        }
 }
 
-static int cros_ec_sensor_set_ec_rate(struct cros_ec_sensors_core_state *st,
-                                     int rate)
-{
-       int ret;
-
-       if (rate > U16_MAX)
-               rate = U16_MAX;
-
-       mutex_lock(&st->cmd_lock);
-       st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
-       st->param.ec_rate.data = rate;
-       ret = cros_ec_motion_send_host_cmd(st, 0);
-       mutex_unlock(&st->cmd_lock);
-       return ret;
-}
-
 static ssize_t cros_ec_sensor_set_report_latency(struct device *dev,
                                                 struct device_attribute *attr,
                                                 const char *buf, size_t len)
@@ -136,7 +120,25 @@ static ssize_t cros_ec_sensor_set_report_latency(struct device *dev,
 
        /* EC rate is in ms. */
        latency = integer * 1000 + fract / 1000;
-       ret = cros_ec_sensor_set_ec_rate(st, latency);
+
+       mutex_lock(&st->cmd_lock);
+       st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
+       st->param.ec_rate.data = min(U16_MAX, latency);
+       ret = cros_ec_motion_send_host_cmd(st, 0);
+       mutex_unlock(&st->cmd_lock);
+       if (ret < 0)
+               return ret;
+
+       /*
+        * Flush samples currently in the FIFO, expecially when the new latency
+        * is shorter than the old one: new timeout value is only considered when
+        * there is a new sample available. It can take a while for a slow
+        * sensor.
+        */
+       mutex_lock(&st->cmd_lock);
+       st->param.cmd = MOTIONSENSE_CMD_FIFO_FLUSH;
+       ret = cros_ec_motion_send_host_cmd(st, 0);
+       mutex_unlock(&st->cmd_lock);
        if (ret < 0)
                return ret;
 
@@ -154,7 +156,6 @@ static ssize_t cros_ec_sensor_get_report_latency(struct device *dev,
        mutex_lock(&st->cmd_lock);
        st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
        st->param.ec_rate.data = EC_MOTION_SENSE_NO_VALUE;
-
        ret = cros_ec_motion_send_host_cmd(st, 0);
        latency = st->resp->ec_rate.ret;
        mutex_unlock(&st->cmd_lock);
@@ -769,6 +770,8 @@ EXPORT_SYMBOL_GPL(cros_ec_sensors_capture);
  * @mask:      specifies which values to be requested
  *
  * Return:     the type of value returned by the device
+ *
+ * cmd_lock mutex held.
  */
 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
                          struct iio_chan_spec const *chan,
@@ -841,6 +844,8 @@ EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read_avail);
  * @mask:      specifies which values to write
  *
  * Return:     the type of value returned by the device
+ *
+ * cmd_lock mutex held.
  */
 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
                               struct iio_chan_spec const *chan,
@@ -858,6 +863,16 @@ int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
                st->param.sensor_odr.roundup = 1;
 
                ret = cros_ec_motion_send_host_cmd(st, 0);
+
+               /* Flush the FIFO in case we are stopping a sensor.
+                * If the FIFO has just been emptied, pending samples will be
+                * stuck until new samples are available. It will not happen
+                * when all the sensors are stopped.
+                */
+               if (frequency == 0) {
+                       st->param.cmd = MOTIONSENSE_CMD_FIFO_FLUSH;
+                       cros_ec_motion_send_host_cmd(st, 0);
+               }
                break;
        default:
                ret = -EINVAL;
--

2.49.0.472.ge94155a9ec-goog