blob: 0ebcea49145abfaa295ea532177ff6cb5f9b5157 [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
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/err.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020017#include <linux/slab.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050018#include <linux/device.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020019#include <linux/list.h>
20#include <linux/rculist.h>
21#include <linux/rcupdate.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050022#include <linux/pm_opp.h>
Shawn Guob496dfb2012-09-05 01:09:12 +020023#include <linux/of.h>
Liam Girdwood80126ce2012-10-23 01:27:44 +020024#include <linux/export.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020025
26/*
27 * Internal data structure organization with the OPP layer library is as
28 * follows:
29 * dev_opp_list (root)
30 * |- device 1 (represents voltage domain 1)
31 * | |- opp 1 (availability, freq, voltage)
32 * | |- opp 2 ..
33 * ... ...
34 * | `- opp n ..
35 * |- device 2 (represents the next voltage domain)
36 * ...
37 * `- device m (represents mth voltage domain)
38 * device 1, 2.. are represented by dev_opp structure while each opp
39 * is represented by the opp structure.
40 */
41
42/**
Nishanth Menon47d43ba2013-09-19 16:03:51 -050043 * struct dev_pm_opp - Generic OPP description structure
Nishanth Menone1f60b22010-10-13 00:13:10 +020044 * @node: opp list node. The nodes are maintained throughout the lifetime
45 * of boot. It is expected only an optimal set of OPPs are
46 * added to the library by the SoC framework.
47 * RCU usage: opp list is traversed with RCU locks. node
48 * modification is possible realtime, hence the modifications
49 * are protected by the dev_opp_list_lock for integrity.
50 * IMPORTANT: the opp nodes should be maintained in increasing
51 * order.
Viresh Kumar383934092014-11-25 16:04:18 +053052 * @dynamic: not-created from static DT entries.
Nishanth Menone1f60b22010-10-13 00:13:10 +020053 * @available: true/false - marks if this OPP as available or not
Viresh Kumar27465902015-07-29 16:23:02 +053054 * @turbo: true if turbo (boost) OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +020055 * @rate: Frequency in hertz
Viresh Kumar27465902015-07-29 16:23:02 +053056 * @u_volt: Target voltage in microvolts corresponding to this OPP
57 * @u_volt_min: Minimum voltage in microvolts corresponding to this OPP
58 * @u_volt_max: Maximum voltage in microvolts corresponding to this OPP
59 * @u_amp: Maximum current drawn by the device in microamperes
Viresh Kumar3ca9bb32015-07-29 16:23:03 +053060 * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's
61 * frequency from any other OPP's frequency.
Nishanth Menone1f60b22010-10-13 00:13:10 +020062 * @dev_opp: points back to the device_opp struct this opp belongs to
Viresh Kumarcd1a0682014-11-25 16:04:16 +053063 * @rcu_head: RCU callback head used for deferred freeing
Viresh Kumar27465902015-07-29 16:23:02 +053064 * @np: OPP's device node.
Nishanth Menone1f60b22010-10-13 00:13:10 +020065 *
66 * This structure stores the OPP information for a given device.
67 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -050068struct dev_pm_opp {
Nishanth Menone1f60b22010-10-13 00:13:10 +020069 struct list_head node;
70
71 bool available;
Viresh Kumar383934092014-11-25 16:04:18 +053072 bool dynamic;
Viresh Kumar27465902015-07-29 16:23:02 +053073 bool turbo;
Nishanth Menone1f60b22010-10-13 00:13:10 +020074 unsigned long rate;
Viresh Kumar27465902015-07-29 16:23:02 +053075
Nishanth Menone1f60b22010-10-13 00:13:10 +020076 unsigned long u_volt;
Viresh Kumar27465902015-07-29 16:23:02 +053077 unsigned long u_volt_min;
78 unsigned long u_volt_max;
79 unsigned long u_amp;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +053080 unsigned long clock_latency_ns;
Nishanth Menone1f60b22010-10-13 00:13:10 +020081
82 struct device_opp *dev_opp;
Viresh Kumarcd1a0682014-11-25 16:04:16 +053083 struct rcu_head rcu_head;
Viresh Kumar27465902015-07-29 16:23:02 +053084
85 struct device_node *np;
Nishanth Menone1f60b22010-10-13 00:13:10 +020086};
87
88/**
Viresh Kumar06441652015-07-29 16:23:04 +053089 * struct device_list_opp - devices managed by 'struct device_opp'
90 * @node: list node
91 * @dev: device to which the struct object belongs
92 * @rcu_head: RCU callback head used for deferred freeing
93 *
94 * This is an internal data structure maintaining the list of devices that are
95 * managed by 'struct device_opp'.
96 */
97struct device_list_opp {
98 struct list_head node;
99 const struct device *dev;
100 struct rcu_head rcu_head;
101};
102
103/**
Nishanth Menone1f60b22010-10-13 00:13:10 +0200104 * struct device_opp - Device opp structure
105 * @node: list node - contains the devices with OPPs that
106 * have been registered. Nodes once added are not modified in this
107 * list.
108 * RCU usage: nodes are not modified in the list of device_opp,
109 * however addition is possible and is secured by dev_opp_list_lock
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530110 * @srcu_head: notifier head to notify the OPP availability changes.
Viresh Kumar129eec52014-11-27 08:54:06 +0530111 * @rcu_head: RCU callback head used for deferred freeing
Viresh Kumar06441652015-07-29 16:23:04 +0530112 * @dev_list: list of devices that share these OPPs
Nishanth Menone1f60b22010-10-13 00:13:10 +0200113 * @opp_list: list of opps
Viresh Kumar06441652015-07-29 16:23:04 +0530114 * @np: struct device_node pointer for opp's DT node.
115 * @shared_opp: OPP is shared between multiple devices.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200116 *
117 * This is an internal data structure maintaining the link to opps attached to
118 * a device. This structure is not meant to be shared to users as it is
Viresh Kumar1c6a6622014-12-10 09:45:31 +0530119 * meant for book keeping and private to OPP library.
120 *
121 * Because the opp structures can be used from both rcu and srcu readers, we
122 * need to wait for the grace period of both of them before freeing any
123 * resources. And so we have used kfree_rcu() from within call_srcu() handlers.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200124 */
125struct device_opp {
126 struct list_head node;
127
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530128 struct srcu_notifier_head srcu_head;
Viresh Kumar129eec52014-11-27 08:54:06 +0530129 struct rcu_head rcu_head;
Viresh Kumar06441652015-07-29 16:23:04 +0530130 struct list_head dev_list;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200131 struct list_head opp_list;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530132
Viresh Kumar06441652015-07-29 16:23:04 +0530133 struct device_node *np;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530134 unsigned long clock_latency_ns_max;
Viresh Kumar06441652015-07-29 16:23:04 +0530135 bool shared_opp;
Viresh Kumarad656a62015-06-13 15:10:21 +0530136 struct dev_pm_opp *suspend_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200137};
138
139/*
140 * The root of the list of all devices. All device_opp structures branch off
141 * from here, with each device_opp containing the list of opp it supports in
142 * various states of availability.
143 */
144static LIST_HEAD(dev_opp_list);
145/* Lock to allow exclusive modification to the device and opp lists */
146static DEFINE_MUTEX(dev_opp_list_lock);
147
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800148#define opp_rcu_lockdep_assert() \
149do { \
150 rcu_lockdep_assert(rcu_read_lock_held() || \
151 lockdep_is_held(&dev_opp_list_lock), \
152 "Missing rcu_read_lock() or " \
153 "dev_opp_list_lock protection"); \
154} while (0)
155
Viresh Kumar06441652015-07-29 16:23:04 +0530156static struct device_list_opp *_find_list_dev(const struct device *dev,
157 struct device_opp *dev_opp)
158{
159 struct device_list_opp *list_dev;
160
161 list_for_each_entry(list_dev, &dev_opp->dev_list, node)
162 if (list_dev->dev == dev)
163 return list_dev;
164
165 return NULL;
166}
167
168static struct device_opp *_managed_opp(const struct device_node *np)
169{
170 struct device_opp *dev_opp;
171
172 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) {
173 if (dev_opp->np == np) {
174 /*
175 * Multiple devices can point to the same OPP table and
176 * so will have same node-pointer, np.
177 *
178 * But the OPPs will be considered as shared only if the
179 * OPP table contains a "opp-shared" property.
180 */
181 return dev_opp->shared_opp ? dev_opp : NULL;
182 }
183 }
184
185 return NULL;
186}
187
Nishanth Menone1f60b22010-10-13 00:13:10 +0200188/**
Nishanth Menon327854c2014-12-24 11:22:56 -0600189 * _find_device_opp() - find device_opp struct using device pointer
Nishanth Menone1f60b22010-10-13 00:13:10 +0200190 * @dev: device pointer used to lookup device OPPs
191 *
192 * Search list of device OPPs for one containing matching device. Does a RCU
193 * reader operation to grab the pointer needed.
194 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600195 * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +0200196 * -EINVAL based on type of error.
197 *
198 * Locking: This function must be called under rcu_read_lock(). device_opp
199 * is a RCU protected pointer. This means that device_opp is valid as long
200 * as we are under RCU lock.
201 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600202static struct device_opp *_find_device_opp(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200203{
Viresh Kumar06441652015-07-29 16:23:04 +0530204 struct device_opp *dev_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200205
206 if (unlikely(IS_ERR_OR_NULL(dev))) {
207 pr_err("%s: Invalid parameters\n", __func__);
208 return ERR_PTR(-EINVAL);
209 }
210
Viresh Kumar06441652015-07-29 16:23:04 +0530211 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node)
212 if (_find_list_dev(dev, dev_opp))
213 return dev_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200214
Viresh Kumar06441652015-07-29 16:23:04 +0530215 return ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200216}
217
218/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500219 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200220 * @opp: opp for which voltage has to be returned for
221 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600222 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200223 * return 0
224 *
225 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
226 * protected pointer. This means that opp which could have been fetched by
227 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
228 * under RCU lock. The pointer returned by the opp_find_freq family must be
229 * used in the same section as the usage of this function with the pointer
230 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
231 * pointer.
232 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500233unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200234{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500235 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200236 unsigned long v = 0;
237
Krzysztof Kozlowski04bf1c72015-01-09 09:27:57 +0100238 opp_rcu_lockdep_assert();
239
Nishanth Menone1f60b22010-10-13 00:13:10 +0200240 tmp_opp = rcu_dereference(opp);
241 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
242 pr_err("%s: Invalid parameters\n", __func__);
243 else
244 v = tmp_opp->u_volt;
245
246 return v;
247}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500248EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200249
250/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500251 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200252 * @opp: opp for which frequency has to be returned for
253 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600254 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200255 * return 0
256 *
257 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
258 * protected pointer. This means that opp which could have been fetched by
259 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
260 * under RCU lock. The pointer returned by the opp_find_freq family must be
261 * used in the same section as the usage of this function with the pointer
262 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
263 * pointer.
264 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500265unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200266{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500267 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200268 unsigned long f = 0;
269
Krzysztof Kozlowski04bf1c72015-01-09 09:27:57 +0100270 opp_rcu_lockdep_assert();
271
Nishanth Menone1f60b22010-10-13 00:13:10 +0200272 tmp_opp = rcu_dereference(opp);
273 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
274 pr_err("%s: Invalid parameters\n", __func__);
275 else
276 f = tmp_opp->rate;
277
278 return f;
279}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500280EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200281
282/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530283 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
284 * @dev: device for which we do this operation
285 *
286 * Return: This function returns the max clock latency in nanoseconds.
287 *
288 * Locking: This function takes rcu_read_lock().
289 */
290unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
291{
292 struct device_opp *dev_opp;
293 unsigned long clock_latency_ns;
294
295 rcu_read_lock();
296
297 dev_opp = _find_device_opp(dev);
298 if (IS_ERR(dev_opp))
299 clock_latency_ns = 0;
300 else
301 clock_latency_ns = dev_opp->clock_latency_ns_max;
302
303 rcu_read_unlock();
304 return clock_latency_ns;
305}
306EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
307
308/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500309 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
Nishanth Menone1f60b22010-10-13 00:13:10 +0200310 * @dev: device for which we do this operation
311 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600312 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200313 * else returns 0 if none or the corresponding error value.
314 *
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800315 * Locking: This function takes rcu_read_lock().
Nishanth Menone1f60b22010-10-13 00:13:10 +0200316 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500317int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200318{
319 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500320 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200321 int count = 0;
322
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800323 rcu_read_lock();
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800324
Nishanth Menon327854c2014-12-24 11:22:56 -0600325 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200326 if (IS_ERR(dev_opp)) {
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800327 count = PTR_ERR(dev_opp);
328 dev_err(dev, "%s: device OPP not found (%d)\n",
329 __func__, count);
330 goto out_unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200331 }
332
333 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
334 if (temp_opp->available)
335 count++;
336 }
337
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800338out_unlock:
339 rcu_read_unlock();
Nishanth Menone1f60b22010-10-13 00:13:10 +0200340 return count;
341}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500342EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200343
344/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500345 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200346 * @dev: device for which we do this operation
347 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100348 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200349 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600350 * Return: Searches for exact match in the opp list and returns pointer to the
351 * matching opp if found, else returns ERR_PTR in case of error and should
352 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200353 * EINVAL: for bad pointer
354 * ERANGE: no match found for search
355 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200356 *
357 * Note: available is a modifier for the search. if available=true, then the
358 * match is for exact matching frequency and is available in the stored OPP
359 * table. if false, the match is for exact frequency which is not available.
360 *
361 * This provides a mechanism to enable an opp which is not available currently
362 * or the opposite as well.
363 *
364 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
365 * protected pointer. The reason for the same is that the opp pointer which is
366 * returned will remain valid for use with opp_get_{voltage, freq} only while
367 * under the locked area. The pointer returned must be used prior to unlocking
368 * with rcu_read_unlock() to maintain the integrity of the pointer.
369 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500370struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
371 unsigned long freq,
372 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200373{
374 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500375 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200376
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800377 opp_rcu_lockdep_assert();
378
Nishanth Menon327854c2014-12-24 11:22:56 -0600379 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200380 if (IS_ERR(dev_opp)) {
381 int r = PTR_ERR(dev_opp);
382 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
383 return ERR_PTR(r);
384 }
385
386 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
387 if (temp_opp->available == available &&
388 temp_opp->rate == freq) {
389 opp = temp_opp;
390 break;
391 }
392 }
393
394 return opp;
395}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500396EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200397
398/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500399 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200400 * @dev: device for which we do this operation
401 * @freq: Start frequency
402 *
403 * Search for the matching ceil *available* OPP from a starting freq
404 * for a device.
405 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600406 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200407 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
408 * values can be:
409 * EINVAL: for bad pointer
410 * ERANGE: no match found for search
411 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200412 *
413 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
414 * protected pointer. The reason for the same is that the opp pointer which is
415 * returned will remain valid for use with opp_get_{voltage, freq} only while
416 * under the locked area. The pointer returned must be used prior to unlocking
417 * with rcu_read_unlock() to maintain the integrity of the pointer.
418 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500419struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
420 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200421{
422 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500423 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200424
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800425 opp_rcu_lockdep_assert();
426
Nishanth Menone1f60b22010-10-13 00:13:10 +0200427 if (!dev || !freq) {
428 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
429 return ERR_PTR(-EINVAL);
430 }
431
Nishanth Menon327854c2014-12-24 11:22:56 -0600432 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200433 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200434 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200435
436 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
437 if (temp_opp->available && temp_opp->rate >= *freq) {
438 opp = temp_opp;
439 *freq = opp->rate;
440 break;
441 }
442 }
443
444 return opp;
445}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500446EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200447
448/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500449 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200450 * @dev: device for which we do this operation
451 * @freq: Start frequency
452 *
453 * Search for the matching floor *available* OPP from a starting freq
454 * for a device.
455 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600456 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200457 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
458 * values can be:
459 * EINVAL: for bad pointer
460 * ERANGE: no match found for search
461 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200462 *
463 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
464 * protected pointer. The reason for the same is that the opp pointer which is
465 * returned will remain valid for use with opp_get_{voltage, freq} only while
466 * under the locked area. The pointer returned must be used prior to unlocking
467 * with rcu_read_unlock() to maintain the integrity of the pointer.
468 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500469struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
470 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200471{
472 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500473 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200474
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800475 opp_rcu_lockdep_assert();
476
Nishanth Menone1f60b22010-10-13 00:13:10 +0200477 if (!dev || !freq) {
478 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
479 return ERR_PTR(-EINVAL);
480 }
481
Nishanth Menon327854c2014-12-24 11:22:56 -0600482 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200483 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200484 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200485
486 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
487 if (temp_opp->available) {
488 /* go to the next node, before choosing prev */
489 if (temp_opp->rate > *freq)
490 break;
491 else
492 opp = temp_opp;
493 }
494 }
495 if (!IS_ERR(opp))
496 *freq = opp->rate;
497
498 return opp;
499}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500500EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200501
Viresh Kumar06441652015-07-29 16:23:04 +0530502/* List-dev Helpers */
503static void _kfree_list_dev_rcu(struct rcu_head *head)
504{
505 struct device_list_opp *list_dev;
506
507 list_dev = container_of(head, struct device_list_opp, rcu_head);
508 kfree_rcu(list_dev, rcu_head);
509}
510
511static void _remove_list_dev(struct device_list_opp *list_dev,
512 struct device_opp *dev_opp)
513{
514 list_del(&list_dev->node);
515 call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head,
516 _kfree_list_dev_rcu);
517}
518
519static struct device_list_opp *_add_list_dev(const struct device *dev,
520 struct device_opp *dev_opp)
521{
522 struct device_list_opp *list_dev;
523
524 list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL);
525 if (!list_dev)
526 return NULL;
527
528 /* Initialize list-dev */
529 list_dev->dev = dev;
530 list_add_rcu(&list_dev->node, &dev_opp->dev_list);
531
532 return list_dev;
533}
534
Nishanth Menon984f16c2014-12-24 11:22:57 -0600535/**
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530536 * _add_device_opp() - Find device OPP table or allocate a new one
Nishanth Menon984f16c2014-12-24 11:22:57 -0600537 * @dev: device for which we do this operation
538 *
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530539 * It tries to find an existing table first, if it couldn't find one, it
540 * allocates a new OPP table and returns that.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600541 *
542 * Return: valid device_opp pointer if success, else NULL.
543 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600544static struct device_opp *_add_device_opp(struct device *dev)
Viresh Kumar07cce742014-12-10 09:45:34 +0530545{
546 struct device_opp *dev_opp;
Viresh Kumar06441652015-07-29 16:23:04 +0530547 struct device_list_opp *list_dev;
Viresh Kumar07cce742014-12-10 09:45:34 +0530548
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530549 /* Check for existing list for 'dev' first */
550 dev_opp = _find_device_opp(dev);
551 if (!IS_ERR(dev_opp))
552 return dev_opp;
553
Viresh Kumar07cce742014-12-10 09:45:34 +0530554 /*
555 * Allocate a new device OPP table. In the infrequent case where a new
556 * device is needed to be added, we pay this penalty.
557 */
558 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
559 if (!dev_opp)
560 return NULL;
561
Viresh Kumar06441652015-07-29 16:23:04 +0530562 INIT_LIST_HEAD(&dev_opp->dev_list);
563
564 list_dev = _add_list_dev(dev, dev_opp);
565 if (!list_dev) {
566 kfree(dev_opp);
567 return NULL;
568 }
569
Viresh Kumar07cce742014-12-10 09:45:34 +0530570 srcu_init_notifier_head(&dev_opp->srcu_head);
571 INIT_LIST_HEAD(&dev_opp->opp_list);
572
573 /* Secure the device list modification */
574 list_add_rcu(&dev_opp->node, &dev_opp_list);
575 return dev_opp;
576}
577
Nishanth Menon984f16c2014-12-24 11:22:57 -0600578/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530579 * _kfree_device_rcu() - Free device_opp RCU handler
580 * @head: RCU head
581 */
582static void _kfree_device_rcu(struct rcu_head *head)
583{
584 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
585
586 kfree_rcu(device_opp, rcu_head);
587}
588
589/**
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530590 * _remove_device_opp() - Removes a device OPP table
591 * @dev_opp: device OPP table to be removed.
592 *
593 * Removes/frees device OPP table it it doesn't contain any OPPs.
594 */
595static void _remove_device_opp(struct device_opp *dev_opp)
596{
Viresh Kumar06441652015-07-29 16:23:04 +0530597 struct device_list_opp *list_dev;
598
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530599 if (!list_empty(&dev_opp->opp_list))
600 return;
601
Viresh Kumar06441652015-07-29 16:23:04 +0530602 list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp,
603 node);
604
605 _remove_list_dev(list_dev, dev_opp);
606
607 /* dev_list must be empty now */
608 WARN_ON(!list_empty(&dev_opp->dev_list));
609
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530610 list_del_rcu(&dev_opp->node);
611 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
612 _kfree_device_rcu);
613}
614
615/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530616 * _kfree_opp_rcu() - Free OPP RCU handler
617 * @head: RCU head
618 */
619static void _kfree_opp_rcu(struct rcu_head *head)
620{
621 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
622
623 kfree_rcu(opp, rcu_head);
624}
625
626/**
627 * _opp_remove() - Remove an OPP from a table definition
628 * @dev_opp: points back to the device_opp struct this opp belongs to
629 * @opp: pointer to the OPP to remove
Viresh Kumar23dacf62015-07-29 16:23:01 +0530630 * @notify: OPP_EVENT_REMOVE notification should be sent or not
Viresh Kumar737002b2015-07-29 16:22:58 +0530631 *
632 * This function removes an opp definition from the opp list.
633 *
634 * Locking: The internal device_opp and opp structures are RCU protected.
635 * It is assumed that the caller holds required mutex for an RCU updater
636 * strategy.
637 */
638static void _opp_remove(struct device_opp *dev_opp,
Viresh Kumar23dacf62015-07-29 16:23:01 +0530639 struct dev_pm_opp *opp, bool notify)
Viresh Kumar737002b2015-07-29 16:22:58 +0530640{
641 /*
642 * Notify the changes in the availability of the operable
643 * frequency/voltage list.
644 */
Viresh Kumar23dacf62015-07-29 16:23:01 +0530645 if (notify)
646 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530647 list_del_rcu(&opp->node);
648 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
649
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530650 _remove_device_opp(dev_opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530651}
652
653/**
654 * dev_pm_opp_remove() - Remove an OPP from OPP list
655 * @dev: device for which we do this operation
656 * @freq: OPP to remove with matching 'freq'
657 *
658 * This function removes an opp from the opp list.
659 *
660 * Locking: The internal device_opp and opp structures are RCU protected.
661 * Hence this function internally uses RCU updater strategy with mutex locks
662 * to keep the integrity of the internal data structures. Callers should ensure
663 * that this function is *NOT* called under RCU protection or in contexts where
664 * mutex cannot be locked.
665 */
666void dev_pm_opp_remove(struct device *dev, unsigned long freq)
667{
668 struct dev_pm_opp *opp;
669 struct device_opp *dev_opp;
670 bool found = false;
671
672 /* Hold our list modification lock here */
673 mutex_lock(&dev_opp_list_lock);
674
675 dev_opp = _find_device_opp(dev);
676 if (IS_ERR(dev_opp))
677 goto unlock;
678
679 list_for_each_entry(opp, &dev_opp->opp_list, node) {
680 if (opp->rate == freq) {
681 found = true;
682 break;
683 }
684 }
685
686 if (!found) {
687 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
688 __func__, freq);
689 goto unlock;
690 }
691
Viresh Kumar23dacf62015-07-29 16:23:01 +0530692 _opp_remove(dev_opp, opp, true);
Viresh Kumar737002b2015-07-29 16:22:58 +0530693unlock:
694 mutex_unlock(&dev_opp_list_lock);
695}
696EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
697
Viresh Kumar23dacf62015-07-29 16:23:01 +0530698static struct dev_pm_opp *_allocate_opp(struct device *dev,
699 struct device_opp **dev_opp)
700{
701 struct dev_pm_opp *opp;
702
703 /* allocate new OPP node */
704 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
705 if (!opp)
706 return NULL;
707
708 INIT_LIST_HEAD(&opp->node);
709
710 *dev_opp = _add_device_opp(dev);
711 if (!*dev_opp) {
712 kfree(opp);
713 return NULL;
714 }
715
716 return opp;
717}
718
Viresh Kumar06441652015-07-29 16:23:04 +0530719static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
720 struct device_opp *dev_opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530721{
722 struct dev_pm_opp *opp;
723 struct list_head *head = &dev_opp->opp_list;
724
725 /*
726 * Insert new OPP in order of increasing frequency and discard if
727 * already present.
728 *
729 * Need to use &dev_opp->opp_list in the condition part of the 'for'
730 * loop, don't replace it with head otherwise it will become an infinite
731 * loop.
732 */
733 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
734 if (new_opp->rate > opp->rate) {
735 head = &opp->node;
736 continue;
737 }
738
739 if (new_opp->rate < opp->rate)
740 break;
741
742 /* Duplicate OPPs */
Viresh Kumar06441652015-07-29 16:23:04 +0530743 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 +0530744 __func__, opp->rate, opp->u_volt, opp->available,
745 new_opp->rate, new_opp->u_volt, new_opp->available);
746
747 return opp->available && new_opp->u_volt == opp->u_volt ?
748 0 : -EEXIST;
749 }
750
751 new_opp->dev_opp = dev_opp;
752 list_add_rcu(&new_opp->node, head);
753
754 return 0;
755}
756
Viresh Kumar737002b2015-07-29 16:22:58 +0530757/**
Nishanth Menon984f16c2014-12-24 11:22:57 -0600758 * _opp_add_dynamic() - Allocate a dynamic OPP.
759 * @dev: device for which we do this operation
760 * @freq: Frequency in Hz for this OPP
761 * @u_volt: Voltage in uVolts for this OPP
762 * @dynamic: Dynamically added OPPs.
763 *
764 * This function adds an opp definition to the opp list and returns status.
765 * The opp is made available by default and it can be controlled using
766 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
767 *
768 * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and
769 * freed by of_free_opp_table.
770 *
771 * Locking: The internal device_opp and opp structures are RCU protected.
772 * Hence this function internally uses RCU updater strategy with mutex locks
773 * to keep the integrity of the internal data structures. Callers should ensure
774 * that this function is *NOT* called under RCU protection or in contexts where
775 * mutex cannot be locked.
776 *
777 * Return:
778 * 0 On success OR
779 * Duplicate OPPs (both freq and volt are same) and opp->available
780 * -EEXIST Freq are same and volt are different OR
781 * Duplicate OPPs (both freq and volt are same) and !opp->available
782 * -ENOMEM Memory allocation failure
783 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600784static int _opp_add_dynamic(struct device *dev, unsigned long freq,
785 long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200786{
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530787 struct device_opp *dev_opp;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530788 struct dev_pm_opp *new_opp;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530789 int ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200790
Nishanth Menone1f60b22010-10-13 00:13:10 +0200791 /* Hold our list modification lock here */
792 mutex_lock(&dev_opp_list_lock);
793
Viresh Kumar23dacf62015-07-29 16:23:01 +0530794 new_opp = _allocate_opp(dev, &dev_opp);
795 if (!new_opp) {
796 ret = -ENOMEM;
797 goto unlock;
798 }
799
Viresh Kumara7470db2014-11-25 16:04:17 +0530800 /* populate the opp table */
Viresh Kumara7470db2014-11-25 16:04:17 +0530801 new_opp->rate = freq;
802 new_opp->u_volt = u_volt;
803 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530804 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530805
Viresh Kumar06441652015-07-29 16:23:04 +0530806 ret = _opp_add(dev, new_opp, dev_opp);
Viresh Kumar23dacf62015-07-29 16:23:01 +0530807 if (ret)
808 goto free_opp;
809
Nishanth Menone1f60b22010-10-13 00:13:10 +0200810 mutex_unlock(&dev_opp_list_lock);
811
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200812 /*
813 * Notify the changes in the availability of the operable
814 * frequency/voltage list.
815 */
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530816 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200817 return 0;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530818
819free_opp:
Viresh Kumar23dacf62015-07-29 16:23:01 +0530820 _opp_remove(dev_opp, new_opp, false);
821unlock:
Viresh Kumar6ce41842014-12-10 09:45:35 +0530822 mutex_unlock(&dev_opp_list_lock);
Viresh Kumar6ce41842014-12-10 09:45:35 +0530823 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200824}
Viresh Kumar383934092014-11-25 16:04:18 +0530825
Viresh Kumar27465902015-07-29 16:23:02 +0530826/* TODO: Support multiple regulators */
827static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev)
828{
829 u32 microvolt[3] = {0};
830 int count, ret;
831
832 count = of_property_count_u32_elems(opp->np, "opp-microvolt");
833 if (!count)
834 return 0;
835
836 /* There can be one or three elements here */
837 if (count != 1 && count != 3) {
838 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
839 __func__, count);
840 return -EINVAL;
841 }
842
843 ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
844 count);
845 if (ret) {
846 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
847 ret);
848 return -EINVAL;
849 }
850
851 opp->u_volt = microvolt[0];
852 opp->u_volt_min = microvolt[1];
853 opp->u_volt_max = microvolt[2];
854
855 return 0;
856}
857
858/**
859 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
860 * @dev: device for which we do this operation
861 * @np: device node
862 *
863 * This function adds an opp definition to the opp list and returns status. The
864 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
865 * removed by dev_pm_opp_remove.
866 *
867 * Locking: The internal device_opp and opp structures are RCU protected.
868 * Hence this function internally uses RCU updater strategy with mutex locks
869 * to keep the integrity of the internal data structures. Callers should ensure
870 * that this function is *NOT* called under RCU protection or in contexts where
871 * mutex cannot be locked.
872 *
873 * Return:
874 * 0 On success OR
875 * Duplicate OPPs (both freq and volt are same) and opp->available
876 * -EEXIST Freq are same and volt are different OR
877 * Duplicate OPPs (both freq and volt are same) and !opp->available
878 * -ENOMEM Memory allocation failure
879 * -EINVAL Failed parsing the OPP node
880 */
881static int _opp_add_static_v2(struct device *dev, struct device_node *np)
882{
883 struct device_opp *dev_opp;
884 struct dev_pm_opp *new_opp;
885 u64 rate;
886 int ret;
887
888 /* Hold our list modification lock here */
889 mutex_lock(&dev_opp_list_lock);
890
891 new_opp = _allocate_opp(dev, &dev_opp);
892 if (!new_opp) {
893 ret = -ENOMEM;
894 goto unlock;
895 }
896
897 ret = of_property_read_u64(np, "opp-hz", &rate);
898 if (ret < 0) {
899 dev_err(dev, "%s: opp-hz not found\n", __func__);
900 goto free_opp;
901 }
902
903 /*
904 * Rate is defined as an unsigned long in clk API, and so casting
905 * explicitly to its type. Must be fixed once rate is 64 bit
906 * guaranteed in clk API.
907 */
908 new_opp->rate = (unsigned long)rate;
909 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
910
911 new_opp->np = np;
912 new_opp->dynamic = false;
913 new_opp->available = true;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530914 of_property_read_u32(np, "clock-latency-ns",
915 (u32 *)&new_opp->clock_latency_ns);
Viresh Kumar27465902015-07-29 16:23:02 +0530916
917 ret = opp_get_microvolt(new_opp, dev);
918 if (ret)
919 goto free_opp;
920
921 of_property_read_u32(np, "opp-microamp", (u32 *)&new_opp->u_amp);
922
Viresh Kumar06441652015-07-29 16:23:04 +0530923 ret = _opp_add(dev, new_opp, dev_opp);
Viresh Kumar27465902015-07-29 16:23:02 +0530924 if (ret)
925 goto free_opp;
926
Viresh Kumarad656a62015-06-13 15:10:21 +0530927 /* OPP to select on device suspend */
928 if (of_property_read_bool(np, "opp-suspend")) {
929 if (dev_opp->suspend_opp)
930 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
931 __func__, dev_opp->suspend_opp->rate,
932 new_opp->rate);
933 else
934 dev_opp->suspend_opp = new_opp;
935 }
936
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530937 if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max)
938 dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns;
939
Viresh Kumar27465902015-07-29 16:23:02 +0530940 mutex_unlock(&dev_opp_list_lock);
941
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530942 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
Viresh Kumar27465902015-07-29 16:23:02 +0530943 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530944 new_opp->u_volt_min, new_opp->u_volt_max,
945 new_opp->clock_latency_ns);
Viresh Kumar27465902015-07-29 16:23:02 +0530946
947 /*
948 * Notify the changes in the availability of the operable
949 * frequency/voltage list.
950 */
951 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
952 return 0;
953
954free_opp:
955 _opp_remove(dev_opp, new_opp, false);
956unlock:
957 mutex_unlock(&dev_opp_list_lock);
958 return ret;
959}
960
Viresh Kumar383934092014-11-25 16:04:18 +0530961/**
962 * dev_pm_opp_add() - Add an OPP table from a table definitions
963 * @dev: device for which we do this operation
964 * @freq: Frequency in Hz for this OPP
965 * @u_volt: Voltage in uVolts for this OPP
966 *
967 * This function adds an opp definition to the opp list and returns status.
968 * The opp is made available by default and it can be controlled using
969 * dev_pm_opp_enable/disable functions.
970 *
971 * Locking: The internal device_opp and opp structures are RCU protected.
972 * Hence this function internally uses RCU updater strategy with mutex locks
973 * to keep the integrity of the internal data structures. Callers should ensure
974 * that this function is *NOT* called under RCU protection or in contexts where
975 * mutex cannot be locked.
976 *
977 * Return:
Nishanth Menon984f16c2014-12-24 11:22:57 -0600978 * 0 On success OR
Viresh Kumar383934092014-11-25 16:04:18 +0530979 * Duplicate OPPs (both freq and volt are same) and opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -0600980 * -EEXIST Freq are same and volt are different OR
Viresh Kumar383934092014-11-25 16:04:18 +0530981 * Duplicate OPPs (both freq and volt are same) and !opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -0600982 * -ENOMEM Memory allocation failure
Viresh Kumar383934092014-11-25 16:04:18 +0530983 */
984int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
985{
Nishanth Menon327854c2014-12-24 11:22:56 -0600986 return _opp_add_dynamic(dev, freq, u_volt, true);
Viresh Kumar383934092014-11-25 16:04:18 +0530987}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500988EXPORT_SYMBOL_GPL(dev_pm_opp_add);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200989
Nishanth Menon984f16c2014-12-24 11:22:57 -0600990/**
Nishanth Menon327854c2014-12-24 11:22:56 -0600991 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200992 * @dev: device for which we do this operation
993 * @freq: OPP frequency to modify availability
994 * @availability_req: availability status requested for this opp
995 *
996 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
997 * share a common logic which is isolated here.
998 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600999 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Nishanth Menone1f60b22010-10-13 00:13:10 +02001000 * copy operation, returns 0 if no modifcation was done OR modification was
1001 * successful.
1002 *
1003 * Locking: The internal device_opp and opp structures are RCU protected.
1004 * Hence this function internally uses RCU updater strategy with mutex locks to
1005 * keep the integrity of the internal data structures. Callers should ensure
1006 * that this function is *NOT* called under RCU protection or in contexts where
1007 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1008 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001009static int _opp_set_availability(struct device *dev, unsigned long freq,
1010 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001011{
Viresh Kumar29df0ee2014-12-10 09:45:33 +05301012 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001013 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001014 int r = 0;
1015
1016 /* keep the node allocated */
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001017 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
Quentin Lambert59d84ca2015-02-09 10:45:32 +01001018 if (!new_opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001019 return -ENOMEM;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001020
1021 mutex_lock(&dev_opp_list_lock);
1022
1023 /* Find the device_opp */
Nishanth Menon327854c2014-12-24 11:22:56 -06001024 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001025 if (IS_ERR(dev_opp)) {
1026 r = PTR_ERR(dev_opp);
1027 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1028 goto unlock;
1029 }
1030
1031 /* Do we have the frequency? */
1032 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
1033 if (tmp_opp->rate == freq) {
1034 opp = tmp_opp;
1035 break;
1036 }
1037 }
1038 if (IS_ERR(opp)) {
1039 r = PTR_ERR(opp);
1040 goto unlock;
1041 }
1042
1043 /* Is update really needed? */
1044 if (opp->available == availability_req)
1045 goto unlock;
1046 /* copy the old data over */
1047 *new_opp = *opp;
1048
1049 /* plug in new node */
1050 new_opp->available = availability_req;
1051
1052 list_replace_rcu(&opp->node, &new_opp->node);
1053 mutex_unlock(&dev_opp_list_lock);
Nishanth Menon327854c2014-12-24 11:22:56 -06001054 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001055
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001056 /* Notify the change of the OPP availability */
1057 if (availability_req)
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301058 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001059 new_opp);
1060 else
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301061 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001062 new_opp);
1063
Vincent Guittotdde84372012-10-23 01:21:49 +02001064 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001065
1066unlock:
1067 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001068 kfree(new_opp);
1069 return r;
1070}
1071
1072/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001073 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001074 * @dev: device for which we do this operation
1075 * @freq: OPP frequency to enable
1076 *
1077 * Enables a provided opp. If the operation is valid, this returns 0, else the
1078 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001079 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001080 *
1081 * Locking: The internal device_opp and opp structures are RCU protected.
1082 * Hence this function indirectly uses RCU and mutex locks to keep the
1083 * integrity of the internal data structures. Callers should ensure that
1084 * this function is *NOT* called under RCU protection or in contexts where
1085 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001086 *
1087 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1088 * copy operation, returns 0 if no modifcation was done OR modification was
1089 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001090 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001091int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001092{
Nishanth Menon327854c2014-12-24 11:22:56 -06001093 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001094}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001095EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001096
1097/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001098 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001099 * @dev: device for which we do this operation
1100 * @freq: OPP frequency to disable
1101 *
1102 * Disables a provided opp. If the operation is valid, this returns
1103 * 0, else the corresponding error value. It is meant to be a temporary
1104 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001105 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001106 *
1107 * Locking: The internal device_opp and opp structures are RCU protected.
1108 * Hence this function indirectly uses RCU and mutex locks to keep the
1109 * integrity of the internal data structures. Callers should ensure that
1110 * this function is *NOT* called under RCU protection or in contexts where
1111 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001112 *
1113 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1114 * copy operation, returns 0 if no modifcation was done OR modification was
1115 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001116 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001117int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001118{
Nishanth Menon327854c2014-12-24 11:22:56 -06001119 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001120}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001121EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001122
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001123/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001124 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001125 * @dev: device pointer used to lookup device OPPs.
Nishanth Menon984f16c2014-12-24 11:22:57 -06001126 *
1127 * Return: pointer to notifier head if found, otherwise -ENODEV or
1128 * -EINVAL based on type of error casted as pointer. value must be checked
1129 * with IS_ERR to determine valid pointer or error result.
1130 *
1131 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
1132 * protected pointer. The reason for the same is that the opp pointer which is
1133 * returned will remain valid for use with opp_get_{voltage, freq} only while
1134 * under the locked area. The pointer returned must be used prior to unlocking
1135 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001136 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001137struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001138{
Nishanth Menon327854c2014-12-24 11:22:56 -06001139 struct device_opp *dev_opp = _find_device_opp(dev);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001140
1141 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +01001142 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001143
Viresh Kumarcd1a0682014-11-25 16:04:16 +05301144 return &dev_opp->srcu_head;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001145}
Nishanth Menon4679ec32014-12-24 11:22:55 -06001146EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001147
1148#ifdef CONFIG_OF
1149/**
Viresh Kumar737002b2015-07-29 16:22:58 +05301150 * of_free_opp_table() - Free OPP table entries created from static DT entries
1151 * @dev: device pointer used to lookup device OPPs.
1152 *
1153 * Free OPPs created using static entries present in DT.
1154 *
1155 * Locking: The internal device_opp and opp structures are RCU protected.
1156 * Hence this function indirectly uses RCU updater strategy with mutex locks
1157 * to keep the integrity of the internal data structures. Callers should ensure
1158 * that this function is *NOT* called under RCU protection or in contexts where
1159 * mutex cannot be locked.
1160 */
1161void of_free_opp_table(struct device *dev)
1162{
1163 struct device_opp *dev_opp;
1164 struct dev_pm_opp *opp, *tmp;
1165
Viresh Kumar06441652015-07-29 16:23:04 +05301166 /* Hold our list modification lock here */
1167 mutex_lock(&dev_opp_list_lock);
1168
Viresh Kumar737002b2015-07-29 16:22:58 +05301169 /* Check for existing list for 'dev' */
1170 dev_opp = _find_device_opp(dev);
1171 if (IS_ERR(dev_opp)) {
1172 int error = PTR_ERR(dev_opp);
1173
1174 if (error != -ENODEV)
1175 WARN(1, "%s: dev_opp: %d\n",
1176 IS_ERR_OR_NULL(dev) ?
1177 "Invalid device" : dev_name(dev),
1178 error);
Viresh Kumar06441652015-07-29 16:23:04 +05301179 goto unlock;
Viresh Kumar737002b2015-07-29 16:22:58 +05301180 }
1181
Viresh Kumar06441652015-07-29 16:23:04 +05301182 /* Find if dev_opp manages a single device */
1183 if (list_is_singular(&dev_opp->dev_list)) {
1184 /* Free static OPPs */
1185 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1186 if (!opp->dynamic)
1187 _opp_remove(dev_opp, opp, true);
1188 }
1189 } else {
1190 _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp);
Viresh Kumar737002b2015-07-29 16:22:58 +05301191 }
1192
Viresh Kumar06441652015-07-29 16:23:04 +05301193unlock:
Viresh Kumar737002b2015-07-29 16:22:58 +05301194 mutex_unlock(&dev_opp_list_lock);
1195}
1196EXPORT_SYMBOL_GPL(of_free_opp_table);
1197
Viresh Kumar27465902015-07-29 16:23:02 +05301198/* Returns opp descriptor node from its phandle. Caller must do of_node_put() */
1199static struct device_node *
1200_of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop)
1201{
1202 struct device_node *opp_np;
1203
1204 opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value));
1205 if (!opp_np) {
1206 dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n",
1207 __func__, prop->name);
1208 return ERR_PTR(-EINVAL);
1209 }
1210
1211 return opp_np;
1212}
1213
1214/* Initializes OPP tables based on new bindings */
1215static int _of_init_opp_table_v2(struct device *dev,
1216 const struct property *prop)
1217{
1218 struct device_node *opp_np, *np;
Viresh Kumar06441652015-07-29 16:23:04 +05301219 struct device_opp *dev_opp;
Viresh Kumar27465902015-07-29 16:23:02 +05301220 int ret = 0, count = 0;
1221
1222 if (!prop->value)
1223 return -ENODATA;
1224
1225 /* Get opp node */
1226 opp_np = _of_get_opp_desc_node_from_prop(dev, prop);
1227 if (IS_ERR(opp_np))
1228 return PTR_ERR(opp_np);
1229
Viresh Kumar06441652015-07-29 16:23:04 +05301230 dev_opp = _managed_opp(opp_np);
1231 if (dev_opp) {
1232 /* OPPs are already managed */
1233 if (!_add_list_dev(dev, dev_opp))
1234 ret = -ENOMEM;
1235 goto put_opp_np;
1236 }
1237
Viresh Kumar27465902015-07-29 16:23:02 +05301238 /* We have opp-list node now, iterate over it and add OPPs */
1239 for_each_available_child_of_node(opp_np, np) {
1240 count++;
1241
1242 ret = _opp_add_static_v2(dev, np);
1243 if (ret) {
1244 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1245 ret);
1246 break;
1247 }
1248 }
1249
1250 /* There should be one of more OPP defined */
1251 if (WARN_ON(!count))
1252 goto put_opp_np;
1253
Viresh Kumar06441652015-07-29 16:23:04 +05301254 if (!ret) {
1255 if (!dev_opp) {
1256 dev_opp = _find_device_opp(dev);
1257 if (WARN_ON(!dev_opp))
1258 goto put_opp_np;
1259 }
1260
1261 dev_opp->np = opp_np;
1262 dev_opp->shared_opp = of_property_read_bool(opp_np,
1263 "opp-shared");
1264 } else {
Viresh Kumar27465902015-07-29 16:23:02 +05301265 of_free_opp_table(dev);
Viresh Kumar06441652015-07-29 16:23:04 +05301266 }
Viresh Kumar27465902015-07-29 16:23:02 +05301267
1268put_opp_np:
1269 of_node_put(opp_np);
1270
1271 return ret;
1272}
1273
1274/* Initializes OPP tables based on old-deprecated bindings */
1275static int _of_init_opp_table_v1(struct device *dev)
Shawn Guob496dfb2012-09-05 01:09:12 +02001276{
1277 const struct property *prop;
1278 const __be32 *val;
1279 int nr;
1280
1281 prop = of_find_property(dev->of_node, "operating-points", NULL);
1282 if (!prop)
1283 return -ENODEV;
1284 if (!prop->value)
1285 return -ENODATA;
1286
1287 /*
1288 * Each OPP is a set of tuples consisting of frequency and
1289 * voltage like <freq-kHz vol-uV>.
1290 */
1291 nr = prop->length / sizeof(u32);
1292 if (nr % 2) {
1293 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1294 return -EINVAL;
1295 }
1296
1297 val = prop->value;
1298 while (nr) {
1299 unsigned long freq = be32_to_cpup(val++) * 1000;
1300 unsigned long volt = be32_to_cpup(val++);
1301
Nishanth Menon327854c2014-12-24 11:22:56 -06001302 if (_opp_add_dynamic(dev, freq, volt, false))
Shawn Guob496dfb2012-09-05 01:09:12 +02001303 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1304 __func__, freq);
Shawn Guob496dfb2012-09-05 01:09:12 +02001305 nr -= 2;
1306 }
1307
1308 return 0;
1309}
Viresh Kumar27465902015-07-29 16:23:02 +05301310
1311/**
1312 * of_init_opp_table() - Initialize opp table from device tree
1313 * @dev: device pointer used to lookup device OPPs.
1314 *
1315 * Register the initial OPP table with the OPP library for given device.
1316 *
1317 * Locking: The internal device_opp and opp structures are RCU protected.
1318 * Hence this function indirectly uses RCU updater strategy with mutex locks
1319 * to keep the integrity of the internal data structures. Callers should ensure
1320 * that this function is *NOT* called under RCU protection or in contexts where
1321 * mutex cannot be locked.
1322 *
1323 * Return:
1324 * 0 On success OR
1325 * Duplicate OPPs (both freq and volt are same) and opp->available
1326 * -EEXIST Freq are same and volt are different OR
1327 * Duplicate OPPs (both freq and volt are same) and !opp->available
1328 * -ENOMEM Memory allocation failure
1329 * -ENODEV when 'operating-points' property is not found or is invalid data
1330 * in device node.
1331 * -ENODATA when empty 'operating-points' property is found
1332 * -EINVAL when invalid entries are found in opp-v2 table
1333 */
1334int of_init_opp_table(struct device *dev)
1335{
1336 const struct property *prop;
1337
1338 /*
1339 * OPPs have two version of bindings now. The older one is deprecated,
1340 * try for the new binding first.
1341 */
1342 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1343 if (!prop) {
1344 /*
1345 * Try old-deprecated bindings for backward compatibility with
1346 * older dtbs.
1347 */
1348 return _of_init_opp_table_v1(dev);
1349 }
1350
1351 return _of_init_opp_table_v2(dev, prop);
1352}
Mark Langsdorf74c46c62013-01-28 18:26:16 +00001353EXPORT_SYMBOL_GPL(of_init_opp_table);
Shawn Guob496dfb2012-09-05 01:09:12 +02001354#endif