blob: 4d6c4576f7ae090c39bcefbf5975ad8e75453fb1 [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/**
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200284 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
285 * @opp: opp for which turbo mode is being verified
286 *
287 * Turbo OPPs are not for normal use, and can be enabled (under certain
288 * conditions) for short duration of times to finish high throughput work
289 * quickly. Running on them for longer times may overheat the chip.
290 *
291 * Return: true if opp is turbo opp, else false.
292 *
293 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
294 * protected pointer. This means that opp which could have been fetched by
295 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
296 * under RCU lock. The pointer returned by the opp_find_freq family must be
297 * used in the same section as the usage of this function with the pointer
298 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
299 * pointer.
300 */
301bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
302{
303 struct dev_pm_opp *tmp_opp;
304
305 opp_rcu_lockdep_assert();
306
307 tmp_opp = rcu_dereference(opp);
308 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
309 pr_err("%s: Invalid parameters\n", __func__);
310 return false;
311 }
312
313 return tmp_opp->turbo;
314}
315EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
316
317/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530318 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
319 * @dev: device for which we do this operation
320 *
321 * Return: This function returns the max clock latency in nanoseconds.
322 *
323 * Locking: This function takes rcu_read_lock().
324 */
325unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
326{
327 struct device_opp *dev_opp;
328 unsigned long clock_latency_ns;
329
330 rcu_read_lock();
331
332 dev_opp = _find_device_opp(dev);
333 if (IS_ERR(dev_opp))
334 clock_latency_ns = 0;
335 else
336 clock_latency_ns = dev_opp->clock_latency_ns_max;
337
338 rcu_read_unlock();
339 return clock_latency_ns;
340}
341EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
342
343/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500344 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
Nishanth Menone1f60b22010-10-13 00:13:10 +0200345 * @dev: device for which we do this operation
346 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600347 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200348 * else returns 0 if none or the corresponding error value.
349 *
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800350 * Locking: This function takes rcu_read_lock().
Nishanth Menone1f60b22010-10-13 00:13:10 +0200351 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500352int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200353{
354 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500355 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200356 int count = 0;
357
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800358 rcu_read_lock();
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800359
Nishanth Menon327854c2014-12-24 11:22:56 -0600360 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200361 if (IS_ERR(dev_opp)) {
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800362 count = PTR_ERR(dev_opp);
363 dev_err(dev, "%s: device OPP not found (%d)\n",
364 __func__, count);
365 goto out_unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200366 }
367
368 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
369 if (temp_opp->available)
370 count++;
371 }
372
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800373out_unlock:
374 rcu_read_unlock();
Nishanth Menone1f60b22010-10-13 00:13:10 +0200375 return count;
376}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500377EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200378
379/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500380 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200381 * @dev: device for which we do this operation
382 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100383 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200384 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600385 * Return: Searches for exact match in the opp list and returns pointer to the
386 * matching opp if found, else returns ERR_PTR in case of error and should
387 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200388 * EINVAL: for bad pointer
389 * ERANGE: no match found for search
390 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200391 *
392 * Note: available is a modifier for the search. if available=true, then the
393 * match is for exact matching frequency and is available in the stored OPP
394 * table. if false, the match is for exact frequency which is not available.
395 *
396 * This provides a mechanism to enable an opp which is not available currently
397 * or the opposite as well.
398 *
399 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
400 * protected pointer. The reason for the same is that the opp pointer which is
401 * returned will remain valid for use with opp_get_{voltage, freq} only while
402 * under the locked area. The pointer returned must be used prior to unlocking
403 * with rcu_read_unlock() to maintain the integrity of the pointer.
404 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500405struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
406 unsigned long freq,
407 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200408{
409 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500410 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200411
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800412 opp_rcu_lockdep_assert();
413
Nishanth Menon327854c2014-12-24 11:22:56 -0600414 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200415 if (IS_ERR(dev_opp)) {
416 int r = PTR_ERR(dev_opp);
417 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
418 return ERR_PTR(r);
419 }
420
421 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
422 if (temp_opp->available == available &&
423 temp_opp->rate == freq) {
424 opp = temp_opp;
425 break;
426 }
427 }
428
429 return opp;
430}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500431EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200432
433/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500434 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200435 * @dev: device for which we do this operation
436 * @freq: Start frequency
437 *
438 * Search for the matching ceil *available* OPP from a starting freq
439 * for a device.
440 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600441 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200442 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
443 * values can be:
444 * EINVAL: for bad pointer
445 * ERANGE: no match found for search
446 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200447 *
448 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
449 * protected pointer. The reason for the same is that the opp pointer which is
450 * returned will remain valid for use with opp_get_{voltage, freq} only while
451 * under the locked area. The pointer returned must be used prior to unlocking
452 * with rcu_read_unlock() to maintain the integrity of the pointer.
453 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500454struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
455 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200456{
457 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500458 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200459
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800460 opp_rcu_lockdep_assert();
461
Nishanth Menone1f60b22010-10-13 00:13:10 +0200462 if (!dev || !freq) {
463 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
464 return ERR_PTR(-EINVAL);
465 }
466
Nishanth Menon327854c2014-12-24 11:22:56 -0600467 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200468 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200469 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200470
471 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
472 if (temp_opp->available && temp_opp->rate >= *freq) {
473 opp = temp_opp;
474 *freq = opp->rate;
475 break;
476 }
477 }
478
479 return opp;
480}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500481EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200482
483/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500484 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200485 * @dev: device for which we do this operation
486 * @freq: Start frequency
487 *
488 * Search for the matching floor *available* OPP from a starting freq
489 * for a device.
490 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600491 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200492 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
493 * values can be:
494 * EINVAL: for bad pointer
495 * ERANGE: no match found for search
496 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200497 *
498 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
499 * protected pointer. The reason for the same is that the opp pointer which is
500 * returned will remain valid for use with opp_get_{voltage, freq} only while
501 * under the locked area. The pointer returned must be used prior to unlocking
502 * with rcu_read_unlock() to maintain the integrity of the pointer.
503 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500504struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
505 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200506{
507 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500508 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200509
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800510 opp_rcu_lockdep_assert();
511
Nishanth Menone1f60b22010-10-13 00:13:10 +0200512 if (!dev || !freq) {
513 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
514 return ERR_PTR(-EINVAL);
515 }
516
Nishanth Menon327854c2014-12-24 11:22:56 -0600517 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200518 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200519 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200520
521 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
522 if (temp_opp->available) {
523 /* go to the next node, before choosing prev */
524 if (temp_opp->rate > *freq)
525 break;
526 else
527 opp = temp_opp;
528 }
529 }
530 if (!IS_ERR(opp))
531 *freq = opp->rate;
532
533 return opp;
534}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500535EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200536
Viresh Kumar06441652015-07-29 16:23:04 +0530537/* List-dev Helpers */
538static void _kfree_list_dev_rcu(struct rcu_head *head)
539{
540 struct device_list_opp *list_dev;
541
542 list_dev = container_of(head, struct device_list_opp, rcu_head);
543 kfree_rcu(list_dev, rcu_head);
544}
545
546static void _remove_list_dev(struct device_list_opp *list_dev,
547 struct device_opp *dev_opp)
548{
549 list_del(&list_dev->node);
550 call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head,
551 _kfree_list_dev_rcu);
552}
553
554static struct device_list_opp *_add_list_dev(const struct device *dev,
555 struct device_opp *dev_opp)
556{
557 struct device_list_opp *list_dev;
558
559 list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL);
560 if (!list_dev)
561 return NULL;
562
563 /* Initialize list-dev */
564 list_dev->dev = dev;
565 list_add_rcu(&list_dev->node, &dev_opp->dev_list);
566
567 return list_dev;
568}
569
Nishanth Menon984f16c2014-12-24 11:22:57 -0600570/**
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530571 * _add_device_opp() - Find device OPP table or allocate a new one
Nishanth Menon984f16c2014-12-24 11:22:57 -0600572 * @dev: device for which we do this operation
573 *
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530574 * It tries to find an existing table first, if it couldn't find one, it
575 * allocates a new OPP table and returns that.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600576 *
577 * Return: valid device_opp pointer if success, else NULL.
578 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600579static struct device_opp *_add_device_opp(struct device *dev)
Viresh Kumar07cce742014-12-10 09:45:34 +0530580{
581 struct device_opp *dev_opp;
Viresh Kumar06441652015-07-29 16:23:04 +0530582 struct device_list_opp *list_dev;
Viresh Kumar07cce742014-12-10 09:45:34 +0530583
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530584 /* Check for existing list for 'dev' first */
585 dev_opp = _find_device_opp(dev);
586 if (!IS_ERR(dev_opp))
587 return dev_opp;
588
Viresh Kumar07cce742014-12-10 09:45:34 +0530589 /*
590 * Allocate a new device OPP table. In the infrequent case where a new
591 * device is needed to be added, we pay this penalty.
592 */
593 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
594 if (!dev_opp)
595 return NULL;
596
Viresh Kumar06441652015-07-29 16:23:04 +0530597 INIT_LIST_HEAD(&dev_opp->dev_list);
598
599 list_dev = _add_list_dev(dev, dev_opp);
600 if (!list_dev) {
601 kfree(dev_opp);
602 return NULL;
603 }
604
Viresh Kumar07cce742014-12-10 09:45:34 +0530605 srcu_init_notifier_head(&dev_opp->srcu_head);
606 INIT_LIST_HEAD(&dev_opp->opp_list);
607
608 /* Secure the device list modification */
609 list_add_rcu(&dev_opp->node, &dev_opp_list);
610 return dev_opp;
611}
612
Nishanth Menon984f16c2014-12-24 11:22:57 -0600613/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530614 * _kfree_device_rcu() - Free device_opp RCU handler
615 * @head: RCU head
616 */
617static void _kfree_device_rcu(struct rcu_head *head)
618{
619 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
620
621 kfree_rcu(device_opp, rcu_head);
622}
623
624/**
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530625 * _remove_device_opp() - Removes a device OPP table
626 * @dev_opp: device OPP table to be removed.
627 *
628 * Removes/frees device OPP table it it doesn't contain any OPPs.
629 */
630static void _remove_device_opp(struct device_opp *dev_opp)
631{
Viresh Kumar06441652015-07-29 16:23:04 +0530632 struct device_list_opp *list_dev;
633
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530634 if (!list_empty(&dev_opp->opp_list))
635 return;
636
Viresh Kumar06441652015-07-29 16:23:04 +0530637 list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp,
638 node);
639
640 _remove_list_dev(list_dev, dev_opp);
641
642 /* dev_list must be empty now */
643 WARN_ON(!list_empty(&dev_opp->dev_list));
644
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530645 list_del_rcu(&dev_opp->node);
646 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
647 _kfree_device_rcu);
648}
649
650/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530651 * _kfree_opp_rcu() - Free OPP RCU handler
652 * @head: RCU head
653 */
654static void _kfree_opp_rcu(struct rcu_head *head)
655{
656 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
657
658 kfree_rcu(opp, rcu_head);
659}
660
661/**
662 * _opp_remove() - Remove an OPP from a table definition
663 * @dev_opp: points back to the device_opp struct this opp belongs to
664 * @opp: pointer to the OPP to remove
Viresh Kumar23dacf62015-07-29 16:23:01 +0530665 * @notify: OPP_EVENT_REMOVE notification should be sent or not
Viresh Kumar737002b2015-07-29 16:22:58 +0530666 *
667 * This function removes an opp definition from the opp list.
668 *
669 * Locking: The internal device_opp and opp structures are RCU protected.
670 * It is assumed that the caller holds required mutex for an RCU updater
671 * strategy.
672 */
673static void _opp_remove(struct device_opp *dev_opp,
Viresh Kumar23dacf62015-07-29 16:23:01 +0530674 struct dev_pm_opp *opp, bool notify)
Viresh Kumar737002b2015-07-29 16:22:58 +0530675{
676 /*
677 * Notify the changes in the availability of the operable
678 * frequency/voltage list.
679 */
Viresh Kumar23dacf62015-07-29 16:23:01 +0530680 if (notify)
681 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530682 list_del_rcu(&opp->node);
683 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
684
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530685 _remove_device_opp(dev_opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530686}
687
688/**
689 * dev_pm_opp_remove() - Remove an OPP from OPP list
690 * @dev: device for which we do this operation
691 * @freq: OPP to remove with matching 'freq'
692 *
693 * This function removes an opp from the opp list.
694 *
695 * Locking: The internal device_opp and opp structures are RCU protected.
696 * Hence this function internally uses RCU updater strategy with mutex locks
697 * to keep the integrity of the internal data structures. Callers should ensure
698 * that this function is *NOT* called under RCU protection or in contexts where
699 * mutex cannot be locked.
700 */
701void dev_pm_opp_remove(struct device *dev, unsigned long freq)
702{
703 struct dev_pm_opp *opp;
704 struct device_opp *dev_opp;
705 bool found = false;
706
707 /* Hold our list modification lock here */
708 mutex_lock(&dev_opp_list_lock);
709
710 dev_opp = _find_device_opp(dev);
711 if (IS_ERR(dev_opp))
712 goto unlock;
713
714 list_for_each_entry(opp, &dev_opp->opp_list, node) {
715 if (opp->rate == freq) {
716 found = true;
717 break;
718 }
719 }
720
721 if (!found) {
722 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
723 __func__, freq);
724 goto unlock;
725 }
726
Viresh Kumar23dacf62015-07-29 16:23:01 +0530727 _opp_remove(dev_opp, opp, true);
Viresh Kumar737002b2015-07-29 16:22:58 +0530728unlock:
729 mutex_unlock(&dev_opp_list_lock);
730}
731EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
732
Viresh Kumar23dacf62015-07-29 16:23:01 +0530733static struct dev_pm_opp *_allocate_opp(struct device *dev,
734 struct device_opp **dev_opp)
735{
736 struct dev_pm_opp *opp;
737
738 /* allocate new OPP node */
739 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
740 if (!opp)
741 return NULL;
742
743 INIT_LIST_HEAD(&opp->node);
744
745 *dev_opp = _add_device_opp(dev);
746 if (!*dev_opp) {
747 kfree(opp);
748 return NULL;
749 }
750
751 return opp;
752}
753
Viresh Kumar06441652015-07-29 16:23:04 +0530754static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
755 struct device_opp *dev_opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530756{
757 struct dev_pm_opp *opp;
758 struct list_head *head = &dev_opp->opp_list;
759
760 /*
761 * Insert new OPP in order of increasing frequency and discard if
762 * already present.
763 *
764 * Need to use &dev_opp->opp_list in the condition part of the 'for'
765 * loop, don't replace it with head otherwise it will become an infinite
766 * loop.
767 */
768 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
769 if (new_opp->rate > opp->rate) {
770 head = &opp->node;
771 continue;
772 }
773
774 if (new_opp->rate < opp->rate)
775 break;
776
777 /* Duplicate OPPs */
Viresh Kumar06441652015-07-29 16:23:04 +0530778 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 +0530779 __func__, opp->rate, opp->u_volt, opp->available,
780 new_opp->rate, new_opp->u_volt, new_opp->available);
781
782 return opp->available && new_opp->u_volt == opp->u_volt ?
783 0 : -EEXIST;
784 }
785
786 new_opp->dev_opp = dev_opp;
787 list_add_rcu(&new_opp->node, head);
788
789 return 0;
790}
791
Viresh Kumar737002b2015-07-29 16:22:58 +0530792/**
Nishanth Menon984f16c2014-12-24 11:22:57 -0600793 * _opp_add_dynamic() - Allocate a dynamic OPP.
794 * @dev: device for which we do this operation
795 * @freq: Frequency in Hz for this OPP
796 * @u_volt: Voltage in uVolts for this OPP
797 * @dynamic: Dynamically added OPPs.
798 *
799 * This function adds an opp definition to the opp list and returns status.
800 * The opp is made available by default and it can be controlled using
801 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
802 *
803 * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and
804 * freed by of_free_opp_table.
805 *
806 * Locking: The internal device_opp and opp structures are RCU protected.
807 * Hence this function internally uses RCU updater strategy with mutex locks
808 * to keep the integrity of the internal data structures. Callers should ensure
809 * that this function is *NOT* called under RCU protection or in contexts where
810 * mutex cannot be locked.
811 *
812 * Return:
813 * 0 On success OR
814 * Duplicate OPPs (both freq and volt are same) and opp->available
815 * -EEXIST Freq are same and volt are different OR
816 * Duplicate OPPs (both freq and volt are same) and !opp->available
817 * -ENOMEM Memory allocation failure
818 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600819static int _opp_add_dynamic(struct device *dev, unsigned long freq,
820 long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200821{
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530822 struct device_opp *dev_opp;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530823 struct dev_pm_opp *new_opp;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530824 int ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200825
Nishanth Menone1f60b22010-10-13 00:13:10 +0200826 /* Hold our list modification lock here */
827 mutex_lock(&dev_opp_list_lock);
828
Viresh Kumar23dacf62015-07-29 16:23:01 +0530829 new_opp = _allocate_opp(dev, &dev_opp);
830 if (!new_opp) {
831 ret = -ENOMEM;
832 goto unlock;
833 }
834
Viresh Kumara7470db2014-11-25 16:04:17 +0530835 /* populate the opp table */
Viresh Kumara7470db2014-11-25 16:04:17 +0530836 new_opp->rate = freq;
837 new_opp->u_volt = u_volt;
838 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530839 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530840
Viresh Kumar06441652015-07-29 16:23:04 +0530841 ret = _opp_add(dev, new_opp, dev_opp);
Viresh Kumar23dacf62015-07-29 16:23:01 +0530842 if (ret)
843 goto free_opp;
844
Nishanth Menone1f60b22010-10-13 00:13:10 +0200845 mutex_unlock(&dev_opp_list_lock);
846
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200847 /*
848 * Notify the changes in the availability of the operable
849 * frequency/voltage list.
850 */
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530851 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200852 return 0;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530853
854free_opp:
Viresh Kumar23dacf62015-07-29 16:23:01 +0530855 _opp_remove(dev_opp, new_opp, false);
856unlock:
Viresh Kumar6ce41842014-12-10 09:45:35 +0530857 mutex_unlock(&dev_opp_list_lock);
Viresh Kumar6ce41842014-12-10 09:45:35 +0530858 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200859}
Viresh Kumar383934092014-11-25 16:04:18 +0530860
Viresh Kumar27465902015-07-29 16:23:02 +0530861/* TODO: Support multiple regulators */
862static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev)
863{
864 u32 microvolt[3] = {0};
865 int count, ret;
866
867 count = of_property_count_u32_elems(opp->np, "opp-microvolt");
868 if (!count)
869 return 0;
870
871 /* There can be one or three elements here */
872 if (count != 1 && count != 3) {
873 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
874 __func__, count);
875 return -EINVAL;
876 }
877
878 ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
879 count);
880 if (ret) {
881 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
882 ret);
883 return -EINVAL;
884 }
885
886 opp->u_volt = microvolt[0];
887 opp->u_volt_min = microvolt[1];
888 opp->u_volt_max = microvolt[2];
889
890 return 0;
891}
892
893/**
894 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
895 * @dev: device for which we do this operation
896 * @np: device node
897 *
898 * This function adds an opp definition to the opp list and returns status. The
899 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
900 * removed by dev_pm_opp_remove.
901 *
902 * Locking: The internal device_opp and opp structures are RCU protected.
903 * Hence this function internally uses RCU updater strategy with mutex locks
904 * to keep the integrity of the internal data structures. Callers should ensure
905 * that this function is *NOT* called under RCU protection or in contexts where
906 * mutex cannot be locked.
907 *
908 * Return:
909 * 0 On success OR
910 * Duplicate OPPs (both freq and volt are same) and opp->available
911 * -EEXIST Freq are same and volt are different OR
912 * Duplicate OPPs (both freq and volt are same) and !opp->available
913 * -ENOMEM Memory allocation failure
914 * -EINVAL Failed parsing the OPP node
915 */
916static int _opp_add_static_v2(struct device *dev, struct device_node *np)
917{
918 struct device_opp *dev_opp;
919 struct dev_pm_opp *new_opp;
920 u64 rate;
921 int ret;
922
923 /* Hold our list modification lock here */
924 mutex_lock(&dev_opp_list_lock);
925
926 new_opp = _allocate_opp(dev, &dev_opp);
927 if (!new_opp) {
928 ret = -ENOMEM;
929 goto unlock;
930 }
931
932 ret = of_property_read_u64(np, "opp-hz", &rate);
933 if (ret < 0) {
934 dev_err(dev, "%s: opp-hz not found\n", __func__);
935 goto free_opp;
936 }
937
938 /*
939 * Rate is defined as an unsigned long in clk API, and so casting
940 * explicitly to its type. Must be fixed once rate is 64 bit
941 * guaranteed in clk API.
942 */
943 new_opp->rate = (unsigned long)rate;
944 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
945
946 new_opp->np = np;
947 new_opp->dynamic = false;
948 new_opp->available = true;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530949 of_property_read_u32(np, "clock-latency-ns",
950 (u32 *)&new_opp->clock_latency_ns);
Viresh Kumar27465902015-07-29 16:23:02 +0530951
952 ret = opp_get_microvolt(new_opp, dev);
953 if (ret)
954 goto free_opp;
955
956 of_property_read_u32(np, "opp-microamp", (u32 *)&new_opp->u_amp);
957
Viresh Kumar06441652015-07-29 16:23:04 +0530958 ret = _opp_add(dev, new_opp, dev_opp);
Viresh Kumar27465902015-07-29 16:23:02 +0530959 if (ret)
960 goto free_opp;
961
Viresh Kumarad656a62015-06-13 15:10:21 +0530962 /* OPP to select on device suspend */
963 if (of_property_read_bool(np, "opp-suspend")) {
964 if (dev_opp->suspend_opp)
965 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
966 __func__, dev_opp->suspend_opp->rate,
967 new_opp->rate);
968 else
969 dev_opp->suspend_opp = new_opp;
970 }
971
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530972 if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max)
973 dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns;
974
Viresh Kumar27465902015-07-29 16:23:02 +0530975 mutex_unlock(&dev_opp_list_lock);
976
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530977 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
Viresh Kumar27465902015-07-29 16:23:02 +0530978 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530979 new_opp->u_volt_min, new_opp->u_volt_max,
980 new_opp->clock_latency_ns);
Viresh Kumar27465902015-07-29 16:23:02 +0530981
982 /*
983 * Notify the changes in the availability of the operable
984 * frequency/voltage list.
985 */
986 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
987 return 0;
988
989free_opp:
990 _opp_remove(dev_opp, new_opp, false);
991unlock:
992 mutex_unlock(&dev_opp_list_lock);
993 return ret;
994}
995
Viresh Kumar383934092014-11-25 16:04:18 +0530996/**
997 * dev_pm_opp_add() - Add an OPP table from a table definitions
998 * @dev: device for which we do this operation
999 * @freq: Frequency in Hz for this OPP
1000 * @u_volt: Voltage in uVolts for this OPP
1001 *
1002 * This function adds an opp definition to the opp list and returns status.
1003 * The opp is made available by default and it can be controlled using
1004 * dev_pm_opp_enable/disable functions.
1005 *
1006 * Locking: The internal device_opp and opp structures are RCU protected.
1007 * Hence this function internally uses RCU updater strategy with mutex locks
1008 * to keep the integrity of the internal data structures. Callers should ensure
1009 * that this function is *NOT* called under RCU protection or in contexts where
1010 * mutex cannot be locked.
1011 *
1012 * Return:
Nishanth Menon984f16c2014-12-24 11:22:57 -06001013 * 0 On success OR
Viresh Kumar383934092014-11-25 16:04:18 +05301014 * Duplicate OPPs (both freq and volt are same) and opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -06001015 * -EEXIST Freq are same and volt are different OR
Viresh Kumar383934092014-11-25 16:04:18 +05301016 * Duplicate OPPs (both freq and volt are same) and !opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -06001017 * -ENOMEM Memory allocation failure
Viresh Kumar383934092014-11-25 16:04:18 +05301018 */
1019int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1020{
Nishanth Menon327854c2014-12-24 11:22:56 -06001021 return _opp_add_dynamic(dev, freq, u_volt, true);
Viresh Kumar383934092014-11-25 16:04:18 +05301022}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001023EXPORT_SYMBOL_GPL(dev_pm_opp_add);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001024
Nishanth Menon984f16c2014-12-24 11:22:57 -06001025/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001026 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001027 * @dev: device for which we do this operation
1028 * @freq: OPP frequency to modify availability
1029 * @availability_req: availability status requested for this opp
1030 *
1031 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1032 * share a common logic which is isolated here.
1033 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001034 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Nishanth Menone1f60b22010-10-13 00:13:10 +02001035 * copy operation, returns 0 if no modifcation was done OR modification was
1036 * successful.
1037 *
1038 * Locking: The internal device_opp and opp structures are RCU protected.
1039 * Hence this function internally uses RCU updater strategy with mutex locks to
1040 * keep the integrity of the internal data structures. Callers should ensure
1041 * that this function is *NOT* called under RCU protection or in contexts where
1042 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1043 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001044static int _opp_set_availability(struct device *dev, unsigned long freq,
1045 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001046{
Viresh Kumar29df0ee2014-12-10 09:45:33 +05301047 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001048 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001049 int r = 0;
1050
1051 /* keep the node allocated */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001052 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
Quentin Lambert59d84ca2015-02-09 10:45:32 +01001053 if (!new_opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001054 return -ENOMEM;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001055
1056 mutex_lock(&dev_opp_list_lock);
1057
1058 /* Find the device_opp */
Nishanth Menon327854c2014-12-24 11:22:56 -06001059 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001060 if (IS_ERR(dev_opp)) {
1061 r = PTR_ERR(dev_opp);
1062 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1063 goto unlock;
1064 }
1065
1066 /* Do we have the frequency? */
1067 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
1068 if (tmp_opp->rate == freq) {
1069 opp = tmp_opp;
1070 break;
1071 }
1072 }
1073 if (IS_ERR(opp)) {
1074 r = PTR_ERR(opp);
1075 goto unlock;
1076 }
1077
1078 /* Is update really needed? */
1079 if (opp->available == availability_req)
1080 goto unlock;
1081 /* copy the old data over */
1082 *new_opp = *opp;
1083
1084 /* plug in new node */
1085 new_opp->available = availability_req;
1086
1087 list_replace_rcu(&opp->node, &new_opp->node);
1088 mutex_unlock(&dev_opp_list_lock);
Nishanth Menon327854c2014-12-24 11:22:56 -06001089 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001090
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001091 /* Notify the change of the OPP availability */
1092 if (availability_req)
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301093 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001094 new_opp);
1095 else
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301096 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001097 new_opp);
1098
Vincent Guittotdde84372012-10-23 01:21:49 +02001099 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001100
1101unlock:
1102 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001103 kfree(new_opp);
1104 return r;
1105}
1106
1107/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001108 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001109 * @dev: device for which we do this operation
1110 * @freq: OPP frequency to enable
1111 *
1112 * Enables a provided opp. If the operation is valid, this returns 0, else the
1113 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001114 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001115 *
1116 * Locking: The internal device_opp and opp structures are RCU protected.
1117 * Hence this function indirectly uses RCU and mutex locks to keep the
1118 * integrity of the internal data structures. Callers should ensure that
1119 * this function is *NOT* called under RCU protection or in contexts where
1120 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001121 *
1122 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1123 * copy operation, returns 0 if no modifcation was done OR modification was
1124 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001125 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001126int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001127{
Nishanth Menon327854c2014-12-24 11:22:56 -06001128 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001129}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001130EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001131
1132/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001133 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001134 * @dev: device for which we do this operation
1135 * @freq: OPP frequency to disable
1136 *
1137 * Disables a provided opp. If the operation is valid, this returns
1138 * 0, else the corresponding error value. It is meant to be a temporary
1139 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001140 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001141 *
1142 * Locking: The internal device_opp and opp structures are RCU protected.
1143 * Hence this function indirectly uses RCU and mutex locks to keep the
1144 * integrity of the internal data structures. Callers should ensure that
1145 * this function is *NOT* called under RCU protection or in contexts where
1146 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001147 *
1148 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1149 * copy operation, returns 0 if no modifcation was done OR modification was
1150 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001151 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001152int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001153{
Nishanth Menon327854c2014-12-24 11:22:56 -06001154 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001155}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001156EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001157
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001158/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001159 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001160 * @dev: device pointer used to lookup device OPPs.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001161 *
1162 * Return: pointer to notifier head if found, otherwise -ENODEV or
1163 * -EINVAL based on type of error casted as pointer. value must be checked
1164 * with IS_ERR to determine valid pointer or error result.
1165 *
1166 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
1167 * protected pointer. The reason for the same is that the opp pointer which is
1168 * returned will remain valid for use with opp_get_{voltage, freq} only while
1169 * under the locked area. The pointer returned must be used prior to unlocking
1170 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001171 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001172struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001173{
Nishanth Menon327854c2014-12-24 11:22:56 -06001174 struct device_opp *dev_opp = _find_device_opp(dev);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001175
1176 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +01001177 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001178
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301179 return &dev_opp->srcu_head;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001180}
Nishanth Menon4679ec32014-12-24 11:22:55 -06001181EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001182
1183#ifdef CONFIG_OF
1184/**
Viresh Kumar737002b2015-07-29 16:22:58 +05301185 * of_free_opp_table() - Free OPP table entries created from static DT entries
1186 * @dev: device pointer used to lookup device OPPs.
1187 *
1188 * Free OPPs created using static entries present in DT.
1189 *
1190 * Locking: The internal device_opp and opp structures are RCU protected.
1191 * Hence this function indirectly uses RCU updater strategy with mutex locks
1192 * to keep the integrity of the internal data structures. Callers should ensure
1193 * that this function is *NOT* called under RCU protection or in contexts where
1194 * mutex cannot be locked.
1195 */
1196void of_free_opp_table(struct device *dev)
1197{
1198 struct device_opp *dev_opp;
1199 struct dev_pm_opp *opp, *tmp;
1200
Viresh Kumar06441652015-07-29 16:23:04 +05301201 /* Hold our list modification lock here */
1202 mutex_lock(&dev_opp_list_lock);
1203
Viresh Kumar737002b2015-07-29 16:22:58 +05301204 /* Check for existing list for 'dev' */
1205 dev_opp = _find_device_opp(dev);
1206 if (IS_ERR(dev_opp)) {
1207 int error = PTR_ERR(dev_opp);
1208
1209 if (error != -ENODEV)
1210 WARN(1, "%s: dev_opp: %d\n",
1211 IS_ERR_OR_NULL(dev) ?
1212 "Invalid device" : dev_name(dev),
1213 error);
Viresh Kumar06441652015-07-29 16:23:04 +05301214 goto unlock;
Viresh Kumar737002b2015-07-29 16:22:58 +05301215 }
1216
Viresh Kumar06441652015-07-29 16:23:04 +05301217 /* Find if dev_opp manages a single device */
1218 if (list_is_singular(&dev_opp->dev_list)) {
1219 /* Free static OPPs */
1220 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1221 if (!opp->dynamic)
1222 _opp_remove(dev_opp, opp, true);
1223 }
1224 } else {
1225 _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp);
Viresh Kumar737002b2015-07-29 16:22:58 +05301226 }
1227
Viresh Kumar06441652015-07-29 16:23:04 +05301228unlock:
Viresh Kumar737002b2015-07-29 16:22:58 +05301229 mutex_unlock(&dev_opp_list_lock);
1230}
1231EXPORT_SYMBOL_GPL(of_free_opp_table);
1232
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301233void of_cpumask_free_opp_table(cpumask_var_t cpumask)
1234{
1235 struct device *cpu_dev;
1236 int cpu;
1237
1238 WARN_ON(cpumask_empty(cpumask));
1239
1240 for_each_cpu(cpu, cpumask) {
1241 cpu_dev = get_cpu_device(cpu);
1242 if (!cpu_dev) {
1243 pr_err("%s: failed to get cpu%d device\n", __func__,
1244 cpu);
1245 continue;
1246 }
1247
1248 of_free_opp_table(cpu_dev);
1249 }
1250}
1251EXPORT_SYMBOL_GPL(of_cpumask_free_opp_table);
1252
Viresh Kumar27465902015-07-29 16:23:02 +05301253/* Returns opp descriptor node from its phandle. Caller must do of_node_put() */
1254static struct device_node *
1255_of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop)
1256{
1257 struct device_node *opp_np;
1258
1259 opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value));
1260 if (!opp_np) {
1261 dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n",
1262 __func__, prop->name);
1263 return ERR_PTR(-EINVAL);
1264 }
1265
1266 return opp_np;
1267}
1268
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301269/* Returns opp descriptor node for a device. Caller must do of_node_put() */
1270static struct device_node *_of_get_opp_desc_node(struct device *dev)
1271{
1272 const struct property *prop;
1273
1274 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1275 if (!prop)
1276 return ERR_PTR(-ENODEV);
1277 if (!prop->value)
1278 return ERR_PTR(-ENODATA);
1279
1280 /*
1281 * TODO: Support for multiple OPP tables.
1282 *
1283 * There should be only ONE phandle present in "operating-points-v2"
1284 * property.
1285 */
1286 if (prop->length != sizeof(__be32)) {
1287 dev_err(dev, "%s: Invalid opp desc phandle\n", __func__);
1288 return ERR_PTR(-EINVAL);
1289 }
1290
1291 return _of_get_opp_desc_node_from_prop(dev, prop);
1292}
1293
Viresh Kumar27465902015-07-29 16:23:02 +05301294/* Initializes OPP tables based on new bindings */
1295static int _of_init_opp_table_v2(struct device *dev,
1296 const struct property *prop)
1297{
1298 struct device_node *opp_np, *np;
Viresh Kumar06441652015-07-29 16:23:04 +05301299 struct device_opp *dev_opp;
Viresh Kumar27465902015-07-29 16:23:02 +05301300 int ret = 0, count = 0;
1301
1302 if (!prop->value)
1303 return -ENODATA;
1304
1305 /* Get opp node */
1306 opp_np = _of_get_opp_desc_node_from_prop(dev, prop);
1307 if (IS_ERR(opp_np))
1308 return PTR_ERR(opp_np);
1309
Viresh Kumar06441652015-07-29 16:23:04 +05301310 dev_opp = _managed_opp(opp_np);
1311 if (dev_opp) {
1312 /* OPPs are already managed */
1313 if (!_add_list_dev(dev, dev_opp))
1314 ret = -ENOMEM;
1315 goto put_opp_np;
1316 }
1317
Viresh Kumar27465902015-07-29 16:23:02 +05301318 /* We have opp-list node now, iterate over it and add OPPs */
1319 for_each_available_child_of_node(opp_np, np) {
1320 count++;
1321
1322 ret = _opp_add_static_v2(dev, np);
1323 if (ret) {
1324 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1325 ret);
Viresh Kumar1f821ed2015-08-12 16:30:18 +05301326 goto free_table;
Viresh Kumar27465902015-07-29 16:23:02 +05301327 }
1328 }
1329
1330 /* There should be one of more OPP defined */
Viresh Kumar1f821ed2015-08-12 16:30:18 +05301331 if (WARN_ON(!count)) {
1332 ret = -ENOENT;
Viresh Kumar27465902015-07-29 16:23:02 +05301333 goto put_opp_np;
Viresh Kumar06441652015-07-29 16:23:04 +05301334 }
Viresh Kumar27465902015-07-29 16:23:02 +05301335
Viresh Kumar1f821ed2015-08-12 16:30:18 +05301336 dev_opp = _find_device_opp(dev);
1337 if (WARN_ON(IS_ERR(dev_opp))) {
1338 ret = PTR_ERR(dev_opp);
1339 goto free_table;
1340 }
1341
1342 dev_opp->np = opp_np;
1343 dev_opp->shared_opp = of_property_read_bool(opp_np, "opp-shared");
1344
1345 of_node_put(opp_np);
1346 return 0;
1347
1348free_table:
1349 of_free_opp_table(dev);
Viresh Kumar27465902015-07-29 16:23:02 +05301350put_opp_np:
1351 of_node_put(opp_np);
1352
1353 return ret;
1354}
1355
1356/* Initializes OPP tables based on old-deprecated bindings */
1357static int _of_init_opp_table_v1(struct device *dev)
Shawn Guob496dfb2012-09-05 01:09:12 +02001358{
1359 const struct property *prop;
1360 const __be32 *val;
1361 int nr;
1362
1363 prop = of_find_property(dev->of_node, "operating-points", NULL);
1364 if (!prop)
1365 return -ENODEV;
1366 if (!prop->value)
1367 return -ENODATA;
1368
1369 /*
1370 * Each OPP is a set of tuples consisting of frequency and
1371 * voltage like <freq-kHz vol-uV>.
1372 */
1373 nr = prop->length / sizeof(u32);
1374 if (nr % 2) {
1375 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1376 return -EINVAL;
1377 }
1378
1379 val = prop->value;
1380 while (nr) {
1381 unsigned long freq = be32_to_cpup(val++) * 1000;
1382 unsigned long volt = be32_to_cpup(val++);
1383
Nishanth Menon327854c2014-12-24 11:22:56 -06001384 if (_opp_add_dynamic(dev, freq, volt, false))
Shawn Guob496dfb2012-09-05 01:09:12 +02001385 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1386 __func__, freq);
Shawn Guob496dfb2012-09-05 01:09:12 +02001387 nr -= 2;
1388 }
1389
1390 return 0;
1391}
Viresh Kumar27465902015-07-29 16:23:02 +05301392
1393/**
1394 * of_init_opp_table() - Initialize opp table from device tree
1395 * @dev: device pointer used to lookup device OPPs.
1396 *
1397 * Register the initial OPP table with the OPP library for given device.
1398 *
1399 * Locking: The internal device_opp and opp structures are RCU protected.
1400 * Hence this function indirectly uses RCU updater strategy with mutex locks
1401 * to keep the integrity of the internal data structures. Callers should ensure
1402 * that this function is *NOT* called under RCU protection or in contexts where
1403 * mutex cannot be locked.
1404 *
1405 * Return:
1406 * 0 On success OR
1407 * Duplicate OPPs (both freq and volt are same) and opp->available
1408 * -EEXIST Freq are same and volt are different OR
1409 * Duplicate OPPs (both freq and volt are same) and !opp->available
1410 * -ENOMEM Memory allocation failure
1411 * -ENODEV when 'operating-points' property is not found or is invalid data
1412 * in device node.
1413 * -ENODATA when empty 'operating-points' property is found
1414 * -EINVAL when invalid entries are found in opp-v2 table
1415 */
1416int of_init_opp_table(struct device *dev)
1417{
1418 const struct property *prop;
1419
1420 /*
1421 * OPPs have two version of bindings now. The older one is deprecated,
1422 * try for the new binding first.
1423 */
1424 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1425 if (!prop) {
1426 /*
1427 * Try old-deprecated bindings for backward compatibility with
1428 * older dtbs.
1429 */
1430 return _of_init_opp_table_v1(dev);
1431 }
1432
1433 return _of_init_opp_table_v2(dev, prop);
1434}
Mark Langsdorf74c46c62013-01-28 18:26:16 +00001435EXPORT_SYMBOL_GPL(of_init_opp_table);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301436
1437int of_cpumask_init_opp_table(cpumask_var_t cpumask)
1438{
1439 struct device *cpu_dev;
1440 int cpu, ret = 0;
1441
1442 WARN_ON(cpumask_empty(cpumask));
1443
1444 for_each_cpu(cpu, cpumask) {
1445 cpu_dev = get_cpu_device(cpu);
1446 if (!cpu_dev) {
1447 pr_err("%s: failed to get cpu%d device\n", __func__,
1448 cpu);
1449 continue;
1450 }
1451
1452 ret = of_init_opp_table(cpu_dev);
1453 if (ret) {
1454 pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
1455 __func__, cpu, ret);
1456
1457 /* Free all other OPPs */
1458 of_cpumask_free_opp_table(cpumask);
1459 break;
1460 }
1461 }
1462
1463 return ret;
1464}
1465EXPORT_SYMBOL_GPL(of_cpumask_init_opp_table);
1466
1467/* Required only for V1 bindings, as v2 can manage it from DT itself */
1468int set_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1469{
1470 struct device_list_opp *list_dev;
1471 struct device_opp *dev_opp;
1472 struct device *dev;
1473 int cpu, ret = 0;
1474
1475 rcu_read_lock();
1476
1477 dev_opp = _find_device_opp(cpu_dev);
1478 if (IS_ERR(dev_opp)) {
1479 ret = -EINVAL;
1480 goto out_rcu_read_unlock;
1481 }
1482
1483 for_each_cpu(cpu, cpumask) {
1484 if (cpu == cpu_dev->id)
1485 continue;
1486
1487 dev = get_cpu_device(cpu);
1488 if (!dev) {
1489 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1490 __func__, cpu);
1491 continue;
1492 }
1493
1494 list_dev = _add_list_dev(dev, dev_opp);
1495 if (!list_dev) {
1496 dev_err(dev, "%s: failed to add list-dev for cpu%d device\n",
1497 __func__, cpu);
1498 continue;
1499 }
1500 }
1501out_rcu_read_unlock:
1502 rcu_read_unlock();
1503
1504 return 0;
1505}
1506EXPORT_SYMBOL_GPL(set_cpus_sharing_opps);
1507
1508/*
1509 * Works only for OPP v2 bindings.
1510 *
1511 * cpumask should be already set to mask of cpu_dev->id.
1512 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1513 */
1514int of_get_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1515{
1516 struct device_node *np, *tmp_np;
1517 struct device *tcpu_dev;
1518 int cpu, ret = 0;
1519
1520 /* Get OPP descriptor node */
1521 np = _of_get_opp_desc_node(cpu_dev);
1522 if (IS_ERR(np)) {
1523 dev_dbg(cpu_dev, "%s: Couldn't find opp node: %ld\n", __func__,
1524 PTR_ERR(np));
1525 return -ENOENT;
1526 }
1527
1528 /* OPPs are shared ? */
1529 if (!of_property_read_bool(np, "opp-shared"))
1530 goto put_cpu_node;
1531
1532 for_each_possible_cpu(cpu) {
1533 if (cpu == cpu_dev->id)
1534 continue;
1535
1536 tcpu_dev = get_cpu_device(cpu);
1537 if (!tcpu_dev) {
1538 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1539 __func__, cpu);
1540 ret = -ENODEV;
1541 goto put_cpu_node;
1542 }
1543
1544 /* Get OPP descriptor node */
1545 tmp_np = _of_get_opp_desc_node(tcpu_dev);
1546 if (IS_ERR(tmp_np)) {
1547 dev_err(tcpu_dev, "%s: Couldn't find opp node: %ld\n",
1548 __func__, PTR_ERR(tmp_np));
1549 ret = PTR_ERR(tmp_np);
1550 goto put_cpu_node;
1551 }
1552
1553 /* CPUs are sharing opp node */
1554 if (np == tmp_np)
1555 cpumask_set_cpu(cpu, cpumask);
1556
1557 of_node_put(tmp_np);
1558 }
1559
1560put_cpu_node:
1561 of_node_put(np);
1562 return ret;
1563}
1564EXPORT_SYMBOL_GPL(of_get_cpus_sharing_opps);
Shawn Guob496dfb2012-09-05 01:09:12 +02001565#endif