blob: 5dd0acaa799f9a925db257fdb9ff5a9f7b125958 [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
Leonard Crestezb99f20c2019-09-24 10:52:23 +0300161 lockdep_assert_held(&devfreq->lock);
Jonghwa Leee552bba2012-08-23 20:00:46 +0900162 cur_time = jiffies;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800163
Tobias Jakobid0563a02016-09-29 14:36:36 +0200164 /* Immediately exit if previous_freq is not initialized yet. */
165 if (!devfreq->previous_freq)
166 goto out;
167
Saravana Kannane35d35a2014-02-27 19:38:57 -0800168 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
169 if (prev_lev < 0) {
170 ret = prev_lev;
171 goto out;
172 }
173
174 devfreq->time_in_state[prev_lev] +=
Jonghwa Leee552bba2012-08-23 20:00:46 +0900175 cur_time - devfreq->last_stat_updated;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800176
177 lev = devfreq_get_freq_level(devfreq, freq);
178 if (lev < 0) {
179 ret = lev;
180 goto out;
181 }
182
183 if (lev != prev_lev) {
Jonghwa Leee552bba2012-08-23 20:00:46 +0900184 devfreq->trans_table[(prev_lev *
185 devfreq->profile->max_state) + lev]++;
186 devfreq->total_trans++;
187 }
Jonghwa Leee552bba2012-08-23 20:00:46 +0900188
Saravana Kannane35d35a2014-02-27 19:38:57 -0800189out:
190 devfreq->last_stat_updated = cur_time;
191 return ret;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900192}
Chanwoo Choife8f92c2017-01-31 15:38:17 +0900193EXPORT_SYMBOL(devfreq_update_status);
Jonghwa Leee552bba2012-08-23 20:00:46 +0900194
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500195/**
196 * find_devfreq_governor() - find devfreq governor from name
197 * @name: name of the governor
198 *
199 * Search the list of devfreq governors and return the matched
200 * governor's pointer. devfreq_list_lock should be held by the caller.
201 */
202static struct devfreq_governor *find_devfreq_governor(const char *name)
203{
204 struct devfreq_governor *tmp_governor;
205
Viresh Kumar9348da22015-08-10 11:42:25 +0530206 if (IS_ERR_OR_NULL(name)) {
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500207 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
208 return ERR_PTR(-EINVAL);
209 }
210 WARN(!mutex_is_locked(&devfreq_list_lock),
211 "devfreq_list_lock must be locked.");
212
213 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
214 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
215 return tmp_governor;
216 }
217
218 return ERR_PTR(-ENODEV);
219}
220
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900221static int devfreq_notify_transition(struct devfreq *devfreq,
222 struct devfreq_freqs *freqs, unsigned int state)
223{
224 if (!devfreq)
225 return -EINVAL;
226
227 switch (state) {
228 case DEVFREQ_PRECHANGE:
229 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
230 DEVFREQ_PRECHANGE, freqs);
231 break;
232
233 case DEVFREQ_POSTCHANGE:
234 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
235 DEVFREQ_POSTCHANGE, freqs);
236 break;
237 default:
238 return -EINVAL;
239 }
240
241 return 0;
242}
243
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200244/* Load monitoring helper functions for governors use */
245
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200246/**
247 * update_devfreq() - Reevaluate the device and configure frequency.
248 * @devfreq: the devfreq instance.
249 *
250 * Note: Lock devfreq->lock before calling update_devfreq
251 * This function is exported for governors.
252 */
253int update_devfreq(struct devfreq *devfreq)
254{
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900255 struct devfreq_freqs freqs;
256 unsigned long freq, cur_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200257 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100258 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200259
260 if (!mutex_is_locked(&devfreq->lock)) {
261 WARN(true, "devfreq->lock must be locked by the caller.\n");
262 return -EINVAL;
263 }
264
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500265 if (!devfreq->governor)
266 return -EINVAL;
267
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200268 /* Reevaluate the proper frequency */
269 err = devfreq->governor->get_target_freq(devfreq, &freq);
270 if (err)
271 return err;
272
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100273 /*
Javi Merinod3b7e172015-08-14 18:57:00 +0100274 * Adjust the frequency with user freq and QoS.
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100275 *
Javi Merinod3b7e172015-08-14 18:57:00 +0100276 * List from the highest priority
277 * max_freq
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100278 * min_freq
279 */
280
281 if (devfreq->min_freq && freq < devfreq->min_freq) {
282 freq = devfreq->min_freq;
283 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
284 }
285 if (devfreq->max_freq && freq > devfreq->max_freq) {
286 freq = devfreq->max_freq;
287 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
288 }
289
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900290 if (devfreq->profile->get_cur_freq)
291 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
292 else
293 cur_freq = devfreq->previous_freq;
294
295 freqs.old = cur_freq;
296 freqs.new = freq;
297 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
298
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100299 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
Chanwoo Choi0d371892016-06-23 11:18:43 +0900300 if (err) {
301 freqs.new = cur_freq;
302 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200303 return err;
Chanwoo Choi0d371892016-06-23 11:18:43 +0900304 }
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200305
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900306 freqs.new = freq;
307 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
308
Jonghwa Leee552bba2012-08-23 20:00:46 +0900309 if (devfreq->profile->freq_table)
310 if (devfreq_update_status(devfreq, freq))
311 dev_err(&devfreq->dev,
312 "Couldn't update frequency transition information.\n");
313
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200314 devfreq->previous_freq = freq;
315 return err;
316}
Nishanth Menon2df50212012-10-29 15:01:42 -0500317EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200318
319/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200320 * devfreq_monitor() - Periodically poll devfreq objects.
321 * @work: the work struct used to run devfreq_monitor periodically.
322 *
323 */
324static void devfreq_monitor(struct work_struct *work)
325{
326 int err;
327 struct devfreq *devfreq = container_of(work,
328 struct devfreq, work.work);
329
330 mutex_lock(&devfreq->lock);
331 err = update_devfreq(devfreq);
332 if (err)
333 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
334
335 queue_delayed_work(devfreq_wq, &devfreq->work,
336 msecs_to_jiffies(devfreq->profile->polling_ms));
337 mutex_unlock(&devfreq->lock);
338}
339
340/**
341 * devfreq_monitor_start() - Start load monitoring of devfreq instance
342 * @devfreq: the devfreq instance.
343 *
344 * Helper function for starting devfreq device load monitoing. By
345 * default delayed work based monitoring is supported. Function
346 * to be called from governor in response to DEVFREQ_GOV_START
347 * event when device is added to devfreq framework.
348 */
349void devfreq_monitor_start(struct devfreq *devfreq)
350{
351 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
352 if (devfreq->profile->polling_ms)
353 queue_delayed_work(devfreq_wq, &devfreq->work,
354 msecs_to_jiffies(devfreq->profile->polling_ms));
355}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100356EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200357
358/**
359 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
360 * @devfreq: the devfreq instance.
361 *
362 * Helper function to stop devfreq device load monitoing. Function
363 * to be called from governor in response to DEVFREQ_GOV_STOP
364 * event when device is removed from devfreq framework.
365 */
366void devfreq_monitor_stop(struct devfreq *devfreq)
367{
368 cancel_delayed_work_sync(&devfreq->work);
369}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100370EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200371
372/**
373 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
374 * @devfreq: the devfreq instance.
375 *
376 * Helper function to suspend devfreq device load monitoing. Function
377 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
378 * event or when polling interval is set to zero.
379 *
380 * Note: Though this function is same as devfreq_monitor_stop(),
381 * intentionally kept separate to provide hooks for collecting
382 * transition statistics.
383 */
384void devfreq_monitor_suspend(struct devfreq *devfreq)
385{
386 mutex_lock(&devfreq->lock);
387 if (devfreq->stop_polling) {
388 mutex_unlock(&devfreq->lock);
389 return;
390 }
391
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530392 devfreq_update_status(devfreq, devfreq->previous_freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200393 devfreq->stop_polling = true;
394 mutex_unlock(&devfreq->lock);
395 cancel_delayed_work_sync(&devfreq->work);
396}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100397EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200398
399/**
400 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
401 * @devfreq: the devfreq instance.
402 *
403 * Helper function to resume devfreq device load monitoing. Function
404 * to be called from governor in response to DEVFREQ_GOV_RESUME
405 * event or when polling interval is set to non-zero.
406 */
407void devfreq_monitor_resume(struct devfreq *devfreq)
408{
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530409 unsigned long freq;
410
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200411 mutex_lock(&devfreq->lock);
412 if (!devfreq->stop_polling)
413 goto out;
414
415 if (!delayed_work_pending(&devfreq->work) &&
416 devfreq->profile->polling_ms)
417 queue_delayed_work(devfreq_wq, &devfreq->work,
418 msecs_to_jiffies(devfreq->profile->polling_ms));
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530419
420 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200421 devfreq->stop_polling = false;
422
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530423 if (devfreq->profile->get_cur_freq &&
424 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
425 devfreq->previous_freq = freq;
426
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200427out:
428 mutex_unlock(&devfreq->lock);
429}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100430EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200431
432/**
433 * devfreq_interval_update() - Update device devfreq monitoring interval
434 * @devfreq: the devfreq instance.
435 * @delay: new polling interval to be set.
436 *
437 * Helper function to set new load monitoring polling interval. Function
438 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
439 */
440void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
441{
442 unsigned int cur_delay = devfreq->profile->polling_ms;
443 unsigned int new_delay = *delay;
444
445 mutex_lock(&devfreq->lock);
446 devfreq->profile->polling_ms = new_delay;
447
448 if (devfreq->stop_polling)
449 goto out;
450
451 /* if new delay is zero, stop polling */
452 if (!new_delay) {
453 mutex_unlock(&devfreq->lock);
454 cancel_delayed_work_sync(&devfreq->work);
455 return;
456 }
457
458 /* if current delay is zero, start polling with new delay */
459 if (!cur_delay) {
460 queue_delayed_work(devfreq_wq, &devfreq->work,
461 msecs_to_jiffies(devfreq->profile->polling_ms));
462 goto out;
463 }
464
465 /* if current delay is greater than new delay, restart polling */
466 if (cur_delay > new_delay) {
467 mutex_unlock(&devfreq->lock);
468 cancel_delayed_work_sync(&devfreq->work);
469 mutex_lock(&devfreq->lock);
470 if (!devfreq->stop_polling)
471 queue_delayed_work(devfreq_wq, &devfreq->work,
472 msecs_to_jiffies(devfreq->profile->polling_ms));
473 }
474out:
475 mutex_unlock(&devfreq->lock);
476}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100477EXPORT_SYMBOL(devfreq_interval_update);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200478
479/**
480 * devfreq_notifier_call() - Notify that the device frequency requirements
481 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200482 * @nb: the notifier_block (supposed to be devfreq->nb)
483 * @type: not used
484 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200485 *
486 * Called by a notifier that uses devfreq->nb.
487 */
488static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
489 void *devp)
490{
491 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
492 int ret;
493
494 mutex_lock(&devfreq->lock);
495 ret = update_devfreq(devfreq);
496 mutex_unlock(&devfreq->lock);
497
498 return ret;
499}
500
501/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200502 * _remove_devfreq() - Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200503 * @devfreq: the devfreq struct
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200504 */
Chanwoo Choi585fc832014-05-09 16:43:07 +0900505static void _remove_devfreq(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200506{
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200507 mutex_lock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200508 list_del(&devfreq->node);
509 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200510
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500511 if (devfreq->governor)
512 devfreq->governor->event_handler(devfreq,
513 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200514
515 if (devfreq->profile->exit)
516 devfreq->profile->exit(devfreq->dev.parent);
517
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200518 mutex_destroy(&devfreq->lock);
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -0800519 mutex_destroy(&devfreq->sysfs_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200520 kfree(devfreq);
521}
522
523/**
524 * devfreq_dev_release() - Callback for struct device to release the device.
525 * @dev: the devfreq device
526 *
527 * This calls _remove_devfreq() if _remove_devfreq() is not called.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200528 */
529static void devfreq_dev_release(struct device *dev)
530{
531 struct devfreq *devfreq = to_devfreq(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200532
Chanwoo Choi585fc832014-05-09 16:43:07 +0900533 _remove_devfreq(devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200534}
535
536/**
537 * devfreq_add_device() - Add devfreq feature to the device
538 * @dev: the device to add devfreq feature.
539 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500540 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200541 * @data: private data for the governor. The devfreq framework does not
542 * touch this value.
543 */
544struct devfreq *devfreq_add_device(struct device *dev,
545 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500546 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200547 void *data)
548{
549 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500550 struct devfreq_governor *governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200551 int err = 0;
552
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500553 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200554 dev_err(dev, "%s: Invalid parameters.\n", __func__);
555 return ERR_PTR(-EINVAL);
556 }
557
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200558 mutex_lock(&devfreq_list_lock);
559 devfreq = find_device_devfreq(dev);
560 mutex_unlock(&devfreq_list_lock);
561 if (!IS_ERR(devfreq)) {
562 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
563 err = -EINVAL;
564 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200565 }
566
567 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
568 if (!devfreq) {
569 dev_err(dev, "%s: Unable to create devfreq for the device\n",
570 __func__);
571 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100572 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200573 }
574
575 mutex_init(&devfreq->lock);
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -0800576 mutex_init(&devfreq->sysfs_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200577 mutex_lock(&devfreq->lock);
578 devfreq->dev.parent = dev;
579 devfreq->dev.class = devfreq_class;
580 devfreq->dev.release = devfreq_dev_release;
Leonard Crestezce13d562019-11-14 01:21:31 +0200581 INIT_LIST_HEAD(&devfreq->node);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200582 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500583 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200584 devfreq->previous_freq = profile->initial_freq;
Lukasz Luba8d39fc02016-05-31 11:25:09 +0100585 devfreq->last_status.current_frequency = profile->initial_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200586 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200587 devfreq->nb.notifier_call = devfreq_notifier_call;
588
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900589 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
590 mutex_unlock(&devfreq->lock);
591 devfreq_set_freq_table(devfreq);
592 mutex_lock(&devfreq->lock);
593 }
Rajagopal Venkatf517b3a2013-06-11 16:42:27 -0600594 devfreq_set_freq_limits(devfreq);
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900595
Kees Cook02aa2a32013-07-03 15:04:56 -0700596 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200597 err = device_register(&devfreq->dev);
598 if (err) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200599 mutex_unlock(&devfreq->lock);
Chanwoo Choif5c3fd82017-08-24 10:42:48 +0900600 goto err_dev;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200601 }
602
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900603 devfreq->trans_table = devm_kzalloc(&devfreq->dev, sizeof(unsigned int) *
604 devfreq->profile->max_state *
605 devfreq->profile->max_state,
606 GFP_KERNEL);
607 devfreq->time_in_state = devm_kzalloc(&devfreq->dev, sizeof(unsigned long) *
608 devfreq->profile->max_state,
609 GFP_KERNEL);
610 devfreq->last_stat_updated = jiffies;
611
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900612 srcu_init_notifier_head(&devfreq->transition_notifier_list);
613
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200614 mutex_unlock(&devfreq->lock);
615
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200616 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200617 list_add(&devfreq->node, &devfreq_list);
618
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500619 governor = find_devfreq_governor(devfreq->governor_name);
Chanwoo Choiebdfcaa2016-12-28 20:52:35 +0900620 if (IS_ERR(governor)) {
621 dev_err(dev, "%s: Unable to find governor for the device\n",
622 __func__);
623 err = PTR_ERR(governor);
624 goto err_init;
625 }
626
627 devfreq->governor = governor;
628 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
629 NULL);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200630 if (err) {
631 dev_err(dev, "%s: Unable to start governor for the device\n",
632 __func__);
633 goto err_init;
634 }
Axel Lin0f376c92016-09-29 10:13:28 +0800635 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200636
Axel Lin3f19f082011-11-15 21:59:09 +0100637 return devfreq;
638
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200639err_init:
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200640 list_del(&devfreq->node);
Axel Lin0f376c92016-09-29 10:13:28 +0800641 mutex_unlock(&devfreq_list_lock);
642
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200643 device_unregister(&devfreq->dev);
Chanwoo Choif5c3fd82017-08-24 10:42:48 +0900644err_dev:
645 if (devfreq)
646 kfree(devfreq);
Axel Lin3f19f082011-11-15 21:59:09 +0100647err_out:
648 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200649}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200650EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200651
652/**
653 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200654 * @devfreq: the devfreq instance to be removed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900655 *
656 * The opposite of devfreq_add_device().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200657 */
658int devfreq_remove_device(struct devfreq *devfreq)
659{
660 if (!devfreq)
661 return -EINVAL;
662
Chanwoo Choi585fc832014-05-09 16:43:07 +0900663 device_unregister(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200664
665 return 0;
666}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200667EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200668
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900669static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
670{
671 struct devfreq **r = res;
672
673 if (WARN_ON(!r || !*r))
674 return 0;
675
676 return *r == data;
677}
678
679static void devm_devfreq_dev_release(struct device *dev, void *res)
680{
681 devfreq_remove_device(*(struct devfreq **)res);
682}
683
684/**
685 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
686 * @dev: the device to add devfreq feature.
687 * @profile: device-specific profile to run devfreq.
688 * @governor_name: name of the policy to choose frequency.
689 * @data: private data for the governor. The devfreq framework does not
690 * touch this value.
691 *
692 * This function manages automatically the memory of devfreq device using device
693 * resource management and simplify the free operation for memory of devfreq
694 * device.
695 */
696struct devfreq *devm_devfreq_add_device(struct device *dev,
697 struct devfreq_dev_profile *profile,
698 const char *governor_name,
699 void *data)
700{
701 struct devfreq **ptr, *devfreq;
702
703 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
704 if (!ptr)
705 return ERR_PTR(-ENOMEM);
706
707 devfreq = devfreq_add_device(dev, profile, governor_name, data);
708 if (IS_ERR(devfreq)) {
709 devres_free(ptr);
Bjorn Anderssonb3685e82017-11-05 21:27:41 -0800710 return devfreq;
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900711 }
712
713 *ptr = devfreq;
714 devres_add(dev, ptr);
715
716 return devfreq;
717}
718EXPORT_SYMBOL(devm_devfreq_add_device);
719
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900720#ifdef CONFIG_OF
721/*
722 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
723 * @dev - instance to the given device
724 * @index - index into list of devfreq
725 *
726 * return the instance of devfreq device
727 */
728struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
729{
730 struct device_node *node;
731 struct devfreq *devfreq;
732
733 if (!dev)
734 return ERR_PTR(-EINVAL);
735
736 if (!dev->of_node)
737 return ERR_PTR(-EINVAL);
738
739 node = of_parse_phandle(dev->of_node, "devfreq", index);
740 if (!node)
741 return ERR_PTR(-ENODEV);
742
743 mutex_lock(&devfreq_list_lock);
744 list_for_each_entry(devfreq, &devfreq_list, node) {
745 if (devfreq->dev.parent
746 && devfreq->dev.parent->of_node == node) {
747 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800748 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900749 return devfreq;
750 }
751 }
752 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800753 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900754
755 return ERR_PTR(-EPROBE_DEFER);
756}
757#else
758struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
759{
760 return ERR_PTR(-ENODEV);
761}
762#endif /* CONFIG_OF */
763EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
764
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900765/**
766 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
767 * @dev: the device to add devfreq feature.
768 * @devfreq: the devfreq instance to be removed
769 */
770void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
771{
772 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
773 devm_devfreq_dev_match, devfreq));
774}
775EXPORT_SYMBOL(devm_devfreq_remove_device);
776
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200777/**
778 * devfreq_suspend_device() - Suspend devfreq of a device.
779 * @devfreq: the devfreq instance to be suspended
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900780 *
781 * This function is intended to be called by the pm callbacks
782 * (e.g., runtime_suspend, suspend) of the device driver that
783 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200784 */
785int devfreq_suspend_device(struct devfreq *devfreq)
786{
787 if (!devfreq)
788 return -EINVAL;
789
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500790 if (!devfreq->governor)
791 return 0;
792
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200793 return devfreq->governor->event_handler(devfreq,
794 DEVFREQ_GOV_SUSPEND, NULL);
795}
796EXPORT_SYMBOL(devfreq_suspend_device);
797
798/**
799 * devfreq_resume_device() - Resume devfreq of a device.
800 * @devfreq: the devfreq instance to be resumed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900801 *
802 * This function is intended to be called by the pm callbacks
803 * (e.g., runtime_resume, resume) of the device driver that
804 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200805 */
806int devfreq_resume_device(struct devfreq *devfreq)
807{
808 if (!devfreq)
809 return -EINVAL;
810
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500811 if (!devfreq->governor)
812 return 0;
813
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200814 return devfreq->governor->event_handler(devfreq,
815 DEVFREQ_GOV_RESUME, NULL);
816}
817EXPORT_SYMBOL(devfreq_resume_device);
818
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500819/**
820 * devfreq_add_governor() - Add devfreq governor
821 * @governor: the devfreq governor to be added
822 */
823int devfreq_add_governor(struct devfreq_governor *governor)
824{
825 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500826 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500827 int err = 0;
828
829 if (!governor) {
830 pr_err("%s: Invalid parameters.\n", __func__);
831 return -EINVAL;
832 }
833
834 mutex_lock(&devfreq_list_lock);
835 g = find_devfreq_governor(governor->name);
836 if (!IS_ERR(g)) {
837 pr_err("%s: governor %s already registered\n", __func__,
838 g->name);
839 err = -EINVAL;
840 goto err_out;
841 }
842
843 list_add(&governor->node, &devfreq_governor_list);
844
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500845 list_for_each_entry(devfreq, &devfreq_list, node) {
846 int ret = 0;
847 struct device *dev = devfreq->dev.parent;
848
849 if (!strncmp(devfreq->governor_name, governor->name,
850 DEVFREQ_NAME_LEN)) {
851 /* The following should never occur */
852 if (devfreq->governor) {
853 dev_warn(dev,
854 "%s: Governor %s already present\n",
855 __func__, devfreq->governor->name);
856 ret = devfreq->governor->event_handler(devfreq,
857 DEVFREQ_GOV_STOP, NULL);
858 if (ret) {
859 dev_warn(dev,
860 "%s: Governor %s stop = %d\n",
861 __func__,
862 devfreq->governor->name, ret);
863 }
864 /* Fall through */
865 }
866 devfreq->governor = governor;
867 ret = devfreq->governor->event_handler(devfreq,
868 DEVFREQ_GOV_START, NULL);
869 if (ret) {
870 dev_warn(dev, "%s: Governor %s start=%d\n",
871 __func__, devfreq->governor->name,
872 ret);
873 }
874 }
875 }
876
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500877err_out:
878 mutex_unlock(&devfreq_list_lock);
879
880 return err;
881}
882EXPORT_SYMBOL(devfreq_add_governor);
883
884/**
885 * devfreq_remove_device() - Remove devfreq feature from a device.
886 * @governor: the devfreq governor to be removed
887 */
888int devfreq_remove_governor(struct devfreq_governor *governor)
889{
890 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500891 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500892 int err = 0;
893
894 if (!governor) {
895 pr_err("%s: Invalid parameters.\n", __func__);
896 return -EINVAL;
897 }
898
899 mutex_lock(&devfreq_list_lock);
900 g = find_devfreq_governor(governor->name);
901 if (IS_ERR(g)) {
902 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamatb9e1c8e2012-11-21 10:36:13 +0530903 governor->name);
Sachin Kamatf9c08e22012-11-21 10:36:14 +0530904 err = PTR_ERR(g);
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500905 goto err_out;
906 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500907 list_for_each_entry(devfreq, &devfreq_list, node) {
908 int ret;
909 struct device *dev = devfreq->dev.parent;
910
911 if (!strncmp(devfreq->governor_name, governor->name,
912 DEVFREQ_NAME_LEN)) {
913 /* we should have a devfreq governor! */
914 if (!devfreq->governor) {
915 dev_warn(dev, "%s: Governor %s NOT present\n",
916 __func__, governor->name);
917 continue;
918 /* Fall through */
919 }
920 ret = devfreq->governor->event_handler(devfreq,
921 DEVFREQ_GOV_STOP, NULL);
922 if (ret) {
923 dev_warn(dev, "%s: Governor %s stop=%d\n",
924 __func__, devfreq->governor->name,
925 ret);
926 }
927 devfreq->governor = NULL;
928 }
929 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500930
931 list_del(&governor->node);
932err_out:
933 mutex_unlock(&devfreq_list_lock);
934
935 return err;
936}
937EXPORT_SYMBOL(devfreq_remove_governor);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200938
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700939static ssize_t governor_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200940 struct device_attribute *attr, char *buf)
941{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500942 if (!to_devfreq(dev)->governor)
943 return -EINVAL;
944
MyungJoo Ham9005b652011-10-02 00:19:28 +0200945 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
946}
947
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700948static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500949 const char *buf, size_t count)
950{
951 struct devfreq *df = to_devfreq(dev);
952 int ret;
953 char str_governor[DEVFREQ_NAME_LEN + 1];
Saravana Kannan7897df32016-10-24 16:35:05 -0700954 const struct devfreq_governor *governor, *prev_gov;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500955
956 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
957 if (ret != 1)
958 return -EINVAL;
959
960 mutex_lock(&devfreq_list_lock);
961 governor = find_devfreq_governor(str_governor);
962 if (IS_ERR(governor)) {
963 ret = PTR_ERR(governor);
964 goto out;
965 }
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200966 if (df->governor == governor) {
967 ret = 0;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500968 goto out;
Gustavo A. R. Silva715f7e32017-12-06 14:20:15 -0600969 } else if ((df->governor && df->governor->immutable) ||
970 governor->immutable) {
Chanwoo Choi2294b772017-01-31 15:38:16 +0900971 ret = -EINVAL;
972 goto out;
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200973 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500974
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -0800975 mutex_lock(&df->sysfs_lock);
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500976 if (df->governor) {
977 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
978 if (ret) {
979 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
980 __func__, df->governor->name, ret);
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -0800981 goto gov_stop_out;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500982 }
983 }
Saravana Kannan7897df32016-10-24 16:35:05 -0700984 prev_gov = df->governor;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500985 df->governor = governor;
986 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
987 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
Saravana Kannan7897df32016-10-24 16:35:05 -0700988 if (ret) {
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500989 dev_warn(dev, "%s: Governor %s not started(%d)\n",
990 __func__, df->governor->name, ret);
Saravana Kannan7897df32016-10-24 16:35:05 -0700991 if (prev_gov) {
992 df->governor = prev_gov;
993 strlcpy(df->governor_name, prev_gov->name,
994 DEVFREQ_NAME_LEN);
995 df->governor->event_handler(df, DEVFREQ_GOV_START,
996 NULL);
997 }
998 }
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -0800999
1000gov_stop_out:
1001 mutex_unlock(&df->sysfs_lock);
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001002out:
1003 mutex_unlock(&devfreq_list_lock);
1004
1005 if (!ret)
1006 ret = count;
1007 return ret;
1008}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001009static DEVICE_ATTR_RW(governor);
1010
1011static ssize_t available_governors_show(struct device *d,
1012 struct device_attribute *attr,
1013 char *buf)
Nishanth Menon50a5b332012-10-29 15:01:48 -05001014{
Chanwoo Choi2294b772017-01-31 15:38:16 +09001015 struct devfreq *df = to_devfreq(d);
Nishanth Menon50a5b332012-10-29 15:01:48 -05001016 ssize_t count = 0;
1017
1018 mutex_lock(&devfreq_list_lock);
Chanwoo Choi2294b772017-01-31 15:38:16 +09001019
1020 /*
1021 * The devfreq with immutable governor (e.g., passive) shows
1022 * only own governor.
1023 */
Leonard Crestezb110cf22019-09-24 10:26:53 +03001024 if (df->governor && df->governor->immutable) {
Chanwoo Choi2294b772017-01-31 15:38:16 +09001025 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1026 "%s ", df->governor_name);
1027 /*
1028 * The devfreq device shows the registered governor except for
1029 * immutable governors such as passive governor .
1030 */
1031 } else {
1032 struct devfreq_governor *governor;
1033
1034 list_for_each_entry(governor, &devfreq_governor_list, node) {
1035 if (governor->immutable)
1036 continue;
1037 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1038 "%s ", governor->name);
1039 }
1040 }
1041
Nishanth Menon50a5b332012-10-29 15:01:48 -05001042 mutex_unlock(&devfreq_list_lock);
1043
1044 /* Truncate the trailing space */
1045 if (count)
1046 count--;
1047
1048 count += sprintf(&buf[count], "\n");
1049
1050 return count;
1051}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001052static DEVICE_ATTR_RO(available_governors);
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001053
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001054static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1055 char *buf)
MyungJoo Ham9005b652011-10-02 00:19:28 +02001056{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001057 unsigned long freq;
1058 struct devfreq *devfreq = to_devfreq(dev);
1059
1060 if (devfreq->profile->get_cur_freq &&
1061 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1062 return sprintf(buf, "%lu\n", freq);
1063
1064 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1065}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001066static DEVICE_ATTR_RO(cur_freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001067
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001068static ssize_t target_freq_show(struct device *dev,
1069 struct device_attribute *attr, char *buf)
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001070{
MyungJoo Ham9005b652011-10-02 00:19:28 +02001071 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1072}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001073static DEVICE_ATTR_RO(target_freq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001074
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001075static ssize_t polling_interval_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001076 struct device_attribute *attr, char *buf)
1077{
1078 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1079}
1080
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001081static ssize_t polling_interval_store(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001082 struct device_attribute *attr,
1083 const char *buf, size_t count)
1084{
1085 struct devfreq *df = to_devfreq(dev);
1086 unsigned int value;
1087 int ret;
1088
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001089 if (!df->governor)
1090 return -EINVAL;
1091
MyungJoo Ham9005b652011-10-02 00:19:28 +02001092 ret = sscanf(buf, "%u", &value);
1093 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001094 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +02001095
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -08001096 mutex_lock(&df->sysfs_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001097 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001098 ret = count;
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -08001099 mutex_unlock(&df->sysfs_lock);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001100
MyungJoo Ham9005b652011-10-02 00:19:28 +02001101 return ret;
1102}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001103static DEVICE_ATTR_RW(polling_interval);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001104
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001105static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001106 const char *buf, size_t count)
1107{
1108 struct devfreq *df = to_devfreq(dev);
1109 unsigned long value;
1110 int ret;
1111 unsigned long max;
1112
1113 ret = sscanf(buf, "%lu", &value);
1114 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001115 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001116
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -08001117 mutex_lock(&df->sysfs_lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001118 mutex_lock(&df->lock);
1119 max = df->max_freq;
1120 if (value && max && value > max) {
1121 ret = -EINVAL;
1122 goto unlock;
1123 }
1124
1125 df->min_freq = value;
1126 update_devfreq(df);
1127 ret = count;
1128unlock:
1129 mutex_unlock(&df->lock);
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -08001130 mutex_unlock(&df->sysfs_lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001131 return ret;
1132}
1133
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001134static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001135 const char *buf, size_t count)
1136{
1137 struct devfreq *df = to_devfreq(dev);
1138 unsigned long value;
1139 int ret;
1140 unsigned long min;
1141
1142 ret = sscanf(buf, "%lu", &value);
1143 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001144 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001145
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -08001146 mutex_lock(&df->sysfs_lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001147 mutex_lock(&df->lock);
1148 min = df->min_freq;
1149 if (value && min && value < min) {
1150 ret = -EINVAL;
1151 goto unlock;
1152 }
1153
1154 df->max_freq = value;
1155 update_devfreq(df);
1156 ret = count;
1157unlock:
1158 mutex_unlock(&df->lock);
Jonathan Avila5a2f4ce2017-12-01 16:45:18 -08001159 mutex_unlock(&df->sysfs_lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001160 return ret;
1161}
1162
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001163#define show_one(name) \
1164static ssize_t name##_show \
1165(struct device *dev, struct device_attribute *attr, char *buf) \
1166{ \
1167 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001168}
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001169show_one(min_freq);
1170show_one(max_freq);
1171
1172static DEVICE_ATTR_RW(min_freq);
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001173static DEVICE_ATTR_RW(max_freq);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001174
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001175static ssize_t available_frequencies_show(struct device *d,
1176 struct device_attribute *attr,
1177 char *buf)
Nishanth Menond287de82012-10-25 19:48:59 -05001178{
1179 struct devfreq *df = to_devfreq(d);
1180 struct device *dev = df->dev.parent;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001181 struct dev_pm_opp *opp;
Saravana Kannan345ca9622014-04-10 16:46:25 -07001182 unsigned int i = 0, max_state = df->profile->max_state;
1183 bool use_opp;
Nishanth Menond287de82012-10-25 19:48:59 -05001184 ssize_t count = 0;
1185 unsigned long freq = 0;
1186
1187 rcu_read_lock();
Saravana Kannan345ca9622014-04-10 16:46:25 -07001188 use_opp = dev_pm_opp_get_opp_count(dev) > 0;
1189 while (use_opp || (!use_opp && i < max_state)) {
1190 if (use_opp) {
1191 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
1192 if (IS_ERR(opp))
1193 break;
1194 } else {
1195 freq = df->profile->freq_table[i++];
1196 }
Nishanth Menond287de82012-10-25 19:48:59 -05001197
1198 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1199 "%lu ", freq);
1200 freq++;
Saravana Kannan345ca9622014-04-10 16:46:25 -07001201 }
Nishanth Menond287de82012-10-25 19:48:59 -05001202 rcu_read_unlock();
1203
1204 /* Truncate the trailing space */
1205 if (count)
1206 count--;
1207
1208 count += sprintf(&buf[count], "\n");
1209
1210 return count;
1211}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001212static DEVICE_ATTR_RO(available_frequencies);
Nishanth Menond287de82012-10-25 19:48:59 -05001213
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001214static ssize_t trans_stat_show(struct device *dev,
1215 struct device_attribute *attr, char *buf)
Jonghwa Leee552bba2012-08-23 20:00:46 +09001216{
1217 struct devfreq *devfreq = to_devfreq(dev);
1218 ssize_t len;
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301219 int i, j;
Jonghwa Leee552bba2012-08-23 20:00:46 +09001220 unsigned int max_state = devfreq->profile->max_state;
1221
MyungJoo Ham34bd3222015-11-23 15:45:36 +09001222 if (max_state == 0)
1223 return sprintf(buf, "Not Supported.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001224
Leonard Crestezb99f20c2019-09-24 10:52:23 +03001225 mutex_lock(&devfreq->lock);
1226 if (!devfreq->stop_polling &&
1227 devfreq_update_status(devfreq, devfreq->previous_freq)) {
1228 mutex_unlock(&devfreq->lock);
1229 return 0;
1230 }
1231 mutex_unlock(&devfreq->lock);
1232
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001233 len = sprintf(buf, " From : To\n");
1234 len += sprintf(buf + len, " :");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001235 for (i = 0; i < max_state; i++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001236 len += sprintf(buf + len, "%10lu",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001237 devfreq->profile->freq_table[i]);
1238
1239 len += sprintf(buf + len, " time(ms)\n");
1240
1241 for (i = 0; i < max_state; i++) {
1242 if (devfreq->profile->freq_table[i]
1243 == devfreq->previous_freq) {
1244 len += sprintf(buf + len, "*");
1245 } else {
1246 len += sprintf(buf + len, " ");
1247 }
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001248 len += sprintf(buf + len, "%10lu:",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001249 devfreq->profile->freq_table[i]);
1250 for (j = 0; j < max_state; j++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001251 len += sprintf(buf + len, "%10u",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001252 devfreq->trans_table[(i * max_state) + j]);
1253 len += sprintf(buf + len, "%10u\n",
1254 jiffies_to_msecs(devfreq->time_in_state[i]));
1255 }
1256
1257 len += sprintf(buf + len, "Total transition : %u\n",
1258 devfreq->total_trans);
1259 return len;
1260}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001261static DEVICE_ATTR_RO(trans_stat);
Jonghwa Leee552bba2012-08-23 20:00:46 +09001262
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001263static struct attribute *devfreq_attrs[] = {
1264 &dev_attr_governor.attr,
1265 &dev_attr_available_governors.attr,
1266 &dev_attr_cur_freq.attr,
1267 &dev_attr_available_frequencies.attr,
1268 &dev_attr_target_freq.attr,
1269 &dev_attr_polling_interval.attr,
1270 &dev_attr_min_freq.attr,
1271 &dev_attr_max_freq.attr,
1272 &dev_attr_trans_stat.attr,
1273 NULL,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001274};
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001275ATTRIBUTE_GROUPS(devfreq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001276
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001277static int __init devfreq_init(void)
1278{
1279 devfreq_class = class_create(THIS_MODULE, "devfreq");
1280 if (IS_ERR(devfreq_class)) {
1281 pr_err("%s: couldn't create class\n", __FILE__);
1282 return PTR_ERR(devfreq_class);
1283 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001284
1285 devfreq_wq = create_freezable_workqueue("devfreq_wq");
Dan Carpenterea7f4542013-08-15 10:55:10 +03001286 if (!devfreq_wq) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001287 class_destroy(devfreq_class);
1288 pr_err("%s: couldn't create workqueue\n", __FILE__);
Dan Carpenterea7f4542013-08-15 10:55:10 +03001289 return -ENOMEM;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001290 }
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001291 devfreq_class->dev_groups = devfreq_groups;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001292
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001293 return 0;
1294}
1295subsys_initcall(devfreq_init);
1296
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001297/*
1298 * The followings are helper functions for devfreq user device drivers with
1299 * OPP framework.
1300 */
1301
1302/**
1303 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1304 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001305 * @dev: The devfreq user device. (parent of devfreq)
1306 * @freq: The frequency given to target function
1307 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001308 *
Nishanth Menonbcb27542013-01-18 19:52:34 +00001309 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1310 * protected pointer. The reason for the same is that the opp pointer which is
1311 * returned will remain valid for use with opp_get_{voltage, freq} only while
1312 * under the locked area. The pointer returned must be used prior to unlocking
1313 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001314 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001315struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1316 unsigned long *freq,
1317 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001318{
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001319 struct dev_pm_opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001320
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001321 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1322 /* The freq is an upper bound. opp should be lower */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001323 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001324
1325 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001326 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001327 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001328 } else {
1329 /* The freq is an lower bound. opp should be higher */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001330 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001331
1332 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001333 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001334 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001335 }
1336
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001337 return opp;
1338}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001339EXPORT_SYMBOL(devfreq_recommended_opp);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001340
1341/**
1342 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1343 * for any changes in the OPP availability
1344 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001345 * @dev: The devfreq user device. (parent of devfreq)
1346 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001347 */
1348int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1349{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001350 struct srcu_notifier_head *nh;
1351 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001352
MyungJoo Ham389baed2012-11-21 19:04:51 +09001353 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001354 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001355 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001356 ret = PTR_ERR(nh);
1357 rcu_read_unlock();
1358 if (!ret)
1359 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1360
1361 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001362}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001363EXPORT_SYMBOL(devfreq_register_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001364
1365/**
1366 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1367 * notified for any changes in the OPP
1368 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001369 * @dev: The devfreq user device. (parent of devfreq)
1370 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001371 *
1372 * At exit() callback of devfreq_dev_profile, this must be included if
1373 * devfreq_recommended_opp is used.
1374 */
1375int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1376{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001377 struct srcu_notifier_head *nh;
1378 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001379
MyungJoo Ham389baed2012-11-21 19:04:51 +09001380 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001381 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001382 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001383 ret = PTR_ERR(nh);
1384 rcu_read_unlock();
1385 if (!ret)
1386 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1387
1388 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001389}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001390EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001391
Chanwoo Choid5b040d2014-05-09 16:43:09 +09001392static void devm_devfreq_opp_release(struct device *dev, void *res)
1393{
1394 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1395}
1396
1397/**
1398 * devm_ devfreq_register_opp_notifier()
1399 * - Resource-managed devfreq_register_opp_notifier()
1400 * @dev: The devfreq user device. (parent of devfreq)
1401 * @devfreq: The devfreq object.
1402 */
1403int devm_devfreq_register_opp_notifier(struct device *dev,
1404 struct devfreq *devfreq)
1405{
1406 struct devfreq **ptr;
1407 int ret;
1408
1409 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1410 if (!ptr)
1411 return -ENOMEM;
1412
1413 ret = devfreq_register_opp_notifier(dev, devfreq);
1414 if (ret) {
1415 devres_free(ptr);
1416 return ret;
1417 }
1418
1419 *ptr = devfreq;
1420 devres_add(dev, ptr);
1421
1422 return 0;
1423}
1424EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1425
1426/**
1427 * devm_devfreq_unregister_opp_notifier()
1428 * - Resource-managed devfreq_unregister_opp_notifier()
1429 * @dev: The devfreq user device. (parent of devfreq)
1430 * @devfreq: The devfreq object.
1431 */
1432void devm_devfreq_unregister_opp_notifier(struct device *dev,
1433 struct devfreq *devfreq)
1434{
1435 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1436 devm_devfreq_dev_match, devfreq));
1437}
1438EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1439
Chanwoo Choi0fe3a662016-01-26 13:21:26 +09001440/**
1441 * devfreq_register_notifier() - Register a driver with devfreq
1442 * @devfreq: The devfreq object.
1443 * @nb: The notifier block to register.
1444 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1445 */
1446int devfreq_register_notifier(struct devfreq *devfreq,
1447 struct notifier_block *nb,
1448 unsigned int list)
1449{
1450 int ret = 0;
1451
1452 if (!devfreq)
1453 return -EINVAL;
1454
1455 switch (list) {
1456 case DEVFREQ_TRANSITION_NOTIFIER:
1457 ret = srcu_notifier_chain_register(
1458 &devfreq->transition_notifier_list, nb);
1459 break;
1460 default:
1461 ret = -EINVAL;
1462 }
1463
1464 return ret;
1465}
1466EXPORT_SYMBOL(devfreq_register_notifier);
1467
1468/*
1469 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1470 * @devfreq: The devfreq object.
1471 * @nb: The notifier block to be unregistered.
1472 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1473 */
1474int devfreq_unregister_notifier(struct devfreq *devfreq,
1475 struct notifier_block *nb,
1476 unsigned int list)
1477{
1478 int ret = 0;
1479
1480 if (!devfreq)
1481 return -EINVAL;
1482
1483 switch (list) {
1484 case DEVFREQ_TRANSITION_NOTIFIER:
1485 ret = srcu_notifier_chain_unregister(
1486 &devfreq->transition_notifier_list, nb);
1487 break;
1488 default:
1489 ret = -EINVAL;
1490 }
1491
1492 return ret;
1493}
1494EXPORT_SYMBOL(devfreq_unregister_notifier);
1495
1496struct devfreq_notifier_devres {
1497 struct devfreq *devfreq;
1498 struct notifier_block *nb;
1499 unsigned int list;
1500};
1501
1502static void devm_devfreq_notifier_release(struct device *dev, void *res)
1503{
1504 struct devfreq_notifier_devres *this = res;
1505
1506 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1507}
1508
1509/**
1510 * devm_devfreq_register_notifier()
1511 - Resource-managed devfreq_register_notifier()
1512 * @dev: The devfreq user device. (parent of devfreq)
1513 * @devfreq: The devfreq object.
1514 * @nb: The notifier block to be unregistered.
1515 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1516 */
1517int devm_devfreq_register_notifier(struct device *dev,
1518 struct devfreq *devfreq,
1519 struct notifier_block *nb,
1520 unsigned int list)
1521{
1522 struct devfreq_notifier_devres *ptr;
1523 int ret;
1524
1525 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1526 GFP_KERNEL);
1527 if (!ptr)
1528 return -ENOMEM;
1529
1530 ret = devfreq_register_notifier(devfreq, nb, list);
1531 if (ret) {
1532 devres_free(ptr);
1533 return ret;
1534 }
1535
1536 ptr->devfreq = devfreq;
1537 ptr->nb = nb;
1538 ptr->list = list;
1539 devres_add(dev, ptr);
1540
1541 return 0;
1542}
1543EXPORT_SYMBOL(devm_devfreq_register_notifier);
1544
1545/**
1546 * devm_devfreq_unregister_notifier()
1547 - Resource-managed devfreq_unregister_notifier()
1548 * @dev: The devfreq user device. (parent of devfreq)
1549 * @devfreq: The devfreq object.
1550 * @nb: The notifier block to be unregistered.
1551 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1552 */
1553void devm_devfreq_unregister_notifier(struct device *dev,
1554 struct devfreq *devfreq,
1555 struct notifier_block *nb,
1556 unsigned int list)
1557{
1558 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1559 devm_devfreq_dev_match, devfreq));
1560}
1561EXPORT_SYMBOL(devm_devfreq_unregister_notifier);