Re: [PATCH v2 1/2] iio: adc: add cros_ec_pwrmon driver
From: Jonathan Cameron
Date: Sat Nov 28 2026 - 14:31:03 EST
On Thu, 26 Nov 2026 10:47:56 +0000
Daniel Balint <dbalint@xxxxxxxxxx> 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_PWRMON host
> command API and EC_MKBP_EVENT_PWRMON events to start/stop accumulation,
> latch ADC accumulators, and expose cumulative per-rail energy through
> IIO energy channels and a triggered buffer.
>
> The driver is intentionally buffer-only (no single-shot RAW reads), as
> EC_PWRMON_LATCH snapshots all rails simultaneously into a timestamped
> frame.
>
> Signed-off-by: Daniel Balint <dbalint@xxxxxxxxxx>
Given it's a cros_ec driver I've added a few more to the +CC, so they are
aware the driver is under review.
The feedback I had is all very minor stuff.
> ---
> MAINTAINERS | 7 +
> drivers/iio/adc/Kconfig | 13 ++
> drivers/iio/adc/Makefile | 1 +
> drivers/iio/adc/cros_ec_pwrmon.c | 370 +++++++++++++++++++++++++++++++
> 4 files changed, 391 insertions(+)
> create mode 100644 drivers/iio/adc/cros_ec_pwrmon.c
> diff --git a/drivers/iio/adc/cros_ec_pwrmon.c b/drivers/iio/adc/cros_ec_pwrmon.c
> new file mode 100644
> index 000000000000..685a713dfee2
> --- /dev/null
> +++ b/drivers/iio/adc/cros_ec_pwrmon.c
> +struct cros_ec_pwrmon_state {
> + struct device *dev;
> + struct cros_ec_dev *ec_dev;
> + /* Serializes EC host commands and energy accumulation state */
> + struct mutex lock;
> + /* Protects MKBP event results and collection state */
> + spinlock_t event_lock;
> + struct notifier_block notifier;
> + struct completion comp;
> + struct cros_ec_pwrmon_chan *chans;
> + unsigned int num_channels;
> + unsigned int results_received;
> + bool collecting;
> + u64 last_time_ms;
> + /* Buffer for enabled channel energies plus 64-bit soft timestamp */
Can we use IIO_DECLARE_BUFFER_WITH_TS(u64, PWRMON_MAX_CHANNELS); for this
then the comment should become unneeded I think. Also avoids people wondering
why we need alignment for the channels (it's there for the timestamp
but that is perhaps not that obvious!)
> + aligned_u64 scan[PWRMON_MAX_CHANNELS + 1];
> +};
> +
> +/*
> + * MKBP event handler -- called once per channel event from the EC.
> + */
> +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 *cros_ec = st->ec_dev->ec_dev;
> + s64 val;
> + u8 ch;
> +
> + if (cros_ec->event_data.event_type != EC_MKBP_EVENT_PWRMON)
> + return NOTIFY_DONE;
> +
> + if (cros_ec->event_size < sizeof(cros_ec->event_data.data.pwrmon_data))
> + return NOTIFY_OK;
> +
> + 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) {
Up to you given lots of scoped_guard() in here where they are needed but
you could use a simple guard() here.
> + 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;
> +}
> +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_response_pwrmon resp;
> + struct cros_ec_pwrmon_state *st;
> + struct iio_chan_spec *iio_chans;
> + struct iio_dev *indio_dev;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + st = iio_priv(indio_dev);
> + st->dev = dev;
> + st->ec_dev = ec_dev;
> +
> + spin_lock_init(&st->event_lock);
> + init_completion(&st->comp);
> +
> + ret = devm_mutex_init(dev, &st->lock);
> + if (ret)
> + return ret;
> +
> + /* Start power accumulators */
> + params.cmd = EC_PWRMON_START;
Given params has several fields only some of which are used for certain commands
it might be clearer to do
params = (struct ec_params_pwrmon) {
.cmd = EC_PWRMON_START,
};
so it is clear that whole thing is being reinitialized each time.
This one is up to you given it does bloat the code a bit for a fairly minor
gain.
> + ret = cros_ec_cmd(cros_ec, 0, EC_CMD_PWRMON + ec_dev->cmd_offset,
> + ¶ms, sizeof(params), NULL, 0);
> + if (ret < 0)
> + return dev_err_probe(dev, ret, "Failed to start PACs\n");
> + st->last_time_ms = ktime_to_ms(ktime_get_boottime());
> +
> + 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(cros_ec, 0, EC_CMD_PWRMON + ec_dev->cmd_offset,
> + ¶ms, sizeof(params), &resp, sizeof(resp));
> + if (ret < 0)
> + return dev_err_probe(dev, ret, "Failed to get channel count\n");
> +
> + st->num_channels = min(resp.channel_count, PWRMON_MAX_CHANNELS);
> + 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);
> + if (!st->chans)
> + return -ENOMEM;
> +
> + iio_chans = devm_kcalloc(dev, st->num_channels + 1, sizeof(*iio_chans),
> + GFP_KERNEL);
You crossed with the patch switching all these calls to devm_kzalloc_objs()
but to reduce churn next cycle, perhaps good to base on that now.
> + 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;
I'd do this lot as the following as it's a little more readable.
iio_chans[i] = (stuct iio_chan_info) {
.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.cmd = EC_PWRMON_DUMP_INFO;
> + params.channel_id = i;
> + ret = cros_ec_cmd(cros_ec, 0,
> + EC_CMD_PWRMON + ec_dev->cmd_offset,
> + ¶ms, sizeof(params), &resp, sizeof(resp));
> + if (ret < 0) {
> + dev_warn(dev, "Failed to get dump info for channel %u: %d\n",
> + i, ret);
> + continue;
> + }
> + strscpy(st->chans[i].name, resp.dump_info.channel_name,
> + sizeof(st->chans[i].name));
> + }
> + iio_chans[st->num_channels] = IIO_CHAN_SOFT_TIMESTAMP(st->num_channels);