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