blob: b84d345c2764aa6ca8e702fa078c466498772245 [file] [log] [blame]
Priyanka Mathur3abfd442013-01-11 12:58:51 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Praveen Chidambaram85b7b282012-04-16 13:45:15 -06002 *
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
Mahesh Sivasubramanianfae923f2012-10-08 16:22:51 -060025
26enum {
27 MSM_LPM_LVL_DBG_SUSPEND_LIMITS = BIT(0),
28 MSM_LPM_LVL_DBG_IDLE_LIMITS = BIT(1),
29};
30
31static int msm_lpm_lvl_dbg_msk;
32
33module_param_named(
34 debug_mask, msm_lpm_lvl_dbg_msk, int, S_IRUGO | S_IWUSR | S_IWGRP
35);
36
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060037static struct msm_rpmrs_level *msm_lpm_levels;
38static int msm_lpm_level_count;
39
Priyanka Mathur3abfd442013-01-11 12:58:51 -080040static DEFINE_PER_CPU(uint32_t , msm_lpm_sleep_time);
41static DEFINE_PER_CPU(int , lpm_permitted_level);
42static DEFINE_PER_CPU(struct atomic_notifier_head, lpm_notify_head);
43
Girish Mahadevan40abbe12012-04-25 14:58:13 -060044static void msm_lpm_level_update(void)
45{
46 unsigned int lpm_level;
47 struct msm_rpmrs_level *level = NULL;
48
49 for (lpm_level = 0; lpm_level < msm_lpm_level_count; lpm_level++) {
50 level = &msm_lpm_levels[lpm_level];
51 level->available =
52 !msm_lpm_level_beyond_limit(&level->rs_limits);
53 }
54}
55
56int msm_lpm_enter_sleep(uint32_t sclk_count, void *limits,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060057 bool from_idle, bool notify_rpm)
58{
Girish Mahadevan40abbe12012-04-25 14:58:13 -060059 int ret = 0;
Mahesh Sivasubramanianfae923f2012-10-08 16:22:51 -060060 int debug_mask;
Mahesh Sivasubramanian2efbc352012-07-18 14:15:44 -060061 struct msm_rpmrs_limits *l = (struct msm_rpmrs_limits *)limits;
Priyanka Mathur3abfd442013-01-11 12:58:51 -080062 struct msm_lpm_sleep_data sleep_data;
63
64 sleep_data.limits = limits;
65 sleep_data.kernel_sleep = __get_cpu_var(msm_lpm_sleep_time);
66 atomic_notifier_call_chain(&__get_cpu_var(lpm_notify_head),
67 MSM_LPM_STATE_ENTER, &sleep_data);
Girish Mahadevan40abbe12012-04-25 14:58:13 -060068
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060069 ret = msm_rpm_enter_sleep();
70 if (ret) {
71 pr_warn("%s(): RPM failed to enter sleep err:%d\n",
72 __func__, ret);
73 goto bail;
74 }
Mahesh Sivasubramanianfae923f2012-10-08 16:22:51 -060075 if (from_idle)
76 debug_mask = msm_lpm_lvl_dbg_msk &
77 MSM_LPM_LVL_DBG_IDLE_LIMITS;
78 else
79 debug_mask = msm_lpm_lvl_dbg_msk &
80 MSM_LPM_LVL_DBG_SUSPEND_LIMITS;
81
82 if (debug_mask)
83 pr_info("%s(): pxo:%d l2:%d mem:0x%x(0x%x) dig:0x%x(0x%x)\n",
84 __func__, l->pxo, l->l2_cache,
85 l->vdd_mem_lower_bound,
86 l->vdd_mem_upper_bound,
87 l->vdd_dig_lower_bound,
88 l->vdd_dig_upper_bound);
89
Mahesh Sivasubramanian2efbc352012-07-18 14:15:44 -060090 ret = msm_lpmrs_enter_sleep(sclk_count, l, from_idle, notify_rpm);
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060091bail:
Girish Mahadevan40abbe12012-04-25 14:58:13 -060092 return ret;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060093}
94
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -060095static void msm_lpm_exit_sleep(void *limits, bool from_idle,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060096 bool notify_rpm, bool collapsed)
97{
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060098 msm_rpm_exit_sleep();
Girish Mahadevana9964a52012-06-29 10:14:09 -060099 msm_lpmrs_exit_sleep((struct msm_rpmrs_limits *)limits,
100 from_idle, notify_rpm, collapsed);
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800101 atomic_notifier_call_chain(&__get_cpu_var(lpm_notify_head),
102 MSM_LPM_STATE_EXIT, NULL);
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600103}
104
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600105void msm_lpm_show_resources(void)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600106{
107 /* TODO */
108 return;
109}
110
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800111uint32_t msm_pm_get_pxo(struct msm_rpmrs_limits *limits)
112{
113 return limits->pxo;
114}
115
116uint32_t msm_pm_get_l2_cache(struct msm_rpmrs_limits *limits)
117{
118 return limits->l2_cache;
119}
120
121uint32_t msm_pm_get_vdd_mem(struct msm_rpmrs_limits *limits)
122{
123 return limits->vdd_mem_upper_bound;
124}
125
126uint32_t msm_pm_get_vdd_dig(struct msm_rpmrs_limits *limits)
127{
128 return limits->vdd_dig_upper_bound;
129}
130
131static bool lpm_level_permitted(int cur_level_count)
132{
133 if (__get_cpu_var(lpm_permitted_level) == msm_lpm_level_count + 1)
134 return true;
135 return (__get_cpu_var(lpm_permitted_level) == cur_level_count);
136}
137
138int msm_lpm_register_notifier(int cpu, int level_iter,
139 struct notifier_block *nb, bool is_latency_measure)
140{
141 per_cpu(lpm_permitted_level, cpu) = level_iter;
142 return atomic_notifier_chain_register(&per_cpu(lpm_notify_head,
143 cpu), nb);
144}
145
146int msm_lpm_unregister_notifier(int cpu, struct notifier_block *nb)
147{
148 per_cpu(lpm_permitted_level, cpu) = msm_lpm_level_count + 1;
149 return atomic_notifier_chain_unregister(&per_cpu(lpm_notify_head, cpu),
150 nb);
151}
152
Stephen Boyd3f4bac22012-05-30 10:03:13 -0700153s32 msm_cpuidle_get_deep_idle_latency(void)
154{
155 int i;
156 struct msm_rpmrs_level *level = msm_lpm_levels, *best = level;
157
158 if (!level)
159 return 0;
160
161 for (i = 0; i < msm_lpm_level_count; i++, level++) {
162 if (!level->available)
163 continue;
164 if (level->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
165 continue;
166 /* Pick the first power collapse mode by default */
167 if (best->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
168 best = level;
169 /* Find the lowest latency for power collapse */
170 if (level->latency_us < best->latency_us)
171 best = level;
172 }
173 return best->latency_us - 1;
174}
175
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600176static void *msm_lpm_lowest_limits(bool from_idle,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600177 enum msm_pm_sleep_mode sleep_mode,
178 struct msm_pm_time_params *time_param, uint32_t *power)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600179{
180 unsigned int cpu = smp_processor_id();
181 struct msm_rpmrs_level *best_level = NULL;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600182 uint32_t pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600183 int i;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800184 int best_level_iter = msm_lpm_level_count + 1;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600185 if (!msm_lpm_levels)
186 return NULL;
187
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600188 msm_lpm_level_update();
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600189
190 for (i = 0; i < msm_lpm_level_count; i++) {
191 struct msm_rpmrs_level *level = &msm_lpm_levels[i];
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600192
193 if (!level->available)
194 continue;
195
196 if (sleep_mode != level->sleep_mode)
197 continue;
198
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600199 if (time_param->latency_us < level->latency_us)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600200 continue;
201
Mahesh Sivasubramanian9063a292012-11-09 09:15:30 -0700202 if ((MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE == sleep_mode)
203 || (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == sleep_mode))
204 if (!cpu && msm_rpm_waiting_for_ack())
205 break;
206
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600207 if (time_param->sleep_us <= 1) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600208 pwr = level->energy_overhead;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600209 } else if (time_param->sleep_us <= level->time_overhead_us) {
210 pwr = level->energy_overhead / time_param->sleep_us;
211 } else if ((time_param->sleep_us >> 10)
212 > level->time_overhead_us) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600213 pwr = level->steady_state_power;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600214 } else {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600215 pwr = level->steady_state_power;
216 pwr -= (level->time_overhead_us *
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600217 level->steady_state_power) /
218 time_param->sleep_us;
219 pwr += level->energy_overhead / time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600220 }
221
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600222 if (!best_level || best_level->rs_limits.power[cpu] >= pwr) {
223
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600224 level->rs_limits.latency_us[cpu] = level->latency_us;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600225 level->rs_limits.power[cpu] = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600226 best_level = level;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800227 best_level_iter = i;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600228 if (power)
229 *power = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600230 }
231 }
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800232 if (best_level && !lpm_level_permitted(best_level_iter))
233 best_level = NULL;
234 else
235 per_cpu(msm_lpm_sleep_time, cpu) =
236 time_param->modified_time_us ?
237 time_param->modified_time_us : time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600238
239 return best_level ? &best_level->rs_limits : NULL;
240}
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800241
242static struct lpm_test_platform_data lpm_test_pdata;
243
244static struct platform_device msm_lpm_test_device = {
245 .name = "lpm_test",
246 .id = -1,
247 .dev = {
248 .platform_data = &lpm_test_pdata,
249 },
250};
251
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600252static struct msm_pm_sleep_ops msm_lpm_ops = {
253 .lowest_limits = msm_lpm_lowest_limits,
254 .enter_sleep = msm_lpm_enter_sleep,
255 .exit_sleep = msm_lpm_exit_sleep,
256};
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600257
258static int __devinit msm_lpm_levels_probe(struct platform_device *pdev)
259{
260 struct msm_rpmrs_level *levels = NULL;
261 struct msm_rpmrs_level *level = NULL;
262 struct device_node *node = NULL;
263 char *key = NULL;
264 uint32_t val = 0;
265 int ret = 0;
266 uint32_t num_levels = 0;
267 int idx = 0;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800268 unsigned int m_cpu = 0;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600269
270 for_each_child_of_node(pdev->dev.of_node, node)
271 num_levels++;
272
273 levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level),
274 GFP_KERNEL);
275 if (!levels)
276 return -ENOMEM;
277
278 for_each_child_of_node(pdev->dev.of_node, node) {
279 level = &levels[idx++];
280 level->available = false;
281
282 key = "qcom,mode";
283 ret = of_property_read_u32(node, key, &val);
284 if (ret)
285 goto fail;
286 level->sleep_mode = val;
287
288 key = "qcom,xo";
289 ret = of_property_read_u32(node, key, &val);
290 if (ret)
291 goto fail;
292 level->rs_limits.pxo = val;
293
294 key = "qcom,l2";
295 ret = of_property_read_u32(node, key, &val);
296 if (ret)
297 goto fail;
298 level->rs_limits.l2_cache = val;
299
300 key = "qcom,vdd-dig-upper-bound";
301 ret = of_property_read_u32(node, key, &val);
302 if (ret)
303 goto fail;
304 level->rs_limits.vdd_dig_upper_bound = val;
305
306 key = "qcom,vdd-dig-lower-bound";
307 ret = of_property_read_u32(node, key, &val);
308 if (ret)
309 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600310 level->rs_limits.vdd_dig_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600311
312 key = "qcom,vdd-mem-upper-bound";
313 ret = of_property_read_u32(node, key, &val);
314 if (ret)
315 goto fail;
316 level->rs_limits.vdd_mem_upper_bound = val;
317
318 key = "qcom,vdd-mem-lower-bound";
319 ret = of_property_read_u32(node, key, &val);
320 if (ret)
321 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600322 level->rs_limits.vdd_mem_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600323
324 key = "qcom,latency-us";
325 ret = of_property_read_u32(node, key, &val);
326 if (ret)
327 goto fail;
328 level->latency_us = val;
329
330 key = "qcom,ss-power";
331 ret = of_property_read_u32(node, key, &val);
332 if (ret)
333 goto fail;
334 level->steady_state_power = val;
335
336 key = "qcom,energy-overhead";
337 ret = of_property_read_u32(node, key, &val);
338 if (ret)
339 goto fail;
340 level->energy_overhead = val;
341
342 key = "qcom,time-overhead";
343 ret = of_property_read_u32(node, key, &val);
344 if (ret)
345 goto fail;
346 level->time_overhead_us = val;
347
348 level->available = true;
349 }
350
351 msm_lpm_levels = levels;
352 msm_lpm_level_count = idx;
353
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800354 lpm_test_pdata.msm_lpm_test_levels = msm_lpm_levels;
355 lpm_test_pdata.msm_lpm_test_level_count = msm_lpm_level_count;
356
357 for_each_possible_cpu(m_cpu)
358 per_cpu(lpm_permitted_level, m_cpu) =
359 msm_lpm_level_count + 1;
360
361 platform_device_register(&msm_lpm_test_device);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600362 msm_pm_set_sleep_ops(&msm_lpm_ops);
363
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600364 return 0;
365fail:
366 pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key);
367 kfree(levels);
368 return -EFAULT;
369}
370
371static struct of_device_id msm_lpm_levels_match_table[] = {
372 {.compatible = "qcom,lpm-levels"},
373 {},
374};
375
376static struct platform_driver msm_lpm_levels_driver = {
377 .probe = msm_lpm_levels_probe,
378 .driver = {
379 .name = "lpm-levels",
380 .owner = THIS_MODULE,
381 .of_match_table = msm_lpm_levels_match_table,
382 },
383};
384
385static int __init msm_lpm_levels_module_init(void)
386{
387 return platform_driver_register(&msm_lpm_levels_driver);
388}
389late_initcall(msm_lpm_levels_module_init);