blob: 663aae1c9834d334799179cc1c5a97c66f9de5d7 [file] [log] [blame]
Nishanth Menone1f60b22010-10-13 00:13:10 +02001/*
2 * Generic OPP Interface
3 *
4 * Copyright (C) 2009-2010 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
Viresh Kumar8d4d4e92015-06-12 17:10:38 +053014#include <linux/cpu.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020015#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/err.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020018#include <linux/slab.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050019#include <linux/device.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020020#include <linux/list.h>
21#include <linux/rculist.h>
22#include <linux/rcupdate.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050023#include <linux/pm_opp.h>
Shawn Guob496dfb2012-09-05 01:09:12 +020024#include <linux/of.h>
Liam Girdwood80126ce2012-10-23 01:27:44 +020025#include <linux/export.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020026
27/*
28 * Internal data structure organization with the OPP layer library is as
29 * follows:
30 * dev_opp_list (root)
31 * |- device 1 (represents voltage domain 1)
32 * | |- opp 1 (availability, freq, voltage)
33 * | |- opp 2 ..
34 * ... ...
35 * | `- opp n ..
36 * |- device 2 (represents the next voltage domain)
37 * ...
38 * `- device m (represents mth voltage domain)
39 * device 1, 2.. are represented by dev_opp structure while each opp
40 * is represented by the opp structure.
41 */
42
43/**
Nishanth Menon47d43ba2013-09-19 16:03:51 -050044 * struct dev_pm_opp - Generic OPP description structure
Nishanth Menone1f60b22010-10-13 00:13:10 +020045 * @node: opp list node. The nodes are maintained throughout the lifetime
46 * of boot. It is expected only an optimal set of OPPs are
47 * added to the library by the SoC framework.
48 * RCU usage: opp list is traversed with RCU locks. node
49 * modification is possible realtime, hence the modifications
50 * are protected by the dev_opp_list_lock for integrity.
51 * IMPORTANT: the opp nodes should be maintained in increasing
52 * order.
Viresh Kumar383934092014-11-25 16:04:18 +053053 * @dynamic: not-created from static DT entries.
Nishanth Menone1f60b22010-10-13 00:13:10 +020054 * @available: true/false - marks if this OPP as available or not
Viresh Kumar27465902015-07-29 16:23:02 +053055 * @turbo: true if turbo (boost) OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +020056 * @rate: Frequency in hertz
Viresh Kumar27465902015-07-29 16:23:02 +053057 * @u_volt: Target voltage in microvolts corresponding to this OPP
58 * @u_volt_min: Minimum voltage in microvolts corresponding to this OPP
59 * @u_volt_max: Maximum voltage in microvolts corresponding to this OPP
60 * @u_amp: Maximum current drawn by the device in microamperes
Viresh Kumar3ca9bb32015-07-29 16:23:03 +053061 * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's
62 * frequency from any other OPP's frequency.
Nishanth Menone1f60b22010-10-13 00:13:10 +020063 * @dev_opp: points back to the device_opp struct this opp belongs to
Viresh Kumarcd1a0682014-11-25 16:04:16 +053064 * @rcu_head: RCU callback head used for deferred freeing
Viresh Kumar27465902015-07-29 16:23:02 +053065 * @np: OPP's device node.
Nishanth Menone1f60b22010-10-13 00:13:10 +020066 *
67 * This structure stores the OPP information for a given device.
68 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -050069struct dev_pm_opp {
Nishanth Menone1f60b22010-10-13 00:13:10 +020070 struct list_head node;
71
72 bool available;
Viresh Kumar383934092014-11-25 16:04:18 +053073 bool dynamic;
Viresh Kumar27465902015-07-29 16:23:02 +053074 bool turbo;
Nishanth Menone1f60b22010-10-13 00:13:10 +020075 unsigned long rate;
Viresh Kumar27465902015-07-29 16:23:02 +053076
Nishanth Menone1f60b22010-10-13 00:13:10 +020077 unsigned long u_volt;
Viresh Kumar27465902015-07-29 16:23:02 +053078 unsigned long u_volt_min;
79 unsigned long u_volt_max;
80 unsigned long u_amp;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +053081 unsigned long clock_latency_ns;
Nishanth Menone1f60b22010-10-13 00:13:10 +020082
83 struct device_opp *dev_opp;
Viresh Kumarcd1a0682014-11-25 16:04:16 +053084 struct rcu_head rcu_head;
Viresh Kumar27465902015-07-29 16:23:02 +053085
86 struct device_node *np;
Nishanth Menone1f60b22010-10-13 00:13:10 +020087};
88
89/**
Viresh Kumar06441652015-07-29 16:23:04 +053090 * struct device_list_opp - devices managed by 'struct device_opp'
91 * @node: list node
92 * @dev: device to which the struct object belongs
93 * @rcu_head: RCU callback head used for deferred freeing
94 *
95 * This is an internal data structure maintaining the list of devices that are
96 * managed by 'struct device_opp'.
97 */
98struct device_list_opp {
99 struct list_head node;
100 const struct device *dev;
101 struct rcu_head rcu_head;
102};
103
104/**
Nishanth Menone1f60b22010-10-13 00:13:10 +0200105 * struct device_opp - Device opp structure
106 * @node: list node - contains the devices with OPPs that
107 * have been registered. Nodes once added are not modified in this
108 * list.
109 * RCU usage: nodes are not modified in the list of device_opp,
110 * however addition is possible and is secured by dev_opp_list_lock
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530111 * @srcu_head: notifier head to notify the OPP availability changes.
Viresh Kumar129eec52014-11-27 08:54:06 +0530112 * @rcu_head: RCU callback head used for deferred freeing
Viresh Kumar06441652015-07-29 16:23:04 +0530113 * @dev_list: list of devices that share these OPPs
Nishanth Menone1f60b22010-10-13 00:13:10 +0200114 * @opp_list: list of opps
Viresh Kumar06441652015-07-29 16:23:04 +0530115 * @np: struct device_node pointer for opp's DT node.
116 * @shared_opp: OPP is shared between multiple devices.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200117 *
118 * This is an internal data structure maintaining the link to opps attached to
119 * a device. This structure is not meant to be shared to users as it is
Viresh Kumar1c6a6622014-12-10 09:45:31 +0530120 * meant for book keeping and private to OPP library.
121 *
122 * Because the opp structures can be used from both rcu and srcu readers, we
123 * need to wait for the grace period of both of them before freeing any
124 * resources. And so we have used kfree_rcu() from within call_srcu() handlers.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200125 */
126struct device_opp {
127 struct list_head node;
128
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530129 struct srcu_notifier_head srcu_head;
Viresh Kumar129eec52014-11-27 08:54:06 +0530130 struct rcu_head rcu_head;
Viresh Kumar06441652015-07-29 16:23:04 +0530131 struct list_head dev_list;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200132 struct list_head opp_list;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530133
Viresh Kumar06441652015-07-29 16:23:04 +0530134 struct device_node *np;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530135 unsigned long clock_latency_ns_max;
Viresh Kumar06441652015-07-29 16:23:04 +0530136 bool shared_opp;
Viresh Kumarad656a62015-06-13 15:10:21 +0530137 struct dev_pm_opp *suspend_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200138};
139
140/*
141 * The root of the list of all devices. All device_opp structures branch off
142 * from here, with each device_opp containing the list of opp it supports in
143 * various states of availability.
144 */
145static LIST_HEAD(dev_opp_list);
146/* Lock to allow exclusive modification to the device and opp lists */
147static DEFINE_MUTEX(dev_opp_list_lock);
148
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800149#define opp_rcu_lockdep_assert() \
150do { \
151 rcu_lockdep_assert(rcu_read_lock_held() || \
152 lockdep_is_held(&dev_opp_list_lock), \
153 "Missing rcu_read_lock() or " \
154 "dev_opp_list_lock protection"); \
155} while (0)
156
Viresh Kumar06441652015-07-29 16:23:04 +0530157static struct device_list_opp *_find_list_dev(const struct device *dev,
158 struct device_opp *dev_opp)
159{
160 struct device_list_opp *list_dev;
161
162 list_for_each_entry(list_dev, &dev_opp->dev_list, node)
163 if (list_dev->dev == dev)
164 return list_dev;
165
166 return NULL;
167}
168
169static struct device_opp *_managed_opp(const struct device_node *np)
170{
171 struct device_opp *dev_opp;
172
173 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) {
174 if (dev_opp->np == np) {
175 /*
176 * Multiple devices can point to the same OPP table and
177 * so will have same node-pointer, np.
178 *
179 * But the OPPs will be considered as shared only if the
180 * OPP table contains a "opp-shared" property.
181 */
182 return dev_opp->shared_opp ? dev_opp : NULL;
183 }
184 }
185
186 return NULL;
187}
188
Nishanth Menone1f60b22010-10-13 00:13:10 +0200189/**
Nishanth Menon327854c2014-12-24 11:22:56 -0600190 * _find_device_opp() - find device_opp struct using device pointer
Nishanth Menone1f60b22010-10-13 00:13:10 +0200191 * @dev: device pointer used to lookup device OPPs
192 *
193 * Search list of device OPPs for one containing matching device. Does a RCU
194 * reader operation to grab the pointer needed.
195 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600196 * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +0200197 * -EINVAL based on type of error.
198 *
199 * Locking: This function must be called under rcu_read_lock(). device_opp
200 * is a RCU protected pointer. This means that device_opp is valid as long
201 * as we are under RCU lock.
202 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600203static struct device_opp *_find_device_opp(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200204{
Viresh Kumar06441652015-07-29 16:23:04 +0530205 struct device_opp *dev_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200206
207 if (unlikely(IS_ERR_OR_NULL(dev))) {
208 pr_err("%s: Invalid parameters\n", __func__);
209 return ERR_PTR(-EINVAL);
210 }
211
Viresh Kumar06441652015-07-29 16:23:04 +0530212 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node)
213 if (_find_list_dev(dev, dev_opp))
214 return dev_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200215
Viresh Kumar06441652015-07-29 16:23:04 +0530216 return ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200217}
218
219/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500220 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200221 * @opp: opp for which voltage has to be returned for
222 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600223 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200224 * return 0
225 *
226 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
227 * protected pointer. This means that opp which could have been fetched by
228 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
229 * under RCU lock. The pointer returned by the opp_find_freq family must be
230 * used in the same section as the usage of this function with the pointer
231 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
232 * pointer.
233 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500234unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200235{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500236 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200237 unsigned long v = 0;
238
Krzysztof Kozlowski04bf1c72015-01-09 09:27:57 +0100239 opp_rcu_lockdep_assert();
240
Nishanth Menone1f60b22010-10-13 00:13:10 +0200241 tmp_opp = rcu_dereference(opp);
242 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
243 pr_err("%s: Invalid parameters\n", __func__);
244 else
245 v = tmp_opp->u_volt;
246
247 return v;
248}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500249EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200250
251/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500252 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200253 * @opp: opp for which frequency has to be returned for
254 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600255 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200256 * return 0
257 *
258 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
259 * protected pointer. This means that opp which could have been fetched by
260 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
261 * under RCU lock. The pointer returned by the opp_find_freq family must be
262 * used in the same section as the usage of this function with the pointer
263 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
264 * pointer.
265 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500266unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200267{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500268 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200269 unsigned long f = 0;
270
Krzysztof Kozlowski04bf1c72015-01-09 09:27:57 +0100271 opp_rcu_lockdep_assert();
272
Nishanth Menone1f60b22010-10-13 00:13:10 +0200273 tmp_opp = rcu_dereference(opp);
274 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
275 pr_err("%s: Invalid parameters\n", __func__);
276 else
277 f = tmp_opp->rate;
278
279 return f;
280}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500281EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200282
283/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530284 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
285 * @dev: device for which we do this operation
286 *
287 * Return: This function returns the max clock latency in nanoseconds.
288 *
289 * Locking: This function takes rcu_read_lock().
290 */
291unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
292{
293 struct device_opp *dev_opp;
294 unsigned long clock_latency_ns;
295
296 rcu_read_lock();
297
298 dev_opp = _find_device_opp(dev);
299 if (IS_ERR(dev_opp))
300 clock_latency_ns = 0;
301 else
302 clock_latency_ns = dev_opp->clock_latency_ns_max;
303
304 rcu_read_unlock();
305 return clock_latency_ns;
306}
307EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
308
309/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500310 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
Nishanth Menone1f60b22010-10-13 00:13:10 +0200311 * @dev: device for which we do this operation
312 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600313 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200314 * else returns 0 if none or the corresponding error value.
315 *
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800316 * Locking: This function takes rcu_read_lock().
Nishanth Menone1f60b22010-10-13 00:13:10 +0200317 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500318int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200319{
320 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500321 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200322 int count = 0;
323
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800324 rcu_read_lock();
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800325
Nishanth Menon327854c2014-12-24 11:22:56 -0600326 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200327 if (IS_ERR(dev_opp)) {
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800328 count = PTR_ERR(dev_opp);
329 dev_err(dev, "%s: device OPP not found (%d)\n",
330 __func__, count);
331 goto out_unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200332 }
333
334 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
335 if (temp_opp->available)
336 count++;
337 }
338
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800339out_unlock:
340 rcu_read_unlock();
Nishanth Menone1f60b22010-10-13 00:13:10 +0200341 return count;
342}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500343EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200344
345/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500346 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200347 * @dev: device for which we do this operation
348 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100349 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200350 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600351 * Return: Searches for exact match in the opp list and returns pointer to the
352 * matching opp if found, else returns ERR_PTR in case of error and should
353 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200354 * EINVAL: for bad pointer
355 * ERANGE: no match found for search
356 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200357 *
358 * Note: available is a modifier for the search. if available=true, then the
359 * match is for exact matching frequency and is available in the stored OPP
360 * table. if false, the match is for exact frequency which is not available.
361 *
362 * This provides a mechanism to enable an opp which is not available currently
363 * or the opposite as well.
364 *
365 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
366 * protected pointer. The reason for the same is that the opp pointer which is
367 * returned will remain valid for use with opp_get_{voltage, freq} only while
368 * under the locked area. The pointer returned must be used prior to unlocking
369 * with rcu_read_unlock() to maintain the integrity of the pointer.
370 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500371struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
372 unsigned long freq,
373 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200374{
375 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500376 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200377
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800378 opp_rcu_lockdep_assert();
379
Nishanth Menon327854c2014-12-24 11:22:56 -0600380 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200381 if (IS_ERR(dev_opp)) {
382 int r = PTR_ERR(dev_opp);
383 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
384 return ERR_PTR(r);
385 }
386
387 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
388 if (temp_opp->available == available &&
389 temp_opp->rate == freq) {
390 opp = temp_opp;
391 break;
392 }
393 }
394
395 return opp;
396}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500397EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200398
399/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500400 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200401 * @dev: device for which we do this operation
402 * @freq: Start frequency
403 *
404 * Search for the matching ceil *available* OPP from a starting freq
405 * for a device.
406 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600407 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200408 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
409 * values can be:
410 * EINVAL: for bad pointer
411 * ERANGE: no match found for search
412 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200413 *
414 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
415 * protected pointer. The reason for the same is that the opp pointer which is
416 * returned will remain valid for use with opp_get_{voltage, freq} only while
417 * under the locked area. The pointer returned must be used prior to unlocking
418 * with rcu_read_unlock() to maintain the integrity of the pointer.
419 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500420struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
421 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200422{
423 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500424 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200425
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800426 opp_rcu_lockdep_assert();
427
Nishanth Menone1f60b22010-10-13 00:13:10 +0200428 if (!dev || !freq) {
429 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
430 return ERR_PTR(-EINVAL);
431 }
432
Nishanth Menon327854c2014-12-24 11:22:56 -0600433 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200434 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200435 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200436
437 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
438 if (temp_opp->available && temp_opp->rate >= *freq) {
439 opp = temp_opp;
440 *freq = opp->rate;
441 break;
442 }
443 }
444
445 return opp;
446}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500447EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200448
449/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500450 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200451 * @dev: device for which we do this operation
452 * @freq: Start frequency
453 *
454 * Search for the matching floor *available* OPP from a starting freq
455 * for a device.
456 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600457 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200458 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
459 * values can be:
460 * EINVAL: for bad pointer
461 * ERANGE: no match found for search
462 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200463 *
464 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
465 * protected pointer. The reason for the same is that the opp pointer which is
466 * returned will remain valid for use with opp_get_{voltage, freq} only while
467 * under the locked area. The pointer returned must be used prior to unlocking
468 * with rcu_read_unlock() to maintain the integrity of the pointer.
469 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500470struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
471 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200472{
473 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500474 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200475
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800476 opp_rcu_lockdep_assert();
477
Nishanth Menone1f60b22010-10-13 00:13:10 +0200478 if (!dev || !freq) {
479 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
480 return ERR_PTR(-EINVAL);
481 }
482
Nishanth Menon327854c2014-12-24 11:22:56 -0600483 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200484 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200485 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200486
487 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
488 if (temp_opp->available) {
489 /* go to the next node, before choosing prev */
490 if (temp_opp->rate > *freq)
491 break;
492 else
493 opp = temp_opp;
494 }
495 }
496 if (!IS_ERR(opp))
497 *freq = opp->rate;
498
499 return opp;
500}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500501EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200502
Viresh Kumar06441652015-07-29 16:23:04 +0530503/* List-dev Helpers */
504static void _kfree_list_dev_rcu(struct rcu_head *head)
505{
506 struct device_list_opp *list_dev;
507
508 list_dev = container_of(head, struct device_list_opp, rcu_head);
509 kfree_rcu(list_dev, rcu_head);
510}
511
512static void _remove_list_dev(struct device_list_opp *list_dev,
513 struct device_opp *dev_opp)
514{
515 list_del(&list_dev->node);
516 call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head,
517 _kfree_list_dev_rcu);
518}
519
520static struct device_list_opp *_add_list_dev(const struct device *dev,
521 struct device_opp *dev_opp)
522{
523 struct device_list_opp *list_dev;
524
525 list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL);
526 if (!list_dev)
527 return NULL;
528
529 /* Initialize list-dev */
530 list_dev->dev = dev;
531 list_add_rcu(&list_dev->node, &dev_opp->dev_list);
532
533 return list_dev;
534}
535
Nishanth Menon984f16c2014-12-24 11:22:57 -0600536/**
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530537 * _add_device_opp() - Find device OPP table or allocate a new one
Nishanth Menon984f16c2014-12-24 11:22:57 -0600538 * @dev: device for which we do this operation
539 *
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530540 * It tries to find an existing table first, if it couldn't find one, it
541 * allocates a new OPP table and returns that.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600542 *
543 * Return: valid device_opp pointer if success, else NULL.
544 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600545static struct device_opp *_add_device_opp(struct device *dev)
Viresh Kumar07cce742014-12-10 09:45:34 +0530546{
547 struct device_opp *dev_opp;
Viresh Kumar06441652015-07-29 16:23:04 +0530548 struct device_list_opp *list_dev;
Viresh Kumar07cce742014-12-10 09:45:34 +0530549
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530550 /* Check for existing list for 'dev' first */
551 dev_opp = _find_device_opp(dev);
552 if (!IS_ERR(dev_opp))
553 return dev_opp;
554
Viresh Kumar07cce742014-12-10 09:45:34 +0530555 /*
556 * Allocate a new device OPP table. In the infrequent case where a new
557 * device is needed to be added, we pay this penalty.
558 */
559 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
560 if (!dev_opp)
561 return NULL;
562
Viresh Kumar06441652015-07-29 16:23:04 +0530563 INIT_LIST_HEAD(&dev_opp->dev_list);
564
565 list_dev = _add_list_dev(dev, dev_opp);
566 if (!list_dev) {
567 kfree(dev_opp);
568 return NULL;
569 }
570
Viresh Kumar07cce742014-12-10 09:45:34 +0530571 srcu_init_notifier_head(&dev_opp->srcu_head);
572 INIT_LIST_HEAD(&dev_opp->opp_list);
573
574 /* Secure the device list modification */
575 list_add_rcu(&dev_opp->node, &dev_opp_list);
576 return dev_opp;
577}
578
Nishanth Menon984f16c2014-12-24 11:22:57 -0600579/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530580 * _kfree_device_rcu() - Free device_opp RCU handler
581 * @head: RCU head
582 */
583static void _kfree_device_rcu(struct rcu_head *head)
584{
585 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
586
587 kfree_rcu(device_opp, rcu_head);
588}
589
590/**
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530591 * _remove_device_opp() - Removes a device OPP table
592 * @dev_opp: device OPP table to be removed.
593 *
594 * Removes/frees device OPP table it it doesn't contain any OPPs.
595 */
596static void _remove_device_opp(struct device_opp *dev_opp)
597{
Viresh Kumar06441652015-07-29 16:23:04 +0530598 struct device_list_opp *list_dev;
599
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530600 if (!list_empty(&dev_opp->opp_list))
601 return;
602
Viresh Kumar06441652015-07-29 16:23:04 +0530603 list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp,
604 node);
605
606 _remove_list_dev(list_dev, dev_opp);
607
608 /* dev_list must be empty now */
609 WARN_ON(!list_empty(&dev_opp->dev_list));
610
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530611 list_del_rcu(&dev_opp->node);
612 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
613 _kfree_device_rcu);
614}
615
616/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530617 * _kfree_opp_rcu() - Free OPP RCU handler
618 * @head: RCU head
619 */
620static void _kfree_opp_rcu(struct rcu_head *head)
621{
622 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
623
624 kfree_rcu(opp, rcu_head);
625}
626
627/**
628 * _opp_remove() - Remove an OPP from a table definition
629 * @dev_opp: points back to the device_opp struct this opp belongs to
630 * @opp: pointer to the OPP to remove
Viresh Kumar23dacf62015-07-29 16:23:01 +0530631 * @notify: OPP_EVENT_REMOVE notification should be sent or not
Viresh Kumar737002b2015-07-29 16:22:58 +0530632 *
633 * This function removes an opp definition from the opp list.
634 *
635 * Locking: The internal device_opp and opp structures are RCU protected.
636 * It is assumed that the caller holds required mutex for an RCU updater
637 * strategy.
638 */
639static void _opp_remove(struct device_opp *dev_opp,
Viresh Kumar23dacf62015-07-29 16:23:01 +0530640 struct dev_pm_opp *opp, bool notify)
Viresh Kumar737002b2015-07-29 16:22:58 +0530641{
642 /*
643 * Notify the changes in the availability of the operable
644 * frequency/voltage list.
645 */
Viresh Kumar23dacf62015-07-29 16:23:01 +0530646 if (notify)
647 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530648 list_del_rcu(&opp->node);
649 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
650
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530651 _remove_device_opp(dev_opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530652}
653
654/**
655 * dev_pm_opp_remove() - Remove an OPP from OPP list
656 * @dev: device for which we do this operation
657 * @freq: OPP to remove with matching 'freq'
658 *
659 * This function removes an opp from the opp list.
660 *
661 * Locking: The internal device_opp and opp structures are RCU protected.
662 * Hence this function internally uses RCU updater strategy with mutex locks
663 * to keep the integrity of the internal data structures. Callers should ensure
664 * that this function is *NOT* called under RCU protection or in contexts where
665 * mutex cannot be locked.
666 */
667void dev_pm_opp_remove(struct device *dev, unsigned long freq)
668{
669 struct dev_pm_opp *opp;
670 struct device_opp *dev_opp;
671 bool found = false;
672
673 /* Hold our list modification lock here */
674 mutex_lock(&dev_opp_list_lock);
675
676 dev_opp = _find_device_opp(dev);
677 if (IS_ERR(dev_opp))
678 goto unlock;
679
680 list_for_each_entry(opp, &dev_opp->opp_list, node) {
681 if (opp->rate == freq) {
682 found = true;
683 break;
684 }
685 }
686
687 if (!found) {
688 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
689 __func__, freq);
690 goto unlock;
691 }
692
Viresh Kumar23dacf62015-07-29 16:23:01 +0530693 _opp_remove(dev_opp, opp, true);
Viresh Kumar737002b2015-07-29 16:22:58 +0530694unlock:
695 mutex_unlock(&dev_opp_list_lock);
696}
697EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
698
Viresh Kumar23dacf62015-07-29 16:23:01 +0530699static struct dev_pm_opp *_allocate_opp(struct device *dev,
700 struct device_opp **dev_opp)
701{
702 struct dev_pm_opp *opp;
703
704 /* allocate new OPP node */
705 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
706 if (!opp)
707 return NULL;
708
709 INIT_LIST_HEAD(&opp->node);
710
711 *dev_opp = _add_device_opp(dev);
712 if (!*dev_opp) {
713 kfree(opp);
714 return NULL;
715 }
716
717 return opp;
718}
719
Viresh Kumar06441652015-07-29 16:23:04 +0530720static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
721 struct device_opp *dev_opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530722{
723 struct dev_pm_opp *opp;
724 struct list_head *head = &dev_opp->opp_list;
725
726 /*
727 * Insert new OPP in order of increasing frequency and discard if
728 * already present.
729 *
730 * Need to use &dev_opp->opp_list in the condition part of the 'for'
731 * loop, don't replace it with head otherwise it will become an infinite
732 * loop.
733 */
734 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
735 if (new_opp->rate > opp->rate) {
736 head = &opp->node;
737 continue;
738 }
739
740 if (new_opp->rate < opp->rate)
741 break;
742
743 /* Duplicate OPPs */
Viresh Kumar06441652015-07-29 16:23:04 +0530744 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
Viresh Kumar23dacf62015-07-29 16:23:01 +0530745 __func__, opp->rate, opp->u_volt, opp->available,
746 new_opp->rate, new_opp->u_volt, new_opp->available);
747
748 return opp->available && new_opp->u_volt == opp->u_volt ?
749 0 : -EEXIST;
750 }
751
752 new_opp->dev_opp = dev_opp;
753 list_add_rcu(&new_opp->node, head);
754
755 return 0;
756}
757
Viresh Kumar737002b2015-07-29 16:22:58 +0530758/**
Nishanth Menon984f16c2014-12-24 11:22:57 -0600759 * _opp_add_dynamic() - Allocate a dynamic OPP.
760 * @dev: device for which we do this operation
761 * @freq: Frequency in Hz for this OPP
762 * @u_volt: Voltage in uVolts for this OPP
763 * @dynamic: Dynamically added OPPs.
764 *
765 * This function adds an opp definition to the opp list and returns status.
766 * The opp is made available by default and it can be controlled using
767 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
768 *
769 * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and
770 * freed by of_free_opp_table.
771 *
772 * Locking: The internal device_opp and opp structures are RCU protected.
773 * Hence this function internally uses RCU updater strategy with mutex locks
774 * to keep the integrity of the internal data structures. Callers should ensure
775 * that this function is *NOT* called under RCU protection or in contexts where
776 * mutex cannot be locked.
777 *
778 * Return:
779 * 0 On success OR
780 * Duplicate OPPs (both freq and volt are same) and opp->available
781 * -EEXIST Freq are same and volt are different OR
782 * Duplicate OPPs (both freq and volt are same) and !opp->available
783 * -ENOMEM Memory allocation failure
784 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600785static int _opp_add_dynamic(struct device *dev, unsigned long freq,
786 long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200787{
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530788 struct device_opp *dev_opp;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530789 struct dev_pm_opp *new_opp;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530790 int ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200791
Nishanth Menone1f60b22010-10-13 00:13:10 +0200792 /* Hold our list modification lock here */
793 mutex_lock(&dev_opp_list_lock);
794
Viresh Kumar23dacf62015-07-29 16:23:01 +0530795 new_opp = _allocate_opp(dev, &dev_opp);
796 if (!new_opp) {
797 ret = -ENOMEM;
798 goto unlock;
799 }
800
Viresh Kumara7470db2014-11-25 16:04:17 +0530801 /* populate the opp table */
Viresh Kumara7470db2014-11-25 16:04:17 +0530802 new_opp->rate = freq;
803 new_opp->u_volt = u_volt;
804 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530805 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530806
Viresh Kumar06441652015-07-29 16:23:04 +0530807 ret = _opp_add(dev, new_opp, dev_opp);
Viresh Kumar23dacf62015-07-29 16:23:01 +0530808 if (ret)
809 goto free_opp;
810
Nishanth Menone1f60b22010-10-13 00:13:10 +0200811 mutex_unlock(&dev_opp_list_lock);
812
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200813 /*
814 * Notify the changes in the availability of the operable
815 * frequency/voltage list.
816 */
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530817 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200818 return 0;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530819
820free_opp:
Viresh Kumar23dacf62015-07-29 16:23:01 +0530821 _opp_remove(dev_opp, new_opp, false);
822unlock:
Viresh Kumar6ce41842014-12-10 09:45:35 +0530823 mutex_unlock(&dev_opp_list_lock);
Viresh Kumar6ce41842014-12-10 09:45:35 +0530824 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200825}
Viresh Kumar383934092014-11-25 16:04:18 +0530826
Viresh Kumar27465902015-07-29 16:23:02 +0530827/* TODO: Support multiple regulators */
828static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev)
829{
830 u32 microvolt[3] = {0};
831 int count, ret;
832
833 count = of_property_count_u32_elems(opp->np, "opp-microvolt");
834 if (!count)
835 return 0;
836
837 /* There can be one or three elements here */
838 if (count != 1 && count != 3) {
839 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
840 __func__, count);
841 return -EINVAL;
842 }
843
844 ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
845 count);
846 if (ret) {
847 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
848 ret);
849 return -EINVAL;
850 }
851
852 opp->u_volt = microvolt[0];
853 opp->u_volt_min = microvolt[1];
854 opp->u_volt_max = microvolt[2];
855
856 return 0;
857}
858
859/**
860 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
861 * @dev: device for which we do this operation
862 * @np: device node
863 *
864 * This function adds an opp definition to the opp list and returns status. The
865 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
866 * removed by dev_pm_opp_remove.
867 *
868 * Locking: The internal device_opp and opp structures are RCU protected.
869 * Hence this function internally uses RCU updater strategy with mutex locks
870 * to keep the integrity of the internal data structures. Callers should ensure
871 * that this function is *NOT* called under RCU protection or in contexts where
872 * mutex cannot be locked.
873 *
874 * Return:
875 * 0 On success OR
876 * Duplicate OPPs (both freq and volt are same) and opp->available
877 * -EEXIST Freq are same and volt are different OR
878 * Duplicate OPPs (both freq and volt are same) and !opp->available
879 * -ENOMEM Memory allocation failure
880 * -EINVAL Failed parsing the OPP node
881 */
882static int _opp_add_static_v2(struct device *dev, struct device_node *np)
883{
884 struct device_opp *dev_opp;
885 struct dev_pm_opp *new_opp;
886 u64 rate;
887 int ret;
888
889 /* Hold our list modification lock here */
890 mutex_lock(&dev_opp_list_lock);
891
892 new_opp = _allocate_opp(dev, &dev_opp);
893 if (!new_opp) {
894 ret = -ENOMEM;
895 goto unlock;
896 }
897
898 ret = of_property_read_u64(np, "opp-hz", &rate);
899 if (ret < 0) {
900 dev_err(dev, "%s: opp-hz not found\n", __func__);
901 goto free_opp;
902 }
903
904 /*
905 * Rate is defined as an unsigned long in clk API, and so casting
906 * explicitly to its type. Must be fixed once rate is 64 bit
907 * guaranteed in clk API.
908 */
909 new_opp->rate = (unsigned long)rate;
910 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
911
912 new_opp->np = np;
913 new_opp->dynamic = false;
914 new_opp->available = true;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530915 of_property_read_u32(np, "clock-latency-ns",
916 (u32 *)&new_opp->clock_latency_ns);
Viresh Kumar27465902015-07-29 16:23:02 +0530917
918 ret = opp_get_microvolt(new_opp, dev);
919 if (ret)
920 goto free_opp;
921
922 of_property_read_u32(np, "opp-microamp", (u32 *)&new_opp->u_amp);
923
Viresh Kumar06441652015-07-29 16:23:04 +0530924 ret = _opp_add(dev, new_opp, dev_opp);
Viresh Kumar27465902015-07-29 16:23:02 +0530925 if (ret)
926 goto free_opp;
927
Viresh Kumarad656a62015-06-13 15:10:21 +0530928 /* OPP to select on device suspend */
929 if (of_property_read_bool(np, "opp-suspend")) {
930 if (dev_opp->suspend_opp)
931 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
932 __func__, dev_opp->suspend_opp->rate,
933 new_opp->rate);
934 else
935 dev_opp->suspend_opp = new_opp;
936 }
937
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530938 if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max)
939 dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns;
940
Viresh Kumar27465902015-07-29 16:23:02 +0530941 mutex_unlock(&dev_opp_list_lock);
942
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530943 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
Viresh Kumar27465902015-07-29 16:23:02 +0530944 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530945 new_opp->u_volt_min, new_opp->u_volt_max,
946 new_opp->clock_latency_ns);
Viresh Kumar27465902015-07-29 16:23:02 +0530947
948 /*
949 * Notify the changes in the availability of the operable
950 * frequency/voltage list.
951 */
952 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
953 return 0;
954
955free_opp:
956 _opp_remove(dev_opp, new_opp, false);
957unlock:
958 mutex_unlock(&dev_opp_list_lock);
959 return ret;
960}
961
Viresh Kumar383934092014-11-25 16:04:18 +0530962/**
963 * dev_pm_opp_add() - Add an OPP table from a table definitions
964 * @dev: device for which we do this operation
965 * @freq: Frequency in Hz for this OPP
966 * @u_volt: Voltage in uVolts for this OPP
967 *
968 * This function adds an opp definition to the opp list and returns status.
969 * The opp is made available by default and it can be controlled using
970 * dev_pm_opp_enable/disable functions.
971 *
972 * Locking: The internal device_opp and opp structures are RCU protected.
973 * Hence this function internally uses RCU updater strategy with mutex locks
974 * to keep the integrity of the internal data structures. Callers should ensure
975 * that this function is *NOT* called under RCU protection or in contexts where
976 * mutex cannot be locked.
977 *
978 * Return:
Nishanth Menon984f16c2014-12-24 11:22:57 -0600979 * 0 On success OR
Viresh Kumar383934092014-11-25 16:04:18 +0530980 * Duplicate OPPs (both freq and volt are same) and opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -0600981 * -EEXIST Freq are same and volt are different OR
Viresh Kumar383934092014-11-25 16:04:18 +0530982 * Duplicate OPPs (both freq and volt are same) and !opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -0600983 * -ENOMEM Memory allocation failure
Viresh Kumar383934092014-11-25 16:04:18 +0530984 */
985int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
986{
Nishanth Menon327854c2014-12-24 11:22:56 -0600987 return _opp_add_dynamic(dev, freq, u_volt, true);
Viresh Kumar383934092014-11-25 16:04:18 +0530988}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500989EXPORT_SYMBOL_GPL(dev_pm_opp_add);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200990
Nishanth Menon984f16c2014-12-24 11:22:57 -0600991/**
Nishanth Menon327854c2014-12-24 11:22:56 -0600992 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200993 * @dev: device for which we do this operation
994 * @freq: OPP frequency to modify availability
995 * @availability_req: availability status requested for this opp
996 *
997 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
998 * share a common logic which is isolated here.
999 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001000 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Nishanth Menone1f60b22010-10-13 00:13:10 +02001001 * copy operation, returns 0 if no modifcation was done OR modification was
1002 * successful.
1003 *
1004 * Locking: The internal device_opp and opp structures are RCU protected.
1005 * Hence this function internally uses RCU updater strategy with mutex locks to
1006 * keep the integrity of the internal data structures. Callers should ensure
1007 * that this function is *NOT* called under RCU protection or in contexts where
1008 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1009 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001010static int _opp_set_availability(struct device *dev, unsigned long freq,
1011 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001012{
Viresh Kumar29df0ee2014-12-10 09:45:33 +05301013 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001014 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001015 int r = 0;
1016
1017 /* keep the node allocated */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001018 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
Quentin Lambert59d84ca2015-02-09 10:45:32 +01001019 if (!new_opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001020 return -ENOMEM;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001021
1022 mutex_lock(&dev_opp_list_lock);
1023
1024 /* Find the device_opp */
Nishanth Menon327854c2014-12-24 11:22:56 -06001025 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001026 if (IS_ERR(dev_opp)) {
1027 r = PTR_ERR(dev_opp);
1028 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1029 goto unlock;
1030 }
1031
1032 /* Do we have the frequency? */
1033 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
1034 if (tmp_opp->rate == freq) {
1035 opp = tmp_opp;
1036 break;
1037 }
1038 }
1039 if (IS_ERR(opp)) {
1040 r = PTR_ERR(opp);
1041 goto unlock;
1042 }
1043
1044 /* Is update really needed? */
1045 if (opp->available == availability_req)
1046 goto unlock;
1047 /* copy the old data over */
1048 *new_opp = *opp;
1049
1050 /* plug in new node */
1051 new_opp->available = availability_req;
1052
1053 list_replace_rcu(&opp->node, &new_opp->node);
1054 mutex_unlock(&dev_opp_list_lock);
Nishanth Menon327854c2014-12-24 11:22:56 -06001055 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001056
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001057 /* Notify the change of the OPP availability */
1058 if (availability_req)
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301059 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001060 new_opp);
1061 else
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301062 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001063 new_opp);
1064
Vincent Guittotdde84372012-10-23 01:21:49 +02001065 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001066
1067unlock:
1068 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001069 kfree(new_opp);
1070 return r;
1071}
1072
1073/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001074 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001075 * @dev: device for which we do this operation
1076 * @freq: OPP frequency to enable
1077 *
1078 * Enables a provided opp. If the operation is valid, this returns 0, else the
1079 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001080 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001081 *
1082 * Locking: The internal device_opp and opp structures are RCU protected.
1083 * Hence this function indirectly uses RCU and mutex locks to keep the
1084 * integrity of the internal data structures. Callers should ensure that
1085 * this function is *NOT* called under RCU protection or in contexts where
1086 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001087 *
1088 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1089 * copy operation, returns 0 if no modifcation was done OR modification was
1090 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001091 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001092int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001093{
Nishanth Menon327854c2014-12-24 11:22:56 -06001094 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001095}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001096EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001097
1098/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001099 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001100 * @dev: device for which we do this operation
1101 * @freq: OPP frequency to disable
1102 *
1103 * Disables a provided opp. If the operation is valid, this returns
1104 * 0, else the corresponding error value. It is meant to be a temporary
1105 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001106 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001107 *
1108 * Locking: The internal device_opp and opp structures are RCU protected.
1109 * Hence this function indirectly uses RCU and mutex locks to keep the
1110 * integrity of the internal data structures. Callers should ensure that
1111 * this function is *NOT* called under RCU protection or in contexts where
1112 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001113 *
1114 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1115 * copy operation, returns 0 if no modifcation was done OR modification was
1116 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001117 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001118int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001119{
Nishanth Menon327854c2014-12-24 11:22:56 -06001120 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001121}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001122EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001123
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001124/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001125 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001126 * @dev: device pointer used to lookup device OPPs.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001127 *
1128 * Return: pointer to notifier head if found, otherwise -ENODEV or
1129 * -EINVAL based on type of error casted as pointer. value must be checked
1130 * with IS_ERR to determine valid pointer or error result.
1131 *
1132 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
1133 * protected pointer. The reason for the same is that the opp pointer which is
1134 * returned will remain valid for use with opp_get_{voltage, freq} only while
1135 * under the locked area. The pointer returned must be used prior to unlocking
1136 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001137 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001138struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001139{
Nishanth Menon327854c2014-12-24 11:22:56 -06001140 struct device_opp *dev_opp = _find_device_opp(dev);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001141
1142 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +01001143 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001144
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301145 return &dev_opp->srcu_head;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001146}
Nishanth Menon4679ec32014-12-24 11:22:55 -06001147EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001148
1149#ifdef CONFIG_OF
1150/**
Viresh Kumar737002b2015-07-29 16:22:58 +05301151 * of_free_opp_table() - Free OPP table entries created from static DT entries
1152 * @dev: device pointer used to lookup device OPPs.
1153 *
1154 * Free OPPs created using static entries present in DT.
1155 *
1156 * Locking: The internal device_opp and opp structures are RCU protected.
1157 * Hence this function indirectly uses RCU updater strategy with mutex locks
1158 * to keep the integrity of the internal data structures. Callers should ensure
1159 * that this function is *NOT* called under RCU protection or in contexts where
1160 * mutex cannot be locked.
1161 */
1162void of_free_opp_table(struct device *dev)
1163{
1164 struct device_opp *dev_opp;
1165 struct dev_pm_opp *opp, *tmp;
1166
Viresh Kumar06441652015-07-29 16:23:04 +05301167 /* Hold our list modification lock here */
1168 mutex_lock(&dev_opp_list_lock);
1169
Viresh Kumar737002b2015-07-29 16:22:58 +05301170 /* Check for existing list for 'dev' */
1171 dev_opp = _find_device_opp(dev);
1172 if (IS_ERR(dev_opp)) {
1173 int error = PTR_ERR(dev_opp);
1174
1175 if (error != -ENODEV)
1176 WARN(1, "%s: dev_opp: %d\n",
1177 IS_ERR_OR_NULL(dev) ?
1178 "Invalid device" : dev_name(dev),
1179 error);
Viresh Kumar06441652015-07-29 16:23:04 +05301180 goto unlock;
Viresh Kumar737002b2015-07-29 16:22:58 +05301181 }
1182
Viresh Kumar06441652015-07-29 16:23:04 +05301183 /* Find if dev_opp manages a single device */
1184 if (list_is_singular(&dev_opp->dev_list)) {
1185 /* Free static OPPs */
1186 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1187 if (!opp->dynamic)
1188 _opp_remove(dev_opp, opp, true);
1189 }
1190 } else {
1191 _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp);
Viresh Kumar737002b2015-07-29 16:22:58 +05301192 }
1193
Viresh Kumar06441652015-07-29 16:23:04 +05301194unlock:
Viresh Kumar737002b2015-07-29 16:22:58 +05301195 mutex_unlock(&dev_opp_list_lock);
1196}
1197EXPORT_SYMBOL_GPL(of_free_opp_table);
1198
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301199void of_cpumask_free_opp_table(cpumask_var_t cpumask)
1200{
1201 struct device *cpu_dev;
1202 int cpu;
1203
1204 WARN_ON(cpumask_empty(cpumask));
1205
1206 for_each_cpu(cpu, cpumask) {
1207 cpu_dev = get_cpu_device(cpu);
1208 if (!cpu_dev) {
1209 pr_err("%s: failed to get cpu%d device\n", __func__,
1210 cpu);
1211 continue;
1212 }
1213
1214 of_free_opp_table(cpu_dev);
1215 }
1216}
1217EXPORT_SYMBOL_GPL(of_cpumask_free_opp_table);
1218
Viresh Kumar27465902015-07-29 16:23:02 +05301219/* Returns opp descriptor node from its phandle. Caller must do of_node_put() */
1220static struct device_node *
1221_of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop)
1222{
1223 struct device_node *opp_np;
1224
1225 opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value));
1226 if (!opp_np) {
1227 dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n",
1228 __func__, prop->name);
1229 return ERR_PTR(-EINVAL);
1230 }
1231
1232 return opp_np;
1233}
1234
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301235/* Returns opp descriptor node for a device. Caller must do of_node_put() */
1236static struct device_node *_of_get_opp_desc_node(struct device *dev)
1237{
1238 const struct property *prop;
1239
1240 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1241 if (!prop)
1242 return ERR_PTR(-ENODEV);
1243 if (!prop->value)
1244 return ERR_PTR(-ENODATA);
1245
1246 /*
1247 * TODO: Support for multiple OPP tables.
1248 *
1249 * There should be only ONE phandle present in "operating-points-v2"
1250 * property.
1251 */
1252 if (prop->length != sizeof(__be32)) {
1253 dev_err(dev, "%s: Invalid opp desc phandle\n", __func__);
1254 return ERR_PTR(-EINVAL);
1255 }
1256
1257 return _of_get_opp_desc_node_from_prop(dev, prop);
1258}
1259
Viresh Kumar27465902015-07-29 16:23:02 +05301260/* Initializes OPP tables based on new bindings */
1261static int _of_init_opp_table_v2(struct device *dev,
1262 const struct property *prop)
1263{
1264 struct device_node *opp_np, *np;
Viresh Kumar06441652015-07-29 16:23:04 +05301265 struct device_opp *dev_opp;
Viresh Kumar27465902015-07-29 16:23:02 +05301266 int ret = 0, count = 0;
1267
1268 if (!prop->value)
1269 return -ENODATA;
1270
1271 /* Get opp node */
1272 opp_np = _of_get_opp_desc_node_from_prop(dev, prop);
1273 if (IS_ERR(opp_np))
1274 return PTR_ERR(opp_np);
1275
Viresh Kumar06441652015-07-29 16:23:04 +05301276 dev_opp = _managed_opp(opp_np);
1277 if (dev_opp) {
1278 /* OPPs are already managed */
1279 if (!_add_list_dev(dev, dev_opp))
1280 ret = -ENOMEM;
1281 goto put_opp_np;
1282 }
1283
Viresh Kumar27465902015-07-29 16:23:02 +05301284 /* We have opp-list node now, iterate over it and add OPPs */
1285 for_each_available_child_of_node(opp_np, np) {
1286 count++;
1287
1288 ret = _opp_add_static_v2(dev, np);
1289 if (ret) {
1290 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1291 ret);
1292 break;
1293 }
1294 }
1295
1296 /* There should be one of more OPP defined */
1297 if (WARN_ON(!count))
1298 goto put_opp_np;
1299
Viresh Kumar06441652015-07-29 16:23:04 +05301300 if (!ret) {
1301 if (!dev_opp) {
1302 dev_opp = _find_device_opp(dev);
1303 if (WARN_ON(!dev_opp))
1304 goto put_opp_np;
1305 }
1306
1307 dev_opp->np = opp_np;
1308 dev_opp->shared_opp = of_property_read_bool(opp_np,
1309 "opp-shared");
1310 } else {
Viresh Kumar27465902015-07-29 16:23:02 +05301311 of_free_opp_table(dev);
Viresh Kumar06441652015-07-29 16:23:04 +05301312 }
Viresh Kumar27465902015-07-29 16:23:02 +05301313
1314put_opp_np:
1315 of_node_put(opp_np);
1316
1317 return ret;
1318}
1319
1320/* Initializes OPP tables based on old-deprecated bindings */
1321static int _of_init_opp_table_v1(struct device *dev)
Shawn Guob496dfb2012-09-05 01:09:12 +02001322{
1323 const struct property *prop;
1324 const __be32 *val;
1325 int nr;
1326
1327 prop = of_find_property(dev->of_node, "operating-points", NULL);
1328 if (!prop)
1329 return -ENODEV;
1330 if (!prop->value)
1331 return -ENODATA;
1332
1333 /*
1334 * Each OPP is a set of tuples consisting of frequency and
1335 * voltage like <freq-kHz vol-uV>.
1336 */
1337 nr = prop->length / sizeof(u32);
1338 if (nr % 2) {
1339 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1340 return -EINVAL;
1341 }
1342
1343 val = prop->value;
1344 while (nr) {
1345 unsigned long freq = be32_to_cpup(val++) * 1000;
1346 unsigned long volt = be32_to_cpup(val++);
1347
Nishanth Menon327854c2014-12-24 11:22:56 -06001348 if (_opp_add_dynamic(dev, freq, volt, false))
Shawn Guob496dfb2012-09-05 01:09:12 +02001349 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1350 __func__, freq);
Shawn Guob496dfb2012-09-05 01:09:12 +02001351 nr -= 2;
1352 }
1353
1354 return 0;
1355}
Viresh Kumar27465902015-07-29 16:23:02 +05301356
1357/**
1358 * of_init_opp_table() - Initialize opp table from device tree
1359 * @dev: device pointer used to lookup device OPPs.
1360 *
1361 * Register the initial OPP table with the OPP library for given device.
1362 *
1363 * Locking: The internal device_opp and opp structures are RCU protected.
1364 * Hence this function indirectly uses RCU updater strategy with mutex locks
1365 * to keep the integrity of the internal data structures. Callers should ensure
1366 * that this function is *NOT* called under RCU protection or in contexts where
1367 * mutex cannot be locked.
1368 *
1369 * Return:
1370 * 0 On success OR
1371 * Duplicate OPPs (both freq and volt are same) and opp->available
1372 * -EEXIST Freq are same and volt are different OR
1373 * Duplicate OPPs (both freq and volt are same) and !opp->available
1374 * -ENOMEM Memory allocation failure
1375 * -ENODEV when 'operating-points' property is not found or is invalid data
1376 * in device node.
1377 * -ENODATA when empty 'operating-points' property is found
1378 * -EINVAL when invalid entries are found in opp-v2 table
1379 */
1380int of_init_opp_table(struct device *dev)
1381{
1382 const struct property *prop;
1383
1384 /*
1385 * OPPs have two version of bindings now. The older one is deprecated,
1386 * try for the new binding first.
1387 */
1388 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1389 if (!prop) {
1390 /*
1391 * Try old-deprecated bindings for backward compatibility with
1392 * older dtbs.
1393 */
1394 return _of_init_opp_table_v1(dev);
1395 }
1396
1397 return _of_init_opp_table_v2(dev, prop);
1398}
Mark Langsdorf74c46c62013-01-28 18:26:16 +00001399EXPORT_SYMBOL_GPL(of_init_opp_table);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301400
1401int of_cpumask_init_opp_table(cpumask_var_t cpumask)
1402{
1403 struct device *cpu_dev;
1404 int cpu, ret = 0;
1405
1406 WARN_ON(cpumask_empty(cpumask));
1407
1408 for_each_cpu(cpu, cpumask) {
1409 cpu_dev = get_cpu_device(cpu);
1410 if (!cpu_dev) {
1411 pr_err("%s: failed to get cpu%d device\n", __func__,
1412 cpu);
1413 continue;
1414 }
1415
1416 ret = of_init_opp_table(cpu_dev);
1417 if (ret) {
1418 pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
1419 __func__, cpu, ret);
1420
1421 /* Free all other OPPs */
1422 of_cpumask_free_opp_table(cpumask);
1423 break;
1424 }
1425 }
1426
1427 return ret;
1428}
1429EXPORT_SYMBOL_GPL(of_cpumask_init_opp_table);
1430
1431/* Required only for V1 bindings, as v2 can manage it from DT itself */
1432int set_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1433{
1434 struct device_list_opp *list_dev;
1435 struct device_opp *dev_opp;
1436 struct device *dev;
1437 int cpu, ret = 0;
1438
1439 rcu_read_lock();
1440
1441 dev_opp = _find_device_opp(cpu_dev);
1442 if (IS_ERR(dev_opp)) {
1443 ret = -EINVAL;
1444 goto out_rcu_read_unlock;
1445 }
1446
1447 for_each_cpu(cpu, cpumask) {
1448 if (cpu == cpu_dev->id)
1449 continue;
1450
1451 dev = get_cpu_device(cpu);
1452 if (!dev) {
1453 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1454 __func__, cpu);
1455 continue;
1456 }
1457
1458 list_dev = _add_list_dev(dev, dev_opp);
1459 if (!list_dev) {
1460 dev_err(dev, "%s: failed to add list-dev for cpu%d device\n",
1461 __func__, cpu);
1462 continue;
1463 }
1464 }
1465out_rcu_read_unlock:
1466 rcu_read_unlock();
1467
1468 return 0;
1469}
1470EXPORT_SYMBOL_GPL(set_cpus_sharing_opps);
1471
1472/*
1473 * Works only for OPP v2 bindings.
1474 *
1475 * cpumask should be already set to mask of cpu_dev->id.
1476 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1477 */
1478int of_get_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1479{
1480 struct device_node *np, *tmp_np;
1481 struct device *tcpu_dev;
1482 int cpu, ret = 0;
1483
1484 /* Get OPP descriptor node */
1485 np = _of_get_opp_desc_node(cpu_dev);
1486 if (IS_ERR(np)) {
1487 dev_dbg(cpu_dev, "%s: Couldn't find opp node: %ld\n", __func__,
1488 PTR_ERR(np));
1489 return -ENOENT;
1490 }
1491
1492 /* OPPs are shared ? */
1493 if (!of_property_read_bool(np, "opp-shared"))
1494 goto put_cpu_node;
1495
1496 for_each_possible_cpu(cpu) {
1497 if (cpu == cpu_dev->id)
1498 continue;
1499
1500 tcpu_dev = get_cpu_device(cpu);
1501 if (!tcpu_dev) {
1502 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1503 __func__, cpu);
1504 ret = -ENODEV;
1505 goto put_cpu_node;
1506 }
1507
1508 /* Get OPP descriptor node */
1509 tmp_np = _of_get_opp_desc_node(tcpu_dev);
1510 if (IS_ERR(tmp_np)) {
1511 dev_err(tcpu_dev, "%s: Couldn't find opp node: %ld\n",
1512 __func__, PTR_ERR(tmp_np));
1513 ret = PTR_ERR(tmp_np);
1514 goto put_cpu_node;
1515 }
1516
1517 /* CPUs are sharing opp node */
1518 if (np == tmp_np)
1519 cpumask_set_cpu(cpu, cpumask);
1520
1521 of_node_put(tmp_np);
1522 }
1523
1524put_cpu_node:
1525 of_node_put(np);
1526 return ret;
1527}
1528EXPORT_SYMBOL_GPL(of_get_cpus_sharing_opps);
Shawn Guob496dfb2012-09-05 01:09:12 +02001529#endif