PM / OPP: Propagate error properly from dev_pm_opp_set_sharing_cpus()
[deliverable/linux.git] / drivers / base / power / opp / core.c
CommitLineData
e1f60b29
NM
1/*
2 * Generic OPP Interface
3 *
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
d6d2a528
VK
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
e1f60b29
NM
16#include <linux/errno.h>
17#include <linux/err.h>
e1f60b29 18#include <linux/slab.h>
51990e82 19#include <linux/device.h>
b496dfbc 20#include <linux/of.h>
80126ce7 21#include <linux/export.h>
e1f60b29 22
f59d3ee8 23#include "opp.h"
e1f60b29
NM
24
25/*
26 * The root of the list of all devices. All device_opp structures branch off
27 * from here, with each device_opp containing the list of opp it supports in
28 * various states of availability.
29 */
30static LIST_HEAD(dev_opp_list);
31/* Lock to allow exclusive modification to the device and opp lists */
32static DEFINE_MUTEX(dev_opp_list_lock);
33
b02ded24
DT
34#define opp_rcu_lockdep_assert() \
35do { \
f78f5b90
PM
36 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
37 !lockdep_is_held(&dev_opp_list_lock), \
b02ded24
DT
38 "Missing rcu_read_lock() or " \
39 "dev_opp_list_lock protection"); \
40} while (0)
41
06441658
VK
42static struct device_list_opp *_find_list_dev(const struct device *dev,
43 struct device_opp *dev_opp)
44{
45 struct device_list_opp *list_dev;
46
47 list_for_each_entry(list_dev, &dev_opp->dev_list, node)
48 if (list_dev->dev == dev)
49 return list_dev;
50
51 return NULL;
52}
53
54static struct device_opp *_managed_opp(const struct device_node *np)
55{
56 struct device_opp *dev_opp;
57
58 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) {
59 if (dev_opp->np == np) {
60 /*
61 * Multiple devices can point to the same OPP table and
62 * so will have same node-pointer, np.
63 *
64 * But the OPPs will be considered as shared only if the
65 * OPP table contains a "opp-shared" property.
66 */
67 return dev_opp->shared_opp ? dev_opp : NULL;
68 }
69 }
70
71 return NULL;
72}
73
e1f60b29 74/**
327854c8 75 * _find_device_opp() - find device_opp struct using device pointer
e1f60b29
NM
76 * @dev: device pointer used to lookup device OPPs
77 *
78 * Search list of device OPPs for one containing matching device. Does a RCU
79 * reader operation to grab the pointer needed.
80 *
984f16c8 81 * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or
e1f60b29
NM
82 * -EINVAL based on type of error.
83 *
84 * Locking: This function must be called under rcu_read_lock(). device_opp
85 * is a RCU protected pointer. This means that device_opp is valid as long
86 * as we are under RCU lock.
87 */
f59d3ee8 88struct device_opp *_find_device_opp(struct device *dev)
e1f60b29 89{
06441658 90 struct device_opp *dev_opp;
e1f60b29 91
50a3cb04 92 if (IS_ERR_OR_NULL(dev)) {
e1f60b29
NM
93 pr_err("%s: Invalid parameters\n", __func__);
94 return ERR_PTR(-EINVAL);
95 }
96
06441658
VK
97 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node)
98 if (_find_list_dev(dev, dev_opp))
99 return dev_opp;
e1f60b29 100
06441658 101 return ERR_PTR(-ENODEV);
e1f60b29
NM
102}
103
104/**
5d4879cd 105 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
e1f60b29
NM
106 * @opp: opp for which voltage has to be returned for
107 *
984f16c8 108 * Return: voltage in micro volt corresponding to the opp, else
e1f60b29
NM
109 * return 0
110 *
111 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
112 * protected pointer. This means that opp which could have been fetched by
113 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
114 * under RCU lock. The pointer returned by the opp_find_freq family must be
115 * used in the same section as the usage of this function with the pointer
116 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
117 * pointer.
118 */
47d43ba7 119unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
e1f60b29 120{
47d43ba7 121 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
122 unsigned long v = 0;
123
04bf1c7f
KK
124 opp_rcu_lockdep_assert();
125
e1f60b29 126 tmp_opp = rcu_dereference(opp);
50a3cb04 127 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
e1f60b29
NM
128 pr_err("%s: Invalid parameters\n", __func__);
129 else
130 v = tmp_opp->u_volt;
131
132 return v;
133}
5d4879cd 134EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
e1f60b29
NM
135
136/**
5d4879cd 137 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
e1f60b29
NM
138 * @opp: opp for which frequency has to be returned for
139 *
984f16c8 140 * Return: frequency in hertz corresponding to the opp, else
e1f60b29
NM
141 * return 0
142 *
143 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
144 * protected pointer. This means that opp which could have been fetched by
145 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
146 * under RCU lock. The pointer returned by the opp_find_freq family must be
147 * used in the same section as the usage of this function with the pointer
148 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
149 * pointer.
150 */
47d43ba7 151unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
e1f60b29 152{
47d43ba7 153 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
154 unsigned long f = 0;
155
04bf1c7f
KK
156 opp_rcu_lockdep_assert();
157
e1f60b29 158 tmp_opp = rcu_dereference(opp);
50a3cb04 159 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
e1f60b29
NM
160 pr_err("%s: Invalid parameters\n", __func__);
161 else
162 f = tmp_opp->rate;
163
164 return f;
165}
5d4879cd 166EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
e1f60b29 167
19445b25
BZ
168/**
169 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
170 * @opp: opp for which turbo mode is being verified
171 *
172 * Turbo OPPs are not for normal use, and can be enabled (under certain
173 * conditions) for short duration of times to finish high throughput work
174 * quickly. Running on them for longer times may overheat the chip.
175 *
176 * Return: true if opp is turbo opp, else false.
177 *
178 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
179 * protected pointer. This means that opp which could have been fetched by
180 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
181 * under RCU lock. The pointer returned by the opp_find_freq family must be
182 * used in the same section as the usage of this function with the pointer
183 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
184 * pointer.
185 */
186bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
187{
188 struct dev_pm_opp *tmp_opp;
189
190 opp_rcu_lockdep_assert();
191
192 tmp_opp = rcu_dereference(opp);
193 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
194 pr_err("%s: Invalid parameters\n", __func__);
195 return false;
196 }
197
198 return tmp_opp->turbo;
199}
200EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
201
3ca9bb33
VK
202/**
203 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
204 * @dev: device for which we do this operation
205 *
206 * Return: This function returns the max clock latency in nanoseconds.
207 *
208 * Locking: This function takes rcu_read_lock().
209 */
210unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
211{
212 struct device_opp *dev_opp;
213 unsigned long clock_latency_ns;
214
215 rcu_read_lock();
216
217 dev_opp = _find_device_opp(dev);
218 if (IS_ERR(dev_opp))
219 clock_latency_ns = 0;
220 else
221 clock_latency_ns = dev_opp->clock_latency_ns_max;
222
223 rcu_read_unlock();
224 return clock_latency_ns;
225}
226EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
227
4eafbd15
BZ
228/**
229 * dev_pm_opp_get_suspend_opp() - Get suspend opp
230 * @dev: device for which we do this operation
231 *
232 * Return: This function returns pointer to the suspend opp if it is
1b2b90cb 233 * defined and available, otherwise it returns NULL.
4eafbd15
BZ
234 *
235 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
236 * protected pointer. The reason for the same is that the opp pointer which is
237 * returned will remain valid for use with opp_get_{voltage, freq} only while
238 * under the locked area. The pointer returned must be used prior to unlocking
239 * with rcu_read_unlock() to maintain the integrity of the pointer.
240 */
241struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
242{
243 struct device_opp *dev_opp;
4eafbd15
BZ
244
245 opp_rcu_lockdep_assert();
246
247 dev_opp = _find_device_opp(dev);
1b2b90cb
VK
248 if (IS_ERR(dev_opp) || !dev_opp->suspend_opp ||
249 !dev_opp->suspend_opp->available)
250 return NULL;
4eafbd15 251
1b2b90cb 252 return dev_opp->suspend_opp;
4eafbd15
BZ
253}
254EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
255
e1f60b29 256/**
5d4879cd 257 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
e1f60b29
NM
258 * @dev: device for which we do this operation
259 *
984f16c8 260 * Return: This function returns the number of available opps if there are any,
e1f60b29
NM
261 * else returns 0 if none or the corresponding error value.
262 *
b4718c02 263 * Locking: This function takes rcu_read_lock().
e1f60b29 264 */
5d4879cd 265int dev_pm_opp_get_opp_count(struct device *dev)
e1f60b29
NM
266{
267 struct device_opp *dev_opp;
47d43ba7 268 struct dev_pm_opp *temp_opp;
e1f60b29
NM
269 int count = 0;
270
b4718c02 271 rcu_read_lock();
b02ded24 272
327854c8 273 dev_opp = _find_device_opp(dev);
e1f60b29 274 if (IS_ERR(dev_opp)) {
b4718c02
DT
275 count = PTR_ERR(dev_opp);
276 dev_err(dev, "%s: device OPP not found (%d)\n",
277 __func__, count);
278 goto out_unlock;
e1f60b29
NM
279 }
280
281 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
282 if (temp_opp->available)
283 count++;
284 }
285
b4718c02
DT
286out_unlock:
287 rcu_read_unlock();
e1f60b29
NM
288 return count;
289}
5d4879cd 290EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
e1f60b29
NM
291
292/**
5d4879cd 293 * dev_pm_opp_find_freq_exact() - search for an exact frequency
e1f60b29
NM
294 * @dev: device for which we do this operation
295 * @freq: frequency to search for
7ae49618 296 * @available: true/false - match for available opp
e1f60b29 297 *
984f16c8
NM
298 * Return: Searches for exact match in the opp list and returns pointer to the
299 * matching opp if found, else returns ERR_PTR in case of error and should
300 * be handled using IS_ERR. Error return values can be:
0779726c
NM
301 * EINVAL: for bad pointer
302 * ERANGE: no match found for search
303 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
304 *
305 * Note: available is a modifier for the search. if available=true, then the
306 * match is for exact matching frequency and is available in the stored OPP
307 * table. if false, the match is for exact frequency which is not available.
308 *
309 * This provides a mechanism to enable an opp which is not available currently
310 * or the opposite as well.
311 *
312 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
313 * protected pointer. The reason for the same is that the opp pointer which is
314 * returned will remain valid for use with opp_get_{voltage, freq} only while
315 * under the locked area. The pointer returned must be used prior to unlocking
316 * with rcu_read_unlock() to maintain the integrity of the pointer.
317 */
47d43ba7
NM
318struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
319 unsigned long freq,
320 bool available)
e1f60b29
NM
321{
322 struct device_opp *dev_opp;
47d43ba7 323 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 324
b02ded24
DT
325 opp_rcu_lockdep_assert();
326
327854c8 327 dev_opp = _find_device_opp(dev);
e1f60b29
NM
328 if (IS_ERR(dev_opp)) {
329 int r = PTR_ERR(dev_opp);
330 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
331 return ERR_PTR(r);
332 }
333
334 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
335 if (temp_opp->available == available &&
336 temp_opp->rate == freq) {
337 opp = temp_opp;
338 break;
339 }
340 }
341
342 return opp;
343}
5d4879cd 344EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
e1f60b29
NM
345
346/**
5d4879cd 347 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
e1f60b29
NM
348 * @dev: device for which we do this operation
349 * @freq: Start frequency
350 *
351 * Search for the matching ceil *available* OPP from a starting freq
352 * for a device.
353 *
984f16c8 354 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
355 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
356 * values can be:
357 * EINVAL: for bad pointer
358 * ERANGE: no match found for search
359 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
360 *
361 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
362 * protected pointer. The reason for the same is that the opp pointer which is
363 * returned will remain valid for use with opp_get_{voltage, freq} only while
364 * under the locked area. The pointer returned must be used prior to unlocking
365 * with rcu_read_unlock() to maintain the integrity of the pointer.
366 */
47d43ba7
NM
367struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
368 unsigned long *freq)
e1f60b29
NM
369{
370 struct device_opp *dev_opp;
47d43ba7 371 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 372
b02ded24
DT
373 opp_rcu_lockdep_assert();
374
e1f60b29
NM
375 if (!dev || !freq) {
376 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
377 return ERR_PTR(-EINVAL);
378 }
379
327854c8 380 dev_opp = _find_device_opp(dev);
e1f60b29 381 if (IS_ERR(dev_opp))
0779726c 382 return ERR_CAST(dev_opp);
e1f60b29
NM
383
384 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
385 if (temp_opp->available && temp_opp->rate >= *freq) {
386 opp = temp_opp;
387 *freq = opp->rate;
388 break;
389 }
390 }
391
392 return opp;
393}
5d4879cd 394EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
e1f60b29
NM
395
396/**
5d4879cd 397 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
e1f60b29
NM
398 * @dev: device for which we do this operation
399 * @freq: Start frequency
400 *
401 * Search for the matching floor *available* OPP from a starting freq
402 * for a device.
403 *
984f16c8 404 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
405 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
406 * values can be:
407 * EINVAL: for bad pointer
408 * ERANGE: no match found for search
409 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
410 *
411 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
412 * protected pointer. The reason for the same is that the opp pointer which is
413 * returned will remain valid for use with opp_get_{voltage, freq} only while
414 * under the locked area. The pointer returned must be used prior to unlocking
415 * with rcu_read_unlock() to maintain the integrity of the pointer.
416 */
47d43ba7
NM
417struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
418 unsigned long *freq)
e1f60b29
NM
419{
420 struct device_opp *dev_opp;
47d43ba7 421 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 422
b02ded24
DT
423 opp_rcu_lockdep_assert();
424
e1f60b29
NM
425 if (!dev || !freq) {
426 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
427 return ERR_PTR(-EINVAL);
428 }
429
327854c8 430 dev_opp = _find_device_opp(dev);
e1f60b29 431 if (IS_ERR(dev_opp))
0779726c 432 return ERR_CAST(dev_opp);
e1f60b29
NM
433
434 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
435 if (temp_opp->available) {
436 /* go to the next node, before choosing prev */
437 if (temp_opp->rate > *freq)
438 break;
439 else
440 opp = temp_opp;
441 }
442 }
443 if (!IS_ERR(opp))
444 *freq = opp->rate;
445
446 return opp;
447}
5d4879cd 448EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
e1f60b29 449
06441658
VK
450/* List-dev Helpers */
451static void _kfree_list_dev_rcu(struct rcu_head *head)
452{
453 struct device_list_opp *list_dev;
454
455 list_dev = container_of(head, struct device_list_opp, rcu_head);
456 kfree_rcu(list_dev, rcu_head);
457}
458
459static void _remove_list_dev(struct device_list_opp *list_dev,
460 struct device_opp *dev_opp)
461{
462 list_del(&list_dev->node);
463 call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head,
464 _kfree_list_dev_rcu);
465}
466
f59d3ee8
VK
467struct device_list_opp *_add_list_dev(const struct device *dev,
468 struct device_opp *dev_opp)
06441658
VK
469{
470 struct device_list_opp *list_dev;
471
472 list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL);
473 if (!list_dev)
474 return NULL;
475
476 /* Initialize list-dev */
477 list_dev->dev = dev;
478 list_add_rcu(&list_dev->node, &dev_opp->dev_list);
479
480 return list_dev;
481}
482
984f16c8 483/**
aa5f2f85 484 * _add_device_opp() - Find device OPP table or allocate a new one
984f16c8
NM
485 * @dev: device for which we do this operation
486 *
aa5f2f85
VK
487 * It tries to find an existing table first, if it couldn't find one, it
488 * allocates a new OPP table and returns that.
984f16c8
NM
489 *
490 * Return: valid device_opp pointer if success, else NULL.
491 */
327854c8 492static struct device_opp *_add_device_opp(struct device *dev)
07cce74a
VK
493{
494 struct device_opp *dev_opp;
06441658 495 struct device_list_opp *list_dev;
07cce74a 496
aa5f2f85
VK
497 /* Check for existing list for 'dev' first */
498 dev_opp = _find_device_opp(dev);
499 if (!IS_ERR(dev_opp))
500 return dev_opp;
07cce74a
VK
501
502 /*
503 * Allocate a new device OPP table. In the infrequent case where a new
504 * device is needed to be added, we pay this penalty.
505 */
506 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
507 if (!dev_opp)
508 return NULL;
509
06441658
VK
510 INIT_LIST_HEAD(&dev_opp->dev_list);
511
512 list_dev = _add_list_dev(dev, dev_opp);
513 if (!list_dev) {
514 kfree(dev_opp);
515 return NULL;
516 }
517
07cce74a
VK
518 srcu_init_notifier_head(&dev_opp->srcu_head);
519 INIT_LIST_HEAD(&dev_opp->opp_list);
520
521 /* Secure the device list modification */
522 list_add_rcu(&dev_opp->node, &dev_opp_list);
523 return dev_opp;
524}
525
984f16c8 526/**
737002b5
VK
527 * _kfree_device_rcu() - Free device_opp RCU handler
528 * @head: RCU head
984f16c8 529 */
737002b5 530static void _kfree_device_rcu(struct rcu_head *head)
e1f60b29 531{
737002b5 532 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
6ce4184d 533
737002b5 534 kfree_rcu(device_opp, rcu_head);
e1f60b29 535}
38393409
VK
536
537/**
3bac42ca
VK
538 * _remove_device_opp() - Removes a device OPP table
539 * @dev_opp: device OPP table to be removed.
38393409 540 *
3bac42ca 541 * Removes/frees device OPP table it it doesn't contain any OPPs.
38393409 542 */
3bac42ca 543static void _remove_device_opp(struct device_opp *dev_opp)
38393409 544{
06441658
VK
545 struct device_list_opp *list_dev;
546
3bac42ca
VK
547 if (!list_empty(&dev_opp->opp_list))
548 return;
549
06441658
VK
550 list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp,
551 node);
552
553 _remove_list_dev(list_dev, dev_opp);
554
555 /* dev_list must be empty now */
556 WARN_ON(!list_empty(&dev_opp->dev_list));
557
3bac42ca
VK
558 list_del_rcu(&dev_opp->node);
559 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
560 _kfree_device_rcu);
38393409 561}
e1f60b29 562
984f16c8
NM
563/**
564 * _kfree_opp_rcu() - Free OPP RCU handler
565 * @head: RCU head
566 */
327854c8 567static void _kfree_opp_rcu(struct rcu_head *head)
129eec55
VK
568{
569 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
570
571 kfree_rcu(opp, rcu_head);
572}
573
984f16c8
NM
574/**
575 * _opp_remove() - Remove an OPP from a table definition
576 * @dev_opp: points back to the device_opp struct this opp belongs to
577 * @opp: pointer to the OPP to remove
23dacf6d 578 * @notify: OPP_EVENT_REMOVE notification should be sent or not
984f16c8
NM
579 *
580 * This function removes an opp definition from the opp list.
581 *
582 * Locking: The internal device_opp and opp structures are RCU protected.
583 * It is assumed that the caller holds required mutex for an RCU updater
584 * strategy.
585 */
327854c8 586static void _opp_remove(struct device_opp *dev_opp,
23dacf6d 587 struct dev_pm_opp *opp, bool notify)
129eec55
VK
588{
589 /*
590 * Notify the changes in the availability of the operable
591 * frequency/voltage list.
592 */
23dacf6d
VK
593 if (notify)
594 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
129eec55 595 list_del_rcu(&opp->node);
327854c8 596 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
129eec55 597
3bac42ca 598 _remove_device_opp(dev_opp);
129eec55
VK
599}
600
601/**
602 * dev_pm_opp_remove() - Remove an OPP from OPP list
603 * @dev: device for which we do this operation
604 * @freq: OPP to remove with matching 'freq'
605 *
606 * This function removes an opp from the opp list.
984f16c8
NM
607 *
608 * Locking: The internal device_opp and opp structures are RCU protected.
609 * Hence this function internally uses RCU updater strategy with mutex locks
610 * to keep the integrity of the internal data structures. Callers should ensure
611 * that this function is *NOT* called under RCU protection or in contexts where
612 * mutex cannot be locked.
129eec55
VK
613 */
614void dev_pm_opp_remove(struct device *dev, unsigned long freq)
615{
616 struct dev_pm_opp *opp;
617 struct device_opp *dev_opp;
618 bool found = false;
619
620 /* Hold our list modification lock here */
621 mutex_lock(&dev_opp_list_lock);
622
327854c8 623 dev_opp = _find_device_opp(dev);
129eec55
VK
624 if (IS_ERR(dev_opp))
625 goto unlock;
626
627 list_for_each_entry(opp, &dev_opp->opp_list, node) {
628 if (opp->rate == freq) {
629 found = true;
630 break;
631 }
632 }
633
634 if (!found) {
635 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
636 __func__, freq);
637 goto unlock;
638 }
639
23dacf6d 640 _opp_remove(dev_opp, opp, true);
129eec55
VK
641unlock:
642 mutex_unlock(&dev_opp_list_lock);
643}
644EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
645
23dacf6d
VK
646static struct dev_pm_opp *_allocate_opp(struct device *dev,
647 struct device_opp **dev_opp)
e1f60b29 648{
23dacf6d 649 struct dev_pm_opp *opp;
e1f60b29 650
23dacf6d
VK
651 /* allocate new OPP node */
652 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
653 if (!opp)
654 return NULL;
e1f60b29 655
23dacf6d 656 INIT_LIST_HEAD(&opp->node);
e1f60b29 657
23dacf6d
VK
658 *dev_opp = _add_device_opp(dev);
659 if (!*dev_opp) {
660 kfree(opp);
661 return NULL;
662 }
663
664 return opp;
665}
666
06441658
VK
667static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
668 struct device_opp *dev_opp)
23dacf6d
VK
669{
670 struct dev_pm_opp *opp;
671 struct list_head *head = &dev_opp->opp_list;
672
673 /*
674 * Insert new OPP in order of increasing frequency and discard if
675 * already present.
676 *
677 * Need to use &dev_opp->opp_list in the condition part of the 'for'
678 * loop, don't replace it with head otherwise it will become an infinite
679 * loop.
680 */
681 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
682 if (new_opp->rate > opp->rate) {
683 head = &opp->node;
684 continue;
685 }
686
687 if (new_opp->rate < opp->rate)
688 break;
689
690 /* Duplicate OPPs */
06441658 691 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
23dacf6d
VK
692 __func__, opp->rate, opp->u_volt, opp->available,
693 new_opp->rate, new_opp->u_volt, new_opp->available);
694
695 return opp->available && new_opp->u_volt == opp->u_volt ?
696 0 : -EEXIST;
697 }
698
699 new_opp->dev_opp = dev_opp;
700 list_add_rcu(&new_opp->node, head);
701
702 return 0;
703}
704
984f16c8 705/**
b64b9c3f 706 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
984f16c8
NM
707 * @dev: device for which we do this operation
708 * @freq: Frequency in Hz for this OPP
709 * @u_volt: Voltage in uVolts for this OPP
710 * @dynamic: Dynamically added OPPs.
711 *
712 * This function adds an opp definition to the opp list and returns status.
713 * The opp is made available by default and it can be controlled using
714 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
715 *
8f8d37b2
VK
716 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
717 * and freed by dev_pm_opp_of_remove_table.
984f16c8
NM
718 *
719 * Locking: The internal device_opp and opp structures are RCU protected.
720 * Hence this function internally uses RCU updater strategy with mutex locks
721 * to keep the integrity of the internal data structures. Callers should ensure
722 * that this function is *NOT* called under RCU protection or in contexts where
723 * mutex cannot be locked.
724 *
725 * Return:
726 * 0 On success OR
727 * Duplicate OPPs (both freq and volt are same) and opp->available
728 * -EEXIST Freq are same and volt are different OR
729 * Duplicate OPPs (both freq and volt are same) and !opp->available
730 * -ENOMEM Memory allocation failure
731 */
b64b9c3f
VK
732static int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
733 bool dynamic)
e1f60b29 734{
aa5f2f85 735 struct device_opp *dev_opp;
23dacf6d 736 struct dev_pm_opp *new_opp;
6ce4184d 737 int ret;
e1f60b29 738
e1f60b29
NM
739 /* Hold our list modification lock here */
740 mutex_lock(&dev_opp_list_lock);
741
23dacf6d
VK
742 new_opp = _allocate_opp(dev, &dev_opp);
743 if (!new_opp) {
744 ret = -ENOMEM;
745 goto unlock;
746 }
747
a7470db6 748 /* populate the opp table */
a7470db6
VK
749 new_opp->rate = freq;
750 new_opp->u_volt = u_volt;
751 new_opp->available = true;
23dacf6d 752 new_opp->dynamic = dynamic;
a7470db6 753
06441658 754 ret = _opp_add(dev, new_opp, dev_opp);
23dacf6d 755 if (ret)
6ce4184d 756 goto free_opp;
64ce8545 757
e1f60b29
NM
758 mutex_unlock(&dev_opp_list_lock);
759
03ca370f
MH
760 /*
761 * Notify the changes in the availability of the operable
762 * frequency/voltage list.
763 */
cd1a068a 764 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29 765 return 0;
6ce4184d
VK
766
767free_opp:
23dacf6d
VK
768 _opp_remove(dev_opp, new_opp, false);
769unlock:
6ce4184d 770 mutex_unlock(&dev_opp_list_lock);
6ce4184d 771 return ret;
e1f60b29 772}
38393409 773
27465902 774/* TODO: Support multiple regulators */
ad623c31 775static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev)
27465902
VK
776{
777 u32 microvolt[3] = {0};
ad623c31 778 u32 val;
27465902
VK
779 int count, ret;
780
781 count = of_property_count_u32_elems(opp->np, "opp-microvolt");
782 if (!count)
783 return 0;
784
785 /* There can be one or three elements here */
786 if (count != 1 && count != 3) {
787 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
788 __func__, count);
789 return -EINVAL;
790 }
791
792 ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
793 count);
794 if (ret) {
795 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
796 ret);
797 return -EINVAL;
798 }
799
800 opp->u_volt = microvolt[0];
801 opp->u_volt_min = microvolt[1];
802 opp->u_volt_max = microvolt[2];
803
ad623c31
VK
804 if (!of_property_read_u32(opp->np, "opp-microamp", &val))
805 opp->u_amp = val;
806
27465902
VK
807 return 0;
808}
809
810/**
811 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
812 * @dev: device for which we do this operation
813 * @np: device node
814 *
815 * This function adds an opp definition to the opp list and returns status. The
816 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
817 * removed by dev_pm_opp_remove.
818 *
819 * Locking: The internal device_opp and opp structures are RCU protected.
820 * Hence this function internally uses RCU updater strategy with mutex locks
821 * to keep the integrity of the internal data structures. Callers should ensure
822 * that this function is *NOT* called under RCU protection or in contexts where
823 * mutex cannot be locked.
824 *
825 * Return:
826 * 0 On success OR
827 * Duplicate OPPs (both freq and volt are same) and opp->available
828 * -EEXIST Freq are same and volt are different OR
829 * Duplicate OPPs (both freq and volt are same) and !opp->available
830 * -ENOMEM Memory allocation failure
831 * -EINVAL Failed parsing the OPP node
832 */
833static int _opp_add_static_v2(struct device *dev, struct device_node *np)
834{
835 struct device_opp *dev_opp;
836 struct dev_pm_opp *new_opp;
837 u64 rate;
68fa9f0a 838 u32 val;
27465902
VK
839 int ret;
840
841 /* Hold our list modification lock here */
842 mutex_lock(&dev_opp_list_lock);
843
844 new_opp = _allocate_opp(dev, &dev_opp);
845 if (!new_opp) {
846 ret = -ENOMEM;
847 goto unlock;
848 }
849
850 ret = of_property_read_u64(np, "opp-hz", &rate);
851 if (ret < 0) {
852 dev_err(dev, "%s: opp-hz not found\n", __func__);
853 goto free_opp;
854 }
855
856 /*
857 * Rate is defined as an unsigned long in clk API, and so casting
858 * explicitly to its type. Must be fixed once rate is 64 bit
859 * guaranteed in clk API.
860 */
861 new_opp->rate = (unsigned long)rate;
862 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
863
864 new_opp->np = np;
865 new_opp->dynamic = false;
866 new_opp->available = true;
68fa9f0a
VK
867
868 if (!of_property_read_u32(np, "clock-latency-ns", &val))
869 new_opp->clock_latency_ns = val;
27465902 870
ad623c31 871 ret = opp_parse_supplies(new_opp, dev);
27465902
VK
872 if (ret)
873 goto free_opp;
874
06441658 875 ret = _opp_add(dev, new_opp, dev_opp);
27465902
VK
876 if (ret)
877 goto free_opp;
878
ad656a6a
VK
879 /* OPP to select on device suspend */
880 if (of_property_read_bool(np, "opp-suspend")) {
881 if (dev_opp->suspend_opp)
882 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
883 __func__, dev_opp->suspend_opp->rate,
884 new_opp->rate);
885 else
886 dev_opp->suspend_opp = new_opp;
887 }
888
3ca9bb33
VK
889 if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max)
890 dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns;
891
27465902
VK
892 mutex_unlock(&dev_opp_list_lock);
893
3ca9bb33 894 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
27465902 895 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
3ca9bb33
VK
896 new_opp->u_volt_min, new_opp->u_volt_max,
897 new_opp->clock_latency_ns);
27465902
VK
898
899 /*
900 * Notify the changes in the availability of the operable
901 * frequency/voltage list.
902 */
903 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
904 return 0;
905
906free_opp:
907 _opp_remove(dev_opp, new_opp, false);
908unlock:
909 mutex_unlock(&dev_opp_list_lock);
910 return ret;
911}
912
38393409
VK
913/**
914 * dev_pm_opp_add() - Add an OPP table from a table definitions
915 * @dev: device for which we do this operation
916 * @freq: Frequency in Hz for this OPP
917 * @u_volt: Voltage in uVolts for this OPP
918 *
919 * This function adds an opp definition to the opp list and returns status.
920 * The opp is made available by default and it can be controlled using
921 * dev_pm_opp_enable/disable functions.
922 *
923 * Locking: The internal device_opp and opp structures are RCU protected.
924 * Hence this function internally uses RCU updater strategy with mutex locks
925 * to keep the integrity of the internal data structures. Callers should ensure
926 * that this function is *NOT* called under RCU protection or in contexts where
927 * mutex cannot be locked.
928 *
929 * Return:
984f16c8 930 * 0 On success OR
38393409 931 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 932 * -EEXIST Freq are same and volt are different OR
38393409 933 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 934 * -ENOMEM Memory allocation failure
38393409
VK
935 */
936int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
937{
b64b9c3f 938 return _opp_add_v1(dev, freq, u_volt, true);
38393409 939}
5d4879cd 940EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
941
942/**
327854c8 943 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
944 * @dev: device for which we do this operation
945 * @freq: OPP frequency to modify availability
946 * @availability_req: availability status requested for this opp
947 *
948 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
949 * share a common logic which is isolated here.
950 *
984f16c8 951 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1f60b29
NM
952 * copy operation, returns 0 if no modifcation was done OR modification was
953 * successful.
954 *
955 * Locking: The internal device_opp and opp structures are RCU protected.
956 * Hence this function internally uses RCU updater strategy with mutex locks to
957 * keep the integrity of the internal data structures. Callers should ensure
958 * that this function is *NOT* called under RCU protection or in contexts where
959 * mutex locking or synchronize_rcu() blocking calls cannot be used.
960 */
327854c8
NM
961static int _opp_set_availability(struct device *dev, unsigned long freq,
962 bool availability_req)
e1f60b29 963{
29df0ee1 964 struct device_opp *dev_opp;
47d43ba7 965 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
966 int r = 0;
967
968 /* keep the node allocated */
47d43ba7 969 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
59d84ca8 970 if (!new_opp)
e1f60b29 971 return -ENOMEM;
e1f60b29
NM
972
973 mutex_lock(&dev_opp_list_lock);
974
975 /* Find the device_opp */
327854c8 976 dev_opp = _find_device_opp(dev);
e1f60b29
NM
977 if (IS_ERR(dev_opp)) {
978 r = PTR_ERR(dev_opp);
979 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
980 goto unlock;
981 }
982
983 /* Do we have the frequency? */
984 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
985 if (tmp_opp->rate == freq) {
986 opp = tmp_opp;
987 break;
988 }
989 }
990 if (IS_ERR(opp)) {
991 r = PTR_ERR(opp);
992 goto unlock;
993 }
994
995 /* Is update really needed? */
996 if (opp->available == availability_req)
997 goto unlock;
998 /* copy the old data over */
999 *new_opp = *opp;
1000
1001 /* plug in new node */
1002 new_opp->available = availability_req;
1003
1004 list_replace_rcu(&opp->node, &new_opp->node);
1005 mutex_unlock(&dev_opp_list_lock);
327854c8 1006 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
e1f60b29 1007
03ca370f
MH
1008 /* Notify the change of the OPP availability */
1009 if (availability_req)
cd1a068a 1010 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
03ca370f
MH
1011 new_opp);
1012 else
cd1a068a 1013 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
03ca370f
MH
1014 new_opp);
1015
dde8437d 1016 return 0;
e1f60b29
NM
1017
1018unlock:
1019 mutex_unlock(&dev_opp_list_lock);
e1f60b29
NM
1020 kfree(new_opp);
1021 return r;
1022}
1023
1024/**
5d4879cd 1025 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
1026 * @dev: device for which we do this operation
1027 * @freq: OPP frequency to enable
1028 *
1029 * Enables a provided opp. If the operation is valid, this returns 0, else the
1030 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 1031 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29
NM
1032 *
1033 * Locking: The internal device_opp and opp structures are RCU protected.
1034 * Hence this function indirectly uses RCU and mutex locks to keep the
1035 * integrity of the internal data structures. Callers should ensure that
1036 * this function is *NOT* called under RCU protection or in contexts where
1037 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1038 *
1039 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1040 * copy operation, returns 0 if no modifcation was done OR modification was
1041 * successful.
e1f60b29 1042 */
5d4879cd 1043int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 1044{
327854c8 1045 return _opp_set_availability(dev, freq, true);
e1f60b29 1046}
5d4879cd 1047EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
1048
1049/**
5d4879cd 1050 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
1051 * @dev: device for which we do this operation
1052 * @freq: OPP frequency to disable
1053 *
1054 * Disables a provided opp. If the operation is valid, this returns
1055 * 0, else the corresponding error value. It is meant to be a temporary
1056 * control by users to make this OPP not available until the circumstances are
5d4879cd 1057 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29
NM
1058 *
1059 * Locking: The internal device_opp and opp structures are RCU protected.
1060 * Hence this function indirectly uses RCU and mutex locks to keep the
1061 * integrity of the internal data structures. Callers should ensure that
1062 * this function is *NOT* called under RCU protection or in contexts where
1063 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1064 *
1065 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1066 * copy operation, returns 0 if no modifcation was done OR modification was
1067 * successful.
e1f60b29 1068 */
5d4879cd 1069int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 1070{
327854c8 1071 return _opp_set_availability(dev, freq, false);
e1f60b29 1072}
5d4879cd 1073EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 1074
03ca370f 1075/**
5d4879cd 1076 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
03ca370f 1077 * @dev: device pointer used to lookup device OPPs.
984f16c8
NM
1078 *
1079 * Return: pointer to notifier head if found, otherwise -ENODEV or
1080 * -EINVAL based on type of error casted as pointer. value must be checked
1081 * with IS_ERR to determine valid pointer or error result.
1082 *
1083 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
1084 * protected pointer. The reason for the same is that the opp pointer which is
1085 * returned will remain valid for use with opp_get_{voltage, freq} only while
1086 * under the locked area. The pointer returned must be used prior to unlocking
1087 * with rcu_read_unlock() to maintain the integrity of the pointer.
03ca370f 1088 */
5d4879cd 1089struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
03ca370f 1090{
327854c8 1091 struct device_opp *dev_opp = _find_device_opp(dev);
03ca370f
MH
1092
1093 if (IS_ERR(dev_opp))
156acb16 1094 return ERR_CAST(dev_opp); /* matching type */
03ca370f 1095
cd1a068a 1096 return &dev_opp->srcu_head;
03ca370f 1097}
4679ec37 1098EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
b496dfbc
SG
1099
1100#ifdef CONFIG_OF
1101/**
8f8d37b2
VK
1102 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
1103 * entries
b496dfbc
SG
1104 * @dev: device pointer used to lookup device OPPs.
1105 *
737002b5 1106 * Free OPPs created using static entries present in DT.
984f16c8
NM
1107 *
1108 * Locking: The internal device_opp and opp structures are RCU protected.
1109 * Hence this function indirectly uses RCU updater strategy with mutex locks
1110 * to keep the integrity of the internal data structures. Callers should ensure
1111 * that this function is *NOT* called under RCU protection or in contexts where
1112 * mutex cannot be locked.
b496dfbc 1113 */
8f8d37b2 1114void dev_pm_opp_of_remove_table(struct device *dev)
737002b5
VK
1115{
1116 struct device_opp *dev_opp;
1117 struct dev_pm_opp *opp, *tmp;
1118
06441658
VK
1119 /* Hold our list modification lock here */
1120 mutex_lock(&dev_opp_list_lock);
1121
737002b5
VK
1122 /* Check for existing list for 'dev' */
1123 dev_opp = _find_device_opp(dev);
1124 if (IS_ERR(dev_opp)) {
1125 int error = PTR_ERR(dev_opp);
1126
1127 if (error != -ENODEV)
1128 WARN(1, "%s: dev_opp: %d\n",
1129 IS_ERR_OR_NULL(dev) ?
1130 "Invalid device" : dev_name(dev),
1131 error);
06441658 1132 goto unlock;
737002b5
VK
1133 }
1134
06441658
VK
1135 /* Find if dev_opp manages a single device */
1136 if (list_is_singular(&dev_opp->dev_list)) {
1137 /* Free static OPPs */
1138 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1139 if (!opp->dynamic)
1140 _opp_remove(dev_opp, opp, true);
1141 }
1142 } else {
1143 _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp);
737002b5
VK
1144 }
1145
06441658 1146unlock:
737002b5
VK
1147 mutex_unlock(&dev_opp_list_lock);
1148}
8f8d37b2 1149EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
737002b5 1150
1840995c 1151/* Returns opp descriptor node for a device, caller must do of_node_put() */
f59d3ee8 1152struct device_node *_of_get_opp_desc_node(struct device *dev)
8d4d4e98 1153{
8d4d4e98
VK
1154 /*
1155 * TODO: Support for multiple OPP tables.
1156 *
1157 * There should be only ONE phandle present in "operating-points-v2"
1158 * property.
1159 */
8d4d4e98 1160
1840995c 1161 return of_parse_phandle(dev->of_node, "operating-points-v2", 0);
8d4d4e98
VK
1162}
1163
27465902 1164/* Initializes OPP tables based on new bindings */
f0489a5e 1165static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
27465902 1166{
1840995c 1167 struct device_node *np;
06441658 1168 struct device_opp *dev_opp;
27465902
VK
1169 int ret = 0, count = 0;
1170
06441658
VK
1171 dev_opp = _managed_opp(opp_np);
1172 if (dev_opp) {
1173 /* OPPs are already managed */
1174 if (!_add_list_dev(dev, dev_opp))
1175 ret = -ENOMEM;
1840995c 1176 return ret;
06441658
VK
1177 }
1178
27465902
VK
1179 /* We have opp-list node now, iterate over it and add OPPs */
1180 for_each_available_child_of_node(opp_np, np) {
1181 count++;
1182
1183 ret = _opp_add_static_v2(dev, np);
1184 if (ret) {
1185 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1186 ret);
1f821ed7 1187 goto free_table;
27465902
VK
1188 }
1189 }
1190
1191 /* There should be one of more OPP defined */
1840995c
VK
1192 if (WARN_ON(!count))
1193 return -ENOENT;
27465902 1194
1f821ed7
VK
1195 dev_opp = _find_device_opp(dev);
1196 if (WARN_ON(IS_ERR(dev_opp))) {
1197 ret = PTR_ERR(dev_opp);
1198 goto free_table;
06441658 1199 }
27465902 1200
1f821ed7
VK
1201 dev_opp->np = opp_np;
1202 dev_opp->shared_opp = of_property_read_bool(opp_np, "opp-shared");
1203
1f821ed7
VK
1204 return 0;
1205
1206free_table:
8f8d37b2 1207 dev_pm_opp_of_remove_table(dev);
27465902
VK
1208
1209 return ret;
1210}
1211
1212/* Initializes OPP tables based on old-deprecated bindings */
f0489a5e 1213static int _of_add_opp_table_v1(struct device *dev)
b496dfbc
SG
1214{
1215 const struct property *prop;
1216 const __be32 *val;
1217 int nr;
1218
1219 prop = of_find_property(dev->of_node, "operating-points", NULL);
1220 if (!prop)
1221 return -ENODEV;
1222 if (!prop->value)
1223 return -ENODATA;
1224
1225 /*
1226 * Each OPP is a set of tuples consisting of frequency and
1227 * voltage like <freq-kHz vol-uV>.
1228 */
1229 nr = prop->length / sizeof(u32);
1230 if (nr % 2) {
1231 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1232 return -EINVAL;
1233 }
1234
1235 val = prop->value;
1236 while (nr) {
1237 unsigned long freq = be32_to_cpup(val++) * 1000;
1238 unsigned long volt = be32_to_cpup(val++);
1239
b64b9c3f 1240 if (_opp_add_v1(dev, freq, volt, false))
b496dfbc
SG
1241 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1242 __func__, freq);
b496dfbc
SG
1243 nr -= 2;
1244 }
1245
1246 return 0;
1247}
129eec55
VK
1248
1249/**
8f8d37b2 1250 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
129eec55
VK
1251 * @dev: device pointer used to lookup device OPPs.
1252 *
27465902 1253 * Register the initial OPP table with the OPP library for given device.
984f16c8
NM
1254 *
1255 * Locking: The internal device_opp and opp structures are RCU protected.
1256 * Hence this function indirectly uses RCU updater strategy with mutex locks
1257 * to keep the integrity of the internal data structures. Callers should ensure
1258 * that this function is *NOT* called under RCU protection or in contexts where
1259 * mutex cannot be locked.
27465902
VK
1260 *
1261 * Return:
1262 * 0 On success OR
1263 * Duplicate OPPs (both freq and volt are same) and opp->available
1264 * -EEXIST Freq are same and volt are different OR
1265 * Duplicate OPPs (both freq and volt are same) and !opp->available
1266 * -ENOMEM Memory allocation failure
1267 * -ENODEV when 'operating-points' property is not found or is invalid data
1268 * in device node.
1269 * -ENODATA when empty 'operating-points' property is found
1270 * -EINVAL when invalid entries are found in opp-v2 table
129eec55 1271 */
8f8d37b2 1272int dev_pm_opp_of_add_table(struct device *dev)
129eec55 1273{
1840995c
VK
1274 struct device_node *opp_np;
1275 int ret;
27465902
VK
1276
1277 /*
1278 * OPPs have two version of bindings now. The older one is deprecated,
1279 * try for the new binding first.
1280 */
1840995c
VK
1281 opp_np = _of_get_opp_desc_node(dev);
1282 if (!opp_np) {
27465902
VK
1283 /*
1284 * Try old-deprecated bindings for backward compatibility with
1285 * older dtbs.
1286 */
f0489a5e 1287 return _of_add_opp_table_v1(dev);
27465902
VK
1288 }
1289
f0489a5e 1290 ret = _of_add_opp_table_v2(dev, opp_np);
1840995c
VK
1291 of_node_put(opp_np);
1292
1293 return ret;
27465902 1294}
8f8d37b2 1295EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
b496dfbc 1296#endif
This page took 0.306381 seconds and 5 git commands to generate.