Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1 | /* 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 Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 21 | #include "lpm_resources.h" |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 22 | #include "pm.h" |
Mahesh Sivasubramanian | 11dad77 | 2012-07-13 14:00:01 -0600 | [diff] [blame] | 23 | #include "rpm-notifier.h" |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 24 | |
| 25 | static struct msm_rpmrs_level *msm_lpm_levels; |
| 26 | static int msm_lpm_level_count; |
| 27 | |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 28 | static 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 | |
| 40 | int msm_lpm_enter_sleep(uint32_t sclk_count, void *limits, |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 41 | bool from_idle, bool notify_rpm) |
| 42 | { |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 43 | int ret = 0; |
Mahesh Sivasubramanian | 2efbc35 | 2012-07-18 14:15:44 -0600 | [diff] [blame] | 44 | struct msm_rpmrs_limits *l = (struct msm_rpmrs_limits *)limits; |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 45 | |
Mahesh Sivasubramanian | 11dad77 | 2012-07-13 14:00:01 -0600 | [diff] [blame] | 46 | 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 Sivasubramanian | 2efbc35 | 2012-07-18 14:15:44 -0600 | [diff] [blame] | 52 | ret = msm_lpmrs_enter_sleep(sclk_count, l, from_idle, notify_rpm); |
Mahesh Sivasubramanian | 11dad77 | 2012-07-13 14:00:01 -0600 | [diff] [blame] | 53 | bail: |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 54 | return ret; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 55 | } |
| 56 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 57 | static void msm_lpm_exit_sleep(void *limits, bool from_idle, |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 58 | bool notify_rpm, bool collapsed) |
| 59 | { |
Mahesh Sivasubramanian | 11dad77 | 2012-07-13 14:00:01 -0600 | [diff] [blame] | 60 | msm_rpm_exit_sleep(); |
Girish Mahadevan | a9964a5 | 2012-06-29 10:14:09 -0600 | [diff] [blame] | 61 | msm_lpmrs_exit_sleep((struct msm_rpmrs_limits *)limits, |
| 62 | from_idle, notify_rpm, collapsed); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 65 | void msm_lpm_show_resources(void) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 66 | { |
| 67 | /* TODO */ |
| 68 | return; |
| 69 | } |
| 70 | |
Stephen Boyd | 3f4bac2 | 2012-05-30 10:03:13 -0700 | [diff] [blame] | 71 | s32 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 Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 94 | static void *msm_lpm_lowest_limits(bool from_idle, |
| 95 | enum msm_pm_sleep_mode sleep_mode, uint32_t latency_us, |
| 96 | uint32_t sleep_us, uint32_t *power) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 97 | { |
| 98 | unsigned int cpu = smp_processor_id(); |
| 99 | struct msm_rpmrs_level *best_level = NULL; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 100 | uint32_t pwr; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 101 | int i; |
| 102 | |
| 103 | if (!msm_lpm_levels) |
| 104 | return NULL; |
| 105 | |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 106 | msm_lpm_level_update(); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 107 | |
| 108 | for (i = 0; i < msm_lpm_level_count; i++) { |
| 109 | struct msm_rpmrs_level *level = &msm_lpm_levels[i]; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 110 | |
| 111 | if (!level->available) |
| 112 | continue; |
| 113 | |
| 114 | if (sleep_mode != level->sleep_mode) |
| 115 | continue; |
| 116 | |
| 117 | if (latency_us < level->latency_us) |
| 118 | continue; |
| 119 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 120 | if (sleep_us <= 1) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 121 | pwr = level->energy_overhead; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 122 | } else if (sleep_us <= level->time_overhead_us) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 123 | pwr = level->energy_overhead / sleep_us; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 124 | } else if ((sleep_us >> 10) > level->time_overhead_us) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 125 | pwr = level->steady_state_power; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 126 | } else { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 127 | pwr = level->steady_state_power; |
| 128 | pwr -= (level->time_overhead_us * |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 129 | level->steady_state_power)/sleep_us; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 130 | pwr += level->energy_overhead / sleep_us; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 131 | } |
| 132 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 133 | if (!best_level || best_level->rs_limits.power[cpu] >= pwr) { |
| 134 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 135 | level->rs_limits.latency_us[cpu] = level->latency_us; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 136 | level->rs_limits.power[cpu] = pwr; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 137 | best_level = level; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 138 | |
| 139 | if (power) |
| 140 | *power = pwr; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | return best_level ? &best_level->rs_limits : NULL; |
| 145 | } |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 146 | static struct msm_pm_sleep_ops msm_lpm_ops = { |
| 147 | .lowest_limits = msm_lpm_lowest_limits, |
| 148 | .enter_sleep = msm_lpm_enter_sleep, |
| 149 | .exit_sleep = msm_lpm_exit_sleep, |
| 150 | }; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 151 | |
| 152 | static int __devinit msm_lpm_levels_probe(struct platform_device *pdev) |
| 153 | { |
| 154 | struct msm_rpmrs_level *levels = NULL; |
| 155 | struct msm_rpmrs_level *level = NULL; |
| 156 | struct device_node *node = NULL; |
| 157 | char *key = NULL; |
| 158 | uint32_t val = 0; |
| 159 | int ret = 0; |
| 160 | uint32_t num_levels = 0; |
| 161 | int idx = 0; |
| 162 | |
| 163 | for_each_child_of_node(pdev->dev.of_node, node) |
| 164 | num_levels++; |
| 165 | |
| 166 | levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level), |
| 167 | GFP_KERNEL); |
| 168 | if (!levels) |
| 169 | return -ENOMEM; |
| 170 | |
| 171 | for_each_child_of_node(pdev->dev.of_node, node) { |
| 172 | level = &levels[idx++]; |
| 173 | level->available = false; |
| 174 | |
| 175 | key = "qcom,mode"; |
| 176 | ret = of_property_read_u32(node, key, &val); |
| 177 | if (ret) |
| 178 | goto fail; |
| 179 | level->sleep_mode = val; |
| 180 | |
| 181 | key = "qcom,xo"; |
| 182 | ret = of_property_read_u32(node, key, &val); |
| 183 | if (ret) |
| 184 | goto fail; |
| 185 | level->rs_limits.pxo = val; |
| 186 | |
| 187 | key = "qcom,l2"; |
| 188 | ret = of_property_read_u32(node, key, &val); |
| 189 | if (ret) |
| 190 | goto fail; |
| 191 | level->rs_limits.l2_cache = val; |
| 192 | |
| 193 | key = "qcom,vdd-dig-upper-bound"; |
| 194 | ret = of_property_read_u32(node, key, &val); |
| 195 | if (ret) |
| 196 | goto fail; |
| 197 | level->rs_limits.vdd_dig_upper_bound = val; |
| 198 | |
| 199 | key = "qcom,vdd-dig-lower-bound"; |
| 200 | ret = of_property_read_u32(node, key, &val); |
| 201 | if (ret) |
| 202 | goto fail; |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 203 | level->rs_limits.vdd_dig_lower_bound = val; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 204 | |
| 205 | key = "qcom,vdd-mem-upper-bound"; |
| 206 | ret = of_property_read_u32(node, key, &val); |
| 207 | if (ret) |
| 208 | goto fail; |
| 209 | level->rs_limits.vdd_mem_upper_bound = val; |
| 210 | |
| 211 | key = "qcom,vdd-mem-lower-bound"; |
| 212 | ret = of_property_read_u32(node, key, &val); |
| 213 | if (ret) |
| 214 | goto fail; |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 215 | level->rs_limits.vdd_mem_lower_bound = val; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 216 | |
| 217 | key = "qcom,latency-us"; |
| 218 | ret = of_property_read_u32(node, key, &val); |
| 219 | if (ret) |
| 220 | goto fail; |
| 221 | level->latency_us = val; |
| 222 | |
| 223 | key = "qcom,ss-power"; |
| 224 | ret = of_property_read_u32(node, key, &val); |
| 225 | if (ret) |
| 226 | goto fail; |
| 227 | level->steady_state_power = val; |
| 228 | |
| 229 | key = "qcom,energy-overhead"; |
| 230 | ret = of_property_read_u32(node, key, &val); |
| 231 | if (ret) |
| 232 | goto fail; |
| 233 | level->energy_overhead = val; |
| 234 | |
| 235 | key = "qcom,time-overhead"; |
| 236 | ret = of_property_read_u32(node, key, &val); |
| 237 | if (ret) |
| 238 | goto fail; |
| 239 | level->time_overhead_us = val; |
| 240 | |
| 241 | level->available = true; |
| 242 | } |
| 243 | |
| 244 | msm_lpm_levels = levels; |
| 245 | msm_lpm_level_count = idx; |
| 246 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 247 | msm_pm_set_sleep_ops(&msm_lpm_ops); |
| 248 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 249 | return 0; |
| 250 | fail: |
| 251 | pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key); |
| 252 | kfree(levels); |
| 253 | return -EFAULT; |
| 254 | } |
| 255 | |
| 256 | static struct of_device_id msm_lpm_levels_match_table[] = { |
| 257 | {.compatible = "qcom,lpm-levels"}, |
| 258 | {}, |
| 259 | }; |
| 260 | |
| 261 | static struct platform_driver msm_lpm_levels_driver = { |
| 262 | .probe = msm_lpm_levels_probe, |
| 263 | .driver = { |
| 264 | .name = "lpm-levels", |
| 265 | .owner = THIS_MODULE, |
| 266 | .of_match_table = msm_lpm_levels_match_table, |
| 267 | }, |
| 268 | }; |
| 269 | |
| 270 | static int __init msm_lpm_levels_module_init(void) |
| 271 | { |
| 272 | return platform_driver_register(&msm_lpm_levels_driver); |
| 273 | } |
| 274 | late_initcall(msm_lpm_levels_module_init); |