blob: f7456ef5fd3039015d2c2e5c3563ba504dc8aad0 [file] [log] [blame]
Praveen Chidambaram85b7b282012-04-16 13:45:15 -06001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/platform_device.h>
19#include <linux/of.h>
20#include <mach/mpm.h>
Girish Mahadevan40abbe12012-04-25 14:58:13 -060021#include "lpm_resources.h"
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -060022#include "pm.h"
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060023#include "rpm-notifier.h"
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060024
25static struct msm_rpmrs_level *msm_lpm_levels;
26static int msm_lpm_level_count;
27
Girish Mahadevan40abbe12012-04-25 14:58:13 -060028static void msm_lpm_level_update(void)
29{
30 unsigned int lpm_level;
31 struct msm_rpmrs_level *level = NULL;
32
33 for (lpm_level = 0; lpm_level < msm_lpm_level_count; lpm_level++) {
34 level = &msm_lpm_levels[lpm_level];
35 level->available =
36 !msm_lpm_level_beyond_limit(&level->rs_limits);
37 }
38}
39
40int msm_lpm_enter_sleep(uint32_t sclk_count, void *limits,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060041 bool from_idle, bool notify_rpm)
42{
Girish Mahadevan40abbe12012-04-25 14:58:13 -060043 int ret = 0;
Mahesh Sivasubramanian2efbc352012-07-18 14:15:44 -060044 struct msm_rpmrs_limits *l = (struct msm_rpmrs_limits *)limits;
Girish Mahadevan40abbe12012-04-25 14:58:13 -060045
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060046 ret = msm_rpm_enter_sleep();
47 if (ret) {
48 pr_warn("%s(): RPM failed to enter sleep err:%d\n",
49 __func__, ret);
50 goto bail;
51 }
Mahesh Sivasubramanian2efbc352012-07-18 14:15:44 -060052 ret = msm_lpmrs_enter_sleep(sclk_count, l, from_idle, notify_rpm);
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060053bail:
Girish Mahadevan40abbe12012-04-25 14:58:13 -060054 return ret;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060055}
56
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -060057static void msm_lpm_exit_sleep(void *limits, bool from_idle,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060058 bool notify_rpm, bool collapsed)
59{
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060060 msm_rpm_exit_sleep();
Girish Mahadevana9964a52012-06-29 10:14:09 -060061 msm_lpmrs_exit_sleep((struct msm_rpmrs_limits *)limits,
62 from_idle, notify_rpm, collapsed);
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060063}
64
Girish Mahadevan40abbe12012-04-25 14:58:13 -060065void msm_lpm_show_resources(void)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060066{
67 /* TODO */
68 return;
69}
70
Stephen Boyd3f4bac22012-05-30 10:03:13 -070071s32 msm_cpuidle_get_deep_idle_latency(void)
72{
73 int i;
74 struct msm_rpmrs_level *level = msm_lpm_levels, *best = level;
75
76 if (!level)
77 return 0;
78
79 for (i = 0; i < msm_lpm_level_count; i++, level++) {
80 if (!level->available)
81 continue;
82 if (level->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
83 continue;
84 /* Pick the first power collapse mode by default */
85 if (best->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
86 best = level;
87 /* Find the lowest latency for power collapse */
88 if (level->latency_us < best->latency_us)
89 best = level;
90 }
91 return best->latency_us - 1;
92}
93
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -060094static void *msm_lpm_lowest_limits(bool from_idle,
95 enum msm_pm_sleep_mode sleep_mode, uint32_t latency_us,
96 uint32_t sleep_us, uint32_t *power)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060097{
98 unsigned int cpu = smp_processor_id();
99 struct msm_rpmrs_level *best_level = NULL;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600100 uint32_t pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600101 int i;
102
103 if (!msm_lpm_levels)
104 return NULL;
105
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600106 msm_lpm_level_update();
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600107
108 for (i = 0; i < msm_lpm_level_count; i++) {
109 struct msm_rpmrs_level *level = &msm_lpm_levels[i];
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600110
111 if (!level->available)
112 continue;
113
114 if (sleep_mode != level->sleep_mode)
115 continue;
116
117 if (latency_us < level->latency_us)
118 continue;
119
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600120 if (sleep_us <= 1) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600121 pwr = level->energy_overhead;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600122 } else if (sleep_us <= level->time_overhead_us) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600123 pwr = level->energy_overhead / sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600124 } else if ((sleep_us >> 10) > level->time_overhead_us) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600125 pwr = level->steady_state_power;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600126 } else {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600127 pwr = level->steady_state_power;
128 pwr -= (level->time_overhead_us *
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600129 level->steady_state_power)/sleep_us;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600130 pwr += level->energy_overhead / sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600131 }
132
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600133 if (!best_level || best_level->rs_limits.power[cpu] >= pwr) {
134
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600135 level->rs_limits.latency_us[cpu] = level->latency_us;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600136 level->rs_limits.power[cpu] = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600137 best_level = level;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600138
139 if (power)
140 *power = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600141 }
142 }
143
144 return best_level ? &best_level->rs_limits : NULL;
145}
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600146static struct msm_pm_sleep_ops msm_lpm_ops = {
147 .lowest_limits = msm_lpm_lowest_limits,
148 .enter_sleep = msm_lpm_enter_sleep,
149 .exit_sleep = msm_lpm_exit_sleep,
150};
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600151
152static int __devinit msm_lpm_levels_probe(struct platform_device *pdev)
153{
154 struct msm_rpmrs_level *levels = NULL;
155 struct msm_rpmrs_level *level = NULL;
156 struct device_node *node = NULL;
157 char *key = NULL;
158 uint32_t val = 0;
159 int ret = 0;
160 uint32_t num_levels = 0;
161 int idx = 0;
162
163 for_each_child_of_node(pdev->dev.of_node, node)
164 num_levels++;
165
166 levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level),
167 GFP_KERNEL);
168 if (!levels)
169 return -ENOMEM;
170
171 for_each_child_of_node(pdev->dev.of_node, node) {
172 level = &levels[idx++];
173 level->available = false;
174
175 key = "qcom,mode";
176 ret = of_property_read_u32(node, key, &val);
177 if (ret)
178 goto fail;
179 level->sleep_mode = val;
180
181 key = "qcom,xo";
182 ret = of_property_read_u32(node, key, &val);
183 if (ret)
184 goto fail;
185 level->rs_limits.pxo = val;
186
187 key = "qcom,l2";
188 ret = of_property_read_u32(node, key, &val);
189 if (ret)
190 goto fail;
191 level->rs_limits.l2_cache = val;
192
193 key = "qcom,vdd-dig-upper-bound";
194 ret = of_property_read_u32(node, key, &val);
195 if (ret)
196 goto fail;
197 level->rs_limits.vdd_dig_upper_bound = val;
198
199 key = "qcom,vdd-dig-lower-bound";
200 ret = of_property_read_u32(node, key, &val);
201 if (ret)
202 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600203 level->rs_limits.vdd_dig_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600204
205 key = "qcom,vdd-mem-upper-bound";
206 ret = of_property_read_u32(node, key, &val);
207 if (ret)
208 goto fail;
209 level->rs_limits.vdd_mem_upper_bound = val;
210
211 key = "qcom,vdd-mem-lower-bound";
212 ret = of_property_read_u32(node, key, &val);
213 if (ret)
214 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600215 level->rs_limits.vdd_mem_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600216
217 key = "qcom,latency-us";
218 ret = of_property_read_u32(node, key, &val);
219 if (ret)
220 goto fail;
221 level->latency_us = val;
222
223 key = "qcom,ss-power";
224 ret = of_property_read_u32(node, key, &val);
225 if (ret)
226 goto fail;
227 level->steady_state_power = val;
228
229 key = "qcom,energy-overhead";
230 ret = of_property_read_u32(node, key, &val);
231 if (ret)
232 goto fail;
233 level->energy_overhead = val;
234
235 key = "qcom,time-overhead";
236 ret = of_property_read_u32(node, key, &val);
237 if (ret)
238 goto fail;
239 level->time_overhead_us = val;
240
241 level->available = true;
242 }
243
244 msm_lpm_levels = levels;
245 msm_lpm_level_count = idx;
246
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600247 msm_pm_set_sleep_ops(&msm_lpm_ops);
248
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600249 return 0;
250fail:
251 pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key);
252 kfree(levels);
253 return -EFAULT;
254}
255
256static struct of_device_id msm_lpm_levels_match_table[] = {
257 {.compatible = "qcom,lpm-levels"},
258 {},
259};
260
261static struct platform_driver msm_lpm_levels_driver = {
262 .probe = msm_lpm_levels_probe,
263 .driver = {
264 .name = "lpm-levels",
265 .owner = THIS_MODULE,
266 .of_match_table = msm_lpm_levels_match_table,
267 },
268};
269
270static int __init msm_lpm_levels_module_init(void)
271{
272 return platform_driver_register(&msm_lpm_levels_driver);
273}
274late_initcall(msm_lpm_levels_module_init);