blob: 45e053e5b139aadc633e6cef6d7aabc3dd45629c [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>
MyungJoo Hama3c98b82011-10-02 00:19:15 +020021#include <linux/opp.h>
22#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>
28#include "governor.h"
29
Nishanth Menon1a1357e2012-10-26 01:50:53 +020030static struct class *devfreq_class;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020031
32/*
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +020033 * devfreq core provides delayed work based load monitoring helper
34 * functions. Governors can use these or can implement their own
35 * monitoring mechanism.
MyungJoo Hama3c98b82011-10-02 00:19:15 +020036 */
MyungJoo Hama3c98b82011-10-02 00:19:15 +020037static struct workqueue_struct *devfreq_wq;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020038
Nishanth Menon3aa173b2012-10-29 15:01:43 -050039/* The list of all device-devfreq governors */
40static LIST_HEAD(devfreq_governor_list);
MyungJoo Hama3c98b82011-10-02 00:19:15 +020041/* The list of all device-devfreq */
42static LIST_HEAD(devfreq_list);
43static DEFINE_MUTEX(devfreq_list_lock);
44
45/**
46 * find_device_devfreq() - find devfreq struct using device pointer
47 * @dev: device pointer used to lookup device devfreq.
48 *
49 * Search the list of device devfreqs and return the matched device's
50 * devfreq info. devfreq_list_lock should be held by the caller.
51 */
52static struct devfreq *find_device_devfreq(struct device *dev)
53{
54 struct devfreq *tmp_devfreq;
55
56 if (unlikely(IS_ERR_OR_NULL(dev))) {
57 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
58 return ERR_PTR(-EINVAL);
59 }
60 WARN(!mutex_is_locked(&devfreq_list_lock),
61 "devfreq_list_lock must be locked.");
62
63 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
64 if (tmp_devfreq->dev.parent == dev)
65 return tmp_devfreq;
66 }
67
68 return ERR_PTR(-ENODEV);
69}
70
Jonghwa Leee552bba2012-08-23 20:00:46 +090071/**
72 * devfreq_get_freq_level() - Lookup freq_table for the frequency
73 * @devfreq: the devfreq instance
74 * @freq: the target frequency
75 */
76static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
77{
78 int lev;
79
80 for (lev = 0; lev < devfreq->profile->max_state; lev++)
81 if (freq == devfreq->profile->freq_table[lev])
82 return lev;
83
84 return -EINVAL;
85}
86
87/**
88 * devfreq_update_status() - Update statistics of devfreq behavior
89 * @devfreq: the devfreq instance
90 * @freq: the update target frequency
91 */
92static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
93{
94 int lev, prev_lev;
95 unsigned long cur_time;
96
97 lev = devfreq_get_freq_level(devfreq, freq);
98 if (lev < 0)
99 return lev;
100
101 cur_time = jiffies;
102 devfreq->time_in_state[lev] +=
103 cur_time - devfreq->last_stat_updated;
104 if (freq != devfreq->previous_freq) {
105 prev_lev = devfreq_get_freq_level(devfreq,
106 devfreq->previous_freq);
107 devfreq->trans_table[(prev_lev *
108 devfreq->profile->max_state) + lev]++;
109 devfreq->total_trans++;
110 }
111 devfreq->last_stat_updated = cur_time;
112
113 return 0;
114}
115
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500116/**
117 * find_devfreq_governor() - find devfreq governor from name
118 * @name: name of the governor
119 *
120 * Search the list of devfreq governors and return the matched
121 * governor's pointer. devfreq_list_lock should be held by the caller.
122 */
123static struct devfreq_governor *find_devfreq_governor(const char *name)
124{
125 struct devfreq_governor *tmp_governor;
126
127 if (unlikely(IS_ERR_OR_NULL(name))) {
128 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
129 return ERR_PTR(-EINVAL);
130 }
131 WARN(!mutex_is_locked(&devfreq_list_lock),
132 "devfreq_list_lock must be locked.");
133
134 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
135 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
136 return tmp_governor;
137 }
138
139 return ERR_PTR(-ENODEV);
140}
141
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200142/* Load monitoring helper functions for governors use */
143
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200144/**
145 * update_devfreq() - Reevaluate the device and configure frequency.
146 * @devfreq: the devfreq instance.
147 *
148 * Note: Lock devfreq->lock before calling update_devfreq
149 * This function is exported for governors.
150 */
151int update_devfreq(struct devfreq *devfreq)
152{
153 unsigned long freq;
154 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100155 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200156
157 if (!mutex_is_locked(&devfreq->lock)) {
158 WARN(true, "devfreq->lock must be locked by the caller.\n");
159 return -EINVAL;
160 }
161
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500162 if (!devfreq->governor)
163 return -EINVAL;
164
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200165 /* Reevaluate the proper frequency */
166 err = devfreq->governor->get_target_freq(devfreq, &freq);
167 if (err)
168 return err;
169
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100170 /*
171 * Adjust the freuqency with user freq and QoS.
172 *
173 * List from the highest proiority
174 * max_freq (probably called by thermal when it's too hot)
175 * min_freq
176 */
177
178 if (devfreq->min_freq && freq < devfreq->min_freq) {
179 freq = devfreq->min_freq;
180 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
181 }
182 if (devfreq->max_freq && freq > devfreq->max_freq) {
183 freq = devfreq->max_freq;
184 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
185 }
186
187 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200188 if (err)
189 return err;
190
Jonghwa Leee552bba2012-08-23 20:00:46 +0900191 if (devfreq->profile->freq_table)
192 if (devfreq_update_status(devfreq, freq))
193 dev_err(&devfreq->dev,
194 "Couldn't update frequency transition information.\n");
195
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200196 devfreq->previous_freq = freq;
197 return err;
198}
Nishanth Menon2df50212012-10-29 15:01:42 -0500199EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200200
201/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200202 * devfreq_monitor() - Periodically poll devfreq objects.
203 * @work: the work struct used to run devfreq_monitor periodically.
204 *
205 */
206static void devfreq_monitor(struct work_struct *work)
207{
208 int err;
209 struct devfreq *devfreq = container_of(work,
210 struct devfreq, work.work);
211
212 mutex_lock(&devfreq->lock);
213 err = update_devfreq(devfreq);
214 if (err)
215 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
216
217 queue_delayed_work(devfreq_wq, &devfreq->work,
218 msecs_to_jiffies(devfreq->profile->polling_ms));
219 mutex_unlock(&devfreq->lock);
220}
221
222/**
223 * devfreq_monitor_start() - Start load monitoring of devfreq instance
224 * @devfreq: the devfreq instance.
225 *
226 * Helper function for starting devfreq device load monitoing. By
227 * default delayed work based monitoring is supported. Function
228 * to be called from governor in response to DEVFREQ_GOV_START
229 * event when device is added to devfreq framework.
230 */
231void devfreq_monitor_start(struct devfreq *devfreq)
232{
233 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
234 if (devfreq->profile->polling_ms)
235 queue_delayed_work(devfreq_wq, &devfreq->work,
236 msecs_to_jiffies(devfreq->profile->polling_ms));
237}
238
239/**
240 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
241 * @devfreq: the devfreq instance.
242 *
243 * Helper function to stop devfreq device load monitoing. Function
244 * to be called from governor in response to DEVFREQ_GOV_STOP
245 * event when device is removed from devfreq framework.
246 */
247void devfreq_monitor_stop(struct devfreq *devfreq)
248{
249 cancel_delayed_work_sync(&devfreq->work);
250}
251
252/**
253 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
254 * @devfreq: the devfreq instance.
255 *
256 * Helper function to suspend devfreq device load monitoing. Function
257 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
258 * event or when polling interval is set to zero.
259 *
260 * Note: Though this function is same as devfreq_monitor_stop(),
261 * intentionally kept separate to provide hooks for collecting
262 * transition statistics.
263 */
264void devfreq_monitor_suspend(struct devfreq *devfreq)
265{
266 mutex_lock(&devfreq->lock);
267 if (devfreq->stop_polling) {
268 mutex_unlock(&devfreq->lock);
269 return;
270 }
271
272 devfreq->stop_polling = true;
273 mutex_unlock(&devfreq->lock);
274 cancel_delayed_work_sync(&devfreq->work);
275}
276
277/**
278 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
279 * @devfreq: the devfreq instance.
280 *
281 * Helper function to resume devfreq device load monitoing. Function
282 * to be called from governor in response to DEVFREQ_GOV_RESUME
283 * event or when polling interval is set to non-zero.
284 */
285void devfreq_monitor_resume(struct devfreq *devfreq)
286{
287 mutex_lock(&devfreq->lock);
288 if (!devfreq->stop_polling)
289 goto out;
290
291 if (!delayed_work_pending(&devfreq->work) &&
292 devfreq->profile->polling_ms)
293 queue_delayed_work(devfreq_wq, &devfreq->work,
294 msecs_to_jiffies(devfreq->profile->polling_ms));
295 devfreq->stop_polling = false;
296
297out:
298 mutex_unlock(&devfreq->lock);
299}
300
301/**
302 * devfreq_interval_update() - Update device devfreq monitoring interval
303 * @devfreq: the devfreq instance.
304 * @delay: new polling interval to be set.
305 *
306 * Helper function to set new load monitoring polling interval. Function
307 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
308 */
309void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
310{
311 unsigned int cur_delay = devfreq->profile->polling_ms;
312 unsigned int new_delay = *delay;
313
314 mutex_lock(&devfreq->lock);
315 devfreq->profile->polling_ms = new_delay;
316
317 if (devfreq->stop_polling)
318 goto out;
319
320 /* if new delay is zero, stop polling */
321 if (!new_delay) {
322 mutex_unlock(&devfreq->lock);
323 cancel_delayed_work_sync(&devfreq->work);
324 return;
325 }
326
327 /* if current delay is zero, start polling with new delay */
328 if (!cur_delay) {
329 queue_delayed_work(devfreq_wq, &devfreq->work,
330 msecs_to_jiffies(devfreq->profile->polling_ms));
331 goto out;
332 }
333
334 /* if current delay is greater than new delay, restart polling */
335 if (cur_delay > new_delay) {
336 mutex_unlock(&devfreq->lock);
337 cancel_delayed_work_sync(&devfreq->work);
338 mutex_lock(&devfreq->lock);
339 if (!devfreq->stop_polling)
340 queue_delayed_work(devfreq_wq, &devfreq->work,
341 msecs_to_jiffies(devfreq->profile->polling_ms));
342 }
343out:
344 mutex_unlock(&devfreq->lock);
345}
346
347/**
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200348 * devfreq_notifier_call() - Notify that the device frequency requirements
349 * has been changed out of devfreq framework.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200350 * @nb: the notifier_block (supposed to be devfreq->nb)
351 * @type: not used
352 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200353 *
354 * Called by a notifier that uses devfreq->nb.
355 */
356static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
357 void *devp)
358{
359 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
360 int ret;
361
362 mutex_lock(&devfreq->lock);
363 ret = update_devfreq(devfreq);
364 mutex_unlock(&devfreq->lock);
365
366 return ret;
367}
368
369/**
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200370 * _remove_devfreq() - Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200371 * @devfreq: the devfreq struct
372 * @skip: skip calling device_unregister().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200373 */
374static void _remove_devfreq(struct devfreq *devfreq, bool skip)
375{
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200376 mutex_lock(&devfreq_list_lock);
377 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
378 mutex_unlock(&devfreq_list_lock);
379 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200380 return;
381 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200382 list_del(&devfreq->node);
383 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200384
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500385 if (devfreq->governor)
386 devfreq->governor->event_handler(devfreq,
387 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200388
389 if (devfreq->profile->exit)
390 devfreq->profile->exit(devfreq->dev.parent);
391
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200392 if (!skip && get_device(&devfreq->dev)) {
393 device_unregister(&devfreq->dev);
394 put_device(&devfreq->dev);
395 }
396
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200397 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200398 kfree(devfreq);
399}
400
401/**
402 * devfreq_dev_release() - Callback for struct device to release the device.
403 * @dev: the devfreq device
404 *
405 * This calls _remove_devfreq() if _remove_devfreq() is not called.
406 * Note that devfreq_dev_release() could be called by _remove_devfreq() as
407 * well as by others unregistering the device.
408 */
409static void devfreq_dev_release(struct device *dev)
410{
411 struct devfreq *devfreq = to_devfreq(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200412
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200413 _remove_devfreq(devfreq, true);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200414}
415
416/**
417 * devfreq_add_device() - Add devfreq feature to the device
418 * @dev: the device to add devfreq feature.
419 * @profile: device-specific profile to run devfreq.
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500420 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200421 * @data: private data for the governor. The devfreq framework does not
422 * touch this value.
423 */
424struct devfreq *devfreq_add_device(struct device *dev,
425 struct devfreq_dev_profile *profile,
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500426 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200427 void *data)
428{
429 struct devfreq *devfreq;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500430 struct devfreq_governor *governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200431 int err = 0;
432
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500433 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200434 dev_err(dev, "%s: Invalid parameters.\n", __func__);
435 return ERR_PTR(-EINVAL);
436 }
437
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200438 mutex_lock(&devfreq_list_lock);
439 devfreq = find_device_devfreq(dev);
440 mutex_unlock(&devfreq_list_lock);
441 if (!IS_ERR(devfreq)) {
442 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
443 err = -EINVAL;
444 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200445 }
446
447 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
448 if (!devfreq) {
449 dev_err(dev, "%s: Unable to create devfreq for the device\n",
450 __func__);
451 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100452 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200453 }
454
455 mutex_init(&devfreq->lock);
456 mutex_lock(&devfreq->lock);
457 devfreq->dev.parent = dev;
458 devfreq->dev.class = devfreq_class;
459 devfreq->dev.release = devfreq_dev_release;
460 devfreq->profile = profile;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500461 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200462 devfreq->previous_freq = profile->initial_freq;
463 devfreq->data = data;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200464 devfreq->nb.notifier_call = devfreq_notifier_call;
465
Jonghwa Leee552bba2012-08-23 20:00:46 +0900466 devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
467 devfreq->profile->max_state *
468 devfreq->profile->max_state,
469 GFP_KERNEL);
470 devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) *
471 devfreq->profile->max_state,
472 GFP_KERNEL);
473 devfreq->last_stat_updated = jiffies;
474
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200475 dev_set_name(&devfreq->dev, dev_name(dev));
476 err = device_register(&devfreq->dev);
477 if (err) {
478 put_device(&devfreq->dev);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200479 mutex_unlock(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200480 goto err_dev;
481 }
482
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200483 mutex_unlock(&devfreq->lock);
484
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200485 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200486 list_add(&devfreq->node, &devfreq_list);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200487
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500488 governor = find_devfreq_governor(devfreq->governor_name);
489 if (!IS_ERR(governor))
490 devfreq->governor = governor;
491 if (devfreq->governor)
492 err = devfreq->governor->event_handler(devfreq,
493 DEVFREQ_GOV_START, NULL);
494 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200495 if (err) {
496 dev_err(dev, "%s: Unable to start governor for the device\n",
497 __func__);
498 goto err_init;
499 }
500
Axel Lin3f19f082011-11-15 21:59:09 +0100501 return devfreq;
502
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200503err_init:
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200504 list_del(&devfreq->node);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200505 device_unregister(&devfreq->dev);
506err_dev:
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200507 kfree(devfreq);
Axel Lin3f19f082011-11-15 21:59:09 +0100508err_out:
509 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200510}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200511EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200512
513/**
514 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200515 * @devfreq: the devfreq instance to be removed
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200516 */
517int devfreq_remove_device(struct devfreq *devfreq)
518{
519 if (!devfreq)
520 return -EINVAL;
521
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200522 _remove_devfreq(devfreq, false);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200523
524 return 0;
525}
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200526EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200527
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200528/**
529 * devfreq_suspend_device() - Suspend devfreq of a device.
530 * @devfreq: the devfreq instance to be suspended
531 */
532int devfreq_suspend_device(struct devfreq *devfreq)
533{
534 if (!devfreq)
535 return -EINVAL;
536
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500537 if (!devfreq->governor)
538 return 0;
539
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200540 return devfreq->governor->event_handler(devfreq,
541 DEVFREQ_GOV_SUSPEND, NULL);
542}
543EXPORT_SYMBOL(devfreq_suspend_device);
544
545/**
546 * devfreq_resume_device() - Resume devfreq of a device.
547 * @devfreq: the devfreq instance to be resumed
548 */
549int devfreq_resume_device(struct devfreq *devfreq)
550{
551 if (!devfreq)
552 return -EINVAL;
553
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500554 if (!devfreq->governor)
555 return 0;
556
Rajagopal Venkat206c30c2012-10-26 01:50:18 +0200557 return devfreq->governor->event_handler(devfreq,
558 DEVFREQ_GOV_RESUME, NULL);
559}
560EXPORT_SYMBOL(devfreq_resume_device);
561
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500562/**
563 * devfreq_add_governor() - Add devfreq governor
564 * @governor: the devfreq governor to be added
565 */
566int devfreq_add_governor(struct devfreq_governor *governor)
567{
568 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500569 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500570 int err = 0;
571
572 if (!governor) {
573 pr_err("%s: Invalid parameters.\n", __func__);
574 return -EINVAL;
575 }
576
577 mutex_lock(&devfreq_list_lock);
578 g = find_devfreq_governor(governor->name);
579 if (!IS_ERR(g)) {
580 pr_err("%s: governor %s already registered\n", __func__,
581 g->name);
582 err = -EINVAL;
583 goto err_out;
584 }
585
586 list_add(&governor->node, &devfreq_governor_list);
587
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500588 list_for_each_entry(devfreq, &devfreq_list, node) {
589 int ret = 0;
590 struct device *dev = devfreq->dev.parent;
591
592 if (!strncmp(devfreq->governor_name, governor->name,
593 DEVFREQ_NAME_LEN)) {
594 /* The following should never occur */
595 if (devfreq->governor) {
596 dev_warn(dev,
597 "%s: Governor %s already present\n",
598 __func__, devfreq->governor->name);
599 ret = devfreq->governor->event_handler(devfreq,
600 DEVFREQ_GOV_STOP, NULL);
601 if (ret) {
602 dev_warn(dev,
603 "%s: Governor %s stop = %d\n",
604 __func__,
605 devfreq->governor->name, ret);
606 }
607 /* Fall through */
608 }
609 devfreq->governor = governor;
610 ret = devfreq->governor->event_handler(devfreq,
611 DEVFREQ_GOV_START, NULL);
612 if (ret) {
613 dev_warn(dev, "%s: Governor %s start=%d\n",
614 __func__, devfreq->governor->name,
615 ret);
616 }
617 }
618 }
619
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500620err_out:
621 mutex_unlock(&devfreq_list_lock);
622
623 return err;
624}
625EXPORT_SYMBOL(devfreq_add_governor);
626
627/**
628 * devfreq_remove_device() - Remove devfreq feature from a device.
629 * @governor: the devfreq governor to be removed
630 */
631int devfreq_remove_governor(struct devfreq_governor *governor)
632{
633 struct devfreq_governor *g;
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500634 struct devfreq *devfreq;
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500635 int err = 0;
636
637 if (!governor) {
638 pr_err("%s: Invalid parameters.\n", __func__);
639 return -EINVAL;
640 }
641
642 mutex_lock(&devfreq_list_lock);
643 g = find_devfreq_governor(governor->name);
644 if (IS_ERR(g)) {
645 pr_err("%s: governor %s not registered\n", __func__,
646 g->name);
647 err = -EINVAL;
648 goto err_out;
649 }
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500650 list_for_each_entry(devfreq, &devfreq_list, node) {
651 int ret;
652 struct device *dev = devfreq->dev.parent;
653
654 if (!strncmp(devfreq->governor_name, governor->name,
655 DEVFREQ_NAME_LEN)) {
656 /* we should have a devfreq governor! */
657 if (!devfreq->governor) {
658 dev_warn(dev, "%s: Governor %s NOT present\n",
659 __func__, governor->name);
660 continue;
661 /* Fall through */
662 }
663 ret = devfreq->governor->event_handler(devfreq,
664 DEVFREQ_GOV_STOP, NULL);
665 if (ret) {
666 dev_warn(dev, "%s: Governor %s stop=%d\n",
667 __func__, devfreq->governor->name,
668 ret);
669 }
670 devfreq->governor = NULL;
671 }
672 }
Nishanth Menon3aa173b2012-10-29 15:01:43 -0500673
674 list_del(&governor->node);
675err_out:
676 mutex_unlock(&devfreq_list_lock);
677
678 return err;
679}
680EXPORT_SYMBOL(devfreq_remove_governor);
681
MyungJoo Ham9005b652011-10-02 00:19:28 +0200682static ssize_t show_governor(struct device *dev,
683 struct device_attribute *attr, char *buf)
684{
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500685 if (!to_devfreq(dev)->governor)
686 return -EINVAL;
687
MyungJoo Ham9005b652011-10-02 00:19:28 +0200688 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
689}
690
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500691static ssize_t store_governor(struct device *dev, struct device_attribute *attr,
692 const char *buf, size_t count)
693{
694 struct devfreq *df = to_devfreq(dev);
695 int ret;
696 char str_governor[DEVFREQ_NAME_LEN + 1];
697 struct devfreq_governor *governor;
698
699 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
700 if (ret != 1)
701 return -EINVAL;
702
703 mutex_lock(&devfreq_list_lock);
704 governor = find_devfreq_governor(str_governor);
705 if (IS_ERR(governor)) {
706 ret = PTR_ERR(governor);
707 goto out;
708 }
709 if (df->governor == governor)
710 goto out;
711
712 if (df->governor) {
713 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
714 if (ret) {
715 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
716 __func__, df->governor->name, ret);
717 goto out;
718 }
719 }
720 df->governor = governor;
721 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
722 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
723 if (ret)
724 dev_warn(dev, "%s: Governor %s not started(%d)\n",
725 __func__, df->governor->name, ret);
726out:
727 mutex_unlock(&devfreq_list_lock);
728
729 if (!ret)
730 ret = count;
731 return ret;
732}
Nishanth Menon50a5b332012-10-29 15:01:48 -0500733static ssize_t show_available_governors(struct device *d,
734 struct device_attribute *attr,
735 char *buf)
736{
737 struct devfreq_governor *tmp_governor;
738 ssize_t count = 0;
739
740 mutex_lock(&devfreq_list_lock);
741 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
742 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
743 "%s ", tmp_governor->name);
744 mutex_unlock(&devfreq_list_lock);
745
746 /* Truncate the trailing space */
747 if (count)
748 count--;
749
750 count += sprintf(&buf[count], "\n");
751
752 return count;
753}
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500754
MyungJoo Ham9005b652011-10-02 00:19:28 +0200755static ssize_t show_freq(struct device *dev,
756 struct device_attribute *attr, char *buf)
757{
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200758 unsigned long freq;
759 struct devfreq *devfreq = to_devfreq(dev);
760
761 if (devfreq->profile->get_cur_freq &&
762 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
763 return sprintf(buf, "%lu\n", freq);
764
765 return sprintf(buf, "%lu\n", devfreq->previous_freq);
766}
767
768static ssize_t show_target_freq(struct device *dev,
769 struct device_attribute *attr, char *buf)
770{
MyungJoo Ham9005b652011-10-02 00:19:28 +0200771 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
772}
773
774static ssize_t show_polling_interval(struct device *dev,
775 struct device_attribute *attr, char *buf)
776{
777 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
778}
779
780static ssize_t store_polling_interval(struct device *dev,
781 struct device_attribute *attr,
782 const char *buf, size_t count)
783{
784 struct devfreq *df = to_devfreq(dev);
785 unsigned int value;
786 int ret;
787
Nishanth Menon1b5c1be2012-10-29 15:01:45 -0500788 if (!df->governor)
789 return -EINVAL;
790
MyungJoo Ham9005b652011-10-02 00:19:28 +0200791 ret = sscanf(buf, "%u", &value);
792 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +0200793 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +0200794
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200795 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +0200796 ret = count;
797
MyungJoo Ham9005b652011-10-02 00:19:28 +0200798 return ret;
799}
800
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900801static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr,
802 const char *buf, size_t count)
803{
804 struct devfreq *df = to_devfreq(dev);
805 unsigned long value;
806 int ret;
807 unsigned long max;
808
809 ret = sscanf(buf, "%lu", &value);
810 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +0200811 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900812
813 mutex_lock(&df->lock);
814 max = df->max_freq;
815 if (value && max && value > max) {
816 ret = -EINVAL;
817 goto unlock;
818 }
819
820 df->min_freq = value;
821 update_devfreq(df);
822 ret = count;
823unlock:
824 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900825 return ret;
826}
827
828static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr,
829 char *buf)
830{
831 return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
832}
833
834static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr,
835 const char *buf, size_t count)
836{
837 struct devfreq *df = to_devfreq(dev);
838 unsigned long value;
839 int ret;
840 unsigned long min;
841
842 ret = sscanf(buf, "%lu", &value);
843 if (ret != 1)
Nishanth Menon12e26262012-10-26 01:50:43 +0200844 return -EINVAL;
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900845
846 mutex_lock(&df->lock);
847 min = df->min_freq;
848 if (value && min && value < min) {
849 ret = -EINVAL;
850 goto unlock;
851 }
852
853 df->max_freq = value;
854 update_devfreq(df);
855 ret = count;
856unlock:
857 mutex_unlock(&df->lock);
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900858 return ret;
859}
860
861static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr,
862 char *buf)
863{
864 return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
865}
866
Nishanth Menond287de82012-10-25 19:48:59 -0500867static ssize_t show_available_freqs(struct device *d,
868 struct device_attribute *attr,
869 char *buf)
870{
871 struct devfreq *df = to_devfreq(d);
872 struct device *dev = df->dev.parent;
873 struct opp *opp;
874 ssize_t count = 0;
875 unsigned long freq = 0;
876
877 rcu_read_lock();
878 do {
879 opp = opp_find_freq_ceil(dev, &freq);
880 if (IS_ERR(opp))
881 break;
882
883 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
884 "%lu ", freq);
885 freq++;
886 } while (1);
887 rcu_read_unlock();
888
889 /* Truncate the trailing space */
890 if (count)
891 count--;
892
893 count += sprintf(&buf[count], "\n");
894
895 return count;
896}
897
Jonghwa Leee552bba2012-08-23 20:00:46 +0900898static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr,
899 char *buf)
900{
901 struct devfreq *devfreq = to_devfreq(dev);
902 ssize_t len;
903 int i, j, err;
904 unsigned int max_state = devfreq->profile->max_state;
905
906 err = devfreq_update_status(devfreq, devfreq->previous_freq);
907 if (err)
908 return 0;
909
910 len = sprintf(buf, " From : To\n");
911 len += sprintf(buf + len, " :");
912 for (i = 0; i < max_state; i++)
913 len += sprintf(buf + len, "%8u",
914 devfreq->profile->freq_table[i]);
915
916 len += sprintf(buf + len, " time(ms)\n");
917
918 for (i = 0; i < max_state; i++) {
919 if (devfreq->profile->freq_table[i]
920 == devfreq->previous_freq) {
921 len += sprintf(buf + len, "*");
922 } else {
923 len += sprintf(buf + len, " ");
924 }
925 len += sprintf(buf + len, "%8u:",
926 devfreq->profile->freq_table[i]);
927 for (j = 0; j < max_state; j++)
928 len += sprintf(buf + len, "%8u",
929 devfreq->trans_table[(i * max_state) + j]);
930 len += sprintf(buf + len, "%10u\n",
931 jiffies_to_msecs(devfreq->time_in_state[i]));
932 }
933
934 len += sprintf(buf + len, "Total transition : %u\n",
935 devfreq->total_trans);
936 return len;
937}
938
MyungJoo Ham9005b652011-10-02 00:19:28 +0200939static struct device_attribute devfreq_attrs[] = {
Nishanth Menon0359d1a2012-10-29 15:01:47 -0500940 __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor),
Nishanth Menon50a5b332012-10-29 15:01:48 -0500941 __ATTR(available_governors, S_IRUGO, show_available_governors, NULL),
MyungJoo Ham9005b652011-10-02 00:19:28 +0200942 __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
Nishanth Menond287de82012-10-25 19:48:59 -0500943 __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL),
Rajagopal Venkat7f98a902012-10-26 01:50:26 +0200944 __ATTR(target_freq, S_IRUGO, show_target_freq, NULL),
MyungJoo Ham9005b652011-10-02 00:19:28 +0200945 __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
946 store_polling_interval),
MyungJoo Ham6530b9de2011-12-09 16:42:19 +0900947 __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
948 __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
Jonghwa Leee552bba2012-08-23 20:00:46 +0900949 __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL),
MyungJoo Ham9005b652011-10-02 00:19:28 +0200950 { },
951};
952
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200953static int __init devfreq_init(void)
954{
955 devfreq_class = class_create(THIS_MODULE, "devfreq");
956 if (IS_ERR(devfreq_class)) {
957 pr_err("%s: couldn't create class\n", __FILE__);
958 return PTR_ERR(devfreq_class);
959 }
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200960
961 devfreq_wq = create_freezable_workqueue("devfreq_wq");
962 if (IS_ERR(devfreq_wq)) {
963 class_destroy(devfreq_class);
964 pr_err("%s: couldn't create workqueue\n", __FILE__);
965 return PTR_ERR(devfreq_wq);
966 }
MyungJoo Ham9005b652011-10-02 00:19:28 +0200967 devfreq_class->dev_attrs = devfreq_attrs;
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200968
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200969 return 0;
970}
971subsys_initcall(devfreq_init);
972
973static void __exit devfreq_exit(void)
974{
975 class_destroy(devfreq_class);
Rajagopal Venkat7e6fdd42012-10-26 01:50:09 +0200976 destroy_workqueue(devfreq_wq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200977}
978module_exit(devfreq_exit);
979
980/*
981 * The followings are helper functions for devfreq user device drivers with
982 * OPP framework.
983 */
984
985/**
986 * devfreq_recommended_opp() - Helper function to get proper OPP for the
987 * freq value given to target callback.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +0200988 * @dev: The devfreq user device. (parent of devfreq)
989 * @freq: The frequency given to target function
990 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200991 *
992 */
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100993struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
994 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200995{
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100996 struct opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200997
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100998 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
999 /* The freq is an upper bound. opp should be lower */
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001000 opp = opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001001
1002 /* If not available, use the closest opp */
1003 if (opp == ERR_PTR(-ENODEV))
1004 opp = opp_find_freq_ceil(dev, freq);
1005 } else {
1006 /* The freq is an lower bound. opp should be higher */
1007 opp = opp_find_freq_ceil(dev, freq);
1008
1009 /* If not available, use the closest opp */
1010 if (opp == ERR_PTR(-ENODEV))
1011 opp = opp_find_freq_floor(dev, freq);
1012 }
1013
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001014 return opp;
1015}
1016
1017/**
1018 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1019 * for any changes in the OPP availability
1020 * changes
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001021 * @dev: The devfreq user device. (parent of devfreq)
1022 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001023 */
1024int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1025{
1026 struct srcu_notifier_head *nh = opp_get_notifier(dev);
1027
1028 if (IS_ERR(nh))
1029 return PTR_ERR(nh);
1030 return srcu_notifier_chain_register(nh, &devfreq->nb);
1031}
1032
1033/**
1034 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1035 * notified for any changes in the OPP
1036 * availability changes anymore.
Nishanth Menonc5b4a1c12012-10-26 01:50:35 +02001037 * @dev: The devfreq user device. (parent of devfreq)
1038 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001039 *
1040 * At exit() callback of devfreq_dev_profile, this must be included if
1041 * devfreq_recommended_opp is used.
1042 */
1043int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1044{
1045 struct srcu_notifier_head *nh = opp_get_notifier(dev);
1046
1047 if (IS_ERR(nh))
1048 return PTR_ERR(nh);
1049 return srcu_notifier_chain_unregister(nh, &devfreq->nb);
1050}
1051
1052MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1053MODULE_DESCRIPTION("devfreq class support");
1054MODULE_LICENSE("GPL");