blob: a9fd61bbacf170ad98b352c9ec79a9832835ecb8 [file] [log] [blame]
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001/*
2 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
3 * for Non-CPU Devices.
4 *
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/kernel.h>
Enric Balletbo i Serra23c7b542018-07-04 10:45:50 +020014#include <linux/kmod.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020015#include <linux/sched.h>
16#include <linux/errno.h>
17#include <linux/err.h>
18#include <linux/init.h>
Paul Gortmaker417dc4b2016-06-26 03:43:47 +090019#include <linux/export.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020020#include <linux/slab.h>
MyungJoo Ham952f6d12011-11-10 10:16:23 +010021#include <linux/stat.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050022#include <linux/pm_opp.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020023#include <linux/devfreq.h>
24#include <linux/workqueue.h>
25#include <linux/platform_device.h>
26#include <linux/list.h>
27#include <linux/printk.h>
28#include <linux/hrtimer.h>
Chanwoo Choi8f510ae2015-11-10 20:31:07 +090029#include <linux/of.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020030#include "governor.h"
31
Nishanth Menon1a1357e2012-10-26 01:50:53 +020032static struct class *devfreq_class;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020033
34/*
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020035 * devfreq core provides delayed work based load monitoring helper
36 * functions. Governors can use these or can implement their own
37 * monitoring mechanism.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020038 */
MyungJoo Hama3c98b82011-10-02 00:19:15 +020039static struct workqueue_struct *devfreq_wq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020040
Nishanth Menon3aa173b2012-10-29 15:01:43 -050041/* The list of all device-devfreq governors */
42static LIST_HEAD(devfreq_governor_list);
MyungJoo Hama3c98b82011-10-02 00:19:15 +020043/* The list of all device-devfreq */
44static LIST_HEAD(devfreq_list);
45static 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 */
54static struct devfreq *find_device_devfreq(struct device *dev)
55{
56 struct devfreq *tmp_devfreq;
57
Viresh Kumar9348da22015-08-10 11:42:25 +053058 if (IS_ERR_OR_NULL(dev)) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +020059 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
Chanwoo Choiab8f58a2017-10-23 10:32:06 +090073static 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
87static 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
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200101/**
Jonghwa Leee552bba2012-08-23 20:00:46 +0900102 * devfreq_get_freq_level() - Lookup freq_table for the frequency
103 * @devfreq: the devfreq instance
104 * @freq: the target frequency
105 */
106static 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
Chanwoo Choiea572f82017-10-23 10:32:09 +0900117static int set_freq_table(struct devfreq *devfreq)
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900118{
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)
Chanwoo Choiea572f82017-10-23 10:32:09 +0900127 return -EINVAL;
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900128
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;
Chanwoo Choiea572f82017-10-23 10:32:09 +0900136 return -ENOMEM;
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900137 }
138
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900139 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;
Chanwoo Choiea572f82017-10-23 10:32:09 +0900144 return PTR_ERR(opp);
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900145 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530146 dev_pm_opp_put(opp);
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900147 profile->freq_table[i] = freq;
148 }
Chanwoo Choiea572f82017-10-23 10:32:09 +0900149
150 return 0;
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900151}
152
153/**
Jonghwa Leee552bba2012-08-23 20:00:46 +0900154 * devfreq_update_status() - Update statistics of devfreq behavior
155 * @devfreq: the devfreq instance
156 * @freq: the update target frequency
157 */
Chanwoo Choi30582c22017-01-31 15:38:17 +0900158int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
Jonghwa Leee552bba2012-08-23 20:00:46 +0900159{
Saravana Kannane35d35a2014-02-27 19:38:57 -0800160 int lev, prev_lev, ret = 0;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900161 unsigned long cur_time;
162
Jonghwa Leee552bba2012-08-23 20:00:46 +0900163 cur_time = jiffies;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800164
Tobias Jakobid0563a02016-09-29 14:36:36 +0200165 /* Immediately exit if previous_freq is not initialized yet. */
166 if (!devfreq->previous_freq)
167 goto out;
168
Saravana Kannane35d35a2014-02-27 19:38:57 -0800169 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
170 if (prev_lev < 0) {
171 ret = prev_lev;
172 goto out;
173 }
174
175 devfreq->time_in_state[prev_lev] +=
Jonghwa Leee552bba2012-08-23 20:00:46 +0900176 cur_time - devfreq->last_stat_updated;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800177
178 lev = devfreq_get_freq_level(devfreq, freq);
179 if (lev < 0) {
180 ret = lev;
181 goto out;
182 }
183
184 if (lev != prev_lev) {
Jonghwa Leee552bba2012-08-23 20:00:46 +0900185 devfreq->trans_table[(prev_lev *
186 devfreq->profile->max_state) + lev]++;
187 devfreq->total_trans++;
188 }
Jonghwa Leee552bba2012-08-23 20:00:46 +0900189
Saravana Kannane35d35a2014-02-27 19:38:57 -0800190out:
191 devfreq->last_stat_updated = cur_time;
192 return ret;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900193}
Chanwoo Choi30582c22017-01-31 15:38:17 +0900194EXPORT_SYMBOL(devfreq_update_status);
Jonghwa Leee552bba2012-08-23 20:00:46 +0900195
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500196/**
197 * find_devfreq_governor() - find devfreq governor from name
198 * @name: name of the governor
199 *
200 * Search the list of devfreq governors and return the matched
201 * governor's pointer. devfreq_list_lock should be held by the caller.
202 */
203static struct devfreq_governor *find_devfreq_governor(const char *name)
204{
205 struct devfreq_governor *tmp_governor;
206
Viresh Kumar9348da22015-08-10 11:42:25 +0530207 if (IS_ERR_OR_NULL(name)) {
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500208 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
209 return ERR_PTR(-EINVAL);
210 }
211 WARN(!mutex_is_locked(&devfreq_list_lock),
212 "devfreq_list_lock must be locked.");
213
214 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
215 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
216 return tmp_governor;
217 }
218
219 return ERR_PTR(-ENODEV);
220}
221
Enric Balletbo i Serra23c7b542018-07-04 10:45:50 +0200222/**
223 * try_then_request_governor() - Try to find the governor and request the
224 * module if is not found.
225 * @name: name of the governor
226 *
227 * Search the list of devfreq governors and request the module and try again
228 * if is not found. This can happen when both drivers (the governor driver
229 * and the driver that call devfreq_add_device) are built as modules.
230 * devfreq_list_lock should be held by the caller. Returns the matched
231 * governor's pointer.
232 */
233static struct devfreq_governor *try_then_request_governor(const char *name)
234{
235 struct devfreq_governor *governor;
236 int err = 0;
237
238 if (IS_ERR_OR_NULL(name)) {
239 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
240 return ERR_PTR(-EINVAL);
241 }
242 WARN(!mutex_is_locked(&devfreq_list_lock),
243 "devfreq_list_lock must be locked.");
244
245 governor = find_devfreq_governor(name);
246 if (IS_ERR(governor)) {
247 mutex_unlock(&devfreq_list_lock);
248
249 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
250 DEVFREQ_NAME_LEN))
251 err = request_module("governor_%s", "simpleondemand");
252 else
253 err = request_module("governor_%s", name);
254 /* Restore previous state before return */
255 mutex_lock(&devfreq_list_lock);
256 if (err)
257 return NULL;
258
259 governor = find_devfreq_governor(name);
260 }
261
262 return governor;
263}
264
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900265static int devfreq_notify_transition(struct devfreq *devfreq,
266 struct devfreq_freqs *freqs, unsigned int state)
267{
268 if (!devfreq)
269 return -EINVAL;
270
271 switch (state) {
272 case DEVFREQ_PRECHANGE:
273 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
274 DEVFREQ_PRECHANGE, freqs);
275 break;
276
277 case DEVFREQ_POSTCHANGE:
278 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
279 DEVFREQ_POSTCHANGE, freqs);
280 break;
281 default:
282 return -EINVAL;
283 }
284
285 return 0;
286}
287
Lukasz Luba63314172018-12-05 12:05:52 +0100288static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
289 u32 flags)
290{
291 struct devfreq_freqs freqs;
292 unsigned long cur_freq;
293 int err = 0;
294
295 if (devfreq->profile->get_cur_freq)
296 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
297 else
298 cur_freq = devfreq->previous_freq;
299
300 freqs.old = cur_freq;
301 freqs.new = new_freq;
302 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
303
304 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
305 if (err) {
306 freqs.new = cur_freq;
307 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
308 return err;
309 }
310
311 freqs.new = new_freq;
312 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
313
314 if (devfreq_update_status(devfreq, new_freq))
315 dev_err(&devfreq->dev,
316 "Couldn't update frequency transition information.\n");
317
318 devfreq->previous_freq = new_freq;
319 return err;
320}
321
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200322/* Load monitoring helper functions for governors use */
323
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200324/**
325 * update_devfreq() - Reevaluate the device and configure frequency.
326 * @devfreq: the devfreq instance.
327 *
328 * Note: Lock devfreq->lock before calling update_devfreq
329 * This function is exported for governors.
330 */
331int update_devfreq(struct devfreq *devfreq)
332{
Lukasz Luba63314172018-12-05 12:05:52 +0100333 unsigned long freq, min_freq, max_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200334 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100335 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200336
337 if (!mutex_is_locked(&devfreq->lock)) {
338 WARN(true, "devfreq->lock must be locked by the caller.\n");
339 return -EINVAL;
340 }
341
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500342 if (!devfreq->governor)
343 return -EINVAL;
344
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200345 /* Reevaluate the proper frequency */
346 err = devfreq->governor->get_target_freq(devfreq, &freq);
347 if (err)
348 return err;
349
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100350 /*
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900351 * Adjust the frequency with user freq, QoS and available freq.
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100352 *
Javi Merinod3b7e172015-08-14 18:57:00 +0100353 * List from the highest priority
354 * max_freq
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100355 * min_freq
356 */
Bjorn Anderssond0e46422018-04-24 12:46:39 -0700357 max_freq = min(devfreq->scaling_max_freq, devfreq->max_freq);
358 min_freq = max(devfreq->scaling_min_freq, devfreq->min_freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100359
Matthias Kaehlckedf5cf4a2018-08-03 13:05:09 -0700360 if (freq < min_freq) {
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900361 freq = min_freq;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100362 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
363 }
Matthias Kaehlckedf5cf4a2018-08-03 13:05:09 -0700364 if (freq > max_freq) {
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900365 freq = max_freq;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100366 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
367 }
368
Lukasz Luba63314172018-12-05 12:05:52 +0100369 return devfreq_set_target(devfreq, freq, flags);
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900370
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200371}
Nishanth Menon2df50212012-10-29 15:01:42 -0500372EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200373
374/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200375 * devfreq_monitor() - Periodically poll devfreq objects.
376 * @work: the work struct used to run devfreq_monitor periodically.
377 *
378 */
379static void devfreq_monitor(struct work_struct *work)
380{
381 int err;
382 struct devfreq *devfreq = container_of(work,
383 struct devfreq, work.work);
384
385 mutex_lock(&devfreq->lock);
386 err = update_devfreq(devfreq);
387 if (err)
388 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
389
390 queue_delayed_work(devfreq_wq, &devfreq->work,
391 msecs_to_jiffies(devfreq->profile->polling_ms));
392 mutex_unlock(&devfreq->lock);
393}
394
395/**
396 * devfreq_monitor_start() - Start load monitoring of devfreq instance
397 * @devfreq: the devfreq instance.
398 *
399 * Helper function for starting devfreq device load monitoing. By
400 * default delayed work based monitoring is supported. Function
401 * to be called from governor in response to DEVFREQ_GOV_START
402 * event when device is added to devfreq framework.
403 */
404void devfreq_monitor_start(struct devfreq *devfreq)
405{
406 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
407 if (devfreq->profile->polling_ms)
408 queue_delayed_work(devfreq_wq, &devfreq->work,
409 msecs_to_jiffies(devfreq->profile->polling_ms));
410}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100411EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200412
413/**
414 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
415 * @devfreq: the devfreq instance.
416 *
417 * Helper function to stop devfreq device load monitoing. Function
418 * to be called from governor in response to DEVFREQ_GOV_STOP
419 * event when device is removed from devfreq framework.
420 */
421void devfreq_monitor_stop(struct devfreq *devfreq)
422{
423 cancel_delayed_work_sync(&devfreq->work);
424}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100425EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200426
427/**
428 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
429 * @devfreq: the devfreq instance.
430 *
431 * Helper function to suspend devfreq device load monitoing. Function
432 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
433 * event or when polling interval is set to zero.
434 *
435 * Note: Though this function is same as devfreq_monitor_stop(),
436 * intentionally kept separate to provide hooks for collecting
437 * transition statistics.
438 */
439void devfreq_monitor_suspend(struct devfreq *devfreq)
440{
441 mutex_lock(&devfreq->lock);
442 if (devfreq->stop_polling) {
443 mutex_unlock(&devfreq->lock);
444 return;
445 }
446
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530447 devfreq_update_status(devfreq, devfreq->previous_freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200448 devfreq->stop_polling = true;
449 mutex_unlock(&devfreq->lock);
450 cancel_delayed_work_sync(&devfreq->work);
451}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100452EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200453
454/**
455 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
456 * @devfreq: the devfreq instance.
457 *
458 * Helper function to resume devfreq device load monitoing. Function
459 * to be called from governor in response to DEVFREQ_GOV_RESUME
460 * event or when polling interval is set to non-zero.
461 */
462void devfreq_monitor_resume(struct devfreq *devfreq)
463{
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530464 unsigned long freq;
465
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200466 mutex_lock(&devfreq->lock);
467 if (!devfreq->stop_polling)
468 goto out;
469
470 if (!delayed_work_pending(&devfreq->work) &&
471 devfreq->profile->polling_ms)
472 queue_delayed_work(devfreq_wq, &devfreq->work,
473 msecs_to_jiffies(devfreq->profile->polling_ms));
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530474
475 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200476 devfreq->stop_polling = false;
477
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530478 if (devfreq->profile->get_cur_freq &&
479 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
480 devfreq->previous_freq = freq;
481
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200482out:
483 mutex_unlock(&devfreq->lock);
484}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100485EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200486
487/**
488 * devfreq_interval_update() - Update device devfreq monitoring interval
489 * @devfreq: the devfreq instance.
490 * @delay: new polling interval to be set.
491 *
492 * Helper function to set new load monitoring polling interval. Function
493 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
494 */
495void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
496{
497 unsigned int cur_delay = devfreq->profile->polling_ms;
498 unsigned int new_delay = *delay;
499
500 mutex_lock(&devfreq->lock);
501 devfreq->profile->polling_ms = new_delay;
502
503 if (devfreq->stop_polling)
504 goto out;
505
506 /* if new delay is zero, stop polling */
507 if (!new_delay) {
508 mutex_unlock(&devfreq->lock);
509 cancel_delayed_work_sync(&devfreq->work);
510 return;
511 }
512
513 /* if current delay is zero, start polling with new delay */
514 if (!cur_delay) {
515 queue_delayed_work(devfreq_wq, &devfreq->work,
516 msecs_to_jiffies(devfreq->profile->polling_ms));
517 goto out;
518 }
519
520 /* if current delay is greater than new delay, restart polling */
521 if (cur_delay > new_delay) {
522 mutex_unlock(&devfreq->lock);
523 cancel_delayed_work_sync(&devfreq->work);
524 mutex_lock(&devfreq->lock);
525 if (!devfreq->stop_polling)
526 queue_delayed_work(devfreq_wq, &devfreq->work,
527 msecs_to_jiffies(devfreq->profile->polling_ms));
528 }
529out:
530 mutex_unlock(&devfreq->lock);
531}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100532EXPORT_SYMBOL(devfreq_interval_update);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200533
534/**
535 * devfreq_notifier_call() - Notify that the device frequency requirements
536 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200537 * @nb: the notifier_block (supposed to be devfreq->nb)
538 * @type: not used
539 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200540 *
541 * Called by a notifier that uses devfreq->nb.
542 */
543static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
544 void *devp)
545{
546 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
547 int ret;
548
549 mutex_lock(&devfreq->lock);
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900550
551 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
552 if (!devfreq->scaling_min_freq) {
553 mutex_unlock(&devfreq->lock);
554 return -EINVAL;
555 }
556
557 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
558 if (!devfreq->scaling_max_freq) {
559 mutex_unlock(&devfreq->lock);
560 return -EINVAL;
561 }
562
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200563 ret = update_devfreq(devfreq);
564 mutex_unlock(&devfreq->lock);
565
566 return ret;
567}
568
569/**
Chanwoo Choi29b69682017-01-31 15:38:18 +0900570 * devfreq_dev_release() - Callback for struct device to release the device.
571 * @dev: the devfreq device
572 *
573 * Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200574 */
Chanwoo Choi29b69682017-01-31 15:38:18 +0900575static void devfreq_dev_release(struct device *dev)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200576{
Chanwoo Choi29b69682017-01-31 15:38:18 +0900577 struct devfreq *devfreq = to_devfreq(dev);
578
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200579 mutex_lock(&devfreq_list_lock);
580 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
581 mutex_unlock(&devfreq_list_lock);
582 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200583 return;
584 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200585 list_del(&devfreq->node);
586 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200587
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200588 if (devfreq->profile->exit)
589 devfreq->profile->exit(devfreq->dev.parent);
590
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200591 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200592 kfree(devfreq);
593}
594
595/**
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200596 * devfreq_add_device() - Add devfreq feature to the device
597 * @dev: the device to add devfreq feature.
598 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500599 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200600 * @data: private data for the governor. The devfreq framework does not
601 * touch this value.
602 */
603struct devfreq *devfreq_add_device(struct device *dev,
604 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500605 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200606 void *data)
607{
608 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500609 struct devfreq_governor *governor;
Chanwoo Choi4585fbc2017-01-31 16:47:57 +0900610 static atomic_t devfreq_no = ATOMIC_INIT(-1);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200611 int err = 0;
612
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500613 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200614 dev_err(dev, "%s: Invalid parameters.\n", __func__);
615 return ERR_PTR(-EINVAL);
616 }
617
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200618 mutex_lock(&devfreq_list_lock);
619 devfreq = find_device_devfreq(dev);
620 mutex_unlock(&devfreq_list_lock);
621 if (!IS_ERR(devfreq)) {
Chanwoo Choi9d0109b2016-11-19 22:47:36 +0900622 dev_err(dev, "%s: Unable to create devfreq for the device.\n",
623 __func__);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200624 err = -EINVAL;
625 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200626 }
627
628 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
629 if (!devfreq) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200630 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100631 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200632 }
633
634 mutex_init(&devfreq->lock);
635 mutex_lock(&devfreq->lock);
636 devfreq->dev.parent = dev;
637 devfreq->dev.class = devfreq_class;
638 devfreq->dev.release = devfreq_dev_release;
639 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500640 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200641 devfreq->previous_freq = profile->initial_freq;
Lukasz Luba8d39fc082016-05-31 11:25:09 +0100642 devfreq->last_status.current_frequency = profile->initial_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200643 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200644 devfreq->nb.notifier_call = devfreq_notifier_call;
645
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900646 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
647 mutex_unlock(&devfreq->lock);
Chanwoo Choiea572f82017-10-23 10:32:09 +0900648 err = set_freq_table(devfreq);
649 if (err < 0)
650 goto err_out;
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900651 mutex_lock(&devfreq->lock);
652 }
653
Matthias Kaehlcke2c2cb1e62018-05-25 13:30:33 -0700654 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
655 if (!devfreq->scaling_min_freq) {
Chanwoo Choiab8f58a2017-10-23 10:32:06 +0900656 mutex_unlock(&devfreq->lock);
657 err = -EINVAL;
658 goto err_dev;
659 }
Matthias Kaehlcke2c2cb1e62018-05-25 13:30:33 -0700660 devfreq->min_freq = devfreq->scaling_min_freq;
Chanwoo Choiab8f58a2017-10-23 10:32:06 +0900661
Matthias Kaehlcke2c2cb1e62018-05-25 13:30:33 -0700662 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
663 if (!devfreq->scaling_max_freq) {
Chanwoo Choiab8f58a2017-10-23 10:32:06 +0900664 mutex_unlock(&devfreq->lock);
665 err = -EINVAL;
666 goto err_dev;
667 }
Matthias Kaehlcke2c2cb1e62018-05-25 13:30:33 -0700668 devfreq->max_freq = devfreq->scaling_max_freq;
Chanwoo Choiab8f58a2017-10-23 10:32:06 +0900669
Chanwoo Choi4585fbc2017-01-31 16:47:57 +0900670 dev_set_name(&devfreq->dev, "devfreq%d",
671 atomic_inc_return(&devfreq_no));
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200672 err = device_register(&devfreq->dev);
673 if (err) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200674 mutex_unlock(&devfreq->lock);
Arvind Yadav2d803dc2018-03-30 17:14:03 +0530675 put_device(&devfreq->dev);
676 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200677 }
678
Kees Cooka86854d2018-06-12 14:07:58 -0700679 devfreq->trans_table =
680 devm_kzalloc(&devfreq->dev,
681 array3_size(sizeof(unsigned int),
682 devfreq->profile->max_state,
683 devfreq->profile->max_state),
684 GFP_KERNEL);
685 devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900686 devfreq->profile->max_state,
Kees Cooka86854d2018-06-12 14:07:58 -0700687 sizeof(unsigned long),
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900688 GFP_KERNEL);
689 devfreq->last_stat_updated = jiffies;
690
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900691 srcu_init_notifier_head(&devfreq->transition_notifier_list);
692
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200693 mutex_unlock(&devfreq->lock);
694
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200695 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200696
Enric Balletbo i Serra23c7b542018-07-04 10:45:50 +0200697 governor = try_then_request_governor(devfreq->governor_name);
Chanwoo Choi73613b12016-12-28 20:52:35 +0900698 if (IS_ERR(governor)) {
699 dev_err(dev, "%s: Unable to find governor for the device\n",
700 __func__);
701 err = PTR_ERR(governor);
702 goto err_init;
703 }
704
705 devfreq->governor = governor;
706 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
707 NULL);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200708 if (err) {
709 dev_err(dev, "%s: Unable to start governor for the device\n",
710 __func__);
711 goto err_init;
712 }
Enric Balletbo i Serra23c7b542018-07-04 10:45:50 +0200713
714 list_add(&devfreq->node, &devfreq_list);
715
Axel Lin0f376c92016-09-29 10:13:28 +0800716 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200717
Axel Lin3f19f082011-11-15 21:59:09 +0100718 return devfreq;
719
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200720err_init:
Axel Lin0f376c92016-09-29 10:13:28 +0800721 mutex_unlock(&devfreq_list_lock);
722
Vincent Donnefort2f061fd2018-09-03 09:02:07 +0900723 devfreq_remove_device(devfreq);
Arvind Yadav2d803dc2018-03-30 17:14:03 +0530724 devfreq = NULL;
Chanwoo Choi9e14de12017-08-24 10:42:48 +0900725err_dev:
zhong jiang8188b152018-09-21 21:18:43 +0800726 kfree(devfreq);
Axel Lin3f19f082011-11-15 21:59:09 +0100727err_out:
728 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200729}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200730EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200731
732/**
733 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200734 * @devfreq: the devfreq instance to be removed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900735 *
736 * The opposite of devfreq_add_device().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200737 */
738int devfreq_remove_device(struct devfreq *devfreq)
739{
740 if (!devfreq)
741 return -EINVAL;
742
Vincent Donnefort2f061fd2018-09-03 09:02:07 +0900743 if (devfreq->governor)
744 devfreq->governor->event_handler(devfreq,
745 DEVFREQ_GOV_STOP, NULL);
Chanwoo Choi585fc832014-05-09 16:43:07 +0900746 device_unregister(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200747
748 return 0;
749}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200750EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200751
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900752static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
753{
754 struct devfreq **r = res;
755
756 if (WARN_ON(!r || !*r))
757 return 0;
758
759 return *r == data;
760}
761
762static void devm_devfreq_dev_release(struct device *dev, void *res)
763{
764 devfreq_remove_device(*(struct devfreq **)res);
765}
766
767/**
768 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
769 * @dev: the device to add devfreq feature.
770 * @profile: device-specific profile to run devfreq.
771 * @governor_name: name of the policy to choose frequency.
772 * @data: private data for the governor. The devfreq framework does not
773 * touch this value.
774 *
775 * This function manages automatically the memory of devfreq device using device
776 * resource management and simplify the free operation for memory of devfreq
777 * device.
778 */
779struct devfreq *devm_devfreq_add_device(struct device *dev,
780 struct devfreq_dev_profile *profile,
781 const char *governor_name,
782 void *data)
783{
784 struct devfreq **ptr, *devfreq;
785
786 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
787 if (!ptr)
788 return ERR_PTR(-ENOMEM);
789
790 devfreq = devfreq_add_device(dev, profile, governor_name, data);
791 if (IS_ERR(devfreq)) {
792 devres_free(ptr);
Bjorn Anderssond1bf2d302017-11-05 21:27:41 -0800793 return devfreq;
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900794 }
795
796 *ptr = devfreq;
797 devres_add(dev, ptr);
798
799 return devfreq;
800}
801EXPORT_SYMBOL(devm_devfreq_add_device);
802
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900803#ifdef CONFIG_OF
804/*
805 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
806 * @dev - instance to the given device
807 * @index - index into list of devfreq
808 *
809 * return the instance of devfreq device
810 */
811struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
812{
813 struct device_node *node;
814 struct devfreq *devfreq;
815
816 if (!dev)
817 return ERR_PTR(-EINVAL);
818
819 if (!dev->of_node)
820 return ERR_PTR(-EINVAL);
821
822 node = of_parse_phandle(dev->of_node, "devfreq", index);
823 if (!node)
824 return ERR_PTR(-ENODEV);
825
826 mutex_lock(&devfreq_list_lock);
827 list_for_each_entry(devfreq, &devfreq_list, node) {
828 if (devfreq->dev.parent
829 && devfreq->dev.parent->of_node == node) {
830 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800831 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900832 return devfreq;
833 }
834 }
835 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800836 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900837
838 return ERR_PTR(-EPROBE_DEFER);
839}
840#else
841struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
842{
843 return ERR_PTR(-ENODEV);
844}
845#endif /* CONFIG_OF */
846EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
847
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900848/**
849 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
850 * @dev: the device to add devfreq feature.
851 * @devfreq: the devfreq instance to be removed
852 */
853void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
854{
855 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
856 devm_devfreq_dev_match, devfreq));
857}
858EXPORT_SYMBOL(devm_devfreq_remove_device);
859
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200860/**
861 * devfreq_suspend_device() - Suspend devfreq of a device.
862 * @devfreq: the devfreq instance to be suspended
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900863 *
864 * This function is intended to be called by the pm callbacks
865 * (e.g., runtime_suspend, suspend) of the device driver that
866 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200867 */
868int devfreq_suspend_device(struct devfreq *devfreq)
869{
870 if (!devfreq)
871 return -EINVAL;
872
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500873 if (!devfreq->governor)
874 return 0;
875
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200876 return devfreq->governor->event_handler(devfreq,
877 DEVFREQ_GOV_SUSPEND, NULL);
878}
879EXPORT_SYMBOL(devfreq_suspend_device);
880
881/**
882 * devfreq_resume_device() - Resume devfreq of a device.
883 * @devfreq: the devfreq instance to be resumed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900884 *
885 * This function is intended to be called by the pm callbacks
886 * (e.g., runtime_resume, resume) of the device driver that
887 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200888 */
889int devfreq_resume_device(struct devfreq *devfreq)
890{
891 if (!devfreq)
892 return -EINVAL;
893
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500894 if (!devfreq->governor)
895 return 0;
896
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200897 return devfreq->governor->event_handler(devfreq,
898 DEVFREQ_GOV_RESUME, NULL);
899}
900EXPORT_SYMBOL(devfreq_resume_device);
901
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500902/**
903 * devfreq_add_governor() - Add devfreq governor
904 * @governor: the devfreq governor to be added
905 */
906int devfreq_add_governor(struct devfreq_governor *governor)
907{
908 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500909 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500910 int err = 0;
911
912 if (!governor) {
913 pr_err("%s: Invalid parameters.\n", __func__);
914 return -EINVAL;
915 }
916
917 mutex_lock(&devfreq_list_lock);
918 g = find_devfreq_governor(governor->name);
919 if (!IS_ERR(g)) {
920 pr_err("%s: governor %s already registered\n", __func__,
921 g->name);
922 err = -EINVAL;
923 goto err_out;
924 }
925
926 list_add(&governor->node, &devfreq_governor_list);
927
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500928 list_for_each_entry(devfreq, &devfreq_list, node) {
929 int ret = 0;
930 struct device *dev = devfreq->dev.parent;
931
932 if (!strncmp(devfreq->governor_name, governor->name,
933 DEVFREQ_NAME_LEN)) {
934 /* The following should never occur */
935 if (devfreq->governor) {
936 dev_warn(dev,
937 "%s: Governor %s already present\n",
938 __func__, devfreq->governor->name);
939 ret = devfreq->governor->event_handler(devfreq,
940 DEVFREQ_GOV_STOP, NULL);
941 if (ret) {
942 dev_warn(dev,
943 "%s: Governor %s stop = %d\n",
944 __func__,
945 devfreq->governor->name, ret);
946 }
947 /* Fall through */
948 }
949 devfreq->governor = governor;
950 ret = devfreq->governor->event_handler(devfreq,
951 DEVFREQ_GOV_START, NULL);
952 if (ret) {
953 dev_warn(dev, "%s: Governor %s start=%d\n",
954 __func__, devfreq->governor->name,
955 ret);
956 }
957 }
958 }
959
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500960err_out:
961 mutex_unlock(&devfreq_list_lock);
962
963 return err;
964}
965EXPORT_SYMBOL(devfreq_add_governor);
966
967/**
MyungJoo Hambafeb422016-11-09 10:29:14 +0900968 * devfreq_remove_governor() - Remove devfreq feature from a device.
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500969 * @governor: the devfreq governor to be removed
970 */
971int devfreq_remove_governor(struct devfreq_governor *governor)
972{
973 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500974 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500975 int err = 0;
976
977 if (!governor) {
978 pr_err("%s: Invalid parameters.\n", __func__);
979 return -EINVAL;
980 }
981
982 mutex_lock(&devfreq_list_lock);
983 g = find_devfreq_governor(governor->name);
984 if (IS_ERR(g)) {
985 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamatb9e1c8e2012-11-21 10:36:13 +0530986 governor->name);
Sachin Kamatf9c08e22012-11-21 10:36:14 +0530987 err = PTR_ERR(g);
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500988 goto err_out;
989 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500990 list_for_each_entry(devfreq, &devfreq_list, node) {
991 int ret;
992 struct device *dev = devfreq->dev.parent;
993
994 if (!strncmp(devfreq->governor_name, governor->name,
995 DEVFREQ_NAME_LEN)) {
996 /* we should have a devfreq governor! */
997 if (!devfreq->governor) {
998 dev_warn(dev, "%s: Governor %s NOT present\n",
999 __func__, governor->name);
1000 continue;
1001 /* Fall through */
1002 }
1003 ret = devfreq->governor->event_handler(devfreq,
1004 DEVFREQ_GOV_STOP, NULL);
1005 if (ret) {
1006 dev_warn(dev, "%s: Governor %s stop=%d\n",
1007 __func__, devfreq->governor->name,
1008 ret);
1009 }
1010 devfreq->governor = NULL;
1011 }
1012 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -05001013
1014 list_del(&governor->node);
1015err_out:
1016 mutex_unlock(&devfreq_list_lock);
1017
1018 return err;
1019}
1020EXPORT_SYMBOL(devfreq_remove_governor);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001021
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001022static ssize_t governor_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001023 struct device_attribute *attr, char *buf)
1024{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001025 if (!to_devfreq(dev)->governor)
1026 return -EINVAL;
1027
MyungJoo Ham9005b652011-10-02 00:19:28 +02001028 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1029}
1030
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001031static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001032 const char *buf, size_t count)
1033{
1034 struct devfreq *df = to_devfreq(dev);
1035 int ret;
1036 char str_governor[DEVFREQ_NAME_LEN + 1];
1037 struct devfreq_governor *governor;
1038
1039 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1040 if (ret != 1)
1041 return -EINVAL;
1042
1043 mutex_lock(&devfreq_list_lock);
Enric Balletbo i Serra23c7b542018-07-04 10:45:50 +02001044 governor = try_then_request_governor(str_governor);
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001045 if (IS_ERR(governor)) {
1046 ret = PTR_ERR(governor);
1047 goto out;
1048 }
Tobias Jakobi14a21e72015-09-21 20:23:52 +02001049 if (df->governor == governor) {
1050 ret = 0;
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001051 goto out;
Gustavo A. R. Silva63f1e052017-12-06 14:20:15 -06001052 } else if ((df->governor && df->governor->immutable) ||
1053 governor->immutable) {
Chanwoo Choibcf23c72017-01-31 15:38:16 +09001054 ret = -EINVAL;
1055 goto out;
Tobias Jakobi14a21e72015-09-21 20:23:52 +02001056 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001057
1058 if (df->governor) {
1059 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1060 if (ret) {
1061 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1062 __func__, df->governor->name, ret);
1063 goto out;
1064 }
1065 }
1066 df->governor = governor;
1067 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1068 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1069 if (ret)
1070 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1071 __func__, df->governor->name, ret);
1072out:
1073 mutex_unlock(&devfreq_list_lock);
1074
1075 if (!ret)
1076 ret = count;
1077 return ret;
1078}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001079static DEVICE_ATTR_RW(governor);
1080
1081static ssize_t available_governors_show(struct device *d,
1082 struct device_attribute *attr,
1083 char *buf)
Nishanth Menon50a5b332012-10-29 15:01:48 -05001084{
Chanwoo Choibcf23c72017-01-31 15:38:16 +09001085 struct devfreq *df = to_devfreq(d);
Nishanth Menon50a5b332012-10-29 15:01:48 -05001086 ssize_t count = 0;
1087
1088 mutex_lock(&devfreq_list_lock);
Chanwoo Choibcf23c72017-01-31 15:38:16 +09001089
1090 /*
1091 * The devfreq with immutable governor (e.g., passive) shows
1092 * only own governor.
1093 */
1094 if (df->governor->immutable) {
1095 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1096 "%s ", df->governor_name);
1097 /*
1098 * The devfreq device shows the registered governor except for
1099 * immutable governors such as passive governor .
1100 */
1101 } else {
1102 struct devfreq_governor *governor;
1103
1104 list_for_each_entry(governor, &devfreq_governor_list, node) {
1105 if (governor->immutable)
1106 continue;
1107 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1108 "%s ", governor->name);
1109 }
1110 }
1111
Nishanth Menon50a5b332012-10-29 15:01:48 -05001112 mutex_unlock(&devfreq_list_lock);
1113
1114 /* Truncate the trailing space */
1115 if (count)
1116 count--;
1117
1118 count += sprintf(&buf[count], "\n");
1119
1120 return count;
1121}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001122static DEVICE_ATTR_RO(available_governors);
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001123
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001124static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1125 char *buf)
MyungJoo Ham9005b652011-10-02 00:19:28 +02001126{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001127 unsigned long freq;
1128 struct devfreq *devfreq = to_devfreq(dev);
1129
1130 if (devfreq->profile->get_cur_freq &&
1131 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
Chanwoo Choi9d0109b2016-11-19 22:47:36 +09001132 return sprintf(buf, "%lu\n", freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001133
1134 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1135}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001136static DEVICE_ATTR_RO(cur_freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001137
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001138static ssize_t target_freq_show(struct device *dev,
1139 struct device_attribute *attr, char *buf)
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001140{
MyungJoo Ham9005b652011-10-02 00:19:28 +02001141 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1142}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001143static DEVICE_ATTR_RO(target_freq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001144
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001145static ssize_t polling_interval_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001146 struct device_attribute *attr, char *buf)
1147{
1148 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1149}
1150
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001151static ssize_t polling_interval_store(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001152 struct device_attribute *attr,
1153 const char *buf, size_t count)
1154{
1155 struct devfreq *df = to_devfreq(dev);
1156 unsigned int value;
1157 int ret;
1158
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001159 if (!df->governor)
1160 return -EINVAL;
1161
MyungJoo Ham9005b652011-10-02 00:19:28 +02001162 ret = sscanf(buf, "%u", &value);
1163 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001164 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +02001165
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001166 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001167 ret = count;
1168
MyungJoo Ham9005b652011-10-02 00:19:28 +02001169 return ret;
1170}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001171static DEVICE_ATTR_RW(polling_interval);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001172
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001173static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001174 const char *buf, size_t count)
1175{
1176 struct devfreq *df = to_devfreq(dev);
1177 unsigned long value;
1178 int ret;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001179
1180 ret = sscanf(buf, "%lu", &value);
1181 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001182 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001183
1184 mutex_lock(&df->lock);
Matthias Kaehlckedf5cf4a2018-08-03 13:05:09 -07001185
1186 if (value) {
1187 if (value > df->max_freq) {
1188 ret = -EINVAL;
1189 goto unlock;
1190 }
1191 } else {
1192 unsigned long *freq_table = df->profile->freq_table;
1193
1194 /* Get minimum frequency according to sorting order */
1195 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1196 value = freq_table[0];
1197 else
1198 value = freq_table[df->profile->max_state - 1];
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001199 }
1200
1201 df->min_freq = value;
1202 update_devfreq(df);
1203 ret = count;
1204unlock:
1205 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001206 return ret;
1207}
1208
Chanwoo Choi1051e2c2017-10-23 10:32:07 +09001209static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1210 char *buf)
1211{
Chanwoo Choif1d981e2017-10-23 10:32:08 +09001212 struct devfreq *df = to_devfreq(dev);
1213
Bjorn Anderssond0e46422018-04-24 12:46:39 -07001214 return sprintf(buf, "%lu\n", max(df->scaling_min_freq, df->min_freq));
Chanwoo Choi1051e2c2017-10-23 10:32:07 +09001215}
1216
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001217static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001218 const char *buf, size_t count)
1219{
1220 struct devfreq *df = to_devfreq(dev);
1221 unsigned long value;
1222 int ret;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001223
1224 ret = sscanf(buf, "%lu", &value);
1225 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001226 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001227
1228 mutex_lock(&df->lock);
Matthias Kaehlckedf5cf4a2018-08-03 13:05:09 -07001229
1230 if (value) {
1231 if (value < df->min_freq) {
1232 ret = -EINVAL;
1233 goto unlock;
1234 }
1235 } else {
1236 unsigned long *freq_table = df->profile->freq_table;
1237
1238 /* Get maximum frequency according to sorting order */
1239 if (freq_table[0] < freq_table[df->profile->max_state - 1])
1240 value = freq_table[df->profile->max_state - 1];
1241 else
1242 value = freq_table[0];
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001243 }
1244
1245 df->max_freq = value;
1246 update_devfreq(df);
1247 ret = count;
1248unlock:
1249 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001250 return ret;
1251}
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001252static DEVICE_ATTR_RW(min_freq);
Chanwoo Choi1051e2c2017-10-23 10:32:07 +09001253
1254static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1255 char *buf)
1256{
Chanwoo Choif1d981e2017-10-23 10:32:08 +09001257 struct devfreq *df = to_devfreq(dev);
1258
Bjorn Anderssond0e46422018-04-24 12:46:39 -07001259 return sprintf(buf, "%lu\n", min(df->scaling_max_freq, df->max_freq));
Chanwoo Choi1051e2c2017-10-23 10:32:07 +09001260}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001261static DEVICE_ATTR_RW(max_freq);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001262
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001263static ssize_t available_frequencies_show(struct device *d,
1264 struct device_attribute *attr,
1265 char *buf)
Nishanth Menond287de82012-10-25 19:48:59 -05001266{
1267 struct devfreq *df = to_devfreq(d);
Nishanth Menond287de82012-10-25 19:48:59 -05001268 ssize_t count = 0;
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001269 int i;
Nishanth Menond287de82012-10-25 19:48:59 -05001270
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001271 mutex_lock(&df->lock);
Nishanth Menond287de82012-10-25 19:48:59 -05001272
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001273 for (i = 0; i < df->profile->max_state; i++)
Nishanth Menond287de82012-10-25 19:48:59 -05001274 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001275 "%lu ", df->profile->freq_table[i]);
Nishanth Menond287de82012-10-25 19:48:59 -05001276
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001277 mutex_unlock(&df->lock);
Nishanth Menond287de82012-10-25 19:48:59 -05001278 /* Truncate the trailing space */
1279 if (count)
1280 count--;
1281
1282 count += sprintf(&buf[count], "\n");
1283
1284 return count;
1285}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001286static DEVICE_ATTR_RO(available_frequencies);
Nishanth Menond287de82012-10-25 19:48:59 -05001287
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001288static ssize_t trans_stat_show(struct device *dev,
1289 struct device_attribute *attr, char *buf)
Jonghwa Leee552bba2012-08-23 20:00:46 +09001290{
1291 struct devfreq *devfreq = to_devfreq(dev);
1292 ssize_t len;
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301293 int i, j;
Jonghwa Leee552bba2012-08-23 20:00:46 +09001294 unsigned int max_state = devfreq->profile->max_state;
1295
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301296 if (!devfreq->stop_polling &&
1297 devfreq_update_status(devfreq, devfreq->previous_freq))
Jonghwa Leee552bba2012-08-23 20:00:46 +09001298 return 0;
MyungJoo Ham34bd3222015-11-23 15:45:36 +09001299 if (max_state == 0)
1300 return sprintf(buf, "Not Supported.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001301
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001302 len = sprintf(buf, " From : To\n");
1303 len += sprintf(buf + len, " :");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001304 for (i = 0; i < max_state; i++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001305 len += sprintf(buf + len, "%10lu",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001306 devfreq->profile->freq_table[i]);
1307
1308 len += sprintf(buf + len, " time(ms)\n");
1309
1310 for (i = 0; i < max_state; i++) {
1311 if (devfreq->profile->freq_table[i]
1312 == devfreq->previous_freq) {
1313 len += sprintf(buf + len, "*");
1314 } else {
1315 len += sprintf(buf + len, " ");
1316 }
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001317 len += sprintf(buf + len, "%10lu:",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001318 devfreq->profile->freq_table[i]);
1319 for (j = 0; j < max_state; j++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001320 len += sprintf(buf + len, "%10u",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001321 devfreq->trans_table[(i * max_state) + j]);
1322 len += sprintf(buf + len, "%10u\n",
1323 jiffies_to_msecs(devfreq->time_in_state[i]));
1324 }
1325
1326 len += sprintf(buf + len, "Total transition : %u\n",
1327 devfreq->total_trans);
1328 return len;
1329}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001330static DEVICE_ATTR_RO(trans_stat);
Jonghwa Leee552bba2012-08-23 20:00:46 +09001331
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001332static struct attribute *devfreq_attrs[] = {
1333 &dev_attr_governor.attr,
1334 &dev_attr_available_governors.attr,
1335 &dev_attr_cur_freq.attr,
1336 &dev_attr_available_frequencies.attr,
1337 &dev_attr_target_freq.attr,
1338 &dev_attr_polling_interval.attr,
1339 &dev_attr_min_freq.attr,
1340 &dev_attr_max_freq.attr,
1341 &dev_attr_trans_stat.attr,
1342 NULL,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001343};
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001344ATTRIBUTE_GROUPS(devfreq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001345
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001346static int __init devfreq_init(void)
1347{
1348 devfreq_class = class_create(THIS_MODULE, "devfreq");
1349 if (IS_ERR(devfreq_class)) {
1350 pr_err("%s: couldn't create class\n", __FILE__);
1351 return PTR_ERR(devfreq_class);
1352 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001353
1354 devfreq_wq = create_freezable_workqueue("devfreq_wq");
Dan Carpenterea7f4542013-08-15 10:55:10 +03001355 if (!devfreq_wq) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001356 class_destroy(devfreq_class);
1357 pr_err("%s: couldn't create workqueue\n", __FILE__);
Dan Carpenterea7f4542013-08-15 10:55:10 +03001358 return -ENOMEM;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001359 }
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001360 devfreq_class->dev_groups = devfreq_groups;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001361
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001362 return 0;
1363}
1364subsys_initcall(devfreq_init);
1365
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001366/*
Masahiro Yamada4091fb92017-02-27 14:29:56 -08001367 * The following are helper functions for devfreq user device drivers with
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001368 * OPP framework.
1369 */
1370
1371/**
1372 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1373 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001374 * @dev: The devfreq user device. (parent of devfreq)
1375 * @freq: The frequency given to target function
1376 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001377 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301378 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1379 * use.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001380 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001381struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1382 unsigned long *freq,
1383 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001384{
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001385 struct dev_pm_opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001386
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001387 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1388 /* The freq is an upper bound. opp should be lower */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001389 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001390
1391 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001392 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001393 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001394 } else {
1395 /* The freq is an lower bound. opp should be higher */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001396 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001397
1398 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001399 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001400 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001401 }
1402
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001403 return opp;
1404}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001405EXPORT_SYMBOL(devfreq_recommended_opp);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001406
1407/**
1408 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1409 * for any changes in the OPP availability
1410 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001411 * @dev: The devfreq user device. (parent of devfreq)
1412 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001413 */
1414int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1415{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301416 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001417}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001418EXPORT_SYMBOL(devfreq_register_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001419
1420/**
1421 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1422 * notified for any changes in the OPP
1423 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001424 * @dev: The devfreq user device. (parent of devfreq)
1425 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001426 *
1427 * At exit() callback of devfreq_dev_profile, this must be included if
1428 * devfreq_recommended_opp is used.
1429 */
1430int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1431{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301432 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001433}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001434EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001435
Chanwoo Choid5b040d2014-05-09 16:43:09 +09001436static void devm_devfreq_opp_release(struct device *dev, void *res)
1437{
1438 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1439}
1440
1441/**
1442 * devm_ devfreq_register_opp_notifier()
1443 * - Resource-managed devfreq_register_opp_notifier()
1444 * @dev: The devfreq user device. (parent of devfreq)
1445 * @devfreq: The devfreq object.
1446 */
1447int devm_devfreq_register_opp_notifier(struct device *dev,
1448 struct devfreq *devfreq)
1449{
1450 struct devfreq **ptr;
1451 int ret;
1452
1453 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1454 if (!ptr)
1455 return -ENOMEM;
1456
1457 ret = devfreq_register_opp_notifier(dev, devfreq);
1458 if (ret) {
1459 devres_free(ptr);
1460 return ret;
1461 }
1462
1463 *ptr = devfreq;
1464 devres_add(dev, ptr);
1465
1466 return 0;
1467}
1468EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1469
1470/**
1471 * devm_devfreq_unregister_opp_notifier()
1472 * - Resource-managed devfreq_unregister_opp_notifier()
1473 * @dev: The devfreq user device. (parent of devfreq)
1474 * @devfreq: The devfreq object.
1475 */
1476void devm_devfreq_unregister_opp_notifier(struct device *dev,
1477 struct devfreq *devfreq)
1478{
1479 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1480 devm_devfreq_dev_match, devfreq));
1481}
1482EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1483
Chanwoo Choi0fe3a662016-01-26 13:21:26 +09001484/**
1485 * devfreq_register_notifier() - Register a driver with devfreq
1486 * @devfreq: The devfreq object.
1487 * @nb: The notifier block to register.
1488 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1489 */
1490int devfreq_register_notifier(struct devfreq *devfreq,
1491 struct notifier_block *nb,
1492 unsigned int list)
1493{
1494 int ret = 0;
1495
1496 if (!devfreq)
1497 return -EINVAL;
1498
1499 switch (list) {
1500 case DEVFREQ_TRANSITION_NOTIFIER:
1501 ret = srcu_notifier_chain_register(
1502 &devfreq->transition_notifier_list, nb);
1503 break;
1504 default:
1505 ret = -EINVAL;
1506 }
1507
1508 return ret;
1509}
1510EXPORT_SYMBOL(devfreq_register_notifier);
1511
1512/*
1513 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1514 * @devfreq: The devfreq object.
1515 * @nb: The notifier block to be unregistered.
1516 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1517 */
1518int devfreq_unregister_notifier(struct devfreq *devfreq,
1519 struct notifier_block *nb,
1520 unsigned int list)
1521{
1522 int ret = 0;
1523
1524 if (!devfreq)
1525 return -EINVAL;
1526
1527 switch (list) {
1528 case DEVFREQ_TRANSITION_NOTIFIER:
1529 ret = srcu_notifier_chain_unregister(
1530 &devfreq->transition_notifier_list, nb);
1531 break;
1532 default:
1533 ret = -EINVAL;
1534 }
1535
1536 return ret;
1537}
1538EXPORT_SYMBOL(devfreq_unregister_notifier);
1539
1540struct devfreq_notifier_devres {
1541 struct devfreq *devfreq;
1542 struct notifier_block *nb;
1543 unsigned int list;
1544};
1545
1546static void devm_devfreq_notifier_release(struct device *dev, void *res)
1547{
1548 struct devfreq_notifier_devres *this = res;
1549
1550 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1551}
1552
1553/**
1554 * devm_devfreq_register_notifier()
1555 - Resource-managed devfreq_register_notifier()
1556 * @dev: The devfreq user device. (parent of devfreq)
1557 * @devfreq: The devfreq object.
1558 * @nb: The notifier block to be unregistered.
1559 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1560 */
1561int devm_devfreq_register_notifier(struct device *dev,
1562 struct devfreq *devfreq,
1563 struct notifier_block *nb,
1564 unsigned int list)
1565{
1566 struct devfreq_notifier_devres *ptr;
1567 int ret;
1568
1569 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1570 GFP_KERNEL);
1571 if (!ptr)
1572 return -ENOMEM;
1573
1574 ret = devfreq_register_notifier(devfreq, nb, list);
1575 if (ret) {
1576 devres_free(ptr);
1577 return ret;
1578 }
1579
1580 ptr->devfreq = devfreq;
1581 ptr->nb = nb;
1582 ptr->list = list;
1583 devres_add(dev, ptr);
1584
1585 return 0;
1586}
1587EXPORT_SYMBOL(devm_devfreq_register_notifier);
1588
1589/**
1590 * devm_devfreq_unregister_notifier()
1591 - Resource-managed devfreq_unregister_notifier()
1592 * @dev: The devfreq user device. (parent of devfreq)
1593 * @devfreq: The devfreq object.
1594 * @nb: The notifier block to be unregistered.
1595 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1596 */
1597void devm_devfreq_unregister_notifier(struct device *dev,
1598 struct devfreq *devfreq,
1599 struct notifier_block *nb,
1600 unsigned int list)
1601{
1602 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1603 devm_devfreq_dev_match, devfreq));
1604}
1605EXPORT_SYMBOL(devm_devfreq_unregister_notifier);