xref: /linux/drivers/opp/core.c (revision fba2689ee77e63b05e203b3f26079ef915e55660)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic OPP Interface
4  *
5  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
6  *	Nishanth Menon
7  *	Romit Dasgupta
8  *	Kevin Hilman
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/clk.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/export.h>
19 #include <linux/pm_domain.h>
20 #include <linux/regulator/consumer.h>
21 
22 #include "opp.h"
23 
24 /*
25  * The root of the list of all opp-tables. All opp_table structures branch off
26  * from here, with each opp_table containing the list of opps it supports in
27  * various states of availability.
28  */
29 LIST_HEAD(opp_tables);
30 
31 /* OPP tables with uninitialized required OPPs */
32 LIST_HEAD(lazy_opp_tables);
33 
34 /* Lock to allow exclusive modification to the device and opp lists */
35 DEFINE_MUTEX(opp_table_lock);
36 /* Flag indicating that opp_tables list is being updated at the moment */
37 static bool opp_tables_busy;
38 
39 static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
40 {
41 	struct opp_device *opp_dev;
42 	bool found = false;
43 
44 	mutex_lock(&opp_table->lock);
45 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
46 		if (opp_dev->dev == dev) {
47 			found = true;
48 			break;
49 		}
50 
51 	mutex_unlock(&opp_table->lock);
52 	return found;
53 }
54 
55 static struct opp_table *_find_opp_table_unlocked(struct device *dev)
56 {
57 	struct opp_table *opp_table;
58 
59 	list_for_each_entry(opp_table, &opp_tables, node) {
60 		if (_find_opp_dev(dev, opp_table)) {
61 			_get_opp_table_kref(opp_table);
62 			return opp_table;
63 		}
64 	}
65 
66 	return ERR_PTR(-ENODEV);
67 }
68 
69 /**
70  * _find_opp_table() - find opp_table struct using device pointer
71  * @dev:	device pointer used to lookup OPP table
72  *
73  * Search OPP table for one containing matching device.
74  *
75  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
76  * -EINVAL based on type of error.
77  *
78  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
79  */
80 struct opp_table *_find_opp_table(struct device *dev)
81 {
82 	struct opp_table *opp_table;
83 
84 	if (IS_ERR_OR_NULL(dev)) {
85 		pr_err("%s: Invalid parameters\n", __func__);
86 		return ERR_PTR(-EINVAL);
87 	}
88 
89 	mutex_lock(&opp_table_lock);
90 	opp_table = _find_opp_table_unlocked(dev);
91 	mutex_unlock(&opp_table_lock);
92 
93 	return opp_table;
94 }
95 
96 /**
97  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
98  * @opp:	opp for which voltage has to be returned for
99  *
100  * Return: voltage in micro volt corresponding to the opp, else
101  * return 0
102  *
103  * This is useful only for devices with single power supply.
104  */
105 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
106 {
107 	if (IS_ERR_OR_NULL(opp)) {
108 		pr_err("%s: Invalid parameters\n", __func__);
109 		return 0;
110 	}
111 
112 	return opp->supplies[0].u_volt;
113 }
114 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
115 
116 /**
117  * dev_pm_opp_get_power() - Gets the power corresponding to an opp
118  * @opp:	opp for which power has to be returned for
119  *
120  * Return: power in micro watt corresponding to the opp, else
121  * return 0
122  *
123  * This is useful only for devices with single power supply.
124  */
125 unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
126 {
127 	unsigned long opp_power = 0;
128 	int i;
129 
130 	if (IS_ERR_OR_NULL(opp)) {
131 		pr_err("%s: Invalid parameters\n", __func__);
132 		return 0;
133 	}
134 	for (i = 0; i < opp->opp_table->regulator_count; i++)
135 		opp_power += opp->supplies[i].u_watt;
136 
137 	return opp_power;
138 }
139 EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
140 
141 /**
142  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
143  * @opp:	opp for which frequency has to be returned for
144  *
145  * Return: frequency in hertz corresponding to the opp, else
146  * return 0
147  */
148 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
149 {
150 	if (IS_ERR_OR_NULL(opp)) {
151 		pr_err("%s: Invalid parameters\n", __func__);
152 		return 0;
153 	}
154 
155 	return opp->rate;
156 }
157 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
158 
159 /**
160  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
161  * @opp:	opp for which level value has to be returned for
162  *
163  * Return: level read from device tree corresponding to the opp, else
164  * return 0.
165  */
166 unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
167 {
168 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
169 		pr_err("%s: Invalid parameters\n", __func__);
170 		return 0;
171 	}
172 
173 	return opp->level;
174 }
175 EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
176 
177 /**
178  * dev_pm_opp_get_required_pstate() - Gets the required performance state
179  *                                    corresponding to an available opp
180  * @opp:	opp for which performance state has to be returned for
181  * @index:	index of the required opp
182  *
183  * Return: performance state read from device tree corresponding to the
184  * required opp, else return 0.
185  */
186 unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
187 					    unsigned int index)
188 {
189 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
190 	    index >= opp->opp_table->required_opp_count) {
191 		pr_err("%s: Invalid parameters\n", __func__);
192 		return 0;
193 	}
194 
195 	/* required-opps not fully initialized yet */
196 	if (lazy_linking_pending(opp->opp_table))
197 		return 0;
198 
199 	return opp->required_opps[index]->pstate;
200 }
201 EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
202 
203 /**
204  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
205  * @opp: opp for which turbo mode is being verified
206  *
207  * Turbo OPPs are not for normal use, and can be enabled (under certain
208  * conditions) for short duration of times to finish high throughput work
209  * quickly. Running on them for longer times may overheat the chip.
210  *
211  * Return: true if opp is turbo opp, else false.
212  */
213 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
214 {
215 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
216 		pr_err("%s: Invalid parameters\n", __func__);
217 		return false;
218 	}
219 
220 	return opp->turbo;
221 }
222 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
223 
224 /**
225  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
226  * @dev:	device for which we do this operation
227  *
228  * Return: This function returns the max clock latency in nanoseconds.
229  */
230 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
231 {
232 	struct opp_table *opp_table;
233 	unsigned long clock_latency_ns;
234 
235 	opp_table = _find_opp_table(dev);
236 	if (IS_ERR(opp_table))
237 		return 0;
238 
239 	clock_latency_ns = opp_table->clock_latency_ns_max;
240 
241 	dev_pm_opp_put_opp_table(opp_table);
242 
243 	return clock_latency_ns;
244 }
245 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
246 
247 /**
248  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
249  * @dev: device for which we do this operation
250  *
251  * Return: This function returns the max voltage latency in nanoseconds.
252  */
253 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
254 {
255 	struct opp_table *opp_table;
256 	struct dev_pm_opp *opp;
257 	struct regulator *reg;
258 	unsigned long latency_ns = 0;
259 	int ret, i, count;
260 	struct {
261 		unsigned long min;
262 		unsigned long max;
263 	} *uV;
264 
265 	opp_table = _find_opp_table(dev);
266 	if (IS_ERR(opp_table))
267 		return 0;
268 
269 	/* Regulator may not be required for the device */
270 	if (!opp_table->regulators)
271 		goto put_opp_table;
272 
273 	count = opp_table->regulator_count;
274 
275 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
276 	if (!uV)
277 		goto put_opp_table;
278 
279 	mutex_lock(&opp_table->lock);
280 
281 	for (i = 0; i < count; i++) {
282 		uV[i].min = ~0;
283 		uV[i].max = 0;
284 
285 		list_for_each_entry(opp, &opp_table->opp_list, node) {
286 			if (!opp->available)
287 				continue;
288 
289 			if (opp->supplies[i].u_volt_min < uV[i].min)
290 				uV[i].min = opp->supplies[i].u_volt_min;
291 			if (opp->supplies[i].u_volt_max > uV[i].max)
292 				uV[i].max = opp->supplies[i].u_volt_max;
293 		}
294 	}
295 
296 	mutex_unlock(&opp_table->lock);
297 
298 	/*
299 	 * The caller needs to ensure that opp_table (and hence the regulator)
300 	 * isn't freed, while we are executing this routine.
301 	 */
302 	for (i = 0; i < count; i++) {
303 		reg = opp_table->regulators[i];
304 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
305 		if (ret > 0)
306 			latency_ns += ret * 1000;
307 	}
308 
309 	kfree(uV);
310 put_opp_table:
311 	dev_pm_opp_put_opp_table(opp_table);
312 
313 	return latency_ns;
314 }
315 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
316 
317 /**
318  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
319  *					     nanoseconds
320  * @dev: device for which we do this operation
321  *
322  * Return: This function returns the max transition latency, in nanoseconds, to
323  * switch from one OPP to other.
324  */
325 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
326 {
327 	return dev_pm_opp_get_max_volt_latency(dev) +
328 		dev_pm_opp_get_max_clock_latency(dev);
329 }
330 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
331 
332 /**
333  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
334  * @dev:	device for which we do this operation
335  *
336  * Return: This function returns the frequency of the OPP marked as suspend_opp
337  * if one is available, else returns 0;
338  */
339 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
340 {
341 	struct opp_table *opp_table;
342 	unsigned long freq = 0;
343 
344 	opp_table = _find_opp_table(dev);
345 	if (IS_ERR(opp_table))
346 		return 0;
347 
348 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
349 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
350 
351 	dev_pm_opp_put_opp_table(opp_table);
352 
353 	return freq;
354 }
355 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
356 
357 int _get_opp_count(struct opp_table *opp_table)
358 {
359 	struct dev_pm_opp *opp;
360 	int count = 0;
361 
362 	mutex_lock(&opp_table->lock);
363 
364 	list_for_each_entry(opp, &opp_table->opp_list, node) {
365 		if (opp->available)
366 			count++;
367 	}
368 
369 	mutex_unlock(&opp_table->lock);
370 
371 	return count;
372 }
373 
374 /**
375  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
376  * @dev:	device for which we do this operation
377  *
378  * Return: This function returns the number of available opps if there are any,
379  * else returns 0 if none or the corresponding error value.
380  */
381 int dev_pm_opp_get_opp_count(struct device *dev)
382 {
383 	struct opp_table *opp_table;
384 	int count;
385 
386 	opp_table = _find_opp_table(dev);
387 	if (IS_ERR(opp_table)) {
388 		count = PTR_ERR(opp_table);
389 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
390 			__func__, count);
391 		return count;
392 	}
393 
394 	count = _get_opp_count(opp_table);
395 	dev_pm_opp_put_opp_table(opp_table);
396 
397 	return count;
398 }
399 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
400 
401 /**
402  * dev_pm_opp_find_freq_exact() - search for an exact frequency
403  * @dev:		device for which we do this operation
404  * @freq:		frequency to search for
405  * @available:		true/false - match for available opp
406  *
407  * Return: Searches for exact match in the opp table and returns pointer to the
408  * matching opp if found, else returns ERR_PTR in case of error and should
409  * be handled using IS_ERR. Error return values can be:
410  * EINVAL:	for bad pointer
411  * ERANGE:	no match found for search
412  * ENODEV:	if device not found in list of registered devices
413  *
414  * Note: available is a modifier for the search. if available=true, then the
415  * match is for exact matching frequency and is available in the stored OPP
416  * table. if false, the match is for exact frequency which is not available.
417  *
418  * This provides a mechanism to enable an opp which is not available currently
419  * or the opposite as well.
420  *
421  * The callers are required to call dev_pm_opp_put() for the returned OPP after
422  * use.
423  */
424 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
425 					      unsigned long freq,
426 					      bool available)
427 {
428 	struct opp_table *opp_table;
429 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
430 
431 	opp_table = _find_opp_table(dev);
432 	if (IS_ERR(opp_table)) {
433 		int r = PTR_ERR(opp_table);
434 
435 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
436 		return ERR_PTR(r);
437 	}
438 
439 	mutex_lock(&opp_table->lock);
440 
441 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
442 		if (temp_opp->available == available &&
443 				temp_opp->rate == freq) {
444 			opp = temp_opp;
445 
446 			/* Increment the reference count of OPP */
447 			dev_pm_opp_get(opp);
448 			break;
449 		}
450 	}
451 
452 	mutex_unlock(&opp_table->lock);
453 	dev_pm_opp_put_opp_table(opp_table);
454 
455 	return opp;
456 }
457 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
458 
459 /**
460  * dev_pm_opp_find_level_exact() - search for an exact level
461  * @dev:		device for which we do this operation
462  * @level:		level to search for
463  *
464  * Return: Searches for exact match in the opp table and returns pointer to the
465  * matching opp if found, else returns ERR_PTR in case of error and should
466  * be handled using IS_ERR. Error return values can be:
467  * EINVAL:	for bad pointer
468  * ERANGE:	no match found for search
469  * ENODEV:	if device not found in list of registered devices
470  *
471  * The callers are required to call dev_pm_opp_put() for the returned OPP after
472  * use.
473  */
474 struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
475 					       unsigned int level)
476 {
477 	struct opp_table *opp_table;
478 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
479 
480 	opp_table = _find_opp_table(dev);
481 	if (IS_ERR(opp_table)) {
482 		int r = PTR_ERR(opp_table);
483 
484 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
485 		return ERR_PTR(r);
486 	}
487 
488 	mutex_lock(&opp_table->lock);
489 
490 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
491 		if (temp_opp->level == level) {
492 			opp = temp_opp;
493 
494 			/* Increment the reference count of OPP */
495 			dev_pm_opp_get(opp);
496 			break;
497 		}
498 	}
499 
500 	mutex_unlock(&opp_table->lock);
501 	dev_pm_opp_put_opp_table(opp_table);
502 
503 	return opp;
504 }
505 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
506 
507 /**
508  * dev_pm_opp_find_level_ceil() - search for an rounded up level
509  * @dev:		device for which we do this operation
510  * @level:		level to search for
511  *
512  * Return: Searches for rounded up match in the opp table and returns pointer
513  * to the  matching opp if found, else returns ERR_PTR in case of error and
514  * should be handled using IS_ERR. Error return values can be:
515  * EINVAL:	for bad pointer
516  * ERANGE:	no match found for search
517  * ENODEV:	if device not found in list of registered devices
518  *
519  * The callers are required to call dev_pm_opp_put() for the returned OPP after
520  * use.
521  */
522 struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
523 					      unsigned int *level)
524 {
525 	struct opp_table *opp_table;
526 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
527 
528 	opp_table = _find_opp_table(dev);
529 	if (IS_ERR(opp_table)) {
530 		int r = PTR_ERR(opp_table);
531 
532 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
533 		return ERR_PTR(r);
534 	}
535 
536 	mutex_lock(&opp_table->lock);
537 
538 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
539 		if (temp_opp->available && temp_opp->level >= *level) {
540 			opp = temp_opp;
541 			*level = opp->level;
542 
543 			/* Increment the reference count of OPP */
544 			dev_pm_opp_get(opp);
545 			break;
546 		}
547 	}
548 
549 	mutex_unlock(&opp_table->lock);
550 	dev_pm_opp_put_opp_table(opp_table);
551 
552 	return opp;
553 }
554 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
555 
556 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
557 						   unsigned long *freq)
558 {
559 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
560 
561 	mutex_lock(&opp_table->lock);
562 
563 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
564 		if (temp_opp->available && temp_opp->rate >= *freq) {
565 			opp = temp_opp;
566 			*freq = opp->rate;
567 
568 			/* Increment the reference count of OPP */
569 			dev_pm_opp_get(opp);
570 			break;
571 		}
572 	}
573 
574 	mutex_unlock(&opp_table->lock);
575 
576 	return opp;
577 }
578 
579 /**
580  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
581  * @dev:	device for which we do this operation
582  * @freq:	Start frequency
583  *
584  * Search for the matching ceil *available* OPP from a starting freq
585  * for a device.
586  *
587  * Return: matching *opp and refreshes *freq accordingly, else returns
588  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
589  * values can be:
590  * EINVAL:	for bad pointer
591  * ERANGE:	no match found for search
592  * ENODEV:	if device not found in list of registered devices
593  *
594  * The callers are required to call dev_pm_opp_put() for the returned OPP after
595  * use.
596  */
597 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
598 					     unsigned long *freq)
599 {
600 	struct opp_table *opp_table;
601 	struct dev_pm_opp *opp;
602 
603 	if (!dev || !freq) {
604 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
605 		return ERR_PTR(-EINVAL);
606 	}
607 
608 	opp_table = _find_opp_table(dev);
609 	if (IS_ERR(opp_table))
610 		return ERR_CAST(opp_table);
611 
612 	opp = _find_freq_ceil(opp_table, freq);
613 
614 	dev_pm_opp_put_opp_table(opp_table);
615 
616 	return opp;
617 }
618 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
619 
620 /**
621  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
622  * @dev:	device for which we do this operation
623  * @freq:	Start frequency
624  *
625  * Search for the matching floor *available* OPP from a starting freq
626  * for a device.
627  *
628  * Return: matching *opp and refreshes *freq accordingly, else returns
629  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
630  * values can be:
631  * EINVAL:	for bad pointer
632  * ERANGE:	no match found for search
633  * ENODEV:	if device not found in list of registered devices
634  *
635  * The callers are required to call dev_pm_opp_put() for the returned OPP after
636  * use.
637  */
638 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
639 					      unsigned long *freq)
640 {
641 	struct opp_table *opp_table;
642 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
643 
644 	if (!dev || !freq) {
645 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
646 		return ERR_PTR(-EINVAL);
647 	}
648 
649 	opp_table = _find_opp_table(dev);
650 	if (IS_ERR(opp_table))
651 		return ERR_CAST(opp_table);
652 
653 	mutex_lock(&opp_table->lock);
654 
655 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
656 		if (temp_opp->available) {
657 			/* go to the next node, before choosing prev */
658 			if (temp_opp->rate > *freq)
659 				break;
660 			else
661 				opp = temp_opp;
662 		}
663 	}
664 
665 	/* Increment the reference count of OPP */
666 	if (!IS_ERR(opp))
667 		dev_pm_opp_get(opp);
668 	mutex_unlock(&opp_table->lock);
669 	dev_pm_opp_put_opp_table(opp_table);
670 
671 	if (!IS_ERR(opp))
672 		*freq = opp->rate;
673 
674 	return opp;
675 }
676 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
677 
678 /**
679  * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
680  *					 target voltage.
681  * @dev:	Device for which we do this operation.
682  * @u_volt:	Target voltage.
683  *
684  * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
685  *
686  * Return: matching *opp, else returns ERR_PTR in case of error which should be
687  * handled using IS_ERR.
688  *
689  * Error return values can be:
690  * EINVAL:	bad parameters
691  *
692  * The callers are required to call dev_pm_opp_put() for the returned OPP after
693  * use.
694  */
695 struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
696 						     unsigned long u_volt)
697 {
698 	struct opp_table *opp_table;
699 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
700 
701 	if (!dev || !u_volt) {
702 		dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
703 			u_volt);
704 		return ERR_PTR(-EINVAL);
705 	}
706 
707 	opp_table = _find_opp_table(dev);
708 	if (IS_ERR(opp_table))
709 		return ERR_CAST(opp_table);
710 
711 	mutex_lock(&opp_table->lock);
712 
713 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
714 		if (temp_opp->available) {
715 			if (temp_opp->supplies[0].u_volt > u_volt)
716 				break;
717 			opp = temp_opp;
718 		}
719 	}
720 
721 	/* Increment the reference count of OPP */
722 	if (!IS_ERR(opp))
723 		dev_pm_opp_get(opp);
724 
725 	mutex_unlock(&opp_table->lock);
726 	dev_pm_opp_put_opp_table(opp_table);
727 
728 	return opp;
729 }
730 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
731 
732 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
733 			    struct dev_pm_opp_supply *supply)
734 {
735 	int ret;
736 
737 	/* Regulator not available for device */
738 	if (IS_ERR(reg)) {
739 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
740 			PTR_ERR(reg));
741 		return 0;
742 	}
743 
744 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
745 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
746 
747 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
748 					    supply->u_volt, supply->u_volt_max);
749 	if (ret)
750 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
751 			__func__, supply->u_volt_min, supply->u_volt,
752 			supply->u_volt_max, ret);
753 
754 	return ret;
755 }
756 
757 static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
758 					    unsigned long freq)
759 {
760 	int ret;
761 
762 	/* We may reach here for devices which don't change frequency */
763 	if (IS_ERR(clk))
764 		return 0;
765 
766 	ret = clk_set_rate(clk, freq);
767 	if (ret) {
768 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
769 			ret);
770 	}
771 
772 	return ret;
773 }
774 
775 static int _generic_set_opp_regulator(struct opp_table *opp_table,
776 				      struct device *dev,
777 				      struct dev_pm_opp *opp,
778 				      unsigned long freq,
779 				      int scaling_down)
780 {
781 	struct regulator *reg = opp_table->regulators[0];
782 	struct dev_pm_opp *old_opp = opp_table->current_opp;
783 	int ret;
784 
785 	/* This function only supports single regulator per device */
786 	if (WARN_ON(opp_table->regulator_count > 1)) {
787 		dev_err(dev, "multiple regulators are not supported\n");
788 		return -EINVAL;
789 	}
790 
791 	/* Scaling up? Scale voltage before frequency */
792 	if (!scaling_down) {
793 		ret = _set_opp_voltage(dev, reg, opp->supplies);
794 		if (ret)
795 			goto restore_voltage;
796 	}
797 
798 	/* Change frequency */
799 	ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
800 	if (ret)
801 		goto restore_voltage;
802 
803 	/* Scaling down? Scale voltage after frequency */
804 	if (scaling_down) {
805 		ret = _set_opp_voltage(dev, reg, opp->supplies);
806 		if (ret)
807 			goto restore_freq;
808 	}
809 
810 	/*
811 	 * Enable the regulator after setting its voltages, otherwise it breaks
812 	 * some boot-enabled regulators.
813 	 */
814 	if (unlikely(!opp_table->enabled)) {
815 		ret = regulator_enable(reg);
816 		if (ret < 0)
817 			dev_warn(dev, "Failed to enable regulator: %d", ret);
818 	}
819 
820 	return 0;
821 
822 restore_freq:
823 	if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate))
824 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
825 			__func__, old_opp->rate);
826 restore_voltage:
827 	/* This shouldn't harm even if the voltages weren't updated earlier */
828 	_set_opp_voltage(dev, reg, old_opp->supplies);
829 
830 	return ret;
831 }
832 
833 static int _set_opp_bw(const struct opp_table *opp_table,
834 		       struct dev_pm_opp *opp, struct device *dev)
835 {
836 	u32 avg, peak;
837 	int i, ret;
838 
839 	if (!opp_table->paths)
840 		return 0;
841 
842 	for (i = 0; i < opp_table->path_count; i++) {
843 		if (!opp) {
844 			avg = 0;
845 			peak = 0;
846 		} else {
847 			avg = opp->bandwidth[i].avg;
848 			peak = opp->bandwidth[i].peak;
849 		}
850 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
851 		if (ret) {
852 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
853 				opp ? "set" : "remove", i, ret);
854 			return ret;
855 		}
856 	}
857 
858 	return 0;
859 }
860 
861 static int _set_opp_custom(const struct opp_table *opp_table,
862 			   struct device *dev, struct dev_pm_opp *opp,
863 			   unsigned long freq)
864 {
865 	struct dev_pm_set_opp_data *data = opp_table->set_opp_data;
866 	struct dev_pm_opp *old_opp = opp_table->current_opp;
867 	int size;
868 
869 	/*
870 	 * We support this only if dev_pm_opp_set_regulators() was called
871 	 * earlier.
872 	 */
873 	if (opp_table->sod_supplies) {
874 		size = sizeof(*old_opp->supplies) * opp_table->regulator_count;
875 		memcpy(data->old_opp.supplies, old_opp->supplies, size);
876 		memcpy(data->new_opp.supplies, opp->supplies, size);
877 		data->regulator_count = opp_table->regulator_count;
878 	} else {
879 		data->regulator_count = 0;
880 	}
881 
882 	data->regulators = opp_table->regulators;
883 	data->clk = opp_table->clk;
884 	data->dev = dev;
885 	data->old_opp.rate = old_opp->rate;
886 	data->new_opp.rate = freq;
887 
888 	return opp_table->set_opp(data);
889 }
890 
891 static int _set_required_opp(struct device *dev, struct device *pd_dev,
892 			     struct dev_pm_opp *opp, int i)
893 {
894 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
895 	int ret;
896 
897 	if (!pd_dev)
898 		return 0;
899 
900 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
901 	if (ret) {
902 		dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
903 			dev_name(pd_dev), pstate, ret);
904 	}
905 
906 	return ret;
907 }
908 
909 /* This is only called for PM domain for now */
910 static int _set_required_opps(struct device *dev,
911 			      struct opp_table *opp_table,
912 			      struct dev_pm_opp *opp, bool up)
913 {
914 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
915 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
916 	int i, ret = 0;
917 
918 	if (!required_opp_tables)
919 		return 0;
920 
921 	/* required-opps not fully initialized yet */
922 	if (lazy_linking_pending(opp_table))
923 		return -EBUSY;
924 
925 	/*
926 	 * We only support genpd's OPPs in the "required-opps" for now, as we
927 	 * don't know much about other use cases. Error out if the required OPP
928 	 * doesn't belong to a genpd.
929 	 */
930 	if (unlikely(!required_opp_tables[0]->is_genpd)) {
931 		dev_err(dev, "required-opps don't belong to a genpd\n");
932 		return -ENOENT;
933 	}
934 
935 	/* Single genpd case */
936 	if (!genpd_virt_devs)
937 		return _set_required_opp(dev, dev, opp, 0);
938 
939 	/* Multiple genpd case */
940 
941 	/*
942 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
943 	 * after it is freed from another thread.
944 	 */
945 	mutex_lock(&opp_table->genpd_virt_dev_lock);
946 
947 	/* Scaling up? Set required OPPs in normal order, else reverse */
948 	if (up) {
949 		for (i = 0; i < opp_table->required_opp_count; i++) {
950 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
951 			if (ret)
952 				break;
953 		}
954 	} else {
955 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
956 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
957 			if (ret)
958 				break;
959 		}
960 	}
961 
962 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
963 
964 	return ret;
965 }
966 
967 static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
968 {
969 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
970 	unsigned long freq;
971 
972 	if (!IS_ERR(opp_table->clk)) {
973 		freq = clk_get_rate(opp_table->clk);
974 		opp = _find_freq_ceil(opp_table, &freq);
975 	}
976 
977 	/*
978 	 * Unable to find the current OPP ? Pick the first from the list since
979 	 * it is in ascending order, otherwise rest of the code will need to
980 	 * make special checks to validate current_opp.
981 	 */
982 	if (IS_ERR(opp)) {
983 		mutex_lock(&opp_table->lock);
984 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
985 		dev_pm_opp_get(opp);
986 		mutex_unlock(&opp_table->lock);
987 	}
988 
989 	opp_table->current_opp = opp;
990 }
991 
992 static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
993 {
994 	int ret;
995 
996 	if (!opp_table->enabled)
997 		return 0;
998 
999 	/*
1000 	 * Some drivers need to support cases where some platforms may
1001 	 * have OPP table for the device, while others don't and
1002 	 * opp_set_rate() just needs to behave like clk_set_rate().
1003 	 */
1004 	if (!_get_opp_count(opp_table))
1005 		return 0;
1006 
1007 	ret = _set_opp_bw(opp_table, NULL, dev);
1008 	if (ret)
1009 		return ret;
1010 
1011 	if (opp_table->regulators)
1012 		regulator_disable(opp_table->regulators[0]);
1013 
1014 	ret = _set_required_opps(dev, opp_table, NULL, false);
1015 
1016 	opp_table->enabled = false;
1017 	return ret;
1018 }
1019 
1020 static int _set_opp(struct device *dev, struct opp_table *opp_table,
1021 		    struct dev_pm_opp *opp, unsigned long freq)
1022 {
1023 	struct dev_pm_opp *old_opp;
1024 	int scaling_down, ret;
1025 
1026 	if (unlikely(!opp))
1027 		return _disable_opp_table(dev, opp_table);
1028 
1029 	/* Find the currently set OPP if we don't know already */
1030 	if (unlikely(!opp_table->current_opp))
1031 		_find_current_opp(dev, opp_table);
1032 
1033 	old_opp = opp_table->current_opp;
1034 
1035 	/* Return early if nothing to do */
1036 	if (old_opp == opp && opp_table->current_rate == freq &&
1037 	    opp_table->enabled) {
1038 		dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
1039 		return 0;
1040 	}
1041 
1042 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1043 		__func__, opp_table->current_rate, freq, old_opp->level,
1044 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1045 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1046 
1047 	scaling_down = _opp_compare_key(old_opp, opp);
1048 	if (scaling_down == -1)
1049 		scaling_down = 0;
1050 
1051 	/* Scaling up? Configure required OPPs before frequency */
1052 	if (!scaling_down) {
1053 		ret = _set_required_opps(dev, opp_table, opp, true);
1054 		if (ret) {
1055 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1056 			return ret;
1057 		}
1058 
1059 		ret = _set_opp_bw(opp_table, opp, dev);
1060 		if (ret) {
1061 			dev_err(dev, "Failed to set bw: %d\n", ret);
1062 			return ret;
1063 		}
1064 	}
1065 
1066 	if (opp_table->set_opp) {
1067 		ret = _set_opp_custom(opp_table, dev, opp, freq);
1068 	} else if (opp_table->regulators) {
1069 		ret = _generic_set_opp_regulator(opp_table, dev, opp, freq,
1070 						 scaling_down);
1071 	} else {
1072 		/* Only frequency scaling */
1073 		ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
1074 	}
1075 
1076 	if (ret)
1077 		return ret;
1078 
1079 	/* Scaling down? Configure required OPPs after frequency */
1080 	if (scaling_down) {
1081 		ret = _set_opp_bw(opp_table, opp, dev);
1082 		if (ret) {
1083 			dev_err(dev, "Failed to set bw: %d\n", ret);
1084 			return ret;
1085 		}
1086 
1087 		ret = _set_required_opps(dev, opp_table, opp, false);
1088 		if (ret) {
1089 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1090 			return ret;
1091 		}
1092 	}
1093 
1094 	opp_table->enabled = true;
1095 	dev_pm_opp_put(old_opp);
1096 
1097 	/* Make sure current_opp doesn't get freed */
1098 	dev_pm_opp_get(opp);
1099 	opp_table->current_opp = opp;
1100 	opp_table->current_rate = freq;
1101 
1102 	return ret;
1103 }
1104 
1105 /**
1106  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1107  * @dev:	 device for which we do this operation
1108  * @target_freq: frequency to achieve
1109  *
1110  * This configures the power-supplies to the levels specified by the OPP
1111  * corresponding to the target_freq, and programs the clock to a value <=
1112  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1113  * provided by the opp, should have already rounded to the target OPP's
1114  * frequency.
1115  */
1116 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1117 {
1118 	struct opp_table *opp_table;
1119 	unsigned long freq = 0, temp_freq;
1120 	struct dev_pm_opp *opp = NULL;
1121 	int ret;
1122 
1123 	opp_table = _find_opp_table(dev);
1124 	if (IS_ERR(opp_table)) {
1125 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1126 		return PTR_ERR(opp_table);
1127 	}
1128 
1129 	if (target_freq) {
1130 		/*
1131 		 * For IO devices which require an OPP on some platforms/SoCs
1132 		 * while just needing to scale the clock on some others
1133 		 * we look for empty OPP tables with just a clock handle and
1134 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1135 		 * equivalent to a clk_set_rate()
1136 		 */
1137 		if (!_get_opp_count(opp_table)) {
1138 			ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq);
1139 			goto put_opp_table;
1140 		}
1141 
1142 		freq = clk_round_rate(opp_table->clk, target_freq);
1143 		if ((long)freq <= 0)
1144 			freq = target_freq;
1145 
1146 		/*
1147 		 * The clock driver may support finer resolution of the
1148 		 * frequencies than the OPP table, don't update the frequency we
1149 		 * pass to clk_set_rate() here.
1150 		 */
1151 		temp_freq = freq;
1152 		opp = _find_freq_ceil(opp_table, &temp_freq);
1153 		if (IS_ERR(opp)) {
1154 			ret = PTR_ERR(opp);
1155 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1156 				__func__, freq, ret);
1157 			goto put_opp_table;
1158 		}
1159 	}
1160 
1161 	ret = _set_opp(dev, opp_table, opp, freq);
1162 
1163 	if (target_freq)
1164 		dev_pm_opp_put(opp);
1165 put_opp_table:
1166 	dev_pm_opp_put_opp_table(opp_table);
1167 	return ret;
1168 }
1169 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
1170 
1171 /**
1172  * dev_pm_opp_set_opp() - Configure device for OPP
1173  * @dev: device for which we do this operation
1174  * @opp: OPP to set to
1175  *
1176  * This configures the device based on the properties of the OPP passed to this
1177  * routine.
1178  *
1179  * Return: 0 on success, a negative error number otherwise.
1180  */
1181 int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1182 {
1183 	struct opp_table *opp_table;
1184 	int ret;
1185 
1186 	opp_table = _find_opp_table(dev);
1187 	if (IS_ERR(opp_table)) {
1188 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1189 		return PTR_ERR(opp_table);
1190 	}
1191 
1192 	ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0);
1193 	dev_pm_opp_put_opp_table(opp_table);
1194 
1195 	return ret;
1196 }
1197 EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1198 
1199 /* OPP-dev Helpers */
1200 static void _remove_opp_dev(struct opp_device *opp_dev,
1201 			    struct opp_table *opp_table)
1202 {
1203 	opp_debug_unregister(opp_dev, opp_table);
1204 	list_del(&opp_dev->node);
1205 	kfree(opp_dev);
1206 }
1207 
1208 struct opp_device *_add_opp_dev(const struct device *dev,
1209 				struct opp_table *opp_table)
1210 {
1211 	struct opp_device *opp_dev;
1212 
1213 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
1214 	if (!opp_dev)
1215 		return NULL;
1216 
1217 	/* Initialize opp-dev */
1218 	opp_dev->dev = dev;
1219 
1220 	mutex_lock(&opp_table->lock);
1221 	list_add(&opp_dev->node, &opp_table->dev_list);
1222 	mutex_unlock(&opp_table->lock);
1223 
1224 	/* Create debugfs entries for the opp_table */
1225 	opp_debug_register(opp_dev, opp_table);
1226 
1227 	return opp_dev;
1228 }
1229 
1230 static struct opp_table *_allocate_opp_table(struct device *dev, int index)
1231 {
1232 	struct opp_table *opp_table;
1233 	struct opp_device *opp_dev;
1234 	int ret;
1235 
1236 	/*
1237 	 * Allocate a new OPP table. In the infrequent case where a new
1238 	 * device is needed to be added, we pay this penalty.
1239 	 */
1240 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
1241 	if (!opp_table)
1242 		return ERR_PTR(-ENOMEM);
1243 
1244 	mutex_init(&opp_table->lock);
1245 	mutex_init(&opp_table->genpd_virt_dev_lock);
1246 	INIT_LIST_HEAD(&opp_table->dev_list);
1247 	INIT_LIST_HEAD(&opp_table->lazy);
1248 
1249 	/* Mark regulator count uninitialized */
1250 	opp_table->regulator_count = -1;
1251 
1252 	opp_dev = _add_opp_dev(dev, opp_table);
1253 	if (!opp_dev) {
1254 		ret = -ENOMEM;
1255 		goto err;
1256 	}
1257 
1258 	_of_init_opp_table(opp_table, dev, index);
1259 
1260 	/* Find interconnect path(s) for the device */
1261 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1262 	if (ret) {
1263 		if (ret == -EPROBE_DEFER)
1264 			goto remove_opp_dev;
1265 
1266 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
1267 			 __func__, ret);
1268 	}
1269 
1270 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
1271 	INIT_LIST_HEAD(&opp_table->opp_list);
1272 	kref_init(&opp_table->kref);
1273 
1274 	return opp_table;
1275 
1276 remove_opp_dev:
1277 	_remove_opp_dev(opp_dev, opp_table);
1278 err:
1279 	kfree(opp_table);
1280 	return ERR_PTR(ret);
1281 }
1282 
1283 void _get_opp_table_kref(struct opp_table *opp_table)
1284 {
1285 	kref_get(&opp_table->kref);
1286 }
1287 
1288 static struct opp_table *_update_opp_table_clk(struct device *dev,
1289 					       struct opp_table *opp_table,
1290 					       bool getclk)
1291 {
1292 	int ret;
1293 
1294 	/*
1295 	 * Return early if we don't need to get clk or we have already tried it
1296 	 * earlier.
1297 	 */
1298 	if (!getclk || IS_ERR(opp_table) || opp_table->clk)
1299 		return opp_table;
1300 
1301 	/* Find clk for the device */
1302 	opp_table->clk = clk_get(dev, NULL);
1303 
1304 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
1305 	if (!ret)
1306 		return opp_table;
1307 
1308 	if (ret == -ENOENT) {
1309 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1310 		return opp_table;
1311 	}
1312 
1313 	dev_pm_opp_put_opp_table(opp_table);
1314 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1315 
1316 	return ERR_PTR(ret);
1317 }
1318 
1319 /*
1320  * We need to make sure that the OPP table for a device doesn't get added twice,
1321  * if this routine gets called in parallel with the same device pointer.
1322  *
1323  * The simplest way to enforce that is to perform everything (find existing
1324  * table and if not found, create a new one) under the opp_table_lock, so only
1325  * one creator gets access to the same. But that expands the critical section
1326  * under the lock and may end up causing circular dependencies with frameworks
1327  * like debugfs, interconnect or clock framework as they may be direct or
1328  * indirect users of OPP core.
1329  *
1330  * And for that reason we have to go for a bit tricky implementation here, which
1331  * uses the opp_tables_busy flag to indicate if another creator is in the middle
1332  * of adding an OPP table and others should wait for it to finish.
1333  */
1334 struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
1335 					 bool getclk)
1336 {
1337 	struct opp_table *opp_table;
1338 
1339 again:
1340 	mutex_lock(&opp_table_lock);
1341 
1342 	opp_table = _find_opp_table_unlocked(dev);
1343 	if (!IS_ERR(opp_table))
1344 		goto unlock;
1345 
1346 	/*
1347 	 * The opp_tables list or an OPP table's dev_list is getting updated by
1348 	 * another user, wait for it to finish.
1349 	 */
1350 	if (unlikely(opp_tables_busy)) {
1351 		mutex_unlock(&opp_table_lock);
1352 		cpu_relax();
1353 		goto again;
1354 	}
1355 
1356 	opp_tables_busy = true;
1357 	opp_table = _managed_opp(dev, index);
1358 
1359 	/* Drop the lock to reduce the size of critical section */
1360 	mutex_unlock(&opp_table_lock);
1361 
1362 	if (opp_table) {
1363 		if (!_add_opp_dev(dev, opp_table)) {
1364 			dev_pm_opp_put_opp_table(opp_table);
1365 			opp_table = ERR_PTR(-ENOMEM);
1366 		}
1367 
1368 		mutex_lock(&opp_table_lock);
1369 	} else {
1370 		opp_table = _allocate_opp_table(dev, index);
1371 
1372 		mutex_lock(&opp_table_lock);
1373 		if (!IS_ERR(opp_table))
1374 			list_add(&opp_table->node, &opp_tables);
1375 	}
1376 
1377 	opp_tables_busy = false;
1378 
1379 unlock:
1380 	mutex_unlock(&opp_table_lock);
1381 
1382 	return _update_opp_table_clk(dev, opp_table, getclk);
1383 }
1384 
1385 static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1386 {
1387 	return _add_opp_table_indexed(dev, 0, getclk);
1388 }
1389 
1390 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1391 {
1392 	return _find_opp_table(dev);
1393 }
1394 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
1395 
1396 static void _opp_table_kref_release(struct kref *kref)
1397 {
1398 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1399 	struct opp_device *opp_dev, *temp;
1400 	int i;
1401 
1402 	/* Drop the lock as soon as we can */
1403 	list_del(&opp_table->node);
1404 	mutex_unlock(&opp_table_lock);
1405 
1406 	if (opp_table->current_opp)
1407 		dev_pm_opp_put(opp_table->current_opp);
1408 
1409 	_of_clear_opp_table(opp_table);
1410 
1411 	/* Release clk */
1412 	if (!IS_ERR(opp_table->clk))
1413 		clk_put(opp_table->clk);
1414 
1415 	if (opp_table->paths) {
1416 		for (i = 0; i < opp_table->path_count; i++)
1417 			icc_put(opp_table->paths[i]);
1418 		kfree(opp_table->paths);
1419 	}
1420 
1421 	WARN_ON(!list_empty(&opp_table->opp_list));
1422 
1423 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
1424 		/*
1425 		 * The OPP table is getting removed, drop the performance state
1426 		 * constraints.
1427 		 */
1428 		if (opp_table->genpd_performance_state)
1429 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
1430 
1431 		_remove_opp_dev(opp_dev, opp_table);
1432 	}
1433 
1434 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
1435 	mutex_destroy(&opp_table->lock);
1436 	kfree(opp_table);
1437 }
1438 
1439 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1440 {
1441 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1442 		       &opp_table_lock);
1443 }
1444 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1445 
1446 void _opp_free(struct dev_pm_opp *opp)
1447 {
1448 	kfree(opp);
1449 }
1450 
1451 static void _opp_kref_release(struct kref *kref)
1452 {
1453 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1454 	struct opp_table *opp_table = opp->opp_table;
1455 
1456 	list_del(&opp->node);
1457 	mutex_unlock(&opp_table->lock);
1458 
1459 	/*
1460 	 * Notify the changes in the availability of the operable
1461 	 * frequency/voltage list.
1462 	 */
1463 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1464 	_of_opp_free_required_opps(opp_table, opp);
1465 	opp_debug_remove_one(opp);
1466 	kfree(opp);
1467 }
1468 
1469 void dev_pm_opp_get(struct dev_pm_opp *opp)
1470 {
1471 	kref_get(&opp->kref);
1472 }
1473 
1474 void dev_pm_opp_put(struct dev_pm_opp *opp)
1475 {
1476 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
1477 }
1478 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1479 
1480 /**
1481  * dev_pm_opp_remove()  - Remove an OPP from OPP table
1482  * @dev:	device for which we do this operation
1483  * @freq:	OPP to remove with matching 'freq'
1484  *
1485  * This function removes an opp from the opp table.
1486  */
1487 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1488 {
1489 	struct dev_pm_opp *opp;
1490 	struct opp_table *opp_table;
1491 	bool found = false;
1492 
1493 	opp_table = _find_opp_table(dev);
1494 	if (IS_ERR(opp_table))
1495 		return;
1496 
1497 	mutex_lock(&opp_table->lock);
1498 
1499 	list_for_each_entry(opp, &opp_table->opp_list, node) {
1500 		if (opp->rate == freq) {
1501 			found = true;
1502 			break;
1503 		}
1504 	}
1505 
1506 	mutex_unlock(&opp_table->lock);
1507 
1508 	if (found) {
1509 		dev_pm_opp_put(opp);
1510 
1511 		/* Drop the reference taken by dev_pm_opp_add() */
1512 		dev_pm_opp_put_opp_table(opp_table);
1513 	} else {
1514 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1515 			 __func__, freq);
1516 	}
1517 
1518 	/* Drop the reference taken by _find_opp_table() */
1519 	dev_pm_opp_put_opp_table(opp_table);
1520 }
1521 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1522 
1523 static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1524 					bool dynamic)
1525 {
1526 	struct dev_pm_opp *opp = NULL, *temp;
1527 
1528 	mutex_lock(&opp_table->lock);
1529 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1530 		/*
1531 		 * Refcount must be dropped only once for each OPP by OPP core,
1532 		 * do that with help of "removed" flag.
1533 		 */
1534 		if (!temp->removed && dynamic == temp->dynamic) {
1535 			opp = temp;
1536 			break;
1537 		}
1538 	}
1539 
1540 	mutex_unlock(&opp_table->lock);
1541 	return opp;
1542 }
1543 
1544 /*
1545  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1546  * happen lock less to avoid circular dependency issues. This routine must be
1547  * called without the opp_table->lock held.
1548  */
1549 static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
1550 {
1551 	struct dev_pm_opp *opp;
1552 
1553 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1554 		opp->removed = true;
1555 		dev_pm_opp_put(opp);
1556 
1557 		/* Drop the references taken by dev_pm_opp_add() */
1558 		if (dynamic)
1559 			dev_pm_opp_put_opp_table(opp_table);
1560 	}
1561 }
1562 
1563 bool _opp_remove_all_static(struct opp_table *opp_table)
1564 {
1565 	mutex_lock(&opp_table->lock);
1566 
1567 	if (!opp_table->parsed_static_opps) {
1568 		mutex_unlock(&opp_table->lock);
1569 		return false;
1570 	}
1571 
1572 	if (--opp_table->parsed_static_opps) {
1573 		mutex_unlock(&opp_table->lock);
1574 		return true;
1575 	}
1576 
1577 	mutex_unlock(&opp_table->lock);
1578 
1579 	_opp_remove_all(opp_table, false);
1580 	return true;
1581 }
1582 
1583 /**
1584  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1585  * @dev:	device for which we do this operation
1586  *
1587  * This function removes all dynamically created OPPs from the opp table.
1588  */
1589 void dev_pm_opp_remove_all_dynamic(struct device *dev)
1590 {
1591 	struct opp_table *opp_table;
1592 
1593 	opp_table = _find_opp_table(dev);
1594 	if (IS_ERR(opp_table))
1595 		return;
1596 
1597 	_opp_remove_all(opp_table, true);
1598 
1599 	/* Drop the reference taken by _find_opp_table() */
1600 	dev_pm_opp_put_opp_table(opp_table);
1601 }
1602 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1603 
1604 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
1605 {
1606 	struct dev_pm_opp *opp;
1607 	int supply_count, supply_size, icc_size;
1608 
1609 	/* Allocate space for at least one supply */
1610 	supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
1611 	supply_size = sizeof(*opp->supplies) * supply_count;
1612 	icc_size = sizeof(*opp->bandwidth) * table->path_count;
1613 
1614 	/* allocate new OPP node and supplies structures */
1615 	opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
1616 
1617 	if (!opp)
1618 		return NULL;
1619 
1620 	/* Put the supplies at the end of the OPP structure as an empty array */
1621 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1622 	if (icc_size)
1623 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
1624 	INIT_LIST_HEAD(&opp->node);
1625 
1626 	return opp;
1627 }
1628 
1629 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1630 					 struct opp_table *opp_table)
1631 {
1632 	struct regulator *reg;
1633 	int i;
1634 
1635 	if (!opp_table->regulators)
1636 		return true;
1637 
1638 	for (i = 0; i < opp_table->regulator_count; i++) {
1639 		reg = opp_table->regulators[i];
1640 
1641 		if (!regulator_is_supported_voltage(reg,
1642 					opp->supplies[i].u_volt_min,
1643 					opp->supplies[i].u_volt_max)) {
1644 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1645 				__func__, opp->supplies[i].u_volt_min,
1646 				opp->supplies[i].u_volt_max);
1647 			return false;
1648 		}
1649 	}
1650 
1651 	return true;
1652 }
1653 
1654 int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
1655 {
1656 	if (opp1->rate != opp2->rate)
1657 		return opp1->rate < opp2->rate ? -1 : 1;
1658 	if (opp1->bandwidth && opp2->bandwidth &&
1659 	    opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
1660 		return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
1661 	if (opp1->level != opp2->level)
1662 		return opp1->level < opp2->level ? -1 : 1;
1663 	return 0;
1664 }
1665 
1666 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1667 			     struct opp_table *opp_table,
1668 			     struct list_head **head)
1669 {
1670 	struct dev_pm_opp *opp;
1671 	int opp_cmp;
1672 
1673 	/*
1674 	 * Insert new OPP in order of increasing frequency and discard if
1675 	 * already present.
1676 	 *
1677 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1678 	 * loop, don't replace it with head otherwise it will become an infinite
1679 	 * loop.
1680 	 */
1681 	list_for_each_entry(opp, &opp_table->opp_list, node) {
1682 		opp_cmp = _opp_compare_key(new_opp, opp);
1683 		if (opp_cmp > 0) {
1684 			*head = &opp->node;
1685 			continue;
1686 		}
1687 
1688 		if (opp_cmp < 0)
1689 			return 0;
1690 
1691 		/* Duplicate OPPs */
1692 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1693 			 __func__, opp->rate, opp->supplies[0].u_volt,
1694 			 opp->available, new_opp->rate,
1695 			 new_opp->supplies[0].u_volt, new_opp->available);
1696 
1697 		/* Should we compare voltages for all regulators here ? */
1698 		return opp->available &&
1699 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1700 	}
1701 
1702 	return 0;
1703 }
1704 
1705 void _required_opps_available(struct dev_pm_opp *opp, int count)
1706 {
1707 	int i;
1708 
1709 	for (i = 0; i < count; i++) {
1710 		if (opp->required_opps[i]->available)
1711 			continue;
1712 
1713 		opp->available = false;
1714 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
1715 			 __func__, opp->required_opps[i]->np, opp->rate);
1716 		return;
1717 	}
1718 }
1719 
1720 /*
1721  * Returns:
1722  * 0: On success. And appropriate error message for duplicate OPPs.
1723  * -EBUSY: For OPP with same freq/volt and is available. The callers of
1724  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1725  *  sure we don't print error messages unnecessarily if different parts of
1726  *  kernel try to initialize the OPP table.
1727  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1728  *  should be considered an error by the callers of _opp_add().
1729  */
1730 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1731 	     struct opp_table *opp_table, bool rate_not_available)
1732 {
1733 	struct list_head *head;
1734 	int ret;
1735 
1736 	mutex_lock(&opp_table->lock);
1737 	head = &opp_table->opp_list;
1738 
1739 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1740 	if (ret) {
1741 		mutex_unlock(&opp_table->lock);
1742 		return ret;
1743 	}
1744 
1745 	list_add(&new_opp->node, head);
1746 	mutex_unlock(&opp_table->lock);
1747 
1748 	new_opp->opp_table = opp_table;
1749 	kref_init(&new_opp->kref);
1750 
1751 	opp_debug_create_one(new_opp, opp_table);
1752 
1753 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1754 		new_opp->available = false;
1755 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1756 			 __func__, new_opp->rate);
1757 	}
1758 
1759 	/* required-opps not fully initialized yet */
1760 	if (lazy_linking_pending(opp_table))
1761 		return 0;
1762 
1763 	_required_opps_available(new_opp, opp_table->required_opp_count);
1764 
1765 	return 0;
1766 }
1767 
1768 /**
1769  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1770  * @opp_table:	OPP table
1771  * @dev:	device for which we do this operation
1772  * @freq:	Frequency in Hz for this OPP
1773  * @u_volt:	Voltage in uVolts for this OPP
1774  * @dynamic:	Dynamically added OPPs.
1775  *
1776  * This function adds an opp definition to the opp table and returns status.
1777  * The opp is made available by default and it can be controlled using
1778  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1779  *
1780  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1781  * and freed by dev_pm_opp_of_remove_table.
1782  *
1783  * Return:
1784  * 0		On success OR
1785  *		Duplicate OPPs (both freq and volt are same) and opp->available
1786  * -EEXIST	Freq are same and volt are different OR
1787  *		Duplicate OPPs (both freq and volt are same) and !opp->available
1788  * -ENOMEM	Memory allocation failure
1789  */
1790 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1791 		unsigned long freq, long u_volt, bool dynamic)
1792 {
1793 	struct dev_pm_opp *new_opp;
1794 	unsigned long tol;
1795 	int ret;
1796 
1797 	new_opp = _opp_allocate(opp_table);
1798 	if (!new_opp)
1799 		return -ENOMEM;
1800 
1801 	/* populate the opp table */
1802 	new_opp->rate = freq;
1803 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1804 	new_opp->supplies[0].u_volt = u_volt;
1805 	new_opp->supplies[0].u_volt_min = u_volt - tol;
1806 	new_opp->supplies[0].u_volt_max = u_volt + tol;
1807 	new_opp->available = true;
1808 	new_opp->dynamic = dynamic;
1809 
1810 	ret = _opp_add(dev, new_opp, opp_table, false);
1811 	if (ret) {
1812 		/* Don't return error for duplicate OPPs */
1813 		if (ret == -EBUSY)
1814 			ret = 0;
1815 		goto free_opp;
1816 	}
1817 
1818 	/*
1819 	 * Notify the changes in the availability of the operable
1820 	 * frequency/voltage list.
1821 	 */
1822 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1823 	return 0;
1824 
1825 free_opp:
1826 	_opp_free(new_opp);
1827 
1828 	return ret;
1829 }
1830 
1831 /**
1832  * dev_pm_opp_set_supported_hw() - Set supported platforms
1833  * @dev: Device for which supported-hw has to be set.
1834  * @versions: Array of hierarchy of versions to match.
1835  * @count: Number of elements in the array.
1836  *
1837  * This is required only for the V2 bindings, and it enables a platform to
1838  * specify the hierarchy of versions it supports. OPP layer will then enable
1839  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1840  * property.
1841  */
1842 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1843 			const u32 *versions, unsigned int count)
1844 {
1845 	struct opp_table *opp_table;
1846 
1847 	opp_table = _add_opp_table(dev, false);
1848 	if (IS_ERR(opp_table))
1849 		return opp_table;
1850 
1851 	/* Make sure there are no concurrent readers while updating opp_table */
1852 	WARN_ON(!list_empty(&opp_table->opp_list));
1853 
1854 	/* Another CPU that shares the OPP table has set the property ? */
1855 	if (opp_table->supported_hw)
1856 		return opp_table;
1857 
1858 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1859 					GFP_KERNEL);
1860 	if (!opp_table->supported_hw) {
1861 		dev_pm_opp_put_opp_table(opp_table);
1862 		return ERR_PTR(-ENOMEM);
1863 	}
1864 
1865 	opp_table->supported_hw_count = count;
1866 
1867 	return opp_table;
1868 }
1869 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1870 
1871 /**
1872  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1873  * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1874  *
1875  * This is required only for the V2 bindings, and is called for a matching
1876  * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1877  * will not be freed.
1878  */
1879 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1880 {
1881 	if (unlikely(!opp_table))
1882 		return;
1883 
1884 	kfree(opp_table->supported_hw);
1885 	opp_table->supported_hw = NULL;
1886 	opp_table->supported_hw_count = 0;
1887 
1888 	dev_pm_opp_put_opp_table(opp_table);
1889 }
1890 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1891 
1892 static void devm_pm_opp_supported_hw_release(void *data)
1893 {
1894 	dev_pm_opp_put_supported_hw(data);
1895 }
1896 
1897 /**
1898  * devm_pm_opp_set_supported_hw() - Set supported platforms
1899  * @dev: Device for which supported-hw has to be set.
1900  * @versions: Array of hierarchy of versions to match.
1901  * @count: Number of elements in the array.
1902  *
1903  * This is a resource-managed variant of dev_pm_opp_set_supported_hw().
1904  *
1905  * Return: 0 on success and errorno otherwise.
1906  */
1907 int devm_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1908 				 unsigned int count)
1909 {
1910 	struct opp_table *opp_table;
1911 
1912 	opp_table = dev_pm_opp_set_supported_hw(dev, versions, count);
1913 	if (IS_ERR(opp_table))
1914 		return PTR_ERR(opp_table);
1915 
1916 	return devm_add_action_or_reset(dev, devm_pm_opp_supported_hw_release,
1917 					opp_table);
1918 }
1919 EXPORT_SYMBOL_GPL(devm_pm_opp_set_supported_hw);
1920 
1921 /**
1922  * dev_pm_opp_set_prop_name() - Set prop-extn name
1923  * @dev: Device for which the prop-name has to be set.
1924  * @name: name to postfix to properties.
1925  *
1926  * This is required only for the V2 bindings, and it enables a platform to
1927  * specify the extn to be used for certain property names. The properties to
1928  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1929  * should postfix the property name with -<name> while looking for them.
1930  */
1931 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1932 {
1933 	struct opp_table *opp_table;
1934 
1935 	opp_table = _add_opp_table(dev, false);
1936 	if (IS_ERR(opp_table))
1937 		return opp_table;
1938 
1939 	/* Make sure there are no concurrent readers while updating opp_table */
1940 	WARN_ON(!list_empty(&opp_table->opp_list));
1941 
1942 	/* Another CPU that shares the OPP table has set the property ? */
1943 	if (opp_table->prop_name)
1944 		return opp_table;
1945 
1946 	opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1947 	if (!opp_table->prop_name) {
1948 		dev_pm_opp_put_opp_table(opp_table);
1949 		return ERR_PTR(-ENOMEM);
1950 	}
1951 
1952 	return opp_table;
1953 }
1954 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1955 
1956 /**
1957  * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1958  * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1959  *
1960  * This is required only for the V2 bindings, and is called for a matching
1961  * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1962  * will not be freed.
1963  */
1964 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1965 {
1966 	if (unlikely(!opp_table))
1967 		return;
1968 
1969 	kfree(opp_table->prop_name);
1970 	opp_table->prop_name = NULL;
1971 
1972 	dev_pm_opp_put_opp_table(opp_table);
1973 }
1974 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1975 
1976 /**
1977  * dev_pm_opp_set_regulators() - Set regulator names for the device
1978  * @dev: Device for which regulator name is being set.
1979  * @names: Array of pointers to the names of the regulator.
1980  * @count: Number of regulators.
1981  *
1982  * In order to support OPP switching, OPP layer needs to know the name of the
1983  * device's regulators, as the core would be required to switch voltages as
1984  * well.
1985  *
1986  * This must be called before any OPPs are initialized for the device.
1987  */
1988 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1989 					    const char * const names[],
1990 					    unsigned int count)
1991 {
1992 	struct dev_pm_opp_supply *supplies;
1993 	struct opp_table *opp_table;
1994 	struct regulator *reg;
1995 	int ret, i;
1996 
1997 	opp_table = _add_opp_table(dev, false);
1998 	if (IS_ERR(opp_table))
1999 		return opp_table;
2000 
2001 	/* This should be called before OPPs are initialized */
2002 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2003 		ret = -EBUSY;
2004 		goto err;
2005 	}
2006 
2007 	/* Another CPU that shares the OPP table has set the regulators ? */
2008 	if (opp_table->regulators)
2009 		return opp_table;
2010 
2011 	opp_table->regulators = kmalloc_array(count,
2012 					      sizeof(*opp_table->regulators),
2013 					      GFP_KERNEL);
2014 	if (!opp_table->regulators) {
2015 		ret = -ENOMEM;
2016 		goto err;
2017 	}
2018 
2019 	for (i = 0; i < count; i++) {
2020 		reg = regulator_get_optional(dev, names[i]);
2021 		if (IS_ERR(reg)) {
2022 			ret = PTR_ERR(reg);
2023 			if (ret != -EPROBE_DEFER)
2024 				dev_err(dev, "%s: no regulator (%s) found: %d\n",
2025 					__func__, names[i], ret);
2026 			goto free_regulators;
2027 		}
2028 
2029 		opp_table->regulators[i] = reg;
2030 	}
2031 
2032 	opp_table->regulator_count = count;
2033 
2034 	supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
2035 	if (!supplies) {
2036 		ret = -ENOMEM;
2037 		goto free_regulators;
2038 	}
2039 
2040 	mutex_lock(&opp_table->lock);
2041 	opp_table->sod_supplies = supplies;
2042 	if (opp_table->set_opp_data) {
2043 		opp_table->set_opp_data->old_opp.supplies = supplies;
2044 		opp_table->set_opp_data->new_opp.supplies = supplies + count;
2045 	}
2046 	mutex_unlock(&opp_table->lock);
2047 
2048 	return opp_table;
2049 
2050 free_regulators:
2051 	while (i != 0)
2052 		regulator_put(opp_table->regulators[--i]);
2053 
2054 	kfree(opp_table->regulators);
2055 	opp_table->regulators = NULL;
2056 	opp_table->regulator_count = -1;
2057 err:
2058 	dev_pm_opp_put_opp_table(opp_table);
2059 
2060 	return ERR_PTR(ret);
2061 }
2062 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
2063 
2064 /**
2065  * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
2066  * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
2067  */
2068 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
2069 {
2070 	int i;
2071 
2072 	if (unlikely(!opp_table))
2073 		return;
2074 
2075 	if (!opp_table->regulators)
2076 		goto put_opp_table;
2077 
2078 	if (opp_table->enabled) {
2079 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
2080 			regulator_disable(opp_table->regulators[i]);
2081 	}
2082 
2083 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
2084 		regulator_put(opp_table->regulators[i]);
2085 
2086 	mutex_lock(&opp_table->lock);
2087 	if (opp_table->set_opp_data) {
2088 		opp_table->set_opp_data->old_opp.supplies = NULL;
2089 		opp_table->set_opp_data->new_opp.supplies = NULL;
2090 	}
2091 
2092 	kfree(opp_table->sod_supplies);
2093 	opp_table->sod_supplies = NULL;
2094 	mutex_unlock(&opp_table->lock);
2095 
2096 	kfree(opp_table->regulators);
2097 	opp_table->regulators = NULL;
2098 	opp_table->regulator_count = -1;
2099 
2100 put_opp_table:
2101 	dev_pm_opp_put_opp_table(opp_table);
2102 }
2103 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
2104 
2105 static void devm_pm_opp_regulators_release(void *data)
2106 {
2107 	dev_pm_opp_put_regulators(data);
2108 }
2109 
2110 /**
2111  * devm_pm_opp_set_regulators() - Set regulator names for the device
2112  * @dev: Device for which regulator name is being set.
2113  * @names: Array of pointers to the names of the regulator.
2114  * @count: Number of regulators.
2115  *
2116  * This is a resource-managed variant of dev_pm_opp_set_regulators().
2117  *
2118  * Return: 0 on success and errorno otherwise.
2119  */
2120 int devm_pm_opp_set_regulators(struct device *dev,
2121 			       const char * const names[],
2122 			       unsigned int count)
2123 {
2124 	struct opp_table *opp_table;
2125 
2126 	opp_table = dev_pm_opp_set_regulators(dev, names, count);
2127 	if (IS_ERR(opp_table))
2128 		return PTR_ERR(opp_table);
2129 
2130 	return devm_add_action_or_reset(dev, devm_pm_opp_regulators_release,
2131 					opp_table);
2132 }
2133 EXPORT_SYMBOL_GPL(devm_pm_opp_set_regulators);
2134 
2135 /**
2136  * dev_pm_opp_set_clkname() - Set clk name for the device
2137  * @dev: Device for which clk name is being set.
2138  * @name: Clk name.
2139  *
2140  * In order to support OPP switching, OPP layer needs to get pointer to the
2141  * clock for the device. Simple cases work fine without using this routine (i.e.
2142  * by passing connection-id as NULL), but for a device with multiple clocks
2143  * available, the OPP core needs to know the exact name of the clk to use.
2144  *
2145  * This must be called before any OPPs are initialized for the device.
2146  */
2147 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
2148 {
2149 	struct opp_table *opp_table;
2150 	int ret;
2151 
2152 	opp_table = _add_opp_table(dev, false);
2153 	if (IS_ERR(opp_table))
2154 		return opp_table;
2155 
2156 	/* This should be called before OPPs are initialized */
2157 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2158 		ret = -EBUSY;
2159 		goto err;
2160 	}
2161 
2162 	/* clk shouldn't be initialized at this point */
2163 	if (WARN_ON(opp_table->clk)) {
2164 		ret = -EBUSY;
2165 		goto err;
2166 	}
2167 
2168 	/* Find clk for the device */
2169 	opp_table->clk = clk_get(dev, name);
2170 	if (IS_ERR(opp_table->clk)) {
2171 		ret = PTR_ERR(opp_table->clk);
2172 		if (ret != -EPROBE_DEFER) {
2173 			dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
2174 				ret);
2175 		}
2176 		goto err;
2177 	}
2178 
2179 	return opp_table;
2180 
2181 err:
2182 	dev_pm_opp_put_opp_table(opp_table);
2183 
2184 	return ERR_PTR(ret);
2185 }
2186 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
2187 
2188 /**
2189  * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
2190  * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
2191  */
2192 void dev_pm_opp_put_clkname(struct opp_table *opp_table)
2193 {
2194 	if (unlikely(!opp_table))
2195 		return;
2196 
2197 	clk_put(opp_table->clk);
2198 	opp_table->clk = ERR_PTR(-EINVAL);
2199 
2200 	dev_pm_opp_put_opp_table(opp_table);
2201 }
2202 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
2203 
2204 static void devm_pm_opp_clkname_release(void *data)
2205 {
2206 	dev_pm_opp_put_clkname(data);
2207 }
2208 
2209 /**
2210  * devm_pm_opp_set_clkname() - Set clk name for the device
2211  * @dev: Device for which clk name is being set.
2212  * @name: Clk name.
2213  *
2214  * This is a resource-managed variant of dev_pm_opp_set_clkname().
2215  *
2216  * Return: 0 on success and errorno otherwise.
2217  */
2218 int devm_pm_opp_set_clkname(struct device *dev, const char *name)
2219 {
2220 	struct opp_table *opp_table;
2221 
2222 	opp_table = dev_pm_opp_set_clkname(dev, name);
2223 	if (IS_ERR(opp_table))
2224 		return PTR_ERR(opp_table);
2225 
2226 	return devm_add_action_or_reset(dev, devm_pm_opp_clkname_release,
2227 					opp_table);
2228 }
2229 EXPORT_SYMBOL_GPL(devm_pm_opp_set_clkname);
2230 
2231 /**
2232  * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2233  * @dev: Device for which the helper is getting registered.
2234  * @set_opp: Custom set OPP helper.
2235  *
2236  * This is useful to support complex platforms (like platforms with multiple
2237  * regulators per device), instead of the generic OPP set rate helper.
2238  *
2239  * This must be called before any OPPs are initialized for the device.
2240  */
2241 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
2242 			int (*set_opp)(struct dev_pm_set_opp_data *data))
2243 {
2244 	struct dev_pm_set_opp_data *data;
2245 	struct opp_table *opp_table;
2246 
2247 	if (!set_opp)
2248 		return ERR_PTR(-EINVAL);
2249 
2250 	opp_table = _add_opp_table(dev, false);
2251 	if (IS_ERR(opp_table))
2252 		return opp_table;
2253 
2254 	/* This should be called before OPPs are initialized */
2255 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2256 		dev_pm_opp_put_opp_table(opp_table);
2257 		return ERR_PTR(-EBUSY);
2258 	}
2259 
2260 	/* Another CPU that shares the OPP table has set the helper ? */
2261 	if (opp_table->set_opp)
2262 		return opp_table;
2263 
2264 	data = kzalloc(sizeof(*data), GFP_KERNEL);
2265 	if (!data)
2266 		return ERR_PTR(-ENOMEM);
2267 
2268 	mutex_lock(&opp_table->lock);
2269 	opp_table->set_opp_data = data;
2270 	if (opp_table->sod_supplies) {
2271 		data->old_opp.supplies = opp_table->sod_supplies;
2272 		data->new_opp.supplies = opp_table->sod_supplies +
2273 					 opp_table->regulator_count;
2274 	}
2275 	mutex_unlock(&opp_table->lock);
2276 
2277 	opp_table->set_opp = set_opp;
2278 
2279 	return opp_table;
2280 }
2281 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
2282 
2283 /**
2284  * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
2285  *					   set_opp helper
2286  * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
2287  *
2288  * Release resources blocked for platform specific set_opp helper.
2289  */
2290 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
2291 {
2292 	if (unlikely(!opp_table))
2293 		return;
2294 
2295 	opp_table->set_opp = NULL;
2296 
2297 	mutex_lock(&opp_table->lock);
2298 	kfree(opp_table->set_opp_data);
2299 	opp_table->set_opp_data = NULL;
2300 	mutex_unlock(&opp_table->lock);
2301 
2302 	dev_pm_opp_put_opp_table(opp_table);
2303 }
2304 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
2305 
2306 static void devm_pm_opp_unregister_set_opp_helper(void *data)
2307 {
2308 	dev_pm_opp_unregister_set_opp_helper(data);
2309 }
2310 
2311 /**
2312  * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2313  * @dev: Device for which the helper is getting registered.
2314  * @set_opp: Custom set OPP helper.
2315  *
2316  * This is a resource-managed version of dev_pm_opp_register_set_opp_helper().
2317  *
2318  * Return: 0 on success and errorno otherwise.
2319  */
2320 int devm_pm_opp_register_set_opp_helper(struct device *dev,
2321 					int (*set_opp)(struct dev_pm_set_opp_data *data))
2322 {
2323 	struct opp_table *opp_table;
2324 
2325 	opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp);
2326 	if (IS_ERR(opp_table))
2327 		return PTR_ERR(opp_table);
2328 
2329 	return devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper,
2330 					opp_table);
2331 }
2332 EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper);
2333 
2334 static void _opp_detach_genpd(struct opp_table *opp_table)
2335 {
2336 	int index;
2337 
2338 	if (!opp_table->genpd_virt_devs)
2339 		return;
2340 
2341 	for (index = 0; index < opp_table->required_opp_count; index++) {
2342 		if (!opp_table->genpd_virt_devs[index])
2343 			continue;
2344 
2345 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
2346 		opp_table->genpd_virt_devs[index] = NULL;
2347 	}
2348 
2349 	kfree(opp_table->genpd_virt_devs);
2350 	opp_table->genpd_virt_devs = NULL;
2351 }
2352 
2353 /**
2354  * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
2355  * @dev: Consumer device for which the genpd is getting attached.
2356  * @names: Null terminated array of pointers containing names of genpd to attach.
2357  * @virt_devs: Pointer to return the array of virtual devices.
2358  *
2359  * Multiple generic power domains for a device are supported with the help of
2360  * virtual genpd devices, which are created for each consumer device - genpd
2361  * pair. These are the device structures which are attached to the power domain
2362  * and are required by the OPP core to set the performance state of the genpd.
2363  * The same API also works for the case where single genpd is available and so
2364  * we don't need to support that separately.
2365  *
2366  * This helper will normally be called by the consumer driver of the device
2367  * "dev", as only that has details of the genpd names.
2368  *
2369  * This helper needs to be called once with a list of all genpd to attach.
2370  * Otherwise the original device structure will be used instead by the OPP core.
2371  *
2372  * The order of entries in the names array must match the order in which
2373  * "required-opps" are added in DT.
2374  */
2375 struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
2376 		const char * const *names, struct device ***virt_devs)
2377 {
2378 	struct opp_table *opp_table;
2379 	struct device *virt_dev;
2380 	int index = 0, ret = -EINVAL;
2381 	const char * const *name = names;
2382 
2383 	opp_table = _add_opp_table(dev, false);
2384 	if (IS_ERR(opp_table))
2385 		return opp_table;
2386 
2387 	if (opp_table->genpd_virt_devs)
2388 		return opp_table;
2389 
2390 	/*
2391 	 * If the genpd's OPP table isn't already initialized, parsing of the
2392 	 * required-opps fail for dev. We should retry this after genpd's OPP
2393 	 * table is added.
2394 	 */
2395 	if (!opp_table->required_opp_count) {
2396 		ret = -EPROBE_DEFER;
2397 		goto put_table;
2398 	}
2399 
2400 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2401 
2402 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2403 					     sizeof(*opp_table->genpd_virt_devs),
2404 					     GFP_KERNEL);
2405 	if (!opp_table->genpd_virt_devs)
2406 		goto unlock;
2407 
2408 	while (*name) {
2409 		if (index >= opp_table->required_opp_count) {
2410 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
2411 				*name, opp_table->required_opp_count, index);
2412 			goto err;
2413 		}
2414 
2415 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
2416 		if (IS_ERR(virt_dev)) {
2417 			ret = PTR_ERR(virt_dev);
2418 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
2419 			goto err;
2420 		}
2421 
2422 		opp_table->genpd_virt_devs[index] = virt_dev;
2423 		index++;
2424 		name++;
2425 	}
2426 
2427 	if (virt_devs)
2428 		*virt_devs = opp_table->genpd_virt_devs;
2429 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2430 
2431 	return opp_table;
2432 
2433 err:
2434 	_opp_detach_genpd(opp_table);
2435 unlock:
2436 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2437 
2438 put_table:
2439 	dev_pm_opp_put_opp_table(opp_table);
2440 
2441 	return ERR_PTR(ret);
2442 }
2443 EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
2444 
2445 /**
2446  * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
2447  * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
2448  *
2449  * This detaches the genpd(s), resets the virtual device pointers, and puts the
2450  * OPP table.
2451  */
2452 void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
2453 {
2454 	if (unlikely(!opp_table))
2455 		return;
2456 
2457 	/*
2458 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
2459 	 * used in parallel.
2460 	 */
2461 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2462 	_opp_detach_genpd(opp_table);
2463 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2464 
2465 	dev_pm_opp_put_opp_table(opp_table);
2466 }
2467 EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
2468 
2469 static void devm_pm_opp_detach_genpd(void *data)
2470 {
2471 	dev_pm_opp_detach_genpd(data);
2472 }
2473 
2474 /**
2475  * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual
2476  *			      device pointer
2477  * @dev: Consumer device for which the genpd is getting attached.
2478  * @names: Null terminated array of pointers containing names of genpd to attach.
2479  * @virt_devs: Pointer to return the array of virtual devices.
2480  *
2481  * This is a resource-managed version of dev_pm_opp_attach_genpd().
2482  *
2483  * Return: 0 on success and errorno otherwise.
2484  */
2485 int devm_pm_opp_attach_genpd(struct device *dev, const char * const *names,
2486 			     struct device ***virt_devs)
2487 {
2488 	struct opp_table *opp_table;
2489 
2490 	opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs);
2491 	if (IS_ERR(opp_table))
2492 		return PTR_ERR(opp_table);
2493 
2494 	return devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd,
2495 					opp_table);
2496 }
2497 EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd);
2498 
2499 /**
2500  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
2501  * @src_table: OPP table which has @dst_table as one of its required OPP table.
2502  * @dst_table: Required OPP table of the @src_table.
2503  * @src_opp: OPP from the @src_table.
2504  *
2505  * This function returns the OPP (present in @dst_table) pointed out by the
2506  * "required-opps" property of the @src_opp (present in @src_table).
2507  *
2508  * The callers are required to call dev_pm_opp_put() for the returned OPP after
2509  * use.
2510  *
2511  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
2512  */
2513 struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
2514 						 struct opp_table *dst_table,
2515 						 struct dev_pm_opp *src_opp)
2516 {
2517 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
2518 	int i;
2519 
2520 	if (!src_table || !dst_table || !src_opp ||
2521 	    !src_table->required_opp_tables)
2522 		return ERR_PTR(-EINVAL);
2523 
2524 	/* required-opps not fully initialized yet */
2525 	if (lazy_linking_pending(src_table))
2526 		return ERR_PTR(-EBUSY);
2527 
2528 	for (i = 0; i < src_table->required_opp_count; i++) {
2529 		if (src_table->required_opp_tables[i] == dst_table) {
2530 			mutex_lock(&src_table->lock);
2531 
2532 			list_for_each_entry(opp, &src_table->opp_list, node) {
2533 				if (opp == src_opp) {
2534 					dest_opp = opp->required_opps[i];
2535 					dev_pm_opp_get(dest_opp);
2536 					break;
2537 				}
2538 			}
2539 
2540 			mutex_unlock(&src_table->lock);
2541 			break;
2542 		}
2543 	}
2544 
2545 	if (IS_ERR(dest_opp)) {
2546 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
2547 		       src_table, dst_table);
2548 	}
2549 
2550 	return dest_opp;
2551 }
2552 EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
2553 
2554 /**
2555  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2556  * @src_table: OPP table which has dst_table as one of its required OPP table.
2557  * @dst_table: Required OPP table of the src_table.
2558  * @pstate: Current performance state of the src_table.
2559  *
2560  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2561  * "required-opps" property of the OPP (present in @src_table) which has
2562  * performance state set to @pstate.
2563  *
2564  * Return: Zero or positive performance state on success, otherwise negative
2565  * value on errors.
2566  */
2567 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2568 				       struct opp_table *dst_table,
2569 				       unsigned int pstate)
2570 {
2571 	struct dev_pm_opp *opp;
2572 	int dest_pstate = -EINVAL;
2573 	int i;
2574 
2575 	/*
2576 	 * Normally the src_table will have the "required_opps" property set to
2577 	 * point to one of the OPPs in the dst_table, but in some cases the
2578 	 * genpd and its master have one to one mapping of performance states
2579 	 * and so none of them have the "required-opps" property set. Return the
2580 	 * pstate of the src_table as it is in such cases.
2581 	 */
2582 	if (!src_table || !src_table->required_opp_count)
2583 		return pstate;
2584 
2585 	/* required-opps not fully initialized yet */
2586 	if (lazy_linking_pending(src_table))
2587 		return -EBUSY;
2588 
2589 	for (i = 0; i < src_table->required_opp_count; i++) {
2590 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2591 			break;
2592 	}
2593 
2594 	if (unlikely(i == src_table->required_opp_count)) {
2595 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2596 		       __func__, src_table, dst_table);
2597 		return -EINVAL;
2598 	}
2599 
2600 	mutex_lock(&src_table->lock);
2601 
2602 	list_for_each_entry(opp, &src_table->opp_list, node) {
2603 		if (opp->pstate == pstate) {
2604 			dest_pstate = opp->required_opps[i]->pstate;
2605 			goto unlock;
2606 		}
2607 	}
2608 
2609 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2610 	       dst_table);
2611 
2612 unlock:
2613 	mutex_unlock(&src_table->lock);
2614 
2615 	return dest_pstate;
2616 }
2617 
2618 /**
2619  * dev_pm_opp_add()  - Add an OPP table from a table definitions
2620  * @dev:	device for which we do this operation
2621  * @freq:	Frequency in Hz for this OPP
2622  * @u_volt:	Voltage in uVolts for this OPP
2623  *
2624  * This function adds an opp definition to the opp table and returns status.
2625  * The opp is made available by default and it can be controlled using
2626  * dev_pm_opp_enable/disable functions.
2627  *
2628  * Return:
2629  * 0		On success OR
2630  *		Duplicate OPPs (both freq and volt are same) and opp->available
2631  * -EEXIST	Freq are same and volt are different OR
2632  *		Duplicate OPPs (both freq and volt are same) and !opp->available
2633  * -ENOMEM	Memory allocation failure
2634  */
2635 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
2636 {
2637 	struct opp_table *opp_table;
2638 	int ret;
2639 
2640 	opp_table = _add_opp_table(dev, true);
2641 	if (IS_ERR(opp_table))
2642 		return PTR_ERR(opp_table);
2643 
2644 	/* Fix regulator count for dynamic OPPs */
2645 	opp_table->regulator_count = 1;
2646 
2647 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
2648 	if (ret)
2649 		dev_pm_opp_put_opp_table(opp_table);
2650 
2651 	return ret;
2652 }
2653 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
2654 
2655 /**
2656  * _opp_set_availability() - helper to set the availability of an opp
2657  * @dev:		device for which we do this operation
2658  * @freq:		OPP frequency to modify availability
2659  * @availability_req:	availability status requested for this opp
2660  *
2661  * Set the availability of an OPP, opp_{enable,disable} share a common logic
2662  * which is isolated here.
2663  *
2664  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2665  * copy operation, returns 0 if no modification was done OR modification was
2666  * successful.
2667  */
2668 static int _opp_set_availability(struct device *dev, unsigned long freq,
2669 				 bool availability_req)
2670 {
2671 	struct opp_table *opp_table;
2672 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2673 	int r = 0;
2674 
2675 	/* Find the opp_table */
2676 	opp_table = _find_opp_table(dev);
2677 	if (IS_ERR(opp_table)) {
2678 		r = PTR_ERR(opp_table);
2679 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2680 		return r;
2681 	}
2682 
2683 	mutex_lock(&opp_table->lock);
2684 
2685 	/* Do we have the frequency? */
2686 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2687 		if (tmp_opp->rate == freq) {
2688 			opp = tmp_opp;
2689 			break;
2690 		}
2691 	}
2692 
2693 	if (IS_ERR(opp)) {
2694 		r = PTR_ERR(opp);
2695 		goto unlock;
2696 	}
2697 
2698 	/* Is update really needed? */
2699 	if (opp->available == availability_req)
2700 		goto unlock;
2701 
2702 	opp->available = availability_req;
2703 
2704 	dev_pm_opp_get(opp);
2705 	mutex_unlock(&opp_table->lock);
2706 
2707 	/* Notify the change of the OPP availability */
2708 	if (availability_req)
2709 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
2710 					     opp);
2711 	else
2712 		blocking_notifier_call_chain(&opp_table->head,
2713 					     OPP_EVENT_DISABLE, opp);
2714 
2715 	dev_pm_opp_put(opp);
2716 	goto put_table;
2717 
2718 unlock:
2719 	mutex_unlock(&opp_table->lock);
2720 put_table:
2721 	dev_pm_opp_put_opp_table(opp_table);
2722 	return r;
2723 }
2724 
2725 /**
2726  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2727  * @dev:		device for which we do this operation
2728  * @freq:		OPP frequency to adjust voltage of
2729  * @u_volt:		new OPP target voltage
2730  * @u_volt_min:		new OPP min voltage
2731  * @u_volt_max:		new OPP max voltage
2732  *
2733  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2734  * copy operation, returns 0 if no modifcation was done OR modification was
2735  * successful.
2736  */
2737 int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2738 			      unsigned long u_volt, unsigned long u_volt_min,
2739 			      unsigned long u_volt_max)
2740 
2741 {
2742 	struct opp_table *opp_table;
2743 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2744 	int r = 0;
2745 
2746 	/* Find the opp_table */
2747 	opp_table = _find_opp_table(dev);
2748 	if (IS_ERR(opp_table)) {
2749 		r = PTR_ERR(opp_table);
2750 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2751 		return r;
2752 	}
2753 
2754 	mutex_lock(&opp_table->lock);
2755 
2756 	/* Do we have the frequency? */
2757 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2758 		if (tmp_opp->rate == freq) {
2759 			opp = tmp_opp;
2760 			break;
2761 		}
2762 	}
2763 
2764 	if (IS_ERR(opp)) {
2765 		r = PTR_ERR(opp);
2766 		goto adjust_unlock;
2767 	}
2768 
2769 	/* Is update really needed? */
2770 	if (opp->supplies->u_volt == u_volt)
2771 		goto adjust_unlock;
2772 
2773 	opp->supplies->u_volt = u_volt;
2774 	opp->supplies->u_volt_min = u_volt_min;
2775 	opp->supplies->u_volt_max = u_volt_max;
2776 
2777 	dev_pm_opp_get(opp);
2778 	mutex_unlock(&opp_table->lock);
2779 
2780 	/* Notify the voltage change of the OPP */
2781 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
2782 				     opp);
2783 
2784 	dev_pm_opp_put(opp);
2785 	goto adjust_put_table;
2786 
2787 adjust_unlock:
2788 	mutex_unlock(&opp_table->lock);
2789 adjust_put_table:
2790 	dev_pm_opp_put_opp_table(opp_table);
2791 	return r;
2792 }
2793 EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
2794 
2795 /**
2796  * dev_pm_opp_enable() - Enable a specific OPP
2797  * @dev:	device for which we do this operation
2798  * @freq:	OPP frequency to enable
2799  *
2800  * Enables a provided opp. If the operation is valid, this returns 0, else the
2801  * corresponding error value. It is meant to be used for users an OPP available
2802  * after being temporarily made unavailable with dev_pm_opp_disable.
2803  *
2804  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2805  * copy operation, returns 0 if no modification was done OR modification was
2806  * successful.
2807  */
2808 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
2809 {
2810 	return _opp_set_availability(dev, freq, true);
2811 }
2812 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
2813 
2814 /**
2815  * dev_pm_opp_disable() - Disable a specific OPP
2816  * @dev:	device for which we do this operation
2817  * @freq:	OPP frequency to disable
2818  *
2819  * Disables a provided opp. If the operation is valid, this returns
2820  * 0, else the corresponding error value. It is meant to be a temporary
2821  * control by users to make this OPP not available until the circumstances are
2822  * right to make it available again (with a call to dev_pm_opp_enable).
2823  *
2824  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2825  * copy operation, returns 0 if no modification was done OR modification was
2826  * successful.
2827  */
2828 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
2829 {
2830 	return _opp_set_availability(dev, freq, false);
2831 }
2832 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
2833 
2834 /**
2835  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2836  * @dev:	Device for which notifier needs to be registered
2837  * @nb:		Notifier block to be registered
2838  *
2839  * Return: 0 on success or a negative error value.
2840  */
2841 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
2842 {
2843 	struct opp_table *opp_table;
2844 	int ret;
2845 
2846 	opp_table = _find_opp_table(dev);
2847 	if (IS_ERR(opp_table))
2848 		return PTR_ERR(opp_table);
2849 
2850 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
2851 
2852 	dev_pm_opp_put_opp_table(opp_table);
2853 
2854 	return ret;
2855 }
2856 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2857 
2858 /**
2859  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2860  * @dev:	Device for which notifier needs to be unregistered
2861  * @nb:		Notifier block to be unregistered
2862  *
2863  * Return: 0 on success or a negative error value.
2864  */
2865 int dev_pm_opp_unregister_notifier(struct device *dev,
2866 				   struct notifier_block *nb)
2867 {
2868 	struct opp_table *opp_table;
2869 	int ret;
2870 
2871 	opp_table = _find_opp_table(dev);
2872 	if (IS_ERR(opp_table))
2873 		return PTR_ERR(opp_table);
2874 
2875 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
2876 
2877 	dev_pm_opp_put_opp_table(opp_table);
2878 
2879 	return ret;
2880 }
2881 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
2882 
2883 /**
2884  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2885  * @dev:	device pointer used to lookup OPP table.
2886  *
2887  * Free both OPPs created using static entries present in DT and the
2888  * dynamically added entries.
2889  */
2890 void dev_pm_opp_remove_table(struct device *dev)
2891 {
2892 	struct opp_table *opp_table;
2893 
2894 	/* Check for existing table for 'dev' */
2895 	opp_table = _find_opp_table(dev);
2896 	if (IS_ERR(opp_table)) {
2897 		int error = PTR_ERR(opp_table);
2898 
2899 		if (error != -ENODEV)
2900 			WARN(1, "%s: opp_table: %d\n",
2901 			     IS_ERR_OR_NULL(dev) ?
2902 					"Invalid device" : dev_name(dev),
2903 			     error);
2904 		return;
2905 	}
2906 
2907 	/*
2908 	 * Drop the extra reference only if the OPP table was successfully added
2909 	 * with dev_pm_opp_of_add_table() earlier.
2910 	 **/
2911 	if (_opp_remove_all_static(opp_table))
2912 		dev_pm_opp_put_opp_table(opp_table);
2913 
2914 	/* Drop reference taken by _find_opp_table() */
2915 	dev_pm_opp_put_opp_table(opp_table);
2916 }
2917 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
2918 
2919 /**
2920  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2921  * @dev:	device for which we do this operation
2922  *
2923  * Sync voltage state of the OPP table regulators.
2924  *
2925  * Return: 0 on success or a negative error value.
2926  */
2927 int dev_pm_opp_sync_regulators(struct device *dev)
2928 {
2929 	struct opp_table *opp_table;
2930 	struct regulator *reg;
2931 	int i, ret = 0;
2932 
2933 	/* Device may not have OPP table */
2934 	opp_table = _find_opp_table(dev);
2935 	if (IS_ERR(opp_table))
2936 		return 0;
2937 
2938 	/* Regulator may not be required for the device */
2939 	if (unlikely(!opp_table->regulators))
2940 		goto put_table;
2941 
2942 	/* Nothing to sync if voltage wasn't changed */
2943 	if (!opp_table->enabled)
2944 		goto put_table;
2945 
2946 	for (i = 0; i < opp_table->regulator_count; i++) {
2947 		reg = opp_table->regulators[i];
2948 		ret = regulator_sync_voltage(reg);
2949 		if (ret)
2950 			break;
2951 	}
2952 put_table:
2953 	/* Drop reference taken by _find_opp_table() */
2954 	dev_pm_opp_put_opp_table(opp_table);
2955 
2956 	return ret;
2957 }
2958 EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
2959