]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/opp/core.c
OPP: Pass index to _of_init_opp_table()
[linux.git] / drivers / opp / core.c
1 /*
2  * Generic OPP Interface
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/clk.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include <linux/export.h>
22 #include <linux/pm_domain.h>
23 #include <linux/regulator/consumer.h>
24
25 #include "opp.h"
26
27 /*
28  * The root of the list of all opp-tables. All opp_table structures branch off
29  * from here, with each opp_table containing the list of opps it supports in
30  * various states of availability.
31  */
32 LIST_HEAD(opp_tables);
33 /* Lock to allow exclusive modification to the device and opp lists */
34 DEFINE_MUTEX(opp_table_lock);
35
36 static struct opp_device *_find_opp_dev(const struct device *dev,
37                                         struct opp_table *opp_table)
38 {
39         struct opp_device *opp_dev;
40
41         list_for_each_entry(opp_dev, &opp_table->dev_list, node)
42                 if (opp_dev->dev == dev)
43                         return opp_dev;
44
45         return NULL;
46 }
47
48 static struct opp_table *_find_opp_table_unlocked(struct device *dev)
49 {
50         struct opp_table *opp_table;
51         bool found;
52
53         list_for_each_entry(opp_table, &opp_tables, node) {
54                 mutex_lock(&opp_table->lock);
55                 found = !!_find_opp_dev(dev, opp_table);
56                 mutex_unlock(&opp_table->lock);
57
58                 if (found) {
59                         _get_opp_table_kref(opp_table);
60
61                         return opp_table;
62                 }
63         }
64
65         return ERR_PTR(-ENODEV);
66 }
67
68 /**
69  * _find_opp_table() - find opp_table struct using device pointer
70  * @dev:        device pointer used to lookup OPP table
71  *
72  * Search OPP table for one containing matching device.
73  *
74  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
75  * -EINVAL based on type of error.
76  *
77  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
78  */
79 struct opp_table *_find_opp_table(struct device *dev)
80 {
81         struct opp_table *opp_table;
82
83         if (IS_ERR_OR_NULL(dev)) {
84                 pr_err("%s: Invalid parameters\n", __func__);
85                 return ERR_PTR(-EINVAL);
86         }
87
88         mutex_lock(&opp_table_lock);
89         opp_table = _find_opp_table_unlocked(dev);
90         mutex_unlock(&opp_table_lock);
91
92         return opp_table;
93 }
94
95 /**
96  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
97  * @opp:        opp for which voltage has to be returned for
98  *
99  * Return: voltage in micro volt corresponding to the opp, else
100  * return 0
101  *
102  * This is useful only for devices with single power supply.
103  */
104 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
105 {
106         if (IS_ERR_OR_NULL(opp)) {
107                 pr_err("%s: Invalid parameters\n", __func__);
108                 return 0;
109         }
110
111         return opp->supplies[0].u_volt;
112 }
113 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
114
115 /**
116  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
117  * @opp:        opp for which frequency has to be returned for
118  *
119  * Return: frequency in hertz corresponding to the opp, else
120  * return 0
121  */
122 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
123 {
124         if (IS_ERR_OR_NULL(opp) || !opp->available) {
125                 pr_err("%s: Invalid parameters\n", __func__);
126                 return 0;
127         }
128
129         return opp->rate;
130 }
131 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
132
133 /**
134  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
135  * @opp: opp for which turbo mode is being verified
136  *
137  * Turbo OPPs are not for normal use, and can be enabled (under certain
138  * conditions) for short duration of times to finish high throughput work
139  * quickly. Running on them for longer times may overheat the chip.
140  *
141  * Return: true if opp is turbo opp, else false.
142  */
143 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
144 {
145         if (IS_ERR_OR_NULL(opp) || !opp->available) {
146                 pr_err("%s: Invalid parameters\n", __func__);
147                 return false;
148         }
149
150         return opp->turbo;
151 }
152 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
153
154 /**
155  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
156  * @dev:        device for which we do this operation
157  *
158  * Return: This function returns the max clock latency in nanoseconds.
159  */
160 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
161 {
162         struct opp_table *opp_table;
163         unsigned long clock_latency_ns;
164
165         opp_table = _find_opp_table(dev);
166         if (IS_ERR(opp_table))
167                 return 0;
168
169         clock_latency_ns = opp_table->clock_latency_ns_max;
170
171         dev_pm_opp_put_opp_table(opp_table);
172
173         return clock_latency_ns;
174 }
175 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
176
177 /**
178  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
179  * @dev: device for which we do this operation
180  *
181  * Return: This function returns the max voltage latency in nanoseconds.
182  */
183 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
184 {
185         struct opp_table *opp_table;
186         struct dev_pm_opp *opp;
187         struct regulator *reg;
188         unsigned long latency_ns = 0;
189         int ret, i, count;
190         struct {
191                 unsigned long min;
192                 unsigned long max;
193         } *uV;
194
195         opp_table = _find_opp_table(dev);
196         if (IS_ERR(opp_table))
197                 return 0;
198
199         count = opp_table->regulator_count;
200
201         /* Regulator may not be required for the device */
202         if (!count)
203                 goto put_opp_table;
204
205         uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
206         if (!uV)
207                 goto put_opp_table;
208
209         mutex_lock(&opp_table->lock);
210
211         for (i = 0; i < count; i++) {
212                 uV[i].min = ~0;
213                 uV[i].max = 0;
214
215                 list_for_each_entry(opp, &opp_table->opp_list, node) {
216                         if (!opp->available)
217                                 continue;
218
219                         if (opp->supplies[i].u_volt_min < uV[i].min)
220                                 uV[i].min = opp->supplies[i].u_volt_min;
221                         if (opp->supplies[i].u_volt_max > uV[i].max)
222                                 uV[i].max = opp->supplies[i].u_volt_max;
223                 }
224         }
225
226         mutex_unlock(&opp_table->lock);
227
228         /*
229          * The caller needs to ensure that opp_table (and hence the regulator)
230          * isn't freed, while we are executing this routine.
231          */
232         for (i = 0; i < count; i++) {
233                 reg = opp_table->regulators[i];
234                 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
235                 if (ret > 0)
236                         latency_ns += ret * 1000;
237         }
238
239         kfree(uV);
240 put_opp_table:
241         dev_pm_opp_put_opp_table(opp_table);
242
243         return latency_ns;
244 }
245 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
246
247 /**
248  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
249  *                                           nanoseconds
250  * @dev: device for which we do this operation
251  *
252  * Return: This function returns the max transition latency, in nanoseconds, to
253  * switch from one OPP to other.
254  */
255 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
256 {
257         return dev_pm_opp_get_max_volt_latency(dev) +
258                 dev_pm_opp_get_max_clock_latency(dev);
259 }
260 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
261
262 /**
263  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
264  * @dev:        device for which we do this operation
265  *
266  * Return: This function returns the frequency of the OPP marked as suspend_opp
267  * if one is available, else returns 0;
268  */
269 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
270 {
271         struct opp_table *opp_table;
272         unsigned long freq = 0;
273
274         opp_table = _find_opp_table(dev);
275         if (IS_ERR(opp_table))
276                 return 0;
277
278         if (opp_table->suspend_opp && opp_table->suspend_opp->available)
279                 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
280
281         dev_pm_opp_put_opp_table(opp_table);
282
283         return freq;
284 }
285 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
286
287 int _get_opp_count(struct opp_table *opp_table)
288 {
289         struct dev_pm_opp *opp;
290         int count = 0;
291
292         mutex_lock(&opp_table->lock);
293
294         list_for_each_entry(opp, &opp_table->opp_list, node) {
295                 if (opp->available)
296                         count++;
297         }
298
299         mutex_unlock(&opp_table->lock);
300
301         return count;
302 }
303
304 /**
305  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
306  * @dev:        device for which we do this operation
307  *
308  * Return: This function returns the number of available opps if there are any,
309  * else returns 0 if none or the corresponding error value.
310  */
311 int dev_pm_opp_get_opp_count(struct device *dev)
312 {
313         struct opp_table *opp_table;
314         int count;
315
316         opp_table = _find_opp_table(dev);
317         if (IS_ERR(opp_table)) {
318                 count = PTR_ERR(opp_table);
319                 dev_dbg(dev, "%s: OPP table not found (%d)\n",
320                         __func__, count);
321                 return 0;
322         }
323
324         count = _get_opp_count(opp_table);
325         dev_pm_opp_put_opp_table(opp_table);
326
327         return count;
328 }
329 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
330
331 /**
332  * dev_pm_opp_find_freq_exact() - search for an exact frequency
333  * @dev:                device for which we do this operation
334  * @freq:               frequency to search for
335  * @available:          true/false - match for available opp
336  *
337  * Return: Searches for exact match in the opp table and returns pointer to the
338  * matching opp if found, else returns ERR_PTR in case of error and should
339  * be handled using IS_ERR. Error return values can be:
340  * EINVAL:      for bad pointer
341  * ERANGE:      no match found for search
342  * ENODEV:      if device not found in list of registered devices
343  *
344  * Note: available is a modifier for the search. if available=true, then the
345  * match is for exact matching frequency and is available in the stored OPP
346  * table. if false, the match is for exact frequency which is not available.
347  *
348  * This provides a mechanism to enable an opp which is not available currently
349  * or the opposite as well.
350  *
351  * The callers are required to call dev_pm_opp_put() for the returned OPP after
352  * use.
353  */
354 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
355                                               unsigned long freq,
356                                               bool available)
357 {
358         struct opp_table *opp_table;
359         struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
360
361         opp_table = _find_opp_table(dev);
362         if (IS_ERR(opp_table)) {
363                 int r = PTR_ERR(opp_table);
364
365                 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
366                 return ERR_PTR(r);
367         }
368
369         mutex_lock(&opp_table->lock);
370
371         list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
372                 if (temp_opp->available == available &&
373                                 temp_opp->rate == freq) {
374                         opp = temp_opp;
375
376                         /* Increment the reference count of OPP */
377                         dev_pm_opp_get(opp);
378                         break;
379                 }
380         }
381
382         mutex_unlock(&opp_table->lock);
383         dev_pm_opp_put_opp_table(opp_table);
384
385         return opp;
386 }
387 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
388
389 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
390                                                    unsigned long *freq)
391 {
392         struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
393
394         mutex_lock(&opp_table->lock);
395
396         list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
397                 if (temp_opp->available && temp_opp->rate >= *freq) {
398                         opp = temp_opp;
399                         *freq = opp->rate;
400
401                         /* Increment the reference count of OPP */
402                         dev_pm_opp_get(opp);
403                         break;
404                 }
405         }
406
407         mutex_unlock(&opp_table->lock);
408
409         return opp;
410 }
411
412 /**
413  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
414  * @dev:        device for which we do this operation
415  * @freq:       Start frequency
416  *
417  * Search for the matching ceil *available* OPP from a starting freq
418  * for a device.
419  *
420  * Return: matching *opp and refreshes *freq accordingly, else returns
421  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
422  * values can be:
423  * EINVAL:      for bad pointer
424  * ERANGE:      no match found for search
425  * ENODEV:      if device not found in list of registered devices
426  *
427  * The callers are required to call dev_pm_opp_put() for the returned OPP after
428  * use.
429  */
430 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
431                                              unsigned long *freq)
432 {
433         struct opp_table *opp_table;
434         struct dev_pm_opp *opp;
435
436         if (!dev || !freq) {
437                 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
438                 return ERR_PTR(-EINVAL);
439         }
440
441         opp_table = _find_opp_table(dev);
442         if (IS_ERR(opp_table))
443                 return ERR_CAST(opp_table);
444
445         opp = _find_freq_ceil(opp_table, freq);
446
447         dev_pm_opp_put_opp_table(opp_table);
448
449         return opp;
450 }
451 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
452
453 /**
454  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
455  * @dev:        device for which we do this operation
456  * @freq:       Start frequency
457  *
458  * Search for the matching floor *available* OPP from a starting freq
459  * for a device.
460  *
461  * Return: matching *opp and refreshes *freq accordingly, else returns
462  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
463  * values can be:
464  * EINVAL:      for bad pointer
465  * ERANGE:      no match found for search
466  * ENODEV:      if device not found in list of registered devices
467  *
468  * The callers are required to call dev_pm_opp_put() for the returned OPP after
469  * use.
470  */
471 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
472                                               unsigned long *freq)
473 {
474         struct opp_table *opp_table;
475         struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
476
477         if (!dev || !freq) {
478                 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
479                 return ERR_PTR(-EINVAL);
480         }
481
482         opp_table = _find_opp_table(dev);
483         if (IS_ERR(opp_table))
484                 return ERR_CAST(opp_table);
485
486         mutex_lock(&opp_table->lock);
487
488         list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
489                 if (temp_opp->available) {
490                         /* go to the next node, before choosing prev */
491                         if (temp_opp->rate > *freq)
492                                 break;
493                         else
494                                 opp = temp_opp;
495                 }
496         }
497
498         /* Increment the reference count of OPP */
499         if (!IS_ERR(opp))
500                 dev_pm_opp_get(opp);
501         mutex_unlock(&opp_table->lock);
502         dev_pm_opp_put_opp_table(opp_table);
503
504         if (!IS_ERR(opp))
505                 *freq = opp->rate;
506
507         return opp;
508 }
509 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
510
511 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
512                             struct dev_pm_opp_supply *supply)
513 {
514         int ret;
515
516         /* Regulator not available for device */
517         if (IS_ERR(reg)) {
518                 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
519                         PTR_ERR(reg));
520                 return 0;
521         }
522
523         dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
524                 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
525
526         ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
527                                             supply->u_volt, supply->u_volt_max);
528         if (ret)
529                 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
530                         __func__, supply->u_volt_min, supply->u_volt,
531                         supply->u_volt_max, ret);
532
533         return ret;
534 }
535
536 static inline int
537 _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
538                           unsigned long old_freq, unsigned long freq)
539 {
540         int ret;
541
542         ret = clk_set_rate(clk, freq);
543         if (ret) {
544                 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
545                         ret);
546         }
547
548         return ret;
549 }
550
551 static inline int
552 _generic_set_opp_domain(struct device *dev, struct clk *clk,
553                         unsigned long old_freq, unsigned long freq,
554                         unsigned int old_pstate, unsigned int new_pstate)
555 {
556         int ret;
557
558         /* Scaling up? Scale domain performance state before frequency */
559         if (freq > old_freq) {
560                 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
561                 if (ret)
562                         return ret;
563         }
564
565         ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
566         if (ret)
567                 goto restore_domain_state;
568
569         /* Scaling down? Scale domain performance state after frequency */
570         if (freq < old_freq) {
571                 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
572                 if (ret)
573                         goto restore_freq;
574         }
575
576         return 0;
577
578 restore_freq:
579         if (_generic_set_opp_clk_only(dev, clk, freq, old_freq))
580                 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
581                         __func__, old_freq);
582 restore_domain_state:
583         if (freq > old_freq)
584                 dev_pm_genpd_set_performance_state(dev, old_pstate);
585
586         return ret;
587 }
588
589 static int _generic_set_opp_regulator(const struct opp_table *opp_table,
590                                       struct device *dev,
591                                       unsigned long old_freq,
592                                       unsigned long freq,
593                                       struct dev_pm_opp_supply *old_supply,
594                                       struct dev_pm_opp_supply *new_supply)
595 {
596         struct regulator *reg = opp_table->regulators[0];
597         int ret;
598
599         /* This function only supports single regulator per device */
600         if (WARN_ON(opp_table->regulator_count > 1)) {
601                 dev_err(dev, "multiple regulators are not supported\n");
602                 return -EINVAL;
603         }
604
605         /* Scaling up? Scale voltage before frequency */
606         if (freq >= old_freq) {
607                 ret = _set_opp_voltage(dev, reg, new_supply);
608                 if (ret)
609                         goto restore_voltage;
610         }
611
612         /* Change frequency */
613         ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq);
614         if (ret)
615                 goto restore_voltage;
616
617         /* Scaling down? Scale voltage after frequency */
618         if (freq < old_freq) {
619                 ret = _set_opp_voltage(dev, reg, new_supply);
620                 if (ret)
621                         goto restore_freq;
622         }
623
624         return 0;
625
626 restore_freq:
627         if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq))
628                 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
629                         __func__, old_freq);
630 restore_voltage:
631         /* This shouldn't harm even if the voltages weren't updated earlier */
632         if (old_supply)
633                 _set_opp_voltage(dev, reg, old_supply);
634
635         return ret;
636 }
637
638 /**
639  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
640  * @dev:         device for which we do this operation
641  * @target_freq: frequency to achieve
642  *
643  * This configures the power-supplies and clock source to the levels specified
644  * by the OPP corresponding to the target_freq.
645  */
646 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
647 {
648         struct opp_table *opp_table;
649         unsigned long freq, old_freq;
650         struct dev_pm_opp *old_opp, *opp;
651         struct clk *clk;
652         int ret, size;
653
654         if (unlikely(!target_freq)) {
655                 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
656                         target_freq);
657                 return -EINVAL;
658         }
659
660         opp_table = _find_opp_table(dev);
661         if (IS_ERR(opp_table)) {
662                 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
663                 return PTR_ERR(opp_table);
664         }
665
666         clk = opp_table->clk;
667         if (IS_ERR(clk)) {
668                 dev_err(dev, "%s: No clock available for the device\n",
669                         __func__);
670                 ret = PTR_ERR(clk);
671                 goto put_opp_table;
672         }
673
674         freq = clk_round_rate(clk, target_freq);
675         if ((long)freq <= 0)
676                 freq = target_freq;
677
678         old_freq = clk_get_rate(clk);
679
680         /* Return early if nothing to do */
681         if (old_freq == freq) {
682                 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
683                         __func__, freq);
684                 ret = 0;
685                 goto put_opp_table;
686         }
687
688         old_opp = _find_freq_ceil(opp_table, &old_freq);
689         if (IS_ERR(old_opp)) {
690                 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
691                         __func__, old_freq, PTR_ERR(old_opp));
692         }
693
694         opp = _find_freq_ceil(opp_table, &freq);
695         if (IS_ERR(opp)) {
696                 ret = PTR_ERR(opp);
697                 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
698                         __func__, freq, ret);
699                 goto put_old_opp;
700         }
701
702         dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
703                 old_freq, freq);
704
705         /* Only frequency scaling */
706         if (!opp_table->regulators) {
707                 /*
708                  * We don't support devices with both regulator and
709                  * domain performance-state for now.
710                  */
711                 if (opp_table->genpd_performance_state)
712                         ret = _generic_set_opp_domain(dev, clk, old_freq, freq,
713                                                       IS_ERR(old_opp) ? 0 : old_opp->pstate,
714                                                       opp->pstate);
715                 else
716                         ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
717         } else if (!opp_table->set_opp) {
718                 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
719                                                  IS_ERR(old_opp) ? NULL : old_opp->supplies,
720                                                  opp->supplies);
721         } else {
722                 struct dev_pm_set_opp_data *data;
723
724                 data = opp_table->set_opp_data;
725                 data->regulators = opp_table->regulators;
726                 data->regulator_count = opp_table->regulator_count;
727                 data->clk = clk;
728                 data->dev = dev;
729
730                 data->old_opp.rate = old_freq;
731                 size = sizeof(*opp->supplies) * opp_table->regulator_count;
732                 if (IS_ERR(old_opp))
733                         memset(data->old_opp.supplies, 0, size);
734                 else
735                         memcpy(data->old_opp.supplies, old_opp->supplies, size);
736
737                 data->new_opp.rate = freq;
738                 memcpy(data->new_opp.supplies, opp->supplies, size);
739
740                 ret = opp_table->set_opp(data);
741         }
742
743         dev_pm_opp_put(opp);
744 put_old_opp:
745         if (!IS_ERR(old_opp))
746                 dev_pm_opp_put(old_opp);
747 put_opp_table:
748         dev_pm_opp_put_opp_table(opp_table);
749         return ret;
750 }
751 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
752
753 /* OPP-dev Helpers */
754 static void _remove_opp_dev(struct opp_device *opp_dev,
755                             struct opp_table *opp_table)
756 {
757         opp_debug_unregister(opp_dev, opp_table);
758         list_del(&opp_dev->node);
759         kfree(opp_dev);
760 }
761
762 struct opp_device *_add_opp_dev(const struct device *dev,
763                                 struct opp_table *opp_table)
764 {
765         struct opp_device *opp_dev;
766         int ret;
767
768         opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
769         if (!opp_dev)
770                 return NULL;
771
772         /* Initialize opp-dev */
773         opp_dev->dev = dev;
774
775         mutex_lock(&opp_table->lock);
776         list_add(&opp_dev->node, &opp_table->dev_list);
777
778         /* Create debugfs entries for the opp_table */
779         ret = opp_debug_register(opp_dev, opp_table);
780         if (ret)
781                 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
782                         __func__, ret);
783         mutex_unlock(&opp_table->lock);
784
785         return opp_dev;
786 }
787
788 static struct opp_table *_allocate_opp_table(struct device *dev, int index)
789 {
790         struct opp_table *opp_table;
791         struct opp_device *opp_dev;
792         int ret;
793
794         /*
795          * Allocate a new OPP table. In the infrequent case where a new
796          * device is needed to be added, we pay this penalty.
797          */
798         opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
799         if (!opp_table)
800                 return NULL;
801
802         mutex_init(&opp_table->lock);
803         INIT_LIST_HEAD(&opp_table->dev_list);
804
805         opp_dev = _add_opp_dev(dev, opp_table);
806         if (!opp_dev) {
807                 kfree(opp_table);
808                 return NULL;
809         }
810
811         _of_init_opp_table(opp_table, dev, index);
812
813         /* Find clk for the device */
814         opp_table->clk = clk_get(dev, NULL);
815         if (IS_ERR(opp_table->clk)) {
816                 ret = PTR_ERR(opp_table->clk);
817                 if (ret != -EPROBE_DEFER)
818                         dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
819                                 ret);
820         }
821
822         BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
823         INIT_LIST_HEAD(&opp_table->opp_list);
824         kref_init(&opp_table->kref);
825
826         /* Secure the device table modification */
827         list_add(&opp_table->node, &opp_tables);
828         return opp_table;
829 }
830
831 void _get_opp_table_kref(struct opp_table *opp_table)
832 {
833         kref_get(&opp_table->kref);
834 }
835
836 static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
837 {
838         struct opp_table *opp_table;
839
840         /* Hold our table modification lock here */
841         mutex_lock(&opp_table_lock);
842
843         opp_table = _find_opp_table_unlocked(dev);
844         if (!IS_ERR(opp_table))
845                 goto unlock;
846
847         opp_table = _allocate_opp_table(dev, index);
848
849 unlock:
850         mutex_unlock(&opp_table_lock);
851
852         return opp_table;
853 }
854
855 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
856 {
857         return _opp_get_opp_table(dev, 0);
858 }
859 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
860
861 struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
862                                                    int index)
863 {
864         return _opp_get_opp_table(dev, index);
865 }
866
867 static void _opp_table_kref_release(struct kref *kref)
868 {
869         struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
870         struct opp_device *opp_dev;
871
872         /* Release clk */
873         if (!IS_ERR(opp_table->clk))
874                 clk_put(opp_table->clk);
875
876         /*
877          * No need to take opp_table->lock here as we are guaranteed that no
878          * references to the OPP table are taken at this point.
879          */
880         opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
881                                    node);
882
883         _remove_opp_dev(opp_dev, opp_table);
884
885         /* dev_list must be empty now */
886         WARN_ON(!list_empty(&opp_table->dev_list));
887
888         mutex_destroy(&opp_table->lock);
889         list_del(&opp_table->node);
890         kfree(opp_table);
891
892         mutex_unlock(&opp_table_lock);
893 }
894
895 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
896 {
897         kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
898                        &opp_table_lock);
899 }
900 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
901
902 void _opp_free(struct dev_pm_opp *opp)
903 {
904         kfree(opp);
905 }
906
907 static void _opp_kref_release(struct kref *kref)
908 {
909         struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
910         struct opp_table *opp_table = opp->opp_table;
911
912         /*
913          * Notify the changes in the availability of the operable
914          * frequency/voltage list.
915          */
916         blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
917         opp_debug_remove_one(opp);
918         list_del(&opp->node);
919         kfree(opp);
920
921         mutex_unlock(&opp_table->lock);
922         dev_pm_opp_put_opp_table(opp_table);
923 }
924
925 void dev_pm_opp_get(struct dev_pm_opp *opp)
926 {
927         kref_get(&opp->kref);
928 }
929
930 void dev_pm_opp_put(struct dev_pm_opp *opp)
931 {
932         kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
933 }
934 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
935
936 /**
937  * dev_pm_opp_remove()  - Remove an OPP from OPP table
938  * @dev:        device for which we do this operation
939  * @freq:       OPP to remove with matching 'freq'
940  *
941  * This function removes an opp from the opp table.
942  */
943 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
944 {
945         struct dev_pm_opp *opp;
946         struct opp_table *opp_table;
947         bool found = false;
948
949         opp_table = _find_opp_table(dev);
950         if (IS_ERR(opp_table))
951                 return;
952
953         mutex_lock(&opp_table->lock);
954
955         list_for_each_entry(opp, &opp_table->opp_list, node) {
956                 if (opp->rate == freq) {
957                         found = true;
958                         break;
959                 }
960         }
961
962         mutex_unlock(&opp_table->lock);
963
964         if (found) {
965                 dev_pm_opp_put(opp);
966         } else {
967                 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
968                          __func__, freq);
969         }
970
971         dev_pm_opp_put_opp_table(opp_table);
972 }
973 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
974
975 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
976 {
977         struct dev_pm_opp *opp;
978         int count, supply_size;
979
980         /* Allocate space for at least one supply */
981         count = table->regulator_count ? table->regulator_count : 1;
982         supply_size = sizeof(*opp->supplies) * count;
983
984         /* allocate new OPP node and supplies structures */
985         opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
986         if (!opp)
987                 return NULL;
988
989         /* Put the supplies at the end of the OPP structure as an empty array */
990         opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
991         INIT_LIST_HEAD(&opp->node);
992
993         return opp;
994 }
995
996 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
997                                          struct opp_table *opp_table)
998 {
999         struct regulator *reg;
1000         int i;
1001
1002         for (i = 0; i < opp_table->regulator_count; i++) {
1003                 reg = opp_table->regulators[i];
1004
1005                 if (!regulator_is_supported_voltage(reg,
1006                                         opp->supplies[i].u_volt_min,
1007                                         opp->supplies[i].u_volt_max)) {
1008                         pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1009                                 __func__, opp->supplies[i].u_volt_min,
1010                                 opp->supplies[i].u_volt_max);
1011                         return false;
1012                 }
1013         }
1014
1015         return true;
1016 }
1017
1018 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1019                              struct opp_table *opp_table,
1020                              struct list_head **head)
1021 {
1022         struct dev_pm_opp *opp;
1023
1024         /*
1025          * Insert new OPP in order of increasing frequency and discard if
1026          * already present.
1027          *
1028          * Need to use &opp_table->opp_list in the condition part of the 'for'
1029          * loop, don't replace it with head otherwise it will become an infinite
1030          * loop.
1031          */
1032         list_for_each_entry(opp, &opp_table->opp_list, node) {
1033                 if (new_opp->rate > opp->rate) {
1034                         *head = &opp->node;
1035                         continue;
1036                 }
1037
1038                 if (new_opp->rate < opp->rate)
1039                         return 0;
1040
1041                 /* Duplicate OPPs */
1042                 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1043                          __func__, opp->rate, opp->supplies[0].u_volt,
1044                          opp->available, new_opp->rate,
1045                          new_opp->supplies[0].u_volt, new_opp->available);
1046
1047                 /* Should we compare voltages for all regulators here ? */
1048                 return opp->available &&
1049                        new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1050         }
1051
1052         return 0;
1053 }
1054
1055 /*
1056  * Returns:
1057  * 0: On success. And appropriate error message for duplicate OPPs.
1058  * -EBUSY: For OPP with same freq/volt and is available. The callers of
1059  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1060  *  sure we don't print error messages unnecessarily if different parts of
1061  *  kernel try to initialize the OPP table.
1062  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1063  *  should be considered an error by the callers of _opp_add().
1064  */
1065 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1066              struct opp_table *opp_table, bool rate_not_available)
1067 {
1068         struct list_head *head;
1069         int ret;
1070
1071         mutex_lock(&opp_table->lock);
1072         head = &opp_table->opp_list;
1073
1074         if (likely(!rate_not_available)) {
1075                 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1076                 if (ret) {
1077                         mutex_unlock(&opp_table->lock);
1078                         return ret;
1079                 }
1080         }
1081
1082         list_add(&new_opp->node, head);
1083         mutex_unlock(&opp_table->lock);
1084
1085         new_opp->opp_table = opp_table;
1086         kref_init(&new_opp->kref);
1087
1088         /* Get a reference to the OPP table */
1089         _get_opp_table_kref(opp_table);
1090
1091         ret = opp_debug_create_one(new_opp, opp_table);
1092         if (ret)
1093                 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1094                         __func__, ret);
1095
1096         if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1097                 new_opp->available = false;
1098                 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1099                          __func__, new_opp->rate);
1100         }
1101
1102         return 0;
1103 }
1104
1105 /**
1106  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1107  * @opp_table:  OPP table
1108  * @dev:        device for which we do this operation
1109  * @freq:       Frequency in Hz for this OPP
1110  * @u_volt:     Voltage in uVolts for this OPP
1111  * @dynamic:    Dynamically added OPPs.
1112  *
1113  * This function adds an opp definition to the opp table and returns status.
1114  * The opp is made available by default and it can be controlled using
1115  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1116  *
1117  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1118  * and freed by dev_pm_opp_of_remove_table.
1119  *
1120  * Return:
1121  * 0            On success OR
1122  *              Duplicate OPPs (both freq and volt are same) and opp->available
1123  * -EEXIST      Freq are same and volt are different OR
1124  *              Duplicate OPPs (both freq and volt are same) and !opp->available
1125  * -ENOMEM      Memory allocation failure
1126  */
1127 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1128                 unsigned long freq, long u_volt, bool dynamic)
1129 {
1130         struct dev_pm_opp *new_opp;
1131         unsigned long tol;
1132         int ret;
1133
1134         new_opp = _opp_allocate(opp_table);
1135         if (!new_opp)
1136                 return -ENOMEM;
1137
1138         /* populate the opp table */
1139         new_opp->rate = freq;
1140         tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1141         new_opp->supplies[0].u_volt = u_volt;
1142         new_opp->supplies[0].u_volt_min = u_volt - tol;
1143         new_opp->supplies[0].u_volt_max = u_volt + tol;
1144         new_opp->available = true;
1145         new_opp->dynamic = dynamic;
1146
1147         ret = _opp_add(dev, new_opp, opp_table, false);
1148         if (ret) {
1149                 /* Don't return error for duplicate OPPs */
1150                 if (ret == -EBUSY)
1151                         ret = 0;
1152                 goto free_opp;
1153         }
1154
1155         /*
1156          * Notify the changes in the availability of the operable
1157          * frequency/voltage list.
1158          */
1159         blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1160         return 0;
1161
1162 free_opp:
1163         _opp_free(new_opp);
1164
1165         return ret;
1166 }
1167
1168 /**
1169  * dev_pm_opp_set_supported_hw() - Set supported platforms
1170  * @dev: Device for which supported-hw has to be set.
1171  * @versions: Array of hierarchy of versions to match.
1172  * @count: Number of elements in the array.
1173  *
1174  * This is required only for the V2 bindings, and it enables a platform to
1175  * specify the hierarchy of versions it supports. OPP layer will then enable
1176  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1177  * property.
1178  */
1179 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1180                         const u32 *versions, unsigned int count)
1181 {
1182         struct opp_table *opp_table;
1183
1184         opp_table = dev_pm_opp_get_opp_table(dev);
1185         if (!opp_table)
1186                 return ERR_PTR(-ENOMEM);
1187
1188         /* Make sure there are no concurrent readers while updating opp_table */
1189         WARN_ON(!list_empty(&opp_table->opp_list));
1190
1191         /* Another CPU that shares the OPP table has set the property ? */
1192         if (opp_table->supported_hw)
1193                 return opp_table;
1194
1195         opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1196                                         GFP_KERNEL);
1197         if (!opp_table->supported_hw) {
1198                 dev_pm_opp_put_opp_table(opp_table);
1199                 return ERR_PTR(-ENOMEM);
1200         }
1201
1202         opp_table->supported_hw_count = count;
1203
1204         return opp_table;
1205 }
1206 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1207
1208 /**
1209  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1210  * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1211  *
1212  * This is required only for the V2 bindings, and is called for a matching
1213  * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1214  * will not be freed.
1215  */
1216 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1217 {
1218         /* Make sure there are no concurrent readers while updating opp_table */
1219         WARN_ON(!list_empty(&opp_table->opp_list));
1220
1221         kfree(opp_table->supported_hw);
1222         opp_table->supported_hw = NULL;
1223         opp_table->supported_hw_count = 0;
1224
1225         dev_pm_opp_put_opp_table(opp_table);
1226 }
1227 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1228
1229 /**
1230  * dev_pm_opp_set_prop_name() - Set prop-extn name
1231  * @dev: Device for which the prop-name has to be set.
1232  * @name: name to postfix to properties.
1233  *
1234  * This is required only for the V2 bindings, and it enables a platform to
1235  * specify the extn to be used for certain property names. The properties to
1236  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1237  * should postfix the property name with -<name> while looking for them.
1238  */
1239 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1240 {
1241         struct opp_table *opp_table;
1242
1243         opp_table = dev_pm_opp_get_opp_table(dev);
1244         if (!opp_table)
1245                 return ERR_PTR(-ENOMEM);
1246
1247         /* Make sure there are no concurrent readers while updating opp_table */
1248         WARN_ON(!list_empty(&opp_table->opp_list));
1249
1250         /* Another CPU that shares the OPP table has set the property ? */
1251         if (opp_table->prop_name)
1252                 return opp_table;
1253
1254         opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1255         if (!opp_table->prop_name) {
1256                 dev_pm_opp_put_opp_table(opp_table);
1257                 return ERR_PTR(-ENOMEM);
1258         }
1259
1260         return opp_table;
1261 }
1262 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1263
1264 /**
1265  * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1266  * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1267  *
1268  * This is required only for the V2 bindings, and is called for a matching
1269  * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1270  * will not be freed.
1271  */
1272 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1273 {
1274         /* Make sure there are no concurrent readers while updating opp_table */
1275         WARN_ON(!list_empty(&opp_table->opp_list));
1276
1277         kfree(opp_table->prop_name);
1278         opp_table->prop_name = NULL;
1279
1280         dev_pm_opp_put_opp_table(opp_table);
1281 }
1282 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1283
1284 static int _allocate_set_opp_data(struct opp_table *opp_table)
1285 {
1286         struct dev_pm_set_opp_data *data;
1287         int len, count = opp_table->regulator_count;
1288
1289         if (WARN_ON(!count))
1290                 return -EINVAL;
1291
1292         /* space for set_opp_data */
1293         len = sizeof(*data);
1294
1295         /* space for old_opp.supplies and new_opp.supplies */
1296         len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1297
1298         data = kzalloc(len, GFP_KERNEL);
1299         if (!data)
1300                 return -ENOMEM;
1301
1302         data->old_opp.supplies = (void *)(data + 1);
1303         data->new_opp.supplies = data->old_opp.supplies + count;
1304
1305         opp_table->set_opp_data = data;
1306
1307         return 0;
1308 }
1309
1310 static void _free_set_opp_data(struct opp_table *opp_table)
1311 {
1312         kfree(opp_table->set_opp_data);
1313         opp_table->set_opp_data = NULL;
1314 }
1315
1316 /**
1317  * dev_pm_opp_set_regulators() - Set regulator names for the device
1318  * @dev: Device for which regulator name is being set.
1319  * @names: Array of pointers to the names of the regulator.
1320  * @count: Number of regulators.
1321  *
1322  * In order to support OPP switching, OPP layer needs to know the name of the
1323  * device's regulators, as the core would be required to switch voltages as
1324  * well.
1325  *
1326  * This must be called before any OPPs are initialized for the device.
1327  */
1328 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1329                                             const char * const names[],
1330                                             unsigned int count)
1331 {
1332         struct opp_table *opp_table;
1333         struct regulator *reg;
1334         int ret, i;
1335
1336         opp_table = dev_pm_opp_get_opp_table(dev);
1337         if (!opp_table)
1338                 return ERR_PTR(-ENOMEM);
1339
1340         /* This should be called before OPPs are initialized */
1341         if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1342                 ret = -EBUSY;
1343                 goto err;
1344         }
1345
1346         /* Another CPU that shares the OPP table has set the regulators ? */
1347         if (opp_table->regulators)
1348                 return opp_table;
1349
1350         opp_table->regulators = kmalloc_array(count,
1351                                               sizeof(*opp_table->regulators),
1352                                               GFP_KERNEL);
1353         if (!opp_table->regulators) {
1354                 ret = -ENOMEM;
1355                 goto err;
1356         }
1357
1358         for (i = 0; i < count; i++) {
1359                 reg = regulator_get_optional(dev, names[i]);
1360                 if (IS_ERR(reg)) {
1361                         ret = PTR_ERR(reg);
1362                         if (ret != -EPROBE_DEFER)
1363                                 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1364                                         __func__, names[i], ret);
1365                         goto free_regulators;
1366                 }
1367
1368                 opp_table->regulators[i] = reg;
1369         }
1370
1371         opp_table->regulator_count = count;
1372
1373         /* Allocate block only once to pass to set_opp() routines */
1374         ret = _allocate_set_opp_data(opp_table);
1375         if (ret)
1376                 goto free_regulators;
1377
1378         return opp_table;
1379
1380 free_regulators:
1381         while (i != 0)
1382                 regulator_put(opp_table->regulators[--i]);
1383
1384         kfree(opp_table->regulators);
1385         opp_table->regulators = NULL;
1386         opp_table->regulator_count = 0;
1387 err:
1388         dev_pm_opp_put_opp_table(opp_table);
1389
1390         return ERR_PTR(ret);
1391 }
1392 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
1393
1394 /**
1395  * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1396  * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1397  */
1398 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
1399 {
1400         int i;
1401
1402         if (!opp_table->regulators)
1403                 goto put_opp_table;
1404
1405         /* Make sure there are no concurrent readers while updating opp_table */
1406         WARN_ON(!list_empty(&opp_table->opp_list));
1407
1408         for (i = opp_table->regulator_count - 1; i >= 0; i--)
1409                 regulator_put(opp_table->regulators[i]);
1410
1411         _free_set_opp_data(opp_table);
1412
1413         kfree(opp_table->regulators);
1414         opp_table->regulators = NULL;
1415         opp_table->regulator_count = 0;
1416
1417 put_opp_table:
1418         dev_pm_opp_put_opp_table(opp_table);
1419 }
1420 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
1421
1422 /**
1423  * dev_pm_opp_set_clkname() - Set clk name for the device
1424  * @dev: Device for which clk name is being set.
1425  * @name: Clk name.
1426  *
1427  * In order to support OPP switching, OPP layer needs to get pointer to the
1428  * clock for the device. Simple cases work fine without using this routine (i.e.
1429  * by passing connection-id as NULL), but for a device with multiple clocks
1430  * available, the OPP core needs to know the exact name of the clk to use.
1431  *
1432  * This must be called before any OPPs are initialized for the device.
1433  */
1434 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1435 {
1436         struct opp_table *opp_table;
1437         int ret;
1438
1439         opp_table = dev_pm_opp_get_opp_table(dev);
1440         if (!opp_table)
1441                 return ERR_PTR(-ENOMEM);
1442
1443         /* This should be called before OPPs are initialized */
1444         if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1445                 ret = -EBUSY;
1446                 goto err;
1447         }
1448
1449         /* Already have default clk set, free it */
1450         if (!IS_ERR(opp_table->clk))
1451                 clk_put(opp_table->clk);
1452
1453         /* Find clk for the device */
1454         opp_table->clk = clk_get(dev, name);
1455         if (IS_ERR(opp_table->clk)) {
1456                 ret = PTR_ERR(opp_table->clk);
1457                 if (ret != -EPROBE_DEFER) {
1458                         dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1459                                 ret);
1460                 }
1461                 goto err;
1462         }
1463
1464         return opp_table;
1465
1466 err:
1467         dev_pm_opp_put_opp_table(opp_table);
1468
1469         return ERR_PTR(ret);
1470 }
1471 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1472
1473 /**
1474  * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1475  * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1476  */
1477 void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1478 {
1479         /* Make sure there are no concurrent readers while updating opp_table */
1480         WARN_ON(!list_empty(&opp_table->opp_list));
1481
1482         clk_put(opp_table->clk);
1483         opp_table->clk = ERR_PTR(-EINVAL);
1484
1485         dev_pm_opp_put_opp_table(opp_table);
1486 }
1487 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1488
1489 /**
1490  * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1491  * @dev: Device for which the helper is getting registered.
1492  * @set_opp: Custom set OPP helper.
1493  *
1494  * This is useful to support complex platforms (like platforms with multiple
1495  * regulators per device), instead of the generic OPP set rate helper.
1496  *
1497  * This must be called before any OPPs are initialized for the device.
1498  */
1499 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
1500                         int (*set_opp)(struct dev_pm_set_opp_data *data))
1501 {
1502         struct opp_table *opp_table;
1503
1504         if (!set_opp)
1505                 return ERR_PTR(-EINVAL);
1506
1507         opp_table = dev_pm_opp_get_opp_table(dev);
1508         if (!opp_table)
1509                 return ERR_PTR(-ENOMEM);
1510
1511         /* This should be called before OPPs are initialized */
1512         if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1513                 dev_pm_opp_put_opp_table(opp_table);
1514                 return ERR_PTR(-EBUSY);
1515         }
1516
1517         /* Another CPU that shares the OPP table has set the helper ? */
1518         if (!opp_table->set_opp)
1519                 opp_table->set_opp = set_opp;
1520
1521         return opp_table;
1522 }
1523 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1524
1525 /**
1526  * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
1527  *                                         set_opp helper
1528  * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
1529  *
1530  * Release resources blocked for platform specific set_opp helper.
1531  */
1532 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
1533 {
1534         /* Make sure there are no concurrent readers while updating opp_table */
1535         WARN_ON(!list_empty(&opp_table->opp_list));
1536
1537         opp_table->set_opp = NULL;
1538         dev_pm_opp_put_opp_table(opp_table);
1539 }
1540 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
1541
1542 /**
1543  * dev_pm_opp_add()  - Add an OPP table from a table definitions
1544  * @dev:        device for which we do this operation
1545  * @freq:       Frequency in Hz for this OPP
1546  * @u_volt:     Voltage in uVolts for this OPP
1547  *
1548  * This function adds an opp definition to the opp table and returns status.
1549  * The opp is made available by default and it can be controlled using
1550  * dev_pm_opp_enable/disable functions.
1551  *
1552  * Return:
1553  * 0            On success OR
1554  *              Duplicate OPPs (both freq and volt are same) and opp->available
1555  * -EEXIST      Freq are same and volt are different OR
1556  *              Duplicate OPPs (both freq and volt are same) and !opp->available
1557  * -ENOMEM      Memory allocation failure
1558  */
1559 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1560 {
1561         struct opp_table *opp_table;
1562         int ret;
1563
1564         opp_table = dev_pm_opp_get_opp_table(dev);
1565         if (!opp_table)
1566                 return -ENOMEM;
1567
1568         ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1569
1570         dev_pm_opp_put_opp_table(opp_table);
1571         return ret;
1572 }
1573 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1574
1575 /**
1576  * _opp_set_availability() - helper to set the availability of an opp
1577  * @dev:                device for which we do this operation
1578  * @freq:               OPP frequency to modify availability
1579  * @availability_req:   availability status requested for this opp
1580  *
1581  * Set the availability of an OPP, opp_{enable,disable} share a common logic
1582  * which is isolated here.
1583  *
1584  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1585  * copy operation, returns 0 if no modification was done OR modification was
1586  * successful.
1587  */
1588 static int _opp_set_availability(struct device *dev, unsigned long freq,
1589                                  bool availability_req)
1590 {
1591         struct opp_table *opp_table;
1592         struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
1593         int r = 0;
1594
1595         /* Find the opp_table */
1596         opp_table = _find_opp_table(dev);
1597         if (IS_ERR(opp_table)) {
1598                 r = PTR_ERR(opp_table);
1599                 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1600                 return r;
1601         }
1602
1603         mutex_lock(&opp_table->lock);
1604
1605         /* Do we have the frequency? */
1606         list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1607                 if (tmp_opp->rate == freq) {
1608                         opp = tmp_opp;
1609                         break;
1610                 }
1611         }
1612
1613         if (IS_ERR(opp)) {
1614                 r = PTR_ERR(opp);
1615                 goto unlock;
1616         }
1617
1618         /* Is update really needed? */
1619         if (opp->available == availability_req)
1620                 goto unlock;
1621
1622         opp->available = availability_req;
1623
1624         dev_pm_opp_get(opp);
1625         mutex_unlock(&opp_table->lock);
1626
1627         /* Notify the change of the OPP availability */
1628         if (availability_req)
1629                 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
1630                                              opp);
1631         else
1632                 blocking_notifier_call_chain(&opp_table->head,
1633                                              OPP_EVENT_DISABLE, opp);
1634
1635         dev_pm_opp_put(opp);
1636         goto put_table;
1637
1638 unlock:
1639         mutex_unlock(&opp_table->lock);
1640 put_table:
1641         dev_pm_opp_put_opp_table(opp_table);
1642         return r;
1643 }
1644
1645 /**
1646  * dev_pm_opp_enable() - Enable a specific OPP
1647  * @dev:        device for which we do this operation
1648  * @freq:       OPP frequency to enable
1649  *
1650  * Enables a provided opp. If the operation is valid, this returns 0, else the
1651  * corresponding error value. It is meant to be used for users an OPP available
1652  * after being temporarily made unavailable with dev_pm_opp_disable.
1653  *
1654  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1655  * copy operation, returns 0 if no modification was done OR modification was
1656  * successful.
1657  */
1658 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1659 {
1660         return _opp_set_availability(dev, freq, true);
1661 }
1662 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1663
1664 /**
1665  * dev_pm_opp_disable() - Disable a specific OPP
1666  * @dev:        device for which we do this operation
1667  * @freq:       OPP frequency to disable
1668  *
1669  * Disables a provided opp. If the operation is valid, this returns
1670  * 0, else the corresponding error value. It is meant to be a temporary
1671  * control by users to make this OPP not available until the circumstances are
1672  * right to make it available again (with a call to dev_pm_opp_enable).
1673  *
1674  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1675  * copy operation, returns 0 if no modification was done OR modification was
1676  * successful.
1677  */
1678 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1679 {
1680         return _opp_set_availability(dev, freq, false);
1681 }
1682 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1683
1684 /**
1685  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1686  * @dev:        Device for which notifier needs to be registered
1687  * @nb:         Notifier block to be registered
1688  *
1689  * Return: 0 on success or a negative error value.
1690  */
1691 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
1692 {
1693         struct opp_table *opp_table;
1694         int ret;
1695
1696         opp_table = _find_opp_table(dev);
1697         if (IS_ERR(opp_table))
1698                 return PTR_ERR(opp_table);
1699
1700         ret = blocking_notifier_chain_register(&opp_table->head, nb);
1701
1702         dev_pm_opp_put_opp_table(opp_table);
1703
1704         return ret;
1705 }
1706 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1707
1708 /**
1709  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1710  * @dev:        Device for which notifier needs to be unregistered
1711  * @nb:         Notifier block to be unregistered
1712  *
1713  * Return: 0 on success or a negative error value.
1714  */
1715 int dev_pm_opp_unregister_notifier(struct device *dev,
1716                                    struct notifier_block *nb)
1717 {
1718         struct opp_table *opp_table;
1719         int ret;
1720
1721         opp_table = _find_opp_table(dev);
1722         if (IS_ERR(opp_table))
1723                 return PTR_ERR(opp_table);
1724
1725         ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
1726
1727         dev_pm_opp_put_opp_table(opp_table);
1728
1729         return ret;
1730 }
1731 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
1732
1733 /*
1734  * Free OPPs either created using static entries present in DT or even the
1735  * dynamically added entries based on remove_all param.
1736  */
1737 void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1738                               bool remove_all)
1739 {
1740         struct dev_pm_opp *opp, *tmp;
1741
1742         /* Protect dev_list */
1743         mutex_lock(&opp_table->lock);
1744
1745         /* Find if opp_table manages a single device */
1746         if (list_is_singular(&opp_table->dev_list)) {
1747                 /* Free static OPPs */
1748                 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1749                         if (remove_all || !opp->dynamic)
1750                                 dev_pm_opp_put(opp);
1751                 }
1752
1753                 /*
1754                  * The OPP table is getting removed, drop the performance state
1755                  * constraints.
1756                  */
1757                 if (opp_table->genpd_performance_state)
1758                         dev_pm_genpd_set_performance_state(dev, 0);
1759         } else {
1760                 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1761         }
1762
1763         mutex_unlock(&opp_table->lock);
1764 }
1765
1766 void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
1767 {
1768         struct opp_table *opp_table;
1769
1770         /* Check for existing table for 'dev' */
1771         opp_table = _find_opp_table(dev);
1772         if (IS_ERR(opp_table)) {
1773                 int error = PTR_ERR(opp_table);
1774
1775                 if (error != -ENODEV)
1776                         WARN(1, "%s: opp_table: %d\n",
1777                              IS_ERR_OR_NULL(dev) ?
1778                                         "Invalid device" : dev_name(dev),
1779                              error);
1780                 return;
1781         }
1782
1783         _dev_pm_opp_remove_table(opp_table, dev, remove_all);
1784
1785         dev_pm_opp_put_opp_table(opp_table);
1786 }
1787
1788 /**
1789  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
1790  * @dev:        device pointer used to lookup OPP table.
1791  *
1792  * Free both OPPs created using static entries present in DT and the
1793  * dynamically added entries.
1794  */
1795 void dev_pm_opp_remove_table(struct device *dev)
1796 {
1797         _dev_pm_opp_find_and_remove_table(dev, true);
1798 }
1799 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);