blob: 9999947694509d5867a09fd2903205af350b6814 [file] [log] [blame]
Laurent Pinchartabe844a2013-10-17 23:54:07 +02001/*
2 * r8a7790 Common Clock Framework support
3 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 *
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 */
12
13#include <linux/clk-provider.h>
Laurent Pinchartabe844a2013-10-17 23:54:07 +020014#include <linux/init.h>
15#include <linux/io.h>
16#include <linux/kernel.h>
17#include <linux/of.h>
18#include <linux/of_address.h>
Geert Uytterhoeven5a1cfaf2015-06-23 15:09:27 +020019#include <linux/slab.h>
Laurent Pinchartabe844a2013-10-17 23:54:07 +020020
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +020021#include "clk-div6.h"
22
Laurent Pinchartabe844a2013-10-17 23:54:07 +020023#define CPG_DIV6_CKSTP BIT(8)
24#define CPG_DIV6_DIV(d) ((d) & 0x3f)
25#define CPG_DIV6_DIV_MASK 0x3f
26
27/**
Wolfram Sang95aa4f92014-02-24 20:57:11 +010028 * struct div6_clock - CPG 6 bit divider clock
Laurent Pinchartabe844a2013-10-17 23:54:07 +020029 * @hw: handle between common and hardware-specific interfaces
30 * @reg: IO-remapped register
31 * @div: divisor value (1-64)
32 */
33struct div6_clock {
34 struct clk_hw hw;
35 void __iomem *reg;
36 unsigned int div;
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +010037 u32 src_shift;
38 u32 src_width;
39 u8 *parents;
Laurent Pinchartabe844a2013-10-17 23:54:07 +020040};
41
42#define to_div6_clock(_hw) container_of(_hw, struct div6_clock, hw)
43
44static int cpg_div6_clock_enable(struct clk_hw *hw)
45{
46 struct div6_clock *clock = to_div6_clock(hw);
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +010047 u32 val;
Laurent Pinchartabe844a2013-10-17 23:54:07 +020048
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +010049 val = (clk_readl(clock->reg) & ~(CPG_DIV6_DIV_MASK | CPG_DIV6_CKSTP))
50 | CPG_DIV6_DIV(clock->div - 1);
51 clk_writel(val, clock->reg);
Laurent Pinchartabe844a2013-10-17 23:54:07 +020052
53 return 0;
54}
55
56static void cpg_div6_clock_disable(struct clk_hw *hw)
57{
58 struct div6_clock *clock = to_div6_clock(hw);
Geert Uytterhoeven7980a862014-11-24 15:57:59 +010059 u32 val;
Laurent Pinchartabe844a2013-10-17 23:54:07 +020060
Geert Uytterhoeven7980a862014-11-24 15:57:59 +010061 val = clk_readl(clock->reg);
62 val |= CPG_DIV6_CKSTP;
63 /*
64 * DIV6 clocks require the divisor field to be non-zero when stopping
65 * the clock. However, some clocks (e.g. ZB on sh73a0) fail to be
66 * re-enabled later if the divisor field is changed when stopping the
67 * clock
Laurent Pinchartabe844a2013-10-17 23:54:07 +020068 */
Geert Uytterhoeven7980a862014-11-24 15:57:59 +010069 if (!(val & CPG_DIV6_DIV_MASK))
70 val |= CPG_DIV6_DIV_MASK;
71 clk_writel(val, clock->reg);
Laurent Pinchartabe844a2013-10-17 23:54:07 +020072}
73
74static int cpg_div6_clock_is_enabled(struct clk_hw *hw)
75{
76 struct div6_clock *clock = to_div6_clock(hw);
77
78 return !(clk_readl(clock->reg) & CPG_DIV6_CKSTP);
79}
80
81static unsigned long cpg_div6_clock_recalc_rate(struct clk_hw *hw,
82 unsigned long parent_rate)
83{
84 struct div6_clock *clock = to_div6_clock(hw);
85 unsigned int div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
86
87 return parent_rate / div;
88}
89
90static unsigned int cpg_div6_clock_calc_div(unsigned long rate,
91 unsigned long parent_rate)
92{
93 unsigned int div;
94
Geert Uytterhoeven5469d4f2015-02-04 13:27:21 +010095 if (!rate)
96 rate = 1;
97
Laurent Pinchartabe844a2013-10-17 23:54:07 +020098 div = DIV_ROUND_CLOSEST(parent_rate, rate);
99 return clamp_t(unsigned int, div, 1, 64);
100}
101
102static long cpg_div6_clock_round_rate(struct clk_hw *hw, unsigned long rate,
103 unsigned long *parent_rate)
104{
105 unsigned int div = cpg_div6_clock_calc_div(rate, *parent_rate);
106
107 return *parent_rate / div;
108}
109
110static int cpg_div6_clock_set_rate(struct clk_hw *hw, unsigned long rate,
111 unsigned long parent_rate)
112{
113 struct div6_clock *clock = to_div6_clock(hw);
114 unsigned int div = cpg_div6_clock_calc_div(rate, parent_rate);
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100115 u32 val;
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200116
117 clock->div = div;
118
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100119 val = clk_readl(clock->reg) & ~CPG_DIV6_DIV_MASK;
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200120 /* Only program the new divisor if the clock isn't stopped. */
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100121 if (!(val & CPG_DIV6_CKSTP))
122 clk_writel(val | CPG_DIV6_DIV(clock->div - 1), clock->reg);
123
124 return 0;
125}
126
127static u8 cpg_div6_clock_get_parent(struct clk_hw *hw)
128{
129 struct div6_clock *clock = to_div6_clock(hw);
130 unsigned int i;
131 u8 hw_index;
132
133 if (clock->src_width == 0)
134 return 0;
135
136 hw_index = (clk_readl(clock->reg) >> clock->src_shift) &
137 (BIT(clock->src_width) - 1);
Stephen Boyd497295a2015-06-25 16:53:23 -0700138 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100139 if (clock->parents[i] == hw_index)
140 return i;
141 }
142
143 pr_err("%s: %s DIV6 clock set to invalid parent %u\n",
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700144 __func__, clk_hw_get_name(hw), hw_index);
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100145 return 0;
146}
147
148static int cpg_div6_clock_set_parent(struct clk_hw *hw, u8 index)
149{
150 struct div6_clock *clock = to_div6_clock(hw);
151 u8 hw_index;
152 u32 mask;
153
Stephen Boyd497295a2015-06-25 16:53:23 -0700154 if (index >= clk_hw_get_num_parents(hw))
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100155 return -EINVAL;
156
157 mask = ~((BIT(clock->src_width) - 1) << clock->src_shift);
158 hw_index = clock->parents[index];
159
160 clk_writel((clk_readl(clock->reg) & mask) |
161 (hw_index << clock->src_shift), clock->reg);
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200162
163 return 0;
164}
165
166static const struct clk_ops cpg_div6_clock_ops = {
167 .enable = cpg_div6_clock_enable,
168 .disable = cpg_div6_clock_disable,
169 .is_enabled = cpg_div6_clock_is_enabled,
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100170 .get_parent = cpg_div6_clock_get_parent,
171 .set_parent = cpg_div6_clock_set_parent,
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200172 .recalc_rate = cpg_div6_clock_recalc_rate,
173 .round_rate = cpg_div6_clock_round_rate,
174 .set_rate = cpg_div6_clock_set_rate,
175};
176
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +0200177
178/**
179 * cpg_div6_register - Register a DIV6 clock
180 * @name: Name of the DIV6 clock
181 * @num_parents: Number of parent clocks of the DIV6 clock (1, 4, or 8)
182 * @parent_names: Array containing the names of the parent clocks
183 * @reg: Mapped register used to control the DIV6 clock
184 */
185struct clk * __init cpg_div6_register(const char *name,
186 unsigned int num_parents,
187 const char **parent_names,
188 void __iomem *reg)
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200189{
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +0200190 unsigned int valid_parents;
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200191 struct clk_init_data init;
192 struct div6_clock *clock;
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200193 struct clk *clk;
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100194 unsigned int i;
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200195
196 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100197 if (!clock)
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +0200198 return ERR_PTR(-ENOMEM);
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200199
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100200 clock->parents = kmalloc_array(num_parents, sizeof(*clock->parents),
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +0200201 GFP_KERNEL);
202 if (!clock->parents) {
203 clk = ERR_PTR(-ENOMEM);
204 goto free_clock;
205 }
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100206
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +0200207 clock->reg = reg;
208
209 /*
210 * Read the divisor. Disabling the clock overwrites the divisor, so we
211 * need to cache its value for the enable operation.
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200212 */
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200213 clock->div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
214
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100215 switch (num_parents) {
216 case 1:
217 /* fixed parent clock */
218 clock->src_shift = clock->src_width = 0;
219 break;
220 case 4:
221 /* clock with EXSRC bits 6-7 */
222 clock->src_shift = 6;
223 clock->src_width = 2;
224 break;
225 case 8:
226 /* VCLK with EXSRC bits 12-14 */
227 clock->src_shift = 12;
228 clock->src_width = 3;
229 break;
230 default:
231 pr_err("%s: invalid number of parents for DIV6 clock %s\n",
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +0200232 __func__, name);
233 clk = ERR_PTR(-EINVAL);
234 goto free_parents;
235 }
236
237 /* Filter out invalid parents */
238 for (i = 0, valid_parents = 0; i < num_parents; i++) {
239 if (parent_names[i]) {
240 parent_names[valid_parents] = parent_names[i];
241 clock->parents[valid_parents] = i;
242 valid_parents++;
243 }
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200244 }
245
246 /* Register the clock. */
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +0200247 init.name = name;
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200248 init.ops = &cpg_div6_clock_ops;
249 init.flags = CLK_IS_BASIC;
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100250 init.parent_names = parent_names;
251 init.num_parents = valid_parents;
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200252
253 clock->hw.init = &init;
254
255 clk = clk_register(NULL, &clock->hw);
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +0200256 if (IS_ERR(clk))
257 goto free_parents;
258
259 return clk;
260
261free_parents:
262 kfree(clock->parents);
263free_clock:
264 kfree(clock);
265 return clk;
266}
267
268static void __init cpg_div6_clock_init(struct device_node *np)
269{
270 unsigned int num_parents;
271 const char **parent_names;
272 const char *clk_name = np->name;
273 void __iomem *reg;
274 struct clk *clk;
275 unsigned int i;
276
277 num_parents = of_clk_get_parent_count(np);
278 if (num_parents < 1) {
279 pr_err("%s: no parent found for %s DIV6 clock\n",
280 __func__, np->name);
281 return;
282 }
283
284 parent_names = kmalloc_array(num_parents, sizeof(*parent_names),
285 GFP_KERNEL);
286 if (!parent_names)
287 return;
288
289 reg = of_iomap(np, 0);
290 if (reg == NULL) {
291 pr_err("%s: failed to map %s DIV6 clock register\n",
292 __func__, np->name);
293 goto error;
294 }
295
296 /* Parse the DT properties. */
297 of_property_read_string(np, "clock-output-names", &clk_name);
298
299 for (i = 0; i < num_parents; i++)
300 parent_names[i] = of_clk_get_parent_name(np, i);
301
302 clk = cpg_div6_register(clk_name, num_parents, parent_names, reg);
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200303 if (IS_ERR(clk)) {
304 pr_err("%s: failed to register %s DIV6 clock (%ld)\n",
305 __func__, np->name, PTR_ERR(clk));
306 goto error;
307 }
308
309 of_clk_add_provider(np, of_clk_src_simple_get, clk);
310
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100311 kfree(parent_names);
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200312 return;
313
314error:
Geert Uytterhoeven1fae91e2015-10-16 11:41:19 +0200315 if (reg)
316 iounmap(reg);
Ulrich Hechtc6d67fb2014-11-07 16:51:07 +0100317 kfree(parent_names);
Laurent Pinchartabe844a2013-10-17 23:54:07 +0200318}
319CLK_OF_DECLARE(cpg_div6_clk, "renesas,cpg-div6-clock", cpg_div6_clock_init);