From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.208.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796069803; b=ia7N2lDxwgRO22oMMhfYO6hIyhoih4X/dU3+h/N8WYnRqmGzS+kV61JuBupTWkBqEGxq I2/n7uwdHhAREGzMJsZhEOk3o4/M9EueKL+16ktyLUo8h+EMyiMG3XUi2whTiZS3ezYnU MC8HNINsz1i9Eb2TjmHQRI7UEZFIBg2sA7vvipfQi3SH34UeywLgLyp1T2OeE5sCy/opD SHXr09gkynI0fVtbRWrlo9BfIctFKVVpMHg6lM5Ot17MypGJyYO/uF7/BAAjpWPl9g/fP FszcOOAiJGX6UHTra1IgfNnYDAa0dtNO0PqJIiC2ZdYiKk7ZqDoD3vklPWfzk7qTzhA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796069803; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=RE3JaBNA6euA2xjF+6yNecZQIXYQzlOqwTl60BOvXAc=; b=Rp22x/+kB/KeBU1acbQMes3F5TwJ+f0oTmJI/uDemun27E1PKORhbC5XoKBIkkA1hLie 8olDsSZXZ+csUFCeP66CziZl22l5iUBEfXJigkxEhjuFQrBS+A0y1BNC85zWALLy7w5GU p/8ZwUr9yRRS43OvBwn9paOFt2TiZ6Ltai79KC+q8jjzjDxx5rSfNKi01Hz5XsKnO3JcX J2iMUsXKmojQoFQOZilalvsGQMZJHJwNKJihXokYLpA54krMJGi57j2Pk31cZn9/gxPEQ vKNzUZBy/pw9aELtEE+VEDVRFaf/5cFuabkP0VNTpmZPAfDsoETeMKNASIwYxnmft7g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--dbalint.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="j+77gh/a"; arc=none smtp.remote-ip=209.85.208.199 Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6AE8B606A32A for ; Mon, 30 Nov 2026 20:16:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6AE8B606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dbalint.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=j+77gh/a Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-3ab32495d61so57625951fa.3 for ; Mon, 30 Nov 2026 12:16:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796069801; x=1796674601; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=RE3JaBNA6euA2xjF+6yNecZQIXYQzlOqwTl60BOvXAc=; b=j+77gh/apc8FPC6gq6kwFW4DIoKVDoyRKcnGNLMHlD39Sf0L8WT/DdOX1vsI8tgEVU zymGdsIoV3UoVhmTnLTDu6FeHf11Wq5Cj2IUpL1E4Y6lMlQsV1dIlBMrL29nzXwfaDL2 1SXaYiSWLpzPXZ3WuVdhPk4kIWXcEKR5I4hfhuJ8fJp2IlTLZ/nQa9/0jx1DCuhQ6aGH hLgaqmCKZrU8Bz0VE3+4ahejfZlG2W4kEZ5mQofWyO+8/q/LwSrj+BkYI66UR6wbctss YmG4ITC+dez8RhIYJ4msLRmjhuGMPzfFaNG/Tj/uVCq4kjQttcsBF+NFCER0LxUdxENO loRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796069801; x=1796674601; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=RE3JaBNA6euA2xjF+6yNecZQIXYQzlOqwTl60BOvXAc=; b=zDOJioe56ykPZ2FgISbrxw56aUodw7ww+7nMowilTk4sDLNOvlCGlNUhKdKfwj+36s 7EDRex+21QSVDjst51G87VXl5bVaWUhSpV233hX1AI1Z5P32AGZxjT95vrKbH8f1EOwB QbQGbPLwOEJIgcJYXi6cNq7JKDam73TxgjE3akvTi9chY5P07OOwjWCek+zpeytDy/6r 4CGGWKR2wJVIOfbhW0nkEBcLlJLDXUjndGDkI7BYqfTKVWf2oVhIMVwZ0k3l3EGhQZxf +N4TlsjpcMh9AUgKHj/rnIibDEy71azoJGtTnJ+tdK8kwLbrXmBjdS6qH3hh9hBu3n+A 823A== X-Forwarded-Encrypted: i=1; AL+hErFcQUSlQkj/Sl84tTTe72ArqKYqz5RfXkt2Wl3az/7msLam4dae9m9xfP138qyKkf8fxkWxvJe18xAA4ufrvok=@lists.linux.dev X-Gm-Message-State: APuU4Z/cypQnoKFqv0nhizKmZ9Z2R44R6EGEL4/tLN53xf+By8HxTltT 52reohvbJosVmx64F+DPQCfHsqMGU0VxQsVV97yqpRwjdk573RjZQ3gYbohveUDJSCyzqDBGfB4 d2POngSrRjQ== X-Received: from lft23.prod.google.com ([2002:ac2:4837:0:b0:5c3:5ca7:a53d]) (user=dbalint job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6512:2312:b0:5c1:4fda:a97a with SMTP id 2adb3069b0e04-5c1bc7cf9d7mr9577771e87.24.1796069800426; Mon, 30 Nov 2026 12:16:40 -0800 (PST) Date: Mon, 30 Nov 2026 20:16:33 +0000 X-Mailing-List: chrome-platform@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261130201635.3600292-1-dbalint@google.com> Subject: [PATCH v3 0/2] Introduce cros_ec power monitoring From: Daniel Balint To: Jonathan Cameron , Lee Jones Cc: 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, Daniel Balint Content-Type: text/plain; charset="UTF-8" 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 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 -- 2.56.1.911.g883d8bcb20-goog