}
}
-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)
/* 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);
+ if (ret < 0) {
+ mutex_unlock(&st->cmd_lock);
+ return ret;
+ }
+
+ /*
+ * Flush samples currently in the FIFO, especially 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.
+ */
+ 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;
st->param.sensor_odr.roundup = 1;
ret = cros_ec_motion_send_host_cmd(st, 0);
+ if (ret)
+ break;
+
+ /* Flush the FIFO when a sensor is stopped.
+ * 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;
+ ret = cros_ec_motion_send_host_cmd(st, 0);
+ }
break;
default:
ret = -EINVAL;