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.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 /* OPP tables with uninitialized required OPPs, protected by opp_table_lock */
25 static LIST_HEAD(lazy_opp_tables);
26
27 /*
28 * Returns opp descriptor node for a device node, caller must
29 * do of_node_put().
30 */
_opp_of_get_opp_desc_node(struct device_node * np,int index)31 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
32 int index)
33 {
34 /* "operating-points-v2" can be an array for power domain providers */
35 return of_parse_phandle(np, "operating-points-v2", index);
36 }
37
38 /* Returns opp descriptor node for a device, caller must do of_node_put() */
dev_pm_opp_of_get_opp_desc_node(struct device * dev)39 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
40 {
41 return _opp_of_get_opp_desc_node(dev->of_node, 0);
42 }
43 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
44
_managed_opp(struct device * dev,int index)45 struct opp_table *_managed_opp(struct device *dev, int index)
46 {
47 struct opp_table *opp_table, *managed_table = NULL;
48
49 struct device_node *np __free(device_node) =
50 _opp_of_get_opp_desc_node(dev->of_node, index);
51
52 if (!np)
53 return NULL;
54
55 list_for_each_entry(opp_table, &opp_tables, node) {
56 if (opp_table->np == np) {
57 /*
58 * Multiple devices can point to the same OPP table and
59 * so will have same node-pointer, np.
60 *
61 * But the OPPs will be considered as shared only if the
62 * OPP table contains a "opp-shared" property.
63 */
64 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED)
65 managed_table = dev_pm_opp_get_opp_table_ref(opp_table);
66
67 break;
68 }
69 }
70
71 return managed_table;
72 }
73
74 /* The caller must call dev_pm_opp_put() after the OPP is used */
_find_opp_of_np(struct opp_table * opp_table,struct device_node * opp_np)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 guard(mutex)(&opp_table->lock);
81
82 list_for_each_entry(opp, &opp_table->opp_list, node) {
83 if (opp->np == opp_np)
84 return dev_pm_opp_get(opp);
85 }
86
87 return NULL;
88 }
89
of_parse_required_opp(struct device_node * np,int index)90 static struct device_node *of_parse_required_opp(struct device_node *np,
91 int index)
92 {
93 return of_parse_phandle(np, "required-opps", index);
94 }
95
96 /* The caller must call dev_pm_opp_put_opp_table() after the table is used */
_find_table_of_opp_np(struct device_node * opp_np)97 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
98 {
99 struct opp_table *opp_table;
100
101 struct device_node *opp_table_np __free(device_node) =
102 of_get_parent(opp_np);
103
104 if (!opp_table_np)
105 return ERR_PTR(-ENODEV);
106
107 guard(mutex)(&opp_table_lock);
108
109 list_for_each_entry(opp_table, &opp_tables, node) {
110 if (opp_table_np == opp_table->np)
111 return dev_pm_opp_get_opp_table_ref(opp_table);
112 }
113
114 return ERR_PTR(-ENODEV);
115 }
116
117 /* Free resources previously acquired by _opp_table_alloc_required_tables() */
_opp_table_free_required_tables(struct opp_table * opp_table)118 static void _opp_table_free_required_tables(struct opp_table *opp_table)
119 {
120 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
121 int i;
122
123 if (!required_opp_tables)
124 return;
125
126 for (i = 0; i < opp_table->required_opp_count; i++) {
127 if (IS_ERR_OR_NULL(required_opp_tables[i]))
128 continue;
129
130 dev_pm_opp_put_opp_table(required_opp_tables[i]);
131 }
132
133 kfree(required_opp_tables);
134
135 opp_table->required_opp_count = 0;
136 opp_table->required_opp_tables = NULL;
137
138 guard(mutex)(&opp_table_lock);
139 list_del(&opp_table->lazy);
140 }
141
142 /*
143 * Populate all devices and opp tables which are part of "required-opps" list.
144 * Checking only the first OPP node should be enough.
145 */
_opp_table_alloc_required_tables(struct opp_table * opp_table,struct device * dev,struct device_node * opp_np)146 static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
147 struct device *dev,
148 struct device_node *opp_np)
149 {
150 struct opp_table **required_opp_tables;
151 bool lazy = false;
152 int count, i, size;
153
154 /* Traversing the first OPP node is all we need */
155 struct device_node *np __free(device_node) =
156 of_get_next_available_child(opp_np, NULL);
157
158 if (!np) {
159 dev_warn(dev, "Empty OPP table\n");
160 return;
161 }
162
163 count = of_count_phandle_with_args(np, "required-opps", NULL);
164 if (count <= 0)
165 return;
166
167 size = sizeof(*required_opp_tables) + sizeof(*opp_table->required_devs);
168 required_opp_tables = kcalloc(count, size, GFP_KERNEL);
169 if (!required_opp_tables)
170 return;
171
172 opp_table->required_opp_tables = required_opp_tables;
173 opp_table->required_devs = (void *)(required_opp_tables + count);
174 opp_table->required_opp_count = count;
175
176 for (i = 0; i < count; i++) {
177 struct device_node *required_np __free(device_node) =
178 of_parse_required_opp(np, i);
179
180 if (!required_np) {
181 _opp_table_free_required_tables(opp_table);
182 return;
183 }
184
185 required_opp_tables[i] = _find_table_of_opp_np(required_np);
186
187 if (IS_ERR(required_opp_tables[i]))
188 lazy = true;
189 }
190
191 /* Let's do the linking later on */
192 if (lazy) {
193 /*
194 * The OPP table is not held while allocating the table, take it
195 * now to avoid corruption to the lazy_opp_tables list.
196 */
197 guard(mutex)(&opp_table_lock);
198 list_add(&opp_table->lazy, &lazy_opp_tables);
199 }
200 }
201
_of_init_opp_table(struct opp_table * opp_table,struct device * dev,int index)202 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
203 int index)
204 {
205 struct device_node *opp_np;
206 u32 val;
207
208 /*
209 * Only required for backward compatibility with v1 bindings, but isn't
210 * harmful for other cases. And so we do it unconditionally.
211 */
212 struct device_node *np __free(device_node) = of_node_get(dev->of_node);
213
214 if (!np)
215 return;
216
217 if (!of_property_read_u32(np, "clock-latency", &val))
218 opp_table->clock_latency_ns_max = val;
219 of_property_read_u32(np, "voltage-tolerance",
220 &opp_table->voltage_tolerance_v1);
221
222 if (of_property_present(np, "#power-domain-cells"))
223 opp_table->is_genpd = true;
224
225 /* Get OPP table node */
226 opp_np = _opp_of_get_opp_desc_node(np, index);
227 if (!opp_np)
228 return;
229
230 if (of_property_read_bool(opp_np, "opp-shared"))
231 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
232 else
233 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
234
235 opp_table->np = opp_np;
236
237 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
238 }
239
_of_clear_opp_table(struct opp_table * opp_table)240 void _of_clear_opp_table(struct opp_table *opp_table)
241 {
242 _opp_table_free_required_tables(opp_table);
243 of_node_put(opp_table->np);
244 }
245
246 /*
247 * Release all resources previously acquired with a call to
248 * _of_opp_alloc_required_opps().
249 */
_of_opp_free_required_opps(struct opp_table * opp_table,struct dev_pm_opp * opp)250 static void _of_opp_free_required_opps(struct opp_table *opp_table,
251 struct dev_pm_opp *opp)
252 {
253 struct dev_pm_opp **required_opps = opp->required_opps;
254 int i;
255
256 if (!required_opps)
257 return;
258
259 for (i = 0; i < opp_table->required_opp_count; i++) {
260 if (!required_opps[i])
261 continue;
262
263 /* Put the reference back */
264 dev_pm_opp_put(required_opps[i]);
265 }
266
267 opp->required_opps = NULL;
268 kfree(required_opps);
269 }
270
_of_clear_opp(struct opp_table * opp_table,struct dev_pm_opp * opp)271 void _of_clear_opp(struct opp_table *opp_table, struct dev_pm_opp *opp)
272 {
273 _of_opp_free_required_opps(opp_table, opp);
274 of_node_put(opp->np);
275 }
276
_link_required_opps(struct dev_pm_opp * opp,struct opp_table * required_table,int index)277 static int _link_required_opps(struct dev_pm_opp *opp,
278 struct opp_table *required_table, int index)
279 {
280 struct device_node *np __free(device_node) =
281 of_parse_required_opp(opp->np, index);
282
283 if (unlikely(!np))
284 return -ENODEV;
285
286 opp->required_opps[index] = _find_opp_of_np(required_table, np);
287 if (!opp->required_opps[index]) {
288 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
289 __func__, opp->np, index);
290 return -ENODEV;
291 }
292
293 return 0;
294 }
295
296 /* Populate all required OPPs which are part of "required-opps" list */
_of_opp_alloc_required_opps(struct opp_table * opp_table,struct dev_pm_opp * opp)297 static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
298 struct dev_pm_opp *opp)
299 {
300 struct opp_table *required_table;
301 int i, ret, count = opp_table->required_opp_count;
302
303 if (!count)
304 return 0;
305
306 opp->required_opps = kzalloc_objs(*opp->required_opps, count);
307 if (!opp->required_opps)
308 return -ENOMEM;
309
310 for (i = 0; i < count; i++) {
311 required_table = opp_table->required_opp_tables[i];
312
313 /* Required table not added yet, we will link later */
314 if (IS_ERR_OR_NULL(required_table))
315 continue;
316
317 ret = _link_required_opps(opp, required_table, i);
318 if (ret)
319 goto free_required_opps;
320 }
321
322 return 0;
323
324 free_required_opps:
325 _of_opp_free_required_opps(opp_table, opp);
326
327 return ret;
328 }
329
330 /* Link required OPPs for an individual OPP */
lazy_link_required_opps(struct opp_table * opp_table,struct opp_table * new_table,int index)331 static int lazy_link_required_opps(struct opp_table *opp_table,
332 struct opp_table *new_table, int index)
333 {
334 struct dev_pm_opp *opp;
335 int ret;
336
337 list_for_each_entry(opp, &opp_table->opp_list, node) {
338 ret = _link_required_opps(opp, new_table, index);
339 if (ret)
340 return ret;
341 }
342
343 return 0;
344 }
345
346 /* Link required OPPs for all OPPs of the newly added OPP table */
lazy_link_required_opp_table(struct opp_table * new_table)347 static void lazy_link_required_opp_table(struct opp_table *new_table)
348 {
349 struct opp_table *opp_table, *temp, **required_opp_tables;
350 struct dev_pm_opp *opp;
351 int i, ret;
352
353 guard(mutex)(&opp_table_lock);
354
355 list_for_each_entry_safe(opp_table, temp, &lazy_opp_tables, lazy) {
356 bool lazy = false;
357
358 /* opp_np can't be invalid here */
359 struct device_node *opp_np __free(device_node) =
360 of_get_next_available_child(opp_table->np, NULL);
361
362 for (i = 0; i < opp_table->required_opp_count; i++) {
363 required_opp_tables = opp_table->required_opp_tables;
364
365 /* Required opp-table is already parsed */
366 if (!IS_ERR(required_opp_tables[i]))
367 continue;
368
369 /* required_np can't be invalid here */
370 struct device_node *required_np __free(device_node) =
371 of_parse_required_opp(opp_np, i);
372 struct device_node *required_table_np __free(device_node) =
373 of_get_parent(required_np);
374
375 /*
376 * Newly added table isn't the required opp-table for
377 * opp_table.
378 */
379 if (required_table_np != new_table->np) {
380 lazy = true;
381 continue;
382 }
383
384 required_opp_tables[i] = dev_pm_opp_get_opp_table_ref(new_table);
385
386 /* Link OPPs now */
387 ret = lazy_link_required_opps(opp_table, new_table, i);
388 if (ret) {
389 /* The OPPs will be marked unusable */
390 lazy = false;
391 break;
392 }
393 }
394
395 /* All required opp-tables found, remove from lazy list */
396 if (!lazy) {
397 list_del_init(&opp_table->lazy);
398
399 list_for_each_entry(opp, &opp_table->opp_list, node)
400 _required_opps_available(opp, opp_table->required_opp_count);
401 }
402 }
403 }
404
_bandwidth_supported(struct device * dev,struct opp_table * opp_table)405 static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table)
406 {
407 struct device_node *opp_np __free(device_node) = NULL;
408 struct property *prop;
409
410 if (!opp_table) {
411 struct device_node *np __free(device_node) =
412 of_node_get(dev->of_node);
413
414 if (!np)
415 return -ENODEV;
416
417 opp_np = _opp_of_get_opp_desc_node(np, 0);
418 } else {
419 opp_np = of_node_get(opp_table->np);
420 }
421
422 /* Lets not fail in case we are parsing opp-v1 bindings */
423 if (!opp_np)
424 return 0;
425
426 /* Checking only first OPP is sufficient */
427 struct device_node *np __free(device_node) =
428 of_get_next_available_child(opp_np, NULL);
429
430 if (!np) {
431 dev_err(dev, "OPP table empty\n");
432 return -EINVAL;
433 }
434
435 prop = of_find_property(np, "opp-peak-kBps", NULL);
436 if (!prop || !prop->length)
437 return 0;
438
439 return 1;
440 }
441
dev_pm_opp_of_find_icc_paths(struct device * dev,struct opp_table * opp_table)442 int dev_pm_opp_of_find_icc_paths(struct device *dev,
443 struct opp_table *opp_table)
444 {
445 struct device_node *np __free(device_node) = of_node_get(dev->of_node);
446 int ret, i, count, num_paths;
447 struct icc_path **paths;
448
449 ret = _bandwidth_supported(dev, opp_table);
450 if (ret == -EINVAL)
451 return 0; /* Empty OPP table is a valid corner-case, let's not fail */
452 else if (ret <= 0)
453 return ret;
454
455 if (!np)
456 return 0;
457
458 ret = 0;
459
460 count = of_count_phandle_with_args(np, "interconnects",
461 "#interconnect-cells");
462 if (count < 0)
463 return 0;
464
465 /* two phandles when #interconnect-cells = <1> */
466 if (count % 2) {
467 dev_err(dev, "%s: Invalid interconnects values\n", __func__);
468 return -EINVAL;
469 }
470
471 num_paths = count / 2;
472 paths = kzalloc_objs(*paths, num_paths);
473 if (!paths)
474 return -ENOMEM;
475
476 for (i = 0; i < num_paths; i++) {
477 paths[i] = of_icc_get_by_index(dev, i);
478 if (IS_ERR(paths[i])) {
479 ret = dev_err_probe(dev, PTR_ERR(paths[i]), "%s: Unable to get path%d\n", __func__, i);
480 goto err;
481 }
482 }
483
484 if (opp_table) {
485 opp_table->paths = paths;
486 opp_table->path_count = num_paths;
487 return 0;
488 }
489
490 err:
491 while (i--)
492 icc_put(paths[i]);
493
494 kfree(paths);
495
496 return ret;
497 }
498 EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths);
499
_opp_is_supported(struct device * dev,struct opp_table * opp_table,struct device_node * np)500 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
501 struct device_node *np)
502 {
503 unsigned int levels = opp_table->supported_hw_count;
504 int count, versions, ret, i, j;
505 u32 val;
506
507 if (!opp_table->supported_hw) {
508 /*
509 * In the case that no supported_hw has been set by the
510 * platform but there is an opp-supported-hw value set for
511 * an OPP then the OPP should not be enabled as there is
512 * no way to see if the hardware supports it.
513 */
514 if (of_property_present(np, "opp-supported-hw"))
515 return false;
516 else
517 return true;
518 }
519
520 count = of_property_count_u32_elems(np, "opp-supported-hw");
521 if (count <= 0 || count % levels) {
522 dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n",
523 __func__, count);
524 return false;
525 }
526
527 versions = count / levels;
528
529 /* All levels in at least one of the versions should match */
530 for (i = 0; i < versions; i++) {
531 bool supported = true;
532
533 for (j = 0; j < levels; j++) {
534 ret = of_property_read_u32_index(np, "opp-supported-hw",
535 i * levels + j, &val);
536 if (ret) {
537 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
538 __func__, i * levels + j, ret);
539 return false;
540 }
541
542 /* Check if the level is supported */
543 if (!(val & opp_table->supported_hw[j])) {
544 supported = false;
545 break;
546 }
547 }
548
549 if (supported)
550 return true;
551 }
552
553 return false;
554 }
555
_parse_named_prop(struct dev_pm_opp * opp,struct device * dev,struct opp_table * opp_table,const char * prop_type,bool * triplet)556 static u32 *_parse_named_prop(struct dev_pm_opp *opp, struct device *dev,
557 struct opp_table *opp_table,
558 const char *prop_type, bool *triplet)
559 {
560 struct property *prop = NULL;
561 char name[NAME_MAX];
562 int count, ret;
563 u32 *out;
564
565 /* Search for "opp-<prop_type>-<name>" */
566 if (opp_table->prop_name) {
567 snprintf(name, sizeof(name), "opp-%s-%s", prop_type,
568 opp_table->prop_name);
569 prop = of_find_property(opp->np, name, NULL);
570 }
571
572 if (!prop) {
573 /* Search for "opp-<prop_type>" */
574 snprintf(name, sizeof(name), "opp-%s", prop_type);
575 prop = of_find_property(opp->np, name, NULL);
576 if (!prop)
577 return NULL;
578 }
579
580 count = of_property_count_u32_elems(opp->np, name);
581 if (count < 0) {
582 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, name,
583 count);
584 return ERR_PTR(count);
585 }
586
587 /*
588 * Initialize regulator_count, if regulator information isn't provided
589 * by the platform. Now that one of the properties is available, fix the
590 * regulator_count to 1.
591 */
592 if (unlikely(opp_table->regulator_count == -1))
593 opp_table->regulator_count = 1;
594
595 if (count != opp_table->regulator_count &&
596 (!triplet || count != opp_table->regulator_count * 3)) {
597 dev_err(dev, "%s: Invalid number of elements in %s property (%u) with supplies (%d)\n",
598 __func__, prop_type, count, opp_table->regulator_count);
599 return ERR_PTR(-EINVAL);
600 }
601
602 out = kmalloc_array(count, sizeof(*out), GFP_KERNEL);
603 if (!out)
604 return ERR_PTR(-EINVAL);
605
606 ret = of_property_read_u32_array(opp->np, name, out, count);
607 if (ret) {
608 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
609 kfree(out);
610 return ERR_PTR(-EINVAL);
611 }
612
613 if (triplet)
614 *triplet = count != opp_table->regulator_count;
615
616 return out;
617 }
618
opp_parse_microvolt(struct dev_pm_opp * opp,struct device * dev,struct opp_table * opp_table,bool * triplet)619 static u32 *opp_parse_microvolt(struct dev_pm_opp *opp, struct device *dev,
620 struct opp_table *opp_table, bool *triplet)
621 {
622 u32 *microvolt;
623
624 microvolt = _parse_named_prop(opp, dev, opp_table, "microvolt", triplet);
625 if (IS_ERR(microvolt))
626 return microvolt;
627
628 if (!microvolt) {
629 /*
630 * Missing property isn't a problem, but an invalid
631 * entry is. This property isn't optional if regulator
632 * information is provided. Check only for the first OPP, as
633 * regulator_count may get initialized after that to a valid
634 * value.
635 */
636 if (list_empty(&opp_table->opp_list) &&
637 opp_table->regulator_count > 0) {
638 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
639 __func__);
640 return ERR_PTR(-EINVAL);
641 }
642 }
643
644 return microvolt;
645 }
646
opp_parse_supplies(struct dev_pm_opp * opp,struct device * dev,struct opp_table * opp_table)647 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
648 struct opp_table *opp_table)
649 {
650 u32 *microvolt, *microamp, *microwatt;
651 int ret = 0, i, j;
652 bool triplet;
653
654 microvolt = opp_parse_microvolt(opp, dev, opp_table, &triplet);
655 if (IS_ERR(microvolt))
656 return PTR_ERR(microvolt);
657
658 microamp = _parse_named_prop(opp, dev, opp_table, "microamp", NULL);
659 if (IS_ERR(microamp)) {
660 ret = PTR_ERR(microamp);
661 goto free_microvolt;
662 }
663
664 microwatt = _parse_named_prop(opp, dev, opp_table, "microwatt", NULL);
665 if (IS_ERR(microwatt)) {
666 ret = PTR_ERR(microwatt);
667 goto free_microamp;
668 }
669
670 /*
671 * Initialize regulator_count if it is uninitialized and no properties
672 * are found.
673 */
674 if (unlikely(opp_table->regulator_count == -1)) {
675 opp_table->regulator_count = 0;
676 goto free_microwatt;
677 }
678
679 for (i = 0, j = 0; i < opp_table->regulator_count; i++) {
680 if (microvolt) {
681 opp->supplies[i].u_volt = microvolt[j++];
682
683 if (triplet) {
684 opp->supplies[i].u_volt_min = microvolt[j++];
685 opp->supplies[i].u_volt_max = microvolt[j++];
686 } else {
687 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
688 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
689 }
690 }
691
692 if (microamp)
693 opp->supplies[i].u_amp = microamp[i];
694
695 if (microwatt)
696 opp->supplies[i].u_watt = microwatt[i];
697 }
698
699 free_microwatt:
700 kfree(microwatt);
701 free_microamp:
702 kfree(microamp);
703 free_microvolt:
704 kfree(microvolt);
705
706 return ret;
707 }
708
709 /**
710 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
711 * entries
712 * @dev: device pointer used to lookup OPP table.
713 *
714 * Free OPPs created using static entries present in DT.
715 */
dev_pm_opp_of_remove_table(struct device * dev)716 void dev_pm_opp_of_remove_table(struct device *dev)
717 {
718 dev_pm_opp_remove_table(dev);
719 }
720 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
721
_read_rate(struct dev_pm_opp * new_opp,struct opp_table * opp_table,struct device_node * np)722 static int _read_rate(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
723 struct device_node *np)
724 {
725 struct property *prop;
726 int i, count, ret;
727 u64 *rates;
728
729 prop = of_find_property(np, "opp-hz", NULL);
730 if (!prop)
731 return -ENODEV;
732
733 count = prop->length / sizeof(u64);
734 if (opp_table->clk_count != count) {
735 pr_err("%s: Count mismatch between opp-hz and clk_count (%d %d)\n",
736 __func__, count, opp_table->clk_count);
737 return -EINVAL;
738 }
739
740 rates = kmalloc_array(count, sizeof(*rates), GFP_KERNEL);
741 if (!rates)
742 return -ENOMEM;
743
744 ret = of_property_read_u64_array(np, "opp-hz", rates, count);
745 if (ret) {
746 pr_err("%s: Error parsing opp-hz: %d\n", __func__, ret);
747 } else {
748 /*
749 * Rate is defined as an unsigned long in clk API, and so
750 * casting explicitly to its type. Must be fixed once rate is 64
751 * bit guaranteed in clk API.
752 */
753 for (i = 0; i < count; i++) {
754 new_opp->rates[i] = (unsigned long)rates[i];
755
756 /* This will happen for frequencies > 4.29 GHz */
757 WARN_ON(new_opp->rates[i] != rates[i]);
758 }
759 }
760
761 kfree(rates);
762
763 return ret;
764 }
765
_read_bw(struct dev_pm_opp * new_opp,struct opp_table * opp_table,struct device_node * np,bool peak)766 static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
767 struct device_node *np, bool peak)
768 {
769 const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps";
770 struct property *prop;
771 int i, count, ret;
772 u32 *bw;
773
774 prop = of_find_property(np, name, NULL);
775 if (!prop)
776 return -ENODEV;
777
778 count = prop->length / sizeof(u32);
779 if (opp_table->path_count != count) {
780 pr_err("%s: Mismatch between %s and paths (%d %d)\n",
781 __func__, name, count, opp_table->path_count);
782 return -EINVAL;
783 }
784
785 bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL);
786 if (!bw)
787 return -ENOMEM;
788
789 ret = of_property_read_u32_array(np, name, bw, count);
790 if (ret) {
791 pr_err("%s: Error parsing %s: %d\n", __func__, name, ret);
792 goto out;
793 }
794
795 for (i = 0; i < count; i++) {
796 if (peak)
797 new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]);
798 else
799 new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]);
800 }
801
802 out:
803 kfree(bw);
804 return ret;
805 }
806
_read_opp_key(struct dev_pm_opp * new_opp,struct opp_table * opp_table,struct device_node * np)807 static int _read_opp_key(struct dev_pm_opp *new_opp,
808 struct opp_table *opp_table, struct device_node *np)
809 {
810 bool found = false;
811 int ret;
812
813 ret = _read_rate(new_opp, opp_table, np);
814 if (!ret)
815 found = true;
816 else if (ret != -ENODEV)
817 return ret;
818
819 /*
820 * Bandwidth consists of peak and average (optional) values:
821 * opp-peak-kBps = <path1_value path2_value>;
822 * opp-avg-kBps = <path1_value path2_value>;
823 */
824 ret = _read_bw(new_opp, opp_table, np, true);
825 if (!ret) {
826 found = true;
827 ret = _read_bw(new_opp, opp_table, np, false);
828 }
829
830 /* The properties were found but we failed to parse them */
831 if (ret && ret != -ENODEV)
832 return ret;
833
834 if (!of_property_read_u32(np, "opp-level", &new_opp->level))
835 found = true;
836
837 if (found)
838 return 0;
839
840 return ret;
841 }
842
843 /**
844 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
845 * @opp_table: OPP table
846 * @dev: device for which we do this operation
847 * @np: device node
848 *
849 * This function adds an opp definition to the opp table and returns status. The
850 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
851 * removed by dev_pm_opp_remove.
852 *
853 * Return:
854 * Valid OPP pointer:
855 * On success
856 * NULL:
857 * Duplicate OPPs (both freq and volt are same) and opp->available
858 * OR if the OPP is not supported by hardware.
859 * ERR_PTR(-EEXIST):
860 * Freq are same and volt are different OR
861 * Duplicate OPPs (both freq and volt are same) and !opp->available
862 * ERR_PTR(-ENOMEM):
863 * Memory allocation failure
864 * ERR_PTR(-EINVAL):
865 * Failed parsing the OPP node
866 */
_opp_add_static_v2(struct opp_table * opp_table,struct device * dev,struct device_node * np)867 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
868 struct device *dev, struct device_node *np)
869 {
870 struct dev_pm_opp *new_opp;
871 u32 val;
872 int ret;
873
874 new_opp = _opp_allocate(opp_table);
875 if (!new_opp)
876 return ERR_PTR(-ENOMEM);
877
878 ret = _read_opp_key(new_opp, opp_table, np);
879 if (ret < 0) {
880 dev_err(dev, "%s: opp key field not found\n", __func__);
881 goto free_opp;
882 }
883
884 /* Check if the OPP supports hardware's hierarchy of versions or not */
885 if (!_opp_is_supported(dev, opp_table, np)) {
886 dev_dbg(dev, "OPP not supported by hardware: %s\n",
887 of_node_full_name(np));
888 goto free_opp;
889 }
890
891 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
892
893 new_opp->np = of_node_get(np);
894 new_opp->dynamic = false;
895 new_opp->available = true;
896
897 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
898 if (ret)
899 goto put_node;
900
901 if (!of_property_read_u32(np, "clock-latency-ns", &val))
902 new_opp->clock_latency_ns = val;
903
904 ret = opp_parse_supplies(new_opp, dev, opp_table);
905 if (ret)
906 goto free_required_opps;
907
908 ret = _opp_add(dev, new_opp, opp_table);
909 if (ret) {
910 /* Don't return error for duplicate OPPs */
911 if (ret == -EBUSY)
912 ret = 0;
913 goto free_required_opps;
914 }
915
916 /* OPP to select on device suspend */
917 if (of_property_read_bool(np, "opp-suspend")) {
918 if (opp_table->suspend_opp) {
919 /* Pick the OPP with higher rate/bw/level as suspend OPP */
920 if (_opp_compare_key(opp_table, new_opp, opp_table->suspend_opp) == 1) {
921 opp_table->suspend_opp->suspend = false;
922 new_opp->suspend = true;
923 opp_table->suspend_opp = new_opp;
924 }
925 } else {
926 new_opp->suspend = true;
927 opp_table->suspend_opp = new_opp;
928 }
929 }
930
931 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
932 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
933
934 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu level:%u\n",
935 __func__, new_opp->turbo, new_opp->rates[0],
936 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
937 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns,
938 new_opp->level);
939
940 /*
941 * Notify the changes in the availability of the operable
942 * frequency/voltage list.
943 */
944 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
945 return new_opp;
946
947 free_required_opps:
948 _of_opp_free_required_opps(opp_table, new_opp);
949 put_node:
950 of_node_put(np);
951 free_opp:
952 _opp_free(new_opp);
953
954 return ret ? ERR_PTR(ret) : NULL;
955 }
956
957 /* Initializes OPP tables based on new bindings */
_of_add_opp_table_v2(struct device * dev,struct opp_table * opp_table)958 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
959 {
960 int ret, count = 0;
961 struct dev_pm_opp *opp;
962
963 /* OPP table is already initialized for the device */
964 scoped_guard(mutex, &opp_table->lock) {
965 if (opp_table->parsed_static_opps) {
966 opp_table->parsed_static_opps++;
967 return 0;
968 }
969
970 opp_table->parsed_static_opps = 1;
971 }
972
973 /* We have opp-table node now, iterate over it and add OPPs */
974 for_each_available_child_of_node_scoped(opp_table->np, np) {
975 opp = _opp_add_static_v2(opp_table, dev, np);
976 if (IS_ERR(opp)) {
977 ret = PTR_ERR(opp);
978 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
979 ret);
980 goto remove_static_opp;
981 } else if (opp) {
982 count++;
983 }
984 }
985
986 /* There should be one or more OPPs defined */
987 if (!count) {
988 dev_err(dev, "%s: no supported OPPs", __func__);
989 ret = -ENOENT;
990 goto remove_static_opp;
991 }
992
993 lazy_link_required_opp_table(opp_table);
994
995 return 0;
996
997 remove_static_opp:
998 _opp_remove_all_static(opp_table);
999
1000 return ret;
1001 }
1002
1003 /* Initializes OPP tables based on old-deprecated bindings */
_of_add_opp_table_v1(struct device * dev,struct opp_table * opp_table)1004 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
1005 {
1006 const struct property *prop;
1007 const __be32 *val;
1008 int nr, ret = 0;
1009
1010 scoped_guard(mutex, &opp_table->lock) {
1011 if (opp_table->parsed_static_opps) {
1012 opp_table->parsed_static_opps++;
1013 return 0;
1014 }
1015
1016 opp_table->parsed_static_opps = 1;
1017 }
1018
1019 prop = of_find_property(dev->of_node, "operating-points", NULL);
1020 if (!prop) {
1021 ret = -ENODEV;
1022 goto remove_static_opp;
1023 }
1024 if (!prop->value) {
1025 ret = -ENODATA;
1026 goto remove_static_opp;
1027 }
1028
1029 /*
1030 * Each OPP is a set of tuples consisting of frequency and
1031 * voltage like <freq-kHz vol-uV>.
1032 */
1033 nr = prop->length / sizeof(u32);
1034 if (nr % 2) {
1035 dev_err(dev, "%s: Invalid OPP table\n", __func__);
1036 ret = -EINVAL;
1037 goto remove_static_opp;
1038 }
1039
1040 val = prop->value;
1041 while (nr) {
1042 unsigned long freq = (unsigned long)be32_to_cpup(val++) * 1000;
1043 unsigned long volt = be32_to_cpup(val++);
1044 struct dev_pm_opp_data data = {
1045 .freq = freq,
1046 .u_volt = volt,
1047 };
1048
1049 ret = _opp_add_v1(opp_table, dev, &data, false);
1050 if (ret) {
1051 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
1052 __func__, data.freq, ret);
1053 goto remove_static_opp;
1054 }
1055 nr -= 2;
1056 }
1057
1058 return 0;
1059
1060 remove_static_opp:
1061 _opp_remove_all_static(opp_table);
1062
1063 return ret;
1064 }
1065
_of_add_table_indexed(struct device * dev,int index)1066 static int _of_add_table_indexed(struct device *dev, int index)
1067 {
1068 struct opp_table *opp_table;
1069 int ret, count;
1070
1071 if (index) {
1072 /*
1073 * If only one phandle is present, then the same OPP table
1074 * applies for all index requests.
1075 */
1076 count = of_count_phandle_with_args(dev->of_node,
1077 "operating-points-v2", NULL);
1078 if (count == 1)
1079 index = 0;
1080 }
1081
1082 opp_table = _add_opp_table_indexed(dev, index, true);
1083 if (IS_ERR(opp_table))
1084 return PTR_ERR(opp_table);
1085
1086 /*
1087 * OPPs have two version of bindings now. Also try the old (v1)
1088 * bindings for backward compatibility with older dtbs.
1089 */
1090 if (opp_table->np)
1091 ret = _of_add_opp_table_v2(dev, opp_table);
1092 else
1093 ret = _of_add_opp_table_v1(dev, opp_table);
1094
1095 if (ret)
1096 dev_pm_opp_put_opp_table(opp_table);
1097
1098 return ret;
1099 }
1100
devm_pm_opp_of_table_release(void * data)1101 static void devm_pm_opp_of_table_release(void *data)
1102 {
1103 dev_pm_opp_of_remove_table(data);
1104 }
1105
_devm_of_add_table_indexed(struct device * dev,int index)1106 static int _devm_of_add_table_indexed(struct device *dev, int index)
1107 {
1108 int ret;
1109
1110 ret = _of_add_table_indexed(dev, index);
1111 if (ret)
1112 return ret;
1113
1114 return devm_add_action_or_reset(dev, devm_pm_opp_of_table_release, dev);
1115 }
1116
1117 /**
1118 * devm_pm_opp_of_add_table() - Initialize opp table from device tree
1119 * @dev: device pointer used to lookup OPP table.
1120 *
1121 * Register the initial OPP table with the OPP library for given device.
1122 *
1123 * The opp_table structure will be freed after the device is destroyed.
1124 *
1125 * Return:
1126 * 0 On success OR
1127 * Duplicate OPPs (both freq and volt are same) and opp->available
1128 * -EEXIST Freq are same and volt are different OR
1129 * Duplicate OPPs (both freq and volt are same) and !opp->available
1130 * -ENOMEM Memory allocation failure
1131 * -ENODEV when 'operating-points' property is not found or is invalid data
1132 * in device node.
1133 * -ENODATA when empty 'operating-points' property is found
1134 * -EINVAL when invalid entries are found in opp-v2 table
1135 */
devm_pm_opp_of_add_table(struct device * dev)1136 int devm_pm_opp_of_add_table(struct device *dev)
1137 {
1138 return _devm_of_add_table_indexed(dev, 0);
1139 }
1140 EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table);
1141
1142 /**
1143 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
1144 * @dev: device pointer used to lookup OPP table.
1145 *
1146 * Register the initial OPP table with the OPP library for given device.
1147 *
1148 * Return:
1149 * 0 On success OR
1150 * Duplicate OPPs (both freq and volt are same) and opp->available
1151 * -EEXIST Freq are same and volt are different OR
1152 * Duplicate OPPs (both freq and volt are same) and !opp->available
1153 * -ENOMEM Memory allocation failure
1154 * -ENODEV when 'operating-points' property is not found or is invalid data
1155 * in device node.
1156 * -ENODATA when empty 'operating-points' property is found
1157 * -EINVAL when invalid entries are found in opp-v2 table
1158 */
dev_pm_opp_of_add_table(struct device * dev)1159 int dev_pm_opp_of_add_table(struct device *dev)
1160 {
1161 return _of_add_table_indexed(dev, 0);
1162 }
1163 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
1164
1165 /**
1166 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
1167 * @dev: device pointer used to lookup OPP table.
1168 * @index: Index number.
1169 *
1170 * Register the initial OPP table with the OPP library for given device only
1171 * using the "operating-points-v2" property.
1172 *
1173 * Return: Refer to dev_pm_opp_of_add_table() for return values.
1174 */
dev_pm_opp_of_add_table_indexed(struct device * dev,int index)1175 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
1176 {
1177 return _of_add_table_indexed(dev, index);
1178 }
1179 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
1180
1181 /**
1182 * devm_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
1183 * @dev: device pointer used to lookup OPP table.
1184 * @index: Index number.
1185 *
1186 * This is a resource-managed variant of dev_pm_opp_of_add_table_indexed().
1187 */
devm_pm_opp_of_add_table_indexed(struct device * dev,int index)1188 int devm_pm_opp_of_add_table_indexed(struct device *dev, int index)
1189 {
1190 return _devm_of_add_table_indexed(dev, index);
1191 }
1192 EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table_indexed);
1193
1194 /* CPU device specific helpers */
1195
1196 /**
1197 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
1198 * @cpumask: cpumask for which OPP table needs to be removed
1199 *
1200 * This removes the OPP tables for CPUs present in the @cpumask.
1201 * This should be used only to remove static entries created from DT.
1202 */
dev_pm_opp_of_cpumask_remove_table(const struct cpumask * cpumask)1203 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
1204 {
1205 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
1206 }
1207 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
1208
1209 /**
1210 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
1211 * @cpumask: cpumask for which OPP table needs to be added.
1212 *
1213 * This adds the OPP tables for CPUs present in the @cpumask.
1214 */
dev_pm_opp_of_cpumask_add_table(const struct cpumask * cpumask)1215 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
1216 {
1217 struct device *cpu_dev;
1218 int cpu, ret;
1219
1220 if (WARN_ON(cpumask_empty(cpumask)))
1221 return -ENODEV;
1222
1223 for_each_cpu(cpu, cpumask) {
1224 cpu_dev = get_cpu_device(cpu);
1225 if (!cpu_dev) {
1226 pr_err("%s: failed to get cpu%d device\n", __func__,
1227 cpu);
1228 ret = -ENODEV;
1229 goto remove_table;
1230 }
1231
1232 ret = dev_pm_opp_of_add_table(cpu_dev);
1233 if (ret) {
1234 /*
1235 * OPP may get registered dynamically, don't print error
1236 * message here.
1237 */
1238 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
1239 __func__, cpu, ret);
1240
1241 goto remove_table;
1242 }
1243 }
1244
1245 return 0;
1246
1247 remove_table:
1248 /* Free all other OPPs */
1249 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
1250
1251 return ret;
1252 }
1253 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
1254
1255 /*
1256 * Works only for OPP v2 bindings.
1257 *
1258 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
1259 */
1260 /**
1261 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
1262 * @cpu_dev using operating-points-v2
1263 * bindings.
1264 *
1265 * @cpu_dev: CPU device for which we do this operation
1266 * @cpumask: cpumask to update with information of sharing CPUs
1267 *
1268 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
1269 *
1270 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
1271 */
dev_pm_opp_of_get_sharing_cpus(struct device * cpu_dev,struct cpumask * cpumask)1272 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
1273 struct cpumask *cpumask)
1274 {
1275 int cpu;
1276
1277 /* Get OPP descriptor node */
1278 struct device_node *np __free(device_node) =
1279 dev_pm_opp_of_get_opp_desc_node(cpu_dev);
1280
1281 if (!np) {
1282 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
1283 return -ENOENT;
1284 }
1285
1286 cpumask_set_cpu(cpu_dev->id, cpumask);
1287
1288 /* OPPs are shared ? */
1289 if (!of_property_read_bool(np, "opp-shared"))
1290 return 0;
1291
1292 for_each_possible_cpu(cpu) {
1293 if (cpu == cpu_dev->id)
1294 continue;
1295
1296 struct device_node *cpu_np __free(device_node) =
1297 of_cpu_device_node_get(cpu);
1298
1299 if (!cpu_np) {
1300 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
1301 __func__, cpu);
1302 return -ENOENT;
1303 }
1304
1305 /* Get OPP descriptor node */
1306 struct device_node *tmp_np __free(device_node) =
1307 _opp_of_get_opp_desc_node(cpu_np, 0);
1308
1309 if (!tmp_np) {
1310 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
1311 return -ENOENT;
1312 }
1313
1314 /* CPUs are sharing opp node */
1315 if (np == tmp_np)
1316 cpumask_set_cpu(cpu, cpumask);
1317 }
1318
1319 return 0;
1320 }
1321 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
1322
1323 /**
1324 * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
1325 * @np: Node that contains the "required-opps" property.
1326 * @index: Index of the phandle to parse.
1327 *
1328 * Returns the performance state of the OPP pointed out by the "required-opps"
1329 * property at @index in @np.
1330 *
1331 * Return: Zero or positive performance state on success, otherwise negative
1332 * value on errors.
1333 */
of_get_required_opp_performance_state(struct device_node * np,int index)1334 int of_get_required_opp_performance_state(struct device_node *np, int index)
1335 {
1336 int pstate = -EINVAL;
1337
1338 struct device_node *required_np __free(device_node) =
1339 of_parse_required_opp(np, index);
1340
1341 if (!required_np)
1342 return -ENODEV;
1343
1344 struct opp_table *opp_table __free(put_opp_table) =
1345 _find_table_of_opp_np(required_np);
1346
1347 if (IS_ERR(opp_table)) {
1348 pr_err("%s: Failed to find required OPP table %pOF: %pe\n",
1349 __func__, np, opp_table);
1350 return PTR_ERR(opp_table);
1351 }
1352
1353 /* The OPP tables must belong to a genpd */
1354 if (unlikely(!opp_table->is_genpd)) {
1355 pr_err("%s: Performance state is only valid for genpds.\n", __func__);
1356 return -EINVAL;
1357 }
1358
1359 struct dev_pm_opp *opp __free(put_opp) =
1360 _find_opp_of_np(opp_table, required_np);
1361
1362 if (opp) {
1363 if (opp->level == OPP_LEVEL_UNSET) {
1364 pr_err("%s: OPP levels aren't available for %pOF\n",
1365 __func__, np);
1366 } else {
1367 pstate = opp->level;
1368 }
1369 }
1370
1371 return pstate;
1372 }
1373 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
1374
1375 /**
1376 * dev_pm_opp_of_has_required_opp - Find out if a required-opps exists.
1377 * @dev: The device to investigate.
1378 *
1379 * Returns true if the device's node has a "operating-points-v2" property and if
1380 * the corresponding node for the opp-table describes opp nodes that uses the
1381 * "required-opps" property.
1382 *
1383 * Return: True if a required-opps is present, else false.
1384 */
dev_pm_opp_of_has_required_opp(struct device * dev)1385 bool dev_pm_opp_of_has_required_opp(struct device *dev)
1386 {
1387 int count;
1388
1389 struct device_node *opp_np __free(device_node) =
1390 _opp_of_get_opp_desc_node(dev->of_node, 0);
1391
1392 if (!opp_np)
1393 return false;
1394
1395 struct device_node *np __free(device_node) =
1396 of_get_next_available_child(opp_np, NULL);
1397
1398 if (!np) {
1399 dev_warn(dev, "Empty OPP table\n");
1400 return false;
1401 }
1402
1403 count = of_count_phandle_with_args(np, "required-opps", NULL);
1404
1405 return count > 0;
1406 }
1407
1408 /**
1409 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1410 * @opp: opp for which DT node has to be returned for
1411 *
1412 * Return: DT node corresponding to the opp, else 0 on success.
1413 *
1414 * The caller needs to put the node with of_node_put() after using it.
1415 */
dev_pm_opp_get_of_node(struct dev_pm_opp * opp)1416 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1417 {
1418 if (IS_ERR_OR_NULL(opp)) {
1419 pr_err("%s: Invalid parameters\n", __func__);
1420 return NULL;
1421 }
1422
1423 return of_node_get(opp->np);
1424 }
1425 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
1426
1427 /*
1428 * Callback function provided to the Energy Model framework upon registration.
1429 * It provides the power used by @dev at @kHz if it is the frequency of an
1430 * existing OPP, or at the frequency of the first OPP above @kHz otherwise
1431 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1432 * frequency and @uW to the associated power.
1433 *
1434 * Returns 0 on success or a proper -EINVAL value in case of error.
1435 */
1436 static int __maybe_unused
_get_dt_power(struct device * dev,unsigned long * uW,unsigned long * kHz)1437 _get_dt_power(struct device *dev, unsigned long *uW, unsigned long *kHz)
1438 {
1439 unsigned long opp_freq, opp_power;
1440
1441 /* Find the right frequency and related OPP */
1442 opp_freq = *kHz * 1000;
1443
1444 struct dev_pm_opp *opp __free(put_opp) =
1445 dev_pm_opp_find_freq_ceil(dev, &opp_freq);
1446
1447 if (IS_ERR(opp))
1448 return -EINVAL;
1449
1450 opp_power = dev_pm_opp_get_power(opp);
1451 if (!opp_power)
1452 return -EINVAL;
1453
1454 *kHz = opp_freq / 1000;
1455 *uW = opp_power;
1456
1457 return 0;
1458 }
1459
1460 /**
1461 * dev_pm_opp_calc_power() - Calculate power value for device with EM
1462 * @dev : Device for which an Energy Model has to be registered
1463 * @uW : New power value that is calculated
1464 * @kHz : Frequency for which the new power is calculated
1465 *
1466 * This computes the power estimated by @dev at @kHz if it is the frequency
1467 * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
1468 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1469 * frequency and @uW to the associated power. The power is estimated as
1470 * P = C * V^2 * f with C being the device's capacitance and V and f
1471 * respectively the voltage and frequency of the OPP.
1472 * It is also used as a callback function provided to the Energy Model
1473 * framework upon registration.
1474 *
1475 * Returns -EINVAL if the power calculation failed because of missing
1476 * parameters, 0 otherwise.
1477 */
dev_pm_opp_calc_power(struct device * dev,unsigned long * uW,unsigned long * kHz)1478 int dev_pm_opp_calc_power(struct device *dev, unsigned long *uW,
1479 unsigned long *kHz)
1480 {
1481 unsigned long mV, Hz;
1482 u32 cap;
1483 u64 tmp;
1484 int ret;
1485
1486 struct device_node *np __free(device_node) = of_node_get(dev->of_node);
1487
1488 if (!np)
1489 return -EINVAL;
1490
1491 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1492 if (ret)
1493 return -EINVAL;
1494
1495 Hz = *kHz * 1000;
1496
1497 struct dev_pm_opp *opp __free(put_opp) =
1498 dev_pm_opp_find_freq_ceil(dev, &Hz);
1499
1500 if (IS_ERR(opp))
1501 return -EINVAL;
1502
1503 mV = dev_pm_opp_get_voltage(opp) / 1000;
1504 if (!mV)
1505 return -EINVAL;
1506
1507 tmp = (u64)cap * mV * mV * (Hz / 1000000);
1508 /* Provide power in micro-Watts */
1509 do_div(tmp, 1000000);
1510
1511 *uW = (unsigned long)tmp;
1512 *kHz = Hz / 1000;
1513
1514 return 0;
1515 }
1516 EXPORT_SYMBOL_GPL(dev_pm_opp_calc_power);
1517
_of_has_opp_microwatt_property(struct device * dev)1518 static bool _of_has_opp_microwatt_property(struct device *dev)
1519 {
1520 unsigned long freq = 0;
1521
1522 /* Check if at least one OPP has needed property */
1523 struct dev_pm_opp *opp __free(put_opp) =
1524 dev_pm_opp_find_freq_ceil(dev, &freq);
1525
1526 if (IS_ERR(opp))
1527 return false;
1528
1529 return !!dev_pm_opp_get_power(opp);
1530 }
1531
1532 /**
1533 * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1534 * @dev : Device for which an Energy Model has to be registered
1535 * @cpus : CPUs for which an Energy Model has to be registered. For
1536 * other type of devices it should be set to NULL.
1537 *
1538 * This checks whether the "dynamic-power-coefficient" devicetree property has
1539 * been specified, and tries to register an Energy Model with it if it has.
1540 * Having this property means the voltages are known for OPPs and the EM
1541 * might be calculated.
1542 */
dev_pm_opp_of_register_em(struct device * dev,struct cpumask * cpus)1543 int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus)
1544 {
1545 struct em_data_callback em_cb;
1546 int ret, nr_opp;
1547 u32 cap;
1548
1549 if (IS_ERR_OR_NULL(dev))
1550 return -EINVAL;
1551
1552 struct device_node *np __free(device_node) = of_node_get(dev->of_node);
1553
1554 if (!np) {
1555 ret = -EINVAL;
1556 goto failed;
1557 }
1558
1559 nr_opp = dev_pm_opp_get_opp_count(dev);
1560 if (nr_opp <= 0) {
1561 ret = -EINVAL;
1562 goto failed;
1563 }
1564
1565 /* First, try to find more precised Energy Model in DT */
1566 if (_of_has_opp_microwatt_property(dev)) {
1567 EM_SET_ACTIVE_POWER_CB(em_cb, _get_dt_power);
1568 goto register_em;
1569 }
1570
1571 /*
1572 * Register an EM only if the 'dynamic-power-coefficient' property is
1573 * set in devicetree. It is assumed the voltage values are known if that
1574 * property is set since it is useless otherwise. If voltages are not
1575 * known, just let the EM registration fail with an error to alert the
1576 * user about the inconsistent configuration.
1577 */
1578 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1579 if (ret || !cap) {
1580 dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n");
1581 ret = -EINVAL;
1582 goto failed;
1583 }
1584
1585 EM_SET_ACTIVE_POWER_CB(em_cb, dev_pm_opp_calc_power);
1586
1587 register_em:
1588 ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus, true);
1589 if (ret)
1590 goto failed;
1591
1592 return 0;
1593
1594 failed:
1595 dev_dbg(dev, "Couldn't register Energy Model %d\n", ret);
1596 return ret;
1597 }
1598 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);
1599