blob: 0ebd64dfc0a98bedfd7eaf45107430ef505a8253 [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>
MyungJoo Ham952f6d12011-11-10 10:16:23 +010018#include <linux/module.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 */
133static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
134{
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
140 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
141 if (prev_lev < 0) {
142 ret = prev_lev;
143 goto out;
144 }
145
146 devfreq->time_in_state[prev_lev] +=
Jonghwa Leee552bba2012-08-23 20:00:46 +0900147 cur_time - devfreq->last_stat_updated;
Saravana Kannane35d35a2014-02-27 19:38:57 -0800148
149 lev = devfreq_get_freq_level(devfreq, freq);
150 if (lev < 0) {
151 ret = lev;
152 goto out;
153 }
154
155 if (lev != prev_lev) {
Jonghwa Leee552bba2012-08-23 20:00:46 +0900156 devfreq->trans_table[(prev_lev *
157 devfreq->profile->max_state) + lev]++;
158 devfreq->total_trans++;
159 }
Jonghwa Leee552bba2012-08-23 20:00:46 +0900160
Saravana Kannane35d35a2014-02-27 19:38:57 -0800161out:
162 devfreq->last_stat_updated = cur_time;
163 return ret;
Jonghwa Leee552bba2012-08-23 20:00:46 +0900164}
165
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500166/**
167 * find_devfreq_governor() - find devfreq governor from name
168 * @name: name of the governor
169 *
170 * Search the list of devfreq governors and return the matched
171 * governor's pointer. devfreq_list_lock should be held by the caller.
172 */
173static struct devfreq_governor *find_devfreq_governor(const char *name)
174{
175 struct devfreq_governor *tmp_governor;
176
Viresh Kumar9348da22015-08-10 11:42:25 +0530177 if (IS_ERR_OR_NULL(name)) {
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500178 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
179 return ERR_PTR(-EINVAL);
180 }
181 WARN(!mutex_is_locked(&devfreq_list_lock),
182 "devfreq_list_lock must be locked.");
183
184 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
185 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
186 return tmp_governor;
187 }
188
189 return ERR_PTR(-ENODEV);
190}
191
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900192static int devfreq_notify_transition(struct devfreq *devfreq,
193 struct devfreq_freqs *freqs, unsigned int state)
194{
195 if (!devfreq)
196 return -EINVAL;
197
198 switch (state) {
199 case DEVFREQ_PRECHANGE:
200 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
201 DEVFREQ_PRECHANGE, freqs);
202 break;
203
204 case DEVFREQ_POSTCHANGE:
205 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
206 DEVFREQ_POSTCHANGE, freqs);
207 break;
208 default:
209 return -EINVAL;
210 }
211
212 return 0;
213}
214
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200215/* Load monitoring helper functions for governors use */
216
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200217/**
218 * update_devfreq() - Reevaluate the device and configure frequency.
219 * @devfreq: the devfreq instance.
220 *
221 * Note: Lock devfreq->lock before calling update_devfreq
222 * This function is exported for governors.
223 */
224int update_devfreq(struct devfreq *devfreq)
225{
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900226 struct devfreq_freqs freqs;
227 unsigned long freq, cur_freq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200228 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100229 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200230
231 if (!mutex_is_locked(&devfreq->lock)) {
232 WARN(true, "devfreq->lock must be locked by the caller.\n");
233 return -EINVAL;
234 }
235
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500236 if (!devfreq->governor)
237 return -EINVAL;
238
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200239 /* Reevaluate the proper frequency */
240 err = devfreq->governor->get_target_freq(devfreq, &freq);
241 if (err)
242 return err;
243
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100244 /*
Javi Merinod3b7e172015-08-14 18:57:00 +0100245 * Adjust the frequency with user freq and QoS.
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100246 *
Javi Merinod3b7e172015-08-14 18:57:00 +0100247 * List from the highest priority
248 * max_freq
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100249 * min_freq
250 */
251
252 if (devfreq->min_freq && freq < devfreq->min_freq) {
253 freq = devfreq->min_freq;
254 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
255 }
256 if (devfreq->max_freq && freq > devfreq->max_freq) {
257 freq = devfreq->max_freq;
258 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
259 }
260
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900261 if (devfreq->profile->get_cur_freq)
262 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
263 else
264 cur_freq = devfreq->previous_freq;
265
266 freqs.old = cur_freq;
267 freqs.new = freq;
268 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
269
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100270 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200271 if (err)
272 return err;
273
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900274 freqs.new = freq;
275 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
276
Jonghwa Leee552bba2012-08-23 20:00:46 +0900277 if (devfreq->profile->freq_table)
278 if (devfreq_update_status(devfreq, freq))
279 dev_err(&devfreq->dev,
280 "Couldn't update frequency transition information.\n");
281
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200282 devfreq->previous_freq = freq;
283 return err;
284}
Nishanth Menon2df50212012-10-29 15:01:42 -0500285EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200286
287/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200288 * devfreq_monitor() - Periodically poll devfreq objects.
289 * @work: the work struct used to run devfreq_monitor periodically.
290 *
291 */
292static void devfreq_monitor(struct work_struct *work)
293{
294 int err;
295 struct devfreq *devfreq = container_of(work,
296 struct devfreq, work.work);
297
298 mutex_lock(&devfreq->lock);
299 err = update_devfreq(devfreq);
300 if (err)
301 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
302
303 queue_delayed_work(devfreq_wq, &devfreq->work,
304 msecs_to_jiffies(devfreq->profile->polling_ms));
305 mutex_unlock(&devfreq->lock);
306}
307
308/**
309 * devfreq_monitor_start() - Start load monitoring of devfreq instance
310 * @devfreq: the devfreq instance.
311 *
312 * Helper function for starting devfreq device load monitoing. By
313 * default delayed work based monitoring is supported. Function
314 * to be called from governor in response to DEVFREQ_GOV_START
315 * event when device is added to devfreq framework.
316 */
317void devfreq_monitor_start(struct devfreq *devfreq)
318{
319 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
320 if (devfreq->profile->polling_ms)
321 queue_delayed_work(devfreq_wq, &devfreq->work,
322 msecs_to_jiffies(devfreq->profile->polling_ms));
323}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100324EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200325
326/**
327 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
328 * @devfreq: the devfreq instance.
329 *
330 * Helper function to stop devfreq device load monitoing. Function
331 * to be called from governor in response to DEVFREQ_GOV_STOP
332 * event when device is removed from devfreq framework.
333 */
334void devfreq_monitor_stop(struct devfreq *devfreq)
335{
336 cancel_delayed_work_sync(&devfreq->work);
337}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100338EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200339
340/**
341 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
342 * @devfreq: the devfreq instance.
343 *
344 * Helper function to suspend devfreq device load monitoing. Function
345 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
346 * event or when polling interval is set to zero.
347 *
348 * Note: Though this function is same as devfreq_monitor_stop(),
349 * intentionally kept separate to provide hooks for collecting
350 * transition statistics.
351 */
352void devfreq_monitor_suspend(struct devfreq *devfreq)
353{
354 mutex_lock(&devfreq->lock);
355 if (devfreq->stop_polling) {
356 mutex_unlock(&devfreq->lock);
357 return;
358 }
359
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530360 devfreq_update_status(devfreq, devfreq->previous_freq);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200361 devfreq->stop_polling = true;
362 mutex_unlock(&devfreq->lock);
363 cancel_delayed_work_sync(&devfreq->work);
364}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100365EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200366
367/**
368 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
369 * @devfreq: the devfreq instance.
370 *
371 * Helper function to resume devfreq device load monitoing. Function
372 * to be called from governor in response to DEVFREQ_GOV_RESUME
373 * event or when polling interval is set to non-zero.
374 */
375void devfreq_monitor_resume(struct devfreq *devfreq)
376{
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530377 unsigned long freq;
378
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200379 mutex_lock(&devfreq->lock);
380 if (!devfreq->stop_polling)
381 goto out;
382
383 if (!delayed_work_pending(&devfreq->work) &&
384 devfreq->profile->polling_ms)
385 queue_delayed_work(devfreq_wq, &devfreq->work,
386 msecs_to_jiffies(devfreq->profile->polling_ms));
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530387
388 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200389 devfreq->stop_polling = false;
390
Rajagopal Venkat39688ce2013-01-08 11:20:39 +0530391 if (devfreq->profile->get_cur_freq &&
392 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
393 devfreq->previous_freq = freq;
394
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200395out:
396 mutex_unlock(&devfreq->lock);
397}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100398EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200399
400/**
401 * devfreq_interval_update() - Update device devfreq monitoring interval
402 * @devfreq: the devfreq instance.
403 * @delay: new polling interval to be set.
404 *
405 * Helper function to set new load monitoring polling interval. Function
406 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
407 */
408void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
409{
410 unsigned int cur_delay = devfreq->profile->polling_ms;
411 unsigned int new_delay = *delay;
412
413 mutex_lock(&devfreq->lock);
414 devfreq->profile->polling_ms = new_delay;
415
416 if (devfreq->stop_polling)
417 goto out;
418
419 /* if new delay is zero, stop polling */
420 if (!new_delay) {
421 mutex_unlock(&devfreq->lock);
422 cancel_delayed_work_sync(&devfreq->work);
423 return;
424 }
425
426 /* if current delay is zero, start polling with new delay */
427 if (!cur_delay) {
428 queue_delayed_work(devfreq_wq, &devfreq->work,
429 msecs_to_jiffies(devfreq->profile->polling_ms));
430 goto out;
431 }
432
433 /* if current delay is greater than new delay, restart polling */
434 if (cur_delay > new_delay) {
435 mutex_unlock(&devfreq->lock);
436 cancel_delayed_work_sync(&devfreq->work);
437 mutex_lock(&devfreq->lock);
438 if (!devfreq->stop_polling)
439 queue_delayed_work(devfreq_wq, &devfreq->work,
440 msecs_to_jiffies(devfreq->profile->polling_ms));
441 }
442out:
443 mutex_unlock(&devfreq->lock);
444}
MyungJoo Ham6dcdd8e2012-11-28 20:29:17 +0100445EXPORT_SYMBOL(devfreq_interval_update);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200446
447/**
448 * devfreq_notifier_call() - Notify that the device frequency requirements
449 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200450 * @nb: the notifier_block (supposed to be devfreq->nb)
451 * @type: not used
452 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200453 *
454 * Called by a notifier that uses devfreq->nb.
455 */
456static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
457 void *devp)
458{
459 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
460 int ret;
461
462 mutex_lock(&devfreq->lock);
463 ret = update_devfreq(devfreq);
464 mutex_unlock(&devfreq->lock);
465
466 return ret;
467}
468
469/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200470 * _remove_devfreq() - Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200471 * @devfreq: the devfreq struct
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200472 */
Chanwoo Choi585fc832014-05-09 16:43:07 +0900473static void _remove_devfreq(struct devfreq *devfreq)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200474{
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200475 mutex_lock(&devfreq_list_lock);
476 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
477 mutex_unlock(&devfreq_list_lock);
478 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200479 return;
480 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200481 list_del(&devfreq->node);
482 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200483
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500484 if (devfreq->governor)
485 devfreq->governor->event_handler(devfreq,
486 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200487
488 if (devfreq->profile->exit)
489 devfreq->profile->exit(devfreq->dev.parent);
490
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200491 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200492 kfree(devfreq);
493}
494
495/**
496 * devfreq_dev_release() - Callback for struct device to release the device.
497 * @dev: the devfreq device
498 *
499 * This calls _remove_devfreq() if _remove_devfreq() is not called.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200500 */
501static void devfreq_dev_release(struct device *dev)
502{
503 struct devfreq *devfreq = to_devfreq(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200504
Chanwoo Choi585fc832014-05-09 16:43:07 +0900505 _remove_devfreq(devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200506}
507
508/**
509 * devfreq_add_device() - Add devfreq feature to the device
510 * @dev: the device to add devfreq feature.
511 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500512 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200513 * @data: private data for the governor. The devfreq framework does not
514 * touch this value.
515 */
516struct devfreq *devfreq_add_device(struct device *dev,
517 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500518 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200519 void *data)
520{
521 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500522 struct devfreq_governor *governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200523 int err = 0;
524
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500525 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200526 dev_err(dev, "%s: Invalid parameters.\n", __func__);
527 return ERR_PTR(-EINVAL);
528 }
529
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200530 mutex_lock(&devfreq_list_lock);
531 devfreq = find_device_devfreq(dev);
532 mutex_unlock(&devfreq_list_lock);
533 if (!IS_ERR(devfreq)) {
534 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
535 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) {
541 dev_err(dev, "%s: Unable to create devfreq for the device\n",
542 __func__);
543 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100544 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200545 }
546
547 mutex_init(&devfreq->lock);
548 mutex_lock(&devfreq->lock);
549 devfreq->dev.parent = dev;
550 devfreq->dev.class = devfreq_class;
551 devfreq->dev.release = devfreq_dev_release;
552 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500553 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200554 devfreq->previous_freq = profile->initial_freq;
555 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200556 devfreq->nb.notifier_call = devfreq_notifier_call;
557
Chanwoo Choi0ec09ac2015-11-18 14:49:02 +0900558 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
559 mutex_unlock(&devfreq->lock);
560 devfreq_set_freq_table(devfreq);
561 mutex_lock(&devfreq->lock);
562 }
563
Kees Cook02aa2a32013-07-03 15:04:56 -0700564 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200565 err = device_register(&devfreq->dev);
566 if (err) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200567 mutex_unlock(&devfreq->lock);
Geliang Tang6d3cbfa2015-10-01 22:18:19 +0800568 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200569 }
570
MyungJoo Ham3e1d7fb2015-10-02 12:39:23 +0900571 devfreq->trans_table = devm_kzalloc(&devfreq->dev, sizeof(unsigned int) *
572 devfreq->profile->max_state *
573 devfreq->profile->max_state,
574 GFP_KERNEL);
575 devfreq->time_in_state = devm_kzalloc(&devfreq->dev, sizeof(unsigned long) *
576 devfreq->profile->max_state,
577 GFP_KERNEL);
578 devfreq->last_stat_updated = jiffies;
579
Chanwoo Choi0fe3a662016-01-26 13:21:26 +0900580 srcu_init_notifier_head(&devfreq->transition_notifier_list);
581
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200582 mutex_unlock(&devfreq->lock);
583
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200584 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200585 list_add(&devfreq->node, &devfreq_list);
586
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500587 governor = find_devfreq_governor(devfreq->governor_name);
588 if (!IS_ERR(governor))
589 devfreq->governor = governor;
590 if (devfreq->governor)
591 err = devfreq->governor->event_handler(devfreq,
592 DEVFREQ_GOV_START, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200593 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200594 if (err) {
595 dev_err(dev, "%s: Unable to start governor for the device\n",
596 __func__);
597 goto err_init;
598 }
599
Axel Lin3f19f082011-11-15 21:59:09 +0100600 return devfreq;
601
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200602err_init:
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200603 list_del(&devfreq->node);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200604 device_unregister(&devfreq->dev);
Axel Lin3f19f082011-11-15 21:59:09 +0100605err_out:
606 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200607}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200608EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200609
610/**
611 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200612 * @devfreq: the devfreq instance to be removed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900613 *
614 * The opposite of devfreq_add_device().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200615 */
616int devfreq_remove_device(struct devfreq *devfreq)
617{
618 if (!devfreq)
619 return -EINVAL;
620
Chanwoo Choi585fc832014-05-09 16:43:07 +0900621 device_unregister(&devfreq->dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200622
623 return 0;
624}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200625EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200626
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900627static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
628{
629 struct devfreq **r = res;
630
631 if (WARN_ON(!r || !*r))
632 return 0;
633
634 return *r == data;
635}
636
637static void devm_devfreq_dev_release(struct device *dev, void *res)
638{
639 devfreq_remove_device(*(struct devfreq **)res);
640}
641
642/**
643 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
644 * @dev: the device to add devfreq feature.
645 * @profile: device-specific profile to run devfreq.
646 * @governor_name: name of the policy to choose frequency.
647 * @data: private data for the governor. The devfreq framework does not
648 * touch this value.
649 *
650 * This function manages automatically the memory of devfreq device using device
651 * resource management and simplify the free operation for memory of devfreq
652 * device.
653 */
654struct devfreq *devm_devfreq_add_device(struct device *dev,
655 struct devfreq_dev_profile *profile,
656 const char *governor_name,
657 void *data)
658{
659 struct devfreq **ptr, *devfreq;
660
661 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
662 if (!ptr)
663 return ERR_PTR(-ENOMEM);
664
665 devfreq = devfreq_add_device(dev, profile, governor_name, data);
666 if (IS_ERR(devfreq)) {
667 devres_free(ptr);
668 return ERR_PTR(-ENOMEM);
669 }
670
671 *ptr = devfreq;
672 devres_add(dev, ptr);
673
674 return devfreq;
675}
676EXPORT_SYMBOL(devm_devfreq_add_device);
677
Chanwoo Choi8f510ae2015-11-10 20:31:07 +0900678#ifdef CONFIG_OF
679/*
680 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
681 * @dev - instance to the given device
682 * @index - index into list of devfreq
683 *
684 * return the instance of devfreq device
685 */
686struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
687{
688 struct device_node *node;
689 struct devfreq *devfreq;
690
691 if (!dev)
692 return ERR_PTR(-EINVAL);
693
694 if (!dev->of_node)
695 return ERR_PTR(-EINVAL);
696
697 node = of_parse_phandle(dev->of_node, "devfreq", index);
698 if (!node)
699 return ERR_PTR(-ENODEV);
700
701 mutex_lock(&devfreq_list_lock);
702 list_for_each_entry(devfreq, &devfreq_list, node) {
703 if (devfreq->dev.parent
704 && devfreq->dev.parent->of_node == node) {
705 mutex_unlock(&devfreq_list_lock);
706 return devfreq;
707 }
708 }
709 mutex_unlock(&devfreq_list_lock);
710
711 return ERR_PTR(-EPROBE_DEFER);
712}
713#else
714struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
715{
716 return ERR_PTR(-ENODEV);
717}
718#endif /* CONFIG_OF */
719EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
720
Chanwoo Choi8cd84092014-05-09 16:43:08 +0900721/**
722 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
723 * @dev: the device to add devfreq feature.
724 * @devfreq: the devfreq instance to be removed
725 */
726void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
727{
728 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
729 devm_devfreq_dev_match, devfreq));
730}
731EXPORT_SYMBOL(devm_devfreq_remove_device);
732
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200733/**
734 * devfreq_suspend_device() - Suspend devfreq of a device.
735 * @devfreq: the devfreq instance to be suspended
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900736 *
737 * This function is intended to be called by the pm callbacks
738 * (e.g., runtime_suspend, suspend) of the device driver that
739 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200740 */
741int devfreq_suspend_device(struct devfreq *devfreq)
742{
743 if (!devfreq)
744 return -EINVAL;
745
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500746 if (!devfreq->governor)
747 return 0;
748
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200749 return devfreq->governor->event_handler(devfreq,
750 DEVFREQ_GOV_SUSPEND, NULL);
751}
752EXPORT_SYMBOL(devfreq_suspend_device);
753
754/**
755 * devfreq_resume_device() - Resume devfreq of a device.
756 * @devfreq: the devfreq instance to be resumed
MyungJoo Hamde9c7392013-02-05 18:40:17 +0900757 *
758 * This function is intended to be called by the pm callbacks
759 * (e.g., runtime_resume, resume) of the device driver that
760 * holds the devfreq.
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200761 */
762int devfreq_resume_device(struct devfreq *devfreq)
763{
764 if (!devfreq)
765 return -EINVAL;
766
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500767 if (!devfreq->governor)
768 return 0;
769
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200770 return devfreq->governor->event_handler(devfreq,
771 DEVFREQ_GOV_RESUME, NULL);
772}
773EXPORT_SYMBOL(devfreq_resume_device);
774
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500775/**
776 * devfreq_add_governor() - Add devfreq governor
777 * @governor: the devfreq governor to be added
778 */
779int devfreq_add_governor(struct devfreq_governor *governor)
780{
781 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500782 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500783 int err = 0;
784
785 if (!governor) {
786 pr_err("%s: Invalid parameters.\n", __func__);
787 return -EINVAL;
788 }
789
790 mutex_lock(&devfreq_list_lock);
791 g = find_devfreq_governor(governor->name);
792 if (!IS_ERR(g)) {
793 pr_err("%s: governor %s already registered\n", __func__,
794 g->name);
795 err = -EINVAL;
796 goto err_out;
797 }
798
799 list_add(&governor->node, &devfreq_governor_list);
800
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500801 list_for_each_entry(devfreq, &devfreq_list, node) {
802 int ret = 0;
803 struct device *dev = devfreq->dev.parent;
804
805 if (!strncmp(devfreq->governor_name, governor->name,
806 DEVFREQ_NAME_LEN)) {
807 /* The following should never occur */
808 if (devfreq->governor) {
809 dev_warn(dev,
810 "%s: Governor %s already present\n",
811 __func__, devfreq->governor->name);
812 ret = devfreq->governor->event_handler(devfreq,
813 DEVFREQ_GOV_STOP, NULL);
814 if (ret) {
815 dev_warn(dev,
816 "%s: Governor %s stop = %d\n",
817 __func__,
818 devfreq->governor->name, ret);
819 }
820 /* Fall through */
821 }
822 devfreq->governor = governor;
823 ret = devfreq->governor->event_handler(devfreq,
824 DEVFREQ_GOV_START, NULL);
825 if (ret) {
826 dev_warn(dev, "%s: Governor %s start=%d\n",
827 __func__, devfreq->governor->name,
828 ret);
829 }
830 }
831 }
832
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500833err_out:
834 mutex_unlock(&devfreq_list_lock);
835
836 return err;
837}
838EXPORT_SYMBOL(devfreq_add_governor);
839
840/**
841 * devfreq_remove_device() - Remove devfreq feature from a device.
842 * @governor: the devfreq governor to be removed
843 */
844int devfreq_remove_governor(struct devfreq_governor *governor)
845{
846 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500847 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500848 int err = 0;
849
850 if (!governor) {
851 pr_err("%s: Invalid parameters.\n", __func__);
852 return -EINVAL;
853 }
854
855 mutex_lock(&devfreq_list_lock);
856 g = find_devfreq_governor(governor->name);
857 if (IS_ERR(g)) {
858 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamatb9e1c8e2012-11-21 10:36:13 +0530859 governor->name);
Sachin Kamatf9c08e22012-11-21 10:36:14 +0530860 err = PTR_ERR(g);
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500861 goto err_out;
862 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500863 list_for_each_entry(devfreq, &devfreq_list, node) {
864 int ret;
865 struct device *dev = devfreq->dev.parent;
866
867 if (!strncmp(devfreq->governor_name, governor->name,
868 DEVFREQ_NAME_LEN)) {
869 /* we should have a devfreq governor! */
870 if (!devfreq->governor) {
871 dev_warn(dev, "%s: Governor %s NOT present\n",
872 __func__, governor->name);
873 continue;
874 /* Fall through */
875 }
876 ret = devfreq->governor->event_handler(devfreq,
877 DEVFREQ_GOV_STOP, NULL);
878 if (ret) {
879 dev_warn(dev, "%s: Governor %s stop=%d\n",
880 __func__, devfreq->governor->name,
881 ret);
882 }
883 devfreq->governor = NULL;
884 }
885 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500886
887 list_del(&governor->node);
888err_out:
889 mutex_unlock(&devfreq_list_lock);
890
891 return err;
892}
893EXPORT_SYMBOL(devfreq_remove_governor);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200894
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700895static ssize_t governor_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200896 struct device_attribute *attr, char *buf)
897{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500898 if (!to_devfreq(dev)->governor)
899 return -EINVAL;
900
MyungJoo Ham9005b652011-10-02 00:19:28 +0200901 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
902}
903
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700904static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500905 const char *buf, size_t count)
906{
907 struct devfreq *df = to_devfreq(dev);
908 int ret;
909 char str_governor[DEVFREQ_NAME_LEN + 1];
910 struct devfreq_governor *governor;
911
912 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
913 if (ret != 1)
914 return -EINVAL;
915
916 mutex_lock(&devfreq_list_lock);
917 governor = find_devfreq_governor(str_governor);
918 if (IS_ERR(governor)) {
919 ret = PTR_ERR(governor);
920 goto out;
921 }
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200922 if (df->governor == governor) {
923 ret = 0;
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500924 goto out;
Tobias Jakobi14a21e72015-09-21 20:23:52 +0200925 }
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500926
927 if (df->governor) {
928 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
929 if (ret) {
930 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
931 __func__, df->governor->name, ret);
932 goto out;
933 }
934 }
935 df->governor = governor;
936 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
937 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
938 if (ret)
939 dev_warn(dev, "%s: Governor %s not started(%d)\n",
940 __func__, df->governor->name, ret);
941out:
942 mutex_unlock(&devfreq_list_lock);
943
944 if (!ret)
945 ret = count;
946 return ret;
947}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700948static DEVICE_ATTR_RW(governor);
949
950static ssize_t available_governors_show(struct device *d,
951 struct device_attribute *attr,
952 char *buf)
Nishanth Menon50a5b332012-10-29 15:01:48 -0500953{
954 struct devfreq_governor *tmp_governor;
955 ssize_t count = 0;
956
957 mutex_lock(&devfreq_list_lock);
958 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
959 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
960 "%s ", tmp_governor->name);
961 mutex_unlock(&devfreq_list_lock);
962
963 /* Truncate the trailing space */
964 if (count)
965 count--;
966
967 count += sprintf(&buf[count], "\n");
968
969 return count;
970}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700971static DEVICE_ATTR_RO(available_governors);
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500972
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700973static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
974 char *buf)
MyungJoo Ham9005b652011-10-02 00:19:28 +0200975{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200976 unsigned long freq;
977 struct devfreq *devfreq = to_devfreq(dev);
978
979 if (devfreq->profile->get_cur_freq &&
980 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
981 return sprintf(buf, "%lu\n", freq);
982
983 return sprintf(buf, "%lu\n", devfreq->previous_freq);
984}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700985static DEVICE_ATTR_RO(cur_freq);
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200986
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700987static ssize_t target_freq_show(struct device *dev,
988 struct device_attribute *attr, char *buf)
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200989{
MyungJoo Ham9005b652011-10-02 00:19:28 +0200990 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
991}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700992static DEVICE_ATTR_RO(target_freq);
MyungJoo Ham9005b652011-10-02 00:19:28 +0200993
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -0700994static ssize_t polling_interval_show(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +0200995 struct device_attribute *attr, char *buf)
996{
997 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
998}
999
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001000static ssize_t polling_interval_store(struct device *dev,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001001 struct device_attribute *attr,
1002 const char *buf, size_t count)
1003{
1004 struct devfreq *df = to_devfreq(dev);
1005 unsigned int value;
1006 int ret;
1007
Nishanth Menon1b5c1be2012-10-29 15:01:45 -05001008 if (!df->governor)
1009 return -EINVAL;
1010
MyungJoo Ham9005b652011-10-02 00:19:28 +02001011 ret = sscanf(buf, "%u", &value);
1012 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001013 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +02001014
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001015 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001016 ret = count;
1017
MyungJoo Ham9005b652011-10-02 00:19:28 +02001018 return ret;
1019}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001020static DEVICE_ATTR_RW(polling_interval);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001021
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001022static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001023 const char *buf, size_t count)
1024{
1025 struct devfreq *df = to_devfreq(dev);
1026 unsigned long value;
1027 int ret;
1028 unsigned long max;
1029
1030 ret = sscanf(buf, "%lu", &value);
1031 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001032 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001033
1034 mutex_lock(&df->lock);
1035 max = df->max_freq;
1036 if (value && max && value > max) {
1037 ret = -EINVAL;
1038 goto unlock;
1039 }
1040
1041 df->min_freq = value;
1042 update_devfreq(df);
1043 ret = count;
1044unlock:
1045 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001046 return ret;
1047}
1048
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001049static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001050 const char *buf, size_t count)
1051{
1052 struct devfreq *df = to_devfreq(dev);
1053 unsigned long value;
1054 int ret;
1055 unsigned long min;
1056
1057 ret = sscanf(buf, "%lu", &value);
1058 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +02001059 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001060
1061 mutex_lock(&df->lock);
1062 min = df->min_freq;
1063 if (value && min && value < min) {
1064 ret = -EINVAL;
1065 goto unlock;
1066 }
1067
1068 df->max_freq = value;
1069 update_devfreq(df);
1070 ret = count;
1071unlock:
1072 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001073 return ret;
1074}
1075
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001076#define show_one(name) \
1077static ssize_t name##_show \
1078(struct device *dev, struct device_attribute *attr, char *buf) \
1079{ \
1080 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001081}
Chanwoo Choi3104fa32015-11-13 19:25:28 +09001082show_one(min_freq);
1083show_one(max_freq);
1084
1085static DEVICE_ATTR_RW(min_freq);
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001086static DEVICE_ATTR_RW(max_freq);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +09001087
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001088static ssize_t available_frequencies_show(struct device *d,
1089 struct device_attribute *attr,
1090 char *buf)
Nishanth Menond287de82012-10-25 19:48:59 -05001091{
1092 struct devfreq *df = to_devfreq(d);
1093 struct device *dev = df->dev.parent;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001094 struct dev_pm_opp *opp;
Nishanth Menond287de82012-10-25 19:48:59 -05001095 ssize_t count = 0;
1096 unsigned long freq = 0;
1097
1098 rcu_read_lock();
1099 do {
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001100 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
Nishanth Menond287de82012-10-25 19:48:59 -05001101 if (IS_ERR(opp))
1102 break;
1103
1104 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1105 "%lu ", freq);
1106 freq++;
1107 } while (1);
1108 rcu_read_unlock();
1109
1110 /* Truncate the trailing space */
1111 if (count)
1112 count--;
1113
1114 count += sprintf(&buf[count], "\n");
1115
1116 return count;
1117}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001118static DEVICE_ATTR_RO(available_frequencies);
Nishanth Menond287de82012-10-25 19:48:59 -05001119
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001120static ssize_t trans_stat_show(struct device *dev,
1121 struct device_attribute *attr, char *buf)
Jonghwa Leee552bba2012-08-23 20:00:46 +09001122{
1123 struct devfreq *devfreq = to_devfreq(dev);
1124 ssize_t len;
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301125 int i, j;
Jonghwa Leee552bba2012-08-23 20:00:46 +09001126 unsigned int max_state = devfreq->profile->max_state;
1127
Rajagopal Venkat39688ce2013-01-08 11:20:39 +05301128 if (!devfreq->stop_polling &&
1129 devfreq_update_status(devfreq, devfreq->previous_freq))
Jonghwa Leee552bba2012-08-23 20:00:46 +09001130 return 0;
MyungJoo Ham34bd3222015-11-23 15:45:36 +09001131 if (max_state == 0)
1132 return sprintf(buf, "Not Supported.\n");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001133
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001134 len = sprintf(buf, " From : To\n");
1135 len += sprintf(buf + len, " :");
Jonghwa Leee552bba2012-08-23 20:00:46 +09001136 for (i = 0; i < max_state; i++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001137 len += sprintf(buf + len, "%10lu",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001138 devfreq->profile->freq_table[i]);
1139
1140 len += sprintf(buf + len, " time(ms)\n");
1141
1142 for (i = 0; i < max_state; i++) {
1143 if (devfreq->profile->freq_table[i]
1144 == devfreq->previous_freq) {
1145 len += sprintf(buf + len, "*");
1146 } else {
1147 len += sprintf(buf + len, " ");
1148 }
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001149 len += sprintf(buf + len, "%10lu:",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001150 devfreq->profile->freq_table[i]);
1151 for (j = 0; j < max_state; j++)
Chanwoo Choid7df1e42015-11-19 16:28:46 +09001152 len += sprintf(buf + len, "%10u",
Jonghwa Leee552bba2012-08-23 20:00:46 +09001153 devfreq->trans_table[(i * max_state) + j]);
1154 len += sprintf(buf + len, "%10u\n",
1155 jiffies_to_msecs(devfreq->time_in_state[i]));
1156 }
1157
1158 len += sprintf(buf + len, "Total transition : %u\n",
1159 devfreq->total_trans);
1160 return len;
1161}
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001162static DEVICE_ATTR_RO(trans_stat);
Jonghwa Leee552bba2012-08-23 20:00:46 +09001163
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001164static struct attribute *devfreq_attrs[] = {
1165 &dev_attr_governor.attr,
1166 &dev_attr_available_governors.attr,
1167 &dev_attr_cur_freq.attr,
1168 &dev_attr_available_frequencies.attr,
1169 &dev_attr_target_freq.attr,
1170 &dev_attr_polling_interval.attr,
1171 &dev_attr_min_freq.attr,
1172 &dev_attr_max_freq.attr,
1173 &dev_attr_trans_stat.attr,
1174 NULL,
MyungJoo Ham9005b652011-10-02 00:19:28 +02001175};
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001176ATTRIBUTE_GROUPS(devfreq);
MyungJoo Ham9005b652011-10-02 00:19:28 +02001177
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001178static int __init devfreq_init(void)
1179{
1180 devfreq_class = class_create(THIS_MODULE, "devfreq");
1181 if (IS_ERR(devfreq_class)) {
1182 pr_err("%s: couldn't create class\n", __FILE__);
1183 return PTR_ERR(devfreq_class);
1184 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001185
1186 devfreq_wq = create_freezable_workqueue("devfreq_wq");
Dan Carpenterea7f4542013-08-15 10:55:10 +03001187 if (!devfreq_wq) {
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001188 class_destroy(devfreq_class);
1189 pr_err("%s: couldn't create workqueue\n", __FILE__);
Dan Carpenterea7f4542013-08-15 10:55:10 +03001190 return -ENOMEM;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001191 }
Greg Kroah-Hartmana93d6b02013-07-24 15:05:09 -07001192 devfreq_class->dev_groups = devfreq_groups;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001193
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001194 return 0;
1195}
1196subsys_initcall(devfreq_init);
1197
1198static void __exit devfreq_exit(void)
1199{
1200 class_destroy(devfreq_class);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +02001201 destroy_workqueue(devfreq_wq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001202}
1203module_exit(devfreq_exit);
1204
1205/*
1206 * The followings are helper functions for devfreq user device drivers with
1207 * OPP framework.
1208 */
1209
1210/**
1211 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1212 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001213 * @dev: The devfreq user device. (parent of devfreq)
1214 * @freq: The frequency given to target function
1215 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001216 *
Nishanth Menonbcb27542013-01-18 19:52:34 +00001217 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1218 * protected pointer. The reason for the same is that the opp pointer which is
1219 * returned will remain valid for use with opp_get_{voltage, freq} only while
1220 * under the locked area. The pointer returned must be used prior to unlocking
1221 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001222 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001223struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1224 unsigned long *freq,
1225 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001226{
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001227 struct dev_pm_opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001228
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001229 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1230 /* The freq is an upper bound. opp should be lower */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001231 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001232
1233 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001234 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001235 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001236 } else {
1237 /* The freq is an lower bound. opp should be higher */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001238 opp = dev_pm_opp_find_freq_ceil(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001239
1240 /* If not available, use the closest opp */
Nishanth Menon07797262012-10-24 22:00:12 +02001241 if (opp == ERR_PTR(-ERANGE))
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001242 opp = dev_pm_opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001243 }
1244
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001245 return opp;
1246}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001247EXPORT_SYMBOL(devfreq_recommended_opp);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001248
1249/**
1250 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1251 * for any changes in the OPP availability
1252 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001253 * @dev: The devfreq user device. (parent of devfreq)
1254 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001255 */
1256int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1257{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001258 struct srcu_notifier_head *nh;
1259 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001260
MyungJoo Ham389baed2012-11-21 19:04:51 +09001261 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001262 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001263 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001264 ret = PTR_ERR(nh);
1265 rcu_read_unlock();
1266 if (!ret)
1267 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1268
1269 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001270}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001271EXPORT_SYMBOL(devfreq_register_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001272
1273/**
1274 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1275 * notified for any changes in the OPP
1276 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001277 * @dev: The devfreq user device. (parent of devfreq)
1278 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001279 *
1280 * At exit() callback of devfreq_dev_profile, this must be included if
1281 * devfreq_recommended_opp is used.
1282 */
1283int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1284{
MyungJoo Ham389baed2012-11-21 19:04:51 +09001285 struct srcu_notifier_head *nh;
1286 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001287
MyungJoo Ham389baed2012-11-21 19:04:51 +09001288 rcu_read_lock();
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001289 nh = dev_pm_opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001290 if (IS_ERR(nh))
MyungJoo Ham389baed2012-11-21 19:04:51 +09001291 ret = PTR_ERR(nh);
1292 rcu_read_unlock();
1293 if (!ret)
1294 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1295
1296 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001297}
Ãrjan Eidebd7e9272014-07-18 15:09:53 +01001298EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001299
Chanwoo Choid5b040d2014-05-09 16:43:09 +09001300static void devm_devfreq_opp_release(struct device *dev, void *res)
1301{
1302 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1303}
1304
1305/**
1306 * devm_ devfreq_register_opp_notifier()
1307 * - Resource-managed devfreq_register_opp_notifier()
1308 * @dev: The devfreq user device. (parent of devfreq)
1309 * @devfreq: The devfreq object.
1310 */
1311int devm_devfreq_register_opp_notifier(struct device *dev,
1312 struct devfreq *devfreq)
1313{
1314 struct devfreq **ptr;
1315 int ret;
1316
1317 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1318 if (!ptr)
1319 return -ENOMEM;
1320
1321 ret = devfreq_register_opp_notifier(dev, devfreq);
1322 if (ret) {
1323 devres_free(ptr);
1324 return ret;
1325 }
1326
1327 *ptr = devfreq;
1328 devres_add(dev, ptr);
1329
1330 return 0;
1331}
1332EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1333
1334/**
1335 * devm_devfreq_unregister_opp_notifier()
1336 * - Resource-managed devfreq_unregister_opp_notifier()
1337 * @dev: The devfreq user device. (parent of devfreq)
1338 * @devfreq: The devfreq object.
1339 */
1340void devm_devfreq_unregister_opp_notifier(struct device *dev,
1341 struct devfreq *devfreq)
1342{
1343 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1344 devm_devfreq_dev_match, devfreq));
1345}
1346EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1347
Chanwoo Choi0fe3a662016-01-26 13:21:26 +09001348/**
1349 * devfreq_register_notifier() - Register a driver with devfreq
1350 * @devfreq: The devfreq object.
1351 * @nb: The notifier block to register.
1352 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1353 */
1354int devfreq_register_notifier(struct devfreq *devfreq,
1355 struct notifier_block *nb,
1356 unsigned int list)
1357{
1358 int ret = 0;
1359
1360 if (!devfreq)
1361 return -EINVAL;
1362
1363 switch (list) {
1364 case DEVFREQ_TRANSITION_NOTIFIER:
1365 ret = srcu_notifier_chain_register(
1366 &devfreq->transition_notifier_list, nb);
1367 break;
1368 default:
1369 ret = -EINVAL;
1370 }
1371
1372 return ret;
1373}
1374EXPORT_SYMBOL(devfreq_register_notifier);
1375
1376/*
1377 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1378 * @devfreq: The devfreq object.
1379 * @nb: The notifier block to be unregistered.
1380 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1381 */
1382int devfreq_unregister_notifier(struct devfreq *devfreq,
1383 struct notifier_block *nb,
1384 unsigned int list)
1385{
1386 int ret = 0;
1387
1388 if (!devfreq)
1389 return -EINVAL;
1390
1391 switch (list) {
1392 case DEVFREQ_TRANSITION_NOTIFIER:
1393 ret = srcu_notifier_chain_unregister(
1394 &devfreq->transition_notifier_list, nb);
1395 break;
1396 default:
1397 ret = -EINVAL;
1398 }
1399
1400 return ret;
1401}
1402EXPORT_SYMBOL(devfreq_unregister_notifier);
1403
1404struct devfreq_notifier_devres {
1405 struct devfreq *devfreq;
1406 struct notifier_block *nb;
1407 unsigned int list;
1408};
1409
1410static void devm_devfreq_notifier_release(struct device *dev, void *res)
1411{
1412 struct devfreq_notifier_devres *this = res;
1413
1414 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1415}
1416
1417/**
1418 * devm_devfreq_register_notifier()
1419 - Resource-managed devfreq_register_notifier()
1420 * @dev: The devfreq user device. (parent of devfreq)
1421 * @devfreq: The devfreq object.
1422 * @nb: The notifier block to be unregistered.
1423 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1424 */
1425int devm_devfreq_register_notifier(struct device *dev,
1426 struct devfreq *devfreq,
1427 struct notifier_block *nb,
1428 unsigned int list)
1429{
1430 struct devfreq_notifier_devres *ptr;
1431 int ret;
1432
1433 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1434 GFP_KERNEL);
1435 if (!ptr)
1436 return -ENOMEM;
1437
1438 ret = devfreq_register_notifier(devfreq, nb, list);
1439 if (ret) {
1440 devres_free(ptr);
1441 return ret;
1442 }
1443
1444 ptr->devfreq = devfreq;
1445 ptr->nb = nb;
1446 ptr->list = list;
1447 devres_add(dev, ptr);
1448
1449 return 0;
1450}
1451EXPORT_SYMBOL(devm_devfreq_register_notifier);
1452
1453/**
1454 * devm_devfreq_unregister_notifier()
1455 - Resource-managed devfreq_unregister_notifier()
1456 * @dev: The devfreq user device. (parent of devfreq)
1457 * @devfreq: The devfreq object.
1458 * @nb: The notifier block to be unregistered.
1459 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1460 */
1461void devm_devfreq_unregister_notifier(struct device *dev,
1462 struct devfreq *devfreq,
1463 struct notifier_block *nb,
1464 unsigned int list)
1465{
1466 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1467 devm_devfreq_dev_match, devfreq));
1468}
1469EXPORT_SYMBOL(devm_devfreq_unregister_notifier);
1470
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001471MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1472MODULE_DESCRIPTION("devfreq class support");
1473MODULE_LICENSE("GPL");