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