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