xref: /openbmc/linux/drivers/devfreq/devfreq.c (revision 6a143a7c)
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 	if (devfreq->opp_table)
761 		dev_pm_opp_put_opp_table(devfreq->opp_table);
762 
763 	mutex_destroy(&devfreq->lock);
764 	kfree(devfreq);
765 }
766 
767 static void create_sysfs_files(struct devfreq *devfreq,
768 				const struct devfreq_governor *gov);
769 static void remove_sysfs_files(struct devfreq *devfreq,
770 				const struct devfreq_governor *gov);
771 
772 /**
773  * devfreq_add_device() - Add devfreq feature to the device
774  * @dev:	the device to add devfreq feature.
775  * @profile:	device-specific profile to run devfreq.
776  * @governor_name:	name of the policy to choose frequency.
777  * @data:	private data for the governor. The devfreq framework does not
778  *		touch this value.
779  */
780 struct devfreq *devfreq_add_device(struct device *dev,
781 				   struct devfreq_dev_profile *profile,
782 				   const char *governor_name,
783 				   void *data)
784 {
785 	struct devfreq *devfreq;
786 	struct devfreq_governor *governor;
787 	int err = 0;
788 
789 	if (!dev || !profile || !governor_name) {
790 		dev_err(dev, "%s: Invalid parameters.\n", __func__);
791 		return ERR_PTR(-EINVAL);
792 	}
793 
794 	mutex_lock(&devfreq_list_lock);
795 	devfreq = find_device_devfreq(dev);
796 	mutex_unlock(&devfreq_list_lock);
797 	if (!IS_ERR(devfreq)) {
798 		dev_err(dev, "%s: devfreq device already exists!\n",
799 			__func__);
800 		err = -EINVAL;
801 		goto err_out;
802 	}
803 
804 	devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
805 	if (!devfreq) {
806 		err = -ENOMEM;
807 		goto err_out;
808 	}
809 
810 	mutex_init(&devfreq->lock);
811 	mutex_lock(&devfreq->lock);
812 	devfreq->dev.parent = dev;
813 	devfreq->dev.class = devfreq_class;
814 	devfreq->dev.release = devfreq_dev_release;
815 	INIT_LIST_HEAD(&devfreq->node);
816 	devfreq->profile = profile;
817 	devfreq->previous_freq = profile->initial_freq;
818 	devfreq->last_status.current_frequency = profile->initial_freq;
819 	devfreq->data = data;
820 	devfreq->nb.notifier_call = devfreq_notifier_call;
821 
822 	if (devfreq->profile->timer < 0
823 		|| devfreq->profile->timer >= DEVFREQ_TIMER_NUM) {
824 		goto err_out;
825 	}
826 
827 	if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
828 		mutex_unlock(&devfreq->lock);
829 		err = set_freq_table(devfreq);
830 		if (err < 0)
831 			goto err_dev;
832 		mutex_lock(&devfreq->lock);
833 	}
834 
835 	devfreq->scaling_min_freq = find_available_min_freq(devfreq);
836 	if (!devfreq->scaling_min_freq) {
837 		mutex_unlock(&devfreq->lock);
838 		err = -EINVAL;
839 		goto err_dev;
840 	}
841 
842 	devfreq->scaling_max_freq = find_available_max_freq(devfreq);
843 	if (!devfreq->scaling_max_freq) {
844 		mutex_unlock(&devfreq->lock);
845 		err = -EINVAL;
846 		goto err_dev;
847 	}
848 
849 	devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
850 	devfreq->opp_table = dev_pm_opp_get_opp_table(dev);
851 	if (IS_ERR(devfreq->opp_table))
852 		devfreq->opp_table = NULL;
853 
854 	atomic_set(&devfreq->suspend_count, 0);
855 
856 	dev_set_name(&devfreq->dev, "%s", dev_name(dev));
857 	err = device_register(&devfreq->dev);
858 	if (err) {
859 		mutex_unlock(&devfreq->lock);
860 		put_device(&devfreq->dev);
861 		goto err_out;
862 	}
863 
864 	devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
865 			array3_size(sizeof(unsigned int),
866 				    devfreq->profile->max_state,
867 				    devfreq->profile->max_state),
868 			GFP_KERNEL);
869 	if (!devfreq->stats.trans_table) {
870 		mutex_unlock(&devfreq->lock);
871 		err = -ENOMEM;
872 		goto err_devfreq;
873 	}
874 
875 	devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
876 			devfreq->profile->max_state,
877 			sizeof(*devfreq->stats.time_in_state),
878 			GFP_KERNEL);
879 	if (!devfreq->stats.time_in_state) {
880 		mutex_unlock(&devfreq->lock);
881 		err = -ENOMEM;
882 		goto err_devfreq;
883 	}
884 
885 	devfreq->stats.total_trans = 0;
886 	devfreq->stats.last_update = get_jiffies_64();
887 
888 	srcu_init_notifier_head(&devfreq->transition_notifier_list);
889 
890 	mutex_unlock(&devfreq->lock);
891 
892 	err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
893 				     DEV_PM_QOS_MIN_FREQUENCY, 0);
894 	if (err < 0)
895 		goto err_devfreq;
896 	err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
897 				     DEV_PM_QOS_MAX_FREQUENCY,
898 				     PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
899 	if (err < 0)
900 		goto err_devfreq;
901 
902 	devfreq->nb_min.notifier_call = qos_min_notifier_call;
903 	err = dev_pm_qos_add_notifier(dev, &devfreq->nb_min,
904 				      DEV_PM_QOS_MIN_FREQUENCY);
905 	if (err)
906 		goto err_devfreq;
907 
908 	devfreq->nb_max.notifier_call = qos_max_notifier_call;
909 	err = dev_pm_qos_add_notifier(dev, &devfreq->nb_max,
910 				      DEV_PM_QOS_MAX_FREQUENCY);
911 	if (err)
912 		goto err_devfreq;
913 
914 	mutex_lock(&devfreq_list_lock);
915 
916 	governor = try_then_request_governor(governor_name);
917 	if (IS_ERR(governor)) {
918 		dev_err(dev, "%s: Unable to find governor for the device\n",
919 			__func__);
920 		err = PTR_ERR(governor);
921 		goto err_init;
922 	}
923 
924 	devfreq->governor = governor;
925 	err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
926 						NULL);
927 	if (err) {
928 		dev_err(dev, "%s: Unable to start governor for the device\n",
929 			__func__);
930 		goto err_init;
931 	}
932 	create_sysfs_files(devfreq, devfreq->governor);
933 
934 	list_add(&devfreq->node, &devfreq_list);
935 
936 	mutex_unlock(&devfreq_list_lock);
937 
938 	return devfreq;
939 
940 err_init:
941 	mutex_unlock(&devfreq_list_lock);
942 err_devfreq:
943 	devfreq_remove_device(devfreq);
944 	devfreq = NULL;
945 err_dev:
946 	kfree(devfreq);
947 err_out:
948 	return ERR_PTR(err);
949 }
950 EXPORT_SYMBOL(devfreq_add_device);
951 
952 /**
953  * devfreq_remove_device() - Remove devfreq feature from a device.
954  * @devfreq:	the devfreq instance to be removed
955  *
956  * The opposite of devfreq_add_device().
957  */
958 int devfreq_remove_device(struct devfreq *devfreq)
959 {
960 	if (!devfreq)
961 		return -EINVAL;
962 
963 	if (devfreq->governor) {
964 		devfreq->governor->event_handler(devfreq,
965 						 DEVFREQ_GOV_STOP, NULL);
966 		remove_sysfs_files(devfreq, devfreq->governor);
967 	}
968 
969 	device_unregister(&devfreq->dev);
970 
971 	return 0;
972 }
973 EXPORT_SYMBOL(devfreq_remove_device);
974 
975 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
976 {
977 	struct devfreq **r = res;
978 
979 	if (WARN_ON(!r || !*r))
980 		return 0;
981 
982 	return *r == data;
983 }
984 
985 static void devm_devfreq_dev_release(struct device *dev, void *res)
986 {
987 	devfreq_remove_device(*(struct devfreq **)res);
988 }
989 
990 /**
991  * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
992  * @dev:	the device to add devfreq feature.
993  * @profile:	device-specific profile to run devfreq.
994  * @governor_name:	name of the policy to choose frequency.
995  * @data:	private data for the governor. The devfreq framework does not
996  *		touch this value.
997  *
998  * This function manages automatically the memory of devfreq device using device
999  * resource management and simplify the free operation for memory of devfreq
1000  * device.
1001  */
1002 struct devfreq *devm_devfreq_add_device(struct device *dev,
1003 					struct devfreq_dev_profile *profile,
1004 					const char *governor_name,
1005 					void *data)
1006 {
1007 	struct devfreq **ptr, *devfreq;
1008 
1009 	ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
1010 	if (!ptr)
1011 		return ERR_PTR(-ENOMEM);
1012 
1013 	devfreq = devfreq_add_device(dev, profile, governor_name, data);
1014 	if (IS_ERR(devfreq)) {
1015 		devres_free(ptr);
1016 		return devfreq;
1017 	}
1018 
1019 	*ptr = devfreq;
1020 	devres_add(dev, ptr);
1021 
1022 	return devfreq;
1023 }
1024 EXPORT_SYMBOL(devm_devfreq_add_device);
1025 
1026 #ifdef CONFIG_OF
1027 /*
1028  * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
1029  * @node - pointer to device_node
1030  *
1031  * return the instance of devfreq device
1032  */
1033 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1034 {
1035 	struct devfreq *devfreq;
1036 
1037 	if (!node)
1038 		return ERR_PTR(-EINVAL);
1039 
1040 	mutex_lock(&devfreq_list_lock);
1041 	list_for_each_entry(devfreq, &devfreq_list, node) {
1042 		if (devfreq->dev.parent
1043 			&& devfreq->dev.parent->of_node == node) {
1044 			mutex_unlock(&devfreq_list_lock);
1045 			return devfreq;
1046 		}
1047 	}
1048 	mutex_unlock(&devfreq_list_lock);
1049 
1050 	return ERR_PTR(-ENODEV);
1051 }
1052 
1053 /*
1054  * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
1055  * @dev - instance to the given device
1056  * @phandle_name - name of property holding a phandle value
1057  * @index - index into list of devfreq
1058  *
1059  * return the instance of devfreq device
1060  */
1061 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1062 					const char *phandle_name, int index)
1063 {
1064 	struct device_node *node;
1065 	struct devfreq *devfreq;
1066 
1067 	if (!dev || !phandle_name)
1068 		return ERR_PTR(-EINVAL);
1069 
1070 	if (!dev->of_node)
1071 		return ERR_PTR(-EINVAL);
1072 
1073 	node = of_parse_phandle(dev->of_node, phandle_name, index);
1074 	if (!node)
1075 		return ERR_PTR(-ENODEV);
1076 
1077 	devfreq = devfreq_get_devfreq_by_node(node);
1078 	of_node_put(node);
1079 
1080 	return devfreq;
1081 }
1082 
1083 #else
1084 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1085 {
1086 	return ERR_PTR(-ENODEV);
1087 }
1088 
1089 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1090 					const char *phandle_name, int index)
1091 {
1092 	return ERR_PTR(-ENODEV);
1093 }
1094 #endif /* CONFIG_OF */
1095 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
1096 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1097 
1098 /**
1099  * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1100  * @dev:	the device from which to remove devfreq feature.
1101  * @devfreq:	the devfreq instance to be removed
1102  */
1103 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1104 {
1105 	WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1106 			       devm_devfreq_dev_match, devfreq));
1107 }
1108 EXPORT_SYMBOL(devm_devfreq_remove_device);
1109 
1110 /**
1111  * devfreq_suspend_device() - Suspend devfreq of a device.
1112  * @devfreq: the devfreq instance to be suspended
1113  *
1114  * This function is intended to be called by the pm callbacks
1115  * (e.g., runtime_suspend, suspend) of the device driver that
1116  * holds the devfreq.
1117  */
1118 int devfreq_suspend_device(struct devfreq *devfreq)
1119 {
1120 	int ret;
1121 
1122 	if (!devfreq)
1123 		return -EINVAL;
1124 
1125 	if (atomic_inc_return(&devfreq->suspend_count) > 1)
1126 		return 0;
1127 
1128 	if (devfreq->governor) {
1129 		ret = devfreq->governor->event_handler(devfreq,
1130 					DEVFREQ_GOV_SUSPEND, NULL);
1131 		if (ret)
1132 			return ret;
1133 	}
1134 
1135 	if (devfreq->suspend_freq) {
1136 		mutex_lock(&devfreq->lock);
1137 		ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1138 		mutex_unlock(&devfreq->lock);
1139 		if (ret)
1140 			return ret;
1141 	}
1142 
1143 	return 0;
1144 }
1145 EXPORT_SYMBOL(devfreq_suspend_device);
1146 
1147 /**
1148  * devfreq_resume_device() - Resume devfreq of a device.
1149  * @devfreq: the devfreq instance to be resumed
1150  *
1151  * This function is intended to be called by the pm callbacks
1152  * (e.g., runtime_resume, resume) of the device driver that
1153  * holds the devfreq.
1154  */
1155 int devfreq_resume_device(struct devfreq *devfreq)
1156 {
1157 	int ret;
1158 
1159 	if (!devfreq)
1160 		return -EINVAL;
1161 
1162 	if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1163 		return 0;
1164 
1165 	if (devfreq->resume_freq) {
1166 		mutex_lock(&devfreq->lock);
1167 		ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1168 		mutex_unlock(&devfreq->lock);
1169 		if (ret)
1170 			return ret;
1171 	}
1172 
1173 	if (devfreq->governor) {
1174 		ret = devfreq->governor->event_handler(devfreq,
1175 					DEVFREQ_GOV_RESUME, NULL);
1176 		if (ret)
1177 			return ret;
1178 	}
1179 
1180 	return 0;
1181 }
1182 EXPORT_SYMBOL(devfreq_resume_device);
1183 
1184 /**
1185  * devfreq_suspend() - Suspend devfreq governors and devices
1186  *
1187  * Called during system wide Suspend/Hibernate cycles for suspending governors
1188  * and devices preserving the state for resume. On some platforms the devfreq
1189  * device must have precise state (frequency) after resume in order to provide
1190  * fully operating setup.
1191  */
1192 void devfreq_suspend(void)
1193 {
1194 	struct devfreq *devfreq;
1195 	int ret;
1196 
1197 	mutex_lock(&devfreq_list_lock);
1198 	list_for_each_entry(devfreq, &devfreq_list, node) {
1199 		ret = devfreq_suspend_device(devfreq);
1200 		if (ret)
1201 			dev_err(&devfreq->dev,
1202 				"failed to suspend devfreq device\n");
1203 	}
1204 	mutex_unlock(&devfreq_list_lock);
1205 }
1206 
1207 /**
1208  * devfreq_resume() - Resume devfreq governors and devices
1209  *
1210  * Called during system wide Suspend/Hibernate cycle for resuming governors and
1211  * devices that are suspended with devfreq_suspend().
1212  */
1213 void devfreq_resume(void)
1214 {
1215 	struct devfreq *devfreq;
1216 	int ret;
1217 
1218 	mutex_lock(&devfreq_list_lock);
1219 	list_for_each_entry(devfreq, &devfreq_list, node) {
1220 		ret = devfreq_resume_device(devfreq);
1221 		if (ret)
1222 			dev_warn(&devfreq->dev,
1223 				 "failed to resume devfreq device\n");
1224 	}
1225 	mutex_unlock(&devfreq_list_lock);
1226 }
1227 
1228 /**
1229  * devfreq_add_governor() - Add devfreq governor
1230  * @governor:	the devfreq governor to be added
1231  */
1232 int devfreq_add_governor(struct devfreq_governor *governor)
1233 {
1234 	struct devfreq_governor *g;
1235 	struct devfreq *devfreq;
1236 	int err = 0;
1237 
1238 	if (!governor) {
1239 		pr_err("%s: Invalid parameters.\n", __func__);
1240 		return -EINVAL;
1241 	}
1242 
1243 	mutex_lock(&devfreq_list_lock);
1244 	g = find_devfreq_governor(governor->name);
1245 	if (!IS_ERR(g)) {
1246 		pr_err("%s: governor %s already registered\n", __func__,
1247 		       g->name);
1248 		err = -EINVAL;
1249 		goto err_out;
1250 	}
1251 
1252 	list_add(&governor->node, &devfreq_governor_list);
1253 
1254 	list_for_each_entry(devfreq, &devfreq_list, node) {
1255 		int ret = 0;
1256 		struct device *dev = devfreq->dev.parent;
1257 
1258 		if (!strncmp(devfreq->governor->name, governor->name,
1259 			     DEVFREQ_NAME_LEN)) {
1260 			/* The following should never occur */
1261 			if (devfreq->governor) {
1262 				dev_warn(dev,
1263 					 "%s: Governor %s already present\n",
1264 					 __func__, devfreq->governor->name);
1265 				ret = devfreq->governor->event_handler(devfreq,
1266 							DEVFREQ_GOV_STOP, NULL);
1267 				if (ret) {
1268 					dev_warn(dev,
1269 						 "%s: Governor %s stop = %d\n",
1270 						 __func__,
1271 						 devfreq->governor->name, ret);
1272 				}
1273 				/* Fall through */
1274 			}
1275 			devfreq->governor = governor;
1276 			ret = devfreq->governor->event_handler(devfreq,
1277 						DEVFREQ_GOV_START, NULL);
1278 			if (ret) {
1279 				dev_warn(dev, "%s: Governor %s start=%d\n",
1280 					 __func__, devfreq->governor->name,
1281 					 ret);
1282 			}
1283 		}
1284 	}
1285 
1286 err_out:
1287 	mutex_unlock(&devfreq_list_lock);
1288 
1289 	return err;
1290 }
1291 EXPORT_SYMBOL(devfreq_add_governor);
1292 
1293 /**
1294  * devfreq_remove_governor() - Remove devfreq feature from a device.
1295  * @governor:	the devfreq governor to be removed
1296  */
1297 int devfreq_remove_governor(struct devfreq_governor *governor)
1298 {
1299 	struct devfreq_governor *g;
1300 	struct devfreq *devfreq;
1301 	int err = 0;
1302 
1303 	if (!governor) {
1304 		pr_err("%s: Invalid parameters.\n", __func__);
1305 		return -EINVAL;
1306 	}
1307 
1308 	mutex_lock(&devfreq_list_lock);
1309 	g = find_devfreq_governor(governor->name);
1310 	if (IS_ERR(g)) {
1311 		pr_err("%s: governor %s not registered\n", __func__,
1312 		       governor->name);
1313 		err = PTR_ERR(g);
1314 		goto err_out;
1315 	}
1316 	list_for_each_entry(devfreq, &devfreq_list, node) {
1317 		int ret;
1318 		struct device *dev = devfreq->dev.parent;
1319 
1320 		if (!strncmp(devfreq->governor->name, governor->name,
1321 			     DEVFREQ_NAME_LEN)) {
1322 			/* we should have a devfreq governor! */
1323 			if (!devfreq->governor) {
1324 				dev_warn(dev, "%s: Governor %s NOT present\n",
1325 					 __func__, governor->name);
1326 				continue;
1327 				/* Fall through */
1328 			}
1329 			ret = devfreq->governor->event_handler(devfreq,
1330 						DEVFREQ_GOV_STOP, NULL);
1331 			if (ret) {
1332 				dev_warn(dev, "%s: Governor %s stop=%d\n",
1333 					 __func__, devfreq->governor->name,
1334 					 ret);
1335 			}
1336 			devfreq->governor = NULL;
1337 		}
1338 	}
1339 
1340 	list_del(&governor->node);
1341 err_out:
1342 	mutex_unlock(&devfreq_list_lock);
1343 
1344 	return err;
1345 }
1346 EXPORT_SYMBOL(devfreq_remove_governor);
1347 
1348 static ssize_t name_show(struct device *dev,
1349 			struct device_attribute *attr, char *buf)
1350 {
1351 	struct devfreq *df = to_devfreq(dev);
1352 	return sprintf(buf, "%s\n", dev_name(df->dev.parent));
1353 }
1354 static DEVICE_ATTR_RO(name);
1355 
1356 static ssize_t governor_show(struct device *dev,
1357 			     struct device_attribute *attr, char *buf)
1358 {
1359 	struct devfreq *df = to_devfreq(dev);
1360 
1361 	if (!df->governor)
1362 		return -EINVAL;
1363 
1364 	return sprintf(buf, "%s\n", df->governor->name);
1365 }
1366 
1367 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1368 			      const char *buf, size_t count)
1369 {
1370 	struct devfreq *df = to_devfreq(dev);
1371 	int ret;
1372 	char str_governor[DEVFREQ_NAME_LEN + 1];
1373 	const struct devfreq_governor *governor, *prev_governor;
1374 
1375 	if (!df->governor)
1376 		return -EINVAL;
1377 
1378 	ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1379 	if (ret != 1)
1380 		return -EINVAL;
1381 
1382 	mutex_lock(&devfreq_list_lock);
1383 	governor = try_then_request_governor(str_governor);
1384 	if (IS_ERR(governor)) {
1385 		ret = PTR_ERR(governor);
1386 		goto out;
1387 	}
1388 	if (df->governor == governor) {
1389 		ret = 0;
1390 		goto out;
1391 	} else if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)
1392 		|| IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) {
1393 		ret = -EINVAL;
1394 		goto out;
1395 	}
1396 
1397 	/*
1398 	 * Stop the current governor and remove the specific sysfs files
1399 	 * which depend on current governor.
1400 	 */
1401 	ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1402 	if (ret) {
1403 		dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1404 			 __func__, df->governor->name, ret);
1405 		goto out;
1406 	}
1407 	remove_sysfs_files(df, df->governor);
1408 
1409 	/*
1410 	 * Start the new governor and create the specific sysfs files
1411 	 * which depend on the new governor.
1412 	 */
1413 	prev_governor = df->governor;
1414 	df->governor = governor;
1415 	ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1416 	if (ret) {
1417 		dev_warn(dev, "%s: Governor %s not started(%d)\n",
1418 			 __func__, df->governor->name, ret);
1419 
1420 		/* Restore previous governor */
1421 		df->governor = prev_governor;
1422 		ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1423 		if (ret) {
1424 			dev_err(dev,
1425 				"%s: reverting to Governor %s failed (%d)\n",
1426 				__func__, prev_governor->name, ret);
1427 			df->governor = NULL;
1428 			goto out;
1429 		}
1430 	}
1431 
1432 	/*
1433 	 * Create the sysfs files for the new governor. But if failed to start
1434 	 * the new governor, restore the sysfs files of previous governor.
1435 	 */
1436 	create_sysfs_files(df, df->governor);
1437 
1438 out:
1439 	mutex_unlock(&devfreq_list_lock);
1440 
1441 	if (!ret)
1442 		ret = count;
1443 	return ret;
1444 }
1445 static DEVICE_ATTR_RW(governor);
1446 
1447 static ssize_t available_governors_show(struct device *d,
1448 					struct device_attribute *attr,
1449 					char *buf)
1450 {
1451 	struct devfreq *df = to_devfreq(d);
1452 	ssize_t count = 0;
1453 
1454 	if (!df->governor)
1455 		return -EINVAL;
1456 
1457 	mutex_lock(&devfreq_list_lock);
1458 
1459 	/*
1460 	 * The devfreq with immutable governor (e.g., passive) shows
1461 	 * only own governor.
1462 	 */
1463 	if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)) {
1464 		count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1465 				  "%s ", df->governor->name);
1466 	/*
1467 	 * The devfreq device shows the registered governor except for
1468 	 * immutable governors such as passive governor .
1469 	 */
1470 	} else {
1471 		struct devfreq_governor *governor;
1472 
1473 		list_for_each_entry(governor, &devfreq_governor_list, node) {
1474 			if (IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE))
1475 				continue;
1476 			count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1477 					   "%s ", governor->name);
1478 		}
1479 	}
1480 
1481 	mutex_unlock(&devfreq_list_lock);
1482 
1483 	/* Truncate the trailing space */
1484 	if (count)
1485 		count--;
1486 
1487 	count += sprintf(&buf[count], "\n");
1488 
1489 	return count;
1490 }
1491 static DEVICE_ATTR_RO(available_governors);
1492 
1493 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1494 			     char *buf)
1495 {
1496 	unsigned long freq;
1497 	struct devfreq *df = to_devfreq(dev);
1498 
1499 	if (!df->profile)
1500 		return -EINVAL;
1501 
1502 	if (df->profile->get_cur_freq &&
1503 		!df->profile->get_cur_freq(df->dev.parent, &freq))
1504 		return sprintf(buf, "%lu\n", freq);
1505 
1506 	return sprintf(buf, "%lu\n", df->previous_freq);
1507 }
1508 static DEVICE_ATTR_RO(cur_freq);
1509 
1510 static ssize_t target_freq_show(struct device *dev,
1511 				struct device_attribute *attr, char *buf)
1512 {
1513 	struct devfreq *df = to_devfreq(dev);
1514 
1515 	return sprintf(buf, "%lu\n", df->previous_freq);
1516 }
1517 static DEVICE_ATTR_RO(target_freq);
1518 
1519 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1520 			      const char *buf, size_t count)
1521 {
1522 	struct devfreq *df = to_devfreq(dev);
1523 	unsigned long value;
1524 	int ret;
1525 
1526 	/*
1527 	 * Protect against theoretical sysfs writes between
1528 	 * device_add and dev_pm_qos_add_request
1529 	 */
1530 	if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1531 		return -EAGAIN;
1532 
1533 	ret = sscanf(buf, "%lu", &value);
1534 	if (ret != 1)
1535 		return -EINVAL;
1536 
1537 	/* Round down to kHz for PM QoS */
1538 	ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1539 					value / HZ_PER_KHZ);
1540 	if (ret < 0)
1541 		return ret;
1542 
1543 	return count;
1544 }
1545 
1546 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1547 			     char *buf)
1548 {
1549 	struct devfreq *df = to_devfreq(dev);
1550 	unsigned long min_freq, max_freq;
1551 
1552 	mutex_lock(&df->lock);
1553 	get_freq_range(df, &min_freq, &max_freq);
1554 	mutex_unlock(&df->lock);
1555 
1556 	return sprintf(buf, "%lu\n", min_freq);
1557 }
1558 static DEVICE_ATTR_RW(min_freq);
1559 
1560 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1561 			      const char *buf, size_t count)
1562 {
1563 	struct devfreq *df = to_devfreq(dev);
1564 	unsigned long value;
1565 	int ret;
1566 
1567 	/*
1568 	 * Protect against theoretical sysfs writes between
1569 	 * device_add and dev_pm_qos_add_request
1570 	 */
1571 	if (!dev_pm_qos_request_active(&df->user_max_freq_req))
1572 		return -EINVAL;
1573 
1574 	ret = sscanf(buf, "%lu", &value);
1575 	if (ret != 1)
1576 		return -EINVAL;
1577 
1578 	/*
1579 	 * PM QoS frequencies are in kHz so we need to convert. Convert by
1580 	 * rounding upwards so that the acceptable interval never shrinks.
1581 	 *
1582 	 * For example if the user writes "666666666" to sysfs this value will
1583 	 * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1584 	 * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1585 	 *
1586 	 * A value of zero means "no limit".
1587 	 */
1588 	if (value)
1589 		value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1590 	else
1591 		value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1592 
1593 	ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1594 	if (ret < 0)
1595 		return ret;
1596 
1597 	return count;
1598 }
1599 
1600 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1601 			     char *buf)
1602 {
1603 	struct devfreq *df = to_devfreq(dev);
1604 	unsigned long min_freq, max_freq;
1605 
1606 	mutex_lock(&df->lock);
1607 	get_freq_range(df, &min_freq, &max_freq);
1608 	mutex_unlock(&df->lock);
1609 
1610 	return sprintf(buf, "%lu\n", max_freq);
1611 }
1612 static DEVICE_ATTR_RW(max_freq);
1613 
1614 static ssize_t available_frequencies_show(struct device *d,
1615 					  struct device_attribute *attr,
1616 					  char *buf)
1617 {
1618 	struct devfreq *df = to_devfreq(d);
1619 	ssize_t count = 0;
1620 	int i;
1621 
1622 	if (!df->profile)
1623 		return -EINVAL;
1624 
1625 	mutex_lock(&df->lock);
1626 
1627 	for (i = 0; i < df->profile->max_state; i++)
1628 		count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1629 				"%lu ", df->profile->freq_table[i]);
1630 
1631 	mutex_unlock(&df->lock);
1632 	/* Truncate the trailing space */
1633 	if (count)
1634 		count--;
1635 
1636 	count += sprintf(&buf[count], "\n");
1637 
1638 	return count;
1639 }
1640 static DEVICE_ATTR_RO(available_frequencies);
1641 
1642 static ssize_t trans_stat_show(struct device *dev,
1643 			       struct device_attribute *attr, char *buf)
1644 {
1645 	struct devfreq *df = to_devfreq(dev);
1646 	ssize_t len;
1647 	int i, j;
1648 	unsigned int max_state;
1649 
1650 	if (!df->profile)
1651 		return -EINVAL;
1652 	max_state = df->profile->max_state;
1653 
1654 	if (max_state == 0)
1655 		return sprintf(buf, "Not Supported.\n");
1656 
1657 	mutex_lock(&df->lock);
1658 	if (!df->stop_polling &&
1659 			devfreq_update_status(df, df->previous_freq)) {
1660 		mutex_unlock(&df->lock);
1661 		return 0;
1662 	}
1663 	mutex_unlock(&df->lock);
1664 
1665 	len = sprintf(buf, "     From  :   To\n");
1666 	len += sprintf(buf + len, "           :");
1667 	for (i = 0; i < max_state; i++)
1668 		len += sprintf(buf + len, "%10lu",
1669 				df->profile->freq_table[i]);
1670 
1671 	len += sprintf(buf + len, "   time(ms)\n");
1672 
1673 	for (i = 0; i < max_state; i++) {
1674 		if (df->profile->freq_table[i]
1675 					== df->previous_freq) {
1676 			len += sprintf(buf + len, "*");
1677 		} else {
1678 			len += sprintf(buf + len, " ");
1679 		}
1680 		len += sprintf(buf + len, "%10lu:",
1681 				df->profile->freq_table[i]);
1682 		for (j = 0; j < max_state; j++)
1683 			len += sprintf(buf + len, "%10u",
1684 				df->stats.trans_table[(i * max_state) + j]);
1685 
1686 		len += sprintf(buf + len, "%10llu\n", (u64)
1687 			jiffies64_to_msecs(df->stats.time_in_state[i]));
1688 	}
1689 
1690 	len += sprintf(buf + len, "Total transition : %u\n",
1691 					df->stats.total_trans);
1692 	return len;
1693 }
1694 
1695 static ssize_t trans_stat_store(struct device *dev,
1696 				struct device_attribute *attr,
1697 				const char *buf, size_t count)
1698 {
1699 	struct devfreq *df = to_devfreq(dev);
1700 	int err, value;
1701 
1702 	if (!df->profile)
1703 		return -EINVAL;
1704 
1705 	if (df->profile->max_state == 0)
1706 		return count;
1707 
1708 	err = kstrtoint(buf, 10, &value);
1709 	if (err || value != 0)
1710 		return -EINVAL;
1711 
1712 	mutex_lock(&df->lock);
1713 	memset(df->stats.time_in_state, 0, (df->profile->max_state *
1714 					sizeof(*df->stats.time_in_state)));
1715 	memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1716 					df->profile->max_state,
1717 					df->profile->max_state));
1718 	df->stats.total_trans = 0;
1719 	df->stats.last_update = get_jiffies_64();
1720 	mutex_unlock(&df->lock);
1721 
1722 	return count;
1723 }
1724 static DEVICE_ATTR_RW(trans_stat);
1725 
1726 static struct attribute *devfreq_attrs[] = {
1727 	&dev_attr_name.attr,
1728 	&dev_attr_governor.attr,
1729 	&dev_attr_available_governors.attr,
1730 	&dev_attr_cur_freq.attr,
1731 	&dev_attr_available_frequencies.attr,
1732 	&dev_attr_target_freq.attr,
1733 	&dev_attr_min_freq.attr,
1734 	&dev_attr_max_freq.attr,
1735 	&dev_attr_trans_stat.attr,
1736 	NULL,
1737 };
1738 ATTRIBUTE_GROUPS(devfreq);
1739 
1740 static ssize_t polling_interval_show(struct device *dev,
1741 				     struct device_attribute *attr, char *buf)
1742 {
1743 	struct devfreq *df = to_devfreq(dev);
1744 
1745 	if (!df->profile)
1746 		return -EINVAL;
1747 
1748 	return sprintf(buf, "%d\n", df->profile->polling_ms);
1749 }
1750 
1751 static ssize_t polling_interval_store(struct device *dev,
1752 				      struct device_attribute *attr,
1753 				      const char *buf, size_t count)
1754 {
1755 	struct devfreq *df = to_devfreq(dev);
1756 	unsigned int value;
1757 	int ret;
1758 
1759 	if (!df->governor)
1760 		return -EINVAL;
1761 
1762 	ret = sscanf(buf, "%u", &value);
1763 	if (ret != 1)
1764 		return -EINVAL;
1765 
1766 	df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1767 	ret = count;
1768 
1769 	return ret;
1770 }
1771 static DEVICE_ATTR_RW(polling_interval);
1772 
1773 static ssize_t timer_show(struct device *dev,
1774 			     struct device_attribute *attr, char *buf)
1775 {
1776 	struct devfreq *df = to_devfreq(dev);
1777 
1778 	if (!df->profile)
1779 		return -EINVAL;
1780 
1781 	return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
1782 }
1783 
1784 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
1785 			      const char *buf, size_t count)
1786 {
1787 	struct devfreq *df = to_devfreq(dev);
1788 	char str_timer[DEVFREQ_NAME_LEN + 1];
1789 	int timer = -1;
1790 	int ret = 0, i;
1791 
1792 	if (!df->governor || !df->profile)
1793 		return -EINVAL;
1794 
1795 	ret = sscanf(buf, "%16s", str_timer);
1796 	if (ret != 1)
1797 		return -EINVAL;
1798 
1799 	for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
1800 		if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
1801 			timer = i;
1802 			break;
1803 		}
1804 	}
1805 
1806 	if (timer < 0) {
1807 		ret = -EINVAL;
1808 		goto out;
1809 	}
1810 
1811 	if (df->profile->timer == timer) {
1812 		ret = 0;
1813 		goto out;
1814 	}
1815 
1816 	mutex_lock(&df->lock);
1817 	df->profile->timer = timer;
1818 	mutex_unlock(&df->lock);
1819 
1820 	ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1821 	if (ret) {
1822 		dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1823 			 __func__, df->governor->name, ret);
1824 		goto out;
1825 	}
1826 
1827 	ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1828 	if (ret)
1829 		dev_warn(dev, "%s: Governor %s not started(%d)\n",
1830 			 __func__, df->governor->name, ret);
1831 out:
1832 	return ret ? ret : count;
1833 }
1834 static DEVICE_ATTR_RW(timer);
1835 
1836 #define CREATE_SYSFS_FILE(df, name)					\
1837 {									\
1838 	int ret;							\
1839 	ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr);	\
1840 	if (ret < 0) {							\
1841 		dev_warn(&df->dev,					\
1842 			"Unable to create attr(%s)\n", "##name");	\
1843 	}								\
1844 }									\
1845 
1846 /* Create the specific sysfs files which depend on each governor. */
1847 static void create_sysfs_files(struct devfreq *devfreq,
1848 				const struct devfreq_governor *gov)
1849 {
1850 	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1851 		CREATE_SYSFS_FILE(devfreq, polling_interval);
1852 	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1853 		CREATE_SYSFS_FILE(devfreq, timer);
1854 }
1855 
1856 /* Remove the specific sysfs files which depend on each governor. */
1857 static void remove_sysfs_files(struct devfreq *devfreq,
1858 				const struct devfreq_governor *gov)
1859 {
1860 	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1861 		sysfs_remove_file(&devfreq->dev.kobj,
1862 				&dev_attr_polling_interval.attr);
1863 	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1864 		sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
1865 }
1866 
1867 /**
1868  * devfreq_summary_show() - Show the summary of the devfreq devices
1869  * @s:		seq_file instance to show the summary of devfreq devices
1870  * @data:	not used
1871  *
1872  * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1873  * It helps that user can know the detailed information of the devfreq devices.
1874  *
1875  * Return 0 always because it shows the information without any data change.
1876  */
1877 static int devfreq_summary_show(struct seq_file *s, void *data)
1878 {
1879 	struct devfreq *devfreq;
1880 	struct devfreq *p_devfreq = NULL;
1881 	unsigned long cur_freq, min_freq, max_freq;
1882 	unsigned int polling_ms;
1883 	unsigned int timer;
1884 
1885 	seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1886 			"dev",
1887 			"parent_dev",
1888 			"governor",
1889 			"timer",
1890 			"polling_ms",
1891 			"cur_freq_Hz",
1892 			"min_freq_Hz",
1893 			"max_freq_Hz");
1894 	seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1895 			"------------------------------",
1896 			"------------------------------",
1897 			"---------------",
1898 			"----------",
1899 			"----------",
1900 			"------------",
1901 			"------------",
1902 			"------------");
1903 
1904 	mutex_lock(&devfreq_list_lock);
1905 
1906 	list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1907 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1908 		if (!strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE,
1909 							DEVFREQ_NAME_LEN)) {
1910 			struct devfreq_passive_data *data = devfreq->data;
1911 
1912 			if (data)
1913 				p_devfreq = data->parent;
1914 		} else {
1915 			p_devfreq = NULL;
1916 		}
1917 #endif
1918 
1919 		mutex_lock(&devfreq->lock);
1920 		cur_freq = devfreq->previous_freq;
1921 		get_freq_range(devfreq, &min_freq, &max_freq);
1922 		timer = devfreq->profile->timer;
1923 
1924 		if (IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL))
1925 			polling_ms = devfreq->profile->polling_ms;
1926 		else
1927 			polling_ms = 0;
1928 		mutex_unlock(&devfreq->lock);
1929 
1930 		seq_printf(s,
1931 			"%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
1932 			dev_name(&devfreq->dev),
1933 			p_devfreq ? dev_name(&p_devfreq->dev) : "null",
1934 			devfreq->governor->name,
1935 			polling_ms ? timer_name[timer] : "null",
1936 			polling_ms,
1937 			cur_freq,
1938 			min_freq,
1939 			max_freq);
1940 	}
1941 
1942 	mutex_unlock(&devfreq_list_lock);
1943 
1944 	return 0;
1945 }
1946 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
1947 
1948 static int __init devfreq_init(void)
1949 {
1950 	devfreq_class = class_create(THIS_MODULE, "devfreq");
1951 	if (IS_ERR(devfreq_class)) {
1952 		pr_err("%s: couldn't create class\n", __FILE__);
1953 		return PTR_ERR(devfreq_class);
1954 	}
1955 
1956 	devfreq_wq = create_freezable_workqueue("devfreq_wq");
1957 	if (!devfreq_wq) {
1958 		class_destroy(devfreq_class);
1959 		pr_err("%s: couldn't create workqueue\n", __FILE__);
1960 		return -ENOMEM;
1961 	}
1962 	devfreq_class->dev_groups = devfreq_groups;
1963 
1964 	devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
1965 	debugfs_create_file("devfreq_summary", 0444,
1966 				devfreq_debugfs, NULL,
1967 				&devfreq_summary_fops);
1968 
1969 	return 0;
1970 }
1971 subsys_initcall(devfreq_init);
1972 
1973 /*
1974  * The following are helper functions for devfreq user device drivers with
1975  * OPP framework.
1976  */
1977 
1978 /**
1979  * devfreq_recommended_opp() - Helper function to get proper OPP for the
1980  *			     freq value given to target callback.
1981  * @dev:	The devfreq user device. (parent of devfreq)
1982  * @freq:	The frequency given to target function
1983  * @flags:	Flags handed from devfreq framework.
1984  *
1985  * The callers are required to call dev_pm_opp_put() for the returned OPP after
1986  * use.
1987  */
1988 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1989 					   unsigned long *freq,
1990 					   u32 flags)
1991 {
1992 	struct dev_pm_opp *opp;
1993 
1994 	if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1995 		/* The freq is an upper bound. opp should be lower */
1996 		opp = dev_pm_opp_find_freq_floor(dev, freq);
1997 
1998 		/* If not available, use the closest opp */
1999 		if (opp == ERR_PTR(-ERANGE))
2000 			opp = dev_pm_opp_find_freq_ceil(dev, freq);
2001 	} else {
2002 		/* The freq is an lower bound. opp should be higher */
2003 		opp = dev_pm_opp_find_freq_ceil(dev, freq);
2004 
2005 		/* If not available, use the closest opp */
2006 		if (opp == ERR_PTR(-ERANGE))
2007 			opp = dev_pm_opp_find_freq_floor(dev, freq);
2008 	}
2009 
2010 	return opp;
2011 }
2012 EXPORT_SYMBOL(devfreq_recommended_opp);
2013 
2014 /**
2015  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
2016  *				     for any changes in the OPP availability
2017  *				     changes
2018  * @dev:	The devfreq user device. (parent of devfreq)
2019  * @devfreq:	The devfreq object.
2020  */
2021 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
2022 {
2023 	return dev_pm_opp_register_notifier(dev, &devfreq->nb);
2024 }
2025 EXPORT_SYMBOL(devfreq_register_opp_notifier);
2026 
2027 /**
2028  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
2029  *				       notified for any changes in the OPP
2030  *				       availability changes anymore.
2031  * @dev:	The devfreq user device. (parent of devfreq)
2032  * @devfreq:	The devfreq object.
2033  *
2034  * At exit() callback of devfreq_dev_profile, this must be included if
2035  * devfreq_recommended_opp is used.
2036  */
2037 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
2038 {
2039 	return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
2040 }
2041 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
2042 
2043 static void devm_devfreq_opp_release(struct device *dev, void *res)
2044 {
2045 	devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
2046 }
2047 
2048 /**
2049  * devm_devfreq_register_opp_notifier() - Resource-managed
2050  *					  devfreq_register_opp_notifier()
2051  * @dev:	The devfreq user device. (parent of devfreq)
2052  * @devfreq:	The devfreq object.
2053  */
2054 int devm_devfreq_register_opp_notifier(struct device *dev,
2055 				       struct devfreq *devfreq)
2056 {
2057 	struct devfreq **ptr;
2058 	int ret;
2059 
2060 	ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
2061 	if (!ptr)
2062 		return -ENOMEM;
2063 
2064 	ret = devfreq_register_opp_notifier(dev, devfreq);
2065 	if (ret) {
2066 		devres_free(ptr);
2067 		return ret;
2068 	}
2069 
2070 	*ptr = devfreq;
2071 	devres_add(dev, ptr);
2072 
2073 	return 0;
2074 }
2075 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
2076 
2077 /**
2078  * devm_devfreq_unregister_opp_notifier() - Resource-managed
2079  *					    devfreq_unregister_opp_notifier()
2080  * @dev:	The devfreq user device. (parent of devfreq)
2081  * @devfreq:	The devfreq object.
2082  */
2083 void devm_devfreq_unregister_opp_notifier(struct device *dev,
2084 					 struct devfreq *devfreq)
2085 {
2086 	WARN_ON(devres_release(dev, devm_devfreq_opp_release,
2087 			       devm_devfreq_dev_match, devfreq));
2088 }
2089 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
2090 
2091 /**
2092  * devfreq_register_notifier() - Register a driver with devfreq
2093  * @devfreq:	The devfreq object.
2094  * @nb:		The notifier block to register.
2095  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
2096  */
2097 int devfreq_register_notifier(struct devfreq *devfreq,
2098 			      struct notifier_block *nb,
2099 			      unsigned int list)
2100 {
2101 	int ret = 0;
2102 
2103 	if (!devfreq)
2104 		return -EINVAL;
2105 
2106 	switch (list) {
2107 	case DEVFREQ_TRANSITION_NOTIFIER:
2108 		ret = srcu_notifier_chain_register(
2109 				&devfreq->transition_notifier_list, nb);
2110 		break;
2111 	default:
2112 		ret = -EINVAL;
2113 	}
2114 
2115 	return ret;
2116 }
2117 EXPORT_SYMBOL(devfreq_register_notifier);
2118 
2119 /*
2120  * devfreq_unregister_notifier() - Unregister a driver with devfreq
2121  * @devfreq:	The devfreq object.
2122  * @nb:		The notifier block to be unregistered.
2123  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
2124  */
2125 int devfreq_unregister_notifier(struct devfreq *devfreq,
2126 				struct notifier_block *nb,
2127 				unsigned int list)
2128 {
2129 	int ret = 0;
2130 
2131 	if (!devfreq)
2132 		return -EINVAL;
2133 
2134 	switch (list) {
2135 	case DEVFREQ_TRANSITION_NOTIFIER:
2136 		ret = srcu_notifier_chain_unregister(
2137 				&devfreq->transition_notifier_list, nb);
2138 		break;
2139 	default:
2140 		ret = -EINVAL;
2141 	}
2142 
2143 	return ret;
2144 }
2145 EXPORT_SYMBOL(devfreq_unregister_notifier);
2146 
2147 struct devfreq_notifier_devres {
2148 	struct devfreq *devfreq;
2149 	struct notifier_block *nb;
2150 	unsigned int list;
2151 };
2152 
2153 static void devm_devfreq_notifier_release(struct device *dev, void *res)
2154 {
2155 	struct devfreq_notifier_devres *this = res;
2156 
2157 	devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
2158 }
2159 
2160 /**
2161  * devm_devfreq_register_notifier()
2162  *	- Resource-managed devfreq_register_notifier()
2163  * @dev:	The devfreq user device. (parent of devfreq)
2164  * @devfreq:	The devfreq object.
2165  * @nb:		The notifier block to be unregistered.
2166  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
2167  */
2168 int devm_devfreq_register_notifier(struct device *dev,
2169 				struct devfreq *devfreq,
2170 				struct notifier_block *nb,
2171 				unsigned int list)
2172 {
2173 	struct devfreq_notifier_devres *ptr;
2174 	int ret;
2175 
2176 	ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
2177 				GFP_KERNEL);
2178 	if (!ptr)
2179 		return -ENOMEM;
2180 
2181 	ret = devfreq_register_notifier(devfreq, nb, list);
2182 	if (ret) {
2183 		devres_free(ptr);
2184 		return ret;
2185 	}
2186 
2187 	ptr->devfreq = devfreq;
2188 	ptr->nb = nb;
2189 	ptr->list = list;
2190 	devres_add(dev, ptr);
2191 
2192 	return 0;
2193 }
2194 EXPORT_SYMBOL(devm_devfreq_register_notifier);
2195 
2196 /**
2197  * devm_devfreq_unregister_notifier()
2198  *	- Resource-managed devfreq_unregister_notifier()
2199  * @dev:	The devfreq user device. (parent of devfreq)
2200  * @devfreq:	The devfreq object.
2201  * @nb:		The notifier block to be unregistered.
2202  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
2203  */
2204 void devm_devfreq_unregister_notifier(struct device *dev,
2205 				      struct devfreq *devfreq,
2206 				      struct notifier_block *nb,
2207 				      unsigned int list)
2208 {
2209 	WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
2210 			       devm_devfreq_dev_match, devfreq));
2211 }
2212 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);
2213