| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework |
| 4 | * for Non-CPU Devices. |
| 5 | * |
| 6 | * Copyright (C) 2011 Samsung Electronics |
| 7 | * MyungJoo Ham <myungjoo.ham@samsung.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/kmod.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/debugfs.h> |
| 14 | #include <linux/devfreq_cooling.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/export.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/stat.h> |
| 21 | #include <linux/pm_opp.h> |
| 22 | #include <linux/devfreq.h> |
| 23 | #include <linux/devfreq-governor.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/printk.h> |
| 28 | #include <linux/hrtimer.h> |
| 29 | #include <linux/of.h> |
| 30 | #include <linux/pm_qos.h> |
| 31 | #include <linux/units.h> |
| 32 | |
| 33 | #define CREATE_TRACE_POINTS |
| 34 | #include <trace/events/devfreq.h> |
| 35 | |
| 36 | #define IS_SUPPORTED_FLAG(f, name) ((f & DEVFREQ_GOV_FLAG_##name) ? true : false) |
| 37 | #define IS_SUPPORTED_ATTR(f, name) ((f & DEVFREQ_GOV_ATTR_##name) ? true : false) |
| 38 | |
| 39 | static struct class *devfreq_class; |
| 40 | static struct dentry *devfreq_debugfs; |
| 41 | |
| 42 | /* |
| 43 | * devfreq core provides delayed work based load monitoring helper |
| 44 | * functions. Governors can use these or can implement their own |
| 45 | * monitoring mechanism. |
| 46 | */ |
| 47 | static struct workqueue_struct *devfreq_wq; |
| 48 | |
| 49 | /* The list of all device-devfreq governors */ |
| 50 | static LIST_HEAD(devfreq_governor_list); |
| 51 | /* The list of all device-devfreq */ |
| 52 | static LIST_HEAD(devfreq_list); |
| 53 | static DEFINE_MUTEX(devfreq_list_lock); |
| 54 | |
| 55 | static const char timer_name[][DEVFREQ_NAME_LEN] = { |
| 56 | [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" }, |
| 57 | [DEVFREQ_TIMER_DELAYED] = { "delayed" }, |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * find_device_devfreq() - find devfreq struct using device pointer |
| 62 | * @dev: device pointer used to lookup device devfreq. |
| 63 | * |
| 64 | * Search the list of device devfreqs and return the matched device's |
| 65 | * devfreq info. devfreq_list_lock should be held by the caller. |
| 66 | */ |
| 67 | static struct devfreq *find_device_devfreq(struct device *dev) |
| 68 | { |
| 69 | struct devfreq *tmp_devfreq; |
| 70 | |
| 71 | lockdep_assert_held(&devfreq_list_lock); |
| 72 | |
| 73 | if (IS_ERR_OR_NULL(ptr: dev)) { |
| 74 | pr_err("DEVFREQ: %s: Invalid parameters\n" , __func__); |
| 75 | return ERR_PTR(error: -EINVAL); |
| 76 | } |
| 77 | |
| 78 | list_for_each_entry(tmp_devfreq, &devfreq_list, node) { |
| 79 | if (tmp_devfreq->dev.parent == dev) |
| 80 | return tmp_devfreq; |
| 81 | } |
| 82 | |
| 83 | return ERR_PTR(error: -ENODEV); |
| 84 | } |
| 85 | |
| 86 | static unsigned long find_available_min_freq(struct devfreq *devfreq) |
| 87 | { |
| 88 | struct dev_pm_opp *opp; |
| 89 | unsigned long min_freq = 0; |
| 90 | |
| 91 | opp = dev_pm_opp_find_freq_ceil_indexed(dev: devfreq->dev.parent, freq: &min_freq, index: 0); |
| 92 | if (IS_ERR(ptr: opp)) |
| 93 | min_freq = 0; |
| 94 | else |
| 95 | dev_pm_opp_put(opp); |
| 96 | |
| 97 | return min_freq; |
| 98 | } |
| 99 | |
| 100 | static unsigned long find_available_max_freq(struct devfreq *devfreq) |
| 101 | { |
| 102 | struct dev_pm_opp *opp; |
| 103 | unsigned long max_freq = ULONG_MAX; |
| 104 | |
| 105 | opp = dev_pm_opp_find_freq_floor_indexed(dev: devfreq->dev.parent, freq: &max_freq, index: 0); |
| 106 | if (IS_ERR(ptr: opp)) |
| 107 | max_freq = 0; |
| 108 | else |
| 109 | dev_pm_opp_put(opp); |
| 110 | |
| 111 | return max_freq; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * devfreq_get_freq_range() - Get the current freq range |
| 116 | * @devfreq: the devfreq instance |
| 117 | * @min_freq: the min frequency |
| 118 | * @max_freq: the max frequency |
| 119 | * |
| 120 | * This takes into consideration all constraints. |
| 121 | */ |
| 122 | void devfreq_get_freq_range(struct devfreq *devfreq, |
| 123 | unsigned long *min_freq, |
| 124 | unsigned long *max_freq) |
| 125 | { |
| 126 | unsigned long *freq_table = devfreq->freq_table; |
| 127 | s32 qos_min_freq, qos_max_freq; |
| 128 | |
| 129 | lockdep_assert_held(&devfreq->lock); |
| 130 | |
| 131 | /* |
| 132 | * Initialize minimum/maximum frequency from freq table. |
| 133 | * The devfreq drivers can initialize this in either ascending or |
| 134 | * descending order and devfreq core supports both. |
| 135 | */ |
| 136 | if (freq_table[0] < freq_table[devfreq->max_state - 1]) { |
| 137 | *min_freq = freq_table[0]; |
| 138 | *max_freq = freq_table[devfreq->max_state - 1]; |
| 139 | } else { |
| 140 | *min_freq = freq_table[devfreq->max_state - 1]; |
| 141 | *max_freq = freq_table[0]; |
| 142 | } |
| 143 | |
| 144 | /* Apply constraints from PM QoS */ |
| 145 | qos_min_freq = dev_pm_qos_read_value(dev: devfreq->dev.parent, |
| 146 | type: DEV_PM_QOS_MIN_FREQUENCY); |
| 147 | qos_max_freq = dev_pm_qos_read_value(dev: devfreq->dev.parent, |
| 148 | type: DEV_PM_QOS_MAX_FREQUENCY); |
| 149 | *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq); |
| 150 | if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE) |
| 151 | *max_freq = min(*max_freq, |
| 152 | (unsigned long)HZ_PER_KHZ * qos_max_freq); |
| 153 | |
| 154 | /* Apply constraints from OPP interface */ |
| 155 | *max_freq = clamp(*max_freq, devfreq->scaling_min_freq, devfreq->scaling_max_freq); |
| 156 | *min_freq = clamp(*min_freq, devfreq->scaling_min_freq, *max_freq); |
| 157 | } |
| 158 | EXPORT_SYMBOL(devfreq_get_freq_range); |
| 159 | |
| 160 | /** |
| 161 | * devfreq_get_freq_level() - Lookup freq_table for the frequency |
| 162 | * @devfreq: the devfreq instance |
| 163 | * @freq: the target frequency |
| 164 | */ |
| 165 | static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq) |
| 166 | { |
| 167 | int lev; |
| 168 | |
| 169 | for (lev = 0; lev < devfreq->max_state; lev++) |
| 170 | if (freq == devfreq->freq_table[lev]) |
| 171 | return lev; |
| 172 | |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | |
| 176 | static int set_freq_table(struct devfreq *devfreq) |
| 177 | { |
| 178 | struct dev_pm_opp *opp; |
| 179 | unsigned long freq; |
| 180 | int i, count; |
| 181 | |
| 182 | /* Initialize the freq_table from OPP table */ |
| 183 | count = dev_pm_opp_get_opp_count(dev: devfreq->dev.parent); |
| 184 | if (count <= 0) |
| 185 | return -EINVAL; |
| 186 | |
| 187 | devfreq->max_state = count; |
| 188 | devfreq->freq_table = devm_kcalloc(dev: devfreq->dev.parent, |
| 189 | n: devfreq->max_state, |
| 190 | size: sizeof(*devfreq->freq_table), |
| 191 | GFP_KERNEL); |
| 192 | if (!devfreq->freq_table) |
| 193 | return -ENOMEM; |
| 194 | |
| 195 | for (i = 0, freq = 0; i < devfreq->max_state; i++, freq++) { |
| 196 | opp = dev_pm_opp_find_freq_ceil_indexed(dev: devfreq->dev.parent, freq: &freq, index: 0); |
| 197 | if (IS_ERR(ptr: opp)) { |
| 198 | devm_kfree(dev: devfreq->dev.parent, p: devfreq->freq_table); |
| 199 | return PTR_ERR(ptr: opp); |
| 200 | } |
| 201 | dev_pm_opp_put(opp); |
| 202 | devfreq->freq_table[i] = freq; |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * devfreq_update_status() - Update statistics of devfreq behavior |
| 210 | * @devfreq: the devfreq instance |
| 211 | * @freq: the update target frequency |
| 212 | */ |
| 213 | int devfreq_update_status(struct devfreq *devfreq, unsigned long freq) |
| 214 | { |
| 215 | int lev, prev_lev, ret = 0; |
| 216 | u64 cur_time; |
| 217 | |
| 218 | lockdep_assert_held(&devfreq->lock); |
| 219 | cur_time = get_jiffies_64(); |
| 220 | |
| 221 | /* Immediately exit if previous_freq is not initialized yet. */ |
| 222 | if (!devfreq->previous_freq) |
| 223 | goto out; |
| 224 | |
| 225 | prev_lev = devfreq_get_freq_level(devfreq, freq: devfreq->previous_freq); |
| 226 | if (prev_lev < 0) { |
| 227 | ret = prev_lev; |
| 228 | goto out; |
| 229 | } |
| 230 | |
| 231 | devfreq->stats.time_in_state[prev_lev] += |
| 232 | cur_time - devfreq->stats.last_update; |
| 233 | |
| 234 | lev = devfreq_get_freq_level(devfreq, freq); |
| 235 | if (lev < 0) { |
| 236 | ret = lev; |
| 237 | goto out; |
| 238 | } |
| 239 | |
| 240 | if (lev != prev_lev) { |
| 241 | devfreq->stats.trans_table[ |
| 242 | (prev_lev * devfreq->max_state) + lev]++; |
| 243 | devfreq->stats.total_trans++; |
| 244 | } |
| 245 | |
| 246 | out: |
| 247 | devfreq->stats.last_update = cur_time; |
| 248 | return ret; |
| 249 | } |
| 250 | EXPORT_SYMBOL(devfreq_update_status); |
| 251 | |
| 252 | /** |
| 253 | * find_devfreq_governor() - find devfreq governor from name |
| 254 | * @name: name of the governor |
| 255 | * |
| 256 | * Search the list of devfreq governors and return the matched |
| 257 | * governor's pointer. devfreq_list_lock should be held by the caller. |
| 258 | */ |
| 259 | static struct devfreq_governor *find_devfreq_governor(const char *name) |
| 260 | { |
| 261 | struct devfreq_governor *tmp_governor; |
| 262 | |
| 263 | lockdep_assert_held(&devfreq_list_lock); |
| 264 | |
| 265 | if (IS_ERR_OR_NULL(ptr: name)) { |
| 266 | pr_err("DEVFREQ: %s: Invalid parameters\n" , __func__); |
| 267 | return ERR_PTR(error: -EINVAL); |
| 268 | } |
| 269 | |
| 270 | list_for_each_entry(tmp_governor, &devfreq_governor_list, node) { |
| 271 | if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN)) |
| 272 | return tmp_governor; |
| 273 | } |
| 274 | |
| 275 | return ERR_PTR(error: -ENODEV); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * try_then_request_governor() - Try to find the governor and request the |
| 280 | * module if is not found. |
| 281 | * @name: name of the governor |
| 282 | * |
| 283 | * Search the list of devfreq governors and request the module and try again |
| 284 | * if is not found. This can happen when both drivers (the governor driver |
| 285 | * and the driver that call devfreq_add_device) are built as modules. |
| 286 | * devfreq_list_lock should be held by the caller. Returns the matched |
| 287 | * governor's pointer or an error pointer. |
| 288 | */ |
| 289 | static struct devfreq_governor *try_then_request_governor(const char *name) |
| 290 | { |
| 291 | struct devfreq_governor *governor; |
| 292 | int err = 0; |
| 293 | |
| 294 | lockdep_assert_held(&devfreq_list_lock); |
| 295 | |
| 296 | if (IS_ERR_OR_NULL(ptr: name)) { |
| 297 | pr_err("DEVFREQ: %s: Invalid parameters\n" , __func__); |
| 298 | return ERR_PTR(error: -EINVAL); |
| 299 | } |
| 300 | |
| 301 | governor = find_devfreq_governor(name); |
| 302 | if (IS_ERR(ptr: governor)) { |
| 303 | mutex_unlock(lock: &devfreq_list_lock); |
| 304 | |
| 305 | if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND, |
| 306 | DEVFREQ_NAME_LEN)) |
| 307 | err = request_module("governor_%s" , "simpleondemand" ); |
| 308 | else |
| 309 | err = request_module("governor_%s" , name); |
| 310 | /* Restore previous state before return */ |
| 311 | mutex_lock(&devfreq_list_lock); |
| 312 | if (err) |
| 313 | return (err < 0) ? ERR_PTR(error: err) : ERR_PTR(error: -EINVAL); |
| 314 | |
| 315 | governor = find_devfreq_governor(name); |
| 316 | } |
| 317 | |
| 318 | return governor; |
| 319 | } |
| 320 | |
| 321 | static int devfreq_notify_transition(struct devfreq *devfreq, |
| 322 | struct devfreq_freqs *freqs, unsigned int state) |
| 323 | { |
| 324 | if (!devfreq) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | switch (state) { |
| 328 | case DEVFREQ_PRECHANGE: |
| 329 | srcu_notifier_call_chain(nh: &devfreq->transition_notifier_list, |
| 330 | DEVFREQ_PRECHANGE, v: freqs); |
| 331 | break; |
| 332 | |
| 333 | case DEVFREQ_POSTCHANGE: |
| 334 | srcu_notifier_call_chain(nh: &devfreq->transition_notifier_list, |
| 335 | DEVFREQ_POSTCHANGE, v: freqs); |
| 336 | break; |
| 337 | default: |
| 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq, |
| 345 | u32 flags) |
| 346 | { |
| 347 | struct devfreq_freqs freqs; |
| 348 | unsigned long cur_freq; |
| 349 | int err = 0; |
| 350 | |
| 351 | if (devfreq->profile->get_cur_freq) |
| 352 | devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq); |
| 353 | else |
| 354 | cur_freq = devfreq->previous_freq; |
| 355 | |
| 356 | freqs.old = cur_freq; |
| 357 | freqs.new = new_freq; |
| 358 | devfreq_notify_transition(devfreq, freqs: &freqs, DEVFREQ_PRECHANGE); |
| 359 | |
| 360 | err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags); |
| 361 | if (err) { |
| 362 | freqs.new = cur_freq; |
| 363 | devfreq_notify_transition(devfreq, freqs: &freqs, DEVFREQ_POSTCHANGE); |
| 364 | return err; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Print devfreq_frequency trace information between DEVFREQ_PRECHANGE |
| 369 | * and DEVFREQ_POSTCHANGE because for showing the correct frequency |
| 370 | * change order of between devfreq device and passive devfreq device. |
| 371 | */ |
| 372 | if (trace_devfreq_frequency_enabled() && new_freq != cur_freq) |
| 373 | trace_devfreq_frequency(devfreq, freq: new_freq, prev_freq: cur_freq); |
| 374 | |
| 375 | freqs.new = new_freq; |
| 376 | devfreq_notify_transition(devfreq, freqs: &freqs, DEVFREQ_POSTCHANGE); |
| 377 | |
| 378 | if (devfreq_update_status(devfreq, new_freq)) |
| 379 | dev_warn(&devfreq->dev, |
| 380 | "Couldn't update frequency transition information.\n" ); |
| 381 | |
| 382 | devfreq->previous_freq = new_freq; |
| 383 | |
| 384 | if (devfreq->suspend_freq) |
| 385 | devfreq->resume_freq = new_freq; |
| 386 | |
| 387 | return err; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * devfreq_update_target() - Reevaluate the device and configure frequency |
| 392 | * on the final stage. |
| 393 | * @devfreq: the devfreq instance. |
| 394 | * @freq: the new frequency of parent device. This argument |
| 395 | * is only used for devfreq device using passive governor. |
| 396 | * |
| 397 | * Note: Lock devfreq->lock before calling devfreq_update_target. This function |
| 398 | * should be only used by both update_devfreq() and devfreq governors. |
| 399 | */ |
| 400 | int devfreq_update_target(struct devfreq *devfreq, unsigned long freq) |
| 401 | { |
| 402 | unsigned long min_freq, max_freq; |
| 403 | int err = 0; |
| 404 | u32 flags = 0; |
| 405 | |
| 406 | lockdep_assert_held(&devfreq->lock); |
| 407 | |
| 408 | if (!devfreq->governor) |
| 409 | return -EINVAL; |
| 410 | |
| 411 | /* Reevaluate the proper frequency */ |
| 412 | err = devfreq->governor->get_target_freq(devfreq, &freq); |
| 413 | if (err) |
| 414 | return err; |
| 415 | devfreq_get_freq_range(devfreq, &min_freq, &max_freq); |
| 416 | |
| 417 | if (freq < min_freq) { |
| 418 | freq = min_freq; |
| 419 | flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */ |
| 420 | } |
| 421 | if (freq > max_freq) { |
| 422 | freq = max_freq; |
| 423 | flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */ |
| 424 | } |
| 425 | |
| 426 | return devfreq_set_target(devfreq, new_freq: freq, flags); |
| 427 | } |
| 428 | EXPORT_SYMBOL(devfreq_update_target); |
| 429 | |
| 430 | /* Load monitoring helper functions for governors use */ |
| 431 | |
| 432 | /** |
| 433 | * update_devfreq() - Reevaluate the device and configure frequency. |
| 434 | * @devfreq: the devfreq instance. |
| 435 | * |
| 436 | * Note: Lock devfreq->lock before calling update_devfreq |
| 437 | * This function is exported for governors. |
| 438 | */ |
| 439 | int update_devfreq(struct devfreq *devfreq) |
| 440 | { |
| 441 | return devfreq_update_target(devfreq, 0L); |
| 442 | } |
| 443 | EXPORT_SYMBOL(update_devfreq); |
| 444 | |
| 445 | /** |
| 446 | * devfreq_monitor() - Periodically poll devfreq objects. |
| 447 | * @work: the work struct used to run devfreq_monitor periodically. |
| 448 | * |
| 449 | */ |
| 450 | static void devfreq_monitor(struct work_struct *work) |
| 451 | { |
| 452 | int err; |
| 453 | struct devfreq *devfreq = container_of(work, |
| 454 | struct devfreq, work.work); |
| 455 | |
| 456 | mutex_lock(&devfreq->lock); |
| 457 | err = update_devfreq(devfreq); |
| 458 | if (err) |
| 459 | dev_err(&devfreq->dev, "dvfs failed with (%d) error\n" , err); |
| 460 | |
| 461 | if (devfreq->stop_polling) |
| 462 | goto out; |
| 463 | |
| 464 | queue_delayed_work(wq: devfreq_wq, dwork: &devfreq->work, |
| 465 | delay: msecs_to_jiffies(m: devfreq->profile->polling_ms)); |
| 466 | |
| 467 | out: |
| 468 | mutex_unlock(lock: &devfreq->lock); |
| 469 | trace_devfreq_monitor(devfreq); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * devfreq_monitor_start() - Start load monitoring of devfreq instance |
| 474 | * @devfreq: the devfreq instance. |
| 475 | * |
| 476 | * Helper function for starting devfreq device load monitoring. By default, |
| 477 | * deferrable timer is used for load monitoring. But the users can change this |
| 478 | * behavior using the "timer" type in devfreq_dev_profile. This function will be |
| 479 | * called by devfreq governor in response to the DEVFREQ_GOV_START event |
| 480 | * generated while adding a device to the devfreq framework. |
| 481 | */ |
| 482 | void devfreq_monitor_start(struct devfreq *devfreq) |
| 483 | { |
| 484 | if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) |
| 485 | return; |
| 486 | |
| 487 | mutex_lock(&devfreq->lock); |
| 488 | if (delayed_work_pending(&devfreq->work)) |
| 489 | goto out; |
| 490 | |
| 491 | switch (devfreq->profile->timer) { |
| 492 | case DEVFREQ_TIMER_DEFERRABLE: |
| 493 | INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor); |
| 494 | break; |
| 495 | case DEVFREQ_TIMER_DELAYED: |
| 496 | INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor); |
| 497 | break; |
| 498 | default: |
| 499 | goto out; |
| 500 | } |
| 501 | |
| 502 | if (devfreq->profile->polling_ms) |
| 503 | queue_delayed_work(wq: devfreq_wq, dwork: &devfreq->work, |
| 504 | delay: msecs_to_jiffies(m: devfreq->profile->polling_ms)); |
| 505 | |
| 506 | out: |
| 507 | devfreq->stop_polling = false; |
| 508 | mutex_unlock(lock: &devfreq->lock); |
| 509 | } |
| 510 | EXPORT_SYMBOL(devfreq_monitor_start); |
| 511 | |
| 512 | /** |
| 513 | * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance |
| 514 | * @devfreq: the devfreq instance. |
| 515 | * |
| 516 | * Helper function to stop devfreq device load monitoring. Function |
| 517 | * to be called from governor in response to DEVFREQ_GOV_STOP |
| 518 | * event when device is removed from devfreq framework. |
| 519 | */ |
| 520 | void devfreq_monitor_stop(struct devfreq *devfreq) |
| 521 | { |
| 522 | if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) |
| 523 | return; |
| 524 | |
| 525 | mutex_lock(&devfreq->lock); |
| 526 | if (devfreq->stop_polling) { |
| 527 | mutex_unlock(lock: &devfreq->lock); |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | devfreq->stop_polling = true; |
| 532 | mutex_unlock(lock: &devfreq->lock); |
| 533 | cancel_delayed_work_sync(dwork: &devfreq->work); |
| 534 | } |
| 535 | EXPORT_SYMBOL(devfreq_monitor_stop); |
| 536 | |
| 537 | /** |
| 538 | * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance |
| 539 | * @devfreq: the devfreq instance. |
| 540 | * |
| 541 | * Helper function to suspend devfreq device load monitoring. Function |
| 542 | * to be called from governor in response to DEVFREQ_GOV_SUSPEND |
| 543 | * event or when polling interval is set to zero. |
| 544 | * |
| 545 | * Note: Though this function is same as devfreq_monitor_stop(), |
| 546 | * intentionally kept separate to provide hooks for collecting |
| 547 | * transition statistics. |
| 548 | */ |
| 549 | void devfreq_monitor_suspend(struct devfreq *devfreq) |
| 550 | { |
| 551 | mutex_lock(&devfreq->lock); |
| 552 | if (devfreq->stop_polling) { |
| 553 | mutex_unlock(lock: &devfreq->lock); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | devfreq_update_status(devfreq, devfreq->previous_freq); |
| 558 | devfreq->stop_polling = true; |
| 559 | mutex_unlock(lock: &devfreq->lock); |
| 560 | |
| 561 | if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) |
| 562 | return; |
| 563 | |
| 564 | cancel_delayed_work_sync(dwork: &devfreq->work); |
| 565 | } |
| 566 | EXPORT_SYMBOL(devfreq_monitor_suspend); |
| 567 | |
| 568 | /** |
| 569 | * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance |
| 570 | * @devfreq: the devfreq instance. |
| 571 | * |
| 572 | * Helper function to resume devfreq device load monitoring. Function |
| 573 | * to be called from governor in response to DEVFREQ_GOV_RESUME |
| 574 | * event or when polling interval is set to non-zero. |
| 575 | */ |
| 576 | void devfreq_monitor_resume(struct devfreq *devfreq) |
| 577 | { |
| 578 | unsigned long freq; |
| 579 | |
| 580 | mutex_lock(&devfreq->lock); |
| 581 | |
| 582 | if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) |
| 583 | goto out_update; |
| 584 | |
| 585 | if (!devfreq->stop_polling) |
| 586 | goto out; |
| 587 | |
| 588 | if (!delayed_work_pending(&devfreq->work) && |
| 589 | devfreq->profile->polling_ms) |
| 590 | queue_delayed_work(wq: devfreq_wq, dwork: &devfreq->work, |
| 591 | delay: msecs_to_jiffies(m: devfreq->profile->polling_ms)); |
| 592 | |
| 593 | out_update: |
| 594 | devfreq->stats.last_update = get_jiffies_64(); |
| 595 | devfreq->stop_polling = false; |
| 596 | |
| 597 | if (devfreq->profile->get_cur_freq && |
| 598 | !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq)) |
| 599 | devfreq->previous_freq = freq; |
| 600 | |
| 601 | out: |
| 602 | mutex_unlock(lock: &devfreq->lock); |
| 603 | } |
| 604 | EXPORT_SYMBOL(devfreq_monitor_resume); |
| 605 | |
| 606 | /** |
| 607 | * devfreq_update_interval() - Update device devfreq monitoring interval |
| 608 | * @devfreq: the devfreq instance. |
| 609 | * @delay: new polling interval to be set. |
| 610 | * |
| 611 | * Helper function to set new load monitoring polling interval. Function |
| 612 | * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event. |
| 613 | */ |
| 614 | void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay) |
| 615 | { |
| 616 | unsigned int cur_delay = devfreq->profile->polling_ms; |
| 617 | unsigned int new_delay = *delay; |
| 618 | |
| 619 | mutex_lock(&devfreq->lock); |
| 620 | devfreq->profile->polling_ms = new_delay; |
| 621 | |
| 622 | if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) |
| 623 | goto out; |
| 624 | |
| 625 | if (devfreq->stop_polling) |
| 626 | goto out; |
| 627 | |
| 628 | /* if new delay is zero, stop polling */ |
| 629 | if (!new_delay) { |
| 630 | mutex_unlock(lock: &devfreq->lock); |
| 631 | cancel_delayed_work_sync(dwork: &devfreq->work); |
| 632 | return; |
| 633 | } |
| 634 | |
| 635 | /* if current delay is zero, start polling with new delay */ |
| 636 | if (!cur_delay) { |
| 637 | queue_delayed_work(wq: devfreq_wq, dwork: &devfreq->work, |
| 638 | delay: msecs_to_jiffies(m: devfreq->profile->polling_ms)); |
| 639 | goto out; |
| 640 | } |
| 641 | |
| 642 | /* if current delay is greater than new delay, restart polling */ |
| 643 | if (cur_delay > new_delay) { |
| 644 | mutex_unlock(lock: &devfreq->lock); |
| 645 | cancel_delayed_work_sync(dwork: &devfreq->work); |
| 646 | mutex_lock(&devfreq->lock); |
| 647 | if (!devfreq->stop_polling) |
| 648 | queue_delayed_work(wq: devfreq_wq, dwork: &devfreq->work, |
| 649 | delay: msecs_to_jiffies(m: devfreq->profile->polling_ms)); |
| 650 | } |
| 651 | out: |
| 652 | mutex_unlock(lock: &devfreq->lock); |
| 653 | } |
| 654 | EXPORT_SYMBOL(devfreq_update_interval); |
| 655 | |
| 656 | /** |
| 657 | * devfreq_notifier_call() - Notify that the device frequency requirements |
| 658 | * has been changed out of devfreq framework. |
| 659 | * @nb: the notifier_block (supposed to be devfreq->nb) |
| 660 | * @type: not used |
| 661 | * @devp: not used |
| 662 | * |
| 663 | * Called by a notifier that uses devfreq->nb. |
| 664 | */ |
| 665 | static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type, |
| 666 | void *devp) |
| 667 | { |
| 668 | struct devfreq *devfreq = container_of(nb, struct devfreq, nb); |
| 669 | int err = -EINVAL; |
| 670 | |
| 671 | mutex_lock(&devfreq->lock); |
| 672 | |
| 673 | devfreq->scaling_min_freq = find_available_min_freq(devfreq); |
| 674 | if (!devfreq->scaling_min_freq) |
| 675 | goto out; |
| 676 | |
| 677 | devfreq->scaling_max_freq = find_available_max_freq(devfreq); |
| 678 | if (!devfreq->scaling_max_freq) { |
| 679 | devfreq->scaling_max_freq = ULONG_MAX; |
| 680 | goto out; |
| 681 | } |
| 682 | |
| 683 | err = update_devfreq(devfreq); |
| 684 | |
| 685 | out: |
| 686 | mutex_unlock(lock: &devfreq->lock); |
| 687 | if (err) |
| 688 | dev_err(devfreq->dev.parent, |
| 689 | "failed to update frequency from OPP notifier (%d)\n" , |
| 690 | err); |
| 691 | |
| 692 | return NOTIFY_OK; |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * qos_notifier_call() - Common handler for QoS constraints. |
| 697 | * @devfreq: the devfreq instance. |
| 698 | */ |
| 699 | static int qos_notifier_call(struct devfreq *devfreq) |
| 700 | { |
| 701 | int err; |
| 702 | |
| 703 | mutex_lock(&devfreq->lock); |
| 704 | err = update_devfreq(devfreq); |
| 705 | mutex_unlock(lock: &devfreq->lock); |
| 706 | if (err) |
| 707 | dev_err(devfreq->dev.parent, |
| 708 | "failed to update frequency from PM QoS (%d)\n" , |
| 709 | err); |
| 710 | |
| 711 | return NOTIFY_OK; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * qos_min_notifier_call() - Callback for QoS min_freq changes. |
| 716 | * @nb: Should be devfreq->nb_min |
| 717 | * @val: not used |
| 718 | * @ptr: not used |
| 719 | */ |
| 720 | static int qos_min_notifier_call(struct notifier_block *nb, |
| 721 | unsigned long val, void *ptr) |
| 722 | { |
| 723 | return qos_notifier_call(container_of(nb, struct devfreq, nb_min)); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * qos_max_notifier_call() - Callback for QoS max_freq changes. |
| 728 | * @nb: Should be devfreq->nb_max |
| 729 | * @val: not used |
| 730 | * @ptr: not used |
| 731 | */ |
| 732 | static int qos_max_notifier_call(struct notifier_block *nb, |
| 733 | unsigned long val, void *ptr) |
| 734 | { |
| 735 | return qos_notifier_call(container_of(nb, struct devfreq, nb_max)); |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * devfreq_dev_release() - Callback for struct device to release the device. |
| 740 | * @dev: the devfreq device |
| 741 | * |
| 742 | * Remove devfreq from the list and release its resources. |
| 743 | */ |
| 744 | static void devfreq_dev_release(struct device *dev) |
| 745 | { |
| 746 | struct devfreq *devfreq = to_devfreq(dev); |
| 747 | int err; |
| 748 | |
| 749 | mutex_lock(&devfreq_list_lock); |
| 750 | list_del(entry: &devfreq->node); |
| 751 | mutex_unlock(lock: &devfreq_list_lock); |
| 752 | |
| 753 | err = dev_pm_qos_remove_notifier(dev: devfreq->dev.parent, notifier: &devfreq->nb_max, |
| 754 | type: DEV_PM_QOS_MAX_FREQUENCY); |
| 755 | if (err && err != -ENOENT) |
| 756 | dev_warn(dev->parent, |
| 757 | "Failed to remove max_freq notifier: %d\n" , err); |
| 758 | err = dev_pm_qos_remove_notifier(dev: devfreq->dev.parent, notifier: &devfreq->nb_min, |
| 759 | type: DEV_PM_QOS_MIN_FREQUENCY); |
| 760 | if (err && err != -ENOENT) |
| 761 | dev_warn(dev->parent, |
| 762 | "Failed to remove min_freq notifier: %d\n" , err); |
| 763 | |
| 764 | if (dev_pm_qos_request_active(req: &devfreq->user_max_freq_req)) { |
| 765 | err = dev_pm_qos_remove_request(req: &devfreq->user_max_freq_req); |
| 766 | if (err < 0) |
| 767 | dev_warn(dev->parent, |
| 768 | "Failed to remove max_freq request: %d\n" , err); |
| 769 | } |
| 770 | if (dev_pm_qos_request_active(req: &devfreq->user_min_freq_req)) { |
| 771 | err = dev_pm_qos_remove_request(req: &devfreq->user_min_freq_req); |
| 772 | if (err < 0) |
| 773 | dev_warn(dev->parent, |
| 774 | "Failed to remove min_freq request: %d\n" , err); |
| 775 | } |
| 776 | |
| 777 | if (devfreq->profile->exit) |
| 778 | devfreq->profile->exit(devfreq->dev.parent); |
| 779 | |
| 780 | if (devfreq->opp_table) |
| 781 | dev_pm_opp_put_opp_table(opp_table: devfreq->opp_table); |
| 782 | |
| 783 | mutex_destroy(lock: &devfreq->lock); |
| 784 | srcu_cleanup_notifier_head(&devfreq->transition_notifier_list); |
| 785 | kfree(objp: devfreq); |
| 786 | } |
| 787 | |
| 788 | static void create_sysfs_files(struct devfreq *devfreq, |
| 789 | const struct devfreq_governor *gov); |
| 790 | static void remove_sysfs_files(struct devfreq *devfreq, |
| 791 | const struct devfreq_governor *gov); |
| 792 | |
| 793 | /** |
| 794 | * devfreq_add_device() - Add devfreq feature to the device |
| 795 | * @dev: the device to add devfreq feature. |
| 796 | * @profile: device-specific profile to run devfreq. |
| 797 | * @governor_name: name of the policy to choose frequency. |
| 798 | * @data: devfreq driver pass to governors, governor should not change it. |
| 799 | */ |
| 800 | struct devfreq *devfreq_add_device(struct device *dev, |
| 801 | struct devfreq_dev_profile *profile, |
| 802 | const char *governor_name, |
| 803 | void *data) |
| 804 | { |
| 805 | struct devfreq *devfreq; |
| 806 | struct devfreq_governor *governor; |
| 807 | int err = 0; |
| 808 | |
| 809 | if (!dev || !profile || !governor_name) { |
| 810 | dev_err(dev, "%s: Invalid parameters.\n" , __func__); |
| 811 | return ERR_PTR(error: -EINVAL); |
| 812 | } |
| 813 | |
| 814 | mutex_lock(&devfreq_list_lock); |
| 815 | devfreq = find_device_devfreq(dev); |
| 816 | mutex_unlock(lock: &devfreq_list_lock); |
| 817 | if (!IS_ERR(ptr: devfreq)) { |
| 818 | dev_err(dev, "%s: devfreq device already exists!\n" , |
| 819 | __func__); |
| 820 | err = -EINVAL; |
| 821 | goto err_out; |
| 822 | } |
| 823 | |
| 824 | devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL); |
| 825 | if (!devfreq) { |
| 826 | err = -ENOMEM; |
| 827 | goto err_out; |
| 828 | } |
| 829 | |
| 830 | mutex_init(&devfreq->lock); |
| 831 | mutex_lock(&devfreq->lock); |
| 832 | devfreq->dev.parent = dev; |
| 833 | devfreq->dev.class = devfreq_class; |
| 834 | devfreq->dev.groups = profile->dev_groups; |
| 835 | devfreq->dev.release = devfreq_dev_release; |
| 836 | INIT_LIST_HEAD(list: &devfreq->node); |
| 837 | devfreq->profile = profile; |
| 838 | devfreq->previous_freq = profile->initial_freq; |
| 839 | devfreq->last_status.current_frequency = profile->initial_freq; |
| 840 | devfreq->data = data; |
| 841 | devfreq->nb.notifier_call = devfreq_notifier_call; |
| 842 | |
| 843 | if (devfreq->profile->timer < 0 |
| 844 | || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) { |
| 845 | mutex_unlock(lock: &devfreq->lock); |
| 846 | err = -EINVAL; |
| 847 | goto err_dev; |
| 848 | } |
| 849 | |
| 850 | if (!devfreq->profile->max_state || !devfreq->profile->freq_table) { |
| 851 | mutex_unlock(lock: &devfreq->lock); |
| 852 | err = set_freq_table(devfreq); |
| 853 | if (err < 0) |
| 854 | goto err_dev; |
| 855 | mutex_lock(&devfreq->lock); |
| 856 | } else { |
| 857 | devfreq->freq_table = devfreq->profile->freq_table; |
| 858 | devfreq->max_state = devfreq->profile->max_state; |
| 859 | } |
| 860 | |
| 861 | devfreq->scaling_min_freq = find_available_min_freq(devfreq); |
| 862 | if (!devfreq->scaling_min_freq) { |
| 863 | mutex_unlock(lock: &devfreq->lock); |
| 864 | err = -EINVAL; |
| 865 | goto err_dev; |
| 866 | } |
| 867 | |
| 868 | devfreq->scaling_max_freq = find_available_max_freq(devfreq); |
| 869 | if (!devfreq->scaling_max_freq) { |
| 870 | mutex_unlock(lock: &devfreq->lock); |
| 871 | err = -EINVAL; |
| 872 | goto err_dev; |
| 873 | } |
| 874 | |
| 875 | devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev); |
| 876 | devfreq->opp_table = dev_pm_opp_get_opp_table(dev); |
| 877 | if (IS_ERR(ptr: devfreq->opp_table)) |
| 878 | devfreq->opp_table = NULL; |
| 879 | |
| 880 | atomic_set(v: &devfreq->suspend_count, i: 0); |
| 881 | |
| 882 | dev_set_name(dev: &devfreq->dev, name: "%s" , dev_name(dev)); |
| 883 | err = device_register(dev: &devfreq->dev); |
| 884 | if (err) { |
| 885 | mutex_unlock(lock: &devfreq->lock); |
| 886 | put_device(dev: &devfreq->dev); |
| 887 | goto err_out; |
| 888 | } |
| 889 | |
| 890 | devfreq->stats.trans_table = devm_kzalloc(dev: &devfreq->dev, |
| 891 | array3_size(sizeof(unsigned int), |
| 892 | devfreq->max_state, |
| 893 | devfreq->max_state), |
| 894 | GFP_KERNEL); |
| 895 | if (!devfreq->stats.trans_table) { |
| 896 | mutex_unlock(lock: &devfreq->lock); |
| 897 | err = -ENOMEM; |
| 898 | goto err_devfreq; |
| 899 | } |
| 900 | |
| 901 | devfreq->stats.time_in_state = devm_kcalloc(dev: &devfreq->dev, |
| 902 | n: devfreq->max_state, |
| 903 | size: sizeof(*devfreq->stats.time_in_state), |
| 904 | GFP_KERNEL); |
| 905 | if (!devfreq->stats.time_in_state) { |
| 906 | mutex_unlock(lock: &devfreq->lock); |
| 907 | err = -ENOMEM; |
| 908 | goto err_devfreq; |
| 909 | } |
| 910 | |
| 911 | devfreq->stats.total_trans = 0; |
| 912 | devfreq->stats.last_update = get_jiffies_64(); |
| 913 | |
| 914 | srcu_init_notifier_head(nh: &devfreq->transition_notifier_list); |
| 915 | |
| 916 | mutex_unlock(lock: &devfreq->lock); |
| 917 | |
| 918 | err = dev_pm_qos_add_request(dev, req: &devfreq->user_min_freq_req, |
| 919 | type: DEV_PM_QOS_MIN_FREQUENCY, value: 0); |
| 920 | if (err < 0) |
| 921 | goto err_devfreq; |
| 922 | err = dev_pm_qos_add_request(dev, req: &devfreq->user_max_freq_req, |
| 923 | type: DEV_PM_QOS_MAX_FREQUENCY, |
| 924 | PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE); |
| 925 | if (err < 0) |
| 926 | goto err_devfreq; |
| 927 | |
| 928 | devfreq->nb_min.notifier_call = qos_min_notifier_call; |
| 929 | err = dev_pm_qos_add_notifier(dev, notifier: &devfreq->nb_min, |
| 930 | type: DEV_PM_QOS_MIN_FREQUENCY); |
| 931 | if (err) |
| 932 | goto err_devfreq; |
| 933 | |
| 934 | devfreq->nb_max.notifier_call = qos_max_notifier_call; |
| 935 | err = dev_pm_qos_add_notifier(dev, notifier: &devfreq->nb_max, |
| 936 | type: DEV_PM_QOS_MAX_FREQUENCY); |
| 937 | if (err) |
| 938 | goto err_devfreq; |
| 939 | |
| 940 | mutex_lock(&devfreq_list_lock); |
| 941 | |
| 942 | governor = try_then_request_governor(name: governor_name); |
| 943 | if (IS_ERR(ptr: governor)) { |
| 944 | dev_err(dev, "%s: Unable to find governor for the device\n" , |
| 945 | __func__); |
| 946 | err = PTR_ERR(ptr: governor); |
| 947 | goto err_init; |
| 948 | } |
| 949 | |
| 950 | devfreq->governor = governor; |
| 951 | err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START, |
| 952 | NULL); |
| 953 | if (err) { |
| 954 | dev_err_probe(dev, err, |
| 955 | fmt: "%s: Unable to start governor for the device\n" , |
| 956 | __func__); |
| 957 | goto err_init; |
| 958 | } |
| 959 | create_sysfs_files(devfreq, gov: devfreq->governor); |
| 960 | |
| 961 | list_add(new: &devfreq->node, head: &devfreq_list); |
| 962 | |
| 963 | mutex_unlock(lock: &devfreq_list_lock); |
| 964 | |
| 965 | if (devfreq->profile->is_cooling_device) { |
| 966 | devfreq->cdev = devfreq_cooling_em_register(df: devfreq, NULL); |
| 967 | if (IS_ERR(ptr: devfreq->cdev)) |
| 968 | devfreq->cdev = NULL; |
| 969 | } |
| 970 | |
| 971 | return devfreq; |
| 972 | |
| 973 | err_init: |
| 974 | mutex_unlock(lock: &devfreq_list_lock); |
| 975 | err_devfreq: |
| 976 | devfreq_remove_device(devfreq); |
| 977 | devfreq = NULL; |
| 978 | err_dev: |
| 979 | kfree(objp: devfreq); |
| 980 | err_out: |
| 981 | return ERR_PTR(error: err); |
| 982 | } |
| 983 | EXPORT_SYMBOL(devfreq_add_device); |
| 984 | |
| 985 | /** |
| 986 | * devfreq_remove_device() - Remove devfreq feature from a device. |
| 987 | * @devfreq: the devfreq instance to be removed |
| 988 | * |
| 989 | * The opposite of devfreq_add_device(). |
| 990 | */ |
| 991 | int devfreq_remove_device(struct devfreq *devfreq) |
| 992 | { |
| 993 | if (!devfreq) |
| 994 | return -EINVAL; |
| 995 | |
| 996 | devfreq_cooling_unregister(dfc: devfreq->cdev); |
| 997 | |
| 998 | if (devfreq->governor) { |
| 999 | devfreq->governor->event_handler(devfreq, |
| 1000 | DEVFREQ_GOV_STOP, NULL); |
| 1001 | remove_sysfs_files(devfreq, gov: devfreq->governor); |
| 1002 | } |
| 1003 | |
| 1004 | device_unregister(dev: &devfreq->dev); |
| 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | EXPORT_SYMBOL(devfreq_remove_device); |
| 1009 | |
| 1010 | static int devm_devfreq_dev_match(struct device *dev, void *res, void *data) |
| 1011 | { |
| 1012 | struct devfreq **r = res; |
| 1013 | |
| 1014 | if (WARN_ON(!r || !*r)) |
| 1015 | return 0; |
| 1016 | |
| 1017 | return *r == data; |
| 1018 | } |
| 1019 | |
| 1020 | static void devm_devfreq_dev_release(struct device *dev, void *res) |
| 1021 | { |
| 1022 | devfreq_remove_device(*(struct devfreq **)res); |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * devm_devfreq_add_device() - Resource-managed devfreq_add_device() |
| 1027 | * @dev: the device to add devfreq feature. |
| 1028 | * @profile: device-specific profile to run devfreq. |
| 1029 | * @governor_name: name of the policy to choose frequency. |
| 1030 | * @data: devfreq driver pass to governors, governor should not change it. |
| 1031 | * |
| 1032 | * This function manages automatically the memory of devfreq device using device |
| 1033 | * resource management and simplify the free operation for memory of devfreq |
| 1034 | * device. |
| 1035 | */ |
| 1036 | struct devfreq *devm_devfreq_add_device(struct device *dev, |
| 1037 | struct devfreq_dev_profile *profile, |
| 1038 | const char *governor_name, |
| 1039 | void *data) |
| 1040 | { |
| 1041 | struct devfreq **ptr, *devfreq; |
| 1042 | |
| 1043 | ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL); |
| 1044 | if (!ptr) |
| 1045 | return ERR_PTR(error: -ENOMEM); |
| 1046 | |
| 1047 | devfreq = devfreq_add_device(dev, profile, governor_name, data); |
| 1048 | if (IS_ERR(ptr: devfreq)) { |
| 1049 | devres_free(res: ptr); |
| 1050 | return devfreq; |
| 1051 | } |
| 1052 | |
| 1053 | *ptr = devfreq; |
| 1054 | devres_add(dev, res: ptr); |
| 1055 | |
| 1056 | return devfreq; |
| 1057 | } |
| 1058 | EXPORT_SYMBOL(devm_devfreq_add_device); |
| 1059 | |
| 1060 | #ifdef CONFIG_OF |
| 1061 | /* |
| 1062 | * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree |
| 1063 | * @node - pointer to device_node |
| 1064 | * |
| 1065 | * return the instance of devfreq device |
| 1066 | */ |
| 1067 | struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node) |
| 1068 | { |
| 1069 | struct devfreq *devfreq; |
| 1070 | |
| 1071 | if (!node) |
| 1072 | return ERR_PTR(error: -EINVAL); |
| 1073 | |
| 1074 | mutex_lock(&devfreq_list_lock); |
| 1075 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 1076 | if (devfreq->dev.parent |
| 1077 | && device_match_of_node(dev: devfreq->dev.parent, np: node)) { |
| 1078 | mutex_unlock(lock: &devfreq_list_lock); |
| 1079 | return devfreq; |
| 1080 | } |
| 1081 | } |
| 1082 | mutex_unlock(lock: &devfreq_list_lock); |
| 1083 | |
| 1084 | return ERR_PTR(error: -ENODEV); |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree |
| 1089 | * @dev - instance to the given device |
| 1090 | * @phandle_name - name of property holding a phandle value |
| 1091 | * @index - index into list of devfreq |
| 1092 | * |
| 1093 | * return the instance of devfreq device |
| 1094 | */ |
| 1095 | struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, |
| 1096 | const char *phandle_name, int index) |
| 1097 | { |
| 1098 | struct device_node *node; |
| 1099 | struct devfreq *devfreq; |
| 1100 | |
| 1101 | if (!dev || !phandle_name) |
| 1102 | return ERR_PTR(error: -EINVAL); |
| 1103 | |
| 1104 | if (!dev->of_node) |
| 1105 | return ERR_PTR(error: -EINVAL); |
| 1106 | |
| 1107 | node = of_parse_phandle(np: dev->of_node, phandle_name, index); |
| 1108 | if (!node) |
| 1109 | return ERR_PTR(error: -ENODEV); |
| 1110 | |
| 1111 | devfreq = devfreq_get_devfreq_by_node(node); |
| 1112 | of_node_put(node); |
| 1113 | |
| 1114 | return devfreq; |
| 1115 | } |
| 1116 | |
| 1117 | #else |
| 1118 | struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node) |
| 1119 | { |
| 1120 | return ERR_PTR(-ENODEV); |
| 1121 | } |
| 1122 | |
| 1123 | struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, |
| 1124 | const char *phandle_name, int index) |
| 1125 | { |
| 1126 | return ERR_PTR(-ENODEV); |
| 1127 | } |
| 1128 | #endif /* CONFIG_OF */ |
| 1129 | EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node); |
| 1130 | EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle); |
| 1131 | |
| 1132 | /** |
| 1133 | * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device() |
| 1134 | * @dev: the device from which to remove devfreq feature. |
| 1135 | * @devfreq: the devfreq instance to be removed |
| 1136 | */ |
| 1137 | void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq) |
| 1138 | { |
| 1139 | WARN_ON(devres_release(dev, devm_devfreq_dev_release, |
| 1140 | devm_devfreq_dev_match, devfreq)); |
| 1141 | } |
| 1142 | EXPORT_SYMBOL(devm_devfreq_remove_device); |
| 1143 | |
| 1144 | /** |
| 1145 | * devfreq_suspend_device() - Suspend devfreq of a device. |
| 1146 | * @devfreq: the devfreq instance to be suspended |
| 1147 | * |
| 1148 | * This function is intended to be called by the pm callbacks |
| 1149 | * (e.g., runtime_suspend, suspend) of the device driver that |
| 1150 | * holds the devfreq. |
| 1151 | */ |
| 1152 | int devfreq_suspend_device(struct devfreq *devfreq) |
| 1153 | { |
| 1154 | int ret; |
| 1155 | |
| 1156 | if (!devfreq) |
| 1157 | return -EINVAL; |
| 1158 | |
| 1159 | if (atomic_inc_return(v: &devfreq->suspend_count) > 1) |
| 1160 | return 0; |
| 1161 | |
| 1162 | if (devfreq->governor) { |
| 1163 | ret = devfreq->governor->event_handler(devfreq, |
| 1164 | DEVFREQ_GOV_SUSPEND, NULL); |
| 1165 | if (ret) |
| 1166 | return ret; |
| 1167 | } |
| 1168 | |
| 1169 | if (devfreq->suspend_freq) { |
| 1170 | mutex_lock(&devfreq->lock); |
| 1171 | ret = devfreq_set_target(devfreq, new_freq: devfreq->suspend_freq, flags: 0); |
| 1172 | mutex_unlock(lock: &devfreq->lock); |
| 1173 | if (ret) |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | EXPORT_SYMBOL(devfreq_suspend_device); |
| 1180 | |
| 1181 | /** |
| 1182 | * devfreq_resume_device() - Resume devfreq of a device. |
| 1183 | * @devfreq: the devfreq instance to be resumed |
| 1184 | * |
| 1185 | * This function is intended to be called by the pm callbacks |
| 1186 | * (e.g., runtime_resume, resume) of the device driver that |
| 1187 | * holds the devfreq. |
| 1188 | */ |
| 1189 | int devfreq_resume_device(struct devfreq *devfreq) |
| 1190 | { |
| 1191 | int ret; |
| 1192 | |
| 1193 | if (!devfreq) |
| 1194 | return -EINVAL; |
| 1195 | |
| 1196 | if (atomic_dec_return(v: &devfreq->suspend_count) >= 1) |
| 1197 | return 0; |
| 1198 | |
| 1199 | if (devfreq->resume_freq) { |
| 1200 | mutex_lock(&devfreq->lock); |
| 1201 | ret = devfreq_set_target(devfreq, new_freq: devfreq->resume_freq, flags: 0); |
| 1202 | mutex_unlock(lock: &devfreq->lock); |
| 1203 | if (ret) |
| 1204 | return ret; |
| 1205 | } |
| 1206 | |
| 1207 | if (devfreq->governor) { |
| 1208 | ret = devfreq->governor->event_handler(devfreq, |
| 1209 | DEVFREQ_GOV_RESUME, NULL); |
| 1210 | if (ret) |
| 1211 | return ret; |
| 1212 | } |
| 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | EXPORT_SYMBOL(devfreq_resume_device); |
| 1217 | |
| 1218 | /** |
| 1219 | * devfreq_suspend() - Suspend devfreq governors and devices |
| 1220 | * |
| 1221 | * Called during system wide Suspend/Hibernate cycles for suspending governors |
| 1222 | * and devices preserving the state for resume. On some platforms the devfreq |
| 1223 | * device must have precise state (frequency) after resume in order to provide |
| 1224 | * fully operating setup. |
| 1225 | */ |
| 1226 | void devfreq_suspend(void) |
| 1227 | { |
| 1228 | struct devfreq *devfreq; |
| 1229 | int ret; |
| 1230 | |
| 1231 | mutex_lock(&devfreq_list_lock); |
| 1232 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 1233 | ret = devfreq_suspend_device(devfreq); |
| 1234 | if (ret) |
| 1235 | dev_err(&devfreq->dev, |
| 1236 | "failed to suspend devfreq device\n" ); |
| 1237 | } |
| 1238 | mutex_unlock(lock: &devfreq_list_lock); |
| 1239 | } |
| 1240 | |
| 1241 | /** |
| 1242 | * devfreq_resume() - Resume devfreq governors and devices |
| 1243 | * |
| 1244 | * Called during system wide Suspend/Hibernate cycle for resuming governors and |
| 1245 | * devices that are suspended with devfreq_suspend(). |
| 1246 | */ |
| 1247 | void devfreq_resume(void) |
| 1248 | { |
| 1249 | struct devfreq *devfreq; |
| 1250 | int ret; |
| 1251 | |
| 1252 | mutex_lock(&devfreq_list_lock); |
| 1253 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 1254 | ret = devfreq_resume_device(devfreq); |
| 1255 | if (ret) |
| 1256 | dev_warn(&devfreq->dev, |
| 1257 | "failed to resume devfreq device\n" ); |
| 1258 | } |
| 1259 | mutex_unlock(lock: &devfreq_list_lock); |
| 1260 | } |
| 1261 | |
| 1262 | /** |
| 1263 | * devfreq_add_governor() - Add devfreq governor |
| 1264 | * @governor: the devfreq governor to be added |
| 1265 | */ |
| 1266 | int devfreq_add_governor(struct devfreq_governor *governor) |
| 1267 | { |
| 1268 | struct devfreq_governor *g; |
| 1269 | struct devfreq *devfreq; |
| 1270 | int err = 0; |
| 1271 | |
| 1272 | if (!governor) { |
| 1273 | pr_err("%s: Invalid parameters.\n" , __func__); |
| 1274 | return -EINVAL; |
| 1275 | } |
| 1276 | |
| 1277 | mutex_lock(&devfreq_list_lock); |
| 1278 | g = find_devfreq_governor(name: governor->name); |
| 1279 | if (!IS_ERR(ptr: g)) { |
| 1280 | pr_err("%s: governor %s already registered\n" , __func__, |
| 1281 | g->name); |
| 1282 | err = -EINVAL; |
| 1283 | goto err_out; |
| 1284 | } |
| 1285 | |
| 1286 | list_add(new: &governor->node, head: &devfreq_governor_list); |
| 1287 | |
| 1288 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 1289 | int ret = 0; |
| 1290 | struct device *dev = devfreq->dev.parent; |
| 1291 | |
| 1292 | if (!strncmp(devfreq->governor->name, governor->name, |
| 1293 | DEVFREQ_NAME_LEN)) { |
| 1294 | /* The following should never occur */ |
| 1295 | if (devfreq->governor) { |
| 1296 | dev_warn(dev, |
| 1297 | "%s: Governor %s already present\n" , |
| 1298 | __func__, devfreq->governor->name); |
| 1299 | ret = devfreq->governor->event_handler(devfreq, |
| 1300 | DEVFREQ_GOV_STOP, NULL); |
| 1301 | if (ret) { |
| 1302 | dev_warn(dev, |
| 1303 | "%s: Governor %s stop = %d\n" , |
| 1304 | __func__, |
| 1305 | devfreq->governor->name, ret); |
| 1306 | } |
| 1307 | /* Fall through */ |
| 1308 | } |
| 1309 | devfreq->governor = governor; |
| 1310 | ret = devfreq->governor->event_handler(devfreq, |
| 1311 | DEVFREQ_GOV_START, NULL); |
| 1312 | if (ret) { |
| 1313 | dev_warn(dev, "%s: Governor %s start=%d\n" , |
| 1314 | __func__, devfreq->governor->name, |
| 1315 | ret); |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | err_out: |
| 1321 | mutex_unlock(lock: &devfreq_list_lock); |
| 1322 | |
| 1323 | return err; |
| 1324 | } |
| 1325 | EXPORT_SYMBOL(devfreq_add_governor); |
| 1326 | |
| 1327 | static void devm_devfreq_remove_governor(void *governor) |
| 1328 | { |
| 1329 | WARN_ON(devfreq_remove_governor(governor)); |
| 1330 | } |
| 1331 | |
| 1332 | /** |
| 1333 | * devm_devfreq_add_governor() - Add devfreq governor |
| 1334 | * @dev: device which adds devfreq governor |
| 1335 | * @governor: the devfreq governor to be added |
| 1336 | * |
| 1337 | * This is a resource-managed variant of devfreq_add_governor(). |
| 1338 | */ |
| 1339 | int devm_devfreq_add_governor(struct device *dev, |
| 1340 | struct devfreq_governor *governor) |
| 1341 | { |
| 1342 | int err; |
| 1343 | |
| 1344 | err = devfreq_add_governor(governor); |
| 1345 | if (err) |
| 1346 | return err; |
| 1347 | |
| 1348 | return devm_add_action_or_reset(dev, devm_devfreq_remove_governor, |
| 1349 | governor); |
| 1350 | } |
| 1351 | EXPORT_SYMBOL(devm_devfreq_add_governor); |
| 1352 | |
| 1353 | /** |
| 1354 | * devfreq_remove_governor() - Remove devfreq feature from a device. |
| 1355 | * @governor: the devfreq governor to be removed |
| 1356 | */ |
| 1357 | int devfreq_remove_governor(struct devfreq_governor *governor) |
| 1358 | { |
| 1359 | struct devfreq_governor *g; |
| 1360 | struct devfreq *devfreq; |
| 1361 | int err = 0; |
| 1362 | |
| 1363 | if (!governor) { |
| 1364 | pr_err("%s: Invalid parameters.\n" , __func__); |
| 1365 | return -EINVAL; |
| 1366 | } |
| 1367 | |
| 1368 | mutex_lock(&devfreq_list_lock); |
| 1369 | g = find_devfreq_governor(name: governor->name); |
| 1370 | if (IS_ERR(ptr: g)) { |
| 1371 | pr_err("%s: governor %s not registered\n" , __func__, |
| 1372 | governor->name); |
| 1373 | err = PTR_ERR(ptr: g); |
| 1374 | goto err_out; |
| 1375 | } |
| 1376 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 1377 | int ret; |
| 1378 | struct device *dev = devfreq->dev.parent; |
| 1379 | |
| 1380 | if (!devfreq->governor) |
| 1381 | continue; |
| 1382 | |
| 1383 | if (!strncmp(devfreq->governor->name, governor->name, |
| 1384 | DEVFREQ_NAME_LEN)) { |
| 1385 | ret = devfreq->governor->event_handler(devfreq, |
| 1386 | DEVFREQ_GOV_STOP, NULL); |
| 1387 | if (ret) { |
| 1388 | dev_warn(dev, "%s: Governor %s stop=%d\n" , |
| 1389 | __func__, devfreq->governor->name, |
| 1390 | ret); |
| 1391 | } |
| 1392 | devfreq->governor = NULL; |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | list_del(entry: &governor->node); |
| 1397 | err_out: |
| 1398 | mutex_unlock(lock: &devfreq_list_lock); |
| 1399 | |
| 1400 | return err; |
| 1401 | } |
| 1402 | EXPORT_SYMBOL(devfreq_remove_governor); |
| 1403 | |
| 1404 | static ssize_t name_show(struct device *dev, |
| 1405 | struct device_attribute *attr, char *buf) |
| 1406 | { |
| 1407 | struct devfreq *df = to_devfreq(dev); |
| 1408 | return sprintf(buf, fmt: "%s\n" , dev_name(dev: df->dev.parent)); |
| 1409 | } |
| 1410 | static DEVICE_ATTR_RO(name); |
| 1411 | |
| 1412 | static ssize_t governor_show(struct device *dev, |
| 1413 | struct device_attribute *attr, char *buf) |
| 1414 | { |
| 1415 | struct devfreq *df = to_devfreq(dev); |
| 1416 | |
| 1417 | if (!df->governor) |
| 1418 | return -EINVAL; |
| 1419 | |
| 1420 | return sprintf(buf, fmt: "%s\n" , df->governor->name); |
| 1421 | } |
| 1422 | |
| 1423 | static ssize_t governor_store(struct device *dev, struct device_attribute *attr, |
| 1424 | const char *buf, size_t count) |
| 1425 | { |
| 1426 | struct devfreq *df = to_devfreq(dev); |
| 1427 | int ret; |
| 1428 | char str_governor[DEVFREQ_NAME_LEN + 1]; |
| 1429 | const struct devfreq_governor *governor, *prev_governor; |
| 1430 | |
| 1431 | if (!df->governor) |
| 1432 | return -EINVAL; |
| 1433 | |
| 1434 | ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s" , str_governor); |
| 1435 | if (ret != 1) |
| 1436 | return -EINVAL; |
| 1437 | |
| 1438 | mutex_lock(&devfreq_list_lock); |
| 1439 | governor = try_then_request_governor(name: str_governor); |
| 1440 | if (IS_ERR(ptr: governor)) { |
| 1441 | ret = PTR_ERR(ptr: governor); |
| 1442 | goto out; |
| 1443 | } |
| 1444 | if (df->governor == governor) { |
| 1445 | ret = 0; |
| 1446 | goto out; |
| 1447 | } else if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE) |
| 1448 | || IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) { |
| 1449 | ret = -EINVAL; |
| 1450 | goto out; |
| 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | * Stop the current governor and remove the specific sysfs files |
| 1455 | * which depend on current governor. |
| 1456 | */ |
| 1457 | ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL); |
| 1458 | if (ret) { |
| 1459 | dev_warn(dev, "%s: Governor %s not stopped(%d)\n" , |
| 1460 | __func__, df->governor->name, ret); |
| 1461 | goto out; |
| 1462 | } |
| 1463 | remove_sysfs_files(devfreq: df, gov: df->governor); |
| 1464 | |
| 1465 | /* |
| 1466 | * Start the new governor and create the specific sysfs files |
| 1467 | * which depend on the new governor. |
| 1468 | */ |
| 1469 | prev_governor = df->governor; |
| 1470 | df->governor = governor; |
| 1471 | ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); |
| 1472 | if (ret) { |
| 1473 | dev_warn(dev, "%s: Governor %s not started(%d)\n" , |
| 1474 | __func__, df->governor->name, ret); |
| 1475 | |
| 1476 | /* Restore previous governor */ |
| 1477 | df->governor = prev_governor; |
| 1478 | ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); |
| 1479 | if (ret) { |
| 1480 | dev_err(dev, |
| 1481 | "%s: reverting to Governor %s failed (%d)\n" , |
| 1482 | __func__, prev_governor->name, ret); |
| 1483 | df->governor = NULL; |
| 1484 | goto out; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * Create the sysfs files for the new governor. But if failed to start |
| 1490 | * the new governor, restore the sysfs files of previous governor. |
| 1491 | */ |
| 1492 | create_sysfs_files(devfreq: df, gov: df->governor); |
| 1493 | |
| 1494 | out: |
| 1495 | mutex_unlock(lock: &devfreq_list_lock); |
| 1496 | |
| 1497 | if (!ret) |
| 1498 | ret = count; |
| 1499 | return ret; |
| 1500 | } |
| 1501 | static DEVICE_ATTR_RW(governor); |
| 1502 | |
| 1503 | static ssize_t available_governors_show(struct device *d, |
| 1504 | struct device_attribute *attr, |
| 1505 | char *buf) |
| 1506 | { |
| 1507 | struct devfreq *df = to_devfreq(d); |
| 1508 | ssize_t count = 0; |
| 1509 | |
| 1510 | if (!df->governor) |
| 1511 | return -EINVAL; |
| 1512 | |
| 1513 | mutex_lock(&devfreq_list_lock); |
| 1514 | |
| 1515 | /* |
| 1516 | * The devfreq with immutable governor (e.g., passive) shows |
| 1517 | * only own governor. |
| 1518 | */ |
| 1519 | if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)) { |
| 1520 | count = scnprintf(buf: &buf[count], DEVFREQ_NAME_LEN, |
| 1521 | fmt: "%s " , df->governor->name); |
| 1522 | /* |
| 1523 | * The devfreq device shows the registered governor except for |
| 1524 | * immutable governors such as passive governor . |
| 1525 | */ |
| 1526 | } else { |
| 1527 | struct devfreq_governor *governor; |
| 1528 | |
| 1529 | list_for_each_entry(governor, &devfreq_governor_list, node) { |
| 1530 | if (IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) |
| 1531 | continue; |
| 1532 | count += scnprintf(buf: &buf[count], size: (PAGE_SIZE - count - 2), |
| 1533 | fmt: "%s " , governor->name); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | mutex_unlock(lock: &devfreq_list_lock); |
| 1538 | |
| 1539 | /* Truncate the trailing space */ |
| 1540 | if (count) |
| 1541 | count--; |
| 1542 | |
| 1543 | count += sprintf(buf: &buf[count], fmt: "\n" ); |
| 1544 | |
| 1545 | return count; |
| 1546 | } |
| 1547 | static DEVICE_ATTR_RO(available_governors); |
| 1548 | |
| 1549 | static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr, |
| 1550 | char *buf) |
| 1551 | { |
| 1552 | unsigned long freq; |
| 1553 | struct devfreq *df = to_devfreq(dev); |
| 1554 | |
| 1555 | if (!df->profile) |
| 1556 | return -EINVAL; |
| 1557 | |
| 1558 | if (df->profile->get_cur_freq && |
| 1559 | !df->profile->get_cur_freq(df->dev.parent, &freq)) |
| 1560 | return sprintf(buf, fmt: "%lu\n" , freq); |
| 1561 | |
| 1562 | return sprintf(buf, fmt: "%lu\n" , df->previous_freq); |
| 1563 | } |
| 1564 | static DEVICE_ATTR_RO(cur_freq); |
| 1565 | |
| 1566 | static ssize_t target_freq_show(struct device *dev, |
| 1567 | struct device_attribute *attr, char *buf) |
| 1568 | { |
| 1569 | struct devfreq *df = to_devfreq(dev); |
| 1570 | |
| 1571 | return sprintf(buf, fmt: "%lu\n" , df->previous_freq); |
| 1572 | } |
| 1573 | static DEVICE_ATTR_RO(target_freq); |
| 1574 | |
| 1575 | static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr, |
| 1576 | const char *buf, size_t count) |
| 1577 | { |
| 1578 | struct devfreq *df = to_devfreq(dev); |
| 1579 | unsigned long value; |
| 1580 | int ret; |
| 1581 | |
| 1582 | /* |
| 1583 | * Protect against theoretical sysfs writes between |
| 1584 | * device_add and dev_pm_qos_add_request |
| 1585 | */ |
| 1586 | if (!dev_pm_qos_request_active(req: &df->user_min_freq_req)) |
| 1587 | return -EAGAIN; |
| 1588 | |
| 1589 | ret = sscanf(buf, "%lu" , &value); |
| 1590 | if (ret != 1) |
| 1591 | return -EINVAL; |
| 1592 | |
| 1593 | /* Round down to kHz for PM QoS */ |
| 1594 | ret = dev_pm_qos_update_request(req: &df->user_min_freq_req, |
| 1595 | new_value: value / HZ_PER_KHZ); |
| 1596 | if (ret < 0) |
| 1597 | return ret; |
| 1598 | |
| 1599 | return count; |
| 1600 | } |
| 1601 | |
| 1602 | static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr, |
| 1603 | char *buf) |
| 1604 | { |
| 1605 | struct devfreq *df = to_devfreq(dev); |
| 1606 | unsigned long min_freq, max_freq; |
| 1607 | |
| 1608 | mutex_lock(&df->lock); |
| 1609 | devfreq_get_freq_range(df, &min_freq, &max_freq); |
| 1610 | mutex_unlock(lock: &df->lock); |
| 1611 | |
| 1612 | return sprintf(buf, fmt: "%lu\n" , min_freq); |
| 1613 | } |
| 1614 | static DEVICE_ATTR_RW(min_freq); |
| 1615 | |
| 1616 | static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr, |
| 1617 | const char *buf, size_t count) |
| 1618 | { |
| 1619 | struct devfreq *df = to_devfreq(dev); |
| 1620 | unsigned long value; |
| 1621 | int ret; |
| 1622 | |
| 1623 | /* |
| 1624 | * Protect against theoretical sysfs writes between |
| 1625 | * device_add and dev_pm_qos_add_request |
| 1626 | */ |
| 1627 | if (!dev_pm_qos_request_active(req: &df->user_max_freq_req)) |
| 1628 | return -EINVAL; |
| 1629 | |
| 1630 | ret = sscanf(buf, "%lu" , &value); |
| 1631 | if (ret != 1) |
| 1632 | return -EINVAL; |
| 1633 | |
| 1634 | /* |
| 1635 | * PM QoS frequencies are in kHz so we need to convert. Convert by |
| 1636 | * rounding upwards so that the acceptable interval never shrinks. |
| 1637 | * |
| 1638 | * For example if the user writes "666666666" to sysfs this value will |
| 1639 | * be converted to 666667 kHz and back to 666667000 Hz before an OPP |
| 1640 | * lookup, this ensures that an OPP of 666666666Hz is still accepted. |
| 1641 | * |
| 1642 | * A value of zero means "no limit". |
| 1643 | */ |
| 1644 | if (value) |
| 1645 | value = DIV_ROUND_UP(value, HZ_PER_KHZ); |
| 1646 | else |
| 1647 | value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE; |
| 1648 | |
| 1649 | ret = dev_pm_qos_update_request(req: &df->user_max_freq_req, new_value: value); |
| 1650 | if (ret < 0) |
| 1651 | return ret; |
| 1652 | |
| 1653 | return count; |
| 1654 | } |
| 1655 | |
| 1656 | static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr, |
| 1657 | char *buf) |
| 1658 | { |
| 1659 | struct devfreq *df = to_devfreq(dev); |
| 1660 | unsigned long min_freq, max_freq; |
| 1661 | |
| 1662 | mutex_lock(&df->lock); |
| 1663 | devfreq_get_freq_range(df, &min_freq, &max_freq); |
| 1664 | mutex_unlock(lock: &df->lock); |
| 1665 | |
| 1666 | return sprintf(buf, fmt: "%lu\n" , max_freq); |
| 1667 | } |
| 1668 | static DEVICE_ATTR_RW(max_freq); |
| 1669 | |
| 1670 | static ssize_t available_frequencies_show(struct device *d, |
| 1671 | struct device_attribute *attr, |
| 1672 | char *buf) |
| 1673 | { |
| 1674 | struct devfreq *df = to_devfreq(d); |
| 1675 | ssize_t count = 0; |
| 1676 | int i; |
| 1677 | |
| 1678 | if (!df->profile) |
| 1679 | return -EINVAL; |
| 1680 | |
| 1681 | mutex_lock(&df->lock); |
| 1682 | |
| 1683 | for (i = 0; i < df->max_state; i++) |
| 1684 | count += scnprintf(buf: &buf[count], size: (PAGE_SIZE - count - 2), |
| 1685 | fmt: "%lu " , df->freq_table[i]); |
| 1686 | |
| 1687 | mutex_unlock(lock: &df->lock); |
| 1688 | /* Truncate the trailing space */ |
| 1689 | if (count) |
| 1690 | count--; |
| 1691 | |
| 1692 | count += sprintf(buf: &buf[count], fmt: "\n" ); |
| 1693 | |
| 1694 | return count; |
| 1695 | } |
| 1696 | static DEVICE_ATTR_RO(available_frequencies); |
| 1697 | |
| 1698 | static ssize_t trans_stat_show(struct device *dev, |
| 1699 | struct device_attribute *attr, char *buf) |
| 1700 | { |
| 1701 | struct devfreq *df = to_devfreq(dev); |
| 1702 | ssize_t len = 0; |
| 1703 | int i, j; |
| 1704 | unsigned int max_state; |
| 1705 | |
| 1706 | if (!df->profile) |
| 1707 | return -EINVAL; |
| 1708 | max_state = df->max_state; |
| 1709 | |
| 1710 | if (max_state == 0) |
| 1711 | return sysfs_emit(buf, fmt: "Not Supported.\n" ); |
| 1712 | |
| 1713 | mutex_lock(&df->lock); |
| 1714 | if (!df->stop_polling && |
| 1715 | devfreq_update_status(df, df->previous_freq)) { |
| 1716 | mutex_unlock(lock: &df->lock); |
| 1717 | return 0; |
| 1718 | } |
| 1719 | mutex_unlock(lock: &df->lock); |
| 1720 | |
| 1721 | len += sysfs_emit_at(buf, at: len, fmt: " From : To\n" ); |
| 1722 | len += sysfs_emit_at(buf, at: len, fmt: " :" ); |
| 1723 | for (i = 0; i < max_state; i++) { |
| 1724 | if (len >= PAGE_SIZE - 1) |
| 1725 | break; |
| 1726 | len += sysfs_emit_at(buf, at: len, fmt: "%10lu" , |
| 1727 | df->freq_table[i]); |
| 1728 | } |
| 1729 | |
| 1730 | if (len >= PAGE_SIZE - 1) |
| 1731 | return PAGE_SIZE - 1; |
| 1732 | len += sysfs_emit_at(buf, at: len, fmt: " time(ms)\n" ); |
| 1733 | |
| 1734 | for (i = 0; i < max_state; i++) { |
| 1735 | if (len >= PAGE_SIZE - 1) |
| 1736 | break; |
| 1737 | if (df->freq_table[i] == df->previous_freq) |
| 1738 | len += sysfs_emit_at(buf, at: len, fmt: "*" ); |
| 1739 | else |
| 1740 | len += sysfs_emit_at(buf, at: len, fmt: " " ); |
| 1741 | if (len >= PAGE_SIZE - 1) |
| 1742 | break; |
| 1743 | len += sysfs_emit_at(buf, at: len, fmt: "%10lu:" , df->freq_table[i]); |
| 1744 | for (j = 0; j < max_state; j++) { |
| 1745 | if (len >= PAGE_SIZE - 1) |
| 1746 | break; |
| 1747 | len += sysfs_emit_at(buf, at: len, fmt: "%10u" , |
| 1748 | df->stats.trans_table[(i * max_state) + j]); |
| 1749 | } |
| 1750 | if (len >= PAGE_SIZE - 1) |
| 1751 | break; |
| 1752 | len += sysfs_emit_at(buf, at: len, fmt: "%10llu\n" , (u64) |
| 1753 | jiffies64_to_msecs(j: df->stats.time_in_state[i])); |
| 1754 | } |
| 1755 | |
| 1756 | if (len < PAGE_SIZE - 1) |
| 1757 | len += sysfs_emit_at(buf, at: len, fmt: "Total transition : %u\n" , |
| 1758 | df->stats.total_trans); |
| 1759 | if (len >= PAGE_SIZE - 1) { |
| 1760 | pr_warn_once("devfreq transition table exceeds PAGE_SIZE. Disabling\n" ); |
| 1761 | return -EFBIG; |
| 1762 | } |
| 1763 | |
| 1764 | return len; |
| 1765 | } |
| 1766 | |
| 1767 | static ssize_t trans_stat_store(struct device *dev, |
| 1768 | struct device_attribute *attr, |
| 1769 | const char *buf, size_t count) |
| 1770 | { |
| 1771 | struct devfreq *df = to_devfreq(dev); |
| 1772 | int err, value; |
| 1773 | |
| 1774 | if (!df->profile) |
| 1775 | return -EINVAL; |
| 1776 | |
| 1777 | if (df->max_state == 0) |
| 1778 | return count; |
| 1779 | |
| 1780 | err = kstrtoint(s: buf, base: 10, res: &value); |
| 1781 | if (err || value != 0) |
| 1782 | return -EINVAL; |
| 1783 | |
| 1784 | mutex_lock(&df->lock); |
| 1785 | memset(df->stats.time_in_state, 0, (df->max_state * |
| 1786 | sizeof(*df->stats.time_in_state))); |
| 1787 | memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int), |
| 1788 | df->max_state, |
| 1789 | df->max_state)); |
| 1790 | df->stats.total_trans = 0; |
| 1791 | df->stats.last_update = get_jiffies_64(); |
| 1792 | mutex_unlock(lock: &df->lock); |
| 1793 | |
| 1794 | return count; |
| 1795 | } |
| 1796 | static DEVICE_ATTR_RW(trans_stat); |
| 1797 | |
| 1798 | static struct attribute *devfreq_attrs[] = { |
| 1799 | &dev_attr_name.attr, |
| 1800 | &dev_attr_governor.attr, |
| 1801 | &dev_attr_available_governors.attr, |
| 1802 | &dev_attr_cur_freq.attr, |
| 1803 | &dev_attr_available_frequencies.attr, |
| 1804 | &dev_attr_target_freq.attr, |
| 1805 | &dev_attr_min_freq.attr, |
| 1806 | &dev_attr_max_freq.attr, |
| 1807 | &dev_attr_trans_stat.attr, |
| 1808 | NULL, |
| 1809 | }; |
| 1810 | ATTRIBUTE_GROUPS(devfreq); |
| 1811 | |
| 1812 | static ssize_t polling_interval_show(struct device *dev, |
| 1813 | struct device_attribute *attr, char *buf) |
| 1814 | { |
| 1815 | struct devfreq *df = to_devfreq(dev); |
| 1816 | |
| 1817 | if (!df->profile) |
| 1818 | return -EINVAL; |
| 1819 | |
| 1820 | return sprintf(buf, fmt: "%d\n" , df->profile->polling_ms); |
| 1821 | } |
| 1822 | |
| 1823 | static ssize_t polling_interval_store(struct device *dev, |
| 1824 | struct device_attribute *attr, |
| 1825 | const char *buf, size_t count) |
| 1826 | { |
| 1827 | struct devfreq *df = to_devfreq(dev); |
| 1828 | unsigned int value; |
| 1829 | int ret; |
| 1830 | |
| 1831 | if (!df->governor) |
| 1832 | return -EINVAL; |
| 1833 | |
| 1834 | ret = sscanf(buf, "%u" , &value); |
| 1835 | if (ret != 1) |
| 1836 | return -EINVAL; |
| 1837 | |
| 1838 | df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value); |
| 1839 | ret = count; |
| 1840 | |
| 1841 | return ret; |
| 1842 | } |
| 1843 | static DEVICE_ATTR_RW(polling_interval); |
| 1844 | |
| 1845 | static ssize_t timer_show(struct device *dev, |
| 1846 | struct device_attribute *attr, char *buf) |
| 1847 | { |
| 1848 | struct devfreq *df = to_devfreq(dev); |
| 1849 | |
| 1850 | if (!df->profile) |
| 1851 | return -EINVAL; |
| 1852 | |
| 1853 | return sprintf(buf, fmt: "%s\n" , timer_name[df->profile->timer]); |
| 1854 | } |
| 1855 | |
| 1856 | static ssize_t timer_store(struct device *dev, struct device_attribute *attr, |
| 1857 | const char *buf, size_t count) |
| 1858 | { |
| 1859 | struct devfreq *df = to_devfreq(dev); |
| 1860 | char str_timer[DEVFREQ_NAME_LEN + 1]; |
| 1861 | int timer = -1; |
| 1862 | int ret = 0, i; |
| 1863 | |
| 1864 | if (!df->governor || !df->profile) |
| 1865 | return -EINVAL; |
| 1866 | |
| 1867 | ret = sscanf(buf, "%16s" , str_timer); |
| 1868 | if (ret != 1) |
| 1869 | return -EINVAL; |
| 1870 | |
| 1871 | for (i = 0; i < DEVFREQ_TIMER_NUM; i++) { |
| 1872 | if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) { |
| 1873 | timer = i; |
| 1874 | break; |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | if (timer < 0) { |
| 1879 | ret = -EINVAL; |
| 1880 | goto out; |
| 1881 | } |
| 1882 | |
| 1883 | if (df->profile->timer == timer) { |
| 1884 | ret = 0; |
| 1885 | goto out; |
| 1886 | } |
| 1887 | |
| 1888 | mutex_lock(&df->lock); |
| 1889 | df->profile->timer = timer; |
| 1890 | mutex_unlock(lock: &df->lock); |
| 1891 | |
| 1892 | ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL); |
| 1893 | if (ret) { |
| 1894 | dev_warn(dev, "%s: Governor %s not stopped(%d)\n" , |
| 1895 | __func__, df->governor->name, ret); |
| 1896 | goto out; |
| 1897 | } |
| 1898 | |
| 1899 | ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); |
| 1900 | if (ret) |
| 1901 | dev_warn(dev, "%s: Governor %s not started(%d)\n" , |
| 1902 | __func__, df->governor->name, ret); |
| 1903 | out: |
| 1904 | return ret ? ret : count; |
| 1905 | } |
| 1906 | static DEVICE_ATTR_RW(timer); |
| 1907 | |
| 1908 | #define CREATE_SYSFS_FILE(df, name) \ |
| 1909 | { \ |
| 1910 | int ret; \ |
| 1911 | ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr); \ |
| 1912 | if (ret < 0) { \ |
| 1913 | dev_warn(&df->dev, \ |
| 1914 | "Unable to create attr(%s)\n", "##name"); \ |
| 1915 | } \ |
| 1916 | } \ |
| 1917 | |
| 1918 | /* Create the specific sysfs files which depend on each governor. */ |
| 1919 | static void create_sysfs_files(struct devfreq *devfreq, |
| 1920 | const struct devfreq_governor *gov) |
| 1921 | { |
| 1922 | if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL)) |
| 1923 | CREATE_SYSFS_FILE(devfreq, polling_interval); |
| 1924 | if (IS_SUPPORTED_ATTR(gov->attrs, TIMER)) |
| 1925 | CREATE_SYSFS_FILE(devfreq, timer); |
| 1926 | } |
| 1927 | |
| 1928 | /* Remove the specific sysfs files which depend on each governor. */ |
| 1929 | static void remove_sysfs_files(struct devfreq *devfreq, |
| 1930 | const struct devfreq_governor *gov) |
| 1931 | { |
| 1932 | if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL)) |
| 1933 | sysfs_remove_file(kobj: &devfreq->dev.kobj, |
| 1934 | attr: &dev_attr_polling_interval.attr); |
| 1935 | if (IS_SUPPORTED_ATTR(gov->attrs, TIMER)) |
| 1936 | sysfs_remove_file(kobj: &devfreq->dev.kobj, attr: &dev_attr_timer.attr); |
| 1937 | } |
| 1938 | |
| 1939 | /** |
| 1940 | * devfreq_summary_show() - Show the summary of the devfreq devices |
| 1941 | * @s: seq_file instance to show the summary of devfreq devices |
| 1942 | * @data: not used |
| 1943 | * |
| 1944 | * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file. |
| 1945 | * It helps that user can know the detailed information of the devfreq devices. |
| 1946 | * |
| 1947 | * Return 0 always because it shows the information without any data change. |
| 1948 | */ |
| 1949 | static int devfreq_summary_show(struct seq_file *s, void *data) |
| 1950 | { |
| 1951 | struct devfreq *devfreq; |
| 1952 | struct devfreq *p_devfreq = NULL; |
| 1953 | unsigned long cur_freq, min_freq, max_freq; |
| 1954 | unsigned int polling_ms; |
| 1955 | unsigned int timer; |
| 1956 | |
| 1957 | seq_printf(m: s, fmt: "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n" , |
| 1958 | "dev" , |
| 1959 | "parent_dev" , |
| 1960 | "governor" , |
| 1961 | "timer" , |
| 1962 | "polling_ms" , |
| 1963 | "cur_freq_Hz" , |
| 1964 | "min_freq_Hz" , |
| 1965 | "max_freq_Hz" ); |
| 1966 | seq_printf(m: s, fmt: "%30s %30s %15s %10s %10s %12s %12s %12s\n" , |
| 1967 | "------------------------------" , |
| 1968 | "------------------------------" , |
| 1969 | "---------------" , |
| 1970 | "----------" , |
| 1971 | "----------" , |
| 1972 | "------------" , |
| 1973 | "------------" , |
| 1974 | "------------" ); |
| 1975 | |
| 1976 | mutex_lock(&devfreq_list_lock); |
| 1977 | |
| 1978 | list_for_each_entry_reverse(devfreq, &devfreq_list, node) { |
| 1979 | #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE) |
| 1980 | if (!strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE, |
| 1981 | DEVFREQ_NAME_LEN)) { |
| 1982 | struct devfreq_passive_data *data = devfreq->data; |
| 1983 | |
| 1984 | if (data) |
| 1985 | p_devfreq = data->parent; |
| 1986 | } else { |
| 1987 | p_devfreq = NULL; |
| 1988 | } |
| 1989 | #endif |
| 1990 | |
| 1991 | mutex_lock(&devfreq->lock); |
| 1992 | cur_freq = devfreq->previous_freq; |
| 1993 | devfreq_get_freq_range(devfreq, &min_freq, &max_freq); |
| 1994 | timer = devfreq->profile->timer; |
| 1995 | |
| 1996 | if (IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL)) |
| 1997 | polling_ms = devfreq->profile->polling_ms; |
| 1998 | else |
| 1999 | polling_ms = 0; |
| 2000 | mutex_unlock(lock: &devfreq->lock); |
| 2001 | |
| 2002 | seq_printf(m: s, |
| 2003 | fmt: "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n" , |
| 2004 | dev_name(dev: &devfreq->dev), |
| 2005 | p_devfreq ? dev_name(dev: &p_devfreq->dev) : "null" , |
| 2006 | devfreq->governor->name, |
| 2007 | polling_ms ? timer_name[timer] : "null" , |
| 2008 | polling_ms, |
| 2009 | cur_freq, |
| 2010 | min_freq, |
| 2011 | max_freq); |
| 2012 | } |
| 2013 | |
| 2014 | mutex_unlock(lock: &devfreq_list_lock); |
| 2015 | |
| 2016 | return 0; |
| 2017 | } |
| 2018 | DEFINE_SHOW_ATTRIBUTE(devfreq_summary); |
| 2019 | |
| 2020 | static int __init devfreq_init(void) |
| 2021 | { |
| 2022 | devfreq_class = class_create(name: "devfreq" ); |
| 2023 | if (IS_ERR(ptr: devfreq_class)) { |
| 2024 | pr_err("%s: couldn't create class\n" , __FILE__); |
| 2025 | return PTR_ERR(ptr: devfreq_class); |
| 2026 | } |
| 2027 | |
| 2028 | devfreq_wq = create_freezable_workqueue("devfreq_wq" ); |
| 2029 | if (!devfreq_wq) { |
| 2030 | class_destroy(cls: devfreq_class); |
| 2031 | pr_err("%s: couldn't create workqueue\n" , __FILE__); |
| 2032 | return -ENOMEM; |
| 2033 | } |
| 2034 | devfreq_class->dev_groups = devfreq_groups; |
| 2035 | |
| 2036 | devfreq_debugfs = debugfs_create_dir(name: "devfreq" , NULL); |
| 2037 | debugfs_create_file("devfreq_summary" , 0444, |
| 2038 | devfreq_debugfs, NULL, |
| 2039 | &devfreq_summary_fops); |
| 2040 | |
| 2041 | return 0; |
| 2042 | } |
| 2043 | subsys_initcall(devfreq_init); |
| 2044 | |
| 2045 | /* |
| 2046 | * The following are helper functions for devfreq user device drivers with |
| 2047 | * OPP framework. |
| 2048 | */ |
| 2049 | |
| 2050 | /** |
| 2051 | * devfreq_recommended_opp() - Helper function to get proper OPP for the |
| 2052 | * freq value given to target callback. |
| 2053 | * @dev: The devfreq user device. (parent of devfreq) |
| 2054 | * @freq: The frequency given to target function |
| 2055 | * @flags: Flags handed from devfreq framework. |
| 2056 | * |
| 2057 | * The callers are required to call dev_pm_opp_put() for the returned OPP after |
| 2058 | * use. |
| 2059 | */ |
| 2060 | struct dev_pm_opp *devfreq_recommended_opp(struct device *dev, |
| 2061 | unsigned long *freq, |
| 2062 | u32 flags) |
| 2063 | { |
| 2064 | struct dev_pm_opp *opp; |
| 2065 | |
| 2066 | if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) { |
| 2067 | /* The freq is an upper bound. opp should be lower */ |
| 2068 | opp = dev_pm_opp_find_freq_floor_indexed(dev, freq, index: 0); |
| 2069 | |
| 2070 | /* If not available, use the closest opp */ |
| 2071 | if (opp == ERR_PTR(error: -ERANGE)) |
| 2072 | opp = dev_pm_opp_find_freq_ceil_indexed(dev, freq, index: 0); |
| 2073 | } else { |
| 2074 | /* The freq is an lower bound. opp should be higher */ |
| 2075 | opp = dev_pm_opp_find_freq_ceil_indexed(dev, freq, index: 0); |
| 2076 | |
| 2077 | /* If not available, use the closest opp */ |
| 2078 | if (opp == ERR_PTR(error: -ERANGE)) |
| 2079 | opp = dev_pm_opp_find_freq_floor_indexed(dev, freq, index: 0); |
| 2080 | } |
| 2081 | |
| 2082 | return opp; |
| 2083 | } |
| 2084 | EXPORT_SYMBOL(devfreq_recommended_opp); |
| 2085 | |
| 2086 | /** |
| 2087 | * devfreq_register_opp_notifier() - Helper function to get devfreq notified |
| 2088 | * for any changes in the OPP availability |
| 2089 | * changes |
| 2090 | * @dev: The devfreq user device. (parent of devfreq) |
| 2091 | * @devfreq: The devfreq object. |
| 2092 | */ |
| 2093 | int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq) |
| 2094 | { |
| 2095 | return dev_pm_opp_register_notifier(dev, nb: &devfreq->nb); |
| 2096 | } |
| 2097 | EXPORT_SYMBOL(devfreq_register_opp_notifier); |
| 2098 | |
| 2099 | /** |
| 2100 | * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq |
| 2101 | * notified for any changes in the OPP |
| 2102 | * availability changes anymore. |
| 2103 | * @dev: The devfreq user device. (parent of devfreq) |
| 2104 | * @devfreq: The devfreq object. |
| 2105 | * |
| 2106 | * At exit() callback of devfreq_dev_profile, this must be included if |
| 2107 | * devfreq_recommended_opp is used. |
| 2108 | */ |
| 2109 | int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq) |
| 2110 | { |
| 2111 | return dev_pm_opp_unregister_notifier(dev, nb: &devfreq->nb); |
| 2112 | } |
| 2113 | EXPORT_SYMBOL(devfreq_unregister_opp_notifier); |
| 2114 | |
| 2115 | static void devm_devfreq_opp_release(struct device *dev, void *res) |
| 2116 | { |
| 2117 | devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res); |
| 2118 | } |
| 2119 | |
| 2120 | /** |
| 2121 | * devm_devfreq_register_opp_notifier() - Resource-managed |
| 2122 | * devfreq_register_opp_notifier() |
| 2123 | * @dev: The devfreq user device. (parent of devfreq) |
| 2124 | * @devfreq: The devfreq object. |
| 2125 | */ |
| 2126 | int devm_devfreq_register_opp_notifier(struct device *dev, |
| 2127 | struct devfreq *devfreq) |
| 2128 | { |
| 2129 | struct devfreq **ptr; |
| 2130 | int ret; |
| 2131 | |
| 2132 | ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL); |
| 2133 | if (!ptr) |
| 2134 | return -ENOMEM; |
| 2135 | |
| 2136 | ret = devfreq_register_opp_notifier(dev, devfreq); |
| 2137 | if (ret) { |
| 2138 | devres_free(res: ptr); |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
| 2142 | *ptr = devfreq; |
| 2143 | devres_add(dev, res: ptr); |
| 2144 | |
| 2145 | return 0; |
| 2146 | } |
| 2147 | EXPORT_SYMBOL(devm_devfreq_register_opp_notifier); |
| 2148 | |
| 2149 | /** |
| 2150 | * devm_devfreq_unregister_opp_notifier() - Resource-managed |
| 2151 | * devfreq_unregister_opp_notifier() |
| 2152 | * @dev: The devfreq user device. (parent of devfreq) |
| 2153 | * @devfreq: The devfreq object. |
| 2154 | */ |
| 2155 | void devm_devfreq_unregister_opp_notifier(struct device *dev, |
| 2156 | struct devfreq *devfreq) |
| 2157 | { |
| 2158 | WARN_ON(devres_release(dev, devm_devfreq_opp_release, |
| 2159 | devm_devfreq_dev_match, devfreq)); |
| 2160 | } |
| 2161 | EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier); |
| 2162 | |
| 2163 | /** |
| 2164 | * devfreq_register_notifier() - Register a driver with devfreq |
| 2165 | * @devfreq: The devfreq object. |
| 2166 | * @nb: The notifier block to register. |
| 2167 | * @list: DEVFREQ_TRANSITION_NOTIFIER. |
| 2168 | */ |
| 2169 | int devfreq_register_notifier(struct devfreq *devfreq, |
| 2170 | struct notifier_block *nb, |
| 2171 | unsigned int list) |
| 2172 | { |
| 2173 | int ret = 0; |
| 2174 | |
| 2175 | if (!devfreq) |
| 2176 | return -EINVAL; |
| 2177 | |
| 2178 | switch (list) { |
| 2179 | case DEVFREQ_TRANSITION_NOTIFIER: |
| 2180 | ret = srcu_notifier_chain_register( |
| 2181 | nh: &devfreq->transition_notifier_list, nb); |
| 2182 | break; |
| 2183 | default: |
| 2184 | ret = -EINVAL; |
| 2185 | } |
| 2186 | |
| 2187 | return ret; |
| 2188 | } |
| 2189 | EXPORT_SYMBOL(devfreq_register_notifier); |
| 2190 | |
| 2191 | /* |
| 2192 | * devfreq_unregister_notifier() - Unregister a driver with devfreq |
| 2193 | * @devfreq: The devfreq object. |
| 2194 | * @nb: The notifier block to be unregistered. |
| 2195 | * @list: DEVFREQ_TRANSITION_NOTIFIER. |
| 2196 | */ |
| 2197 | int devfreq_unregister_notifier(struct devfreq *devfreq, |
| 2198 | struct notifier_block *nb, |
| 2199 | unsigned int list) |
| 2200 | { |
| 2201 | int ret = 0; |
| 2202 | |
| 2203 | if (!devfreq) |
| 2204 | return -EINVAL; |
| 2205 | |
| 2206 | switch (list) { |
| 2207 | case DEVFREQ_TRANSITION_NOTIFIER: |
| 2208 | ret = srcu_notifier_chain_unregister( |
| 2209 | nh: &devfreq->transition_notifier_list, nb); |
| 2210 | break; |
| 2211 | default: |
| 2212 | ret = -EINVAL; |
| 2213 | } |
| 2214 | |
| 2215 | return ret; |
| 2216 | } |
| 2217 | EXPORT_SYMBOL(devfreq_unregister_notifier); |
| 2218 | |
| 2219 | struct devfreq_notifier_devres { |
| 2220 | struct devfreq *devfreq; |
| 2221 | struct notifier_block *nb; |
| 2222 | unsigned int list; |
| 2223 | }; |
| 2224 | |
| 2225 | static void devm_devfreq_notifier_release(struct device *dev, void *res) |
| 2226 | { |
| 2227 | struct devfreq_notifier_devres *this = res; |
| 2228 | |
| 2229 | devfreq_unregister_notifier(this->devfreq, this->nb, this->list); |
| 2230 | } |
| 2231 | |
| 2232 | /** |
| 2233 | * devm_devfreq_register_notifier() |
| 2234 | * - Resource-managed devfreq_register_notifier() |
| 2235 | * @dev: The devfreq user device. (parent of devfreq) |
| 2236 | * @devfreq: The devfreq object. |
| 2237 | * @nb: The notifier block to be unregistered. |
| 2238 | * @list: DEVFREQ_TRANSITION_NOTIFIER. |
| 2239 | */ |
| 2240 | int devm_devfreq_register_notifier(struct device *dev, |
| 2241 | struct devfreq *devfreq, |
| 2242 | struct notifier_block *nb, |
| 2243 | unsigned int list) |
| 2244 | { |
| 2245 | struct devfreq_notifier_devres *ptr; |
| 2246 | int ret; |
| 2247 | |
| 2248 | ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr), |
| 2249 | GFP_KERNEL); |
| 2250 | if (!ptr) |
| 2251 | return -ENOMEM; |
| 2252 | |
| 2253 | ret = devfreq_register_notifier(devfreq, nb, list); |
| 2254 | if (ret) { |
| 2255 | devres_free(res: ptr); |
| 2256 | return ret; |
| 2257 | } |
| 2258 | |
| 2259 | ptr->devfreq = devfreq; |
| 2260 | ptr->nb = nb; |
| 2261 | ptr->list = list; |
| 2262 | devres_add(dev, res: ptr); |
| 2263 | |
| 2264 | return 0; |
| 2265 | } |
| 2266 | EXPORT_SYMBOL(devm_devfreq_register_notifier); |
| 2267 | |
| 2268 | /** |
| 2269 | * devm_devfreq_unregister_notifier() |
| 2270 | * - Resource-managed devfreq_unregister_notifier() |
| 2271 | * @dev: The devfreq user device. (parent of devfreq) |
| 2272 | * @devfreq: The devfreq object. |
| 2273 | * @nb: The notifier block to be unregistered. |
| 2274 | * @list: DEVFREQ_TRANSITION_NOTIFIER. |
| 2275 | */ |
| 2276 | void devm_devfreq_unregister_notifier(struct device *dev, |
| 2277 | struct devfreq *devfreq, |
| 2278 | struct notifier_block *nb, |
| 2279 | unsigned int list) |
| 2280 | { |
| 2281 | WARN_ON(devres_release(dev, devm_devfreq_notifier_release, |
| 2282 | devm_devfreq_dev_match, devfreq)); |
| 2283 | } |
| 2284 | EXPORT_SYMBOL(devm_devfreq_unregister_notifier); |
| 2285 | |