Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/thermal/cpu_cooling.c |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com) |
| 5 | * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org> |
| 6 | * |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 20 | * |
| 21 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 22 | */ |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/thermal.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 25 | #include <linux/cpufreq.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/cpu.h> |
| 29 | #include <linux/cpu_cooling.h> |
| 30 | |
| 31 | /** |
Eduardo Valentin | 3b3c074 | 2013-04-17 17:11:56 +0000 | [diff] [blame] | 32 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 33 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
| 34 | * registered. |
Eduardo Valentin | 3b3c074 | 2013-04-17 17:11:56 +0000 | [diff] [blame] | 35 | * @cool_dev: thermal_cooling_device pointer to keep track of the |
| 36 | * registered cooling device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 37 | * @cpufreq_state: integer value representing the current state of cpufreq |
| 38 | * cooling devices. |
| 39 | * @cpufreq_val: integer value representing the absolute value of the clipped |
| 40 | * frequency. |
| 41 | * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 42 | * |
| 43 | * This structure is required for keeping information of each |
Eduardo Valentin | 67d0b2a | 2013-04-17 17:12:19 +0000 | [diff] [blame] | 44 | * cpufreq_cooling_device registered. In order to prevent corruption of this a |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 45 | * mutex lock cooling_cpufreq_lock is used. |
| 46 | */ |
| 47 | struct cpufreq_cooling_device { |
| 48 | int id; |
| 49 | struct thermal_cooling_device *cool_dev; |
| 50 | unsigned int cpufreq_state; |
| 51 | unsigned int cpufreq_val; |
| 52 | struct cpumask allowed_cpus; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 53 | struct list_head node; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 54 | }; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 55 | static DEFINE_IDR(cpufreq_idr); |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 56 | static DEFINE_MUTEX(cooling_cpufreq_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 57 | |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 58 | static unsigned int cpufreq_dev_count; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 59 | |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 60 | static LIST_HEAD(cpufreq_dev_list); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * get_idr - function to get a unique id. |
| 64 | * @idr: struct idr * handle used to create a id. |
| 65 | * @id: int * value generated by this function. |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 66 | * |
| 67 | * This function will populate @id with an unique |
| 68 | * id, using the idr API. |
| 69 | * |
| 70 | * Return: 0 on success, an error code on failure. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 71 | */ |
| 72 | static int get_idr(struct idr *idr, int *id) |
| 73 | { |
Tejun Heo | 6deb69f | 2013-02-27 17:04:46 -0800 | [diff] [blame] | 74 | int ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 75 | |
| 76 | mutex_lock(&cooling_cpufreq_lock); |
Tejun Heo | 6deb69f | 2013-02-27 17:04:46 -0800 | [diff] [blame] | 77 | ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 78 | mutex_unlock(&cooling_cpufreq_lock); |
Tejun Heo | 6deb69f | 2013-02-27 17:04:46 -0800 | [diff] [blame] | 79 | if (unlikely(ret < 0)) |
| 80 | return ret; |
| 81 | *id = ret; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 82 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * release_idr - function to free the unique id. |
| 88 | * @idr: struct idr * handle used for creating the id. |
| 89 | * @id: int value representing the unique id. |
| 90 | */ |
| 91 | static void release_idr(struct idr *idr, int id) |
| 92 | { |
| 93 | mutex_lock(&cooling_cpufreq_lock); |
| 94 | idr_remove(idr, id); |
| 95 | mutex_unlock(&cooling_cpufreq_lock); |
| 96 | } |
| 97 | |
| 98 | /* Below code defines functions to be used for cpufreq as cooling device */ |
| 99 | |
| 100 | /** |
Eduardo Valentin | 82b9ee4 | 2013-04-17 17:12:00 +0000 | [diff] [blame] | 101 | * is_cpufreq_valid - function to check frequency transitioning capability. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 102 | * @cpu: cpu for which check is needed. |
Eduardo Valentin | 82b9ee4 | 2013-04-17 17:12:00 +0000 | [diff] [blame] | 103 | * |
| 104 | * This function will check the current state of the system if |
| 105 | * it is capable of changing the frequency for a given @cpu. |
| 106 | * |
| 107 | * Return: 0 if the system is not currently capable of changing |
| 108 | * the frequency of given cpu. !0 in case the frequency is changeable. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 109 | */ |
| 110 | static int is_cpufreq_valid(int cpu) |
| 111 | { |
| 112 | struct cpufreq_policy policy; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 113 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 114 | return !cpufreq_get_policy(&policy, cpu); |
| 115 | } |
| 116 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 117 | enum cpufreq_cooling_property { |
| 118 | GET_LEVEL, |
| 119 | GET_FREQ, |
| 120 | GET_MAXL, |
| 121 | }; |
| 122 | |
Eduardo Valentin | 2d6f28f | 2013-04-17 17:12:01 +0000 | [diff] [blame] | 123 | /** |
| 124 | * get_property - fetch a property of interest for a give cpu. |
| 125 | * @cpu: cpu for which the property is required |
| 126 | * @input: query parameter |
| 127 | * @output: query return |
| 128 | * @property: type of query (frequency, level, max level) |
| 129 | * |
| 130 | * This is the common function to |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 131 | * 1. get maximum cpu cooling states |
| 132 | * 2. translate frequency to cooling state |
| 133 | * 3. translate cooling state to frequency |
| 134 | * Note that the code may be not in good shape |
| 135 | * but it is written in this way in order to: |
| 136 | * a) reduce duplicate code as most of the code can be shared. |
| 137 | * b) make sure the logic is consistent when translating between |
| 138 | * cooling states and frequencies. |
Eduardo Valentin | 2d6f28f | 2013-04-17 17:12:01 +0000 | [diff] [blame] | 139 | * |
| 140 | * Return: 0 on success, -EINVAL when invalid parameters are passed. |
| 141 | */ |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 142 | static int get_property(unsigned int cpu, unsigned long input, |
Eduardo Valentin | d8892a0 | 2013-04-17 17:12:03 +0000 | [diff] [blame] | 143 | unsigned int *output, |
| 144 | enum cpufreq_cooling_property property) |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 145 | { |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 146 | int i; |
Eduardo Valentin | 4469b99 | 2013-04-17 17:11:58 +0000 | [diff] [blame] | 147 | unsigned long max_level = 0, level = 0; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 148 | unsigned int freq = CPUFREQ_ENTRY_INVALID; |
| 149 | int descend = -1; |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 150 | struct cpufreq_frequency_table *pos, *table = |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 151 | cpufreq_frequency_get_table(cpu); |
Eduardo Valentin | 79d2640 | 2013-04-17 17:11:55 +0000 | [diff] [blame] | 152 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 153 | if (!output) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | if (!table) |
| 157 | return -EINVAL; |
| 158 | |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 159 | cpufreq_for_each_valid_entry(pos, table) { |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 160 | /* ignore duplicate entry */ |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 161 | if (freq == pos->frequency) |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 162 | continue; |
| 163 | |
| 164 | /* get the frequency order */ |
Shawn Guo | 24c7a38 | 2013-05-28 06:22:32 +0000 | [diff] [blame] | 165 | if (freq != CPUFREQ_ENTRY_INVALID && descend == -1) |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 166 | descend = freq > pos->frequency; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 167 | |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 168 | freq = pos->frequency; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 169 | max_level++; |
| 170 | } |
Zhang Rui | a116776 | 2014-01-02 11:57:48 +0800 | [diff] [blame] | 171 | |
| 172 | /* No valid cpu frequency entry */ |
| 173 | if (max_level == 0) |
| 174 | return -EINVAL; |
| 175 | |
Eduardo Valentin | 1c9573a | 2013-11-13 14:11:09 -0400 | [diff] [blame] | 176 | /* max_level is an index, not a counter */ |
| 177 | max_level--; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 178 | |
| 179 | /* get max level */ |
| 180 | if (property == GET_MAXL) { |
| 181 | *output = (unsigned int)max_level; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | if (property == GET_FREQ) |
Eduardo Valentin | 1c9573a | 2013-11-13 14:11:09 -0400 | [diff] [blame] | 186 | level = descend ? input : (max_level - input); |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 187 | |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 188 | i = 0; |
| 189 | cpufreq_for_each_valid_entry(pos, table) { |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 190 | /* ignore duplicate entry */ |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 191 | if (freq == pos->frequency) |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 192 | continue; |
| 193 | |
| 194 | /* now we have a valid frequency entry */ |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 195 | freq = pos->frequency; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 196 | |
| 197 | if (property == GET_LEVEL && (unsigned int)input == freq) { |
| 198 | /* get level by frequency */ |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 199 | *output = descend ? i : (max_level - i); |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 200 | return 0; |
| 201 | } |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 202 | if (property == GET_FREQ && level == i) { |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 203 | /* get frequency by level */ |
| 204 | *output = freq; |
| 205 | return 0; |
| 206 | } |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 207 | i++; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 208 | } |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 209 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 210 | return -EINVAL; |
| 211 | } |
| 212 | |
Eduardo Valentin | 44952d3 | 2013-04-17 17:12:05 +0000 | [diff] [blame] | 213 | /** |
| 214 | * cpufreq_cooling_get_level - for a give cpu, return the cooling level. |
| 215 | * @cpu: cpu for which the level is required |
| 216 | * @freq: the frequency of interest |
| 217 | * |
| 218 | * This function will match the cooling level corresponding to the |
| 219 | * requested @freq and return it. |
| 220 | * |
| 221 | * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID |
| 222 | * otherwise. |
| 223 | */ |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 224 | unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq) |
| 225 | { |
| 226 | unsigned int val; |
| 227 | |
| 228 | if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL)) |
| 229 | return THERMAL_CSTATE_INVALID; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 230 | |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 231 | return (unsigned long)val; |
| 232 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 233 | EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level); |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 234 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 235 | /** |
| 236 | * get_cpu_frequency - get the absolute value of frequency from level. |
| 237 | * @cpu: cpu for which frequency is fetched. |
Eduardo Valentin | 41518c4 | 2013-04-17 17:12:07 +0000 | [diff] [blame] | 238 | * @level: cooling level |
| 239 | * |
| 240 | * This function matches cooling level with frequency. Based on a cooling level |
| 241 | * of frequency, equals cooling state of cpu cooling device, it will return |
| 242 | * the corresponding frequency. |
Zhang Rui | 475f41c | 2013-02-06 09:34:32 +0800 | [diff] [blame] | 243 | * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc |
Eduardo Valentin | 41518c4 | 2013-04-17 17:12:07 +0000 | [diff] [blame] | 244 | * |
| 245 | * Return: 0 on error, the corresponding frequency otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 246 | */ |
| 247 | static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level) |
| 248 | { |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 249 | int ret = 0; |
| 250 | unsigned int freq; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 251 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 252 | ret = get_property(cpu, level, &freq, GET_FREQ); |
| 253 | if (ret) |
| 254 | return 0; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 255 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 256 | return freq; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * cpufreq_apply_cooling - function to apply frequency clipping. |
| 261 | * @cpufreq_device: cpufreq_cooling_device pointer containing frequency |
| 262 | * clipping data. |
| 263 | * @cooling_state: value of the cooling state. |
Eduardo Valentin | 4b33deb | 2013-04-17 17:12:08 +0000 | [diff] [blame] | 264 | * |
| 265 | * Function used to make sure the cpufreq layer is aware of current thermal |
| 266 | * limits. The limits are applied by updating the cpufreq policy. |
| 267 | * |
| 268 | * Return: 0 on success, an error code otherwise (-EINVAL in case wrong |
| 269 | * cooling state). |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 270 | */ |
| 271 | static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 272 | unsigned long cooling_state) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 273 | { |
| 274 | unsigned int cpuid, clip_freq; |
Laurent Navet [Mali] | bde0066 | 2013-03-12 10:47:50 +0000 | [diff] [blame] | 275 | struct cpumask *mask = &cpufreq_device->allowed_cpus; |
| 276 | unsigned int cpu = cpumask_any(mask); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 277 | |
| 278 | |
| 279 | /* Check if the old cooling action is same as new cooling action */ |
| 280 | if (cpufreq_device->cpufreq_state == cooling_state) |
| 281 | return 0; |
| 282 | |
| 283 | clip_freq = get_cpu_frequency(cpu, cooling_state); |
| 284 | if (!clip_freq) |
| 285 | return -EINVAL; |
| 286 | |
| 287 | cpufreq_device->cpufreq_state = cooling_state; |
| 288 | cpufreq_device->cpufreq_val = clip_freq; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 289 | |
Laurent Navet [Mali] | bde0066 | 2013-03-12 10:47:50 +0000 | [diff] [blame] | 290 | for_each_cpu(cpuid, mask) { |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 291 | if (is_cpufreq_valid(cpuid)) |
| 292 | cpufreq_update_policy(cpuid); |
| 293 | } |
| 294 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. |
| 300 | * @nb: struct notifier_block * with callback info. |
| 301 | * @event: value showing cpufreq event for which this function invoked. |
| 302 | * @data: callback-specific data |
Eduardo Valentin | bab3055 | 2013-04-17 17:12:09 +0000 | [diff] [blame] | 303 | * |
Javi Merino | 9746b6e | 2014-06-25 18:11:17 +0100 | [diff] [blame] | 304 | * Callback to hijack the notification on cpufreq policy transition. |
Eduardo Valentin | bab3055 | 2013-04-17 17:12:09 +0000 | [diff] [blame] | 305 | * Every time there is a change in policy, we will intercept and |
| 306 | * update the cpufreq policy with thermal constraints. |
| 307 | * |
| 308 | * Return: 0 (success) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 309 | */ |
| 310 | static int cpufreq_thermal_notifier(struct notifier_block *nb, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 311 | unsigned long event, void *data) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 312 | { |
| 313 | struct cpufreq_policy *policy = data; |
| 314 | unsigned long max_freq = 0; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 315 | struct cpufreq_cooling_device *cpufreq_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 316 | |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 317 | if (event != CPUFREQ_ADJUST) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 318 | return 0; |
| 319 | |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 320 | mutex_lock(&cooling_cpufreq_lock); |
| 321 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { |
| 322 | if (!cpumask_test_cpu(policy->cpu, |
| 323 | &cpufreq_dev->allowed_cpus)) |
| 324 | continue; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 325 | |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 326 | if (!cpufreq_dev->cpufreq_val) |
| 327 | cpufreq_dev->cpufreq_val = get_cpu_frequency( |
| 328 | cpumask_any(&cpufreq_dev->allowed_cpus), |
| 329 | cpufreq_dev->cpufreq_state); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 330 | |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 331 | max_freq = cpufreq_dev->cpufreq_val; |
| 332 | |
| 333 | if (policy->max != max_freq) |
| 334 | cpufreq_verify_within_limits(policy, 0, max_freq); |
| 335 | } |
| 336 | mutex_unlock(&cooling_cpufreq_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
Eduardo Valentin | 1b9e352 | 2013-04-17 17:12:02 +0000 | [diff] [blame] | 341 | /* cpufreq cooling device callback functions are defined below */ |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 342 | |
| 343 | /** |
| 344 | * cpufreq_get_max_state - callback function to get the max cooling state. |
| 345 | * @cdev: thermal cooling device pointer. |
| 346 | * @state: fill this variable with the max cooling state. |
Eduardo Valentin | 62c0042 | 2013-04-17 17:12:12 +0000 | [diff] [blame] | 347 | * |
| 348 | * Callback for the thermal cooling device to return the cpufreq |
| 349 | * max cooling state. |
| 350 | * |
| 351 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 352 | */ |
| 353 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, |
| 354 | unsigned long *state) |
| 355 | { |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 356 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Laurent Navet [Mali] | bde0066 | 2013-03-12 10:47:50 +0000 | [diff] [blame] | 357 | struct cpumask *mask = &cpufreq_device->allowed_cpus; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 358 | unsigned int cpu; |
Dan Carpenter | 4f89038 | 2013-04-17 07:18:28 +0000 | [diff] [blame] | 359 | unsigned int count = 0; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 360 | int ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 361 | |
Laurent Navet [Mali] | bde0066 | 2013-03-12 10:47:50 +0000 | [diff] [blame] | 362 | cpu = cpumask_any(mask); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 363 | |
Dan Carpenter | 4f89038 | 2013-04-17 07:18:28 +0000 | [diff] [blame] | 364 | ret = get_property(cpu, 0, &count, GET_MAXL); |
hongbo.zhang | 9c51b05 | 2012-10-30 17:48:58 +0100 | [diff] [blame] | 365 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 366 | if (count > 0) |
| 367 | *state = count; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 368 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 369 | return ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
| 373 | * cpufreq_get_cur_state - callback function to get the current cooling state. |
| 374 | * @cdev: thermal cooling device pointer. |
| 375 | * @state: fill this variable with the current cooling state. |
Eduardo Valentin | 3672552 | 2013-04-17 17:12:13 +0000 | [diff] [blame] | 376 | * |
| 377 | * Callback for the thermal cooling device to return the cpufreq |
| 378 | * current cooling state. |
| 379 | * |
| 380 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 381 | */ |
| 382 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, |
| 383 | unsigned long *state) |
| 384 | { |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 385 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 386 | |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 387 | *state = cpufreq_device->cpufreq_state; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 388 | |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 389 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
| 393 | * cpufreq_set_cur_state - callback function to set the current cooling state. |
| 394 | * @cdev: thermal cooling device pointer. |
| 395 | * @state: set this variable to the current cooling state. |
Eduardo Valentin | 56e05fdb | 2013-04-17 17:12:14 +0000 | [diff] [blame] | 396 | * |
| 397 | * Callback for the thermal cooling device to change the cpufreq |
| 398 | * current cooling state. |
| 399 | * |
| 400 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 401 | */ |
| 402 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, |
| 403 | unsigned long state) |
| 404 | { |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 405 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 406 | |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 407 | return cpufreq_apply_cooling(cpufreq_device, state); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
| 411 | static struct thermal_cooling_device_ops const cpufreq_cooling_ops = { |
| 412 | .get_max_state = cpufreq_get_max_state, |
| 413 | .get_cur_state = cpufreq_get_cur_state, |
| 414 | .set_cur_state = cpufreq_set_cur_state, |
| 415 | }; |
| 416 | |
| 417 | /* Notifier for cpufreq policy change */ |
| 418 | static struct notifier_block thermal_cpufreq_notifier_block = { |
| 419 | .notifier_call = cpufreq_thermal_notifier, |
| 420 | }; |
| 421 | |
| 422 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 423 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
| 424 | * @np: a valid struct device_node to the cooling device device tree node |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 425 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
Eduardo Valentin | 12cb08b | 2013-04-17 17:12:15 +0000 | [diff] [blame] | 426 | * |
| 427 | * This interface function registers the cpufreq cooling device with the name |
| 428 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 429 | * cooling devices. It also gives the opportunity to link the cooling device |
| 430 | * with a device tree node, in order to bind it via the thermal DT code. |
Eduardo Valentin | 12cb08b | 2013-04-17 17:12:15 +0000 | [diff] [blame] | 431 | * |
| 432 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 433 | * on failure, it returns a corresponding ERR_PTR(). |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 434 | */ |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 435 | static struct thermal_cooling_device * |
| 436 | __cpufreq_cooling_register(struct device_node *np, |
| 437 | const struct cpumask *clip_cpus) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 438 | { |
| 439 | struct thermal_cooling_device *cool_dev; |
| 440 | struct cpufreq_cooling_device *cpufreq_dev = NULL; |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 441 | unsigned int min = 0, max = 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 442 | char dev_name[THERMAL_NAME_LENGTH]; |
Jonghwa Lee | a4b6fec | 2012-09-26 09:43:31 +0900 | [diff] [blame] | 443 | int ret = 0, i; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 444 | struct cpufreq_policy policy; |
| 445 | |
Eduardo Valentin | 1b9e352 | 2013-04-17 17:12:02 +0000 | [diff] [blame] | 446 | /* Verify that all the clip cpus have same freq_min, freq_max limit */ |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 447 | for_each_cpu(i, clip_cpus) { |
Eduardo Valentin | 1b9e352 | 2013-04-17 17:12:02 +0000 | [diff] [blame] | 448 | /* continue if cpufreq policy not found and not return error */ |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 449 | if (!cpufreq_get_policy(&policy, i)) |
| 450 | continue; |
| 451 | if (min == 0 && max == 0) { |
| 452 | min = policy.cpuinfo.min_freq; |
| 453 | max = policy.cpuinfo.max_freq; |
| 454 | } else { |
| 455 | if (min != policy.cpuinfo.min_freq || |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 456 | max != policy.cpuinfo.max_freq) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 457 | return ERR_PTR(-EINVAL); |
hongbo.zhang | 6b6519d | 2012-10-30 17:48:57 +0100 | [diff] [blame] | 458 | } |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 459 | } |
| 460 | cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device), |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 461 | GFP_KERNEL); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 462 | if (!cpufreq_dev) |
| 463 | return ERR_PTR(-ENOMEM); |
| 464 | |
| 465 | cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus); |
| 466 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 467 | ret = get_idr(&cpufreq_idr, &cpufreq_dev->id); |
| 468 | if (ret) { |
| 469 | kfree(cpufreq_dev); |
| 470 | return ERR_PTR(-EINVAL); |
| 471 | } |
| 472 | |
Eduardo Valentin | 99871a7 | 2013-04-17 17:12:17 +0000 | [diff] [blame] | 473 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
| 474 | cpufreq_dev->id); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 475 | |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 476 | cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev, |
| 477 | &cpufreq_cooling_ops); |
Wei Yongjun | 73b9bcd | 2013-10-25 21:55:42 +0800 | [diff] [blame] | 478 | if (IS_ERR(cool_dev)) { |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 479 | release_idr(&cpufreq_idr, cpufreq_dev->id); |
| 480 | kfree(cpufreq_dev); |
Wei Yongjun | 73b9bcd | 2013-10-25 21:55:42 +0800 | [diff] [blame] | 481 | return cool_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 482 | } |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 483 | cpufreq_dev->cool_dev = cool_dev; |
| 484 | cpufreq_dev->cpufreq_state = 0; |
| 485 | mutex_lock(&cooling_cpufreq_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 486 | |
| 487 | /* Register the notifier for first cpufreq cooling device */ |
| 488 | if (cpufreq_dev_count == 0) |
| 489 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 490 | CPUFREQ_POLICY_NOTIFIER); |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 491 | cpufreq_dev_count++; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 492 | list_add(&cpufreq_dev->node, &cpufreq_dev_list); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 493 | |
| 494 | mutex_unlock(&cooling_cpufreq_lock); |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 495 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 496 | return cool_dev; |
| 497 | } |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * cpufreq_cooling_register - function to create cpufreq cooling device. |
| 501 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
| 502 | * |
| 503 | * This interface function registers the cpufreq cooling device with the name |
| 504 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 505 | * cooling devices. |
| 506 | * |
| 507 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 508 | * on failure, it returns a corresponding ERR_PTR(). |
| 509 | */ |
| 510 | struct thermal_cooling_device * |
| 511 | cpufreq_cooling_register(const struct cpumask *clip_cpus) |
| 512 | { |
| 513 | return __cpufreq_cooling_register(NULL, clip_cpus); |
| 514 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 515 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 516 | |
| 517 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 518 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. |
| 519 | * @np: a valid struct device_node to the cooling device device tree node |
| 520 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
| 521 | * |
| 522 | * This interface function registers the cpufreq cooling device with the name |
| 523 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 524 | * cooling devices. Using this API, the cpufreq cooling device will be |
| 525 | * linked to the device tree node provided. |
| 526 | * |
| 527 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 528 | * on failure, it returns a corresponding ERR_PTR(). |
| 529 | */ |
| 530 | struct thermal_cooling_device * |
| 531 | of_cpufreq_cooling_register(struct device_node *np, |
| 532 | const struct cpumask *clip_cpus) |
| 533 | { |
| 534 | if (!np) |
| 535 | return ERR_PTR(-EINVAL); |
| 536 | |
| 537 | return __cpufreq_cooling_register(np, clip_cpus); |
| 538 | } |
| 539 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); |
| 540 | |
| 541 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 542 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. |
| 543 | * @cdev: thermal cooling device pointer. |
Eduardo Valentin | 135266b | 2013-04-17 17:12:16 +0000 | [diff] [blame] | 544 | * |
| 545 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 546 | */ |
| 547 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) |
| 548 | { |
Eduardo Valentin | 50e66c7 | 2013-08-15 10:54:46 -0400 | [diff] [blame] | 549 | struct cpufreq_cooling_device *cpufreq_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 550 | |
Eduardo Valentin | 50e66c7 | 2013-08-15 10:54:46 -0400 | [diff] [blame] | 551 | if (!cdev) |
| 552 | return; |
| 553 | |
| 554 | cpufreq_dev = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 555 | mutex_lock(&cooling_cpufreq_lock); |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 556 | list_del(&cpufreq_dev->node); |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 557 | cpufreq_dev_count--; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 558 | |
| 559 | /* Unregister the notifier for the last cpufreq cooling device */ |
Eduardo Valentin | 2d9bf71 | 2013-04-17 17:12:18 +0000 | [diff] [blame] | 560 | if (cpufreq_dev_count == 0) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 561 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 562 | CPUFREQ_POLICY_NOTIFIER); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 563 | mutex_unlock(&cooling_cpufreq_lock); |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 564 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 565 | thermal_cooling_device_unregister(cpufreq_dev->cool_dev); |
| 566 | release_idr(&cpufreq_idr, cpufreq_dev->id); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 567 | kfree(cpufreq_dev); |
| 568 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 569 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |