x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / base / power / opp / of.c
blob779428676f63c07e8f0e05982e6354d623b4a821
1 /*
2 * Generic OPP OF helpers
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/cpu.h>
17 #include <linux/errno.h>
18 #include <linux/device.h>
19 #include <linux/of.h>
20 #include <linux/slab.h>
21 #include <linux/export.h>
23 #include "opp.h"
25 static struct opp_table *_managed_opp(const struct device_node *np)
27 struct opp_table *opp_table, *managed_table = NULL;
29 mutex_lock(&opp_table_lock);
31 list_for_each_entry(opp_table, &opp_tables, node) {
32 if (opp_table->np == np) {
34 * Multiple devices can point to the same OPP table and
35 * so will have same node-pointer, np.
37 * But the OPPs will be considered as shared only if the
38 * OPP table contains a "opp-shared" property.
40 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
41 _get_opp_table_kref(opp_table);
42 managed_table = opp_table;
45 break;
49 mutex_unlock(&opp_table_lock);
51 return managed_table;
54 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev)
56 struct device_node *np;
59 * Only required for backward compatibility with v1 bindings, but isn't
60 * harmful for other cases. And so we do it unconditionally.
62 np = of_node_get(dev->of_node);
63 if (np) {
64 u32 val;
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);
74 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
75 struct device_node *np)
77 unsigned int count = opp_table->supported_hw_count;
78 u32 version;
79 int ret;
81 if (!opp_table->supported_hw) {
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.
88 if (of_find_property(np, "opp-supported-hw", NULL))
89 return false;
90 else
91 return true;
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;
103 /* Both of these are bitwise masks of the versions */
104 if (!(version & opp_table->supported_hw[count]))
105 return false;
108 return true;
111 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
112 struct opp_table *opp_table)
114 u32 *microvolt, *microamp = NULL;
115 int supplies, vcount, icount, ret, i, j;
116 struct property *prop = NULL;
117 char name[NAME_MAX];
119 supplies = opp_table->regulator_count ? opp_table->regulator_count : 1;
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);
128 if (!prop) {
129 /* Search for "opp-microvolt" */
130 sprintf(name, "opp-microvolt");
131 prop = of_find_property(opp->np, name, NULL);
133 /* Missing property isn't a problem, but an invalid entry is */
134 if (!prop)
135 return 0;
138 vcount = of_property_count_u32_elems(opp->np, name);
139 if (vcount < 0) {
140 dev_err(dev, "%s: Invalid %s property (%d)\n",
141 __func__, name, vcount);
142 return vcount;
145 /* There can be one or three elements per supply */
146 if (vcount != supplies && vcount != supplies * 3) {
147 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
148 __func__, name, vcount, supplies);
149 return -EINVAL;
152 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
153 if (!microvolt)
154 return -ENOMEM;
156 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
157 if (ret) {
158 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
159 ret = -EINVAL;
160 goto free_microvolt;
163 /* Search for "opp-microamp-<name>" */
164 prop = NULL;
165 if (opp_table->prop_name) {
166 snprintf(name, sizeof(name), "opp-microamp-%s",
167 opp_table->prop_name);
168 prop = of_find_property(opp->np, name, NULL);
171 if (!prop) {
172 /* Search for "opp-microamp" */
173 sprintf(name, "opp-microamp");
174 prop = of_find_property(opp->np, name, NULL);
177 if (prop) {
178 icount = of_property_count_u32_elems(opp->np, name);
179 if (icount < 0) {
180 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
181 name, icount);
182 ret = icount;
183 goto free_microvolt;
186 if (icount != supplies) {
187 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
188 __func__, name, icount, supplies);
189 ret = -EINVAL;
190 goto free_microvolt;
193 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
194 if (!microamp) {
195 ret = -EINVAL;
196 goto free_microvolt;
199 ret = of_property_read_u32_array(opp->np, name, microamp,
200 icount);
201 if (ret) {
202 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
203 name, ret);
204 ret = -EINVAL;
205 goto free_microamp;
209 for (i = 0, j = 0; i < supplies; i++) {
210 opp->supplies[i].u_volt = microvolt[j++];
212 if (vcount == supplies) {
213 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
214 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
215 } else {
216 opp->supplies[i].u_volt_min = microvolt[j++];
217 opp->supplies[i].u_volt_max = microvolt[j++];
220 if (microamp)
221 opp->supplies[i].u_amp = microamp[i];
224 free_microamp:
225 kfree(microamp);
226 free_microvolt:
227 kfree(microvolt);
229 return ret;
233 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
234 * entries
235 * @dev: device pointer used to lookup OPP table.
237 * Free OPPs created using static entries present in DT.
239 void dev_pm_opp_of_remove_table(struct device *dev)
241 _dev_pm_opp_find_and_remove_table(dev, false);
243 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
245 /* Returns opp descriptor node for a device, caller must do of_node_put() */
246 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
249 * There should be only ONE phandle present in "operating-points-v2"
250 * property.
253 return of_parse_phandle(dev->of_node, "operating-points-v2", 0);
255 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
258 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
259 * @opp_table: OPP table
260 * @dev: device for which we do this operation
261 * @np: device node
263 * This function adds an opp definition to the opp table and returns status. The
264 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
265 * removed by dev_pm_opp_remove.
267 * Return:
268 * 0 On success OR
269 * Duplicate OPPs (both freq and volt are same) and opp->available
270 * -EEXIST Freq are same and volt are different OR
271 * Duplicate OPPs (both freq and volt are same) and !opp->available
272 * -ENOMEM Memory allocation failure
273 * -EINVAL Failed parsing the OPP node
275 static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev,
276 struct device_node *np)
278 struct dev_pm_opp *new_opp;
279 u64 rate;
280 u32 val;
281 int ret;
283 new_opp = _opp_allocate(opp_table);
284 if (!new_opp)
285 return -ENOMEM;
287 ret = of_property_read_u64(np, "opp-hz", &rate);
288 if (ret < 0) {
289 dev_err(dev, "%s: opp-hz not found\n", __func__);
290 goto free_opp;
293 /* Check if the OPP supports hardware's hierarchy of versions or not */
294 if (!_opp_is_supported(dev, opp_table, np)) {
295 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
296 goto free_opp;
300 * Rate is defined as an unsigned long in clk API, and so casting
301 * explicitly to its type. Must be fixed once rate is 64 bit
302 * guaranteed in clk API.
304 new_opp->rate = (unsigned long)rate;
305 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
307 new_opp->np = np;
308 new_opp->dynamic = false;
309 new_opp->available = true;
311 if (!of_property_read_u32(np, "clock-latency-ns", &val))
312 new_opp->clock_latency_ns = val;
314 ret = opp_parse_supplies(new_opp, dev, opp_table);
315 if (ret)
316 goto free_opp;
318 ret = _opp_add(dev, new_opp, opp_table);
319 if (ret) {
320 /* Don't return error for duplicate OPPs */
321 if (ret == -EBUSY)
322 ret = 0;
323 goto free_opp;
326 /* OPP to select on device suspend */
327 if (of_property_read_bool(np, "opp-suspend")) {
328 if (opp_table->suspend_opp) {
329 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
330 __func__, opp_table->suspend_opp->rate,
331 new_opp->rate);
332 } else {
333 new_opp->suspend = true;
334 opp_table->suspend_opp = new_opp;
338 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
339 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
341 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
342 __func__, new_opp->turbo, new_opp->rate,
343 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
344 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
347 * Notify the changes in the availability of the operable
348 * frequency/voltage list.
350 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
351 return 0;
353 free_opp:
354 _opp_free(new_opp);
356 return ret;
359 /* Initializes OPP tables based on new bindings */
360 static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
362 struct device_node *np;
363 struct opp_table *opp_table;
364 int ret = 0, count = 0;
366 opp_table = _managed_opp(opp_np);
367 if (opp_table) {
368 /* OPPs are already managed */
369 if (!_add_opp_dev(dev, opp_table))
370 ret = -ENOMEM;
371 goto put_opp_table;
374 opp_table = dev_pm_opp_get_opp_table(dev);
375 if (!opp_table)
376 return -ENOMEM;
378 /* We have opp-table node now, iterate over it and add OPPs */
379 for_each_available_child_of_node(opp_np, np) {
380 count++;
382 ret = _opp_add_static_v2(opp_table, dev, np);
383 if (ret) {
384 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
385 ret);
386 _dev_pm_opp_remove_table(opp_table, dev, false);
387 goto put_opp_table;
391 /* There should be one of more OPP defined */
392 if (WARN_ON(!count)) {
393 ret = -ENOENT;
394 goto put_opp_table;
397 opp_table->np = opp_np;
398 if (of_property_read_bool(opp_np, "opp-shared"))
399 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
400 else
401 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
403 put_opp_table:
404 dev_pm_opp_put_opp_table(opp_table);
406 return ret;
409 /* Initializes OPP tables based on old-deprecated bindings */
410 static int _of_add_opp_table_v1(struct device *dev)
412 struct opp_table *opp_table;
413 const struct property *prop;
414 const __be32 *val;
415 int nr, ret = 0;
417 prop = of_find_property(dev->of_node, "operating-points", NULL);
418 if (!prop)
419 return -ENODEV;
420 if (!prop->value)
421 return -ENODATA;
424 * Each OPP is a set of tuples consisting of frequency and
425 * voltage like <freq-kHz vol-uV>.
427 nr = prop->length / sizeof(u32);
428 if (nr % 2) {
429 dev_err(dev, "%s: Invalid OPP table\n", __func__);
430 return -EINVAL;
433 opp_table = dev_pm_opp_get_opp_table(dev);
434 if (!opp_table)
435 return -ENOMEM;
437 val = prop->value;
438 while (nr) {
439 unsigned long freq = be32_to_cpup(val++) * 1000;
440 unsigned long volt = be32_to_cpup(val++);
442 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
443 if (ret) {
444 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
445 __func__, freq, ret);
446 _dev_pm_opp_remove_table(opp_table, dev, false);
447 break;
449 nr -= 2;
452 dev_pm_opp_put_opp_table(opp_table);
453 return ret;
457 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
458 * @dev: device pointer used to lookup OPP table.
460 * Register the initial OPP table with the OPP library for given device.
462 * Return:
463 * 0 On success OR
464 * Duplicate OPPs (both freq and volt are same) and opp->available
465 * -EEXIST Freq are same and volt are different OR
466 * Duplicate OPPs (both freq and volt are same) and !opp->available
467 * -ENOMEM Memory allocation failure
468 * -ENODEV when 'operating-points' property is not found or is invalid data
469 * in device node.
470 * -ENODATA when empty 'operating-points' property is found
471 * -EINVAL when invalid entries are found in opp-v2 table
473 int dev_pm_opp_of_add_table(struct device *dev)
475 struct device_node *opp_np;
476 int ret;
479 * OPPs have two version of bindings now. The older one is deprecated,
480 * try for the new binding first.
482 opp_np = dev_pm_opp_of_get_opp_desc_node(dev);
483 if (!opp_np) {
485 * Try old-deprecated bindings for backward compatibility with
486 * older dtbs.
488 return _of_add_opp_table_v1(dev);
491 ret = _of_add_opp_table_v2(dev, opp_np);
492 of_node_put(opp_np);
494 return ret;
496 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
498 /* CPU device specific helpers */
501 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
502 * @cpumask: cpumask for which OPP table needs to be removed
504 * This removes the OPP tables for CPUs present in the @cpumask.
505 * This should be used only to remove static entries created from DT.
507 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
509 _dev_pm_opp_cpumask_remove_table(cpumask, true);
511 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
514 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
515 * @cpumask: cpumask for which OPP table needs to be added.
517 * This adds the OPP tables for CPUs present in the @cpumask.
519 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
521 struct device *cpu_dev;
522 int cpu, ret = 0;
524 WARN_ON(cpumask_empty(cpumask));
526 for_each_cpu(cpu, cpumask) {
527 cpu_dev = get_cpu_device(cpu);
528 if (!cpu_dev) {
529 pr_err("%s: failed to get cpu%d device\n", __func__,
530 cpu);
531 continue;
534 ret = dev_pm_opp_of_add_table(cpu_dev);
535 if (ret) {
536 pr_err("%s: couldn't find opp table for cpu:%d, %d\n",
537 __func__, cpu, ret);
539 /* Free all other OPPs */
540 dev_pm_opp_of_cpumask_remove_table(cpumask);
541 break;
545 return ret;
547 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
550 * Works only for OPP v2 bindings.
552 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
555 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
556 * @cpu_dev using operating-points-v2
557 * bindings.
559 * @cpu_dev: CPU device for which we do this operation
560 * @cpumask: cpumask to update with information of sharing CPUs
562 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
564 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
566 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
567 struct cpumask *cpumask)
569 struct device_node *np, *tmp_np;
570 struct device *tcpu_dev;
571 int cpu, ret = 0;
573 /* Get OPP descriptor node */
574 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
575 if (!np) {
576 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
577 return -ENOENT;
580 cpumask_set_cpu(cpu_dev->id, cpumask);
582 /* OPPs are shared ? */
583 if (!of_property_read_bool(np, "opp-shared"))
584 goto put_cpu_node;
586 for_each_possible_cpu(cpu) {
587 if (cpu == cpu_dev->id)
588 continue;
590 tcpu_dev = get_cpu_device(cpu);
591 if (!tcpu_dev) {
592 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
593 __func__, cpu);
594 ret = -ENODEV;
595 goto put_cpu_node;
598 /* Get OPP descriptor node */
599 tmp_np = dev_pm_opp_of_get_opp_desc_node(tcpu_dev);
600 if (!tmp_np) {
601 dev_err(tcpu_dev, "%s: Couldn't find opp node.\n",
602 __func__);
603 ret = -ENOENT;
604 goto put_cpu_node;
607 /* CPUs are sharing opp node */
608 if (np == tmp_np)
609 cpumask_set_cpu(cpu, cpumask);
611 of_node_put(tmp_np);
614 put_cpu_node:
615 of_node_put(np);
616 return ret;
618 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);