blob: 62ead442a87211c04a21ea2032197ee2baea76f3 [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
Chanwoo Choif1d981e2017-10-23 10:32:08 +090032#define MAX(a,b) ((a > b) ? a : b)
33#define MIN(a,b) ((a < b) ? a : b)
34
Nishanth Menon1a1357e2012-10-26 01:50:53 +020035static struct class *devfreq_class;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020036
37/*
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020038 * devfreq core provides delayed work based load monitoring helper
39 * functions. Governors can use these or can implement their own
40 * monitoring mechanism.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020041 */
MyungJoo Hama3c98b82011-10-02 00:19:15 +020042static struct workqueue_struct *devfreq_wq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020043
Nishanth Menon3aa173b2012-10-29 15:01:43 -050044/* The list of all device-devfreq governors */
45static LIST_HEAD(devfreq_governor_list);
MyungJoo Hama3c98b82011-10-02 00:19:15 +020046/* The list of all device-devfreq */
47static LIST_HEAD(devfreq_list);
48static DEFINE_MUTEX(devfreq_list_lock);
49
50/**
51 * find_device_devfreq() - find devfreq struct using device pointer
52 * @dev: device pointer used to lookup device devfreq.
53 *
54 * Search the list of device devfreqs and return the matched device's
55 * devfreq info. devfreq_list_lock should be held by the caller.
56 */
57static struct devfreq *find_device_devfreq(struct device *dev)
58{
59 struct devfreq *tmp_devfreq;
60
Viresh Kumar9348da22015-08-10 11:42:25 +053061 if (IS_ERR_OR_NULL(dev)) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +020062 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
63 return ERR_PTR(-EINVAL);
64 }
65 WARN(!mutex_is_locked(&devfreq_list_lock),
66 "devfreq_list_lock must be locked.");
67
68 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
69 if (tmp_devfreq->dev.parent == dev)
70 return tmp_devfreq;
71 }
72
73 return ERR_PTR(-ENODEV);
74}
75
Chanwoo Choiab8f58a2017-10-23 10:32:06 +090076static unsigned long find_available_min_freq(struct devfreq *devfreq)
77{
78 struct dev_pm_opp *opp;
79 unsigned long min_freq = 0;
80
81 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
82 if (IS_ERR(opp))
83 min_freq = 0;
84 else
85 dev_pm_opp_put(opp);
86
87 return min_freq;
88}
89
90static unsigned long find_available_max_freq(struct devfreq *devfreq)
91{
92 struct dev_pm_opp *opp;
93 unsigned long max_freq = ULONG_MAX;
94
95 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
96 if (IS_ERR(opp))
97 max_freq = 0;
98 else
99 dev_pm_opp_put(opp);
100
101 return max_freq;
102}
103
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200104/**
Jonghwa Leee552bba2012-08-23 20:00:46 +0900105 * devfreq_get_freq_level() - Lookup freq_table for the frequency
106 * @devfreq: the devfreq instance
107 * @freq: the target frequency
108 */
109static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
110{
111 int lev;
112
113 for (lev = 0; lev < devfreq->profile->max_state; lev++)
114 if (freq == devfreq->profile->freq_table[lev])
115 return lev;
116
117 return -EINVAL;
118}
119
Chanwoo Choiea572f82017-10-23 10:32:09 +0900120static int set_freq_table(struct devfreq *devfreq)
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900121{
122 struct devfreq_dev_profile *profile = devfreq->profile;
123 struct dev_pm_opp *opp;
124 unsigned long freq;
125 int i, count;
126
127 /* Initialize the freq_table from OPP table */
128 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
129 if (count <= 0)
Chanwoo Choiea572f82017-10-23 10:32:09 +0900130 return -EINVAL;
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900131
132 profile->max_state = count;
133 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
134 profile->max_state,
135 sizeof(*profile->freq_table),
136 GFP_KERNEL);
137 if (!profile->freq_table) {
138 profile->max_state = 0;
Chanwoo Choiea572f82017-10-23 10:32:09 +0900139 return -ENOMEM;
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900140 }
141
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900142 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
143 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
144 if (IS_ERR(opp)) {
145 devm_kfree(devfreq->dev.parent, profile->freq_table);
146 profile->max_state = 0;
Chanwoo Choiea572f82017-10-23 10:32:09 +0900147 return PTR_ERR(opp);
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900148 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530149 dev_pm_opp_put(opp);
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900150 profile->freq_table[i] = freq;
151 }
Chanwoo Choiea572f82017-10-23 10:32:09 +0900152
153 return 0;
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900154}
155
156/**
Jonghwa Leee552bba2012-08-23 20:00:46 +0900157 * devfreq_update_status() - Update statistics of devfreq behavior
158 * @devfreq: the devfreq instance
159 * @freq: the update target frequency
160 */
Chanwoo Choi30582c22017-01-31 15:38:17 +0900161int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
Jonghwa Leee552bba2012-08-23 20:00:46 +0900162{
Saravana Kannane35d35a2014-02-27 19:38:57 -0800163 int lev, prev_lev, ret = 0;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900164 unsigned long cur_time;
165
Jonghwa Leee552bba2012-08-23 20:00:46 +0900166 cur_time = jiffies;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800167
Tobias Jakobid0563a02016-09-29 14:36:36 +0200168 /* Immediately exit if previous_freq is not initialized yet. */
169 if (!devfreq->previous_freq)
170 goto out;
171
Saravana Kannane35d35a2014-02-27 19:38:57 -0800172 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
173 if (prev_lev < 0) {
174 ret = prev_lev;
175 goto out;
176 }
177
178 devfreq->time_in_state[prev_lev] +=
Jonghwa Leee552bba2012-08-23 20:00:46 +0900179 cur_time - devfreq->last_stat_updated;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800180
181 lev = devfreq_get_freq_level(devfreq, freq);
182 if (lev < 0) {
183 ret = lev;
184 goto out;
185 }
186
187 if (lev != prev_lev) {
Jonghwa Leee552bba2012-08-23 20:00:46 +0900188 devfreq->trans_table[(prev_lev *
189 devfreq->profile->max_state) + lev]++;
190 devfreq->total_trans++;
191 }
Jonghwa Leee552bba2012-08-23 20:00:46 +0900192
Saravana Kannane35d35a2014-02-27 19:38:57 -0800193out:
194 devfreq->last_stat_updated = cur_time;
195 return ret;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900196}
Chanwoo Choi30582c22017-01-31 15:38:17 +0900197EXPORT_SYMBOL(devfreq_update_status);
Jonghwa Leee552bba2012-08-23 20:00:46 +0900198
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500199/**
200 * find_devfreq_governor() - find devfreq governor from name
201 * @name: name of the governor
202 *
203 * Search the list of devfreq governors and return the matched
204 * governor's pointer. devfreq_list_lock should be held by the caller.
205 */
206static struct devfreq_governor *find_devfreq_governor(const char *name)
207{
208 struct devfreq_governor *tmp_governor;
209
Viresh Kumar9348da22015-08-10 11:42:25 +0530210 if (IS_ERR_OR_NULL(name)) {
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500211 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
212 return ERR_PTR(-EINVAL);
213 }
214 WARN(!mutex_is_locked(&devfreq_list_lock),
215 "devfreq_list_lock must be locked.");
216
217 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
218 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
219 return tmp_governor;
220 }
221
222 return ERR_PTR(-ENODEV);
223}
224
Enric Balletbo i Serra23c7b542018-07-04 10:45:50 +0200225/**
226 * try_then_request_governor() - Try to find the governor and request the
227 * module if is not found.
228 * @name: name of the governor
229 *
230 * Search the list of devfreq governors and request the module and try again
231 * if is not found. This can happen when both drivers (the governor driver
232 * and the driver that call devfreq_add_device) are built as modules.
233 * devfreq_list_lock should be held by the caller. Returns the matched
234 * governor's pointer.
235 */
236static struct devfreq_governor *try_then_request_governor(const char *name)
237{
238 struct devfreq_governor *governor;
239 int err = 0;
240
241 if (IS_ERR_OR_NULL(name)) {
242 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
243 return ERR_PTR(-EINVAL);
244 }
245 WARN(!mutex_is_locked(&devfreq_list_lock),
246 "devfreq_list_lock must be locked.");
247
248 governor = find_devfreq_governor(name);
249 if (IS_ERR(governor)) {
250 mutex_unlock(&devfreq_list_lock);
251
252 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
253 DEVFREQ_NAME_LEN))
254 err = request_module("governor_%s", "simpleondemand");
255 else
256 err = request_module("governor_%s", name);
257 /* Restore previous state before return */
258 mutex_lock(&devfreq_list_lock);
259 if (err)
260 return NULL;
261
262 governor = find_devfreq_governor(name);
263 }
264
265 return governor;
266}
267
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900268static int devfreq_notify_transition(struct devfreq *devfreq,
269 struct devfreq_freqs *freqs, unsigned int state)
270{
271 if (!devfreq)
272 return -EINVAL;
273
274 switch (state) {
275 case DEVFREQ_PRECHANGE:
276 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
277 DEVFREQ_PRECHANGE, freqs);
278 break;
279
280 case DEVFREQ_POSTCHANGE:
281 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
282 DEVFREQ_POSTCHANGE, freqs);
283 break;
284 default:
285 return -EINVAL;
286 }
287
288 return 0;
289}
290
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200291/* Load monitoring helper functions for governors use */
292
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200293/**
294 * update_devfreq() - Reevaluate the device and configure frequency.
295 * @devfreq: the devfreq instance.
296 *
297 * Note: Lock devfreq->lock before calling update_devfreq
298 * This function is exported for governors.
299 */
300int update_devfreq(struct devfreq *devfreq)
301{
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900302 struct devfreq_freqs freqs;
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900303 unsigned long freq, cur_freq, min_freq, max_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200304 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100305 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200306
307 if (!mutex_is_locked(&devfreq->lock)) {
308 WARN(true, "devfreq->lock must be locked by the caller.\n");
309 return -EINVAL;
310 }
311
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500312 if (!devfreq->governor)
313 return -EINVAL;
314
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200315 /* Reevaluate the proper frequency */
316 err = devfreq->governor->get_target_freq(devfreq, &freq);
317 if (err)
318 return err;
319
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100320 /*
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900321 * Adjust the frequency with user freq, QoS and available freq.
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100322 *
Javi Merinod3b7e172015-08-14 18:57:00 +0100323 * List from the highest priority
324 * max_freq
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100325 * min_freq
326 */
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900327 max_freq = MIN(devfreq->scaling_max_freq, devfreq->max_freq);
328 min_freq = MAX(devfreq->scaling_min_freq, devfreq->min_freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100329
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900330 if (min_freq && freq < min_freq) {
331 freq = min_freq;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100332 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
333 }
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900334 if (max_freq && freq > max_freq) {
335 freq = max_freq;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100336 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
337 }
338
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900339 if (devfreq->profile->get_cur_freq)
340 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
341 else
342 cur_freq = devfreq->previous_freq;
343
344 freqs.old = cur_freq;
345 freqs.new = freq;
346 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
347
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100348 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
Chanwoo Choi0d371892016-06-23 11:18:43 +0900349 if (err) {
350 freqs.new = cur_freq;
351 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200352 return err;
Chanwoo Choi0d371892016-06-23 11:18:43 +0900353 }
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200354
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900355 freqs.new = freq;
356 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
357
Chanwoo Choiccc4c3b2017-10-23 10:32:11 +0900358 if (devfreq_update_status(devfreq, freq))
359 dev_err(&devfreq->dev,
360 "Couldn't update frequency transition information.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +0900361
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200362 devfreq->previous_freq = freq;
363 return err;
364}
Nishanth Menon2df50212012-10-29 15:01:42 -0500365EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200366
367/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200368 * devfreq_monitor() - Periodically poll devfreq objects.
369 * @work: the work struct used to run devfreq_monitor periodically.
370 *
371 */
372static void devfreq_monitor(struct work_struct *work)
373{
374 int err;
375 struct devfreq *devfreq = container_of(work,
376 struct devfreq, work.work);
377
378 mutex_lock(&devfreq->lock);
379 err = update_devfreq(devfreq);
380 if (err)
381 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
382
383 queue_delayed_work(devfreq_wq, &devfreq->work,
384 msecs_to_jiffies(devfreq->profile->polling_ms));
385 mutex_unlock(&devfreq->lock);
386}
387
388/**
389 * devfreq_monitor_start() - Start load monitoring of devfreq instance
390 * @devfreq: the devfreq instance.
391 *
392 * Helper function for starting devfreq device load monitoing. By
393 * default delayed work based monitoring is supported. Function
394 * to be called from governor in response to DEVFREQ_GOV_START
395 * event when device is added to devfreq framework.
396 */
397void devfreq_monitor_start(struct devfreq *devfreq)
398{
399 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
400 if (devfreq->profile->polling_ms)
401 queue_delayed_work(devfreq_wq, &devfreq->work,
402 msecs_to_jiffies(devfreq->profile->polling_ms));
403}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100404EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200405
406/**
407 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
408 * @devfreq: the devfreq instance.
409 *
410 * Helper function to stop devfreq device load monitoing. Function
411 * to be called from governor in response to DEVFREQ_GOV_STOP
412 * event when device is removed from devfreq framework.
413 */
414void devfreq_monitor_stop(struct devfreq *devfreq)
415{
416 cancel_delayed_work_sync(&devfreq->work);
417}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100418EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200419
420/**
421 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
422 * @devfreq: the devfreq instance.
423 *
424 * Helper function to suspend devfreq device load monitoing. Function
425 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
426 * event or when polling interval is set to zero.
427 *
428 * Note: Though this function is same as devfreq_monitor_stop(),
429 * intentionally kept separate to provide hooks for collecting
430 * transition statistics.
431 */
432void devfreq_monitor_suspend(struct devfreq *devfreq)
433{
434 mutex_lock(&devfreq->lock);
435 if (devfreq->stop_polling) {
436 mutex_unlock(&devfreq->lock);
437 return;
438 }
439
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530440 devfreq_update_status(devfreq, devfreq->previous_freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200441 devfreq->stop_polling = true;
442 mutex_unlock(&devfreq->lock);
443 cancel_delayed_work_sync(&devfreq->work);
444}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100445EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200446
447/**
448 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
449 * @devfreq: the devfreq instance.
450 *
451 * Helper function to resume devfreq device load monitoing. Function
452 * to be called from governor in response to DEVFREQ_GOV_RESUME
453 * event or when polling interval is set to non-zero.
454 */
455void devfreq_monitor_resume(struct devfreq *devfreq)
456{
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530457 unsigned long freq;
458
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200459 mutex_lock(&devfreq->lock);
460 if (!devfreq->stop_polling)
461 goto out;
462
463 if (!delayed_work_pending(&devfreq->work) &&
464 devfreq->profile->polling_ms)
465 queue_delayed_work(devfreq_wq, &devfreq->work,
466 msecs_to_jiffies(devfreq->profile->polling_ms));
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530467
468 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200469 devfreq->stop_polling = false;
470
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530471 if (devfreq->profile->get_cur_freq &&
472 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
473 devfreq->previous_freq = freq;
474
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200475out:
476 mutex_unlock(&devfreq->lock);
477}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100478EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200479
480/**
481 * devfreq_interval_update() - Update device devfreq monitoring interval
482 * @devfreq: the devfreq instance.
483 * @delay: new polling interval to be set.
484 *
485 * Helper function to set new load monitoring polling interval. Function
486 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
487 */
488void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
489{
490 unsigned int cur_delay = devfreq->profile->polling_ms;
491 unsigned int new_delay = *delay;
492
493 mutex_lock(&devfreq->lock);
494 devfreq->profile->polling_ms = new_delay;
495
496 if (devfreq->stop_polling)
497 goto out;
498
499 /* if new delay is zero, stop polling */
500 if (!new_delay) {
501 mutex_unlock(&devfreq->lock);
502 cancel_delayed_work_sync(&devfreq->work);
503 return;
504 }
505
506 /* if current delay is zero, start polling with new delay */
507 if (!cur_delay) {
508 queue_delayed_work(devfreq_wq, &devfreq->work,
509 msecs_to_jiffies(devfreq->profile->polling_ms));
510 goto out;
511 }
512
513 /* if current delay is greater than new delay, restart polling */
514 if (cur_delay > new_delay) {
515 mutex_unlock(&devfreq->lock);
516 cancel_delayed_work_sync(&devfreq->work);
517 mutex_lock(&devfreq->lock);
518 if (!devfreq->stop_polling)
519 queue_delayed_work(devfreq_wq, &devfreq->work,
520 msecs_to_jiffies(devfreq->profile->polling_ms));
521 }
522out:
523 mutex_unlock(&devfreq->lock);
524}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100525EXPORT_SYMBOL(devfreq_interval_update);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200526
527/**
528 * devfreq_notifier_call() - Notify that the device frequency requirements
529 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200530 * @nb: the notifier_block (supposed to be devfreq->nb)
531 * @type: not used
532 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200533 *
534 * Called by a notifier that uses devfreq->nb.
535 */
536static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
537 void *devp)
538{
539 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
540 int ret;
541
542 mutex_lock(&devfreq->lock);
Chanwoo Choif1d981e2017-10-23 10:32:08 +0900543
544 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
545 if (!devfreq->scaling_min_freq) {
546 mutex_unlock(&devfreq->lock);
547 return -EINVAL;
548 }
549
550 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
551 if (!devfreq->scaling_max_freq) {
552 mutex_unlock(&devfreq->lock);
553 return -EINVAL;
554 }
555
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200556 ret = update_devfreq(devfreq);
557 mutex_unlock(&devfreq->lock);
558
559 return ret;
560}
561
562/**
Chanwoo Choi29b69682017-01-31 15:38:18 +0900563 * devfreq_dev_release() - Callback for struct device to release the device.
564 * @dev: the devfreq device
565 *
566 * Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200567 */
Chanwoo Choi29b69682017-01-31 15:38:18 +0900568static void devfreq_dev_release(struct device *dev)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200569{
Chanwoo Choi29b69682017-01-31 15:38:18 +0900570 struct devfreq *devfreq = to_devfreq(dev);
571
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200572 mutex_lock(&devfreq_list_lock);
573 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
574 mutex_unlock(&devfreq_list_lock);
575 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200576 return;
577 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200578 list_del(&devfreq->node);
579 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200580
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500581 if (devfreq->governor)
582 devfreq->governor->event_handler(devfreq,
583 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200584
585 if (devfreq->profile->exit)
586 devfreq->profile->exit(devfreq->dev.parent);
587
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200588 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200589 kfree(devfreq);
590}
591
592/**
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200593 * devfreq_add_device() - Add devfreq feature to the device
594 * @dev: the device to add devfreq feature.
595 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500596 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200597 * @data: private data for the governor. The devfreq framework does not
598 * touch this value.
599 */
600struct devfreq *devfreq_add_device(struct device *dev,
601 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500602 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200603 void *data)
604{
605 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500606 struct devfreq_governor *governor;
Chanwoo Choi4585fbc2017-01-31 16:47:57 +0900607 static atomic_t devfreq_no = ATOMIC_INIT(-1);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200608 int err = 0;
609
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500610 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200611 dev_err(dev, "%s: Invalid parameters.\n", __func__);
612 return ERR_PTR(-EINVAL);
613 }
614
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200615 mutex_lock(&devfreq_list_lock);
616 devfreq = find_device_devfreq(dev);
617 mutex_unlock(&devfreq_list_lock);
618 if (!IS_ERR(devfreq)) {
Chanwoo Choi9d0109b2016-11-19 22:47:36 +0900619 dev_err(dev, "%s: Unable to create devfreq for the device.\n",
620 __func__);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200621 err = -EINVAL;
622 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200623 }
624
625 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
626 if (!devfreq) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200627 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100628 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200629 }
630
631 mutex_init(&devfreq->lock);
632 mutex_lock(&devfreq->lock);
633 devfreq->dev.parent = dev;
634 devfreq->dev.class = devfreq_class;
635 devfreq->dev.release = devfreq_dev_release;
636 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500637 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200638 devfreq->previous_freq = profile->initial_freq;
Lukasz Luba8d39fc082016-05-31 11:25:09 +0100639 devfreq->last_status.current_frequency = profile->initial_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200640 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200641 devfreq->nb.notifier_call = devfreq_notifier_call;
642
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900643 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
644 mutex_unlock(&devfreq->lock);
Chanwoo Choiea572f82017-10-23 10:32:09 +0900645 err = set_freq_table(devfreq);
646 if (err < 0)
647 goto err_out;
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900648 mutex_lock(&devfreq->lock);
649 }
650
Matthias Kaehlcke2c2cb1e62018-05-25 13:30:33 -0700651 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
652 if (!devfreq->scaling_min_freq) {
Chanwoo Choiab8f58a2017-10-23 10:32:06 +0900653 mutex_unlock(&devfreq->lock);
654 err = -EINVAL;
655 goto err_dev;
656 }
Matthias Kaehlcke2c2cb1e62018-05-25 13:30:33 -0700657 devfreq->min_freq = devfreq->scaling_min_freq;
Chanwoo Choiab8f58a2017-10-23 10:32:06 +0900658
Matthias Kaehlcke2c2cb1e62018-05-25 13:30:33 -0700659 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
660 if (!devfreq->scaling_max_freq) {
Chanwoo Choiab8f58a2017-10-23 10:32:06 +0900661 mutex_unlock(&devfreq->lock);
662 err = -EINVAL;
663 goto err_dev;
664 }
Matthias Kaehlcke2c2cb1e62018-05-25 13:30:33 -0700665 devfreq->max_freq = devfreq->scaling_max_freq;
Chanwoo Choiab8f58a2017-10-23 10:32:06 +0900666
Chanwoo Choi4585fbc2017-01-31 16:47:57 +0900667 dev_set_name(&devfreq->dev, "devfreq%d",
668 atomic_inc_return(&devfreq_no));
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200669 err = device_register(&devfreq->dev);
670 if (err) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200671 mutex_unlock(&devfreq->lock);
Arvind Yadav2d803dc2018-03-30 17:14:03 +0530672 put_device(&devfreq->dev);
673 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200674 }
675
Kees Cooka86854d2018-06-12 14:07:58 -0700676 devfreq->trans_table =
677 devm_kzalloc(&devfreq->dev,
678 array3_size(sizeof(unsigned int),
679 devfreq->profile->max_state,
680 devfreq->profile->max_state),
681 GFP_KERNEL);
682 devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900683 devfreq->profile->max_state,
Kees Cooka86854d2018-06-12 14:07:58 -0700684 sizeof(unsigned long),
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900685 GFP_KERNEL);
686 devfreq->last_stat_updated = jiffies;
687
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900688 srcu_init_notifier_head(&devfreq->transition_notifier_list);
689
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200690 mutex_unlock(&devfreq->lock);
691
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200692 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200693
Enric Balletbo i Serra23c7b542018-07-04 10:45:50 +0200694 governor = try_then_request_governor(devfreq->governor_name);
Chanwoo Choi73613b12016-12-28 20:52:35 +0900695 if (IS_ERR(governor)) {
696 dev_err(dev, "%s: Unable to find governor for the device\n",
697 __func__);
698 err = PTR_ERR(governor);
699 goto err_init;
700 }
701
702 devfreq->governor = governor;
703 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
704 NULL);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200705 if (err) {
706 dev_err(dev, "%s: Unable to start governor for the device\n",
707 __func__);
708 goto err_init;
709 }
Enric Balletbo i Serra23c7b542018-07-04 10:45:50 +0200710
711 list_add(&devfreq->node, &devfreq_list);
712
Axel Lin0f376c92016-09-29 10:13:28 +0800713 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200714
Axel Lin3f19f082011-11-15 21:59:09 +0100715 return devfreq;
716
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200717err_init:
Axel Lin0f376c92016-09-29 10:13:28 +0800718 mutex_unlock(&devfreq_list_lock);
719
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200720 device_unregister(&devfreq->dev);
Arvind Yadav2d803dc2018-03-30 17:14:03 +0530721 devfreq = NULL;
Chanwoo Choi9e14de12017-08-24 10:42:48 +0900722err_dev:
723 if (devfreq)
724 kfree(devfreq);
Axel Lin3f19f082011-11-15 21:59:09 +0100725err_out:
726 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200727}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200728EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200729
730/**
731 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200732 * @devfreq: the devfreq instance to be removed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900733 *
734 * The opposite of devfreq_add_device().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200735 */
736int devfreq_remove_device(struct devfreq *devfreq)
737{
738 if (!devfreq)
739 return -EINVAL;
740
Chanwoo Choi585fc832014-05-09 16:43:07 +0900741 device_unregister(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200742
743 return 0;
744}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200745EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200746
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900747static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
748{
749 struct devfreq **r = res;
750
751 if (WARN_ON(!r || !*r))
752 return 0;
753
754 return *r == data;
755}
756
757static void devm_devfreq_dev_release(struct device *dev, void *res)
758{
759 devfreq_remove_device(*(struct devfreq **)res);
760}
761
762/**
763 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
764 * @dev: the device to add devfreq feature.
765 * @profile: device-specific profile to run devfreq.
766 * @governor_name: name of the policy to choose frequency.
767 * @data: private data for the governor. The devfreq framework does not
768 * touch this value.
769 *
770 * This function manages automatically the memory of devfreq device using device
771 * resource management and simplify the free operation for memory of devfreq
772 * device.
773 */
774struct devfreq *devm_devfreq_add_device(struct device *dev,
775 struct devfreq_dev_profile *profile,
776 const char *governor_name,
777 void *data)
778{
779 struct devfreq **ptr, *devfreq;
780
781 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
782 if (!ptr)
783 return ERR_PTR(-ENOMEM);
784
785 devfreq = devfreq_add_device(dev, profile, governor_name, data);
786 if (IS_ERR(devfreq)) {
787 devres_free(ptr);
Bjorn Anderssond1bf2d302017-11-05 21:27:41 -0800788 return devfreq;
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900789 }
790
791 *ptr = devfreq;
792 devres_add(dev, ptr);
793
794 return devfreq;
795}
796EXPORT_SYMBOL(devm_devfreq_add_device);
797
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900798#ifdef CONFIG_OF
799/*
800 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
801 * @dev - instance to the given device
802 * @index - index into list of devfreq
803 *
804 * return the instance of devfreq device
805 */
806struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
807{
808 struct device_node *node;
809 struct devfreq *devfreq;
810
811 if (!dev)
812 return ERR_PTR(-EINVAL);
813
814 if (!dev->of_node)
815 return ERR_PTR(-EINVAL);
816
817 node = of_parse_phandle(dev->of_node, "devfreq", index);
818 if (!node)
819 return ERR_PTR(-ENODEV);
820
821 mutex_lock(&devfreq_list_lock);
822 list_for_each_entry(devfreq, &devfreq_list, node) {
823 if (devfreq->dev.parent
824 && devfreq->dev.parent->of_node == node) {
825 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800826 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900827 return devfreq;
828 }
829 }
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
833 return ERR_PTR(-EPROBE_DEFER);
834}
835#else
836struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
837{
838 return ERR_PTR(-ENODEV);
839}
840#endif /* CONFIG_OF */
841EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
842
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900843/**
844 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
845 * @dev: the device to add devfreq feature.
846 * @devfreq: the devfreq instance to be removed
847 */
848void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
849{
850 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
851 devm_devfreq_dev_match, devfreq));
852}
853EXPORT_SYMBOL(devm_devfreq_remove_device);
854
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200855/**
856 * devfreq_suspend_device() - Suspend devfreq of a device.
857 * @devfreq: the devfreq instance to be suspended
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900858 *
859 * This function is intended to be called by the pm callbacks
860 * (e.g., runtime_suspend, suspend) of the device driver that
861 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200862 */
863int devfreq_suspend_device(struct devfreq *devfreq)
864{
865 if (!devfreq)
866 return -EINVAL;
867
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500868 if (!devfreq->governor)
869 return 0;
870
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200871 return devfreq->governor->event_handler(devfreq,
872 DEVFREQ_GOV_SUSPEND, NULL);
873}
874EXPORT_SYMBOL(devfreq_suspend_device);
875
876/**
877 * devfreq_resume_device() - Resume devfreq of a device.
878 * @devfreq: the devfreq instance to be resumed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900879 *
880 * This function is intended to be called by the pm callbacks
881 * (e.g., runtime_resume, resume) of the device driver that
882 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200883 */
884int devfreq_resume_device(struct devfreq *devfreq)
885{
886 if (!devfreq)
887 return -EINVAL;
888
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500889 if (!devfreq->governor)
890 return 0;
891
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200892 return devfreq->governor->event_handler(devfreq,
893 DEVFREQ_GOV_RESUME, NULL);
894}
895EXPORT_SYMBOL(devfreq_resume_device);
896
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500897/**
898 * devfreq_add_governor() - Add devfreq governor
899 * @governor: the devfreq governor to be added
900 */
901int devfreq_add_governor(struct devfreq_governor *governor)
902{
903 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500904 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500905 int err = 0;
906
907 if (!governor) {
908 pr_err("%s: Invalid parameters.\n", __func__);
909 return -EINVAL;
910 }
911
912 mutex_lock(&devfreq_list_lock);
913 g = find_devfreq_governor(governor->name);
914 if (!IS_ERR(g)) {
915 pr_err("%s: governor %s already registered\n", __func__,
916 g->name);
917 err = -EINVAL;
918 goto err_out;
919 }
920
921 list_add(&governor->node, &devfreq_governor_list);
922
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500923 list_for_each_entry(devfreq, &devfreq_list, node) {
924 int ret = 0;
925 struct device *dev = devfreq->dev.parent;
926
927 if (!strncmp(devfreq->governor_name, governor->name,
928 DEVFREQ_NAME_LEN)) {
929 /* The following should never occur */
930 if (devfreq->governor) {
931 dev_warn(dev,
932 "%s: Governor %s already present\n",
933 __func__, devfreq->governor->name);
934 ret = devfreq->governor->event_handler(devfreq,
935 DEVFREQ_GOV_STOP, NULL);
936 if (ret) {
937 dev_warn(dev,
938 "%s: Governor %s stop = %d\n",
939 __func__,
940 devfreq->governor->name, ret);
941 }
942 /* Fall through */
943 }
944 devfreq->governor = governor;
945 ret = devfreq->governor->event_handler(devfreq,
946 DEVFREQ_GOV_START, NULL);
947 if (ret) {
948 dev_warn(dev, "%s: Governor %s start=%d\n",
949 __func__, devfreq->governor->name,
950 ret);
951 }
952 }
953 }
954
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500955err_out:
956 mutex_unlock(&devfreq_list_lock);
957
958 return err;
959}
960EXPORT_SYMBOL(devfreq_add_governor);
961
962/**
MyungJoo Hambafeb422016-11-09 10:29:14 +0900963 * devfreq_remove_governor() - Remove devfreq feature from a device.
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500964 * @governor: the devfreq governor to be removed
965 */
966int devfreq_remove_governor(struct devfreq_governor *governor)
967{
968 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500969 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500970 int err = 0;
971
972 if (!governor) {
973 pr_err("%s: Invalid parameters.\n", __func__);
974 return -EINVAL;
975 }
976
977 mutex_lock(&devfreq_list_lock);
978 g = find_devfreq_governor(governor->name);
979 if (IS_ERR(g)) {
980 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamatb9e1c8e2012-11-21 10:36:13 +0530981 governor->name);
Sachin Kamatf9c08e22012-11-21 10:36:14 +0530982 err = PTR_ERR(g);
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500983 goto err_out;
984 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500985 list_for_each_entry(devfreq, &devfreq_list, node) {
986 int ret;
987 struct device *dev = devfreq->dev.parent;
988
989 if (!strncmp(devfreq->governor_name, governor->name,
990 DEVFREQ_NAME_LEN)) {
991 /* we should have a devfreq governor! */
992 if (!devfreq->governor) {
993 dev_warn(dev, "%s: Governor %s NOT present\n",
994 __func__, governor->name);
995 continue;
996 /* Fall through */
997 }
998 ret = devfreq->governor->event_handler(devfreq,
999 DEVFREQ_GOV_STOP, NULL);
1000 if (ret) {
1001 dev_warn(dev, "%s: Governor %s stop=%d\n",
1002 __func__, devfreq->governor->name,
1003 ret);
1004 }
1005 devfreq->governor = NULL;
1006 }
1007 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -05001008
1009 list_del(&governor->node);
1010err_out:
1011 mutex_unlock(&devfreq_list_lock);
1012
1013 return err;
1014}
1015EXPORT_SYMBOL(devfreq_remove_governor);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001016
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001017static ssize_t governor_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001018 struct device_attribute *attr, char *buf)
1019{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001020 if (!to_devfreq(dev)->governor)
1021 return -EINVAL;
1022
MyungJoo Ham9005b652011-10-02 00:19:28 +02001023 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1024}
1025
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001026static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001027 const char *buf, size_t count)
1028{
1029 struct devfreq *df = to_devfreq(dev);
1030 int ret;
1031 char str_governor[DEVFREQ_NAME_LEN + 1];
1032 struct devfreq_governor *governor;
1033
1034 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1035 if (ret != 1)
1036 return -EINVAL;
1037
1038 mutex_lock(&devfreq_list_lock);
Enric Balletbo i Serra23c7b542018-07-04 10:45:50 +02001039 governor = try_then_request_governor(str_governor);
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001040 if (IS_ERR(governor)) {
1041 ret = PTR_ERR(governor);
1042 goto out;
1043 }
Tobias Jakobi14a21e72015-09-21 20:23:52 +02001044 if (df->governor == governor) {
1045 ret = 0;
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001046 goto out;
Gustavo A. R. Silva63f1e052017-12-06 14:20:15 -06001047 } else if ((df->governor && df->governor->immutable) ||
1048 governor->immutable) {
Chanwoo Choibcf23c72017-01-31 15:38:16 +09001049 ret = -EINVAL;
1050 goto out;
Tobias Jakobi14a21e72015-09-21 20:23:52 +02001051 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001052
1053 if (df->governor) {
1054 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1055 if (ret) {
1056 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1057 __func__, df->governor->name, ret);
1058 goto out;
1059 }
1060 }
1061 df->governor = governor;
1062 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1063 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1064 if (ret)
1065 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1066 __func__, df->governor->name, ret);
1067out:
1068 mutex_unlock(&devfreq_list_lock);
1069
1070 if (!ret)
1071 ret = count;
1072 return ret;
1073}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001074static DEVICE_ATTR_RW(governor);
1075
1076static ssize_t available_governors_show(struct device *d,
1077 struct device_attribute *attr,
1078 char *buf)
Nishanth Menon50a5b332012-10-29 15:01:48 -05001079{
Chanwoo Choibcf23c72017-01-31 15:38:16 +09001080 struct devfreq *df = to_devfreq(d);
Nishanth Menon50a5b332012-10-29 15:01:48 -05001081 ssize_t count = 0;
1082
1083 mutex_lock(&devfreq_list_lock);
Chanwoo Choibcf23c72017-01-31 15:38:16 +09001084
1085 /*
1086 * The devfreq with immutable governor (e.g., passive) shows
1087 * only own governor.
1088 */
1089 if (df->governor->immutable) {
1090 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1091 "%s ", df->governor_name);
1092 /*
1093 * The devfreq device shows the registered governor except for
1094 * immutable governors such as passive governor .
1095 */
1096 } else {
1097 struct devfreq_governor *governor;
1098
1099 list_for_each_entry(governor, &devfreq_governor_list, node) {
1100 if (governor->immutable)
1101 continue;
1102 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1103 "%s ", governor->name);
1104 }
1105 }
1106
Nishanth Menon50a5b332012-10-29 15:01:48 -05001107 mutex_unlock(&devfreq_list_lock);
1108
1109 /* Truncate the trailing space */
1110 if (count)
1111 count--;
1112
1113 count += sprintf(&buf[count], "\n");
1114
1115 return count;
1116}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001117static DEVICE_ATTR_RO(available_governors);
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001118
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001119static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1120 char *buf)
MyungJoo Ham9005b652011-10-02 00:19:28 +02001121{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001122 unsigned long freq;
1123 struct devfreq *devfreq = to_devfreq(dev);
1124
1125 if (devfreq->profile->get_cur_freq &&
1126 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
Chanwoo Choi9d0109b2016-11-19 22:47:36 +09001127 return sprintf(buf, "%lu\n", freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001128
1129 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1130}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001131static DEVICE_ATTR_RO(cur_freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001132
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001133static ssize_t target_freq_show(struct device *dev,
1134 struct device_attribute *attr, char *buf)
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001135{
MyungJoo Ham9005b652011-10-02 00:19:28 +02001136 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1137}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001138static DEVICE_ATTR_RO(target_freq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001139
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001140static ssize_t polling_interval_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001141 struct device_attribute *attr, char *buf)
1142{
1143 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1144}
1145
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001146static ssize_t polling_interval_store(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001147 struct device_attribute *attr,
1148 const char *buf, size_t count)
1149{
1150 struct devfreq *df = to_devfreq(dev);
1151 unsigned int value;
1152 int ret;
1153
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001154 if (!df->governor)
1155 return -EINVAL;
1156
MyungJoo Ham9005b652011-10-02 00:19:28 +02001157 ret = sscanf(buf, "%u", &value);
1158 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001159 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +02001160
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001161 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001162 ret = count;
1163
MyungJoo Ham9005b652011-10-02 00:19:28 +02001164 return ret;
1165}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001166static DEVICE_ATTR_RW(polling_interval);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001167
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001168static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001169 const char *buf, size_t count)
1170{
1171 struct devfreq *df = to_devfreq(dev);
1172 unsigned long value;
1173 int ret;
1174 unsigned long max;
1175
1176 ret = sscanf(buf, "%lu", &value);
1177 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001178 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001179
1180 mutex_lock(&df->lock);
1181 max = df->max_freq;
1182 if (value && max && value > max) {
1183 ret = -EINVAL;
1184 goto unlock;
1185 }
1186
1187 df->min_freq = value;
1188 update_devfreq(df);
1189 ret = count;
1190unlock:
1191 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001192 return ret;
1193}
1194
Chanwoo Choi1051e2c2017-10-23 10:32:07 +09001195static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1196 char *buf)
1197{
Chanwoo Choif1d981e2017-10-23 10:32:08 +09001198 struct devfreq *df = to_devfreq(dev);
1199
1200 return sprintf(buf, "%lu\n", MAX(df->scaling_min_freq, df->min_freq));
Chanwoo Choi1051e2c2017-10-23 10:32:07 +09001201}
1202
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001203static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001204 const char *buf, size_t count)
1205{
1206 struct devfreq *df = to_devfreq(dev);
1207 unsigned long value;
1208 int ret;
1209 unsigned long min;
1210
1211 ret = sscanf(buf, "%lu", &value);
1212 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001213 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001214
1215 mutex_lock(&df->lock);
1216 min = df->min_freq;
1217 if (value && min && value < min) {
1218 ret = -EINVAL;
1219 goto unlock;
1220 }
1221
1222 df->max_freq = value;
1223 update_devfreq(df);
1224 ret = count;
1225unlock:
1226 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001227 return ret;
1228}
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001229static DEVICE_ATTR_RW(min_freq);
Chanwoo Choi1051e2c2017-10-23 10:32:07 +09001230
1231static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1232 char *buf)
1233{
Chanwoo Choif1d981e2017-10-23 10:32:08 +09001234 struct devfreq *df = to_devfreq(dev);
1235
1236 return sprintf(buf, "%lu\n", MIN(df->scaling_max_freq, df->max_freq));
Chanwoo Choi1051e2c2017-10-23 10:32:07 +09001237}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001238static DEVICE_ATTR_RW(max_freq);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001239
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001240static ssize_t available_frequencies_show(struct device *d,
1241 struct device_attribute *attr,
1242 char *buf)
Nishanth Menond287de82012-10-25 19:48:59 -05001243{
1244 struct devfreq *df = to_devfreq(d);
Nishanth Menond287de82012-10-25 19:48:59 -05001245 ssize_t count = 0;
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001246 int i;
Nishanth Menond287de82012-10-25 19:48:59 -05001247
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001248 mutex_lock(&df->lock);
Nishanth Menond287de82012-10-25 19:48:59 -05001249
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001250 for (i = 0; i < df->profile->max_state; i++)
Nishanth Menond287de82012-10-25 19:48:59 -05001251 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001252 "%lu ", df->profile->freq_table[i]);
Nishanth Menond287de82012-10-25 19:48:59 -05001253
Chanwoo Choi416b46a2017-10-23 10:32:10 +09001254 mutex_unlock(&df->lock);
Nishanth Menond287de82012-10-25 19:48:59 -05001255 /* Truncate the trailing space */
1256 if (count)
1257 count--;
1258
1259 count += sprintf(&buf[count], "\n");
1260
1261 return count;
1262}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001263static DEVICE_ATTR_RO(available_frequencies);
Nishanth Menond287de82012-10-25 19:48:59 -05001264
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001265static ssize_t trans_stat_show(struct device *dev,
1266 struct device_attribute *attr, char *buf)
Jonghwa Leee552bba2012-08-23 20:00:46 +09001267{
1268 struct devfreq *devfreq = to_devfreq(dev);
1269 ssize_t len;
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301270 int i, j;
Jonghwa Leee552bba2012-08-23 20:00:46 +09001271 unsigned int max_state = devfreq->profile->max_state;
1272
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301273 if (!devfreq->stop_polling &&
1274 devfreq_update_status(devfreq, devfreq->previous_freq))
Jonghwa Leee552bba2012-08-23 20:00:46 +09001275 return 0;
MyungJoo Ham34bd3222015-11-23 15:45:36 +09001276 if (max_state == 0)
1277 return sprintf(buf, "Not Supported.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001278
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001279 len = sprintf(buf, " From : To\n");
1280 len += sprintf(buf + len, " :");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001281 for (i = 0; i < max_state; i++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001282 len += sprintf(buf + len, "%10lu",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001283 devfreq->profile->freq_table[i]);
1284
1285 len += sprintf(buf + len, " time(ms)\n");
1286
1287 for (i = 0; i < max_state; i++) {
1288 if (devfreq->profile->freq_table[i]
1289 == devfreq->previous_freq) {
1290 len += sprintf(buf + len, "*");
1291 } else {
1292 len += sprintf(buf + len, " ");
1293 }
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001294 len += sprintf(buf + len, "%10lu:",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001295 devfreq->profile->freq_table[i]);
1296 for (j = 0; j < max_state; j++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001297 len += sprintf(buf + len, "%10u",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001298 devfreq->trans_table[(i * max_state) + j]);
1299 len += sprintf(buf + len, "%10u\n",
1300 jiffies_to_msecs(devfreq->time_in_state[i]));
1301 }
1302
1303 len += sprintf(buf + len, "Total transition : %u\n",
1304 devfreq->total_trans);
1305 return len;
1306}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001307static DEVICE_ATTR_RO(trans_stat);
Jonghwa Leee552bba2012-08-23 20:00:46 +09001308
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001309static struct attribute *devfreq_attrs[] = {
1310 &dev_attr_governor.attr,
1311 &dev_attr_available_governors.attr,
1312 &dev_attr_cur_freq.attr,
1313 &dev_attr_available_frequencies.attr,
1314 &dev_attr_target_freq.attr,
1315 &dev_attr_polling_interval.attr,
1316 &dev_attr_min_freq.attr,
1317 &dev_attr_max_freq.attr,
1318 &dev_attr_trans_stat.attr,
1319 NULL,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001320};
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001321ATTRIBUTE_GROUPS(devfreq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001322
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001323static int __init devfreq_init(void)
1324{
1325 devfreq_class = class_create(THIS_MODULE, "devfreq");
1326 if (IS_ERR(devfreq_class)) {
1327 pr_err("%s: couldn't create class\n", __FILE__);
1328 return PTR_ERR(devfreq_class);
1329 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001330
1331 devfreq_wq = create_freezable_workqueue("devfreq_wq");
Dan Carpenterea7f4542013-08-15 10:55:10 +03001332 if (!devfreq_wq) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001333 class_destroy(devfreq_class);
1334 pr_err("%s: couldn't create workqueue\n", __FILE__);
Dan Carpenterea7f4542013-08-15 10:55:10 +03001335 return -ENOMEM;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001336 }
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001337 devfreq_class->dev_groups = devfreq_groups;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001338
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001339 return 0;
1340}
1341subsys_initcall(devfreq_init);
1342
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001343/*
Masahiro Yamada4091fb92017-02-27 14:29:56 -08001344 * The following are helper functions for devfreq user device drivers with
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001345 * OPP framework.
1346 */
1347
1348/**
1349 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1350 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001351 * @dev: The devfreq user device. (parent of devfreq)
1352 * @freq: The frequency given to target function
1353 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001354 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301355 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1356 * use.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001357 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001358struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1359 unsigned long *freq,
1360 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001361{
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001362 struct dev_pm_opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001363
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001364 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1365 /* The freq is an upper bound. opp should be lower */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001366 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001367
1368 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001369 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001370 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001371 } else {
1372 /* The freq is an lower bound. opp should be higher */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001373 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001374
1375 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001376 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001377 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001378 }
1379
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001380 return opp;
1381}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001382EXPORT_SYMBOL(devfreq_recommended_opp);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001383
1384/**
1385 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1386 * for any changes in the OPP availability
1387 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001388 * @dev: The devfreq user device. (parent of devfreq)
1389 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001390 */
1391int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1392{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301393 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001394}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001395EXPORT_SYMBOL(devfreq_register_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001396
1397/**
1398 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1399 * notified for any changes in the OPP
1400 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001401 * @dev: The devfreq user device. (parent of devfreq)
1402 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001403 *
1404 * At exit() callback of devfreq_dev_profile, this must be included if
1405 * devfreq_recommended_opp is used.
1406 */
1407int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1408{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301409 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001410}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001411EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001412
Chanwoo Choid5b040d2014-05-09 16:43:09 +09001413static void devm_devfreq_opp_release(struct device *dev, void *res)
1414{
1415 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1416}
1417
1418/**
1419 * devm_ devfreq_register_opp_notifier()
1420 * - Resource-managed devfreq_register_opp_notifier()
1421 * @dev: The devfreq user device. (parent of devfreq)
1422 * @devfreq: The devfreq object.
1423 */
1424int devm_devfreq_register_opp_notifier(struct device *dev,
1425 struct devfreq *devfreq)
1426{
1427 struct devfreq **ptr;
1428 int ret;
1429
1430 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1431 if (!ptr)
1432 return -ENOMEM;
1433
1434 ret = devfreq_register_opp_notifier(dev, devfreq);
1435 if (ret) {
1436 devres_free(ptr);
1437 return ret;
1438 }
1439
1440 *ptr = devfreq;
1441 devres_add(dev, ptr);
1442
1443 return 0;
1444}
1445EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1446
1447/**
1448 * devm_devfreq_unregister_opp_notifier()
1449 * - Resource-managed devfreq_unregister_opp_notifier()
1450 * @dev: The devfreq user device. (parent of devfreq)
1451 * @devfreq: The devfreq object.
1452 */
1453void devm_devfreq_unregister_opp_notifier(struct device *dev,
1454 struct devfreq *devfreq)
1455{
1456 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1457 devm_devfreq_dev_match, devfreq));
1458}
1459EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1460
Chanwoo Choi0fe3a662016-01-26 13:21:26 +09001461/**
1462 * devfreq_register_notifier() - Register a driver with devfreq
1463 * @devfreq: The devfreq object.
1464 * @nb: The notifier block to register.
1465 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1466 */
1467int devfreq_register_notifier(struct devfreq *devfreq,
1468 struct notifier_block *nb,
1469 unsigned int list)
1470{
1471 int ret = 0;
1472
1473 if (!devfreq)
1474 return -EINVAL;
1475
1476 switch (list) {
1477 case DEVFREQ_TRANSITION_NOTIFIER:
1478 ret = srcu_notifier_chain_register(
1479 &devfreq->transition_notifier_list, nb);
1480 break;
1481 default:
1482 ret = -EINVAL;
1483 }
1484
1485 return ret;
1486}
1487EXPORT_SYMBOL(devfreq_register_notifier);
1488
1489/*
1490 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1491 * @devfreq: The devfreq object.
1492 * @nb: The notifier block to be unregistered.
1493 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1494 */
1495int devfreq_unregister_notifier(struct devfreq *devfreq,
1496 struct notifier_block *nb,
1497 unsigned int list)
1498{
1499 int ret = 0;
1500
1501 if (!devfreq)
1502 return -EINVAL;
1503
1504 switch (list) {
1505 case DEVFREQ_TRANSITION_NOTIFIER:
1506 ret = srcu_notifier_chain_unregister(
1507 &devfreq->transition_notifier_list, nb);
1508 break;
1509 default:
1510 ret = -EINVAL;
1511 }
1512
1513 return ret;
1514}
1515EXPORT_SYMBOL(devfreq_unregister_notifier);
1516
1517struct devfreq_notifier_devres {
1518 struct devfreq *devfreq;
1519 struct notifier_block *nb;
1520 unsigned int list;
1521};
1522
1523static void devm_devfreq_notifier_release(struct device *dev, void *res)
1524{
1525 struct devfreq_notifier_devres *this = res;
1526
1527 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1528}
1529
1530/**
1531 * devm_devfreq_register_notifier()
1532 - Resource-managed devfreq_register_notifier()
1533 * @dev: The devfreq user device. (parent of devfreq)
1534 * @devfreq: The devfreq object.
1535 * @nb: The notifier block to be unregistered.
1536 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1537 */
1538int devm_devfreq_register_notifier(struct device *dev,
1539 struct devfreq *devfreq,
1540 struct notifier_block *nb,
1541 unsigned int list)
1542{
1543 struct devfreq_notifier_devres *ptr;
1544 int ret;
1545
1546 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1547 GFP_KERNEL);
1548 if (!ptr)
1549 return -ENOMEM;
1550
1551 ret = devfreq_register_notifier(devfreq, nb, list);
1552 if (ret) {
1553 devres_free(ptr);
1554 return ret;
1555 }
1556
1557 ptr->devfreq = devfreq;
1558 ptr->nb = nb;
1559 ptr->list = list;
1560 devres_add(dev, ptr);
1561
1562 return 0;
1563}
1564EXPORT_SYMBOL(devm_devfreq_register_notifier);
1565
1566/**
1567 * devm_devfreq_unregister_notifier()
1568 - Resource-managed devfreq_unregister_notifier()
1569 * @dev: The devfreq user device. (parent of devfreq)
1570 * @devfreq: The devfreq object.
1571 * @nb: The notifier block to be unregistered.
1572 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1573 */
1574void devm_devfreq_unregister_notifier(struct device *dev,
1575 struct devfreq *devfreq,
1576 struct notifier_block *nb,
1577 unsigned int list)
1578{
1579 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1580 devm_devfreq_dev_match, devfreq));
1581}
1582EXPORT_SYMBOL(devm_devfreq_unregister_notifier);