blob: c5c5afcaa2e309f3035019a091dde34e98be8b20 [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 Kumardfbe4672016-12-01 16:28:19 +053020#include <linux/slab.h>
Viresh Kumarf47b72a2016-05-05 16:20:33 +053021#include <linux/export.h>
22
23#include "opp.h"
24
25static struct opp_table *_managed_opp(const struct device_node *np)
26{
Viresh Kumarb83c1892017-01-23 10:11:45 +053027 struct opp_table *opp_table, *managed_table = NULL;
28
29 mutex_lock(&opp_table_lock);
Viresh Kumarf47b72a2016-05-05 16:20:33 +053030
Viresh Kumar052c6f12017-01-23 10:11:49 +053031 list_for_each_entry(opp_table, &opp_tables, node) {
Viresh Kumarf47b72a2016-05-05 16:20:33 +053032 if (opp_table->np == np) {
33 /*
34 * Multiple devices can point to the same OPP table and
35 * so will have same node-pointer, np.
36 *
37 * But the OPPs will be considered as shared only if the
38 * OPP table contains a "opp-shared" property.
39 */
Viresh Kumarb83c1892017-01-23 10:11:45 +053040 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
41 _get_opp_table_kref(opp_table);
42 managed_table = opp_table;
43 }
Viresh Kumar79ee2e82016-06-16 19:03:11 +053044
Viresh Kumarb83c1892017-01-23 10:11:45 +053045 break;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053046 }
47 }
48
Viresh Kumarb83c1892017-01-23 10:11:45 +053049 mutex_unlock(&opp_table_lock);
50
51 return managed_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053052}
53
54void _of_init_opp_table(struct opp_table *opp_table, struct device *dev)
55{
56 struct device_node *np;
57
58 /*
59 * Only required for backward compatibility with v1 bindings, but isn't
60 * harmful for other cases. And so we do it unconditionally.
61 */
62 np = of_node_get(dev->of_node);
63 if (np) {
64 u32 val;
65
66 if (!of_property_read_u32(np, "clock-latency", &val))
67 opp_table->clock_latency_ns_max = val;
68 of_property_read_u32(np, "voltage-tolerance",
69 &opp_table->voltage_tolerance_v1);
70 of_node_put(np);
71 }
72}
73
74static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
75 struct device_node *np)
76{
77 unsigned int count = opp_table->supported_hw_count;
78 u32 version;
79 int ret;
80
Dave Gerlacha4ee4542016-09-23 15:07:47 -050081 if (!opp_table->supported_hw) {
82 /*
83 * In the case that no supported_hw has been set by the
84 * platform but there is an opp-supported-hw value set for
85 * an OPP then the OPP should not be enabled as there is
86 * no way to see if the hardware supports it.
87 */
88 if (of_find_property(np, "opp-supported-hw", NULL))
89 return false;
90 else
91 return true;
92 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +053093
94 while (count--) {
95 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
96 &version);
97 if (ret) {
98 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
99 __func__, count, ret);
100 return false;
101 }
102
103 /* Both of these are bitwise masks of the versions */
104 if (!(version & opp_table->supported_hw[count]))
105 return false;
106 }
107
108 return true;
109}
110
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530111static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
112 struct opp_table *opp_table)
113{
Viresh Kumardfbe4672016-12-01 16:28:19 +0530114 u32 *microvolt, *microamp = NULL;
115 int supplies, vcount, icount, ret, i, j;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530116 struct property *prop = NULL;
117 char name[NAME_MAX];
118
Viresh Kumardfbe4672016-12-01 16:28:19 +0530119 supplies = opp_table->regulator_count ? opp_table->regulator_count : 1;
120
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530121 /* Search for "opp-microvolt-<name>" */
122 if (opp_table->prop_name) {
123 snprintf(name, sizeof(name), "opp-microvolt-%s",
124 opp_table->prop_name);
125 prop = of_find_property(opp->np, name, NULL);
126 }
127
128 if (!prop) {
129 /* Search for "opp-microvolt" */
130 sprintf(name, "opp-microvolt");
131 prop = of_find_property(opp->np, name, NULL);
132
133 /* Missing property isn't a problem, but an invalid entry is */
Viresh Kumar688a48b2017-05-23 09:32:12 +0530134 if (!prop) {
135 if (!opp_table->regulator_count)
136 return 0;
137
138 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
139 __func__);
140 return -EINVAL;
141 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530142 }
143
Viresh Kumardfbe4672016-12-01 16:28:19 +0530144 vcount = of_property_count_u32_elems(opp->np, name);
145 if (vcount < 0) {
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530146 dev_err(dev, "%s: Invalid %s property (%d)\n",
Viresh Kumardfbe4672016-12-01 16:28:19 +0530147 __func__, name, vcount);
148 return vcount;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530149 }
150
Viresh Kumardfbe4672016-12-01 16:28:19 +0530151 /* There can be one or three elements per supply */
152 if (vcount != supplies && vcount != supplies * 3) {
153 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
154 __func__, name, vcount, supplies);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530155 return -EINVAL;
156 }
157
Viresh Kumardfbe4672016-12-01 16:28:19 +0530158 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
159 if (!microvolt)
160 return -ENOMEM;
161
162 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530163 if (ret) {
164 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530165 ret = -EINVAL;
166 goto free_microvolt;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530167 }
168
169 /* Search for "opp-microamp-<name>" */
170 prop = NULL;
171 if (opp_table->prop_name) {
172 snprintf(name, sizeof(name), "opp-microamp-%s",
173 opp_table->prop_name);
174 prop = of_find_property(opp->np, name, NULL);
175 }
176
177 if (!prop) {
178 /* Search for "opp-microamp" */
179 sprintf(name, "opp-microamp");
180 prop = of_find_property(opp->np, name, NULL);
181 }
182
Viresh Kumardfbe4672016-12-01 16:28:19 +0530183 if (prop) {
184 icount = of_property_count_u32_elems(opp->np, name);
185 if (icount < 0) {
186 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
187 name, icount);
188 ret = icount;
189 goto free_microvolt;
190 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530191
Viresh Kumardfbe4672016-12-01 16:28:19 +0530192 if (icount != supplies) {
193 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
194 __func__, name, icount, supplies);
195 ret = -EINVAL;
196 goto free_microvolt;
197 }
198
199 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
200 if (!microamp) {
201 ret = -EINVAL;
202 goto free_microvolt;
203 }
204
205 ret = of_property_read_u32_array(opp->np, name, microamp,
206 icount);
207 if (ret) {
208 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
209 name, ret);
210 ret = -EINVAL;
211 goto free_microamp;
212 }
213 }
214
215 for (i = 0, j = 0; i < supplies; i++) {
216 opp->supplies[i].u_volt = microvolt[j++];
217
218 if (vcount == supplies) {
219 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
220 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
221 } else {
222 opp->supplies[i].u_volt_min = microvolt[j++];
223 opp->supplies[i].u_volt_max = microvolt[j++];
224 }
225
226 if (microamp)
227 opp->supplies[i].u_amp = microamp[i];
228 }
229
230free_microamp:
231 kfree(microamp);
232free_microvolt:
233 kfree(microvolt);
234
235 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530236}
237
238/**
239 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
240 * entries
241 * @dev: device pointer used to lookup OPP table.
242 *
243 * Free OPPs created using static entries present in DT.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530244 */
245void dev_pm_opp_of_remove_table(struct device *dev)
246{
Viresh Kumar9274c892017-01-02 14:41:00 +0530247 _dev_pm_opp_find_and_remove_table(dev, false);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530248}
249EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
250
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200251/* Returns opp descriptor node for a device node, caller must
252 * do of_node_put() */
253static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530254{
255 /*
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530256 * There should be only ONE phandle present in "operating-points-v2"
257 * property.
258 */
259
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200260 return of_parse_phandle(np, "operating-points-v2", 0);
261}
262
263/* Returns opp descriptor node for a device, caller must do of_node_put() */
264struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
265{
266 return _opp_of_get_opp_desc_node(dev->of_node);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530267}
Dave Gerlach0764c602017-02-03 11:29:26 -0600268EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530269
270/**
271 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530272 * @opp_table: OPP table
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530273 * @dev: device for which we do this operation
274 * @np: device node
275 *
276 * This function adds an opp definition to the opp table and returns status. The
277 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
278 * removed by dev_pm_opp_remove.
279 *
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530280 * Return:
281 * 0 On success OR
282 * Duplicate OPPs (both freq and volt are same) and opp->available
283 * -EEXIST Freq are same and volt are different OR
284 * Duplicate OPPs (both freq and volt are same) and !opp->available
285 * -ENOMEM Memory allocation failure
286 * -EINVAL Failed parsing the OPP node
287 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530288static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev,
289 struct device_node *np)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530290{
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530291 struct dev_pm_opp *new_opp;
292 u64 rate;
293 u32 val;
294 int ret;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530295 bool rate_not_available = false;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530296
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530297 new_opp = _opp_allocate(opp_table);
298 if (!new_opp)
299 return -ENOMEM;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530300
301 ret = of_property_read_u64(np, "opp-hz", &rate);
302 if (ret < 0) {
Viresh Kumara1e8c132018-04-06 14:35:45 +0530303 /* "opp-hz" is optional for devices like power domains. */
304 if (!of_find_property(dev->of_node, "#power-domain-cells",
305 NULL)) {
306 dev_err(dev, "%s: opp-hz not found\n", __func__);
307 goto free_opp;
308 }
309
310 rate_not_available = true;
311 } else {
312 /*
313 * Rate is defined as an unsigned long in clk API, and so
314 * casting explicitly to its type. Must be fixed once rate is 64
315 * bit guaranteed in clk API.
316 */
317 new_opp->rate = (unsigned long)rate;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530318 }
319
320 /* Check if the OPP supports hardware's hierarchy of versions or not */
321 if (!_opp_is_supported(dev, opp_table, np)) {
322 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
323 goto free_opp;
324 }
325
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530326 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
327
328 new_opp->np = np;
329 new_opp->dynamic = false;
330 new_opp->available = true;
331
332 if (!of_property_read_u32(np, "clock-latency-ns", &val))
333 new_opp->clock_latency_ns = val;
334
335 ret = opp_parse_supplies(new_opp, dev, opp_table);
336 if (ret)
337 goto free_opp;
338
Viresh Kumara1e8c132018-04-06 14:35:45 +0530339 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530340 if (ret) {
341 /* Don't return error for duplicate OPPs */
342 if (ret == -EBUSY)
343 ret = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530344 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530345 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530346
347 /* OPP to select on device suspend */
348 if (of_property_read_bool(np, "opp-suspend")) {
349 if (opp_table->suspend_opp) {
350 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
351 __func__, opp_table->suspend_opp->rate,
352 new_opp->rate);
353 } else {
354 new_opp->suspend = true;
355 opp_table->suspend_opp = new_opp;
356 }
357 }
358
359 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
360 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
361
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530362 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
Viresh Kumar0f0fe7e2016-12-01 16:28:17 +0530363 __func__, new_opp->turbo, new_opp->rate,
Viresh Kumardfbe4672016-12-01 16:28:19 +0530364 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
365 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530366
367 /*
368 * Notify the changes in the availability of the operable
369 * frequency/voltage list.
370 */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530371 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530372 return 0;
373
374free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530375 _opp_free(new_opp);
376
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530377 return ret;
378}
379
380/* Initializes OPP tables based on new bindings */
381static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
382{
383 struct device_node *np;
384 struct opp_table *opp_table;
385 int ret = 0, count = 0;
386
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530387 opp_table = _managed_opp(opp_np);
388 if (opp_table) {
389 /* OPPs are already managed */
390 if (!_add_opp_dev(dev, opp_table))
391 ret = -ENOMEM;
Viresh Kumarb83c1892017-01-23 10:11:45 +0530392 goto put_opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530393 }
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530394
Viresh Kumarb83c1892017-01-23 10:11:45 +0530395 opp_table = dev_pm_opp_get_opp_table(dev);
396 if (!opp_table)
397 return -ENOMEM;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530398
399 /* We have opp-table node now, iterate over it and add OPPs */
400 for_each_available_child_of_node(opp_np, np) {
401 count++;
402
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530403 ret = _opp_add_static_v2(opp_table, dev, np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530404 if (ret) {
405 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
406 ret);
Viresh Kumarb83c1892017-01-23 10:11:45 +0530407 _dev_pm_opp_remove_table(opp_table, dev, false);
Tobias Jordan7978db32017-10-04 11:35:03 +0530408 of_node_put(np);
Viresh Kumarb83c1892017-01-23 10:11:45 +0530409 goto put_opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530410 }
411 }
412
413 /* There should be one of more OPP defined */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530414 if (WARN_ON(!count)) {
415 ret = -ENOENT;
Viresh Kumarb83c1892017-01-23 10:11:45 +0530416 goto put_opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530417 }
418
419 opp_table->np = opp_np;
Viresh Kumar79ee2e82016-06-16 19:03:11 +0530420 if (of_property_read_bool(opp_np, "opp-shared"))
421 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
422 else
423 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530424
Viresh Kumarb83c1892017-01-23 10:11:45 +0530425put_opp_table:
426 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530427
428 return ret;
429}
430
431/* Initializes OPP tables based on old-deprecated bindings */
432static int _of_add_opp_table_v1(struct device *dev)
433{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530434 struct opp_table *opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530435 const struct property *prop;
436 const __be32 *val;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530437 int nr, ret = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530438
439 prop = of_find_property(dev->of_node, "operating-points", NULL);
440 if (!prop)
441 return -ENODEV;
442 if (!prop->value)
443 return -ENODATA;
444
445 /*
446 * Each OPP is a set of tuples consisting of frequency and
447 * voltage like <freq-kHz vol-uV>.
448 */
449 nr = prop->length / sizeof(u32);
450 if (nr % 2) {
451 dev_err(dev, "%s: Invalid OPP table\n", __func__);
452 return -EINVAL;
453 }
454
Viresh Kumarb83c1892017-01-23 10:11:45 +0530455 opp_table = dev_pm_opp_get_opp_table(dev);
456 if (!opp_table)
457 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530458
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530459 val = prop->value;
460 while (nr) {
461 unsigned long freq = be32_to_cpup(val++) * 1000;
462 unsigned long volt = be32_to_cpup(val++);
463
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530464 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
Viresh Kumar04a86a82017-01-02 14:40:58 +0530465 if (ret) {
466 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
467 __func__, freq, ret);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530468 _dev_pm_opp_remove_table(opp_table, dev, false);
469 break;
Viresh Kumar04a86a82017-01-02 14:40:58 +0530470 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530471 nr -= 2;
472 }
473
Viresh Kumarb83c1892017-01-23 10:11:45 +0530474 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530475 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530476}
477
478/**
479 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
480 * @dev: device pointer used to lookup OPP table.
481 *
482 * Register the initial OPP table with the OPP library for given device.
483 *
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530484 * Return:
485 * 0 On success OR
486 * Duplicate OPPs (both freq and volt are same) and opp->available
487 * -EEXIST Freq are same and volt are different OR
488 * Duplicate OPPs (both freq and volt are same) and !opp->available
489 * -ENOMEM Memory allocation failure
490 * -ENODEV when 'operating-points' property is not found or is invalid data
491 * in device node.
492 * -ENODATA when empty 'operating-points' property is found
493 * -EINVAL when invalid entries are found in opp-v2 table
494 */
495int dev_pm_opp_of_add_table(struct device *dev)
496{
497 struct device_node *opp_np;
498 int ret;
499
500 /*
501 * OPPs have two version of bindings now. The older one is deprecated,
502 * try for the new binding first.
503 */
Dave Gerlach0764c602017-02-03 11:29:26 -0600504 opp_np = dev_pm_opp_of_get_opp_desc_node(dev);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530505 if (!opp_np) {
506 /*
507 * Try old-deprecated bindings for backward compatibility with
508 * older dtbs.
509 */
510 return _of_add_opp_table_v1(dev);
511 }
512
513 ret = _of_add_opp_table_v2(dev, opp_np);
514 of_node_put(opp_np);
515
516 return ret;
517}
518EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
519
520/* CPU device specific helpers */
521
522/**
523 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
524 * @cpumask: cpumask for which OPP table needs to be removed
525 *
526 * This removes the OPP tables for CPUs present in the @cpumask.
527 * This should be used only to remove static entries created from DT.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530528 */
529void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
530{
531 _dev_pm_opp_cpumask_remove_table(cpumask, true);
532}
533EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
534
535/**
536 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
537 * @cpumask: cpumask for which OPP table needs to be added.
538 *
539 * This adds the OPP tables for CPUs present in the @cpumask.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530540 */
541int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
542{
543 struct device *cpu_dev;
544 int cpu, ret = 0;
545
546 WARN_ON(cpumask_empty(cpumask));
547
548 for_each_cpu(cpu, cpumask) {
549 cpu_dev = get_cpu_device(cpu);
550 if (!cpu_dev) {
551 pr_err("%s: failed to get cpu%d device\n", __func__,
552 cpu);
553 continue;
554 }
555
556 ret = dev_pm_opp_of_add_table(cpu_dev);
557 if (ret) {
Viresh Kumar5b606972017-07-12 09:21:49 +0530558 /*
559 * OPP may get registered dynamically, don't print error
560 * message here.
561 */
562 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
563 __func__, cpu, ret);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530564
565 /* Free all other OPPs */
566 dev_pm_opp_of_cpumask_remove_table(cpumask);
567 break;
568 }
569 }
570
571 return ret;
572}
573EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
574
575/*
576 * Works only for OPP v2 bindings.
577 *
578 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
579 */
580/**
581 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
582 * @cpu_dev using operating-points-v2
583 * bindings.
584 *
585 * @cpu_dev: CPU device for which we do this operation
586 * @cpumask: cpumask to update with information of sharing CPUs
587 *
588 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
589 *
590 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530591 */
592int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
593 struct cpumask *cpumask)
594{
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200595 struct device_node *np, *tmp_np, *cpu_np;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530596 int cpu, ret = 0;
597
598 /* Get OPP descriptor node */
Dave Gerlach0764c602017-02-03 11:29:26 -0600599 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530600 if (!np) {
Masahiro Yamada349aa922016-10-20 16:12:49 +0900601 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530602 return -ENOENT;
603 }
604
605 cpumask_set_cpu(cpu_dev->id, cpumask);
606
607 /* OPPs are shared ? */
608 if (!of_property_read_bool(np, "opp-shared"))
609 goto put_cpu_node;
610
611 for_each_possible_cpu(cpu) {
612 if (cpu == cpu_dev->id)
613 continue;
614
Sudeep Holla98679992017-10-12 11:32:23 +0100615 cpu_np = of_cpu_device_node_get(cpu);
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200616 if (!cpu_np) {
617 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530618 __func__, cpu);
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200619 ret = -ENOENT;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530620 goto put_cpu_node;
621 }
622
623 /* Get OPP descriptor node */
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200624 tmp_np = _opp_of_get_opp_desc_node(cpu_np);
Sudeep Holla98679992017-10-12 11:32:23 +0100625 of_node_put(cpu_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530626 if (!tmp_np) {
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200627 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530628 ret = -ENOENT;
629 goto put_cpu_node;
630 }
631
632 /* CPUs are sharing opp node */
633 if (np == tmp_np)
634 cpumask_set_cpu(cpu, cpumask);
635
636 of_node_put(tmp_np);
637 }
638
639put_cpu_node:
640 of_node_put(np);
641 return ret;
642}
643EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);