blob: 62504b18f1985e9bd5beb5024d07236cf46a9d79 [file] [log] [blame]
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301/*
2 * Generic OPP OF helpers
3 *
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/cpu.h>
17#include <linux/errno.h>
18#include <linux/device.h>
Sudeep Holla98679992017-10-12 11:32:23 +010019#include <linux/of_device.h>
Viresh Kumar3ba98322016-11-18 15:47:46 +053020#include <linux/pm_domain.h>
Viresh Kumardfbe4672016-12-01 16:28:19 +053021#include <linux/slab.h>
Viresh Kumarf47b72a2016-05-05 16:20:33 +053022#include <linux/export.h>
Quentin Perreta4f342b2019-02-04 11:09:48 +000023#include <linux/energy_model.h>
Viresh Kumarf47b72a2016-05-05 16:20:33 +053024
25#include "opp.h"
26
Viresh Kumarf06ed902018-06-14 12:04:43 +053027/*
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
Viresh Kumar283d55e2018-09-07 09:01:54 +053045struct opp_table *_managed_opp(struct device *dev, int index)
Viresh Kumarf47b72a2016-05-05 16:20:33 +053046{
Viresh Kumarb83c1892017-01-23 10:11:45 +053047 struct opp_table *opp_table, *managed_table = NULL;
Viresh Kumar283d55e2018-09-07 09:01:54 +053048 struct device_node *np;
Viresh Kumarb83c1892017-01-23 10:11:45 +053049
Viresh Kumar283d55e2018-09-07 09:01:54 +053050 np = _opp_of_get_opp_desc_node(dev->of_node, index);
51 if (!np)
52 return NULL;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053053
Viresh Kumar052c6f12017-01-23 10:11:49 +053054 list_for_each_entry(opp_table, &opp_tables, node) {
Viresh Kumarf47b72a2016-05-05 16:20:33 +053055 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 */
Viresh Kumarb83c1892017-01-23 10:11:45 +053063 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
64 _get_opp_table_kref(opp_table);
65 managed_table = opp_table;
66 }
Viresh Kumar79ee2e82016-06-16 19:03:11 +053067
Viresh Kumarb83c1892017-01-23 10:11:45 +053068 break;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053069 }
70 }
71
Viresh Kumar283d55e2018-09-07 09:01:54 +053072 of_node_put(np);
Viresh Kumarb83c1892017-01-23 10:11:45 +053073
74 return managed_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053075}
76
Viresh Kumar5d6d1062018-06-07 14:50:43 +053077/* 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
83 lockdep_assert_held(&opp_table_lock);
84
85 mutex_lock(&opp_table->lock);
86
87 list_for_each_entry(opp, &opp_table->opp_list, node) {
88 if (opp->np == opp_np) {
89 dev_pm_opp_get(opp);
90 mutex_unlock(&opp_table->lock);
91 return opp;
92 }
93 }
94
95 mutex_unlock(&opp_table->lock);
96
97 return NULL;
98}
99
100static struct device_node *of_parse_required_opp(struct device_node *np,
101 int index)
102{
103 struct device_node *required_np;
104
105 required_np = of_parse_phandle(np, "required-opps", index);
106 if (unlikely(!required_np)) {
107 pr_err("%s: Unable to parse required-opps: %pOF, index: %d\n",
108 __func__, np, index);
109 }
110
111 return required_np;
112}
113
114/* The caller must call dev_pm_opp_put_opp_table() after the table is used */
115static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np)
116{
117 struct opp_table *opp_table;
Viresh Kumar699e21e2018-11-22 11:04:00 +0530118 struct device_node *opp_table_np;
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530119
120 lockdep_assert_held(&opp_table_lock);
121
Viresh Kumar699e21e2018-11-22 11:04:00 +0530122 opp_table_np = of_get_parent(opp_np);
123 if (!opp_table_np)
124 goto err;
125
126 /* It is safe to put the node now as all we need now is its address */
127 of_node_put(opp_table_np);
128
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530129 list_for_each_entry(opp_table, &opp_tables, node) {
Viresh Kumar699e21e2018-11-22 11:04:00 +0530130 if (opp_table_np == opp_table->np) {
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530131 _get_opp_table_kref(opp_table);
132 return opp_table;
133 }
134 }
135
Viresh Kumar699e21e2018-11-22 11:04:00 +0530136err:
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530137 return ERR_PTR(-ENODEV);
138}
139
140/* Free resources previously acquired by _opp_table_alloc_required_tables() */
141static void _opp_table_free_required_tables(struct opp_table *opp_table)
142{
143 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530144 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530145 int i;
146
147 if (!required_opp_tables)
148 return;
149
150 for (i = 0; i < opp_table->required_opp_count; i++) {
151 if (IS_ERR_OR_NULL(required_opp_tables[i]))
152 break;
153
154 dev_pm_opp_put_opp_table(required_opp_tables[i]);
155 }
156
157 kfree(required_opp_tables);
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530158 kfree(genpd_virt_devs);
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530159
160 opp_table->required_opp_count = 0;
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530161 opp_table->genpd_virt_devs = NULL;
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530162 opp_table->required_opp_tables = NULL;
163}
164
165/*
166 * Populate all devices and opp tables which are part of "required-opps" list.
167 * Checking only the first OPP node should be enough.
168 */
169static void _opp_table_alloc_required_tables(struct opp_table *opp_table,
170 struct device *dev,
171 struct device_node *opp_np)
172{
173 struct opp_table **required_opp_tables;
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530174 struct device **genpd_virt_devs = NULL;
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530175 struct device_node *required_np, *np;
176 int count, i;
177
178 /* Traversing the first OPP node is all we need */
179 np = of_get_next_available_child(opp_np, NULL);
180 if (!np) {
181 dev_err(dev, "Empty OPP table\n");
182 return;
183 }
184
185 count = of_count_phandle_with_args(np, "required-opps", NULL);
186 if (!count)
187 goto put_np;
188
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530189 if (count > 1) {
190 genpd_virt_devs = kcalloc(count, sizeof(*genpd_virt_devs),
191 GFP_KERNEL);
192 if (!genpd_virt_devs)
193 goto put_np;
194 }
195
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530196 required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
197 GFP_KERNEL);
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530198 if (!required_opp_tables) {
199 kfree(genpd_virt_devs);
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530200 goto put_np;
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530201 }
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530202
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530203 opp_table->genpd_virt_devs = genpd_virt_devs;
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530204 opp_table->required_opp_tables = required_opp_tables;
205 opp_table->required_opp_count = count;
206
207 for (i = 0; i < count; i++) {
208 required_np = of_parse_required_opp(np, i);
209 if (!required_np)
210 goto free_required_tables;
211
212 required_opp_tables[i] = _find_table_of_opp_np(required_np);
213 of_node_put(required_np);
214
215 if (IS_ERR(required_opp_tables[i]))
216 goto free_required_tables;
217
218 /*
219 * We only support genpd's OPPs in the "required-opps" for now,
220 * as we don't know how much about other cases. Error out if the
221 * required OPP doesn't belong to a genpd.
222 */
223 if (!required_opp_tables[i]->is_genpd) {
224 dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",
225 required_np);
226 goto free_required_tables;
227 }
228 }
229
230 goto put_np;
231
232free_required_tables:
233 _opp_table_free_required_tables(opp_table);
234put_np:
235 of_node_put(np);
236}
237
Viresh Kumareb7c87432018-09-05 16:17:14 +0530238void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
239 int index)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530240{
Viresh Kumarf06ed902018-06-14 12:04:43 +0530241 struct device_node *np, *opp_np;
242 u32 val;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530243
244 /*
245 * Only required for backward compatibility with v1 bindings, but isn't
246 * harmful for other cases. And so we do it unconditionally.
247 */
248 np = of_node_get(dev->of_node);
Viresh Kumarf06ed902018-06-14 12:04:43 +0530249 if (!np)
250 return;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530251
Viresh Kumarf06ed902018-06-14 12:04:43 +0530252 if (!of_property_read_u32(np, "clock-latency", &val))
253 opp_table->clock_latency_ns_max = val;
254 of_property_read_u32(np, "voltage-tolerance",
255 &opp_table->voltage_tolerance_v1);
256
Viresh Kumar61d8e7c2018-06-13 16:00:11 +0530257 if (of_find_property(np, "#power-domain-cells", NULL))
258 opp_table->is_genpd = true;
259
Viresh Kumarf06ed902018-06-14 12:04:43 +0530260 /* Get OPP table node */
261 opp_np = _opp_of_get_opp_desc_node(np, index);
262 of_node_put(np);
263
264 if (!opp_np)
265 return;
266
267 if (of_property_read_bool(opp_np, "opp-shared"))
268 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
269 else
270 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
271
272 opp_table->np = opp_np;
273
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530274 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
Viresh Kumarf06ed902018-06-14 12:04:43 +0530275 of_node_put(opp_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530276}
277
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530278void _of_clear_opp_table(struct opp_table *opp_table)
279{
280 _opp_table_free_required_tables(opp_table);
281}
282
Viresh Kumarda544b62018-06-07 14:50:43 +0530283/*
284 * Release all resources previously acquired with a call to
285 * _of_opp_alloc_required_opps().
286 */
287void _of_opp_free_required_opps(struct opp_table *opp_table,
288 struct dev_pm_opp *opp)
289{
290 struct dev_pm_opp **required_opps = opp->required_opps;
291 int i;
292
293 if (!required_opps)
294 return;
295
296 for (i = 0; i < opp_table->required_opp_count; i++) {
297 if (!required_opps[i])
298 break;
299
300 /* Put the reference back */
301 dev_pm_opp_put(required_opps[i]);
302 }
303
304 kfree(required_opps);
305 opp->required_opps = NULL;
306}
307
308/* Populate all required OPPs which are part of "required-opps" list */
309static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
310 struct dev_pm_opp *opp)
311{
312 struct dev_pm_opp **required_opps;
313 struct opp_table *required_table;
314 struct device_node *np;
315 int i, ret, count = opp_table->required_opp_count;
316
317 if (!count)
318 return 0;
319
320 required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);
321 if (!required_opps)
322 return -ENOMEM;
323
324 opp->required_opps = required_opps;
325
326 for (i = 0; i < count; i++) {
327 required_table = opp_table->required_opp_tables[i];
328
329 np = of_parse_required_opp(opp->np, i);
330 if (unlikely(!np)) {
331 ret = -ENODEV;
332 goto free_required_opps;
333 }
334
335 required_opps[i] = _find_opp_of_np(required_table, np);
336 of_node_put(np);
337
338 if (!required_opps[i]) {
339 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
340 __func__, opp->np, i);
341 ret = -ENODEV;
342 goto free_required_opps;
343 }
344 }
345
346 return 0;
347
348free_required_opps:
349 _of_opp_free_required_opps(opp_table, opp);
350
351 return ret;
352}
353
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530354static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
355 struct device_node *np)
356{
357 unsigned int count = opp_table->supported_hw_count;
358 u32 version;
359 int ret;
360
Dave Gerlacha4ee4542016-09-23 15:07:47 -0500361 if (!opp_table->supported_hw) {
362 /*
363 * In the case that no supported_hw has been set by the
364 * platform but there is an opp-supported-hw value set for
365 * an OPP then the OPP should not be enabled as there is
366 * no way to see if the hardware supports it.
367 */
368 if (of_find_property(np, "opp-supported-hw", NULL))
369 return false;
370 else
371 return true;
372 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530373
374 while (count--) {
375 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
376 &version);
377 if (ret) {
378 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
379 __func__, count, ret);
380 return false;
381 }
382
383 /* Both of these are bitwise masks of the versions */
384 if (!(version & opp_table->supported_hw[count]))
385 return false;
386 }
387
388 return true;
389}
390
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530391static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
392 struct opp_table *opp_table)
393{
Viresh Kumardfbe4672016-12-01 16:28:19 +0530394 u32 *microvolt, *microamp = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +0530395 int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530396 struct property *prop = NULL;
397 char name[NAME_MAX];
398
399 /* Search for "opp-microvolt-<name>" */
400 if (opp_table->prop_name) {
401 snprintf(name, sizeof(name), "opp-microvolt-%s",
402 opp_table->prop_name);
403 prop = of_find_property(opp->np, name, NULL);
404 }
405
406 if (!prop) {
407 /* Search for "opp-microvolt" */
408 sprintf(name, "opp-microvolt");
409 prop = of_find_property(opp->np, name, NULL);
410
411 /* Missing property isn't a problem, but an invalid entry is */
Viresh Kumar688a48b2017-05-23 09:32:12 +0530412 if (!prop) {
Viresh Kumar46f48ac2018-12-11 16:39:36 +0530413 if (unlikely(supplies == -1)) {
414 /* Initialize regulator_count */
415 opp_table->regulator_count = 0;
416 return 0;
417 }
418
419 if (!supplies)
Viresh Kumar688a48b2017-05-23 09:32:12 +0530420 return 0;
421
422 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
423 __func__);
424 return -EINVAL;
425 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530426 }
427
Viresh Kumar46f48ac2018-12-11 16:39:36 +0530428 if (unlikely(supplies == -1)) {
429 /* Initialize regulator_count */
430 supplies = opp_table->regulator_count = 1;
431 } else if (unlikely(!supplies)) {
432 dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__);
433 return -EINVAL;
434 }
435
Viresh Kumardfbe4672016-12-01 16:28:19 +0530436 vcount = of_property_count_u32_elems(opp->np, name);
437 if (vcount < 0) {
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530438 dev_err(dev, "%s: Invalid %s property (%d)\n",
Viresh Kumardfbe4672016-12-01 16:28:19 +0530439 __func__, name, vcount);
440 return vcount;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530441 }
442
Viresh Kumardfbe4672016-12-01 16:28:19 +0530443 /* There can be one or three elements per supply */
444 if (vcount != supplies && vcount != supplies * 3) {
445 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
446 __func__, name, vcount, supplies);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530447 return -EINVAL;
448 }
449
Viresh Kumardfbe4672016-12-01 16:28:19 +0530450 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
451 if (!microvolt)
452 return -ENOMEM;
453
454 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530455 if (ret) {
456 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530457 ret = -EINVAL;
458 goto free_microvolt;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530459 }
460
461 /* Search for "opp-microamp-<name>" */
462 prop = NULL;
463 if (opp_table->prop_name) {
464 snprintf(name, sizeof(name), "opp-microamp-%s",
465 opp_table->prop_name);
466 prop = of_find_property(opp->np, name, NULL);
467 }
468
469 if (!prop) {
470 /* Search for "opp-microamp" */
471 sprintf(name, "opp-microamp");
472 prop = of_find_property(opp->np, name, NULL);
473 }
474
Viresh Kumardfbe4672016-12-01 16:28:19 +0530475 if (prop) {
476 icount = of_property_count_u32_elems(opp->np, name);
477 if (icount < 0) {
478 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
479 name, icount);
480 ret = icount;
481 goto free_microvolt;
482 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530483
Viresh Kumardfbe4672016-12-01 16:28:19 +0530484 if (icount != supplies) {
485 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
486 __func__, name, icount, supplies);
487 ret = -EINVAL;
488 goto free_microvolt;
489 }
490
491 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
492 if (!microamp) {
493 ret = -EINVAL;
494 goto free_microvolt;
495 }
496
497 ret = of_property_read_u32_array(opp->np, name, microamp,
498 icount);
499 if (ret) {
500 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
501 name, ret);
502 ret = -EINVAL;
503 goto free_microamp;
504 }
505 }
506
507 for (i = 0, j = 0; i < supplies; i++) {
508 opp->supplies[i].u_volt = microvolt[j++];
509
510 if (vcount == supplies) {
511 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
512 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
513 } else {
514 opp->supplies[i].u_volt_min = microvolt[j++];
515 opp->supplies[i].u_volt_max = microvolt[j++];
516 }
517
518 if (microamp)
519 opp->supplies[i].u_amp = microamp[i];
520 }
521
522free_microamp:
523 kfree(microamp);
524free_microvolt:
525 kfree(microvolt);
526
527 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530528}
529
530/**
531 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
532 * entries
533 * @dev: device pointer used to lookup OPP table.
534 *
535 * Free OPPs created using static entries present in DT.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530536 */
537void dev_pm_opp_of_remove_table(struct device *dev)
538{
Viresh Kumar2a4eb732018-09-13 13:14:36 +0530539 _dev_pm_opp_find_and_remove_table(dev);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530540}
541EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
542
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530543/**
544 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530545 * @opp_table: OPP table
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530546 * @dev: device for which we do this operation
547 * @np: device node
548 *
549 * This function adds an opp definition to the opp table and returns status. The
550 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
551 * removed by dev_pm_opp_remove.
552 *
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530553 * Return:
Dave Gerlachdeac8702018-10-03 16:13:15 +0530554 * Valid OPP pointer:
555 * On success
556 * NULL:
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530557 * Duplicate OPPs (both freq and volt are same) and opp->available
Dave Gerlachdeac8702018-10-03 16:13:15 +0530558 * OR if the OPP is not supported by hardware.
559 * ERR_PTR(-EEXIST):
560 * Freq are same and volt are different OR
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530561 * Duplicate OPPs (both freq and volt are same) and !opp->available
Dave Gerlachdeac8702018-10-03 16:13:15 +0530562 * ERR_PTR(-ENOMEM):
563 * Memory allocation failure
564 * ERR_PTR(-EINVAL):
565 * Failed parsing the OPP node
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530566 */
Dave Gerlachdeac8702018-10-03 16:13:15 +0530567static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
568 struct device *dev, struct device_node *np)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530569{
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530570 struct dev_pm_opp *new_opp;
Dan Carpenter6a89e012018-05-16 12:52:41 +0300571 u64 rate = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530572 u32 val;
573 int ret;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530574 bool rate_not_available = false;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530575
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530576 new_opp = _opp_allocate(opp_table);
577 if (!new_opp)
Dave Gerlachdeac8702018-10-03 16:13:15 +0530578 return ERR_PTR(-ENOMEM);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530579
580 ret = of_property_read_u64(np, "opp-hz", &rate);
581 if (ret < 0) {
Viresh Kumara1e8c132018-04-06 14:35:45 +0530582 /* "opp-hz" is optional for devices like power domains. */
Viresh Kumar61d8e7c2018-06-13 16:00:11 +0530583 if (!opp_table->is_genpd) {
Viresh Kumara1e8c132018-04-06 14:35:45 +0530584 dev_err(dev, "%s: opp-hz not found\n", __func__);
585 goto free_opp;
586 }
587
588 rate_not_available = true;
589 } else {
590 /*
591 * Rate is defined as an unsigned long in clk API, and so
592 * casting explicitly to its type. Must be fixed once rate is 64
593 * bit guaranteed in clk API.
594 */
595 new_opp->rate = (unsigned long)rate;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530596 }
597
Rajendra Nayak5b93ac52019-01-10 09:32:02 +0530598 of_property_read_u32(np, "opp-level", &new_opp->level);
599
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530600 /* Check if the OPP supports hardware's hierarchy of versions or not */
601 if (!_opp_is_supported(dev, opp_table, np)) {
602 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
603 goto free_opp;
604 }
605
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530606 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
607
608 new_opp->np = np;
609 new_opp->dynamic = false;
610 new_opp->available = true;
611
Viresh Kumarda544b62018-06-07 14:50:43 +0530612 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
613 if (ret)
614 goto free_opp;
615
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530616 if (!of_property_read_u32(np, "clock-latency-ns", &val))
617 new_opp->clock_latency_ns = val;
618
619 ret = opp_parse_supplies(new_opp, dev, opp_table);
620 if (ret)
Viresh Kumarda544b62018-06-07 14:50:43 +0530621 goto free_required_opps;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530622
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530623 if (opp_table->is_genpd)
624 new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530625
Viresh Kumara1e8c132018-04-06 14:35:45 +0530626 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530627 if (ret) {
628 /* Don't return error for duplicate OPPs */
629 if (ret == -EBUSY)
630 ret = 0;
Viresh Kumarda544b62018-06-07 14:50:43 +0530631 goto free_required_opps;
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530632 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530633
634 /* OPP to select on device suspend */
635 if (of_property_read_bool(np, "opp-suspend")) {
636 if (opp_table->suspend_opp) {
637 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
638 __func__, opp_table->suspend_opp->rate,
639 new_opp->rate);
640 } else {
641 new_opp->suspend = true;
642 opp_table->suspend_opp = new_opp;
643 }
644 }
645
646 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
647 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
648
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530649 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
Viresh Kumar0f0fe7e2016-12-01 16:28:17 +0530650 __func__, new_opp->turbo, new_opp->rate,
Viresh Kumardfbe4672016-12-01 16:28:19 +0530651 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
652 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530653
654 /*
655 * Notify the changes in the availability of the operable
656 * frequency/voltage list.
657 */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530658 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Dave Gerlachdeac8702018-10-03 16:13:15 +0530659 return new_opp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530660
Viresh Kumarda544b62018-06-07 14:50:43 +0530661free_required_opps:
662 _of_opp_free_required_opps(opp_table, new_opp);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530663free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530664 _opp_free(new_opp);
665
Dave Gerlachdeac8702018-10-03 16:13:15 +0530666 return ERR_PTR(ret);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530667}
668
669/* Initializes OPP tables based on new bindings */
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530670static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530671{
672 struct device_node *np;
Viresh Kumar283d55e2018-09-07 09:01:54 +0530673 int ret, count = 0, pstate_count = 0;
Viresh Kumar3ba98322016-11-18 15:47:46 +0530674 struct dev_pm_opp *opp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530675
Viresh Kumar283d55e2018-09-07 09:01:54 +0530676 /* OPP table is already initialized for the device */
677 if (opp_table->parsed_static_opps) {
678 kref_get(&opp_table->list_kref);
679 return 0;
680 }
681
Viresh Kumard0e8ae62018-09-11 11:14:11 +0530682 kref_init(&opp_table->list_kref);
683
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530684 /* We have opp-table node now, iterate over it and add OPPs */
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530685 for_each_available_child_of_node(opp_table->np, np) {
Dave Gerlachdeac8702018-10-03 16:13:15 +0530686 opp = _opp_add_static_v2(opp_table, dev, np);
687 if (IS_ERR(opp)) {
688 ret = PTR_ERR(opp);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530689 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
690 ret);
Tobias Jordan7978db32017-10-04 11:35:03 +0530691 of_node_put(np);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530692 goto put_list_kref;
Dave Gerlachdeac8702018-10-03 16:13:15 +0530693 } else if (opp) {
694 count++;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530695 }
696 }
697
698 /* There should be one of more OPP defined */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530699 if (WARN_ON(!count)) {
700 ret = -ENOENT;
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530701 goto put_list_kref;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530702 }
703
Viresh Kumar3ba98322016-11-18 15:47:46 +0530704 list_for_each_entry(opp, &opp_table->opp_list, node)
705 pstate_count += !!opp->pstate;
706
707 /* Either all or none of the nodes shall have performance state set */
708 if (pstate_count && pstate_count != count) {
709 dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
710 count, pstate_count);
711 ret = -ENOENT;
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530712 goto put_list_kref;
Viresh Kumar3ba98322016-11-18 15:47:46 +0530713 }
714
715 if (pstate_count)
716 opp_table->genpd_performance_state = true;
717
Viresh Kumarf06ed902018-06-14 12:04:43 +0530718 opp_table->parsed_static_opps = true;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530719
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530720 return 0;
721
722put_list_kref:
723 _put_opp_list_kref(opp_table);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530724
725 return ret;
726}
727
728/* Initializes OPP tables based on old-deprecated bindings */
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530729static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530730{
731 const struct property *prop;
732 const __be32 *val;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530733 int nr, ret = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530734
735 prop = of_find_property(dev->of_node, "operating-points", NULL);
736 if (!prop)
737 return -ENODEV;
738 if (!prop->value)
739 return -ENODATA;
740
741 /*
742 * Each OPP is a set of tuples consisting of frequency and
743 * voltage like <freq-kHz vol-uV>.
744 */
745 nr = prop->length / sizeof(u32);
746 if (nr % 2) {
747 dev_err(dev, "%s: Invalid OPP table\n", __func__);
748 return -EINVAL;
749 }
750
Viresh Kumard0e8ae62018-09-11 11:14:11 +0530751 kref_init(&opp_table->list_kref);
752
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530753 val = prop->value;
754 while (nr) {
755 unsigned long freq = be32_to_cpup(val++) * 1000;
756 unsigned long volt = be32_to_cpup(val++);
757
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530758 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
Viresh Kumar04a86a82017-01-02 14:40:58 +0530759 if (ret) {
760 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
761 __func__, freq, ret);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530762 _put_opp_list_kref(opp_table);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530763 return ret;
Viresh Kumar04a86a82017-01-02 14:40:58 +0530764 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530765 nr -= 2;
766 }
767
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530768 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530769}
770
771/**
772 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
773 * @dev: device pointer used to lookup OPP table.
774 *
775 * Register the initial OPP table with the OPP library for given device.
776 *
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530777 * Return:
778 * 0 On success OR
779 * Duplicate OPPs (both freq and volt are same) and opp->available
780 * -EEXIST Freq are same and volt are different OR
781 * Duplicate OPPs (both freq and volt are same) and !opp->available
782 * -ENOMEM Memory allocation failure
783 * -ENODEV when 'operating-points' property is not found or is invalid data
784 * in device node.
785 * -ENODATA when empty 'operating-points' property is found
786 * -EINVAL when invalid entries are found in opp-v2 table
787 */
788int dev_pm_opp_of_add_table(struct device *dev)
789{
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530790 struct opp_table *opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530791 int ret;
792
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530793 opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0);
794 if (!opp_table)
795 return -ENOMEM;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530796
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530797 /*
798 * OPPs have two version of bindings now. Also try the old (v1)
799 * bindings for backward compatibility with older dtbs.
800 */
801 if (opp_table->np)
802 ret = _of_add_opp_table_v2(dev, opp_table);
803 else
804 ret = _of_add_opp_table_v1(dev, opp_table);
805
806 if (ret)
807 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530808
809 return ret;
810}
811EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
812
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530813/**
814 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
815 * @dev: device pointer used to lookup OPP table.
816 * @index: Index number.
817 *
818 * Register the initial OPP table with the OPP library for given device only
819 * using the "operating-points-v2" property.
820 *
821 * Return:
822 * 0 On success OR
823 * Duplicate OPPs (both freq and volt are same) and opp->available
824 * -EEXIST Freq are same and volt are different OR
825 * Duplicate OPPs (both freq and volt are same) and !opp->available
826 * -ENOMEM Memory allocation failure
827 * -ENODEV when 'operating-points' property is not found or is invalid data
828 * in device node.
829 * -ENODATA when empty 'operating-points' property is found
830 * -EINVAL when invalid entries are found in opp-v2 table
831 */
832int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
833{
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530834 struct opp_table *opp_table;
Viresh Kumar8a352fd2018-05-30 15:19:38 +0530835 int ret, count;
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530836
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530837 if (index) {
Viresh Kumar8a352fd2018-05-30 15:19:38 +0530838 /*
839 * If only one phandle is present, then the same OPP table
840 * applies for all index requests.
841 */
842 count = of_count_phandle_with_args(dev->of_node,
843 "operating-points-v2", NULL);
Viresh Kumar3e27c792018-11-23 10:36:07 +0530844 if (count == 1)
845 index = 0;
Viresh Kumar8a352fd2018-05-30 15:19:38 +0530846 }
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530847
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530848 opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
849 if (!opp_table)
850 return -ENOMEM;
851
852 ret = _of_add_opp_table_v2(dev, opp_table);
853 if (ret)
854 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530855
856 return ret;
857}
858EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
859
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530860/* CPU device specific helpers */
861
862/**
863 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
864 * @cpumask: cpumask for which OPP table needs to be removed
865 *
866 * This removes the OPP tables for CPUs present in the @cpumask.
867 * This should be used only to remove static entries created from DT.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530868 */
869void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
870{
Viresh Kumar2a4eb732018-09-13 13:14:36 +0530871 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530872}
873EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
874
875/**
876 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
877 * @cpumask: cpumask for which OPP table needs to be added.
878 *
879 * This adds the OPP tables for CPUs present in the @cpumask.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530880 */
881int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
882{
883 struct device *cpu_dev;
Viresh Kumar50b6b872018-10-03 15:12:06 +0530884 int cpu, ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530885
Viresh Kumar50b6b872018-10-03 15:12:06 +0530886 if (WARN_ON(cpumask_empty(cpumask)))
887 return -ENODEV;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530888
889 for_each_cpu(cpu, cpumask) {
890 cpu_dev = get_cpu_device(cpu);
891 if (!cpu_dev) {
892 pr_err("%s: failed to get cpu%d device\n", __func__,
893 cpu);
Viresh Kumar50b6b872018-10-03 15:12:06 +0530894 ret = -ENODEV;
895 goto remove_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530896 }
897
898 ret = dev_pm_opp_of_add_table(cpu_dev);
899 if (ret) {
Viresh Kumar5b606972017-07-12 09:21:49 +0530900 /*
901 * OPP may get registered dynamically, don't print error
902 * message here.
903 */
904 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
905 __func__, cpu, ret);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530906
Viresh Kumar50b6b872018-10-03 15:12:06 +0530907 goto remove_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530908 }
909 }
910
Viresh Kumar50b6b872018-10-03 15:12:06 +0530911 return 0;
912
913remove_table:
914 /* Free all other OPPs */
915 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
916
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530917 return ret;
918}
919EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
920
921/*
922 * Works only for OPP v2 bindings.
923 *
924 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
925 */
926/**
927 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
928 * @cpu_dev using operating-points-v2
929 * bindings.
930 *
931 * @cpu_dev: CPU device for which we do this operation
932 * @cpumask: cpumask to update with information of sharing CPUs
933 *
934 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
935 *
936 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530937 */
938int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
939 struct cpumask *cpumask)
940{
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200941 struct device_node *np, *tmp_np, *cpu_np;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530942 int cpu, ret = 0;
943
944 /* Get OPP descriptor node */
Dave Gerlach0764c602017-02-03 11:29:26 -0600945 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530946 if (!np) {
Masahiro Yamada349aa922016-10-20 16:12:49 +0900947 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530948 return -ENOENT;
949 }
950
951 cpumask_set_cpu(cpu_dev->id, cpumask);
952
953 /* OPPs are shared ? */
954 if (!of_property_read_bool(np, "opp-shared"))
955 goto put_cpu_node;
956
957 for_each_possible_cpu(cpu) {
958 if (cpu == cpu_dev->id)
959 continue;
960
Sudeep Holla98679992017-10-12 11:32:23 +0100961 cpu_np = of_cpu_device_node_get(cpu);
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200962 if (!cpu_np) {
963 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530964 __func__, cpu);
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200965 ret = -ENOENT;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530966 goto put_cpu_node;
967 }
968
969 /* Get OPP descriptor node */
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530970 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
Sudeep Holla98679992017-10-12 11:32:23 +0100971 of_node_put(cpu_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530972 if (!tmp_np) {
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200973 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530974 ret = -ENOENT;
975 goto put_cpu_node;
976 }
977
978 /* CPUs are sharing opp node */
979 if (np == tmp_np)
980 cpumask_set_cpu(cpu, cpumask);
981
982 of_node_put(tmp_np);
983 }
984
985put_cpu_node:
986 of_node_put(np);
987 return ret;
988}
989EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
Viresh Kumara88bd2a2017-11-29 15:18:36 +0530990
991/**
Viresh Kumar4c6a3432018-06-27 16:29:50 +0530992 * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
Viresh Kumara88bd2a2017-11-29 15:18:36 +0530993 * @np: Node that contains the "required-opps" property.
Viresh Kumar4c6a3432018-06-27 16:29:50 +0530994 * @index: Index of the phandle to parse.
Viresh Kumara88bd2a2017-11-29 15:18:36 +0530995 *
Viresh Kumar4c6a3432018-06-27 16:29:50 +0530996 * Returns the performance state of the OPP pointed out by the "required-opps"
997 * property at @index in @np.
Viresh Kumara88bd2a2017-11-29 15:18:36 +0530998 *
Viresh Kumar2feb5a82018-12-14 15:20:56 +0530999 * Return: Zero or positive performance state on success, otherwise negative
1000 * value on errors.
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301001 */
Viresh Kumar2feb5a82018-12-14 15:20:56 +05301002int of_get_required_opp_performance_state(struct device_node *np, int index)
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301003{
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301004 struct dev_pm_opp *opp;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301005 struct device_node *required_np;
1006 struct opp_table *opp_table;
Viresh Kumar2feb5a82018-12-14 15:20:56 +05301007 int pstate = -EINVAL;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301008
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301009 required_np = of_parse_required_opp(np, index);
1010 if (!required_np)
Viresh Kumar2feb5a82018-12-14 15:20:56 +05301011 return -EINVAL;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301012
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301013 opp_table = _find_table_of_opp_np(required_np);
1014 if (IS_ERR(opp_table)) {
1015 pr_err("%s: Failed to find required OPP table %pOF: %ld\n",
1016 __func__, np, PTR_ERR(opp_table));
1017 goto put_required_np;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301018 }
1019
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301020 opp = _find_opp_of_np(opp_table, required_np);
1021 if (opp) {
1022 pstate = opp->pstate;
1023 dev_pm_opp_put(opp);
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301024 }
1025
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301026 dev_pm_opp_put_opp_table(opp_table);
1027
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301028put_required_np:
1029 of_node_put(required_np);
1030
1031 return pstate;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301032}
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301033EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
Viresh Kumare2f4b5f2018-01-12 10:03:45 +05301034
1035/**
1036 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
1037 * @opp: opp for which DT node has to be returned for
1038 *
1039 * Return: DT node corresponding to the opp, else 0 on success.
1040 *
1041 * The caller needs to put the node with of_node_put() after using it.
1042 */
1043struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
1044{
1045 if (IS_ERR_OR_NULL(opp)) {
1046 pr_err("%s: Invalid parameters\n", __func__);
1047 return NULL;
1048 }
1049
1050 return of_node_get(opp->np);
1051}
1052EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);
Quentin Perreta4f342b2019-02-04 11:09:48 +00001053
1054/*
1055 * Callback function provided to the Energy Model framework upon registration.
1056 * This computes the power estimated by @CPU at @kHz if it is the frequency
1057 * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise
1058 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled
1059 * frequency and @mW to the associated power. The power is estimated as
1060 * P = C * V^2 * f with C being the CPU's capacitance and V and f respectively
1061 * the voltage and frequency of the OPP.
1062 *
1063 * Returns -ENODEV if the CPU device cannot be found, -EINVAL if the power
1064 * calculation failed because of missing parameters, 0 otherwise.
1065 */
1066static int __maybe_unused _get_cpu_power(unsigned long *mW, unsigned long *kHz,
1067 int cpu)
1068{
1069 struct device *cpu_dev;
1070 struct dev_pm_opp *opp;
1071 struct device_node *np;
1072 unsigned long mV, Hz;
1073 u32 cap;
1074 u64 tmp;
1075 int ret;
1076
1077 cpu_dev = get_cpu_device(cpu);
1078 if (!cpu_dev)
1079 return -ENODEV;
1080
1081 np = of_node_get(cpu_dev->of_node);
1082 if (!np)
1083 return -EINVAL;
1084
1085 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1086 of_node_put(np);
1087 if (ret)
1088 return -EINVAL;
1089
1090 Hz = *kHz * 1000;
1091 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &Hz);
1092 if (IS_ERR(opp))
1093 return -EINVAL;
1094
1095 mV = dev_pm_opp_get_voltage(opp) / 1000;
1096 dev_pm_opp_put(opp);
1097 if (!mV)
1098 return -EINVAL;
1099
1100 tmp = (u64)cap * mV * mV * (Hz / 1000000);
1101 do_div(tmp, 1000000000);
1102
1103 *mW = (unsigned long)tmp;
1104 *kHz = Hz / 1000;
1105
1106 return 0;
1107}
1108
1109/**
1110 * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
1111 * @cpus : CPUs for which an Energy Model has to be registered
1112 *
1113 * This checks whether the "dynamic-power-coefficient" devicetree property has
1114 * been specified, and tries to register an Energy Model with it if it has.
1115 */
1116void dev_pm_opp_of_register_em(struct cpumask *cpus)
1117{
1118 struct em_data_callback em_cb = EM_DATA_CB(_get_cpu_power);
1119 int ret, nr_opp, cpu = cpumask_first(cpus);
1120 struct device *cpu_dev;
1121 struct device_node *np;
1122 u32 cap;
1123
1124 cpu_dev = get_cpu_device(cpu);
1125 if (!cpu_dev)
1126 return;
1127
1128 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
1129 if (nr_opp <= 0)
1130 return;
1131
1132 np = of_node_get(cpu_dev->of_node);
1133 if (!np)
1134 return;
1135
1136 /*
1137 * Register an EM only if the 'dynamic-power-coefficient' property is
1138 * set in devicetree. It is assumed the voltage values are known if that
1139 * property is set since it is useless otherwise. If voltages are not
1140 * known, just let the EM registration fail with an error to alert the
1141 * user about the inconsistent configuration.
1142 */
1143 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap);
1144 of_node_put(np);
1145 if (ret || !cap)
1146 return;
1147
1148 em_register_perf_domain(cpus, nr_opp, &em_cb);
1149}
1150EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);