Skip to contentSkip to conversation

iio: cros_ec_sensors: add cros_ec_activity driver

PATCH Superseded Gwendal Grignou 2 participants 2 messages May 20, 2025 – May 21, 2025

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: cros_ec_sensors: add cros_ec_activity driver

ChromeOS EC can report activity information derived from the accelerometer:
- reports on-body/off-body as a proximity event.
- reports significant motion as an activity event.

This new sensor is a virtual sensor, included only when the EC firmware is compiled with the appropriate module.

---
 drivers/iio/common/cros_ec_sensors/Kconfig    |  10 +
 drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
 .../common/cros_ec_sensors/cros_ec_activity.c | 330 ++++++++++++++++++
 .../cros_ec_sensors/cros_ec_sensors_core.c    |  10 +
 .../linux/iio/common/cros_ec_sensors_core.h   |   1 +
 .../linux/platform_data/cros_ec_commands.h    |  36 +-
 6 files changed, 382 insertions(+), 6 deletions(-)
 create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_activity.c
drivers/iio/common/cros_ec_sensors/Kconfig +10 -0
index fefad9572790..7ef925850a62 100644
@@ -30,3 +30,13 @@ config IIO_CROS_EC_SENSORS_LID_ANGLE
 	  convertible devices.
 	  This module is loaded when the EC can calculate the angle between the base
 	  and the lid.
+
+config IIO_CROS_EC_ACTIVITY
+	tristate "ChromeOS EC Activity Sensors"
+	depends on IIO_CROS_EC_SENSORS_CORE
+	help
+	  Module to handle activity events detections presented by the ChromeOS
+	  EC Sensor hub.
+	  Activities can be a proximity detector (on body/off body detection)
+	  or an activity to trigger an event on significant motion.
+	  Creates an IIO device to manage all activities.
drivers/iio/common/cros_ec_sensors/Makefile +1 -0
index e0a33ab66d21..a7849c52f3c8 100644
@@ -3,6 +3,7 @@
 # Makefile for sensors seen through the ChromeOS EC sensor hub.
 #
 
+obj-$(CONFIG_IIO_CROS_EC_ACTIVITY) += cros_ec_activity.o
 obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
 obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
 obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +330 -0

330 lines not on this page · raw message

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +10 -0
index 64bada1e8678..1bd07ca3abfe 100644
@@ -489,6 +489,16 @@ const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
 };
 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info);
 
+const struct iio_chan_spec_ext_info cros_ec_sensors_limited_info[] = {
+	{
+		.name = "id",
+		.shared = IIO_SHARED_BY_ALL,
+		.read = cros_ec_sensors_id
+	},
+	{ },
+};
+EXPORT_SYMBOL_GPL(cros_ec_sensors_limited_info);
+
 /**
  * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory
  * @st:		pointer to state information for device
include/linux/iio/common/cros_ec_sensors_core.h +1 -0
index e72167b96d27..bb966abcde53 100644
@@ -126,5 +126,6 @@ extern const struct dev_pm_ops cros_ec_sensors_pm_ops;
 
 /* List of extended channel specification for all sensors. */
 extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[];
+extern const struct iio_chan_spec_ext_info cros_ec_sensors_limited_info[];
 
 #endif  /* __CROS_EC_SENSORS_CORE_H */
include/linux/platform_data/cros_ec_commands.h +30 -6
index ecf290a0c98f..0f6d53e7cb97 100644
@@ -2388,6 +2388,12 @@ enum motionsense_command {
 	 */
 	MOTIONSENSE_CMD_SENSOR_SCALE = 18,
 
+	/*
+	 * Activity management
+	 * Retrieve current status of given activity.
+	 */
+	MOTIONSENSE_CMD_GET_ACTIVITY = 20,
+
 	/* Number of motionsense sub-commands. */
 	MOTIONSENSE_NUM_CMDS
 };
@@ -2447,6 +2453,11 @@ enum motionsensor_orientation {
 	MOTIONSENSE_ORIENTATION_UNKNOWN = 4,
 };
 
+struct ec_response_activity_data {
+	uint8_t activity; /* motionsensor_activity */
+	uint8_t state;
+} __ec_todo_packed;
+
 struct ec_response_motion_sensor_data {
 	/* Flags for each sensor. */
 	uint8_t flags;
@@ -2454,15 +2465,14 @@ struct ec_response_motion_sensor_data {
 	uint8_t sensor_num;
 	/* Each sensor is up to 3-axis. */
 	union {
-		int16_t             data[3];
+		int16_t                                  data[3];
 		struct __ec_todo_packed {
-			uint16_t    reserved;
-			uint32_t    timestamp;
+			uint16_t                         reserved;
+			uint32_t                         timestamp;
 		};
 		struct __ec_todo_unpacked {
-			uint8_t     activity; /* motionsensor_activity */
-			uint8_t     state;
-			int16_t     add_info[2];
+			struct ec_response_activity_data activity_data;
+			int16_t                          add_info[2];
 		};
 	};
 } __ec_todo_packed;
@@ -2494,6 +2504,7 @@ enum motionsensor_activity {
 	MOTIONSENSE_ACTIVITY_SIG_MOTION = 1,
 	MOTIONSENSE_ACTIVITY_DOUBLE_TAP = 2,
 	MOTIONSENSE_ACTIVITY_ORIENTATION = 3,
+	MOTIONSENSE_ACTIVITY_BODY_DETECTION = 4,
 };
 
 struct ec_motion_sense_activity {
@@ -2671,6 +2682,7 @@ struct ec_params_motion_sense {
 			uint32_t max_data_vector;
 		} fifo_read;
 
+		/* Used for MOTIONSENSE_CMD_SET_ACTIVITY */
 		struct ec_motion_sense_activity set_activity;
 
 		/* Used for MOTIONSENSE_CMD_LID_ANGLE */
@@ -2716,6 +2728,14 @@ struct ec_params_motion_sense {
 			 */
 			int16_t hys_degree;
 		} tablet_mode_threshold;
+
+		/*
+		 * Used for MOTIONSENSE_CMD_GET_ACTIVITY.
+		 */
+		struct __ec_todo_unpacked {
+			uint8_t sensor_num;
+			uint8_t activity;  /* enum motionsensor_activity */
+		} get_activity;
 	};
 } __ec_todo_packed;
 
@@ -2833,6 +2853,10 @@ struct ec_response_motion_sense {
 			uint16_t hys_degree;
 		} tablet_mode_threshold;
 
+		/* USED for MOTIONSENSE_CMD_GET_ACTIVITY. */
+		struct __ec_todo_unpacked {
+			uint8_t     state;
+		} get_activity;
 	};
 } __ec_todo_packed;
 
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
#1 lore raw
Quoted 4 lines · Gwendal Grignou

ChromeOS EC can report activity information derived from the accelerometer:
- reports on-body/off-body as a proximity event.
- reports significant motion as an activity event.

s/^r/R/.

drivers/iio/common/cros_ec_sensors/Kconfig config IIO_CROS_EC_SENSORS_LID_ANGLE · line 38

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/Kconfig +5 -0
[...]
+config IIO_CROS_EC_ACTIVITY
+	tristate "ChromeOS EC Activity Sensors"
+	depends on IIO_CROS_EC_SENSORS_CORE
+	help
+	  Module to handle activity events detections presented by the ChromeOS
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^

I'm confused and wondering if there is a typo. Which one:
- activity event detections
- activity events
- activity detections

drivers/iio/common/cros_ec_sensors/Kconfig config IIO_CROS_EC_SENSORS_LID_ANGLE · line 39

Quoted 1 line
drivers/iio/common/cros_ec_sensors/Kconfig +1 -0
+	  EC Sensor hub.

s/S/s/?

drivers/iio/common/cros_ec_sensors/Kconfig config IIO_CROS_EC_SENSORS_LID_ANGLE · line 41

Quoted 2 lines
drivers/iio/common/cros_ec_sensors/Kconfig +2 -0
+	  Activities can be a proximity detector (on body/off body detection)
+	  or an activity to trigger an event on significant motion.

s/an activity to trigger //?

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 2

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +2 -0
[...]
+// SPDX-License-Identifier: GPL-2.0
+//

Drop the empty comment line.

Quoted 6 lines

+/*
+ * cros_ec_sensors_activity - Driver for activities/gesture recognition.
+ *
+ * Copyright 2025 Google, Inc
+ *
+ * This driver uses the cros-ec interface to communicate with the Chrome OS

s/Chrome OS/ChromeOS/.

Quoted 1 line

+#include <linux/delay.h>

Drop it. The driver doesn't use anything from the header.

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 22

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +10 -0
+#include <linux/device.h>
+#include <linux/iio/common/cros_ec_sensors_core.h>
+#include <linux/iio/events.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_data/cros_ec_commands.h>
+#include <linux/platform_data/cros_ec_proto.h>
+#include <linux/platform_device.h>

I guess the list may be copied from somewhere drivers. Could you review again to make sure the driver needs to include them?

Quoted 1 line

+#include <linux/slab.h>

Drop it. The driver doesn't use anything from the header.

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 44

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +7 -0
+static const struct iio_event_spec cros_ec_activity_single_shot[] = {
+	{
+		.type = IIO_EV_TYPE_CHANGE,
+		/* significant motion trigger when we get out of still. */
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+	 },

Remove the extra space before "}".

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 52

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +6 -0
+static const struct iio_event_spec cros_ec_body_detect_events[] = {
+	{
+		.type = IIO_EV_TYPE_CHANGE,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+	 },

Remove the extra space before "}".

Quoted 5 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +5 -0
+static int ec_sensors_read(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int *val, int *val2, long mask)
+{
+	struct cros_ec_sensors_state *st = iio_priv(indio_dev);

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 69

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +10 -0
+	int ret;
+
+	mutex_lock(&st->core.cmd_lock);
+	if (chan->type == IIO_PROXIMITY &&
+	    mask == IIO_CHAN_INFO_RAW) {
+		st->core.param.cmd = MOTIONSENSE_CMD_GET_ACTIVITY;
+		st->core.param.get_activity.activity =
+			MOTIONSENSE_ACTIVITY_BODY_DETECTION;
+		if (cros_ec_motion_send_host_cmd(&st->core, 0) !=
+				EC_RES_SUCCESS) {

cros_ec_motion_send_host_cmd() returns 0 or negative errno like most other kernel APIs. Even though EC_RES_SUCCESS is also 0, however, they aren't the same domain space (see also cros_ec_map_error()). Don't use it.

		if (cros_ec_motion_send_host_cmd(&st->core, 0))
Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +10 -0
+static int cros_ec_read_event_config(struct iio_dev *indio_dev,
[...]
+	switch (chan->type) {
+	case IIO_PROXIMITY:
+		ret = !!(st->core.resp->list_activities.enabled &
+			 (1 << MOTIONSENSE_ACTIVITY_BODY_DETECTION));
+		break;
+	case IIO_ACTIVITY:
+		if (chan->channel2 == IIO_MOD_STILL) {
+			ret = !!(st->core.resp->list_activities.enabled &
+				 (1 << MOTIONSENSE_ACTIVITY_SIG_MOTION));

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 116

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +10 -0
+		} else {
+			dev_warn(&indio_dev->dev, "Unknown activity: %d\n",
+				 chan->channel2);
+			ret = -EINVAL;
+		}
+		break;
+	default:
+		dev_warn(&indio_dev->dev, "Unknown channel type: %d\n",
+			 chan->type);
+		ret = -EINVAL;

Curious about: wouldn't compiler or some checkers emit warnings about it lacks a break?

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +8 -0
+static int cros_ec_write_event_config(struct iio_dev *indio_dev,
[...]
+	switch (chan->type) {
+	case IIO_PROXIMITY:
+		st->core.param.set_activity.activity =
+			MOTIONSENSE_ACTIVITY_BODY_DETECTION;
+		break;
+	case IIO_ACTIVITY:
+		if (chan->channel2 == IIO_MOD_STILL) {

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 152

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +10 -0
+			st->core.param.set_activity.activity =
+				MOTIONSENSE_ACTIVITY_SIG_MOTION;
+		} else {
+			dev_warn(&indio_dev->dev, "Unknown activity: %d\n",
+				 chan->channel2);
+		}
+		break;
+	default:
+		dev_warn(&indio_dev->dev, "Unknown channel type: %d\n",
+			 chan->type);

Same here.

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 224

Quoted 12 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +12 -0
+static int cros_ec_sensors_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct cros_ec_device *ec_device = dev_get_drvdata(dev->parent);
+	struct iio_dev *indio_dev;
+	struct cros_ec_sensors_state *st;
+	struct iio_chan_spec *channel;
+	unsigned long activities;
+	int i, index, ret, nb_activities;
+
+	if (!ec_device) {
+		dev_warn(&pdev->dev, "No CROS EC device found.\n");

The function already has a local variable dev for &pdev->dev. Use it.

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 228

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +4 -0
+		return -EINVAL;
+	}
+
+	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));

dev.

Quoted 12 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +12 -0
+	if (!indio_dev)
+		return -ENOMEM;
+
+	ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
+					cros_ec_activity_capture);
+	if (ret)
+		return ret;
+
+	indio_dev->info = &ec_sensors_info;
+	st = iio_priv(indio_dev);
+	st->core.type = st->core.resp->info.type;
+

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 250

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +10 -0
+	/*
+	 * List all available activities
+	 */
+	st->core.param.cmd = MOTIONSENSE_CMD_LIST_ACTIVITIES;
+	ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+	if (ret)
+		return ret;
+	activities = st->core.resp->list_activities.enabled |
+		     st->core.resp->list_activities.disabled;
+	nb_activities = hweight_long(activities) + 1;

Moving the line just right before the following devm_kcalloc() for st->channels makes more sense.

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +4 -0
+
+	if (!activities)
+		return -ENODEV;
+

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 264

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +10 -0
+	/* Allocate a channel per activity and one for timestamp */
+	st->channels = devm_kcalloc(&pdev->dev, nb_activities,
+				    sizeof(*st->channels), GFP_KERNEL);
+	if (!st->channels)
+		return -ENOMEM;
+
+	channel = &st->channels[0];
+	index = 0;
+	for_each_set_bit(i, &activities, BITS_PER_LONG) {
+		channel->scan_index = index;

It seems the loop body doesn't use the channel->scan_index. Move the assignment to the end of loop body when it's sure to occupy the index?

		channel->scan_index = index++;

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c

Quoted 2 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +2 -0
+
+		/* List all available activities */

Drop the comment as it doesn't reflect what the following code does?

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +9 -0
+		if (i == MOTIONSENSE_ACTIVITY_BODY_DETECTION) {
+			channel->type = IIO_PROXIMITY;
+			channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
+			channel->modified = 0;
+			channel->event_spec = cros_ec_body_detect_events;
+			channel->num_event_specs =
+				ARRAY_SIZE(cros_ec_body_detect_events);
+			st->body_detection_channel_index = index;
+		} else {

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 285

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +10 -0
+			channel->type = IIO_ACTIVITY;
+			channel->modified = 1;
+			channel->event_spec = cros_ec_activity_single_shot;
+			channel->num_event_specs =
+				ARRAY_SIZE(cros_ec_activity_single_shot);
+			if (i == MOTIONSENSE_ACTIVITY_SIG_MOTION) {
+				channel->channel2 = IIO_MOD_STILL;
+				st->sig_motion_channel_index = index;
+			} else {
+				dev_warn(&pdev->dev,

dev.

Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +11 -0
+					 "Unknown activity: %d\n", i);
+				continue;
+			}
+		}
+		channel->ext_info = cros_ec_sensors_limited_info;
+		channel++;
+		index++;
+	}
+
+	/* Timestamp */
+	channel->scan_index = index;

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c line 306

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c +10 -0
+	channel->type = IIO_TIMESTAMP;
+	channel->channel = -1;
+	channel->scan_type.sign = 's';
+	channel->scan_type.realbits = 64;
+	channel->scan_type.storagebits = 64;
+
+	indio_dev->channels = st->channels;
+	indio_dev->num_channels = index + 1;
+
+	st->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;

The statement here isn't intuitive. Can it move somewhere above when setting the st->core? E.g. right after the assignment for st->core.type.