GNU Linux-libre 4.14.313-gnu1
[releases.git] / drivers / base / power / opp / of.c
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>
19 #include <linux/of.h>
20 #include <linux/slab.h>
21 #include <linux/export.h>
22
23 #include "opp.h"
24
25 static struct opp_table *_managed_opp(const struct device_node *np)
26 {
27         struct opp_table *opp_table, *managed_table = NULL;
28
29         mutex_lock(&opp_table_lock);
30
31         list_for_each_entry(opp_table, &opp_tables, node) {
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                          */
40                         if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
41                                 _get_opp_table_kref(opp_table);
42                                 managed_table = opp_table;
43                         }
44
45                         break;
46                 }
47         }
48
49         mutex_unlock(&opp_table_lock);
50
51         return managed_table;
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
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         }
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
111 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
112                               struct opp_table *opp_table)
113 {
114         u32 *microvolt, *microamp = NULL;
115         int supplies, vcount, icount, ret, i, j;
116         struct property *prop = NULL;
117         char name[NAME_MAX];
118
119         supplies = opp_table->regulator_count ? opp_table->regulator_count : 1;
120
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 */
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                 }
142         }
143
144         vcount = of_property_count_u32_elems(opp->np, name);
145         if (vcount < 0) {
146                 dev_err(dev, "%s: Invalid %s property (%d)\n",
147                         __func__, name, vcount);
148                 return vcount;
149         }
150
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);
155                 return -EINVAL;
156         }
157
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);
163         if (ret) {
164                 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
165                 ret = -EINVAL;
166                 goto free_microvolt;
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
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                 }
191
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;
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.
244  */
245 void dev_pm_opp_of_remove_table(struct device *dev)
246 {
247         _dev_pm_opp_find_and_remove_table(dev, false);
248 }
249 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
250
251 /* Returns opp descriptor node for a device node, caller must
252  * do of_node_put() */
253 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np)
254 {
255         /*
256          * There should be only ONE phandle present in "operating-points-v2"
257          * property.
258          */
259
260         return of_parse_phandle(np, "operating-points-v2", 0);
261 }
262
263 /* Returns opp descriptor node for a device, caller must do of_node_put() */
264 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
265 {
266         return _opp_of_get_opp_desc_node(dev->of_node);
267 }
268 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
269
270 /**
271  * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
272  * @opp_table:  OPP table
273  * @dev:        device for which we do this operation
274  * @np:         device node
275  *
276  * This function adds an opp definition to the opp table and returns status. The
277  * opp can be controlled using dev_pm_opp_enable/disable functions and may be
278  * removed by dev_pm_opp_remove.
279  *
280  * Return:
281  * 0            On success OR
282  *              Duplicate OPPs (both freq and volt are same) and opp->available
283  * -EEXIST      Freq are same and volt are different OR
284  *              Duplicate OPPs (both freq and volt are same) and !opp->available
285  * -ENOMEM      Memory allocation failure
286  * -EINVAL      Failed parsing the OPP node
287  */
288 static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev,
289                               struct device_node *np)
290 {
291         struct dev_pm_opp *new_opp;
292         u64 rate;
293         u32 val;
294         int ret;
295
296         new_opp = _opp_allocate(opp_table);
297         if (!new_opp)
298                 return -ENOMEM;
299
300         ret = of_property_read_u64(np, "opp-hz", &rate);
301         if (ret < 0) {
302                 dev_err(dev, "%s: opp-hz not found\n", __func__);
303                 goto free_opp;
304         }
305
306         /* Check if the OPP supports hardware's hierarchy of versions or not */
307         if (!_opp_is_supported(dev, opp_table, np)) {
308                 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
309                 goto free_opp;
310         }
311
312         /*
313          * Rate is defined as an unsigned long in clk API, and so casting
314          * explicitly to its type. Must be fixed once rate is 64 bit
315          * guaranteed in clk API.
316          */
317         new_opp->rate = (unsigned long)rate;
318         new_opp->turbo = of_property_read_bool(np, "turbo-mode");
319
320         new_opp->np = np;
321         new_opp->dynamic = false;
322         new_opp->available = true;
323
324         if (!of_property_read_u32(np, "clock-latency-ns", &val))
325                 new_opp->clock_latency_ns = val;
326
327         ret = opp_parse_supplies(new_opp, dev, opp_table);
328         if (ret)
329                 goto free_opp;
330
331         ret = _opp_add(dev, new_opp, opp_table);
332         if (ret) {
333                 /* Don't return error for duplicate OPPs */
334                 if (ret == -EBUSY)
335                         ret = 0;
336                 goto free_opp;
337         }
338
339         /* OPP to select on device suspend */
340         if (of_property_read_bool(np, "opp-suspend")) {
341                 if (opp_table->suspend_opp) {
342                         dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
343                                  __func__, opp_table->suspend_opp->rate,
344                                  new_opp->rate);
345                 } else {
346                         new_opp->suspend = true;
347                         opp_table->suspend_opp = new_opp;
348                 }
349         }
350
351         if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
352                 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
353
354         pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
355                  __func__, new_opp->turbo, new_opp->rate,
356                  new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
357                  new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
358
359         /*
360          * Notify the changes in the availability of the operable
361          * frequency/voltage list.
362          */
363         blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
364         return 0;
365
366 free_opp:
367         _opp_free(new_opp);
368
369         return ret;
370 }
371
372 /* Initializes OPP tables based on new bindings */
373 static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
374 {
375         struct device_node *np;
376         struct opp_table *opp_table;
377         int ret = 0, count = 0;
378
379         opp_table = _managed_opp(opp_np);
380         if (opp_table) {
381                 /* OPPs are already managed */
382                 if (!_add_opp_dev(dev, opp_table))
383                         ret = -ENOMEM;
384                 goto put_opp_table;
385         }
386
387         opp_table = dev_pm_opp_get_opp_table(dev);
388         if (!opp_table)
389                 return -ENOMEM;
390
391         /* We have opp-table node now, iterate over it and add OPPs */
392         for_each_available_child_of_node(opp_np, np) {
393                 count++;
394
395                 ret = _opp_add_static_v2(opp_table, dev, np);
396                 if (ret) {
397                         dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
398                                 ret);
399                         _dev_pm_opp_remove_table(opp_table, dev, false);
400                         of_node_put(np);
401                         goto put_opp_table;
402                 }
403         }
404
405         /* There should be one or more OPPs defined */
406         if (!count) {
407                 dev_err(dev, "%s: no supported OPPs", __func__);
408                 ret = -ENOENT;
409                 goto put_opp_table;
410         }
411
412         opp_table->np = opp_np;
413         if (of_property_read_bool(opp_np, "opp-shared"))
414                 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
415         else
416                 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
417
418 put_opp_table:
419         dev_pm_opp_put_opp_table(opp_table);
420
421         return ret;
422 }
423
424 /* Initializes OPP tables based on old-deprecated bindings */
425 static int _of_add_opp_table_v1(struct device *dev)
426 {
427         struct opp_table *opp_table;
428         const struct property *prop;
429         const __be32 *val;
430         int nr, ret = 0;
431
432         prop = of_find_property(dev->of_node, "operating-points", NULL);
433         if (!prop)
434                 return -ENODEV;
435         if (!prop->value)
436                 return -ENODATA;
437
438         /*
439          * Each OPP is a set of tuples consisting of frequency and
440          * voltage like <freq-kHz vol-uV>.
441          */
442         nr = prop->length / sizeof(u32);
443         if (nr % 2) {
444                 dev_err(dev, "%s: Invalid OPP table\n", __func__);
445                 return -EINVAL;
446         }
447
448         opp_table = dev_pm_opp_get_opp_table(dev);
449         if (!opp_table)
450                 return -ENOMEM;
451
452         val = prop->value;
453         while (nr) {
454                 unsigned long freq = be32_to_cpup(val++) * 1000;
455                 unsigned long volt = be32_to_cpup(val++);
456
457                 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
458                 if (ret) {
459                         dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
460                                 __func__, freq, ret);
461                         _dev_pm_opp_remove_table(opp_table, dev, false);
462                         break;
463                 }
464                 nr -= 2;
465         }
466
467         dev_pm_opp_put_opp_table(opp_table);
468         return ret;
469 }
470
471 /**
472  * dev_pm_opp_of_add_table() - Initialize opp table from device tree
473  * @dev:        device pointer used to lookup OPP table.
474  *
475  * Register the initial OPP table with the OPP library for given device.
476  *
477  * Return:
478  * 0            On success OR
479  *              Duplicate OPPs (both freq and volt are same) and opp->available
480  * -EEXIST      Freq are same and volt are different OR
481  *              Duplicate OPPs (both freq and volt are same) and !opp->available
482  * -ENOMEM      Memory allocation failure
483  * -ENODEV      when 'operating-points' property is not found or is invalid data
484  *              in device node.
485  * -ENODATA     when empty 'operating-points' property is found
486  * -EINVAL      when invalid entries are found in opp-v2 table
487  */
488 int dev_pm_opp_of_add_table(struct device *dev)
489 {
490         struct device_node *opp_np;
491         int ret;
492
493         /*
494          * OPPs have two version of bindings now. The older one is deprecated,
495          * try for the new binding first.
496          */
497         opp_np = dev_pm_opp_of_get_opp_desc_node(dev);
498         if (!opp_np) {
499                 /*
500                  * Try old-deprecated bindings for backward compatibility with
501                  * older dtbs.
502                  */
503                 return _of_add_opp_table_v1(dev);
504         }
505
506         ret = _of_add_opp_table_v2(dev, opp_np);
507         of_node_put(opp_np);
508
509         return ret;
510 }
511 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
512
513 /* CPU device specific helpers */
514
515 /**
516  * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
517  * @cpumask:    cpumask for which OPP table needs to be removed
518  *
519  * This removes the OPP tables for CPUs present in the @cpumask.
520  * This should be used only to remove static entries created from DT.
521  */
522 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
523 {
524         _dev_pm_opp_cpumask_remove_table(cpumask, true);
525 }
526 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
527
528 /**
529  * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
530  * @cpumask:    cpumask for which OPP table needs to be added.
531  *
532  * This adds the OPP tables for CPUs present in the @cpumask.
533  */
534 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
535 {
536         struct device *cpu_dev;
537         int cpu, ret = 0;
538
539         WARN_ON(cpumask_empty(cpumask));
540
541         for_each_cpu(cpu, cpumask) {
542                 cpu_dev = get_cpu_device(cpu);
543                 if (!cpu_dev) {
544                         pr_err("%s: failed to get cpu%d device\n", __func__,
545                                cpu);
546                         continue;
547                 }
548
549                 ret = dev_pm_opp_of_add_table(cpu_dev);
550                 if (ret) {
551                         /*
552                          * OPP may get registered dynamically, don't print error
553                          * message here.
554                          */
555                         pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
556                                  __func__, cpu, ret);
557
558                         /* Free all other OPPs */
559                         dev_pm_opp_of_cpumask_remove_table(cpumask);
560                         break;
561                 }
562         }
563
564         return ret;
565 }
566 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
567
568 /*
569  * Works only for OPP v2 bindings.
570  *
571  * Returns -ENOENT if operating-points-v2 bindings aren't supported.
572  */
573 /**
574  * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
575  *                                    @cpu_dev using operating-points-v2
576  *                                    bindings.
577  *
578  * @cpu_dev:    CPU device for which we do this operation
579  * @cpumask:    cpumask to update with information of sharing CPUs
580  *
581  * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
582  *
583  * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
584  */
585 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
586                                    struct cpumask *cpumask)
587 {
588         struct device_node *np, *tmp_np, *cpu_np;
589         int cpu, ret = 0;
590
591         /* Get OPP descriptor node */
592         np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
593         if (!np) {
594                 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
595                 return -ENOENT;
596         }
597
598         cpumask_set_cpu(cpu_dev->id, cpumask);
599
600         /* OPPs are shared ? */
601         if (!of_property_read_bool(np, "opp-shared"))
602                 goto put_cpu_node;
603
604         for_each_possible_cpu(cpu) {
605                 if (cpu == cpu_dev->id)
606                         continue;
607
608                 cpu_np = of_get_cpu_node(cpu, NULL);
609                 if (!cpu_np) {
610                         dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
611                                 __func__, cpu);
612                         ret = -ENOENT;
613                         goto put_cpu_node;
614                 }
615
616                 /* Get OPP descriptor node */
617                 tmp_np = _opp_of_get_opp_desc_node(cpu_np);
618                 if (!tmp_np) {
619                         pr_err("%pOF: Couldn't find opp node\n", cpu_np);
620                         ret = -ENOENT;
621                         goto put_cpu_node;
622                 }
623
624                 /* CPUs are sharing opp node */
625                 if (np == tmp_np)
626                         cpumask_set_cpu(cpu, cpumask);
627
628                 of_node_put(tmp_np);
629         }
630
631 put_cpu_node:
632         of_node_put(np);
633         return ret;
634 }
635 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);