blob: 8571bda060c8dee6f3aef34d75cb3108fb2981ac [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;
Priyanka Mathur3abfd442013-01-11 12:58:51 -080060 struct msm_lpm_sleep_data sleep_data;
61
62 sleep_data.limits = limits;
63 sleep_data.kernel_sleep = __get_cpu_var(msm_lpm_sleep_time);
64 atomic_notifier_call_chain(&__get_cpu_var(lpm_notify_head),
65 MSM_LPM_STATE_ENTER, &sleep_data);
Girish Mahadevan40abbe12012-04-25 14:58:13 -060066
Priyanka Mathurab1479f2013-01-10 17:03:04 -080067 if (notify_rpm) {
68 int debug_mask;
69 struct msm_rpmrs_limits *l = (struct msm_rpmrs_limits *)limits;
70
71 ret = msm_rpm_enter_sleep();
72 if (ret) {
73 pr_warn("%s(): RPM failed to enter sleep err:%d\n",
74 __func__, ret);
75 goto bail;
76 }
77 if (from_idle)
78 debug_mask = msm_lpm_lvl_dbg_msk &
79 MSM_LPM_LVL_DBG_IDLE_LIMITS;
80 else
81 debug_mask = msm_lpm_lvl_dbg_msk &
82 MSM_LPM_LVL_DBG_SUSPEND_LIMITS;
83
84 if (debug_mask)
85 pr_info("%s(): pxo:%d l2:%d mem:0x%x(0x%x) dig:0x%x(0x%x)\n",
86 __func__, l->pxo, l->l2_cache,
87 l->vdd_mem_lower_bound,
88 l->vdd_mem_upper_bound,
89 l->vdd_dig_lower_bound,
90 l->vdd_dig_upper_bound);
91
92 ret = msm_lpmrs_enter_sleep(sclk_count, l, from_idle,
93 notify_rpm);
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060094 }
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060095bail:
Girish Mahadevan40abbe12012-04-25 14:58:13 -060096 return ret;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060097}
98
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -060099static void msm_lpm_exit_sleep(void *limits, bool from_idle,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600100 bool notify_rpm, bool collapsed)
101{
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -0600102 msm_rpm_exit_sleep();
Girish Mahadevana9964a52012-06-29 10:14:09 -0600103 msm_lpmrs_exit_sleep((struct msm_rpmrs_limits *)limits,
104 from_idle, notify_rpm, collapsed);
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800105 atomic_notifier_call_chain(&__get_cpu_var(lpm_notify_head),
106 MSM_LPM_STATE_EXIT, NULL);
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600107}
108
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600109void msm_lpm_show_resources(void)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600110{
111 /* TODO */
112 return;
113}
114
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800115uint32_t msm_pm_get_pxo(struct msm_rpmrs_limits *limits)
116{
117 return limits->pxo;
118}
119
120uint32_t msm_pm_get_l2_cache(struct msm_rpmrs_limits *limits)
121{
122 return limits->l2_cache;
123}
124
125uint32_t msm_pm_get_vdd_mem(struct msm_rpmrs_limits *limits)
126{
127 return limits->vdd_mem_upper_bound;
128}
129
130uint32_t msm_pm_get_vdd_dig(struct msm_rpmrs_limits *limits)
131{
132 return limits->vdd_dig_upper_bound;
133}
134
135static bool lpm_level_permitted(int cur_level_count)
136{
137 if (__get_cpu_var(lpm_permitted_level) == msm_lpm_level_count + 1)
138 return true;
139 return (__get_cpu_var(lpm_permitted_level) == cur_level_count);
140}
141
142int msm_lpm_register_notifier(int cpu, int level_iter,
143 struct notifier_block *nb, bool is_latency_measure)
144{
145 per_cpu(lpm_permitted_level, cpu) = level_iter;
146 return atomic_notifier_chain_register(&per_cpu(lpm_notify_head,
147 cpu), nb);
148}
149
150int msm_lpm_unregister_notifier(int cpu, struct notifier_block *nb)
151{
152 per_cpu(lpm_permitted_level, cpu) = msm_lpm_level_count + 1;
153 return atomic_notifier_chain_unregister(&per_cpu(lpm_notify_head, cpu),
154 nb);
155}
156
Stephen Boyd3f4bac22012-05-30 10:03:13 -0700157s32 msm_cpuidle_get_deep_idle_latency(void)
158{
159 int i;
160 struct msm_rpmrs_level *level = msm_lpm_levels, *best = level;
161
162 if (!level)
163 return 0;
164
165 for (i = 0; i < msm_lpm_level_count; i++, level++) {
166 if (!level->available)
167 continue;
168 if (level->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
169 continue;
170 /* Pick the first power collapse mode by default */
171 if (best->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
172 best = level;
173 /* Find the lowest latency for power collapse */
174 if (level->latency_us < best->latency_us)
175 best = level;
176 }
177 return best->latency_us - 1;
178}
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700179static bool msm_lpm_irqs_detectable(struct msm_rpmrs_limits *limits,
180 bool irqs_detectable, bool gpio_detectable)
181{
182 if (!limits->irqs_detectable)
183 return irqs_detectable;
184
185 if (!limits->gpio_detectable)
186 return gpio_detectable;
187
188 return true;
189
190}
Stephen Boyd3f4bac22012-05-30 10:03:13 -0700191
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600192static void *msm_lpm_lowest_limits(bool from_idle,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600193 enum msm_pm_sleep_mode sleep_mode,
194 struct msm_pm_time_params *time_param, uint32_t *power)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600195{
196 unsigned int cpu = smp_processor_id();
197 struct msm_rpmrs_level *best_level = NULL;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600198 uint32_t pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600199 int i;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800200 int best_level_iter = msm_lpm_level_count + 1;
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700201 bool irqs_detect = false;
202 bool gpio_detect = false;
203
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600204 if (!msm_lpm_levels)
205 return NULL;
206
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600207 msm_lpm_level_update();
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600208
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700209 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
210 irqs_detect = msm_mpm_irqs_detectable(from_idle);
211 gpio_detect = msm_mpm_gpio_irqs_detectable(from_idle);
212 }
213
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600214 for (i = 0; i < msm_lpm_level_count; i++) {
215 struct msm_rpmrs_level *level = &msm_lpm_levels[i];
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600216
217 if (!level->available)
218 continue;
219
220 if (sleep_mode != level->sleep_mode)
221 continue;
222
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600223 if (time_param->latency_us < level->latency_us)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600224 continue;
225
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700226 if ((sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) &&
227 !msm_lpm_irqs_detectable(&level->rs_limits,
228 irqs_detect, gpio_detect))
229 continue;
230
Mahesh Sivasubramanian9063a292012-11-09 09:15:30 -0700231 if ((MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE == sleep_mode)
232 || (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == sleep_mode))
233 if (!cpu && msm_rpm_waiting_for_ack())
234 break;
235
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600236 if (time_param->sleep_us <= 1) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600237 pwr = level->energy_overhead;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600238 } else if (time_param->sleep_us <= level->time_overhead_us) {
239 pwr = level->energy_overhead / time_param->sleep_us;
240 } else if ((time_param->sleep_us >> 10)
241 > level->time_overhead_us) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600242 pwr = level->steady_state_power;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600243 } else {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600244 pwr = level->steady_state_power;
245 pwr -= (level->time_overhead_us *
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600246 level->steady_state_power) /
247 time_param->sleep_us;
248 pwr += level->energy_overhead / time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600249 }
250
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600251 if (!best_level || best_level->rs_limits.power[cpu] >= pwr) {
252
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600253 level->rs_limits.latency_us[cpu] = level->latency_us;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600254 level->rs_limits.power[cpu] = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600255 best_level = level;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800256 best_level_iter = i;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600257 if (power)
258 *power = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600259 }
260 }
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800261 if (best_level && !lpm_level_permitted(best_level_iter))
262 best_level = NULL;
263 else
264 per_cpu(msm_lpm_sleep_time, cpu) =
265 time_param->modified_time_us ?
266 time_param->modified_time_us : time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600267
268 return best_level ? &best_level->rs_limits : NULL;
269}
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800270
271static struct lpm_test_platform_data lpm_test_pdata;
272
273static struct platform_device msm_lpm_test_device = {
274 .name = "lpm_test",
275 .id = -1,
276 .dev = {
277 .platform_data = &lpm_test_pdata,
278 },
279};
280
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600281static struct msm_pm_sleep_ops msm_lpm_ops = {
282 .lowest_limits = msm_lpm_lowest_limits,
283 .enter_sleep = msm_lpm_enter_sleep,
284 .exit_sleep = msm_lpm_exit_sleep,
285};
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600286
287static int __devinit msm_lpm_levels_probe(struct platform_device *pdev)
288{
289 struct msm_rpmrs_level *levels = NULL;
290 struct msm_rpmrs_level *level = NULL;
291 struct device_node *node = NULL;
292 char *key = NULL;
293 uint32_t val = 0;
294 int ret = 0;
295 uint32_t num_levels = 0;
296 int idx = 0;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800297 unsigned int m_cpu = 0;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600298
299 for_each_child_of_node(pdev->dev.of_node, node)
300 num_levels++;
301
302 levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level),
303 GFP_KERNEL);
304 if (!levels)
305 return -ENOMEM;
306
307 for_each_child_of_node(pdev->dev.of_node, node) {
308 level = &levels[idx++];
309 level->available = false;
310
311 key = "qcom,mode";
312 ret = of_property_read_u32(node, key, &val);
313 if (ret)
314 goto fail;
315 level->sleep_mode = val;
316
317 key = "qcom,xo";
318 ret = of_property_read_u32(node, key, &val);
319 if (ret)
320 goto fail;
321 level->rs_limits.pxo = val;
322
323 key = "qcom,l2";
324 ret = of_property_read_u32(node, key, &val);
325 if (ret)
326 goto fail;
327 level->rs_limits.l2_cache = val;
328
329 key = "qcom,vdd-dig-upper-bound";
330 ret = of_property_read_u32(node, key, &val);
331 if (ret)
332 goto fail;
333 level->rs_limits.vdd_dig_upper_bound = val;
334
335 key = "qcom,vdd-dig-lower-bound";
336 ret = of_property_read_u32(node, key, &val);
337 if (ret)
338 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600339 level->rs_limits.vdd_dig_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600340
341 key = "qcom,vdd-mem-upper-bound";
342 ret = of_property_read_u32(node, key, &val);
343 if (ret)
344 goto fail;
345 level->rs_limits.vdd_mem_upper_bound = val;
346
347 key = "qcom,vdd-mem-lower-bound";
348 ret = of_property_read_u32(node, key, &val);
349 if (ret)
350 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600351 level->rs_limits.vdd_mem_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600352
Mahesh Sivasubramanianb71ce092013-01-08 13:44:23 -0700353 key = "qcom,gpio-detectable";
354 level->rs_limits.gpio_detectable =
355 of_property_read_bool(node, key);
356
357 key = "qcom,irqs-detectable";
358 level->rs_limits.irqs_detectable =
359 of_property_read_bool(node, key);
360
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600361 key = "qcom,latency-us";
362 ret = of_property_read_u32(node, key, &val);
363 if (ret)
364 goto fail;
365 level->latency_us = val;
366
367 key = "qcom,ss-power";
368 ret = of_property_read_u32(node, key, &val);
369 if (ret)
370 goto fail;
371 level->steady_state_power = val;
372
373 key = "qcom,energy-overhead";
374 ret = of_property_read_u32(node, key, &val);
375 if (ret)
376 goto fail;
377 level->energy_overhead = val;
378
379 key = "qcom,time-overhead";
380 ret = of_property_read_u32(node, key, &val);
381 if (ret)
382 goto fail;
383 level->time_overhead_us = val;
384
385 level->available = true;
386 }
387
388 msm_lpm_levels = levels;
389 msm_lpm_level_count = idx;
390
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800391 lpm_test_pdata.msm_lpm_test_levels = msm_lpm_levels;
392 lpm_test_pdata.msm_lpm_test_level_count = msm_lpm_level_count;
Priyanka Mathur32d51ce2013-01-17 10:38:05 -0800393 key = "qcom,use-qtimer";
394 lpm_test_pdata.use_qtimer =
395 of_property_read_bool(pdev->dev.of_node, key);
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800396
397 for_each_possible_cpu(m_cpu)
398 per_cpu(lpm_permitted_level, m_cpu) =
399 msm_lpm_level_count + 1;
400
401 platform_device_register(&msm_lpm_test_device);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600402 msm_pm_set_sleep_ops(&msm_lpm_ops);
403
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600404 return 0;
405fail:
406 pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key);
407 kfree(levels);
408 return -EFAULT;
409}
410
411static struct of_device_id msm_lpm_levels_match_table[] = {
412 {.compatible = "qcom,lpm-levels"},
413 {},
414};
415
416static struct platform_driver msm_lpm_levels_driver = {
417 .probe = msm_lpm_levels_probe,
418 .driver = {
419 .name = "lpm-levels",
420 .owner = THIS_MODULE,
421 .of_match_table = msm_lpm_levels_match_table,
422 },
423};
424
425static int __init msm_lpm_levels_module_init(void)
426{
427 return platform_driver_register(&msm_lpm_levels_driver);
428}
429late_initcall(msm_lpm_levels_module_init);