xref: /openbmc/linux/drivers/opp/core.c (revision 5f756d03e2c7db63c1df7148d7b1739f29ff1532)
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/device.h>
177813dd6fSViresh Kumar #include <linux/export.h>
18009acd19SViresh Kumar #include <linux/pm_domain.h>
197813dd6fSViresh Kumar #include <linux/regulator/consumer.h>
2011b9b663SViresh Kumar #include <linux/slab.h>
2111b9b663SViresh Kumar #include <linux/xarray.h>
227813dd6fSViresh Kumar 
237813dd6fSViresh Kumar #include "opp.h"
247813dd6fSViresh Kumar 
257813dd6fSViresh Kumar /*
267813dd6fSViresh Kumar  * The root of the list of all opp-tables. All opp_table structures branch off
277813dd6fSViresh Kumar  * from here, with each opp_table containing the list of opps it supports in
287813dd6fSViresh Kumar  * various states of availability.
297813dd6fSViresh Kumar  */
307813dd6fSViresh Kumar LIST_HEAD(opp_tables);
317eba0c76SViresh Kumar 
327813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
337813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
3427c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */
3527c09484SViresh Kumar static bool opp_tables_busy;
367813dd6fSViresh Kumar 
3711b9b663SViresh Kumar /* OPP ID allocator */
3811b9b663SViresh Kumar static DEFINE_XARRAY_ALLOC1(opp_configs);
3911b9b663SViresh Kumar 
409e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
417813dd6fSViresh Kumar {
427813dd6fSViresh Kumar 	struct opp_device *opp_dev;
439e62edacSViresh Kumar 	bool found = false;
447813dd6fSViresh Kumar 
459e62edacSViresh Kumar 	mutex_lock(&opp_table->lock);
467813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
479e62edacSViresh Kumar 		if (opp_dev->dev == dev) {
489e62edacSViresh Kumar 			found = true;
499e62edacSViresh Kumar 			break;
509e62edacSViresh Kumar 		}
517813dd6fSViresh Kumar 
529e62edacSViresh Kumar 	mutex_unlock(&opp_table->lock);
539e62edacSViresh Kumar 	return found;
547813dd6fSViresh Kumar }
557813dd6fSViresh Kumar 
567813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
577813dd6fSViresh Kumar {
587813dd6fSViresh Kumar 	struct opp_table *opp_table;
597813dd6fSViresh Kumar 
607813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
619e62edacSViresh Kumar 		if (_find_opp_dev(dev, opp_table)) {
627813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
637813dd6fSViresh Kumar 			return opp_table;
647813dd6fSViresh Kumar 		}
657813dd6fSViresh Kumar 	}
667813dd6fSViresh Kumar 
677813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
687813dd6fSViresh Kumar }
697813dd6fSViresh Kumar 
707813dd6fSViresh Kumar /**
717813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
727813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
737813dd6fSViresh Kumar  *
747813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
757813dd6fSViresh Kumar  *
767813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
777813dd6fSViresh Kumar  * -EINVAL based on type of error.
787813dd6fSViresh Kumar  *
797813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
807813dd6fSViresh Kumar  */
817813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
827813dd6fSViresh Kumar {
837813dd6fSViresh Kumar 	struct opp_table *opp_table;
847813dd6fSViresh Kumar 
857813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
867813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
877813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
887813dd6fSViresh Kumar 	}
897813dd6fSViresh Kumar 
907813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
917813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
927813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
937813dd6fSViresh Kumar 
947813dd6fSViresh Kumar 	return opp_table;
957813dd6fSViresh Kumar }
967813dd6fSViresh Kumar 
97f123ea74SViresh Kumar /*
98f123ea74SViresh Kumar  * Returns true if multiple clocks aren't there, else returns false with WARN.
99f123ea74SViresh Kumar  *
100f123ea74SViresh Kumar  * We don't force clk_count == 1 here as there are users who don't have a clock
101f123ea74SViresh Kumar  * representation in the OPP table and manage the clock configuration themselves
102f123ea74SViresh Kumar  * in an platform specific way.
103f123ea74SViresh Kumar  */
104f123ea74SViresh Kumar static bool assert_single_clk(struct opp_table *opp_table)
105f123ea74SViresh Kumar {
106f123ea74SViresh Kumar 	return !WARN_ON(opp_table->clk_count > 1);
107f123ea74SViresh Kumar }
108f123ea74SViresh Kumar 
1097813dd6fSViresh Kumar /**
1107813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
1117813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
1127813dd6fSViresh Kumar  *
1137813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
1147813dd6fSViresh Kumar  * return 0
1157813dd6fSViresh Kumar  *
1167813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1177813dd6fSViresh Kumar  */
1187813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1197813dd6fSViresh Kumar {
1207813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1217813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1227813dd6fSViresh Kumar 		return 0;
1237813dd6fSViresh Kumar 	}
1247813dd6fSViresh Kumar 
1257813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1267813dd6fSViresh Kumar }
1277813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1287813dd6fSViresh Kumar 
1297813dd6fSViresh Kumar /**
13069b1af17SViresh Kumar  * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp
13169b1af17SViresh Kumar  * @opp:	opp for which voltage has to be returned for
13269b1af17SViresh Kumar  * @supplies:	Placeholder for copying the supply information.
13369b1af17SViresh Kumar  *
13469b1af17SViresh Kumar  * Return: negative error number on failure, 0 otherwise on success after
13569b1af17SViresh Kumar  * setting @supplies.
13669b1af17SViresh Kumar  *
13769b1af17SViresh Kumar  * This can be used for devices with any number of power supplies. The caller
13869b1af17SViresh Kumar  * must ensure the @supplies array must contain space for each regulator.
13969b1af17SViresh Kumar  */
14069b1af17SViresh Kumar int dev_pm_opp_get_supplies(struct dev_pm_opp *opp,
14169b1af17SViresh Kumar 			    struct dev_pm_opp_supply *supplies)
14269b1af17SViresh Kumar {
14369b1af17SViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !supplies) {
14469b1af17SViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
14569b1af17SViresh Kumar 		return -EINVAL;
14669b1af17SViresh Kumar 	}
14769b1af17SViresh Kumar 
14869b1af17SViresh Kumar 	memcpy(supplies, opp->supplies,
14969b1af17SViresh Kumar 	       sizeof(*supplies) * opp->opp_table->regulator_count);
15069b1af17SViresh Kumar 	return 0;
15169b1af17SViresh Kumar }
15269b1af17SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_supplies);
15369b1af17SViresh Kumar 
15469b1af17SViresh Kumar /**
1554f9a7a1dSLukasz Luba  * dev_pm_opp_get_power() - Gets the power corresponding to an opp
1564f9a7a1dSLukasz Luba  * @opp:	opp for which power has to be returned for
1574f9a7a1dSLukasz Luba  *
1584f9a7a1dSLukasz Luba  * Return: power in micro watt corresponding to the opp, else
1594f9a7a1dSLukasz Luba  * return 0
1604f9a7a1dSLukasz Luba  *
1614f9a7a1dSLukasz Luba  * This is useful only for devices with single power supply.
1624f9a7a1dSLukasz Luba  */
1634f9a7a1dSLukasz Luba unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
1644f9a7a1dSLukasz Luba {
1654f9a7a1dSLukasz Luba 	unsigned long opp_power = 0;
1664f9a7a1dSLukasz Luba 	int i;
1674f9a7a1dSLukasz Luba 
1684f9a7a1dSLukasz Luba 	if (IS_ERR_OR_NULL(opp)) {
1694f9a7a1dSLukasz Luba 		pr_err("%s: Invalid parameters\n", __func__);
1704f9a7a1dSLukasz Luba 		return 0;
1714f9a7a1dSLukasz Luba 	}
1724f9a7a1dSLukasz Luba 	for (i = 0; i < opp->opp_table->regulator_count; i++)
1734f9a7a1dSLukasz Luba 		opp_power += opp->supplies[i].u_watt;
1744f9a7a1dSLukasz Luba 
1754f9a7a1dSLukasz Luba 	return opp_power;
1764f9a7a1dSLukasz Luba }
1774f9a7a1dSLukasz Luba EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
1784f9a7a1dSLukasz Luba 
1794f9a7a1dSLukasz Luba /**
1807813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1817813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1827813dd6fSViresh Kumar  *
1837813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1847813dd6fSViresh Kumar  * return 0
1857813dd6fSViresh Kumar  */
1867813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1877813dd6fSViresh Kumar {
18806a8a059SAndrew-sh.Cheng 	if (IS_ERR_OR_NULL(opp)) {
1897813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1907813dd6fSViresh Kumar 		return 0;
1917813dd6fSViresh Kumar 	}
1927813dd6fSViresh Kumar 
193f123ea74SViresh Kumar 	if (!assert_single_clk(opp->opp_table))
194f123ea74SViresh Kumar 		return 0;
195f123ea74SViresh Kumar 
1962083da24SViresh Kumar 	return opp->rates[0];
1977813dd6fSViresh Kumar }
1987813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
1997813dd6fSViresh Kumar 
2007813dd6fSViresh Kumar /**
201*5f756d03SManivannan Sadhasivam  * dev_pm_opp_get_freq_indexed() - Gets the frequency corresponding to an
202*5f756d03SManivannan Sadhasivam  *				   available opp with specified index
203*5f756d03SManivannan Sadhasivam  * @opp: opp for which frequency has to be returned for
204*5f756d03SManivannan Sadhasivam  * @index: index of the frequency within the required opp
205*5f756d03SManivannan Sadhasivam  *
206*5f756d03SManivannan Sadhasivam  * Return: frequency in hertz corresponding to the opp with specified index,
207*5f756d03SManivannan Sadhasivam  * else return 0
208*5f756d03SManivannan Sadhasivam  */
209*5f756d03SManivannan Sadhasivam unsigned long dev_pm_opp_get_freq_indexed(struct dev_pm_opp *opp, u32 index)
210*5f756d03SManivannan Sadhasivam {
211*5f756d03SManivannan Sadhasivam 	if (IS_ERR_OR_NULL(opp) || index >= opp->opp_table->clk_count) {
212*5f756d03SManivannan Sadhasivam 		pr_err("%s: Invalid parameters\n", __func__);
213*5f756d03SManivannan Sadhasivam 		return 0;
214*5f756d03SManivannan Sadhasivam 	}
215*5f756d03SManivannan Sadhasivam 
216*5f756d03SManivannan Sadhasivam 	return opp->rates[index];
217*5f756d03SManivannan Sadhasivam }
218*5f756d03SManivannan Sadhasivam EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq_indexed);
219*5f756d03SManivannan Sadhasivam 
220*5f756d03SManivannan Sadhasivam /**
2215b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
2225b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
2235b93ac54SRajendra Nayak  *
2245b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
2255b93ac54SRajendra Nayak  * return 0.
2265b93ac54SRajendra Nayak  */
2275b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
2285b93ac54SRajendra Nayak {
2295b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2305b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
2315b93ac54SRajendra Nayak 		return 0;
2325b93ac54SRajendra Nayak 	}
2335b93ac54SRajendra Nayak 
2345b93ac54SRajendra Nayak 	return opp->level;
2355b93ac54SRajendra Nayak }
2365b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
2375b93ac54SRajendra Nayak 
2385b93ac54SRajendra Nayak /**
239597ff543SDmitry Osipenko  * dev_pm_opp_get_required_pstate() - Gets the required performance state
240597ff543SDmitry Osipenko  *                                    corresponding to an available opp
241597ff543SDmitry Osipenko  * @opp:	opp for which performance state has to be returned for
242597ff543SDmitry Osipenko  * @index:	index of the required opp
243597ff543SDmitry Osipenko  *
244597ff543SDmitry Osipenko  * Return: performance state read from device tree corresponding to the
245597ff543SDmitry Osipenko  * required opp, else return 0.
246597ff543SDmitry Osipenko  */
247597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
248597ff543SDmitry Osipenko 					    unsigned int index)
249597ff543SDmitry Osipenko {
25084cb7ff3SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
25184cb7ff3SViresh Kumar 
252597ff543SDmitry Osipenko 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
25384cb7ff3SViresh Kumar 	    index >= opp_table->required_opp_count) {
254597ff543SDmitry Osipenko 		pr_err("%s: Invalid parameters\n", __func__);
255597ff543SDmitry Osipenko 		return 0;
256597ff543SDmitry Osipenko 	}
257597ff543SDmitry Osipenko 
2587eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
25984cb7ff3SViresh Kumar 	if (lazy_linking_pending(opp_table))
2607eba0c76SViresh Kumar 		return 0;
2617eba0c76SViresh Kumar 
26284cb7ff3SViresh Kumar 	/* The required OPP table must belong to a genpd */
26384cb7ff3SViresh Kumar 	if (unlikely(!opp_table->required_opp_tables[index]->is_genpd)) {
26484cb7ff3SViresh Kumar 		pr_err("%s: Performance state is only valid for genpds.\n", __func__);
26584cb7ff3SViresh Kumar 		return 0;
26684cb7ff3SViresh Kumar 	}
26784cb7ff3SViresh Kumar 
2687c41cdcdSViresh Kumar 	return opp->required_opps[index]->level;
269597ff543SDmitry Osipenko }
270597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
271597ff543SDmitry Osipenko 
272597ff543SDmitry Osipenko /**
2737813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
2747813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
2757813dd6fSViresh Kumar  *
2767813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
2777813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
2787813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
2797813dd6fSViresh Kumar  *
2807813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
2817813dd6fSViresh Kumar  */
2827813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
2837813dd6fSViresh Kumar {
2847813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2857813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
2867813dd6fSViresh Kumar 		return false;
2877813dd6fSViresh Kumar 	}
2887813dd6fSViresh Kumar 
2897813dd6fSViresh Kumar 	return opp->turbo;
2907813dd6fSViresh Kumar }
2917813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
2927813dd6fSViresh Kumar 
2937813dd6fSViresh Kumar /**
2947813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
2957813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2967813dd6fSViresh Kumar  *
2977813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
2987813dd6fSViresh Kumar  */
2997813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
3007813dd6fSViresh Kumar {
3017813dd6fSViresh Kumar 	struct opp_table *opp_table;
3027813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
3037813dd6fSViresh Kumar 
3047813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3057813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3067813dd6fSViresh Kumar 		return 0;
3077813dd6fSViresh Kumar 
3087813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
3097813dd6fSViresh Kumar 
3107813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3117813dd6fSViresh Kumar 
3127813dd6fSViresh Kumar 	return clock_latency_ns;
3137813dd6fSViresh Kumar }
3147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
3157813dd6fSViresh Kumar 
3167813dd6fSViresh Kumar /**
3177813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
3187813dd6fSViresh Kumar  * @dev: device for which we do this operation
3197813dd6fSViresh Kumar  *
3207813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
3217813dd6fSViresh Kumar  */
3227813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
3237813dd6fSViresh Kumar {
3247813dd6fSViresh Kumar 	struct opp_table *opp_table;
3257813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
3267813dd6fSViresh Kumar 	struct regulator *reg;
3277813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
3287813dd6fSViresh Kumar 	int ret, i, count;
3297813dd6fSViresh Kumar 	struct {
3307813dd6fSViresh Kumar 		unsigned long min;
3317813dd6fSViresh Kumar 		unsigned long max;
3327813dd6fSViresh Kumar 	} *uV;
3337813dd6fSViresh Kumar 
3347813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3357813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3367813dd6fSViresh Kumar 		return 0;
3377813dd6fSViresh Kumar 
3387813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
33990e3577bSViresh Kumar 	if (!opp_table->regulators)
3407813dd6fSViresh Kumar 		goto put_opp_table;
3417813dd6fSViresh Kumar 
34290e3577bSViresh Kumar 	count = opp_table->regulator_count;
34390e3577bSViresh Kumar 
3447813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
3457813dd6fSViresh Kumar 	if (!uV)
3467813dd6fSViresh Kumar 		goto put_opp_table;
3477813dd6fSViresh Kumar 
3487813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
3497813dd6fSViresh Kumar 
3507813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3517813dd6fSViresh Kumar 		uV[i].min = ~0;
3527813dd6fSViresh Kumar 		uV[i].max = 0;
3537813dd6fSViresh Kumar 
3547813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
3557813dd6fSViresh Kumar 			if (!opp->available)
3567813dd6fSViresh Kumar 				continue;
3577813dd6fSViresh Kumar 
3587813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
3597813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
3607813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
3617813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
3627813dd6fSViresh Kumar 		}
3637813dd6fSViresh Kumar 	}
3647813dd6fSViresh Kumar 
3657813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
3667813dd6fSViresh Kumar 
3677813dd6fSViresh Kumar 	/*
3687813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
3697813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
3707813dd6fSViresh Kumar 	 */
3717813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3727813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
3737813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
3747813dd6fSViresh Kumar 		if (ret > 0)
3757813dd6fSViresh Kumar 			latency_ns += ret * 1000;
3767813dd6fSViresh Kumar 	}
3777813dd6fSViresh Kumar 
3787813dd6fSViresh Kumar 	kfree(uV);
3797813dd6fSViresh Kumar put_opp_table:
3807813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3817813dd6fSViresh Kumar 
3827813dd6fSViresh Kumar 	return latency_ns;
3837813dd6fSViresh Kumar }
3847813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
3857813dd6fSViresh Kumar 
3867813dd6fSViresh Kumar /**
3877813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
3887813dd6fSViresh Kumar  *					     nanoseconds
3897813dd6fSViresh Kumar  * @dev: device for which we do this operation
3907813dd6fSViresh Kumar  *
3917813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
3927813dd6fSViresh Kumar  * switch from one OPP to other.
3937813dd6fSViresh Kumar  */
3947813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
3957813dd6fSViresh Kumar {
3967813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
3977813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
3987813dd6fSViresh Kumar }
3997813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
4007813dd6fSViresh Kumar 
4017813dd6fSViresh Kumar /**
4027813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
4037813dd6fSViresh Kumar  * @dev:	device for which we do this operation
4047813dd6fSViresh Kumar  *
4057813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
4067813dd6fSViresh Kumar  * if one is available, else returns 0;
4077813dd6fSViresh Kumar  */
4087813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
4097813dd6fSViresh Kumar {
4107813dd6fSViresh Kumar 	struct opp_table *opp_table;
4117813dd6fSViresh Kumar 	unsigned long freq = 0;
4127813dd6fSViresh Kumar 
4137813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4147813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
4157813dd6fSViresh Kumar 		return 0;
4167813dd6fSViresh Kumar 
4177813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
4187813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
4197813dd6fSViresh Kumar 
4207813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4217813dd6fSViresh Kumar 
4227813dd6fSViresh Kumar 	return freq;
4237813dd6fSViresh Kumar }
4247813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
4257813dd6fSViresh Kumar 
426a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
427a1e8c136SViresh Kumar {
428a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
429a1e8c136SViresh Kumar 	int count = 0;
430a1e8c136SViresh Kumar 
431a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
432a1e8c136SViresh Kumar 
433a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
434a1e8c136SViresh Kumar 		if (opp->available)
435a1e8c136SViresh Kumar 			count++;
436a1e8c136SViresh Kumar 	}
437a1e8c136SViresh Kumar 
438a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
439a1e8c136SViresh Kumar 
440a1e8c136SViresh Kumar 	return count;
441a1e8c136SViresh Kumar }
442a1e8c136SViresh Kumar 
4437813dd6fSViresh Kumar /**
4447813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
4457813dd6fSViresh Kumar  * @dev:	device for which we do this operation
4467813dd6fSViresh Kumar  *
4477813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
4487813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
4497813dd6fSViresh Kumar  */
4507813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
4517813dd6fSViresh Kumar {
4527813dd6fSViresh Kumar 	struct opp_table *opp_table;
453a1e8c136SViresh Kumar 	int count;
4547813dd6fSViresh Kumar 
4557813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4567813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
4577813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
458035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
4597813dd6fSViresh Kumar 			__func__, count);
46009f662f9SViresh Kumar 		return count;
4617813dd6fSViresh Kumar 	}
4627813dd6fSViresh Kumar 
463a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
4647813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4657813dd6fSViresh Kumar 
4667813dd6fSViresh Kumar 	return count;
4677813dd6fSViresh Kumar }
4687813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
4697813dd6fSViresh Kumar 
470aab8ced2SViresh Kumar /* Helpers to read keys */
471aab8ced2SViresh Kumar static unsigned long _read_freq(struct dev_pm_opp *opp, int index)
472aab8ced2SViresh Kumar {
4732083da24SViresh Kumar 	return opp->rates[0];
474aab8ced2SViresh Kumar }
475aab8ced2SViresh Kumar 
476c2ab2cb6SViresh Kumar static unsigned long _read_level(struct dev_pm_opp *opp, int index)
477c2ab2cb6SViresh Kumar {
478c2ab2cb6SViresh Kumar 	return opp->level;
479c2ab2cb6SViresh Kumar }
480c2ab2cb6SViresh Kumar 
481add1dc09SViresh Kumar static unsigned long _read_bw(struct dev_pm_opp *opp, int index)
482add1dc09SViresh Kumar {
483add1dc09SViresh Kumar 	return opp->bandwidth[index].peak;
484add1dc09SViresh Kumar }
485add1dc09SViresh Kumar 
486aab8ced2SViresh Kumar /* Generic comparison helpers */
487aab8ced2SViresh Kumar static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
488aab8ced2SViresh Kumar 			   unsigned long opp_key, unsigned long key)
489aab8ced2SViresh Kumar {
490aab8ced2SViresh Kumar 	if (opp_key == key) {
491aab8ced2SViresh Kumar 		*opp = temp_opp;
492aab8ced2SViresh Kumar 		return true;
493aab8ced2SViresh Kumar 	}
494aab8ced2SViresh Kumar 
495aab8ced2SViresh Kumar 	return false;
496aab8ced2SViresh Kumar }
497aab8ced2SViresh Kumar 
498aab8ced2SViresh Kumar static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
499aab8ced2SViresh Kumar 			  unsigned long opp_key, unsigned long key)
500aab8ced2SViresh Kumar {
501aab8ced2SViresh Kumar 	if (opp_key >= key) {
502aab8ced2SViresh Kumar 		*opp = temp_opp;
503aab8ced2SViresh Kumar 		return true;
504aab8ced2SViresh Kumar 	}
505aab8ced2SViresh Kumar 
506aab8ced2SViresh Kumar 	return false;
507aab8ced2SViresh Kumar }
508aab8ced2SViresh Kumar 
509aab8ced2SViresh Kumar static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
510aab8ced2SViresh Kumar 			   unsigned long opp_key, unsigned long key)
511aab8ced2SViresh Kumar {
512aab8ced2SViresh Kumar 	if (opp_key > key)
513aab8ced2SViresh Kumar 		return true;
514aab8ced2SViresh Kumar 
515aab8ced2SViresh Kumar 	*opp = temp_opp;
516aab8ced2SViresh Kumar 	return false;
517aab8ced2SViresh Kumar }
518aab8ced2SViresh Kumar 
519aab8ced2SViresh Kumar /* Generic key finding helpers */
520aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
521aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
522aab8ced2SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
523aab8ced2SViresh Kumar 		bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
524e10a4644SViresh Kumar 				unsigned long opp_key, unsigned long key),
525e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
526aab8ced2SViresh Kumar {
527aab8ced2SViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
528aab8ced2SViresh Kumar 
529e10a4644SViresh Kumar 	/* Assert that the requirement is met */
530e10a4644SViresh Kumar 	if (assert && !assert(opp_table))
531e10a4644SViresh Kumar 		return ERR_PTR(-EINVAL);
532e10a4644SViresh Kumar 
533aab8ced2SViresh Kumar 	mutex_lock(&opp_table->lock);
534aab8ced2SViresh Kumar 
535aab8ced2SViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
536aab8ced2SViresh Kumar 		if (temp_opp->available == available) {
537aab8ced2SViresh Kumar 			if (compare(&opp, temp_opp, read(temp_opp, index), *key))
538aab8ced2SViresh Kumar 				break;
539aab8ced2SViresh Kumar 		}
540aab8ced2SViresh Kumar 	}
541aab8ced2SViresh Kumar 
542aab8ced2SViresh Kumar 	/* Increment the reference count of OPP */
543aab8ced2SViresh Kumar 	if (!IS_ERR(opp)) {
544aab8ced2SViresh Kumar 		*key = read(opp, index);
545aab8ced2SViresh Kumar 		dev_pm_opp_get(opp);
546aab8ced2SViresh Kumar 	}
547aab8ced2SViresh Kumar 
548aab8ced2SViresh Kumar 	mutex_unlock(&opp_table->lock);
549aab8ced2SViresh Kumar 
550aab8ced2SViresh Kumar 	return opp;
551aab8ced2SViresh Kumar }
552aab8ced2SViresh Kumar 
553aab8ced2SViresh Kumar static struct dev_pm_opp *
554aab8ced2SViresh Kumar _find_key(struct device *dev, unsigned long *key, int index, bool available,
555aab8ced2SViresh Kumar 	  unsigned long (*read)(struct dev_pm_opp *opp, int index),
556aab8ced2SViresh Kumar 	  bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
557e10a4644SViresh Kumar 			  unsigned long opp_key, unsigned long key),
558e10a4644SViresh Kumar 	  bool (*assert)(struct opp_table *opp_table))
559aab8ced2SViresh Kumar {
560aab8ced2SViresh Kumar 	struct opp_table *opp_table;
561aab8ced2SViresh Kumar 	struct dev_pm_opp *opp;
562aab8ced2SViresh Kumar 
563aab8ced2SViresh Kumar 	opp_table = _find_opp_table(dev);
564aab8ced2SViresh Kumar 	if (IS_ERR(opp_table)) {
565aab8ced2SViresh Kumar 		dev_err(dev, "%s: OPP table not found (%ld)\n", __func__,
566aab8ced2SViresh Kumar 			PTR_ERR(opp_table));
567aab8ced2SViresh Kumar 		return ERR_CAST(opp_table);
568aab8ced2SViresh Kumar 	}
569aab8ced2SViresh Kumar 
570aab8ced2SViresh Kumar 	opp = _opp_table_find_key(opp_table, key, index, available, read,
571e10a4644SViresh Kumar 				  compare, assert);
572aab8ced2SViresh Kumar 
573aab8ced2SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
574aab8ced2SViresh Kumar 
575aab8ced2SViresh Kumar 	return opp;
576aab8ced2SViresh Kumar }
577aab8ced2SViresh Kumar 
578aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_exact(struct device *dev,
579aab8ced2SViresh Kumar 		unsigned long key, int index, bool available,
580e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
581e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
582aab8ced2SViresh Kumar {
583aab8ced2SViresh Kumar 	/*
584aab8ced2SViresh Kumar 	 * The value of key will be updated here, but will be ignored as the
585aab8ced2SViresh Kumar 	 * caller doesn't need it.
586aab8ced2SViresh Kumar 	 */
587e10a4644SViresh Kumar 	return _find_key(dev, &key, index, available, read, _compare_exact,
588e10a4644SViresh Kumar 			 assert);
589aab8ced2SViresh Kumar }
590aab8ced2SViresh Kumar 
591aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
592aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
593e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
594e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
595aab8ced2SViresh Kumar {
596aab8ced2SViresh Kumar 	return _opp_table_find_key(opp_table, key, index, available, read,
597e10a4644SViresh Kumar 				   _compare_ceil, assert);
598aab8ced2SViresh Kumar }
599aab8ced2SViresh Kumar 
600aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
601aab8ced2SViresh Kumar 		int index, bool available,
602e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
603e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
604aab8ced2SViresh Kumar {
605e10a4644SViresh Kumar 	return _find_key(dev, key, index, available, read, _compare_ceil,
606e10a4644SViresh Kumar 			 assert);
607aab8ced2SViresh Kumar }
608aab8ced2SViresh Kumar 
609aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_floor(struct device *dev,
610aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
611e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
612e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
613aab8ced2SViresh Kumar {
614e10a4644SViresh Kumar 	return _find_key(dev, key, index, available, read, _compare_floor,
615e10a4644SViresh Kumar 			 assert);
616aab8ced2SViresh Kumar }
617aab8ced2SViresh Kumar 
6187813dd6fSViresh Kumar /**
6197813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
6207813dd6fSViresh Kumar  * @dev:		device for which we do this operation
6217813dd6fSViresh Kumar  * @freq:		frequency to search for
6227813dd6fSViresh Kumar  * @available:		true/false - match for available opp
6237813dd6fSViresh Kumar  *
6247813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
6257813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
6267813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
6277813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6287813dd6fSViresh Kumar  * ERANGE:	no match found for search
6297813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6307813dd6fSViresh Kumar  *
6317813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
6327813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
6337813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
6347813dd6fSViresh Kumar  *
6357813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
6367813dd6fSViresh Kumar  * or the opposite as well.
6377813dd6fSViresh Kumar  *
6387813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6397813dd6fSViresh Kumar  * use.
6407813dd6fSViresh Kumar  */
6417813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
642aab8ced2SViresh Kumar 		unsigned long freq, bool available)
6437813dd6fSViresh Kumar {
644f123ea74SViresh Kumar 	return _find_key_exact(dev, freq, 0, available, _read_freq,
645f123ea74SViresh Kumar 			       assert_single_clk);
6467813dd6fSViresh Kumar }
6477813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
6487813dd6fSViresh Kumar 
6497813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
6507813dd6fSViresh Kumar 						   unsigned long *freq)
6517813dd6fSViresh Kumar {
652e10a4644SViresh Kumar 	return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq,
653f123ea74SViresh Kumar 					assert_single_clk);
6547813dd6fSViresh Kumar }
6557813dd6fSViresh Kumar 
6567813dd6fSViresh Kumar /**
6577813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
6587813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6597813dd6fSViresh Kumar  * @freq:	Start frequency
6607813dd6fSViresh Kumar  *
6617813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
6627813dd6fSViresh Kumar  * for a device.
6637813dd6fSViresh Kumar  *
6647813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6657813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6667813dd6fSViresh Kumar  * values can be:
6677813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6687813dd6fSViresh Kumar  * ERANGE:	no match found for search
6697813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6707813dd6fSViresh Kumar  *
6717813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6727813dd6fSViresh Kumar  * use.
6737813dd6fSViresh Kumar  */
6747813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
6757813dd6fSViresh Kumar 					     unsigned long *freq)
6767813dd6fSViresh Kumar {
677f123ea74SViresh Kumar 	return _find_key_ceil(dev, freq, 0, true, _read_freq, assert_single_clk);
6787813dd6fSViresh Kumar }
6797813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
6807813dd6fSViresh Kumar 
6817813dd6fSViresh Kumar /**
682142e17c1SManivannan Sadhasivam  * dev_pm_opp_find_freq_ceil_indexed() - Search for a rounded ceil freq for the
683142e17c1SManivannan Sadhasivam  *					 clock corresponding to the index
684142e17c1SManivannan Sadhasivam  * @dev:	Device for which we do this operation
685142e17c1SManivannan Sadhasivam  * @freq:	Start frequency
686142e17c1SManivannan Sadhasivam  * @index:	Clock index
687142e17c1SManivannan Sadhasivam  *
688142e17c1SManivannan Sadhasivam  * Search for the matching ceil *available* OPP for the clock corresponding to
689142e17c1SManivannan Sadhasivam  * the specified index from a starting freq for a device.
690142e17c1SManivannan Sadhasivam  *
691142e17c1SManivannan Sadhasivam  * Return: matching *opp and refreshes *freq accordingly, else returns
692142e17c1SManivannan Sadhasivam  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
693142e17c1SManivannan Sadhasivam  * values can be:
694142e17c1SManivannan Sadhasivam  * EINVAL:	for bad pointer
695142e17c1SManivannan Sadhasivam  * ERANGE:	no match found for search
696142e17c1SManivannan Sadhasivam  * ENODEV:	if device not found in list of registered devices
697142e17c1SManivannan Sadhasivam  *
698142e17c1SManivannan Sadhasivam  * The callers are required to call dev_pm_opp_put() for the returned OPP after
699142e17c1SManivannan Sadhasivam  * use.
700142e17c1SManivannan Sadhasivam  */
701142e17c1SManivannan Sadhasivam struct dev_pm_opp *
702142e17c1SManivannan Sadhasivam dev_pm_opp_find_freq_ceil_indexed(struct device *dev, unsigned long *freq,
703142e17c1SManivannan Sadhasivam 				  u32 index)
704142e17c1SManivannan Sadhasivam {
705142e17c1SManivannan Sadhasivam 	return _find_key_ceil(dev, freq, index, true, _read_freq, NULL);
706142e17c1SManivannan Sadhasivam }
707142e17c1SManivannan Sadhasivam EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_indexed);
708142e17c1SManivannan Sadhasivam 
709142e17c1SManivannan Sadhasivam /**
7107813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
7117813dd6fSViresh Kumar  * @dev:	device for which we do this operation
7127813dd6fSViresh Kumar  * @freq:	Start frequency
7137813dd6fSViresh Kumar  *
7147813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
7157813dd6fSViresh Kumar  * for a device.
7167813dd6fSViresh Kumar  *
7177813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
7187813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
7197813dd6fSViresh Kumar  * values can be:
7207813dd6fSViresh Kumar  * EINVAL:	for bad pointer
7217813dd6fSViresh Kumar  * ERANGE:	no match found for search
7227813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
7237813dd6fSViresh Kumar  *
7247813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
7257813dd6fSViresh Kumar  * use.
7267813dd6fSViresh Kumar  */
7277813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
7287813dd6fSViresh Kumar 					      unsigned long *freq)
7297813dd6fSViresh Kumar {
730f123ea74SViresh Kumar 	return _find_key_floor(dev, freq, 0, true, _read_freq, assert_single_clk);
7317813dd6fSViresh Kumar }
7327813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
7337813dd6fSViresh Kumar 
7342f36bde0SAndrew-sh.Cheng /**
735142e17c1SManivannan Sadhasivam  * dev_pm_opp_find_freq_floor_indexed() - Search for a rounded floor freq for the
736142e17c1SManivannan Sadhasivam  *					  clock corresponding to the index
737142e17c1SManivannan Sadhasivam  * @dev:	Device for which we do this operation
738142e17c1SManivannan Sadhasivam  * @freq:	Start frequency
739142e17c1SManivannan Sadhasivam  * @index:	Clock index
740142e17c1SManivannan Sadhasivam  *
741142e17c1SManivannan Sadhasivam  * Search for the matching floor *available* OPP for the clock corresponding to
742142e17c1SManivannan Sadhasivam  * the specified index from a starting freq for a device.
743142e17c1SManivannan Sadhasivam  *
744142e17c1SManivannan Sadhasivam  * Return: matching *opp and refreshes *freq accordingly, else returns
745142e17c1SManivannan Sadhasivam  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
746142e17c1SManivannan Sadhasivam  * values can be:
747142e17c1SManivannan Sadhasivam  * EINVAL:	for bad pointer
748142e17c1SManivannan Sadhasivam  * ERANGE:	no match found for search
749142e17c1SManivannan Sadhasivam  * ENODEV:	if device not found in list of registered devices
750142e17c1SManivannan Sadhasivam  *
751142e17c1SManivannan Sadhasivam  * The callers are required to call dev_pm_opp_put() for the returned OPP after
752142e17c1SManivannan Sadhasivam  * use.
753142e17c1SManivannan Sadhasivam  */
754142e17c1SManivannan Sadhasivam struct dev_pm_opp *
755142e17c1SManivannan Sadhasivam dev_pm_opp_find_freq_floor_indexed(struct device *dev, unsigned long *freq,
756142e17c1SManivannan Sadhasivam 				   u32 index)
757142e17c1SManivannan Sadhasivam {
758142e17c1SManivannan Sadhasivam 	return _find_key_floor(dev, freq, index, true, _read_freq, NULL);
759142e17c1SManivannan Sadhasivam }
760142e17c1SManivannan Sadhasivam EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor_indexed);
761142e17c1SManivannan Sadhasivam 
762142e17c1SManivannan Sadhasivam /**
76322079af7SViresh Kumar  * dev_pm_opp_find_level_exact() - search for an exact level
76422079af7SViresh Kumar  * @dev:		device for which we do this operation
76522079af7SViresh Kumar  * @level:		level to search for
76622079af7SViresh Kumar  *
76722079af7SViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
76822079af7SViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
76922079af7SViresh Kumar  * be handled using IS_ERR. Error return values can be:
77022079af7SViresh Kumar  * EINVAL:	for bad pointer
77122079af7SViresh Kumar  * ERANGE:	no match found for search
77222079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
77322079af7SViresh Kumar  *
77422079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
77522079af7SViresh Kumar  * use.
77622079af7SViresh Kumar  */
77722079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
77822079af7SViresh Kumar 					       unsigned int level)
77922079af7SViresh Kumar {
780e10a4644SViresh Kumar 	return _find_key_exact(dev, level, 0, true, _read_level, NULL);
78122079af7SViresh Kumar }
78222079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
78322079af7SViresh Kumar 
78422079af7SViresh Kumar /**
78522079af7SViresh Kumar  * dev_pm_opp_find_level_ceil() - search for an rounded up level
78622079af7SViresh Kumar  * @dev:		device for which we do this operation
78722079af7SViresh Kumar  * @level:		level to search for
78822079af7SViresh Kumar  *
78922079af7SViresh Kumar  * Return: Searches for rounded up match in the opp table and returns pointer
79022079af7SViresh Kumar  * to the  matching opp if found, else returns ERR_PTR in case of error and
79122079af7SViresh Kumar  * should be handled using IS_ERR. Error return values can be:
79222079af7SViresh Kumar  * EINVAL:	for bad pointer
79322079af7SViresh Kumar  * ERANGE:	no match found for search
79422079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
79522079af7SViresh Kumar  *
79622079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
79722079af7SViresh Kumar  * use.
79822079af7SViresh Kumar  */
79922079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
80022079af7SViresh Kumar 					      unsigned int *level)
80122079af7SViresh Kumar {
802c2ab2cb6SViresh Kumar 	unsigned long temp = *level;
803c2ab2cb6SViresh Kumar 	struct dev_pm_opp *opp;
80422079af7SViresh Kumar 
805e10a4644SViresh Kumar 	opp = _find_key_ceil(dev, &temp, 0, true, _read_level, NULL);
806c2ab2cb6SViresh Kumar 	*level = temp;
80722079af7SViresh Kumar 	return opp;
80822079af7SViresh Kumar }
80922079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
81022079af7SViresh Kumar 
81122079af7SViresh Kumar /**
81200ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
81300ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
814617df304SYang Li  * @bw:	start bandwidth
81500ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
81600ce3873SKrzysztof Kozlowski  *
81700ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
81800ce3873SKrzysztof Kozlowski  * for a device.
81900ce3873SKrzysztof Kozlowski  *
82000ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
82100ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
82200ce3873SKrzysztof Kozlowski  * values can be:
82300ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
82400ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
82500ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
82600ce3873SKrzysztof Kozlowski  *
82700ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
82800ce3873SKrzysztof Kozlowski  * use.
82900ce3873SKrzysztof Kozlowski  */
830add1dc09SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
831add1dc09SViresh Kumar 					   int index)
83200ce3873SKrzysztof Kozlowski {
833add1dc09SViresh Kumar 	unsigned long temp = *bw;
834add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
83500ce3873SKrzysztof Kozlowski 
836e10a4644SViresh Kumar 	opp = _find_key_ceil(dev, &temp, index, true, _read_bw, NULL);
837add1dc09SViresh Kumar 	*bw = temp;
83800ce3873SKrzysztof Kozlowski 	return opp;
83900ce3873SKrzysztof Kozlowski }
84000ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
84100ce3873SKrzysztof Kozlowski 
84200ce3873SKrzysztof Kozlowski /**
84300ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
84400ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
845617df304SYang Li  * @bw:	start bandwidth
84600ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
84700ce3873SKrzysztof Kozlowski  *
84800ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
84900ce3873SKrzysztof Kozlowski  * for a device.
85000ce3873SKrzysztof Kozlowski  *
85100ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
85200ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
85300ce3873SKrzysztof Kozlowski  * values can be:
85400ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
85500ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
85600ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
85700ce3873SKrzysztof Kozlowski  *
85800ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
85900ce3873SKrzysztof Kozlowski  * use.
86000ce3873SKrzysztof Kozlowski  */
86100ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
86200ce3873SKrzysztof Kozlowski 					    unsigned int *bw, int index)
86300ce3873SKrzysztof Kozlowski {
864add1dc09SViresh Kumar 	unsigned long temp = *bw;
865add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
86600ce3873SKrzysztof Kozlowski 
867e10a4644SViresh Kumar 	opp = _find_key_floor(dev, &temp, index, true, _read_bw, NULL);
868add1dc09SViresh Kumar 	*bw = temp;
86900ce3873SKrzysztof Kozlowski 	return opp;
87000ce3873SKrzysztof Kozlowski }
87100ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
87200ce3873SKrzysztof Kozlowski 
8737813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
8747813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
8757813dd6fSViresh Kumar {
8767813dd6fSViresh Kumar 	int ret;
8777813dd6fSViresh Kumar 
8787813dd6fSViresh Kumar 	/* Regulator not available for device */
8797813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
8807813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
8817813dd6fSViresh Kumar 			PTR_ERR(reg));
8827813dd6fSViresh Kumar 		return 0;
8837813dd6fSViresh Kumar 	}
8847813dd6fSViresh Kumar 
8857813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
8867813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
8877813dd6fSViresh Kumar 
8887813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
8897813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
8907813dd6fSViresh Kumar 	if (ret)
8917813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
8927813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
8937813dd6fSViresh Kumar 			supply->u_volt_max, ret);
8947813dd6fSViresh Kumar 
8957813dd6fSViresh Kumar 	return ret;
8967813dd6fSViresh Kumar }
8977813dd6fSViresh Kumar 
8982083da24SViresh Kumar static int
8992083da24SViresh Kumar _opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
9002083da24SViresh Kumar 		       struct dev_pm_opp *opp, void *data, bool scaling_down)
9017813dd6fSViresh Kumar {
9021efae8d2SViresh Kumar 	unsigned long *target = data;
9031efae8d2SViresh Kumar 	unsigned long freq;
9047813dd6fSViresh Kumar 	int ret;
9057813dd6fSViresh Kumar 
9061efae8d2SViresh Kumar 	/* One of target and opp must be available */
9071efae8d2SViresh Kumar 	if (target) {
9081efae8d2SViresh Kumar 		freq = *target;
9091efae8d2SViresh Kumar 	} else if (opp) {
9102083da24SViresh Kumar 		freq = opp->rates[0];
9111efae8d2SViresh Kumar 	} else {
9121efae8d2SViresh Kumar 		WARN_ON(1);
9131efae8d2SViresh Kumar 		return -EINVAL;
9141efae8d2SViresh Kumar 	}
9151efae8d2SViresh Kumar 
9161efae8d2SViresh Kumar 	ret = clk_set_rate(opp_table->clk, freq);
9177813dd6fSViresh Kumar 	if (ret) {
9187813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
9197813dd6fSViresh Kumar 			ret);
9201efae8d2SViresh Kumar 	} else {
9211efae8d2SViresh Kumar 		opp_table->rate_clk_single = freq;
9227813dd6fSViresh Kumar 	}
9237813dd6fSViresh Kumar 
9247813dd6fSViresh Kumar 	return ret;
9257813dd6fSViresh Kumar }
9267813dd6fSViresh Kumar 
9278174a3a6SViresh Kumar /*
9288174a3a6SViresh Kumar  * Simple implementation for configuring multiple clocks. Configure clocks in
9298174a3a6SViresh Kumar  * the order in which they are present in the array while scaling up.
9308174a3a6SViresh Kumar  */
9318174a3a6SViresh Kumar int dev_pm_opp_config_clks_simple(struct device *dev,
9328174a3a6SViresh Kumar 		struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
9338174a3a6SViresh Kumar 		bool scaling_down)
9348174a3a6SViresh Kumar {
9358174a3a6SViresh Kumar 	int ret, i;
9368174a3a6SViresh Kumar 
9378174a3a6SViresh Kumar 	if (scaling_down) {
9388174a3a6SViresh Kumar 		for (i = opp_table->clk_count - 1; i >= 0; i--) {
9398174a3a6SViresh Kumar 			ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
9408174a3a6SViresh Kumar 			if (ret) {
9418174a3a6SViresh Kumar 				dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
9428174a3a6SViresh Kumar 					ret);
9438174a3a6SViresh Kumar 				return ret;
9448174a3a6SViresh Kumar 			}
9458174a3a6SViresh Kumar 		}
9468174a3a6SViresh Kumar 	} else {
9478174a3a6SViresh Kumar 		for (i = 0; i < opp_table->clk_count; i++) {
9488174a3a6SViresh Kumar 			ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
9498174a3a6SViresh Kumar 			if (ret) {
9508174a3a6SViresh Kumar 				dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
9518174a3a6SViresh Kumar 					ret);
9528174a3a6SViresh Kumar 				return ret;
9538174a3a6SViresh Kumar 			}
9548174a3a6SViresh Kumar 		}
9558174a3a6SViresh Kumar 	}
9568174a3a6SViresh Kumar 
957d36cb843SChristophe JAILLET 	return 0;
9588174a3a6SViresh Kumar }
9598174a3a6SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_config_clks_simple);
9608174a3a6SViresh Kumar 
961c522ce8aSViresh Kumar static int _opp_config_regulator_single(struct device *dev,
962c522ce8aSViresh Kumar 			struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
963c522ce8aSViresh Kumar 			struct regulator **regulators, unsigned int count)
9647813dd6fSViresh Kumar {
965c522ce8aSViresh Kumar 	struct regulator *reg = regulators[0];
9667813dd6fSViresh Kumar 	int ret;
9677813dd6fSViresh Kumar 
9687813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
969c522ce8aSViresh Kumar 	if (WARN_ON(count > 1)) {
9707813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
9717813dd6fSViresh Kumar 		return -EINVAL;
9727813dd6fSViresh Kumar 	}
9737813dd6fSViresh Kumar 
974c522ce8aSViresh Kumar 	ret = _set_opp_voltage(dev, reg, new_opp->supplies);
9757813dd6fSViresh Kumar 	if (ret)
976c522ce8aSViresh Kumar 		return ret;
9777813dd6fSViresh Kumar 
9788d45719cSKamil Konieczny 	/*
9798d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
9808d45719cSKamil Konieczny 	 * some boot-enabled regulators.
9818d45719cSKamil Konieczny 	 */
982c522ce8aSViresh Kumar 	if (unlikely(!new_opp->opp_table->enabled)) {
9838d45719cSKamil Konieczny 		ret = regulator_enable(reg);
9848d45719cSKamil Konieczny 		if (ret < 0)
9858d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
9868d45719cSKamil Konieczny 	}
9878d45719cSKamil Konieczny 
9887813dd6fSViresh Kumar 	return 0;
9897813dd6fSViresh Kumar }
9907813dd6fSViresh Kumar 
991b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
992240ae50eSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev)
993b00e667aSViresh Kumar {
994b00e667aSViresh Kumar 	u32 avg, peak;
995b00e667aSViresh Kumar 	int i, ret;
996b00e667aSViresh Kumar 
997b00e667aSViresh Kumar 	if (!opp_table->paths)
998b00e667aSViresh Kumar 		return 0;
999b00e667aSViresh Kumar 
1000b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
1001240ae50eSViresh Kumar 		if (!opp) {
1002b00e667aSViresh Kumar 			avg = 0;
1003b00e667aSViresh Kumar 			peak = 0;
1004b00e667aSViresh Kumar 		} else {
1005b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
1006b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
1007b00e667aSViresh Kumar 		}
1008b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
1009b00e667aSViresh Kumar 		if (ret) {
1010b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
1011240ae50eSViresh Kumar 				opp ? "set" : "remove", i, ret);
1012b00e667aSViresh Kumar 			return ret;
1013b00e667aSViresh Kumar 		}
1014b00e667aSViresh Kumar 	}
1015b00e667aSViresh Kumar 
1016b00e667aSViresh Kumar 	return 0;
1017b00e667aSViresh Kumar }
1018b00e667aSViresh Kumar 
1019528f2d8dSViresh Kumar static int _set_performance_state(struct device *dev, struct device *pd_dev,
102060cdeae0SStephan Gerhold 				  struct dev_pm_opp *opp, int i)
102160cdeae0SStephan Gerhold {
10227c41cdcdSViresh Kumar 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->level: 0;
102360cdeae0SStephan Gerhold 	int ret;
102460cdeae0SStephan Gerhold 
102560cdeae0SStephan Gerhold 	if (!pd_dev)
102660cdeae0SStephan Gerhold 		return 0;
102760cdeae0SStephan Gerhold 
102860cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
102960cdeae0SStephan Gerhold 	if (ret) {
10309bfb1fffSViresh Kumar 		dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
103160cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
103260cdeae0SStephan Gerhold 	}
103360cdeae0SStephan Gerhold 
103460cdeae0SStephan Gerhold 	return ret;
103560cdeae0SStephan Gerhold }
103660cdeae0SStephan Gerhold 
1037528f2d8dSViresh Kumar static int _opp_set_required_opps_generic(struct device *dev,
1038528f2d8dSViresh Kumar 	struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
1039ca1b5d77SViresh Kumar {
1040528f2d8dSViresh Kumar 	dev_err(dev, "setting required-opps isn't supported for non-genpd devices\n");
1041528f2d8dSViresh Kumar 	return -ENOENT;
1042528f2d8dSViresh Kumar }
1043528f2d8dSViresh Kumar 
1044528f2d8dSViresh Kumar static int _opp_set_required_opps_genpd(struct device *dev,
1045528f2d8dSViresh Kumar 	struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
1046528f2d8dSViresh Kumar {
104729b1a92eSViresh Kumar 	struct device **genpd_virt_devs =
104829b1a92eSViresh Kumar 		opp_table->genpd_virt_devs ? opp_table->genpd_virt_devs : &dev;
1049ca1b5d77SViresh Kumar 	int i, ret = 0;
1050ca1b5d77SViresh Kumar 
1051ca1b5d77SViresh Kumar 	/*
1052ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
1053ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
1054ca1b5d77SViresh Kumar 	 */
1055ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
1056ca1b5d77SViresh Kumar 
10572c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
1058528f2d8dSViresh Kumar 	if (!scaling_down) {
1059ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
1060528f2d8dSViresh Kumar 			ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
106160cdeae0SStephan Gerhold 			if (ret)
1062ca1b5d77SViresh Kumar 				break;
1063ca1b5d77SViresh Kumar 		}
10642c59138cSStephan Gerhold 	} else {
10652c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
1066528f2d8dSViresh Kumar 			ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
10672c59138cSStephan Gerhold 			if (ret)
1068ca1b5d77SViresh Kumar 				break;
1069ca1b5d77SViresh Kumar 		}
1070ca1b5d77SViresh Kumar 	}
10712c59138cSStephan Gerhold 
1072ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
1073ca1b5d77SViresh Kumar 
1074ca1b5d77SViresh Kumar 	return ret;
1075ca1b5d77SViresh Kumar }
1076ca1b5d77SViresh Kumar 
1077528f2d8dSViresh Kumar /* This is only called for PM domain for now */
1078528f2d8dSViresh Kumar static int _set_required_opps(struct device *dev, struct opp_table *opp_table,
1079528f2d8dSViresh Kumar 			      struct dev_pm_opp *opp, bool up)
1080528f2d8dSViresh Kumar {
1081528f2d8dSViresh Kumar 	/* required-opps not fully initialized yet */
1082528f2d8dSViresh Kumar 	if (lazy_linking_pending(opp_table))
1083528f2d8dSViresh Kumar 		return -EBUSY;
1084528f2d8dSViresh Kumar 
1085528f2d8dSViresh Kumar 	if (opp_table->set_required_opps)
1086528f2d8dSViresh Kumar 		return opp_table->set_required_opps(dev, opp_table, opp, up);
1087528f2d8dSViresh Kumar 
1088528f2d8dSViresh Kumar 	return 0;
1089528f2d8dSViresh Kumar }
1090528f2d8dSViresh Kumar 
1091528f2d8dSViresh Kumar /* Update set_required_opps handler */
1092528f2d8dSViresh Kumar void _update_set_required_opps(struct opp_table *opp_table)
1093528f2d8dSViresh Kumar {
1094528f2d8dSViresh Kumar 	/* Already set */
1095528f2d8dSViresh Kumar 	if (opp_table->set_required_opps)
1096528f2d8dSViresh Kumar 		return;
1097528f2d8dSViresh Kumar 
1098528f2d8dSViresh Kumar 	/* All required OPPs will belong to genpd or none */
1099528f2d8dSViresh Kumar 	if (opp_table->required_opp_tables[0]->is_genpd)
1100528f2d8dSViresh Kumar 		opp_table->set_required_opps = _opp_set_required_opps_genpd;
1101528f2d8dSViresh Kumar 	else
1102528f2d8dSViresh Kumar 		opp_table->set_required_opps = _opp_set_required_opps_generic;
1103528f2d8dSViresh Kumar }
1104528f2d8dSViresh Kumar 
110581c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
110681c4d8a3SViresh Kumar {
110781c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
110881c4d8a3SViresh Kumar 	unsigned long freq;
110981c4d8a3SViresh Kumar 
111081c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
111181c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
111281c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
111381c4d8a3SViresh Kumar 	}
111481c4d8a3SViresh Kumar 
111581c4d8a3SViresh Kumar 	/*
111681c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
111781c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
111881c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
111981c4d8a3SViresh Kumar 	 */
112081c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
112181c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
112281c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
112381c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
112481c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
112581c4d8a3SViresh Kumar 	}
112681c4d8a3SViresh Kumar 
112781c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
112881c4d8a3SViresh Kumar }
112981c4d8a3SViresh Kumar 
11305ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
1131f3364e17SViresh Kumar {
1132f3364e17SViresh Kumar 	int ret;
1133f3364e17SViresh Kumar 
1134f3364e17SViresh Kumar 	if (!opp_table->enabled)
1135f3364e17SViresh Kumar 		return 0;
1136f3364e17SViresh Kumar 
1137f3364e17SViresh Kumar 	/*
1138f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
1139f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
1140f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
1141f3364e17SViresh Kumar 	 */
1142f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
1143f3364e17SViresh Kumar 		return 0;
1144f3364e17SViresh Kumar 
1145240ae50eSViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev);
1146f3364e17SViresh Kumar 	if (ret)
1147f3364e17SViresh Kumar 		return ret;
1148f3364e17SViresh Kumar 
1149f3364e17SViresh Kumar 	if (opp_table->regulators)
1150f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
1151f3364e17SViresh Kumar 
11522c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
1153f3364e17SViresh Kumar 
1154f3364e17SViresh Kumar 	opp_table->enabled = false;
1155f3364e17SViresh Kumar 	return ret;
1156f3364e17SViresh Kumar }
1157f3364e17SViresh Kumar 
1158386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
11591efae8d2SViresh Kumar 		    struct dev_pm_opp *opp, void *clk_data, bool forced)
11607813dd6fSViresh Kumar {
1161386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
1162f0b88fa4SViresh Kumar 	int scaling_down, ret;
11637813dd6fSViresh Kumar 
1164386ba854SViresh Kumar 	if (unlikely(!opp))
1165386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
1166aca48b61SRajendra Nayak 
116781c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
116881c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
116981c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
11707813dd6fSViresh Kumar 
117181c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
117281c4d8a3SViresh Kumar 
117381c4d8a3SViresh Kumar 	/* Return early if nothing to do */
11741efae8d2SViresh Kumar 	if (!forced && old_opp == opp && opp_table->enabled) {
11759e28f7a7SAdrián Larumbe 		dev_dbg_ratelimited(dev, "%s: OPPs are same, nothing to do\n", __func__);
1176386ba854SViresh Kumar 		return 0;
11777813dd6fSViresh Kumar 	}
11787813dd6fSViresh Kumar 
1179f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
11802083da24SViresh Kumar 		__func__, old_opp->rates[0], opp->rates[0], old_opp->level,
11812083da24SViresh Kumar 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1182f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1183f0b88fa4SViresh Kumar 
11842083da24SViresh Kumar 	scaling_down = _opp_compare_key(opp_table, old_opp, opp);
1185f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1186f0b88fa4SViresh Kumar 		scaling_down = 0;
11877813dd6fSViresh Kumar 
1188ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1189f0b88fa4SViresh Kumar 	if (!scaling_down) {
11902c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1191870d5d96SViresh Kumar 		if (ret) {
1192870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1193386ba854SViresh Kumar 			return ret;
1194ca1b5d77SViresh Kumar 		}
1195ca1b5d77SViresh Kumar 
1196870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1197870d5d96SViresh Kumar 		if (ret) {
1198870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1199870d5d96SViresh Kumar 			return ret;
1200870d5d96SViresh Kumar 		}
1201aee3352fSViresh Kumar 
1202aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1203aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1204aee3352fSViresh Kumar 							   opp_table->regulators,
1205aee3352fSViresh Kumar 							   opp_table->regulator_count);
1206aee3352fSViresh Kumar 			if (ret) {
1207aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1208aee3352fSViresh Kumar 					ret);
1209aee3352fSViresh Kumar 				return ret;
1210aee3352fSViresh Kumar 			}
1211aee3352fSViresh Kumar 		}
1212870d5d96SViresh Kumar 	}
1213870d5d96SViresh Kumar 
12142083da24SViresh Kumar 	if (opp_table->config_clks) {
12152083da24SViresh Kumar 		ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
1216ca1b5d77SViresh Kumar 		if (ret)
1217870d5d96SViresh Kumar 			return ret;
12182083da24SViresh Kumar 	}
1219870d5d96SViresh Kumar 
1220870d5d96SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1221870d5d96SViresh Kumar 	if (scaling_down) {
1222aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1223aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1224aee3352fSViresh Kumar 							   opp_table->regulators,
1225aee3352fSViresh Kumar 							   opp_table->regulator_count);
1226aee3352fSViresh Kumar 			if (ret) {
1227aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1228aee3352fSViresh Kumar 					ret);
1229aee3352fSViresh Kumar 				return ret;
1230aee3352fSViresh Kumar 			}
1231aee3352fSViresh Kumar 		}
1232aee3352fSViresh Kumar 
1233870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1234870d5d96SViresh Kumar 		if (ret) {
1235870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1236870d5d96SViresh Kumar 			return ret;
1237ca1b5d77SViresh Kumar 		}
1238ca1b5d77SViresh Kumar 
1239870d5d96SViresh Kumar 		ret = _set_required_opps(dev, opp_table, opp, false);
1240870d5d96SViresh Kumar 		if (ret) {
1241870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1242870d5d96SViresh Kumar 			return ret;
1243870d5d96SViresh Kumar 		}
1244870d5d96SViresh Kumar 	}
1245870d5d96SViresh Kumar 
124672f80ce4SViresh Kumar 	opp_table->enabled = true;
124781c4d8a3SViresh Kumar 	dev_pm_opp_put(old_opp);
124881c4d8a3SViresh Kumar 
124981c4d8a3SViresh Kumar 	/* Make sure current_opp doesn't get freed */
125081c4d8a3SViresh Kumar 	dev_pm_opp_get(opp);
125181c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
1252fe2af402SGeorgi Djakov 
1253386ba854SViresh Kumar 	return ret;
1254386ba854SViresh Kumar }
1255386ba854SViresh Kumar 
1256386ba854SViresh Kumar /**
1257386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1258386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1259386ba854SViresh Kumar  * @target_freq: frequency to achieve
1260386ba854SViresh Kumar  *
1261386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1262386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1263386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1264386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1265386ba854SViresh Kumar  * frequency.
1266386ba854SViresh Kumar  */
1267386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1268386ba854SViresh Kumar {
1269386ba854SViresh Kumar 	struct opp_table *opp_table;
1270386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1271386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
12721efae8d2SViresh Kumar 	bool forced = false;
1273386ba854SViresh Kumar 	int ret;
1274386ba854SViresh Kumar 
1275386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1276386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1277386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1278386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1279386ba854SViresh Kumar 	}
1280386ba854SViresh Kumar 
1281386ba854SViresh Kumar 	if (target_freq) {
1282386ba854SViresh Kumar 		/*
1283386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1284386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1285386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1286386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1287386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1288386ba854SViresh Kumar 		 */
1289386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
12902083da24SViresh Kumar 			ret = opp_table->config_clks(dev, opp_table, NULL,
12912083da24SViresh Kumar 						     &target_freq, false);
1292386ba854SViresh Kumar 			goto put_opp_table;
1293386ba854SViresh Kumar 		}
1294386ba854SViresh Kumar 
1295386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1296386ba854SViresh Kumar 		if ((long)freq <= 0)
1297386ba854SViresh Kumar 			freq = target_freq;
1298386ba854SViresh Kumar 
1299386ba854SViresh Kumar 		/*
1300386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1301386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1302386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1303386ba854SViresh Kumar 		 */
1304386ba854SViresh Kumar 		temp_freq = freq;
1305386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1306386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1307386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1308386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1309386ba854SViresh Kumar 				__func__, freq, ret);
1310386ba854SViresh Kumar 			goto put_opp_table;
1311386ba854SViresh Kumar 		}
13121efae8d2SViresh Kumar 
13131efae8d2SViresh Kumar 		/*
13141efae8d2SViresh Kumar 		 * An OPP entry specifies the highest frequency at which other
13151efae8d2SViresh Kumar 		 * properties of the OPP entry apply. Even if the new OPP is
13161efae8d2SViresh Kumar 		 * same as the old one, we may still reach here for a different
13171efae8d2SViresh Kumar 		 * value of the frequency. In such a case, do not abort but
13181efae8d2SViresh Kumar 		 * configure the hardware to the desired frequency forcefully.
13191efae8d2SViresh Kumar 		 */
13201efae8d2SViresh Kumar 		forced = opp_table->rate_clk_single != target_freq;
1321386ba854SViresh Kumar 	}
1322386ba854SViresh Kumar 
13231efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, &target_freq, forced);
1324386ba854SViresh Kumar 
1325386ba854SViresh Kumar 	if (target_freq)
13267813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
13271efae8d2SViresh Kumar 
13287813dd6fSViresh Kumar put_opp_table:
13297813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
13307813dd6fSViresh Kumar 	return ret;
13317813dd6fSViresh Kumar }
13327813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
13337813dd6fSViresh Kumar 
1334abbe3483SViresh Kumar /**
1335abbe3483SViresh Kumar  * dev_pm_opp_set_opp() - Configure device for OPP
1336abbe3483SViresh Kumar  * @dev: device for which we do this operation
1337abbe3483SViresh Kumar  * @opp: OPP to set to
1338abbe3483SViresh Kumar  *
1339abbe3483SViresh Kumar  * This configures the device based on the properties of the OPP passed to this
1340abbe3483SViresh Kumar  * routine.
1341abbe3483SViresh Kumar  *
1342abbe3483SViresh Kumar  * Return: 0 on success, a negative error number otherwise.
1343abbe3483SViresh Kumar  */
1344abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1345abbe3483SViresh Kumar {
1346abbe3483SViresh Kumar 	struct opp_table *opp_table;
1347abbe3483SViresh Kumar 	int ret;
1348abbe3483SViresh Kumar 
1349abbe3483SViresh Kumar 	opp_table = _find_opp_table(dev);
1350abbe3483SViresh Kumar 	if (IS_ERR(opp_table)) {
1351abbe3483SViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1352abbe3483SViresh Kumar 		return PTR_ERR(opp_table);
1353abbe3483SViresh Kumar 	}
1354abbe3483SViresh Kumar 
13551efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, NULL, false);
1356abbe3483SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1357abbe3483SViresh Kumar 
1358abbe3483SViresh Kumar 	return ret;
1359abbe3483SViresh Kumar }
1360abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1361abbe3483SViresh Kumar 
13627813dd6fSViresh Kumar /* OPP-dev Helpers */
13637813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
13647813dd6fSViresh Kumar 			    struct opp_table *opp_table)
13657813dd6fSViresh Kumar {
13667813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
13677813dd6fSViresh Kumar 	list_del(&opp_dev->node);
13687813dd6fSViresh Kumar 	kfree(opp_dev);
13697813dd6fSViresh Kumar }
13707813dd6fSViresh Kumar 
1371ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
13727813dd6fSViresh Kumar 				struct opp_table *opp_table)
13737813dd6fSViresh Kumar {
13747813dd6fSViresh Kumar 	struct opp_device *opp_dev;
13757813dd6fSViresh Kumar 
13767813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
13777813dd6fSViresh Kumar 	if (!opp_dev)
13787813dd6fSViresh Kumar 		return NULL;
13797813dd6fSViresh Kumar 
13807813dd6fSViresh Kumar 	/* Initialize opp-dev */
13817813dd6fSViresh Kumar 	opp_dev->dev = dev;
13823d255699SViresh Kumar 
1383ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
13847813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1385ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
13867813dd6fSViresh Kumar 
13877813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1388a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1389283d55e6SViresh Kumar 
1390283d55e6SViresh Kumar 	return opp_dev;
1391283d55e6SViresh Kumar }
1392283d55e6SViresh Kumar 
1393eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
13947813dd6fSViresh Kumar {
13957813dd6fSViresh Kumar 	struct opp_table *opp_table;
13967813dd6fSViresh Kumar 	struct opp_device *opp_dev;
13977813dd6fSViresh Kumar 	int ret;
13987813dd6fSViresh Kumar 
13997813dd6fSViresh Kumar 	/*
14007813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
14017813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
14027813dd6fSViresh Kumar 	 */
14037813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
14047813dd6fSViresh Kumar 	if (!opp_table)
1405dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
14067813dd6fSViresh Kumar 
14073d255699SViresh Kumar 	mutex_init(&opp_table->lock);
14084f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
14097813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
14107eba0c76SViresh Kumar 	INIT_LIST_HEAD(&opp_table->lazy);
14117813dd6fSViresh Kumar 
14122083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
14132083da24SViresh Kumar 
141446f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
141546f48acaSViresh Kumar 	opp_table->regulator_count = -1;
141646f48acaSViresh Kumar 
14177813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
14187813dd6fSViresh Kumar 	if (!opp_dev) {
1419dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1420dd461cd9SStephan Gerhold 		goto err;
14217813dd6fSViresh Kumar 	}
14227813dd6fSViresh Kumar 
1423eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
14247813dd6fSViresh Kumar 
14256d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
14266d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1427dd461cd9SStephan Gerhold 	if (ret) {
1428dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
142932439ac7SViresh Kumar 			goto remove_opp_dev;
1430dd461cd9SStephan Gerhold 
14316d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
14326d3f922cSGeorgi Djakov 			 __func__, ret);
1433dd461cd9SStephan Gerhold 	}
14346d3f922cSGeorgi Djakov 
14357813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
14367813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
14377813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
14387813dd6fSViresh Kumar 
14397813dd6fSViresh Kumar 	return opp_table;
1440dd461cd9SStephan Gerhold 
1441976509bbSQuanyang Wang remove_opp_dev:
1442b2a2ab03SStephan Gerhold 	_of_clear_opp_table(opp_table);
1443976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1444b2a2ab03SStephan Gerhold 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
1445b2a2ab03SStephan Gerhold 	mutex_destroy(&opp_table->lock);
1446dd461cd9SStephan Gerhold err:
1447dd461cd9SStephan Gerhold 	kfree(opp_table);
1448dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
14497813dd6fSViresh Kumar }
14507813dd6fSViresh Kumar 
14517813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
14527813dd6fSViresh Kumar {
14537813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
14547813dd6fSViresh Kumar }
14557813dd6fSViresh Kumar 
145632439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
145732439ac7SViresh Kumar 					       struct opp_table *opp_table,
145832439ac7SViresh Kumar 					       bool getclk)
145932439ac7SViresh Kumar {
1460d4a4c7a4SViresh Kumar 	int ret;
1461d4a4c7a4SViresh Kumar 
146232439ac7SViresh Kumar 	/*
14632083da24SViresh Kumar 	 * Return early if we don't need to get clk or we have already done it
146432439ac7SViresh Kumar 	 * earlier.
146532439ac7SViresh Kumar 	 */
14662083da24SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
14672083da24SViresh Kumar 	    opp_table->clks)
146832439ac7SViresh Kumar 		return opp_table;
146932439ac7SViresh Kumar 
147032439ac7SViresh Kumar 	/* Find clk for the device */
147132439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
147232439ac7SViresh Kumar 
1473d4a4c7a4SViresh Kumar 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
14742083da24SViresh Kumar 	if (!ret) {
14752083da24SViresh Kumar 		opp_table->config_clks = _opp_config_clk_single;
14762083da24SViresh Kumar 		opp_table->clk_count = 1;
147732439ac7SViresh Kumar 		return opp_table;
14782083da24SViresh Kumar 	}
1479d4a4c7a4SViresh Kumar 
1480d4a4c7a4SViresh Kumar 	if (ret == -ENOENT) {
14812083da24SViresh Kumar 		/*
14822083da24SViresh Kumar 		 * There are few platforms which don't want the OPP core to
14832083da24SViresh Kumar 		 * manage device's clock settings. In such cases neither the
14842083da24SViresh Kumar 		 * platform provides the clks explicitly to us, nor the DT
14852083da24SViresh Kumar 		 * contains a valid clk entry. The OPP nodes in DT may still
14862083da24SViresh Kumar 		 * contain "opp-hz" property though, which we need to parse and
14872083da24SViresh Kumar 		 * allow the platform to find an OPP based on freq later on.
14882083da24SViresh Kumar 		 *
14892083da24SViresh Kumar 		 * This is a simple solution to take care of such corner cases,
14902083da24SViresh Kumar 		 * i.e. make the clk_count 1, which lets us allocate space for
14912083da24SViresh Kumar 		 * frequency in opp->rates and also parse the entries in DT.
14922083da24SViresh Kumar 		 */
14932083da24SViresh Kumar 		opp_table->clk_count = 1;
14942083da24SViresh Kumar 
1495d4a4c7a4SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1496d4a4c7a4SViresh Kumar 		return opp_table;
1497d4a4c7a4SViresh Kumar 	}
1498d4a4c7a4SViresh Kumar 
1499d4a4c7a4SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1500d4a4c7a4SViresh Kumar 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1501d4a4c7a4SViresh Kumar 
1502d4a4c7a4SViresh Kumar 	return ERR_PTR(ret);
150332439ac7SViresh Kumar }
150432439ac7SViresh Kumar 
150527c09484SViresh Kumar /*
150627c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
150727c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
150827c09484SViresh Kumar  *
150927c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
151027c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
151127c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
151227c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
151327c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
151427c09484SViresh Kumar  * indirect users of OPP core.
151527c09484SViresh Kumar  *
151627c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
151727c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
151827c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
151927c09484SViresh Kumar  */
152032439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
152132439ac7SViresh Kumar 					 bool getclk)
15227813dd6fSViresh Kumar {
15237813dd6fSViresh Kumar 	struct opp_table *opp_table;
15247813dd6fSViresh Kumar 
152527c09484SViresh Kumar again:
15267813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
15277813dd6fSViresh Kumar 
15287813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
15297813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
15307813dd6fSViresh Kumar 		goto unlock;
15317813dd6fSViresh Kumar 
153227c09484SViresh Kumar 	/*
153327c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
153427c09484SViresh Kumar 	 * another user, wait for it to finish.
153527c09484SViresh Kumar 	 */
153627c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
153727c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
153827c09484SViresh Kumar 		cpu_relax();
153927c09484SViresh Kumar 		goto again;
154027c09484SViresh Kumar 	}
154127c09484SViresh Kumar 
154227c09484SViresh Kumar 	opp_tables_busy = true;
1543283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
154427c09484SViresh Kumar 
154527c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
154627c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
154727c09484SViresh Kumar 
1548283d55e6SViresh Kumar 	if (opp_table) {
1549ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1550283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1551dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1552283d55e6SViresh Kumar 		}
155327c09484SViresh Kumar 
155427c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
155527c09484SViresh Kumar 	} else {
155627c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
155727c09484SViresh Kumar 
155827c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
155927c09484SViresh Kumar 		if (!IS_ERR(opp_table))
156027c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1561283d55e6SViresh Kumar 	}
1562283d55e6SViresh Kumar 
156327c09484SViresh Kumar 	opp_tables_busy = false;
15647813dd6fSViresh Kumar 
15657813dd6fSViresh Kumar unlock:
15667813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
15677813dd6fSViresh Kumar 
156832439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
15697813dd6fSViresh Kumar }
1570eb7c8743SViresh Kumar 
157132439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1572e77dcb0bSViresh Kumar {
157332439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1574e77dcb0bSViresh Kumar }
1575e77dcb0bSViresh Kumar 
1576eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1577eb7c8743SViresh Kumar {
1578e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1579eb7c8743SViresh Kumar }
15807813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
15817813dd6fSViresh Kumar 
15827813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
15837813dd6fSViresh Kumar {
15847813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1585cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
15866d3f922cSGeorgi Djakov 	int i;
15877813dd6fSViresh Kumar 
1588e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1589e0df59deSViresh Kumar 	list_del(&opp_table->node);
1590e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1591e0df59deSViresh Kumar 
159281c4d8a3SViresh Kumar 	if (opp_table->current_opp)
159381c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
159481c4d8a3SViresh Kumar 
15955d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
15965d6d106fSViresh Kumar 
15972083da24SViresh Kumar 	/* Release automatically acquired single clk */
15987813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
15997813dd6fSViresh Kumar 		clk_put(opp_table->clk);
16007813dd6fSViresh Kumar 
16016d3f922cSGeorgi Djakov 	if (opp_table->paths) {
16026d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
16036d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
16046d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
16056d3f922cSGeorgi Djakov 	}
16066d3f922cSGeorgi Djakov 
1607cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1608cdd6ed90SViresh Kumar 
160904bd2eafSViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node)
16107813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
16117813dd6fSViresh Kumar 
16124f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
16137813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
16147813dd6fSViresh Kumar 	kfree(opp_table);
16157813dd6fSViresh Kumar }
16167813dd6fSViresh Kumar 
16177813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
16187813dd6fSViresh Kumar {
16197813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
16207813dd6fSViresh Kumar 		       &opp_table_lock);
16217813dd6fSViresh Kumar }
16227813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
16237813dd6fSViresh Kumar 
16247813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
16257813dd6fSViresh Kumar {
16267813dd6fSViresh Kumar 	kfree(opp);
16277813dd6fSViresh Kumar }
16287813dd6fSViresh Kumar 
1629cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
16307813dd6fSViresh Kumar {
1631cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1632cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1633cf1fac94SViresh Kumar 
1634cf1fac94SViresh Kumar 	list_del(&opp->node);
1635cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1636cf1fac94SViresh Kumar 
16377813dd6fSViresh Kumar 	/*
16387813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
16397813dd6fSViresh Kumar 	 * frequency/voltage list.
16407813dd6fSViresh Kumar 	 */
16417813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
16423466ea2cSLiang He 	_of_clear_opp(opp_table, opp);
16437813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
16447813dd6fSViresh Kumar 	kfree(opp);
16451690d8bbSViresh Kumar }
16467813dd6fSViresh Kumar 
1647a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
16487813dd6fSViresh Kumar {
16497813dd6fSViresh Kumar 	kref_get(&opp->kref);
16507813dd6fSViresh Kumar }
16517813dd6fSViresh Kumar 
16527813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
16537813dd6fSViresh Kumar {
1654cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
16557813dd6fSViresh Kumar }
16567813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
16577813dd6fSViresh Kumar 
16587813dd6fSViresh Kumar /**
16597813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
16607813dd6fSViresh Kumar  * @dev:	device for which we do this operation
16617813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
16627813dd6fSViresh Kumar  *
16637813dd6fSViresh Kumar  * This function removes an opp from the opp table.
16647813dd6fSViresh Kumar  */
16657813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
16667813dd6fSViresh Kumar {
166795073b72SJakob Koschel 	struct dev_pm_opp *opp = NULL, *iter;
16687813dd6fSViresh Kumar 	struct opp_table *opp_table;
16697813dd6fSViresh Kumar 
16707813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
16717813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
16727813dd6fSViresh Kumar 		return;
16737813dd6fSViresh Kumar 
1674f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table))
1675f123ea74SViresh Kumar 		goto put_table;
1676f123ea74SViresh Kumar 
16777813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
16787813dd6fSViresh Kumar 
167995073b72SJakob Koschel 	list_for_each_entry(iter, &opp_table->opp_list, node) {
16802083da24SViresh Kumar 		if (iter->rates[0] == freq) {
168195073b72SJakob Koschel 			opp = iter;
16827813dd6fSViresh Kumar 			break;
16837813dd6fSViresh Kumar 		}
16847813dd6fSViresh Kumar 	}
16857813dd6fSViresh Kumar 
16867813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
16877813dd6fSViresh Kumar 
168895073b72SJakob Koschel 	if (opp) {
16897813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
16900ad8c623SViresh Kumar 
16910ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
16920ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
16937813dd6fSViresh Kumar 	} else {
16947813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
16957813dd6fSViresh Kumar 			 __func__, freq);
16967813dd6fSViresh Kumar 	}
16977813dd6fSViresh Kumar 
1698f123ea74SViresh Kumar put_table:
16990ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
17007813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17017813dd6fSViresh Kumar }
17027813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
17037813dd6fSViresh Kumar 
1704cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1705cf1fac94SViresh Kumar 					bool dynamic)
1706cf1fac94SViresh Kumar {
1707cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1708cf1fac94SViresh Kumar 
1709cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1710cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1711606a5d42SBeata Michalska 		/*
1712606a5d42SBeata Michalska 		 * Refcount must be dropped only once for each OPP by OPP core,
1713606a5d42SBeata Michalska 		 * do that with help of "removed" flag.
1714606a5d42SBeata Michalska 		 */
1715606a5d42SBeata Michalska 		if (!temp->removed && dynamic == temp->dynamic) {
1716cf1fac94SViresh Kumar 			opp = temp;
1717cf1fac94SViresh Kumar 			break;
1718cf1fac94SViresh Kumar 		}
1719cf1fac94SViresh Kumar 	}
1720cf1fac94SViresh Kumar 
1721cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1722cf1fac94SViresh Kumar 	return opp;
1723cf1fac94SViresh Kumar }
1724cf1fac94SViresh Kumar 
1725606a5d42SBeata Michalska /*
1726606a5d42SBeata Michalska  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1727606a5d42SBeata Michalska  * happen lock less to avoid circular dependency issues. This routine must be
1728606a5d42SBeata Michalska  * called without the opp_table->lock held.
1729606a5d42SBeata Michalska  */
1730606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
173103758d60SViresh Kumar {
1732cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
173303758d60SViresh Kumar 
1734606a5d42SBeata Michalska 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1735606a5d42SBeata Michalska 		opp->removed = true;
1736606a5d42SBeata Michalska 		dev_pm_opp_put(opp);
1737606a5d42SBeata Michalska 
1738606a5d42SBeata Michalska 		/* Drop the references taken by dev_pm_opp_add() */
1739606a5d42SBeata Michalska 		if (dynamic)
1740606a5d42SBeata Michalska 			dev_pm_opp_put_opp_table(opp_table);
1741606a5d42SBeata Michalska 	}
1742606a5d42SBeata Michalska }
1743606a5d42SBeata Michalska 
1744606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table)
1745606a5d42SBeata Michalska {
174603758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
174703758d60SViresh Kumar 
1748922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1749cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1750cf1fac94SViresh Kumar 		return false;
1751922ff075SViresh Kumar 	}
1752922ff075SViresh Kumar 
1753cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1754cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1755cf1fac94SViresh Kumar 		return true;
175603758d60SViresh Kumar 	}
175703758d60SViresh Kumar 
175803758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1759922ff075SViresh Kumar 
1760606a5d42SBeata Michalska 	_opp_remove_all(opp_table, false);
1761cf1fac94SViresh Kumar 	return true;
176203758d60SViresh Kumar }
176303758d60SViresh Kumar 
17641690d8bbSViresh Kumar /**
17651690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
17661690d8bbSViresh Kumar  * @dev:	device for which we do this operation
17671690d8bbSViresh Kumar  *
17681690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
17691690d8bbSViresh Kumar  */
17701690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
17711690d8bbSViresh Kumar {
17721690d8bbSViresh Kumar 	struct opp_table *opp_table;
17731690d8bbSViresh Kumar 
17741690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
17751690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
17761690d8bbSViresh Kumar 		return;
17771690d8bbSViresh Kumar 
1778606a5d42SBeata Michalska 	_opp_remove_all(opp_table, true);
17791690d8bbSViresh Kumar 
17801690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
17811690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17821690d8bbSViresh Kumar }
17831690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
17841690d8bbSViresh Kumar 
1785d6134583SViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table)
17867813dd6fSViresh Kumar {
17877813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
17882083da24SViresh Kumar 	int supply_count, supply_size, icc_size, clk_size;
17897813dd6fSViresh Kumar 
17907813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
1791d6134583SViresh Kumar 	supply_count = opp_table->regulator_count > 0 ?
1792d6134583SViresh Kumar 			opp_table->regulator_count : 1;
17936d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
17942083da24SViresh Kumar 	clk_size = sizeof(*opp->rates) * opp_table->clk_count;
1795d6134583SViresh Kumar 	icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
17967813dd6fSViresh Kumar 
17977813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
17982083da24SViresh Kumar 	opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL);
17997813dd6fSViresh Kumar 	if (!opp)
18007813dd6fSViresh Kumar 		return NULL;
18017813dd6fSViresh Kumar 
18022083da24SViresh Kumar 	/* Put the supplies, bw and clock at the end of the OPP structure */
18037813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
18042083da24SViresh Kumar 
18052083da24SViresh Kumar 	opp->rates = (unsigned long *)(opp->supplies + supply_count);
18062083da24SViresh Kumar 
18076d3f922cSGeorgi Djakov 	if (icc_size)
18082083da24SViresh Kumar 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
18092083da24SViresh Kumar 
18107813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
18117813dd6fSViresh Kumar 
18127813dd6fSViresh Kumar 	return opp;
18137813dd6fSViresh Kumar }
18147813dd6fSViresh Kumar 
18157813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
18167813dd6fSViresh Kumar 					 struct opp_table *opp_table)
18177813dd6fSViresh Kumar {
18187813dd6fSViresh Kumar 	struct regulator *reg;
18197813dd6fSViresh Kumar 	int i;
18207813dd6fSViresh Kumar 
182190e3577bSViresh Kumar 	if (!opp_table->regulators)
182290e3577bSViresh Kumar 		return true;
182390e3577bSViresh Kumar 
18247813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
18257813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
18267813dd6fSViresh Kumar 
18277813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
18287813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
18297813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
18307813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
18317813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
18327813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
18337813dd6fSViresh Kumar 			return false;
18347813dd6fSViresh Kumar 		}
18357813dd6fSViresh Kumar 	}
18367813dd6fSViresh Kumar 
18377813dd6fSViresh Kumar 	return true;
18387813dd6fSViresh Kumar }
18397813dd6fSViresh Kumar 
18402083da24SViresh Kumar static int _opp_compare_rate(struct opp_table *opp_table,
18412083da24SViresh Kumar 			     struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
18422083da24SViresh Kumar {
18432083da24SViresh Kumar 	int i;
18442083da24SViresh Kumar 
18452083da24SViresh Kumar 	for (i = 0; i < opp_table->clk_count; i++) {
18462083da24SViresh Kumar 		if (opp1->rates[i] != opp2->rates[i])
18472083da24SViresh Kumar 			return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
18482083da24SViresh Kumar 	}
18492083da24SViresh Kumar 
18502083da24SViresh Kumar 	/* Same rates for both OPPs */
18512083da24SViresh Kumar 	return 0;
18522083da24SViresh Kumar }
18532083da24SViresh Kumar 
1854274c3e83SViresh Kumar static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1855274c3e83SViresh Kumar 			   struct dev_pm_opp *opp2)
1856274c3e83SViresh Kumar {
1857274c3e83SViresh Kumar 	int i;
1858274c3e83SViresh Kumar 
1859274c3e83SViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
1860274c3e83SViresh Kumar 		if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
1861274c3e83SViresh Kumar 			return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
1862274c3e83SViresh Kumar 	}
1863274c3e83SViresh Kumar 
1864274c3e83SViresh Kumar 	/* Same bw for both OPPs */
1865274c3e83SViresh Kumar 	return 0;
1866274c3e83SViresh Kumar }
1867274c3e83SViresh Kumar 
18688bdac14bSViresh Kumar /*
18698bdac14bSViresh Kumar  * Returns
18708bdac14bSViresh Kumar  * 0: opp1 == opp2
18718bdac14bSViresh Kumar  * 1: opp1 > opp2
18728bdac14bSViresh Kumar  * -1: opp1 < opp2
18738bdac14bSViresh Kumar  */
18742083da24SViresh Kumar int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1,
18752083da24SViresh Kumar 		     struct dev_pm_opp *opp2)
18766c591eecSSaravana Kannan {
18772083da24SViresh Kumar 	int ret;
18782083da24SViresh Kumar 
18792083da24SViresh Kumar 	ret = _opp_compare_rate(opp_table, opp1, opp2);
18802083da24SViresh Kumar 	if (ret)
18812083da24SViresh Kumar 		return ret;
18822083da24SViresh Kumar 
1883274c3e83SViresh Kumar 	ret = _opp_compare_bw(opp_table, opp1, opp2);
1884274c3e83SViresh Kumar 	if (ret)
1885274c3e83SViresh Kumar 		return ret;
18862083da24SViresh Kumar 
18876c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
18886c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
18892083da24SViresh Kumar 
18902083da24SViresh Kumar 	/* Duplicate OPPs */
18916c591eecSSaravana Kannan 	return 0;
18926c591eecSSaravana Kannan }
18936c591eecSSaravana Kannan 
1894a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1895a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1896a1e8c136SViresh Kumar 			     struct list_head **head)
1897a1e8c136SViresh Kumar {
1898a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
18996c591eecSSaravana Kannan 	int opp_cmp;
1900a1e8c136SViresh Kumar 
1901a1e8c136SViresh Kumar 	/*
1902a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1903a1e8c136SViresh Kumar 	 * already present.
1904a1e8c136SViresh Kumar 	 *
1905a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1906a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1907a1e8c136SViresh Kumar 	 * loop.
1908a1e8c136SViresh Kumar 	 */
1909a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
19102083da24SViresh Kumar 		opp_cmp = _opp_compare_key(opp_table, new_opp, opp);
19116c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1912a1e8c136SViresh Kumar 			*head = &opp->node;
1913a1e8c136SViresh Kumar 			continue;
1914a1e8c136SViresh Kumar 		}
1915a1e8c136SViresh Kumar 
19166c591eecSSaravana Kannan 		if (opp_cmp < 0)
1917a1e8c136SViresh Kumar 			return 0;
1918a1e8c136SViresh Kumar 
1919a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1920a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
19212083da24SViresh Kumar 			 __func__, opp->rates[0], opp->supplies[0].u_volt,
19222083da24SViresh Kumar 			 opp->available, new_opp->rates[0],
1923a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1924a1e8c136SViresh Kumar 
1925a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1926a1e8c136SViresh Kumar 		return opp->available &&
1927a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1928a1e8c136SViresh Kumar 	}
1929a1e8c136SViresh Kumar 
1930a1e8c136SViresh Kumar 	return 0;
1931a1e8c136SViresh Kumar }
1932a1e8c136SViresh Kumar 
19337eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count)
19347eba0c76SViresh Kumar {
19357eba0c76SViresh Kumar 	int i;
19367eba0c76SViresh Kumar 
19377eba0c76SViresh Kumar 	for (i = 0; i < count; i++) {
19387eba0c76SViresh Kumar 		if (opp->required_opps[i]->available)
19397eba0c76SViresh Kumar 			continue;
19407eba0c76SViresh Kumar 
19417eba0c76SViresh Kumar 		opp->available = false;
19427eba0c76SViresh Kumar 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
19432083da24SViresh Kumar 			 __func__, opp->required_opps[i]->np, opp->rates[0]);
19447eba0c76SViresh Kumar 		return;
19457eba0c76SViresh Kumar 	}
19467eba0c76SViresh Kumar }
19477eba0c76SViresh Kumar 
19487813dd6fSViresh Kumar /*
19497813dd6fSViresh Kumar  * Returns:
19507813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
19517813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
19527813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
19537813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
19547813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
19557813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
19567813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
19577813dd6fSViresh Kumar  */
19587813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
19594768914bSViresh Kumar 	     struct opp_table *opp_table)
19607813dd6fSViresh Kumar {
19617813dd6fSViresh Kumar 	struct list_head *head;
19627813dd6fSViresh Kumar 	int ret;
19637813dd6fSViresh Kumar 
19647813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
19657813dd6fSViresh Kumar 	head = &opp_table->opp_list;
19667813dd6fSViresh Kumar 
1967a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1968a1e8c136SViresh Kumar 	if (ret) {
19697813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
19707813dd6fSViresh Kumar 		return ret;
19717813dd6fSViresh Kumar 	}
19727813dd6fSViresh Kumar 
19737813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
19747813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
19757813dd6fSViresh Kumar 
19767813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
19777813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
19787813dd6fSViresh Kumar 
1979a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
19807813dd6fSViresh Kumar 
19817813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
19827813dd6fSViresh Kumar 		new_opp->available = false;
19837813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
19842083da24SViresh Kumar 			 __func__, new_opp->rates[0]);
19857813dd6fSViresh Kumar 	}
19867813dd6fSViresh Kumar 
19877eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
19887eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
19897eba0c76SViresh Kumar 		return 0;
1990cf65948dSDmitry Osipenko 
19917eba0c76SViresh Kumar 	_required_opps_available(new_opp, opp_table->required_opp_count);
1992cf65948dSDmitry Osipenko 
19937813dd6fSViresh Kumar 	return 0;
19947813dd6fSViresh Kumar }
19957813dd6fSViresh Kumar 
19967813dd6fSViresh Kumar /**
19977813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
19987813dd6fSViresh Kumar  * @opp_table:	OPP table
19997813dd6fSViresh Kumar  * @dev:	device for which we do this operation
20007813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
20017813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
20027813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
20037813dd6fSViresh Kumar  *
20047813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
20057813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
20067813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
20077813dd6fSViresh Kumar  *
20087813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
20097813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
20107813dd6fSViresh Kumar  *
20117813dd6fSViresh Kumar  * Return:
20127813dd6fSViresh Kumar  * 0		On success OR
20137813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
20147813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
20157813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
20167813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
20177813dd6fSViresh Kumar  */
20187813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
20197813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
20207813dd6fSViresh Kumar {
20217813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
20227813dd6fSViresh Kumar 	unsigned long tol;
20237813dd6fSViresh Kumar 	int ret;
20247813dd6fSViresh Kumar 
2025f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table))
2026f123ea74SViresh Kumar 		return -EINVAL;
2027f123ea74SViresh Kumar 
20287813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
20297813dd6fSViresh Kumar 	if (!new_opp)
20307813dd6fSViresh Kumar 		return -ENOMEM;
20317813dd6fSViresh Kumar 
20327813dd6fSViresh Kumar 	/* populate the opp table */
20332083da24SViresh Kumar 	new_opp->rates[0] = freq;
20347813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
20357813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
20367813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
20377813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
20387813dd6fSViresh Kumar 	new_opp->available = true;
20397813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
20407813dd6fSViresh Kumar 
20414768914bSViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table);
20427813dd6fSViresh Kumar 	if (ret) {
20437813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
20447813dd6fSViresh Kumar 		if (ret == -EBUSY)
20457813dd6fSViresh Kumar 			ret = 0;
20467813dd6fSViresh Kumar 		goto free_opp;
20477813dd6fSViresh Kumar 	}
20487813dd6fSViresh Kumar 
20497813dd6fSViresh Kumar 	/*
20507813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
20517813dd6fSViresh Kumar 	 * frequency/voltage list.
20527813dd6fSViresh Kumar 	 */
20537813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
20547813dd6fSViresh Kumar 	return 0;
20557813dd6fSViresh Kumar 
20567813dd6fSViresh Kumar free_opp:
20577813dd6fSViresh Kumar 	_opp_free(new_opp);
20587813dd6fSViresh Kumar 
20597813dd6fSViresh Kumar 	return ret;
20607813dd6fSViresh Kumar }
20617813dd6fSViresh Kumar 
20627813dd6fSViresh Kumar /**
206389f03984SViresh Kumar  * _opp_set_supported_hw() - Set supported platforms
20647813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
20657813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
20667813dd6fSViresh Kumar  * @count: Number of elements in the array.
20677813dd6fSViresh Kumar  *
20687813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
20697813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
20707813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
20717813dd6fSViresh Kumar  * property.
20727813dd6fSViresh Kumar  */
207389f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table,
20747813dd6fSViresh Kumar 				 const u32 *versions, unsigned int count)
20757813dd6fSViresh Kumar {
207625419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
207725419de1SViresh Kumar 	if (opp_table->supported_hw)
207889f03984SViresh Kumar 		return 0;
20797813dd6fSViresh Kumar 
20807813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
20817813dd6fSViresh Kumar 					GFP_KERNEL);
208289f03984SViresh Kumar 	if (!opp_table->supported_hw)
208389f03984SViresh Kumar 		return -ENOMEM;
20847813dd6fSViresh Kumar 
20857813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
20867813dd6fSViresh Kumar 
208789f03984SViresh Kumar 	return 0;
20887813dd6fSViresh Kumar }
20897813dd6fSViresh Kumar 
20907813dd6fSViresh Kumar /**
209189f03984SViresh Kumar  * _opp_put_supported_hw() - Releases resources blocked for supported hw
209289f03984SViresh Kumar  * @opp_table: OPP table returned by _opp_set_supported_hw().
20937813dd6fSViresh Kumar  *
20947813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
209589f03984SViresh Kumar  * _opp_set_supported_hw(). Until this is called, the opp_table structure
20967813dd6fSViresh Kumar  * will not be freed.
20977813dd6fSViresh Kumar  */
209889f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table)
20997813dd6fSViresh Kumar {
210089f03984SViresh Kumar 	if (opp_table->supported_hw) {
21017813dd6fSViresh Kumar 		kfree(opp_table->supported_hw);
21027813dd6fSViresh Kumar 		opp_table->supported_hw = NULL;
21037813dd6fSViresh Kumar 		opp_table->supported_hw_count = 0;
21047813dd6fSViresh Kumar 	}
21059c4f220fSYangtao Li }
21069c4f220fSYangtao Li 
21079c4f220fSYangtao Li /**
2108298098e5SViresh Kumar  * _opp_set_prop_name() - Set prop-extn name
21097813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
21107813dd6fSViresh Kumar  * @name: name to postfix to properties.
21117813dd6fSViresh Kumar  *
21127813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
21137813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
21147813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
21157813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
21167813dd6fSViresh Kumar  */
2117298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
21187813dd6fSViresh Kumar {
2119878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
21207813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
2121298098e5SViresh Kumar 		opp_table->prop_name = kstrdup(name, GFP_KERNEL);
2122298098e5SViresh Kumar 		if (!opp_table->prop_name)
2123298098e5SViresh Kumar 			return -ENOMEM;
21247813dd6fSViresh Kumar 	}
21257813dd6fSViresh Kumar 
2126298098e5SViresh Kumar 	return 0;
21277813dd6fSViresh Kumar }
21287813dd6fSViresh Kumar 
21297813dd6fSViresh Kumar /**
2130298098e5SViresh Kumar  * _opp_put_prop_name() - Releases resources blocked for prop-name
2131298098e5SViresh Kumar  * @opp_table: OPP table returned by _opp_set_prop_name().
21327813dd6fSViresh Kumar  *
21337813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
2134298098e5SViresh Kumar  * _opp_set_prop_name(). Until this is called, the opp_table structure
21357813dd6fSViresh Kumar  * will not be freed.
21367813dd6fSViresh Kumar  */
2137298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table)
21387813dd6fSViresh Kumar {
2139298098e5SViresh Kumar 	if (opp_table->prop_name) {
21407813dd6fSViresh Kumar 		kfree(opp_table->prop_name);
21417813dd6fSViresh Kumar 		opp_table->prop_name = NULL;
21427813dd6fSViresh Kumar 	}
2143298098e5SViresh Kumar }
21447813dd6fSViresh Kumar 
21457813dd6fSViresh Kumar /**
2146b0ec0942SViresh Kumar  * _opp_set_regulators() - Set regulator names for the device
21477813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
21487813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
21497813dd6fSViresh Kumar  * @count: Number of regulators.
21507813dd6fSViresh Kumar  *
21517813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
21527813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
21537813dd6fSViresh Kumar  * well.
21547813dd6fSViresh Kumar  *
21557813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
21567813dd6fSViresh Kumar  */
2157b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
215887686cc8SViresh Kumar 			       const char * const names[])
21597813dd6fSViresh Kumar {
216087686cc8SViresh Kumar 	const char * const *temp = names;
21617813dd6fSViresh Kumar 	struct regulator *reg;
216287686cc8SViresh Kumar 	int count = 0, ret, i;
216387686cc8SViresh Kumar 
216487686cc8SViresh Kumar 	/* Count number of regulators */
216587686cc8SViresh Kumar 	while (*temp++)
216687686cc8SViresh Kumar 		count++;
216787686cc8SViresh Kumar 
216887686cc8SViresh Kumar 	if (!count)
2169b0ec0942SViresh Kumar 		return -EINVAL;
21707813dd6fSViresh Kumar 
2171779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
2172779b783cSViresh Kumar 	if (opp_table->regulators)
2173b0ec0942SViresh Kumar 		return 0;
21747813dd6fSViresh Kumar 
21757813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
21767813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
21777813dd6fSViresh Kumar 					      GFP_KERNEL);
2178b0ec0942SViresh Kumar 	if (!opp_table->regulators)
2179b0ec0942SViresh Kumar 		return -ENOMEM;
21807813dd6fSViresh Kumar 
21817813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
21827813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
21837813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
2184543256d2SKrzysztof Kozlowski 			ret = dev_err_probe(dev, PTR_ERR(reg),
2185543256d2SKrzysztof Kozlowski 					    "%s: no regulator (%s) found\n",
2186543256d2SKrzysztof Kozlowski 					    __func__, names[i]);
21877813dd6fSViresh Kumar 			goto free_regulators;
21887813dd6fSViresh Kumar 		}
21897813dd6fSViresh Kumar 
21907813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
21917813dd6fSViresh Kumar 	}
21927813dd6fSViresh Kumar 
21937813dd6fSViresh Kumar 	opp_table->regulator_count = count;
21947813dd6fSViresh Kumar 
2195c522ce8aSViresh Kumar 	/* Set generic config_regulators() for single regulators here */
2196c522ce8aSViresh Kumar 	if (count == 1)
2197c522ce8aSViresh Kumar 		opp_table->config_regulators = _opp_config_regulator_single;
2198c522ce8aSViresh Kumar 
2199b0ec0942SViresh Kumar 	return 0;
22007813dd6fSViresh Kumar 
22017813dd6fSViresh Kumar free_regulators:
220224957db1SMarek Szyprowski 	while (i != 0)
220324957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
22047813dd6fSViresh Kumar 
22057813dd6fSViresh Kumar 	kfree(opp_table->regulators);
22067813dd6fSViresh Kumar 	opp_table->regulators = NULL;
220746f48acaSViresh Kumar 	opp_table->regulator_count = -1;
22087813dd6fSViresh Kumar 
2209b0ec0942SViresh Kumar 	return ret;
22107813dd6fSViresh Kumar }
22117813dd6fSViresh Kumar 
22127813dd6fSViresh Kumar /**
2213b0ec0942SViresh Kumar  * _opp_put_regulators() - Releases resources blocked for regulator
2214b0ec0942SViresh Kumar  * @opp_table: OPP table returned from _opp_set_regulators().
22157813dd6fSViresh Kumar  */
2216b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table)
22177813dd6fSViresh Kumar {
22187813dd6fSViresh Kumar 	int i;
22197813dd6fSViresh Kumar 
2220779b783cSViresh Kumar 	if (!opp_table->regulators)
2221b0ec0942SViresh Kumar 		return;
22227813dd6fSViresh Kumar 
222372f80ce4SViresh Kumar 	if (opp_table->enabled) {
22248d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
22258d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
22268d45719cSKamil Konieczny 	}
22278d45719cSKamil Konieczny 
222824957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
22297813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
22307813dd6fSViresh Kumar 
22317813dd6fSViresh Kumar 	kfree(opp_table->regulators);
22327813dd6fSViresh Kumar 	opp_table->regulators = NULL;
223346f48acaSViresh Kumar 	opp_table->regulator_count = -1;
22347813dd6fSViresh Kumar }
223532aee78bSYangtao Li 
22362083da24SViresh Kumar static void _put_clks(struct opp_table *opp_table, int count)
22372083da24SViresh Kumar {
22382083da24SViresh Kumar 	int i;
22392083da24SViresh Kumar 
22402083da24SViresh Kumar 	for (i = count - 1; i >= 0; i--)
22412083da24SViresh Kumar 		clk_put(opp_table->clks[i]);
22422083da24SViresh Kumar 
22432083da24SViresh Kumar 	kfree(opp_table->clks);
22442083da24SViresh Kumar 	opp_table->clks = NULL;
22452083da24SViresh Kumar }
22462083da24SViresh Kumar 
22477813dd6fSViresh Kumar /**
22482368f576SViresh Kumar  * _opp_set_clknames() - Set clk names for the device
22492368f576SViresh Kumar  * @dev: Device for which clk names is being set.
22502368f576SViresh Kumar  * @names: Clk names.
22517813dd6fSViresh Kumar  *
22522368f576SViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointers to the
22532368f576SViresh Kumar  * clocks for the device. Simple cases work fine without using this routine
22542368f576SViresh Kumar  * (i.e. by passing connection-id as NULL), but for a device with multiple
22552368f576SViresh Kumar  * clocks available, the OPP core needs to know the exact names of the clks to
22562368f576SViresh Kumar  * use.
22577813dd6fSViresh Kumar  *
22587813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
22597813dd6fSViresh Kumar  */
22602368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
22612083da24SViresh Kumar 			     const char * const names[],
22622083da24SViresh Kumar 			     config_clks_t config_clks)
22637813dd6fSViresh Kumar {
22642368f576SViresh Kumar 	const char * const *temp = names;
22652083da24SViresh Kumar 	int count = 0, ret, i;
22662083da24SViresh Kumar 	struct clk *clk;
22677813dd6fSViresh Kumar 
22682368f576SViresh Kumar 	/* Count number of clks */
22692368f576SViresh Kumar 	while (*temp++)
22702368f576SViresh Kumar 		count++;
22717813dd6fSViresh Kumar 
22722368f576SViresh Kumar 	/*
22732368f576SViresh Kumar 	 * This is a special case where we have a single clock, whose connection
22742368f576SViresh Kumar 	 * id name is NULL, i.e. first two entries are NULL in the array.
22752368f576SViresh Kumar 	 */
22762368f576SViresh Kumar 	if (!count && !names[1])
22772368f576SViresh Kumar 		count = 1;
22782368f576SViresh Kumar 
22792083da24SViresh Kumar 	/* Fail early for invalid configurations */
22802f71ae1aSViresh Kumar 	if (!count || (!config_clks && count > 1))
22812368f576SViresh Kumar 		return -EINVAL;
22827813dd6fSViresh Kumar 
22830a43452bSViresh Kumar 	/* Another CPU that shares the OPP table has set the clkname ? */
22842083da24SViresh Kumar 	if (opp_table->clks)
22852368f576SViresh Kumar 		return 0;
22860a43452bSViresh Kumar 
22872083da24SViresh Kumar 	opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks),
22882083da24SViresh Kumar 					GFP_KERNEL);
22892083da24SViresh Kumar 	if (!opp_table->clks)
22902083da24SViresh Kumar 		return -ENOMEM;
22917813dd6fSViresh Kumar 
22922083da24SViresh Kumar 	/* Find clks for the device */
22932083da24SViresh Kumar 	for (i = 0; i < count; i++) {
22942083da24SViresh Kumar 		clk = clk_get(dev, names[i]);
22952083da24SViresh Kumar 		if (IS_ERR(clk)) {
22962083da24SViresh Kumar 			ret = dev_err_probe(dev, PTR_ERR(clk),
22972083da24SViresh Kumar 					    "%s: Couldn't find clock with name: %s\n",
22982083da24SViresh Kumar 					    __func__, names[i]);
22992083da24SViresh Kumar 			goto free_clks;
23007813dd6fSViresh Kumar 		}
23017813dd6fSViresh Kumar 
23022083da24SViresh Kumar 		opp_table->clks[i] = clk;
23032083da24SViresh Kumar 	}
23042083da24SViresh Kumar 
23052083da24SViresh Kumar 	opp_table->clk_count = count;
23062f71ae1aSViresh Kumar 	opp_table->config_clks = config_clks;
23072083da24SViresh Kumar 
23082083da24SViresh Kumar 	/* Set generic single clk set here */
23092083da24SViresh Kumar 	if (count == 1) {
23102f71ae1aSViresh Kumar 		if (!opp_table->config_clks)
23112083da24SViresh Kumar 			opp_table->config_clks = _opp_config_clk_single;
23122083da24SViresh Kumar 
23132083da24SViresh Kumar 		/*
23142083da24SViresh Kumar 		 * We could have just dropped the "clk" field and used "clks"
23152083da24SViresh Kumar 		 * everywhere. Instead we kept the "clk" field around for
23162083da24SViresh Kumar 		 * following reasons:
23172083da24SViresh Kumar 		 *
23182083da24SViresh Kumar 		 * - avoiding clks[0] everywhere else.
23192083da24SViresh Kumar 		 * - not running single clk helpers for multiple clk usecase by
23202083da24SViresh Kumar 		 *   mistake.
23212083da24SViresh Kumar 		 *
23222083da24SViresh Kumar 		 * Since this is single-clk case, just update the clk pointer
23232083da24SViresh Kumar 		 * too.
23242083da24SViresh Kumar 		 */
23252083da24SViresh Kumar 		opp_table->clk = opp_table->clks[0];
23262083da24SViresh Kumar 	}
23270a43452bSViresh Kumar 
23282368f576SViresh Kumar 	return 0;
23292083da24SViresh Kumar 
23302083da24SViresh Kumar free_clks:
23312083da24SViresh Kumar 	_put_clks(opp_table, i);
23322083da24SViresh Kumar 	return ret;
23337813dd6fSViresh Kumar }
23347813dd6fSViresh Kumar 
23357813dd6fSViresh Kumar /**
23362368f576SViresh Kumar  * _opp_put_clknames() - Releases resources blocked for clks.
23372368f576SViresh Kumar  * @opp_table: OPP table returned from _opp_set_clknames().
23387813dd6fSViresh Kumar  */
23392368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table)
23407813dd6fSViresh Kumar {
23412083da24SViresh Kumar 	if (!opp_table->clks)
23422083da24SViresh Kumar 		return;
23432083da24SViresh Kumar 
23442083da24SViresh Kumar 	opp_table->config_clks = NULL;
23452083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
23462083da24SViresh Kumar 
23472083da24SViresh Kumar 	_put_clks(opp_table, opp_table->clk_count);
2348a74f681cSYangtao Li }
2349a74f681cSYangtao Li 
2350a74f681cSYangtao Li /**
2351aee3352fSViresh Kumar  * _opp_set_config_regulators_helper() - Register custom set regulator helper.
2352aee3352fSViresh Kumar  * @dev: Device for which the helper is getting registered.
2353aee3352fSViresh Kumar  * @config_regulators: Custom set regulator helper.
2354aee3352fSViresh Kumar  *
2355aee3352fSViresh Kumar  * This is useful to support platforms with multiple regulators per device.
2356aee3352fSViresh Kumar  *
2357aee3352fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
2358aee3352fSViresh Kumar  */
2359aee3352fSViresh Kumar static int _opp_set_config_regulators_helper(struct opp_table *opp_table,
2360aee3352fSViresh Kumar 		struct device *dev, config_regulators_t config_regulators)
2361aee3352fSViresh Kumar {
2362aee3352fSViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
2363aee3352fSViresh Kumar 	if (!opp_table->config_regulators)
2364aee3352fSViresh Kumar 		opp_table->config_regulators = config_regulators;
2365aee3352fSViresh Kumar 
2366aee3352fSViresh Kumar 	return 0;
2367aee3352fSViresh Kumar }
2368aee3352fSViresh Kumar 
2369aee3352fSViresh Kumar /**
2370aee3352fSViresh Kumar  * _opp_put_config_regulators_helper() - Releases resources blocked for
2371aee3352fSViresh Kumar  *					 config_regulators helper.
2372aee3352fSViresh Kumar  * @opp_table: OPP table returned from _opp_set_config_regulators_helper().
2373aee3352fSViresh Kumar  *
2374aee3352fSViresh Kumar  * Release resources blocked for platform specific config_regulators helper.
2375aee3352fSViresh Kumar  */
2376aee3352fSViresh Kumar static void _opp_put_config_regulators_helper(struct opp_table *opp_table)
2377aee3352fSViresh Kumar {
2378aee3352fSViresh Kumar 	if (opp_table->config_regulators)
2379aee3352fSViresh Kumar 		opp_table->config_regulators = NULL;
2380aee3352fSViresh Kumar }
2381aee3352fSViresh Kumar 
2382442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table)
23836319aee1SViresh Kumar {
23846319aee1SViresh Kumar 	int index;
23856319aee1SViresh Kumar 
2386cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2387cb60e960SViresh Kumar 		return;
2388cb60e960SViresh Kumar 
23896319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
23906319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
23916319aee1SViresh Kumar 			continue;
23926319aee1SViresh Kumar 
23936319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
23946319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
23956319aee1SViresh Kumar 	}
2396c0ab9e08SViresh Kumar 
2397c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2398c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
23996319aee1SViresh Kumar }
24006319aee1SViresh Kumar 
24017813dd6fSViresh Kumar /**
2402442e7a17SViresh Kumar  * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
24036319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
24046319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
240517a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
24064f018bc0SViresh Kumar  *
24074f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
24084f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
24094f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
24104f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
24116319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
24126319aee1SViresh Kumar  * we don't need to support that separately.
24134f018bc0SViresh Kumar  *
24144f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
24156319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
24164f018bc0SViresh Kumar  *
24176319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
24186319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2419baea35e4SViresh Kumar  *
2420baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2421baea35e4SViresh Kumar  * "required-opps" are added in DT.
24224f018bc0SViresh Kumar  */
2423442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
24243734b9f2SDmitry Osipenko 			const char * const *names, struct device ***virt_devs)
24254f018bc0SViresh Kumar {
24266319aee1SViresh Kumar 	struct device *virt_dev;
2427baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
24283734b9f2SDmitry Osipenko 	const char * const *name = names;
24294f018bc0SViresh Kumar 
2430cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2431442e7a17SViresh Kumar 		return 0;
24324f018bc0SViresh Kumar 
24336319aee1SViresh Kumar 	/*
24346319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
24356319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
24366319aee1SViresh Kumar 	 * table is added.
24376319aee1SViresh Kumar 	 */
2438442e7a17SViresh Kumar 	if (!opp_table->required_opp_count)
2439442e7a17SViresh Kumar 		return -EPROBE_DEFER;
24406319aee1SViresh Kumar 
24414f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
24424f018bc0SViresh Kumar 
2443c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2444c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2445c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2446c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2447c0ab9e08SViresh Kumar 		goto unlock;
24484f018bc0SViresh Kumar 
24496319aee1SViresh Kumar 	while (*name) {
24506319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
24516319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
24526319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
24536319aee1SViresh Kumar 			goto err;
24546319aee1SViresh Kumar 		}
24554f018bc0SViresh Kumar 
24566319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
24574ea9496cSTang Bin 		if (IS_ERR_OR_NULL(virt_dev)) {
24584ea9496cSTang Bin 			ret = PTR_ERR(virt_dev) ? : -ENODEV;
24596319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
24606319aee1SViresh Kumar 			goto err;
24614f018bc0SViresh Kumar 		}
24624f018bc0SViresh Kumar 
24634f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2464baea35e4SViresh Kumar 		index++;
24656319aee1SViresh Kumar 		name++;
24666319aee1SViresh Kumar 	}
24676319aee1SViresh Kumar 
246817a8f868SViresh Kumar 	if (virt_devs)
246917a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
24704f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
24714f018bc0SViresh Kumar 
2472442e7a17SViresh Kumar 	return 0;
24736319aee1SViresh Kumar 
24746319aee1SViresh Kumar err:
2475442e7a17SViresh Kumar 	_detach_genpd(opp_table);
2476c0ab9e08SViresh Kumar unlock:
24776319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2478442e7a17SViresh Kumar 	return ret;
24796319aee1SViresh Kumar 
24804f018bc0SViresh Kumar }
24814f018bc0SViresh Kumar 
24824f018bc0SViresh Kumar /**
2483442e7a17SViresh Kumar  * _opp_detach_genpd() - Detach genpd(s) from the device.
2484442e7a17SViresh Kumar  * @opp_table: OPP table returned by _opp_attach_genpd().
24854f018bc0SViresh Kumar  *
24866319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
24876319aee1SViresh Kumar  * OPP table.
24884f018bc0SViresh Kumar  */
2489442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
24904f018bc0SViresh Kumar {
24914f018bc0SViresh Kumar 	/*
24924f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
24934f018bc0SViresh Kumar 	 * used in parallel.
24944f018bc0SViresh Kumar 	 */
24954f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2496442e7a17SViresh Kumar 	_detach_genpd(opp_table);
24974f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
24984f018bc0SViresh Kumar }
2499b4b9e223SDmitry Osipenko 
250011b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data)
250111b9b663SViresh Kumar {
250211b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_GENPD)
2503442e7a17SViresh Kumar 		_opp_detach_genpd(data->opp_table);
250411b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR)
2505b0ec0942SViresh Kumar 		_opp_put_regulators(data->opp_table);
250611b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_SUPPORTED_HW)
250789f03984SViresh Kumar 		_opp_put_supported_hw(data->opp_table);
25081f378c6eSViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
2509aee3352fSViresh Kumar 		_opp_put_config_regulators_helper(data->opp_table);
251011b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_PROP_NAME)
2511298098e5SViresh Kumar 		_opp_put_prop_name(data->opp_table);
251211b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_CLK)
25132368f576SViresh Kumar 		_opp_put_clknames(data->opp_table);
251411b9b663SViresh Kumar 
251511b9b663SViresh Kumar 	dev_pm_opp_put_opp_table(data->opp_table);
251611b9b663SViresh Kumar 	kfree(data);
251711b9b663SViresh Kumar }
251811b9b663SViresh Kumar 
251911b9b663SViresh Kumar /**
252011b9b663SViresh Kumar  * dev_pm_opp_set_config() - Set OPP configuration for the device.
252111b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
252211b9b663SViresh Kumar  * @config: OPP configuration.
252311b9b663SViresh Kumar  *
252411b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
252511b9b663SViresh Kumar  *
252611b9b663SViresh Kumar  * This must be called before any OPPs are initialized for the device. This may
252711b9b663SViresh Kumar  * be called multiple times for the same OPP table, for example once for each
252811b9b663SViresh Kumar  * CPU that share the same table. This must be balanced by the same number of
252911b9b663SViresh Kumar  * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
253011b9b663SViresh Kumar  *
253111b9b663SViresh Kumar  * This returns a token to the caller, which must be passed to
253211b9b663SViresh Kumar  * dev_pm_opp_clear_config() to free the resources later. The value of the
253311b9b663SViresh Kumar  * returned token will be >= 1 for success and negative for errors. The minimum
253411b9b663SViresh Kumar  * value of 1 is chosen here to make it easy for callers to manage the resource.
253511b9b663SViresh Kumar  */
253611b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
253711b9b663SViresh Kumar {
2538298098e5SViresh Kumar 	struct opp_table *opp_table;
253911b9b663SViresh Kumar 	struct opp_config_data *data;
254011b9b663SViresh Kumar 	unsigned int id;
254111b9b663SViresh Kumar 	int ret;
254211b9b663SViresh Kumar 
254311b9b663SViresh Kumar 	data = kmalloc(sizeof(*data), GFP_KERNEL);
254411b9b663SViresh Kumar 	if (!data)
254511b9b663SViresh Kumar 		return -ENOMEM;
254611b9b663SViresh Kumar 
254711b9b663SViresh Kumar 	opp_table = _add_opp_table(dev, false);
254811b9b663SViresh Kumar 	if (IS_ERR(opp_table)) {
254911b9b663SViresh Kumar 		kfree(data);
255011b9b663SViresh Kumar 		return PTR_ERR(opp_table);
255111b9b663SViresh Kumar 	}
255211b9b663SViresh Kumar 
255311b9b663SViresh Kumar 	data->opp_table = opp_table;
255411b9b663SViresh Kumar 	data->flags = 0;
255511b9b663SViresh Kumar 
255611b9b663SViresh Kumar 	/* This should be called before OPPs are initialized */
255711b9b663SViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
255811b9b663SViresh Kumar 		ret = -EBUSY;
255911b9b663SViresh Kumar 		goto err;
256011b9b663SViresh Kumar 	}
256111b9b663SViresh Kumar 
256211b9b663SViresh Kumar 	/* Configure clocks */
256311b9b663SViresh Kumar 	if (config->clk_names) {
25642083da24SViresh Kumar 		ret = _opp_set_clknames(opp_table, dev, config->clk_names,
25652083da24SViresh Kumar 					config->config_clks);
25662368f576SViresh Kumar 		if (ret)
256711b9b663SViresh Kumar 			goto err;
256811b9b663SViresh Kumar 
256911b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_CLK;
25702083da24SViresh Kumar 	} else if (config->config_clks) {
25712083da24SViresh Kumar 		/* Don't allow config callback without clocks */
25722083da24SViresh Kumar 		ret = -EINVAL;
25732083da24SViresh Kumar 		goto err;
257411b9b663SViresh Kumar 	}
257511b9b663SViresh Kumar 
257611b9b663SViresh Kumar 	/* Configure property names */
257711b9b663SViresh Kumar 	if (config->prop_name) {
2578298098e5SViresh Kumar 		ret = _opp_set_prop_name(opp_table, config->prop_name);
2579298098e5SViresh Kumar 		if (ret)
258011b9b663SViresh Kumar 			goto err;
258111b9b663SViresh Kumar 
258211b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_PROP_NAME;
258311b9b663SViresh Kumar 	}
258411b9b663SViresh Kumar 
2585aee3352fSViresh Kumar 	/* Configure config_regulators helper */
2586aee3352fSViresh Kumar 	if (config->config_regulators) {
2587aee3352fSViresh Kumar 		ret = _opp_set_config_regulators_helper(opp_table, dev,
2588aee3352fSViresh Kumar 						config->config_regulators);
2589aee3352fSViresh Kumar 		if (ret)
2590aee3352fSViresh Kumar 			goto err;
2591aee3352fSViresh Kumar 
2592aee3352fSViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR_HELPER;
2593aee3352fSViresh Kumar 	}
2594aee3352fSViresh Kumar 
259511b9b663SViresh Kumar 	/* Configure supported hardware */
259611b9b663SViresh Kumar 	if (config->supported_hw) {
259789f03984SViresh Kumar 		ret = _opp_set_supported_hw(opp_table, config->supported_hw,
259811b9b663SViresh Kumar 					    config->supported_hw_count);
259989f03984SViresh Kumar 		if (ret)
260011b9b663SViresh Kumar 			goto err;
260111b9b663SViresh Kumar 
260211b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_SUPPORTED_HW;
260311b9b663SViresh Kumar 	}
260411b9b663SViresh Kumar 
260511b9b663SViresh Kumar 	/* Configure supplies */
260611b9b663SViresh Kumar 	if (config->regulator_names) {
2607b0ec0942SViresh Kumar 		ret = _opp_set_regulators(opp_table, dev,
2608b0ec0942SViresh Kumar 					  config->regulator_names);
2609b0ec0942SViresh Kumar 		if (ret)
261011b9b663SViresh Kumar 			goto err;
261111b9b663SViresh Kumar 
261211b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR;
261311b9b663SViresh Kumar 	}
261411b9b663SViresh Kumar 
261511b9b663SViresh Kumar 	/* Attach genpds */
261611b9b663SViresh Kumar 	if (config->genpd_names) {
2617442e7a17SViresh Kumar 		ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
261811b9b663SViresh Kumar 					config->virt_devs);
2619442e7a17SViresh Kumar 		if (ret)
262011b9b663SViresh Kumar 			goto err;
262111b9b663SViresh Kumar 
262211b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_GENPD;
262311b9b663SViresh Kumar 	}
262411b9b663SViresh Kumar 
262511b9b663SViresh Kumar 	ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
262611b9b663SViresh Kumar 		       GFP_KERNEL);
262711b9b663SViresh Kumar 	if (ret)
262811b9b663SViresh Kumar 		goto err;
262911b9b663SViresh Kumar 
263011b9b663SViresh Kumar 	return id;
263111b9b663SViresh Kumar 
263211b9b663SViresh Kumar err:
263311b9b663SViresh Kumar 	_opp_clear_config(data);
263411b9b663SViresh Kumar 	return ret;
263511b9b663SViresh Kumar }
263611b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
263711b9b663SViresh Kumar 
263811b9b663SViresh Kumar /**
263911b9b663SViresh Kumar  * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
264011b9b663SViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_config().
264111b9b663SViresh Kumar  *
264211b9b663SViresh Kumar  * This allows all device OPP configurations to be cleared at once. This must be
264311b9b663SViresh Kumar  * called once for each call made to dev_pm_opp_set_config(), in order to free
264411b9b663SViresh Kumar  * the OPPs properly.
264511b9b663SViresh Kumar  *
264611b9b663SViresh Kumar  * Currently the first call itself ends up freeing all the OPP configurations,
264711b9b663SViresh Kumar  * while the later ones only drop the OPP table reference. This works well for
264811b9b663SViresh Kumar  * now as we would never want to use an half initialized OPP table and want to
264911b9b663SViresh Kumar  * remove the configurations together.
265011b9b663SViresh Kumar  */
265111b9b663SViresh Kumar void dev_pm_opp_clear_config(int token)
265211b9b663SViresh Kumar {
265311b9b663SViresh Kumar 	struct opp_config_data *data;
265411b9b663SViresh Kumar 
265511b9b663SViresh Kumar 	/*
265611b9b663SViresh Kumar 	 * This lets the callers call this unconditionally and keep their code
265711b9b663SViresh Kumar 	 * simple.
265811b9b663SViresh Kumar 	 */
265911b9b663SViresh Kumar 	if (unlikely(token <= 0))
266011b9b663SViresh Kumar 		return;
266111b9b663SViresh Kumar 
266211b9b663SViresh Kumar 	data = xa_erase(&opp_configs, token);
266311b9b663SViresh Kumar 	if (WARN_ON(!data))
266411b9b663SViresh Kumar 		return;
266511b9b663SViresh Kumar 
266611b9b663SViresh Kumar 	_opp_clear_config(data);
266711b9b663SViresh Kumar }
266811b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
266911b9b663SViresh Kumar 
267011b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token)
267111b9b663SViresh Kumar {
267211b9b663SViresh Kumar 	dev_pm_opp_clear_config((unsigned long)token);
267311b9b663SViresh Kumar }
267411b9b663SViresh Kumar 
267511b9b663SViresh Kumar /**
267611b9b663SViresh Kumar  * devm_pm_opp_set_config() - Set OPP configuration for the device.
267711b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
267811b9b663SViresh Kumar  * @config: OPP configuration.
267911b9b663SViresh Kumar  *
268011b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
268111b9b663SViresh Kumar  * This is a resource-managed variant of dev_pm_opp_set_config().
268211b9b663SViresh Kumar  *
268311b9b663SViresh Kumar  * Return: 0 on success and errorno otherwise.
268411b9b663SViresh Kumar  */
268511b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
268611b9b663SViresh Kumar {
268711b9b663SViresh Kumar 	int token = dev_pm_opp_set_config(dev, config);
268811b9b663SViresh Kumar 
268911b9b663SViresh Kumar 	if (token < 0)
269011b9b663SViresh Kumar 		return token;
269111b9b663SViresh Kumar 
269211b9b663SViresh Kumar 	return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
269311b9b663SViresh Kumar 					(void *) ((unsigned long) token));
269411b9b663SViresh Kumar }
269511b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
269611b9b663SViresh Kumar 
26974f018bc0SViresh Kumar /**
26987d8658efSSaravana Kannan  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
26997d8658efSSaravana Kannan  * @src_table: OPP table which has @dst_table as one of its required OPP table.
27007d8658efSSaravana Kannan  * @dst_table: Required OPP table of the @src_table.
27017d8658efSSaravana Kannan  * @src_opp: OPP from the @src_table.
27027d8658efSSaravana Kannan  *
27037d8658efSSaravana Kannan  * This function returns the OPP (present in @dst_table) pointed out by the
27047d8658efSSaravana Kannan  * "required-opps" property of the @src_opp (present in @src_table).
27057d8658efSSaravana Kannan  *
27067d8658efSSaravana Kannan  * The callers are required to call dev_pm_opp_put() for the returned OPP after
27077d8658efSSaravana Kannan  * use.
27087d8658efSSaravana Kannan  *
27097d8658efSSaravana Kannan  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
27107d8658efSSaravana Kannan  */
27117d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
27127d8658efSSaravana Kannan 						 struct opp_table *dst_table,
27137d8658efSSaravana Kannan 						 struct dev_pm_opp *src_opp)
27147d8658efSSaravana Kannan {
27157d8658efSSaravana Kannan 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
27167d8658efSSaravana Kannan 	int i;
27177d8658efSSaravana Kannan 
27187d8658efSSaravana Kannan 	if (!src_table || !dst_table || !src_opp ||
27197d8658efSSaravana Kannan 	    !src_table->required_opp_tables)
27207d8658efSSaravana Kannan 		return ERR_PTR(-EINVAL);
27217d8658efSSaravana Kannan 
27227d8658efSSaravana Kannan 	/* required-opps not fully initialized yet */
27237d8658efSSaravana Kannan 	if (lazy_linking_pending(src_table))
27247d8658efSSaravana Kannan 		return ERR_PTR(-EBUSY);
27257d8658efSSaravana Kannan 
27267d8658efSSaravana Kannan 	for (i = 0; i < src_table->required_opp_count; i++) {
27277d8658efSSaravana Kannan 		if (src_table->required_opp_tables[i] == dst_table) {
27287d8658efSSaravana Kannan 			mutex_lock(&src_table->lock);
27297d8658efSSaravana Kannan 
27307d8658efSSaravana Kannan 			list_for_each_entry(opp, &src_table->opp_list, node) {
27317d8658efSSaravana Kannan 				if (opp == src_opp) {
27327d8658efSSaravana Kannan 					dest_opp = opp->required_opps[i];
27337d8658efSSaravana Kannan 					dev_pm_opp_get(dest_opp);
27347d8658efSSaravana Kannan 					break;
27357d8658efSSaravana Kannan 				}
27367d8658efSSaravana Kannan 			}
27377d8658efSSaravana Kannan 
27387d8658efSSaravana Kannan 			mutex_unlock(&src_table->lock);
27397d8658efSSaravana Kannan 			break;
27407d8658efSSaravana Kannan 		}
27417d8658efSSaravana Kannan 	}
27427d8658efSSaravana Kannan 
27437d8658efSSaravana Kannan 	if (IS_ERR(dest_opp)) {
27447d8658efSSaravana Kannan 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
27457d8658efSSaravana Kannan 		       src_table, dst_table);
27467d8658efSSaravana Kannan 	}
27477d8658efSSaravana Kannan 
27487d8658efSSaravana Kannan 	return dest_opp;
27497d8658efSSaravana Kannan }
27507d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
27517d8658efSSaravana Kannan 
27527d8658efSSaravana Kannan /**
2753c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2754c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2755c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2756c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2757c8a59103SViresh Kumar  *
2758c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2759c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2760c8a59103SViresh Kumar  * performance state set to @pstate.
2761c8a59103SViresh Kumar  *
2762c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2763c8a59103SViresh Kumar  * value on errors.
2764c8a59103SViresh Kumar  */
2765c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2766c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2767c8a59103SViresh Kumar 				       unsigned int pstate)
2768c8a59103SViresh Kumar {
2769c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2770c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2771c8a59103SViresh Kumar 	int i;
2772c8a59103SViresh Kumar 
2773c8a59103SViresh Kumar 	/*
2774c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2775c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2776c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2777c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2778c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2779c8a59103SViresh Kumar 	 */
2780f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2781c8a59103SViresh Kumar 		return pstate;
2782c8a59103SViresh Kumar 
278384cb7ff3SViresh Kumar 	/* Both OPP tables must belong to genpds */
278484cb7ff3SViresh Kumar 	if (unlikely(!src_table->is_genpd || !dst_table->is_genpd)) {
278584cb7ff3SViresh Kumar 		pr_err("%s: Performance state is only valid for genpds.\n", __func__);
278684cb7ff3SViresh Kumar 		return -EINVAL;
278784cb7ff3SViresh Kumar 	}
278884cb7ff3SViresh Kumar 
27897eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
27907eba0c76SViresh Kumar 	if (lazy_linking_pending(src_table))
27917eba0c76SViresh Kumar 		return -EBUSY;
27927eba0c76SViresh Kumar 
2793c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2794c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2795c8a59103SViresh Kumar 			break;
2796c8a59103SViresh Kumar 	}
2797c8a59103SViresh Kumar 
2798c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2799c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2800c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2801c8a59103SViresh Kumar 		return -EINVAL;
2802c8a59103SViresh Kumar 	}
2803c8a59103SViresh Kumar 
2804c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2805c8a59103SViresh Kumar 
2806c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
28077c41cdcdSViresh Kumar 		if (opp->level == pstate) {
28087c41cdcdSViresh Kumar 			dest_pstate = opp->required_opps[i]->level;
2809c8a59103SViresh Kumar 			goto unlock;
2810c8a59103SViresh Kumar 		}
2811c8a59103SViresh Kumar 	}
2812c8a59103SViresh Kumar 
2813c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2814c8a59103SViresh Kumar 	       dst_table);
2815c8a59103SViresh Kumar 
2816c8a59103SViresh Kumar unlock:
2817c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2818c8a59103SViresh Kumar 
2819c8a59103SViresh Kumar 	return dest_pstate;
2820c8a59103SViresh Kumar }
2821c8a59103SViresh Kumar 
2822c8a59103SViresh Kumar /**
28237813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
28247813dd6fSViresh Kumar  * @dev:	device for which we do this operation
28257813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
28267813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
28277813dd6fSViresh Kumar  *
28287813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
28297813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
28307813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
28317813dd6fSViresh Kumar  *
28327813dd6fSViresh Kumar  * Return:
28337813dd6fSViresh Kumar  * 0		On success OR
28347813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
28357813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
28367813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
28377813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
28387813dd6fSViresh Kumar  */
28397813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
28407813dd6fSViresh Kumar {
28417813dd6fSViresh Kumar 	struct opp_table *opp_table;
28427813dd6fSViresh Kumar 	int ret;
28437813dd6fSViresh Kumar 
284432439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2845dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2846dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
28477813dd6fSViresh Kumar 
284846f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
284946f48acaSViresh Kumar 	opp_table->regulator_count = 1;
285046f48acaSViresh Kumar 
28517813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
28520ad8c623SViresh Kumar 	if (ret)
28537813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
28540ad8c623SViresh Kumar 
28557813dd6fSViresh Kumar 	return ret;
28567813dd6fSViresh Kumar }
28577813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
28587813dd6fSViresh Kumar 
28597813dd6fSViresh Kumar /**
28607813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
28617813dd6fSViresh Kumar  * @dev:		device for which we do this operation
28627813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
28637813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
28647813dd6fSViresh Kumar  *
28657813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
28667813dd6fSViresh Kumar  * which is isolated here.
28677813dd6fSViresh Kumar  *
28687813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
28697813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
28707813dd6fSViresh Kumar  * successful.
28717813dd6fSViresh Kumar  */
28727813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
28737813dd6fSViresh Kumar 				 bool availability_req)
28747813dd6fSViresh Kumar {
28757813dd6fSViresh Kumar 	struct opp_table *opp_table;
28767813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
28777813dd6fSViresh Kumar 	int r = 0;
28787813dd6fSViresh Kumar 
28797813dd6fSViresh Kumar 	/* Find the opp_table */
28807813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
28817813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
28827813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
28837813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
28847813dd6fSViresh Kumar 		return r;
28857813dd6fSViresh Kumar 	}
28867813dd6fSViresh Kumar 
2887f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table)) {
2888f123ea74SViresh Kumar 		r = -EINVAL;
2889f123ea74SViresh Kumar 		goto put_table;
2890f123ea74SViresh Kumar 	}
2891f123ea74SViresh Kumar 
28927813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
28937813dd6fSViresh Kumar 
28947813dd6fSViresh Kumar 	/* Do we have the frequency? */
28957813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
28962083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
28977813dd6fSViresh Kumar 			opp = tmp_opp;
28987813dd6fSViresh Kumar 			break;
28997813dd6fSViresh Kumar 		}
29007813dd6fSViresh Kumar 	}
29017813dd6fSViresh Kumar 
29027813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
29037813dd6fSViresh Kumar 		r = PTR_ERR(opp);
29047813dd6fSViresh Kumar 		goto unlock;
29057813dd6fSViresh Kumar 	}
29067813dd6fSViresh Kumar 
29077813dd6fSViresh Kumar 	/* Is update really needed? */
29087813dd6fSViresh Kumar 	if (opp->available == availability_req)
29097813dd6fSViresh Kumar 		goto unlock;
29107813dd6fSViresh Kumar 
29117813dd6fSViresh Kumar 	opp->available = availability_req;
29127813dd6fSViresh Kumar 
29137813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
29147813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
29157813dd6fSViresh Kumar 
29167813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
29177813dd6fSViresh Kumar 	if (availability_req)
29187813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
29197813dd6fSViresh Kumar 					     opp);
29207813dd6fSViresh Kumar 	else
29217813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
29227813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
29237813dd6fSViresh Kumar 
29247813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
29257813dd6fSViresh Kumar 	goto put_table;
29267813dd6fSViresh Kumar 
29277813dd6fSViresh Kumar unlock:
29287813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
29297813dd6fSViresh Kumar put_table:
29307813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29317813dd6fSViresh Kumar 	return r;
29327813dd6fSViresh Kumar }
29337813dd6fSViresh Kumar 
29347813dd6fSViresh Kumar /**
293525cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
293625cb20a2SStephen Boyd  * @dev:		device for which we do this operation
293725cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
293825cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
293925cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
294025cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
294125cb20a2SStephen Boyd  *
294225cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
294325cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
294425cb20a2SStephen Boyd  * successful.
294525cb20a2SStephen Boyd  */
294625cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
294725cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
294825cb20a2SStephen Boyd 			      unsigned long u_volt_max)
294925cb20a2SStephen Boyd 
295025cb20a2SStephen Boyd {
295125cb20a2SStephen Boyd 	struct opp_table *opp_table;
295225cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
295325cb20a2SStephen Boyd 	int r = 0;
295425cb20a2SStephen Boyd 
295525cb20a2SStephen Boyd 	/* Find the opp_table */
295625cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
295725cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
295825cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
295925cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
296025cb20a2SStephen Boyd 		return r;
296125cb20a2SStephen Boyd 	}
296225cb20a2SStephen Boyd 
2963f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table)) {
2964f123ea74SViresh Kumar 		r = -EINVAL;
2965f123ea74SViresh Kumar 		goto put_table;
2966f123ea74SViresh Kumar 	}
2967f123ea74SViresh Kumar 
296825cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
296925cb20a2SStephen Boyd 
297025cb20a2SStephen Boyd 	/* Do we have the frequency? */
297125cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
29722083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
297325cb20a2SStephen Boyd 			opp = tmp_opp;
297425cb20a2SStephen Boyd 			break;
297525cb20a2SStephen Boyd 		}
297625cb20a2SStephen Boyd 	}
297725cb20a2SStephen Boyd 
297825cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
297925cb20a2SStephen Boyd 		r = PTR_ERR(opp);
298025cb20a2SStephen Boyd 		goto adjust_unlock;
298125cb20a2SStephen Boyd 	}
298225cb20a2SStephen Boyd 
298325cb20a2SStephen Boyd 	/* Is update really needed? */
298425cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
298525cb20a2SStephen Boyd 		goto adjust_unlock;
298625cb20a2SStephen Boyd 
298725cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
298825cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
298925cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
299025cb20a2SStephen Boyd 
299125cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
299225cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
299325cb20a2SStephen Boyd 
299425cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
299525cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
299625cb20a2SStephen Boyd 				     opp);
299725cb20a2SStephen Boyd 
299825cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
2999f123ea74SViresh Kumar 	goto put_table;
300025cb20a2SStephen Boyd 
300125cb20a2SStephen Boyd adjust_unlock:
300225cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
3003f123ea74SViresh Kumar put_table:
300425cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
300525cb20a2SStephen Boyd 	return r;
300625cb20a2SStephen Boyd }
300703649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
300825cb20a2SStephen Boyd 
300925cb20a2SStephen Boyd /**
30107813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
30117813dd6fSViresh Kumar  * @dev:	device for which we do this operation
30127813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
30137813dd6fSViresh Kumar  *
30147813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
30157813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
30167813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
30177813dd6fSViresh Kumar  *
30187813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
30197813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
30207813dd6fSViresh Kumar  * successful.
30217813dd6fSViresh Kumar  */
30227813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
30237813dd6fSViresh Kumar {
30247813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
30257813dd6fSViresh Kumar }
30267813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
30277813dd6fSViresh Kumar 
30287813dd6fSViresh Kumar /**
30297813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
30307813dd6fSViresh Kumar  * @dev:	device for which we do this operation
30317813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
30327813dd6fSViresh Kumar  *
30337813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
30347813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
30357813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
30367813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
30377813dd6fSViresh Kumar  *
30387813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
30397813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
30407813dd6fSViresh Kumar  * successful.
30417813dd6fSViresh Kumar  */
30427813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
30437813dd6fSViresh Kumar {
30447813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
30457813dd6fSViresh Kumar }
30467813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
30477813dd6fSViresh Kumar 
30487813dd6fSViresh Kumar /**
30497813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
30507813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
30517813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
30527813dd6fSViresh Kumar  *
30537813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
30547813dd6fSViresh Kumar  */
30557813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
30567813dd6fSViresh Kumar {
30577813dd6fSViresh Kumar 	struct opp_table *opp_table;
30587813dd6fSViresh Kumar 	int ret;
30597813dd6fSViresh Kumar 
30607813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
30617813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
30627813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
30637813dd6fSViresh Kumar 
30647813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
30657813dd6fSViresh Kumar 
30667813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
30677813dd6fSViresh Kumar 
30687813dd6fSViresh Kumar 	return ret;
30697813dd6fSViresh Kumar }
30707813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
30717813dd6fSViresh Kumar 
30727813dd6fSViresh Kumar /**
30737813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
30747813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
30757813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
30767813dd6fSViresh Kumar  *
30777813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
30787813dd6fSViresh Kumar  */
30797813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
30807813dd6fSViresh Kumar 				   struct notifier_block *nb)
30817813dd6fSViresh Kumar {
30827813dd6fSViresh Kumar 	struct opp_table *opp_table;
30837813dd6fSViresh Kumar 	int ret;
30847813dd6fSViresh Kumar 
30857813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
30867813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
30877813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
30887813dd6fSViresh Kumar 
30897813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
30907813dd6fSViresh Kumar 
30917813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
30927813dd6fSViresh Kumar 
30937813dd6fSViresh Kumar 	return ret;
30947813dd6fSViresh Kumar }
30957813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
30967813dd6fSViresh Kumar 
30978aaf6264SViresh Kumar /**
30988aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
30998aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
31008aaf6264SViresh Kumar  *
31018aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
31028aaf6264SViresh Kumar  * dynamically added entries.
31038aaf6264SViresh Kumar  */
31048aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
31057813dd6fSViresh Kumar {
31067813dd6fSViresh Kumar 	struct opp_table *opp_table;
31077813dd6fSViresh Kumar 
31087813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
31097813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
31107813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
31117813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
31127813dd6fSViresh Kumar 
31137813dd6fSViresh Kumar 		if (error != -ENODEV)
31147813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
31157813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
31167813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
31177813dd6fSViresh Kumar 			     error);
31187813dd6fSViresh Kumar 		return;
31197813dd6fSViresh Kumar 	}
31207813dd6fSViresh Kumar 
3121922ff075SViresh Kumar 	/*
3122922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
3123922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
3124922ff075SViresh Kumar 	 **/
3125922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
3126cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
3127cdd6ed90SViresh Kumar 
3128922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
31297813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
31307813dd6fSViresh Kumar }
31317813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
3132ce8073d8SDmitry Osipenko 
3133ce8073d8SDmitry Osipenko /**
3134ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
3135ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
3136ce8073d8SDmitry Osipenko  *
3137ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
3138ce8073d8SDmitry Osipenko  *
3139ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
3140ce8073d8SDmitry Osipenko  */
3141ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
3142ce8073d8SDmitry Osipenko {
3143ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
3144ce8073d8SDmitry Osipenko 	struct regulator *reg;
3145ce8073d8SDmitry Osipenko 	int i, ret = 0;
3146ce8073d8SDmitry Osipenko 
3147ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
3148ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
3149ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
3150ce8073d8SDmitry Osipenko 		return 0;
3151ce8073d8SDmitry Osipenko 
3152ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
3153ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
3154ce8073d8SDmitry Osipenko 		goto put_table;
3155ce8073d8SDmitry Osipenko 
3156ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
3157ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
3158ce8073d8SDmitry Osipenko 		goto put_table;
3159ce8073d8SDmitry Osipenko 
3160ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
3161ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
3162ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
3163ce8073d8SDmitry Osipenko 		if (ret)
3164ce8073d8SDmitry Osipenko 			break;
3165ce8073d8SDmitry Osipenko 	}
3166ce8073d8SDmitry Osipenko put_table:
3167ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
3168ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
3169ce8073d8SDmitry Osipenko 
3170ce8073d8SDmitry Osipenko 	return ret;
3171ce8073d8SDmitry Osipenko }
3172ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
3173