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=1796512898; b=SSPTNfHSw43ZaUet6Wnf/KK68FxhQCismnGzT3igpBUp4i+1/x4WuxyUc69QeeUtGFth LZoXI1oc6Lp2yWjaJKDPwbTjpF3ajocmggFQ24YSXZz/NzchhjEI0vN6oqud5BD+1qLJn msktMgCO5WaIdkDmQFLBFxKmhFQFlCUJPm3z4EPJzdgiprBEJdOCIi9m50o+qfyP2vklr rE+geJw/53wtjO90zAmWCC6/cOKPWUSJqwrwqO7tGLL7SWCdGBf9NkqdKlHTehHtBW42/ v1fVEznoi0DjC5TYY36FLZtVWeq7BdHI5G8pajT8reM/0Eq7ZiJIsXpFdLOqsxAWQhg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512898; 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=6NxvjbUfastBM5I/5KoZSW8HsGXWRLElv66CMeZV1hc=; b=hHg/nc4XXmHVvpjKvs5M/xF3qLjmp5StJu43ww88F+0ng1m750q0T86QMqrbvdM6q47u LW5FpJWZLnsXSZy6hlpySKr/9eumzsG0pji2cbF+BM/2o4bziEcy9PEL3/rPIV5HBOaQ4 t+y7OC5OuoQB67Vtbyi7a9Vw5gX4aozZBqEk74shTARr0Es48m3ShJ2ZfUHWZx0nOBf5O IR4LdYzxfjL2pPPWHuVkuuyJ0Cl8Q6dANGigFDwckuwPcGZ42ZIvwnZtgQGBNub1HqEhh xXiLoTyKqzRHs3iuIPeAuWm2m0AeIdY/nIEFf8tGof6zrpYe94cW2zxVKoNhgwdeZdg== 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=fEGIa1Vi; 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 942EA606A31F; Sat, 5 Dec 2026 23:21:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fEGIa1Vi Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B5851F00936; Sat, 5 Dec 2026 23:21:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796512898; bh=6NxvjbUfastBM5I/5KoZSW8HsGXWRLElv66CMeZV1hc=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=fEGIa1ViQ6YYLPOs6+sQmf19U3oxLatXt940Oj/1UJ7kHeRC9LqgHQlCF5stqLeHP YsOVJu5DbYBwfkDn0SyoQ7pVaMswoZSCEnQUE1DsEW/MMfxrH+JLupNi53nJDcGXlc usbJnZTRcXpa0qbWscexWGSIL+YsPqZFpqvQraw+7BmbG38ETxeJ2UFvTtgN80X8p3 PUgZiktuZF2B0rwOVsYFqMGpLk3u9g8BgGiFOSHQkND1cfBm9dirkLRSRUkJWEz4or p+a+/knZt2allVMTSMEhHtC9xyMCjPVc2Z9fEkjHPTfe9RwVY+/ZoPe+zqUGx++zYa mrj9KY9NMsiqw== Date: Sat, 5 Dec 2026 23:21:30 +0000 From: Jonathan Cameron To: Daniel Balint Cc: Lee Jones , Benson Leung , Guenter Roeck , Gwendal Grignou , 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 Subject: Re: [PATCH v3 0/2] Introduce cros_ec power monitoring Message-ID: <20261205232130.64a8889e@jic23-hlaptop> In-Reply-To: <20261130201635.3600292-1-dbalint@google.com> References: <20261130201635.3600292-1-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 Mon, 30 Nov 2026 20:16:33 +0000 Daniel Balint 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 > 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