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 static 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;
169 * find_devfreq_governor() - find devfreq governor from name
170 * @name: name of the governor
172 * Search the list of devfreq governors and return the matched
173 * governor's pointer. devfreq_list_lock should be held by the caller.
175 static struct devfreq_governor *find_devfreq_governor(const char *name)
177 struct devfreq_governor *tmp_governor;
179 if (IS_ERR_OR_NULL(name)) {
180 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
181 return ERR_PTR(-EINVAL);
183 WARN(!mutex_is_locked(&devfreq_list_lock),
184 "devfreq_list_lock must be locked.");
186 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
187 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
191 return ERR_PTR(-ENODEV);
194 static int devfreq_notify_transition(struct devfreq *devfreq,
195 struct devfreq_freqs *freqs, unsigned int state)
201 case DEVFREQ_PRECHANGE:
202 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
203 DEVFREQ_PRECHANGE, freqs);
206 case DEVFREQ_POSTCHANGE:
207 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
208 DEVFREQ_POSTCHANGE, freqs);
217 /* Load monitoring helper functions for governors use */
220 * update_devfreq() - Reevaluate the device and configure frequency.
221 * @devfreq: the devfreq instance.
223 * Note: Lock devfreq->lock before calling update_devfreq
224 * This function is exported for governors.
226 int update_devfreq(struct devfreq *devfreq)
228 struct devfreq_freqs freqs;
229 unsigned long freq, cur_freq;
233 if (!mutex_is_locked(&devfreq->lock)) {
234 WARN(true, "devfreq->lock must be locked by the caller.\n");
238 if (!devfreq->governor)
241 /* Reevaluate the proper frequency */
242 err = devfreq->governor->get_target_freq(devfreq, &freq);
247 * Adjust the frequency with user freq and QoS.
249 * List from the highest priority
254 if (devfreq->min_freq && freq < devfreq->min_freq) {
255 freq = devfreq->min_freq;
256 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
258 if (devfreq->max_freq && freq > devfreq->max_freq) {
259 freq = devfreq->max_freq;
260 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
263 if (devfreq->profile->get_cur_freq)
264 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
266 cur_freq = devfreq->previous_freq;
268 freqs.old = cur_freq;
270 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
272 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
274 freqs.new = cur_freq;
275 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
280 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
282 if (devfreq->profile->freq_table)
283 if (devfreq_update_status(devfreq, freq))
284 dev_err(&devfreq->dev,
285 "Couldn't update frequency transition information.\n");
287 devfreq->previous_freq = freq;
290 EXPORT_SYMBOL(update_devfreq);
293 * devfreq_monitor() - Periodically poll devfreq objects.
294 * @work: the work struct used to run devfreq_monitor periodically.
297 static void devfreq_monitor(struct work_struct *work)
300 struct devfreq *devfreq = container_of(work,
301 struct devfreq, work.work);
303 mutex_lock(&devfreq->lock);
304 err = update_devfreq(devfreq);
306 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
308 queue_delayed_work(devfreq_wq, &devfreq->work,
309 msecs_to_jiffies(devfreq->profile->polling_ms));
310 mutex_unlock(&devfreq->lock);
314 * devfreq_monitor_start() - Start load monitoring of devfreq instance
315 * @devfreq: the devfreq instance.
317 * Helper function for starting devfreq device load monitoing. By
318 * default delayed work based monitoring is supported. Function
319 * to be called from governor in response to DEVFREQ_GOV_START
320 * event when device is added to devfreq framework.
322 void devfreq_monitor_start(struct devfreq *devfreq)
324 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
325 if (devfreq->profile->polling_ms)
326 queue_delayed_work(devfreq_wq, &devfreq->work,
327 msecs_to_jiffies(devfreq->profile->polling_ms));
329 EXPORT_SYMBOL(devfreq_monitor_start);
332 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
333 * @devfreq: the devfreq instance.
335 * Helper function to stop devfreq device load monitoing. Function
336 * to be called from governor in response to DEVFREQ_GOV_STOP
337 * event when device is removed from devfreq framework.
339 void devfreq_monitor_stop(struct devfreq *devfreq)
341 cancel_delayed_work_sync(&devfreq->work);
343 EXPORT_SYMBOL(devfreq_monitor_stop);
346 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
347 * @devfreq: the devfreq instance.
349 * Helper function to suspend devfreq device load monitoing. Function
350 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
351 * event or when polling interval is set to zero.
353 * Note: Though this function is same as devfreq_monitor_stop(),
354 * intentionally kept separate to provide hooks for collecting
355 * transition statistics.
357 void devfreq_monitor_suspend(struct devfreq *devfreq)
359 mutex_lock(&devfreq->lock);
360 if (devfreq->stop_polling) {
361 mutex_unlock(&devfreq->lock);
365 devfreq_update_status(devfreq, devfreq->previous_freq);
366 devfreq->stop_polling = true;
367 mutex_unlock(&devfreq->lock);
368 cancel_delayed_work_sync(&devfreq->work);
370 EXPORT_SYMBOL(devfreq_monitor_suspend);
373 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
374 * @devfreq: the devfreq instance.
376 * Helper function to resume devfreq device load monitoing. Function
377 * to be called from governor in response to DEVFREQ_GOV_RESUME
378 * event or when polling interval is set to non-zero.
380 void devfreq_monitor_resume(struct devfreq *devfreq)
384 mutex_lock(&devfreq->lock);
385 if (!devfreq->stop_polling)
388 if (!delayed_work_pending(&devfreq->work) &&
389 devfreq->profile->polling_ms)
390 queue_delayed_work(devfreq_wq, &devfreq->work,
391 msecs_to_jiffies(devfreq->profile->polling_ms));
393 devfreq->last_stat_updated = jiffies;
394 devfreq->stop_polling = false;
396 if (devfreq->profile->get_cur_freq &&
397 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
398 devfreq->previous_freq = freq;
401 mutex_unlock(&devfreq->lock);
403 EXPORT_SYMBOL(devfreq_monitor_resume);
406 * devfreq_interval_update() - Update device devfreq monitoring interval
407 * @devfreq: the devfreq instance.
408 * @delay: new polling interval to be set.
410 * Helper function to set new load monitoring polling interval. Function
411 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
413 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
415 unsigned int cur_delay = devfreq->profile->polling_ms;
416 unsigned int new_delay = *delay;
418 mutex_lock(&devfreq->lock);
419 devfreq->profile->polling_ms = new_delay;
421 if (devfreq->stop_polling)
424 /* if new delay is zero, stop polling */
426 mutex_unlock(&devfreq->lock);
427 cancel_delayed_work_sync(&devfreq->work);
431 /* if current delay is zero, start polling with new delay */
433 queue_delayed_work(devfreq_wq, &devfreq->work,
434 msecs_to_jiffies(devfreq->profile->polling_ms));
438 /* if current delay is greater than new delay, restart polling */
439 if (cur_delay > new_delay) {
440 mutex_unlock(&devfreq->lock);
441 cancel_delayed_work_sync(&devfreq->work);
442 mutex_lock(&devfreq->lock);
443 if (!devfreq->stop_polling)
444 queue_delayed_work(devfreq_wq, &devfreq->work,
445 msecs_to_jiffies(devfreq->profile->polling_ms));
448 mutex_unlock(&devfreq->lock);
450 EXPORT_SYMBOL(devfreq_interval_update);
453 * devfreq_notifier_call() - Notify that the device frequency requirements
454 * has been changed out of devfreq framework.
455 * @nb: the notifier_block (supposed to be devfreq->nb)
459 * Called by a notifier that uses devfreq->nb.
461 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
464 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
467 mutex_lock(&devfreq->lock);
468 ret = update_devfreq(devfreq);
469 mutex_unlock(&devfreq->lock);
475 * _remove_devfreq() - Remove devfreq from the list and release its resources.
476 * @devfreq: the devfreq struct
478 static void _remove_devfreq(struct devfreq *devfreq)
480 mutex_lock(&devfreq_list_lock);
481 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
482 mutex_unlock(&devfreq_list_lock);
483 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
486 list_del(&devfreq->node);
487 mutex_unlock(&devfreq_list_lock);
489 if (devfreq->governor)
490 devfreq->governor->event_handler(devfreq,
491 DEVFREQ_GOV_STOP, NULL);
493 if (devfreq->profile->exit)
494 devfreq->profile->exit(devfreq->dev.parent);
496 mutex_destroy(&devfreq->lock);
501 * devfreq_dev_release() - Callback for struct device to release the device.
502 * @dev: the devfreq device
504 * This calls _remove_devfreq() if _remove_devfreq() is not called.
506 static void devfreq_dev_release(struct device *dev)
508 struct devfreq *devfreq = to_devfreq(dev);
510 _remove_devfreq(devfreq);
514 * devfreq_add_device() - Add devfreq feature to the device
515 * @dev: the device to add devfreq feature.
516 * @profile: device-specific profile to run devfreq.
517 * @governor_name: name of the policy to choose frequency.
518 * @data: private data for the governor. The devfreq framework does not
521 struct devfreq *devfreq_add_device(struct device *dev,
522 struct devfreq_dev_profile *profile,
523 const char *governor_name,
526 struct devfreq *devfreq;
527 struct devfreq_governor *governor;
530 if (!dev || !profile || !governor_name) {
531 dev_err(dev, "%s: Invalid parameters.\n", __func__);
532 return ERR_PTR(-EINVAL);
535 mutex_lock(&devfreq_list_lock);
536 devfreq = find_device_devfreq(dev);
537 mutex_unlock(&devfreq_list_lock);
538 if (!IS_ERR(devfreq)) {
539 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
544 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
546 dev_err(dev, "%s: Unable to create devfreq for the device\n",
552 mutex_init(&devfreq->lock);
553 mutex_lock(&devfreq->lock);
554 devfreq->dev.parent = dev;
555 devfreq->dev.class = devfreq_class;
556 devfreq->dev.release = devfreq_dev_release;
557 devfreq->profile = profile;
558 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
559 devfreq->previous_freq = profile->initial_freq;
560 devfreq->last_status.current_frequency = profile->initial_freq;
561 devfreq->data = data;
562 devfreq->nb.notifier_call = devfreq_notifier_call;
564 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
565 mutex_unlock(&devfreq->lock);
566 devfreq_set_freq_table(devfreq);
567 mutex_lock(&devfreq->lock);
570 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
571 err = device_register(&devfreq->dev);
573 mutex_unlock(&devfreq->lock);
577 devfreq->trans_table = devm_kzalloc(&devfreq->dev, sizeof(unsigned int) *
578 devfreq->profile->max_state *
579 devfreq->profile->max_state,
581 devfreq->time_in_state = devm_kzalloc(&devfreq->dev, sizeof(unsigned long) *
582 devfreq->profile->max_state,
584 devfreq->last_stat_updated = jiffies;
586 srcu_init_notifier_head(&devfreq->transition_notifier_list);
588 mutex_unlock(&devfreq->lock);
590 mutex_lock(&devfreq_list_lock);
591 list_add(&devfreq->node, &devfreq_list);
593 governor = find_devfreq_governor(devfreq->governor_name);
594 if (IS_ERR(governor)) {
595 dev_err(dev, "%s: Unable to find governor for the device\n",
597 err = PTR_ERR(governor);
601 devfreq->governor = governor;
602 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
605 dev_err(dev, "%s: Unable to start governor for the device\n",
609 mutex_unlock(&devfreq_list_lock);
614 list_del(&devfreq->node);
615 mutex_unlock(&devfreq_list_lock);
617 device_unregister(&devfreq->dev);
621 EXPORT_SYMBOL(devfreq_add_device);
624 * devfreq_remove_device() - Remove devfreq feature from a device.
625 * @devfreq: the devfreq instance to be removed
627 * The opposite of devfreq_add_device().
629 int devfreq_remove_device(struct devfreq *devfreq)
634 device_unregister(&devfreq->dev);
638 EXPORT_SYMBOL(devfreq_remove_device);
640 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
642 struct devfreq **r = res;
644 if (WARN_ON(!r || !*r))
650 static void devm_devfreq_dev_release(struct device *dev, void *res)
652 devfreq_remove_device(*(struct devfreq **)res);
656 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
657 * @dev: the device to add devfreq feature.
658 * @profile: device-specific profile to run devfreq.
659 * @governor_name: name of the policy to choose frequency.
660 * @data: private data for the governor. The devfreq framework does not
663 * This function manages automatically the memory of devfreq device using device
664 * resource management and simplify the free operation for memory of devfreq
667 struct devfreq *devm_devfreq_add_device(struct device *dev,
668 struct devfreq_dev_profile *profile,
669 const char *governor_name,
672 struct devfreq **ptr, *devfreq;
674 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
676 return ERR_PTR(-ENOMEM);
678 devfreq = devfreq_add_device(dev, profile, governor_name, data);
679 if (IS_ERR(devfreq)) {
681 return ERR_PTR(-ENOMEM);
685 devres_add(dev, ptr);
689 EXPORT_SYMBOL(devm_devfreq_add_device);
693 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
694 * @dev - instance to the given device
695 * @index - index into list of devfreq
697 * return the instance of devfreq device
699 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
701 struct device_node *node;
702 struct devfreq *devfreq;
705 return ERR_PTR(-EINVAL);
708 return ERR_PTR(-EINVAL);
710 node = of_parse_phandle(dev->of_node, "devfreq", index);
712 return ERR_PTR(-ENODEV);
714 mutex_lock(&devfreq_list_lock);
715 list_for_each_entry(devfreq, &devfreq_list, node) {
716 if (devfreq->dev.parent
717 && devfreq->dev.parent->of_node == node) {
718 mutex_unlock(&devfreq_list_lock);
723 mutex_unlock(&devfreq_list_lock);
726 return ERR_PTR(-EPROBE_DEFER);
729 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
731 return ERR_PTR(-ENODEV);
733 #endif /* CONFIG_OF */
734 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
737 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
738 * @dev: the device to add devfreq feature.
739 * @devfreq: the devfreq instance to be removed
741 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
743 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
744 devm_devfreq_dev_match, devfreq));
746 EXPORT_SYMBOL(devm_devfreq_remove_device);
749 * devfreq_suspend_device() - Suspend devfreq of a device.
750 * @devfreq: the devfreq instance to be suspended
752 * This function is intended to be called by the pm callbacks
753 * (e.g., runtime_suspend, suspend) of the device driver that
756 int devfreq_suspend_device(struct devfreq *devfreq)
761 if (!devfreq->governor)
764 return devfreq->governor->event_handler(devfreq,
765 DEVFREQ_GOV_SUSPEND, NULL);
767 EXPORT_SYMBOL(devfreq_suspend_device);
770 * devfreq_resume_device() - Resume devfreq of a device.
771 * @devfreq: the devfreq instance to be resumed
773 * This function is intended to be called by the pm callbacks
774 * (e.g., runtime_resume, resume) of the device driver that
777 int devfreq_resume_device(struct devfreq *devfreq)
782 if (!devfreq->governor)
785 return devfreq->governor->event_handler(devfreq,
786 DEVFREQ_GOV_RESUME, NULL);
788 EXPORT_SYMBOL(devfreq_resume_device);
791 * devfreq_add_governor() - Add devfreq governor
792 * @governor: the devfreq governor to be added
794 int devfreq_add_governor(struct devfreq_governor *governor)
796 struct devfreq_governor *g;
797 struct devfreq *devfreq;
801 pr_err("%s: Invalid parameters.\n", __func__);
805 mutex_lock(&devfreq_list_lock);
806 g = find_devfreq_governor(governor->name);
808 pr_err("%s: governor %s already registered\n", __func__,
814 list_add(&governor->node, &devfreq_governor_list);
816 list_for_each_entry(devfreq, &devfreq_list, node) {
818 struct device *dev = devfreq->dev.parent;
820 if (!strncmp(devfreq->governor_name, governor->name,
822 /* The following should never occur */
823 if (devfreq->governor) {
825 "%s: Governor %s already present\n",
826 __func__, devfreq->governor->name);
827 ret = devfreq->governor->event_handler(devfreq,
828 DEVFREQ_GOV_STOP, NULL);
831 "%s: Governor %s stop = %d\n",
833 devfreq->governor->name, ret);
837 devfreq->governor = governor;
838 ret = devfreq->governor->event_handler(devfreq,
839 DEVFREQ_GOV_START, NULL);
841 dev_warn(dev, "%s: Governor %s start=%d\n",
842 __func__, devfreq->governor->name,
849 mutex_unlock(&devfreq_list_lock);
853 EXPORT_SYMBOL(devfreq_add_governor);
856 * devfreq_remove_governor() - Remove devfreq feature from a device.
857 * @governor: the devfreq governor to be removed
859 int devfreq_remove_governor(struct devfreq_governor *governor)
861 struct devfreq_governor *g;
862 struct devfreq *devfreq;
866 pr_err("%s: Invalid parameters.\n", __func__);
870 mutex_lock(&devfreq_list_lock);
871 g = find_devfreq_governor(governor->name);
873 pr_err("%s: governor %s not registered\n", __func__,
878 list_for_each_entry(devfreq, &devfreq_list, node) {
880 struct device *dev = devfreq->dev.parent;
882 if (!strncmp(devfreq->governor_name, governor->name,
884 /* we should have a devfreq governor! */
885 if (!devfreq->governor) {
886 dev_warn(dev, "%s: Governor %s NOT present\n",
887 __func__, governor->name);
891 ret = devfreq->governor->event_handler(devfreq,
892 DEVFREQ_GOV_STOP, NULL);
894 dev_warn(dev, "%s: Governor %s stop=%d\n",
895 __func__, devfreq->governor->name,
898 devfreq->governor = NULL;
902 list_del(&governor->node);
904 mutex_unlock(&devfreq_list_lock);
908 EXPORT_SYMBOL(devfreq_remove_governor);
910 static ssize_t governor_show(struct device *dev,
911 struct device_attribute *attr, char *buf)
913 if (!to_devfreq(dev)->governor)
916 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
919 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
920 const char *buf, size_t count)
922 struct devfreq *df = to_devfreq(dev);
924 char str_governor[DEVFREQ_NAME_LEN + 1];
925 struct devfreq_governor *governor;
927 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
931 mutex_lock(&devfreq_list_lock);
932 governor = find_devfreq_governor(str_governor);
933 if (IS_ERR(governor)) {
934 ret = PTR_ERR(governor);
937 if (df->governor == governor) {
943 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
945 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
946 __func__, df->governor->name, ret);
950 df->governor = governor;
951 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
952 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
954 dev_warn(dev, "%s: Governor %s not started(%d)\n",
955 __func__, df->governor->name, ret);
957 mutex_unlock(&devfreq_list_lock);
963 static DEVICE_ATTR_RW(governor);
965 static ssize_t available_governors_show(struct device *d,
966 struct device_attribute *attr,
969 struct devfreq_governor *tmp_governor;
972 mutex_lock(&devfreq_list_lock);
973 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
974 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
975 "%s ", tmp_governor->name);
976 mutex_unlock(&devfreq_list_lock);
978 /* Truncate the trailing space */
982 count += sprintf(&buf[count], "\n");
986 static DEVICE_ATTR_RO(available_governors);
988 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
992 struct devfreq *devfreq = to_devfreq(dev);
994 if (devfreq->profile->get_cur_freq &&
995 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
996 return sprintf(buf, "%lu\n", freq);
998 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1000 static DEVICE_ATTR_RO(cur_freq);
1002 static ssize_t target_freq_show(struct device *dev,
1003 struct device_attribute *attr, char *buf)
1005 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1007 static DEVICE_ATTR_RO(target_freq);
1009 static ssize_t polling_interval_show(struct device *dev,
1010 struct device_attribute *attr, char *buf)
1012 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1015 static ssize_t polling_interval_store(struct device *dev,
1016 struct device_attribute *attr,
1017 const char *buf, size_t count)
1019 struct devfreq *df = to_devfreq(dev);
1026 ret = sscanf(buf, "%u", &value);
1030 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1035 static DEVICE_ATTR_RW(polling_interval);
1037 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1038 const char *buf, size_t count)
1040 struct devfreq *df = to_devfreq(dev);
1041 unsigned long value;
1045 ret = sscanf(buf, "%lu", &value);
1049 mutex_lock(&df->lock);
1051 if (value && max && value > max) {
1056 df->min_freq = value;
1060 mutex_unlock(&df->lock);
1064 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1065 const char *buf, size_t count)
1067 struct devfreq *df = to_devfreq(dev);
1068 unsigned long value;
1072 ret = sscanf(buf, "%lu", &value);
1076 mutex_lock(&df->lock);
1078 if (value && min && value < min) {
1083 df->max_freq = value;
1087 mutex_unlock(&df->lock);
1091 #define show_one(name) \
1092 static ssize_t name##_show \
1093 (struct device *dev, struct device_attribute *attr, char *buf) \
1095 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
1100 static DEVICE_ATTR_RW(min_freq);
1101 static DEVICE_ATTR_RW(max_freq);
1103 static ssize_t available_frequencies_show(struct device *d,
1104 struct device_attribute *attr,
1107 struct devfreq *df = to_devfreq(d);
1108 struct device *dev = df->dev.parent;
1109 struct dev_pm_opp *opp;
1111 unsigned long freq = 0;
1114 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
1118 dev_pm_opp_put(opp);
1119 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1124 /* Truncate the trailing space */
1128 count += sprintf(&buf[count], "\n");
1132 static DEVICE_ATTR_RO(available_frequencies);
1134 static ssize_t trans_stat_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
1137 struct devfreq *devfreq = to_devfreq(dev);
1140 unsigned int max_state = devfreq->profile->max_state;
1142 if (!devfreq->stop_polling &&
1143 devfreq_update_status(devfreq, devfreq->previous_freq))
1146 return sprintf(buf, "Not Supported.\n");
1148 len = sprintf(buf, " From : To\n");
1149 len += sprintf(buf + len, " :");
1150 for (i = 0; i < max_state; i++)
1151 len += sprintf(buf + len, "%10lu",
1152 devfreq->profile->freq_table[i]);
1154 len += sprintf(buf + len, " time(ms)\n");
1156 for (i = 0; i < max_state; i++) {
1157 if (devfreq->profile->freq_table[i]
1158 == devfreq->previous_freq) {
1159 len += sprintf(buf + len, "*");
1161 len += sprintf(buf + len, " ");
1163 len += sprintf(buf + len, "%10lu:",
1164 devfreq->profile->freq_table[i]);
1165 for (j = 0; j < max_state; j++)
1166 len += sprintf(buf + len, "%10u",
1167 devfreq->trans_table[(i * max_state) + j]);
1168 len += sprintf(buf + len, "%10u\n",
1169 jiffies_to_msecs(devfreq->time_in_state[i]));
1172 len += sprintf(buf + len, "Total transition : %u\n",
1173 devfreq->total_trans);
1176 static DEVICE_ATTR_RO(trans_stat);
1178 static struct attribute *devfreq_attrs[] = {
1179 &dev_attr_governor.attr,
1180 &dev_attr_available_governors.attr,
1181 &dev_attr_cur_freq.attr,
1182 &dev_attr_available_frequencies.attr,
1183 &dev_attr_target_freq.attr,
1184 &dev_attr_polling_interval.attr,
1185 &dev_attr_min_freq.attr,
1186 &dev_attr_max_freq.attr,
1187 &dev_attr_trans_stat.attr,
1190 ATTRIBUTE_GROUPS(devfreq);
1192 static int __init devfreq_init(void)
1194 devfreq_class = class_create(THIS_MODULE, "devfreq");
1195 if (IS_ERR(devfreq_class)) {
1196 pr_err("%s: couldn't create class\n", __FILE__);
1197 return PTR_ERR(devfreq_class);
1200 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1202 class_destroy(devfreq_class);
1203 pr_err("%s: couldn't create workqueue\n", __FILE__);
1206 devfreq_class->dev_groups = devfreq_groups;
1210 subsys_initcall(devfreq_init);
1213 * The followings are helper functions for devfreq user device drivers with
1218 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1219 * freq value given to target callback.
1220 * @dev: The devfreq user device. (parent of devfreq)
1221 * @freq: The frequency given to target function
1222 * @flags: Flags handed from devfreq framework.
1224 * The callers are required to call dev_pm_opp_put() for the returned OPP after
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 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1264 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1267 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1268 * notified for any changes in the OPP
1269 * availability changes anymore.
1270 * @dev: The devfreq user device. (parent of devfreq)
1271 * @devfreq: The devfreq object.
1273 * At exit() callback of devfreq_dev_profile, this must be included if
1274 * devfreq_recommended_opp is used.
1276 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1278 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1280 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1282 static void devm_devfreq_opp_release(struct device *dev, void *res)
1284 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1288 * devm_ devfreq_register_opp_notifier()
1289 * - Resource-managed devfreq_register_opp_notifier()
1290 * @dev: The devfreq user device. (parent of devfreq)
1291 * @devfreq: The devfreq object.
1293 int devm_devfreq_register_opp_notifier(struct device *dev,
1294 struct devfreq *devfreq)
1296 struct devfreq **ptr;
1299 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1303 ret = devfreq_register_opp_notifier(dev, devfreq);
1310 devres_add(dev, ptr);
1314 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1317 * devm_devfreq_unregister_opp_notifier()
1318 * - Resource-managed devfreq_unregister_opp_notifier()
1319 * @dev: The devfreq user device. (parent of devfreq)
1320 * @devfreq: The devfreq object.
1322 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1323 struct devfreq *devfreq)
1325 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1326 devm_devfreq_dev_match, devfreq));
1328 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1331 * devfreq_register_notifier() - Register a driver with devfreq
1332 * @devfreq: The devfreq object.
1333 * @nb: The notifier block to register.
1334 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1336 int devfreq_register_notifier(struct devfreq *devfreq,
1337 struct notifier_block *nb,
1346 case DEVFREQ_TRANSITION_NOTIFIER:
1347 ret = srcu_notifier_chain_register(
1348 &devfreq->transition_notifier_list, nb);
1356 EXPORT_SYMBOL(devfreq_register_notifier);
1359 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1360 * @devfreq: The devfreq object.
1361 * @nb: The notifier block to be unregistered.
1362 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1364 int devfreq_unregister_notifier(struct devfreq *devfreq,
1365 struct notifier_block *nb,
1374 case DEVFREQ_TRANSITION_NOTIFIER:
1375 ret = srcu_notifier_chain_unregister(
1376 &devfreq->transition_notifier_list, nb);
1384 EXPORT_SYMBOL(devfreq_unregister_notifier);
1386 struct devfreq_notifier_devres {
1387 struct devfreq *devfreq;
1388 struct notifier_block *nb;
1392 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1394 struct devfreq_notifier_devres *this = res;
1396 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1400 * devm_devfreq_register_notifier()
1401 - Resource-managed devfreq_register_notifier()
1402 * @dev: The devfreq user device. (parent of devfreq)
1403 * @devfreq: The devfreq object.
1404 * @nb: The notifier block to be unregistered.
1405 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1407 int devm_devfreq_register_notifier(struct device *dev,
1408 struct devfreq *devfreq,
1409 struct notifier_block *nb,
1412 struct devfreq_notifier_devres *ptr;
1415 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1420 ret = devfreq_register_notifier(devfreq, nb, list);
1426 ptr->devfreq = devfreq;
1429 devres_add(dev, ptr);
1433 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1436 * devm_devfreq_unregister_notifier()
1437 - Resource-managed devfreq_unregister_notifier()
1438 * @dev: The devfreq user device. (parent of devfreq)
1439 * @devfreq: The devfreq object.
1440 * @nb: The notifier block to be unregistered.
1441 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1443 void devm_devfreq_unregister_notifier(struct device *dev,
1444 struct devfreq *devfreq,
1445 struct notifier_block *nb,
1448 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1449 devm_devfreq_dev_match, devfreq));
1451 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);