From: Jonathan Cameron <jic23@kernel.org>
To: Daniel Balint <dbalint@google.com>
Cc: Lee Jones <lee@kernel.org>, Benson Leung <bleung@chromium.org>,
Guenter Roeck <groeck@chromium.org>,
Gwendal Grignou <gwendal@chromium.org>,
mwojtas@google.com, linux-iio@vger.kernel.org,
chrome-platform@lists.linux.dev, mfd@lists.linux.dev,
linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
Tzung-Bi Shih <tzungbi@kernel.org>
Subject: Re: [PATCH v3 0/2] Introduce cros_ec power monitoring
Date: Sat, 5 Dec 2026 23:21:30 +0000 [thread overview]
Message-ID: <20261205232130.64a8889e@jic23-hlaptop> (raw)
In-Reply-To: <20261130201635.3600292-1-dbalint@google.com>
On Mon, 30 Nov 2026 20:16:33 +0000
Daniel Balint <dbalint@google.com> 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).
This looks good to me, but I'm going to let it sit a while longer
in case any of the cros_ec related folk have time to take a look.
Added Tzung-Bi who has been reviewing some stuff for the
iio/common/cros_ec_sensors/ folder recently and may want to take
a look at this.
Thanks,
Jonathan
>
> Several ChromeOS platforms are equipped with onboard ADCs connected to
> the EC via I2C. 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 per-rail accumulated energy through IIO
> energy channels and a triggered buffer. The driver is only loaded when
> EC_FEATURE_PWRMON is advertised by the EC.
>
> This is a borderline case between IIO and hwmon. IIO is used rather than
> hwmon [1] because userspace queries all rails (up to 64) together as a
> coherent frame -- for example, to compare per-rail drain contributions
> across device-state transitions (such as screen on/off triggering a
> snapshot) and to correlate fine-grained ODPM frames with other
> timestamped system diagnostics for off-device per-process, per-rail
> energy attribution, where relative deltas across channels are as
> important as absolute values. Since a single EC latch and MKBP delivery
> takes non-trivial bus time, latching per sysfs read across 64 files
> would introduce significant inter-channel skid and redundant EC work.
> While driver-side caching in hwmon could avoid per-read latching, hwmon
> has neither an on-demand trigger ABI to force a fresh latch nor a
> timestamp ABI to report the actual latch time (or expose whether a read
> hit cached data or crossed a mid-sweep cache expiry), making IIO's
> explicit trigger, latch timestamp, and single buffer read a cleaner fit.
>
> Reference to detailed v1 discussion:
> [1] https://lore.kernel.org/linux-iio/20261120091022.1459658-1-dbalint@google.com/
>
> Changes in v3:
> - Used IIO_DECLARE_BUFFER_WITH_TS() for the scan buffer in
> struct cros_ec_pwrmon_state.
> - Replaced scoped_guard() with guard() in cros_ec_pwrmon_notify().
> - Switched devm_kcalloc() allocations to devm_kzalloc_objs().
> - Re-initialized struct ec_params_pwrmon and initialized
> struct iio_chan_spec entries with compound literals in probe().
>
> Changes in v2:
> - Dropped v1 patch 1/3 ("platform/chrome: cros_ec: fix alignment of
> ec_params_pwrmon"), already applied to chrome-platform/for-next
> (346739bcdf65).
> - Replaced custom multi-value energy_value sysfs attribute and its ABI
> doc with standard buffer-only IIO_ENERGY channels (.read_label, scale)
> and an IIO triggered buffer with IIO_CHAN_SOFT_TIMESTAMP
> (CLOCK_BOOTTIME); selected IIO_BUFFER and IIO_TRIGGERED_BUFFER in
> Kconfig.
> - Replaced wait_queue_head_t with struct completion.
> - Grouped per-channel state into struct cros_ec_pwrmon_chan, and used
> aligned_u64 scan buffer.
> - Switched EC host commands from legacy cros_ec_cmd_xfer_status() to
> cros_ec_cmd(), which manages msg buffers and cleanup automatically.
> - Dropped redundant st->ec and st->msg fields, used local params/resp
> variables instead of casts.
> - Added an event_size check in the MKBP notifier.
> - Fixed signed/unsigned energy integration and channel counting for
> duplicate/out-of-range MKBP events; returned -EINVAL for missing
> channel labels.
> - Switched probe error paths to dev_err_probe(), clamped channel count
> with min(), hardcoded indio_dev->name to "cros-ec-pwrmon", switched to
> devm_blocking_notifier_chain_register(), dropped direct
> <linux/mod_devicetable.h> include, and used named initializer with
> "{ }" sentinel in cros_ec_pwrmon_id[].
> - Capitalized "Load" in the mfd patch subject.
>
> Daniel Balint (2):
> iio: adc: add cros_ec_pwrmon driver
> mfd: cros_ec: Load cros_ec_pwrmon on supported ECs
>
> MAINTAINERS | 7 +
> drivers/iio/adc/Kconfig | 13 ++
> drivers/iio/adc/Makefile | 1 +
> drivers/iio/adc/cros_ec_pwrmon.c | 377 +++++++++++++++++++++++++++++++
> drivers/mfd/cros_ec_dev.c | 9 +
> 5 files changed, 407 insertions(+)
> create mode 100644 drivers/iio/adc/cros_ec_pwrmon.c
>
> Interdiff against v2:
> diff --git a/drivers/iio/adc/cros_ec_pwrmon.c b/drivers/iio/adc/cros_ec_pwrmon.c
> index 685a713dfee2..a477b1790394 100644
> --- a/drivers/iio/adc/cros_ec_pwrmon.c
> +++ b/drivers/iio/adc/cros_ec_pwrmon.c
> @@ -53,8 +53,7 @@ struct cros_ec_pwrmon_state {
> unsigned int results_received;
> bool collecting;
> u64 last_time_ms;
> - /* Buffer for enabled channel energies plus 64-bit soft timestamp */
> - aligned_u64 scan[PWRMON_MAX_CHANNELS + 1];
> + IIO_DECLARE_BUFFER_WITH_TS(u64, scan, PWRMON_MAX_CHANNELS);
> };
>
> /*
> @@ -79,17 +78,17 @@ static int cros_ec_pwrmon_notify(struct notifier_block *nb,
> ch = cros_ec->event_data.data.pwrmon_data.channel_id;
> val = get_unaligned_le64(&cros_ec->event_data.data.pwrmon_data.value);
>
> - scoped_guard(spinlock, &st->event_lock) {
> - if (!st->collecting)
> - return NOTIFY_OK;
> -
> - if (ch < st->num_channels && !st->chans[ch].valid) {
> - st->chans[ch].latched_power = val;
> - st->chans[ch].valid = true;
> - st->results_received++;
> - if (st->results_received == st->num_channels)
> - complete(&st->comp);
> - }
> + guard(spinlock)(&st->event_lock);
> +
> + if (!st->collecting)
> + return NOTIFY_OK;
> +
> + if (ch < st->num_channels && !st->chans[ch].valid) {
> + st->chans[ch].latched_power = val;
> + st->chans[ch].valid = true;
> + st->results_received++;
> + if (st->results_received == st->num_channels)
> + complete(&st->comp);
> }
>
> return NOTIFY_OK;
> @@ -242,7 +241,7 @@ 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_device *cros_ec = ec_dev->ec_dev;
> - struct ec_params_pwrmon params = {};
> + struct ec_params_pwrmon params;
> struct ec_response_pwrmon resp;
> struct cros_ec_pwrmon_state *st;
> struct iio_chan_spec *iio_chans;
> @@ -265,7 +264,9 @@ static int cros_ec_pwrmon_probe(struct platform_device *pdev)
> return ret;
>
> /* Start power accumulators */
> - params.cmd = EC_PWRMON_START;
> + params = (struct ec_params_pwrmon) {
> + .cmd = EC_PWRMON_START,
> + };
> ret = cros_ec_cmd(cros_ec, 0, EC_CMD_PWRMON + ec_dev->cmd_offset,
> ¶ms, sizeof(params), NULL, 0);
> if (ret < 0)
> @@ -277,7 +278,9 @@ static int cros_ec_pwrmon_probe(struct platform_device *pdev)
> return ret;
>
> /* Get Channel Count */
> - params.cmd = EC_PWRMON_GET_CHANNEL_COUNT;
> + params = (struct ec_params_pwrmon) {
> + .cmd = EC_PWRMON_GET_CHANNEL_COUNT,
> + };
> ret = cros_ec_cmd(cros_ec, 0, EC_CMD_PWRMON + ec_dev->cmd_offset,
> ¶ms, sizeof(params), &resp, sizeof(resp));
> if (ret < 0)
> @@ -287,30 +290,34 @@ static int cros_ec_pwrmon_probe(struct platform_device *pdev)
> if (!st->num_channels)
> return dev_err_probe(dev, -ENODEV, "No power monitor channels\n");
>
> - st->chans = devm_kcalloc(dev, st->num_channels, sizeof(*st->chans),
> - GFP_KERNEL);
> + st->chans = devm_kzalloc_objs(dev, *st->chans, st->num_channels);
> if (!st->chans)
> return -ENOMEM;
>
> - iio_chans = devm_kcalloc(dev, st->num_channels + 1, sizeof(*iio_chans),
> - GFP_KERNEL);
> + iio_chans = devm_kzalloc_objs(dev, *iio_chans, st->num_channels + 1);
> if (!iio_chans)
> return -ENOMEM;
>
> /* Get Dump Info (Names) */
> for (unsigned int i = 0; i < st->num_channels; i++) {
> - iio_chans[i].type = IIO_ENERGY;
> - iio_chans[i].indexed = 1;
> - iio_chans[i].channel = i;
> - iio_chans[i].info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
> - iio_chans[i].scan_index = i;
> - iio_chans[i].scan_type.sign = 'u';
> - iio_chans[i].scan_type.realbits = 64;
> - iio_chans[i].scan_type.storagebits = 64;
> - iio_chans[i].scan_type.endianness = IIO_CPU;
> -
> - params.cmd = EC_PWRMON_DUMP_INFO;
> - params.channel_id = i;
> + iio_chans[i] = (struct iio_chan_spec) {
> + .type = IIO_ENERGY,
> + .indexed = 1,
> + .channel = i,
> + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
> + .scan_index = i,
> + .scan_type = {
> + .sign = 'u',
> + .realbits = 64,
> + .storagebits = 64,
> + .endianness = IIO_CPU,
> + },
> + };
> +
> + params = (struct ec_params_pwrmon) {
> + .cmd = EC_PWRMON_DUMP_INFO,
> + .channel_id = i,
> + };
> ret = cros_ec_cmd(cros_ec, 0,
> EC_CMD_PWRMON + ec_dev->cmd_offset,
> ¶ms, sizeof(params), &resp, sizeof(resp));
>
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
prev parent reply other threads:[~2026-12-05 23:21 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 20:16 Daniel Balint
2026-11-30 20:16 ` [PATCH v3 1/2] iio: adc: add cros_ec_pwrmon driver Daniel Balint
2026-11-30 20:16 ` [PATCH v3 2/2] mfd: cros_ec: Load cros_ec_pwrmon on supported ECs Daniel Balint
2026-12-05 23:21 ` Jonathan Cameron [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205232130.64a8889e@jic23-hlaptop \
--to=jic23@kernel.org \
--cc=bleung@chromium.org \
--cc=chrome-platform@lists.linux.dev \
--cc=dbalint@google.com \
--cc=groeck@chromium.org \
--cc=gwendal@chromium.org \
--cc=lee@kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mfd@lists.linux.dev \
--cc=mwojtas@google.com \
--cc=tzungbi@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®