From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796069802; b=bgywVKhivc1tm8ODXlDh10IRJfGbYNZjOdBpHGt9vN3h0Bw3rjopS3Jxt/1qAuCR2rv9 M6lKFdoFsUxv8M4Lqs1+SAMRcvY1Iwl8N50Ud/F1NiTlMRyPGGk4rmyrgLcNEstppVOev IebqaSjylu5jLnQZgMXlJafkx0l3LS9CZOW5UX29C2nYwoj0UBsyi/z4PfJYY4kATA9gv AwBU9WbL5OJ2rvZnysC0ldgzdtrphxk2vb1jXaMaxtLfsPog35AbrqYbH9pJrdE7jUKeT doLJ2FrhhqIKqsPnagiodsNul8hod6bTSg3lNaILNCqfquk6ysfZCdwmaCuZDnLtQEQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796069802; 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=QTqCyGSGwOOBRwwGS7xsQ3vdRCzZFO4TNV5NGv/N0JaVbyoYRgZVFNV3rXsn09QrVvKl 29S7ACMpfLB9tFTpTalf0t3qJZHsh9FLJZ82H6D+FoiJEQfFCY8+H4qhWZXX58wJURSnO qTsQ7OmzU0AWvO2TNk7mE7NbuI/iOujtxOQZ7QASGbhGrzbSGggsMVhz2Pzr6GG8yfNDJ uECqW940qoySgamWsVbfJ3A2Tlv63S3z6t189flz2JIGejv1kR3izYXXIXnZAk4awWUDg Gy/ivD/QOSwZ+7r089AtEyCWQF2tOZsc4R1vJO16fMT8oFKuuwgXmhHvWuZCHoj9OkA== 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=modgq+iy; arc=none smtp.remote-ip=209.85.167.72 Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) (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 695F4606A327 for ; Mon, 30 Nov 2026 20:16:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 695F4606A327 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=modgq+iy Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-5c34cc38174so1000493e87.1 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=1796069800; x=1796674600; darn=vger.kernel.org; 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=modgq+iyVMiJL/qKMk1S2ameuMeDocIwggF4kCNlEJzlD85eCgSpQ2v6RyAu+gkMoX idTyrsHKIQOkmsgVZj3yehXuPI9DMONkehbQQNUdBR02zkJHQebeKdBEIYRLdG0ILBKu ueoO6q1WSB4ZHTZODLOT+/mpSLvmzGz8t5IwxyPVPUw+9RNGIikmbTDZMdT0yP+O8npq zP5K+pyoSW6DzduHynyfgCR3tHT3a+yTV0lIzgA1d3iZ6sSsGTOGXvfrOirR1s34bwap EOpXUoKzcYgwqOyzB9PqwvXd832nUxr+Fnn1meuOWI20ZSvVUYwlnf8H+93DPj7wTbxn 7dCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796069800; x=1796674600; 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=kKzGMkntAT5rJqnIWNjcfqlszB7DAp2eym+RVjuu6jdRDghdJ9Fn+z1+H7A3/GN7RE cR86P0htxN2nbiqaXlYDPzK3fUGojIC8Ry6CGIY+4esinJcRSEzxpXONvg/QtCXDm2mv azOiPQ5Wh3eyVOP6oAOKUeWwtNm9wM8FOCFO+ubAY9+pkQbFLgsweKoRxdOh6CKPQz4S LJVPp+/zsXYHNHVRjjjYqtzxulMr0mAJ8u3LkCHPK4N7jzjBc3CZj7Qnuhm/hK0U79kq iPdSY/zZJ2wuR+dwlJHOfDw5B0t/gaGjzjHsgTdAvAYOapHj6e7HnfK/Tp92aWydktOI SG1w== X-Forwarded-Encrypted: i=1; AL+hErGfjqiFITIeQPKMONoUnFPZfRQYjU6pR4MfqlH/Ivmdo3rkyaBNWUrp5yjRgyz/5729Wfddu8cN4hEkag==@vger.kernel.org X-Gm-Message-State: APuU4Z/poZyT0oVeHk/zSEk3JUAVlMs2XTutt9JT0mTXNzWCjkvhh4VW PHExzIITwcNtPAL+C5QcHIdUqoenVuWV9JliCzfM20WiCeTt6jdZ5SVbccTt5iwaUEUClsO5HuJ OqgwsbVAdrg== 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: linux-hwmon@vger.kernel.org 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