| @@ -1,445 +0,0 @@ |
| 1 | | −// SPDX-License-Identifier: GPL-2.0-only |
| 2 | | −/* |
| 3 | | − * drivers/thermal/clock_cooling.c |
| 4 | | − * |
| 5 | | − * Copyright (C) 2014 Eduardo Valentin <edubezval@gmail.com> |
| 6 | | − * |
| 7 | | − * Copyright (C) 2013 Texas Instruments Inc. |
| 8 | | − * Contact: Eduardo Valentin <eduardo.valentin@ti.com> |
| 9 | | − * |
| 10 | | − * Highly based on cpufreq_cooling.c. |
| 11 | | − * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com) |
| 12 | | − * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org> |
| 13 | | − */ |
| 14 | | −#include <linux/clk.h> |
| 15 | | −#include <linux/clock_cooling.h> |
| 16 | | −#include <linux/cpufreq.h> |
| 17 | | −#include <linux/device.h> |
| 18 | | −#include <linux/err.h> |
| 19 | | −#include <linux/export.h> |
| 20 | | −#include <linux/idr.h> |
| 21 | | −#include <linux/mutex.h> |
| 22 | | −#include <linux/pm_opp.h> |
| 23 | | −#include <linux/slab.h> |
| 24 | | −#include <linux/thermal.h> |
| 25 | | − |
| 26 | | −/** |
| 27 | | − * struct clock_cooling_device - data for cooling device with clock |
| 28 | | − * @id: unique integer value corresponding to each clock_cooling_device |
| 29 | | − * registered. |
| 30 | | − * @dev: struct device pointer to the device being used to cool off using |
| 31 | | − * clock frequencies. |
| 32 | | − * @cdev: thermal_cooling_device pointer to keep track of the |
| 33 | | − * registered cooling device. |
| 34 | | − * @clk_rate_change_nb: reference to notifier block used to receive clock |
| 35 | | − * rate changes. |
| 36 | | − * @freq_table: frequency table used to keep track of available frequencies. |
| 37 | | − * @clock_state: integer value representing the current state of clock |
| 38 | | − * cooling devices. |
| 39 | | − * @clock_val: integer value representing the absolute value of the clipped |
| 40 | | − * frequency. |
| 41 | | − * @clk: struct clk reference used to enforce clock limits. |
| 42 | | − * @lock: mutex lock to protect this struct. |
| 43 | | − * |
| 44 | | − * This structure is required for keeping information of each |
| 45 | | − * clock_cooling_device registered. In order to prevent corruption of this a |
| 46 | | − * mutex @lock is used. |
| 47 | | − */ |
| 48 | | −struct clock_cooling_device { |
| 49 | | − int id; |
| 50 | | − struct device *dev; |
| 51 | | − struct thermal_cooling_device *cdev; |
| 52 | | − struct notifier_block clk_rate_change_nb; |
| 53 | | − struct cpufreq_frequency_table *freq_table; |
| 54 | | − unsigned long clock_state; |
| 55 | | − unsigned long clock_val; |
| 56 | | − struct clk *clk; |
| 57 | | − struct mutex lock; /* lock to protect the content of this struct */ |
| 58 | | −}; |
| 59 | | −#define to_clock_cooling_device(x) \ |
| 60 | | − container_of(x, struct clock_cooling_device, clk_rate_change_nb) |
| 61 | | −static DEFINE_IDA(clock_ida); |
| 62 | | − |
| 63 | | −/* Below code defines functions to be used for clock as cooling device */ |
| 64 | | − |
| 65 | | −enum clock_cooling_property { |
| 66 | | − GET_LEVEL, |
| 67 | | − GET_FREQ, |
| 68 | | − GET_MAXL, |
| 69 | | −}; |
| 70 | | − |
| 71 | | −/** |
| 72 | | − * clock_cooling_get_property - fetch a property of interest for a give cpu. |
| 73 | | − * @ccdev: clock cooling device reference |
| 74 | | − * @input: query parameter |
| 75 | | − * @output: query return |
| 76 | | − * @property: type of query (frequency, level, max level) |
| 77 | | − * |
| 78 | | − * This is the common function to |
| 79 | | − * 1. get maximum clock cooling states |
| 80 | | − * 2. translate frequency to cooling state |
| 81 | | − * 3. translate cooling state to frequency |
| 82 | | − * Note that the code may be not in good shape |
| 83 | | − * but it is written in this way in order to: |
| 84 | | − * a) reduce duplicate code as most of the code can be shared. |
| 85 | | − * b) make sure the logic is consistent when translating between |
| 86 | | − * cooling states and frequencies. |
| 87 | | − * |
| 88 | | − * Return: 0 on success, -EINVAL when invalid parameters are passed. |
| 89 | | − */ |
| 90 | | −static int clock_cooling_get_property(struct clock_cooling_device *ccdev, |
| 91 | | − unsigned long input, |
| 92 | | − unsigned long *output, |
| 93 | | − enum clock_cooling_property property) |
| 94 | | −{ |
| 95 | | − int i; |
| 96 | | − unsigned long max_level = 0, level = 0; |
| 97 | | − unsigned int freq = CPUFREQ_ENTRY_INVALID; |
| 98 | | − int descend = -1; |
| 99 | | − struct cpufreq_frequency_table *pos, *table = ccdev->freq_table; |
| 100 | | − |
| 101 | | − if (!output) |
| 102 | | − return -EINVAL; |
| 103 | | − |
| 104 | | − if (!table) |
| 105 | | − return -EINVAL; |
| 106 | | − |
| 107 | | − cpufreq_for_each_valid_entry(pos, table) { |
| 108 | | − /* ignore duplicate entry */ |
| 109 | | − if (freq == pos->frequency) |
| 110 | | − continue; |
| 111 | | − |
| 112 | | − /* get the frequency order */ |
| 113 | | − if (freq != CPUFREQ_ENTRY_INVALID && descend == -1) |
| 114 | | − descend = freq > pos->frequency; |
| 115 | | − |
| 116 | | − freq = pos->frequency; |
| 117 | | − max_level++; |
| 118 | | − } |
| 119 | | − |
| 120 | | − /* No valid cpu frequency entry */ |
| 121 | | − if (max_level == 0) |
| 122 | | − return -EINVAL; |
| 123 | | − |
| 124 | | − /* max_level is an index, not a counter */ |
| 125 | | − max_level--; |
| 126 | | − |
| 127 | | − /* get max level */ |
| 128 | | − if (property == GET_MAXL) { |
| 129 | | − *output = max_level; |
| 130 | | − return 0; |
| 131 | | − } |
| 132 | | − |
| 133 | | − if (property == GET_FREQ) |
| 134 | | − level = descend ? input : (max_level - input); |
| 135 | | − |
| 136 | | − i = 0; |
| 137 | | − cpufreq_for_each_valid_entry(pos, table) { |
| 138 | | − /* ignore duplicate entry */ |
| 139 | | − if (freq == pos->frequency) |
| 140 | | − continue; |
| 141 | | − |
| 142 | | − /* now we have a valid frequency entry */ |
| 143 | | − freq = pos->frequency; |
| 144 | | − |
| 145 | | − if (property == GET_LEVEL && (unsigned int)input == freq) { |
| 146 | | − /* get level by frequency */ |
| 147 | | − *output = descend ? i : (max_level - i); |
| 148 | | − return 0; |
| 149 | | − } |
| 150 | | − if (property == GET_FREQ && level == i) { |
| 151 | | − /* get frequency by level */ |
| 152 | | − *output = freq; |
| 153 | | − return 0; |
| 154 | | − } |
| 155 | | − i++; |
| 156 | | − } |
| 157 | | − |
| 158 | | − return -EINVAL; |
| 159 | | −} |
| 160 | | − |
| 161 | | −/** |
| 162 | | − * clock_cooling_get_level - return the cooling level of given clock cooling. |
| 163 | | − * @cdev: reference of a thermal cooling device of used as clock cooling device |
| 164 | | − * @freq: the frequency of interest |
| 165 | | − * |
| 166 | | − * This function will match the cooling level corresponding to the |
| 167 | | − * requested @freq and return it. |
| 168 | | − * |
| 169 | | − * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID |
| 170 | | − * otherwise. |
| 171 | | − */ |
| 172 | | −unsigned long clock_cooling_get_level(struct thermal_cooling_device *cdev, |
| 173 | | − unsigned long freq) |
| 174 | | −{ |
| 175 | | − struct clock_cooling_device *ccdev = cdev->devdata; |
| 176 | | − unsigned long val; |
| 177 | | − |
| 178 | | − if (clock_cooling_get_property(ccdev, (unsigned long)freq, &val, |
| 179 | | − GET_LEVEL)) |
| 180 | | − return THERMAL_CSTATE_INVALID; |
| 181 | | − |
| 182 | | − return val; |
| 183 | | −} |
| 184 | | −EXPORT_SYMBOL_GPL(clock_cooling_get_level); |
| 185 | | − |
| 186 | | −/** |
| 187 | | − * clock_cooling_get_frequency - get the absolute value of frequency from level. |
| 188 | | − * @ccdev: clock cooling device reference |
| 189 | | − * @level: cooling level |
| 190 | | − * |
| 191 | | − * This function matches cooling level with frequency. Based on a cooling level |
| 192 | | − * of frequency, equals cooling state of cpu cooling device, it will return |
| 193 | | − * the corresponding frequency. |
| 194 | | − * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc |
| 195 | | − * |
| 196 | | − * Return: 0 on error, the corresponding frequency otherwise. |
| 197 | | − */ |
| 198 | | −static unsigned long |
| 199 | | −clock_cooling_get_frequency(struct clock_cooling_device *ccdev, |
| 200 | | − unsigned long level) |
| 201 | | −{ |
| 202 | | − int ret = 0; |
| 203 | | − unsigned long freq; |
| 204 | | − |
| 205 | | − ret = clock_cooling_get_property(ccdev, level, &freq, GET_FREQ); |
| 206 | | − if (ret) |
| 207 | | − return 0; |
| 208 | | − |
| 209 | | − return freq; |
| 210 | | −} |
| 211 | | − |
| 212 | | −/** |
| 213 | | − * clock_cooling_apply - function to apply frequency clipping. |
| 214 | | − * @ccdev: clock_cooling_device pointer containing frequency clipping data. |
| 215 | | − * @cooling_state: value of the cooling state. |
| 216 | | − * |
| 217 | | − * Function used to make sure the clock layer is aware of current thermal |
| 218 | | − * limits. The limits are applied by updating the clock rate in case it is |
| 219 | | − * higher than the corresponding frequency based on the requested cooling_state. |
| 220 | | − * |
| 221 | | − * Return: 0 on success, an error code otherwise (-EINVAL in case wrong |
| 222 | | − * cooling state). |
| 223 | | − */ |
| 224 | | −static int clock_cooling_apply(struct clock_cooling_device *ccdev, |
| 225 | | − unsigned long cooling_state) |
| 226 | | −{ |
| 227 | | − unsigned long clip_freq, cur_freq; |
| 228 | | − int ret = 0; |
| 229 | | − |
| 230 | | − /* Here we write the clipping */ |
| 231 | | − /* Check if the old cooling action is same as new cooling action */ |
| 232 | | − if (ccdev->clock_state == cooling_state) |
| 233 | | − return 0; |
| 234 | | − |
| 235 | | − clip_freq = clock_cooling_get_frequency(ccdev, cooling_state); |
| 236 | | − if (!clip_freq) |
| 237 | | − return -EINVAL; |
| 238 | | − |
| 239 | | − cur_freq = clk_get_rate(ccdev->clk); |
| 240 | | − |
| 241 | | − mutex_lock(&ccdev->lock); |
| 242 | | − ccdev->clock_state = cooling_state; |
| 243 | | − ccdev->clock_val = clip_freq; |
| 244 | | − /* enforce clock level */ |
| 245 | | − if (cur_freq > clip_freq) |
| 246 | | − ret = clk_set_rate(ccdev->clk, clip_freq); |
| 247 | | − mutex_unlock(&ccdev->lock); |
| 248 | | − |
| 249 | | − return ret; |
| 250 | | −} |
| 251 | | − |
| 252 | | −/** |
| 253 | | − * clock_cooling_clock_notifier - notifier callback on clock rate changes. |
| 254 | | − * @nb: struct notifier_block * with callback info. |
| 255 | | − * @event: value showing clock event for which this function invoked. |
| 256 | | − * @data: callback-specific data |
| 257 | | − * |
| 258 | | − * Callback to hijack the notification on clock transition. |
| 259 | | − * Every time there is a clock change, we intercept all pre change events |
| 260 | | − * and block the transition in case the new rate infringes thermal limits. |
| 261 | | − * |
| 262 | | − * Return: NOTIFY_DONE (success) or NOTIFY_BAD (new_rate > thermal limit). |
| 263 | | − */ |
| 264 | | −static int clock_cooling_clock_notifier(struct notifier_block *nb, |
| 265 | | − unsigned long event, void *data) |
| 266 | | −{ |
| 267 | | − struct clk_notifier_data *ndata = data; |
| 268 | | − struct clock_cooling_device *ccdev = to_clock_cooling_device(nb); |
| 269 | | − |
| 270 | | − switch (event) { |
| 271 | | − case PRE_RATE_CHANGE: |
| 272 | | − /* |
| 273 | | − * checks on current state |
| 274 | | − * TODO: current method is not best we can find as it |
| 275 | | − * allows possibly voltage transitions, in case DVFS |
| 276 | | − * layer is also hijacking clock pre notifications. |
| 277 | | − */ |
| 278 | | − if (ndata->new_rate > ccdev->clock_val) |
| 279 | | − return NOTIFY_BAD; |
| 280 | | − /* fall through */ |
| 281 | | − case POST_RATE_CHANGE: |
| 282 | | − case ABORT_RATE_CHANGE: |
| 283 | | − default: |
| 284 | | − return NOTIFY_DONE; |
| 285 | | − } |
| 286 | | −} |
| 287 | | − |
| 288 | | −/* clock cooling device thermal callback functions are defined below */ |
| 289 | | − |
| 290 | | −/** |
| 291 | | − * clock_cooling_get_max_state - callback function to get the max cooling state. |
| 292 | | − * @cdev: thermal cooling device pointer. |
| 293 | | − * @state: fill this variable with the max cooling state. |
| 294 | | − * |
| 295 | | − * Callback for the thermal cooling device to return the clock |
| 296 | | − * max cooling state. |
| 297 | | − * |
| 298 | | − * Return: 0 on success, an error code otherwise. |
| 299 | | − */ |
| 300 | | −static int clock_cooling_get_max_state(struct thermal_cooling_device *cdev, |
| 301 | | − unsigned long *state) |
| 302 | | −{ |
| 303 | | − struct clock_cooling_device *ccdev = cdev->devdata; |
| 304 | | − unsigned long count = 0; |
| 305 | | − int ret; |
| 306 | | − |
| 307 | | − ret = clock_cooling_get_property(ccdev, 0, &count, GET_MAXL); |
| 308 | | − if (!ret) |
| 309 | | − *state = count; |
| 310 | | − |
| 311 | | − return ret; |
| 312 | | −} |
| 313 | | − |
| 314 | | −/** |
| 315 | | − * clock_cooling_get_cur_state - function to get the current cooling state. |
| 316 | | − * @cdev: thermal cooling device pointer. |
| 317 | | − * @state: fill this variable with the current cooling state. |
| 318 | | − * |
| 319 | | − * Callback for the thermal cooling device to return the clock |
| 320 | | − * current cooling state. |
| 321 | | − * |
| 322 | | − * Return: 0 (success) |
| 323 | | − */ |
| 324 | | −static int clock_cooling_get_cur_state(struct thermal_cooling_device *cdev, |
| 325 | | − unsigned long *state) |
| 326 | | −{ |
| 327 | | − struct clock_cooling_device *ccdev = cdev->devdata; |
| 328 | | − |
| 329 | | − *state = ccdev->clock_state; |
| 330 | | − |
| 331 | | − return 0; |
| 332 | | −} |
| 333 | | − |
| 334 | | −/** |
| 335 | | − * clock_cooling_set_cur_state - function to set the current cooling state. |
| 336 | | − * @cdev: thermal cooling device pointer. |
| 337 | | − * @state: set this variable to the current cooling state. |
| 338 | | − * |
| 339 | | − * Callback for the thermal cooling device to change the clock cooling |
| 340 | | − * current cooling state. |
| 341 | | − * |
| 342 | | − * Return: 0 on success, an error code otherwise. |
| 343 | | − */ |
| 344 | | −static int clock_cooling_set_cur_state(struct thermal_cooling_device *cdev, |
| 345 | | − unsigned long state) |
| 346 | | −{ |
| 347 | | − struct clock_cooling_device *clock_device = cdev->devdata; |
| 348 | | − |
| 349 | | − return clock_cooling_apply(clock_device, state); |
| 350 | | −} |
| 351 | | − |
| 352 | | −/* Bind clock callbacks to thermal cooling device ops */ |
| 353 | | −static struct thermal_cooling_device_ops const clock_cooling_ops = { |
| 354 | | − .get_max_state = clock_cooling_get_max_state, |
| 355 | | − .get_cur_state = clock_cooling_get_cur_state, |
| 356 | | − .set_cur_state = clock_cooling_set_cur_state, |
| 357 | | −}; |
| 358 | | − |
| 359 | | −/** |
| 360 | | − * clock_cooling_register - function to create clock cooling device. |
| 361 | | − * @dev: struct device pointer to the device used as clock cooling device. |
| 362 | | − * @clock_name: string containing the clock used as cooling mechanism. |
| 363 | | − * |
| 364 | | − * This interface function registers the clock cooling device with the name |
| 365 | | − * "thermal-clock-%x". The cooling device is based on clock frequencies. |
| 366 | | − * The struct device is assumed to be capable of DVFS transitions. |
| 367 | | − * The OPP layer is used to fetch and fill the available frequencies for |
| 368 | | − * the referred device. The ordered frequency table is used to control |
| 369 | | − * the clock cooling device cooling states and to limit clock transitions |
| 370 | | − * based on the cooling state requested by the thermal framework. |
| 371 | | − * |
| 372 | | − * Return: a valid struct thermal_cooling_device pointer on success, |
| 373 | | − * on failure, it returns a corresponding ERR_PTR(). |
| 374 | | − */ |
| 375 | | −struct thermal_cooling_device * |
| 376 | | −clock_cooling_register(struct device *dev, const char *clock_name) |
| 377 | | −{ |
| 378 | | − struct thermal_cooling_device *cdev; |
| 379 | | − struct clock_cooling_device *ccdev = NULL; |
| 380 | | − char dev_name[THERMAL_NAME_LENGTH]; |
| 381 | | − int ret = 0; |
| 382 | | − |
| 383 | | − ccdev = devm_kzalloc(dev, sizeof(*ccdev), GFP_KERNEL); |
| 384 | | − if (!ccdev) |
| 385 | | − return ERR_PTR(-ENOMEM); |
| 386 | | − |
| 387 | | − mutex_init(&ccdev->lock); |
| 388 | | − ccdev->dev = dev; |
| 389 | | − ccdev->clk = devm_clk_get(dev, clock_name); |
| 390 | | − if (IS_ERR(ccdev->clk)) |
| 391 | | − return ERR_CAST(ccdev->clk); |
| 392 | | − |
| 393 | | − ret = ida_simple_get(&clock_ida, 0, 0, GFP_KERNEL); |
| 394 | | − if (ret < 0) |
| 395 | | − return ERR_PTR(ret); |
| 396 | | − ccdev->id = ret; |
| 397 | | − |
| 398 | | − snprintf(dev_name, sizeof(dev_name), "thermal-clock-%d", ccdev->id); |
| 399 | | − |
| 400 | | − cdev = thermal_cooling_device_register(dev_name, ccdev, |
| 401 | | − &clock_cooling_ops); |
| 402 | | − if (IS_ERR(cdev)) { |
| 403 | | − ida_simple_remove(&clock_ida, ccdev->id); |
| 404 | | − return ERR_PTR(-EINVAL); |
| 405 | | − } |
| 406 | | − ccdev->cdev = cdev; |
| 407 | | − ccdev->clk_rate_change_nb.notifier_call = clock_cooling_clock_notifier; |
| 408 | | − |
| 409 | | − /* Assuming someone has already filled the opp table for this device */ |
| 410 | | − ret = dev_pm_opp_init_cpufreq_table(dev, &ccdev->freq_table); |
| 411 | | − if (ret) { |
| 412 | | − ida_simple_remove(&clock_ida, ccdev->id); |
| 413 | | − return ERR_PTR(ret); |
| 414 | | − } |
| 415 | | − ccdev->clock_state = 0; |
| 416 | | − ccdev->clock_val = clock_cooling_get_frequency(ccdev, 0); |
| 417 | | − |
| 418 | | − clk_notifier_register(ccdev->clk, &ccdev->clk_rate_change_nb); |
| 419 | | − |
| 420 | | − return cdev; |
| 421 | | −} |
| 422 | | −EXPORT_SYMBOL_GPL(clock_cooling_register); |
| 423 | | − |
| 424 | | −/** |
| 425 | | − * clock_cooling_unregister - function to remove clock cooling device. |
| 426 | | − * @cdev: thermal cooling device pointer. |
| 427 | | − * |
| 428 | | − * This interface function unregisters the "thermal-clock-%x" cooling device. |
| 429 | | − */ |
| 430 | | −void clock_cooling_unregister(struct thermal_cooling_device *cdev) |
| 431 | | −{ |
| 432 | | − struct clock_cooling_device *ccdev; |
| 433 | | − |
| 434 | | − if (!cdev) |
| 435 | | − return; |
| 436 | | − |
| 437 | | − ccdev = cdev->devdata; |
| 438 | | − |
| 439 | | − clk_notifier_unregister(ccdev->clk, &ccdev->clk_rate_change_nb); |
| 440 | | − dev_pm_opp_free_cpufreq_table(ccdev->dev, &ccdev->freq_table); |
| 441 | | − |
| 442 | | − thermal_cooling_device_unregister(ccdev->cdev); |
| 443 | | − ida_simple_remove(&clock_ida, ccdev->id); |
| 444 | | −} |
| 445 | | −EXPORT_SYMBOL_GPL(clock_cooling_unregister); |