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