1 From b3f2f10693aadeacf83ab5be03810941a4b77612 Mon Sep 17 00:00:00 2001
2 From: Stephen Boyd <sboyd@codeaurora.org>
3 Date: Tue, 14 Aug 2018 17:42:21 +0530
4 Subject: [PATCH 02/12] clk: qcom: Add support for High-Frequency PLLs (HFPLLs)
6 HFPLLs are the main frequency source for Krait CPU clocks. Add
7 support for changing the rate of these PLLs.
9 Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
10 Signed-off-by: Sricharan R <sricharan@codeaurora.org>
11 Tested-by: Craig Tatlor <ctatlor97@gmail.com>
12 Signed-off-by: Stephen Boyd <sboyd@kernel.org>
14 drivers/clk/qcom/Makefile | 1 +
15 drivers/clk/qcom/clk-hfpll.c | 244 +++++++++++++++++++++++++++++++++++
16 drivers/clk/qcom/clk-hfpll.h | 44 +++++++
17 3 files changed, 289 insertions(+)
18 create mode 100644 drivers/clk/qcom/clk-hfpll.c
19 create mode 100644 drivers/clk/qcom/clk-hfpll.h
21 --- a/drivers/clk/qcom/Makefile
22 +++ b/drivers/clk/qcom/Makefile
23 @@ -11,6 +11,7 @@ clk-qcom-y += clk-branch.o
24 clk-qcom-y += clk-regmap-divider.o
25 clk-qcom-y += clk-regmap-mux.o
26 clk-qcom-y += clk-regmap-mux-div.o
27 +clk-qcom-y += clk-hfpll.o
29 clk-qcom-$(CONFIG_QCOM_GDSC) += gdsc.o
32 +++ b/drivers/clk/qcom/clk-hfpll.c
34 +// SPDX-License-Identifier: GPL-2.0
35 +// Copyright (c) 2018, The Linux Foundation. All rights reserved.
37 +#include <linux/kernel.h>
38 +#include <linux/export.h>
39 +#include <linux/regmap.h>
40 +#include <linux/delay.h>
41 +#include <linux/err.h>
42 +#include <linux/clk-provider.h>
43 +#include <linux/spinlock.h>
45 +#include "clk-regmap.h"
46 +#include "clk-hfpll.h"
48 +#define PLL_OUTCTRL BIT(0)
49 +#define PLL_BYPASSNL BIT(1)
50 +#define PLL_RESET_N BIT(2)
52 +/* Initialize a HFPLL at a given rate and enable it. */
53 +static void __clk_hfpll_init_once(struct clk_hw *hw)
55 + struct clk_hfpll *h = to_clk_hfpll(hw);
56 + struct hfpll_data const *hd = h->d;
57 + struct regmap *regmap = h->clkr.regmap;
59 + if (likely(h->init_done))
62 + /* Configure PLL parameters for integer mode. */
64 + regmap_write(regmap, hd->config_reg, hd->config_val);
65 + regmap_write(regmap, hd->m_reg, 0);
66 + regmap_write(regmap, hd->n_reg, 1);
69 + u32 regval = hd->user_val;
72 + rate = clk_hw_get_rate(hw);
74 + /* Pick the right VCO. */
75 + if (hd->user_vco_mask && rate > hd->low_vco_max_rate)
76 + regval |= hd->user_vco_mask;
77 + regmap_write(regmap, hd->user_reg, regval);
81 + regmap_write(regmap, hd->droop_reg, hd->droop_val);
83 + h->init_done = true;
86 +static void __clk_hfpll_enable(struct clk_hw *hw)
88 + struct clk_hfpll *h = to_clk_hfpll(hw);
89 + struct hfpll_data const *hd = h->d;
90 + struct regmap *regmap = h->clkr.regmap;
93 + __clk_hfpll_init_once(hw);
95 + /* Disable PLL bypass mode. */
96 + regmap_update_bits(regmap, hd->mode_reg, PLL_BYPASSNL, PLL_BYPASSNL);
99 + * H/W requires a 5us delay between disabling the bypass and
100 + * de-asserting the reset. Delay 10us just to be safe.
104 + /* De-assert active-low PLL reset. */
105 + regmap_update_bits(regmap, hd->mode_reg, PLL_RESET_N, PLL_RESET_N);
107 + /* Wait for PLL to lock. */
108 + if (hd->status_reg) {
110 + regmap_read(regmap, hd->status_reg, &val);
111 + } while (!(val & BIT(hd->lock_bit)));
116 + /* Enable PLL output. */
117 + regmap_update_bits(regmap, hd->mode_reg, PLL_OUTCTRL, PLL_OUTCTRL);
120 +/* Enable an already-configured HFPLL. */
121 +static int clk_hfpll_enable(struct clk_hw *hw)
123 + unsigned long flags;
124 + struct clk_hfpll *h = to_clk_hfpll(hw);
125 + struct hfpll_data const *hd = h->d;
126 + struct regmap *regmap = h->clkr.regmap;
129 + spin_lock_irqsave(&h->lock, flags);
130 + regmap_read(regmap, hd->mode_reg, &mode);
131 + if (!(mode & (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)))
132 + __clk_hfpll_enable(hw);
133 + spin_unlock_irqrestore(&h->lock, flags);
138 +static void __clk_hfpll_disable(struct clk_hfpll *h)
140 + struct hfpll_data const *hd = h->d;
141 + struct regmap *regmap = h->clkr.regmap;
144 + * Disable the PLL output, disable test mode, enable the bypass mode,
145 + * and assert the reset.
147 + regmap_update_bits(regmap, hd->mode_reg,
148 + PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL, 0);
151 +static void clk_hfpll_disable(struct clk_hw *hw)
153 + struct clk_hfpll *h = to_clk_hfpll(hw);
154 + unsigned long flags;
156 + spin_lock_irqsave(&h->lock, flags);
157 + __clk_hfpll_disable(h);
158 + spin_unlock_irqrestore(&h->lock, flags);
161 +static long clk_hfpll_round_rate(struct clk_hw *hw, unsigned long rate,
162 + unsigned long *parent_rate)
164 + struct clk_hfpll *h = to_clk_hfpll(hw);
165 + struct hfpll_data const *hd = h->d;
166 + unsigned long rrate;
168 + rate = clamp(rate, hd->min_rate, hd->max_rate);
170 + rrate = DIV_ROUND_UP(rate, *parent_rate) * *parent_rate;
171 + if (rrate > hd->max_rate)
172 + rrate -= *parent_rate;
178 + * For optimization reasons, assumes no downstream clocks are actively using
181 +static int clk_hfpll_set_rate(struct clk_hw *hw, unsigned long rate,
182 + unsigned long parent_rate)
184 + struct clk_hfpll *h = to_clk_hfpll(hw);
185 + struct hfpll_data const *hd = h->d;
186 + struct regmap *regmap = h->clkr.regmap;
187 + unsigned long flags;
191 + l_val = rate / parent_rate;
193 + spin_lock_irqsave(&h->lock, flags);
195 + enabled = __clk_is_enabled(hw->clk);
197 + __clk_hfpll_disable(h);
199 + /* Pick the right VCO. */
200 + if (hd->user_reg && hd->user_vco_mask) {
201 + regmap_read(regmap, hd->user_reg, &val);
202 + if (rate <= hd->low_vco_max_rate)
203 + val &= ~hd->user_vco_mask;
205 + val |= hd->user_vco_mask;
206 + regmap_write(regmap, hd->user_reg, val);
209 + regmap_write(regmap, hd->l_reg, l_val);
212 + __clk_hfpll_enable(hw);
214 + spin_unlock_irqrestore(&h->lock, flags);
219 +static unsigned long clk_hfpll_recalc_rate(struct clk_hw *hw,
220 + unsigned long parent_rate)
222 + struct clk_hfpll *h = to_clk_hfpll(hw);
223 + struct hfpll_data const *hd = h->d;
224 + struct regmap *regmap = h->clkr.regmap;
227 + regmap_read(regmap, hd->l_reg, &l_val);
229 + return l_val * parent_rate;
232 +static void clk_hfpll_init(struct clk_hw *hw)
234 + struct clk_hfpll *h = to_clk_hfpll(hw);
235 + struct hfpll_data const *hd = h->d;
236 + struct regmap *regmap = h->clkr.regmap;
239 + regmap_read(regmap, hd->mode_reg, &mode);
240 + if (mode != (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)) {
241 + __clk_hfpll_init_once(hw);
245 + if (hd->status_reg) {
246 + regmap_read(regmap, hd->status_reg, &status);
247 + if (!(status & BIT(hd->lock_bit))) {
248 + WARN(1, "HFPLL %s is ON, but not locked!\n",
249 + __clk_get_name(hw->clk));
250 + clk_hfpll_disable(hw);
251 + __clk_hfpll_init_once(hw);
256 +static int hfpll_is_enabled(struct clk_hw *hw)
258 + struct clk_hfpll *h = to_clk_hfpll(hw);
259 + struct hfpll_data const *hd = h->d;
260 + struct regmap *regmap = h->clkr.regmap;
263 + regmap_read(regmap, hd->mode_reg, &mode);
265 + return mode == (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL);
268 +const struct clk_ops clk_ops_hfpll = {
269 + .enable = clk_hfpll_enable,
270 + .disable = clk_hfpll_disable,
271 + .is_enabled = hfpll_is_enabled,
272 + .round_rate = clk_hfpll_round_rate,
273 + .set_rate = clk_hfpll_set_rate,
274 + .recalc_rate = clk_hfpll_recalc_rate,
275 + .init = clk_hfpll_init,
277 +EXPORT_SYMBOL_GPL(clk_ops_hfpll);
279 +++ b/drivers/clk/qcom/clk-hfpll.h
281 +/* SPDX-License-Identifier: GPL-2.0 */
283 +#ifndef __QCOM_CLK_HFPLL_H__
284 +#define __QCOM_CLK_HFPLL_H__
286 +#include <linux/clk-provider.h>
287 +#include <linux/spinlock.h>
288 +#include "clk-regmap.h"
305 + unsigned long low_vco_max_rate;
307 + unsigned long min_rate;
308 + unsigned long max_rate;
312 + struct hfpll_data const *d;
315 + struct clk_regmap clkr;
319 +#define to_clk_hfpll(_hw) \
320 + container_of(to_clk_regmap(_hw), struct clk_hfpll, clkr)
322 +extern const struct clk_ops clk_ops_hfpll;