Merge tag 'pm+acpi-4.6-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[deliverable/linux.git] / drivers / base / power / opp / core.c
CommitLineData
e1f60b29
NM
1/*
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
d6d2a528
VK
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
d54974c2 16#include <linux/clk.h>
e1f60b29
NM
17#include <linux/errno.h>
18#include <linux/err.h>
e1f60b29 19#include <linux/slab.h>
51990e82 20#include <linux/device.h>
b496dfbc 21#include <linux/of.h>
80126ce7 22#include <linux/export.h>
9f8ea969 23#include <linux/regulator/consumer.h>
e1f60b29 24
f59d3ee8 25#include "opp.h"
e1f60b29
NM
26
27/*
2c2709dc
VK
28 * The root of the list of all opp-tables. All opp_table structures branch off
29 * from here, with each opp_table containing the list of opps it supports in
e1f60b29
NM
30 * various states of availability.
31 */
2c2709dc 32static LIST_HEAD(opp_tables);
e1f60b29 33/* Lock to allow exclusive modification to the device and opp lists */
2c2709dc 34DEFINE_MUTEX(opp_table_lock);
e1f60b29 35
b02ded24
DT
36#define opp_rcu_lockdep_assert() \
37do { \
f78f5b90 38 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
2c2709dc
VK
39 !lockdep_is_held(&opp_table_lock), \
40 "Missing rcu_read_lock() or " \
41 "opp_table_lock protection"); \
b02ded24
DT
42} while (0)
43
2c2709dc
VK
44static struct opp_device *_find_opp_dev(const struct device *dev,
45 struct opp_table *opp_table)
06441658 46{
2c2709dc 47 struct opp_device *opp_dev;
06441658 48
2c2709dc
VK
49 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
50 if (opp_dev->dev == dev)
51 return opp_dev;
06441658
VK
52
53 return NULL;
54}
55
2c2709dc 56static struct opp_table *_managed_opp(const struct device_node *np)
06441658 57{
2c2709dc 58 struct opp_table *opp_table;
06441658 59
2c2709dc
VK
60 list_for_each_entry_rcu(opp_table, &opp_tables, node) {
61 if (opp_table->np == np) {
06441658
VK
62 /*
63 * Multiple devices can point to the same OPP table and
64 * so will have same node-pointer, np.
65 *
66 * But the OPPs will be considered as shared only if the
67 * OPP table contains a "opp-shared" property.
68 */
2c2709dc 69 return opp_table->shared_opp ? opp_table : NULL;
06441658
VK
70 }
71 }
72
73 return NULL;
74}
75
e1f60b29 76/**
2c2709dc
VK
77 * _find_opp_table() - find opp_table struct using device pointer
78 * @dev: device pointer used to lookup OPP table
e1f60b29 79 *
2c2709dc
VK
80 * Search OPP table for one containing matching device. Does a RCU reader
81 * operation to grab the pointer needed.
e1f60b29 82 *
2c2709dc 83 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
e1f60b29
NM
84 * -EINVAL based on type of error.
85 *
0597e818 86 * Locking: For readers, this function must be called under rcu_read_lock().
2c2709dc 87 * opp_table is a RCU protected pointer, which means that opp_table is valid
0597e818
VK
88 * as long as we are under RCU lock.
89 *
2c2709dc 90 * For Writers, this function must be called with opp_table_lock held.
e1f60b29 91 */
2c2709dc 92struct opp_table *_find_opp_table(struct device *dev)
e1f60b29 93{
2c2709dc 94 struct opp_table *opp_table;
e1f60b29 95
0597e818
VK
96 opp_rcu_lockdep_assert();
97
50a3cb04 98 if (IS_ERR_OR_NULL(dev)) {
e1f60b29
NM
99 pr_err("%s: Invalid parameters\n", __func__);
100 return ERR_PTR(-EINVAL);
101 }
102
2c2709dc
VK
103 list_for_each_entry_rcu(opp_table, &opp_tables, node)
104 if (_find_opp_dev(dev, opp_table))
105 return opp_table;
e1f60b29 106
06441658 107 return ERR_PTR(-ENODEV);
e1f60b29
NM
108}
109
110/**
d6d00742 111 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
e1f60b29
NM
112 * @opp: opp for which voltage has to be returned for
113 *
984f16c8 114 * Return: voltage in micro volt corresponding to the opp, else
e1f60b29
NM
115 * return 0
116 *
117 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
118 * protected pointer. This means that opp which could have been fetched by
119 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
120 * under RCU lock. The pointer returned by the opp_find_freq family must be
121 * used in the same section as the usage of this function with the pointer
122 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
123 * pointer.
124 */
47d43ba7 125unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
e1f60b29 126{
47d43ba7 127 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
128 unsigned long v = 0;
129
04bf1c7f
KK
130 opp_rcu_lockdep_assert();
131
e1f60b29 132 tmp_opp = rcu_dereference(opp);
d6d00742 133 if (IS_ERR_OR_NULL(tmp_opp))
e1f60b29
NM
134 pr_err("%s: Invalid parameters\n", __func__);
135 else
136 v = tmp_opp->u_volt;
137
138 return v;
139}
5d4879cd 140EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
e1f60b29
NM
141
142/**
5d4879cd 143 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
e1f60b29
NM
144 * @opp: opp for which frequency has to be returned for
145 *
984f16c8 146 * Return: frequency in hertz corresponding to the opp, else
e1f60b29
NM
147 * return 0
148 *
149 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
150 * protected pointer. This means that opp which could have been fetched by
151 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
152 * under RCU lock. The pointer returned by the opp_find_freq family must be
153 * used in the same section as the usage of this function with the pointer
154 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
155 * pointer.
156 */
47d43ba7 157unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
e1f60b29 158{
47d43ba7 159 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
160 unsigned long f = 0;
161
04bf1c7f
KK
162 opp_rcu_lockdep_assert();
163
e1f60b29 164 tmp_opp = rcu_dereference(opp);
50a3cb04 165 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
e1f60b29
NM
166 pr_err("%s: Invalid parameters\n", __func__);
167 else
168 f = tmp_opp->rate;
169
170 return f;
171}
5d4879cd 172EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
e1f60b29 173
19445b25
BZ
174/**
175 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
176 * @opp: opp for which turbo mode is being verified
177 *
178 * Turbo OPPs are not for normal use, and can be enabled (under certain
179 * conditions) for short duration of times to finish high throughput work
180 * quickly. Running on them for longer times may overheat the chip.
181 *
182 * Return: true if opp is turbo opp, else false.
183 *
184 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
185 * protected pointer. This means that opp which could have been fetched by
186 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
187 * under RCU lock. The pointer returned by the opp_find_freq family must be
188 * used in the same section as the usage of this function with the pointer
189 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
190 * pointer.
191 */
192bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
193{
194 struct dev_pm_opp *tmp_opp;
195
196 opp_rcu_lockdep_assert();
197
198 tmp_opp = rcu_dereference(opp);
199 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
200 pr_err("%s: Invalid parameters\n", __func__);
201 return false;
202 }
203
204 return tmp_opp->turbo;
205}
206EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
207
3ca9bb33
VK
208/**
209 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
210 * @dev: device for which we do this operation
211 *
212 * Return: This function returns the max clock latency in nanoseconds.
213 *
214 * Locking: This function takes rcu_read_lock().
215 */
216unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
217{
2c2709dc 218 struct opp_table *opp_table;
3ca9bb33
VK
219 unsigned long clock_latency_ns;
220
221 rcu_read_lock();
222
2c2709dc
VK
223 opp_table = _find_opp_table(dev);
224 if (IS_ERR(opp_table))
3ca9bb33
VK
225 clock_latency_ns = 0;
226 else
2c2709dc 227 clock_latency_ns = opp_table->clock_latency_ns_max;
3ca9bb33
VK
228
229 rcu_read_unlock();
230 return clock_latency_ns;
231}
232EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
233
655c9df9
VK
234/**
235 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
236 * @dev: device for which we do this operation
237 *
238 * Return: This function returns the max voltage latency in nanoseconds.
239 *
240 * Locking: This function takes rcu_read_lock().
241 */
242unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
243{
2c2709dc 244 struct opp_table *opp_table;
655c9df9
VK
245 struct dev_pm_opp *opp;
246 struct regulator *reg;
247 unsigned long latency_ns = 0;
248 unsigned long min_uV = ~0, max_uV = 0;
249 int ret;
250
251 rcu_read_lock();
252
2c2709dc
VK
253 opp_table = _find_opp_table(dev);
254 if (IS_ERR(opp_table)) {
655c9df9
VK
255 rcu_read_unlock();
256 return 0;
257 }
258
2c2709dc 259 reg = opp_table->regulator;
0c717d0f 260 if (IS_ERR(reg)) {
655c9df9 261 /* Regulator may not be required for device */
655c9df9
VK
262 rcu_read_unlock();
263 return 0;
264 }
265
2c2709dc 266 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
655c9df9
VK
267 if (!opp->available)
268 continue;
269
270 if (opp->u_volt_min < min_uV)
271 min_uV = opp->u_volt_min;
272 if (opp->u_volt_max > max_uV)
273 max_uV = opp->u_volt_max;
274 }
275
276 rcu_read_unlock();
277
278 /*
2c2709dc 279 * The caller needs to ensure that opp_table (and hence the regulator)
655c9df9
VK
280 * isn't freed, while we are executing this routine.
281 */
282 ret = regulator_set_voltage_time(reg, min_uV, max_uV);
283 if (ret > 0)
284 latency_ns = ret * 1000;
285
286 return latency_ns;
287}
288EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
289
21743447
VK
290/**
291 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
292 * nanoseconds
293 * @dev: device for which we do this operation
294 *
295 * Return: This function returns the max transition latency, in nanoseconds, to
296 * switch from one OPP to other.
297 *
298 * Locking: This function takes rcu_read_lock().
299 */
300unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
301{
302 return dev_pm_opp_get_max_volt_latency(dev) +
303 dev_pm_opp_get_max_clock_latency(dev);
304}
305EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
306
4eafbd15
BZ
307/**
308 * dev_pm_opp_get_suspend_opp() - Get suspend opp
309 * @dev: device for which we do this operation
310 *
311 * Return: This function returns pointer to the suspend opp if it is
1b2b90cb 312 * defined and available, otherwise it returns NULL.
4eafbd15
BZ
313 *
314 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
315 * protected pointer. The reason for the same is that the opp pointer which is
316 * returned will remain valid for use with opp_get_{voltage, freq} only while
317 * under the locked area. The pointer returned must be used prior to unlocking
318 * with rcu_read_unlock() to maintain the integrity of the pointer.
319 */
320struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
321{
2c2709dc 322 struct opp_table *opp_table;
4eafbd15
BZ
323
324 opp_rcu_lockdep_assert();
325
2c2709dc
VK
326 opp_table = _find_opp_table(dev);
327 if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
328 !opp_table->suspend_opp->available)
1b2b90cb 329 return NULL;
4eafbd15 330
2c2709dc 331 return opp_table->suspend_opp;
4eafbd15
BZ
332}
333EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
334
e1f60b29 335/**
2c2709dc 336 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
e1f60b29
NM
337 * @dev: device for which we do this operation
338 *
984f16c8 339 * Return: This function returns the number of available opps if there are any,
e1f60b29
NM
340 * else returns 0 if none or the corresponding error value.
341 *
b4718c02 342 * Locking: This function takes rcu_read_lock().
e1f60b29 343 */
5d4879cd 344int dev_pm_opp_get_opp_count(struct device *dev)
e1f60b29 345{
2c2709dc 346 struct opp_table *opp_table;
47d43ba7 347 struct dev_pm_opp *temp_opp;
e1f60b29
NM
348 int count = 0;
349
b4718c02 350 rcu_read_lock();
b02ded24 351
2c2709dc
VK
352 opp_table = _find_opp_table(dev);
353 if (IS_ERR(opp_table)) {
354 count = PTR_ERR(opp_table);
355 dev_err(dev, "%s: OPP table not found (%d)\n",
b4718c02
DT
356 __func__, count);
357 goto out_unlock;
e1f60b29
NM
358 }
359
2c2709dc 360 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
361 if (temp_opp->available)
362 count++;
363 }
364
b4718c02
DT
365out_unlock:
366 rcu_read_unlock();
e1f60b29
NM
367 return count;
368}
5d4879cd 369EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
e1f60b29
NM
370
371/**
5d4879cd 372 * dev_pm_opp_find_freq_exact() - search for an exact frequency
e1f60b29
NM
373 * @dev: device for which we do this operation
374 * @freq: frequency to search for
7ae49618 375 * @available: true/false - match for available opp
e1f60b29 376 *
2c2709dc 377 * Return: Searches for exact match in the opp table and returns pointer to the
984f16c8
NM
378 * matching opp if found, else returns ERR_PTR in case of error and should
379 * be handled using IS_ERR. Error return values can be:
0779726c
NM
380 * EINVAL: for bad pointer
381 * ERANGE: no match found for search
382 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
383 *
384 * Note: available is a modifier for the search. if available=true, then the
385 * match is for exact matching frequency and is available in the stored OPP
386 * table. if false, the match is for exact frequency which is not available.
387 *
388 * This provides a mechanism to enable an opp which is not available currently
389 * or the opposite as well.
390 *
391 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
392 * protected pointer. The reason for the same is that the opp pointer which is
393 * returned will remain valid for use with opp_get_{voltage, freq} only while
394 * under the locked area. The pointer returned must be used prior to unlocking
395 * with rcu_read_unlock() to maintain the integrity of the pointer.
396 */
47d43ba7
NM
397struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
398 unsigned long freq,
399 bool available)
e1f60b29 400{
2c2709dc 401 struct opp_table *opp_table;
47d43ba7 402 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 403
b02ded24
DT
404 opp_rcu_lockdep_assert();
405
2c2709dc
VK
406 opp_table = _find_opp_table(dev);
407 if (IS_ERR(opp_table)) {
408 int r = PTR_ERR(opp_table);
409
410 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
e1f60b29
NM
411 return ERR_PTR(r);
412 }
413
2c2709dc 414 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
415 if (temp_opp->available == available &&
416 temp_opp->rate == freq) {
417 opp = temp_opp;
418 break;
419 }
420 }
421
422 return opp;
423}
5d4879cd 424EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
e1f60b29
NM
425
426/**
5d4879cd 427 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
e1f60b29
NM
428 * @dev: device for which we do this operation
429 * @freq: Start frequency
430 *
431 * Search for the matching ceil *available* OPP from a starting freq
432 * for a device.
433 *
984f16c8 434 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
435 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
436 * values can be:
437 * EINVAL: for bad pointer
438 * ERANGE: no match found for search
439 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
440 *
441 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
442 * protected pointer. The reason for the same is that the opp pointer which is
443 * returned will remain valid for use with opp_get_{voltage, freq} only while
444 * under the locked area. The pointer returned must be used prior to unlocking
445 * with rcu_read_unlock() to maintain the integrity of the pointer.
446 */
47d43ba7
NM
447struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
448 unsigned long *freq)
e1f60b29 449{
2c2709dc 450 struct opp_table *opp_table;
47d43ba7 451 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 452
b02ded24
DT
453 opp_rcu_lockdep_assert();
454
e1f60b29
NM
455 if (!dev || !freq) {
456 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
457 return ERR_PTR(-EINVAL);
458 }
459
2c2709dc
VK
460 opp_table = _find_opp_table(dev);
461 if (IS_ERR(opp_table))
462 return ERR_CAST(opp_table);
e1f60b29 463
2c2709dc 464 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
465 if (temp_opp->available && temp_opp->rate >= *freq) {
466 opp = temp_opp;
467 *freq = opp->rate;
468 break;
469 }
470 }
471
472 return opp;
473}
5d4879cd 474EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
e1f60b29
NM
475
476/**
5d4879cd 477 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
e1f60b29
NM
478 * @dev: device for which we do this operation
479 * @freq: Start frequency
480 *
481 * Search for the matching floor *available* OPP from a starting freq
482 * for a device.
483 *
984f16c8 484 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
485 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
486 * values can be:
487 * EINVAL: for bad pointer
488 * ERANGE: no match found for search
489 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
490 *
491 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
492 * protected pointer. The reason for the same is that the opp pointer which is
493 * returned will remain valid for use with opp_get_{voltage, freq} only while
494 * under the locked area. The pointer returned must be used prior to unlocking
495 * with rcu_read_unlock() to maintain the integrity of the pointer.
496 */
47d43ba7
NM
497struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
498 unsigned long *freq)
e1f60b29 499{
2c2709dc 500 struct opp_table *opp_table;
47d43ba7 501 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 502
b02ded24
DT
503 opp_rcu_lockdep_assert();
504
e1f60b29
NM
505 if (!dev || !freq) {
506 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
507 return ERR_PTR(-EINVAL);
508 }
509
2c2709dc
VK
510 opp_table = _find_opp_table(dev);
511 if (IS_ERR(opp_table))
512 return ERR_CAST(opp_table);
e1f60b29 513
2c2709dc 514 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
515 if (temp_opp->available) {
516 /* go to the next node, before choosing prev */
517 if (temp_opp->rate > *freq)
518 break;
519 else
520 opp = temp_opp;
521 }
522 }
523 if (!IS_ERR(opp))
524 *freq = opp->rate;
525
526 return opp;
527}
5d4879cd 528EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
e1f60b29 529
6a0712f6 530/*
2c2709dc 531 * The caller needs to ensure that opp_table (and hence the clk) isn't freed,
6a0712f6
VK
532 * while clk returned here is used.
533 */
534static struct clk *_get_opp_clk(struct device *dev)
535{
2c2709dc 536 struct opp_table *opp_table;
6a0712f6
VK
537 struct clk *clk;
538
539 rcu_read_lock();
540
2c2709dc
VK
541 opp_table = _find_opp_table(dev);
542 if (IS_ERR(opp_table)) {
6a0712f6 543 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
2c2709dc 544 clk = ERR_CAST(opp_table);
6a0712f6
VK
545 goto unlock;
546 }
547
2c2709dc 548 clk = opp_table->clk;
6a0712f6
VK
549 if (IS_ERR(clk))
550 dev_err(dev, "%s: No clock available for the device\n",
551 __func__);
552
553unlock:
554 rcu_read_unlock();
555 return clk;
556}
557
558static int _set_opp_voltage(struct device *dev, struct regulator *reg,
559 unsigned long u_volt, unsigned long u_volt_min,
560 unsigned long u_volt_max)
561{
562 int ret;
563
564 /* Regulator not available for device */
565 if (IS_ERR(reg)) {
566 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
567 PTR_ERR(reg));
568 return 0;
569 }
570
571 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, u_volt_min,
572 u_volt, u_volt_max);
573
574 ret = regulator_set_voltage_triplet(reg, u_volt_min, u_volt,
575 u_volt_max);
576 if (ret)
577 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
578 __func__, u_volt_min, u_volt, u_volt_max, ret);
579
580 return ret;
581}
582
583/**
584 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
585 * @dev: device for which we do this operation
586 * @target_freq: frequency to achieve
587 *
588 * This configures the power-supplies and clock source to the levels specified
589 * by the OPP corresponding to the target_freq.
590 *
591 * Locking: This function takes rcu_read_lock().
592 */
593int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
594{
2c2709dc 595 struct opp_table *opp_table;
6a0712f6
VK
596 struct dev_pm_opp *old_opp, *opp;
597 struct regulator *reg;
598 struct clk *clk;
599 unsigned long freq, old_freq;
600 unsigned long u_volt, u_volt_min, u_volt_max;
601 unsigned long ou_volt, ou_volt_min, ou_volt_max;
602 int ret;
603
604 if (unlikely(!target_freq)) {
605 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
606 target_freq);
607 return -EINVAL;
608 }
609
610 clk = _get_opp_clk(dev);
611 if (IS_ERR(clk))
612 return PTR_ERR(clk);
613
614 freq = clk_round_rate(clk, target_freq);
615 if ((long)freq <= 0)
616 freq = target_freq;
617
618 old_freq = clk_get_rate(clk);
619
620 /* Return early if nothing to do */
621 if (old_freq == freq) {
622 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
623 __func__, freq);
624 return 0;
625 }
626
627 rcu_read_lock();
628
2c2709dc
VK
629 opp_table = _find_opp_table(dev);
630 if (IS_ERR(opp_table)) {
6a0712f6
VK
631 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
632 rcu_read_unlock();
2c2709dc 633 return PTR_ERR(opp_table);
6a0712f6
VK
634 }
635
636 old_opp = dev_pm_opp_find_freq_ceil(dev, &old_freq);
637 if (!IS_ERR(old_opp)) {
638 ou_volt = old_opp->u_volt;
639 ou_volt_min = old_opp->u_volt_min;
640 ou_volt_max = old_opp->u_volt_max;
641 } else {
642 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
643 __func__, old_freq, PTR_ERR(old_opp));
644 }
645
646 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
647 if (IS_ERR(opp)) {
648 ret = PTR_ERR(opp);
649 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
650 __func__, freq, ret);
651 rcu_read_unlock();
652 return ret;
653 }
654
655 u_volt = opp->u_volt;
656 u_volt_min = opp->u_volt_min;
657 u_volt_max = opp->u_volt_max;
658
2c2709dc 659 reg = opp_table->regulator;
6a0712f6
VK
660
661 rcu_read_unlock();
662
663 /* Scaling up? Scale voltage before frequency */
664 if (freq > old_freq) {
665 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
666 u_volt_max);
667 if (ret)
668 goto restore_voltage;
669 }
670
671 /* Change frequency */
672
673 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n",
674 __func__, old_freq, freq);
675
676 ret = clk_set_rate(clk, freq);
677 if (ret) {
678 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
679 ret);
680 goto restore_voltage;
681 }
682
683 /* Scaling down? Scale voltage after frequency */
684 if (freq < old_freq) {
685 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
686 u_volt_max);
687 if (ret)
688 goto restore_freq;
689 }
690
691 return 0;
692
693restore_freq:
694 if (clk_set_rate(clk, old_freq))
695 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
696 __func__, old_freq);
697restore_voltage:
698 /* This shouldn't harm even if the voltages weren't updated earlier */
699 if (!IS_ERR(old_opp))
700 _set_opp_voltage(dev, reg, ou_volt, ou_volt_min, ou_volt_max);
701
702 return ret;
703}
704EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
705
2c2709dc
VK
706/* OPP-dev Helpers */
707static void _kfree_opp_dev_rcu(struct rcu_head *head)
06441658 708{
2c2709dc 709 struct opp_device *opp_dev;
06441658 710
2c2709dc
VK
711 opp_dev = container_of(head, struct opp_device, rcu_head);
712 kfree_rcu(opp_dev, rcu_head);
06441658
VK
713}
714
2c2709dc
VK
715static void _remove_opp_dev(struct opp_device *opp_dev,
716 struct opp_table *opp_table)
06441658 717{
2c2709dc
VK
718 opp_debug_unregister(opp_dev, opp_table);
719 list_del(&opp_dev->node);
720 call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
721 _kfree_opp_dev_rcu);
06441658
VK
722}
723
2c2709dc
VK
724struct opp_device *_add_opp_dev(const struct device *dev,
725 struct opp_table *opp_table)
06441658 726{
2c2709dc 727 struct opp_device *opp_dev;
deaa5146 728 int ret;
06441658 729
2c2709dc
VK
730 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
731 if (!opp_dev)
06441658
VK
732 return NULL;
733
2c2709dc
VK
734 /* Initialize opp-dev */
735 opp_dev->dev = dev;
736 list_add_rcu(&opp_dev->node, &opp_table->dev_list);
06441658 737
2c2709dc
VK
738 /* Create debugfs entries for the opp_table */
739 ret = opp_debug_register(opp_dev, opp_table);
deaa5146
VK
740 if (ret)
741 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
742 __func__, ret);
743
2c2709dc 744 return opp_dev;
06441658
VK
745}
746
984f16c8 747/**
2c2709dc 748 * _add_opp_table() - Find OPP table or allocate a new one
984f16c8
NM
749 * @dev: device for which we do this operation
750 *
aa5f2f85
VK
751 * It tries to find an existing table first, if it couldn't find one, it
752 * allocates a new OPP table and returns that.
984f16c8 753 *
2c2709dc 754 * Return: valid opp_table pointer if success, else NULL.
984f16c8 755 */
2c2709dc 756static struct opp_table *_add_opp_table(struct device *dev)
07cce74a 757{
2c2709dc
VK
758 struct opp_table *opp_table;
759 struct opp_device *opp_dev;
50f8cfbd 760 struct device_node *np;
d54974c2 761 int ret;
07cce74a 762
2c2709dc
VK
763 /* Check for existing table for 'dev' first */
764 opp_table = _find_opp_table(dev);
765 if (!IS_ERR(opp_table))
766 return opp_table;
07cce74a
VK
767
768 /*
2c2709dc 769 * Allocate a new OPP table. In the infrequent case where a new
07cce74a
VK
770 * device is needed to be added, we pay this penalty.
771 */
2c2709dc
VK
772 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
773 if (!opp_table)
07cce74a
VK
774 return NULL;
775
2c2709dc 776 INIT_LIST_HEAD(&opp_table->dev_list);
06441658 777
2c2709dc
VK
778 opp_dev = _add_opp_dev(dev, opp_table);
779 if (!opp_dev) {
780 kfree(opp_table);
06441658
VK
781 return NULL;
782 }
783
50f8cfbd
VK
784 /*
785 * Only required for backward compatibility with v1 bindings, but isn't
786 * harmful for other cases. And so we do it unconditionally.
787 */
788 np = of_node_get(dev->of_node);
789 if (np) {
790 u32 val;
791
792 if (!of_property_read_u32(np, "clock-latency", &val))
2c2709dc 793 opp_table->clock_latency_ns_max = val;
50f8cfbd 794 of_property_read_u32(np, "voltage-tolerance",
2c2709dc 795 &opp_table->voltage_tolerance_v1);
50f8cfbd
VK
796 of_node_put(np);
797 }
798
0c717d0f 799 /* Set regulator to a non-NULL error value */
2c2709dc 800 opp_table->regulator = ERR_PTR(-ENXIO);
0c717d0f 801
d54974c2 802 /* Find clk for the device */
2c2709dc
VK
803 opp_table->clk = clk_get(dev, NULL);
804 if (IS_ERR(opp_table->clk)) {
805 ret = PTR_ERR(opp_table->clk);
d54974c2
VK
806 if (ret != -EPROBE_DEFER)
807 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
808 ret);
809 }
810
2c2709dc
VK
811 srcu_init_notifier_head(&opp_table->srcu_head);
812 INIT_LIST_HEAD(&opp_table->opp_list);
07cce74a 813
2c2709dc
VK
814 /* Secure the device table modification */
815 list_add_rcu(&opp_table->node, &opp_tables);
816 return opp_table;
07cce74a
VK
817}
818
984f16c8 819/**
2c2709dc 820 * _kfree_device_rcu() - Free opp_table RCU handler
737002b5 821 * @head: RCU head
984f16c8 822 */
737002b5 823static void _kfree_device_rcu(struct rcu_head *head)
e1f60b29 824{
2c2709dc
VK
825 struct opp_table *opp_table = container_of(head, struct opp_table,
826 rcu_head);
6ce4184d 827
2c2709dc 828 kfree_rcu(opp_table, rcu_head);
e1f60b29 829}
38393409
VK
830
831/**
2c2709dc
VK
832 * _remove_opp_table() - Removes a OPP table
833 * @opp_table: OPP table to be removed.
38393409 834 *
2c2709dc 835 * Removes/frees OPP table if it doesn't contain any OPPs.
38393409 836 */
2c2709dc 837static void _remove_opp_table(struct opp_table *opp_table)
38393409 838{
2c2709dc 839 struct opp_device *opp_dev;
06441658 840
2c2709dc 841 if (!list_empty(&opp_table->opp_list))
3bac42ca
VK
842 return;
843
2c2709dc 844 if (opp_table->supported_hw)
7de36b0a
VK
845 return;
846
2c2709dc 847 if (opp_table->prop_name)
01fb4d3c
VK
848 return;
849
2c2709dc 850 if (!IS_ERR(opp_table->regulator))
9f8ea969
VK
851 return;
852
d54974c2 853 /* Release clk */
2c2709dc
VK
854 if (!IS_ERR(opp_table->clk))
855 clk_put(opp_table->clk);
d54974c2 856
2c2709dc
VK
857 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
858 node);
06441658 859
2c2709dc 860 _remove_opp_dev(opp_dev, opp_table);
06441658
VK
861
862 /* dev_list must be empty now */
2c2709dc 863 WARN_ON(!list_empty(&opp_table->dev_list));
06441658 864
2c2709dc
VK
865 list_del_rcu(&opp_table->node);
866 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
3bac42ca 867 _kfree_device_rcu);
38393409 868}
e1f60b29 869
984f16c8
NM
870/**
871 * _kfree_opp_rcu() - Free OPP RCU handler
872 * @head: RCU head
873 */
327854c8 874static void _kfree_opp_rcu(struct rcu_head *head)
129eec55
VK
875{
876 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
877
878 kfree_rcu(opp, rcu_head);
879}
880
984f16c8
NM
881/**
882 * _opp_remove() - Remove an OPP from a table definition
2c2709dc 883 * @opp_table: points back to the opp_table struct this opp belongs to
984f16c8 884 * @opp: pointer to the OPP to remove
23dacf6d 885 * @notify: OPP_EVENT_REMOVE notification should be sent or not
984f16c8 886 *
2c2709dc 887 * This function removes an opp definition from the opp table.
984f16c8 888 *
2c2709dc 889 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
890 * It is assumed that the caller holds required mutex for an RCU updater
891 * strategy.
892 */
2c2709dc 893static void _opp_remove(struct opp_table *opp_table,
23dacf6d 894 struct dev_pm_opp *opp, bool notify)
129eec55
VK
895{
896 /*
897 * Notify the changes in the availability of the operable
898 * frequency/voltage list.
899 */
23dacf6d 900 if (notify)
2c2709dc
VK
901 srcu_notifier_call_chain(&opp_table->srcu_head,
902 OPP_EVENT_REMOVE, opp);
deaa5146 903 opp_debug_remove_one(opp);
129eec55 904 list_del_rcu(&opp->node);
2c2709dc 905 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
129eec55 906
2c2709dc 907 _remove_opp_table(opp_table);
129eec55
VK
908}
909
910/**
2c2709dc 911 * dev_pm_opp_remove() - Remove an OPP from OPP table
129eec55
VK
912 * @dev: device for which we do this operation
913 * @freq: OPP to remove with matching 'freq'
914 *
2c2709dc 915 * This function removes an opp from the opp table.
984f16c8 916 *
2c2709dc 917 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
918 * Hence this function internally uses RCU updater strategy with mutex locks
919 * to keep the integrity of the internal data structures. Callers should ensure
920 * that this function is *NOT* called under RCU protection or in contexts where
921 * mutex cannot be locked.
129eec55
VK
922 */
923void dev_pm_opp_remove(struct device *dev, unsigned long freq)
924{
925 struct dev_pm_opp *opp;
2c2709dc 926 struct opp_table *opp_table;
129eec55
VK
927 bool found = false;
928
2c2709dc
VK
929 /* Hold our table modification lock here */
930 mutex_lock(&opp_table_lock);
129eec55 931
2c2709dc
VK
932 opp_table = _find_opp_table(dev);
933 if (IS_ERR(opp_table))
129eec55
VK
934 goto unlock;
935
2c2709dc 936 list_for_each_entry(opp, &opp_table->opp_list, node) {
129eec55
VK
937 if (opp->rate == freq) {
938 found = true;
939 break;
940 }
941 }
942
943 if (!found) {
944 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
945 __func__, freq);
946 goto unlock;
947 }
948
2c2709dc 949 _opp_remove(opp_table, opp, true);
129eec55 950unlock:
2c2709dc 951 mutex_unlock(&opp_table_lock);
129eec55
VK
952}
953EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
954
23dacf6d 955static struct dev_pm_opp *_allocate_opp(struct device *dev,
2c2709dc 956 struct opp_table **opp_table)
e1f60b29 957{
23dacf6d 958 struct dev_pm_opp *opp;
e1f60b29 959
23dacf6d
VK
960 /* allocate new OPP node */
961 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
962 if (!opp)
963 return NULL;
e1f60b29 964
23dacf6d 965 INIT_LIST_HEAD(&opp->node);
e1f60b29 966
2c2709dc
VK
967 *opp_table = _add_opp_table(dev);
968 if (!*opp_table) {
23dacf6d
VK
969 kfree(opp);
970 return NULL;
971 }
972
973 return opp;
974}
975
7d34d56e 976static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
2c2709dc 977 struct opp_table *opp_table)
7d34d56e 978{
2c2709dc 979 struct regulator *reg = opp_table->regulator;
7d34d56e 980
0c717d0f 981 if (!IS_ERR(reg) &&
7d34d56e
VK
982 !regulator_is_supported_voltage(reg, opp->u_volt_min,
983 opp->u_volt_max)) {
984 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
985 __func__, opp->u_volt_min, opp->u_volt_max);
986 return false;
987 }
988
989 return true;
990}
991
06441658 992static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
2c2709dc 993 struct opp_table *opp_table)
23dacf6d
VK
994{
995 struct dev_pm_opp *opp;
2c2709dc 996 struct list_head *head = &opp_table->opp_list;
deaa5146 997 int ret;
23dacf6d
VK
998
999 /*
1000 * Insert new OPP in order of increasing frequency and discard if
1001 * already present.
1002 *
2c2709dc 1003 * Need to use &opp_table->opp_list in the condition part of the 'for'
23dacf6d
VK
1004 * loop, don't replace it with head otherwise it will become an infinite
1005 * loop.
1006 */
2c2709dc 1007 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
23dacf6d
VK
1008 if (new_opp->rate > opp->rate) {
1009 head = &opp->node;
1010 continue;
1011 }
1012
1013 if (new_opp->rate < opp->rate)
1014 break;
1015
1016 /* Duplicate OPPs */
06441658 1017 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
23dacf6d
VK
1018 __func__, opp->rate, opp->u_volt, opp->available,
1019 new_opp->rate, new_opp->u_volt, new_opp->available);
1020
1021 return opp->available && new_opp->u_volt == opp->u_volt ?
1022 0 : -EEXIST;
1023 }
1024
2c2709dc 1025 new_opp->opp_table = opp_table;
23dacf6d
VK
1026 list_add_rcu(&new_opp->node, head);
1027
2c2709dc 1028 ret = opp_debug_create_one(new_opp, opp_table);
deaa5146
VK
1029 if (ret)
1030 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1031 __func__, ret);
1032
2c2709dc 1033 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
7d34d56e
VK
1034 new_opp->available = false;
1035 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1036 __func__, new_opp->rate);
1037 }
1038
23dacf6d
VK
1039 return 0;
1040}
1041
984f16c8 1042/**
b64b9c3f 1043 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
984f16c8
NM
1044 * @dev: device for which we do this operation
1045 * @freq: Frequency in Hz for this OPP
1046 * @u_volt: Voltage in uVolts for this OPP
1047 * @dynamic: Dynamically added OPPs.
1048 *
2c2709dc 1049 * This function adds an opp definition to the opp table and returns status.
984f16c8
NM
1050 * The opp is made available by default and it can be controlled using
1051 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1052 *
8f8d37b2
VK
1053 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1054 * and freed by dev_pm_opp_of_remove_table.
984f16c8 1055 *
2c2709dc 1056 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1057 * Hence this function internally uses RCU updater strategy with mutex locks
1058 * to keep the integrity of the internal data structures. Callers should ensure
1059 * that this function is *NOT* called under RCU protection or in contexts where
1060 * mutex cannot be locked.
1061 *
1062 * Return:
1063 * 0 On success OR
1064 * Duplicate OPPs (both freq and volt are same) and opp->available
1065 * -EEXIST Freq are same and volt are different OR
1066 * Duplicate OPPs (both freq and volt are same) and !opp->available
1067 * -ENOMEM Memory allocation failure
1068 */
b64b9c3f
VK
1069static int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
1070 bool dynamic)
e1f60b29 1071{
2c2709dc 1072 struct opp_table *opp_table;
23dacf6d 1073 struct dev_pm_opp *new_opp;
50f8cfbd 1074 unsigned long tol;
6ce4184d 1075 int ret;
e1f60b29 1076
2c2709dc
VK
1077 /* Hold our table modification lock here */
1078 mutex_lock(&opp_table_lock);
e1f60b29 1079
2c2709dc 1080 new_opp = _allocate_opp(dev, &opp_table);
23dacf6d
VK
1081 if (!new_opp) {
1082 ret = -ENOMEM;
1083 goto unlock;
1084 }
1085
a7470db6 1086 /* populate the opp table */
a7470db6 1087 new_opp->rate = freq;
2c2709dc 1088 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
a7470db6 1089 new_opp->u_volt = u_volt;
50f8cfbd
VK
1090 new_opp->u_volt_min = u_volt - tol;
1091 new_opp->u_volt_max = u_volt + tol;
a7470db6 1092 new_opp->available = true;
23dacf6d 1093 new_opp->dynamic = dynamic;
a7470db6 1094
2c2709dc 1095 ret = _opp_add(dev, new_opp, opp_table);
23dacf6d 1096 if (ret)
6ce4184d 1097 goto free_opp;
64ce8545 1098
2c2709dc 1099 mutex_unlock(&opp_table_lock);
e1f60b29 1100
03ca370f
MH
1101 /*
1102 * Notify the changes in the availability of the operable
1103 * frequency/voltage list.
1104 */
2c2709dc 1105 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29 1106 return 0;
6ce4184d
VK
1107
1108free_opp:
2c2709dc 1109 _opp_remove(opp_table, new_opp, false);
23dacf6d 1110unlock:
2c2709dc 1111 mutex_unlock(&opp_table_lock);
6ce4184d 1112 return ret;
e1f60b29 1113}
38393409 1114
27465902 1115/* TODO: Support multiple regulators */
01fb4d3c 1116static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
2c2709dc 1117 struct opp_table *opp_table)
27465902
VK
1118{
1119 u32 microvolt[3] = {0};
ad623c31 1120 u32 val;
27465902 1121 int count, ret;
01fb4d3c
VK
1122 struct property *prop = NULL;
1123 char name[NAME_MAX];
1124
1125 /* Search for "opp-microvolt-<name>" */
2c2709dc 1126 if (opp_table->prop_name) {
5ff24d60 1127 snprintf(name, sizeof(name), "opp-microvolt-%s",
2c2709dc 1128 opp_table->prop_name);
01fb4d3c
VK
1129 prop = of_find_property(opp->np, name, NULL);
1130 }
1131
1132 if (!prop) {
1133 /* Search for "opp-microvolt" */
fd8d8e63 1134 sprintf(name, "opp-microvolt");
01fb4d3c 1135 prop = of_find_property(opp->np, name, NULL);
27465902 1136
01fb4d3c
VK
1137 /* Missing property isn't a problem, but an invalid entry is */
1138 if (!prop)
1139 return 0;
1140 }
27465902 1141
01fb4d3c 1142 count = of_property_count_u32_elems(opp->np, name);
680168a5 1143 if (count < 0) {
01fb4d3c
VK
1144 dev_err(dev, "%s: Invalid %s property (%d)\n",
1145 __func__, name, count);
680168a5
VK
1146 return count;
1147 }
1148
27465902
VK
1149 /* There can be one or three elements here */
1150 if (count != 1 && count != 3) {
01fb4d3c
VK
1151 dev_err(dev, "%s: Invalid number of elements in %s property (%d)\n",
1152 __func__, name, count);
27465902
VK
1153 return -EINVAL;
1154 }
1155
01fb4d3c 1156 ret = of_property_read_u32_array(opp->np, name, microvolt, count);
27465902 1157 if (ret) {
01fb4d3c 1158 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
27465902
VK
1159 return -EINVAL;
1160 }
1161
1162 opp->u_volt = microvolt[0];
c88c395f
VK
1163
1164 if (count == 1) {
1165 opp->u_volt_min = opp->u_volt;
1166 opp->u_volt_max = opp->u_volt;
1167 } else {
1168 opp->u_volt_min = microvolt[1];
1169 opp->u_volt_max = microvolt[2];
1170 }
27465902 1171
01fb4d3c
VK
1172 /* Search for "opp-microamp-<name>" */
1173 prop = NULL;
2c2709dc 1174 if (opp_table->prop_name) {
5ff24d60 1175 snprintf(name, sizeof(name), "opp-microamp-%s",
2c2709dc 1176 opp_table->prop_name);
01fb4d3c
VK
1177 prop = of_find_property(opp->np, name, NULL);
1178 }
1179
1180 if (!prop) {
1181 /* Search for "opp-microamp" */
fd8d8e63 1182 sprintf(name, "opp-microamp");
01fb4d3c
VK
1183 prop = of_find_property(opp->np, name, NULL);
1184 }
1185
1186 if (prop && !of_property_read_u32(opp->np, name, &val))
ad623c31
VK
1187 opp->u_amp = val;
1188
27465902
VK
1189 return 0;
1190}
1191
7de36b0a
VK
1192/**
1193 * dev_pm_opp_set_supported_hw() - Set supported platforms
1194 * @dev: Device for which supported-hw has to be set.
1195 * @versions: Array of hierarchy of versions to match.
1196 * @count: Number of elements in the array.
1197 *
1198 * This is required only for the V2 bindings, and it enables a platform to
1199 * specify the hierarchy of versions it supports. OPP layer will then enable
1200 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1201 * property.
1202 *
2c2709dc 1203 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1204 * Hence this function internally uses RCU updater strategy with mutex locks
1205 * to keep the integrity of the internal data structures. Callers should ensure
1206 * that this function is *NOT* called under RCU protection or in contexts where
1207 * mutex cannot be locked.
1208 */
1209int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1210 unsigned int count)
1211{
2c2709dc 1212 struct opp_table *opp_table;
7de36b0a
VK
1213 int ret = 0;
1214
2c2709dc
VK
1215 /* Hold our table modification lock here */
1216 mutex_lock(&opp_table_lock);
7de36b0a 1217
2c2709dc
VK
1218 opp_table = _add_opp_table(dev);
1219 if (!opp_table) {
7de36b0a
VK
1220 ret = -ENOMEM;
1221 goto unlock;
1222 }
1223
2c2709dc
VK
1224 /* Make sure there are no concurrent readers while updating opp_table */
1225 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1226
2c2709dc
VK
1227 /* Do we already have a version hierarchy associated with opp_table? */
1228 if (opp_table->supported_hw) {
7de36b0a
VK
1229 dev_err(dev, "%s: Already have supported hardware list\n",
1230 __func__);
1231 ret = -EBUSY;
1232 goto err;
1233 }
1234
2c2709dc 1235 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
7de36b0a 1236 GFP_KERNEL);
2c2709dc 1237 if (!opp_table->supported_hw) {
7de36b0a
VK
1238 ret = -ENOMEM;
1239 goto err;
1240 }
1241
2c2709dc
VK
1242 opp_table->supported_hw_count = count;
1243 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1244 return 0;
1245
1246err:
2c2709dc 1247 _remove_opp_table(opp_table);
7de36b0a 1248unlock:
2c2709dc 1249 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1250
1251 return ret;
1252}
1253EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1254
1255/**
1256 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
a5da6447 1257 * @dev: Device for which supported-hw has to be put.
7de36b0a
VK
1258 *
1259 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1260 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
7de36b0a
VK
1261 * will not be freed.
1262 *
2c2709dc 1263 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1264 * Hence this function internally uses RCU updater strategy with mutex locks
1265 * to keep the integrity of the internal data structures. Callers should ensure
1266 * that this function is *NOT* called under RCU protection or in contexts where
1267 * mutex cannot be locked.
1268 */
1269void dev_pm_opp_put_supported_hw(struct device *dev)
1270{
2c2709dc 1271 struct opp_table *opp_table;
7de36b0a 1272
2c2709dc
VK
1273 /* Hold our table modification lock here */
1274 mutex_lock(&opp_table_lock);
7de36b0a 1275
2c2709dc
VK
1276 /* Check for existing table for 'dev' first */
1277 opp_table = _find_opp_table(dev);
1278 if (IS_ERR(opp_table)) {
1279 dev_err(dev, "Failed to find opp_table: %ld\n",
1280 PTR_ERR(opp_table));
7de36b0a
VK
1281 goto unlock;
1282 }
1283
2c2709dc
VK
1284 /* Make sure there are no concurrent readers while updating opp_table */
1285 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1286
2c2709dc 1287 if (!opp_table->supported_hw) {
7de36b0a
VK
1288 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1289 __func__);
1290 goto unlock;
1291 }
1292
2c2709dc
VK
1293 kfree(opp_table->supported_hw);
1294 opp_table->supported_hw = NULL;
1295 opp_table->supported_hw_count = 0;
7de36b0a 1296
2c2709dc
VK
1297 /* Try freeing opp_table if this was the last blocking resource */
1298 _remove_opp_table(opp_table);
7de36b0a
VK
1299
1300unlock:
2c2709dc 1301 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1302}
1303EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1304
01fb4d3c
VK
1305/**
1306 * dev_pm_opp_set_prop_name() - Set prop-extn name
a5da6447 1307 * @dev: Device for which the prop-name has to be set.
01fb4d3c
VK
1308 * @name: name to postfix to properties.
1309 *
1310 * This is required only for the V2 bindings, and it enables a platform to
1311 * specify the extn to be used for certain property names. The properties to
1312 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1313 * should postfix the property name with -<name> while looking for them.
1314 *
2c2709dc 1315 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1316 * Hence this function internally uses RCU updater strategy with mutex locks
1317 * to keep the integrity of the internal data structures. Callers should ensure
1318 * that this function is *NOT* called under RCU protection or in contexts where
1319 * mutex cannot be locked.
1320 */
1321int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1322{
2c2709dc 1323 struct opp_table *opp_table;
01fb4d3c
VK
1324 int ret = 0;
1325
2c2709dc
VK
1326 /* Hold our table modification lock here */
1327 mutex_lock(&opp_table_lock);
01fb4d3c 1328
2c2709dc
VK
1329 opp_table = _add_opp_table(dev);
1330 if (!opp_table) {
01fb4d3c
VK
1331 ret = -ENOMEM;
1332 goto unlock;
1333 }
1334
2c2709dc
VK
1335 /* Make sure there are no concurrent readers while updating opp_table */
1336 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1337
2c2709dc
VK
1338 /* Do we already have a prop-name associated with opp_table? */
1339 if (opp_table->prop_name) {
01fb4d3c 1340 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
2c2709dc 1341 opp_table->prop_name);
01fb4d3c
VK
1342 ret = -EBUSY;
1343 goto err;
1344 }
1345
2c2709dc
VK
1346 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1347 if (!opp_table->prop_name) {
01fb4d3c
VK
1348 ret = -ENOMEM;
1349 goto err;
1350 }
1351
2c2709dc 1352 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1353 return 0;
1354
1355err:
2c2709dc 1356 _remove_opp_table(opp_table);
01fb4d3c 1357unlock:
2c2709dc 1358 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1359
1360 return ret;
1361}
1362EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1363
1364/**
1365 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
a5da6447 1366 * @dev: Device for which the prop-name has to be put.
01fb4d3c
VK
1367 *
1368 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1369 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
01fb4d3c
VK
1370 * will not be freed.
1371 *
2c2709dc 1372 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1373 * Hence this function internally uses RCU updater strategy with mutex locks
1374 * to keep the integrity of the internal data structures. Callers should ensure
1375 * that this function is *NOT* called under RCU protection or in contexts where
1376 * mutex cannot be locked.
1377 */
1378void dev_pm_opp_put_prop_name(struct device *dev)
1379{
2c2709dc 1380 struct opp_table *opp_table;
01fb4d3c 1381
2c2709dc
VK
1382 /* Hold our table modification lock here */
1383 mutex_lock(&opp_table_lock);
01fb4d3c 1384
2c2709dc
VK
1385 /* Check for existing table for 'dev' first */
1386 opp_table = _find_opp_table(dev);
1387 if (IS_ERR(opp_table)) {
1388 dev_err(dev, "Failed to find opp_table: %ld\n",
1389 PTR_ERR(opp_table));
01fb4d3c
VK
1390 goto unlock;
1391 }
1392
2c2709dc
VK
1393 /* Make sure there are no concurrent readers while updating opp_table */
1394 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1395
2c2709dc 1396 if (!opp_table->prop_name) {
01fb4d3c
VK
1397 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1398 goto unlock;
1399 }
1400
2c2709dc
VK
1401 kfree(opp_table->prop_name);
1402 opp_table->prop_name = NULL;
01fb4d3c 1403
2c2709dc
VK
1404 /* Try freeing opp_table if this was the last blocking resource */
1405 _remove_opp_table(opp_table);
01fb4d3c
VK
1406
1407unlock:
2c2709dc 1408 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1409}
1410EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1411
9f8ea969
VK
1412/**
1413 * dev_pm_opp_set_regulator() - Set regulator name for the device
1414 * @dev: Device for which regulator name is being set.
1415 * @name: Name of the regulator.
1416 *
1417 * In order to support OPP switching, OPP layer needs to know the name of the
1418 * device's regulator, as the core would be required to switch voltages as well.
1419 *
1420 * This must be called before any OPPs are initialized for the device.
1421 *
2c2709dc 1422 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1423 * Hence this function internally uses RCU updater strategy with mutex locks
1424 * to keep the integrity of the internal data structures. Callers should ensure
1425 * that this function is *NOT* called under RCU protection or in contexts where
1426 * mutex cannot be locked.
1427 */
1428int dev_pm_opp_set_regulator(struct device *dev, const char *name)
1429{
2c2709dc 1430 struct opp_table *opp_table;
9f8ea969
VK
1431 struct regulator *reg;
1432 int ret;
1433
2c2709dc 1434 mutex_lock(&opp_table_lock);
9f8ea969 1435
2c2709dc
VK
1436 opp_table = _add_opp_table(dev);
1437 if (!opp_table) {
9f8ea969
VK
1438 ret = -ENOMEM;
1439 goto unlock;
1440 }
1441
1442 /* This should be called before OPPs are initialized */
2c2709dc 1443 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
9f8ea969
VK
1444 ret = -EBUSY;
1445 goto err;
1446 }
1447
1448 /* Already have a regulator set */
2c2709dc 1449 if (WARN_ON(!IS_ERR(opp_table->regulator))) {
9f8ea969
VK
1450 ret = -EBUSY;
1451 goto err;
1452 }
1453 /* Allocate the regulator */
1454 reg = regulator_get_optional(dev, name);
1455 if (IS_ERR(reg)) {
1456 ret = PTR_ERR(reg);
1457 if (ret != -EPROBE_DEFER)
1458 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1459 __func__, name, ret);
1460 goto err;
1461 }
1462
2c2709dc 1463 opp_table->regulator = reg;
9f8ea969 1464
2c2709dc 1465 mutex_unlock(&opp_table_lock);
9f8ea969
VK
1466 return 0;
1467
1468err:
2c2709dc 1469 _remove_opp_table(opp_table);
9f8ea969 1470unlock:
2c2709dc 1471 mutex_unlock(&opp_table_lock);
9f8ea969
VK
1472
1473 return ret;
1474}
1475EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulator);
1476
1477/**
1478 * dev_pm_opp_put_regulator() - Releases resources blocked for regulator
1479 * @dev: Device for which regulator was set.
1480 *
2c2709dc 1481 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1482 * Hence this function internally uses RCU updater strategy with mutex locks
1483 * to keep the integrity of the internal data structures. Callers should ensure
1484 * that this function is *NOT* called under RCU protection or in contexts where
1485 * mutex cannot be locked.
1486 */
1487void dev_pm_opp_put_regulator(struct device *dev)
1488{
2c2709dc 1489 struct opp_table *opp_table;
9f8ea969 1490
2c2709dc 1491 mutex_lock(&opp_table_lock);
9f8ea969 1492
2c2709dc
VK
1493 /* Check for existing table for 'dev' first */
1494 opp_table = _find_opp_table(dev);
1495 if (IS_ERR(opp_table)) {
1496 dev_err(dev, "Failed to find opp_table: %ld\n",
1497 PTR_ERR(opp_table));
9f8ea969
VK
1498 goto unlock;
1499 }
1500
2c2709dc 1501 if (IS_ERR(opp_table->regulator)) {
9f8ea969
VK
1502 dev_err(dev, "%s: Doesn't have regulator set\n", __func__);
1503 goto unlock;
1504 }
1505
2c2709dc
VK
1506 /* Make sure there are no concurrent readers while updating opp_table */
1507 WARN_ON(!list_empty(&opp_table->opp_list));
9f8ea969 1508
2c2709dc
VK
1509 regulator_put(opp_table->regulator);
1510 opp_table->regulator = ERR_PTR(-ENXIO);
9f8ea969 1511
2c2709dc
VK
1512 /* Try freeing opp_table if this was the last blocking resource */
1513 _remove_opp_table(opp_table);
9f8ea969
VK
1514
1515unlock:
2c2709dc 1516 mutex_unlock(&opp_table_lock);
9f8ea969
VK
1517}
1518EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulator);
1519
2c2709dc 1520static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
7de36b0a
VK
1521 struct device_node *np)
1522{
2c2709dc 1523 unsigned int count = opp_table->supported_hw_count;
7de36b0a
VK
1524 u32 version;
1525 int ret;
1526
2c2709dc 1527 if (!opp_table->supported_hw)
7de36b0a
VK
1528 return true;
1529
1530 while (count--) {
1531 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
1532 &version);
1533 if (ret) {
1534 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
1535 __func__, count, ret);
1536 return false;
1537 }
1538
1539 /* Both of these are bitwise masks of the versions */
2c2709dc 1540 if (!(version & opp_table->supported_hw[count]))
7de36b0a
VK
1541 return false;
1542 }
1543
1544 return true;
1545}
1546
27465902
VK
1547/**
1548 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
1549 * @dev: device for which we do this operation
1550 * @np: device node
1551 *
2c2709dc 1552 * This function adds an opp definition to the opp table and returns status. The
27465902
VK
1553 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
1554 * removed by dev_pm_opp_remove.
1555 *
2c2709dc 1556 * Locking: The internal opp_table and opp structures are RCU protected.
27465902
VK
1557 * Hence this function internally uses RCU updater strategy with mutex locks
1558 * to keep the integrity of the internal data structures. Callers should ensure
1559 * that this function is *NOT* called under RCU protection or in contexts where
1560 * mutex cannot be locked.
1561 *
1562 * Return:
1563 * 0 On success OR
1564 * Duplicate OPPs (both freq and volt are same) and opp->available
1565 * -EEXIST Freq are same and volt are different OR
1566 * Duplicate OPPs (both freq and volt are same) and !opp->available
1567 * -ENOMEM Memory allocation failure
1568 * -EINVAL Failed parsing the OPP node
1569 */
1570static int _opp_add_static_v2(struct device *dev, struct device_node *np)
1571{
2c2709dc 1572 struct opp_table *opp_table;
27465902
VK
1573 struct dev_pm_opp *new_opp;
1574 u64 rate;
68fa9f0a 1575 u32 val;
27465902
VK
1576 int ret;
1577
2c2709dc
VK
1578 /* Hold our table modification lock here */
1579 mutex_lock(&opp_table_lock);
27465902 1580
2c2709dc 1581 new_opp = _allocate_opp(dev, &opp_table);
27465902
VK
1582 if (!new_opp) {
1583 ret = -ENOMEM;
1584 goto unlock;
1585 }
1586
1587 ret = of_property_read_u64(np, "opp-hz", &rate);
1588 if (ret < 0) {
1589 dev_err(dev, "%s: opp-hz not found\n", __func__);
1590 goto free_opp;
1591 }
1592
7de36b0a 1593 /* Check if the OPP supports hardware's hierarchy of versions or not */
2c2709dc 1594 if (!_opp_is_supported(dev, opp_table, np)) {
7de36b0a
VK
1595 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
1596 goto free_opp;
1597 }
1598
27465902
VK
1599 /*
1600 * Rate is defined as an unsigned long in clk API, and so casting
1601 * explicitly to its type. Must be fixed once rate is 64 bit
1602 * guaranteed in clk API.
1603 */
1604 new_opp->rate = (unsigned long)rate;
1605 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
1606
1607 new_opp->np = np;
1608 new_opp->dynamic = false;
1609 new_opp->available = true;
68fa9f0a
VK
1610
1611 if (!of_property_read_u32(np, "clock-latency-ns", &val))
1612 new_opp->clock_latency_ns = val;
27465902 1613
2c2709dc 1614 ret = opp_parse_supplies(new_opp, dev, opp_table);
27465902
VK
1615 if (ret)
1616 goto free_opp;
1617
2c2709dc 1618 ret = _opp_add(dev, new_opp, opp_table);
27465902
VK
1619 if (ret)
1620 goto free_opp;
1621
ad656a6a
VK
1622 /* OPP to select on device suspend */
1623 if (of_property_read_bool(np, "opp-suspend")) {
2c2709dc 1624 if (opp_table->suspend_opp) {
ad656a6a 1625 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
2c2709dc 1626 __func__, opp_table->suspend_opp->rate,
ad656a6a 1627 new_opp->rate);
deaa5146
VK
1628 } else {
1629 new_opp->suspend = true;
2c2709dc 1630 opp_table->suspend_opp = new_opp;
deaa5146 1631 }
ad656a6a
VK
1632 }
1633
2c2709dc
VK
1634 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
1635 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
3ca9bb33 1636
2c2709dc 1637 mutex_unlock(&opp_table_lock);
27465902 1638
3ca9bb33 1639 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
27465902 1640 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
3ca9bb33
VK
1641 new_opp->u_volt_min, new_opp->u_volt_max,
1642 new_opp->clock_latency_ns);
27465902
VK
1643
1644 /*
1645 * Notify the changes in the availability of the operable
1646 * frequency/voltage list.
1647 */
2c2709dc 1648 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
27465902
VK
1649 return 0;
1650
1651free_opp:
2c2709dc 1652 _opp_remove(opp_table, new_opp, false);
27465902 1653unlock:
2c2709dc 1654 mutex_unlock(&opp_table_lock);
27465902
VK
1655 return ret;
1656}
1657
38393409
VK
1658/**
1659 * dev_pm_opp_add() - Add an OPP table from a table definitions
1660 * @dev: device for which we do this operation
1661 * @freq: Frequency in Hz for this OPP
1662 * @u_volt: Voltage in uVolts for this OPP
1663 *
2c2709dc 1664 * This function adds an opp definition to the opp table and returns status.
38393409
VK
1665 * The opp is made available by default and it can be controlled using
1666 * dev_pm_opp_enable/disable functions.
1667 *
2c2709dc 1668 * Locking: The internal opp_table and opp structures are RCU protected.
38393409
VK
1669 * Hence this function internally uses RCU updater strategy with mutex locks
1670 * to keep the integrity of the internal data structures. Callers should ensure
1671 * that this function is *NOT* called under RCU protection or in contexts where
1672 * mutex cannot be locked.
1673 *
1674 * Return:
984f16c8 1675 * 0 On success OR
38393409 1676 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 1677 * -EEXIST Freq are same and volt are different OR
38393409 1678 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 1679 * -ENOMEM Memory allocation failure
38393409
VK
1680 */
1681int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1682{
b64b9c3f 1683 return _opp_add_v1(dev, freq, u_volt, true);
38393409 1684}
5d4879cd 1685EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
1686
1687/**
327854c8 1688 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
1689 * @dev: device for which we do this operation
1690 * @freq: OPP frequency to modify availability
1691 * @availability_req: availability status requested for this opp
1692 *
1693 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1694 * share a common logic which is isolated here.
1695 *
984f16c8 1696 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1697 * copy operation, returns 0 if no modification was done OR modification was
e1f60b29
NM
1698 * successful.
1699 *
2c2709dc 1700 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1701 * Hence this function internally uses RCU updater strategy with mutex locks to
1702 * keep the integrity of the internal data structures. Callers should ensure
1703 * that this function is *NOT* called under RCU protection or in contexts where
1704 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1705 */
327854c8
NM
1706static int _opp_set_availability(struct device *dev, unsigned long freq,
1707 bool availability_req)
e1f60b29 1708{
2c2709dc 1709 struct opp_table *opp_table;
47d43ba7 1710 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
1711 int r = 0;
1712
1713 /* keep the node allocated */
47d43ba7 1714 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
59d84ca8 1715 if (!new_opp)
e1f60b29 1716 return -ENOMEM;
e1f60b29 1717
2c2709dc 1718 mutex_lock(&opp_table_lock);
e1f60b29 1719
2c2709dc
VK
1720 /* Find the opp_table */
1721 opp_table = _find_opp_table(dev);
1722 if (IS_ERR(opp_table)) {
1723 r = PTR_ERR(opp_table);
e1f60b29
NM
1724 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1725 goto unlock;
1726 }
1727
1728 /* Do we have the frequency? */
2c2709dc 1729 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
1730 if (tmp_opp->rate == freq) {
1731 opp = tmp_opp;
1732 break;
1733 }
1734 }
1735 if (IS_ERR(opp)) {
1736 r = PTR_ERR(opp);
1737 goto unlock;
1738 }
1739
1740 /* Is update really needed? */
1741 if (opp->available == availability_req)
1742 goto unlock;
1743 /* copy the old data over */
1744 *new_opp = *opp;
1745
1746 /* plug in new node */
1747 new_opp->available = availability_req;
1748
1749 list_replace_rcu(&opp->node, &new_opp->node);
2c2709dc
VK
1750 mutex_unlock(&opp_table_lock);
1751 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
e1f60b29 1752
03ca370f
MH
1753 /* Notify the change of the OPP availability */
1754 if (availability_req)
2c2709dc
VK
1755 srcu_notifier_call_chain(&opp_table->srcu_head,
1756 OPP_EVENT_ENABLE, new_opp);
03ca370f 1757 else
2c2709dc
VK
1758 srcu_notifier_call_chain(&opp_table->srcu_head,
1759 OPP_EVENT_DISABLE, new_opp);
03ca370f 1760
dde8437d 1761 return 0;
e1f60b29
NM
1762
1763unlock:
2c2709dc 1764 mutex_unlock(&opp_table_lock);
e1f60b29
NM
1765 kfree(new_opp);
1766 return r;
1767}
1768
1769/**
5d4879cd 1770 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
1771 * @dev: device for which we do this operation
1772 * @freq: OPP frequency to enable
1773 *
1774 * Enables a provided opp. If the operation is valid, this returns 0, else the
1775 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 1776 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29 1777 *
2c2709dc 1778 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1779 * Hence this function indirectly uses RCU and mutex locks to keep the
1780 * integrity of the internal data structures. Callers should ensure that
1781 * this function is *NOT* called under RCU protection or in contexts where
1782 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1783 *
1784 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1785 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1786 * successful.
e1f60b29 1787 */
5d4879cd 1788int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 1789{
327854c8 1790 return _opp_set_availability(dev, freq, true);
e1f60b29 1791}
5d4879cd 1792EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
1793
1794/**
5d4879cd 1795 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
1796 * @dev: device for which we do this operation
1797 * @freq: OPP frequency to disable
1798 *
1799 * Disables a provided opp. If the operation is valid, this returns
1800 * 0, else the corresponding error value. It is meant to be a temporary
1801 * control by users to make this OPP not available until the circumstances are
5d4879cd 1802 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29 1803 *
2c2709dc 1804 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1805 * Hence this function indirectly uses RCU and mutex locks to keep the
1806 * integrity of the internal data structures. Callers should ensure that
1807 * this function is *NOT* called under RCU protection or in contexts where
1808 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1809 *
1810 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1811 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1812 * successful.
e1f60b29 1813 */
5d4879cd 1814int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 1815{
327854c8 1816 return _opp_set_availability(dev, freq, false);
e1f60b29 1817}
5d4879cd 1818EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 1819
03ca370f 1820/**
5d4879cd 1821 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
2c2709dc 1822 * @dev: device pointer used to lookup OPP table.
984f16c8
NM
1823 *
1824 * Return: pointer to notifier head if found, otherwise -ENODEV or
1825 * -EINVAL based on type of error casted as pointer. value must be checked
1826 * with IS_ERR to determine valid pointer or error result.
1827 *
2c2709dc
VK
1828 * Locking: This function must be called under rcu_read_lock(). opp_table is a
1829 * RCU protected pointer. The reason for the same is that the opp pointer which
1830 * is returned will remain valid for use with opp_get_{voltage, freq} only while
984f16c8
NM
1831 * under the locked area. The pointer returned must be used prior to unlocking
1832 * with rcu_read_unlock() to maintain the integrity of the pointer.
03ca370f 1833 */
5d4879cd 1834struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
03ca370f 1835{
2c2709dc 1836 struct opp_table *opp_table = _find_opp_table(dev);
03ca370f 1837
2c2709dc
VK
1838 if (IS_ERR(opp_table))
1839 return ERR_CAST(opp_table); /* matching type */
03ca370f 1840
2c2709dc 1841 return &opp_table->srcu_head;
03ca370f 1842}
4679ec37 1843EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
b496dfbc
SG
1844
1845#ifdef CONFIG_OF
1846/**
8f8d37b2
VK
1847 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
1848 * entries
2c2709dc 1849 * @dev: device pointer used to lookup OPP table.
b496dfbc 1850 *
737002b5 1851 * Free OPPs created using static entries present in DT.
984f16c8 1852 *
2c2709dc 1853 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1854 * Hence this function indirectly uses RCU updater strategy with mutex locks
1855 * to keep the integrity of the internal data structures. Callers should ensure
1856 * that this function is *NOT* called under RCU protection or in contexts where
1857 * mutex cannot be locked.
b496dfbc 1858 */
8f8d37b2 1859void dev_pm_opp_of_remove_table(struct device *dev)
737002b5 1860{
2c2709dc 1861 struct opp_table *opp_table;
737002b5
VK
1862 struct dev_pm_opp *opp, *tmp;
1863
2c2709dc
VK
1864 /* Hold our table modification lock here */
1865 mutex_lock(&opp_table_lock);
06441658 1866
2c2709dc
VK
1867 /* Check for existing table for 'dev' */
1868 opp_table = _find_opp_table(dev);
1869 if (IS_ERR(opp_table)) {
1870 int error = PTR_ERR(opp_table);
737002b5
VK
1871
1872 if (error != -ENODEV)
2c2709dc 1873 WARN(1, "%s: opp_table: %d\n",
737002b5
VK
1874 IS_ERR_OR_NULL(dev) ?
1875 "Invalid device" : dev_name(dev),
1876 error);
06441658 1877 goto unlock;
737002b5
VK
1878 }
1879
2c2709dc
VK
1880 /* Find if opp_table manages a single device */
1881 if (list_is_singular(&opp_table->dev_list)) {
06441658 1882 /* Free static OPPs */
2c2709dc 1883 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
06441658 1884 if (!opp->dynamic)
2c2709dc 1885 _opp_remove(opp_table, opp, true);
06441658
VK
1886 }
1887 } else {
2c2709dc 1888 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
737002b5
VK
1889 }
1890
06441658 1891unlock:
2c2709dc 1892 mutex_unlock(&opp_table_lock);
737002b5 1893}
8f8d37b2 1894EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
737002b5 1895
1840995c 1896/* Returns opp descriptor node for a device, caller must do of_node_put() */
f59d3ee8 1897struct device_node *_of_get_opp_desc_node(struct device *dev)
8d4d4e98 1898{
8d4d4e98
VK
1899 /*
1900 * TODO: Support for multiple OPP tables.
1901 *
1902 * There should be only ONE phandle present in "operating-points-v2"
1903 * property.
1904 */
8d4d4e98 1905
1840995c 1906 return of_parse_phandle(dev->of_node, "operating-points-v2", 0);
8d4d4e98
VK
1907}
1908
27465902 1909/* Initializes OPP tables based on new bindings */
f0489a5e 1910static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
27465902 1911{
1840995c 1912 struct device_node *np;
2c2709dc 1913 struct opp_table *opp_table;
27465902
VK
1914 int ret = 0, count = 0;
1915
2c2709dc 1916 mutex_lock(&opp_table_lock);
4a3a1353 1917
2c2709dc
VK
1918 opp_table = _managed_opp(opp_np);
1919 if (opp_table) {
06441658 1920 /* OPPs are already managed */
2c2709dc 1921 if (!_add_opp_dev(dev, opp_table))
06441658 1922 ret = -ENOMEM;
2c2709dc 1923 mutex_unlock(&opp_table_lock);
1840995c 1924 return ret;
06441658 1925 }
2c2709dc 1926 mutex_unlock(&opp_table_lock);
06441658 1927
2c2709dc 1928 /* We have opp-table node now, iterate over it and add OPPs */
27465902
VK
1929 for_each_available_child_of_node(opp_np, np) {
1930 count++;
1931
1932 ret = _opp_add_static_v2(dev, np);
1933 if (ret) {
1934 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1935 ret);
1f821ed7 1936 goto free_table;
27465902
VK
1937 }
1938 }
1939
1940 /* There should be one of more OPP defined */
1840995c
VK
1941 if (WARN_ON(!count))
1942 return -ENOENT;
27465902 1943
2c2709dc 1944 mutex_lock(&opp_table_lock);
4a3a1353 1945
2c2709dc
VK
1946 opp_table = _find_opp_table(dev);
1947 if (WARN_ON(IS_ERR(opp_table))) {
1948 ret = PTR_ERR(opp_table);
1949 mutex_unlock(&opp_table_lock);
1f821ed7 1950 goto free_table;
06441658 1951 }
27465902 1952
2c2709dc
VK
1953 opp_table->np = opp_np;
1954 opp_table->shared_opp = of_property_read_bool(opp_np, "opp-shared");
1f821ed7 1955
2c2709dc 1956 mutex_unlock(&opp_table_lock);
4a3a1353 1957
1f821ed7
VK
1958 return 0;
1959
1960free_table:
8f8d37b2 1961 dev_pm_opp_of_remove_table(dev);
27465902
VK
1962
1963 return ret;
1964}
1965
1966/* Initializes OPP tables based on old-deprecated bindings */
f0489a5e 1967static int _of_add_opp_table_v1(struct device *dev)
b496dfbc
SG
1968{
1969 const struct property *prop;
1970 const __be32 *val;
1971 int nr;
1972
1973 prop = of_find_property(dev->of_node, "operating-points", NULL);
1974 if (!prop)
1975 return -ENODEV;
1976 if (!prop->value)
1977 return -ENODATA;
1978
1979 /*
1980 * Each OPP is a set of tuples consisting of frequency and
1981 * voltage like <freq-kHz vol-uV>.
1982 */
1983 nr = prop->length / sizeof(u32);
1984 if (nr % 2) {
2c2709dc 1985 dev_err(dev, "%s: Invalid OPP table\n", __func__);
b496dfbc
SG
1986 return -EINVAL;
1987 }
1988
1989 val = prop->value;
1990 while (nr) {
1991 unsigned long freq = be32_to_cpup(val++) * 1000;
1992 unsigned long volt = be32_to_cpup(val++);
1993
b64b9c3f 1994 if (_opp_add_v1(dev, freq, volt, false))
b496dfbc
SG
1995 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1996 __func__, freq);
b496dfbc
SG
1997 nr -= 2;
1998 }
1999
2000 return 0;
2001}
129eec55
VK
2002
2003/**
8f8d37b2 2004 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
2c2709dc 2005 * @dev: device pointer used to lookup OPP table.
129eec55 2006 *
27465902 2007 * Register the initial OPP table with the OPP library for given device.
984f16c8 2008 *
2c2709dc 2009 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
2010 * Hence this function indirectly uses RCU updater strategy with mutex locks
2011 * to keep the integrity of the internal data structures. Callers should ensure
2012 * that this function is *NOT* called under RCU protection or in contexts where
2013 * mutex cannot be locked.
27465902
VK
2014 *
2015 * Return:
2016 * 0 On success OR
2017 * Duplicate OPPs (both freq and volt are same) and opp->available
2018 * -EEXIST Freq are same and volt are different OR
2019 * Duplicate OPPs (both freq and volt are same) and !opp->available
2020 * -ENOMEM Memory allocation failure
2021 * -ENODEV when 'operating-points' property is not found or is invalid data
2022 * in device node.
2023 * -ENODATA when empty 'operating-points' property is found
2024 * -EINVAL when invalid entries are found in opp-v2 table
129eec55 2025 */
8f8d37b2 2026int dev_pm_opp_of_add_table(struct device *dev)
129eec55 2027{
1840995c
VK
2028 struct device_node *opp_np;
2029 int ret;
27465902
VK
2030
2031 /*
2032 * OPPs have two version of bindings now. The older one is deprecated,
2033 * try for the new binding first.
2034 */
1840995c
VK
2035 opp_np = _of_get_opp_desc_node(dev);
2036 if (!opp_np) {
27465902
VK
2037 /*
2038 * Try old-deprecated bindings for backward compatibility with
2039 * older dtbs.
2040 */
f0489a5e 2041 return _of_add_opp_table_v1(dev);
8d4d4e98
VK
2042 }
2043
f0489a5e 2044 ret = _of_add_opp_table_v2(dev, opp_np);
1840995c 2045 of_node_put(opp_np);
8d4d4e98 2046
8d4d4e98
VK
2047 return ret;
2048}
8f8d37b2 2049EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
b496dfbc 2050#endif
This page took 0.448727 seconds and 5 git commands to generate.