blob: 3df62dbcec3ad7679b7fa5ea51958202cdefd563 [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
Viresh Kumar50a3cb02015-08-12 15:59:39 +0530207 if (IS_ERR_OR_NULL(dev)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200208 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);
Viresh Kumar50a3cb02015-08-12 15:59:39 +0530242 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200243 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);
Viresh Kumar50a3cb02015-08-12 15:59:39 +0530274 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200275 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/**
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200344 * dev_pm_opp_get_suspend_opp() - Get suspend opp
345 * @dev: device for which we do this operation
346 *
347 * Return: This function returns pointer to the suspend opp if it is
348 * defined, otherwise it returns NULL.
349 *
350 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
351 * protected pointer. The reason for the same is that the opp pointer which is
352 * returned will remain valid for use with opp_get_{voltage, freq} only while
353 * under the locked area. The pointer returned must be used prior to unlocking
354 * with rcu_read_unlock() to maintain the integrity of the pointer.
355 */
356struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
357{
358 struct device_opp *dev_opp;
359 struct dev_pm_opp *opp;
360
361 opp_rcu_lockdep_assert();
362
363 dev_opp = _find_device_opp(dev);
364 if (IS_ERR(dev_opp))
365 opp = NULL;
366 else
367 opp = dev_opp->suspend_opp;
368
369 return opp;
370}
371EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
372
373/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500374 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
Nishanth Menone1f60b22010-10-13 00:13:10 +0200375 * @dev: device for which we do this operation
376 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600377 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200378 * else returns 0 if none or the corresponding error value.
379 *
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800380 * Locking: This function takes rcu_read_lock().
Nishanth Menone1f60b22010-10-13 00:13:10 +0200381 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500382int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200383{
384 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500385 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200386 int count = 0;
387
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800388 rcu_read_lock();
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800389
Nishanth Menon327854c2014-12-24 11:22:56 -0600390 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200391 if (IS_ERR(dev_opp)) {
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800392 count = PTR_ERR(dev_opp);
393 dev_err(dev, "%s: device OPP not found (%d)\n",
394 __func__, count);
395 goto out_unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200396 }
397
398 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
399 if (temp_opp->available)
400 count++;
401 }
402
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800403out_unlock:
404 rcu_read_unlock();
Nishanth Menone1f60b22010-10-13 00:13:10 +0200405 return count;
406}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500407EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200408
409/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500410 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200411 * @dev: device for which we do this operation
412 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100413 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200414 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600415 * Return: Searches for exact match in the opp list and returns pointer to the
416 * matching opp if found, else returns ERR_PTR in case of error and should
417 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200418 * EINVAL: for bad pointer
419 * ERANGE: no match found for search
420 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200421 *
422 * Note: available is a modifier for the search. if available=true, then the
423 * match is for exact matching frequency and is available in the stored OPP
424 * table. if false, the match is for exact frequency which is not available.
425 *
426 * This provides a mechanism to enable an opp which is not available currently
427 * or the opposite as well.
428 *
429 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
430 * protected pointer. The reason for the same is that the opp pointer which is
431 * returned will remain valid for use with opp_get_{voltage, freq} only while
432 * under the locked area. The pointer returned must be used prior to unlocking
433 * with rcu_read_unlock() to maintain the integrity of the pointer.
434 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500435struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
436 unsigned long freq,
437 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200438{
439 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500440 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200441
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800442 opp_rcu_lockdep_assert();
443
Nishanth Menon327854c2014-12-24 11:22:56 -0600444 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200445 if (IS_ERR(dev_opp)) {
446 int r = PTR_ERR(dev_opp);
447 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
448 return ERR_PTR(r);
449 }
450
451 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
452 if (temp_opp->available == available &&
453 temp_opp->rate == freq) {
454 opp = temp_opp;
455 break;
456 }
457 }
458
459 return opp;
460}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500461EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200462
463/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500464 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200465 * @dev: device for which we do this operation
466 * @freq: Start frequency
467 *
468 * Search for the matching ceil *available* OPP from a starting freq
469 * for a device.
470 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600471 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200472 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
473 * values can be:
474 * EINVAL: for bad pointer
475 * ERANGE: no match found for search
476 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200477 *
478 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
479 * protected pointer. The reason for the same is that the opp pointer which is
480 * returned will remain valid for use with opp_get_{voltage, freq} only while
481 * under the locked area. The pointer returned must be used prior to unlocking
482 * with rcu_read_unlock() to maintain the integrity of the pointer.
483 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500484struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
485 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200486{
487 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500488 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200489
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800490 opp_rcu_lockdep_assert();
491
Nishanth Menone1f60b22010-10-13 00:13:10 +0200492 if (!dev || !freq) {
493 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
494 return ERR_PTR(-EINVAL);
495 }
496
Nishanth Menon327854c2014-12-24 11:22:56 -0600497 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200498 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200499 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200500
501 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
502 if (temp_opp->available && temp_opp->rate >= *freq) {
503 opp = temp_opp;
504 *freq = opp->rate;
505 break;
506 }
507 }
508
509 return opp;
510}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500511EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200512
513/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500514 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200515 * @dev: device for which we do this operation
516 * @freq: Start frequency
517 *
518 * Search for the matching floor *available* OPP from a starting freq
519 * for a device.
520 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600521 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200522 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
523 * values can be:
524 * EINVAL: for bad pointer
525 * ERANGE: no match found for search
526 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200527 *
528 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
529 * protected pointer. The reason for the same is that the opp pointer which is
530 * returned will remain valid for use with opp_get_{voltage, freq} only while
531 * under the locked area. The pointer returned must be used prior to unlocking
532 * with rcu_read_unlock() to maintain the integrity of the pointer.
533 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500534struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
535 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200536{
537 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500538 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200539
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800540 opp_rcu_lockdep_assert();
541
Nishanth Menone1f60b22010-10-13 00:13:10 +0200542 if (!dev || !freq) {
543 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
544 return ERR_PTR(-EINVAL);
545 }
546
Nishanth Menon327854c2014-12-24 11:22:56 -0600547 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200548 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200549 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200550
551 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
552 if (temp_opp->available) {
553 /* go to the next node, before choosing prev */
554 if (temp_opp->rate > *freq)
555 break;
556 else
557 opp = temp_opp;
558 }
559 }
560 if (!IS_ERR(opp))
561 *freq = opp->rate;
562
563 return opp;
564}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500565EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200566
Viresh Kumar06441652015-07-29 16:23:04 +0530567/* List-dev Helpers */
568static void _kfree_list_dev_rcu(struct rcu_head *head)
569{
570 struct device_list_opp *list_dev;
571
572 list_dev = container_of(head, struct device_list_opp, rcu_head);
573 kfree_rcu(list_dev, rcu_head);
574}
575
576static void _remove_list_dev(struct device_list_opp *list_dev,
577 struct device_opp *dev_opp)
578{
579 list_del(&list_dev->node);
580 call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head,
581 _kfree_list_dev_rcu);
582}
583
584static struct device_list_opp *_add_list_dev(const struct device *dev,
585 struct device_opp *dev_opp)
586{
587 struct device_list_opp *list_dev;
588
589 list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL);
590 if (!list_dev)
591 return NULL;
592
593 /* Initialize list-dev */
594 list_dev->dev = dev;
595 list_add_rcu(&list_dev->node, &dev_opp->dev_list);
596
597 return list_dev;
598}
599
Nishanth Menon984f16c2014-12-24 11:22:57 -0600600/**
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530601 * _add_device_opp() - Find device OPP table or allocate a new one
Nishanth Menon984f16c2014-12-24 11:22:57 -0600602 * @dev: device for which we do this operation
603 *
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530604 * It tries to find an existing table first, if it couldn't find one, it
605 * allocates a new OPP table and returns that.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600606 *
607 * Return: valid device_opp pointer if success, else NULL.
608 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600609static struct device_opp *_add_device_opp(struct device *dev)
Viresh Kumar07cce742014-12-10 09:45:34 +0530610{
611 struct device_opp *dev_opp;
Viresh Kumar06441652015-07-29 16:23:04 +0530612 struct device_list_opp *list_dev;
Viresh Kumar07cce742014-12-10 09:45:34 +0530613
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530614 /* Check for existing list for 'dev' first */
615 dev_opp = _find_device_opp(dev);
616 if (!IS_ERR(dev_opp))
617 return dev_opp;
618
Viresh Kumar07cce742014-12-10 09:45:34 +0530619 /*
620 * Allocate a new device OPP table. In the infrequent case where a new
621 * device is needed to be added, we pay this penalty.
622 */
623 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
624 if (!dev_opp)
625 return NULL;
626
Viresh Kumar06441652015-07-29 16:23:04 +0530627 INIT_LIST_HEAD(&dev_opp->dev_list);
628
629 list_dev = _add_list_dev(dev, dev_opp);
630 if (!list_dev) {
631 kfree(dev_opp);
632 return NULL;
633 }
634
Viresh Kumar07cce742014-12-10 09:45:34 +0530635 srcu_init_notifier_head(&dev_opp->srcu_head);
636 INIT_LIST_HEAD(&dev_opp->opp_list);
637
638 /* Secure the device list modification */
639 list_add_rcu(&dev_opp->node, &dev_opp_list);
640 return dev_opp;
641}
642
Nishanth Menon984f16c2014-12-24 11:22:57 -0600643/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530644 * _kfree_device_rcu() - Free device_opp RCU handler
645 * @head: RCU head
646 */
647static void _kfree_device_rcu(struct rcu_head *head)
648{
649 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
650
651 kfree_rcu(device_opp, rcu_head);
652}
653
654/**
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530655 * _remove_device_opp() - Removes a device OPP table
656 * @dev_opp: device OPP table to be removed.
657 *
658 * Removes/frees device OPP table it it doesn't contain any OPPs.
659 */
660static void _remove_device_opp(struct device_opp *dev_opp)
661{
Viresh Kumar06441652015-07-29 16:23:04 +0530662 struct device_list_opp *list_dev;
663
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530664 if (!list_empty(&dev_opp->opp_list))
665 return;
666
Viresh Kumar06441652015-07-29 16:23:04 +0530667 list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp,
668 node);
669
670 _remove_list_dev(list_dev, dev_opp);
671
672 /* dev_list must be empty now */
673 WARN_ON(!list_empty(&dev_opp->dev_list));
674
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530675 list_del_rcu(&dev_opp->node);
676 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
677 _kfree_device_rcu);
678}
679
680/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530681 * _kfree_opp_rcu() - Free OPP RCU handler
682 * @head: RCU head
683 */
684static void _kfree_opp_rcu(struct rcu_head *head)
685{
686 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
687
688 kfree_rcu(opp, rcu_head);
689}
690
691/**
692 * _opp_remove() - Remove an OPP from a table definition
693 * @dev_opp: points back to the device_opp struct this opp belongs to
694 * @opp: pointer to the OPP to remove
Viresh Kumar23dacf62015-07-29 16:23:01 +0530695 * @notify: OPP_EVENT_REMOVE notification should be sent or not
Viresh Kumar737002b2015-07-29 16:22:58 +0530696 *
697 * This function removes an opp definition from the opp list.
698 *
699 * Locking: The internal device_opp and opp structures are RCU protected.
700 * It is assumed that the caller holds required mutex for an RCU updater
701 * strategy.
702 */
703static void _opp_remove(struct device_opp *dev_opp,
Viresh Kumar23dacf62015-07-29 16:23:01 +0530704 struct dev_pm_opp *opp, bool notify)
Viresh Kumar737002b2015-07-29 16:22:58 +0530705{
706 /*
707 * Notify the changes in the availability of the operable
708 * frequency/voltage list.
709 */
Viresh Kumar23dacf62015-07-29 16:23:01 +0530710 if (notify)
711 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530712 list_del_rcu(&opp->node);
713 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
714
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530715 _remove_device_opp(dev_opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530716}
717
718/**
719 * dev_pm_opp_remove() - Remove an OPP from OPP list
720 * @dev: device for which we do this operation
721 * @freq: OPP to remove with matching 'freq'
722 *
723 * This function removes an opp from the opp list.
724 *
725 * Locking: The internal device_opp and opp structures are RCU protected.
726 * Hence this function internally uses RCU updater strategy with mutex locks
727 * to keep the integrity of the internal data structures. Callers should ensure
728 * that this function is *NOT* called under RCU protection or in contexts where
729 * mutex cannot be locked.
730 */
731void dev_pm_opp_remove(struct device *dev, unsigned long freq)
732{
733 struct dev_pm_opp *opp;
734 struct device_opp *dev_opp;
735 bool found = false;
736
737 /* Hold our list modification lock here */
738 mutex_lock(&dev_opp_list_lock);
739
740 dev_opp = _find_device_opp(dev);
741 if (IS_ERR(dev_opp))
742 goto unlock;
743
744 list_for_each_entry(opp, &dev_opp->opp_list, node) {
745 if (opp->rate == freq) {
746 found = true;
747 break;
748 }
749 }
750
751 if (!found) {
752 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
753 __func__, freq);
754 goto unlock;
755 }
756
Viresh Kumar23dacf62015-07-29 16:23:01 +0530757 _opp_remove(dev_opp, opp, true);
Viresh Kumar737002b2015-07-29 16:22:58 +0530758unlock:
759 mutex_unlock(&dev_opp_list_lock);
760}
761EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
762
Viresh Kumar23dacf62015-07-29 16:23:01 +0530763static struct dev_pm_opp *_allocate_opp(struct device *dev,
764 struct device_opp **dev_opp)
765{
766 struct dev_pm_opp *opp;
767
768 /* allocate new OPP node */
769 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
770 if (!opp)
771 return NULL;
772
773 INIT_LIST_HEAD(&opp->node);
774
775 *dev_opp = _add_device_opp(dev);
776 if (!*dev_opp) {
777 kfree(opp);
778 return NULL;
779 }
780
781 return opp;
782}
783
Viresh Kumar06441652015-07-29 16:23:04 +0530784static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
785 struct device_opp *dev_opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530786{
787 struct dev_pm_opp *opp;
788 struct list_head *head = &dev_opp->opp_list;
789
790 /*
791 * Insert new OPP in order of increasing frequency and discard if
792 * already present.
793 *
794 * Need to use &dev_opp->opp_list in the condition part of the 'for'
795 * loop, don't replace it with head otherwise it will become an infinite
796 * loop.
797 */
798 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
799 if (new_opp->rate > opp->rate) {
800 head = &opp->node;
801 continue;
802 }
803
804 if (new_opp->rate < opp->rate)
805 break;
806
807 /* Duplicate OPPs */
Viresh Kumar06441652015-07-29 16:23:04 +0530808 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 +0530809 __func__, opp->rate, opp->u_volt, opp->available,
810 new_opp->rate, new_opp->u_volt, new_opp->available);
811
812 return opp->available && new_opp->u_volt == opp->u_volt ?
813 0 : -EEXIST;
814 }
815
816 new_opp->dev_opp = dev_opp;
817 list_add_rcu(&new_opp->node, head);
818
819 return 0;
820}
821
Viresh Kumar737002b2015-07-29 16:22:58 +0530822/**
Nishanth Menon984f16c2014-12-24 11:22:57 -0600823 * _opp_add_dynamic() - Allocate a dynamic OPP.
824 * @dev: device for which we do this operation
825 * @freq: Frequency in Hz for this OPP
826 * @u_volt: Voltage in uVolts for this OPP
827 * @dynamic: Dynamically added OPPs.
828 *
829 * This function adds an opp definition to the opp list and returns status.
830 * The opp is made available by default and it can be controlled using
831 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
832 *
833 * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and
834 * freed by of_free_opp_table.
835 *
836 * Locking: The internal device_opp and opp structures are RCU protected.
837 * Hence this function internally uses RCU updater strategy with mutex locks
838 * to keep the integrity of the internal data structures. Callers should ensure
839 * that this function is *NOT* called under RCU protection or in contexts where
840 * mutex cannot be locked.
841 *
842 * Return:
843 * 0 On success OR
844 * Duplicate OPPs (both freq and volt are same) and opp->available
845 * -EEXIST Freq are same and volt are different OR
846 * Duplicate OPPs (both freq and volt are same) and !opp->available
847 * -ENOMEM Memory allocation failure
848 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600849static int _opp_add_dynamic(struct device *dev, unsigned long freq,
850 long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200851{
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530852 struct device_opp *dev_opp;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530853 struct dev_pm_opp *new_opp;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530854 int ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200855
Nishanth Menone1f60b22010-10-13 00:13:10 +0200856 /* Hold our list modification lock here */
857 mutex_lock(&dev_opp_list_lock);
858
Viresh Kumar23dacf62015-07-29 16:23:01 +0530859 new_opp = _allocate_opp(dev, &dev_opp);
860 if (!new_opp) {
861 ret = -ENOMEM;
862 goto unlock;
863 }
864
Viresh Kumara7470db2014-11-25 16:04:17 +0530865 /* populate the opp table */
Viresh Kumara7470db2014-11-25 16:04:17 +0530866 new_opp->rate = freq;
867 new_opp->u_volt = u_volt;
868 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530869 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530870
Viresh Kumar06441652015-07-29 16:23:04 +0530871 ret = _opp_add(dev, new_opp, dev_opp);
Viresh Kumar23dacf62015-07-29 16:23:01 +0530872 if (ret)
873 goto free_opp;
874
Nishanth Menone1f60b22010-10-13 00:13:10 +0200875 mutex_unlock(&dev_opp_list_lock);
876
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200877 /*
878 * Notify the changes in the availability of the operable
879 * frequency/voltage list.
880 */
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530881 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200882 return 0;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530883
884free_opp:
Viresh Kumar23dacf62015-07-29 16:23:01 +0530885 _opp_remove(dev_opp, new_opp, false);
886unlock:
Viresh Kumar6ce41842014-12-10 09:45:35 +0530887 mutex_unlock(&dev_opp_list_lock);
Viresh Kumar6ce41842014-12-10 09:45:35 +0530888 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200889}
Viresh Kumar383934092014-11-25 16:04:18 +0530890
Viresh Kumar27465902015-07-29 16:23:02 +0530891/* TODO: Support multiple regulators */
892static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev)
893{
894 u32 microvolt[3] = {0};
895 int count, ret;
896
897 count = of_property_count_u32_elems(opp->np, "opp-microvolt");
898 if (!count)
899 return 0;
900
901 /* There can be one or three elements here */
902 if (count != 1 && count != 3) {
903 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
904 __func__, count);
905 return -EINVAL;
906 }
907
908 ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
909 count);
910 if (ret) {
911 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
912 ret);
913 return -EINVAL;
914 }
915
916 opp->u_volt = microvolt[0];
917 opp->u_volt_min = microvolt[1];
918 opp->u_volt_max = microvolt[2];
919
920 return 0;
921}
922
923/**
924 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
925 * @dev: device for which we do this operation
926 * @np: device node
927 *
928 * This function adds an opp definition to the opp list and returns status. The
929 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
930 * removed by dev_pm_opp_remove.
931 *
932 * Locking: The internal device_opp and opp structures are RCU protected.
933 * Hence this function internally uses RCU updater strategy with mutex locks
934 * to keep the integrity of the internal data structures. Callers should ensure
935 * that this function is *NOT* called under RCU protection or in contexts where
936 * mutex cannot be locked.
937 *
938 * Return:
939 * 0 On success OR
940 * Duplicate OPPs (both freq and volt are same) and opp->available
941 * -EEXIST Freq are same and volt are different OR
942 * Duplicate OPPs (both freq and volt are same) and !opp->available
943 * -ENOMEM Memory allocation failure
944 * -EINVAL Failed parsing the OPP node
945 */
946static int _opp_add_static_v2(struct device *dev, struct device_node *np)
947{
948 struct device_opp *dev_opp;
949 struct dev_pm_opp *new_opp;
950 u64 rate;
Viresh Kumar68fa9f02015-08-17 19:20:20 +0530951 u32 val;
Viresh Kumar27465902015-07-29 16:23:02 +0530952 int ret;
953
954 /* Hold our list modification lock here */
955 mutex_lock(&dev_opp_list_lock);
956
957 new_opp = _allocate_opp(dev, &dev_opp);
958 if (!new_opp) {
959 ret = -ENOMEM;
960 goto unlock;
961 }
962
963 ret = of_property_read_u64(np, "opp-hz", &rate);
964 if (ret < 0) {
965 dev_err(dev, "%s: opp-hz not found\n", __func__);
966 goto free_opp;
967 }
968
969 /*
970 * Rate is defined as an unsigned long in clk API, and so casting
971 * explicitly to its type. Must be fixed once rate is 64 bit
972 * guaranteed in clk API.
973 */
974 new_opp->rate = (unsigned long)rate;
975 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
976
977 new_opp->np = np;
978 new_opp->dynamic = false;
979 new_opp->available = true;
Viresh Kumar68fa9f02015-08-17 19:20:20 +0530980
981 if (!of_property_read_u32(np, "clock-latency-ns", &val))
982 new_opp->clock_latency_ns = val;
Viresh Kumar27465902015-07-29 16:23:02 +0530983
984 ret = opp_get_microvolt(new_opp, dev);
985 if (ret)
986 goto free_opp;
987
Viresh Kumar68fa9f02015-08-17 19:20:20 +0530988 if (!of_property_read_u32(new_opp->np, "opp-microamp", &val))
989 new_opp->u_amp = val;
Viresh Kumar27465902015-07-29 16:23:02 +0530990
Viresh Kumar06441652015-07-29 16:23:04 +0530991 ret = _opp_add(dev, new_opp, dev_opp);
Viresh Kumar27465902015-07-29 16:23:02 +0530992 if (ret)
993 goto free_opp;
994
Viresh Kumarad656a62015-06-13 15:10:21 +0530995 /* OPP to select on device suspend */
996 if (of_property_read_bool(np, "opp-suspend")) {
997 if (dev_opp->suspend_opp)
998 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
999 __func__, dev_opp->suspend_opp->rate,
1000 new_opp->rate);
1001 else
1002 dev_opp->suspend_opp = new_opp;
1003 }
1004
Viresh Kumar3ca9bb32015-07-29 16:23:03 +05301005 if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max)
1006 dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns;
1007
Viresh Kumar27465902015-07-29 16:23:02 +05301008 mutex_unlock(&dev_opp_list_lock);
1009
Viresh Kumar3ca9bb32015-07-29 16:23:03 +05301010 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
Viresh Kumar27465902015-07-29 16:23:02 +05301011 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
Viresh Kumar3ca9bb32015-07-29 16:23:03 +05301012 new_opp->u_volt_min, new_opp->u_volt_max,
1013 new_opp->clock_latency_ns);
Viresh Kumar27465902015-07-29 16:23:02 +05301014
1015 /*
1016 * Notify the changes in the availability of the operable
1017 * frequency/voltage list.
1018 */
1019 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
1020 return 0;
1021
1022free_opp:
1023 _opp_remove(dev_opp, new_opp, false);
1024unlock:
1025 mutex_unlock(&dev_opp_list_lock);
1026 return ret;
1027}
1028
Viresh Kumar383934092014-11-25 16:04:18 +05301029/**
1030 * dev_pm_opp_add() - Add an OPP table from a table definitions
1031 * @dev: device for which we do this operation
1032 * @freq: Frequency in Hz for this OPP
1033 * @u_volt: Voltage in uVolts for this OPP
1034 *
1035 * This function adds an opp definition to the opp list and returns status.
1036 * The opp is made available by default and it can be controlled using
1037 * dev_pm_opp_enable/disable functions.
1038 *
1039 * Locking: The internal device_opp and opp structures are RCU protected.
1040 * Hence this function internally uses RCU updater strategy with mutex locks
1041 * to keep the integrity of the internal data structures. Callers should ensure
1042 * that this function is *NOT* called under RCU protection or in contexts where
1043 * mutex cannot be locked.
1044 *
1045 * Return:
Nishanth Menon984f16c2014-12-24 11:22:57 -06001046 * 0 On success OR
Viresh Kumar383934092014-11-25 16:04:18 +05301047 * Duplicate OPPs (both freq and volt are same) and opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -06001048 * -EEXIST Freq are same and volt are different OR
Viresh Kumar383934092014-11-25 16:04:18 +05301049 * Duplicate OPPs (both freq and volt are same) and !opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -06001050 * -ENOMEM Memory allocation failure
Viresh Kumar383934092014-11-25 16:04:18 +05301051 */
1052int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1053{
Nishanth Menon327854c2014-12-24 11:22:56 -06001054 return _opp_add_dynamic(dev, freq, u_volt, true);
Viresh Kumar383934092014-11-25 16:04:18 +05301055}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001056EXPORT_SYMBOL_GPL(dev_pm_opp_add);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001057
Nishanth Menon984f16c2014-12-24 11:22:57 -06001058/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001059 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001060 * @dev: device for which we do this operation
1061 * @freq: OPP frequency to modify availability
1062 * @availability_req: availability status requested for this opp
1063 *
1064 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1065 * share a common logic which is isolated here.
1066 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001067 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Nishanth Menone1f60b22010-10-13 00:13:10 +02001068 * copy operation, returns 0 if no modifcation was done OR modification was
1069 * successful.
1070 *
1071 * Locking: The internal device_opp and opp structures are RCU protected.
1072 * Hence this function internally uses RCU updater strategy with mutex locks to
1073 * keep the integrity of the internal data structures. Callers should ensure
1074 * that this function is *NOT* called under RCU protection or in contexts where
1075 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1076 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001077static int _opp_set_availability(struct device *dev, unsigned long freq,
1078 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001079{
Viresh Kumar29df0ee2014-12-10 09:45:33 +05301080 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001081 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001082 int r = 0;
1083
1084 /* keep the node allocated */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001085 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
Quentin Lambert59d84ca2015-02-09 10:45:32 +01001086 if (!new_opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001087 return -ENOMEM;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001088
1089 mutex_lock(&dev_opp_list_lock);
1090
1091 /* Find the device_opp */
Nishanth Menon327854c2014-12-24 11:22:56 -06001092 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001093 if (IS_ERR(dev_opp)) {
1094 r = PTR_ERR(dev_opp);
1095 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1096 goto unlock;
1097 }
1098
1099 /* Do we have the frequency? */
1100 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
1101 if (tmp_opp->rate == freq) {
1102 opp = tmp_opp;
1103 break;
1104 }
1105 }
1106 if (IS_ERR(opp)) {
1107 r = PTR_ERR(opp);
1108 goto unlock;
1109 }
1110
1111 /* Is update really needed? */
1112 if (opp->available == availability_req)
1113 goto unlock;
1114 /* copy the old data over */
1115 *new_opp = *opp;
1116
1117 /* plug in new node */
1118 new_opp->available = availability_req;
1119
1120 list_replace_rcu(&opp->node, &new_opp->node);
1121 mutex_unlock(&dev_opp_list_lock);
Nishanth Menon327854c2014-12-24 11:22:56 -06001122 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001123
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001124 /* Notify the change of the OPP availability */
1125 if (availability_req)
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301126 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001127 new_opp);
1128 else
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301129 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001130 new_opp);
1131
Vincent Guittotdde84372012-10-23 01:21:49 +02001132 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001133
1134unlock:
1135 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001136 kfree(new_opp);
1137 return r;
1138}
1139
1140/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001141 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001142 * @dev: device for which we do this operation
1143 * @freq: OPP frequency to enable
1144 *
1145 * Enables a provided opp. If the operation is valid, this returns 0, else the
1146 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001147 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001148 *
1149 * Locking: The internal device_opp and opp structures are RCU protected.
1150 * Hence this function indirectly uses RCU and mutex locks to keep the
1151 * integrity of the internal data structures. Callers should ensure that
1152 * this function is *NOT* called under RCU protection or in contexts where
1153 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001154 *
1155 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1156 * copy operation, returns 0 if no modifcation was done OR modification was
1157 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001158 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001159int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001160{
Nishanth Menon327854c2014-12-24 11:22:56 -06001161 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001162}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001163EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001164
1165/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001166 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001167 * @dev: device for which we do this operation
1168 * @freq: OPP frequency to disable
1169 *
1170 * Disables a provided opp. If the operation is valid, this returns
1171 * 0, else the corresponding error value. It is meant to be a temporary
1172 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001173 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001174 *
1175 * Locking: The internal device_opp and opp structures are RCU protected.
1176 * Hence this function indirectly uses RCU and mutex locks to keep the
1177 * integrity of the internal data structures. Callers should ensure that
1178 * this function is *NOT* called under RCU protection or in contexts where
1179 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001180 *
1181 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1182 * copy operation, returns 0 if no modifcation was done OR modification was
1183 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001184 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001185int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001186{
Nishanth Menon327854c2014-12-24 11:22:56 -06001187 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001188}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001189EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001190
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001191/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001192 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001193 * @dev: device pointer used to lookup device OPPs.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001194 *
1195 * Return: pointer to notifier head if found, otherwise -ENODEV or
1196 * -EINVAL based on type of error casted as pointer. value must be checked
1197 * with IS_ERR to determine valid pointer or error result.
1198 *
1199 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
1200 * protected pointer. The reason for the same is that the opp pointer which is
1201 * returned will remain valid for use with opp_get_{voltage, freq} only while
1202 * under the locked area. The pointer returned must be used prior to unlocking
1203 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001204 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001205struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001206{
Nishanth Menon327854c2014-12-24 11:22:56 -06001207 struct device_opp *dev_opp = _find_device_opp(dev);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001208
1209 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +01001210 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001211
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301212 return &dev_opp->srcu_head;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001213}
Nishanth Menon4679ec32014-12-24 11:22:55 -06001214EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001215
1216#ifdef CONFIG_OF
1217/**
Viresh Kumar737002b2015-07-29 16:22:58 +05301218 * of_free_opp_table() - Free OPP table entries created from static DT entries
1219 * @dev: device pointer used to lookup device OPPs.
1220 *
1221 * Free OPPs created using static entries present in DT.
1222 *
1223 * Locking: The internal device_opp and opp structures are RCU protected.
1224 * Hence this function indirectly uses RCU updater strategy with mutex locks
1225 * to keep the integrity of the internal data structures. Callers should ensure
1226 * that this function is *NOT* called under RCU protection or in contexts where
1227 * mutex cannot be locked.
1228 */
1229void of_free_opp_table(struct device *dev)
1230{
1231 struct device_opp *dev_opp;
1232 struct dev_pm_opp *opp, *tmp;
1233
Viresh Kumar06441652015-07-29 16:23:04 +05301234 /* Hold our list modification lock here */
1235 mutex_lock(&dev_opp_list_lock);
1236
Viresh Kumar737002b2015-07-29 16:22:58 +05301237 /* Check for existing list for 'dev' */
1238 dev_opp = _find_device_opp(dev);
1239 if (IS_ERR(dev_opp)) {
1240 int error = PTR_ERR(dev_opp);
1241
1242 if (error != -ENODEV)
1243 WARN(1, "%s: dev_opp: %d\n",
1244 IS_ERR_OR_NULL(dev) ?
1245 "Invalid device" : dev_name(dev),
1246 error);
Viresh Kumar06441652015-07-29 16:23:04 +05301247 goto unlock;
Viresh Kumar737002b2015-07-29 16:22:58 +05301248 }
1249
Viresh Kumar06441652015-07-29 16:23:04 +05301250 /* Find if dev_opp manages a single device */
1251 if (list_is_singular(&dev_opp->dev_list)) {
1252 /* Free static OPPs */
1253 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1254 if (!opp->dynamic)
1255 _opp_remove(dev_opp, opp, true);
1256 }
1257 } else {
1258 _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp);
Viresh Kumar737002b2015-07-29 16:22:58 +05301259 }
1260
Viresh Kumar06441652015-07-29 16:23:04 +05301261unlock:
Viresh Kumar737002b2015-07-29 16:22:58 +05301262 mutex_unlock(&dev_opp_list_lock);
1263}
1264EXPORT_SYMBOL_GPL(of_free_opp_table);
1265
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301266void of_cpumask_free_opp_table(cpumask_var_t cpumask)
1267{
1268 struct device *cpu_dev;
1269 int cpu;
1270
1271 WARN_ON(cpumask_empty(cpumask));
1272
1273 for_each_cpu(cpu, cpumask) {
1274 cpu_dev = get_cpu_device(cpu);
1275 if (!cpu_dev) {
1276 pr_err("%s: failed to get cpu%d device\n", __func__,
1277 cpu);
1278 continue;
1279 }
1280
1281 of_free_opp_table(cpu_dev);
1282 }
1283}
1284EXPORT_SYMBOL_GPL(of_cpumask_free_opp_table);
1285
Viresh Kumar27465902015-07-29 16:23:02 +05301286/* Returns opp descriptor node from its phandle. Caller must do of_node_put() */
1287static struct device_node *
1288_of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop)
1289{
1290 struct device_node *opp_np;
1291
1292 opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value));
1293 if (!opp_np) {
1294 dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n",
1295 __func__, prop->name);
1296 return ERR_PTR(-EINVAL);
1297 }
1298
1299 return opp_np;
1300}
1301
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301302/* Returns opp descriptor node for a device. Caller must do of_node_put() */
1303static struct device_node *_of_get_opp_desc_node(struct device *dev)
1304{
1305 const struct property *prop;
1306
1307 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1308 if (!prop)
1309 return ERR_PTR(-ENODEV);
1310 if (!prop->value)
1311 return ERR_PTR(-ENODATA);
1312
1313 /*
1314 * TODO: Support for multiple OPP tables.
1315 *
1316 * There should be only ONE phandle present in "operating-points-v2"
1317 * property.
1318 */
1319 if (prop->length != sizeof(__be32)) {
1320 dev_err(dev, "%s: Invalid opp desc phandle\n", __func__);
1321 return ERR_PTR(-EINVAL);
1322 }
1323
1324 return _of_get_opp_desc_node_from_prop(dev, prop);
1325}
1326
Viresh Kumar27465902015-07-29 16:23:02 +05301327/* Initializes OPP tables based on new bindings */
1328static int _of_init_opp_table_v2(struct device *dev,
1329 const struct property *prop)
1330{
1331 struct device_node *opp_np, *np;
Viresh Kumar06441652015-07-29 16:23:04 +05301332 struct device_opp *dev_opp;
Viresh Kumar27465902015-07-29 16:23:02 +05301333 int ret = 0, count = 0;
1334
1335 if (!prop->value)
1336 return -ENODATA;
1337
1338 /* Get opp node */
1339 opp_np = _of_get_opp_desc_node_from_prop(dev, prop);
1340 if (IS_ERR(opp_np))
1341 return PTR_ERR(opp_np);
1342
Viresh Kumar06441652015-07-29 16:23:04 +05301343 dev_opp = _managed_opp(opp_np);
1344 if (dev_opp) {
1345 /* OPPs are already managed */
1346 if (!_add_list_dev(dev, dev_opp))
1347 ret = -ENOMEM;
1348 goto put_opp_np;
1349 }
1350
Viresh Kumar27465902015-07-29 16:23:02 +05301351 /* We have opp-list node now, iterate over it and add OPPs */
1352 for_each_available_child_of_node(opp_np, np) {
1353 count++;
1354
1355 ret = _opp_add_static_v2(dev, np);
1356 if (ret) {
1357 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1358 ret);
Viresh Kumar1f821ed2015-08-12 16:30:18 +05301359 goto free_table;
Viresh Kumar27465902015-07-29 16:23:02 +05301360 }
1361 }
1362
1363 /* There should be one of more OPP defined */
Viresh Kumar1f821ed2015-08-12 16:30:18 +05301364 if (WARN_ON(!count)) {
1365 ret = -ENOENT;
Viresh Kumar27465902015-07-29 16:23:02 +05301366 goto put_opp_np;
Viresh Kumar06441652015-07-29 16:23:04 +05301367 }
Viresh Kumar27465902015-07-29 16:23:02 +05301368
Viresh Kumar1f821ed2015-08-12 16:30:18 +05301369 dev_opp = _find_device_opp(dev);
1370 if (WARN_ON(IS_ERR(dev_opp))) {
1371 ret = PTR_ERR(dev_opp);
1372 goto free_table;
1373 }
1374
1375 dev_opp->np = opp_np;
1376 dev_opp->shared_opp = of_property_read_bool(opp_np, "opp-shared");
1377
1378 of_node_put(opp_np);
1379 return 0;
1380
1381free_table:
1382 of_free_opp_table(dev);
Viresh Kumar27465902015-07-29 16:23:02 +05301383put_opp_np:
1384 of_node_put(opp_np);
1385
1386 return ret;
1387}
1388
1389/* Initializes OPP tables based on old-deprecated bindings */
1390static int _of_init_opp_table_v1(struct device *dev)
Shawn Guob496dfb2012-09-05 01:09:12 +02001391{
1392 const struct property *prop;
1393 const __be32 *val;
1394 int nr;
1395
1396 prop = of_find_property(dev->of_node, "operating-points", NULL);
1397 if (!prop)
1398 return -ENODEV;
1399 if (!prop->value)
1400 return -ENODATA;
1401
1402 /*
1403 * Each OPP is a set of tuples consisting of frequency and
1404 * voltage like <freq-kHz vol-uV>.
1405 */
1406 nr = prop->length / sizeof(u32);
1407 if (nr % 2) {
1408 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1409 return -EINVAL;
1410 }
1411
1412 val = prop->value;
1413 while (nr) {
1414 unsigned long freq = be32_to_cpup(val++) * 1000;
1415 unsigned long volt = be32_to_cpup(val++);
1416
Nishanth Menon327854c2014-12-24 11:22:56 -06001417 if (_opp_add_dynamic(dev, freq, volt, false))
Shawn Guob496dfb2012-09-05 01:09:12 +02001418 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1419 __func__, freq);
Shawn Guob496dfb2012-09-05 01:09:12 +02001420 nr -= 2;
1421 }
1422
1423 return 0;
1424}
Viresh Kumar27465902015-07-29 16:23:02 +05301425
1426/**
1427 * of_init_opp_table() - Initialize opp table from device tree
1428 * @dev: device pointer used to lookup device OPPs.
1429 *
1430 * Register the initial OPP table with the OPP library for given device.
1431 *
1432 * Locking: The internal device_opp and opp structures are RCU protected.
1433 * Hence this function indirectly uses RCU updater strategy with mutex locks
1434 * to keep the integrity of the internal data structures. Callers should ensure
1435 * that this function is *NOT* called under RCU protection or in contexts where
1436 * mutex cannot be locked.
1437 *
1438 * Return:
1439 * 0 On success OR
1440 * Duplicate OPPs (both freq and volt are same) and opp->available
1441 * -EEXIST Freq are same and volt are different OR
1442 * Duplicate OPPs (both freq and volt are same) and !opp->available
1443 * -ENOMEM Memory allocation failure
1444 * -ENODEV when 'operating-points' property is not found or is invalid data
1445 * in device node.
1446 * -ENODATA when empty 'operating-points' property is found
1447 * -EINVAL when invalid entries are found in opp-v2 table
1448 */
1449int of_init_opp_table(struct device *dev)
1450{
1451 const struct property *prop;
1452
1453 /*
1454 * OPPs have two version of bindings now. The older one is deprecated,
1455 * try for the new binding first.
1456 */
1457 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1458 if (!prop) {
1459 /*
1460 * Try old-deprecated bindings for backward compatibility with
1461 * older dtbs.
1462 */
1463 return _of_init_opp_table_v1(dev);
1464 }
1465
1466 return _of_init_opp_table_v2(dev, prop);
1467}
Mark Langsdorf74c46c62013-01-28 18:26:16 +00001468EXPORT_SYMBOL_GPL(of_init_opp_table);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301469
1470int of_cpumask_init_opp_table(cpumask_var_t cpumask)
1471{
1472 struct device *cpu_dev;
1473 int cpu, ret = 0;
1474
1475 WARN_ON(cpumask_empty(cpumask));
1476
1477 for_each_cpu(cpu, cpumask) {
1478 cpu_dev = get_cpu_device(cpu);
1479 if (!cpu_dev) {
1480 pr_err("%s: failed to get cpu%d device\n", __func__,
1481 cpu);
1482 continue;
1483 }
1484
1485 ret = of_init_opp_table(cpu_dev);
1486 if (ret) {
1487 pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
1488 __func__, cpu, ret);
1489
1490 /* Free all other OPPs */
1491 of_cpumask_free_opp_table(cpumask);
1492 break;
1493 }
1494 }
1495
1496 return ret;
1497}
1498EXPORT_SYMBOL_GPL(of_cpumask_init_opp_table);
1499
1500/* Required only for V1 bindings, as v2 can manage it from DT itself */
1501int set_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1502{
1503 struct device_list_opp *list_dev;
1504 struct device_opp *dev_opp;
1505 struct device *dev;
1506 int cpu, ret = 0;
1507
1508 rcu_read_lock();
1509
1510 dev_opp = _find_device_opp(cpu_dev);
1511 if (IS_ERR(dev_opp)) {
1512 ret = -EINVAL;
1513 goto out_rcu_read_unlock;
1514 }
1515
1516 for_each_cpu(cpu, cpumask) {
1517 if (cpu == cpu_dev->id)
1518 continue;
1519
1520 dev = get_cpu_device(cpu);
1521 if (!dev) {
1522 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1523 __func__, cpu);
1524 continue;
1525 }
1526
1527 list_dev = _add_list_dev(dev, dev_opp);
1528 if (!list_dev) {
1529 dev_err(dev, "%s: failed to add list-dev for cpu%d device\n",
1530 __func__, cpu);
1531 continue;
1532 }
1533 }
1534out_rcu_read_unlock:
1535 rcu_read_unlock();
1536
1537 return 0;
1538}
1539EXPORT_SYMBOL_GPL(set_cpus_sharing_opps);
1540
1541/*
1542 * Works only for OPP v2 bindings.
1543 *
1544 * cpumask should be already set to mask of cpu_dev->id.
1545 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1546 */
1547int of_get_cpus_sharing_opps(struct device *cpu_dev, cpumask_var_t cpumask)
1548{
1549 struct device_node *np, *tmp_np;
1550 struct device *tcpu_dev;
1551 int cpu, ret = 0;
1552
1553 /* Get OPP descriptor node */
1554 np = _of_get_opp_desc_node(cpu_dev);
1555 if (IS_ERR(np)) {
1556 dev_dbg(cpu_dev, "%s: Couldn't find opp node: %ld\n", __func__,
1557 PTR_ERR(np));
1558 return -ENOENT;
1559 }
1560
1561 /* OPPs are shared ? */
1562 if (!of_property_read_bool(np, "opp-shared"))
1563 goto put_cpu_node;
1564
1565 for_each_possible_cpu(cpu) {
1566 if (cpu == cpu_dev->id)
1567 continue;
1568
1569 tcpu_dev = get_cpu_device(cpu);
1570 if (!tcpu_dev) {
1571 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
1572 __func__, cpu);
1573 ret = -ENODEV;
1574 goto put_cpu_node;
1575 }
1576
1577 /* Get OPP descriptor node */
1578 tmp_np = _of_get_opp_desc_node(tcpu_dev);
1579 if (IS_ERR(tmp_np)) {
1580 dev_err(tcpu_dev, "%s: Couldn't find opp node: %ld\n",
1581 __func__, PTR_ERR(tmp_np));
1582 ret = PTR_ERR(tmp_np);
1583 goto put_cpu_node;
1584 }
1585
1586 /* CPUs are sharing opp node */
1587 if (np == tmp_np)
1588 cpumask_set_cpu(cpu, cpumask);
1589
1590 of_node_put(tmp_np);
1591 }
1592
1593put_cpu_node:
1594 of_node_put(np);
1595 return ret;
1596}
1597EXPORT_SYMBOL_GPL(of_get_cpus_sharing_opps);
Shawn Guob496dfb2012-09-05 01:09:12 +02001598#endif