2 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/kmod.h>
15 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/export.h>
20 #include <linux/slab.h>
21 #include <linux/stat.h>
22 #include <linux/pm_opp.h>
23 #include <linux/devfreq.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>
32 #define MAX(a,b) ((a > b) ? a : b)
33 #define MIN(a,b) ((a < b) ? a : b)
35 static struct class *devfreq_class
;
38 * devfreq core provides delayed work based load monitoring helper
39 * functions. Governors can use these or can implement their own
40 * monitoring mechanism.
42 static struct workqueue_struct
*devfreq_wq
;
44 /* The list of all device-devfreq governors */
45 static LIST_HEAD(devfreq_governor_list
);
46 /* The list of all device-devfreq */
47 static LIST_HEAD(devfreq_list
);
48 static DEFINE_MUTEX(devfreq_list_lock
);
51 * find_device_devfreq() - find devfreq struct using device pointer
52 * @dev: device pointer used to lookup device devfreq.
54 * Search the list of device devfreqs and return the matched device's
55 * devfreq info. devfreq_list_lock should be held by the caller.
57 static struct devfreq
*find_device_devfreq(struct device
*dev
)
59 struct devfreq
*tmp_devfreq
;
61 if (IS_ERR_OR_NULL(dev
)) {
62 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__
);
63 return ERR_PTR(-EINVAL
);
65 WARN(!mutex_is_locked(&devfreq_list_lock
),
66 "devfreq_list_lock must be locked.");
68 list_for_each_entry(tmp_devfreq
, &devfreq_list
, node
) {
69 if (tmp_devfreq
->dev
.parent
== dev
)
73 return ERR_PTR(-ENODEV
);
76 static unsigned long find_available_min_freq(struct devfreq
*devfreq
)
78 struct dev_pm_opp
*opp
;
79 unsigned long min_freq
= 0;
81 opp
= dev_pm_opp_find_freq_ceil(devfreq
->dev
.parent
, &min_freq
);
90 static unsigned long find_available_max_freq(struct devfreq
*devfreq
)
92 struct dev_pm_opp
*opp
;
93 unsigned long max_freq
= ULONG_MAX
;
95 opp
= dev_pm_opp_find_freq_floor(devfreq
->dev
.parent
, &max_freq
);
105 * devfreq_get_freq_level() - Lookup freq_table for the frequency
106 * @devfreq: the devfreq instance
107 * @freq: the target frequency
109 static int devfreq_get_freq_level(struct devfreq
*devfreq
, unsigned long freq
)
113 for (lev
= 0; lev
< devfreq
->profile
->max_state
; lev
++)
114 if (freq
== devfreq
->profile
->freq_table
[lev
])
120 static int set_freq_table(struct devfreq
*devfreq
)
122 struct devfreq_dev_profile
*profile
= devfreq
->profile
;
123 struct dev_pm_opp
*opp
;
127 /* Initialize the freq_table from OPP table */
128 count
= dev_pm_opp_get_opp_count(devfreq
->dev
.parent
);
132 profile
->max_state
= count
;
133 profile
->freq_table
= devm_kcalloc(devfreq
->dev
.parent
,
135 sizeof(*profile
->freq_table
),
137 if (!profile
->freq_table
) {
138 profile
->max_state
= 0;
142 for (i
= 0, freq
= 0; i
< profile
->max_state
; i
++, freq
++) {
143 opp
= dev_pm_opp_find_freq_ceil(devfreq
->dev
.parent
, &freq
);
145 devm_kfree(devfreq
->dev
.parent
, profile
->freq_table
);
146 profile
->max_state
= 0;
150 profile
->freq_table
[i
] = freq
;
157 * devfreq_update_status() - Update statistics of devfreq behavior
158 * @devfreq: the devfreq instance
159 * @freq: the update target frequency
161 int devfreq_update_status(struct devfreq
*devfreq
, unsigned long freq
)
163 int lev
, prev_lev
, ret
= 0;
164 unsigned long cur_time
;
166 lockdep_assert_held(&devfreq
->lock
);
169 /* Immediately exit if previous_freq is not initialized yet. */
170 if (!devfreq
->previous_freq
)
173 prev_lev
= devfreq_get_freq_level(devfreq
, devfreq
->previous_freq
);
179 devfreq
->time_in_state
[prev_lev
] +=
180 cur_time
- devfreq
->last_stat_updated
;
182 lev
= devfreq_get_freq_level(devfreq
, freq
);
188 if (lev
!= prev_lev
) {
189 devfreq
->trans_table
[(prev_lev
*
190 devfreq
->profile
->max_state
) + lev
]++;
191 devfreq
->total_trans
++;
195 devfreq
->last_stat_updated
= cur_time
;
198 EXPORT_SYMBOL(devfreq_update_status
);
201 * find_devfreq_governor() - find devfreq governor from name
202 * @name: name of the governor
204 * Search the list of devfreq governors and return the matched
205 * governor's pointer. devfreq_list_lock should be held by the caller.
207 static struct devfreq_governor
*find_devfreq_governor(const char *name
)
209 struct devfreq_governor
*tmp_governor
;
211 if (IS_ERR_OR_NULL(name
)) {
212 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__
);
213 return ERR_PTR(-EINVAL
);
215 WARN(!mutex_is_locked(&devfreq_list_lock
),
216 "devfreq_list_lock must be locked.");
218 list_for_each_entry(tmp_governor
, &devfreq_governor_list
, node
) {
219 if (!strncmp(tmp_governor
->name
, name
, DEVFREQ_NAME_LEN
))
223 return ERR_PTR(-ENODEV
);
227 * try_then_request_governor() - Try to find the governor and request the
228 * module if is not found.
229 * @name: name of the governor
231 * Search the list of devfreq governors and request the module and try again
232 * if is not found. This can happen when both drivers (the governor driver
233 * and the driver that call devfreq_add_device) are built as modules.
234 * devfreq_list_lock should be held by the caller. Returns the matched
235 * governor's pointer or an error pointer.
237 static struct devfreq_governor
*try_then_request_governor(const char *name
)
239 struct devfreq_governor
*governor
;
242 if (IS_ERR_OR_NULL(name
)) {
243 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__
);
244 return ERR_PTR(-EINVAL
);
246 WARN(!mutex_is_locked(&devfreq_list_lock
),
247 "devfreq_list_lock must be locked.");
249 governor
= find_devfreq_governor(name
);
250 if (IS_ERR(governor
)) {
251 mutex_unlock(&devfreq_list_lock
);
253 if (!strncmp(name
, DEVFREQ_GOV_SIMPLE_ONDEMAND
,
255 err
= request_module("governor_%s", "simpleondemand");
257 err
= request_module("governor_%s", name
);
258 /* Restore previous state before return */
259 mutex_lock(&devfreq_list_lock
);
261 return (err
< 0) ? ERR_PTR(err
) : ERR_PTR(-EINVAL
);
263 governor
= find_devfreq_governor(name
);
269 static int devfreq_notify_transition(struct devfreq
*devfreq
,
270 struct devfreq_freqs
*freqs
, unsigned int state
)
276 case DEVFREQ_PRECHANGE
:
277 srcu_notifier_call_chain(&devfreq
->transition_notifier_list
,
278 DEVFREQ_PRECHANGE
, freqs
);
281 case DEVFREQ_POSTCHANGE
:
282 srcu_notifier_call_chain(&devfreq
->transition_notifier_list
,
283 DEVFREQ_POSTCHANGE
, freqs
);
292 /* Load monitoring helper functions for governors use */
295 * update_devfreq() - Reevaluate the device and configure frequency.
296 * @devfreq: the devfreq instance.
298 * Note: Lock devfreq->lock before calling update_devfreq
299 * This function is exported for governors.
301 int update_devfreq(struct devfreq
*devfreq
)
303 struct devfreq_freqs freqs
;
304 unsigned long freq
, cur_freq
, min_freq
, max_freq
;
308 if (!mutex_is_locked(&devfreq
->lock
)) {
309 WARN(true, "devfreq->lock must be locked by the caller.\n");
313 if (!devfreq
->governor
)
316 /* Reevaluate the proper frequency */
317 err
= devfreq
->governor
->get_target_freq(devfreq
, &freq
);
322 * Adjust the frequency with user freq, QoS and available freq.
324 * List from the highest priority
328 max_freq
= MIN(devfreq
->scaling_max_freq
, devfreq
->max_freq
);
329 min_freq
= MAX(devfreq
->scaling_min_freq
, devfreq
->min_freq
);
331 if (freq
< min_freq
) {
333 flags
&= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND
; /* Use GLB */
335 if (freq
> max_freq
) {
337 flags
|= DEVFREQ_FLAG_LEAST_UPPER_BOUND
; /* Use LUB */
340 if (devfreq
->profile
->get_cur_freq
)
341 devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &cur_freq
);
343 cur_freq
= devfreq
->previous_freq
;
345 freqs
.old
= cur_freq
;
347 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_PRECHANGE
);
349 err
= devfreq
->profile
->target(devfreq
->dev
.parent
, &freq
, flags
);
351 freqs
.new = cur_freq
;
352 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_POSTCHANGE
);
357 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_POSTCHANGE
);
359 if (devfreq_update_status(devfreq
, freq
))
360 dev_err(&devfreq
->dev
,
361 "Couldn't update frequency transition information.\n");
363 devfreq
->previous_freq
= freq
;
366 EXPORT_SYMBOL(update_devfreq
);
369 * devfreq_monitor() - Periodically poll devfreq objects.
370 * @work: the work struct used to run devfreq_monitor periodically.
373 static void devfreq_monitor(struct work_struct
*work
)
376 struct devfreq
*devfreq
= container_of(work
,
377 struct devfreq
, work
.work
);
379 mutex_lock(&devfreq
->lock
);
380 err
= update_devfreq(devfreq
);
382 dev_err(&devfreq
->dev
, "dvfs failed with (%d) error\n", err
);
384 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
385 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
386 mutex_unlock(&devfreq
->lock
);
390 * devfreq_monitor_start() - Start load monitoring of devfreq instance
391 * @devfreq: the devfreq instance.
393 * Helper function for starting devfreq device load monitoing. By
394 * default delayed work based monitoring is supported. Function
395 * to be called from governor in response to DEVFREQ_GOV_START
396 * event when device is added to devfreq framework.
398 void devfreq_monitor_start(struct devfreq
*devfreq
)
400 INIT_DEFERRABLE_WORK(&devfreq
->work
, devfreq_monitor
);
401 if (devfreq
->profile
->polling_ms
)
402 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
403 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
405 EXPORT_SYMBOL(devfreq_monitor_start
);
408 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
409 * @devfreq: the devfreq instance.
411 * Helper function to stop devfreq device load monitoing. Function
412 * to be called from governor in response to DEVFREQ_GOV_STOP
413 * event when device is removed from devfreq framework.
415 void devfreq_monitor_stop(struct devfreq
*devfreq
)
417 cancel_delayed_work_sync(&devfreq
->work
);
419 EXPORT_SYMBOL(devfreq_monitor_stop
);
422 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
423 * @devfreq: the devfreq instance.
425 * Helper function to suspend devfreq device load monitoing. Function
426 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
427 * event or when polling interval is set to zero.
429 * Note: Though this function is same as devfreq_monitor_stop(),
430 * intentionally kept separate to provide hooks for collecting
431 * transition statistics.
433 void devfreq_monitor_suspend(struct devfreq
*devfreq
)
435 mutex_lock(&devfreq
->lock
);
436 if (devfreq
->stop_polling
) {
437 mutex_unlock(&devfreq
->lock
);
441 devfreq_update_status(devfreq
, devfreq
->previous_freq
);
442 devfreq
->stop_polling
= true;
443 mutex_unlock(&devfreq
->lock
);
444 cancel_delayed_work_sync(&devfreq
->work
);
446 EXPORT_SYMBOL(devfreq_monitor_suspend
);
449 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
450 * @devfreq: the devfreq instance.
452 * Helper function to resume devfreq device load monitoing. Function
453 * to be called from governor in response to DEVFREQ_GOV_RESUME
454 * event or when polling interval is set to non-zero.
456 void devfreq_monitor_resume(struct devfreq
*devfreq
)
460 mutex_lock(&devfreq
->lock
);
461 if (!devfreq
->stop_polling
)
464 if (!delayed_work_pending(&devfreq
->work
) &&
465 devfreq
->profile
->polling_ms
)
466 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
467 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
469 devfreq
->last_stat_updated
= jiffies
;
470 devfreq
->stop_polling
= false;
472 if (devfreq
->profile
->get_cur_freq
&&
473 !devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &freq
))
474 devfreq
->previous_freq
= freq
;
477 mutex_unlock(&devfreq
->lock
);
479 EXPORT_SYMBOL(devfreq_monitor_resume
);
482 * devfreq_interval_update() - Update device devfreq monitoring interval
483 * @devfreq: the devfreq instance.
484 * @delay: new polling interval to be set.
486 * Helper function to set new load monitoring polling interval. Function
487 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
489 void devfreq_interval_update(struct devfreq
*devfreq
, unsigned int *delay
)
491 unsigned int cur_delay
= devfreq
->profile
->polling_ms
;
492 unsigned int new_delay
= *delay
;
494 mutex_lock(&devfreq
->lock
);
495 devfreq
->profile
->polling_ms
= new_delay
;
497 if (devfreq
->stop_polling
)
500 /* if new delay is zero, stop polling */
502 mutex_unlock(&devfreq
->lock
);
503 cancel_delayed_work_sync(&devfreq
->work
);
507 /* if current delay is zero, start polling with new delay */
509 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
510 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
514 /* if current delay is greater than new delay, restart polling */
515 if (cur_delay
> new_delay
) {
516 mutex_unlock(&devfreq
->lock
);
517 cancel_delayed_work_sync(&devfreq
->work
);
518 mutex_lock(&devfreq
->lock
);
519 if (!devfreq
->stop_polling
)
520 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
521 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
524 mutex_unlock(&devfreq
->lock
);
526 EXPORT_SYMBOL(devfreq_interval_update
);
529 * devfreq_notifier_call() - Notify that the device frequency requirements
530 * has been changed out of devfreq framework.
531 * @nb: the notifier_block (supposed to be devfreq->nb)
535 * Called by a notifier that uses devfreq->nb.
537 static int devfreq_notifier_call(struct notifier_block
*nb
, unsigned long type
,
540 struct devfreq
*devfreq
= container_of(nb
, struct devfreq
, nb
);
543 mutex_lock(&devfreq
->lock
);
545 devfreq
->scaling_min_freq
= find_available_min_freq(devfreq
);
546 if (!devfreq
->scaling_min_freq
)
549 devfreq
->scaling_max_freq
= find_available_max_freq(devfreq
);
550 if (!devfreq
->scaling_max_freq
) {
551 devfreq
->scaling_max_freq
= ULONG_MAX
;
555 err
= update_devfreq(devfreq
);
558 mutex_unlock(&devfreq
->lock
);
560 dev_err(devfreq
->dev
.parent
,
561 "failed to update frequency from OPP notifier (%d)\n",
568 * devfreq_dev_release() - Callback for struct device to release the device.
569 * @dev: the devfreq device
571 * Remove devfreq from the list and release its resources.
573 static void devfreq_dev_release(struct device
*dev
)
575 struct devfreq
*devfreq
= to_devfreq(dev
);
577 mutex_lock(&devfreq_list_lock
);
578 list_del(&devfreq
->node
);
579 mutex_unlock(&devfreq_list_lock
);
581 if (devfreq
->profile
->exit
)
582 devfreq
->profile
->exit(devfreq
->dev
.parent
);
584 mutex_destroy(&devfreq
->lock
);
589 * devfreq_add_device() - Add devfreq feature to the device
590 * @dev: the device to add devfreq feature.
591 * @profile: device-specific profile to run devfreq.
592 * @governor_name: name of the policy to choose frequency.
593 * @data: private data for the governor. The devfreq framework does not
596 struct devfreq
*devfreq_add_device(struct device
*dev
,
597 struct devfreq_dev_profile
*profile
,
598 const char *governor_name
,
601 struct devfreq
*devfreq
;
602 struct devfreq_governor
*governor
;
605 if (!dev
|| !profile
|| !governor_name
) {
606 dev_err(dev
, "%s: Invalid parameters.\n", __func__
);
607 return ERR_PTR(-EINVAL
);
610 mutex_lock(&devfreq_list_lock
);
611 devfreq
= find_device_devfreq(dev
);
612 mutex_unlock(&devfreq_list_lock
);
613 if (!IS_ERR(devfreq
)) {
614 dev_err(dev
, "%s: Unable to create devfreq for the device.\n",
620 devfreq
= kzalloc(sizeof(struct devfreq
), GFP_KERNEL
);
626 mutex_init(&devfreq
->lock
);
627 mutex_lock(&devfreq
->lock
);
628 devfreq
->dev
.parent
= dev
;
629 devfreq
->dev
.class = devfreq_class
;
630 devfreq
->dev
.release
= devfreq_dev_release
;
631 INIT_LIST_HEAD(&devfreq
->node
);
632 devfreq
->profile
= profile
;
633 strncpy(devfreq
->governor_name
, governor_name
, DEVFREQ_NAME_LEN
);
634 devfreq
->previous_freq
= profile
->initial_freq
;
635 devfreq
->last_status
.current_frequency
= profile
->initial_freq
;
636 devfreq
->data
= data
;
637 devfreq
->nb
.notifier_call
= devfreq_notifier_call
;
639 if (!devfreq
->profile
->max_state
&& !devfreq
->profile
->freq_table
) {
640 mutex_unlock(&devfreq
->lock
);
641 err
= set_freq_table(devfreq
);
644 mutex_lock(&devfreq
->lock
);
647 devfreq
->scaling_min_freq
= find_available_min_freq(devfreq
);
648 if (!devfreq
->scaling_min_freq
) {
649 mutex_unlock(&devfreq
->lock
);
653 devfreq
->min_freq
= devfreq
->scaling_min_freq
;
655 devfreq
->scaling_max_freq
= find_available_max_freq(devfreq
);
656 if (!devfreq
->scaling_max_freq
) {
657 mutex_unlock(&devfreq
->lock
);
661 devfreq
->max_freq
= devfreq
->scaling_max_freq
;
663 dev_set_name(&devfreq
->dev
, "%s", dev_name(dev
));
664 err
= device_register(&devfreq
->dev
);
666 mutex_unlock(&devfreq
->lock
);
667 put_device(&devfreq
->dev
);
671 devfreq
->trans_table
=
672 devm_kzalloc(&devfreq
->dev
,
673 array3_size(sizeof(unsigned int),
674 devfreq
->profile
->max_state
,
675 devfreq
->profile
->max_state
),
677 devfreq
->time_in_state
= devm_kcalloc(&devfreq
->dev
,
678 devfreq
->profile
->max_state
,
679 sizeof(unsigned long),
681 devfreq
->last_stat_updated
= jiffies
;
683 srcu_init_notifier_head(&devfreq
->transition_notifier_list
);
685 mutex_unlock(&devfreq
->lock
);
687 mutex_lock(&devfreq_list_lock
);
689 governor
= try_then_request_governor(devfreq
->governor_name
);
690 if (IS_ERR(governor
)) {
691 dev_err(dev
, "%s: Unable to find governor for the device\n",
693 err
= PTR_ERR(governor
);
697 devfreq
->governor
= governor
;
698 err
= devfreq
->governor
->event_handler(devfreq
, DEVFREQ_GOV_START
,
701 dev_err(dev
, "%s: Unable to start governor for the device\n",
706 list_add(&devfreq
->node
, &devfreq_list
);
708 mutex_unlock(&devfreq_list_lock
);
713 mutex_unlock(&devfreq_list_lock
);
715 devfreq_remove_device(devfreq
);
723 EXPORT_SYMBOL(devfreq_add_device
);
726 * devfreq_remove_device() - Remove devfreq feature from a device.
727 * @devfreq: the devfreq instance to be removed
729 * The opposite of devfreq_add_device().
731 int devfreq_remove_device(struct devfreq
*devfreq
)
736 if (devfreq
->governor
)
737 devfreq
->governor
->event_handler(devfreq
,
738 DEVFREQ_GOV_STOP
, NULL
);
739 device_unregister(&devfreq
->dev
);
743 EXPORT_SYMBOL(devfreq_remove_device
);
745 static int devm_devfreq_dev_match(struct device
*dev
, void *res
, void *data
)
747 struct devfreq
**r
= res
;
749 if (WARN_ON(!r
|| !*r
))
755 static void devm_devfreq_dev_release(struct device
*dev
, void *res
)
757 devfreq_remove_device(*(struct devfreq
**)res
);
761 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
762 * @dev: the device to add devfreq feature.
763 * @profile: device-specific profile to run devfreq.
764 * @governor_name: name of the policy to choose frequency.
765 * @data: private data for the governor. The devfreq framework does not
768 * This function manages automatically the memory of devfreq device using device
769 * resource management and simplify the free operation for memory of devfreq
772 struct devfreq
*devm_devfreq_add_device(struct device
*dev
,
773 struct devfreq_dev_profile
*profile
,
774 const char *governor_name
,
777 struct devfreq
**ptr
, *devfreq
;
779 ptr
= devres_alloc(devm_devfreq_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
781 return ERR_PTR(-ENOMEM
);
783 devfreq
= devfreq_add_device(dev
, profile
, governor_name
, data
);
784 if (IS_ERR(devfreq
)) {
790 devres_add(dev
, ptr
);
794 EXPORT_SYMBOL(devm_devfreq_add_device
);
798 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
799 * @dev - instance to the given device
800 * @index - index into list of devfreq
802 * return the instance of devfreq device
804 struct devfreq
*devfreq_get_devfreq_by_phandle(struct device
*dev
, int index
)
806 struct device_node
*node
;
807 struct devfreq
*devfreq
;
810 return ERR_PTR(-EINVAL
);
813 return ERR_PTR(-EINVAL
);
815 node
= of_parse_phandle(dev
->of_node
, "devfreq", index
);
817 return ERR_PTR(-ENODEV
);
819 mutex_lock(&devfreq_list_lock
);
820 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
821 if (devfreq
->dev
.parent
822 && devfreq
->dev
.parent
->of_node
== node
) {
823 mutex_unlock(&devfreq_list_lock
);
828 mutex_unlock(&devfreq_list_lock
);
831 return ERR_PTR(-EPROBE_DEFER
);
834 struct devfreq
*devfreq_get_devfreq_by_phandle(struct device
*dev
, int index
)
836 return ERR_PTR(-ENODEV
);
838 #endif /* CONFIG_OF */
839 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle
);
842 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
843 * @dev: the device to add devfreq feature.
844 * @devfreq: the devfreq instance to be removed
846 void devm_devfreq_remove_device(struct device
*dev
, struct devfreq
*devfreq
)
848 WARN_ON(devres_release(dev
, devm_devfreq_dev_release
,
849 devm_devfreq_dev_match
, devfreq
));
851 EXPORT_SYMBOL(devm_devfreq_remove_device
);
854 * devfreq_suspend_device() - Suspend devfreq of a device.
855 * @devfreq: the devfreq instance to be suspended
857 * This function is intended to be called by the pm callbacks
858 * (e.g., runtime_suspend, suspend) of the device driver that
861 int devfreq_suspend_device(struct devfreq
*devfreq
)
866 if (!devfreq
->governor
)
869 return devfreq
->governor
->event_handler(devfreq
,
870 DEVFREQ_GOV_SUSPEND
, NULL
);
872 EXPORT_SYMBOL(devfreq_suspend_device
);
875 * devfreq_resume_device() - Resume devfreq of a device.
876 * @devfreq: the devfreq instance to be resumed
878 * This function is intended to be called by the pm callbacks
879 * (e.g., runtime_resume, resume) of the device driver that
882 int devfreq_resume_device(struct devfreq
*devfreq
)
887 if (!devfreq
->governor
)
890 return devfreq
->governor
->event_handler(devfreq
,
891 DEVFREQ_GOV_RESUME
, NULL
);
893 EXPORT_SYMBOL(devfreq_resume_device
);
896 * devfreq_add_governor() - Add devfreq governor
897 * @governor: the devfreq governor to be added
899 int devfreq_add_governor(struct devfreq_governor
*governor
)
901 struct devfreq_governor
*g
;
902 struct devfreq
*devfreq
;
906 pr_err("%s: Invalid parameters.\n", __func__
);
910 mutex_lock(&devfreq_list_lock
);
911 g
= find_devfreq_governor(governor
->name
);
913 pr_err("%s: governor %s already registered\n", __func__
,
919 list_add(&governor
->node
, &devfreq_governor_list
);
921 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
923 struct device
*dev
= devfreq
->dev
.parent
;
925 if (!strncmp(devfreq
->governor_name
, governor
->name
,
927 /* The following should never occur */
928 if (devfreq
->governor
) {
930 "%s: Governor %s already present\n",
931 __func__
, devfreq
->governor
->name
);
932 ret
= devfreq
->governor
->event_handler(devfreq
,
933 DEVFREQ_GOV_STOP
, NULL
);
936 "%s: Governor %s stop = %d\n",
938 devfreq
->governor
->name
, ret
);
942 devfreq
->governor
= governor
;
943 ret
= devfreq
->governor
->event_handler(devfreq
,
944 DEVFREQ_GOV_START
, NULL
);
946 dev_warn(dev
, "%s: Governor %s start=%d\n",
947 __func__
, devfreq
->governor
->name
,
954 mutex_unlock(&devfreq_list_lock
);
958 EXPORT_SYMBOL(devfreq_add_governor
);
961 * devfreq_remove_governor() - Remove devfreq feature from a device.
962 * @governor: the devfreq governor to be removed
964 int devfreq_remove_governor(struct devfreq_governor
*governor
)
966 struct devfreq_governor
*g
;
967 struct devfreq
*devfreq
;
971 pr_err("%s: Invalid parameters.\n", __func__
);
975 mutex_lock(&devfreq_list_lock
);
976 g
= find_devfreq_governor(governor
->name
);
978 pr_err("%s: governor %s not registered\n", __func__
,
983 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
985 struct device
*dev
= devfreq
->dev
.parent
;
987 if (!strncmp(devfreq
->governor_name
, governor
->name
,
989 /* we should have a devfreq governor! */
990 if (!devfreq
->governor
) {
991 dev_warn(dev
, "%s: Governor %s NOT present\n",
992 __func__
, governor
->name
);
996 ret
= devfreq
->governor
->event_handler(devfreq
,
997 DEVFREQ_GOV_STOP
, NULL
);
999 dev_warn(dev
, "%s: Governor %s stop=%d\n",
1000 __func__
, devfreq
->governor
->name
,
1003 devfreq
->governor
= NULL
;
1007 list_del(&governor
->node
);
1009 mutex_unlock(&devfreq_list_lock
);
1013 EXPORT_SYMBOL(devfreq_remove_governor
);
1015 static ssize_t
name_show(struct device
*dev
,
1016 struct device_attribute
*attr
, char *buf
)
1018 struct devfreq
*devfreq
= to_devfreq(dev
);
1019 return sprintf(buf
, "%s\n", dev_name(devfreq
->dev
.parent
));
1021 static DEVICE_ATTR_RO(name
);
1023 static ssize_t
governor_show(struct device
*dev
,
1024 struct device_attribute
*attr
, char *buf
)
1026 if (!to_devfreq(dev
)->governor
)
1029 return sprintf(buf
, "%s\n", to_devfreq(dev
)->governor
->name
);
1032 static ssize_t
governor_store(struct device
*dev
, struct device_attribute
*attr
,
1033 const char *buf
, size_t count
)
1035 struct devfreq
*df
= to_devfreq(dev
);
1037 char str_governor
[DEVFREQ_NAME_LEN
+ 1];
1038 struct devfreq_governor
*governor
;
1040 ret
= sscanf(buf
, "%" __stringify(DEVFREQ_NAME_LEN
) "s", str_governor
);
1044 mutex_lock(&devfreq_list_lock
);
1045 governor
= try_then_request_governor(str_governor
);
1046 if (IS_ERR(governor
)) {
1047 ret
= PTR_ERR(governor
);
1050 if (df
->governor
== governor
) {
1053 } else if ((df
->governor
&& df
->governor
->immutable
) ||
1054 governor
->immutable
) {
1060 ret
= df
->governor
->event_handler(df
, DEVFREQ_GOV_STOP
, NULL
);
1062 dev_warn(dev
, "%s: Governor %s not stopped(%d)\n",
1063 __func__
, df
->governor
->name
, ret
);
1067 df
->governor
= governor
;
1068 strncpy(df
->governor_name
, governor
->name
, DEVFREQ_NAME_LEN
);
1069 ret
= df
->governor
->event_handler(df
, DEVFREQ_GOV_START
, NULL
);
1071 dev_warn(dev
, "%s: Governor %s not started(%d)\n",
1072 __func__
, df
->governor
->name
, ret
);
1074 mutex_unlock(&devfreq_list_lock
);
1080 static DEVICE_ATTR_RW(governor
);
1082 static ssize_t
available_governors_show(struct device
*d
,
1083 struct device_attribute
*attr
,
1086 struct devfreq
*df
= to_devfreq(d
);
1089 mutex_lock(&devfreq_list_lock
);
1092 * The devfreq with immutable governor (e.g., passive) shows
1093 * only own governor.
1095 if (df
->governor
&& df
->governor
->immutable
) {
1096 count
= scnprintf(&buf
[count
], DEVFREQ_NAME_LEN
,
1097 "%s ", df
->governor_name
);
1099 * The devfreq device shows the registered governor except for
1100 * immutable governors such as passive governor .
1103 struct devfreq_governor
*governor
;
1105 list_for_each_entry(governor
, &devfreq_governor_list
, node
) {
1106 if (governor
->immutable
)
1108 count
+= scnprintf(&buf
[count
], (PAGE_SIZE
- count
- 2),
1109 "%s ", governor
->name
);
1113 mutex_unlock(&devfreq_list_lock
);
1115 /* Truncate the trailing space */
1119 count
+= sprintf(&buf
[count
], "\n");
1123 static DEVICE_ATTR_RO(available_governors
);
1125 static ssize_t
cur_freq_show(struct device
*dev
, struct device_attribute
*attr
,
1129 struct devfreq
*devfreq
= to_devfreq(dev
);
1131 if (devfreq
->profile
->get_cur_freq
&&
1132 !devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &freq
))
1133 return sprintf(buf
, "%lu\n", freq
);
1135 return sprintf(buf
, "%lu\n", devfreq
->previous_freq
);
1137 static DEVICE_ATTR_RO(cur_freq
);
1139 static ssize_t
target_freq_show(struct device
*dev
,
1140 struct device_attribute
*attr
, char *buf
)
1142 return sprintf(buf
, "%lu\n", to_devfreq(dev
)->previous_freq
);
1144 static DEVICE_ATTR_RO(target_freq
);
1146 static ssize_t
polling_interval_show(struct device
*dev
,
1147 struct device_attribute
*attr
, char *buf
)
1149 return sprintf(buf
, "%d\n", to_devfreq(dev
)->profile
->polling_ms
);
1152 static ssize_t
polling_interval_store(struct device
*dev
,
1153 struct device_attribute
*attr
,
1154 const char *buf
, size_t count
)
1156 struct devfreq
*df
= to_devfreq(dev
);
1163 ret
= sscanf(buf
, "%u", &value
);
1167 df
->governor
->event_handler(df
, DEVFREQ_GOV_INTERVAL
, &value
);
1172 static DEVICE_ATTR_RW(polling_interval
);
1174 static ssize_t
min_freq_store(struct device
*dev
, struct device_attribute
*attr
,
1175 const char *buf
, size_t count
)
1177 struct devfreq
*df
= to_devfreq(dev
);
1178 unsigned long value
;
1181 ret
= sscanf(buf
, "%lu", &value
);
1185 mutex_lock(&df
->lock
);
1188 if (value
> df
->max_freq
) {
1193 unsigned long *freq_table
= df
->profile
->freq_table
;
1195 /* Get minimum frequency according to sorting order */
1196 if (freq_table
[0] < freq_table
[df
->profile
->max_state
- 1])
1197 value
= freq_table
[0];
1199 value
= freq_table
[df
->profile
->max_state
- 1];
1202 df
->min_freq
= value
;
1206 mutex_unlock(&df
->lock
);
1210 static ssize_t
min_freq_show(struct device
*dev
, struct device_attribute
*attr
,
1213 struct devfreq
*df
= to_devfreq(dev
);
1215 return sprintf(buf
, "%lu\n", MAX(df
->scaling_min_freq
, df
->min_freq
));
1218 static ssize_t
max_freq_store(struct device
*dev
, struct device_attribute
*attr
,
1219 const char *buf
, size_t count
)
1221 struct devfreq
*df
= to_devfreq(dev
);
1222 unsigned long value
;
1225 ret
= sscanf(buf
, "%lu", &value
);
1229 mutex_lock(&df
->lock
);
1232 if (value
< df
->min_freq
) {
1237 unsigned long *freq_table
= df
->profile
->freq_table
;
1239 /* Get maximum frequency according to sorting order */
1240 if (freq_table
[0] < freq_table
[df
->profile
->max_state
- 1])
1241 value
= freq_table
[df
->profile
->max_state
- 1];
1243 value
= freq_table
[0];
1246 df
->max_freq
= value
;
1250 mutex_unlock(&df
->lock
);
1253 static DEVICE_ATTR_RW(min_freq
);
1255 static ssize_t
max_freq_show(struct device
*dev
, struct device_attribute
*attr
,
1258 struct devfreq
*df
= to_devfreq(dev
);
1260 return sprintf(buf
, "%lu\n", MIN(df
->scaling_max_freq
, df
->max_freq
));
1262 static DEVICE_ATTR_RW(max_freq
);
1264 static ssize_t
available_frequencies_show(struct device
*d
,
1265 struct device_attribute
*attr
,
1268 struct devfreq
*df
= to_devfreq(d
);
1272 mutex_lock(&df
->lock
);
1274 for (i
= 0; i
< df
->profile
->max_state
; i
++)
1275 count
+= scnprintf(&buf
[count
], (PAGE_SIZE
- count
- 2),
1276 "%lu ", df
->profile
->freq_table
[i
]);
1278 mutex_unlock(&df
->lock
);
1279 /* Truncate the trailing space */
1283 count
+= sprintf(&buf
[count
], "\n");
1287 static DEVICE_ATTR_RO(available_frequencies
);
1289 static ssize_t
trans_stat_show(struct device
*dev
,
1290 struct device_attribute
*attr
, char *buf
)
1292 struct devfreq
*devfreq
= to_devfreq(dev
);
1295 unsigned int max_state
= devfreq
->profile
->max_state
;
1298 return sprintf(buf
, "Not Supported.\n");
1300 mutex_lock(&devfreq
->lock
);
1301 if (!devfreq
->stop_polling
&&
1302 devfreq_update_status(devfreq
, devfreq
->previous_freq
)) {
1303 mutex_unlock(&devfreq
->lock
);
1306 mutex_unlock(&devfreq
->lock
);
1308 len
= sprintf(buf
, " From : To\n");
1309 len
+= sprintf(buf
+ len
, " :");
1310 for (i
= 0; i
< max_state
; i
++)
1311 len
+= sprintf(buf
+ len
, "%10lu",
1312 devfreq
->profile
->freq_table
[i
]);
1314 len
+= sprintf(buf
+ len
, " time(ms)\n");
1316 for (i
= 0; i
< max_state
; i
++) {
1317 if (devfreq
->profile
->freq_table
[i
]
1318 == devfreq
->previous_freq
) {
1319 len
+= sprintf(buf
+ len
, "*");
1321 len
+= sprintf(buf
+ len
, " ");
1323 len
+= sprintf(buf
+ len
, "%10lu:",
1324 devfreq
->profile
->freq_table
[i
]);
1325 for (j
= 0; j
< max_state
; j
++)
1326 len
+= sprintf(buf
+ len
, "%10u",
1327 devfreq
->trans_table
[(i
* max_state
) + j
]);
1328 len
+= sprintf(buf
+ len
, "%10u\n",
1329 jiffies_to_msecs(devfreq
->time_in_state
[i
]));
1332 len
+= sprintf(buf
+ len
, "Total transition : %u\n",
1333 devfreq
->total_trans
);
1336 static DEVICE_ATTR_RO(trans_stat
);
1338 static struct attribute
*devfreq_attrs
[] = {
1339 &dev_attr_name
.attr
,
1340 &dev_attr_governor
.attr
,
1341 &dev_attr_available_governors
.attr
,
1342 &dev_attr_cur_freq
.attr
,
1343 &dev_attr_available_frequencies
.attr
,
1344 &dev_attr_target_freq
.attr
,
1345 &dev_attr_polling_interval
.attr
,
1346 &dev_attr_min_freq
.attr
,
1347 &dev_attr_max_freq
.attr
,
1348 &dev_attr_trans_stat
.attr
,
1351 ATTRIBUTE_GROUPS(devfreq
);
1353 static int __init
devfreq_init(void)
1355 devfreq_class
= class_create(THIS_MODULE
, "devfreq");
1356 if (IS_ERR(devfreq_class
)) {
1357 pr_err("%s: couldn't create class\n", __FILE__
);
1358 return PTR_ERR(devfreq_class
);
1361 devfreq_wq
= create_freezable_workqueue("devfreq_wq");
1363 class_destroy(devfreq_class
);
1364 pr_err("%s: couldn't create workqueue\n", __FILE__
);
1367 devfreq_class
->dev_groups
= devfreq_groups
;
1371 subsys_initcall(devfreq_init
);
1374 * The following are helper functions for devfreq user device drivers with
1379 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1380 * freq value given to target callback.
1381 * @dev: The devfreq user device. (parent of devfreq)
1382 * @freq: The frequency given to target function
1383 * @flags: Flags handed from devfreq framework.
1385 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1388 struct dev_pm_opp
*devfreq_recommended_opp(struct device
*dev
,
1389 unsigned long *freq
,
1392 struct dev_pm_opp
*opp
;
1394 if (flags
& DEVFREQ_FLAG_LEAST_UPPER_BOUND
) {
1395 /* The freq is an upper bound. opp should be lower */
1396 opp
= dev_pm_opp_find_freq_floor(dev
, freq
);
1398 /* If not available, use the closest opp */
1399 if (opp
== ERR_PTR(-ERANGE
))
1400 opp
= dev_pm_opp_find_freq_ceil(dev
, freq
);
1402 /* The freq is an lower bound. opp should be higher */
1403 opp
= dev_pm_opp_find_freq_ceil(dev
, freq
);
1405 /* If not available, use the closest opp */
1406 if (opp
== ERR_PTR(-ERANGE
))
1407 opp
= dev_pm_opp_find_freq_floor(dev
, freq
);
1412 EXPORT_SYMBOL(devfreq_recommended_opp
);
1415 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1416 * for any changes in the OPP availability
1418 * @dev: The devfreq user device. (parent of devfreq)
1419 * @devfreq: The devfreq object.
1421 int devfreq_register_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
1423 return dev_pm_opp_register_notifier(dev
, &devfreq
->nb
);
1425 EXPORT_SYMBOL(devfreq_register_opp_notifier
);
1428 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1429 * notified for any changes in the OPP
1430 * availability changes anymore.
1431 * @dev: The devfreq user device. (parent of devfreq)
1432 * @devfreq: The devfreq object.
1434 * At exit() callback of devfreq_dev_profile, this must be included if
1435 * devfreq_recommended_opp is used.
1437 int devfreq_unregister_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
1439 return dev_pm_opp_unregister_notifier(dev
, &devfreq
->nb
);
1441 EXPORT_SYMBOL(devfreq_unregister_opp_notifier
);
1443 static void devm_devfreq_opp_release(struct device
*dev
, void *res
)
1445 devfreq_unregister_opp_notifier(dev
, *(struct devfreq
**)res
);
1449 * devm_ devfreq_register_opp_notifier()
1450 * - Resource-managed devfreq_register_opp_notifier()
1451 * @dev: The devfreq user device. (parent of devfreq)
1452 * @devfreq: The devfreq object.
1454 int devm_devfreq_register_opp_notifier(struct device
*dev
,
1455 struct devfreq
*devfreq
)
1457 struct devfreq
**ptr
;
1460 ptr
= devres_alloc(devm_devfreq_opp_release
, sizeof(*ptr
), GFP_KERNEL
);
1464 ret
= devfreq_register_opp_notifier(dev
, devfreq
);
1471 devres_add(dev
, ptr
);
1475 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier
);
1478 * devm_devfreq_unregister_opp_notifier()
1479 * - Resource-managed devfreq_unregister_opp_notifier()
1480 * @dev: The devfreq user device. (parent of devfreq)
1481 * @devfreq: The devfreq object.
1483 void devm_devfreq_unregister_opp_notifier(struct device
*dev
,
1484 struct devfreq
*devfreq
)
1486 WARN_ON(devres_release(dev
, devm_devfreq_opp_release
,
1487 devm_devfreq_dev_match
, devfreq
));
1489 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier
);
1492 * devfreq_register_notifier() - Register a driver with devfreq
1493 * @devfreq: The devfreq object.
1494 * @nb: The notifier block to register.
1495 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1497 int devfreq_register_notifier(struct devfreq
*devfreq
,
1498 struct notifier_block
*nb
,
1507 case DEVFREQ_TRANSITION_NOTIFIER
:
1508 ret
= srcu_notifier_chain_register(
1509 &devfreq
->transition_notifier_list
, nb
);
1517 EXPORT_SYMBOL(devfreq_register_notifier
);
1520 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1521 * @devfreq: The devfreq object.
1522 * @nb: The notifier block to be unregistered.
1523 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1525 int devfreq_unregister_notifier(struct devfreq
*devfreq
,
1526 struct notifier_block
*nb
,
1535 case DEVFREQ_TRANSITION_NOTIFIER
:
1536 ret
= srcu_notifier_chain_unregister(
1537 &devfreq
->transition_notifier_list
, nb
);
1545 EXPORT_SYMBOL(devfreq_unregister_notifier
);
1547 struct devfreq_notifier_devres
{
1548 struct devfreq
*devfreq
;
1549 struct notifier_block
*nb
;
1553 static void devm_devfreq_notifier_release(struct device
*dev
, void *res
)
1555 struct devfreq_notifier_devres
*this = res
;
1557 devfreq_unregister_notifier(this->devfreq
, this->nb
, this->list
);
1561 * devm_devfreq_register_notifier()
1562 - Resource-managed devfreq_register_notifier()
1563 * @dev: The devfreq user device. (parent of devfreq)
1564 * @devfreq: The devfreq object.
1565 * @nb: The notifier block to be unregistered.
1566 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1568 int devm_devfreq_register_notifier(struct device
*dev
,
1569 struct devfreq
*devfreq
,
1570 struct notifier_block
*nb
,
1573 struct devfreq_notifier_devres
*ptr
;
1576 ptr
= devres_alloc(devm_devfreq_notifier_release
, sizeof(*ptr
),
1581 ret
= devfreq_register_notifier(devfreq
, nb
, list
);
1587 ptr
->devfreq
= devfreq
;
1590 devres_add(dev
, ptr
);
1594 EXPORT_SYMBOL(devm_devfreq_register_notifier
);
1597 * devm_devfreq_unregister_notifier()
1598 - Resource-managed devfreq_unregister_notifier()
1599 * @dev: The devfreq user device. (parent of devfreq)
1600 * @devfreq: The devfreq object.
1601 * @nb: The notifier block to be unregistered.
1602 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1604 void devm_devfreq_unregister_notifier(struct device
*dev
,
1605 struct devfreq
*devfreq
,
1606 struct notifier_block
*nb
,
1609 WARN_ON(devres_release(dev
, devm_devfreq_notifier_release
,
1610 devm_devfreq_dev_match
, devfreq
));
1612 EXPORT_SYMBOL(devm_devfreq_unregister_notifier
);