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