Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 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> |
Prathyush K | c760569 | 2014-07-11 08:02:15 +0900 | [diff] [blame] | 20 | #include <linux/clk.h> |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 21 | #include <linux/delay.h> |
| 22 | #include <linux/of_address.h> |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 23 | #include <linux/of_platform.h> |
| 24 | #include <linux/sched.h> |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 25 | |
Pankaj Dubey | b634e38f | 2014-07-08 07:54:19 +0900 | [diff] [blame] | 26 | #define INT_LOCAL_PWR_EN 0x7 |
Prathyush K | c760569 | 2014-07-11 08:02:15 +0900 | [diff] [blame] | 27 | #define MAX_CLK_PER_DOMAIN 4 |
| 28 | |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 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; |
Prathyush K | c760569 | 2014-07-11 08:02:15 +0900 | [diff] [blame] | 37 | struct clk *oscclk; |
| 38 | struct clk *clk[MAX_CLK_PER_DOMAIN]; |
| 39 | struct clk *pclk[MAX_CLK_PER_DOMAIN]; |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 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 | |
Prathyush K | c760569 | 2014-07-11 08:02:15 +0900 | [diff] [blame] | 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 | |
Pankaj Dubey | b634e38f | 2014-07-08 07:54:19 +0900 | [diff] [blame] | 65 | pwr = power_on ? INT_LOCAL_PWR_EN : 0; |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 66 | __raw_writel(pwr, base); |
| 67 | |
| 68 | /* Wait max 1ms */ |
| 69 | timeout = 10; |
| 70 | |
Pankaj Dubey | b634e38f | 2014-07-08 07:54:19 +0900 | [diff] [blame] | 71 | while ((__raw_readl(base + 0x4) & INT_LOCAL_PWR_EN) != pwr) { |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 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 | } |
Prathyush K | c760569 | 2014-07-11 08:02:15 +0900 | [diff] [blame] | 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 | |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 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 | |
Tomasz Figa | 8eaa9e4 | 2013-06-15 09:13:25 +0900 | [diff] [blame] | 108 | static __init int exynos4_pm_init_power_domain(void) |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 109 | { |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 110 | struct platform_device *pdev; |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 111 | struct device_node *np; |
| 112 | |
| 113 | for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") { |
| 114 | struct exynos_pm_domain *pd; |
Prathyush K | c760569 | 2014-07-11 08:02:15 +0900 | [diff] [blame] | 115 | int on, i; |
| 116 | struct device *dev; |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 117 | |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 118 | pdev = of_find_device_by_node(np); |
Prathyush K | c760569 | 2014-07-11 08:02:15 +0900 | [diff] [blame] | 119 | dev = &pdev->dev; |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 120 | |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 121 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
| 122 | if (!pd) { |
| 123 | pr_err("%s: failed to allocate memory for domain\n", |
| 124 | __func__); |
| 125 | return -ENOMEM; |
| 126 | } |
| 127 | |
Tomasz Figa | 7add0ec | 2012-11-22 00:21:12 +0900 | [diff] [blame] | 128 | pd->pd.name = kstrdup(np->name, GFP_KERNEL); |
| 129 | pd->name = pd->pd.name; |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 130 | pd->base = of_iomap(np, 0); |
| 131 | pd->pd.power_off = exynos_pd_power_off; |
| 132 | pd->pd.power_on = exynos_pd_power_on; |
Tomasz Figa | 2ed5f23 | 2012-11-22 00:21:08 +0900 | [diff] [blame] | 133 | |
Prathyush K | c760569 | 2014-07-11 08:02:15 +0900 | [diff] [blame] | 134 | pd->oscclk = clk_get(dev, "oscclk"); |
| 135 | if (IS_ERR(pd->oscclk)) |
| 136 | goto no_clk; |
| 137 | |
| 138 | for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) { |
| 139 | char clk_name[8]; |
| 140 | |
| 141 | snprintf(clk_name, sizeof(clk_name), "clk%d", i); |
| 142 | pd->clk[i] = clk_get(dev, clk_name); |
| 143 | if (IS_ERR(pd->clk[i])) |
| 144 | break; |
| 145 | snprintf(clk_name, sizeof(clk_name), "pclk%d", i); |
| 146 | pd->pclk[i] = clk_get(dev, clk_name); |
| 147 | if (IS_ERR(pd->pclk[i])) { |
| 148 | clk_put(pd->clk[i]); |
| 149 | pd->clk[i] = ERR_PTR(-EINVAL); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (IS_ERR(pd->clk[0])) |
| 155 | clk_put(pd->oscclk); |
| 156 | |
| 157 | no_clk: |
Pankaj Dubey | b634e38f | 2014-07-08 07:54:19 +0900 | [diff] [blame] | 158 | on = __raw_readl(pd->base + 0x4) & INT_LOCAL_PWR_EN; |
Tomasz Figa | 2ed5f23 | 2012-11-22 00:21:08 +0900 | [diff] [blame] | 159 | |
| 160 | pm_genpd_init(&pd->pd, NULL, !on); |
Tomasz Figa | a4a8c2c | 2014-09-19 20:27:43 +0200 | [diff] [blame] | 161 | of_genpd_add_provider_simple(np, &pd->pd); |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 162 | } |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 163 | |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 164 | return 0; |
| 165 | } |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 166 | arch_initcall(exynos4_pm_init_power_domain); |