Commit | Line | Data |
---|---|---|
91cfbd4e TA |
1 | /* |
2 | * Exynos Generic power domain support. | |
3 | * | |
4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. | |
5 | * http://www.samsung.com | |
6 | * | |
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. | |
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 | */ | |
15 | ||
16 | #include <linux/io.h> | |
17 | #include <linux/err.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/pm_domain.h> | |
c760569d | 20 | #include <linux/clk.h> |
91cfbd4e TA |
21 | #include <linux/delay.h> |
22 | #include <linux/of_address.h> | |
8a65d236 TF |
23 | #include <linux/of_platform.h> |
24 | #include <linux/sched.h> | |
91cfbd4e | 25 | |
b634e38f | 26 | #define INT_LOCAL_PWR_EN 0x7 |
c760569d P |
27 | #define MAX_CLK_PER_DOMAIN 4 |
28 | ||
91cfbd4e TA |
29 | /* |
30 | * Exynos specific wrapper around the generic power domain | |
31 | */ | |
32 | struct exynos_pm_domain { | |
33 | void __iomem *base; | |
34 | char const *name; | |
35 | bool is_off; | |
36 | struct generic_pm_domain pd; | |
c760569d P |
37 | struct clk *oscclk; |
38 | struct clk *clk[MAX_CLK_PER_DOMAIN]; | |
39 | struct clk *pclk[MAX_CLK_PER_DOMAIN]; | |
91cfbd4e TA |
40 | }; |
41 | ||
42 | static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on) | |
43 | { | |
44 | struct exynos_pm_domain *pd; | |
45 | void __iomem *base; | |
46 | u32 timeout, pwr; | |
47 | char *op; | |
48 | ||
49 | pd = container_of(domain, struct exynos_pm_domain, pd); | |
50 | base = pd->base; | |
51 | ||
c760569d P |
52 | /* Set oscclk before powering off a domain*/ |
53 | if (!power_on) { | |
54 | int i; | |
55 | ||
56 | for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) { | |
57 | if (IS_ERR(pd->clk[i])) | |
58 | break; | |
59 | if (clk_set_parent(pd->clk[i], pd->oscclk)) | |
60 | pr_err("%s: error setting oscclk as parent to clock %d\n", | |
61 | pd->name, i); | |
62 | } | |
63 | } | |
64 | ||
b634e38f | 65 | pwr = power_on ? INT_LOCAL_PWR_EN : 0; |
91cfbd4e TA |
66 | __raw_writel(pwr, base); |
67 | ||
68 | /* Wait max 1ms */ | |
69 | timeout = 10; | |
70 | ||
b634e38f | 71 | while ((__raw_readl(base + 0x4) & INT_LOCAL_PWR_EN) != pwr) { |
91cfbd4e TA |
72 | if (!timeout) { |
73 | op = (power_on) ? "enable" : "disable"; | |
74 | pr_err("Power domain %s %s failed\n", domain->name, op); | |
75 | return -ETIMEDOUT; | |
76 | } | |
77 | timeout--; | |
78 | cpu_relax(); | |
79 | usleep_range(80, 100); | |
80 | } | |
c760569d P |
81 | |
82 | /* Restore clocks after powering on a domain*/ | |
83 | if (power_on) { | |
84 | int i; | |
85 | ||
86 | for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) { | |
87 | if (IS_ERR(pd->clk[i])) | |
88 | break; | |
89 | if (clk_set_parent(pd->clk[i], pd->pclk[i])) | |
90 | pr_err("%s: error setting parent to clock%d\n", | |
91 | pd->name, i); | |
92 | } | |
93 | } | |
94 | ||
91cfbd4e TA |
95 | return 0; |
96 | } | |
97 | ||
98 | static int exynos_pd_power_on(struct generic_pm_domain *domain) | |
99 | { | |
100 | return exynos_pd_power(domain, true); | |
101 | } | |
102 | ||
103 | static int exynos_pd_power_off(struct generic_pm_domain *domain) | |
104 | { | |
105 | return exynos_pd_power(domain, false); | |
106 | } | |
107 | ||
8a65d236 TF |
108 | static void exynos_add_device_to_domain(struct exynos_pm_domain *pd, |
109 | struct device *dev) | |
110 | { | |
111 | int ret; | |
112 | ||
113 | dev_dbg(dev, "adding to power domain %s\n", pd->pd.name); | |
114 | ||
115 | while (1) { | |
116 | ret = pm_genpd_add_device(&pd->pd, dev); | |
117 | if (ret != -EAGAIN) | |
118 | break; | |
119 | cond_resched(); | |
120 | } | |
121 | ||
122 | pm_genpd_dev_need_restore(dev, true); | |
123 | } | |
124 | ||
125 | static void exynos_remove_device_from_domain(struct device *dev) | |
126 | { | |
127 | struct generic_pm_domain *genpd = dev_to_genpd(dev); | |
128 | int ret; | |
129 | ||
130 | dev_dbg(dev, "removing from power domain %s\n", genpd->name); | |
131 | ||
132 | while (1) { | |
133 | ret = pm_genpd_remove_device(genpd, dev); | |
134 | if (ret != -EAGAIN) | |
135 | break; | |
136 | cond_resched(); | |
137 | } | |
138 | } | |
139 | ||
140 | static void exynos_read_domain_from_dt(struct device *dev) | |
141 | { | |
142 | struct platform_device *pd_pdev; | |
143 | struct exynos_pm_domain *pd; | |
144 | struct device_node *node; | |
145 | ||
146 | node = of_parse_phandle(dev->of_node, "samsung,power-domain", 0); | |
147 | if (!node) | |
148 | return; | |
149 | pd_pdev = of_find_device_by_node(node); | |
150 | if (!pd_pdev) | |
151 | return; | |
152 | pd = platform_get_drvdata(pd_pdev); | |
153 | exynos_add_device_to_domain(pd, dev); | |
154 | } | |
155 | ||
156 | static int exynos_pm_notifier_call(struct notifier_block *nb, | |
157 | unsigned long event, void *data) | |
158 | { | |
159 | struct device *dev = data; | |
160 | ||
161 | switch (event) { | |
162 | case BUS_NOTIFY_BIND_DRIVER: | |
163 | if (dev->of_node) | |
164 | exynos_read_domain_from_dt(dev); | |
165 | ||
166 | break; | |
167 | ||
168 | case BUS_NOTIFY_UNBOUND_DRIVER: | |
169 | exynos_remove_device_from_domain(dev); | |
170 | ||
171 | break; | |
172 | } | |
173 | return NOTIFY_DONE; | |
174 | } | |
175 | ||
176 | static struct notifier_block platform_nb = { | |
177 | .notifier_call = exynos_pm_notifier_call, | |
178 | }; | |
179 | ||
8eaa9e42 | 180 | static __init int exynos4_pm_init_power_domain(void) |
91cfbd4e | 181 | { |
8a65d236 | 182 | struct platform_device *pdev; |
91cfbd4e TA |
183 | struct device_node *np; |
184 | ||
185 | for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") { | |
186 | struct exynos_pm_domain *pd; | |
c760569d P |
187 | int on, i; |
188 | struct device *dev; | |
91cfbd4e | 189 | |
8a65d236 | 190 | pdev = of_find_device_by_node(np); |
c760569d | 191 | dev = &pdev->dev; |
8a65d236 | 192 | |
91cfbd4e TA |
193 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
194 | if (!pd) { | |
195 | pr_err("%s: failed to allocate memory for domain\n", | |
196 | __func__); | |
197 | return -ENOMEM; | |
198 | } | |
199 | ||
7add0ec0 TF |
200 | pd->pd.name = kstrdup(np->name, GFP_KERNEL); |
201 | pd->name = pd->pd.name; | |
91cfbd4e TA |
202 | pd->base = of_iomap(np, 0); |
203 | pd->pd.power_off = exynos_pd_power_off; | |
204 | pd->pd.power_on = exynos_pd_power_on; | |
205 | pd->pd.of_node = np; | |
2ed5f236 | 206 | |
c760569d P |
207 | pd->oscclk = clk_get(dev, "oscclk"); |
208 | if (IS_ERR(pd->oscclk)) | |
209 | goto no_clk; | |
210 | ||
211 | for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) { | |
212 | char clk_name[8]; | |
213 | ||
214 | snprintf(clk_name, sizeof(clk_name), "clk%d", i); | |
215 | pd->clk[i] = clk_get(dev, clk_name); | |
216 | if (IS_ERR(pd->clk[i])) | |
217 | break; | |
218 | snprintf(clk_name, sizeof(clk_name), "pclk%d", i); | |
219 | pd->pclk[i] = clk_get(dev, clk_name); | |
220 | if (IS_ERR(pd->pclk[i])) { | |
221 | clk_put(pd->clk[i]); | |
222 | pd->clk[i] = ERR_PTR(-EINVAL); | |
223 | break; | |
224 | } | |
225 | } | |
226 | ||
227 | if (IS_ERR(pd->clk[0])) | |
228 | clk_put(pd->oscclk); | |
229 | ||
230 | no_clk: | |
8a65d236 TF |
231 | platform_set_drvdata(pdev, pd); |
232 | ||
b634e38f | 233 | on = __raw_readl(pd->base + 0x4) & INT_LOCAL_PWR_EN; |
2ed5f236 TF |
234 | |
235 | pm_genpd_init(&pd->pd, NULL, !on); | |
91cfbd4e | 236 | } |
8a65d236 TF |
237 | |
238 | bus_register_notifier(&platform_bus_type, &platform_nb); | |
239 | ||
91cfbd4e TA |
240 | return 0; |
241 | } | |
91cfbd4e | 242 | arch_initcall(exynos4_pm_init_power_domain); |