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