ARM: OMAP: clocks: Delay clk inits atleast until slab is initialized
[firefly-linux-kernel-4.4.55.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/delay.h>
21 #include <linux/of_address.h>
22 #include <linux/of_platform.h>
23 #include <linux/sched.h>
24
25 #include <mach/regs-pmu.h>
26 #include <plat/devs.h>
27
28 /*
29  * Exynos specific wrapper around the generic power domain
30  */
31 struct exynos_pm_domain {
32         void __iomem *base;
33         char const *name;
34         bool is_off;
35         struct generic_pm_domain pd;
36 };
37
38 static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
39 {
40         struct exynos_pm_domain *pd;
41         void __iomem *base;
42         u32 timeout, pwr;
43         char *op;
44
45         pd = container_of(domain, struct exynos_pm_domain, pd);
46         base = pd->base;
47
48         pwr = power_on ? S5P_INT_LOCAL_PWR_EN : 0;
49         __raw_writel(pwr, base);
50
51         /* Wait max 1ms */
52         timeout = 10;
53
54         while ((__raw_readl(base + 0x4) & S5P_INT_LOCAL_PWR_EN) != pwr) {
55                 if (!timeout) {
56                         op = (power_on) ? "enable" : "disable";
57                         pr_err("Power domain %s %s failed\n", domain->name, op);
58                         return -ETIMEDOUT;
59                 }
60                 timeout--;
61                 cpu_relax();
62                 usleep_range(80, 100);
63         }
64         return 0;
65 }
66
67 static int exynos_pd_power_on(struct generic_pm_domain *domain)
68 {
69         return exynos_pd_power(domain, true);
70 }
71
72 static int exynos_pd_power_off(struct generic_pm_domain *domain)
73 {
74         return exynos_pd_power(domain, false);
75 }
76
77 #define EXYNOS_GPD(PD, BASE, NAME)                      \
78 static struct exynos_pm_domain PD = {                   \
79         .base = (void __iomem *)BASE,                   \
80         .name = NAME,                                   \
81         .pd = {                                         \
82                 .power_off = exynos_pd_power_off,       \
83                 .power_on = exynos_pd_power_on, \
84         },                                              \
85 }
86
87 #ifdef CONFIG_OF
88 static void exynos_add_device_to_domain(struct exynos_pm_domain *pd,
89                                          struct device *dev)
90 {
91         int ret;
92
93         dev_dbg(dev, "adding to power domain %s\n", pd->pd.name);
94
95         while (1) {
96                 ret = pm_genpd_add_device(&pd->pd, dev);
97                 if (ret != -EAGAIN)
98                         break;
99                 cond_resched();
100         }
101
102         pm_genpd_dev_need_restore(dev, true);
103 }
104
105 static void exynos_remove_device_from_domain(struct device *dev)
106 {
107         struct generic_pm_domain *genpd = dev_to_genpd(dev);
108         int ret;
109
110         dev_dbg(dev, "removing from power domain %s\n", genpd->name);
111
112         while (1) {
113                 ret = pm_genpd_remove_device(genpd, dev);
114                 if (ret != -EAGAIN)
115                         break;
116                 cond_resched();
117         }
118 }
119
120 static void exynos_read_domain_from_dt(struct device *dev)
121 {
122         struct platform_device *pd_pdev;
123         struct exynos_pm_domain *pd;
124         struct device_node *node;
125
126         node = of_parse_phandle(dev->of_node, "samsung,power-domain", 0);
127         if (!node)
128                 return;
129         pd_pdev = of_find_device_by_node(node);
130         if (!pd_pdev)
131                 return;
132         pd = platform_get_drvdata(pd_pdev);
133         exynos_add_device_to_domain(pd, dev);
134 }
135
136 static int exynos_pm_notifier_call(struct notifier_block *nb,
137                                     unsigned long event, void *data)
138 {
139         struct device *dev = data;
140
141         switch (event) {
142         case BUS_NOTIFY_BIND_DRIVER:
143                 if (dev->of_node)
144                         exynos_read_domain_from_dt(dev);
145
146                 break;
147
148         case BUS_NOTIFY_UNBOUND_DRIVER:
149                 exynos_remove_device_from_domain(dev);
150
151                 break;
152         }
153         return NOTIFY_DONE;
154 }
155
156 static struct notifier_block platform_nb = {
157         .notifier_call = exynos_pm_notifier_call,
158 };
159
160 static __init int exynos_pm_dt_parse_domains(void)
161 {
162         struct platform_device *pdev;
163         struct device_node *np;
164
165         for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
166                 struct exynos_pm_domain *pd;
167                 int on;
168
169                 pdev = of_find_device_by_node(np);
170
171                 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
172                 if (!pd) {
173                         pr_err("%s: failed to allocate memory for domain\n",
174                                         __func__);
175                         return -ENOMEM;
176                 }
177
178                 pd->pd.name = kstrdup(np->name, GFP_KERNEL);
179                 pd->name = pd->pd.name;
180                 pd->base = of_iomap(np, 0);
181                 pd->pd.power_off = exynos_pd_power_off;
182                 pd->pd.power_on = exynos_pd_power_on;
183                 pd->pd.of_node = np;
184
185                 platform_set_drvdata(pdev, pd);
186
187                 on = __raw_readl(pd->base + 0x4) & S5P_INT_LOCAL_PWR_EN;
188
189                 pm_genpd_init(&pd->pd, NULL, !on);
190         }
191
192         bus_register_notifier(&platform_bus_type, &platform_nb);
193
194         return 0;
195 }
196 #else
197 static __init int exynos_pm_dt_parse_domains(void)
198 {
199         return 0;
200 }
201 #endif /* CONFIG_OF */
202
203 static __init __maybe_unused void exynos_pm_add_dev_to_genpd(struct platform_device *pdev,
204                                                 struct exynos_pm_domain *pd)
205 {
206         if (pdev->dev.bus) {
207                 if (!pm_genpd_add_device(&pd->pd, &pdev->dev))
208                         pm_genpd_dev_need_restore(&pdev->dev, true);
209                 else
210                         pr_info("%s: error in adding %s device to %s power"
211                                 "domain\n", __func__, dev_name(&pdev->dev),
212                                 pd->name);
213         }
214 }
215
216 EXYNOS_GPD(exynos4_pd_mfc, S5P_PMU_MFC_CONF, "pd-mfc");
217 EXYNOS_GPD(exynos4_pd_g3d, S5P_PMU_G3D_CONF, "pd-g3d");
218 EXYNOS_GPD(exynos4_pd_lcd0, S5P_PMU_LCD0_CONF, "pd-lcd0");
219 EXYNOS_GPD(exynos4_pd_lcd1, S5P_PMU_LCD1_CONF, "pd-lcd1");
220 EXYNOS_GPD(exynos4_pd_tv, S5P_PMU_TV_CONF, "pd-tv");
221 EXYNOS_GPD(exynos4_pd_cam, S5P_PMU_CAM_CONF, "pd-cam");
222 EXYNOS_GPD(exynos4_pd_gps, S5P_PMU_GPS_CONF, "pd-gps");
223
224 static struct exynos_pm_domain *exynos4_pm_domains[] = {
225         &exynos4_pd_mfc,
226         &exynos4_pd_g3d,
227         &exynos4_pd_lcd0,
228         &exynos4_pd_lcd1,
229         &exynos4_pd_tv,
230         &exynos4_pd_cam,
231         &exynos4_pd_gps,
232 };
233
234 static __init int exynos4_pm_init_power_domain(void)
235 {
236         int idx;
237
238         if (of_have_populated_dt())
239                 return exynos_pm_dt_parse_domains();
240
241         for (idx = 0; idx < ARRAY_SIZE(exynos4_pm_domains); idx++) {
242                 struct exynos_pm_domain *pd = exynos4_pm_domains[idx];
243                 int on = __raw_readl(pd->base + 0x4) & S5P_INT_LOCAL_PWR_EN;
244
245                 pm_genpd_init(&pd->pd, NULL, !on);
246         }
247
248 #ifdef CONFIG_S5P_DEV_FIMD0
249         exynos_pm_add_dev_to_genpd(&s5p_device_fimd0, &exynos4_pd_lcd0);
250 #endif
251 #ifdef CONFIG_S5P_DEV_TV
252         exynos_pm_add_dev_to_genpd(&s5p_device_hdmi, &exynos4_pd_tv);
253         exynos_pm_add_dev_to_genpd(&s5p_device_mixer, &exynos4_pd_tv);
254 #endif
255 #ifdef CONFIG_S5P_DEV_MFC
256         exynos_pm_add_dev_to_genpd(&s5p_device_mfc, &exynos4_pd_mfc);
257 #endif
258 #ifdef CONFIG_S5P_DEV_FIMC0
259         exynos_pm_add_dev_to_genpd(&s5p_device_fimc0, &exynos4_pd_cam);
260 #endif
261 #ifdef CONFIG_S5P_DEV_FIMC1
262         exynos_pm_add_dev_to_genpd(&s5p_device_fimc1, &exynos4_pd_cam);
263 #endif
264 #ifdef CONFIG_S5P_DEV_FIMC2
265         exynos_pm_add_dev_to_genpd(&s5p_device_fimc2, &exynos4_pd_cam);
266 #endif
267 #ifdef CONFIG_S5P_DEV_FIMC3
268         exynos_pm_add_dev_to_genpd(&s5p_device_fimc3, &exynos4_pd_cam);
269 #endif
270 #ifdef CONFIG_S5P_DEV_CSIS0
271         exynos_pm_add_dev_to_genpd(&s5p_device_mipi_csis0, &exynos4_pd_cam);
272 #endif
273 #ifdef CONFIG_S5P_DEV_CSIS1
274         exynos_pm_add_dev_to_genpd(&s5p_device_mipi_csis1, &exynos4_pd_cam);
275 #endif
276 #ifdef CONFIG_S5P_DEV_G2D
277         exynos_pm_add_dev_to_genpd(&s5p_device_g2d, &exynos4_pd_lcd0);
278 #endif
279 #ifdef CONFIG_S5P_DEV_JPEG
280         exynos_pm_add_dev_to_genpd(&s5p_device_jpeg, &exynos4_pd_cam);
281 #endif
282         return 0;
283 }
284 arch_initcall(exynos4_pm_init_power_domain);
285
286 int __init exynos_pm_late_initcall(void)
287 {
288         pm_genpd_poweroff_unused();
289         return 0;
290 }