HID: sensor-hub: Add quirk for Lenovo Yoga 900 with ITE Chips
[cascardo/linux.git] / drivers / base / power / opp / cpu.c
1 /*
2  * Generic OPP helper interface for CPU device
3  *
4  * Copyright (C) 2009-2014 Texas Instruments Incorporated.
5  *      Nishanth Menon
6  *      Romit Dasgupta
7  *      Kevin Hilman
8  *
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.
12  */
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>
18 #include <linux/of.h>
19 #include <linux/slab.h>
20
21 #include "opp.h"
22
23 #ifdef CONFIG_CPU_FREQ
24
25 /**
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
29  *
30  * Generate a cpufreq table for a provided device- this assumes that the
31  * opp list is already initialized and ready for usage.
32  *
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.
36  *
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.
40  *
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.
43  *
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.
48  */
49 int dev_pm_opp_init_cpufreq_table(struct device *dev,
50                                   struct cpufreq_frequency_table **table)
51 {
52         struct dev_pm_opp *opp;
53         struct cpufreq_frequency_table *freq_table = NULL;
54         int i, max_opps, ret = 0;
55         unsigned long rate;
56
57         rcu_read_lock();
58
59         max_opps = dev_pm_opp_get_opp_count(dev);
60         if (max_opps <= 0) {
61                 ret = max_opps ? max_opps : -ENODATA;
62                 goto out;
63         }
64
65         freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_ATOMIC);
66         if (!freq_table) {
67                 ret = -ENOMEM;
68                 goto out;
69         }
70
71         for (i = 0, rate = 0; i < max_opps; i++, rate++) {
72                 /* find next rate */
73                 opp = dev_pm_opp_find_freq_ceil(dev, &rate);
74                 if (IS_ERR(opp)) {
75                         ret = PTR_ERR(opp);
76                         goto out;
77                 }
78                 freq_table[i].driver_data = i;
79                 freq_table[i].frequency = rate / 1000;
80
81                 /* Is Boost/turbo opp ? */
82                 if (dev_pm_opp_is_turbo(opp))
83                         freq_table[i].flags = CPUFREQ_BOOST_FREQ;
84         }
85
86         freq_table[i].driver_data = i;
87         freq_table[i].frequency = CPUFREQ_TABLE_END;
88
89         *table = &freq_table[0];
90
91 out:
92         rcu_read_unlock();
93         if (ret)
94                 kfree(freq_table);
95
96         return ret;
97 }
98 EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table);
99
100 /**
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
104  *
105  * Free up the table allocated by dev_pm_opp_init_cpufreq_table
106  */
107 void dev_pm_opp_free_cpufreq_table(struct device *dev,
108                                    struct cpufreq_frequency_table **table)
109 {
110         if (!table)
111                 return;
112
113         kfree(*table);
114         *table = NULL;
115 }
116 EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table);
117 #endif  /* CONFIG_CPU_FREQ */
118
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)
121 {
122         struct device_list_opp *list_dev;
123         struct device_opp *dev_opp;
124         struct device *dev;
125         int cpu, ret = 0;
126
127         rcu_read_lock();
128
129         dev_opp = _find_device_opp(cpu_dev);
130         if (IS_ERR(dev_opp)) {
131                 ret = -EINVAL;
132                 goto out_rcu_read_unlock;
133         }
134
135         for_each_cpu(cpu, cpumask) {
136                 if (cpu == cpu_dev->id)
137                         continue;
138
139                 dev = get_cpu_device(cpu);
140                 if (!dev) {
141                         dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
142                                 __func__, cpu);
143                         continue;
144                 }
145
146                 list_dev = _add_list_dev(dev, dev_opp);
147                 if (!list_dev) {
148                         dev_err(dev, "%s: failed to add list-dev for cpu%d device\n",
149                                 __func__, cpu);
150                         continue;
151                 }
152         }
153 out_rcu_read_unlock:
154         rcu_read_unlock();
155
156         return 0;
157 }
158 EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus);
159
160 #ifdef CONFIG_OF
161 void dev_pm_opp_of_cpumask_remove_table(cpumask_var_t cpumask)
162 {
163         struct device *cpu_dev;
164         int cpu;
165
166         WARN_ON(cpumask_empty(cpumask));
167
168         for_each_cpu(cpu, cpumask) {
169                 cpu_dev = get_cpu_device(cpu);
170                 if (!cpu_dev) {
171                         pr_err("%s: failed to get cpu%d device\n", __func__,
172                                cpu);
173                         continue;
174                 }
175
176                 dev_pm_opp_of_remove_table(cpu_dev);
177         }
178 }
179 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
180
181 int dev_pm_opp_of_cpumask_add_table(cpumask_var_t cpumask)
182 {
183         struct device *cpu_dev;
184         int cpu, ret = 0;
185
186         WARN_ON(cpumask_empty(cpumask));
187
188         for_each_cpu(cpu, cpumask) {
189                 cpu_dev = get_cpu_device(cpu);
190                 if (!cpu_dev) {
191                         pr_err("%s: failed to get cpu%d device\n", __func__,
192                                cpu);
193                         continue;
194                 }
195
196                 ret = dev_pm_opp_of_add_table(cpu_dev);
197                 if (ret) {
198                         pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
199                                __func__, cpu, ret);
200
201                         /* Free all other OPPs */
202                         dev_pm_opp_of_cpumask_remove_table(cpumask);
203                         break;
204                 }
205         }
206
207         return ret;
208 }
209 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
210
211 /*
212  * Works only for OPP v2 bindings.
213  *
214  * cpumask should be already set to mask of cpu_dev->id.
215  * Returns -ENOENT if operating-points-v2 bindings aren't supported.
216  */
217 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask)
218 {
219         struct device_node *np, *tmp_np;
220         struct device *tcpu_dev;
221         int cpu, ret = 0;
222
223         /* Get OPP descriptor node */
224         np = _of_get_opp_desc_node(cpu_dev);
225         if (!np) {
226                 dev_dbg(cpu_dev, "%s: Couldn't find cpu_dev node.\n", __func__);
227                 return -ENOENT;
228         }
229
230         /* OPPs are shared ? */
231         if (!of_property_read_bool(np, "opp-shared"))
232                 goto put_cpu_node;
233
234         for_each_possible_cpu(cpu) {
235                 if (cpu == cpu_dev->id)
236                         continue;
237
238                 tcpu_dev = get_cpu_device(cpu);
239                 if (!tcpu_dev) {
240                         dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
241                                 __func__, cpu);
242                         ret = -ENODEV;
243                         goto put_cpu_node;
244                 }
245
246                 /* Get OPP descriptor node */
247                 tmp_np = _of_get_opp_desc_node(tcpu_dev);
248                 if (!tmp_np) {
249                         dev_err(tcpu_dev, "%s: Couldn't find tcpu_dev node.\n",
250                                 __func__);
251                         ret = -ENOENT;
252                         goto put_cpu_node;
253                 }
254
255                 /* CPUs are sharing opp node */
256                 if (np == tmp_np)
257                         cpumask_set_cpu(cpu, cpumask);
258
259                 of_node_put(tmp_np);
260         }
261
262 put_cpu_node:
263         of_node_put(np);
264         return ret;
265 }
266 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
267 #endif