PM / OPP: Parse 'opp-supported-hw' binding
[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/errno.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/of.h>
21 #include <linux/export.h>
22
23 #include "opp.h"
24
25 /*
26  * The root of the list of all devices. All device_opp structures branch off
27  * from here, with each device_opp containing the list of opp it supports in
28  * various states of availability.
29  */
30 static LIST_HEAD(dev_opp_list);
31 /* Lock to allow exclusive modification to the device and opp lists */
32 DEFINE_MUTEX(dev_opp_list_lock);
33
34 #define opp_rcu_lockdep_assert()                                        \
35 do {                                                                    \
36         RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&                       \
37                                 !lockdep_is_held(&dev_opp_list_lock),   \
38                            "Missing rcu_read_lock() or "                \
39                            "dev_opp_list_lock protection");             \
40 } while (0)
41
42 static struct device_list_opp *_find_list_dev(const struct device *dev,
43                                               struct device_opp *dev_opp)
44 {
45         struct device_list_opp *list_dev;
46
47         list_for_each_entry(list_dev, &dev_opp->dev_list, node)
48                 if (list_dev->dev == dev)
49                         return list_dev;
50
51         return NULL;
52 }
53
54 static struct device_opp *_managed_opp(const struct device_node *np)
55 {
56         struct device_opp *dev_opp;
57
58         list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) {
59                 if (dev_opp->np == np) {
60                         /*
61                          * Multiple devices can point to the same OPP table and
62                          * so will have same node-pointer, np.
63                          *
64                          * But the OPPs will be considered as shared only if the
65                          * OPP table contains a "opp-shared" property.
66                          */
67                         return dev_opp->shared_opp ? dev_opp : NULL;
68                 }
69         }
70
71         return NULL;
72 }
73
74 /**
75  * _find_device_opp() - find device_opp struct using device pointer
76  * @dev:        device pointer used to lookup device OPPs
77  *
78  * Search list of device OPPs for one containing matching device. Does a RCU
79  * reader operation to grab the pointer needed.
80  *
81  * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or
82  * -EINVAL based on type of error.
83  *
84  * Locking: For readers, this function must be called under rcu_read_lock().
85  * device_opp is a RCU protected pointer, which means that device_opp is valid
86  * as long as we are under RCU lock.
87  *
88  * For Writers, this function must be called with dev_opp_list_lock held.
89  */
90 struct device_opp *_find_device_opp(struct device *dev)
91 {
92         struct device_opp *dev_opp;
93
94         opp_rcu_lockdep_assert();
95
96         if (IS_ERR_OR_NULL(dev)) {
97                 pr_err("%s: Invalid parameters\n", __func__);
98                 return ERR_PTR(-EINVAL);
99         }
100
101         list_for_each_entry_rcu(dev_opp, &dev_opp_list, node)
102                 if (_find_list_dev(dev, dev_opp))
103                         return dev_opp;
104
105         return ERR_PTR(-ENODEV);
106 }
107
108 /**
109  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
110  * @opp:        opp for which voltage has to be returned for
111  *
112  * Return: voltage in micro volt corresponding to the opp, else
113  * return 0
114  *
115  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
116  * protected pointer. This means that opp which could have been fetched by
117  * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
118  * under RCU lock. The pointer returned by the opp_find_freq family must be
119  * used in the same section as the usage of this function with the pointer
120  * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
121  * pointer.
122  */
123 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
124 {
125         struct dev_pm_opp *tmp_opp;
126         unsigned long v = 0;
127
128         opp_rcu_lockdep_assert();
129
130         tmp_opp = rcu_dereference(opp);
131         if (IS_ERR_OR_NULL(tmp_opp))
132                 pr_err("%s: Invalid parameters\n", __func__);
133         else
134                 v = tmp_opp->u_volt;
135
136         return v;
137 }
138 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
139
140 /**
141  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
142  * @opp:        opp for which frequency has to be returned for
143  *
144  * Return: frequency in hertz corresponding to the opp, else
145  * return 0
146  *
147  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
148  * protected pointer. This means that opp which could have been fetched by
149  * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
150  * under RCU lock. The pointer returned by the opp_find_freq family must be
151  * used in the same section as the usage of this function with the pointer
152  * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
153  * pointer.
154  */
155 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
156 {
157         struct dev_pm_opp *tmp_opp;
158         unsigned long f = 0;
159
160         opp_rcu_lockdep_assert();
161
162         tmp_opp = rcu_dereference(opp);
163         if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
164                 pr_err("%s: Invalid parameters\n", __func__);
165         else
166                 f = tmp_opp->rate;
167
168         return f;
169 }
170 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
171
172 /**
173  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
174  * @opp: opp for which turbo mode is being verified
175  *
176  * Turbo OPPs are not for normal use, and can be enabled (under certain
177  * conditions) for short duration of times to finish high throughput work
178  * quickly. Running on them for longer times may overheat the chip.
179  *
180  * Return: true if opp is turbo opp, else false.
181  *
182  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
183  * protected pointer. This means that opp which could have been fetched by
184  * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
185  * under RCU lock. The pointer returned by the opp_find_freq family must be
186  * used in the same section as the usage of this function with the pointer
187  * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
188  * pointer.
189  */
190 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
191 {
192         struct dev_pm_opp *tmp_opp;
193
194         opp_rcu_lockdep_assert();
195
196         tmp_opp = rcu_dereference(opp);
197         if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
198                 pr_err("%s: Invalid parameters\n", __func__);
199                 return false;
200         }
201
202         return tmp_opp->turbo;
203 }
204 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
205
206 /**
207  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
208  * @dev:        device for which we do this operation
209  *
210  * Return: This function returns the max clock latency in nanoseconds.
211  *
212  * Locking: This function takes rcu_read_lock().
213  */
214 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
215 {
216         struct device_opp *dev_opp;
217         unsigned long clock_latency_ns;
218
219         rcu_read_lock();
220
221         dev_opp = _find_device_opp(dev);
222         if (IS_ERR(dev_opp))
223                 clock_latency_ns = 0;
224         else
225                 clock_latency_ns = dev_opp->clock_latency_ns_max;
226
227         rcu_read_unlock();
228         return clock_latency_ns;
229 }
230 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
231
232 /**
233  * dev_pm_opp_get_suspend_opp() - Get suspend opp
234  * @dev:        device for which we do this operation
235  *
236  * Return: This function returns pointer to the suspend opp if it is
237  * defined and available, otherwise it returns NULL.
238  *
239  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
240  * protected pointer. The reason for the same is that the opp pointer which is
241  * returned will remain valid for use with opp_get_{voltage, freq} only while
242  * under the locked area. The pointer returned must be used prior to unlocking
243  * with rcu_read_unlock() to maintain the integrity of the pointer.
244  */
245 struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
246 {
247         struct device_opp *dev_opp;
248
249         opp_rcu_lockdep_assert();
250
251         dev_opp = _find_device_opp(dev);
252         if (IS_ERR(dev_opp) || !dev_opp->suspend_opp ||
253             !dev_opp->suspend_opp->available)
254                 return NULL;
255
256         return dev_opp->suspend_opp;
257 }
258 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
259
260 /**
261  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
262  * @dev:        device for which we do this operation
263  *
264  * Return: This function returns the number of available opps if there are any,
265  * else returns 0 if none or the corresponding error value.
266  *
267  * Locking: This function takes rcu_read_lock().
268  */
269 int dev_pm_opp_get_opp_count(struct device *dev)
270 {
271         struct device_opp *dev_opp;
272         struct dev_pm_opp *temp_opp;
273         int count = 0;
274
275         rcu_read_lock();
276
277         dev_opp = _find_device_opp(dev);
278         if (IS_ERR(dev_opp)) {
279                 count = PTR_ERR(dev_opp);
280                 dev_err(dev, "%s: device OPP not found (%d)\n",
281                         __func__, count);
282                 goto out_unlock;
283         }
284
285         list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
286                 if (temp_opp->available)
287                         count++;
288         }
289
290 out_unlock:
291         rcu_read_unlock();
292         return count;
293 }
294 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
295
296 /**
297  * dev_pm_opp_find_freq_exact() - search for an exact frequency
298  * @dev:                device for which we do this operation
299  * @freq:               frequency to search for
300  * @available:          true/false - match for available opp
301  *
302  * Return: Searches for exact match in the opp list and returns pointer to the
303  * matching opp if found, else returns ERR_PTR in case of error and should
304  * be handled using IS_ERR. Error return values can be:
305  * EINVAL:      for bad pointer
306  * ERANGE:      no match found for search
307  * ENODEV:      if device not found in list of registered devices
308  *
309  * Note: available is a modifier for the search. if available=true, then the
310  * match is for exact matching frequency and is available in the stored OPP
311  * table. if false, the match is for exact frequency which is not available.
312  *
313  * This provides a mechanism to enable an opp which is not available currently
314  * or the opposite as well.
315  *
316  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
317  * protected pointer. The reason for the same is that the opp pointer which is
318  * returned will remain valid for use with opp_get_{voltage, freq} only while
319  * under the locked area. The pointer returned must be used prior to unlocking
320  * with rcu_read_unlock() to maintain the integrity of the pointer.
321  */
322 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
323                                               unsigned long freq,
324                                               bool available)
325 {
326         struct device_opp *dev_opp;
327         struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
328
329         opp_rcu_lockdep_assert();
330
331         dev_opp = _find_device_opp(dev);
332         if (IS_ERR(dev_opp)) {
333                 int r = PTR_ERR(dev_opp);
334                 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
335                 return ERR_PTR(r);
336         }
337
338         list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
339                 if (temp_opp->available == available &&
340                                 temp_opp->rate == freq) {
341                         opp = temp_opp;
342                         break;
343                 }
344         }
345
346         return opp;
347 }
348 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
349
350 /**
351  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
352  * @dev:        device for which we do this operation
353  * @freq:       Start frequency
354  *
355  * Search for the matching ceil *available* OPP from a starting freq
356  * for a device.
357  *
358  * Return: matching *opp and refreshes *freq accordingly, else returns
359  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
360  * values can be:
361  * EINVAL:      for bad pointer
362  * ERANGE:      no match found for search
363  * ENODEV:      if device not found in list of registered devices
364  *
365  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
366  * protected pointer. The reason for the same is that the opp pointer which is
367  * returned will remain valid for use with opp_get_{voltage, freq} only while
368  * under the locked area. The pointer returned must be used prior to unlocking
369  * with rcu_read_unlock() to maintain the integrity of the pointer.
370  */
371 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
372                                              unsigned long *freq)
373 {
374         struct device_opp *dev_opp;
375         struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
376
377         opp_rcu_lockdep_assert();
378
379         if (!dev || !freq) {
380                 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
381                 return ERR_PTR(-EINVAL);
382         }
383
384         dev_opp = _find_device_opp(dev);
385         if (IS_ERR(dev_opp))
386                 return ERR_CAST(dev_opp);
387
388         list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
389                 if (temp_opp->available && temp_opp->rate >= *freq) {
390                         opp = temp_opp;
391                         *freq = opp->rate;
392                         break;
393                 }
394         }
395
396         return opp;
397 }
398 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
399
400 /**
401  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
402  * @dev:        device for which we do this operation
403  * @freq:       Start frequency
404  *
405  * Search for the matching floor *available* OPP from a starting freq
406  * for a device.
407  *
408  * Return: matching *opp and refreshes *freq accordingly, else returns
409  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
410  * values can be:
411  * EINVAL:      for bad pointer
412  * ERANGE:      no match found for search
413  * ENODEV:      if device not found in list of registered devices
414  *
415  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
416  * protected pointer. The reason for the same is that the opp pointer which is
417  * returned will remain valid for use with opp_get_{voltage, freq} only while
418  * under the locked area. The pointer returned must be used prior to unlocking
419  * with rcu_read_unlock() to maintain the integrity of the pointer.
420  */
421 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
422                                               unsigned long *freq)
423 {
424         struct device_opp *dev_opp;
425         struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
426
427         opp_rcu_lockdep_assert();
428
429         if (!dev || !freq) {
430                 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
431                 return ERR_PTR(-EINVAL);
432         }
433
434         dev_opp = _find_device_opp(dev);
435         if (IS_ERR(dev_opp))
436                 return ERR_CAST(dev_opp);
437
438         list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
439                 if (temp_opp->available) {
440                         /* go to the next node, before choosing prev */
441                         if (temp_opp->rate > *freq)
442                                 break;
443                         else
444                                 opp = temp_opp;
445                 }
446         }
447         if (!IS_ERR(opp))
448                 *freq = opp->rate;
449
450         return opp;
451 }
452 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
453
454 /* List-dev Helpers */
455 static void _kfree_list_dev_rcu(struct rcu_head *head)
456 {
457         struct device_list_opp *list_dev;
458
459         list_dev = container_of(head, struct device_list_opp, rcu_head);
460         kfree_rcu(list_dev, rcu_head);
461 }
462
463 static void _remove_list_dev(struct device_list_opp *list_dev,
464                              struct device_opp *dev_opp)
465 {
466         opp_debug_unregister(list_dev, dev_opp);
467         list_del(&list_dev->node);
468         call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head,
469                   _kfree_list_dev_rcu);
470 }
471
472 struct device_list_opp *_add_list_dev(const struct device *dev,
473                                       struct device_opp *dev_opp)
474 {
475         struct device_list_opp *list_dev;
476         int ret;
477
478         list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL);
479         if (!list_dev)
480                 return NULL;
481
482         /* Initialize list-dev */
483         list_dev->dev = dev;
484         list_add_rcu(&list_dev->node, &dev_opp->dev_list);
485
486         /* Create debugfs entries for the dev_opp */
487         ret = opp_debug_register(list_dev, dev_opp);
488         if (ret)
489                 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
490                         __func__, ret);
491
492         return list_dev;
493 }
494
495 /**
496  * _add_device_opp() - Find device OPP table or allocate a new one
497  * @dev:        device for which we do this operation
498  *
499  * It tries to find an existing table first, if it couldn't find one, it
500  * allocates a new OPP table and returns that.
501  *
502  * Return: valid device_opp pointer if success, else NULL.
503  */
504 static struct device_opp *_add_device_opp(struct device *dev)
505 {
506         struct device_opp *dev_opp;
507         struct device_list_opp *list_dev;
508
509         /* Check for existing list for 'dev' first */
510         dev_opp = _find_device_opp(dev);
511         if (!IS_ERR(dev_opp))
512                 return dev_opp;
513
514         /*
515          * Allocate a new device OPP table. In the infrequent case where a new
516          * device is needed to be added, we pay this penalty.
517          */
518         dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
519         if (!dev_opp)
520                 return NULL;
521
522         INIT_LIST_HEAD(&dev_opp->dev_list);
523
524         list_dev = _add_list_dev(dev, dev_opp);
525         if (!list_dev) {
526                 kfree(dev_opp);
527                 return NULL;
528         }
529
530         srcu_init_notifier_head(&dev_opp->srcu_head);
531         INIT_LIST_HEAD(&dev_opp->opp_list);
532
533         /* Secure the device list modification */
534         list_add_rcu(&dev_opp->node, &dev_opp_list);
535         return dev_opp;
536 }
537
538 /**
539  * _kfree_device_rcu() - Free device_opp RCU handler
540  * @head:       RCU head
541  */
542 static void _kfree_device_rcu(struct rcu_head *head)
543 {
544         struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
545
546         kfree_rcu(device_opp, rcu_head);
547 }
548
549 /**
550  * _remove_device_opp() - Removes a device OPP table
551  * @dev_opp: device OPP table to be removed.
552  *
553  * Removes/frees device OPP table it it doesn't contain any OPPs.
554  */
555 static void _remove_device_opp(struct device_opp *dev_opp)
556 {
557         struct device_list_opp *list_dev;
558
559         if (!list_empty(&dev_opp->opp_list))
560                 return;
561
562         if (dev_opp->supported_hw)
563                 return;
564
565         list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp,
566                                     node);
567
568         _remove_list_dev(list_dev, dev_opp);
569
570         /* dev_list must be empty now */
571         WARN_ON(!list_empty(&dev_opp->dev_list));
572
573         list_del_rcu(&dev_opp->node);
574         call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
575                   _kfree_device_rcu);
576 }
577
578 /**
579  * _kfree_opp_rcu() - Free OPP RCU handler
580  * @head:       RCU head
581  */
582 static void _kfree_opp_rcu(struct rcu_head *head)
583 {
584         struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
585
586         kfree_rcu(opp, rcu_head);
587 }
588
589 /**
590  * _opp_remove()  - Remove an OPP from a table definition
591  * @dev_opp:    points back to the device_opp struct this opp belongs to
592  * @opp:        pointer to the OPP to remove
593  * @notify:     OPP_EVENT_REMOVE notification should be sent or not
594  *
595  * This function removes an opp definition from the opp list.
596  *
597  * Locking: The internal device_opp and opp structures are RCU protected.
598  * It is assumed that the caller holds required mutex for an RCU updater
599  * strategy.
600  */
601 static void _opp_remove(struct device_opp *dev_opp,
602                         struct dev_pm_opp *opp, bool notify)
603 {
604         /*
605          * Notify the changes in the availability of the operable
606          * frequency/voltage list.
607          */
608         if (notify)
609                 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
610         opp_debug_remove_one(opp);
611         list_del_rcu(&opp->node);
612         call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
613
614         _remove_device_opp(dev_opp);
615 }
616
617 /**
618  * dev_pm_opp_remove()  - Remove an OPP from OPP list
619  * @dev:        device for which we do this operation
620  * @freq:       OPP to remove with matching 'freq'
621  *
622  * This function removes an opp from the opp list.
623  *
624  * Locking: The internal device_opp and opp structures are RCU protected.
625  * Hence this function internally uses RCU updater strategy with mutex locks
626  * to keep the integrity of the internal data structures. Callers should ensure
627  * that this function is *NOT* called under RCU protection or in contexts where
628  * mutex cannot be locked.
629  */
630 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
631 {
632         struct dev_pm_opp *opp;
633         struct device_opp *dev_opp;
634         bool found = false;
635
636         /* Hold our list modification lock here */
637         mutex_lock(&dev_opp_list_lock);
638
639         dev_opp = _find_device_opp(dev);
640         if (IS_ERR(dev_opp))
641                 goto unlock;
642
643         list_for_each_entry(opp, &dev_opp->opp_list, node) {
644                 if (opp->rate == freq) {
645                         found = true;
646                         break;
647                 }
648         }
649
650         if (!found) {
651                 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
652                          __func__, freq);
653                 goto unlock;
654         }
655
656         _opp_remove(dev_opp, opp, true);
657 unlock:
658         mutex_unlock(&dev_opp_list_lock);
659 }
660 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
661
662 static struct dev_pm_opp *_allocate_opp(struct device *dev,
663                                         struct device_opp **dev_opp)
664 {
665         struct dev_pm_opp *opp;
666
667         /* allocate new OPP node */
668         opp = kzalloc(sizeof(*opp), GFP_KERNEL);
669         if (!opp)
670                 return NULL;
671
672         INIT_LIST_HEAD(&opp->node);
673
674         *dev_opp = _add_device_opp(dev);
675         if (!*dev_opp) {
676                 kfree(opp);
677                 return NULL;
678         }
679
680         return opp;
681 }
682
683 static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
684                     struct device_opp *dev_opp)
685 {
686         struct dev_pm_opp *opp;
687         struct list_head *head = &dev_opp->opp_list;
688         int ret;
689
690         /*
691          * Insert new OPP in order of increasing frequency and discard if
692          * already present.
693          *
694          * Need to use &dev_opp->opp_list in the condition part of the 'for'
695          * loop, don't replace it with head otherwise it will become an infinite
696          * loop.
697          */
698         list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
699                 if (new_opp->rate > opp->rate) {
700                         head = &opp->node;
701                         continue;
702                 }
703
704                 if (new_opp->rate < opp->rate)
705                         break;
706
707                 /* Duplicate OPPs */
708                 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
709                          __func__, opp->rate, opp->u_volt, opp->available,
710                          new_opp->rate, new_opp->u_volt, new_opp->available);
711
712                 return opp->available && new_opp->u_volt == opp->u_volt ?
713                         0 : -EEXIST;
714         }
715
716         new_opp->dev_opp = dev_opp;
717         list_add_rcu(&new_opp->node, head);
718
719         ret = opp_debug_create_one(new_opp, dev_opp);
720         if (ret)
721                 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
722                         __func__, ret);
723
724         return 0;
725 }
726
727 /**
728  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
729  * @dev:        device for which we do this operation
730  * @freq:       Frequency in Hz for this OPP
731  * @u_volt:     Voltage in uVolts for this OPP
732  * @dynamic:    Dynamically added OPPs.
733  *
734  * This function adds an opp definition to the opp list and returns status.
735  * The opp is made available by default and it can be controlled using
736  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
737  *
738  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
739  * and freed by dev_pm_opp_of_remove_table.
740  *
741  * Locking: The internal device_opp and opp structures are RCU protected.
742  * Hence this function internally uses RCU updater strategy with mutex locks
743  * to keep the integrity of the internal data structures. Callers should ensure
744  * that this function is *NOT* called under RCU protection or in contexts where
745  * mutex cannot be locked.
746  *
747  * Return:
748  * 0            On success OR
749  *              Duplicate OPPs (both freq and volt are same) and opp->available
750  * -EEXIST      Freq are same and volt are different OR
751  *              Duplicate OPPs (both freq and volt are same) and !opp->available
752  * -ENOMEM      Memory allocation failure
753  */
754 static int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
755                        bool dynamic)
756 {
757         struct device_opp *dev_opp;
758         struct dev_pm_opp *new_opp;
759         int ret;
760
761         /* Hold our list modification lock here */
762         mutex_lock(&dev_opp_list_lock);
763
764         new_opp = _allocate_opp(dev, &dev_opp);
765         if (!new_opp) {
766                 ret = -ENOMEM;
767                 goto unlock;
768         }
769
770         /* populate the opp table */
771         new_opp->rate = freq;
772         new_opp->u_volt = u_volt;
773         new_opp->available = true;
774         new_opp->dynamic = dynamic;
775
776         ret = _opp_add(dev, new_opp, dev_opp);
777         if (ret)
778                 goto free_opp;
779
780         mutex_unlock(&dev_opp_list_lock);
781
782         /*
783          * Notify the changes in the availability of the operable
784          * frequency/voltage list.
785          */
786         srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
787         return 0;
788
789 free_opp:
790         _opp_remove(dev_opp, new_opp, false);
791 unlock:
792         mutex_unlock(&dev_opp_list_lock);
793         return ret;
794 }
795
796 /* TODO: Support multiple regulators */
797 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev)
798 {
799         u32 microvolt[3] = {0};
800         u32 val;
801         int count, ret;
802
803         /* Missing property isn't a problem, but an invalid entry is */
804         if (!of_find_property(opp->np, "opp-microvolt", NULL))
805                 return 0;
806
807         count = of_property_count_u32_elems(opp->np, "opp-microvolt");
808         if (count < 0) {
809                 dev_err(dev, "%s: Invalid opp-microvolt property (%d)\n",
810                         __func__, count);
811                 return count;
812         }
813
814         /* There can be one or three elements here */
815         if (count != 1 && count != 3) {
816                 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
817                         __func__, count);
818                 return -EINVAL;
819         }
820
821         ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
822                                          count);
823         if (ret) {
824                 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
825                         ret);
826                 return -EINVAL;
827         }
828
829         opp->u_volt = microvolt[0];
830         opp->u_volt_min = microvolt[1];
831         opp->u_volt_max = microvolt[2];
832
833         if (!of_property_read_u32(opp->np, "opp-microamp", &val))
834                 opp->u_amp = val;
835
836         return 0;
837 }
838
839 /**
840  * dev_pm_opp_set_supported_hw() - Set supported platforms
841  * @dev: Device for which supported-hw has to be set.
842  * @versions: Array of hierarchy of versions to match.
843  * @count: Number of elements in the array.
844  *
845  * This is required only for the V2 bindings, and it enables a platform to
846  * specify the hierarchy of versions it supports. OPP layer will then enable
847  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
848  * property.
849  *
850  * Locking: The internal device_opp and opp structures are RCU protected.
851  * Hence this function internally uses RCU updater strategy with mutex locks
852  * to keep the integrity of the internal data structures. Callers should ensure
853  * that this function is *NOT* called under RCU protection or in contexts where
854  * mutex cannot be locked.
855  */
856 int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
857                                 unsigned int count)
858 {
859         struct device_opp *dev_opp;
860         int ret = 0;
861
862         /* Hold our list modification lock here */
863         mutex_lock(&dev_opp_list_lock);
864
865         dev_opp = _add_device_opp(dev);
866         if (!dev_opp) {
867                 ret = -ENOMEM;
868                 goto unlock;
869         }
870
871         /* Make sure there are no concurrent readers while updating dev_opp */
872         WARN_ON(!list_empty(&dev_opp->opp_list));
873
874         /* Do we already have a version hierarchy associated with dev_opp? */
875         if (dev_opp->supported_hw) {
876                 dev_err(dev, "%s: Already have supported hardware list\n",
877                         __func__);
878                 ret = -EBUSY;
879                 goto err;
880         }
881
882         dev_opp->supported_hw = kmemdup(versions, count * sizeof(*versions),
883                                         GFP_KERNEL);
884         if (!dev_opp->supported_hw) {
885                 ret = -ENOMEM;
886                 goto err;
887         }
888
889         dev_opp->supported_hw_count = count;
890         mutex_unlock(&dev_opp_list_lock);
891         return 0;
892
893 err:
894         _remove_device_opp(dev_opp);
895 unlock:
896         mutex_unlock(&dev_opp_list_lock);
897
898         return ret;
899 }
900 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
901
902 /**
903  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
904  * @dev: Device for which supported-hw has to be set.
905  *
906  * This is required only for the V2 bindings, and is called for a matching
907  * dev_pm_opp_set_supported_hw(). Until this is called, the device_opp structure
908  * will not be freed.
909  *
910  * Locking: The internal device_opp and opp structures are RCU protected.
911  * Hence this function internally uses RCU updater strategy with mutex locks
912  * to keep the integrity of the internal data structures. Callers should ensure
913  * that this function is *NOT* called under RCU protection or in contexts where
914  * mutex cannot be locked.
915  */
916 void dev_pm_opp_put_supported_hw(struct device *dev)
917 {
918         struct device_opp *dev_opp;
919
920         /* Hold our list modification lock here */
921         mutex_lock(&dev_opp_list_lock);
922
923         /* Check for existing list for 'dev' first */
924         dev_opp = _find_device_opp(dev);
925         if (IS_ERR(dev_opp)) {
926                 dev_err(dev, "Failed to find dev_opp: %ld\n", PTR_ERR(dev_opp));
927                 goto unlock;
928         }
929
930         /* Make sure there are no concurrent readers while updating dev_opp */
931         WARN_ON(!list_empty(&dev_opp->opp_list));
932
933         if (!dev_opp->supported_hw) {
934                 dev_err(dev, "%s: Doesn't have supported hardware list\n",
935                         __func__);
936                 goto unlock;
937         }
938
939         kfree(dev_opp->supported_hw);
940         dev_opp->supported_hw = NULL;
941         dev_opp->supported_hw_count = 0;
942
943         /* Try freeing device_opp if this was the last blocking resource */
944         _remove_device_opp(dev_opp);
945
946 unlock:
947         mutex_unlock(&dev_opp_list_lock);
948 }
949 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
950
951 static bool _opp_is_supported(struct device *dev, struct device_opp *dev_opp,
952                               struct device_node *np)
953 {
954         unsigned int count = dev_opp->supported_hw_count;
955         u32 version;
956         int ret;
957
958         if (!dev_opp->supported_hw)
959                 return true;
960
961         while (count--) {
962                 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
963                                                  &version);
964                 if (ret) {
965                         dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
966                                  __func__, count, ret);
967                         return false;
968                 }
969
970                 /* Both of these are bitwise masks of the versions */
971                 if (!(version & dev_opp->supported_hw[count]))
972                         return false;
973         }
974
975         return true;
976 }
977
978 /**
979  * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
980  * @dev:        device for which we do this operation
981  * @np:         device node
982  *
983  * This function adds an opp definition to the opp list and returns status. The
984  * opp can be controlled using dev_pm_opp_enable/disable functions and may be
985  * removed by dev_pm_opp_remove.
986  *
987  * Locking: The internal device_opp and opp structures are RCU protected.
988  * Hence this function internally uses RCU updater strategy with mutex locks
989  * to keep the integrity of the internal data structures. Callers should ensure
990  * that this function is *NOT* called under RCU protection or in contexts where
991  * mutex cannot be locked.
992  *
993  * Return:
994  * 0            On success OR
995  *              Duplicate OPPs (both freq and volt are same) and opp->available
996  * -EEXIST      Freq are same and volt are different OR
997  *              Duplicate OPPs (both freq and volt are same) and !opp->available
998  * -ENOMEM      Memory allocation failure
999  * -EINVAL      Failed parsing the OPP node
1000  */
1001 static int _opp_add_static_v2(struct device *dev, struct device_node *np)
1002 {
1003         struct device_opp *dev_opp;
1004         struct dev_pm_opp *new_opp;
1005         u64 rate;
1006         u32 val;
1007         int ret;
1008
1009         /* Hold our list modification lock here */
1010         mutex_lock(&dev_opp_list_lock);
1011
1012         new_opp = _allocate_opp(dev, &dev_opp);
1013         if (!new_opp) {
1014                 ret = -ENOMEM;
1015                 goto unlock;
1016         }
1017
1018         ret = of_property_read_u64(np, "opp-hz", &rate);
1019         if (ret < 0) {
1020                 dev_err(dev, "%s: opp-hz not found\n", __func__);
1021                 goto free_opp;
1022         }
1023
1024         /* Check if the OPP supports hardware's hierarchy of versions or not */
1025         if (!_opp_is_supported(dev, dev_opp, np)) {
1026                 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
1027                 goto free_opp;
1028         }
1029
1030         /*
1031          * Rate is defined as an unsigned long in clk API, and so casting
1032          * explicitly to its type. Must be fixed once rate is 64 bit
1033          * guaranteed in clk API.
1034          */
1035         new_opp->rate = (unsigned long)rate;
1036         new_opp->turbo = of_property_read_bool(np, "turbo-mode");
1037
1038         new_opp->np = np;
1039         new_opp->dynamic = false;
1040         new_opp->available = true;
1041
1042         if (!of_property_read_u32(np, "clock-latency-ns", &val))
1043                 new_opp->clock_latency_ns = val;
1044
1045         ret = opp_parse_supplies(new_opp, dev);
1046         if (ret)
1047                 goto free_opp;
1048
1049         ret = _opp_add(dev, new_opp, dev_opp);
1050         if (ret)
1051                 goto free_opp;
1052
1053         /* OPP to select on device suspend */
1054         if (of_property_read_bool(np, "opp-suspend")) {
1055                 if (dev_opp->suspend_opp) {
1056                         dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
1057                                  __func__, dev_opp->suspend_opp->rate,
1058                                  new_opp->rate);
1059                 } else {
1060                         new_opp->suspend = true;
1061                         dev_opp->suspend_opp = new_opp;
1062                 }
1063         }
1064
1065         if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max)
1066                 dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns;
1067
1068         mutex_unlock(&dev_opp_list_lock);
1069
1070         pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
1071                  __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
1072                  new_opp->u_volt_min, new_opp->u_volt_max,
1073                  new_opp->clock_latency_ns);
1074
1075         /*
1076          * Notify the changes in the availability of the operable
1077          * frequency/voltage list.
1078          */
1079         srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
1080         return 0;
1081
1082 free_opp:
1083         _opp_remove(dev_opp, new_opp, false);
1084 unlock:
1085         mutex_unlock(&dev_opp_list_lock);
1086         return ret;
1087 }
1088
1089 /**
1090  * dev_pm_opp_add()  - Add an OPP table from a table definitions
1091  * @dev:        device for which we do this operation
1092  * @freq:       Frequency in Hz for this OPP
1093  * @u_volt:     Voltage in uVolts for this OPP
1094  *
1095  * This function adds an opp definition to the opp list and returns status.
1096  * The opp is made available by default and it can be controlled using
1097  * dev_pm_opp_enable/disable functions.
1098  *
1099  * Locking: The internal device_opp and opp structures are RCU protected.
1100  * Hence this function internally uses RCU updater strategy with mutex locks
1101  * to keep the integrity of the internal data structures. Callers should ensure
1102  * that this function is *NOT* called under RCU protection or in contexts where
1103  * mutex cannot be locked.
1104  *
1105  * Return:
1106  * 0            On success OR
1107  *              Duplicate OPPs (both freq and volt are same) and opp->available
1108  * -EEXIST      Freq are same and volt are different OR
1109  *              Duplicate OPPs (both freq and volt are same) and !opp->available
1110  * -ENOMEM      Memory allocation failure
1111  */
1112 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1113 {
1114         return _opp_add_v1(dev, freq, u_volt, true);
1115 }
1116 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1117
1118 /**
1119  * _opp_set_availability() - helper to set the availability of an opp
1120  * @dev:                device for which we do this operation
1121  * @freq:               OPP frequency to modify availability
1122  * @availability_req:   availability status requested for this opp
1123  *
1124  * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1125  * share a common logic which is isolated here.
1126  *
1127  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1128  * copy operation, returns 0 if no modification was done OR modification was
1129  * successful.
1130  *
1131  * Locking: The internal device_opp and opp structures are RCU protected.
1132  * Hence this function internally uses RCU updater strategy with mutex locks to
1133  * keep the integrity of the internal data structures. Callers should ensure
1134  * that this function is *NOT* called under RCU protection or in contexts where
1135  * mutex locking or synchronize_rcu() blocking calls cannot be used.
1136  */
1137 static int _opp_set_availability(struct device *dev, unsigned long freq,
1138                                  bool availability_req)
1139 {
1140         struct device_opp *dev_opp;
1141         struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
1142         int r = 0;
1143
1144         /* keep the node allocated */
1145         new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
1146         if (!new_opp)
1147                 return -ENOMEM;
1148
1149         mutex_lock(&dev_opp_list_lock);
1150
1151         /* Find the device_opp */
1152         dev_opp = _find_device_opp(dev);
1153         if (IS_ERR(dev_opp)) {
1154                 r = PTR_ERR(dev_opp);
1155                 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1156                 goto unlock;
1157         }
1158
1159         /* Do we have the frequency? */
1160         list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
1161                 if (tmp_opp->rate == freq) {
1162                         opp = tmp_opp;
1163                         break;
1164                 }
1165         }
1166         if (IS_ERR(opp)) {
1167                 r = PTR_ERR(opp);
1168                 goto unlock;
1169         }
1170
1171         /* Is update really needed? */
1172         if (opp->available == availability_req)
1173                 goto unlock;
1174         /* copy the old data over */
1175         *new_opp = *opp;
1176
1177         /* plug in new node */
1178         new_opp->available = availability_req;
1179
1180         list_replace_rcu(&opp->node, &new_opp->node);
1181         mutex_unlock(&dev_opp_list_lock);
1182         call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
1183
1184         /* Notify the change of the OPP availability */
1185         if (availability_req)
1186                 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
1187                                          new_opp);
1188         else
1189                 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
1190                                          new_opp);
1191
1192         return 0;
1193
1194 unlock:
1195         mutex_unlock(&dev_opp_list_lock);
1196         kfree(new_opp);
1197         return r;
1198 }
1199
1200 /**
1201  * dev_pm_opp_enable() - Enable a specific OPP
1202  * @dev:        device for which we do this operation
1203  * @freq:       OPP frequency to enable
1204  *
1205  * Enables a provided opp. If the operation is valid, this returns 0, else the
1206  * corresponding error value. It is meant to be used for users an OPP available
1207  * after being temporarily made unavailable with dev_pm_opp_disable.
1208  *
1209  * Locking: The internal device_opp and opp structures are RCU protected.
1210  * Hence this function indirectly uses RCU and mutex locks to keep the
1211  * integrity of the internal data structures. Callers should ensure that
1212  * this function is *NOT* called under RCU protection or in contexts where
1213  * mutex locking or synchronize_rcu() blocking calls cannot be used.
1214  *
1215  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1216  * copy operation, returns 0 if no modification was done OR modification was
1217  * successful.
1218  */
1219 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1220 {
1221         return _opp_set_availability(dev, freq, true);
1222 }
1223 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1224
1225 /**
1226  * dev_pm_opp_disable() - Disable a specific OPP
1227  * @dev:        device for which we do this operation
1228  * @freq:       OPP frequency to disable
1229  *
1230  * Disables a provided opp. If the operation is valid, this returns
1231  * 0, else the corresponding error value. It is meant to be a temporary
1232  * control by users to make this OPP not available until the circumstances are
1233  * right to make it available again (with a call to dev_pm_opp_enable).
1234  *
1235  * Locking: The internal device_opp and opp structures are RCU protected.
1236  * Hence this function indirectly uses RCU and mutex locks to keep the
1237  * integrity of the internal data structures. Callers should ensure that
1238  * this function is *NOT* called under RCU protection or in contexts where
1239  * mutex locking or synchronize_rcu() blocking calls cannot be used.
1240  *
1241  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1242  * copy operation, returns 0 if no modification was done OR modification was
1243  * successful.
1244  */
1245 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1246 {
1247         return _opp_set_availability(dev, freq, false);
1248 }
1249 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1250
1251 /**
1252  * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
1253  * @dev:        device pointer used to lookup device OPPs.
1254  *
1255  * Return: pointer to  notifier head if found, otherwise -ENODEV or
1256  * -EINVAL based on type of error casted as pointer. value must be checked
1257  *  with IS_ERR to determine valid pointer or error result.
1258  *
1259  * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
1260  * protected pointer. The reason for the same is that the opp pointer which is
1261  * returned will remain valid for use with opp_get_{voltage, freq} only while
1262  * under the locked area. The pointer returned must be used prior to unlocking
1263  * with rcu_read_unlock() to maintain the integrity of the pointer.
1264  */
1265 struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
1266 {
1267         struct device_opp *dev_opp = _find_device_opp(dev);
1268
1269         if (IS_ERR(dev_opp))
1270                 return ERR_CAST(dev_opp); /* matching type */
1271
1272         return &dev_opp->srcu_head;
1273 }
1274 EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
1275
1276 #ifdef CONFIG_OF
1277 /**
1278  * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
1279  *                                entries
1280  * @dev:        device pointer used to lookup device OPPs.
1281  *
1282  * Free OPPs created using static entries present in DT.
1283  *
1284  * Locking: The internal device_opp and opp structures are RCU protected.
1285  * Hence this function indirectly uses RCU updater strategy with mutex locks
1286  * to keep the integrity of the internal data structures. Callers should ensure
1287  * that this function is *NOT* called under RCU protection or in contexts where
1288  * mutex cannot be locked.
1289  */
1290 void dev_pm_opp_of_remove_table(struct device *dev)
1291 {
1292         struct device_opp *dev_opp;
1293         struct dev_pm_opp *opp, *tmp;
1294
1295         /* Hold our list modification lock here */
1296         mutex_lock(&dev_opp_list_lock);
1297
1298         /* Check for existing list for 'dev' */
1299         dev_opp = _find_device_opp(dev);
1300         if (IS_ERR(dev_opp)) {
1301                 int error = PTR_ERR(dev_opp);
1302
1303                 if (error != -ENODEV)
1304                         WARN(1, "%s: dev_opp: %d\n",
1305                              IS_ERR_OR_NULL(dev) ?
1306                                         "Invalid device" : dev_name(dev),
1307                              error);
1308                 goto unlock;
1309         }
1310
1311         /* Find if dev_opp manages a single device */
1312         if (list_is_singular(&dev_opp->dev_list)) {
1313                 /* Free static OPPs */
1314                 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1315                         if (!opp->dynamic)
1316                                 _opp_remove(dev_opp, opp, true);
1317                 }
1318         } else {
1319                 _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp);
1320         }
1321
1322 unlock:
1323         mutex_unlock(&dev_opp_list_lock);
1324 }
1325 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
1326
1327 /* Returns opp descriptor node for a device, caller must do of_node_put() */
1328 struct device_node *_of_get_opp_desc_node(struct device *dev)
1329 {
1330         /*
1331          * TODO: Support for multiple OPP tables.
1332          *
1333          * There should be only ONE phandle present in "operating-points-v2"
1334          * property.
1335          */
1336
1337         return of_parse_phandle(dev->of_node, "operating-points-v2", 0);
1338 }
1339
1340 /* Initializes OPP tables based on new bindings */
1341 static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
1342 {
1343         struct device_node *np;
1344         struct device_opp *dev_opp;
1345         int ret = 0, count = 0;
1346
1347         mutex_lock(&dev_opp_list_lock);
1348
1349         dev_opp = _managed_opp(opp_np);
1350         if (dev_opp) {
1351                 /* OPPs are already managed */
1352                 if (!_add_list_dev(dev, dev_opp))
1353                         ret = -ENOMEM;
1354                 mutex_unlock(&dev_opp_list_lock);
1355                 return ret;
1356         }
1357         mutex_unlock(&dev_opp_list_lock);
1358
1359         /* We have opp-list node now, iterate over it and add OPPs */
1360         for_each_available_child_of_node(opp_np, np) {
1361                 count++;
1362
1363                 ret = _opp_add_static_v2(dev, np);
1364                 if (ret) {
1365                         dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1366                                 ret);
1367                         goto free_table;
1368                 }
1369         }
1370
1371         /* There should be one of more OPP defined */
1372         if (WARN_ON(!count))
1373                 return -ENOENT;
1374
1375         mutex_lock(&dev_opp_list_lock);
1376
1377         dev_opp = _find_device_opp(dev);
1378         if (WARN_ON(IS_ERR(dev_opp))) {
1379                 ret = PTR_ERR(dev_opp);
1380                 mutex_unlock(&dev_opp_list_lock);
1381                 goto free_table;
1382         }
1383
1384         dev_opp->np = opp_np;
1385         dev_opp->shared_opp = of_property_read_bool(opp_np, "opp-shared");
1386
1387         mutex_unlock(&dev_opp_list_lock);
1388
1389         return 0;
1390
1391 free_table:
1392         dev_pm_opp_of_remove_table(dev);
1393
1394         return ret;
1395 }
1396
1397 /* Initializes OPP tables based on old-deprecated bindings */
1398 static int _of_add_opp_table_v1(struct device *dev)
1399 {
1400         const struct property *prop;
1401         const __be32 *val;
1402         int nr;
1403
1404         prop = of_find_property(dev->of_node, "operating-points", NULL);
1405         if (!prop)
1406                 return -ENODEV;
1407         if (!prop->value)
1408                 return -ENODATA;
1409
1410         /*
1411          * Each OPP is a set of tuples consisting of frequency and
1412          * voltage like <freq-kHz vol-uV>.
1413          */
1414         nr = prop->length / sizeof(u32);
1415         if (nr % 2) {
1416                 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1417                 return -EINVAL;
1418         }
1419
1420         val = prop->value;
1421         while (nr) {
1422                 unsigned long freq = be32_to_cpup(val++) * 1000;
1423                 unsigned long volt = be32_to_cpup(val++);
1424
1425                 if (_opp_add_v1(dev, freq, volt, false))
1426                         dev_warn(dev, "%s: Failed to add OPP %ld\n",
1427                                  __func__, freq);
1428                 nr -= 2;
1429         }
1430
1431         return 0;
1432 }
1433
1434 /**
1435  * dev_pm_opp_of_add_table() - Initialize opp table from device tree
1436  * @dev:        device pointer used to lookup device OPPs.
1437  *
1438  * Register the initial OPP table with the OPP library for given device.
1439  *
1440  * Locking: The internal device_opp and opp structures are RCU protected.
1441  * Hence this function indirectly uses RCU updater strategy with mutex locks
1442  * to keep the integrity of the internal data structures. Callers should ensure
1443  * that this function is *NOT* called under RCU protection or in contexts where
1444  * mutex cannot be locked.
1445  *
1446  * Return:
1447  * 0            On success OR
1448  *              Duplicate OPPs (both freq and volt are same) and opp->available
1449  * -EEXIST      Freq are same and volt are different OR
1450  *              Duplicate OPPs (both freq and volt are same) and !opp->available
1451  * -ENOMEM      Memory allocation failure
1452  * -ENODEV      when 'operating-points' property is not found or is invalid data
1453  *              in device node.
1454  * -ENODATA     when empty 'operating-points' property is found
1455  * -EINVAL      when invalid entries are found in opp-v2 table
1456  */
1457 int dev_pm_opp_of_add_table(struct device *dev)
1458 {
1459         struct device_node *opp_np;
1460         int ret;
1461
1462         /*
1463          * OPPs have two version of bindings now. The older one is deprecated,
1464          * try for the new binding first.
1465          */
1466         opp_np = _of_get_opp_desc_node(dev);
1467         if (!opp_np) {
1468                 /*
1469                  * Try old-deprecated bindings for backward compatibility with
1470                  * older dtbs.
1471                  */
1472                 return _of_add_opp_table_v1(dev);
1473         }
1474
1475         ret = _of_add_opp_table_v2(dev, opp_np);
1476         of_node_put(opp_np);
1477
1478         return ret;
1479 }
1480 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
1481 #endif