Skip to contentSkip to conversation

iio: cros_ec_sensors: add cros_ec_activity driver

PATCH In review Gwendal Grignou 3 participants 4 messages Jun 4, 2025 – Jun 7, 2025

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

PATCH v5 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.

---
Changes in v2:
- fix usage of cros_ec_motion_send_host_cmd
- use dev instead of &pdev->dev
- fix documentation errors.
Changes in v3:
- fix index increment at probe correctly
- prevent sending command on invalid inputs in cros_ec_write_event_config
Changes in v4:
- no need to continue setting command structure on error. Changes in v5:
- Use guard(mutex), simplify error path.
- Use driver name as function prefix.
- Remove cros_ec_sensors_remove function.
- Remove identation of structure field.
- Invert raw value to return 1m when far, 0m when close.

 drivers/iio/common/cros_ec_sensors/Kconfig    |   9 +
 drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
 .../common/cros_ec_sensors/cros_ec_activity.c | 307 ++++++++++++++++++
 .../cros_ec_sensors/cros_ec_sensors_core.c    |  12 +-
 .../linux/iio/common/cros_ec_sensors_core.h   |   1 +
 .../linux/platform_data/cros_ec_commands.h    |  26 +-
 6 files changed, 353 insertions(+), 3 deletions(-)
 create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_activity.c
drivers/iio/common/cros_ec_sensors/Kconfig +9 -0
index fefad9572790..394e319c9c97 100644
@@ -30,3 +30,12 @@ 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 presented by the ChromeOS EC sensor hub.
+	  Activities can be a proximity detector (on body/off body detection)
+	  or a significant motion detector.
+	  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 +307 -0

307 lines not on this page · raw message

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +11 -1
index 64bada1e8678..a47b0cd26bf5 100644
@@ -485,10 +485,20 @@ const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
 		.shared = IIO_SHARED_BY_ALL,
 		.read = cros_ec_sensors_id
 	},
-	{ },
+	{ }
 };
 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 +24 -2
index ecf290a0c98f..c969d74a51c0 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;
@@ -2460,8 +2471,7 @@ struct ec_response_motion_sensor_data {
 			uint32_t    timestamp;
 		};
 		struct __ec_todo_unpacked {
-			uint8_t     activity; /* motionsensor_activity */
-			uint8_t     state;
+			struct ec_response_activity_data activity_data;
 			int16_t     add_info[2];
 		};
 	};
@@ -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,12 @@ 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 +2851,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.1204.g71687c7c1d-goog
Reviewed-by on v5 1/1
#1 lore raw
Quoted 10 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.

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

Quoted 8 lines

---
[...]
Changes in v5:
- Use guard(mutex), simplify error path.
- Use driver name as function prefix.
- Remove cros_ec_sensors_remove function.
- Remove identation of structure field.
- Invert raw value to return 1m when far, 0m when close.

As v5 changed a bit from v4, it'd be better to drop my R-b tag in v5.

Anyway, it overall looks good to me. Only a few minor comments:

Quoted 20 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +20 -0
+static int cros_ec_activity_read_event_config(struct iio_dev *indio_dev,
+					      const struct iio_chan_spec *chan,
+					      enum iio_event_type type,
+					      enum iio_event_direction dir)
+{
+	struct cros_ec_sensors_state *st = iio_priv(indio_dev);
+	int ret;
+
+	if (chan->type != IIO_ACTIVITY && chan->type != IIO_PROXIMITY)
+		return -EINVAL;
+
+	guard(mutex)(&st->core.cmd_lock);
+	st->core.param.cmd = MOTIONSENSE_CMD_LIST_ACTIVITIES;
+	ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+	if (ret)
+		return ret;
+
+	switch (chan->type) {
+	case IIO_PROXIMITY:
+		return !!(st->core.resp->list_activities.enabled &

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+			 (1 << MOTIONSENSE_ACTIVITY_BODY_DETECTION));
+	case IIO_ACTIVITY:
+		if (chan->channel2 == IIO_MOD_STILL) {
+			return !!(st->core.resp->list_activities.enabled &
+				 (1 << MOTIONSENSE_ACTIVITY_SIG_MOTION));
+		} else {
+			dev_warn(&indio_dev->dev, "Unknown activity: %d\n",
+				 chan->channel2);
+			return -EINVAL;
+		}

By referencing to cros_ec_activity_write_event_config(), maybe consider to drop the " else {" and "}".

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

Quoted 8 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +8 -0
+static int cros_ec_activity_write_event_config(struct iio_dev *indio_dev,
+					       const struct iio_chan_spec *chan,
+					       enum iio_event_type type,
+					       enum iio_event_direction dir,
+					       int state)
+{
+	struct cros_ec_sensors_state *st = iio_priv(indio_dev);
+

By referencing to cros_ec_activity_read_event_config(), maybe consider to exit the function earlier:

	if (chan->type != IIO_ACTIVITY && chan->type != IIO_PROXIMITY)
maintainer
#2 lore raw
Quoted 50 lines · On Thu, 5 Jun 2025 06:28:59 +0000 Tzung-Bi Shih <[email protected]> wrote:
Quoted 10 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.

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

Quoted 8 lines

---
[...]
Changes in v5:
- Use guard(mutex), simplify error path.
- Use driver name as function prefix.
- Remove cros_ec_sensors_remove function.
- Remove identation of structure field.
- Invert raw value to return 1m when far, 0m when close.

As v5 changed a bit from v4, it'd be better to drop my R-b tag in v5.

Anyway, it overall looks good to me. Only a few minor comments:

Quoted 25 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +25 -0
+static int cros_ec_activity_read_event_config(struct iio_dev *indio_dev,
+					      const struct iio_chan_spec *chan,
+					      enum iio_event_type type,
+					      enum iio_event_direction dir)
+{
+	struct cros_ec_sensors_state *st = iio_priv(indio_dev);
+	int ret;
+
+	if (chan->type != IIO_ACTIVITY && chan->type != IIO_PROXIMITY)
+		return -EINVAL;
+
+	guard(mutex)(&st->core.cmd_lock);
+	st->core.param.cmd = MOTIONSENSE_CMD_LIST_ACTIVITIES;
+	ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+	if (ret)
+		return ret;
+
+	switch (chan->type) {
+	case IIO_PROXIMITY:
+		return !!(st->core.resp->list_activities.enabled &
+			 (1 << MOTIONSENSE_ACTIVITY_BODY_DETECTION));
+	case IIO_ACTIVITY:
+		if (chan->channel2 == IIO_MOD_STILL) {
+			return !!(st->core.resp->list_activities.enabled &
+				 (1 << MOTIONSENSE_ACTIVITY_SIG_MOTION));
Quoted 8 lines

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

Quoted 5 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +5 -0
+		} else {
+			dev_warn(&indio_dev->dev, "Unknown activity: %d\n",
+				 chan->channel2);
+			return -EINVAL;
+		}  

By referencing to cros_ec_activity_write_event_config(), maybe consider to drop the " else {" and "}".

Agreed. I tweaked this.

Quoted 12 lines

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

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +8 -0
+static int cros_ec_activity_write_event_config(struct iio_dev *indio_dev,
+					       const struct iio_chan_spec *chan,
+					       enum iio_event_type type,
+					       enum iio_event_direction dir,
+					       int state)
+{
+	struct cros_ec_sensors_state *st = iio_priv(indio_dev);
+  

By referencing to cros_ec_activity_read_event_config(), maybe consider to exit the function earlier:

Quoted 1 line
	if (chan->type != IIO_ACTIVITY && chan->type != IIO_PROXIMITY)

In this case we aren't really doing anything before the switch statement, unlike read where we read the data from the device.

So I left this one alone.

Needed another couple of tweaks. Signature of this function now takes a bool state and the { }, fix was already present upstream. Also some mess in the Makefile due to tracing patch that it seems you don't have on your tree.

Please check the result. Pushed out for now as testing but I'll rebase on rc1 sometime soon.

Jonathan

maintainer
#3 lore raw
In reply to Gwendal Grignou
Quoted 10 lines · On Tue, 3 Jun 2025 22:39:03 -0700 Gwendal Grignou <[email protected]> wrote:

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.

Passing comment inline.

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = { · line 488

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -1
index 64bada1e8678..a47b0cd26bf5 100644
@@ -485,10 +485,20 @@ const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
 		.shared = IIO_SHARED_BY_ALL,
 		.read = cros_ec_sensors_id
 	},
-	{ },
+	{ }

Unrelated change. Should really be in a separate patch but meh it's trivial so I don't care enough to insist.

Quoted 16 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +10 -0
 };
 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