]>
Commit | Line | Data |
---|---|---|
f47b72a1 VK |
1 | /* |
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> | |
9867999f | 19 | #include <linux/of_device.h> |
dfbe4678 | 20 | #include <linux/slab.h> |
f47b72a1 VK |
21 | #include <linux/export.h> |
22 | ||
23 | #include "opp.h" | |
24 | ||
25 | static struct opp_table *_managed_opp(const struct device_node *np) | |
26 | { | |
b83c1899 VK |
27 | struct opp_table *opp_table, *managed_table = NULL; |
28 | ||
29 | mutex_lock(&opp_table_lock); | |
f47b72a1 | 30 | |
052c6f19 | 31 | list_for_each_entry(opp_table, &opp_tables, node) { |
f47b72a1 VK |
32 | 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 | */ | |
b83c1899 VK |
40 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { |
41 | _get_opp_table_kref(opp_table); | |
42 | managed_table = opp_table; | |
43 | } | |
79ee2e8f | 44 | |
b83c1899 | 45 | break; |
f47b72a1 VK |
46 | } |
47 | } | |
48 | ||
b83c1899 VK |
49 | mutex_unlock(&opp_table_lock); |
50 | ||
51 | return managed_table; | |
f47b72a1 VK |
52 | } |
53 | ||
54 | void _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 | ||
74 | static 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 | ||
a4ee4545 DG |
81 | 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 | } | |
f47b72a1 VK |
93 | |
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 | ||
f47b72a1 VK |
111 | static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, |
112 | struct opp_table *opp_table) | |
113 | { | |
dfbe4678 VK |
114 | u32 *microvolt, *microamp = NULL; |
115 | int supplies, vcount, icount, ret, i, j; | |
f47b72a1 VK |
116 | struct property *prop = NULL; |
117 | char name[NAME_MAX]; | |
118 | ||
dfbe4678 VK |
119 | supplies = opp_table->regulator_count ? opp_table->regulator_count : 1; |
120 | ||
f47b72a1 VK |
121 | /* 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 */ | |
688a48b0 VK |
134 | 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 | } | |
f47b72a1 VK |
142 | } |
143 | ||
dfbe4678 VK |
144 | vcount = of_property_count_u32_elems(opp->np, name); |
145 | if (vcount < 0) { | |
f47b72a1 | 146 | dev_err(dev, "%s: Invalid %s property (%d)\n", |
dfbe4678 VK |
147 | __func__, name, vcount); |
148 | return vcount; | |
f47b72a1 VK |
149 | } |
150 | ||
dfbe4678 VK |
151 | /* 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); | |
f47b72a1 VK |
155 | return -EINVAL; |
156 | } | |
157 | ||
dfbe4678 VK |
158 | 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); | |
f47b72a1 VK |
163 | if (ret) { |
164 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret); | |
dfbe4678 VK |
165 | ret = -EINVAL; |
166 | goto free_microvolt; | |
f47b72a1 VK |
167 | } |
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 | ||
dfbe4678 VK |
183 | 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 | } | |
f47b72a1 | 191 | |
dfbe4678 VK |
192 | 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 | ||
230 | free_microamp: | |
231 | kfree(microamp); | |
232 | free_microvolt: | |
233 | kfree(microvolt); | |
234 | ||
235 | return ret; | |
f47b72a1 VK |
236 | } |
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. | |
f47b72a1 VK |
244 | */ |
245 | void dev_pm_opp_of_remove_table(struct device *dev) | |
246 | { | |
9274c892 | 247 | _dev_pm_opp_find_and_remove_table(dev, false); |
f47b72a1 VK |
248 | } |
249 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table); | |
250 | ||
76279291 WR |
251 | /* Returns opp descriptor node for a device node, caller must |
252 | * do of_node_put() */ | |
fa9b274f VK |
253 | static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np, |
254 | int index) | |
f47b72a1 | 255 | { |
fa9b274f VK |
256 | /* "operating-points-v2" can be an array for power domain providers */ |
257 | return of_parse_phandle(np, "operating-points-v2", index); | |
76279291 WR |
258 | } |
259 | ||
260 | /* Returns opp descriptor node for a device, caller must do of_node_put() */ | |
261 | struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) | |
262 | { | |
fa9b274f | 263 | return _opp_of_get_opp_desc_node(dev->of_node, 0); |
f47b72a1 | 264 | } |
0764c604 | 265 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node); |
f47b72a1 VK |
266 | |
267 | /** | |
268 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) | |
8cd2f6e8 | 269 | * @opp_table: OPP table |
f47b72a1 VK |
270 | * @dev: device for which we do this operation |
271 | * @np: device node | |
272 | * | |
273 | * This function adds an opp definition to the opp table and returns status. The | |
274 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be | |
275 | * removed by dev_pm_opp_remove. | |
276 | * | |
f47b72a1 VK |
277 | * Return: |
278 | * 0 On success OR | |
279 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
280 | * -EEXIST Freq are same and volt are different OR | |
281 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
282 | * -ENOMEM Memory allocation failure | |
283 | * -EINVAL Failed parsing the OPP node | |
284 | */ | |
8cd2f6e8 VK |
285 | static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev, |
286 | struct device_node *np) | |
f47b72a1 | 287 | { |
f47b72a1 VK |
288 | struct dev_pm_opp *new_opp; |
289 | u64 rate; | |
290 | u32 val; | |
291 | int ret; | |
a1e8c136 | 292 | bool rate_not_available = false; |
f47b72a1 | 293 | |
8cd2f6e8 VK |
294 | new_opp = _opp_allocate(opp_table); |
295 | if (!new_opp) | |
296 | return -ENOMEM; | |
f47b72a1 VK |
297 | |
298 | ret = of_property_read_u64(np, "opp-hz", &rate); | |
299 | if (ret < 0) { | |
a1e8c136 VK |
300 | /* "opp-hz" is optional for devices like power domains. */ |
301 | if (!of_find_property(dev->of_node, "#power-domain-cells", | |
302 | NULL)) { | |
303 | dev_err(dev, "%s: opp-hz not found\n", __func__); | |
304 | goto free_opp; | |
305 | } | |
306 | ||
307 | rate_not_available = true; | |
308 | } else { | |
309 | /* | |
310 | * Rate is defined as an unsigned long in clk API, and so | |
311 | * casting explicitly to its type. Must be fixed once rate is 64 | |
312 | * bit guaranteed in clk API. | |
313 | */ | |
314 | new_opp->rate = (unsigned long)rate; | |
f47b72a1 VK |
315 | } |
316 | ||
317 | /* Check if the OPP supports hardware's hierarchy of versions or not */ | |
318 | if (!_opp_is_supported(dev, opp_table, np)) { | |
319 | dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate); | |
320 | goto free_opp; | |
321 | } | |
322 | ||
f47b72a1 VK |
323 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); |
324 | ||
325 | new_opp->np = np; | |
326 | new_opp->dynamic = false; | |
327 | new_opp->available = true; | |
328 | ||
329 | if (!of_property_read_u32(np, "clock-latency-ns", &val)) | |
330 | new_opp->clock_latency_ns = val; | |
331 | ||
332 | ret = opp_parse_supplies(new_opp, dev, opp_table); | |
333 | if (ret) | |
334 | goto free_opp; | |
335 | ||
a1e8c136 | 336 | ret = _opp_add(dev, new_opp, opp_table, rate_not_available); |
7f8538eb VK |
337 | if (ret) { |
338 | /* Don't return error for duplicate OPPs */ | |
339 | if (ret == -EBUSY) | |
340 | ret = 0; | |
f47b72a1 | 341 | goto free_opp; |
7f8538eb | 342 | } |
f47b72a1 VK |
343 | |
344 | /* OPP to select on device suspend */ | |
345 | if (of_property_read_bool(np, "opp-suspend")) { | |
346 | if (opp_table->suspend_opp) { | |
347 | dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n", | |
348 | __func__, opp_table->suspend_opp->rate, | |
349 | new_opp->rate); | |
350 | } else { | |
351 | new_opp->suspend = true; | |
352 | opp_table->suspend_opp = new_opp; | |
353 | } | |
354 | } | |
355 | ||
356 | if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max) | |
357 | opp_table->clock_latency_ns_max = new_opp->clock_latency_ns; | |
358 | ||
f47b72a1 | 359 | pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", |
0f0fe7e0 | 360 | __func__, new_opp->turbo, new_opp->rate, |
dfbe4678 VK |
361 | new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min, |
362 | new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns); | |
f47b72a1 VK |
363 | |
364 | /* | |
365 | * Notify the changes in the availability of the operable | |
366 | * frequency/voltage list. | |
367 | */ | |
052c6f19 | 368 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
f47b72a1 VK |
369 | return 0; |
370 | ||
371 | free_opp: | |
8cd2f6e8 VK |
372 | _opp_free(new_opp); |
373 | ||
f47b72a1 VK |
374 | return ret; |
375 | } | |
376 | ||
377 | /* Initializes OPP tables based on new bindings */ | |
378 | static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np) | |
379 | { | |
380 | struct device_node *np; | |
381 | struct opp_table *opp_table; | |
382 | int ret = 0, count = 0; | |
383 | ||
f47b72a1 VK |
384 | opp_table = _managed_opp(opp_np); |
385 | if (opp_table) { | |
386 | /* OPPs are already managed */ | |
387 | if (!_add_opp_dev(dev, opp_table)) | |
388 | ret = -ENOMEM; | |
b83c1899 | 389 | goto put_opp_table; |
8cd2f6e8 VK |
390 | } |
391 | ||
b83c1899 VK |
392 | opp_table = dev_pm_opp_get_opp_table(dev); |
393 | if (!opp_table) | |
394 | return -ENOMEM; | |
f47b72a1 VK |
395 | |
396 | /* We have opp-table node now, iterate over it and add OPPs */ | |
397 | for_each_available_child_of_node(opp_np, np) { | |
398 | count++; | |
399 | ||
8cd2f6e8 | 400 | ret = _opp_add_static_v2(opp_table, dev, np); |
f47b72a1 VK |
401 | if (ret) { |
402 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, | |
403 | ret); | |
b83c1899 | 404 | _dev_pm_opp_remove_table(opp_table, dev, false); |
7978db34 | 405 | of_node_put(np); |
b83c1899 | 406 | goto put_opp_table; |
f47b72a1 VK |
407 | } |
408 | } | |
409 | ||
410 | /* There should be one of more OPP defined */ | |
8cd2f6e8 VK |
411 | if (WARN_ON(!count)) { |
412 | ret = -ENOENT; | |
b83c1899 | 413 | goto put_opp_table; |
f47b72a1 VK |
414 | } |
415 | ||
416 | opp_table->np = opp_np; | |
79ee2e8f VK |
417 | if (of_property_read_bool(opp_np, "opp-shared")) |
418 | opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; | |
419 | else | |
420 | opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; | |
f47b72a1 | 421 | |
b83c1899 VK |
422 | put_opp_table: |
423 | dev_pm_opp_put_opp_table(opp_table); | |
f47b72a1 VK |
424 | |
425 | return ret; | |
426 | } | |
427 | ||
428 | /* Initializes OPP tables based on old-deprecated bindings */ | |
429 | static int _of_add_opp_table_v1(struct device *dev) | |
430 | { | |
8cd2f6e8 | 431 | struct opp_table *opp_table; |
f47b72a1 VK |
432 | const struct property *prop; |
433 | const __be32 *val; | |
8cd2f6e8 | 434 | int nr, ret = 0; |
f47b72a1 VK |
435 | |
436 | prop = of_find_property(dev->of_node, "operating-points", NULL); | |
437 | if (!prop) | |
438 | return -ENODEV; | |
439 | if (!prop->value) | |
440 | return -ENODATA; | |
441 | ||
442 | /* | |
443 | * Each OPP is a set of tuples consisting of frequency and | |
444 | * voltage like <freq-kHz vol-uV>. | |
445 | */ | |
446 | nr = prop->length / sizeof(u32); | |
447 | if (nr % 2) { | |
448 | dev_err(dev, "%s: Invalid OPP table\n", __func__); | |
449 | return -EINVAL; | |
450 | } | |
451 | ||
b83c1899 VK |
452 | opp_table = dev_pm_opp_get_opp_table(dev); |
453 | if (!opp_table) | |
454 | return -ENOMEM; | |
8cd2f6e8 | 455 | |
f47b72a1 VK |
456 | val = prop->value; |
457 | while (nr) { | |
458 | unsigned long freq = be32_to_cpup(val++) * 1000; | |
459 | unsigned long volt = be32_to_cpup(val++); | |
460 | ||
8cd2f6e8 | 461 | ret = _opp_add_v1(opp_table, dev, freq, volt, false); |
04a86a84 VK |
462 | if (ret) { |
463 | dev_err(dev, "%s: Failed to add OPP %ld (%d)\n", | |
464 | __func__, freq, ret); | |
8cd2f6e8 VK |
465 | _dev_pm_opp_remove_table(opp_table, dev, false); |
466 | break; | |
04a86a84 | 467 | } |
f47b72a1 VK |
468 | nr -= 2; |
469 | } | |
470 | ||
b83c1899 | 471 | dev_pm_opp_put_opp_table(opp_table); |
8cd2f6e8 | 472 | return ret; |
f47b72a1 VK |
473 | } |
474 | ||
475 | /** | |
476 | * dev_pm_opp_of_add_table() - Initialize opp table from device tree | |
477 | * @dev: device pointer used to lookup OPP table. | |
478 | * | |
479 | * Register the initial OPP table with the OPP library for given device. | |
480 | * | |
f47b72a1 VK |
481 | * Return: |
482 | * 0 On success OR | |
483 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
484 | * -EEXIST Freq are same and volt are different OR | |
485 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
486 | * -ENOMEM Memory allocation failure | |
487 | * -ENODEV when 'operating-points' property is not found or is invalid data | |
488 | * in device node. | |
489 | * -ENODATA when empty 'operating-points' property is found | |
490 | * -EINVAL when invalid entries are found in opp-v2 table | |
491 | */ | |
492 | int dev_pm_opp_of_add_table(struct device *dev) | |
493 | { | |
494 | struct device_node *opp_np; | |
495 | int ret; | |
496 | ||
497 | /* | |
498 | * OPPs have two version of bindings now. The older one is deprecated, | |
499 | * try for the new binding first. | |
500 | */ | |
0764c604 | 501 | opp_np = dev_pm_opp_of_get_opp_desc_node(dev); |
f47b72a1 VK |
502 | if (!opp_np) { |
503 | /* | |
504 | * Try old-deprecated bindings for backward compatibility with | |
505 | * older dtbs. | |
506 | */ | |
507 | return _of_add_opp_table_v1(dev); | |
508 | } | |
509 | ||
510 | ret = _of_add_opp_table_v2(dev, opp_np); | |
511 | of_node_put(opp_np); | |
512 | ||
513 | return ret; | |
514 | } | |
515 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table); | |
516 | ||
fa9b274f VK |
517 | /** |
518 | * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree | |
519 | * @dev: device pointer used to lookup OPP table. | |
520 | * @index: Index number. | |
521 | * | |
522 | * Register the initial OPP table with the OPP library for given device only | |
523 | * using the "operating-points-v2" property. | |
524 | * | |
525 | * Return: | |
526 | * 0 On success OR | |
527 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
528 | * -EEXIST Freq are same and volt are different OR | |
529 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
530 | * -ENOMEM Memory allocation failure | |
531 | * -ENODEV when 'operating-points' property is not found or is invalid data | |
532 | * in device node. | |
533 | * -ENODATA when empty 'operating-points' property is found | |
534 | * -EINVAL when invalid entries are found in opp-v2 table | |
535 | */ | |
536 | int dev_pm_opp_of_add_table_indexed(struct device *dev, int index) | |
537 | { | |
538 | struct device_node *opp_np; | |
539 | int ret; | |
540 | ||
541 | opp_np = _opp_of_get_opp_desc_node(dev->of_node, index); | |
542 | if (!opp_np) | |
543 | return -ENODEV; | |
544 | ||
545 | ret = _of_add_opp_table_v2(dev, opp_np); | |
546 | of_node_put(opp_np); | |
547 | ||
548 | return ret; | |
549 | } | |
550 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed); | |
551 | ||
f47b72a1 VK |
552 | /* CPU device specific helpers */ |
553 | ||
554 | /** | |
555 | * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask | |
556 | * @cpumask: cpumask for which OPP table needs to be removed | |
557 | * | |
558 | * This removes the OPP tables for CPUs present in the @cpumask. | |
559 | * This should be used only to remove static entries created from DT. | |
f47b72a1 VK |
560 | */ |
561 | void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask) | |
562 | { | |
563 | _dev_pm_opp_cpumask_remove_table(cpumask, true); | |
564 | } | |
565 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table); | |
566 | ||
567 | /** | |
568 | * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask | |
569 | * @cpumask: cpumask for which OPP table needs to be added. | |
570 | * | |
571 | * This adds the OPP tables for CPUs present in the @cpumask. | |
f47b72a1 VK |
572 | */ |
573 | int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) | |
574 | { | |
575 | struct device *cpu_dev; | |
576 | int cpu, ret = 0; | |
577 | ||
578 | WARN_ON(cpumask_empty(cpumask)); | |
579 | ||
580 | for_each_cpu(cpu, cpumask) { | |
581 | cpu_dev = get_cpu_device(cpu); | |
582 | if (!cpu_dev) { | |
583 | pr_err("%s: failed to get cpu%d device\n", __func__, | |
584 | cpu); | |
585 | continue; | |
586 | } | |
587 | ||
588 | ret = dev_pm_opp_of_add_table(cpu_dev); | |
589 | if (ret) { | |
5b60697c VK |
590 | /* |
591 | * OPP may get registered dynamically, don't print error | |
592 | * message here. | |
593 | */ | |
594 | pr_debug("%s: couldn't find opp table for cpu:%d, %d\n", | |
595 | __func__, cpu, ret); | |
f47b72a1 VK |
596 | |
597 | /* Free all other OPPs */ | |
598 | dev_pm_opp_of_cpumask_remove_table(cpumask); | |
599 | break; | |
600 | } | |
601 | } | |
602 | ||
603 | return ret; | |
604 | } | |
605 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table); | |
606 | ||
607 | /* | |
608 | * Works only for OPP v2 bindings. | |
609 | * | |
610 | * Returns -ENOENT if operating-points-v2 bindings aren't supported. | |
611 | */ | |
612 | /** | |
613 | * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with | |
614 | * @cpu_dev using operating-points-v2 | |
615 | * bindings. | |
616 | * | |
617 | * @cpu_dev: CPU device for which we do this operation | |
618 | * @cpumask: cpumask to update with information of sharing CPUs | |
619 | * | |
620 | * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. | |
621 | * | |
622 | * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev. | |
f47b72a1 VK |
623 | */ |
624 | int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, | |
625 | struct cpumask *cpumask) | |
626 | { | |
76279291 | 627 | struct device_node *np, *tmp_np, *cpu_np; |
f47b72a1 VK |
628 | int cpu, ret = 0; |
629 | ||
630 | /* Get OPP descriptor node */ | |
0764c604 | 631 | np = dev_pm_opp_of_get_opp_desc_node(cpu_dev); |
f47b72a1 | 632 | if (!np) { |
349aa92e | 633 | dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__); |
f47b72a1 VK |
634 | return -ENOENT; |
635 | } | |
636 | ||
637 | cpumask_set_cpu(cpu_dev->id, cpumask); | |
638 | ||
639 | /* OPPs are shared ? */ | |
640 | if (!of_property_read_bool(np, "opp-shared")) | |
641 | goto put_cpu_node; | |
642 | ||
643 | for_each_possible_cpu(cpu) { | |
644 | if (cpu == cpu_dev->id) | |
645 | continue; | |
646 | ||
9867999f | 647 | cpu_np = of_cpu_device_node_get(cpu); |
76279291 WR |
648 | if (!cpu_np) { |
649 | dev_err(cpu_dev, "%s: failed to get cpu%d node\n", | |
f47b72a1 | 650 | __func__, cpu); |
76279291 | 651 | ret = -ENOENT; |
f47b72a1 VK |
652 | goto put_cpu_node; |
653 | } | |
654 | ||
655 | /* Get OPP descriptor node */ | |
fa9b274f | 656 | tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0); |
9867999f | 657 | of_node_put(cpu_np); |
f47b72a1 | 658 | if (!tmp_np) { |
76279291 | 659 | pr_err("%pOF: Couldn't find opp node\n", cpu_np); |
f47b72a1 VK |
660 | ret = -ENOENT; |
661 | goto put_cpu_node; | |
662 | } | |
663 | ||
664 | /* CPUs are sharing opp node */ | |
665 | if (np == tmp_np) | |
666 | cpumask_set_cpu(cpu, cpumask); | |
667 | ||
668 | of_node_put(tmp_np); | |
669 | } | |
670 | ||
671 | put_cpu_node: | |
672 | of_node_put(np); | |
673 | return ret; | |
674 | } | |
675 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus); | |
a88bd2a5 VK |
676 | |
677 | /** | |
678 | * of_dev_pm_opp_find_required_opp() - Search for required OPP. | |
679 | * @dev: The device whose OPP node is referenced by the 'np' DT node. | |
680 | * @np: Node that contains the "required-opps" property. | |
681 | * | |
682 | * Returns the OPP of the device 'dev', whose phandle is present in the "np" | |
683 | * node. Although the "required-opps" property supports having multiple | |
684 | * phandles, this helper routine only parses the very first phandle in the list. | |
685 | * | |
686 | * Return: Matching opp, else returns ERR_PTR in case of error and should be | |
687 | * handled using IS_ERR. | |
688 | * | |
689 | * The callers are required to call dev_pm_opp_put() for the returned OPP after | |
690 | * use. | |
691 | */ | |
692 | struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev, | |
693 | struct device_node *np) | |
694 | { | |
695 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV); | |
696 | struct device_node *required_np; | |
697 | struct opp_table *opp_table; | |
698 | ||
699 | opp_table = _find_opp_table(dev); | |
700 | if (IS_ERR(opp_table)) | |
701 | return ERR_CAST(opp_table); | |
702 | ||
703 | required_np = of_parse_phandle(np, "required-opps", 0); | |
704 | if (unlikely(!required_np)) { | |
705 | dev_err(dev, "Unable to parse required-opps\n"); | |
706 | goto put_opp_table; | |
707 | } | |
708 | ||
709 | mutex_lock(&opp_table->lock); | |
710 | ||
711 | list_for_each_entry(temp_opp, &opp_table->opp_list, node) { | |
712 | if (temp_opp->available && temp_opp->np == required_np) { | |
713 | opp = temp_opp; | |
714 | ||
715 | /* Increment the reference count of OPP */ | |
716 | dev_pm_opp_get(opp); | |
717 | break; | |
718 | } | |
719 | } | |
720 | ||
721 | mutex_unlock(&opp_table->lock); | |
722 | ||
723 | of_node_put(required_np); | |
724 | put_opp_table: | |
725 | dev_pm_opp_put_opp_table(opp_table); | |
726 | ||
727 | return opp; | |
728 | } | |
729 | EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp); |