blob: 8218a426a83c29872567a4f350135f80ee9f0996 [file] [log] [blame]
Praveen Chidambaram85b7b282012-04-16 13:45:15 -06001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
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
25static struct msm_rpmrs_level *msm_lpm_levels;
26static int msm_lpm_level_count;
27
Girish Mahadevan40abbe12012-04-25 14:58:13 -060028static void msm_lpm_level_update(void)
29{
30 unsigned int lpm_level;
31 struct msm_rpmrs_level *level = NULL;
32
33 for (lpm_level = 0; lpm_level < msm_lpm_level_count; lpm_level++) {
34 level = &msm_lpm_levels[lpm_level];
35 level->available =
36 !msm_lpm_level_beyond_limit(&level->rs_limits);
37 }
38}
39
40int msm_lpm_enter_sleep(uint32_t sclk_count, void *limits,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060041 bool from_idle, bool notify_rpm)
42{
Girish Mahadevan40abbe12012-04-25 14:58:13 -060043 int ret = 0;
Mahesh Sivasubramanian2efbc352012-07-18 14:15:44 -060044 struct msm_rpmrs_limits *l = (struct msm_rpmrs_limits *)limits;
Girish Mahadevan40abbe12012-04-25 14:58:13 -060045
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060046 ret = msm_rpm_enter_sleep();
47 if (ret) {
48 pr_warn("%s(): RPM failed to enter sleep err:%d\n",
49 __func__, ret);
50 goto bail;
51 }
Mahesh Sivasubramanian2efbc352012-07-18 14:15:44 -060052 ret = msm_lpmrs_enter_sleep(sclk_count, l, from_idle, notify_rpm);
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060053bail:
Girish Mahadevan40abbe12012-04-25 14:58:13 -060054 return ret;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060055}
56
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -060057static void msm_lpm_exit_sleep(void *limits, bool from_idle,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060058 bool notify_rpm, bool collapsed)
59{
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060060 msm_rpm_exit_sleep();
Girish Mahadevana9964a52012-06-29 10:14:09 -060061 msm_lpmrs_exit_sleep((struct msm_rpmrs_limits *)limits,
62 from_idle, notify_rpm, collapsed);
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060063}
64
Girish Mahadevan40abbe12012-04-25 14:58:13 -060065void msm_lpm_show_resources(void)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060066{
67 /* TODO */
68 return;
69}
70
Stephen Boyd3f4bac22012-05-30 10:03:13 -070071s32 msm_cpuidle_get_deep_idle_latency(void)
72{
73 int i;
74 struct msm_rpmrs_level *level = msm_lpm_levels, *best = level;
75
76 if (!level)
77 return 0;
78
79 for (i = 0; i < msm_lpm_level_count; i++, level++) {
80 if (!level->available)
81 continue;
82 if (level->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
83 continue;
84 /* Pick the first power collapse mode by default */
85 if (best->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
86 best = level;
87 /* Find the lowest latency for power collapse */
88 if (level->latency_us < best->latency_us)
89 best = level;
90 }
91 return best->latency_us - 1;
92}
93
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -060094static void *msm_lpm_lowest_limits(bool from_idle,
Girish Mahadevandc318fd2012-08-17 16:48:05 -060095 enum msm_pm_sleep_mode sleep_mode,
96 struct msm_pm_time_params *time_param, uint32_t *power)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060097{
98 unsigned int cpu = smp_processor_id();
99 struct msm_rpmrs_level *best_level = NULL;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600100 uint32_t pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600101 int i;
102
103 if (!msm_lpm_levels)
104 return NULL;
105
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600106 msm_lpm_level_update();
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600107
108 for (i = 0; i < msm_lpm_level_count; i++) {
109 struct msm_rpmrs_level *level = &msm_lpm_levels[i];
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600110
111 if (!level->available)
112 continue;
113
114 if (sleep_mode != level->sleep_mode)
115 continue;
116
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600117 if (time_param->latency_us < level->latency_us)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600118 continue;
119
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600120 if (time_param->sleep_us <= 1) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600121 pwr = level->energy_overhead;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600122 } else if (time_param->sleep_us <= level->time_overhead_us) {
123 pwr = level->energy_overhead / time_param->sleep_us;
124 } else if ((time_param->sleep_us >> 10)
125 > level->time_overhead_us) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600126 pwr = level->steady_state_power;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600127 } else {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600128 pwr = level->steady_state_power;
129 pwr -= (level->time_overhead_us *
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600130 level->steady_state_power) /
131 time_param->sleep_us;
132 pwr += level->energy_overhead / time_param->sleep_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600133 }
134
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600135 if (!best_level || best_level->rs_limits.power[cpu] >= pwr) {
136
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600137 level->rs_limits.latency_us[cpu] = level->latency_us;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600138 level->rs_limits.power[cpu] = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600139 best_level = level;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600140
141 if (power)
142 *power = pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600143 }
144 }
145
146 return best_level ? &best_level->rs_limits : NULL;
147}
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600148static struct msm_pm_sleep_ops msm_lpm_ops = {
149 .lowest_limits = msm_lpm_lowest_limits,
150 .enter_sleep = msm_lpm_enter_sleep,
151 .exit_sleep = msm_lpm_exit_sleep,
152};
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600153
154static int __devinit msm_lpm_levels_probe(struct platform_device *pdev)
155{
156 struct msm_rpmrs_level *levels = NULL;
157 struct msm_rpmrs_level *level = NULL;
158 struct device_node *node = NULL;
159 char *key = NULL;
160 uint32_t val = 0;
161 int ret = 0;
162 uint32_t num_levels = 0;
163 int idx = 0;
164
165 for_each_child_of_node(pdev->dev.of_node, node)
166 num_levels++;
167
168 levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level),
169 GFP_KERNEL);
170 if (!levels)
171 return -ENOMEM;
172
173 for_each_child_of_node(pdev->dev.of_node, node) {
174 level = &levels[idx++];
175 level->available = false;
176
177 key = "qcom,mode";
178 ret = of_property_read_u32(node, key, &val);
179 if (ret)
180 goto fail;
181 level->sleep_mode = val;
182
183 key = "qcom,xo";
184 ret = of_property_read_u32(node, key, &val);
185 if (ret)
186 goto fail;
187 level->rs_limits.pxo = val;
188
189 key = "qcom,l2";
190 ret = of_property_read_u32(node, key, &val);
191 if (ret)
192 goto fail;
193 level->rs_limits.l2_cache = val;
194
195 key = "qcom,vdd-dig-upper-bound";
196 ret = of_property_read_u32(node, key, &val);
197 if (ret)
198 goto fail;
199 level->rs_limits.vdd_dig_upper_bound = val;
200
201 key = "qcom,vdd-dig-lower-bound";
202 ret = of_property_read_u32(node, key, &val);
203 if (ret)
204 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600205 level->rs_limits.vdd_dig_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600206
207 key = "qcom,vdd-mem-upper-bound";
208 ret = of_property_read_u32(node, key, &val);
209 if (ret)
210 goto fail;
211 level->rs_limits.vdd_mem_upper_bound = val;
212
213 key = "qcom,vdd-mem-lower-bound";
214 ret = of_property_read_u32(node, key, &val);
215 if (ret)
216 goto fail;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600217 level->rs_limits.vdd_mem_lower_bound = val;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600218
219 key = "qcom,latency-us";
220 ret = of_property_read_u32(node, key, &val);
221 if (ret)
222 goto fail;
223 level->latency_us = val;
224
225 key = "qcom,ss-power";
226 ret = of_property_read_u32(node, key, &val);
227 if (ret)
228 goto fail;
229 level->steady_state_power = val;
230
231 key = "qcom,energy-overhead";
232 ret = of_property_read_u32(node, key, &val);
233 if (ret)
234 goto fail;
235 level->energy_overhead = val;
236
237 key = "qcom,time-overhead";
238 ret = of_property_read_u32(node, key, &val);
239 if (ret)
240 goto fail;
241 level->time_overhead_us = val;
242
243 level->available = true;
244 }
245
246 msm_lpm_levels = levels;
247 msm_lpm_level_count = idx;
248
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600249 msm_pm_set_sleep_ops(&msm_lpm_ops);
250
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600251 return 0;
252fail:
253 pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key);
254 kfree(levels);
255 return -EFAULT;
256}
257
258static struct of_device_id msm_lpm_levels_match_table[] = {
259 {.compatible = "qcom,lpm-levels"},
260 {},
261};
262
263static struct platform_driver msm_lpm_levels_driver = {
264 .probe = msm_lpm_levels_probe,
265 .driver = {
266 .name = "lpm-levels",
267 .owner = THIS_MODULE,
268 .of_match_table = msm_lpm_levels_match_table,
269 },
270};
271
272static int __init msm_lpm_levels_module_init(void)
273{
274 return platform_driver_register(&msm_lpm_levels_driver);
275}
276late_initcall(msm_lpm_levels_module_init);