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/module.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;
115 for (i
= 0, freq
= 0; i
< profile
->max_state
; i
++, freq
++) {
116 opp
= dev_pm_opp_find_freq_ceil(devfreq
->dev
.parent
, &freq
);
118 devm_kfree(devfreq
->dev
.parent
, profile
->freq_table
);
119 profile
->max_state
= 0;
123 profile
->freq_table
[i
] = freq
;
129 * devfreq_update_status() - Update statistics of devfreq behavior
130 * @devfreq: the devfreq instance
131 * @freq: the update target frequency
133 static int devfreq_update_status(struct devfreq
*devfreq
, unsigned long freq
)
135 int lev
, prev_lev
, ret
= 0;
136 unsigned long cur_time
;
140 prev_lev
= devfreq_get_freq_level(devfreq
, devfreq
->previous_freq
);
146 devfreq
->time_in_state
[prev_lev
] +=
147 cur_time
- devfreq
->last_stat_updated
;
149 lev
= devfreq_get_freq_level(devfreq
, freq
);
155 if (lev
!= prev_lev
) {
156 devfreq
->trans_table
[(prev_lev
*
157 devfreq
->profile
->max_state
) + lev
]++;
158 devfreq
->total_trans
++;
162 devfreq
->last_stat_updated
= cur_time
;
167 * find_devfreq_governor() - find devfreq governor from name
168 * @name: name of the governor
170 * Search the list of devfreq governors and return the matched
171 * governor's pointer. devfreq_list_lock should be held by the caller.
173 static struct devfreq_governor
*find_devfreq_governor(const char *name
)
175 struct devfreq_governor
*tmp_governor
;
177 if (IS_ERR_OR_NULL(name
)) {
178 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__
);
179 return ERR_PTR(-EINVAL
);
181 WARN(!mutex_is_locked(&devfreq_list_lock
),
182 "devfreq_list_lock must be locked.");
184 list_for_each_entry(tmp_governor
, &devfreq_governor_list
, node
) {
185 if (!strncmp(tmp_governor
->name
, name
, DEVFREQ_NAME_LEN
))
189 return ERR_PTR(-ENODEV
);
192 static int devfreq_notify_transition(struct devfreq
*devfreq
,
193 struct devfreq_freqs
*freqs
, unsigned int state
)
199 case DEVFREQ_PRECHANGE
:
200 srcu_notifier_call_chain(&devfreq
->transition_notifier_list
,
201 DEVFREQ_PRECHANGE
, freqs
);
204 case DEVFREQ_POSTCHANGE
:
205 srcu_notifier_call_chain(&devfreq
->transition_notifier_list
,
206 DEVFREQ_POSTCHANGE
, freqs
);
215 /* Load monitoring helper functions for governors use */
218 * update_devfreq() - Reevaluate the device and configure frequency.
219 * @devfreq: the devfreq instance.
221 * Note: Lock devfreq->lock before calling update_devfreq
222 * This function is exported for governors.
224 int update_devfreq(struct devfreq
*devfreq
)
226 struct devfreq_freqs freqs
;
227 unsigned long freq
, cur_freq
;
231 if (!mutex_is_locked(&devfreq
->lock
)) {
232 WARN(true, "devfreq->lock must be locked by the caller.\n");
236 if (!devfreq
->governor
)
239 /* Reevaluate the proper frequency */
240 err
= devfreq
->governor
->get_target_freq(devfreq
, &freq
);
245 * Adjust the frequency with user freq and QoS.
247 * List from the highest priority
252 if (devfreq
->min_freq
&& freq
< devfreq
->min_freq
) {
253 freq
= devfreq
->min_freq
;
254 flags
&= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND
; /* Use GLB */
256 if (devfreq
->max_freq
&& freq
> devfreq
->max_freq
) {
257 freq
= devfreq
->max_freq
;
258 flags
|= DEVFREQ_FLAG_LEAST_UPPER_BOUND
; /* Use LUB */
261 if (devfreq
->profile
->get_cur_freq
)
262 devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &cur_freq
);
264 cur_freq
= devfreq
->previous_freq
;
266 freqs
.old
= cur_freq
;
268 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_PRECHANGE
);
270 err
= devfreq
->profile
->target(devfreq
->dev
.parent
, &freq
, flags
);
272 freqs
.new = cur_freq
;
273 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_POSTCHANGE
);
278 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_POSTCHANGE
);
280 if (devfreq
->profile
->freq_table
)
281 if (devfreq_update_status(devfreq
, freq
))
282 dev_err(&devfreq
->dev
,
283 "Couldn't update frequency transition information.\n");
285 devfreq
->previous_freq
= freq
;
288 EXPORT_SYMBOL(update_devfreq
);
291 * devfreq_monitor() - Periodically poll devfreq objects.
292 * @work: the work struct used to run devfreq_monitor periodically.
295 static void devfreq_monitor(struct work_struct
*work
)
298 struct devfreq
*devfreq
= container_of(work
,
299 struct devfreq
, work
.work
);
301 mutex_lock(&devfreq
->lock
);
302 err
= update_devfreq(devfreq
);
304 dev_err(&devfreq
->dev
, "dvfs failed with (%d) error\n", err
);
306 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
307 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
308 mutex_unlock(&devfreq
->lock
);
312 * devfreq_monitor_start() - Start load monitoring of devfreq instance
313 * @devfreq: the devfreq instance.
315 * Helper function for starting devfreq device load monitoing. By
316 * default delayed work based monitoring is supported. Function
317 * to be called from governor in response to DEVFREQ_GOV_START
318 * event when device is added to devfreq framework.
320 void devfreq_monitor_start(struct devfreq
*devfreq
)
322 INIT_DEFERRABLE_WORK(&devfreq
->work
, devfreq_monitor
);
323 if (devfreq
->profile
->polling_ms
)
324 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
325 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
327 EXPORT_SYMBOL(devfreq_monitor_start
);
330 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
331 * @devfreq: the devfreq instance.
333 * Helper function to stop devfreq device load monitoing. Function
334 * to be called from governor in response to DEVFREQ_GOV_STOP
335 * event when device is removed from devfreq framework.
337 void devfreq_monitor_stop(struct devfreq
*devfreq
)
339 cancel_delayed_work_sync(&devfreq
->work
);
341 EXPORT_SYMBOL(devfreq_monitor_stop
);
344 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
345 * @devfreq: the devfreq instance.
347 * Helper function to suspend devfreq device load monitoing. Function
348 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
349 * event or when polling interval is set to zero.
351 * Note: Though this function is same as devfreq_monitor_stop(),
352 * intentionally kept separate to provide hooks for collecting
353 * transition statistics.
355 void devfreq_monitor_suspend(struct devfreq
*devfreq
)
357 mutex_lock(&devfreq
->lock
);
358 if (devfreq
->stop_polling
) {
359 mutex_unlock(&devfreq
->lock
);
363 devfreq_update_status(devfreq
, devfreq
->previous_freq
);
364 devfreq
->stop_polling
= true;
365 mutex_unlock(&devfreq
->lock
);
366 cancel_delayed_work_sync(&devfreq
->work
);
368 EXPORT_SYMBOL(devfreq_monitor_suspend
);
371 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
372 * @devfreq: the devfreq instance.
374 * Helper function to resume devfreq device load monitoing. Function
375 * to be called from governor in response to DEVFREQ_GOV_RESUME
376 * event or when polling interval is set to non-zero.
378 void devfreq_monitor_resume(struct devfreq
*devfreq
)
382 mutex_lock(&devfreq
->lock
);
383 if (!devfreq
->stop_polling
)
386 if (!delayed_work_pending(&devfreq
->work
) &&
387 devfreq
->profile
->polling_ms
)
388 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
389 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
391 devfreq
->last_stat_updated
= jiffies
;
392 devfreq
->stop_polling
= false;
394 if (devfreq
->profile
->get_cur_freq
&&
395 !devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &freq
))
396 devfreq
->previous_freq
= freq
;
399 mutex_unlock(&devfreq
->lock
);
401 EXPORT_SYMBOL(devfreq_monitor_resume
);
404 * devfreq_interval_update() - Update device devfreq monitoring interval
405 * @devfreq: the devfreq instance.
406 * @delay: new polling interval to be set.
408 * Helper function to set new load monitoring polling interval. Function
409 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
411 void devfreq_interval_update(struct devfreq
*devfreq
, unsigned int *delay
)
413 unsigned int cur_delay
= devfreq
->profile
->polling_ms
;
414 unsigned int new_delay
= *delay
;
416 mutex_lock(&devfreq
->lock
);
417 devfreq
->profile
->polling_ms
= new_delay
;
419 if (devfreq
->stop_polling
)
422 /* if new delay is zero, stop polling */
424 mutex_unlock(&devfreq
->lock
);
425 cancel_delayed_work_sync(&devfreq
->work
);
429 /* if current delay is zero, start polling with new delay */
431 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
432 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
436 /* if current delay is greater than new delay, restart polling */
437 if (cur_delay
> new_delay
) {
438 mutex_unlock(&devfreq
->lock
);
439 cancel_delayed_work_sync(&devfreq
->work
);
440 mutex_lock(&devfreq
->lock
);
441 if (!devfreq
->stop_polling
)
442 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
443 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
446 mutex_unlock(&devfreq
->lock
);
448 EXPORT_SYMBOL(devfreq_interval_update
);
451 * devfreq_notifier_call() - Notify that the device frequency requirements
452 * has been changed out of devfreq framework.
453 * @nb: the notifier_block (supposed to be devfreq->nb)
457 * Called by a notifier that uses devfreq->nb.
459 static int devfreq_notifier_call(struct notifier_block
*nb
, unsigned long type
,
462 struct devfreq
*devfreq
= container_of(nb
, struct devfreq
, nb
);
465 mutex_lock(&devfreq
->lock
);
466 ret
= update_devfreq(devfreq
);
467 mutex_unlock(&devfreq
->lock
);
473 * _remove_devfreq() - Remove devfreq from the list and release its resources.
474 * @devfreq: the devfreq struct
476 static void _remove_devfreq(struct devfreq
*devfreq
)
478 mutex_lock(&devfreq_list_lock
);
479 if (IS_ERR(find_device_devfreq(devfreq
->dev
.parent
))) {
480 mutex_unlock(&devfreq_list_lock
);
481 dev_warn(&devfreq
->dev
, "releasing devfreq which doesn't exist\n");
484 list_del(&devfreq
->node
);
485 mutex_unlock(&devfreq_list_lock
);
487 if (devfreq
->governor
)
488 devfreq
->governor
->event_handler(devfreq
,
489 DEVFREQ_GOV_STOP
, NULL
);
491 if (devfreq
->profile
->exit
)
492 devfreq
->profile
->exit(devfreq
->dev
.parent
);
494 mutex_destroy(&devfreq
->lock
);
499 * devfreq_dev_release() - Callback for struct device to release the device.
500 * @dev: the devfreq device
502 * This calls _remove_devfreq() if _remove_devfreq() is not called.
504 static void devfreq_dev_release(struct device
*dev
)
506 struct devfreq
*devfreq
= to_devfreq(dev
);
508 _remove_devfreq(devfreq
);
512 * devfreq_add_device() - Add devfreq feature to the device
513 * @dev: the device to add devfreq feature.
514 * @profile: device-specific profile to run devfreq.
515 * @governor_name: name of the policy to choose frequency.
516 * @data: private data for the governor. The devfreq framework does not
519 struct devfreq
*devfreq_add_device(struct device
*dev
,
520 struct devfreq_dev_profile
*profile
,
521 const char *governor_name
,
524 struct devfreq
*devfreq
;
525 struct devfreq_governor
*governor
;
528 if (!dev
|| !profile
|| !governor_name
) {
529 dev_err(dev
, "%s: Invalid parameters.\n", __func__
);
530 return ERR_PTR(-EINVAL
);
533 mutex_lock(&devfreq_list_lock
);
534 devfreq
= find_device_devfreq(dev
);
535 mutex_unlock(&devfreq_list_lock
);
536 if (!IS_ERR(devfreq
)) {
537 dev_err(dev
, "%s: Unable to create devfreq for the device. It already has one.\n", __func__
);
542 devfreq
= kzalloc(sizeof(struct devfreq
), GFP_KERNEL
);
544 dev_err(dev
, "%s: Unable to create devfreq for the device\n",
550 mutex_init(&devfreq
->lock
);
551 mutex_lock(&devfreq
->lock
);
552 devfreq
->dev
.parent
= dev
;
553 devfreq
->dev
.class = devfreq_class
;
554 devfreq
->dev
.release
= devfreq_dev_release
;
555 devfreq
->profile
= profile
;
556 strncpy(devfreq
->governor_name
, governor_name
, DEVFREQ_NAME_LEN
);
557 devfreq
->previous_freq
= profile
->initial_freq
;
558 devfreq
->last_status
.current_frequency
= profile
->initial_freq
;
559 devfreq
->data
= data
;
560 devfreq
->nb
.notifier_call
= devfreq_notifier_call
;
562 if (!devfreq
->profile
->max_state
&& !devfreq
->profile
->freq_table
) {
563 mutex_unlock(&devfreq
->lock
);
564 devfreq_set_freq_table(devfreq
);
565 mutex_lock(&devfreq
->lock
);
568 dev_set_name(&devfreq
->dev
, "%s", dev_name(dev
));
569 err
= device_register(&devfreq
->dev
);
571 mutex_unlock(&devfreq
->lock
);
575 devfreq
->trans_table
= devm_kzalloc(&devfreq
->dev
, sizeof(unsigned int) *
576 devfreq
->profile
->max_state
*
577 devfreq
->profile
->max_state
,
579 devfreq
->time_in_state
= devm_kzalloc(&devfreq
->dev
, sizeof(unsigned long) *
580 devfreq
->profile
->max_state
,
582 devfreq
->last_stat_updated
= jiffies
;
584 srcu_init_notifier_head(&devfreq
->transition_notifier_list
);
586 mutex_unlock(&devfreq
->lock
);
588 mutex_lock(&devfreq_list_lock
);
589 list_add(&devfreq
->node
, &devfreq_list
);
591 governor
= find_devfreq_governor(devfreq
->governor_name
);
592 if (!IS_ERR(governor
))
593 devfreq
->governor
= governor
;
594 if (devfreq
->governor
)
595 err
= devfreq
->governor
->event_handler(devfreq
,
596 DEVFREQ_GOV_START
, NULL
);
597 mutex_unlock(&devfreq_list_lock
);
599 dev_err(dev
, "%s: Unable to start governor for the device\n",
607 list_del(&devfreq
->node
);
608 device_unregister(&devfreq
->dev
);
612 EXPORT_SYMBOL(devfreq_add_device
);
615 * devfreq_remove_device() - Remove devfreq feature from a device.
616 * @devfreq: the devfreq instance to be removed
618 * The opposite of devfreq_add_device().
620 int devfreq_remove_device(struct devfreq
*devfreq
)
625 device_unregister(&devfreq
->dev
);
629 EXPORT_SYMBOL(devfreq_remove_device
);
631 static int devm_devfreq_dev_match(struct device
*dev
, void *res
, void *data
)
633 struct devfreq
**r
= res
;
635 if (WARN_ON(!r
|| !*r
))
641 static void devm_devfreq_dev_release(struct device
*dev
, void *res
)
643 devfreq_remove_device(*(struct devfreq
**)res
);
647 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
648 * @dev: the device to add devfreq feature.
649 * @profile: device-specific profile to run devfreq.
650 * @governor_name: name of the policy to choose frequency.
651 * @data: private data for the governor. The devfreq framework does not
654 * This function manages automatically the memory of devfreq device using device
655 * resource management and simplify the free operation for memory of devfreq
658 struct devfreq
*devm_devfreq_add_device(struct device
*dev
,
659 struct devfreq_dev_profile
*profile
,
660 const char *governor_name
,
663 struct devfreq
**ptr
, *devfreq
;
665 ptr
= devres_alloc(devm_devfreq_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
667 return ERR_PTR(-ENOMEM
);
669 devfreq
= devfreq_add_device(dev
, profile
, governor_name
, data
);
670 if (IS_ERR(devfreq
)) {
672 return ERR_PTR(-ENOMEM
);
676 devres_add(dev
, ptr
);
680 EXPORT_SYMBOL(devm_devfreq_add_device
);
684 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
685 * @dev - instance to the given device
686 * @index - index into list of devfreq
688 * return the instance of devfreq device
690 struct devfreq
*devfreq_get_devfreq_by_phandle(struct device
*dev
, int index
)
692 struct device_node
*node
;
693 struct devfreq
*devfreq
;
696 return ERR_PTR(-EINVAL
);
699 return ERR_PTR(-EINVAL
);
701 node
= of_parse_phandle(dev
->of_node
, "devfreq", index
);
703 return ERR_PTR(-ENODEV
);
705 mutex_lock(&devfreq_list_lock
);
706 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
707 if (devfreq
->dev
.parent
708 && devfreq
->dev
.parent
->of_node
== node
) {
709 mutex_unlock(&devfreq_list_lock
);
713 mutex_unlock(&devfreq_list_lock
);
715 return ERR_PTR(-EPROBE_DEFER
);
718 struct devfreq
*devfreq_get_devfreq_by_phandle(struct device
*dev
, int index
)
720 return ERR_PTR(-ENODEV
);
722 #endif /* CONFIG_OF */
723 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle
);
726 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
727 * @dev: the device to add devfreq feature.
728 * @devfreq: the devfreq instance to be removed
730 void devm_devfreq_remove_device(struct device
*dev
, struct devfreq
*devfreq
)
732 WARN_ON(devres_release(dev
, devm_devfreq_dev_release
,
733 devm_devfreq_dev_match
, devfreq
));
735 EXPORT_SYMBOL(devm_devfreq_remove_device
);
738 * devfreq_suspend_device() - Suspend devfreq of a device.
739 * @devfreq: the devfreq instance to be suspended
741 * This function is intended to be called by the pm callbacks
742 * (e.g., runtime_suspend, suspend) of the device driver that
745 int devfreq_suspend_device(struct devfreq
*devfreq
)
750 if (!devfreq
->governor
)
753 return devfreq
->governor
->event_handler(devfreq
,
754 DEVFREQ_GOV_SUSPEND
, NULL
);
756 EXPORT_SYMBOL(devfreq_suspend_device
);
759 * devfreq_resume_device() - Resume devfreq of a device.
760 * @devfreq: the devfreq instance to be resumed
762 * This function is intended to be called by the pm callbacks
763 * (e.g., runtime_resume, resume) of the device driver that
766 int devfreq_resume_device(struct devfreq
*devfreq
)
771 if (!devfreq
->governor
)
774 return devfreq
->governor
->event_handler(devfreq
,
775 DEVFREQ_GOV_RESUME
, NULL
);
777 EXPORT_SYMBOL(devfreq_resume_device
);
780 * devfreq_add_governor() - Add devfreq governor
781 * @governor: the devfreq governor to be added
783 int devfreq_add_governor(struct devfreq_governor
*governor
)
785 struct devfreq_governor
*g
;
786 struct devfreq
*devfreq
;
790 pr_err("%s: Invalid parameters.\n", __func__
);
794 mutex_lock(&devfreq_list_lock
);
795 g
= find_devfreq_governor(governor
->name
);
797 pr_err("%s: governor %s already registered\n", __func__
,
803 list_add(&governor
->node
, &devfreq_governor_list
);
805 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
807 struct device
*dev
= devfreq
->dev
.parent
;
809 if (!strncmp(devfreq
->governor_name
, governor
->name
,
811 /* The following should never occur */
812 if (devfreq
->governor
) {
814 "%s: Governor %s already present\n",
815 __func__
, devfreq
->governor
->name
);
816 ret
= devfreq
->governor
->event_handler(devfreq
,
817 DEVFREQ_GOV_STOP
, NULL
);
820 "%s: Governor %s stop = %d\n",
822 devfreq
->governor
->name
, ret
);
826 devfreq
->governor
= governor
;
827 ret
= devfreq
->governor
->event_handler(devfreq
,
828 DEVFREQ_GOV_START
, NULL
);
830 dev_warn(dev
, "%s: Governor %s start=%d\n",
831 __func__
, devfreq
->governor
->name
,
838 mutex_unlock(&devfreq_list_lock
);
842 EXPORT_SYMBOL(devfreq_add_governor
);
845 * devfreq_remove_device() - Remove devfreq feature from a device.
846 * @governor: the devfreq governor to be removed
848 int devfreq_remove_governor(struct devfreq_governor
*governor
)
850 struct devfreq_governor
*g
;
851 struct devfreq
*devfreq
;
855 pr_err("%s: Invalid parameters.\n", __func__
);
859 mutex_lock(&devfreq_list_lock
);
860 g
= find_devfreq_governor(governor
->name
);
862 pr_err("%s: governor %s not registered\n", __func__
,
867 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
869 struct device
*dev
= devfreq
->dev
.parent
;
871 if (!strncmp(devfreq
->governor_name
, governor
->name
,
873 /* we should have a devfreq governor! */
874 if (!devfreq
->governor
) {
875 dev_warn(dev
, "%s: Governor %s NOT present\n",
876 __func__
, governor
->name
);
880 ret
= devfreq
->governor
->event_handler(devfreq
,
881 DEVFREQ_GOV_STOP
, NULL
);
883 dev_warn(dev
, "%s: Governor %s stop=%d\n",
884 __func__
, devfreq
->governor
->name
,
887 devfreq
->governor
= NULL
;
891 list_del(&governor
->node
);
893 mutex_unlock(&devfreq_list_lock
);
897 EXPORT_SYMBOL(devfreq_remove_governor
);
899 static ssize_t
governor_show(struct device
*dev
,
900 struct device_attribute
*attr
, char *buf
)
902 if (!to_devfreq(dev
)->governor
)
905 return sprintf(buf
, "%s\n", to_devfreq(dev
)->governor
->name
);
908 static ssize_t
governor_store(struct device
*dev
, struct device_attribute
*attr
,
909 const char *buf
, size_t count
)
911 struct devfreq
*df
= to_devfreq(dev
);
913 char str_governor
[DEVFREQ_NAME_LEN
+ 1];
914 struct devfreq_governor
*governor
;
916 ret
= sscanf(buf
, "%" __stringify(DEVFREQ_NAME_LEN
) "s", str_governor
);
920 mutex_lock(&devfreq_list_lock
);
921 governor
= find_devfreq_governor(str_governor
);
922 if (IS_ERR(governor
)) {
923 ret
= PTR_ERR(governor
);
926 if (df
->governor
== governor
) {
932 ret
= df
->governor
->event_handler(df
, DEVFREQ_GOV_STOP
, NULL
);
934 dev_warn(dev
, "%s: Governor %s not stopped(%d)\n",
935 __func__
, df
->governor
->name
, ret
);
939 df
->governor
= governor
;
940 strncpy(df
->governor_name
, governor
->name
, DEVFREQ_NAME_LEN
);
941 ret
= df
->governor
->event_handler(df
, DEVFREQ_GOV_START
, NULL
);
943 dev_warn(dev
, "%s: Governor %s not started(%d)\n",
944 __func__
, df
->governor
->name
, ret
);
946 mutex_unlock(&devfreq_list_lock
);
952 static DEVICE_ATTR_RW(governor
);
954 static ssize_t
available_governors_show(struct device
*d
,
955 struct device_attribute
*attr
,
958 struct devfreq_governor
*tmp_governor
;
961 mutex_lock(&devfreq_list_lock
);
962 list_for_each_entry(tmp_governor
, &devfreq_governor_list
, node
)
963 count
+= scnprintf(&buf
[count
], (PAGE_SIZE
- count
- 2),
964 "%s ", tmp_governor
->name
);
965 mutex_unlock(&devfreq_list_lock
);
967 /* Truncate the trailing space */
971 count
+= sprintf(&buf
[count
], "\n");
975 static DEVICE_ATTR_RO(available_governors
);
977 static ssize_t
cur_freq_show(struct device
*dev
, struct device_attribute
*attr
,
981 struct devfreq
*devfreq
= to_devfreq(dev
);
983 if (devfreq
->profile
->get_cur_freq
&&
984 !devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &freq
))
985 return sprintf(buf
, "%lu\n", freq
);
987 return sprintf(buf
, "%lu\n", devfreq
->previous_freq
);
989 static DEVICE_ATTR_RO(cur_freq
);
991 static ssize_t
target_freq_show(struct device
*dev
,
992 struct device_attribute
*attr
, char *buf
)
994 return sprintf(buf
, "%lu\n", to_devfreq(dev
)->previous_freq
);
996 static DEVICE_ATTR_RO(target_freq
);
998 static ssize_t
polling_interval_show(struct device
*dev
,
999 struct device_attribute
*attr
, char *buf
)
1001 return sprintf(buf
, "%d\n", to_devfreq(dev
)->profile
->polling_ms
);
1004 static ssize_t
polling_interval_store(struct device
*dev
,
1005 struct device_attribute
*attr
,
1006 const char *buf
, size_t count
)
1008 struct devfreq
*df
= to_devfreq(dev
);
1015 ret
= sscanf(buf
, "%u", &value
);
1019 df
->governor
->event_handler(df
, DEVFREQ_GOV_INTERVAL
, &value
);
1024 static DEVICE_ATTR_RW(polling_interval
);
1026 static ssize_t
min_freq_store(struct device
*dev
, struct device_attribute
*attr
,
1027 const char *buf
, size_t count
)
1029 struct devfreq
*df
= to_devfreq(dev
);
1030 unsigned long value
;
1034 ret
= sscanf(buf
, "%lu", &value
);
1038 mutex_lock(&df
->lock
);
1040 if (value
&& max
&& value
> max
) {
1045 df
->min_freq
= value
;
1049 mutex_unlock(&df
->lock
);
1053 static ssize_t
max_freq_store(struct device
*dev
, struct device_attribute
*attr
,
1054 const char *buf
, size_t count
)
1056 struct devfreq
*df
= to_devfreq(dev
);
1057 unsigned long value
;
1061 ret
= sscanf(buf
, "%lu", &value
);
1065 mutex_lock(&df
->lock
);
1067 if (value
&& min
&& value
< min
) {
1072 df
->max_freq
= value
;
1076 mutex_unlock(&df
->lock
);
1080 #define show_one(name) \
1081 static ssize_t name##_show \
1082 (struct device *dev, struct device_attribute *attr, char *buf) \
1084 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
1089 static DEVICE_ATTR_RW(min_freq
);
1090 static DEVICE_ATTR_RW(max_freq
);
1092 static ssize_t
available_frequencies_show(struct device
*d
,
1093 struct device_attribute
*attr
,
1096 struct devfreq
*df
= to_devfreq(d
);
1097 struct device
*dev
= df
->dev
.parent
;
1098 struct dev_pm_opp
*opp
;
1100 unsigned long freq
= 0;
1104 opp
= dev_pm_opp_find_freq_ceil(dev
, &freq
);
1108 count
+= scnprintf(&buf
[count
], (PAGE_SIZE
- count
- 2),
1114 /* Truncate the trailing space */
1118 count
+= sprintf(&buf
[count
], "\n");
1122 static DEVICE_ATTR_RO(available_frequencies
);
1124 static ssize_t
trans_stat_show(struct device
*dev
,
1125 struct device_attribute
*attr
, char *buf
)
1127 struct devfreq
*devfreq
= to_devfreq(dev
);
1130 unsigned int max_state
= devfreq
->profile
->max_state
;
1132 if (!devfreq
->stop_polling
&&
1133 devfreq_update_status(devfreq
, devfreq
->previous_freq
))
1136 return sprintf(buf
, "Not Supported.\n");
1138 len
= sprintf(buf
, " From : To\n");
1139 len
+= sprintf(buf
+ len
, " :");
1140 for (i
= 0; i
< max_state
; i
++)
1141 len
+= sprintf(buf
+ len
, "%10lu",
1142 devfreq
->profile
->freq_table
[i
]);
1144 len
+= sprintf(buf
+ len
, " time(ms)\n");
1146 for (i
= 0; i
< max_state
; i
++) {
1147 if (devfreq
->profile
->freq_table
[i
]
1148 == devfreq
->previous_freq
) {
1149 len
+= sprintf(buf
+ len
, "*");
1151 len
+= sprintf(buf
+ len
, " ");
1153 len
+= sprintf(buf
+ len
, "%10lu:",
1154 devfreq
->profile
->freq_table
[i
]);
1155 for (j
= 0; j
< max_state
; j
++)
1156 len
+= sprintf(buf
+ len
, "%10u",
1157 devfreq
->trans_table
[(i
* max_state
) + j
]);
1158 len
+= sprintf(buf
+ len
, "%10u\n",
1159 jiffies_to_msecs(devfreq
->time_in_state
[i
]));
1162 len
+= sprintf(buf
+ len
, "Total transition : %u\n",
1163 devfreq
->total_trans
);
1166 static DEVICE_ATTR_RO(trans_stat
);
1168 static struct attribute
*devfreq_attrs
[] = {
1169 &dev_attr_governor
.attr
,
1170 &dev_attr_available_governors
.attr
,
1171 &dev_attr_cur_freq
.attr
,
1172 &dev_attr_available_frequencies
.attr
,
1173 &dev_attr_target_freq
.attr
,
1174 &dev_attr_polling_interval
.attr
,
1175 &dev_attr_min_freq
.attr
,
1176 &dev_attr_max_freq
.attr
,
1177 &dev_attr_trans_stat
.attr
,
1180 ATTRIBUTE_GROUPS(devfreq
);
1182 static int __init
devfreq_init(void)
1184 devfreq_class
= class_create(THIS_MODULE
, "devfreq");
1185 if (IS_ERR(devfreq_class
)) {
1186 pr_err("%s: couldn't create class\n", __FILE__
);
1187 return PTR_ERR(devfreq_class
);
1190 devfreq_wq
= create_freezable_workqueue("devfreq_wq");
1192 class_destroy(devfreq_class
);
1193 pr_err("%s: couldn't create workqueue\n", __FILE__
);
1196 devfreq_class
->dev_groups
= devfreq_groups
;
1200 subsys_initcall(devfreq_init
);
1202 static void __exit
devfreq_exit(void)
1204 class_destroy(devfreq_class
);
1205 destroy_workqueue(devfreq_wq
);
1207 module_exit(devfreq_exit
);
1210 * The followings are helper functions for devfreq user device drivers with
1215 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1216 * freq value given to target callback.
1217 * @dev: The devfreq user device. (parent of devfreq)
1218 * @freq: The frequency given to target function
1219 * @flags: Flags handed from devfreq framework.
1221 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1222 * protected pointer. The reason for the same is that the opp pointer which is
1223 * returned will remain valid for use with opp_get_{voltage, freq} only while
1224 * under the locked area. The pointer returned must be used prior to unlocking
1225 * with rcu_read_unlock() to maintain the integrity of the pointer.
1227 struct dev_pm_opp
*devfreq_recommended_opp(struct device
*dev
,
1228 unsigned long *freq
,
1231 struct dev_pm_opp
*opp
;
1233 if (flags
& DEVFREQ_FLAG_LEAST_UPPER_BOUND
) {
1234 /* The freq is an upper bound. opp should be lower */
1235 opp
= dev_pm_opp_find_freq_floor(dev
, freq
);
1237 /* If not available, use the closest opp */
1238 if (opp
== ERR_PTR(-ERANGE
))
1239 opp
= dev_pm_opp_find_freq_ceil(dev
, freq
);
1241 /* The freq is an lower bound. opp should be higher */
1242 opp
= dev_pm_opp_find_freq_ceil(dev
, freq
);
1244 /* If not available, use the closest opp */
1245 if (opp
== ERR_PTR(-ERANGE
))
1246 opp
= dev_pm_opp_find_freq_floor(dev
, freq
);
1251 EXPORT_SYMBOL(devfreq_recommended_opp
);
1254 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1255 * for any changes in the OPP availability
1257 * @dev: The devfreq user device. (parent of devfreq)
1258 * @devfreq: The devfreq object.
1260 int devfreq_register_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
1262 struct srcu_notifier_head
*nh
;
1266 nh
= dev_pm_opp_get_notifier(dev
);
1271 ret
= srcu_notifier_chain_register(nh
, &devfreq
->nb
);
1275 EXPORT_SYMBOL(devfreq_register_opp_notifier
);
1278 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1279 * notified for any changes in the OPP
1280 * availability changes anymore.
1281 * @dev: The devfreq user device. (parent of devfreq)
1282 * @devfreq: The devfreq object.
1284 * At exit() callback of devfreq_dev_profile, this must be included if
1285 * devfreq_recommended_opp is used.
1287 int devfreq_unregister_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
1289 struct srcu_notifier_head
*nh
;
1293 nh
= dev_pm_opp_get_notifier(dev
);
1298 ret
= srcu_notifier_chain_unregister(nh
, &devfreq
->nb
);
1302 EXPORT_SYMBOL(devfreq_unregister_opp_notifier
);
1304 static void devm_devfreq_opp_release(struct device
*dev
, void *res
)
1306 devfreq_unregister_opp_notifier(dev
, *(struct devfreq
**)res
);
1310 * devm_ devfreq_register_opp_notifier()
1311 * - Resource-managed devfreq_register_opp_notifier()
1312 * @dev: The devfreq user device. (parent of devfreq)
1313 * @devfreq: The devfreq object.
1315 int devm_devfreq_register_opp_notifier(struct device
*dev
,
1316 struct devfreq
*devfreq
)
1318 struct devfreq
**ptr
;
1321 ptr
= devres_alloc(devm_devfreq_opp_release
, sizeof(*ptr
), GFP_KERNEL
);
1325 ret
= devfreq_register_opp_notifier(dev
, devfreq
);
1332 devres_add(dev
, ptr
);
1336 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier
);
1339 * devm_devfreq_unregister_opp_notifier()
1340 * - Resource-managed devfreq_unregister_opp_notifier()
1341 * @dev: The devfreq user device. (parent of devfreq)
1342 * @devfreq: The devfreq object.
1344 void devm_devfreq_unregister_opp_notifier(struct device
*dev
,
1345 struct devfreq
*devfreq
)
1347 WARN_ON(devres_release(dev
, devm_devfreq_opp_release
,
1348 devm_devfreq_dev_match
, devfreq
));
1350 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier
);
1353 * devfreq_register_notifier() - Register a driver with devfreq
1354 * @devfreq: The devfreq object.
1355 * @nb: The notifier block to register.
1356 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1358 int devfreq_register_notifier(struct devfreq
*devfreq
,
1359 struct notifier_block
*nb
,
1368 case DEVFREQ_TRANSITION_NOTIFIER
:
1369 ret
= srcu_notifier_chain_register(
1370 &devfreq
->transition_notifier_list
, nb
);
1378 EXPORT_SYMBOL(devfreq_register_notifier
);
1381 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1382 * @devfreq: The devfreq object.
1383 * @nb: The notifier block to be unregistered.
1384 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1386 int devfreq_unregister_notifier(struct devfreq
*devfreq
,
1387 struct notifier_block
*nb
,
1396 case DEVFREQ_TRANSITION_NOTIFIER
:
1397 ret
= srcu_notifier_chain_unregister(
1398 &devfreq
->transition_notifier_list
, nb
);
1406 EXPORT_SYMBOL(devfreq_unregister_notifier
);
1408 struct devfreq_notifier_devres
{
1409 struct devfreq
*devfreq
;
1410 struct notifier_block
*nb
;
1414 static void devm_devfreq_notifier_release(struct device
*dev
, void *res
)
1416 struct devfreq_notifier_devres
*this = res
;
1418 devfreq_unregister_notifier(this->devfreq
, this->nb
, this->list
);
1422 * devm_devfreq_register_notifier()
1423 - Resource-managed devfreq_register_notifier()
1424 * @dev: The devfreq user device. (parent of devfreq)
1425 * @devfreq: The devfreq object.
1426 * @nb: The notifier block to be unregistered.
1427 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1429 int devm_devfreq_register_notifier(struct device
*dev
,
1430 struct devfreq
*devfreq
,
1431 struct notifier_block
*nb
,
1434 struct devfreq_notifier_devres
*ptr
;
1437 ptr
= devres_alloc(devm_devfreq_notifier_release
, sizeof(*ptr
),
1442 ret
= devfreq_register_notifier(devfreq
, nb
, list
);
1448 ptr
->devfreq
= devfreq
;
1451 devres_add(dev
, ptr
);
1455 EXPORT_SYMBOL(devm_devfreq_register_notifier
);
1458 * devm_devfreq_unregister_notifier()
1459 - Resource-managed devfreq_unregister_notifier()
1460 * @dev: The devfreq user device. (parent of devfreq)
1461 * @devfreq: The devfreq object.
1462 * @nb: The notifier block to be unregistered.
1463 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1465 void devm_devfreq_unregister_notifier(struct device
*dev
,
1466 struct devfreq
*devfreq
,
1467 struct notifier_block
*nb
,
1470 WARN_ON(devres_release(dev
, devm_devfreq_notifier_release
,
1471 devm_devfreq_dev_match
, devfreq
));
1473 EXPORT_SYMBOL(devm_devfreq_unregister_notifier
);
1475 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1476 MODULE_DESCRIPTION("devfreq class support");
1477 MODULE_LICENSE("GPL");