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