blob: 82aeed1f5a4711a60d7e77f5ea82bde5f9f13188 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
4 * for Non-CPU Devices.
5 *
6 * Copyright (C) 2011 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 */
9
10#include <linux/kernel.h>
11#include <linux/kmod.h>
12#include <linux/sched.h>
13#include <linux/errno.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/export.h>
17#include <linux/slab.h>
18#include <linux/stat.h>
19#include <linux/pm_opp.h>
20#include <linux/devfreq.h>
21#include <linux/workqueue.h>
22#include <linux/platform_device.h>
23#include <linux/list.h>
24#include <linux/printk.h>
25#include <linux/hrtimer.h>
26#include <linux/of.h>
27#include "governor.h"
28
29#define CREATE_TRACE_POINTS
30#include <trace/events/devfreq.h>
31
32static struct class *devfreq_class;
33
34/*
35 * devfreq core provides delayed work based load monitoring helper
36 * functions. Governors can use these or can implement their own
37 * monitoring mechanism.
38 */
39static struct workqueue_struct *devfreq_wq;
40
41/* The list of all device-devfreq governors */
42static LIST_HEAD(devfreq_governor_list);
43/* The list of all device-devfreq */
44static LIST_HEAD(devfreq_list);
45static DEFINE_MUTEX(devfreq_list_lock);
46
47/**
48 * find_device_devfreq() - find devfreq struct using device pointer
49 * @dev: device pointer used to lookup device devfreq.
50 *
51 * Search the list of device devfreqs and return the matched device's
52 * devfreq info. devfreq_list_lock should be held by the caller.
53 */
54static struct devfreq *find_device_devfreq(struct device *dev)
55{
56 struct devfreq *tmp_devfreq;
57
58 if (IS_ERR_OR_NULL(dev)) {
59 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
60 return ERR_PTR(-EINVAL);
61 }
62 WARN(!mutex_is_locked(&devfreq_list_lock),
63 "devfreq_list_lock must be locked.");
64
65 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
66 if (tmp_devfreq->dev.parent == dev)
67 return tmp_devfreq;
68 }
69
70 return ERR_PTR(-ENODEV);
71}
72
73static unsigned long find_available_min_freq(struct devfreq *devfreq)
74{
75 struct dev_pm_opp *opp;
76 unsigned long min_freq = 0;
77
78 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
79 if (IS_ERR(opp))
80 min_freq = 0;
81 else
82 dev_pm_opp_put(opp);
83
84 return min_freq;
85}
86
87static unsigned long find_available_max_freq(struct devfreq *devfreq)
88{
89 struct dev_pm_opp *opp;
90 unsigned long max_freq = ULONG_MAX;
91
92 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
93 if (IS_ERR(opp))
94 max_freq = 0;
95 else
96 dev_pm_opp_put(opp);
97
98 return max_freq;
99}
100
101/**
102 * devfreq_get_freq_level() - Lookup freq_table for the frequency
103 * @devfreq: the devfreq instance
104 * @freq: the target frequency
105 */
106static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
107{
108 int lev;
109
110 for (lev = 0; lev < devfreq->profile->max_state; lev++)
111 if (freq == devfreq->profile->freq_table[lev])
112 return lev;
113
114 return -EINVAL;
115}
116
117static int set_freq_table(struct devfreq *devfreq)
118{
119 struct devfreq_dev_profile *profile = devfreq->profile;
120 struct dev_pm_opp *opp;
121 unsigned long freq;
122 int i, count;
123
124 /* Initialize the freq_table from OPP table */
125 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
126 if (count <= 0)
127 return -EINVAL;
128
129 profile->max_state = count;
130 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
131 profile->max_state,
132 sizeof(*profile->freq_table),
133 GFP_KERNEL);
134 if (!profile->freq_table) {
135 profile->max_state = 0;
136 return -ENOMEM;
137 }
138
139 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
140 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
141 if (IS_ERR(opp)) {
142 devm_kfree(devfreq->dev.parent, profile->freq_table);
143 profile->max_state = 0;
144 return PTR_ERR(opp);
145 }
146 dev_pm_opp_put(opp);
147 profile->freq_table[i] = freq;
148 }
149
150 return 0;
151}
152
153/**
154 * devfreq_update_status() - Update statistics of devfreq behavior
155 * @devfreq: the devfreq instance
156 * @freq: the update target frequency
157 */
158int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
159{
160 int lev, prev_lev, ret = 0;
161 unsigned long cur_time;
162
163 lockdep_assert_held(&devfreq->lock);
164 cur_time = jiffies;
165
166 /* Immediately exit if previous_freq is not initialized yet. */
167 if (!devfreq->previous_freq)
168 goto out;
169
170 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
171 if (prev_lev < 0) {
172 ret = prev_lev;
173 goto out;
174 }
175
176 devfreq->time_in_state[prev_lev] +=
177 cur_time - devfreq->last_stat_updated;
178
179 lev = devfreq_get_freq_level(devfreq, freq);
180 if (lev < 0) {
181 ret = lev;
182 goto out;
183 }
184
185 if (lev != prev_lev) {
186 devfreq->trans_table[(prev_lev *
187 devfreq->profile->max_state) + lev]++;
188 devfreq->total_trans++;
189 }
190
191out:
192 devfreq->last_stat_updated = cur_time;
193 return ret;
194}
195EXPORT_SYMBOL(devfreq_update_status);
196
197/**
198 * find_devfreq_governor() - find devfreq governor from name
199 * @name: name of the governor
200 *
201 * Search the list of devfreq governors and return the matched
202 * governor's pointer. devfreq_list_lock should be held by the caller.
203 */
204static struct devfreq_governor *find_devfreq_governor(const char *name)
205{
206 struct devfreq_governor *tmp_governor;
207
208 if (IS_ERR_OR_NULL(name)) {
209 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
210 return ERR_PTR(-EINVAL);
211 }
212 WARN(!mutex_is_locked(&devfreq_list_lock),
213 "devfreq_list_lock must be locked.");
214
215 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
216 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
217 return tmp_governor;
218 }
219
220 return ERR_PTR(-ENODEV);
221}
222
223/**
224 * try_then_request_governor() - Try to find the governor and request the
225 * module if is not found.
226 * @name: name of the governor
227 *
228 * Search the list of devfreq governors and request the module and try again
229 * if is not found. This can happen when both drivers (the governor driver
230 * and the driver that call devfreq_add_device) are built as modules.
231 * devfreq_list_lock should be held by the caller. Returns the matched
232 * governor's pointer or an error pointer.
233 */
234static struct devfreq_governor *try_then_request_governor(const char *name)
235{
236 struct devfreq_governor *governor;
237 int err = 0;
238
239 if (IS_ERR_OR_NULL(name)) {
240 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
241 return ERR_PTR(-EINVAL);
242 }
243 WARN(!mutex_is_locked(&devfreq_list_lock),
244 "devfreq_list_lock must be locked.");
245
246 governor = find_devfreq_governor(name);
247 if (IS_ERR(governor)) {
248 mutex_unlock(&devfreq_list_lock);
249
250 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
251 DEVFREQ_NAME_LEN))
252 err = request_module("governor_%s", "simpleondemand");
253 else
254 err = request_module("governor_%s", name);
255 /* Restore previous state before return */
256 mutex_lock(&devfreq_list_lock);
257 if (err)
258 return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
259
260 governor = find_devfreq_governor(name);
261 }
262
263 return governor;
264}
265
266static int devfreq_notify_transition(struct devfreq *devfreq,
267 struct devfreq_freqs *freqs, unsigned int state)
268{
269 if (!devfreq)
270 return -EINVAL;
271
272 switch (state) {
273 case DEVFREQ_PRECHANGE:
274 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
275 DEVFREQ_PRECHANGE, freqs);
276 break;
277
278 case DEVFREQ_POSTCHANGE:
279 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
280 DEVFREQ_POSTCHANGE, freqs);
281 break;
282 default:
283 return -EINVAL;
284 }
285
286 return 0;
287}
288
289static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
290 u32 flags)
291{
292 struct devfreq_freqs freqs;
293 unsigned long cur_freq;
294 int err = 0;
295
296 if (devfreq->profile->get_cur_freq)
297 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
298 else
299 cur_freq = devfreq->previous_freq;
300
301 freqs.old = cur_freq;
302 freqs.new = new_freq;
303 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
304
305 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
306 if (err) {
307 freqs.new = cur_freq;
308 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
309 return err;
310 }
311
312 freqs.new = new_freq;
313 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
314
315 if (devfreq_update_status(devfreq, new_freq))
316 dev_err(&devfreq->dev,
317 "Couldn't update frequency transition information.\n");
318
319 devfreq->previous_freq = new_freq;
320
321 if (devfreq->suspend_freq)
322 devfreq->resume_freq = new_freq;
323
324 return err;
325}
326
327/* Load monitoring helper functions for governors use */
328
329/**
330 * update_devfreq() - Reevaluate the device and configure frequency.
331 * @devfreq: the devfreq instance.
332 *
333 * Note: Lock devfreq->lock before calling update_devfreq
334 * This function is exported for governors.
335 */
336int update_devfreq(struct devfreq *devfreq)
337{
338 unsigned long freq;
339 int err = 0;
340 u32 flags = 0;
341
342 if (!mutex_is_locked(&devfreq->lock)) {
343 WARN(true, "devfreq->lock must be locked by the caller.\n");
344 return -EINVAL;
345 }
346
347 if (!devfreq->governor)
348 return -EINVAL;
349
350 /* Reevaluate the proper frequency */
351 err = devfreq->governor->get_target_freq(devfreq, &freq);
352 if (err)
353 return err;
354
355 /*
356 * Adjust the frequency with user freq, QoS and available freq.
357 *
358 * List from the highest priority
359 * max_freq
360 * min_freq
361 */
362#if 0
363 max_freq = min(devfreq->scaling_max_freq, devfreq->max_freq);
364 min_freq = max(devfreq->scaling_min_freq, devfreq->min_freq);
365
366 if (freq < min_freq) {
367 freq = min_freq;
368 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
369 }
370 if (freq > max_freq) {
371 freq = max_freq;
372 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
373 }
374#else
375 if (devfreq->profile->min_qos_type)
376 freq = max(freq, devfreq->qos_min_freq);
377 freq = max(freq, devfreq->min_freq);
378 if (devfreq->profile->max_qos_type)
379 freq = min(freq, devfreq->qos_max_freq);
380 freq = min(freq, devfreq->max_freq);
381
382 if ((devfreq->profile->max_qos_type && (freq == devfreq->qos_max_freq))
383 || (freq == devfreq->max_freq))
384 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
385 else
386 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
387#endif
388 return devfreq_set_target(devfreq, freq, flags);
389
390}
391EXPORT_SYMBOL(update_devfreq);
392
393/**
394 * devfreq_monitor() - Periodically poll devfreq objects.
395 * @work: the work struct used to run devfreq_monitor periodically.
396 *
397 */
398static void devfreq_monitor(struct work_struct *work)
399{
400 int err;
401 struct devfreq *devfreq = container_of(work,
402 struct devfreq, work.work);
403
404 mutex_lock(&devfreq->lock);
405 err = update_devfreq(devfreq);
406 if (err)
407 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
408
409 queue_delayed_work(devfreq_wq, &devfreq->work,
410 msecs_to_jiffies(devfreq->profile->polling_ms));
411 mutex_unlock(&devfreq->lock);
412
413 trace_devfreq_monitor(devfreq);
414}
415
416/**
417 * devfreq_monitor_start() - Start load monitoring of devfreq instance
418 * @devfreq: the devfreq instance.
419 *
420 * Helper function for starting devfreq device load monitoring. By
421 * default delayed work based monitoring is supported. Function
422 * to be called from governor in response to DEVFREQ_GOV_START
423 * event when device is added to devfreq framework.
424 */
425void devfreq_monitor_start(struct devfreq *devfreq)
426{
427 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
428 if (devfreq->profile->polling_ms)
429 queue_delayed_work(devfreq_wq, &devfreq->work,
430 msecs_to_jiffies(devfreq->profile->polling_ms));
431}
432EXPORT_SYMBOL(devfreq_monitor_start);
433
434/**
435 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
436 * @devfreq: the devfreq instance.
437 *
438 * Helper function to stop devfreq device load monitoring. Function
439 * to be called from governor in response to DEVFREQ_GOV_STOP
440 * event when device is removed from devfreq framework.
441 */
442void devfreq_monitor_stop(struct devfreq *devfreq)
443{
444 cancel_delayed_work_sync(&devfreq->work);
445}
446EXPORT_SYMBOL(devfreq_monitor_stop);
447
448/**
449 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
450 * @devfreq: the devfreq instance.
451 *
452 * Helper function to suspend devfreq device load monitoring. Function
453 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
454 * event or when polling interval is set to zero.
455 *
456 * Note: Though this function is same as devfreq_monitor_stop(),
457 * intentionally kept separate to provide hooks for collecting
458 * transition statistics.
459 */
460void devfreq_monitor_suspend(struct devfreq *devfreq)
461{
462 mutex_lock(&devfreq->lock);
463 if (devfreq->stop_polling) {
464 mutex_unlock(&devfreq->lock);
465 return;
466 }
467
468 devfreq_update_status(devfreq, devfreq->previous_freq);
469 devfreq->stop_polling = true;
470 mutex_unlock(&devfreq->lock);
471 cancel_delayed_work_sync(&devfreq->work);
472}
473EXPORT_SYMBOL(devfreq_monitor_suspend);
474
475/**
476 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
477 * @devfreq: the devfreq instance.
478 *
479 * Helper function to resume devfreq device load monitoring. Function
480 * to be called from governor in response to DEVFREQ_GOV_RESUME
481 * event or when polling interval is set to non-zero.
482 */
483void devfreq_monitor_resume(struct devfreq *devfreq)
484{
485 unsigned long freq;
486
487 mutex_lock(&devfreq->lock);
488 if (!devfreq->stop_polling)
489 goto out;
490
491 if (!delayed_work_pending(&devfreq->work) &&
492 devfreq->profile->polling_ms)
493 queue_delayed_work(devfreq_wq, &devfreq->work,
494 msecs_to_jiffies(devfreq->profile->polling_ms));
495
496 devfreq->last_stat_updated = jiffies;
497 devfreq->stop_polling = false;
498
499 if (devfreq->profile->get_cur_freq &&
500 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
501 devfreq->previous_freq = freq;
502
503out:
504 mutex_unlock(&devfreq->lock);
505}
506EXPORT_SYMBOL(devfreq_monitor_resume);
507
508/**
509 * devfreq_interval_update() - Update device devfreq monitoring interval
510 * @devfreq: the devfreq instance.
511 * @delay: new polling interval to be set.
512 *
513 * Helper function to set new load monitoring polling interval. Function
514 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
515 */
516void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
517{
518 unsigned int cur_delay = devfreq->profile->polling_ms;
519 unsigned int new_delay = *delay;
520
521 mutex_lock(&devfreq->lock);
522 devfreq->profile->polling_ms = new_delay;
523
524 if (devfreq->stop_polling)
525 goto out;
526
527 /* if new delay is zero, stop polling */
528 if (!new_delay) {
529 mutex_unlock(&devfreq->lock);
530 cancel_delayed_work_sync(&devfreq->work);
531 return;
532 }
533
534 /* if current delay is zero, start polling with new delay */
535 if (!cur_delay) {
536 queue_delayed_work(devfreq_wq, &devfreq->work,
537 msecs_to_jiffies(devfreq->profile->polling_ms));
538 goto out;
539 }
540
541 /* if current delay is greater than new delay, restart polling */
542 if (cur_delay > new_delay) {
543 mutex_unlock(&devfreq->lock);
544 cancel_delayed_work_sync(&devfreq->work);
545 mutex_lock(&devfreq->lock);
546 if (!devfreq->stop_polling)
547 queue_delayed_work(devfreq_wq, &devfreq->work,
548 msecs_to_jiffies(devfreq->profile->polling_ms));
549 }
550out:
551 mutex_unlock(&devfreq->lock);
552}
553EXPORT_SYMBOL(devfreq_interval_update);
554
555/**
556 * devfreq_notifier_call() - Notify that the device frequency requirements
557 * has been changed out of devfreq framework.
558 * @nb: the notifier_block (supposed to be devfreq->nb)
559 * @type: not used
560 * @devp: not used
561 *
562 * Called by a notifier that uses devfreq->nb.
563 */
564static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
565 void *devp)
566{
567 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
568 int err = -EINVAL;
569
570 mutex_lock(&devfreq->lock);
571
572 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
573 if (!devfreq->scaling_min_freq)
574 goto out;
575
576 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
577 if (!devfreq->scaling_max_freq) {
578 devfreq->scaling_max_freq = ULONG_MAX;
579 goto out;
580 }
581
582 err = update_devfreq(devfreq);
583
584out:
585 mutex_unlock(&devfreq->lock);
586 if (err)
587 dev_err(devfreq->dev.parent,
588 "failed to update frequency from OPP notifier (%d)\n",
589 err);
590
591 return NOTIFY_OK;
592}
593
594
595/**
596 * devfreq_qos_notifier_call() - It is called when kernel driver
597 * has constraints
598 */
599static int devfreq_qos_min_notifier_call(struct notifier_block *nb,
600 unsigned long value, void *devp)
601{
602 struct devfreq *devfreq = container_of(nb, struct devfreq, qos_min_nb);
603 int default_value = PM_QOS_DEFAULT_VALUE, ret, i;
604 unsigned long *freq_tbl = devfreq->profile->freq_table;
605 unsigned int tbl_len = devfreq->profile->max_state;
606
607 mutex_lock(&devfreq->lock);
608
609 if (value == default_value) {
610 devfreq->qos_min_freq = freq_tbl[0];
611 goto update;
612 }
613
614 for (i = 0; i < tbl_len; i++) {
615 if (freq_tbl[i] >= value) {
616 devfreq->qos_min_freq = freq_tbl[i];
617 goto update;
618 }
619 }
620
621 if (i == tbl_len)
622 devfreq->qos_min_freq = freq_tbl[tbl_len - 1];
623update:
624 ret = update_devfreq(devfreq);
625 mutex_unlock(&devfreq->lock);
626
627 return ret;
628}
629
630/**
631 * devfreq_qos_max_notifier_call() - It is called when kernel driver
632 * has constraints
633 */
634static int devfreq_qos_max_notifier_call(struct notifier_block *nb,
635 unsigned long value, void *devp)
636{
637 struct devfreq *devfreq = container_of(nb, struct devfreq, qos_max_nb);
638 int default_value = PM_QOS_DEFAULT_VALUE, ret, i;
639 unsigned long *freq_tbl = devfreq->profile->freq_table;
640 unsigned int tbl_len = devfreq->profile->max_state;
641
642 mutex_lock(&devfreq->lock);
643
644 if (value == default_value) {
645 devfreq->qos_max_freq = freq_tbl[tbl_len - 1];
646 goto update;
647 }
648
649 for (i = tbl_len - 1; i >= 0; i--) {
650 if (freq_tbl[i] <= value) {
651 devfreq->qos_max_freq = freq_tbl[i];
652 goto update;
653 }
654 }
655
656 if (i < 0)
657 devfreq->qos_max_freq = freq_tbl[0];
658update:
659 ret = update_devfreq(devfreq);
660 mutex_unlock(&devfreq->lock);
661
662 return ret;
663}
664
665/**
666 * devfreq_dev_release() - Callback for struct device to release the device.
667 * @dev: the devfreq device
668 *
669 * Remove devfreq from the list and release its resources.
670 */
671static void devfreq_dev_release(struct device *dev)
672{
673 struct devfreq *devfreq = to_devfreq(dev);
674
675 mutex_lock(&devfreq_list_lock);
676 list_del(&devfreq->node);
677 mutex_unlock(&devfreq_list_lock);
678
679 if (devfreq->profile->exit)
680 devfreq->profile->exit(devfreq->dev.parent);
681
682 mutex_destroy(&devfreq->lock);
683 srcu_cleanup_notifier_head(&devfreq->transition_notifier_list);
684 kfree(devfreq);
685}
686
687/**
688 * devfreq_add_device() - Add devfreq feature to the device
689 * @dev: the device to add devfreq feature.
690 * @profile: device-specific profile to run devfreq.
691 * @governor_name: name of the policy to choose frequency.
692 * @data: devfreq driver pass to governors, governor should not change it.
693 */
694struct devfreq *devfreq_add_device(struct device *dev,
695 struct devfreq_dev_profile *profile,
696 const char *governor_name,
697 void *data)
698{
699 struct devfreq *devfreq;
700 struct devfreq_governor *governor;
701 int err = 0;
702 int i = 0;
703
704 if (!dev || !profile || !governor_name) {
705 dev_err(dev, "%s: Invalid parameters.\n", __func__);
706 return ERR_PTR(-EINVAL);
707 }
708
709 mutex_lock(&devfreq_list_lock);
710 devfreq = find_device_devfreq(dev);
711 mutex_unlock(&devfreq_list_lock);
712 if (!IS_ERR(devfreq)) {
713 dev_err(dev, "%s: Unable to create devfreq for the device.\n",
714 __func__);
715 err = -EINVAL;
716 goto err_out;
717 }
718
719 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
720 if (!devfreq) {
721 err = -ENOMEM;
722 goto err_out;
723 }
724
725 mutex_init(&devfreq->lock);
726 mutex_lock(&devfreq->lock);
727 devfreq->dev.parent = dev;
728 devfreq->dev.class = devfreq_class;
729 devfreq->dev.release = devfreq_dev_release;
730 INIT_LIST_HEAD(&devfreq->node);
731 devfreq->profile = profile;
732 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
733 devfreq->previous_freq = profile->initial_freq;
734 devfreq->last_status.current_frequency = profile->initial_freq;
735 devfreq->data = data;
736 devfreq->nb.notifier_call = devfreq_notifier_call;
737 devfreq->qos_min_nb.notifier_call = devfreq_qos_min_notifier_call;
738 devfreq->qos_max_nb.notifier_call = devfreq_qos_max_notifier_call;
739
740 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
741 mutex_unlock(&devfreq->lock);
742 err = set_freq_table(devfreq);
743 if (err < 0)
744 goto err_dev;
745 mutex_lock(&devfreq->lock);
746 }
747
748 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
749 if (!devfreq->scaling_min_freq) {
750 mutex_unlock(&devfreq->lock);
751 err = -EINVAL;
752 goto err_dev;
753 }
754 devfreq->min_freq = devfreq->scaling_min_freq;
755
756 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
757 if (!devfreq->scaling_max_freq) {
758 mutex_unlock(&devfreq->lock);
759 err = -EINVAL;
760 goto err_dev;
761 }
762 devfreq->max_freq = devfreq->scaling_max_freq;
763
764 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
765 atomic_set(&devfreq->suspend_count, 0);
766
767 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
768 err = device_register(&devfreq->dev);
769 if (err) {
770 mutex_unlock(&devfreq->lock);
771 put_device(&devfreq->dev);
772 goto err_out;
773 }
774
775 devfreq->trans_table = devm_kzalloc(&devfreq->dev,
776 array3_size(sizeof(unsigned int),
777 devfreq->profile->max_state,
778 devfreq->profile->max_state),
779 GFP_KERNEL);
780 if (!devfreq->trans_table) {
781 mutex_unlock(&devfreq->lock);
782 err = -ENOMEM;
783 goto err_devfreq;
784 }
785
786 devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
787 devfreq->profile->max_state,
788 sizeof(unsigned long),
789 GFP_KERNEL);
790 if (!devfreq->time_in_state) {
791 mutex_unlock(&devfreq->lock);
792 err = -ENOMEM;
793 goto err_devfreq;
794 }
795
796 devfreq->last_stat_updated = jiffies;
797
798 srcu_init_notifier_head(&devfreq->transition_notifier_list);
799
800 mutex_unlock(&devfreq->lock);
801
802 /* Check the sanity of freq list */
803 if (profile->max_state) {
804 for (i = 1; i < profile->max_state; i++) {
805 if (WARN(profile->freq_table[i] <=
806 profile->freq_table[i - 1],
807 "%s's freq not sorted in the "
808 "ascending order. ([%d]=%lu, [%d]=%lu)\n",
809 dev_name(dev), i - 1,
810 profile->freq_table[i - 1], i,
811 profile->freq_table[i])) {
812 err = -EINVAL;
813 goto err_devfreq;
814 }
815 }
816 if (profile->min_qos_type) {
817 pm_qos_add_notifier(profile->min_qos_type,
818 &devfreq->qos_min_nb);
819 profile->qos_req_min.name = "userspace";
820 /* Must be out of devfreq->lock
821 or qos notify will cause deadlock */
822 pm_qos_add_request(&profile->qos_req_min,
823 profile->min_qos_type, profile->freq_table[0]);
824 }
825 if (profile->max_qos_type) {
826 pm_qos_add_notifier(profile->max_qos_type,
827 &devfreq->qos_max_nb);
828 profile->qos_req_max.name = "userspace";
829 /* Must be out of devfreq->lock
830 or qos notify will cause deadlock */
831 pm_qos_add_request(&profile->qos_req_max,
832 profile->max_qos_type,
833 profile->freq_table[profile->max_state - 1]);
834 }
835 }
836
837 mutex_lock(&devfreq_list_lock);
838
839 governor = try_then_request_governor(devfreq->governor_name);
840 if (IS_ERR(governor)) {
841 dev_err(dev, "%s: Unable to find governor for the device\n",
842 __func__);
843 err = PTR_ERR(governor);
844 goto err_init;
845 }
846
847 devfreq->governor = governor;
848 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
849 NULL);
850 if (err) {
851 dev_err(dev, "%s: Unable to start governor for the device\n",
852 __func__);
853 goto err_init;
854 }
855
856 list_add(&devfreq->node, &devfreq_list);
857
858 mutex_unlock(&devfreq_list_lock);
859
860 return devfreq;
861
862err_init:
863 mutex_unlock(&devfreq_list_lock);
864err_devfreq:
865 devfreq_remove_device(devfreq);
866 devfreq = NULL;
867
868 if (profile->min_qos_type)
869 pm_qos_remove_notifier(profile->min_qos_type,
870 &devfreq->qos_min_nb);
871 if (profile->max_qos_type)
872 pm_qos_remove_notifier(profile->max_qos_type,
873 &devfreq->qos_max_nb);
874err_dev:
875 kfree(devfreq);
876err_out:
877 return ERR_PTR(err);
878}
879EXPORT_SYMBOL(devfreq_add_device);
880
881/**
882 * devfreq_remove_device() - Remove devfreq feature from a device.
883 * @devfreq: the devfreq instance to be removed
884 *
885 * The opposite of devfreq_add_device().
886 */
887int devfreq_remove_device(struct devfreq *devfreq)
888{
889 if (!devfreq)
890 return -EINVAL;
891
892 if (devfreq->profile->min_qos_type)
893 pm_qos_remove_notifier(devfreq->profile->min_qos_type,
894 &devfreq->qos_min_nb);
895 if (devfreq->profile->max_qos_type)
896 pm_qos_remove_notifier(devfreq->profile->max_qos_type,
897 &devfreq->qos_max_nb);
898
899 if (devfreq->governor)
900 devfreq->governor->event_handler(devfreq,
901 DEVFREQ_GOV_STOP, NULL);
902 device_unregister(&devfreq->dev);
903
904 return 0;
905}
906EXPORT_SYMBOL(devfreq_remove_device);
907
908static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
909{
910 struct devfreq **r = res;
911
912 if (WARN_ON(!r || !*r))
913 return 0;
914
915 return *r == data;
916}
917
918static void devm_devfreq_dev_release(struct device *dev, void *res)
919{
920 devfreq_remove_device(*(struct devfreq **)res);
921}
922
923/**
924 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
925 * @dev: the device to add devfreq feature.
926 * @profile: device-specific profile to run devfreq.
927 * @governor_name: name of the policy to choose frequency.
928 * @data: devfreq driver pass to governors, governor should not change it.
929 *
930 * This function manages automatically the memory of devfreq device using device
931 * resource management and simplify the free operation for memory of devfreq
932 * device.
933 */
934struct devfreq *devm_devfreq_add_device(struct device *dev,
935 struct devfreq_dev_profile *profile,
936 const char *governor_name,
937 void *data)
938{
939 struct devfreq **ptr, *devfreq;
940
941 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
942 if (!ptr)
943 return ERR_PTR(-ENOMEM);
944
945 devfreq = devfreq_add_device(dev, profile, governor_name, data);
946 if (IS_ERR(devfreq)) {
947 devres_free(ptr);
948 return devfreq;
949 }
950
951 *ptr = devfreq;
952 devres_add(dev, ptr);
953
954 return devfreq;
955}
956EXPORT_SYMBOL(devm_devfreq_add_device);
957
958#ifdef CONFIG_OF
959/*
960 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
961 * @dev - instance to the given device
962 * @index - index into list of devfreq
963 *
964 * return the instance of devfreq device
965 */
966struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
967{
968 struct device_node *node;
969 struct devfreq *devfreq;
970
971 if (!dev)
972 return ERR_PTR(-EINVAL);
973
974 if (!dev->of_node)
975 return ERR_PTR(-EINVAL);
976
977 node = of_parse_phandle(dev->of_node, "devfreq", index);
978 if (!node)
979 return ERR_PTR(-ENODEV);
980
981 mutex_lock(&devfreq_list_lock);
982 list_for_each_entry(devfreq, &devfreq_list, node) {
983 if (devfreq->dev.parent
984 && devfreq->dev.parent->of_node == node) {
985 mutex_unlock(&devfreq_list_lock);
986 of_node_put(node);
987 return devfreq;
988 }
989 }
990 mutex_unlock(&devfreq_list_lock);
991 of_node_put(node);
992
993 return ERR_PTR(-EPROBE_DEFER);
994}
995#else
996struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
997{
998 return ERR_PTR(-ENODEV);
999}
1000#endif /* CONFIG_OF */
1001EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1002
1003/**
1004 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1005 * @dev: the device from which to remove devfreq feature.
1006 * @devfreq: the devfreq instance to be removed
1007 */
1008void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1009{
1010 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1011 devm_devfreq_dev_match, devfreq));
1012}
1013EXPORT_SYMBOL(devm_devfreq_remove_device);
1014
1015/**
1016 * devfreq_suspend_device() - Suspend devfreq of a device.
1017 * @devfreq: the devfreq instance to be suspended
1018 *
1019 * This function is intended to be called by the pm callbacks
1020 * (e.g., runtime_suspend, suspend) of the device driver that
1021 * holds the devfreq.
1022 */
1023int devfreq_suspend_device(struct devfreq *devfreq)
1024{
1025 int ret;
1026
1027 if (!devfreq)
1028 return -EINVAL;
1029
1030 if (atomic_inc_return(&devfreq->suspend_count) > 1)
1031 return 0;
1032
1033 if (devfreq->governor) {
1034 ret = devfreq->governor->event_handler(devfreq,
1035 DEVFREQ_GOV_SUSPEND, NULL);
1036 if (ret)
1037 return ret;
1038 }
1039
1040 if (devfreq->suspend_freq) {
1041 mutex_lock(&devfreq->lock);
1042 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1043 mutex_unlock(&devfreq->lock);
1044 if (ret)
1045 return ret;
1046 }
1047
1048 return 0;
1049}
1050EXPORT_SYMBOL(devfreq_suspend_device);
1051
1052/**
1053 * devfreq_resume_device() - Resume devfreq of a device.
1054 * @devfreq: the devfreq instance to be resumed
1055 *
1056 * This function is intended to be called by the pm callbacks
1057 * (e.g., runtime_resume, resume) of the device driver that
1058 * holds the devfreq.
1059 */
1060int devfreq_resume_device(struct devfreq *devfreq)
1061{
1062 int ret;
1063
1064 if (!devfreq)
1065 return -EINVAL;
1066
1067 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1068 return 0;
1069
1070 if (devfreq->resume_freq) {
1071 mutex_lock(&devfreq->lock);
1072 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1073 mutex_unlock(&devfreq->lock);
1074 if (ret)
1075 return ret;
1076 }
1077
1078 if (devfreq->governor) {
1079 ret = devfreq->governor->event_handler(devfreq,
1080 DEVFREQ_GOV_RESUME, NULL);
1081 if (ret)
1082 return ret;
1083 }
1084
1085 return 0;
1086}
1087EXPORT_SYMBOL(devfreq_resume_device);
1088
1089/**
1090 * devfreq_suspend() - Suspend devfreq governors and devices
1091 *
1092 * Called during system wide Suspend/Hibernate cycles for suspending governors
1093 * and devices preserving the state for resume. On some platforms the devfreq
1094 * device must have precise state (frequency) after resume in order to provide
1095 * fully operating setup.
1096 */
1097void devfreq_suspend(void)
1098{
1099 struct devfreq *devfreq;
1100 int ret;
1101
1102 mutex_lock(&devfreq_list_lock);
1103 list_for_each_entry(devfreq, &devfreq_list, node) {
1104 ret = devfreq_suspend_device(devfreq);
1105 if (ret)
1106 dev_err(&devfreq->dev,
1107 "failed to suspend devfreq device\n");
1108 }
1109 mutex_unlock(&devfreq_list_lock);
1110}
1111
1112/**
1113 * devfreq_resume() - Resume devfreq governors and devices
1114 *
1115 * Called during system wide Suspend/Hibernate cycle for resuming governors and
1116 * devices that are suspended with devfreq_suspend().
1117 */
1118void devfreq_resume(void)
1119{
1120 struct devfreq *devfreq;
1121 int ret;
1122
1123 mutex_lock(&devfreq_list_lock);
1124 list_for_each_entry(devfreq, &devfreq_list, node) {
1125 ret = devfreq_resume_device(devfreq);
1126 if (ret)
1127 dev_warn(&devfreq->dev,
1128 "failed to resume devfreq device\n");
1129 }
1130 mutex_unlock(&devfreq_list_lock);
1131}
1132
1133/**
1134 * devfreq_add_governor() - Add devfreq governor
1135 * @governor: the devfreq governor to be added
1136 */
1137int devfreq_add_governor(struct devfreq_governor *governor)
1138{
1139 struct devfreq_governor *g;
1140 struct devfreq *devfreq;
1141 int err = 0;
1142
1143 if (!governor) {
1144 pr_err("%s: Invalid parameters.\n", __func__);
1145 return -EINVAL;
1146 }
1147
1148 mutex_lock(&devfreq_list_lock);
1149 g = find_devfreq_governor(governor->name);
1150 if (!IS_ERR(g)) {
1151 pr_err("%s: governor %s already registered\n", __func__,
1152 g->name);
1153 err = -EINVAL;
1154 goto err_out;
1155 }
1156
1157 list_add(&governor->node, &devfreq_governor_list);
1158
1159 list_for_each_entry(devfreq, &devfreq_list, node) {
1160 int ret = 0;
1161 struct device *dev = devfreq->dev.parent;
1162
1163 if (!strncmp(devfreq->governor_name, governor->name,
1164 DEVFREQ_NAME_LEN)) {
1165 /* The following should never occur */
1166 if (devfreq->governor) {
1167 dev_warn(dev,
1168 "%s: Governor %s already present\n",
1169 __func__, devfreq->governor->name);
1170 ret = devfreq->governor->event_handler(devfreq,
1171 DEVFREQ_GOV_STOP, NULL);
1172 if (ret) {
1173 dev_warn(dev,
1174 "%s: Governor %s stop = %d\n",
1175 __func__,
1176 devfreq->governor->name, ret);
1177 }
1178 /* Fall through */
1179 }
1180 devfreq->governor = governor;
1181 ret = devfreq->governor->event_handler(devfreq,
1182 DEVFREQ_GOV_START, NULL);
1183 if (ret) {
1184 dev_warn(dev, "%s: Governor %s start=%d\n",
1185 __func__, devfreq->governor->name,
1186 ret);
1187 }
1188 }
1189 }
1190
1191err_out:
1192 mutex_unlock(&devfreq_list_lock);
1193
1194 return err;
1195}
1196EXPORT_SYMBOL(devfreq_add_governor);
1197
1198/**
1199 * devfreq_remove_governor() - Remove devfreq feature from a device.
1200 * @governor: the devfreq governor to be removed
1201 */
1202int devfreq_remove_governor(struct devfreq_governor *governor)
1203{
1204 struct devfreq_governor *g;
1205 struct devfreq *devfreq;
1206 int err = 0;
1207
1208 if (!governor) {
1209 pr_err("%s: Invalid parameters.\n", __func__);
1210 return -EINVAL;
1211 }
1212
1213 mutex_lock(&devfreq_list_lock);
1214 g = find_devfreq_governor(governor->name);
1215 if (IS_ERR(g)) {
1216 pr_err("%s: governor %s not registered\n", __func__,
1217 governor->name);
1218 err = PTR_ERR(g);
1219 goto err_out;
1220 }
1221 list_for_each_entry(devfreq, &devfreq_list, node) {
1222 int ret;
1223 struct device *dev = devfreq->dev.parent;
1224
1225 if (!strncmp(devfreq->governor_name, governor->name,
1226 DEVFREQ_NAME_LEN)) {
1227 /* we should have a devfreq governor! */
1228 if (!devfreq->governor) {
1229 dev_warn(dev, "%s: Governor %s NOT present\n",
1230 __func__, governor->name);
1231 continue;
1232 /* Fall through */
1233 }
1234 ret = devfreq->governor->event_handler(devfreq,
1235 DEVFREQ_GOV_STOP, NULL);
1236 if (ret) {
1237 dev_warn(dev, "%s: Governor %s stop=%d\n",
1238 __func__, devfreq->governor->name,
1239 ret);
1240 }
1241 devfreq->governor = NULL;
1242 }
1243 }
1244
1245 list_del(&governor->node);
1246err_out:
1247 mutex_unlock(&devfreq_list_lock);
1248
1249 return err;
1250}
1251EXPORT_SYMBOL(devfreq_remove_governor);
1252
1253static ssize_t name_show(struct device *dev,
1254 struct device_attribute *attr, char *buf)
1255{
1256 struct devfreq *devfreq = to_devfreq(dev);
1257 return sprintf(buf, "%s\n", dev_name(devfreq->dev.parent));
1258}
1259static DEVICE_ATTR_RO(name);
1260
1261static ssize_t governor_show(struct device *dev,
1262 struct device_attribute *attr, char *buf)
1263{
1264 if (!to_devfreq(dev)->governor)
1265 return -EINVAL;
1266
1267 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1268}
1269
1270static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1271 const char *buf, size_t count)
1272{
1273 struct devfreq *df = to_devfreq(dev);
1274 int ret;
1275 char str_governor[DEVFREQ_NAME_LEN + 1];
1276 const struct devfreq_governor *governor, *prev_governor;
1277
1278 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1279 if (ret != 1)
1280 return -EINVAL;
1281
1282 mutex_lock(&devfreq_list_lock);
1283 governor = try_then_request_governor(str_governor);
1284 if (IS_ERR(governor)) {
1285 ret = PTR_ERR(governor);
1286 goto out;
1287 }
1288 if (df->governor == governor) {
1289 ret = 0;
1290 goto out;
1291 } else if ((df->governor && df->governor->immutable) ||
1292 governor->immutable) {
1293 ret = -EINVAL;
1294 goto out;
1295 }
1296
1297 if (df->governor) {
1298 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1299 if (ret) {
1300 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1301 __func__, df->governor->name, ret);
1302 goto out;
1303 }
1304 }
1305 prev_governor = df->governor;
1306 df->governor = governor;
1307 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1308 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1309 if (ret) {
1310 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1311 __func__, df->governor->name, ret);
1312 df->governor = prev_governor;
1313 strncpy(df->governor_name, prev_governor->name,
1314 DEVFREQ_NAME_LEN);
1315 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1316 if (ret) {
1317 dev_err(dev,
1318 "%s: reverting to Governor %s failed (%d)\n",
1319 __func__, df->governor_name, ret);
1320 df->governor = NULL;
1321 }
1322 }
1323out:
1324 mutex_unlock(&devfreq_list_lock);
1325
1326 if (!ret)
1327 ret = count;
1328 return ret;
1329}
1330static DEVICE_ATTR_RW(governor);
1331
1332static ssize_t available_governors_show(struct device *d,
1333 struct device_attribute *attr,
1334 char *buf)
1335{
1336 struct devfreq *df = to_devfreq(d);
1337 ssize_t count = 0;
1338
1339 mutex_lock(&devfreq_list_lock);
1340
1341 /*
1342 * The devfreq with immutable governor (e.g., passive) shows
1343 * only own governor.
1344 */
1345 if (df->governor && df->governor->immutable) {
1346 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1347 "%s ", df->governor_name);
1348 /*
1349 * The devfreq device shows the registered governor except for
1350 * immutable governors such as passive governor .
1351 */
1352 } else {
1353 struct devfreq_governor *governor;
1354
1355 list_for_each_entry(governor, &devfreq_governor_list, node) {
1356 if (governor->immutable)
1357 continue;
1358 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1359 "%s ", governor->name);
1360 }
1361 }
1362
1363 mutex_unlock(&devfreq_list_lock);
1364
1365 /* Truncate the trailing space */
1366 if (count)
1367 count--;
1368
1369 count += sprintf(&buf[count], "\n");
1370
1371 return count;
1372}
1373static DEVICE_ATTR_RO(available_governors);
1374
1375static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1376 char *buf)
1377{
1378 unsigned long freq;
1379 struct devfreq *devfreq = to_devfreq(dev);
1380
1381 if (devfreq->profile->get_cur_freq &&
1382 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1383 return sprintf(buf, "%lu\n", freq);
1384
1385 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1386}
1387static DEVICE_ATTR_RO(cur_freq);
1388
1389static ssize_t target_freq_show(struct device *dev,
1390 struct device_attribute *attr, char *buf)
1391{
1392 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1393}
1394static DEVICE_ATTR_RO(target_freq);
1395
1396static ssize_t polling_interval_show(struct device *dev,
1397 struct device_attribute *attr, char *buf)
1398{
1399 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1400}
1401
1402static ssize_t polling_interval_store(struct device *dev,
1403 struct device_attribute *attr,
1404 const char *buf, size_t count)
1405{
1406 struct devfreq *df = to_devfreq(dev);
1407 unsigned int value;
1408 int ret;
1409
1410 if (!df->governor)
1411 return -EINVAL;
1412
1413 ret = sscanf(buf, "%u", &value);
1414 if (ret != 1)
1415 return -EINVAL;
1416
1417 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1418 ret = count;
1419
1420 return ret;
1421}
1422static DEVICE_ATTR_RW(polling_interval);
1423
1424static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1425 const char *buf, size_t count)
1426{
1427 struct devfreq *df = to_devfreq(dev);
1428 unsigned long value;
1429 int ret;
1430
1431 ret = sscanf(buf, "%lu", &value);
1432 if (ret != 1)
1433 return -EINVAL;
1434
1435 mutex_lock(&df->lock);
1436
1437 if (value) {
1438 if (value > df->max_freq) {
1439 ret = -EINVAL;
1440 goto unlock;
1441 }
1442 } else {
1443 unsigned long *freq_table = df->profile->freq_table;
1444
1445 /* Get minimum frequency according to sorting order */
1446 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1447 value = freq_table[0];
1448 else
1449 value = freq_table[df->profile->max_state - 1];
1450 }
1451
1452 df->min_freq = value;
1453 update_devfreq(df);
1454 ret = count;
1455unlock:
1456 mutex_unlock(&df->lock);
1457 return ret;
1458}
1459
1460static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1461 char *buf)
1462{
1463 struct devfreq *df = to_devfreq(dev);
1464
1465 return sprintf(buf, "%lu\n", max(df->scaling_min_freq, df->min_freq));
1466}
1467
1468static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1469 const char *buf, size_t count)
1470{
1471 struct devfreq *df = to_devfreq(dev);
1472 unsigned long value;
1473 int ret;
1474
1475 ret = sscanf(buf, "%lu", &value);
1476 if (ret != 1)
1477 return -EINVAL;
1478
1479 mutex_lock(&df->lock);
1480
1481 if (value) {
1482 if (value < df->min_freq) {
1483 ret = -EINVAL;
1484 goto unlock;
1485 }
1486 } else {
1487 unsigned long *freq_table = df->profile->freq_table;
1488
1489 /* Get maximum frequency according to sorting order */
1490 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1491 value = freq_table[df->profile->max_state - 1];
1492 else
1493 value = freq_table[0];
1494 }
1495
1496 df->max_freq = value;
1497 update_devfreq(df);
1498 ret = count;
1499unlock:
1500 mutex_unlock(&df->lock);
1501 return ret;
1502}
1503static DEVICE_ATTR_RW(min_freq);
1504
1505static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1506 char *buf)
1507{
1508 struct devfreq *df = to_devfreq(dev);
1509
1510 return sprintf(buf, "%lu\n", min(df->scaling_max_freq, df->max_freq));
1511}
1512static DEVICE_ATTR_RW(max_freq);
1513
1514static ssize_t available_frequencies_show(struct device *d,
1515 struct device_attribute *attr,
1516 char *buf)
1517{
1518 struct devfreq *df = to_devfreq(d);
1519 ssize_t count = 0;
1520 int i;
1521
1522 mutex_lock(&df->lock);
1523
1524 for (i = 0; i < df->profile->max_state; i++)
1525 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1526 "%lu ", df->profile->freq_table[i]);
1527
1528 mutex_unlock(&df->lock);
1529 /* Truncate the trailing space */
1530 if (count)
1531 count--;
1532
1533 count += sprintf(&buf[count], "\n");
1534
1535 return count;
1536}
1537static DEVICE_ATTR_RO(available_frequencies);
1538
1539static ssize_t trans_stat_show(struct device *dev,
1540 struct device_attribute *attr, char *buf)
1541{
1542 struct devfreq *devfreq = to_devfreq(dev);
1543 ssize_t len;
1544 int i, j;
1545 unsigned int max_state = devfreq->profile->max_state;
1546
1547 if (max_state == 0)
1548 return sprintf(buf, "Not Supported.\n");
1549
1550 mutex_lock(&devfreq->lock);
1551 if (!devfreq->stop_polling &&
1552 devfreq_update_status(devfreq, devfreq->previous_freq)) {
1553 mutex_unlock(&devfreq->lock);
1554 return 0;
1555 }
1556 mutex_unlock(&devfreq->lock);
1557
1558 len = sprintf(buf, " From : To\n");
1559 len += sprintf(buf + len, " :");
1560 for (i = 0; i < max_state; i++)
1561 len += sprintf(buf + len, "%10lu",
1562 devfreq->profile->freq_table[i]);
1563
1564 len += sprintf(buf + len, " time(ms)\n");
1565
1566 for (i = 0; i < max_state; i++) {
1567 if (devfreq->profile->freq_table[i]
1568 == devfreq->previous_freq) {
1569 len += sprintf(buf + len, "*");
1570 } else {
1571 len += sprintf(buf + len, " ");
1572 }
1573 len += sprintf(buf + len, "%10lu:",
1574 devfreq->profile->freq_table[i]);
1575 for (j = 0; j < max_state; j++)
1576 len += sprintf(buf + len, "%10u",
1577 devfreq->trans_table[(i * max_state) + j]);
1578 len += sprintf(buf + len, "%10u\n",
1579 jiffies_to_msecs(devfreq->time_in_state[i]));
1580 }
1581
1582 len += sprintf(buf + len, "Total transition : %u\n",
1583 devfreq->total_trans);
1584 return len;
1585}
1586static DEVICE_ATTR_RO(trans_stat);
1587
1588static struct attribute *devfreq_attrs[] = {
1589 &dev_attr_name.attr,
1590 &dev_attr_governor.attr,
1591 &dev_attr_available_governors.attr,
1592 &dev_attr_cur_freq.attr,
1593 &dev_attr_available_frequencies.attr,
1594 &dev_attr_target_freq.attr,
1595 &dev_attr_polling_interval.attr,
1596 &dev_attr_min_freq.attr,
1597 &dev_attr_max_freq.attr,
1598 &dev_attr_trans_stat.attr,
1599 NULL,
1600};
1601ATTRIBUTE_GROUPS(devfreq);
1602
1603static int __init devfreq_init(void)
1604{
1605 devfreq_class = class_create(THIS_MODULE, "devfreq");
1606 if (IS_ERR(devfreq_class)) {
1607 pr_err("%s: couldn't create class\n", __FILE__);
1608 return PTR_ERR(devfreq_class);
1609 }
1610
1611 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1612 if (!devfreq_wq) {
1613 class_destroy(devfreq_class);
1614 pr_err("%s: couldn't create workqueue\n", __FILE__);
1615 return -ENOMEM;
1616 }
1617 devfreq_class->dev_groups = devfreq_groups;
1618
1619 return 0;
1620}
1621subsys_initcall(devfreq_init);
1622
1623/*
1624 * The following are helper functions for devfreq user device drivers with
1625 * OPP framework.
1626 */
1627
1628/**
1629 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1630 * freq value given to target callback.
1631 * @dev: The devfreq user device. (parent of devfreq)
1632 * @freq: The frequency given to target function
1633 * @flags: Flags handed from devfreq framework.
1634 *
1635 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1636 * use.
1637 */
1638struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1639 unsigned long *freq,
1640 u32 flags)
1641{
1642 struct dev_pm_opp *opp;
1643
1644 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1645 /* The freq is an upper bound. opp should be lower */
1646 opp = dev_pm_opp_find_freq_floor(dev, freq);
1647
1648 /* If not available, use the closest opp */
1649 if (opp == ERR_PTR(-ERANGE))
1650 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1651 } else {
1652 /* The freq is an lower bound. opp should be higher */
1653 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1654
1655 /* If not available, use the closest opp */
1656 if (opp == ERR_PTR(-ERANGE))
1657 opp = dev_pm_opp_find_freq_floor(dev, freq);
1658 }
1659
1660 return opp;
1661}
1662EXPORT_SYMBOL(devfreq_recommended_opp);
1663
1664/**
1665 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1666 * for any changes in the OPP availability
1667 * changes
1668 * @dev: The devfreq user device. (parent of devfreq)
1669 * @devfreq: The devfreq object.
1670 */
1671int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1672{
1673 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1674}
1675EXPORT_SYMBOL(devfreq_register_opp_notifier);
1676
1677/**
1678 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1679 * notified for any changes in the OPP
1680 * availability changes anymore.
1681 * @dev: The devfreq user device. (parent of devfreq)
1682 * @devfreq: The devfreq object.
1683 *
1684 * At exit() callback of devfreq_dev_profile, this must be included if
1685 * devfreq_recommended_opp is used.
1686 */
1687int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1688{
1689 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1690}
1691EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1692
1693static void devm_devfreq_opp_release(struct device *dev, void *res)
1694{
1695 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1696}
1697
1698/**
1699 * devm_devfreq_register_opp_notifier() - Resource-managed
1700 * devfreq_register_opp_notifier()
1701 * @dev: The devfreq user device. (parent of devfreq)
1702 * @devfreq: The devfreq object.
1703 */
1704int devm_devfreq_register_opp_notifier(struct device *dev,
1705 struct devfreq *devfreq)
1706{
1707 struct devfreq **ptr;
1708 int ret;
1709
1710 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1711 if (!ptr)
1712 return -ENOMEM;
1713
1714 ret = devfreq_register_opp_notifier(dev, devfreq);
1715 if (ret) {
1716 devres_free(ptr);
1717 return ret;
1718 }
1719
1720 *ptr = devfreq;
1721 devres_add(dev, ptr);
1722
1723 return 0;
1724}
1725EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1726
1727/**
1728 * devm_devfreq_unregister_opp_notifier() - Resource-managed
1729 * devfreq_unregister_opp_notifier()
1730 * @dev: The devfreq user device. (parent of devfreq)
1731 * @devfreq: The devfreq object.
1732 */
1733void devm_devfreq_unregister_opp_notifier(struct device *dev,
1734 struct devfreq *devfreq)
1735{
1736 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1737 devm_devfreq_dev_match, devfreq));
1738}
1739EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1740
1741/**
1742 * devfreq_register_notifier() - Register a driver with devfreq
1743 * @devfreq: The devfreq object.
1744 * @nb: The notifier block to register.
1745 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1746 */
1747int devfreq_register_notifier(struct devfreq *devfreq,
1748 struct notifier_block *nb,
1749 unsigned int list)
1750{
1751 int ret = 0;
1752
1753 if (!devfreq)
1754 return -EINVAL;
1755
1756 switch (list) {
1757 case DEVFREQ_TRANSITION_NOTIFIER:
1758 ret = srcu_notifier_chain_register(
1759 &devfreq->transition_notifier_list, nb);
1760 break;
1761 default:
1762 ret = -EINVAL;
1763 }
1764
1765 return ret;
1766}
1767EXPORT_SYMBOL(devfreq_register_notifier);
1768
1769/*
1770 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1771 * @devfreq: The devfreq object.
1772 * @nb: The notifier block to be unregistered.
1773 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1774 */
1775int devfreq_unregister_notifier(struct devfreq *devfreq,
1776 struct notifier_block *nb,
1777 unsigned int list)
1778{
1779 int ret = 0;
1780
1781 if (!devfreq)
1782 return -EINVAL;
1783
1784 switch (list) {
1785 case DEVFREQ_TRANSITION_NOTIFIER:
1786 ret = srcu_notifier_chain_unregister(
1787 &devfreq->transition_notifier_list, nb);
1788 break;
1789 default:
1790 ret = -EINVAL;
1791 }
1792
1793 return ret;
1794}
1795EXPORT_SYMBOL(devfreq_unregister_notifier);
1796
1797struct devfreq_notifier_devres {
1798 struct devfreq *devfreq;
1799 struct notifier_block *nb;
1800 unsigned int list;
1801};
1802
1803static void devm_devfreq_notifier_release(struct device *dev, void *res)
1804{
1805 struct devfreq_notifier_devres *this = res;
1806
1807 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1808}
1809
1810/**
1811 * devm_devfreq_register_notifier()
1812 - Resource-managed devfreq_register_notifier()
1813 * @dev: The devfreq user device. (parent of devfreq)
1814 * @devfreq: The devfreq object.
1815 * @nb: The notifier block to be unregistered.
1816 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1817 */
1818int devm_devfreq_register_notifier(struct device *dev,
1819 struct devfreq *devfreq,
1820 struct notifier_block *nb,
1821 unsigned int list)
1822{
1823 struct devfreq_notifier_devres *ptr;
1824 int ret;
1825
1826 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1827 GFP_KERNEL);
1828 if (!ptr)
1829 return -ENOMEM;
1830
1831 ret = devfreq_register_notifier(devfreq, nb, list);
1832 if (ret) {
1833 devres_free(ptr);
1834 return ret;
1835 }
1836
1837 ptr->devfreq = devfreq;
1838 ptr->nb = nb;
1839 ptr->list = list;
1840 devres_add(dev, ptr);
1841
1842 return 0;
1843}
1844EXPORT_SYMBOL(devm_devfreq_register_notifier);
1845
1846/**
1847 * devm_devfreq_unregister_notifier()
1848 - Resource-managed devfreq_unregister_notifier()
1849 * @dev: The devfreq user device. (parent of devfreq)
1850 * @devfreq: The devfreq object.
1851 * @nb: The notifier block to be unregistered.
1852 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1853 */
1854void devm_devfreq_unregister_notifier(struct device *dev,
1855 struct devfreq *devfreq,
1856 struct notifier_block *nb,
1857 unsigned int list)
1858{
1859 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1860 devm_devfreq_dev_match, devfreq));
1861}
1862EXPORT_SYMBOL(devm_devfreq_unregister_notifier);