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