Skip to contentSkip to conversation

iio: cros_ec_sensors: add cros_ec_activity driver

PATCH Superseded Gwendal Grignou 4 participants 6 messages May 23, 2025 – Jun 7, 2025

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

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

 drivers/iio/common/cros_ec_sensors/Kconfig    |   9 +
 drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
 .../common/cros_ec_sensors/cros_ec_activity.c | 322 ++++++++++++++++++
 .../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, 373 insertions(+), 6 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 +322 -0

322 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.1151.ga128411c76-goog
maintainer
#1 lore raw

On Fri, 23 May 2025 10:27:27 -0700
Gwendal Grignou <[email protected]> wrote:

Hi Gwendal,

Quoted 3 lines

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

How do you do proximity from an accelerometer?

Quoted 1 line

- Reports significant motion as an activity event.

This one might be from an accelerometer. In which case is there any information on the algorithm?

Quoted 6 lines

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

Various comments inline.

Biggest question I have is what the significant motion detector is based on. Do we know more about that as I'd prefer it associated with an actual channel if possible?

Also does it only surface as an event? Is there an underlying value to read.

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +0 -0
new file mode 100644
index 000000000000..620203f7aa63

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

Quoted 8 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +8 -0
@@ -0,0 +1,322 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * cros_ec_activity - Driver for activities/gesture recognition.
+ *
+ * Copyright 2025 Google, Inc
+ *
+ * This driver uses the cros-ec interface to communicate with the ChromeOS
+ * EC about activity data. Accelerometer access is presented through iio sysfs.

Maybe update as this isn't returning accelerometer data .

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

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +3 -0
+ */
 
+
+static int ec_sensors_read(struct iio_dev *indio_dev,

Given this only applies to this sensor, maybe needs a more specific name?

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

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +7 -0
+			   struct iio_chan_spec const *chan,
+			   int *val, int *val2, long mask)
+{
+	struct cros_ec_sensors_state *st = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&st->core.cmd_lock);

Include cleanup.h and use

	guard(mutex)(&st->core.cmd_lock);

to simpilfy this.

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

Quoted 8 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +8 -0
+	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;
+		ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+		if (ret == 0) {
+			*val = st->core.resp->get_activity.state;
+			ret = IIO_VAL_INT;

With guard magic, can handle error out path and return directly in good path.

		if (ret)
			return ret;

		*val = ..

		return IIO_VAL_INT;

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

Quoted 3 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +3 -0
+		}
+	} else {
+		ret = -EINVAL;

With guard() magic can just return here.

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +7 -0
+	}
+	mutex_unlock(&st->core.cmd_lock);
+	return ret;
+}
+
+static int cros_ec_read_event_config(struct iio_dev *indio_dev,
+				     const struct iio_chan_spec *chan,

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+				     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;
+
+	mutex_lock(&st->core.cmd_lock);

Similar to below. guard() will simplify this quite a bit.

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

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +4 -0
+	st->core.param.cmd = MOTIONSENSE_CMD_LIST_ACTIVITIES;
+	ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+	if (ret)
+		goto done;

Blank line here.

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

Quoted 13 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +13 -0
+	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));
+		} else {
+			dev_warn(&indio_dev->dev, "Unknown activity: %d\n",
+				 chan->channel2);
+			ret = -EINVAL;

Flip logic and direct returns etc.

Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +11 -0
+		}
+		break;
+	default:
+		dev_warn(&indio_dev->dev, "Unknown channel type: %d\n",
+			 chan->type);
+		ret = -EINVAL;
+	}
+done:
+	mutex_unlock(&st->core.cmd_lock);
+	return ret;
+}

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+
+static int cros_ec_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);
+	int ret = 0;
+
+	mutex_lock(&st->core.cmd_lock);

Guard and direct returns. (See below for more on this)

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

Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +11 -0
+	st->core.param.cmd = MOTIONSENSE_CMD_SET_ACTIVITY;
+	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) {
+			st->core.param.set_activity.activity =
+				MOTIONSENSE_ACTIVITY_SIG_MOTION;
+		} else {

flip logic so error is out of line and good path inline. Then with guard you an directly return for error path.

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

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +9 -0
+			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;

return -EINVAL; when using guard()

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c

Quoted 2 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +2 -0
+	}
+	if (ret == 0) {

Then we only get here if ret == 0 so condition can go away.

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

Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +11 -0
+		st->core.param.set_activity.enable = state;
+		ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+	}
+
+	mutex_unlock(&st->core.cmd_lock);
+	return ret;
+}
+
+static int cros_ec_activity_push_data(struct iio_dev *indio_dev,
+				      s16 *data,
+				      s64 timestamp)

Can reduce wrap and have a parameters share a line. Nice to stay under 80 but this is well below that.

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+{
+	struct ec_response_activity_data *activity_data =
+			(struct ec_response_activity_data *)data;
+	enum motionsensor_activity activity = activity_data->activity;
+	u8 state = activity_data->state;
+	const struct cros_ec_sensors_state *st = iio_priv(indio_dev);
+	const struct iio_chan_spec *chan;
+	const struct iio_event_spec *event;
+	enum iio_event_direction dir;
+	int index;

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+	u64 ev;
+
+	switch (activity) {
+	case MOTIONSENSE_ACTIVITY_BODY_DETECTION:
+		index = st->body_detection_channel_index;
+		dir = state ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
+		break;
+	case MOTIONSENSE_ACTIVITY_SIG_MOTION:
+		index = st->sig_motion_channel_index;
+		dir = IIO_EV_DIR_FALLING;

Falling stillness is I guess a detection of activity... As below though I'd like to know if we have any more info on what this is actually measuring?

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

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +7 -0
+		break;
+	default:
+		dev_warn(&indio_dev->dev, "Unknown activity: %d\n", activity);
+		return 0;
+	}
+	chan = &st->channels[index];
+	event = &chan->event_spec[0];

I'd not have so many local variables. Pick some to put inline. Either these two or ev. Just in the interests of shorter code and little cost to readability.

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

Quoted 3 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +3 -0
+
+	ev = IIO_UNMOD_EVENT_CODE(chan->type, index, event->type, dir);
+	iio_push_event(indio_dev, ev, timestamp);

Blank line here.

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

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +9 -0
+	return 0;
+}
+
+static irqreturn_t cros_ec_activity_capture(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+
+	dev_warn(&indio_dev->dev, "%s: Not Expected\n", __func__);

Add a comment on why.

Quoted 24 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +23 -0
+	return IRQ_NONE;
+}
 
+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(dev, "No CROS EC device found.\n");
+		return -EINVAL;
+	}
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
+					cros_ec_activity_capture);

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+	if (ret)
+		return ret;
+
+	indio_dev->info = &ec_sensors_info;
+	st = iio_priv(indio_dev);
+	st->core.type = st->core.resp->info.type;
+	st->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
+
+	/*
+	 * List all available activities

Single line comment. also, it's kind of obvious given the name of the command so maybe don't bother with the comment at all?

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

Quoted 5 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +5 -0
+	 */
+	st->core.param.cmd = MOTIONSENSE_CMD_LIST_ACTIVITIES;
+	ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+	if (ret)
+		return ret;

I'd put a blank line here to separate the error path out from where we carry on to in the good case.

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+	activities = st->core.resp->list_activities.enabled |
+		     st->core.resp->list_activities.disabled;
+	if (!activities)
+		return -ENODEV;
+
+	/* Allocate a channel per activity and one for timestamp */
+	nb_activities = hweight_long(activities) + 1;
+	st->channels = devm_kcalloc(dev, nb_activities,
+				    sizeof(*st->channels), GFP_KERNEL);
+	if (!st->channels)

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+		return -ENOMEM;
+
+	channel = &st->channels[0];
+	index = 0;
+	for_each_set_bit(i, &activities, BITS_PER_LONG) {
+		/* List all available triggers */
+		if (i == MOTIONSENSE_ACTIVITY_BODY_DETECTION) {
+			channel->type = IIO_PROXIMITY;
+			channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
+			channel->modified = 0;

Not modified is the 'obvious' default so can probably drop that one.

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

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +6 -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 {
+			channel->type = IIO_ACTIVITY;

This is getting a little creative. Do we know anything about what the underlying detection actually is? Activity sensor are often some form of magnitude sensor on a specific type of data. I guess if we have no idea what this then activity detection makes some sense.

Is it only ever an event?

Quoted 27 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +27 -0
+			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(dev, "Unknown activity: %d\n", i);
+				continue;
+			}
+		}
+		channel->ext_info = cros_ec_sensors_limited_info;
+		channel->scan_index = index++;
+		channel++;
+	}
+
+	/* Timestamp */
+	channel->scan_index = index;
+	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;

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+
+	return cros_ec_sensors_core_register(dev, indio_dev,
+					     cros_ec_activity_push_data);
+}
+
+static void cros_ec_sensors_remove(struct platform_device *pdev)
+{
+	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+
+	iio_device_unregister(indio_dev);

Why? The cros_ec_sensors_core_register() uses devm internally to clean this up so I'd not expect to see a remove function at all for one of these drivers

Quoted 19 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +14 -0
+}
+
+static struct platform_driver cros_ec_sensors_platform_driver = {
+	.driver = {
+		.name	= DRV_NAME,
+	},
+	.probe		= cros_ec_sensors_probe,
+	.remove		= cros_ec_sensors_remove,
+};
+module_platform_driver(cros_ec_sensors_platform_driver);
+
+MODULE_DESCRIPTION("ChromeOS EC activity sensors driver");
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_LICENSE("GPL v2");
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0
index 64bada1e8678..1bd07ca3abfe 100644

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

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

No comma on terminating entries / sentinels like this. We don't want it to be easy to add things after them as that should never happen.

Quoted 17 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 -0
+};
+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/platform_data/cros_ec_commands.h +0 -0
index ecf290a0c98f..0f6d53e7cb97 100644
 
 
 struct ec_response_motion_sensor_data {
 	/* Flags for each sensor. */
 	uint8_t flags;

include/linux/platform_data/cros_ec_commands.h struct ec_response_motion_sensor_data { · line 2468

Quoted 5 lines
include/linux/platform_data/cros_ec_commands.h +1 -1
@@ -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];

I dislike careful alignment like this because of the noise it tends to add - this being a classic example.
I'd just not update the existing field and let one through that isn't aligned.

Quoted 27 lines
include/linux/platform_data/cros_ec_commands.h +4 -5
 		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;

struct ec_motion_sense_activity {

include/linux/platform_data/cros_ec_commands.h +1 -0
@@ -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 */

include/linux/platform_data/cros_ec_commands.h struct ec_params_motion_sense { · line 2733

Quoted 6 lines
include/linux/platform_data/cros_ec_commands.h +3 -0
@@ -2716,6 +2728,14 @@ struct ec_params_motion_sense {
 			 */
 			int16_t hys_degree;
 		} tablet_mode_threshold;
+
+		/*
+		 * Used for MOTIONSENSE_CMD_GET_ACTIVITY.

Single line comment works fine here and fits with local style.

		/* Used for MOTIONSENSE_CMD_GET_ACTIVITY */

include/linux/platform_data/cros_ec_commands.h struct ec_response_motion_sense { · line 2856

Quoted 13 lines
include/linux/platform_data/cros_ec_commands.h +6 -0
+		 */
+		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. */

Maybe no fullstop is more consistent with local style? Only a bit visible in this patch and I'm lazy :)

Quoted 6 lines
include/linux/platform_data/cros_ec_commands.h +3 -0
+		struct __ec_todo_unpacked {
+			uint8_t     state;
+		} get_activity;
 	};
 } __ec_todo_packed;
 
#2 lore raw

Jonathan,

Thanks for the thorough review. I just send a v5, much cleaner driver.

Gwendal.

Quoted 11 lines · Jonathan Cameron

On Fri, 23 May 2025 10:27:27 -0700
Gwendal Grignou <[email protected]> wrote:

Hi Gwendal,

Quoted 3 lines

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

How do you do proximity from an accelerometer?

It is a crude proximity detection: The accelerometer is set to ~50Hz and uses a low pass filter to check for vibration.
It is not vibrating (sitting on a table), then it is in off-body mode. It is very conservative, erring on the side of on-body: if the table is in a train, the device may stay in on-body mode. See its implmentation here
(https://chromium.googlesource.com/chromiumos/platform/ec/+/refs/heads/main/common/body_detection.c).
The sensor can be used to leave the device unlocked (see
https://support.google.com/android/answer/9075927#zippy=%2Ckeep-your-phone-unlocked-when-its-on-you)
or kick on the CPU fans earlier to keep the device cooler than when it is on a table.

Quoted 5 lines
Quoted 2 lines

- Reports significant motion as an activity event.

This one might be from an accelerometer. In which case is there any information on the algorithm?

This one looks at a significant motion a few seconds apart. It can come directly from the accelerometer, see BMI160 datasheet
(https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi160-ds000.pdf)
secion 2.6.2. Or it can be implemented in the firmware of the embedded controller/sensor hub.

Quoted 9 lines
Quoted 7 lines

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

Quoted 5 lines

Various comments inline.

Biggest question I have is what the significant motion detector is based on. Do we know more about that as I'd prefer it associated with an actual channel if possible?

These 2 events are indeed currently generated today from the raw output of a single accelerometer. It does not have to be: we could further improve the algorithms by fusionning data from the accelerometer and the gyroscope. As such a virtual sensor is presented to the kernel by the sensor hub just for the activities.

Quoted 3 lines

Also does it only surface as an event? Is there an underlying value to read.

The on-body/off-body algorithm calculates a moving average of the samples variance and uses an internal threshold to decide the device state.
The thresholds are model specific (depends on the sensors/device weight) and a set in the factory.
Same with significant motion; threshold set in the factory based on experiments decides to trigger an event or not.

Quoted 18 lines
Quoted 8 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +0 -0
new file mode 100644
index 000000000000..620203f7aa63

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

Quoted 8 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +8 -0
@@ -0,0 +1,322 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * cros_ec_activity - Driver for activities/gesture recognition.
+ *
+ * Copyright 2025 Google, Inc
+ *
+ * This driver uses the cros-ec interface to communicate with the ChromeOS
+ * EC about activity data. Accelerometer access is presented through iio sysfs.

Maybe update as this isn't returning accelerometer data .

Removed.

Quoted 17 lines

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

Quoted 5 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +3 -0
+ */
 
+
+static int ec_sensors_read(struct iio_dev *indio_dev,

Given this only applies to this sensor, maybe needs a more specific name?

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

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +7 -0
+                        struct iio_chan_spec const *chan,
+                        int *val, int *val2, long mask)
+{
+     struct cros_ec_sensors_state *st = iio_priv(indio_dev);
+     int ret;
+
+     mutex_lock(&st->core.cmd_lock);

Include cleanup.h and use

Quoted 2 lines
        guard(mutex)(&st->core.cmd_lock);

to simpilfy this.

Done. It is indeed much cleaner. I will work on another patch to clean up the rest of the cros_ec drivers.

Quoted 119 lines

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

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +8 -0
+     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;
+             ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+             if (ret == 0) {
+                     *val = st->core.resp->get_activity.state;
+                     ret = IIO_VAL_INT;

With guard magic, can handle error out path and return directly in good path.

                if (ret)
                        return ret;

                *val = ..

                return IIO_VAL_INT;

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

Quoted 3 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +3 -0
+             }
+     } else {
+             ret = -EINVAL;

With guard() magic can just return here.

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +7 -0
+     }
+     mutex_unlock(&st->core.cmd_lock);
+     return ret;
+}
+
+static int cros_ec_read_event_config(struct iio_dev *indio_dev,
+                                  const struct iio_chan_spec *chan,

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+                                  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;
+
+     mutex_lock(&st->core.cmd_lock);

Similar to below. guard() will simplify this quite a bit.

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

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +4 -0
+     st->core.param.cmd = MOTIONSENSE_CMD_LIST_ACTIVITIES;
+     ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+     if (ret)
+             goto done;

Blank line here.

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

Quoted 13 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +13 -0
+     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));
+             } else {
+                     dev_warn(&indio_dev->dev, "Unknown activity: %d\n",
+                              chan->channel2);
+                     ret = -EINVAL;

Flip logic and direct returns etc.

Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +11 -0
+             }
+             break;
+     default:
+             dev_warn(&indio_dev->dev, "Unknown channel type: %d\n",
+                      chan->type);
+             ret = -EINVAL;
+     }
+done:
+     mutex_unlock(&st->core.cmd_lock);
+     return ret;
+}

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+
+static int cros_ec_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);
+     int ret = 0;
+
+     mutex_lock(&st->core.cmd_lock);

Guard and direct returns. (See below for more on this)

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

Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +11 -0
+     st->core.param.cmd = MOTIONSENSE_CMD_SET_ACTIVITY;
+     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) {
+                     st->core.param.set_activity.activity =
+                             MOTIONSENSE_ACTIVITY_SIG_MOTION;
+             } else {

flip logic so error is out of line and good path inline. Then with guard you an directly return for error path.

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

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +9 -0
+                     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;

return -EINVAL; when using guard()

drivers/iio/common/cros_ec_sensors/cros_ec_activity.c

Quoted 2 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +2 -0
+     }
+     if (ret == 0) {

Then we only get here if ret == 0 so condition can go away.

Quoted 14 lines

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

Quoted 11 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +11 -0
+             st->core.param.set_activity.enable = state;
+             ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+     }
+
+     mutex_unlock(&st->core.cmd_lock);
+     return ret;
+}
+
+static int cros_ec_activity_push_data(struct iio_dev *indio_dev,
+                                   s16 *data,
+                                   s64 timestamp)

Can reduce wrap and have a parameters share a line. Nice to stay under 80 but this is well below that.

Done

Quoted 16 lines
Quoted 16 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +15 -0
+{
+     struct ec_response_activity_data *activity_data =
+                     (struct ec_response_activity_data *)data;
+     enum motionsensor_activity activity = activity_data->activity;
+     u8 state = activity_data->state;
+     const struct cros_ec_sensors_state *st = iio_priv(indio_dev);
+     const struct iio_chan_spec *chan;
+     const struct iio_event_spec *event;
+     enum iio_event_direction dir;
+     int index;
+     u64 ev;
+
+     switch (activity) {
+     case MOTIONSENSE_ACTIVITY_BODY_DETECTION:
+             index = st->body_detection_channel_index;
Quoted 8 lines

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

Quoted 5 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +5 -0
+             dir = state ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
+             break;
+     case MOTIONSENSE_ACTIVITY_SIG_MOTION:
+             index = st->sig_motion_channel_index;
+             dir = IIO_EV_DIR_FALLING;

Falling stillness is I guess a detection of activity... As below though I'd like to know if we have any more info on what this is actually measuring?

Since the activity name is "IIO_MOD_STILL", when the sensor detects a significant motion, we are out of still, so the condition is false, transitioning from true (1) to false (0), thus falling. Note the event is a one-off, it has to be retriggered, to prevent an avalanche of significant motion events.

Quoted 9 lines

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

Quoted 8 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +7 -0
+             break;
+     default:
+             dev_warn(&indio_dev->dev, "Unknown activity: %d\n", activity);
+             return 0;
+     }
+     chan = &st->channels[index];
+     event = &chan->event_spec[0];
Quoted 3 lines

I'd not have so many local variables. Pick some to put inline. Either these two or ev. Just in the interests of shorter code and little cost to readability.

Done, removed event.

Quoted 6 lines

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

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +3 -0
+
+     ev = IIO_UNMOD_EVENT_CODE(chan->type, index, event->type, dir);
+     iio_push_event(indio_dev, ev, timestamp);

Blank line here.

Quoted 11 lines

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

Quoted 9 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +9 -0
+     return 0;
+}
+
+static irqreturn_t cros_ec_activity_capture(int irq, void *p)
+{
+     struct iio_poll_func *pf = p;
+     struct iio_dev *indio_dev = pf->indio_dev;
+
+     dev_warn(&indio_dev->dev, "%s: Not Expected\n", __func__);

Add a comment on why.

Done.

Quoted 30 lines
Quoted 30 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +28 -0
+     return IRQ_NONE;
+}
 
+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(dev, "No CROS EC device found.\n");
+             return -EINVAL;
+     }
+
+     indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
+     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);
Quoted 8 lines

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

Quoted 5 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +5 -0
+     st->core.type = st->core.resp->info.type;
+     st->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
+
+     /*
+      * List all available activities

Single line comment. also, it's kind of obvious given the name of the command so maybe don't bother with the comment at all?

Removed.

Quoted 41 lines

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

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +5 -0
+      */
+     st->core.param.cmd = MOTIONSENSE_CMD_LIST_ACTIVITIES;
+     ret = cros_ec_motion_send_host_cmd(&st->core, 0);
+     if (ret)
+             return ret;

I'd put a blank line here to separate the error path out from where we carry on to in the good case.

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+     activities = st->core.resp->list_activities.enabled |
+                  st->core.resp->list_activities.disabled;
+     if (!activities)
+             return -ENODEV;
+
+     /* Allocate a channel per activity and one for timestamp */
+     nb_activities = hweight_long(activities) + 1;
+     st->channels = devm_kcalloc(dev, nb_activities,
+                                 sizeof(*st->channels), GFP_KERNEL);
+     if (!st->channels)

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

Quoted 10 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +10 -0
+             return -ENOMEM;
+
+     channel = &st->channels[0];
+     index = 0;
+     for_each_set_bit(i, &activities, BITS_PER_LONG) {
+             /* List all available triggers */
+             if (i == MOTIONSENSE_ACTIVITY_BODY_DETECTION) {
+                     channel->type = IIO_PROXIMITY;
+                     channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
+                     channel->modified = 0;

Not modified is the 'obvious' default so can probably drop that one.

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

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +6 -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 {
+                     channel->type = IIO_ACTIVITY;
Quoted 4 lines

This is getting a little creative. Do we know anything about what the underlying detection actually is? Activity sensor are often some form of magnitude sensor on a specific type of data. I guess if we have no idea what this then activity detection makes some sense.

As written earlier, the event is triggered when a threshold is crossed. So contrary to other activity sensors like walking or swimming, there is no confidence level, it just happened that we are not still anymore.

For proximity, since the proximity API is used, the output is either far or near, there are no distances.

Quoted 36 lines

Is it only ever an event?

Quoted 33 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +33 -0
+                     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(dev, "Unknown activity: %d\n", i);
+                             continue;
+                     }
+             }
+             channel->ext_info = cros_ec_sensors_limited_info;
+             channel->scan_index = index++;
+             channel++;
+     }
+
+     /* Timestamp */
+     channel->scan_index = index;
+     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;
+
+     return cros_ec_sensors_core_register(dev, indio_dev,
+                                          cros_ec_activity_push_data);
+}
+
+static void cros_ec_sensors_remove(struct platform_device *pdev)
Quoted 8 lines

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

Quoted 4 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +4 -0
+{
+     struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+
+     iio_device_unregister(indio_dev);

Why? The cros_ec_sensors_core_register() uses devm internally to clean this up so I'd not expect to see a remove function at all for one of these drivers

Removed.

Quoted 23 lines
Quoted 23 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +14 -0
+}
+
+static struct platform_driver cros_ec_sensors_platform_driver = {
+     .driver = {
+             .name   = DRV_NAME,
+     },
+     .probe          = cros_ec_sensors_probe,
+     .remove         = cros_ec_sensors_remove,
+};
+module_platform_driver(cros_ec_sensors_platform_driver);
+
+MODULE_DESCRIPTION("ChromeOS EC activity sensors driver");
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_LICENSE("GPL v2");
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -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);
 
Quoted 1 line

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

Quoted 1 line
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -0
+const struct iio_chan_spec_ext_info cros_ec_sensors_limcros_ec_sensors_limited_info

ited_info[] = {

Quoted 9 lines

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

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +6 -0
+     {
+             .name = "id",
+             .shared = IIO_SHARED_BY_ALL,
+             .read = cros_ec_sensors_id
+     },
+     { },

No comma on terminating entries / sentinels like this. We don't want it to be easy to add things after them as that should never happen.

Done for cros_ec_sensors_ext_info as well.

Quoted 14 lines
Quoted 12 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 -0
+};
+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/platform_data/cros_ec_commands.h +0 -0
index ecf290a0c98f..0f6d53e7cb97 100644
Quoted 14 lines

include/linux/platform_data/cros_ec_commands.h struct ec_response_motion_sensor_data { · line 2468

Quoted 9 lines

struct ec_response_motion_sensor_data {
/* Flags for each sensor. */
uint8_t flags;Each sensor is up to 3-axis.

include/linux/platform_data/cros_ec_commands.h +1 -1
@@ -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];

I dislike careful alignment like this because of the noise it tends to add - this being a classic example.
I'd just not update the existing field and let one through that isn't aligned.

Done.

Quoted 46 lines
Quoted 28 lines
include/linux/platform_data/cros_ec_commands.h +4 -5
              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;

struct ec_motion_sense_activity {

include/linux/platform_data/cros_ec_commands.h +1 -0
@@ -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 */

include/linux/platform_data/cros_ec_commands.h struct ec_params_motion_sense { · line 2733

Quoted 6 lines
include/linux/platform_data/cros_ec_commands.h +3 -0
@@ -2716,6 +2728,14 @@ struct ec_params_motion_sense {
                       */
                      int16_t hys_degree;
              } tablet_mode_threshold;
+
+             /*
+              * Used for MOTIONSENSE_CMD_GET_ACTIVITY.

Single line comment works fine here and fits with local style.

                /* Used for MOTIONSENSE_CMD_GET_ACTIVITY */
Quoted 8 lines
include/linux/platform_data/cros_ec_commands.h +5 -0
+              */
+             struct __ec_todo_unpacked {
+                     uint8_t sensor_num;
+                     uint8_t activity;  /* enum motionsensor_activity */
+             } get_activity;
      };
 } __ec_todo_packed;
 
Quoted 8 lines

include/linux/platform_data/cros_ec_commands.h struct ec_response_motion_sense { · line 2856

Quoted 5 lines
include/linux/platform_data/cros_ec_commands.h +1 -0
@@ -2833,6 +2853,10 @@ struct ec_response_motion_sense {
                      uint16_t hys_degree;
              } tablet_mode_threshold;
 
+             /* USED for MOTIONSENSE_CMD_GET_ACTIVITY. */

Maybe no fullstop is more consistent with local style? Only a bit visible in this patch and I'm lazy :)

It is a mix and match in this file. Full stop is supposed to be the norm, but it was not always been the case. Remove full stop to blend with other commands.

Quoted 8 lines
Quoted 7 lines
include/linux/platform_data/cros_ec_commands.h +3 -0
+             struct __ec_todo_unpacked {
+                     uint8_t     state;
+             } get_activity;
      };
 } __ec_todo_packed;
 
#3 lore raw
Quoted 25 lines · Gwendal Grignou
Quoted 1 line
Quoted 1 line
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +0 -0

[...]

Quoted 23 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c struct ec_params_motion_sense { · line 2733

Quoted 7 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +3 -0
@@ -2716,6 +2728,14 @@ struct ec_params_motion_sense {
                       */
                      int16_t hys_degree;
              } tablet_mode_threshold;
+
+             /*
+              * Used for MOTIONSENSE_CMD_GET_ACTIVITY.

Single line comment works fine here and fits with local style.

                /* Used for MOTIONSENSE_CMD_GET_ACTIVITY */
Quoted 12 lines
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +5 -0
+              */
+             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;
 
Quoted 7 lines
Quoted 4 lines

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c struct ec_response_motion_sense { · line 2856

Quoted 1 line
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c +1 -0
+             /* USED for MOTIONSENSE_CMD_GET_ACTIVITY. */

Maybe no fullstop is more consistent with local style? Only a bit visible in this patch and I'm lazy :)

It is a mix and match in this file. Full stop is supposed to be the norm, but it was not always been the case. Remove full stop to blend with other commands.

It seems the full stop is still in v5.

Just a side note: people manually synchronizes the header with [1] when needed. I wouldn't bother with the details in the header as long as the struct content is the same with [1]. Fix it or leave it as is are both fine for me.

[1]: https://chromium.googlesource.com/chromiumos/platform/ec/+/refs/heads/main/include/ec_commands.h

maintainer
#4 lore raw
In reply to Gwendal Grignou
Quoted 16 lines
Quoted 16 lines

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

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +6 -0
+     for_each_set_bit(i, &activities, BITS_PER_LONG) {
+             /* List all available triggers */
+             if (i == MOTIONSENSE_ACTIVITY_BODY_DETECTION) {
+                     channel->type = IIO_PROXIMITY;
+                     channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
+                     channel->modified = 0;  

Not modified is the 'obvious' default so can probably drop that one.

Quoted 6 lines
drivers/iio/common/cros_ec_sensors/cros_ec_activity.c new file +6 -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 {
+                     channel->type = IIO_ACTIVITY;  
Quoted 8 lines
Quoted 4 lines

This is getting a little creative. Do we know anything about what the underlying detection actually is? Activity sensor are often some form of magnitude sensor on a specific type of data. I guess if we have no idea what this then activity detection makes some sense.

As written earlier, the event is triggered when a threshold is crossed. So contrary to other activity sensors like walking or swimming, there is no confidence level, it just happened that we are not still anymore.

The confidence level is a bit of a fiction for all sensors. I just wanted the ABI to allow for algorithms getting more clever over time. Right now they all claim to be 0 or 100.

Anyhow, I don't think that matters anyway.

maintainer
#5 lore raw
In reply to Gwendal Grignou
Quoted 10 lines · On Fri, 23 May 2025 10:27:27 -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.

4 versions in as many days isn't a good way to post to the list.

Please slow it down to give time for more reviews to come in.

Thanks,

Jonathan