^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * for Non-CPU Devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2011 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * MyungJoo Ham <myungjoo.ham@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kmod.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/pm_opp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/devfreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/pm_qos.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "governor.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <trace/events/devfreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define HZ_PER_KHZ 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static struct class *devfreq_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static struct dentry *devfreq_debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * devfreq core provides delayed work based load monitoring helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * functions. Governors can use these or can implement their own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * monitoring mechanism.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static struct workqueue_struct *devfreq_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* The list of all device-devfreq governors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static LIST_HEAD(devfreq_governor_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* The list of all device-devfreq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static LIST_HEAD(devfreq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static DEFINE_MUTEX(devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static const char timer_name[][DEVFREQ_NAME_LEN] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) [DEVFREQ_TIMER_DELAYED] = { "delayed" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * find_device_devfreq() - find devfreq struct using device pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @dev: device pointer used to lookup device devfreq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * Search the list of device devfreqs and return the matched device's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * devfreq info. devfreq_list_lock should be held by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static struct devfreq *find_device_devfreq(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct devfreq *tmp_devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) lockdep_assert_held(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (IS_ERR_OR_NULL(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (tmp_devfreq->dev.parent == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return tmp_devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static unsigned long find_available_min_freq(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct dev_pm_opp *opp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned long min_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (IS_ERR(opp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) min_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) dev_pm_opp_put(opp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return min_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static unsigned long find_available_max_freq(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct dev_pm_opp *opp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned long max_freq = ULONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (IS_ERR(opp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) max_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) dev_pm_opp_put(opp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * get_freq_range() - Get the current freq range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * @devfreq: the devfreq instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * @min_freq: the min frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * @max_freq: the max frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * This takes into consideration all constraints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void get_freq_range(struct devfreq *devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) unsigned long *min_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unsigned long *max_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned long *freq_table = devfreq->profile->freq_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) s32 qos_min_freq, qos_max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) lockdep_assert_held(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * Initialize minimum/maximum frequency from freq table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * The devfreq drivers can initialize this in either ascending or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * descending order and devfreq core supports both.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (freq_table[0] < freq_table[devfreq->profile->max_state - 1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) *min_freq = freq_table[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) *max_freq = freq_table[devfreq->profile->max_state - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) *min_freq = freq_table[devfreq->profile->max_state - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *max_freq = freq_table[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Apply constraints from PM QoS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) DEV_PM_QOS_MIN_FREQUENCY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) DEV_PM_QOS_MAX_FREQUENCY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) *max_freq = min(*max_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) (unsigned long)HZ_PER_KHZ * qos_max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Apply constraints from OPP interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *min_freq = max(*min_freq, devfreq->scaling_min_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *max_freq = min(*max_freq, devfreq->scaling_max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (*min_freq > *max_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) *min_freq = *max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * devfreq_get_freq_level() - Lookup freq_table for the frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @devfreq: the devfreq instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * @freq: the target frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int lev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) for (lev = 0; lev < devfreq->profile->max_state; lev++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (freq == devfreq->profile->freq_table[lev])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return lev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int set_freq_table(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct devfreq_dev_profile *profile = devfreq->profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct dev_pm_opp *opp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) unsigned long freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) int i, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* Initialize the freq_table from OPP table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (count <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) profile->max_state = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) profile->freq_table = devm_kcalloc(devfreq->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) profile->max_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) sizeof(*profile->freq_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (!profile->freq_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) profile->max_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (IS_ERR(opp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) devm_kfree(devfreq->dev.parent, profile->freq_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) profile->max_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return PTR_ERR(opp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) dev_pm_opp_put(opp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) profile->freq_table[i] = freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * devfreq_update_status() - Update statistics of devfreq behavior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * @devfreq: the devfreq instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * @freq: the update target frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int lev, prev_lev, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) u64 cur_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) lockdep_assert_held(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) cur_time = get_jiffies_64();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* Immediately exit if previous_freq is not initialized yet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (!devfreq->previous_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (prev_lev < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) ret = prev_lev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) devfreq->stats.time_in_state[prev_lev] +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) cur_time - devfreq->stats.last_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) lev = devfreq_get_freq_level(devfreq, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (lev < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) ret = lev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (lev != prev_lev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) devfreq->stats.trans_table[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) (prev_lev * devfreq->profile->max_state) + lev]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) devfreq->stats.total_trans++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) devfreq->stats.last_update = cur_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) EXPORT_SYMBOL(devfreq_update_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * find_devfreq_governor() - find devfreq governor from name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * @name: name of the governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * Search the list of devfreq governors and return the matched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * governor's pointer. devfreq_list_lock should be held by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static struct devfreq_governor *find_devfreq_governor(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct devfreq_governor *tmp_governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) lockdep_assert_held(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (IS_ERR_OR_NULL(name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return tmp_governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * try_then_request_governor() - Try to find the governor and request the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * module if is not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * @name: name of the governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * Search the list of devfreq governors and request the module and try again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * if is not found. This can happen when both drivers (the governor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * and the driver that call devfreq_add_device) are built as modules.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * devfreq_list_lock should be held by the caller. Returns the matched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * governor's pointer or an error pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static struct devfreq_governor *try_then_request_governor(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct devfreq_governor *governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) lockdep_assert_held(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (IS_ERR_OR_NULL(name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) governor = find_devfreq_governor(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (IS_ERR(governor)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) DEVFREQ_NAME_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) err = request_module("governor_%s", "simpleondemand");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) err = request_module("governor_%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* Restore previous state before return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) governor = find_devfreq_governor(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int devfreq_notify_transition(struct devfreq *devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct devfreq_freqs *freqs, unsigned int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) case DEVFREQ_PRECHANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) srcu_notifier_call_chain(&devfreq->transition_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) DEVFREQ_PRECHANGE, freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) case DEVFREQ_POSTCHANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) srcu_notifier_call_chain(&devfreq->transition_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) DEVFREQ_POSTCHANGE, freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct devfreq_freqs freqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) unsigned long cur_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (devfreq->profile->get_cur_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) cur_freq = devfreq->previous_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) freqs.old = cur_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) freqs.new = new_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) freqs.new = cur_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) freqs.new = new_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (devfreq_update_status(devfreq, new_freq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) dev_err(&devfreq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) "Couldn't update frequency transition information.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) devfreq->previous_freq = new_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (devfreq->suspend_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) devfreq->resume_freq = new_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* Load monitoring helper functions for governors use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * update_devfreq() - Reevaluate the device and configure frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * @devfreq: the devfreq instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * Note: Lock devfreq->lock before calling update_devfreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * This function is exported for governors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int update_devfreq(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) unsigned long freq, min_freq, max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) u32 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) lockdep_assert_held(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (!devfreq->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /* Reevaluate the proper frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) err = devfreq->governor->get_target_freq(devfreq, &freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) get_freq_range(devfreq, &min_freq, &max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (freq < min_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) freq = min_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (freq > max_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) freq = max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return devfreq_set_target(devfreq, freq, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) EXPORT_SYMBOL(update_devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * devfreq_monitor() - Periodically poll devfreq objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * @work: the work struct used to run devfreq_monitor periodically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static void devfreq_monitor(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct devfreq *devfreq = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) struct devfreq, work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) err = update_devfreq(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) queue_delayed_work(devfreq_wq, &devfreq->work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) msecs_to_jiffies(devfreq->profile->polling_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) trace_devfreq_monitor(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * devfreq_monitor_start() - Start load monitoring of devfreq instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * @devfreq: the devfreq instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * Helper function for starting devfreq device load monitoring. By
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * default delayed work based monitoring is supported. Function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * to be called from governor in response to DEVFREQ_GOV_START
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * event when device is added to devfreq framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) void devfreq_monitor_start(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (devfreq->governor->interrupt_driven)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) switch (devfreq->profile->timer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) case DEVFREQ_TIMER_DEFERRABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) case DEVFREQ_TIMER_DELAYED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (devfreq->profile->polling_ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) queue_delayed_work(devfreq_wq, &devfreq->work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) msecs_to_jiffies(devfreq->profile->polling_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) EXPORT_SYMBOL(devfreq_monitor_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * @devfreq: the devfreq instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * Helper function to stop devfreq device load monitoring. Function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * to be called from governor in response to DEVFREQ_GOV_STOP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * event when device is removed from devfreq framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) void devfreq_monitor_stop(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (devfreq->governor->interrupt_driven)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) cancel_delayed_work_sync(&devfreq->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) EXPORT_SYMBOL(devfreq_monitor_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * @devfreq: the devfreq instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * Helper function to suspend devfreq device load monitoring. Function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * to be called from governor in response to DEVFREQ_GOV_SUSPEND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * event or when polling interval is set to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * Note: Though this function is same as devfreq_monitor_stop(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * intentionally kept separate to provide hooks for collecting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * transition statistics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) void devfreq_monitor_suspend(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (devfreq->stop_polling) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) devfreq_update_status(devfreq, devfreq->previous_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) devfreq->stop_polling = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (devfreq->governor->interrupt_driven)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) cancel_delayed_work_sync(&devfreq->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) EXPORT_SYMBOL(devfreq_monitor_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * @devfreq: the devfreq instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * Helper function to resume devfreq device load monitoring. Function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * to be called from governor in response to DEVFREQ_GOV_RESUME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * event or when polling interval is set to non-zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) void devfreq_monitor_resume(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) unsigned long freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (!devfreq->stop_polling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (devfreq->governor->interrupt_driven)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) goto out_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (!delayed_work_pending(&devfreq->work) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) devfreq->profile->polling_ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) queue_delayed_work(devfreq_wq, &devfreq->work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) msecs_to_jiffies(devfreq->profile->polling_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) out_update:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) devfreq->stats.last_update = get_jiffies_64();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) devfreq->stop_polling = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (devfreq->profile->get_cur_freq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) devfreq->previous_freq = freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) EXPORT_SYMBOL(devfreq_monitor_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * devfreq_update_interval() - Update device devfreq monitoring interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * @devfreq: the devfreq instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * @delay: new polling interval to be set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * Helper function to set new load monitoring polling interval. Function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) unsigned int cur_delay = devfreq->profile->polling_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) unsigned int new_delay = *delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) devfreq->profile->polling_ms = new_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (devfreq->stop_polling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (devfreq->governor->interrupt_driven)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /* if new delay is zero, stop polling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!new_delay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) cancel_delayed_work_sync(&devfreq->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) /* if current delay is zero, start polling with new delay */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (!cur_delay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) queue_delayed_work(devfreq_wq, &devfreq->work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) msecs_to_jiffies(devfreq->profile->polling_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) /* if current delay is greater than new delay, restart polling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (cur_delay > new_delay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) cancel_delayed_work_sync(&devfreq->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (!devfreq->stop_polling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) queue_delayed_work(devfreq_wq, &devfreq->work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) msecs_to_jiffies(devfreq->profile->polling_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) EXPORT_SYMBOL(devfreq_update_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * devfreq_notifier_call() - Notify that the device frequency requirements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * has been changed out of devfreq framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * @nb: the notifier_block (supposed to be devfreq->nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * @type: not used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * @devp: not used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * Called by a notifier that uses devfreq->nb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) void *devp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) devfreq->scaling_min_freq = find_available_min_freq(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) if (!devfreq->scaling_min_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) devfreq->scaling_max_freq = find_available_max_freq(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (!devfreq->scaling_max_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) devfreq->scaling_max_freq = ULONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) err = update_devfreq(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) dev_err(devfreq->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) "failed to update frequency from OPP notifier (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * qos_notifier_call() - Common handler for QoS constraints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * @devfreq: the devfreq instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static int qos_notifier_call(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) err = update_devfreq(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) dev_err(devfreq->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) "failed to update frequency from PM QoS (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * qos_min_notifier_call() - Callback for QoS min_freq changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * @nb: Should be devfreq->nb_min
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) static int qos_min_notifier_call(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) unsigned long val, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) return qos_notifier_call(container_of(nb, struct devfreq, nb_min));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * qos_max_notifier_call() - Callback for QoS max_freq changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * @nb: Should be devfreq->nb_max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) static int qos_max_notifier_call(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) unsigned long val, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) return qos_notifier_call(container_of(nb, struct devfreq, nb_max));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * devfreq_dev_release() - Callback for struct device to release the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * @dev: the devfreq device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * Remove devfreq from the list and release its resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) static void devfreq_dev_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) struct devfreq *devfreq = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) list_del(&devfreq->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) DEV_PM_QOS_MAX_FREQUENCY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (err && err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) dev_warn(dev->parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) "Failed to remove max_freq notifier: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) DEV_PM_QOS_MIN_FREQUENCY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (err && err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) dev_warn(dev->parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) "Failed to remove min_freq notifier: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) dev_warn(dev->parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) "Failed to remove max_freq request: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) dev_warn(dev->parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) "Failed to remove min_freq request: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (devfreq->profile->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) devfreq->profile->exit(devfreq->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) mutex_destroy(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) kfree(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * devfreq_add_device() - Add devfreq feature to the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * @dev: the device to add devfreq feature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) * @profile: device-specific profile to run devfreq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * @governor_name: name of the policy to choose frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) * @data: private data for the governor. The devfreq framework does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) * touch this value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) struct devfreq *devfreq_add_device(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) struct devfreq_dev_profile *profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) const char *governor_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) struct devfreq_governor *governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (!dev || !profile || !governor_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) dev_err(dev, "%s: Invalid parameters.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) devfreq = find_device_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (!IS_ERR(devfreq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) dev_err(dev, "%s: devfreq device already exists!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (!devfreq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) mutex_init(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) devfreq->dev.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) devfreq->dev.class = devfreq_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) devfreq->dev.release = devfreq_dev_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) INIT_LIST_HEAD(&devfreq->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) devfreq->profile = profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) strscpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) devfreq->previous_freq = profile->initial_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) devfreq->last_status.current_frequency = profile->initial_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) devfreq->data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) devfreq->nb.notifier_call = devfreq_notifier_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) if (devfreq->profile->timer < 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) goto err_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) err = set_freq_table(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) goto err_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) devfreq->scaling_min_freq = find_available_min_freq(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (!devfreq->scaling_min_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) goto err_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) devfreq->scaling_max_freq = find_available_max_freq(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (!devfreq->scaling_max_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) goto err_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) atomic_set(&devfreq->suspend_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) dev_set_name(&devfreq->dev, "%s", dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) err = device_register(&devfreq->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) put_device(&devfreq->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) array3_size(sizeof(unsigned int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) devfreq->profile->max_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) devfreq->profile->max_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (!devfreq->stats.trans_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) goto err_devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) devfreq->profile->max_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) sizeof(*devfreq->stats.time_in_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (!devfreq->stats.time_in_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) goto err_devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) devfreq->stats.total_trans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) devfreq->stats.last_update = get_jiffies_64();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) srcu_init_notifier_head(&devfreq->transition_notifier_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) DEV_PM_QOS_MIN_FREQUENCY, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) goto err_devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) DEV_PM_QOS_MAX_FREQUENCY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) goto err_devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) devfreq->nb_min.notifier_call = qos_min_notifier_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) DEV_PM_QOS_MIN_FREQUENCY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) goto err_devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) devfreq->nb_max.notifier_call = qos_max_notifier_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) DEV_PM_QOS_MAX_FREQUENCY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) goto err_devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) governor = try_then_request_governor(devfreq->governor_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (IS_ERR(governor)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) dev_err(dev, "%s: Unable to find governor for the device\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) err = PTR_ERR(governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) goto err_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) devfreq->governor = governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) dev_err(dev, "%s: Unable to start governor for the device\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) goto err_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) list_add(&devfreq->node, &devfreq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) return devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) err_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) err_devfreq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) devfreq_remove_device(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) devfreq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) err_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) kfree(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) EXPORT_SYMBOL(devfreq_add_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) * devfreq_remove_device() - Remove devfreq feature from a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) * @devfreq: the devfreq instance to be removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) * The opposite of devfreq_add_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) int devfreq_remove_device(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (!devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (devfreq->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) devfreq->governor->event_handler(devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) DEVFREQ_GOV_STOP, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) device_unregister(&devfreq->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) EXPORT_SYMBOL(devfreq_remove_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) struct devfreq **r = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (WARN_ON(!r || !*r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) return *r == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) static void devm_devfreq_dev_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) devfreq_remove_device(*(struct devfreq **)res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * @dev: the device to add devfreq feature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * @profile: device-specific profile to run devfreq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) * @governor_name: name of the policy to choose frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * @data: private data for the governor. The devfreq framework does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) * touch this value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) * This function manages automatically the memory of devfreq device using device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * resource management and simplify the free operation for memory of devfreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) * device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) struct devfreq *devm_devfreq_add_device(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) struct devfreq_dev_profile *profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) const char *governor_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) struct devfreq **ptr, *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) devfreq = devfreq_add_device(dev, profile, governor_name, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if (IS_ERR(devfreq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) return devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) *ptr = devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) return devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) EXPORT_SYMBOL(devm_devfreq_add_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) * @node - pointer to device_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * return the instance of devfreq device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) list_for_each_entry(devfreq, &devfreq_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) if (devfreq->dev.parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) && devfreq->dev.parent->of_node == node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) return devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) * @dev - instance to the given device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) * @phandle_name - name of property holding a phandle value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) * @index - index into list of devfreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) * return the instance of devfreq device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) const char *phandle_name, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (!dev || !phandle_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (!dev->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) node = of_parse_phandle(dev->of_node, phandle_name, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) devfreq = devfreq_get_devfreq_by_node(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) return devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) const char *phandle_name, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) #endif /* CONFIG_OF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * @dev: the device from which to remove devfreq feature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) * @devfreq: the devfreq instance to be removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) WARN_ON(devres_release(dev, devm_devfreq_dev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) devm_devfreq_dev_match, devfreq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) EXPORT_SYMBOL(devm_devfreq_remove_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) * devfreq_suspend_device() - Suspend devfreq of a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) * @devfreq: the devfreq instance to be suspended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * This function is intended to be called by the pm callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) * (e.g., runtime_suspend, suspend) of the device driver that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) * holds the devfreq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) int devfreq_suspend_device(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) if (!devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (atomic_inc_return(&devfreq->suspend_count) > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) if (devfreq->governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) ret = devfreq->governor->event_handler(devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) DEVFREQ_GOV_SUSPEND, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (devfreq->suspend_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) EXPORT_SYMBOL(devfreq_suspend_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) * devfreq_resume_device() - Resume devfreq of a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) * @devfreq: the devfreq instance to be resumed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) * This function is intended to be called by the pm callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) * (e.g., runtime_resume, resume) of the device driver that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) * holds the devfreq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) int devfreq_resume_device(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) if (!devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) if (atomic_dec_return(&devfreq->suspend_count) >= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) if (devfreq->resume_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) if (devfreq->governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) ret = devfreq->governor->event_handler(devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) DEVFREQ_GOV_RESUME, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) EXPORT_SYMBOL(devfreq_resume_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) * devfreq_suspend() - Suspend devfreq governors and devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) * Called during system wide Suspend/Hibernate cycles for suspending governors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) * and devices preserving the state for resume. On some platforms the devfreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) * device must have precise state (frequency) after resume in order to provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) * fully operating setup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) void devfreq_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) list_for_each_entry(devfreq, &devfreq_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) ret = devfreq_suspend_device(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) dev_err(&devfreq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) "failed to suspend devfreq device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * devfreq_resume() - Resume devfreq governors and devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * Called during system wide Suspend/Hibernate cycle for resuming governors and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) * devices that are suspended with devfreq_suspend().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) void devfreq_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) list_for_each_entry(devfreq, &devfreq_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) ret = devfreq_resume_device(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) dev_warn(&devfreq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) "failed to resume devfreq device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) * devfreq_add_governor() - Add devfreq governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) * @governor: the devfreq governor to be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) int devfreq_add_governor(struct devfreq_governor *governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) struct devfreq_governor *g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) if (!governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) pr_err("%s: Invalid parameters.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) g = find_devfreq_governor(governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) if (!IS_ERR(g)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) pr_err("%s: governor %s already registered\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) g->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) list_add(&governor->node, &devfreq_governor_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) list_for_each_entry(devfreq, &devfreq_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) struct device *dev = devfreq->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) if (!strncmp(devfreq->governor_name, governor->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) DEVFREQ_NAME_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) /* The following should never occur */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) if (devfreq->governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) dev_warn(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) "%s: Governor %s already present\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) __func__, devfreq->governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) ret = devfreq->governor->event_handler(devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) DEVFREQ_GOV_STOP, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) dev_warn(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) "%s: Governor %s stop = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) devfreq->governor->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) /* Fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) devfreq->governor = governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) ret = devfreq->governor->event_handler(devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) DEVFREQ_GOV_START, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) dev_warn(dev, "%s: Governor %s start=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) __func__, devfreq->governor->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) EXPORT_SYMBOL(devfreq_add_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) * devfreq_remove_governor() - Remove devfreq feature from a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) * @governor: the devfreq governor to be removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) int devfreq_remove_governor(struct devfreq_governor *governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) struct devfreq_governor *g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (!governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) pr_err("%s: Invalid parameters.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) g = find_devfreq_governor(governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) if (IS_ERR(g)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) pr_err("%s: governor %s not registered\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) err = PTR_ERR(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) list_for_each_entry(devfreq, &devfreq_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) struct device *dev = devfreq->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) if (!strncmp(devfreq->governor_name, governor->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) DEVFREQ_NAME_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) /* we should have a devfreq governor! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) if (!devfreq->governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) dev_warn(dev, "%s: Governor %s NOT present\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) __func__, governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) /* Fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) ret = devfreq->governor->event_handler(devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) DEVFREQ_GOV_STOP, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) dev_warn(dev, "%s: Governor %s stop=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) __func__, devfreq->governor->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) devfreq->governor = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) list_del(&governor->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) EXPORT_SYMBOL(devfreq_remove_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) static ssize_t name_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) return sprintf(buf, "%s\n", dev_name(df->dev.parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) static DEVICE_ATTR_RO(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) static ssize_t governor_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) if (!df->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) return sprintf(buf, "%s\n", df->governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) char str_governor[DEVFREQ_NAME_LEN + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) const struct devfreq_governor *governor, *prev_governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) if (!df->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) governor = try_then_request_governor(str_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) if (IS_ERR(governor)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) ret = PTR_ERR(governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) if (df->governor == governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) } else if (df->governor->immutable || governor->immutable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) __func__, df->governor->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) prev_governor = df->governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) df->governor = governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) dev_warn(dev, "%s: Governor %s not started(%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) __func__, df->governor->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) df->governor = prev_governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) strncpy(df->governor_name, prev_governor->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) DEVFREQ_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) "%s: reverting to Governor %s failed (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) __func__, df->governor_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) df->governor = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) static DEVICE_ATTR_RW(governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) static ssize_t available_governors_show(struct device *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) struct devfreq *df = to_devfreq(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) ssize_t count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) if (!df->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) * The devfreq with immutable governor (e.g., passive) shows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) * only own governor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) if (df->governor->immutable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) "%s ", df->governor_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) * The devfreq device shows the registered governor except for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) * immutable governors such as passive governor .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) struct devfreq_governor *governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) list_for_each_entry(governor, &devfreq_governor_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) if (governor->immutable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) "%s ", governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) /* Truncate the trailing space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) if (count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) count += sprintf(&buf[count], "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) static DEVICE_ATTR_RO(available_governors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) unsigned long freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) if (!df->profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) if (df->profile->get_cur_freq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) !df->profile->get_cur_freq(df->dev.parent, &freq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) return sprintf(buf, "%lu\n", freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) return sprintf(buf, "%lu\n", df->previous_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) static DEVICE_ATTR_RO(cur_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) static ssize_t target_freq_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) return sprintf(buf, "%lu\n", df->previous_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) static DEVICE_ATTR_RO(target_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) static ssize_t polling_interval_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) if (!df->profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) return sprintf(buf, "%d\n", df->profile->polling_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) static ssize_t polling_interval_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) unsigned int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) if (!df->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) ret = sscanf(buf, "%u", &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) static DEVICE_ATTR_RW(polling_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) unsigned long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) * Protect against theoretical sysfs writes between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) * device_add and dev_pm_qos_add_request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) if (!dev_pm_qos_request_active(&df->user_min_freq_req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) ret = sscanf(buf, "%lu", &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) /* Round down to kHz for PM QoS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) ret = dev_pm_qos_update_request(&df->user_min_freq_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) value / HZ_PER_KHZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) unsigned long min_freq, max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) mutex_lock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) get_freq_range(df, &min_freq, &max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) mutex_unlock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) return sprintf(buf, "%lu\n", min_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) static DEVICE_ATTR_RW(min_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) unsigned long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) * Protect against theoretical sysfs writes between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) * device_add and dev_pm_qos_add_request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) if (!dev_pm_qos_request_active(&df->user_max_freq_req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) ret = sscanf(buf, "%lu", &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) * PM QoS frequencies are in kHz so we need to convert. Convert by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) * rounding upwards so that the acceptable interval never shrinks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) * For example if the user writes "666666666" to sysfs this value will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) * be converted to 666667 kHz and back to 666667000 Hz before an OPP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) * lookup, this ensures that an OPP of 666666666Hz is still accepted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) * A value of zero means "no limit".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) value = DIV_ROUND_UP(value, HZ_PER_KHZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) unsigned long min_freq, max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) mutex_lock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) get_freq_range(df, &min_freq, &max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) mutex_unlock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) return sprintf(buf, "%lu\n", max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) static DEVICE_ATTR_RW(max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) static ssize_t available_frequencies_show(struct device *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) struct devfreq *df = to_devfreq(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) ssize_t count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) if (!df->profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) mutex_lock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) for (i = 0; i < df->profile->max_state; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) "%lu ", df->profile->freq_table[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) mutex_unlock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) /* Truncate the trailing space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) if (count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) count += sprintf(&buf[count], "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) static DEVICE_ATTR_RO(available_frequencies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) static ssize_t trans_stat_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) ssize_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) unsigned int max_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) if (!df->profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) max_state = df->profile->max_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) if (max_state == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) return sprintf(buf, "Not Supported.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) mutex_lock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) if (!df->stop_polling &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) devfreq_update_status(df, df->previous_freq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) mutex_unlock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) mutex_unlock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) len = sprintf(buf, " From : To\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) len += sprintf(buf + len, " :");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) for (i = 0; i < max_state; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) len += sprintf(buf + len, "%10lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) df->profile->freq_table[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) len += sprintf(buf + len, " time(ms)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) for (i = 0; i < max_state; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) if (df->profile->freq_table[i]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) == df->previous_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) len += sprintf(buf + len, "*");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) len += sprintf(buf + len, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) len += sprintf(buf + len, "%10lu:",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) df->profile->freq_table[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) for (j = 0; j < max_state; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) len += sprintf(buf + len, "%10u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) df->stats.trans_table[(i * max_state) + j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) len += sprintf(buf + len, "%10llu\n", (u64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) jiffies64_to_msecs(df->stats.time_in_state[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) len += sprintf(buf + len, "Total transition : %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) df->stats.total_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) static ssize_t trans_stat_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) int err, value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) if (!df->profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) if (df->profile->max_state == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) err = kstrtoint(buf, 10, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) if (err || value != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) mutex_lock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) memset(df->stats.time_in_state, 0, (df->profile->max_state *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) sizeof(*df->stats.time_in_state)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) df->profile->max_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) df->profile->max_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) df->stats.total_trans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) df->stats.last_update = get_jiffies_64();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) mutex_unlock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) static DEVICE_ATTR_RW(trans_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) static ssize_t timer_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) if (!df->profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) struct devfreq *df = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) char str_timer[DEVFREQ_NAME_LEN + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) int timer = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) int ret = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) if (!df->governor || !df->profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) ret = sscanf(buf, "%16s", str_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) timer = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) if (timer < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) if (df->profile->timer == timer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) mutex_lock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) df->profile->timer = timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) mutex_unlock(&df->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) __func__, df->governor->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) dev_warn(dev, "%s: Governor %s not started(%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) __func__, df->governor->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) return ret ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) static DEVICE_ATTR_RW(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) static ssize_t load_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) struct devfreq *devfreq = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) struct devfreq_dev_status stat = devfreq->last_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) unsigned long freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) ssize_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) err = devfreq_update_stats(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) if (stat.total_time < stat.busy_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) err = devfreq_update_stats(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) if (!stat.total_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) len = sprintf(buf, "%lu", stat.busy_time * 100 / stat.total_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) if (devfreq->profile->get_cur_freq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) len += sprintf(buf + len, "@%luHz\n", freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) len += sprintf(buf + len, "@%luHz\n", devfreq->previous_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) static DEVICE_ATTR_RO(load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) static struct attribute *devfreq_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) &dev_attr_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) &dev_attr_governor.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) &dev_attr_available_governors.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) &dev_attr_cur_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) &dev_attr_available_frequencies.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) &dev_attr_target_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) &dev_attr_polling_interval.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) &dev_attr_min_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) &dev_attr_max_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) &dev_attr_trans_stat.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) &dev_attr_timer.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) &dev_attr_load.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) ATTRIBUTE_GROUPS(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) * devfreq_summary_show() - Show the summary of the devfreq devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) * @s: seq_file instance to show the summary of devfreq devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) * @data: not used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) * It helps that user can know the detailed information of the devfreq devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) * Return 0 always because it shows the information without any data change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) static int devfreq_summary_show(struct seq_file *s, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) struct devfreq *p_devfreq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) unsigned long cur_freq, min_freq, max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) unsigned int polling_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) unsigned int timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) "dev",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) "parent_dev",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) "governor",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) "timer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) "polling_ms",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) "cur_freq_Hz",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) "min_freq_Hz",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) "max_freq_Hz");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) "------------------------------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) "------------------------------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) "---------------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) "----------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) "----------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) "------------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) "------------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) "------------");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) mutex_lock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) if (!strncmp(devfreq->governor_name, DEVFREQ_GOV_PASSIVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) DEVFREQ_NAME_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) struct devfreq_passive_data *data = devfreq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) if (data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) p_devfreq = data->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) p_devfreq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) cur_freq = devfreq->previous_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) get_freq_range(devfreq, &min_freq, &max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) polling_ms = devfreq->profile->polling_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) timer = devfreq->profile->timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) seq_printf(s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) dev_name(&devfreq->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) p_devfreq ? dev_name(&p_devfreq->dev) : "null",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) devfreq->governor_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) polling_ms ? timer_name[timer] : "null",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) polling_ms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) cur_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) min_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) mutex_unlock(&devfreq_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) static int __init devfreq_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) devfreq_class = class_create(THIS_MODULE, "devfreq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) if (IS_ERR(devfreq_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) pr_err("%s: couldn't create class\n", __FILE__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) return PTR_ERR(devfreq_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) devfreq_wq = create_freezable_workqueue("devfreq_wq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) if (!devfreq_wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) class_destroy(devfreq_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) pr_err("%s: couldn't create workqueue\n", __FILE__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) devfreq_class->dev_groups = devfreq_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) debugfs_create_file("devfreq_summary", 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) devfreq_debugfs, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) &devfreq_summary_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) subsys_initcall(devfreq_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) * The following are helper functions for devfreq user device drivers with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) * OPP framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) * devfreq_recommended_opp() - Helper function to get proper OPP for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) * freq value given to target callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) * @dev: The devfreq user device. (parent of devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) * @freq: The frequency given to target function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) * @flags: Flags handed from devfreq framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) * The callers are required to call dev_pm_opp_put() for the returned OPP after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) * use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) unsigned long *freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) struct dev_pm_opp *opp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) /* The freq is an upper bound. opp should be lower */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) opp = dev_pm_opp_find_freq_floor(dev, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) /* If not available, use the closest opp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) if (opp == ERR_PTR(-ERANGE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) opp = dev_pm_opp_find_freq_ceil(dev, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) /* The freq is an lower bound. opp should be higher */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) opp = dev_pm_opp_find_freq_ceil(dev, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) /* If not available, use the closest opp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) if (opp == ERR_PTR(-ERANGE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) opp = dev_pm_opp_find_freq_floor(dev, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) return opp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) EXPORT_SYMBOL(devfreq_recommended_opp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) * devfreq_register_opp_notifier() - Helper function to get devfreq notified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) * for any changes in the OPP availability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) * changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) * @dev: The devfreq user device. (parent of devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) * @devfreq: The devfreq object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) return dev_pm_opp_register_notifier(dev, &devfreq->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) EXPORT_SYMBOL(devfreq_register_opp_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) * notified for any changes in the OPP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) * availability changes anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) * @dev: The devfreq user device. (parent of devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) * @devfreq: The devfreq object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) * At exit() callback of devfreq_dev_profile, this must be included if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) * devfreq_recommended_opp is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) static void devm_devfreq_opp_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) * devm_devfreq_register_opp_notifier() - Resource-managed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) * devfreq_register_opp_notifier()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) * @dev: The devfreq user device. (parent of devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) * @devfreq: The devfreq object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) int devm_devfreq_register_opp_notifier(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) struct devfreq **ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) ret = devfreq_register_opp_notifier(dev, devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) *ptr = devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) * devm_devfreq_unregister_opp_notifier() - Resource-managed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) * devfreq_unregister_opp_notifier()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) * @dev: The devfreq user device. (parent of devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) * @devfreq: The devfreq object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) void devm_devfreq_unregister_opp_notifier(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) WARN_ON(devres_release(dev, devm_devfreq_opp_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) devm_devfreq_dev_match, devfreq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) * devfreq_register_notifier() - Register a driver with devfreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) * @devfreq: The devfreq object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) * @nb: The notifier block to register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) * @list: DEVFREQ_TRANSITION_NOTIFIER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) int devfreq_register_notifier(struct devfreq *devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) unsigned int list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) if (!devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) switch (list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) case DEVFREQ_TRANSITION_NOTIFIER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) ret = srcu_notifier_chain_register(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) &devfreq->transition_notifier_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) EXPORT_SYMBOL(devfreq_register_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) * devfreq_unregister_notifier() - Unregister a driver with devfreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) * @devfreq: The devfreq object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) * @nb: The notifier block to be unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) * @list: DEVFREQ_TRANSITION_NOTIFIER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) int devfreq_unregister_notifier(struct devfreq *devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) unsigned int list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) if (!devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) switch (list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) case DEVFREQ_TRANSITION_NOTIFIER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) ret = srcu_notifier_chain_unregister(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) &devfreq->transition_notifier_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) EXPORT_SYMBOL(devfreq_unregister_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) struct devfreq_notifier_devres {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) struct notifier_block *nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) unsigned int list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) static void devm_devfreq_notifier_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) struct devfreq_notifier_devres *this = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) * devm_devfreq_register_notifier()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) * - Resource-managed devfreq_register_notifier()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) * @dev: The devfreq user device. (parent of devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) * @devfreq: The devfreq object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) * @nb: The notifier block to be unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) * @list: DEVFREQ_TRANSITION_NOTIFIER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) int devm_devfreq_register_notifier(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) struct devfreq *devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) unsigned int list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) struct devfreq_notifier_devres *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) ret = devfreq_register_notifier(devfreq, nb, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) ptr->devfreq = devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) ptr->nb = nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) ptr->list = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) EXPORT_SYMBOL(devm_devfreq_register_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) * devm_devfreq_unregister_notifier()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) * - Resource-managed devfreq_unregister_notifier()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) * @dev: The devfreq user device. (parent of devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) * @devfreq: The devfreq object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) * @nb: The notifier block to be unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) * @list: DEVFREQ_TRANSITION_NOTIFIER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) void devm_devfreq_unregister_notifier(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) struct devfreq *devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) unsigned int list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) devm_devfreq_dev_match, devfreq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) EXPORT_SYMBOL(devm_devfreq_unregister_notifier);