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 /* Load monitoring helper functions for governors use */
195 * update_devfreq() - Reevaluate the device and configure frequency.
196 * @devfreq: the devfreq instance.
198 * Note: Lock devfreq->lock before calling update_devfreq
199 * This function is exported for governors.
201 int update_devfreq(struct devfreq *devfreq)
207 if (!mutex_is_locked(&devfreq->lock)) {
208 WARN(true, "devfreq->lock must be locked by the caller.\n");
212 if (!devfreq->governor)
215 /* Reevaluate the proper frequency */
216 err = devfreq->governor->get_target_freq(devfreq, &freq);
221 * Adjust the frequency with user freq and QoS.
223 * List from the highest priority
228 if (devfreq->min_freq && freq < devfreq->min_freq) {
229 freq = devfreq->min_freq;
230 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
232 if (devfreq->max_freq && freq > devfreq->max_freq) {
233 freq = devfreq->max_freq;
234 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
237 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
241 if (devfreq->profile->freq_table)
242 if (devfreq_update_status(devfreq, freq))
243 dev_err(&devfreq->dev,
244 "Couldn't update frequency transition information.\n");
246 devfreq->previous_freq = freq;
249 EXPORT_SYMBOL(update_devfreq);
252 * devfreq_monitor() - Periodically poll devfreq objects.
253 * @work: the work struct used to run devfreq_monitor periodically.
256 static void devfreq_monitor(struct work_struct *work)
259 struct devfreq *devfreq = container_of(work,
260 struct devfreq, work.work);
262 mutex_lock(&devfreq->lock);
263 err = update_devfreq(devfreq);
265 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
267 queue_delayed_work(devfreq_wq, &devfreq->work,
268 msecs_to_jiffies(devfreq->profile->polling_ms));
269 mutex_unlock(&devfreq->lock);
273 * devfreq_monitor_start() - Start load monitoring of devfreq instance
274 * @devfreq: the devfreq instance.
276 * Helper function for starting devfreq device load monitoing. By
277 * default delayed work based monitoring is supported. Function
278 * to be called from governor in response to DEVFREQ_GOV_START
279 * event when device is added to devfreq framework.
281 void devfreq_monitor_start(struct devfreq *devfreq)
283 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
284 if (devfreq->profile->polling_ms)
285 queue_delayed_work(devfreq_wq, &devfreq->work,
286 msecs_to_jiffies(devfreq->profile->polling_ms));
288 EXPORT_SYMBOL(devfreq_monitor_start);
291 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
292 * @devfreq: the devfreq instance.
294 * Helper function to stop devfreq device load monitoing. Function
295 * to be called from governor in response to DEVFREQ_GOV_STOP
296 * event when device is removed from devfreq framework.
298 void devfreq_monitor_stop(struct devfreq *devfreq)
300 cancel_delayed_work_sync(&devfreq->work);
302 EXPORT_SYMBOL(devfreq_monitor_stop);
305 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
306 * @devfreq: the devfreq instance.
308 * Helper function to suspend devfreq device load monitoing. Function
309 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
310 * event or when polling interval is set to zero.
312 * Note: Though this function is same as devfreq_monitor_stop(),
313 * intentionally kept separate to provide hooks for collecting
314 * transition statistics.
316 void devfreq_monitor_suspend(struct devfreq *devfreq)
318 mutex_lock(&devfreq->lock);
319 if (devfreq->stop_polling) {
320 mutex_unlock(&devfreq->lock);
324 devfreq_update_status(devfreq, devfreq->previous_freq);
325 devfreq->stop_polling = true;
326 mutex_unlock(&devfreq->lock);
327 cancel_delayed_work_sync(&devfreq->work);
329 EXPORT_SYMBOL(devfreq_monitor_suspend);
332 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
333 * @devfreq: the devfreq instance.
335 * Helper function to resume devfreq device load monitoing. Function
336 * to be called from governor in response to DEVFREQ_GOV_RESUME
337 * event or when polling interval is set to non-zero.
339 void devfreq_monitor_resume(struct devfreq *devfreq)
343 mutex_lock(&devfreq->lock);
344 if (!devfreq->stop_polling)
347 if (!delayed_work_pending(&devfreq->work) &&
348 devfreq->profile->polling_ms)
349 queue_delayed_work(devfreq_wq, &devfreq->work,
350 msecs_to_jiffies(devfreq->profile->polling_ms));
352 devfreq->last_stat_updated = jiffies;
353 devfreq->stop_polling = false;
355 if (devfreq->profile->get_cur_freq &&
356 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
357 devfreq->previous_freq = freq;
360 mutex_unlock(&devfreq->lock);
362 EXPORT_SYMBOL(devfreq_monitor_resume);
365 * devfreq_interval_update() - Update device devfreq monitoring interval
366 * @devfreq: the devfreq instance.
367 * @delay: new polling interval to be set.
369 * Helper function to set new load monitoring polling interval. Function
370 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
372 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
374 unsigned int cur_delay = devfreq->profile->polling_ms;
375 unsigned int new_delay = *delay;
377 mutex_lock(&devfreq->lock);
378 devfreq->profile->polling_ms = new_delay;
380 if (devfreq->stop_polling)
383 /* if new delay is zero, stop polling */
385 mutex_unlock(&devfreq->lock);
386 cancel_delayed_work_sync(&devfreq->work);
390 /* if current delay is zero, start polling with new delay */
392 queue_delayed_work(devfreq_wq, &devfreq->work,
393 msecs_to_jiffies(devfreq->profile->polling_ms));
397 /* if current delay is greater than new delay, restart polling */
398 if (cur_delay > new_delay) {
399 mutex_unlock(&devfreq->lock);
400 cancel_delayed_work_sync(&devfreq->work);
401 mutex_lock(&devfreq->lock);
402 if (!devfreq->stop_polling)
403 queue_delayed_work(devfreq_wq, &devfreq->work,
404 msecs_to_jiffies(devfreq->profile->polling_ms));
407 mutex_unlock(&devfreq->lock);
409 EXPORT_SYMBOL(devfreq_interval_update);
412 * devfreq_notifier_call() - Notify that the device frequency requirements
413 * has been changed out of devfreq framework.
414 * @nb: the notifier_block (supposed to be devfreq->nb)
418 * Called by a notifier that uses devfreq->nb.
420 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
423 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
426 mutex_lock(&devfreq->lock);
427 ret = update_devfreq(devfreq);
428 mutex_unlock(&devfreq->lock);
434 * _remove_devfreq() - Remove devfreq from the list and release its resources.
435 * @devfreq: the devfreq struct
437 static void _remove_devfreq(struct devfreq *devfreq)
439 mutex_lock(&devfreq_list_lock);
440 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
441 mutex_unlock(&devfreq_list_lock);
442 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
445 list_del(&devfreq->node);
446 mutex_unlock(&devfreq_list_lock);
448 if (devfreq->governor)
449 devfreq->governor->event_handler(devfreq,
450 DEVFREQ_GOV_STOP, NULL);
452 if (devfreq->profile->exit)
453 devfreq->profile->exit(devfreq->dev.parent);
455 mutex_destroy(&devfreq->lock);
460 * devfreq_dev_release() - Callback for struct device to release the device.
461 * @dev: the devfreq device
463 * This calls _remove_devfreq() if _remove_devfreq() is not called.
465 static void devfreq_dev_release(struct device *dev)
467 struct devfreq *devfreq = to_devfreq(dev);
469 _remove_devfreq(devfreq);
473 * devfreq_add_device() - Add devfreq feature to the device
474 * @dev: the device to add devfreq feature.
475 * @profile: device-specific profile to run devfreq.
476 * @governor_name: name of the policy to choose frequency.
477 * @data: private data for the governor. The devfreq framework does not
480 struct devfreq *devfreq_add_device(struct device *dev,
481 struct devfreq_dev_profile *profile,
482 const char *governor_name,
485 struct devfreq *devfreq;
486 struct devfreq_governor *governor;
489 if (!dev || !profile || !governor_name) {
490 dev_err(dev, "%s: Invalid parameters.\n", __func__);
491 return ERR_PTR(-EINVAL);
494 mutex_lock(&devfreq_list_lock);
495 devfreq = find_device_devfreq(dev);
496 mutex_unlock(&devfreq_list_lock);
497 if (!IS_ERR(devfreq)) {
498 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
503 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
505 dev_err(dev, "%s: Unable to create devfreq for the device\n",
511 mutex_init(&devfreq->lock);
512 mutex_lock(&devfreq->lock);
513 devfreq->dev.parent = dev;
514 devfreq->dev.class = devfreq_class;
515 devfreq->dev.release = devfreq_dev_release;
516 devfreq->profile = profile;
517 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
518 devfreq->previous_freq = profile->initial_freq;
519 devfreq->data = data;
520 devfreq->nb.notifier_call = devfreq_notifier_call;
522 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
523 mutex_unlock(&devfreq->lock);
524 devfreq_set_freq_table(devfreq);
525 mutex_lock(&devfreq->lock);
528 devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
529 devfreq->profile->max_state *
530 devfreq->profile->max_state,
532 devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned long) *
533 devfreq->profile->max_state,
535 devfreq->last_stat_updated = jiffies;
537 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
538 err = device_register(&devfreq->dev);
540 put_device(&devfreq->dev);
541 mutex_unlock(&devfreq->lock);
545 mutex_unlock(&devfreq->lock);
547 mutex_lock(&devfreq_list_lock);
548 list_add(&devfreq->node, &devfreq_list);
550 governor = find_devfreq_governor(devfreq->governor_name);
551 if (!IS_ERR(governor))
552 devfreq->governor = governor;
553 if (devfreq->governor)
554 err = devfreq->governor->event_handler(devfreq,
555 DEVFREQ_GOV_START, NULL);
556 mutex_unlock(&devfreq_list_lock);
558 dev_err(dev, "%s: Unable to start governor for the device\n",
566 list_del(&devfreq->node);
567 device_unregister(&devfreq->dev);
572 EXPORT_SYMBOL(devfreq_add_device);
575 * devfreq_remove_device() - Remove devfreq feature from a device.
576 * @devfreq: the devfreq instance to be removed
578 * The opposite of devfreq_add_device().
580 int devfreq_remove_device(struct devfreq *devfreq)
585 device_unregister(&devfreq->dev);
586 put_device(&devfreq->dev);
590 EXPORT_SYMBOL(devfreq_remove_device);
592 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
594 struct devfreq **r = res;
596 if (WARN_ON(!r || !*r))
602 static void devm_devfreq_dev_release(struct device *dev, void *res)
604 devfreq_remove_device(*(struct devfreq **)res);
608 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
609 * @dev: the device to add devfreq feature.
610 * @profile: device-specific profile to run devfreq.
611 * @governor_name: name of the policy to choose frequency.
612 * @data: private data for the governor. The devfreq framework does not
615 * This function manages automatically the memory of devfreq device using device
616 * resource management and simplify the free operation for memory of devfreq
619 struct devfreq *devm_devfreq_add_device(struct device *dev,
620 struct devfreq_dev_profile *profile,
621 const char *governor_name,
624 struct devfreq **ptr, *devfreq;
626 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
628 return ERR_PTR(-ENOMEM);
630 devfreq = devfreq_add_device(dev, profile, governor_name, data);
631 if (IS_ERR(devfreq)) {
633 return ERR_PTR(-ENOMEM);
637 devres_add(dev, ptr);
641 EXPORT_SYMBOL(devm_devfreq_add_device);
645 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
646 * @dev - instance to the given device
647 * @index - index into list of devfreq
649 * return the instance of devfreq device
651 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
653 struct device_node *node;
654 struct devfreq *devfreq;
657 return ERR_PTR(-EINVAL);
660 return ERR_PTR(-EINVAL);
662 node = of_parse_phandle(dev->of_node, "devfreq", index);
664 return ERR_PTR(-ENODEV);
666 mutex_lock(&devfreq_list_lock);
667 list_for_each_entry(devfreq, &devfreq_list, node) {
668 if (devfreq->dev.parent
669 && devfreq->dev.parent->of_node == node) {
670 mutex_unlock(&devfreq_list_lock);
674 mutex_unlock(&devfreq_list_lock);
676 return ERR_PTR(-EPROBE_DEFER);
679 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
681 return ERR_PTR(-ENODEV);
683 #endif /* CONFIG_OF */
684 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
687 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
688 * @dev: the device to add devfreq feature.
689 * @devfreq: the devfreq instance to be removed
691 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
693 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
694 devm_devfreq_dev_match, devfreq));
696 EXPORT_SYMBOL(devm_devfreq_remove_device);
699 * devfreq_suspend_device() - Suspend devfreq of a device.
700 * @devfreq: the devfreq instance to be suspended
702 * This function is intended to be called by the pm callbacks
703 * (e.g., runtime_suspend, suspend) of the device driver that
706 int devfreq_suspend_device(struct devfreq *devfreq)
711 if (!devfreq->governor)
714 return devfreq->governor->event_handler(devfreq,
715 DEVFREQ_GOV_SUSPEND, NULL);
717 EXPORT_SYMBOL(devfreq_suspend_device);
720 * devfreq_resume_device() - Resume devfreq of a device.
721 * @devfreq: the devfreq instance to be resumed
723 * This function is intended to be called by the pm callbacks
724 * (e.g., runtime_resume, resume) of the device driver that
727 int devfreq_resume_device(struct devfreq *devfreq)
732 if (!devfreq->governor)
735 return devfreq->governor->event_handler(devfreq,
736 DEVFREQ_GOV_RESUME, NULL);
738 EXPORT_SYMBOL(devfreq_resume_device);
741 * devfreq_add_governor() - Add devfreq governor
742 * @governor: the devfreq governor to be added
744 int devfreq_add_governor(struct devfreq_governor *governor)
746 struct devfreq_governor *g;
747 struct devfreq *devfreq;
751 pr_err("%s: Invalid parameters.\n", __func__);
755 mutex_lock(&devfreq_list_lock);
756 g = find_devfreq_governor(governor->name);
758 pr_err("%s: governor %s already registered\n", __func__,
764 list_add(&governor->node, &devfreq_governor_list);
766 list_for_each_entry(devfreq, &devfreq_list, node) {
768 struct device *dev = devfreq->dev.parent;
770 if (!strncmp(devfreq->governor_name, governor->name,
772 /* The following should never occur */
773 if (devfreq->governor) {
775 "%s: Governor %s already present\n",
776 __func__, devfreq->governor->name);
777 ret = devfreq->governor->event_handler(devfreq,
778 DEVFREQ_GOV_STOP, NULL);
781 "%s: Governor %s stop = %d\n",
783 devfreq->governor->name, ret);
787 devfreq->governor = governor;
788 ret = devfreq->governor->event_handler(devfreq,
789 DEVFREQ_GOV_START, NULL);
791 dev_warn(dev, "%s: Governor %s start=%d\n",
792 __func__, devfreq->governor->name,
799 mutex_unlock(&devfreq_list_lock);
803 EXPORT_SYMBOL(devfreq_add_governor);
806 * devfreq_remove_device() - Remove devfreq feature from a device.
807 * @governor: the devfreq governor to be removed
809 int devfreq_remove_governor(struct devfreq_governor *governor)
811 struct devfreq_governor *g;
812 struct devfreq *devfreq;
816 pr_err("%s: Invalid parameters.\n", __func__);
820 mutex_lock(&devfreq_list_lock);
821 g = find_devfreq_governor(governor->name);
823 pr_err("%s: governor %s not registered\n", __func__,
828 list_for_each_entry(devfreq, &devfreq_list, node) {
830 struct device *dev = devfreq->dev.parent;
832 if (!strncmp(devfreq->governor_name, governor->name,
834 /* we should have a devfreq governor! */
835 if (!devfreq->governor) {
836 dev_warn(dev, "%s: Governor %s NOT present\n",
837 __func__, governor->name);
841 ret = devfreq->governor->event_handler(devfreq,
842 DEVFREQ_GOV_STOP, NULL);
844 dev_warn(dev, "%s: Governor %s stop=%d\n",
845 __func__, devfreq->governor->name,
848 devfreq->governor = NULL;
852 list_del(&governor->node);
854 mutex_unlock(&devfreq_list_lock);
858 EXPORT_SYMBOL(devfreq_remove_governor);
860 static ssize_t governor_show(struct device *dev,
861 struct device_attribute *attr, char *buf)
863 if (!to_devfreq(dev)->governor)
866 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
869 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
870 const char *buf, size_t count)
872 struct devfreq *df = to_devfreq(dev);
874 char str_governor[DEVFREQ_NAME_LEN + 1];
875 struct devfreq_governor *governor;
877 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
881 mutex_lock(&devfreq_list_lock);
882 governor = find_devfreq_governor(str_governor);
883 if (IS_ERR(governor)) {
884 ret = PTR_ERR(governor);
887 if (df->governor == governor) {
893 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
895 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
896 __func__, df->governor->name, ret);
900 df->governor = governor;
901 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
902 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
904 dev_warn(dev, "%s: Governor %s not started(%d)\n",
905 __func__, df->governor->name, ret);
907 mutex_unlock(&devfreq_list_lock);
913 static DEVICE_ATTR_RW(governor);
915 static ssize_t available_governors_show(struct device *d,
916 struct device_attribute *attr,
919 struct devfreq_governor *tmp_governor;
922 mutex_lock(&devfreq_list_lock);
923 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
924 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
925 "%s ", tmp_governor->name);
926 mutex_unlock(&devfreq_list_lock);
928 /* Truncate the trailing space */
932 count += sprintf(&buf[count], "\n");
936 static DEVICE_ATTR_RO(available_governors);
938 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
942 struct devfreq *devfreq = to_devfreq(dev);
944 if (devfreq->profile->get_cur_freq &&
945 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
946 return sprintf(buf, "%lu\n", freq);
948 return sprintf(buf, "%lu\n", devfreq->previous_freq);
950 static DEVICE_ATTR_RO(cur_freq);
952 static ssize_t target_freq_show(struct device *dev,
953 struct device_attribute *attr, char *buf)
955 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
957 static DEVICE_ATTR_RO(target_freq);
959 static ssize_t polling_interval_show(struct device *dev,
960 struct device_attribute *attr, char *buf)
962 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
965 static ssize_t polling_interval_store(struct device *dev,
966 struct device_attribute *attr,
967 const char *buf, size_t count)
969 struct devfreq *df = to_devfreq(dev);
976 ret = sscanf(buf, "%u", &value);
980 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
985 static DEVICE_ATTR_RW(polling_interval);
987 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
988 const char *buf, size_t count)
990 struct devfreq *df = to_devfreq(dev);
995 ret = sscanf(buf, "%lu", &value);
999 mutex_lock(&df->lock);
1001 if (value && max && value > max) {
1006 df->min_freq = value;
1010 mutex_unlock(&df->lock);
1014 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1015 const char *buf, size_t count)
1017 struct devfreq *df = to_devfreq(dev);
1018 unsigned long value;
1022 ret = sscanf(buf, "%lu", &value);
1026 mutex_lock(&df->lock);
1028 if (value && min && value < min) {
1033 df->max_freq = value;
1037 mutex_unlock(&df->lock);
1041 #define show_one(name) \
1042 static ssize_t name##_show \
1043 (struct device *dev, struct device_attribute *attr, char *buf) \
1045 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
1050 static DEVICE_ATTR_RW(min_freq);
1051 static DEVICE_ATTR_RW(max_freq);
1053 static ssize_t available_frequencies_show(struct device *d,
1054 struct device_attribute *attr,
1057 struct devfreq *df = to_devfreq(d);
1058 struct device *dev = df->dev.parent;
1059 struct dev_pm_opp *opp;
1061 unsigned long freq = 0;
1065 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
1069 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1075 /* Truncate the trailing space */
1079 count += sprintf(&buf[count], "\n");
1083 static DEVICE_ATTR_RO(available_frequencies);
1085 static ssize_t trans_stat_show(struct device *dev,
1086 struct device_attribute *attr, char *buf)
1088 struct devfreq *devfreq = to_devfreq(dev);
1091 unsigned int max_state = devfreq->profile->max_state;
1093 if (!devfreq->stop_polling &&
1094 devfreq_update_status(devfreq, devfreq->previous_freq))
1097 return sprintf(buf, "Not Supported.\n");
1099 len = sprintf(buf, " From : To\n");
1100 len += sprintf(buf + len, " :");
1101 for (i = 0; i < max_state; i++)
1102 len += sprintf(buf + len, "%10lu",
1103 devfreq->profile->freq_table[i]);
1105 len += sprintf(buf + len, " time(ms)\n");
1107 for (i = 0; i < max_state; i++) {
1108 if (devfreq->profile->freq_table[i]
1109 == devfreq->previous_freq) {
1110 len += sprintf(buf + len, "*");
1112 len += sprintf(buf + len, " ");
1114 len += sprintf(buf + len, "%10lu:",
1115 devfreq->profile->freq_table[i]);
1116 for (j = 0; j < max_state; j++)
1117 len += sprintf(buf + len, "%10u",
1118 devfreq->trans_table[(i * max_state) + j]);
1119 len += sprintf(buf + len, "%10u\n",
1120 jiffies_to_msecs(devfreq->time_in_state[i]));
1123 len += sprintf(buf + len, "Total transition : %u\n",
1124 devfreq->total_trans);
1127 static DEVICE_ATTR_RO(trans_stat);
1129 static struct attribute *devfreq_attrs[] = {
1130 &dev_attr_governor.attr,
1131 &dev_attr_available_governors.attr,
1132 &dev_attr_cur_freq.attr,
1133 &dev_attr_available_frequencies.attr,
1134 &dev_attr_target_freq.attr,
1135 &dev_attr_polling_interval.attr,
1136 &dev_attr_min_freq.attr,
1137 &dev_attr_max_freq.attr,
1138 &dev_attr_trans_stat.attr,
1141 ATTRIBUTE_GROUPS(devfreq);
1143 static int __init devfreq_init(void)
1145 devfreq_class = class_create(THIS_MODULE, "devfreq");
1146 if (IS_ERR(devfreq_class)) {
1147 pr_err("%s: couldn't create class\n", __FILE__);
1148 return PTR_ERR(devfreq_class);
1151 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1153 class_destroy(devfreq_class);
1154 pr_err("%s: couldn't create workqueue\n", __FILE__);
1157 devfreq_class->dev_groups = devfreq_groups;
1161 subsys_initcall(devfreq_init);
1163 static void __exit devfreq_exit(void)
1165 class_destroy(devfreq_class);
1166 destroy_workqueue(devfreq_wq);
1168 module_exit(devfreq_exit);
1171 * The followings are helper functions for devfreq user device drivers with
1176 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1177 * freq value given to target callback.
1178 * @dev: The devfreq user device. (parent of devfreq)
1179 * @freq: The frequency given to target function
1180 * @flags: Flags handed from devfreq framework.
1182 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1183 * protected pointer. The reason for the same is that the opp pointer which is
1184 * returned will remain valid for use with opp_get_{voltage, freq} only while
1185 * under the locked area. The pointer returned must be used prior to unlocking
1186 * with rcu_read_unlock() to maintain the integrity of the pointer.
1188 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1189 unsigned long *freq,
1192 struct dev_pm_opp *opp;
1194 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1195 /* The freq is an upper bound. opp should be lower */
1196 opp = dev_pm_opp_find_freq_floor(dev, freq);
1198 /* If not available, use the closest opp */
1199 if (opp == ERR_PTR(-ERANGE))
1200 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1202 /* The freq is an lower bound. opp should be higher */
1203 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1205 /* If not available, use the closest opp */
1206 if (opp == ERR_PTR(-ERANGE))
1207 opp = dev_pm_opp_find_freq_floor(dev, freq);
1212 EXPORT_SYMBOL(devfreq_recommended_opp);
1215 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1216 * for any changes in the OPP availability
1218 * @dev: The devfreq user device. (parent of devfreq)
1219 * @devfreq: The devfreq object.
1221 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1223 struct srcu_notifier_head *nh;
1227 nh = dev_pm_opp_get_notifier(dev);
1232 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1236 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1239 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1240 * notified for any changes in the OPP
1241 * availability changes anymore.
1242 * @dev: The devfreq user device. (parent of devfreq)
1243 * @devfreq: The devfreq object.
1245 * At exit() callback of devfreq_dev_profile, this must be included if
1246 * devfreq_recommended_opp is used.
1248 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1250 struct srcu_notifier_head *nh;
1254 nh = dev_pm_opp_get_notifier(dev);
1259 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1263 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1265 static void devm_devfreq_opp_release(struct device *dev, void *res)
1267 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1271 * devm_ devfreq_register_opp_notifier()
1272 * - Resource-managed devfreq_register_opp_notifier()
1273 * @dev: The devfreq user device. (parent of devfreq)
1274 * @devfreq: The devfreq object.
1276 int devm_devfreq_register_opp_notifier(struct device *dev,
1277 struct devfreq *devfreq)
1279 struct devfreq **ptr;
1282 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1286 ret = devfreq_register_opp_notifier(dev, devfreq);
1293 devres_add(dev, ptr);
1297 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1300 * devm_devfreq_unregister_opp_notifier()
1301 * - Resource-managed devfreq_unregister_opp_notifier()
1302 * @dev: The devfreq user device. (parent of devfreq)
1303 * @devfreq: The devfreq object.
1305 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1306 struct devfreq *devfreq)
1308 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1309 devm_devfreq_dev_match, devfreq));
1311 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1313 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1314 MODULE_DESCRIPTION("devfreq class support");
1315 MODULE_LICENSE("GPL");