Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * devfreq_cooling: Thermal cooling device implementation for devices using |
| 3 | * devfreq |
| 4 | * |
| 5 | * Copyright (C) 2014-2015 ARM Limited |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 12 | * kind, whether express or implied; without even the implied warranty |
| 13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * TODO: |
| 17 | * - If OPPs are added or removed after devfreq cooling has |
| 18 | * registered, the devfreq cooling won't react to it. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/devfreq.h> |
| 22 | #include <linux/devfreq_cooling.h> |
| 23 | #include <linux/export.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/pm_opp.h> |
| 26 | #include <linux/thermal.h> |
| 27 | |
Javi Merino | 9876b1a | 2015-09-10 18:09:31 +0100 | [diff] [blame] | 28 | #include <trace/events/thermal.h> |
| 29 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 30 | static DEFINE_MUTEX(devfreq_lock); |
| 31 | static DEFINE_IDR(devfreq_idr); |
| 32 | |
| 33 | /** |
| 34 | * struct devfreq_cooling_device - Devfreq cooling device |
| 35 | * @id: unique integer value corresponding to each |
| 36 | * devfreq_cooling_device registered. |
| 37 | * @cdev: Pointer to associated thermal cooling device. |
| 38 | * @devfreq: Pointer to associated devfreq device. |
| 39 | * @cooling_state: Current cooling state. |
| 40 | * @power_table: Pointer to table with maximum power draw for each |
| 41 | * cooling state. State is the index into the table, and |
| 42 | * the power is in mW. |
| 43 | * @freq_table: Pointer to a table with the frequencies sorted in descending |
| 44 | * order. You can index the table by cooling device state |
| 45 | * @freq_table_size: Size of the @freq_table and @power_table |
| 46 | * @power_ops: Pointer to devfreq_cooling_power, used to generate the |
| 47 | * @power_table. |
| 48 | */ |
| 49 | struct devfreq_cooling_device { |
| 50 | int id; |
| 51 | struct thermal_cooling_device *cdev; |
| 52 | struct devfreq *devfreq; |
| 53 | unsigned long cooling_state; |
Ram Chandrasekar | 510ac85 | 2016-09-22 13:32:33 -0600 | [diff] [blame] | 54 | unsigned long cooling_min_state; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 55 | u32 *power_table; |
| 56 | u32 *freq_table; |
| 57 | size_t freq_table_size; |
| 58 | struct devfreq_cooling_power *power_ops; |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * get_idr - function to get a unique id. |
| 63 | * @idr: struct idr * handle used to create a id. |
| 64 | * @id: int * value generated by this function. |
| 65 | * |
| 66 | * This function will populate @id with an unique |
| 67 | * id, using the idr API. |
| 68 | * |
| 69 | * Return: 0 on success, an error code on failure. |
| 70 | */ |
| 71 | static int get_idr(struct idr *idr, int *id) |
| 72 | { |
| 73 | int ret; |
| 74 | |
| 75 | mutex_lock(&devfreq_lock); |
| 76 | ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL); |
| 77 | mutex_unlock(&devfreq_lock); |
| 78 | if (unlikely(ret < 0)) |
| 79 | return ret; |
| 80 | *id = ret; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * release_idr - function to free the unique id. |
| 87 | * @idr: struct idr * handle used for creating the id. |
| 88 | * @id: int value representing the unique id. |
| 89 | */ |
| 90 | static void release_idr(struct idr *idr, int id) |
| 91 | { |
| 92 | mutex_lock(&devfreq_lock); |
| 93 | idr_remove(idr, id); |
| 94 | mutex_unlock(&devfreq_lock); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * partition_enable_opps() - disable all opps above a given state |
| 99 | * @dfc: Pointer to devfreq we are operating on |
Ram Chandrasekar | 510ac85 | 2016-09-22 13:32:33 -0600 | [diff] [blame] | 100 | * @cdev_max_state: Max cooling device state we're setting |
| 101 | * @cdev_min_state: Min cooling device state we're setting |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 102 | * |
| 103 | * Go through the OPPs of the device, enabling all OPPs until |
| 104 | * @cdev_state and disabling those frequencies above it. |
| 105 | */ |
| 106 | static int partition_enable_opps(struct devfreq_cooling_device *dfc, |
Ram Chandrasekar | 510ac85 | 2016-09-22 13:32:33 -0600 | [diff] [blame] | 107 | unsigned long cdev_max_state, |
| 108 | unsigned long cdev_min_state) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 109 | { |
| 110 | int i; |
| 111 | struct device *dev = dfc->devfreq->dev.parent; |
| 112 | |
| 113 | for (i = 0; i < dfc->freq_table_size; i++) { |
| 114 | struct dev_pm_opp *opp; |
| 115 | int ret = 0; |
| 116 | unsigned int freq = dfc->freq_table[i]; |
Ram Chandrasekar | 510ac85 | 2016-09-22 13:32:33 -0600 | [diff] [blame] | 117 | bool want_enable = (i >= cdev_max_state) && |
| 118 | (i <= cdev_min_state) ? true : false; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 119 | |
| 120 | rcu_read_lock(); |
| 121 | opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable); |
| 122 | rcu_read_unlock(); |
| 123 | |
| 124 | if (PTR_ERR(opp) == -ERANGE) |
| 125 | continue; |
| 126 | else if (IS_ERR(opp)) |
| 127 | return PTR_ERR(opp); |
| 128 | |
| 129 | if (want_enable) |
| 130 | ret = dev_pm_opp_enable(dev, freq); |
| 131 | else |
| 132 | ret = dev_pm_opp_disable(dev, freq); |
| 133 | |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev, |
| 142 | unsigned long *state) |
| 143 | { |
| 144 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 145 | |
| 146 | *state = dfc->freq_table_size - 1; |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Ram Chandrasekar | 510ac85 | 2016-09-22 13:32:33 -0600 | [diff] [blame] | 151 | static int devfreq_cooling_get_min_state(struct thermal_cooling_device *cdev, |
| 152 | unsigned long *state) |
| 153 | { |
| 154 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 155 | |
| 156 | *state = dfc->cooling_min_state; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int devfreq_cooling_set_min_state(struct thermal_cooling_device *cdev, |
| 162 | unsigned long state) |
| 163 | { |
| 164 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 165 | struct devfreq *df = dfc->devfreq; |
| 166 | struct device *dev = df->dev.parent; |
| 167 | int ret; |
| 168 | |
| 169 | if (state == dfc->cooling_min_state) |
| 170 | return 0; |
| 171 | |
| 172 | dev_dbg(dev, "Setting cooling min state %lu\n", state); |
| 173 | |
| 174 | if (state >= dfc->freq_table_size) |
| 175 | state = dfc->freq_table_size - 1; |
| 176 | |
| 177 | ret = partition_enable_opps(dfc, dfc->cooling_state, state); |
| 178 | if (ret) |
| 179 | return ret; |
| 180 | |
| 181 | dfc->cooling_min_state = state; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 186 | static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev, |
| 187 | unsigned long *state) |
| 188 | { |
| 189 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 190 | |
| 191 | *state = dfc->cooling_state; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev, |
| 197 | unsigned long state) |
| 198 | { |
| 199 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 200 | struct devfreq *df = dfc->devfreq; |
| 201 | struct device *dev = df->dev.parent; |
| 202 | int ret; |
| 203 | |
| 204 | if (state == dfc->cooling_state) |
| 205 | return 0; |
| 206 | |
| 207 | dev_dbg(dev, "Setting cooling state %lu\n", state); |
| 208 | |
| 209 | if (state >= dfc->freq_table_size) |
| 210 | return -EINVAL; |
| 211 | |
Ram Chandrasekar | 510ac85 | 2016-09-22 13:32:33 -0600 | [diff] [blame] | 212 | ret = partition_enable_opps(dfc, state, dfc->cooling_min_state); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 213 | if (ret) |
| 214 | return ret; |
| 215 | |
| 216 | dfc->cooling_state = state; |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * freq_get_state() - get the cooling state corresponding to a frequency |
| 223 | * @dfc: Pointer to devfreq cooling device |
| 224 | * @freq: frequency in Hz |
| 225 | * |
| 226 | * Return: the cooling state associated with the @freq, or |
| 227 | * THERMAL_CSTATE_INVALID if it wasn't found. |
| 228 | */ |
| 229 | static unsigned long |
| 230 | freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq) |
| 231 | { |
| 232 | int i; |
| 233 | |
| 234 | for (i = 0; i < dfc->freq_table_size; i++) { |
| 235 | if (dfc->freq_table[i] == freq) |
| 236 | return i; |
| 237 | } |
| 238 | |
| 239 | return THERMAL_CSTATE_INVALID; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * get_static_power() - calculate the static power |
| 244 | * @dfc: Pointer to devfreq cooling device |
| 245 | * @freq: Frequency in Hz |
| 246 | * |
| 247 | * Calculate the static power in milliwatts using the supplied |
| 248 | * get_static_power(). The current voltage is calculated using the |
| 249 | * OPP library. If no get_static_power() was supplied, assume the |
| 250 | * static power is negligible. |
| 251 | */ |
| 252 | static unsigned long |
| 253 | get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq) |
| 254 | { |
| 255 | struct devfreq *df = dfc->devfreq; |
| 256 | struct device *dev = df->dev.parent; |
| 257 | unsigned long voltage; |
| 258 | struct dev_pm_opp *opp; |
| 259 | |
| 260 | if (!dfc->power_ops->get_static_power) |
| 261 | return 0; |
| 262 | |
| 263 | rcu_read_lock(); |
| 264 | |
| 265 | opp = dev_pm_opp_find_freq_exact(dev, freq, true); |
| 266 | if (IS_ERR(opp) && (PTR_ERR(opp) == -ERANGE)) |
| 267 | opp = dev_pm_opp_find_freq_exact(dev, freq, false); |
| 268 | |
| 269 | voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */ |
| 270 | |
| 271 | rcu_read_unlock(); |
| 272 | |
| 273 | if (voltage == 0) { |
| 274 | dev_warn_ratelimited(dev, |
| 275 | "Failed to get voltage for frequency %lu: %ld\n", |
| 276 | freq, IS_ERR(opp) ? PTR_ERR(opp) : 0); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | return dfc->power_ops->get_static_power(voltage); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * get_dynamic_power - calculate the dynamic power |
| 285 | * @dfc: Pointer to devfreq cooling device |
| 286 | * @freq: Frequency in Hz |
| 287 | * @voltage: Voltage in millivolts |
| 288 | * |
| 289 | * Calculate the dynamic power in milliwatts consumed by the device at |
| 290 | * frequency @freq and voltage @voltage. If the get_dynamic_power() |
| 291 | * was supplied as part of the devfreq_cooling_power struct, then that |
| 292 | * function is used. Otherwise, a simple power model (Pdyn = Coeff * |
| 293 | * Voltage^2 * Frequency) is used. |
| 294 | */ |
| 295 | static unsigned long |
| 296 | get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq, |
| 297 | unsigned long voltage) |
| 298 | { |
Javi Merino | 61c8e8a | 2015-11-02 19:03:04 +0000 | [diff] [blame] | 299 | u64 power; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 300 | u32 freq_mhz; |
| 301 | struct devfreq_cooling_power *dfc_power = dfc->power_ops; |
| 302 | |
| 303 | if (dfc_power->get_dynamic_power) |
| 304 | return dfc_power->get_dynamic_power(freq, voltage); |
| 305 | |
| 306 | freq_mhz = freq / 1000000; |
| 307 | power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage; |
| 308 | do_div(power, 1000000000); |
| 309 | |
| 310 | return power; |
| 311 | } |
| 312 | |
| 313 | static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev, |
| 314 | struct thermal_zone_device *tz, |
| 315 | u32 *power) |
| 316 | { |
| 317 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 318 | struct devfreq *df = dfc->devfreq; |
| 319 | struct devfreq_dev_status *status = &df->last_status; |
| 320 | unsigned long state; |
| 321 | unsigned long freq = status->current_frequency; |
| 322 | u32 dyn_power, static_power; |
| 323 | |
| 324 | /* Get dynamic power for state */ |
| 325 | state = freq_get_state(dfc, freq); |
| 326 | if (state == THERMAL_CSTATE_INVALID) |
| 327 | return -EAGAIN; |
| 328 | |
| 329 | dyn_power = dfc->power_table[state]; |
| 330 | |
| 331 | /* Scale dynamic power for utilization */ |
| 332 | dyn_power = (dyn_power * status->busy_time) / status->total_time; |
| 333 | |
| 334 | /* Get static power */ |
| 335 | static_power = get_static_power(dfc, freq); |
| 336 | |
Javi Merino | 9876b1a | 2015-09-10 18:09:31 +0100 | [diff] [blame] | 337 | trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power, |
| 338 | static_power); |
| 339 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 340 | *power = dyn_power + static_power; |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev, |
| 346 | struct thermal_zone_device *tz, |
| 347 | unsigned long state, |
| 348 | u32 *power) |
| 349 | { |
| 350 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 351 | unsigned long freq; |
| 352 | u32 static_power; |
| 353 | |
Shawn Lin | e3da1cb | 2016-08-22 16:08:06 +0800 | [diff] [blame] | 354 | if (state >= dfc->freq_table_size) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 355 | return -EINVAL; |
| 356 | |
| 357 | freq = dfc->freq_table[state]; |
| 358 | static_power = get_static_power(dfc, freq); |
| 359 | |
| 360 | *power = dfc->power_table[state] + static_power; |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev, |
| 365 | struct thermal_zone_device *tz, |
| 366 | u32 power, unsigned long *state) |
| 367 | { |
| 368 | struct devfreq_cooling_device *dfc = cdev->devdata; |
| 369 | struct devfreq *df = dfc->devfreq; |
| 370 | struct devfreq_dev_status *status = &df->last_status; |
| 371 | unsigned long freq = status->current_frequency; |
| 372 | unsigned long busy_time; |
| 373 | s32 dyn_power; |
| 374 | u32 static_power; |
| 375 | int i; |
| 376 | |
| 377 | static_power = get_static_power(dfc, freq); |
| 378 | |
| 379 | dyn_power = power - static_power; |
| 380 | dyn_power = dyn_power > 0 ? dyn_power : 0; |
| 381 | |
| 382 | /* Scale dynamic power for utilization */ |
| 383 | busy_time = status->busy_time ?: 1; |
| 384 | dyn_power = (dyn_power * status->total_time) / busy_time; |
| 385 | |
| 386 | /* |
| 387 | * Find the first cooling state that is within the power |
| 388 | * budget for dynamic power. |
| 389 | */ |
| 390 | for (i = 0; i < dfc->freq_table_size - 1; i++) |
| 391 | if (dyn_power >= dfc->power_table[i]) |
| 392 | break; |
| 393 | |
| 394 | *state = i; |
Javi Merino | 9876b1a | 2015-09-10 18:09:31 +0100 | [diff] [blame] | 395 | trace_thermal_power_devfreq_limit(cdev, freq, *state, power); |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static struct thermal_cooling_device_ops devfreq_cooling_ops = { |
| 400 | .get_max_state = devfreq_cooling_get_max_state, |
| 401 | .get_cur_state = devfreq_cooling_get_cur_state, |
| 402 | .set_cur_state = devfreq_cooling_set_cur_state, |
Ram Chandrasekar | 510ac85 | 2016-09-22 13:32:33 -0600 | [diff] [blame] | 403 | .get_min_state = devfreq_cooling_get_min_state, |
| 404 | .set_min_state = devfreq_cooling_set_min_state, |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | /** |
| 408 | * devfreq_cooling_gen_tables() - Generate power and freq tables. |
| 409 | * @dfc: Pointer to devfreq cooling device. |
| 410 | * |
| 411 | * Generate power and frequency tables: the power table hold the |
| 412 | * device's maximum power usage at each cooling state (OPP). The |
| 413 | * static and dynamic power using the appropriate voltage and |
| 414 | * frequency for the state, is acquired from the struct |
| 415 | * devfreq_cooling_power, and summed to make the maximum power draw. |
| 416 | * |
| 417 | * The frequency table holds the frequencies in descending order. |
| 418 | * That way its indexed by cooling device state. |
| 419 | * |
| 420 | * The tables are malloced, and pointers put in dfc. They must be |
| 421 | * freed when unregistering the devfreq cooling device. |
| 422 | * |
| 423 | * Return: 0 on success, negative error code on failure. |
| 424 | */ |
| 425 | static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc) |
| 426 | { |
| 427 | struct devfreq *df = dfc->devfreq; |
| 428 | struct device *dev = df->dev.parent; |
| 429 | int ret, num_opps; |
| 430 | unsigned long freq; |
| 431 | u32 *power_table = NULL; |
| 432 | u32 *freq_table; |
| 433 | int i; |
| 434 | |
| 435 | num_opps = dev_pm_opp_get_opp_count(dev); |
| 436 | |
| 437 | if (dfc->power_ops) { |
| 438 | power_table = kcalloc(num_opps, sizeof(*power_table), |
| 439 | GFP_KERNEL); |
| 440 | if (!power_table) |
Dan Carpenter | ce5ee16 | 2015-11-04 16:36:20 +0300 | [diff] [blame] | 441 | return -ENOMEM; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | freq_table = kcalloc(num_opps, sizeof(*freq_table), |
| 445 | GFP_KERNEL); |
| 446 | if (!freq_table) { |
| 447 | ret = -ENOMEM; |
| 448 | goto free_power_table; |
| 449 | } |
| 450 | |
| 451 | for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) { |
| 452 | unsigned long power_dyn, voltage; |
| 453 | struct dev_pm_opp *opp; |
| 454 | |
| 455 | rcu_read_lock(); |
| 456 | |
| 457 | opp = dev_pm_opp_find_freq_floor(dev, &freq); |
| 458 | if (IS_ERR(opp)) { |
| 459 | rcu_read_unlock(); |
| 460 | ret = PTR_ERR(opp); |
| 461 | goto free_tables; |
| 462 | } |
| 463 | |
| 464 | voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */ |
| 465 | |
| 466 | rcu_read_unlock(); |
| 467 | |
| 468 | if (dfc->power_ops) { |
| 469 | power_dyn = get_dynamic_power(dfc, freq, voltage); |
| 470 | |
| 471 | dev_dbg(dev, "Dynamic power table: %lu MHz @ %lu mV: %lu = %lu mW\n", |
| 472 | freq / 1000000, voltage, power_dyn, power_dyn); |
| 473 | |
| 474 | power_table[i] = power_dyn; |
| 475 | } |
| 476 | |
| 477 | freq_table[i] = freq; |
| 478 | } |
| 479 | |
| 480 | if (dfc->power_ops) |
| 481 | dfc->power_table = power_table; |
| 482 | |
| 483 | dfc->freq_table = freq_table; |
| 484 | dfc->freq_table_size = num_opps; |
| 485 | |
| 486 | return 0; |
| 487 | |
| 488 | free_tables: |
| 489 | kfree(freq_table); |
| 490 | free_power_table: |
| 491 | kfree(power_table); |
| 492 | |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * of_devfreq_cooling_register_power() - Register devfreq cooling device, |
| 498 | * with OF and power information. |
| 499 | * @np: Pointer to OF device_node. |
| 500 | * @df: Pointer to devfreq device. |
| 501 | * @dfc_power: Pointer to devfreq_cooling_power. |
| 502 | * |
| 503 | * Register a devfreq cooling device. The available OPPs must be |
| 504 | * registered on the device. |
| 505 | * |
| 506 | * If @dfc_power is provided, the cooling device is registered with the |
| 507 | * power extensions. For the power extensions to work correctly, |
| 508 | * devfreq should use the simple_ondemand governor, other governors |
| 509 | * are not currently supported. |
| 510 | */ |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 511 | struct thermal_cooling_device * |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 512 | of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df, |
| 513 | struct devfreq_cooling_power *dfc_power) |
| 514 | { |
| 515 | struct thermal_cooling_device *cdev; |
| 516 | struct devfreq_cooling_device *dfc; |
| 517 | char dev_name[THERMAL_NAME_LENGTH]; |
| 518 | int err; |
| 519 | |
| 520 | dfc = kzalloc(sizeof(*dfc), GFP_KERNEL); |
| 521 | if (!dfc) |
| 522 | return ERR_PTR(-ENOMEM); |
| 523 | |
| 524 | dfc->devfreq = df; |
| 525 | |
| 526 | if (dfc_power) { |
| 527 | dfc->power_ops = dfc_power; |
| 528 | |
| 529 | devfreq_cooling_ops.get_requested_power = |
| 530 | devfreq_cooling_get_requested_power; |
| 531 | devfreq_cooling_ops.state2power = devfreq_cooling_state2power; |
| 532 | devfreq_cooling_ops.power2state = devfreq_cooling_power2state; |
| 533 | } |
| 534 | |
| 535 | err = devfreq_cooling_gen_tables(dfc); |
| 536 | if (err) |
| 537 | goto free_dfc; |
| 538 | |
| 539 | err = get_idr(&devfreq_idr, &dfc->id); |
| 540 | if (err) |
| 541 | goto free_tables; |
| 542 | |
Ram Chandrasekar | 510ac85 | 2016-09-22 13:32:33 -0600 | [diff] [blame] | 543 | dfc->cooling_min_state = dfc->freq_table_size - 1; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 544 | snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id); |
| 545 | |
| 546 | cdev = thermal_of_cooling_device_register(np, dev_name, dfc, |
| 547 | &devfreq_cooling_ops); |
| 548 | if (IS_ERR(cdev)) { |
| 549 | err = PTR_ERR(cdev); |
| 550 | dev_err(df->dev.parent, |
| 551 | "Failed to register devfreq cooling device (%d)\n", |
| 552 | err); |
| 553 | goto release_idr; |
| 554 | } |
| 555 | |
| 556 | dfc->cdev = cdev; |
| 557 | |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 558 | return cdev; |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 559 | |
| 560 | release_idr: |
| 561 | release_idr(&devfreq_idr, dfc->id); |
| 562 | free_tables: |
| 563 | kfree(dfc->power_table); |
| 564 | kfree(dfc->freq_table); |
| 565 | free_dfc: |
| 566 | kfree(dfc); |
| 567 | |
| 568 | return ERR_PTR(err); |
| 569 | } |
| 570 | EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power); |
| 571 | |
| 572 | /** |
| 573 | * of_devfreq_cooling_register() - Register devfreq cooling device, |
| 574 | * with OF information. |
| 575 | * @np: Pointer to OF device_node. |
| 576 | * @df: Pointer to devfreq device. |
| 577 | */ |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 578 | struct thermal_cooling_device * |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 579 | of_devfreq_cooling_register(struct device_node *np, struct devfreq *df) |
| 580 | { |
| 581 | return of_devfreq_cooling_register_power(np, df, NULL); |
| 582 | } |
| 583 | EXPORT_SYMBOL_GPL(of_devfreq_cooling_register); |
| 584 | |
| 585 | /** |
| 586 | * devfreq_cooling_register() - Register devfreq cooling device. |
| 587 | * @df: Pointer to devfreq device. |
| 588 | */ |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 589 | struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 590 | { |
| 591 | return of_devfreq_cooling_register(NULL, df); |
| 592 | } |
| 593 | EXPORT_SYMBOL_GPL(devfreq_cooling_register); |
| 594 | |
| 595 | /** |
| 596 | * devfreq_cooling_unregister() - Unregister devfreq cooling device. |
| 597 | * @dfc: Pointer to devfreq cooling device to unregister. |
| 598 | */ |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 599 | void devfreq_cooling_unregister(struct thermal_cooling_device *cdev) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 600 | { |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 601 | struct devfreq_cooling_device *dfc; |
| 602 | |
| 603 | if (!cdev) |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 604 | return; |
| 605 | |
Javi Merino | 3c99c2c | 2015-11-02 19:03:03 +0000 | [diff] [blame] | 606 | dfc = cdev->devdata; |
| 607 | |
Ørjan Eide | a76caf5 | 2015-09-10 18:09:30 +0100 | [diff] [blame] | 608 | thermal_cooling_device_unregister(dfc->cdev); |
| 609 | release_idr(&devfreq_idr, dfc->id); |
| 610 | kfree(dfc->power_table); |
| 611 | kfree(dfc->freq_table); |
| 612 | |
| 613 | kfree(dfc); |
| 614 | } |
| 615 | EXPORT_SYMBOL_GPL(devfreq_cooling_unregister); |