]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/base/power/opp/core.c
PM / OPP: Add 'struct kref' to OPP table
[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
b6160e26 822static struct opp_table *_allocate_opp_table(struct device *dev)
07cce74a 823{
2c2709dc
VK
824 struct opp_table *opp_table;
825 struct opp_device *opp_dev;
d54974c2 826 int ret;
07cce74a
VK
827
828 /*
2c2709dc 829 * Allocate a new OPP table. In the infrequent case where a new
07cce74a
VK
830 * device is needed to be added, we pay this penalty.
831 */
2c2709dc
VK
832 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
833 if (!opp_table)
07cce74a
VK
834 return NULL;
835
2c2709dc 836 INIT_LIST_HEAD(&opp_table->dev_list);
06441658 837
2c2709dc
VK
838 opp_dev = _add_opp_dev(dev, opp_table);
839 if (!opp_dev) {
840 kfree(opp_table);
06441658
VK
841 return NULL;
842 }
843
f47b72a1 844 _of_init_opp_table(opp_table, dev);
50f8cfbd 845
d54974c2 846 /* Find clk for the device */
2c2709dc
VK
847 opp_table->clk = clk_get(dev, NULL);
848 if (IS_ERR(opp_table->clk)) {
849 ret = PTR_ERR(opp_table->clk);
d54974c2
VK
850 if (ret != -EPROBE_DEFER)
851 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
852 ret);
853 }
854
2c2709dc
VK
855 srcu_init_notifier_head(&opp_table->srcu_head);
856 INIT_LIST_HEAD(&opp_table->opp_list);
37a73ec0 857 mutex_init(&opp_table->lock);
f067a982 858 kref_init(&opp_table->kref);
07cce74a 859
2c2709dc
VK
860 /* Secure the device table modification */
861 list_add_rcu(&opp_table->node, &opp_tables);
862 return opp_table;
07cce74a
VK
863}
864
b6160e26
VK
865/**
866 * _add_opp_table() - Find OPP table or allocate a new one
867 * @dev: device for which we do this operation
868 *
869 * It tries to find an existing table first, if it couldn't find one, it
870 * allocates a new OPP table and returns that.
871 *
872 * Return: valid opp_table pointer if success, else NULL.
873 */
874struct opp_table *_add_opp_table(struct device *dev)
875{
876 struct opp_table *opp_table;
877
878 /* Check for existing table for 'dev' first */
879 opp_table = _find_opp_table(dev);
880 if (!IS_ERR(opp_table))
881 return opp_table;
882
883 return _allocate_opp_table(dev);
884}
885
984f16c8 886/**
2c2709dc 887 * _kfree_device_rcu() - Free opp_table RCU handler
737002b5 888 * @head: RCU head
984f16c8 889 */
737002b5 890static void _kfree_device_rcu(struct rcu_head *head)
e1f60b29 891{
2c2709dc
VK
892 struct opp_table *opp_table = container_of(head, struct opp_table,
893 rcu_head);
6ce4184d 894
2c2709dc 895 kfree_rcu(opp_table, rcu_head);
e1f60b29 896}
38393409 897
f067a982 898void _get_opp_table_kref(struct opp_table *opp_table)
b6160e26 899{
f067a982
VK
900 kref_get(&opp_table->kref);
901}
902
903struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
904{
905 struct opp_table *opp_table;
906
907 /* Hold our table modification lock here */
908 mutex_lock(&opp_table_lock);
909
910 opp_table = _find_opp_table(dev);
911 if (!IS_ERR(opp_table)) {
912 _get_opp_table_kref(opp_table);
913 goto unlock;
914 }
915
916 opp_table = _allocate_opp_table(dev);
917
918unlock:
919 mutex_unlock(&opp_table_lock);
920
921 return opp_table;
922}
923EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
924
925static void _opp_table_kref_release_unlocked(struct kref *kref)
926{
927 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
b6160e26
VK
928 struct opp_device *opp_dev;
929
930 /* Release clk */
931 if (!IS_ERR(opp_table->clk))
932 clk_put(opp_table->clk);
933
934 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
935 node);
936
937 _remove_opp_dev(opp_dev, opp_table);
938
939 /* dev_list must be empty now */
940 WARN_ON(!list_empty(&opp_table->dev_list));
941
37a73ec0 942 mutex_destroy(&opp_table->lock);
b6160e26
VK
943 list_del_rcu(&opp_table->node);
944 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
945 _kfree_device_rcu);
946}
947
f067a982
VK
948static void dev_pm_opp_put_opp_table_unlocked(struct opp_table *opp_table)
949{
950 kref_put(&opp_table->kref, _opp_table_kref_release_unlocked);
951}
952
953static void _opp_table_kref_release(struct kref *kref)
954{
955 _opp_table_kref_release_unlocked(kref);
956 mutex_unlock(&opp_table_lock);
957}
958
959void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
960{
961 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
962 &opp_table_lock);
963}
964EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
965
38393409 966/**
2c2709dc
VK
967 * _remove_opp_table() - Removes a OPP table
968 * @opp_table: OPP table to be removed.
38393409 969 *
2c2709dc 970 * Removes/frees OPP table if it doesn't contain any OPPs.
38393409 971 */
2c2709dc 972static void _remove_opp_table(struct opp_table *opp_table)
38393409 973{
2c2709dc 974 if (!list_empty(&opp_table->opp_list))
3bac42ca
VK
975 return;
976
2c2709dc 977 if (opp_table->supported_hw)
7de36b0a
VK
978 return;
979
2c2709dc 980 if (opp_table->prop_name)
01fb4d3c
VK
981 return;
982
dfbe4678 983 if (opp_table->regulators)
9f8ea969
VK
984 return;
985
4dab160e
VK
986 if (opp_table->set_opp)
987 return;
988
f067a982 989 dev_pm_opp_put_opp_table_unlocked(opp_table);
38393409 990}
e1f60b29 991
8cd2f6e8 992void _opp_free(struct dev_pm_opp *opp)
969fceb3
VK
993{
994 kfree(opp);
969fceb3
VK
995}
996
984f16c8
NM
997/**
998 * _kfree_opp_rcu() - Free OPP RCU handler
999 * @head: RCU head
1000 */
327854c8 1001static void _kfree_opp_rcu(struct rcu_head *head)
129eec55
VK
1002{
1003 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
1004
1005 kfree_rcu(opp, rcu_head);
1006}
1007
984f16c8
NM
1008/**
1009 * _opp_remove() - Remove an OPP from a table definition
2c2709dc 1010 * @opp_table: points back to the opp_table struct this opp belongs to
984f16c8
NM
1011 * @opp: pointer to the OPP to remove
1012 *
2c2709dc 1013 * This function removes an opp definition from the opp table.
984f16c8 1014 *
2c2709dc 1015 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1016 * It is assumed that the caller holds required mutex for an RCU updater
1017 * strategy.
1018 */
969fceb3 1019static void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp)
129eec55 1020{
37a73ec0
VK
1021 mutex_lock(&opp_table->lock);
1022
129eec55
VK
1023 /*
1024 * Notify the changes in the availability of the operable
1025 * frequency/voltage list.
1026 */
969fceb3 1027 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_REMOVE, opp);
deaa5146 1028 opp_debug_remove_one(opp);
129eec55 1029 list_del_rcu(&opp->node);
2c2709dc 1030 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
129eec55 1031
37a73ec0
VK
1032 mutex_unlock(&opp_table->lock);
1033
2c2709dc 1034 _remove_opp_table(opp_table);
129eec55
VK
1035}
1036
1037/**
2c2709dc 1038 * dev_pm_opp_remove() - Remove an OPP from OPP table
129eec55
VK
1039 * @dev: device for which we do this operation
1040 * @freq: OPP to remove with matching 'freq'
1041 *
2c2709dc 1042 * This function removes an opp from the opp table.
984f16c8 1043 *
2c2709dc 1044 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1045 * Hence this function internally uses RCU updater strategy with mutex locks
1046 * to keep the integrity of the internal data structures. Callers should ensure
1047 * that this function is *NOT* called under RCU protection or in contexts where
1048 * mutex cannot be locked.
129eec55
VK
1049 */
1050void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1051{
1052 struct dev_pm_opp *opp;
2c2709dc 1053 struct opp_table *opp_table;
129eec55
VK
1054 bool found = false;
1055
2c2709dc
VK
1056 /* Hold our table modification lock here */
1057 mutex_lock(&opp_table_lock);
129eec55 1058
2c2709dc
VK
1059 opp_table = _find_opp_table(dev);
1060 if (IS_ERR(opp_table))
129eec55
VK
1061 goto unlock;
1062
37a73ec0
VK
1063 mutex_lock(&opp_table->lock);
1064
2c2709dc 1065 list_for_each_entry(opp, &opp_table->opp_list, node) {
129eec55
VK
1066 if (opp->rate == freq) {
1067 found = true;
1068 break;
1069 }
1070 }
1071
37a73ec0
VK
1072 mutex_unlock(&opp_table->lock);
1073
129eec55
VK
1074 if (!found) {
1075 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1076 __func__, freq);
1077 goto unlock;
1078 }
1079
969fceb3 1080 _opp_remove(opp_table, opp);
129eec55 1081unlock:
2c2709dc 1082 mutex_unlock(&opp_table_lock);
129eec55
VK
1083}
1084EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1085
8cd2f6e8 1086struct dev_pm_opp *_opp_allocate(struct opp_table *table)
e1f60b29 1087{
23dacf6d 1088 struct dev_pm_opp *opp;
dfbe4678 1089 int count, supply_size;
e1f60b29 1090
dfbe4678
VK
1091 /* Allocate space for at least one supply */
1092 count = table->regulator_count ? table->regulator_count : 1;
1093 supply_size = sizeof(*opp->supplies) * count;
e1f60b29 1094
dfbe4678
VK
1095 /* allocate new OPP node and supplies structures */
1096 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
8cd2f6e8 1097 if (!opp)
23dacf6d 1098 return NULL;
23dacf6d 1099
dfbe4678
VK
1100 /* Put the supplies at the end of the OPP structure as an empty array */
1101 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1102 INIT_LIST_HEAD(&opp->node);
1103
23dacf6d
VK
1104 return opp;
1105}
1106
7d34d56e 1107static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
2c2709dc 1108 struct opp_table *opp_table)
7d34d56e 1109{
dfbe4678
VK
1110 struct regulator *reg;
1111 int i;
1112
1113 for (i = 0; i < opp_table->regulator_count; i++) {
1114 reg = opp_table->regulators[i];
1115
1116 if (!regulator_is_supported_voltage(reg,
1117 opp->supplies[i].u_volt_min,
1118 opp->supplies[i].u_volt_max)) {
1119 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1120 __func__, opp->supplies[i].u_volt_min,
1121 opp->supplies[i].u_volt_max);
1122 return false;
1123 }
7d34d56e
VK
1124 }
1125
1126 return true;
1127}
1128
7f8538eb
VK
1129/*
1130 * Returns:
1131 * 0: On success. And appropriate error message for duplicate OPPs.
1132 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1133 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1134 * sure we don't print error messages unnecessarily if different parts of
1135 * kernel try to initialize the OPP table.
1136 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1137 * should be considered an error by the callers of _opp_add().
1138 */
f47b72a1
VK
1139int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1140 struct opp_table *opp_table)
23dacf6d
VK
1141{
1142 struct dev_pm_opp *opp;
37a73ec0 1143 struct list_head *head;
deaa5146 1144 int ret;
23dacf6d
VK
1145
1146 /*
1147 * Insert new OPP in order of increasing frequency and discard if
1148 * already present.
1149 *
2c2709dc 1150 * Need to use &opp_table->opp_list in the condition part of the 'for'
23dacf6d
VK
1151 * loop, don't replace it with head otherwise it will become an infinite
1152 * loop.
1153 */
37a73ec0
VK
1154 mutex_lock(&opp_table->lock);
1155 head = &opp_table->opp_list;
1156
2c2709dc 1157 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
23dacf6d
VK
1158 if (new_opp->rate > opp->rate) {
1159 head = &opp->node;
1160 continue;
1161 }
1162
1163 if (new_opp->rate < opp->rate)
1164 break;
1165
1166 /* Duplicate OPPs */
06441658 1167 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
dfbe4678
VK
1168 __func__, opp->rate, opp->supplies[0].u_volt,
1169 opp->available, new_opp->rate,
1170 new_opp->supplies[0].u_volt, new_opp->available);
23dacf6d 1171
dfbe4678 1172 /* Should we compare voltages for all regulators here ? */
37a73ec0
VK
1173 ret = opp->available &&
1174 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1175
1176 mutex_unlock(&opp_table->lock);
1177 return ret;
23dacf6d
VK
1178 }
1179
23dacf6d 1180 list_add_rcu(&new_opp->node, head);
37a73ec0
VK
1181 mutex_unlock(&opp_table->lock);
1182
1183 new_opp->opp_table = opp_table;
23dacf6d 1184
2c2709dc 1185 ret = opp_debug_create_one(new_opp, opp_table);
deaa5146
VK
1186 if (ret)
1187 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1188 __func__, ret);
1189
2c2709dc 1190 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
7d34d56e
VK
1191 new_opp->available = false;
1192 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1193 __func__, new_opp->rate);
1194 }
1195
23dacf6d
VK
1196 return 0;
1197}
1198
984f16c8 1199/**
b64b9c3f 1200 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
8cd2f6e8 1201 * @opp_table: OPP table
984f16c8
NM
1202 * @dev: device for which we do this operation
1203 * @freq: Frequency in Hz for this OPP
1204 * @u_volt: Voltage in uVolts for this OPP
1205 * @dynamic: Dynamically added OPPs.
1206 *
2c2709dc 1207 * This function adds an opp definition to the opp table and returns status.
984f16c8
NM
1208 * The opp is made available by default and it can be controlled using
1209 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1210 *
8f8d37b2
VK
1211 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1212 * and freed by dev_pm_opp_of_remove_table.
984f16c8 1213 *
2c2709dc 1214 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1215 * Hence this function internally uses RCU updater strategy with mutex locks
1216 * to keep the integrity of the internal data structures. Callers should ensure
1217 * that this function is *NOT* called under RCU protection or in contexts where
1218 * mutex cannot be locked.
1219 *
1220 * Return:
1221 * 0 On success OR
1222 * Duplicate OPPs (both freq and volt are same) and opp->available
1223 * -EEXIST Freq are same and volt are different OR
1224 * Duplicate OPPs (both freq and volt are same) and !opp->available
1225 * -ENOMEM Memory allocation failure
1226 */
8cd2f6e8
VK
1227int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1228 unsigned long freq, long u_volt, bool dynamic)
e1f60b29 1229{
23dacf6d 1230 struct dev_pm_opp *new_opp;
50f8cfbd 1231 unsigned long tol;
6ce4184d 1232 int ret;
e1f60b29 1233
8cd2f6e8 1234 opp_rcu_lockdep_assert();
e1f60b29 1235
8cd2f6e8
VK
1236 new_opp = _opp_allocate(opp_table);
1237 if (!new_opp)
1238 return -ENOMEM;
23dacf6d 1239
a7470db6 1240 /* populate the opp table */
a7470db6 1241 new_opp->rate = freq;
2c2709dc 1242 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
dfbe4678
VK
1243 new_opp->supplies[0].u_volt = u_volt;
1244 new_opp->supplies[0].u_volt_min = u_volt - tol;
1245 new_opp->supplies[0].u_volt_max = u_volt + tol;
a7470db6 1246 new_opp->available = true;
23dacf6d 1247 new_opp->dynamic = dynamic;
a7470db6 1248
2c2709dc 1249 ret = _opp_add(dev, new_opp, opp_table);
7f8538eb
VK
1250 if (ret) {
1251 /* Don't return error for duplicate OPPs */
1252 if (ret == -EBUSY)
1253 ret = 0;
6ce4184d 1254 goto free_opp;
7f8538eb 1255 }
64ce8545 1256
03ca370f
MH
1257 /*
1258 * Notify the changes in the availability of the operable
1259 * frequency/voltage list.
1260 */
2c2709dc 1261 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29 1262 return 0;
6ce4184d
VK
1263
1264free_opp:
8cd2f6e8
VK
1265 _opp_free(new_opp);
1266
6ce4184d 1267 return ret;
e1f60b29 1268}
38393409 1269
7de36b0a
VK
1270/**
1271 * dev_pm_opp_set_supported_hw() - Set supported platforms
1272 * @dev: Device for which supported-hw has to be set.
1273 * @versions: Array of hierarchy of versions to match.
1274 * @count: Number of elements in the array.
1275 *
1276 * This is required only for the V2 bindings, and it enables a platform to
1277 * specify the hierarchy of versions it supports. OPP layer will then enable
1278 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1279 * property.
1280 *
2c2709dc 1281 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1282 * Hence this function internally uses RCU updater strategy with mutex locks
1283 * to keep the integrity of the internal data structures. Callers should ensure
1284 * that this function is *NOT* called under RCU protection or in contexts where
1285 * mutex cannot be locked.
1286 */
1287int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1288 unsigned int count)
1289{
2c2709dc 1290 struct opp_table *opp_table;
7de36b0a
VK
1291 int ret = 0;
1292
2c2709dc
VK
1293 /* Hold our table modification lock here */
1294 mutex_lock(&opp_table_lock);
7de36b0a 1295
2c2709dc
VK
1296 opp_table = _add_opp_table(dev);
1297 if (!opp_table) {
7de36b0a
VK
1298 ret = -ENOMEM;
1299 goto unlock;
1300 }
1301
2c2709dc
VK
1302 /* Make sure there are no concurrent readers while updating opp_table */
1303 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1304
2c2709dc
VK
1305 /* Do we already have a version hierarchy associated with opp_table? */
1306 if (opp_table->supported_hw) {
7de36b0a
VK
1307 dev_err(dev, "%s: Already have supported hardware list\n",
1308 __func__);
1309 ret = -EBUSY;
1310 goto err;
1311 }
1312
2c2709dc 1313 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
7de36b0a 1314 GFP_KERNEL);
2c2709dc 1315 if (!opp_table->supported_hw) {
7de36b0a
VK
1316 ret = -ENOMEM;
1317 goto err;
1318 }
1319
2c2709dc
VK
1320 opp_table->supported_hw_count = count;
1321 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1322 return 0;
1323
1324err:
2c2709dc 1325 _remove_opp_table(opp_table);
7de36b0a 1326unlock:
2c2709dc 1327 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1328
1329 return ret;
1330}
1331EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1332
1333/**
1334 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
a5da6447 1335 * @dev: Device for which supported-hw has to be put.
7de36b0a
VK
1336 *
1337 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1338 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
7de36b0a
VK
1339 * will not be freed.
1340 *
2c2709dc 1341 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1342 * Hence this function internally uses RCU updater strategy with mutex locks
1343 * to keep the integrity of the internal data structures. Callers should ensure
1344 * that this function is *NOT* called under RCU protection or in contexts where
1345 * mutex cannot be locked.
1346 */
1347void dev_pm_opp_put_supported_hw(struct device *dev)
1348{
2c2709dc 1349 struct opp_table *opp_table;
7de36b0a 1350
2c2709dc
VK
1351 /* Hold our table modification lock here */
1352 mutex_lock(&opp_table_lock);
7de36b0a 1353
2c2709dc
VK
1354 /* Check for existing table for 'dev' first */
1355 opp_table = _find_opp_table(dev);
1356 if (IS_ERR(opp_table)) {
1357 dev_err(dev, "Failed to find opp_table: %ld\n",
1358 PTR_ERR(opp_table));
7de36b0a
VK
1359 goto unlock;
1360 }
1361
2c2709dc
VK
1362 /* Make sure there are no concurrent readers while updating opp_table */
1363 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1364
2c2709dc 1365 if (!opp_table->supported_hw) {
7de36b0a
VK
1366 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1367 __func__);
1368 goto unlock;
1369 }
1370
2c2709dc
VK
1371 kfree(opp_table->supported_hw);
1372 opp_table->supported_hw = NULL;
1373 opp_table->supported_hw_count = 0;
7de36b0a 1374
2c2709dc
VK
1375 /* Try freeing opp_table if this was the last blocking resource */
1376 _remove_opp_table(opp_table);
7de36b0a
VK
1377
1378unlock:
2c2709dc 1379 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1380}
1381EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1382
01fb4d3c
VK
1383/**
1384 * dev_pm_opp_set_prop_name() - Set prop-extn name
a5da6447 1385 * @dev: Device for which the prop-name has to be set.
01fb4d3c
VK
1386 * @name: name to postfix to properties.
1387 *
1388 * This is required only for the V2 bindings, and it enables a platform to
1389 * specify the extn to be used for certain property names. The properties to
1390 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1391 * should postfix the property name with -<name> while looking for them.
1392 *
2c2709dc 1393 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1394 * Hence this function internally uses RCU updater strategy with mutex locks
1395 * to keep the integrity of the internal data structures. Callers should ensure
1396 * that this function is *NOT* called under RCU protection or in contexts where
1397 * mutex cannot be locked.
1398 */
1399int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1400{
2c2709dc 1401 struct opp_table *opp_table;
01fb4d3c
VK
1402 int ret = 0;
1403
2c2709dc
VK
1404 /* Hold our table modification lock here */
1405 mutex_lock(&opp_table_lock);
01fb4d3c 1406
2c2709dc
VK
1407 opp_table = _add_opp_table(dev);
1408 if (!opp_table) {
01fb4d3c
VK
1409 ret = -ENOMEM;
1410 goto unlock;
1411 }
1412
2c2709dc
VK
1413 /* Make sure there are no concurrent readers while updating opp_table */
1414 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1415
2c2709dc
VK
1416 /* Do we already have a prop-name associated with opp_table? */
1417 if (opp_table->prop_name) {
01fb4d3c 1418 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
2c2709dc 1419 opp_table->prop_name);
01fb4d3c
VK
1420 ret = -EBUSY;
1421 goto err;
1422 }
1423
2c2709dc
VK
1424 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1425 if (!opp_table->prop_name) {
01fb4d3c
VK
1426 ret = -ENOMEM;
1427 goto err;
1428 }
1429
2c2709dc 1430 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1431 return 0;
1432
1433err:
2c2709dc 1434 _remove_opp_table(opp_table);
01fb4d3c 1435unlock:
2c2709dc 1436 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1437
1438 return ret;
1439}
1440EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1441
1442/**
1443 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
a5da6447 1444 * @dev: Device for which the prop-name has to be put.
01fb4d3c
VK
1445 *
1446 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1447 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
01fb4d3c
VK
1448 * will not be freed.
1449 *
2c2709dc 1450 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1451 * Hence this function internally uses RCU updater strategy with mutex locks
1452 * to keep the integrity of the internal data structures. Callers should ensure
1453 * that this function is *NOT* called under RCU protection or in contexts where
1454 * mutex cannot be locked.
1455 */
1456void dev_pm_opp_put_prop_name(struct device *dev)
1457{
2c2709dc 1458 struct opp_table *opp_table;
01fb4d3c 1459
2c2709dc
VK
1460 /* Hold our table modification lock here */
1461 mutex_lock(&opp_table_lock);
01fb4d3c 1462
2c2709dc
VK
1463 /* Check for existing table for 'dev' first */
1464 opp_table = _find_opp_table(dev);
1465 if (IS_ERR(opp_table)) {
1466 dev_err(dev, "Failed to find opp_table: %ld\n",
1467 PTR_ERR(opp_table));
01fb4d3c
VK
1468 goto unlock;
1469 }
1470
2c2709dc
VK
1471 /* Make sure there are no concurrent readers while updating opp_table */
1472 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1473
2c2709dc 1474 if (!opp_table->prop_name) {
01fb4d3c
VK
1475 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1476 goto unlock;
1477 }
1478
2c2709dc
VK
1479 kfree(opp_table->prop_name);
1480 opp_table->prop_name = NULL;
01fb4d3c 1481
2c2709dc
VK
1482 /* Try freeing opp_table if this was the last blocking resource */
1483 _remove_opp_table(opp_table);
01fb4d3c
VK
1484
1485unlock:
2c2709dc 1486 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1487}
1488EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1489
94735585
VK
1490static int _allocate_set_opp_data(struct opp_table *opp_table)
1491{
1492 struct dev_pm_set_opp_data *data;
1493 int len, count = opp_table->regulator_count;
1494
1495 if (WARN_ON(!count))
1496 return -EINVAL;
1497
1498 /* space for set_opp_data */
1499 len = sizeof(*data);
1500
1501 /* space for old_opp.supplies and new_opp.supplies */
1502 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1503
1504 data = kzalloc(len, GFP_KERNEL);
1505 if (!data)
1506 return -ENOMEM;
1507
1508 data->old_opp.supplies = (void *)(data + 1);
1509 data->new_opp.supplies = data->old_opp.supplies + count;
1510
1511 opp_table->set_opp_data = data;
1512
1513 return 0;
1514}
1515
1516static void _free_set_opp_data(struct opp_table *opp_table)
1517{
1518 kfree(opp_table->set_opp_data);
1519 opp_table->set_opp_data = NULL;
1520}
1521
9f8ea969 1522/**
dfbe4678 1523 * dev_pm_opp_set_regulators() - Set regulator names for the device
9f8ea969 1524 * @dev: Device for which regulator name is being set.
dfbe4678
VK
1525 * @names: Array of pointers to the names of the regulator.
1526 * @count: Number of regulators.
9f8ea969
VK
1527 *
1528 * In order to support OPP switching, OPP layer needs to know the name of the
dfbe4678
VK
1529 * device's regulators, as the core would be required to switch voltages as
1530 * well.
9f8ea969
VK
1531 *
1532 * This must be called before any OPPs are initialized for the device.
1533 *
2c2709dc 1534 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1535 * Hence this function internally uses RCU updater strategy with mutex locks
1536 * to keep the integrity of the internal data structures. Callers should ensure
1537 * that this function is *NOT* called under RCU protection or in contexts where
1538 * mutex cannot be locked.
1539 */
dfbe4678
VK
1540struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1541 const char * const names[],
1542 unsigned int count)
9f8ea969 1543{
2c2709dc 1544 struct opp_table *opp_table;
9f8ea969 1545 struct regulator *reg;
dfbe4678 1546 int ret, i;
9f8ea969 1547
2c2709dc 1548 mutex_lock(&opp_table_lock);
9f8ea969 1549
2c2709dc
VK
1550 opp_table = _add_opp_table(dev);
1551 if (!opp_table) {
9f8ea969
VK
1552 ret = -ENOMEM;
1553 goto unlock;
1554 }
1555
1556 /* This should be called before OPPs are initialized */
2c2709dc 1557 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
9f8ea969
VK
1558 ret = -EBUSY;
1559 goto err;
1560 }
1561
dfbe4678 1562 /* Already have regulators set */
e231f8d7 1563 if (opp_table->regulators) {
9f8ea969
VK
1564 ret = -EBUSY;
1565 goto err;
1566 }
dfbe4678
VK
1567
1568 opp_table->regulators = kmalloc_array(count,
1569 sizeof(*opp_table->regulators),
1570 GFP_KERNEL);
1571 if (!opp_table->regulators) {
1572 ret = -ENOMEM;
9f8ea969
VK
1573 goto err;
1574 }
1575
dfbe4678
VK
1576 for (i = 0; i < count; i++) {
1577 reg = regulator_get_optional(dev, names[i]);
1578 if (IS_ERR(reg)) {
1579 ret = PTR_ERR(reg);
1580 if (ret != -EPROBE_DEFER)
1581 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1582 __func__, names[i], ret);
1583 goto free_regulators;
1584 }
1585
1586 opp_table->regulators[i] = reg;
1587 }
1588
1589 opp_table->regulator_count = count;
9f8ea969 1590
94735585
VK
1591 /* Allocate block only once to pass to set_opp() routines */
1592 ret = _allocate_set_opp_data(opp_table);
1593 if (ret)
1594 goto free_regulators;
1595
2c2709dc 1596 mutex_unlock(&opp_table_lock);
91291d9a 1597 return opp_table;
9f8ea969 1598
dfbe4678
VK
1599free_regulators:
1600 while (i != 0)
1601 regulator_put(opp_table->regulators[--i]);
1602
1603 kfree(opp_table->regulators);
1604 opp_table->regulators = NULL;
94735585 1605 opp_table->regulator_count = 0;
9f8ea969 1606err:
2c2709dc 1607 _remove_opp_table(opp_table);
9f8ea969 1608unlock:
2c2709dc 1609 mutex_unlock(&opp_table_lock);
9f8ea969 1610
91291d9a 1611 return ERR_PTR(ret);
9f8ea969 1612}
dfbe4678 1613EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
9f8ea969
VK
1614
1615/**
dfbe4678
VK
1616 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1617 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
9f8ea969 1618 *
2c2709dc 1619 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1620 * Hence this function internally uses RCU updater strategy with mutex locks
1621 * to keep the integrity of the internal data structures. Callers should ensure
1622 * that this function is *NOT* called under RCU protection or in contexts where
1623 * mutex cannot be locked.
1624 */
dfbe4678 1625void dev_pm_opp_put_regulators(struct opp_table *opp_table)
9f8ea969 1626{
dfbe4678
VK
1627 int i;
1628
2c2709dc 1629 mutex_lock(&opp_table_lock);
9f8ea969 1630
dfbe4678
VK
1631 if (!opp_table->regulators) {
1632 pr_err("%s: Doesn't have regulators set\n", __func__);
9f8ea969
VK
1633 goto unlock;
1634 }
1635
2c2709dc
VK
1636 /* Make sure there are no concurrent readers while updating opp_table */
1637 WARN_ON(!list_empty(&opp_table->opp_list));
9f8ea969 1638
dfbe4678
VK
1639 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1640 regulator_put(opp_table->regulators[i]);
1641
94735585
VK
1642 _free_set_opp_data(opp_table);
1643
dfbe4678
VK
1644 kfree(opp_table->regulators);
1645 opp_table->regulators = NULL;
1646 opp_table->regulator_count = 0;
9f8ea969 1647
2c2709dc
VK
1648 /* Try freeing opp_table if this was the last blocking resource */
1649 _remove_opp_table(opp_table);
9f8ea969
VK
1650
1651unlock:
2c2709dc 1652 mutex_unlock(&opp_table_lock);
9f8ea969 1653}
dfbe4678 1654EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
9f8ea969 1655
4dab160e
VK
1656/**
1657 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1658 * @dev: Device for which the helper is getting registered.
1659 * @set_opp: Custom set OPP helper.
1660 *
1661 * This is useful to support complex platforms (like platforms with multiple
1662 * regulators per device), instead of the generic OPP set rate helper.
1663 *
1664 * This must be called before any OPPs are initialized for the device.
1665 *
1666 * Locking: The internal opp_table and opp structures are RCU protected.
1667 * Hence this function internally uses RCU updater strategy with mutex locks
1668 * to keep the integrity of the internal data structures. Callers should ensure
1669 * that this function is *NOT* called under RCU protection or in contexts where
1670 * mutex cannot be locked.
1671 */
1672int dev_pm_opp_register_set_opp_helper(struct device *dev,
1673 int (*set_opp)(struct dev_pm_set_opp_data *data))
1674{
1675 struct opp_table *opp_table;
1676 int ret;
1677
1678 if (!set_opp)
1679 return -EINVAL;
1680
1681 mutex_lock(&opp_table_lock);
1682
1683 opp_table = _add_opp_table(dev);
1684 if (!opp_table) {
1685 ret = -ENOMEM;
1686 goto unlock;
1687 }
1688
1689 /* This should be called before OPPs are initialized */
1690 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1691 ret = -EBUSY;
1692 goto err;
1693 }
1694
1695 /* Already have custom set_opp helper */
1696 if (WARN_ON(opp_table->set_opp)) {
1697 ret = -EBUSY;
1698 goto err;
1699 }
1700
1701 opp_table->set_opp = set_opp;
1702
1703 mutex_unlock(&opp_table_lock);
1704 return 0;
1705
1706err:
1707 _remove_opp_table(opp_table);
1708unlock:
1709 mutex_unlock(&opp_table_lock);
1710
1711 return ret;
1712}
1713EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1714
1715/**
1716 * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for
1717 * set_opp helper
1718 * @dev: Device for which custom set_opp helper has to be cleared.
1719 *
1720 * Locking: The internal opp_table and opp structures are RCU protected.
1721 * Hence this function internally uses RCU updater strategy with mutex locks
1722 * to keep the integrity of the internal data structures. Callers should ensure
1723 * that this function is *NOT* called under RCU protection or in contexts where
1724 * mutex cannot be locked.
1725 */
1726void dev_pm_opp_register_put_opp_helper(struct device *dev)
1727{
1728 struct opp_table *opp_table;
1729
1730 mutex_lock(&opp_table_lock);
1731
1732 /* Check for existing table for 'dev' first */
1733 opp_table = _find_opp_table(dev);
1734 if (IS_ERR(opp_table)) {
1735 dev_err(dev, "Failed to find opp_table: %ld\n",
1736 PTR_ERR(opp_table));
1737 goto unlock;
1738 }
1739
1740 if (!opp_table->set_opp) {
1741 dev_err(dev, "%s: Doesn't have custom set_opp helper set\n",
1742 __func__);
1743 goto unlock;
1744 }
1745
1746 /* Make sure there are no concurrent readers while updating opp_table */
1747 WARN_ON(!list_empty(&opp_table->opp_list));
1748
1749 opp_table->set_opp = NULL;
1750
1751 /* Try freeing opp_table if this was the last blocking resource */
1752 _remove_opp_table(opp_table);
1753
1754unlock:
1755 mutex_unlock(&opp_table_lock);
1756}
1757EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
1758
38393409
VK
1759/**
1760 * dev_pm_opp_add() - Add an OPP table from a table definitions
1761 * @dev: device for which we do this operation
1762 * @freq: Frequency in Hz for this OPP
1763 * @u_volt: Voltage in uVolts for this OPP
1764 *
2c2709dc 1765 * This function adds an opp definition to the opp table and returns status.
38393409
VK
1766 * The opp is made available by default and it can be controlled using
1767 * dev_pm_opp_enable/disable functions.
1768 *
2c2709dc 1769 * Locking: The internal opp_table and opp structures are RCU protected.
38393409
VK
1770 * Hence this function internally uses RCU updater strategy with mutex locks
1771 * to keep the integrity of the internal data structures. Callers should ensure
1772 * that this function is *NOT* called under RCU protection or in contexts where
1773 * mutex cannot be locked.
1774 *
1775 * Return:
984f16c8 1776 * 0 On success OR
38393409 1777 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 1778 * -EEXIST Freq are same and volt are different OR
38393409 1779 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 1780 * -ENOMEM Memory allocation failure
38393409
VK
1781 */
1782int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1783{
8cd2f6e8
VK
1784 struct opp_table *opp_table;
1785 int ret;
1786
1787 /* Hold our table modification lock here */
1788 mutex_lock(&opp_table_lock);
1789
1790 opp_table = _add_opp_table(dev);
1791 if (!opp_table) {
1792 ret = -ENOMEM;
1793 goto unlock;
1794 }
1795
1796 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1797 if (ret)
1798 _remove_opp_table(opp_table);
1799
1800unlock:
1801 mutex_unlock(&opp_table_lock);
1802 return ret;
38393409 1803}
5d4879cd 1804EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
1805
1806/**
327854c8 1807 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
1808 * @dev: device for which we do this operation
1809 * @freq: OPP frequency to modify availability
1810 * @availability_req: availability status requested for this opp
1811 *
1812 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1813 * share a common logic which is isolated here.
1814 *
984f16c8 1815 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1816 * copy operation, returns 0 if no modification was done OR modification was
e1f60b29
NM
1817 * successful.
1818 *
2c2709dc 1819 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1820 * Hence this function internally uses RCU updater strategy with mutex locks to
1821 * keep the integrity of the internal data structures. Callers should ensure
1822 * that this function is *NOT* called under RCU protection or in contexts where
1823 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1824 */
327854c8
NM
1825static int _opp_set_availability(struct device *dev, unsigned long freq,
1826 bool availability_req)
e1f60b29 1827{
2c2709dc 1828 struct opp_table *opp_table;
47d43ba7 1829 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
1830 int r = 0;
1831
1832 /* keep the node allocated */
47d43ba7 1833 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
59d84ca8 1834 if (!new_opp)
e1f60b29 1835 return -ENOMEM;
e1f60b29 1836
2c2709dc 1837 mutex_lock(&opp_table_lock);
e1f60b29 1838
2c2709dc
VK
1839 /* Find the opp_table */
1840 opp_table = _find_opp_table(dev);
1841 if (IS_ERR(opp_table)) {
1842 r = PTR_ERR(opp_table);
e1f60b29
NM
1843 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1844 goto unlock;
1845 }
1846
37a73ec0
VK
1847 mutex_lock(&opp_table->lock);
1848
e1f60b29 1849 /* Do we have the frequency? */
2c2709dc 1850 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
1851 if (tmp_opp->rate == freq) {
1852 opp = tmp_opp;
1853 break;
1854 }
1855 }
37a73ec0
VK
1856
1857 mutex_unlock(&opp_table->lock);
1858
e1f60b29
NM
1859 if (IS_ERR(opp)) {
1860 r = PTR_ERR(opp);
1861 goto unlock;
1862 }
1863
1864 /* Is update really needed? */
1865 if (opp->available == availability_req)
1866 goto unlock;
1867 /* copy the old data over */
1868 *new_opp = *opp;
1869
1870 /* plug in new node */
1871 new_opp->available = availability_req;
1872
1873 list_replace_rcu(&opp->node, &new_opp->node);
2c2709dc
VK
1874 mutex_unlock(&opp_table_lock);
1875 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
e1f60b29 1876
03ca370f
MH
1877 /* Notify the change of the OPP availability */
1878 if (availability_req)
2c2709dc
VK
1879 srcu_notifier_call_chain(&opp_table->srcu_head,
1880 OPP_EVENT_ENABLE, new_opp);
03ca370f 1881 else
2c2709dc
VK
1882 srcu_notifier_call_chain(&opp_table->srcu_head,
1883 OPP_EVENT_DISABLE, new_opp);
03ca370f 1884
dde8437d 1885 return 0;
e1f60b29
NM
1886
1887unlock:
2c2709dc 1888 mutex_unlock(&opp_table_lock);
e1f60b29
NM
1889 kfree(new_opp);
1890 return r;
1891}
1892
1893/**
5d4879cd 1894 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
1895 * @dev: device for which we do this operation
1896 * @freq: OPP frequency to enable
1897 *
1898 * Enables a provided opp. If the operation is valid, this returns 0, else the
1899 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 1900 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29 1901 *
2c2709dc 1902 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1903 * Hence this function indirectly uses RCU and mutex locks to keep the
1904 * integrity of the internal data structures. Callers should ensure that
1905 * this function is *NOT* called under RCU protection or in contexts where
1906 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1907 *
1908 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1909 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1910 * successful.
e1f60b29 1911 */
5d4879cd 1912int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 1913{
327854c8 1914 return _opp_set_availability(dev, freq, true);
e1f60b29 1915}
5d4879cd 1916EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
1917
1918/**
5d4879cd 1919 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
1920 * @dev: device for which we do this operation
1921 * @freq: OPP frequency to disable
1922 *
1923 * Disables a provided opp. If the operation is valid, this returns
1924 * 0, else the corresponding error value. It is meant to be a temporary
1925 * control by users to make this OPP not available until the circumstances are
5d4879cd 1926 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29 1927 *
2c2709dc 1928 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1929 * Hence this function indirectly uses RCU and mutex locks to keep the
1930 * integrity of the internal data structures. Callers should ensure that
1931 * this function is *NOT* called under RCU protection or in contexts where
1932 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1933 *
1934 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1935 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1936 * successful.
e1f60b29 1937 */
5d4879cd 1938int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 1939{
327854c8 1940 return _opp_set_availability(dev, freq, false);
e1f60b29 1941}
5d4879cd 1942EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 1943
03ca370f 1944/**
dc2c9ad5
VK
1945 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1946 * @dev: Device for which notifier needs to be registered
1947 * @nb: Notifier block to be registered
984f16c8 1948 *
dc2c9ad5
VK
1949 * Return: 0 on success or a negative error value.
1950 */
1951int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
1952{
1953 struct opp_table *opp_table;
1954 int ret;
1955
1956 rcu_read_lock();
1957
1958 opp_table = _find_opp_table(dev);
1959 if (IS_ERR(opp_table)) {
1960 ret = PTR_ERR(opp_table);
1961 goto unlock;
1962 }
1963
1964 ret = srcu_notifier_chain_register(&opp_table->srcu_head, nb);
1965
1966unlock:
1967 rcu_read_unlock();
1968
1969 return ret;
1970}
1971EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1972
1973/**
1974 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1975 * @dev: Device for which notifier needs to be unregistered
1976 * @nb: Notifier block to be unregistered
984f16c8 1977 *
dc2c9ad5 1978 * Return: 0 on success or a negative error value.
03ca370f 1979 */
dc2c9ad5
VK
1980int dev_pm_opp_unregister_notifier(struct device *dev,
1981 struct notifier_block *nb)
03ca370f 1982{
dc2c9ad5
VK
1983 struct opp_table *opp_table;
1984 int ret;
03ca370f 1985
dc2c9ad5
VK
1986 rcu_read_lock();
1987
1988 opp_table = _find_opp_table(dev);
1989 if (IS_ERR(opp_table)) {
1990 ret = PTR_ERR(opp_table);
1991 goto unlock;
1992 }
1993
1994 ret = srcu_notifier_chain_unregister(&opp_table->srcu_head, nb);
03ca370f 1995
dc2c9ad5
VK
1996unlock:
1997 rcu_read_unlock();
1998
1999 return ret;
03ca370f 2000}
dc2c9ad5 2001EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
b496dfbc 2002
411466c5
SH
2003/*
2004 * Free OPPs either created using static entries present in DT or even the
2005 * dynamically added entries based on remove_all param.
b496dfbc 2006 */
8cd2f6e8
VK
2007void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
2008 bool remove_all)
737002b5 2009{
737002b5
VK
2010 struct dev_pm_opp *opp, *tmp;
2011
9274c892
VK
2012 opp_rcu_lockdep_assert();
2013
2014 /* Find if opp_table manages a single device */
2015 if (list_is_singular(&opp_table->dev_list)) {
2016 /* Free static OPPs */
2017 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
2018 if (remove_all || !opp->dynamic)
2019 _opp_remove(opp_table, opp);
2020 }
2021 } else {
2022 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
2023 }
2024}
2025
2026void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
2027{
2028 struct opp_table *opp_table;
2029
2c2709dc
VK
2030 /* Hold our table modification lock here */
2031 mutex_lock(&opp_table_lock);
06441658 2032
2c2709dc
VK
2033 /* Check for existing table for 'dev' */
2034 opp_table = _find_opp_table(dev);
2035 if (IS_ERR(opp_table)) {
2036 int error = PTR_ERR(opp_table);
737002b5
VK
2037
2038 if (error != -ENODEV)
2c2709dc 2039 WARN(1, "%s: opp_table: %d\n",
737002b5
VK
2040 IS_ERR_OR_NULL(dev) ?
2041 "Invalid device" : dev_name(dev),
2042 error);
06441658 2043 goto unlock;
737002b5
VK
2044 }
2045
9274c892 2046 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
737002b5 2047
06441658 2048unlock:
2c2709dc 2049 mutex_unlock(&opp_table_lock);
737002b5 2050}
129eec55
VK
2051
2052/**
411466c5 2053 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2c2709dc 2054 * @dev: device pointer used to lookup OPP table.
129eec55 2055 *
411466c5
SH
2056 * Free both OPPs created using static entries present in DT and the
2057 * dynamically added entries.
984f16c8 2058 *
2c2709dc 2059 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
2060 * Hence this function indirectly uses RCU updater strategy with mutex locks
2061 * to keep the integrity of the internal data structures. Callers should ensure
2062 * that this function is *NOT* called under RCU protection or in contexts where
2063 * mutex cannot be locked.
129eec55 2064 */
411466c5 2065void dev_pm_opp_remove_table(struct device *dev)
129eec55 2066{
9274c892 2067 _dev_pm_opp_find_and_remove_table(dev, true);
8d4d4e98 2068}
411466c5 2069EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);