xref: /openbmc/linux/drivers/opp/core.c (revision 27c09484dd3d8fdb56eb3787877d6035d0e89669)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27813dd6fSViresh Kumar /*
37813dd6fSViresh Kumar  * Generic OPP Interface
47813dd6fSViresh Kumar  *
57813dd6fSViresh Kumar  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
67813dd6fSViresh Kumar  *	Nishanth Menon
77813dd6fSViresh Kumar  *	Romit Dasgupta
87813dd6fSViresh Kumar  *	Kevin Hilman
97813dd6fSViresh Kumar  */
107813dd6fSViresh Kumar 
117813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
127813dd6fSViresh Kumar 
137813dd6fSViresh Kumar #include <linux/clk.h>
147813dd6fSViresh Kumar #include <linux/errno.h>
157813dd6fSViresh Kumar #include <linux/err.h>
167813dd6fSViresh Kumar #include <linux/slab.h>
177813dd6fSViresh Kumar #include <linux/device.h>
187813dd6fSViresh Kumar #include <linux/export.h>
19009acd19SViresh Kumar #include <linux/pm_domain.h>
207813dd6fSViresh Kumar #include <linux/regulator/consumer.h>
217813dd6fSViresh Kumar 
227813dd6fSViresh Kumar #include "opp.h"
237813dd6fSViresh Kumar 
247813dd6fSViresh Kumar /*
257813dd6fSViresh Kumar  * The root of the list of all opp-tables. All opp_table structures branch off
267813dd6fSViresh Kumar  * from here, with each opp_table containing the list of opps it supports in
277813dd6fSViresh Kumar  * various states of availability.
287813dd6fSViresh Kumar  */
297813dd6fSViresh Kumar LIST_HEAD(opp_tables);
307813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
317813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
32*27c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */
33*27c09484SViresh Kumar static bool opp_tables_busy;
347813dd6fSViresh Kumar 
357813dd6fSViresh Kumar static struct opp_device *_find_opp_dev(const struct device *dev,
367813dd6fSViresh Kumar 					struct opp_table *opp_table)
377813dd6fSViresh Kumar {
387813dd6fSViresh Kumar 	struct opp_device *opp_dev;
397813dd6fSViresh Kumar 
407813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
417813dd6fSViresh Kumar 		if (opp_dev->dev == dev)
427813dd6fSViresh Kumar 			return opp_dev;
437813dd6fSViresh Kumar 
447813dd6fSViresh Kumar 	return NULL;
457813dd6fSViresh Kumar }
467813dd6fSViresh Kumar 
477813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
487813dd6fSViresh Kumar {
497813dd6fSViresh Kumar 	struct opp_table *opp_table;
503d255699SViresh Kumar 	bool found;
517813dd6fSViresh Kumar 
527813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
533d255699SViresh Kumar 		mutex_lock(&opp_table->lock);
543d255699SViresh Kumar 		found = !!_find_opp_dev(dev, opp_table);
553d255699SViresh Kumar 		mutex_unlock(&opp_table->lock);
563d255699SViresh Kumar 
573d255699SViresh Kumar 		if (found) {
587813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
597813dd6fSViresh Kumar 
607813dd6fSViresh Kumar 			return opp_table;
617813dd6fSViresh Kumar 		}
627813dd6fSViresh Kumar 	}
637813dd6fSViresh Kumar 
647813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
657813dd6fSViresh Kumar }
667813dd6fSViresh Kumar 
677813dd6fSViresh Kumar /**
687813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
697813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
707813dd6fSViresh Kumar  *
717813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
727813dd6fSViresh Kumar  *
737813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
747813dd6fSViresh Kumar  * -EINVAL based on type of error.
757813dd6fSViresh Kumar  *
767813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
777813dd6fSViresh Kumar  */
787813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
797813dd6fSViresh Kumar {
807813dd6fSViresh Kumar 	struct opp_table *opp_table;
817813dd6fSViresh Kumar 
827813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
837813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
847813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
857813dd6fSViresh Kumar 	}
867813dd6fSViresh Kumar 
877813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
887813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
897813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
907813dd6fSViresh Kumar 
917813dd6fSViresh Kumar 	return opp_table;
927813dd6fSViresh Kumar }
937813dd6fSViresh Kumar 
947813dd6fSViresh Kumar /**
957813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
967813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
977813dd6fSViresh Kumar  *
987813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
997813dd6fSViresh Kumar  * return 0
1007813dd6fSViresh Kumar  *
1017813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1027813dd6fSViresh Kumar  */
1037813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1047813dd6fSViresh Kumar {
1057813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1067813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1077813dd6fSViresh Kumar 		return 0;
1087813dd6fSViresh Kumar 	}
1097813dd6fSViresh Kumar 
1107813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1117813dd6fSViresh Kumar }
1127813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1137813dd6fSViresh Kumar 
1147813dd6fSViresh Kumar /**
1157813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1167813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1177813dd6fSViresh Kumar  *
1187813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1197813dd6fSViresh Kumar  * return 0
1207813dd6fSViresh Kumar  */
1217813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1227813dd6fSViresh Kumar {
12306a8a059SAndrew-sh.Cheng 	if (IS_ERR_OR_NULL(opp)) {
1247813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1257813dd6fSViresh Kumar 		return 0;
1267813dd6fSViresh Kumar 	}
1277813dd6fSViresh Kumar 
1287813dd6fSViresh Kumar 	return opp->rate;
1297813dd6fSViresh Kumar }
1307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
1317813dd6fSViresh Kumar 
1327813dd6fSViresh Kumar /**
1335b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
1345b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
1355b93ac54SRajendra Nayak  *
1365b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
1375b93ac54SRajendra Nayak  * return 0.
1385b93ac54SRajendra Nayak  */
1395b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
1405b93ac54SRajendra Nayak {
1415b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1425b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
1435b93ac54SRajendra Nayak 		return 0;
1445b93ac54SRajendra Nayak 	}
1455b93ac54SRajendra Nayak 
1465b93ac54SRajendra Nayak 	return opp->level;
1475b93ac54SRajendra Nayak }
1485b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
1495b93ac54SRajendra Nayak 
1505b93ac54SRajendra Nayak /**
1517813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
1527813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
1537813dd6fSViresh Kumar  *
1547813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
1557813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
1567813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
1577813dd6fSViresh Kumar  *
1587813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
1597813dd6fSViresh Kumar  */
1607813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
1617813dd6fSViresh Kumar {
1627813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1637813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1647813dd6fSViresh Kumar 		return false;
1657813dd6fSViresh Kumar 	}
1667813dd6fSViresh Kumar 
1677813dd6fSViresh Kumar 	return opp->turbo;
1687813dd6fSViresh Kumar }
1697813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
1707813dd6fSViresh Kumar 
1717813dd6fSViresh Kumar /**
1727813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
1737813dd6fSViresh Kumar  * @dev:	device for which we do this operation
1747813dd6fSViresh Kumar  *
1757813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
1767813dd6fSViresh Kumar  */
1777813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
1787813dd6fSViresh Kumar {
1797813dd6fSViresh Kumar 	struct opp_table *opp_table;
1807813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
1817813dd6fSViresh Kumar 
1827813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
1837813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
1847813dd6fSViresh Kumar 		return 0;
1857813dd6fSViresh Kumar 
1867813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
1877813dd6fSViresh Kumar 
1887813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1897813dd6fSViresh Kumar 
1907813dd6fSViresh Kumar 	return clock_latency_ns;
1917813dd6fSViresh Kumar }
1927813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
1937813dd6fSViresh Kumar 
1947813dd6fSViresh Kumar /**
1957813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
1967813dd6fSViresh Kumar  * @dev: device for which we do this operation
1977813dd6fSViresh Kumar  *
1987813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
1997813dd6fSViresh Kumar  */
2007813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
2017813dd6fSViresh Kumar {
2027813dd6fSViresh Kumar 	struct opp_table *opp_table;
2037813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
2047813dd6fSViresh Kumar 	struct regulator *reg;
2057813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
2067813dd6fSViresh Kumar 	int ret, i, count;
2077813dd6fSViresh Kumar 	struct {
2087813dd6fSViresh Kumar 		unsigned long min;
2097813dd6fSViresh Kumar 		unsigned long max;
2107813dd6fSViresh Kumar 	} *uV;
2117813dd6fSViresh Kumar 
2127813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2137813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2147813dd6fSViresh Kumar 		return 0;
2157813dd6fSViresh Kumar 
2167813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
21790e3577bSViresh Kumar 	if (!opp_table->regulators)
2187813dd6fSViresh Kumar 		goto put_opp_table;
2197813dd6fSViresh Kumar 
22090e3577bSViresh Kumar 	count = opp_table->regulator_count;
22190e3577bSViresh Kumar 
2227813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
2237813dd6fSViresh Kumar 	if (!uV)
2247813dd6fSViresh Kumar 		goto put_opp_table;
2257813dd6fSViresh Kumar 
2267813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
2277813dd6fSViresh Kumar 
2287813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2297813dd6fSViresh Kumar 		uV[i].min = ~0;
2307813dd6fSViresh Kumar 		uV[i].max = 0;
2317813dd6fSViresh Kumar 
2327813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
2337813dd6fSViresh Kumar 			if (!opp->available)
2347813dd6fSViresh Kumar 				continue;
2357813dd6fSViresh Kumar 
2367813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
2377813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
2387813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
2397813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
2407813dd6fSViresh Kumar 		}
2417813dd6fSViresh Kumar 	}
2427813dd6fSViresh Kumar 
2437813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
2447813dd6fSViresh Kumar 
2457813dd6fSViresh Kumar 	/*
2467813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
2477813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
2487813dd6fSViresh Kumar 	 */
2497813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2507813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
2517813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
2527813dd6fSViresh Kumar 		if (ret > 0)
2537813dd6fSViresh Kumar 			latency_ns += ret * 1000;
2547813dd6fSViresh Kumar 	}
2557813dd6fSViresh Kumar 
2567813dd6fSViresh Kumar 	kfree(uV);
2577813dd6fSViresh Kumar put_opp_table:
2587813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2597813dd6fSViresh Kumar 
2607813dd6fSViresh Kumar 	return latency_ns;
2617813dd6fSViresh Kumar }
2627813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
2637813dd6fSViresh Kumar 
2647813dd6fSViresh Kumar /**
2657813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
2667813dd6fSViresh Kumar  *					     nanoseconds
2677813dd6fSViresh Kumar  * @dev: device for which we do this operation
2687813dd6fSViresh Kumar  *
2697813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
2707813dd6fSViresh Kumar  * switch from one OPP to other.
2717813dd6fSViresh Kumar  */
2727813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
2737813dd6fSViresh Kumar {
2747813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
2757813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
2767813dd6fSViresh Kumar }
2777813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
2787813dd6fSViresh Kumar 
2797813dd6fSViresh Kumar /**
2807813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
2817813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2827813dd6fSViresh Kumar  *
2837813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
2847813dd6fSViresh Kumar  * if one is available, else returns 0;
2857813dd6fSViresh Kumar  */
2867813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
2877813dd6fSViresh Kumar {
2887813dd6fSViresh Kumar 	struct opp_table *opp_table;
2897813dd6fSViresh Kumar 	unsigned long freq = 0;
2907813dd6fSViresh Kumar 
2917813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2927813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2937813dd6fSViresh Kumar 		return 0;
2947813dd6fSViresh Kumar 
2957813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
2967813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
2977813dd6fSViresh Kumar 
2987813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2997813dd6fSViresh Kumar 
3007813dd6fSViresh Kumar 	return freq;
3017813dd6fSViresh Kumar }
3027813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
3037813dd6fSViresh Kumar 
304a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
305a1e8c136SViresh Kumar {
306a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
307a1e8c136SViresh Kumar 	int count = 0;
308a1e8c136SViresh Kumar 
309a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
310a1e8c136SViresh Kumar 
311a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
312a1e8c136SViresh Kumar 		if (opp->available)
313a1e8c136SViresh Kumar 			count++;
314a1e8c136SViresh Kumar 	}
315a1e8c136SViresh Kumar 
316a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
317a1e8c136SViresh Kumar 
318a1e8c136SViresh Kumar 	return count;
319a1e8c136SViresh Kumar }
320a1e8c136SViresh Kumar 
3217813dd6fSViresh Kumar /**
3227813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
3237813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3247813dd6fSViresh Kumar  *
3257813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
3267813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
3277813dd6fSViresh Kumar  */
3287813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
3297813dd6fSViresh Kumar {
3307813dd6fSViresh Kumar 	struct opp_table *opp_table;
331a1e8c136SViresh Kumar 	int count;
3327813dd6fSViresh Kumar 
3337813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3347813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
3357813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
336035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
3377813dd6fSViresh Kumar 			__func__, count);
33809f662f9SViresh Kumar 		return count;
3397813dd6fSViresh Kumar 	}
3407813dd6fSViresh Kumar 
341a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
3427813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3437813dd6fSViresh Kumar 
3447813dd6fSViresh Kumar 	return count;
3457813dd6fSViresh Kumar }
3467813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
3477813dd6fSViresh Kumar 
3487813dd6fSViresh Kumar /**
3497813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
3507813dd6fSViresh Kumar  * @dev:		device for which we do this operation
3517813dd6fSViresh Kumar  * @freq:		frequency to search for
3527813dd6fSViresh Kumar  * @available:		true/false - match for available opp
3537813dd6fSViresh Kumar  *
3547813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
3557813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
3567813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
3577813dd6fSViresh Kumar  * EINVAL:	for bad pointer
3587813dd6fSViresh Kumar  * ERANGE:	no match found for search
3597813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
3607813dd6fSViresh Kumar  *
3617813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
3627813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
3637813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
3647813dd6fSViresh Kumar  *
3657813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
3667813dd6fSViresh Kumar  * or the opposite as well.
3677813dd6fSViresh Kumar  *
3687813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
3697813dd6fSViresh Kumar  * use.
3707813dd6fSViresh Kumar  */
3717813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
3727813dd6fSViresh Kumar 					      unsigned long freq,
3737813dd6fSViresh Kumar 					      bool available)
3747813dd6fSViresh Kumar {
3757813dd6fSViresh Kumar 	struct opp_table *opp_table;
3767813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
3777813dd6fSViresh Kumar 
3787813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3797813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
3807813dd6fSViresh Kumar 		int r = PTR_ERR(opp_table);
3817813dd6fSViresh Kumar 
3827813dd6fSViresh Kumar 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
3837813dd6fSViresh Kumar 		return ERR_PTR(r);
3847813dd6fSViresh Kumar 	}
3857813dd6fSViresh Kumar 
3867813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
3877813dd6fSViresh Kumar 
3887813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
3897813dd6fSViresh Kumar 		if (temp_opp->available == available &&
3907813dd6fSViresh Kumar 				temp_opp->rate == freq) {
3917813dd6fSViresh Kumar 			opp = temp_opp;
3927813dd6fSViresh Kumar 
3937813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
3947813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
3957813dd6fSViresh Kumar 			break;
3967813dd6fSViresh Kumar 		}
3977813dd6fSViresh Kumar 	}
3987813dd6fSViresh Kumar 
3997813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
4007813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4017813dd6fSViresh Kumar 
4027813dd6fSViresh Kumar 	return opp;
4037813dd6fSViresh Kumar }
4047813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
4057813dd6fSViresh Kumar 
40671419d84SNiklas Cassel /**
40771419d84SNiklas Cassel  * dev_pm_opp_find_level_exact() - search for an exact level
40871419d84SNiklas Cassel  * @dev:		device for which we do this operation
40971419d84SNiklas Cassel  * @level:		level to search for
41071419d84SNiklas Cassel  *
41171419d84SNiklas Cassel  * Return: Searches for exact match in the opp table and returns pointer to the
41271419d84SNiklas Cassel  * matching opp if found, else returns ERR_PTR in case of error and should
41371419d84SNiklas Cassel  * be handled using IS_ERR. Error return values can be:
41471419d84SNiklas Cassel  * EINVAL:	for bad pointer
41571419d84SNiklas Cassel  * ERANGE:	no match found for search
41671419d84SNiklas Cassel  * ENODEV:	if device not found in list of registered devices
41771419d84SNiklas Cassel  *
41871419d84SNiklas Cassel  * The callers are required to call dev_pm_opp_put() for the returned OPP after
41971419d84SNiklas Cassel  * use.
42071419d84SNiklas Cassel  */
42171419d84SNiklas Cassel struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
42271419d84SNiklas Cassel 					       unsigned int level)
42371419d84SNiklas Cassel {
42471419d84SNiklas Cassel 	struct opp_table *opp_table;
42571419d84SNiklas Cassel 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
42671419d84SNiklas Cassel 
42771419d84SNiklas Cassel 	opp_table = _find_opp_table(dev);
42871419d84SNiklas Cassel 	if (IS_ERR(opp_table)) {
42971419d84SNiklas Cassel 		int r = PTR_ERR(opp_table);
43071419d84SNiklas Cassel 
43171419d84SNiklas Cassel 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
43271419d84SNiklas Cassel 		return ERR_PTR(r);
43371419d84SNiklas Cassel 	}
43471419d84SNiklas Cassel 
43571419d84SNiklas Cassel 	mutex_lock(&opp_table->lock);
43671419d84SNiklas Cassel 
43771419d84SNiklas Cassel 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
43871419d84SNiklas Cassel 		if (temp_opp->level == level) {
43971419d84SNiklas Cassel 			opp = temp_opp;
44071419d84SNiklas Cassel 
44171419d84SNiklas Cassel 			/* Increment the reference count of OPP */
44271419d84SNiklas Cassel 			dev_pm_opp_get(opp);
44371419d84SNiklas Cassel 			break;
44471419d84SNiklas Cassel 		}
44571419d84SNiklas Cassel 	}
44671419d84SNiklas Cassel 
44771419d84SNiklas Cassel 	mutex_unlock(&opp_table->lock);
44871419d84SNiklas Cassel 	dev_pm_opp_put_opp_table(opp_table);
44971419d84SNiklas Cassel 
45071419d84SNiklas Cassel 	return opp;
45171419d84SNiklas Cassel }
45271419d84SNiklas Cassel EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
45371419d84SNiklas Cassel 
4547813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
4557813dd6fSViresh Kumar 						   unsigned long *freq)
4567813dd6fSViresh Kumar {
4577813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
4587813dd6fSViresh Kumar 
4597813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
4607813dd6fSViresh Kumar 
4617813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
4627813dd6fSViresh Kumar 		if (temp_opp->available && temp_opp->rate >= *freq) {
4637813dd6fSViresh Kumar 			opp = temp_opp;
4647813dd6fSViresh Kumar 			*freq = opp->rate;
4657813dd6fSViresh Kumar 
4667813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
4677813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
4687813dd6fSViresh Kumar 			break;
4697813dd6fSViresh Kumar 		}
4707813dd6fSViresh Kumar 	}
4717813dd6fSViresh Kumar 
4727813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
4737813dd6fSViresh Kumar 
4747813dd6fSViresh Kumar 	return opp;
4757813dd6fSViresh Kumar }
4767813dd6fSViresh Kumar 
4777813dd6fSViresh Kumar /**
4787813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
4797813dd6fSViresh Kumar  * @dev:	device for which we do this operation
4807813dd6fSViresh Kumar  * @freq:	Start frequency
4817813dd6fSViresh Kumar  *
4827813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
4837813dd6fSViresh Kumar  * for a device.
4847813dd6fSViresh Kumar  *
4857813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
4867813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
4877813dd6fSViresh Kumar  * values can be:
4887813dd6fSViresh Kumar  * EINVAL:	for bad pointer
4897813dd6fSViresh Kumar  * ERANGE:	no match found for search
4907813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
4917813dd6fSViresh Kumar  *
4927813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
4937813dd6fSViresh Kumar  * use.
4947813dd6fSViresh Kumar  */
4957813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
4967813dd6fSViresh Kumar 					     unsigned long *freq)
4977813dd6fSViresh Kumar {
4987813dd6fSViresh Kumar 	struct opp_table *opp_table;
4997813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
5007813dd6fSViresh Kumar 
5017813dd6fSViresh Kumar 	if (!dev || !freq) {
5027813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
5037813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
5047813dd6fSViresh Kumar 	}
5057813dd6fSViresh Kumar 
5067813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
5077813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
5087813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
5097813dd6fSViresh Kumar 
5107813dd6fSViresh Kumar 	opp = _find_freq_ceil(opp_table, freq);
5117813dd6fSViresh Kumar 
5127813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
5137813dd6fSViresh Kumar 
5147813dd6fSViresh Kumar 	return opp;
5157813dd6fSViresh Kumar }
5167813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
5177813dd6fSViresh Kumar 
5187813dd6fSViresh Kumar /**
5197813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
5207813dd6fSViresh Kumar  * @dev:	device for which we do this operation
5217813dd6fSViresh Kumar  * @freq:	Start frequency
5227813dd6fSViresh Kumar  *
5237813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
5247813dd6fSViresh Kumar  * for a device.
5257813dd6fSViresh Kumar  *
5267813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
5277813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
5287813dd6fSViresh Kumar  * values can be:
5297813dd6fSViresh Kumar  * EINVAL:	for bad pointer
5307813dd6fSViresh Kumar  * ERANGE:	no match found for search
5317813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
5327813dd6fSViresh Kumar  *
5337813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5347813dd6fSViresh Kumar  * use.
5357813dd6fSViresh Kumar  */
5367813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
5377813dd6fSViresh Kumar 					      unsigned long *freq)
5387813dd6fSViresh Kumar {
5397813dd6fSViresh Kumar 	struct opp_table *opp_table;
5407813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5417813dd6fSViresh Kumar 
5427813dd6fSViresh Kumar 	if (!dev || !freq) {
5437813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
5447813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
5457813dd6fSViresh Kumar 	}
5467813dd6fSViresh Kumar 
5477813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
5487813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
5497813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
5507813dd6fSViresh Kumar 
5517813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
5527813dd6fSViresh Kumar 
5537813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5547813dd6fSViresh Kumar 		if (temp_opp->available) {
5557813dd6fSViresh Kumar 			/* go to the next node, before choosing prev */
5567813dd6fSViresh Kumar 			if (temp_opp->rate > *freq)
5577813dd6fSViresh Kumar 				break;
5587813dd6fSViresh Kumar 			else
5597813dd6fSViresh Kumar 				opp = temp_opp;
5607813dd6fSViresh Kumar 		}
5617813dd6fSViresh Kumar 	}
5627813dd6fSViresh Kumar 
5637813dd6fSViresh Kumar 	/* Increment the reference count of OPP */
5647813dd6fSViresh Kumar 	if (!IS_ERR(opp))
5657813dd6fSViresh Kumar 		dev_pm_opp_get(opp);
5667813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
5677813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
5687813dd6fSViresh Kumar 
5697813dd6fSViresh Kumar 	if (!IS_ERR(opp))
5707813dd6fSViresh Kumar 		*freq = opp->rate;
5717813dd6fSViresh Kumar 
5727813dd6fSViresh Kumar 	return opp;
5737813dd6fSViresh Kumar }
5747813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
5757813dd6fSViresh Kumar 
5762f36bde0SAndrew-sh.Cheng /**
5772f36bde0SAndrew-sh.Cheng  * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
5782f36bde0SAndrew-sh.Cheng  *					 target voltage.
5792f36bde0SAndrew-sh.Cheng  * @dev:	Device for which we do this operation.
5802f36bde0SAndrew-sh.Cheng  * @u_volt:	Target voltage.
5812f36bde0SAndrew-sh.Cheng  *
5822f36bde0SAndrew-sh.Cheng  * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
5832f36bde0SAndrew-sh.Cheng  *
5842f36bde0SAndrew-sh.Cheng  * Return: matching *opp, else returns ERR_PTR in case of error which should be
5852f36bde0SAndrew-sh.Cheng  * handled using IS_ERR.
5862f36bde0SAndrew-sh.Cheng  *
5872f36bde0SAndrew-sh.Cheng  * Error return values can be:
5882f36bde0SAndrew-sh.Cheng  * EINVAL:	bad parameters
5892f36bde0SAndrew-sh.Cheng  *
5902f36bde0SAndrew-sh.Cheng  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5912f36bde0SAndrew-sh.Cheng  * use.
5922f36bde0SAndrew-sh.Cheng  */
5932f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
5942f36bde0SAndrew-sh.Cheng 						     unsigned long u_volt)
5952f36bde0SAndrew-sh.Cheng {
5962f36bde0SAndrew-sh.Cheng 	struct opp_table *opp_table;
5972f36bde0SAndrew-sh.Cheng 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5982f36bde0SAndrew-sh.Cheng 
5992f36bde0SAndrew-sh.Cheng 	if (!dev || !u_volt) {
6002f36bde0SAndrew-sh.Cheng 		dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
6012f36bde0SAndrew-sh.Cheng 			u_volt);
6022f36bde0SAndrew-sh.Cheng 		return ERR_PTR(-EINVAL);
6032f36bde0SAndrew-sh.Cheng 	}
6042f36bde0SAndrew-sh.Cheng 
6052f36bde0SAndrew-sh.Cheng 	opp_table = _find_opp_table(dev);
6062f36bde0SAndrew-sh.Cheng 	if (IS_ERR(opp_table))
6072f36bde0SAndrew-sh.Cheng 		return ERR_CAST(opp_table);
6082f36bde0SAndrew-sh.Cheng 
6092f36bde0SAndrew-sh.Cheng 	mutex_lock(&opp_table->lock);
6102f36bde0SAndrew-sh.Cheng 
6112f36bde0SAndrew-sh.Cheng 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
6122f36bde0SAndrew-sh.Cheng 		if (temp_opp->available) {
6132f36bde0SAndrew-sh.Cheng 			if (temp_opp->supplies[0].u_volt > u_volt)
6142f36bde0SAndrew-sh.Cheng 				break;
6152f36bde0SAndrew-sh.Cheng 			opp = temp_opp;
6162f36bde0SAndrew-sh.Cheng 		}
6172f36bde0SAndrew-sh.Cheng 	}
6182f36bde0SAndrew-sh.Cheng 
6192f36bde0SAndrew-sh.Cheng 	/* Increment the reference count of OPP */
6202f36bde0SAndrew-sh.Cheng 	if (!IS_ERR(opp))
6212f36bde0SAndrew-sh.Cheng 		dev_pm_opp_get(opp);
6222f36bde0SAndrew-sh.Cheng 
6232f36bde0SAndrew-sh.Cheng 	mutex_unlock(&opp_table->lock);
6242f36bde0SAndrew-sh.Cheng 	dev_pm_opp_put_opp_table(opp_table);
6252f36bde0SAndrew-sh.Cheng 
6262f36bde0SAndrew-sh.Cheng 	return opp;
6272f36bde0SAndrew-sh.Cheng }
6282f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
6292f36bde0SAndrew-sh.Cheng 
6307813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
6317813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
6327813dd6fSViresh Kumar {
6337813dd6fSViresh Kumar 	int ret;
6347813dd6fSViresh Kumar 
6357813dd6fSViresh Kumar 	/* Regulator not available for device */
6367813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
6377813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
6387813dd6fSViresh Kumar 			PTR_ERR(reg));
6397813dd6fSViresh Kumar 		return 0;
6407813dd6fSViresh Kumar 	}
6417813dd6fSViresh Kumar 
6427813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
6437813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
6447813dd6fSViresh Kumar 
6457813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
6467813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
6477813dd6fSViresh Kumar 	if (ret)
6487813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
6497813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
6507813dd6fSViresh Kumar 			supply->u_volt_max, ret);
6517813dd6fSViresh Kumar 
6527813dd6fSViresh Kumar 	return ret;
6537813dd6fSViresh Kumar }
6547813dd6fSViresh Kumar 
655285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
656285881b5SViresh Kumar 					    unsigned long freq)
6577813dd6fSViresh Kumar {
6587813dd6fSViresh Kumar 	int ret;
6597813dd6fSViresh Kumar 
6607813dd6fSViresh Kumar 	ret = clk_set_rate(clk, freq);
6617813dd6fSViresh Kumar 	if (ret) {
6627813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
6637813dd6fSViresh Kumar 			ret);
6647813dd6fSViresh Kumar 	}
6657813dd6fSViresh Kumar 
6667813dd6fSViresh Kumar 	return ret;
6677813dd6fSViresh Kumar }
6687813dd6fSViresh Kumar 
6698d45719cSKamil Konieczny static int _generic_set_opp_regulator(struct opp_table *opp_table,
6707813dd6fSViresh Kumar 				      struct device *dev,
6717813dd6fSViresh Kumar 				      unsigned long old_freq,
6727813dd6fSViresh Kumar 				      unsigned long freq,
6737813dd6fSViresh Kumar 				      struct dev_pm_opp_supply *old_supply,
6747813dd6fSViresh Kumar 				      struct dev_pm_opp_supply *new_supply)
6757813dd6fSViresh Kumar {
6767813dd6fSViresh Kumar 	struct regulator *reg = opp_table->regulators[0];
6777813dd6fSViresh Kumar 	int ret;
6787813dd6fSViresh Kumar 
6797813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
6807813dd6fSViresh Kumar 	if (WARN_ON(opp_table->regulator_count > 1)) {
6817813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
6827813dd6fSViresh Kumar 		return -EINVAL;
6837813dd6fSViresh Kumar 	}
6847813dd6fSViresh Kumar 
6857813dd6fSViresh Kumar 	/* Scaling up? Scale voltage before frequency */
686c5c2a97bSWaldemar Rymarkiewicz 	if (freq >= old_freq) {
6877813dd6fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, new_supply);
6887813dd6fSViresh Kumar 		if (ret)
6897813dd6fSViresh Kumar 			goto restore_voltage;
6907813dd6fSViresh Kumar 	}
6917813dd6fSViresh Kumar 
6927813dd6fSViresh Kumar 	/* Change frequency */
693285881b5SViresh Kumar 	ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
6947813dd6fSViresh Kumar 	if (ret)
6957813dd6fSViresh Kumar 		goto restore_voltage;
6967813dd6fSViresh Kumar 
6977813dd6fSViresh Kumar 	/* Scaling down? Scale voltage after frequency */
6987813dd6fSViresh Kumar 	if (freq < old_freq) {
6997813dd6fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, new_supply);
7007813dd6fSViresh Kumar 		if (ret)
7017813dd6fSViresh Kumar 			goto restore_freq;
7027813dd6fSViresh Kumar 	}
7037813dd6fSViresh Kumar 
7048d45719cSKamil Konieczny 	/*
7058d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
7068d45719cSKamil Konieczny 	 * some boot-enabled regulators.
7078d45719cSKamil Konieczny 	 */
70872f80ce4SViresh Kumar 	if (unlikely(!opp_table->enabled)) {
7098d45719cSKamil Konieczny 		ret = regulator_enable(reg);
7108d45719cSKamil Konieczny 		if (ret < 0)
7118d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
7128d45719cSKamil Konieczny 	}
7138d45719cSKamil Konieczny 
7147813dd6fSViresh Kumar 	return 0;
7157813dd6fSViresh Kumar 
7167813dd6fSViresh Kumar restore_freq:
717285881b5SViresh Kumar 	if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
7187813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
7197813dd6fSViresh Kumar 			__func__, old_freq);
7207813dd6fSViresh Kumar restore_voltage:
7217813dd6fSViresh Kumar 	/* This shouldn't harm even if the voltages weren't updated earlier */
7227813dd6fSViresh Kumar 	if (old_supply)
7237813dd6fSViresh Kumar 		_set_opp_voltage(dev, reg, old_supply);
7247813dd6fSViresh Kumar 
7257813dd6fSViresh Kumar 	return ret;
7267813dd6fSViresh Kumar }
7277813dd6fSViresh Kumar 
728b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
729b00e667aSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev, bool remove)
730b00e667aSViresh Kumar {
731b00e667aSViresh Kumar 	u32 avg, peak;
732b00e667aSViresh Kumar 	int i, ret;
733b00e667aSViresh Kumar 
734b00e667aSViresh Kumar 	if (!opp_table->paths)
735b00e667aSViresh Kumar 		return 0;
736b00e667aSViresh Kumar 
737b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
738b00e667aSViresh Kumar 		if (remove) {
739b00e667aSViresh Kumar 			avg = 0;
740b00e667aSViresh Kumar 			peak = 0;
741b00e667aSViresh Kumar 		} else {
742b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
743b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
744b00e667aSViresh Kumar 		}
745b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
746b00e667aSViresh Kumar 		if (ret) {
747b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
748b00e667aSViresh Kumar 				remove ? "remove" : "set", i, ret);
749b00e667aSViresh Kumar 			return ret;
750b00e667aSViresh Kumar 		}
751b00e667aSViresh Kumar 	}
752b00e667aSViresh Kumar 
753b00e667aSViresh Kumar 	return 0;
754b00e667aSViresh Kumar }
755b00e667aSViresh Kumar 
7567e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table,
7577e535993SViresh Kumar 			   struct device *dev, unsigned long old_freq,
7587e535993SViresh Kumar 			   unsigned long freq,
7597e535993SViresh Kumar 			   struct dev_pm_opp_supply *old_supply,
7607e535993SViresh Kumar 			   struct dev_pm_opp_supply *new_supply)
7617e535993SViresh Kumar {
7627e535993SViresh Kumar 	struct dev_pm_set_opp_data *data;
7637e535993SViresh Kumar 	int size;
7647e535993SViresh Kumar 
7657e535993SViresh Kumar 	data = opp_table->set_opp_data;
7667e535993SViresh Kumar 	data->regulators = opp_table->regulators;
7677e535993SViresh Kumar 	data->regulator_count = opp_table->regulator_count;
7687e535993SViresh Kumar 	data->clk = opp_table->clk;
7697e535993SViresh Kumar 	data->dev = dev;
7707e535993SViresh Kumar 
7717e535993SViresh Kumar 	data->old_opp.rate = old_freq;
7727e535993SViresh Kumar 	size = sizeof(*old_supply) * opp_table->regulator_count;
773560d1bcaSDmitry Osipenko 	if (!old_supply)
7747e535993SViresh Kumar 		memset(data->old_opp.supplies, 0, size);
7757e535993SViresh Kumar 	else
7767e535993SViresh Kumar 		memcpy(data->old_opp.supplies, old_supply, size);
7777e535993SViresh Kumar 
7787e535993SViresh Kumar 	data->new_opp.rate = freq;
7797e535993SViresh Kumar 	memcpy(data->new_opp.supplies, new_supply, size);
7807e535993SViresh Kumar 
7817e535993SViresh Kumar 	return opp_table->set_opp(data);
7827e535993SViresh Kumar }
7837e535993SViresh Kumar 
78460cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev,
78560cdeae0SStephan Gerhold 			     struct dev_pm_opp *opp, int i)
78660cdeae0SStephan Gerhold {
78760cdeae0SStephan Gerhold 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
78860cdeae0SStephan Gerhold 	int ret;
78960cdeae0SStephan Gerhold 
79060cdeae0SStephan Gerhold 	if (!pd_dev)
79160cdeae0SStephan Gerhold 		return 0;
79260cdeae0SStephan Gerhold 
79360cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
79460cdeae0SStephan Gerhold 	if (ret) {
79560cdeae0SStephan Gerhold 		dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
79660cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
79760cdeae0SStephan Gerhold 	}
79860cdeae0SStephan Gerhold 
79960cdeae0SStephan Gerhold 	return ret;
80060cdeae0SStephan Gerhold }
80160cdeae0SStephan Gerhold 
802ca1b5d77SViresh Kumar /* This is only called for PM domain for now */
803ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev,
804ca1b5d77SViresh Kumar 			      struct opp_table *opp_table,
8052c59138cSStephan Gerhold 			      struct dev_pm_opp *opp, bool up)
806ca1b5d77SViresh Kumar {
807ca1b5d77SViresh Kumar 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
808ca1b5d77SViresh Kumar 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
809ca1b5d77SViresh Kumar 	int i, ret = 0;
810ca1b5d77SViresh Kumar 
811ca1b5d77SViresh Kumar 	if (!required_opp_tables)
812ca1b5d77SViresh Kumar 		return 0;
813ca1b5d77SViresh Kumar 
814ca1b5d77SViresh Kumar 	/* Single genpd case */
81560cdeae0SStephan Gerhold 	if (!genpd_virt_devs)
81660cdeae0SStephan Gerhold 		return _set_required_opp(dev, dev, opp, 0);
817ca1b5d77SViresh Kumar 
818ca1b5d77SViresh Kumar 	/* Multiple genpd case */
819ca1b5d77SViresh Kumar 
820ca1b5d77SViresh Kumar 	/*
821ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
822ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
823ca1b5d77SViresh Kumar 	 */
824ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
825ca1b5d77SViresh Kumar 
8262c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
8272c59138cSStephan Gerhold 	if (up) {
828ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
82960cdeae0SStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
83060cdeae0SStephan Gerhold 			if (ret)
831ca1b5d77SViresh Kumar 				break;
832ca1b5d77SViresh Kumar 		}
8332c59138cSStephan Gerhold 	} else {
8342c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
8352c59138cSStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
8362c59138cSStephan Gerhold 			if (ret)
837ca1b5d77SViresh Kumar 				break;
838ca1b5d77SViresh Kumar 		}
839ca1b5d77SViresh Kumar 	}
8402c59138cSStephan Gerhold 
841ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
842ca1b5d77SViresh Kumar 
843ca1b5d77SViresh Kumar 	return ret;
844ca1b5d77SViresh Kumar }
845ca1b5d77SViresh Kumar 
8467813dd6fSViresh Kumar /**
8473ae1f39aSSibi Sankar  * dev_pm_opp_set_bw() - sets bandwidth levels corresponding to an opp
8483ae1f39aSSibi Sankar  * @dev:	device for which we do this operation
8493ae1f39aSSibi Sankar  * @opp:	opp based on which the bandwidth levels are to be configured
8503ae1f39aSSibi Sankar  *
8513ae1f39aSSibi Sankar  * This configures the bandwidth to the levels specified by the OPP. However
8523ae1f39aSSibi Sankar  * if the OPP specified is NULL the bandwidth levels are cleared out.
8533ae1f39aSSibi Sankar  *
8543ae1f39aSSibi Sankar  * Return: 0 on success or a negative error value.
8553ae1f39aSSibi Sankar  */
8563ae1f39aSSibi Sankar int dev_pm_opp_set_bw(struct device *dev, struct dev_pm_opp *opp)
8573ae1f39aSSibi Sankar {
8583ae1f39aSSibi Sankar 	struct opp_table *opp_table;
8593ae1f39aSSibi Sankar 	int ret;
8603ae1f39aSSibi Sankar 
8613ae1f39aSSibi Sankar 	opp_table = _find_opp_table(dev);
8623ae1f39aSSibi Sankar 	if (IS_ERR(opp_table)) {
8633ae1f39aSSibi Sankar 		dev_err(dev, "%s: device opp table doesn't exist\n", __func__);
8643ae1f39aSSibi Sankar 		return PTR_ERR(opp_table);
8653ae1f39aSSibi Sankar 	}
8663ae1f39aSSibi Sankar 
8673ae1f39aSSibi Sankar 	if (opp)
8683ae1f39aSSibi Sankar 		ret = _set_opp_bw(opp_table, opp, dev, false);
8693ae1f39aSSibi Sankar 	else
8703ae1f39aSSibi Sankar 		ret = _set_opp_bw(opp_table, NULL, dev, true);
8713ae1f39aSSibi Sankar 
8723ae1f39aSSibi Sankar 	dev_pm_opp_put_opp_table(opp_table);
8733ae1f39aSSibi Sankar 	return ret;
8743ae1f39aSSibi Sankar }
8753ae1f39aSSibi Sankar EXPORT_SYMBOL_GPL(dev_pm_opp_set_bw);
8763ae1f39aSSibi Sankar 
877f3364e17SViresh Kumar static int _opp_set_rate_zero(struct device *dev, struct opp_table *opp_table)
878f3364e17SViresh Kumar {
879f3364e17SViresh Kumar 	int ret;
880f3364e17SViresh Kumar 
881f3364e17SViresh Kumar 	if (!opp_table->enabled)
882f3364e17SViresh Kumar 		return 0;
883f3364e17SViresh Kumar 
884f3364e17SViresh Kumar 	/*
885f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
886f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
887f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
888f3364e17SViresh Kumar 	 */
889f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
890f3364e17SViresh Kumar 		return 0;
891f3364e17SViresh Kumar 
892f3364e17SViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev, true);
893f3364e17SViresh Kumar 	if (ret)
894f3364e17SViresh Kumar 		return ret;
895f3364e17SViresh Kumar 
896f3364e17SViresh Kumar 	if (opp_table->regulators)
897f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
898f3364e17SViresh Kumar 
8992c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
900f3364e17SViresh Kumar 
901f3364e17SViresh Kumar 	opp_table->enabled = false;
902f3364e17SViresh Kumar 	return ret;
903f3364e17SViresh Kumar }
904f3364e17SViresh Kumar 
9053ae1f39aSSibi Sankar /**
9067813dd6fSViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
9077813dd6fSViresh Kumar  * @dev:	 device for which we do this operation
9087813dd6fSViresh Kumar  * @target_freq: frequency to achieve
9097813dd6fSViresh Kumar  *
910b3e3759eSStephen Boyd  * This configures the power-supplies to the levels specified by the OPP
911b3e3759eSStephen Boyd  * corresponding to the target_freq, and programs the clock to a value <=
912b3e3759eSStephen Boyd  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
913b3e3759eSStephen Boyd  * provided by the opp, should have already rounded to the target OPP's
914b3e3759eSStephen Boyd  * frequency.
9157813dd6fSViresh Kumar  */
9167813dd6fSViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
9177813dd6fSViresh Kumar {
9187813dd6fSViresh Kumar 	struct opp_table *opp_table;
919b3e3759eSStephen Boyd 	unsigned long freq, old_freq, temp_freq;
9207813dd6fSViresh Kumar 	struct dev_pm_opp *old_opp, *opp;
9217813dd6fSViresh Kumar 	struct clk *clk;
922b00e667aSViresh Kumar 	int ret;
9237813dd6fSViresh Kumar 
9247813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
9257813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
9267813dd6fSViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
9277813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
9287813dd6fSViresh Kumar 	}
9297813dd6fSViresh Kumar 
930cd7ea582SRajendra Nayak 	if (unlikely(!target_freq)) {
931f3364e17SViresh Kumar 		ret = _opp_set_rate_zero(dev, opp_table);
932cd7ea582SRajendra Nayak 		goto put_opp_table;
933cd7ea582SRajendra Nayak 	}
934cd7ea582SRajendra Nayak 
9357813dd6fSViresh Kumar 	clk = opp_table->clk;
9367813dd6fSViresh Kumar 	if (IS_ERR(clk)) {
9377813dd6fSViresh Kumar 		dev_err(dev, "%s: No clock available for the device\n",
9387813dd6fSViresh Kumar 			__func__);
9397813dd6fSViresh Kumar 		ret = PTR_ERR(clk);
9407813dd6fSViresh Kumar 		goto put_opp_table;
9417813dd6fSViresh Kumar 	}
9427813dd6fSViresh Kumar 
9437813dd6fSViresh Kumar 	freq = clk_round_rate(clk, target_freq);
9447813dd6fSViresh Kumar 	if ((long)freq <= 0)
9457813dd6fSViresh Kumar 		freq = target_freq;
9467813dd6fSViresh Kumar 
9477813dd6fSViresh Kumar 	old_freq = clk_get_rate(clk);
9487813dd6fSViresh Kumar 
9497813dd6fSViresh Kumar 	/* Return early if nothing to do */
95010b21736SViresh Kumar 	if (opp_table->enabled && old_freq == freq) {
9517813dd6fSViresh Kumar 		dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
9527813dd6fSViresh Kumar 			__func__, freq);
9537813dd6fSViresh Kumar 		ret = 0;
9547813dd6fSViresh Kumar 		goto put_opp_table;
9557813dd6fSViresh Kumar 	}
9567813dd6fSViresh Kumar 
957aca48b61SRajendra Nayak 	/*
958aca48b61SRajendra Nayak 	 * For IO devices which require an OPP on some platforms/SoCs
959aca48b61SRajendra Nayak 	 * while just needing to scale the clock on some others
960aca48b61SRajendra Nayak 	 * we look for empty OPP tables with just a clock handle and
961aca48b61SRajendra Nayak 	 * scale only the clk. This makes dev_pm_opp_set_rate()
962aca48b61SRajendra Nayak 	 * equivalent to a clk_set_rate()
963aca48b61SRajendra Nayak 	 */
964aca48b61SRajendra Nayak 	if (!_get_opp_count(opp_table)) {
965aca48b61SRajendra Nayak 		ret = _generic_set_opp_clk_only(dev, clk, freq);
966aca48b61SRajendra Nayak 		goto put_opp_table;
967aca48b61SRajendra Nayak 	}
968aca48b61SRajendra Nayak 
969b3e3759eSStephen Boyd 	temp_freq = old_freq;
970b3e3759eSStephen Boyd 	old_opp = _find_freq_ceil(opp_table, &temp_freq);
9717813dd6fSViresh Kumar 	if (IS_ERR(old_opp)) {
9727813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
9737813dd6fSViresh Kumar 			__func__, old_freq, PTR_ERR(old_opp));
9747813dd6fSViresh Kumar 	}
9757813dd6fSViresh Kumar 
976b3e3759eSStephen Boyd 	temp_freq = freq;
977b3e3759eSStephen Boyd 	opp = _find_freq_ceil(opp_table, &temp_freq);
9787813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
9797813dd6fSViresh Kumar 		ret = PTR_ERR(opp);
9807813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
9817813dd6fSViresh Kumar 			__func__, freq, ret);
9827813dd6fSViresh Kumar 		goto put_old_opp;
9837813dd6fSViresh Kumar 	}
9847813dd6fSViresh Kumar 
9857813dd6fSViresh Kumar 	dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
9867813dd6fSViresh Kumar 		old_freq, freq);
9877813dd6fSViresh Kumar 
988ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
989faef080fSViresh Kumar 	if (freq >= old_freq) {
9902c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
991ca1b5d77SViresh Kumar 		if (ret)
992ca1b5d77SViresh Kumar 			goto put_opp;
993ca1b5d77SViresh Kumar 	}
994ca1b5d77SViresh Kumar 
9957e535993SViresh Kumar 	if (opp_table->set_opp) {
9967e535993SViresh Kumar 		ret = _set_opp_custom(opp_table, dev, old_freq, freq,
9977e535993SViresh Kumar 				      IS_ERR(old_opp) ? NULL : old_opp->supplies,
9987e535993SViresh Kumar 				      opp->supplies);
9997e535993SViresh Kumar 	} else if (opp_table->regulators) {
10007813dd6fSViresh Kumar 		ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
10017813dd6fSViresh Kumar 						 IS_ERR(old_opp) ? NULL : old_opp->supplies,
10027813dd6fSViresh Kumar 						 opp->supplies);
10037813dd6fSViresh Kumar 	} else {
10047813dd6fSViresh Kumar 		/* Only frequency scaling */
1005285881b5SViresh Kumar 		ret = _generic_set_opp_clk_only(dev, clk, freq);
10067813dd6fSViresh Kumar 	}
10077813dd6fSViresh Kumar 
1008ca1b5d77SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1009ca1b5d77SViresh Kumar 	if (!ret && freq < old_freq) {
10102c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, false);
1011ca1b5d77SViresh Kumar 		if (ret)
1012ca1b5d77SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1013ca1b5d77SViresh Kumar 	}
1014ca1b5d77SViresh Kumar 
101572f80ce4SViresh Kumar 	if (!ret) {
1016b00e667aSViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev, false);
101772f80ce4SViresh Kumar 		if (!ret)
101872f80ce4SViresh Kumar 			opp_table->enabled = true;
101972f80ce4SViresh Kumar 	}
1020fe2af402SGeorgi Djakov 
1021ca1b5d77SViresh Kumar put_opp:
10227813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
10237813dd6fSViresh Kumar put_old_opp:
10247813dd6fSViresh Kumar 	if (!IS_ERR(old_opp))
10257813dd6fSViresh Kumar 		dev_pm_opp_put(old_opp);
10267813dd6fSViresh Kumar put_opp_table:
10277813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
10287813dd6fSViresh Kumar 	return ret;
10297813dd6fSViresh Kumar }
10307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
10317813dd6fSViresh Kumar 
10327813dd6fSViresh Kumar /* OPP-dev Helpers */
10337813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
10347813dd6fSViresh Kumar 			    struct opp_table *opp_table)
10357813dd6fSViresh Kumar {
10367813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
10377813dd6fSViresh Kumar 	list_del(&opp_dev->node);
10387813dd6fSViresh Kumar 	kfree(opp_dev);
10397813dd6fSViresh Kumar }
10407813dd6fSViresh Kumar 
1041ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
10427813dd6fSViresh Kumar 				struct opp_table *opp_table)
10437813dd6fSViresh Kumar {
10447813dd6fSViresh Kumar 	struct opp_device *opp_dev;
10457813dd6fSViresh Kumar 
10467813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
10477813dd6fSViresh Kumar 	if (!opp_dev)
10487813dd6fSViresh Kumar 		return NULL;
10497813dd6fSViresh Kumar 
10507813dd6fSViresh Kumar 	/* Initialize opp-dev */
10517813dd6fSViresh Kumar 	opp_dev->dev = dev;
10523d255699SViresh Kumar 
1053ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
10547813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1055ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
10567813dd6fSViresh Kumar 
10577813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1058a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1059283d55e6SViresh Kumar 
1060283d55e6SViresh Kumar 	return opp_dev;
1061283d55e6SViresh Kumar }
1062283d55e6SViresh Kumar 
1063eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
10647813dd6fSViresh Kumar {
10657813dd6fSViresh Kumar 	struct opp_table *opp_table;
10667813dd6fSViresh Kumar 	struct opp_device *opp_dev;
10677813dd6fSViresh Kumar 	int ret;
10687813dd6fSViresh Kumar 
10697813dd6fSViresh Kumar 	/*
10707813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
10717813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
10727813dd6fSViresh Kumar 	 */
10737813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
10747813dd6fSViresh Kumar 	if (!opp_table)
1075dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
10767813dd6fSViresh Kumar 
10773d255699SViresh Kumar 	mutex_init(&opp_table->lock);
10784f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
10797813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
10807813dd6fSViresh Kumar 
108146f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
108246f48acaSViresh Kumar 	opp_table->regulator_count = -1;
108346f48acaSViresh Kumar 
10847813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
10857813dd6fSViresh Kumar 	if (!opp_dev) {
1086dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1087dd461cd9SStephan Gerhold 		goto err;
10887813dd6fSViresh Kumar 	}
10897813dd6fSViresh Kumar 
1090eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
10917813dd6fSViresh Kumar 
10927813dd6fSViresh Kumar 	/* Find clk for the device */
10937813dd6fSViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
10947813dd6fSViresh Kumar 	if (IS_ERR(opp_table->clk)) {
10957813dd6fSViresh Kumar 		ret = PTR_ERR(opp_table->clk);
1096dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
1097dd461cd9SStephan Gerhold 			goto err;
1098dd461cd9SStephan Gerhold 
1099dd461cd9SStephan Gerhold 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
11007813dd6fSViresh Kumar 	}
11017813dd6fSViresh Kumar 
11026d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
11036d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1104dd461cd9SStephan Gerhold 	if (ret) {
1105dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
1106dd461cd9SStephan Gerhold 			goto err;
1107dd461cd9SStephan Gerhold 
11086d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
11096d3f922cSGeorgi Djakov 			 __func__, ret);
1110dd461cd9SStephan Gerhold 	}
11116d3f922cSGeorgi Djakov 
11127813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
11137813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
11147813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
11157813dd6fSViresh Kumar 
11167813dd6fSViresh Kumar 	return opp_table;
1117dd461cd9SStephan Gerhold 
1118dd461cd9SStephan Gerhold err:
1119dd461cd9SStephan Gerhold 	kfree(opp_table);
1120dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
11217813dd6fSViresh Kumar }
11227813dd6fSViresh Kumar 
11237813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
11247813dd6fSViresh Kumar {
11257813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
11267813dd6fSViresh Kumar }
11277813dd6fSViresh Kumar 
1128*27c09484SViresh Kumar /*
1129*27c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
1130*27c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
1131*27c09484SViresh Kumar  *
1132*27c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
1133*27c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
1134*27c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
1135*27c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
1136*27c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
1137*27c09484SViresh Kumar  * indirect users of OPP core.
1138*27c09484SViresh Kumar  *
1139*27c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
1140*27c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
1141*27c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
1142*27c09484SViresh Kumar  */
1143eb7c8743SViresh Kumar static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
11447813dd6fSViresh Kumar {
11457813dd6fSViresh Kumar 	struct opp_table *opp_table;
11467813dd6fSViresh Kumar 
1147*27c09484SViresh Kumar again:
11487813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
11497813dd6fSViresh Kumar 
11507813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
11517813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
11527813dd6fSViresh Kumar 		goto unlock;
11537813dd6fSViresh Kumar 
1154*27c09484SViresh Kumar 	/*
1155*27c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
1156*27c09484SViresh Kumar 	 * another user, wait for it to finish.
1157*27c09484SViresh Kumar 	 */
1158*27c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
1159*27c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
1160*27c09484SViresh Kumar 		cpu_relax();
1161*27c09484SViresh Kumar 		goto again;
1162*27c09484SViresh Kumar 	}
1163*27c09484SViresh Kumar 
1164*27c09484SViresh Kumar 	opp_tables_busy = true;
1165283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
1166*27c09484SViresh Kumar 
1167*27c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
1168*27c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
1169*27c09484SViresh Kumar 
1170283d55e6SViresh Kumar 	if (opp_table) {
1171ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1172283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1173dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1174283d55e6SViresh Kumar 		}
1175*27c09484SViresh Kumar 
1176*27c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
1177*27c09484SViresh Kumar 	} else {
1178*27c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
1179*27c09484SViresh Kumar 
1180*27c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
1181*27c09484SViresh Kumar 		if (!IS_ERR(opp_table))
1182*27c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1183283d55e6SViresh Kumar 	}
1184283d55e6SViresh Kumar 
1185*27c09484SViresh Kumar 	opp_tables_busy = false;
11867813dd6fSViresh Kumar 
11877813dd6fSViresh Kumar unlock:
11887813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
11897813dd6fSViresh Kumar 
11907813dd6fSViresh Kumar 	return opp_table;
11917813dd6fSViresh Kumar }
1192eb7c8743SViresh Kumar 
1193eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1194eb7c8743SViresh Kumar {
1195eb7c8743SViresh Kumar 	return _opp_get_opp_table(dev, 0);
1196eb7c8743SViresh Kumar }
11977813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
11987813dd6fSViresh Kumar 
1199eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
1200eb7c8743SViresh Kumar 						   int index)
1201eb7c8743SViresh Kumar {
1202eb7c8743SViresh Kumar 	return _opp_get_opp_table(dev, index);
1203eb7c8743SViresh Kumar }
1204eb7c8743SViresh Kumar 
12057813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
12067813dd6fSViresh Kumar {
12077813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1208cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
12096d3f922cSGeorgi Djakov 	int i;
12107813dd6fSViresh Kumar 
1211e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1212e0df59deSViresh Kumar 	list_del(&opp_table->node);
1213e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1214e0df59deSViresh Kumar 
12155d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
12165d6d106fSViresh Kumar 
12177813dd6fSViresh Kumar 	/* Release clk */
12187813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
12197813dd6fSViresh Kumar 		clk_put(opp_table->clk);
12207813dd6fSViresh Kumar 
12216d3f922cSGeorgi Djakov 	if (opp_table->paths) {
12226d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
12236d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
12246d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
12256d3f922cSGeorgi Djakov 	}
12266d3f922cSGeorgi Djakov 
1227cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1228cdd6ed90SViresh Kumar 
1229cdd6ed90SViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
12303d255699SViresh Kumar 		/*
1231cdd6ed90SViresh Kumar 		 * The OPP table is getting removed, drop the performance state
1232cdd6ed90SViresh Kumar 		 * constraints.
12333d255699SViresh Kumar 		 */
1234cdd6ed90SViresh Kumar 		if (opp_table->genpd_performance_state)
1235cdd6ed90SViresh Kumar 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
12367813dd6fSViresh Kumar 
12377813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
1238cdd6ed90SViresh Kumar 	}
12397813dd6fSViresh Kumar 
12404f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
12417813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
12427813dd6fSViresh Kumar 	kfree(opp_table);
12437813dd6fSViresh Kumar }
12447813dd6fSViresh Kumar 
12457813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
12467813dd6fSViresh Kumar {
12477813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
12487813dd6fSViresh Kumar 		       &opp_table_lock);
12497813dd6fSViresh Kumar }
12507813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
12517813dd6fSViresh Kumar 
12527813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
12537813dd6fSViresh Kumar {
12547813dd6fSViresh Kumar 	kfree(opp);
12557813dd6fSViresh Kumar }
12567813dd6fSViresh Kumar 
12571690d8bbSViresh Kumar static void _opp_kref_release(struct dev_pm_opp *opp,
12581690d8bbSViresh Kumar 			      struct opp_table *opp_table)
12597813dd6fSViresh Kumar {
12607813dd6fSViresh Kumar 	/*
12617813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
12627813dd6fSViresh Kumar 	 * frequency/voltage list.
12637813dd6fSViresh Kumar 	 */
12647813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1265da544b61SViresh Kumar 	_of_opp_free_required_opps(opp_table, opp);
12667813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
12677813dd6fSViresh Kumar 	list_del(&opp->node);
12687813dd6fSViresh Kumar 	kfree(opp);
12691690d8bbSViresh Kumar }
12707813dd6fSViresh Kumar 
12711690d8bbSViresh Kumar static void _opp_kref_release_unlocked(struct kref *kref)
12721690d8bbSViresh Kumar {
12731690d8bbSViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
12741690d8bbSViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
12751690d8bbSViresh Kumar 
12761690d8bbSViresh Kumar 	_opp_kref_release(opp, opp_table);
12771690d8bbSViresh Kumar }
12781690d8bbSViresh Kumar 
12791690d8bbSViresh Kumar static void _opp_kref_release_locked(struct kref *kref)
12801690d8bbSViresh Kumar {
12811690d8bbSViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
12821690d8bbSViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
12831690d8bbSViresh Kumar 
12841690d8bbSViresh Kumar 	_opp_kref_release(opp, opp_table);
12857813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
12867813dd6fSViresh Kumar }
12877813dd6fSViresh Kumar 
1288a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
12897813dd6fSViresh Kumar {
12907813dd6fSViresh Kumar 	kref_get(&opp->kref);
12917813dd6fSViresh Kumar }
12927813dd6fSViresh Kumar 
12937813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
12947813dd6fSViresh Kumar {
12951690d8bbSViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release_locked,
12961690d8bbSViresh Kumar 		       &opp->opp_table->lock);
12977813dd6fSViresh Kumar }
12987813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
12997813dd6fSViresh Kumar 
13001690d8bbSViresh Kumar static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp)
13011690d8bbSViresh Kumar {
13021690d8bbSViresh Kumar 	kref_put(&opp->kref, _opp_kref_release_unlocked);
13031690d8bbSViresh Kumar }
13041690d8bbSViresh Kumar 
13057813dd6fSViresh Kumar /**
13067813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
13077813dd6fSViresh Kumar  * @dev:	device for which we do this operation
13087813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
13097813dd6fSViresh Kumar  *
13107813dd6fSViresh Kumar  * This function removes an opp from the opp table.
13117813dd6fSViresh Kumar  */
13127813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
13137813dd6fSViresh Kumar {
13147813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
13157813dd6fSViresh Kumar 	struct opp_table *opp_table;
13167813dd6fSViresh Kumar 	bool found = false;
13177813dd6fSViresh Kumar 
13187813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
13197813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
13207813dd6fSViresh Kumar 		return;
13217813dd6fSViresh Kumar 
13227813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
13237813dd6fSViresh Kumar 
13247813dd6fSViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
13257813dd6fSViresh Kumar 		if (opp->rate == freq) {
13267813dd6fSViresh Kumar 			found = true;
13277813dd6fSViresh Kumar 			break;
13287813dd6fSViresh Kumar 		}
13297813dd6fSViresh Kumar 	}
13307813dd6fSViresh Kumar 
13317813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
13327813dd6fSViresh Kumar 
13337813dd6fSViresh Kumar 	if (found) {
13347813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
13350ad8c623SViresh Kumar 
13360ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
13370ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
13387813dd6fSViresh Kumar 	} else {
13397813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
13407813dd6fSViresh Kumar 			 __func__, freq);
13417813dd6fSViresh Kumar 	}
13427813dd6fSViresh Kumar 
13430ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
13447813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
13457813dd6fSViresh Kumar }
13467813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
13477813dd6fSViresh Kumar 
1348922ff075SViresh Kumar bool _opp_remove_all_static(struct opp_table *opp_table)
134903758d60SViresh Kumar {
135003758d60SViresh Kumar 	struct dev_pm_opp *opp, *tmp;
1351922ff075SViresh Kumar 	bool ret = true;
135203758d60SViresh Kumar 
135303758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
135403758d60SViresh Kumar 
1355922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1356922ff075SViresh Kumar 		ret = false;
1357922ff075SViresh Kumar 		goto unlock;
1358922ff075SViresh Kumar 	}
1359922ff075SViresh Kumar 
1360922ff075SViresh Kumar 	if (--opp_table->parsed_static_opps)
136103758d60SViresh Kumar 		goto unlock;
136203758d60SViresh Kumar 
136303758d60SViresh Kumar 	list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
136403758d60SViresh Kumar 		if (!opp->dynamic)
136503758d60SViresh Kumar 			dev_pm_opp_put_unlocked(opp);
136603758d60SViresh Kumar 	}
136703758d60SViresh Kumar 
136803758d60SViresh Kumar unlock:
136903758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1370922ff075SViresh Kumar 
1371922ff075SViresh Kumar 	return ret;
137203758d60SViresh Kumar }
137303758d60SViresh Kumar 
13741690d8bbSViresh Kumar /**
13751690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
13761690d8bbSViresh Kumar  * @dev:	device for which we do this operation
13771690d8bbSViresh Kumar  *
13781690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
13791690d8bbSViresh Kumar  */
13801690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
13811690d8bbSViresh Kumar {
13821690d8bbSViresh Kumar 	struct opp_table *opp_table;
13831690d8bbSViresh Kumar 	struct dev_pm_opp *opp, *temp;
13841690d8bbSViresh Kumar 	int count = 0;
13851690d8bbSViresh Kumar 
13861690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
13871690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
13881690d8bbSViresh Kumar 		return;
13891690d8bbSViresh Kumar 
13901690d8bbSViresh Kumar 	mutex_lock(&opp_table->lock);
13911690d8bbSViresh Kumar 	list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) {
13921690d8bbSViresh Kumar 		if (opp->dynamic) {
13931690d8bbSViresh Kumar 			dev_pm_opp_put_unlocked(opp);
13941690d8bbSViresh Kumar 			count++;
13951690d8bbSViresh Kumar 		}
13961690d8bbSViresh Kumar 	}
13971690d8bbSViresh Kumar 	mutex_unlock(&opp_table->lock);
13981690d8bbSViresh Kumar 
13991690d8bbSViresh Kumar 	/* Drop the references taken by dev_pm_opp_add() */
14001690d8bbSViresh Kumar 	while (count--)
14011690d8bbSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
14021690d8bbSViresh Kumar 
14031690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
14041690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
14051690d8bbSViresh Kumar }
14061690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
14071690d8bbSViresh Kumar 
14087813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table)
14097813dd6fSViresh Kumar {
14107813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
14116d3f922cSGeorgi Djakov 	int supply_count, supply_size, icc_size;
14127813dd6fSViresh Kumar 
14137813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
14146d3f922cSGeorgi Djakov 	supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
14156d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
14166d3f922cSGeorgi Djakov 	icc_size = sizeof(*opp->bandwidth) * table->path_count;
14177813dd6fSViresh Kumar 
14187813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
14196d3f922cSGeorgi Djakov 	opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
14206d3f922cSGeorgi Djakov 
14217813dd6fSViresh Kumar 	if (!opp)
14227813dd6fSViresh Kumar 		return NULL;
14237813dd6fSViresh Kumar 
14247813dd6fSViresh Kumar 	/* Put the supplies at the end of the OPP structure as an empty array */
14257813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
14266d3f922cSGeorgi Djakov 	if (icc_size)
14276d3f922cSGeorgi Djakov 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
14287813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
14297813dd6fSViresh Kumar 
14307813dd6fSViresh Kumar 	return opp;
14317813dd6fSViresh Kumar }
14327813dd6fSViresh Kumar 
14337813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
14347813dd6fSViresh Kumar 					 struct opp_table *opp_table)
14357813dd6fSViresh Kumar {
14367813dd6fSViresh Kumar 	struct regulator *reg;
14377813dd6fSViresh Kumar 	int i;
14387813dd6fSViresh Kumar 
143990e3577bSViresh Kumar 	if (!opp_table->regulators)
144090e3577bSViresh Kumar 		return true;
144190e3577bSViresh Kumar 
14427813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
14437813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
14447813dd6fSViresh Kumar 
14457813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
14467813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
14477813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
14487813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
14497813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
14507813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
14517813dd6fSViresh Kumar 			return false;
14527813dd6fSViresh Kumar 		}
14537813dd6fSViresh Kumar 	}
14547813dd6fSViresh Kumar 
14557813dd6fSViresh Kumar 	return true;
14567813dd6fSViresh Kumar }
14577813dd6fSViresh Kumar 
14586c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
14596c591eecSSaravana Kannan {
14606c591eecSSaravana Kannan 	if (opp1->rate != opp2->rate)
14616c591eecSSaravana Kannan 		return opp1->rate < opp2->rate ? -1 : 1;
14626d3f922cSGeorgi Djakov 	if (opp1->bandwidth && opp2->bandwidth &&
14636d3f922cSGeorgi Djakov 	    opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
14646d3f922cSGeorgi Djakov 		return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
14656c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
14666c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
14676c591eecSSaravana Kannan 	return 0;
14686c591eecSSaravana Kannan }
14696c591eecSSaravana Kannan 
1470a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1471a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1472a1e8c136SViresh Kumar 			     struct list_head **head)
1473a1e8c136SViresh Kumar {
1474a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
14756c591eecSSaravana Kannan 	int opp_cmp;
1476a1e8c136SViresh Kumar 
1477a1e8c136SViresh Kumar 	/*
1478a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1479a1e8c136SViresh Kumar 	 * already present.
1480a1e8c136SViresh Kumar 	 *
1481a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1482a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1483a1e8c136SViresh Kumar 	 * loop.
1484a1e8c136SViresh Kumar 	 */
1485a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
14866c591eecSSaravana Kannan 		opp_cmp = _opp_compare_key(new_opp, opp);
14876c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1488a1e8c136SViresh Kumar 			*head = &opp->node;
1489a1e8c136SViresh Kumar 			continue;
1490a1e8c136SViresh Kumar 		}
1491a1e8c136SViresh Kumar 
14926c591eecSSaravana Kannan 		if (opp_cmp < 0)
1493a1e8c136SViresh Kumar 			return 0;
1494a1e8c136SViresh Kumar 
1495a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1496a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1497a1e8c136SViresh Kumar 			 __func__, opp->rate, opp->supplies[0].u_volt,
1498a1e8c136SViresh Kumar 			 opp->available, new_opp->rate,
1499a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1500a1e8c136SViresh Kumar 
1501a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1502a1e8c136SViresh Kumar 		return opp->available &&
1503a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1504a1e8c136SViresh Kumar 	}
1505a1e8c136SViresh Kumar 
1506a1e8c136SViresh Kumar 	return 0;
1507a1e8c136SViresh Kumar }
1508a1e8c136SViresh Kumar 
15097813dd6fSViresh Kumar /*
15107813dd6fSViresh Kumar  * Returns:
15117813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
15127813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
15137813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
15147813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
15157813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
15167813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
15177813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
15187813dd6fSViresh Kumar  */
15197813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1520a1e8c136SViresh Kumar 	     struct opp_table *opp_table, bool rate_not_available)
15217813dd6fSViresh Kumar {
15227813dd6fSViresh Kumar 	struct list_head *head;
15237813dd6fSViresh Kumar 	int ret;
15247813dd6fSViresh Kumar 
15257813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
15267813dd6fSViresh Kumar 	head = &opp_table->opp_list;
15277813dd6fSViresh Kumar 
1528a1e8c136SViresh Kumar 	if (likely(!rate_not_available)) {
1529a1e8c136SViresh Kumar 		ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1530a1e8c136SViresh Kumar 		if (ret) {
15317813dd6fSViresh Kumar 			mutex_unlock(&opp_table->lock);
15327813dd6fSViresh Kumar 			return ret;
15337813dd6fSViresh Kumar 		}
1534a1e8c136SViresh Kumar 	}
15357813dd6fSViresh Kumar 
15367813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
15377813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
15387813dd6fSViresh Kumar 
15397813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
15407813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
15417813dd6fSViresh Kumar 
1542a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
15437813dd6fSViresh Kumar 
15447813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
15457813dd6fSViresh Kumar 		new_opp->available = false;
15467813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
15477813dd6fSViresh Kumar 			 __func__, new_opp->rate);
15487813dd6fSViresh Kumar 	}
15497813dd6fSViresh Kumar 
15507813dd6fSViresh Kumar 	return 0;
15517813dd6fSViresh Kumar }
15527813dd6fSViresh Kumar 
15537813dd6fSViresh Kumar /**
15547813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
15557813dd6fSViresh Kumar  * @opp_table:	OPP table
15567813dd6fSViresh Kumar  * @dev:	device for which we do this operation
15577813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
15587813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
15597813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
15607813dd6fSViresh Kumar  *
15617813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
15627813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
15637813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
15647813dd6fSViresh Kumar  *
15657813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
15667813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
15677813dd6fSViresh Kumar  *
15687813dd6fSViresh Kumar  * Return:
15697813dd6fSViresh Kumar  * 0		On success OR
15707813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
15717813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
15727813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
15737813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
15747813dd6fSViresh Kumar  */
15757813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
15767813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
15777813dd6fSViresh Kumar {
15787813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
15797813dd6fSViresh Kumar 	unsigned long tol;
15807813dd6fSViresh Kumar 	int ret;
15817813dd6fSViresh Kumar 
15827813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
15837813dd6fSViresh Kumar 	if (!new_opp)
15847813dd6fSViresh Kumar 		return -ENOMEM;
15857813dd6fSViresh Kumar 
15867813dd6fSViresh Kumar 	/* populate the opp table */
15877813dd6fSViresh Kumar 	new_opp->rate = freq;
15887813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
15897813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
15907813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
15917813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
15927813dd6fSViresh Kumar 	new_opp->available = true;
15937813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
15947813dd6fSViresh Kumar 
1595a1e8c136SViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table, false);
15967813dd6fSViresh Kumar 	if (ret) {
15977813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
15987813dd6fSViresh Kumar 		if (ret == -EBUSY)
15997813dd6fSViresh Kumar 			ret = 0;
16007813dd6fSViresh Kumar 		goto free_opp;
16017813dd6fSViresh Kumar 	}
16027813dd6fSViresh Kumar 
16037813dd6fSViresh Kumar 	/*
16047813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
16057813dd6fSViresh Kumar 	 * frequency/voltage list.
16067813dd6fSViresh Kumar 	 */
16077813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
16087813dd6fSViresh Kumar 	return 0;
16097813dd6fSViresh Kumar 
16107813dd6fSViresh Kumar free_opp:
16117813dd6fSViresh Kumar 	_opp_free(new_opp);
16127813dd6fSViresh Kumar 
16137813dd6fSViresh Kumar 	return ret;
16147813dd6fSViresh Kumar }
16157813dd6fSViresh Kumar 
16167813dd6fSViresh Kumar /**
16177813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw() - Set supported platforms
16187813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
16197813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
16207813dd6fSViresh Kumar  * @count: Number of elements in the array.
16217813dd6fSViresh Kumar  *
16227813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
16237813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
16247813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
16257813dd6fSViresh Kumar  * property.
16267813dd6fSViresh Kumar  */
16277813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
16287813dd6fSViresh Kumar 			const u32 *versions, unsigned int count)
16297813dd6fSViresh Kumar {
16307813dd6fSViresh Kumar 	struct opp_table *opp_table;
16317813dd6fSViresh Kumar 
16327813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
1633dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1634dd461cd9SStephan Gerhold 		return opp_table;
16357813dd6fSViresh Kumar 
16367813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
16377813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
16387813dd6fSViresh Kumar 
163925419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
164025419de1SViresh Kumar 	if (opp_table->supported_hw)
164125419de1SViresh Kumar 		return opp_table;
16427813dd6fSViresh Kumar 
16437813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
16447813dd6fSViresh Kumar 					GFP_KERNEL);
16457813dd6fSViresh Kumar 	if (!opp_table->supported_hw) {
164625419de1SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
164725419de1SViresh Kumar 		return ERR_PTR(-ENOMEM);
16487813dd6fSViresh Kumar 	}
16497813dd6fSViresh Kumar 
16507813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
16517813dd6fSViresh Kumar 
16527813dd6fSViresh Kumar 	return opp_table;
16537813dd6fSViresh Kumar }
16547813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
16557813dd6fSViresh Kumar 
16567813dd6fSViresh Kumar /**
16577813dd6fSViresh Kumar  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
16587813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
16597813dd6fSViresh Kumar  *
16607813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
16617813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
16627813dd6fSViresh Kumar  * will not be freed.
16637813dd6fSViresh Kumar  */
16647813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
16657813dd6fSViresh Kumar {
16667813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
16677813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
16687813dd6fSViresh Kumar 
16697813dd6fSViresh Kumar 	kfree(opp_table->supported_hw);
16707813dd6fSViresh Kumar 	opp_table->supported_hw = NULL;
16717813dd6fSViresh Kumar 	opp_table->supported_hw_count = 0;
16727813dd6fSViresh Kumar 
16737813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
16747813dd6fSViresh Kumar }
16757813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
16767813dd6fSViresh Kumar 
16777813dd6fSViresh Kumar /**
16787813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name() - Set prop-extn name
16797813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
16807813dd6fSViresh Kumar  * @name: name to postfix to properties.
16817813dd6fSViresh Kumar  *
16827813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
16837813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
16847813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
16857813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
16867813dd6fSViresh Kumar  */
16877813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
16887813dd6fSViresh Kumar {
16897813dd6fSViresh Kumar 	struct opp_table *opp_table;
16907813dd6fSViresh Kumar 
16917813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
1692dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1693dd461cd9SStephan Gerhold 		return opp_table;
16947813dd6fSViresh Kumar 
16957813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
16967813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
16977813dd6fSViresh Kumar 
1698878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
1699878ec1a9SViresh Kumar 	if (opp_table->prop_name)
1700878ec1a9SViresh Kumar 		return opp_table;
17017813dd6fSViresh Kumar 
17027813dd6fSViresh Kumar 	opp_table->prop_name = kstrdup(name, GFP_KERNEL);
17037813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
1704878ec1a9SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
1705878ec1a9SViresh Kumar 		return ERR_PTR(-ENOMEM);
17067813dd6fSViresh Kumar 	}
17077813dd6fSViresh Kumar 
17087813dd6fSViresh Kumar 	return opp_table;
17097813dd6fSViresh Kumar }
17107813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
17117813dd6fSViresh Kumar 
17127813dd6fSViresh Kumar /**
17137813dd6fSViresh Kumar  * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
17147813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
17157813dd6fSViresh Kumar  *
17167813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
17177813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
17187813dd6fSViresh Kumar  * will not be freed.
17197813dd6fSViresh Kumar  */
17207813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
17217813dd6fSViresh Kumar {
17227813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
17237813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
17247813dd6fSViresh Kumar 
17257813dd6fSViresh Kumar 	kfree(opp_table->prop_name);
17267813dd6fSViresh Kumar 	opp_table->prop_name = NULL;
17277813dd6fSViresh Kumar 
17287813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17297813dd6fSViresh Kumar }
17307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
17317813dd6fSViresh Kumar 
17327813dd6fSViresh Kumar static int _allocate_set_opp_data(struct opp_table *opp_table)
17337813dd6fSViresh Kumar {
17347813dd6fSViresh Kumar 	struct dev_pm_set_opp_data *data;
17357813dd6fSViresh Kumar 	int len, count = opp_table->regulator_count;
17367813dd6fSViresh Kumar 
173790e3577bSViresh Kumar 	if (WARN_ON(!opp_table->regulators))
17387813dd6fSViresh Kumar 		return -EINVAL;
17397813dd6fSViresh Kumar 
17407813dd6fSViresh Kumar 	/* space for set_opp_data */
17417813dd6fSViresh Kumar 	len = sizeof(*data);
17427813dd6fSViresh Kumar 
17437813dd6fSViresh Kumar 	/* space for old_opp.supplies and new_opp.supplies */
17447813dd6fSViresh Kumar 	len += 2 * sizeof(struct dev_pm_opp_supply) * count;
17457813dd6fSViresh Kumar 
17467813dd6fSViresh Kumar 	data = kzalloc(len, GFP_KERNEL);
17477813dd6fSViresh Kumar 	if (!data)
17487813dd6fSViresh Kumar 		return -ENOMEM;
17497813dd6fSViresh Kumar 
17507813dd6fSViresh Kumar 	data->old_opp.supplies = (void *)(data + 1);
17517813dd6fSViresh Kumar 	data->new_opp.supplies = data->old_opp.supplies + count;
17527813dd6fSViresh Kumar 
17537813dd6fSViresh Kumar 	opp_table->set_opp_data = data;
17547813dd6fSViresh Kumar 
17557813dd6fSViresh Kumar 	return 0;
17567813dd6fSViresh Kumar }
17577813dd6fSViresh Kumar 
17587813dd6fSViresh Kumar static void _free_set_opp_data(struct opp_table *opp_table)
17597813dd6fSViresh Kumar {
17607813dd6fSViresh Kumar 	kfree(opp_table->set_opp_data);
17617813dd6fSViresh Kumar 	opp_table->set_opp_data = NULL;
17627813dd6fSViresh Kumar }
17637813dd6fSViresh Kumar 
17647813dd6fSViresh Kumar /**
17657813dd6fSViresh Kumar  * dev_pm_opp_set_regulators() - Set regulator names for the device
17667813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
17677813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
17687813dd6fSViresh Kumar  * @count: Number of regulators.
17697813dd6fSViresh Kumar  *
17707813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
17717813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
17727813dd6fSViresh Kumar  * well.
17737813dd6fSViresh Kumar  *
17747813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
17757813dd6fSViresh Kumar  */
17767813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
17777813dd6fSViresh Kumar 					    const char * const names[],
17787813dd6fSViresh Kumar 					    unsigned int count)
17797813dd6fSViresh Kumar {
17807813dd6fSViresh Kumar 	struct opp_table *opp_table;
17817813dd6fSViresh Kumar 	struct regulator *reg;
17827813dd6fSViresh Kumar 	int ret, i;
17837813dd6fSViresh Kumar 
17847813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
1785dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1786dd461cd9SStephan Gerhold 		return opp_table;
17877813dd6fSViresh Kumar 
17887813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
17897813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
17907813dd6fSViresh Kumar 		ret = -EBUSY;
17917813dd6fSViresh Kumar 		goto err;
17927813dd6fSViresh Kumar 	}
17937813dd6fSViresh Kumar 
1794779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
1795779b783cSViresh Kumar 	if (opp_table->regulators)
1796779b783cSViresh Kumar 		return opp_table;
17977813dd6fSViresh Kumar 
17987813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
17997813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
18007813dd6fSViresh Kumar 					      GFP_KERNEL);
18017813dd6fSViresh Kumar 	if (!opp_table->regulators) {
18027813dd6fSViresh Kumar 		ret = -ENOMEM;
18037813dd6fSViresh Kumar 		goto err;
18047813dd6fSViresh Kumar 	}
18057813dd6fSViresh Kumar 
18067813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
18077813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
18087813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
18097813dd6fSViresh Kumar 			ret = PTR_ERR(reg);
18107813dd6fSViresh Kumar 			if (ret != -EPROBE_DEFER)
18117813dd6fSViresh Kumar 				dev_err(dev, "%s: no regulator (%s) found: %d\n",
18127813dd6fSViresh Kumar 					__func__, names[i], ret);
18137813dd6fSViresh Kumar 			goto free_regulators;
18147813dd6fSViresh Kumar 		}
18157813dd6fSViresh Kumar 
18167813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
18177813dd6fSViresh Kumar 	}
18187813dd6fSViresh Kumar 
18197813dd6fSViresh Kumar 	opp_table->regulator_count = count;
18207813dd6fSViresh Kumar 
18217813dd6fSViresh Kumar 	/* Allocate block only once to pass to set_opp() routines */
18227813dd6fSViresh Kumar 	ret = _allocate_set_opp_data(opp_table);
18237813dd6fSViresh Kumar 	if (ret)
18247813dd6fSViresh Kumar 		goto free_regulators;
18257813dd6fSViresh Kumar 
18267813dd6fSViresh Kumar 	return opp_table;
18277813dd6fSViresh Kumar 
18287813dd6fSViresh Kumar free_regulators:
182924957db1SMarek Szyprowski 	while (i != 0)
183024957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
18317813dd6fSViresh Kumar 
18327813dd6fSViresh Kumar 	kfree(opp_table->regulators);
18337813dd6fSViresh Kumar 	opp_table->regulators = NULL;
183446f48acaSViresh Kumar 	opp_table->regulator_count = -1;
18357813dd6fSViresh Kumar err:
18367813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
18377813dd6fSViresh Kumar 
18387813dd6fSViresh Kumar 	return ERR_PTR(ret);
18397813dd6fSViresh Kumar }
18407813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
18417813dd6fSViresh Kumar 
18427813dd6fSViresh Kumar /**
18437813dd6fSViresh Kumar  * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
18447813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
18457813dd6fSViresh Kumar  */
18467813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table)
18477813dd6fSViresh Kumar {
18487813dd6fSViresh Kumar 	int i;
18497813dd6fSViresh Kumar 
1850779b783cSViresh Kumar 	if (!opp_table->regulators)
1851779b783cSViresh Kumar 		goto put_opp_table;
18527813dd6fSViresh Kumar 
18537813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
18547813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
18557813dd6fSViresh Kumar 
185672f80ce4SViresh Kumar 	if (opp_table->enabled) {
18578d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
18588d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
18598d45719cSKamil Konieczny 	}
18608d45719cSKamil Konieczny 
186124957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
18627813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
18637813dd6fSViresh Kumar 
18647813dd6fSViresh Kumar 	_free_set_opp_data(opp_table);
18657813dd6fSViresh Kumar 
18667813dd6fSViresh Kumar 	kfree(opp_table->regulators);
18677813dd6fSViresh Kumar 	opp_table->regulators = NULL;
186846f48acaSViresh Kumar 	opp_table->regulator_count = -1;
18697813dd6fSViresh Kumar 
1870779b783cSViresh Kumar put_opp_table:
18717813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
18727813dd6fSViresh Kumar }
18737813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
18747813dd6fSViresh Kumar 
18757813dd6fSViresh Kumar /**
18767813dd6fSViresh Kumar  * dev_pm_opp_set_clkname() - Set clk name for the device
18777813dd6fSViresh Kumar  * @dev: Device for which clk name is being set.
18787813dd6fSViresh Kumar  * @name: Clk name.
18797813dd6fSViresh Kumar  *
18807813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointer to the
18817813dd6fSViresh Kumar  * clock for the device. Simple cases work fine without using this routine (i.e.
18827813dd6fSViresh Kumar  * by passing connection-id as NULL), but for a device with multiple clocks
18837813dd6fSViresh Kumar  * available, the OPP core needs to know the exact name of the clk to use.
18847813dd6fSViresh Kumar  *
18857813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
18867813dd6fSViresh Kumar  */
18877813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
18887813dd6fSViresh Kumar {
18897813dd6fSViresh Kumar 	struct opp_table *opp_table;
18907813dd6fSViresh Kumar 	int ret;
18917813dd6fSViresh Kumar 
18927813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
1893dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1894dd461cd9SStephan Gerhold 		return opp_table;
18957813dd6fSViresh Kumar 
18967813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
18977813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
18987813dd6fSViresh Kumar 		ret = -EBUSY;
18997813dd6fSViresh Kumar 		goto err;
19007813dd6fSViresh Kumar 	}
19017813dd6fSViresh Kumar 
19027813dd6fSViresh Kumar 	/* Already have default clk set, free it */
19037813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
19047813dd6fSViresh Kumar 		clk_put(opp_table->clk);
19057813dd6fSViresh Kumar 
19067813dd6fSViresh Kumar 	/* Find clk for the device */
19077813dd6fSViresh Kumar 	opp_table->clk = clk_get(dev, name);
19087813dd6fSViresh Kumar 	if (IS_ERR(opp_table->clk)) {
19097813dd6fSViresh Kumar 		ret = PTR_ERR(opp_table->clk);
19107813dd6fSViresh Kumar 		if (ret != -EPROBE_DEFER) {
19117813dd6fSViresh Kumar 			dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
19127813dd6fSViresh Kumar 				ret);
19137813dd6fSViresh Kumar 		}
19147813dd6fSViresh Kumar 		goto err;
19157813dd6fSViresh Kumar 	}
19167813dd6fSViresh Kumar 
19177813dd6fSViresh Kumar 	return opp_table;
19187813dd6fSViresh Kumar 
19197813dd6fSViresh Kumar err:
19207813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
19217813dd6fSViresh Kumar 
19227813dd6fSViresh Kumar 	return ERR_PTR(ret);
19237813dd6fSViresh Kumar }
19247813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
19257813dd6fSViresh Kumar 
19267813dd6fSViresh Kumar /**
19277813dd6fSViresh Kumar  * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
19287813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
19297813dd6fSViresh Kumar  */
19307813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table)
19317813dd6fSViresh Kumar {
19327813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
19337813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
19347813dd6fSViresh Kumar 
19357813dd6fSViresh Kumar 	clk_put(opp_table->clk);
19367813dd6fSViresh Kumar 	opp_table->clk = ERR_PTR(-EINVAL);
19377813dd6fSViresh Kumar 
19387813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
19397813dd6fSViresh Kumar }
19407813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
19417813dd6fSViresh Kumar 
19427813dd6fSViresh Kumar /**
19437813dd6fSViresh Kumar  * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
19447813dd6fSViresh Kumar  * @dev: Device for which the helper is getting registered.
19457813dd6fSViresh Kumar  * @set_opp: Custom set OPP helper.
19467813dd6fSViresh Kumar  *
19477813dd6fSViresh Kumar  * This is useful to support complex platforms (like platforms with multiple
19487813dd6fSViresh Kumar  * regulators per device), instead of the generic OPP set rate helper.
19497813dd6fSViresh Kumar  *
19507813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
19517813dd6fSViresh Kumar  */
19527813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
19537813dd6fSViresh Kumar 			int (*set_opp)(struct dev_pm_set_opp_data *data))
19547813dd6fSViresh Kumar {
19557813dd6fSViresh Kumar 	struct opp_table *opp_table;
19567813dd6fSViresh Kumar 
19577813dd6fSViresh Kumar 	if (!set_opp)
19587813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
19597813dd6fSViresh Kumar 
19607813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
196147efcbcbSViresh Kumar 	if (IS_ERR(opp_table))
1962dd461cd9SStephan Gerhold 		return opp_table;
19637813dd6fSViresh Kumar 
19647813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
19657813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
19665019acc6SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
19675019acc6SViresh Kumar 		return ERR_PTR(-EBUSY);
19687813dd6fSViresh Kumar 	}
19697813dd6fSViresh Kumar 
19705019acc6SViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
19715019acc6SViresh Kumar 	if (!opp_table->set_opp)
19727813dd6fSViresh Kumar 		opp_table->set_opp = set_opp;
19737813dd6fSViresh Kumar 
19747813dd6fSViresh Kumar 	return opp_table;
19757813dd6fSViresh Kumar }
19767813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
19777813dd6fSViresh Kumar 
19787813dd6fSViresh Kumar /**
1979604a7aebSViresh Kumar  * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
19807813dd6fSViresh Kumar  *					   set_opp helper
19817813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
19827813dd6fSViresh Kumar  *
19837813dd6fSViresh Kumar  * Release resources blocked for platform specific set_opp helper.
19847813dd6fSViresh Kumar  */
1985604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
19867813dd6fSViresh Kumar {
19877813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
19887813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
19897813dd6fSViresh Kumar 
19907813dd6fSViresh Kumar 	opp_table->set_opp = NULL;
19917813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
19927813dd6fSViresh Kumar }
1993604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
19947813dd6fSViresh Kumar 
19956319aee1SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
19966319aee1SViresh Kumar {
19976319aee1SViresh Kumar 	int index;
19986319aee1SViresh Kumar 
1999cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2000cb60e960SViresh Kumar 		return;
2001cb60e960SViresh Kumar 
20026319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
20036319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
20046319aee1SViresh Kumar 			continue;
20056319aee1SViresh Kumar 
20066319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
20076319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
20086319aee1SViresh Kumar 	}
2009c0ab9e08SViresh Kumar 
2010c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2011c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
20126319aee1SViresh Kumar }
20136319aee1SViresh Kumar 
20147813dd6fSViresh Kumar /**
20156319aee1SViresh Kumar  * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
20166319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
20176319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
201817a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
20194f018bc0SViresh Kumar  *
20204f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
20214f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
20224f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
20234f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
20246319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
20256319aee1SViresh Kumar  * we don't need to support that separately.
20264f018bc0SViresh Kumar  *
20274f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
20286319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
20294f018bc0SViresh Kumar  *
20306319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
20316319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2032baea35e4SViresh Kumar  *
2033baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2034baea35e4SViresh Kumar  * "required-opps" are added in DT.
20354f018bc0SViresh Kumar  */
203617a8f868SViresh Kumar struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
203717a8f868SViresh Kumar 		const char **names, struct device ***virt_devs)
20384f018bc0SViresh Kumar {
20394f018bc0SViresh Kumar 	struct opp_table *opp_table;
20406319aee1SViresh Kumar 	struct device *virt_dev;
2041baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
20426319aee1SViresh Kumar 	const char **name = names;
20434f018bc0SViresh Kumar 
20444f018bc0SViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
2045dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2046dd461cd9SStephan Gerhold 		return opp_table;
20474f018bc0SViresh Kumar 
2048cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2049cb60e960SViresh Kumar 		return opp_table;
20504f018bc0SViresh Kumar 
20516319aee1SViresh Kumar 	/*
20526319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
20536319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
20546319aee1SViresh Kumar 	 * table is added.
20556319aee1SViresh Kumar 	 */
20566319aee1SViresh Kumar 	if (!opp_table->required_opp_count) {
20576319aee1SViresh Kumar 		ret = -EPROBE_DEFER;
20586319aee1SViresh Kumar 		goto put_table;
20596319aee1SViresh Kumar 	}
20606319aee1SViresh Kumar 
20614f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
20624f018bc0SViresh Kumar 
2063c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2064c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2065c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2066c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2067c0ab9e08SViresh Kumar 		goto unlock;
20684f018bc0SViresh Kumar 
20696319aee1SViresh Kumar 	while (*name) {
20706319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
20716319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
20726319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
20736319aee1SViresh Kumar 			goto err;
20746319aee1SViresh Kumar 		}
20754f018bc0SViresh Kumar 
20766319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
20776319aee1SViresh Kumar 		if (IS_ERR(virt_dev)) {
20786319aee1SViresh Kumar 			ret = PTR_ERR(virt_dev);
20796319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
20806319aee1SViresh Kumar 			goto err;
20814f018bc0SViresh Kumar 		}
20824f018bc0SViresh Kumar 
20834f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2084baea35e4SViresh Kumar 		index++;
20856319aee1SViresh Kumar 		name++;
20866319aee1SViresh Kumar 	}
20876319aee1SViresh Kumar 
208817a8f868SViresh Kumar 	if (virt_devs)
208917a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
20904f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
20914f018bc0SViresh Kumar 
20924f018bc0SViresh Kumar 	return opp_table;
20936319aee1SViresh Kumar 
20946319aee1SViresh Kumar err:
20956319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
2096c0ab9e08SViresh Kumar unlock:
20976319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
20986319aee1SViresh Kumar 
20996319aee1SViresh Kumar put_table:
21006319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
21016319aee1SViresh Kumar 
21026319aee1SViresh Kumar 	return ERR_PTR(ret);
21034f018bc0SViresh Kumar }
21046319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
21054f018bc0SViresh Kumar 
21064f018bc0SViresh Kumar /**
21076319aee1SViresh Kumar  * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
21086319aee1SViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
21094f018bc0SViresh Kumar  *
21106319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
21116319aee1SViresh Kumar  * OPP table.
21124f018bc0SViresh Kumar  */
21136319aee1SViresh Kumar void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
21144f018bc0SViresh Kumar {
21154f018bc0SViresh Kumar 	/*
21164f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
21174f018bc0SViresh Kumar 	 * used in parallel.
21184f018bc0SViresh Kumar 	 */
21194f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
21206319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
21214f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
21224f018bc0SViresh Kumar 
21236319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
21244f018bc0SViresh Kumar }
21256319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
21264f018bc0SViresh Kumar 
21274f018bc0SViresh Kumar /**
2128c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2129c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2130c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2131c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2132c8a59103SViresh Kumar  *
2133c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2134c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2135c8a59103SViresh Kumar  * performance state set to @pstate.
2136c8a59103SViresh Kumar  *
2137c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2138c8a59103SViresh Kumar  * value on errors.
2139c8a59103SViresh Kumar  */
2140c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2141c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2142c8a59103SViresh Kumar 				       unsigned int pstate)
2143c8a59103SViresh Kumar {
2144c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2145c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2146c8a59103SViresh Kumar 	int i;
2147c8a59103SViresh Kumar 
2148c8a59103SViresh Kumar 	/*
2149c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2150c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2151c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2152c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2153c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2154c8a59103SViresh Kumar 	 */
2155c8a59103SViresh Kumar 	if (!src_table->required_opp_count)
2156c8a59103SViresh Kumar 		return pstate;
2157c8a59103SViresh Kumar 
2158c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2159c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2160c8a59103SViresh Kumar 			break;
2161c8a59103SViresh Kumar 	}
2162c8a59103SViresh Kumar 
2163c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2164c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2165c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2166c8a59103SViresh Kumar 		return -EINVAL;
2167c8a59103SViresh Kumar 	}
2168c8a59103SViresh Kumar 
2169c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2170c8a59103SViresh Kumar 
2171c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
2172c8a59103SViresh Kumar 		if (opp->pstate == pstate) {
2173c8a59103SViresh Kumar 			dest_pstate = opp->required_opps[i]->pstate;
2174c8a59103SViresh Kumar 			goto unlock;
2175c8a59103SViresh Kumar 		}
2176c8a59103SViresh Kumar 	}
2177c8a59103SViresh Kumar 
2178c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2179c8a59103SViresh Kumar 	       dst_table);
2180c8a59103SViresh Kumar 
2181c8a59103SViresh Kumar unlock:
2182c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2183c8a59103SViresh Kumar 
2184c8a59103SViresh Kumar 	return dest_pstate;
2185c8a59103SViresh Kumar }
2186c8a59103SViresh Kumar 
2187c8a59103SViresh Kumar /**
21887813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
21897813dd6fSViresh Kumar  * @dev:	device for which we do this operation
21907813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
21917813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
21927813dd6fSViresh Kumar  *
21937813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
21947813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
21957813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
21967813dd6fSViresh Kumar  *
21977813dd6fSViresh Kumar  * Return:
21987813dd6fSViresh Kumar  * 0		On success OR
21997813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
22007813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
22017813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
22027813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
22037813dd6fSViresh Kumar  */
22047813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
22057813dd6fSViresh Kumar {
22067813dd6fSViresh Kumar 	struct opp_table *opp_table;
22077813dd6fSViresh Kumar 	int ret;
22087813dd6fSViresh Kumar 
22097813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
2210dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2211dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
22127813dd6fSViresh Kumar 
221346f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
221446f48acaSViresh Kumar 	opp_table->regulator_count = 1;
221546f48acaSViresh Kumar 
22167813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
22170ad8c623SViresh Kumar 	if (ret)
22187813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
22190ad8c623SViresh Kumar 
22207813dd6fSViresh Kumar 	return ret;
22217813dd6fSViresh Kumar }
22227813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
22237813dd6fSViresh Kumar 
22247813dd6fSViresh Kumar /**
22257813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
22267813dd6fSViresh Kumar  * @dev:		device for which we do this operation
22277813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
22287813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
22297813dd6fSViresh Kumar  *
22307813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
22317813dd6fSViresh Kumar  * which is isolated here.
22327813dd6fSViresh Kumar  *
22337813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
22347813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
22357813dd6fSViresh Kumar  * successful.
22367813dd6fSViresh Kumar  */
22377813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
22387813dd6fSViresh Kumar 				 bool availability_req)
22397813dd6fSViresh Kumar {
22407813dd6fSViresh Kumar 	struct opp_table *opp_table;
22417813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
22427813dd6fSViresh Kumar 	int r = 0;
22437813dd6fSViresh Kumar 
22447813dd6fSViresh Kumar 	/* Find the opp_table */
22457813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
22467813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
22477813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
22487813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
22497813dd6fSViresh Kumar 		return r;
22507813dd6fSViresh Kumar 	}
22517813dd6fSViresh Kumar 
22527813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
22537813dd6fSViresh Kumar 
22547813dd6fSViresh Kumar 	/* Do we have the frequency? */
22557813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
22567813dd6fSViresh Kumar 		if (tmp_opp->rate == freq) {
22577813dd6fSViresh Kumar 			opp = tmp_opp;
22587813dd6fSViresh Kumar 			break;
22597813dd6fSViresh Kumar 		}
22607813dd6fSViresh Kumar 	}
22617813dd6fSViresh Kumar 
22627813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
22637813dd6fSViresh Kumar 		r = PTR_ERR(opp);
22647813dd6fSViresh Kumar 		goto unlock;
22657813dd6fSViresh Kumar 	}
22667813dd6fSViresh Kumar 
22677813dd6fSViresh Kumar 	/* Is update really needed? */
22687813dd6fSViresh Kumar 	if (opp->available == availability_req)
22697813dd6fSViresh Kumar 		goto unlock;
22707813dd6fSViresh Kumar 
22717813dd6fSViresh Kumar 	opp->available = availability_req;
22727813dd6fSViresh Kumar 
22737813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
22747813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
22757813dd6fSViresh Kumar 
22767813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
22777813dd6fSViresh Kumar 	if (availability_req)
22787813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
22797813dd6fSViresh Kumar 					     opp);
22807813dd6fSViresh Kumar 	else
22817813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
22827813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
22837813dd6fSViresh Kumar 
22847813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
22857813dd6fSViresh Kumar 	goto put_table;
22867813dd6fSViresh Kumar 
22877813dd6fSViresh Kumar unlock:
22887813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
22897813dd6fSViresh Kumar put_table:
22907813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
22917813dd6fSViresh Kumar 	return r;
22927813dd6fSViresh Kumar }
22937813dd6fSViresh Kumar 
22947813dd6fSViresh Kumar /**
229525cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
229625cb20a2SStephen Boyd  * @dev:		device for which we do this operation
229725cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
229825cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
229925cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
230025cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
230125cb20a2SStephen Boyd  *
230225cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
230325cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
230425cb20a2SStephen Boyd  * successful.
230525cb20a2SStephen Boyd  */
230625cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
230725cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
230825cb20a2SStephen Boyd 			      unsigned long u_volt_max)
230925cb20a2SStephen Boyd 
231025cb20a2SStephen Boyd {
231125cb20a2SStephen Boyd 	struct opp_table *opp_table;
231225cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
231325cb20a2SStephen Boyd 	int r = 0;
231425cb20a2SStephen Boyd 
231525cb20a2SStephen Boyd 	/* Find the opp_table */
231625cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
231725cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
231825cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
231925cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
232025cb20a2SStephen Boyd 		return r;
232125cb20a2SStephen Boyd 	}
232225cb20a2SStephen Boyd 
232325cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
232425cb20a2SStephen Boyd 
232525cb20a2SStephen Boyd 	/* Do we have the frequency? */
232625cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
232725cb20a2SStephen Boyd 		if (tmp_opp->rate == freq) {
232825cb20a2SStephen Boyd 			opp = tmp_opp;
232925cb20a2SStephen Boyd 			break;
233025cb20a2SStephen Boyd 		}
233125cb20a2SStephen Boyd 	}
233225cb20a2SStephen Boyd 
233325cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
233425cb20a2SStephen Boyd 		r = PTR_ERR(opp);
233525cb20a2SStephen Boyd 		goto adjust_unlock;
233625cb20a2SStephen Boyd 	}
233725cb20a2SStephen Boyd 
233825cb20a2SStephen Boyd 	/* Is update really needed? */
233925cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
234025cb20a2SStephen Boyd 		goto adjust_unlock;
234125cb20a2SStephen Boyd 
234225cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
234325cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
234425cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
234525cb20a2SStephen Boyd 
234625cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
234725cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
234825cb20a2SStephen Boyd 
234925cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
235025cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
235125cb20a2SStephen Boyd 				     opp);
235225cb20a2SStephen Boyd 
235325cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
235425cb20a2SStephen Boyd 	goto adjust_put_table;
235525cb20a2SStephen Boyd 
235625cb20a2SStephen Boyd adjust_unlock:
235725cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
235825cb20a2SStephen Boyd adjust_put_table:
235925cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
236025cb20a2SStephen Boyd 	return r;
236125cb20a2SStephen Boyd }
236203649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
236325cb20a2SStephen Boyd 
236425cb20a2SStephen Boyd /**
23657813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
23667813dd6fSViresh Kumar  * @dev:	device for which we do this operation
23677813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
23687813dd6fSViresh Kumar  *
23697813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
23707813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
23717813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
23727813dd6fSViresh Kumar  *
23737813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
23747813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
23757813dd6fSViresh Kumar  * successful.
23767813dd6fSViresh Kumar  */
23777813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
23787813dd6fSViresh Kumar {
23797813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
23807813dd6fSViresh Kumar }
23817813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
23827813dd6fSViresh Kumar 
23837813dd6fSViresh Kumar /**
23847813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
23857813dd6fSViresh Kumar  * @dev:	device for which we do this operation
23867813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
23877813dd6fSViresh Kumar  *
23887813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
23897813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
23907813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
23917813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
23927813dd6fSViresh Kumar  *
23937813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
23947813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
23957813dd6fSViresh Kumar  * successful.
23967813dd6fSViresh Kumar  */
23977813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
23987813dd6fSViresh Kumar {
23997813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
24007813dd6fSViresh Kumar }
24017813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
24027813dd6fSViresh Kumar 
24037813dd6fSViresh Kumar /**
24047813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
24057813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
24067813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
24077813dd6fSViresh Kumar  *
24087813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
24097813dd6fSViresh Kumar  */
24107813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
24117813dd6fSViresh Kumar {
24127813dd6fSViresh Kumar 	struct opp_table *opp_table;
24137813dd6fSViresh Kumar 	int ret;
24147813dd6fSViresh Kumar 
24157813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
24167813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
24177813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
24187813dd6fSViresh Kumar 
24197813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
24207813dd6fSViresh Kumar 
24217813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
24227813dd6fSViresh Kumar 
24237813dd6fSViresh Kumar 	return ret;
24247813dd6fSViresh Kumar }
24257813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
24267813dd6fSViresh Kumar 
24277813dd6fSViresh Kumar /**
24287813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
24297813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
24307813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
24317813dd6fSViresh Kumar  *
24327813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
24337813dd6fSViresh Kumar  */
24347813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
24357813dd6fSViresh Kumar 				   struct notifier_block *nb)
24367813dd6fSViresh Kumar {
24377813dd6fSViresh Kumar 	struct opp_table *opp_table;
24387813dd6fSViresh Kumar 	int ret;
24397813dd6fSViresh Kumar 
24407813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
24417813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
24427813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
24437813dd6fSViresh Kumar 
24447813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
24457813dd6fSViresh Kumar 
24467813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
24477813dd6fSViresh Kumar 
24487813dd6fSViresh Kumar 	return ret;
24497813dd6fSViresh Kumar }
24507813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
24517813dd6fSViresh Kumar 
24528aaf6264SViresh Kumar /**
24538aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
24548aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
24558aaf6264SViresh Kumar  *
24568aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
24578aaf6264SViresh Kumar  * dynamically added entries.
24588aaf6264SViresh Kumar  */
24598aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
24607813dd6fSViresh Kumar {
24617813dd6fSViresh Kumar 	struct opp_table *opp_table;
24627813dd6fSViresh Kumar 
24637813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
24647813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
24657813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
24667813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
24677813dd6fSViresh Kumar 
24687813dd6fSViresh Kumar 		if (error != -ENODEV)
24697813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
24707813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
24717813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
24727813dd6fSViresh Kumar 			     error);
24737813dd6fSViresh Kumar 		return;
24747813dd6fSViresh Kumar 	}
24757813dd6fSViresh Kumar 
2476922ff075SViresh Kumar 	/*
2477922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
2478922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
2479922ff075SViresh Kumar 	 **/
2480922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
2481cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
2482cdd6ed90SViresh Kumar 
2483922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
24847813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
24857813dd6fSViresh Kumar }
24867813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
2487