Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [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/kernel.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/cpufreq.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/msm_tsens.h> |
| 20 | #include <linux/workqueue.h> |
Eugene Seah | 7d6d273 | 2012-03-09 17:48:42 -0700 | [diff] [blame] | 21 | #include <linux/cpu.h> |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 22 | |
| 23 | #define DEF_TEMP_SENSOR 0 |
| 24 | #define DEF_THERMAL_CHECK_MS 1000 |
| 25 | #define DEF_ALLOWED_MAX_HIGH 60 |
| 26 | #define DEF_ALLOWED_MAX_FREQ 918000 |
| 27 | |
| 28 | static int enabled; |
| 29 | static int allowed_max_high = DEF_ALLOWED_MAX_HIGH; |
| 30 | static int allowed_max_low = (DEF_ALLOWED_MAX_HIGH - 10); |
| 31 | static int allowed_max_freq = DEF_ALLOWED_MAX_FREQ; |
| 32 | static int check_interval_ms = DEF_THERMAL_CHECK_MS; |
| 33 | |
| 34 | module_param(allowed_max_high, int, 0); |
| 35 | module_param(allowed_max_freq, int, 0); |
| 36 | module_param(check_interval_ms, int, 0); |
| 37 | |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 38 | static struct delayed_work check_temp_work; |
| 39 | |
Eugene Seah | 7d6d273 | 2012-03-09 17:48:42 -0700 | [diff] [blame] | 40 | static int update_cpu_max_freq(struct cpufreq_policy *cpu_policy, |
| 41 | int cpu, int max_freq) |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 42 | { |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 43 | int ret = 0; |
| 44 | |
| 45 | if (!cpu_policy) |
| 46 | return -EINVAL; |
| 47 | |
| 48 | cpufreq_verify_within_limits(cpu_policy, |
| 49 | cpu_policy->min, max_freq); |
| 50 | cpu_policy->user_policy.max = max_freq; |
| 51 | |
| 52 | ret = cpufreq_update_policy(cpu); |
Praveen Chidambaram | c7fc681 | 2012-02-01 13:09:50 -0700 | [diff] [blame] | 53 | if (!ret) |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 54 | pr_info("msm_thermal: Limiting core%d max frequency to %d\n", |
| 55 | cpu, max_freq); |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | static void check_temp(struct work_struct *work) |
| 61 | { |
| 62 | struct cpufreq_policy *cpu_policy = NULL; |
| 63 | struct tsens_device tsens_dev; |
| 64 | unsigned long temp = 0; |
| 65 | unsigned int max_freq = 0; |
| 66 | int update_policy = 0; |
| 67 | int cpu = 0; |
| 68 | int ret = 0; |
| 69 | |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 70 | tsens_dev.sensor_num = DEF_TEMP_SENSOR; |
| 71 | ret = tsens_get_temp(&tsens_dev, &temp); |
| 72 | if (ret) { |
Jeff Ohlstein | f744c86 | 2012-02-01 17:52:44 -0800 | [diff] [blame] | 73 | pr_debug("msm_thermal: Unable to read TSENS sensor %d\n", |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 74 | tsens_dev.sensor_num); |
| 75 | goto reschedule; |
| 76 | } |
| 77 | |
| 78 | for_each_possible_cpu(cpu) { |
Eugene Seah | 7d6d273 | 2012-03-09 17:48:42 -0700 | [diff] [blame] | 79 | update_policy = 0; |
| 80 | cpu_policy = cpufreq_cpu_get(cpu); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 81 | if (!cpu_policy) { |
Eugene Seah | 7d6d273 | 2012-03-09 17:48:42 -0700 | [diff] [blame] | 82 | pr_debug("msm_thermal: NULL policy on cpu %d\n", cpu); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 83 | continue; |
| 84 | } |
| 85 | if (temp >= allowed_max_high) { |
| 86 | if (cpu_policy->max > allowed_max_freq) { |
| 87 | update_policy = 1; |
| 88 | max_freq = allowed_max_freq; |
Eugene Seah | 7d6d273 | 2012-03-09 17:48:42 -0700 | [diff] [blame] | 89 | } else { |
| 90 | pr_debug("msm_thermal: policy max for cpu %d " |
| 91 | "already < allowed_max_freq\n", cpu); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 92 | } |
| 93 | } else if (temp < allowed_max_low) { |
| 94 | if (cpu_policy->max < cpu_policy->cpuinfo.max_freq) { |
| 95 | max_freq = cpu_policy->cpuinfo.max_freq; |
| 96 | update_policy = 1; |
Eugene Seah | 7d6d273 | 2012-03-09 17:48:42 -0700 | [diff] [blame] | 97 | } else { |
| 98 | pr_debug("msm_thermal: policy max for cpu %d " |
| 99 | "already at max allowed\n", cpu); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | if (update_policy) |
Eugene Seah | 7d6d273 | 2012-03-09 17:48:42 -0700 | [diff] [blame] | 104 | update_cpu_max_freq(cpu_policy, cpu, max_freq); |
| 105 | |
| 106 | cpufreq_cpu_put(cpu_policy); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | reschedule: |
| 110 | if (enabled) |
| 111 | schedule_delayed_work(&check_temp_work, |
| 112 | msecs_to_jiffies(check_interval_ms)); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 115 | static void disable_msm_thermal(void) |
| 116 | { |
| 117 | int cpu = 0; |
| 118 | struct cpufreq_policy *cpu_policy = NULL; |
| 119 | |
Eugene Seah | cbc0753 | 2012-04-11 19:32:27 -0600 | [diff] [blame] | 120 | /* make sure check_temp is no longer running */ |
| 121 | cancel_delayed_work(&check_temp_work); |
| 122 | flush_scheduled_work(); |
| 123 | |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 124 | for_each_possible_cpu(cpu) { |
Eugene Seah | 7d6d273 | 2012-03-09 17:48:42 -0700 | [diff] [blame] | 125 | cpu_policy = cpufreq_cpu_get(cpu); |
| 126 | if (cpu_policy) { |
| 127 | if (cpu_policy->max < cpu_policy->cpuinfo.max_freq) |
| 128 | update_cpu_max_freq(cpu_policy, cpu, |
| 129 | cpu_policy-> |
| 130 | cpuinfo.max_freq); |
| 131 | cpufreq_cpu_put(cpu_policy); |
| 132 | } |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 133 | } |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static int set_enabled(const char *val, const struct kernel_param *kp) |
| 137 | { |
| 138 | int ret = 0; |
| 139 | |
| 140 | ret = param_set_bool(val, kp); |
| 141 | if (!enabled) |
| 142 | disable_msm_thermal(); |
| 143 | else |
| 144 | pr_info("msm_thermal: no action for enabled = %d\n", enabled); |
| 145 | |
| 146 | pr_info("msm_thermal: enabled = %d\n", enabled); |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static struct kernel_param_ops module_ops = { |
| 152 | .set = set_enabled, |
| 153 | .get = param_get_bool, |
| 154 | }; |
| 155 | |
| 156 | module_param_cb(enabled, &module_ops, &enabled, 0644); |
| 157 | MODULE_PARM_DESC(enabled, "enforce thermal limit on cpu"); |
| 158 | |
| 159 | static int __init msm_thermal_init(void) |
| 160 | { |
| 161 | int ret = 0; |
| 162 | |
| 163 | enabled = 1; |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 164 | INIT_DELAYED_WORK(&check_temp_work, check_temp); |
| 165 | |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 166 | schedule_delayed_work(&check_temp_work, 0); |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | fs_initcall(msm_thermal_init); |
| 171 | |