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