]>
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> |
3ba98324 | 20 | #include <linux/pm_domain.h> |
dfbe4678 | 21 | #include <linux/slab.h> |
f47b72a1 VK |
22 | #include <linux/export.h> |
23 | ||
24 | #include "opp.h" | |
25 | ||
26 | static struct opp_table *_managed_opp(const struct device_node *np) | |
27 | { | |
b83c1899 VK |
28 | struct opp_table *opp_table, *managed_table = NULL; |
29 | ||
30 | mutex_lock(&opp_table_lock); | |
f47b72a1 | 31 | |
052c6f19 | 32 | list_for_each_entry(opp_table, &opp_tables, node) { |
f47b72a1 VK |
33 | if (opp_table->np == np) { |
34 | /* | |
35 | * Multiple devices can point to the same OPP table and | |
36 | * so will have same node-pointer, np. | |
37 | * | |
38 | * But the OPPs will be considered as shared only if the | |
39 | * OPP table contains a "opp-shared" property. | |
40 | */ | |
b83c1899 VK |
41 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { |
42 | _get_opp_table_kref(opp_table); | |
43 | managed_table = opp_table; | |
44 | } | |
79ee2e8f | 45 | |
b83c1899 | 46 | break; |
f47b72a1 VK |
47 | } |
48 | } | |
49 | ||
b83c1899 VK |
50 | mutex_unlock(&opp_table_lock); |
51 | ||
52 | return managed_table; | |
f47b72a1 VK |
53 | } |
54 | ||
eb7c8743 VK |
55 | void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, |
56 | int index) | |
f47b72a1 VK |
57 | { |
58 | struct device_node *np; | |
59 | ||
60 | /* | |
61 | * Only required for backward compatibility with v1 bindings, but isn't | |
62 | * harmful for other cases. And so we do it unconditionally. | |
63 | */ | |
64 | np = of_node_get(dev->of_node); | |
65 | if (np) { | |
66 | u32 val; | |
67 | ||
68 | if (!of_property_read_u32(np, "clock-latency", &val)) | |
69 | opp_table->clock_latency_ns_max = val; | |
70 | of_property_read_u32(np, "voltage-tolerance", | |
71 | &opp_table->voltage_tolerance_v1); | |
72 | of_node_put(np); | |
73 | } | |
74 | } | |
75 | ||
76 | static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table, | |
77 | struct device_node *np) | |
78 | { | |
79 | unsigned int count = opp_table->supported_hw_count; | |
80 | u32 version; | |
81 | int ret; | |
82 | ||
a4ee4545 DG |
83 | if (!opp_table->supported_hw) { |
84 | /* | |
85 | * In the case that no supported_hw has been set by the | |
86 | * platform but there is an opp-supported-hw value set for | |
87 | * an OPP then the OPP should not be enabled as there is | |
88 | * no way to see if the hardware supports it. | |
89 | */ | |
90 | if (of_find_property(np, "opp-supported-hw", NULL)) | |
91 | return false; | |
92 | else | |
93 | return true; | |
94 | } | |
f47b72a1 VK |
95 | |
96 | while (count--) { | |
97 | ret = of_property_read_u32_index(np, "opp-supported-hw", count, | |
98 | &version); | |
99 | if (ret) { | |
100 | dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n", | |
101 | __func__, count, ret); | |
102 | return false; | |
103 | } | |
104 | ||
105 | /* Both of these are bitwise masks of the versions */ | |
106 | if (!(version & opp_table->supported_hw[count])) | |
107 | return false; | |
108 | } | |
109 | ||
110 | return true; | |
111 | } | |
112 | ||
f47b72a1 VK |
113 | static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, |
114 | struct opp_table *opp_table) | |
115 | { | |
dfbe4678 VK |
116 | u32 *microvolt, *microamp = NULL; |
117 | int supplies, vcount, icount, ret, i, j; | |
f47b72a1 VK |
118 | struct property *prop = NULL; |
119 | char name[NAME_MAX]; | |
120 | ||
dfbe4678 VK |
121 | supplies = opp_table->regulator_count ? opp_table->regulator_count : 1; |
122 | ||
f47b72a1 VK |
123 | /* Search for "opp-microvolt-<name>" */ |
124 | if (opp_table->prop_name) { | |
125 | snprintf(name, sizeof(name), "opp-microvolt-%s", | |
126 | opp_table->prop_name); | |
127 | prop = of_find_property(opp->np, name, NULL); | |
128 | } | |
129 | ||
130 | if (!prop) { | |
131 | /* Search for "opp-microvolt" */ | |
132 | sprintf(name, "opp-microvolt"); | |
133 | prop = of_find_property(opp->np, name, NULL); | |
134 | ||
135 | /* Missing property isn't a problem, but an invalid entry is */ | |
688a48b0 VK |
136 | if (!prop) { |
137 | if (!opp_table->regulator_count) | |
138 | return 0; | |
139 | ||
140 | dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n", | |
141 | __func__); | |
142 | return -EINVAL; | |
143 | } | |
f47b72a1 VK |
144 | } |
145 | ||
dfbe4678 VK |
146 | vcount = of_property_count_u32_elems(opp->np, name); |
147 | if (vcount < 0) { | |
f47b72a1 | 148 | dev_err(dev, "%s: Invalid %s property (%d)\n", |
dfbe4678 VK |
149 | __func__, name, vcount); |
150 | return vcount; | |
f47b72a1 VK |
151 | } |
152 | ||
dfbe4678 VK |
153 | /* There can be one or three elements per supply */ |
154 | if (vcount != supplies && vcount != supplies * 3) { | |
155 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", | |
156 | __func__, name, vcount, supplies); | |
f47b72a1 VK |
157 | return -EINVAL; |
158 | } | |
159 | ||
dfbe4678 VK |
160 | microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL); |
161 | if (!microvolt) | |
162 | return -ENOMEM; | |
163 | ||
164 | ret = of_property_read_u32_array(opp->np, name, microvolt, vcount); | |
f47b72a1 VK |
165 | if (ret) { |
166 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret); | |
dfbe4678 VK |
167 | ret = -EINVAL; |
168 | goto free_microvolt; | |
f47b72a1 VK |
169 | } |
170 | ||
171 | /* Search for "opp-microamp-<name>" */ | |
172 | prop = NULL; | |
173 | if (opp_table->prop_name) { | |
174 | snprintf(name, sizeof(name), "opp-microamp-%s", | |
175 | opp_table->prop_name); | |
176 | prop = of_find_property(opp->np, name, NULL); | |
177 | } | |
178 | ||
179 | if (!prop) { | |
180 | /* Search for "opp-microamp" */ | |
181 | sprintf(name, "opp-microamp"); | |
182 | prop = of_find_property(opp->np, name, NULL); | |
183 | } | |
184 | ||
dfbe4678 VK |
185 | if (prop) { |
186 | icount = of_property_count_u32_elems(opp->np, name); | |
187 | if (icount < 0) { | |
188 | dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, | |
189 | name, icount); | |
190 | ret = icount; | |
191 | goto free_microvolt; | |
192 | } | |
f47b72a1 | 193 | |
dfbe4678 VK |
194 | if (icount != supplies) { |
195 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", | |
196 | __func__, name, icount, supplies); | |
197 | ret = -EINVAL; | |
198 | goto free_microvolt; | |
199 | } | |
200 | ||
201 | microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL); | |
202 | if (!microamp) { | |
203 | ret = -EINVAL; | |
204 | goto free_microvolt; | |
205 | } | |
206 | ||
207 | ret = of_property_read_u32_array(opp->np, name, microamp, | |
208 | icount); | |
209 | if (ret) { | |
210 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, | |
211 | name, ret); | |
212 | ret = -EINVAL; | |
213 | goto free_microamp; | |
214 | } | |
215 | } | |
216 | ||
217 | for (i = 0, j = 0; i < supplies; i++) { | |
218 | opp->supplies[i].u_volt = microvolt[j++]; | |
219 | ||
220 | if (vcount == supplies) { | |
221 | opp->supplies[i].u_volt_min = opp->supplies[i].u_volt; | |
222 | opp->supplies[i].u_volt_max = opp->supplies[i].u_volt; | |
223 | } else { | |
224 | opp->supplies[i].u_volt_min = microvolt[j++]; | |
225 | opp->supplies[i].u_volt_max = microvolt[j++]; | |
226 | } | |
227 | ||
228 | if (microamp) | |
229 | opp->supplies[i].u_amp = microamp[i]; | |
230 | } | |
231 | ||
232 | free_microamp: | |
233 | kfree(microamp); | |
234 | free_microvolt: | |
235 | kfree(microvolt); | |
236 | ||
237 | return ret; | |
f47b72a1 VK |
238 | } |
239 | ||
240 | /** | |
241 | * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT | |
242 | * entries | |
243 | * @dev: device pointer used to lookup OPP table. | |
244 | * | |
245 | * Free OPPs created using static entries present in DT. | |
f47b72a1 VK |
246 | */ |
247 | void dev_pm_opp_of_remove_table(struct device *dev) | |
248 | { | |
9274c892 | 249 | _dev_pm_opp_find_and_remove_table(dev, false); |
f47b72a1 VK |
250 | } |
251 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table); | |
252 | ||
76279291 WR |
253 | /* Returns opp descriptor node for a device node, caller must |
254 | * do of_node_put() */ | |
fa9b274f VK |
255 | static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np, |
256 | int index) | |
f47b72a1 | 257 | { |
fa9b274f VK |
258 | /* "operating-points-v2" can be an array for power domain providers */ |
259 | return of_parse_phandle(np, "operating-points-v2", index); | |
76279291 WR |
260 | } |
261 | ||
262 | /* Returns opp descriptor node for a device, caller must do of_node_put() */ | |
263 | struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) | |
264 | { | |
fa9b274f | 265 | return _opp_of_get_opp_desc_node(dev->of_node, 0); |
f47b72a1 | 266 | } |
0764c604 | 267 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node); |
f47b72a1 VK |
268 | |
269 | /** | |
270 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) | |
8cd2f6e8 | 271 | * @opp_table: OPP table |
f47b72a1 VK |
272 | * @dev: device for which we do this operation |
273 | * @np: device node | |
274 | * | |
275 | * This function adds an opp definition to the opp table and returns status. The | |
276 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be | |
277 | * removed by dev_pm_opp_remove. | |
278 | * | |
f47b72a1 VK |
279 | * Return: |
280 | * 0 On success OR | |
281 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
282 | * -EEXIST Freq are same and volt are different OR | |
283 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
284 | * -ENOMEM Memory allocation failure | |
285 | * -EINVAL Failed parsing the OPP node | |
286 | */ | |
8cd2f6e8 VK |
287 | static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev, |
288 | struct device_node *np) | |
f47b72a1 | 289 | { |
f47b72a1 | 290 | struct dev_pm_opp *new_opp; |
6a89e012 | 291 | u64 rate = 0; |
f47b72a1 VK |
292 | u32 val; |
293 | int ret; | |
a1e8c136 | 294 | bool rate_not_available = false; |
f47b72a1 | 295 | |
8cd2f6e8 VK |
296 | new_opp = _opp_allocate(opp_table); |
297 | if (!new_opp) | |
298 | return -ENOMEM; | |
f47b72a1 VK |
299 | |
300 | ret = of_property_read_u64(np, "opp-hz", &rate); | |
301 | if (ret < 0) { | |
a1e8c136 VK |
302 | /* "opp-hz" is optional for devices like power domains. */ |
303 | if (!of_find_property(dev->of_node, "#power-domain-cells", | |
304 | NULL)) { | |
305 | dev_err(dev, "%s: opp-hz not found\n", __func__); | |
306 | goto free_opp; | |
307 | } | |
308 | ||
309 | rate_not_available = true; | |
310 | } else { | |
311 | /* | |
312 | * Rate is defined as an unsigned long in clk API, and so | |
313 | * casting explicitly to its type. Must be fixed once rate is 64 | |
314 | * bit guaranteed in clk API. | |
315 | */ | |
316 | new_opp->rate = (unsigned long)rate; | |
f47b72a1 VK |
317 | } |
318 | ||
319 | /* Check if the OPP supports hardware's hierarchy of versions or not */ | |
320 | if (!_opp_is_supported(dev, opp_table, np)) { | |
321 | dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate); | |
322 | goto free_opp; | |
323 | } | |
324 | ||
f47b72a1 VK |
325 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); |
326 | ||
327 | new_opp->np = np; | |
328 | new_opp->dynamic = false; | |
329 | new_opp->available = true; | |
330 | ||
331 | if (!of_property_read_u32(np, "clock-latency-ns", &val)) | |
332 | new_opp->clock_latency_ns = val; | |
333 | ||
3ba98324 VK |
334 | new_opp->pstate = of_genpd_opp_to_performance_state(dev, np); |
335 | ||
f47b72a1 VK |
336 | ret = opp_parse_supplies(new_opp, dev, opp_table); |
337 | if (ret) | |
338 | goto free_opp; | |
339 | ||
a1e8c136 | 340 | ret = _opp_add(dev, new_opp, opp_table, rate_not_available); |
7f8538eb VK |
341 | if (ret) { |
342 | /* Don't return error for duplicate OPPs */ | |
343 | if (ret == -EBUSY) | |
344 | ret = 0; | |
f47b72a1 | 345 | goto free_opp; |
7f8538eb | 346 | } |
f47b72a1 VK |
347 | |
348 | /* OPP to select on device suspend */ | |
349 | if (of_property_read_bool(np, "opp-suspend")) { | |
350 | if (opp_table->suspend_opp) { | |
351 | dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n", | |
352 | __func__, opp_table->suspend_opp->rate, | |
353 | new_opp->rate); | |
354 | } else { | |
355 | new_opp->suspend = true; | |
356 | opp_table->suspend_opp = new_opp; | |
357 | } | |
358 | } | |
359 | ||
360 | if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max) | |
361 | opp_table->clock_latency_ns_max = new_opp->clock_latency_ns; | |
362 | ||
f47b72a1 | 363 | pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", |
0f0fe7e0 | 364 | __func__, new_opp->turbo, new_opp->rate, |
dfbe4678 VK |
365 | new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min, |
366 | new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns); | |
f47b72a1 VK |
367 | |
368 | /* | |
369 | * Notify the changes in the availability of the operable | |
370 | * frequency/voltage list. | |
371 | */ | |
052c6f19 | 372 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
f47b72a1 VK |
373 | return 0; |
374 | ||
375 | free_opp: | |
8cd2f6e8 VK |
376 | _opp_free(new_opp); |
377 | ||
f47b72a1 VK |
378 | return ret; |
379 | } | |
380 | ||
381 | /* Initializes OPP tables based on new bindings */ | |
eb7c8743 VK |
382 | static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np, |
383 | int index) | |
f47b72a1 VK |
384 | { |
385 | struct device_node *np; | |
386 | struct opp_table *opp_table; | |
3ba98324 VK |
387 | int ret = 0, count = 0, pstate_count = 0; |
388 | struct dev_pm_opp *opp; | |
f47b72a1 | 389 | |
f47b72a1 VK |
390 | opp_table = _managed_opp(opp_np); |
391 | if (opp_table) { | |
392 | /* OPPs are already managed */ | |
393 | if (!_add_opp_dev(dev, opp_table)) | |
394 | ret = -ENOMEM; | |
b83c1899 | 395 | goto put_opp_table; |
8cd2f6e8 VK |
396 | } |
397 | ||
eb7c8743 | 398 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, index); |
b83c1899 VK |
399 | if (!opp_table) |
400 | return -ENOMEM; | |
f47b72a1 VK |
401 | |
402 | /* We have opp-table node now, iterate over it and add OPPs */ | |
403 | for_each_available_child_of_node(opp_np, np) { | |
404 | count++; | |
405 | ||
8cd2f6e8 | 406 | ret = _opp_add_static_v2(opp_table, dev, np); |
f47b72a1 VK |
407 | if (ret) { |
408 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, | |
409 | ret); | |
b83c1899 | 410 | _dev_pm_opp_remove_table(opp_table, dev, false); |
7978db34 | 411 | of_node_put(np); |
b83c1899 | 412 | goto put_opp_table; |
f47b72a1 VK |
413 | } |
414 | } | |
415 | ||
416 | /* There should be one of more OPP defined */ | |
8cd2f6e8 VK |
417 | if (WARN_ON(!count)) { |
418 | ret = -ENOENT; | |
b83c1899 | 419 | goto put_opp_table; |
f47b72a1 VK |
420 | } |
421 | ||
3ba98324 VK |
422 | list_for_each_entry(opp, &opp_table->opp_list, node) |
423 | pstate_count += !!opp->pstate; | |
424 | ||
425 | /* Either all or none of the nodes shall have performance state set */ | |
426 | if (pstate_count && pstate_count != count) { | |
427 | dev_err(dev, "Not all nodes have performance state set (%d: %d)\n", | |
428 | count, pstate_count); | |
429 | ret = -ENOENT; | |
2fbb8670 | 430 | _dev_pm_opp_remove_table(opp_table, dev, false); |
3ba98324 VK |
431 | goto put_opp_table; |
432 | } | |
433 | ||
434 | if (pstate_count) | |
435 | opp_table->genpd_performance_state = true; | |
436 | ||
f47b72a1 | 437 | opp_table->np = opp_np; |
79ee2e8f VK |
438 | if (of_property_read_bool(opp_np, "opp-shared")) |
439 | opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; | |
440 | else | |
441 | opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; | |
f47b72a1 | 442 | |
b83c1899 VK |
443 | put_opp_table: |
444 | dev_pm_opp_put_opp_table(opp_table); | |
f47b72a1 VK |
445 | |
446 | return ret; | |
447 | } | |
448 | ||
449 | /* Initializes OPP tables based on old-deprecated bindings */ | |
450 | static int _of_add_opp_table_v1(struct device *dev) | |
451 | { | |
8cd2f6e8 | 452 | struct opp_table *opp_table; |
f47b72a1 VK |
453 | const struct property *prop; |
454 | const __be32 *val; | |
8cd2f6e8 | 455 | int nr, ret = 0; |
f47b72a1 VK |
456 | |
457 | prop = of_find_property(dev->of_node, "operating-points", NULL); | |
458 | if (!prop) | |
459 | return -ENODEV; | |
460 | if (!prop->value) | |
461 | return -ENODATA; | |
462 | ||
463 | /* | |
464 | * Each OPP is a set of tuples consisting of frequency and | |
465 | * voltage like <freq-kHz vol-uV>. | |
466 | */ | |
467 | nr = prop->length / sizeof(u32); | |
468 | if (nr % 2) { | |
469 | dev_err(dev, "%s: Invalid OPP table\n", __func__); | |
470 | return -EINVAL; | |
471 | } | |
472 | ||
b83c1899 VK |
473 | opp_table = dev_pm_opp_get_opp_table(dev); |
474 | if (!opp_table) | |
475 | return -ENOMEM; | |
8cd2f6e8 | 476 | |
f47b72a1 VK |
477 | val = prop->value; |
478 | while (nr) { | |
479 | unsigned long freq = be32_to_cpup(val++) * 1000; | |
480 | unsigned long volt = be32_to_cpup(val++); | |
481 | ||
8cd2f6e8 | 482 | ret = _opp_add_v1(opp_table, dev, freq, volt, false); |
04a86a84 VK |
483 | if (ret) { |
484 | dev_err(dev, "%s: Failed to add OPP %ld (%d)\n", | |
485 | __func__, freq, ret); | |
8cd2f6e8 VK |
486 | _dev_pm_opp_remove_table(opp_table, dev, false); |
487 | break; | |
04a86a84 | 488 | } |
f47b72a1 VK |
489 | nr -= 2; |
490 | } | |
491 | ||
b83c1899 | 492 | dev_pm_opp_put_opp_table(opp_table); |
8cd2f6e8 | 493 | return ret; |
f47b72a1 VK |
494 | } |
495 | ||
496 | /** | |
497 | * dev_pm_opp_of_add_table() - Initialize opp table from device tree | |
498 | * @dev: device pointer used to lookup OPP table. | |
499 | * | |
500 | * Register the initial OPP table with the OPP library for given device. | |
501 | * | |
f47b72a1 VK |
502 | * Return: |
503 | * 0 On success OR | |
504 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
505 | * -EEXIST Freq are same and volt are different OR | |
506 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
507 | * -ENOMEM Memory allocation failure | |
508 | * -ENODEV when 'operating-points' property is not found or is invalid data | |
509 | * in device node. | |
510 | * -ENODATA when empty 'operating-points' property is found | |
511 | * -EINVAL when invalid entries are found in opp-v2 table | |
512 | */ | |
513 | int dev_pm_opp_of_add_table(struct device *dev) | |
514 | { | |
515 | struct device_node *opp_np; | |
516 | int ret; | |
517 | ||
518 | /* | |
519 | * OPPs have two version of bindings now. The older one is deprecated, | |
520 | * try for the new binding first. | |
521 | */ | |
0764c604 | 522 | opp_np = dev_pm_opp_of_get_opp_desc_node(dev); |
f47b72a1 VK |
523 | if (!opp_np) { |
524 | /* | |
525 | * Try old-deprecated bindings for backward compatibility with | |
526 | * older dtbs. | |
527 | */ | |
528 | return _of_add_opp_table_v1(dev); | |
529 | } | |
530 | ||
eb7c8743 | 531 | ret = _of_add_opp_table_v2(dev, opp_np, 0); |
f47b72a1 VK |
532 | of_node_put(opp_np); |
533 | ||
534 | return ret; | |
535 | } | |
536 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table); | |
537 | ||
fa9b274f VK |
538 | /** |
539 | * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree | |
540 | * @dev: device pointer used to lookup OPP table. | |
541 | * @index: Index number. | |
542 | * | |
543 | * Register the initial OPP table with the OPP library for given device only | |
544 | * using the "operating-points-v2" property. | |
545 | * | |
546 | * Return: | |
547 | * 0 On success OR | |
548 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
549 | * -EEXIST Freq are same and volt are different OR | |
550 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
551 | * -ENOMEM Memory allocation failure | |
552 | * -ENODEV when 'operating-points' property is not found or is invalid data | |
553 | * in device node. | |
554 | * -ENODATA when empty 'operating-points' property is found | |
555 | * -EINVAL when invalid entries are found in opp-v2 table | |
556 | */ | |
557 | int dev_pm_opp_of_add_table_indexed(struct device *dev, int index) | |
558 | { | |
559 | struct device_node *opp_np; | |
8a352fd8 | 560 | int ret, count; |
fa9b274f | 561 | |
8a352fd8 | 562 | again: |
fa9b274f | 563 | opp_np = _opp_of_get_opp_desc_node(dev->of_node, index); |
8a352fd8 VK |
564 | if (!opp_np) { |
565 | /* | |
566 | * If only one phandle is present, then the same OPP table | |
567 | * applies for all index requests. | |
568 | */ | |
569 | count = of_count_phandle_with_args(dev->of_node, | |
570 | "operating-points-v2", NULL); | |
571 | if (count == 1 && index) { | |
572 | index = 0; | |
573 | goto again; | |
574 | } | |
575 | ||
fa9b274f | 576 | return -ENODEV; |
8a352fd8 | 577 | } |
fa9b274f | 578 | |
eb7c8743 | 579 | ret = _of_add_opp_table_v2(dev, opp_np, index); |
fa9b274f VK |
580 | of_node_put(opp_np); |
581 | ||
582 | return ret; | |
583 | } | |
584 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed); | |
585 | ||
f47b72a1 VK |
586 | /* CPU device specific helpers */ |
587 | ||
588 | /** | |
589 | * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask | |
590 | * @cpumask: cpumask for which OPP table needs to be removed | |
591 | * | |
592 | * This removes the OPP tables for CPUs present in the @cpumask. | |
593 | * This should be used only to remove static entries created from DT. | |
f47b72a1 VK |
594 | */ |
595 | void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask) | |
596 | { | |
404b1369 | 597 | _dev_pm_opp_cpumask_remove_table(cpumask, true, -1); |
f47b72a1 VK |
598 | } |
599 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table); | |
600 | ||
601 | /** | |
602 | * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask | |
603 | * @cpumask: cpumask for which OPP table needs to be added. | |
604 | * | |
605 | * This adds the OPP tables for CPUs present in the @cpumask. | |
f47b72a1 VK |
606 | */ |
607 | int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) | |
608 | { | |
609 | struct device *cpu_dev; | |
610 | int cpu, ret = 0; | |
611 | ||
612 | WARN_ON(cpumask_empty(cpumask)); | |
613 | ||
614 | for_each_cpu(cpu, cpumask) { | |
615 | cpu_dev = get_cpu_device(cpu); | |
616 | if (!cpu_dev) { | |
617 | pr_err("%s: failed to get cpu%d device\n", __func__, | |
618 | cpu); | |
619 | continue; | |
620 | } | |
621 | ||
622 | ret = dev_pm_opp_of_add_table(cpu_dev); | |
623 | if (ret) { | |
5b60697c VK |
624 | /* |
625 | * OPP may get registered dynamically, don't print error | |
626 | * message here. | |
627 | */ | |
628 | pr_debug("%s: couldn't find opp table for cpu:%d, %d\n", | |
629 | __func__, cpu, ret); | |
f47b72a1 VK |
630 | |
631 | /* Free all other OPPs */ | |
404b1369 | 632 | _dev_pm_opp_cpumask_remove_table(cpumask, true, cpu); |
f47b72a1 VK |
633 | break; |
634 | } | |
635 | } | |
636 | ||
637 | return ret; | |
638 | } | |
639 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table); | |
640 | ||
641 | /* | |
642 | * Works only for OPP v2 bindings. | |
643 | * | |
644 | * Returns -ENOENT if operating-points-v2 bindings aren't supported. | |
645 | */ | |
646 | /** | |
647 | * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with | |
648 | * @cpu_dev using operating-points-v2 | |
649 | * bindings. | |
650 | * | |
651 | * @cpu_dev: CPU device for which we do this operation | |
652 | * @cpumask: cpumask to update with information of sharing CPUs | |
653 | * | |
654 | * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. | |
655 | * | |
656 | * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev. | |
f47b72a1 VK |
657 | */ |
658 | int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, | |
659 | struct cpumask *cpumask) | |
660 | { | |
76279291 | 661 | struct device_node *np, *tmp_np, *cpu_np; |
f47b72a1 VK |
662 | int cpu, ret = 0; |
663 | ||
664 | /* Get OPP descriptor node */ | |
0764c604 | 665 | np = dev_pm_opp_of_get_opp_desc_node(cpu_dev); |
f47b72a1 | 666 | if (!np) { |
349aa92e | 667 | dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__); |
f47b72a1 VK |
668 | return -ENOENT; |
669 | } | |
670 | ||
671 | cpumask_set_cpu(cpu_dev->id, cpumask); | |
672 | ||
673 | /* OPPs are shared ? */ | |
674 | if (!of_property_read_bool(np, "opp-shared")) | |
675 | goto put_cpu_node; | |
676 | ||
677 | for_each_possible_cpu(cpu) { | |
678 | if (cpu == cpu_dev->id) | |
679 | continue; | |
680 | ||
9867999f | 681 | cpu_np = of_cpu_device_node_get(cpu); |
76279291 WR |
682 | if (!cpu_np) { |
683 | dev_err(cpu_dev, "%s: failed to get cpu%d node\n", | |
f47b72a1 | 684 | __func__, cpu); |
76279291 | 685 | ret = -ENOENT; |
f47b72a1 VK |
686 | goto put_cpu_node; |
687 | } | |
688 | ||
689 | /* Get OPP descriptor node */ | |
fa9b274f | 690 | tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0); |
9867999f | 691 | of_node_put(cpu_np); |
f47b72a1 | 692 | if (!tmp_np) { |
76279291 | 693 | pr_err("%pOF: Couldn't find opp node\n", cpu_np); |
f47b72a1 VK |
694 | ret = -ENOENT; |
695 | goto put_cpu_node; | |
696 | } | |
697 | ||
698 | /* CPUs are sharing opp node */ | |
699 | if (np == tmp_np) | |
700 | cpumask_set_cpu(cpu, cpumask); | |
701 | ||
702 | of_node_put(tmp_np); | |
703 | } | |
704 | ||
705 | put_cpu_node: | |
706 | of_node_put(np); | |
707 | return ret; | |
708 | } | |
709 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus); | |
a88bd2a5 VK |
710 | |
711 | /** | |
712 | * of_dev_pm_opp_find_required_opp() - Search for required OPP. | |
713 | * @dev: The device whose OPP node is referenced by the 'np' DT node. | |
714 | * @np: Node that contains the "required-opps" property. | |
715 | * | |
716 | * Returns the OPP of the device 'dev', whose phandle is present in the "np" | |
717 | * node. Although the "required-opps" property supports having multiple | |
718 | * phandles, this helper routine only parses the very first phandle in the list. | |
719 | * | |
720 | * Return: Matching opp, else returns ERR_PTR in case of error and should be | |
721 | * handled using IS_ERR. | |
722 | * | |
723 | * The callers are required to call dev_pm_opp_put() for the returned OPP after | |
724 | * use. | |
725 | */ | |
726 | struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev, | |
727 | struct device_node *np) | |
728 | { | |
729 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV); | |
730 | struct device_node *required_np; | |
731 | struct opp_table *opp_table; | |
732 | ||
733 | opp_table = _find_opp_table(dev); | |
734 | if (IS_ERR(opp_table)) | |
735 | return ERR_CAST(opp_table); | |
736 | ||
737 | required_np = of_parse_phandle(np, "required-opps", 0); | |
738 | if (unlikely(!required_np)) { | |
739 | dev_err(dev, "Unable to parse required-opps\n"); | |
740 | goto put_opp_table; | |
741 | } | |
742 | ||
743 | mutex_lock(&opp_table->lock); | |
744 | ||
745 | list_for_each_entry(temp_opp, &opp_table->opp_list, node) { | |
746 | if (temp_opp->available && temp_opp->np == required_np) { | |
747 | opp = temp_opp; | |
748 | ||
749 | /* Increment the reference count of OPP */ | |
750 | dev_pm_opp_get(opp); | |
751 | break; | |
752 | } | |
753 | } | |
754 | ||
755 | mutex_unlock(&opp_table->lock); | |
756 | ||
757 | of_node_put(required_np); | |
758 | put_opp_table: | |
759 | dev_pm_opp_put_opp_table(opp_table); | |
760 | ||
761 | return opp; | |
762 | } | |
763 | EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp); | |
e2f4b5f8 VK |
764 | |
765 | /** | |
766 | * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp | |
767 | * @opp: opp for which DT node has to be returned for | |
768 | * | |
769 | * Return: DT node corresponding to the opp, else 0 on success. | |
770 | * | |
771 | * The caller needs to put the node with of_node_put() after using it. | |
772 | */ | |
773 | struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp) | |
774 | { | |
775 | if (IS_ERR_OR_NULL(opp)) { | |
776 | pr_err("%s: Invalid parameters\n", __func__); | |
777 | return NULL; | |
778 | } | |
779 | ||
780 | return of_node_get(opp->np); | |
781 | } | |
782 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node); |