battery: sec_battery: export {CURRENT/VOLTAGE}_MAX to sysfs
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / drivers / devfreq / devfreq.c
CommitLineData
a3c98b8b
MH
1/*
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>
952f6d13 18#include <linux/module.h>
a3c98b8b 19#include <linux/slab.h>
952f6d13 20#include <linux/stat.h>
a3c98b8b
MH
21#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
1a1357ea 30static struct class *devfreq_class;
a3c98b8b
MH
31
32/*
7e6fdd4b
RV
33 * devfreq core provides delayed work based load monitoring helper
34 * functions. Governors can use these or can implement their own
35 * monitoring mechanism.
a3c98b8b 36 */
a3c98b8b 37static struct workqueue_struct *devfreq_wq;
a3c98b8b 38
3aa173b8
NM
39/* The list of all device-devfreq governors */
40static LIST_HEAD(devfreq_governor_list);
a3c98b8b
MH
41/* 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
e552bbaf
JL
71/**
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{
3c2a0909 94 int lev, prev_lev, ret = 0;
e552bbaf
JL
95 unsigned long cur_time;
96
e552bbaf 97 cur_time = jiffies;
3c2a0909
S
98
99 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
100 if (prev_lev < 0) {
101 pr_err("DEVFREQ: invalid index to update status\n");
102 ret = prev_lev;
103 goto out;
104 }
105
106 devfreq->time_in_state[prev_lev] +=
e552bbaf 107 cur_time - devfreq->last_stat_updated;
3c2a0909
S
108
109 lev = devfreq_get_freq_level(devfreq, freq);
110 if (lev < 0) {
111 ret = lev;
112 goto out;
113 }
114
115 if (lev != prev_lev) {
e552bbaf
JL
116 devfreq->trans_table[(prev_lev *
117 devfreq->profile->max_state) + lev]++;
118 devfreq->total_trans++;
119 }
e552bbaf 120
3c2a0909
S
121out:
122 devfreq->last_stat_updated = cur_time;
123 return ret;
e552bbaf
JL
124}
125
3aa173b8
NM
126/**
127 * find_devfreq_governor() - find devfreq governor from name
128 * @name: name of the governor
129 *
130 * Search the list of devfreq governors and return the matched
131 * governor's pointer. devfreq_list_lock should be held by the caller.
132 */
133static struct devfreq_governor *find_devfreq_governor(const char *name)
134{
135 struct devfreq_governor *tmp_governor;
136
137 if (unlikely(IS_ERR_OR_NULL(name))) {
138 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
139 return ERR_PTR(-EINVAL);
140 }
141 WARN(!mutex_is_locked(&devfreq_list_lock),
142 "devfreq_list_lock must be locked.");
143
144 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
145 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
146 return tmp_governor;
147 }
148
149 return ERR_PTR(-ENODEV);
150}
151
7e6fdd4b
RV
152/* Load monitoring helper functions for governors use */
153
a3c98b8b
MH
154/**
155 * update_devfreq() - Reevaluate the device and configure frequency.
156 * @devfreq: the devfreq instance.
157 *
158 * Note: Lock devfreq->lock before calling update_devfreq
159 * This function is exported for governors.
160 */
161int update_devfreq(struct devfreq *devfreq)
162{
163 unsigned long freq;
164 int err = 0;
ab5f299f 165 u32 flags = 0;
a3c98b8b
MH
166
167 if (!mutex_is_locked(&devfreq->lock)) {
168 WARN(true, "devfreq->lock must be locked by the caller.\n");
169 return -EINVAL;
170 }
171
1b5c1be2
NM
172 if (!devfreq->governor)
173 return -EINVAL;
174
a3c98b8b
MH
175 /* Reevaluate the proper frequency */
176 err = devfreq->governor->get_target_freq(devfreq, &freq);
177 if (err)
178 return err;
179
ab5f299f
MH
180 /*
181 * Adjust the freuqency with user freq and QoS.
182 *
183 * List from the highest proiority
184 * max_freq (probably called by thermal when it's too hot)
185 * min_freq
186 */
187
188 if (devfreq->min_freq && freq < devfreq->min_freq) {
189 freq = devfreq->min_freq;
190 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
191 }
192 if (devfreq->max_freq && freq > devfreq->max_freq) {
193 freq = devfreq->max_freq;
194 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
195 }
196
197 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
a3c98b8b
MH
198 if (err)
199 return err;
200
e552bbaf
JL
201 if (devfreq->profile->freq_table)
202 if (devfreq_update_status(devfreq, freq))
203 dev_err(&devfreq->dev,
204 "Couldn't update frequency transition information.\n");
205
a3c98b8b
MH
206 devfreq->previous_freq = freq;
207 return err;
208}
2df5021f 209EXPORT_SYMBOL(update_devfreq);
a3c98b8b 210
7e6fdd4b
RV
211/**
212 * devfreq_monitor() - Periodically poll devfreq objects.
213 * @work: the work struct used to run devfreq_monitor periodically.
214 *
215 */
216static void devfreq_monitor(struct work_struct *work)
217{
218 int err;
219 struct devfreq *devfreq = container_of(work,
220 struct devfreq, work.work);
221
222 mutex_lock(&devfreq->lock);
223 err = update_devfreq(devfreq);
3c2a0909 224 if (err && err != -EAGAIN)
7e6fdd4b 225 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
3c2a0909
S
226#ifdef CONFIG_SCHED_HMP
227 mod_delayed_work_on(0, devfreq_wq, &devfreq->work,
228 msecs_to_jiffies(devfreq->profile->polling_ms));
229#else
7e6fdd4b
RV
230 queue_delayed_work(devfreq_wq, &devfreq->work,
231 msecs_to_jiffies(devfreq->profile->polling_ms));
3c2a0909 232#endif
7e6fdd4b
RV
233 mutex_unlock(&devfreq->lock);
234}
235
236/**
237 * devfreq_monitor_start() - Start load monitoring of devfreq instance
238 * @devfreq: the devfreq instance.
239 *
240 * Helper function for starting devfreq device load monitoing. By
241 * default delayed work based monitoring is supported. Function
242 * to be called from governor in response to DEVFREQ_GOV_START
243 * event when device is added to devfreq framework.
244 */
245void devfreq_monitor_start(struct devfreq *devfreq)
246{
3c2a0909 247 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
7e6fdd4b
RV
248 if (devfreq->profile->polling_ms)
249 queue_delayed_work(devfreq_wq, &devfreq->work,
250 msecs_to_jiffies(devfreq->profile->polling_ms));
251}
6dcdd8e3 252EXPORT_SYMBOL(devfreq_monitor_start);
7e6fdd4b
RV
253
254/**
255 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
256 * @devfreq: the devfreq instance.
257 *
258 * Helper function to stop devfreq device load monitoing. Function
259 * to be called from governor in response to DEVFREQ_GOV_STOP
260 * event when device is removed from devfreq framework.
261 */
262void devfreq_monitor_stop(struct devfreq *devfreq)
263{
264 cancel_delayed_work_sync(&devfreq->work);
265}
6dcdd8e3 266EXPORT_SYMBOL(devfreq_monitor_stop);
7e6fdd4b
RV
267
268/**
269 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
270 * @devfreq: the devfreq instance.
271 *
272 * Helper function to suspend devfreq device load monitoing. Function
273 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
274 * event or when polling interval is set to zero.
275 *
276 * Note: Though this function is same as devfreq_monitor_stop(),
277 * intentionally kept separate to provide hooks for collecting
278 * transition statistics.
279 */
280void devfreq_monitor_suspend(struct devfreq *devfreq)
281{
282 mutex_lock(&devfreq->lock);
283 if (devfreq->stop_polling) {
284 mutex_unlock(&devfreq->lock);
285 return;
286 }
287
288 devfreq->stop_polling = true;
289 mutex_unlock(&devfreq->lock);
290 cancel_delayed_work_sync(&devfreq->work);
291}
6dcdd8e3 292EXPORT_SYMBOL(devfreq_monitor_suspend);
7e6fdd4b
RV
293
294/**
295 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
296 * @devfreq: the devfreq instance.
297 *
298 * Helper function to resume devfreq device load monitoing. Function
299 * to be called from governor in response to DEVFREQ_GOV_RESUME
300 * event or when polling interval is set to non-zero.
301 */
302void devfreq_monitor_resume(struct devfreq *devfreq)
303{
304 mutex_lock(&devfreq->lock);
305 if (!devfreq->stop_polling)
306 goto out;
307
308 if (!delayed_work_pending(&devfreq->work) &&
309 devfreq->profile->polling_ms)
310 queue_delayed_work(devfreq_wq, &devfreq->work,
311 msecs_to_jiffies(devfreq->profile->polling_ms));
312 devfreq->stop_polling = false;
313
314out:
315 mutex_unlock(&devfreq->lock);
316}
6dcdd8e3 317EXPORT_SYMBOL(devfreq_monitor_resume);
7e6fdd4b
RV
318
319/**
320 * devfreq_interval_update() - Update device devfreq monitoring interval
321 * @devfreq: the devfreq instance.
322 * @delay: new polling interval to be set.
323 *
324 * Helper function to set new load monitoring polling interval. Function
325 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
326 */
327void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
328{
329 unsigned int cur_delay = devfreq->profile->polling_ms;
330 unsigned int new_delay = *delay;
331
332 mutex_lock(&devfreq->lock);
333 devfreq->profile->polling_ms = new_delay;
334
335 if (devfreq->stop_polling)
336 goto out;
337
338 /* if new delay is zero, stop polling */
339 if (!new_delay) {
340 mutex_unlock(&devfreq->lock);
341 cancel_delayed_work_sync(&devfreq->work);
342 return;
343 }
344
345 /* if current delay is zero, start polling with new delay */
346 if (!cur_delay) {
347 queue_delayed_work(devfreq_wq, &devfreq->work,
348 msecs_to_jiffies(devfreq->profile->polling_ms));
349 goto out;
350 }
351
352 /* if current delay is greater than new delay, restart polling */
353 if (cur_delay > new_delay) {
354 mutex_unlock(&devfreq->lock);
355 cancel_delayed_work_sync(&devfreq->work);
356 mutex_lock(&devfreq->lock);
357 if (!devfreq->stop_polling)
358 queue_delayed_work(devfreq_wq, &devfreq->work,
359 msecs_to_jiffies(devfreq->profile->polling_ms));
360 }
361out:
362 mutex_unlock(&devfreq->lock);
363}
6dcdd8e3 364EXPORT_SYMBOL(devfreq_interval_update);
a3c98b8b
MH
365
366/**
367 * devfreq_notifier_call() - Notify that the device frequency requirements
368 * has been changed out of devfreq framework.
c5b4a1c1
NM
369 * @nb: the notifier_block (supposed to be devfreq->nb)
370 * @type: not used
371 * @devp: not used
a3c98b8b
MH
372 *
373 * Called by a notifier that uses devfreq->nb.
374 */
375static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
376 void *devp)
377{
378 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
379 int ret;
380
381 mutex_lock(&devfreq->lock);
382 ret = update_devfreq(devfreq);
383 mutex_unlock(&devfreq->lock);
384
385 return ret;
386}
387
388/**
7e6fdd4b 389 * _remove_devfreq() - Remove devfreq from the list and release its resources.
a3c98b8b
MH
390 * @devfreq: the devfreq struct
391 * @skip: skip calling device_unregister().
a3c98b8b
MH
392 */
393static void _remove_devfreq(struct devfreq *devfreq, bool skip)
394{
7e6fdd4b
RV
395 mutex_lock(&devfreq_list_lock);
396 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
397 mutex_unlock(&devfreq_list_lock);
398 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
a3c98b8b
MH
399 return;
400 }
7e6fdd4b
RV
401 list_del(&devfreq->node);
402 mutex_unlock(&devfreq_list_lock);
a3c98b8b 403
1b5c1be2
NM
404 if (devfreq->governor)
405 devfreq->governor->event_handler(devfreq,
406 DEVFREQ_GOV_STOP, NULL);
a3c98b8b
MH
407
408 if (devfreq->profile->exit)
409 devfreq->profile->exit(devfreq->dev.parent);
410
a3c98b8b
MH
411 if (!skip && get_device(&devfreq->dev)) {
412 device_unregister(&devfreq->dev);
413 put_device(&devfreq->dev);
414 }
415
a3c98b8b 416 mutex_destroy(&devfreq->lock);
a3c98b8b
MH
417 kfree(devfreq);
418}
419
420/**
421 * devfreq_dev_release() - Callback for struct device to release the device.
422 * @dev: the devfreq device
423 *
424 * This calls _remove_devfreq() if _remove_devfreq() is not called.
425 * Note that devfreq_dev_release() could be called by _remove_devfreq() as
426 * well as by others unregistering the device.
427 */
428static void devfreq_dev_release(struct device *dev)
429{
430 struct devfreq *devfreq = to_devfreq(dev);
a3c98b8b 431
a3c98b8b 432 _remove_devfreq(devfreq, true);
a3c98b8b
MH
433}
434
435/**
436 * devfreq_add_device() - Add devfreq feature to the device
437 * @dev: the device to add devfreq feature.
438 * @profile: device-specific profile to run devfreq.
1b5c1be2 439 * @governor_name: name of the policy to choose frequency.
a3c98b8b
MH
440 * @data: private data for the governor. The devfreq framework does not
441 * touch this value.
442 */
443struct devfreq *devfreq_add_device(struct device *dev,
444 struct devfreq_dev_profile *profile,
1b5c1be2 445 const char *governor_name,
a3c98b8b
MH
446 void *data)
447{
448 struct devfreq *devfreq;
1b5c1be2 449 struct devfreq_governor *governor;
a3c98b8b
MH
450 int err = 0;
451
1b5c1be2 452 if (!dev || !profile || !governor_name) {
a3c98b8b
MH
453 dev_err(dev, "%s: Invalid parameters.\n", __func__);
454 return ERR_PTR(-EINVAL);
455 }
456
7e6fdd4b
RV
457 mutex_lock(&devfreq_list_lock);
458 devfreq = find_device_devfreq(dev);
459 mutex_unlock(&devfreq_list_lock);
460 if (!IS_ERR(devfreq)) {
461 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
462 err = -EINVAL;
463 goto err_out;
a3c98b8b
MH
464 }
465
466 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
467 if (!devfreq) {
468 dev_err(dev, "%s: Unable to create devfreq for the device\n",
469 __func__);
470 err = -ENOMEM;
3f19f08a 471 goto err_out;
a3c98b8b
MH
472 }
473
474 mutex_init(&devfreq->lock);
475 mutex_lock(&devfreq->lock);
476 devfreq->dev.parent = dev;
477 devfreq->dev.class = devfreq_class;
478 devfreq->dev.release = devfreq_dev_release;
479 devfreq->profile = profile;
1b5c1be2 480 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
a3c98b8b
MH
481 devfreq->previous_freq = profile->initial_freq;
482 devfreq->data = data;
a3c98b8b
MH
483 devfreq->nb.notifier_call = devfreq_notifier_call;
484
e552bbaf
JL
485 devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
486 devfreq->profile->max_state *
487 devfreq->profile->max_state,
488 GFP_KERNEL);
3c2a0909 489 devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned long) *
e552bbaf
JL
490 devfreq->profile->max_state,
491 GFP_KERNEL);
492 devfreq->last_stat_updated = jiffies;
493
a3c98b8b
MH
494 dev_set_name(&devfreq->dev, dev_name(dev));
495 err = device_register(&devfreq->dev);
496 if (err) {
497 put_device(&devfreq->dev);
7e6fdd4b 498 mutex_unlock(&devfreq->lock);
a3c98b8b
MH
499 goto err_dev;
500 }
501
a3c98b8b
MH
502 mutex_unlock(&devfreq->lock);
503
a3c98b8b 504 mutex_lock(&devfreq_list_lock);
a3c98b8b
MH
505 list_add(&devfreq->node, &devfreq_list);
506
1b5c1be2
NM
507 governor = find_devfreq_governor(devfreq->governor_name);
508 if (!IS_ERR(governor))
509 devfreq->governor = governor;
510 if (devfreq->governor)
511 err = devfreq->governor->event_handler(devfreq,
512 DEVFREQ_GOV_START, NULL);
a3c98b8b 513 mutex_unlock(&devfreq_list_lock);
7e6fdd4b
RV
514 if (err) {
515 dev_err(dev, "%s: Unable to start governor for the device\n",
516 __func__);
517 goto err_init;
a3c98b8b 518 }
7e6fdd4b 519
3f19f08a
AL
520 return devfreq;
521
a3c98b8b 522err_init:
7e6fdd4b 523 list_del(&devfreq->node);
a3c98b8b
MH
524 device_unregister(&devfreq->dev);
525err_dev:
a3c98b8b 526 kfree(devfreq);
3f19f08a
AL
527err_out:
528 return ERR_PTR(err);
a3c98b8b 529}
7e6fdd4b 530EXPORT_SYMBOL(devfreq_add_device);
a3c98b8b
MH
531
532/**
533 * devfreq_remove_device() - Remove devfreq feature from a device.
c5b4a1c1 534 * @devfreq: the devfreq instance to be removed
a3c98b8b
MH
535 */
536int devfreq_remove_device(struct devfreq *devfreq)
537{
538 if (!devfreq)
539 return -EINVAL;
540
7e6fdd4b 541 _remove_devfreq(devfreq, false);
9f3bdd4f 542
a3c98b8b
MH
543 return 0;
544}
7e6fdd4b 545EXPORT_SYMBOL(devfreq_remove_device);
a3c98b8b 546
206c30cf
RV
547/**
548 * devfreq_suspend_device() - Suspend devfreq of a device.
549 * @devfreq: the devfreq instance to be suspended
550 */
551int devfreq_suspend_device(struct devfreq *devfreq)
552{
a3c98b8b
MH
553 if (!devfreq)
554 return -EINVAL;
555
1b5c1be2
NM
556 if (!devfreq->governor)
557 return 0;
558
206c30cf
RV
559 return devfreq->governor->event_handler(devfreq,
560 DEVFREQ_GOV_SUSPEND, NULL);
561}
562EXPORT_SYMBOL(devfreq_suspend_device);
563
564/**
565 * devfreq_resume_device() - Resume devfreq of a device.
566 * @devfreq: the devfreq instance to be resumed
567 */
568int devfreq_resume_device(struct devfreq *devfreq)
569{
570 if (!devfreq)
571 return -EINVAL;
572
1b5c1be2
NM
573 if (!devfreq->governor)
574 return 0;
575
206c30cf
RV
576 return devfreq->governor->event_handler(devfreq,
577 DEVFREQ_GOV_RESUME, NULL);
578}
579EXPORT_SYMBOL(devfreq_resume_device);
580
3aa173b8
NM
581/**
582 * devfreq_add_governor() - Add devfreq governor
583 * @governor: the devfreq governor to be added
584 */
585int devfreq_add_governor(struct devfreq_governor *governor)
586{
587 struct devfreq_governor *g;
1b5c1be2 588 struct devfreq *devfreq;
3aa173b8
NM
589 int err = 0;
590
591 if (!governor) {
592 pr_err("%s: Invalid parameters.\n", __func__);
593 return -EINVAL;
594 }
595
596 mutex_lock(&devfreq_list_lock);
597 g = find_devfreq_governor(governor->name);
598 if (!IS_ERR(g)) {
599 pr_err("%s: governor %s already registered\n", __func__,
600 g->name);
601 err = -EINVAL;
602 goto err_out;
603 }
9f3bdd4f 604
3aa173b8
NM
605 list_add(&governor->node, &devfreq_governor_list);
606
1b5c1be2
NM
607 list_for_each_entry(devfreq, &devfreq_list, node) {
608 int ret = 0;
609 struct device *dev = devfreq->dev.parent;
610
611 if (!strncmp(devfreq->governor_name, governor->name,
612 DEVFREQ_NAME_LEN)) {
613 /* The following should never occur */
614 if (devfreq->governor) {
615 dev_warn(dev,
616 "%s: Governor %s already present\n",
617 __func__, devfreq->governor->name);
618 ret = devfreq->governor->event_handler(devfreq,
619 DEVFREQ_GOV_STOP, NULL);
620 if (ret) {
621 dev_warn(dev,
622 "%s: Governor %s stop = %d\n",
623 __func__,
624 devfreq->governor->name, ret);
625 }
626 /* Fall through */
627 }
628 devfreq->governor = governor;
629 ret = devfreq->governor->event_handler(devfreq,
630 DEVFREQ_GOV_START, NULL);
631 if (ret) {
632 dev_warn(dev, "%s: Governor %s start=%d\n",
633 __func__, devfreq->governor->name,
634 ret);
635 }
a3c98b8b
MH
636 }
637 }
638
3aa173b8
NM
639err_out:
640 mutex_unlock(&devfreq_list_lock);
a3c98b8b 641
3aa173b8
NM
642 return err;
643}
644EXPORT_SYMBOL(devfreq_add_governor);
a3c98b8b 645
3aa173b8
NM
646/**
647 * devfreq_remove_device() - Remove devfreq feature from a device.
648 * @governor: the devfreq governor to be removed
649 */
650int devfreq_remove_governor(struct devfreq_governor *governor)
651{
652 struct devfreq_governor *g;
1b5c1be2 653 struct devfreq *devfreq;
3aa173b8
NM
654 int err = 0;
655
656 if (!governor) {
657 pr_err("%s: Invalid parameters.\n", __func__);
658 return -EINVAL;
659 }
660
661 mutex_lock(&devfreq_list_lock);
662 g = find_devfreq_governor(governor->name);
663 if (IS_ERR(g)) {
664 pr_err("%s: governor %s not registered\n", __func__,
b9e1c8e8 665 governor->name);
f9c08e2a 666 err = PTR_ERR(g);
3aa173b8
NM
667 goto err_out;
668 }
1b5c1be2
NM
669 list_for_each_entry(devfreq, &devfreq_list, node) {
670 int ret;
671 struct device *dev = devfreq->dev.parent;
672
673 if (!strncmp(devfreq->governor_name, governor->name,
674 DEVFREQ_NAME_LEN)) {
675 /* we should have a devfreq governor! */
676 if (!devfreq->governor) {
677 dev_warn(dev, "%s: Governor %s NOT present\n",
678 __func__, governor->name);
679 continue;
680 /* Fall through */
681 }
682 ret = devfreq->governor->event_handler(devfreq,
683 DEVFREQ_GOV_STOP, NULL);
684 if (ret) {
685 dev_warn(dev, "%s: Governor %s stop=%d\n",
686 __func__, devfreq->governor->name,
687 ret);
688 }
689 devfreq->governor = NULL;
690 }
691 }
3aa173b8
NM
692
693 list_del(&governor->node);
694err_out:
695 mutex_unlock(&devfreq_list_lock);
696
697 return err;
a3c98b8b 698}
3aa173b8 699EXPORT_SYMBOL(devfreq_remove_governor);
a3c98b8b 700
9005b650
MH
701static ssize_t show_governor(struct device *dev,
702 struct device_attribute *attr, char *buf)
703{
1b5c1be2
NM
704 if (!to_devfreq(dev)->governor)
705 return -EINVAL;
706
9005b650
MH
707 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
708}
709
0359d1af
NM
710static ssize_t store_governor(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
712{
713 struct devfreq *df = to_devfreq(dev);
714 int ret;
715 char str_governor[DEVFREQ_NAME_LEN + 1];
716 struct devfreq_governor *governor;
717
718 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
719 if (ret != 1)
720 return -EINVAL;
721
722 mutex_lock(&devfreq_list_lock);
723 governor = find_devfreq_governor(str_governor);
724 if (IS_ERR(governor)) {
725 ret = PTR_ERR(governor);
726 goto out;
727 }
728 if (df->governor == governor)
729 goto out;
730
731 if (df->governor) {
732 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
733 if (ret) {
734 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
735 __func__, df->governor->name, ret);
736 goto out;
737 }
738 }
739 df->governor = governor;
740 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
741 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
742 if (ret)
743 dev_warn(dev, "%s: Governor %s not started(%d)\n",
744 __func__, df->governor->name, ret);
745out:
746 mutex_unlock(&devfreq_list_lock);
747
748 if (!ret)
749 ret = count;
750 return ret;
751}
50a5b33e
NM
752static ssize_t show_available_governors(struct device *d,
753 struct device_attribute *attr,
754 char *buf)
755{
756 struct devfreq_governor *tmp_governor;
757 ssize_t count = 0;
758
759 mutex_lock(&devfreq_list_lock);
760 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
761 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
762 "%s ", tmp_governor->name);
763 mutex_unlock(&devfreq_list_lock);
764
765 /* Truncate the trailing space */
766 if (count)
767 count--;
768
769 count += sprintf(&buf[count], "\n");
770
771 return count;
772}
0359d1af 773
9005b650
MH
774static ssize_t show_freq(struct device *dev,
775 struct device_attribute *attr, char *buf)
7f98a905
RV
776{
777 unsigned long freq;
778 struct devfreq *devfreq = to_devfreq(dev);
779
780 if (devfreq->profile->get_cur_freq &&
781 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
782 return sprintf(buf, "%lu\n", freq);
783
784 return sprintf(buf, "%lu\n", devfreq->previous_freq);
785}
786
787static ssize_t show_target_freq(struct device *dev,
788 struct device_attribute *attr, char *buf)
9005b650
MH
789{
790 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
791}
792
793static ssize_t show_polling_interval(struct device *dev,
794 struct device_attribute *attr, char *buf)
795{
796 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
797}
798
799static ssize_t store_polling_interval(struct device *dev,
800 struct device_attribute *attr,
801 const char *buf, size_t count)
802{
803 struct devfreq *df = to_devfreq(dev);
804 unsigned int value;
805 int ret;
806
1b5c1be2
NM
807 if (!df->governor)
808 return -EINVAL;
809
9005b650
MH
810 ret = sscanf(buf, "%u", &value);
811 if (ret != 1)
12e26265 812 return -EINVAL;
9005b650 813
7e6fdd4b 814 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
9005b650
MH
815 ret = count;
816
9005b650
MH
817 return ret;
818}
819
6530b9de
MH
820static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr,
821 const char *buf, size_t count)
822{
823 struct devfreq *df = to_devfreq(dev);
824 unsigned long value;
825 int ret;
826 unsigned long max;
827
828 ret = sscanf(buf, "%lu", &value);
829 if (ret != 1)
12e26265 830 return -EINVAL;
6530b9de
MH
831
832 mutex_lock(&df->lock);
833 max = df->max_freq;
834 if (value && max && value > max) {
835 ret = -EINVAL;
836 goto unlock;
837 }
838
839 df->min_freq = value;
840 update_devfreq(df);
841 ret = count;
842unlock:
843 mutex_unlock(&df->lock);
6530b9de
MH
844 return ret;
845}
846
847static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr,
848 char *buf)
849{
850 return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
851}
852
853static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr,
854 const char *buf, size_t count)
855{
856 struct devfreq *df = to_devfreq(dev);
857 unsigned long value;
858 int ret;
859 unsigned long min;
860
861 ret = sscanf(buf, "%lu", &value);
862 if (ret != 1)
12e26265 863 return -EINVAL;
6530b9de
MH
864
865 mutex_lock(&df->lock);
866 min = df->min_freq;
867 if (value && min && value < min) {
868 ret = -EINVAL;
869 goto unlock;
870 }
871
872 df->max_freq = value;
873 update_devfreq(df);
874 ret = count;
875unlock:
876 mutex_unlock(&df->lock);
6530b9de
MH
877 return ret;
878}
879
880static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr,
881 char *buf)
882{
883 return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
884}
885
d287de85
NM
886static ssize_t show_available_freqs(struct device *d,
887 struct device_attribute *attr,
888 char *buf)
889{
890 struct devfreq *df = to_devfreq(d);
891 struct device *dev = df->dev.parent;
892 struct opp *opp;
893 ssize_t count = 0;
894 unsigned long freq = 0;
895
896 rcu_read_lock();
897 do {
898 opp = opp_find_freq_ceil(dev, &freq);
899 if (IS_ERR(opp))
900 break;
901
902 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
903 "%lu ", freq);
904 freq++;
905 } while (1);
906 rcu_read_unlock();
907
908 /* Truncate the trailing space */
909 if (count)
910 count--;
911
912 count += sprintf(&buf[count], "\n");
913
914 return count;
915}
916
e552bbaf
JL
917static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr,
918 char *buf)
919{
920 struct devfreq *devfreq = to_devfreq(dev);
921 ssize_t len;
922 int i, j, err;
923 unsigned int max_state = devfreq->profile->max_state;
924
925 err = devfreq_update_status(devfreq, devfreq->previous_freq);
926 if (err)
927 return 0;
928
929 len = sprintf(buf, " From : To\n");
930 len += sprintf(buf + len, " :");
931 for (i = 0; i < max_state; i++)
932 len += sprintf(buf + len, "%8u",
933 devfreq->profile->freq_table[i]);
934
935 len += sprintf(buf + len, " time(ms)\n");
936
937 for (i = 0; i < max_state; i++) {
938 if (devfreq->profile->freq_table[i]
939 == devfreq->previous_freq) {
940 len += sprintf(buf + len, "*");
941 } else {
942 len += sprintf(buf + len, " ");
943 }
944 len += sprintf(buf + len, "%8u:",
945 devfreq->profile->freq_table[i]);
946 for (j = 0; j < max_state; j++)
947 len += sprintf(buf + len, "%8u",
948 devfreq->trans_table[(i * max_state) + j]);
949 len += sprintf(buf + len, "%10u\n",
950 jiffies_to_msecs(devfreq->time_in_state[i]));
951 }
952
953 len += sprintf(buf + len, "Total transition : %u\n",
954 devfreq->total_trans);
955 return len;
956}
957
3c2a0909
S
958static ssize_t show_time_in_state(struct device *dev,
959 struct device_attribute *attr, char *buf)
960{
961 struct devfreq *devfreq = to_devfreq(dev);
962 ssize_t len = 0;
963 int i, 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 for (i = 0; i < max_state; i++) {
971 len += sprintf(buf + len, "%8u",
972 devfreq->profile->freq_table[i]);
973 len += sprintf(buf + len, "%10u\n",
974 jiffies_to_msecs(devfreq->time_in_state[i]));
975 }
976 return len;
977}
978
9005b650 979static struct device_attribute devfreq_attrs[] = {
0359d1af 980 __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor),
50a5b33e 981 __ATTR(available_governors, S_IRUGO, show_available_governors, NULL),
9005b650 982 __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
d287de85 983 __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL),
7f98a905 984 __ATTR(target_freq, S_IRUGO, show_target_freq, NULL),
9005b650
MH
985 __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
986 store_polling_interval),
6530b9de
MH
987 __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
988 __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
e552bbaf 989 __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL),
3c2a0909 990 __ATTR(time_in_state_raw, S_IRUGO, show_time_in_state, NULL),
9005b650
MH
991 { },
992};
993
a3c98b8b
MH
994static int __init devfreq_init(void)
995{
996 devfreq_class = class_create(THIS_MODULE, "devfreq");
997 if (IS_ERR(devfreq_class)) {
998 pr_err("%s: couldn't create class\n", __FILE__);
999 return PTR_ERR(devfreq_class);
1000 }
7e6fdd4b
RV
1001
1002 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1003 if (IS_ERR(devfreq_wq)) {
1004 class_destroy(devfreq_class);
1005 pr_err("%s: couldn't create workqueue\n", __FILE__);
1006 return PTR_ERR(devfreq_wq);
1007 }
9005b650 1008 devfreq_class->dev_attrs = devfreq_attrs;
7e6fdd4b 1009
a3c98b8b
MH
1010 return 0;
1011}
1012subsys_initcall(devfreq_init);
1013
1014static void __exit devfreq_exit(void)
1015{
1016 class_destroy(devfreq_class);
7e6fdd4b 1017 destroy_workqueue(devfreq_wq);
a3c98b8b
MH
1018}
1019module_exit(devfreq_exit);
1020
1021/*
1022 * The followings are helper functions for devfreq user device drivers with
1023 * OPP framework.
1024 */
1025
1026/**
1027 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1028 * freq value given to target callback.
c5b4a1c1
NM
1029 * @dev: The devfreq user device. (parent of devfreq)
1030 * @freq: The frequency given to target function
1031 * @flags: Flags handed from devfreq framework.
a3c98b8b 1032 *
bcb27549
NM
1033 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1034 * protected pointer. The reason for the same is that the opp pointer which is
1035 * returned will remain valid for use with opp_get_{voltage, freq} only while
1036 * under the locked area. The pointer returned must be used prior to unlocking
1037 * with rcu_read_unlock() to maintain the integrity of the pointer.
a3c98b8b 1038 */
ab5f299f
MH
1039struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
1040 u32 flags)
a3c98b8b 1041{
ab5f299f 1042 struct opp *opp;
a3c98b8b 1043
ab5f299f
MH
1044 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1045 /* The freq is an upper bound. opp should be lower */
a3c98b8b 1046 opp = opp_find_freq_floor(dev, freq);
ab5f299f
MH
1047
1048 /* If not available, use the closest opp */
0779726c 1049 if (opp == ERR_PTR(-ERANGE))
ab5f299f
MH
1050 opp = opp_find_freq_ceil(dev, freq);
1051 } else {
1052 /* The freq is an lower bound. opp should be higher */
1053 opp = opp_find_freq_ceil(dev, freq);
1054
1055 /* If not available, use the closest opp */
0779726c 1056 if (opp == ERR_PTR(-ERANGE))
ab5f299f
MH
1057 opp = opp_find_freq_floor(dev, freq);
1058 }
1059
a3c98b8b
MH
1060 return opp;
1061}
1062
1063/**
1064 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1065 * for any changes in the OPP availability
1066 * changes
c5b4a1c1
NM
1067 * @dev: The devfreq user device. (parent of devfreq)
1068 * @devfreq: The devfreq object.
a3c98b8b
MH
1069 */
1070int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1071{
389baed0
MH
1072 struct srcu_notifier_head *nh;
1073 int ret = 0;
a3c98b8b 1074
389baed0
MH
1075 rcu_read_lock();
1076 nh = opp_get_notifier(dev);
a3c98b8b 1077 if (IS_ERR(nh))
389baed0
MH
1078 ret = PTR_ERR(nh);
1079 rcu_read_unlock();
1080 if (!ret)
1081 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1082
1083 return ret;
a3c98b8b
MH
1084}
1085
1086/**
1087 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1088 * notified for any changes in the OPP
1089 * availability changes anymore.
c5b4a1c1
NM
1090 * @dev: The devfreq user device. (parent of devfreq)
1091 * @devfreq: The devfreq object.
a3c98b8b
MH
1092 *
1093 * At exit() callback of devfreq_dev_profile, this must be included if
1094 * devfreq_recommended_opp is used.
1095 */
1096int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1097{
389baed0
MH
1098 struct srcu_notifier_head *nh;
1099 int ret = 0;
a3c98b8b 1100
389baed0
MH
1101 rcu_read_lock();
1102 nh = opp_get_notifier(dev);
a3c98b8b 1103 if (IS_ERR(nh))
389baed0
MH
1104 ret = PTR_ERR(nh);
1105 rcu_read_unlock();
1106 if (!ret)
1107 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1108
1109 return ret;
a3c98b8b
MH
1110}
1111
1112MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1113MODULE_DESCRIPTION("devfreq class support");
1114MODULE_LICENSE("GPL");