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 | ||
f06ed90e VK |
26 | /* |
27 | * Returns opp descriptor node for a device node, caller must | |
28 | * do of_node_put(). | |
29 | */ | |
30 | static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np, | |
31 | int index) | |
32 | { | |
33 | /* "operating-points-v2" can be an array for power domain providers */ | |
34 | return of_parse_phandle(np, "operating-points-v2", index); | |
35 | } | |
36 | ||
37 | /* Returns opp descriptor node for a device, caller must do of_node_put() */ | |
38 | struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) | |
39 | { | |
40 | return _opp_of_get_opp_desc_node(dev->of_node, 0); | |
41 | } | |
42 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node); | |
43 | ||
283d55e6 | 44 | struct opp_table *_managed_opp(struct device *dev, int index) |
f47b72a1 | 45 | { |
b83c1899 | 46 | struct opp_table *opp_table, *managed_table = NULL; |
283d55e6 | 47 | struct device_node *np; |
b83c1899 | 48 | |
283d55e6 VK |
49 | np = _opp_of_get_opp_desc_node(dev->of_node, index); |
50 | if (!np) | |
51 | return NULL; | |
f47b72a1 | 52 | |
052c6f19 | 53 | list_for_each_entry(opp_table, &opp_tables, node) { |
f47b72a1 VK |
54 | if (opp_table->np == np) { |
55 | /* | |
56 | * Multiple devices can point to the same OPP table and | |
57 | * so will have same node-pointer, np. | |
58 | * | |
59 | * But the OPPs will be considered as shared only if the | |
60 | * OPP table contains a "opp-shared" property. | |
61 | */ | |
b83c1899 VK |
62 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { |
63 | _get_opp_table_kref(opp_table); | |
64 | managed_table = opp_table; | |
65 | } | |
79ee2e8f | 66 | |
b83c1899 | 67 | break; |
f47b72a1 VK |
68 | } |
69 | } | |
70 | ||
283d55e6 | 71 | of_node_put(np); |
b83c1899 VK |
72 | |
73 | return managed_table; | |
f47b72a1 VK |
74 | } |
75 | ||
5d6d106f VK |
76 | /* The caller must call dev_pm_opp_put() after the OPP is used */ |
77 | static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table, | |
78 | struct device_node *opp_np) | |
79 | { | |
80 | struct dev_pm_opp *opp; | |
81 | ||
82 | lockdep_assert_held(&opp_table_lock); | |
83 | ||
84 | mutex_lock(&opp_table->lock); | |
85 | ||
86 | list_for_each_entry(opp, &opp_table->opp_list, node) { | |
87 | if (opp->np == opp_np) { | |
88 | dev_pm_opp_get(opp); | |
89 | mutex_unlock(&opp_table->lock); | |
90 | return opp; | |
91 | } | |
92 | } | |
93 | ||
94 | mutex_unlock(&opp_table->lock); | |
95 | ||
96 | return NULL; | |
97 | } | |
98 | ||
99 | static struct device_node *of_parse_required_opp(struct device_node *np, | |
100 | int index) | |
101 | { | |
102 | struct device_node *required_np; | |
103 | ||
104 | required_np = of_parse_phandle(np, "required-opps", index); | |
105 | if (unlikely(!required_np)) { | |
106 | pr_err("%s: Unable to parse required-opps: %pOF, index: %d\n", | |
107 | __func__, np, index); | |
108 | } | |
109 | ||
110 | return required_np; | |
111 | } | |
112 | ||
113 | /* The caller must call dev_pm_opp_put_opp_table() after the table is used */ | |
114 | static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np) | |
115 | { | |
116 | struct opp_table *opp_table; | |
699e21e4 | 117 | struct device_node *opp_table_np; |
5d6d106f VK |
118 | |
119 | lockdep_assert_held(&opp_table_lock); | |
120 | ||
699e21e4 VK |
121 | opp_table_np = of_get_parent(opp_np); |
122 | if (!opp_table_np) | |
123 | goto err; | |
124 | ||
125 | /* It is safe to put the node now as all we need now is its address */ | |
126 | of_node_put(opp_table_np); | |
127 | ||
5d6d106f | 128 | list_for_each_entry(opp_table, &opp_tables, node) { |
699e21e4 | 129 | if (opp_table_np == opp_table->np) { |
5d6d106f VK |
130 | _get_opp_table_kref(opp_table); |
131 | return opp_table; | |
132 | } | |
133 | } | |
134 | ||
699e21e4 | 135 | err: |
5d6d106f VK |
136 | return ERR_PTR(-ENODEV); |
137 | } | |
138 | ||
139 | /* Free resources previously acquired by _opp_table_alloc_required_tables() */ | |
140 | static void _opp_table_free_required_tables(struct opp_table *opp_table) | |
141 | { | |
142 | struct opp_table **required_opp_tables = opp_table->required_opp_tables; | |
4f018bc0 | 143 | struct device **genpd_virt_devs = opp_table->genpd_virt_devs; |
5d6d106f VK |
144 | int i; |
145 | ||
146 | if (!required_opp_tables) | |
147 | return; | |
148 | ||
149 | for (i = 0; i < opp_table->required_opp_count; i++) { | |
150 | if (IS_ERR_OR_NULL(required_opp_tables[i])) | |
151 | break; | |
152 | ||
153 | dev_pm_opp_put_opp_table(required_opp_tables[i]); | |
154 | } | |
155 | ||
156 | kfree(required_opp_tables); | |
4f018bc0 | 157 | kfree(genpd_virt_devs); |
5d6d106f VK |
158 | |
159 | opp_table->required_opp_count = 0; | |
4f018bc0 | 160 | opp_table->genpd_virt_devs = NULL; |
5d6d106f VK |
161 | opp_table->required_opp_tables = NULL; |
162 | } | |
163 | ||
164 | /* | |
165 | * Populate all devices and opp tables which are part of "required-opps" list. | |
166 | * Checking only the first OPP node should be enough. | |
167 | */ | |
168 | static void _opp_table_alloc_required_tables(struct opp_table *opp_table, | |
169 | struct device *dev, | |
170 | struct device_node *opp_np) | |
171 | { | |
172 | struct opp_table **required_opp_tables; | |
4f018bc0 | 173 | struct device **genpd_virt_devs = NULL; |
5d6d106f VK |
174 | struct device_node *required_np, *np; |
175 | int count, i; | |
176 | ||
177 | /* Traversing the first OPP node is all we need */ | |
178 | np = of_get_next_available_child(opp_np, NULL); | |
179 | if (!np) { | |
180 | dev_err(dev, "Empty OPP table\n"); | |
181 | return; | |
182 | } | |
183 | ||
184 | count = of_count_phandle_with_args(np, "required-opps", NULL); | |
185 | if (!count) | |
186 | goto put_np; | |
187 | ||
4f018bc0 VK |
188 | if (count > 1) { |
189 | genpd_virt_devs = kcalloc(count, sizeof(*genpd_virt_devs), | |
190 | GFP_KERNEL); | |
191 | if (!genpd_virt_devs) | |
192 | goto put_np; | |
193 | } | |
194 | ||
5d6d106f VK |
195 | required_opp_tables = kcalloc(count, sizeof(*required_opp_tables), |
196 | GFP_KERNEL); | |
4f018bc0 VK |
197 | if (!required_opp_tables) { |
198 | kfree(genpd_virt_devs); | |
5d6d106f | 199 | goto put_np; |
4f018bc0 | 200 | } |
5d6d106f | 201 | |
4f018bc0 | 202 | opp_table->genpd_virt_devs = genpd_virt_devs; |
5d6d106f VK |
203 | opp_table->required_opp_tables = required_opp_tables; |
204 | opp_table->required_opp_count = count; | |
205 | ||
206 | for (i = 0; i < count; i++) { | |
207 | required_np = of_parse_required_opp(np, i); | |
208 | if (!required_np) | |
209 | goto free_required_tables; | |
210 | ||
211 | required_opp_tables[i] = _find_table_of_opp_np(required_np); | |
212 | of_node_put(required_np); | |
213 | ||
214 | if (IS_ERR(required_opp_tables[i])) | |
215 | goto free_required_tables; | |
216 | ||
217 | /* | |
218 | * We only support genpd's OPPs in the "required-opps" for now, | |
219 | * as we don't know how much about other cases. Error out if the | |
220 | * required OPP doesn't belong to a genpd. | |
221 | */ | |
222 | if (!required_opp_tables[i]->is_genpd) { | |
223 | dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n", | |
224 | required_np); | |
225 | goto free_required_tables; | |
226 | } | |
227 | } | |
228 | ||
229 | goto put_np; | |
230 | ||
231 | free_required_tables: | |
232 | _opp_table_free_required_tables(opp_table); | |
233 | put_np: | |
234 | of_node_put(np); | |
235 | } | |
236 | ||
eb7c8743 VK |
237 | void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, |
238 | int index) | |
f47b72a1 | 239 | { |
f06ed90e VK |
240 | struct device_node *np, *opp_np; |
241 | u32 val; | |
f47b72a1 VK |
242 | |
243 | /* | |
244 | * Only required for backward compatibility with v1 bindings, but isn't | |
245 | * harmful for other cases. And so we do it unconditionally. | |
246 | */ | |
247 | np = of_node_get(dev->of_node); | |
f06ed90e VK |
248 | if (!np) |
249 | return; | |
250 | ||
251 | if (!of_property_read_u32(np, "clock-latency", &val)) | |
252 | opp_table->clock_latency_ns_max = val; | |
253 | of_property_read_u32(np, "voltage-tolerance", | |
254 | &opp_table->voltage_tolerance_v1); | |
255 | ||
61d8e7c7 VK |
256 | if (of_find_property(np, "#power-domain-cells", NULL)) |
257 | opp_table->is_genpd = true; | |
258 | ||
f06ed90e VK |
259 | /* Get OPP table node */ |
260 | opp_np = _opp_of_get_opp_desc_node(np, index); | |
261 | of_node_put(np); | |
262 | ||
263 | if (!opp_np) | |
264 | return; | |
265 | ||
266 | if (of_property_read_bool(opp_np, "opp-shared")) | |
267 | opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; | |
268 | else | |
269 | opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; | |
270 | ||
271 | opp_table->np = opp_np; | |
272 | ||
5d6d106f | 273 | _opp_table_alloc_required_tables(opp_table, dev, opp_np); |
f06ed90e | 274 | of_node_put(opp_np); |
f47b72a1 VK |
275 | } |
276 | ||
5d6d106f VK |
277 | void _of_clear_opp_table(struct opp_table *opp_table) |
278 | { | |
279 | _opp_table_free_required_tables(opp_table); | |
280 | } | |
281 | ||
da544b61 VK |
282 | /* |
283 | * Release all resources previously acquired with a call to | |
284 | * _of_opp_alloc_required_opps(). | |
285 | */ | |
286 | void _of_opp_free_required_opps(struct opp_table *opp_table, | |
287 | struct dev_pm_opp *opp) | |
288 | { | |
289 | struct dev_pm_opp **required_opps = opp->required_opps; | |
290 | int i; | |
291 | ||
292 | if (!required_opps) | |
293 | return; | |
294 | ||
295 | for (i = 0; i < opp_table->required_opp_count; i++) { | |
296 | if (!required_opps[i]) | |
297 | break; | |
298 | ||
299 | /* Put the reference back */ | |
300 | dev_pm_opp_put(required_opps[i]); | |
301 | } | |
302 | ||
303 | kfree(required_opps); | |
304 | opp->required_opps = NULL; | |
305 | } | |
306 | ||
307 | /* Populate all required OPPs which are part of "required-opps" list */ | |
308 | static int _of_opp_alloc_required_opps(struct opp_table *opp_table, | |
309 | struct dev_pm_opp *opp) | |
310 | { | |
311 | struct dev_pm_opp **required_opps; | |
312 | struct opp_table *required_table; | |
313 | struct device_node *np; | |
314 | int i, ret, count = opp_table->required_opp_count; | |
315 | ||
316 | if (!count) | |
317 | return 0; | |
318 | ||
319 | required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL); | |
320 | if (!required_opps) | |
321 | return -ENOMEM; | |
322 | ||
323 | opp->required_opps = required_opps; | |
324 | ||
325 | for (i = 0; i < count; i++) { | |
326 | required_table = opp_table->required_opp_tables[i]; | |
327 | ||
328 | np = of_parse_required_opp(opp->np, i); | |
329 | if (unlikely(!np)) { | |
330 | ret = -ENODEV; | |
331 | goto free_required_opps; | |
332 | } | |
333 | ||
334 | required_opps[i] = _find_opp_of_np(required_table, np); | |
335 | of_node_put(np); | |
336 | ||
337 | if (!required_opps[i]) { | |
338 | pr_err("%s: Unable to find required OPP node: %pOF (%d)\n", | |
339 | __func__, opp->np, i); | |
340 | ret = -ENODEV; | |
341 | goto free_required_opps; | |
342 | } | |
343 | } | |
344 | ||
345 | return 0; | |
346 | ||
347 | free_required_opps: | |
348 | _of_opp_free_required_opps(opp_table, opp); | |
349 | ||
350 | return ret; | |
351 | } | |
352 | ||
f47b72a1 VK |
353 | static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table, |
354 | struct device_node *np) | |
355 | { | |
356 | unsigned int count = opp_table->supported_hw_count; | |
357 | u32 version; | |
358 | int ret; | |
359 | ||
a4ee4545 DG |
360 | if (!opp_table->supported_hw) { |
361 | /* | |
362 | * In the case that no supported_hw has been set by the | |
363 | * platform but there is an opp-supported-hw value set for | |
364 | * an OPP then the OPP should not be enabled as there is | |
365 | * no way to see if the hardware supports it. | |
366 | */ | |
367 | if (of_find_property(np, "opp-supported-hw", NULL)) | |
368 | return false; | |
369 | else | |
370 | return true; | |
371 | } | |
f47b72a1 VK |
372 | |
373 | while (count--) { | |
374 | ret = of_property_read_u32_index(np, "opp-supported-hw", count, | |
375 | &version); | |
376 | if (ret) { | |
377 | dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n", | |
378 | __func__, count, ret); | |
379 | return false; | |
380 | } | |
381 | ||
382 | /* Both of these are bitwise masks of the versions */ | |
383 | if (!(version & opp_table->supported_hw[count])) | |
384 | return false; | |
385 | } | |
386 | ||
387 | return true; | |
388 | } | |
389 | ||
f47b72a1 VK |
390 | static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, |
391 | struct opp_table *opp_table) | |
392 | { | |
dfbe4678 | 393 | u32 *microvolt, *microamp = NULL; |
46f48aca | 394 | int supplies = opp_table->regulator_count, vcount, icount, ret, i, j; |
f47b72a1 VK |
395 | struct property *prop = NULL; |
396 | char name[NAME_MAX]; | |
397 | ||
398 | /* Search for "opp-microvolt-<name>" */ | |
399 | if (opp_table->prop_name) { | |
400 | snprintf(name, sizeof(name), "opp-microvolt-%s", | |
401 | opp_table->prop_name); | |
402 | prop = of_find_property(opp->np, name, NULL); | |
403 | } | |
404 | ||
405 | if (!prop) { | |
406 | /* Search for "opp-microvolt" */ | |
407 | sprintf(name, "opp-microvolt"); | |
408 | prop = of_find_property(opp->np, name, NULL); | |
409 | ||
410 | /* Missing property isn't a problem, but an invalid entry is */ | |
688a48b0 | 411 | if (!prop) { |
46f48aca VK |
412 | if (unlikely(supplies == -1)) { |
413 | /* Initialize regulator_count */ | |
414 | opp_table->regulator_count = 0; | |
415 | return 0; | |
416 | } | |
417 | ||
418 | if (!supplies) | |
688a48b0 VK |
419 | return 0; |
420 | ||
421 | dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n", | |
422 | __func__); | |
423 | return -EINVAL; | |
424 | } | |
f47b72a1 VK |
425 | } |
426 | ||
46f48aca VK |
427 | if (unlikely(supplies == -1)) { |
428 | /* Initialize regulator_count */ | |
429 | supplies = opp_table->regulator_count = 1; | |
430 | } else if (unlikely(!supplies)) { | |
431 | dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__); | |
432 | return -EINVAL; | |
433 | } | |
434 | ||
dfbe4678 VK |
435 | vcount = of_property_count_u32_elems(opp->np, name); |
436 | if (vcount < 0) { | |
f47b72a1 | 437 | dev_err(dev, "%s: Invalid %s property (%d)\n", |
dfbe4678 VK |
438 | __func__, name, vcount); |
439 | return vcount; | |
f47b72a1 VK |
440 | } |
441 | ||
dfbe4678 VK |
442 | /* There can be one or three elements per supply */ |
443 | if (vcount != supplies && vcount != supplies * 3) { | |
444 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", | |
445 | __func__, name, vcount, supplies); | |
f47b72a1 VK |
446 | return -EINVAL; |
447 | } | |
448 | ||
dfbe4678 VK |
449 | microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL); |
450 | if (!microvolt) | |
451 | return -ENOMEM; | |
452 | ||
453 | ret = of_property_read_u32_array(opp->np, name, microvolt, vcount); | |
f47b72a1 VK |
454 | if (ret) { |
455 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret); | |
dfbe4678 VK |
456 | ret = -EINVAL; |
457 | goto free_microvolt; | |
f47b72a1 VK |
458 | } |
459 | ||
460 | /* Search for "opp-microamp-<name>" */ | |
461 | prop = NULL; | |
462 | if (opp_table->prop_name) { | |
463 | snprintf(name, sizeof(name), "opp-microamp-%s", | |
464 | opp_table->prop_name); | |
465 | prop = of_find_property(opp->np, name, NULL); | |
466 | } | |
467 | ||
468 | if (!prop) { | |
469 | /* Search for "opp-microamp" */ | |
470 | sprintf(name, "opp-microamp"); | |
471 | prop = of_find_property(opp->np, name, NULL); | |
472 | } | |
473 | ||
dfbe4678 VK |
474 | if (prop) { |
475 | icount = of_property_count_u32_elems(opp->np, name); | |
476 | if (icount < 0) { | |
477 | dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, | |
478 | name, icount); | |
479 | ret = icount; | |
480 | goto free_microvolt; | |
481 | } | |
f47b72a1 | 482 | |
dfbe4678 VK |
483 | if (icount != supplies) { |
484 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", | |
485 | __func__, name, icount, supplies); | |
486 | ret = -EINVAL; | |
487 | goto free_microvolt; | |
488 | } | |
489 | ||
490 | microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL); | |
491 | if (!microamp) { | |
492 | ret = -EINVAL; | |
493 | goto free_microvolt; | |
494 | } | |
495 | ||
496 | ret = of_property_read_u32_array(opp->np, name, microamp, | |
497 | icount); | |
498 | if (ret) { | |
499 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, | |
500 | name, ret); | |
501 | ret = -EINVAL; | |
502 | goto free_microamp; | |
503 | } | |
504 | } | |
505 | ||
506 | for (i = 0, j = 0; i < supplies; i++) { | |
507 | opp->supplies[i].u_volt = microvolt[j++]; | |
508 | ||
509 | if (vcount == supplies) { | |
510 | opp->supplies[i].u_volt_min = opp->supplies[i].u_volt; | |
511 | opp->supplies[i].u_volt_max = opp->supplies[i].u_volt; | |
512 | } else { | |
513 | opp->supplies[i].u_volt_min = microvolt[j++]; | |
514 | opp->supplies[i].u_volt_max = microvolt[j++]; | |
515 | } | |
516 | ||
517 | if (microamp) | |
518 | opp->supplies[i].u_amp = microamp[i]; | |
519 | } | |
520 | ||
521 | free_microamp: | |
522 | kfree(microamp); | |
523 | free_microvolt: | |
524 | kfree(microvolt); | |
525 | ||
526 | return ret; | |
f47b72a1 VK |
527 | } |
528 | ||
529 | /** | |
530 | * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT | |
531 | * entries | |
532 | * @dev: device pointer used to lookup OPP table. | |
533 | * | |
534 | * Free OPPs created using static entries present in DT. | |
f47b72a1 VK |
535 | */ |
536 | void dev_pm_opp_of_remove_table(struct device *dev) | |
537 | { | |
2a4eb735 | 538 | _dev_pm_opp_find_and_remove_table(dev); |
f47b72a1 VK |
539 | } |
540 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table); | |
541 | ||
f47b72a1 VK |
542 | /** |
543 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) | |
8cd2f6e8 | 544 | * @opp_table: OPP table |
f47b72a1 VK |
545 | * @dev: device for which we do this operation |
546 | * @np: device node | |
547 | * | |
548 | * This function adds an opp definition to the opp table and returns status. The | |
549 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be | |
550 | * removed by dev_pm_opp_remove. | |
551 | * | |
f47b72a1 | 552 | * Return: |
deac8703 DG |
553 | * Valid OPP pointer: |
554 | * On success | |
555 | * NULL: | |
f47b72a1 | 556 | * Duplicate OPPs (both freq and volt are same) and opp->available |
deac8703 DG |
557 | * OR if the OPP is not supported by hardware. |
558 | * ERR_PTR(-EEXIST): | |
559 | * Freq are same and volt are different OR | |
f47b72a1 | 560 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
deac8703 DG |
561 | * ERR_PTR(-ENOMEM): |
562 | * Memory allocation failure | |
563 | * ERR_PTR(-EINVAL): | |
564 | * Failed parsing the OPP node | |
f47b72a1 | 565 | */ |
deac8703 DG |
566 | static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table, |
567 | struct device *dev, struct device_node *np) | |
f47b72a1 | 568 | { |
f47b72a1 | 569 | struct dev_pm_opp *new_opp; |
6a89e012 | 570 | u64 rate = 0; |
f47b72a1 VK |
571 | u32 val; |
572 | int ret; | |
a1e8c136 | 573 | bool rate_not_available = false; |
f47b72a1 | 574 | |
8cd2f6e8 VK |
575 | new_opp = _opp_allocate(opp_table); |
576 | if (!new_opp) | |
deac8703 | 577 | return ERR_PTR(-ENOMEM); |
f47b72a1 VK |
578 | |
579 | ret = of_property_read_u64(np, "opp-hz", &rate); | |
580 | if (ret < 0) { | |
a1e8c136 | 581 | /* "opp-hz" is optional for devices like power domains. */ |
61d8e7c7 | 582 | if (!opp_table->is_genpd) { |
a1e8c136 VK |
583 | dev_err(dev, "%s: opp-hz not found\n", __func__); |
584 | goto free_opp; | |
585 | } | |
586 | ||
587 | rate_not_available = true; | |
588 | } else { | |
589 | /* | |
590 | * Rate is defined as an unsigned long in clk API, and so | |
591 | * casting explicitly to its type. Must be fixed once rate is 64 | |
592 | * bit guaranteed in clk API. | |
593 | */ | |
594 | new_opp->rate = (unsigned long)rate; | |
f47b72a1 VK |
595 | } |
596 | ||
597 | /* Check if the OPP supports hardware's hierarchy of versions or not */ | |
598 | if (!_opp_is_supported(dev, opp_table, np)) { | |
599 | dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate); | |
600 | goto free_opp; | |
601 | } | |
602 | ||
f47b72a1 VK |
603 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); |
604 | ||
605 | new_opp->np = np; | |
606 | new_opp->dynamic = false; | |
607 | new_opp->available = true; | |
608 | ||
da544b61 VK |
609 | ret = _of_opp_alloc_required_opps(opp_table, new_opp); |
610 | if (ret) | |
611 | goto free_opp; | |
612 | ||
f47b72a1 VK |
613 | if (!of_property_read_u32(np, "clock-latency-ns", &val)) |
614 | new_opp->clock_latency_ns = val; | |
615 | ||
616 | ret = opp_parse_supplies(new_opp, dev, opp_table); | |
617 | if (ret) | |
da544b61 | 618 | goto free_required_opps; |
f47b72a1 | 619 | |
ca1b5d77 VK |
620 | if (opp_table->is_genpd) |
621 | new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp); | |
f47b72a1 | 622 | |
a1e8c136 | 623 | ret = _opp_add(dev, new_opp, opp_table, rate_not_available); |
7f8538eb VK |
624 | if (ret) { |
625 | /* Don't return error for duplicate OPPs */ | |
626 | if (ret == -EBUSY) | |
627 | ret = 0; | |
da544b61 | 628 | goto free_required_opps; |
7f8538eb | 629 | } |
f47b72a1 VK |
630 | |
631 | /* OPP to select on device suspend */ | |
632 | if (of_property_read_bool(np, "opp-suspend")) { | |
633 | if (opp_table->suspend_opp) { | |
634 | dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n", | |
635 | __func__, opp_table->suspend_opp->rate, | |
636 | new_opp->rate); | |
637 | } else { | |
638 | new_opp->suspend = true; | |
639 | opp_table->suspend_opp = new_opp; | |
640 | } | |
641 | } | |
642 | ||
643 | if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max) | |
644 | opp_table->clock_latency_ns_max = new_opp->clock_latency_ns; | |
645 | ||
f47b72a1 | 646 | pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", |
0f0fe7e0 | 647 | __func__, new_opp->turbo, new_opp->rate, |
dfbe4678 VK |
648 | new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min, |
649 | new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns); | |
f47b72a1 VK |
650 | |
651 | /* | |
652 | * Notify the changes in the availability of the operable | |
653 | * frequency/voltage list. | |
654 | */ | |
052c6f19 | 655 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
deac8703 | 656 | return new_opp; |
f47b72a1 | 657 | |
da544b61 VK |
658 | free_required_opps: |
659 | _of_opp_free_required_opps(opp_table, new_opp); | |
f47b72a1 | 660 | free_opp: |
8cd2f6e8 VK |
661 | _opp_free(new_opp); |
662 | ||
deac8703 | 663 | return ERR_PTR(ret); |
f47b72a1 VK |
664 | } |
665 | ||
666 | /* Initializes OPP tables based on new bindings */ | |
5ed4cecd | 667 | static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table) |
f47b72a1 VK |
668 | { |
669 | struct device_node *np; | |
283d55e6 | 670 | int ret, count = 0, pstate_count = 0; |
3ba98324 | 671 | struct dev_pm_opp *opp; |
f47b72a1 | 672 | |
283d55e6 VK |
673 | /* OPP table is already initialized for the device */ |
674 | if (opp_table->parsed_static_opps) { | |
675 | kref_get(&opp_table->list_kref); | |
676 | return 0; | |
677 | } | |
678 | ||
d0e8ae6c VK |
679 | kref_init(&opp_table->list_kref); |
680 | ||
f47b72a1 | 681 | /* We have opp-table node now, iterate over it and add OPPs */ |
5ed4cecd | 682 | for_each_available_child_of_node(opp_table->np, np) { |
deac8703 DG |
683 | opp = _opp_add_static_v2(opp_table, dev, np); |
684 | if (IS_ERR(opp)) { | |
685 | ret = PTR_ERR(opp); | |
f47b72a1 VK |
686 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, |
687 | ret); | |
7978db34 | 688 | of_node_put(np); |
cdd6ed90 | 689 | goto put_list_kref; |
deac8703 DG |
690 | } else if (opp) { |
691 | count++; | |
f47b72a1 VK |
692 | } |
693 | } | |
694 | ||
695 | /* There should be one of more OPP defined */ | |
8cd2f6e8 VK |
696 | if (WARN_ON(!count)) { |
697 | ret = -ENOENT; | |
cdd6ed90 | 698 | goto put_list_kref; |
f47b72a1 VK |
699 | } |
700 | ||
3ba98324 VK |
701 | list_for_each_entry(opp, &opp_table->opp_list, node) |
702 | pstate_count += !!opp->pstate; | |
703 | ||
704 | /* Either all or none of the nodes shall have performance state set */ | |
705 | if (pstate_count && pstate_count != count) { | |
706 | dev_err(dev, "Not all nodes have performance state set (%d: %d)\n", | |
707 | count, pstate_count); | |
708 | ret = -ENOENT; | |
cdd6ed90 | 709 | goto put_list_kref; |
3ba98324 VK |
710 | } |
711 | ||
712 | if (pstate_count) | |
713 | opp_table->genpd_performance_state = true; | |
714 | ||
f06ed90e | 715 | opp_table->parsed_static_opps = true; |
f47b72a1 | 716 | |
cdd6ed90 VK |
717 | return 0; |
718 | ||
719 | put_list_kref: | |
720 | _put_opp_list_kref(opp_table); | |
f47b72a1 VK |
721 | |
722 | return ret; | |
723 | } | |
724 | ||
725 | /* Initializes OPP tables based on old-deprecated bindings */ | |
5ed4cecd | 726 | static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table) |
f47b72a1 VK |
727 | { |
728 | const struct property *prop; | |
729 | const __be32 *val; | |
8cd2f6e8 | 730 | int nr, ret = 0; |
f47b72a1 VK |
731 | |
732 | prop = of_find_property(dev->of_node, "operating-points", NULL); | |
733 | if (!prop) | |
734 | return -ENODEV; | |
735 | if (!prop->value) | |
736 | return -ENODATA; | |
737 | ||
738 | /* | |
739 | * Each OPP is a set of tuples consisting of frequency and | |
740 | * voltage like <freq-kHz vol-uV>. | |
741 | */ | |
742 | nr = prop->length / sizeof(u32); | |
743 | if (nr % 2) { | |
744 | dev_err(dev, "%s: Invalid OPP table\n", __func__); | |
745 | return -EINVAL; | |
746 | } | |
747 | ||
d0e8ae6c VK |
748 | kref_init(&opp_table->list_kref); |
749 | ||
f47b72a1 VK |
750 | val = prop->value; |
751 | while (nr) { | |
752 | unsigned long freq = be32_to_cpup(val++) * 1000; | |
753 | unsigned long volt = be32_to_cpup(val++); | |
754 | ||
8cd2f6e8 | 755 | ret = _opp_add_v1(opp_table, dev, freq, volt, false); |
04a86a84 VK |
756 | if (ret) { |
757 | dev_err(dev, "%s: Failed to add OPP %ld (%d)\n", | |
758 | __func__, freq, ret); | |
cdd6ed90 | 759 | _put_opp_list_kref(opp_table); |
cdd6ed90 | 760 | return ret; |
04a86a84 | 761 | } |
f47b72a1 VK |
762 | nr -= 2; |
763 | } | |
764 | ||
8cd2f6e8 | 765 | return ret; |
f47b72a1 VK |
766 | } |
767 | ||
768 | /** | |
769 | * dev_pm_opp_of_add_table() - Initialize opp table from device tree | |
770 | * @dev: device pointer used to lookup OPP table. | |
771 | * | |
772 | * Register the initial OPP table with the OPP library for given device. | |
773 | * | |
f47b72a1 VK |
774 | * Return: |
775 | * 0 On success OR | |
776 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
777 | * -EEXIST Freq are same and volt are different OR | |
778 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
779 | * -ENOMEM Memory allocation failure | |
780 | * -ENODEV when 'operating-points' property is not found or is invalid data | |
781 | * in device node. | |
782 | * -ENODATA when empty 'operating-points' property is found | |
783 | * -EINVAL when invalid entries are found in opp-v2 table | |
784 | */ | |
785 | int dev_pm_opp_of_add_table(struct device *dev) | |
786 | { | |
5ed4cecd | 787 | struct opp_table *opp_table; |
f47b72a1 VK |
788 | int ret; |
789 | ||
5ed4cecd VK |
790 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0); |
791 | if (!opp_table) | |
792 | return -ENOMEM; | |
793 | ||
f47b72a1 | 794 | /* |
5ed4cecd VK |
795 | * OPPs have two version of bindings now. Also try the old (v1) |
796 | * bindings for backward compatibility with older dtbs. | |
f47b72a1 | 797 | */ |
5ed4cecd VK |
798 | if (opp_table->np) |
799 | ret = _of_add_opp_table_v2(dev, opp_table); | |
800 | else | |
801 | ret = _of_add_opp_table_v1(dev, opp_table); | |
f47b72a1 | 802 | |
5ed4cecd VK |
803 | if (ret) |
804 | dev_pm_opp_put_opp_table(opp_table); | |
f47b72a1 VK |
805 | |
806 | return ret; | |
807 | } | |
808 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table); | |
809 | ||
fa9b274f VK |
810 | /** |
811 | * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree | |
812 | * @dev: device pointer used to lookup OPP table. | |
813 | * @index: Index number. | |
814 | * | |
815 | * Register the initial OPP table with the OPP library for given device only | |
816 | * using the "operating-points-v2" property. | |
817 | * | |
818 | * Return: | |
819 | * 0 On success OR | |
820 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
821 | * -EEXIST Freq are same and volt are different OR | |
822 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
823 | * -ENOMEM Memory allocation failure | |
824 | * -ENODEV when 'operating-points' property is not found or is invalid data | |
825 | * in device node. | |
826 | * -ENODATA when empty 'operating-points' property is found | |
827 | * -EINVAL when invalid entries are found in opp-v2 table | |
828 | */ | |
829 | int dev_pm_opp_of_add_table_indexed(struct device *dev, int index) | |
830 | { | |
5ed4cecd | 831 | struct opp_table *opp_table; |
8a352fd8 | 832 | int ret, count; |
fa9b274f | 833 | |
5ed4cecd | 834 | if (index) { |
8a352fd8 VK |
835 | /* |
836 | * If only one phandle is present, then the same OPP table | |
837 | * applies for all index requests. | |
838 | */ | |
839 | count = of_count_phandle_with_args(dev->of_node, | |
840 | "operating-points-v2", NULL); | |
3e27c79c VK |
841 | if (count == 1) |
842 | index = 0; | |
8a352fd8 | 843 | } |
fa9b274f | 844 | |
5ed4cecd VK |
845 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, index); |
846 | if (!opp_table) | |
847 | return -ENOMEM; | |
848 | ||
849 | ret = _of_add_opp_table_v2(dev, opp_table); | |
850 | if (ret) | |
851 | dev_pm_opp_put_opp_table(opp_table); | |
fa9b274f VK |
852 | |
853 | return ret; | |
854 | } | |
855 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed); | |
856 | ||
f47b72a1 VK |
857 | /* CPU device specific helpers */ |
858 | ||
859 | /** | |
860 | * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask | |
861 | * @cpumask: cpumask for which OPP table needs to be removed | |
862 | * | |
863 | * This removes the OPP tables for CPUs present in the @cpumask. | |
864 | * This should be used only to remove static entries created from DT. | |
f47b72a1 VK |
865 | */ |
866 | void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask) | |
867 | { | |
2a4eb735 | 868 | _dev_pm_opp_cpumask_remove_table(cpumask, -1); |
f47b72a1 VK |
869 | } |
870 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table); | |
871 | ||
872 | /** | |
873 | * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask | |
874 | * @cpumask: cpumask for which OPP table needs to be added. | |
875 | * | |
876 | * This adds the OPP tables for CPUs present in the @cpumask. | |
f47b72a1 VK |
877 | */ |
878 | int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) | |
879 | { | |
880 | struct device *cpu_dev; | |
50b6b87c | 881 | int cpu, ret; |
f47b72a1 | 882 | |
50b6b87c VK |
883 | if (WARN_ON(cpumask_empty(cpumask))) |
884 | return -ENODEV; | |
f47b72a1 VK |
885 | |
886 | for_each_cpu(cpu, cpumask) { | |
887 | cpu_dev = get_cpu_device(cpu); | |
888 | if (!cpu_dev) { | |
889 | pr_err("%s: failed to get cpu%d device\n", __func__, | |
890 | cpu); | |
50b6b87c VK |
891 | ret = -ENODEV; |
892 | goto remove_table; | |
f47b72a1 VK |
893 | } |
894 | ||
895 | ret = dev_pm_opp_of_add_table(cpu_dev); | |
896 | if (ret) { | |
5b60697c VK |
897 | /* |
898 | * OPP may get registered dynamically, don't print error | |
899 | * message here. | |
900 | */ | |
901 | pr_debug("%s: couldn't find opp table for cpu:%d, %d\n", | |
902 | __func__, cpu, ret); | |
f47b72a1 | 903 | |
50b6b87c | 904 | goto remove_table; |
f47b72a1 VK |
905 | } |
906 | } | |
907 | ||
50b6b87c VK |
908 | return 0; |
909 | ||
910 | remove_table: | |
911 | /* Free all other OPPs */ | |
912 | _dev_pm_opp_cpumask_remove_table(cpumask, cpu); | |
913 | ||
f47b72a1 VK |
914 | return ret; |
915 | } | |
916 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table); | |
917 | ||
918 | /* | |
919 | * Works only for OPP v2 bindings. | |
920 | * | |
921 | * Returns -ENOENT if operating-points-v2 bindings aren't supported. | |
922 | */ | |
923 | /** | |
924 | * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with | |
925 | * @cpu_dev using operating-points-v2 | |
926 | * bindings. | |
927 | * | |
928 | * @cpu_dev: CPU device for which we do this operation | |
929 | * @cpumask: cpumask to update with information of sharing CPUs | |
930 | * | |
931 | * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. | |
932 | * | |
933 | * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev. | |
f47b72a1 VK |
934 | */ |
935 | int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, | |
936 | struct cpumask *cpumask) | |
937 | { | |
76279291 | 938 | struct device_node *np, *tmp_np, *cpu_np; |
f47b72a1 VK |
939 | int cpu, ret = 0; |
940 | ||
941 | /* Get OPP descriptor node */ | |
0764c604 | 942 | np = dev_pm_opp_of_get_opp_desc_node(cpu_dev); |
f47b72a1 | 943 | if (!np) { |
349aa92e | 944 | dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__); |
f47b72a1 VK |
945 | return -ENOENT; |
946 | } | |
947 | ||
948 | cpumask_set_cpu(cpu_dev->id, cpumask); | |
949 | ||
950 | /* OPPs are shared ? */ | |
951 | if (!of_property_read_bool(np, "opp-shared")) | |
952 | goto put_cpu_node; | |
953 | ||
954 | for_each_possible_cpu(cpu) { | |
955 | if (cpu == cpu_dev->id) | |
956 | continue; | |
957 | ||
9867999f | 958 | cpu_np = of_cpu_device_node_get(cpu); |
76279291 WR |
959 | if (!cpu_np) { |
960 | dev_err(cpu_dev, "%s: failed to get cpu%d node\n", | |
f47b72a1 | 961 | __func__, cpu); |
76279291 | 962 | ret = -ENOENT; |
f47b72a1 VK |
963 | goto put_cpu_node; |
964 | } | |
965 | ||
966 | /* Get OPP descriptor node */ | |
fa9b274f | 967 | tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0); |
9867999f | 968 | of_node_put(cpu_np); |
f47b72a1 | 969 | if (!tmp_np) { |
76279291 | 970 | pr_err("%pOF: Couldn't find opp node\n", cpu_np); |
f47b72a1 VK |
971 | ret = -ENOENT; |
972 | goto put_cpu_node; | |
973 | } | |
974 | ||
975 | /* CPUs are sharing opp node */ | |
976 | if (np == tmp_np) | |
977 | cpumask_set_cpu(cpu, cpumask); | |
978 | ||
979 | of_node_put(tmp_np); | |
980 | } | |
981 | ||
982 | put_cpu_node: | |
983 | of_node_put(np); | |
984 | return ret; | |
985 | } | |
986 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus); | |
a88bd2a5 VK |
987 | |
988 | /** | |
4c6a343e | 989 | * of_get_required_opp_performance_state() - Search for required OPP and return its performance state. |
a88bd2a5 | 990 | * @np: Node that contains the "required-opps" property. |
4c6a343e | 991 | * @index: Index of the phandle to parse. |
a88bd2a5 | 992 | * |
4c6a343e VK |
993 | * Returns the performance state of the OPP pointed out by the "required-opps" |
994 | * property at @index in @np. | |
a88bd2a5 | 995 | * |
2feb5a89 VK |
996 | * Return: Zero or positive performance state on success, otherwise negative |
997 | * value on errors. | |
a88bd2a5 | 998 | */ |
2feb5a89 | 999 | int of_get_required_opp_performance_state(struct device_node *np, int index) |
a88bd2a5 | 1000 | { |
4c6a343e | 1001 | struct dev_pm_opp *opp; |
a88bd2a5 VK |
1002 | struct device_node *required_np; |
1003 | struct opp_table *opp_table; | |
2feb5a89 | 1004 | int pstate = -EINVAL; |
a88bd2a5 | 1005 | |
4c6a343e VK |
1006 | required_np = of_parse_required_opp(np, index); |
1007 | if (!required_np) | |
2feb5a89 | 1008 | return -EINVAL; |
a88bd2a5 | 1009 | |
4c6a343e VK |
1010 | opp_table = _find_table_of_opp_np(required_np); |
1011 | if (IS_ERR(opp_table)) { | |
1012 | pr_err("%s: Failed to find required OPP table %pOF: %ld\n", | |
1013 | __func__, np, PTR_ERR(opp_table)); | |
1014 | goto put_required_np; | |
a88bd2a5 VK |
1015 | } |
1016 | ||
4c6a343e VK |
1017 | opp = _find_opp_of_np(opp_table, required_np); |
1018 | if (opp) { | |
1019 | pstate = opp->pstate; | |
1020 | dev_pm_opp_put(opp); | |
a88bd2a5 VK |
1021 | } |
1022 | ||
4c6a343e | 1023 | dev_pm_opp_put_opp_table(opp_table); |
a88bd2a5 | 1024 | |
4c6a343e | 1025 | put_required_np: |
a88bd2a5 | 1026 | of_node_put(required_np); |
a88bd2a5 | 1027 | |
4c6a343e | 1028 | return pstate; |
a88bd2a5 | 1029 | } |
4c6a343e | 1030 | EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state); |
e2f4b5f8 VK |
1031 | |
1032 | /** | |
1033 | * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp | |
1034 | * @opp: opp for which DT node has to be returned for | |
1035 | * | |
1036 | * Return: DT node corresponding to the opp, else 0 on success. | |
1037 | * | |
1038 | * The caller needs to put the node with of_node_put() after using it. | |
1039 | */ | |
1040 | struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp) | |
1041 | { | |
1042 | if (IS_ERR_OR_NULL(opp)) { | |
1043 | pr_err("%s: Invalid parameters\n", __func__); | |
1044 | return NULL; | |
1045 | } | |
1046 | ||
1047 | return of_node_get(opp->np); | |
1048 | } | |
1049 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node); |