This patchset adds a sensorhub driver for spreading sensor
events coming from the Embedded controller sensor FIFO:
+---------------+ +--------------+ +----
| cros_ec_accel | | cros_ec_gyro | | ...
+---------------+ +--------------+ +----
id:0 \ id:1 | / id:..
+------------------------------+
| cros_ec_sensorhub |
+------------------------------+
| cros_ec_dev |
+------------------------------+
| cros_ec_i2c, cros_ec_lpc, .. |
+------------------------------+
|
EC
When new sensors events are present, the EC raises and interrupt,
sensorhub reads the FIFO and uses the 'id' field to spread the event to
the proper IIO sensors. This stack is similar to the HID sensor input
stack.
The first 3 patches add a primitive cros_ec_sensorhub. MFD just have to
register this driver if at least one sensor is presented by the EC.
cros_ec_sensorhub retrieves more information from the EC to find out
which sensors are actually present:
mfd: cros_ec: Add sensor_count and make check_features public
platform: cros_ec: Add cros_ec_sensor_hub driver
platform/mfd:iio: cros_ec: Register sensor through sensorhub
The next 3 patches prepare for FIFO support:
platform: chrome: cros-ec: record event timestamp in the hard irq
platform: chrome: cros_ec: Do not attempt to register a non-positive
platform: chrome: cros_ec: handle MKBP more events flag
The next 4 patches add FIFO support. An interface is added to connect
the IIO sensors with cros_ec_sensorhub, and filters are needed to spread
the timestamp when the EC send batches of events and deal with variation
in interrupt delay.
platform: chrome: sensorhub: Add FIFO support
platform: chrome: sensorhub: Add code to spread timestmap
platform: chrome: sensorhub: Add median filter
iio: cros_ec: Use triggered buffer only when EC does not support FIFO
Finally, the last 3 patches present sensor information following the IIO
ABI:
- Configurable EC timeout to allow batch mode in buffer/hwfifo_timeout,
in seconds.
- Hard coded EC FIFO size in buffer/hwfifo_watermark_max
- Sensor sampling frequency in hertz at sampling_frequency:
iio: cros_ec: Expose hwfifo_timeout
iio: cros_ec: Report hwfifo_watermark_max
iio: cros_ec: Use Hertz as unit for sampling frequency
For testing, libiio test tools can be used:
A iio device link looks like:
iio:device1 ->
...09:00/GOOG0004:00/cros-ec-dev.6.auto/cros-ec-sensorhub.7.auto/
cros-ec-accel.15.auto/iio:device1
When FIFO is available, no trigger are presented. Once
sampling_freqeuncy and hwfifo_timeout are set, sensor events flow
when listening to /dev/iio:device1:
echo 12 > sampling_frequency # Set ODR to at least 12Hz
echo .100 > buffer/hwfifo_timeout # do not wait more than 100ms to
# to send samples
iio_readdev -b 2 -T 1000 -s 2 iio:device1 2>/dev/null| od -x
0000000 ffd0 2e20 d990 0000 8630 b56c 07ea 0000
0000020 ffc0 2e10 d970 0000 877e b56c 07ea 0000
0000040`
When FIFO is not supported by the EC, a trigger is present in the
directory. After registering a trigger, setting sampling_frequency,
the latest data collected by the sensor will be retrieved by the host
when the trigger expires.
When cros_ec_accel_legacy driver is used, no FIFO is supported and the
sampling frequency for the accelerometers is hard coded at 10Hz.
This set is built upon the master branch of
git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
Enrico Granata (2):
platform: chrome: cros_ec: Do not attempt to register a non-positive
IRQ number
platform: chrome: cros_ec: handle MKBP more events flag
Gwendal Grignou (11):
mfd: cros_ec: Add sensor_count and make check_features public
platform: cros_ec: Add cros_ec_sensor_hub driver
platform/mfd:iio: cros_ec: Register sensor through sensorhub
platform: chrome: cros-ec: record event timestamp in the hard irq
platform: chrome: sensorhub: Add FIFO support
platform: chrome: sensorhub: Add code to spread timestmap
platform: chrome: sensorhub: Add median filter
iio: cros_ec: Use triggered buffer only when EC does not support FIFO
iio: cros_ec: Expose hwfifo_timeout
iio: cros_ec: Report hwfifo_watermark_max
iio: cros_ec: Use Hertz as unit for sampling frequency
drivers/iio/accel/cros_ec_accel_legacy.c | 13 +-
drivers/iio/common/cros_ec_sensors/Kconfig | 2 +-
.../cros_ec_sensors/cros_ec_lid_angle.c | 2 +-
.../common/cros_ec_sensors/cros_ec_sensors.c | 14 +-
.../cros_ec_sensors/cros_ec_sensors_core.c | 251 ++++-
drivers/iio/light/cros_ec_light_prox.c | 18 +-
drivers/iio/pressure/cros_ec_baro.c | 12 +-
drivers/mfd/cros_ec_dev.c | 208 +---
drivers/platform/chrome/Kconfig | 18 +-
drivers/platform/chrome/Makefile | 2 +
drivers/platform/chrome/cros_ec.c | 51 +-
drivers/platform/chrome/cros_ec_lpc.c | 2 +
drivers/platform/chrome/cros_ec_proto.c | 51 +-
drivers/platform/chrome/cros_ec_sensorhub.c | 269 +++++
.../platform/chrome/cros_ec_sensorhub_ring.c | 918 ++++++++++++++++++
.../linux/iio/common/cros_ec_sensors_core.h | 29 +-
include/linux/mfd/cros_ec.h | 17 +
include/linux/platform_data/cros_ec_proto.h | 30 +-
.../linux/platform_data/cros_ec_sensorhub.h | 173 ++++
19 files changed, 1780 insertions(+), 300 deletions(-)
create mode 100644 drivers/platform/chrome/cros_ec_sensorhub.c
create mode 100644 drivers/platform/chrome/cros_ec_sensorhub_ring.c
create mode 100644 include/linux/platform_data/cros_ec_sensorhub.h
--
2.23.0.351.gc4317032e6-goog
To improve sensor timestamp precision, given EC and AP are in
different time domains, the AP needs to try to record the exact
moment an event was signalled to the AP by the EC as soon as
possible after it happens.
First thing in the hard irq is the best place for this.
Signed-off-by: Gwendal Grignou <[email protected]>
---
drivers/platform/chrome/cros_ec.c | 18 +++++++++++++++++-
drivers/platform/chrome/cros_ec_lpc.c | 2 ++
include/linux/platform_data/cros_ec_proto.h | 15 +++++++++++++++
3 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
index fd77e6fa74c2..f49eb1d1e3cd 100644
--- a/drivers/platform/chrome/cros_ec.c
+++ b/drivers/platform/chrome/cros_ec.c
@@ -31,6 +31,21 @@ static struct cros_ec_platform pd_p = {
.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
};
+s64 cros_ec_get_time_ns(void)
+{
+ return ktime_get_boottime_ns();
+}
+EXPORT_SYMBOL(cros_ec_get_time_ns);
+
+static irqreturn_t ec_irq_handler(int irq, void *data)
+{
+ struct cros_ec_device *ec_dev = data;
+
+ ec_dev->last_event_time = cros_ec_get_time_ns();
+
+ return IRQ_WAKE_THREAD;
+}
+
static irqreturn_t ec_irq_thread(int irq, void *data)
{
struct cros_ec_device *ec_dev = data;
@@ -132,7 +147,8 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
}
if (ec_dev->irq) {
- err = devm_request_threaded_irq(dev, ec_dev->irq, NULL,
+ err = devm_request_threaded_irq(
+ dev, ec_dev->irq, ec_irq_handler,
ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
"chromeos-ec", ec_dev);
if (err) {
diff --git a/drivers/platform/chrome/cros_ec_lpc.c b/drivers/platform/chrome/cros_ec_lpc.c
index 7d10d909435f..3c77496e164d 100644
--- a/drivers/platform/chrome/cros_ec_lpc.c
+++ b/drivers/platform/chrome/cros_ec_lpc.c
@@ -313,6 +313,8 @@ static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
{
struct cros_ec_device *ec_dev = data;
+ ec_dev->last_event_time = cros_ec_get_time_ns();
+
if (ec_dev->mkbp_event_supported &&
cros_ec_get_next_event(ec_dev, NULL) > 0)
blocking_notifier_call_chain(&ec_dev->event_notifier, 0,
diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h
index e91e3fcb0348..ab12e28f2107 100644
--- a/include/linux/platform_data/cros_ec_proto.h
+++ b/include/linux/platform_data/cros_ec_proto.h
@@ -121,6 +121,8 @@ struct cros_ec_command {
* @event_data: Raw payload transferred with the MKBP event.
* @event_size: Size in bytes of the event data.
* @host_event_wake_mask: Mask of host events that cause wake from suspend.
+ * @last_event_time: exact time from the hard irq when we got notified of
+ * a new event.
* @ec: The platform_device used by the mfd driver to interface with the
* main EC.
* @pd: The platform_device used by the mfd driver to interface with the
@@ -161,6 +163,7 @@ struct cros_ec_device {
int event_size;
u32 host_event_wake_mask;
u32 last_resume_result;
+ s64 last_event_time;
/* The platform devices used by the mfd driver */
struct platform_device *ec;
@@ -308,4 +311,16 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
*/
u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
+/**
+ * cros_ec_get_time_ns - Return time in ns.
+ *
+ * This is the function used to record the time for last_event_time in struct
+ * cros_ec_device during the hard irq.
+ *
+ * This function is probably implemented using ktime_get_boot_ns(), but it's
+ * exposed here to make sure all cros_ec drivers use the same code path to get
+ * the time.
+ */
+s64 cros_ec_get_time_ns(void);
+
#endif /* __LINUX_CROS_EC_PROTO_H */
--
2.23.0.351.gc4317032e6-goog
EC FIFO can send sensor events in batch. Spread them based on
previous (TSa) and currnet timestamp (TSb)
EC FIFO iio events
+-----------+
| TSa |
+-----------+ +---------------------------------------+
| event 1 | | event 1 | TSb - (TSb - TSa)/n * (n-1) |
+-----------+ +---------------------------------------+
| event 2 | | event 2 | TSb - (TSb - TSa)/n * (n-2) |
+-----------+ +---------------------------------------+
| ... | ------> | .... | |
+-----------+ +---------------------------------------+
| event n-1 | | event 2 | TSb - (TSb - TSa)/n |
+-----------+ +---------------------------------------+
| event n | | event 2 | TSb |
+-----------+ +---------------------------------------+
| TSb |
+-----------+
Signed-off-by: Gwendal Grignou <[email protected]>
---
.../platform/chrome/cros_ec_sensorhub_ring.c | 99 ++++++++++++++++++-
1 file changed, 96 insertions(+), 3 deletions(-)
diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
index 8cd533d5542e..48327e80a5a3 100644
--- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c
+++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
@@ -160,6 +160,97 @@ static bool cros_ec_ring_process_event(
return true;
}
+/*
+ * cros_ec_ring_spread_add: Calculate proper timestamps then
+ * add to ringbuffer (legacy).
+ *
+ * If there is a sample with a proper timestamp
+ * timestamp | count
+ * older_unprocess_out --> TS1 | 1
+ * TS1 | 2
+ * out --> TS1 | 3
+ * next_out --> TS2 |
+ * We spread time for the samples [older_unprocess_out .. out]
+ * between TS1 and TS2: [TS1+1/4, TS1+2/4, TS1+3/4, TS2].
+ *
+ * If we reach the end of the samples, we compare with the
+ * current timestamp:
+ *
+ * older_unprocess_out --> TS1 | 1
+ * TS1 | 2
+ * out --> TS1 | 3
+ * We know have [TS1+1/3, TS1+2/3, current timestamp]
+ */
+static void cros_ec_ring_spread_add(
+ struct cros_ec_sensorhub *sensorhub,
+ unsigned long sensor_mask,
+ s64 current_timestamp,
+ struct cros_ec_sensors_ring_sample *last_out)
+{
+ struct cros_ec_sensors_ring_sample *out;
+ int i;
+
+ for_each_set_bit(i, &sensor_mask, BITS_PER_LONG) {
+ s64 older_timestamp;
+ s64 timestamp;
+ struct cros_ec_sensors_ring_sample *older_unprocess_out =
+ sensorhub->ring;
+ struct cros_ec_sensors_ring_sample *next_out;
+ int count = 1;
+
+ for (out = sensorhub->ring; out < last_out; out = next_out) {
+ s64 time_period;
+
+ next_out = out + 1;
+ if (out->sensor_id != i)
+ continue;
+
+ /* Timestamp to start with */
+ older_timestamp = out->timestamp;
+
+ /* find next sample */
+ while (next_out < last_out && next_out->sensor_id != i)
+ next_out++;
+
+ if (next_out >= last_out) {
+ timestamp = current_timestamp;
+ } else {
+ timestamp = next_out->timestamp;
+ if (timestamp == older_timestamp) {
+ count++;
+ continue;
+ }
+ }
+
+ /*
+ * The next sample has a new timestamp,
+ * spread the unprocessed samples.
+ */
+ if (next_out < last_out)
+ count++;
+ time_period = div_s64(timestamp - older_timestamp,
+ count);
+
+ for (; older_unprocess_out <= out;
+ older_unprocess_out++) {
+ if (older_unprocess_out->sensor_id != i)
+ continue;
+ older_timestamp += time_period;
+ older_unprocess_out->timestamp =
+ older_timestamp;
+ }
+ count = 1;
+ /* The next_out sample has a valid timestamp, skip. */
+ next_out++;
+ older_unprocess_out = next_out;
+ }
+ }
+
+ /* push the event into the kfifo */
+ for (out = sensorhub->ring; out < last_out; out++)
+ cros_sensorhub_send_sample(sensorhub, out);
+}
+
/*
* cros_ec_sensorhub_ring_handler - the trigger handler function
*
@@ -280,9 +371,11 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub)
}
}
- /* push the event into the kfifo */
- for (out = sensorhub->ring; out < last_out; out++)
- cros_sensorhub_send_sample(sensorhub, out);
+ /*
+ * Spread samples in case of batching, then add them to the ringbuffer.
+ */
+ cros_ec_ring_spread_add(sensorhub, sensor_mask,
+ current_timestamp, last_out);
ring_handler_end:
sensorhub->fifo_timestamp[LAST_TS] = current_timestamp;
--
2.23.0.351.gc4317032e6-goog
- Add sensorhub include file
- Remove duplicate code in mfd, since mfd just register
cros_ec_sensorhub if at least one sensor is present
- Change iio cros_ec driver to get the pointer to the cros_ec_dev
through cros_ec_sensorhub.
Signed-off-by: Gwendal Grignou <[email protected]>
---
drivers/iio/accel/cros_ec_accel_legacy.c | 4 +-
.../common/cros_ec_sensors/cros_ec_sensors.c | 4 +-
.../cros_ec_sensors/cros_ec_sensors_core.c | 4 +-
drivers/iio/light/cros_ec_light_prox.c | 6 +-
drivers/mfd/cros_ec_dev.c | 203 ++----------------
drivers/platform/chrome/cros_ec_sensorhub.c | 2 +-
include/linux/platform_data/cros_ec_proto.h | 8 -
.../linux/platform_data/cros_ec_sensorhub.h | 8 +
8 files changed, 34 insertions(+), 205 deletions(-)
diff --git a/drivers/iio/accel/cros_ec_accel_legacy.c b/drivers/iio/accel/cros_ec_accel_legacy.c
index fcc3f999e482..c9af6fa0670d 100644
--- a/drivers/iio/accel/cros_ec_accel_legacy.c
+++ b/drivers/iio/accel/cros_ec_accel_legacy.c
@@ -23,6 +23,7 @@
#include <linux/slab.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
+#include <linux/platform_data/cros_ec_sensorhub.h>
#include <linux/platform_device.h>
#define DRV_NAME "cros-ec-accel-legacy"
@@ -163,7 +164,8 @@ static const struct iio_chan_spec cros_ec_accel_legacy_channels[] = {
static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
- struct cros_ec_dev *ec = dev_get_drvdata(dev->parent);
+ struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
+ struct cros_ec_dev *ec = sensor_hub->ec;
struct iio_dev *indio_dev;
struct cros_ec_sensors_core_state *state;
int ret;
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
index a6987726eeb8..5bd6f54afc42 100644
--- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
@@ -20,6 +20,7 @@
#include <linux/module.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
+#include <linux/platform_data/cros_ec_sensorhub.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
@@ -222,7 +223,8 @@ static const struct iio_info ec_sensors_info = {
static int cros_ec_sensors_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
- struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
+ struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
+ struct cros_ec_dev *ec_dev = sensor_hub->ec;
struct iio_dev *indio_dev;
struct cros_ec_sensors_state *state;
struct iio_chan_spec *channel;
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
index d2609e6feda4..81a7f692de2f 100644
--- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
@@ -18,6 +18,7 @@
#include <linux/slab.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
+#include <linux/platform_data/cros_ec_sensorhub.h>
#include <linux/platform_device.h>
static char *cros_ec_loc[] = {
@@ -88,7 +89,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
{
struct device *dev = &pdev->dev;
struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
- struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
+ struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
+ struct cros_ec_dev *ec = sensor_hub->ec;
struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
u32 ver_mask;
int ret, i;
diff --git a/drivers/iio/light/cros_ec_light_prox.c b/drivers/iio/light/cros_ec_light_prox.c
index c5263b563fc1..205effc1f404 100644
--- a/drivers/iio/light/cros_ec_light_prox.c
+++ b/drivers/iio/light/cros_ec_light_prox.c
@@ -18,6 +18,7 @@
#include <linux/module.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
+#include <linux/platform_data/cros_ec_sensorhub.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
@@ -169,13 +170,14 @@ static const struct iio_info cros_ec_light_prox_info = {
static int cros_ec_light_prox_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
- struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
+ struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
+ struct cros_ec_dev *ec = sensor_hub->ec;
struct iio_dev *indio_dev;
struct cros_ec_light_prox_state *state;
struct iio_chan_spec *channel;
int ret;
- if (!ec_dev || !ec_dev->ec_dev) {
+ if (!ec || !ec->ec_dev) {
dev_warn(dev, "No CROS EC device found.\n");
return -EINVAL;
}
diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index 3be80183ccaa..3a583d3503ca 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -78,6 +78,10 @@ static const struct mfd_cell cros_ec_rtc_cells[] = {
{ .name = "cros-ec-rtc", },
};
+static const struct mfd_cell cros_ec_sensorhub_cells[] = {
+ { .name = "cros-ec-sensorhub", },
+};
+
static const struct mfd_cell cros_usbpd_charger_cells[] = {
{ .name = "cros-usbpd-charger", },
{ .name = "cros-usbpd-logger", },
@@ -208,192 +212,6 @@ int cros_ec_get_sensor_count(struct cros_ec_dev *ec)
}
EXPORT_SYMBOL_GPL(cros_ec_get_sensor_count);
-static void cros_ec_sensors_register(struct cros_ec_dev *ec)
-{
- /*
- * Issue a command to get the number of sensor reported.
- * Build an array of sensors driver and register them all.
- */
- int ret, i, id, sensor_num;
- struct mfd_cell *sensor_cells;
- struct cros_ec_sensor_platform *sensor_platforms;
- int sensor_type[MOTIONSENSE_TYPE_MAX];
- struct ec_params_motion_sense *params;
- struct ec_response_motion_sense *resp;
- struct cros_ec_command *msg;
-
- msg = kzalloc(sizeof(struct cros_ec_command) +
- max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
- if (msg == NULL)
- return;
-
- msg->version = 2;
- msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
- msg->outsize = sizeof(*params);
- msg->insize = sizeof(*resp);
-
- params = (struct ec_params_motion_sense *)msg->data;
- params->cmd = MOTIONSENSE_CMD_DUMP;
-
- ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
- if (ret < 0) {
- dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
- ret, msg->result);
- goto error;
- }
-
- resp = (struct ec_response_motion_sense *)msg->data;
- sensor_num = resp->dump.sensor_count;
- /*
- * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
- */
- sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
- GFP_KERNEL);
- if (sensor_cells == NULL)
- goto error;
-
- sensor_platforms = kcalloc(sensor_num,
- sizeof(struct cros_ec_sensor_platform),
- GFP_KERNEL);
- if (sensor_platforms == NULL)
- goto error_platforms;
-
- memset(sensor_type, 0, sizeof(sensor_type));
- id = 0;
- for (i = 0; i < sensor_num; i++) {
- params->cmd = MOTIONSENSE_CMD_INFO;
- params->info.sensor_num = i;
- ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
- if (ret < 0) {
- dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
- i, ret, msg->result);
- continue;
- }
- switch (resp->info.type) {
- case MOTIONSENSE_TYPE_ACCEL:
- sensor_cells[id].name = "cros-ec-accel";
- break;
- case MOTIONSENSE_TYPE_BARO:
- sensor_cells[id].name = "cros-ec-baro";
- break;
- case MOTIONSENSE_TYPE_GYRO:
- sensor_cells[id].name = "cros-ec-gyro";
- break;
- case MOTIONSENSE_TYPE_MAG:
- sensor_cells[id].name = "cros-ec-mag";
- break;
- case MOTIONSENSE_TYPE_PROX:
- sensor_cells[id].name = "cros-ec-prox";
- break;
- case MOTIONSENSE_TYPE_LIGHT:
- sensor_cells[id].name = "cros-ec-light";
- break;
- case MOTIONSENSE_TYPE_ACTIVITY:
- sensor_cells[id].name = "cros-ec-activity";
- break;
- default:
- dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
- continue;
- }
- sensor_platforms[id].sensor_num = i;
- sensor_cells[id].id = sensor_type[resp->info.type];
- sensor_cells[id].platform_data = &sensor_platforms[id];
- sensor_cells[id].pdata_size =
- sizeof(struct cros_ec_sensor_platform);
-
- sensor_type[resp->info.type]++;
- id++;
- }
-
- if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
- ec->has_kb_wake_angle = true;
-
- if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
- sensor_cells[id].name = "cros-ec-ring";
- id++;
- }
- if (cros_ec_check_features(ec,
- EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
- sensor_cells[id].name = "cros-ec-lid-angle";
- id++;
- }
-
- ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
- NULL, 0, NULL);
- if (ret)
- dev_err(ec->dev, "failed to add EC sensors\n");
-
- kfree(sensor_platforms);
-error_platforms:
- kfree(sensor_cells);
-error:
- kfree(msg);
-}
-
-static struct cros_ec_sensor_platform sensor_platforms[] = {
- { .sensor_num = 0 },
- { .sensor_num = 1 }
-};
-
-static const struct mfd_cell cros_ec_accel_legacy_cells[] = {
- {
- .name = "cros-ec-accel-legacy",
- .platform_data = &sensor_platforms[0],
- .pdata_size = sizeof(struct cros_ec_sensor_platform),
- },
- {
- .name = "cros-ec-accel-legacy",
- .platform_data = &sensor_platforms[1],
- .pdata_size = sizeof(struct cros_ec_sensor_platform),
- }
-};
-
-static void cros_ec_accel_legacy_register(struct cros_ec_dev *ec)
-{
- struct cros_ec_device *ec_dev = ec->ec_dev;
- u8 status;
- int ret;
-
- /*
- * ECs that need legacy support are the main EC, directly connected to
- * the AP.
- */
- if (ec->cmd_offset != 0)
- return;
-
- /*
- * Check if EC supports direct memory reads and if EC has
- * accelerometers.
- */
- if (ec_dev->cmd_readmem) {
- ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS, 1,
- &status);
- if (ret < 0) {
- dev_warn(ec->dev, "EC direct read error.\n");
- return;
- }
-
- /* Check if EC has accelerometers. */
- if (!(status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
- dev_info(ec->dev, "EC does not have accelerometers.\n");
- return;
- }
- }
-
- /*
- * The device may still support accelerometers:
- * it would be an older ARM based device that do not suppor the
- * EC_CMD_GET_FEATURES command.
- *
- * Register 2 accelerometers, we will fail in the IIO driver if there
- * are no sensors.
- */
- ret = mfd_add_hotplug_devices(ec->dev, cros_ec_accel_legacy_cells,
- ARRAY_SIZE(cros_ec_accel_legacy_cells));
- if (ret)
- dev_err(ec_dev->dev, "failed to add EC sensors\n");
-}
-
static int ec_device_probe(struct platform_device *pdev)
{
int retval = -ENOMEM;
@@ -449,11 +267,14 @@ static int ec_device_probe(struct platform_device *pdev)
goto failed;
/* check whether this EC is a sensor hub. */
- if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
- cros_ec_sensors_register(ec);
- else
- /* Workaroud for older EC firmware */
- cros_ec_accel_legacy_register(ec);
+ if (cros_ec_get_sensor_count(ec) > 0) {
+ retval = mfd_add_hotplug_devices(ec->dev,
+ cros_ec_sensorhub_cells,
+ ARRAY_SIZE(cros_ec_sensorhub_cells));
+ if (retval)
+ dev_err(ec->dev, "failed to add %s subdevice: %d\n",
+ cros_ec_sensorhub_cells->name, retval);
+ }
/*
* The following subdevices can be detected by sending the
diff --git a/drivers/platform/chrome/cros_ec_sensorhub.c b/drivers/platform/chrome/cros_ec_sensorhub.c
index 80688018ef66..01f11ed611fb 100644
--- a/drivers/platform/chrome/cros_ec_sensorhub.c
+++ b/drivers/platform/chrome/cros_ec_sensorhub.c
@@ -181,7 +181,7 @@ static int cros_ec_sensorhub_probe(struct platform_device *pdev)
data->ec = ec;
dev_set_drvdata(dev, data);
- /* check whether this EC is a sensor hub. */
+ /* Check whether this EC is a sensor hub. */
if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) {
ret = cros_ec_sensors_register(dev, ec);
} else {
diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h
index eab7036cda09..e91e3fcb0348 100644
--- a/include/linux/platform_data/cros_ec_proto.h
+++ b/include/linux/platform_data/cros_ec_proto.h
@@ -167,14 +167,6 @@ struct cros_ec_device {
struct platform_device *pd;
};
-/**
- * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
- * @sensor_num: Id of the sensor, as reported by the EC.
- */
-struct cros_ec_sensor_platform {
- u8 sensor_num;
-};
-
/**
* struct cros_ec_platform - ChromeOS EC platform information.
* @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
diff --git a/include/linux/platform_data/cros_ec_sensorhub.h b/include/linux/platform_data/cros_ec_sensorhub.h
index a8b64ecf5b9b..9295eabb16f6 100644
--- a/include/linux/platform_data/cros_ec_sensorhub.h
+++ b/include/linux/platform_data/cros_ec_sensorhub.h
@@ -11,6 +11,14 @@
#include <linux/platform_data/cros_ec_commands.h>
+/**
+ * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
+ * @sensor_num: Id of the sensor, as reported by the EC.
+ */
+struct cros_ec_sensor_platform {
+ u8 sensor_num;
+};
+
/**
* struct cros_ec_sensorhub - Sensor Hub device data.
*/
--
2.23.0.351.gc4317032e6-goog
When EC supports FIFO, the samples will flow from the kernel by
themselves.
When no FIFO, the user space app needs to call trigger_new, or better
register a high precision timer.
Signed-off-by: Gwendal Grignou <[email protected]>
---
drivers/iio/accel/cros_ec_accel_legacy.c | 8 +--
.../cros_ec_sensors/cros_ec_lid_angle.c | 2 +-
.../common/cros_ec_sensors/cros_ec_sensors.c | 8 +--
.../cros_ec_sensors/cros_ec_sensors_core.c | 61 +++++++++++++++++--
drivers/iio/light/cros_ec_light_prox.c | 8 +--
drivers/iio/pressure/cros_ec_baro.c | 8 +--
.../linux/iio/common/cros_ec_sensors_core.h | 16 ++++-
7 files changed, 80 insertions(+), 31 deletions(-)
diff --git a/drivers/iio/accel/cros_ec_accel_legacy.c b/drivers/iio/accel/cros_ec_accel_legacy.c
index 591c0d962c44..d607fbc52c95 100644
--- a/drivers/iio/accel/cros_ec_accel_legacy.c
+++ b/drivers/iio/accel/cros_ec_accel_legacy.c
@@ -179,7 +179,8 @@ static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
if (!indio_dev)
return -ENOMEM;
- ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
+ ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
+ cros_ec_sensors_capture, cros_ec_sensors_push_data);
if (ret)
return ret;
@@ -199,11 +200,6 @@ static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
state->sign[CROS_EC_SENSOR_Z] = -1;
}
- ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
- cros_ec_sensors_capture, NULL);
- if (ret)
- return ret;
-
return devm_iio_device_register(dev, indio_dev);
}
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
index 1dcc2a16ab2d..e30a59fcf0f9 100644
--- a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
@@ -97,7 +97,7 @@ static int cros_ec_lid_angle_probe(struct platform_device *pdev)
if (!indio_dev)
return -ENOMEM;
- ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
+ ret = cros_ec_sensors_core_init(pdev, indio_dev, false, NULL, NULL);
if (ret)
return ret;
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
index a88dd8deade9..b78a942ac8e5 100644
--- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
@@ -239,7 +239,8 @@ static int cros_ec_sensors_probe(struct platform_device *pdev)
if (!indio_dev)
return -ENOMEM;
- ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
+ ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
+ cros_ec_sensors_capture, cros_ec_sensors_push_data);
if (ret)
return ret;
@@ -301,11 +302,6 @@ static int cros_ec_sensors_probe(struct platform_device *pdev)
else
state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
- ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
- cros_ec_sensors_capture, NULL);
- if (ret)
- return ret;
-
return devm_iio_device_register(dev, indio_dev);
}
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
index 43eb1d42820e..c4c37c6df301 100644
--- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
@@ -12,6 +12,7 @@
#include <linux/iio/iio.h>
#include <linux/iio/kfifo_buf.h>
#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
#include <linux/kernel.h>
#include <linux/mfd/cros_ec.h>
#include <linux/module.h>
@@ -83,15 +84,50 @@ static void get_default_min_max_freq(enum motionsensor_type type,
}
}
+int cros_ec_sensors_push_data(
+ struct iio_dev *indio_dev,
+ s16 *data,
+ s64 timestamp)
+{
+ struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
+ s16 *out;
+ unsigned int i;
+
+ /*
+ * It can happen if we get a samples before the iio device is fully
+ * registered.
+ */
+ if (!st)
+ return 0;
+
+ /* Ignore samples if the buffer is not set. */
+ if (!indio_dev->active_scan_mask)
+ return 0;
+
+ out = (s16 *)st->samples;
+ for_each_set_bit(i,
+ indio_dev->active_scan_mask,
+ indio_dev->masklength) {
+ *out = data[i];
+ out++;
+ }
+ iio_push_to_buffers_with_timestamp(indio_dev, st->samples, timestamp);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(cros_ec_sensors_push_data);
+
int cros_ec_sensors_core_init(struct platform_device *pdev,
struct iio_dev *indio_dev,
- bool physical_device)
+ bool physical_device,
+ cros_ec_sensors_capture_t trigger_capture,
+ cros_ec_sensorhub_push_data_cb_t push_data)
{
struct device *dev = &pdev->dev;
struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
struct cros_ec_dev *ec = sensor_hub->ec;
struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
+ struct iio_buffer *buffer;
u32 ver_mask;
int ret, i;
@@ -124,8 +160,6 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
indio_dev->name = pdev->name;
if (physical_device) {
- indio_dev->modes = INDIO_DIRECT_MODE;
-
state->param.cmd = MOTIONSENSE_CMD_INFO;
state->param.info.sensor_num = sensor_platform->sensor_num;
ret = cros_ec_motion_send_host_cmd(state, 0);
@@ -154,9 +188,26 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
state->frequencies[2] =
state->resp->info_3.max_frequency;
}
- }
- return 0;
+ if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
+ buffer = devm_iio_kfifo_allocate(dev);
+ if (!buffer)
+ return -ENOMEM;
+
+ iio_device_attach_buffer(indio_dev, buffer);
+ indio_dev->modes = INDIO_BUFFER_SOFTWARE;
+ ret = cros_ec_sensorhub_register_push_data(
+ sensor_hub,
+ sensor_platform->sensor_num,
+ indio_dev, push_data);
+ } else {
+ indio_dev->modes = INDIO_DIRECT_MODE;
+ ret = devm_iio_triggered_buffer_setup(
+ dev, indio_dev, NULL,
+ trigger_capture, NULL);
+ }
+ }
+ return ret;
}
EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init);
diff --git a/drivers/iio/light/cros_ec_light_prox.c b/drivers/iio/light/cros_ec_light_prox.c
index c431e4d1482d..d58f010880ce 100644
--- a/drivers/iio/light/cros_ec_light_prox.c
+++ b/drivers/iio/light/cros_ec_light_prox.c
@@ -186,7 +186,8 @@ static int cros_ec_light_prox_probe(struct platform_device *pdev)
if (!indio_dev)
return -ENOMEM;
- ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
+ ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
+ cros_ec_sensors_capture, cros_ec_sensors_push_data);
if (ret)
return ret;
@@ -245,11 +246,6 @@ static int cros_ec_light_prox_probe(struct platform_device *pdev)
state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
- ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
- cros_ec_sensors_capture, NULL);
- if (ret)
- return ret;
-
return devm_iio_device_register(dev, indio_dev);
}
diff --git a/drivers/iio/pressure/cros_ec_baro.c b/drivers/iio/pressure/cros_ec_baro.c
index 2f4d6d3ab41d..c7538e93d24f 100644
--- a/drivers/iio/pressure/cros_ec_baro.c
+++ b/drivers/iio/pressure/cros_ec_baro.c
@@ -134,7 +134,8 @@ static int cros_ec_baro_probe(struct platform_device *pdev)
if (!indio_dev)
return -ENOMEM;
- ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
+ ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
+ cros_ec_sensors_capture, cros_ec_sensors_push_data);
if (ret)
return ret;
@@ -180,11 +181,6 @@ static int cros_ec_baro_probe(struct platform_device *pdev)
state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
- ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
- cros_ec_sensors_capture, NULL);
- if (ret)
- return ret;
-
return devm_iio_device_register(dev, indio_dev);
}
diff --git a/include/linux/iio/common/cros_ec_sensors_core.h b/include/linux/iio/common/cros_ec_sensors_core.h
index abef66d0e884..60f78f0e4884 100644
--- a/include/linux/iio/common/cros_ec_sensors_core.h
+++ b/include/linux/iio/common/cros_ec_sensors_core.h
@@ -12,6 +12,7 @@
#include <linux/irqreturn.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
+#include <linux/platform_data/cros_ec_sensorhub.h>
enum {
CROS_EC_SENSOR_X,
@@ -32,6 +33,9 @@ enum {
/* Minimum sampling period to use when device is suspending */
#define CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY 1000 /* 1 second */
+typedef irqreturn_t (*cros_ec_sensors_capture_t)(int irq, void *p);
+
+
/**
* struct cros_ec_sensors_core_state - state data for EC sensors IIO driver
* @ec: cros EC device structure
@@ -110,11 +114,17 @@ struct platform_device;
* @pdev: platform device created for the sensors
* @indio_dev: iio device structure of the device
* @physical_device: true if the device refers to a physical device
+ * @trigger_capture: function pointer to call buffer is triggered,
+ * for backward compatibility.
+ * @push_data: function to call when cros_ec_sensorhub receives
+ * a sample for that sensor.
*
* Return: 0 on success, -errno on failure.
*/
int cros_ec_sensors_core_init(struct platform_device *pdev,
- struct iio_dev *indio_dev, bool physical_device);
+ struct iio_dev *indio_dev, bool physical_device,
+ cros_ec_sensors_capture_t trigger_capture,
+ cros_ec_sensorhub_push_data_cb_t push_data);
/* To remove association of physical device to cros_ec_sensorhub. */
int cros_ec_sensors_core_clean(struct platform_device *pdev);
@@ -132,6 +142,10 @@ int cros_ec_sensors_core_clean(struct platform_device *pdev);
* Return: IRQ_HANDLED
*/
irqreturn_t cros_ec_sensors_capture(int irq, void *p);
+int cros_ec_sensors_push_data(
+ struct iio_dev *indio_dev,
+ s16 *data,
+ s64 timestamp);
/**
* cros_ec_motion_send_host_cmd() - send motion sense host command
--
2.23.0.351.gc4317032e6-goog
From: Enrico Granata <[email protected]>
Add a layer of sanity checking to cros_ec_register against attempting to
register IRQ values that are not strictly greater than 0.
Signed-off-by: Enrico Granata <[email protected]>
Signed-off-by: Enrico Granata <[email protected]>
Signed-off-by: Gwendal Grignou <[email protected]>
---
drivers/platform/chrome/cros_ec.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
index f49eb1d1e3cd..9c8dc7cdb2b7 100644
--- a/drivers/platform/chrome/cros_ec.c
+++ b/drivers/platform/chrome/cros_ec.c
@@ -146,7 +146,7 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
return err;
}
- if (ec_dev->irq) {
+ if (ec_dev->irq > 0) {
err = devm_request_threaded_irq(
dev, ec_dev->irq, ec_irq_handler,
ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
--
2.23.0.351.gc4317032e6-goog
Events are timestamped in EC time space, their timestamps need to be
converted in host time space.
The assumption is the time delta between when the interrupt is sent
by the EC and when it is receive by the host is a [small] constant.
This is not always true, even with hard-wired interrupt. To mitigate
worst offenders, add a median filter to weed out bigger than expected
delays.
Signed-off-by: Gwendal Grignou <[email protected]>
---
.../platform/chrome/cros_ec_sensorhub_ring.c | 485 +++++++++++++++++-
.../linux/platform_data/cros_ec_sensorhub.h | 65 +++
2 files changed, 533 insertions(+), 17 deletions(-)
diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
index 48327e80a5a3..9955c80d0907 100644
--- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c
+++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
@@ -69,9 +69,11 @@ int cros_ec_sensorhub_ring_fifo_toggle(
struct cros_ec_sensorhub *sensorhub,
bool on)
{
- int ret;
+ int ret, i;
mutex_lock(&sensorhub->cmd_lock);
+ for (i = 0; i < CROS_EC_SENSOR_MAX; i++)
+ sensorhub->last_batch_len[i] = 0;
sensorhub->params->cmd = MOTIONSENSE_CMD_FIFO_INT_ENABLE;
sensorhub->params->fifo_int_enable.enable = on;
@@ -86,6 +88,227 @@ int cros_ec_sensorhub_ring_fifo_toggle(
return ret;
}
+static int cros_ec_ring_median_cmp(const void *pv1, const void *pv2)
+{
+ s64 v1 = *(s64 *)pv1;
+ s64 v2 = *(s64 *)pv2;
+
+ if (v1 > v2)
+ return 1;
+ else if (v1 < v2)
+ return -1;
+ else
+ return 0;
+}
+
+/*
+ * cros_ec_ring_median: Gets median of an array of numbers
+ *
+ * For now it's implemented using an inefficient > O(n) sort then return
+ * the middle element. A more optimal method would be something like
+ * quickselect, but given that n = 64 we can probably live with it in the
+ * name of clarity.
+ *
+ * Warning: the input array gets modified (sorted)!
+ */
+static s64 cros_ec_ring_median(s64 *array, size_t length)
+{
+ sort(array, length, sizeof(s64), cros_ec_ring_median_cmp, NULL);
+ return array[length / 2];
+}
+
+/*
+ * IRQ Timestamp Filtering
+ *
+ * Lower down in cros_ec_ring_process_event(), for each sensor event we have to
+ * calculate it's timestamp in the AP timebase. There are 3 time points:
+ * a - EC timebase, sensor event
+ * b - EC timebase, IRQ
+ * c - AP timebase, IRQ
+ * a' - what we want: sensor even in AP timebase
+ *
+ * While a and b are recorded at accurate times (due to the EC real time
+ * nature); c is pretty untrustworthy, even though it's recorded the
+ * first thing in ec_irq_handler(). There is a very good change we'll get
+ * added lantency due to:
+ * other irqs
+ * ddrfreq
+ * cpuidle
+ *
+ * Normally a' = c - b + a, but if we do that naive math any jitter in c
+ * will get coupled in a', which we don't want. We want a function
+ * a' = cros_ec_ring_ts_filter(a) which will filter out outliers in c.
+ *
+ * Think of a graph of AP time(b) on the y axis vs EC time(c) on the x axis.
+ * The slope of the line won't be exactly 1, there will be some clock drift
+ * between the 2 chips for various reasons (mechanical stress, temperature,
+ * voltage). We need to extrapolate values for a future x, without trusting
+ * recent y values too much.
+ *
+ * We use a median filter for the slope, then another median filter for the
+ * y-intercept to calculate this function:
+ * dx[n] = x[n-1] - x[n]
+ * dy[n] = x[n-1] - x[n]
+ * m[n] = dy[n] / dx[n]
+ * median_m = median(m[n-k:n])
+ * error[i] = y[n-i] - median_m * x[n-i]
+ * median_error = median(error[:k])
+ * predicted_y = median_m * x + median_error
+ *
+ * Implementation differences from above:
+ * - Redefined y to be actually c - b, this gives us a lot more precision
+ * to do the math. (c-b)/b variations are more obvious than c/b variations.
+ * - Since we don't have floating point, any operations involving slope are
+ * done using fixed point math (*M_PRECISION)
+ * - Since x and y grow with time, we keep zeroing the graph (relative to
+ * the last sample), this way math involving *x[n-i] will not overflow
+ * - EC timestamps are kept in us, it improves the slope calculation precision
+ */
+
+/*
+ * cros_ec_ring_ts_filter_update: Given a new IRQ timestamp pair (EC and
+ * AP timebases), add it to the filter history.
+ *
+ * @b IRQ timestamp, EC timebase (us)
+ * @c IRQ timestamp, AP timebase (ns)
+ */
+static void cros_ec_ring_ts_filter_update(
+ struct cros_ec_sensors_ts_filter_state *state,
+ s64 b, s64 c)
+{
+ s64 x, y;
+ s64 dx, dy;
+ s64 m; /* stored as *M_PRECISION */
+ s64 *m_history_copy = state->temp_buf;
+ s64 *error = state->temp_buf;
+ int i;
+
+ /* we trust b the most, that'll be our independent variable */
+ x = b;
+ /* y is the offset between AP and EC times, in ns */
+ y = c - b * 1000;
+
+ dx = (state->x_history[0] + state->x_offset) - x;
+ if (dx == 0)
+ return; /* we already have this irq in the history */
+ dy = (state->y_history[0] + state->y_offset) - y;
+ m = div64_s64(dy * M_PRECISION, dx);
+
+ /* Empty filter if we haven't seen any action in a while. */
+ if (-dx > TS_HISTORY_BORED_US)
+ state->history_len = 0;
+
+ /* Move everything over, also update offset to all absolute coords .*/
+ for (i = state->history_len - 1; i >= 1; i--) {
+ state->x_history[i] = state->x_history[i-1] + dx;
+ state->y_history[i] = state->y_history[i-1] + dy;
+
+ state->m_history[i] = state->m_history[i-1];
+ /*
+ * Also use the same loop to copy m_history for future
+ * median extraction.
+ */
+ m_history_copy[i] = state->m_history[i-1];
+ }
+
+ /* Store the x and y, but remember offset is actually last sample. */
+ state->x_offset = x;
+ state->y_offset = y;
+ state->x_history[0] = 0;
+ state->y_history[0] = 0;
+
+ state->m_history[0] = m;
+ m_history_copy[0] = m;
+
+ if (state->history_len < TS_HISTORY_SIZE)
+ state->history_len++;
+
+ /* Precalculate things for the filter. */
+ if (state->history_len > TS_HISTORY_THRESHOLD) {
+ state->median_m =
+ cros_ec_ring_median(m_history_copy, state->history_len - 1);
+
+ /*
+ * Calculate y-intercepts as if m_median is the slope and
+ * points in the history are on the line. median_error will
+ * still be in the offset coordinate system.
+ */
+ for (i = 0; i < state->history_len; i++)
+ error[i] = state->y_history[i] -
+ div_s64(state->median_m * state->x_history[i],
+ M_PRECISION);
+ state->median_error =
+ cros_ec_ring_median(error, state->history_len);
+ } else {
+ state->median_m = 0;
+ state->median_error = 0;
+ }
+}
+
+/*
+ * cros_ec_ring_ts_filter: Translate EC timebase timestamp to AP timebase
+ *
+ * @x any ec timestamp (us):
+ *
+ * cros_ec_ring_ts_filter(a) => a' event timestamp, AP timebase
+ * cros_ec_ring_ts_filter(b) => calculated timestamp when the EC IRQ
+ * should have happened on the AP, with low jitter
+ *
+ * @returns timestamp in AP timebase (ns)
+ *
+ * Note: The filter will only activate once state->history_len goes
+ * over TS_HISTORY_THRESHOLD. Otherwise it'll just do the naive c - b + a
+ * transform.
+ *
+ * How to derive the formula, starting from:
+ * f(x) = median_m * x + median_error
+ * That's the calculated AP - EC offset (at the x point in time)
+ * Undo the coordinate system transform:
+ * f(x) = median_m * (x - x_offset) + median_error + y_offset
+ * Remember to undo the "y = c - b * 1000" modification:
+ * f(x) = median_m * (x - x_offset) + median_error + y_offset + x * 1000
+ */
+static s64 cros_ec_ring_ts_filter(struct cros_ec_sensors_ts_filter_state *state,
+ s64 x)
+{
+ return div_s64(state->median_m * (x - state->x_offset), M_PRECISION)
+ + state->median_error + state->y_offset + x * 1000;
+}
+
+/*
+ * Since a and b were originally 32 bit values from the EC,
+ * they overflow relatively often, casting is not enough, so we need to
+ * add an offset.
+ */
+static void cros_ec_ring_fix_overflow(s64 *ts,
+ const s64 overflow_period,
+ struct cros_ec_sensors_ec_overflow_state *state)
+{
+ s64 adjust;
+
+ *ts += state->offset;
+ if (abs(state->last - *ts) > (overflow_period / 2)) {
+ adjust = state->last > *ts ? overflow_period : -overflow_period;
+ state->offset += adjust;
+ *ts += adjust;
+ }
+ state->last = *ts;
+}
+
+static void cros_ec_ring_check_for_past_timestamp(
+ struct cros_ec_sensorhub *sensorhub,
+ struct cros_ec_sensors_ring_sample *sample)
+{
+ const u8 sensor_id = sample->sensor_id;
+
+ // if this event is earlier than one we saw before...
+ if (sensorhub->newest_sensor_event[sensor_id] > sample->timestamp)
+ // mark it for spreading
+ sample->timestamp = sensorhub->last_batch_timestamp[sensor_id];
+ else
+ sensorhub->newest_sensor_event[sensor_id] = sample->timestamp;
+}
+
/*
* cros_ec_ring_process_event: process one EC FIFO event
*
@@ -117,25 +340,47 @@ static bool cros_ec_ring_process_event(
s64 a = in->timestamp;
s64 b = fifo_info->info.timestamp;
s64 c = fifo_timestamp;
- s64 new_timestamp;
+ cros_ec_ring_fix_overflow(&a, 1LL << 32,
+ &sensorhub->overflow_a);
+ cros_ec_ring_fix_overflow(&b, 1LL << 32,
+ &sensorhub->overflow_b);
+
+ if (sensorhub->tight_timestamps) {
+ cros_ec_ring_ts_filter_update(&sensorhub->filter, b, c);
+ *current_timestamp =
+ cros_ec_ring_ts_filter(&sensorhub->filter, a);
+ } else {
+ s64 new_timestamp;
+ /*
+ * disable filtering since we might add more jitter
+ * if b is in a random point in time
+ */
+ new_timestamp = c - b * 1000 + a * 1000;
+ /*
+ * The timestamp can be stale if we had to use the fifo
+ * info timestamp.
+ */
+ if (new_timestamp - *current_timestamp > 0)
+ *current_timestamp = new_timestamp;
+ }
+ }
+
+ if (in->flags & MOTIONSENSE_SENSOR_FLAG_ODR) {
+ sensorhub->last_batch_len[in->sensor_num] =
+ sensorhub->penultimate_batch_len[in->sensor_num] = 0;
/*
- * disable filtering since we might add more jitter
- * if b is in a random point in time
- */
- new_timestamp = c - b * 1000 + a * 1000;
- /*
- * The timestamp can be stale if we had to use the fifo
- * info timestamp.
+ * ODR change is only useful for the sensor_ring, it does not
+ * convey information to clients.
*/
- if (new_timestamp - *current_timestamp > 0)
- *current_timestamp = new_timestamp;
+ return false;
}
if (in->flags & MOTIONSENSE_SENSOR_FLAG_FLUSH) {
out->sensor_id = in->sensor_num;
out->timestamp = *current_timestamp;
out->flag = in->flags;
+ sensorhub->last_batch_len[out->sensor_id] = 0;
/*
* No other payload information provided with
* flush ack.
@@ -149,7 +394,22 @@ static bool cros_ec_ring_process_event(
/* Regular sample */
out->sensor_id = in->sensor_num;
if (*current_timestamp - now > 0) {
- /* If the timestamp is in the future. */
+ /*
+ * This fix is needed to overcome the timestamp filter putting
+ * events in the future.
+ */
+ sensorhub->future_timestamp_total_ns +=
+ *current_timestamp - now;
+ if (++sensorhub->future_timestamp_count ==
+ FUTURE_TS_ANALYTICS_COUNT_MAX) {
+ s64 avg = div_s64(sensorhub->future_timestamp_total_ns,
+ sensorhub->future_timestamp_count);
+ dev_warn(sensorhub->dev,
+ "100 timestamps in the future, %lldns shaved on average\n",
+ avg);
+ sensorhub->future_timestamp_count = 0;
+ sensorhub->future_timestamp_total_ns = 0;
+ }
out->timestamp = now;
} else {
out->timestamp = *current_timestamp;
@@ -157,13 +417,195 @@ static bool cros_ec_ring_process_event(
out->flag = in->flags;
for (axis = 0; axis < 3; axis++)
out->vector[axis] = in->data[axis];
+ if (sensorhub->tight_timestamps)
+ cros_ec_ring_check_for_past_timestamp(sensorhub, out);
return true;
}
/*
- * cros_ec_ring_spread_add: Calculate proper timestamps then
+ * cros_ec_ring_spread_add: Calculate proper timestamps then add to ringbuffer.
+ *
+ * Note: This is the new spreading code, assumes every sample's timestamp
+ * preceeds the sample. Run if tight_timestamps == true.
+ *
+ * Sometimes the EC receives only one interrupt (hence timestamp) for
+ * a batch of samples. Only the first sample will have the correct
+ * timestamp. So we must interpolate the other samples.
+ * We use the previous batch timestamp and our current batch timestamp
+ * as a way to calculate period, then spread the samples evenly.
+ *
+ * s0 int, 0ms
+ * s1 int, 10ms
+ * s2 int, 20ms
+ * 30ms point goes by, no interrupt, previous one is still asserted
+ * downloading s2 and s3
+ * s3 sample, 20ms (incorrect timestamp)
+ * s4 int, 40ms
+ *
+ * The batches are [(s0), (s1), (s2, s3), (s4)]. Since the 3rd batch
+ * has 2 samples in them, we adjust the timestamp of s3.
+ * s2 - s1 = 10ms, so s3 must be s2 + 10ms => 20ms. If s1 would have
+ * been part of a bigger batch things would have gotten a little
+ * more complicated.
+ *
+ * Note: we also assume another sensor sample doesn't break up a batch
+ * in 2 or more partitions. Example, there can't ever be a sync sensor
+ * in between S2 and S3. This simplifies the following code.
+ */
+static void cros_ec_ring_spread_add(
+ struct cros_ec_sensorhub *sensorhub,
+ unsigned long sensor_mask,
+ struct cros_ec_sensors_ring_sample *last_out)
+{
+ struct cros_ec_sensors_ring_sample *batch_start, *next_batch_start;
+ int id;
+
+ for_each_set_bit(id, &sensor_mask, BITS_PER_LONG) {
+ for (batch_start = sensorhub->ring; batch_start < last_out;
+ batch_start = next_batch_start) {
+ /*
+ * For each batch (where all samples have the same
+ * timestamp).
+ */
+ int batch_len, sample_idx;
+ struct cros_ec_sensors_ring_sample *batch_end =
+ batch_start;
+ struct cros_ec_sensors_ring_sample *s;
+ s64 batch_timestamp = batch_start->timestamp;
+ s64 sample_period;
+
+ /*
+ * Skip over batches that start with the sensor types
+ * we're not looking at right now.
+ */
+ if (batch_start->sensor_id != id) {
+ next_batch_start = batch_start + 1;
+ continue;
+ }
+
+ /*
+ * TODO(gwendal): can not send out flush packets
+ * anymore.
+ * Do not start a batch
+ * from a flush, as it happens asynchronously to the
+ * regular flow of events.
+ */
+ if (batch_start->flag &
+ MOTIONSENSE_SENSOR_FLAG_FLUSH) {
+ next_batch_start = batch_start + 1;
+ continue;
+ }
+
+ if (batch_start->timestamp <=
+ sensorhub->last_batch_timestamp[id]) {
+
+ batch_timestamp =
+ sensorhub->last_batch_timestamp[id];
+ batch_len = sensorhub->last_batch_len[id];
+
+ sample_idx = batch_len;
+
+ sensorhub->last_batch_timestamp[id] =
+ sensorhub->penultimate_batch_timestamp[id];
+ sensorhub->last_batch_len[id] =
+ sensorhub->penultimate_batch_len[id];
+ } else {
+ /*
+ * Push first sample in the batch to the,
+ * kifo, it's guaranteed to be correct, the
+ * rest will follow later on.
+ */
+ sample_idx = batch_len = 1;
+ cros_sensorhub_send_sample(
+ sensorhub, batch_start);
+ batch_start++;
+ }
+
+ /* Find all samples have the same timestamp. */
+ for (s = batch_start; s < last_out; s++) {
+ if (s->sensor_id != id)
+ /*
+ * Skip over other sensor types that
+ * are interleaved, don't count them.
+ */
+ continue;
+ if (s->timestamp != batch_timestamp)
+ /* we discovered the next batch */
+ break;
+ if (s->flag & MOTIONSENSE_SENSOR_FLAG_FLUSH)
+ /* break on flush packets */
+ break;
+ batch_end = s;
+ batch_len++;
+ }
+
+ if (batch_len == 1)
+ goto done_with_this_batch;
+
+ /* Can we calculate period? */
+ if (sensorhub->last_batch_len[id] == 0) {
+ dev_warn(sensorhub->dev, "Sensor %d: lost %d samples when spreading\n",
+ id, batch_len - 1);
+ goto done_with_this_batch;
+ /*
+ * Note: we're dropping the rest of the samples
+ * in this batch since we have no idea where
+ * they're supposed to go without a period
+ * calculation.
+ */
+ }
+
+ sample_period = div_s64(batch_timestamp -
+ sensorhub->last_batch_timestamp[id],
+ sensorhub->last_batch_len[id]);
+ dev_dbg(sensorhub->dev,
+ "Adjusting %d samples, sensor %d last_batch @%lld (%d samples) batch_timestamp=%lld => period=%lld\n",
+ batch_len, id,
+ sensorhub->last_batch_timestamp[id],
+ sensorhub->last_batch_len[id],
+ batch_timestamp,
+ sample_period);
+
+ /*
+ * Adjust timestamps of the samples then push them to
+ * kfifo.
+ */
+ for (s = batch_start; s <= batch_end; s++) {
+ if (s->sensor_id != id)
+ /*
+ * Skip over other sensor types that
+ * are interleaved, don't change them.
+ */
+ continue;
+
+ s->timestamp = batch_timestamp +
+ sample_period * sample_idx;
+ sample_idx++;
+
+ cros_sensorhub_send_sample(sensorhub, s);
+ }
+
+done_with_this_batch:
+ sensorhub->penultimate_batch_timestamp[id] =
+ sensorhub->last_batch_timestamp[id];
+ sensorhub->penultimate_batch_len[id] =
+ sensorhub->last_batch_len[id];
+
+ sensorhub->last_batch_timestamp[id] = batch_timestamp;
+ sensorhub->last_batch_len[id] = batch_len;
+
+ next_batch_start = batch_end + 1;
+ }
+ }
+}
+
+/*
+ * cros_ec_ring_spread_add_legacy: Calculate proper timestamps then
* add to ringbuffer (legacy).
*
+ * Note: This assumes we're running old firmware, where every sample's timestamp
+ * is after the sample. Run if tight_timestamps == false.
+ *
* If there is a sample with a proper timestamp
* timestamp | count
* older_unprocess_out --> TS1 | 1
@@ -181,7 +623,7 @@ static bool cros_ec_ring_process_event(
* out --> TS1 | 3
* We know have [TS1+1/3, TS1+2/3, current timestamp]
*/
-static void cros_ec_ring_spread_add(
+static void cros_ec_ring_spread_add_legacy(
struct cros_ec_sensorhub *sensorhub,
unsigned long sensor_mask,
s64 current_timestamp,
@@ -355,7 +797,8 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub)
* the AP is slow to respond to the IRQ, the EC may have added new
* samples. Use the FIFO info timestamp as last timestamp then.
*/
- if ((last_out-1)->timestamp == current_timestamp)
+ if (!sensorhub->tight_timestamps &&
+ (last_out-1)->timestamp == current_timestamp)
current_timestamp = fifo_timestamp;
/* Warn on lost samples. */
@@ -367,6 +810,7 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub)
dev_warn(sensorhub->dev,
"Sensor %d: lost: %d out of %d\n", i,
lost, fifo_info->info.total_lost);
+ sensorhub->last_batch_len[i] = 0;
}
}
}
@@ -374,8 +818,11 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub)
/*
* Spread samples in case of batching, then add them to the ringbuffer.
*/
- cros_ec_ring_spread_add(sensorhub, sensor_mask,
- current_timestamp, last_out);
+ if (sensorhub->tight_timestamps)
+ cros_ec_ring_spread_add(sensorhub, sensor_mask, last_out);
+ else
+ cros_ec_ring_spread_add_legacy(sensorhub, sensor_mask,
+ current_timestamp, last_out);
ring_handler_end:
sensorhub->fifo_timestamp[LAST_TS] = current_timestamp;
@@ -436,6 +883,10 @@ int cros_ec_sensorhub_ring_add(struct cros_ec_sensorhub *sensorhub)
sensorhub->fifo_timestamp[LAST_TS] = cros_ec_get_time_ns();
+ sensorhub->tight_timestamps = cros_ec_check_features(ec,
+ EC_FEATURE_MOTION_SENSE_TIGHT_TIMESTAMPS);
+
+
/* register the notifier that will act as a top half interrupt. */
sensorhub->notifier.notifier_call = cros_ec_sensorhub_event;
ret = blocking_notifier_chain_register(&ec->ec_dev->event_notifier,
diff --git a/include/linux/platform_data/cros_ec_sensorhub.h b/include/linux/platform_data/cros_ec_sensorhub.h
index 18cda568c58a..df67f2015da9 100644
--- a/include/linux/platform_data/cros_ec_sensorhub.h
+++ b/include/linux/platform_data/cros_ec_sensorhub.h
@@ -53,6 +53,42 @@ struct cros_ec_sensors_ring_sample {
s64 timestamp;
} __packed;
+/* State used for cros_ec_ring_fix_overflow */
+struct cros_ec_sensors_ec_overflow_state {
+ s64 offset;
+ s64 last;
+};
+
+/* Precision of fixed point for the m values from the filter */
+#define M_PRECISION (1 << 23)
+
+/* Length of the filter, how long to remember entries for */
+#define TS_HISTORY_SIZE 64
+
+/* Only activate the filter once we have at least this many elements. */
+#define TS_HISTORY_THRESHOLD 8
+
+/*
+ * If we don't have any history entries for this long, empty the filter to
+ * make sure there are no big discontinuities.
+ */
+#define TS_HISTORY_BORED_US 500000
+
+struct cros_ec_sensors_ts_filter_state {
+ s64 x_offset, y_offset;
+ s64 x_history[TS_HISTORY_SIZE]; /* stored relative to x_offset */
+ s64 y_history[TS_HISTORY_SIZE]; /* stored relative to y_offset */
+ s64 m_history[TS_HISTORY_SIZE]; /* stored as *M_PRECISION */
+ int history_len;
+
+ s64 temp_buf[TS_HISTORY_SIZE];
+
+ s64 median_m;
+ s64 median_error;
+};
+
+#define FUTURE_TS_ANALYTICS_COUNT_MAX 100
+
/**
* struct cros_ec_sensorhub - Sensor Hub device data.
*/
@@ -76,6 +112,35 @@ struct cros_ec_sensorhub {
struct cros_ec_fifo_info fifo_info;
int fifo_size;
+ /* Used for timestamp spreading calculations when a batch shows up */
+ s64 penultimate_batch_timestamp[CROS_EC_SENSOR_MAX];
+ int penultimate_batch_len[CROS_EC_SENSOR_MAX];
+ s64 last_batch_timestamp[CROS_EC_SENSOR_MAX];
+ int last_batch_len[CROS_EC_SENSOR_MAX];
+ s64 newest_sensor_event[CROS_EC_SENSOR_MAX];
+
+ struct cros_ec_sensors_ec_overflow_state overflow_a;
+ struct cros_ec_sensors_ec_overflow_state overflow_b;
+
+ struct cros_ec_sensors_ts_filter_state filter;
+
+ /*
+ * The timestamps reported from the EC have low jitter.
+ * Timestamps also come before every sample.
+ * Set either by feature bits coming from the EC or userspace.
+ */
+ bool tight_timestamps;
+
+ /*
+ * Statistics used to compute shaved time. This occures when
+ * timestamp interpolation from EC time to AP time accidentally
+ * puts timestamps in the future. These timestamps are clamped
+ * to `now` and these count/total_ns maintain the statistics for
+ * how much time was removed in a given period..
+ */
+ s32 future_timestamp_count;
+ s64 future_timestamp_total_ns;
+
/*
* Dynamic array to be able to spread datum to iio sensor objects.
*/
--
2.23.0.351.gc4317032e6-goog
Hi Gwendal,
Some comments below.
On 22/9/19 19:50, Gwendal Grignou wrote:
> To improve sensor timestamp precision, given EC and AP are in
> different time domains, the AP needs to try to record the exact
> moment an event was signalled to the AP by the EC as soon as
> possible after it happens.
>
> First thing in the hard irq is the best place for this.
>
> Signed-off-by: Gwendal Grignou <[email protected]>
> ---
> drivers/platform/chrome/cros_ec.c | 18 +++++++++++++++++-
> drivers/platform/chrome/cros_ec_lpc.c | 2 ++
> include/linux/platform_data/cros_ec_proto.h | 15 +++++++++++++++
> 3 files changed, 34 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
> index fd77e6fa74c2..f49eb1d1e3cd 100644
> --- a/drivers/platform/chrome/cros_ec.c
> +++ b/drivers/platform/chrome/cros_ec.c
> @@ -31,6 +31,21 @@ static struct cros_ec_platform pd_p = {
> .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
> };
>
> +s64 cros_ec_get_time_ns(void)
> +{
> + return ktime_get_boottime_ns();
> +}
> +EXPORT_SYMBOL(cros_ec_get_time_ns);
> +
That's a simple wrapper only to make sure all cros_ec drivers use the same code
path, right? So maybe instead of doing this we can just add an inline function
in the header like this
/**
* cros_ec_get_time_ns - Get time since boot including time spend in suspend
*
* Return: ktime_t format since boot.
*/
static inline ktime_t cros_ec_get_time_ns(void)
{
return ktime_get_boottime_ns();
}
I'd use ktime_t instead of s64 also.
Thanks,
Enric
> +static irqreturn_t ec_irq_handler(int irq, void *data)
> +{
> + struct cros_ec_device *ec_dev = data;
> +
> + ec_dev->last_event_time = cros_ec_get_time_ns();
> +
> + return IRQ_WAKE_THREAD;
> +}
> +
> static irqreturn_t ec_irq_thread(int irq, void *data)
> {
> struct cros_ec_device *ec_dev = data;
> @@ -132,7 +147,8 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
> }
>
> if (ec_dev->irq) {
> - err = devm_request_threaded_irq(dev, ec_dev->irq, NULL,
> + err = devm_request_threaded_irq(
> + dev, ec_dev->irq, ec_irq_handler,
> ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
> "chromeos-ec", ec_dev);
> if (err) {
> diff --git a/drivers/platform/chrome/cros_ec_lpc.c b/drivers/platform/chrome/cros_ec_lpc.c
> index 7d10d909435f..3c77496e164d 100644
> --- a/drivers/platform/chrome/cros_ec_lpc.c
> +++ b/drivers/platform/chrome/cros_ec_lpc.c
> @@ -313,6 +313,8 @@ static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
> {
> struct cros_ec_device *ec_dev = data;
>
> + ec_dev->last_event_time = cros_ec_get_time_ns();
> +
> if (ec_dev->mkbp_event_supported &&
> cros_ec_get_next_event(ec_dev, NULL) > 0)
> blocking_notifier_call_chain(&ec_dev->event_notifier, 0,
> diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h
> index e91e3fcb0348..ab12e28f2107 100644
> --- a/include/linux/platform_data/cros_ec_proto.h
> +++ b/include/linux/platform_data/cros_ec_proto.h
> @@ -121,6 +121,8 @@ struct cros_ec_command {
> * @event_data: Raw payload transferred with the MKBP event.
> * @event_size: Size in bytes of the event data.
> * @host_event_wake_mask: Mask of host events that cause wake from suspend.
> + * @last_event_time: exact time from the hard irq when we got notified of
> + * a new event.
> * @ec: The platform_device used by the mfd driver to interface with the
> * main EC.
> * @pd: The platform_device used by the mfd driver to interface with the
> @@ -161,6 +163,7 @@ struct cros_ec_device {
> int event_size;
> u32 host_event_wake_mask;
> u32 last_resume_result;
> + s64 last_event_time;
>
> /* The platform devices used by the mfd driver */
> struct platform_device *ec;
> @@ -308,4 +311,16 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
> */
> u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
>
> +/**
> + * cros_ec_get_time_ns - Return time in ns.
> + *
> + * This is the function used to record the time for last_event_time in struct
> + * cros_ec_device during the hard irq.
> + *
> + * This function is probably implemented using ktime_get_boot_ns(), but it's
> + * exposed here to make sure all cros_ec drivers use the same code path to get
> + * the time.
> + */
> +s64 cros_ec_get_time_ns(void);
> +
> #endif /* __LINUX_CROS_EC_PROTO_H */
>
Hi Gwendal and Enrico
On 22/9/19 19:50, Gwendal Grignou wrote:
> From: Enrico Granata <[email protected]>
>
> Add a layer of sanity checking to cros_ec_register against attempting to
> register IRQ values that are not strictly greater than 0.
>
> Signed-off-by: Enrico Granata <[email protected]>
I'll get rid of this signed-off as is the same as following
> Signed-off-by: Enrico Granata <[email protected]>
> Signed-off-by: Gwendal Grignou <[email protected]>
For my own reference:
Reviewed-for-chrome-by: Enric Balletbo i Serra <[email protected]>
> ---
> drivers/platform/chrome/cros_ec.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
> index f49eb1d1e3cd..9c8dc7cdb2b7 100644
> --- a/drivers/platform/chrome/cros_ec.c
> +++ b/drivers/platform/chrome/cros_ec.c
> @@ -146,7 +146,7 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
> return err;
> }
>
> - if (ec_dev->irq) {
> + if (ec_dev->irq > 0) {
> err = devm_request_threaded_irq(
> dev, ec_dev->irq, ec_irq_handler,
> ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
>
On Sun, 22 Sep 2019 10:50:08 -0700
Gwendal Grignou <[email protected]> wrote:
> This patchset adds a sensorhub driver for spreading sensor
> events coming from the Embedded controller sensor FIFO:
>
> +---------------+ +--------------+ +----
> | cros_ec_accel | | cros_ec_gyro | | ...
> +---------------+ +--------------+ +----
> id:0 \ id:1 | / id:..
> +------------------------------+
> | cros_ec_sensorhub |
> +------------------------------+
> | cros_ec_dev |
> +------------------------------+
> | cros_ec_i2c, cros_ec_lpc, .. |
> +------------------------------+
> |
> EC
>
> When new sensors events are present, the EC raises and interrupt,
> sensorhub reads the FIFO and uses the 'id' field to spread the event to
> the proper IIO sensors. This stack is similar to the HID sensor input
> stack.
>
> The first 3 patches add a primitive cros_ec_sensorhub. MFD just have to
> register this driver if at least one sensor is presented by the EC.
> cros_ec_sensorhub retrieves more information from the EC to find out
> which sensors are actually present:
> mfd: cros_ec: Add sensor_count and make check_features public
> platform: cros_ec: Add cros_ec_sensor_hub driver
> platform/mfd:iio: cros_ec: Register sensor through sensorhub
>
> The next 3 patches prepare for FIFO support:
> platform: chrome: cros-ec: record event timestamp in the hard irq
> platform: chrome: cros_ec: Do not attempt to register a non-positive
> platform: chrome: cros_ec: handle MKBP more events flag
>
> The next 4 patches add FIFO support. An interface is added to connect
> the IIO sensors with cros_ec_sensorhub, and filters are needed to spread
> the timestamp when the EC send batches of events and deal with variation
> in interrupt delay.
> platform: chrome: sensorhub: Add FIFO support
> platform: chrome: sensorhub: Add code to spread timestmap
> platform: chrome: sensorhub: Add median filter
> iio: cros_ec: Use triggered buffer only when EC does not support FIFO
>
> Finally, the last 3 patches present sensor information following the IIO
> ABI:
> - Configurable EC timeout to allow batch mode in buffer/hwfifo_timeout,
> in seconds.
> - Hard coded EC FIFO size in buffer/hwfifo_watermark_max
> - Sensor sampling frequency in hertz at sampling_frequency:
> iio: cros_ec: Expose hwfifo_timeout
> iio: cros_ec: Report hwfifo_watermark_max
> iio: cros_ec: Use Hertz as unit for sampling frequency
>
> For testing, libiio test tools can be used:
> A iio device link looks like:
> iio:device1 ->
> ...09:00/GOOG0004:00/cros-ec-dev.6.auto/cros-ec-sensorhub.7.auto/
> cros-ec-accel.15.auto/iio:device1
>
> When FIFO is available, no trigger are presented. Once
> sampling_freqeuncy and hwfifo_timeout are set, sensor events flow
> when listening to /dev/iio:device1:
> echo 12 > sampling_frequency # Set ODR to at least 12Hz
> echo .100 > buffer/hwfifo_timeout # do not wait more than 100ms to
> # to send samples
> iio_readdev -b 2 -T 1000 -s 2 iio:device1 2>/dev/null| od -x
> 0000000 ffd0 2e20 d990 0000 8630 b56c 07ea 0000
> 0000020 ffc0 2e10 d970 0000 877e b56c 07ea 0000
> 0000040`
>
> When FIFO is not supported by the EC, a trigger is present in the
> directory. After registering a trigger, setting sampling_frequency,
> the latest data collected by the sensor will be retrieved by the host
> when the trigger expires.
This is the only bit that makes me wonder... For other devices we have
supported allowing triggers even when there is a fifo present. I assume
we can disable the fifo if necessary? In that case we use the hwfifo
path if no trigger is provided, but if there is one switch back to
the handling for when the hardware doesn't support such a fifo.
Could that model be useful here?
Thanks,
Jonathan
>
> When cros_ec_accel_legacy driver is used, no FIFO is supported and the
> sampling frequency for the accelerometers is hard coded at 10Hz.
>
> This set is built upon the master branch of
> git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
>
>
> Enrico Granata (2):
> platform: chrome: cros_ec: Do not attempt to register a non-positive
> IRQ number
> platform: chrome: cros_ec: handle MKBP more events flag
>
> Gwendal Grignou (11):
> mfd: cros_ec: Add sensor_count and make check_features public
> platform: cros_ec: Add cros_ec_sensor_hub driver
> platform/mfd:iio: cros_ec: Register sensor through sensorhub
> platform: chrome: cros-ec: record event timestamp in the hard irq
> platform: chrome: sensorhub: Add FIFO support
> platform: chrome: sensorhub: Add code to spread timestmap
> platform: chrome: sensorhub: Add median filter
> iio: cros_ec: Use triggered buffer only when EC does not support FIFO
> iio: cros_ec: Expose hwfifo_timeout
> iio: cros_ec: Report hwfifo_watermark_max
> iio: cros_ec: Use Hertz as unit for sampling frequency
>
> drivers/iio/accel/cros_ec_accel_legacy.c | 13 +-
> drivers/iio/common/cros_ec_sensors/Kconfig | 2 +-
> .../cros_ec_sensors/cros_ec_lid_angle.c | 2 +-
> .../common/cros_ec_sensors/cros_ec_sensors.c | 14 +-
> .../cros_ec_sensors/cros_ec_sensors_core.c | 251 ++++-
> drivers/iio/light/cros_ec_light_prox.c | 18 +-
> drivers/iio/pressure/cros_ec_baro.c | 12 +-
> drivers/mfd/cros_ec_dev.c | 208 +---
> drivers/platform/chrome/Kconfig | 18 +-
> drivers/platform/chrome/Makefile | 2 +
> drivers/platform/chrome/cros_ec.c | 51 +-
> drivers/platform/chrome/cros_ec_lpc.c | 2 +
> drivers/platform/chrome/cros_ec_proto.c | 51 +-
> drivers/platform/chrome/cros_ec_sensorhub.c | 269 +++++
> .../platform/chrome/cros_ec_sensorhub_ring.c | 918 ++++++++++++++++++
> .../linux/iio/common/cros_ec_sensors_core.h | 29 +-
> include/linux/mfd/cros_ec.h | 17 +
> include/linux/platform_data/cros_ec_proto.h | 30 +-
> .../linux/platform_data/cros_ec_sensorhub.h | 173 ++++
> 19 files changed, 1780 insertions(+), 300 deletions(-)
> create mode 100644 drivers/platform/chrome/cros_ec_sensorhub.c
> create mode 100644 drivers/platform/chrome/cros_ec_sensorhub_ring.c
> create mode 100644 include/linux/platform_data/cros_ec_sensorhub.h
>
On Sun, 22 Sep 2019 10:50:11 -0700
Gwendal Grignou <[email protected]> wrote:
> - Add sensorhub include file
> - Remove duplicate code in mfd, since mfd just register
> cros_ec_sensorhub if at least one sensor is present
> - Change iio cros_ec driver to get the pointer to the cros_ec_dev
> through cros_ec_sensorhub.
>
> Signed-off-by: Gwendal Grignou <[email protected]>
Trivial comment inline.
Thanks,
Jonathan
> ---
> drivers/iio/accel/cros_ec_accel_legacy.c | 4 +-
> .../common/cros_ec_sensors/cros_ec_sensors.c | 4 +-
> .../cros_ec_sensors/cros_ec_sensors_core.c | 4 +-
> drivers/iio/light/cros_ec_light_prox.c | 6 +-
> drivers/mfd/cros_ec_dev.c | 203 ++----------------
> drivers/platform/chrome/cros_ec_sensorhub.c | 2 +-
> include/linux/platform_data/cros_ec_proto.h | 8 -
> .../linux/platform_data/cros_ec_sensorhub.h | 8 +
> 8 files changed, 34 insertions(+), 205 deletions(-)
>
> diff --git a/drivers/iio/accel/cros_ec_accel_legacy.c b/drivers/iio/accel/cros_ec_accel_legacy.c
> index fcc3f999e482..c9af6fa0670d 100644
> --- a/drivers/iio/accel/cros_ec_accel_legacy.c
> +++ b/drivers/iio/accel/cros_ec_accel_legacy.c
> @@ -23,6 +23,7 @@
> #include <linux/slab.h>
> #include <linux/platform_data/cros_ec_commands.h>
> #include <linux/platform_data/cros_ec_proto.h>
> +#include <linux/platform_data/cros_ec_sensorhub.h>
> #include <linux/platform_device.h>
>
> #define DRV_NAME "cros-ec-accel-legacy"
> @@ -163,7 +164,8 @@ static const struct iio_chan_spec cros_ec_accel_legacy_channels[] = {
> static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
> {
> struct device *dev = &pdev->dev;
> - struct cros_ec_dev *ec = dev_get_drvdata(dev->parent);
> + struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
> + struct cros_ec_dev *ec = sensor_hub->ec;
> struct iio_dev *indio_dev;
> struct cros_ec_sensors_core_state *state;
> int ret;
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
> index a6987726eeb8..5bd6f54afc42 100644
> --- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
> @@ -20,6 +20,7 @@
> #include <linux/module.h>
> #include <linux/platform_data/cros_ec_commands.h>
> #include <linux/platform_data/cros_ec_proto.h>
> +#include <linux/platform_data/cros_ec_sensorhub.h>
> #include <linux/platform_device.h>
> #include <linux/slab.h>
>
> @@ -222,7 +223,8 @@ static const struct iio_info ec_sensors_info = {
> static int cros_ec_sensors_probe(struct platform_device *pdev)
> {
> struct device *dev = &pdev->dev;
> - struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
> + struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
> + struct cros_ec_dev *ec_dev = sensor_hub->ec;
> struct iio_dev *indio_dev;
> struct cros_ec_sensors_state *state;
> struct iio_chan_spec *channel;
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> index d2609e6feda4..81a7f692de2f 100644
> --- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> @@ -18,6 +18,7 @@
> #include <linux/slab.h>
> #include <linux/platform_data/cros_ec_commands.h>
> #include <linux/platform_data/cros_ec_proto.h>
> +#include <linux/platform_data/cros_ec_sensorhub.h>
> #include <linux/platform_device.h>
>
> static char *cros_ec_loc[] = {
> @@ -88,7 +89,8 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
> {
> struct device *dev = &pdev->dev;
> struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
> - struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
> + struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
> + struct cros_ec_dev *ec = sensor_hub->ec;
> struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
> u32 ver_mask;
> int ret, i;
> diff --git a/drivers/iio/light/cros_ec_light_prox.c b/drivers/iio/light/cros_ec_light_prox.c
> index c5263b563fc1..205effc1f404 100644
> --- a/drivers/iio/light/cros_ec_light_prox.c
> +++ b/drivers/iio/light/cros_ec_light_prox.c
> @@ -18,6 +18,7 @@
> #include <linux/module.h>
> #include <linux/platform_data/cros_ec_commands.h>
> #include <linux/platform_data/cros_ec_proto.h>
> +#include <linux/platform_data/cros_ec_sensorhub.h>
> #include <linux/platform_device.h>
> #include <linux/slab.h>
>
> @@ -169,13 +170,14 @@ static const struct iio_info cros_ec_light_prox_info = {
> static int cros_ec_light_prox_probe(struct platform_device *pdev)
> {
> struct device *dev = &pdev->dev;
> - struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
> + struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
> + struct cros_ec_dev *ec = sensor_hub->ec;
> struct iio_dev *indio_dev;
> struct cros_ec_light_prox_state *state;
> struct iio_chan_spec *channel;
> int ret;
>
> - if (!ec_dev || !ec_dev->ec_dev) {
> + if (!ec || !ec->ec_dev) {
> dev_warn(dev, "No CROS EC device found.\n");
> return -EINVAL;
> }
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index 3be80183ccaa..3a583d3503ca 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -78,6 +78,10 @@ static const struct mfd_cell cros_ec_rtc_cells[] = {
> { .name = "cros-ec-rtc", },
> };
>
> +static const struct mfd_cell cros_ec_sensorhub_cells[] = {
> + { .name = "cros-ec-sensorhub", },
> +};
> +
> static const struct mfd_cell cros_usbpd_charger_cells[] = {
> { .name = "cros-usbpd-charger", },
> { .name = "cros-usbpd-logger", },
> @@ -208,192 +212,6 @@ int cros_ec_get_sensor_count(struct cros_ec_dev *ec)
> }
> EXPORT_SYMBOL_GPL(cros_ec_get_sensor_count);
>
> -static void cros_ec_sensors_register(struct cros_ec_dev *ec)
> -{
> - /*
> - * Issue a command to get the number of sensor reported.
> - * Build an array of sensors driver and register them all.
> - */
> - int ret, i, id, sensor_num;
> - struct mfd_cell *sensor_cells;
> - struct cros_ec_sensor_platform *sensor_platforms;
> - int sensor_type[MOTIONSENSE_TYPE_MAX];
> - struct ec_params_motion_sense *params;
> - struct ec_response_motion_sense *resp;
> - struct cros_ec_command *msg;
> -
> - msg = kzalloc(sizeof(struct cros_ec_command) +
> - max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
> - if (msg == NULL)
> - return;
> -
> - msg->version = 2;
> - msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
> - msg->outsize = sizeof(*params);
> - msg->insize = sizeof(*resp);
> -
> - params = (struct ec_params_motion_sense *)msg->data;
> - params->cmd = MOTIONSENSE_CMD_DUMP;
> -
> - ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
> - if (ret < 0) {
> - dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
> - ret, msg->result);
> - goto error;
> - }
> -
> - resp = (struct ec_response_motion_sense *)msg->data;
> - sensor_num = resp->dump.sensor_count;
> - /*
> - * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
> - */
> - sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
> - GFP_KERNEL);
> - if (sensor_cells == NULL)
> - goto error;
> -
> - sensor_platforms = kcalloc(sensor_num,
> - sizeof(struct cros_ec_sensor_platform),
> - GFP_KERNEL);
> - if (sensor_platforms == NULL)
> - goto error_platforms;
> -
> - memset(sensor_type, 0, sizeof(sensor_type));
> - id = 0;
> - for (i = 0; i < sensor_num; i++) {
> - params->cmd = MOTIONSENSE_CMD_INFO;
> - params->info.sensor_num = i;
> - ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
> - if (ret < 0) {
> - dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
> - i, ret, msg->result);
> - continue;
> - }
> - switch (resp->info.type) {
> - case MOTIONSENSE_TYPE_ACCEL:
> - sensor_cells[id].name = "cros-ec-accel";
> - break;
> - case MOTIONSENSE_TYPE_BARO:
> - sensor_cells[id].name = "cros-ec-baro";
> - break;
> - case MOTIONSENSE_TYPE_GYRO:
> - sensor_cells[id].name = "cros-ec-gyro";
> - break;
> - case MOTIONSENSE_TYPE_MAG:
> - sensor_cells[id].name = "cros-ec-mag";
> - break;
> - case MOTIONSENSE_TYPE_PROX:
> - sensor_cells[id].name = "cros-ec-prox";
> - break;
> - case MOTIONSENSE_TYPE_LIGHT:
> - sensor_cells[id].name = "cros-ec-light";
> - break;
> - case MOTIONSENSE_TYPE_ACTIVITY:
> - sensor_cells[id].name = "cros-ec-activity";
> - break;
> - default:
> - dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
> - continue;
> - }
> - sensor_platforms[id].sensor_num = i;
> - sensor_cells[id].id = sensor_type[resp->info.type];
> - sensor_cells[id].platform_data = &sensor_platforms[id];
> - sensor_cells[id].pdata_size =
> - sizeof(struct cros_ec_sensor_platform);
> -
> - sensor_type[resp->info.type]++;
> - id++;
> - }
> -
> - if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
> - ec->has_kb_wake_angle = true;
> -
> - if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
> - sensor_cells[id].name = "cros-ec-ring";
> - id++;
> - }
> - if (cros_ec_check_features(ec,
> - EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> - sensor_cells[id].name = "cros-ec-lid-angle";
> - id++;
> - }
> -
> - ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
> - NULL, 0, NULL);
> - if (ret)
> - dev_err(ec->dev, "failed to add EC sensors\n");
> -
> - kfree(sensor_platforms);
> -error_platforms:
> - kfree(sensor_cells);
> -error:
> - kfree(msg);
> -}
> -
> -static struct cros_ec_sensor_platform sensor_platforms[] = {
> - { .sensor_num = 0 },
> - { .sensor_num = 1 }
> -};
> -
> -static const struct mfd_cell cros_ec_accel_legacy_cells[] = {
> - {
> - .name = "cros-ec-accel-legacy",
> - .platform_data = &sensor_platforms[0],
> - .pdata_size = sizeof(struct cros_ec_sensor_platform),
> - },
> - {
> - .name = "cros-ec-accel-legacy",
> - .platform_data = &sensor_platforms[1],
> - .pdata_size = sizeof(struct cros_ec_sensor_platform),
> - }
> -};
> -
> -static void cros_ec_accel_legacy_register(struct cros_ec_dev *ec)
> -{
> - struct cros_ec_device *ec_dev = ec->ec_dev;
> - u8 status;
> - int ret;
> -
> - /*
> - * ECs that need legacy support are the main EC, directly connected to
> - * the AP.
> - */
> - if (ec->cmd_offset != 0)
> - return;
> -
> - /*
> - * Check if EC supports direct memory reads and if EC has
> - * accelerometers.
> - */
> - if (ec_dev->cmd_readmem) {
> - ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS, 1,
> - &status);
> - if (ret < 0) {
> - dev_warn(ec->dev, "EC direct read error.\n");
> - return;
> - }
> -
> - /* Check if EC has accelerometers. */
> - if (!(status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
> - dev_info(ec->dev, "EC does not have accelerometers.\n");
> - return;
> - }
> - }
> -
> - /*
> - * The device may still support accelerometers:
> - * it would be an older ARM based device that do not suppor the
> - * EC_CMD_GET_FEATURES command.
> - *
> - * Register 2 accelerometers, we will fail in the IIO driver if there
> - * are no sensors.
> - */
> - ret = mfd_add_hotplug_devices(ec->dev, cros_ec_accel_legacy_cells,
> - ARRAY_SIZE(cros_ec_accel_legacy_cells));
> - if (ret)
> - dev_err(ec_dev->dev, "failed to add EC sensors\n");
> -}
> -
> static int ec_device_probe(struct platform_device *pdev)
> {
> int retval = -ENOMEM;
> @@ -449,11 +267,14 @@ static int ec_device_probe(struct platform_device *pdev)
> goto failed;
>
> /* check whether this EC is a sensor hub. */
> - if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
> - cros_ec_sensors_register(ec);
> - else
> - /* Workaroud for older EC firmware */
> - cros_ec_accel_legacy_register(ec);
> + if (cros_ec_get_sensor_count(ec) > 0) {
> + retval = mfd_add_hotplug_devices(ec->dev,
> + cros_ec_sensorhub_cells,
> + ARRAY_SIZE(cros_ec_sensorhub_cells));
> + if (retval)
> + dev_err(ec->dev, "failed to add %s subdevice: %d\n",
> + cros_ec_sensorhub_cells->name, retval);
> + }
>
> /*
> * The following subdevices can be detected by sending the
> diff --git a/drivers/platform/chrome/cros_ec_sensorhub.c b/drivers/platform/chrome/cros_ec_sensorhub.c
> index 80688018ef66..01f11ed611fb 100644
> --- a/drivers/platform/chrome/cros_ec_sensorhub.c
> +++ b/drivers/platform/chrome/cros_ec_sensorhub.c
> @@ -181,7 +181,7 @@ static int cros_ec_sensorhub_probe(struct platform_device *pdev)
> data->ec = ec;
> dev_set_drvdata(dev, data);
>
> - /* check whether this EC is a sensor hub. */
> + /* Check whether this EC is a sensor hub. */
Change not related to this patches main purpose...
> if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) {
> ret = cros_ec_sensors_register(dev, ec);
> } else {
> diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h
> index eab7036cda09..e91e3fcb0348 100644
> --- a/include/linux/platform_data/cros_ec_proto.h
> +++ b/include/linux/platform_data/cros_ec_proto.h
> @@ -167,14 +167,6 @@ struct cros_ec_device {
> struct platform_device *pd;
> };
>
> -/**
> - * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
> - * @sensor_num: Id of the sensor, as reported by the EC.
> - */
> -struct cros_ec_sensor_platform {
> - u8 sensor_num;
> -};
> -
> /**
> * struct cros_ec_platform - ChromeOS EC platform information.
> * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
> diff --git a/include/linux/platform_data/cros_ec_sensorhub.h b/include/linux/platform_data/cros_ec_sensorhub.h
> index a8b64ecf5b9b..9295eabb16f6 100644
> --- a/include/linux/platform_data/cros_ec_sensorhub.h
> +++ b/include/linux/platform_data/cros_ec_sensorhub.h
> @@ -11,6 +11,14 @@
>
> #include <linux/platform_data/cros_ec_commands.h>
>
> +/**
> + * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
> + * @sensor_num: Id of the sensor, as reported by the EC.
> + */
> +struct cros_ec_sensor_platform {
> + u8 sensor_num;
> +};
> +
> /**
> * struct cros_ec_sensorhub - Sensor Hub device data.
> */
On Sun, 22 Sep 2019 10:50:12 -0700
Gwendal Grignou <[email protected]> wrote:
> To improve sensor timestamp precision, given EC and AP are in
> different time domains, the AP needs to try to record the exact
> moment an event was signalled to the AP by the EC as soon as
> possible after it happens.
>
> First thing in the hard irq is the best place for this.
>
> Signed-off-by: Gwendal Grignou <[email protected]>
If this is only going to be used in IIO paths, the there is a control
to set which clock is used. This is really a legacy thing due to
me once picking a silly default, but we may be causing confusion by
having that control but it having no effect.
Otherwise looks good to me.
> ---
> drivers/platform/chrome/cros_ec.c | 18 +++++++++++++++++-
> drivers/platform/chrome/cros_ec_lpc.c | 2 ++
> include/linux/platform_data/cros_ec_proto.h | 15 +++++++++++++++
> 3 files changed, 34 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
> index fd77e6fa74c2..f49eb1d1e3cd 100644
> --- a/drivers/platform/chrome/cros_ec.c
> +++ b/drivers/platform/chrome/cros_ec.c
> @@ -31,6 +31,21 @@ static struct cros_ec_platform pd_p = {
> .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
> };
>
> +s64 cros_ec_get_time_ns(void)
> +{
> + return ktime_get_boottime_ns();
> +}
> +EXPORT_SYMBOL(cros_ec_get_time_ns);
> +
> +static irqreturn_t ec_irq_handler(int irq, void *data)
> +{
> + struct cros_ec_device *ec_dev = data;
> +
> + ec_dev->last_event_time = cros_ec_get_time_ns();
> +
> + return IRQ_WAKE_THREAD;
> +}
> +
> static irqreturn_t ec_irq_thread(int irq, void *data)
> {
> struct cros_ec_device *ec_dev = data;
> @@ -132,7 +147,8 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
> }
>
> if (ec_dev->irq) {
> - err = devm_request_threaded_irq(dev, ec_dev->irq, NULL,
> + err = devm_request_threaded_irq(
> + dev, ec_dev->irq, ec_irq_handler,
> ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
> "chromeos-ec", ec_dev);
> if (err) {
> diff --git a/drivers/platform/chrome/cros_ec_lpc.c b/drivers/platform/chrome/cros_ec_lpc.c
> index 7d10d909435f..3c77496e164d 100644
> --- a/drivers/platform/chrome/cros_ec_lpc.c
> +++ b/drivers/platform/chrome/cros_ec_lpc.c
> @@ -313,6 +313,8 @@ static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
> {
> struct cros_ec_device *ec_dev = data;
>
> + ec_dev->last_event_time = cros_ec_get_time_ns();
> +
> if (ec_dev->mkbp_event_supported &&
> cros_ec_get_next_event(ec_dev, NULL) > 0)
> blocking_notifier_call_chain(&ec_dev->event_notifier, 0,
> diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h
> index e91e3fcb0348..ab12e28f2107 100644
> --- a/include/linux/platform_data/cros_ec_proto.h
> +++ b/include/linux/platform_data/cros_ec_proto.h
> @@ -121,6 +121,8 @@ struct cros_ec_command {
> * @event_data: Raw payload transferred with the MKBP event.
> * @event_size: Size in bytes of the event data.
> * @host_event_wake_mask: Mask of host events that cause wake from suspend.
> + * @last_event_time: exact time from the hard irq when we got notified of
> + * a new event.
> * @ec: The platform_device used by the mfd driver to interface with the
> * main EC.
> * @pd: The platform_device used by the mfd driver to interface with the
> @@ -161,6 +163,7 @@ struct cros_ec_device {
> int event_size;
> u32 host_event_wake_mask;
> u32 last_resume_result;
> + s64 last_event_time;
>
> /* The platform devices used by the mfd driver */
> struct platform_device *ec;
> @@ -308,4 +311,16 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
> */
> u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
>
> +/**
> + * cros_ec_get_time_ns - Return time in ns.
> + *
> + * This is the function used to record the time for last_event_time in struct
> + * cros_ec_device during the hard irq.
> + *
> + * This function is probably implemented using ktime_get_boot_ns(), but it's
> + * exposed here to make sure all cros_ec drivers use the same code path to get
> + * the time.
> + */
> +s64 cros_ec_get_time_ns(void);
> +
> #endif /* __LINUX_CROS_EC_PROTO_H */
On Sun, 22 Sep 2019 10:50:16 -0700
Gwendal Grignou <[email protected]> wrote:
> EC FIFO can send sensor events in batch. Spread them based on
> previous (TSa) and currnet timestamp (TSb)
>
> EC FIFO iio events
> +-----------+
> | TSa |
> +-----------+ +---------------------------------------+
> | event 1 | | event 1 | TSb - (TSb - TSa)/n * (n-1) |
> +-----------+ +---------------------------------------+
> | event 2 | | event 2 | TSb - (TSb - TSa)/n * (n-2) |
> +-----------+ +---------------------------------------+
> | ... | ------> | .... | |
> +-----------+ +---------------------------------------+
> | event n-1 | | event 2 | TSb - (TSb - TSa)/n |
> +-----------+ +---------------------------------------+
> | event n | | event 2 | TSb |
> +-----------+ +---------------------------------------+
> | TSb |
> +-----------+
>
> Signed-off-by: Gwendal Grignou <[email protected]>
Looks fine to me and the docs are nice :)
Timestamps for batches are always a pita.
Reviewed-by: Jonathan Cameron <[email protected]>
> ---
> .../platform/chrome/cros_ec_sensorhub_ring.c | 99 ++++++++++++++++++-
> 1 file changed, 96 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
> index 8cd533d5542e..48327e80a5a3 100644
> --- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c
> +++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
> @@ -160,6 +160,97 @@ static bool cros_ec_ring_process_event(
> return true;
> }
>
> +/*
> + * cros_ec_ring_spread_add: Calculate proper timestamps then
> + * add to ringbuffer (legacy).
> + *
> + * If there is a sample with a proper timestamp
> + * timestamp | count
> + * older_unprocess_out --> TS1 | 1
> + * TS1 | 2
> + * out --> TS1 | 3
> + * next_out --> TS2 |
> + * We spread time for the samples [older_unprocess_out .. out]
> + * between TS1 and TS2: [TS1+1/4, TS1+2/4, TS1+3/4, TS2].
> + *
> + * If we reach the end of the samples, we compare with the
> + * current timestamp:
> + *
> + * older_unprocess_out --> TS1 | 1
> + * TS1 | 2
> + * out --> TS1 | 3
> + * We know have [TS1+1/3, TS1+2/3, current timestamp]
> + */
> +static void cros_ec_ring_spread_add(
> + struct cros_ec_sensorhub *sensorhub,
> + unsigned long sensor_mask,
> + s64 current_timestamp,
> + struct cros_ec_sensors_ring_sample *last_out)
> +{
> + struct cros_ec_sensors_ring_sample *out;
> + int i;
> +
> + for_each_set_bit(i, &sensor_mask, BITS_PER_LONG) {
> + s64 older_timestamp;
> + s64 timestamp;
> + struct cros_ec_sensors_ring_sample *older_unprocess_out =
> + sensorhub->ring;
> + struct cros_ec_sensors_ring_sample *next_out;
> + int count = 1;
> +
> + for (out = sensorhub->ring; out < last_out; out = next_out) {
> + s64 time_period;
> +
> + next_out = out + 1;
> + if (out->sensor_id != i)
> + continue;
> +
> + /* Timestamp to start with */
> + older_timestamp = out->timestamp;
> +
> + /* find next sample */
> + while (next_out < last_out && next_out->sensor_id != i)
> + next_out++;
> +
> + if (next_out >= last_out) {
> + timestamp = current_timestamp;
> + } else {
> + timestamp = next_out->timestamp;
> + if (timestamp == older_timestamp) {
> + count++;
> + continue;
> + }
> + }
> +
> + /*
> + * The next sample has a new timestamp,
> + * spread the unprocessed samples.
> + */
> + if (next_out < last_out)
> + count++;
> + time_period = div_s64(timestamp - older_timestamp,
> + count);
> +
> + for (; older_unprocess_out <= out;
> + older_unprocess_out++) {
> + if (older_unprocess_out->sensor_id != i)
> + continue;
> + older_timestamp += time_period;
> + older_unprocess_out->timestamp =
> + older_timestamp;
> + }
> + count = 1;
> + /* The next_out sample has a valid timestamp, skip. */
> + next_out++;
> + older_unprocess_out = next_out;
> + }
> + }
> +
> + /* push the event into the kfifo */
> + for (out = sensorhub->ring; out < last_out; out++)
> + cros_sensorhub_send_sample(sensorhub, out);
> +}
> +
> /*
> * cros_ec_sensorhub_ring_handler - the trigger handler function
> *
> @@ -280,9 +371,11 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub)
> }
> }
>
> - /* push the event into the kfifo */
> - for (out = sensorhub->ring; out < last_out; out++)
> - cros_sensorhub_send_sample(sensorhub, out);
> + /*
> + * Spread samples in case of batching, then add them to the ringbuffer.
> + */
> + cros_ec_ring_spread_add(sensorhub, sensor_mask,
> + current_timestamp, last_out);
>
> ring_handler_end:
> sensorhub->fifo_timestamp[LAST_TS] = current_timestamp;
On Sun, 22 Sep 2019 10:50:17 -0700
Gwendal Grignou <[email protected]> wrote:
> Events are timestamped in EC time space, their timestamps need to be
> converted in host time space.
> The assumption is the time delta between when the interrupt is sent
> by the EC and when it is receive by the host is a [small] constant.
> This is not always true, even with hard-wired interrupt. To mitigate
> worst offenders, add a median filter to weed out bigger than expected
> delays.
>
> Signed-off-by: Gwendal Grignou <[email protected]>
This stuff is always hard to get right. I'll assume you've tested
this extensively :)
I'll be honest, I'm going to assume you have this right as it would be a
while before I'd find time to go through this with a fine toothed comb
and I'd probably need right unit tests etc. Hence my comments
are superficial + there is a worrying looking todo in there I don't
understand ;)
Thanks,
Jonathan
> ---
> .../platform/chrome/cros_ec_sensorhub_ring.c | 485 +++++++++++++++++-
> .../linux/platform_data/cros_ec_sensorhub.h | 65 +++
> 2 files changed, 533 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
> index 48327e80a5a3..9955c80d0907 100644
> --- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c
> +++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
> @@ -69,9 +69,11 @@ int cros_ec_sensorhub_ring_fifo_toggle(
> struct cros_ec_sensorhub *sensorhub,
> bool on)
> {
> - int ret;
> + int ret, i;
>
> mutex_lock(&sensorhub->cmd_lock);
> + for (i = 0; i < CROS_EC_SENSOR_MAX; i++)
> + sensorhub->last_batch_len[i] = 0;
> sensorhub->params->cmd = MOTIONSENSE_CMD_FIFO_INT_ENABLE;
> sensorhub->params->fifo_int_enable.enable = on;
>
> @@ -86,6 +88,227 @@ int cros_ec_sensorhub_ring_fifo_toggle(
> return ret;
> }
>
> +static int cros_ec_ring_median_cmp(const void *pv1, const void *pv2)
> +{
> + s64 v1 = *(s64 *)pv1;
> + s64 v2 = *(s64 *)pv2;
> +
> + if (v1 > v2)
> + return 1;
> + else if (v1 < v2)
> + return -1;
> + else
> + return 0;
> +}
> +
> +/*
> + * cros_ec_ring_median: Gets median of an array of numbers
> + *
> + * For now it's implemented using an inefficient > O(n) sort then return
> + * the middle element. A more optimal method would be something like
> + * quickselect, but given that n = 64 we can probably live with it in the
> + * name of clarity.
> + *
> + * Warning: the input array gets modified (sorted)!
> + */
> +static s64 cros_ec_ring_median(s64 *array, size_t length)
> +{
> + sort(array, length, sizeof(s64), cros_ec_ring_median_cmp, NULL);
> + return array[length / 2];
> +}
> +
> +/*
> + * IRQ Timestamp Filtering
> + *
> + * Lower down in cros_ec_ring_process_event(), for each sensor event we have to
> + * calculate it's timestamp in the AP timebase. There are 3 time points:
> + * a - EC timebase, sensor event
> + * b - EC timebase, IRQ
> + * c - AP timebase, IRQ
> + * a' - what we want: sensor even in AP timebase
> + *
> + * While a and b are recorded at accurate times (due to the EC real time
> + * nature); c is pretty untrustworthy, even though it's recorded the
> + * first thing in ec_irq_handler(). There is a very good change we'll get
> + * added lantency due to:
> + * other irqs
> + * ddrfreq
> + * cpuidle
> + *
> + * Normally a' = c - b + a, but if we do that naive math any jitter in c
> + * will get coupled in a', which we don't want. We want a function
> + * a' = cros_ec_ring_ts_filter(a) which will filter out outliers in c.
> + *
> + * Think of a graph of AP time(b) on the y axis vs EC time(c) on the x axis.
> + * The slope of the line won't be exactly 1, there will be some clock drift
> + * between the 2 chips for various reasons (mechanical stress, temperature,
> + * voltage). We need to extrapolate values for a future x, without trusting
> + * recent y values too much.
> + *
> + * We use a median filter for the slope, then another median filter for the
> + * y-intercept to calculate this function:
> + * dx[n] = x[n-1] - x[n]
> + * dy[n] = x[n-1] - x[n]
> + * m[n] = dy[n] / dx[n]
> + * median_m = median(m[n-k:n])
> + * error[i] = y[n-i] - median_m * x[n-i]
> + * median_error = median(error[:k])
> + * predicted_y = median_m * x + median_error
> + *
> + * Implementation differences from above:
> + * - Redefined y to be actually c - b, this gives us a lot more precision
> + * to do the math. (c-b)/b variations are more obvious than c/b variations.
> + * - Since we don't have floating point, any operations involving slope are
> + * done using fixed point math (*M_PRECISION)
> + * - Since x and y grow with time, we keep zeroing the graph (relative to
> + * the last sample), this way math involving *x[n-i] will not overflow
> + * - EC timestamps are kept in us, it improves the slope calculation precision
> + */
> +
> +/*
> + * cros_ec_ring_ts_filter_update: Given a new IRQ timestamp pair (EC and
> + * AP timebases), add it to the filter history.
> + *
> + * @b IRQ timestamp, EC timebase (us)
> + * @c IRQ timestamp, AP timebase (ns)
> + */
> +static void cros_ec_ring_ts_filter_update(
> + struct cros_ec_sensors_ts_filter_state *state,
> + s64 b, s64 c)
> +{
> + s64 x, y;
> + s64 dx, dy;
> + s64 m; /* stored as *M_PRECISION */
> + s64 *m_history_copy = state->temp_buf;
> + s64 *error = state->temp_buf;
> + int i;
> +
> + /* we trust b the most, that'll be our independent variable */
> + x = b;
> + /* y is the offset between AP and EC times, in ns */
> + y = c - b * 1000;
> +
> + dx = (state->x_history[0] + state->x_offset) - x;
> + if (dx == 0)
> + return; /* we already have this irq in the history */
> + dy = (state->y_history[0] + state->y_offset) - y;
> + m = div64_s64(dy * M_PRECISION, dx);
> +
> + /* Empty filter if we haven't seen any action in a while. */
> + if (-dx > TS_HISTORY_BORED_US)
> + state->history_len = 0;
> +
> + /* Move everything over, also update offset to all absolute coords .*/
> + for (i = state->history_len - 1; i >= 1; i--) {
> + state->x_history[i] = state->x_history[i-1] + dx;
> + state->y_history[i] = state->y_history[i-1] + dy;
> +
> + state->m_history[i] = state->m_history[i-1];
> + /*
> + * Also use the same loop to copy m_history for future
> + * median extraction.
> + */
> + m_history_copy[i] = state->m_history[i-1];
> + }
> +
> + /* Store the x and y, but remember offset is actually last sample. */
> + state->x_offset = x;
> + state->y_offset = y;
> + state->x_history[0] = 0;
> + state->y_history[0] = 0;
> +
> + state->m_history[0] = m;
> + m_history_copy[0] = m;
> +
> + if (state->history_len < TS_HISTORY_SIZE)
> + state->history_len++;
> +
> + /* Precalculate things for the filter. */
> + if (state->history_len > TS_HISTORY_THRESHOLD) {
> + state->median_m =
> + cros_ec_ring_median(m_history_copy, state->history_len - 1);
> +
> + /*
> + * Calculate y-intercepts as if m_median is the slope and
> + * points in the history are on the line. median_error will
> + * still be in the offset coordinate system.
> + */
> + for (i = 0; i < state->history_len; i++)
> + error[i] = state->y_history[i] -
> + div_s64(state->median_m * state->x_history[i],
> + M_PRECISION);
> + state->median_error =
> + cros_ec_ring_median(error, state->history_len);
> + } else {
> + state->median_m = 0;
> + state->median_error = 0;
> + }
> +}
> +
> +/*
> + * cros_ec_ring_ts_filter: Translate EC timebase timestamp to AP timebase
> + *
> + * @x any ec timestamp (us):
> + *
> + * cros_ec_ring_ts_filter(a) => a' event timestamp, AP timebase
> + * cros_ec_ring_ts_filter(b) => calculated timestamp when the EC IRQ
> + * should have happened on the AP, with low jitter
> + *
> + * @returns timestamp in AP timebase (ns)
> + *
> + * Note: The filter will only activate once state->history_len goes
> + * over TS_HISTORY_THRESHOLD. Otherwise it'll just do the naive c - b + a
> + * transform.
> + *
> + * How to derive the formula, starting from:
> + * f(x) = median_m * x + median_error
> + * That's the calculated AP - EC offset (at the x point in time)
> + * Undo the coordinate system transform:
> + * f(x) = median_m * (x - x_offset) + median_error + y_offset
> + * Remember to undo the "y = c - b * 1000" modification:
> + * f(x) = median_m * (x - x_offset) + median_error + y_offset + x * 1000
> + */
> +static s64 cros_ec_ring_ts_filter(struct cros_ec_sensors_ts_filter_state *state,
> + s64 x)
> +{
> + return div_s64(state->median_m * (x - state->x_offset), M_PRECISION)
> + + state->median_error + state->y_offset + x * 1000;
> +}
> +
> +/*
> + * Since a and b were originally 32 bit values from the EC,
> + * they overflow relatively often, casting is not enough, so we need to
> + * add an offset.
> + */
> +static void cros_ec_ring_fix_overflow(s64 *ts,
> + const s64 overflow_period,
> + struct cros_ec_sensors_ec_overflow_state *state)
> +{
> + s64 adjust;
> +
> + *ts += state->offset;
> + if (abs(state->last - *ts) > (overflow_period / 2)) {
> + adjust = state->last > *ts ? overflow_period : -overflow_period;
> + state->offset += adjust;
> + *ts += adjust;
> + }
> + state->last = *ts;
> +}
> +
> +static void cros_ec_ring_check_for_past_timestamp(
> + struct cros_ec_sensorhub *sensorhub,
> + struct cros_ec_sensors_ring_sample *sample)
> +{
> + const u8 sensor_id = sample->sensor_id;
> +
> + // if this event is earlier than one we saw before...
/* please :)
> + if (sensorhub->newest_sensor_event[sensor_id] > sample->timestamp)
> + // mark it for spreading
> + sample->timestamp = sensorhub->last_batch_timestamp[sensor_id];
> + else
> + sensorhub->newest_sensor_event[sensor_id] = sample->timestamp;
> +}
> +
> /*
> * cros_ec_ring_process_event: process one EC FIFO event
> *
> @@ -117,25 +340,47 @@ static bool cros_ec_ring_process_event(
> s64 a = in->timestamp;
> s64 b = fifo_info->info.timestamp;
> s64 c = fifo_timestamp;
> - s64 new_timestamp;
>
> + cros_ec_ring_fix_overflow(&a, 1LL << 32,
> + &sensorhub->overflow_a);
> + cros_ec_ring_fix_overflow(&b, 1LL << 32,
> + &sensorhub->overflow_b);
> +
> + if (sensorhub->tight_timestamps) {
> + cros_ec_ring_ts_filter_update(&sensorhub->filter, b, c);
> + *current_timestamp =
> + cros_ec_ring_ts_filter(&sensorhub->filter, a);
> + } else {
> + s64 new_timestamp;
> + /*
> + * disable filtering since we might add more jitter
> + * if b is in a random point in time
> + */
> + new_timestamp = c - b * 1000 + a * 1000;
> + /*
> + * The timestamp can be stale if we had to use the fifo
> + * info timestamp.
> + */
> + if (new_timestamp - *current_timestamp > 0)
> + *current_timestamp = new_timestamp;
> + }
> + }
> +
> + if (in->flags & MOTIONSENSE_SENSOR_FLAG_ODR) {
> + sensorhub->last_batch_len[in->sensor_num] =
> + sensorhub->penultimate_batch_len[in->sensor_num] = 0;
> /*
> - * disable filtering since we might add more jitter
> - * if b is in a random point in time
> - */
> - new_timestamp = c - b * 1000 + a * 1000;
> - /*
> - * The timestamp can be stale if we had to use the fifo
> - * info timestamp.
> + * ODR change is only useful for the sensor_ring, it does not
> + * convey information to clients.
> */
> - if (new_timestamp - *current_timestamp > 0)
> - *current_timestamp = new_timestamp;
> + return false;
> }
>
> if (in->flags & MOTIONSENSE_SENSOR_FLAG_FLUSH) {
> out->sensor_id = in->sensor_num;
> out->timestamp = *current_timestamp;
> out->flag = in->flags;
> + sensorhub->last_batch_len[out->sensor_id] = 0;
> /*
> * No other payload information provided with
> * flush ack.
> @@ -149,7 +394,22 @@ static bool cros_ec_ring_process_event(
> /* Regular sample */
> out->sensor_id = in->sensor_num;
> if (*current_timestamp - now > 0) {
> - /* If the timestamp is in the future. */
> + /*
> + * This fix is needed to overcome the timestamp filter putting
> + * events in the future.
> + */
> + sensorhub->future_timestamp_total_ns +=
> + *current_timestamp - now;
> + if (++sensorhub->future_timestamp_count ==
> + FUTURE_TS_ANALYTICS_COUNT_MAX) {
> + s64 avg = div_s64(sensorhub->future_timestamp_total_ns,
> + sensorhub->future_timestamp_count);
> + dev_warn(sensorhub->dev,
> + "100 timestamps in the future, %lldns shaved on average\n",
> + avg);
> + sensorhub->future_timestamp_count = 0;
> + sensorhub->future_timestamp_total_ns = 0;
> + }
> out->timestamp = now;
> } else {
> out->timestamp = *current_timestamp;
> @@ -157,13 +417,195 @@ static bool cros_ec_ring_process_event(
> out->flag = in->flags;
> for (axis = 0; axis < 3; axis++)
> out->vector[axis] = in->data[axis];
> + if (sensorhub->tight_timestamps)
> + cros_ec_ring_check_for_past_timestamp(sensorhub, out);
> return true;
> }
>
> /*
> - * cros_ec_ring_spread_add: Calculate proper timestamps then
> + * cros_ec_ring_spread_add: Calculate proper timestamps then add to ringbuffer.
> + *
> + * Note: This is the new spreading code, assumes every sample's timestamp
> + * preceeds the sample. Run if tight_timestamps == true.
> + *
> + * Sometimes the EC receives only one interrupt (hence timestamp) for
> + * a batch of samples. Only the first sample will have the correct
> + * timestamp. So we must interpolate the other samples.
> + * We use the previous batch timestamp and our current batch timestamp
> + * as a way to calculate period, then spread the samples evenly.
> + *
> + * s0 int, 0ms
> + * s1 int, 10ms
> + * s2 int, 20ms
> + * 30ms point goes by, no interrupt, previous one is still asserted
> + * downloading s2 and s3
> + * s3 sample, 20ms (incorrect timestamp)
> + * s4 int, 40ms
> + *
> + * The batches are [(s0), (s1), (s2, s3), (s4)]. Since the 3rd batch
> + * has 2 samples in them, we adjust the timestamp of s3.
> + * s2 - s1 = 10ms, so s3 must be s2 + 10ms => 20ms. If s1 would have
> + * been part of a bigger batch things would have gotten a little
> + * more complicated.
> + *
> + * Note: we also assume another sensor sample doesn't break up a batch
> + * in 2 or more partitions. Example, there can't ever be a sync sensor
> + * in between S2 and S3. This simplifies the following code.
> + */
> +static void cros_ec_ring_spread_add(
> + struct cros_ec_sensorhub *sensorhub,
> + unsigned long sensor_mask,
> + struct cros_ec_sensors_ring_sample *last_out)
> +{
> + struct cros_ec_sensors_ring_sample *batch_start, *next_batch_start;
> + int id;
> +
> + for_each_set_bit(id, &sensor_mask, BITS_PER_LONG) {
> + for (batch_start = sensorhub->ring; batch_start < last_out;
> + batch_start = next_batch_start) {
> + /*
> + * For each batch (where all samples have the same
> + * timestamp).
> + */
> + int batch_len, sample_idx;
> + struct cros_ec_sensors_ring_sample *batch_end =
> + batch_start;
> + struct cros_ec_sensors_ring_sample *s;
> + s64 batch_timestamp = batch_start->timestamp;
> + s64 sample_period;
> +
> + /*
> + * Skip over batches that start with the sensor types
> + * we're not looking at right now.
> + */
> + if (batch_start->sensor_id != id) {
> + next_batch_start = batch_start + 1;
> + continue;
> + }
> +
> + /*
> + * TODO(gwendal): can not send out flush packets
> + * anymore.
> + * Do not start a batch
> + * from a flush, as it happens asynchronously to the
> + * regular flow of events.
Slightly worrying TODO... Fixed later?
> + */
> + if (batch_start->flag &
> + MOTIONSENSE_SENSOR_FLAG_FLUSH) {
> + next_batch_start = batch_start + 1;
> + continue;
> + }
> +
> + if (batch_start->timestamp <=
> + sensorhub->last_batch_timestamp[id]) {
> +
> + batch_timestamp =
> + sensorhub->last_batch_timestamp[id];
> + batch_len = sensorhub->last_batch_len[id];
> +
> + sample_idx = batch_len;
> +
> + sensorhub->last_batch_timestamp[id] =
> + sensorhub->penultimate_batch_timestamp[id];
> + sensorhub->last_batch_len[id] =
> + sensorhub->penultimate_batch_len[id];
> + } else {
> + /*
> + * Push first sample in the batch to the,
> + * kifo, it's guaranteed to be correct, the
> + * rest will follow later on.
> + */
> + sample_idx = batch_len = 1;
> + cros_sensorhub_send_sample(
> + sensorhub, batch_start);
> + batch_start++;
> + }
> +
> + /* Find all samples have the same timestamp. */
> + for (s = batch_start; s < last_out; s++) {
> + if (s->sensor_id != id)
> + /*
> + * Skip over other sensor types that
> + * are interleaved, don't count them.
> + */
> + continue;
> + if (s->timestamp != batch_timestamp)
> + /* we discovered the next batch */
> + break;
> + if (s->flag & MOTIONSENSE_SENSOR_FLAG_FLUSH)
> + /* break on flush packets */
> + break;
> + batch_end = s;
> + batch_len++;
> + }
> +
> + if (batch_len == 1)
> + goto done_with_this_batch;
> +
> + /* Can we calculate period? */
> + if (sensorhub->last_batch_len[id] == 0) {
> + dev_warn(sensorhub->dev, "Sensor %d: lost %d samples when spreading\n",
> + id, batch_len - 1);
> + goto done_with_this_batch;
> + /*
> + * Note: we're dropping the rest of the samples
> + * in this batch since we have no idea where
> + * they're supposed to go without a period
> + * calculation.
> + */
> + }
> +
> + sample_period = div_s64(batch_timestamp -
> + sensorhub->last_batch_timestamp[id],
> + sensorhub->last_batch_len[id]);
> + dev_dbg(sensorhub->dev,
> + "Adjusting %d samples, sensor %d last_batch @%lld (%d samples) batch_timestamp=%lld => period=%lld\n",
> + batch_len, id,
> + sensorhub->last_batch_timestamp[id],
> + sensorhub->last_batch_len[id],
> + batch_timestamp,
> + sample_period);
> +
> + /*
> + * Adjust timestamps of the samples then push them to
> + * kfifo.
> + */
> + for (s = batch_start; s <= batch_end; s++) {
> + if (s->sensor_id != id)
> + /*
> + * Skip over other sensor types that
> + * are interleaved, don't change them.
> + */
> + continue;
> +
> + s->timestamp = batch_timestamp +
> + sample_period * sample_idx;
> + sample_idx++;
> +
> + cros_sensorhub_send_sample(sensorhub, s);
> + }
> +
> +done_with_this_batch:
> + sensorhub->penultimate_batch_timestamp[id] =
> + sensorhub->last_batch_timestamp[id];
> + sensorhub->penultimate_batch_len[id] =
> + sensorhub->last_batch_len[id];
> +
> + sensorhub->last_batch_timestamp[id] = batch_timestamp;
> + sensorhub->last_batch_len[id] = batch_len;
> +
> + next_batch_start = batch_end + 1;
> + }
> + }
> +}
> +
> +/*
> + * cros_ec_ring_spread_add_legacy: Calculate proper timestamps then
> * add to ringbuffer (legacy).
> *
> + * Note: This assumes we're running old firmware, where every sample's timestamp
> + * is after the sample. Run if tight_timestamps == false.
> + *
> * If there is a sample with a proper timestamp
> * timestamp | count
> * older_unprocess_out --> TS1 | 1
> @@ -181,7 +623,7 @@ static bool cros_ec_ring_process_event(
> * out --> TS1 | 3
> * We know have [TS1+1/3, TS1+2/3, current timestamp]
> */
> -static void cros_ec_ring_spread_add(
> +static void cros_ec_ring_spread_add_legacy(
> struct cros_ec_sensorhub *sensorhub,
> unsigned long sensor_mask,
> s64 current_timestamp,
> @@ -355,7 +797,8 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub)
> * the AP is slow to respond to the IRQ, the EC may have added new
> * samples. Use the FIFO info timestamp as last timestamp then.
> */
> - if ((last_out-1)->timestamp == current_timestamp)
> + if (!sensorhub->tight_timestamps &&
> + (last_out-1)->timestamp == current_timestamp)
> current_timestamp = fifo_timestamp;
>
> /* Warn on lost samples. */
> @@ -367,6 +810,7 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub)
> dev_warn(sensorhub->dev,
> "Sensor %d: lost: %d out of %d\n", i,
> lost, fifo_info->info.total_lost);
> + sensorhub->last_batch_len[i] = 0;
> }
> }
> }
> @@ -374,8 +818,11 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub)
> /*
> * Spread samples in case of batching, then add them to the ringbuffer.
> */
> - cros_ec_ring_spread_add(sensorhub, sensor_mask,
> - current_timestamp, last_out);
> + if (sensorhub->tight_timestamps)
> + cros_ec_ring_spread_add(sensorhub, sensor_mask, last_out);
> + else
> + cros_ec_ring_spread_add_legacy(sensorhub, sensor_mask,
> + current_timestamp, last_out);
>
> ring_handler_end:
> sensorhub->fifo_timestamp[LAST_TS] = current_timestamp;
> @@ -436,6 +883,10 @@ int cros_ec_sensorhub_ring_add(struct cros_ec_sensorhub *sensorhub)
>
> sensorhub->fifo_timestamp[LAST_TS] = cros_ec_get_time_ns();
>
> + sensorhub->tight_timestamps = cros_ec_check_features(ec,
> + EC_FEATURE_MOTION_SENSE_TIGHT_TIMESTAMPS);
> +
> +
> /* register the notifier that will act as a top half interrupt. */
> sensorhub->notifier.notifier_call = cros_ec_sensorhub_event;
> ret = blocking_notifier_chain_register(&ec->ec_dev->event_notifier,
> diff --git a/include/linux/platform_data/cros_ec_sensorhub.h b/include/linux/platform_data/cros_ec_sensorhub.h
> index 18cda568c58a..df67f2015da9 100644
> --- a/include/linux/platform_data/cros_ec_sensorhub.h
> +++ b/include/linux/platform_data/cros_ec_sensorhub.h
> @@ -53,6 +53,42 @@ struct cros_ec_sensors_ring_sample {
> s64 timestamp;
> } __packed;
>
> +/* State used for cros_ec_ring_fix_overflow */
> +struct cros_ec_sensors_ec_overflow_state {
> + s64 offset;
> + s64 last;
> +};
> +
> +/* Precision of fixed point for the m values from the filter */
> +#define M_PRECISION (1 << 23)
These are a bit generic names. Prefix them so we don't get a clash
sometime in the future.
> +
> +/* Length of the filter, how long to remember entries for */
> +#define TS_HISTORY_SIZE 64
> +
> +/* Only activate the filter once we have at least this many elements. */
> +#define TS_HISTORY_THRESHOLD 8
> +
> +/*
> + * If we don't have any history entries for this long, empty the filter to
> + * make sure there are no big discontinuities.
> + */
> +#define TS_HISTORY_BORED_US 500000
> +
> +struct cros_ec_sensors_ts_filter_state {
> + s64 x_offset, y_offset;
> + s64 x_history[TS_HISTORY_SIZE]; /* stored relative to x_offset */
> + s64 y_history[TS_HISTORY_SIZE]; /* stored relative to y_offset */
> + s64 m_history[TS_HISTORY_SIZE]; /* stored as *M_PRECISION */
> + int history_len;
> +
> + s64 temp_buf[TS_HISTORY_SIZE];
> +
> + s64 median_m;
> + s64 median_error;
> +};
> +
> +#define FUTURE_TS_ANALYTICS_COUNT_MAX 100
> +
> /**
> * struct cros_ec_sensorhub - Sensor Hub device data.
> */
> @@ -76,6 +112,35 @@ struct cros_ec_sensorhub {
> struct cros_ec_fifo_info fifo_info;
> int fifo_size;
>
> + /* Used for timestamp spreading calculations when a batch shows up */
> + s64 penultimate_batch_timestamp[CROS_EC_SENSOR_MAX];
> + int penultimate_batch_len[CROS_EC_SENSOR_MAX];
> + s64 last_batch_timestamp[CROS_EC_SENSOR_MAX];
> + int last_batch_len[CROS_EC_SENSOR_MAX];
> + s64 newest_sensor_event[CROS_EC_SENSOR_MAX];
> +
> + struct cros_ec_sensors_ec_overflow_state overflow_a;
> + struct cros_ec_sensors_ec_overflow_state overflow_b;
> +
> + struct cros_ec_sensors_ts_filter_state filter;
> +
> + /*
> + * The timestamps reported from the EC have low jitter.
> + * Timestamps also come before every sample.
> + * Set either by feature bits coming from the EC or userspace.
> + */
> + bool tight_timestamps;
> +
> + /*
> + * Statistics used to compute shaved time. This occures when
> + * timestamp interpolation from EC time to AP time accidentally
> + * puts timestamps in the future. These timestamps are clamped
> + * to `now` and these count/total_ns maintain the statistics for
> + * how much time was removed in a given period..
> + */
> + s32 future_timestamp_count;
> + s64 future_timestamp_total_ns;
> +
> /*
> * Dynamic array to be able to spread datum to iio sensor objects.
> */
On Sun, 22 Sep 2019 10:50:18 -0700
Gwendal Grignou <[email protected]> wrote:
> When EC supports FIFO, the samples will flow from the kernel by
> themselves.
> When no FIFO, the user space app needs to call trigger_new, or better
> register a high precision timer.
>
> Signed-off-by: Gwendal Grignou <[email protected]>
Trivial comments inline.
> ---
> drivers/iio/accel/cros_ec_accel_legacy.c | 8 +--
> .../cros_ec_sensors/cros_ec_lid_angle.c | 2 +-
> .../common/cros_ec_sensors/cros_ec_sensors.c | 8 +--
> .../cros_ec_sensors/cros_ec_sensors_core.c | 61 +++++++++++++++++--
> drivers/iio/light/cros_ec_light_prox.c | 8 +--
> drivers/iio/pressure/cros_ec_baro.c | 8 +--
> .../linux/iio/common/cros_ec_sensors_core.h | 16 ++++-
> 7 files changed, 80 insertions(+), 31 deletions(-)
>
> diff --git a/drivers/iio/accel/cros_ec_accel_legacy.c b/drivers/iio/accel/cros_ec_accel_legacy.c
> index 591c0d962c44..d607fbc52c95 100644
> --- a/drivers/iio/accel/cros_ec_accel_legacy.c
> +++ b/drivers/iio/accel/cros_ec_accel_legacy.c
> @@ -179,7 +179,8 @@ static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
> if (!indio_dev)
> return -ENOMEM;
>
> - ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
> + ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
> + cros_ec_sensors_capture, cros_ec_sensors_push_data);
> if (ret)
> return ret;
>
> @@ -199,11 +200,6 @@ static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
> state->sign[CROS_EC_SENSOR_Z] = -1;
> }
>
> - ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> - cros_ec_sensors_capture, NULL);
> - if (ret)
> - return ret;
> -
> return devm_iio_device_register(dev, indio_dev);
> }
>
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> index 1dcc2a16ab2d..e30a59fcf0f9 100644
> --- a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> @@ -97,7 +97,7 @@ static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> if (!indio_dev)
> return -ENOMEM;
>
> - ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> + ret = cros_ec_sensors_core_init(pdev, indio_dev, false, NULL, NULL);
> if (ret)
> return ret;
>
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
> index a88dd8deade9..b78a942ac8e5 100644
> --- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
> @@ -239,7 +239,8 @@ static int cros_ec_sensors_probe(struct platform_device *pdev)
> if (!indio_dev)
> return -ENOMEM;
>
> - ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
> + ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
> + cros_ec_sensors_capture, cros_ec_sensors_push_data);
> if (ret)
> return ret;
>
> @@ -301,11 +302,6 @@ static int cros_ec_sensors_probe(struct platform_device *pdev)
> else
> state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
>
> - ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> - cros_ec_sensors_capture, NULL);
> - if (ret)
> - return ret;
> -
> return devm_iio_device_register(dev, indio_dev);
> }
>
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> index 43eb1d42820e..c4c37c6df301 100644
> --- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
> @@ -12,6 +12,7 @@
> #include <linux/iio/iio.h>
> #include <linux/iio/kfifo_buf.h>
> #include <linux/iio/trigger_consumer.h>
> +#include <linux/iio/triggered_buffer.h>
> #include <linux/kernel.h>
> #include <linux/mfd/cros_ec.h>
> #include <linux/module.h>
> @@ -83,15 +84,50 @@ static void get_default_min_max_freq(enum motionsensor_type type,
> }
> }
>
> +int cros_ec_sensors_push_data(
> + struct iio_dev *indio_dev,
> + s16 *data,
> + s64 timestamp)
> +{
> + struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> + s16 *out;
> + unsigned int i;
> +
> + /*
> + * It can happen if we get a samples before the iio device is fully
> + * registered.
> + */
> + if (!st)
> + return 0;
> +
> + /* Ignore samples if the buffer is not set. */
> + if (!indio_dev->active_scan_mask)
Odd test. That tests if the core has created the bitmask.
Is the intent to check if any bits are enabled?
> + return 0;
> +
> + out = (s16 *)st->samples;
> + for_each_set_bit(i,
> + indio_dev->active_scan_mask,
> + indio_dev->masklength) {
> + *out = data[i];
> + out++;
> + }
blank line.
> + iio_push_to_buffers_with_timestamp(indio_dev, st->samples, timestamp);
blank line.
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(cros_ec_sensors_push_data);
> +
> int cros_ec_sensors_core_init(struct platform_device *pdev,
> struct iio_dev *indio_dev,
> - bool physical_device)
> + bool physical_device,
> + cros_ec_sensors_capture_t trigger_capture,
> + cros_ec_sensorhub_push_data_cb_t push_data)
> {
> struct device *dev = &pdev->dev;
> struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
> struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
> struct cros_ec_dev *ec = sensor_hub->ec;
> struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
> + struct iio_buffer *buffer;
> u32 ver_mask;
> int ret, i;
>
> @@ -124,8 +160,6 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
> indio_dev->name = pdev->name;
>
> if (physical_device) {
> - indio_dev->modes = INDIO_DIRECT_MODE;
> -
> state->param.cmd = MOTIONSENSE_CMD_INFO;
> state->param.info.sensor_num = sensor_platform->sensor_num;
> ret = cros_ec_motion_send_host_cmd(state, 0);
> @@ -154,9 +188,26 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
> state->frequencies[2] =
> state->resp->info_3.max_frequency;
> }
> - }
>
> - return 0;
> + if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
> + buffer = devm_iio_kfifo_allocate(dev);
> + if (!buffer)
> + return -ENOMEM;
> +
> + iio_device_attach_buffer(indio_dev, buffer);
> + indio_dev->modes = INDIO_BUFFER_SOFTWARE;
> + ret = cros_ec_sensorhub_register_push_data(
> + sensor_hub,
> + sensor_platform->sensor_num,
> + indio_dev, push_data);
> + } else {
> + indio_dev->modes = INDIO_DIRECT_MODE;
> + ret = devm_iio_triggered_buffer_setup(
> + dev, indio_dev, NULL,
> + trigger_capture, NULL);
> + }
> + }
> + return ret;
> }
> EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init);
>
> diff --git a/drivers/iio/light/cros_ec_light_prox.c b/drivers/iio/light/cros_ec_light_prox.c
> index c431e4d1482d..d58f010880ce 100644
> --- a/drivers/iio/light/cros_ec_light_prox.c
> +++ b/drivers/iio/light/cros_ec_light_prox.c
> @@ -186,7 +186,8 @@ static int cros_ec_light_prox_probe(struct platform_device *pdev)
> if (!indio_dev)
> return -ENOMEM;
>
> - ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
> + ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
> + cros_ec_sensors_capture, cros_ec_sensors_push_data);
> if (ret)
> return ret;
>
> @@ -245,11 +246,6 @@ static int cros_ec_light_prox_probe(struct platform_device *pdev)
>
> state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
>
> - ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> - cros_ec_sensors_capture, NULL);
> - if (ret)
> - return ret;
> -
> return devm_iio_device_register(dev, indio_dev);
> }
>
> diff --git a/drivers/iio/pressure/cros_ec_baro.c b/drivers/iio/pressure/cros_ec_baro.c
> index 2f4d6d3ab41d..c7538e93d24f 100644
> --- a/drivers/iio/pressure/cros_ec_baro.c
> +++ b/drivers/iio/pressure/cros_ec_baro.c
> @@ -134,7 +134,8 @@ static int cros_ec_baro_probe(struct platform_device *pdev)
> if (!indio_dev)
> return -ENOMEM;
>
> - ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
> + ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
> + cros_ec_sensors_capture, cros_ec_sensors_push_data);
> if (ret)
> return ret;
>
> @@ -180,11 +181,6 @@ static int cros_ec_baro_probe(struct platform_device *pdev)
>
> state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
>
> - ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> - cros_ec_sensors_capture, NULL);
> - if (ret)
> - return ret;
> -
> return devm_iio_device_register(dev, indio_dev);
> }
>
> diff --git a/include/linux/iio/common/cros_ec_sensors_core.h b/include/linux/iio/common/cros_ec_sensors_core.h
> index abef66d0e884..60f78f0e4884 100644
> --- a/include/linux/iio/common/cros_ec_sensors_core.h
> +++ b/include/linux/iio/common/cros_ec_sensors_core.h
> @@ -12,6 +12,7 @@
> #include <linux/irqreturn.h>
> #include <linux/platform_data/cros_ec_commands.h>
> #include <linux/platform_data/cros_ec_proto.h>
> +#include <linux/platform_data/cros_ec_sensorhub.h>
>
> enum {
> CROS_EC_SENSOR_X,
> @@ -32,6 +33,9 @@ enum {
> /* Minimum sampling period to use when device is suspending */
> #define CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY 1000 /* 1 second */
>
> +typedef irqreturn_t (*cros_ec_sensors_capture_t)(int irq, void *p);
> +
> +
> /**
> * struct cros_ec_sensors_core_state - state data for EC sensors IIO driver
> * @ec: cros EC device structure
> @@ -110,11 +114,17 @@ struct platform_device;
> * @pdev: platform device created for the sensors
> * @indio_dev: iio device structure of the device
> * @physical_device: true if the device refers to a physical device
> + * @trigger_capture: function pointer to call buffer is triggered,
> + * for backward compatibility.
> + * @push_data: function to call when cros_ec_sensorhub receives
> + * a sample for that sensor.
> *
> * Return: 0 on success, -errno on failure.
> */
> int cros_ec_sensors_core_init(struct platform_device *pdev,
> - struct iio_dev *indio_dev, bool physical_device);
> + struct iio_dev *indio_dev, bool physical_device,
> + cros_ec_sensors_capture_t trigger_capture,
> + cros_ec_sensorhub_push_data_cb_t push_data);
>
> /* To remove association of physical device to cros_ec_sensorhub. */
> int cros_ec_sensors_core_clean(struct platform_device *pdev);
> @@ -132,6 +142,10 @@ int cros_ec_sensors_core_clean(struct platform_device *pdev);
> * Return: IRQ_HANDLED
> */
> irqreturn_t cros_ec_sensors_capture(int irq, void *p);
> +int cros_ec_sensors_push_data(
> + struct iio_dev *indio_dev,
> + s16 *data,
> + s64 timestamp);
>
> /**
> * cros_ec_motion_send_host_cmd() - send motion sense host command