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 | |
Viresh Kumar | 07d888d | 2014-12-04 09:41:49 +0530 | [diff] [blame] | 31 | /* |
| 32 | * Cooling state <-> CPUFreq frequency |
| 33 | * |
| 34 | * Cooling states are translated to frequencies throughout this driver and this |
| 35 | * is the relation between them. |
| 36 | * |
| 37 | * Highest cooling state corresponds to lowest possible frequency. |
| 38 | * |
| 39 | * i.e. |
| 40 | * level 0 --> 1st Max Freq |
| 41 | * level 1 --> 2nd Max Freq |
| 42 | * ... |
| 43 | */ |
| 44 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 45 | /** |
Eduardo Valentin | 3b3c074 | 2013-04-17 17:11:56 +0000 | [diff] [blame] | 46 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 47 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
| 48 | * registered. |
Eduardo Valentin | 3b3c074 | 2013-04-17 17:11:56 +0000 | [diff] [blame] | 49 | * @cool_dev: thermal_cooling_device pointer to keep track of the |
| 50 | * registered cooling device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 51 | * @cpufreq_state: integer value representing the current state of cpufreq |
| 52 | * cooling devices. |
| 53 | * @cpufreq_val: integer value representing the absolute value of the clipped |
| 54 | * frequency. |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 55 | * @max_level: maximum cooling level. One less than total number of valid |
| 56 | * cpufreq frequencies. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 57 | * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 58 | * |
Viresh Kumar | beca605 | 2014-12-04 09:41:48 +0530 | [diff] [blame] | 59 | * This structure is required for keeping information of each registered |
| 60 | * cpufreq_cooling_device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 61 | */ |
| 62 | struct cpufreq_cooling_device { |
| 63 | int id; |
| 64 | struct thermal_cooling_device *cool_dev; |
| 65 | unsigned int cpufreq_state; |
| 66 | unsigned int cpufreq_val; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 67 | unsigned int max_level; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 68 | struct cpumask allowed_cpus; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 69 | struct list_head node; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 70 | }; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 71 | static DEFINE_IDR(cpufreq_idr); |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 72 | static DEFINE_MUTEX(cooling_cpufreq_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 73 | |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 74 | static LIST_HEAD(cpufreq_dev_list); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * get_idr - function to get a unique id. |
| 78 | * @idr: struct idr * handle used to create a id. |
| 79 | * @id: int * value generated by this function. |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 80 | * |
| 81 | * This function will populate @id with an unique |
| 82 | * id, using the idr API. |
| 83 | * |
| 84 | * Return: 0 on success, an error code on failure. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 85 | */ |
| 86 | static int get_idr(struct idr *idr, int *id) |
| 87 | { |
Tejun Heo | 6deb69f | 2013-02-27 17:04:46 -0800 | [diff] [blame] | 88 | int ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 89 | |
| 90 | mutex_lock(&cooling_cpufreq_lock); |
Tejun Heo | 6deb69f | 2013-02-27 17:04:46 -0800 | [diff] [blame] | 91 | ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 92 | mutex_unlock(&cooling_cpufreq_lock); |
Tejun Heo | 6deb69f | 2013-02-27 17:04:46 -0800 | [diff] [blame] | 93 | if (unlikely(ret < 0)) |
| 94 | return ret; |
| 95 | *id = ret; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 96 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * release_idr - function to free the unique id. |
| 102 | * @idr: struct idr * handle used for creating the id. |
| 103 | * @id: int value representing the unique id. |
| 104 | */ |
| 105 | static void release_idr(struct idr *idr, int id) |
| 106 | { |
| 107 | mutex_lock(&cooling_cpufreq_lock); |
| 108 | idr_remove(idr, id); |
| 109 | mutex_unlock(&cooling_cpufreq_lock); |
| 110 | } |
| 111 | |
| 112 | /* Below code defines functions to be used for cpufreq as cooling device */ |
| 113 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 114 | enum cpufreq_cooling_property { |
| 115 | GET_LEVEL, |
| 116 | GET_FREQ, |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 117 | }; |
| 118 | |
Eduardo Valentin | 2d6f28fe | 2013-04-17 17:12:01 +0000 | [diff] [blame] | 119 | /** |
Viresh Kumar | 728c03c | 2014-12-04 09:41:47 +0530 | [diff] [blame] | 120 | * get_property - fetch a property of interest for a given cpu. |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 121 | * @cpufreq_dev: cpufreq_dev for which the property is required |
Eduardo Valentin | 2d6f28fe | 2013-04-17 17:12:01 +0000 | [diff] [blame] | 122 | * @input: query parameter |
| 123 | * @output: query return |
Viresh Kumar | 97afa4a | 2014-12-04 09:42:03 +0530 | [diff] [blame] | 124 | * @property: type of query (frequency, level) |
Eduardo Valentin | 2d6f28fe | 2013-04-17 17:12:01 +0000 | [diff] [blame] | 125 | * |
| 126 | * This is the common function to |
Viresh Kumar | 97afa4a | 2014-12-04 09:42:03 +0530 | [diff] [blame] | 127 | * 1. translate frequency to cooling state |
| 128 | * 2. translate cooling state to frequency |
Viresh Kumar | 728c03c | 2014-12-04 09:41:47 +0530 | [diff] [blame] | 129 | * |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 130 | * Note that the code may be not in good shape |
| 131 | * but it is written in this way in order to: |
| 132 | * a) reduce duplicate code as most of the code can be shared. |
| 133 | * b) make sure the logic is consistent when translating between |
| 134 | * cooling states and frequencies. |
Eduardo Valentin | 2d6f28fe | 2013-04-17 17:12:01 +0000 | [diff] [blame] | 135 | * |
| 136 | * Return: 0 on success, -EINVAL when invalid parameters are passed. |
| 137 | */ |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 138 | static int get_property(struct cpufreq_cooling_device *cpufreq_dev, |
| 139 | unsigned long input, unsigned int *output, |
Eduardo Valentin | d8892a0 | 2013-04-17 17:12:03 +0000 | [diff] [blame] | 140 | enum cpufreq_cooling_property property) |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 141 | { |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 142 | int i; |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 143 | unsigned long level = 0; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 144 | unsigned int freq = CPUFREQ_ENTRY_INVALID; |
| 145 | int descend = -1; |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 146 | struct cpufreq_frequency_table *pos, *table; |
Eduardo Valentin | 79d2640 | 2013-04-17 17:11:55 +0000 | [diff] [blame] | 147 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 148 | if (!output) |
| 149 | return -EINVAL; |
| 150 | |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 151 | table = cpufreq_frequency_get_table(cpumask_first(&cpufreq_dev->allowed_cpus)); |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 152 | if (!table) |
| 153 | return -EINVAL; |
| 154 | |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 155 | cpufreq_for_each_valid_entry(pos, table) { |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 156 | /* ignore duplicate entry */ |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 157 | if (freq == pos->frequency) |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 158 | continue; |
| 159 | |
| 160 | /* get the frequency order */ |
Shawn Guo | 24c7a38 | 2013-05-28 06:22:32 +0000 | [diff] [blame] | 161 | if (freq != CPUFREQ_ENTRY_INVALID && descend == -1) |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 162 | descend = freq > pos->frequency; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 163 | |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 164 | freq = pos->frequency; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 165 | } |
Zhang Rui | a116776 | 2014-01-02 11:57:48 +0800 | [diff] [blame] | 166 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 167 | if (property == GET_FREQ) |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 168 | level = descend ? input : (cpufreq_dev->max_level - input); |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 169 | |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 170 | i = 0; |
| 171 | cpufreq_for_each_valid_entry(pos, table) { |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 172 | /* ignore duplicate entry */ |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 173 | if (freq == pos->frequency) |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 174 | continue; |
| 175 | |
| 176 | /* now we have a valid frequency entry */ |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 177 | freq = pos->frequency; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 178 | |
| 179 | if (property == GET_LEVEL && (unsigned int)input == freq) { |
| 180 | /* get level by frequency */ |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 181 | *output = descend ? i : (cpufreq_dev->max_level - i); |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 182 | return 0; |
| 183 | } |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 184 | if (property == GET_FREQ && level == i) { |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 185 | /* get frequency by level */ |
| 186 | *output = freq; |
| 187 | return 0; |
| 188 | } |
Stratos Karafotis | 3c84ef3 | 2014-04-25 23:16:39 +0300 | [diff] [blame] | 189 | i++; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 190 | } |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 191 | |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 192 | return -EINVAL; |
| 193 | } |
| 194 | |
Eduardo Valentin | 44952d3 | 2013-04-17 17:12:05 +0000 | [diff] [blame] | 195 | /** |
Viresh Kumar | 728c03c | 2014-12-04 09:41:47 +0530 | [diff] [blame] | 196 | * cpufreq_cooling_get_level - for a given cpu, return the cooling level. |
Eduardo Valentin | 44952d3 | 2013-04-17 17:12:05 +0000 | [diff] [blame] | 197 | * @cpu: cpu for which the level is required |
| 198 | * @freq: the frequency of interest |
| 199 | * |
| 200 | * This function will match the cooling level corresponding to the |
| 201 | * requested @freq and return it. |
| 202 | * |
| 203 | * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID |
| 204 | * otherwise. |
| 205 | */ |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 206 | unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq) |
| 207 | { |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 208 | struct cpufreq_cooling_device *cpufreq_dev; |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 209 | |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 210 | mutex_lock(&cooling_cpufreq_lock); |
| 211 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { |
| 212 | if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) { |
| 213 | unsigned int val; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 214 | |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 215 | mutex_unlock(&cooling_cpufreq_lock); |
| 216 | if (get_property(cpufreq_dev, (unsigned long)freq, &val, |
| 217 | GET_LEVEL)) |
| 218 | return THERMAL_CSTATE_INVALID; |
| 219 | |
| 220 | return (unsigned long)val; |
| 221 | } |
| 222 | } |
| 223 | mutex_unlock(&cooling_cpufreq_lock); |
| 224 | |
| 225 | pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu); |
| 226 | return THERMAL_CSTATE_INVALID; |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 227 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 228 | EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level); |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 229 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 230 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 231 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. |
| 232 | * @nb: struct notifier_block * with callback info. |
| 233 | * @event: value showing cpufreq event for which this function invoked. |
| 234 | * @data: callback-specific data |
Eduardo Valentin | bab3055 | 2013-04-17 17:12:09 +0000 | [diff] [blame] | 235 | * |
Javi Merino | 9746b6e | 2014-06-25 18:11:17 +0100 | [diff] [blame] | 236 | * Callback to hijack the notification on cpufreq policy transition. |
Eduardo Valentin | bab3055 | 2013-04-17 17:12:09 +0000 | [diff] [blame] | 237 | * Every time there is a change in policy, we will intercept and |
| 238 | * update the cpufreq policy with thermal constraints. |
| 239 | * |
| 240 | * Return: 0 (success) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 241 | */ |
| 242 | static int cpufreq_thermal_notifier(struct notifier_block *nb, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 243 | unsigned long event, void *data) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 244 | { |
| 245 | struct cpufreq_policy *policy = data; |
| 246 | unsigned long max_freq = 0; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 247 | struct cpufreq_cooling_device *cpufreq_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 248 | |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 249 | if (event != CPUFREQ_ADJUST) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 250 | return 0; |
| 251 | |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 252 | mutex_lock(&cooling_cpufreq_lock); |
| 253 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { |
| 254 | if (!cpumask_test_cpu(policy->cpu, |
| 255 | &cpufreq_dev->allowed_cpus)) |
| 256 | continue; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 257 | |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 258 | max_freq = cpufreq_dev->cpufreq_val; |
| 259 | |
| 260 | if (policy->max != max_freq) |
| 261 | cpufreq_verify_within_limits(policy, 0, max_freq); |
| 262 | } |
| 263 | mutex_unlock(&cooling_cpufreq_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Eduardo Valentin | 1b9e352 | 2013-04-17 17:12:02 +0000 | [diff] [blame] | 268 | /* cpufreq cooling device callback functions are defined below */ |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * cpufreq_get_max_state - callback function to get the max cooling state. |
| 272 | * @cdev: thermal cooling device pointer. |
| 273 | * @state: fill this variable with the max cooling state. |
Eduardo Valentin | 62c0042 | 2013-04-17 17:12:12 +0000 | [diff] [blame] | 274 | * |
| 275 | * Callback for the thermal cooling device to return the cpufreq |
| 276 | * max cooling state. |
| 277 | * |
| 278 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 279 | */ |
| 280 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, |
| 281 | unsigned long *state) |
| 282 | { |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 283 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 284 | |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 285 | *state = cpufreq_device->max_level; |
| 286 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
| 290 | * cpufreq_get_cur_state - callback function to get the current cooling state. |
| 291 | * @cdev: thermal cooling device pointer. |
| 292 | * @state: fill this variable with the current cooling state. |
Eduardo Valentin | 3672552 | 2013-04-17 17:12:13 +0000 | [diff] [blame] | 293 | * |
| 294 | * Callback for the thermal cooling device to return the cpufreq |
| 295 | * current cooling state. |
| 296 | * |
| 297 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 298 | */ |
| 299 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, |
| 300 | unsigned long *state) |
| 301 | { |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 302 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 303 | |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 304 | *state = cpufreq_device->cpufreq_state; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 305 | |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 306 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * cpufreq_set_cur_state - callback function to set the current cooling state. |
| 311 | * @cdev: thermal cooling device pointer. |
| 312 | * @state: set this variable to the current cooling state. |
Eduardo Valentin | 56e05fd | 2013-04-17 17:12:14 +0000 | [diff] [blame] | 313 | * |
| 314 | * Callback for the thermal cooling device to change the cpufreq |
| 315 | * current cooling state. |
| 316 | * |
| 317 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 318 | */ |
| 319 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, |
| 320 | unsigned long state) |
| 321 | { |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 322 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 323 | unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus); |
| 324 | unsigned int clip_freq; |
Viresh Kumar | 521a2e5 | 2014-12-04 09:42:01 +0530 | [diff] [blame] | 325 | int ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 326 | |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 327 | /* Check if the old cooling action is same as new cooling action */ |
| 328 | if (cpufreq_device->cpufreq_state == state) |
| 329 | return 0; |
| 330 | |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 331 | ret = get_property(cpufreq_device, state, &clip_freq, GET_FREQ); |
Viresh Kumar | 521a2e5 | 2014-12-04 09:42:01 +0530 | [diff] [blame] | 332 | if (ret) |
| 333 | return ret; |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 334 | |
| 335 | cpufreq_device->cpufreq_state = state; |
| 336 | cpufreq_device->cpufreq_val = clip_freq; |
| 337 | |
| 338 | cpufreq_update_policy(cpu); |
| 339 | |
| 340 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
| 344 | static struct thermal_cooling_device_ops const cpufreq_cooling_ops = { |
| 345 | .get_max_state = cpufreq_get_max_state, |
| 346 | .get_cur_state = cpufreq_get_cur_state, |
| 347 | .set_cur_state = cpufreq_set_cur_state, |
| 348 | }; |
| 349 | |
| 350 | /* Notifier for cpufreq policy change */ |
| 351 | static struct notifier_block thermal_cpufreq_notifier_block = { |
| 352 | .notifier_call = cpufreq_thermal_notifier, |
| 353 | }; |
| 354 | |
| 355 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 356 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
| 357 | * @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] | 358 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 359 | * Normally this should be same as cpufreq policy->related_cpus. |
Eduardo Valentin | 12cb08b | 2013-04-17 17:12:15 +0000 | [diff] [blame] | 360 | * |
| 361 | * This interface function registers the cpufreq cooling device with the name |
| 362 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 363 | * cooling devices. It also gives the opportunity to link the cooling device |
| 364 | * 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] | 365 | * |
| 366 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 367 | * on failure, it returns a corresponding ERR_PTR(). |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 368 | */ |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 369 | static struct thermal_cooling_device * |
| 370 | __cpufreq_cooling_register(struct device_node *np, |
| 371 | const struct cpumask *clip_cpus) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 372 | { |
| 373 | struct thermal_cooling_device *cool_dev; |
Viresh Kumar | 5d3bdb8 | 2014-12-04 09:41:52 +0530 | [diff] [blame] | 374 | struct cpufreq_cooling_device *cpufreq_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 375 | char dev_name[THERMAL_NAME_LENGTH]; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 376 | struct cpufreq_frequency_table *pos, *table; |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 377 | int ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 378 | |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 379 | table = cpufreq_frequency_get_table(cpumask_first(clip_cpus)); |
| 380 | if (!table) { |
Eduardo Valentin | 0f1be51 | 2014-12-04 09:41:43 +0530 | [diff] [blame] | 381 | pr_debug("%s: CPUFreq table not found\n", __func__); |
| 382 | return ERR_PTR(-EPROBE_DEFER); |
| 383 | } |
| 384 | |
Viresh Kumar | 98d522f | 2014-12-04 09:41:50 +0530 | [diff] [blame] | 385 | cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 386 | if (!cpufreq_dev) |
| 387 | return ERR_PTR(-ENOMEM); |
| 388 | |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame^] | 389 | ret = get_property(cpufreq_dev, 0, &cpufreq_dev->cpufreq_val, GET_FREQ); |
Viresh Kumar | 521a2e5 | 2014-12-04 09:42:01 +0530 | [diff] [blame] | 390 | if (ret) { |
| 391 | pr_err("%s: Failed to get frequency: %d", __func__, ret); |
| 392 | cool_dev = ERR_PTR(ret); |
Viresh Kumar | 7adb635 | 2014-12-04 09:41:59 +0530 | [diff] [blame] | 393 | goto free_cdev; |
| 394 | } |
| 395 | |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 396 | /* Find max levels */ |
| 397 | cpufreq_for_each_valid_entry(pos, table) |
| 398 | cpufreq_dev->max_level++; |
| 399 | |
| 400 | /* max_level is an index, not a counter */ |
| 401 | cpufreq_dev->max_level--; |
| 402 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 403 | cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus); |
| 404 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 405 | ret = get_idr(&cpufreq_idr, &cpufreq_dev->id); |
| 406 | if (ret) { |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 407 | cool_dev = ERR_PTR(ret); |
| 408 | goto free_cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 409 | } |
| 410 | |
Eduardo Valentin | 99871a7 | 2013-04-17 17:12:17 +0000 | [diff] [blame] | 411 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
| 412 | cpufreq_dev->id); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 413 | |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 414 | cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev, |
| 415 | &cpufreq_cooling_ops); |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 416 | if (IS_ERR(cool_dev)) |
| 417 | goto remove_idr; |
| 418 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 419 | cpufreq_dev->cool_dev = cool_dev; |
Viresh Kumar | 92e615e | 2014-12-04 09:41:51 +0530 | [diff] [blame] | 420 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 421 | mutex_lock(&cooling_cpufreq_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 422 | |
| 423 | /* Register the notifier for first cpufreq cooling device */ |
Viresh Kumar | 2479bb6 | 2014-12-04 09:42:04 +0530 | [diff] [blame] | 424 | if (list_empty(&cpufreq_dev_list)) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 425 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 426 | CPUFREQ_POLICY_NOTIFIER); |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 427 | list_add(&cpufreq_dev->node, &cpufreq_dev_list); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 428 | |
| 429 | mutex_unlock(&cooling_cpufreq_lock); |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 430 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 431 | return cool_dev; |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 432 | |
| 433 | remove_idr: |
| 434 | release_idr(&cpufreq_idr, cpufreq_dev->id); |
| 435 | free_cdev: |
| 436 | kfree(cpufreq_dev); |
| 437 | |
| 438 | return cool_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 439 | } |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * cpufreq_cooling_register - function to create cpufreq cooling device. |
| 443 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
| 444 | * |
| 445 | * This interface function registers the cpufreq cooling device with the name |
| 446 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 447 | * cooling devices. |
| 448 | * |
| 449 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 450 | * on failure, it returns a corresponding ERR_PTR(). |
| 451 | */ |
| 452 | struct thermal_cooling_device * |
| 453 | cpufreq_cooling_register(const struct cpumask *clip_cpus) |
| 454 | { |
| 455 | return __cpufreq_cooling_register(NULL, clip_cpus); |
| 456 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 457 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 458 | |
| 459 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 460 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. |
| 461 | * @np: a valid struct device_node to the cooling device device tree node |
| 462 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
| 463 | * |
| 464 | * This interface function registers the cpufreq cooling device with the name |
| 465 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 466 | * cooling devices. Using this API, the cpufreq cooling device will be |
| 467 | * linked to the device tree node provided. |
| 468 | * |
| 469 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 470 | * on failure, it returns a corresponding ERR_PTR(). |
| 471 | */ |
| 472 | struct thermal_cooling_device * |
| 473 | of_cpufreq_cooling_register(struct device_node *np, |
| 474 | const struct cpumask *clip_cpus) |
| 475 | { |
| 476 | if (!np) |
| 477 | return ERR_PTR(-EINVAL); |
| 478 | |
| 479 | return __cpufreq_cooling_register(np, clip_cpus); |
| 480 | } |
| 481 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); |
| 482 | |
| 483 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 484 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. |
| 485 | * @cdev: thermal cooling device pointer. |
Eduardo Valentin | 135266b | 2013-04-17 17:12:16 +0000 | [diff] [blame] | 486 | * |
| 487 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 488 | */ |
| 489 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) |
| 490 | { |
Eduardo Valentin | 50e66c7 | 2013-08-15 10:54:46 -0400 | [diff] [blame] | 491 | struct cpufreq_cooling_device *cpufreq_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 492 | |
Eduardo Valentin | 50e66c7 | 2013-08-15 10:54:46 -0400 | [diff] [blame] | 493 | if (!cdev) |
| 494 | return; |
| 495 | |
| 496 | cpufreq_dev = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 497 | mutex_lock(&cooling_cpufreq_lock); |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 498 | list_del(&cpufreq_dev->node); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 499 | |
| 500 | /* Unregister the notifier for the last cpufreq cooling device */ |
Viresh Kumar | 2479bb6 | 2014-12-04 09:42:04 +0530 | [diff] [blame] | 501 | if (list_empty(&cpufreq_dev_list)) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 502 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 503 | CPUFREQ_POLICY_NOTIFIER); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 504 | mutex_unlock(&cooling_cpufreq_lock); |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 505 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 506 | thermal_cooling_device_unregister(cpufreq_dev->cool_dev); |
| 507 | release_idr(&cpufreq_idr, cpufreq_dev->id); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 508 | kfree(cpufreq_dev); |
| 509 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 510 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |