1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * PLL clock driver for TI Davinci SoCs
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2018 David Lechner <david@lechnology.com>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Based on arch/arm/mach-davinci/clock.c
8*4882a593Smuzhiyun * Copyright (C) 2006-2007 Texas Instruments.
9*4882a593Smuzhiyun * Copyright (C) 2008-2009 Deep Root Systems, LLC
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/clk-provider.h>
13*4882a593Smuzhiyun #include <linux/clk.h>
14*4882a593Smuzhiyun #include <linux/clk/davinci.h>
15*4882a593Smuzhiyun #include <linux/delay.h>
16*4882a593Smuzhiyun #include <linux/err.h>
17*4882a593Smuzhiyun #include <linux/io.h>
18*4882a593Smuzhiyun #include <linux/kernel.h>
19*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
20*4882a593Smuzhiyun #include <linux/notifier.h>
21*4882a593Smuzhiyun #include <linux/of_address.h>
22*4882a593Smuzhiyun #include <linux/of_device.h>
23*4882a593Smuzhiyun #include <linux/of.h>
24*4882a593Smuzhiyun #include <linux/platform_data/clk-davinci-pll.h>
25*4882a593Smuzhiyun #include <linux/platform_device.h>
26*4882a593Smuzhiyun #include <linux/regmap.h>
27*4882a593Smuzhiyun #include <linux/slab.h>
28*4882a593Smuzhiyun #include <linux/types.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include "pll.h"
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define MAX_NAME_SIZE 20
33*4882a593Smuzhiyun #define OSCIN_CLK_NAME "oscin"
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #define REVID 0x000
36*4882a593Smuzhiyun #define PLLCTL 0x100
37*4882a593Smuzhiyun #define OCSEL 0x104
38*4882a593Smuzhiyun #define PLLSECCTL 0x108
39*4882a593Smuzhiyun #define PLLM 0x110
40*4882a593Smuzhiyun #define PREDIV 0x114
41*4882a593Smuzhiyun #define PLLDIV1 0x118
42*4882a593Smuzhiyun #define PLLDIV2 0x11c
43*4882a593Smuzhiyun #define PLLDIV3 0x120
44*4882a593Smuzhiyun #define OSCDIV 0x124
45*4882a593Smuzhiyun #define POSTDIV 0x128
46*4882a593Smuzhiyun #define BPDIV 0x12c
47*4882a593Smuzhiyun #define PLLCMD 0x138
48*4882a593Smuzhiyun #define PLLSTAT 0x13c
49*4882a593Smuzhiyun #define ALNCTL 0x140
50*4882a593Smuzhiyun #define DCHANGE 0x144
51*4882a593Smuzhiyun #define CKEN 0x148
52*4882a593Smuzhiyun #define CKSTAT 0x14c
53*4882a593Smuzhiyun #define SYSTAT 0x150
54*4882a593Smuzhiyun #define PLLDIV4 0x160
55*4882a593Smuzhiyun #define PLLDIV5 0x164
56*4882a593Smuzhiyun #define PLLDIV6 0x168
57*4882a593Smuzhiyun #define PLLDIV7 0x16c
58*4882a593Smuzhiyun #define PLLDIV8 0x170
59*4882a593Smuzhiyun #define PLLDIV9 0x174
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define PLLCTL_PLLEN BIT(0)
62*4882a593Smuzhiyun #define PLLCTL_PLLPWRDN BIT(1)
63*4882a593Smuzhiyun #define PLLCTL_PLLRST BIT(3)
64*4882a593Smuzhiyun #define PLLCTL_PLLDIS BIT(4)
65*4882a593Smuzhiyun #define PLLCTL_PLLENSRC BIT(5)
66*4882a593Smuzhiyun #define PLLCTL_CLKMODE BIT(8)
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* shared by most *DIV registers */
69*4882a593Smuzhiyun #define DIV_RATIO_SHIFT 0
70*4882a593Smuzhiyun #define DIV_RATIO_WIDTH 5
71*4882a593Smuzhiyun #define DIV_ENABLE_SHIFT 15
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun #define PLLCMD_GOSET BIT(0)
74*4882a593Smuzhiyun #define PLLSTAT_GOSTAT BIT(0)
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun #define CKEN_OBSCLK_SHIFT 1
77*4882a593Smuzhiyun #define CKEN_AUXEN_SHIFT 0
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun * OMAP-L138 system reference guide recommends a wait for 4 OSCIN/CLKIN
81*4882a593Smuzhiyun * cycles to ensure that the PLLC has switched to bypass mode. Delay of 1us
82*4882a593Smuzhiyun * ensures we are good for all > 4MHz OSCIN/CLKIN inputs. Typically the input
83*4882a593Smuzhiyun * is ~25MHz. Units are micro seconds.
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun #define PLL_BYPASS_TIME 1
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* From OMAP-L138 datasheet table 6-4. Units are micro seconds */
88*4882a593Smuzhiyun #define PLL_RESET_TIME 1
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /*
91*4882a593Smuzhiyun * From OMAP-L138 datasheet table 6-4; assuming prediv = 1, sqrt(pllm) = 4
92*4882a593Smuzhiyun * Units are micro seconds.
93*4882a593Smuzhiyun */
94*4882a593Smuzhiyun #define PLL_LOCK_TIME 20
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /**
97*4882a593Smuzhiyun * struct davinci_pll_clk - Main PLL clock (aka PLLOUT)
98*4882a593Smuzhiyun * @hw: clk_hw for the pll
99*4882a593Smuzhiyun * @base: Base memory address
100*4882a593Smuzhiyun * @pllm_min: The minimum allowable PLLM[PLLM] value
101*4882a593Smuzhiyun * @pllm_max: The maxiumum allowable PLLM[PLLM] value
102*4882a593Smuzhiyun * @pllm_mask: Bitmask for PLLM[PLLM] value
103*4882a593Smuzhiyun */
104*4882a593Smuzhiyun struct davinci_pll_clk {
105*4882a593Smuzhiyun struct clk_hw hw;
106*4882a593Smuzhiyun void __iomem *base;
107*4882a593Smuzhiyun u32 pllm_min;
108*4882a593Smuzhiyun u32 pllm_max;
109*4882a593Smuzhiyun u32 pllm_mask;
110*4882a593Smuzhiyun };
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun #define to_davinci_pll_clk(_hw) \
113*4882a593Smuzhiyun container_of((_hw), struct davinci_pll_clk, hw)
114*4882a593Smuzhiyun
davinci_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)115*4882a593Smuzhiyun static unsigned long davinci_pll_recalc_rate(struct clk_hw *hw,
116*4882a593Smuzhiyun unsigned long parent_rate)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
119*4882a593Smuzhiyun unsigned long rate = parent_rate;
120*4882a593Smuzhiyun u32 mult;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun mult = readl(pll->base + PLLM) & pll->pllm_mask;
123*4882a593Smuzhiyun rate *= mult + 1;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun return rate;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
davinci_pll_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)128*4882a593Smuzhiyun static int davinci_pll_determine_rate(struct clk_hw *hw,
129*4882a593Smuzhiyun struct clk_rate_request *req)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
132*4882a593Smuzhiyun struct clk_hw *parent = req->best_parent_hw;
133*4882a593Smuzhiyun unsigned long parent_rate = req->best_parent_rate;
134*4882a593Smuzhiyun unsigned long rate = req->rate;
135*4882a593Smuzhiyun unsigned long best_rate, r;
136*4882a593Smuzhiyun u32 mult;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /* there is a limited range of valid outputs (see datasheet) */
139*4882a593Smuzhiyun if (rate < req->min_rate)
140*4882a593Smuzhiyun return -EINVAL;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun rate = min(rate, req->max_rate);
143*4882a593Smuzhiyun mult = rate / parent_rate;
144*4882a593Smuzhiyun best_rate = parent_rate * mult;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* easy case when there is no PREDIV */
147*4882a593Smuzhiyun if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
148*4882a593Smuzhiyun if (best_rate < req->min_rate)
149*4882a593Smuzhiyun return -EINVAL;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun if (mult < pll->pllm_min || mult > pll->pllm_max)
152*4882a593Smuzhiyun return -EINVAL;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun req->rate = best_rate;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun return 0;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* see if the PREDIV clock can help us */
160*4882a593Smuzhiyun best_rate = 0;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun for (mult = pll->pllm_min; mult <= pll->pllm_max; mult++) {
163*4882a593Smuzhiyun parent_rate = clk_hw_round_rate(parent, rate / mult);
164*4882a593Smuzhiyun r = parent_rate * mult;
165*4882a593Smuzhiyun if (r < req->min_rate)
166*4882a593Smuzhiyun continue;
167*4882a593Smuzhiyun if (r > rate || r > req->max_rate)
168*4882a593Smuzhiyun break;
169*4882a593Smuzhiyun if (r > best_rate) {
170*4882a593Smuzhiyun best_rate = r;
171*4882a593Smuzhiyun req->rate = best_rate;
172*4882a593Smuzhiyun req->best_parent_rate = parent_rate;
173*4882a593Smuzhiyun if (best_rate == rate)
174*4882a593Smuzhiyun break;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun return 0;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
davinci_pll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)181*4882a593Smuzhiyun static int davinci_pll_set_rate(struct clk_hw *hw, unsigned long rate,
182*4882a593Smuzhiyun unsigned long parent_rate)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
185*4882a593Smuzhiyun u32 mult;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun mult = rate / parent_rate;
188*4882a593Smuzhiyun writel(mult - 1, pll->base + PLLM);
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun return 0;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
194*4882a593Smuzhiyun static void davinci_pll_debug_init(struct clk_hw *hw, struct dentry *dentry);
195*4882a593Smuzhiyun #else
196*4882a593Smuzhiyun #define davinci_pll_debug_init NULL
197*4882a593Smuzhiyun #endif
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun static const struct clk_ops davinci_pll_ops = {
200*4882a593Smuzhiyun .recalc_rate = davinci_pll_recalc_rate,
201*4882a593Smuzhiyun .determine_rate = davinci_pll_determine_rate,
202*4882a593Smuzhiyun .set_rate = davinci_pll_set_rate,
203*4882a593Smuzhiyun .debug_init = davinci_pll_debug_init,
204*4882a593Smuzhiyun };
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* PLLM works differently on DM365 */
dm365_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)207*4882a593Smuzhiyun static unsigned long dm365_pll_recalc_rate(struct clk_hw *hw,
208*4882a593Smuzhiyun unsigned long parent_rate)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
211*4882a593Smuzhiyun unsigned long rate = parent_rate;
212*4882a593Smuzhiyun u32 mult;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun mult = readl(pll->base + PLLM) & pll->pllm_mask;
215*4882a593Smuzhiyun rate *= mult * 2;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun return rate;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun static const struct clk_ops dm365_pll_ops = {
221*4882a593Smuzhiyun .recalc_rate = dm365_pll_recalc_rate,
222*4882a593Smuzhiyun .debug_init = davinci_pll_debug_init,
223*4882a593Smuzhiyun };
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /**
226*4882a593Smuzhiyun * davinci_pll_div_register - common *DIV clock implementation
227*4882a593Smuzhiyun * @dev: The PLL platform device or NULL
228*4882a593Smuzhiyun * @name: the clock name
229*4882a593Smuzhiyun * @parent_name: the parent clock name
230*4882a593Smuzhiyun * @reg: the *DIV register
231*4882a593Smuzhiyun * @fixed: if true, the divider is a fixed value
232*4882a593Smuzhiyun * @flags: bitmap of CLK_* flags from clock-provider.h
233*4882a593Smuzhiyun */
davinci_pll_div_register(struct device * dev,const char * name,const char * parent_name,void __iomem * reg,bool fixed,u32 flags)234*4882a593Smuzhiyun static struct clk *davinci_pll_div_register(struct device *dev,
235*4882a593Smuzhiyun const char *name,
236*4882a593Smuzhiyun const char *parent_name,
237*4882a593Smuzhiyun void __iomem *reg,
238*4882a593Smuzhiyun bool fixed, u32 flags)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun const char * const *parent_names = parent_name ? &parent_name : NULL;
241*4882a593Smuzhiyun int num_parents = parent_name ? 1 : 0;
242*4882a593Smuzhiyun const struct clk_ops *divider_ops = &clk_divider_ops;
243*4882a593Smuzhiyun struct clk_gate *gate;
244*4882a593Smuzhiyun struct clk_divider *divider;
245*4882a593Smuzhiyun struct clk *clk;
246*4882a593Smuzhiyun int ret;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun gate = kzalloc(sizeof(*gate), GFP_KERNEL);
249*4882a593Smuzhiyun if (!gate)
250*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun gate->reg = reg;
253*4882a593Smuzhiyun gate->bit_idx = DIV_ENABLE_SHIFT;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun divider = kzalloc(sizeof(*divider), GFP_KERNEL);
256*4882a593Smuzhiyun if (!divider) {
257*4882a593Smuzhiyun ret = -ENOMEM;
258*4882a593Smuzhiyun goto err_free_gate;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun divider->reg = reg;
262*4882a593Smuzhiyun divider->shift = DIV_RATIO_SHIFT;
263*4882a593Smuzhiyun divider->width = DIV_RATIO_WIDTH;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun if (fixed) {
266*4882a593Smuzhiyun divider->flags |= CLK_DIVIDER_READ_ONLY;
267*4882a593Smuzhiyun divider_ops = &clk_divider_ro_ops;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun clk = clk_register_composite(dev, name, parent_names, num_parents,
271*4882a593Smuzhiyun NULL, NULL, ÷r->hw, divider_ops,
272*4882a593Smuzhiyun &gate->hw, &clk_gate_ops, flags);
273*4882a593Smuzhiyun if (IS_ERR(clk)) {
274*4882a593Smuzhiyun ret = PTR_ERR(clk);
275*4882a593Smuzhiyun goto err_free_divider;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun return clk;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun err_free_divider:
281*4882a593Smuzhiyun kfree(divider);
282*4882a593Smuzhiyun err_free_gate:
283*4882a593Smuzhiyun kfree(gate);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun return ERR_PTR(ret);
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun struct davinci_pllen_clk {
289*4882a593Smuzhiyun struct clk_hw hw;
290*4882a593Smuzhiyun void __iomem *base;
291*4882a593Smuzhiyun };
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun #define to_davinci_pllen_clk(_hw) \
294*4882a593Smuzhiyun container_of((_hw), struct davinci_pllen_clk, hw)
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun static const struct clk_ops davinci_pllen_ops = {
297*4882a593Smuzhiyun /* this clocks just uses the clock notification feature */
298*4882a593Smuzhiyun };
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun /*
301*4882a593Smuzhiyun * The PLL has to be switched into bypass mode while we are chaning the rate,
302*4882a593Smuzhiyun * so we do that on the PLLEN clock since it is the end of the line. This will
303*4882a593Smuzhiyun * switch to bypass before any of the parent clocks (PREDIV, PLL, POSTDIV) are
304*4882a593Smuzhiyun * changed and will switch back to the PLL after the changes have been made.
305*4882a593Smuzhiyun */
davinci_pllen_rate_change(struct notifier_block * nb,unsigned long flags,void * data)306*4882a593Smuzhiyun static int davinci_pllen_rate_change(struct notifier_block *nb,
307*4882a593Smuzhiyun unsigned long flags, void *data)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun struct clk_notifier_data *cnd = data;
310*4882a593Smuzhiyun struct clk_hw *hw = __clk_get_hw(cnd->clk);
311*4882a593Smuzhiyun struct davinci_pllen_clk *pll = to_davinci_pllen_clk(hw);
312*4882a593Smuzhiyun u32 ctrl;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun ctrl = readl(pll->base + PLLCTL);
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun if (flags == PRE_RATE_CHANGE) {
317*4882a593Smuzhiyun /* Switch the PLL to bypass mode */
318*4882a593Smuzhiyun ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
319*4882a593Smuzhiyun writel(ctrl, pll->base + PLLCTL);
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun udelay(PLL_BYPASS_TIME);
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun /* Reset and enable PLL */
324*4882a593Smuzhiyun ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
325*4882a593Smuzhiyun writel(ctrl, pll->base + PLLCTL);
326*4882a593Smuzhiyun } else {
327*4882a593Smuzhiyun udelay(PLL_RESET_TIME);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /* Bring PLL out of reset */
330*4882a593Smuzhiyun ctrl |= PLLCTL_PLLRST;
331*4882a593Smuzhiyun writel(ctrl, pll->base + PLLCTL);
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun udelay(PLL_LOCK_TIME);
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /* Remove PLL from bypass mode */
336*4882a593Smuzhiyun ctrl |= PLLCTL_PLLEN;
337*4882a593Smuzhiyun writel(ctrl, pll->base + PLLCTL);
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun return NOTIFY_OK;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun static struct notifier_block davinci_pllen_notifier = {
344*4882a593Smuzhiyun .notifier_call = davinci_pllen_rate_change,
345*4882a593Smuzhiyun };
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /**
348*4882a593Smuzhiyun * davinci_pll_clk_register - Register a PLL clock
349*4882a593Smuzhiyun * @dev: The PLL platform device or NULL
350*4882a593Smuzhiyun * @info: The device-specific clock info
351*4882a593Smuzhiyun * @parent_name: The parent clock name
352*4882a593Smuzhiyun * @base: The PLL's memory region
353*4882a593Smuzhiyun * @cfgchip: CFGCHIP syscon regmap for info->unlock_reg or NULL
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * This creates a series of clocks that represent the PLL.
356*4882a593Smuzhiyun *
357*4882a593Smuzhiyun * OSCIN > [PREDIV >] PLL > [POSTDIV >] PLLEN
358*4882a593Smuzhiyun *
359*4882a593Smuzhiyun * - OSCIN is the parent clock (on secondary PLL, may come from primary PLL)
360*4882a593Smuzhiyun * - PREDIV and POSTDIV are optional (depends on the PLL controller)
361*4882a593Smuzhiyun * - PLL is the PLL output (aka PLLOUT)
362*4882a593Smuzhiyun * - PLLEN is the bypass multiplexer
363*4882a593Smuzhiyun *
364*4882a593Smuzhiyun * Returns: The PLLOUT clock or a negative error code.
365*4882a593Smuzhiyun */
davinci_pll_clk_register(struct device * dev,const struct davinci_pll_clk_info * info,const char * parent_name,void __iomem * base,struct regmap * cfgchip)366*4882a593Smuzhiyun struct clk *davinci_pll_clk_register(struct device *dev,
367*4882a593Smuzhiyun const struct davinci_pll_clk_info *info,
368*4882a593Smuzhiyun const char *parent_name,
369*4882a593Smuzhiyun void __iomem *base,
370*4882a593Smuzhiyun struct regmap *cfgchip)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun char prediv_name[MAX_NAME_SIZE];
373*4882a593Smuzhiyun char pllout_name[MAX_NAME_SIZE];
374*4882a593Smuzhiyun char postdiv_name[MAX_NAME_SIZE];
375*4882a593Smuzhiyun char pllen_name[MAX_NAME_SIZE];
376*4882a593Smuzhiyun struct clk_init_data init;
377*4882a593Smuzhiyun struct davinci_pll_clk *pllout;
378*4882a593Smuzhiyun struct davinci_pllen_clk *pllen;
379*4882a593Smuzhiyun struct clk *oscin_clk = NULL;
380*4882a593Smuzhiyun struct clk *prediv_clk = NULL;
381*4882a593Smuzhiyun struct clk *pllout_clk;
382*4882a593Smuzhiyun struct clk *postdiv_clk = NULL;
383*4882a593Smuzhiyun struct clk *pllen_clk;
384*4882a593Smuzhiyun int ret;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun if (info->flags & PLL_HAS_CLKMODE) {
387*4882a593Smuzhiyun /*
388*4882a593Smuzhiyun * If a PLL has PLLCTL[CLKMODE], then it is the primary PLL.
389*4882a593Smuzhiyun * We register a clock named "oscin" that serves as the internal
390*4882a593Smuzhiyun * "input clock" domain shared by both PLLs (if there are 2)
391*4882a593Smuzhiyun * and will be the parent clock to the AUXCLK, SYSCLKBP and
392*4882a593Smuzhiyun * OBSCLK domains. NB: The various TRMs use "OSCIN" to mean
393*4882a593Smuzhiyun * a number of different things. In this driver we use it to
394*4882a593Smuzhiyun * mean the signal after the PLLCTL[CLKMODE] switch.
395*4882a593Smuzhiyun */
396*4882a593Smuzhiyun oscin_clk = clk_register_fixed_factor(dev, OSCIN_CLK_NAME,
397*4882a593Smuzhiyun parent_name, 0, 1, 1);
398*4882a593Smuzhiyun if (IS_ERR(oscin_clk))
399*4882a593Smuzhiyun return oscin_clk;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun parent_name = OSCIN_CLK_NAME;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun if (info->flags & PLL_HAS_PREDIV) {
405*4882a593Smuzhiyun bool fixed = info->flags & PLL_PREDIV_FIXED_DIV;
406*4882a593Smuzhiyun u32 flags = 0;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun snprintf(prediv_name, MAX_NAME_SIZE, "%s_prediv", info->name);
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun if (info->flags & PLL_PREDIV_ALWAYS_ENABLED)
411*4882a593Smuzhiyun flags |= CLK_IS_CRITICAL;
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun /* Some? DM355 chips don't correctly report the PREDIV value */
414*4882a593Smuzhiyun if (info->flags & PLL_PREDIV_FIXED8)
415*4882a593Smuzhiyun prediv_clk = clk_register_fixed_factor(dev, prediv_name,
416*4882a593Smuzhiyun parent_name, flags, 1, 8);
417*4882a593Smuzhiyun else
418*4882a593Smuzhiyun prediv_clk = davinci_pll_div_register(dev, prediv_name,
419*4882a593Smuzhiyun parent_name, base + PREDIV, fixed, flags);
420*4882a593Smuzhiyun if (IS_ERR(prediv_clk)) {
421*4882a593Smuzhiyun ret = PTR_ERR(prediv_clk);
422*4882a593Smuzhiyun goto err_unregister_oscin;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun parent_name = prediv_name;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /* Unlock writing to PLL registers */
429*4882a593Smuzhiyun if (info->unlock_reg) {
430*4882a593Smuzhiyun if (IS_ERR_OR_NULL(cfgchip))
431*4882a593Smuzhiyun dev_warn(dev, "Failed to get CFGCHIP (%ld)\n",
432*4882a593Smuzhiyun PTR_ERR(cfgchip));
433*4882a593Smuzhiyun else
434*4882a593Smuzhiyun regmap_write_bits(cfgchip, info->unlock_reg,
435*4882a593Smuzhiyun info->unlock_mask, 0);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun pllout = kzalloc(sizeof(*pllout), GFP_KERNEL);
439*4882a593Smuzhiyun if (!pllout) {
440*4882a593Smuzhiyun ret = -ENOMEM;
441*4882a593Smuzhiyun goto err_unregister_prediv;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun snprintf(pllout_name, MAX_NAME_SIZE, "%s_pllout", info->name);
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun init.name = pllout_name;
447*4882a593Smuzhiyun if (info->flags & PLL_PLLM_2X)
448*4882a593Smuzhiyun init.ops = &dm365_pll_ops;
449*4882a593Smuzhiyun else
450*4882a593Smuzhiyun init.ops = &davinci_pll_ops;
451*4882a593Smuzhiyun init.parent_names = &parent_name;
452*4882a593Smuzhiyun init.num_parents = 1;
453*4882a593Smuzhiyun init.flags = 0;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun if (info->flags & PLL_HAS_PREDIV)
456*4882a593Smuzhiyun init.flags |= CLK_SET_RATE_PARENT;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun pllout->hw.init = &init;
459*4882a593Smuzhiyun pllout->base = base;
460*4882a593Smuzhiyun pllout->pllm_mask = info->pllm_mask;
461*4882a593Smuzhiyun pllout->pllm_min = info->pllm_min;
462*4882a593Smuzhiyun pllout->pllm_max = info->pllm_max;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun pllout_clk = clk_register(dev, &pllout->hw);
465*4882a593Smuzhiyun if (IS_ERR(pllout_clk)) {
466*4882a593Smuzhiyun ret = PTR_ERR(pllout_clk);
467*4882a593Smuzhiyun goto err_free_pllout;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun clk_hw_set_rate_range(&pllout->hw, info->pllout_min_rate,
471*4882a593Smuzhiyun info->pllout_max_rate);
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun parent_name = pllout_name;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun if (info->flags & PLL_HAS_POSTDIV) {
476*4882a593Smuzhiyun bool fixed = info->flags & PLL_POSTDIV_FIXED_DIV;
477*4882a593Smuzhiyun u32 flags = CLK_SET_RATE_PARENT;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun snprintf(postdiv_name, MAX_NAME_SIZE, "%s_postdiv", info->name);
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun if (info->flags & PLL_POSTDIV_ALWAYS_ENABLED)
482*4882a593Smuzhiyun flags |= CLK_IS_CRITICAL;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun postdiv_clk = davinci_pll_div_register(dev, postdiv_name,
485*4882a593Smuzhiyun parent_name, base + POSTDIV, fixed, flags);
486*4882a593Smuzhiyun if (IS_ERR(postdiv_clk)) {
487*4882a593Smuzhiyun ret = PTR_ERR(postdiv_clk);
488*4882a593Smuzhiyun goto err_unregister_pllout;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun parent_name = postdiv_name;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun pllen = kzalloc(sizeof(*pllen), GFP_KERNEL);
495*4882a593Smuzhiyun if (!pllen) {
496*4882a593Smuzhiyun ret = -ENOMEM;
497*4882a593Smuzhiyun goto err_unregister_postdiv;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun snprintf(pllen_name, MAX_NAME_SIZE, "%s_pllen", info->name);
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun init.name = pllen_name;
503*4882a593Smuzhiyun init.ops = &davinci_pllen_ops;
504*4882a593Smuzhiyun init.parent_names = &parent_name;
505*4882a593Smuzhiyun init.num_parents = 1;
506*4882a593Smuzhiyun init.flags = CLK_SET_RATE_PARENT;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun pllen->hw.init = &init;
509*4882a593Smuzhiyun pllen->base = base;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun pllen_clk = clk_register(dev, &pllen->hw);
512*4882a593Smuzhiyun if (IS_ERR(pllen_clk)) {
513*4882a593Smuzhiyun ret = PTR_ERR(pllen_clk);
514*4882a593Smuzhiyun goto err_free_pllen;
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun clk_notifier_register(pllen_clk, &davinci_pllen_notifier);
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun return pllout_clk;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun err_free_pllen:
522*4882a593Smuzhiyun kfree(pllen);
523*4882a593Smuzhiyun err_unregister_postdiv:
524*4882a593Smuzhiyun clk_unregister(postdiv_clk);
525*4882a593Smuzhiyun err_unregister_pllout:
526*4882a593Smuzhiyun clk_unregister(pllout_clk);
527*4882a593Smuzhiyun err_free_pllout:
528*4882a593Smuzhiyun kfree(pllout);
529*4882a593Smuzhiyun err_unregister_prediv:
530*4882a593Smuzhiyun clk_unregister(prediv_clk);
531*4882a593Smuzhiyun err_unregister_oscin:
532*4882a593Smuzhiyun clk_unregister(oscin_clk);
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun return ERR_PTR(ret);
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /**
538*4882a593Smuzhiyun * davinci_pll_auxclk_register - Register bypass clock (AUXCLK)
539*4882a593Smuzhiyun * @dev: The PLL platform device or NULL
540*4882a593Smuzhiyun * @name: The clock name
541*4882a593Smuzhiyun * @base: The PLL memory region
542*4882a593Smuzhiyun */
davinci_pll_auxclk_register(struct device * dev,const char * name,void __iomem * base)543*4882a593Smuzhiyun struct clk *davinci_pll_auxclk_register(struct device *dev,
544*4882a593Smuzhiyun const char *name,
545*4882a593Smuzhiyun void __iomem *base)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun return clk_register_gate(dev, name, OSCIN_CLK_NAME, 0, base + CKEN,
548*4882a593Smuzhiyun CKEN_AUXEN_SHIFT, 0, NULL);
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun /**
552*4882a593Smuzhiyun * davinci_pll_sysclkbp_clk_register - Register bypass divider clock (SYSCLKBP)
553*4882a593Smuzhiyun * @dev: The PLL platform device or NULL
554*4882a593Smuzhiyun * @name: The clock name
555*4882a593Smuzhiyun * @base: The PLL memory region
556*4882a593Smuzhiyun */
davinci_pll_sysclkbp_clk_register(struct device * dev,const char * name,void __iomem * base)557*4882a593Smuzhiyun struct clk *davinci_pll_sysclkbp_clk_register(struct device *dev,
558*4882a593Smuzhiyun const char *name,
559*4882a593Smuzhiyun void __iomem *base)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun return clk_register_divider(dev, name, OSCIN_CLK_NAME, 0, base + BPDIV,
562*4882a593Smuzhiyun DIV_RATIO_SHIFT, DIV_RATIO_WIDTH,
563*4882a593Smuzhiyun CLK_DIVIDER_READ_ONLY, NULL);
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun /**
567*4882a593Smuzhiyun * davinci_pll_obsclk_register - Register oscillator divider clock (OBSCLK)
568*4882a593Smuzhiyun * @dev: The PLL platform device or NULL
569*4882a593Smuzhiyun * @info: The clock info
570*4882a593Smuzhiyun * @base: The PLL memory region
571*4882a593Smuzhiyun */
572*4882a593Smuzhiyun struct clk *
davinci_pll_obsclk_register(struct device * dev,const struct davinci_pll_obsclk_info * info,void __iomem * base)573*4882a593Smuzhiyun davinci_pll_obsclk_register(struct device *dev,
574*4882a593Smuzhiyun const struct davinci_pll_obsclk_info *info,
575*4882a593Smuzhiyun void __iomem *base)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun struct clk_mux *mux;
578*4882a593Smuzhiyun struct clk_gate *gate;
579*4882a593Smuzhiyun struct clk_divider *divider;
580*4882a593Smuzhiyun struct clk *clk;
581*4882a593Smuzhiyun u32 oscdiv;
582*4882a593Smuzhiyun int ret;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun mux = kzalloc(sizeof(*mux), GFP_KERNEL);
585*4882a593Smuzhiyun if (!mux)
586*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun mux->reg = base + OCSEL;
589*4882a593Smuzhiyun mux->table = info->table;
590*4882a593Smuzhiyun mux->mask = info->ocsrc_mask;
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun gate = kzalloc(sizeof(*gate), GFP_KERNEL);
593*4882a593Smuzhiyun if (!gate) {
594*4882a593Smuzhiyun ret = -ENOMEM;
595*4882a593Smuzhiyun goto err_free_mux;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun gate->reg = base + CKEN;
599*4882a593Smuzhiyun gate->bit_idx = CKEN_OBSCLK_SHIFT;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun divider = kzalloc(sizeof(*divider), GFP_KERNEL);
602*4882a593Smuzhiyun if (!divider) {
603*4882a593Smuzhiyun ret = -ENOMEM;
604*4882a593Smuzhiyun goto err_free_gate;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun divider->reg = base + OSCDIV;
608*4882a593Smuzhiyun divider->shift = DIV_RATIO_SHIFT;
609*4882a593Smuzhiyun divider->width = DIV_RATIO_WIDTH;
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun /* make sure divider is enabled just in case bootloader disabled it */
612*4882a593Smuzhiyun oscdiv = readl(base + OSCDIV);
613*4882a593Smuzhiyun oscdiv |= BIT(DIV_ENABLE_SHIFT);
614*4882a593Smuzhiyun writel(oscdiv, base + OSCDIV);
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun clk = clk_register_composite(dev, info->name, info->parent_names,
617*4882a593Smuzhiyun info->num_parents,
618*4882a593Smuzhiyun &mux->hw, &clk_mux_ops,
619*4882a593Smuzhiyun ÷r->hw, &clk_divider_ops,
620*4882a593Smuzhiyun &gate->hw, &clk_gate_ops, 0);
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun if (IS_ERR(clk)) {
623*4882a593Smuzhiyun ret = PTR_ERR(clk);
624*4882a593Smuzhiyun goto err_free_divider;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun return clk;
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun err_free_divider:
630*4882a593Smuzhiyun kfree(divider);
631*4882a593Smuzhiyun err_free_gate:
632*4882a593Smuzhiyun kfree(gate);
633*4882a593Smuzhiyun err_free_mux:
634*4882a593Smuzhiyun kfree(mux);
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun return ERR_PTR(ret);
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun /* The PLL SYSCLKn clocks have a mechanism for synchronizing rate changes. */
davinci_pll_sysclk_rate_change(struct notifier_block * nb,unsigned long flags,void * data)640*4882a593Smuzhiyun static int davinci_pll_sysclk_rate_change(struct notifier_block *nb,
641*4882a593Smuzhiyun unsigned long flags, void *data)
642*4882a593Smuzhiyun {
643*4882a593Smuzhiyun struct clk_notifier_data *cnd = data;
644*4882a593Smuzhiyun struct clk_hw *hw = __clk_get_hw(clk_get_parent(cnd->clk));
645*4882a593Smuzhiyun struct davinci_pllen_clk *pll = to_davinci_pllen_clk(hw);
646*4882a593Smuzhiyun u32 pllcmd, pllstat;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun switch (flags) {
649*4882a593Smuzhiyun case POST_RATE_CHANGE:
650*4882a593Smuzhiyun /* apply the changes */
651*4882a593Smuzhiyun pllcmd = readl(pll->base + PLLCMD);
652*4882a593Smuzhiyun pllcmd |= PLLCMD_GOSET;
653*4882a593Smuzhiyun writel(pllcmd, pll->base + PLLCMD);
654*4882a593Smuzhiyun fallthrough;
655*4882a593Smuzhiyun case PRE_RATE_CHANGE:
656*4882a593Smuzhiyun /* Wait until for outstanding changes to take effect */
657*4882a593Smuzhiyun do {
658*4882a593Smuzhiyun pllstat = readl(pll->base + PLLSTAT);
659*4882a593Smuzhiyun } while (pllstat & PLLSTAT_GOSTAT);
660*4882a593Smuzhiyun break;
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun return NOTIFY_OK;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun static struct notifier_block davinci_pll_sysclk_notifier = {
667*4882a593Smuzhiyun .notifier_call = davinci_pll_sysclk_rate_change,
668*4882a593Smuzhiyun };
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun /**
671*4882a593Smuzhiyun * davinci_pll_sysclk_register - Register divider clocks (SYSCLKn)
672*4882a593Smuzhiyun * @dev: The PLL platform device or NULL
673*4882a593Smuzhiyun * @info: The clock info
674*4882a593Smuzhiyun * @base: The PLL memory region
675*4882a593Smuzhiyun */
676*4882a593Smuzhiyun struct clk *
davinci_pll_sysclk_register(struct device * dev,const struct davinci_pll_sysclk_info * info,void __iomem * base)677*4882a593Smuzhiyun davinci_pll_sysclk_register(struct device *dev,
678*4882a593Smuzhiyun const struct davinci_pll_sysclk_info *info,
679*4882a593Smuzhiyun void __iomem *base)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun const struct clk_ops *divider_ops = &clk_divider_ops;
682*4882a593Smuzhiyun struct clk_gate *gate;
683*4882a593Smuzhiyun struct clk_divider *divider;
684*4882a593Smuzhiyun struct clk *clk;
685*4882a593Smuzhiyun u32 reg;
686*4882a593Smuzhiyun u32 flags = 0;
687*4882a593Smuzhiyun int ret;
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun /* PLLDIVn registers are not entirely consecutive */
690*4882a593Smuzhiyun if (info->id < 4)
691*4882a593Smuzhiyun reg = PLLDIV1 + 4 * (info->id - 1);
692*4882a593Smuzhiyun else
693*4882a593Smuzhiyun reg = PLLDIV4 + 4 * (info->id - 4);
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun gate = kzalloc(sizeof(*gate), GFP_KERNEL);
696*4882a593Smuzhiyun if (!gate)
697*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun gate->reg = base + reg;
700*4882a593Smuzhiyun gate->bit_idx = DIV_ENABLE_SHIFT;
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun divider = kzalloc(sizeof(*divider), GFP_KERNEL);
703*4882a593Smuzhiyun if (!divider) {
704*4882a593Smuzhiyun ret = -ENOMEM;
705*4882a593Smuzhiyun goto err_free_gate;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun divider->reg = base + reg;
709*4882a593Smuzhiyun divider->shift = DIV_RATIO_SHIFT;
710*4882a593Smuzhiyun divider->width = info->ratio_width;
711*4882a593Smuzhiyun divider->flags = 0;
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun if (info->flags & SYSCLK_FIXED_DIV) {
714*4882a593Smuzhiyun divider->flags |= CLK_DIVIDER_READ_ONLY;
715*4882a593Smuzhiyun divider_ops = &clk_divider_ro_ops;
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun
718*4882a593Smuzhiyun /* Only the ARM clock can change the parent PLL rate */
719*4882a593Smuzhiyun if (info->flags & SYSCLK_ARM_RATE)
720*4882a593Smuzhiyun flags |= CLK_SET_RATE_PARENT;
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun if (info->flags & SYSCLK_ALWAYS_ENABLED)
723*4882a593Smuzhiyun flags |= CLK_IS_CRITICAL;
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun clk = clk_register_composite(dev, info->name, &info->parent_name, 1,
726*4882a593Smuzhiyun NULL, NULL, ÷r->hw, divider_ops,
727*4882a593Smuzhiyun &gate->hw, &clk_gate_ops, flags);
728*4882a593Smuzhiyun if (IS_ERR(clk)) {
729*4882a593Smuzhiyun ret = PTR_ERR(clk);
730*4882a593Smuzhiyun goto err_free_divider;
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun clk_notifier_register(clk, &davinci_pll_sysclk_notifier);
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun return clk;
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun err_free_divider:
738*4882a593Smuzhiyun kfree(divider);
739*4882a593Smuzhiyun err_free_gate:
740*4882a593Smuzhiyun kfree(gate);
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun return ERR_PTR(ret);
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun
of_davinci_pll_init(struct device * dev,struct device_node * node,const struct davinci_pll_clk_info * info,const struct davinci_pll_obsclk_info * obsclk_info,const struct davinci_pll_sysclk_info ** div_info,u8 max_sysclk_id,void __iomem * base,struct regmap * cfgchip)745*4882a593Smuzhiyun int of_davinci_pll_init(struct device *dev, struct device_node *node,
746*4882a593Smuzhiyun const struct davinci_pll_clk_info *info,
747*4882a593Smuzhiyun const struct davinci_pll_obsclk_info *obsclk_info,
748*4882a593Smuzhiyun const struct davinci_pll_sysclk_info **div_info,
749*4882a593Smuzhiyun u8 max_sysclk_id,
750*4882a593Smuzhiyun void __iomem *base,
751*4882a593Smuzhiyun struct regmap *cfgchip)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun struct device_node *child;
754*4882a593Smuzhiyun const char *parent_name;
755*4882a593Smuzhiyun struct clk *clk;
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun if (info->flags & PLL_HAS_CLKMODE)
758*4882a593Smuzhiyun parent_name = of_clk_get_parent_name(node, 0);
759*4882a593Smuzhiyun else
760*4882a593Smuzhiyun parent_name = OSCIN_CLK_NAME;
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun clk = davinci_pll_clk_register(dev, info, parent_name, base, cfgchip);
763*4882a593Smuzhiyun if (IS_ERR(clk)) {
764*4882a593Smuzhiyun dev_err(dev, "failed to register %s\n", info->name);
765*4882a593Smuzhiyun return PTR_ERR(clk);
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun child = of_get_child_by_name(node, "pllout");
769*4882a593Smuzhiyun if (of_device_is_available(child))
770*4882a593Smuzhiyun of_clk_add_provider(child, of_clk_src_simple_get, clk);
771*4882a593Smuzhiyun of_node_put(child);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun child = of_get_child_by_name(node, "sysclk");
774*4882a593Smuzhiyun if (of_device_is_available(child)) {
775*4882a593Smuzhiyun struct clk_onecell_data *clk_data;
776*4882a593Smuzhiyun struct clk **clks;
777*4882a593Smuzhiyun int n_clks = max_sysclk_id + 1;
778*4882a593Smuzhiyun int i;
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
781*4882a593Smuzhiyun if (!clk_data) {
782*4882a593Smuzhiyun of_node_put(child);
783*4882a593Smuzhiyun return -ENOMEM;
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun clks = kmalloc_array(n_clks, sizeof(*clks), GFP_KERNEL);
787*4882a593Smuzhiyun if (!clks) {
788*4882a593Smuzhiyun kfree(clk_data);
789*4882a593Smuzhiyun of_node_put(child);
790*4882a593Smuzhiyun return -ENOMEM;
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun clk_data->clks = clks;
794*4882a593Smuzhiyun clk_data->clk_num = n_clks;
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun for (i = 0; i < n_clks; i++)
797*4882a593Smuzhiyun clks[i] = ERR_PTR(-ENOENT);
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun for (; *div_info; div_info++) {
800*4882a593Smuzhiyun clk = davinci_pll_sysclk_register(dev, *div_info, base);
801*4882a593Smuzhiyun if (IS_ERR(clk))
802*4882a593Smuzhiyun dev_warn(dev, "failed to register %s (%ld)\n",
803*4882a593Smuzhiyun (*div_info)->name, PTR_ERR(clk));
804*4882a593Smuzhiyun else
805*4882a593Smuzhiyun clks[(*div_info)->id] = clk;
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun of_clk_add_provider(child, of_clk_src_onecell_get, clk_data);
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun of_node_put(child);
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun child = of_get_child_by_name(node, "auxclk");
812*4882a593Smuzhiyun if (of_device_is_available(child)) {
813*4882a593Smuzhiyun char child_name[MAX_NAME_SIZE];
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun snprintf(child_name, MAX_NAME_SIZE, "%s_auxclk", info->name);
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun clk = davinci_pll_auxclk_register(dev, child_name, base);
818*4882a593Smuzhiyun if (IS_ERR(clk))
819*4882a593Smuzhiyun dev_warn(dev, "failed to register %s (%ld)\n",
820*4882a593Smuzhiyun child_name, PTR_ERR(clk));
821*4882a593Smuzhiyun else
822*4882a593Smuzhiyun of_clk_add_provider(child, of_clk_src_simple_get, clk);
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun of_node_put(child);
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun child = of_get_child_by_name(node, "obsclk");
827*4882a593Smuzhiyun if (of_device_is_available(child)) {
828*4882a593Smuzhiyun if (obsclk_info)
829*4882a593Smuzhiyun clk = davinci_pll_obsclk_register(dev, obsclk_info, base);
830*4882a593Smuzhiyun else
831*4882a593Smuzhiyun clk = ERR_PTR(-EINVAL);
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun if (IS_ERR(clk))
834*4882a593Smuzhiyun dev_warn(dev, "failed to register obsclk (%ld)\n",
835*4882a593Smuzhiyun PTR_ERR(clk));
836*4882a593Smuzhiyun else
837*4882a593Smuzhiyun of_clk_add_provider(child, of_clk_src_simple_get, clk);
838*4882a593Smuzhiyun }
839*4882a593Smuzhiyun of_node_put(child);
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun return 0;
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun
davinci_pll_get_pdata(struct device * dev)844*4882a593Smuzhiyun static struct davinci_pll_platform_data *davinci_pll_get_pdata(struct device *dev)
845*4882a593Smuzhiyun {
846*4882a593Smuzhiyun struct davinci_pll_platform_data *pdata = dev_get_platdata(dev);
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun /*
849*4882a593Smuzhiyun * Platform data is optional, so allocate a new struct if one was not
850*4882a593Smuzhiyun * provided. For device tree, this will always be the case.
851*4882a593Smuzhiyun */
852*4882a593Smuzhiyun if (!pdata)
853*4882a593Smuzhiyun pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
854*4882a593Smuzhiyun if (!pdata)
855*4882a593Smuzhiyun return NULL;
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun /* for device tree, we need to fill in the struct */
858*4882a593Smuzhiyun if (dev->of_node)
859*4882a593Smuzhiyun pdata->cfgchip =
860*4882a593Smuzhiyun syscon_regmap_lookup_by_compatible("ti,da830-cfgchip");
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun return pdata;
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun /* needed in early boot for clocksource/clockevent */
866*4882a593Smuzhiyun #ifdef CONFIG_ARCH_DAVINCI_DA850
867*4882a593Smuzhiyun CLK_OF_DECLARE(da850_pll0, "ti,da850-pll0", of_da850_pll0_init);
868*4882a593Smuzhiyun #endif
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun static const struct of_device_id davinci_pll_of_match[] = {
871*4882a593Smuzhiyun #ifdef CONFIG_ARCH_DAVINCI_DA850
872*4882a593Smuzhiyun { .compatible = "ti,da850-pll1", .data = of_da850_pll1_init },
873*4882a593Smuzhiyun #endif
874*4882a593Smuzhiyun { }
875*4882a593Smuzhiyun };
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun static const struct platform_device_id davinci_pll_id_table[] = {
878*4882a593Smuzhiyun #ifdef CONFIG_ARCH_DAVINCI_DA830
879*4882a593Smuzhiyun { .name = "da830-pll", .driver_data = (kernel_ulong_t)da830_pll_init },
880*4882a593Smuzhiyun #endif
881*4882a593Smuzhiyun #ifdef CONFIG_ARCH_DAVINCI_DA850
882*4882a593Smuzhiyun { .name = "da850-pll0", .driver_data = (kernel_ulong_t)da850_pll0_init },
883*4882a593Smuzhiyun { .name = "da850-pll1", .driver_data = (kernel_ulong_t)da850_pll1_init },
884*4882a593Smuzhiyun #endif
885*4882a593Smuzhiyun #ifdef CONFIG_ARCH_DAVINCI_DM355
886*4882a593Smuzhiyun { .name = "dm355-pll1", .driver_data = (kernel_ulong_t)dm355_pll1_init },
887*4882a593Smuzhiyun { .name = "dm355-pll2", .driver_data = (kernel_ulong_t)dm355_pll2_init },
888*4882a593Smuzhiyun #endif
889*4882a593Smuzhiyun #ifdef CONFIG_ARCH_DAVINCI_DM365
890*4882a593Smuzhiyun { .name = "dm365-pll1", .driver_data = (kernel_ulong_t)dm365_pll1_init },
891*4882a593Smuzhiyun { .name = "dm365-pll2", .driver_data = (kernel_ulong_t)dm365_pll2_init },
892*4882a593Smuzhiyun #endif
893*4882a593Smuzhiyun #ifdef CONFIG_ARCH_DAVINCI_DM644x
894*4882a593Smuzhiyun { .name = "dm644x-pll1", .driver_data = (kernel_ulong_t)dm644x_pll1_init },
895*4882a593Smuzhiyun { .name = "dm644x-pll2", .driver_data = (kernel_ulong_t)dm644x_pll2_init },
896*4882a593Smuzhiyun #endif
897*4882a593Smuzhiyun #ifdef CONFIG_ARCH_DAVINCI_DM646x
898*4882a593Smuzhiyun { .name = "dm646x-pll1", .driver_data = (kernel_ulong_t)dm646x_pll1_init },
899*4882a593Smuzhiyun { .name = "dm646x-pll2", .driver_data = (kernel_ulong_t)dm646x_pll2_init },
900*4882a593Smuzhiyun #endif
901*4882a593Smuzhiyun { }
902*4882a593Smuzhiyun };
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun typedef int (*davinci_pll_init)(struct device *dev, void __iomem *base,
905*4882a593Smuzhiyun struct regmap *cfgchip);
906*4882a593Smuzhiyun
davinci_pll_probe(struct platform_device * pdev)907*4882a593Smuzhiyun static int davinci_pll_probe(struct platform_device *pdev)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun struct device *dev = &pdev->dev;
910*4882a593Smuzhiyun struct davinci_pll_platform_data *pdata;
911*4882a593Smuzhiyun const struct of_device_id *of_id;
912*4882a593Smuzhiyun davinci_pll_init pll_init = NULL;
913*4882a593Smuzhiyun void __iomem *base;
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun of_id = of_match_device(davinci_pll_of_match, dev);
916*4882a593Smuzhiyun if (of_id)
917*4882a593Smuzhiyun pll_init = of_id->data;
918*4882a593Smuzhiyun else if (pdev->id_entry)
919*4882a593Smuzhiyun pll_init = (void *)pdev->id_entry->driver_data;
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun if (!pll_init) {
922*4882a593Smuzhiyun dev_err(dev, "unable to find driver data\n");
923*4882a593Smuzhiyun return -EINVAL;
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun pdata = davinci_pll_get_pdata(dev);
927*4882a593Smuzhiyun if (!pdata) {
928*4882a593Smuzhiyun dev_err(dev, "missing platform data\n");
929*4882a593Smuzhiyun return -EINVAL;
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun base = devm_platform_ioremap_resource(pdev, 0);
933*4882a593Smuzhiyun if (IS_ERR(base))
934*4882a593Smuzhiyun return PTR_ERR(base);
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun return pll_init(dev, base, pdata->cfgchip);
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun static struct platform_driver davinci_pll_driver = {
940*4882a593Smuzhiyun .probe = davinci_pll_probe,
941*4882a593Smuzhiyun .driver = {
942*4882a593Smuzhiyun .name = "davinci-pll-clk",
943*4882a593Smuzhiyun .of_match_table = davinci_pll_of_match,
944*4882a593Smuzhiyun },
945*4882a593Smuzhiyun .id_table = davinci_pll_id_table,
946*4882a593Smuzhiyun };
947*4882a593Smuzhiyun
davinci_pll_driver_init(void)948*4882a593Smuzhiyun static int __init davinci_pll_driver_init(void)
949*4882a593Smuzhiyun {
950*4882a593Smuzhiyun return platform_driver_register(&davinci_pll_driver);
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun /* has to be postcore_initcall because PSC devices depend on PLL parent clocks */
954*4882a593Smuzhiyun postcore_initcall(davinci_pll_driver_init);
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
957*4882a593Smuzhiyun #include <linux/debugfs.h>
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun #define DEBUG_REG(n) \
960*4882a593Smuzhiyun { \
961*4882a593Smuzhiyun .name = #n, \
962*4882a593Smuzhiyun .offset = n, \
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun static const struct debugfs_reg32 davinci_pll_regs[] = {
966*4882a593Smuzhiyun DEBUG_REG(REVID),
967*4882a593Smuzhiyun DEBUG_REG(PLLCTL),
968*4882a593Smuzhiyun DEBUG_REG(OCSEL),
969*4882a593Smuzhiyun DEBUG_REG(PLLSECCTL),
970*4882a593Smuzhiyun DEBUG_REG(PLLM),
971*4882a593Smuzhiyun DEBUG_REG(PREDIV),
972*4882a593Smuzhiyun DEBUG_REG(PLLDIV1),
973*4882a593Smuzhiyun DEBUG_REG(PLLDIV2),
974*4882a593Smuzhiyun DEBUG_REG(PLLDIV3),
975*4882a593Smuzhiyun DEBUG_REG(OSCDIV),
976*4882a593Smuzhiyun DEBUG_REG(POSTDIV),
977*4882a593Smuzhiyun DEBUG_REG(BPDIV),
978*4882a593Smuzhiyun DEBUG_REG(PLLCMD),
979*4882a593Smuzhiyun DEBUG_REG(PLLSTAT),
980*4882a593Smuzhiyun DEBUG_REG(ALNCTL),
981*4882a593Smuzhiyun DEBUG_REG(DCHANGE),
982*4882a593Smuzhiyun DEBUG_REG(CKEN),
983*4882a593Smuzhiyun DEBUG_REG(CKSTAT),
984*4882a593Smuzhiyun DEBUG_REG(SYSTAT),
985*4882a593Smuzhiyun DEBUG_REG(PLLDIV4),
986*4882a593Smuzhiyun DEBUG_REG(PLLDIV5),
987*4882a593Smuzhiyun DEBUG_REG(PLLDIV6),
988*4882a593Smuzhiyun DEBUG_REG(PLLDIV7),
989*4882a593Smuzhiyun DEBUG_REG(PLLDIV8),
990*4882a593Smuzhiyun DEBUG_REG(PLLDIV9),
991*4882a593Smuzhiyun };
992*4882a593Smuzhiyun
davinci_pll_debug_init(struct clk_hw * hw,struct dentry * dentry)993*4882a593Smuzhiyun static void davinci_pll_debug_init(struct clk_hw *hw, struct dentry *dentry)
994*4882a593Smuzhiyun {
995*4882a593Smuzhiyun struct davinci_pll_clk *pll = to_davinci_pll_clk(hw);
996*4882a593Smuzhiyun struct debugfs_regset32 *regset;
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun regset = kzalloc(sizeof(*regset), GFP_KERNEL);
999*4882a593Smuzhiyun if (!regset)
1000*4882a593Smuzhiyun return;
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun regset->regs = davinci_pll_regs;
1003*4882a593Smuzhiyun regset->nregs = ARRAY_SIZE(davinci_pll_regs);
1004*4882a593Smuzhiyun regset->base = pll->base;
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun debugfs_create_regset32("registers", 0400, dentry, regset);
1007*4882a593Smuzhiyun }
1008*4882a593Smuzhiyun #endif
1009