[PATCH v3 5/5] i2c: qcom-cci: Enforce the required CCI clock rate
From: Loic Poulain
Date: Wed Jul 29 2026 - 08:44:17 EST
The CCI hw_params timing values are only valid at the specific clock
rate they were calibrated for. A previous change made the driver select
the timing set matching the currently running clock rate, but the rate
itself was still left to the DT (assigned-clock-rates) or the bootloader,
which is fragile: if no rate is enforced the timings may not match and
violate the I2C specification.
Actively drive the CCI clock to the rate required by the configured
modes. The single CCI clock is shared by all masters, which may run in
different modes, so cci_get_required_rate() picks the lowest rate that
has a valid timing set for every active master's mode. This avoids
clocking the bus faster than necessary while still satisfying every
master (e.g. a Fast+ master forces 37.5 MHz).
Apply the rate through the OPP framework so that boards describing an
opp table also get the required power-domain/regulator votes for that rate.
Boards without an OPP table simply fall back to plain clk_set_rate()
behavior, so existing DTs keep working.
Suggested-by: Konrad Dybcio <konrad.dybcio@xxxxxxxxxxxxxxxx>
Tested-by: Wenmeng Liu <wenmeng.liu@xxxxxxxxxxxxxxxx>
Signed-off-by: Loic Poulain <loic.poulain@xxxxxxxxxxxxxxxx>
---
drivers/i2c/busses/i2c-qcom-cci.c | 75 +++++++++++++++++++++++++++++++++++++++
1 file changed, 75 insertions(+)
diff --git a/drivers/i2c/busses/i2c-qcom-cci.c b/drivers/i2c/busses/i2c-qcom-cci.c
index 8e4641de049e56da5afbddbab122946bcdd4f60d..fbe3fef1b49fb3d0f1b77ed9677f2d5e0eef4df5 100644
--- a/drivers/i2c/busses/i2c-qcom-cci.c
+++ b/drivers/i2c/busses/i2c-qcom-cci.c
@@ -11,6 +11,7 @@
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
+#include <linux/pm_opp.h>
#define CCI_HW_VERSION 0x0
#define CCI_RESET_CMD 0x004
@@ -575,10 +576,67 @@ static void cci_disable_clocks(struct cci *cci)
clk_bulk_disable_unprepare(cci->nclocks, cci->clocks);
}
+/*
+ * The single CCI clock is shared by all masters, which may run in different
+ * modes. Pick the lowest rate that has a valid timing set for every active
+ * master's mode.
+ */
+static unsigned long cci_get_required_rate(struct cci *cci)
+{
+ int ri, i;
+
+ for (ri = 0; ri < NUM_CCI_CLK_RATES; ri++) {
+ bool supported = true;
+
+ for (i = 0; i < cci->data->num_masters; i++) {
+ int mode = cci->master[i].mode;
+
+ if (!cci->master[i].cci)
+ continue;
+
+ if (mode > cci->data->max_mode ||
+ !cci_hw_params[ri][mode].thigh) {
+ supported = false;
+ break;
+ }
+ }
+
+ if (supported)
+ return cci_clk_rates[ri];
+ }
+
+ return 0;
+}
+
+static int cci_set_core_rate(struct cci *cci, unsigned long rate)
+{
+ struct device *dev = cci->dev;
+ int ret;
+
+ ret = dev_pm_opp_set_rate(dev, rate);
+ if (ret) {
+ dev_warn(dev, "CCI clock could not be set to %lu Hz\n", rate);
+ return ret;
+ }
+
+ if (!rate)
+ return 0;
+
+ /*
+ * Sanity: The hw_params timings are only valid at the exact
+ * expected rate, verify what landed on the hardware.
+ */
+ if (clk_get_rate(cci->cci_clk) != rate)
+ dev_warn(dev, "CCI clock is not at expected %lu Hz\n", rate);
+
+ return 0;
+}
+
static int __maybe_unused cci_suspend_runtime(struct device *dev)
{
struct cci *cci = dev_get_drvdata(dev);
+ cci_set_core_rate(cci, 0);
cci_disable_clocks(cci);
return 0;
}
@@ -588,6 +646,10 @@ static int __maybe_unused cci_resume_runtime(struct device *dev)
struct cci *cci = dev_get_drvdata(dev);
int ret;
+ ret = cci_set_core_rate(cci, cci_get_required_rate(cci));
+ if (ret)
+ return ret;
+
ret = cci_enable_clocks(cci);
if (ret)
return ret;
@@ -694,6 +756,19 @@ static int cci_probe(struct platform_device *pdev)
return dev_err_probe(dev, PTR_ERR(cci->cci_clk),
"failed to get CCI clock\n");
+ ret = devm_pm_opp_set_clkname(dev, "cci");
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to set CCI OPP clk\n");
+
+ /* OPP table is optional */
+ ret = devm_pm_opp_of_add_table(dev);
+ if (ret && ret != -ENODEV)
+ return dev_err_probe(dev, ret, "failed to add OPP table\n");
+
+ ret = cci_set_core_rate(cci, cci_get_required_rate(cci));
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to set CCI clock rate\n");
+
ret = cci_enable_clocks(cci);
if (ret < 0)
return ret;
--
2.34.1