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