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