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