blob: dadf87c275c69a99bf12341edf9ab24bb88b113b [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 Menon177c4ef2012-10-26 01:50:53 +020030static struct class *devfreq_class;
MyungJoo Hama3c98b82011-10-02 00:19:15 +020031
32/*
Rajagopal Venkat97ad73e2012-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 Menon71502002012-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 Lee2468b282012-08-23 20:00:46 +090071/**
Rajagopal Venkat75d05ad2013-06-11 16:42:27 -060072 * devfreq_set_freq_limits() - Set min and max frequency from freq_table
73 * @devfreq: the devfreq instance
74 */
75static void devfreq_set_freq_limits(struct devfreq *devfreq)
76{
77 int idx;
78 unsigned long min = ~0, max = 0;
79
80 if (!devfreq->profile->freq_table)
81 return;
82
83 for (idx = 0; idx < devfreq->profile->max_state; idx++) {
84 if (min > devfreq->profile->freq_table[idx])
85 min = devfreq->profile->freq_table[idx];
86 if (max < devfreq->profile->freq_table[idx])
87 max = devfreq->profile->freq_table[idx];
88 }
89
90 devfreq->min_freq = min;
91 devfreq->max_freq = max;
92}
93
94/**
Jonghwa Lee2468b282012-08-23 20:00:46 +090095 * devfreq_get_freq_level() - Lookup freq_table for the frequency
96 * @devfreq: the devfreq instance
97 * @freq: the target frequency
98 */
Jeremy Gebbena59ce7e2013-03-18 14:14:16 -060099int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
Jonghwa Lee2468b282012-08-23 20:00:46 +0900100{
101 int lev;
102
103 for (lev = 0; lev < devfreq->profile->max_state; lev++)
104 if (freq == devfreq->profile->freq_table[lev])
105 return lev;
106
107 return -EINVAL;
108}
Jeremy Gebbena59ce7e2013-03-18 14:14:16 -0600109EXPORT_SYMBOL(devfreq_get_freq_level);
Jonghwa Lee2468b282012-08-23 20:00:46 +0900110
111/**
112 * devfreq_update_status() - Update statistics of devfreq behavior
113 * @devfreq: the devfreq instance
114 * @freq: the update target frequency
115 */
116static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
117{
118 int lev, prev_lev;
119 unsigned long cur_time;
120
121 lev = devfreq_get_freq_level(devfreq, freq);
122 if (lev < 0)
123 return lev;
124
125 cur_time = jiffies;
126 devfreq->time_in_state[lev] +=
127 cur_time - devfreq->last_stat_updated;
128 if (freq != devfreq->previous_freq) {
129 prev_lev = devfreq_get_freq_level(devfreq,
130 devfreq->previous_freq);
131 devfreq->trans_table[(prev_lev *
132 devfreq->profile->max_state) + lev]++;
133 devfreq->total_trans++;
134 }
135 devfreq->last_stat_updated = cur_time;
136
137 return 0;
138}
139
Nishanth Menon71502002012-10-29 15:01:43 -0500140/**
141 * find_devfreq_governor() - find devfreq governor from name
142 * @name: name of the governor
143 *
144 * Search the list of devfreq governors and return the matched
145 * governor's pointer. devfreq_list_lock should be held by the caller.
146 */
147static struct devfreq_governor *find_devfreq_governor(const char *name)
148{
149 struct devfreq_governor *tmp_governor;
150
151 if (unlikely(IS_ERR_OR_NULL(name))) {
152 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
153 return ERR_PTR(-EINVAL);
154 }
155 WARN(!mutex_is_locked(&devfreq_list_lock),
156 "devfreq_list_lock must be locked.");
157
158 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
159 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
160 return tmp_governor;
161 }
162
163 return ERR_PTR(-ENODEV);
164}
165
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200166/* Load monitoring helper functions for governors use */
167
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200168/**
169 * update_devfreq() - Reevaluate the device and configure frequency.
170 * @devfreq: the devfreq instance.
171 *
172 * Note: Lock devfreq->lock before calling update_devfreq
173 * This function is exported for governors.
174 */
175int update_devfreq(struct devfreq *devfreq)
176{
177 unsigned long freq;
178 int err = 0;
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100179 u32 flags = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200180
181 if (!mutex_is_locked(&devfreq->lock)) {
182 WARN(true, "devfreq->lock must be locked by the caller.\n");
183 return -EINVAL;
184 }
185
Nishanth Menona6134a42012-10-29 15:01:45 -0500186 if (!devfreq->governor)
187 return -EINVAL;
188
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200189 /* Reevaluate the proper frequency */
190 err = devfreq->governor->get_target_freq(devfreq, &freq);
191 if (err)
192 return err;
193
MyungJoo Hamab5f2992012-03-16 21:54:53 +0100194 /*
195 * Adjust the freuqency with user freq and QoS.
196 *
197 * List from the highest proiority
198 * max_freq (probably called by thermal when it's too hot)
199 * min_freq
200 */
201
202 if (devfreq->min_freq && freq < devfreq->min_freq) {
203 freq = devfreq->min_freq;
204 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
205 }
206 if (devfreq->max_freq && freq > devfreq->max_freq) {
207 freq = devfreq->max_freq;
208 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
209 }
210
211 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200212 if (err)
213 return err;
214
Jonghwa Lee2468b282012-08-23 20:00:46 +0900215 if (devfreq->profile->freq_table)
216 if (devfreq_update_status(devfreq, freq))
217 dev_err(&devfreq->dev,
218 "Couldn't update frequency transition information.\n");
219
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200220 devfreq->previous_freq = freq;
221 return err;
222}
Nishanth Menonc0679b42012-10-29 15:01:42 -0500223EXPORT_SYMBOL(update_devfreq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200224
225/**
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200226 * devfreq_monitor() - Periodically poll devfreq objects.
227 * @work: the work struct used to run devfreq_monitor periodically.
228 *
229 */
230static void devfreq_monitor(struct work_struct *work)
231{
232 int err;
233 struct devfreq *devfreq = container_of(work,
234 struct devfreq, work.work);
235
236 mutex_lock(&devfreq->lock);
237 err = update_devfreq(devfreq);
238 if (err)
239 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
240
241 queue_delayed_work(devfreq_wq, &devfreq->work,
242 msecs_to_jiffies(devfreq->profile->polling_ms));
243 mutex_unlock(&devfreq->lock);
244}
245
246/**
247 * devfreq_monitor_start() - Start load monitoring of devfreq instance
248 * @devfreq: the devfreq instance.
249 *
250 * Helper function for starting devfreq device load monitoing. By
251 * default delayed work based monitoring is supported. Function
252 * to be called from governor in response to DEVFREQ_GOV_START
253 * event when device is added to devfreq framework.
254 */
255void devfreq_monitor_start(struct devfreq *devfreq)
256{
257 INIT_DELAYED_WORK_DEFERRABLE(&devfreq->work, devfreq_monitor);
258 if (devfreq->profile->polling_ms)
259 queue_delayed_work(devfreq_wq, &devfreq->work,
260 msecs_to_jiffies(devfreq->profile->polling_ms));
261}
MyungJoo Hamce9dea12012-11-28 20:29:17 +0100262EXPORT_SYMBOL(devfreq_monitor_start);
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200263
264/**
265 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
266 * @devfreq: the devfreq instance.
267 *
268 * Helper function to stop devfreq device load monitoing. Function
269 * to be called from governor in response to DEVFREQ_GOV_STOP
270 * event when device is removed from devfreq framework.
271 */
272void devfreq_monitor_stop(struct devfreq *devfreq)
273{
274 cancel_delayed_work_sync(&devfreq->work);
275}
MyungJoo Hamce9dea12012-11-28 20:29:17 +0100276EXPORT_SYMBOL(devfreq_monitor_stop);
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200277
278/**
279 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
280 * @devfreq: the devfreq instance.
281 *
282 * Helper function to suspend devfreq device load monitoing. Function
283 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
284 * event or when polling interval is set to zero.
285 *
286 * Note: Though this function is same as devfreq_monitor_stop(),
287 * intentionally kept separate to provide hooks for collecting
288 * transition statistics.
289 */
290void devfreq_monitor_suspend(struct devfreq *devfreq)
291{
292 mutex_lock(&devfreq->lock);
293 if (devfreq->stop_polling) {
294 mutex_unlock(&devfreq->lock);
295 return;
296 }
297
298 devfreq->stop_polling = true;
299 mutex_unlock(&devfreq->lock);
300 cancel_delayed_work_sync(&devfreq->work);
301}
MyungJoo Hamce9dea12012-11-28 20:29:17 +0100302EXPORT_SYMBOL(devfreq_monitor_suspend);
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200303
304/**
305 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
306 * @devfreq: the devfreq instance.
307 *
308 * Helper function to resume devfreq device load monitoing. Function
309 * to be called from governor in response to DEVFREQ_GOV_RESUME
310 * event or when polling interval is set to non-zero.
311 */
312void devfreq_monitor_resume(struct devfreq *devfreq)
313{
314 mutex_lock(&devfreq->lock);
315 if (!devfreq->stop_polling)
316 goto out;
317
318 if (!delayed_work_pending(&devfreq->work) &&
319 devfreq->profile->polling_ms)
320 queue_delayed_work(devfreq_wq, &devfreq->work,
321 msecs_to_jiffies(devfreq->profile->polling_ms));
322 devfreq->stop_polling = false;
323
324out:
325 mutex_unlock(&devfreq->lock);
326}
MyungJoo Hamce9dea12012-11-28 20:29:17 +0100327EXPORT_SYMBOL(devfreq_monitor_resume);
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200328
329/**
330 * devfreq_interval_update() - Update device devfreq monitoring interval
331 * @devfreq: the devfreq instance.
332 * @delay: new polling interval to be set.
333 *
334 * Helper function to set new load monitoring polling interval. Function
335 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
336 */
337void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
338{
339 unsigned int cur_delay = devfreq->profile->polling_ms;
340 unsigned int new_delay = *delay;
341
342 mutex_lock(&devfreq->lock);
343 devfreq->profile->polling_ms = new_delay;
344
345 if (devfreq->stop_polling)
346 goto out;
347
348 /* if new delay is zero, stop polling */
349 if (!new_delay) {
350 mutex_unlock(&devfreq->lock);
351 cancel_delayed_work_sync(&devfreq->work);
352 return;
353 }
354
355 /* if current delay is zero, start polling with new delay */
356 if (!cur_delay) {
357 queue_delayed_work(devfreq_wq, &devfreq->work,
358 msecs_to_jiffies(devfreq->profile->polling_ms));
359 goto out;
360 }
361
362 /* if current delay is greater than new delay, restart polling */
363 if (cur_delay > new_delay) {
364 mutex_unlock(&devfreq->lock);
365 cancel_delayed_work_sync(&devfreq->work);
366 mutex_lock(&devfreq->lock);
367 if (!devfreq->stop_polling)
368 queue_delayed_work(devfreq_wq, &devfreq->work,
369 msecs_to_jiffies(devfreq->profile->polling_ms));
370 }
371out:
372 mutex_unlock(&devfreq->lock);
373}
MyungJoo Hamce9dea12012-11-28 20:29:17 +0100374EXPORT_SYMBOL(devfreq_interval_update);
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200375
376/**
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200377 * devfreq_notifier_call() - Notify that the device frequency requirements
378 * has been changed out of devfreq framework.
Nishanth Menon3b392b52012-10-26 01:50:35 +0200379 * @nb: the notifier_block (supposed to be devfreq->nb)
380 * @type: not used
381 * @devp: not used
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200382 *
383 * Called by a notifier that uses devfreq->nb.
384 */
385static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
386 void *devp)
387{
388 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
389 int ret;
390
391 mutex_lock(&devfreq->lock);
392 ret = update_devfreq(devfreq);
393 mutex_unlock(&devfreq->lock);
394
395 return ret;
396}
397
398/**
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200399 * _remove_devfreq() - Remove devfreq from the list and release its resources.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200400 * @devfreq: the devfreq struct
401 * @skip: skip calling device_unregister().
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200402 */
403static void _remove_devfreq(struct devfreq *devfreq, bool skip)
404{
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200405 mutex_lock(&devfreq_list_lock);
406 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
407 mutex_unlock(&devfreq_list_lock);
408 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200409 return;
410 }
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200411 list_del(&devfreq->node);
412 mutex_unlock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200413
Nishanth Menona6134a42012-10-29 15:01:45 -0500414 if (devfreq->governor)
415 devfreq->governor->event_handler(devfreq,
416 DEVFREQ_GOV_STOP, NULL);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200417
418 if (devfreq->profile->exit)
419 devfreq->profile->exit(devfreq->dev.parent);
420
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200421 if (!skip && get_device(&devfreq->dev)) {
422 device_unregister(&devfreq->dev);
423 put_device(&devfreq->dev);
424 }
425
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200426 mutex_destroy(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200427 kfree(devfreq);
428}
429
430/**
431 * devfreq_dev_release() - Callback for struct device to release the device.
432 * @dev: the devfreq device
433 *
434 * This calls _remove_devfreq() if _remove_devfreq() is not called.
435 * Note that devfreq_dev_release() could be called by _remove_devfreq() as
436 * well as by others unregistering the device.
437 */
438static void devfreq_dev_release(struct device *dev)
439{
440 struct devfreq *devfreq = to_devfreq(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200441
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200442 _remove_devfreq(devfreq, true);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200443}
444
445/**
Jeremy Gebbend24922d2013-03-14 11:30:39 -0600446 * find_governor_data - Find device specific private data for a governor.
447 * @profile: The profile to search.
448 * @governor_name: The governor to search for.
449 *
450 * Look up the device specific data for a governor.
451 */
452static void *find_governor_data(struct devfreq_dev_profile *profile,
453 const char *governor_name)
454{
455 void *data = NULL;
456 int i;
457
458 if (profile->governor_data == NULL)
459 return NULL;
460
461 for (i = 0; i < profile->num_governor_data; i++) {
462 if (!strncmp(governor_name, profile->governor_data[i].name,
463 DEVFREQ_NAME_LEN) == 0) {
464 data = profile->governor_data[i].data;
465 break;
466 }
467 }
468 return data;
469}
470
471/**
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200472 * devfreq_add_device() - Add devfreq feature to the device
473 * @dev: the device to add devfreq feature.
474 * @profile: device-specific profile to run devfreq.
Nishanth Menona6134a42012-10-29 15:01:45 -0500475 * @governor_name: name of the policy to choose frequency.
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200476 * @data: private data for the governor. The devfreq framework does not
477 * touch this value.
478 */
479struct devfreq *devfreq_add_device(struct device *dev,
480 struct devfreq_dev_profile *profile,
Nishanth Menona6134a42012-10-29 15:01:45 -0500481 const char *governor_name,
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200482 void *data)
483{
484 struct devfreq *devfreq;
Nishanth Menona6134a42012-10-29 15:01:45 -0500485 struct devfreq_governor *governor;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200486 int err = 0;
487
Nishanth Menona6134a42012-10-29 15:01:45 -0500488 if (!dev || !profile || !governor_name) {
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200489 dev_err(dev, "%s: Invalid parameters.\n", __func__);
490 return ERR_PTR(-EINVAL);
491 }
492
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200493 mutex_lock(&devfreq_list_lock);
494 devfreq = find_device_devfreq(dev);
495 mutex_unlock(&devfreq_list_lock);
496 if (!IS_ERR(devfreq)) {
497 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
498 err = -EINVAL;
499 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200500 }
501
502 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
503 if (!devfreq) {
504 dev_err(dev, "%s: Unable to create devfreq for the device\n",
505 __func__);
506 err = -ENOMEM;
Axel Lin3f19f082011-11-15 21:59:09 +0100507 goto err_out;
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200508 }
509
510 mutex_init(&devfreq->lock);
511 mutex_lock(&devfreq->lock);
512 devfreq->dev.parent = dev;
513 devfreq->dev.class = devfreq_class;
514 devfreq->dev.release = devfreq_dev_release;
515 devfreq->profile = profile;
Nishanth Menona6134a42012-10-29 15:01:45 -0500516 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200517 devfreq->previous_freq = profile->initial_freq;
Jeremy Gebbend24922d2013-03-14 11:30:39 -0600518
519 devfreq->data = data ? data : find_governor_data(devfreq->profile,
520 governor_name);
521
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200522 devfreq->nb.notifier_call = devfreq_notifier_call;
523
Jonghwa Lee2468b282012-08-23 20:00:46 +0900524 devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
525 devfreq->profile->max_state *
526 devfreq->profile->max_state,
527 GFP_KERNEL);
528 devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) *
529 devfreq->profile->max_state,
530 GFP_KERNEL);
531 devfreq->last_stat_updated = jiffies;
Rajagopal Venkat75d05ad2013-06-11 16:42:27 -0600532 devfreq_set_freq_limits(devfreq);
Jonghwa Lee2468b282012-08-23 20:00:46 +0900533
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200534 dev_set_name(&devfreq->dev, dev_name(dev));
535 err = device_register(&devfreq->dev);
536 if (err) {
537 put_device(&devfreq->dev);
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200538 mutex_unlock(&devfreq->lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200539 goto err_dev;
540 }
541
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200542 mutex_unlock(&devfreq->lock);
543
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200544 mutex_lock(&devfreq_list_lock);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200545 list_add(&devfreq->node, &devfreq_list);
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200546
Nishanth Menona6134a42012-10-29 15:01:45 -0500547 governor = find_devfreq_governor(devfreq->governor_name);
548 if (!IS_ERR(governor))
549 devfreq->governor = governor;
550 if (devfreq->governor)
551 err = devfreq->governor->event_handler(devfreq,
552 DEVFREQ_GOV_START, NULL);
553 mutex_unlock(&devfreq_list_lock);
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200554 if (err) {
555 dev_err(dev, "%s: Unable to start governor for the device\n",
556 __func__);
557 goto err_init;
558 }
559
Axel Lin3f19f082011-11-15 21:59:09 +0100560 return devfreq;
561
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200562err_init:
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200563 list_del(&devfreq->node);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200564 device_unregister(&devfreq->dev);
565err_dev:
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200566 kfree(devfreq);
Axel Lin3f19f082011-11-15 21:59:09 +0100567err_out:
568 return ERR_PTR(err);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200569}
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200570EXPORT_SYMBOL(devfreq_add_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200571
572/**
573 * devfreq_remove_device() - Remove devfreq feature from a device.
Nishanth Menon3b392b52012-10-26 01:50:35 +0200574 * @devfreq: the devfreq instance to be removed
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200575 */
576int devfreq_remove_device(struct devfreq *devfreq)
577{
578 if (!devfreq)
579 return -EINVAL;
580
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200581 _remove_devfreq(devfreq, false);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200582
583 return 0;
584}
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200585EXPORT_SYMBOL(devfreq_remove_device);
MyungJoo Hama3c98b82011-10-02 00:19:15 +0200586
Rajagopal Venkat6efab212012-10-26 01:50:18 +0200587/**
588 * devfreq_suspend_device() - Suspend devfreq of a device.
589 * @devfreq: the devfreq instance to be suspended
590 */
591int devfreq_suspend_device(struct devfreq *devfreq)
592{
593 if (!devfreq)
594 return -EINVAL;
595
Nishanth Menona6134a42012-10-29 15:01:45 -0500596 if (!devfreq->governor)
597 return 0;
598
Rajagopal Venkat6efab212012-10-26 01:50:18 +0200599 return devfreq->governor->event_handler(devfreq,
600 DEVFREQ_GOV_SUSPEND, NULL);
601}
602EXPORT_SYMBOL(devfreq_suspend_device);
603
604/**
605 * devfreq_resume_device() - Resume devfreq of a device.
606 * @devfreq: the devfreq instance to be resumed
607 */
608int devfreq_resume_device(struct devfreq *devfreq)
609{
610 if (!devfreq)
611 return -EINVAL;
612
Nishanth Menona6134a42012-10-29 15:01:45 -0500613 if (!devfreq->governor)
614 return 0;
615
Rajagopal Venkat6efab212012-10-26 01:50:18 +0200616 return devfreq->governor->event_handler(devfreq,
617 DEVFREQ_GOV_RESUME, NULL);
618}
619EXPORT_SYMBOL(devfreq_resume_device);
620
Nishanth Menon71502002012-10-29 15:01:43 -0500621/**
622 * devfreq_add_governor() - Add devfreq governor
623 * @governor: the devfreq governor to be added
624 */
625int devfreq_add_governor(struct devfreq_governor *governor)
626{
627 struct devfreq_governor *g;
Nishanth Menona6134a42012-10-29 15:01:45 -0500628 struct devfreq *devfreq;
Nishanth Menon71502002012-10-29 15:01:43 -0500629 int err = 0;
630
631 if (!governor) {
632 pr_err("%s: Invalid parameters.\n", __func__);
633 return -EINVAL;
634 }
635
636 mutex_lock(&devfreq_list_lock);
637 g = find_devfreq_governor(governor->name);
638 if (!IS_ERR(g)) {
639 pr_err("%s: governor %s already registered\n", __func__,
640 g->name);
641 err = -EINVAL;
642 goto err_out;
643 }
644
645 list_add(&governor->node, &devfreq_governor_list);
646
Nishanth Menona6134a42012-10-29 15:01:45 -0500647 list_for_each_entry(devfreq, &devfreq_list, node) {
648 int ret = 0;
649 struct device *dev = devfreq->dev.parent;
650
651 if (!strncmp(devfreq->governor_name, governor->name,
652 DEVFREQ_NAME_LEN)) {
653 /* The following should never occur */
654 if (devfreq->governor) {
655 dev_warn(dev,
656 "%s: Governor %s already present\n",
657 __func__, devfreq->governor->name);
658 ret = devfreq->governor->event_handler(devfreq,
659 DEVFREQ_GOV_STOP, NULL);
660 if (ret) {
661 dev_warn(dev,
662 "%s: Governor %s stop = %d\n",
663 __func__,
664 devfreq->governor->name, ret);
665 }
666 /* Fall through */
667 }
668 devfreq->governor = governor;
669 ret = devfreq->governor->event_handler(devfreq,
670 DEVFREQ_GOV_START, NULL);
671 if (ret) {
672 dev_warn(dev, "%s: Governor %s start=%d\n",
673 __func__, devfreq->governor->name,
674 ret);
675 }
676 }
677 }
678
Nishanth Menon71502002012-10-29 15:01:43 -0500679err_out:
680 mutex_unlock(&devfreq_list_lock);
681
682 return err;
683}
684EXPORT_SYMBOL(devfreq_add_governor);
685
686/**
687 * devfreq_remove_device() - Remove devfreq feature from a device.
688 * @governor: the devfreq governor to be removed
689 */
690int devfreq_remove_governor(struct devfreq_governor *governor)
691{
692 struct devfreq_governor *g;
Nishanth Menona6134a42012-10-29 15:01:45 -0500693 struct devfreq *devfreq;
Nishanth Menon71502002012-10-29 15:01:43 -0500694 int err = 0;
695
696 if (!governor) {
697 pr_err("%s: Invalid parameters.\n", __func__);
698 return -EINVAL;
699 }
700
701 mutex_lock(&devfreq_list_lock);
702 g = find_devfreq_governor(governor->name);
703 if (IS_ERR(g)) {
704 pr_err("%s: governor %s not registered\n", __func__,
Sachin Kamat9edac512012-11-21 10:36:13 +0530705 governor->name);
Sachin Kamat7dd4c6a2012-11-21 10:36:14 +0530706 err = PTR_ERR(g);
Nishanth Menon71502002012-10-29 15:01:43 -0500707 goto err_out;
708 }
Nishanth Menona6134a42012-10-29 15:01:45 -0500709 list_for_each_entry(devfreq, &devfreq_list, node) {
710 int ret;
711 struct device *dev = devfreq->dev.parent;
712
713 if (!strncmp(devfreq->governor_name, governor->name,
714 DEVFREQ_NAME_LEN)) {
715 /* we should have a devfreq governor! */
716 if (!devfreq->governor) {
717 dev_warn(dev, "%s: Governor %s NOT present\n",
718 __func__, governor->name);
719 continue;
720 /* Fall through */
721 }
722 ret = devfreq->governor->event_handler(devfreq,
723 DEVFREQ_GOV_STOP, NULL);
724 if (ret) {
725 dev_warn(dev, "%s: Governor %s stop=%d\n",
726 __func__, devfreq->governor->name,
727 ret);
728 }
729 devfreq->governor = NULL;
730 }
731 }
Nishanth Menon71502002012-10-29 15:01:43 -0500732
733 list_del(&governor->node);
734err_out:
735 mutex_unlock(&devfreq_list_lock);
736
737 return err;
738}
739EXPORT_SYMBOL(devfreq_remove_governor);
740
MyungJoo Ham9005b652011-10-02 00:19:28 +0200741static ssize_t show_governor(struct device *dev,
742 struct device_attribute *attr, char *buf)
743{
Nishanth Menona6134a42012-10-29 15:01:45 -0500744 if (!to_devfreq(dev)->governor)
745 return -EINVAL;
746
MyungJoo Ham9005b652011-10-02 00:19:28 +0200747 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
748}
749
Nishanth Menon4dcb6f92012-10-29 15:01:47 -0500750static ssize_t store_governor(struct device *dev, struct device_attribute *attr,
751 const char *buf, size_t count)
752{
753 struct devfreq *df = to_devfreq(dev);
754 int ret;
755 char str_governor[DEVFREQ_NAME_LEN + 1];
756 struct devfreq_governor *governor;
757
758 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
759 if (ret != 1)
760 return -EINVAL;
761
762 mutex_lock(&devfreq_list_lock);
763 governor = find_devfreq_governor(str_governor);
764 if (IS_ERR(governor)) {
765 ret = PTR_ERR(governor);
766 goto out;
767 }
768 if (df->governor == governor)
769 goto out;
770
771 if (df->governor) {
772 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
773 if (ret) {
774 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
775 __func__, df->governor->name, ret);
776 goto out;
777 }
778 }
Jeremy Gebbend24922d2013-03-14 11:30:39 -0600779 df->data = find_governor_data(df->profile, str_governor);
Nishanth Menon4dcb6f92012-10-29 15:01:47 -0500780 df->governor = governor;
781 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
782 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
783 if (ret)
784 dev_warn(dev, "%s: Governor %s not started(%d)\n",
785 __func__, df->governor->name, ret);
786out:
787 mutex_unlock(&devfreq_list_lock);
788
789 if (!ret)
790 ret = count;
791 return ret;
792}
Nishanth Menon708c2372012-10-29 15:01:48 -0500793static ssize_t show_available_governors(struct device *d,
794 struct device_attribute *attr,
795 char *buf)
796{
797 struct devfreq_governor *tmp_governor;
798 ssize_t count = 0;
799
800 mutex_lock(&devfreq_list_lock);
801 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
802 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
803 "%s ", tmp_governor->name);
804 mutex_unlock(&devfreq_list_lock);
805
806 /* Truncate the trailing space */
807 if (count)
808 count--;
809
810 count += sprintf(&buf[count], "\n");
811
812 return count;
813}
Nishanth Menon4dcb6f92012-10-29 15:01:47 -0500814
MyungJoo Ham9005b652011-10-02 00:19:28 +0200815static ssize_t show_freq(struct device *dev,
816 struct device_attribute *attr, char *buf)
817{
Rajagopal Venkat37926742012-10-26 01:50:26 +0200818 unsigned long freq;
819 struct devfreq *devfreq = to_devfreq(dev);
820
821 if (devfreq->profile->get_cur_freq &&
822 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
823 return sprintf(buf, "%lu\n", freq);
824
825 return sprintf(buf, "%lu\n", devfreq->previous_freq);
826}
827
828static ssize_t show_target_freq(struct device *dev,
829 struct device_attribute *attr, char *buf)
830{
MyungJoo Ham9005b652011-10-02 00:19:28 +0200831 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
832}
833
834static ssize_t show_polling_interval(struct device *dev,
835 struct device_attribute *attr, char *buf)
836{
837 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
838}
839
840static ssize_t store_polling_interval(struct device *dev,
841 struct device_attribute *attr,
842 const char *buf, size_t count)
843{
844 struct devfreq *df = to_devfreq(dev);
845 unsigned int value;
846 int ret;
847
Nishanth Menona6134a42012-10-29 15:01:45 -0500848 if (!df->governor)
849 return -EINVAL;
850
MyungJoo Ham9005b652011-10-02 00:19:28 +0200851 ret = sscanf(buf, "%u", &value);
852 if (ret != 1)
Nishanth Menonda059042012-10-26 01:50:43 +0200853 return -EINVAL;
MyungJoo Ham9005b652011-10-02 00:19:28 +0200854
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +0200855 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
MyungJoo Ham9005b652011-10-02 00:19:28 +0200856 ret = count;
857
MyungJoo Ham9005b652011-10-02 00:19:28 +0200858 return ret;
859}
860
MyungJoo Ham6530b9d2011-12-09 16:42:19 +0900861static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr,
862 const char *buf, size_t count)
863{
864 struct devfreq *df = to_devfreq(dev);
865 unsigned long value;
866 int ret;
867 unsigned long max;
868
869 ret = sscanf(buf, "%lu", &value);
870 if (ret != 1)
Nishanth Menonda059042012-10-26 01:50:43 +0200871 return -EINVAL;
MyungJoo Ham6530b9d2011-12-09 16:42:19 +0900872
873 mutex_lock(&df->lock);
874 max = df->max_freq;
875 if (value && max && value > max) {
876 ret = -EINVAL;
877 goto unlock;
878 }
879
880 df->min_freq = value;
881 update_devfreq(df);
882 ret = count;
883unlock:
884 mutex_unlock(&df->lock);
MyungJoo Ham6530b9d2011-12-09 16:42:19 +0900885 return ret;
886}
887
888static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr,
889 char *buf)
890{
891 return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
892}
893
894static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr,
895 const char *buf, size_t count)
896{
897 struct devfreq *df = to_devfreq(dev);
898 unsigned long value;
899 int ret;
900 unsigned long min;
901
902 ret = sscanf(buf, "%lu", &value);
903 if (ret != 1)
Nishanth Menonda059042012-10-26 01:50:43 +0200904 return -EINVAL;
MyungJoo Ham6530b9d2011-12-09 16:42:19 +0900905
906 mutex_lock(&df->lock);
907 min = df->min_freq;
908 if (value && min && value < min) {
909 ret = -EINVAL;
910 goto unlock;
911 }
912
913 df->max_freq = value;
914 update_devfreq(df);
915 ret = count;
916unlock:
917 mutex_unlock(&df->lock);
MyungJoo Ham6530b9d2011-12-09 16:42:19 +0900918 return ret;
919}
920
921static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr,
922 char *buf)
923{
924 return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
925}
926
Nishanth Menone1ab0612012-10-25 19:48:59 -0500927static ssize_t show_available_freqs(struct device *d,
928 struct device_attribute *attr,
929 char *buf)
930{
931 struct devfreq *df = to_devfreq(d);
932 struct device *dev = df->dev.parent;
933 struct opp *opp;
934 ssize_t count = 0;
935 unsigned long freq = 0;
936
937 rcu_read_lock();
938 do {
939 opp = opp_find_freq_ceil(dev, &freq);
940 if (IS_ERR(opp))
941 break;
942
943 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
944 "%lu ", freq);
945 freq++;
946 } while (1);
947 rcu_read_unlock();
948
949 /* Truncate the trailing space */
950 if (count)
951 count--;
952
953 count += sprintf(&buf[count], "\n");
954
955 return count;
956}
957
Jonghwa Lee2468b282012-08-23 20:00:46 +0900958static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr,
959 char *buf)
960{
961 struct devfreq *devfreq = to_devfreq(dev);
962 ssize_t len;
963 int i, j, err;
964 unsigned int max_state = devfreq->profile->max_state;
965
966 err = devfreq_update_status(devfreq, devfreq->previous_freq);
967 if (err)
968 return 0;
969
970 len = sprintf(buf, " From : To\n");
971 len += sprintf(buf + len, " :");
972 for (i = 0; i < max_state; i++)
973 len += sprintf(buf + len, "%8u",
974 devfreq->profile->freq_table[i]);
975
976 len += sprintf(buf + len, " time(ms)\n");
977
978 for (i = 0; i < max_state; i++) {
979 if (devfreq->profile->freq_table[i]
980 == devfreq->previous_freq) {
981 len += sprintf(buf + len, "*");
982 } else {
983 len += sprintf(buf + len, " ");
984 }
985 len += sprintf(buf + len, "%8u:",
986 devfreq->profile->freq_table[i]);
987 for (j = 0; j < max_state; j++)
988 len += sprintf(buf + len, "%8u",
989 devfreq->trans_table[(i * max_state) + j]);
990 len += sprintf(buf + len, "%10u\n",
991 jiffies_to_msecs(devfreq->time_in_state[i]));
992 }
993
994 len += sprintf(buf + len, "Total transition : %u\n",
995 devfreq->total_trans);
996 return len;
997}
998
MyungJoo Ham9005b652011-10-02 00:19:28 +0200999static struct device_attribute devfreq_attrs[] = {
Nishanth Menon4dcb6f92012-10-29 15:01:47 -05001000 __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor),
Nishanth Menon708c2372012-10-29 15:01:48 -05001001 __ATTR(available_governors, S_IRUGO, show_available_governors, NULL),
MyungJoo Ham9005b652011-10-02 00:19:28 +02001002 __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
Nishanth Menone1ab0612012-10-25 19:48:59 -05001003 __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL),
Rajagopal Venkat37926742012-10-26 01:50:26 +02001004 __ATTR(target_freq, S_IRUGO, show_target_freq, NULL),
MyungJoo Ham9005b652011-10-02 00:19:28 +02001005 __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
1006 store_polling_interval),
MyungJoo Ham6530b9d2011-12-09 16:42:19 +09001007 __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
1008 __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
Jonghwa Lee2468b282012-08-23 20:00:46 +09001009 __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL),
MyungJoo Ham9005b652011-10-02 00:19:28 +02001010 { },
1011};
1012
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001013static int __init devfreq_init(void)
1014{
1015 devfreq_class = class_create(THIS_MODULE, "devfreq");
1016 if (IS_ERR(devfreq_class)) {
1017 pr_err("%s: couldn't create class\n", __FILE__);
1018 return PTR_ERR(devfreq_class);
1019 }
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +02001020
1021 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1022 if (IS_ERR(devfreq_wq)) {
1023 class_destroy(devfreq_class);
1024 pr_err("%s: couldn't create workqueue\n", __FILE__);
1025 return PTR_ERR(devfreq_wq);
1026 }
MyungJoo Ham9005b652011-10-02 00:19:28 +02001027 devfreq_class->dev_attrs = devfreq_attrs;
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +02001028
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001029 return 0;
1030}
1031subsys_initcall(devfreq_init);
1032
1033static void __exit devfreq_exit(void)
1034{
1035 class_destroy(devfreq_class);
Rajagopal Venkat97ad73e2012-10-26 01:50:09 +02001036 destroy_workqueue(devfreq_wq);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001037}
1038module_exit(devfreq_exit);
1039
1040/*
1041 * The followings are helper functions for devfreq user device drivers with
1042 * OPP framework.
1043 */
1044
1045/**
1046 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1047 * freq value given to target callback.
Nishanth Menon3b392b52012-10-26 01:50:35 +02001048 * @dev: The devfreq user device. (parent of devfreq)
1049 * @freq: The frequency given to target function
1050 * @flags: Flags handed from devfreq framework.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001051 *
Nishanth Menonda4ddd02013-01-18 19:52:34 +00001052 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1053 * protected pointer. The reason for the same is that the opp pointer which is
1054 * returned will remain valid for use with opp_get_{voltage, freq} only while
1055 * under the locked area. The pointer returned must be used prior to unlocking
1056 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001057 */
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001058struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
1059 u32 flags)
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001060{
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001061 struct opp *opp;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001062
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001063 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1064 /* The freq is an upper bound. opp should be lower */
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001065 opp = opp_find_freq_floor(dev, freq);
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001066
1067 /* If not available, use the closest opp */
Nishanth Menonc36e1372012-10-24 22:00:12 +02001068 if (opp == ERR_PTR(-ERANGE))
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001069 opp = opp_find_freq_ceil(dev, freq);
1070 } else {
1071 /* The freq is an lower bound. opp should be higher */
1072 opp = opp_find_freq_ceil(dev, freq);
1073
1074 /* If not available, use the closest opp */
Nishanth Menonc36e1372012-10-24 22:00:12 +02001075 if (opp == ERR_PTR(-ERANGE))
MyungJoo Hamab5f2992012-03-16 21:54:53 +01001076 opp = opp_find_freq_floor(dev, freq);
1077 }
1078
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001079 return opp;
1080}
1081
1082/**
1083 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1084 * for any changes in the OPP availability
1085 * changes
Nishanth Menon3b392b52012-10-26 01:50:35 +02001086 * @dev: The devfreq user device. (parent of devfreq)
1087 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001088 */
1089int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1090{
MyungJoo Ham33659722012-11-21 19:04:51 +09001091 struct srcu_notifier_head *nh;
1092 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001093
MyungJoo Ham33659722012-11-21 19:04:51 +09001094 rcu_read_lock();
1095 nh = opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001096 if (IS_ERR(nh))
MyungJoo Ham33659722012-11-21 19:04:51 +09001097 ret = PTR_ERR(nh);
1098 rcu_read_unlock();
1099 if (!ret)
1100 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1101
1102 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001103}
1104
1105/**
1106 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1107 * notified for any changes in the OPP
1108 * availability changes anymore.
Nishanth Menon3b392b52012-10-26 01:50:35 +02001109 * @dev: The devfreq user device. (parent of devfreq)
1110 * @devfreq: The devfreq object.
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001111 *
1112 * At exit() callback of devfreq_dev_profile, this must be included if
1113 * devfreq_recommended_opp is used.
1114 */
1115int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1116{
MyungJoo Ham33659722012-11-21 19:04:51 +09001117 struct srcu_notifier_head *nh;
1118 int ret = 0;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001119
MyungJoo Ham33659722012-11-21 19:04:51 +09001120 rcu_read_lock();
1121 nh = opp_get_notifier(dev);
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001122 if (IS_ERR(nh))
MyungJoo Ham33659722012-11-21 19:04:51 +09001123 ret = PTR_ERR(nh);
1124 rcu_read_unlock();
1125 if (!ret)
1126 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1127
1128 return ret;
MyungJoo Hama3c98b82011-10-02 00:19:15 +02001129}
1130
1131MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1132MODULE_DESCRIPTION("devfreq class support");
1133MODULE_LICENSE("GPL");