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);
275 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
277 if (devfreq->profile->freq_table)
278 if (devfreq_update_status(devfreq, freq))
279 dev_err(&devfreq->dev,
280 "Couldn't update frequency transition information.\n");
282 devfreq->previous_freq = freq;
285 EXPORT_SYMBOL(update_devfreq);
288 * devfreq_monitor() - Periodically poll devfreq objects.
289 * @work: the work struct used to run devfreq_monitor periodically.
292 static void devfreq_monitor(struct work_struct *work)
295 struct devfreq *devfreq = container_of(work,
296 struct devfreq, work.work);
298 mutex_lock(&devfreq->lock);
299 err = update_devfreq(devfreq);
301 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
303 queue_delayed_work(devfreq_wq, &devfreq->work,
304 msecs_to_jiffies(devfreq->profile->polling_ms));
305 mutex_unlock(&devfreq->lock);
309 * devfreq_monitor_start() - Start load monitoring of devfreq instance
310 * @devfreq: the devfreq instance.
312 * Helper function for starting devfreq device load monitoing. By
313 * default delayed work based monitoring is supported. Function
314 * to be called from governor in response to DEVFREQ_GOV_START
315 * event when device is added to devfreq framework.
317 void devfreq_monitor_start(struct devfreq *devfreq)
319 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
320 if (devfreq->profile->polling_ms)
321 queue_delayed_work(devfreq_wq, &devfreq->work,
322 msecs_to_jiffies(devfreq->profile->polling_ms));
324 EXPORT_SYMBOL(devfreq_monitor_start);
327 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
328 * @devfreq: the devfreq instance.
330 * Helper function to stop devfreq device load monitoing. Function
331 * to be called from governor in response to DEVFREQ_GOV_STOP
332 * event when device is removed from devfreq framework.
334 void devfreq_monitor_stop(struct devfreq *devfreq)
336 cancel_delayed_work_sync(&devfreq->work);
338 EXPORT_SYMBOL(devfreq_monitor_stop);
341 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
342 * @devfreq: the devfreq instance.
344 * Helper function to suspend devfreq device load monitoing. Function
345 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
346 * event or when polling interval is set to zero.
348 * Note: Though this function is same as devfreq_monitor_stop(),
349 * intentionally kept separate to provide hooks for collecting
350 * transition statistics.
352 void devfreq_monitor_suspend(struct devfreq *devfreq)
354 mutex_lock(&devfreq->lock);
355 if (devfreq->stop_polling) {
356 mutex_unlock(&devfreq->lock);
360 devfreq_update_status(devfreq, devfreq->previous_freq);
361 devfreq->stop_polling = true;
362 mutex_unlock(&devfreq->lock);
363 cancel_delayed_work_sync(&devfreq->work);
365 EXPORT_SYMBOL(devfreq_monitor_suspend);
368 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
369 * @devfreq: the devfreq instance.
371 * Helper function to resume devfreq device load monitoing. Function
372 * to be called from governor in response to DEVFREQ_GOV_RESUME
373 * event or when polling interval is set to non-zero.
375 void devfreq_monitor_resume(struct devfreq *devfreq)
379 mutex_lock(&devfreq->lock);
380 if (!devfreq->stop_polling)
383 if (!delayed_work_pending(&devfreq->work) &&
384 devfreq->profile->polling_ms)
385 queue_delayed_work(devfreq_wq, &devfreq->work,
386 msecs_to_jiffies(devfreq->profile->polling_ms));
388 devfreq->last_stat_updated = jiffies;
389 devfreq->stop_polling = false;
391 if (devfreq->profile->get_cur_freq &&
392 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
393 devfreq->previous_freq = freq;
396 mutex_unlock(&devfreq->lock);
398 EXPORT_SYMBOL(devfreq_monitor_resume);
401 * devfreq_interval_update() - Update device devfreq monitoring interval
402 * @devfreq: the devfreq instance.
403 * @delay: new polling interval to be set.
405 * Helper function to set new load monitoring polling interval. Function
406 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
408 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
410 unsigned int cur_delay = devfreq->profile->polling_ms;
411 unsigned int new_delay = *delay;
413 mutex_lock(&devfreq->lock);
414 devfreq->profile->polling_ms = new_delay;
416 if (devfreq->stop_polling)
419 /* if new delay is zero, stop polling */
421 mutex_unlock(&devfreq->lock);
422 cancel_delayed_work_sync(&devfreq->work);
426 /* if current delay is zero, start polling with new delay */
428 queue_delayed_work(devfreq_wq, &devfreq->work,
429 msecs_to_jiffies(devfreq->profile->polling_ms));
433 /* if current delay is greater than new delay, restart polling */
434 if (cur_delay > new_delay) {
435 mutex_unlock(&devfreq->lock);
436 cancel_delayed_work_sync(&devfreq->work);
437 mutex_lock(&devfreq->lock);
438 if (!devfreq->stop_polling)
439 queue_delayed_work(devfreq_wq, &devfreq->work,
440 msecs_to_jiffies(devfreq->profile->polling_ms));
443 mutex_unlock(&devfreq->lock);
445 EXPORT_SYMBOL(devfreq_interval_update);
448 * devfreq_notifier_call() - Notify that the device frequency requirements
449 * has been changed out of devfreq framework.
450 * @nb: the notifier_block (supposed to be devfreq->nb)
454 * Called by a notifier that uses devfreq->nb.
456 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
459 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
462 mutex_lock(&devfreq->lock);
463 ret = update_devfreq(devfreq);
464 mutex_unlock(&devfreq->lock);
470 * _remove_devfreq() - Remove devfreq from the list and release its resources.
471 * @devfreq: the devfreq struct
473 static void _remove_devfreq(struct devfreq *devfreq)
475 mutex_lock(&devfreq_list_lock);
476 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
477 mutex_unlock(&devfreq_list_lock);
478 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
481 list_del(&devfreq->node);
482 mutex_unlock(&devfreq_list_lock);
484 if (devfreq->governor)
485 devfreq->governor->event_handler(devfreq,
486 DEVFREQ_GOV_STOP, NULL);
488 if (devfreq->profile->exit)
489 devfreq->profile->exit(devfreq->dev.parent);
491 mutex_destroy(&devfreq->lock);
496 * devfreq_dev_release() - Callback for struct device to release the device.
497 * @dev: the devfreq device
499 * This calls _remove_devfreq() if _remove_devfreq() is not called.
501 static void devfreq_dev_release(struct device *dev)
503 struct devfreq *devfreq = to_devfreq(dev);
505 _remove_devfreq(devfreq);
509 * devfreq_add_device() - Add devfreq feature to the device
510 * @dev: the device to add devfreq feature.
511 * @profile: device-specific profile to run devfreq.
512 * @governor_name: name of the policy to choose frequency.
513 * @data: private data for the governor. The devfreq framework does not
516 struct devfreq *devfreq_add_device(struct device *dev,
517 struct devfreq_dev_profile *profile,
518 const char *governor_name,
521 struct devfreq *devfreq;
522 struct devfreq_governor *governor;
525 if (!dev || !profile || !governor_name) {
526 dev_err(dev, "%s: Invalid parameters.\n", __func__);
527 return ERR_PTR(-EINVAL);
530 mutex_lock(&devfreq_list_lock);
531 devfreq = find_device_devfreq(dev);
532 mutex_unlock(&devfreq_list_lock);
533 if (!IS_ERR(devfreq)) {
534 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
539 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
541 dev_err(dev, "%s: Unable to create devfreq for the device\n",
547 mutex_init(&devfreq->lock);
548 mutex_lock(&devfreq->lock);
549 devfreq->dev.parent = dev;
550 devfreq->dev.class = devfreq_class;
551 devfreq->dev.release = devfreq_dev_release;
552 devfreq->profile = profile;
553 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
554 devfreq->previous_freq = profile->initial_freq;
555 devfreq->data = data;
556 devfreq->nb.notifier_call = devfreq_notifier_call;
558 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
559 mutex_unlock(&devfreq->lock);
560 devfreq_set_freq_table(devfreq);
561 mutex_lock(&devfreq->lock);
564 devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
565 devfreq->profile->max_state *
566 devfreq->profile->max_state,
568 devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned long) *
569 devfreq->profile->max_state,
571 devfreq->last_stat_updated = jiffies;
573 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
574 err = device_register(&devfreq->dev);
576 put_device(&devfreq->dev);
577 mutex_unlock(&devfreq->lock);
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 devfreq->governor = governor;
591 if (devfreq->governor)
592 err = devfreq->governor->event_handler(devfreq,
593 DEVFREQ_GOV_START, NULL);
594 mutex_unlock(&devfreq_list_lock);
596 dev_err(dev, "%s: Unable to start governor for the device\n",
604 list_del(&devfreq->node);
605 device_unregister(&devfreq->dev);
610 EXPORT_SYMBOL(devfreq_add_device);
613 * devfreq_remove_device() - Remove devfreq feature from a device.
614 * @devfreq: the devfreq instance to be removed
616 * The opposite of devfreq_add_device().
618 int devfreq_remove_device(struct devfreq *devfreq)
623 device_unregister(&devfreq->dev);
624 put_device(&devfreq->dev);
628 EXPORT_SYMBOL(devfreq_remove_device);
630 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
632 struct devfreq **r = res;
634 if (WARN_ON(!r || !*r))
640 static void devm_devfreq_dev_release(struct device *dev, void *res)
642 devfreq_remove_device(*(struct devfreq **)res);
646 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
647 * @dev: the device to add devfreq feature.
648 * @profile: device-specific profile to run devfreq.
649 * @governor_name: name of the policy to choose frequency.
650 * @data: private data for the governor. The devfreq framework does not
653 * This function manages automatically the memory of devfreq device using device
654 * resource management and simplify the free operation for memory of devfreq
657 struct devfreq *devm_devfreq_add_device(struct device *dev,
658 struct devfreq_dev_profile *profile,
659 const char *governor_name,
662 struct devfreq **ptr, *devfreq;
664 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
666 return ERR_PTR(-ENOMEM);
668 devfreq = devfreq_add_device(dev, profile, governor_name, data);
669 if (IS_ERR(devfreq)) {
671 return ERR_PTR(-ENOMEM);
675 devres_add(dev, ptr);
679 EXPORT_SYMBOL(devm_devfreq_add_device);
683 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
684 * @dev - instance to the given device
685 * @index - index into list of devfreq
687 * return the instance of devfreq device
689 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
691 struct device_node *node;
692 struct devfreq *devfreq;
695 return ERR_PTR(-EINVAL);
698 return ERR_PTR(-EINVAL);
700 node = of_parse_phandle(dev->of_node, "devfreq", index);
702 return ERR_PTR(-ENODEV);
704 mutex_lock(&devfreq_list_lock);
705 list_for_each_entry(devfreq, &devfreq_list, node) {
706 if (devfreq->dev.parent
707 && devfreq->dev.parent->of_node == node) {
708 mutex_unlock(&devfreq_list_lock);
712 mutex_unlock(&devfreq_list_lock);
714 return ERR_PTR(-EPROBE_DEFER);
717 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
719 return ERR_PTR(-ENODEV);
721 #endif /* CONFIG_OF */
722 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
725 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
726 * @dev: the device to add devfreq feature.
727 * @devfreq: the devfreq instance to be removed
729 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
731 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
732 devm_devfreq_dev_match, devfreq));
734 EXPORT_SYMBOL(devm_devfreq_remove_device);
737 * devfreq_suspend_device() - Suspend devfreq of a device.
738 * @devfreq: the devfreq instance to be suspended
740 * This function is intended to be called by the pm callbacks
741 * (e.g., runtime_suspend, suspend) of the device driver that
744 int devfreq_suspend_device(struct devfreq *devfreq)
749 if (!devfreq->governor)
752 return devfreq->governor->event_handler(devfreq,
753 DEVFREQ_GOV_SUSPEND, NULL);
755 EXPORT_SYMBOL(devfreq_suspend_device);
758 * devfreq_resume_device() - Resume devfreq of a device.
759 * @devfreq: the devfreq instance to be resumed
761 * This function is intended to be called by the pm callbacks
762 * (e.g., runtime_resume, resume) of the device driver that
765 int devfreq_resume_device(struct devfreq *devfreq)
770 if (!devfreq->governor)
773 return devfreq->governor->event_handler(devfreq,
774 DEVFREQ_GOV_RESUME, NULL);
776 EXPORT_SYMBOL(devfreq_resume_device);
779 * devfreq_add_governor() - Add devfreq governor
780 * @governor: the devfreq governor to be added
782 int devfreq_add_governor(struct devfreq_governor *governor)
784 struct devfreq_governor *g;
785 struct devfreq *devfreq;
789 pr_err("%s: Invalid parameters.\n", __func__);
793 mutex_lock(&devfreq_list_lock);
794 g = find_devfreq_governor(governor->name);
796 pr_err("%s: governor %s already registered\n", __func__,
802 list_add(&governor->node, &devfreq_governor_list);
804 list_for_each_entry(devfreq, &devfreq_list, node) {
806 struct device *dev = devfreq->dev.parent;
808 if (!strncmp(devfreq->governor_name, governor->name,
810 /* The following should never occur */
811 if (devfreq->governor) {
813 "%s: Governor %s already present\n",
814 __func__, devfreq->governor->name);
815 ret = devfreq->governor->event_handler(devfreq,
816 DEVFREQ_GOV_STOP, NULL);
819 "%s: Governor %s stop = %d\n",
821 devfreq->governor->name, ret);
825 devfreq->governor = governor;
826 ret = devfreq->governor->event_handler(devfreq,
827 DEVFREQ_GOV_START, NULL);
829 dev_warn(dev, "%s: Governor %s start=%d\n",
830 __func__, devfreq->governor->name,
837 mutex_unlock(&devfreq_list_lock);
841 EXPORT_SYMBOL(devfreq_add_governor);
844 * devfreq_remove_device() - Remove devfreq feature from a device.
845 * @governor: the devfreq governor to be removed
847 int devfreq_remove_governor(struct devfreq_governor *governor)
849 struct devfreq_governor *g;
850 struct devfreq *devfreq;
854 pr_err("%s: Invalid parameters.\n", __func__);
858 mutex_lock(&devfreq_list_lock);
859 g = find_devfreq_governor(governor->name);
861 pr_err("%s: governor %s not registered\n", __func__,
866 list_for_each_entry(devfreq, &devfreq_list, node) {
868 struct device *dev = devfreq->dev.parent;
870 if (!strncmp(devfreq->governor_name, governor->name,
872 /* we should have a devfreq governor! */
873 if (!devfreq->governor) {
874 dev_warn(dev, "%s: Governor %s NOT present\n",
875 __func__, governor->name);
879 ret = devfreq->governor->event_handler(devfreq,
880 DEVFREQ_GOV_STOP, NULL);
882 dev_warn(dev, "%s: Governor %s stop=%d\n",
883 __func__, devfreq->governor->name,
886 devfreq->governor = NULL;
890 list_del(&governor->node);
892 mutex_unlock(&devfreq_list_lock);
896 EXPORT_SYMBOL(devfreq_remove_governor);
898 static ssize_t governor_show(struct device *dev,
899 struct device_attribute *attr, char *buf)
901 if (!to_devfreq(dev)->governor)
904 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
907 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
908 const char *buf, size_t count)
910 struct devfreq *df = to_devfreq(dev);
912 char str_governor[DEVFREQ_NAME_LEN + 1];
913 struct devfreq_governor *governor;
915 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
919 mutex_lock(&devfreq_list_lock);
920 governor = find_devfreq_governor(str_governor);
921 if (IS_ERR(governor)) {
922 ret = PTR_ERR(governor);
925 if (df->governor == governor) {
931 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
933 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
934 __func__, df->governor->name, ret);
938 df->governor = governor;
939 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
940 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
942 dev_warn(dev, "%s: Governor %s not started(%d)\n",
943 __func__, df->governor->name, ret);
945 mutex_unlock(&devfreq_list_lock);
951 static DEVICE_ATTR_RW(governor);
953 static ssize_t available_governors_show(struct device *d,
954 struct device_attribute *attr,
957 struct devfreq_governor *tmp_governor;
960 mutex_lock(&devfreq_list_lock);
961 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
962 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
963 "%s ", tmp_governor->name);
964 mutex_unlock(&devfreq_list_lock);
966 /* Truncate the trailing space */
970 count += sprintf(&buf[count], "\n");
974 static DEVICE_ATTR_RO(available_governors);
976 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
980 struct devfreq *devfreq = to_devfreq(dev);
982 if (devfreq->profile->get_cur_freq &&
983 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
984 return sprintf(buf, "%lu\n", freq);
986 return sprintf(buf, "%lu\n", devfreq->previous_freq);
988 static DEVICE_ATTR_RO(cur_freq);
990 static ssize_t target_freq_show(struct device *dev,
991 struct device_attribute *attr, char *buf)
993 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
995 static DEVICE_ATTR_RO(target_freq);
997 static ssize_t polling_interval_show(struct device *dev,
998 struct device_attribute *attr, char *buf)
1000 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1003 static ssize_t polling_interval_store(struct device *dev,
1004 struct device_attribute *attr,
1005 const char *buf, size_t count)
1007 struct devfreq *df = to_devfreq(dev);
1014 ret = sscanf(buf, "%u", &value);
1018 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1023 static DEVICE_ATTR_RW(polling_interval);
1025 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1026 const char *buf, size_t count)
1028 struct devfreq *df = to_devfreq(dev);
1029 unsigned long value;
1033 ret = sscanf(buf, "%lu", &value);
1037 mutex_lock(&df->lock);
1039 if (value && max && value > max) {
1044 df->min_freq = value;
1048 mutex_unlock(&df->lock);
1052 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1053 const char *buf, size_t count)
1055 struct devfreq *df = to_devfreq(dev);
1056 unsigned long value;
1060 ret = sscanf(buf, "%lu", &value);
1064 mutex_lock(&df->lock);
1066 if (value && min && value < min) {
1071 df->max_freq = value;
1075 mutex_unlock(&df->lock);
1079 #define show_one(name) \
1080 static ssize_t name##_show \
1081 (struct device *dev, struct device_attribute *attr, char *buf) \
1083 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
1088 static DEVICE_ATTR_RW(min_freq);
1089 static DEVICE_ATTR_RW(max_freq);
1091 static ssize_t available_frequencies_show(struct device *d,
1092 struct device_attribute *attr,
1095 struct devfreq *df = to_devfreq(d);
1096 struct device *dev = df->dev.parent;
1097 struct dev_pm_opp *opp;
1099 unsigned long freq = 0;
1103 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
1107 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1113 /* Truncate the trailing space */
1117 count += sprintf(&buf[count], "\n");
1121 static DEVICE_ATTR_RO(available_frequencies);
1123 static ssize_t trans_stat_show(struct device *dev,
1124 struct device_attribute *attr, char *buf)
1126 struct devfreq *devfreq = to_devfreq(dev);
1129 unsigned int max_state = devfreq->profile->max_state;
1131 if (!devfreq->stop_polling &&
1132 devfreq_update_status(devfreq, devfreq->previous_freq))
1135 return sprintf(buf, "Not Supported.\n");
1137 len = sprintf(buf, " From : To\n");
1138 len += sprintf(buf + len, " :");
1139 for (i = 0; i < max_state; i++)
1140 len += sprintf(buf + len, "%10lu",
1141 devfreq->profile->freq_table[i]);
1143 len += sprintf(buf + len, " time(ms)\n");
1145 for (i = 0; i < max_state; i++) {
1146 if (devfreq->profile->freq_table[i]
1147 == devfreq->previous_freq) {
1148 len += sprintf(buf + len, "*");
1150 len += sprintf(buf + len, " ");
1152 len += sprintf(buf + len, "%10lu:",
1153 devfreq->profile->freq_table[i]);
1154 for (j = 0; j < max_state; j++)
1155 len += sprintf(buf + len, "%10u",
1156 devfreq->trans_table[(i * max_state) + j]);
1157 len += sprintf(buf + len, "%10u\n",
1158 jiffies_to_msecs(devfreq->time_in_state[i]));
1161 len += sprintf(buf + len, "Total transition : %u\n",
1162 devfreq->total_trans);
1165 static DEVICE_ATTR_RO(trans_stat);
1167 static struct attribute *devfreq_attrs[] = {
1168 &dev_attr_governor.attr,
1169 &dev_attr_available_governors.attr,
1170 &dev_attr_cur_freq.attr,
1171 &dev_attr_available_frequencies.attr,
1172 &dev_attr_target_freq.attr,
1173 &dev_attr_polling_interval.attr,
1174 &dev_attr_min_freq.attr,
1175 &dev_attr_max_freq.attr,
1176 &dev_attr_trans_stat.attr,
1179 ATTRIBUTE_GROUPS(devfreq);
1181 static int __init devfreq_init(void)
1183 devfreq_class = class_create(THIS_MODULE, "devfreq");
1184 if (IS_ERR(devfreq_class)) {
1185 pr_err("%s: couldn't create class\n", __FILE__);
1186 return PTR_ERR(devfreq_class);
1189 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1191 class_destroy(devfreq_class);
1192 pr_err("%s: couldn't create workqueue\n", __FILE__);
1195 devfreq_class->dev_groups = devfreq_groups;
1199 subsys_initcall(devfreq_init);
1201 static void __exit devfreq_exit(void)
1203 class_destroy(devfreq_class);
1204 destroy_workqueue(devfreq_wq);
1206 module_exit(devfreq_exit);
1209 * The followings are helper functions for devfreq user device drivers with
1214 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1215 * freq value given to target callback.
1216 * @dev: The devfreq user device. (parent of devfreq)
1217 * @freq: The frequency given to target function
1218 * @flags: Flags handed from devfreq framework.
1220 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1221 * protected pointer. The reason for the same is that the opp pointer which is
1222 * returned will remain valid for use with opp_get_{voltage, freq} only while
1223 * under the locked area. The pointer returned must be used prior to unlocking
1224 * with rcu_read_unlock() to maintain the integrity of the pointer.
1226 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1227 unsigned long *freq,
1230 struct dev_pm_opp *opp;
1232 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1233 /* The freq is an upper bound. opp should be lower */
1234 opp = dev_pm_opp_find_freq_floor(dev, freq);
1236 /* If not available, use the closest opp */
1237 if (opp == ERR_PTR(-ERANGE))
1238 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1240 /* The freq is an lower bound. opp should be higher */
1241 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1243 /* If not available, use the closest opp */
1244 if (opp == ERR_PTR(-ERANGE))
1245 opp = dev_pm_opp_find_freq_floor(dev, freq);
1250 EXPORT_SYMBOL(devfreq_recommended_opp);
1253 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1254 * for any changes in the OPP availability
1256 * @dev: The devfreq user device. (parent of devfreq)
1257 * @devfreq: The devfreq object.
1259 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1261 struct srcu_notifier_head *nh;
1265 nh = dev_pm_opp_get_notifier(dev);
1270 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1274 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1277 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1278 * notified for any changes in the OPP
1279 * availability changes anymore.
1280 * @dev: The devfreq user device. (parent of devfreq)
1281 * @devfreq: The devfreq object.
1283 * At exit() callback of devfreq_dev_profile, this must be included if
1284 * devfreq_recommended_opp is used.
1286 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1288 struct srcu_notifier_head *nh;
1292 nh = dev_pm_opp_get_notifier(dev);
1297 ret = srcu_notifier_chain_unregister(nh, &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);
1474 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1475 MODULE_DESCRIPTION("devfreq class support");
1476 MODULE_LICENSE("GPL");