]>
Commit | Line | Data |
---|---|---|
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 | 30 | static 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 | 37 | static struct workqueue_struct *devfreq_wq; |
a3c98b8b | 38 | |
3aa173b8 NM |
39 | /* The list of all device-devfreq governors */ |
40 | static LIST_HEAD(devfreq_governor_list); | |
a3c98b8b MH |
41 | /* The list of all device-devfreq */ |
42 | static LIST_HEAD(devfreq_list); | |
43 | static 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 | */ | |
52 | static 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 | */ | |
76 | static 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 | */ | |
92 | static 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 | ||
3aa173b8 NM |
116 | /** |
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 | */ | |
123 | static 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 | ||
7e6fdd4b RV |
142 | /* Load monitoring helper functions for governors use */ |
143 | ||
a3c98b8b MH |
144 | /** |
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 | */ | |
151 | int update_devfreq(struct devfreq *devfreq) | |
152 | { | |
153 | unsigned long freq; | |
154 | int err = 0; | |
ab5f299f | 155 | u32 flags = 0; |
a3c98b8b MH |
156 | |
157 | if (!mutex_is_locked(&devfreq->lock)) { | |
158 | WARN(true, "devfreq->lock must be locked by the caller.\n"); | |
159 | return -EINVAL; | |
160 | } | |
161 | ||
1b5c1be2 NM |
162 | if (!devfreq->governor) |
163 | return -EINVAL; | |
164 | ||
a3c98b8b MH |
165 | /* Reevaluate the proper frequency */ |
166 | err = devfreq->governor->get_target_freq(devfreq, &freq); | |
167 | if (err) | |
168 | return err; | |
169 | ||
ab5f299f MH |
170 | /* |
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); | |
a3c98b8b MH |
188 | if (err) |
189 | return err; | |
190 | ||
e552bbaf JL |
191 | 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 | ||
a3c98b8b MH |
196 | devfreq->previous_freq = freq; |
197 | return err; | |
198 | } | |
2df5021f | 199 | EXPORT_SYMBOL(update_devfreq); |
a3c98b8b | 200 | |
7e6fdd4b RV |
201 | /** |
202 | * devfreq_monitor() - Periodically poll devfreq objects. | |
203 | * @work: the work struct used to run devfreq_monitor periodically. | |
204 | * | |
205 | */ | |
206 | static 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 | */ | |
231 | void 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 | */ | |
247 | void 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 | */ | |
264 | void 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 | */ | |
285 | void 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 | ||
297 | out: | |
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 | */ | |
309 | void 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 | } | |
343 | out: | |
344 | mutex_unlock(&devfreq->lock); | |
345 | } | |
346 | ||
a3c98b8b MH |
347 | /** |
348 | * devfreq_notifier_call() - Notify that the device frequency requirements | |
349 | * has been changed out of devfreq framework. | |
c5b4a1c1 NM |
350 | * @nb: the notifier_block (supposed to be devfreq->nb) |
351 | * @type: not used | |
352 | * @devp: not used | |
a3c98b8b MH |
353 | * |
354 | * Called by a notifier that uses devfreq->nb. | |
355 | */ | |
356 | static 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 | /** | |
7e6fdd4b | 370 | * _remove_devfreq() - Remove devfreq from the list and release its resources. |
a3c98b8b MH |
371 | * @devfreq: the devfreq struct |
372 | * @skip: skip calling device_unregister(). | |
a3c98b8b MH |
373 | */ |
374 | static void _remove_devfreq(struct devfreq *devfreq, bool skip) | |
375 | { | |
7e6fdd4b RV |
376 | 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"); | |
a3c98b8b MH |
380 | return; |
381 | } | |
7e6fdd4b RV |
382 | list_del(&devfreq->node); |
383 | mutex_unlock(&devfreq_list_lock); | |
a3c98b8b | 384 | |
1b5c1be2 NM |
385 | if (devfreq->governor) |
386 | devfreq->governor->event_handler(devfreq, | |
387 | DEVFREQ_GOV_STOP, NULL); | |
a3c98b8b MH |
388 | |
389 | if (devfreq->profile->exit) | |
390 | devfreq->profile->exit(devfreq->dev.parent); | |
391 | ||
a3c98b8b MH |
392 | if (!skip && get_device(&devfreq->dev)) { |
393 | device_unregister(&devfreq->dev); | |
394 | put_device(&devfreq->dev); | |
395 | } | |
396 | ||
a3c98b8b | 397 | mutex_destroy(&devfreq->lock); |
a3c98b8b MH |
398 | 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 | */ | |
409 | static void devfreq_dev_release(struct device *dev) | |
410 | { | |
411 | struct devfreq *devfreq = to_devfreq(dev); | |
a3c98b8b | 412 | |
a3c98b8b | 413 | _remove_devfreq(devfreq, true); |
a3c98b8b MH |
414 | } |
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. | |
1b5c1be2 | 420 | * @governor_name: name of the policy to choose frequency. |
a3c98b8b MH |
421 | * @data: private data for the governor. The devfreq framework does not |
422 | * touch this value. | |
423 | */ | |
424 | struct devfreq *devfreq_add_device(struct device *dev, | |
425 | struct devfreq_dev_profile *profile, | |
1b5c1be2 | 426 | const char *governor_name, |
a3c98b8b MH |
427 | void *data) |
428 | { | |
429 | struct devfreq *devfreq; | |
1b5c1be2 | 430 | struct devfreq_governor *governor; |
a3c98b8b MH |
431 | int err = 0; |
432 | ||
1b5c1be2 | 433 | if (!dev || !profile || !governor_name) { |
a3c98b8b MH |
434 | dev_err(dev, "%s: Invalid parameters.\n", __func__); |
435 | return ERR_PTR(-EINVAL); | |
436 | } | |
437 | ||
7e6fdd4b RV |
438 | 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; | |
a3c98b8b MH |
445 | } |
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; | |
3f19f08a | 452 | goto err_out; |
a3c98b8b MH |
453 | } |
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; | |
1b5c1be2 | 461 | strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN); |
a3c98b8b MH |
462 | devfreq->previous_freq = profile->initial_freq; |
463 | devfreq->data = data; | |
a3c98b8b MH |
464 | devfreq->nb.notifier_call = devfreq_notifier_call; |
465 | ||
e552bbaf JL |
466 | 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 | ||
a3c98b8b MH |
475 | dev_set_name(&devfreq->dev, dev_name(dev)); |
476 | err = device_register(&devfreq->dev); | |
477 | if (err) { | |
478 | put_device(&devfreq->dev); | |
7e6fdd4b | 479 | mutex_unlock(&devfreq->lock); |
a3c98b8b MH |
480 | goto err_dev; |
481 | } | |
482 | ||
a3c98b8b MH |
483 | mutex_unlock(&devfreq->lock); |
484 | ||
a3c98b8b | 485 | mutex_lock(&devfreq_list_lock); |
a3c98b8b MH |
486 | list_add(&devfreq->node, &devfreq_list); |
487 | ||
1b5c1be2 NM |
488 | 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); | |
7e6fdd4b RV |
495 | if (err) { |
496 | dev_err(dev, "%s: Unable to start governor for the device\n", | |
497 | __func__); | |
498 | goto err_init; | |
a3c98b8b | 499 | } |
7e6fdd4b | 500 | |
3f19f08a AL |
501 | return devfreq; |
502 | ||
a3c98b8b | 503 | err_init: |
7e6fdd4b | 504 | list_del(&devfreq->node); |
a3c98b8b MH |
505 | device_unregister(&devfreq->dev); |
506 | err_dev: | |
a3c98b8b | 507 | kfree(devfreq); |
3f19f08a AL |
508 | err_out: |
509 | return ERR_PTR(err); | |
a3c98b8b | 510 | } |
7e6fdd4b | 511 | EXPORT_SYMBOL(devfreq_add_device); |
a3c98b8b MH |
512 | |
513 | /** | |
514 | * devfreq_remove_device() - Remove devfreq feature from a device. | |
c5b4a1c1 | 515 | * @devfreq: the devfreq instance to be removed |
a3c98b8b MH |
516 | */ |
517 | int devfreq_remove_device(struct devfreq *devfreq) | |
518 | { | |
519 | if (!devfreq) | |
520 | return -EINVAL; | |
521 | ||
7e6fdd4b | 522 | _remove_devfreq(devfreq, false); |
a3c98b8b MH |
523 | |
524 | return 0; | |
525 | } | |
7e6fdd4b | 526 | EXPORT_SYMBOL(devfreq_remove_device); |
a3c98b8b | 527 | |
206c30cf RV |
528 | /** |
529 | * devfreq_suspend_device() - Suspend devfreq of a device. | |
530 | * @devfreq: the devfreq instance to be suspended | |
531 | */ | |
532 | int devfreq_suspend_device(struct devfreq *devfreq) | |
533 | { | |
534 | if (!devfreq) | |
535 | return -EINVAL; | |
536 | ||
1b5c1be2 NM |
537 | if (!devfreq->governor) |
538 | return 0; | |
539 | ||
206c30cf RV |
540 | return devfreq->governor->event_handler(devfreq, |
541 | DEVFREQ_GOV_SUSPEND, NULL); | |
542 | } | |
543 | EXPORT_SYMBOL(devfreq_suspend_device); | |
544 | ||
545 | /** | |
546 | * devfreq_resume_device() - Resume devfreq of a device. | |
547 | * @devfreq: the devfreq instance to be resumed | |
548 | */ | |
549 | int devfreq_resume_device(struct devfreq *devfreq) | |
550 | { | |
551 | if (!devfreq) | |
552 | return -EINVAL; | |
553 | ||
1b5c1be2 NM |
554 | if (!devfreq->governor) |
555 | return 0; | |
556 | ||
206c30cf RV |
557 | return devfreq->governor->event_handler(devfreq, |
558 | DEVFREQ_GOV_RESUME, NULL); | |
559 | } | |
560 | EXPORT_SYMBOL(devfreq_resume_device); | |
561 | ||
3aa173b8 NM |
562 | /** |
563 | * devfreq_add_governor() - Add devfreq governor | |
564 | * @governor: the devfreq governor to be added | |
565 | */ | |
566 | int devfreq_add_governor(struct devfreq_governor *governor) | |
567 | { | |
568 | struct devfreq_governor *g; | |
1b5c1be2 | 569 | struct devfreq *devfreq; |
3aa173b8 NM |
570 | 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 | ||
1b5c1be2 NM |
588 | 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 | ||
3aa173b8 NM |
620 | err_out: |
621 | mutex_unlock(&devfreq_list_lock); | |
622 | ||
623 | return err; | |
624 | } | |
625 | EXPORT_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 | */ | |
631 | int devfreq_remove_governor(struct devfreq_governor *governor) | |
632 | { | |
633 | struct devfreq_governor *g; | |
1b5c1be2 | 634 | struct devfreq *devfreq; |
3aa173b8 NM |
635 | 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__, | |
b9e1c8e8 | 646 | governor->name); |
3aa173b8 NM |
647 | err = -EINVAL; |
648 | goto err_out; | |
649 | } | |
1b5c1be2 NM |
650 | 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 | } | |
3aa173b8 NM |
673 | |
674 | list_del(&governor->node); | |
675 | err_out: | |
676 | mutex_unlock(&devfreq_list_lock); | |
677 | ||
678 | return err; | |
679 | } | |
680 | EXPORT_SYMBOL(devfreq_remove_governor); | |
681 | ||
9005b650 MH |
682 | static ssize_t show_governor(struct device *dev, |
683 | struct device_attribute *attr, char *buf) | |
684 | { | |
1b5c1be2 NM |
685 | if (!to_devfreq(dev)->governor) |
686 | return -EINVAL; | |
687 | ||
9005b650 MH |
688 | return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name); |
689 | } | |
690 | ||
0359d1af NM |
691 | static 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); | |
726 | out: | |
727 | mutex_unlock(&devfreq_list_lock); | |
728 | ||
729 | if (!ret) | |
730 | ret = count; | |
731 | return ret; | |
732 | } | |
50a5b33e NM |
733 | static 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 | } | |
0359d1af | 754 | |
9005b650 MH |
755 | static ssize_t show_freq(struct device *dev, |
756 | struct device_attribute *attr, char *buf) | |
7f98a905 RV |
757 | { |
758 | 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 | ||
768 | static ssize_t show_target_freq(struct device *dev, | |
769 | struct device_attribute *attr, char *buf) | |
9005b650 MH |
770 | { |
771 | return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq); | |
772 | } | |
773 | ||
774 | static 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 | ||
780 | static 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 | ||
1b5c1be2 NM |
788 | if (!df->governor) |
789 | return -EINVAL; | |
790 | ||
9005b650 MH |
791 | ret = sscanf(buf, "%u", &value); |
792 | if (ret != 1) | |
12e26265 | 793 | return -EINVAL; |
9005b650 | 794 | |
7e6fdd4b | 795 | df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value); |
9005b650 MH |
796 | ret = count; |
797 | ||
9005b650 MH |
798 | return ret; |
799 | } | |
800 | ||
6530b9de MH |
801 | static 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) | |
12e26265 | 811 | return -EINVAL; |
6530b9de MH |
812 | |
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; | |
823 | unlock: | |
824 | mutex_unlock(&df->lock); | |
6530b9de MH |
825 | return ret; |
826 | } | |
827 | ||
828 | static 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 | ||
834 | static 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) | |
12e26265 | 844 | return -EINVAL; |
6530b9de MH |
845 | |
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; | |
856 | unlock: | |
857 | mutex_unlock(&df->lock); | |
6530b9de MH |
858 | return ret; |
859 | } | |
860 | ||
861 | static 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 | ||
d287de85 NM |
867 | static 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 | ||
e552bbaf JL |
898 | static 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 | ||
9005b650 | 939 | static struct device_attribute devfreq_attrs[] = { |
0359d1af | 940 | __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor), |
50a5b33e | 941 | __ATTR(available_governors, S_IRUGO, show_available_governors, NULL), |
9005b650 | 942 | __ATTR(cur_freq, S_IRUGO, show_freq, NULL), |
d287de85 | 943 | __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL), |
7f98a905 | 944 | __ATTR(target_freq, S_IRUGO, show_target_freq, NULL), |
9005b650 MH |
945 | __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval, |
946 | store_polling_interval), | |
6530b9de MH |
947 | __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), | |
e552bbaf | 949 | __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL), |
9005b650 MH |
950 | { }, |
951 | }; | |
952 | ||
a3c98b8b MH |
953 | static 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 | } | |
7e6fdd4b RV |
960 | |
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 | } | |
9005b650 | 967 | devfreq_class->dev_attrs = devfreq_attrs; |
7e6fdd4b | 968 | |
a3c98b8b MH |
969 | return 0; |
970 | } | |
971 | subsys_initcall(devfreq_init); | |
972 | ||
973 | static void __exit devfreq_exit(void) | |
974 | { | |
975 | class_destroy(devfreq_class); | |
7e6fdd4b | 976 | destroy_workqueue(devfreq_wq); |
a3c98b8b MH |
977 | } |
978 | module_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. | |
c5b4a1c1 NM |
988 | * @dev: The devfreq user device. (parent of devfreq) |
989 | * @freq: The frequency given to target function | |
990 | * @flags: Flags handed from devfreq framework. | |
a3c98b8b MH |
991 | * |
992 | */ | |
ab5f299f MH |
993 | struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq, |
994 | u32 flags) | |
a3c98b8b | 995 | { |
ab5f299f | 996 | struct opp *opp; |
a3c98b8b | 997 | |
ab5f299f MH |
998 | if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) { |
999 | /* The freq is an upper bound. opp should be lower */ | |
a3c98b8b | 1000 | opp = opp_find_freq_floor(dev, freq); |
ab5f299f MH |
1001 | |
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 | ||
a3c98b8b MH |
1014 | 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 | |
c5b4a1c1 NM |
1021 | * @dev: The devfreq user device. (parent of devfreq) |
1022 | * @devfreq: The devfreq object. | |
a3c98b8b MH |
1023 | */ |
1024 | int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq) | |
1025 | { | |
389baed0 MH |
1026 | struct srcu_notifier_head *nh; |
1027 | int ret = 0; | |
a3c98b8b | 1028 | |
389baed0 MH |
1029 | rcu_read_lock(); |
1030 | nh = opp_get_notifier(dev); | |
a3c98b8b | 1031 | if (IS_ERR(nh)) |
389baed0 MH |
1032 | ret = PTR_ERR(nh); |
1033 | rcu_read_unlock(); | |
1034 | if (!ret) | |
1035 | ret = srcu_notifier_chain_register(nh, &devfreq->nb); | |
1036 | ||
1037 | return ret; | |
a3c98b8b MH |
1038 | } |
1039 | ||
1040 | /** | |
1041 | * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq | |
1042 | * notified for any changes in the OPP | |
1043 | * availability changes anymore. | |
c5b4a1c1 NM |
1044 | * @dev: The devfreq user device. (parent of devfreq) |
1045 | * @devfreq: The devfreq object. | |
a3c98b8b MH |
1046 | * |
1047 | * At exit() callback of devfreq_dev_profile, this must be included if | |
1048 | * devfreq_recommended_opp is used. | |
1049 | */ | |
1050 | int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq) | |
1051 | { | |
389baed0 MH |
1052 | struct srcu_notifier_head *nh; |
1053 | int ret = 0; | |
a3c98b8b | 1054 | |
389baed0 MH |
1055 | rcu_read_lock(); |
1056 | nh = opp_get_notifier(dev); | |
a3c98b8b | 1057 | if (IS_ERR(nh)) |
389baed0 MH |
1058 | ret = PTR_ERR(nh); |
1059 | rcu_read_unlock(); | |
1060 | if (!ret) | |
1061 | ret = srcu_notifier_chain_unregister(nh, &devfreq->nb); | |
1062 | ||
1063 | return ret; | |
a3c98b8b MH |
1064 | } |
1065 | ||
1066 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
1067 | MODULE_DESCRIPTION("devfreq class support"); | |
1068 | MODULE_LICENSE("GPL"); |