blob: 61c2aa88c16027307703a5025f7a670f6d7ad5dd [file] [log] [blame]
Mahesh Sivasubramanian9063a292012-11-09 09:15:30 -07001/* Copyright (c) 2012, 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
Girish Mahadevan40abbe12012-04-25 14:58:13 -060040static void msm_lpm_level_update(void)
41{
42 unsigned int lpm_level;
43 struct msm_rpmrs_level *level = NULL;
44
45 for (lpm_level = 0; lpm_level < msm_lpm_level_count; lpm_level++) {
46 level = &msm_lpm_levels[lpm_level];
47 level->available =
48 !msm_lpm_level_beyond_limit(&level->rs_limits);
49 }
50}
51
52int msm_lpm_enter_sleep(uint32_t sclk_count, void *limits,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060053 bool from_idle, bool notify_rpm)
54{
Girish Mahadevan40abbe12012-04-25 14:58:13 -060055 int ret = 0;
Mahesh Sivasubramanianfae923f2012-10-08 16:22:51 -060056 int debug_mask;
Mahesh Sivasubramanian2efbc352012-07-18 14:15:44 -060057 struct msm_rpmrs_limits *l = (struct msm_rpmrs_limits *)limits;
Girish Mahadevan40abbe12012-04-25 14:58:13 -060058
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060059 ret = msm_rpm_enter_sleep();
60 if (ret) {
61 pr_warn("%s(): RPM failed to enter sleep err:%d\n",
62 __func__, ret);
63 goto bail;
64 }
Mahesh Sivasubramanianfae923f2012-10-08 16:22:51 -060065 if (from_idle)
66 debug_mask = msm_lpm_lvl_dbg_msk &
67 MSM_LPM_LVL_DBG_IDLE_LIMITS;
68 else
69 debug_mask = msm_lpm_lvl_dbg_msk &
70 MSM_LPM_LVL_DBG_SUSPEND_LIMITS;
71
72 if (debug_mask)
73 pr_info("%s(): pxo:%d l2:%d mem:0x%x(0x%x) dig:0x%x(0x%x)\n",
74 __func__, l->pxo, l->l2_cache,
75 l->vdd_mem_lower_bound,
76 l->vdd_mem_upper_bound,
77 l->vdd_dig_lower_bound,
78 l->vdd_dig_upper_bound);
79
Mahesh Sivasubramanian2efbc352012-07-18 14:15:44 -060080 ret = msm_lpmrs_enter_sleep(sclk_count, l, from_idle, notify_rpm);
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060081bail:
Girish Mahadevan40abbe12012-04-25 14:58:13 -060082 return ret;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060083}
84
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -060085static void msm_lpm_exit_sleep(void *limits, bool from_idle,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060086 bool notify_rpm, bool collapsed)
87{
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060088 msm_rpm_exit_sleep();
Girish Mahadevana9964a52012-06-29 10:14:09 -060089 msm_lpmrs_exit_sleep((struct msm_rpmrs_limits *)limits,
90 from_idle, notify_rpm, collapsed);
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060091}
92
Girish Mahadevan40abbe12012-04-25 14:58:13 -060093void msm_lpm_show_resources(void)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060094{
95 /* TODO */
96 return;
97}
98
Stephen Boyd3f4bac22012-05-30 10:03:13 -070099s32 msm_cpuidle_get_deep_idle_latency(void)
100{
101 int i;
102 struct msm_rpmrs_level *level = msm_lpm_levels, *best = level;
103
104 if (!level)
105 return 0;
106
107 for (i = 0; i < msm_lpm_level_count; i++, level++) {
108 if (!level->available)
109 continue;
110 if (level->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
111 continue;
112 /* Pick the first power collapse mode by default */
113 if (best->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
114 best = level;
115 /* Find the lowest latency for power collapse */
116 if (level->latency_us < best->latency_us)
117 best = level;
118 }
119 return best->latency_us - 1;
120}
121
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600122static void *msm_lpm_lowest_limits(bool from_idle,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600123 enum msm_pm_sleep_mode sleep_mode,
124 struct msm_pm_time_params *time_param, uint32_t *power)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600125{
126 unsigned int cpu = smp_processor_id();
127 struct msm_rpmrs_level *best_level = NULL;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600128 uint32_t pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600129 int i;
130
131 if (!msm_lpm_levels)
132 return NULL;
133
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600134 msm_lpm_level_update();
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600135
136 for (i = 0; i < msm_lpm_level_count; i++) {
137 struct msm_rpmrs_level *level = &msm_lpm_levels[i];
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600138
139 if (!level->available)
140 continue;
141
142 if (sleep_mode != level->sleep_mode)
143 continue;
144
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600145 if (time_param->latency_us < level->latency_us)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600146 continue;
147
Mahesh Sivasubramanian9063a292012-11-09 09:15:30 -0700148 if ((MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE == sleep_mode)
149 || (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == sleep_mode))
150 if (!cpu && msm_rpm_waiting_for_ack())
151 break;
152
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600153 if (time_param->sleep_us <= 1) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600154 pwr = level->energy_overhead;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600155 } else if (time_param->sleep_us <= level->time_overhead_us) {
156 pwr = level->energy_overhead / time_param->sleep_us;
157 } else if ((time_param->sleep_us >> 10)
158 > level->time_overhead_us) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600159 pwr = level->steady_state_power;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600160 } else {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600161 pwr = level->steady_state_power;
162 pwr -= (level->time_overhead_us *
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600163 level->steady_state_power) /
164 time_param->sleep_us;
165 pwr += level->energy_overhead / time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600166 }
167
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600168 if (!best_level || best_level->rs_limits.power[cpu] >= pwr) {
169
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600170 level->rs_limits.latency_us[cpu] = level->latency_us;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600171 level->rs_limits.power[cpu] = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600172 best_level = level;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600173
174 if (power)
175 *power = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600176 }
177 }
178
179 return best_level ? &best_level->rs_limits : NULL;
180}
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600181static struct msm_pm_sleep_ops msm_lpm_ops = {
182 .lowest_limits = msm_lpm_lowest_limits,
183 .enter_sleep = msm_lpm_enter_sleep,
184 .exit_sleep = msm_lpm_exit_sleep,
185};
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600186
187static int __devinit msm_lpm_levels_probe(struct platform_device *pdev)
188{
189 struct msm_rpmrs_level *levels = NULL;
190 struct msm_rpmrs_level *level = NULL;
191 struct device_node *node = NULL;
192 char *key = NULL;
193 uint32_t val = 0;
194 int ret = 0;
195 uint32_t num_levels = 0;
196 int idx = 0;
197
198 for_each_child_of_node(pdev->dev.of_node, node)
199 num_levels++;
200
201 levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level),
202 GFP_KERNEL);
203 if (!levels)
204 return -ENOMEM;
205
206 for_each_child_of_node(pdev->dev.of_node, node) {
207 level = &levels[idx++];
208 level->available = false;
209
210 key = "qcom,mode";
211 ret = of_property_read_u32(node, key, &val);
212 if (ret)
213 goto fail;
214 level->sleep_mode = val;
215
216 key = "qcom,xo";
217 ret = of_property_read_u32(node, key, &val);
218 if (ret)
219 goto fail;
220 level->rs_limits.pxo = val;
221
222 key = "qcom,l2";
223 ret = of_property_read_u32(node, key, &val);
224 if (ret)
225 goto fail;
226 level->rs_limits.l2_cache = val;
227
228 key = "qcom,vdd-dig-upper-bound";
229 ret = of_property_read_u32(node, key, &val);
230 if (ret)
231 goto fail;
232 level->rs_limits.vdd_dig_upper_bound = val;
233
234 key = "qcom,vdd-dig-lower-bound";
235 ret = of_property_read_u32(node, key, &val);
236 if (ret)
237 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600238 level->rs_limits.vdd_dig_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600239
240 key = "qcom,vdd-mem-upper-bound";
241 ret = of_property_read_u32(node, key, &val);
242 if (ret)
243 goto fail;
244 level->rs_limits.vdd_mem_upper_bound = val;
245
246 key = "qcom,vdd-mem-lower-bound";
247 ret = of_property_read_u32(node, key, &val);
248 if (ret)
249 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600250 level->rs_limits.vdd_mem_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600251
252 key = "qcom,latency-us";
253 ret = of_property_read_u32(node, key, &val);
254 if (ret)
255 goto fail;
256 level->latency_us = val;
257
258 key = "qcom,ss-power";
259 ret = of_property_read_u32(node, key, &val);
260 if (ret)
261 goto fail;
262 level->steady_state_power = val;
263
264 key = "qcom,energy-overhead";
265 ret = of_property_read_u32(node, key, &val);
266 if (ret)
267 goto fail;
268 level->energy_overhead = val;
269
270 key = "qcom,time-overhead";
271 ret = of_property_read_u32(node, key, &val);
272 if (ret)
273 goto fail;
274 level->time_overhead_us = val;
275
276 level->available = true;
277 }
278
279 msm_lpm_levels = levels;
280 msm_lpm_level_count = idx;
281
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600282 msm_pm_set_sleep_ops(&msm_lpm_ops);
283
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600284 return 0;
285fail:
286 pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key);
287 kfree(levels);
288 return -EFAULT;
289}
290
291static struct of_device_id msm_lpm_levels_match_table[] = {
292 {.compatible = "qcom,lpm-levels"},
293 {},
294};
295
296static struct platform_driver msm_lpm_levels_driver = {
297 .probe = msm_lpm_levels_probe,
298 .driver = {
299 .name = "lpm-levels",
300 .owner = THIS_MODULE,
301 .of_match_table = msm_lpm_levels_match_table,
302 },
303};
304
305static int __init msm_lpm_levels_module_init(void)
306{
307 return platform_driver_register(&msm_lpm_levels_driver);
308}
309late_initcall(msm_lpm_levels_module_init);