blob: 2e685164c54958a72f25555c85fe863cd242caee [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/**
Jonghwa Leee552bba2012-08-23 20:00:46 +090073 * devfreq_get_freq_level() - Lookup freq_table for the frequency
74 * @devfreq: the devfreq instance
75 * @freq: the target frequency
76 */
77static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
78{
79 int lev;
80
81 for (lev = 0; lev < devfreq->profile->max_state; lev++)
82 if (freq == devfreq->profile->freq_table[lev])
83 return lev;
84
85 return -EINVAL;
86}
87
88/**
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +090089 * devfreq_set_freq_table() - Initialize freq_table for the frequency
90 * @devfreq: the devfreq instance
91 */
92static void devfreq_set_freq_table(struct devfreq *devfreq)
93{
94 struct devfreq_dev_profile *profile = devfreq->profile;
95 struct dev_pm_opp *opp;
96 unsigned long freq;
97 int i, count;
98
99 /* Initialize the freq_table from OPP table */
100 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
101 if (count <= 0)
102 return;
103
104 profile->max_state = count;
105 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
106 profile->max_state,
107 sizeof(*profile->freq_table),
108 GFP_KERNEL);
109 if (!profile->freq_table) {
110 profile->max_state = 0;
111 return;
112 }
113
114 rcu_read_lock();
115 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
116 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
117 if (IS_ERR(opp)) {
118 devm_kfree(devfreq->dev.parent, profile->freq_table);
119 profile->max_state = 0;
120 rcu_read_unlock();
121 return;
122 }
123 profile->freq_table[i] = freq;
124 }
125 rcu_read_unlock();
126}
127
128/**
Jonghwa Leee552bba2012-08-23 20:00:46 +0900129 * devfreq_update_status() - Update statistics of devfreq behavior
130 * @devfreq: the devfreq instance
131 * @freq: the update target frequency
132 */
Chanwoo Choi30582c22017-01-31 15:38:17 +0900133int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
Jonghwa Leee552bba2012-08-23 20:00:46 +0900134{
Saravana Kannane35d35a2014-02-27 19:38:57 -0800135 int lev, prev_lev, ret = 0;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900136 unsigned long cur_time;
137
Jonghwa Leee552bba2012-08-23 20:00:46 +0900138 cur_time = jiffies;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800139
Tobias Jakobid0563a02016-09-29 14:36:36 +0200140 /* Immediately exit if previous_freq is not initialized yet. */
141 if (!devfreq->previous_freq)
142 goto out;
143
Saravana Kannane35d35a2014-02-27 19:38:57 -0800144 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
145 if (prev_lev < 0) {
146 ret = prev_lev;
147 goto out;
148 }
149
150 devfreq->time_in_state[prev_lev] +=
Jonghwa Leee552bba2012-08-23 20:00:46 +0900151 cur_time - devfreq->last_stat_updated;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800152
153 lev = devfreq_get_freq_level(devfreq, freq);
154 if (lev < 0) {
155 ret = lev;
156 goto out;
157 }
158
159 if (lev != prev_lev) {
Jonghwa Leee552bba2012-08-23 20:00:46 +0900160 devfreq->trans_table[(prev_lev *
161 devfreq->profile->max_state) + lev]++;
162 devfreq->total_trans++;
163 }
Jonghwa Leee552bba2012-08-23 20:00:46 +0900164
Saravana Kannane35d35a2014-02-27 19:38:57 -0800165out:
166 devfreq->last_stat_updated = cur_time;
167 return ret;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900168}
Chanwoo Choi30582c22017-01-31 15:38:17 +0900169EXPORT_SYMBOL(devfreq_update_status);
Jonghwa Leee552bba2012-08-23 20:00:46 +0900170
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500171/**
172 * find_devfreq_governor() - find devfreq governor from name
173 * @name: name of the governor
174 *
175 * Search the list of devfreq governors and return the matched
176 * governor's pointer. devfreq_list_lock should be held by the caller.
177 */
178static struct devfreq_governor *find_devfreq_governor(const char *name)
179{
180 struct devfreq_governor *tmp_governor;
181
Viresh Kumar9348da22015-08-10 11:42:25 +0530182 if (IS_ERR_OR_NULL(name)) {
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500183 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
184 return ERR_PTR(-EINVAL);
185 }
186 WARN(!mutex_is_locked(&devfreq_list_lock),
187 "devfreq_list_lock must be locked.");
188
189 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
190 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
191 return tmp_governor;
192 }
193
194 return ERR_PTR(-ENODEV);
195}
196
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900197static int devfreq_notify_transition(struct devfreq *devfreq,
198 struct devfreq_freqs *freqs, unsigned int state)
199{
200 if (!devfreq)
201 return -EINVAL;
202
203 switch (state) {
204 case DEVFREQ_PRECHANGE:
205 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
206 DEVFREQ_PRECHANGE, freqs);
207 break;
208
209 case DEVFREQ_POSTCHANGE:
210 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
211 DEVFREQ_POSTCHANGE, freqs);
212 break;
213 default:
214 return -EINVAL;
215 }
216
217 return 0;
218}
219
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200220/* Load monitoring helper functions for governors use */
221
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200222/**
223 * update_devfreq() - Reevaluate the device and configure frequency.
224 * @devfreq: the devfreq instance.
225 *
226 * Note: Lock devfreq->lock before calling update_devfreq
227 * This function is exported for governors.
228 */
229int update_devfreq(struct devfreq *devfreq)
230{
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900231 struct devfreq_freqs freqs;
232 unsigned long freq, cur_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200233 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100234 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200235
236 if (!mutex_is_locked(&devfreq->lock)) {
237 WARN(true, "devfreq->lock must be locked by the caller.\n");
238 return -EINVAL;
239 }
240
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500241 if (!devfreq->governor)
242 return -EINVAL;
243
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200244 /* Reevaluate the proper frequency */
245 err = devfreq->governor->get_target_freq(devfreq, &freq);
246 if (err)
247 return err;
248
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100249 /*
Javi Merinod3b7e172015-08-14 18:57:00 +0100250 * Adjust the frequency with user freq and QoS.
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100251 *
Javi Merinod3b7e172015-08-14 18:57:00 +0100252 * List from the highest priority
253 * max_freq
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100254 * min_freq
255 */
256
257 if (devfreq->min_freq && freq < devfreq->min_freq) {
258 freq = devfreq->min_freq;
259 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
260 }
261 if (devfreq->max_freq && freq > devfreq->max_freq) {
262 freq = devfreq->max_freq;
263 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
264 }
265
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900266 if (devfreq->profile->get_cur_freq)
267 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
268 else
269 cur_freq = devfreq->previous_freq;
270
271 freqs.old = cur_freq;
272 freqs.new = freq;
273 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
274
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100275 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
Chanwoo Choi0d371892016-06-23 11:18:43 +0900276 if (err) {
277 freqs.new = cur_freq;
278 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200279 return err;
Chanwoo Choi0d371892016-06-23 11:18:43 +0900280 }
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200281
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900282 freqs.new = freq;
283 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
284
Jonghwa Leee552bba2012-08-23 20:00:46 +0900285 if (devfreq->profile->freq_table)
286 if (devfreq_update_status(devfreq, freq))
287 dev_err(&devfreq->dev,
288 "Couldn't update frequency transition information.\n");
289
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200290 devfreq->previous_freq = freq;
291 return err;
292}
Nishanth Menon2df50212012-10-29 15:01:42 -0500293EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200294
295/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200296 * devfreq_monitor() - Periodically poll devfreq objects.
297 * @work: the work struct used to run devfreq_monitor periodically.
298 *
299 */
300static void devfreq_monitor(struct work_struct *work)
301{
302 int err;
303 struct devfreq *devfreq = container_of(work,
304 struct devfreq, work.work);
305
306 mutex_lock(&devfreq->lock);
307 err = update_devfreq(devfreq);
308 if (err)
309 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
310
311 queue_delayed_work(devfreq_wq, &devfreq->work,
312 msecs_to_jiffies(devfreq->profile->polling_ms));
313 mutex_unlock(&devfreq->lock);
314}
315
316/**
317 * devfreq_monitor_start() - Start load monitoring of devfreq instance
318 * @devfreq: the devfreq instance.
319 *
320 * Helper function for starting devfreq device load monitoing. By
321 * default delayed work based monitoring is supported. Function
322 * to be called from governor in response to DEVFREQ_GOV_START
323 * event when device is added to devfreq framework.
324 */
325void devfreq_monitor_start(struct devfreq *devfreq)
326{
327 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
328 if (devfreq->profile->polling_ms)
329 queue_delayed_work(devfreq_wq, &devfreq->work,
330 msecs_to_jiffies(devfreq->profile->polling_ms));
331}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100332EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200333
334/**
335 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
336 * @devfreq: the devfreq instance.
337 *
338 * Helper function to stop devfreq device load monitoing. Function
339 * to be called from governor in response to DEVFREQ_GOV_STOP
340 * event when device is removed from devfreq framework.
341 */
342void devfreq_monitor_stop(struct devfreq *devfreq)
343{
344 cancel_delayed_work_sync(&devfreq->work);
345}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100346EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200347
348/**
349 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
350 * @devfreq: the devfreq instance.
351 *
352 * Helper function to suspend devfreq device load monitoing. Function
353 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
354 * event or when polling interval is set to zero.
355 *
356 * Note: Though this function is same as devfreq_monitor_stop(),
357 * intentionally kept separate to provide hooks for collecting
358 * transition statistics.
359 */
360void devfreq_monitor_suspend(struct devfreq *devfreq)
361{
362 mutex_lock(&devfreq->lock);
363 if (devfreq->stop_polling) {
364 mutex_unlock(&devfreq->lock);
365 return;
366 }
367
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530368 devfreq_update_status(devfreq, devfreq->previous_freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200369 devfreq->stop_polling = true;
370 mutex_unlock(&devfreq->lock);
371 cancel_delayed_work_sync(&devfreq->work);
372}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100373EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200374
375/**
376 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
377 * @devfreq: the devfreq instance.
378 *
379 * Helper function to resume devfreq device load monitoing. Function
380 * to be called from governor in response to DEVFREQ_GOV_RESUME
381 * event or when polling interval is set to non-zero.
382 */
383void devfreq_monitor_resume(struct devfreq *devfreq)
384{
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530385 unsigned long freq;
386
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200387 mutex_lock(&devfreq->lock);
388 if (!devfreq->stop_polling)
389 goto out;
390
391 if (!delayed_work_pending(&devfreq->work) &&
392 devfreq->profile->polling_ms)
393 queue_delayed_work(devfreq_wq, &devfreq->work,
394 msecs_to_jiffies(devfreq->profile->polling_ms));
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530395
396 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200397 devfreq->stop_polling = false;
398
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530399 if (devfreq->profile->get_cur_freq &&
400 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
401 devfreq->previous_freq = freq;
402
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200403out:
404 mutex_unlock(&devfreq->lock);
405}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100406EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200407
408/**
409 * devfreq_interval_update() - Update device devfreq monitoring interval
410 * @devfreq: the devfreq instance.
411 * @delay: new polling interval to be set.
412 *
413 * Helper function to set new load monitoring polling interval. Function
414 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
415 */
416void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
417{
418 unsigned int cur_delay = devfreq->profile->polling_ms;
419 unsigned int new_delay = *delay;
420
421 mutex_lock(&devfreq->lock);
422 devfreq->profile->polling_ms = new_delay;
423
424 if (devfreq->stop_polling)
425 goto out;
426
427 /* if new delay is zero, stop polling */
428 if (!new_delay) {
429 mutex_unlock(&devfreq->lock);
430 cancel_delayed_work_sync(&devfreq->work);
431 return;
432 }
433
434 /* if current delay is zero, start polling with new delay */
435 if (!cur_delay) {
436 queue_delayed_work(devfreq_wq, &devfreq->work,
437 msecs_to_jiffies(devfreq->profile->polling_ms));
438 goto out;
439 }
440
441 /* if current delay is greater than new delay, restart polling */
442 if (cur_delay > new_delay) {
443 mutex_unlock(&devfreq->lock);
444 cancel_delayed_work_sync(&devfreq->work);
445 mutex_lock(&devfreq->lock);
446 if (!devfreq->stop_polling)
447 queue_delayed_work(devfreq_wq, &devfreq->work,
448 msecs_to_jiffies(devfreq->profile->polling_ms));
449 }
450out:
451 mutex_unlock(&devfreq->lock);
452}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100453EXPORT_SYMBOL(devfreq_interval_update);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200454
455/**
456 * devfreq_notifier_call() - Notify that the device frequency requirements
457 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200458 * @nb: the notifier_block (supposed to be devfreq->nb)
459 * @type: not used
460 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200461 *
462 * Called by a notifier that uses devfreq->nb.
463 */
464static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
465 void *devp)
466{
467 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
468 int ret;
469
470 mutex_lock(&devfreq->lock);
471 ret = update_devfreq(devfreq);
472 mutex_unlock(&devfreq->lock);
473
474 return ret;
475}
476
477/**
Chanwoo Choi29b69682017-01-31 15:38:18 +0900478 * devfreq_dev_release() - Callback for struct device to release the device.
479 * @dev: the devfreq device
480 *
481 * Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200482 */
Chanwoo Choi29b69682017-01-31 15:38:18 +0900483static void devfreq_dev_release(struct device *dev)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200484{
Chanwoo Choi29b69682017-01-31 15:38:18 +0900485 struct devfreq *devfreq = to_devfreq(dev);
486
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200487 mutex_lock(&devfreq_list_lock);
488 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
489 mutex_unlock(&devfreq_list_lock);
490 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200491 return;
492 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200493 list_del(&devfreq->node);
494 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200495
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500496 if (devfreq->governor)
497 devfreq->governor->event_handler(devfreq,
498 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200499
500 if (devfreq->profile->exit)
501 devfreq->profile->exit(devfreq->dev.parent);
502
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200503 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200504 kfree(devfreq);
505}
506
507/**
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200508 * devfreq_add_device() - Add devfreq feature to the device
509 * @dev: the device to add devfreq feature.
510 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500511 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200512 * @data: private data for the governor. The devfreq framework does not
513 * touch this value.
514 */
515struct devfreq *devfreq_add_device(struct device *dev,
516 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500517 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200518 void *data)
519{
520 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500521 struct devfreq_governor *governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200522 int err = 0;
523
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500524 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200525 dev_err(dev, "%s: Invalid parameters.\n", __func__);
526 return ERR_PTR(-EINVAL);
527 }
528
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200529 mutex_lock(&devfreq_list_lock);
530 devfreq = find_device_devfreq(dev);
531 mutex_unlock(&devfreq_list_lock);
532 if (!IS_ERR(devfreq)) {
Chanwoo Choi9d0109b2016-11-19 22:47:36 +0900533 dev_err(dev, "%s: Unable to create devfreq for the device.\n",
534 __func__);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200535 err = -EINVAL;
536 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200537 }
538
539 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
540 if (!devfreq) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200541 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100542 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200543 }
544
545 mutex_init(&devfreq->lock);
546 mutex_lock(&devfreq->lock);
547 devfreq->dev.parent = dev;
548 devfreq->dev.class = devfreq_class;
549 devfreq->dev.release = devfreq_dev_release;
550 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500551 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200552 devfreq->previous_freq = profile->initial_freq;
Lukasz Luba8d39fc082016-05-31 11:25:09 +0100553 devfreq->last_status.current_frequency = profile->initial_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200554 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200555 devfreq->nb.notifier_call = devfreq_notifier_call;
556
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900557 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
558 mutex_unlock(&devfreq->lock);
559 devfreq_set_freq_table(devfreq);
560 mutex_lock(&devfreq->lock);
561 }
562
Kees Cook02aa2a32013-07-03 15:04:56 -0700563 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200564 err = device_register(&devfreq->dev);
565 if (err) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200566 mutex_unlock(&devfreq->lock);
Geliang Tang6d3cbfa2015-10-01 22:18:19 +0800567 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200568 }
569
Chanwoo Choi9d0109b2016-11-19 22:47:36 +0900570 devfreq->trans_table = devm_kzalloc(&devfreq->dev,
571 sizeof(unsigned int) *
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900572 devfreq->profile->max_state *
573 devfreq->profile->max_state,
574 GFP_KERNEL);
Chanwoo Choi9d0109b2016-11-19 22:47:36 +0900575 devfreq->time_in_state = devm_kzalloc(&devfreq->dev,
576 sizeof(unsigned long) *
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900577 devfreq->profile->max_state,
578 GFP_KERNEL);
579 devfreq->last_stat_updated = jiffies;
580
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900581 srcu_init_notifier_head(&devfreq->transition_notifier_list);
582
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200583 mutex_unlock(&devfreq->lock);
584
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200585 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200586 list_add(&devfreq->node, &devfreq_list);
587
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500588 governor = find_devfreq_governor(devfreq->governor_name);
Chanwoo Choi73613b12016-12-28 20:52:35 +0900589 if (IS_ERR(governor)) {
590 dev_err(dev, "%s: Unable to find governor for the device\n",
591 __func__);
592 err = PTR_ERR(governor);
593 goto err_init;
594 }
595
596 devfreq->governor = governor;
597 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
598 NULL);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200599 if (err) {
600 dev_err(dev, "%s: Unable to start governor for the device\n",
601 __func__);
602 goto err_init;
603 }
Axel Lin0f376c92016-09-29 10:13:28 +0800604 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200605
Axel Lin3f19f082011-11-15 21:59:09 +0100606 return devfreq;
607
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200608err_init:
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200609 list_del(&devfreq->node);
Axel Lin0f376c92016-09-29 10:13:28 +0800610 mutex_unlock(&devfreq_list_lock);
611
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200612 device_unregister(&devfreq->dev);
Axel Lin3f19f082011-11-15 21:59:09 +0100613err_out:
614 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200615}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200616EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200617
618/**
619 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200620 * @devfreq: the devfreq instance to be removed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900621 *
622 * The opposite of devfreq_add_device().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200623 */
624int devfreq_remove_device(struct devfreq *devfreq)
625{
626 if (!devfreq)
627 return -EINVAL;
628
Chanwoo Choi585fc832014-05-09 16:43:07 +0900629 device_unregister(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200630
631 return 0;
632}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200633EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200634
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900635static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
636{
637 struct devfreq **r = res;
638
639 if (WARN_ON(!r || !*r))
640 return 0;
641
642 return *r == data;
643}
644
645static void devm_devfreq_dev_release(struct device *dev, void *res)
646{
647 devfreq_remove_device(*(struct devfreq **)res);
648}
649
650/**
651 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
652 * @dev: the device to add devfreq feature.
653 * @profile: device-specific profile to run devfreq.
654 * @governor_name: name of the policy to choose frequency.
655 * @data: private data for the governor. The devfreq framework does not
656 * touch this value.
657 *
658 * This function manages automatically the memory of devfreq device using device
659 * resource management and simplify the free operation for memory of devfreq
660 * device.
661 */
662struct devfreq *devm_devfreq_add_device(struct device *dev,
663 struct devfreq_dev_profile *profile,
664 const char *governor_name,
665 void *data)
666{
667 struct devfreq **ptr, *devfreq;
668
669 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
670 if (!ptr)
671 return ERR_PTR(-ENOMEM);
672
673 devfreq = devfreq_add_device(dev, profile, governor_name, data);
674 if (IS_ERR(devfreq)) {
675 devres_free(ptr);
676 return ERR_PTR(-ENOMEM);
677 }
678
679 *ptr = devfreq;
680 devres_add(dev, ptr);
681
682 return devfreq;
683}
684EXPORT_SYMBOL(devm_devfreq_add_device);
685
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900686#ifdef CONFIG_OF
687/*
688 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
689 * @dev - instance to the given device
690 * @index - index into list of devfreq
691 *
692 * return the instance of devfreq device
693 */
694struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
695{
696 struct device_node *node;
697 struct devfreq *devfreq;
698
699 if (!dev)
700 return ERR_PTR(-EINVAL);
701
702 if (!dev->of_node)
703 return ERR_PTR(-EINVAL);
704
705 node = of_parse_phandle(dev->of_node, "devfreq", index);
706 if (!node)
707 return ERR_PTR(-ENODEV);
708
709 mutex_lock(&devfreq_list_lock);
710 list_for_each_entry(devfreq, &devfreq_list, node) {
711 if (devfreq->dev.parent
712 && devfreq->dev.parent->of_node == node) {
713 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800714 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900715 return devfreq;
716 }
717 }
718 mutex_unlock(&devfreq_list_lock);
Peter Chen3427c6f2016-07-01 17:42:00 +0800719 of_node_put(node);
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900720
721 return ERR_PTR(-EPROBE_DEFER);
722}
723#else
724struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
725{
726 return ERR_PTR(-ENODEV);
727}
728#endif /* CONFIG_OF */
729EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
730
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900731/**
732 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
733 * @dev: the device to add devfreq feature.
734 * @devfreq: the devfreq instance to be removed
735 */
736void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
737{
738 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
739 devm_devfreq_dev_match, devfreq));
740}
741EXPORT_SYMBOL(devm_devfreq_remove_device);
742
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200743/**
744 * devfreq_suspend_device() - Suspend devfreq of a device.
745 * @devfreq: the devfreq instance to be suspended
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900746 *
747 * This function is intended to be called by the pm callbacks
748 * (e.g., runtime_suspend, suspend) of the device driver that
749 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200750 */
751int devfreq_suspend_device(struct devfreq *devfreq)
752{
753 if (!devfreq)
754 return -EINVAL;
755
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500756 if (!devfreq->governor)
757 return 0;
758
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200759 return devfreq->governor->event_handler(devfreq,
760 DEVFREQ_GOV_SUSPEND, NULL);
761}
762EXPORT_SYMBOL(devfreq_suspend_device);
763
764/**
765 * devfreq_resume_device() - Resume devfreq of a device.
766 * @devfreq: the devfreq instance to be resumed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900767 *
768 * This function is intended to be called by the pm callbacks
769 * (e.g., runtime_resume, resume) of the device driver that
770 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200771 */
772int devfreq_resume_device(struct devfreq *devfreq)
773{
774 if (!devfreq)
775 return -EINVAL;
776
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500777 if (!devfreq->governor)
778 return 0;
779
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200780 return devfreq->governor->event_handler(devfreq,
781 DEVFREQ_GOV_RESUME, NULL);
782}
783EXPORT_SYMBOL(devfreq_resume_device);
784
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500785/**
786 * devfreq_add_governor() - Add devfreq governor
787 * @governor: the devfreq governor to be added
788 */
789int devfreq_add_governor(struct devfreq_governor *governor)
790{
791 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500792 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500793 int err = 0;
794
795 if (!governor) {
796 pr_err("%s: Invalid parameters.\n", __func__);
797 return -EINVAL;
798 }
799
800 mutex_lock(&devfreq_list_lock);
801 g = find_devfreq_governor(governor->name);
802 if (!IS_ERR(g)) {
803 pr_err("%s: governor %s already registered\n", __func__,
804 g->name);
805 err = -EINVAL;
806 goto err_out;
807 }
808
809 list_add(&governor->node, &devfreq_governor_list);
810
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500811 list_for_each_entry(devfreq, &devfreq_list, node) {
812 int ret = 0;
813 struct device *dev = devfreq->dev.parent;
814
815 if (!strncmp(devfreq->governor_name, governor->name,
816 DEVFREQ_NAME_LEN)) {
817 /* The following should never occur */
818 if (devfreq->governor) {
819 dev_warn(dev,
820 "%s: Governor %s already present\n",
821 __func__, devfreq->governor->name);
822 ret = devfreq->governor->event_handler(devfreq,
823 DEVFREQ_GOV_STOP, NULL);
824 if (ret) {
825 dev_warn(dev,
826 "%s: Governor %s stop = %d\n",
827 __func__,
828 devfreq->governor->name, ret);
829 }
830 /* Fall through */
831 }
832 devfreq->governor = governor;
833 ret = devfreq->governor->event_handler(devfreq,
834 DEVFREQ_GOV_START, NULL);
835 if (ret) {
836 dev_warn(dev, "%s: Governor %s start=%d\n",
837 __func__, devfreq->governor->name,
838 ret);
839 }
840 }
841 }
842
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500843err_out:
844 mutex_unlock(&devfreq_list_lock);
845
846 return err;
847}
848EXPORT_SYMBOL(devfreq_add_governor);
849
850/**
MyungJoo Hambafeb422016-11-09 10:29:14 +0900851 * devfreq_remove_governor() - Remove devfreq feature from a device.
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500852 * @governor: the devfreq governor to be removed
853 */
854int devfreq_remove_governor(struct devfreq_governor *governor)
855{
856 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500857 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500858 int err = 0;
859
860 if (!governor) {
861 pr_err("%s: Invalid parameters.\n", __func__);
862 return -EINVAL;
863 }
864
865 mutex_lock(&devfreq_list_lock);
866 g = find_devfreq_governor(governor->name);
867 if (IS_ERR(g)) {
868 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamatb9e1c8e2012-11-21 10:36:13 +0530869 governor->name);
Sachin Kamatf9c08e22012-11-21 10:36:14 +0530870 err = PTR_ERR(g);
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500871 goto err_out;
872 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500873 list_for_each_entry(devfreq, &devfreq_list, node) {
874 int ret;
875 struct device *dev = devfreq->dev.parent;
876
877 if (!strncmp(devfreq->governor_name, governor->name,
878 DEVFREQ_NAME_LEN)) {
879 /* we should have a devfreq governor! */
880 if (!devfreq->governor) {
881 dev_warn(dev, "%s: Governor %s NOT present\n",
882 __func__, governor->name);
883 continue;
884 /* Fall through */
885 }
886 ret = devfreq->governor->event_handler(devfreq,
887 DEVFREQ_GOV_STOP, NULL);
888 if (ret) {
889 dev_warn(dev, "%s: Governor %s stop=%d\n",
890 __func__, devfreq->governor->name,
891 ret);
892 }
893 devfreq->governor = NULL;
894 }
895 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500896
897 list_del(&governor->node);
898err_out:
899 mutex_unlock(&devfreq_list_lock);
900
901 return err;
902}
903EXPORT_SYMBOL(devfreq_remove_governor);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200904
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700905static ssize_t governor_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200906 struct device_attribute *attr, char *buf)
907{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500908 if (!to_devfreq(dev)->governor)
909 return -EINVAL;
910
MyungJoo Ham9005b652011-10-02 00:19:28 +0200911 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
912}
913
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700914static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500915 const char *buf, size_t count)
916{
917 struct devfreq *df = to_devfreq(dev);
918 int ret;
919 char str_governor[DEVFREQ_NAME_LEN + 1];
920 struct devfreq_governor *governor;
921
922 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
923 if (ret != 1)
924 return -EINVAL;
925
926 mutex_lock(&devfreq_list_lock);
927 governor = find_devfreq_governor(str_governor);
928 if (IS_ERR(governor)) {
929 ret = PTR_ERR(governor);
930 goto out;
931 }
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200932 if (df->governor == governor) {
933 ret = 0;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500934 goto out;
Chanwoo Choibcf23c72017-01-31 15:38:16 +0900935 } else if (df->governor->immutable || governor->immutable) {
936 ret = -EINVAL;
937 goto out;
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200938 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500939
940 if (df->governor) {
941 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
942 if (ret) {
943 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
944 __func__, df->governor->name, ret);
945 goto out;
946 }
947 }
948 df->governor = governor;
949 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
950 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
951 if (ret)
952 dev_warn(dev, "%s: Governor %s not started(%d)\n",
953 __func__, df->governor->name, ret);
954out:
955 mutex_unlock(&devfreq_list_lock);
956
957 if (!ret)
958 ret = count;
959 return ret;
960}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700961static DEVICE_ATTR_RW(governor);
962
963static ssize_t available_governors_show(struct device *d,
964 struct device_attribute *attr,
965 char *buf)
Nishanth Menon50a5b332012-10-29 15:01:48 -0500966{
Chanwoo Choibcf23c72017-01-31 15:38:16 +0900967 struct devfreq *df = to_devfreq(d);
Nishanth Menon50a5b332012-10-29 15:01:48 -0500968 ssize_t count = 0;
969
970 mutex_lock(&devfreq_list_lock);
Chanwoo Choibcf23c72017-01-31 15:38:16 +0900971
972 /*
973 * The devfreq with immutable governor (e.g., passive) shows
974 * only own governor.
975 */
976 if (df->governor->immutable) {
977 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
978 "%s ", df->governor_name);
979 /*
980 * The devfreq device shows the registered governor except for
981 * immutable governors such as passive governor .
982 */
983 } else {
984 struct devfreq_governor *governor;
985
986 list_for_each_entry(governor, &devfreq_governor_list, node) {
987 if (governor->immutable)
988 continue;
989 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
990 "%s ", governor->name);
991 }
992 }
993
Nishanth Menon50a5b332012-10-29 15:01:48 -0500994 mutex_unlock(&devfreq_list_lock);
995
996 /* Truncate the trailing space */
997 if (count)
998 count--;
999
1000 count += sprintf(&buf[count], "\n");
1001
1002 return count;
1003}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001004static DEVICE_ATTR_RO(available_governors);
Nishanth Menon0359d1a2012-10-29 15:01:47 -05001005
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001006static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1007 char *buf)
MyungJoo Ham9005b652011-10-02 00:19:28 +02001008{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001009 unsigned long freq;
1010 struct devfreq *devfreq = to_devfreq(dev);
1011
1012 if (devfreq->profile->get_cur_freq &&
1013 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
Chanwoo Choi9d0109b2016-11-19 22:47:36 +09001014 return sprintf(buf, "%lu\n", freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001015
1016 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1017}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001018static DEVICE_ATTR_RO(cur_freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001019
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001020static ssize_t target_freq_show(struct device *dev,
1021 struct device_attribute *attr, char *buf)
Rajagopal Venkat7f98a902012-10-26 01:50:26 +02001022{
MyungJoo Ham9005b652011-10-02 00:19:28 +02001023 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1024}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001025static DEVICE_ATTR_RO(target_freq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001026
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001027static ssize_t polling_interval_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001028 struct device_attribute *attr, char *buf)
1029{
1030 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1031}
1032
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001033static ssize_t polling_interval_store(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001034 struct device_attribute *attr,
1035 const char *buf, size_t count)
1036{
1037 struct devfreq *df = to_devfreq(dev);
1038 unsigned int value;
1039 int ret;
1040
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001041 if (!df->governor)
1042 return -EINVAL;
1043
MyungJoo Ham9005b652011-10-02 00:19:28 +02001044 ret = sscanf(buf, "%u", &value);
1045 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001046 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +02001047
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001048 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001049 ret = count;
1050
MyungJoo Ham9005b652011-10-02 00:19:28 +02001051 return ret;
1052}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001053static DEVICE_ATTR_RW(polling_interval);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001054
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001055static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001056 const char *buf, size_t count)
1057{
1058 struct devfreq *df = to_devfreq(dev);
1059 unsigned long value;
1060 int ret;
1061 unsigned long max;
1062
1063 ret = sscanf(buf, "%lu", &value);
1064 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001065 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001066
1067 mutex_lock(&df->lock);
1068 max = df->max_freq;
1069 if (value && max && value > max) {
1070 ret = -EINVAL;
1071 goto unlock;
1072 }
1073
1074 df->min_freq = value;
1075 update_devfreq(df);
1076 ret = count;
1077unlock:
1078 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001079 return ret;
1080}
1081
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001082static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001083 const char *buf, size_t count)
1084{
1085 struct devfreq *df = to_devfreq(dev);
1086 unsigned long value;
1087 int ret;
1088 unsigned long min;
1089
1090 ret = sscanf(buf, "%lu", &value);
1091 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001092 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001093
1094 mutex_lock(&df->lock);
1095 min = df->min_freq;
1096 if (value && min && value < min) {
1097 ret = -EINVAL;
1098 goto unlock;
1099 }
1100
1101 df->max_freq = value;
1102 update_devfreq(df);
1103 ret = count;
1104unlock:
1105 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001106 return ret;
1107}
1108
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001109#define show_one(name) \
1110static ssize_t name##_show \
1111(struct device *dev, struct device_attribute *attr, char *buf) \
1112{ \
1113 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001114}
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001115show_one(min_freq);
1116show_one(max_freq);
1117
1118static DEVICE_ATTR_RW(min_freq);
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001119static DEVICE_ATTR_RW(max_freq);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001120
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001121static ssize_t available_frequencies_show(struct device *d,
1122 struct device_attribute *attr,
1123 char *buf)
Nishanth Menond287de82012-10-25 19:48:59 -05001124{
1125 struct devfreq *df = to_devfreq(d);
1126 struct device *dev = df->dev.parent;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001127 struct dev_pm_opp *opp;
Nishanth Menond287de82012-10-25 19:48:59 -05001128 ssize_t count = 0;
1129 unsigned long freq = 0;
1130
1131 rcu_read_lock();
1132 do {
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001133 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
Nishanth Menond287de82012-10-25 19:48:59 -05001134 if (IS_ERR(opp))
1135 break;
1136
1137 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1138 "%lu ", freq);
1139 freq++;
1140 } while (1);
1141 rcu_read_unlock();
1142
1143 /* Truncate the trailing space */
1144 if (count)
1145 count--;
1146
1147 count += sprintf(&buf[count], "\n");
1148
1149 return count;
1150}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001151static DEVICE_ATTR_RO(available_frequencies);
Nishanth Menond287de82012-10-25 19:48:59 -05001152
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001153static ssize_t trans_stat_show(struct device *dev,
1154 struct device_attribute *attr, char *buf)
Jonghwa Leee552bba2012-08-23 20:00:46 +09001155{
1156 struct devfreq *devfreq = to_devfreq(dev);
1157 ssize_t len;
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301158 int i, j;
Jonghwa Leee552bba2012-08-23 20:00:46 +09001159 unsigned int max_state = devfreq->profile->max_state;
1160
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301161 if (!devfreq->stop_polling &&
1162 devfreq_update_status(devfreq, devfreq->previous_freq))
Jonghwa Leee552bba2012-08-23 20:00:46 +09001163 return 0;
MyungJoo Ham34bd3222015-11-23 15:45:36 +09001164 if (max_state == 0)
1165 return sprintf(buf, "Not Supported.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001166
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001167 len = sprintf(buf, " From : To\n");
1168 len += sprintf(buf + len, " :");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001169 for (i = 0; i < max_state; i++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001170 len += sprintf(buf + len, "%10lu",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001171 devfreq->profile->freq_table[i]);
1172
1173 len += sprintf(buf + len, " time(ms)\n");
1174
1175 for (i = 0; i < max_state; i++) {
1176 if (devfreq->profile->freq_table[i]
1177 == devfreq->previous_freq) {
1178 len += sprintf(buf + len, "*");
1179 } else {
1180 len += sprintf(buf + len, " ");
1181 }
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001182 len += sprintf(buf + len, "%10lu:",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001183 devfreq->profile->freq_table[i]);
1184 for (j = 0; j < max_state; j++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001185 len += sprintf(buf + len, "%10u",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001186 devfreq->trans_table[(i * max_state) + j]);
1187 len += sprintf(buf + len, "%10u\n",
1188 jiffies_to_msecs(devfreq->time_in_state[i]));
1189 }
1190
1191 len += sprintf(buf + len, "Total transition : %u\n",
1192 devfreq->total_trans);
1193 return len;
1194}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001195static DEVICE_ATTR_RO(trans_stat);
Jonghwa Leee552bba2012-08-23 20:00:46 +09001196
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001197static struct attribute *devfreq_attrs[] = {
1198 &dev_attr_governor.attr,
1199 &dev_attr_available_governors.attr,
1200 &dev_attr_cur_freq.attr,
1201 &dev_attr_available_frequencies.attr,
1202 &dev_attr_target_freq.attr,
1203 &dev_attr_polling_interval.attr,
1204 &dev_attr_min_freq.attr,
1205 &dev_attr_max_freq.attr,
1206 &dev_attr_trans_stat.attr,
1207 NULL,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001208};
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001209ATTRIBUTE_GROUPS(devfreq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001210
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001211static int __init devfreq_init(void)
1212{
1213 devfreq_class = class_create(THIS_MODULE, "devfreq");
1214 if (IS_ERR(devfreq_class)) {
1215 pr_err("%s: couldn't create class\n", __FILE__);
1216 return PTR_ERR(devfreq_class);
1217 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001218
1219 devfreq_wq = create_freezable_workqueue("devfreq_wq");
Dan Carpenterea7f4542013-08-15 10:55:10 +03001220 if (!devfreq_wq) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001221 class_destroy(devfreq_class);
1222 pr_err("%s: couldn't create workqueue\n", __FILE__);
Dan Carpenterea7f4542013-08-15 10:55:10 +03001223 return -ENOMEM;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001224 }
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001225 devfreq_class->dev_groups = devfreq_groups;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001226
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001227 return 0;
1228}
1229subsys_initcall(devfreq_init);
1230
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001231/*
1232 * The followings are helper functions for devfreq user device drivers with
1233 * OPP framework.
1234 */
1235
1236/**
1237 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1238 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001239 * @dev: The devfreq user device. (parent of devfreq)
1240 * @freq: The frequency given to target function
1241 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001242 *
Nishanth Menonbcb27542013-01-18 19:52:34 +00001243 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1244 * protected pointer. The reason for the same is that the opp pointer which is
1245 * returned will remain valid for use with opp_get_{voltage, freq} only while
1246 * under the locked area. The pointer returned must be used prior to unlocking
1247 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001248 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001249struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1250 unsigned long *freq,
1251 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001252{
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001253 struct dev_pm_opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001254
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001255 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1256 /* The freq is an upper bound. opp should be lower */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001257 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001258
1259 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001260 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001261 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001262 } else {
1263 /* The freq is an lower bound. opp should be higher */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001264 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001265
1266 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001267 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001268 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001269 }
1270
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001271 return opp;
1272}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001273EXPORT_SYMBOL(devfreq_recommended_opp);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001274
1275/**
1276 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1277 * for any changes in the OPP availability
1278 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001279 * @dev: The devfreq user device. (parent of devfreq)
1280 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001281 */
1282int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1283{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001284 struct srcu_notifier_head *nh;
1285 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001286
MyungJoo Ham389baed2012-11-21 19:04:51 +09001287 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001288 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001289 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001290 ret = PTR_ERR(nh);
1291 rcu_read_unlock();
1292 if (!ret)
1293 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1294
1295 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001296}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001297EXPORT_SYMBOL(devfreq_register_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001298
1299/**
1300 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1301 * notified for any changes in the OPP
1302 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001303 * @dev: The devfreq user device. (parent of devfreq)
1304 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001305 *
1306 * At exit() callback of devfreq_dev_profile, this must be included if
1307 * devfreq_recommended_opp is used.
1308 */
1309int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1310{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001311 struct srcu_notifier_head *nh;
1312 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001313
MyungJoo Ham389baed2012-11-21 19:04:51 +09001314 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001315 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001316 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001317 ret = PTR_ERR(nh);
1318 rcu_read_unlock();
1319 if (!ret)
1320 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1321
1322 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001323}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001324EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001325
Chanwoo Choid5b040d2014-05-09 16:43:09 +09001326static void devm_devfreq_opp_release(struct device *dev, void *res)
1327{
1328 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1329}
1330
1331/**
1332 * devm_ devfreq_register_opp_notifier()
1333 * - Resource-managed devfreq_register_opp_notifier()
1334 * @dev: The devfreq user device. (parent of devfreq)
1335 * @devfreq: The devfreq object.
1336 */
1337int devm_devfreq_register_opp_notifier(struct device *dev,
1338 struct devfreq *devfreq)
1339{
1340 struct devfreq **ptr;
1341 int ret;
1342
1343 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1344 if (!ptr)
1345 return -ENOMEM;
1346
1347 ret = devfreq_register_opp_notifier(dev, devfreq);
1348 if (ret) {
1349 devres_free(ptr);
1350 return ret;
1351 }
1352
1353 *ptr = devfreq;
1354 devres_add(dev, ptr);
1355
1356 return 0;
1357}
1358EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1359
1360/**
1361 * devm_devfreq_unregister_opp_notifier()
1362 * - Resource-managed devfreq_unregister_opp_notifier()
1363 * @dev: The devfreq user device. (parent of devfreq)
1364 * @devfreq: The devfreq object.
1365 */
1366void devm_devfreq_unregister_opp_notifier(struct device *dev,
1367 struct devfreq *devfreq)
1368{
1369 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1370 devm_devfreq_dev_match, devfreq));
1371}
1372EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1373
Chanwoo Choi0fe3a662016-01-26 13:21:26 +09001374/**
1375 * devfreq_register_notifier() - Register a driver with devfreq
1376 * @devfreq: The devfreq object.
1377 * @nb: The notifier block to register.
1378 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1379 */
1380int devfreq_register_notifier(struct devfreq *devfreq,
1381 struct notifier_block *nb,
1382 unsigned int list)
1383{
1384 int ret = 0;
1385
1386 if (!devfreq)
1387 return -EINVAL;
1388
1389 switch (list) {
1390 case DEVFREQ_TRANSITION_NOTIFIER:
1391 ret = srcu_notifier_chain_register(
1392 &devfreq->transition_notifier_list, nb);
1393 break;
1394 default:
1395 ret = -EINVAL;
1396 }
1397
1398 return ret;
1399}
1400EXPORT_SYMBOL(devfreq_register_notifier);
1401
1402/*
1403 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1404 * @devfreq: The devfreq object.
1405 * @nb: The notifier block to be unregistered.
1406 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1407 */
1408int devfreq_unregister_notifier(struct devfreq *devfreq,
1409 struct notifier_block *nb,
1410 unsigned int list)
1411{
1412 int ret = 0;
1413
1414 if (!devfreq)
1415 return -EINVAL;
1416
1417 switch (list) {
1418 case DEVFREQ_TRANSITION_NOTIFIER:
1419 ret = srcu_notifier_chain_unregister(
1420 &devfreq->transition_notifier_list, nb);
1421 break;
1422 default:
1423 ret = -EINVAL;
1424 }
1425
1426 return ret;
1427}
1428EXPORT_SYMBOL(devfreq_unregister_notifier);
1429
1430struct devfreq_notifier_devres {
1431 struct devfreq *devfreq;
1432 struct notifier_block *nb;
1433 unsigned int list;
1434};
1435
1436static void devm_devfreq_notifier_release(struct device *dev, void *res)
1437{
1438 struct devfreq_notifier_devres *this = res;
1439
1440 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1441}
1442
1443/**
1444 * devm_devfreq_register_notifier()
1445 - Resource-managed devfreq_register_notifier()
1446 * @dev: The devfreq user device. (parent of devfreq)
1447 * @devfreq: The devfreq object.
1448 * @nb: The notifier block to be unregistered.
1449 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1450 */
1451int devm_devfreq_register_notifier(struct device *dev,
1452 struct devfreq *devfreq,
1453 struct notifier_block *nb,
1454 unsigned int list)
1455{
1456 struct devfreq_notifier_devres *ptr;
1457 int ret;
1458
1459 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1460 GFP_KERNEL);
1461 if (!ptr)
1462 return -ENOMEM;
1463
1464 ret = devfreq_register_notifier(devfreq, nb, list);
1465 if (ret) {
1466 devres_free(ptr);
1467 return ret;
1468 }
1469
1470 ptr->devfreq = devfreq;
1471 ptr->nb = nb;
1472 ptr->list = list;
1473 devres_add(dev, ptr);
1474
1475 return 0;
1476}
1477EXPORT_SYMBOL(devm_devfreq_register_notifier);
1478
1479/**
1480 * devm_devfreq_unregister_notifier()
1481 - Resource-managed devfreq_unregister_notifier()
1482 * @dev: The devfreq user device. (parent of devfreq)
1483 * @devfreq: The devfreq object.
1484 * @nb: The notifier block to be unregistered.
1485 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1486 */
1487void devm_devfreq_unregister_notifier(struct device *dev,
1488 struct devfreq *devfreq,
1489 struct notifier_block *nb,
1490 unsigned int list)
1491{
1492 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1493 devm_devfreq_dev_match, devfreq));
1494}
1495EXPORT_SYMBOL(devm_devfreq_unregister_notifier);