blob: 53646facda45fe246df6b6eb264e896a527441fa [file] [log] [blame]
Paul Walmsleydf791b32010-01-26 20:13:04 -07001/*
2 * clkt_clksel.c - OMAP2/3/4 clksel clock functions
3 *
4 * Copyright (C) 2005-2008 Texas Instruments, Inc.
5 * Copyright (C) 2004-2010 Nokia Corporation
6 *
7 * Contacts:
8 * Richard Woodruff <r-woodruff2@ti.com>
9 * Paul Walmsley
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
Paul Walmsley435699d2010-05-18 18:40:24 -060015 *
16 * clksel clocks are clocks that do not have a fixed parent, or that
17 * can divide their parent's rate, or possibly both at the same time, based
18 * on the contents of a hardware register bitfield.
19 *
20 * All of the various mux and divider settings can be encoded into
21 * struct clksel* data structures, and then these can be autogenerated
22 * from some hardware database for each new chip generation. This
23 * should avoid the need to write, review, and validate a lot of new
24 * clock code for each new chip, since it can be exported from the SoC
25 * design flow. This is now done on OMAP4.
26 *
27 * The fusion of mux and divider clocks is a software creation. In
28 * hardware reality, the multiplexer (parent selection) and the
29 * divider exist separately. XXX At some point these clksel clocks
30 * should be split into "divider" clocks and "mux" clocks to better
31 * match the hardware.
32 *
33 * (The name "clksel" comes from the name of the corresponding
34 * register field in the OMAP2/3 family of SoCs.)
Paul Walmsleydf791b32010-01-26 20:13:04 -070035 *
36 * XXX Currently these clocks are only used in the OMAP2/3/4 code, but
37 * many of the OMAP1 clocks should be convertible to use this
38 * mechanism.
39 */
40#undef DEBUG
41
42#include <linux/kernel.h>
43#include <linux/errno.h>
44#include <linux/clk.h>
45#include <linux/io.h>
Tony Lindgrend9a5f4d2012-03-07 17:28:01 -080046#include <linux/bug.h>
Paul Walmsleydf791b32010-01-26 20:13:04 -070047
Paul Walmsleydf791b32010-01-26 20:13:04 -070048#include "clock.h"
Paul Walmsleydf791b32010-01-26 20:13:04 -070049
50/* Private functions */
51
52/**
Paul Walmsley435699d2010-05-18 18:40:24 -060053 * _get_clksel_by_parent() - return clksel struct for a given clk & parent
Paul Walmsleydf791b32010-01-26 20:13:04 -070054 * @clk: OMAP struct clk ptr to inspect
55 * @src_clk: OMAP struct clk ptr of the parent clk to search for
56 *
57 * Scan the struct clksel array associated with the clock to find
58 * the element associated with the supplied parent clock address.
59 * Returns a pointer to the struct clksel on success or NULL on error.
60 */
Paul Walmsley435699d2010-05-18 18:40:24 -060061static const struct clksel *_get_clksel_by_parent(struct clk *clk,
62 struct clk *src_clk)
Paul Walmsleydf791b32010-01-26 20:13:04 -070063{
64 const struct clksel *clks;
65
Paul Walmsley435699d2010-05-18 18:40:24 -060066 for (clks = clk->clksel; clks->parent; clks++)
Paul Walmsleydf791b32010-01-26 20:13:04 -070067 if (clks->parent == src_clk)
68 break; /* Found the requested parent */
Paul Walmsleydf791b32010-01-26 20:13:04 -070069
70 if (!clks->parent) {
Paul Walmsley435699d2010-05-18 18:40:24 -060071 /* This indicates a data problem */
Paul Walmsley7852ec02012-07-26 00:54:26 -060072 WARN(1, "clock: %s: could not find parent clock %s in clksel array\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060073 __clk_get_name(clk), __clk_get_name(src_clk));
Paul Walmsleydf791b32010-01-26 20:13:04 -070074 return NULL;
75 }
76
77 return clks;
78}
79
Paul Walmsleyd74b4942010-05-18 18:40:24 -060080/**
Paul Walmsley435699d2010-05-18 18:40:24 -060081 * _get_div_and_fieldval() - find the new clksel divisor and field value to use
Paul Walmsleyd74b4942010-05-18 18:40:24 -060082 * @src_clk: planned new parent struct clk *
83 * @clk: struct clk * that is being reparented
84 * @field_val: pointer to a u32 to contain the register data for the divisor
85 *
86 * Given an intended new parent struct clk * @src_clk, and the struct
87 * clk * @clk to the clock that is being reparented, find the
88 * appropriate rate divisor for the new clock (returned as the return
89 * value), and the corresponding register bitfield data to program to
90 * reach that divisor (returned in the u32 pointed to by @field_val).
91 * Returns 0 on error, or returns the newly-selected divisor upon
92 * success (in this latter case, the corresponding register bitfield
93 * value is passed back in the variable pointed to by @field_val)
Paul Walmsleydf791b32010-01-26 20:13:04 -070094 */
Paul Walmsley435699d2010-05-18 18:40:24 -060095static u8 _get_div_and_fieldval(struct clk *src_clk, struct clk *clk,
96 u32 *field_val)
Paul Walmsleydf791b32010-01-26 20:13:04 -070097{
98 const struct clksel *clks;
Felipe Balbi405505c2011-01-16 13:22:03 +020099 const struct clksel_rate *clkr, *max_clkr = NULL;
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600100 u8 max_div = 0;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700101
Paul Walmsley435699d2010-05-18 18:40:24 -0600102 clks = _get_clksel_by_parent(clk, src_clk);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700103 if (!clks)
104 return 0;
105
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600106 /*
107 * Find the highest divisor (e.g., the one resulting in the
108 * lowest rate) to use as the default. This should avoid
109 * clock rates that are too high for the device. XXX A better
110 * solution here would be to try to determine if there is a
111 * divisor matching the original clock rate before the parent
112 * switch, and if it cannot be found, to fall back to the
113 * highest divisor.
114 */
Paul Walmsleydf791b32010-01-26 20:13:04 -0700115 for (clkr = clks->rates; clkr->div; clkr++) {
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600116 if (!(clkr->flags & cpu_mask))
117 continue;
118
119 if (clkr->div > max_div) {
120 max_div = clkr->div;
121 max_clkr = clkr;
122 }
Paul Walmsleydf791b32010-01-26 20:13:04 -0700123 }
124
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600125 if (max_div == 0) {
Paul Walmsley435699d2010-05-18 18:40:24 -0600126 /* This indicates an error in the clksel data */
Paul Walmsley7852ec02012-07-26 00:54:26 -0600127 WARN(1, "clock: %s: could not find divisor for parent %s\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600128 __clk_get_name(clk),
129 __clk_get_name(__clk_get_parent(src_clk)));
Paul Walmsleydf791b32010-01-26 20:13:04 -0700130 return 0;
131 }
132
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600133 *field_val = max_clkr->val;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700134
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600135 return max_div;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700136}
137
Paul Walmsley435699d2010-05-18 18:40:24 -0600138/**
139 * _write_clksel_reg() - program a clock's clksel register in hardware
140 * @clk: struct clk * to program
141 * @v: clksel bitfield value to program (with LSB at bit 0)
142 *
143 * Shift the clksel register bitfield value @v to its appropriate
144 * location in the clksel register and write it in. This function
145 * will ensure that the write to the clksel_reg reaches its
146 * destination before returning -- important since PRM and CM register
147 * accesses can be quite slow compared to ARM cycles -- but does not
148 * take into account any time the hardware might take to switch the
149 * clock source.
150 */
151static void _write_clksel_reg(struct clk *clk, u32 field_val)
152{
153 u32 v;
154
155 v = __raw_readl(clk->clksel_reg);
156 v &= ~clk->clksel_mask;
157 v |= field_val << __ffs(clk->clksel_mask);
158 __raw_writel(v, clk->clksel_reg);
159
160 v = __raw_readl(clk->clksel_reg); /* OCP barrier */
161}
162
163/**
164 * _clksel_to_divisor() - turn clksel field value into integer divider
165 * @clk: OMAP struct clk to use
166 * @field_val: register field value to find
167 *
168 * Given a struct clk of a rate-selectable clksel clock, and a register field
169 * value to search for, find the corresponding clock divisor. The register
170 * field value should be pre-masked and shifted down so the LSB is at bit 0
171 * before calling. Returns 0 on error or returns the actual integer divisor
172 * upon success.
173 */
174static u32 _clksel_to_divisor(struct clk *clk, u32 field_val)
175{
176 const struct clksel *clks;
177 const struct clksel_rate *clkr;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600178 struct clk *parent;
Paul Walmsley435699d2010-05-18 18:40:24 -0600179
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600180 parent = __clk_get_parent(clk);
181 clks = _get_clksel_by_parent(clk, parent);
Paul Walmsley435699d2010-05-18 18:40:24 -0600182 if (!clks)
183 return 0;
184
185 for (clkr = clks->rates; clkr->div; clkr++) {
186 if (!(clkr->flags & cpu_mask))
187 continue;
188
189 if (clkr->val == field_val)
190 break;
191 }
192
193 if (!clkr->div) {
194 /* This indicates a data error */
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600195 WARN(1, "clock: %s: could not find fieldval %d for parent %s\n",
196 __clk_get_name(clk), field_val, __clk_get_name(parent));
Paul Walmsley435699d2010-05-18 18:40:24 -0600197 return 0;
198 }
199
200 return clkr->div;
201}
202
203/**
204 * _divisor_to_clksel() - turn clksel integer divisor into a field value
205 * @clk: OMAP struct clk to use
206 * @div: integer divisor to search for
207 *
208 * Given a struct clk of a rate-selectable clksel clock, and a clock
209 * divisor, find the corresponding register field value. Returns the
210 * register field value _before_ left-shifting (i.e., LSB is at bit
211 * 0); or returns 0xFFFFFFFF (~0) upon error.
212 */
213static u32 _divisor_to_clksel(struct clk *clk, u32 div)
214{
215 const struct clksel *clks;
216 const struct clksel_rate *clkr;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600217 struct clk *parent;
Paul Walmsley435699d2010-05-18 18:40:24 -0600218
219 /* should never happen */
220 WARN_ON(div == 0);
221
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600222 parent = __clk_get_parent(clk);
223 clks = _get_clksel_by_parent(clk, parent);
Paul Walmsley435699d2010-05-18 18:40:24 -0600224 if (!clks)
225 return ~0;
226
227 for (clkr = clks->rates; clkr->div; clkr++) {
228 if (!(clkr->flags & cpu_mask))
229 continue;
230
231 if (clkr->div == div)
232 break;
233 }
234
235 if (!clkr->div) {
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600236 pr_err("clock: %s: could not find divisor %d for parent %s\n",
237 __clk_get_name(clk), div, __clk_get_name(parent));
Paul Walmsley435699d2010-05-18 18:40:24 -0600238 return ~0;
239 }
240
241 return clkr->val;
242}
243
244/**
245 * _read_divisor() - get current divisor applied to parent clock (from hdwr)
246 * @clk: OMAP struct clk to use.
247 *
248 * Read the current divisor register value for @clk that is programmed
249 * into the hardware, convert it into the actual divisor value, and
250 * return it; or return 0 on error.
251 */
252static u32 _read_divisor(struct clk *clk)
253{
254 u32 v;
255
256 if (!clk->clksel || !clk->clksel_mask)
257 return 0;
258
259 v = __raw_readl(clk->clksel_reg);
260 v &= clk->clksel_mask;
261 v >>= __ffs(clk->clksel_mask);
262
263 return _clksel_to_divisor(clk, v);
264}
Paul Walmsleydf791b32010-01-26 20:13:04 -0700265
266/* Public functions */
267
268/**
Paul Walmsley435699d2010-05-18 18:40:24 -0600269 * omap2_clksel_round_rate_div() - find divisor for the given clock and rate
Paul Walmsleydf791b32010-01-26 20:13:04 -0700270 * @clk: OMAP struct clk to use
271 * @target_rate: desired clock rate
272 * @new_div: ptr to where we should store the divisor
273 *
274 * Finds 'best' divider value in an array based on the source and target
275 * rates. The divider array must be sorted with smallest divider first.
Paul Walmsley435699d2010-05-18 18:40:24 -0600276 * This function is also used by the DPLL3 M2 divider code.
Paul Walmsleydf791b32010-01-26 20:13:04 -0700277 *
278 * Returns the rounded clock rate or returns 0xffffffff on error.
279 */
280u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
281 u32 *new_div)
282{
283 unsigned long test_rate;
284 const struct clksel *clks;
285 const struct clksel_rate *clkr;
286 u32 last_div = 0;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600287 struct clk *parent;
288 unsigned long parent_rate;
289 const char *clk_name;
290
291 parent = __clk_get_parent(clk);
292 parent_rate = __clk_get_rate(parent);
293 clk_name = __clk_get_name(clk);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700294
Paul Walmsley435699d2010-05-18 18:40:24 -0600295 if (!clk->clksel || !clk->clksel_mask)
296 return ~0;
297
Paul Walmsleydf791b32010-01-26 20:13:04 -0700298 pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600299 clk_name, target_rate);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700300
301 *new_div = 1;
302
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600303 clks = _get_clksel_by_parent(clk, parent);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700304 if (!clks)
305 return ~0;
306
307 for (clkr = clks->rates; clkr->div; clkr++) {
308 if (!(clkr->flags & cpu_mask))
309 continue;
310
311 /* Sanity check */
312 if (clkr->div <= last_div)
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600313 pr_err("clock: %s: clksel_rate table not sorted\n",
314 clk_name);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700315
316 last_div = clkr->div;
317
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600318 test_rate = parent_rate / clkr->div;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700319
320 if (test_rate <= target_rate)
321 break; /* found it */
322 }
323
324 if (!clkr->div) {
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600325 pr_err("clock: %s: could not find divisor for target rate %ld for parent %s\n",
326 clk_name, target_rate, __clk_get_name(parent));
Paul Walmsleydf791b32010-01-26 20:13:04 -0700327 return ~0;
328 }
329
330 *new_div = clkr->div;
331
332 pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600333 (parent_rate / clkr->div));
Paul Walmsleydf791b32010-01-26 20:13:04 -0700334
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600335 return parent_rate / clkr->div;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700336}
337
Paul Walmsley435699d2010-05-18 18:40:24 -0600338/*
339 * Clocktype interface functions to the OMAP clock code
340 * (i.e., those used in struct clk field function pointers, etc.)
341 */
342
Paul Walmsleydf791b32010-01-26 20:13:04 -0700343/**
Paul Walmsley435699d2010-05-18 18:40:24 -0600344 * omap2_init_clksel_parent() - set a clksel clk's parent field from the hdwr
345 * @clk: OMAP clock struct ptr to use
346 *
347 * Given a pointer @clk to a source-selectable struct clk, read the
348 * hardware register and determine what its parent is currently set
349 * to. Update @clk's .parent field with the appropriate clk ptr. No
350 * return value.
351 */
352void omap2_init_clksel_parent(struct clk *clk)
353{
354 const struct clksel *clks;
355 const struct clksel_rate *clkr;
356 u32 r, found = 0;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600357 struct clk *parent;
358 const char *clk_name;
Paul Walmsley435699d2010-05-18 18:40:24 -0600359
360 if (!clk->clksel || !clk->clksel_mask)
361 return;
362
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600363 parent = __clk_get_parent(clk);
364 clk_name = __clk_get_name(clk);
365
Paul Walmsley435699d2010-05-18 18:40:24 -0600366 r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
367 r >>= __ffs(clk->clksel_mask);
368
369 for (clks = clk->clksel; clks->parent && !found; clks++) {
370 for (clkr = clks->rates; clkr->div && !found; clkr++) {
371 if (!(clkr->flags & cpu_mask))
372 continue;
373
374 if (clkr->val == r) {
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600375 if (parent != clks->parent) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600376 pr_debug("clock: %s: inited parent to %s (was %s)\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600377 clk_name,
378 __clk_get_name(clks->parent),
379 ((parent) ?
380 __clk_get_name(parent) :
381 "NULL"));
Paul Walmsley435699d2010-05-18 18:40:24 -0600382 clk_reparent(clk, clks->parent);
Peter Senna Tschudinc09fcc432012-09-18 18:36:11 +0200383 }
Paul Walmsley435699d2010-05-18 18:40:24 -0600384 found = 1;
385 }
386 }
387 }
388
389 /* This indicates a data error */
390 WARN(!found, "clock: %s: init parent: could not find regval %0x\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600391 clk_name, r);
Paul Walmsley435699d2010-05-18 18:40:24 -0600392
393 return;
394}
395
396/**
397 * omap2_clksel_recalc() - function ptr to pass via struct clk .recalc field
398 * @clk: struct clk *
399 *
400 * This function is intended to be called only by the clock framework.
401 * Each clksel clock should have its struct clk .recalc field set to this
402 * function. Returns the clock's current rate, based on its parent's rate
403 * and its current divisor setting in the hardware.
404 */
405unsigned long omap2_clksel_recalc(struct clk *clk)
406{
407 unsigned long rate;
408 u32 div = 0;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600409 struct clk *parent;
Paul Walmsley435699d2010-05-18 18:40:24 -0600410
411 div = _read_divisor(clk);
412 if (div == 0)
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600413 return __clk_get_rate(clk);
Paul Walmsley435699d2010-05-18 18:40:24 -0600414
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600415 parent = __clk_get_parent(clk);
416 rate = __clk_get_rate(parent) / div;
Paul Walmsley435699d2010-05-18 18:40:24 -0600417
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600418 pr_debug("clock: %s: recalc'd rate is %ld (div %d)\n",
419 __clk_get_name(clk), rate, div);
Paul Walmsley435699d2010-05-18 18:40:24 -0600420
421 return rate;
422}
423
424/**
425 * omap2_clksel_round_rate() - find rounded rate for the given clock and rate
Paul Walmsleydf791b32010-01-26 20:13:04 -0700426 * @clk: OMAP struct clk to use
427 * @target_rate: desired clock rate
428 *
Paul Walmsley435699d2010-05-18 18:40:24 -0600429 * This function is intended to be called only by the clock framework.
Paul Walmsleydf791b32010-01-26 20:13:04 -0700430 * Finds best target rate based on the source clock and possible dividers.
431 * rates. The divider array must be sorted with smallest divider first.
Paul Walmsleydf791b32010-01-26 20:13:04 -0700432 *
433 * Returns the rounded clock rate or returns 0xffffffff on error.
434 */
435long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
436{
437 u32 new_div;
438
439 return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
440}
441
Paul Walmsleydf791b32010-01-26 20:13:04 -0700442/**
Paul Walmsley435699d2010-05-18 18:40:24 -0600443 * omap2_clksel_set_rate() - program clock rate in hardware
444 * @clk: struct clk * to program rate
445 * @rate: target rate to program
Paul Walmsleydf791b32010-01-26 20:13:04 -0700446 *
Paul Walmsley435699d2010-05-18 18:40:24 -0600447 * This function is intended to be called only by the clock framework.
448 * Program @clk's rate to @rate in the hardware. The clock can be
449 * either enabled or disabled when this happens, although if the clock
450 * is enabled, some downstream devices may glitch or behave
451 * unpredictably when the clock rate is changed - this depends on the
452 * hardware. This function does not currently check the usecount of
453 * the clock, so if multiple drivers are using the clock, and the rate
454 * is changed, they will all be affected without any notification.
455 * Returns -EINVAL upon error, or 0 upon success.
Paul Walmsleydf791b32010-01-26 20:13:04 -0700456 */
Paul Walmsleydf791b32010-01-26 20:13:04 -0700457int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
458{
Paul Walmsley435699d2010-05-18 18:40:24 -0600459 u32 field_val, validrate, new_div = 0;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700460
Paul Walmsley435699d2010-05-18 18:40:24 -0600461 if (!clk->clksel || !clk->clksel_mask)
Paul Walmsleydf791b32010-01-26 20:13:04 -0700462 return -EINVAL;
463
464 validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
465 if (validrate != rate)
466 return -EINVAL;
467
Paul Walmsley435699d2010-05-18 18:40:24 -0600468 field_val = _divisor_to_clksel(clk, new_div);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700469 if (field_val == ~0)
470 return -EINVAL;
471
Paul Walmsley435699d2010-05-18 18:40:24 -0600472 _write_clksel_reg(clk, field_val);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700473
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600474 clk->rate = __clk_get_rate(__clk_get_parent(clk)) / new_div;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700475
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600476 pr_debug("clock: %s: set rate to %ld\n", __clk_get_name(clk),
477 __clk_get_rate(clk));
Paul Walmsley435699d2010-05-18 18:40:24 -0600478
Paul Walmsleydf791b32010-01-26 20:13:04 -0700479 return 0;
480}
481
Paul Walmsley435699d2010-05-18 18:40:24 -0600482/*
483 * Clksel parent setting function - not passed in struct clk function
484 * pointer - instead, the OMAP clock code currently assumes that any
485 * parent-setting clock is a clksel clock, and calls
486 * omap2_clksel_set_parent() by default
487 */
488
489/**
490 * omap2_clksel_set_parent() - change a clock's parent clock
491 * @clk: struct clk * of the child clock
492 * @new_parent: struct clk * of the new parent clock
493 *
494 * This function is intended to be called only by the clock framework.
495 * Change the parent clock of clock @clk to @new_parent. This is
496 * intended to be used while @clk is disabled. This function does not
497 * currently check the usecount of the clock, so if multiple drivers
498 * are using the clock, and the parent is changed, they will all be
499 * affected without any notification. Returns -EINVAL upon error, or
500 * 0 upon success.
501 */
Paul Walmsleydf791b32010-01-26 20:13:04 -0700502int omap2_clksel_set_parent(struct clk *clk, struct clk *new_parent)
503{
Paul Walmsley435699d2010-05-18 18:40:24 -0600504 u32 field_val = 0;
505 u32 parent_div;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700506
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600507 if (!clk->clksel || !clk->clksel_mask)
Paul Walmsleydf791b32010-01-26 20:13:04 -0700508 return -EINVAL;
509
Paul Walmsley435699d2010-05-18 18:40:24 -0600510 parent_div = _get_div_and_fieldval(new_parent, clk, &field_val);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700511 if (!parent_div)
512 return -EINVAL;
513
Paul Walmsley435699d2010-05-18 18:40:24 -0600514 _write_clksel_reg(clk, field_val);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700515
Paul Walmsleydf791b32010-01-26 20:13:04 -0700516 clk_reparent(clk, new_parent);
517
518 /* CLKSEL clocks follow their parents' rates, divided by a divisor */
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600519 clk->rate = __clk_get_rate(new_parent);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700520
521 if (parent_div > 0)
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600522 __clk_get_rate(clk) /= parent_div;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700523
Paul Walmsley435699d2010-05-18 18:40:24 -0600524 pr_debug("clock: %s: set parent to %s (new rate %ld)\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600525 __clk_get_name(clk),
526 __clk_get_name(__clk_get_parent(clk)),
527 __clk_get_rate(clk));
Paul Walmsleydf791b32010-01-26 20:13:04 -0700528
529 return 0;
530}