blob: 249a334ffd2b8d7a81b2a5a5e85e339815d0df46 [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>
Girish Mahadevanc45b4c72013-04-24 14:07:11 -060019#include <linux/mutex.h>
20#include <linux/cpu.h>
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060021#include <linux/of.h>
22#include <mach/mpm.h>
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -060023#include "pm.h"
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -060024#include "rpm-notifier.h"
Girish Mahadevanc45b4c72013-04-24 14:07:11 -060025#include "spm.h"
26#include "idle.h"
Mahesh Sivasubramanianfae923f2012-10-08 16:22:51 -060027
28enum {
29 MSM_LPM_LVL_DBG_SUSPEND_LIMITS = BIT(0),
30 MSM_LPM_LVL_DBG_IDLE_LIMITS = BIT(1),
31};
32
Girish Mahadevanc45b4c72013-04-24 14:07:11 -060033enum {
34 MSM_SCM_L2_ON = 0,
35 MSM_SCM_L2_OFF = 1,
36 MSM_SCM_L2_GDHS = 3,
37};
38
39struct msm_rpmrs_level {
40 enum msm_pm_sleep_mode sleep_mode;
41 uint32_t l2_cache;
42 bool available;
43 uint32_t latency_us;
44 uint32_t steady_state_power;
45 uint32_t energy_overhead;
46 uint32_t time_overhead_us;
47};
48
49struct lpm_lookup_table {
50 uint32_t modes;
51 const char *mode_name;
52};
53
54static void msm_lpm_level_update(void);
55
56static int msm_lpm_cpu_callback(struct notifier_block *cpu_nb,
57 unsigned long action, void *hcpu);
58
59static struct notifier_block __refdata msm_lpm_cpu_nblk = {
60 .notifier_call = msm_lpm_cpu_callback,
61};
62
63static uint32_t allowed_l2_mode;
64static uint32_t sysfs_dbg_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE;
65static uint32_t default_l2_mode;
66
67static bool no_l2_saw;
68
69static ssize_t msm_lpm_levels_attr_show(
70 struct kobject *kobj, struct kobj_attribute *attr, char *buf);
71static ssize_t msm_lpm_levels_attr_store(struct kobject *kobj,
72 struct kobj_attribute *attr, const char *buf, size_t count);
Archana Sathyakumare6a35102013-01-31 16:18:49 -070073
Girish Mahadevane6b1dd42013-05-22 11:59:08 -060074#define ADJUST_LATENCY(x) \
75 ((x == MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE) ?\
76 (num_online_cpus()) / 2 : 0)
77
Mahesh Sivasubramanianfae923f2012-10-08 16:22:51 -060078static int msm_lpm_lvl_dbg_msk;
79
80module_param_named(
81 debug_mask, msm_lpm_lvl_dbg_msk, int, S_IRUGO | S_IWUSR | S_IWGRP
82);
83
Praveen Chidambaram85b7b282012-04-16 13:45:15 -060084static struct msm_rpmrs_level *msm_lpm_levels;
85static int msm_lpm_level_count;
86
Girish Mahadevanc45b4c72013-04-24 14:07:11 -060087static struct kobj_attribute lpm_l2_kattr = __ATTR(l2, S_IRUGO|S_IWUSR,\
88 msm_lpm_levels_attr_show, msm_lpm_levels_attr_store);
89
90static struct attribute *lpm_levels_attr[] = {
91 &lpm_l2_kattr.attr,
92 NULL,
93};
94
95static struct attribute_group lpm_levels_attr_grp = {
96 .attrs = lpm_levels_attr,
97};
98
99/* SYSFS */
100static ssize_t msm_lpm_levels_attr_show(
101 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
102{
103 struct kernel_param kp;
104 int rc;
105
106 kp.arg = &sysfs_dbg_l2_mode;
107
108 rc = param_get_uint(buf, &kp);
109
110 if (rc > 0) {
111 strlcat(buf, "\n", PAGE_SIZE);
112 rc++;
113 }
114
115 return rc;
116}
117
118static ssize_t msm_lpm_levels_attr_store(struct kobject *kobj,
119 struct kobj_attribute *attr, const char *buf, size_t count)
120{
121 struct kernel_param kp;
122 unsigned int temp;
123 int rc;
124
125 kp.arg = &temp;
126 rc = param_set_uint(buf, &kp);
127 if (rc)
128 return rc;
129
130 sysfs_dbg_l2_mode = temp;
131 msm_lpm_level_update();
132
133 return count;
134}
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800135
Archana Sathyakumare6a35102013-01-31 16:18:49 -0700136static int msm_pm_get_sleep_mode_value(struct device_node *node,
137 const char *key, uint32_t *sleep_mode_val)
138{
139 int i;
140 struct lpm_lookup_table {
141 uint32_t modes;
142 const char *mode_name;
143 };
144 struct lpm_lookup_table pm_sm_lookup[] = {
145 {MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT,
146 "wfi"},
147 {MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT,
148 "ramp_down_and_wfi"},
149 {MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE,
150 "standalone_pc"},
151 {MSM_PM_SLEEP_MODE_POWER_COLLAPSE,
152 "pc"},
153 {MSM_PM_SLEEP_MODE_RETENTION,
154 "retention"},
155 {MSM_PM_SLEEP_MODE_POWER_COLLAPSE_SUSPEND,
156 "pc_suspend"},
157 {MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN,
158 "pc_no_xo_shutdown"}
159 };
160 int ret;
161 const char *mode_name;
162
163 ret = of_property_read_string(node, key, &mode_name);
164 if (!ret) {
165 ret = -EINVAL;
166 for (i = 0; i < ARRAY_SIZE(pm_sm_lookup); i++) {
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600167 if (!strcmp(mode_name, pm_sm_lookup[i].mode_name)) {
Archana Sathyakumare6a35102013-01-31 16:18:49 -0700168 *sleep_mode_val = pm_sm_lookup[i].modes;
169 ret = 0;
170 break;
171 }
172 }
173 }
174 return ret;
175}
176
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600177static int msm_lpm_set_l2_mode(int sleep_mode)
178{
179 int lpm = sleep_mode;
180 int rc = 0;
181
182 if (no_l2_saw)
183 goto bail_set_l2_mode;
184
185 msm_pm_set_l2_flush_flag(MSM_SCM_L2_ON);
186
187 switch (sleep_mode) {
188 case MSM_SPM_L2_MODE_POWER_COLLAPSE:
189 msm_pm_set_l2_flush_flag(MSM_SCM_L2_OFF);
190 break;
191 case MSM_SPM_L2_MODE_GDHS:
192 msm_pm_set_l2_flush_flag(MSM_SCM_L2_GDHS);
193 break;
Murali Nalajala82123b02013-10-04 16:00:19 +0530194 case MSM_SPM_L2_MODE_PC_NO_RPM:
195 msm_pm_set_l2_flush_flag(MSM_SCM_L2_OFF);
196 break;
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600197 case MSM_SPM_L2_MODE_RETENTION:
198 case MSM_SPM_L2_MODE_DISABLED:
199 break;
200 default:
201 lpm = MSM_SPM_L2_MODE_DISABLED;
202 break;
203 }
204
205 rc = msm_spm_l2_set_low_power_mode(lpm, true);
206
207 if (rc) {
208 if (rc == -ENXIO)
209 WARN_ON_ONCE(1);
210 else
211 pr_err("%s: Failed to set L2 low power mode %d, ERR %d",
212 __func__, lpm, rc);
213 }
214
215bail_set_l2_mode:
216 return rc;
217}
218
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600219static void msm_lpm_level_update(void)
220{
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600221 int lpm_level;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600222 struct msm_rpmrs_level *level = NULL;
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600223 uint32_t max_l2_mode;
224 static DEFINE_MUTEX(lpm_lock);
225
226 mutex_lock(&lpm_lock);
227
228 max_l2_mode = min(allowed_l2_mode, sysfs_dbg_l2_mode);
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600229
230 for (lpm_level = 0; lpm_level < msm_lpm_level_count; lpm_level++) {
231 level = &msm_lpm_levels[lpm_level];
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600232 level->available = !(level->l2_cache > max_l2_mode);
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600233 }
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600234 mutex_unlock(&lpm_lock);
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600235}
236
237int msm_lpm_enter_sleep(uint32_t sclk_count, void *limits,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600238 bool from_idle, bool notify_rpm)
239{
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600240 int ret = 0;
Mahesh Sivasubramanian9b0f4122013-02-15 14:20:52 -0700241 int debug_mask;
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600242 uint32_t l2 = *(uint32_t *)limits;
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600243
Mahesh Sivasubramanian9b0f4122013-02-15 14:20:52 -0700244 if (from_idle)
245 debug_mask = msm_lpm_lvl_dbg_msk &
Priyanka Mathur86cfc762013-01-10 17:03:04 -0800246 MSM_LPM_LVL_DBG_IDLE_LIMITS;
Mahesh Sivasubramanian9b0f4122013-02-15 14:20:52 -0700247 else
248 debug_mask = msm_lpm_lvl_dbg_msk &
Priyanka Mathur86cfc762013-01-10 17:03:04 -0800249 MSM_LPM_LVL_DBG_SUSPEND_LIMITS;
Mahesh Sivasubramanian9b0f4122013-02-15 14:20:52 -0700250
251 if (debug_mask)
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600252 pr_info("%s(): l2:%d", __func__, l2);
Mahesh Sivasubramanian9b0f4122013-02-15 14:20:52 -0700253
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600254 ret = msm_lpm_set_l2_mode(l2);
255
Mahesh Sivasubramanian0558d4b2012-10-12 18:05:28 -0600256 if (ret) {
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600257 if (ret == -ENXIO)
258 ret = 0;
259 else {
260 pr_warn("%s(): Failed to set L2 SPM Mode %d",
261 __func__, l2);
262 goto bail;
263 }
Mahesh Sivasubramanian0558d4b2012-10-12 18:05:28 -0600264 }
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600265
Priyanka Mathur86cfc762013-01-10 17:03:04 -0800266 if (notify_rpm) {
267 ret = msm_rpm_enter_sleep(debug_mask);
268 if (ret) {
269 pr_warn("%s(): RPM failed to enter sleep err:%d\n",
270 __func__, ret);
271 goto bail;
272 }
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600273
274 msm_mpm_enter_sleep(sclk_count, from_idle);
Priyanka Mathur86cfc762013-01-10 17:03:04 -0800275 }
Mahesh Sivasubramanian11dad772012-07-13 14:00:01 -0600276bail:
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600277 return ret;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600278}
279
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600280static void msm_lpm_exit_sleep(void *limits, bool from_idle,
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600281 bool notify_rpm, bool collapsed)
282{
Mahesh Sivasubramanian0558d4b2012-10-12 18:05:28 -0600283
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600284 msm_lpm_set_l2_mode(default_l2_mode);
285
286 if (notify_rpm) {
287 msm_mpm_exit_sleep(from_idle);
Priyanka Mathur86cfc762013-01-10 17:03:04 -0800288 msm_rpm_exit_sleep();
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600289 }
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600290}
291
Girish Mahadevan40abbe12012-04-25 14:58:13 -0600292void msm_lpm_show_resources(void)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600293{
294 /* TODO */
295 return;
296}
297
Stephen Boyd3f4bac22012-05-30 10:03:13 -0700298s32 msm_cpuidle_get_deep_idle_latency(void)
299{
300 int i;
301 struct msm_rpmrs_level *level = msm_lpm_levels, *best = level;
302
303 if (!level)
304 return 0;
305
306 for (i = 0; i < msm_lpm_level_count; i++, level++) {
307 if (!level->available)
308 continue;
309 if (level->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
310 continue;
311 /* Pick the first power collapse mode by default */
312 if (best->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
313 best = level;
314 /* Find the lowest latency for power collapse */
315 if (level->latency_us < best->latency_us)
316 best = level;
317 }
318 return best->latency_us - 1;
319}
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600320
321static int msm_lpm_cpu_callback(struct notifier_block *cpu_nb,
322 unsigned long action, void *hcpu)
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700323{
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600324 switch (action) {
325 case CPU_UP_PREPARE:
326 case CPU_UP_PREPARE_FROZEN:
327 allowed_l2_mode = default_l2_mode;
328 msm_lpm_level_update();
329 break;
330 case CPU_DEAD_FROZEN:
331 case CPU_DEAD:
332 case CPU_UP_CANCELED:
333 case CPU_UP_CANCELED_FROZEN:
334 if (num_online_cpus() == 1)
335 allowed_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE;
336 msm_lpm_level_update();
337 break;
338 }
339 return NOTIFY_OK;
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700340}
Stephen Boyd3f4bac22012-05-30 10:03:13 -0700341
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600342static void *msm_lpm_lowest_limits(bool from_idle,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600343 enum msm_pm_sleep_mode sleep_mode,
344 struct msm_pm_time_params *time_param, uint32_t *power)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600345{
346 unsigned int cpu = smp_processor_id();
347 struct msm_rpmrs_level *best_level = NULL;
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600348 uint32_t best_level_pwr = 0;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600349 uint32_t pwr;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600350 int i;
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700351 bool modify_event_timer;
352 uint32_t next_wakeup_us = time_param->sleep_us;
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600353 uint32_t lvl_latency_us = 0;
354 uint32_t lvl_overhead_us = 0;
355 uint32_t lvl_overhead_energy = 0;
Mahesh Sivasubramanianf1ddf042013-01-08 14:03:32 -0700356
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600357 if (!msm_lpm_levels)
358 return NULL;
359
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600360 for (i = 0; i < msm_lpm_level_count; i++) {
361 struct msm_rpmrs_level *level = &msm_lpm_levels[i];
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600362
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700363 modify_event_timer = false;
364
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600365 if (!level->available)
366 continue;
367
368 if (sleep_mode != level->sleep_mode)
369 continue;
370
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600371 lvl_latency_us =
372 level->latency_us + (level->latency_us *
373 ADJUST_LATENCY(sleep_mode));
374
375 lvl_overhead_us =
376 level->time_overhead_us + (level->time_overhead_us *
377 ADJUST_LATENCY(sleep_mode));
378
379 lvl_overhead_energy =
380 level->energy_overhead + level->energy_overhead *
381 ADJUST_LATENCY(sleep_mode);
382
383 if (time_param->latency_us < lvl_latency_us)
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600384 continue;
385
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700386 if (time_param->next_event_us &&
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600387 time_param->next_event_us < lvl_latency_us)
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700388 continue;
389
390 if (time_param->next_event_us) {
391 if ((time_param->next_event_us < time_param->sleep_us)
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600392 || ((time_param->next_event_us - lvl_latency_us) <
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700393 time_param->sleep_us)) {
394 modify_event_timer = true;
395 next_wakeup_us = time_param->next_event_us -
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600396 lvl_latency_us;
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700397 }
398 }
399
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600400 if (next_wakeup_us <= lvl_overhead_us)
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700401 continue;
402
Mahesh Sivasubramanian9063a292012-11-09 09:15:30 -0700403 if ((MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE == sleep_mode)
404 || (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == sleep_mode))
405 if (!cpu && msm_rpm_waiting_for_ack())
406 break;
407
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700408 if (next_wakeup_us <= 1) {
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600409 pwr = lvl_overhead_energy;
410 } else if (next_wakeup_us <= lvl_overhead_us) {
411 pwr = lvl_overhead_energy / next_wakeup_us;
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700412 } else if ((next_wakeup_us >> 10)
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600413 > lvl_overhead_us) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600414 pwr = level->steady_state_power;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600415 } else {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600416 pwr = level->steady_state_power;
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600417 pwr -= (lvl_overhead_us *
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600418 level->steady_state_power) /
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700419 next_wakeup_us;
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600420 pwr += lvl_overhead_energy / next_wakeup_us;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600421 }
422
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600423 if (!best_level || (best_level_pwr >= pwr)) {
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600424 best_level = level;
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600425 best_level_pwr = pwr;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600426 if (power)
427 *power = pwr;
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700428 if (modify_event_timer &&
429 (sleep_mode !=
430 MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT))
431 time_param->modified_time_us =
432 time_param->next_event_us -
Girish Mahadevane6b1dd42013-05-22 11:59:08 -0600433 lvl_latency_us;
Girish Mahadevana351c0b2013-02-22 11:06:00 -0700434 else
435 time_param->modified_time_us = 0;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600436 }
437 }
438
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600439 return best_level ? &best_level->l2_cache : NULL;
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600440}
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800441
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600442static struct msm_pm_sleep_ops msm_lpm_ops = {
443 .lowest_limits = msm_lpm_lowest_limits,
444 .enter_sleep = msm_lpm_enter_sleep,
445 .exit_sleep = msm_lpm_exit_sleep,
446};
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600447
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600448static int msm_lpm_get_l2_cache_value(struct device_node *node,
449 char *key, uint32_t *l2_val)
450{
451 int i;
452 struct lpm_lookup_table l2_mode_lookup[] = {
453 {MSM_SPM_L2_MODE_POWER_COLLAPSE, "l2_cache_pc"},
Murali Nalajala82123b02013-10-04 16:00:19 +0530454 {MSM_SPM_L2_MODE_PC_NO_RPM, "l2_cache_pc_no_rpm"},
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600455 {MSM_SPM_L2_MODE_GDHS, "l2_cache_gdhs"},
456 {MSM_SPM_L2_MODE_RETENTION, "l2_cache_retention"},
457 {MSM_SPM_L2_MODE_DISABLED, "l2_cache_active"}
458 };
459 const char *l2_str;
460 int ret;
461
462 ret = of_property_read_string(node, key, &l2_str);
463 if (!ret) {
464 ret = -EINVAL;
465 for (i = 0; i < ARRAY_SIZE(l2_mode_lookup); i++) {
466 if (!strcmp(l2_str, l2_mode_lookup[i].mode_name)) {
467 *l2_val = l2_mode_lookup[i].modes;
468 ret = 0;
469 break;
470 }
471 }
472 }
473 return ret;
474}
475
476static int __devinit msm_lpm_levels_sysfs_add(void)
477{
478 struct kobject *module_kobj = NULL;
479 struct kobject *low_power_kobj = NULL;
480 int rc = 0;
481
482 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
483 if (!module_kobj) {
484 pr_err("%s: cannot find kobject for module %s\n",
485 __func__, KBUILD_MODNAME);
486 rc = -ENOENT;
487 goto resource_sysfs_add_exit;
488 }
489
490 low_power_kobj = kobject_create_and_add(
491 "enable_low_power", module_kobj);
492 if (!low_power_kobj) {
493 pr_err("%s: cannot create kobject\n", __func__);
494 rc = -ENOMEM;
495 goto resource_sysfs_add_exit;
496 }
497
498 rc = sysfs_create_group(low_power_kobj, &lpm_levels_attr_grp);
499resource_sysfs_add_exit:
500 if (rc) {
501 if (low_power_kobj) {
502 sysfs_remove_group(low_power_kobj,
503 &lpm_levels_attr_grp);
504 kobject_del(low_power_kobj);
505 }
506 }
507
508 return rc;
509}
510
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600511static int __devinit msm_lpm_levels_probe(struct platform_device *pdev)
512{
513 struct msm_rpmrs_level *levels = NULL;
514 struct msm_rpmrs_level *level = NULL;
515 struct device_node *node = NULL;
516 char *key = NULL;
517 uint32_t val = 0;
518 int ret = 0;
519 uint32_t num_levels = 0;
520 int idx = 0;
521
522 for_each_child_of_node(pdev->dev.of_node, node)
523 num_levels++;
524
525 levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level),
526 GFP_KERNEL);
527 if (!levels)
528 return -ENOMEM;
529
530 for_each_child_of_node(pdev->dev.of_node, node) {
531 level = &levels[idx++];
532 level->available = false;
533
534 key = "qcom,mode";
Archana Sathyakumare6a35102013-01-31 16:18:49 -0700535 ret = msm_pm_get_sleep_mode_value(node, key, &val);
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600536 if (ret)
537 goto fail;
538 level->sleep_mode = val;
539
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600540 key = "qcom,l2";
Archana Sathyakumare6a35102013-01-31 16:18:49 -0700541 ret = msm_lpm_get_l2_cache_value(node, key, &val);
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600542 if (ret)
543 goto fail;
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600544 level->l2_cache = val;
Mahesh Sivasubramanianb71ce092013-01-08 13:44:23 -0700545
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600546 key = "qcom,latency-us";
547 ret = of_property_read_u32(node, key, &val);
548 if (ret)
549 goto fail;
550 level->latency_us = val;
551
552 key = "qcom,ss-power";
553 ret = of_property_read_u32(node, key, &val);
554 if (ret)
555 goto fail;
556 level->steady_state_power = val;
557
558 key = "qcom,energy-overhead";
559 ret = of_property_read_u32(node, key, &val);
560 if (ret)
561 goto fail;
562 level->energy_overhead = val;
563
564 key = "qcom,time-overhead";
565 ret = of_property_read_u32(node, key, &val);
566 if (ret)
567 goto fail;
568 level->time_overhead_us = val;
569
570 level->available = true;
571 }
572
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600573 node = pdev->dev.of_node;
574 key = "qcom,no-l2-saw";
575 no_l2_saw = of_property_read_bool(node, key);
576
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600577 msm_lpm_levels = levels;
578 msm_lpm_level_count = idx;
579
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600580 if (num_online_cpus() == 1)
581 allowed_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800582
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600583 /* Do the following two steps only if L2 SAW is present */
584 if (!no_l2_saw) {
585 key = "qcom,default-l2-state";
586 if (msm_lpm_get_l2_cache_value(node, key, &default_l2_mode))
587 goto fail;
Priyanka Mathur3abfd442013-01-11 12:58:51 -0800588
Girish Mahadevanc45b4c72013-04-24 14:07:11 -0600589 if (msm_lpm_levels_sysfs_add())
590 goto fail;
591 register_hotcpu_notifier(&msm_lpm_cpu_nblk);
592 msm_pm_set_l2_flush_flag(0);
593 } else {
594 msm_pm_set_l2_flush_flag(1);
595 default_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE;
596 }
597
598 msm_lpm_level_update();
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600599 msm_pm_set_sleep_ops(&msm_lpm_ops);
Praveen Chidambaram85b7b282012-04-16 13:45:15 -0600600 return 0;
601fail:
602 pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key);
603 kfree(levels);
604 return -EFAULT;
605}
606
607static struct of_device_id msm_lpm_levels_match_table[] = {
608 {.compatible = "qcom,lpm-levels"},
609 {},
610};
611
612static struct platform_driver msm_lpm_levels_driver = {
613 .probe = msm_lpm_levels_probe,
614 .driver = {
615 .name = "lpm-levels",
616 .owner = THIS_MODULE,
617 .of_match_table = msm_lpm_levels_match_table,
618 },
619};
620
621static int __init msm_lpm_levels_module_init(void)
622{
623 return platform_driver_register(&msm_lpm_levels_driver);
624}
625late_initcall(msm_lpm_levels_module_init);