blob: b2636967ddddf501b7bd320a46955dac3997f1f1 [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>
14#include <linux/sched.h>
15#include <linux/errno.h>
16#include <linux/err.h>
17#include <linux/init.h>
Paul Gortmaker417dc4b2016-06-26 03:43:47 +090018#include <linux/export.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020019#include <linux/slab.h>
MyungJoo Ham952f6d12011-11-10 10:16:23 +010020#include <linux/stat.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050021#include <linux/pm_opp.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020022#include <linux/devfreq.h>
23#include <linux/workqueue.h>
24#include <linux/platform_device.h>
25#include <linux/list.h>
26#include <linux/printk.h>
27#include <linux/hrtimer.h>
Chanwoo Choi8f510ae2015-11-10 20:31:07 +090028#include <linux/of.h>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020029#include "governor.h"
30
Nishanth Menon1a1357e2012-10-26 01:50:53 +020031static struct class *devfreq_class;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020032
33/*
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020034 * devfreq core provides delayed work based load monitoring helper
35 * functions. Governors can use these or can implement their own
36 * monitoring mechanism.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020037 */
MyungJoo Hama3c98b82011-10-02 00:19:15 +020038static struct workqueue_struct *devfreq_wq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020039
Nishanth Menon3aa173b2012-10-29 15:01:43 -050040/* The list of all device-devfreq governors */
41static LIST_HEAD(devfreq_governor_list);
MyungJoo Hama3c98b82011-10-02 00:19:15 +020042/* The list of all device-devfreq */
43static LIST_HEAD(devfreq_list);
44static DEFINE_MUTEX(devfreq_list_lock);
45
46/**
47 * find_device_devfreq() - find devfreq struct using device pointer
48 * @dev: device pointer used to lookup device devfreq.
49 *
50 * Search the list of device devfreqs and return the matched device's
51 * devfreq info. devfreq_list_lock should be held by the caller.
52 */
53static struct devfreq *find_device_devfreq(struct device *dev)
54{
55 struct devfreq *tmp_devfreq;
56
Viresh Kumar9348da22015-08-10 11:42:25 +053057 if (IS_ERR_OR_NULL(dev)) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +020058 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
59 return ERR_PTR(-EINVAL);
60 }
61 WARN(!mutex_is_locked(&devfreq_list_lock),
62 "devfreq_list_lock must be locked.");
63
64 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
65 if (tmp_devfreq->dev.parent == dev)
66 return tmp_devfreq;
67 }
68
69 return ERR_PTR(-ENODEV);
70}
71
72/**
Rajagopal Venkatf517b3a2013-06-11 16:42:27 -060073 * devfreq_set_freq_limits() - Set min and max frequency from freq_table
74 * @devfreq: the devfreq instance
75 */
76static void devfreq_set_freq_limits(struct devfreq *devfreq)
77{
78 int idx;
79 unsigned long min = ~0, max = 0;
80
81 if (!devfreq->profile->freq_table)
82 return;
83
84 for (idx = 0; idx < devfreq->profile->max_state; idx++) {
85 if (min > devfreq->profile->freq_table[idx])
86 min = devfreq->profile->freq_table[idx];
87 if (max < devfreq->profile->freq_table[idx])
88 max = devfreq->profile->freq_table[idx];
89 }
90
91 devfreq->min_freq = min;
92 devfreq->max_freq = max;
93}
94
95/**
Jonghwa Leee552bba2012-08-23 20:00:46 +090096 * devfreq_get_freq_level() - Lookup freq_table for the frequency
97 * @devfreq: the devfreq instance
98 * @freq: the target frequency
99 */
100static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
101{
102 int lev;
103
104 for (lev = 0; lev < devfreq->profile->max_state; lev++)
105 if (freq == devfreq->profile->freq_table[lev])
106 return lev;
107
108 return -EINVAL;
109}
110
111/**
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900112 * devfreq_set_freq_table() - Initialize freq_table for the frequency
113 * @devfreq: the devfreq instance
114 */
115static void devfreq_set_freq_table(struct devfreq *devfreq)
116{
117 struct devfreq_dev_profile *profile = devfreq->profile;
118 struct dev_pm_opp *opp;
119 unsigned long freq;
120 int i, count;
121
122 /* Initialize the freq_table from OPP table */
123 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
124 if (count <= 0)
125 return;
126
127 profile->max_state = count;
128 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
129 profile->max_state,
130 sizeof(*profile->freq_table),
131 GFP_KERNEL);
132 if (!profile->freq_table) {
133 profile->max_state = 0;
134 return;
135 }
136
137 rcu_read_lock();
138 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
139 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
140 if (IS_ERR(opp)) {
141 devm_kfree(devfreq->dev.parent, profile->freq_table);
142 profile->max_state = 0;
143 rcu_read_unlock();
144 return;
145 }
146 profile->freq_table[i] = freq;
147 }
148 rcu_read_unlock();
149}
150
151/**
Jonghwa Leee552bba2012-08-23 20:00:46 +0900152 * devfreq_update_status() - Update statistics of devfreq behavior
153 * @devfreq: the devfreq instance
154 * @freq: the update target frequency
155 */
Chanwoo Choife8f92c2017-01-31 15:38:17 +0900156int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
Jonghwa Leee552bba2012-08-23 20:00:46 +0900157{
Saravana Kannane35d35a2014-02-27 19:38:57 -0800158 int lev, prev_lev, ret = 0;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900159 unsigned long cur_time;
160
Jonghwa Leee552bba2012-08-23 20:00:46 +0900161 cur_time = jiffies;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800162
Tobias Jakobid0563a02016-09-29 14:36:36 +0200163 /* Immediately exit if previous_freq is not initialized yet. */
164 if (!devfreq->previous_freq)
165 goto out;
166
Saravana Kannane35d35a2014-02-27 19:38:57 -0800167 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
168 if (prev_lev < 0) {
169 ret = prev_lev;
170 goto out;
171 }
172
173 devfreq->time_in_state[prev_lev] +=
Jonghwa Leee552bba2012-08-23 20:00:46 +0900174 cur_time - devfreq->last_stat_updated;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800175
176 lev = devfreq_get_freq_level(devfreq, freq);
177 if (lev < 0) {
178 ret = lev;
179 goto out;
180 }
181
182 if (lev != prev_lev) {
Jonghwa Leee552bba2012-08-23 20:00:46 +0900183 devfreq->trans_table[(prev_lev *
184 devfreq->profile->max_state) + lev]++;
185 devfreq->total_trans++;
186 }
Jonghwa Leee552bba2012-08-23 20:00:46 +0900187
Saravana Kannane35d35a2014-02-27 19:38:57 -0800188out:
189 devfreq->last_stat_updated = cur_time;
190 return ret;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900191}
Chanwoo Choife8f92c2017-01-31 15:38:17 +0900192EXPORT_SYMBOL(devfreq_update_status);
Jonghwa Leee552bba2012-08-23 20:00:46 +0900193
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500194/**
195 * find_devfreq_governor() - find devfreq governor from name
196 * @name: name of the governor
197 *
198 * Search the list of devfreq governors and return the matched
199 * governor's pointer. devfreq_list_lock should be held by the caller.
200 */
201static struct devfreq_governor *find_devfreq_governor(const char *name)
202{
203 struct devfreq_governor *tmp_governor;
204
Viresh Kumar9348da22015-08-10 11:42:25 +0530205 if (IS_ERR_OR_NULL(name)) {
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500206 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
207 return ERR_PTR(-EINVAL);
208 }
209 WARN(!mutex_is_locked(&devfreq_list_lock),
210 "devfreq_list_lock must be locked.");
211
212 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
213 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
214 return tmp_governor;
215 }
216
217 return ERR_PTR(-ENODEV);
218}
219
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900220static int devfreq_notify_transition(struct devfreq *devfreq,
221 struct devfreq_freqs *freqs, unsigned int state)
222{
223 if (!devfreq)
224 return -EINVAL;
225
226 switch (state) {
227 case DEVFREQ_PRECHANGE:
228 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
229 DEVFREQ_PRECHANGE, freqs);
230 break;
231
232 case DEVFREQ_POSTCHANGE:
233 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
234 DEVFREQ_POSTCHANGE, freqs);
235 break;
236 default:
237 return -EINVAL;
238 }
239
240 return 0;
241}
242
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200243/* Load monitoring helper functions for governors use */
244
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200245/**
246 * update_devfreq() - Reevaluate the device and configure frequency.
247 * @devfreq: the devfreq instance.
248 *
249 * Note: Lock devfreq->lock before calling update_devfreq
250 * This function is exported for governors.
251 */
252int update_devfreq(struct devfreq *devfreq)
253{
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900254 struct devfreq_freqs freqs;
255 unsigned long freq, cur_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200256 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100257 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200258
259 if (!mutex_is_locked(&devfreq->lock)) {
260 WARN(true, "devfreq->lock must be locked by the caller.\n");
261 return -EINVAL;
262 }
263
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500264 if (!devfreq->governor)
265 return -EINVAL;
266
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200267 /* Reevaluate the proper frequency */
268 err = devfreq->governor->get_target_freq(devfreq, &freq);
269 if (err)
270 return err;
271
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100272 /*
Javi Merinod3b7e172015-08-14 18:57:00 +0100273 * Adjust the frequency with user freq and QoS.
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100274 *
Javi Merinod3b7e172015-08-14 18:57:00 +0100275 * List from the highest priority
276 * max_freq
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100277 * min_freq
278 */
279
280 if (devfreq->min_freq && freq < devfreq->min_freq) {
281 freq = devfreq->min_freq;
282 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
283 }
284 if (devfreq->max_freq && freq > devfreq->max_freq) {
285 freq = devfreq->max_freq;
286 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
287 }
288
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900289 if (devfreq->profile->get_cur_freq)
290 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
291 else
292 cur_freq = devfreq->previous_freq;
293
294 freqs.old = cur_freq;
295 freqs.new = freq;
296 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
297
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100298 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
Chanwoo Choi0d371892016-06-23 11:18:43 +0900299 if (err) {
300 freqs.new = cur_freq;
301 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200302 return err;
Chanwoo Choi0d371892016-06-23 11:18:43 +0900303 }
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200304
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900305 freqs.new = freq;
306 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
307
Jonghwa Leee552bba2012-08-23 20:00:46 +0900308 if (devfreq->profile->freq_table)
309 if (devfreq_update_status(devfreq, freq))
310 dev_err(&devfreq->dev,
311 "Couldn't update frequency transition information.\n");
312
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200313 devfreq->previous_freq = freq;
314 return err;
315}
Nishanth Menon2df50212012-10-29 15:01:42 -0500316EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200317
318/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200319 * devfreq_monitor() - Periodically poll devfreq objects.
320 * @work: the work struct used to run devfreq_monitor periodically.
321 *
322 */
323static void devfreq_monitor(struct work_struct *work)
324{
325 int err;
326 struct devfreq *devfreq = container_of(work,
327 struct devfreq, work.work);
328
329 mutex_lock(&devfreq->lock);
330 err = update_devfreq(devfreq);
331 if (err)
332 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
333
334 queue_delayed_work(devfreq_wq, &devfreq->work,
335 msecs_to_jiffies(devfreq->profile->polling_ms));
336 mutex_unlock(&devfreq->lock);
337}
338
339/**
340 * devfreq_monitor_start() - Start load monitoring of devfreq instance
341 * @devfreq: the devfreq instance.
342 *
343 * Helper function for starting devfreq device load monitoing. By
344 * default delayed work based monitoring is supported. Function
345 * to be called from governor in response to DEVFREQ_GOV_START
346 * event when device is added to devfreq framework.
347 */
348void devfreq_monitor_start(struct devfreq *devfreq)
349{
350 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
351 if (devfreq->profile->polling_ms)
352 queue_delayed_work(devfreq_wq, &devfreq->work,
353 msecs_to_jiffies(devfreq->profile->polling_ms));
354}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100355EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200356
357/**
358 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
359 * @devfreq: the devfreq instance.
360 *
361 * Helper function to stop devfreq device load monitoing. Function
362 * to be called from governor in response to DEVFREQ_GOV_STOP
363 * event when device is removed from devfreq framework.
364 */
365void devfreq_monitor_stop(struct devfreq *devfreq)
366{
367 cancel_delayed_work_sync(&devfreq->work);
368}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100369EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200370
371/**
372 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
373 * @devfreq: the devfreq instance.
374 *
375 * Helper function to suspend devfreq device load monitoing. Function
376 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
377 * event or when polling interval is set to zero.
378 *
379 * Note: Though this function is same as devfreq_monitor_stop(),
380 * intentionally kept separate to provide hooks for collecting
381 * transition statistics.
382 */
383void devfreq_monitor_suspend(struct devfreq *devfreq)
384{
385 mutex_lock(&devfreq->lock);
386 if (devfreq->stop_polling) {
387 mutex_unlock(&devfreq->lock);
388 return;
389 }
390
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530391 devfreq_update_status(devfreq, devfreq->previous_freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200392 devfreq->stop_polling = true;
393 mutex_unlock(&devfreq->lock);
394 cancel_delayed_work_sync(&devfreq->work);
395}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100396EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200397
398/**
399 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
400 * @devfreq: the devfreq instance.
401 *
402 * Helper function to resume devfreq device load monitoing. Function
403 * to be called from governor in response to DEVFREQ_GOV_RESUME
404 * event or when polling interval is set to non-zero.
405 */
406void devfreq_monitor_resume(struct devfreq *devfreq)
407{
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530408 unsigned long freq;
409
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200410 mutex_lock(&devfreq->lock);
411 if (!devfreq->stop_polling)
412 goto out;
413
414 if (!delayed_work_pending(&devfreq->work) &&
415 devfreq->profile->polling_ms)
416 queue_delayed_work(devfreq_wq, &devfreq->work,
417 msecs_to_jiffies(devfreq->profile->polling_ms));
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530418
419 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200420 devfreq->stop_polling = false;
421
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530422 if (devfreq->profile->get_cur_freq &&
423 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
424 devfreq->previous_freq = freq;
425
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200426out:
427 mutex_unlock(&devfreq->lock);
428}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100429EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200430
431/**
432 * devfreq_interval_update() - Update device devfreq monitoring interval
433 * @devfreq: the devfreq instance.
434 * @delay: new polling interval to be set.
435 *
436 * Helper function to set new load monitoring polling interval. Function
437 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
438 */
439void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
440{
441 unsigned int cur_delay = devfreq->profile->polling_ms;
442 unsigned int new_delay = *delay;
443
444 mutex_lock(&devfreq->lock);
445 devfreq->profile->polling_ms = new_delay;
446
447 if (devfreq->stop_polling)
448 goto out;
449
450 /* if new delay is zero, stop polling */
451 if (!new_delay) {
452 mutex_unlock(&devfreq->lock);
453 cancel_delayed_work_sync(&devfreq->work);
454 return;
455 }
456
457 /* if current delay is zero, start polling with new delay */
458 if (!cur_delay) {
459 queue_delayed_work(devfreq_wq, &devfreq->work,
460 msecs_to_jiffies(devfreq->profile->polling_ms));
461 goto out;
462 }
463
464 /* if current delay is greater than new delay, restart polling */
465 if (cur_delay > new_delay) {
466 mutex_unlock(&devfreq->lock);
467 cancel_delayed_work_sync(&devfreq->work);
468 mutex_lock(&devfreq->lock);
469 if (!devfreq->stop_polling)
470 queue_delayed_work(devfreq_wq, &devfreq->work,
471 msecs_to_jiffies(devfreq->profile->polling_ms));
472 }
473out:
474 mutex_unlock(&devfreq->lock);
475}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100476EXPORT_SYMBOL(devfreq_interval_update);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200477
478/**
479 * devfreq_notifier_call() - Notify that the device frequency requirements
480 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200481 * @nb: the notifier_block (supposed to be devfreq->nb)
482 * @type: not used
483 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200484 *
485 * Called by a notifier that uses devfreq->nb.
486 */
487static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
488 void *devp)
489{
490 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
491 int ret;
492
493 mutex_lock(&devfreq->lock);
494 ret = update_devfreq(devfreq);
495 mutex_unlock(&devfreq->lock);
496
497 return ret;
498}
499
500/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200501 * _remove_devfreq() - Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200502 * @devfreq: the devfreq struct
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200503 */
Chanwoo Choi585fc832014-05-09 16:43:07 +0900504static void _remove_devfreq(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200505{
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200506 mutex_lock(&devfreq_list_lock);
507 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
508 mutex_unlock(&devfreq_list_lock);
509 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200510 return;
511 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200512 list_del(&devfreq->node);
513 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200514
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500515 if (devfreq->governor)
516 devfreq->governor->event_handler(devfreq,
517 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200518
519 if (devfreq->profile->exit)
520 devfreq->profile->exit(devfreq->dev.parent);
521
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200522 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200523 kfree(devfreq);
524}
525
526/**
527 * devfreq_dev_release() - Callback for struct device to release the device.
528 * @dev: the devfreq device
529 *
530 * This calls _remove_devfreq() if _remove_devfreq() is not called.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200531 */
532static void devfreq_dev_release(struct device *dev)
533{
534 struct devfreq *devfreq = to_devfreq(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200535
Chanwoo Choi585fc832014-05-09 16:43:07 +0900536 _remove_devfreq(devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200537}
538
539/**
540 * devfreq_add_device() - Add devfreq feature to the device
541 * @dev: the device to add devfreq feature.
542 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500543 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200544 * @data: private data for the governor. The devfreq framework does not
545 * touch this value.
546 */
547struct devfreq *devfreq_add_device(struct device *dev,
548 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500549 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200550 void *data)
551{
552 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500553 struct devfreq_governor *governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200554 int err = 0;
555
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500556 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200557 dev_err(dev, "%s: Invalid parameters.\n", __func__);
558 return ERR_PTR(-EINVAL);
559 }
560
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200561 mutex_lock(&devfreq_list_lock);
562 devfreq = find_device_devfreq(dev);
563 mutex_unlock(&devfreq_list_lock);
564 if (!IS_ERR(devfreq)) {
565 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
566 err = -EINVAL;
567 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200568 }
569
570 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
571 if (!devfreq) {
572 dev_err(dev, "%s: Unable to create devfreq for the device\n",
573 __func__);
574 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100575 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200576 }
577
578 mutex_init(&devfreq->lock);
579 mutex_lock(&devfreq->lock);
580 devfreq->dev.parent = dev;
581 devfreq->dev.class = devfreq_class;
582 devfreq->dev.release = devfreq_dev_release;
583 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500584 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200585 devfreq->previous_freq = profile->initial_freq;
Lukasz Luba8d39fc02016-05-31 11:25:09 +0100586 devfreq->last_status.current_frequency = profile->initial_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200587 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200588 devfreq->nb.notifier_call = devfreq_notifier_call;
589
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900590 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
591 mutex_unlock(&devfreq->lock);
592 devfreq_set_freq_table(devfreq);
593 mutex_lock(&devfreq->lock);
594 }
Rajagopal Venkatf517b3a2013-06-11 16:42:27 -0600595 devfreq_set_freq_limits(devfreq);
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900596
Kees Cook02aa2a32013-07-03 15:04:56 -0700597 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200598 err = device_register(&devfreq->dev);
599 if (err) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200600 mutex_unlock(&devfreq->lock);
Chanwoo Choif5c3fd82017-08-24 10:42:48 +0900601 goto err_dev;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200602 }
603
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900604 devfreq->trans_table = devm_kzalloc(&devfreq->dev, sizeof(unsigned int) *
605 devfreq->profile->max_state *
606 devfreq->profile->max_state,
607 GFP_KERNEL);
608 devfreq->time_in_state = devm_kzalloc(&devfreq->dev, sizeof(unsigned long) *
609 devfreq->profile->max_state,
610 GFP_KERNEL);
611 devfreq->last_stat_updated = jiffies;
612
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900613 srcu_init_notifier_head(&devfreq->transition_notifier_list);
614
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200615 mutex_unlock(&devfreq->lock);
616
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200617 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200618 list_add(&devfreq->node, &devfreq_list);
619
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500620 governor = find_devfreq_governor(devfreq->governor_name);
Chanwoo Choiebdfcaa2016-12-28 20:52:35 +0900621 if (IS_ERR(governor)) {
622 dev_err(dev, "%s: Unable to find governor for the device\n",
623 __func__);
624 err = PTR_ERR(governor);
625 goto err_init;
626 }
627
628 devfreq->governor = governor;
629 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
630 NULL);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200631 if (err) {
632 dev_err(dev, "%s: Unable to start governor for the device\n",
633 __func__);
634 goto err_init;
635 }
Axel Lin0f376c92016-09-29 10:13:28 +0800636 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200637
Axel Lin3f19f082011-11-15 21:59:09 +0100638 return devfreq;
639
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200640err_init:
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200641 list_del(&devfreq->node);
Axel Lin0f376c92016-09-29 10:13:28 +0800642 mutex_unlock(&devfreq_list_lock);
643
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200644 device_unregister(&devfreq->dev);
Chanwoo Choif5c3fd82017-08-24 10:42:48 +0900645err_dev:
646 if (devfreq)
647 kfree(devfreq);
Axel Lin3f19f082011-11-15 21:59:09 +0100648err_out:
649 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200650}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200651EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200652
653/**
654 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200655 * @devfreq: the devfreq instance to be removed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900656 *
657 * The opposite of devfreq_add_device().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200658 */
659int devfreq_remove_device(struct devfreq *devfreq)
660{
661 if (!devfreq)
662 return -EINVAL;
663
Chanwoo Choi585fc832014-05-09 16:43:07 +0900664 device_unregister(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200665
666 return 0;
667}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200668EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200669
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900670static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
671{
672 struct devfreq **r = res;
673
674 if (WARN_ON(!r || !*r))
675 return 0;
676
677 return *r == data;
678}
679
680static void devm_devfreq_dev_release(struct device *dev, void *res)
681{
682 devfreq_remove_device(*(struct devfreq **)res);
683}
684
685/**
686 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
687 * @dev: the device to add devfreq feature.
688 * @profile: device-specific profile to run devfreq.
689 * @governor_name: name of the policy to choose frequency.
690 * @data: private data for the governor. The devfreq framework does not
691 * touch this value.
692 *
693 * This function manages automatically the memory of devfreq device using device
694 * resource management and simplify the free operation for memory of devfreq
695 * device.
696 */
697struct devfreq *devm_devfreq_add_device(struct device *dev,
698 struct devfreq_dev_profile *profile,
699 const char *governor_name,
700 void *data)
701{
702 struct devfreq **ptr, *devfreq;
703
704 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
705 if (!ptr)
706 return ERR_PTR(-ENOMEM);
707
708 devfreq = devfreq_add_device(dev, profile, governor_name, data);
709 if (IS_ERR(devfreq)) {
710 devres_free(ptr);
711 return ERR_PTR(-ENOMEM);
712 }
713
714 *ptr = devfreq;
715 devres_add(dev, ptr);
716
717 return devfreq;
718}
719EXPORT_SYMBOL(devm_devfreq_add_device);
720
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900721#ifdef CONFIG_OF
722/*
723 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
724 * @dev - instance to the given device
725 * @index - index into list of devfreq
726 *
727 * return the instance of devfreq device
728 */
729struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
730{
731 struct device_node *node;
732 struct devfreq *devfreq;
733
734 if (!dev)
735 return ERR_PTR(-EINVAL);
736
737 if (!dev->of_node)
738 return ERR_PTR(-EINVAL);
739
740 node = of_parse_phandle(dev->of_node, "devfreq", index);
741 if (!node)
742 return ERR_PTR(-ENODEV);
743
744 mutex_lock(&devfreq_list_lock);
745 list_for_each_entry(devfreq, &devfreq_list, node) {
746 if (devfreq->dev.parent
747 && devfreq->dev.parent->of_node == node) {
748 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800749 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900750 return devfreq;
751 }
752 }
753 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800754 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900755
756 return ERR_PTR(-EPROBE_DEFER);
757}
758#else
759struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
760{
761 return ERR_PTR(-ENODEV);
762}
763#endif /* CONFIG_OF */
764EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
765
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900766/**
767 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
768 * @dev: the device to add devfreq feature.
769 * @devfreq: the devfreq instance to be removed
770 */
771void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
772{
773 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
774 devm_devfreq_dev_match, devfreq));
775}
776EXPORT_SYMBOL(devm_devfreq_remove_device);
777
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200778/**
779 * devfreq_suspend_device() - Suspend devfreq of a device.
780 * @devfreq: the devfreq instance to be suspended
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900781 *
782 * This function is intended to be called by the pm callbacks
783 * (e.g., runtime_suspend, suspend) of the device driver that
784 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200785 */
786int devfreq_suspend_device(struct devfreq *devfreq)
787{
788 if (!devfreq)
789 return -EINVAL;
790
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500791 if (!devfreq->governor)
792 return 0;
793
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200794 return devfreq->governor->event_handler(devfreq,
795 DEVFREQ_GOV_SUSPEND, NULL);
796}
797EXPORT_SYMBOL(devfreq_suspend_device);
798
799/**
800 * devfreq_resume_device() - Resume devfreq of a device.
801 * @devfreq: the devfreq instance to be resumed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900802 *
803 * This function is intended to be called by the pm callbacks
804 * (e.g., runtime_resume, resume) of the device driver that
805 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200806 */
807int devfreq_resume_device(struct devfreq *devfreq)
808{
809 if (!devfreq)
810 return -EINVAL;
811
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500812 if (!devfreq->governor)
813 return 0;
814
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200815 return devfreq->governor->event_handler(devfreq,
816 DEVFREQ_GOV_RESUME, NULL);
817}
818EXPORT_SYMBOL(devfreq_resume_device);
819
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500820/**
821 * devfreq_add_governor() - Add devfreq governor
822 * @governor: the devfreq governor to be added
823 */
824int devfreq_add_governor(struct devfreq_governor *governor)
825{
826 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500827 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500828 int err = 0;
829
830 if (!governor) {
831 pr_err("%s: Invalid parameters.\n", __func__);
832 return -EINVAL;
833 }
834
835 mutex_lock(&devfreq_list_lock);
836 g = find_devfreq_governor(governor->name);
837 if (!IS_ERR(g)) {
838 pr_err("%s: governor %s already registered\n", __func__,
839 g->name);
840 err = -EINVAL;
841 goto err_out;
842 }
843
844 list_add(&governor->node, &devfreq_governor_list);
845
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500846 list_for_each_entry(devfreq, &devfreq_list, node) {
847 int ret = 0;
848 struct device *dev = devfreq->dev.parent;
849
850 if (!strncmp(devfreq->governor_name, governor->name,
851 DEVFREQ_NAME_LEN)) {
852 /* The following should never occur */
853 if (devfreq->governor) {
854 dev_warn(dev,
855 "%s: Governor %s already present\n",
856 __func__, devfreq->governor->name);
857 ret = devfreq->governor->event_handler(devfreq,
858 DEVFREQ_GOV_STOP, NULL);
859 if (ret) {
860 dev_warn(dev,
861 "%s: Governor %s stop = %d\n",
862 __func__,
863 devfreq->governor->name, ret);
864 }
865 /* Fall through */
866 }
867 devfreq->governor = governor;
868 ret = devfreq->governor->event_handler(devfreq,
869 DEVFREQ_GOV_START, NULL);
870 if (ret) {
871 dev_warn(dev, "%s: Governor %s start=%d\n",
872 __func__, devfreq->governor->name,
873 ret);
874 }
875 }
876 }
877
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500878err_out:
879 mutex_unlock(&devfreq_list_lock);
880
881 return err;
882}
883EXPORT_SYMBOL(devfreq_add_governor);
884
885/**
886 * devfreq_remove_device() - Remove devfreq feature from a device.
887 * @governor: the devfreq governor to be removed
888 */
889int devfreq_remove_governor(struct devfreq_governor *governor)
890{
891 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500892 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500893 int err = 0;
894
895 if (!governor) {
896 pr_err("%s: Invalid parameters.\n", __func__);
897 return -EINVAL;
898 }
899
900 mutex_lock(&devfreq_list_lock);
901 g = find_devfreq_governor(governor->name);
902 if (IS_ERR(g)) {
903 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamatb9e1c8e2012-11-21 10:36:13 +0530904 governor->name);
Sachin Kamatf9c08e22012-11-21 10:36:14 +0530905 err = PTR_ERR(g);
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500906 goto err_out;
907 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500908 list_for_each_entry(devfreq, &devfreq_list, node) {
909 int ret;
910 struct device *dev = devfreq->dev.parent;
911
912 if (!strncmp(devfreq->governor_name, governor->name,
913 DEVFREQ_NAME_LEN)) {
914 /* we should have a devfreq governor! */
915 if (!devfreq->governor) {
916 dev_warn(dev, "%s: Governor %s NOT present\n",
917 __func__, governor->name);
918 continue;
919 /* Fall through */
920 }
921 ret = devfreq->governor->event_handler(devfreq,
922 DEVFREQ_GOV_STOP, NULL);
923 if (ret) {
924 dev_warn(dev, "%s: Governor %s stop=%d\n",
925 __func__, devfreq->governor->name,
926 ret);
927 }
928 devfreq->governor = NULL;
929 }
930 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500931
932 list_del(&governor->node);
933err_out:
934 mutex_unlock(&devfreq_list_lock);
935
936 return err;
937}
938EXPORT_SYMBOL(devfreq_remove_governor);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200939
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700940static ssize_t governor_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200941 struct device_attribute *attr, char *buf)
942{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500943 if (!to_devfreq(dev)->governor)
944 return -EINVAL;
945
MyungJoo Ham9005b652011-10-02 00:19:28 +0200946 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
947}
948
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700949static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500950 const char *buf, size_t count)
951{
952 struct devfreq *df = to_devfreq(dev);
953 int ret;
954 char str_governor[DEVFREQ_NAME_LEN + 1];
Saravana Kannan7897df32016-10-24 16:35:05 -0700955 const struct devfreq_governor *governor, *prev_gov;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500956
957 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
958 if (ret != 1)
959 return -EINVAL;
960
961 mutex_lock(&devfreq_list_lock);
962 governor = find_devfreq_governor(str_governor);
963 if (IS_ERR(governor)) {
964 ret = PTR_ERR(governor);
965 goto out;
966 }
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200967 if (df->governor == governor) {
968 ret = 0;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500969 goto out;
Chanwoo Choi2294b772017-01-31 15:38:16 +0900970 } else if (df->governor->immutable || governor->immutable) {
971 ret = -EINVAL;
972 goto out;
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200973 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500974
975 if (df->governor) {
976 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
977 if (ret) {
978 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
979 __func__, df->governor->name, ret);
980 goto out;
981 }
982 }
Saravana Kannan7897df32016-10-24 16:35:05 -0700983 prev_gov = df->governor;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500984 df->governor = governor;
985 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
986 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
Saravana Kannan7897df32016-10-24 16:35:05 -0700987 if (ret) {
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500988 dev_warn(dev, "%s: Governor %s not started(%d)\n",
989 __func__, df->governor->name, ret);
Saravana Kannan7897df32016-10-24 16:35:05 -0700990 if (prev_gov) {
991 df->governor = prev_gov;
992 strlcpy(df->governor_name, prev_gov->name,
993 DEVFREQ_NAME_LEN);
994 df->governor->event_handler(df, DEVFREQ_GOV_START,
995 NULL);
996 }
997 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500998out:
999 mutex_unlock(&devfreq_list_lock);
1000
1001 if (!ret)
1002 ret = count;
1003 return ret;
1004}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001005static DEVICE_ATTR_RW(governor);
1006
1007static ssize_t available_governors_show(struct device *d,
1008 struct device_attribute *attr,
1009 char *buf)
Nishanth Menon50a5b332012-10-29 15:01:48 -05001010{
Chanwoo Choi2294b772017-01-31 15:38:16 +09001011 struct devfreq *df = to_devfreq(d);
Nishanth Menon50a5b332012-10-29 15:01:48 -05001012 ssize_t count = 0;
1013
1014 mutex_lock(&devfreq_list_lock);
Chanwoo Choi2294b772017-01-31 15:38:16 +09001015
1016 /*
1017 * The devfreq with immutable governor (e.g., passive) shows
1018 * only own governor.
1019 */
1020 if (df->governor->immutable) {
1021 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1022 "%s ", df->governor_name);
1023 /*
1024 * The devfreq device shows the registered governor except for
1025 * immutable governors such as passive governor .
1026 */
1027 } else {
1028 struct devfreq_governor *governor;
1029
1030 list_for_each_entry(governor, &devfreq_governor_list, node) {
1031 if (governor->immutable)
1032 continue;
1033 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1034 "%s ", governor->name);
1035 }
1036 }
1037
Nishanth Menon50a5b332012-10-29 15:01:48 -05001038 mutex_unlock(&devfreq_list_lock);
1039
1040 /* Truncate the trailing space */
1041 if (count)
1042 count--;
1043
1044 count += sprintf(&buf[count], "\n");
1045
1046 return count;
1047}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001048static DEVICE_ATTR_RO(available_governors);
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001049
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001050static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1051 char *buf)
MyungJoo Ham9005b652011-10-02 00:19:28 +02001052{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001053 unsigned long freq;
1054 struct devfreq *devfreq = to_devfreq(dev);
1055
1056 if (devfreq->profile->get_cur_freq &&
1057 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1058 return sprintf(buf, "%lu\n", freq);
1059
1060 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1061}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001062static DEVICE_ATTR_RO(cur_freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001063
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001064static ssize_t target_freq_show(struct device *dev,
1065 struct device_attribute *attr, char *buf)
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001066{
MyungJoo Ham9005b652011-10-02 00:19:28 +02001067 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1068}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001069static DEVICE_ATTR_RO(target_freq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001070
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001071static ssize_t polling_interval_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001072 struct device_attribute *attr, char *buf)
1073{
1074 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1075}
1076
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001077static ssize_t polling_interval_store(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001078 struct device_attribute *attr,
1079 const char *buf, size_t count)
1080{
1081 struct devfreq *df = to_devfreq(dev);
1082 unsigned int value;
1083 int ret;
1084
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001085 if (!df->governor)
1086 return -EINVAL;
1087
MyungJoo Ham9005b652011-10-02 00:19:28 +02001088 ret = sscanf(buf, "%u", &value);
1089 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001090 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +02001091
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001092 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001093 ret = count;
1094
MyungJoo Ham9005b652011-10-02 00:19:28 +02001095 return ret;
1096}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001097static DEVICE_ATTR_RW(polling_interval);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001098
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001099static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001100 const char *buf, size_t count)
1101{
1102 struct devfreq *df = to_devfreq(dev);
1103 unsigned long value;
1104 int ret;
1105 unsigned long max;
1106
1107 ret = sscanf(buf, "%lu", &value);
1108 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001109 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001110
1111 mutex_lock(&df->lock);
1112 max = df->max_freq;
1113 if (value && max && value > max) {
1114 ret = -EINVAL;
1115 goto unlock;
1116 }
1117
1118 df->min_freq = value;
1119 update_devfreq(df);
1120 ret = count;
1121unlock:
1122 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001123 return ret;
1124}
1125
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001126static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001127 const char *buf, size_t count)
1128{
1129 struct devfreq *df = to_devfreq(dev);
1130 unsigned long value;
1131 int ret;
1132 unsigned long min;
1133
1134 ret = sscanf(buf, "%lu", &value);
1135 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001136 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001137
1138 mutex_lock(&df->lock);
1139 min = df->min_freq;
1140 if (value && min && value < min) {
1141 ret = -EINVAL;
1142 goto unlock;
1143 }
1144
1145 df->max_freq = value;
1146 update_devfreq(df);
1147 ret = count;
1148unlock:
1149 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001150 return ret;
1151}
1152
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001153#define show_one(name) \
1154static ssize_t name##_show \
1155(struct device *dev, struct device_attribute *attr, char *buf) \
1156{ \
1157 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001158}
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001159show_one(min_freq);
1160show_one(max_freq);
1161
1162static DEVICE_ATTR_RW(min_freq);
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001163static DEVICE_ATTR_RW(max_freq);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001164
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001165static ssize_t available_frequencies_show(struct device *d,
1166 struct device_attribute *attr,
1167 char *buf)
Nishanth Menond287de82012-10-25 19:48:59 -05001168{
1169 struct devfreq *df = to_devfreq(d);
1170 struct device *dev = df->dev.parent;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001171 struct dev_pm_opp *opp;
Saravana Kannan345ca9622014-04-10 16:46:25 -07001172 unsigned int i = 0, max_state = df->profile->max_state;
1173 bool use_opp;
Nishanth Menond287de82012-10-25 19:48:59 -05001174 ssize_t count = 0;
1175 unsigned long freq = 0;
1176
1177 rcu_read_lock();
Saravana Kannan345ca9622014-04-10 16:46:25 -07001178 use_opp = dev_pm_opp_get_opp_count(dev) > 0;
1179 while (use_opp || (!use_opp && i < max_state)) {
1180 if (use_opp) {
1181 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
1182 if (IS_ERR(opp))
1183 break;
1184 } else {
1185 freq = df->profile->freq_table[i++];
1186 }
Nishanth Menond287de82012-10-25 19:48:59 -05001187
1188 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1189 "%lu ", freq);
1190 freq++;
Saravana Kannan345ca9622014-04-10 16:46:25 -07001191 }
Nishanth Menond287de82012-10-25 19:48:59 -05001192 rcu_read_unlock();
1193
1194 /* Truncate the trailing space */
1195 if (count)
1196 count--;
1197
1198 count += sprintf(&buf[count], "\n");
1199
1200 return count;
1201}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001202static DEVICE_ATTR_RO(available_frequencies);
Nishanth Menond287de82012-10-25 19:48:59 -05001203
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001204static ssize_t trans_stat_show(struct device *dev,
1205 struct device_attribute *attr, char *buf)
Jonghwa Leee552bba2012-08-23 20:00:46 +09001206{
1207 struct devfreq *devfreq = to_devfreq(dev);
1208 ssize_t len;
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301209 int i, j;
Jonghwa Leee552bba2012-08-23 20:00:46 +09001210 unsigned int max_state = devfreq->profile->max_state;
1211
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301212 if (!devfreq->stop_polling &&
1213 devfreq_update_status(devfreq, devfreq->previous_freq))
Jonghwa Leee552bba2012-08-23 20:00:46 +09001214 return 0;
MyungJoo Ham34bd3222015-11-23 15:45:36 +09001215 if (max_state == 0)
1216 return sprintf(buf, "Not Supported.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001217
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001218 len = sprintf(buf, " From : To\n");
1219 len += sprintf(buf + len, " :");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001220 for (i = 0; i < max_state; i++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001221 len += sprintf(buf + len, "%10lu",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001222 devfreq->profile->freq_table[i]);
1223
1224 len += sprintf(buf + len, " time(ms)\n");
1225
1226 for (i = 0; i < max_state; i++) {
1227 if (devfreq->profile->freq_table[i]
1228 == devfreq->previous_freq) {
1229 len += sprintf(buf + len, "*");
1230 } else {
1231 len += sprintf(buf + len, " ");
1232 }
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001233 len += sprintf(buf + len, "%10lu:",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001234 devfreq->profile->freq_table[i]);
1235 for (j = 0; j < max_state; j++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001236 len += sprintf(buf + len, "%10u",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001237 devfreq->trans_table[(i * max_state) + j]);
1238 len += sprintf(buf + len, "%10u\n",
1239 jiffies_to_msecs(devfreq->time_in_state[i]));
1240 }
1241
1242 len += sprintf(buf + len, "Total transition : %u\n",
1243 devfreq->total_trans);
1244 return len;
1245}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001246static DEVICE_ATTR_RO(trans_stat);
Jonghwa Leee552bba2012-08-23 20:00:46 +09001247
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001248static struct attribute *devfreq_attrs[] = {
1249 &dev_attr_governor.attr,
1250 &dev_attr_available_governors.attr,
1251 &dev_attr_cur_freq.attr,
1252 &dev_attr_available_frequencies.attr,
1253 &dev_attr_target_freq.attr,
1254 &dev_attr_polling_interval.attr,
1255 &dev_attr_min_freq.attr,
1256 &dev_attr_max_freq.attr,
1257 &dev_attr_trans_stat.attr,
1258 NULL,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001259};
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001260ATTRIBUTE_GROUPS(devfreq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001261
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001262static int __init devfreq_init(void)
1263{
1264 devfreq_class = class_create(THIS_MODULE, "devfreq");
1265 if (IS_ERR(devfreq_class)) {
1266 pr_err("%s: couldn't create class\n", __FILE__);
1267 return PTR_ERR(devfreq_class);
1268 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001269
1270 devfreq_wq = create_freezable_workqueue("devfreq_wq");
Dan Carpenterea7f4542013-08-15 10:55:10 +03001271 if (!devfreq_wq) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001272 class_destroy(devfreq_class);
1273 pr_err("%s: couldn't create workqueue\n", __FILE__);
Dan Carpenterea7f4542013-08-15 10:55:10 +03001274 return -ENOMEM;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001275 }
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001276 devfreq_class->dev_groups = devfreq_groups;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001277
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001278 return 0;
1279}
1280subsys_initcall(devfreq_init);
1281
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001282/*
1283 * The followings are helper functions for devfreq user device drivers with
1284 * OPP framework.
1285 */
1286
1287/**
1288 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1289 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001290 * @dev: The devfreq user device. (parent of devfreq)
1291 * @freq: The frequency given to target function
1292 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001293 *
Nishanth Menonbcb27542013-01-18 19:52:34 +00001294 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1295 * protected pointer. The reason for the same is that the opp pointer which is
1296 * returned will remain valid for use with opp_get_{voltage, freq} only while
1297 * under the locked area. The pointer returned must be used prior to unlocking
1298 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001299 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001300struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1301 unsigned long *freq,
1302 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001303{
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001304 struct dev_pm_opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001305
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001306 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1307 /* The freq is an upper bound. opp should be lower */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001308 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001309
1310 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001311 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001312 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001313 } else {
1314 /* The freq is an lower bound. opp should be higher */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001315 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001316
1317 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001318 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001319 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001320 }
1321
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001322 return opp;
1323}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001324EXPORT_SYMBOL(devfreq_recommended_opp);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001325
1326/**
1327 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1328 * for any changes in the OPP availability
1329 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001330 * @dev: The devfreq user device. (parent of devfreq)
1331 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001332 */
1333int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1334{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001335 struct srcu_notifier_head *nh;
1336 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001337
MyungJoo Ham389baed2012-11-21 19:04:51 +09001338 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001339 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001340 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001341 ret = PTR_ERR(nh);
1342 rcu_read_unlock();
1343 if (!ret)
1344 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1345
1346 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001347}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001348EXPORT_SYMBOL(devfreq_register_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001349
1350/**
1351 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1352 * notified for any changes in the OPP
1353 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001354 * @dev: The devfreq user device. (parent of devfreq)
1355 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001356 *
1357 * At exit() callback of devfreq_dev_profile, this must be included if
1358 * devfreq_recommended_opp is used.
1359 */
1360int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1361{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001362 struct srcu_notifier_head *nh;
1363 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001364
MyungJoo Ham389baed2012-11-21 19:04:51 +09001365 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001366 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001367 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001368 ret = PTR_ERR(nh);
1369 rcu_read_unlock();
1370 if (!ret)
1371 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1372
1373 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001374}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001375EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001376
Chanwoo Choid5b040d2014-05-09 16:43:09 +09001377static void devm_devfreq_opp_release(struct device *dev, void *res)
1378{
1379 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1380}
1381
1382/**
1383 * devm_ devfreq_register_opp_notifier()
1384 * - Resource-managed devfreq_register_opp_notifier()
1385 * @dev: The devfreq user device. (parent of devfreq)
1386 * @devfreq: The devfreq object.
1387 */
1388int devm_devfreq_register_opp_notifier(struct device *dev,
1389 struct devfreq *devfreq)
1390{
1391 struct devfreq **ptr;
1392 int ret;
1393
1394 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1395 if (!ptr)
1396 return -ENOMEM;
1397
1398 ret = devfreq_register_opp_notifier(dev, devfreq);
1399 if (ret) {
1400 devres_free(ptr);
1401 return ret;
1402 }
1403
1404 *ptr = devfreq;
1405 devres_add(dev, ptr);
1406
1407 return 0;
1408}
1409EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1410
1411/**
1412 * devm_devfreq_unregister_opp_notifier()
1413 * - Resource-managed devfreq_unregister_opp_notifier()
1414 * @dev: The devfreq user device. (parent of devfreq)
1415 * @devfreq: The devfreq object.
1416 */
1417void devm_devfreq_unregister_opp_notifier(struct device *dev,
1418 struct devfreq *devfreq)
1419{
1420 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1421 devm_devfreq_dev_match, devfreq));
1422}
1423EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1424
Chanwoo Choi0fe3a662016-01-26 13:21:26 +09001425/**
1426 * devfreq_register_notifier() - Register a driver with devfreq
1427 * @devfreq: The devfreq object.
1428 * @nb: The notifier block to register.
1429 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1430 */
1431int devfreq_register_notifier(struct devfreq *devfreq,
1432 struct notifier_block *nb,
1433 unsigned int list)
1434{
1435 int ret = 0;
1436
1437 if (!devfreq)
1438 return -EINVAL;
1439
1440 switch (list) {
1441 case DEVFREQ_TRANSITION_NOTIFIER:
1442 ret = srcu_notifier_chain_register(
1443 &devfreq->transition_notifier_list, nb);
1444 break;
1445 default:
1446 ret = -EINVAL;
1447 }
1448
1449 return ret;
1450}
1451EXPORT_SYMBOL(devfreq_register_notifier);
1452
1453/*
1454 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1455 * @devfreq: The devfreq object.
1456 * @nb: The notifier block to be unregistered.
1457 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1458 */
1459int devfreq_unregister_notifier(struct devfreq *devfreq,
1460 struct notifier_block *nb,
1461 unsigned int list)
1462{
1463 int ret = 0;
1464
1465 if (!devfreq)
1466 return -EINVAL;
1467
1468 switch (list) {
1469 case DEVFREQ_TRANSITION_NOTIFIER:
1470 ret = srcu_notifier_chain_unregister(
1471 &devfreq->transition_notifier_list, nb);
1472 break;
1473 default:
1474 ret = -EINVAL;
1475 }
1476
1477 return ret;
1478}
1479EXPORT_SYMBOL(devfreq_unregister_notifier);
1480
1481struct devfreq_notifier_devres {
1482 struct devfreq *devfreq;
1483 struct notifier_block *nb;
1484 unsigned int list;
1485};
1486
1487static void devm_devfreq_notifier_release(struct device *dev, void *res)
1488{
1489 struct devfreq_notifier_devres *this = res;
1490
1491 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1492}
1493
1494/**
1495 * devm_devfreq_register_notifier()
1496 - Resource-managed devfreq_register_notifier()
1497 * @dev: The devfreq user device. (parent of devfreq)
1498 * @devfreq: The devfreq object.
1499 * @nb: The notifier block to be unregistered.
1500 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1501 */
1502int devm_devfreq_register_notifier(struct device *dev,
1503 struct devfreq *devfreq,
1504 struct notifier_block *nb,
1505 unsigned int list)
1506{
1507 struct devfreq_notifier_devres *ptr;
1508 int ret;
1509
1510 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1511 GFP_KERNEL);
1512 if (!ptr)
1513 return -ENOMEM;
1514
1515 ret = devfreq_register_notifier(devfreq, nb, list);
1516 if (ret) {
1517 devres_free(ptr);
1518 return ret;
1519 }
1520
1521 ptr->devfreq = devfreq;
1522 ptr->nb = nb;
1523 ptr->list = list;
1524 devres_add(dev, ptr);
1525
1526 return 0;
1527}
1528EXPORT_SYMBOL(devm_devfreq_register_notifier);
1529
1530/**
1531 * devm_devfreq_unregister_notifier()
1532 - Resource-managed devfreq_unregister_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 */
1538void devm_devfreq_unregister_notifier(struct device *dev,
1539 struct devfreq *devfreq,
1540 struct notifier_block *nb,
1541 unsigned int list)
1542{
1543 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1544 devm_devfreq_dev_match, devfreq));
1545}
1546EXPORT_SYMBOL(devm_devfreq_unregister_notifier);