• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
3  *	    for Non-CPU Devices.
4  *
5  * Copyright (C) 2011 Samsung Electronics
6  *	MyungJoo Ham <myungjoo.ham@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/sched.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 "governor.h"
30 
31 static struct class *devfreq_class;
32 
33 /*
34  * devfreq core provides delayed work based load monitoring helper
35  * functions. Governors can use these or can implement their own
36  * monitoring mechanism.
37  */
38 static struct workqueue_struct *devfreq_wq;
39 
40 /* The list of all device-devfreq governors */
41 static LIST_HEAD(devfreq_governor_list);
42 /* The list of all device-devfreq */
43 static LIST_HEAD(devfreq_list);
44 static DEFINE_MUTEX(devfreq_list_lock);
45 
46 /**
47  * find_device_devfreq() - find devfreq struct using device pointer
48  * @dev:	device pointer used to lookup device devfreq.
49  *
50  * Search the list of device devfreqs and return the matched device's
51  * devfreq info. devfreq_list_lock should be held by the caller.
52  */
find_device_devfreq(struct device * dev)53 static struct devfreq *find_device_devfreq(struct device *dev)
54 {
55 	struct devfreq *tmp_devfreq;
56 
57 	if (IS_ERR_OR_NULL(dev)) {
58 		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
59 		return ERR_PTR(-EINVAL);
60 	}
61 	WARN(!mutex_is_locked(&devfreq_list_lock),
62 	     "devfreq_list_lock must be locked.");
63 
64 	list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
65 		if (tmp_devfreq->dev.parent == dev)
66 			return tmp_devfreq;
67 	}
68 
69 	return ERR_PTR(-ENODEV);
70 }
71 
72 /**
73  * devfreq_get_freq_level() - Lookup freq_table for the frequency
74  * @devfreq:	the devfreq instance
75  * @freq:	the target frequency
76  */
devfreq_get_freq_level(struct devfreq * devfreq,unsigned long freq)77 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
78 {
79 	int lev;
80 
81 	for (lev = 0; lev < devfreq->profile->max_state; lev++)
82 		if (freq == devfreq->profile->freq_table[lev])
83 			return lev;
84 
85 	return -EINVAL;
86 }
87 
88 /**
89  * devfreq_set_freq_table() - Initialize freq_table for the frequency
90  * @devfreq:	the devfreq instance
91  */
devfreq_set_freq_table(struct devfreq * devfreq)92 static void devfreq_set_freq_table(struct devfreq *devfreq)
93 {
94 	struct devfreq_dev_profile *profile = devfreq->profile;
95 	struct dev_pm_opp *opp;
96 	unsigned long freq;
97 	int i, count;
98 
99 	/* Initialize the freq_table from OPP table */
100 	count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
101 	if (count <= 0)
102 		return;
103 
104 	profile->max_state = count;
105 	profile->freq_table = devm_kcalloc(devfreq->dev.parent,
106 					profile->max_state,
107 					sizeof(*profile->freq_table),
108 					GFP_KERNEL);
109 	if (!profile->freq_table) {
110 		profile->max_state = 0;
111 		return;
112 	}
113 
114 	for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
115 		opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
116 		if (IS_ERR(opp)) {
117 			devm_kfree(devfreq->dev.parent, profile->freq_table);
118 			profile->max_state = 0;
119 			return;
120 		}
121 		dev_pm_opp_put(opp);
122 		profile->freq_table[i] = freq;
123 	}
124 }
125 
126 /**
127  * devfreq_update_status() - Update statistics of devfreq behavior
128  * @devfreq:	the devfreq instance
129  * @freq:	the update target frequency
130  */
devfreq_update_status(struct devfreq * devfreq,unsigned long freq)131 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
132 {
133 	int lev, prev_lev, ret = 0;
134 	unsigned long cur_time;
135 
136 	lockdep_assert_held(&devfreq->lock);
137 	cur_time = jiffies;
138 
139 	/* Immediately exit if previous_freq is not initialized yet. */
140 	if (!devfreq->previous_freq)
141 		goto out;
142 
143 	prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
144 	if (prev_lev < 0) {
145 		ret = prev_lev;
146 		goto out;
147 	}
148 
149 	devfreq->time_in_state[prev_lev] +=
150 			 cur_time - devfreq->last_stat_updated;
151 
152 	lev = devfreq_get_freq_level(devfreq, freq);
153 	if (lev < 0) {
154 		ret = lev;
155 		goto out;
156 	}
157 
158 	if (lev != prev_lev) {
159 		devfreq->trans_table[(prev_lev *
160 				devfreq->profile->max_state) + lev]++;
161 		devfreq->total_trans++;
162 	}
163 
164 out:
165 	devfreq->last_stat_updated = cur_time;
166 	return ret;
167 }
168 EXPORT_SYMBOL(devfreq_update_status);
169 
170 /**
171  * find_devfreq_governor() - find devfreq governor from name
172  * @name:	name of the governor
173  *
174  * Search the list of devfreq governors and return the matched
175  * governor's pointer. devfreq_list_lock should be held by the caller.
176  */
find_devfreq_governor(const char * name)177 static struct devfreq_governor *find_devfreq_governor(const char *name)
178 {
179 	struct devfreq_governor *tmp_governor;
180 
181 	if (IS_ERR_OR_NULL(name)) {
182 		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
183 		return ERR_PTR(-EINVAL);
184 	}
185 	WARN(!mutex_is_locked(&devfreq_list_lock),
186 	     "devfreq_list_lock must be locked.");
187 
188 	list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
189 		if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
190 			return tmp_governor;
191 	}
192 
193 	return ERR_PTR(-ENODEV);
194 }
195 
devfreq_notify_transition(struct devfreq * devfreq,struct devfreq_freqs * freqs,unsigned int state)196 static int devfreq_notify_transition(struct devfreq *devfreq,
197 		struct devfreq_freqs *freqs, unsigned int state)
198 {
199 	if (!devfreq)
200 		return -EINVAL;
201 
202 	switch (state) {
203 	case DEVFREQ_PRECHANGE:
204 		srcu_notifier_call_chain(&devfreq->transition_notifier_list,
205 				DEVFREQ_PRECHANGE, freqs);
206 		break;
207 
208 	case DEVFREQ_POSTCHANGE:
209 		srcu_notifier_call_chain(&devfreq->transition_notifier_list,
210 				DEVFREQ_POSTCHANGE, freqs);
211 		break;
212 	default:
213 		return -EINVAL;
214 	}
215 
216 	return 0;
217 }
218 
219 /* Load monitoring helper functions for governors use */
220 
221 /**
222  * update_devfreq() - Reevaluate the device and configure frequency.
223  * @devfreq:	the devfreq instance.
224  *
225  * Note: Lock devfreq->lock before calling update_devfreq
226  *	 This function is exported for governors.
227  */
update_devfreq(struct devfreq * devfreq)228 int update_devfreq(struct devfreq *devfreq)
229 {
230 	struct devfreq_freqs freqs;
231 	unsigned long freq, cur_freq;
232 	int err = 0;
233 	u32 flags = 0;
234 
235 	if (!mutex_is_locked(&devfreq->lock)) {
236 		WARN(true, "devfreq->lock must be locked by the caller.\n");
237 		return -EINVAL;
238 	}
239 
240 	if (!devfreq->governor)
241 		return -EINVAL;
242 
243 	/* Reevaluate the proper frequency */
244 	err = devfreq->governor->get_target_freq(devfreq, &freq);
245 	if (err)
246 		return err;
247 
248 	/*
249 	 * Adjust the frequency with user freq and QoS.
250 	 *
251 	 * List from the highest priority
252 	 * max_freq
253 	 * min_freq
254 	 */
255 
256 	if (devfreq->min_freq && freq < devfreq->min_freq) {
257 		freq = devfreq->min_freq;
258 		flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
259 	}
260 	if (devfreq->max_freq && freq > devfreq->max_freq) {
261 		freq = devfreq->max_freq;
262 		flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
263 	}
264 
265 	if (devfreq->profile->get_cur_freq)
266 		devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
267 	else
268 		cur_freq = devfreq->previous_freq;
269 
270 	freqs.old = cur_freq;
271 	freqs.new = freq;
272 	devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
273 
274 	err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
275 	if (err) {
276 		freqs.new = cur_freq;
277 		devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
278 		return err;
279 	}
280 
281 	freqs.new = freq;
282 	devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
283 
284 	if (devfreq->profile->freq_table)
285 		if (devfreq_update_status(devfreq, freq))
286 			dev_err(&devfreq->dev,
287 				"Couldn't update frequency transition information.\n");
288 
289 	devfreq->previous_freq = freq;
290 	return err;
291 }
292 EXPORT_SYMBOL(update_devfreq);
293 
294 /**
295  * devfreq_monitor() - Periodically poll devfreq objects.
296  * @work:	the work struct used to run devfreq_monitor periodically.
297  *
298  */
devfreq_monitor(struct work_struct * work)299 static void devfreq_monitor(struct work_struct *work)
300 {
301 	int err;
302 	struct devfreq *devfreq = container_of(work,
303 					struct devfreq, work.work);
304 
305 	mutex_lock(&devfreq->lock);
306 	err = update_devfreq(devfreq);
307 	if (err)
308 		dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
309 
310 	queue_delayed_work(devfreq_wq, &devfreq->work,
311 				msecs_to_jiffies(devfreq->profile->polling_ms));
312 	mutex_unlock(&devfreq->lock);
313 }
314 
315 /**
316  * devfreq_monitor_start() - Start load monitoring of devfreq instance
317  * @devfreq:	the devfreq instance.
318  *
319  * Helper function for starting devfreq device load monitoing. By
320  * default delayed work based monitoring is supported. Function
321  * to be called from governor in response to DEVFREQ_GOV_START
322  * event when device is added to devfreq framework.
323  */
devfreq_monitor_start(struct devfreq * devfreq)324 void devfreq_monitor_start(struct devfreq *devfreq)
325 {
326 	INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
327 	if (devfreq->profile->polling_ms)
328 		queue_delayed_work(devfreq_wq, &devfreq->work,
329 			msecs_to_jiffies(devfreq->profile->polling_ms));
330 }
331 EXPORT_SYMBOL(devfreq_monitor_start);
332 
333 /**
334  * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
335  * @devfreq:	the devfreq instance.
336  *
337  * Helper function to stop devfreq device load monitoing. Function
338  * to be called from governor in response to DEVFREQ_GOV_STOP
339  * event when device is removed from devfreq framework.
340  */
devfreq_monitor_stop(struct devfreq * devfreq)341 void devfreq_monitor_stop(struct devfreq *devfreq)
342 {
343 	cancel_delayed_work_sync(&devfreq->work);
344 }
345 EXPORT_SYMBOL(devfreq_monitor_stop);
346 
347 /**
348  * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
349  * @devfreq:	the devfreq instance.
350  *
351  * Helper function to suspend devfreq device load monitoing. Function
352  * to be called from governor in response to DEVFREQ_GOV_SUSPEND
353  * event or when polling interval is set to zero.
354  *
355  * Note: Though this function is same as devfreq_monitor_stop(),
356  * intentionally kept separate to provide hooks for collecting
357  * transition statistics.
358  */
devfreq_monitor_suspend(struct devfreq * devfreq)359 void devfreq_monitor_suspend(struct devfreq *devfreq)
360 {
361 	mutex_lock(&devfreq->lock);
362 	if (devfreq->stop_polling) {
363 		mutex_unlock(&devfreq->lock);
364 		return;
365 	}
366 
367 	devfreq_update_status(devfreq, devfreq->previous_freq);
368 	devfreq->stop_polling = true;
369 	mutex_unlock(&devfreq->lock);
370 	cancel_delayed_work_sync(&devfreq->work);
371 }
372 EXPORT_SYMBOL(devfreq_monitor_suspend);
373 
374 /**
375  * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
376  * @devfreq:    the devfreq instance.
377  *
378  * Helper function to resume devfreq device load monitoing. Function
379  * to be called from governor in response to DEVFREQ_GOV_RESUME
380  * event or when polling interval is set to non-zero.
381  */
devfreq_monitor_resume(struct devfreq * devfreq)382 void devfreq_monitor_resume(struct devfreq *devfreq)
383 {
384 	unsigned long freq;
385 
386 	mutex_lock(&devfreq->lock);
387 	if (!devfreq->stop_polling)
388 		goto out;
389 
390 	if (!delayed_work_pending(&devfreq->work) &&
391 			devfreq->profile->polling_ms)
392 		queue_delayed_work(devfreq_wq, &devfreq->work,
393 			msecs_to_jiffies(devfreq->profile->polling_ms));
394 
395 	devfreq->last_stat_updated = jiffies;
396 	devfreq->stop_polling = false;
397 
398 	if (devfreq->profile->get_cur_freq &&
399 		!devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
400 		devfreq->previous_freq = freq;
401 
402 out:
403 	mutex_unlock(&devfreq->lock);
404 }
405 EXPORT_SYMBOL(devfreq_monitor_resume);
406 
407 /**
408  * devfreq_interval_update() - Update device devfreq monitoring interval
409  * @devfreq:    the devfreq instance.
410  * @delay:      new polling interval to be set.
411  *
412  * Helper function to set new load monitoring polling interval. Function
413  * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
414  */
devfreq_interval_update(struct devfreq * devfreq,unsigned int * delay)415 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
416 {
417 	unsigned int cur_delay = devfreq->profile->polling_ms;
418 	unsigned int new_delay = *delay;
419 
420 	mutex_lock(&devfreq->lock);
421 	devfreq->profile->polling_ms = new_delay;
422 
423 	if (devfreq->stop_polling)
424 		goto out;
425 
426 	/* if new delay is zero, stop polling */
427 	if (!new_delay) {
428 		mutex_unlock(&devfreq->lock);
429 		cancel_delayed_work_sync(&devfreq->work);
430 		return;
431 	}
432 
433 	/* if current delay is zero, start polling with new delay */
434 	if (!cur_delay) {
435 		queue_delayed_work(devfreq_wq, &devfreq->work,
436 			msecs_to_jiffies(devfreq->profile->polling_ms));
437 		goto out;
438 	}
439 
440 	/* if current delay is greater than new delay, restart polling */
441 	if (cur_delay > new_delay) {
442 		mutex_unlock(&devfreq->lock);
443 		cancel_delayed_work_sync(&devfreq->work);
444 		mutex_lock(&devfreq->lock);
445 		if (!devfreq->stop_polling)
446 			queue_delayed_work(devfreq_wq, &devfreq->work,
447 			      msecs_to_jiffies(devfreq->profile->polling_ms));
448 	}
449 out:
450 	mutex_unlock(&devfreq->lock);
451 }
452 EXPORT_SYMBOL(devfreq_interval_update);
453 
454 /**
455  * devfreq_notifier_call() - Notify that the device frequency requirements
456  *			   has been changed out of devfreq framework.
457  * @nb:		the notifier_block (supposed to be devfreq->nb)
458  * @type:	not used
459  * @devp:	not used
460  *
461  * Called by a notifier that uses devfreq->nb.
462  */
devfreq_notifier_call(struct notifier_block * nb,unsigned long type,void * devp)463 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
464 				 void *devp)
465 {
466 	struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
467 	int ret;
468 
469 	mutex_lock(&devfreq->lock);
470 	ret = update_devfreq(devfreq);
471 	mutex_unlock(&devfreq->lock);
472 
473 	return ret;
474 }
475 
476 /**
477  * devfreq_dev_release() - Callback for struct device to release the device.
478  * @dev:	the devfreq device
479  *
480  * Remove devfreq from the list and release its resources.
481  */
devfreq_dev_release(struct device * dev)482 static void devfreq_dev_release(struct device *dev)
483 {
484 	struct devfreq *devfreq = to_devfreq(dev);
485 
486 	mutex_lock(&devfreq_list_lock);
487 	list_del(&devfreq->node);
488 	mutex_unlock(&devfreq_list_lock);
489 
490 	if (devfreq->governor)
491 		devfreq->governor->event_handler(devfreq,
492 						 DEVFREQ_GOV_STOP, NULL);
493 
494 	if (devfreq->profile->exit)
495 		devfreq->profile->exit(devfreq->dev.parent);
496 
497 	mutex_destroy(&devfreq->lock);
498 	kfree(devfreq);
499 }
500 
501 /**
502  * devfreq_add_device() - Add devfreq feature to the device
503  * @dev:	the device to add devfreq feature.
504  * @profile:	device-specific profile to run devfreq.
505  * @governor_name:	name of the policy to choose frequency.
506  * @data:	private data for the governor. The devfreq framework does not
507  *		touch this value.
508  */
devfreq_add_device(struct device * dev,struct devfreq_dev_profile * profile,const char * governor_name,void * data)509 struct devfreq *devfreq_add_device(struct device *dev,
510 				   struct devfreq_dev_profile *profile,
511 				   const char *governor_name,
512 				   void *data)
513 {
514 	struct devfreq *devfreq;
515 	struct devfreq_governor *governor;
516 	int err = 0;
517 
518 	if (!dev || !profile || !governor_name) {
519 		dev_err(dev, "%s: Invalid parameters.\n", __func__);
520 		return ERR_PTR(-EINVAL);
521 	}
522 
523 	mutex_lock(&devfreq_list_lock);
524 	devfreq = find_device_devfreq(dev);
525 	mutex_unlock(&devfreq_list_lock);
526 	if (!IS_ERR(devfreq)) {
527 		dev_err(dev, "%s: Unable to create devfreq for the device.\n",
528 			__func__);
529 		err = -EINVAL;
530 		goto err_out;
531 	}
532 
533 	devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
534 	if (!devfreq) {
535 		err = -ENOMEM;
536 		goto err_out;
537 	}
538 
539 	mutex_init(&devfreq->lock);
540 	mutex_lock(&devfreq->lock);
541 	devfreq->dev.parent = dev;
542 	devfreq->dev.class = devfreq_class;
543 	devfreq->dev.release = devfreq_dev_release;
544 	INIT_LIST_HEAD(&devfreq->node);
545 	devfreq->profile = profile;
546 	strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
547 	devfreq->previous_freq = profile->initial_freq;
548 	devfreq->last_status.current_frequency = profile->initial_freq;
549 	devfreq->data = data;
550 	devfreq->nb.notifier_call = devfreq_notifier_call;
551 
552 	if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
553 		mutex_unlock(&devfreq->lock);
554 		devfreq_set_freq_table(devfreq);
555 		mutex_lock(&devfreq->lock);
556 	}
557 
558 	dev_set_name(&devfreq->dev, "%s", dev_name(dev));
559 	err = device_register(&devfreq->dev);
560 	if (err) {
561 		mutex_unlock(&devfreq->lock);
562 		goto err_dev;
563 	}
564 
565 	devfreq->trans_table =	devm_kzalloc(&devfreq->dev,
566 						sizeof(unsigned int) *
567 						devfreq->profile->max_state *
568 						devfreq->profile->max_state,
569 						GFP_KERNEL);
570 	devfreq->time_in_state = devm_kzalloc(&devfreq->dev,
571 						sizeof(unsigned long) *
572 						devfreq->profile->max_state,
573 						GFP_KERNEL);
574 	devfreq->last_stat_updated = jiffies;
575 
576 	srcu_init_notifier_head(&devfreq->transition_notifier_list);
577 
578 	mutex_unlock(&devfreq->lock);
579 
580 	mutex_lock(&devfreq_list_lock);
581 	list_add(&devfreq->node, &devfreq_list);
582 
583 	governor = find_devfreq_governor(devfreq->governor_name);
584 	if (IS_ERR(governor)) {
585 		dev_err(dev, "%s: Unable to find governor for the device\n",
586 			__func__);
587 		err = PTR_ERR(governor);
588 		goto err_init;
589 	}
590 
591 	devfreq->governor = governor;
592 	err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
593 						NULL);
594 	if (err) {
595 		dev_err(dev, "%s: Unable to start governor for the device\n",
596 			__func__);
597 		goto err_init;
598 	}
599 	mutex_unlock(&devfreq_list_lock);
600 
601 	return devfreq;
602 
603 err_init:
604 	list_del(&devfreq->node);
605 	mutex_unlock(&devfreq_list_lock);
606 
607 	device_unregister(&devfreq->dev);
608 err_dev:
609 	if (devfreq)
610 		kfree(devfreq);
611 err_out:
612 	return ERR_PTR(err);
613 }
614 EXPORT_SYMBOL(devfreq_add_device);
615 
616 /**
617  * devfreq_remove_device() - Remove devfreq feature from a device.
618  * @devfreq:	the devfreq instance to be removed
619  *
620  * The opposite of devfreq_add_device().
621  */
devfreq_remove_device(struct devfreq * devfreq)622 int devfreq_remove_device(struct devfreq *devfreq)
623 {
624 	if (!devfreq)
625 		return -EINVAL;
626 
627 	device_unregister(&devfreq->dev);
628 
629 	return 0;
630 }
631 EXPORT_SYMBOL(devfreq_remove_device);
632 
devm_devfreq_dev_match(struct device * dev,void * res,void * data)633 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
634 {
635 	struct devfreq **r = res;
636 
637 	if (WARN_ON(!r || !*r))
638 		return 0;
639 
640 	return *r == data;
641 }
642 
devm_devfreq_dev_release(struct device * dev,void * res)643 static void devm_devfreq_dev_release(struct device *dev, void *res)
644 {
645 	devfreq_remove_device(*(struct devfreq **)res);
646 }
647 
648 /**
649  * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
650  * @dev:	the device to add devfreq feature.
651  * @profile:	device-specific profile to run devfreq.
652  * @governor_name:	name of the policy to choose frequency.
653  * @data:	private data for the governor. The devfreq framework does not
654  *		touch this value.
655  *
656  * This function manages automatically the memory of devfreq device using device
657  * resource management and simplify the free operation for memory of devfreq
658  * device.
659  */
devm_devfreq_add_device(struct device * dev,struct devfreq_dev_profile * profile,const char * governor_name,void * data)660 struct devfreq *devm_devfreq_add_device(struct device *dev,
661 					struct devfreq_dev_profile *profile,
662 					const char *governor_name,
663 					void *data)
664 {
665 	struct devfreq **ptr, *devfreq;
666 
667 	ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
668 	if (!ptr)
669 		return ERR_PTR(-ENOMEM);
670 
671 	devfreq = devfreq_add_device(dev, profile, governor_name, data);
672 	if (IS_ERR(devfreq)) {
673 		devres_free(ptr);
674 		return devfreq;
675 	}
676 
677 	*ptr = devfreq;
678 	devres_add(dev, ptr);
679 
680 	return devfreq;
681 }
682 EXPORT_SYMBOL(devm_devfreq_add_device);
683 
684 #ifdef CONFIG_OF
685 /*
686  * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
687  * @dev - instance to the given device
688  * @index - index into list of devfreq
689  *
690  * return the instance of devfreq device
691  */
devfreq_get_devfreq_by_phandle(struct device * dev,int index)692 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
693 {
694 	struct device_node *node;
695 	struct devfreq *devfreq;
696 
697 	if (!dev)
698 		return ERR_PTR(-EINVAL);
699 
700 	if (!dev->of_node)
701 		return ERR_PTR(-EINVAL);
702 
703 	node = of_parse_phandle(dev->of_node, "devfreq", index);
704 	if (!node)
705 		return ERR_PTR(-ENODEV);
706 
707 	mutex_lock(&devfreq_list_lock);
708 	list_for_each_entry(devfreq, &devfreq_list, node) {
709 		if (devfreq->dev.parent
710 			&& devfreq->dev.parent->of_node == node) {
711 			mutex_unlock(&devfreq_list_lock);
712 			of_node_put(node);
713 			return devfreq;
714 		}
715 	}
716 	mutex_unlock(&devfreq_list_lock);
717 	of_node_put(node);
718 
719 	return ERR_PTR(-EPROBE_DEFER);
720 }
721 #else
devfreq_get_devfreq_by_phandle(struct device * dev,int index)722 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
723 {
724 	return ERR_PTR(-ENODEV);
725 }
726 #endif /* CONFIG_OF */
727 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
728 
729 /**
730  * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
731  * @dev:	the device to add devfreq feature.
732  * @devfreq:	the devfreq instance to be removed
733  */
devm_devfreq_remove_device(struct device * dev,struct devfreq * devfreq)734 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
735 {
736 	WARN_ON(devres_release(dev, devm_devfreq_dev_release,
737 			       devm_devfreq_dev_match, devfreq));
738 }
739 EXPORT_SYMBOL(devm_devfreq_remove_device);
740 
741 /**
742  * devfreq_suspend_device() - Suspend devfreq of a device.
743  * @devfreq: the devfreq instance to be suspended
744  *
745  * This function is intended to be called by the pm callbacks
746  * (e.g., runtime_suspend, suspend) of the device driver that
747  * holds the devfreq.
748  */
devfreq_suspend_device(struct devfreq * devfreq)749 int devfreq_suspend_device(struct devfreq *devfreq)
750 {
751 	if (!devfreq)
752 		return -EINVAL;
753 
754 	if (!devfreq->governor)
755 		return 0;
756 
757 	return devfreq->governor->event_handler(devfreq,
758 				DEVFREQ_GOV_SUSPEND, NULL);
759 }
760 EXPORT_SYMBOL(devfreq_suspend_device);
761 
762 /**
763  * devfreq_resume_device() - Resume devfreq of a device.
764  * @devfreq: the devfreq instance to be resumed
765  *
766  * This function is intended to be called by the pm callbacks
767  * (e.g., runtime_resume, resume) of the device driver that
768  * holds the devfreq.
769  */
devfreq_resume_device(struct devfreq * devfreq)770 int devfreq_resume_device(struct devfreq *devfreq)
771 {
772 	if (!devfreq)
773 		return -EINVAL;
774 
775 	if (!devfreq->governor)
776 		return 0;
777 
778 	return devfreq->governor->event_handler(devfreq,
779 				DEVFREQ_GOV_RESUME, NULL);
780 }
781 EXPORT_SYMBOL(devfreq_resume_device);
782 
783 /**
784  * devfreq_add_governor() - Add devfreq governor
785  * @governor:	the devfreq governor to be added
786  */
devfreq_add_governor(struct devfreq_governor * governor)787 int devfreq_add_governor(struct devfreq_governor *governor)
788 {
789 	struct devfreq_governor *g;
790 	struct devfreq *devfreq;
791 	int err = 0;
792 
793 	if (!governor) {
794 		pr_err("%s: Invalid parameters.\n", __func__);
795 		return -EINVAL;
796 	}
797 
798 	mutex_lock(&devfreq_list_lock);
799 	g = find_devfreq_governor(governor->name);
800 	if (!IS_ERR(g)) {
801 		pr_err("%s: governor %s already registered\n", __func__,
802 		       g->name);
803 		err = -EINVAL;
804 		goto err_out;
805 	}
806 
807 	list_add(&governor->node, &devfreq_governor_list);
808 
809 	list_for_each_entry(devfreq, &devfreq_list, node) {
810 		int ret = 0;
811 		struct device *dev = devfreq->dev.parent;
812 
813 		if (!strncmp(devfreq->governor_name, governor->name,
814 			     DEVFREQ_NAME_LEN)) {
815 			/* The following should never occur */
816 			if (devfreq->governor) {
817 				dev_warn(dev,
818 					 "%s: Governor %s already present\n",
819 					 __func__, devfreq->governor->name);
820 				ret = devfreq->governor->event_handler(devfreq,
821 							DEVFREQ_GOV_STOP, NULL);
822 				if (ret) {
823 					dev_warn(dev,
824 						 "%s: Governor %s stop = %d\n",
825 						 __func__,
826 						 devfreq->governor->name, ret);
827 				}
828 				/* Fall through */
829 			}
830 			devfreq->governor = governor;
831 			ret = devfreq->governor->event_handler(devfreq,
832 						DEVFREQ_GOV_START, NULL);
833 			if (ret) {
834 				dev_warn(dev, "%s: Governor %s start=%d\n",
835 					 __func__, devfreq->governor->name,
836 					 ret);
837 			}
838 		}
839 	}
840 
841 err_out:
842 	mutex_unlock(&devfreq_list_lock);
843 
844 	return err;
845 }
846 EXPORT_SYMBOL(devfreq_add_governor);
847 
848 /**
849  * devfreq_remove_governor() - Remove devfreq feature from a device.
850  * @governor:	the devfreq governor to be removed
851  */
devfreq_remove_governor(struct devfreq_governor * governor)852 int devfreq_remove_governor(struct devfreq_governor *governor)
853 {
854 	struct devfreq_governor *g;
855 	struct devfreq *devfreq;
856 	int err = 0;
857 
858 	if (!governor) {
859 		pr_err("%s: Invalid parameters.\n", __func__);
860 		return -EINVAL;
861 	}
862 
863 	mutex_lock(&devfreq_list_lock);
864 	g = find_devfreq_governor(governor->name);
865 	if (IS_ERR(g)) {
866 		pr_err("%s: governor %s not registered\n", __func__,
867 		       governor->name);
868 		err = PTR_ERR(g);
869 		goto err_out;
870 	}
871 	list_for_each_entry(devfreq, &devfreq_list, node) {
872 		int ret;
873 		struct device *dev = devfreq->dev.parent;
874 
875 		if (!strncmp(devfreq->governor_name, governor->name,
876 			     DEVFREQ_NAME_LEN)) {
877 			/* we should have a devfreq governor! */
878 			if (!devfreq->governor) {
879 				dev_warn(dev, "%s: Governor %s NOT present\n",
880 					 __func__, governor->name);
881 				continue;
882 				/* Fall through */
883 			}
884 			ret = devfreq->governor->event_handler(devfreq,
885 						DEVFREQ_GOV_STOP, NULL);
886 			if (ret) {
887 				dev_warn(dev, "%s: Governor %s stop=%d\n",
888 					 __func__, devfreq->governor->name,
889 					 ret);
890 			}
891 			devfreq->governor = NULL;
892 		}
893 	}
894 
895 	list_del(&governor->node);
896 err_out:
897 	mutex_unlock(&devfreq_list_lock);
898 
899 	return err;
900 }
901 EXPORT_SYMBOL(devfreq_remove_governor);
902 
name_show(struct device * dev,struct device_attribute * attr,char * buf)903 static ssize_t name_show(struct device *dev,
904 			struct device_attribute *attr, char *buf)
905 {
906 	struct devfreq *devfreq = to_devfreq(dev);
907 	return sprintf(buf, "%s\n", dev_name(devfreq->dev.parent));
908 }
909 static DEVICE_ATTR_RO(name);
910 
governor_show(struct device * dev,struct device_attribute * attr,char * buf)911 static ssize_t governor_show(struct device *dev,
912 			     struct device_attribute *attr, char *buf)
913 {
914 	if (!to_devfreq(dev)->governor)
915 		return -EINVAL;
916 
917 	return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
918 }
919 
governor_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)920 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
921 			      const char *buf, size_t count)
922 {
923 	struct devfreq *df = to_devfreq(dev);
924 	int ret;
925 	char str_governor[DEVFREQ_NAME_LEN + 1];
926 	struct devfreq_governor *governor;
927 
928 	ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
929 	if (ret != 1)
930 		return -EINVAL;
931 
932 	mutex_lock(&devfreq_list_lock);
933 	governor = find_devfreq_governor(str_governor);
934 	if (IS_ERR(governor)) {
935 		ret = PTR_ERR(governor);
936 		goto out;
937 	}
938 	if (df->governor == governor) {
939 		ret = 0;
940 		goto out;
941 	} else if ((df->governor && df->governor->immutable) ||
942 					governor->immutable) {
943 		ret = -EINVAL;
944 		goto out;
945 	}
946 
947 	if (df->governor) {
948 		ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
949 		if (ret) {
950 			dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
951 				 __func__, df->governor->name, ret);
952 			goto out;
953 		}
954 	}
955 	df->governor = governor;
956 	strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
957 	ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
958 	if (ret)
959 		dev_warn(dev, "%s: Governor %s not started(%d)\n",
960 			 __func__, df->governor->name, ret);
961 out:
962 	mutex_unlock(&devfreq_list_lock);
963 
964 	if (!ret)
965 		ret = count;
966 	return ret;
967 }
968 static DEVICE_ATTR_RW(governor);
969 
available_governors_show(struct device * d,struct device_attribute * attr,char * buf)970 static ssize_t available_governors_show(struct device *d,
971 					struct device_attribute *attr,
972 					char *buf)
973 {
974 	struct devfreq *df = to_devfreq(d);
975 	ssize_t count = 0;
976 
977 	mutex_lock(&devfreq_list_lock);
978 
979 	/*
980 	 * The devfreq with immutable governor (e.g., passive) shows
981 	 * only own governor.
982 	 */
983 	if (df->governor && df->governor->immutable) {
984 		count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
985 				   "%s ", df->governor_name);
986 	/*
987 	 * The devfreq device shows the registered governor except for
988 	 * immutable governors such as passive governor .
989 	 */
990 	} else {
991 		struct devfreq_governor *governor;
992 
993 		list_for_each_entry(governor, &devfreq_governor_list, node) {
994 			if (governor->immutable)
995 				continue;
996 			count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
997 					   "%s ", governor->name);
998 		}
999 	}
1000 
1001 	mutex_unlock(&devfreq_list_lock);
1002 
1003 	/* Truncate the trailing space */
1004 	if (count)
1005 		count--;
1006 
1007 	count += sprintf(&buf[count], "\n");
1008 
1009 	return count;
1010 }
1011 static DEVICE_ATTR_RO(available_governors);
1012 
cur_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1013 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1014 			     char *buf)
1015 {
1016 	unsigned long freq;
1017 	struct devfreq *devfreq = to_devfreq(dev);
1018 
1019 	if (devfreq->profile->get_cur_freq &&
1020 		!devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1021 		return sprintf(buf, "%lu\n", freq);
1022 
1023 	return sprintf(buf, "%lu\n", devfreq->previous_freq);
1024 }
1025 static DEVICE_ATTR_RO(cur_freq);
1026 
target_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1027 static ssize_t target_freq_show(struct device *dev,
1028 				struct device_attribute *attr, char *buf)
1029 {
1030 	return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1031 }
1032 static DEVICE_ATTR_RO(target_freq);
1033 
polling_interval_show(struct device * dev,struct device_attribute * attr,char * buf)1034 static ssize_t polling_interval_show(struct device *dev,
1035 				     struct device_attribute *attr, char *buf)
1036 {
1037 	return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1038 }
1039 
polling_interval_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1040 static ssize_t polling_interval_store(struct device *dev,
1041 				      struct device_attribute *attr,
1042 				      const char *buf, size_t count)
1043 {
1044 	struct devfreq *df = to_devfreq(dev);
1045 	unsigned int value;
1046 	int ret;
1047 
1048 	if (!df->governor)
1049 		return -EINVAL;
1050 
1051 	ret = sscanf(buf, "%u", &value);
1052 	if (ret != 1)
1053 		return -EINVAL;
1054 
1055 	df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1056 	ret = count;
1057 
1058 	return ret;
1059 }
1060 static DEVICE_ATTR_RW(polling_interval);
1061 
min_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1062 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1063 			      const char *buf, size_t count)
1064 {
1065 	struct devfreq *df = to_devfreq(dev);
1066 	unsigned long value;
1067 	int ret;
1068 	unsigned long max;
1069 
1070 	ret = sscanf(buf, "%lu", &value);
1071 	if (ret != 1)
1072 		return -EINVAL;
1073 
1074 	mutex_lock(&df->lock);
1075 	max = df->max_freq;
1076 	if (value && max && value > max) {
1077 		ret = -EINVAL;
1078 		goto unlock;
1079 	}
1080 
1081 	df->min_freq = value;
1082 	update_devfreq(df);
1083 	ret = count;
1084 unlock:
1085 	mutex_unlock(&df->lock);
1086 	return ret;
1087 }
1088 
max_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1089 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1090 			      const char *buf, size_t count)
1091 {
1092 	struct devfreq *df = to_devfreq(dev);
1093 	unsigned long value;
1094 	int ret;
1095 	unsigned long min;
1096 
1097 	ret = sscanf(buf, "%lu", &value);
1098 	if (ret != 1)
1099 		return -EINVAL;
1100 
1101 	mutex_lock(&df->lock);
1102 	min = df->min_freq;
1103 	if (value && min && value < min) {
1104 		ret = -EINVAL;
1105 		goto unlock;
1106 	}
1107 
1108 	df->max_freq = value;
1109 	update_devfreq(df);
1110 	ret = count;
1111 unlock:
1112 	mutex_unlock(&df->lock);
1113 	return ret;
1114 }
1115 
1116 #define show_one(name)						\
1117 static ssize_t name##_show					\
1118 (struct device *dev, struct device_attribute *attr, char *buf)	\
1119 {								\
1120 	return sprintf(buf, "%lu\n", to_devfreq(dev)->name);	\
1121 }
1122 show_one(min_freq);
1123 show_one(max_freq);
1124 
1125 static DEVICE_ATTR_RW(min_freq);
1126 static DEVICE_ATTR_RW(max_freq);
1127 
available_frequencies_show(struct device * d,struct device_attribute * attr,char * buf)1128 static ssize_t available_frequencies_show(struct device *d,
1129 					  struct device_attribute *attr,
1130 					  char *buf)
1131 {
1132 	struct devfreq *df = to_devfreq(d);
1133 	struct device *dev = df->dev.parent;
1134 	struct dev_pm_opp *opp;
1135 	ssize_t count = 0;
1136 	unsigned long freq = 0;
1137 
1138 	do {
1139 		opp = dev_pm_opp_find_freq_ceil(dev, &freq);
1140 		if (IS_ERR(opp))
1141 			break;
1142 
1143 		dev_pm_opp_put(opp);
1144 		count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1145 				   "%lu ", freq);
1146 		freq++;
1147 	} while (1);
1148 
1149 	/* Truncate the trailing space */
1150 	if (count)
1151 		count--;
1152 
1153 	count += sprintf(&buf[count], "\n");
1154 
1155 	return count;
1156 }
1157 static DEVICE_ATTR_RO(available_frequencies);
1158 
trans_stat_show(struct device * dev,struct device_attribute * attr,char * buf)1159 static ssize_t trans_stat_show(struct device *dev,
1160 			       struct device_attribute *attr, char *buf)
1161 {
1162 	struct devfreq *devfreq = to_devfreq(dev);
1163 	ssize_t len;
1164 	int i, j;
1165 	unsigned int max_state = devfreq->profile->max_state;
1166 
1167 	if (max_state == 0)
1168 		return sprintf(buf, "Not Supported.\n");
1169 
1170 	mutex_lock(&devfreq->lock);
1171 	if (!devfreq->stop_polling &&
1172 			devfreq_update_status(devfreq, devfreq->previous_freq)) {
1173 		mutex_unlock(&devfreq->lock);
1174 		return 0;
1175 	}
1176 	mutex_unlock(&devfreq->lock);
1177 
1178 	len = sprintf(buf, "     From  :   To\n");
1179 	len += sprintf(buf + len, "           :");
1180 	for (i = 0; i < max_state; i++)
1181 		len += sprintf(buf + len, "%10lu",
1182 				devfreq->profile->freq_table[i]);
1183 
1184 	len += sprintf(buf + len, "   time(ms)\n");
1185 
1186 	for (i = 0; i < max_state; i++) {
1187 		if (devfreq->profile->freq_table[i]
1188 					== devfreq->previous_freq) {
1189 			len += sprintf(buf + len, "*");
1190 		} else {
1191 			len += sprintf(buf + len, " ");
1192 		}
1193 		len += sprintf(buf + len, "%10lu:",
1194 				devfreq->profile->freq_table[i]);
1195 		for (j = 0; j < max_state; j++)
1196 			len += sprintf(buf + len, "%10u",
1197 				devfreq->trans_table[(i * max_state) + j]);
1198 		len += sprintf(buf + len, "%10u\n",
1199 			jiffies_to_msecs(devfreq->time_in_state[i]));
1200 	}
1201 
1202 	len += sprintf(buf + len, "Total transition : %u\n",
1203 					devfreq->total_trans);
1204 	return len;
1205 }
1206 static DEVICE_ATTR_RO(trans_stat);
1207 
1208 static struct attribute *devfreq_attrs[] = {
1209 	&dev_attr_name.attr,
1210 	&dev_attr_governor.attr,
1211 	&dev_attr_available_governors.attr,
1212 	&dev_attr_cur_freq.attr,
1213 	&dev_attr_available_frequencies.attr,
1214 	&dev_attr_target_freq.attr,
1215 	&dev_attr_polling_interval.attr,
1216 	&dev_attr_min_freq.attr,
1217 	&dev_attr_max_freq.attr,
1218 	&dev_attr_trans_stat.attr,
1219 	NULL,
1220 };
1221 ATTRIBUTE_GROUPS(devfreq);
1222 
devfreq_init(void)1223 static int __init devfreq_init(void)
1224 {
1225 	devfreq_class = class_create(THIS_MODULE, "devfreq");
1226 	if (IS_ERR(devfreq_class)) {
1227 		pr_err("%s: couldn't create class\n", __FILE__);
1228 		return PTR_ERR(devfreq_class);
1229 	}
1230 
1231 	devfreq_wq = create_freezable_workqueue("devfreq_wq");
1232 	if (!devfreq_wq) {
1233 		class_destroy(devfreq_class);
1234 		pr_err("%s: couldn't create workqueue\n", __FILE__);
1235 		return -ENOMEM;
1236 	}
1237 	devfreq_class->dev_groups = devfreq_groups;
1238 
1239 	return 0;
1240 }
1241 subsys_initcall(devfreq_init);
1242 
1243 /*
1244  * The following are helper functions for devfreq user device drivers with
1245  * OPP framework.
1246  */
1247 
1248 /**
1249  * devfreq_recommended_opp() - Helper function to get proper OPP for the
1250  *			     freq value given to target callback.
1251  * @dev:	The devfreq user device. (parent of devfreq)
1252  * @freq:	The frequency given to target function
1253  * @flags:	Flags handed from devfreq framework.
1254  *
1255  * The callers are required to call dev_pm_opp_put() for the returned OPP after
1256  * use.
1257  */
devfreq_recommended_opp(struct device * dev,unsigned long * freq,u32 flags)1258 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1259 					   unsigned long *freq,
1260 					   u32 flags)
1261 {
1262 	struct dev_pm_opp *opp;
1263 
1264 	if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1265 		/* The freq is an upper bound. opp should be lower */
1266 		opp = dev_pm_opp_find_freq_floor(dev, freq);
1267 
1268 		/* If not available, use the closest opp */
1269 		if (opp == ERR_PTR(-ERANGE))
1270 			opp = dev_pm_opp_find_freq_ceil(dev, freq);
1271 	} else {
1272 		/* The freq is an lower bound. opp should be higher */
1273 		opp = dev_pm_opp_find_freq_ceil(dev, freq);
1274 
1275 		/* If not available, use the closest opp */
1276 		if (opp == ERR_PTR(-ERANGE))
1277 			opp = dev_pm_opp_find_freq_floor(dev, freq);
1278 	}
1279 
1280 	return opp;
1281 }
1282 EXPORT_SYMBOL(devfreq_recommended_opp);
1283 
1284 /**
1285  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1286  *				   for any changes in the OPP availability
1287  *				   changes
1288  * @dev:	The devfreq user device. (parent of devfreq)
1289  * @devfreq:	The devfreq object.
1290  */
devfreq_register_opp_notifier(struct device * dev,struct devfreq * devfreq)1291 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1292 {
1293 	return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1294 }
1295 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1296 
1297 /**
1298  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1299  *				     notified for any changes in the OPP
1300  *				     availability changes anymore.
1301  * @dev:	The devfreq user device. (parent of devfreq)
1302  * @devfreq:	The devfreq object.
1303  *
1304  * At exit() callback of devfreq_dev_profile, this must be included if
1305  * devfreq_recommended_opp is used.
1306  */
devfreq_unregister_opp_notifier(struct device * dev,struct devfreq * devfreq)1307 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1308 {
1309 	return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1310 }
1311 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1312 
devm_devfreq_opp_release(struct device * dev,void * res)1313 static void devm_devfreq_opp_release(struct device *dev, void *res)
1314 {
1315 	devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1316 }
1317 
1318 /**
1319  * devm_ devfreq_register_opp_notifier()
1320  *		- Resource-managed devfreq_register_opp_notifier()
1321  * @dev:	The devfreq user device. (parent of devfreq)
1322  * @devfreq:	The devfreq object.
1323  */
devm_devfreq_register_opp_notifier(struct device * dev,struct devfreq * devfreq)1324 int devm_devfreq_register_opp_notifier(struct device *dev,
1325 				       struct devfreq *devfreq)
1326 {
1327 	struct devfreq **ptr;
1328 	int ret;
1329 
1330 	ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1331 	if (!ptr)
1332 		return -ENOMEM;
1333 
1334 	ret = devfreq_register_opp_notifier(dev, devfreq);
1335 	if (ret) {
1336 		devres_free(ptr);
1337 		return ret;
1338 	}
1339 
1340 	*ptr = devfreq;
1341 	devres_add(dev, ptr);
1342 
1343 	return 0;
1344 }
1345 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1346 
1347 /**
1348  * devm_devfreq_unregister_opp_notifier()
1349  *		- Resource-managed devfreq_unregister_opp_notifier()
1350  * @dev:	The devfreq user device. (parent of devfreq)
1351  * @devfreq:	The devfreq object.
1352  */
devm_devfreq_unregister_opp_notifier(struct device * dev,struct devfreq * devfreq)1353 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1354 					 struct devfreq *devfreq)
1355 {
1356 	WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1357 			       devm_devfreq_dev_match, devfreq));
1358 }
1359 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1360 
1361 /**
1362  * devfreq_register_notifier() - Register a driver with devfreq
1363  * @devfreq:	The devfreq object.
1364  * @nb:		The notifier block to register.
1365  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1366  */
devfreq_register_notifier(struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)1367 int devfreq_register_notifier(struct devfreq *devfreq,
1368 				struct notifier_block *nb,
1369 				unsigned int list)
1370 {
1371 	int ret = 0;
1372 
1373 	if (!devfreq)
1374 		return -EINVAL;
1375 
1376 	switch (list) {
1377 	case DEVFREQ_TRANSITION_NOTIFIER:
1378 		ret = srcu_notifier_chain_register(
1379 				&devfreq->transition_notifier_list, nb);
1380 		break;
1381 	default:
1382 		ret = -EINVAL;
1383 	}
1384 
1385 	return ret;
1386 }
1387 EXPORT_SYMBOL(devfreq_register_notifier);
1388 
1389 /*
1390  * devfreq_unregister_notifier() - Unregister a driver with devfreq
1391  * @devfreq:	The devfreq object.
1392  * @nb:		The notifier block to be unregistered.
1393  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1394  */
devfreq_unregister_notifier(struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)1395 int devfreq_unregister_notifier(struct devfreq *devfreq,
1396 				struct notifier_block *nb,
1397 				unsigned int list)
1398 {
1399 	int ret = 0;
1400 
1401 	if (!devfreq)
1402 		return -EINVAL;
1403 
1404 	switch (list) {
1405 	case DEVFREQ_TRANSITION_NOTIFIER:
1406 		ret = srcu_notifier_chain_unregister(
1407 				&devfreq->transition_notifier_list, nb);
1408 		break;
1409 	default:
1410 		ret = -EINVAL;
1411 	}
1412 
1413 	return ret;
1414 }
1415 EXPORT_SYMBOL(devfreq_unregister_notifier);
1416 
1417 struct devfreq_notifier_devres {
1418 	struct devfreq *devfreq;
1419 	struct notifier_block *nb;
1420 	unsigned int list;
1421 };
1422 
devm_devfreq_notifier_release(struct device * dev,void * res)1423 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1424 {
1425 	struct devfreq_notifier_devres *this = res;
1426 
1427 	devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1428 }
1429 
1430 /**
1431  * devm_devfreq_register_notifier()
1432 	- Resource-managed devfreq_register_notifier()
1433  * @dev:	The devfreq user device. (parent of devfreq)
1434  * @devfreq:	The devfreq object.
1435  * @nb:		The notifier block to be unregistered.
1436  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1437  */
devm_devfreq_register_notifier(struct device * dev,struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)1438 int devm_devfreq_register_notifier(struct device *dev,
1439 				struct devfreq *devfreq,
1440 				struct notifier_block *nb,
1441 				unsigned int list)
1442 {
1443 	struct devfreq_notifier_devres *ptr;
1444 	int ret;
1445 
1446 	ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1447 				GFP_KERNEL);
1448 	if (!ptr)
1449 		return -ENOMEM;
1450 
1451 	ret = devfreq_register_notifier(devfreq, nb, list);
1452 	if (ret) {
1453 		devres_free(ptr);
1454 		return ret;
1455 	}
1456 
1457 	ptr->devfreq = devfreq;
1458 	ptr->nb = nb;
1459 	ptr->list = list;
1460 	devres_add(dev, ptr);
1461 
1462 	return 0;
1463 }
1464 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1465 
1466 /**
1467  * devm_devfreq_unregister_notifier()
1468 	- Resource-managed devfreq_unregister_notifier()
1469  * @dev:	The devfreq user device. (parent of devfreq)
1470  * @devfreq:	The devfreq object.
1471  * @nb:		The notifier block to be unregistered.
1472  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1473  */
devm_devfreq_unregister_notifier(struct device * dev,struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)1474 void devm_devfreq_unregister_notifier(struct device *dev,
1475 				struct devfreq *devfreq,
1476 				struct notifier_block *nb,
1477 				unsigned int list)
1478 {
1479 	WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1480 			       devm_devfreq_dev_match, devfreq));
1481 }
1482 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);
1483