• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
find_device_devfreq(struct device * dev)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 
find_available_min_freq(struct devfreq * devfreq)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 
find_available_max_freq(struct devfreq * devfreq)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  */
devfreq_get_freq_level(struct devfreq * devfreq,unsigned long freq)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 
set_freq_table(struct devfreq * devfreq)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  */
devfreq_update_status(struct devfreq * devfreq,unsigned long freq)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  */
find_devfreq_governor(const char * name)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  */
try_then_request_governor(const char * name)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 
devfreq_notify_transition(struct devfreq * devfreq,struct devfreq_freqs * freqs,unsigned int state)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 
devfreq_set_target(struct devfreq * devfreq,unsigned long new_freq,u32 flags)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  */
update_devfreq(struct devfreq * devfreq)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  */
devfreq_monitor(struct work_struct * work)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  */
devfreq_monitor_start(struct devfreq * devfreq)411 void devfreq_monitor_start(struct devfreq *devfreq)
412 {
413 	INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
414 	if (devfreq->profile->polling_ms)
415 		queue_delayed_work(devfreq_wq, &devfreq->work,
416 			msecs_to_jiffies(devfreq->profile->polling_ms));
417 }
418 EXPORT_SYMBOL(devfreq_monitor_start);
419 
420 /**
421  * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
422  * @devfreq:	the devfreq instance.
423  *
424  * Helper function to stop devfreq device load monitoring. Function
425  * to be called from governor in response to DEVFREQ_GOV_STOP
426  * event when device is removed from devfreq framework.
427  */
devfreq_monitor_stop(struct devfreq * devfreq)428 void devfreq_monitor_stop(struct devfreq *devfreq)
429 {
430 	cancel_delayed_work_sync(&devfreq->work);
431 }
432 EXPORT_SYMBOL(devfreq_monitor_stop);
433 
434 /**
435  * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
436  * @devfreq:	the devfreq instance.
437  *
438  * Helper function to suspend devfreq device load monitoring. Function
439  * to be called from governor in response to DEVFREQ_GOV_SUSPEND
440  * event or when polling interval is set to zero.
441  *
442  * Note: Though this function is same as devfreq_monitor_stop(),
443  * intentionally kept separate to provide hooks for collecting
444  * transition statistics.
445  */
devfreq_monitor_suspend(struct devfreq * devfreq)446 void devfreq_monitor_suspend(struct devfreq *devfreq)
447 {
448 	mutex_lock(&devfreq->lock);
449 	if (devfreq->stop_polling) {
450 		mutex_unlock(&devfreq->lock);
451 		return;
452 	}
453 
454 	devfreq_update_status(devfreq, devfreq->previous_freq);
455 	devfreq->stop_polling = true;
456 	mutex_unlock(&devfreq->lock);
457 	cancel_delayed_work_sync(&devfreq->work);
458 }
459 EXPORT_SYMBOL(devfreq_monitor_suspend);
460 
461 /**
462  * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
463  * @devfreq:    the devfreq instance.
464  *
465  * Helper function to resume devfreq device load monitoring. Function
466  * to be called from governor in response to DEVFREQ_GOV_RESUME
467  * event or when polling interval is set to non-zero.
468  */
devfreq_monitor_resume(struct devfreq * devfreq)469 void devfreq_monitor_resume(struct devfreq *devfreq)
470 {
471 	unsigned long freq;
472 
473 	mutex_lock(&devfreq->lock);
474 	if (!devfreq->stop_polling)
475 		goto out;
476 
477 	if (!delayed_work_pending(&devfreq->work) &&
478 			devfreq->profile->polling_ms)
479 		queue_delayed_work(devfreq_wq, &devfreq->work,
480 			msecs_to_jiffies(devfreq->profile->polling_ms));
481 
482 	devfreq->last_stat_updated = jiffies;
483 	devfreq->stop_polling = false;
484 
485 	if (devfreq->profile->get_cur_freq &&
486 		!devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
487 		devfreq->previous_freq = freq;
488 
489 out:
490 	mutex_unlock(&devfreq->lock);
491 }
492 EXPORT_SYMBOL(devfreq_monitor_resume);
493 
494 /**
495  * devfreq_interval_update() - Update device devfreq monitoring interval
496  * @devfreq:    the devfreq instance.
497  * @delay:      new polling interval to be set.
498  *
499  * Helper function to set new load monitoring polling interval. Function
500  * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
501  */
devfreq_interval_update(struct devfreq * devfreq,unsigned int * delay)502 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
503 {
504 	unsigned int cur_delay = devfreq->profile->polling_ms;
505 	unsigned int new_delay = *delay;
506 
507 	mutex_lock(&devfreq->lock);
508 	devfreq->profile->polling_ms = new_delay;
509 
510 	if (devfreq->stop_polling)
511 		goto out;
512 
513 	/* if new delay is zero, stop polling */
514 	if (!new_delay) {
515 		mutex_unlock(&devfreq->lock);
516 		cancel_delayed_work_sync(&devfreq->work);
517 		return;
518 	}
519 
520 	/* if current delay is zero, start polling with new delay */
521 	if (!cur_delay) {
522 		queue_delayed_work(devfreq_wq, &devfreq->work,
523 			msecs_to_jiffies(devfreq->profile->polling_ms));
524 		goto out;
525 	}
526 
527 	/* if current delay is greater than new delay, restart polling */
528 	if (cur_delay > new_delay) {
529 		mutex_unlock(&devfreq->lock);
530 		cancel_delayed_work_sync(&devfreq->work);
531 		mutex_lock(&devfreq->lock);
532 		if (!devfreq->stop_polling)
533 			queue_delayed_work(devfreq_wq, &devfreq->work,
534 				msecs_to_jiffies(devfreq->profile->polling_ms));
535 	}
536 out:
537 	mutex_unlock(&devfreq->lock);
538 }
539 EXPORT_SYMBOL(devfreq_interval_update);
540 
541 /**
542  * devfreq_notifier_call() - Notify that the device frequency requirements
543  *			     has been changed out of devfreq framework.
544  * @nb:		the notifier_block (supposed to be devfreq->nb)
545  * @type:	not used
546  * @devp:	not used
547  *
548  * Called by a notifier that uses devfreq->nb.
549  */
devfreq_notifier_call(struct notifier_block * nb,unsigned long type,void * devp)550 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
551 				 void *devp)
552 {
553 	struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
554 	int err = -EINVAL;
555 
556 	mutex_lock(&devfreq->lock);
557 
558 	devfreq->scaling_min_freq = find_available_min_freq(devfreq);
559 	if (!devfreq->scaling_min_freq)
560 		goto out;
561 
562 	devfreq->scaling_max_freq = find_available_max_freq(devfreq);
563 	if (!devfreq->scaling_max_freq) {
564 		devfreq->scaling_max_freq = ULONG_MAX;
565 		goto out;
566 	}
567 
568 	err = update_devfreq(devfreq);
569 
570 out:
571 	mutex_unlock(&devfreq->lock);
572 	if (err)
573 		dev_err(devfreq->dev.parent,
574 			"failed to update frequency from OPP notifier (%d)\n",
575 			err);
576 
577 	return NOTIFY_OK;
578 }
579 
580 /**
581  * devfreq_dev_release() - Callback for struct device to release the device.
582  * @dev:	the devfreq device
583  *
584  * Remove devfreq from the list and release its resources.
585  */
devfreq_dev_release(struct device * dev)586 static void devfreq_dev_release(struct device *dev)
587 {
588 	struct devfreq *devfreq = to_devfreq(dev);
589 
590 	mutex_lock(&devfreq_list_lock);
591 	list_del(&devfreq->node);
592 	mutex_unlock(&devfreq_list_lock);
593 
594 	if (devfreq->profile->exit)
595 		devfreq->profile->exit(devfreq->dev.parent);
596 
597 	mutex_destroy(&devfreq->lock);
598 	kfree(devfreq);
599 }
600 
601 /**
602  * devfreq_add_device() - Add devfreq feature to the device
603  * @dev:	the device to add devfreq feature.
604  * @profile:	device-specific profile to run devfreq.
605  * @governor_name:	name of the policy to choose frequency.
606  * @data:	private data for the governor. The devfreq framework does not
607  *		touch this value.
608  */
devfreq_add_device(struct device * dev,struct devfreq_dev_profile * profile,const char * governor_name,void * data)609 struct devfreq *devfreq_add_device(struct device *dev,
610 				   struct devfreq_dev_profile *profile,
611 				   const char *governor_name,
612 				   void *data)
613 {
614 	struct devfreq *devfreq;
615 	struct devfreq_governor *governor;
616 	static atomic_t devfreq_no = ATOMIC_INIT(-1);
617 	int err = 0;
618 
619 	if (!dev || !profile || !governor_name) {
620 		dev_err(dev, "%s: Invalid parameters.\n", __func__);
621 		return ERR_PTR(-EINVAL);
622 	}
623 
624 	mutex_lock(&devfreq_list_lock);
625 	devfreq = find_device_devfreq(dev);
626 	mutex_unlock(&devfreq_list_lock);
627 	if (!IS_ERR(devfreq)) {
628 		dev_err(dev, "%s: Unable to create devfreq for the device.\n",
629 			__func__);
630 		err = -EINVAL;
631 		goto err_out;
632 	}
633 
634 	devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
635 	if (!devfreq) {
636 		err = -ENOMEM;
637 		goto err_out;
638 	}
639 
640 	mutex_init(&devfreq->lock);
641 	mutex_lock(&devfreq->lock);
642 	devfreq->dev.parent = dev;
643 	devfreq->dev.class = devfreq_class;
644 	devfreq->dev.release = devfreq_dev_release;
645 	INIT_LIST_HEAD(&devfreq->node);
646 	devfreq->profile = profile;
647 	strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
648 	devfreq->previous_freq = profile->initial_freq;
649 	devfreq->last_status.current_frequency = profile->initial_freq;
650 	devfreq->data = data;
651 	devfreq->nb.notifier_call = devfreq_notifier_call;
652 
653 	if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
654 		mutex_unlock(&devfreq->lock);
655 		err = set_freq_table(devfreq);
656 		if (err < 0)
657 			goto err_dev;
658 		mutex_lock(&devfreq->lock);
659 	}
660 
661 	devfreq->scaling_min_freq = find_available_min_freq(devfreq);
662 	if (!devfreq->scaling_min_freq) {
663 		mutex_unlock(&devfreq->lock);
664 		err = -EINVAL;
665 		goto err_dev;
666 	}
667 	devfreq->min_freq = devfreq->scaling_min_freq;
668 
669 	devfreq->scaling_max_freq = find_available_max_freq(devfreq);
670 	if (!devfreq->scaling_max_freq) {
671 		mutex_unlock(&devfreq->lock);
672 		err = -EINVAL;
673 		goto err_dev;
674 	}
675 	devfreq->max_freq = devfreq->scaling_max_freq;
676 
677 	devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
678 	atomic_set(&devfreq->suspend_count, 0);
679 
680 	dev_set_name(&devfreq->dev, "devfreq%d",
681 				atomic_inc_return(&devfreq_no));
682 	err = device_register(&devfreq->dev);
683 	if (err) {
684 		mutex_unlock(&devfreq->lock);
685 		put_device(&devfreq->dev);
686 		goto err_out;
687 	}
688 
689 	devfreq->trans_table = devm_kzalloc(&devfreq->dev,
690 			array3_size(sizeof(unsigned int),
691 				    devfreq->profile->max_state,
692 				    devfreq->profile->max_state),
693 			GFP_KERNEL);
694 	if (!devfreq->trans_table) {
695 		mutex_unlock(&devfreq->lock);
696 		err = -ENOMEM;
697 		goto err_devfreq;
698 	}
699 
700 	devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
701 			devfreq->profile->max_state,
702 			sizeof(unsigned long),
703 			GFP_KERNEL);
704 	if (!devfreq->time_in_state) {
705 		mutex_unlock(&devfreq->lock);
706 		err = -ENOMEM;
707 		goto err_devfreq;
708 	}
709 
710 	devfreq->last_stat_updated = jiffies;
711 
712 	srcu_init_notifier_head(&devfreq->transition_notifier_list);
713 
714 	mutex_unlock(&devfreq->lock);
715 
716 	mutex_lock(&devfreq_list_lock);
717 
718 	governor = try_then_request_governor(devfreq->governor_name);
719 	if (IS_ERR(governor)) {
720 		dev_err(dev, "%s: Unable to find governor for the device\n",
721 			__func__);
722 		err = PTR_ERR(governor);
723 		goto err_init;
724 	}
725 
726 	devfreq->governor = governor;
727 	err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
728 						NULL);
729 	if (err) {
730 		dev_err(dev, "%s: Unable to start governor for the device\n",
731 			__func__);
732 		goto err_init;
733 	}
734 
735 	list_add(&devfreq->node, &devfreq_list);
736 
737 	mutex_unlock(&devfreq_list_lock);
738 
739 	return devfreq;
740 
741 err_init:
742 	mutex_unlock(&devfreq_list_lock);
743 err_devfreq:
744 	devfreq_remove_device(devfreq);
745 	devfreq = NULL;
746 err_dev:
747 	kfree(devfreq);
748 err_out:
749 	return ERR_PTR(err);
750 }
751 EXPORT_SYMBOL(devfreq_add_device);
752 
753 /**
754  * devfreq_remove_device() - Remove devfreq feature from a device.
755  * @devfreq:	the devfreq instance to be removed
756  *
757  * The opposite of devfreq_add_device().
758  */
devfreq_remove_device(struct devfreq * devfreq)759 int devfreq_remove_device(struct devfreq *devfreq)
760 {
761 	if (!devfreq)
762 		return -EINVAL;
763 
764 	if (devfreq->governor)
765 		devfreq->governor->event_handler(devfreq,
766 						 DEVFREQ_GOV_STOP, NULL);
767 	device_unregister(&devfreq->dev);
768 
769 	return 0;
770 }
771 EXPORT_SYMBOL(devfreq_remove_device);
772 
devm_devfreq_dev_match(struct device * dev,void * res,void * data)773 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
774 {
775 	struct devfreq **r = res;
776 
777 	if (WARN_ON(!r || !*r))
778 		return 0;
779 
780 	return *r == data;
781 }
782 
devm_devfreq_dev_release(struct device * dev,void * res)783 static void devm_devfreq_dev_release(struct device *dev, void *res)
784 {
785 	devfreq_remove_device(*(struct devfreq **)res);
786 }
787 
788 /**
789  * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
790  * @dev:	the device to add devfreq feature.
791  * @profile:	device-specific profile to run devfreq.
792  * @governor_name:	name of the policy to choose frequency.
793  * @data:	private data for the governor. The devfreq framework does not
794  *		touch this value.
795  *
796  * This function manages automatically the memory of devfreq device using device
797  * resource management and simplify the free operation for memory of devfreq
798  * device.
799  */
devm_devfreq_add_device(struct device * dev,struct devfreq_dev_profile * profile,const char * governor_name,void * data)800 struct devfreq *devm_devfreq_add_device(struct device *dev,
801 					struct devfreq_dev_profile *profile,
802 					const char *governor_name,
803 					void *data)
804 {
805 	struct devfreq **ptr, *devfreq;
806 
807 	ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
808 	if (!ptr)
809 		return ERR_PTR(-ENOMEM);
810 
811 	devfreq = devfreq_add_device(dev, profile, governor_name, data);
812 	if (IS_ERR(devfreq)) {
813 		devres_free(ptr);
814 		return devfreq;
815 	}
816 
817 	*ptr = devfreq;
818 	devres_add(dev, ptr);
819 
820 	return devfreq;
821 }
822 EXPORT_SYMBOL(devm_devfreq_add_device);
823 
824 #ifdef CONFIG_OF
825 /*
826  * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
827  * @dev - instance to the given device
828  * @index - index into list of devfreq
829  *
830  * return the instance of devfreq device
831  */
devfreq_get_devfreq_by_phandle(struct device * dev,int index)832 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
833 {
834 	struct device_node *node;
835 	struct devfreq *devfreq;
836 
837 	if (!dev)
838 		return ERR_PTR(-EINVAL);
839 
840 	if (!dev->of_node)
841 		return ERR_PTR(-EINVAL);
842 
843 	node = of_parse_phandle(dev->of_node, "devfreq", index);
844 	if (!node)
845 		return ERR_PTR(-ENODEV);
846 
847 	mutex_lock(&devfreq_list_lock);
848 	list_for_each_entry(devfreq, &devfreq_list, node) {
849 		if (devfreq->dev.parent
850 			&& devfreq->dev.parent->of_node == node) {
851 			mutex_unlock(&devfreq_list_lock);
852 			of_node_put(node);
853 			return devfreq;
854 		}
855 	}
856 	mutex_unlock(&devfreq_list_lock);
857 	of_node_put(node);
858 
859 	return ERR_PTR(-EPROBE_DEFER);
860 }
861 #else
devfreq_get_devfreq_by_phandle(struct device * dev,int index)862 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
863 {
864 	return ERR_PTR(-ENODEV);
865 }
866 #endif /* CONFIG_OF */
867 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
868 
869 /**
870  * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
871  * @dev:	the device from which to remove devfreq feature.
872  * @devfreq:	the devfreq instance to be removed
873  */
devm_devfreq_remove_device(struct device * dev,struct devfreq * devfreq)874 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
875 {
876 	WARN_ON(devres_release(dev, devm_devfreq_dev_release,
877 			       devm_devfreq_dev_match, devfreq));
878 }
879 EXPORT_SYMBOL(devm_devfreq_remove_device);
880 
881 /**
882  * devfreq_suspend_device() - Suspend devfreq of a device.
883  * @devfreq: the devfreq instance to be suspended
884  *
885  * This function is intended to be called by the pm callbacks
886  * (e.g., runtime_suspend, suspend) of the device driver that
887  * holds the devfreq.
888  */
devfreq_suspend_device(struct devfreq * devfreq)889 int devfreq_suspend_device(struct devfreq *devfreq)
890 {
891 	int ret;
892 
893 	if (!devfreq)
894 		return -EINVAL;
895 
896 	if (atomic_inc_return(&devfreq->suspend_count) > 1)
897 		return 0;
898 
899 	if (devfreq->governor) {
900 		ret = devfreq->governor->event_handler(devfreq,
901 					DEVFREQ_GOV_SUSPEND, NULL);
902 		if (ret)
903 			return ret;
904 	}
905 
906 	if (devfreq->suspend_freq) {
907 		ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
908 		if (ret)
909 			return ret;
910 	}
911 
912 	return 0;
913 }
914 EXPORT_SYMBOL(devfreq_suspend_device);
915 
916 /**
917  * devfreq_resume_device() - Resume devfreq of a device.
918  * @devfreq: the devfreq instance to be resumed
919  *
920  * This function is intended to be called by the pm callbacks
921  * (e.g., runtime_resume, resume) of the device driver that
922  * holds the devfreq.
923  */
devfreq_resume_device(struct devfreq * devfreq)924 int devfreq_resume_device(struct devfreq *devfreq)
925 {
926 	int ret;
927 
928 	if (!devfreq)
929 		return -EINVAL;
930 
931 	if (atomic_dec_return(&devfreq->suspend_count) >= 1)
932 		return 0;
933 
934 	if (devfreq->resume_freq) {
935 		ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
936 		if (ret)
937 			return ret;
938 	}
939 
940 	if (devfreq->governor) {
941 		ret = devfreq->governor->event_handler(devfreq,
942 					DEVFREQ_GOV_RESUME, NULL);
943 		if (ret)
944 			return ret;
945 	}
946 
947 	return 0;
948 }
949 EXPORT_SYMBOL(devfreq_resume_device);
950 
951 /**
952  * devfreq_suspend() - Suspend devfreq governors and devices
953  *
954  * Called during system wide Suspend/Hibernate cycles for suspending governors
955  * and devices preserving the state for resume. On some platforms the devfreq
956  * device must have precise state (frequency) after resume in order to provide
957  * fully operating setup.
958  */
devfreq_suspend(void)959 void devfreq_suspend(void)
960 {
961 	struct devfreq *devfreq;
962 	int ret;
963 
964 	mutex_lock(&devfreq_list_lock);
965 	list_for_each_entry(devfreq, &devfreq_list, node) {
966 		ret = devfreq_suspend_device(devfreq);
967 		if (ret)
968 			dev_err(&devfreq->dev,
969 				"failed to suspend devfreq device\n");
970 	}
971 	mutex_unlock(&devfreq_list_lock);
972 }
973 
974 /**
975  * devfreq_resume() - Resume devfreq governors and devices
976  *
977  * Called during system wide Suspend/Hibernate cycle for resuming governors and
978  * devices that are suspended with devfreq_suspend().
979  */
devfreq_resume(void)980 void devfreq_resume(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_resume_device(devfreq);
988 		if (ret)
989 			dev_warn(&devfreq->dev,
990 				 "failed to resume devfreq device\n");
991 	}
992 	mutex_unlock(&devfreq_list_lock);
993 }
994 
995 /**
996  * devfreq_add_governor() - Add devfreq governor
997  * @governor:	the devfreq governor to be added
998  */
devfreq_add_governor(struct devfreq_governor * governor)999 int devfreq_add_governor(struct devfreq_governor *governor)
1000 {
1001 	struct devfreq_governor *g;
1002 	struct devfreq *devfreq;
1003 	int err = 0;
1004 
1005 	if (!governor) {
1006 		pr_err("%s: Invalid parameters.\n", __func__);
1007 		return -EINVAL;
1008 	}
1009 
1010 	mutex_lock(&devfreq_list_lock);
1011 	g = find_devfreq_governor(governor->name);
1012 	if (!IS_ERR(g)) {
1013 		pr_err("%s: governor %s already registered\n", __func__,
1014 		       g->name);
1015 		err = -EINVAL;
1016 		goto err_out;
1017 	}
1018 
1019 	list_add(&governor->node, &devfreq_governor_list);
1020 
1021 	list_for_each_entry(devfreq, &devfreq_list, node) {
1022 		int ret = 0;
1023 		struct device *dev = devfreq->dev.parent;
1024 
1025 		if (!strncmp(devfreq->governor_name, governor->name,
1026 			     DEVFREQ_NAME_LEN)) {
1027 			/* The following should never occur */
1028 			if (devfreq->governor) {
1029 				dev_warn(dev,
1030 					 "%s: Governor %s already present\n",
1031 					 __func__, devfreq->governor->name);
1032 				ret = devfreq->governor->event_handler(devfreq,
1033 							DEVFREQ_GOV_STOP, NULL);
1034 				if (ret) {
1035 					dev_warn(dev,
1036 						 "%s: Governor %s stop = %d\n",
1037 						 __func__,
1038 						 devfreq->governor->name, ret);
1039 				}
1040 				/* Fall through */
1041 			}
1042 			devfreq->governor = governor;
1043 			ret = devfreq->governor->event_handler(devfreq,
1044 						DEVFREQ_GOV_START, NULL);
1045 			if (ret) {
1046 				dev_warn(dev, "%s: Governor %s start=%d\n",
1047 					 __func__, devfreq->governor->name,
1048 					 ret);
1049 			}
1050 		}
1051 	}
1052 
1053 err_out:
1054 	mutex_unlock(&devfreq_list_lock);
1055 
1056 	return err;
1057 }
1058 EXPORT_SYMBOL(devfreq_add_governor);
1059 
1060 /**
1061  * devfreq_remove_governor() - Remove devfreq feature from a device.
1062  * @governor:	the devfreq governor to be removed
1063  */
devfreq_remove_governor(struct devfreq_governor * governor)1064 int devfreq_remove_governor(struct devfreq_governor *governor)
1065 {
1066 	struct devfreq_governor *g;
1067 	struct devfreq *devfreq;
1068 	int err = 0;
1069 
1070 	if (!governor) {
1071 		pr_err("%s: Invalid parameters.\n", __func__);
1072 		return -EINVAL;
1073 	}
1074 
1075 	mutex_lock(&devfreq_list_lock);
1076 	g = find_devfreq_governor(governor->name);
1077 	if (IS_ERR(g)) {
1078 		pr_err("%s: governor %s not registered\n", __func__,
1079 		       governor->name);
1080 		err = PTR_ERR(g);
1081 		goto err_out;
1082 	}
1083 	list_for_each_entry(devfreq, &devfreq_list, node) {
1084 		int ret;
1085 		struct device *dev = devfreq->dev.parent;
1086 
1087 		if (!strncmp(devfreq->governor_name, governor->name,
1088 			     DEVFREQ_NAME_LEN)) {
1089 			/* we should have a devfreq governor! */
1090 			if (!devfreq->governor) {
1091 				dev_warn(dev, "%s: Governor %s NOT present\n",
1092 					 __func__, governor->name);
1093 				continue;
1094 				/* Fall through */
1095 			}
1096 			ret = devfreq->governor->event_handler(devfreq,
1097 						DEVFREQ_GOV_STOP, NULL);
1098 			if (ret) {
1099 				dev_warn(dev, "%s: Governor %s stop=%d\n",
1100 					 __func__, devfreq->governor->name,
1101 					 ret);
1102 			}
1103 			devfreq->governor = NULL;
1104 		}
1105 	}
1106 
1107 	list_del(&governor->node);
1108 err_out:
1109 	mutex_unlock(&devfreq_list_lock);
1110 
1111 	return err;
1112 }
1113 EXPORT_SYMBOL(devfreq_remove_governor);
1114 
governor_show(struct device * dev,struct device_attribute * attr,char * buf)1115 static ssize_t governor_show(struct device *dev,
1116 			     struct device_attribute *attr, char *buf)
1117 {
1118 	if (!to_devfreq(dev)->governor)
1119 		return -EINVAL;
1120 
1121 	return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1122 }
1123 
governor_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1124 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1125 			      const char *buf, size_t count)
1126 {
1127 	struct devfreq *df = to_devfreq(dev);
1128 	int ret;
1129 	char str_governor[DEVFREQ_NAME_LEN + 1];
1130 	const struct devfreq_governor *governor, *prev_governor;
1131 
1132 	ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1133 	if (ret != 1)
1134 		return -EINVAL;
1135 
1136 	mutex_lock(&devfreq_list_lock);
1137 	governor = try_then_request_governor(str_governor);
1138 	if (IS_ERR(governor)) {
1139 		ret = PTR_ERR(governor);
1140 		goto out;
1141 	}
1142 	if (df->governor == governor) {
1143 		ret = 0;
1144 		goto out;
1145 	} else if ((df->governor && df->governor->immutable) ||
1146 					governor->immutable) {
1147 		ret = -EINVAL;
1148 		goto out;
1149 	}
1150 
1151 	if (df->governor) {
1152 		ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1153 		if (ret) {
1154 			dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1155 				 __func__, df->governor->name, ret);
1156 			goto out;
1157 		}
1158 	}
1159 	prev_governor = df->governor;
1160 	df->governor = governor;
1161 	strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1162 	ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1163 	if (ret) {
1164 		dev_warn(dev, "%s: Governor %s not started(%d)\n",
1165 			 __func__, df->governor->name, ret);
1166 		df->governor = prev_governor;
1167 		strncpy(df->governor_name, prev_governor->name,
1168 			DEVFREQ_NAME_LEN);
1169 		ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1170 		if (ret) {
1171 			dev_err(dev,
1172 				"%s: reverting to Governor %s failed (%d)\n",
1173 				__func__, df->governor_name, ret);
1174 			df->governor = NULL;
1175 		}
1176 	}
1177 out:
1178 	mutex_unlock(&devfreq_list_lock);
1179 
1180 	if (!ret)
1181 		ret = count;
1182 	return ret;
1183 }
1184 static DEVICE_ATTR_RW(governor);
1185 
available_governors_show(struct device * d,struct device_attribute * attr,char * buf)1186 static ssize_t available_governors_show(struct device *d,
1187 					struct device_attribute *attr,
1188 					char *buf)
1189 {
1190 	struct devfreq *df = to_devfreq(d);
1191 	ssize_t count = 0;
1192 
1193 	mutex_lock(&devfreq_list_lock);
1194 
1195 	/*
1196 	 * The devfreq with immutable governor (e.g., passive) shows
1197 	 * only own governor.
1198 	 */
1199 	if (df->governor && df->governor->immutable) {
1200 		count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1201 				  "%s ", df->governor_name);
1202 	/*
1203 	 * The devfreq device shows the registered governor except for
1204 	 * immutable governors such as passive governor .
1205 	 */
1206 	} else {
1207 		struct devfreq_governor *governor;
1208 
1209 		list_for_each_entry(governor, &devfreq_governor_list, node) {
1210 			if (governor->immutable)
1211 				continue;
1212 			count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1213 					   "%s ", governor->name);
1214 		}
1215 	}
1216 
1217 	mutex_unlock(&devfreq_list_lock);
1218 
1219 	/* Truncate the trailing space */
1220 	if (count)
1221 		count--;
1222 
1223 	count += sprintf(&buf[count], "\n");
1224 
1225 	return count;
1226 }
1227 static DEVICE_ATTR_RO(available_governors);
1228 
cur_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1229 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1230 			     char *buf)
1231 {
1232 	unsigned long freq;
1233 	struct devfreq *devfreq = to_devfreq(dev);
1234 
1235 	if (devfreq->profile->get_cur_freq &&
1236 		!devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1237 		return sprintf(buf, "%lu\n", freq);
1238 
1239 	return sprintf(buf, "%lu\n", devfreq->previous_freq);
1240 }
1241 static DEVICE_ATTR_RO(cur_freq);
1242 
target_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1243 static ssize_t target_freq_show(struct device *dev,
1244 				struct device_attribute *attr, char *buf)
1245 {
1246 	return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1247 }
1248 static DEVICE_ATTR_RO(target_freq);
1249 
polling_interval_show(struct device * dev,struct device_attribute * attr,char * buf)1250 static ssize_t polling_interval_show(struct device *dev,
1251 				     struct device_attribute *attr, char *buf)
1252 {
1253 	return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1254 }
1255 
polling_interval_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1256 static ssize_t polling_interval_store(struct device *dev,
1257 				      struct device_attribute *attr,
1258 				      const char *buf, size_t count)
1259 {
1260 	struct devfreq *df = to_devfreq(dev);
1261 	unsigned int value;
1262 	int ret;
1263 
1264 	if (!df->governor)
1265 		return -EINVAL;
1266 
1267 	ret = sscanf(buf, "%u", &value);
1268 	if (ret != 1)
1269 		return -EINVAL;
1270 
1271 	df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1272 	ret = count;
1273 
1274 	return ret;
1275 }
1276 static DEVICE_ATTR_RW(polling_interval);
1277 
min_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1278 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1279 			      const char *buf, size_t count)
1280 {
1281 	struct devfreq *df = to_devfreq(dev);
1282 	unsigned long value;
1283 	int ret;
1284 
1285 	ret = sscanf(buf, "%lu", &value);
1286 	if (ret != 1)
1287 		return -EINVAL;
1288 
1289 	mutex_lock(&df->lock);
1290 
1291 	if (value) {
1292 		if (value > df->max_freq) {
1293 			ret = -EINVAL;
1294 			goto unlock;
1295 		}
1296 	} else {
1297 		unsigned long *freq_table = df->profile->freq_table;
1298 
1299 		/* Get minimum frequency according to sorting order */
1300 		if (freq_table[0] < freq_table[df->profile->max_state - 1])
1301 			value = freq_table[0];
1302 		else
1303 			value = freq_table[df->profile->max_state - 1];
1304 	}
1305 
1306 	df->min_freq = value;
1307 	update_devfreq(df);
1308 	ret = count;
1309 unlock:
1310 	mutex_unlock(&df->lock);
1311 	return ret;
1312 }
1313 
min_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1314 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1315 			     char *buf)
1316 {
1317 	struct devfreq *df = to_devfreq(dev);
1318 
1319 	return sprintf(buf, "%lu\n", max(df->scaling_min_freq, df->min_freq));
1320 }
1321 
max_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1322 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1323 			      const char *buf, size_t count)
1324 {
1325 	struct devfreq *df = to_devfreq(dev);
1326 	unsigned long value;
1327 	int ret;
1328 
1329 	ret = sscanf(buf, "%lu", &value);
1330 	if (ret != 1)
1331 		return -EINVAL;
1332 
1333 	mutex_lock(&df->lock);
1334 
1335 	if (value) {
1336 		if (value < df->min_freq) {
1337 			ret = -EINVAL;
1338 			goto unlock;
1339 		}
1340 	} else {
1341 		unsigned long *freq_table = df->profile->freq_table;
1342 
1343 		/* Get maximum frequency according to sorting order */
1344 		if (freq_table[0] < freq_table[df->profile->max_state - 1])
1345 			value = freq_table[df->profile->max_state - 1];
1346 		else
1347 			value = freq_table[0];
1348 	}
1349 
1350 	df->max_freq = value;
1351 	update_devfreq(df);
1352 	ret = count;
1353 unlock:
1354 	mutex_unlock(&df->lock);
1355 	return ret;
1356 }
1357 static DEVICE_ATTR_RW(min_freq);
1358 
max_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1359 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1360 			     char *buf)
1361 {
1362 	struct devfreq *df = to_devfreq(dev);
1363 
1364 	return sprintf(buf, "%lu\n", min(df->scaling_max_freq, df->max_freq));
1365 }
1366 static DEVICE_ATTR_RW(max_freq);
1367 
available_frequencies_show(struct device * d,struct device_attribute * attr,char * buf)1368 static ssize_t available_frequencies_show(struct device *d,
1369 					  struct device_attribute *attr,
1370 					  char *buf)
1371 {
1372 	struct devfreq *df = to_devfreq(d);
1373 	ssize_t count = 0;
1374 	int i;
1375 
1376 	mutex_lock(&df->lock);
1377 
1378 	for (i = 0; i < df->profile->max_state; i++)
1379 		count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1380 				"%lu ", df->profile->freq_table[i]);
1381 
1382 	mutex_unlock(&df->lock);
1383 	/* Truncate the trailing space */
1384 	if (count)
1385 		count--;
1386 
1387 	count += sprintf(&buf[count], "\n");
1388 
1389 	return count;
1390 }
1391 static DEVICE_ATTR_RO(available_frequencies);
1392 
trans_stat_show(struct device * dev,struct device_attribute * attr,char * buf)1393 static ssize_t trans_stat_show(struct device *dev,
1394 			       struct device_attribute *attr, char *buf)
1395 {
1396 	struct devfreq *devfreq = to_devfreq(dev);
1397 	ssize_t len;
1398 	int i, j;
1399 	unsigned int max_state = devfreq->profile->max_state;
1400 
1401 	if (max_state == 0)
1402 		return sprintf(buf, "Not Supported.\n");
1403 
1404 	mutex_lock(&devfreq->lock);
1405 	if (!devfreq->stop_polling &&
1406 			devfreq_update_status(devfreq, devfreq->previous_freq)) {
1407 		mutex_unlock(&devfreq->lock);
1408 		return 0;
1409 	}
1410 	mutex_unlock(&devfreq->lock);
1411 
1412 	len = sprintf(buf, "     From  :   To\n");
1413 	len += sprintf(buf + len, "           :");
1414 	for (i = 0; i < max_state; i++)
1415 		len += sprintf(buf + len, "%10lu",
1416 				devfreq->profile->freq_table[i]);
1417 
1418 	len += sprintf(buf + len, "   time(ms)\n");
1419 
1420 	for (i = 0; i < max_state; i++) {
1421 		if (devfreq->profile->freq_table[i]
1422 					== devfreq->previous_freq) {
1423 			len += sprintf(buf + len, "*");
1424 		} else {
1425 			len += sprintf(buf + len, " ");
1426 		}
1427 		len += sprintf(buf + len, "%10lu:",
1428 				devfreq->profile->freq_table[i]);
1429 		for (j = 0; j < max_state; j++)
1430 			len += sprintf(buf + len, "%10u",
1431 				devfreq->trans_table[(i * max_state) + j]);
1432 		len += sprintf(buf + len, "%10u\n",
1433 			jiffies_to_msecs(devfreq->time_in_state[i]));
1434 	}
1435 
1436 	len += sprintf(buf + len, "Total transition : %u\n",
1437 					devfreq->total_trans);
1438 	return len;
1439 }
1440 static DEVICE_ATTR_RO(trans_stat);
1441 
1442 static struct attribute *devfreq_attrs[] = {
1443 	&dev_attr_governor.attr,
1444 	&dev_attr_available_governors.attr,
1445 	&dev_attr_cur_freq.attr,
1446 	&dev_attr_available_frequencies.attr,
1447 	&dev_attr_target_freq.attr,
1448 	&dev_attr_polling_interval.attr,
1449 	&dev_attr_min_freq.attr,
1450 	&dev_attr_max_freq.attr,
1451 	&dev_attr_trans_stat.attr,
1452 	NULL,
1453 };
1454 ATTRIBUTE_GROUPS(devfreq);
1455 
devfreq_init(void)1456 static int __init devfreq_init(void)
1457 {
1458 	devfreq_class = class_create(THIS_MODULE, "devfreq");
1459 	if (IS_ERR(devfreq_class)) {
1460 		pr_err("%s: couldn't create class\n", __FILE__);
1461 		return PTR_ERR(devfreq_class);
1462 	}
1463 
1464 	devfreq_wq = create_freezable_workqueue("devfreq_wq");
1465 	if (!devfreq_wq) {
1466 		class_destroy(devfreq_class);
1467 		pr_err("%s: couldn't create workqueue\n", __FILE__);
1468 		return -ENOMEM;
1469 	}
1470 	devfreq_class->dev_groups = devfreq_groups;
1471 
1472 	return 0;
1473 }
1474 subsys_initcall(devfreq_init);
1475 
1476 /*
1477  * The following are helper functions for devfreq user device drivers with
1478  * OPP framework.
1479  */
1480 
1481 /**
1482  * devfreq_recommended_opp() - Helper function to get proper OPP for the
1483  *			     freq value given to target callback.
1484  * @dev:	The devfreq user device. (parent of devfreq)
1485  * @freq:	The frequency given to target function
1486  * @flags:	Flags handed from devfreq framework.
1487  *
1488  * The callers are required to call dev_pm_opp_put() for the returned OPP after
1489  * use.
1490  */
devfreq_recommended_opp(struct device * dev,unsigned long * freq,u32 flags)1491 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1492 					   unsigned long *freq,
1493 					   u32 flags)
1494 {
1495 	struct dev_pm_opp *opp;
1496 
1497 	if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1498 		/* The freq is an upper bound. opp should be lower */
1499 		opp = dev_pm_opp_find_freq_floor(dev, freq);
1500 
1501 		/* If not available, use the closest opp */
1502 		if (opp == ERR_PTR(-ERANGE))
1503 			opp = dev_pm_opp_find_freq_ceil(dev, freq);
1504 	} else {
1505 		/* The freq is an lower bound. opp should be higher */
1506 		opp = dev_pm_opp_find_freq_ceil(dev, freq);
1507 
1508 		/* If not available, use the closest opp */
1509 		if (opp == ERR_PTR(-ERANGE))
1510 			opp = dev_pm_opp_find_freq_floor(dev, freq);
1511 	}
1512 
1513 	return opp;
1514 }
1515 EXPORT_SYMBOL(devfreq_recommended_opp);
1516 
1517 /**
1518  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1519  *				     for any changes in the OPP availability
1520  *				     changes
1521  * @dev:	The devfreq user device. (parent of devfreq)
1522  * @devfreq:	The devfreq object.
1523  */
devfreq_register_opp_notifier(struct device * dev,struct devfreq * devfreq)1524 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1525 {
1526 	return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1527 }
1528 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1529 
1530 /**
1531  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1532  *				       notified for any changes in the OPP
1533  *				       availability changes anymore.
1534  * @dev:	The devfreq user device. (parent of devfreq)
1535  * @devfreq:	The devfreq object.
1536  *
1537  * At exit() callback of devfreq_dev_profile, this must be included if
1538  * devfreq_recommended_opp is used.
1539  */
devfreq_unregister_opp_notifier(struct device * dev,struct devfreq * devfreq)1540 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1541 {
1542 	return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1543 }
1544 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1545 
devm_devfreq_opp_release(struct device * dev,void * res)1546 static void devm_devfreq_opp_release(struct device *dev, void *res)
1547 {
1548 	devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1549 }
1550 
1551 /**
1552  * devm_devfreq_register_opp_notifier() - Resource-managed
1553  *					  devfreq_register_opp_notifier()
1554  * @dev:	The devfreq user device. (parent of devfreq)
1555  * @devfreq:	The devfreq object.
1556  */
devm_devfreq_register_opp_notifier(struct device * dev,struct devfreq * devfreq)1557 int devm_devfreq_register_opp_notifier(struct device *dev,
1558 				       struct devfreq *devfreq)
1559 {
1560 	struct devfreq **ptr;
1561 	int ret;
1562 
1563 	ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1564 	if (!ptr)
1565 		return -ENOMEM;
1566 
1567 	ret = devfreq_register_opp_notifier(dev, devfreq);
1568 	if (ret) {
1569 		devres_free(ptr);
1570 		return ret;
1571 	}
1572 
1573 	*ptr = devfreq;
1574 	devres_add(dev, ptr);
1575 
1576 	return 0;
1577 }
1578 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1579 
1580 /**
1581  * devm_devfreq_unregister_opp_notifier() - Resource-managed
1582  *					    devfreq_unregister_opp_notifier()
1583  * @dev:	The devfreq user device. (parent of devfreq)
1584  * @devfreq:	The devfreq object.
1585  */
devm_devfreq_unregister_opp_notifier(struct device * dev,struct devfreq * devfreq)1586 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1587 					 struct devfreq *devfreq)
1588 {
1589 	WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1590 			       devm_devfreq_dev_match, devfreq));
1591 }
1592 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1593 
1594 /**
1595  * devfreq_register_notifier() - Register a driver with devfreq
1596  * @devfreq:	The devfreq object.
1597  * @nb:		The notifier block to register.
1598  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1599  */
devfreq_register_notifier(struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)1600 int devfreq_register_notifier(struct devfreq *devfreq,
1601 			      struct notifier_block *nb,
1602 			      unsigned int list)
1603 {
1604 	int ret = 0;
1605 
1606 	if (!devfreq)
1607 		return -EINVAL;
1608 
1609 	switch (list) {
1610 	case DEVFREQ_TRANSITION_NOTIFIER:
1611 		ret = srcu_notifier_chain_register(
1612 				&devfreq->transition_notifier_list, nb);
1613 		break;
1614 	default:
1615 		ret = -EINVAL;
1616 	}
1617 
1618 	return ret;
1619 }
1620 EXPORT_SYMBOL(devfreq_register_notifier);
1621 
1622 /*
1623  * devfreq_unregister_notifier() - Unregister a driver with devfreq
1624  * @devfreq:	The devfreq object.
1625  * @nb:		The notifier block to be unregistered.
1626  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1627  */
devfreq_unregister_notifier(struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)1628 int devfreq_unregister_notifier(struct devfreq *devfreq,
1629 				struct notifier_block *nb,
1630 				unsigned int list)
1631 {
1632 	int ret = 0;
1633 
1634 	if (!devfreq)
1635 		return -EINVAL;
1636 
1637 	switch (list) {
1638 	case DEVFREQ_TRANSITION_NOTIFIER:
1639 		ret = srcu_notifier_chain_unregister(
1640 				&devfreq->transition_notifier_list, nb);
1641 		break;
1642 	default:
1643 		ret = -EINVAL;
1644 	}
1645 
1646 	return ret;
1647 }
1648 EXPORT_SYMBOL(devfreq_unregister_notifier);
1649 
1650 struct devfreq_notifier_devres {
1651 	struct devfreq *devfreq;
1652 	struct notifier_block *nb;
1653 	unsigned int list;
1654 };
1655 
devm_devfreq_notifier_release(struct device * dev,void * res)1656 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1657 {
1658 	struct devfreq_notifier_devres *this = res;
1659 
1660 	devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1661 }
1662 
1663 /**
1664  * devm_devfreq_register_notifier()
1665 	- Resource-managed devfreq_register_notifier()
1666  * @dev:	The devfreq user device. (parent of devfreq)
1667  * @devfreq:	The devfreq object.
1668  * @nb:		The notifier block to be unregistered.
1669  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1670  */
devm_devfreq_register_notifier(struct device * dev,struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)1671 int devm_devfreq_register_notifier(struct device *dev,
1672 				struct devfreq *devfreq,
1673 				struct notifier_block *nb,
1674 				unsigned int list)
1675 {
1676 	struct devfreq_notifier_devres *ptr;
1677 	int ret;
1678 
1679 	ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1680 				GFP_KERNEL);
1681 	if (!ptr)
1682 		return -ENOMEM;
1683 
1684 	ret = devfreq_register_notifier(devfreq, nb, list);
1685 	if (ret) {
1686 		devres_free(ptr);
1687 		return ret;
1688 	}
1689 
1690 	ptr->devfreq = devfreq;
1691 	ptr->nb = nb;
1692 	ptr->list = list;
1693 	devres_add(dev, ptr);
1694 
1695 	return 0;
1696 }
1697 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1698 
1699 /**
1700  * devm_devfreq_unregister_notifier()
1701 	- Resource-managed devfreq_unregister_notifier()
1702  * @dev:	The devfreq user device. (parent of devfreq)
1703  * @devfreq:	The devfreq object.
1704  * @nb:		The notifier block to be unregistered.
1705  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1706  */
devm_devfreq_unregister_notifier(struct device * dev,struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)1707 void devm_devfreq_unregister_notifier(struct device *dev,
1708 				      struct devfreq *devfreq,
1709 				      struct notifier_block *nb,
1710 				      unsigned int list)
1711 {
1712 	WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1713 			       devm_devfreq_dev_match, devfreq));
1714 }
1715 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);
1716