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