Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/shli/md
[linux-2.6-block.git] / drivers / opp / of.c
CommitLineData
f47b72a1
VK
1/*
2 * Generic OPP OF helpers
3 *
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/cpu.h>
17#include <linux/errno.h>
18#include <linux/device.h>
9867999f 19#include <linux/of_device.h>
3ba98324 20#include <linux/pm_domain.h>
dfbe4678 21#include <linux/slab.h>
f47b72a1
VK
22#include <linux/export.h>
23
24#include "opp.h"
25
f06ed90e
VK
26/*
27 * Returns opp descriptor node for a device node, caller must
28 * do of_node_put().
29 */
30static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
31 int index)
32{
33 /* "operating-points-v2" can be an array for power domain providers */
34 return of_parse_phandle(np, "operating-points-v2", index);
35}
36
37/* Returns opp descriptor node for a device, caller must do of_node_put() */
38struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
39{
40 return _opp_of_get_opp_desc_node(dev->of_node, 0);
41}
42EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
43
283d55e6 44struct opp_table *_managed_opp(struct device *dev, int index)
f47b72a1 45{
b83c1899 46 struct opp_table *opp_table, *managed_table = NULL;
283d55e6 47 struct device_node *np;
b83c1899 48
283d55e6
VK
49 np = _opp_of_get_opp_desc_node(dev->of_node, index);
50 if (!np)
51 return NULL;
f47b72a1 52
052c6f19 53 list_for_each_entry(opp_table, &opp_tables, node) {
f47b72a1
VK
54 if (opp_table->np == np) {
55 /*
56 * Multiple devices can point to the same OPP table and
57 * so will have same node-pointer, np.
58 *
59 * But the OPPs will be considered as shared only if the
60 * OPP table contains a "opp-shared" property.
61 */
b83c1899
VK
62 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
63 _get_opp_table_kref(opp_table);
64 managed_table = opp_table;
65 }
79ee2e8f 66
b83c1899 67 break;
f47b72a1
VK
68 }
69 }
70
283d55e6 71 of_node_put(np);
b83c1899
VK
72
73 return managed_table;
f47b72a1
VK
74}
75
eb7c8743
VK
76void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
77 int index)
f47b72a1 78{
f06ed90e
VK
79 struct device_node *np, *opp_np;
80 u32 val;
f47b72a1
VK
81
82 /*
83 * Only required for backward compatibility with v1 bindings, but isn't
84 * harmful for other cases. And so we do it unconditionally.
85 */
86 np = of_node_get(dev->of_node);
f06ed90e
VK
87 if (!np)
88 return;
89
90 if (!of_property_read_u32(np, "clock-latency", &val))
91 opp_table->clock_latency_ns_max = val;
92 of_property_read_u32(np, "voltage-tolerance",
93 &opp_table->voltage_tolerance_v1);
94
95 /* Get OPP table node */
96 opp_np = _opp_of_get_opp_desc_node(np, index);
97 of_node_put(np);
98
99 if (!opp_np)
100 return;
101
102 if (of_property_read_bool(opp_np, "opp-shared"))
103 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
104 else
105 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
106
107 opp_table->np = opp_np;
108
109 of_node_put(opp_np);
f47b72a1
VK
110}
111
112static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
113 struct device_node *np)
114{
115 unsigned int count = opp_table->supported_hw_count;
116 u32 version;
117 int ret;
118
a4ee4545
DG
119 if (!opp_table->supported_hw) {
120 /*
121 * In the case that no supported_hw has been set by the
122 * platform but there is an opp-supported-hw value set for
123 * an OPP then the OPP should not be enabled as there is
124 * no way to see if the hardware supports it.
125 */
126 if (of_find_property(np, "opp-supported-hw", NULL))
127 return false;
128 else
129 return true;
130 }
f47b72a1
VK
131
132 while (count--) {
133 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
134 &version);
135 if (ret) {
136 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
137 __func__, count, ret);
138 return false;
139 }
140
141 /* Both of these are bitwise masks of the versions */
142 if (!(version & opp_table->supported_hw[count]))
143 return false;
144 }
145
146 return true;
147}
148
f47b72a1
VK
149static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
150 struct opp_table *opp_table)
151{
dfbe4678
VK
152 u32 *microvolt, *microamp = NULL;
153 int supplies, vcount, icount, ret, i, j;
f47b72a1
VK
154 struct property *prop = NULL;
155 char name[NAME_MAX];
156
dfbe4678
VK
157 supplies = opp_table->regulator_count ? opp_table->regulator_count : 1;
158
f47b72a1
VK
159 /* Search for "opp-microvolt-<name>" */
160 if (opp_table->prop_name) {
161 snprintf(name, sizeof(name), "opp-microvolt-%s",
162 opp_table->prop_name);
163 prop = of_find_property(opp->np, name, NULL);
164 }
165
166 if (!prop) {
167 /* Search for "opp-microvolt" */
168 sprintf(name, "opp-microvolt");
169 prop = of_find_property(opp->np, name, NULL);
170
171 /* Missing property isn't a problem, but an invalid entry is */
688a48b0
VK
172 if (!prop) {
173 if (!opp_table->regulator_count)
174 return 0;
175
176 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
177 __func__);
178 return -EINVAL;
179 }
f47b72a1
VK
180 }
181
dfbe4678
VK
182 vcount = of_property_count_u32_elems(opp->np, name);
183 if (vcount < 0) {
f47b72a1 184 dev_err(dev, "%s: Invalid %s property (%d)\n",
dfbe4678
VK
185 __func__, name, vcount);
186 return vcount;
f47b72a1
VK
187 }
188
dfbe4678
VK
189 /* There can be one or three elements per supply */
190 if (vcount != supplies && vcount != supplies * 3) {
191 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
192 __func__, name, vcount, supplies);
f47b72a1
VK
193 return -EINVAL;
194 }
195
dfbe4678
VK
196 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
197 if (!microvolt)
198 return -ENOMEM;
199
200 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
f47b72a1
VK
201 if (ret) {
202 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
dfbe4678
VK
203 ret = -EINVAL;
204 goto free_microvolt;
f47b72a1
VK
205 }
206
207 /* Search for "opp-microamp-<name>" */
208 prop = NULL;
209 if (opp_table->prop_name) {
210 snprintf(name, sizeof(name), "opp-microamp-%s",
211 opp_table->prop_name);
212 prop = of_find_property(opp->np, name, NULL);
213 }
214
215 if (!prop) {
216 /* Search for "opp-microamp" */
217 sprintf(name, "opp-microamp");
218 prop = of_find_property(opp->np, name, NULL);
219 }
220
dfbe4678
VK
221 if (prop) {
222 icount = of_property_count_u32_elems(opp->np, name);
223 if (icount < 0) {
224 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
225 name, icount);
226 ret = icount;
227 goto free_microvolt;
228 }
f47b72a1 229
dfbe4678
VK
230 if (icount != supplies) {
231 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
232 __func__, name, icount, supplies);
233 ret = -EINVAL;
234 goto free_microvolt;
235 }
236
237 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
238 if (!microamp) {
239 ret = -EINVAL;
240 goto free_microvolt;
241 }
242
243 ret = of_property_read_u32_array(opp->np, name, microamp,
244 icount);
245 if (ret) {
246 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
247 name, ret);
248 ret = -EINVAL;
249 goto free_microamp;
250 }
251 }
252
253 for (i = 0, j = 0; i < supplies; i++) {
254 opp->supplies[i].u_volt = microvolt[j++];
255
256 if (vcount == supplies) {
257 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
258 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
259 } else {
260 opp->supplies[i].u_volt_min = microvolt[j++];
261 opp->supplies[i].u_volt_max = microvolt[j++];
262 }
263
264 if (microamp)
265 opp->supplies[i].u_amp = microamp[i];
266 }
267
268free_microamp:
269 kfree(microamp);
270free_microvolt:
271 kfree(microvolt);
272
273 return ret;
f47b72a1
VK
274}
275
276/**
277 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
278 * entries
279 * @dev: device pointer used to lookup OPP table.
280 *
281 * Free OPPs created using static entries present in DT.
f47b72a1
VK
282 */
283void dev_pm_opp_of_remove_table(struct device *dev)
284{
2a4eb735 285 _dev_pm_opp_find_and_remove_table(dev);
f47b72a1
VK
286}
287EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
288
f47b72a1
VK
289/**
290 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
8cd2f6e8 291 * @opp_table: OPP table
f47b72a1
VK
292 * @dev: device for which we do this operation
293 * @np: device node
294 *
295 * This function adds an opp definition to the opp table and returns status. The
296 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
297 * removed by dev_pm_opp_remove.
298 *
f47b72a1 299 * Return:
deac8703
DG
300 * Valid OPP pointer:
301 * On success
302 * NULL:
f47b72a1 303 * Duplicate OPPs (both freq and volt are same) and opp->available
deac8703
DG
304 * OR if the OPP is not supported by hardware.
305 * ERR_PTR(-EEXIST):
306 * Freq are same and volt are different OR
f47b72a1 307 * Duplicate OPPs (both freq and volt are same) and !opp->available
deac8703
DG
308 * ERR_PTR(-ENOMEM):
309 * Memory allocation failure
310 * ERR_PTR(-EINVAL):
311 * Failed parsing the OPP node
f47b72a1 312 */
deac8703
DG
313static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
314 struct device *dev, struct device_node *np)
f47b72a1 315{
f47b72a1 316 struct dev_pm_opp *new_opp;
6a89e012 317 u64 rate = 0;
f47b72a1
VK
318 u32 val;
319 int ret;
a1e8c136 320 bool rate_not_available = false;
f47b72a1 321
8cd2f6e8
VK
322 new_opp = _opp_allocate(opp_table);
323 if (!new_opp)
deac8703 324 return ERR_PTR(-ENOMEM);
f47b72a1
VK
325
326 ret = of_property_read_u64(np, "opp-hz", &rate);
327 if (ret < 0) {
a1e8c136
VK
328 /* "opp-hz" is optional for devices like power domains. */
329 if (!of_find_property(dev->of_node, "#power-domain-cells",
330 NULL)) {
331 dev_err(dev, "%s: opp-hz not found\n", __func__);
332 goto free_opp;
333 }
334
335 rate_not_available = true;
336 } else {
337 /*
338 * Rate is defined as an unsigned long in clk API, and so
339 * casting explicitly to its type. Must be fixed once rate is 64
340 * bit guaranteed in clk API.
341 */
342 new_opp->rate = (unsigned long)rate;
f47b72a1
VK
343 }
344
345 /* Check if the OPP supports hardware's hierarchy of versions or not */
346 if (!_opp_is_supported(dev, opp_table, np)) {
347 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
348 goto free_opp;
349 }
350
f47b72a1
VK
351 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
352
353 new_opp->np = np;
354 new_opp->dynamic = false;
355 new_opp->available = true;
356
357 if (!of_property_read_u32(np, "clock-latency-ns", &val))
358 new_opp->clock_latency_ns = val;
359
3ba98324
VK
360 new_opp->pstate = of_genpd_opp_to_performance_state(dev, np);
361
f47b72a1
VK
362 ret = opp_parse_supplies(new_opp, dev, opp_table);
363 if (ret)
364 goto free_opp;
365
a1e8c136 366 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
7f8538eb
VK
367 if (ret) {
368 /* Don't return error for duplicate OPPs */
369 if (ret == -EBUSY)
370 ret = 0;
f47b72a1 371 goto free_opp;
7f8538eb 372 }
f47b72a1
VK
373
374 /* OPP to select on device suspend */
375 if (of_property_read_bool(np, "opp-suspend")) {
376 if (opp_table->suspend_opp) {
377 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
378 __func__, opp_table->suspend_opp->rate,
379 new_opp->rate);
380 } else {
381 new_opp->suspend = true;
382 opp_table->suspend_opp = new_opp;
383 }
384 }
385
386 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
387 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
388
f47b72a1 389 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
0f0fe7e0 390 __func__, new_opp->turbo, new_opp->rate,
dfbe4678
VK
391 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
392 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
f47b72a1
VK
393
394 /*
395 * Notify the changes in the availability of the operable
396 * frequency/voltage list.
397 */
052c6f19 398 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
deac8703 399 return new_opp;
f47b72a1
VK
400
401free_opp:
8cd2f6e8
VK
402 _opp_free(new_opp);
403
deac8703 404 return ERR_PTR(ret);
f47b72a1
VK
405}
406
407/* Initializes OPP tables based on new bindings */
5ed4cecd 408static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
f47b72a1
VK
409{
410 struct device_node *np;
283d55e6 411 int ret, count = 0, pstate_count = 0;
3ba98324 412 struct dev_pm_opp *opp;
f47b72a1 413
283d55e6
VK
414 /* OPP table is already initialized for the device */
415 if (opp_table->parsed_static_opps) {
416 kref_get(&opp_table->list_kref);
417 return 0;
418 }
419
d0e8ae6c
VK
420 kref_init(&opp_table->list_kref);
421
f47b72a1 422 /* We have opp-table node now, iterate over it and add OPPs */
5ed4cecd 423 for_each_available_child_of_node(opp_table->np, np) {
deac8703
DG
424 opp = _opp_add_static_v2(opp_table, dev, np);
425 if (IS_ERR(opp)) {
426 ret = PTR_ERR(opp);
f47b72a1
VK
427 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
428 ret);
7978db34 429 of_node_put(np);
cdd6ed90 430 goto put_list_kref;
deac8703
DG
431 } else if (opp) {
432 count++;
f47b72a1
VK
433 }
434 }
435
436 /* There should be one of more OPP defined */
8cd2f6e8
VK
437 if (WARN_ON(!count)) {
438 ret = -ENOENT;
cdd6ed90 439 goto put_list_kref;
f47b72a1
VK
440 }
441
3ba98324
VK
442 list_for_each_entry(opp, &opp_table->opp_list, node)
443 pstate_count += !!opp->pstate;
444
445 /* Either all or none of the nodes shall have performance state set */
446 if (pstate_count && pstate_count != count) {
447 dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
448 count, pstate_count);
449 ret = -ENOENT;
cdd6ed90 450 goto put_list_kref;
3ba98324
VK
451 }
452
453 if (pstate_count)
454 opp_table->genpd_performance_state = true;
455
f06ed90e 456 opp_table->parsed_static_opps = true;
f47b72a1 457
cdd6ed90
VK
458 return 0;
459
460put_list_kref:
461 _put_opp_list_kref(opp_table);
f47b72a1
VK
462
463 return ret;
464}
465
466/* Initializes OPP tables based on old-deprecated bindings */
5ed4cecd 467static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
f47b72a1
VK
468{
469 const struct property *prop;
470 const __be32 *val;
8cd2f6e8 471 int nr, ret = 0;
f47b72a1
VK
472
473 prop = of_find_property(dev->of_node, "operating-points", NULL);
474 if (!prop)
475 return -ENODEV;
476 if (!prop->value)
477 return -ENODATA;
478
479 /*
480 * Each OPP is a set of tuples consisting of frequency and
481 * voltage like <freq-kHz vol-uV>.
482 */
483 nr = prop->length / sizeof(u32);
484 if (nr % 2) {
485 dev_err(dev, "%s: Invalid OPP table\n", __func__);
486 return -EINVAL;
487 }
488
d0e8ae6c
VK
489 kref_init(&opp_table->list_kref);
490
f47b72a1
VK
491 val = prop->value;
492 while (nr) {
493 unsigned long freq = be32_to_cpup(val++) * 1000;
494 unsigned long volt = be32_to_cpup(val++);
495
8cd2f6e8 496 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
04a86a84
VK
497 if (ret) {
498 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
499 __func__, freq, ret);
cdd6ed90 500 _put_opp_list_kref(opp_table);
cdd6ed90 501 return ret;
04a86a84 502 }
f47b72a1
VK
503 nr -= 2;
504 }
505
8cd2f6e8 506 return ret;
f47b72a1
VK
507}
508
509/**
510 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
511 * @dev: device pointer used to lookup OPP table.
512 *
513 * Register the initial OPP table with the OPP library for given device.
514 *
f47b72a1
VK
515 * Return:
516 * 0 On success OR
517 * Duplicate OPPs (both freq and volt are same) and opp->available
518 * -EEXIST Freq are same and volt are different OR
519 * Duplicate OPPs (both freq and volt are same) and !opp->available
520 * -ENOMEM Memory allocation failure
521 * -ENODEV when 'operating-points' property is not found or is invalid data
522 * in device node.
523 * -ENODATA when empty 'operating-points' property is found
524 * -EINVAL when invalid entries are found in opp-v2 table
525 */
526int dev_pm_opp_of_add_table(struct device *dev)
527{
5ed4cecd 528 struct opp_table *opp_table;
f47b72a1
VK
529 int ret;
530
5ed4cecd
VK
531 opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0);
532 if (!opp_table)
533 return -ENOMEM;
534
f47b72a1 535 /*
5ed4cecd
VK
536 * OPPs have two version of bindings now. Also try the old (v1)
537 * bindings for backward compatibility with older dtbs.
f47b72a1 538 */
5ed4cecd
VK
539 if (opp_table->np)
540 ret = _of_add_opp_table_v2(dev, opp_table);
541 else
542 ret = _of_add_opp_table_v1(dev, opp_table);
f47b72a1 543
5ed4cecd
VK
544 if (ret)
545 dev_pm_opp_put_opp_table(opp_table);
f47b72a1
VK
546
547 return ret;
548}
549EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
550
fa9b274f
VK
551/**
552 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
553 * @dev: device pointer used to lookup OPP table.
554 * @index: Index number.
555 *
556 * Register the initial OPP table with the OPP library for given device only
557 * using the "operating-points-v2" property.
558 *
559 * Return:
560 * 0 On success OR
561 * Duplicate OPPs (both freq and volt are same) and opp->available
562 * -EEXIST Freq are same and volt are different OR
563 * Duplicate OPPs (both freq and volt are same) and !opp->available
564 * -ENOMEM Memory allocation failure
565 * -ENODEV when 'operating-points' property is not found or is invalid data
566 * in device node.
567 * -ENODATA when empty 'operating-points' property is found
568 * -EINVAL when invalid entries are found in opp-v2 table
569 */
570int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
571{
5ed4cecd 572 struct opp_table *opp_table;
8a352fd8 573 int ret, count;
fa9b274f 574
5ed4cecd 575 if (index) {
8a352fd8
VK
576 /*
577 * If only one phandle is present, then the same OPP table
578 * applies for all index requests.
579 */
580 count = of_count_phandle_with_args(dev->of_node,
581 "operating-points-v2", NULL);
5ed4cecd
VK
582 if (count != 1)
583 return -ENODEV;
8a352fd8 584
5ed4cecd 585 index = 0;
8a352fd8 586 }
fa9b274f 587
5ed4cecd
VK
588 opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
589 if (!opp_table)
590 return -ENOMEM;
591
592 ret = _of_add_opp_table_v2(dev, opp_table);
593 if (ret)
594 dev_pm_opp_put_opp_table(opp_table);
fa9b274f
VK
595
596 return ret;
597}
598EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
599
f47b72a1
VK
600/* CPU device specific helpers */
601
602/**
603 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
604 * @cpumask: cpumask for which OPP table needs to be removed
605 *
606 * This removes the OPP tables for CPUs present in the @cpumask.
607 * This should be used only to remove static entries created from DT.
f47b72a1
VK
608 */
609void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
610{
2a4eb735 611 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
f47b72a1
VK
612}
613EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
614
615/**
616 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
617 * @cpumask: cpumask for which OPP table needs to be added.
618 *
619 * This adds the OPP tables for CPUs present in the @cpumask.
f47b72a1
VK
620 */
621int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
622{
623 struct device *cpu_dev;
50b6b87c 624 int cpu, ret;
f47b72a1 625
50b6b87c
VK
626 if (WARN_ON(cpumask_empty(cpumask)))
627 return -ENODEV;
f47b72a1
VK
628
629 for_each_cpu(cpu, cpumask) {
630 cpu_dev = get_cpu_device(cpu);
631 if (!cpu_dev) {
632 pr_err("%s: failed to get cpu%d device\n", __func__,
633 cpu);
50b6b87c
VK
634 ret = -ENODEV;
635 goto remove_table;
f47b72a1
VK
636 }
637
638 ret = dev_pm_opp_of_add_table(cpu_dev);
639 if (ret) {
5b60697c
VK
640 /*
641 * OPP may get registered dynamically, don't print error
642 * message here.
643 */
644 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
645 __func__, cpu, ret);
f47b72a1 646
50b6b87c 647 goto remove_table;
f47b72a1
VK
648 }
649 }
650
50b6b87c
VK
651 return 0;
652
653remove_table:
654 /* Free all other OPPs */
655 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
656
f47b72a1
VK
657 return ret;
658}
659EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
660
661/*
662 * Works only for OPP v2 bindings.
663 *
664 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
665 */
666/**
667 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
668 * @cpu_dev using operating-points-v2
669 * bindings.
670 *
671 * @cpu_dev: CPU device for which we do this operation
672 * @cpumask: cpumask to update with information of sharing CPUs
673 *
674 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
675 *
676 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
f47b72a1
VK
677 */
678int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
679 struct cpumask *cpumask)
680{
76279291 681 struct device_node *np, *tmp_np, *cpu_np;
f47b72a1
VK
682 int cpu, ret = 0;
683
684 /* Get OPP descriptor node */
0764c604 685 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
f47b72a1 686 if (!np) {
349aa92e 687 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
f47b72a1
VK
688 return -ENOENT;
689 }
690
691 cpumask_set_cpu(cpu_dev->id, cpumask);
692
693 /* OPPs are shared ? */
694 if (!of_property_read_bool(np, "opp-shared"))
695 goto put_cpu_node;
696
697 for_each_possible_cpu(cpu) {
698 if (cpu == cpu_dev->id)
699 continue;
700
9867999f 701 cpu_np = of_cpu_device_node_get(cpu);
76279291
WR
702 if (!cpu_np) {
703 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
f47b72a1 704 __func__, cpu);
76279291 705 ret = -ENOENT;
f47b72a1
VK
706 goto put_cpu_node;
707 }
708
709 /* Get OPP descriptor node */
fa9b274f 710 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
9867999f 711 of_node_put(cpu_np);
f47b72a1 712 if (!tmp_np) {
76279291 713 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
f47b72a1
VK
714 ret = -ENOENT;
715 goto put_cpu_node;
716 }
717
718 /* CPUs are sharing opp node */
719 if (np == tmp_np)
720 cpumask_set_cpu(cpu, cpumask);
721
722 of_node_put(tmp_np);
723 }
724
725put_cpu_node:
726 of_node_put(np);
727 return ret;
728}
729EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
a88bd2a5
VK
730
731/**
732 * of_dev_pm_opp_find_required_opp() - Search for required OPP.
733 * @dev: The device whose OPP node is referenced by the 'np' DT node.
734 * @np: Node that contains the "required-opps" property.
735 *
736 * Returns the OPP of the device 'dev', whose phandle is present in the "np"
737 * node. Although the "required-opps" property supports having multiple
738 * phandles, this helper routine only parses the very first phandle in the list.
739 *
740 * Return: Matching opp, else returns ERR_PTR in case of error and should be
741 * handled using IS_ERR.
742 *
743 * The callers are required to call dev_pm_opp_put() for the returned OPP after
744 * use.
745 */
746struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev,
747 struct device_node *np)
748{
749 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV);
750 struct device_node *required_np;
751 struct opp_table *opp_table;
752
753 opp_table = _find_opp_table(dev);
754 if (IS_ERR(opp_table))
755 return ERR_CAST(opp_table);
756
757 required_np = of_parse_phandle(np, "required-opps", 0);
758 if (unlikely(!required_np)) {
759 dev_err(dev, "Unable to parse required-opps\n");
760 goto put_opp_table;
761 }
762
763 mutex_lock(&opp_table->lock);
764
765 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
766 if (temp_opp->available && temp_opp->np == required_np) {
767 opp = temp_opp;
768
769 /* Increment the reference count of OPP */
770 dev_pm_opp_get(opp);
771 break;
772 }
773 }
774
775 mutex_unlock(&opp_table->lock);
776
777 of_node_put(required_np);
778put_opp_table:
779 dev_pm_opp_put_opp_table(opp_table);
780
781 return opp;
782}
783EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp);
e2f4b5f8
VK
784
785/**
786 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
787 * @opp: opp for which DT node has to be returned for
788 *
789 * Return: DT node corresponding to the opp, else 0 on success.
790 *
791 * The caller needs to put the node with of_node_put() after using it.
792 */
793struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
794{
795 if (IS_ERR_OR_NULL(opp)) {
796 pr_err("%s: Invalid parameters\n", __func__);
797 return NULL;
798 }
799
800 return of_node_get(opp->np);
801}
802EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);