1
2
3
4
5
6
7
8
9
10
11
12
13#ifdef CONFIG_OMAP_DEBUG_CLOCKDOMAIN
14# define DEBUG
15#endif
16
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/device.h>
20#include <linux/list.h>
21#include <linux/errno.h>
22#include <linux/delay.h>
23#include <linux/clk.h>
24#include <linux/limits.h>
25#include <linux/err.h>
26
27#include <linux/io.h>
28
29#include <linux/bitops.h>
30
31#include <mach/clock.h>
32
33#include "prm.h"
34#include "prm-regbits-24xx.h"
35#include "cm.h"
36
37#include <mach/powerdomain.h>
38#include <mach/clockdomain.h>
39
40
41static LIST_HEAD(clkdm_list);
42
43
44static DEFINE_MUTEX(clkdm_mutex);
45
46
47static struct clkdm_pwrdm_autodep *autodeps;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65static void _autodep_lookup(struct clkdm_pwrdm_autodep *autodep)
66{
67 struct powerdomain *pwrdm;
68
69 if (!autodep)
70 return;
71
72 if (!omap_chip_is(autodep->omap_chip))
73 return;
74
75 pwrdm = pwrdm_lookup(autodep->pwrdm.name);
76 if (!pwrdm) {
77 pr_err("clockdomain: autodeps: powerdomain %s does not exist\n",
78 autodep->pwrdm.name);
79 pwrdm = ERR_PTR(-ENOENT);
80 }
81 autodep->pwrdm.ptr = pwrdm;
82}
83
84
85
86
87
88
89
90
91
92static void _clkdm_add_autodeps(struct clockdomain *clkdm)
93{
94 struct clkdm_pwrdm_autodep *autodep;
95
96 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
97 if (IS_ERR(autodep->pwrdm.ptr))
98 continue;
99
100 if (!omap_chip_is(autodep->omap_chip))
101 continue;
102
103 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
104 "pwrdm %s\n", autodep->pwrdm.ptr->name,
105 clkdm->pwrdm.ptr->name);
106
107 pwrdm_add_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
108 pwrdm_add_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
109 }
110}
111
112
113
114
115
116
117
118
119
120static void _clkdm_del_autodeps(struct clockdomain *clkdm)
121{
122 struct clkdm_pwrdm_autodep *autodep;
123
124 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
125 if (IS_ERR(autodep->pwrdm.ptr))
126 continue;
127
128 if (!omap_chip_is(autodep->omap_chip))
129 continue;
130
131 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
132 "pwrdm %s\n", autodep->pwrdm.ptr->name,
133 clkdm->pwrdm.ptr->name);
134
135 pwrdm_del_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
136 pwrdm_del_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
137 }
138}
139
140
141
142
143
144
145
146
147
148static void _omap2_clkdm_set_hwsup(struct clockdomain *clkdm, int enable)
149{
150 u32 v;
151
152 if (cpu_is_omap24xx()) {
153 if (enable)
154 v = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
155 else
156 v = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
157 } else if (cpu_is_omap34xx()) {
158 if (enable)
159 v = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
160 else
161 v = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
162 } else {
163 BUG();
164 }
165
166 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
167 v << __ffs(clkdm->clktrctrl_mask),
168 clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
169}
170
171static struct clockdomain *_clkdm_lookup(const char *name)
172{
173 struct clockdomain *clkdm, *temp_clkdm;
174
175 if (!name)
176 return NULL;
177
178 clkdm = NULL;
179
180 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
181 if (!strcmp(name, temp_clkdm->name)) {
182 clkdm = temp_clkdm;
183 break;
184 }
185 }
186
187 return clkdm;
188}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204void clkdm_init(struct clockdomain **clkdms,
205 struct clkdm_pwrdm_autodep *init_autodeps)
206{
207 struct clockdomain **c = NULL;
208 struct clkdm_pwrdm_autodep *autodep = NULL;
209
210 if (clkdms)
211 for (c = clkdms; *c; c++)
212 clkdm_register(*c);
213
214 autodeps = init_autodeps;
215 if (autodeps)
216 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++)
217 _autodep_lookup(autodep);
218}
219
220
221
222
223
224
225
226
227
228int clkdm_register(struct clockdomain *clkdm)
229{
230 int ret = -EINVAL;
231 struct powerdomain *pwrdm;
232
233 if (!clkdm || !clkdm->name)
234 return -EINVAL;
235
236 if (!omap_chip_is(clkdm->omap_chip))
237 return -EINVAL;
238
239 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
240 if (!pwrdm) {
241 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
242 clkdm->name, clkdm->pwrdm.name);
243 return -EINVAL;
244 }
245 clkdm->pwrdm.ptr = pwrdm;
246
247 mutex_lock(&clkdm_mutex);
248
249 if (_clkdm_lookup(clkdm->name)) {
250 ret = -EEXIST;
251 goto cr_unlock;
252 }
253
254 list_add(&clkdm->node, &clkdm_list);
255
256 pwrdm_add_clkdm(pwrdm, clkdm);
257
258 pr_debug("clockdomain: registered %s\n", clkdm->name);
259 ret = 0;
260
261cr_unlock:
262 mutex_unlock(&clkdm_mutex);
263
264 return ret;
265}
266
267
268
269
270
271
272
273
274int clkdm_unregister(struct clockdomain *clkdm)
275{
276 if (!clkdm)
277 return -EINVAL;
278
279 pwrdm_del_clkdm(clkdm->pwrdm.ptr, clkdm);
280
281 mutex_lock(&clkdm_mutex);
282 list_del(&clkdm->node);
283 mutex_unlock(&clkdm_mutex);
284
285 pr_debug("clockdomain: unregistered %s\n", clkdm->name);
286
287 return 0;
288}
289
290
291
292
293
294
295
296
297struct clockdomain *clkdm_lookup(const char *name)
298{
299 struct clockdomain *clkdm, *temp_clkdm;
300
301 if (!name)
302 return NULL;
303
304 clkdm = NULL;
305
306 mutex_lock(&clkdm_mutex);
307 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
308 if (!strcmp(name, temp_clkdm->name)) {
309 clkdm = temp_clkdm;
310 break;
311 }
312 }
313 mutex_unlock(&clkdm_mutex);
314
315 return clkdm;
316}
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
333 void *user)
334{
335 struct clockdomain *clkdm;
336 int ret = 0;
337
338 if (!fn)
339 return -EINVAL;
340
341 mutex_lock(&clkdm_mutex);
342 list_for_each_entry(clkdm, &clkdm_list, node) {
343 ret = (*fn)(clkdm, user);
344 if (ret)
345 break;
346 }
347 mutex_unlock(&clkdm_mutex);
348
349 return ret;
350}
351
352
353
354
355
356
357
358
359
360struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
361{
362 if (!clkdm)
363 return NULL;
364
365 return clkdm->pwrdm.ptr;
366}
367
368
369
370
371
372
373
374
375
376
377
378
379static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
380{
381 u32 v;
382
383 if (!clkdm)
384 return -EINVAL;
385
386 v = cm_read_mod_reg(clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
387 v &= clkdm->clktrctrl_mask;
388 v >>= __ffs(clkdm->clktrctrl_mask);
389
390 return v;
391}
392
393
394
395
396
397
398
399
400
401
402int omap2_clkdm_sleep(struct clockdomain *clkdm)
403{
404 if (!clkdm)
405 return -EINVAL;
406
407 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
408 pr_debug("clockdomain: %s does not support forcing "
409 "sleep via software\n", clkdm->name);
410 return -EINVAL;
411 }
412
413 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
414
415 if (cpu_is_omap24xx()) {
416
417 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
418 clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
419
420 } else if (cpu_is_omap34xx()) {
421
422 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
423 __ffs(clkdm->clktrctrl_mask));
424
425 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
426 clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
427
428 } else {
429 BUG();
430 };
431
432 return 0;
433}
434
435
436
437
438
439
440
441
442
443
444int omap2_clkdm_wakeup(struct clockdomain *clkdm)
445{
446 if (!clkdm)
447 return -EINVAL;
448
449 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
450 pr_debug("clockdomain: %s does not support forcing "
451 "wakeup via software\n", clkdm->name);
452 return -EINVAL;
453 }
454
455 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
456
457 if (cpu_is_omap24xx()) {
458
459 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
460 clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
461
462 } else if (cpu_is_omap34xx()) {
463
464 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
465 __ffs(clkdm->clktrctrl_mask));
466
467 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
468 clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
469
470 } else {
471 BUG();
472 };
473
474 return 0;
475}
476
477
478
479
480
481
482
483
484
485
486
487void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
488{
489 if (!clkdm)
490 return;
491
492 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
493 pr_debug("clock: automatic idle transitions cannot be enabled "
494 "on clockdomain %s\n", clkdm->name);
495 return;
496 }
497
498 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
499 clkdm->name);
500
501 if (atomic_read(&clkdm->usecount) > 0)
502 _clkdm_add_autodeps(clkdm);
503
504 _omap2_clkdm_set_hwsup(clkdm, 1);
505
506 pwrdm_clkdm_state_switch(clkdm);
507}
508
509
510
511
512
513
514
515
516
517
518void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
519{
520 if (!clkdm)
521 return;
522
523 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
524 pr_debug("clockdomain: automatic idle transitions cannot be "
525 "disabled on %s\n", clkdm->name);
526 return;
527 }
528
529 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
530 clkdm->name);
531
532 _omap2_clkdm_set_hwsup(clkdm, 0);
533
534 if (atomic_read(&clkdm->usecount) > 0)
535 _clkdm_del_autodeps(clkdm);
536}
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
556{
557 int v;
558
559
560
561
562
563
564 if (!clkdm || !clk)
565 return -EINVAL;
566
567 if (atomic_inc_return(&clkdm->usecount) > 1)
568 return 0;
569
570
571
572 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
573 clk->name);
574
575 v = omap2_clkdm_clktrctrl_read(clkdm);
576
577 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
578 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
579
580 _omap2_clkdm_set_hwsup(clkdm, 0);
581 _clkdm_add_autodeps(clkdm);
582 _omap2_clkdm_set_hwsup(clkdm, 1);
583 } else {
584 omap2_clkdm_wakeup(clkdm);
585 }
586
587 pwrdm_wait_transition(clkdm->pwrdm.ptr);
588 pwrdm_clkdm_state_switch(clkdm);
589
590 return 0;
591}
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
607{
608 int v;
609
610
611
612
613
614
615 if (!clkdm || !clk)
616 return -EINVAL;
617
618#ifdef DEBUG
619 if (atomic_read(&clkdm->usecount) == 0) {
620 WARN_ON(1);
621 return -ERANGE;
622 }
623#endif
624
625 if (atomic_dec_return(&clkdm->usecount) > 0)
626 return 0;
627
628
629
630 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
631 clk->name);
632
633 v = omap2_clkdm_clktrctrl_read(clkdm);
634
635 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
636 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
637
638 _omap2_clkdm_set_hwsup(clkdm, 0);
639 _clkdm_del_autodeps(clkdm);
640 _omap2_clkdm_set_hwsup(clkdm, 1);
641 } else {
642 omap2_clkdm_sleep(clkdm);
643 }
644
645 pwrdm_clkdm_state_switch(clkdm);
646
647 return 0;
648}
649
650