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