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/sched.h>
15 #include <linux/errno.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/slab.h>
20 #include <linux/stat.h>
21 #include <linux/pm_opp.h>
22 #include <linux/devfreq.h>
23 #include <linux/workqueue.h>
24 #include <linux/platform_device.h>
25 #include <linux/list.h>
26 #include <linux/printk.h>
27 #include <linux/hrtimer.h>
31 static struct class *devfreq_class
;
34 * devfreq core provides delayed work based load monitoring helper
35 * functions. Governors can use these or can implement their own
36 * monitoring mechanism.
38 static struct workqueue_struct
*devfreq_wq
;
40 /* The list of all device-devfreq governors */
41 static LIST_HEAD(devfreq_governor_list
);
42 /* The list of all device-devfreq */
43 static LIST_HEAD(devfreq_list
);
44 static DEFINE_MUTEX(devfreq_list_lock
);
47 * find_device_devfreq() - find devfreq struct using device pointer
48 * @dev: device pointer used to lookup device devfreq.
50 * Search the list of device devfreqs and return the matched device's
51 * devfreq info. devfreq_list_lock should be held by the caller.
53 static struct devfreq
*find_device_devfreq(struct device
*dev
)
55 struct devfreq
*tmp_devfreq
;
57 if (IS_ERR_OR_NULL(dev
)) {
58 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__
);
59 return ERR_PTR(-EINVAL
);
61 WARN(!mutex_is_locked(&devfreq_list_lock
),
62 "devfreq_list_lock must be locked.");
64 list_for_each_entry(tmp_devfreq
, &devfreq_list
, node
) {
65 if (tmp_devfreq
->dev
.parent
== dev
)
69 return ERR_PTR(-ENODEV
);
73 * devfreq_get_freq_level() - Lookup freq_table for the frequency
74 * @devfreq: the devfreq instance
75 * @freq: the target frequency
77 static int devfreq_get_freq_level(struct devfreq
*devfreq
, unsigned long freq
)
81 for (lev
= 0; lev
< devfreq
->profile
->max_state
; lev
++)
82 if (freq
== devfreq
->profile
->freq_table
[lev
])
89 * devfreq_set_freq_table() - Initialize freq_table for the frequency
90 * @devfreq: the devfreq instance
92 static void devfreq_set_freq_table(struct devfreq
*devfreq
)
94 struct devfreq_dev_profile
*profile
= devfreq
->profile
;
95 struct dev_pm_opp
*opp
;
99 /* Initialize the freq_table from OPP table */
100 count
= dev_pm_opp_get_opp_count(devfreq
->dev
.parent
);
104 profile
->max_state
= count
;
105 profile
->freq_table
= devm_kcalloc(devfreq
->dev
.parent
,
107 sizeof(*profile
->freq_table
),
109 if (!profile
->freq_table
) {
110 profile
->max_state
= 0;
114 for (i
= 0, freq
= 0; i
< profile
->max_state
; i
++, freq
++) {
115 opp
= dev_pm_opp_find_freq_ceil(devfreq
->dev
.parent
, &freq
);
117 devm_kfree(devfreq
->dev
.parent
, profile
->freq_table
);
118 profile
->max_state
= 0;
122 profile
->freq_table
[i
] = freq
;
127 * devfreq_update_status() - Update statistics of devfreq behavior
128 * @devfreq: the devfreq instance
129 * @freq: the update target frequency
131 int devfreq_update_status(struct devfreq
*devfreq
, unsigned long freq
)
133 int lev
, prev_lev
, ret
= 0;
134 unsigned long cur_time
;
138 /* Immediately exit if previous_freq is not initialized yet. */
139 if (!devfreq
->previous_freq
)
142 prev_lev
= devfreq_get_freq_level(devfreq
, devfreq
->previous_freq
);
148 devfreq
->time_in_state
[prev_lev
] +=
149 cur_time
- devfreq
->last_stat_updated
;
151 lev
= devfreq_get_freq_level(devfreq
, freq
);
157 if (lev
!= prev_lev
) {
158 devfreq
->trans_table
[(prev_lev
*
159 devfreq
->profile
->max_state
) + lev
]++;
160 devfreq
->total_trans
++;
164 devfreq
->last_stat_updated
= cur_time
;
167 EXPORT_SYMBOL(devfreq_update_status
);
170 * find_devfreq_governor() - find devfreq governor from name
171 * @name: name of the governor
173 * Search the list of devfreq governors and return the matched
174 * governor's pointer. devfreq_list_lock should be held by the caller.
176 static struct devfreq_governor
*find_devfreq_governor(const char *name
)
178 struct devfreq_governor
*tmp_governor
;
180 if (IS_ERR_OR_NULL(name
)) {
181 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__
);
182 return ERR_PTR(-EINVAL
);
184 WARN(!mutex_is_locked(&devfreq_list_lock
),
185 "devfreq_list_lock must be locked.");
187 list_for_each_entry(tmp_governor
, &devfreq_governor_list
, node
) {
188 if (!strncmp(tmp_governor
->name
, name
, DEVFREQ_NAME_LEN
))
192 return ERR_PTR(-ENODEV
);
195 static int devfreq_notify_transition(struct devfreq
*devfreq
,
196 struct devfreq_freqs
*freqs
, unsigned int state
)
202 case DEVFREQ_PRECHANGE
:
203 srcu_notifier_call_chain(&devfreq
->transition_notifier_list
,
204 DEVFREQ_PRECHANGE
, freqs
);
207 case DEVFREQ_POSTCHANGE
:
208 srcu_notifier_call_chain(&devfreq
->transition_notifier_list
,
209 DEVFREQ_POSTCHANGE
, freqs
);
218 /* Load monitoring helper functions for governors use */
221 * update_devfreq() - Reevaluate the device and configure frequency.
222 * @devfreq: the devfreq instance.
224 * Note: Lock devfreq->lock before calling update_devfreq
225 * This function is exported for governors.
227 int update_devfreq(struct devfreq
*devfreq
)
229 struct devfreq_freqs freqs
;
230 unsigned long freq
, cur_freq
;
234 if (!mutex_is_locked(&devfreq
->lock
)) {
235 WARN(true, "devfreq->lock must be locked by the caller.\n");
239 if (!devfreq
->governor
)
242 /* Reevaluate the proper frequency */
243 err
= devfreq
->governor
->get_target_freq(devfreq
, &freq
);
248 * Adjust the frequency with user freq and QoS.
250 * List from the highest priority
255 if (devfreq
->min_freq
&& freq
< devfreq
->min_freq
) {
256 freq
= devfreq
->min_freq
;
257 flags
&= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND
; /* Use GLB */
259 if (devfreq
->max_freq
&& freq
> devfreq
->max_freq
) {
260 freq
= devfreq
->max_freq
;
261 flags
|= DEVFREQ_FLAG_LEAST_UPPER_BOUND
; /* Use LUB */
264 if (devfreq
->profile
->get_cur_freq
)
265 devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &cur_freq
);
267 cur_freq
= devfreq
->previous_freq
;
269 freqs
.old
= cur_freq
;
271 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_PRECHANGE
);
273 err
= devfreq
->profile
->target(devfreq
->dev
.parent
, &freq
, flags
);
275 freqs
.new = cur_freq
;
276 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_POSTCHANGE
);
281 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_POSTCHANGE
);
283 if (devfreq
->profile
->freq_table
)
284 if (devfreq_update_status(devfreq
, freq
))
285 dev_err(&devfreq
->dev
,
286 "Couldn't update frequency transition information.\n");
288 devfreq
->previous_freq
= freq
;
291 EXPORT_SYMBOL(update_devfreq
);
294 * devfreq_monitor() - Periodically poll devfreq objects.
295 * @work: the work struct used to run devfreq_monitor periodically.
298 static void devfreq_monitor(struct work_struct
*work
)
301 struct devfreq
*devfreq
= container_of(work
,
302 struct devfreq
, work
.work
);
304 mutex_lock(&devfreq
->lock
);
305 err
= update_devfreq(devfreq
);
307 dev_err(&devfreq
->dev
, "dvfs failed with (%d) error\n", err
);
309 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
310 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
311 mutex_unlock(&devfreq
->lock
);
315 * devfreq_monitor_start() - Start load monitoring of devfreq instance
316 * @devfreq: the devfreq instance.
318 * Helper function for starting devfreq device load monitoing. By
319 * default delayed work based monitoring is supported. Function
320 * to be called from governor in response to DEVFREQ_GOV_START
321 * event when device is added to devfreq framework.
323 void devfreq_monitor_start(struct devfreq
*devfreq
)
325 INIT_DEFERRABLE_WORK(&devfreq
->work
, devfreq_monitor
);
326 if (devfreq
->profile
->polling_ms
)
327 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
328 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
330 EXPORT_SYMBOL(devfreq_monitor_start
);
333 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
334 * @devfreq: the devfreq instance.
336 * Helper function to stop devfreq device load monitoing. Function
337 * to be called from governor in response to DEVFREQ_GOV_STOP
338 * event when device is removed from devfreq framework.
340 void devfreq_monitor_stop(struct devfreq
*devfreq
)
342 cancel_delayed_work_sync(&devfreq
->work
);
344 EXPORT_SYMBOL(devfreq_monitor_stop
);
347 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
348 * @devfreq: the devfreq instance.
350 * Helper function to suspend devfreq device load monitoing. Function
351 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
352 * event or when polling interval is set to zero.
354 * Note: Though this function is same as devfreq_monitor_stop(),
355 * intentionally kept separate to provide hooks for collecting
356 * transition statistics.
358 void devfreq_monitor_suspend(struct devfreq
*devfreq
)
360 mutex_lock(&devfreq
->lock
);
361 if (devfreq
->stop_polling
) {
362 mutex_unlock(&devfreq
->lock
);
366 devfreq_update_status(devfreq
, devfreq
->previous_freq
);
367 devfreq
->stop_polling
= true;
368 mutex_unlock(&devfreq
->lock
);
369 cancel_delayed_work_sync(&devfreq
->work
);
371 EXPORT_SYMBOL(devfreq_monitor_suspend
);
374 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
375 * @devfreq: the devfreq instance.
377 * Helper function to resume devfreq device load monitoing. Function
378 * to be called from governor in response to DEVFREQ_GOV_RESUME
379 * event or when polling interval is set to non-zero.
381 void devfreq_monitor_resume(struct devfreq
*devfreq
)
385 mutex_lock(&devfreq
->lock
);
386 if (!devfreq
->stop_polling
)
389 if (!delayed_work_pending(&devfreq
->work
) &&
390 devfreq
->profile
->polling_ms
)
391 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
392 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
394 devfreq
->last_stat_updated
= jiffies
;
395 devfreq
->stop_polling
= false;
397 if (devfreq
->profile
->get_cur_freq
&&
398 !devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &freq
))
399 devfreq
->previous_freq
= freq
;
402 mutex_unlock(&devfreq
->lock
);
404 EXPORT_SYMBOL(devfreq_monitor_resume
);
407 * devfreq_interval_update() - Update device devfreq monitoring interval
408 * @devfreq: the devfreq instance.
409 * @delay: new polling interval to be set.
411 * Helper function to set new load monitoring polling interval. Function
412 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
414 void devfreq_interval_update(struct devfreq
*devfreq
, unsigned int *delay
)
416 unsigned int cur_delay
= devfreq
->profile
->polling_ms
;
417 unsigned int new_delay
= *delay
;
419 mutex_lock(&devfreq
->lock
);
420 devfreq
->profile
->polling_ms
= new_delay
;
422 if (devfreq
->stop_polling
)
425 /* if new delay is zero, stop polling */
427 mutex_unlock(&devfreq
->lock
);
428 cancel_delayed_work_sync(&devfreq
->work
);
432 /* if current delay is zero, start polling with new delay */
434 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
435 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
439 /* if current delay is greater than new delay, restart polling */
440 if (cur_delay
> new_delay
) {
441 mutex_unlock(&devfreq
->lock
);
442 cancel_delayed_work_sync(&devfreq
->work
);
443 mutex_lock(&devfreq
->lock
);
444 if (!devfreq
->stop_polling
)
445 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
446 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
449 mutex_unlock(&devfreq
->lock
);
451 EXPORT_SYMBOL(devfreq_interval_update
);
454 * devfreq_notifier_call() - Notify that the device frequency requirements
455 * has been changed out of devfreq framework.
456 * @nb: the notifier_block (supposed to be devfreq->nb)
460 * Called by a notifier that uses devfreq->nb.
462 static int devfreq_notifier_call(struct notifier_block
*nb
, unsigned long type
,
465 struct devfreq
*devfreq
= container_of(nb
, struct devfreq
, nb
);
468 mutex_lock(&devfreq
->lock
);
469 ret
= update_devfreq(devfreq
);
470 mutex_unlock(&devfreq
->lock
);
476 * devfreq_dev_release() - Callback for struct device to release the device.
477 * @dev: the devfreq device
479 * Remove devfreq from the list and release its resources.
481 static void devfreq_dev_release(struct device
*dev
)
483 struct devfreq
*devfreq
= to_devfreq(dev
);
485 mutex_lock(&devfreq_list_lock
);
486 if (IS_ERR(find_device_devfreq(devfreq
->dev
.parent
))) {
487 mutex_unlock(&devfreq_list_lock
);
488 dev_warn(&devfreq
->dev
, "releasing devfreq which doesn't exist\n");
491 list_del(&devfreq
->node
);
492 mutex_unlock(&devfreq_list_lock
);
494 if (devfreq
->governor
)
495 devfreq
->governor
->event_handler(devfreq
,
496 DEVFREQ_GOV_STOP
, NULL
);
498 if (devfreq
->profile
->exit
)
499 devfreq
->profile
->exit(devfreq
->dev
.parent
);
501 mutex_destroy(&devfreq
->lock
);
506 * devfreq_add_device() - Add devfreq feature to the device
507 * @dev: the device to add devfreq feature.
508 * @profile: device-specific profile to run devfreq.
509 * @governor_name: name of the policy to choose frequency.
510 * @data: private data for the governor. The devfreq framework does not
513 struct devfreq
*devfreq_add_device(struct device
*dev
,
514 struct devfreq_dev_profile
*profile
,
515 const char *governor_name
,
518 struct devfreq
*devfreq
;
519 struct devfreq_governor
*governor
;
520 static atomic_t devfreq_no
= ATOMIC_INIT(-1);
523 if (!dev
|| !profile
|| !governor_name
) {
524 dev_err(dev
, "%s: Invalid parameters.\n", __func__
);
525 return ERR_PTR(-EINVAL
);
528 mutex_lock(&devfreq_list_lock
);
529 devfreq
= find_device_devfreq(dev
);
530 mutex_unlock(&devfreq_list_lock
);
531 if (!IS_ERR(devfreq
)) {
532 dev_err(dev
, "%s: Unable to create devfreq for the device.\n",
538 devfreq
= kzalloc(sizeof(struct devfreq
), GFP_KERNEL
);
544 mutex_init(&devfreq
->lock
);
545 mutex_lock(&devfreq
->lock
);
546 devfreq
->dev
.parent
= dev
;
547 devfreq
->dev
.class = devfreq_class
;
548 devfreq
->dev
.release
= devfreq_dev_release
;
549 devfreq
->profile
= profile
;
550 strncpy(devfreq
->governor_name
, governor_name
, DEVFREQ_NAME_LEN
);
551 devfreq
->previous_freq
= profile
->initial_freq
;
552 devfreq
->last_status
.current_frequency
= profile
->initial_freq
;
553 devfreq
->data
= data
;
554 devfreq
->nb
.notifier_call
= devfreq_notifier_call
;
556 if (!devfreq
->profile
->max_state
&& !devfreq
->profile
->freq_table
) {
557 mutex_unlock(&devfreq
->lock
);
558 devfreq_set_freq_table(devfreq
);
559 mutex_lock(&devfreq
->lock
);
562 dev_set_name(&devfreq
->dev
, "devfreq%d",
563 atomic_inc_return(&devfreq_no
));
564 err
= device_register(&devfreq
->dev
);
566 mutex_unlock(&devfreq
->lock
);
570 devfreq
->trans_table
= devm_kzalloc(&devfreq
->dev
,
571 sizeof(unsigned int) *
572 devfreq
->profile
->max_state
*
573 devfreq
->profile
->max_state
,
575 devfreq
->time_in_state
= devm_kzalloc(&devfreq
->dev
,
576 sizeof(unsigned long) *
577 devfreq
->profile
->max_state
,
579 devfreq
->last_stat_updated
= jiffies
;
581 srcu_init_notifier_head(&devfreq
->transition_notifier_list
);
583 mutex_unlock(&devfreq
->lock
);
585 mutex_lock(&devfreq_list_lock
);
586 list_add(&devfreq
->node
, &devfreq_list
);
588 governor
= find_devfreq_governor(devfreq
->governor_name
);
589 if (IS_ERR(governor
)) {
590 dev_err(dev
, "%s: Unable to find governor for the device\n",
592 err
= PTR_ERR(governor
);
596 devfreq
->governor
= governor
;
597 err
= devfreq
->governor
->event_handler(devfreq
, DEVFREQ_GOV_START
,
600 dev_err(dev
, "%s: Unable to start governor for the device\n",
604 mutex_unlock(&devfreq_list_lock
);
609 list_del(&devfreq
->node
);
610 mutex_unlock(&devfreq_list_lock
);
612 device_unregister(&devfreq
->dev
);
619 EXPORT_SYMBOL(devfreq_add_device
);
622 * devfreq_remove_device() - Remove devfreq feature from a device.
623 * @devfreq: the devfreq instance to be removed
625 * The opposite of devfreq_add_device().
627 int devfreq_remove_device(struct devfreq
*devfreq
)
632 device_unregister(&devfreq
->dev
);
636 EXPORT_SYMBOL(devfreq_remove_device
);
638 static int devm_devfreq_dev_match(struct device
*dev
, void *res
, void *data
)
640 struct devfreq
**r
= res
;
642 if (WARN_ON(!r
|| !*r
))
648 static void devm_devfreq_dev_release(struct device
*dev
, void *res
)
650 devfreq_remove_device(*(struct devfreq
**)res
);
654 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
655 * @dev: the device to add devfreq feature.
656 * @profile: device-specific profile to run devfreq.
657 * @governor_name: name of the policy to choose frequency.
658 * @data: private data for the governor. The devfreq framework does not
661 * This function manages automatically the memory of devfreq device using device
662 * resource management and simplify the free operation for memory of devfreq
665 struct devfreq
*devm_devfreq_add_device(struct device
*dev
,
666 struct devfreq_dev_profile
*profile
,
667 const char *governor_name
,
670 struct devfreq
**ptr
, *devfreq
;
672 ptr
= devres_alloc(devm_devfreq_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
674 return ERR_PTR(-ENOMEM
);
676 devfreq
= devfreq_add_device(dev
, profile
, governor_name
, data
);
677 if (IS_ERR(devfreq
)) {
679 return ERR_PTR(-ENOMEM
);
683 devres_add(dev
, ptr
);
687 EXPORT_SYMBOL(devm_devfreq_add_device
);
691 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
692 * @dev - instance to the given device
693 * @index - index into list of devfreq
695 * return the instance of devfreq device
697 struct devfreq
*devfreq_get_devfreq_by_phandle(struct device
*dev
, int index
)
699 struct device_node
*node
;
700 struct devfreq
*devfreq
;
703 return ERR_PTR(-EINVAL
);
706 return ERR_PTR(-EINVAL
);
708 node
= of_parse_phandle(dev
->of_node
, "devfreq", index
);
710 return ERR_PTR(-ENODEV
);
712 mutex_lock(&devfreq_list_lock
);
713 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
714 if (devfreq
->dev
.parent
715 && devfreq
->dev
.parent
->of_node
== node
) {
716 mutex_unlock(&devfreq_list_lock
);
721 mutex_unlock(&devfreq_list_lock
);
724 return ERR_PTR(-EPROBE_DEFER
);
727 struct devfreq
*devfreq_get_devfreq_by_phandle(struct device
*dev
, int index
)
729 return ERR_PTR(-ENODEV
);
731 #endif /* CONFIG_OF */
732 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle
);
735 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
736 * @dev: the device to add devfreq feature.
737 * @devfreq: the devfreq instance to be removed
739 void devm_devfreq_remove_device(struct device
*dev
, struct devfreq
*devfreq
)
741 WARN_ON(devres_release(dev
, devm_devfreq_dev_release
,
742 devm_devfreq_dev_match
, devfreq
));
744 EXPORT_SYMBOL(devm_devfreq_remove_device
);
747 * devfreq_suspend_device() - Suspend devfreq of a device.
748 * @devfreq: the devfreq instance to be suspended
750 * This function is intended to be called by the pm callbacks
751 * (e.g., runtime_suspend, suspend) of the device driver that
754 int devfreq_suspend_device(struct devfreq
*devfreq
)
759 if (!devfreq
->governor
)
762 return devfreq
->governor
->event_handler(devfreq
,
763 DEVFREQ_GOV_SUSPEND
, NULL
);
765 EXPORT_SYMBOL(devfreq_suspend_device
);
768 * devfreq_resume_device() - Resume devfreq of a device.
769 * @devfreq: the devfreq instance to be resumed
771 * This function is intended to be called by the pm callbacks
772 * (e.g., runtime_resume, resume) of the device driver that
775 int devfreq_resume_device(struct devfreq
*devfreq
)
780 if (!devfreq
->governor
)
783 return devfreq
->governor
->event_handler(devfreq
,
784 DEVFREQ_GOV_RESUME
, NULL
);
786 EXPORT_SYMBOL(devfreq_resume_device
);
789 * devfreq_add_governor() - Add devfreq governor
790 * @governor: the devfreq governor to be added
792 int devfreq_add_governor(struct devfreq_governor
*governor
)
794 struct devfreq_governor
*g
;
795 struct devfreq
*devfreq
;
799 pr_err("%s: Invalid parameters.\n", __func__
);
803 mutex_lock(&devfreq_list_lock
);
804 g
= find_devfreq_governor(governor
->name
);
806 pr_err("%s: governor %s already registered\n", __func__
,
812 list_add(&governor
->node
, &devfreq_governor_list
);
814 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
816 struct device
*dev
= devfreq
->dev
.parent
;
818 if (!strncmp(devfreq
->governor_name
, governor
->name
,
820 /* The following should never occur */
821 if (devfreq
->governor
) {
823 "%s: Governor %s already present\n",
824 __func__
, devfreq
->governor
->name
);
825 ret
= devfreq
->governor
->event_handler(devfreq
,
826 DEVFREQ_GOV_STOP
, NULL
);
829 "%s: Governor %s stop = %d\n",
831 devfreq
->governor
->name
, ret
);
835 devfreq
->governor
= governor
;
836 ret
= devfreq
->governor
->event_handler(devfreq
,
837 DEVFREQ_GOV_START
, NULL
);
839 dev_warn(dev
, "%s: Governor %s start=%d\n",
840 __func__
, devfreq
->governor
->name
,
847 mutex_unlock(&devfreq_list_lock
);
851 EXPORT_SYMBOL(devfreq_add_governor
);
854 * devfreq_remove_governor() - Remove devfreq feature from a device.
855 * @governor: the devfreq governor to be removed
857 int devfreq_remove_governor(struct devfreq_governor
*governor
)
859 struct devfreq_governor
*g
;
860 struct devfreq
*devfreq
;
864 pr_err("%s: Invalid parameters.\n", __func__
);
868 mutex_lock(&devfreq_list_lock
);
869 g
= find_devfreq_governor(governor
->name
);
871 pr_err("%s: governor %s not registered\n", __func__
,
876 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
878 struct device
*dev
= devfreq
->dev
.parent
;
880 if (!strncmp(devfreq
->governor_name
, governor
->name
,
882 /* we should have a devfreq governor! */
883 if (!devfreq
->governor
) {
884 dev_warn(dev
, "%s: Governor %s NOT present\n",
885 __func__
, governor
->name
);
889 ret
= devfreq
->governor
->event_handler(devfreq
,
890 DEVFREQ_GOV_STOP
, NULL
);
892 dev_warn(dev
, "%s: Governor %s stop=%d\n",
893 __func__
, devfreq
->governor
->name
,
896 devfreq
->governor
= NULL
;
900 list_del(&governor
->node
);
902 mutex_unlock(&devfreq_list_lock
);
906 EXPORT_SYMBOL(devfreq_remove_governor
);
908 static ssize_t
governor_show(struct device
*dev
,
909 struct device_attribute
*attr
, char *buf
)
911 if (!to_devfreq(dev
)->governor
)
914 return sprintf(buf
, "%s\n", to_devfreq(dev
)->governor
->name
);
917 static ssize_t
governor_store(struct device
*dev
, struct device_attribute
*attr
,
918 const char *buf
, size_t count
)
920 struct devfreq
*df
= to_devfreq(dev
);
922 char str_governor
[DEVFREQ_NAME_LEN
+ 1];
923 struct devfreq_governor
*governor
;
925 ret
= sscanf(buf
, "%" __stringify(DEVFREQ_NAME_LEN
) "s", str_governor
);
929 mutex_lock(&devfreq_list_lock
);
930 governor
= find_devfreq_governor(str_governor
);
931 if (IS_ERR(governor
)) {
932 ret
= PTR_ERR(governor
);
935 if (df
->governor
== governor
) {
938 } else if (df
->governor
->immutable
|| governor
->immutable
) {
944 ret
= df
->governor
->event_handler(df
, DEVFREQ_GOV_STOP
, NULL
);
946 dev_warn(dev
, "%s: Governor %s not stopped(%d)\n",
947 __func__
, df
->governor
->name
, ret
);
951 df
->governor
= governor
;
952 strncpy(df
->governor_name
, governor
->name
, DEVFREQ_NAME_LEN
);
953 ret
= df
->governor
->event_handler(df
, DEVFREQ_GOV_START
, NULL
);
955 dev_warn(dev
, "%s: Governor %s not started(%d)\n",
956 __func__
, df
->governor
->name
, ret
);
958 mutex_unlock(&devfreq_list_lock
);
964 static DEVICE_ATTR_RW(governor
);
966 static ssize_t
available_governors_show(struct device
*d
,
967 struct device_attribute
*attr
,
970 struct devfreq
*df
= to_devfreq(d
);
973 mutex_lock(&devfreq_list_lock
);
976 * The devfreq with immutable governor (e.g., passive) shows
979 if (df
->governor
->immutable
) {
980 count
= scnprintf(&buf
[count
], DEVFREQ_NAME_LEN
,
981 "%s ", df
->governor_name
);
983 * The devfreq device shows the registered governor except for
984 * immutable governors such as passive governor .
987 struct devfreq_governor
*governor
;
989 list_for_each_entry(governor
, &devfreq_governor_list
, node
) {
990 if (governor
->immutable
)
992 count
+= scnprintf(&buf
[count
], (PAGE_SIZE
- count
- 2),
993 "%s ", governor
->name
);
997 mutex_unlock(&devfreq_list_lock
);
999 /* Truncate the trailing space */
1003 count
+= sprintf(&buf
[count
], "\n");
1007 static DEVICE_ATTR_RO(available_governors
);
1009 static ssize_t
cur_freq_show(struct device
*dev
, struct device_attribute
*attr
,
1013 struct devfreq
*devfreq
= to_devfreq(dev
);
1015 if (devfreq
->profile
->get_cur_freq
&&
1016 !devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &freq
))
1017 return sprintf(buf
, "%lu\n", freq
);
1019 return sprintf(buf
, "%lu\n", devfreq
->previous_freq
);
1021 static DEVICE_ATTR_RO(cur_freq
);
1023 static ssize_t
target_freq_show(struct device
*dev
,
1024 struct device_attribute
*attr
, char *buf
)
1026 return sprintf(buf
, "%lu\n", to_devfreq(dev
)->previous_freq
);
1028 static DEVICE_ATTR_RO(target_freq
);
1030 static ssize_t
polling_interval_show(struct device
*dev
,
1031 struct device_attribute
*attr
, char *buf
)
1033 return sprintf(buf
, "%d\n", to_devfreq(dev
)->profile
->polling_ms
);
1036 static ssize_t
polling_interval_store(struct device
*dev
,
1037 struct device_attribute
*attr
,
1038 const char *buf
, size_t count
)
1040 struct devfreq
*df
= to_devfreq(dev
);
1047 ret
= sscanf(buf
, "%u", &value
);
1051 df
->governor
->event_handler(df
, DEVFREQ_GOV_INTERVAL
, &value
);
1056 static DEVICE_ATTR_RW(polling_interval
);
1058 static ssize_t
min_freq_store(struct device
*dev
, struct device_attribute
*attr
,
1059 const char *buf
, size_t count
)
1061 struct devfreq
*df
= to_devfreq(dev
);
1062 unsigned long value
;
1066 ret
= sscanf(buf
, "%lu", &value
);
1070 mutex_lock(&df
->lock
);
1072 if (value
&& max
&& value
> max
) {
1077 df
->min_freq
= value
;
1081 mutex_unlock(&df
->lock
);
1085 static ssize_t
max_freq_store(struct device
*dev
, struct device_attribute
*attr
,
1086 const char *buf
, size_t count
)
1088 struct devfreq
*df
= to_devfreq(dev
);
1089 unsigned long value
;
1093 ret
= sscanf(buf
, "%lu", &value
);
1097 mutex_lock(&df
->lock
);
1099 if (value
&& min
&& value
< min
) {
1104 df
->max_freq
= value
;
1108 mutex_unlock(&df
->lock
);
1112 #define show_one(name) \
1113 static ssize_t name##_show \
1114 (struct device *dev, struct device_attribute *attr, char *buf) \
1116 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
1121 static DEVICE_ATTR_RW(min_freq
);
1122 static DEVICE_ATTR_RW(max_freq
);
1124 static ssize_t
available_frequencies_show(struct device
*d
,
1125 struct device_attribute
*attr
,
1128 struct devfreq
*df
= to_devfreq(d
);
1129 struct device
*dev
= df
->dev
.parent
;
1130 struct dev_pm_opp
*opp
;
1132 unsigned long freq
= 0;
1135 opp
= dev_pm_opp_find_freq_ceil(dev
, &freq
);
1139 dev_pm_opp_put(opp
);
1140 count
+= scnprintf(&buf
[count
], (PAGE_SIZE
- count
- 2),
1145 /* Truncate the trailing space */
1149 count
+= sprintf(&buf
[count
], "\n");
1153 static DEVICE_ATTR_RO(available_frequencies
);
1155 static ssize_t
trans_stat_show(struct device
*dev
,
1156 struct device_attribute
*attr
, char *buf
)
1158 struct devfreq
*devfreq
= to_devfreq(dev
);
1161 unsigned int max_state
= devfreq
->profile
->max_state
;
1163 if (!devfreq
->stop_polling
&&
1164 devfreq_update_status(devfreq
, devfreq
->previous_freq
))
1167 return sprintf(buf
, "Not Supported.\n");
1169 len
= sprintf(buf
, " From : To\n");
1170 len
+= sprintf(buf
+ len
, " :");
1171 for (i
= 0; i
< max_state
; i
++)
1172 len
+= sprintf(buf
+ len
, "%10lu",
1173 devfreq
->profile
->freq_table
[i
]);
1175 len
+= sprintf(buf
+ len
, " time(ms)\n");
1177 for (i
= 0; i
< max_state
; i
++) {
1178 if (devfreq
->profile
->freq_table
[i
]
1179 == devfreq
->previous_freq
) {
1180 len
+= sprintf(buf
+ len
, "*");
1182 len
+= sprintf(buf
+ len
, " ");
1184 len
+= sprintf(buf
+ len
, "%10lu:",
1185 devfreq
->profile
->freq_table
[i
]);
1186 for (j
= 0; j
< max_state
; j
++)
1187 len
+= sprintf(buf
+ len
, "%10u",
1188 devfreq
->trans_table
[(i
* max_state
) + j
]);
1189 len
+= sprintf(buf
+ len
, "%10u\n",
1190 jiffies_to_msecs(devfreq
->time_in_state
[i
]));
1193 len
+= sprintf(buf
+ len
, "Total transition : %u\n",
1194 devfreq
->total_trans
);
1197 static DEVICE_ATTR_RO(trans_stat
);
1199 static struct attribute
*devfreq_attrs
[] = {
1200 &dev_attr_governor
.attr
,
1201 &dev_attr_available_governors
.attr
,
1202 &dev_attr_cur_freq
.attr
,
1203 &dev_attr_available_frequencies
.attr
,
1204 &dev_attr_target_freq
.attr
,
1205 &dev_attr_polling_interval
.attr
,
1206 &dev_attr_min_freq
.attr
,
1207 &dev_attr_max_freq
.attr
,
1208 &dev_attr_trans_stat
.attr
,
1211 ATTRIBUTE_GROUPS(devfreq
);
1213 static int __init
devfreq_init(void)
1215 devfreq_class
= class_create(THIS_MODULE
, "devfreq");
1216 if (IS_ERR(devfreq_class
)) {
1217 pr_err("%s: couldn't create class\n", __FILE__
);
1218 return PTR_ERR(devfreq_class
);
1221 devfreq_wq
= create_freezable_workqueue("devfreq_wq");
1223 class_destroy(devfreq_class
);
1224 pr_err("%s: couldn't create workqueue\n", __FILE__
);
1227 devfreq_class
->dev_groups
= devfreq_groups
;
1231 subsys_initcall(devfreq_init
);
1234 * The following are helper functions for devfreq user device drivers with
1239 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1240 * freq value given to target callback.
1241 * @dev: The devfreq user device. (parent of devfreq)
1242 * @freq: The frequency given to target function
1243 * @flags: Flags handed from devfreq framework.
1245 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1248 struct dev_pm_opp
*devfreq_recommended_opp(struct device
*dev
,
1249 unsigned long *freq
,
1252 struct dev_pm_opp
*opp
;
1254 if (flags
& DEVFREQ_FLAG_LEAST_UPPER_BOUND
) {
1255 /* The freq is an upper bound. opp should be lower */
1256 opp
= dev_pm_opp_find_freq_floor(dev
, freq
);
1258 /* If not available, use the closest opp */
1259 if (opp
== ERR_PTR(-ERANGE
))
1260 opp
= dev_pm_opp_find_freq_ceil(dev
, freq
);
1262 /* The freq is an lower bound. opp should be higher */
1263 opp
= dev_pm_opp_find_freq_ceil(dev
, freq
);
1265 /* If not available, use the closest opp */
1266 if (opp
== ERR_PTR(-ERANGE
))
1267 opp
= dev_pm_opp_find_freq_floor(dev
, freq
);
1272 EXPORT_SYMBOL(devfreq_recommended_opp
);
1275 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1276 * for any changes in the OPP availability
1278 * @dev: The devfreq user device. (parent of devfreq)
1279 * @devfreq: The devfreq object.
1281 int devfreq_register_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
1283 return dev_pm_opp_register_notifier(dev
, &devfreq
->nb
);
1285 EXPORT_SYMBOL(devfreq_register_opp_notifier
);
1288 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1289 * notified for any changes in the OPP
1290 * availability changes anymore.
1291 * @dev: The devfreq user device. (parent of devfreq)
1292 * @devfreq: The devfreq object.
1294 * At exit() callback of devfreq_dev_profile, this must be included if
1295 * devfreq_recommended_opp is used.
1297 int devfreq_unregister_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
1299 return dev_pm_opp_unregister_notifier(dev
, &devfreq
->nb
);
1301 EXPORT_SYMBOL(devfreq_unregister_opp_notifier
);
1303 static void devm_devfreq_opp_release(struct device
*dev
, void *res
)
1305 devfreq_unregister_opp_notifier(dev
, *(struct devfreq
**)res
);
1309 * devm_ devfreq_register_opp_notifier()
1310 * - Resource-managed devfreq_register_opp_notifier()
1311 * @dev: The devfreq user device. (parent of devfreq)
1312 * @devfreq: The devfreq object.
1314 int devm_devfreq_register_opp_notifier(struct device
*dev
,
1315 struct devfreq
*devfreq
)
1317 struct devfreq
**ptr
;
1320 ptr
= devres_alloc(devm_devfreq_opp_release
, sizeof(*ptr
), GFP_KERNEL
);
1324 ret
= devfreq_register_opp_notifier(dev
, devfreq
);
1331 devres_add(dev
, ptr
);
1335 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier
);
1338 * devm_devfreq_unregister_opp_notifier()
1339 * - Resource-managed devfreq_unregister_opp_notifier()
1340 * @dev: The devfreq user device. (parent of devfreq)
1341 * @devfreq: The devfreq object.
1343 void devm_devfreq_unregister_opp_notifier(struct device
*dev
,
1344 struct devfreq
*devfreq
)
1346 WARN_ON(devres_release(dev
, devm_devfreq_opp_release
,
1347 devm_devfreq_dev_match
, devfreq
));
1349 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier
);
1352 * devfreq_register_notifier() - Register a driver with devfreq
1353 * @devfreq: The devfreq object.
1354 * @nb: The notifier block to register.
1355 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1357 int devfreq_register_notifier(struct devfreq
*devfreq
,
1358 struct notifier_block
*nb
,
1367 case DEVFREQ_TRANSITION_NOTIFIER
:
1368 ret
= srcu_notifier_chain_register(
1369 &devfreq
->transition_notifier_list
, nb
);
1377 EXPORT_SYMBOL(devfreq_register_notifier
);
1380 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1381 * @devfreq: The devfreq object.
1382 * @nb: The notifier block to be unregistered.
1383 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1385 int devfreq_unregister_notifier(struct devfreq
*devfreq
,
1386 struct notifier_block
*nb
,
1395 case DEVFREQ_TRANSITION_NOTIFIER
:
1396 ret
= srcu_notifier_chain_unregister(
1397 &devfreq
->transition_notifier_list
, nb
);
1405 EXPORT_SYMBOL(devfreq_unregister_notifier
);
1407 struct devfreq_notifier_devres
{
1408 struct devfreq
*devfreq
;
1409 struct notifier_block
*nb
;
1413 static void devm_devfreq_notifier_release(struct device
*dev
, void *res
)
1415 struct devfreq_notifier_devres
*this = res
;
1417 devfreq_unregister_notifier(this->devfreq
, this->nb
, this->list
);
1421 * devm_devfreq_register_notifier()
1422 - Resource-managed devfreq_register_notifier()
1423 * @dev: The devfreq user device. (parent of devfreq)
1424 * @devfreq: The devfreq object.
1425 * @nb: The notifier block to be unregistered.
1426 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1428 int devm_devfreq_register_notifier(struct device
*dev
,
1429 struct devfreq
*devfreq
,
1430 struct notifier_block
*nb
,
1433 struct devfreq_notifier_devres
*ptr
;
1436 ptr
= devres_alloc(devm_devfreq_notifier_release
, sizeof(*ptr
),
1441 ret
= devfreq_register_notifier(devfreq
, nb
, list
);
1447 ptr
->devfreq
= devfreq
;
1450 devres_add(dev
, ptr
);
1454 EXPORT_SYMBOL(devm_devfreq_register_notifier
);
1457 * devm_devfreq_unregister_notifier()
1458 - Resource-managed devfreq_unregister_notifier()
1459 * @dev: The devfreq user device. (parent of devfreq)
1460 * @devfreq: The devfreq object.
1461 * @nb: The notifier block to be unregistered.
1462 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1464 void devm_devfreq_unregister_notifier(struct device
*dev
,
1465 struct devfreq
*devfreq
,
1466 struct notifier_block
*nb
,
1469 WARN_ON(devres_release(dev
, devm_devfreq_notifier_release
,
1470 devm_devfreq_dev_match
, devfreq
));
1472 EXPORT_SYMBOL(devm_devfreq_unregister_notifier
);