2 * Generic OPP helper interface for CPU device
4 * Copyright (C) 2009-2014 Texas Instruments Incorporated.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/cpu.h>
14 #include <linux/cpufreq.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/export.h>
19 #include <linux/slab.h>
23 #ifdef CONFIG_CPU_FREQ
26 * dev_pm_opp_init_cpufreq_table() - create a cpufreq table for a device
27 * @dev: device for which we do this operation
28 * @table: Cpufreq table returned back to caller
30 * Generate a cpufreq table for a provided device- this assumes that the
31 * opp list is already initialized and ready for usage.
33 * This function allocates required memory for the cpufreq table. It is
34 * expected that the caller does the required maintenance such as freeing
35 * the table as required.
37 * Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM
38 * if no memory available for the operation (table is not populated), returns 0
39 * if successful and table is populated.
41 * WARNING: It is important for the callers to ensure refreshing their copy of
42 * the table if any of the mentioned functions have been invoked in the interim.
44 * Locking: The internal device_opp and opp structures are RCU protected.
45 * Since we just use the regular accessor functions to access the internal data
46 * structures, we use RCU read lock inside this function. As a result, users of
47 * this function DONOT need to use explicit locks for invoking.
49 int dev_pm_opp_init_cpufreq_table(struct device *dev,
50 struct cpufreq_frequency_table **table)
52 struct dev_pm_opp *opp;
53 struct cpufreq_frequency_table *freq_table = NULL;
54 int i, max_opps, ret = 0;
59 max_opps = dev_pm_opp_get_opp_count(dev);
61 ret = max_opps ? max_opps : -ENODATA;
65 freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_ATOMIC);
71 for (i = 0, rate = 0; i < max_opps; i++, rate++) {
73 opp = dev_pm_opp_find_freq_ceil(dev, &rate);
78 freq_table[i].driver_data = i;
79 freq_table[i].frequency = rate / 1000;
81 /* Is Boost/turbo opp ? */
82 if (dev_pm_opp_is_turbo(opp))
83 freq_table[i].flags = CPUFREQ_BOOST_FREQ;
86 freq_table[i].driver_data = i;
87 freq_table[i].frequency = CPUFREQ_TABLE_END;
89 *table = &freq_table[0];
98 EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table);
101 * dev_pm_opp_free_cpufreq_table() - free the cpufreq table
102 * @dev: device for which we do this operation
103 * @table: table to free
105 * Free up the table allocated by dev_pm_opp_init_cpufreq_table
107 void dev_pm_opp_free_cpufreq_table(struct device *dev,
108 struct cpufreq_frequency_table **table)
116 EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table);
117 #endif /* CONFIG_CPU_FREQ */
119 /* Required only for V1 bindings, as v2 can manage it from DT itself */
120 int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask)
122 struct device_list_opp *list_dev;
123 struct device_opp *dev_opp;
129 dev_opp = _find_device_opp(cpu_dev);
130 if (IS_ERR(dev_opp)) {
132 goto out_rcu_read_unlock;
135 for_each_cpu(cpu, cpumask) {
136 if (cpu == cpu_dev->id)
139 dev = get_cpu_device(cpu);
141 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
146 list_dev = _add_list_dev(dev, dev_opp);
148 dev_err(dev, "%s: failed to add list-dev for cpu%d device\n",
158 EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus);
161 void dev_pm_opp_of_cpumask_remove_table(cpumask_var_t cpumask)
163 struct device *cpu_dev;
166 WARN_ON(cpumask_empty(cpumask));
168 for_each_cpu(cpu, cpumask) {
169 cpu_dev = get_cpu_device(cpu);
171 pr_err("%s: failed to get cpu%d device\n", __func__,
176 dev_pm_opp_of_remove_table(cpu_dev);
179 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
181 int dev_pm_opp_of_cpumask_add_table(cpumask_var_t cpumask)
183 struct device *cpu_dev;
186 WARN_ON(cpumask_empty(cpumask));
188 for_each_cpu(cpu, cpumask) {
189 cpu_dev = get_cpu_device(cpu);
191 pr_err("%s: failed to get cpu%d device\n", __func__,
196 ret = dev_pm_opp_of_add_table(cpu_dev);
198 pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
201 /* Free all other OPPs */
202 dev_pm_opp_of_cpumask_remove_table(cpumask);
209 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
212 * Works only for OPP v2 bindings.
214 * cpumask should be already set to mask of cpu_dev->id.
215 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
217 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask)
219 struct device_node *np, *tmp_np;
220 struct device *tcpu_dev;
223 /* Get OPP descriptor node */
224 np = _of_get_opp_desc_node(cpu_dev);
226 dev_dbg(cpu_dev, "%s: Couldn't find cpu_dev node.\n", __func__);
230 /* OPPs are shared ? */
231 if (!of_property_read_bool(np, "opp-shared"))
234 for_each_possible_cpu(cpu) {
235 if (cpu == cpu_dev->id)
238 tcpu_dev = get_cpu_device(cpu);
240 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
246 /* Get OPP descriptor node */
247 tmp_np = _of_get_opp_desc_node(tcpu_dev);
249 dev_err(tcpu_dev, "%s: Couldn't find tcpu_dev node.\n",
255 /* CPUs are sharing opp node */
257 cpumask_set_cpu(cpu, cpumask);
266 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);