2 * OMAP APLL clock support
4 * Copyright (C) 2013 Texas Instruments, Inc.
6 * J Keerthy <j-keerthy@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/clk-provider.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/string.h>
24 #include <linux/log2.h>
26 #include <linux/of_address.h>
27 #include <linux/clk/ti.h>
28 #include <linux/delay.h>
30 #define APLL_FORCE_LOCK 0x1
31 #define APLL_AUTO_IDLE 0x2
32 #define MAX_APLL_WAIT_TRIES 1000000
35 #define pr_fmt(fmt) "%s: " fmt, __func__
37 static int dra7_apll_enable(struct clk_hw
*hw
)
39 struct clk_hw_omap
*clk
= to_clk_hw_omap(hw
);
50 clk_name
= __clk_get_name(clk
->hw
.clk
);
52 state
<<= __ffs(ad
->idlest_mask
);
54 /* Check is already locked */
55 v
= ti_clk_ll_ops
->clk_readl(ad
->idlest_reg
);
57 if ((v
& ad
->idlest_mask
) == state
)
60 v
= ti_clk_ll_ops
->clk_readl(ad
->control_reg
);
61 v
&= ~ad
->enable_mask
;
62 v
|= APLL_FORCE_LOCK
<< __ffs(ad
->enable_mask
);
63 ti_clk_ll_ops
->clk_writel(v
, ad
->control_reg
);
65 state
<<= __ffs(ad
->idlest_mask
);
68 v
= ti_clk_ll_ops
->clk_readl(ad
->idlest_reg
);
69 if ((v
& ad
->idlest_mask
) == state
)
71 if (i
> MAX_APLL_WAIT_TRIES
)
77 if (i
== MAX_APLL_WAIT_TRIES
) {
78 pr_warn("clock: %s failed transition to '%s'\n",
79 clk_name
, (state
) ? "locked" : "bypassed");
81 pr_debug("clock: %s transition to '%s' in %d loops\n",
82 clk_name
, (state
) ? "locked" : "bypassed", i
);
90 static void dra7_apll_disable(struct clk_hw
*hw
)
92 struct clk_hw_omap
*clk
= to_clk_hw_omap(hw
);
99 state
<<= __ffs(ad
->idlest_mask
);
101 v
= ti_clk_ll_ops
->clk_readl(ad
->control_reg
);
102 v
&= ~ad
->enable_mask
;
103 v
|= APLL_AUTO_IDLE
<< __ffs(ad
->enable_mask
);
104 ti_clk_ll_ops
->clk_writel(v
, ad
->control_reg
);
107 static int dra7_apll_is_enabled(struct clk_hw
*hw
)
109 struct clk_hw_omap
*clk
= to_clk_hw_omap(hw
);
110 struct dpll_data
*ad
;
115 v
= ti_clk_ll_ops
->clk_readl(ad
->control_reg
);
116 v
&= ad
->enable_mask
;
118 v
>>= __ffs(ad
->enable_mask
);
120 return v
== APLL_AUTO_IDLE
? 0 : 1;
123 static u8
dra7_init_apll_parent(struct clk_hw
*hw
)
128 static const struct clk_ops apll_ck_ops
= {
129 .enable
= &dra7_apll_enable
,
130 .disable
= &dra7_apll_disable
,
131 .is_enabled
= &dra7_apll_is_enabled
,
132 .get_parent
= &dra7_init_apll_parent
,
135 static void __init
omap_clk_register_apll(struct clk_hw
*hw
,
136 struct device_node
*node
)
138 struct clk_hw_omap
*clk_hw
= to_clk_hw_omap(hw
);
139 struct dpll_data
*ad
= clk_hw
->dpll_data
;
142 ad
->clk_ref
= of_clk_get(node
, 0);
143 ad
->clk_bypass
= of_clk_get(node
, 1);
145 if (IS_ERR(ad
->clk_ref
) || IS_ERR(ad
->clk_bypass
)) {
146 pr_debug("clk-ref or clk-bypass for %s not ready, retry\n",
148 if (!ti_clk_retry_init(node
, hw
, omap_clk_register_apll
))
154 clk
= clk_register(NULL
, &clk_hw
->hw
);
156 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
157 kfree(clk_hw
->hw
.init
->parent_names
);
158 kfree(clk_hw
->hw
.init
);
163 kfree(clk_hw
->dpll_data
);
164 kfree(clk_hw
->hw
.init
->parent_names
);
165 kfree(clk_hw
->hw
.init
);
169 static void __init
of_dra7_apll_setup(struct device_node
*node
)
171 struct dpll_data
*ad
= NULL
;
172 struct clk_hw_omap
*clk_hw
= NULL
;
173 struct clk_init_data
*init
= NULL
;
174 const char **parent_names
= NULL
;
177 ad
= kzalloc(sizeof(*ad
), GFP_KERNEL
);
178 clk_hw
= kzalloc(sizeof(*clk_hw
), GFP_KERNEL
);
179 init
= kzalloc(sizeof(*init
), GFP_KERNEL
);
180 if (!ad
|| !clk_hw
|| !init
)
183 clk_hw
->dpll_data
= ad
;
184 clk_hw
->hw
.init
= init
;
185 clk_hw
->flags
= MEMMAP_ADDRESSING
;
187 init
->name
= node
->name
;
188 init
->ops
= &apll_ck_ops
;
190 init
->num_parents
= of_clk_get_parent_count(node
);
191 if (init
->num_parents
< 1) {
192 pr_err("dra7 apll %s must have parent(s)\n", node
->name
);
196 parent_names
= kzalloc(sizeof(char *) * init
->num_parents
, GFP_KERNEL
);
200 for (i
= 0; i
< init
->num_parents
; i
++)
201 parent_names
[i
] = of_clk_get_parent_name(node
, i
);
203 init
->parent_names
= parent_names
;
205 ad
->control_reg
= ti_clk_get_reg_addr(node
, 0);
206 ad
->idlest_reg
= ti_clk_get_reg_addr(node
, 1);
208 if (!ad
->control_reg
|| !ad
->idlest_reg
)
211 ad
->idlest_mask
= 0x1;
212 ad
->enable_mask
= 0x3;
214 omap_clk_register_apll(&clk_hw
->hw
, node
);
223 CLK_OF_DECLARE(dra7_apll_clock
, "ti,dra7-apll-clock", of_dra7_apll_setup
);