Lines Matching defs:devfreq

2  * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
22 #include <linux/devfreq.h>
33 * devfreq core provides delayed work based load monitoring helper
39 /* The list of all device-devfreq governors */
41 /* The list of all device-devfreq */
46 * find_device_devfreq() - find devfreq struct using device pointer
47 * @dev: device pointer used to lookup device devfreq.
50 * devfreq info. devfreq_list_lock should be held by the caller.
52 static struct devfreq *find_device_devfreq(struct device *dev)
54 struct devfreq *tmp_devfreq;
73 * @devfreq: the devfreq instance
76 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
80 for (lev = 0; lev < devfreq->profile->max_state; lev++)
81 if (freq == devfreq->profile->freq_table[lev])
88 * devfreq_update_status() - Update statistics of devfreq behavior
89 * @devfreq: the devfreq instance
92 static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
99 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
105 devfreq->time_in_state[prev_lev] +=
106 cur_time - devfreq->last_stat_updated;
108 lev = devfreq_get_freq_level(devfreq, freq);
115 devfreq->trans_table[(prev_lev *
116 devfreq->profile->max_state) + lev]++;
117 devfreq->total_trans++;
121 devfreq->last_stat_updated = cur_time;
126 * find_devfreq_governor() - find devfreq governor from name
129 * Search the list of devfreq governors and return the matched
155 * @devfreq: the devfreq instance.
157 * Note: Lock devfreq->lock before calling update_devfreq
160 int update_devfreq(struct devfreq *devfreq)
166 if (!mutex_is_locked(&devfreq->lock)) {
167 WARN(true, "devfreq->lock must be locked by the caller.\n");
171 if (!devfreq->governor)
175 err = devfreq->governor->get_target_freq(devfreq, &freq);
187 if (devfreq->min_freq && freq < devfreq->min_freq) {
188 freq = devfreq->min_freq;
191 if (devfreq->max_freq && freq > devfreq->max_freq) {
192 freq = devfreq->max_freq;
196 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
200 if (devfreq->profile->freq_table)
201 if (devfreq_update_status(devfreq, freq))
202 dev_err(&devfreq->dev,
205 devfreq->previous_freq = freq;
211 * devfreq_monitor() - Periodically poll devfreq objects.
218 struct devfreq *devfreq = container_of(work,
219 struct devfreq, work.work);
221 mutex_lock(&devfreq->lock);
222 err = update_devfreq(devfreq);
224 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
226 queue_delayed_work(devfreq_wq, &devfreq->work,
227 msecs_to_jiffies(devfreq->profile->polling_ms));
228 mutex_unlock(&devfreq->lock);
232 * devfreq_monitor_start() - Start load monitoring of devfreq instance
233 * @devfreq: the devfreq instance.
235 * Helper function for starting devfreq device load monitoing. By
238 * event when device is added to devfreq framework.
240 void devfreq_monitor_start(struct devfreq *devfreq)
242 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
243 if (devfreq->profile->polling_ms)
244 queue_delayed_work(devfreq_wq, &devfreq->work,
245 msecs_to_jiffies(devfreq->profile->polling_ms));
250 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
251 * @devfreq: the devfreq instance.
253 * Helper function to stop devfreq device load monitoing. Function
255 * event when device is removed from devfreq framework.
257 void devfreq_monitor_stop(struct devfreq *devfreq)
259 cancel_delayed_work_sync(&devfreq->work);
264 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
265 * @devfreq: the devfreq instance.
267 * Helper function to suspend devfreq device load monitoing. Function
275 void devfreq_monitor_suspend(struct devfreq *devfreq)
277 mutex_lock(&devfreq->lock);
278 if (devfreq->stop_polling) {
279 mutex_unlock(&devfreq->lock);
283 devfreq_update_status(devfreq, devfreq->previous_freq);
284 devfreq->stop_polling = true;
285 mutex_unlock(&devfreq->lock);
286 cancel_delayed_work_sync(&devfreq->work);
291 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
292 * @devfreq: the devfreq instance.
294 * Helper function to resume devfreq device load monitoing. Function
298 void devfreq_monitor_resume(struct devfreq *devfreq)
302 mutex_lock(&devfreq->lock);
303 if (!devfreq->stop_polling)
306 if (!delayed_work_pending(&devfreq->work) &&
307 devfreq->profile->polling_ms)
308 queue_delayed_work(devfreq_wq, &devfreq->work,
309 msecs_to_jiffies(devfreq->profile->polling_ms));
311 devfreq->last_stat_updated = jiffies;
312 devfreq->stop_polling = false;
314 if (devfreq->profile->get_cur_freq &&
315 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
316 devfreq->previous_freq = freq;
319 mutex_unlock(&devfreq->lock);
324 * devfreq_interval_update() - Update device devfreq monitoring interval
325 * @devfreq: the devfreq instance.
331 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
333 unsigned int cur_delay = devfreq->profile->polling_ms;
336 mutex_lock(&devfreq->lock);
337 devfreq->profile->polling_ms = new_delay;
339 if (devfreq->stop_polling)
344 mutex_unlock(&devfreq->lock);
345 cancel_delayed_work_sync(&devfreq->work);
351 queue_delayed_work(devfreq_wq, &devfreq->work,
352 msecs_to_jiffies(devfreq->profile->polling_ms));
358 mutex_unlock(&devfreq->lock);
359 cancel_delayed_work_sync(&devfreq->work);
360 mutex_lock(&devfreq->lock);
361 if (!devfreq->stop_polling)
362 queue_delayed_work(devfreq_wq, &devfreq->work,
363 msecs_to_jiffies(devfreq->profile->polling_ms));
366 mutex_unlock(&devfreq->lock);
372 * has been changed out of devfreq framework.
373 * @nb: the notifier_block (supposed to be devfreq->nb)
377 * Called by a notifier that uses devfreq->nb.
382 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
385 mutex_lock(&devfreq->lock);
386 ret = update_devfreq(devfreq);
387 mutex_unlock(&devfreq->lock);
393 * _remove_devfreq() - Remove devfreq from the list and release its resources.
394 * @devfreq: the devfreq struct
397 static void _remove_devfreq(struct devfreq *devfreq)
400 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
402 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
405 list_del(&devfreq->node);
408 if (devfreq->governor)
409 devfreq->governor->event_handler(devfreq,
412 if (devfreq->profile->exit)
413 devfreq->profile->exit(devfreq->dev.parent);
415 mutex_destroy(&devfreq->lock);
416 kfree(devfreq);
421 * @dev: the devfreq device
427 struct devfreq *devfreq = to_devfreq(dev);
429 _remove_devfreq(devfreq);
433 * devfreq_add_device() - Add devfreq feature to the device
434 * @dev: the device to add devfreq feature.
435 * @profile: device-specific profile to run devfreq.
437 * @data: private data for the governor. The devfreq framework does not
440 struct devfreq *devfreq_add_device(struct device *dev,
445 struct devfreq *devfreq;
455 devfreq = find_device_devfreq(dev);
457 if (!IS_ERR(devfreq)) {
458 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
463 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
464 if (!devfreq) {
465 dev_err(dev, "%s: Unable to create devfreq for the device\n",
471 mutex_init(&devfreq->lock);
472 mutex_lock(&devfreq->lock);
473 devfreq->dev.parent = dev;
474 devfreq->dev.class = devfreq_class;
475 devfreq->dev.release = devfreq_dev_release;
476 devfreq->profile = profile;
477 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
478 devfreq->previous_freq = profile->initial_freq;
479 devfreq->data = data;
480 devfreq->nb.notifier_call = devfreq_notifier_call;
482 devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
483 devfreq->profile->max_state *
484 devfreq->profile->max_state,
486 devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) *
487 devfreq->profile->max_state,
489 devfreq->last_stat_updated = jiffies;
491 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
492 err = device_register(&devfreq->dev);
494 put_device(&devfreq->dev);
495 mutex_unlock(&devfreq->lock);
499 mutex_unlock(&devfreq->lock);
502 list_add(&devfreq->node, &devfreq_list);
504 governor = find_devfreq_governor(devfreq->governor_name);
506 devfreq->governor = governor;
507 if (devfreq->governor)
508 err = devfreq->governor->event_handler(devfreq,
517 return devfreq;
520 list_del(&devfreq->node);
521 device_unregister(&devfreq->dev);
523 kfree(devfreq);
530 * devfreq_remove_device() - Remove devfreq feature from a device.
531 * @devfreq: the devfreq instance to be removed
535 int devfreq_remove_device(struct devfreq *devfreq)
537 if (!devfreq)
540 device_unregister(&devfreq->dev);
541 put_device(&devfreq->dev);
549 struct devfreq **r = res;
559 devfreq_remove_device(*(struct devfreq **)res);
564 * @dev: the device to add devfreq feature.
565 * @profile: device-specific profile to run devfreq.
567 * @data: private data for the governor. The devfreq framework does not
570 * This function manages automatically the memory of devfreq device using device
571 * resource management and simplify the free operation for memory of devfreq
574 struct devfreq *devm_devfreq_add_device(struct device *dev,
579 struct devfreq **ptr, *devfreq;
585 devfreq = devfreq_add_device(dev, profile, governor_name, data);
586 if (IS_ERR(devfreq)) {
591 *ptr = devfreq;
594 return devfreq;
600 * @dev: the device to add devfreq feature.
601 * @devfreq: the devfreq instance to be removed
603 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
606 devm_devfreq_dev_match, devfreq));
611 * devfreq_suspend_device() - Suspend devfreq of a device.
612 * @devfreq: the devfreq instance to be suspended
616 * holds the devfreq.
618 int devfreq_suspend_device(struct devfreq *devfreq)
620 if (!devfreq)
623 if (!devfreq->governor)
626 return devfreq->governor->event_handler(devfreq,
632 * devfreq_resume_device() - Resume devfreq of a device.
633 * @devfreq: the devfreq instance to be resumed
637 * holds the devfreq.
639 int devfreq_resume_device(struct devfreq *devfreq)
641 if (!devfreq)
644 if (!devfreq->governor)
647 return devfreq->governor->event_handler(devfreq,
653 * devfreq_add_governor() - Add devfreq governor
654 * @governor: the devfreq governor to be added
659 struct devfreq *devfreq;
678 list_for_each_entry(devfreq, &devfreq_list, node) {
680 struct device *dev = devfreq->dev.parent;
682 if (!strncmp(devfreq->governor_name, governor->name,
685 if (devfreq->governor) {
688 __func__, devfreq->governor->name);
689 ret = devfreq->governor->event_handler(devfreq,
695 devfreq->governor->name, ret);
699 devfreq->governor = governor;
700 ret = devfreq->governor->event_handler(devfreq,
704 __func__, devfreq->governor->name,
718 * devfreq_remove_device() - Remove devfreq feature from a device.
719 * @governor: the devfreq governor to be removed
724 struct devfreq *devfreq;
740 list_for_each_entry(devfreq, &devfreq_list, node) {
742 struct device *dev = devfreq->dev.parent;
744 if (!strncmp(devfreq->governor_name, governor->name,
746 /* we should have a devfreq governor! */
747 if (!devfreq->governor) {
753 ret = devfreq->governor->event_handler(devfreq,
757 __func__, devfreq->governor->name,
760 devfreq->governor = NULL;
784 struct devfreq *df = to_devfreq(dev);
852 struct devfreq *devfreq = to_devfreq(dev);
854 if (devfreq->profile->get_cur_freq &&
855 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
858 return sprintf(buf, "%lu\n", devfreq->previous_freq);
879 struct devfreq *df = to_devfreq(dev);
900 struct devfreq *df = to_devfreq(dev);
933 struct devfreq *df = to_devfreq(dev);
969 struct devfreq *df = to_devfreq(d);
1000 struct devfreq *devfreq = to_devfreq(dev);
1003 unsigned int max_state = devfreq->profile->max_state;
1005 if (!devfreq->stop_polling &&
1006 devfreq_update_status(devfreq, devfreq->previous_freq))
1013 devfreq->profile->freq_table[i]);
1018 if (devfreq->profile->freq_table[i]
1019 == devfreq->previous_freq) {
1025 devfreq->profile->freq_table[i]);
1028 devfreq->trans_table[(i * max_state) + j]);
1030 jiffies_to_msecs(devfreq->time_in_state[i]));
1034 devfreq->total_trans);
1051 ATTRIBUTE_GROUPS(devfreq);
1055 devfreq_class = class_create(THIS_MODULE, "devfreq");
1081 * The followings are helper functions for devfreq user device drivers with
1088 * @dev: The devfreq user device. (parent of devfreq)
1090 * @flags: Flags handed from devfreq framework.
1125 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1128 * @dev: The devfreq user device. (parent of devfreq)
1129 * @devfreq: The devfreq object.
1131 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1142 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1149 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1152 * @dev: The devfreq user device. (parent of devfreq)
1153 * @devfreq: The devfreq object.
1158 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1169 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1177 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1183 * @dev: The devfreq user device. (parent of devfreq)
1184 * @devfreq: The devfreq object.
1187 struct devfreq *devfreq)
1189 struct devfreq **ptr;
1196 ret = devfreq_register_opp_notifier(dev, devfreq);
1202 *ptr = devfreq;
1212 * @dev: The devfreq user device. (parent of devfreq)
1213 * @devfreq: The devfreq object.
1216 struct devfreq *devfreq)
1219 devm_devfreq_dev_match, devfreq));
1224 MODULE_DESCRIPTION("devfreq class support");