From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795894255; b=rDeALVjB2pA2ZW1MXnQdbWY3E7kKJUFEBCj4g6GGkEm2mKmh96TDxK0L5Z65UIfnWXQF kBUdPmBTGz3LNyGmyQK/vTZRa958JE49JmjaaGhlC7ZYVJjaVGvf8IE3HzSy9ToXz+wr+ Zxwnavvyg9p781p8VTqYeEFyomUJQ6sCA6T46hYjNewupvqx3k+9bPE/dkFkYBbIjfmeu Cyr/F2loJnb0XJTurrRkQHorZmWtH7Uu9tZl46QjPDaMKHNYKDUl0NT/V+grESkjEAc27 fJlGDRcc9CxFuBhUAhGSt9s7bfzMt5iNVB4sqe3V0KhEJO1NG7jcVYJ4SpHYjwFfS9Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795894255; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: In-Reply-To:References:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=qWpDhA6YR5pC8vkMLyqWSkrQXWXCXVL+X9oPIHMSI2E=; b=nTHmHoHmP+gc5Dh2qCn5QcUEAH/Ra6j5Ex7vNEPB0I7ArgS20IZzGwq4WUa9WoWz/m6x 0ybhv1BQWWQJDdXsUyjuNDY7DIrv5yp9cb+wfANjib6bwbV54rhcU9jXFTUasT7ogcxjD ZLjVKLOU/nXtiT1jnJUhqCoWr9hHZMHgSqw+LdDNSc0gnwurrQt+OPq1q04ZfQdWmLkri zlURoGL3IX27myqP8HCbw3+SXHy5xbTrt5iOEB1dO3DExPqIhZW3ud/ARj6On0nYjlsC3 kF9dNzhiyFvWgs6F+tfWYgGmoAqpcP6iUJhUfzsmfOqpKNE71HTnqau8YQFkWztxldQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=J0HUVBRA; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81EF7606A31C; Sat, 28 Nov 2026 19:30:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=J0HUVBRA Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13CD21F00897; Sat, 28 Nov 2026 19:30:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795894255; bh=qWpDhA6YR5pC8vkMLyqWSkrQXWXCXVL+X9oPIHMSI2E=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=J0HUVBRA9hYJs5OXPvpVre4hTO6uctnz9tfEPz8T7vjEaOfI0gPbevnzrxrwOZv+x Hg2oItxpA28ElOa2SXJSzaabFc3lbimlPed91JW6LAGa9Xap+toE6/Abn6q59PdpjG Ah2qkFKYXMsZLEFASeCSwHiavGqat5UgFdSBe8oYnp5icSRYkRbmARFZLl7FeQaXI7 7gcXzvl6fH2GnAlPAbijFkGIFM/4ZMZxRkTYpMrxUr1MsbzHW0/k4TgGScANVTEXWO dV7i0foXUsckhOi7eDfrutRyC1m2LDCPfw551bRZOv+nMA8PTRwzaO4qSAdTRS4kgG 6RaHVn8tb/N2w== Date: Sat, 28 Nov 2026 19:30:48 +0000 From: Jonathan Cameron To: Daniel Balint Cc: Lee Jones , linux-iio@vger.kernel.org, mfd@lists.linux.dev, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, mwojtas@google.com, Benson Leung , Guenter Roeck , chrome-platform@lists.linux.dev, Gwendal Grignou Subject: Re: [PATCH v2 1/2] iio: adc: add cros_ec_pwrmon driver Message-ID: <20261128193025.67ad9040@jic23-hlaptop> In-Reply-To: <20261126104757.2860272-2-dbalint@google.com> References: <20261126104757.2860272-1-dbalint@google.com> <20261126104757.2860272-2-dbalint@google.com> X-Mailer: Claws Mail 4.4.0 (GTK 3.24.52; x86_64-pc-linux-gnu) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 26 Nov 2026 10:47:56 +0000 Daniel Balint 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 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);