Introduce cros_ec power monitoring
cover Introduce cros_ec power monitoring
This series adds the ChromeOS EC On-Device Power Monitoring (cros_ec_pwrmon) driver to read onboard power-monitoring ADCs, such as PAC194x, wired through the embedded controller (EC).
Several ChromeOS platforms are equipped with onboard ADCs that are connected to the EC via I2C bus. The driver interfaces with the EC via the existing EC_CMD_PWRMON host command API (and EC_MKBP_EVENT_PWRMON events) to manage ADC accumulation and expose accumulated per-rail energy through an IIO sysfs attribute.
The driver is only loaded when the underlying hardware feature is present. The series also fixes alignment padding in EC_CMD_PWRMON definitions.
Daniel Balint (3):
platform/chrome: cros_ec: fix alignment of ec_params_pwrmon
iio: adc: add cros_ec_pwrmon driver
mfd: cros_ec: load cros_ec_pwrmon on supported ECs
.../ABI/testing/sysfs-bus-iio-cros-ec | 13 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/cros_ec_pwrmon.c | 333 ++++++++++++++++++ drivers/mfd/cros_ec_dev.c | 9 + .../linux/platform_data/cros_ec_commands.h | 2 + 7 files changed, 376 insertions(+) create mode 100644 drivers/iio/adc/cros_ec_pwrmon.c
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH 1/3 platform/chrome: cros_ec: fix alignment of ec_params_pwrmon
Sync EC_CMD_PWRMON structure padding with ChromeOS EC firmware by adding explicit reserved padding to struct ec_params_pwrmon (2-byte union alignment) and struct pwrmon_dump_info (4-byte struct size alignment), as __ec_align2 and __ec_align4 expand to __packed in Linux.
--- include/linux/platform_data/cros_ec_commands.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/platform_data/cros_ec_commands.h +2 -0
@@ -6635,6 +6635,7 @@ enum ec_pwrmon_cmd {struct ec_params_pwrmon {uint8_t cmd; +uint8_t reserved;union {uint16_t set_rate;uint8_t channel_id;
@@ -6651,6 +6652,7 @@ struct ec_params_pwrmon {struct pwrmon_dump_info {uint8_t channel_id;char channel_name[32 ]; +uint8_t reserved[3 ]; } __ec_align4;struct ec_response_pwrmon {
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH 2/3 iio: adc: add cros_ec_pwrmon driver
In reply to Daniel BalintAdd the ChromeOS EC On-Device Power Monitoring (ODPM) driver to read onboard power-monitoring ADCs (such as PAC194x) wired through the EC.
The driver interfaces with the EC via the existing EC_CMD_PWRMON host command API and EC_MKBP_EVENT_PWRMON events to start/stop accumulation, latch ADC accumulators, and expose cumulative per-rail energy in microjoules (uWs) through the energy_value sysfs attribute.
--- .../ABI/testing/sysfs-bus-iio-cros-ec | 13 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/cros_ec_pwrmon.c | 333 ++++++++++++++++++ 5 files changed, 365 insertions(+) create mode 100644 drivers/iio/adc/cros_ec_pwrmon.c
Documentation/ABI/testing/sysfs-bus-iio-cros-ec +13 -0
@@ -19,3 +19,16 @@ Description: represents the sensor ID as exposed by the EC. This ID is used by the Android sensor service hardware abstraction layer (sensor HAL) through the Android container on ChromeOS. + +What: /sys/bus/iio/devices/iio:deviceX/energy_value +Date: November 2026 +KernelVersion: 7.5 +Contact: [email protected], [email protected] +Description: + This read-only attribute is exposed by the ChromeOS EC power + monitor (cros_ec_pwrmon) driver for On-Device Power Monitoring + (ODPM). Reading it latches all EC power accumulators and returns + a snapshot timestamp followed by per-channel accumulated energy: + + t=<Measurement timestamp in ms since boot> + CH<N>(T=<Duration in ms>)[<Rail schematic name>], <Accumulated energy in uWs>
MAINTAINERS +7 -0
@@ -6335,6 +6335,13 @@ M: Thomas Weißschuh <[email protected]> S: Maintained F: drivers/leds/leds-cros_ec.c +CHROMEOS EC POWER MONITORING +M: Daniel Balint <[email protected]> +L: [email protected] +L: [email protected] +S: Maintained +F: drivers/iio/adc/cros_ec_pwrmon.c + CHROMEOS EC SUBDRIVERS M: Benson Leung <[email protected]> R: Guenter Roeck <[email protected]>
drivers/iio/adc/Kconfig +11 -0
@@ -743,6 +743,17 @@ config CPCAP_ADC This driver can also be built as a module. If so, the module will be called cpcap-adc. +config CROS_EC_PWRMON + tristate "ChromeOS EC Power Monitor Driver" + depends on MFD_CROS_EC_DEV + default MFD_CROS_EC_DEV + help + Say yes here to build support for the ChromeOS EC On-Device Power + Monitoring (ODPM) driver. + + To compile this driver as a module, choose M here: the module will be + called cros_ec_pwrmon. + config DA9150_GPADC tristate "Dialog DA9150 GPADC driver support" depends on MFD_DA9150
drivers/iio/adc/Makefile +1 -0
@@ -63,6 +63,7 @@ obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o obj-$(CONFIG_CC10001_ADC) += cc10001_adc.o obj-$(CONFIG_CPCAP_ADC) += cpcap-adc.o +obj-$(CONFIG_CROS_EC_PWRMON) += cros_ec_pwrmon.o obj-$(CONFIG_DA9150_GPADC) += da9150-gpadc.o obj-$(CONFIG_DLN2_ADC) += dln2-adc.o obj-$(CONFIG_ENVELOPE_DETECTOR) += envelope-detector.o
drivers/iio/adc/cros_ec_pwrmon.c new file +333 -0
333 lines not on this page · raw message
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
PATCH 3/3 mfd: cros_ec: load cros_ec_pwrmon on supported ECs
In reply to Daniel BalintLoad cros_ec_pwrmon driver when the ChromeOS EC advertises EC_FEATURE_PWRMON.
--- drivers/mfd/cros_ec_dev.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/mfd/cros_ec_dev.c +9 -0
@@ -115,6 +115,10 @@ static const struct mfd_cell cros_ec_charge_control_cells[] = { { .name = "cros-charge-control", }, }; +static const struct mfd_cell cros_ec_pwrmon_cells[] = { + { .name ="cros-ec-pwrmon" , }, +}; +static const struct cros_feature_to_cells cros_subdevices[] = { { .id =EC_FEATURE_CEC ,
@@ -151,6 +155,11 @@ static const struct cros_feature_to_cells cros_subdevices[] = { .mfd_cells = cros_ec_charge_control_cells, .num_cells = ARRAY_SIZE(cros_ec_charge_control_cells), }, + { + .id = EC_FEATURE_PWRMON, + .mfd_cells = cros_ec_pwrmon_cells, + .num_cells = ARRAY_SIZE(cros_ec_pwrmon_cells), + }, };static const struct mfd_cell cros_ec_platform_cells[] = {
Signature · 1 line
2.56.0.455.g6c5a503b38-goog
[email protected]
AI reviewRe: [PATCH v1 3/3] mfd: cros_ec: load cros_ec_pwrmon on supported ECs
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mfd: cros_ec: lowercase description in commit subject --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mfd: cros_ec: lowercase description in commit subject --
commit 9d3435eb48d0511f2f3d3ec653ce62f525f2879e
Author: Daniel Balint <[email protected]>
mfd: cros_ec: load cros_ec_pwrmon on supported ECs
This commit registers the cros-ec-pwrmon MFD cell when the ChromeOS EC advertises EC_FEATURE_PWRMON.
Quoted 1 line
mfd:
cros_ec: loadcros_ec_pwrmonon supported ECs
[Severity: Low]
This isn't a bug, but MFD commit subjects capitalize the description after the prefix ("mfd: cros_ec: Load cros_ec_pwrmon on supported ECs").
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH v1 1/3] platform/chrome: cros_ec: fix alignment of ec_params_pwrmon
In reply to Daniel BalintSashiko has reviewed this patch and found no issues.
Quoted 7 lines
Sync
EC_CMD_PWRMONstructure padding with ChromeOS EC firmware by adding explicit reserved padding to structec_params_pwrmon(2-byte union alignment) and structpwrmon_dump_info(4-byte struct size alignment), as__ec_align2and__ec_align4expand to__packedin Linux.Fixes b57af7f8d194 ("platform/chrome: cros_ec: Sync host command header with EC")Signed-off-by Daniel Balint <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v1 2/3] iio: adc: add cros_ec_pwrmon driver
In reply to Daniel BalintThank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] iio: adc: cros_ec_pwrmon: non-standard sysfs table ABI in energy_value_show() - [Medium] iio: adc…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] iio: adc: cros_ec_pwrmon: non-standard sysfs table ABI in energy_value_show()
- [Medium] iio: adc: cros_ec_pwrmon: premature wakeup in cros_ec_pwrmon_notify()
- [Medium] iio: adc: cros_ec_pwrmon: energy loss and time skew in energy_value_show()
--
commit 9eba526765275d37885c463963dacce7dc7badae
Author: Daniel Balint <[email protected]>
iio: adc: add cros_ec_pwrmon driver
This commit adds the ChromeOS EC On-Device Power Monitoring (ODPM) driver to read onboard power-monitoring ADCs via EC_CMD_PWRMON host commands and EC_MKBP_EVENT_PWRMON events, exposing accumulated per-rail energy through an energy_value sysfs attribute.
Quoted 5 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +0
-0
[ ... ]
Quoted 17 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +17
-0+static int cros_ec_pwrmon_notify (struct notifier_block *nb, +unsigned long queued_during_suspend, +void *data) +{ +struct cros_ec_pwrmon_state *st = + container_of(nb, struct cros_ec_pwrmon_state, notifier); +struct cros_ec_device *ec_dev = st->ec; +s64 val; +u8 ch; + +if (ec_dev->event_data.event_type !=EC_MKBP_EVENT_PWRMON ) +return NOTIFY_DONE ; + + ch = ec_dev->event_data.data.pwrmon_data.channel_id; + val =get_unaligned_le64 (&ec_dev->event_data.data.pwrmon_data.value); + + scoped_guard(spinlock, &st->event_lock) {
drivers/iio/adc/cros_ec_pwrmon.c line 82
Quoted 10 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +10
-0+if (!st->collecting) +return NOTIFY_OK ; + +if (ch < st->num_channels) { + st->results[ch].value = val; + st->results[ch].valid = true; + } + +WRITE_ONCE (st->results_received, st->results_received +1 ); + }
[Severity: Medium]
Can cros_ec_pwrmon_notify() wake wait_event_timeout() in energy_value_show() before all valid channels have reported?
st->results_received is incremented unconditionally even when
ch >= st->num_channels (which happens if the EC has more than PWRMON_MAX_CHANNELS channels after cros_ec_pwrmon_probe() clamps st->num_channels) or when st->results[ch].valid is already true. In addition, energy_value_show() sets st->collecting = true before sending EC_PWRMON_LATCH, so a late MKBP event from a previous timed-out latch will also increment st->results_received and cause wait_event_timeout() to finish early while some channels in st->results[] are still invalid.
Only incrementing st->results_received when ch < st->num_channels and !st->results[ch].valid would ensure each counted event corresponds to a distinct valid channel.
Quoted 23 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +23
-0+ +wake_up (&st->wq); + +return NOTIFY_OK ; +} + +/* +* Expose ODPM results in the form of accumulated energy +* through the energy_value sysfs node. +* Format requirement: +* t=<Measurement timestamp, ms> +* CH<N>(T=<Duration, ms>)[<Schematic name>], <Accumulated Energy, uWs> +*/ +static ssize_t energy_value_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +struct cros_ec_pwrmon_state *st =iio_priv (indio_dev); +struct ec_params_pwrmon *params; +u64 current_time_ms; +s64 delta_uws; +u32 duration_ms;
[ ... ]
Quoted 13 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +13
-0+/* Timestamp of snapshot taken. */ + current_time_ms =ktime_to_ms (ktime_get_boottime ()); + len +=sysfs_emit_at (buf, len,"t=%llu\n" , current_time_ms); + + duration_ms = current_time_ms - st->last_time_ms; + +for (i =0 ; i < st->num_channels; i++) { +if (!st->results[i].valid) { +dev_warn (dev,"Missing data for channel %d\n" , i); +continue ; + } + + delta_uws = div_s64(st->results[i].value * duration_ms,
drivers/iio/adc/cros_ec_pwrmon.c line 166
Quoted 10 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +10
-0+ MSEC_PER_SEC); +if (delta_uws >0 ) + st->accumulated_energy[i] += delta_uws; + + len +=sysfs_emit_at (buf, len,"CH%d(T=%u)[%s], %llu\n" , + i, duration_ms, st->channel_names[i], + st->accumulated_energy[i]); + } + + st->last_time_ms = current_time_ms;
[Severity: Medium]
Does advancing a single shared st->last_time_ms unconditionally at the end of energy_value_show() permanently drop accumulated energy for any channel whose MKBP event was missed? When !st->results[i].valid is taken, that channel is skipped, but st->last_time_ms is still updated to current_time_ms, so the next read only integrates that channel from the failed read's timestamp rather than the last time it was accumulated.
Also, sampling current_time_ms after wait_event_timeout() rather than when EC_PWRMON_LATCH completes folds the MKBP delivery delay (up to 1000 ms) into duration_ms. In addition, duration_ms is a u32, which truncates the 64-bit difference current_time_ms - st->last_time_ms if more than ~49.7 days elapse between reads, and st->results[i].value * duration_ms can overflow s64 before div_s64().
drivers/iio/adc/cros_ec_pwrmon.c line 171
Quoted 5 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +5
-0+ +return len; +} + +static IIO_DEVICE_ATTR(energy_value, 0444, energy_value_show, NULL, 0);
[Severity: Medium]
Should this driver expose standard IIO channels (such as IIO_ENERGY with in_energyY_raw or in_energyY_input and in_energyY_label) instead of formatting a multi-line text table into a custom energy_value sysfs attribute on a channel-less iio_dev?
Beyond violating the sysfs one-value-per-file rule and standard IIO ABI, each formatted channel line can be up to 74 bytes when channel_names[i] is 31 bytes, so with up to PWRMON_MAX_CHANNELS (64) channels the output can reach ~4750 bytes and be silently truncated at PAGE_SIZE by sysfs_emit_at().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Jonathan Cameron
maintainerQuoted 4 lines · On Fri, 6 Nov 2026 19:31:14 +0000 Daniel Balint <[email protected]> wrote:
This series adds the ChromeOS EC On-Device Power Monitoring (
cros_ec_pwrmon) driver to read onboard power-monitoring ADCs, such as PAC194x, wired through the embedded controller (EC).
Quoted 8 lines
Several ChromeOS platforms are equipped with onboard ADCs that are connected to the EC via I2C bus. The driver interfaces with the EC via the existing
EC_CMD_PWRMONhost command API (andEC_MKBP_EVENT_PWRMONevents) to manage ADC accumulation and expose accumulated per-rail energy through an IIO sysfs attribute.The driver is only loaded when the underlying hardware feature is present. The series also fixes alignment padding in
EC_CMD_PWRMONdefinitions.
Hi Daniel
I'm not necessarily against this but when adding monitoring components to IIO the usual question is why not hwmon? What features are not well supported by that framework?
Note historically I suspect we accepted one or two drives into IIO that would have fitted better there so precedence isn't a strong argument.
Convention for these somewhat borderline case is +CC hmwon maintainer and list as well as IIO with that explanation in the cover letter. That avoids any last minute surprises!
Jonathan
Quoted 18 lines
Daniel Balint (3):
platform/chrome:cros_ec: fix alignment ofec_params_pwrmon
iio: adc: addcros_ec_pwrmondriver
mfd:cros_ec: loadcros_ec_pwrmonon supported ECs.../ABI/testing/sysfs-bus-iio-cros-ec | 13 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/cros_ec_pwrmon.c | 333 ++++++++++++++++++ drivers/mfd/cros_ec_dev.c | 9 + .../linux/platform_data/cros_ec_commands.h | 2 + 7 files changed, 376 insertions(+) create mode 100644 drivers/iio/adc/cros_ec_pwrmon.cbase-commit:
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Jonathan Cameron
maintainerRe: [PATCH v1 2/3] iio: adc: add cros_ec_pwrmon driver
In reply to Daniel BalintQuoted 9 lines · On Fri, 6 Nov 2026 19:31:16 +0000 Daniel Balint <[email protected]> wrote:
Add the ChromeOS EC On-Device Power Monitoring (ODPM) driver to read onboard power-monitoring ADCs (such as PAC194x) wired through the EC.
The driver interfaces with the EC via the existing
EC_CMD_PWRMONhost command API andEC_MKBP_EVENT_PWRMONevents to start/stop accumulation, latch ADC accumulators, and expose cumulative per-rail energy in microjoules (uWs) through theenergy_valuesysfs attribute.Signed-off-by Daniel Balint <[email protected]>
Hi Daniel
Main comment here is the custom ABI. It doesn't comply with sysfs conventions or the IIO ABI. For this sort of capture lots at the same time we have the IIO chardev 'buffer' interface.
Jonathan
Quoted 14 lines
--- .../ABI/testing/sysfs-bus-iio-cros-ec | 13 + MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/cros_ec_pwrmon.c | 333 ++++++++++++++++++ 5 files changed, 365 insertions(+) create mode 100644 drivers/iio/adc/cros_ec_pwrmon.cDocumentation/ABI/testing/sysfs-bus-iio-cros-ec +0
-0
Documentation/ABI/testing/sysfs-bus-iio-cros-ec Description: · line 23
Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-iio-cros-ec +2
-0@@ -19,3 +19,16 @@ Description: represents the sensor ID as exposed by the EC. This ID is used by the Android sensor service hardware abstraction layer (sensor HAL) through the Android container on ChromeOS. + +What: /sys/bus/iio/devices/iio:deviceX/energy_value
Why custom ABI?
If you need a multi channel snapshot, use buffered IIO interface. That should give you a nice binary representation of this will sysfs description of data backing etc. Hook it up to a sysfs trigger if you want to have userspace request a capture on demand.
As a general rule, multi value sysfs attributes are frowned upon. Occasionally we justify them as a group of numbers that are one value (quaternions for instance) but this one doesn't fit that pattern.
Quoted 22 lines
Documentation/ABI/testing/sysfs-bus-iio-cros-ec +11
-0+Date: November 2026 +KernelVersion: 7.5 +Contact: [email protected], [email protected] +Description: + This read-only attribute is exposed by the ChromeOS EC power + monitor (cros_ec_pwrmon) driver for On-Device Power Monitoring + (ODPM). Reading it latches all EC power accumulators and returns + a snapshot timestamp followed by per-channel accumulated energy: + + t=<Measurement timestamp in ms since boot> + CH<N>(T=<Duration in ms>)[<Rail schematic name>], <Accumulated energy in uWs>drivers/iio/adc/cros_ec_pwrmon.c new file +4
-0@@ -0,0 +1,333 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* ChromeOS EC Power Monitor Driver +*
drivers/iio/adc/cros_ec_pwrmon.c line 14
Quoted 10 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +10
-0+* Copyright (c) 2026 Google Inc. +*/ + +#include <linux/cleanup.h> +#include <linux/device.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/ktime.h> +#include <linux/math64.h> +#include <linux/mod_devicetable.h>
Shouldn't see that in a new driver. The finer grained tables structure defs in linux/device-id are included directly by the relevant bus. Here platform_device.h has it.
Quoted 30 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +29
-0+#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/notifier.h> +#include <linux/platform_data/cros_ec_commands.h> +#include <linux/platform_data/cros_ec_proto.h> +#include <linux/platform_device.h> +#include <linux/spinlock.h> +#include <linux/string.h> +#include <linux/sysfs.h> +#include <linux/types.h> +#include <linux/unaligned.h> +#include <linux/wait.h> + +static void cros_ec_pwrmon_unregister_notifier (void *data) +{ +struct cros_ec_pwrmon_state *st = data; + +blocking_notifier_chain_unregister (&st->ec->event_notifier, + &st->notifier); +} + +static int cros_ec_pwrmon_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct cros_ec_dev *ec_dev =dev_get_drvdata (dev->parent); +struct cros_ec_pwrmon_state *st; +struct ec_params_pwrmon *params; +struct pwrmon_dump_info *dump; +struct iio_dev *indio_dev;
drivers/iio/adc/cros_ec_pwrmon.c
Quoted 10 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +10
-0+size_t msg_size; +int ret, i; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*st)); +if (!indio_dev) +return -ENOMEM ; + + st =iio_priv (indio_dev); + st->ec_dev = ec_dev; + st->ec = ec_dev->ec_dev;
That's somewhat confusing naming wise! Why do we need both of them?
drivers/iio/adc/cros_ec_pwrmon.c line 235
Quoted 9 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +9
-0+ +spin_lock_init (&st->event_lock); +init_waitqueue_head (&st->wq); + + ret =devm_mutex_init (dev, &st->lock); +if (ret) +return ret; + + msg_size = sizeof(*st->msg) + max(sizeof(struct ec_params_pwrmon),
Given you immediately assign params to this, sizeof(*params) would be nicer.
drivers/iio/adc/cros_ec_pwrmon.c line 236
Quoted 1 line
drivers/iio/adc/cros_ec_pwrmon.c new file +1
-0+ sizeof(struct ec_response_pwrmon));
It is a bit of an unusual case but I'd use struct_size() here as it has a trailing u8 data[] into which you are putting the union of those two structures.
Quoted 5 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +5
-0+ st->msg =devm_kzalloc (dev, msg_size,GFP_KERNEL ); +if (!st->msg) +return -ENOMEM ; + + st->msg->version =0 ;
drivers/iio/adc/cros_ec_pwrmon.c line 251
Quoted 10 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +10
-0+ st->msg->command =EC_CMD_PWRMON + st->ec_dev->cmd_offset; + params = (struct ec_params_pwrmon *)st->msg->data; + st->msg->outsize =sizeof (*params); + st->msg->insize =sizeof (struct ec_response_pwrmon ); + +/* Start PACs */ + params->cmd =EC_PWRMON_START ; + ret =cros_ec_cmd_xfer_status (st->ec, st->msg); +if (ret <0 ) { +dev_warn (dev,"Failed to start PACs: %d\n" , ret);
Error out of probe? Use
return dev_err_probe();
Quoted 4 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +4
-0+return ret; + } + st->last_time_ms =ktime_to_ms (ktime_get_boottime ()); +
drivers/iio/adc/cros_ec_pwrmon.c line 265
Quoted 10 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +10
-0+ ret =devm_add_action_or_reset (dev, cros_ec_pwrmon_stop, st); +if (ret) +return ret; + +/* Get Channel Count */ + params->cmd =EC_PWRMON_GET_CHANNEL_COUNT ; + ret =cros_ec_cmd_xfer_status (st->ec, st->msg); +if (ret <0 ) { +dev_warn (dev,"Failed to get channel count: %d\n" , ret); +return ret;
return dev_err_probe()
drivers/iio/adc/cros_ec_pwrmon.c
Quoted 2 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +2
-0+ } + st->num_channels = ((struct ec_response_pwrmon *)st->msg->data)->channel_count;
Use a local variable rather than cast. Added bonus is you can use it for sizing the storage above rather.
drivers/iio/adc/cros_ec_pwrmon.c line 269
Quoted 2 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +2
-0+if (st->num_channels >PWRMON_MAX_CHANNELS ) + st->num_channels =PWRMON_MAX_CHANNELS ;
st->num_channels = min(st->num_channels, PWRMON_MAX_CHANNELS);
drivers/iio/adc/cros_ec_pwrmon.c line 281
Quoted 12 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +12
-0+ + st->accumulated_energy =devm_kcalloc (dev, st->num_channels, +sizeof (*st->accumulated_energy), +GFP_KERNEL ); + st->channel_names =devm_kcalloc (dev, st->num_channels, +sizeof (*st->channel_names), +GFP_KERNEL ); +if (!st->accumulated_energy || !st->channel_names) +return -ENOMEM ; + +/* Get Dump Info (Names) */ +for (i =0 ; i < st->num_channels; i++) {
for (unsigned int i = 0; ...
as might as well keep it locally scoped.
drivers/iio/adc/cros_ec_pwrmon.c line 290
Quoted 9 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +9
-0+ params->cmd =EC_PWRMON_DUMP_INFO ; + params->channel_id = i; + ret =cros_ec_cmd_xfer_status (st->ec, st->msg); +if (ret <0 ) { +dev_warn (dev,"Failed to get dump info for channel %d: %d\n" , + i, ret); +continue ; + } + dump = &((struct ec_response_pwrmon *)st->msg->data)->dump_info;
As above, local variable seems more appropriate.
drivers/iio/adc/cros_ec_pwrmon.c line 295
Quoted 5 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +5
-0+strscpy (st->channel_names[i], dump->channel_name, +sizeof (st->channel_names[i])); + } + + indio_dev->name =dev_name (dev);
Generally we want a part number for dev name. What does this end up as here? Often it is better to hard code them.
drivers/iio/adc/cros_ec_pwrmon.c line 305
Quoted 10 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +10
-0+ indio_dev->info = &cros_ec_pwrmon_info; + indio_dev->modes =INDIO_DIRECT_MODE ; + +/* Register MKBP event notifier */ + st->notifier.notifier_call = cros_ec_pwrmon_notify; + ret =blocking_notifier_chain_register (&st->ec->event_notifier, + &st->notifier); +if (ret <0 ) { +dev_err (dev,"Failed to register notifier: %d\n" , ret); +return ret;
return dev_err_probe()
drivers/iio/adc/cros_ec_pwrmon.c line 309
Quoted 4 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +4
-0+ } + + ret =devm_add_action_or_reset (dev, cros_ec_pwrmon_unregister_notifier, + st);
This one has annoyed me before so maybe not really an ask for you to fix but it would be nice to just have a devm_blocking _notifier_chain_register() that would stash both parameters needed for the unregister.
drivers/iio/adc/cros_ec_pwrmon.c line 317
Quoted 8 lines
drivers/iio/adc/cros_ec_pwrmon.c new file +8
-0+if (ret) +return ret; + +return devm_iio_device_register (dev, indio_dev); +} + +static const struct platform_device_id cros_ec_pwrmon_id[] = { + {"cros-ec-pwrmon" ,0 },
Named initializers for these. This is slowly getting tidied up across the kernel. Not using the named fields makes certain types of refactor trickier to do. Don't set the 0 though as that's what C will fill in anyway and for now there is no use of that value.
drivers/iio/adc/cros_ec_pwrmon.c line 318
Quoted 1 line
drivers/iio/adc/cros_ec_pwrmon.c new file +1
-0+ {}
{ }That one is a trivial IIO style preference. We ended up standardizing that a few years back in the interests of consistency and the choice was with the space (there was no strong reason to go one way or the other!)
Jonathan
Re: [PATCH v1 1/3] platform/chrome: cros_ec: fix alignment of ec_params_pwrmon
In reply to Daniel BalintQuoted 7 lines · Daniel Balint
Sync
EC_CMD_PWRMONstructure padding with ChromeOS EC firmware by adding explicit reserved padding to structec_params_pwrmon(2-byte union alignment) and structpwrmon_dump_info(4-byte struct size alignment), as__ec_align2and__ec_align4expand to__packedin Linux.Fixes b57af7f8d194 ("platform/chrome: cros_ec: Sync host command header with EC")Signed-off-by Daniel Balint <[email protected]>
Quoted 27 lines
--- include/linux/platform_data/cros_ec_commands.h | 2 ++ 1 file changed, 2 insertions(+)include/linux/platform_data/cros_ec_commands.h +2
-0@@ -6635,6 +6635,7 @@ enum ec_pwrmon_cmd {struct ec_params_pwrmon {uint8_t cmd; +uint8_t reserved;union {uint16_t set_rate;uint8_t channel_id;@@ -6651,6 +6652,7 @@ struct ec_params_pwrmon {struct pwrmon_dump_info {uint8_t channel_id;char channel_name[32 ]; +uint8_t reserved[3 ]; } __ec_align4;struct ec_response_pwrmon {Signature · 1 line
2.56.0.455.g6c5a503b38-goog
Re: (subset) [PATCH v1 0/3] Introduce cros_ec power monitoring
In reply to Daniel BalintQuoted 13 lines · Daniel Balint
This series adds the ChromeOS EC On-Device Power Monitoring (
cros_ec_pwrmon) driver to read onboard power-monitoring ADCs, such as PAC194x, wired through the embedded controller (EC).Several ChromeOS platforms are equipped with onboard ADCs that are connected to the EC via I2C bus. The driver interfaces with the EC via the existing
EC_CMD_PWRMONhost command API (andEC_MKBP_EVENT_PWRMONevents) to manage ADC accumulation and expose accumulated per-rail energy through an IIO sysfs attribute.The driver is only loaded when the underlying hardware feature is present. The series also fixes alignment padding in
EC_CMD_PWRMONdefinitions.
Quoted 6 lines
Daniel Balint (3):
platform/chrome:cros_ec: fix alignment ofec_params_pwrmon
iio: adc: addcros_ec_pwrmondriver
mfd:cros_ec: loadcros_ec_pwrmonon supported ECs[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux.git for-next
[1/3] platform/chrome: cros_ec: fix alignment of ec_params_pwrmon
commit: 346739bcdf6577fb5f10845ce339e34e855aae78
Thanks!
Lee Jones
maintainerRe: [PATCH v1 3/3] mfd: cros_ec: load cros_ec_pwrmon on supported ECs
In reply to Daniel BalintQuoted 7 lines · Daniel Balint
Load
cros_ec_pwrmondriver when the ChromeOS EC advertisesEC_FEATURE_PWRMON.Signed-off-by Daniel Balint <[email protected]>--- drivers/mfd/cros_ec_dev.c | 9 +++++++++ 1 file changed, 9 insertions(+)
I can pull this in when Jonathan is happy.
Quoted 30 lines
drivers/mfd/cros_ec_dev.c +9
-0@@ -115,6 +115,10 @@ static const struct mfd_cell cros_ec_charge_control_cells[] = { { .name = "cros-charge-control", }, }; +static const struct mfd_cell cros_ec_pwrmon_cells[] = { + { .name ="cros-ec-pwrmon" , }, +}; +static const struct cros_feature_to_cells cros_subdevices[] = { { .id =EC_FEATURE_CEC ,@@ -151,6 +155,11 @@ static const struct cros_feature_to_cells cros_subdevices[] = { .mfd_cells = cros_ec_charge_control_cells, .num_cells = ARRAY_SIZE(cros_ec_charge_control_cells), }, + { + .id = EC_FEATURE_PWRMON, + .mfd_cells = cros_ec_pwrmon_cells, + .num_cells = ARRAY_SIZE(cros_ec_pwrmon_cells), + }, };static const struct mfd_cell cros_ec_platform_cells[] = {Signature · 1 line
2.56.0.455.g6c5a503b38-goog
Signature · 1 line
Lee Jones
Quoted 6 lines · On Sat, 7 Nov 2026 01:03:56 +0000 Jonathan Cameron <[email protected]> wrote:
Hi Daniel
I'm not necessarily against this but when adding monitoring components to IIO the usual question is why not hwmon? What features are not well supported by that framework?
Quoted 6 lines
Note historically I suspect we accepted one or two drives into IIO that would have fitted better there so precedence isn't a strong argument.
Convention for these somewhat borderline case is +CC hmwon maintainer and list as well as IIO with that explanation in the cover letter. That avoids any last minute surprises!
Hi Jonathan,
Thanks for taking a look!
The main reason for choosing IIO over hwmon is that for On-Device Power Monitoring, userspace needs an atomic, untorn snapshot across all rails (up to 64) over the same integration window, triggered on demand from userspace and read out in a single buffer read rather than repeated per-channel sysfs reads.
Reflecting that requirement, EC_PWRMON_LATCH on the EC is a global, stateful command: a single latch simultaneously snapshots all onboard power accumulators and resets their accumulation window on the EC, so individual channels cannot be latched independently. As far as I know, hwmon only exposes independent per-channel sysfs attributes with no multi-channel buffer or on-demand trigger ABI, whereas IIO's triggered buffer interface is well suited for this kind of pattern.
CC'ing hwmon maintainer and list to keep me honest.
If there is no objection to adding the driver to IIO, I will clean up the custom energy_value attribute and replace it with the standard IIO triggered buffer interface in v2, along with addressing other review comments and including the above explanation in the v2 cover letter.
Thanks,
Daniel
Jonathan Cameron
maintainerQuoted 25 lines · On Wed, 18 Nov 2026 20:45:32 +0000 Daniel Balint <[email protected]> wrote:
Quoted 6 lines · On Sat, 7 Nov 2026 01:03:56 +0000 Jonathan Cameron <[email protected]> wrote:
Hi Daniel
I'm not necessarily against this but when adding monitoring components to IIO the usual question is why not hwmon? What features are not well supported by that framework?
Quoted 6 lines
Note historically I suspect we accepted one or two drives into IIO that would have fitted better there so precedence isn't a strong argument.
Convention for these somewhat borderline case is +CC hmwon maintainer and list as well as IIO with that explanation in the cover letter. That avoids any last minute surprises!
Hi Jonathan,
Thanks for taking a look!
The main reason for choosing IIO over hwmon is that for On-Device Power Monitoring, userspace needs an atomic, untorn snapshot across all rails (up to 64) over the same integration window, triggered on demand from userspace and read out in a single buffer read rather than repeated per-channel sysfs reads.
Quoted 7 lines
Reflecting that requirement,
EC_PWRMON_LATCHon the EC is a global, stateful command: a single latch simultaneously snapshots all onboard power accumulators and resets their accumulation window on the EC, so individual channels cannot be latched independently. As far as I know, hwmon only exposes independent per-channel sysfs attributes with no multi-channel buffer or on-demand trigger ABI, whereas IIO's triggered buffer interface is well suited for this kind of pattern.
Ok, but that's not in the driver yet. I see you refer to that below as a v2 thing, so not a problem.
Do you need that coherent snapshot to get to userspace in one go?
For accumulator channels the way they are normally done to sysfs style reads is to snapshot on each read, update an internal store of accumulation since enable then provide whichever one was requested. That way no data is lost - but you don't get an aligned snapshot. So it depends on what maths you are going to do with them, whether it can be done in kernel and if not how much interchannel skid matters.
So tell us more on why you need that coherent snapshot to userspace.
I agree that if you do, then hwmon wouldn't currently be a good fit (unless I'm also missing a way to do it).
Quoted 10 lines
CC'ing hwmon maintainer and list to keep me honest.
If there is no objection to adding the driver to IIO, I will clean up the custom
energy_valueattribute and replace it with the standard IIO triggered buffer interface in v2, along with addressing other review comments and including the above explanation in the v2 cover letter.Thanks,
Daniel
Guenter Roeck
maintainerQuoted 33 lines · Daniel Balint
Quoted 6 lines · On Sat, 7 Nov 2026 01:03:56 +0000 Jonathan Cameron <[email protected]> wrote:
Hi Daniel
I'm not necessarily against this but when adding monitoring components to IIO the usual question is why not hwmon? What features are not well supported by that framework?
Quoted 6 lines
Note historically I suspect we accepted one or two drives into IIO that would have fitted better there so precedence isn't a strong argument.
Convention for these somewhat borderline case is +CC hmwon maintainer and list as well as IIO with that explanation in the cover letter. That avoids any last minute surprises!
Hi Jonathan,
Thanks for taking a look!
The main reason for choosing IIO over hwmon is that for On-Device Power Monitoring, userspace needs an atomic, untorn snapshot across all rails (up to 64) over the same integration window, triggered on demand from userspace and read out in a single buffer read rather than repeated per-channel sysfs reads.Reflecting that requirement,
EC_PWRMON_LATCHon the EC is a global, stateful command: a single latch simultaneously snapshots all onboard power accumulators and resets their accumulation window on the EC, so individual channels cannot be latched independently. As far as I know, hwmon only exposes independent per-channel sysfs attributes with no multi-channel buffer or on-demand trigger ABI, whereas IIO's triggered buffer interface is well suited for this kind of pattern.
Quoted 2 lines
CC'ing hwmon maintainer and list to keep me honest.
You could accomplish that the same way many hwmon drivers are doing it:
Read all attributes in one go from the hardware, then cache it for a period of time.
Having said that, my experience is that people wanting to implement IIO drivers instead of hardware monitoring drivers keep finding reasons, so I won't object to an IIO implementation.
Guenter
Quoted 11 lines · On Wed, 18 Nov 2026 22:00:33 +0000 Jonathan Cameron <[email protected]> wrote:
Do you need that coherent snapshot to get to userspace in one go?
For accumulator channels the way they are normally done to sysfs style reads is to snapshot on each read, update an internal store of accumulation since enable then provide whichever one was requested. That way no data is lost - but you don't get an aligned snapshot. So it depends on what maths you are going to do with them, whether it can be done in kernel and if not how much interchannel skid matters.
So tell us more on why you need that coherent snapshot to userspace.
Sure, I can provide more context. Usecases are broad, but an example would be that we calculate per-rail drain contributions over short time windows for comparative analysis between device states (device state transitions like screen on/off trigger snapshot), or for debugging and power profiling purposes we correlate fine-grained ODPM data frames (~1s intervals) with other timestamped system diagnostics in our tracing tools (e.g. perform per-process-per-rail energy attribution), so math happens server side, and the relative energy deltas between channels within a frame are just as important to us as their absolute values. In general, data from a single channel makes sense in the context of other channels, there wouldn't be a client that requests only one channel.
At higher userspace sampling frequencies, e.g. ~1s trigger intervals, which can happen in both abovementioned cases, interchannel skid becomes an issue if we read individually.
Because the latching and data delivery (up to 64 MKBP events) roundtrip between EC and kernel takes non-trivial bus time that's in the ~10 ms range, latching per sysfs read across 64 files spreads channels across a hundreds of ms long interval, which would compromise coherency and render the data minimally useful. It would also make the EC unnecessarily busy with redundant work, so latching per channel read isn't really an option.
But to Guenter's point, this could be solved by driver-side caching.
Quoted 3 lines · On Wed, 18 Nov 2026 15:00:20 -0800 Guenter Roeck <[email protected]> wrote:
You could accomplish that the same way many hwmon drivers are doing it:
Read all attributes in one go from the hardware, then cache it for a period of time.
I should have mentioned the importance of timekeeping earlier. As we're correlating the snapshots with other diagnostic time-series data, userspace needs to know when accumulators were latched as accurately as possible.
In contrast to IIO, in hwmon:
(1) There is no timestamp ABI to propagate latch times, so userspace has to rely on its own timestamp when initiating a read.
(2) There is no trigger ABI to force a fresh latch on demand (e.g. on a device state transition), so a userspace timestamp only marks the start of a sysfs read sweep rather than the actual hardware latch.
So, unless I missed something, with a timeout-based cache in hwmon, a reader has no visibility into whether it's hitting newly latched or cached data or when the cache was actually latched (skewing time-series alignment) or whether the cache has expired mid-sweep across 64 sysfs reads.
We could probably tune a short enough cache timeout where time skew would be negligible and mid-sweep expiries would still be infrequent, then it's a trade-off between accuracy and more mid-sweep expirations (more EC roundtrips). It could work, although we still would do 64 sysfs reads instead of 1 on each trigger.
So I guess it's not a must to send the coherent snapshot in one go, and I cannot provide any data to claim that hwmon would be a suboptimal solution to IIO, but our technical judgement is that IIO's explicit trigger, latch timestamp and single buffer read is a cleaner fit for power monitoring via the ChromeOS EC.
Jonathan Cameron
maintainerQuoted 65 lines · On Fri, 20 Nov 2026 09:10:22 +0000 Daniel Balint <[email protected]> wrote:
Quoted 11 lines · On Wed, 18 Nov 2026 22:00:33 +0000 Jonathan Cameron <[email protected]> wrote:
Do you need that coherent snapshot to get to userspace in one go?
For accumulator channels the way they are normally done to sysfs style reads is to snapshot on each read, update an internal store of accumulation since enable then provide whichever one was requested. That way no data is lost - but you don't get an aligned snapshot. So it depends on what maths you are going to do with them, whether it can be done in kernel and if not how much interchannel skid matters.
So tell us more on why you need that coherent snapshot to userspace.
Sure, I can provide more context. Usecases are broad, but an example would be that we calculate per-rail drain contributions over short time windows for comparative analysis between device states (device state transitions like screen on/off trigger snapshot), or for debugging and power profiling purposes we correlate fine-grained ODPM data frames (~1s intervals) with other timestamped system diagnostics in our tracing tools (e.g. perform per-process-per-rail energy attribution), so math happens server side, and the relative energy deltas between channels within a frame are just as important to us as their absolute values. In general, data from a single channel makes sense in the context of other channels, there wouldn't be a client that requests only one channel.
At higher userspace sampling frequencies, e.g. ~1s trigger intervals, which can happen in both abovementioned cases, interchannel skid becomes an issue if we read individually.
Because the latching and data delivery (up to 64 MKBP events) roundtrip between EC and kernel takes non-trivial bus time that's in the ~10 ms range, latching per sysfs read across 64 files spreads channels across a hundreds of ms long interval, which would compromise coherency and render the data minimally useful. It would also make the EC unnecessarily busy with redundant work, so latching per channel read isn't really an option.
But to Guenter's point, this could be solved by driver-side caching.Quoted 3 lines · On Wed, 18 Nov 2026 15:00:20 -0800 Guenter Roeck <[email protected]> wrote:
You could accomplish that the same way many hwmon drivers are doing it:
Read all attributes in one go from the hardware, then cache it for a period of time.I should have mentioned the importance of timekeeping earlier. As we're correlating the snapshots with other diagnostic time-series data, userspace needs to know when accumulators were latched as accurately as possible.
In contrast to IIO, in hwmon:
(1) There is no timestamp ABI to propagate latch times, so userspace has to rely on its own timestamp when initiating a read.
(2) There is no trigger ABI to force a fresh latch on demand (e.g. on a device state transition), so a userspace timestamp only marks the start of a sysfs read sweep rather than the actual hardware latch.So, unless I missed something, with a timeout-based cache in hwmon, a reader has no visibility into whether it's hitting newly latched or cached data or when the cache was actually latched (skewing time-series alignment) or whether the cache has expired mid-sweep across 64 sysfs reads.
Quoted 10 lines
We could probably tune a short enough cache timeout where time skew would be negligible and mid-sweep expiries would still be infrequent, then it's a trade-off between accuracy and more mid-sweep expirations (more EC roundtrips). It could work, although we still would do 64 sysfs reads instead of 1 on each trigger.
So I guess it's not a must to send the coherent snapshot in one go, and I cannot provide any data to claim that hwmon would be a suboptimal solution to IIO, but our technical judgement is that IIO's explicit trigger, latch timestamp and single buffer read is a cleaner fit for power monitoring via the ChromeOS EC.
This is all good information. Capture a short version of it in cover letter for v2 and include a link back to here for anyone wanting the full gory details!
Jonathan