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