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