1 From patchwork Fri Dec 8 09:42:21 2017
2 Content-Type: text/plain; charset="utf-8"
4 Content-Transfer-Encoding: 7bit
5 Subject: [v4,03/12] clk: qcom: Add support for High-Frequency PLLs (HFPLLs)
6 From: Sricharan R <sricharan@codeaurora.org>
7 X-Patchwork-Id: 10102083
8 Message-Id: <1512726150-7204-4-git-send-email-sricharan@codeaurora.org>
9 To: mturquette@baylibre.com, sboyd@codeaurora.org,
10 devicetree@vger.kernel.org, linux-pm@vger.kernel.org,
11 linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org,
12 viresh.kumar@linaro.org, linux-arm-kernel@lists.infradead.org
13 Cc: sricharan@codeaurora.org
14 Date: Fri, 8 Dec 2017 15:12:21 +0530
16 From: Stephen Boyd <sboyd@codeaurora.org>
18 HFPLLs are the main frequency source for Krait CPU clocks. Add
19 support for changing the rate of these PLLs.
21 Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
23 drivers/clk/qcom/Makefile | 1 +
24 drivers/clk/qcom/clk-hfpll.c | 253 +++++++++++++++++++++++++++++++++++++++++++
25 drivers/clk/qcom/clk-hfpll.h | 54 +++++++++
26 3 files changed, 308 insertions(+)
27 create mode 100644 drivers/clk/qcom/clk-hfpll.c
28 create mode 100644 drivers/clk/qcom/clk-hfpll.h
30 --- a/drivers/clk/qcom/Makefile
31 +++ b/drivers/clk/qcom/Makefile
32 @@ -10,6 +10,7 @@ clk-qcom-y += clk-rcg2.o
33 clk-qcom-y += clk-branch.o
34 clk-qcom-y += clk-regmap-divider.o
35 clk-qcom-y += clk-regmap-mux.o
36 +clk-qcom-y += clk-hfpll.o
38 clk-qcom-$(CONFIG_QCOM_GDSC) += gdsc.o
41 +++ b/drivers/clk/qcom/clk-hfpll.c
44 + * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
46 + * This program is free software; you can redistribute it and/or modify
47 + * it under the terms of the GNU General Public License version 2 and
48 + * only version 2 as published by the Free Software Foundation.
50 + * This program is distributed in the hope that it will be useful,
51 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
52 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 + * GNU General Public License for more details.
55 +#include <linux/kernel.h>
56 +#include <linux/export.h>
57 +#include <linux/regmap.h>
58 +#include <linux/delay.h>
59 +#include <linux/err.h>
60 +#include <linux/clk-provider.h>
61 +#include <linux/spinlock.h>
63 +#include "clk-regmap.h"
64 +#include "clk-hfpll.h"
66 +#define PLL_OUTCTRL BIT(0)
67 +#define PLL_BYPASSNL BIT(1)
68 +#define PLL_RESET_N BIT(2)
70 +/* Initialize a HFPLL at a given rate and enable it. */
71 +static void __clk_hfpll_init_once(struct clk_hw *hw)
73 + struct clk_hfpll *h = to_clk_hfpll(hw);
74 + struct hfpll_data const *hd = h->d;
75 + struct regmap *regmap = h->clkr.regmap;
77 + if (likely(h->init_done))
80 + /* Configure PLL parameters for integer mode. */
82 + regmap_write(regmap, hd->config_reg, hd->config_val);
83 + regmap_write(regmap, hd->m_reg, 0);
84 + regmap_write(regmap, hd->n_reg, 1);
87 + u32 regval = hd->user_val;
90 + rate = clk_hw_get_rate(hw);
92 + /* Pick the right VCO. */
93 + if (hd->user_vco_mask && rate > hd->low_vco_max_rate)
94 + regval |= hd->user_vco_mask;
95 + regmap_write(regmap, hd->user_reg, regval);
99 + regmap_write(regmap, hd->droop_reg, hd->droop_val);
101 + h->init_done = true;
104 +static void __clk_hfpll_enable(struct clk_hw *hw)
106 + struct clk_hfpll *h = to_clk_hfpll(hw);
107 + struct hfpll_data const *hd = h->d;
108 + struct regmap *regmap = h->clkr.regmap;
111 + __clk_hfpll_init_once(hw);
113 + /* Disable PLL bypass mode. */
114 + regmap_update_bits(regmap, hd->mode_reg, PLL_BYPASSNL, PLL_BYPASSNL);
117 + * H/W requires a 5us delay between disabling the bypass and
118 + * de-asserting the reset. Delay 10us just to be safe.
122 + /* De-assert active-low PLL reset. */
123 + regmap_update_bits(regmap, hd->mode_reg, PLL_RESET_N, PLL_RESET_N);
125 + /* Wait for PLL to lock. */
126 + if (hd->status_reg) {
128 + regmap_read(regmap, hd->status_reg, &val);
129 + } while (!(val & BIT(hd->lock_bit)));
134 + /* Enable PLL output. */
135 + regmap_update_bits(regmap, hd->mode_reg, PLL_OUTCTRL, PLL_OUTCTRL);
138 +/* Enable an already-configured HFPLL. */
139 +static int clk_hfpll_enable(struct clk_hw *hw)
141 + unsigned long flags;
142 + struct clk_hfpll *h = to_clk_hfpll(hw);
143 + struct hfpll_data const *hd = h->d;
144 + struct regmap *regmap = h->clkr.regmap;
147 + spin_lock_irqsave(&h->lock, flags);
148 + regmap_read(regmap, hd->mode_reg, &mode);
149 + if (!(mode & (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)))
150 + __clk_hfpll_enable(hw);
151 + spin_unlock_irqrestore(&h->lock, flags);
156 +static void __clk_hfpll_disable(struct clk_hfpll *h)
158 + struct hfpll_data const *hd = h->d;
159 + struct regmap *regmap = h->clkr.regmap;
162 + * Disable the PLL output, disable test mode, enable the bypass mode,
163 + * and assert the reset.
165 + regmap_update_bits(regmap, hd->mode_reg,
166 + PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL, 0);
169 +static void clk_hfpll_disable(struct clk_hw *hw)
171 + struct clk_hfpll *h = to_clk_hfpll(hw);
172 + unsigned long flags;
174 + spin_lock_irqsave(&h->lock, flags);
175 + __clk_hfpll_disable(h);
176 + spin_unlock_irqrestore(&h->lock, flags);
179 +static long clk_hfpll_round_rate(struct clk_hw *hw, unsigned long rate,
180 + unsigned long *parent_rate)
182 + struct clk_hfpll *h = to_clk_hfpll(hw);
183 + struct hfpll_data const *hd = h->d;
184 + unsigned long rrate;
186 + rate = clamp(rate, hd->min_rate, hd->max_rate);
188 + rrate = DIV_ROUND_UP(rate, *parent_rate) * *parent_rate;
189 + if (rrate > hd->max_rate)
190 + rrate -= *parent_rate;
196 + * For optimization reasons, assumes no downstream clocks are actively using
199 +static int clk_hfpll_set_rate(struct clk_hw *hw, unsigned long rate,
200 + unsigned long parent_rate)
202 + struct clk_hfpll *h = to_clk_hfpll(hw);
203 + struct hfpll_data const *hd = h->d;
204 + struct regmap *regmap = h->clkr.regmap;
205 + unsigned long flags;
209 + l_val = rate / parent_rate;
211 + spin_lock_irqsave(&h->lock, flags);
213 + enabled = __clk_is_enabled(hw->clk);
215 + __clk_hfpll_disable(h);
217 + /* Pick the right VCO. */
218 + if (hd->user_reg && hd->user_vco_mask) {
219 + regmap_read(regmap, hd->user_reg, &val);
220 + if (rate <= hd->low_vco_max_rate)
221 + val &= ~hd->user_vco_mask;
223 + val |= hd->user_vco_mask;
224 + regmap_write(regmap, hd->user_reg, val);
227 + regmap_write(regmap, hd->l_reg, l_val);
230 + __clk_hfpll_enable(hw);
232 + spin_unlock_irqrestore(&h->lock, flags);
237 +static unsigned long clk_hfpll_recalc_rate(struct clk_hw *hw,
238 + unsigned long parent_rate)
240 + struct clk_hfpll *h = to_clk_hfpll(hw);
241 + struct hfpll_data const *hd = h->d;
242 + struct regmap *regmap = h->clkr.regmap;
245 + regmap_read(regmap, hd->l_reg, &l_val);
247 + return l_val * parent_rate;
250 +static void clk_hfpll_init(struct clk_hw *hw)
252 + struct clk_hfpll *h = to_clk_hfpll(hw);
253 + struct hfpll_data const *hd = h->d;
254 + struct regmap *regmap = h->clkr.regmap;
257 + regmap_read(regmap, hd->mode_reg, &mode);
258 + if (mode != (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)) {
259 + __clk_hfpll_init_once(hw);
263 + if (hd->status_reg) {
264 + regmap_read(regmap, hd->status_reg, &status);
265 + if (!(status & BIT(hd->lock_bit))) {
266 + WARN(1, "HFPLL %s is ON, but not locked!\n",
267 + __clk_get_name(hw->clk));
268 + clk_hfpll_disable(hw);
269 + __clk_hfpll_init_once(hw);
274 +static int hfpll_is_enabled(struct clk_hw *hw)
276 + struct clk_hfpll *h = to_clk_hfpll(hw);
277 + struct hfpll_data const *hd = h->d;
278 + struct regmap *regmap = h->clkr.regmap;
281 + regmap_read(regmap, hd->mode_reg, &mode);
283 + return mode == (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL);
286 +const struct clk_ops clk_ops_hfpll = {
287 + .enable = clk_hfpll_enable,
288 + .disable = clk_hfpll_disable,
289 + .is_enabled = hfpll_is_enabled,
290 + .round_rate = clk_hfpll_round_rate,
291 + .set_rate = clk_hfpll_set_rate,
292 + .recalc_rate = clk_hfpll_recalc_rate,
293 + .init = clk_hfpll_init,
295 +EXPORT_SYMBOL_GPL(clk_ops_hfpll);
297 +++ b/drivers/clk/qcom/clk-hfpll.h
300 + * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
302 + * This program is free software; you can redistribute it and/or modify
303 + * it under the terms of the GNU General Public License version 2 and
304 + * only version 2 as published by the Free Software Foundation.
306 + * This program is distributed in the hope that it will be useful,
307 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
308 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
309 + * GNU General Public License for more details.
311 +#ifndef __QCOM_CLK_HFPLL_H__
312 +#define __QCOM_CLK_HFPLL_H__
314 +#include <linux/clk-provider.h>
315 +#include <linux/spinlock.h>
316 +#include "clk-regmap.h"
333 + unsigned long low_vco_max_rate;
335 + unsigned long min_rate;
336 + unsigned long max_rate;
340 + struct hfpll_data const *d;
343 + struct clk_regmap clkr;
347 +#define to_clk_hfpll(_hw) \
348 + container_of(to_clk_regmap(_hw), struct clk_hfpll, clkr)
350 +extern const struct clk_ops clk_ops_hfpll;