blob: 81fa27599d58b98134d78d54f275b119fe19b996 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Viresh Kumarf47b72a2016-05-05 16:20:33 +05302/*
3 * Generic OPP OF helpers
4 *
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
6 * Nishanth Menon
7 * Romit Dasgupta
8 * Kevin Hilman
Viresh Kumarf47b72a2016-05-05 16:20:33 +05309 */
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>
Rob Herringcd6f0f52023-03-29 10:52:15 -050016#include <linux/of.h>
Viresh Kumar3ba98322016-11-18 15:47:46 +053017#include <linux/pm_domain.h>
Viresh Kumardfbe4672016-12-01 16:28:19 +053018#include <linux/slab.h>
Viresh Kumarf47b72a2016-05-05 16:20:33 +053019#include <linux/export.h>
Quentin Perreta4f342b2019-02-04 11:09:48 +000020#include <linux/energy_model.h>
Viresh Kumarf47b72a2016-05-05 16:20:33 +053021
22#include "opp.h"
23
Viresh Kumar64aaeb72023-06-08 13:10:14 +053024/* OPP tables with uninitialized required OPPs, protected by opp_table_lock */
Viresh Kumar167eb2b2023-06-08 12:55:07 +053025static LIST_HEAD(lazy_opp_tables);
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
Viresh Kumar5d6d1062018-06-07 14:50:43 +053083 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{
Rajendra Nayak020d86f2021-08-12 16:57:20 +0530101 return of_parse_phandle(np, "required-opps", index);
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530102}
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;
Viresh Kumar699e21e2018-11-22 11:04:00 +0530108 struct device_node *opp_table_np;
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530109
Viresh Kumar699e21e2018-11-22 11:04:00 +0530110 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
Viresh Kumar27c09482020-10-27 16:53:21 +0530117 mutex_lock(&opp_table_lock);
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530118 list_for_each_entry(opp_table, &opp_tables, node) {
Viresh Kumar699e21e2018-11-22 11:04:00 +0530119 if (opp_table_np == opp_table->np) {
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530120 _get_opp_table_kref(opp_table);
Viresh Kumar27c09482020-10-27 16:53:21 +0530121 mutex_unlock(&opp_table_lock);
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530122 return opp_table;
123 }
124 }
Viresh Kumar27c09482020-10-27 16:53:21 +0530125 mutex_unlock(&opp_table_lock);
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530126
Viresh Kumar699e21e2018-11-22 11:04:00 +0530127err:
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530128 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]))
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530142 continue;
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530143
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;
Viresh Kumar64aaeb72023-06-08 13:10:14 +0530151
152 mutex_lock(&opp_table_lock);
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530153 list_del(&opp_table->lazy);
Viresh Kumar64aaeb72023-06-08 13:10:14 +0530154 mutex_unlock(&opp_table_lock);
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530155}
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;
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530167 bool lazy = false;
Viresh Kumarc0ab9e02019-05-08 15:43:36 +0530168 int count, i;
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530169
170 /* Traversing the first OPP node is all we need */
171 np = of_get_next_available_child(opp_np, NULL);
172 if (!np) {
Nicola Mazzucato6ee70e82020-12-08 17:42:27 +0000173 dev_warn(dev, "Empty OPP table\n");
174
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530175 return;
176 }
177
178 count = of_count_phandle_with_args(np, "required-opps", NULL);
Pavankumar Kondeti8b7912f2021-09-30 15:39:08 +0530179 if (count <= 0)
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530180 goto put_np;
181
182 required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),
183 GFP_KERNEL);
Viresh Kumarc0ab9e02019-05-08 15:43:36 +0530184 if (!required_opp_tables)
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530185 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
Hsin-Yi Wang4fa82a872021-06-16 13:33:35 +0800198 if (IS_ERR(required_opp_tables[i]))
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530199 lazy = true;
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530200 }
201
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530202 /* Let's do the linking later on */
Viresh Kumar64aaeb72023-06-08 13:10:14 +0530203 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);
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530209 list_add(&opp_table->lazy, &lazy_opp_tables);
Viresh Kumar64aaeb72023-06-08 13:10:14 +0530210 mutex_unlock(&opp_table_lock);
Viresh Kumar8e6db122023-10-12 14:39:26 +0530211 } else {
Viresh Kumar528f2d82023-02-22 16:36:38 +0530212 _update_set_required_opps(opp_table);
Viresh Kumar8e6db122023-10-12 14:39:26 +0530213 }
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530214
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530215 goto put_np;
216
217free_required_tables:
218 _opp_table_free_required_tables(opp_table);
219put_np:
220 of_node_put(np);
221}
222
Viresh Kumareb7c87432018-09-05 16:17:14 +0530223void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
224 int index)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530225{
Viresh Kumarf06ed902018-06-14 12:04:43 +0530226 struct device_node *np, *opp_np;
227 u32 val;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530228
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);
Viresh Kumarf06ed902018-06-14 12:04:43 +0530234 if (!np)
235 return;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530236
Viresh Kumarf06ed902018-06-14 12:04:43 +0530237 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
Rob Herringe9eadc22023-03-10 08:47:18 -0600242 if (of_property_present(np, "#power-domain-cells"))
Viresh Kumar61d8e7c2018-06-13 16:00:11 +0530243 opp_table->is_genpd = true;
244
Viresh Kumarf06ed902018-06-14 12:04:43 +0530245 /* 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
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530259 _opp_table_alloc_required_tables(opp_table, dev, opp_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530260}
261
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530262void _of_clear_opp_table(struct opp_table *opp_table)
263{
264 _opp_table_free_required_tables(opp_table);
Liang Hece736cf2022-07-18 21:36:31 +0800265 of_node_put(opp_table->np);
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530266}
267
Viresh Kumarda544b62018-06-07 14:50:43 +0530268/*
269 * Release all resources previously acquired with a call to
270 * _of_opp_alloc_required_opps().
271 */
Liang He3466ea22022-07-18 21:36:32 +0800272static void _of_opp_free_required_opps(struct opp_table *opp_table,
273 struct dev_pm_opp *opp)
Viresh Kumarda544b62018-06-07 14:50:43 +0530274{
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])
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530283 continue;
Viresh Kumarda544b62018-06-07 14:50:43 +0530284
285 /* Put the reference back */
286 dev_pm_opp_put(required_opps[i]);
287 }
288
Viresh Kumarda544b62018-06-07 14:50:43 +0530289 opp->required_opps = NULL;
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530290 kfree(required_opps);
Viresh Kumarda544b62018-06-07 14:50:43 +0530291}
292
Liang He3466ea22022-07-18 21:36:32 +0800293void _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
Viresh Kumar96104042023-10-12 15:11:30 +0530299static int _link_required_opps(struct dev_pm_opp *opp,
300 struct opp_table *required_table, int index)
301{
302 struct device_node *np;
303
304 np = of_parse_required_opp(opp->np, index);
305 if (unlikely(!np))
306 return -ENODEV;
307
308 opp->required_opps[index] = _find_opp_of_np(required_table, np);
309 of_node_put(np);
310
311 if (!opp->required_opps[index]) {
312 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",
313 __func__, opp->np, index);
314 return -ENODEV;
315 }
316
317 return 0;
318}
319
Viresh Kumarda544b62018-06-07 14:50:43 +0530320/* Populate all required OPPs which are part of "required-opps" list */
321static int _of_opp_alloc_required_opps(struct opp_table *opp_table,
322 struct dev_pm_opp *opp)
323{
Viresh Kumarda544b62018-06-07 14:50:43 +0530324 struct opp_table *required_table;
Viresh Kumarda544b62018-06-07 14:50:43 +0530325 int i, ret, count = opp_table->required_opp_count;
326
327 if (!count)
328 return 0;
329
Viresh Kumar96104042023-10-12 15:11:30 +0530330 opp->required_opps = kcalloc(count, sizeof(*opp->required_opps), GFP_KERNEL);
331 if (!opp->required_opps)
Viresh Kumarda544b62018-06-07 14:50:43 +0530332 return -ENOMEM;
333
Viresh Kumarda544b62018-06-07 14:50:43 +0530334 for (i = 0; i < count; i++) {
335 required_table = opp_table->required_opp_tables[i];
336
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530337 /* Required table not added yet, we will link later */
338 if (IS_ERR_OR_NULL(required_table))
339 continue;
340
Viresh Kumar96104042023-10-12 15:11:30 +0530341 ret = _link_required_opps(opp, required_table, i);
342 if (ret)
Viresh Kumarda544b62018-06-07 14:50:43 +0530343 goto free_required_opps;
Viresh Kumarda544b62018-06-07 14:50:43 +0530344 }
345
346 return 0;
347
348free_required_opps:
349 _of_opp_free_required_opps(opp_table, opp);
350
351 return ret;
352}
353
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530354/* Link required OPPs for an individual OPP */
355static int lazy_link_required_opps(struct opp_table *opp_table,
356 struct opp_table *new_table, int index)
357{
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530358 struct dev_pm_opp *opp;
Viresh Kumar96104042023-10-12 15:11:30 +0530359 int ret;
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530360
361 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar96104042023-10-12 15:11:30 +0530362 ret = _link_required_opps(opp, new_table, index);
363 if (ret)
364 return ret;
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530365 }
366
367 return 0;
368}
369
370/* Link required OPPs for all OPPs of the newly added OPP table */
371static void lazy_link_required_opp_table(struct opp_table *new_table)
372{
373 struct opp_table *opp_table, *temp, **required_opp_tables;
374 struct device_node *required_np, *opp_np, *required_table_np;
375 struct dev_pm_opp *opp;
376 int i, ret;
377
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530378 mutex_lock(&opp_table_lock);
379
380 list_for_each_entry_safe(opp_table, temp, &lazy_opp_tables, lazy) {
381 bool lazy = false;
382
383 /* opp_np can't be invalid here */
384 opp_np = of_get_next_available_child(opp_table->np, NULL);
385
386 for (i = 0; i < opp_table->required_opp_count; i++) {
387 required_opp_tables = opp_table->required_opp_tables;
388
389 /* Required opp-table is already parsed */
390 if (!IS_ERR(required_opp_tables[i]))
391 continue;
392
393 /* required_np can't be invalid here */
394 required_np = of_parse_required_opp(opp_np, i);
395 required_table_np = of_get_parent(required_np);
396
397 of_node_put(required_table_np);
398 of_node_put(required_np);
399
400 /*
401 * Newly added table isn't the required opp-table for
402 * opp_table.
403 */
404 if (required_table_np != new_table->np) {
405 lazy = true;
406 continue;
407 }
408
409 required_opp_tables[i] = new_table;
410 _get_opp_table_kref(new_table);
411
412 /* Link OPPs now */
413 ret = lazy_link_required_opps(opp_table, new_table, i);
414 if (ret) {
415 /* The OPPs will be marked unusable */
416 lazy = false;
417 break;
418 }
419 }
420
421 of_node_put(opp_np);
422
423 /* All required opp-tables found, remove from lazy list */
424 if (!lazy) {
Viresh Kumar528f2d82023-02-22 16:36:38 +0530425 _update_set_required_opps(opp_table);
Yang Yingliangac9fd3c2021-05-18 12:49:10 +0800426 list_del_init(&opp_table->lazy);
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530427
428 list_for_each_entry(opp, &opp_table->opp_list, node)
429 _required_opps_available(opp, opp_table->required_opp_count);
430 }
431 }
432
433 mutex_unlock(&opp_table_lock);
434}
435
Sibi Sankar45679f92020-05-28 00:54:18 +0530436static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table)
437{
438 struct device_node *np, *opp_np;
439 struct property *prop;
440
441 if (!opp_table) {
442 np = of_node_get(dev->of_node);
443 if (!np)
444 return -ENODEV;
445
446 opp_np = _opp_of_get_opp_desc_node(np, 0);
447 of_node_put(np);
448 } else {
449 opp_np = of_node_get(opp_table->np);
450 }
451
452 /* Lets not fail in case we are parsing opp-v1 bindings */
453 if (!opp_np)
454 return 0;
455
456 /* Checking only first OPP is sufficient */
457 np = of_get_next_available_child(opp_np, NULL);
Dan Carpenter907ed122022-04-06 09:40:14 +0300458 of_node_put(opp_np);
Sibi Sankar45679f92020-05-28 00:54:18 +0530459 if (!np) {
460 dev_err(dev, "OPP table empty\n");
461 return -EINVAL;
462 }
Sibi Sankar45679f92020-05-28 00:54:18 +0530463
464 prop = of_find_property(np, "opp-peak-kBps", NULL);
465 of_node_put(np);
466
467 if (!prop || !prop->length)
468 return 0;
469
470 return 1;
471}
472
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300473int dev_pm_opp_of_find_icc_paths(struct device *dev,
474 struct opp_table *opp_table)
475{
476 struct device_node *np;
Sibi Sankar45679f92020-05-28 00:54:18 +0530477 int ret, i, count, num_paths;
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300478 struct icc_path **paths;
479
Sibi Sankar45679f92020-05-28 00:54:18 +0530480 ret = _bandwidth_supported(dev, opp_table);
Nicola Mazzucato6ee70e82020-12-08 17:42:27 +0000481 if (ret == -EINVAL)
482 return 0; /* Empty OPP table is a valid corner-case, let's not fail */
483 else if (ret <= 0)
Sibi Sankar45679f92020-05-28 00:54:18 +0530484 return ret;
485
486 ret = 0;
487
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300488 np = of_node_get(dev->of_node);
489 if (!np)
490 return 0;
491
492 count = of_count_phandle_with_args(np, "interconnects",
493 "#interconnect-cells");
494 of_node_put(np);
495 if (count < 0)
496 return 0;
497
498 /* two phandles when #interconnect-cells = <1> */
499 if (count % 2) {
500 dev_err(dev, "%s: Invalid interconnects values\n", __func__);
501 return -EINVAL;
502 }
503
504 num_paths = count / 2;
505 paths = kcalloc(num_paths, sizeof(*paths), GFP_KERNEL);
506 if (!paths)
507 return -ENOMEM;
508
509 for (i = 0; i < num_paths; i++) {
510 paths[i] = of_icc_get_by_index(dev, i);
511 if (IS_ERR(paths[i])) {
Andrew Halaney5fb28642023-06-26 08:46:46 -0500512 ret = dev_err_probe(dev, PTR_ERR(paths[i]), "%s: Unable to get path%d\n", __func__, i);
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300513 goto err;
514 }
515 }
516
517 if (opp_table) {
518 opp_table->paths = paths;
519 opp_table->path_count = num_paths;
520 return 0;
521 }
522
523err:
524 while (i--)
525 icc_put(paths[i]);
526
527 kfree(paths);
528
529 return ret;
530}
531EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths);
532
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530533static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
534 struct device_node *np)
535{
Viresh Kumar0ff25c92020-08-26 16:32:27 +0530536 unsigned int levels = opp_table->supported_hw_count;
537 int count, versions, ret, i, j;
538 u32 val;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530539
Dave Gerlacha4ee4542016-09-23 15:07:47 -0500540 if (!opp_table->supported_hw) {
541 /*
542 * In the case that no supported_hw has been set by the
543 * platform but there is an opp-supported-hw value set for
544 * an OPP then the OPP should not be enabled as there is
545 * no way to see if the hardware supports it.
546 */
Rob Herringe9eadc22023-03-10 08:47:18 -0600547 if (of_property_present(np, "opp-supported-hw"))
Dave Gerlacha4ee4542016-09-23 15:07:47 -0500548 return false;
549 else
550 return true;
551 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530552
Viresh Kumar0ff25c92020-08-26 16:32:27 +0530553 count = of_property_count_u32_elems(np, "opp-supported-hw");
554 if (count <= 0 || count % levels) {
555 dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n",
556 __func__, count);
557 return false;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530558 }
559
Viresh Kumar0ff25c92020-08-26 16:32:27 +0530560 versions = count / levels;
561
562 /* All levels in at least one of the versions should match */
563 for (i = 0; i < versions; i++) {
564 bool supported = true;
565
566 for (j = 0; j < levels; j++) {
567 ret = of_property_read_u32_index(np, "opp-supported-hw",
568 i * levels + j, &val);
569 if (ret) {
570 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
571 __func__, i * levels + j, ret);
572 return false;
573 }
574
575 /* Check if the level is supported */
576 if (!(val & opp_table->supported_hw[j])) {
577 supported = false;
578 break;
579 }
580 }
581
582 if (supported)
583 return true;
584 }
585
586 return false;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530587}
588
Viresh Kumare5acb192022-11-02 16:47:08 +0530589static u32 *_parse_named_prop(struct dev_pm_opp *opp, struct device *dev,
590 struct opp_table *opp_table,
591 const char *prop_type, bool *triplet)
592{
593 struct property *prop = NULL;
594 char name[NAME_MAX];
595 int count, ret;
596 u32 *out;
597
598 /* Search for "opp-<prop_type>-<name>" */
599 if (opp_table->prop_name) {
600 snprintf(name, sizeof(name), "opp-%s-%s", prop_type,
601 opp_table->prop_name);
602 prop = of_find_property(opp->np, name, NULL);
603 }
604
605 if (!prop) {
606 /* Search for "opp-<prop_type>" */
607 snprintf(name, sizeof(name), "opp-%s", prop_type);
608 prop = of_find_property(opp->np, name, NULL);
609 if (!prop)
610 return NULL;
611 }
612
613 count = of_property_count_u32_elems(opp->np, name);
614 if (count < 0) {
615 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, name,
616 count);
617 return ERR_PTR(count);
618 }
619
620 /*
621 * Initialize regulator_count, if regulator information isn't provided
622 * by the platform. Now that one of the properties is available, fix the
623 * regulator_count to 1.
624 */
625 if (unlikely(opp_table->regulator_count == -1))
626 opp_table->regulator_count = 1;
627
628 if (count != opp_table->regulator_count &&
629 (!triplet || count != opp_table->regulator_count * 3)) {
630 dev_err(dev, "%s: Invalid number of elements in %s property (%u) with supplies (%d)\n",
631 __func__, prop_type, count, opp_table->regulator_count);
632 return ERR_PTR(-EINVAL);
633 }
634
635 out = kmalloc_array(count, sizeof(*out), GFP_KERNEL);
636 if (!out)
637 return ERR_PTR(-EINVAL);
638
639 ret = of_property_read_u32_array(opp->np, name, out, count);
640 if (ret) {
641 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
642 kfree(out);
643 return ERR_PTR(-EINVAL);
644 }
645
646 if (triplet)
647 *triplet = count != opp_table->regulator_count;
648
649 return out;
650}
651
652static u32 *opp_parse_microvolt(struct dev_pm_opp *opp, struct device *dev,
653 struct opp_table *opp_table, bool *triplet)
654{
655 u32 *microvolt;
656
657 microvolt = _parse_named_prop(opp, dev, opp_table, "microvolt", triplet);
658 if (IS_ERR(microvolt))
659 return microvolt;
660
661 if (!microvolt) {
662 /*
663 * Missing property isn't a problem, but an invalid
664 * entry is. This property isn't optional if regulator
James Calligeros2eedf622022-11-03 14:40:44 +0530665 * information is provided. Check only for the first OPP, as
666 * regulator_count may get initialized after that to a valid
667 * value.
Viresh Kumare5acb192022-11-02 16:47:08 +0530668 */
James Calligeros2eedf622022-11-03 14:40:44 +0530669 if (list_empty(&opp_table->opp_list) &&
670 opp_table->regulator_count > 0) {
Viresh Kumare5acb192022-11-02 16:47:08 +0530671 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
672 __func__);
673 return ERR_PTR(-EINVAL);
674 }
675 }
676
677 return microvolt;
678}
679
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530680static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
681 struct opp_table *opp_table)
682{
Viresh Kumare5acb192022-11-02 16:47:08 +0530683 u32 *microvolt, *microamp, *microwatt;
684 int ret = 0, i, j;
685 bool triplet;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530686
Viresh Kumare5acb192022-11-02 16:47:08 +0530687 microvolt = opp_parse_microvolt(opp, dev, opp_table, &triplet);
James Calligeros2eedf622022-11-03 14:40:44 +0530688 if (IS_ERR(microvolt))
Viresh Kumare5acb192022-11-02 16:47:08 +0530689 return PTR_ERR(microvolt);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530690
Viresh Kumare5acb192022-11-02 16:47:08 +0530691 microamp = _parse_named_prop(opp, dev, opp_table, "microamp", NULL);
692 if (IS_ERR(microamp)) {
693 ret = PTR_ERR(microamp);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530694 goto free_microvolt;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530695 }
696
Viresh Kumare5acb192022-11-02 16:47:08 +0530697 microwatt = _parse_named_prop(opp, dev, opp_table, "microwatt", NULL);
698 if (IS_ERR(microwatt)) {
699 ret = PTR_ERR(microwatt);
700 goto free_microamp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530701 }
702
James Calligeros2eedf622022-11-03 14:40:44 +0530703 /*
704 * Initialize regulator_count if it is uninitialized and no properties
705 * are found.
706 */
707 if (unlikely(opp_table->regulator_count == -1)) {
708 opp_table->regulator_count = 0;
709 return 0;
710 }
Viresh Kumardfbe4672016-12-01 16:28:19 +0530711
James Calligeros2eedf622022-11-03 14:40:44 +0530712 for (i = 0, j = 0; i < opp_table->regulator_count; i++) {
713 if (microvolt) {
714 opp->supplies[i].u_volt = microvolt[j++];
715
716 if (triplet) {
717 opp->supplies[i].u_volt_min = microvolt[j++];
718 opp->supplies[i].u_volt_max = microvolt[j++];
719 } else {
720 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
721 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
722 }
Viresh Kumardfbe4672016-12-01 16:28:19 +0530723 }
724
725 if (microamp)
726 opp->supplies[i].u_amp = microamp[i];
Lukasz Luba4f9a7a12022-03-02 11:29:14 +0000727
728 if (microwatt)
729 opp->supplies[i].u_watt = microwatt[i];
Viresh Kumardfbe4672016-12-01 16:28:19 +0530730 }
731
Lukasz Luba4f9a7a12022-03-02 11:29:14 +0000732 kfree(microwatt);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530733free_microamp:
734 kfree(microamp);
735free_microvolt:
736 kfree(microvolt);
737
738 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530739}
740
741/**
742 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
743 * entries
744 * @dev: device pointer used to lookup OPP table.
745 *
746 * Free OPPs created using static entries present in DT.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530747 */
748void dev_pm_opp_of_remove_table(struct device *dev)
749{
Viresh Kumar8aaf6262020-08-20 13:18:23 +0530750 dev_pm_opp_remove_table(dev);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530751}
752EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
753
Viresh Kumar2083da22022-06-10 12:02:04 +0530754static int _read_rate(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
755 struct device_node *np)
756{
757 struct property *prop;
758 int i, count, ret;
759 u64 *rates;
760
761 prop = of_find_property(np, "opp-hz", NULL);
762 if (!prop)
763 return -ENODEV;
764
765 count = prop->length / sizeof(u64);
766 if (opp_table->clk_count != count) {
767 pr_err("%s: Count mismatch between opp-hz and clk_count (%d %d)\n",
768 __func__, count, opp_table->clk_count);
769 return -EINVAL;
770 }
771
772 rates = kmalloc_array(count, sizeof(*rates), GFP_KERNEL);
773 if (!rates)
774 return -ENOMEM;
775
776 ret = of_property_read_u64_array(np, "opp-hz", rates, count);
777 if (ret) {
778 pr_err("%s: Error parsing opp-hz: %d\n", __func__, ret);
779 } else {
780 /*
781 * Rate is defined as an unsigned long in clk API, and so
782 * casting explicitly to its type. Must be fixed once rate is 64
783 * bit guaranteed in clk API.
784 */
785 for (i = 0; i < count; i++) {
786 new_opp->rates[i] = (unsigned long)rates[i];
787
788 /* This will happen for frequencies > 4.29 GHz */
789 WARN_ON(new_opp->rates[i] != rates[i]);
790 }
791 }
792
793 kfree(rates);
794
795 return ret;
796}
797
Viresh Kumard6134582022-06-09 15:35:36 +0530798static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *opp_table,
Georgi Djakov120e1172020-05-12 15:53:22 +0300799 struct device_node *np, bool peak)
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300800{
801 const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps";
802 struct property *prop;
803 int i, count, ret;
804 u32 *bw;
805
806 prop = of_find_property(np, name, NULL);
807 if (!prop)
808 return -ENODEV;
809
810 count = prop->length / sizeof(u32);
Viresh Kumard6134582022-06-09 15:35:36 +0530811 if (opp_table->path_count != count) {
Georgi Djakov120e1172020-05-12 15:53:22 +0300812 pr_err("%s: Mismatch between %s and paths (%d %d)\n",
Viresh Kumard6134582022-06-09 15:35:36 +0530813 __func__, name, count, opp_table->path_count);
Georgi Djakov120e1172020-05-12 15:53:22 +0300814 return -EINVAL;
815 }
816
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300817 bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL);
818 if (!bw)
819 return -ENOMEM;
820
821 ret = of_property_read_u32_array(np, name, bw, count);
822 if (ret) {
823 pr_err("%s: Error parsing %s: %d\n", __func__, name, ret);
824 goto out;
825 }
826
827 for (i = 0; i < count; i++) {
828 if (peak)
829 new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]);
830 else
831 new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]);
832 }
833
834out:
835 kfree(bw);
836 return ret;
837}
838
Viresh Kumar47689142022-06-08 11:57:24 +0530839static int _read_opp_key(struct dev_pm_opp *new_opp,
840 struct opp_table *opp_table, struct device_node *np)
Saravana Kannan6c591ee2020-05-12 15:53:19 +0300841{
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300842 bool found = false;
Saravana Kannan6c591ee2020-05-12 15:53:19 +0300843 int ret;
844
Viresh Kumar2083da22022-06-10 12:02:04 +0530845 ret = _read_rate(new_opp, opp_table, np);
846 if (!ret)
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300847 found = true;
Viresh Kumar2083da22022-06-10 12:02:04 +0530848 else if (ret != -ENODEV)
849 return ret;
Saravana Kannan6c591ee2020-05-12 15:53:19 +0300850
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300851 /*
852 * Bandwidth consists of peak and average (optional) values:
853 * opp-peak-kBps = <path1_value path2_value>;
854 * opp-avg-kBps = <path1_value path2_value>;
855 */
Viresh Kumard6134582022-06-09 15:35:36 +0530856 ret = _read_bw(new_opp, opp_table, np, true);
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300857 if (!ret) {
858 found = true;
Viresh Kumard6134582022-06-09 15:35:36 +0530859 ret = _read_bw(new_opp, opp_table, np, false);
Georgi Djakov6d3f9222020-05-12 15:53:21 +0300860 }
861
862 /* The properties were found but we failed to parse them */
863 if (ret && ret != -ENODEV)
864 return ret;
865
866 if (!of_property_read_u32(np, "opp-level", &new_opp->level))
867 found = true;
868
869 if (found)
870 return 0;
Saravana Kannan6c591ee2020-05-12 15:53:19 +0300871
872 return ret;
873}
874
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530875/**
876 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530877 * @opp_table: OPP table
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530878 * @dev: device for which we do this operation
879 * @np: device node
880 *
881 * This function adds an opp definition to the opp table and returns status. The
882 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
883 * removed by dev_pm_opp_remove.
884 *
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530885 * Return:
Dave Gerlachdeac8702018-10-03 16:13:15 +0530886 * Valid OPP pointer:
887 * On success
888 * NULL:
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530889 * Duplicate OPPs (both freq and volt are same) and opp->available
Dave Gerlachdeac8702018-10-03 16:13:15 +0530890 * OR if the OPP is not supported by hardware.
891 * ERR_PTR(-EEXIST):
892 * Freq are same and volt are different OR
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530893 * Duplicate OPPs (both freq and volt are same) and !opp->available
Dave Gerlachdeac8702018-10-03 16:13:15 +0530894 * ERR_PTR(-ENOMEM):
895 * Memory allocation failure
896 * ERR_PTR(-EINVAL):
897 * Failed parsing the OPP node
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530898 */
Dave Gerlachdeac8702018-10-03 16:13:15 +0530899static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
900 struct device *dev, struct device_node *np)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530901{
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530902 struct dev_pm_opp *new_opp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530903 u32 val;
904 int ret;
905
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530906 new_opp = _opp_allocate(opp_table);
907 if (!new_opp)
Dave Gerlachdeac8702018-10-03 16:13:15 +0530908 return ERR_PTR(-ENOMEM);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530909
Viresh Kumar47689142022-06-08 11:57:24 +0530910 ret = _read_opp_key(new_opp, opp_table, np);
Hsin-Yi Wang4fa82a872021-06-16 13:33:35 +0800911 if (ret < 0) {
Saravana Kannan6c591ee2020-05-12 15:53:19 +0300912 dev_err(dev, "%s: opp key field not found\n", __func__);
913 goto free_opp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530914 }
915
916 /* Check if the OPP supports hardware's hierarchy of versions or not */
917 if (!_opp_is_supported(dev, opp_table, np)) {
Viresh Kumar2083da22022-06-10 12:02:04 +0530918 dev_dbg(dev, "OPP not supported by hardware: %s\n",
919 of_node_full_name(np));
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530920 goto free_opp;
921 }
922
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530923 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
924
Liang He3466ea22022-07-18 21:36:32 +0800925 new_opp->np = of_node_get(np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530926 new_opp->dynamic = false;
927 new_opp->available = true;
928
Viresh Kumarda544b62018-06-07 14:50:43 +0530929 ret = _of_opp_alloc_required_opps(opp_table, new_opp);
930 if (ret)
931 goto free_opp;
932
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530933 if (!of_property_read_u32(np, "clock-latency-ns", &val))
934 new_opp->clock_latency_ns = val;
935
936 ret = opp_parse_supplies(new_opp, dev, opp_table);
937 if (ret)
Viresh Kumarda544b62018-06-07 14:50:43 +0530938 goto free_required_opps;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530939
Viresh Kumar47689142022-06-08 11:57:24 +0530940 ret = _opp_add(dev, new_opp, opp_table);
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530941 if (ret) {
942 /* Don't return error for duplicate OPPs */
943 if (ret == -EBUSY)
944 ret = 0;
Viresh Kumarda544b62018-06-07 14:50:43 +0530945 goto free_required_opps;
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530946 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530947
948 /* OPP to select on device suspend */
949 if (of_property_read_bool(np, "opp-suspend")) {
950 if (opp_table->suspend_opp) {
Viresh Kumar8bdac142022-06-10 10:32:50 +0530951 /* Pick the OPP with higher rate/bw/level as suspend OPP */
Viresh Kumar2083da22022-06-10 12:02:04 +0530952 if (_opp_compare_key(opp_table, new_opp, opp_table->suspend_opp) == 1) {
Anson Huang45275512019-07-09 16:00:13 +0800953 opp_table->suspend_opp->suspend = false;
954 new_opp->suspend = true;
955 opp_table->suspend_opp = new_opp;
956 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530957 } else {
958 new_opp->suspend = true;
959 opp_table->suspend_opp = new_opp;
960 }
961 }
962
963 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
964 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
965
Dmitry Osipenkob6ecd5d2021-01-18 03:55:24 +0300966 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu level:%u\n",
Viresh Kumar2083da22022-06-10 12:02:04 +0530967 __func__, new_opp->turbo, new_opp->rates[0],
Viresh Kumardfbe4672016-12-01 16:28:19 +0530968 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
Dmitry Osipenkob6ecd5d2021-01-18 03:55:24 +0300969 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns,
970 new_opp->level);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530971
972 /*
973 * Notify the changes in the availability of the operable
974 * frequency/voltage list.
975 */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530976 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Dave Gerlachdeac8702018-10-03 16:13:15 +0530977 return new_opp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530978
Viresh Kumarda544b62018-06-07 14:50:43 +0530979free_required_opps:
980 _of_opp_free_required_opps(opp_table, new_opp);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530981free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530982 _opp_free(new_opp);
983
YueHaibing27ff81872021-10-08 15:46:52 +0800984 return ret ? ERR_PTR(ret) : NULL;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530985}
986
987/* Initializes OPP tables based on new bindings */
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530988static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530989{
990 struct device_node *np;
Viresh Kumara5663c92020-09-11 14:56:18 +0530991 int ret, count = 0;
Viresh Kumar3ba98322016-11-18 15:47:46 +0530992 struct dev_pm_opp *opp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530993
Viresh Kumar283d55e2018-09-07 09:01:54 +0530994 /* OPP table is already initialized for the device */
Viresh Kumar03758d62019-11-11 16:35:03 +0530995 mutex_lock(&opp_table->lock);
Viresh Kumar283d55e2018-09-07 09:01:54 +0530996 if (opp_table->parsed_static_opps) {
Viresh Kumar03758d62019-11-11 16:35:03 +0530997 opp_table->parsed_static_opps++;
998 mutex_unlock(&opp_table->lock);
Viresh Kumar283d55e2018-09-07 09:01:54 +0530999 return 0;
1000 }
1001
Viresh Kumar03758d62019-11-11 16:35:03 +05301002 opp_table->parsed_static_opps = 1;
1003 mutex_unlock(&opp_table->lock);
Viresh Kumarb19c23552019-10-18 14:11:58 +05301004
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301005 /* We have opp-table node now, iterate over it and add OPPs */
Viresh Kumar5ed4cec2018-09-12 11:21:17 +05301006 for_each_available_child_of_node(opp_table->np, np) {
Dave Gerlachdeac8702018-10-03 16:13:15 +05301007 opp = _opp_add_static_v2(opp_table, dev, np);
1008 if (IS_ERR(opp)) {
1009 ret = PTR_ERR(opp);
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301010 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1011 ret);
Tobias Jordan7978db32017-10-04 11:35:03 +05301012 of_node_put(np);
Viresh Kumar03758d62019-11-11 16:35:03 +05301013 goto remove_static_opp;
Dave Gerlachdeac8702018-10-03 16:13:15 +05301014 } else if (opp) {
1015 count++;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301016 }
1017 }
1018
Michał Mirosław335ffab2021-07-26 10:30:56 +02001019 /* There should be one or more OPPs defined */
1020 if (!count) {
1021 dev_err(dev, "%s: no supported OPPs", __func__);
Viresh Kumarba003312019-11-18 14:41:07 +05301022 ret = -ENOENT;
Viresh Kumar03758d62019-11-11 16:35:03 +05301023 goto remove_static_opp;
Viresh Kumarba003312019-11-18 14:41:07 +05301024 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301025
Viresh Kumar7eba0c72019-11-25 13:57:58 +05301026 lazy_link_required_opp_table(opp_table);
1027
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301028 return 0;
Viresh Kumarba003312019-11-18 14:41:07 +05301029
Viresh Kumar03758d62019-11-11 16:35:03 +05301030remove_static_opp:
1031 _opp_remove_all_static(opp_table);
Viresh Kumarba003312019-11-18 14:41:07 +05301032
1033 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301034}
1035
1036/* Initializes OPP tables based on old-deprecated bindings */
Viresh Kumar5ed4cec2018-09-12 11:21:17 +05301037static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301038{
1039 const struct property *prop;
1040 const __be32 *val;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301041 int nr, ret = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301042
Viresh Kumar90d46d72020-09-01 15:07:09 +05301043 mutex_lock(&opp_table->lock);
1044 if (opp_table->parsed_static_opps) {
1045 opp_table->parsed_static_opps++;
1046 mutex_unlock(&opp_table->lock);
1047 return 0;
1048 }
1049
1050 opp_table->parsed_static_opps = 1;
1051 mutex_unlock(&opp_table->lock);
1052
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301053 prop = of_find_property(dev->of_node, "operating-points", NULL);
Viresh Kumar90d46d72020-09-01 15:07:09 +05301054 if (!prop) {
1055 ret = -ENODEV;
1056 goto remove_static_opp;
1057 }
1058 if (!prop->value) {
1059 ret = -ENODATA;
1060 goto remove_static_opp;
1061 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301062
1063 /*
1064 * Each OPP is a set of tuples consisting of frequency and
1065 * voltage like <freq-kHz vol-uV>.
1066 */
1067 nr = prop->length / sizeof(u32);
1068 if (nr % 2) {
1069 dev_err(dev, "%s: Invalid OPP table\n", __func__);
Viresh Kumar90d46d72020-09-01 15:07:09 +05301070 ret = -EINVAL;
1071 goto remove_static_opp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301072 }
1073
1074 val = prop->value;
1075 while (nr) {
Nathan Chancellor184ff4f2023-10-05 10:25:27 -07001076 unsigned long freq = be32_to_cpup(val++) * 1000;
1077 unsigned long volt = be32_to_cpup(val++);
Ulf Hansson248a38d52023-09-25 15:17:09 +02001078 struct dev_pm_opp_data data = {
Nathan Chancellor184ff4f2023-10-05 10:25:27 -07001079 .freq = freq,
1080 .u_volt = volt,
Ulf Hansson248a38d52023-09-25 15:17:09 +02001081 };
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301082
Ulf Hansson248a38d52023-09-25 15:17:09 +02001083 ret = _opp_add_v1(opp_table, dev, &data, false);
Viresh Kumar04a86a82017-01-02 14:40:58 +05301084 if (ret) {
1085 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
Ulf Hansson248a38d52023-09-25 15:17:09 +02001086 __func__, data.freq, ret);
Viresh Kumar90d46d72020-09-01 15:07:09 +05301087 goto remove_static_opp;
Viresh Kumar04a86a82017-01-02 14:40:58 +05301088 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301089 nr -= 2;
1090 }
1091
Viresh Kumar1f6620f2020-10-14 09:56:28 +05301092 return 0;
1093
Viresh Kumar90d46d72020-09-01 15:07:09 +05301094remove_static_opp:
1095 _opp_remove_all_static(opp_table);
1096
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301097 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301098}
1099
Viresh Kumar1e5fb382022-07-05 09:41:56 +05301100static int _of_add_table_indexed(struct device *dev, int index)
Viresh Kumarfa9b2742017-04-26 10:45:46 +05301101{
Viresh Kumar5ed4cec2018-09-12 11:21:17 +05301102 struct opp_table *opp_table;
Viresh Kumar8a352fd2018-05-30 15:19:38 +05301103 int ret, count;
Viresh Kumarfa9b2742017-04-26 10:45:46 +05301104
Viresh Kumar5ed4cec2018-09-12 11:21:17 +05301105 if (index) {
Viresh Kumar8a352fd2018-05-30 15:19:38 +05301106 /*
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);
Viresh Kumar3e27c792018-11-23 10:36:07 +05301112 if (count == 1)
1113 index = 0;
Viresh Kumar8a352fd2018-05-30 15:19:38 +05301114 }
Viresh Kumarfa9b2742017-04-26 10:45:46 +05301115
Viresh Kumar1e5fb382022-07-05 09:41:56 +05301116 opp_table = _add_opp_table_indexed(dev, index, true);
Stephan Gerholddd461cd2020-07-27 11:30:46 +02001117 if (IS_ERR(opp_table))
1118 return PTR_ERR(opp_table);
Viresh Kumar5ed4cec2018-09-12 11:21:17 +05301119
Viresh Kumar406e4762021-01-27 12:45:56 +05301120 /*
1121 * OPPs have two version of bindings now. Also try the old (v1)
1122 * bindings for backward compatibility with older dtbs.
1123 */
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);
1128
Viresh Kumar5ed4cec2018-09-12 11:21:17 +05301129 if (ret)
1130 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumarfa9b2742017-04-26 10:45:46 +05301131
1132 return ret;
1133}
Viresh Kumar406e4762021-01-27 12:45:56 +05301134
Yangtao Li3d5cfbb2021-03-14 19:33:57 +03001135static void devm_pm_opp_of_table_release(void *data)
1136{
1137 dev_pm_opp_of_remove_table(data);
1138}
1139
Viresh Kumar1e5fb382022-07-05 09:41:56 +05301140static int _devm_of_add_table_indexed(struct device *dev, int index)
Dmitry Osipenkoe69709f2021-09-20 20:22:46 +03001141{
1142 int ret;
1143
Viresh Kumar1e5fb382022-07-05 09:41:56 +05301144 ret = _of_add_table_indexed(dev, index);
Dmitry Osipenkoe69709f2021-09-20 20:22:46 +03001145 if (ret)
1146 return ret;
1147
1148 return devm_add_action_or_reset(dev, devm_pm_opp_of_table_release, dev);
1149}
1150
Yangtao Li3d5cfbb2021-03-14 19:33:57 +03001151/**
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{
Viresh Kumar1e5fb382022-07-05 09:41:56 +05301172 return _devm_of_add_table_indexed(dev, 0);
Yangtao Li3d5cfbb2021-03-14 19:33:57 +03001173}
1174EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table);
1175
Viresh Kumar406e4762021-01-27 12:45:56 +05301176/**
1177 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
1178 * @dev: device pointer used to lookup OPP table.
1179 *
1180 * Register the initial OPP table with the OPP library for given device.
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 */
1193int dev_pm_opp_of_add_table(struct device *dev)
1194{
Viresh Kumar1e5fb382022-07-05 09:41:56 +05301195 return _of_add_table_indexed(dev, 0);
Viresh Kumar406e4762021-01-27 12:45:56 +05301196}
1197EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
1198
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{
Viresh Kumar1e5fb382022-07-05 09:41:56 +05301211 return _of_add_table_indexed(dev, index);
Viresh Kumar406e4762021-01-27 12:45:56 +05301212}
Viresh Kumarfa9b2742017-04-26 10:45:46 +05301213EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
1214
Viresh Kumar559fef02021-01-27 14:23:45 +05301215/**
Dmitry Osipenkoe69709f2021-09-20 20:22:46 +03001216 * 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{
Viresh Kumar1e5fb382022-07-05 09:41:56 +05301224 return _devm_of_add_table_indexed(dev, index);
Dmitry Osipenkoe69709f2021-09-20 20:22:46 +03001225}
1226EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table_indexed);
1227
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301228/* 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.
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301236 */
1237void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
1238{
Viresh Kumar2a4eb732018-09-13 13:14:36 +05301239 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301240}
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.
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301248 */
1249int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
1250{
1251 struct device *cpu_dev;
Viresh Kumar50b6b872018-10-03 15:12:06 +05301252 int cpu, ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301253
Viresh Kumar50b6b872018-10-03 15:12:06 +05301254 if (WARN_ON(cpumask_empty(cpumask)))
1255 return -ENODEV;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301256
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);
Viresh Kumar50b6b872018-10-03 15:12:06 +05301262 ret = -ENODEV;
1263 goto remove_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301264 }
1265
1266 ret = dev_pm_opp_of_add_table(cpu_dev);
1267 if (ret) {
Viresh Kumar5b606972017-07-12 09:21:49 +05301268 /*
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);
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301274
Viresh Kumar50b6b872018-10-03 15:12:06 +05301275 goto remove_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301276 }
1277 }
1278
Viresh Kumar50b6b872018-10-03 15:12:06 +05301279 return 0;
1280
1281remove_table:
1282 /* Free all other OPPs */
1283 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
1284
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301285 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.
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301305 */
1306int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
1307 struct cpumask *cpumask)
1308{
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +02001309 struct device_node *np, *tmp_np, *cpu_np;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301310 int cpu, ret = 0;
1311
1312 /* Get OPP descriptor node */
Dave Gerlach0764c602017-02-03 11:29:26 -06001313 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301314 if (!np) {
Masahiro Yamada349aa922016-10-20 16:12:49 +09001315 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301316 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
Sudeep Holla9867999f2017-10-12 11:32:23 +01001329 cpu_np = of_cpu_device_node_get(cpu);
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +02001330 if (!cpu_np) {
1331 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301332 __func__, cpu);
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +02001333 ret = -ENOENT;
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301334 goto put_cpu_node;
1335 }
1336
1337 /* Get OPP descriptor node */
Viresh Kumarfa9b2742017-04-26 10:45:46 +05301338 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
Sudeep Holla9867999f2017-10-12 11:32:23 +01001339 of_node_put(cpu_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301340 if (!tmp_np) {
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +02001341 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301342 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);
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301358
1359/**
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301360 * of_get_required_opp_performance_state() - Search for required OPP and return its performance state.
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301361 * @np: Node that contains the "required-opps" property.
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301362 * @index: Index of the phandle to parse.
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301363 *
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301364 * Returns the performance state of the OPP pointed out by the "required-opps"
1365 * property at @index in @np.
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301366 *
Viresh Kumar2feb5a82018-12-14 15:20:56 +05301367 * Return: Zero or positive performance state on success, otherwise negative
1368 * value on errors.
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301369 */
Viresh Kumar2feb5a82018-12-14 15:20:56 +05301370int of_get_required_opp_performance_state(struct device_node *np, int index)
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301371{
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301372 struct dev_pm_opp *opp;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301373 struct device_node *required_np;
1374 struct opp_table *opp_table;
Viresh Kumar2feb5a82018-12-14 15:20:56 +05301375 int pstate = -EINVAL;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301376
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301377 required_np = of_parse_required_opp(np, index);
1378 if (!required_np)
Rajendra Nayak020d86f2021-08-12 16:57:20 +05301379 return -ENODEV;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301380
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301381 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;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301386 }
1387
Viresh Kumar84cb7ff2023-06-14 15:57:43 +05301388 /* 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
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301394 opp = _find_opp_of_np(opp_table, required_np);
1395 if (opp) {
Viresh Kumar7c41cdc2023-06-14 15:29:32 +05301396 pstate = opp->level;
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301397 dev_pm_opp_put(opp);
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301398 }
1399
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301400 dev_pm_opp_put_opp_table(opp_table);
1401
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301402put_required_np:
1403 of_node_put(required_np);
1404
1405 return pstate;
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301406}
Viresh Kumar4c6a3432018-06-27 16:29:50 +05301407EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state);
Viresh Kumare2f4b5f2018-01-12 10:03:45 +05301408
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);
Quentin Perreta4f342b2019-02-04 11:09:48 +00001427
1428/*
1429 * Callback function provided to the Energy Model framework upon registration.
Lukasz Luba32bf8bc2022-03-02 11:29:16 +00001430 * 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
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +01001433 * frequency and @uW to the associated power.
Lukasz Luba32bf8bc2022-03-02 11:29:16 +00001434 *
1435 * Returns 0 on success or a proper -EINVAL value in case of error.
1436 */
1437static int __maybe_unused
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +01001438_get_dt_power(struct device *dev, unsigned long *uW, unsigned long *kHz)
Lukasz Luba32bf8bc2022-03-02 11:29:16 +00001439{
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;
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +01001455 *uW = opp_power;
Lukasz Luba32bf8bc2022-03-02 11:29:16 +00001456
1457 return 0;
1458}
1459
1460/*
1461 * Callback function provided to the Energy Model framework upon registration.
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001462 * This computes the power estimated by @dev at @kHz if it is the frequency
Quentin Perreta4f342b2019-02-04 11:09:48 +00001463 * 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
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +01001465 * frequency and @uW to the associated power. The power is estimated as
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001466 * 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.
Quentin Perreta4f342b2019-02-04 11:09:48 +00001468 *
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001469 * Returns -EINVAL if the power calculation failed because of missing
1470 * parameters, 0 otherwise.
Quentin Perreta4f342b2019-02-04 11:09:48 +00001471 */
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +01001472static int __maybe_unused _get_power(struct device *dev, unsigned long *uW,
Lukasz Luba75a3a992022-03-21 09:57:25 +00001473 unsigned long *kHz)
Quentin Perreta4f342b2019-02-04 11:09:48 +00001474{
Quentin Perreta4f342b2019-02-04 11:09:48 +00001475 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
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001482 np = of_node_get(dev->of_node);
Quentin Perreta4f342b2019-02-04 11:09:48 +00001483 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;
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001492 opp = dev_pm_opp_find_freq_ceil(dev, &Hz);
Quentin Perreta4f342b2019-02-04 11:09:48 +00001493 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);
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +01001502 /* Provide power in micro-Watts */
1503 do_div(tmp, 1000000);
Quentin Perreta4f342b2019-02-04 11:09:48 +00001504
Lukasz Lubaae6ccaa2022-07-07 08:15:52 +01001505 *uW = (unsigned long)tmp;
Quentin Perreta4f342b2019-02-04 11:09:48 +00001506 *kHz = Hz / 1000;
1507
1508 return 0;
1509}
1510
Lukasz Luba32bf8bc2022-03-02 11:29:16 +00001511static 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
Quentin Perreta4f342b2019-02-04 11:09:48 +00001529/**
1530 * dev_pm_opp_of_register_em() - Attempt to register an Energy Model
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001531 * @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.
Quentin Perreta4f342b2019-02-04 11:09:48 +00001534 *
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.
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001537 * Having this property means the voltages are known for OPPs and the EM
1538 * might be calculated.
Quentin Perreta4f342b2019-02-04 11:09:48 +00001539 */
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001540int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus)
Quentin Perreta4f342b2019-02-04 11:09:48 +00001541{
Lukasz Luba32bf8bc2022-03-02 11:29:16 +00001542 struct em_data_callback em_cb;
Quentin Perreta4f342b2019-02-04 11:09:48 +00001543 struct device_node *np;
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001544 int ret, nr_opp;
Quentin Perreta4f342b2019-02-04 11:09:48 +00001545 u32 cap;
1546
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001547 if (IS_ERR_OR_NULL(dev)) {
1548 ret = -EINVAL;
1549 goto failed;
1550 }
Quentin Perreta4f342b2019-02-04 11:09:48 +00001551
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001552 nr_opp = dev_pm_opp_get_opp_count(dev);
1553 if (nr_opp <= 0) {
1554 ret = -EINVAL;
1555 goto failed;
1556 }
Quentin Perreta4f342b2019-02-04 11:09:48 +00001557
Lukasz Luba32bf8bc2022-03-02 11:29:16 +00001558 /* 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
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001564 np = of_node_get(dev->of_node);
1565 if (!np) {
1566 ret = -EINVAL;
1567 goto failed;
1568 }
Quentin Perreta4f342b2019-02-04 11:09:48 +00001569
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);
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001579 if (ret || !cap) {
1580 dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n");
1581 ret = -EINVAL;
1582 goto failed;
1583 }
Quentin Perreta4f342b2019-02-04 11:09:48 +00001584
Lukasz Luba32bf8bc2022-03-02 11:29:16 +00001585 EM_SET_ACTIVE_POWER_CB(em_cb, _get_power);
1586
1587register_em:
Lukasz Lubac250d502020-11-05 12:50:01 +00001588 ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus, true);
Lukasz Luba0e0ffa82020-05-27 10:58:54 +01001589 if (ret)
1590 goto failed;
1591
1592 return 0;
1593
1594failed:
1595 dev_dbg(dev, "Couldn't register Energy Model %d\n", ret);
1596 return ret;
Quentin Perreta4f342b2019-02-04 11:09:48 +00001597}
1598EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em);