2 * Exynos Generic power domain support.
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
7 * Implementation of Exynos specific power domain control which is used in
8 * conjunction with runtime-pm. Support for both device-tree and non-device-tree
9 * based power domain support is included.
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.
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/pm_domain.h>
20 #include <linux/clk.h>
21 #include <linux/delay.h>
22 #include <linux/of_address.h>
23 #include <linux/of_platform.h>
24 #include <linux/sched.h>
26 #define MAX_CLK_PER_DOMAIN 4
28 struct exynos_pm_domain_config
{
29 /* Value for LOCAL_PWR_CFG and STATUS fields for each domain */
34 * Exynos specific wrapper around the generic power domain
36 struct exynos_pm_domain
{
40 struct generic_pm_domain pd
;
42 struct clk
*clk
[MAX_CLK_PER_DOMAIN
];
43 struct clk
*pclk
[MAX_CLK_PER_DOMAIN
];
44 struct clk
*asb_clk
[MAX_CLK_PER_DOMAIN
];
48 static int exynos_pd_power(struct generic_pm_domain
*domain
, bool power_on
)
50 struct exynos_pm_domain
*pd
;
56 pd
= container_of(domain
, struct exynos_pm_domain
, pd
);
59 for (i
= 0; i
< MAX_CLK_PER_DOMAIN
; i
++) {
60 if (IS_ERR(pd
->asb_clk
[i
]))
62 clk_prepare_enable(pd
->asb_clk
[i
]);
65 /* Set oscclk before powering off a domain*/
67 for (i
= 0; i
< MAX_CLK_PER_DOMAIN
; i
++) {
68 if (IS_ERR(pd
->clk
[i
]))
70 pd
->pclk
[i
] = clk_get_parent(pd
->clk
[i
]);
71 if (clk_set_parent(pd
->clk
[i
], pd
->oscclk
))
72 pr_err("%s: error setting oscclk as parent to clock %d\n",
77 pwr
= power_on
? pd
->local_pwr_cfg
: 0;
78 writel_relaxed(pwr
, base
);
83 while ((readl_relaxed(base
+ 0x4) & pd
->local_pwr_cfg
) != pwr
) {
85 op
= (power_on
) ? "enable" : "disable";
86 pr_err("Power domain %s %s failed\n", domain
->name
, op
);
91 usleep_range(80, 100);
94 /* Restore clocks after powering on a domain*/
96 for (i
= 0; i
< MAX_CLK_PER_DOMAIN
; i
++) {
97 if (IS_ERR(pd
->clk
[i
]))
100 if (IS_ERR(pd
->pclk
[i
]))
101 continue; /* Skip on first power up */
102 if (clk_set_parent(pd
->clk
[i
], pd
->pclk
[i
]))
103 pr_err("%s: error setting parent to clock%d\n",
108 for (i
= 0; i
< MAX_CLK_PER_DOMAIN
; i
++) {
109 if (IS_ERR(pd
->asb_clk
[i
]))
111 clk_disable_unprepare(pd
->asb_clk
[i
]);
117 static int exynos_pd_power_on(struct generic_pm_domain
*domain
)
119 return exynos_pd_power(domain
, true);
122 static int exynos_pd_power_off(struct generic_pm_domain
*domain
)
124 return exynos_pd_power(domain
, false);
127 static const struct exynos_pm_domain_config exynos4210_cfg __initconst
= {
128 .local_pwr_cfg
= 0x7,
131 static const struct of_device_id exynos_pm_domain_of_match
[] __initconst
= {
133 .compatible
= "samsung,exynos4210-pd",
134 .data
= &exynos4210_cfg
,
139 static __init
int exynos4_pm_init_power_domain(void)
141 struct device_node
*np
;
142 const struct of_device_id
*match
;
144 for_each_matching_node_and_match(np
, exynos_pm_domain_of_match
, &match
) {
145 const struct exynos_pm_domain_config
*pm_domain_cfg
;
146 struct exynos_pm_domain
*pd
;
149 pm_domain_cfg
= match
->data
;
151 pd
= kzalloc(sizeof(*pd
), GFP_KERNEL
);
153 pr_err("%s: failed to allocate memory for domain\n",
158 pd
->pd
.name
= kstrdup_const(strrchr(np
->full_name
, '/') + 1,
166 pd
->name
= pd
->pd
.name
;
167 pd
->base
= of_iomap(np
, 0);
169 pr_warn("%s: failed to map memory\n", __func__
);
170 kfree_const(pd
->pd
.name
);
175 pd
->pd
.power_off
= exynos_pd_power_off
;
176 pd
->pd
.power_on
= exynos_pd_power_on
;
177 pd
->local_pwr_cfg
= pm_domain_cfg
->local_pwr_cfg
;
179 for (i
= 0; i
< MAX_CLK_PER_DOMAIN
; i
++) {
182 snprintf(clk_name
, sizeof(clk_name
), "asb%d", i
);
183 pd
->asb_clk
[i
] = of_clk_get_by_name(np
, clk_name
);
184 if (IS_ERR(pd
->asb_clk
[i
]))
188 pd
->oscclk
= of_clk_get_by_name(np
, "oscclk");
189 if (IS_ERR(pd
->oscclk
))
192 for (i
= 0; i
< MAX_CLK_PER_DOMAIN
; i
++) {
195 snprintf(clk_name
, sizeof(clk_name
), "clk%d", i
);
196 pd
->clk
[i
] = of_clk_get_by_name(np
, clk_name
);
197 if (IS_ERR(pd
->clk
[i
]))
200 * Skip setting parent on first power up.
201 * The parent at this time may not be useful at all.
203 pd
->pclk
[i
] = ERR_PTR(-EINVAL
);
206 if (IS_ERR(pd
->clk
[0]))
210 on
= readl_relaxed(pd
->base
+ 0x4) & pd
->local_pwr_cfg
;
212 pm_genpd_init(&pd
->pd
, NULL
, !on
);
213 of_genpd_add_provider_simple(np
, &pd
->pd
);
216 /* Assign the child power domains to their parents */
217 for_each_matching_node(np
, exynos_pm_domain_of_match
) {
218 struct of_phandle_args child
, parent
;
221 child
.args_count
= 0;
223 if (of_parse_phandle_with_args(np
, "power-domains",
224 "#power-domain-cells", 0,
228 if (of_genpd_add_subdomain(&parent
, &child
))
229 pr_warn("%s failed to add subdomain: %s\n",
230 parent
.np
->name
, child
.np
->name
);
232 pr_info("%s has as child subdomain: %s.\n",
233 parent
.np
->name
, child
.np
->name
);
238 core_initcall(exynos4_pm_init_power_domain
);