GNU Linux-libre 5.4.241-gnu1
[releases.git] / drivers / opp / of.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic OPP OF helpers
4  *
5  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
6  *      Nishanth Menon
7  *      Romit Dasgupta
8  *      Kevin Hilman
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/cpu.h>
14 #include <linux/errno.h>
15 #include <linux/device.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_domain.h>
18 #include <linux/slab.h>
19 #include <linux/export.h>
20 #include <linux/energy_model.h>
21
22 #include "opp.h"
23
24 /*
25  * Returns opp descriptor node for a device node, caller must
26  * do of_node_put().
27  */
28 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
29                                                      int index)
30 {
31         /* "operating-points-v2" can be an array for power domain providers */
32         return of_parse_phandle(np, "operating-points-v2", index);
33 }
34
35 /* Returns opp descriptor node for a device, caller must do of_node_put() */
36 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
37 {
38         return _opp_of_get_opp_desc_node(dev->of_node, 0);
39 }
40 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
41
42 struct opp_table *_managed_opp(struct device *dev, int index)
43 {
44         struct opp_table *opp_table, *managed_table = NULL;
45         struct device_node *np;
46
47         np = _opp_of_get_opp_desc_node(dev->of_node, index);
48         if (!np)
49                 return NULL;
50
51         list_for_each_entry(opp_table, &opp_tables, node) {
52                 if (opp_table->np == np) {
53                         /*
54                          * Multiple devices can point to the same OPP table and
55                          * so will have same node-pointer, np.
56                          *
57                          * But the OPPs will be considered as shared only if the
58                          * OPP table contains a "opp-shared" property.
59                          */
60                         if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
61                                 _get_opp_table_kref(opp_table);
62                                 managed_table = opp_table;
63                         }
64
65                         break;
66                 }
67         }
68
69         of_node_put(np);
70
71         return managed_table;
72 }
73
74 /* The caller must call dev_pm_opp_put() after the OPP is used */
75 static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,
76                                           struct device_node *opp_np)
77 {
78         struct dev_pm_opp *opp;
79
80         mutex_lock(&opp_table->lock);
81
82         list_for_each_entry(opp, &opp_table->opp_list, node) {
83                 if (opp->np == opp_np) {
84                         dev_pm_opp_get(opp);
85                         mutex_unlock(&opp_table->lock);
86                         return opp;
87                 }
88         }
89
90         mutex_unlock(&opp_table->lock);
91
92         return NULL;
93 }
94
95 static struct device_node *of_parse_required_opp(struct device_node *np,
96                                                  int index)
97 {
98         return of_parse_phandle(np, "required-opps", index);
99 }
100
101 /* The caller must call dev_pm_opp_put_opp_table() after the table is used */
102 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
103 {
104         struct opp_table *opp_table;
105         struct device_node *opp_table_np;
106
107         lockdep_assert_held(&opp_table_lock);
108
109         opp_table_np = of_get_parent(opp_np);
110         if (!opp_table_np)
111                 goto err;
112
113         /* It is safe to put the node now as all we need now is its address */
114         of_node_put(opp_table_np);
115
116         list_for_each_entry(opp_table, &opp_tables, node) {
117                 if (opp_table_np == opp_table->np) {
118                         _get_opp_table_kref(opp_table);
119                         return opp_table;
120                 }
121         }
122
123 err:
124         return ERR_PTR(-ENODEV);
125 }
126
127 /* Free resources previously acquired by _opp_table_alloc_required_tables() */
128 static void _opp_table_free_required_tables(struct opp_table *opp_table)
129 {
130         struct opp_table **required_opp_tables = opp_table->required_opp_tables;
131         int i;
132
133         if (!required_opp_tables)
134                 return;
135
136         for (i = 0; i < opp_table->required_opp_count; i++) {
137                 if (IS_ERR_OR_NULL(required_opp_tables[i]))
138                         break;
139
140                 dev_pm_opp_put_opp_table(required_opp_tables[i]);
141         }
142
143         kfree(required_opp_tables);
144
145         opp_table->required_opp_count = 0;
146         opp_table->required_opp_tables = NULL;
147 }
148
149 /*
150  * Populate all devices and opp tables which are part of "required-opps" list.
151  * Checking only the first OPP node should be enough.
152  */
153 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
154                                              struct device *dev,
155                                              struct device_node *opp_np)
156 {
157         struct opp_table **required_opp_tables;
158         struct device_node *required_np, *np;
159         int count, i;
160
161         /* Traversing the first OPP node is all we need */
162         np = of_get_next_available_child(opp_np, NULL);
163         if (!np) {
164                 dev_err(dev, "Empty OPP table\n");
165                 return;
166         }
167
168         count = of_count_phandle_with_args(np, "required-opps", NULL);
169         if (!count)
170                 goto put_np;
171
172         required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
173                                       GFP_KERNEL);
174         if (!required_opp_tables)
175                 goto put_np;
176
177         opp_table->required_opp_tables = required_opp_tables;
178         opp_table->required_opp_count = count;
179
180         for (i = 0; i < count; i++) {
181                 required_np = of_parse_required_opp(np, i);
182                 if (!required_np)
183                         goto free_required_tables;
184
185                 required_opp_tables[i] = _find_table_of_opp_np(required_np);
186                 of_node_put(required_np);
187
188                 if (IS_ERR(required_opp_tables[i]))
189                         goto free_required_tables;
190
191                 /*
192                  * We only support genpd's OPPs in the "required-opps" for now,
193                  * as we don't know how much about other cases. Error out if the
194                  * required OPP doesn't belong to a genpd.
195                  */
196                 if (!required_opp_tables[i]->is_genpd) {
197                         dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",
198                                 required_np);
199                         goto free_required_tables;
200                 }
201         }
202
203         goto put_np;
204
205 free_required_tables:
206         _opp_table_free_required_tables(opp_table);
207 put_np:
208         of_node_put(np);
209 }
210
211 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
212                         int index)
213 {
214         struct device_node *np, *opp_np;
215         u32 val;
216
217         /*
218          * Only required for backward compatibility with v1 bindings, but isn't
219          * harmful for other cases. And so we do it unconditionally.
220          */
221         np = of_node_get(dev->of_node);
222         if (!np)
223                 return;
224
225         if (!of_property_read_u32(np, "clock-latency", &val))
226                 opp_table->clock_latency_ns_max = val;
227         of_property_read_u32(np, "voltage-tolerance",
228                              &opp_table->voltage_tolerance_v1);
229
230         if (of_find_property(np, "#power-domain-cells", NULL))
231                 opp_table->is_genpd = true;
232
233         /* Get OPP table node */
234         opp_np = _opp_of_get_opp_desc_node(np, index);
235         of_node_put(np);
236
237         if (!opp_np)
238                 return;
239
240         if (of_property_read_bool(opp_np, "opp-shared"))
241                 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
242         else
243                 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
244
245         opp_table->np = opp_np;
246
247         _opp_table_alloc_required_tables(opp_table, dev, opp_np);
248         of_node_put(opp_np);
249 }
250
251 void _of_clear_opp_table(struct opp_table *opp_table)
252 {
253         _opp_table_free_required_tables(opp_table);
254 }
255
256 /*
257  * Release all resources previously acquired with a call to
258  * _of_opp_alloc_required_opps().
259  */
260 void _of_opp_free_required_opps(struct opp_table *opp_table,
261                                 struct dev_pm_opp *opp)
262 {
263         struct dev_pm_opp **required_opps = opp->required_opps;
264         int i;
265
266         if (!required_opps)
267                 return;
268
269         for (i = 0; i < opp_table->required_opp_count; i++) {
270                 if (!required_opps[i])
271                         break;
272
273                 /* Put the reference back */
274                 dev_pm_opp_put(required_opps[i]);
275         }
276
277         kfree(required_opps);
278         opp->required_opps = NULL;
279 }
280
281 /* Populate all required OPPs which are part of "required-opps" list */
282 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
283                                        struct dev_pm_opp *opp)
284 {
285         struct dev_pm_opp **required_opps;
286         struct opp_table *required_table;
287         struct device_node *np;
288         int i, ret, count = opp_table->required_opp_count;
289
290         if (!count)
291                 return 0;
292
293         required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
294         if (!required_opps)
295                 return -ENOMEM;
296
297         opp->required_opps = required_opps;
298
299         for (i = 0; i < count; i++) {
300                 required_table = opp_table->required_opp_tables[i];
301
302                 np = of_parse_required_opp(opp->np, i);
303                 if (unlikely(!np)) {
304                         ret = -ENODEV;
305                         goto free_required_opps;
306                 }
307
308                 required_opps[i] = _find_opp_of_np(required_table, np);
309                 of_node_put(np);
310
311                 if (!required_opps[i]) {
312                         pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
313                                __func__, opp->np, i);
314                         ret = -ENODEV;
315                         goto free_required_opps;
316                 }
317         }
318
319         return 0;
320
321 free_required_opps:
322         _of_opp_free_required_opps(opp_table, opp);
323
324         return ret;
325 }
326
327 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
328                               struct device_node *np)
329 {
330         unsigned int count = opp_table->supported_hw_count;
331         u32 version;
332         int ret;
333
334         if (!opp_table->supported_hw) {
335                 /*
336                  * In the case that no supported_hw has been set by the
337                  * platform but there is an opp-supported-hw value set for
338                  * an OPP then the OPP should not be enabled as there is
339                  * no way to see if the hardware supports it.
340                  */
341                 if (of_find_property(np, "opp-supported-hw", NULL))
342                         return false;
343                 else
344                         return true;
345         }
346
347         while (count--) {
348                 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
349                                                  &version);
350                 if (ret) {
351                         dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
352                                  __func__, count, ret);
353                         return false;
354                 }
355
356                 /* Both of these are bitwise masks of the versions */
357                 if (!(version & opp_table->supported_hw[count]))
358                         return false;
359         }
360
361         return true;
362 }
363
364 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
365                               struct opp_table *opp_table)
366 {
367         u32 *microvolt, *microamp = NULL;
368         int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;
369         struct property *prop = NULL;
370         char name[NAME_MAX];
371
372         /* Search for "opp-microvolt-<name>" */
373         if (opp_table->prop_name) {
374                 snprintf(name, sizeof(name), "opp-microvolt-%s",
375                          opp_table->prop_name);
376                 prop = of_find_property(opp->np, name, NULL);
377         }
378
379         if (!prop) {
380                 /* Search for "opp-microvolt" */
381                 sprintf(name, "opp-microvolt");
382                 prop = of_find_property(opp->np, name, NULL);
383
384                 /* Missing property isn't a problem, but an invalid entry is */
385                 if (!prop) {
386                         if (unlikely(supplies == -1)) {
387                                 /* Initialize regulator_count */
388                                 opp_table->regulator_count = 0;
389                                 return 0;
390                         }
391
392                         if (!supplies)
393                                 return 0;
394
395                         dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
396                                 __func__);
397                         return -EINVAL;
398                 }
399         }
400
401         if (unlikely(supplies == -1)) {
402                 /* Initialize regulator_count */
403                 supplies = opp_table->regulator_count = 1;
404         } else if (unlikely(!supplies)) {
405                 dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
406                 return -EINVAL;
407         }
408
409         vcount = of_property_count_u32_elems(opp->np, name);
410         if (vcount < 0) {
411                 dev_err(dev, "%s: Invalid %s property (%d)\n",
412                         __func__, name, vcount);
413                 return vcount;
414         }
415
416         /* There can be one or three elements per supply */
417         if (vcount != supplies && vcount != supplies * 3) {
418                 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
419                         __func__, name, vcount, supplies);
420                 return -EINVAL;
421         }
422
423         microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
424         if (!microvolt)
425                 return -ENOMEM;
426
427         ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
428         if (ret) {
429                 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
430                 ret = -EINVAL;
431                 goto free_microvolt;
432         }
433
434         /* Search for "opp-microamp-<name>" */
435         prop = NULL;
436         if (opp_table->prop_name) {
437                 snprintf(name, sizeof(name), "opp-microamp-%s",
438                          opp_table->prop_name);
439                 prop = of_find_property(opp->np, name, NULL);
440         }
441
442         if (!prop) {
443                 /* Search for "opp-microamp" */
444                 sprintf(name, "opp-microamp");
445                 prop = of_find_property(opp->np, name, NULL);
446         }
447
448         if (prop) {
449                 icount = of_property_count_u32_elems(opp->np, name);
450                 if (icount < 0) {
451                         dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
452                                 name, icount);
453                         ret = icount;
454                         goto free_microvolt;
455                 }
456
457                 if (icount != supplies) {
458                         dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
459                                 __func__, name, icount, supplies);
460                         ret = -EINVAL;
461                         goto free_microvolt;
462                 }
463
464                 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
465                 if (!microamp) {
466                         ret = -EINVAL;
467                         goto free_microvolt;
468                 }
469
470                 ret = of_property_read_u32_array(opp->np, name, microamp,
471                                                  icount);
472                 if (ret) {
473                         dev_err(dev, "%s: error parsing %s: %d\n", __func__,
474                                 name, ret);
475                         ret = -EINVAL;
476                         goto free_microamp;
477                 }
478         }
479
480         for (i = 0, j = 0; i < supplies; i++) {
481                 opp->supplies[i].u_volt = microvolt[j++];
482
483                 if (vcount == supplies) {
484                         opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
485                         opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
486                 } else {
487                         opp->supplies[i].u_volt_min = microvolt[j++];
488                         opp->supplies[i].u_volt_max = microvolt[j++];
489                 }
490
491                 if (microamp)
492                         opp->supplies[i].u_amp = microamp[i];
493         }
494
495 free_microamp:
496         kfree(microamp);
497 free_microvolt:
498         kfree(microvolt);
499
500         return ret;
501 }
502
503 /**
504  * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
505  *                                entries
506  * @dev:        device pointer used to lookup OPP table.
507  *
508  * Free OPPs created using static entries present in DT.
509  */
510 void dev_pm_opp_of_remove_table(struct device *dev)
511 {
512         _dev_pm_opp_find_and_remove_table(dev);
513 }
514 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
515
516 /**
517  * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
518  * @opp_table:  OPP table
519  * @dev:        device for which we do this operation
520  * @np:         device node
521  *
522  * This function adds an opp definition to the opp table and returns status. The
523  * opp can be controlled using dev_pm_opp_enable/disable functions and may be
524  * removed by dev_pm_opp_remove.
525  *
526  * Return:
527  * Valid OPP pointer:
528  *              On success
529  * NULL:
530  *              Duplicate OPPs (both freq and volt are same) and opp->available
531  *              OR if the OPP is not supported by hardware.
532  * ERR_PTR(-EEXIST):
533  *              Freq are same and volt are different OR
534  *              Duplicate OPPs (both freq and volt are same) and !opp->available
535  * ERR_PTR(-ENOMEM):
536  *              Memory allocation failure
537  * ERR_PTR(-EINVAL):
538  *              Failed parsing the OPP node
539  */
540 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
541                 struct device *dev, struct device_node *np)
542 {
543         struct dev_pm_opp *new_opp;
544         u64 rate = 0;
545         u32 val;
546         int ret;
547         bool rate_not_available = false;
548
549         new_opp = _opp_allocate(opp_table);
550         if (!new_opp)
551                 return ERR_PTR(-ENOMEM);
552
553         ret = of_property_read_u64(np, "opp-hz", &rate);
554         if (ret < 0) {
555                 /* "opp-hz" is optional for devices like power domains. */
556                 if (!opp_table->is_genpd) {
557                         dev_err(dev, "%s: opp-hz not found\n", __func__);
558                         goto free_opp;
559                 }
560
561                 rate_not_available = true;
562         } else {
563                 /*
564                  * Rate is defined as an unsigned long in clk API, and so
565                  * casting explicitly to its type. Must be fixed once rate is 64
566                  * bit guaranteed in clk API.
567                  */
568                 new_opp->rate = (unsigned long)rate;
569         }
570
571         of_property_read_u32(np, "opp-level", &new_opp->level);
572
573         /* Check if the OPP supports hardware's hierarchy of versions or not */
574         if (!_opp_is_supported(dev, opp_table, np)) {
575                 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
576                 goto free_opp;
577         }
578
579         new_opp->turbo = of_property_read_bool(np, "turbo-mode");
580
581         new_opp->np = np;
582         new_opp->dynamic = false;
583         new_opp->available = true;
584
585         ret = _of_opp_alloc_required_opps(opp_table, new_opp);
586         if (ret)
587                 goto free_opp;
588
589         if (!of_property_read_u32(np, "clock-latency-ns", &val))
590                 new_opp->clock_latency_ns = val;
591
592         ret = opp_parse_supplies(new_opp, dev, opp_table);
593         if (ret)
594                 goto free_required_opps;
595
596         if (opp_table->is_genpd)
597                 new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
598
599         ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
600         if (ret) {
601                 /* Don't return error for duplicate OPPs */
602                 if (ret == -EBUSY)
603                         ret = 0;
604                 goto free_required_opps;
605         }
606
607         /* OPP to select on device suspend */
608         if (of_property_read_bool(np, "opp-suspend")) {
609                 if (opp_table->suspend_opp) {
610                         /* Pick the OPP with higher rate as suspend OPP */
611                         if (new_opp->rate > opp_table->suspend_opp->rate) {
612                                 opp_table->suspend_opp->suspend = false;
613                                 new_opp->suspend = true;
614                                 opp_table->suspend_opp = new_opp;
615                         }
616                 } else {
617                         new_opp->suspend = true;
618                         opp_table->suspend_opp = new_opp;
619                 }
620         }
621
622         if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
623                 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
624
625         pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
626                  __func__, new_opp->turbo, new_opp->rate,
627                  new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
628                  new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
629
630         /*
631          * Notify the changes in the availability of the operable
632          * frequency/voltage list.
633          */
634         blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
635         return new_opp;
636
637 free_required_opps:
638         _of_opp_free_required_opps(opp_table, new_opp);
639 free_opp:
640         _opp_free(new_opp);
641
642         return ret ? ERR_PTR(ret) : NULL;
643 }
644
645 /* Initializes OPP tables based on new bindings */
646 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
647 {
648         struct device_node *np;
649         int ret, count = 0, pstate_count = 0;
650         struct dev_pm_opp *opp;
651
652         /* OPP table is already initialized for the device */
653         mutex_lock(&opp_table->lock);
654         if (opp_table->parsed_static_opps) {
655                 opp_table->parsed_static_opps++;
656                 mutex_unlock(&opp_table->lock);
657                 return 0;
658         }
659
660         opp_table->parsed_static_opps = 1;
661         mutex_unlock(&opp_table->lock);
662
663         /* We have opp-table node now, iterate over it and add OPPs */
664         for_each_available_child_of_node(opp_table->np, np) {
665                 opp = _opp_add_static_v2(opp_table, dev, np);
666                 if (IS_ERR(opp)) {
667                         ret = PTR_ERR(opp);
668                         dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
669                                 ret);
670                         of_node_put(np);
671                         goto remove_static_opp;
672                 } else if (opp) {
673                         count++;
674                 }
675         }
676
677         /* There should be one or more OPPs defined */
678         if (!count) {
679                 dev_err(dev, "%s: no supported OPPs", __func__);
680                 ret = -ENOENT;
681                 goto remove_static_opp;
682         }
683
684         list_for_each_entry(opp, &opp_table->opp_list, node)
685                 pstate_count += !!opp->pstate;
686
687         /* Either all or none of the nodes shall have performance state set */
688         if (pstate_count && pstate_count != count) {
689                 dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
690                         count, pstate_count);
691                 ret = -ENOENT;
692                 goto remove_static_opp;
693         }
694
695         if (pstate_count)
696                 opp_table->genpd_performance_state = true;
697
698         return 0;
699
700 remove_static_opp:
701         _opp_remove_all_static(opp_table);
702
703         return ret;
704 }
705
706 /* Initializes OPP tables based on old-deprecated bindings */
707 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
708 {
709         const struct property *prop;
710         const __be32 *val;
711         int nr, ret = 0;
712
713         prop = of_find_property(dev->of_node, "operating-points", NULL);
714         if (!prop)
715                 return -ENODEV;
716         if (!prop->value)
717                 return -ENODATA;
718
719         /*
720          * Each OPP is a set of tuples consisting of frequency and
721          * voltage like <freq-kHz vol-uV>.
722          */
723         nr = prop->length / sizeof(u32);
724         if (nr % 2) {
725                 dev_err(dev, "%s: Invalid OPP table\n", __func__);
726                 return -EINVAL;
727         }
728
729         mutex_lock(&opp_table->lock);
730         opp_table->parsed_static_opps = 1;
731         mutex_unlock(&opp_table->lock);
732
733         val = prop->value;
734         while (nr) {
735                 unsigned long freq = be32_to_cpup(val++) * 1000;
736                 unsigned long volt = be32_to_cpup(val++);
737
738                 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
739                 if (ret) {
740                         dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
741                                 __func__, freq, ret);
742                         _opp_remove_all_static(opp_table);
743                         return ret;
744                 }
745                 nr -= 2;
746         }
747
748         return ret;
749 }
750
751 /**
752  * dev_pm_opp_of_add_table() - Initialize opp table from device tree
753  * @dev:        device pointer used to lookup OPP table.
754  *
755  * Register the initial OPP table with the OPP library for given device.
756  *
757  * Return:
758  * 0            On success OR
759  *              Duplicate OPPs (both freq and volt are same) and opp->available
760  * -EEXIST      Freq are same and volt are different OR
761  *              Duplicate OPPs (both freq and volt are same) and !opp->available
762  * -ENOMEM      Memory allocation failure
763  * -ENODEV      when 'operating-points' property is not found or is invalid data
764  *              in device node.
765  * -ENODATA     when empty 'operating-points' property is found
766  * -EINVAL      when invalid entries are found in opp-v2 table
767  */
768 int dev_pm_opp_of_add_table(struct device *dev)
769 {
770         struct opp_table *opp_table;
771         int ret;
772
773         opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0);
774         if (!opp_table)
775                 return -ENOMEM;
776
777         /*
778          * OPPs have two version of bindings now. Also try the old (v1)
779          * bindings for backward compatibility with older dtbs.
780          */
781         if (opp_table->np)
782                 ret = _of_add_opp_table_v2(dev, opp_table);
783         else
784                 ret = _of_add_opp_table_v1(dev, opp_table);
785
786         if (ret)
787                 dev_pm_opp_put_opp_table(opp_table);
788
789         return ret;
790 }
791 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
792
793 /**
794  * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
795  * @dev:        device pointer used to lookup OPP table.
796  * @index:      Index number.
797  *
798  * Register the initial OPP table with the OPP library for given device only
799  * using the "operating-points-v2" property.
800  *
801  * Return:
802  * 0            On success OR
803  *              Duplicate OPPs (both freq and volt are same) and opp->available
804  * -EEXIST      Freq are same and volt are different OR
805  *              Duplicate OPPs (both freq and volt are same) and !opp->available
806  * -ENOMEM      Memory allocation failure
807  * -ENODEV      when 'operating-points' property is not found or is invalid data
808  *              in device node.
809  * -ENODATA     when empty 'operating-points' property is found
810  * -EINVAL      when invalid entries are found in opp-v2 table
811  */
812 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
813 {
814         struct opp_table *opp_table;
815         int ret, count;
816
817         if (index) {
818                 /*
819                  * If only one phandle is present, then the same OPP table
820                  * applies for all index requests.
821                  */
822                 count = of_count_phandle_with_args(dev->of_node,
823                                                    "operating-points-v2", NULL);
824                 if (count == 1)
825                         index = 0;
826         }
827
828         opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
829         if (!opp_table)
830                 return -ENOMEM;
831
832         ret = _of_add_opp_table_v2(dev, opp_table);
833         if (ret)
834                 dev_pm_opp_put_opp_table(opp_table);
835
836         return ret;
837 }
838 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
839
840 /* CPU device specific helpers */
841
842 /**
843  * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
844  * @cpumask:    cpumask for which OPP table needs to be removed
845  *
846  * This removes the OPP tables for CPUs present in the @cpumask.
847  * This should be used only to remove static entries created from DT.
848  */
849 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
850 {
851         _dev_pm_opp_cpumask_remove_table(cpumask, -1);
852 }
853 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
854
855 /**
856  * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
857  * @cpumask:    cpumask for which OPP table needs to be added.
858  *
859  * This adds the OPP tables for CPUs present in the @cpumask.
860  */
861 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
862 {
863         struct device *cpu_dev;
864         int cpu, ret;
865
866         if (WARN_ON(cpumask_empty(cpumask)))
867                 return -ENODEV;
868
869         for_each_cpu(cpu, cpumask) {
870                 cpu_dev = get_cpu_device(cpu);
871                 if (!cpu_dev) {
872                         pr_err("%s: failed to get cpu%d device\n", __func__,
873                                cpu);
874                         ret = -ENODEV;
875                         goto remove_table;
876                 }
877
878                 ret = dev_pm_opp_of_add_table(cpu_dev);
879                 if (ret) {
880                         /*
881                          * OPP may get registered dynamically, don't print error
882                          * message here.
883                          */
884                         pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
885                                  __func__, cpu, ret);
886
887                         goto remove_table;
888                 }
889         }
890
891         return 0;
892
893 remove_table:
894         /* Free all other OPPs */
895         _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
896
897         return ret;
898 }
899 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
900
901 /*
902  * Works only for OPP v2 bindings.
903  *
904  * Returns -ENOENT if operating-points-v2 bindings aren't supported.
905  */
906 /**
907  * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
908  *                                    @cpu_dev using operating-points-v2
909  *                                    bindings.
910  *
911  * @cpu_dev:    CPU device for which we do this operation
912  * @cpumask:    cpumask to update with information of sharing CPUs
913  *
914  * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
915  *
916  * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
917  */
918 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
919                                    struct cpumask *cpumask)
920 {
921         struct device_node *np, *tmp_np, *cpu_np;
922         int cpu, ret = 0;
923
924         /* Get OPP descriptor node */
925         np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
926         if (!np) {
927                 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
928                 return -ENOENT;
929         }
930
931         cpumask_set_cpu(cpu_dev->id, cpumask);
932
933         /* OPPs are shared ? */
934         if (!of_property_read_bool(np, "opp-shared"))
935                 goto put_cpu_node;
936
937         for_each_possible_cpu(cpu) {
938                 if (cpu == cpu_dev->id)
939                         continue;
940
941                 cpu_np = of_cpu_device_node_get(cpu);
942                 if (!cpu_np) {
943                         dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
944                                 __func__, cpu);
945                         ret = -ENOENT;
946                         goto put_cpu_node;
947                 }
948
949                 /* Get OPP descriptor node */
950                 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
951                 of_node_put(cpu_np);
952                 if (!tmp_np) {
953                         pr_err("%pOF: Couldn't find opp node\n", cpu_np);
954                         ret = -ENOENT;
955                         goto put_cpu_node;
956                 }
957
958                 /* CPUs are sharing opp node */
959                 if (np == tmp_np)
960                         cpumask_set_cpu(cpu, cpumask);
961
962                 of_node_put(tmp_np);
963         }
964
965 put_cpu_node:
966         of_node_put(np);
967         return ret;
968 }
969 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
970
971 /**
972  * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
973  * @np: Node that contains the "required-opps" property.
974  * @index: Index of the phandle to parse.
975  *
976  * Returns the performance state of the OPP pointed out by the "required-opps"
977  * property at @index in @np.
978  *
979  * Return: Zero or positive performance state on success, otherwise negative
980  * value on errors.
981  */
982 int of_get_required_opp_performance_state(struct device_node *np, int index)
983 {
984         struct dev_pm_opp *opp;
985         struct device_node *required_np;
986         struct opp_table *opp_table;
987         int pstate = -EINVAL;
988
989         required_np = of_parse_required_opp(np, index);
990         if (!required_np)
991                 return -ENODEV;
992
993         opp_table = _find_table_of_opp_np(required_np);
994         if (IS_ERR(opp_table)) {
995                 pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
996                        __func__, np, PTR_ERR(opp_table));
997                 goto put_required_np;
998         }
999
1000         opp = _find_opp_of_np(opp_table, required_np);
1001         if (opp) {
1002                 pstate = opp->pstate;
1003                 dev_pm_opp_put(opp);
1004         }
1005
1006         dev_pm_opp_put_opp_table(opp_table);
1007
1008 put_required_np:
1009         of_node_put(required_np);
1010
1011         return pstate;
1012 }
1013 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1014
1015 /**
1016  * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1017  * @opp:        opp for which DT node has to be returned for
1018  *
1019  * Return: DT node corresponding to the opp, else 0 on success.
1020  *
1021  * The caller needs to put the node with of_node_put() after using it.
1022  */
1023 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1024 {
1025         if (IS_ERR_OR_NULL(opp)) {
1026                 pr_err("%s: Invalid parameters\n", __func__);
1027                 return NULL;
1028         }
1029
1030         return of_node_get(opp->np);
1031 }
1032 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1033
1034 /*
1035  * Callback function provided to the Energy Model framework upon registration.
1036  * This computes the power estimated by @CPU at @kHz if it is the frequency
1037  * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
1038  * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1039  * frequency and @mW to the associated power. The power is estimated as
1040  * P = C * V^2 * f with C being the CPU's capacitance and V and f respectively
1041  * the voltage and frequency of the OPP.
1042  *
1043  * Returns -ENODEV if the CPU device cannot be found, -EINVAL if the power
1044  * calculation failed because of missing parameters, 0 otherwise.
1045  */
1046 static int __maybe_unused _get_cpu_power(unsigned long *mW, unsigned long *kHz,
1047                                          int cpu)
1048 {
1049         struct device *cpu_dev;
1050         struct dev_pm_opp *opp;
1051         struct device_node *np;
1052         unsigned long mV, Hz;
1053         u32 cap;
1054         u64 tmp;
1055         int ret;
1056
1057         cpu_dev = get_cpu_device(cpu);
1058         if (!cpu_dev)
1059                 return -ENODEV;
1060
1061         np = of_node_get(cpu_dev->of_node);
1062         if (!np)
1063                 return -EINVAL;
1064
1065         ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1066         of_node_put(np);
1067         if (ret)
1068                 return -EINVAL;
1069
1070         Hz = *kHz * 1000;
1071         opp = dev_pm_opp_find_freq_ceil(cpu_dev, &Hz);
1072         if (IS_ERR(opp))
1073                 return -EINVAL;
1074
1075         mV = dev_pm_opp_get_voltage(opp) / 1000;
1076         dev_pm_opp_put(opp);
1077         if (!mV)
1078                 return -EINVAL;
1079
1080         tmp = (u64)cap * mV * mV * (Hz / 1000000);
1081         do_div(tmp, 1000000000);
1082
1083         *mW = (unsigned long)tmp;
1084         *kHz = Hz / 1000;
1085
1086         return 0;
1087 }
1088
1089 /**
1090  * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1091  * @cpus        : CPUs for which an Energy Model has to be registered
1092  *
1093  * This checks whether the "dynamic-power-coefficient" devicetree property has
1094  * been specified, and tries to register an Energy Model with it if it has.
1095  */
1096 void dev_pm_opp_of_register_em(struct cpumask *cpus)
1097 {
1098         struct em_data_callback em_cb = EM_DATA_CB(_get_cpu_power);
1099         int ret, nr_opp, cpu = cpumask_first(cpus);
1100         struct device *cpu_dev;
1101         struct device_node *np;
1102         u32 cap;
1103
1104         cpu_dev = get_cpu_device(cpu);
1105         if (!cpu_dev)
1106                 return;
1107
1108         nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
1109         if (nr_opp <= 0)
1110                 return;
1111
1112         np = of_node_get(cpu_dev->of_node);
1113         if (!np)
1114                 return;
1115
1116         /*
1117          * Register an EM only if the 'dynamic-power-coefficient' property is
1118          * set in devicetree. It is assumed the voltage values are known if that
1119          * property is set since it is useless otherwise. If voltages are not
1120          * known, just let the EM registration fail with an error to alert the
1121          * user about the inconsistent configuration.
1122          */
1123         ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1124         of_node_put(np);
1125         if (ret || !cap)
1126                 return;
1127
1128         em_register_perf_domain(cpus, nr_opp, &em_cb);
1129 }
1130 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);