Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / clk / at91 / clk-main.c
blob58b5baca670c3416fe0fc1733088f7526349005c
1 /*
2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 */
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
14 #include <linux/delay.h>
15 #include <linux/of.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/regmap.h>
19 #include "pmc.h"
21 #define SLOW_CLOCK_FREQ 32768
22 #define MAINF_DIV 16
23 #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
24 SLOW_CLOCK_FREQ)
25 #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
26 #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
28 #define MOR_KEY_MASK (0xff << 16)
30 struct clk_main_osc {
31 struct clk_hw hw;
32 struct regmap *regmap;
35 #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
37 struct clk_main_rc_osc {
38 struct clk_hw hw;
39 struct regmap *regmap;
40 unsigned long frequency;
41 unsigned long accuracy;
44 #define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw)
46 struct clk_rm9200_main {
47 struct clk_hw hw;
48 struct regmap *regmap;
51 #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
53 struct clk_sam9x5_main {
54 struct clk_hw hw;
55 struct regmap *regmap;
56 u8 parent;
59 #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
61 static inline bool clk_main_osc_ready(struct regmap *regmap)
63 unsigned int status;
65 regmap_read(regmap, AT91_PMC_SR, &status);
67 return status & AT91_PMC_MOSCS;
70 static int clk_main_osc_prepare(struct clk_hw *hw)
72 struct clk_main_osc *osc = to_clk_main_osc(hw);
73 struct regmap *regmap = osc->regmap;
74 u32 tmp;
76 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
77 tmp &= ~MOR_KEY_MASK;
79 if (tmp & AT91_PMC_OSCBYPASS)
80 return 0;
82 if (!(tmp & AT91_PMC_MOSCEN)) {
83 tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
84 regmap_write(regmap, AT91_CKGR_MOR, tmp);
87 while (!clk_main_osc_ready(regmap))
88 cpu_relax();
90 return 0;
93 static void clk_main_osc_unprepare(struct clk_hw *hw)
95 struct clk_main_osc *osc = to_clk_main_osc(hw);
96 struct regmap *regmap = osc->regmap;
97 u32 tmp;
99 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
100 if (tmp & AT91_PMC_OSCBYPASS)
101 return;
103 if (!(tmp & AT91_PMC_MOSCEN))
104 return;
106 tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
107 regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
110 static int clk_main_osc_is_prepared(struct clk_hw *hw)
112 struct clk_main_osc *osc = to_clk_main_osc(hw);
113 struct regmap *regmap = osc->regmap;
114 u32 tmp, status;
116 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
117 if (tmp & AT91_PMC_OSCBYPASS)
118 return 1;
120 regmap_read(regmap, AT91_PMC_SR, &status);
122 return (status & AT91_PMC_MOSCS) && (tmp & AT91_PMC_MOSCEN);
125 static const struct clk_ops main_osc_ops = {
126 .prepare = clk_main_osc_prepare,
127 .unprepare = clk_main_osc_unprepare,
128 .is_prepared = clk_main_osc_is_prepared,
131 static struct clk * __init
132 at91_clk_register_main_osc(struct regmap *regmap,
133 const char *name,
134 const char *parent_name,
135 bool bypass)
137 struct clk_main_osc *osc;
138 struct clk *clk = NULL;
139 struct clk_init_data init;
141 if (!name || !parent_name)
142 return ERR_PTR(-EINVAL);
144 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
145 if (!osc)
146 return ERR_PTR(-ENOMEM);
148 init.name = name;
149 init.ops = &main_osc_ops;
150 init.parent_names = &parent_name;
151 init.num_parents = 1;
152 init.flags = CLK_IGNORE_UNUSED;
154 osc->hw.init = &init;
155 osc->regmap = regmap;
157 if (bypass)
158 regmap_update_bits(regmap,
159 AT91_CKGR_MOR, MOR_KEY_MASK |
160 AT91_PMC_MOSCEN,
161 AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
163 clk = clk_register(NULL, &osc->hw);
164 if (IS_ERR(clk))
165 kfree(osc);
167 return clk;
170 static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np)
172 struct clk *clk;
173 const char *name = np->name;
174 const char *parent_name;
175 struct regmap *regmap;
176 bool bypass;
178 of_property_read_string(np, "clock-output-names", &name);
179 bypass = of_property_read_bool(np, "atmel,osc-bypass");
180 parent_name = of_clk_get_parent_name(np, 0);
182 regmap = syscon_node_to_regmap(of_get_parent(np));
183 if (IS_ERR(regmap))
184 return;
186 clk = at91_clk_register_main_osc(regmap, name, parent_name, bypass);
187 if (IS_ERR(clk))
188 return;
190 of_clk_add_provider(np, of_clk_src_simple_get, clk);
192 CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc",
193 of_at91rm9200_clk_main_osc_setup);
195 static bool clk_main_rc_osc_ready(struct regmap *regmap)
197 unsigned int status;
199 regmap_read(regmap, AT91_PMC_SR, &status);
201 return status & AT91_PMC_MOSCRCS;
204 static int clk_main_rc_osc_prepare(struct clk_hw *hw)
206 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
207 struct regmap *regmap = osc->regmap;
208 unsigned int mor;
210 regmap_read(regmap, AT91_CKGR_MOR, &mor);
212 if (!(mor & AT91_PMC_MOSCRCEN))
213 regmap_update_bits(regmap, AT91_CKGR_MOR,
214 MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
215 AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
217 while (!clk_main_rc_osc_ready(regmap))
218 cpu_relax();
220 return 0;
223 static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
225 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
226 struct regmap *regmap = osc->regmap;
227 unsigned int mor;
229 regmap_read(regmap, AT91_CKGR_MOR, &mor);
231 if (!(mor & AT91_PMC_MOSCRCEN))
232 return;
234 regmap_update_bits(regmap, AT91_CKGR_MOR,
235 MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
238 static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
240 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
241 struct regmap *regmap = osc->regmap;
242 unsigned int mor, status;
244 regmap_read(regmap, AT91_CKGR_MOR, &mor);
245 regmap_read(regmap, AT91_PMC_SR, &status);
247 return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
250 static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
251 unsigned long parent_rate)
253 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
255 return osc->frequency;
258 static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw *hw,
259 unsigned long parent_acc)
261 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
263 return osc->accuracy;
266 static const struct clk_ops main_rc_osc_ops = {
267 .prepare = clk_main_rc_osc_prepare,
268 .unprepare = clk_main_rc_osc_unprepare,
269 .is_prepared = clk_main_rc_osc_is_prepared,
270 .recalc_rate = clk_main_rc_osc_recalc_rate,
271 .recalc_accuracy = clk_main_rc_osc_recalc_accuracy,
274 static struct clk * __init
275 at91_clk_register_main_rc_osc(struct regmap *regmap,
276 const char *name,
277 u32 frequency, u32 accuracy)
279 struct clk_main_rc_osc *osc;
280 struct clk *clk = NULL;
281 struct clk_init_data init;
283 if (!name || !frequency)
284 return ERR_PTR(-EINVAL);
286 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
287 if (!osc)
288 return ERR_PTR(-ENOMEM);
290 init.name = name;
291 init.ops = &main_rc_osc_ops;
292 init.parent_names = NULL;
293 init.num_parents = 0;
294 init.flags = CLK_IGNORE_UNUSED;
296 osc->hw.init = &init;
297 osc->regmap = regmap;
298 osc->frequency = frequency;
299 osc->accuracy = accuracy;
301 clk = clk_register(NULL, &osc->hw);
302 if (IS_ERR(clk))
303 kfree(osc);
305 return clk;
308 static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np)
310 struct clk *clk;
311 u32 frequency = 0;
312 u32 accuracy = 0;
313 const char *name = np->name;
314 struct regmap *regmap;
316 of_property_read_string(np, "clock-output-names", &name);
317 of_property_read_u32(np, "clock-frequency", &frequency);
318 of_property_read_u32(np, "clock-accuracy", &accuracy);
320 regmap = syscon_node_to_regmap(of_get_parent(np));
321 if (IS_ERR(regmap))
322 return;
324 clk = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy);
325 if (IS_ERR(clk))
326 return;
328 of_clk_add_provider(np, of_clk_src_simple_get, clk);
330 CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc",
331 of_at91sam9x5_clk_main_rc_osc_setup);
334 static int clk_main_probe_frequency(struct regmap *regmap)
336 unsigned long prep_time, timeout;
337 unsigned int mcfr;
339 timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
340 do {
341 prep_time = jiffies;
342 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
343 if (mcfr & AT91_PMC_MAINRDY)
344 return 0;
345 usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
346 } while (time_before(prep_time, timeout));
348 return -ETIMEDOUT;
351 static unsigned long clk_main_recalc_rate(struct regmap *regmap,
352 unsigned long parent_rate)
354 unsigned int mcfr;
356 if (parent_rate)
357 return parent_rate;
359 pr_warn("Main crystal frequency not set, using approximate value\n");
360 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
361 if (!(mcfr & AT91_PMC_MAINRDY))
362 return 0;
364 return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
367 static int clk_rm9200_main_prepare(struct clk_hw *hw)
369 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
371 return clk_main_probe_frequency(clkmain->regmap);
374 static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
376 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
377 unsigned int status;
379 regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
381 return status & AT91_PMC_MAINRDY ? 1 : 0;
384 static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
385 unsigned long parent_rate)
387 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
389 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
392 static const struct clk_ops rm9200_main_ops = {
393 .prepare = clk_rm9200_main_prepare,
394 .is_prepared = clk_rm9200_main_is_prepared,
395 .recalc_rate = clk_rm9200_main_recalc_rate,
398 static struct clk * __init
399 at91_clk_register_rm9200_main(struct regmap *regmap,
400 const char *name,
401 const char *parent_name)
403 struct clk_rm9200_main *clkmain;
404 struct clk *clk = NULL;
405 struct clk_init_data init;
407 if (!name)
408 return ERR_PTR(-EINVAL);
410 if (!parent_name)
411 return ERR_PTR(-EINVAL);
413 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
414 if (!clkmain)
415 return ERR_PTR(-ENOMEM);
417 init.name = name;
418 init.ops = &rm9200_main_ops;
419 init.parent_names = &parent_name;
420 init.num_parents = 1;
421 init.flags = 0;
423 clkmain->hw.init = &init;
424 clkmain->regmap = regmap;
426 clk = clk_register(NULL, &clkmain->hw);
427 if (IS_ERR(clk))
428 kfree(clkmain);
430 return clk;
433 static void __init of_at91rm9200_clk_main_setup(struct device_node *np)
435 struct clk *clk;
436 const char *parent_name;
437 const char *name = np->name;
438 struct regmap *regmap;
440 parent_name = of_clk_get_parent_name(np, 0);
441 of_property_read_string(np, "clock-output-names", &name);
443 regmap = syscon_node_to_regmap(of_get_parent(np));
444 if (IS_ERR(regmap))
445 return;
447 clk = at91_clk_register_rm9200_main(regmap, name, parent_name);
448 if (IS_ERR(clk))
449 return;
451 of_clk_add_provider(np, of_clk_src_simple_get, clk);
453 CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main",
454 of_at91rm9200_clk_main_setup);
456 static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
458 unsigned int status;
460 regmap_read(regmap, AT91_PMC_SR, &status);
462 return status & AT91_PMC_MOSCSELS ? 1 : 0;
465 static int clk_sam9x5_main_prepare(struct clk_hw *hw)
467 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
468 struct regmap *regmap = clkmain->regmap;
470 while (!clk_sam9x5_main_ready(regmap))
471 cpu_relax();
473 return clk_main_probe_frequency(regmap);
476 static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
478 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
480 return clk_sam9x5_main_ready(clkmain->regmap);
483 static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
484 unsigned long parent_rate)
486 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
488 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
491 static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
493 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
494 struct regmap *regmap = clkmain->regmap;
495 unsigned int tmp;
497 if (index > 1)
498 return -EINVAL;
500 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
501 tmp &= ~MOR_KEY_MASK;
503 if (index && !(tmp & AT91_PMC_MOSCSEL))
504 regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
505 else if (!index && (tmp & AT91_PMC_MOSCSEL))
506 regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
508 while (!clk_sam9x5_main_ready(regmap))
509 cpu_relax();
511 return 0;
514 static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
516 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
517 unsigned int status;
519 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
521 return status & AT91_PMC_MOSCEN ? 1 : 0;
524 static const struct clk_ops sam9x5_main_ops = {
525 .prepare = clk_sam9x5_main_prepare,
526 .is_prepared = clk_sam9x5_main_is_prepared,
527 .recalc_rate = clk_sam9x5_main_recalc_rate,
528 .set_parent = clk_sam9x5_main_set_parent,
529 .get_parent = clk_sam9x5_main_get_parent,
532 static struct clk * __init
533 at91_clk_register_sam9x5_main(struct regmap *regmap,
534 const char *name,
535 const char **parent_names,
536 int num_parents)
538 struct clk_sam9x5_main *clkmain;
539 struct clk *clk = NULL;
540 struct clk_init_data init;
541 unsigned int status;
543 if (!name)
544 return ERR_PTR(-EINVAL);
546 if (!parent_names || !num_parents)
547 return ERR_PTR(-EINVAL);
549 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
550 if (!clkmain)
551 return ERR_PTR(-ENOMEM);
553 init.name = name;
554 init.ops = &sam9x5_main_ops;
555 init.parent_names = parent_names;
556 init.num_parents = num_parents;
557 init.flags = CLK_SET_PARENT_GATE;
559 clkmain->hw.init = &init;
560 clkmain->regmap = regmap;
561 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
562 clkmain->parent = status & AT91_PMC_MOSCEN ? 1 : 0;
564 clk = clk_register(NULL, &clkmain->hw);
565 if (IS_ERR(clk))
566 kfree(clkmain);
568 return clk;
571 static void __init of_at91sam9x5_clk_main_setup(struct device_node *np)
573 struct clk *clk;
574 const char *parent_names[2];
575 unsigned int num_parents;
576 const char *name = np->name;
577 struct regmap *regmap;
579 num_parents = of_clk_get_parent_count(np);
580 if (num_parents == 0 || num_parents > 2)
581 return;
583 of_clk_parent_fill(np, parent_names, num_parents);
584 regmap = syscon_node_to_regmap(of_get_parent(np));
585 if (IS_ERR(regmap))
586 return;
588 of_property_read_string(np, "clock-output-names", &name);
590 clk = at91_clk_register_sam9x5_main(regmap, name, parent_names,
591 num_parents);
592 if (IS_ERR(clk))
593 return;
595 of_clk_add_provider(np, of_clk_src_simple_get, clk);
597 CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main",
598 of_at91sam9x5_clk_main_setup);