* [PATCH v4 2/3] powercap: qcom: Add SPEL powercap driver
2026-12-06 11:56 [PATCH v4 0/3] Add Qualcomm SPEL powercap driver Manaf Meethalavalappu Pallikunhi
2026-12-06 11:56 ` [PATCH v4 1/3] dt-bindings: soc: qcom: Describe Qualcomm SPEL hardware Manaf Meethalavalappu Pallikunhi
@ 2026-12-06 11:56 ` Manaf Meethalavalappu Pallikunhi
2026-12-06 12:13 ` sashiko-bot
2026-12-06 11:56 ` [PATCH v4 3/3] arm64: dts: qcom: glymur: Enable " Manaf Meethalavalappu Pallikunhi
2 siblings, 1 reply; 6+ messages in thread
From: Manaf Meethalavalappu Pallikunhi @ 2026-12-06 11:56 UTC (permalink / raw)
To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, Rafael J. Wysocki,
Daniel Lezcano
Cc: linux-arm-msm, devicetree, linux-kernel, linux-pm,
Manaf Meethalavalappu Pallikunhi
The Qualcomm SoC Power and Electrical Limits (SPEL) provides hardware
based power monitoring and limiting capabilities for various power
domains including System, SoC, CPU clusters, GPU, and various other
subsystems.
The driver integrates with the Linux powercap framework, exposing SPEL
capabilities through powercap sysfs interfaces.
Signed-off-by: Manaf Meethalavalappu Pallikunhi <manaf.pallikunhi@oss.qualcomm.com>
---
MAINTAINERS | 7 +
drivers/powercap/Kconfig | 3 +
drivers/powercap/Makefile | 1 +
drivers/powercap/qcom/Kconfig | 13 +
drivers/powercap/qcom/Makefile | 2 +
drivers/powercap/qcom/spel.c | 949 +++++++++++++++++++++++++++++++++++++++++
6 files changed, 975 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index d256df53948d..c4b700f0fb07 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -23525,6 +23525,12 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux.git
F: Documentation/devicetree/bindings/pinctrl/qcom,*.yaml
F: drivers/pinctrl/qcom/
+QUALCOMM POWERCAP DRIVERS
+M: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
+L: linux-arm-msm@vger.kernel.org
+S: Maintained
+F: drivers/powercap/qcom/
+
QUALCOMM RMNET DRIVER
M: Subash Abhinov Kasiviswanathan <subash.a.kasiviswanathan@oss.qualcomm.com>
M: Sean Tranchetti <sean.tranchetti@oss.qualcomm.com>
@@ -23539,6 +23545,7 @@ M: Manaf Meethalavalappu Pallikunhi <manaf.pallikunhi@oss.qualcomm.com>
L: linux-arm-msm@vger.kernel.org
S: Maintained
F: Documentation/devicetree/bindings/soc/qcom/qcom,glymur-spel.yaml
+F: drivers/powercap/qcom/spel.c
QUALCOMM SPMI CONTROLLER (PMIC ARBITER) DRIVER
M: Abel Vesa <abelvesa@kernel.org>
diff --git a/drivers/powercap/Kconfig b/drivers/powercap/Kconfig
index 03c4c796d993..94b86b64146c 100644
--- a/drivers/powercap/Kconfig
+++ b/drivers/powercap/Kconfig
@@ -93,4 +93,7 @@ config DTPM_DEVFREQ
help
This enables support for device power limitation based on
energy model.
+
+source "drivers/powercap/qcom/Kconfig"
+
endif
diff --git a/drivers/powercap/Makefile b/drivers/powercap/Makefile
index 5ab0dce565b9..48460c568427 100644
--- a/drivers/powercap/Makefile
+++ b/drivers/powercap/Makefile
@@ -8,3 +8,4 @@ obj-$(CONFIG_INTEL_RAPL) += intel_rapl_msr.o
obj-$(CONFIG_INTEL_RAPL_TPMI) += intel_rapl_tpmi.o
obj-$(CONFIG_IDLE_INJECT) += idle_inject.o
obj-$(CONFIG_ARM_SCMI_POWERCAP) += arm_scmi_powercap.o
+obj-$(CONFIG_QCOM_SPEL) += qcom/
diff --git a/drivers/powercap/qcom/Kconfig b/drivers/powercap/qcom/Kconfig
new file mode 100644
index 000000000000..a870ac606b4d
--- /dev/null
+++ b/drivers/powercap/qcom/Kconfig
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config QCOM_SPEL
+ tristate "Qualcomm SPEL Powercap driver"
+ depends on ARCH_QCOM || COMPILE_TEST
+ help
+ This enables support for the Qualcomm SoC Power and Electrical
+ Limits (SPEL) hardware, which allows power limits to be
+ enforced and monitored on Qualcomm SoCs.
+
+ SPEL provides energy monitoring and power capping for multiple
+ domains including system, SoC, CPU clusters, GPU, and various
+ other subsystems.
diff --git a/drivers/powercap/qcom/Makefile b/drivers/powercap/qcom/Makefile
new file mode 100644
index 000000000000..496dcf6ae051
--- /dev/null
+++ b/drivers/powercap/qcom/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_QCOM_SPEL) += spel.o
diff --git a/drivers/powercap/qcom/spel.c b/drivers/powercap/qcom/spel.c
new file mode 100644
index 000000000000..56cf98bc5466
--- /dev/null
+++ b/drivers/powercap/qcom/spel.c
@@ -0,0 +1,949 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Qualcomm SPEL (SoC Power and Electrical Limits) Driver
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/device.h>
+#include <linux/device-id/of.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/overflow.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/powercap.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <linux/units.h>
+
+/* SPEL register bitmasks */
+#define ENERGY_STATUS_MASK GENMASK(31, 0)
+
+#define POWER_LIMIT_MASK GENMASK(14, 0)
+#define POWER_LIMIT_ENABLE BIT(31)
+
+#define TIME_WINDOW_MASK_L GENMASK(14, 0)
+#define TIME_WINDOW_MASK_H GENMASK(22, 16)
+#define TIME_WINDOW_MAX ((FIELD_MAX(TIME_WINDOW_MASK_H) << 15) | \
+ FIELD_MAX(TIME_WINDOW_MASK_L))
+
+#define ENERGY_UNIT_MASK GENMASK(19, 16)
+#define TIME_UNIT_MASK GENMASK(11, 8)
+#define POWER_UNIT_MASK GENMASK(2, 0)
+
+#define LIMITS_CAPABILITY_OFFSET 0x20
+#define ENERGY_RPT_UNIT_OFFSET 0x04
+
+/* Domain types */
+enum spel_domain_type {
+ SPEL_DOMAIN_SYS,
+ SPEL_DOMAIN_SOC,
+ SPEL_DOMAIN_CL0,
+ SPEL_DOMAIN_CL1,
+ SPEL_DOMAIN_CL2,
+ SPEL_DOMAIN_IGPU,
+ SPEL_DOMAIN_DGPU,
+ SPEL_DOMAIN_NSP,
+ SPEL_DOMAIN_MMCX,
+ SPEL_DOMAIN_INFRA,
+ SPEL_DOMAIN_DRAM,
+ SPEL_DOMAIN_MDM,
+ SPEL_DOMAIN_WLAN,
+ SPEL_DOMAIN_MAX,
+};
+
+/* Power limit IDs */
+enum spel_power_limit_id {
+ POWER_LIMIT1,
+ POWER_LIMIT2,
+ POWER_LIMIT3,
+ POWER_LIMIT4,
+ POWER_LIMITS_MAX,
+};
+
+/* Unit types for conversion */
+enum unit_type {
+ POWER_UNIT,
+ ENERGY_UNIT,
+ TIME_UNIT,
+};
+
+/* Power limit operation types */
+enum pl_ops_type {
+ PL_LIMIT,
+ PL_TIME_WINDOW,
+};
+
+/**
+ * struct spel_domain_data - Domain-specific data for hierarchy nodes
+ * @reg_offset: Register offset for energy counter
+ * @id: Domain type ID
+ */
+struct spel_domain_data {
+ u32 reg_offset;
+ enum spel_domain_type id;
+};
+
+/**
+ * struct spel_constraint_info - Power limit constraint information
+ * @limit_offset: Register offset for power limit value
+ * @time_window_offset: Register offset for time window
+ * @supported_mask: Bit mask in capability register
+ * @domain_id: Domain this constraint applies to
+ * @pl_id: Power limit ID (PL1, PL2, etc.)
+ */
+struct spel_constraint_info {
+ u32 limit_offset;
+ u32 time_window_offset;
+ u32 supported_mask;
+ enum spel_domain_type domain_id;
+ int pl_id;
+};
+
+/**
+ * struct spel_domain - SPEL power domain
+ * @power_zone: Powercap zone
+ * @sp: Pointer to parent spel_system
+ * @lock: Mutex protecting register access
+ * @status_reg: Energy counter register
+ * @id: Domain type ID
+ * @limits: Bitmask of power limit IDs supported by this domain
+ * @cached_power_limit: Per-PL limit and enable fields, snapshotted by
+ * spel_domain_pm_freeze() across a hibernation cycle
+ * and replayed by spel_domain_pm_restore().
+ * @cached_time_window: Per-PL time window fields, snapshotted and replayed
+ * the same way as @cached_power_limit.
+ * @cumulative_energy_uj: Energy consumed up to the start of the hardware
+ * counter's current epoch, i.e. before the live
+ * readl(status_reg) value is added on top. Updated by
+ * spel_domain_pm_restore() so that the counter reported
+ * to userspace keeps increasing across a hibernation
+ * cycle instead of dropping back to 0.
+ * @energy_snapshot_uj: Pre-freeze energy reading staged by
+ * spel_domain_pm_freeze(); folded into
+ * @cumulative_energy_uj only by spel_domain_pm_restore(),
+ * so an aborted hibernation (thaw instead of restore)
+ * never double-counts it.
+ * @last_raw: Last raw @status_reg value observed by
+ * spel_get_energy_counter(), used to detect when the
+ * 32-bit hardware counter has wrapped since the
+ * previous read. Reset to 0 by spel_domain_pm_restore()
+ * since the register itself restarts from 0 on power-on.
+ */
+struct spel_domain {
+ struct powercap_zone power_zone;
+ struct spel_system *sp;
+ struct mutex lock; /* Protects register read/write operations */
+ void __iomem *status_reg;
+ enum spel_domain_type id;
+ int limits;
+ u32 cached_power_limit[POWER_LIMITS_MAX];
+ u32 cached_time_window[POWER_LIMITS_MAX];
+ u64 cumulative_energy_uj;
+ u64 energy_snapshot_uj;
+ u32 last_raw;
+};
+
+/**
+ * struct spel_system - SPEL system
+ * @control_type: Embedded powercap control type
+ * @hierarchy: Powercap hierarchy
+ * @node_base: Base address for node registers
+ * @constraint_base: Base address for constraint registers
+ * @config_base: Base address for config registers
+ * @dev: Device pointer for logging
+ * @pl_capabilities: Power limit capability bits, read once at init
+ * @power_unit_shift: Power unit exponent (base unit: one watt)
+ * @energy_unit_shift: Energy unit exponent (base unit: one joule)
+ * @time_unit_shift: Time unit exponent (base unit: one millisecond)
+ */
+struct spel_system {
+ struct powercap_control_type control_type;
+ struct powercap_hierarchy *hierarchy;
+ void __iomem *node_base;
+ void __iomem *constraint_base;
+ void __iomem *config_base;
+ struct device *dev;
+ u32 pl_capabilities;
+ unsigned int power_unit_shift;
+ unsigned int energy_unit_shift;
+ unsigned int time_unit_shift;
+};
+
+#define power_zone_to_spel_domain(_zone) \
+ container_of(_zone, struct spel_domain, power_zone)
+
+#define spel_domain_to_system(sd) ((sd)->sp)
+
+#define control_type_to_spel_system(_pct) \
+ container_of((_pct), struct spel_system, control_type)
+
+/* Power limit names */
+static const char * const pl_names[] = {
+ [POWER_LIMIT1] = "pl1",
+ [POWER_LIMIT2] = "pl2",
+ [POWER_LIMIT3] = "pl3",
+ [POWER_LIMIT4] = "pl4",
+};
+
+/* Constraint configuration */
+static const struct spel_constraint_info constraints[] = {
+ /* SYS domain constraints */
+ { 0x10, 0x70, BIT(0), SPEL_DOMAIN_SYS, POWER_LIMIT1 },
+ { 0x14, 0x74, BIT(1), SPEL_DOMAIN_SYS, POWER_LIMIT2 },
+ { 0x18, 0x78, BIT(2), SPEL_DOMAIN_SYS, POWER_LIMIT3 },
+ { 0x1c, 0x7c, BIT(3), SPEL_DOMAIN_SYS, POWER_LIMIT4 },
+ /* SoC domain constraints */
+ { 0x00, 0x60, BIT(4), SPEL_DOMAIN_SOC, POWER_LIMIT1 },
+ { 0x04, 0x64, BIT(5), SPEL_DOMAIN_SOC, POWER_LIMIT2 },
+ { 0x08, 0x68, BIT(6), SPEL_DOMAIN_SOC, POWER_LIMIT3 },
+ { 0x0c, 0x6c, BIT(7), SPEL_DOMAIN_SOC, POWER_LIMIT4 },
+};
+
+/* Glymur SPEL hierarchy node definitions */
+static struct powercap_node glymur_spel_hierarchy_nodes[] = {
+ [0] = {
+ .name = "sys",
+ .parent = NULL,
+ .data = &(struct spel_domain_data) { .reg_offset = 0x40, .id = SPEL_DOMAIN_SYS },
+ },
+ [1] = {
+ .name = "soc",
+ .parent = &glymur_spel_hierarchy_nodes[0],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x00, .id = SPEL_DOMAIN_SOC },
+ },
+ [2] = {
+ .name = "cl0",
+ .parent = &glymur_spel_hierarchy_nodes[1],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x5c, .id = SPEL_DOMAIN_CL0 },
+ },
+ [3] = {
+ .name = "cl1",
+ .parent = &glymur_spel_hierarchy_nodes[1],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x60, .id = SPEL_DOMAIN_CL1 },
+ },
+ [4] = {
+ .name = "cl2",
+ .parent = &glymur_spel_hierarchy_nodes[1],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x64, .id = SPEL_DOMAIN_CL2 },
+ },
+ [5] = {
+ .name = "igpu",
+ .parent = &glymur_spel_hierarchy_nodes[1],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x08, .id = SPEL_DOMAIN_IGPU },
+ },
+ [6] = {
+ .name = "nsp",
+ .parent = &glymur_spel_hierarchy_nodes[1],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x0c, .id = SPEL_DOMAIN_NSP },
+ },
+ [7] = {
+ .name = "infra",
+ .parent = &glymur_spel_hierarchy_nodes[1],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x18, .id = SPEL_DOMAIN_INFRA },
+ },
+ [8] = {
+ .name = "mmcx",
+ .parent = &glymur_spel_hierarchy_nodes[7],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x10, .id = SPEL_DOMAIN_MMCX },
+ },
+ [9] = {
+ .name = "dram",
+ .parent = &glymur_spel_hierarchy_nodes[7],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x1c, .id = SPEL_DOMAIN_DRAM },
+ },
+ [10] = {
+ .name = "wlan",
+ .parent = &glymur_spel_hierarchy_nodes[0],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x4c, .id = SPEL_DOMAIN_WLAN },
+ },
+ [11] = {
+ .name = "mdm",
+ .parent = &glymur_spel_hierarchy_nodes[0],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x48, .id = SPEL_DOMAIN_MDM },
+ },
+ [12] = {
+ .name = "dgpu",
+ .parent = &glymur_spel_hierarchy_nodes[0],
+ .data = &(struct spel_domain_data) { .reg_offset = 0x44, .id = SPEL_DOMAIN_DGPU },
+ },
+};
+
+static struct powercap_hierarchy glymur_spel_hierarchy_template = {
+ .nodes = glymur_spel_hierarchy_nodes,
+ .nr_nodes = ARRAY_SIZE(glymur_spel_hierarchy_nodes),
+};
+
+static bool is_pl_valid(struct spel_domain *sd, int pl)
+{
+ return !!(sd->limits & BIT(pl));
+}
+
+static int get_pl_ops_offset(struct spel_domain *sd, int pl, enum pl_ops_type pl_op)
+{
+ for (int i = 0; i < ARRAY_SIZE(constraints); i++) {
+ const struct spel_constraint_info *ci = &constraints[i];
+
+ if (ci->domain_id == sd->id && ci->pl_id == pl) {
+ switch (pl_op) {
+ case PL_LIMIT:
+ return ci->limit_offset;
+ case PL_TIME_WINDOW:
+ return ci->time_window_offset;
+ default:
+ return -EOPNOTSUPP;
+ }
+ }
+ }
+
+ return -EOPNOTSUPP;
+}
+
+static u64 spel_unit_xlate(struct spel_domain *sd, enum unit_type type,
+ u64 value, int to_raw)
+{
+ struct spel_system *sp = spel_domain_to_system(sd);
+ u32 shift, scale;
+
+ switch (type) {
+ case POWER_UNIT:
+ shift = sp->power_unit_shift;
+ scale = MEGA;
+ break;
+ case ENERGY_UNIT:
+ shift = sp->energy_unit_shift;
+ scale = MEGA;
+ break;
+ case TIME_UNIT:
+ shift = sp->time_unit_shift;
+ scale = MILLI;
+ break;
+ default:
+ return value;
+ }
+
+ if (to_raw) {
+ u64 numerator;
+
+ /*
+ * value is an unclamped u64 from sysfs. Reject inputs that would
+ * overflow the multiply, or the (numerator + scale / 2) rounding
+ * add inside DIV_ROUND_CLOSEST_ULL(), instead of silently wrapping.
+ */
+ if (check_mul_overflow(value, BIT_ULL(shift), &numerator) ||
+ numerator > U64_MAX - scale / 2)
+ return U64_MAX;
+
+ return DIV_ROUND_CLOSEST_ULL(numerator, scale);
+ }
+
+ /* Hardware values are at most 32 bits, so the product fits in u64. */
+ return (value * scale) >> shift;
+}
+
+static int spel_read_pl_data(struct spel_domain *sd, int pl,
+ enum pl_ops_type pl_op, bool xlate, u64 *data)
+{
+ struct spel_system *sp = spel_domain_to_system(sd);
+ void __iomem *reg_addr;
+ u64 value;
+ int offset;
+
+ if (!is_pl_valid(sd, pl))
+ return -EINVAL;
+
+ offset = get_pl_ops_offset(sd, pl, pl_op);
+ if (offset < 0)
+ return offset;
+
+ guard(mutex)(&sd->lock);
+
+ reg_addr = sp->constraint_base + offset;
+ value = readl(reg_addr);
+
+ switch (pl_op) {
+ case PL_LIMIT:
+ value = FIELD_GET(POWER_LIMIT_MASK, value);
+ if (xlate)
+ *data = spel_unit_xlate(sd, POWER_UNIT, value, 0);
+ else
+ *data = value;
+ break;
+ case PL_TIME_WINDOW:
+ /* Decode time window: bits [22:16] are upper 7 bits, [14:0] are lower 15 bits */
+ value = (FIELD_GET(TIME_WINDOW_MASK_H, value) << 15) |
+ FIELD_GET(TIME_WINDOW_MASK_L, value);
+ if (xlate)
+ *data = spel_unit_xlate(sd, TIME_UNIT, value, 0);
+ else
+ *data = value;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int spel_write_pl_data(struct spel_domain *sd, int pl,
+ enum pl_ops_type pl_op, unsigned long long value)
+{
+ struct spel_system *sp = spel_domain_to_system(sd);
+ void __iomem *reg_addr;
+ u64 reg_val, new_val;
+ int offset;
+
+ if (!is_pl_valid(sd, pl))
+ return -EINVAL;
+
+ offset = get_pl_ops_offset(sd, pl, pl_op);
+ if (offset < 0)
+ return offset;
+
+ guard(mutex)(&sd->lock);
+
+ reg_addr = sp->constraint_base + offset;
+ reg_val = readl(reg_addr);
+
+ switch (pl_op) {
+ case PL_LIMIT:
+ new_val = spel_unit_xlate(sd, POWER_UNIT, value, 1);
+ if (new_val > FIELD_MAX(POWER_LIMIT_MASK))
+ return -EINVAL;
+
+ FIELD_MODIFY(POWER_LIMIT_MASK, ®_val, new_val);
+
+ /*
+ * Enable/Disable PL based on the value:
+ * - If value is 0, disable the PL (clear enable bit)
+ * - If value is non-zero, enable the PL (set enable bit)
+ */
+ FIELD_MODIFY(POWER_LIMIT_ENABLE, ®_val, !!new_val);
+
+ writel(reg_val, reg_addr);
+ return 0;
+
+ case PL_TIME_WINDOW:
+ /*
+ * Encode time window: upper 7 bits to [22:16], lower 15 bits to [14:0]
+ */
+ new_val = spel_unit_xlate(sd, TIME_UNIT, value, 1);
+ if (new_val > TIME_WINDOW_MAX)
+ return -EINVAL;
+
+ /* Read-modify-write to preserve other bits */
+ FIELD_MODIFY(TIME_WINDOW_MASK_H, ®_val, new_val >> 15);
+ FIELD_MODIFY(TIME_WINDOW_MASK_L, ®_val, new_val & FIELD_MAX(TIME_WINDOW_MASK_L));
+ writel(reg_val, reg_addr);
+
+ /*
+ * Time window register update doesn't trigger firmware interrupt.
+ * Write to the PL register with current value to trigger the interrupt.
+ */
+ offset = get_pl_ops_offset(sd, pl, PL_LIMIT);
+ if (offset >= 0) {
+ reg_addr = sp->constraint_base + offset;
+ reg_val = readl(reg_addr);
+ writel(reg_val, reg_addr);
+ }
+ return 0;
+
+ default:
+ return -EINVAL;
+ }
+}
+
+/*
+ * Must be called with sd->lock held. Folds a full hardware range into
+ * @cumulative_energy_uj if the counter has wrapped since @last_raw was last
+ * updated, then refreshes @last_raw to the current register value.
+ */
+static void spel_domain_sync_energy(struct spel_domain *sd)
+{
+ u32 value = readl(sd->status_reg);
+
+ /* The 32-bit counter wrapped since the last read; fold a full range in. */
+ if (value < sd->last_raw)
+ sd->cumulative_energy_uj +=
+ spel_unit_xlate(sd, ENERGY_UNIT, ENERGY_STATUS_MASK + 1ULL, 0);
+
+ sd->last_raw = value;
+}
+
+static int spel_get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
+{
+ struct spel_domain *sd = power_zone_to_spel_domain(power_zone);
+
+ guard(mutex)(&sd->lock);
+
+ spel_domain_sync_energy(sd);
+
+ *energy_raw = sd->cumulative_energy_uj +
+ spel_unit_xlate(sd, ENERGY_UNIT, sd->last_raw, 0);
+
+ return 0;
+}
+
+static int spel_get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
+{
+ /*
+ * energy_raw is a software accumulator that never wraps at the
+ * hardware's 32-bit range; report the true u64 range instead of the
+ * hardware wrap boundary.
+ */
+ *energy = U64_MAX;
+
+ return 0;
+}
+
+static int spel_release_zone(struct powercap_zone *power_zone)
+{
+ struct spel_domain *sd = power_zone_to_spel_domain(power_zone);
+
+ /* Destroy mutex and free domain */
+ mutex_destroy(&sd->lock);
+ kfree(sd);
+
+ return 0;
+}
+
+static void spel_domain_pm_freeze(struct spel_domain *sd)
+{
+ struct spel_system *sp = spel_domain_to_system(sd);
+
+ guard(mutex)(&sd->lock);
+
+ for (int i = 0; i < ARRAY_SIZE(constraints); i++) {
+ const struct spel_constraint_info *ci = &constraints[i];
+
+ if (ci->domain_id != sd->id || !is_pl_valid(sd, ci->pl_id))
+ continue;
+
+ sd->cached_power_limit[ci->pl_id] =
+ readl(sp->constraint_base + ci->limit_offset) &
+ (POWER_LIMIT_MASK | POWER_LIMIT_ENABLE);
+ sd->cached_time_window[ci->pl_id] =
+ readl(sp->constraint_base + ci->time_window_offset) &
+ (TIME_WINDOW_MASK_H | TIME_WINDOW_MASK_L);
+ }
+
+ spel_domain_sync_energy(sd);
+ sd->energy_snapshot_uj = spel_unit_xlate(sd, ENERGY_UNIT, sd->last_raw, 0);
+}
+
+static void spel_domain_pm_restore(struct spel_domain *sd)
+{
+ struct spel_system *sp = spel_domain_to_system(sd);
+
+ guard(mutex)(&sd->lock);
+
+ for (int i = 0; i < ARRAY_SIZE(constraints); i++) {
+ const struct spel_constraint_info *ci = &constraints[i];
+ void __iomem *reg;
+ u32 value;
+
+ if (ci->domain_id != sd->id || !is_pl_valid(sd, ci->pl_id))
+ continue;
+
+ reg = sp->constraint_base + ci->time_window_offset;
+ value = readl(reg) & ~(TIME_WINDOW_MASK_H | TIME_WINDOW_MASK_L);
+ writel(value | sd->cached_time_window[ci->pl_id], reg);
+
+ /* Write the limit last to notify firmware of both updates. */
+ reg = sp->constraint_base + ci->limit_offset;
+ value = readl(reg) & ~(POWER_LIMIT_MASK | POWER_LIMIT_ENABLE);
+ writel(value | sd->cached_power_limit[ci->pl_id], reg);
+ }
+
+ sd->cumulative_energy_uj += sd->energy_snapshot_uj;
+ sd->last_raw = 0;
+}
+
+static int spel_find_nr_power_limit(struct spel_domain *sd)
+{
+ int nr_pl = 0;
+
+ for (int i = 0; i < ARRAY_SIZE(pl_names); i++) {
+ if (is_pl_valid(sd, i))
+ nr_pl++;
+ }
+
+ return nr_pl;
+}
+
+static const struct powercap_zone_ops zone_ops = {
+ .get_energy_uj = spel_get_energy_counter,
+ .get_max_energy_range_uj = spel_get_max_energy_counter,
+ .release = spel_release_zone,
+};
+
+static int spel_constraint_to_pl(struct spel_domain *sd, int cid)
+{
+ int id = 0;
+
+ for (int i = 0; i < ARRAY_SIZE(pl_names); i++) {
+ if (is_pl_valid(sd, i) && id++ == cid)
+ return i;
+ }
+
+ return -EINVAL;
+}
+
+static int spel_set_power_limit(struct powercap_zone *power_zone, int cid,
+ u64 power_limit)
+{
+ struct spel_domain *sd = power_zone_to_spel_domain(power_zone);
+ int id;
+
+ id = spel_constraint_to_pl(sd, cid);
+ if (id < 0)
+ return id;
+
+ return spel_write_pl_data(sd, id, PL_LIMIT, power_limit);
+}
+
+static int spel_get_power_limit(struct powercap_zone *power_zone, int cid,
+ u64 *data)
+{
+ struct spel_domain *sd = power_zone_to_spel_domain(power_zone);
+ u64 val;
+ int ret, id;
+
+ id = spel_constraint_to_pl(sd, cid);
+ if (id < 0)
+ return id;
+
+ ret = spel_read_pl_data(sd, id, PL_LIMIT, true, &val);
+ if (!ret)
+ *data = val;
+
+ return ret;
+}
+
+static int spel_set_time_window(struct powercap_zone *power_zone, int cid,
+ u64 window)
+{
+ struct spel_domain *sd = power_zone_to_spel_domain(power_zone);
+ int id;
+
+ id = spel_constraint_to_pl(sd, cid);
+ if (id < 0)
+ return id;
+
+ return spel_write_pl_data(sd, id, PL_TIME_WINDOW, window);
+}
+
+static int spel_get_time_window(struct powercap_zone *power_zone, int cid,
+ u64 *data)
+{
+ struct spel_domain *sd = power_zone_to_spel_domain(power_zone);
+ u64 val;
+ int ret, id;
+
+ id = spel_constraint_to_pl(sd, cid);
+ if (id < 0)
+ return id;
+
+ ret = spel_read_pl_data(sd, id, PL_TIME_WINDOW, true, &val);
+ if (!ret)
+ *data = val;
+
+ return ret;
+}
+
+static const char *spel_get_constraint_name(struct powercap_zone *power_zone,
+ int cid)
+{
+ struct spel_domain *sd = power_zone_to_spel_domain(power_zone);
+ int id;
+
+ id = spel_constraint_to_pl(sd, cid);
+ if (id >= 0 && id < ARRAY_SIZE(pl_names))
+ return pl_names[id];
+
+ return NULL;
+}
+
+static const struct powercap_zone_constraint_ops constraint_ops = {
+ .set_power_limit_uw = spel_set_power_limit,
+ .get_power_limit_uw = spel_get_power_limit,
+ .set_time_window_us = spel_set_time_window,
+ .get_time_window_us = spel_get_time_window,
+ .get_name = spel_get_constraint_name,
+};
+
+static int spel_update_unit(struct spel_system *sp)
+{
+ u32 value;
+
+ /* Read power_unit and time_unit from offset 0x0 */
+ value = readl(sp->config_base);
+
+ /* Keep exponents to avoid truncating fractional units. */
+ sp->power_unit_shift = FIELD_GET(POWER_UNIT_MASK, value);
+ sp->time_unit_shift = FIELD_GET(TIME_UNIT_MASK, value);
+
+ /* Read energy_unit from ENERGY_RPT_UNIT_OFFSET */
+ value = readl(sp->config_base + ENERGY_RPT_UNIT_OFFSET);
+
+ sp->energy_unit_shift = FIELD_GET(ENERGY_UNIT_MASK, value);
+
+ dev_dbg(sp->dev, "Unit shifts: energy=%u, time=%u, power=%u\n",
+ sp->energy_unit_shift, sp->time_unit_shift, sp->power_unit_shift);
+
+ return 0;
+}
+
+static int spel_get_domain_limits(struct spel_system *sp, enum spel_domain_type domain_id)
+{
+ /* Enable minimum one constraint, detect the rest from hardware capabilities. */
+ int limits = BIT(POWER_LIMIT1);
+
+ for (int i = 0; i < ARRAY_SIZE(constraints); i++) {
+ const struct spel_constraint_info *ci = &constraints[i];
+
+ if (ci->domain_id == domain_id && sp->pl_capabilities & ci->supported_mask)
+ limits |= BIT(ci->pl_id);
+ }
+
+ return limits;
+}
+
+static int spel_control_type_release(struct powercap_control_type *pct)
+{
+ struct spel_system *sp = control_type_to_spel_system(pct);
+
+ /* Free system structure */
+ kfree(sp);
+
+ return 0;
+}
+
+static const struct powercap_control_type_ops spel_control_type_ops = {
+ .release = spel_control_type_release,
+};
+
+static int spel_init_system(struct spel_system *sp, struct device *dev)
+{
+ int ret;
+
+ /* Read unit configuration (common for all domains) */
+ ret = spel_update_unit(sp);
+ if (ret)
+ return ret;
+
+ /* Cache power limit capability bits; per-domain limits are derived in spel_create_zone() */
+ sp->pl_capabilities = readl(sp->config_base + LIMITS_CAPABILITY_OFFSET);
+
+ return 0;
+}
+
+static struct powercap_zone *spel_create_zone(struct powercap_control_type *pct,
+ const char *name, void *data,
+ struct powercap_zone *parent)
+{
+ struct spel_system *sp = control_type_to_spel_system(pct);
+ struct spel_domain_data *dd = data;
+ struct spel_domain *sd;
+ struct powercap_zone *pcz;
+ int nr_pl;
+
+ /* Allocate domain */
+ sd = kzalloc_obj(*sd, GFP_KERNEL);
+ if (!sd)
+ return ERR_PTR(-ENOMEM);
+
+ /* Initialize domain */
+ sd->sp = sp;
+ sd->id = dd->id;
+ sd->status_reg = sp->node_base + dd->reg_offset;
+ sd->limits = spel_get_domain_limits(sp, dd->id);
+ mutex_init(&sd->lock);
+
+ /* Get power limits for this domain */
+ nr_pl = spel_find_nr_power_limit(sd);
+
+ /* Register powercap zone */
+ pcz = powercap_register_zone(&sd->power_zone, pct, name, parent,
+ &zone_ops, nr_pl, &constraint_ops);
+ if (IS_ERR(pcz)) {
+ dev_err_probe(sp->dev, PTR_ERR(pcz),
+ "Failed to register power zone %s\n", name);
+ mutex_destroy(&sd->lock);
+ kfree(sd);
+ return pcz;
+ }
+
+ return pcz;
+}
+
+static void spel_destroy_zone(struct powercap_control_type *pct,
+ struct powercap_zone *zone, void *data)
+{
+ powercap_unregister_zone(pct, zone);
+}
+
+static int spel_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct powercap_control_type *ct;
+ struct spel_system *sp;
+ int ret;
+
+ /* Allocate system structure */
+ sp = kzalloc_obj(*sp, GFP_KERNEL);
+ if (!sp)
+ return -ENOMEM;
+
+ sp->dev = dev;
+
+ /* Map config registers (units, capabilities) */
+ sp->config_base = devm_platform_ioremap_resource_byname(pdev, "config");
+ if (IS_ERR(sp->config_base)) {
+ ret = dev_err_probe(dev, PTR_ERR(sp->config_base),
+ "Failed to map config registers\n");
+ goto err_free_sp;
+ }
+
+ /* Map constraint registers (power limits) */
+ sp->constraint_base = devm_platform_ioremap_resource_byname(pdev, "constraints");
+ if (IS_ERR(sp->constraint_base)) {
+ ret = dev_err_probe(dev, PTR_ERR(sp->constraint_base),
+ "Failed to map constraint registers\n");
+ goto err_free_sp;
+ }
+
+ /* Map spel domain registers (energy counters) */
+ sp->node_base = devm_platform_ioremap_resource_byname(pdev, "nodes");
+ if (IS_ERR(sp->node_base)) {
+ ret = dev_err_probe(dev, PTR_ERR(sp->node_base),
+ "Failed to map node registers\n");
+ goto err_free_sp;
+ }
+
+ ct = powercap_register_control_type(&sp->control_type, "qcom-spel",
+ &spel_control_type_ops);
+ if (IS_ERR(ct))
+ return dev_err_probe(dev, PTR_ERR(ct),
+ "Failed to register control type\n");
+
+ /* Initialize system and domains */
+ ret = spel_init_system(sp, dev);
+ if (ret) {
+ ret = dev_err_probe(dev, ret, "Failed to initialize system\n");
+ goto err_unregister_control;
+ }
+
+ /* Duplicate the hierarchy template */
+ sp->hierarchy = powercap_hierarchy_dup(&glymur_spel_hierarchy_template);
+ if (IS_ERR(sp->hierarchy)) {
+ ret = dev_err_probe(dev, PTR_ERR(sp->hierarchy),
+ "Failed to duplicate hierarchy\n");
+ goto err_unregister_control;
+ }
+
+ /* Create the powercap hierarchy */
+ ret = powercap_hierarchy_create(ct, sp->hierarchy, spel_create_zone,
+ spel_destroy_zone);
+ if (ret) {
+ ret = dev_err_probe(dev, ret, "Failed to create powercap hierarchy\n");
+ goto err_free_hierarchy;
+ }
+
+ platform_set_drvdata(pdev, sp);
+
+ return 0;
+
+err_free_hierarchy:
+ powercap_hierarchy_free(sp->hierarchy);
+err_unregister_control:
+ /*
+ * Unregister control type - this triggers the release callback
+ * which will free the spel_system structure.
+ * Return directly to avoid double free.
+ */
+ powercap_unregister_control_type(ct);
+ return ret;
+err_free_sp:
+ kfree(sp);
+ return ret;
+}
+
+static void spel_remove(struct platform_device *pdev)
+{
+ struct spel_system *sp = platform_get_drvdata(pdev);
+
+ /* Destroy the hierarchy (unregisters all zones) */
+ powercap_hierarchy_destroy(&sp->control_type, sp->hierarchy, spel_destroy_zone);
+
+ /* Free the hierarchy */
+ powercap_hierarchy_free(sp->hierarchy);
+
+ /*
+ * Unregister control type - this will trigger the release callback
+ * which will cleanup domains and free the spel_system structure
+ */
+ powercap_unregister_control_type(&sp->control_type);
+}
+
+static void spel_system_pm_freeze(struct spel_system *sp)
+{
+ for (size_t i = 0; i < sp->hierarchy->nr_nodes; i++) {
+ struct powercap_zone *zone = sp->hierarchy->nodes[i].zone;
+
+ if (zone)
+ spel_domain_pm_freeze(power_zone_to_spel_domain(zone));
+ }
+}
+
+static void spel_system_pm_restore(struct spel_system *sp)
+{
+ for (size_t i = 0; i < sp->hierarchy->nr_nodes; i++) {
+ struct powercap_zone *zone = sp->hierarchy->nodes[i].zone;
+
+ if (zone)
+ spel_domain_pm_restore(power_zone_to_spel_domain(zone));
+ }
+}
+
+static int spel_pm_freeze(struct device *dev)
+{
+ spel_system_pm_freeze(dev_get_drvdata(dev));
+ return 0;
+}
+
+static int spel_pm_restore(struct device *dev)
+{
+ spel_system_pm_restore(dev_get_drvdata(dev));
+ return 0;
+}
+
+static const struct dev_pm_ops spel_pm_ops = {
+ .freeze = spel_pm_freeze,
+ .restore = spel_pm_restore,
+};
+
+static const struct of_device_id spel_of_match[] = {
+ { .compatible = "qcom,glymur-spel" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, spel_of_match);
+
+static struct platform_driver spel_driver = {
+ .probe = spel_probe,
+ .remove = spel_remove,
+ .driver = {
+ .name = "qcom_spel",
+ .of_match_table = spel_of_match,
+ .pm = pm_sleep_ptr(&spel_pm_ops),
+ },
+};
+
+module_platform_driver(spel_driver);
+
+MODULE_DESCRIPTION("Qualcomm SPEL Powercap Driver");
+MODULE_LICENSE("GPL");
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread