blob: dec62f05dca43642d04efb33001da3077ab52d6c [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 Sivasubramanian9b0f4122013-02-15 14:20:52 -070060 int debug_mask;
61 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 Sivasubramanian9b0f4122013-02-15 14:20:52 -070069 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;
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060074 }
Mahesh Sivasubramanian9b0f4122013-02-15 14:20:52 -070075 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
90 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}
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700175static bool msm_lpm_irqs_detectable(struct msm_rpmrs_limits *limits,
176 bool irqs_detectable, bool gpio_detectable)
177{
178 if (!limits->irqs_detectable)
179 return irqs_detectable;
180
181 if (!limits->gpio_detectable)
182 return gpio_detectable;
183
184 return true;
185
186}
Stephen Boyd3f4bac22012-05-30 10:03:13 -0700187
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600188static void *msm_lpm_lowest_limits(bool from_idle,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600189 enum msm_pm_sleep_mode sleep_mode,
190 struct msm_pm_time_params *time_param, uint32_t *power)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600191{
192 unsigned int cpu = smp_processor_id();
193 struct msm_rpmrs_level *best_level = NULL;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600194 uint32_t pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600195 int i;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800196 int best_level_iter = msm_lpm_level_count + 1;
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700197 bool irqs_detect = false;
198 bool gpio_detect = false;
199
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600200 if (!msm_lpm_levels)
201 return NULL;
202
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600203 msm_lpm_level_update();
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600204
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700205 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
206 irqs_detect = msm_mpm_irqs_detectable(from_idle);
207 gpio_detect = msm_mpm_gpio_irqs_detectable(from_idle);
208 }
209
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600210 for (i = 0; i < msm_lpm_level_count; i++) {
211 struct msm_rpmrs_level *level = &msm_lpm_levels[i];
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600212
213 if (!level->available)
214 continue;
215
216 if (sleep_mode != level->sleep_mode)
217 continue;
218
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600219 if (time_param->latency_us < level->latency_us)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600220 continue;
221
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700222 if ((sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) &&
223 !msm_lpm_irqs_detectable(&level->rs_limits,
224 irqs_detect, gpio_detect))
225 continue;
226
Mahesh Sivasubramanian9063a292012-11-09 09:15:30 -0700227 if ((MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE == sleep_mode)
228 || (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == sleep_mode))
229 if (!cpu && msm_rpm_waiting_for_ack())
230 break;
231
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600232 if (time_param->sleep_us <= 1) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600233 pwr = level->energy_overhead;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600234 } else if (time_param->sleep_us <= level->time_overhead_us) {
235 pwr = level->energy_overhead / time_param->sleep_us;
236 } else if ((time_param->sleep_us >> 10)
237 > level->time_overhead_us) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600238 pwr = level->steady_state_power;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600239 } else {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600240 pwr = level->steady_state_power;
241 pwr -= (level->time_overhead_us *
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600242 level->steady_state_power) /
243 time_param->sleep_us;
244 pwr += level->energy_overhead / time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600245 }
246
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600247 if (!best_level || best_level->rs_limits.power[cpu] >= pwr) {
248
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600249 level->rs_limits.latency_us[cpu] = level->latency_us;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600250 level->rs_limits.power[cpu] = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600251 best_level = level;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800252 best_level_iter = i;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600253 if (power)
254 *power = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600255 }
256 }
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800257 if (best_level && !lpm_level_permitted(best_level_iter))
258 best_level = NULL;
259 else
260 per_cpu(msm_lpm_sleep_time, cpu) =
261 time_param->modified_time_us ?
262 time_param->modified_time_us : time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600263
264 return best_level ? &best_level->rs_limits : NULL;
265}
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800266
267static struct lpm_test_platform_data lpm_test_pdata;
268
269static struct platform_device msm_lpm_test_device = {
270 .name = "lpm_test",
271 .id = -1,
272 .dev = {
273 .platform_data = &lpm_test_pdata,
274 },
275};
276
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600277static struct msm_pm_sleep_ops msm_lpm_ops = {
278 .lowest_limits = msm_lpm_lowest_limits,
279 .enter_sleep = msm_lpm_enter_sleep,
280 .exit_sleep = msm_lpm_exit_sleep,
281};
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600282
283static int __devinit msm_lpm_levels_probe(struct platform_device *pdev)
284{
285 struct msm_rpmrs_level *levels = NULL;
286 struct msm_rpmrs_level *level = NULL;
287 struct device_node *node = NULL;
288 char *key = NULL;
289 uint32_t val = 0;
290 int ret = 0;
291 uint32_t num_levels = 0;
292 int idx = 0;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800293 unsigned int m_cpu = 0;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600294
295 for_each_child_of_node(pdev->dev.of_node, node)
296 num_levels++;
297
298 levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level),
299 GFP_KERNEL);
300 if (!levels)
301 return -ENOMEM;
302
303 for_each_child_of_node(pdev->dev.of_node, node) {
304 level = &levels[idx++];
305 level->available = false;
306
307 key = "qcom,mode";
308 ret = of_property_read_u32(node, key, &val);
309 if (ret)
310 goto fail;
311 level->sleep_mode = val;
312
313 key = "qcom,xo";
314 ret = of_property_read_u32(node, key, &val);
315 if (ret)
316 goto fail;
317 level->rs_limits.pxo = val;
318
319 key = "qcom,l2";
320 ret = of_property_read_u32(node, key, &val);
321 if (ret)
322 goto fail;
323 level->rs_limits.l2_cache = val;
324
325 key = "qcom,vdd-dig-upper-bound";
326 ret = of_property_read_u32(node, key, &val);
327 if (ret)
328 goto fail;
329 level->rs_limits.vdd_dig_upper_bound = val;
330
331 key = "qcom,vdd-dig-lower-bound";
332 ret = of_property_read_u32(node, key, &val);
333 if (ret)
334 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600335 level->rs_limits.vdd_dig_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600336
337 key = "qcom,vdd-mem-upper-bound";
338 ret = of_property_read_u32(node, key, &val);
339 if (ret)
340 goto fail;
341 level->rs_limits.vdd_mem_upper_bound = val;
342
343 key = "qcom,vdd-mem-lower-bound";
344 ret = of_property_read_u32(node, key, &val);
345 if (ret)
346 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600347 level->rs_limits.vdd_mem_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600348
Mahesh Sivasubramanianb71ce092013-01-08 13:44:23 -0700349 key = "qcom,gpio-detectable";
350 level->rs_limits.gpio_detectable =
351 of_property_read_bool(node, key);
352
353 key = "qcom,irqs-detectable";
354 level->rs_limits.irqs_detectable =
355 of_property_read_bool(node, key);
356
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600357 key = "qcom,latency-us";
358 ret = of_property_read_u32(node, key, &val);
359 if (ret)
360 goto fail;
361 level->latency_us = val;
362
363 key = "qcom,ss-power";
364 ret = of_property_read_u32(node, key, &val);
365 if (ret)
366 goto fail;
367 level->steady_state_power = val;
368
369 key = "qcom,energy-overhead";
370 ret = of_property_read_u32(node, key, &val);
371 if (ret)
372 goto fail;
373 level->energy_overhead = val;
374
375 key = "qcom,time-overhead";
376 ret = of_property_read_u32(node, key, &val);
377 if (ret)
378 goto fail;
379 level->time_overhead_us = val;
380
381 level->available = true;
382 }
383
384 msm_lpm_levels = levels;
385 msm_lpm_level_count = idx;
386
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800387 lpm_test_pdata.msm_lpm_test_levels = msm_lpm_levels;
388 lpm_test_pdata.msm_lpm_test_level_count = msm_lpm_level_count;
Priyanka Mathur32d51ce2013-01-17 10:38:05 -0800389 key = "qcom,use-qtimer";
390 lpm_test_pdata.use_qtimer =
391 of_property_read_bool(pdev->dev.of_node, key);
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800392
393 for_each_possible_cpu(m_cpu)
394 per_cpu(lpm_permitted_level, m_cpu) =
395 msm_lpm_level_count + 1;
396
397 platform_device_register(&msm_lpm_test_device);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600398 msm_pm_set_sleep_ops(&msm_lpm_ops);
399
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600400 return 0;
401fail:
402 pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key);
403 kfree(levels);
404 return -EFAULT;
405}
406
407static struct of_device_id msm_lpm_levels_match_table[] = {
408 {.compatible = "qcom,lpm-levels"},
409 {},
410};
411
412static struct platform_driver msm_lpm_levels_driver = {
413 .probe = msm_lpm_levels_probe,
414 .driver = {
415 .name = "lpm-levels",
416 .owner = THIS_MODULE,
417 .of_match_table = msm_lpm_levels_match_table,
418 },
419};
420
421static int __init msm_lpm_levels_module_init(void)
422{
423 return platform_driver_register(&msm_lpm_levels_driver);
424}
425late_initcall(msm_lpm_levels_module_init);