1 From f81715a4c87c3b75ca2640bb61b6c66506061a64 Mon Sep 17 00:00:00 2001
2 From: Luo Jie <quic_luoj@quicinc.com>
3 Date: Fri, 3 Jan 2025 15:31:35 +0800
4 Subject: [PATCH] clk: qcom: Add CMN PLL clock controller driver for IPQ SoC
6 The CMN PLL clock controller supplies clocks to the hardware
7 blocks that together make up the Ethernet function on Qualcomm
8 IPQ SoCs and to GCC. The driver is initially supported for
11 The CMN PLL clock controller expects a reference input clock
12 from the on-board Wi-Fi block acting as clock source. The input
13 reference clock needs to be configured to one of the supported
16 The controller supplies a number of fixed-rate output clocks.
17 For the IPQ9574, there is one output clock of 353 MHZ to PPE
18 (Packet Process Engine) hardware block, three 50 MHZ output
19 clocks and an additional 25 MHZ output clock supplied to the
20 connected Ethernet devices. The PLL also supplies a 24 MHZ
21 clock as XO and a 32 KHZ sleep clock to GCC, and one 31.25
24 Signed-off-by: Luo Jie <quic_luoj@quicinc.com>
25 Acked-by: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
26 Link: https://lore.kernel.org/r/20250103-qcom_ipq_cmnpll-v8-2-c89fb4d4849d@quicinc.com
27 Signed-off-by: Bjorn Andersson <andersson@kernel.org>
29 drivers/clk/qcom/Kconfig | 9 +
30 drivers/clk/qcom/Makefile | 1 +
31 drivers/clk/qcom/ipq-cmn-pll.c | 435 +++++++++++++++++++++++++++++++++
32 3 files changed, 445 insertions(+)
33 create mode 100644 drivers/clk/qcom/ipq-cmn-pll.c
35 diff --git a/drivers/clk/qcom/Kconfig b/drivers/clk/qcom/Kconfig
36 index 42c257e4c433..2daff198aeb3 100644
37 --- a/drivers/clk/qcom/Kconfig
38 +++ b/drivers/clk/qcom/Kconfig
39 @@ -199,6 +199,15 @@ config IPQ_APSS_6018
40 Say Y if you want to support CPU frequency scaling on
44 + tristate "IPQ CMN PLL Clock Controller"
46 + Support for CMN PLL clock controller on IPQ platform. The
47 + CMN PLL consumes the AHB/SYS clocks from GCC and supplies
48 + the output clocks to the networking hardware and GCC blocks.
49 + Say Y or M if you want to support CMN PLL clock on the IPQ
53 tristate "IPQ4019 Global Clock Controller"
55 diff --git a/drivers/clk/qcom/Makefile b/drivers/clk/qcom/Makefile
56 index 1b749da9c13a..6665049cb8c8 100644
57 --- a/drivers/clk/qcom/Makefile
58 +++ b/drivers/clk/qcom/Makefile
59 @@ -30,6 +30,7 @@ obj-$(CONFIG_CLK_X1P42100_GPUCC) += gpucc-x1p42100.o
60 obj-$(CONFIG_CLK_QCM2290_GPUCC) += gpucc-qcm2290.o
61 obj-$(CONFIG_IPQ_APSS_PLL) += apss-ipq-pll.o
62 obj-$(CONFIG_IPQ_APSS_6018) += apss-ipq6018.o
63 +obj-$(CONFIG_IPQ_CMN_PLL) += ipq-cmn-pll.o
64 obj-$(CONFIG_IPQ_GCC_4019) += gcc-ipq4019.o
65 obj-$(CONFIG_IPQ_GCC_5018) += gcc-ipq5018.o
66 obj-$(CONFIG_IPQ_GCC_5332) += gcc-ipq5332.o
67 diff --git a/drivers/clk/qcom/ipq-cmn-pll.c b/drivers/clk/qcom/ipq-cmn-pll.c
69 index 000000000000..432d4c4b7aa6
71 +++ b/drivers/clk/qcom/ipq-cmn-pll.c
73 +// SPDX-License-Identifier: GPL-2.0-only
75 + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
79 + * CMN PLL block expects the reference clock from on-board Wi-Fi block,
80 + * and supplies fixed rate clocks as output to the networking hardware
81 + * blocks and to GCC. The networking related blocks include PPE (packet
82 + * process engine), the externally connected PHY or switch devices, and
85 + * On the IPQ9574 SoC, there are three clocks with 50 MHZ and one clock
86 + * with 25 MHZ which are output from the CMN PLL to Ethernet PHY (or switch),
87 + * and one clock with 353 MHZ to PPE. The other fixed rate output clocks
88 + * are supplied to GCC (24 MHZ as XO and 32 KHZ as sleep clock), and to PCS
96 + * +-------+---+------+
97 + * | +-------------> eth0-50mhz
98 + * REF CLK | IPQ9574 |
99 + * -------->+ +-------------> eth1-50mhz
100 + * | CMN PLL block |
101 + * | +-------------> eth2-50mhz
103 + * +----+----+----+---+-------------> eth-25mhz
109 +#include <linux/bitfield.h>
110 +#include <linux/clk-provider.h>
111 +#include <linux/delay.h>
112 +#include <linux/err.h>
113 +#include <linux/mod_devicetable.h>
114 +#include <linux/module.h>
115 +#include <linux/platform_device.h>
116 +#include <linux/pm_clock.h>
117 +#include <linux/pm_runtime.h>
118 +#include <linux/regmap.h>
120 +#include <dt-bindings/clock/qcom,ipq-cmn-pll.h>
122 +#define CMN_PLL_REFCLK_SRC_SELECTION 0x28
123 +#define CMN_PLL_REFCLK_SRC_DIV GENMASK(9, 8)
125 +#define CMN_PLL_LOCKED 0x64
126 +#define CMN_PLL_CLKS_LOCKED BIT(8)
128 +#define CMN_PLL_POWER_ON_AND_RESET 0x780
129 +#define CMN_ANA_EN_SW_RSTN BIT(6)
131 +#define CMN_PLL_REFCLK_CONFIG 0x784
132 +#define CMN_PLL_REFCLK_EXTERNAL BIT(9)
133 +#define CMN_PLL_REFCLK_DIV GENMASK(8, 4)
134 +#define CMN_PLL_REFCLK_INDEX GENMASK(3, 0)
136 +#define CMN_PLL_CTRL 0x78c
137 +#define CMN_PLL_CTRL_LOCK_DETECT_EN BIT(15)
139 +#define CMN_PLL_DIVIDER_CTRL 0x794
140 +#define CMN_PLL_DIVIDER_CTRL_FACTOR GENMASK(9, 0)
143 + * struct cmn_pll_fixed_output_clk - CMN PLL output clocks information
144 + * @id: Clock specifier to be supplied
145 + * @name: Clock name to be registered
146 + * @rate: Clock rate
148 +struct cmn_pll_fixed_output_clk {
151 + unsigned long rate;
155 + * struct clk_cmn_pll - CMN PLL hardware specific data
156 + * @regmap: hardware regmap.
157 + * @hw: handle between common and hardware-specific interfaces
159 +struct clk_cmn_pll {
160 + struct regmap *regmap;
164 +#define CLK_PLL_OUTPUT(_id, _name, _rate) { \
170 +#define to_clk_cmn_pll(_hw) container_of(_hw, struct clk_cmn_pll, hw)
172 +static const struct regmap_config ipq_cmn_pll_regmap_config = {
176 + .max_register = 0x7fc,
180 +static const struct cmn_pll_fixed_output_clk ipq9574_output_clks[] = {
181 + CLK_PLL_OUTPUT(XO_24MHZ_CLK, "xo-24mhz", 24000000UL),
182 + CLK_PLL_OUTPUT(SLEEP_32KHZ_CLK, "sleep-32khz", 32000UL),
183 + CLK_PLL_OUTPUT(PCS_31P25MHZ_CLK, "pcs-31p25mhz", 31250000UL),
184 + CLK_PLL_OUTPUT(NSS_1200MHZ_CLK, "nss-1200mhz", 1200000000UL),
185 + CLK_PLL_OUTPUT(PPE_353MHZ_CLK, "ppe-353mhz", 353000000UL),
186 + CLK_PLL_OUTPUT(ETH0_50MHZ_CLK, "eth0-50mhz", 50000000UL),
187 + CLK_PLL_OUTPUT(ETH1_50MHZ_CLK, "eth1-50mhz", 50000000UL),
188 + CLK_PLL_OUTPUT(ETH2_50MHZ_CLK, "eth2-50mhz", 50000000UL),
189 + CLK_PLL_OUTPUT(ETH_25MHZ_CLK, "eth-25mhz", 25000000UL),
193 + * CMN PLL has the single parent clock, which supports the several
194 + * possible parent clock rates, each parent clock rate is reflected
195 + * by the specific reference index value in the hardware.
197 +static int ipq_cmn_pll_find_freq_index(unsigned long parent_rate)
199 + int index = -EINVAL;
201 + switch (parent_rate) {
214 + * Parent clock rate 48 MHZ and 96 MHZ take the same value
215 + * of reference clock index. 96 MHZ needs the source clock
216 + * divider to be programmed as 2.
230 +static unsigned long clk_cmn_pll_recalc_rate(struct clk_hw *hw,
231 + unsigned long parent_rate)
233 + struct clk_cmn_pll *cmn_pll = to_clk_cmn_pll(hw);
237 + * The value of CMN_PLL_DIVIDER_CTRL_FACTOR is automatically adjusted
238 + * by HW according to the parent clock rate.
240 + regmap_read(cmn_pll->regmap, CMN_PLL_DIVIDER_CTRL, &val);
241 + factor = FIELD_GET(CMN_PLL_DIVIDER_CTRL_FACTOR, val);
243 + return parent_rate * 2 * factor;
246 +static int clk_cmn_pll_determine_rate(struct clk_hw *hw,
247 + struct clk_rate_request *req)
251 + /* Validate the rate of the single parent clock. */
252 + ret = ipq_cmn_pll_find_freq_index(req->best_parent_rate);
254 + return ret < 0 ? ret : 0;
258 + * This function is used to initialize the CMN PLL to enable the fixed
259 + * rate output clocks. It is expected to be configured once.
261 +static int clk_cmn_pll_set_rate(struct clk_hw *hw, unsigned long rate,
262 + unsigned long parent_rate)
264 + struct clk_cmn_pll *cmn_pll = to_clk_cmn_pll(hw);
269 + * Configure the reference input clock selection as per the given
270 + * parent clock. The output clock rates are always of fixed value.
272 + index = ipq_cmn_pll_find_freq_index(parent_rate);
276 + ret = regmap_update_bits(cmn_pll->regmap, CMN_PLL_REFCLK_CONFIG,
277 + CMN_PLL_REFCLK_INDEX,
278 + FIELD_PREP(CMN_PLL_REFCLK_INDEX, index));
283 + * Update the source clock rate selection and source clock
284 + * divider as 2 when the parent clock rate is 96 MHZ.
286 + if (parent_rate == 96000000) {
287 + ret = regmap_update_bits(cmn_pll->regmap, CMN_PLL_REFCLK_CONFIG,
288 + CMN_PLL_REFCLK_DIV,
289 + FIELD_PREP(CMN_PLL_REFCLK_DIV, 2));
293 + ret = regmap_update_bits(cmn_pll->regmap, CMN_PLL_REFCLK_SRC_SELECTION,
294 + CMN_PLL_REFCLK_SRC_DIV,
295 + FIELD_PREP(CMN_PLL_REFCLK_SRC_DIV, 0));
300 + /* Enable PLL locked detect. */
301 + ret = regmap_set_bits(cmn_pll->regmap, CMN_PLL_CTRL,
302 + CMN_PLL_CTRL_LOCK_DETECT_EN);
307 + * Reset the CMN PLL block to ensure the updated configurations
310 + ret = regmap_clear_bits(cmn_pll->regmap, CMN_PLL_POWER_ON_AND_RESET,
311 + CMN_ANA_EN_SW_RSTN);
315 + usleep_range(1000, 1200);
316 + ret = regmap_set_bits(cmn_pll->regmap, CMN_PLL_POWER_ON_AND_RESET,
317 + CMN_ANA_EN_SW_RSTN);
321 + /* Stability check of CMN PLL output clocks. */
322 + return regmap_read_poll_timeout(cmn_pll->regmap, CMN_PLL_LOCKED, val,
323 + (val & CMN_PLL_CLKS_LOCKED),
324 + 100, 100 * USEC_PER_MSEC);
327 +static const struct clk_ops clk_cmn_pll_ops = {
328 + .recalc_rate = clk_cmn_pll_recalc_rate,
329 + .determine_rate = clk_cmn_pll_determine_rate,
330 + .set_rate = clk_cmn_pll_set_rate,
333 +static struct clk_hw *ipq_cmn_pll_clk_hw_register(struct platform_device *pdev)
335 + struct clk_parent_data pdata = { .index = 0 };
336 + struct device *dev = &pdev->dev;
337 + struct clk_init_data init = {};
338 + struct clk_cmn_pll *cmn_pll;
339 + struct regmap *regmap;
340 + void __iomem *base;
343 + base = devm_platform_ioremap_resource(pdev, 0);
345 + return ERR_CAST(base);
347 + regmap = devm_regmap_init_mmio(dev, base, &ipq_cmn_pll_regmap_config);
348 + if (IS_ERR(regmap))
349 + return ERR_CAST(regmap);
351 + cmn_pll = devm_kzalloc(dev, sizeof(*cmn_pll), GFP_KERNEL);
353 + return ERR_PTR(-ENOMEM);
355 + init.name = "cmn_pll";
356 + init.parent_data = &pdata;
357 + init.num_parents = 1;
358 + init.ops = &clk_cmn_pll_ops;
360 + cmn_pll->hw.init = &init;
361 + cmn_pll->regmap = regmap;
363 + ret = devm_clk_hw_register(dev, &cmn_pll->hw);
365 + return ERR_PTR(ret);
367 + return &cmn_pll->hw;
370 +static int ipq_cmn_pll_register_clks(struct platform_device *pdev)
372 + const struct cmn_pll_fixed_output_clk *fixed_clk;
373 + struct clk_hw_onecell_data *hw_data;
374 + struct device *dev = &pdev->dev;
375 + struct clk_hw *cmn_pll_hw;
376 + unsigned int num_clks;
380 + fixed_clk = ipq9574_output_clks;
381 + num_clks = ARRAY_SIZE(ipq9574_output_clks);
383 + hw_data = devm_kzalloc(dev, struct_size(hw_data, hws, num_clks + 1),
389 + * Register the CMN PLL clock, which is the parent clock of
390 + * the fixed rate output clocks.
392 + cmn_pll_hw = ipq_cmn_pll_clk_hw_register(pdev);
393 + if (IS_ERR(cmn_pll_hw))
394 + return PTR_ERR(cmn_pll_hw);
396 + /* Register the fixed rate output clocks. */
397 + for (i = 0; i < num_clks; i++) {
398 + hw = clk_hw_register_fixed_rate_parent_hw(dev, fixed_clk[i].name,
400 + fixed_clk[i].rate);
403 + goto unregister_fixed_clk;
406 + hw_data->hws[fixed_clk[i].id] = hw;
410 + * Provide the CMN PLL clock. The clock rate of CMN PLL
411 + * is configured to 12 GHZ by DT property assigned-clock-rates-u64.
413 + hw_data->hws[CMN_PLL_CLK] = cmn_pll_hw;
414 + hw_data->num = num_clks + 1;
416 + ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, hw_data);
418 + goto unregister_fixed_clk;
420 + platform_set_drvdata(pdev, hw_data);
424 +unregister_fixed_clk:
426 + clk_hw_unregister(hw_data->hws[fixed_clk[--i].id]);
431 +static int ipq_cmn_pll_clk_probe(struct platform_device *pdev)
433 + struct device *dev = &pdev->dev;
436 + ret = devm_pm_runtime_enable(dev);
440 + ret = devm_pm_clk_create(dev);
445 + * To access the CMN PLL registers, the GCC AHB & SYS clocks
446 + * of CMN PLL block need to be enabled.
448 + ret = pm_clk_add(dev, "ahb");
450 + return dev_err_probe(dev, ret, "Fail to add AHB clock\n");
452 + ret = pm_clk_add(dev, "sys");
454 + return dev_err_probe(dev, ret, "Fail to add SYS clock\n");
456 + ret = pm_runtime_resume_and_get(dev);
460 + /* Register CMN PLL clock and fixed rate output clocks. */
461 + ret = ipq_cmn_pll_register_clks(pdev);
462 + pm_runtime_put(dev);
464 + return dev_err_probe(dev, ret,
465 + "Fail to register CMN PLL clocks\n");
470 +static void ipq_cmn_pll_clk_remove(struct platform_device *pdev)
472 + struct clk_hw_onecell_data *hw_data = platform_get_drvdata(pdev);
476 + * The clock with index CMN_PLL_CLK is unregistered by
477 + * device management.
479 + for (i = 0; i < hw_data->num; i++) {
480 + if (i != CMN_PLL_CLK)
481 + clk_hw_unregister(hw_data->hws[i]);
485 +static const struct dev_pm_ops ipq_cmn_pll_pm_ops = {
486 + SET_RUNTIME_PM_OPS(pm_clk_suspend, pm_clk_resume, NULL)
489 +static const struct of_device_id ipq_cmn_pll_clk_ids[] = {
490 + { .compatible = "qcom,ipq9574-cmn-pll", },
493 +MODULE_DEVICE_TABLE(of, ipq_cmn_pll_clk_ids);
495 +static struct platform_driver ipq_cmn_pll_clk_driver = {
496 + .probe = ipq_cmn_pll_clk_probe,
497 + .remove_new = ipq_cmn_pll_clk_remove,
499 + .name = "ipq_cmn_pll",
500 + .of_match_table = ipq_cmn_pll_clk_ids,
501 + .pm = &ipq_cmn_pll_pm_ops,
504 +module_platform_driver(ipq_cmn_pll_clk_driver);
506 +MODULE_DESCRIPTION("Qualcomm Technologies, Inc. IPQ CMN PLL Driver");
507 +MODULE_LICENSE("GPL");