cd17cd9eb50de5fa0640c1732da1332411a38273
[firefly-linux-kernel-4.4.55.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/of.h>
22 #include <linux/export.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 static LIST_HEAD(opp_tables);
33 /* Lock to allow exclusive modification to the device and opp lists */
34 DEFINE_MUTEX(opp_table_lock);
35
36 #define opp_rcu_lockdep_assert()                                        \
37 do {                                                                    \
38         RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&                       \
39                          !lockdep_is_held(&opp_table_lock),             \
40                          "Missing rcu_read_lock() or "                  \
41                          "opp_table_lock protection");                  \
42 } while (0)
43
44 static struct opp_device *_find_opp_dev(const struct device *dev,
45                                         struct opp_table *opp_table)
46 {
47         struct opp_device *opp_dev;
48
49         list_for_each_entry(opp_dev, &opp_table->dev_list, node)
50                 if (opp_dev->dev == dev)
51                         return opp_dev;
52
53         return NULL;
54 }
55
56 static struct opp_table *_managed_opp(const struct device_node *np)
57 {
58         struct opp_table *opp_table;
59
60         list_for_each_entry_rcu(opp_table, &opp_tables, node) {
61                 if (opp_table->np == np) {
62                         /*
63                          * Multiple devices can point to the same OPP table and
64                          * so will have same node-pointer, np.
65                          *
66                          * But the OPPs will be considered as shared only if the
67                          * OPP table contains a "opp-shared" property.
68                          */
69                         return opp_table->shared_opp ? opp_table : NULL;
70                 }
71         }
72
73         return NULL;
74 }
75
76 /**
77  * _find_opp_table() - find opp_table struct using device pointer
78  * @dev:        device pointer used to lookup OPP table
79  *
80  * Search OPP table for one containing matching device. Does a RCU reader
81  * operation to grab the pointer needed.
82  *
83  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
84  * -EINVAL based on type of error.
85  *
86  * Locking: For readers, this function must be called under rcu_read_lock().
87  * opp_table is a RCU protected pointer, which means that opp_table is valid
88  * as long as we are under RCU lock.
89  *
90  * For Writers, this function must be called with opp_table_lock held.
91  */
92 struct opp_table *_find_opp_table(struct device *dev)
93 {
94         struct opp_table *opp_table;
95
96         opp_rcu_lockdep_assert();
97
98         if (IS_ERR_OR_NULL(dev)) {
99                 pr_err("%s: Invalid parameters\n", __func__);
100                 return ERR_PTR(-EINVAL);
101         }
102
103         list_for_each_entry_rcu(opp_table, &opp_tables, node)
104                 if (_find_opp_dev(dev, opp_table))
105                         return opp_table;
106
107         return ERR_PTR(-ENODEV);
108 }
109
110 /**
111  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
112  * @opp:        opp for which voltage has to be returned for
113  *
114  * Return: voltage in micro volt corresponding to the opp, else
115  * return 0
116  *
117  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
118  * protected pointer. This means that opp which could have been fetched by
119  * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
120  * under RCU lock. The pointer returned by the opp_find_freq family must be
121  * used in the same section as the usage of this function with the pointer
122  * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
123  * pointer.
124  */
125 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
126 {
127         struct dev_pm_opp *tmp_opp;
128         unsigned long v = 0;
129
130         opp_rcu_lockdep_assert();
131
132         tmp_opp = rcu_dereference(opp);
133         if (IS_ERR_OR_NULL(tmp_opp))
134                 pr_err("%s: Invalid parameters\n", __func__);
135         else
136                 v = tmp_opp->u_volt;
137
138         return v;
139 }
140 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
141
142 /**
143  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
144  * @opp:        opp for which frequency has to be returned for
145  *
146  * Return: frequency in hertz corresponding to the opp, else
147  * return 0
148  *
149  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
150  * protected pointer. This means that opp which could have been fetched by
151  * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
152  * under RCU lock. The pointer returned by the opp_find_freq family must be
153  * used in the same section as the usage of this function with the pointer
154  * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
155  * pointer.
156  */
157 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
158 {
159         struct dev_pm_opp *tmp_opp;
160         unsigned long f = 0;
161
162         opp_rcu_lockdep_assert();
163
164         tmp_opp = rcu_dereference(opp);
165         if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
166                 pr_err("%s: Invalid parameters\n", __func__);
167         else
168                 f = tmp_opp->rate;
169
170         return f;
171 }
172 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
173
174 /**
175  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
176  * @opp: opp for which turbo mode is being verified
177  *
178  * Turbo OPPs are not for normal use, and can be enabled (under certain
179  * conditions) for short duration of times to finish high throughput work
180  * quickly. Running on them for longer times may overheat the chip.
181  *
182  * Return: true if opp is turbo opp, else false.
183  *
184  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
185  * protected pointer. This means that opp which could have been fetched by
186  * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
187  * under RCU lock. The pointer returned by the opp_find_freq family must be
188  * used in the same section as the usage of this function with the pointer
189  * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
190  * pointer.
191  */
192 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
193 {
194         struct dev_pm_opp *tmp_opp;
195
196         opp_rcu_lockdep_assert();
197
198         tmp_opp = rcu_dereference(opp);
199         if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
200                 pr_err("%s: Invalid parameters\n", __func__);
201                 return false;
202         }
203
204         return tmp_opp->turbo;
205 }
206 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
207
208 /**
209  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
210  * @dev:        device for which we do this operation
211  *
212  * Return: This function returns the max clock latency in nanoseconds.
213  *
214  * Locking: This function takes rcu_read_lock().
215  */
216 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
217 {
218         struct opp_table *opp_table;
219         unsigned long clock_latency_ns;
220
221         rcu_read_lock();
222
223         opp_table = _find_opp_table(dev);
224         if (IS_ERR(opp_table))
225                 clock_latency_ns = 0;
226         else
227                 clock_latency_ns = opp_table->clock_latency_ns_max;
228
229         rcu_read_unlock();
230         return clock_latency_ns;
231 }
232 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
233
234 /**
235  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
236  * @dev: device for which we do this operation
237  *
238  * Return: This function returns the max voltage latency in nanoseconds.
239  *
240  * Locking: This function takes rcu_read_lock().
241  */
242 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
243 {
244         struct opp_table *opp_table;
245         struct dev_pm_opp *opp;
246         struct regulator *reg;
247         unsigned long latency_ns = 0;
248         unsigned long min_uV = ~0, max_uV = 0;
249         int ret;
250
251         rcu_read_lock();
252
253         opp_table = _find_opp_table(dev);
254         if (IS_ERR(opp_table)) {
255                 rcu_read_unlock();
256                 return 0;
257         }
258
259         reg = opp_table->regulator;
260         if (IS_ERR(reg)) {
261                 /* Regulator may not be required for device */
262                 if (reg)
263                         dev_err(dev, "%s: Invalid regulator (%ld)\n", __func__,
264                                 PTR_ERR(reg));
265                 rcu_read_unlock();
266                 return 0;
267         }
268
269         list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
270                 if (!opp->available)
271                         continue;
272
273                 if (opp->u_volt_min < min_uV)
274                         min_uV = opp->u_volt_min;
275                 if (opp->u_volt_max > max_uV)
276                         max_uV = opp->u_volt_max;
277         }
278
279         rcu_read_unlock();
280
281         /*
282          * The caller needs to ensure that opp_table (and hence the regulator)
283          * isn't freed, while we are executing this routine.
284          */
285         ret = regulator_set_voltage_time(reg, min_uV, max_uV);
286         if (ret > 0)
287                 latency_ns = ret * 1000;
288
289         return latency_ns;
290 }
291 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
292
293 /**
294  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
295  *                                           nanoseconds
296  * @dev: device for which we do this operation
297  *
298  * Return: This function returns the max transition latency, in nanoseconds, to
299  * switch from one OPP to other.
300  *
301  * Locking: This function takes rcu_read_lock().
302  */
303 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
304 {
305         return dev_pm_opp_get_max_volt_latency(dev) +
306                 dev_pm_opp_get_max_clock_latency(dev);
307 }
308 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
309
310 /**
311  * dev_pm_opp_get_suspend_opp() - Get suspend opp
312  * @dev:        device for which we do this operation
313  *
314  * Return: This function returns pointer to the suspend opp if it is
315  * defined and available, otherwise it returns NULL.
316  *
317  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
318  * protected pointer. The reason for the same is that the opp pointer which is
319  * returned will remain valid for use with opp_get_{voltage, freq} only while
320  * under the locked area. The pointer returned must be used prior to unlocking
321  * with rcu_read_unlock() to maintain the integrity of the pointer.
322  */
323 struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
324 {
325         struct opp_table *opp_table;
326
327         opp_rcu_lockdep_assert();
328
329         opp_table = _find_opp_table(dev);
330         if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
331             !opp_table->suspend_opp->available)
332                 return NULL;
333
334         return opp_table->suspend_opp;
335 }
336 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
337
338 /**
339  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
340  * @dev:        device for which we do this operation
341  *
342  * Return: This function returns the number of available opps if there are any,
343  * else returns 0 if none or the corresponding error value.
344  *
345  * Locking: This function takes rcu_read_lock().
346  */
347 int dev_pm_opp_get_opp_count(struct device *dev)
348 {
349         struct opp_table *opp_table;
350         struct dev_pm_opp *temp_opp;
351         int count = 0;
352
353         rcu_read_lock();
354
355         opp_table = _find_opp_table(dev);
356         if (IS_ERR(opp_table)) {
357                 count = PTR_ERR(opp_table);
358                 dev_err(dev, "%s: OPP table not found (%d)\n",
359                         __func__, count);
360                 goto out_unlock;
361         }
362
363         list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
364                 if (temp_opp->available)
365                         count++;
366         }
367
368 out_unlock:
369         rcu_read_unlock();
370         return count;
371 }
372 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
373
374 /**
375  * dev_pm_opp_find_freq_exact() - search for an exact frequency
376  * @dev:                device for which we do this operation
377  * @freq:               frequency to search for
378  * @available:          true/false - match for available opp
379  *
380  * Return: Searches for exact match in the opp table and returns pointer to the
381  * matching opp if found, else returns ERR_PTR in case of error and should
382  * be handled using IS_ERR. Error return values can be:
383  * EINVAL:      for bad pointer
384  * ERANGE:      no match found for search
385  * ENODEV:      if device not found in list of registered devices
386  *
387  * Note: available is a modifier for the search. if available=true, then the
388  * match is for exact matching frequency and is available in the stored OPP
389  * table. if false, the match is for exact frequency which is not available.
390  *
391  * This provides a mechanism to enable an opp which is not available currently
392  * or the opposite as well.
393  *
394  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
395  * protected pointer. The reason for the same is that the opp pointer which is
396  * returned will remain valid for use with opp_get_{voltage, freq} only while
397  * under the locked area. The pointer returned must be used prior to unlocking
398  * with rcu_read_unlock() to maintain the integrity of the pointer.
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         opp_rcu_lockdep_assert();
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                 return ERR_PTR(r);
415         }
416
417         list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
418                 if (temp_opp->available == available &&
419                                 temp_opp->rate == freq) {
420                         opp = temp_opp;
421                         break;
422                 }
423         }
424
425         return opp;
426 }
427 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
428
429 /**
430  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
431  * @dev:        device for which we do this operation
432  * @freq:       Start frequency
433  *
434  * Search for the matching ceil *available* OPP from a starting freq
435  * for a device.
436  *
437  * Return: matching *opp and refreshes *freq accordingly, else returns
438  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
439  * values can be:
440  * EINVAL:      for bad pointer
441  * ERANGE:      no match found for search
442  * ENODEV:      if device not found in list of registered devices
443  *
444  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
445  * protected pointer. The reason for the same is that the opp pointer which is
446  * returned will remain valid for use with opp_get_{voltage, freq} only while
447  * under the locked area. The pointer returned must be used prior to unlocking
448  * with rcu_read_unlock() to maintain the integrity of the pointer.
449  */
450 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
451                                              unsigned long *freq)
452 {
453         struct opp_table *opp_table;
454         struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
455
456         opp_rcu_lockdep_assert();
457
458         if (!dev || !freq) {
459                 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
460                 return ERR_PTR(-EINVAL);
461         }
462
463         opp_table = _find_opp_table(dev);
464         if (IS_ERR(opp_table))
465                 return ERR_CAST(opp_table);
466
467         list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
468                 if (temp_opp->available && temp_opp->rate >= *freq) {
469                         opp = temp_opp;
470                         *freq = opp->rate;
471                         break;
472                 }
473         }
474
475         return opp;
476 }
477 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
478
479 /**
480  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
481  * @dev:        device for which we do this operation
482  * @freq:       Start frequency
483  *
484  * Search for the matching floor *available* OPP from a starting freq
485  * for a device.
486  *
487  * Return: matching *opp and refreshes *freq accordingly, else returns
488  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
489  * values can be:
490  * EINVAL:      for bad pointer
491  * ERANGE:      no match found for search
492  * ENODEV:      if device not found in list of registered devices
493  *
494  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
495  * protected pointer. The reason for the same is that the opp pointer which is
496  * returned will remain valid for use with opp_get_{voltage, freq} only while
497  * under the locked area. The pointer returned must be used prior to unlocking
498  * with rcu_read_unlock() to maintain the integrity of the pointer.
499  */
500 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
501                                               unsigned long *freq)
502 {
503         struct opp_table *opp_table;
504         struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
505
506         opp_rcu_lockdep_assert();
507
508         if (!dev || !freq) {
509                 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
510                 return ERR_PTR(-EINVAL);
511         }
512
513         opp_table = _find_opp_table(dev);
514         if (IS_ERR(opp_table))
515                 return ERR_CAST(opp_table);
516
517         list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
518                 if (temp_opp->available) {
519                         /* go to the next node, before choosing prev */
520                         if (temp_opp->rate > *freq)
521                                 break;
522                         else
523                                 opp = temp_opp;
524                 }
525         }
526         if (!IS_ERR(opp))
527                 *freq = opp->rate;
528
529         return opp;
530 }
531 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
532
533 /*
534  * The caller needs to ensure that opp_table (and hence the clk) isn't freed,
535  * while clk returned here is used.
536  */
537 static struct clk *_get_opp_clk(struct device *dev)
538 {
539         struct opp_table *opp_table;
540         struct clk *clk;
541
542         rcu_read_lock();
543
544         opp_table = _find_opp_table(dev);
545         if (IS_ERR(opp_table)) {
546                 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
547                 clk = ERR_CAST(opp_table);
548                 goto unlock;
549         }
550
551         clk = opp_table->clk;
552         if (IS_ERR(clk))
553                 dev_err(dev, "%s: No clock available for the device\n",
554                         __func__);
555
556 unlock:
557         rcu_read_unlock();
558         return clk;
559 }
560
561 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
562                             unsigned long u_volt, unsigned long u_volt_min,
563                             unsigned long u_volt_max)
564 {
565         int ret;
566
567         /* Regulator not available for device */
568         if (IS_ERR(reg)) {
569                 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
570                         PTR_ERR(reg));
571                 return 0;
572         }
573
574         dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, u_volt_min,
575                 u_volt, u_volt_max);
576
577         ret = regulator_set_voltage_triplet(reg, u_volt_min, u_volt,
578                                             u_volt_max);
579         if (ret)
580                 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
581                         __func__, u_volt_min, u_volt, u_volt_max, ret);
582
583         return ret;
584 }
585
586 /**
587  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
588  * @dev:         device for which we do this operation
589  * @target_freq: frequency to achieve
590  *
591  * This configures the power-supplies and clock source to the levels specified
592  * by the OPP corresponding to the target_freq.
593  *
594  * Locking: This function takes rcu_read_lock().
595  */
596 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
597 {
598         struct opp_table *opp_table;
599         struct dev_pm_opp *old_opp, *opp;
600         struct regulator *reg;
601         struct clk *clk;
602         unsigned long freq, old_freq;
603         unsigned long u_volt, u_volt_min, u_volt_max;
604         unsigned long ou_volt, ou_volt_min, ou_volt_max;
605         int ret;
606
607         if (unlikely(!target_freq)) {
608                 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
609                         target_freq);
610                 return -EINVAL;
611         }
612
613         clk = _get_opp_clk(dev);
614         if (IS_ERR(clk))
615                 return PTR_ERR(clk);
616
617         freq = clk_round_rate(clk, target_freq);
618         if ((long)freq <= 0)
619                 freq = target_freq;
620
621         old_freq = clk_get_rate(clk);
622
623         /* Return early if nothing to do */
624         if (old_freq == freq) {
625                 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
626                         __func__, freq);
627                 return 0;
628         }
629
630         rcu_read_lock();
631
632         opp_table = _find_opp_table(dev);
633         if (IS_ERR(opp_table)) {
634                 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
635                 rcu_read_unlock();
636                 return PTR_ERR(opp_table);
637         }
638
639         old_opp = dev_pm_opp_find_freq_ceil(dev, &old_freq);
640         if (!IS_ERR(old_opp)) {
641                 ou_volt = old_opp->u_volt;
642                 ou_volt_min = old_opp->u_volt_min;
643                 ou_volt_max = old_opp->u_volt_max;
644         } else {
645                 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
646                         __func__, old_freq, PTR_ERR(old_opp));
647         }
648
649         opp = dev_pm_opp_find_freq_ceil(dev, &freq);
650         if (IS_ERR(opp)) {
651                 ret = PTR_ERR(opp);
652                 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
653                         __func__, freq, ret);
654                 rcu_read_unlock();
655                 return ret;
656         }
657
658         u_volt = opp->u_volt;
659         u_volt_min = opp->u_volt_min;
660         u_volt_max = opp->u_volt_max;
661
662         reg = opp_table->regulator;
663
664         rcu_read_unlock();
665
666         /* Scaling up? Scale voltage before frequency */
667         if (freq > old_freq) {
668                 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
669                                        u_volt_max);
670                 if (ret)
671                         goto restore_voltage;
672         }
673
674         /* Change frequency */
675
676         dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n",
677                 __func__, old_freq, freq);
678
679         ret = clk_set_rate(clk, freq);
680         if (ret) {
681                 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
682                         ret);
683                 goto restore_voltage;
684         }
685
686         /* Scaling down? Scale voltage after frequency */
687         if (freq < old_freq) {
688                 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
689                                        u_volt_max);
690                 if (ret)
691                         goto restore_freq;
692         }
693
694         return 0;
695
696 restore_freq:
697         if (clk_set_rate(clk, old_freq))
698                 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
699                         __func__, old_freq);
700 restore_voltage:
701         /* This shouldn't harm even if the voltages weren't updated earlier */
702         if (!IS_ERR(old_opp))
703                 _set_opp_voltage(dev, reg, ou_volt, ou_volt_min, ou_volt_max);
704
705         return ret;
706 }
707 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
708
709 /**
710  * dev_pm_opp_check_initial_rate() - Configure new OPP based on initial rate
711  * @dev:         device for which we do this operation
712  *
713  * This configures the power-supplies and clock source to the levels specified
714  * by the OPP corresponding to the system initial rate.
715  *
716  * Locking: This function takes rcu_read_lock().
717  */
718 int dev_pm_opp_check_initial_rate(struct device *dev, unsigned long *cur_freq)
719 {
720         struct opp_table *opp_table;
721         struct dev_pm_opp *opp;
722         struct regulator *reg;
723         struct clk *clk;
724         unsigned long target_freq, old_freq;
725         unsigned long u_volt, u_volt_min, u_volt_max;
726         int old_volt;
727         int ret;
728
729         rcu_read_lock();
730
731         opp_table = _find_opp_table(dev);
732         if (IS_ERR(opp_table)) {
733                 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
734                 rcu_read_unlock();
735                 return PTR_ERR(opp_table);
736         }
737
738         clk = opp_table->clk;
739         reg = opp_table->regulator;
740
741         old_freq = clk_get_rate(clk);
742         *cur_freq = old_freq;
743         target_freq = old_freq;
744
745         opp = dev_pm_opp_find_freq_ceil(dev, &target_freq);
746         if (IS_ERR(opp)) {
747                 opp = dev_pm_opp_find_freq_floor(dev, &target_freq);
748                 if (IS_ERR(opp)) {
749                         dev_err(dev, "failed to find OPP for freq %lu\n",
750                                 target_freq);
751                         rcu_read_unlock();
752                         return PTR_ERR(opp);
753                 }
754         }
755
756         u_volt = opp->u_volt;
757         u_volt_min = opp->u_volt_min;
758         u_volt_max = opp->u_volt_max;
759
760         rcu_read_unlock();
761
762         target_freq = clk_round_rate(clk, target_freq);
763         old_volt = regulator_get_voltage(reg);
764
765         dev_dbg(dev, "%lu Hz %d uV --> %lu Hz %lu uV\n", old_freq, old_volt,
766                 target_freq, u_volt);
767
768         if (old_freq == target_freq && old_volt == u_volt)
769                 return 0;
770
771         if (old_freq == target_freq && old_volt != u_volt) {
772                 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
773                                        u_volt_max);
774                 if (ret) {
775                         dev_err(dev, "failed to set volt %lu\n", u_volt);
776                         return ret;
777                 }
778                 return 0;
779         }
780
781         /* Scaling up? Scale voltage before frequency */
782         if (target_freq > old_freq) {
783                 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
784                                        u_volt_max);
785                 if (ret) {
786                         dev_err(dev, "failed to set volt %lu\n", u_volt);
787                         return ret;
788                 }
789         }
790
791         /* Change frequency */
792         ret = clk_set_rate(clk, target_freq);
793         if (ret) {
794                 dev_err(dev, "failed to set clock rate %lu\n", target_freq);
795                 return ret;
796         }
797
798         *cur_freq = clk_get_rate(clk);
799
800         /* Scaling down? Scale voltage after frequency */
801         if (target_freq < old_freq) {
802                 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
803                                        u_volt_max);
804                 if (ret) {
805                         dev_err(dev, "failed to set volt %lu\n", u_volt);
806                         return ret;
807                 }
808         }
809
810         return 0;
811 }
812 EXPORT_SYMBOL_GPL(dev_pm_opp_check_initial_rate);
813
814 /* OPP-dev Helpers */
815 static void _kfree_opp_dev_rcu(struct rcu_head *head)
816 {
817         struct opp_device *opp_dev;
818
819         opp_dev = container_of(head, struct opp_device, rcu_head);
820         kfree_rcu(opp_dev, rcu_head);
821 }
822
823 static void _remove_opp_dev(struct opp_device *opp_dev,
824                             struct opp_table *opp_table)
825 {
826         opp_debug_unregister(opp_dev, opp_table);
827         list_del(&opp_dev->node);
828         call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
829                   _kfree_opp_dev_rcu);
830 }
831
832 struct opp_device *_add_opp_dev(const struct device *dev,
833                                 struct opp_table *opp_table)
834 {
835         struct opp_device *opp_dev;
836         int ret;
837
838         opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
839         if (!opp_dev)
840                 return NULL;
841
842         /* Initialize opp-dev */
843         opp_dev->dev = dev;
844         list_add_rcu(&opp_dev->node, &opp_table->dev_list);
845
846         /* Create debugfs entries for the opp_table */
847         ret = opp_debug_register(opp_dev, opp_table);
848         if (ret)
849                 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
850                         __func__, ret);
851
852         return opp_dev;
853 }
854
855 /**
856  * _add_opp_table() - Find OPP table or allocate a new one
857  * @dev:        device for which we do this operation
858  *
859  * It tries to find an existing table first, if it couldn't find one, it
860  * allocates a new OPP table and returns that.
861  *
862  * Return: valid opp_table pointer if success, else NULL.
863  */
864 static struct opp_table *_add_opp_table(struct device *dev)
865 {
866         struct opp_table *opp_table;
867         struct opp_device *opp_dev;
868         struct device_node *np;
869         int ret;
870
871         /* Check for existing table for 'dev' first */
872         opp_table = _find_opp_table(dev);
873         if (!IS_ERR(opp_table))
874                 return opp_table;
875
876         /*
877          * Allocate a new OPP table. In the infrequent case where a new
878          * device is needed to be added, we pay this penalty.
879          */
880         opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
881         if (!opp_table)
882                 return NULL;
883
884         INIT_LIST_HEAD(&opp_table->dev_list);
885
886         opp_dev = _add_opp_dev(dev, opp_table);
887         if (!opp_dev) {
888                 kfree(opp_table);
889                 return NULL;
890         }
891
892         /*
893          * Only required for backward compatibility with v1 bindings, but isn't
894          * harmful for other cases. And so we do it unconditionally.
895          */
896         np = of_node_get(dev->of_node);
897         if (np) {
898                 u32 val;
899
900                 if (!of_property_read_u32(np, "clock-latency", &val))
901                         opp_table->clock_latency_ns_max = val;
902                 of_property_read_u32(np, "voltage-tolerance",
903                                      &opp_table->voltage_tolerance_v1);
904                 of_node_put(np);
905         }
906
907         /* Set regulator to a non-NULL error value */
908         opp_table->regulator = ERR_PTR(-ENXIO);
909
910         /* Find clk for the device */
911         opp_table->clk = clk_get(dev, NULL);
912         if (IS_ERR(opp_table->clk)) {
913                 ret = PTR_ERR(opp_table->clk);
914                 if (ret != -EPROBE_DEFER)
915                         dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
916                                 ret);
917         }
918
919         srcu_init_notifier_head(&opp_table->srcu_head);
920         INIT_LIST_HEAD(&opp_table->opp_list);
921
922         /* Secure the device table modification */
923         list_add_rcu(&opp_table->node, &opp_tables);
924         return opp_table;
925 }
926
927 /**
928  * _kfree_device_rcu() - Free opp_table RCU handler
929  * @head:       RCU head
930  */
931 static void _kfree_device_rcu(struct rcu_head *head)
932 {
933         struct opp_table *opp_table = container_of(head, struct opp_table,
934                                                    rcu_head);
935
936         kfree_rcu(opp_table, rcu_head);
937 }
938
939 /**
940  * _remove_opp_table() - Removes a OPP table
941  * @opp_table: OPP table to be removed.
942  *
943  * Removes/frees OPP table if it doesn't contain any OPPs.
944  */
945 static void _remove_opp_table(struct opp_table *opp_table)
946 {
947         struct opp_device *opp_dev;
948
949         if (!list_empty(&opp_table->opp_list))
950                 return;
951
952         if (opp_table->supported_hw)
953                 return;
954
955         if (opp_table->prop_name)
956                 return;
957
958         if (!IS_ERR(opp_table->regulator))
959                 return;
960
961         /* Release clk */
962         if (!IS_ERR(opp_table->clk))
963                 clk_put(opp_table->clk);
964
965         opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
966                                    node);
967
968         _remove_opp_dev(opp_dev, opp_table);
969
970         /* dev_list must be empty now */
971         WARN_ON(!list_empty(&opp_table->dev_list));
972
973         list_del_rcu(&opp_table->node);
974         call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
975                   _kfree_device_rcu);
976 }
977
978 /**
979  * _kfree_opp_rcu() - Free OPP RCU handler
980  * @head:       RCU head
981  */
982 static void _kfree_opp_rcu(struct rcu_head *head)
983 {
984         struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
985
986         kfree_rcu(opp, rcu_head);
987 }
988
989 /**
990  * _opp_remove()  - Remove an OPP from a table definition
991  * @opp_table:  points back to the opp_table struct this opp belongs to
992  * @opp:        pointer to the OPP to remove
993  * @notify:     OPP_EVENT_REMOVE notification should be sent or not
994  *
995  * This function removes an opp definition from the opp table.
996  *
997  * Locking: The internal opp_table and opp structures are RCU protected.
998  * It is assumed that the caller holds required mutex for an RCU updater
999  * strategy.
1000  */
1001 static void _opp_remove(struct opp_table *opp_table,
1002                         struct dev_pm_opp *opp, bool notify)
1003 {
1004         /*
1005          * Notify the changes in the availability of the operable
1006          * frequency/voltage list.
1007          */
1008         if (notify)
1009                 srcu_notifier_call_chain(&opp_table->srcu_head,
1010                                          OPP_EVENT_REMOVE, opp);
1011         opp_debug_remove_one(opp);
1012         list_del_rcu(&opp->node);
1013         call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
1014
1015         _remove_opp_table(opp_table);
1016 }
1017
1018 /**
1019  * dev_pm_opp_remove()  - Remove an OPP from OPP table
1020  * @dev:        device for which we do this operation
1021  * @freq:       OPP to remove with matching 'freq'
1022  *
1023  * This function removes an opp from the opp table.
1024  *
1025  * Locking: The internal opp_table and opp structures are RCU protected.
1026  * Hence this function internally uses RCU updater strategy with mutex locks
1027  * to keep the integrity of the internal data structures. Callers should ensure
1028  * that this function is *NOT* called under RCU protection or in contexts where
1029  * mutex cannot be locked.
1030  */
1031 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1032 {
1033         struct dev_pm_opp *opp;
1034         struct opp_table *opp_table;
1035         bool found = false;
1036
1037         /* Hold our table modification lock here */
1038         mutex_lock(&opp_table_lock);
1039
1040         opp_table = _find_opp_table(dev);
1041         if (IS_ERR(opp_table))
1042                 goto unlock;
1043
1044         list_for_each_entry(opp, &opp_table->opp_list, node) {
1045                 if (opp->rate == freq) {
1046                         found = true;
1047                         break;
1048                 }
1049         }
1050
1051         if (!found) {
1052                 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1053                          __func__, freq);
1054                 goto unlock;
1055         }
1056
1057         _opp_remove(opp_table, opp, true);
1058 unlock:
1059         mutex_unlock(&opp_table_lock);
1060 }
1061 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1062
1063 static struct dev_pm_opp *_allocate_opp(struct device *dev,
1064                                         struct opp_table **opp_table)
1065 {
1066         struct dev_pm_opp *opp;
1067
1068         /* allocate new OPP node */
1069         opp = kzalloc(sizeof(*opp), GFP_KERNEL);
1070         if (!opp)
1071                 return NULL;
1072
1073         INIT_LIST_HEAD(&opp->node);
1074
1075         *opp_table = _add_opp_table(dev);
1076         if (!*opp_table) {
1077                 kfree(opp);
1078                 return NULL;
1079         }
1080
1081         return opp;
1082 }
1083
1084 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1085                                          struct opp_table *opp_table)
1086 {
1087         struct regulator *reg = opp_table->regulator;
1088
1089         if (!IS_ERR(reg) &&
1090             !regulator_is_supported_voltage(reg, opp->u_volt_min,
1091                                             opp->u_volt_max)) {
1092                 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1093                         __func__, opp->u_volt_min, opp->u_volt_max);
1094                 return false;
1095         }
1096
1097         return true;
1098 }
1099
1100 static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1101                     struct opp_table *opp_table)
1102 {
1103         struct dev_pm_opp *opp;
1104         struct list_head *head = &opp_table->opp_list;
1105         int ret;
1106
1107         /*
1108          * Insert new OPP in order of increasing frequency and discard if
1109          * already present.
1110          *
1111          * Need to use &opp_table->opp_list in the condition part of the 'for'
1112          * loop, don't replace it with head otherwise it will become an infinite
1113          * loop.
1114          */
1115         list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
1116                 if (new_opp->rate > opp->rate) {
1117                         head = &opp->node;
1118                         continue;
1119                 }
1120
1121                 if (new_opp->rate < opp->rate)
1122                         break;
1123
1124                 /* Duplicate OPPs */
1125                 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1126                          __func__, opp->rate, opp->u_volt, opp->available,
1127                          new_opp->rate, new_opp->u_volt, new_opp->available);
1128
1129                 return opp->available && new_opp->u_volt == opp->u_volt ?
1130                         0 : -EEXIST;
1131         }
1132
1133         new_opp->opp_table = opp_table;
1134         list_add_rcu(&new_opp->node, head);
1135
1136         ret = opp_debug_create_one(new_opp, opp_table);
1137         if (ret)
1138                 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1139                         __func__, ret);
1140
1141         if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1142                 new_opp->available = false;
1143                 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1144                          __func__, new_opp->rate);
1145         }
1146
1147         return 0;
1148 }
1149
1150 /**
1151  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1152  * @dev:        device for which we do this operation
1153  * @freq:       Frequency in Hz for this OPP
1154  * @u_volt:     Voltage in uVolts for this OPP
1155  * @dynamic:    Dynamically added OPPs.
1156  *
1157  * This function adds an opp definition to the opp table and returns status.
1158  * The opp is made available by default and it can be controlled using
1159  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1160  *
1161  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1162  * and freed by dev_pm_opp_of_remove_table.
1163  *
1164  * Locking: The internal opp_table and opp structures are RCU protected.
1165  * Hence this function internally uses RCU updater strategy with mutex locks
1166  * to keep the integrity of the internal data structures. Callers should ensure
1167  * that this function is *NOT* called under RCU protection or in contexts where
1168  * mutex cannot be locked.
1169  *
1170  * Return:
1171  * 0            On success OR
1172  *              Duplicate OPPs (both freq and volt are same) and opp->available
1173  * -EEXIST      Freq are same and volt are different OR
1174  *              Duplicate OPPs (both freq and volt are same) and !opp->available
1175  * -ENOMEM      Memory allocation failure
1176  */
1177 static int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
1178                        bool dynamic)
1179 {
1180         struct opp_table *opp_table;
1181         struct dev_pm_opp *new_opp;
1182         unsigned long tol;
1183         int ret;
1184
1185         /* Hold our table modification lock here */
1186         mutex_lock(&opp_table_lock);
1187
1188         new_opp = _allocate_opp(dev, &opp_table);
1189         if (!new_opp) {
1190                 ret = -ENOMEM;
1191                 goto unlock;
1192         }
1193
1194         /* populate the opp table */
1195         new_opp->rate = freq;
1196         tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1197         new_opp->u_volt = u_volt;
1198         new_opp->u_volt_min = u_volt - tol;
1199         new_opp->u_volt_max = u_volt + tol;
1200         new_opp->available = true;
1201         new_opp->dynamic = dynamic;
1202
1203         ret = _opp_add(dev, new_opp, opp_table);
1204         if (ret)
1205                 goto free_opp;
1206
1207         mutex_unlock(&opp_table_lock);
1208
1209         /*
1210          * Notify the changes in the availability of the operable
1211          * frequency/voltage list.
1212          */
1213         srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
1214         return 0;
1215
1216 free_opp:
1217         _opp_remove(opp_table, new_opp, false);
1218 unlock:
1219         mutex_unlock(&opp_table_lock);
1220         return ret;
1221 }
1222
1223 /* TODO: Support multiple regulators */
1224 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
1225                               struct opp_table *opp_table)
1226 {
1227         u32 microvolt[3] = {0};
1228         u32 val;
1229         int count, ret;
1230         struct property *prop = NULL;
1231         char name[NAME_MAX];
1232
1233         /* Search for "opp-microvolt-<name>" */
1234         if (opp_table->prop_name) {
1235                 snprintf(name, sizeof(name), "opp-microvolt-%s",
1236                          opp_table->prop_name);
1237                 prop = of_find_property(opp->np, name, NULL);
1238         }
1239
1240         if (!prop) {
1241                 /* Search for "opp-microvolt" */
1242                 sprintf(name, "opp-microvolt");
1243                 prop = of_find_property(opp->np, name, NULL);
1244
1245                 /* Missing property isn't a problem, but an invalid entry is */
1246                 if (!prop)
1247                         return 0;
1248         }
1249
1250         count = of_property_count_u32_elems(opp->np, name);
1251         if (count < 0) {
1252                 dev_err(dev, "%s: Invalid %s property (%d)\n",
1253                         __func__, name, count);
1254                 return count;
1255         }
1256
1257         /* There can be one or three elements here */
1258         if (count != 1 && count != 3) {
1259                 dev_err(dev, "%s: Invalid number of elements in %s property (%d)\n",
1260                         __func__, name, count);
1261                 return -EINVAL;
1262         }
1263
1264         ret = of_property_read_u32_array(opp->np, name, microvolt, count);
1265         if (ret) {
1266                 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
1267                 return -EINVAL;
1268         }
1269
1270         opp->u_volt = microvolt[0];
1271
1272         if (count == 1) {
1273                 opp->u_volt_min = opp->u_volt;
1274                 opp->u_volt_max = opp->u_volt;
1275         } else {
1276                 opp->u_volt_min = microvolt[1];
1277                 opp->u_volt_max = microvolt[2];
1278         }
1279
1280         /* Search for "opp-microamp-<name>" */
1281         prop = NULL;
1282         if (opp_table->prop_name) {
1283                 snprintf(name, sizeof(name), "opp-microamp-%s",
1284                          opp_table->prop_name);
1285                 prop = of_find_property(opp->np, name, NULL);
1286         }
1287
1288         if (!prop) {
1289                 /* Search for "opp-microamp" */
1290                 sprintf(name, "opp-microamp");
1291                 prop = of_find_property(opp->np, name, NULL);
1292         }
1293
1294         if (prop && !of_property_read_u32(opp->np, name, &val))
1295                 opp->u_amp = val;
1296
1297         return 0;
1298 }
1299
1300 /**
1301  * dev_pm_opp_set_supported_hw() - Set supported platforms
1302  * @dev: Device for which supported-hw has to be set.
1303  * @versions: Array of hierarchy of versions to match.
1304  * @count: Number of elements in the array.
1305  *
1306  * This is required only for the V2 bindings, and it enables a platform to
1307  * specify the hierarchy of versions it supports. OPP layer will then enable
1308  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1309  * property.
1310  *
1311  * Locking: The internal opp_table and opp structures are RCU protected.
1312  * Hence this function internally uses RCU updater strategy with mutex locks
1313  * to keep the integrity of the internal data structures. Callers should ensure
1314  * that this function is *NOT* called under RCU protection or in contexts where
1315  * mutex cannot be locked.
1316  */
1317 int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1318                                 unsigned int count)
1319 {
1320         struct opp_table *opp_table;
1321         int ret = 0;
1322
1323         /* Hold our table modification lock here */
1324         mutex_lock(&opp_table_lock);
1325
1326         opp_table = _add_opp_table(dev);
1327         if (!opp_table) {
1328                 ret = -ENOMEM;
1329                 goto unlock;
1330         }
1331
1332         /* Make sure there are no concurrent readers while updating opp_table */
1333         WARN_ON(!list_empty(&opp_table->opp_list));
1334
1335         /* Do we already have a version hierarchy associated with opp_table? */
1336         if (opp_table->supported_hw) {
1337                 dev_err(dev, "%s: Already have supported hardware list\n",
1338                         __func__);
1339                 ret = -EBUSY;
1340                 goto err;
1341         }
1342
1343         opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1344                                         GFP_KERNEL);
1345         if (!opp_table->supported_hw) {
1346                 ret = -ENOMEM;
1347                 goto err;
1348         }
1349
1350         opp_table->supported_hw_count = count;
1351         mutex_unlock(&opp_table_lock);
1352         return 0;
1353
1354 err:
1355         _remove_opp_table(opp_table);
1356 unlock:
1357         mutex_unlock(&opp_table_lock);
1358
1359         return ret;
1360 }
1361 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1362
1363 /**
1364  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1365  * @dev: Device for which supported-hw has to be put.
1366  *
1367  * This is required only for the V2 bindings, and is called for a matching
1368  * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1369  * will not be freed.
1370  *
1371  * Locking: The internal opp_table and opp structures are RCU protected.
1372  * Hence this function internally uses RCU updater strategy with mutex locks
1373  * to keep the integrity of the internal data structures. Callers should ensure
1374  * that this function is *NOT* called under RCU protection or in contexts where
1375  * mutex cannot be locked.
1376  */
1377 void dev_pm_opp_put_supported_hw(struct device *dev)
1378 {
1379         struct opp_table *opp_table;
1380
1381         /* Hold our table modification lock here */
1382         mutex_lock(&opp_table_lock);
1383
1384         /* Check for existing table for 'dev' first */
1385         opp_table = _find_opp_table(dev);
1386         if (IS_ERR(opp_table)) {
1387                 dev_err(dev, "Failed to find opp_table: %ld\n",
1388                         PTR_ERR(opp_table));
1389                 goto unlock;
1390         }
1391
1392         /* Make sure there are no concurrent readers while updating opp_table */
1393         WARN_ON(!list_empty(&opp_table->opp_list));
1394
1395         if (!opp_table->supported_hw) {
1396                 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1397                         __func__);
1398                 goto unlock;
1399         }
1400
1401         kfree(opp_table->supported_hw);
1402         opp_table->supported_hw = NULL;
1403         opp_table->supported_hw_count = 0;
1404
1405         /* Try freeing opp_table if this was the last blocking resource */
1406         _remove_opp_table(opp_table);
1407
1408 unlock:
1409         mutex_unlock(&opp_table_lock);
1410 }
1411 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1412
1413 /**
1414  * dev_pm_opp_set_prop_name() - Set prop-extn name
1415  * @dev: Device for which the prop-name has to be set.
1416  * @name: name to postfix to properties.
1417  *
1418  * This is required only for the V2 bindings, and it enables a platform to
1419  * specify the extn to be used for certain property names. The properties to
1420  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1421  * should postfix the property name with -<name> while looking for them.
1422  *
1423  * Locking: The internal opp_table and opp structures are RCU protected.
1424  * Hence this function internally uses RCU updater strategy with mutex locks
1425  * to keep the integrity of the internal data structures. Callers should ensure
1426  * that this function is *NOT* called under RCU protection or in contexts where
1427  * mutex cannot be locked.
1428  */
1429 int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1430 {
1431         struct opp_table *opp_table;
1432         int ret = 0;
1433
1434         /* Hold our table modification lock here */
1435         mutex_lock(&opp_table_lock);
1436
1437         opp_table = _add_opp_table(dev);
1438         if (!opp_table) {
1439                 ret = -ENOMEM;
1440                 goto unlock;
1441         }
1442
1443         /* Make sure there are no concurrent readers while updating opp_table */
1444         WARN_ON(!list_empty(&opp_table->opp_list));
1445
1446         /* Do we already have a prop-name associated with opp_table? */
1447         if (opp_table->prop_name) {
1448                 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
1449                         opp_table->prop_name);
1450                 ret = -EBUSY;
1451                 goto err;
1452         }
1453
1454         opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1455         if (!opp_table->prop_name) {
1456                 ret = -ENOMEM;
1457                 goto err;
1458         }
1459
1460         mutex_unlock(&opp_table_lock);
1461         return 0;
1462
1463 err:
1464         _remove_opp_table(opp_table);
1465 unlock:
1466         mutex_unlock(&opp_table_lock);
1467
1468         return ret;
1469 }
1470 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1471
1472 /**
1473  * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1474  * @dev: Device for which the prop-name has to be put.
1475  *
1476  * This is required only for the V2 bindings, and is called for a matching
1477  * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1478  * will not be freed.
1479  *
1480  * Locking: The internal opp_table and opp structures are RCU protected.
1481  * Hence this function internally uses RCU updater strategy with mutex locks
1482  * to keep the integrity of the internal data structures. Callers should ensure
1483  * that this function is *NOT* called under RCU protection or in contexts where
1484  * mutex cannot be locked.
1485  */
1486 void dev_pm_opp_put_prop_name(struct device *dev)
1487 {
1488         struct opp_table *opp_table;
1489
1490         /* Hold our table modification lock here */
1491         mutex_lock(&opp_table_lock);
1492
1493         /* Check for existing table for 'dev' first */
1494         opp_table = _find_opp_table(dev);
1495         if (IS_ERR(opp_table)) {
1496                 dev_err(dev, "Failed to find opp_table: %ld\n",
1497                         PTR_ERR(opp_table));
1498                 goto unlock;
1499         }
1500
1501         /* Make sure there are no concurrent readers while updating opp_table */
1502         WARN_ON(!list_empty(&opp_table->opp_list));
1503
1504         if (!opp_table->prop_name) {
1505                 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1506                 goto unlock;
1507         }
1508
1509         kfree(opp_table->prop_name);
1510         opp_table->prop_name = NULL;
1511
1512         /* Try freeing opp_table if this was the last blocking resource */
1513         _remove_opp_table(opp_table);
1514
1515 unlock:
1516         mutex_unlock(&opp_table_lock);
1517 }
1518 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1519
1520 /**
1521  * dev_pm_opp_set_regulator() - Set regulator name for the device
1522  * @dev: Device for which regulator name is being set.
1523  * @name: Name of the regulator.
1524  *
1525  * In order to support OPP switching, OPP layer needs to know the name of the
1526  * device's regulator, as the core would be required to switch voltages as well.
1527  *
1528  * This must be called before any OPPs are initialized for the device.
1529  *
1530  * Locking: The internal opp_table and opp structures are RCU protected.
1531  * Hence this function internally uses RCU updater strategy with mutex locks
1532  * to keep the integrity of the internal data structures. Callers should ensure
1533  * that this function is *NOT* called under RCU protection or in contexts where
1534  * mutex cannot be locked.
1535  */
1536 int dev_pm_opp_set_regulator(struct device *dev, const char *name)
1537 {
1538         struct opp_table *opp_table;
1539         struct regulator *reg;
1540         int ret;
1541
1542         mutex_lock(&opp_table_lock);
1543
1544         opp_table = _add_opp_table(dev);
1545         if (!opp_table) {
1546                 ret = -ENOMEM;
1547                 goto unlock;
1548         }
1549
1550         /* This should be called before OPPs are initialized */
1551         if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1552                 ret = -EBUSY;
1553                 goto err;
1554         }
1555
1556         /* Already have a regulator set */
1557         if (WARN_ON(!IS_ERR(opp_table->regulator))) {
1558                 ret = -EBUSY;
1559                 goto err;
1560         }
1561         /* Allocate the regulator */
1562         reg = regulator_get_optional(dev, name);
1563         if (IS_ERR(reg)) {
1564                 ret = PTR_ERR(reg);
1565                 if (ret != -EPROBE_DEFER)
1566                         dev_err(dev, "%s: no regulator (%s) found: %d\n",
1567                                 __func__, name, ret);
1568                 goto err;
1569         }
1570
1571         opp_table->regulator = reg;
1572
1573         mutex_unlock(&opp_table_lock);
1574         return 0;
1575
1576 err:
1577         _remove_opp_table(opp_table);
1578 unlock:
1579         mutex_unlock(&opp_table_lock);
1580
1581         return ret;
1582 }
1583 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulator);
1584
1585 /**
1586  * dev_pm_opp_put_regulator() - Releases resources blocked for regulator
1587  * @dev: Device for which regulator was set.
1588  *
1589  * Locking: The internal opp_table and opp structures are RCU protected.
1590  * Hence this function internally uses RCU updater strategy with mutex locks
1591  * to keep the integrity of the internal data structures. Callers should ensure
1592  * that this function is *NOT* called under RCU protection or in contexts where
1593  * mutex cannot be locked.
1594  */
1595 void dev_pm_opp_put_regulator(struct device *dev)
1596 {
1597         struct opp_table *opp_table;
1598
1599         mutex_lock(&opp_table_lock);
1600
1601         /* Check for existing table for 'dev' first */
1602         opp_table = _find_opp_table(dev);
1603         if (IS_ERR(opp_table)) {
1604                 dev_err(dev, "Failed to find opp_table: %ld\n",
1605                         PTR_ERR(opp_table));
1606                 goto unlock;
1607         }
1608
1609         if (IS_ERR(opp_table->regulator)) {
1610                 dev_err(dev, "%s: Doesn't have regulator set\n", __func__);
1611                 goto unlock;
1612         }
1613
1614         /* Make sure there are no concurrent readers while updating opp_table */
1615         WARN_ON(!list_empty(&opp_table->opp_list));
1616
1617         regulator_put(opp_table->regulator);
1618         opp_table->regulator = ERR_PTR(-ENXIO);
1619
1620         /* Try freeing opp_table if this was the last blocking resource */
1621         _remove_opp_table(opp_table);
1622
1623 unlock:
1624         mutex_unlock(&opp_table_lock);
1625 }
1626 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulator);
1627
1628 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
1629                               struct device_node *np)
1630 {
1631         unsigned int count = opp_table->supported_hw_count;
1632         u32 version;
1633         int ret;
1634
1635         if (!opp_table->supported_hw)
1636                 return true;
1637
1638         while (count--) {
1639                 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
1640                                                  &version);
1641                 if (ret) {
1642                         dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
1643                                  __func__, count, ret);
1644                         return false;
1645                 }
1646
1647                 /* Both of these are bitwise masks of the versions */
1648                 if (!(version & opp_table->supported_hw[count]))
1649                         return false;
1650         }
1651
1652         return true;
1653 }
1654
1655 /**
1656  * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
1657  * @dev:        device for which we do this operation
1658  * @np:         device node
1659  *
1660  * This function adds an opp definition to the opp table and returns status. The
1661  * opp can be controlled using dev_pm_opp_enable/disable functions and may be
1662  * removed by dev_pm_opp_remove.
1663  *
1664  * Locking: The internal opp_table and opp structures are RCU protected.
1665  * Hence this function internally uses RCU updater strategy with mutex locks
1666  * to keep the integrity of the internal data structures. Callers should ensure
1667  * that this function is *NOT* called under RCU protection or in contexts where
1668  * mutex cannot be locked.
1669  *
1670  * Return:
1671  * 0            On success OR
1672  *              Duplicate OPPs (both freq and volt are same) and opp->available
1673  * -EEXIST      Freq are same and volt are different OR
1674  *              Duplicate OPPs (both freq and volt are same) and !opp->available
1675  * -ENOMEM      Memory allocation failure
1676  * -EINVAL      Failed parsing the OPP node
1677  */
1678 static int _opp_add_static_v2(struct device *dev, struct device_node *np)
1679 {
1680         struct opp_table *opp_table;
1681         struct dev_pm_opp *new_opp;
1682         u64 rate;
1683         u32 val;
1684         int ret;
1685
1686         /* Hold our table modification lock here */
1687         mutex_lock(&opp_table_lock);
1688
1689         new_opp = _allocate_opp(dev, &opp_table);
1690         if (!new_opp) {
1691                 ret = -ENOMEM;
1692                 goto unlock;
1693         }
1694
1695         ret = of_property_read_u64(np, "opp-hz", &rate);
1696         if (ret < 0) {
1697                 dev_err(dev, "%s: opp-hz not found\n", __func__);
1698                 goto free_opp;
1699         }
1700
1701         /* Check if the OPP supports hardware's hierarchy of versions or not */
1702         if (!_opp_is_supported(dev, opp_table, np)) {
1703                 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
1704                 goto free_opp;
1705         }
1706
1707         /*
1708          * Rate is defined as an unsigned long in clk API, and so casting
1709          * explicitly to its type. Must be fixed once rate is 64 bit
1710          * guaranteed in clk API.
1711          */
1712         new_opp->rate = (unsigned long)rate;
1713         new_opp->turbo = of_property_read_bool(np, "turbo-mode");
1714
1715         new_opp->np = np;
1716         new_opp->dynamic = false;
1717         new_opp->available = true;
1718
1719         if (!of_property_read_u32(np, "clock-latency-ns", &val))
1720                 new_opp->clock_latency_ns = val;
1721
1722         ret = opp_parse_supplies(new_opp, dev, opp_table);
1723         if (ret)
1724                 goto free_opp;
1725
1726         ret = _opp_add(dev, new_opp, opp_table);
1727         if (ret)
1728                 goto free_opp;
1729
1730         /* OPP to select on device suspend */
1731         if (of_property_read_bool(np, "opp-suspend")) {
1732                 if (opp_table->suspend_opp) {
1733                         dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
1734                                  __func__, opp_table->suspend_opp->rate,
1735                                  new_opp->rate);
1736                 } else {
1737                         new_opp->suspend = true;
1738                         opp_table->suspend_opp = new_opp;
1739                 }
1740         }
1741
1742         if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
1743                 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
1744
1745         mutex_unlock(&opp_table_lock);
1746
1747         pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
1748                  __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
1749                  new_opp->u_volt_min, new_opp->u_volt_max,
1750                  new_opp->clock_latency_ns);
1751
1752         /*
1753          * Notify the changes in the availability of the operable
1754          * frequency/voltage list.
1755          */
1756         srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
1757         return 0;
1758
1759 free_opp:
1760         _opp_remove(opp_table, new_opp, false);
1761 unlock:
1762         mutex_unlock(&opp_table_lock);
1763         return ret;
1764 }
1765
1766 /**
1767  * dev_pm_opp_add()  - Add an OPP table from a table definitions
1768  * @dev:        device for which we do this operation
1769  * @freq:       Frequency in Hz for this OPP
1770  * @u_volt:     Voltage in uVolts for this OPP
1771  *
1772  * This function adds an opp definition to the opp table and returns status.
1773  * The opp is made available by default and it can be controlled using
1774  * dev_pm_opp_enable/disable functions.
1775  *
1776  * Locking: The internal opp_table and opp structures are RCU protected.
1777  * Hence this function internally uses RCU updater strategy with mutex locks
1778  * to keep the integrity of the internal data structures. Callers should ensure
1779  * that this function is *NOT* called under RCU protection or in contexts where
1780  * mutex cannot be locked.
1781  *
1782  * Return:
1783  * 0            On success OR
1784  *              Duplicate OPPs (both freq and volt are same) and opp->available
1785  * -EEXIST      Freq are same and volt are different OR
1786  *              Duplicate OPPs (both freq and volt are same) and !opp->available
1787  * -ENOMEM      Memory allocation failure
1788  */
1789 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1790 {
1791         return _opp_add_v1(dev, freq, u_volt, true);
1792 }
1793 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1794
1795 /**
1796  * _opp_set_availability() - helper to set the availability of an opp
1797  * @dev:                device for which we do this operation
1798  * @freq:               OPP frequency to modify availability
1799  * @availability_req:   availability status requested for this opp
1800  *
1801  * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1802  * share a common logic which is isolated here.
1803  *
1804  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1805  * copy operation, returns 0 if no modification was done OR modification was
1806  * successful.
1807  *
1808  * Locking: The internal opp_table and opp structures are RCU protected.
1809  * Hence this function internally uses RCU updater strategy with mutex locks to
1810  * keep the integrity of the internal data structures. Callers should ensure
1811  * that this function is *NOT* called under RCU protection or in contexts where
1812  * mutex locking or synchronize_rcu() blocking calls cannot be used.
1813  */
1814 static int _opp_set_availability(struct device *dev, unsigned long freq,
1815                                  bool availability_req)
1816 {
1817         struct opp_table *opp_table;
1818         struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
1819         int r = 0;
1820
1821         /* keep the node allocated */
1822         new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
1823         if (!new_opp)
1824                 return -ENOMEM;
1825
1826         mutex_lock(&opp_table_lock);
1827
1828         /* Find the opp_table */
1829         opp_table = _find_opp_table(dev);
1830         if (IS_ERR(opp_table)) {
1831                 r = PTR_ERR(opp_table);
1832                 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1833                 goto unlock;
1834         }
1835
1836         /* Do we have the frequency? */
1837         list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1838                 if (tmp_opp->rate == freq) {
1839                         opp = tmp_opp;
1840                         break;
1841                 }
1842         }
1843         if (IS_ERR(opp)) {
1844                 r = PTR_ERR(opp);
1845                 goto unlock;
1846         }
1847
1848         /* Is update really needed? */
1849         if (opp->available == availability_req)
1850                 goto unlock;
1851         /* copy the old data over */
1852         *new_opp = *opp;
1853
1854         /* plug in new node */
1855         new_opp->available = availability_req;
1856
1857         list_replace_rcu(&opp->node, &new_opp->node);
1858         mutex_unlock(&opp_table_lock);
1859         call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
1860
1861         /* Notify the change of the OPP availability */
1862         if (availability_req)
1863                 srcu_notifier_call_chain(&opp_table->srcu_head,
1864                                          OPP_EVENT_ENABLE, new_opp);
1865         else
1866                 srcu_notifier_call_chain(&opp_table->srcu_head,
1867                                          OPP_EVENT_DISABLE, new_opp);
1868
1869         return 0;
1870
1871 unlock:
1872         mutex_unlock(&opp_table_lock);
1873         kfree(new_opp);
1874         return r;
1875 }
1876
1877 /*
1878  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
1879  * @dev:                device for which we do this operation
1880  * @freq:               OPP frequency to adjust voltage of
1881  * @u_volt:             new OPP voltage
1882  *
1883  * Change the voltage of an OPP with an RCU operation.
1884  *
1885  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1886  * copy operation, returns 0 if no modifcation was done OR modification was
1887  * successful.
1888  *
1889  * Locking: The internal device_opp and opp structures are RCU protected.
1890  * Hence this function internally uses RCU updater strategy with mutex locks to
1891  * keep the integrity of the internal data structures. Callers should ensure
1892  * that this function is *NOT* called under RCU protection or in contexts where
1893  * mutex locking or synchronize_rcu() blocking calls cannot be used.
1894  */
1895 int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
1896                               unsigned long u_volt)
1897 {
1898         struct opp_table *opp_table;
1899         struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
1900         int r = 0;
1901
1902         /* keep the node allocated */
1903         new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
1904         if (!new_opp)
1905                 return -ENOMEM;
1906
1907         mutex_lock(&opp_table_lock);
1908
1909         /* Find the opp_table */
1910         opp_table = _find_opp_table(dev);
1911         if (IS_ERR(opp_table)) {
1912                 r = PTR_ERR(opp_table);
1913                 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1914                 goto unlock;
1915         }
1916
1917         /* Do we have the frequency? */
1918         list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1919                 if (tmp_opp->rate == freq) {
1920                         opp = tmp_opp;
1921                         break;
1922                 }
1923         }
1924         if (IS_ERR(opp)) {
1925                 r = PTR_ERR(opp);
1926                 goto unlock;
1927         }
1928
1929         /* Is update really needed? */
1930         if (opp->u_volt == u_volt)
1931                 goto unlock;
1932         /* copy the old data over */
1933         *new_opp = *opp;
1934
1935         /* plug in new node */
1936         new_opp->u_volt = u_volt;
1937
1938         if (new_opp->u_volt_min > u_volt)
1939                 new_opp->u_volt_min = u_volt;
1940         if (new_opp->u_volt_max < u_volt)
1941                 new_opp->u_volt_max = u_volt;
1942
1943         _opp_remove(opp_table, opp, false);
1944         r = _opp_add(dev, new_opp, opp_table);
1945         if (r) {
1946                 dev_err(dev, "Failed to add new_opp (u_volt=%lu)\n", u_volt);
1947                 _opp_add(dev, opp, opp_table);
1948                 goto unlock;
1949         }
1950
1951         mutex_unlock(&opp_table_lock);
1952
1953         /* Notify the change of the OPP */
1954         srcu_notifier_call_chain(&opp_table->srcu_head,
1955                                  OPP_EVENT_ADJUST_VOLTAGE, new_opp);
1956
1957         return 0;
1958
1959 unlock:
1960         mutex_unlock(&opp_table_lock);
1961         kfree(new_opp);
1962         return r;
1963 }
1964
1965 /**
1966  * dev_pm_opp_enable() - Enable a specific OPP
1967  * @dev:        device for which we do this operation
1968  * @freq:       OPP frequency to enable
1969  *
1970  * Enables a provided opp. If the operation is valid, this returns 0, else the
1971  * corresponding error value. It is meant to be used for users an OPP available
1972  * after being temporarily made unavailable with dev_pm_opp_disable.
1973  *
1974  * Locking: The internal opp_table and opp structures are RCU protected.
1975  * Hence this function indirectly uses RCU and mutex locks to keep the
1976  * integrity of the internal data structures. Callers should ensure that
1977  * this function is *NOT* called under RCU protection or in contexts where
1978  * mutex locking or synchronize_rcu() blocking calls cannot be used.
1979  *
1980  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1981  * copy operation, returns 0 if no modification was done OR modification was
1982  * successful.
1983  */
1984 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1985 {
1986         return _opp_set_availability(dev, freq, true);
1987 }
1988 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1989
1990 /**
1991  * dev_pm_opp_disable() - Disable a specific OPP
1992  * @dev:        device for which we do this operation
1993  * @freq:       OPP frequency to disable
1994  *
1995  * Disables a provided opp. If the operation is valid, this returns
1996  * 0, else the corresponding error value. It is meant to be a temporary
1997  * control by users to make this OPP not available until the circumstances are
1998  * right to make it available again (with a call to dev_pm_opp_enable).
1999  *
2000  * Locking: The internal opp_table and opp structures are RCU protected.
2001  * Hence this function indirectly uses RCU and mutex locks to keep the
2002  * integrity of the internal data structures. Callers should ensure that
2003  * this function is *NOT* called under RCU protection or in contexts where
2004  * mutex locking or synchronize_rcu() blocking calls cannot be used.
2005  *
2006  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2007  * copy operation, returns 0 if no modification was done OR modification was
2008  * successful.
2009  */
2010 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
2011 {
2012         return _opp_set_availability(dev, freq, false);
2013 }
2014 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
2015
2016 /**
2017  * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
2018  * @dev:        device pointer used to lookup OPP table.
2019  *
2020  * Return: pointer to  notifier head if found, otherwise -ENODEV or
2021  * -EINVAL based on type of error casted as pointer. value must be checked
2022  *  with IS_ERR to determine valid pointer or error result.
2023  *
2024  * Locking: This function must be called under rcu_read_lock(). opp_table is a
2025  * RCU protected pointer. The reason for the same is that the opp pointer which
2026  * is returned will remain valid for use with opp_get_{voltage, freq} only while
2027  * under the locked area. The pointer returned must be used prior to unlocking
2028  * with rcu_read_unlock() to maintain the integrity of the pointer.
2029  */
2030 struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
2031 {
2032         struct opp_table *opp_table = _find_opp_table(dev);
2033
2034         if (IS_ERR(opp_table))
2035                 return ERR_CAST(opp_table); /* matching type */
2036
2037         return &opp_table->srcu_head;
2038 }
2039 EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
2040
2041 #ifdef CONFIG_OF
2042 /**
2043  * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
2044  *                                entries
2045  * @dev:        device pointer used to lookup OPP table.
2046  *
2047  * Free OPPs created using static entries present in DT.
2048  *
2049  * Locking: The internal opp_table and opp structures are RCU protected.
2050  * Hence this function indirectly uses RCU updater strategy with mutex locks
2051  * to keep the integrity of the internal data structures. Callers should ensure
2052  * that this function is *NOT* called under RCU protection or in contexts where
2053  * mutex cannot be locked.
2054  */
2055 void dev_pm_opp_of_remove_table(struct device *dev)
2056 {
2057         struct opp_table *opp_table;
2058         struct dev_pm_opp *opp, *tmp;
2059
2060         /* Hold our table modification lock here */
2061         mutex_lock(&opp_table_lock);
2062
2063         /* Check for existing table for 'dev' */
2064         opp_table = _find_opp_table(dev);
2065         if (IS_ERR(opp_table)) {
2066                 int error = PTR_ERR(opp_table);
2067
2068                 if (error != -ENODEV)
2069                         WARN(1, "%s: opp_table: %d\n",
2070                              IS_ERR_OR_NULL(dev) ?
2071                                         "Invalid device" : dev_name(dev),
2072                              error);
2073                 goto unlock;
2074         }
2075
2076         /* Find if opp_table manages a single device */
2077         if (list_is_singular(&opp_table->dev_list)) {
2078                 /* Free static OPPs */
2079                 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
2080                         if (!opp->dynamic)
2081                                 _opp_remove(opp_table, opp, true);
2082                 }
2083         } else {
2084                 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
2085         }
2086
2087 unlock:
2088         mutex_unlock(&opp_table_lock);
2089 }
2090 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
2091
2092 /* Returns opp descriptor node for a device, caller must do of_node_put() */
2093 struct device_node *_of_get_opp_desc_node(struct device *dev)
2094 {
2095         /*
2096          * TODO: Support for multiple OPP tables.
2097          *
2098          * There should be only ONE phandle present in "operating-points-v2"
2099          * property.
2100          */
2101
2102         return of_parse_phandle(dev->of_node, "operating-points-v2", 0);
2103 }
2104
2105 /* Initializes OPP tables based on new bindings */
2106 static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
2107 {
2108         struct device_node *np;
2109         struct opp_table *opp_table;
2110         int ret = 0, count = 0;
2111
2112         mutex_lock(&opp_table_lock);
2113
2114         opp_table = _managed_opp(opp_np);
2115         if (opp_table) {
2116                 /* OPPs are already managed */
2117                 if (!_add_opp_dev(dev, opp_table))
2118                         ret = -ENOMEM;
2119                 mutex_unlock(&opp_table_lock);
2120                 return ret;
2121         }
2122         mutex_unlock(&opp_table_lock);
2123
2124         /* We have opp-table node now, iterate over it and add OPPs */
2125         for_each_available_child_of_node(opp_np, np) {
2126                 count++;
2127
2128                 ret = _opp_add_static_v2(dev, np);
2129                 if (ret) {
2130                         dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
2131                                 ret);
2132                         goto free_table;
2133                 }
2134         }
2135
2136         /* There should be one of more OPP defined */
2137         if (WARN_ON(!count))
2138                 return -ENOENT;
2139
2140         mutex_lock(&opp_table_lock);
2141
2142         opp_table = _find_opp_table(dev);
2143         if (WARN_ON(IS_ERR(opp_table))) {
2144                 ret = PTR_ERR(opp_table);
2145                 mutex_unlock(&opp_table_lock);
2146                 goto free_table;
2147         }
2148
2149         opp_table->np = opp_np;
2150         opp_table->shared_opp = of_property_read_bool(opp_np, "opp-shared");
2151
2152         mutex_unlock(&opp_table_lock);
2153
2154         return 0;
2155
2156 free_table:
2157         dev_pm_opp_of_remove_table(dev);
2158
2159         return ret;
2160 }
2161
2162 /* Initializes OPP tables based on old-deprecated bindings */
2163 static int _of_add_opp_table_v1(struct device *dev)
2164 {
2165         const struct property *prop;
2166         const __be32 *val;
2167         int nr;
2168
2169         prop = of_find_property(dev->of_node, "operating-points", NULL);
2170         if (!prop)
2171                 return -ENODEV;
2172         if (!prop->value)
2173                 return -ENODATA;
2174
2175         /*
2176          * Each OPP is a set of tuples consisting of frequency and
2177          * voltage like <freq-kHz vol-uV>.
2178          */
2179         nr = prop->length / sizeof(u32);
2180         if (nr % 2) {
2181                 dev_err(dev, "%s: Invalid OPP table\n", __func__);
2182                 return -EINVAL;
2183         }
2184
2185         val = prop->value;
2186         while (nr) {
2187                 unsigned long freq = be32_to_cpup(val++) * 1000;
2188                 unsigned long volt = be32_to_cpup(val++);
2189
2190                 if (_opp_add_v1(dev, freq, volt, false))
2191                         dev_warn(dev, "%s: Failed to add OPP %ld\n",
2192                                  __func__, freq);
2193                 nr -= 2;
2194         }
2195
2196         return 0;
2197 }
2198
2199 /**
2200  * dev_pm_opp_of_add_table() - Initialize opp table from device tree
2201  * @dev:        device pointer used to lookup OPP table.
2202  *
2203  * Register the initial OPP table with the OPP library for given device.
2204  *
2205  * Locking: The internal opp_table and opp structures are RCU protected.
2206  * Hence this function indirectly uses RCU updater strategy with mutex locks
2207  * to keep the integrity of the internal data structures. Callers should ensure
2208  * that this function is *NOT* called under RCU protection or in contexts where
2209  * mutex cannot be locked.
2210  *
2211  * Return:
2212  * 0            On success OR
2213  *              Duplicate OPPs (both freq and volt are same) and opp->available
2214  * -EEXIST      Freq are same and volt are different OR
2215  *              Duplicate OPPs (both freq and volt are same) and !opp->available
2216  * -ENOMEM      Memory allocation failure
2217  * -ENODEV      when 'operating-points' property is not found or is invalid data
2218  *              in device node.
2219  * -ENODATA     when empty 'operating-points' property is found
2220  * -EINVAL      when invalid entries are found in opp-v2 table
2221  */
2222 int dev_pm_opp_of_add_table(struct device *dev)
2223 {
2224         struct device_node *opp_np;
2225         int ret;
2226
2227         /*
2228          * OPPs have two version of bindings now. The older one is deprecated,
2229          * try for the new binding first.
2230          */
2231         opp_np = _of_get_opp_desc_node(dev);
2232         if (!opp_np) {
2233                 /*
2234                  * Try old-deprecated bindings for backward compatibility with
2235                  * older dtbs.
2236                  */
2237                 return _of_add_opp_table_v1(dev);
2238         }
2239
2240         ret = _of_add_opp_table_v2(dev, opp_np);
2241         of_node_put(opp_np);
2242
2243         return ret;
2244 }
2245 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
2246 #endif