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