* [PATCH 2/7] pmdomain: qcom: add CPRv4 power domain driver
2026-12-01 21:34 [PATCH 0/7] Add APSS CPRv4 support for IPQ8074 Robert Marko
2026-12-01 21:34 ` [PATCH 1/7] dt-bindings: power: document Qualcomm CPRv4 Robert Marko
@ 2026-12-01 21:34 ` Robert Marko
2026-12-01 21:34 ` [PATCH 3/7] pmdomain: qcom: cpr4: add IPQ8074 APSS support Robert Marko
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Robert Marko @ 2026-12-01 21:34 UTC (permalink / raw)
To: robh, krzk+dt, conor+dt, andersson, konradybcio, abelvesa,
ilia.lin, rafael, viresh.kumar, ulfh, lgirdwood, broonie,
angelogioacchino.delregno, devicetree, linux-arm-msm,
linux-kernel, linux-pm
Cc: Konrad Dybcio, Robert Marko
From: AngeloGioacchino Del Regno <angelogioacchino.delregno@somainline.org>
Add a power domain driver for Qualcomm CPRv4 controllers. Read
calibration fuses, map OPPs to voltage corners, and handle CPR
interrupts to adjust the supply voltage. Expose the corners as power
domain performance states for CPU frequency scaling.
Keep controller and SoC parameters in descriptors, including callbacks
for SoC-specific OPP selection and array power mux control.
Based on the CPR3+ driver by AngeloGioacchino Del Regno and Konrad
Dybcio.
[Konrad: rebase, a whole lot of cleanup/fixes]
[Robert: retain CPRv4, remove CPRh support, and adapt for IPQ APSS.]
Link: https://lore.kernel.org/r/20240708-topic-cpr3h-v15-9-5bc8b8936489@linaro.org
Signed-off-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@somainline.org>
Signed-off-by: Konrad Dybcio <konrad.dybcio@linaro.org>
Signed-off-by: Robert Marko <robimarko@gmail.com>
---
drivers/pmdomain/qcom/Kconfig | 21 +
drivers/pmdomain/qcom/Makefile | 1 +
drivers/pmdomain/qcom/cpr4.c | 2194 ++++++++++++++++++++++++++++++++
3 files changed, 2216 insertions(+)
create mode 100644 drivers/pmdomain/qcom/cpr4.c
diff --git a/drivers/pmdomain/qcom/Kconfig b/drivers/pmdomain/qcom/Kconfig
index 4abd43a88d08..7a38fca694e6 100644
--- a/drivers/pmdomain/qcom/Kconfig
+++ b/drivers/pmdomain/qcom/Kconfig
@@ -17,6 +17,27 @@ config QCOM_CPR
To compile this driver as a module, choose M here: the module will
be called qcom-cpr
+config QCOM_CPR4
+ tristate "Qualcomm Core Power Reduction v4 (CPRv4) support"
+ depends on ARCH_QCOM || COMPILE_TEST
+ depends on HAS_IOMEM
+ depends on OF
+ depends on PM
+ depends on NVMEM
+ depends on REGULATOR
+ select REGMAP
+ select PM_OPP
+ select PM_GENERIC_DOMAINS
+ help
+ Say Y here to enable CPRv4 voltage control on supported
+ Qualcomm SoCs. The controller uses ring oscillator measurements to
+ adjust the CPU supply voltage within the limits of each operating
+ point. The driver reads calibration fuses and exposes voltage levels
+ as power domain performance states for CPU frequency scaling.
+
+ To compile this driver as a module, choose M here: the module will
+ be called cpr4.
+
config QCOM_RPMHPD
tristate "Qualcomm RPMh Power domain driver"
depends on QCOM_RPMH && QCOM_COMMAND_DB
diff --git a/drivers/pmdomain/qcom/Makefile b/drivers/pmdomain/qcom/Makefile
index 403dfc5af095..58b1aa264030 100644
--- a/drivers/pmdomain/qcom/Makefile
+++ b/drivers/pmdomain/qcom/Makefile
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_QCOM_CPR) += cpr.o
+obj-$(CONFIG_QCOM_CPR4) += cpr4.o
obj-$(CONFIG_QCOM_RPMPD) += rpmpd.o
obj-$(CONFIG_QCOM_RPMHPD) += rpmhpd.o
diff --git a/drivers/pmdomain/qcom/cpr4.c b/drivers/pmdomain/qcom/cpr4.c
new file mode 100644
index 000000000000..27f6bf2ed132
--- /dev/null
+++ b/drivers/pmdomain/qcom/cpr4.c
@@ -0,0 +1,2194 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2013-2020, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2019 Linaro Limited
+ * Copyright (c) 2021, AngeloGioacchino Del Regno
+ * <angelogioacchino.delregno@somainline.org>
+ */
+
+#include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/debugfs.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/nvmem-consumer.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/pm_domain.h>
+#include <linux/pm_opp.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/workqueue.h>
+
+
+enum voltage_change_dir {
+ NO_CHANGE,
+ DOWN,
+ UP,
+};
+
+struct cpr_fuse {
+ char *ring_osc;
+ char *init_voltage;
+ char *quotient;
+ char *quotient_offset;
+};
+
+struct fuse_corner_data {
+ int ref_uV;
+ int max_uV;
+ int min_uV;
+ int range_uV;
+ /* fuse volt: closed/open loop */
+ int volt_cloop_adjust;
+ int volt_oloop_adjust;
+ int max_volt_scale;
+ int max_quot_scale;
+ /* fuse quot */
+ int quot_offset;
+ int quot_scale;
+ int quot_adjust;
+ /* fuse quot_offset */
+ int quot_offset_scale;
+ int quot_offset_adjust;
+};
+
+struct fuse_corner {
+ int min_uV;
+ int max_uV;
+ int uV;
+ int quot;
+ u64 max_freq;
+ u8 ring_osc_idx;
+};
+
+struct corner {
+ int min_uV;
+ int max_uV;
+ int uV;
+ int last_uV;
+ int quot_adjust;
+ u64 freq;
+ struct fuse_corner *fuse_corner;
+};
+
+struct corner_data {
+ unsigned int fuse_corner;
+ u64 freq;
+};
+
+#define CPR4_RO_COUNT 16
+#define CPR4_RO_MASK GENMASK(CPR4_RO_COUNT - 1, 0)
+
+/* CPR register offsets shared with earlier controller revisions */
+#define CPR4_REG_CPR_CTL 0x4
+#define CPR4_CPR_CTL_LOOP_EN_MASK BIT(0)
+#define CPR4_CPR_CTL_IDLE_CLOCKS_MASK GENMASK(5, 1)
+#define CPR4_CPR_CTL_COUNT_MODE_MASK GENMASK(7, 6)
+#define CPR4_CPR_CTL_COUNT_MODE_ALL_AT_ONCE_MIN 0
+#define CPR4_CPR_CTL_COUNT_REPEAT_MASK GENMASK(31, 9)
+
+#define CPR4_REG_CPR_STATUS 0x8
+#define CPR4_CPR_STATUS_BUSY_MASK BIT(0)
+
+#define CPR4_REG_CPR_TIMER_AUTO_CONT 0xC
+
+#define CPR4_REG_CPR_STEP_QUOT 0x14
+#define CPR4_CPR_STEP_QUOT_MIN_MASK GENMASK(5, 0)
+#define CPR4_CPR_STEP_QUOT_MAX_MASK GENMASK(11, 6)
+
+#define CPR4_REG_GCNT(ro) (0xA0 + 0x4 * (ro))
+#define CPR4_REG_SENSOR_OWNER(sensor) (0x200 + 0x4 * (sensor))
+
+#define CPR4_REG_CONT_CMD 0x800
+#define CPR4_CONT_CMD_ACK 0x1
+#define CPR4_CONT_CMD_NACK 0x0
+
+#define CPR4_REG_THRESH(thread) (0x808 + 0x440 * (thread))
+#define CPR4_THRESH_CONS_DOWN_MASK GENMASK(3, 0)
+#define CPR4_THRESH_CONS_UP_MASK GENMASK(7, 4)
+#define CPR4_THRESH_DOWN_THRESH_MASK GENMASK(12, 8)
+#define CPR4_THRESH_UP_THRESH_MASK GENMASK(17, 13)
+
+#define CPR4_REG_RO_MASK(thread) (0x80C + 0x440 * (thread))
+
+#define CPR4_REG_RESULT0(thread) (0x810 + 0x440 * (thread))
+#define CPR4_RESULT0_BUSY_MASK BIT(0)
+#define CPR4_RESULT0_STEP_DN_MASK BIT(1)
+#define CPR4_RESULT0_STEP_UP_MASK BIT(2)
+#define CPR4_RESULT0_ERROR_STEPS_MASK GENMASK(7, 3)
+#define CPR4_RESULT0_ERROR_MASK GENMASK(19, 8)
+#define CPR4_RESULT0_NEG_MASK BIT(20)
+
+#define CPR4_REG_RESULT1(thread) (0x814 + 0x440 * (thread))
+#define CPR4_RESULT1_QUOT_MIN_MASK GENMASK(11, 0)
+#define CPR4_RESULT1_QUOT_MAX_MASK GENMASK(23, 12)
+#define CPR4_RESULT1_RO_MIN_MASK GENMASK(27, 24)
+#define CPR4_RESULT1_RO_MAX_MASK GENMASK(31, 28)
+
+#define CPR4_REG_RESULT2(thread) (0x818 + 0x440 * (thread))
+#define CPR4_RESULT2_STEP_QUOT_MIN_MASK GENMASK(5, 0)
+#define CPR4_RESULT2_STEP_QUOT_MAX_MASK GENMASK(11, 6)
+#define CPR4_RESULT2_SENSOR_MIN_MASK GENMASK(23, 16)
+#define CPR4_RESULT2_SENSOR_MAX_MASK GENMASK(31, 24)
+
+#define CPR4_REG_IRQ_EN 0x81C
+#define CPR4_REG_IRQ_CLEAR 0x820
+#define CPR4_REG_IRQ_STATUS 0x824
+#define CPR4_IRQ_UP BIT(3)
+#define CPR4_IRQ_MID BIT(2)
+#define CPR4_IRQ_DOWN BIT(1)
+#define CPR4_IRQ_ALL (CPR4_IRQ_UP | CPR4_IRQ_MID | CPR4_IRQ_DOWN)
+
+#define CPR4_REG_TARGET_QUOT(thread, ro) (0x840 + 0x440 * (thread) + 0x4 * (ro))
+
+/* CPR4 controller specific registers and bit definitions */
+#define CPR4_REG_CPR_TIMER_CLAMP 0x10
+#define CPR4_CPR_TIMER_CLAMP_THREAD_AGGREGATION_EN BIT(27)
+
+#define CPR4_REG_MISC 0x700
+#define CPR4_MISC_RESET_STEP_QUOT_LOOP_EN BIT(2)
+#define CPR4_MISC_THREAD_HAS_ALWAYS_VOTE_EN BIT(3)
+
+#define CPR4_REG_SAW_ERROR_STEP_LIMIT 0x7A4
+#define CPR4_SAW_ERROR_STEP_LIMIT_UP_MASK GENMASK(4, 0)
+#define CPR4_SAW_ERROR_STEP_LIMIT_UP_SHIFT 0
+#define CPR4_SAW_ERROR_STEP_LIMIT_DN_MASK GENMASK(9, 5)
+#define CPR4_SAW_ERROR_STEP_LIMIT_DN_SHIFT 5
+
+#define CPR4_REG_MARGIN_TEMP_CORE_TIMERS 0x7A8
+#define CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_MASK GENMASK(28, 18)
+#define CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_SHFT 18
+
+#define CPR4_REG_MARGIN_ADJ_CTL 0x7F8
+#define CPR4_MARGIN_ADJ_HW_CLOSED_LOOP_EN BIT(4)
+#define CPR4_MARGIN_ADJ_PER_RO_KV_MARGIN_EN BIT(7)
+#define CPR4_MARGIN_ADJ_PMIC_STEP_SIZE_MASK GENMASK(16, 12)
+#define CPR4_MARGIN_ADJ_PMIC_STEP_SIZE_SHIFT 12
+#define CPR4_MARGIN_ADJ_KV_MARGIN_ADJ_STEP_QUOT_MASK GENMASK(31, 26)
+#define CPR4_MARGIN_ADJ_KV_MARGIN_ADJ_STEP_QUOT_SHIFT 26
+
+#define CPR4_REG_CPR_MASK_THREAD(thread) (0x80C + 0x440 * (thread))
+#define CPR4_CPR_MASK_THREAD_DISABLE_THREAD BIT(31)
+#define CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK GENMASK(15, 0)
+
+/*
+ * struct cpr_thread_desc - CPR Thread-specific parameters
+ *
+ * @controller_id: Identifier of the CPR controller expected by the HW
+ * @hw_tid: Identifier of the CPR thread expected by the HW
+ * @ro_scaling_factor: Scaling factor for each ring oscillator entry
+ * @ro_scaling_factor_common: Whether the ro_scaling_factor value is common for all fuses
+ * @init_voltage_step: Voltage in uV for number of steps read from fuse array
+ * @init_voltage_width: Bit-width of the voltage read from the fuse array
+ * @sensor_range_start: First sensor ID used by a thread
+ * @sensor_range_end: Last sensor ID used by a thread
+ * @step_quot_init_min: Minimum achievable step quotient for this corner
+ * @step_quot_init_max: Maximum achievable step quotient for this corner
+ * @num_fuse_corners: Number of valid entries in fuse_corner_data
+ * @fuse_corner_data: Parameters for calculation of each fuse corner
+ */
+struct cpr_thread_desc {
+ u8 controller_id;
+ u8 hw_tid;
+ const int (*ro_scaling_factor)[CPR4_RO_COUNT];
+ bool ro_scaling_factor_common;
+ int init_voltage_step;
+ int init_voltage_width;
+ u8 sensor_range_start;
+ u8 sensor_range_end;
+ u8 step_quot_init_min;
+ u8 step_quot_init_max;
+ unsigned int num_fuse_corners;
+ struct fuse_corner_data *fuse_corner_data;
+};
+
+struct cpr_drv;
+
+/*
+ * struct cpr_desc - Driver instance-wide CPR parameters
+ *
+ * @num_threads: Max. number of threads supported by this controller
+ * @timer_delay_us: Loop delay time in uS
+ * @timer_cons_up: Wait between consecutive up requests in uS
+ * @timer_cons_down: Wait between consecutive down requests in uS
+ * @up_threshold: Generic corner up threshold
+ * @down_threshold: Generic corner down threshold
+ * @idle_clocks: CPR Sensor: idle timer in cpr clocks unit
+ * @count_mode: CPR Sensor: counting mode
+ * @count_repeat: CPR Sensor: number of times to repeat reading
+ * @gcnt_us: CPR measurement interval in uS
+ * @vreg_step_fixed: Regulator voltage per step (if vreg unusable)
+ * @vreg_step_up_limit: Num. of steps up at once before re-measuring sensors
+ * @vreg_step_down_limit: Num. of steps dn at once before re-measuring sensors
+ * @vdd_settle_time_us: Settling timer to account for one VDD supply step
+ * @apm_threshold: Array Power Mux (APM) voltage threshold
+ * @cpr_base_voltage: Safety: Absolute minimum voltage (uV) on this CPR
+ * @cpr_max_voltage: Safety: Absolute maximum voltage (uV) on this CPR
+ * @threads: Array containing "CPR Thread" specific parameters
+ * @get_supported_hw: Select supported OPPs for this SoC
+ * @apm_init: Initialize the SoC array power mux
+ * @apm_switch: Switch the array power mux voltage source
+ */
+struct cpr_desc {
+ unsigned int num_threads;
+ unsigned int timer_delay_us;
+ u8 timer_cons_up;
+ u8 timer_cons_down;
+ u8 up_threshold;
+ u8 down_threshold;
+ u8 idle_clocks;
+ u8 count_mode;
+ u8 count_repeat;
+ u8 gcnt_us;
+ u16 vreg_step_fixed;
+ u8 vreg_step_up_limit;
+ u8 vreg_step_down_limit;
+ u8 vdd_settle_time_us;
+ int apm_threshold;
+ u32 cpr_base_voltage;
+ u32 cpr_max_voltage;
+
+ const struct cpr_thread_desc **threads;
+ const u32 *corner_max_uV;
+ int (*get_supported_hw)(struct cpr_drv *drv);
+ int (*apm_init)(struct cpr_drv *drv);
+ int (*apm_switch)(struct cpr_drv *drv, bool to_apcc);
+};
+
+struct cpr_thread {
+ int num_corners;
+ int id;
+ bool enabled;
+ void __iomem *base;
+ struct clk *cpu_clk;
+ struct corner *corner;
+ struct corner *corners;
+ struct fuse_corner *fuse_corners;
+ struct cpr_drv *drv;
+ struct generic_pm_domain pd;
+ struct device *attached_cpu_dev;
+ struct work_struct restart_work;
+ bool restarting;
+
+ const struct cpr_fuse *cpr_fuses;
+ const struct cpr_thread_desc *desc;
+};
+
+struct cpr_drv {
+ int irq;
+ unsigned int ref_clk_khz;
+ struct device *dev;
+ struct mutex lock;
+ struct regulator *vreg;
+ struct regmap *apm;
+ u32 gcnt;
+ u32 supported_hw;
+ u32 last_uV;
+ unsigned int vreg_step;
+ bool enabled;
+
+ struct cpr_thread *threads;
+ struct genpd_onecell_data cell_data;
+
+ const struct cpr_desc *desc;
+ struct dentry *debugfs;
+};
+
+static int cpr_populate_ring_osc_idx(struct device *dev,
+ struct fuse_corner *fuse_corner,
+ const struct cpr_fuse *cpr_fuse,
+ int num_fuse_corners)
+{
+ struct fuse_corner *end = fuse_corner + num_fuse_corners;
+ u32 data;
+ int ret;
+
+ for (; fuse_corner < end; fuse_corner++, cpr_fuse++) {
+ ret = nvmem_cell_read_variable_le_u32(dev, cpr_fuse->ring_osc, &data);
+ if (ret)
+ return ret;
+ fuse_corner->ring_osc_idx = data;
+ }
+
+ return 0;
+}
+
+static int cpr_read_fuse_uV(int init_v_width, int step_size_uV, int ref_uV,
+ int adj, int step_volt, const char *init_v_efuse,
+ struct device *dev)
+{
+ int steps, uV;
+ u32 bits = 0;
+ int ret;
+
+ ret = nvmem_cell_read_variable_le_u32(dev, init_v_efuse, &bits);
+ if (ret)
+ return ret;
+
+ steps = bits & ~BIT(init_v_width - 1);
+ /* Not two's complement.. instead highest bit is sign bit */
+ if (bits & BIT(init_v_width - 1))
+ steps = -steps;
+
+ uV = ref_uV + steps * step_size_uV;
+
+ /* Apply open-loop fixed adjustments to fused values */
+ uV += adj;
+
+ return DIV_ROUND_UP(uV, step_volt) * step_volt;
+}
+
+static const struct cpr_fuse *cpr_get_fuses(struct device *dev, int tid,
+ unsigned int num_fuse_corners)
+{
+ struct cpr_fuse *fuses;
+ char cpr_name[11]; /* length of "cpr" + length of UINT_MAX (7) + \0 */
+ unsigned int i;
+
+ fuses = devm_kcalloc(dev, num_fuse_corners, sizeof(*fuses), GFP_KERNEL);
+ if (!fuses)
+ return ERR_PTR(-ENOMEM);
+
+ /* Support legacy bindings */
+ if (tid == UINT_MAX)
+ snprintf(cpr_name, sizeof(cpr_name), "cpr");
+ else
+ snprintf(cpr_name, sizeof(cpr_name), "cpr%d", tid);
+
+ for (i = 0; i < num_fuse_corners; i++) {
+ char tbuf[50];
+
+ snprintf(tbuf, sizeof(tbuf), "%s_ring_osc%d", cpr_name, i + 1);
+ fuses[i].ring_osc = devm_kstrdup(dev, tbuf, GFP_KERNEL);
+ if (!fuses[i].ring_osc)
+ return ERR_PTR(-ENOMEM);
+
+ snprintf(tbuf, sizeof(tbuf), "%s_init_voltage%d", cpr_name, i + 1);
+ fuses[i].init_voltage = devm_kstrdup(dev, tbuf, GFP_KERNEL);
+ if (!fuses[i].init_voltage)
+ return ERR_PTR(-ENOMEM);
+
+ snprintf(tbuf, sizeof(tbuf), "%s_quotient%d", cpr_name, i + 1);
+ fuses[i].quotient = devm_kstrdup(dev, tbuf, GFP_KERNEL);
+ if (!fuses[i].quotient)
+ return ERR_PTR(-ENOMEM);
+
+ snprintf(tbuf, sizeof(tbuf), "%s_quotient_offset%d", cpr_name, i + 1);
+ fuses[i].quotient_offset = devm_kstrdup(dev, tbuf, GFP_KERNEL);
+ if (!fuses[i].quotient_offset)
+ return ERR_PTR(-ENOMEM);
+ }
+
+ return fuses;
+}
+
+static int cpr_populate_fuse_common(struct device *dev,
+ struct fuse_corner_data *fdata,
+ const struct cpr_fuse *cpr_fuse,
+ struct fuse_corner *fuse_corner,
+ int step_volt, int init_v_width,
+ int init_v_step)
+{
+ int uV, ret;
+
+ /* Populate uV */
+ uV = cpr_read_fuse_uV(init_v_width, init_v_step,
+ fdata->ref_uV, fdata->volt_oloop_adjust,
+ step_volt, cpr_fuse->init_voltage, dev);
+ if (uV < 0)
+ return uV;
+
+ /*
+ * Update SoC voltages: platforms might choose a different
+ * regulators than the one used to characterize the algorithms
+ * (ie, init_voltage_step).
+ */
+ fdata->min_uV = roundup(fdata->min_uV, step_volt);
+ fdata->max_uV = roundup(fdata->max_uV, step_volt);
+
+ fuse_corner->min_uV = fdata->min_uV;
+ fuse_corner->max_uV = fdata->max_uV;
+ fuse_corner->uV = clamp(uV, fuse_corner->min_uV, fuse_corner->max_uV);
+
+ /* Populate target quotient by scaling */
+ ret = nvmem_cell_read_variable_le_u32(dev, cpr_fuse->quotient, &fuse_corner->quot);
+ if (ret)
+ return ret;
+
+ fuse_corner->quot *= fdata->quot_scale;
+ fuse_corner->quot += fdata->quot_offset;
+ fuse_corner->quot += fdata->quot_adjust;
+
+ return 0;
+}
+
+static int cpr_find_initial_corner(struct device *dev, struct clk *cpu_clk,
+ struct corner *corners, int num_corners)
+{
+ unsigned long rate;
+ struct corner *iter, *corner = NULL;
+ const struct corner *end;
+ unsigned int i = 0;
+
+ if (!cpu_clk) {
+ dev_err(dev, "cannot get rate from NULL clk\n");
+ return -EINVAL;
+ }
+
+ if (num_corners < 1)
+ return -EINVAL;
+
+ end = &corners[num_corners - 1];
+ rate = clk_get_rate(cpu_clk);
+ if (!rate) {
+ dev_warn(dev, "CPU clock rate unavailable, using highest CPR corner\n");
+ return num_corners - 1;
+ }
+
+ if (rate < corners[0].freq) {
+ dev_info(dev, "boot CPU rate %lu below lowest OPP %llu, using first CPR corner\n",
+ rate, corners[0].freq);
+ return 0;
+ }
+
+ /*
+ * Some bootloaders set a CPU clock frequency that is not defined
+ * in the OPP table. When running at an unlisted frequency,
+ * cpufreq_online() will change to the OPP which has the lowest
+ * frequency, at or above the unlisted frequency.
+ * Since cpufreq_online() always "rounds up" in the case of an
+ * unlisted frequency, this function always "rounds down" in case
+ * of an unlisted frequency. That way, when cpufreq_online()
+ * triggers the first ever call to cpr_set_performance_state(),
+ * it will correctly determine the direction as UP.
+ */
+ for (iter = corners; iter <= end; iter++) {
+ if (iter->freq > rate)
+ break;
+ i++;
+ if (iter->freq == rate) {
+ corner = iter;
+ break;
+ }
+ if (iter->freq < rate)
+ corner = iter;
+ }
+
+ if (!corner) {
+ dev_err(dev, "boot CPU rate %lu has no CPR corner\n", rate);
+ return -EINVAL;
+ }
+
+ dev_dbg(dev, "boot CPU rate %lu uses CPR corner %u\n", rate, i);
+
+ return i - 1;
+}
+
+static unsigned int cpr_get_fuse_corner(struct dev_pm_opp *opp, u32 tid)
+{
+ struct device_node *np;
+ unsigned int fuse_corner = 0;
+
+ np = dev_pm_opp_get_of_node(opp);
+ if (of_property_read_u32_index(np, "qcom,opp-fuse-level", tid, &fuse_corner))
+ pr_err("%s: missing 'qcom,opp-fuse-level[%u]' property\n",
+ __func__, tid);
+
+ of_node_put(np);
+
+ return fuse_corner;
+}
+
+static u64 cpr_get_opp_hz_for_req(struct dev_pm_opp *ref,
+ struct device *cpu_dev, u32 supported_hw)
+{
+ u64 rate = 0;
+ u32 opp_hw;
+ struct device_node *ref_np;
+ struct device_node *desc_np;
+ struct device_node *child_np = NULL;
+ struct device_node *child_req_np = NULL;
+
+ desc_np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
+ if (!desc_np)
+ return 0;
+
+ ref_np = dev_pm_opp_get_of_node(ref);
+ if (!ref_np)
+ goto out_ref;
+
+ for_each_available_child_of_node(desc_np, child_np) {
+ if (!of_property_read_u32(child_np, "opp-supported-hw", &opp_hw) &&
+ !(opp_hw & supported_hw))
+ continue;
+
+ child_req_np = of_parse_phandle(child_np, "required-opps", 0);
+
+ if (child_req_np == ref_np) {
+ of_property_read_u64(child_np, "opp-hz", &rate);
+ goto out;
+ }
+ of_node_put(child_req_np);
+ child_req_np = NULL;
+ }
+
+out:
+ of_node_put(child_req_np);
+ of_node_put(child_np);
+ of_node_put(ref_np);
+out_ref:
+ of_node_put(desc_np);
+
+ return rate;
+}
+
+static int cpr_calculate_scaling(struct device *dev,
+ const char *quot_offset,
+ const struct fuse_corner_data *fdata,
+ const struct corner *corner)
+{
+ u32 quot_diff = 0;
+ u64 freq_diff;
+ int scaling;
+ const struct fuse_corner *fuse, *prev_fuse;
+ int ret;
+
+ fuse = corner->fuse_corner;
+ prev_fuse = fuse - 1;
+
+ if (quot_offset) {
+ ret = nvmem_cell_read_variable_le_u32(dev, quot_offset, "_diff);
+ if (ret)
+ return ret;
+
+ quot_diff *= fdata->quot_offset_scale;
+ quot_diff += fdata->quot_offset_adjust;
+ } else {
+ quot_diff = fuse->quot - prev_fuse->quot;
+ }
+
+ freq_diff = fuse->max_freq - prev_fuse->max_freq;
+ freq_diff = div_u64(freq_diff, 1000000); /* Convert to MHz */
+ scaling = 1000 * quot_diff;
+ do_div(scaling, freq_diff);
+ return min(scaling, fdata->max_quot_scale);
+}
+
+static int cpr_interpolate(const struct corner *corner, int step_volt,
+ const struct fuse_corner_data *fdata)
+{
+ u64 f_high, f_low, f_diff;
+ int uV_high, uV_low, uV;
+ u64 temp, temp_limit;
+ const struct fuse_corner *fuse, *prev_fuse;
+
+ fuse = corner->fuse_corner;
+ prev_fuse = fuse - 1;
+
+ f_high = fuse->max_freq;
+ f_low = prev_fuse->max_freq;
+ uV_high = fuse->uV;
+ uV_low = prev_fuse->uV;
+ f_diff = fuse->max_freq - corner->freq;
+
+ /*
+ * Don't interpolate in the wrong direction. This could happen
+ * if the adjusted fuse voltage overlaps with the previous fuse's
+ * adjusted voltage.
+ */
+ if (f_high <= f_low || uV_high <= uV_low || f_high <= corner->freq)
+ return corner->uV;
+
+ temp = f_diff * (uV_high - uV_low);
+ temp = div64_ul(temp, f_high - f_low);
+
+ /*
+ * max_volt_scale has units of uV/MHz while freq values
+ * have units of Hz. Divide by 1000000 to convert to.
+ */
+ temp_limit = f_diff * fdata->max_volt_scale;
+ do_div(temp_limit, 1000000);
+
+ uV = uV_high - min(temp, temp_limit);
+ return roundup(uV, step_volt);
+}
+
+static int cpr_check_vreg_constraints(struct device *dev, struct regulator *vreg,
+ struct fuse_corner *f)
+{
+ int ret;
+
+ ret = regulator_is_supported_voltage(vreg, f->min_uV, f->min_uV);
+ if (!ret) {
+ dev_err(dev, "min uV: %d not supported by regulator\n",
+ f->min_uV);
+ return -EINVAL;
+ }
+
+ ret = regulator_is_supported_voltage(vreg, f->max_uV, f->max_uV);
+ if (!ret) {
+ dev_err(dev, "max uV: %d not supported by regulator\n",
+ f->max_uV);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static void cpr_write(struct cpr_thread *thread, u32 offset, u32 value)
+{
+ writel(value, thread->base + offset);
+}
+
+static u32 cpr_read(struct cpr_thread *thread, u32 offset)
+{
+ return readl(thread->base + offset);
+}
+
+static void
+cpr_masked_write(struct cpr_thread *thread, u32 offset, u32 mask, u32 value)
+{
+ u32 val;
+
+ val = readl(thread->base + offset);
+ val &= ~mask;
+ val |= value & mask;
+ writel(val, thread->base + offset);
+}
+
+static void cpr_irq_clr(struct cpr_thread *thread)
+{
+ cpr_write(thread, CPR4_REG_IRQ_CLEAR, CPR4_IRQ_ALL);
+}
+
+static void cpr_irq_clr_nack(struct cpr_thread *thread)
+{
+ cpr_irq_clr(thread);
+ cpr_write(thread, CPR4_REG_CONT_CMD, CPR4_CONT_CMD_NACK);
+}
+
+static void cpr_irq_clr_ack(struct cpr_thread *thread)
+{
+ cpr_irq_clr(thread);
+ cpr_write(thread, CPR4_REG_CONT_CMD, CPR4_CONT_CMD_ACK);
+}
+
+static void cpr_irq_set(struct cpr_thread *thread, u32 int_bits)
+{
+ cpr_write(thread, CPR4_REG_IRQ_EN, int_bits);
+}
+
+/**
+ * cpr_ctl_enable() - Enable CPR thread
+ * @thread: Structure holding CPR thread-specific parameters
+ */
+static void cpr_ctl_enable(struct cpr_thread *thread)
+{
+ if (thread->drv->enabled && !thread->restarting) {
+ cpr_masked_write(thread, CPR4_REG_CPR_CTL,
+ CPR4_CPR_CTL_LOOP_EN_MASK,
+ CPR4_CPR_CTL_LOOP_EN_MASK);
+ }
+}
+
+/**
+ * cpr_ctl_disable() - Disable CPR thread
+ * @thread: Structure holding CPR thread-specific parameters
+ */
+static void cpr_ctl_disable(struct cpr_thread *thread)
+{
+ cpr_irq_set(thread, 0);
+ cpr_irq_clr(thread);
+
+ cpr_masked_write(thread, CPR4_REG_CPR_CTL,
+ CPR4_CPR_CTL_LOOP_EN_MASK, 0);
+}
+
+/**
+ * cpr_ctl_is_enabled() - Check if thread is enabled
+ * @thread: Structure holding CPR thread-specific parameters
+ *
+ * Return: true if the CPR is enabled, false if it is disabled.
+ */
+static bool cpr_ctl_is_enabled(struct cpr_thread *thread)
+{
+ u32 reg_val;
+
+ reg_val = cpr_read(thread, CPR4_REG_CPR_CTL);
+ return reg_val & CPR4_CPR_CTL_LOOP_EN_MASK;
+}
+
+/**
+ * cpr_check_any_thread_busy() - Check if HW is done processing
+ * @thread: Structure holding CPR thread-specific parameters
+ *
+ * Return: true if the CPR is busy, false if it is ready.
+ */
+static bool cpr_check_any_thread_busy(struct cpr_thread *thread)
+{
+ int i;
+
+ for (i = 0; i < thread->drv->desc->num_threads; i++)
+ if (cpr_read(thread, CPR4_REG_RESULT0(i)) &
+ CPR4_RESULT0_BUSY_MASK)
+ return true;
+
+ return false;
+}
+
+static void cpr_restart_worker(struct work_struct *work)
+{
+ struct cpr_thread *thread = container_of(work, struct cpr_thread,
+ restart_work);
+ struct cpr_drv *drv = thread->drv;
+ int i;
+
+ mutex_lock(&drv->lock);
+
+ thread->restarting = true;
+ cpr_ctl_disable(thread);
+ disable_irq(drv->irq);
+
+ mutex_unlock(&drv->lock);
+
+ for (i = 0; i < 20; i++) {
+ u32 cpr_status = cpr_read(thread, CPR4_REG_CPR_STATUS);
+ u32 ctl = cpr_read(thread, CPR4_REG_CPR_CTL);
+
+ if ((cpr_status & CPR4_CPR_STATUS_BUSY_MASK) &&
+ !(ctl & CPR4_CPR_CTL_LOOP_EN_MASK))
+ break;
+
+ udelay(10);
+ }
+
+ cpr_irq_clr(thread);
+
+ for (i = 0; i < 20; i++) {
+ u32 status = cpr_read(thread, CPR4_REG_IRQ_STATUS);
+
+ if (!(status & CPR4_IRQ_ALL))
+ break;
+ udelay(10);
+ }
+
+ mutex_lock(&drv->lock);
+
+ thread->restarting = false;
+ enable_irq(drv->irq);
+ cpr_ctl_enable(thread);
+
+ mutex_unlock(&drv->lock);
+}
+
+/**
+ * cpr_corner_restore() - Restore saved corner level
+ * @thread: Structure holding CPR thread-specific parameters
+ * @corner: Structure holding the saved corner level
+ */
+static void cpr_corner_restore(struct cpr_thread *thread,
+ struct corner *corner)
+{
+ struct cpr_drv *drv = thread->drv;
+ struct fuse_corner *fuse = corner->fuse_corner;
+ const struct cpr_thread_desc *tdesc = thread->desc;
+ u32 ro_sel = fuse->ring_osc_idx;
+
+ cpr_write(thread, CPR4_REG_GCNT(ro_sel), drv->gcnt);
+
+ cpr_write(thread, CPR4_REG_RO_MASK(tdesc->hw_tid),
+ CPR4_RO_MASK & ~BIT(ro_sel));
+
+ cpr_write(thread, CPR4_REG_TARGET_QUOT(tdesc->hw_tid, ro_sel),
+ fuse->quot - corner->quot_adjust);
+
+ cpr_masked_write(thread, CPR4_REG_CPR_MASK_THREAD(tdesc->hw_tid),
+ CPR4_CPR_MASK_THREAD_DISABLE_THREAD |
+ CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK, 0);
+
+ thread->corner = corner;
+ corner->last_uV = corner->uV;
+}
+
+/**
+ * cpr_commit_state() - Set the newly requested voltage
+ * @thread: Structure holding CPR thread-specific parameters
+ *
+ * Return: IRQ_SUCCESS for success, IRQ_NONE if the CPR is disabled.
+ */
+static int cpr_commit_state(struct cpr_thread *thread)
+{
+ struct cpr_drv *drv = thread->drv;
+ int min_uV = 0, max_uV = 0, new_uV = 0;
+ u32 next_irqmask = 0;
+ int ret, i;
+
+ for (i = 0; i < drv->desc->num_threads; i++) {
+ struct cpr_thread *thread = &drv->threads[i];
+
+ if (!thread->corner)
+ continue;
+
+ max_uV = max(max_uV, thread->corner->max_uV);
+ min_uV = max(min_uV, thread->corner->min_uV);
+ new_uV = max(new_uV, thread->corner->last_uV);
+ }
+ dev_vdbg(drv->dev, "%s: new uV: %d, last uV: %d\n",
+ __func__, new_uV, drv->last_uV);
+
+ /*
+ * Safety measure: if the voltage is out of the globally allowed
+ * range, then go out and warn the user.
+ * This should *never* happen.
+ */
+ if (new_uV > drv->desc->cpr_max_voltage ||
+ new_uV < drv->desc->cpr_base_voltage) {
+ dev_warn(drv->dev, "Voltage (%u uV) out of range.", new_uV);
+ return -EINVAL;
+ }
+
+ if (new_uV == drv->last_uV)
+ goto out;
+
+ dev_vdbg(drv->dev, "setting voltage: %d\n", new_uV);
+
+ if (drv->apm &&
+ ((drv->last_uV < drv->desc->apm_threshold &&
+ new_uV >= drv->desc->apm_threshold) ||
+ (drv->last_uV >= drv->desc->apm_threshold &&
+ new_uV < drv->desc->apm_threshold))) {
+ ret = regulator_set_voltage(drv->vreg,
+ drv->desc->apm_threshold,
+ drv->desc->apm_threshold);
+ if (ret)
+ return ret;
+
+ ret = drv->desc->apm_switch(drv,
+ new_uV >= drv->desc->apm_threshold);
+ if (ret)
+ return ret;
+ }
+
+ ret = regulator_set_voltage(drv->vreg, new_uV, new_uV);
+ if (ret) {
+ dev_err_ratelimited(drv->dev, "failed to set voltage %d: %d\n", new_uV, ret);
+ return ret;
+ }
+
+ drv->last_uV = new_uV;
+out:
+ if (new_uV > min_uV)
+ next_irqmask |= CPR4_IRQ_DOWN;
+ if (new_uV < max_uV)
+ next_irqmask |= CPR4_IRQ_UP;
+
+ cpr_irq_set(thread, next_irqmask);
+
+ return 0;
+}
+
+static unsigned int cpr_get_cur_perf_state(struct cpr_thread *thread)
+{
+ return thread->corner ? thread->corner - thread->corners + 1 : 0;
+}
+
+/**
+ * cpr_scale() - Calculate new voltage for the received direction
+ * @thread: Structure holding CPR thread-specific parameters
+ * @dir: Enumeration for voltage change direction
+ *
+ * The CPR scales one by one: this function calculates the new
+ * voltage to set when a voltage-UP or voltage-DOWN request comes
+ * and stores it into the per-thread structure that gets passed.
+ */
+static void cpr_scale(struct cpr_thread *thread, enum voltage_change_dir dir)
+{
+ struct cpr_drv *drv = thread->drv;
+ const struct cpr_thread_desc *tdesc = thread->desc;
+ u32 val, error_steps;
+ int last_uV, new_uV;
+ struct corner *corner;
+
+ if (dir != UP && dir != DOWN)
+ return;
+
+ corner = thread->corner;
+ val = cpr_read(thread, CPR4_REG_RESULT0(tdesc->hw_tid));
+ error_steps = FIELD_GET(CPR4_RESULT0_ERROR_STEPS_MASK, val);
+
+ last_uV = corner->last_uV;
+
+ if (dir == UP) {
+ if (!(val & CPR4_RESULT0_STEP_UP_MASK))
+ return;
+
+ /* Calculate new voltage */
+ new_uV = last_uV + drv->vreg_step;
+ new_uV = min(new_uV, corner->max_uV);
+
+ dev_vdbg(drv->dev, "[T%u] UP - new_uV=%d last_uV=%d p-state=%u st=%u\n",
+ thread->id, new_uV, last_uV,
+ cpr_get_cur_perf_state(thread), error_steps);
+ } else {
+ if (!(val & CPR4_RESULT0_STEP_DN_MASK))
+ return;
+
+ /* Calculate new voltage */
+ new_uV = last_uV - drv->vreg_step;
+ new_uV = max(new_uV, corner->min_uV);
+ dev_vdbg(drv->dev, "[T%u] DOWN - new_uV=%d last_uV=%d p-state=%u st=%u\n",
+ thread->id, new_uV, last_uV,
+ cpr_get_cur_perf_state(thread), error_steps);
+ }
+ corner->last_uV = new_uV;
+}
+
+/**
+ * cpr_irq_handler() - Handle CPR4 status interrupts
+ * @irq: Number of the interrupt
+ * @dev: Pointer to the cpr_thread structure
+ *
+ * Handle the interrupts coming from non-hardened CPR HW as to get
+ * an ok to scale voltages immediately, or to pass error status to
+ * the hardware (either success/ACK or failure/NACK).
+ *
+ * Return: IRQ_SUCCESS for success, IRQ_NONE if the CPR is disabled.
+ */
+static irqreturn_t cpr_irq_handler(int irq, void *dev)
+{
+ struct cpr_thread *thread = dev;
+ struct cpr_drv *drv = thread->drv;
+ irqreturn_t ret = IRQ_HANDLED;
+ int i, rc;
+ enum voltage_change_dir dir = NO_CHANGE;
+ u32 val;
+
+ mutex_lock(&drv->lock);
+
+ val = cpr_read(thread, CPR4_REG_IRQ_STATUS);
+
+ dev_vdbg(drv->dev, "IRQ_STATUS = 0x%x\n", val);
+
+ if (!cpr_ctl_is_enabled(thread)) {
+ dev_vdbg(drv->dev, "CPR is disabled\n");
+ ret = IRQ_NONE;
+ } else if (cpr_check_any_thread_busy(thread)) {
+ cpr_irq_clr_nack(thread);
+ dev_dbg(drv->dev, "CPR measurement is not ready\n");
+ } else {
+ /*
+ * Following sequence of handling is as per each IRQ's
+ * priority
+ */
+ if (val & CPR4_IRQ_UP)
+ dir = UP;
+ else if (val & CPR4_IRQ_DOWN)
+ dir = DOWN;
+
+ if (dir != NO_CHANGE) {
+ for (i = 0; i < drv->desc->num_threads; i++) {
+ thread = &drv->threads[i];
+ cpr_scale(thread, dir);
+ }
+
+ rc = cpr_commit_state(thread);
+ if (rc)
+ cpr_irq_clr_nack(thread);
+ else
+ cpr_irq_clr_ack(thread);
+ } else if (val & CPR4_IRQ_MID) {
+ dev_dbg(drv->dev, "IRQ occurred for Mid Flag\n");
+ } else {
+ dev_dbg(drv->dev, "IRQ received without UP/DOWN status (%#08x)\n",
+ val);
+ schedule_work(&thread->restart_work);
+ }
+ }
+
+ mutex_unlock(&drv->lock);
+
+ return ret;
+}
+
+static int cpr_switch(struct cpr_drv *drv)
+{
+ int i, ret;
+ bool enabled = false;
+
+ for (i = 0; i < drv->desc->num_threads && !enabled; i++)
+ enabled = drv->threads[i].enabled;
+
+ dev_vdbg(drv->dev, "%s: enabled = %d\n", __func__, enabled);
+
+ if (enabled == drv->enabled)
+ return 0;
+
+ if (enabled) {
+ ret = regulator_enable(drv->vreg);
+ if (ret)
+ return ret;
+
+ drv->enabled = enabled;
+
+ for (i = 0; i < drv->desc->num_threads; i++)
+ if (drv->threads[i].corner)
+ break;
+
+ if (i < drv->desc->num_threads) {
+ cpr_irq_clr(&drv->threads[i]);
+
+ ret = cpr_commit_state(&drv->threads[i]);
+ if (ret) {
+ drv->enabled = false;
+ regulator_disable(drv->vreg);
+ return ret;
+ }
+ cpr_ctl_enable(&drv->threads[i]);
+ }
+ } else {
+ for (i = 0; i < drv->desc->num_threads && !enabled; i++)
+ cpr_ctl_disable(&drv->threads[i]);
+
+ drv->enabled = enabled;
+
+ ret = regulator_disable(drv->vreg);
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
+/**
+ * cpr_enable() - Enables a CPR thread
+ * @thread: Structure holding CPR thread-specific parameters
+ *
+ * Return: Zero for success or negative number on errors.
+ */
+static int cpr_enable(struct cpr_thread *thread)
+{
+ struct cpr_drv *drv = thread->drv;
+ int ret;
+
+ dev_dbg(drv->dev, "Enabling thread %d\n", thread->id);
+
+ mutex_lock(&drv->lock);
+
+ thread->enabled = true;
+ ret = cpr_switch(thread->drv);
+
+ mutex_unlock(&drv->lock);
+
+ return ret;
+}
+
+/**
+ * cpr_disable() - Disables a CPR thread
+ * @thread: Structure holding CPR thread-specific parameters
+ *
+ * Return: Zero for success or negative number on errors.
+ */
+static int cpr_disable(struct cpr_thread *thread)
+{
+ struct cpr_drv *drv = thread->drv;
+ int ret;
+
+ dev_dbg(drv->dev, "Disabling thread %d\n", thread->id);
+
+ mutex_lock(&drv->lock);
+
+ thread->enabled = false;
+ ret = cpr_switch(thread->drv);
+
+ mutex_unlock(&drv->lock);
+
+ return ret;
+}
+
+/**
+ * cpr_configure() - Configure main HW parameters
+ * @thread: Structure holding CPR thread-specific parameters
+ *
+ * This function configures the main CPR hardware parameters, such as
+ * internal timers (and delays), sensor ownerships, activates and/or
+ * deactivates CPR threads.
+ *
+ * Context: The CPR must be clocked before calling this function!
+ *
+ * Return: Zero for success or negative number on errors.
+ */
+static int cpr_configure(struct cpr_thread *thread)
+{
+ struct cpr_drv *drv = thread->drv;
+ const struct cpr_desc *desc = drv->desc;
+ const struct cpr_thread_desc *tdesc = thread->desc;
+ u32 val;
+ int i;
+
+ /* Disable interrupt and CPR */
+ cpr_irq_set(thread, 0);
+ cpr_write(thread, CPR4_REG_CPR_CTL, 0);
+
+ /* Init and save gcnt */
+ drv->gcnt = drv->ref_clk_khz * desc->gcnt_us;
+ do_div(drv->gcnt, 1000);
+
+ /* Program the delay count for the timer */
+ val = drv->ref_clk_khz * desc->timer_delay_us;
+ do_div(val, 1000);
+ cpr_write(thread, CPR4_REG_CPR_TIMER_AUTO_CONT, val);
+ dev_dbg(drv->dev, "Timer count: %#0x (for %d us)\n", val,
+ desc->timer_delay_us);
+
+ /* Program the control register */
+ val = FIELD_PREP(CPR4_CPR_CTL_IDLE_CLOCKS_MASK, desc->idle_clocks) |
+ FIELD_PREP(CPR4_CPR_CTL_COUNT_MODE_MASK, desc->count_mode) |
+ FIELD_PREP(CPR4_CPR_CTL_COUNT_REPEAT_MASK, desc->count_repeat);
+ cpr_write(thread, CPR4_REG_CPR_CTL, val);
+
+ /* Configure CPR default step quotients */
+ val = FIELD_PREP(CPR4_CPR_STEP_QUOT_MIN_MASK, tdesc->step_quot_init_min) |
+ FIELD_PREP(CPR4_CPR_STEP_QUOT_MAX_MASK, tdesc->step_quot_init_max);
+
+ cpr_write(thread, CPR4_REG_CPR_STEP_QUOT, val);
+
+ /*
+ * Configure the CPR sensor ownership always on thread 0
+ * TODO: SDM845 has different ownership for sensors!!
+ */
+ for (i = tdesc->sensor_range_start; i < tdesc->sensor_range_end; i++)
+ cpr_write(thread, CPR4_REG_SENSOR_OWNER(i), 0);
+
+ /* Program Consecutive Up & Down */
+ val = FIELD_PREP(CPR4_THRESH_CONS_DOWN_MASK, desc->timer_cons_down) |
+ FIELD_PREP(CPR4_THRESH_CONS_UP_MASK, desc->timer_cons_up) |
+ FIELD_PREP(CPR4_THRESH_DOWN_THRESH_MASK, desc->down_threshold) |
+ FIELD_PREP(CPR4_THRESH_UP_THRESH_MASK, desc->up_threshold);
+ cpr_write(thread, CPR4_REG_THRESH(tdesc->hw_tid), val);
+
+ /* Mask all ring oscillators for all threads initially */
+ cpr_write(thread, CPR4_REG_RO_MASK(tdesc->hw_tid), CPR4_RO_MASK);
+
+ /* Voltage changes are handled in software by the CPR interrupt. */
+ cpr_masked_write(thread, CPR4_REG_MARGIN_ADJ_CTL,
+ CPR4_MARGIN_ADJ_HW_CLOSED_LOOP_EN, 0);
+
+ cpr_masked_write(thread, CPR4_REG_CPR_MASK_THREAD(1),
+ CPR4_CPR_MASK_THREAD_DISABLE_THREAD |
+ CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK,
+ CPR4_CPR_MASK_THREAD_DISABLE_THREAD |
+ CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK);
+
+ if (tdesc->hw_tid > 0)
+ cpr_masked_write(thread, CPR4_REG_MISC,
+ CPR4_MISC_RESET_STEP_QUOT_LOOP_EN |
+ CPR4_MISC_THREAD_HAS_ALWAYS_VOTE_EN,
+ CPR4_MISC_RESET_STEP_QUOT_LOOP_EN |
+ CPR4_MISC_THREAD_HAS_ALWAYS_VOTE_EN);
+
+ val = drv->vreg_step;
+ do_div(val, 1000);
+ cpr_masked_write(thread, CPR4_REG_MARGIN_ADJ_CTL,
+ CPR4_MARGIN_ADJ_PMIC_STEP_SIZE_MASK,
+ val << CPR4_MARGIN_ADJ_PMIC_STEP_SIZE_SHIFT);
+
+ cpr_masked_write(thread, CPR4_REG_SAW_ERROR_STEP_LIMIT,
+ CPR4_SAW_ERROR_STEP_LIMIT_DN_MASK,
+ desc->vreg_step_down_limit <<
+ CPR4_SAW_ERROR_STEP_LIMIT_DN_SHIFT);
+
+ cpr_masked_write(thread, CPR4_REG_SAW_ERROR_STEP_LIMIT,
+ CPR4_SAW_ERROR_STEP_LIMIT_UP_MASK,
+ desc->vreg_step_up_limit <<
+ CPR4_SAW_ERROR_STEP_LIMIT_UP_SHIFT);
+
+ cpr_masked_write(thread, CPR4_REG_MARGIN_ADJ_CTL,
+ CPR4_MARGIN_ADJ_PER_RO_KV_MARGIN_EN,
+ CPR4_MARGIN_ADJ_PER_RO_KV_MARGIN_EN);
+
+ if (tdesc->hw_tid > 0)
+ cpr_masked_write(thread, CPR4_REG_CPR_TIMER_CLAMP,
+ CPR4_CPR_TIMER_CLAMP_THREAD_AGGREGATION_EN,
+ CPR4_CPR_TIMER_CLAMP_THREAD_AGGREGATION_EN);
+
+ /* Settling timer to account for one VDD supply step */
+ if (desc->vdd_settle_time_us > 0) {
+ u32 m = CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_MASK;
+ u32 s = CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_SHFT;
+
+ cpr_masked_write(thread, CPR4_REG_MARGIN_TEMP_CORE_TIMERS,
+ m, desc->vdd_settle_time_us << s);
+ }
+
+ return 0;
+}
+
+static int cpr_set_performance_state(struct generic_pm_domain *domain,
+ unsigned int state)
+{
+ struct cpr_thread *thread = container_of(domain, struct cpr_thread, pd);
+ struct cpr_drv *drv = thread->drv;
+ struct corner *corner, *end;
+ int ret = 0;
+
+ mutex_lock(&drv->lock);
+
+ dev_dbg(drv->dev, "setting perf state: %u (prev state: %u thread: %u)\n",
+ state, cpr_get_cur_perf_state(thread), thread->id);
+
+ /*
+ * Determine new corner we're going to.
+ * Remove one since lowest performance state is 1.
+ */
+ if (!state || state > thread->num_corners) {
+ ret = -EINVAL;
+ goto unlock;
+ }
+ corner = thread->corners + state - 1;
+ end = &thread->corners[thread->num_corners - 1];
+ if (corner > end || corner < thread->corners) {
+ ret = -EINVAL;
+ goto unlock;
+ }
+
+ cpr_ctl_disable(thread);
+
+ cpr_irq_clr(thread);
+ if (thread->corner != corner)
+ cpr_corner_restore(thread, corner);
+
+ ret = cpr_commit_state(thread);
+ if (ret)
+ goto unlock;
+
+ cpr_ctl_enable(thread);
+unlock:
+ mutex_unlock(&drv->lock);
+
+ dev_dbg(drv->dev, "set perf state %u on thread %u\n", state, thread->id);
+
+ return ret;
+}
+
+/**
+ * cpr4_adjust_quot - Adjust the closed-loop quotients
+ * @ring_osc_factor: Ring oscillator adjustment factor
+ * @volt_closed_loop: Closed-loop voltage adjustment factor
+ *
+ * Calculates the quotient adjustment factor based on closed-loop
+ * quotients and ring oscillator factor.
+ *
+ * Return: Adjusted quotient
+ */
+static int cpr4_adjust_quot(int ring_osc_factor, int volt_closed_loop)
+{
+ s64 temp;
+
+ if (ring_osc_factor == 0 || volt_closed_loop == 0)
+ return 0;
+
+ temp = (s64)(ring_osc_factor * volt_closed_loop);
+ return (int)div_s64(temp, 1000000);
+}
+
+/**
+ * cpr_fuse_corner_init() - Calculate fuse corner table
+ * @thread: Structure holding CPR thread-specific parameters
+ *
+ * This function populates the fuse corners table by reading the
+ * values from the fuses, eventually adjusting them with a fixed
+ * per-corner offset and doing basic checks about them being
+ * supported by the regulator assigned to this CPR controller.
+ *
+ * Return: Zero for success, negative number on error
+ */
+static int cpr_fuse_corner_init(struct cpr_thread *thread)
+{
+ struct cpr_drv *drv = thread->drv;
+ const struct cpr_thread_desc *desc = thread->desc;
+ const struct cpr_fuse *cpr_fuse = thread->cpr_fuses;
+ struct fuse_corner_data *fdata;
+ struct fuse_corner *fuse, *prev_fuse, *end;
+ int i, ret;
+
+ /* Populate fuse_corner members */
+ fuse = thread->fuse_corners;
+ prev_fuse = &fuse[0];
+ end = &fuse[desc->num_fuse_corners - 1];
+ fdata = desc->fuse_corner_data;
+
+ for (i = 0; fuse <= end; fuse++, cpr_fuse++, i++, fdata++) {
+ int sf_idx = desc->ro_scaling_factor_common ? 0 : i;
+ int factor = desc->ro_scaling_factor[sf_idx][fuse->ring_osc_idx];
+
+ ret = cpr_populate_fuse_common(drv->dev, fdata, cpr_fuse,
+ fuse, drv->vreg_step,
+ desc->init_voltage_width,
+ desc->init_voltage_step);
+ if (ret)
+ return ret;
+
+ /*
+ * Adjust the fuse quot with per-fuse-corner closed-loop
+ * voltage adjustment parameters.
+ */
+ fuse->quot += cpr4_adjust_quot(factor, fdata->volt_cloop_adjust);
+
+ /* Re-check if corner voltage range is supported by regulator */
+ ret = cpr_check_vreg_constraints(drv->dev, drv->vreg, fuse);
+ if (ret)
+ return ret;
+
+ if (fuse->uV < prev_fuse->uV)
+ fuse->uV = prev_fuse->uV;
+ prev_fuse = fuse;
+ dev_dbg(drv->dev, "fuse corner %d: [%d %d %d] RO%u quot %d\n",
+ i, fuse->min_uV, fuse->uV, fuse->max_uV,
+ fuse->ring_osc_idx, fuse->quot);
+
+ /* Check if constraints are valid */
+ if (fuse->uV < fuse->min_uV || fuse->uV > fuse->max_uV) {
+ dev_err(drv->dev, "fuse corner %d: Bad voltage range.\n", i);
+ return -EINVAL;
+ }
+ }
+
+ return 0;
+}
+
+static void cpr4_restrict_corner(struct corner *corner, int threshold,
+ int hysteresis, int step)
+{
+ if (threshold > corner->min_uV && threshold <= corner->max_uV) {
+ if (corner->uV >= threshold) {
+ corner->min_uV = max(corner->min_uV,
+ threshold - hysteresis);
+ if (corner->min_uV > corner->uV)
+ corner->uV = corner->min_uV;
+ } else {
+ corner->max_uV = threshold;
+ corner->max_uV -= step;
+ }
+ }
+}
+
+/**
+ * cpr4_corner_init() - Calculate and set-up corners for the CPR HW
+ * @thread: Structure holding CPR thread-specific parameters
+ *
+ * This function calculates all the corner parameters by comparing
+ * and interpolating the values read from the various set-points
+ * read from the fuses (also called "fuse corners") to generate and
+ * program to the CPR a lookup table that describes each voltage
+ * step, mapped to a performance level (or corner number).
+ *
+ * Return: Zero for success, negative number on error
+ */
+static int cpr4_corner_init(struct cpr_thread *thread)
+{
+ struct cpr_drv *drv = thread->drv;
+ const struct cpr_desc *desc = drv->desc;
+ const struct cpr_thread_desc *tdesc = thread->desc;
+ const struct cpr_fuse *fuses = thread->cpr_fuses;
+ int i, level, scaling = 0;
+ unsigned int fnum, fc;
+ const char *quot_offset;
+ const struct fuse_corner_data *fdata;
+ struct fuse_corner *fuse, *prev_fuse;
+ struct corner *corner, *prev_corner, *end;
+ struct corner_data *cdata;
+ struct dev_pm_opp *opp;
+ u32 ring_osc_mask = CPR4_RO_MASK, min_quotient = U32_MAX;
+ u64 freq;
+
+ corner = thread->corners;
+ prev_corner = &thread->corners[0];
+ end = &corner[thread->num_corners - 1];
+
+ cdata = devm_kcalloc(drv->dev, thread->num_corners,
+ sizeof(struct corner_data), GFP_KERNEL);
+ if (!cdata)
+ return -ENOMEM;
+
+ for (level = 1; level <= thread->num_corners; level++) {
+ opp = dev_pm_opp_find_level_exact(&thread->pd.dev, level);
+ if (IS_ERR(opp))
+ return -EINVAL;
+
+ fc = cpr_get_fuse_corner(opp, thread->id);
+ if (!fc) {
+ dev_pm_opp_put(opp);
+ return -EINVAL;
+ }
+ fnum = fc - 1;
+
+ freq = cpr_get_opp_hz_for_req(opp, thread->attached_cpu_dev,
+ drv->supported_hw);
+ if (!freq) {
+ thread->num_corners = max(level - 1, 0);
+ end = &thread->corners[thread->num_corners - 1];
+ dev_pm_opp_put(opp);
+ break;
+ }
+
+ cdata[level - 1].fuse_corner = fnum;
+ cdata[level - 1].freq = freq;
+
+ fuse = &thread->fuse_corners[fnum];
+ dev_dbg(drv->dev, "freq: %llu level: %u fuse level: %u\n",
+ freq, dev_pm_opp_get_level(opp) - 1, fnum);
+ if (freq > fuse->max_freq)
+ fuse->max_freq = freq;
+ dev_pm_opp_put(opp);
+
+ /*
+ * Make sure that the frequencies in the table are in ascending
+ * order, as this is critical for the algorithm to work.
+ */
+ if (level > 1 && cdata[level - 2].freq > freq) {
+ dev_err(drv->dev, "Frequency table not in ascending order.\n");
+ return -EINVAL;
+ }
+ }
+
+ if (thread->num_corners < 2) {
+ dev_err(drv->dev, "need at least 2 OPPs to use CPR\n");
+ return -EINVAL;
+ }
+
+ /*
+ * Get the quotient adjustment scaling factor, according to:
+ *
+ * scaling = min(1000 * (QUOT(corner_N) - QUOT(corner_N-1))
+ * / (freq(corner_N) - freq(corner_N-1)), max_factor)
+ *
+ * QUOT(corner_N): quotient read from fuse for fuse corner N
+ * QUOT(corner_N-1): quotient read from fuse for fuse corner (N - 1)
+ * freq(corner_N): max frequency in MHz supported by fuse corner N
+ * freq(corner_N-1): max frequency in MHz supported by fuse corner
+ * (N - 1)
+ *
+ * Then walk through the corners mapped to each fuse corner
+ * and calculate the quotient adjustment for each one using the
+ * following formula:
+ *
+ * quot_adjust = (freq_max - freq_corner) * scaling / 1000
+ *
+ * freq_max: max frequency in MHz supported by the fuse corner
+ * freq_corner: frequency in MHz corresponding to the corner
+ * scaling: calculated from above equation
+ *
+ *
+ * + +
+ * | v |
+ * q | f c o | f c
+ * u | c l | c
+ * o | f t | f
+ * t | c a | c
+ * | c f g | c f
+ * | e |
+ * +--------------- +----------------
+ * 0 1 2 3 4 5 6 0 1 2 3 4 5 6
+ * corner corner
+ *
+ * c = corner
+ * f = fuse corner
+ *
+ */
+ for (i = 0; corner <= end; corner++, i++) {
+ int prev_quot;
+ u64 freq_diff_mhz;
+
+ fnum = cdata[i].fuse_corner;
+ fdata = &tdesc->fuse_corner_data[fnum];
+ quot_offset = fuses[fnum].quotient_offset;
+ fuse = &thread->fuse_corners[fnum];
+ ring_osc_mask &= (u16)(~BIT(fuse->ring_osc_idx));
+ if (fnum)
+ prev_fuse = &thread->fuse_corners[fnum - 1];
+ else
+ prev_fuse = NULL;
+
+ corner->fuse_corner = fuse;
+ corner->freq = cdata[i].freq;
+ corner->uV = fuse->uV;
+
+ if (prev_fuse) {
+ if (prev_fuse->ring_osc_idx == fuse->ring_osc_idx)
+ quot_offset = NULL;
+
+ scaling = cpr_calculate_scaling(drv->dev, quot_offset,
+ fdata, corner);
+ if (scaling < 0)
+ return scaling;
+
+ freq_diff_mhz = fuse->max_freq - corner->freq;
+ do_div(freq_diff_mhz, 1000000); /* now in MHz */
+
+ corner->quot_adjust = scaling * freq_diff_mhz;
+ do_div(corner->quot_adjust, 1000);
+
+ /*
+ * Make sure that we scale (up) monotonically.
+ * P.S.: Fuse quots can never be descending.
+ */
+ prev_quot = prev_corner->fuse_corner->quot;
+ prev_quot -= prev_corner->quot_adjust;
+ if (fuse->quot - corner->quot_adjust < prev_quot) {
+ int new_adj = prev_corner->fuse_corner->quot;
+
+ new_adj -= fuse->quot;
+ dev_vdbg(drv->dev, "Monotonic increase forced: %d->%d\n",
+ corner->quot_adjust, new_adj);
+ corner->quot_adjust = new_adj;
+ }
+
+ corner->uV = cpr_interpolate(corner,
+ drv->vreg_step, fdata);
+ }
+ /* Negative fuse quotients are nonsense. */
+ if (fuse->quot < corner->quot_adjust)
+ return -EINVAL;
+
+ min_quotient = min(min_quotient,
+ (u32)(fuse->quot - corner->quot_adjust));
+
+ corner->max_uV = fuse->max_uV;
+ if (desc->corner_max_uV)
+ corner->max_uV = min_t(int, corner->max_uV,
+ desc->corner_max_uV[i]);
+ corner->min_uV = fuse->min_uV;
+ corner->uV = clamp(corner->uV, corner->min_uV, corner->max_uV);
+ dev_vdbg(drv->dev, "Clamped after interpolation: [%d %d %d]\n",
+ corner->min_uV, corner->uV, corner->max_uV);
+
+ /* Make sure that we scale monotonically here, too. */
+ if (corner->uV < prev_corner->uV)
+ corner->uV = prev_corner->uV;
+
+ corner->last_uV = corner->uV;
+
+ corner->min_uV = max(corner->max_uV - fdata->range_uV,
+ corner->min_uV);
+
+ /*
+ * Keep each corner on one side of the array power mux
+ * voltage threshold.
+ */
+ cpr4_restrict_corner(corner, desc->apm_threshold, 0,
+ drv->vreg_step);
+ if (corner->min_uV > corner->max_uV)
+ return -EINVAL;
+ corner->uV = clamp(corner->uV, corner->min_uV,
+ corner->max_uV);
+ corner->last_uV = corner->uV;
+
+ prev_corner = corner;
+ dev_dbg(drv->dev, "corner %d: [%d %d %d] scaling %d quot %d\n", i,
+ corner->min_uV, corner->uV, corner->max_uV, scaling,
+ fuse->quot - corner->quot_adjust);
+ }
+
+ return 0;
+}
+
+/**
+ * cpr4_init_parameters() - Initialize CPR global parameters
+ * @drv: Main driver structure
+ *
+ * Initial "integrity" checks and setup for the thread-independent parameters.
+ *
+ * Return: Zero for success, negative number on error
+ */
+static int cpr4_init_parameters(struct cpr_drv *drv)
+{
+ const struct cpr_desc *desc = drv->desc;
+ struct clk *clk;
+
+ clk = devm_clk_get(drv->dev, NULL);
+ if (IS_ERR(clk)) {
+ dev_err(drv->dev, "Couldn't get the reference clock: %ld\n", PTR_ERR(clk));
+ return PTR_ERR(clk);
+ }
+
+ drv->ref_clk_khz = clk_get_rate(clk);
+ do_div(drv->ref_clk_khz, 1000);
+
+ devm_clk_put(drv->dev, clk);
+
+ if (desc->timer_cons_up > CPR4_THRESH_CONS_UP_MASK ||
+ desc->timer_cons_down > CPR4_THRESH_CONS_DOWN_MASK ||
+ desc->up_threshold > CPR4_THRESH_UP_THRESH_MASK ||
+ desc->down_threshold > CPR4_THRESH_DOWN_THRESH_MASK ||
+ desc->idle_clocks > CPR4_CPR_CTL_IDLE_CLOCKS_MASK ||
+ desc->count_mode > CPR4_CPR_CTL_COUNT_MODE_MASK ||
+ desc->count_repeat > CPR4_CPR_CTL_COUNT_REPEAT_MASK)
+ return -EINVAL;
+
+ dev_dbg(drv->dev, "up threshold = %u, down threshold = %u\n",
+ desc->up_threshold, desc->down_threshold);
+
+ return 0;
+}
+
+/**
+ * cpr4_find_initial_corner() - Finds boot-up p-state and enables CPR
+ * @thread: Structure holding CPR thread-specific parameters
+ *
+ * Differently from CPRv1, from CPRv3 onwards when we successfully find
+ * the target boot-up performance state, we must refresh the HW
+ * immediately to guarantee system stability and to avoid overheating
+ * during the boot process, thing that would more likely happen without
+ * this driver doing its job.
+ *
+ * Return: Zero for success, negative number on error
+ */
+static int cpr4_find_initial_corner(struct cpr_thread *thread)
+{
+ struct cpr_drv *drv = thread->drv;
+ struct corner *corner;
+ int uV, idx, ret;
+
+ idx = cpr_find_initial_corner(drv->dev, thread->cpu_clk,
+ thread->corners,
+ thread->num_corners);
+ if (idx < 0)
+ return idx;
+
+ cpr_ctl_disable(thread);
+
+ corner = &thread->corners[idx];
+ cpr_corner_restore(thread, corner);
+
+ uV = regulator_get_voltage(drv->vreg);
+ if (uV < 0)
+ return uV;
+
+ corner->last_uV = clamp(uV, corner->min_uV, corner->max_uV);
+ drv->last_uV = uV;
+
+ ret = cpr_commit_state(thread);
+ if (ret)
+ return ret;
+ thread->enabled = true;
+ ret = cpr_switch(drv);
+ if (ret)
+ thread->enabled = false;
+
+ return ret;
+}
+
+static int cpr_power_off(struct generic_pm_domain *domain)
+{
+ struct cpr_thread *thread = container_of(domain, struct cpr_thread, pd);
+
+ return cpr_disable(thread);
+}
+
+static int cpr_power_on(struct generic_pm_domain *domain)
+{
+ struct cpr_thread *thread = container_of(domain, struct cpr_thread, pd);
+
+ return cpr_enable(thread);
+}
+
+static void cpr_pd_detach_dev(struct generic_pm_domain *domain,
+ struct device *dev)
+{
+ struct cpr_thread *thread = container_of(domain, struct cpr_thread, pd);
+ struct cpr_drv *drv = thread->drv;
+
+ mutex_lock(&drv->lock);
+
+ dev_dbg(drv->dev, "detach callback for: %s\n", dev_name(dev));
+ if (thread->attached_cpu_dev == dev)
+ thread->attached_cpu_dev = NULL;
+
+ mutex_unlock(&drv->lock);
+}
+
+static int cpr_pd_attach_dev(struct generic_pm_domain *domain,
+ struct device *dev)
+{
+ struct cpr_thread *thread = container_of(domain, struct cpr_thread, pd);
+ struct cpr_drv *drv = thread->drv;
+ int ret = 0;
+
+ mutex_lock(&drv->lock);
+
+ dev_dbg(drv->dev, "attach callback for: %s\n", dev_name(dev));
+
+ /*
+ * This driver only supports scaling voltage for a CPU cluster
+ * where all CPUs in the cluster share a single regulator.
+ * Therefore, save the struct device pointer only for the first
+ * CPU device that gets attached. There is no need to do any
+ * additional initialization when further CPUs get attached.
+ * This is not an error condition.
+ */
+ if (thread->attached_cpu_dev)
+ goto unlock;
+
+ /*
+ * cpr_scale_voltage() requires the direction (if we are changing
+ * to a higher or lower OPP). The first time
+ * cpr_set_performance_state() is called, there is no previous
+ * performance state defined. Therefore, we call
+ * cpr_find_initial_corner() that gets the CPU clock frequency
+ * set by the bootloader, so that we can determine the direction
+ * the first time cpr_set_performance_state() is called.
+ */
+ thread->cpu_clk = devm_clk_get(dev, NULL);
+ if (IS_ERR(thread->cpu_clk)) {
+ ret = PTR_ERR(thread->cpu_clk);
+ if (ret != -EPROBE_DEFER)
+ dev_err(drv->dev, "could not get cpu clk: %d\n", ret);
+ goto unlock;
+ }
+ thread->attached_cpu_dev = dev;
+
+ dev_dbg(drv->dev, "using cpu clk from: %s\n",
+ dev_name(thread->attached_cpu_dev));
+
+ /*
+ * Everything related to (virtual) corners has to be initialized
+ * here, when attaching to the power domain, since we need to know
+ * the maximum frequency for each fuse corner, and this is only
+ * available after the cpufreq driver has attached to us.
+ * The reason for this is that we need to know the highest
+ * frequency associated with each fuse corner.
+ */
+ ret = dev_pm_opp_get_opp_count(&thread->pd.dev);
+ if (ret < 0) {
+ dev_err(drv->dev, "could not get OPP count\n");
+ thread->attached_cpu_dev = NULL;
+ goto unlock;
+ }
+ thread->num_corners = ret;
+
+ thread->corners = devm_kcalloc(drv->dev,
+ thread->num_corners,
+ sizeof(*thread->corners),
+ GFP_KERNEL);
+ if (!thread->corners) {
+ ret = -ENOMEM;
+ goto unlock;
+ }
+
+ ret = cpr4_corner_init(thread);
+ if (ret)
+ goto exit;
+
+ ret = cpr4_find_initial_corner(thread);
+ if (ret)
+ goto exit;
+
+ dev_info(drv->dev, "thread %d initialized with %u OPPs\n",
+ thread->id, thread->num_corners);
+exit:
+ if (ret)
+ thread->attached_cpu_dev = NULL;
+unlock:
+ mutex_unlock(&drv->lock);
+
+ return ret;
+}
+
+static int cpr4_debug_info_show(struct seq_file *s, void *unused)
+{
+ struct cpr_thread *thread = s->private;
+ struct fuse_corner *fuse = thread->fuse_corners;
+ struct corner *corner = thread->corners;
+ u32 tid = thread->desc->hw_tid;
+ u32 ctl, irq_status, reg;
+ unsigned int i;
+
+ seq_printf(s, "current_volt = %d uV\n", thread->drv->last_uV);
+ seq_printf(s, "requested voltage: %d uV\n", thread->corner->last_uV);
+
+ irq_status = cpr_read(thread, CPR4_REG_IRQ_STATUS);
+ seq_printf(s, "irq_status = 0x%x\n", irq_status);
+
+ ctl = cpr_read(thread, CPR4_REG_CPR_CTL);
+ seq_printf(s, "cpr_ctl = 0x%x\n", ctl);
+
+ seq_printf(s, "thread %d - hw tid: %u - enabled: %d:\n",
+ thread->id, thread->desc->hw_tid, thread->enabled);
+ seq_printf(s, "%d corners, derived from %d fuse corners\n",
+ thread->num_corners, thread->desc->num_fuse_corners);
+
+
+ /* The corners have not been initialized yet. */
+ if (!thread->num_corners)
+ return 0;
+
+ for (i = 0; i < thread->num_corners; i++, corner++)
+ seq_printf(s, "corner %d - uV=[%d %d %d] quot=%d freq=%llu\n",
+ i, corner->min_uV, corner->uV, corner->max_uV,
+ corner->quot_adjust, corner->freq);
+
+ for (i = 0; i < thread->desc->num_fuse_corners; i++, fuse++)
+ seq_printf(s, "fuse %d - uV=[%d %d %d] quot=%d freq=%llu\n",
+ i, fuse->min_uV, fuse->uV, fuse->max_uV,
+ fuse->quot, corner->freq);
+
+ reg = cpr_read(thread, CPR4_REG_RESULT0(tid));
+ seq_printf(s, "cpr_result_0 = 0x%x\n [", reg);
+ seq_printf(s, "busy = %lu, ", FIELD_GET(CPR4_RESULT0_BUSY_MASK, reg));
+ seq_printf(s, "step_dn = %lu, ", FIELD_GET(CPR4_RESULT0_STEP_DN_MASK, reg));
+ seq_printf(s, "step_up = %lu, ", FIELD_GET(CPR4_RESULT0_STEP_UP_MASK, reg));
+ seq_printf(s, "error_steps = %lu, ", FIELD_GET(CPR4_RESULT0_ERROR_STEPS_MASK, reg));
+ seq_printf(s, "error = %lu, ", FIELD_GET(CPR4_RESULT0_ERROR_MASK, reg));
+ seq_printf(s, "negative = %lu", FIELD_GET(CPR4_RESULT0_NEG_MASK, reg));
+ seq_puts(s, "]\n");
+
+ reg = cpr_read(thread, CPR4_REG_RESULT1(tid));
+ seq_printf(s, "cpr_result_1 = 0x%x\n [", reg);
+ seq_printf(s, "quot_min = %lu, ", FIELD_GET(CPR4_RESULT1_QUOT_MIN_MASK, reg));
+ seq_printf(s, "quot_max = %lu, ", FIELD_GET(CPR4_RESULT1_QUOT_MAX_MASK, reg));
+ seq_printf(s, "ro_min = %lu, ", FIELD_GET(CPR4_RESULT1_RO_MIN_MASK, reg));
+ seq_printf(s, "ro_max = %lu", FIELD_GET(CPR4_RESULT1_RO_MAX_MASK, reg));
+ seq_puts(s, "]\n");
+
+ reg = cpr_read(thread, CPR4_REG_RESULT2(tid));
+ seq_printf(s, "cpr_result_2 = 0x%x\n [", reg);
+ seq_printf(s, "qout_step_min = %lu, ", FIELD_GET(CPR4_RESULT2_STEP_QUOT_MIN_MASK, reg));
+ seq_printf(s, "qout_step_max = %lu, ", FIELD_GET(CPR4_RESULT2_STEP_QUOT_MAX_MASK, reg));
+ seq_printf(s, "sensor_min = %lu, ", FIELD_GET(CPR4_RESULT2_SENSOR_MIN_MASK, reg));
+ seq_printf(s, "sensor_max = %lu", FIELD_GET(CPR4_RESULT2_SENSOR_MAX_MASK, reg));
+ seq_puts(s, "]\n");
+
+ return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(cpr4_debug_info);
+
+static void cpr4_debugfs_init(struct cpr_drv *drv)
+{
+ int i;
+
+ drv->debugfs = debugfs_create_dir("qcom_cpr4", NULL);
+
+ for (i = 0; i < drv->desc->num_threads; i++) {
+ char buf[50];
+
+ snprintf(buf, sizeof(buf), "thread%d", i);
+
+ debugfs_create_file(buf, 0444, drv->debugfs, &drv->threads[i],
+ &cpr4_debug_info_fops);
+ }
+}
+
+/**
+ * cpr_thread_init() - Initialize CPR thread related parameters
+ * @drv: Main driver structure
+ * @tid: Thread ID
+ *
+ * Return: Zero for success, negative number on error
+ */
+static int cpr_thread_init(struct cpr_drv *drv, int tid)
+{
+ const struct cpr_desc *desc = drv->desc;
+ const struct cpr_thread_desc *tdesc = desc->threads[tid];
+ struct cpr_thread *thread = &drv->threads[tid];
+ bool pd_registered = false;
+ int ret, i;
+
+ if (tdesc->step_quot_init_min > CPR4_CPR_STEP_QUOT_MIN_MASK ||
+ tdesc->step_quot_init_max > CPR4_CPR_STEP_QUOT_MAX_MASK)
+ return -EINVAL;
+
+ thread->id = tid;
+ thread->drv = drv;
+ thread->desc = tdesc;
+ thread->fuse_corners = devm_kcalloc(drv->dev,
+ tdesc->num_fuse_corners,
+ sizeof(*thread->fuse_corners),
+ GFP_KERNEL);
+ if (!thread->fuse_corners)
+ return -ENOMEM;
+
+ thread->cpr_fuses = cpr_get_fuses(drv->dev, tid,
+ tdesc->num_fuse_corners);
+ if (IS_ERR(thread->cpr_fuses))
+ return PTR_ERR(thread->cpr_fuses);
+
+ ret = cpr_populate_ring_osc_idx(thread->drv->dev, thread->fuse_corners,
+ thread->cpr_fuses,
+ tdesc->num_fuse_corners);
+ if (ret)
+ return ret;
+
+ ret = cpr_fuse_corner_init(thread);
+ if (ret)
+ return ret;
+
+ thread->pd.name = devm_kasprintf(drv->dev, GFP_KERNEL,
+ "%s_thread%d",
+ drv->dev->of_node->full_name,
+ thread->id);
+ if (!thread->pd.name)
+ return -EINVAL;
+
+ thread->pd.power_off = cpr_power_off;
+ thread->pd.power_on = cpr_power_on;
+ thread->pd.attach_dev = cpr_pd_attach_dev;
+ thread->pd.detach_dev = cpr_pd_detach_dev;
+
+ thread->pd.set_performance_state = cpr_set_performance_state;
+
+ /* Keep the CPU supply active while the CPUs are running. */
+ thread->pd.flags = GENPD_FLAG_ALWAYS_ON;
+
+ drv->cell_data.domains[tid] = &thread->pd;
+
+ ret = pm_genpd_init(&thread->pd, NULL, false);
+ if (ret < 0)
+ goto fail;
+ else
+ pd_registered = true;
+
+ INIT_WORK(&thread->restart_work, cpr_restart_worker);
+
+ ret = devm_request_threaded_irq(drv->dev, drv->irq,
+ NULL, cpr_irq_handler,
+ IRQF_ONESHOT |
+ IRQF_TRIGGER_RISING,
+ "cpr", thread);
+ if (ret)
+ goto fail;
+
+ return 0;
+
+fail:
+ /* Unregister all previously registered genpds */
+ for (i = tid - pd_registered; i >= 0; i--)
+ pm_genpd_remove(&drv->threads[i].pd);
+
+ return ret;
+}
+
+/**
+ * cpr4_resources_init() - Initialize resources used by this driver
+ * @pdev: Platform device
+ * @drv: Main driver structure
+ *
+ * Return: Zero for success, negative number on error
+ */
+static int cpr4_resources_init(struct platform_device *pdev,
+ struct cpr_drv *drv)
+{
+ const struct cpr_desc *desc = drv->desc;
+ struct cpr_thread *threads = drv->threads;
+ unsigned int i;
+ u8 cid_mask = 0;
+
+ /*
+ * Here, we are accounting for the following usecases:
+ * - One controller
+ * - One or multiple threads on the same iospace
+ *
+ * - Multiple controllers
+ * - Each controller has its own iospace and each
+ * may have one or multiple threads in their
+ * parent controller's iospace
+ *
+ * Then, to avoid complicating the code for no reason,
+ * this also needs a mandatory order in the list of
+ * threads which implies that all of them from the same
+ * controllers are specified sequentially. As an example:
+ *
+ * C0-T0, C0-T1...C0-Tn, C1-T0, C1-T1...C1-Tn
+ */
+ for (i = 0; i < desc->num_threads; i++) {
+ u8 cid = desc->threads[i]->controller_id;
+
+ if (cid_mask & BIT(cid)) {
+ if (desc->threads[i - 1]->controller_id != cid) {
+ dev_err(drv->dev, "Bad threads order. Please fix!\n");
+ return -EINVAL;
+ }
+ threads[i].base = threads[i - 1].base;
+ continue;
+ }
+ threads[i].base = devm_platform_ioremap_resource(pdev, cid);
+ if (IS_ERR(threads[i].base))
+ return PTR_ERR(threads[i].base);
+ cid_mask |= BIT(cid);
+ }
+ return 0;
+}
+
+static int cpr_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct platform_device *apcs_pdev;
+ struct device_link *link;
+ struct cpr_drv *drv;
+ const struct cpr_desc *desc;
+ struct device_node *np;
+ unsigned int i;
+ int ret;
+
+ desc = of_device_get_match_data(dev);
+ if (!desc)
+ return -EINVAL;
+
+ drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
+ if (!drv)
+ return -ENOMEM;
+
+ drv->dev = dev;
+ drv->desc = desc;
+ drv->supported_hw = ~0U;
+ if (desc->get_supported_hw) {
+ ret = desc->get_supported_hw(drv);
+ if (ret)
+ return ret;
+ }
+
+ drv->threads = devm_kcalloc(dev, desc->num_threads,
+ sizeof(*drv->threads), GFP_KERNEL);
+ if (!drv->threads)
+ return -ENOMEM;
+
+ drv->cell_data.num_domains = desc->num_threads;
+ drv->cell_data.domains = devm_kcalloc(drv->dev,
+ drv->cell_data.num_domains,
+ sizeof(*drv->cell_data.domains),
+ GFP_KERNEL);
+ if (!drv->cell_data.domains)
+ return -ENOMEM;
+
+ mutex_init(&drv->lock);
+
+ ret = cpr4_resources_init(pdev, drv);
+ if (ret)
+ return ret;
+
+ drv->irq = platform_get_irq_optional(pdev, 0);
+ if (drv->irq < 0)
+ return drv->irq;
+
+ drv->vreg = devm_regulator_get_optional(dev, "vdd");
+ if (IS_ERR(drv->vreg))
+ return PTR_ERR(drv->vreg);
+
+ if (desc->apm_threshold) {
+ np = of_parse_phandle(dev->of_node, "qcom,apm-ctrl", 0);
+ if (!np)
+ return -EINVAL;
+
+ apcs_pdev = of_find_device_by_node(np);
+ of_node_put(np);
+ if (!apcs_pdev)
+ return -EPROBE_DEFER;
+
+ drv->apm = dev_get_regmap(&apcs_pdev->dev, NULL);
+ if (!drv->apm) {
+ put_device(&apcs_pdev->dev);
+ return -EPROBE_DEFER;
+ }
+
+ link = device_link_add(dev, &apcs_pdev->dev,
+ DL_FLAG_AUTOREMOVE_CONSUMER);
+ put_device(&apcs_pdev->dev);
+ if (!link)
+ return -ENOMEM;
+
+ if (!desc->apm_switch)
+ return -EINVAL;
+
+ if (desc->apm_init) {
+ ret = desc->apm_init(drv);
+ if (ret)
+ return ret;
+ }
+ }
+
+ /* The APSS PMIC uses the fixed CPR step from the descriptor. */
+ if (desc->vreg_step_fixed)
+ drv->vreg_step = desc->vreg_step_fixed;
+ else
+ drv->vreg_step = regulator_get_linear_step(drv->vreg);
+
+ if (!drv->vreg_step)
+ return -EINVAL;
+
+ /* Initialize all threads */
+ for (i = 0; i < desc->num_threads; i++) {
+ ret = cpr_thread_init(drv, i);
+ if (ret)
+ return ret;
+ }
+
+ /* Initialize global parameters */
+ ret = cpr4_init_parameters(drv);
+ if (ret)
+ goto unreg_genpd;
+
+ /* Write initial configuration on all threads */
+ for (i = 0; i < desc->num_threads; i++) {
+ ret = cpr_configure(&drv->threads[i]);
+ if (ret)
+ goto unreg_genpd;
+ }
+
+ ret = of_genpd_add_provider_onecell(dev->of_node, &drv->cell_data);
+ if (ret)
+ goto unreg_genpd;
+
+ platform_set_drvdata(pdev, drv);
+ cpr4_debugfs_init(drv);
+
+ return 0;
+
+unreg_genpd:
+ /* Clean up genpds */
+ for (i = desc->num_threads - 1; i >= 0; i--)
+ pm_genpd_remove(&drv->threads[i].pd);
+
+ return ret;
+}
+
+static void cpr_remove(struct platform_device *pdev)
+{
+ struct cpr_drv *drv = platform_get_drvdata(pdev);
+ int i;
+
+ of_genpd_del_provider(pdev->dev.of_node);
+
+ for (i = 0; i < drv->desc->num_threads; i++) {
+ cpr_ctl_disable(&drv->threads[i]);
+ cpr_irq_set(&drv->threads[i], 0);
+ pm_genpd_remove(&drv->threads[i].pd);
+ }
+
+ debugfs_remove_recursive(drv->debugfs);
+}
+
+static const struct of_device_id cpr4_match_table[] = {
+ { }
+};
+MODULE_DEVICE_TABLE(of, cpr4_match_table);
+
+static struct platform_driver cpr4_driver = {
+ .probe = cpr_probe,
+ .remove = cpr_remove,
+ .driver = {
+ .name = "qcom-cpr4",
+ .of_match_table = cpr4_match_table,
+ },
+};
+module_platform_driver(cpr4_driver)
+
+MODULE_DESCRIPTION("Qualcomm CPR4 power domain driver");
+MODULE_LICENSE("GPL");
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread