1 // SPDX-License-Identifier: GPL-2.0
3 // Exynos Generic power domain support.
5 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 // http://www.samsung.com
8 // Implementation of Exynos specific power domain control which is used in
9 // conjunction with runtime-pm. Support for both device-tree and non-device-tree
10 // based power domain support is included.
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/pm_domain.h>
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/sched.h>
22 #define MAX_CLK_PER_DOMAIN 4
24 struct exynos_pm_domain_config {
25 /* Value for LOCAL_PWR_CFG and STATUS fields for each domain */
30 * Exynos specific wrapper around the generic power domain
32 struct exynos_pm_domain {
35 struct generic_pm_domain pd;
37 struct clk *clk[MAX_CLK_PER_DOMAIN];
38 struct clk *pclk[MAX_CLK_PER_DOMAIN];
39 struct clk *asb_clk[MAX_CLK_PER_DOMAIN];
43 static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
45 struct exynos_pm_domain *pd;
51 pd = container_of(domain, struct exynos_pm_domain, pd);
54 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
55 if (IS_ERR(pd->asb_clk[i]))
57 clk_prepare_enable(pd->asb_clk[i]);
60 /* Set oscclk before powering off a domain*/
62 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
63 if (IS_ERR(pd->clk[i]))
65 pd->pclk[i] = clk_get_parent(pd->clk[i]);
66 if (clk_set_parent(pd->clk[i], pd->oscclk))
67 pr_err("%s: error setting oscclk as parent to clock %d\n",
72 pwr = power_on ? pd->local_pwr_cfg : 0;
73 writel_relaxed(pwr, base);
78 while ((readl_relaxed(base + 0x4) & pd->local_pwr_cfg) != pwr) {
80 op = (power_on) ? "enable" : "disable";
81 pr_err("Power domain %s %s failed\n", domain->name, op);
86 usleep_range(80, 100);
89 /* Restore clocks after powering on a domain*/
91 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
92 if (IS_ERR(pd->clk[i]))
95 if (IS_ERR(pd->pclk[i]))
96 continue; /* Skip on first power up */
97 if (clk_set_parent(pd->clk[i], pd->pclk[i]))
98 pr_err("%s: error setting parent to clock%d\n",
103 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
104 if (IS_ERR(pd->asb_clk[i]))
106 clk_disable_unprepare(pd->asb_clk[i]);
112 static int exynos_pd_power_on(struct generic_pm_domain *domain)
114 return exynos_pd_power(domain, true);
117 static int exynos_pd_power_off(struct generic_pm_domain *domain)
119 return exynos_pd_power(domain, false);
122 static const struct exynos_pm_domain_config exynos4210_cfg __initconst = {
123 .local_pwr_cfg = 0x7,
126 static const struct exynos_pm_domain_config exynos5433_cfg __initconst = {
127 .local_pwr_cfg = 0xf,
130 static const struct of_device_id exynos_pm_domain_of_match[] __initconst = {
132 .compatible = "samsung,exynos4210-pd",
133 .data = &exynos4210_cfg,
135 .compatible = "samsung,exynos5433-pd",
136 .data = &exynos5433_cfg,
141 static __init const char *exynos_get_domain_name(struct device_node *node)
145 if (of_property_read_string(node, "label", &name) < 0)
146 name = kbasename(node->full_name);
147 return kstrdup_const(name, GFP_KERNEL);
150 static const char *soc_force_no_clk[] = {
151 "samsung,exynos5420-clock",
152 "samsung,exynos5800-clock",
155 static __init int exynos4_pm_init_power_domain(void)
157 struct device_node *np;
158 const struct of_device_id *match;
160 for_each_matching_node_and_match(np, exynos_pm_domain_of_match, &match) {
161 const struct exynos_pm_domain_config *pm_domain_cfg;
162 struct exynos_pm_domain *pd;
165 pm_domain_cfg = match->data;
167 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
172 pd->pd.name = exynos_get_domain_name(np);
179 pd->base = of_iomap(np, 0);
181 pr_warn("%s: failed to map memory\n", __func__);
182 kfree_const(pd->pd.name);
187 pd->pd.power_off = exynos_pd_power_off;
188 pd->pd.power_on = exynos_pd_power_on;
189 pd->local_pwr_cfg = pm_domain_cfg->local_pwr_cfg;
191 for (i = 0; i < ARRAY_SIZE(soc_force_no_clk); i++)
192 if (of_find_compatible_node(NULL, NULL,
193 soc_force_no_clk[i]))
196 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
199 snprintf(clk_name, sizeof(clk_name), "asb%d", i);
200 pd->asb_clk[i] = of_clk_get_by_name(np, clk_name);
201 if (IS_ERR(pd->asb_clk[i]))
205 pd->oscclk = of_clk_get_by_name(np, "oscclk");
206 if (IS_ERR(pd->oscclk))
209 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
212 snprintf(clk_name, sizeof(clk_name), "clk%d", i);
213 pd->clk[i] = of_clk_get_by_name(np, clk_name);
214 if (IS_ERR(pd->clk[i]))
217 * Skip setting parent on first power up.
218 * The parent at this time may not be useful at all.
220 pd->pclk[i] = ERR_PTR(-EINVAL);
223 if (IS_ERR(pd->clk[0]))
227 on = readl_relaxed(pd->base + 0x4) & pd->local_pwr_cfg;
229 pm_genpd_init(&pd->pd, NULL, !on);
230 of_genpd_add_provider_simple(np, &pd->pd);
233 /* Assign the child power domains to their parents */
234 for_each_matching_node(np, exynos_pm_domain_of_match) {
235 struct of_phandle_args child, parent;
238 child.args_count = 0;
240 if (of_parse_phandle_with_args(np, "power-domains",
241 "#power-domain-cells", 0,
245 if (of_genpd_add_subdomain(&parent, &child))
246 pr_warn("%pOF failed to add subdomain: %pOF\n",
247 parent.np, child.np);
249 pr_info("%pOF has as child subdomain: %pOF.\n",
250 parent.np, child.np);
255 core_initcall(exynos4_pm_init_power_domain);