blob: 539a4fef891551c8a8a3755103e8f87f12fb6940 [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 if (from_idle)
70 debug_mask = msm_lpm_lvl_dbg_msk &
Priyanka Mathur86cfc762013-01-10 17:03:04 -080071 MSM_LPM_LVL_DBG_IDLE_LIMITS;
Mahesh Sivasubramanian9b0f4122013-02-15 14:20:52 -070072 else
73 debug_mask = msm_lpm_lvl_dbg_msk &
Priyanka Mathur86cfc762013-01-10 17:03:04 -080074 MSM_LPM_LVL_DBG_SUSPEND_LIMITS;
Mahesh Sivasubramanian9b0f4122013-02-15 14:20:52 -070075
76 if (debug_mask)
77 pr_info("%s(): pxo:%d l2:%d mem:0x%x(0x%x) dig:0x%x(0x%x)\n",
78 __func__, l->pxo, l->l2_cache,
79 l->vdd_mem_lower_bound,
80 l->vdd_mem_upper_bound,
81 l->vdd_dig_lower_bound,
82 l->vdd_dig_upper_bound);
83
84 ret = msm_lpmrs_enter_sleep(sclk_count, l, from_idle, notify_rpm);
Mahesh Sivasubramanian0558d4b2012-10-12 18:05:28 -060085 if (ret) {
86 pr_warn("%s() LPM resources failed to enter sleep\n",
87 __func__);
88 goto bail;
89 }
Priyanka Mathur86cfc762013-01-10 17:03:04 -080090 if (notify_rpm) {
91 ret = msm_rpm_enter_sleep(debug_mask);
92 if (ret) {
93 pr_warn("%s(): RPM failed to enter sleep err:%d\n",
94 __func__, ret);
95 goto bail;
96 }
97 }
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060098bail:
Girish Mahadevan40abbe12012-04-25 14:58:13 -060099 return ret;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600100}
101
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600102static void msm_lpm_exit_sleep(void *limits, bool from_idle,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600103 bool notify_rpm, bool collapsed)
104{
Mahesh Sivasubramanian0558d4b2012-10-12 18:05:28 -0600105
Girish Mahadevana9964a52012-06-29 10:14:09 -0600106 msm_lpmrs_exit_sleep((struct msm_rpmrs_limits *)limits,
107 from_idle, notify_rpm, collapsed);
Priyanka Mathur86cfc762013-01-10 17:03:04 -0800108 if (notify_rpm)
109 msm_rpm_exit_sleep();
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800110 atomic_notifier_call_chain(&__get_cpu_var(lpm_notify_head),
111 MSM_LPM_STATE_EXIT, NULL);
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600112}
113
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600114void msm_lpm_show_resources(void)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600115{
116 /* TODO */
117 return;
118}
119
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800120uint32_t msm_pm_get_pxo(struct msm_rpmrs_limits *limits)
121{
122 return limits->pxo;
123}
124
125uint32_t msm_pm_get_l2_cache(struct msm_rpmrs_limits *limits)
126{
127 return limits->l2_cache;
128}
129
130uint32_t msm_pm_get_vdd_mem(struct msm_rpmrs_limits *limits)
131{
132 return limits->vdd_mem_upper_bound;
133}
134
135uint32_t msm_pm_get_vdd_dig(struct msm_rpmrs_limits *limits)
136{
137 return limits->vdd_dig_upper_bound;
138}
139
140static bool lpm_level_permitted(int cur_level_count)
141{
142 if (__get_cpu_var(lpm_permitted_level) == msm_lpm_level_count + 1)
143 return true;
144 return (__get_cpu_var(lpm_permitted_level) == cur_level_count);
145}
146
147int msm_lpm_register_notifier(int cpu, int level_iter,
148 struct notifier_block *nb, bool is_latency_measure)
149{
150 per_cpu(lpm_permitted_level, cpu) = level_iter;
151 return atomic_notifier_chain_register(&per_cpu(lpm_notify_head,
152 cpu), nb);
153}
154
155int msm_lpm_unregister_notifier(int cpu, struct notifier_block *nb)
156{
157 per_cpu(lpm_permitted_level, cpu) = msm_lpm_level_count + 1;
158 return atomic_notifier_chain_unregister(&per_cpu(lpm_notify_head, cpu),
159 nb);
160}
161
Stephen Boyd3f4bac22012-05-30 10:03:13 -0700162s32 msm_cpuidle_get_deep_idle_latency(void)
163{
164 int i;
165 struct msm_rpmrs_level *level = msm_lpm_levels, *best = level;
166
167 if (!level)
168 return 0;
169
170 for (i = 0; i < msm_lpm_level_count; i++, level++) {
171 if (!level->available)
172 continue;
173 if (level->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
174 continue;
175 /* Pick the first power collapse mode by default */
176 if (best->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
177 best = level;
178 /* Find the lowest latency for power collapse */
179 if (level->latency_us < best->latency_us)
180 best = level;
181 }
182 return best->latency_us - 1;
183}
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700184static bool msm_lpm_irqs_detectable(struct msm_rpmrs_limits *limits,
185 bool irqs_detectable, bool gpio_detectable)
186{
187 if (!limits->irqs_detectable)
188 return irqs_detectable;
189
190 if (!limits->gpio_detectable)
191 return gpio_detectable;
192
193 return true;
194
195}
Stephen Boyd3f4bac22012-05-30 10:03:13 -0700196
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600197static void *msm_lpm_lowest_limits(bool from_idle,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600198 enum msm_pm_sleep_mode sleep_mode,
199 struct msm_pm_time_params *time_param, uint32_t *power)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600200{
201 unsigned int cpu = smp_processor_id();
202 struct msm_rpmrs_level *best_level = NULL;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600203 uint32_t pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600204 int i;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800205 int best_level_iter = msm_lpm_level_count + 1;
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700206 bool irqs_detect = false;
207 bool gpio_detect = false;
208
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600209 if (!msm_lpm_levels)
210 return NULL;
211
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600212 msm_lpm_level_update();
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600213
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700214 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
215 irqs_detect = msm_mpm_irqs_detectable(from_idle);
216 gpio_detect = msm_mpm_gpio_irqs_detectable(from_idle);
217 }
218
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600219 for (i = 0; i < msm_lpm_level_count; i++) {
220 struct msm_rpmrs_level *level = &msm_lpm_levels[i];
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600221
222 if (!level->available)
223 continue;
224
225 if (sleep_mode != level->sleep_mode)
226 continue;
227
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600228 if (time_param->latency_us < level->latency_us)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600229 continue;
230
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700231 if ((sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) &&
232 !msm_lpm_irqs_detectable(&level->rs_limits,
233 irqs_detect, gpio_detect))
234 continue;
235
Mahesh Sivasubramanian9063a292012-11-09 09:15:30 -0700236 if ((MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE == sleep_mode)
237 || (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == sleep_mode))
238 if (!cpu && msm_rpm_waiting_for_ack())
239 break;
240
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600241 if (time_param->sleep_us <= 1) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600242 pwr = level->energy_overhead;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600243 } else if (time_param->sleep_us <= level->time_overhead_us) {
244 pwr = level->energy_overhead / time_param->sleep_us;
245 } else if ((time_param->sleep_us >> 10)
246 > level->time_overhead_us) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600247 pwr = level->steady_state_power;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600248 } else {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600249 pwr = level->steady_state_power;
250 pwr -= (level->time_overhead_us *
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600251 level->steady_state_power) /
252 time_param->sleep_us;
253 pwr += level->energy_overhead / time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600254 }
255
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600256 if (!best_level || best_level->rs_limits.power[cpu] >= pwr) {
257
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600258 level->rs_limits.latency_us[cpu] = level->latency_us;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600259 level->rs_limits.power[cpu] = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600260 best_level = level;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800261 best_level_iter = i;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600262 if (power)
263 *power = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600264 }
265 }
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800266 if (best_level && !lpm_level_permitted(best_level_iter))
267 best_level = NULL;
268 else
269 per_cpu(msm_lpm_sleep_time, cpu) =
270 time_param->modified_time_us ?
271 time_param->modified_time_us : time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600272
273 return best_level ? &best_level->rs_limits : NULL;
274}
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800275
276static struct lpm_test_platform_data lpm_test_pdata;
277
278static struct platform_device msm_lpm_test_device = {
279 .name = "lpm_test",
280 .id = -1,
281 .dev = {
282 .platform_data = &lpm_test_pdata,
283 },
284};
285
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600286static struct msm_pm_sleep_ops msm_lpm_ops = {
287 .lowest_limits = msm_lpm_lowest_limits,
288 .enter_sleep = msm_lpm_enter_sleep,
289 .exit_sleep = msm_lpm_exit_sleep,
290};
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600291
292static int __devinit msm_lpm_levels_probe(struct platform_device *pdev)
293{
294 struct msm_rpmrs_level *levels = NULL;
295 struct msm_rpmrs_level *level = NULL;
296 struct device_node *node = NULL;
297 char *key = NULL;
298 uint32_t val = 0;
299 int ret = 0;
300 uint32_t num_levels = 0;
301 int idx = 0;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800302 unsigned int m_cpu = 0;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600303
304 for_each_child_of_node(pdev->dev.of_node, node)
305 num_levels++;
306
307 levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level),
308 GFP_KERNEL);
309 if (!levels)
310 return -ENOMEM;
311
312 for_each_child_of_node(pdev->dev.of_node, node) {
313 level = &levels[idx++];
314 level->available = false;
315
316 key = "qcom,mode";
317 ret = of_property_read_u32(node, key, &val);
318 if (ret)
319 goto fail;
320 level->sleep_mode = val;
321
322 key = "qcom,xo";
323 ret = of_property_read_u32(node, key, &val);
324 if (ret)
325 goto fail;
326 level->rs_limits.pxo = val;
327
328 key = "qcom,l2";
329 ret = of_property_read_u32(node, key, &val);
330 if (ret)
331 goto fail;
332 level->rs_limits.l2_cache = val;
333
334 key = "qcom,vdd-dig-upper-bound";
335 ret = of_property_read_u32(node, key, &val);
336 if (ret)
337 goto fail;
338 level->rs_limits.vdd_dig_upper_bound = val;
339
340 key = "qcom,vdd-dig-lower-bound";
341 ret = of_property_read_u32(node, key, &val);
342 if (ret)
343 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600344 level->rs_limits.vdd_dig_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600345
346 key = "qcom,vdd-mem-upper-bound";
347 ret = of_property_read_u32(node, key, &val);
348 if (ret)
349 goto fail;
350 level->rs_limits.vdd_mem_upper_bound = val;
351
352 key = "qcom,vdd-mem-lower-bound";
353 ret = of_property_read_u32(node, key, &val);
354 if (ret)
355 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600356 level->rs_limits.vdd_mem_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600357
Mahesh Sivasubramanianb71ce092013-01-08 13:44:23 -0700358 key = "qcom,gpio-detectable";
359 level->rs_limits.gpio_detectable =
360 of_property_read_bool(node, key);
361
362 key = "qcom,irqs-detectable";
363 level->rs_limits.irqs_detectable =
364 of_property_read_bool(node, key);
365
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600366 key = "qcom,latency-us";
367 ret = of_property_read_u32(node, key, &val);
368 if (ret)
369 goto fail;
370 level->latency_us = val;
371
372 key = "qcom,ss-power";
373 ret = of_property_read_u32(node, key, &val);
374 if (ret)
375 goto fail;
376 level->steady_state_power = val;
377
378 key = "qcom,energy-overhead";
379 ret = of_property_read_u32(node, key, &val);
380 if (ret)
381 goto fail;
382 level->energy_overhead = val;
383
384 key = "qcom,time-overhead";
385 ret = of_property_read_u32(node, key, &val);
386 if (ret)
387 goto fail;
388 level->time_overhead_us = val;
389
390 level->available = true;
391 }
392
393 msm_lpm_levels = levels;
394 msm_lpm_level_count = idx;
395
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800396 lpm_test_pdata.msm_lpm_test_levels = msm_lpm_levels;
397 lpm_test_pdata.msm_lpm_test_level_count = msm_lpm_level_count;
Priyanka Mathur32d51ce2013-01-17 10:38:05 -0800398 key = "qcom,use-qtimer";
399 lpm_test_pdata.use_qtimer =
400 of_property_read_bool(pdev->dev.of_node, key);
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800401
402 for_each_possible_cpu(m_cpu)
403 per_cpu(lpm_permitted_level, m_cpu) =
404 msm_lpm_level_count + 1;
405
406 platform_device_register(&msm_lpm_test_device);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600407 msm_pm_set_sleep_ops(&msm_lpm_ops);
408
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600409 return 0;
410fail:
411 pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key);
412 kfree(levels);
413 return -EFAULT;
414}
415
416static struct of_device_id msm_lpm_levels_match_table[] = {
417 {.compatible = "qcom,lpm-levels"},
418 {},
419};
420
421static struct platform_driver msm_lpm_levels_driver = {
422 .probe = msm_lpm_levels_probe,
423 .driver = {
424 .name = "lpm-levels",
425 .owner = THIS_MODULE,
426 .of_match_table = msm_lpm_levels_match_table,
427 },
428};
429
430static int __init msm_lpm_levels_module_init(void)
431{
432 return platform_driver_register(&msm_lpm_levels_driver);
433}
434late_initcall(msm_lpm_levels_module_init);