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