xref: /openbmc/linux/drivers/opp/core.c (revision a5893928bb179d67ca1d44a8f66c990480ba541d)
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 /**
2015f756d03SManivannan Sadhasivam  * dev_pm_opp_get_freq_indexed() - Gets the frequency corresponding to an
2025f756d03SManivannan Sadhasivam  *				   available opp with specified index
2035f756d03SManivannan Sadhasivam  * @opp: opp for which frequency has to be returned for
2045f756d03SManivannan Sadhasivam  * @index: index of the frequency within the required opp
2055f756d03SManivannan Sadhasivam  *
2065f756d03SManivannan Sadhasivam  * Return: frequency in hertz corresponding to the opp with specified index,
2075f756d03SManivannan Sadhasivam  * else return 0
2085f756d03SManivannan Sadhasivam  */
2095f756d03SManivannan Sadhasivam unsigned long dev_pm_opp_get_freq_indexed(struct dev_pm_opp *opp, u32 index)
2105f756d03SManivannan Sadhasivam {
2115f756d03SManivannan Sadhasivam 	if (IS_ERR_OR_NULL(opp) || index >= opp->opp_table->clk_count) {
2125f756d03SManivannan Sadhasivam 		pr_err("%s: Invalid parameters\n", __func__);
2135f756d03SManivannan Sadhasivam 		return 0;
2145f756d03SManivannan Sadhasivam 	}
2155f756d03SManivannan Sadhasivam 
2165f756d03SManivannan Sadhasivam 	return opp->rates[index];
2175f756d03SManivannan Sadhasivam }
2185f756d03SManivannan Sadhasivam EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq_indexed);
2195f756d03SManivannan Sadhasivam 
2205f756d03SManivannan 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 
649*a5893928SViresh Kumar /**
650*a5893928SViresh Kumar  * dev_pm_opp_find_freq_exact_indexed() - Search for an exact freq for the
651*a5893928SViresh Kumar  *					 clock corresponding to the index
652*a5893928SViresh Kumar  * @dev:	Device for which we do this operation
653*a5893928SViresh Kumar  * @freq:	frequency to search for
654*a5893928SViresh Kumar  * @index:	Clock index
655*a5893928SViresh Kumar  * @available:	true/false - match for available opp
656*a5893928SViresh Kumar  *
657*a5893928SViresh Kumar  * Search for the matching exact OPP for the clock corresponding to the
658*a5893928SViresh Kumar  * specified index from a starting freq for a device.
659*a5893928SViresh Kumar  *
660*a5893928SViresh Kumar  * Return: matching *opp , else returns ERR_PTR in case of error and should be
661*a5893928SViresh Kumar  * handled using IS_ERR. Error return values can be:
662*a5893928SViresh Kumar  * EINVAL:	for bad pointer
663*a5893928SViresh Kumar  * ERANGE:	no match found for search
664*a5893928SViresh Kumar  * ENODEV:	if device not found in list of registered devices
665*a5893928SViresh Kumar  *
666*a5893928SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
667*a5893928SViresh Kumar  * use.
668*a5893928SViresh Kumar  */
669*a5893928SViresh Kumar struct dev_pm_opp *
670*a5893928SViresh Kumar dev_pm_opp_find_freq_exact_indexed(struct device *dev, unsigned long freq,
671*a5893928SViresh Kumar 				   u32 index, bool available)
672*a5893928SViresh Kumar {
673*a5893928SViresh Kumar 	return _find_key_exact(dev, freq, index, available, _read_freq, NULL);
674*a5893928SViresh Kumar }
675*a5893928SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact_indexed);
676*a5893928SViresh Kumar 
6777813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
6787813dd6fSViresh Kumar 						   unsigned long *freq)
6797813dd6fSViresh Kumar {
680e10a4644SViresh Kumar 	return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq,
681f123ea74SViresh Kumar 					assert_single_clk);
6827813dd6fSViresh Kumar }
6837813dd6fSViresh Kumar 
6847813dd6fSViresh Kumar /**
6857813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
6867813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6877813dd6fSViresh Kumar  * @freq:	Start frequency
6887813dd6fSViresh Kumar  *
6897813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
6907813dd6fSViresh Kumar  * for a device.
6917813dd6fSViresh Kumar  *
6927813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6937813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6947813dd6fSViresh Kumar  * values can be:
6957813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6967813dd6fSViresh Kumar  * ERANGE:	no match found for search
6977813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6987813dd6fSViresh Kumar  *
6997813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
7007813dd6fSViresh Kumar  * use.
7017813dd6fSViresh Kumar  */
7027813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
7037813dd6fSViresh Kumar 					     unsigned long *freq)
7047813dd6fSViresh Kumar {
705f123ea74SViresh Kumar 	return _find_key_ceil(dev, freq, 0, true, _read_freq, assert_single_clk);
7067813dd6fSViresh Kumar }
7077813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
7087813dd6fSViresh Kumar 
7097813dd6fSViresh Kumar /**
710142e17c1SManivannan Sadhasivam  * dev_pm_opp_find_freq_ceil_indexed() - Search for a rounded ceil freq for the
711142e17c1SManivannan Sadhasivam  *					 clock corresponding to the index
712142e17c1SManivannan Sadhasivam  * @dev:	Device for which we do this operation
713142e17c1SManivannan Sadhasivam  * @freq:	Start frequency
714142e17c1SManivannan Sadhasivam  * @index:	Clock index
715142e17c1SManivannan Sadhasivam  *
716142e17c1SManivannan Sadhasivam  * Search for the matching ceil *available* OPP for the clock corresponding to
717142e17c1SManivannan Sadhasivam  * the specified index from a starting freq for a device.
718142e17c1SManivannan Sadhasivam  *
719142e17c1SManivannan Sadhasivam  * Return: matching *opp and refreshes *freq accordingly, else returns
720142e17c1SManivannan Sadhasivam  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
721142e17c1SManivannan Sadhasivam  * values can be:
722142e17c1SManivannan Sadhasivam  * EINVAL:	for bad pointer
723142e17c1SManivannan Sadhasivam  * ERANGE:	no match found for search
724142e17c1SManivannan Sadhasivam  * ENODEV:	if device not found in list of registered devices
725142e17c1SManivannan Sadhasivam  *
726142e17c1SManivannan Sadhasivam  * The callers are required to call dev_pm_opp_put() for the returned OPP after
727142e17c1SManivannan Sadhasivam  * use.
728142e17c1SManivannan Sadhasivam  */
729142e17c1SManivannan Sadhasivam struct dev_pm_opp *
730142e17c1SManivannan Sadhasivam dev_pm_opp_find_freq_ceil_indexed(struct device *dev, unsigned long *freq,
731142e17c1SManivannan Sadhasivam 				  u32 index)
732142e17c1SManivannan Sadhasivam {
733142e17c1SManivannan Sadhasivam 	return _find_key_ceil(dev, freq, index, true, _read_freq, NULL);
734142e17c1SManivannan Sadhasivam }
735142e17c1SManivannan Sadhasivam EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_indexed);
736142e17c1SManivannan Sadhasivam 
737142e17c1SManivannan Sadhasivam /**
7387813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
7397813dd6fSViresh Kumar  * @dev:	device for which we do this operation
7407813dd6fSViresh Kumar  * @freq:	Start frequency
7417813dd6fSViresh Kumar  *
7427813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
7437813dd6fSViresh Kumar  * for a device.
7447813dd6fSViresh Kumar  *
7457813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
7467813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
7477813dd6fSViresh Kumar  * values can be:
7487813dd6fSViresh Kumar  * EINVAL:	for bad pointer
7497813dd6fSViresh Kumar  * ERANGE:	no match found for search
7507813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
7517813dd6fSViresh Kumar  *
7527813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
7537813dd6fSViresh Kumar  * use.
7547813dd6fSViresh Kumar  */
7557813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
7567813dd6fSViresh Kumar 					      unsigned long *freq)
7577813dd6fSViresh Kumar {
758f123ea74SViresh Kumar 	return _find_key_floor(dev, freq, 0, true, _read_freq, assert_single_clk);
7597813dd6fSViresh Kumar }
7607813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
7617813dd6fSViresh Kumar 
7622f36bde0SAndrew-sh.Cheng /**
763142e17c1SManivannan Sadhasivam  * dev_pm_opp_find_freq_floor_indexed() - Search for a rounded floor freq for the
764142e17c1SManivannan Sadhasivam  *					  clock corresponding to the index
765142e17c1SManivannan Sadhasivam  * @dev:	Device for which we do this operation
766142e17c1SManivannan Sadhasivam  * @freq:	Start frequency
767142e17c1SManivannan Sadhasivam  * @index:	Clock index
768142e17c1SManivannan Sadhasivam  *
769142e17c1SManivannan Sadhasivam  * Search for the matching floor *available* OPP for the clock corresponding to
770142e17c1SManivannan Sadhasivam  * the specified index from a starting freq for a device.
771142e17c1SManivannan Sadhasivam  *
772142e17c1SManivannan Sadhasivam  * Return: matching *opp and refreshes *freq accordingly, else returns
773142e17c1SManivannan Sadhasivam  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
774142e17c1SManivannan Sadhasivam  * values can be:
775142e17c1SManivannan Sadhasivam  * EINVAL:	for bad pointer
776142e17c1SManivannan Sadhasivam  * ERANGE:	no match found for search
777142e17c1SManivannan Sadhasivam  * ENODEV:	if device not found in list of registered devices
778142e17c1SManivannan Sadhasivam  *
779142e17c1SManivannan Sadhasivam  * The callers are required to call dev_pm_opp_put() for the returned OPP after
780142e17c1SManivannan Sadhasivam  * use.
781142e17c1SManivannan Sadhasivam  */
782142e17c1SManivannan Sadhasivam struct dev_pm_opp *
783142e17c1SManivannan Sadhasivam dev_pm_opp_find_freq_floor_indexed(struct device *dev, unsigned long *freq,
784142e17c1SManivannan Sadhasivam 				   u32 index)
785142e17c1SManivannan Sadhasivam {
786142e17c1SManivannan Sadhasivam 	return _find_key_floor(dev, freq, index, true, _read_freq, NULL);
787142e17c1SManivannan Sadhasivam }
788142e17c1SManivannan Sadhasivam EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor_indexed);
789142e17c1SManivannan Sadhasivam 
790142e17c1SManivannan Sadhasivam /**
79122079af7SViresh Kumar  * dev_pm_opp_find_level_exact() - search for an exact level
79222079af7SViresh Kumar  * @dev:		device for which we do this operation
79322079af7SViresh Kumar  * @level:		level to search for
79422079af7SViresh Kumar  *
79522079af7SViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
79622079af7SViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
79722079af7SViresh Kumar  * be handled using IS_ERR. Error return values can be:
79822079af7SViresh Kumar  * EINVAL:	for bad pointer
79922079af7SViresh Kumar  * ERANGE:	no match found for search
80022079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
80122079af7SViresh Kumar  *
80222079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
80322079af7SViresh Kumar  * use.
80422079af7SViresh Kumar  */
80522079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
80622079af7SViresh Kumar 					       unsigned int level)
80722079af7SViresh Kumar {
808e10a4644SViresh Kumar 	return _find_key_exact(dev, level, 0, true, _read_level, NULL);
80922079af7SViresh Kumar }
81022079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
81122079af7SViresh Kumar 
81222079af7SViresh Kumar /**
81322079af7SViresh Kumar  * dev_pm_opp_find_level_ceil() - search for an rounded up level
81422079af7SViresh Kumar  * @dev:		device for which we do this operation
81522079af7SViresh Kumar  * @level:		level to search for
81622079af7SViresh Kumar  *
81722079af7SViresh Kumar  * Return: Searches for rounded up match in the opp table and returns pointer
81822079af7SViresh Kumar  * to the  matching opp if found, else returns ERR_PTR in case of error and
81922079af7SViresh Kumar  * should be handled using IS_ERR. Error return values can be:
82022079af7SViresh Kumar  * EINVAL:	for bad pointer
82122079af7SViresh Kumar  * ERANGE:	no match found for search
82222079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
82322079af7SViresh Kumar  *
82422079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
82522079af7SViresh Kumar  * use.
82622079af7SViresh Kumar  */
82722079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
82822079af7SViresh Kumar 					      unsigned int *level)
82922079af7SViresh Kumar {
830c2ab2cb6SViresh Kumar 	unsigned long temp = *level;
831c2ab2cb6SViresh Kumar 	struct dev_pm_opp *opp;
83222079af7SViresh Kumar 
833e10a4644SViresh Kumar 	opp = _find_key_ceil(dev, &temp, 0, true, _read_level, NULL);
834c2ab2cb6SViresh Kumar 	*level = temp;
83522079af7SViresh Kumar 	return opp;
83622079af7SViresh Kumar }
83722079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
83822079af7SViresh Kumar 
83922079af7SViresh Kumar /**
84000ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
84100ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
842617df304SYang Li  * @bw:	start bandwidth
84300ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
84400ce3873SKrzysztof Kozlowski  *
84500ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
84600ce3873SKrzysztof Kozlowski  * for a device.
84700ce3873SKrzysztof Kozlowski  *
84800ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
84900ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
85000ce3873SKrzysztof Kozlowski  * values can be:
85100ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
85200ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
85300ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
85400ce3873SKrzysztof Kozlowski  *
85500ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
85600ce3873SKrzysztof Kozlowski  * use.
85700ce3873SKrzysztof Kozlowski  */
858add1dc09SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
859add1dc09SViresh Kumar 					   int index)
86000ce3873SKrzysztof Kozlowski {
861add1dc09SViresh Kumar 	unsigned long temp = *bw;
862add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
86300ce3873SKrzysztof Kozlowski 
864e10a4644SViresh Kumar 	opp = _find_key_ceil(dev, &temp, index, true, _read_bw, NULL);
865add1dc09SViresh Kumar 	*bw = temp;
86600ce3873SKrzysztof Kozlowski 	return opp;
86700ce3873SKrzysztof Kozlowski }
86800ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
86900ce3873SKrzysztof Kozlowski 
87000ce3873SKrzysztof Kozlowski /**
87100ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
87200ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
873617df304SYang Li  * @bw:	start bandwidth
87400ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
87500ce3873SKrzysztof Kozlowski  *
87600ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
87700ce3873SKrzysztof Kozlowski  * for a device.
87800ce3873SKrzysztof Kozlowski  *
87900ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
88000ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
88100ce3873SKrzysztof Kozlowski  * values can be:
88200ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
88300ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
88400ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
88500ce3873SKrzysztof Kozlowski  *
88600ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
88700ce3873SKrzysztof Kozlowski  * use.
88800ce3873SKrzysztof Kozlowski  */
88900ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
89000ce3873SKrzysztof Kozlowski 					    unsigned int *bw, int index)
89100ce3873SKrzysztof Kozlowski {
892add1dc09SViresh Kumar 	unsigned long temp = *bw;
893add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
89400ce3873SKrzysztof Kozlowski 
895e10a4644SViresh Kumar 	opp = _find_key_floor(dev, &temp, index, true, _read_bw, NULL);
896add1dc09SViresh Kumar 	*bw = temp;
89700ce3873SKrzysztof Kozlowski 	return opp;
89800ce3873SKrzysztof Kozlowski }
89900ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
90000ce3873SKrzysztof Kozlowski 
9017813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
9027813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
9037813dd6fSViresh Kumar {
9047813dd6fSViresh Kumar 	int ret;
9057813dd6fSViresh Kumar 
9067813dd6fSViresh Kumar 	/* Regulator not available for device */
9077813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
9087813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
9097813dd6fSViresh Kumar 			PTR_ERR(reg));
9107813dd6fSViresh Kumar 		return 0;
9117813dd6fSViresh Kumar 	}
9127813dd6fSViresh Kumar 
9137813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
9147813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
9157813dd6fSViresh Kumar 
9167813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
9177813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
9187813dd6fSViresh Kumar 	if (ret)
9197813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
9207813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
9217813dd6fSViresh Kumar 			supply->u_volt_max, ret);
9227813dd6fSViresh Kumar 
9237813dd6fSViresh Kumar 	return ret;
9247813dd6fSViresh Kumar }
9257813dd6fSViresh Kumar 
9262083da24SViresh Kumar static int
9272083da24SViresh Kumar _opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
9282083da24SViresh Kumar 		       struct dev_pm_opp *opp, void *data, bool scaling_down)
9297813dd6fSViresh Kumar {
9301efae8d2SViresh Kumar 	unsigned long *target = data;
9311efae8d2SViresh Kumar 	unsigned long freq;
9327813dd6fSViresh Kumar 	int ret;
9337813dd6fSViresh Kumar 
9341efae8d2SViresh Kumar 	/* One of target and opp must be available */
9351efae8d2SViresh Kumar 	if (target) {
9361efae8d2SViresh Kumar 		freq = *target;
9371efae8d2SViresh Kumar 	} else if (opp) {
9382083da24SViresh Kumar 		freq = opp->rates[0];
9391efae8d2SViresh Kumar 	} else {
9401efae8d2SViresh Kumar 		WARN_ON(1);
9411efae8d2SViresh Kumar 		return -EINVAL;
9421efae8d2SViresh Kumar 	}
9431efae8d2SViresh Kumar 
9441efae8d2SViresh Kumar 	ret = clk_set_rate(opp_table->clk, freq);
9457813dd6fSViresh Kumar 	if (ret) {
9467813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
9477813dd6fSViresh Kumar 			ret);
9481efae8d2SViresh Kumar 	} else {
9491efae8d2SViresh Kumar 		opp_table->rate_clk_single = freq;
9507813dd6fSViresh Kumar 	}
9517813dd6fSViresh Kumar 
9527813dd6fSViresh Kumar 	return ret;
9537813dd6fSViresh Kumar }
9547813dd6fSViresh Kumar 
9558174a3a6SViresh Kumar /*
9568174a3a6SViresh Kumar  * Simple implementation for configuring multiple clocks. Configure clocks in
9578174a3a6SViresh Kumar  * the order in which they are present in the array while scaling up.
9588174a3a6SViresh Kumar  */
9598174a3a6SViresh Kumar int dev_pm_opp_config_clks_simple(struct device *dev,
9608174a3a6SViresh Kumar 		struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
9618174a3a6SViresh Kumar 		bool scaling_down)
9628174a3a6SViresh Kumar {
9638174a3a6SViresh Kumar 	int ret, i;
9648174a3a6SViresh Kumar 
9658174a3a6SViresh Kumar 	if (scaling_down) {
9668174a3a6SViresh Kumar 		for (i = opp_table->clk_count - 1; i >= 0; i--) {
9678174a3a6SViresh Kumar 			ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
9688174a3a6SViresh Kumar 			if (ret) {
9698174a3a6SViresh Kumar 				dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
9708174a3a6SViresh Kumar 					ret);
9718174a3a6SViresh Kumar 				return ret;
9728174a3a6SViresh Kumar 			}
9738174a3a6SViresh Kumar 		}
9748174a3a6SViresh Kumar 	} else {
9758174a3a6SViresh Kumar 		for (i = 0; i < opp_table->clk_count; i++) {
9768174a3a6SViresh Kumar 			ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
9778174a3a6SViresh Kumar 			if (ret) {
9788174a3a6SViresh Kumar 				dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
9798174a3a6SViresh Kumar 					ret);
9808174a3a6SViresh Kumar 				return ret;
9818174a3a6SViresh Kumar 			}
9828174a3a6SViresh Kumar 		}
9838174a3a6SViresh Kumar 	}
9848174a3a6SViresh Kumar 
985d36cb843SChristophe JAILLET 	return 0;
9868174a3a6SViresh Kumar }
9878174a3a6SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_config_clks_simple);
9888174a3a6SViresh Kumar 
989c522ce8aSViresh Kumar static int _opp_config_regulator_single(struct device *dev,
990c522ce8aSViresh Kumar 			struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
991c522ce8aSViresh Kumar 			struct regulator **regulators, unsigned int count)
9927813dd6fSViresh Kumar {
993c522ce8aSViresh Kumar 	struct regulator *reg = regulators[0];
9947813dd6fSViresh Kumar 	int ret;
9957813dd6fSViresh Kumar 
9967813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
997c522ce8aSViresh Kumar 	if (WARN_ON(count > 1)) {
9987813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
9997813dd6fSViresh Kumar 		return -EINVAL;
10007813dd6fSViresh Kumar 	}
10017813dd6fSViresh Kumar 
1002c522ce8aSViresh Kumar 	ret = _set_opp_voltage(dev, reg, new_opp->supplies);
10037813dd6fSViresh Kumar 	if (ret)
1004c522ce8aSViresh Kumar 		return ret;
10057813dd6fSViresh Kumar 
10068d45719cSKamil Konieczny 	/*
10078d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
10088d45719cSKamil Konieczny 	 * some boot-enabled regulators.
10098d45719cSKamil Konieczny 	 */
1010c522ce8aSViresh Kumar 	if (unlikely(!new_opp->opp_table->enabled)) {
10118d45719cSKamil Konieczny 		ret = regulator_enable(reg);
10128d45719cSKamil Konieczny 		if (ret < 0)
10138d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
10148d45719cSKamil Konieczny 	}
10158d45719cSKamil Konieczny 
10167813dd6fSViresh Kumar 	return 0;
10177813dd6fSViresh Kumar }
10187813dd6fSViresh Kumar 
1019b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
1020240ae50eSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev)
1021b00e667aSViresh Kumar {
1022b00e667aSViresh Kumar 	u32 avg, peak;
1023b00e667aSViresh Kumar 	int i, ret;
1024b00e667aSViresh Kumar 
1025b00e667aSViresh Kumar 	if (!opp_table->paths)
1026b00e667aSViresh Kumar 		return 0;
1027b00e667aSViresh Kumar 
1028b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
1029240ae50eSViresh Kumar 		if (!opp) {
1030b00e667aSViresh Kumar 			avg = 0;
1031b00e667aSViresh Kumar 			peak = 0;
1032b00e667aSViresh Kumar 		} else {
1033b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
1034b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
1035b00e667aSViresh Kumar 		}
1036b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
1037b00e667aSViresh Kumar 		if (ret) {
1038b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
1039240ae50eSViresh Kumar 				opp ? "set" : "remove", i, ret);
1040b00e667aSViresh Kumar 			return ret;
1041b00e667aSViresh Kumar 		}
1042b00e667aSViresh Kumar 	}
1043b00e667aSViresh Kumar 
1044b00e667aSViresh Kumar 	return 0;
1045b00e667aSViresh Kumar }
1046b00e667aSViresh Kumar 
1047528f2d8dSViresh Kumar static int _set_performance_state(struct device *dev, struct device *pd_dev,
104860cdeae0SStephan Gerhold 				  struct dev_pm_opp *opp, int i)
104960cdeae0SStephan Gerhold {
10507c41cdcdSViresh Kumar 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->level: 0;
105160cdeae0SStephan Gerhold 	int ret;
105260cdeae0SStephan Gerhold 
105360cdeae0SStephan Gerhold 	if (!pd_dev)
105460cdeae0SStephan Gerhold 		return 0;
105560cdeae0SStephan Gerhold 
105660cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
105760cdeae0SStephan Gerhold 	if (ret) {
10589bfb1fffSViresh Kumar 		dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
105960cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
106060cdeae0SStephan Gerhold 	}
106160cdeae0SStephan Gerhold 
106260cdeae0SStephan Gerhold 	return ret;
106360cdeae0SStephan Gerhold }
106460cdeae0SStephan Gerhold 
1065528f2d8dSViresh Kumar static int _opp_set_required_opps_generic(struct device *dev,
1066528f2d8dSViresh Kumar 	struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
1067ca1b5d77SViresh Kumar {
1068528f2d8dSViresh Kumar 	dev_err(dev, "setting required-opps isn't supported for non-genpd devices\n");
1069528f2d8dSViresh Kumar 	return -ENOENT;
1070528f2d8dSViresh Kumar }
1071528f2d8dSViresh Kumar 
1072528f2d8dSViresh Kumar static int _opp_set_required_opps_genpd(struct device *dev,
1073528f2d8dSViresh Kumar 	struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
1074528f2d8dSViresh Kumar {
107529b1a92eSViresh Kumar 	struct device **genpd_virt_devs =
107629b1a92eSViresh Kumar 		opp_table->genpd_virt_devs ? opp_table->genpd_virt_devs : &dev;
1077ca1b5d77SViresh Kumar 	int i, ret = 0;
1078ca1b5d77SViresh Kumar 
1079ca1b5d77SViresh Kumar 	/*
1080ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
1081ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
1082ca1b5d77SViresh Kumar 	 */
1083ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
1084ca1b5d77SViresh Kumar 
10852c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
1086528f2d8dSViresh Kumar 	if (!scaling_down) {
1087ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
1088528f2d8dSViresh Kumar 			ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
108960cdeae0SStephan Gerhold 			if (ret)
1090ca1b5d77SViresh Kumar 				break;
1091ca1b5d77SViresh Kumar 		}
10922c59138cSStephan Gerhold 	} else {
10932c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
1094528f2d8dSViresh Kumar 			ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
10952c59138cSStephan Gerhold 			if (ret)
1096ca1b5d77SViresh Kumar 				break;
1097ca1b5d77SViresh Kumar 		}
1098ca1b5d77SViresh Kumar 	}
10992c59138cSStephan Gerhold 
1100ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
1101ca1b5d77SViresh Kumar 
1102ca1b5d77SViresh Kumar 	return ret;
1103ca1b5d77SViresh Kumar }
1104ca1b5d77SViresh Kumar 
1105528f2d8dSViresh Kumar /* This is only called for PM domain for now */
1106528f2d8dSViresh Kumar static int _set_required_opps(struct device *dev, struct opp_table *opp_table,
1107528f2d8dSViresh Kumar 			      struct dev_pm_opp *opp, bool up)
1108528f2d8dSViresh Kumar {
1109528f2d8dSViresh Kumar 	/* required-opps not fully initialized yet */
1110528f2d8dSViresh Kumar 	if (lazy_linking_pending(opp_table))
1111528f2d8dSViresh Kumar 		return -EBUSY;
1112528f2d8dSViresh Kumar 
1113528f2d8dSViresh Kumar 	if (opp_table->set_required_opps)
1114528f2d8dSViresh Kumar 		return opp_table->set_required_opps(dev, opp_table, opp, up);
1115528f2d8dSViresh Kumar 
1116528f2d8dSViresh Kumar 	return 0;
1117528f2d8dSViresh Kumar }
1118528f2d8dSViresh Kumar 
1119528f2d8dSViresh Kumar /* Update set_required_opps handler */
1120528f2d8dSViresh Kumar void _update_set_required_opps(struct opp_table *opp_table)
1121528f2d8dSViresh Kumar {
1122528f2d8dSViresh Kumar 	/* Already set */
1123528f2d8dSViresh Kumar 	if (opp_table->set_required_opps)
1124528f2d8dSViresh Kumar 		return;
1125528f2d8dSViresh Kumar 
1126528f2d8dSViresh Kumar 	/* All required OPPs will belong to genpd or none */
1127528f2d8dSViresh Kumar 	if (opp_table->required_opp_tables[0]->is_genpd)
1128528f2d8dSViresh Kumar 		opp_table->set_required_opps = _opp_set_required_opps_genpd;
1129528f2d8dSViresh Kumar 	else
1130528f2d8dSViresh Kumar 		opp_table->set_required_opps = _opp_set_required_opps_generic;
1131528f2d8dSViresh Kumar }
1132528f2d8dSViresh Kumar 
113381c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
113481c4d8a3SViresh Kumar {
113581c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
113681c4d8a3SViresh Kumar 	unsigned long freq;
113781c4d8a3SViresh Kumar 
113881c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
113981c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
114081c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
114181c4d8a3SViresh Kumar 	}
114281c4d8a3SViresh Kumar 
114381c4d8a3SViresh Kumar 	/*
114481c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
114581c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
114681c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
114781c4d8a3SViresh Kumar 	 */
114881c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
114981c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
115081c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
115181c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
115281c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
115381c4d8a3SViresh Kumar 	}
115481c4d8a3SViresh Kumar 
115581c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
115681c4d8a3SViresh Kumar }
115781c4d8a3SViresh Kumar 
11585ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
1159f3364e17SViresh Kumar {
1160f3364e17SViresh Kumar 	int ret;
1161f3364e17SViresh Kumar 
1162f3364e17SViresh Kumar 	if (!opp_table->enabled)
1163f3364e17SViresh Kumar 		return 0;
1164f3364e17SViresh Kumar 
1165f3364e17SViresh Kumar 	/*
1166f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
1167f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
1168f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
1169f3364e17SViresh Kumar 	 */
1170f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
1171f3364e17SViresh Kumar 		return 0;
1172f3364e17SViresh Kumar 
1173240ae50eSViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev);
1174f3364e17SViresh Kumar 	if (ret)
1175f3364e17SViresh Kumar 		return ret;
1176f3364e17SViresh Kumar 
1177f3364e17SViresh Kumar 	if (opp_table->regulators)
1178f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
1179f3364e17SViresh Kumar 
11802c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
1181f3364e17SViresh Kumar 
1182f3364e17SViresh Kumar 	opp_table->enabled = false;
1183f3364e17SViresh Kumar 	return ret;
1184f3364e17SViresh Kumar }
1185f3364e17SViresh Kumar 
1186386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
11871efae8d2SViresh Kumar 		    struct dev_pm_opp *opp, void *clk_data, bool forced)
11887813dd6fSViresh Kumar {
1189386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
1190f0b88fa4SViresh Kumar 	int scaling_down, ret;
11917813dd6fSViresh Kumar 
1192386ba854SViresh Kumar 	if (unlikely(!opp))
1193386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
1194aca48b61SRajendra Nayak 
119581c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
119681c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
119781c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
11987813dd6fSViresh Kumar 
119981c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
120081c4d8a3SViresh Kumar 
120181c4d8a3SViresh Kumar 	/* Return early if nothing to do */
12021efae8d2SViresh Kumar 	if (!forced && old_opp == opp && opp_table->enabled) {
12039e28f7a7SAdrián Larumbe 		dev_dbg_ratelimited(dev, "%s: OPPs are same, nothing to do\n", __func__);
1204386ba854SViresh Kumar 		return 0;
12057813dd6fSViresh Kumar 	}
12067813dd6fSViresh Kumar 
1207f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
12082083da24SViresh Kumar 		__func__, old_opp->rates[0], opp->rates[0], old_opp->level,
12092083da24SViresh Kumar 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1210f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1211f0b88fa4SViresh Kumar 
12122083da24SViresh Kumar 	scaling_down = _opp_compare_key(opp_table, old_opp, opp);
1213f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1214f0b88fa4SViresh Kumar 		scaling_down = 0;
12157813dd6fSViresh Kumar 
1216ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1217f0b88fa4SViresh Kumar 	if (!scaling_down) {
12182c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1219870d5d96SViresh Kumar 		if (ret) {
1220870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1221386ba854SViresh Kumar 			return ret;
1222ca1b5d77SViresh Kumar 		}
1223ca1b5d77SViresh Kumar 
1224870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1225870d5d96SViresh Kumar 		if (ret) {
1226870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1227870d5d96SViresh Kumar 			return ret;
1228870d5d96SViresh Kumar 		}
1229aee3352fSViresh Kumar 
1230aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1231aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1232aee3352fSViresh Kumar 							   opp_table->regulators,
1233aee3352fSViresh Kumar 							   opp_table->regulator_count);
1234aee3352fSViresh Kumar 			if (ret) {
1235aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1236aee3352fSViresh Kumar 					ret);
1237aee3352fSViresh Kumar 				return ret;
1238aee3352fSViresh Kumar 			}
1239aee3352fSViresh Kumar 		}
1240870d5d96SViresh Kumar 	}
1241870d5d96SViresh Kumar 
12422083da24SViresh Kumar 	if (opp_table->config_clks) {
12432083da24SViresh Kumar 		ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
1244ca1b5d77SViresh Kumar 		if (ret)
1245870d5d96SViresh Kumar 			return ret;
12462083da24SViresh Kumar 	}
1247870d5d96SViresh Kumar 
1248870d5d96SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1249870d5d96SViresh Kumar 	if (scaling_down) {
1250aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1251aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1252aee3352fSViresh Kumar 							   opp_table->regulators,
1253aee3352fSViresh Kumar 							   opp_table->regulator_count);
1254aee3352fSViresh Kumar 			if (ret) {
1255aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1256aee3352fSViresh Kumar 					ret);
1257aee3352fSViresh Kumar 				return ret;
1258aee3352fSViresh Kumar 			}
1259aee3352fSViresh Kumar 		}
1260aee3352fSViresh Kumar 
1261870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1262870d5d96SViresh Kumar 		if (ret) {
1263870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1264870d5d96SViresh Kumar 			return ret;
1265ca1b5d77SViresh Kumar 		}
1266ca1b5d77SViresh Kumar 
1267870d5d96SViresh Kumar 		ret = _set_required_opps(dev, opp_table, opp, false);
1268870d5d96SViresh Kumar 		if (ret) {
1269870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1270870d5d96SViresh Kumar 			return ret;
1271870d5d96SViresh Kumar 		}
1272870d5d96SViresh Kumar 	}
1273870d5d96SViresh Kumar 
127472f80ce4SViresh Kumar 	opp_table->enabled = true;
127581c4d8a3SViresh Kumar 	dev_pm_opp_put(old_opp);
127681c4d8a3SViresh Kumar 
127781c4d8a3SViresh Kumar 	/* Make sure current_opp doesn't get freed */
127881c4d8a3SViresh Kumar 	dev_pm_opp_get(opp);
127981c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
1280fe2af402SGeorgi Djakov 
1281386ba854SViresh Kumar 	return ret;
1282386ba854SViresh Kumar }
1283386ba854SViresh Kumar 
1284386ba854SViresh Kumar /**
1285386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1286386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1287386ba854SViresh Kumar  * @target_freq: frequency to achieve
1288386ba854SViresh Kumar  *
1289386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1290386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1291386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1292386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1293386ba854SViresh Kumar  * frequency.
1294386ba854SViresh Kumar  */
1295386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1296386ba854SViresh Kumar {
1297386ba854SViresh Kumar 	struct opp_table *opp_table;
1298386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1299386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
13001efae8d2SViresh Kumar 	bool forced = false;
1301386ba854SViresh Kumar 	int ret;
1302386ba854SViresh Kumar 
1303386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1304386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1305386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1306386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1307386ba854SViresh Kumar 	}
1308386ba854SViresh Kumar 
1309386ba854SViresh Kumar 	if (target_freq) {
1310386ba854SViresh Kumar 		/*
1311386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1312386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1313386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1314386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1315386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1316386ba854SViresh Kumar 		 */
1317386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
13182083da24SViresh Kumar 			ret = opp_table->config_clks(dev, opp_table, NULL,
13192083da24SViresh Kumar 						     &target_freq, false);
1320386ba854SViresh Kumar 			goto put_opp_table;
1321386ba854SViresh Kumar 		}
1322386ba854SViresh Kumar 
1323386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1324386ba854SViresh Kumar 		if ((long)freq <= 0)
1325386ba854SViresh Kumar 			freq = target_freq;
1326386ba854SViresh Kumar 
1327386ba854SViresh Kumar 		/*
1328386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1329386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1330386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1331386ba854SViresh Kumar 		 */
1332386ba854SViresh Kumar 		temp_freq = freq;
1333386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1334386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1335386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1336386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1337386ba854SViresh Kumar 				__func__, freq, ret);
1338386ba854SViresh Kumar 			goto put_opp_table;
1339386ba854SViresh Kumar 		}
13401efae8d2SViresh Kumar 
13411efae8d2SViresh Kumar 		/*
13421efae8d2SViresh Kumar 		 * An OPP entry specifies the highest frequency at which other
13431efae8d2SViresh Kumar 		 * properties of the OPP entry apply. Even if the new OPP is
13441efae8d2SViresh Kumar 		 * same as the old one, we may still reach here for a different
13451efae8d2SViresh Kumar 		 * value of the frequency. In such a case, do not abort but
13461efae8d2SViresh Kumar 		 * configure the hardware to the desired frequency forcefully.
13471efae8d2SViresh Kumar 		 */
13481efae8d2SViresh Kumar 		forced = opp_table->rate_clk_single != target_freq;
1349386ba854SViresh Kumar 	}
1350386ba854SViresh Kumar 
13511efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, &target_freq, forced);
1352386ba854SViresh Kumar 
1353386ba854SViresh Kumar 	if (target_freq)
13547813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
13551efae8d2SViresh Kumar 
13567813dd6fSViresh Kumar put_opp_table:
13577813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
13587813dd6fSViresh Kumar 	return ret;
13597813dd6fSViresh Kumar }
13607813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
13617813dd6fSViresh Kumar 
1362abbe3483SViresh Kumar /**
1363abbe3483SViresh Kumar  * dev_pm_opp_set_opp() - Configure device for OPP
1364abbe3483SViresh Kumar  * @dev: device for which we do this operation
1365abbe3483SViresh Kumar  * @opp: OPP to set to
1366abbe3483SViresh Kumar  *
1367abbe3483SViresh Kumar  * This configures the device based on the properties of the OPP passed to this
1368abbe3483SViresh Kumar  * routine.
1369abbe3483SViresh Kumar  *
1370abbe3483SViresh Kumar  * Return: 0 on success, a negative error number otherwise.
1371abbe3483SViresh Kumar  */
1372abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1373abbe3483SViresh Kumar {
1374abbe3483SViresh Kumar 	struct opp_table *opp_table;
1375abbe3483SViresh Kumar 	int ret;
1376abbe3483SViresh Kumar 
1377abbe3483SViresh Kumar 	opp_table = _find_opp_table(dev);
1378abbe3483SViresh Kumar 	if (IS_ERR(opp_table)) {
1379abbe3483SViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1380abbe3483SViresh Kumar 		return PTR_ERR(opp_table);
1381abbe3483SViresh Kumar 	}
1382abbe3483SViresh Kumar 
13831efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, NULL, false);
1384abbe3483SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1385abbe3483SViresh Kumar 
1386abbe3483SViresh Kumar 	return ret;
1387abbe3483SViresh Kumar }
1388abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1389abbe3483SViresh Kumar 
13907813dd6fSViresh Kumar /* OPP-dev Helpers */
13917813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
13927813dd6fSViresh Kumar 			    struct opp_table *opp_table)
13937813dd6fSViresh Kumar {
13947813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
13957813dd6fSViresh Kumar 	list_del(&opp_dev->node);
13967813dd6fSViresh Kumar 	kfree(opp_dev);
13977813dd6fSViresh Kumar }
13987813dd6fSViresh Kumar 
1399ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
14007813dd6fSViresh Kumar 				struct opp_table *opp_table)
14017813dd6fSViresh Kumar {
14027813dd6fSViresh Kumar 	struct opp_device *opp_dev;
14037813dd6fSViresh Kumar 
14047813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
14057813dd6fSViresh Kumar 	if (!opp_dev)
14067813dd6fSViresh Kumar 		return NULL;
14077813dd6fSViresh Kumar 
14087813dd6fSViresh Kumar 	/* Initialize opp-dev */
14097813dd6fSViresh Kumar 	opp_dev->dev = dev;
14103d255699SViresh Kumar 
1411ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
14127813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1413ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
14147813dd6fSViresh Kumar 
14157813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1416a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1417283d55e6SViresh Kumar 
1418283d55e6SViresh Kumar 	return opp_dev;
1419283d55e6SViresh Kumar }
1420283d55e6SViresh Kumar 
1421eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
14227813dd6fSViresh Kumar {
14237813dd6fSViresh Kumar 	struct opp_table *opp_table;
14247813dd6fSViresh Kumar 	struct opp_device *opp_dev;
14257813dd6fSViresh Kumar 	int ret;
14267813dd6fSViresh Kumar 
14277813dd6fSViresh Kumar 	/*
14287813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
14297813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
14307813dd6fSViresh Kumar 	 */
14317813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
14327813dd6fSViresh Kumar 	if (!opp_table)
1433dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
14347813dd6fSViresh Kumar 
14353d255699SViresh Kumar 	mutex_init(&opp_table->lock);
14364f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
14377813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
14387eba0c76SViresh Kumar 	INIT_LIST_HEAD(&opp_table->lazy);
14397813dd6fSViresh Kumar 
14402083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
14412083da24SViresh Kumar 
144246f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
144346f48acaSViresh Kumar 	opp_table->regulator_count = -1;
144446f48acaSViresh Kumar 
14457813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
14467813dd6fSViresh Kumar 	if (!opp_dev) {
1447dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1448dd461cd9SStephan Gerhold 		goto err;
14497813dd6fSViresh Kumar 	}
14507813dd6fSViresh Kumar 
1451eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
14527813dd6fSViresh Kumar 
14536d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
14546d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1455dd461cd9SStephan Gerhold 	if (ret) {
1456dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
145732439ac7SViresh Kumar 			goto remove_opp_dev;
1458dd461cd9SStephan Gerhold 
14596d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
14606d3f922cSGeorgi Djakov 			 __func__, ret);
1461dd461cd9SStephan Gerhold 	}
14626d3f922cSGeorgi Djakov 
14637813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
14647813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
14657813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
14667813dd6fSViresh Kumar 
14677813dd6fSViresh Kumar 	return opp_table;
1468dd461cd9SStephan Gerhold 
1469976509bbSQuanyang Wang remove_opp_dev:
1470b2a2ab03SStephan Gerhold 	_of_clear_opp_table(opp_table);
1471976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1472b2a2ab03SStephan Gerhold 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
1473b2a2ab03SStephan Gerhold 	mutex_destroy(&opp_table->lock);
1474dd461cd9SStephan Gerhold err:
1475dd461cd9SStephan Gerhold 	kfree(opp_table);
1476dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
14777813dd6fSViresh Kumar }
14787813dd6fSViresh Kumar 
14797813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
14807813dd6fSViresh Kumar {
14817813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
14827813dd6fSViresh Kumar }
14837813dd6fSViresh Kumar 
148432439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
148532439ac7SViresh Kumar 					       struct opp_table *opp_table,
148632439ac7SViresh Kumar 					       bool getclk)
148732439ac7SViresh Kumar {
1488d4a4c7a4SViresh Kumar 	int ret;
1489d4a4c7a4SViresh Kumar 
149032439ac7SViresh Kumar 	/*
14912083da24SViresh Kumar 	 * Return early if we don't need to get clk or we have already done it
149232439ac7SViresh Kumar 	 * earlier.
149332439ac7SViresh Kumar 	 */
14942083da24SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
14952083da24SViresh Kumar 	    opp_table->clks)
149632439ac7SViresh Kumar 		return opp_table;
149732439ac7SViresh Kumar 
149832439ac7SViresh Kumar 	/* Find clk for the device */
149932439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
150032439ac7SViresh Kumar 
1501d4a4c7a4SViresh Kumar 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
15022083da24SViresh Kumar 	if (!ret) {
15032083da24SViresh Kumar 		opp_table->config_clks = _opp_config_clk_single;
15042083da24SViresh Kumar 		opp_table->clk_count = 1;
150532439ac7SViresh Kumar 		return opp_table;
15062083da24SViresh Kumar 	}
1507d4a4c7a4SViresh Kumar 
1508d4a4c7a4SViresh Kumar 	if (ret == -ENOENT) {
15092083da24SViresh Kumar 		/*
15102083da24SViresh Kumar 		 * There are few platforms which don't want the OPP core to
15112083da24SViresh Kumar 		 * manage device's clock settings. In such cases neither the
15122083da24SViresh Kumar 		 * platform provides the clks explicitly to us, nor the DT
15132083da24SViresh Kumar 		 * contains a valid clk entry. The OPP nodes in DT may still
15142083da24SViresh Kumar 		 * contain "opp-hz" property though, which we need to parse and
15152083da24SViresh Kumar 		 * allow the platform to find an OPP based on freq later on.
15162083da24SViresh Kumar 		 *
15172083da24SViresh Kumar 		 * This is a simple solution to take care of such corner cases,
15182083da24SViresh Kumar 		 * i.e. make the clk_count 1, which lets us allocate space for
15192083da24SViresh Kumar 		 * frequency in opp->rates and also parse the entries in DT.
15202083da24SViresh Kumar 		 */
15212083da24SViresh Kumar 		opp_table->clk_count = 1;
15222083da24SViresh Kumar 
1523d4a4c7a4SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1524d4a4c7a4SViresh Kumar 		return opp_table;
1525d4a4c7a4SViresh Kumar 	}
1526d4a4c7a4SViresh Kumar 
1527d4a4c7a4SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1528d4a4c7a4SViresh Kumar 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1529d4a4c7a4SViresh Kumar 
1530d4a4c7a4SViresh Kumar 	return ERR_PTR(ret);
153132439ac7SViresh Kumar }
153232439ac7SViresh Kumar 
153327c09484SViresh Kumar /*
153427c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
153527c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
153627c09484SViresh Kumar  *
153727c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
153827c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
153927c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
154027c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
154127c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
154227c09484SViresh Kumar  * indirect users of OPP core.
154327c09484SViresh Kumar  *
154427c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
154527c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
154627c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
154727c09484SViresh Kumar  */
154832439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
154932439ac7SViresh Kumar 					 bool getclk)
15507813dd6fSViresh Kumar {
15517813dd6fSViresh Kumar 	struct opp_table *opp_table;
15527813dd6fSViresh Kumar 
155327c09484SViresh Kumar again:
15547813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
15557813dd6fSViresh Kumar 
15567813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
15577813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
15587813dd6fSViresh Kumar 		goto unlock;
15597813dd6fSViresh Kumar 
156027c09484SViresh Kumar 	/*
156127c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
156227c09484SViresh Kumar 	 * another user, wait for it to finish.
156327c09484SViresh Kumar 	 */
156427c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
156527c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
156627c09484SViresh Kumar 		cpu_relax();
156727c09484SViresh Kumar 		goto again;
156827c09484SViresh Kumar 	}
156927c09484SViresh Kumar 
157027c09484SViresh Kumar 	opp_tables_busy = true;
1571283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
157227c09484SViresh Kumar 
157327c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
157427c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
157527c09484SViresh Kumar 
1576283d55e6SViresh Kumar 	if (opp_table) {
1577ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1578283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1579dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1580283d55e6SViresh Kumar 		}
158127c09484SViresh Kumar 
158227c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
158327c09484SViresh Kumar 	} else {
158427c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
158527c09484SViresh Kumar 
158627c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
158727c09484SViresh Kumar 		if (!IS_ERR(opp_table))
158827c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1589283d55e6SViresh Kumar 	}
1590283d55e6SViresh Kumar 
159127c09484SViresh Kumar 	opp_tables_busy = false;
15927813dd6fSViresh Kumar 
15937813dd6fSViresh Kumar unlock:
15947813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
15957813dd6fSViresh Kumar 
159632439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
15977813dd6fSViresh Kumar }
1598eb7c8743SViresh Kumar 
159932439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1600e77dcb0bSViresh Kumar {
160132439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1602e77dcb0bSViresh Kumar }
1603e77dcb0bSViresh Kumar 
1604eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1605eb7c8743SViresh Kumar {
1606e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1607eb7c8743SViresh Kumar }
16087813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
16097813dd6fSViresh Kumar 
16107813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
16117813dd6fSViresh Kumar {
16127813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1613cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
16146d3f922cSGeorgi Djakov 	int i;
16157813dd6fSViresh Kumar 
1616e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1617e0df59deSViresh Kumar 	list_del(&opp_table->node);
1618e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1619e0df59deSViresh Kumar 
162081c4d8a3SViresh Kumar 	if (opp_table->current_opp)
162181c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
162281c4d8a3SViresh Kumar 
16235d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
16245d6d106fSViresh Kumar 
16252083da24SViresh Kumar 	/* Release automatically acquired single clk */
16267813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
16277813dd6fSViresh Kumar 		clk_put(opp_table->clk);
16287813dd6fSViresh Kumar 
16296d3f922cSGeorgi Djakov 	if (opp_table->paths) {
16306d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
16316d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
16326d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
16336d3f922cSGeorgi Djakov 	}
16346d3f922cSGeorgi Djakov 
1635cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1636cdd6ed90SViresh Kumar 
163704bd2eafSViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node)
16387813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
16397813dd6fSViresh Kumar 
16404f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
16417813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
16427813dd6fSViresh Kumar 	kfree(opp_table);
16437813dd6fSViresh Kumar }
16447813dd6fSViresh Kumar 
16457813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
16467813dd6fSViresh Kumar {
16477813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
16487813dd6fSViresh Kumar 		       &opp_table_lock);
16497813dd6fSViresh Kumar }
16507813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
16517813dd6fSViresh Kumar 
16527813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
16537813dd6fSViresh Kumar {
16547813dd6fSViresh Kumar 	kfree(opp);
16557813dd6fSViresh Kumar }
16567813dd6fSViresh Kumar 
1657cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
16587813dd6fSViresh Kumar {
1659cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1660cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1661cf1fac94SViresh Kumar 
1662cf1fac94SViresh Kumar 	list_del(&opp->node);
1663cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1664cf1fac94SViresh Kumar 
16657813dd6fSViresh Kumar 	/*
16667813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
16677813dd6fSViresh Kumar 	 * frequency/voltage list.
16687813dd6fSViresh Kumar 	 */
16697813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
16703466ea2cSLiang He 	_of_clear_opp(opp_table, opp);
16717813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
16727813dd6fSViresh Kumar 	kfree(opp);
16731690d8bbSViresh Kumar }
16747813dd6fSViresh Kumar 
1675a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
16767813dd6fSViresh Kumar {
16777813dd6fSViresh Kumar 	kref_get(&opp->kref);
16787813dd6fSViresh Kumar }
16797813dd6fSViresh Kumar 
16807813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
16817813dd6fSViresh Kumar {
1682cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
16837813dd6fSViresh Kumar }
16847813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
16857813dd6fSViresh Kumar 
16867813dd6fSViresh Kumar /**
16877813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
16887813dd6fSViresh Kumar  * @dev:	device for which we do this operation
16897813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
16907813dd6fSViresh Kumar  *
16917813dd6fSViresh Kumar  * This function removes an opp from the opp table.
16927813dd6fSViresh Kumar  */
16937813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
16947813dd6fSViresh Kumar {
169595073b72SJakob Koschel 	struct dev_pm_opp *opp = NULL, *iter;
16967813dd6fSViresh Kumar 	struct opp_table *opp_table;
16977813dd6fSViresh Kumar 
16987813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
16997813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
17007813dd6fSViresh Kumar 		return;
17017813dd6fSViresh Kumar 
1702f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table))
1703f123ea74SViresh Kumar 		goto put_table;
1704f123ea74SViresh Kumar 
17057813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
17067813dd6fSViresh Kumar 
170795073b72SJakob Koschel 	list_for_each_entry(iter, &opp_table->opp_list, node) {
17082083da24SViresh Kumar 		if (iter->rates[0] == freq) {
170995073b72SJakob Koschel 			opp = iter;
17107813dd6fSViresh Kumar 			break;
17117813dd6fSViresh Kumar 		}
17127813dd6fSViresh Kumar 	}
17137813dd6fSViresh Kumar 
17147813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
17157813dd6fSViresh Kumar 
171695073b72SJakob Koschel 	if (opp) {
17177813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
17180ad8c623SViresh Kumar 
17190ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
17200ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
17217813dd6fSViresh Kumar 	} else {
17227813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
17237813dd6fSViresh Kumar 			 __func__, freq);
17247813dd6fSViresh Kumar 	}
17257813dd6fSViresh Kumar 
1726f123ea74SViresh Kumar put_table:
17270ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
17287813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17297813dd6fSViresh Kumar }
17307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
17317813dd6fSViresh Kumar 
1732cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1733cf1fac94SViresh Kumar 					bool dynamic)
1734cf1fac94SViresh Kumar {
1735cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1736cf1fac94SViresh Kumar 
1737cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1738cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1739606a5d42SBeata Michalska 		/*
1740606a5d42SBeata Michalska 		 * Refcount must be dropped only once for each OPP by OPP core,
1741606a5d42SBeata Michalska 		 * do that with help of "removed" flag.
1742606a5d42SBeata Michalska 		 */
1743606a5d42SBeata Michalska 		if (!temp->removed && dynamic == temp->dynamic) {
1744cf1fac94SViresh Kumar 			opp = temp;
1745cf1fac94SViresh Kumar 			break;
1746cf1fac94SViresh Kumar 		}
1747cf1fac94SViresh Kumar 	}
1748cf1fac94SViresh Kumar 
1749cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1750cf1fac94SViresh Kumar 	return opp;
1751cf1fac94SViresh Kumar }
1752cf1fac94SViresh Kumar 
1753606a5d42SBeata Michalska /*
1754606a5d42SBeata Michalska  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1755606a5d42SBeata Michalska  * happen lock less to avoid circular dependency issues. This routine must be
1756606a5d42SBeata Michalska  * called without the opp_table->lock held.
1757606a5d42SBeata Michalska  */
1758606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
175903758d60SViresh Kumar {
1760cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
176103758d60SViresh Kumar 
1762606a5d42SBeata Michalska 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1763606a5d42SBeata Michalska 		opp->removed = true;
1764606a5d42SBeata Michalska 		dev_pm_opp_put(opp);
1765606a5d42SBeata Michalska 
1766606a5d42SBeata Michalska 		/* Drop the references taken by dev_pm_opp_add() */
1767606a5d42SBeata Michalska 		if (dynamic)
1768606a5d42SBeata Michalska 			dev_pm_opp_put_opp_table(opp_table);
1769606a5d42SBeata Michalska 	}
1770606a5d42SBeata Michalska }
1771606a5d42SBeata Michalska 
1772606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table)
1773606a5d42SBeata Michalska {
177403758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
177503758d60SViresh Kumar 
1776922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1777cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1778cf1fac94SViresh Kumar 		return false;
1779922ff075SViresh Kumar 	}
1780922ff075SViresh Kumar 
1781cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1782cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1783cf1fac94SViresh Kumar 		return true;
178403758d60SViresh Kumar 	}
178503758d60SViresh Kumar 
178603758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1787922ff075SViresh Kumar 
1788606a5d42SBeata Michalska 	_opp_remove_all(opp_table, false);
1789cf1fac94SViresh Kumar 	return true;
179003758d60SViresh Kumar }
179103758d60SViresh Kumar 
17921690d8bbSViresh Kumar /**
17931690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
17941690d8bbSViresh Kumar  * @dev:	device for which we do this operation
17951690d8bbSViresh Kumar  *
17961690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
17971690d8bbSViresh Kumar  */
17981690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
17991690d8bbSViresh Kumar {
18001690d8bbSViresh Kumar 	struct opp_table *opp_table;
18011690d8bbSViresh Kumar 
18021690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
18031690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
18041690d8bbSViresh Kumar 		return;
18051690d8bbSViresh Kumar 
1806606a5d42SBeata Michalska 	_opp_remove_all(opp_table, true);
18071690d8bbSViresh Kumar 
18081690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
18091690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
18101690d8bbSViresh Kumar }
18111690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
18121690d8bbSViresh Kumar 
1813d6134583SViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table)
18147813dd6fSViresh Kumar {
18157813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
18162083da24SViresh Kumar 	int supply_count, supply_size, icc_size, clk_size;
18177813dd6fSViresh Kumar 
18187813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
1819d6134583SViresh Kumar 	supply_count = opp_table->regulator_count > 0 ?
1820d6134583SViresh Kumar 			opp_table->regulator_count : 1;
18216d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
18222083da24SViresh Kumar 	clk_size = sizeof(*opp->rates) * opp_table->clk_count;
1823d6134583SViresh Kumar 	icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
18247813dd6fSViresh Kumar 
18257813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
18262083da24SViresh Kumar 	opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL);
18277813dd6fSViresh Kumar 	if (!opp)
18287813dd6fSViresh Kumar 		return NULL;
18297813dd6fSViresh Kumar 
18302083da24SViresh Kumar 	/* Put the supplies, bw and clock at the end of the OPP structure */
18317813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
18322083da24SViresh Kumar 
18332083da24SViresh Kumar 	opp->rates = (unsigned long *)(opp->supplies + supply_count);
18342083da24SViresh Kumar 
18356d3f922cSGeorgi Djakov 	if (icc_size)
18362083da24SViresh Kumar 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
18372083da24SViresh Kumar 
18387813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
18397813dd6fSViresh Kumar 
18407813dd6fSViresh Kumar 	return opp;
18417813dd6fSViresh Kumar }
18427813dd6fSViresh Kumar 
18437813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
18447813dd6fSViresh Kumar 					 struct opp_table *opp_table)
18457813dd6fSViresh Kumar {
18467813dd6fSViresh Kumar 	struct regulator *reg;
18477813dd6fSViresh Kumar 	int i;
18487813dd6fSViresh Kumar 
184990e3577bSViresh Kumar 	if (!opp_table->regulators)
185090e3577bSViresh Kumar 		return true;
185190e3577bSViresh Kumar 
18527813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
18537813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
18547813dd6fSViresh Kumar 
18557813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
18567813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
18577813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
18587813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
18597813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
18607813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
18617813dd6fSViresh Kumar 			return false;
18627813dd6fSViresh Kumar 		}
18637813dd6fSViresh Kumar 	}
18647813dd6fSViresh Kumar 
18657813dd6fSViresh Kumar 	return true;
18667813dd6fSViresh Kumar }
18677813dd6fSViresh Kumar 
18682083da24SViresh Kumar static int _opp_compare_rate(struct opp_table *opp_table,
18692083da24SViresh Kumar 			     struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
18702083da24SViresh Kumar {
18712083da24SViresh Kumar 	int i;
18722083da24SViresh Kumar 
18732083da24SViresh Kumar 	for (i = 0; i < opp_table->clk_count; i++) {
18742083da24SViresh Kumar 		if (opp1->rates[i] != opp2->rates[i])
18752083da24SViresh Kumar 			return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
18762083da24SViresh Kumar 	}
18772083da24SViresh Kumar 
18782083da24SViresh Kumar 	/* Same rates for both OPPs */
18792083da24SViresh Kumar 	return 0;
18802083da24SViresh Kumar }
18812083da24SViresh Kumar 
1882274c3e83SViresh Kumar static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1883274c3e83SViresh Kumar 			   struct dev_pm_opp *opp2)
1884274c3e83SViresh Kumar {
1885274c3e83SViresh Kumar 	int i;
1886274c3e83SViresh Kumar 
1887274c3e83SViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
1888274c3e83SViresh Kumar 		if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
1889274c3e83SViresh Kumar 			return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
1890274c3e83SViresh Kumar 	}
1891274c3e83SViresh Kumar 
1892274c3e83SViresh Kumar 	/* Same bw for both OPPs */
1893274c3e83SViresh Kumar 	return 0;
1894274c3e83SViresh Kumar }
1895274c3e83SViresh Kumar 
18968bdac14bSViresh Kumar /*
18978bdac14bSViresh Kumar  * Returns
18988bdac14bSViresh Kumar  * 0: opp1 == opp2
18998bdac14bSViresh Kumar  * 1: opp1 > opp2
19008bdac14bSViresh Kumar  * -1: opp1 < opp2
19018bdac14bSViresh Kumar  */
19022083da24SViresh Kumar int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1,
19032083da24SViresh Kumar 		     struct dev_pm_opp *opp2)
19046c591eecSSaravana Kannan {
19052083da24SViresh Kumar 	int ret;
19062083da24SViresh Kumar 
19072083da24SViresh Kumar 	ret = _opp_compare_rate(opp_table, opp1, opp2);
19082083da24SViresh Kumar 	if (ret)
19092083da24SViresh Kumar 		return ret;
19102083da24SViresh Kumar 
1911274c3e83SViresh Kumar 	ret = _opp_compare_bw(opp_table, opp1, opp2);
1912274c3e83SViresh Kumar 	if (ret)
1913274c3e83SViresh Kumar 		return ret;
19142083da24SViresh Kumar 
19156c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
19166c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
19172083da24SViresh Kumar 
19182083da24SViresh Kumar 	/* Duplicate OPPs */
19196c591eecSSaravana Kannan 	return 0;
19206c591eecSSaravana Kannan }
19216c591eecSSaravana Kannan 
1922a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1923a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1924a1e8c136SViresh Kumar 			     struct list_head **head)
1925a1e8c136SViresh Kumar {
1926a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
19276c591eecSSaravana Kannan 	int opp_cmp;
1928a1e8c136SViresh Kumar 
1929a1e8c136SViresh Kumar 	/*
1930a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1931a1e8c136SViresh Kumar 	 * already present.
1932a1e8c136SViresh Kumar 	 *
1933a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1934a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1935a1e8c136SViresh Kumar 	 * loop.
1936a1e8c136SViresh Kumar 	 */
1937a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
19382083da24SViresh Kumar 		opp_cmp = _opp_compare_key(opp_table, new_opp, opp);
19396c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1940a1e8c136SViresh Kumar 			*head = &opp->node;
1941a1e8c136SViresh Kumar 			continue;
1942a1e8c136SViresh Kumar 		}
1943a1e8c136SViresh Kumar 
19446c591eecSSaravana Kannan 		if (opp_cmp < 0)
1945a1e8c136SViresh Kumar 			return 0;
1946a1e8c136SViresh Kumar 
1947a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1948a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
19492083da24SViresh Kumar 			 __func__, opp->rates[0], opp->supplies[0].u_volt,
19502083da24SViresh Kumar 			 opp->available, new_opp->rates[0],
1951a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1952a1e8c136SViresh Kumar 
1953a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1954a1e8c136SViresh Kumar 		return opp->available &&
1955a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1956a1e8c136SViresh Kumar 	}
1957a1e8c136SViresh Kumar 
1958a1e8c136SViresh Kumar 	return 0;
1959a1e8c136SViresh Kumar }
1960a1e8c136SViresh Kumar 
19617eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count)
19627eba0c76SViresh Kumar {
19637eba0c76SViresh Kumar 	int i;
19647eba0c76SViresh Kumar 
19657eba0c76SViresh Kumar 	for (i = 0; i < count; i++) {
19667eba0c76SViresh Kumar 		if (opp->required_opps[i]->available)
19677eba0c76SViresh Kumar 			continue;
19687eba0c76SViresh Kumar 
19697eba0c76SViresh Kumar 		opp->available = false;
19707eba0c76SViresh Kumar 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
19712083da24SViresh Kumar 			 __func__, opp->required_opps[i]->np, opp->rates[0]);
19727eba0c76SViresh Kumar 		return;
19737eba0c76SViresh Kumar 	}
19747eba0c76SViresh Kumar }
19757eba0c76SViresh Kumar 
19767813dd6fSViresh Kumar /*
19777813dd6fSViresh Kumar  * Returns:
19787813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
19797813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
19807813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
19817813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
19827813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
19837813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
19847813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
19857813dd6fSViresh Kumar  */
19867813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
19874768914bSViresh Kumar 	     struct opp_table *opp_table)
19887813dd6fSViresh Kumar {
19897813dd6fSViresh Kumar 	struct list_head *head;
19907813dd6fSViresh Kumar 	int ret;
19917813dd6fSViresh Kumar 
19927813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
19937813dd6fSViresh Kumar 	head = &opp_table->opp_list;
19947813dd6fSViresh Kumar 
1995a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1996a1e8c136SViresh Kumar 	if (ret) {
19977813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
19987813dd6fSViresh Kumar 		return ret;
19997813dd6fSViresh Kumar 	}
20007813dd6fSViresh Kumar 
20017813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
20027813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
20037813dd6fSViresh Kumar 
20047813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
20057813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
20067813dd6fSViresh Kumar 
2007a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
20087813dd6fSViresh Kumar 
20097813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
20107813dd6fSViresh Kumar 		new_opp->available = false;
20117813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
20122083da24SViresh Kumar 			 __func__, new_opp->rates[0]);
20137813dd6fSViresh Kumar 	}
20147813dd6fSViresh Kumar 
20157eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
20167eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
20177eba0c76SViresh Kumar 		return 0;
2018cf65948dSDmitry Osipenko 
20197eba0c76SViresh Kumar 	_required_opps_available(new_opp, opp_table->required_opp_count);
2020cf65948dSDmitry Osipenko 
20217813dd6fSViresh Kumar 	return 0;
20227813dd6fSViresh Kumar }
20237813dd6fSViresh Kumar 
20247813dd6fSViresh Kumar /**
20257813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
20267813dd6fSViresh Kumar  * @opp_table:	OPP table
20277813dd6fSViresh Kumar  * @dev:	device for which we do this operation
20287813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
20297813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
20307813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
20317813dd6fSViresh Kumar  *
20327813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
20337813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
20347813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
20357813dd6fSViresh Kumar  *
20367813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
20377813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
20387813dd6fSViresh Kumar  *
20397813dd6fSViresh Kumar  * Return:
20407813dd6fSViresh Kumar  * 0		On success OR
20417813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
20427813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
20437813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
20447813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
20457813dd6fSViresh Kumar  */
20467813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
20477813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
20487813dd6fSViresh Kumar {
20497813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
20507813dd6fSViresh Kumar 	unsigned long tol;
20517813dd6fSViresh Kumar 	int ret;
20527813dd6fSViresh Kumar 
2053f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table))
2054f123ea74SViresh Kumar 		return -EINVAL;
2055f123ea74SViresh Kumar 
20567813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
20577813dd6fSViresh Kumar 	if (!new_opp)
20587813dd6fSViresh Kumar 		return -ENOMEM;
20597813dd6fSViresh Kumar 
20607813dd6fSViresh Kumar 	/* populate the opp table */
20612083da24SViresh Kumar 	new_opp->rates[0] = freq;
20627813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
20637813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
20647813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
20657813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
20667813dd6fSViresh Kumar 	new_opp->available = true;
20677813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
20687813dd6fSViresh Kumar 
20694768914bSViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table);
20707813dd6fSViresh Kumar 	if (ret) {
20717813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
20727813dd6fSViresh Kumar 		if (ret == -EBUSY)
20737813dd6fSViresh Kumar 			ret = 0;
20747813dd6fSViresh Kumar 		goto free_opp;
20757813dd6fSViresh Kumar 	}
20767813dd6fSViresh Kumar 
20777813dd6fSViresh Kumar 	/*
20787813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
20797813dd6fSViresh Kumar 	 * frequency/voltage list.
20807813dd6fSViresh Kumar 	 */
20817813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
20827813dd6fSViresh Kumar 	return 0;
20837813dd6fSViresh Kumar 
20847813dd6fSViresh Kumar free_opp:
20857813dd6fSViresh Kumar 	_opp_free(new_opp);
20867813dd6fSViresh Kumar 
20877813dd6fSViresh Kumar 	return ret;
20887813dd6fSViresh Kumar }
20897813dd6fSViresh Kumar 
20907813dd6fSViresh Kumar /**
209189f03984SViresh Kumar  * _opp_set_supported_hw() - Set supported platforms
20927813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
20937813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
20947813dd6fSViresh Kumar  * @count: Number of elements in the array.
20957813dd6fSViresh Kumar  *
20967813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
20977813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
20987813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
20997813dd6fSViresh Kumar  * property.
21007813dd6fSViresh Kumar  */
210189f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table,
21027813dd6fSViresh Kumar 				 const u32 *versions, unsigned int count)
21037813dd6fSViresh Kumar {
210425419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
210525419de1SViresh Kumar 	if (opp_table->supported_hw)
210689f03984SViresh Kumar 		return 0;
21077813dd6fSViresh Kumar 
21087813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
21097813dd6fSViresh Kumar 					GFP_KERNEL);
211089f03984SViresh Kumar 	if (!opp_table->supported_hw)
211189f03984SViresh Kumar 		return -ENOMEM;
21127813dd6fSViresh Kumar 
21137813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
21147813dd6fSViresh Kumar 
211589f03984SViresh Kumar 	return 0;
21167813dd6fSViresh Kumar }
21177813dd6fSViresh Kumar 
21187813dd6fSViresh Kumar /**
211989f03984SViresh Kumar  * _opp_put_supported_hw() - Releases resources blocked for supported hw
212089f03984SViresh Kumar  * @opp_table: OPP table returned by _opp_set_supported_hw().
21217813dd6fSViresh Kumar  *
21227813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
212389f03984SViresh Kumar  * _opp_set_supported_hw(). Until this is called, the opp_table structure
21247813dd6fSViresh Kumar  * will not be freed.
21257813dd6fSViresh Kumar  */
212689f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table)
21277813dd6fSViresh Kumar {
212889f03984SViresh Kumar 	if (opp_table->supported_hw) {
21297813dd6fSViresh Kumar 		kfree(opp_table->supported_hw);
21307813dd6fSViresh Kumar 		opp_table->supported_hw = NULL;
21317813dd6fSViresh Kumar 		opp_table->supported_hw_count = 0;
21327813dd6fSViresh Kumar 	}
21339c4f220fSYangtao Li }
21349c4f220fSYangtao Li 
21359c4f220fSYangtao Li /**
2136298098e5SViresh Kumar  * _opp_set_prop_name() - Set prop-extn name
21377813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
21387813dd6fSViresh Kumar  * @name: name to postfix to properties.
21397813dd6fSViresh Kumar  *
21407813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
21417813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
21427813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
21437813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
21447813dd6fSViresh Kumar  */
2145298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
21467813dd6fSViresh Kumar {
2147878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
21487813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
2149298098e5SViresh Kumar 		opp_table->prop_name = kstrdup(name, GFP_KERNEL);
2150298098e5SViresh Kumar 		if (!opp_table->prop_name)
2151298098e5SViresh Kumar 			return -ENOMEM;
21527813dd6fSViresh Kumar 	}
21537813dd6fSViresh Kumar 
2154298098e5SViresh Kumar 	return 0;
21557813dd6fSViresh Kumar }
21567813dd6fSViresh Kumar 
21577813dd6fSViresh Kumar /**
2158298098e5SViresh Kumar  * _opp_put_prop_name() - Releases resources blocked for prop-name
2159298098e5SViresh Kumar  * @opp_table: OPP table returned by _opp_set_prop_name().
21607813dd6fSViresh Kumar  *
21617813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
2162298098e5SViresh Kumar  * _opp_set_prop_name(). Until this is called, the opp_table structure
21637813dd6fSViresh Kumar  * will not be freed.
21647813dd6fSViresh Kumar  */
2165298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table)
21667813dd6fSViresh Kumar {
2167298098e5SViresh Kumar 	if (opp_table->prop_name) {
21687813dd6fSViresh Kumar 		kfree(opp_table->prop_name);
21697813dd6fSViresh Kumar 		opp_table->prop_name = NULL;
21707813dd6fSViresh Kumar 	}
2171298098e5SViresh Kumar }
21727813dd6fSViresh Kumar 
21737813dd6fSViresh Kumar /**
2174b0ec0942SViresh Kumar  * _opp_set_regulators() - Set regulator names for the device
21757813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
21767813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
21777813dd6fSViresh Kumar  * @count: Number of regulators.
21787813dd6fSViresh Kumar  *
21797813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
21807813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
21817813dd6fSViresh Kumar  * well.
21827813dd6fSViresh Kumar  *
21837813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
21847813dd6fSViresh Kumar  */
2185b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
218687686cc8SViresh Kumar 			       const char * const names[])
21877813dd6fSViresh Kumar {
218887686cc8SViresh Kumar 	const char * const *temp = names;
21897813dd6fSViresh Kumar 	struct regulator *reg;
219087686cc8SViresh Kumar 	int count = 0, ret, i;
219187686cc8SViresh Kumar 
219287686cc8SViresh Kumar 	/* Count number of regulators */
219387686cc8SViresh Kumar 	while (*temp++)
219487686cc8SViresh Kumar 		count++;
219587686cc8SViresh Kumar 
219687686cc8SViresh Kumar 	if (!count)
2197b0ec0942SViresh Kumar 		return -EINVAL;
21987813dd6fSViresh Kumar 
2199779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
2200779b783cSViresh Kumar 	if (opp_table->regulators)
2201b0ec0942SViresh Kumar 		return 0;
22027813dd6fSViresh Kumar 
22037813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
22047813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
22057813dd6fSViresh Kumar 					      GFP_KERNEL);
2206b0ec0942SViresh Kumar 	if (!opp_table->regulators)
2207b0ec0942SViresh Kumar 		return -ENOMEM;
22087813dd6fSViresh Kumar 
22097813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
22107813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
22117813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
2212543256d2SKrzysztof Kozlowski 			ret = dev_err_probe(dev, PTR_ERR(reg),
2213543256d2SKrzysztof Kozlowski 					    "%s: no regulator (%s) found\n",
2214543256d2SKrzysztof Kozlowski 					    __func__, names[i]);
22157813dd6fSViresh Kumar 			goto free_regulators;
22167813dd6fSViresh Kumar 		}
22177813dd6fSViresh Kumar 
22187813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
22197813dd6fSViresh Kumar 	}
22207813dd6fSViresh Kumar 
22217813dd6fSViresh Kumar 	opp_table->regulator_count = count;
22227813dd6fSViresh Kumar 
2223c522ce8aSViresh Kumar 	/* Set generic config_regulators() for single regulators here */
2224c522ce8aSViresh Kumar 	if (count == 1)
2225c522ce8aSViresh Kumar 		opp_table->config_regulators = _opp_config_regulator_single;
2226c522ce8aSViresh Kumar 
2227b0ec0942SViresh Kumar 	return 0;
22287813dd6fSViresh Kumar 
22297813dd6fSViresh Kumar free_regulators:
223024957db1SMarek Szyprowski 	while (i != 0)
223124957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
22327813dd6fSViresh Kumar 
22337813dd6fSViresh Kumar 	kfree(opp_table->regulators);
22347813dd6fSViresh Kumar 	opp_table->regulators = NULL;
223546f48acaSViresh Kumar 	opp_table->regulator_count = -1;
22367813dd6fSViresh Kumar 
2237b0ec0942SViresh Kumar 	return ret;
22387813dd6fSViresh Kumar }
22397813dd6fSViresh Kumar 
22407813dd6fSViresh Kumar /**
2241b0ec0942SViresh Kumar  * _opp_put_regulators() - Releases resources blocked for regulator
2242b0ec0942SViresh Kumar  * @opp_table: OPP table returned from _opp_set_regulators().
22437813dd6fSViresh Kumar  */
2244b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table)
22457813dd6fSViresh Kumar {
22467813dd6fSViresh Kumar 	int i;
22477813dd6fSViresh Kumar 
2248779b783cSViresh Kumar 	if (!opp_table->regulators)
2249b0ec0942SViresh Kumar 		return;
22507813dd6fSViresh Kumar 
225172f80ce4SViresh Kumar 	if (opp_table->enabled) {
22528d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
22538d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
22548d45719cSKamil Konieczny 	}
22558d45719cSKamil Konieczny 
225624957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
22577813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
22587813dd6fSViresh Kumar 
22597813dd6fSViresh Kumar 	kfree(opp_table->regulators);
22607813dd6fSViresh Kumar 	opp_table->regulators = NULL;
226146f48acaSViresh Kumar 	opp_table->regulator_count = -1;
22627813dd6fSViresh Kumar }
226332aee78bSYangtao Li 
22642083da24SViresh Kumar static void _put_clks(struct opp_table *opp_table, int count)
22652083da24SViresh Kumar {
22662083da24SViresh Kumar 	int i;
22672083da24SViresh Kumar 
22682083da24SViresh Kumar 	for (i = count - 1; i >= 0; i--)
22692083da24SViresh Kumar 		clk_put(opp_table->clks[i]);
22702083da24SViresh Kumar 
22712083da24SViresh Kumar 	kfree(opp_table->clks);
22722083da24SViresh Kumar 	opp_table->clks = NULL;
22732083da24SViresh Kumar }
22742083da24SViresh Kumar 
22757813dd6fSViresh Kumar /**
22762368f576SViresh Kumar  * _opp_set_clknames() - Set clk names for the device
22772368f576SViresh Kumar  * @dev: Device for which clk names is being set.
22782368f576SViresh Kumar  * @names: Clk names.
22797813dd6fSViresh Kumar  *
22802368f576SViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointers to the
22812368f576SViresh Kumar  * clocks for the device. Simple cases work fine without using this routine
22822368f576SViresh Kumar  * (i.e. by passing connection-id as NULL), but for a device with multiple
22832368f576SViresh Kumar  * clocks available, the OPP core needs to know the exact names of the clks to
22842368f576SViresh Kumar  * use.
22857813dd6fSViresh Kumar  *
22867813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
22877813dd6fSViresh Kumar  */
22882368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
22892083da24SViresh Kumar 			     const char * const names[],
22902083da24SViresh Kumar 			     config_clks_t config_clks)
22917813dd6fSViresh Kumar {
22922368f576SViresh Kumar 	const char * const *temp = names;
22932083da24SViresh Kumar 	int count = 0, ret, i;
22942083da24SViresh Kumar 	struct clk *clk;
22957813dd6fSViresh Kumar 
22962368f576SViresh Kumar 	/* Count number of clks */
22972368f576SViresh Kumar 	while (*temp++)
22982368f576SViresh Kumar 		count++;
22997813dd6fSViresh Kumar 
23002368f576SViresh Kumar 	/*
23012368f576SViresh Kumar 	 * This is a special case where we have a single clock, whose connection
23022368f576SViresh Kumar 	 * id name is NULL, i.e. first two entries are NULL in the array.
23032368f576SViresh Kumar 	 */
23042368f576SViresh Kumar 	if (!count && !names[1])
23052368f576SViresh Kumar 		count = 1;
23062368f576SViresh Kumar 
23072083da24SViresh Kumar 	/* Fail early for invalid configurations */
23082f71ae1aSViresh Kumar 	if (!count || (!config_clks && count > 1))
23092368f576SViresh Kumar 		return -EINVAL;
23107813dd6fSViresh Kumar 
23110a43452bSViresh Kumar 	/* Another CPU that shares the OPP table has set the clkname ? */
23122083da24SViresh Kumar 	if (opp_table->clks)
23132368f576SViresh Kumar 		return 0;
23140a43452bSViresh Kumar 
23152083da24SViresh Kumar 	opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks),
23162083da24SViresh Kumar 					GFP_KERNEL);
23172083da24SViresh Kumar 	if (!opp_table->clks)
23182083da24SViresh Kumar 		return -ENOMEM;
23197813dd6fSViresh Kumar 
23202083da24SViresh Kumar 	/* Find clks for the device */
23212083da24SViresh Kumar 	for (i = 0; i < count; i++) {
23222083da24SViresh Kumar 		clk = clk_get(dev, names[i]);
23232083da24SViresh Kumar 		if (IS_ERR(clk)) {
23242083da24SViresh Kumar 			ret = dev_err_probe(dev, PTR_ERR(clk),
23252083da24SViresh Kumar 					    "%s: Couldn't find clock with name: %s\n",
23262083da24SViresh Kumar 					    __func__, names[i]);
23272083da24SViresh Kumar 			goto free_clks;
23287813dd6fSViresh Kumar 		}
23297813dd6fSViresh Kumar 
23302083da24SViresh Kumar 		opp_table->clks[i] = clk;
23312083da24SViresh Kumar 	}
23322083da24SViresh Kumar 
23332083da24SViresh Kumar 	opp_table->clk_count = count;
23342f71ae1aSViresh Kumar 	opp_table->config_clks = config_clks;
23352083da24SViresh Kumar 
23362083da24SViresh Kumar 	/* Set generic single clk set here */
23372083da24SViresh Kumar 	if (count == 1) {
23382f71ae1aSViresh Kumar 		if (!opp_table->config_clks)
23392083da24SViresh Kumar 			opp_table->config_clks = _opp_config_clk_single;
23402083da24SViresh Kumar 
23412083da24SViresh Kumar 		/*
23422083da24SViresh Kumar 		 * We could have just dropped the "clk" field and used "clks"
23432083da24SViresh Kumar 		 * everywhere. Instead we kept the "clk" field around for
23442083da24SViresh Kumar 		 * following reasons:
23452083da24SViresh Kumar 		 *
23462083da24SViresh Kumar 		 * - avoiding clks[0] everywhere else.
23472083da24SViresh Kumar 		 * - not running single clk helpers for multiple clk usecase by
23482083da24SViresh Kumar 		 *   mistake.
23492083da24SViresh Kumar 		 *
23502083da24SViresh Kumar 		 * Since this is single-clk case, just update the clk pointer
23512083da24SViresh Kumar 		 * too.
23522083da24SViresh Kumar 		 */
23532083da24SViresh Kumar 		opp_table->clk = opp_table->clks[0];
23542083da24SViresh Kumar 	}
23550a43452bSViresh Kumar 
23562368f576SViresh Kumar 	return 0;
23572083da24SViresh Kumar 
23582083da24SViresh Kumar free_clks:
23592083da24SViresh Kumar 	_put_clks(opp_table, i);
23602083da24SViresh Kumar 	return ret;
23617813dd6fSViresh Kumar }
23627813dd6fSViresh Kumar 
23637813dd6fSViresh Kumar /**
23642368f576SViresh Kumar  * _opp_put_clknames() - Releases resources blocked for clks.
23652368f576SViresh Kumar  * @opp_table: OPP table returned from _opp_set_clknames().
23667813dd6fSViresh Kumar  */
23672368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table)
23687813dd6fSViresh Kumar {
23692083da24SViresh Kumar 	if (!opp_table->clks)
23702083da24SViresh Kumar 		return;
23712083da24SViresh Kumar 
23722083da24SViresh Kumar 	opp_table->config_clks = NULL;
23732083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
23742083da24SViresh Kumar 
23752083da24SViresh Kumar 	_put_clks(opp_table, opp_table->clk_count);
2376a74f681cSYangtao Li }
2377a74f681cSYangtao Li 
2378a74f681cSYangtao Li /**
2379aee3352fSViresh Kumar  * _opp_set_config_regulators_helper() - Register custom set regulator helper.
2380aee3352fSViresh Kumar  * @dev: Device for which the helper is getting registered.
2381aee3352fSViresh Kumar  * @config_regulators: Custom set regulator helper.
2382aee3352fSViresh Kumar  *
2383aee3352fSViresh Kumar  * This is useful to support platforms with multiple regulators per device.
2384aee3352fSViresh Kumar  *
2385aee3352fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
2386aee3352fSViresh Kumar  */
2387aee3352fSViresh Kumar static int _opp_set_config_regulators_helper(struct opp_table *opp_table,
2388aee3352fSViresh Kumar 		struct device *dev, config_regulators_t config_regulators)
2389aee3352fSViresh Kumar {
2390aee3352fSViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
2391aee3352fSViresh Kumar 	if (!opp_table->config_regulators)
2392aee3352fSViresh Kumar 		opp_table->config_regulators = config_regulators;
2393aee3352fSViresh Kumar 
2394aee3352fSViresh Kumar 	return 0;
2395aee3352fSViresh Kumar }
2396aee3352fSViresh Kumar 
2397aee3352fSViresh Kumar /**
2398aee3352fSViresh Kumar  * _opp_put_config_regulators_helper() - Releases resources blocked for
2399aee3352fSViresh Kumar  *					 config_regulators helper.
2400aee3352fSViresh Kumar  * @opp_table: OPP table returned from _opp_set_config_regulators_helper().
2401aee3352fSViresh Kumar  *
2402aee3352fSViresh Kumar  * Release resources blocked for platform specific config_regulators helper.
2403aee3352fSViresh Kumar  */
2404aee3352fSViresh Kumar static void _opp_put_config_regulators_helper(struct opp_table *opp_table)
2405aee3352fSViresh Kumar {
2406aee3352fSViresh Kumar 	if (opp_table->config_regulators)
2407aee3352fSViresh Kumar 		opp_table->config_regulators = NULL;
2408aee3352fSViresh Kumar }
2409aee3352fSViresh Kumar 
2410442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table)
24116319aee1SViresh Kumar {
24126319aee1SViresh Kumar 	int index;
24136319aee1SViresh Kumar 
2414cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2415cb60e960SViresh Kumar 		return;
2416cb60e960SViresh Kumar 
24176319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
24186319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
24196319aee1SViresh Kumar 			continue;
24206319aee1SViresh Kumar 
24216319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
24226319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
24236319aee1SViresh Kumar 	}
2424c0ab9e08SViresh Kumar 
2425c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2426c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
24276319aee1SViresh Kumar }
24286319aee1SViresh Kumar 
24297813dd6fSViresh Kumar /**
2430442e7a17SViresh Kumar  * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
24316319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
24326319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
243317a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
24344f018bc0SViresh Kumar  *
24354f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
24364f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
24374f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
24384f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
24396319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
24406319aee1SViresh Kumar  * we don't need to support that separately.
24414f018bc0SViresh Kumar  *
24424f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
24436319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
24444f018bc0SViresh Kumar  *
24456319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
24466319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2447baea35e4SViresh Kumar  *
2448baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2449baea35e4SViresh Kumar  * "required-opps" are added in DT.
24504f018bc0SViresh Kumar  */
2451442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
24523734b9f2SDmitry Osipenko 			const char * const *names, struct device ***virt_devs)
24534f018bc0SViresh Kumar {
24546319aee1SViresh Kumar 	struct device *virt_dev;
2455baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
24563734b9f2SDmitry Osipenko 	const char * const *name = names;
24574f018bc0SViresh Kumar 
2458cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2459442e7a17SViresh Kumar 		return 0;
24604f018bc0SViresh Kumar 
24616319aee1SViresh Kumar 	/*
24626319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
24636319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
24646319aee1SViresh Kumar 	 * table is added.
24656319aee1SViresh Kumar 	 */
2466442e7a17SViresh Kumar 	if (!opp_table->required_opp_count)
2467442e7a17SViresh Kumar 		return -EPROBE_DEFER;
24686319aee1SViresh Kumar 
24694f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
24704f018bc0SViresh Kumar 
2471c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2472c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2473c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2474c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2475c0ab9e08SViresh Kumar 		goto unlock;
24764f018bc0SViresh Kumar 
24776319aee1SViresh Kumar 	while (*name) {
24786319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
24796319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
24806319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
24816319aee1SViresh Kumar 			goto err;
24826319aee1SViresh Kumar 		}
24834f018bc0SViresh Kumar 
24846319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
24854ea9496cSTang Bin 		if (IS_ERR_OR_NULL(virt_dev)) {
24864ea9496cSTang Bin 			ret = PTR_ERR(virt_dev) ? : -ENODEV;
24876319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
24886319aee1SViresh Kumar 			goto err;
24894f018bc0SViresh Kumar 		}
24904f018bc0SViresh Kumar 
24914f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2492baea35e4SViresh Kumar 		index++;
24936319aee1SViresh Kumar 		name++;
24946319aee1SViresh Kumar 	}
24956319aee1SViresh Kumar 
249617a8f868SViresh Kumar 	if (virt_devs)
249717a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
24984f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
24994f018bc0SViresh Kumar 
2500442e7a17SViresh Kumar 	return 0;
25016319aee1SViresh Kumar 
25026319aee1SViresh Kumar err:
2503442e7a17SViresh Kumar 	_detach_genpd(opp_table);
2504c0ab9e08SViresh Kumar unlock:
25056319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2506442e7a17SViresh Kumar 	return ret;
25076319aee1SViresh Kumar 
25084f018bc0SViresh Kumar }
25094f018bc0SViresh Kumar 
25104f018bc0SViresh Kumar /**
2511442e7a17SViresh Kumar  * _opp_detach_genpd() - Detach genpd(s) from the device.
2512442e7a17SViresh Kumar  * @opp_table: OPP table returned by _opp_attach_genpd().
25134f018bc0SViresh Kumar  *
25146319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
25156319aee1SViresh Kumar  * OPP table.
25164f018bc0SViresh Kumar  */
2517442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
25184f018bc0SViresh Kumar {
25194f018bc0SViresh Kumar 	/*
25204f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
25214f018bc0SViresh Kumar 	 * used in parallel.
25224f018bc0SViresh Kumar 	 */
25234f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2524442e7a17SViresh Kumar 	_detach_genpd(opp_table);
25254f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
25264f018bc0SViresh Kumar }
2527b4b9e223SDmitry Osipenko 
252811b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data)
252911b9b663SViresh Kumar {
253011b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_GENPD)
2531442e7a17SViresh Kumar 		_opp_detach_genpd(data->opp_table);
253211b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR)
2533b0ec0942SViresh Kumar 		_opp_put_regulators(data->opp_table);
253411b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_SUPPORTED_HW)
253589f03984SViresh Kumar 		_opp_put_supported_hw(data->opp_table);
25361f378c6eSViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
2537aee3352fSViresh Kumar 		_opp_put_config_regulators_helper(data->opp_table);
253811b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_PROP_NAME)
2539298098e5SViresh Kumar 		_opp_put_prop_name(data->opp_table);
254011b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_CLK)
25412368f576SViresh Kumar 		_opp_put_clknames(data->opp_table);
254211b9b663SViresh Kumar 
254311b9b663SViresh Kumar 	dev_pm_opp_put_opp_table(data->opp_table);
254411b9b663SViresh Kumar 	kfree(data);
254511b9b663SViresh Kumar }
254611b9b663SViresh Kumar 
254711b9b663SViresh Kumar /**
254811b9b663SViresh Kumar  * dev_pm_opp_set_config() - Set OPP configuration for the device.
254911b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
255011b9b663SViresh Kumar  * @config: OPP configuration.
255111b9b663SViresh Kumar  *
255211b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
255311b9b663SViresh Kumar  *
255411b9b663SViresh Kumar  * This must be called before any OPPs are initialized for the device. This may
255511b9b663SViresh Kumar  * be called multiple times for the same OPP table, for example once for each
255611b9b663SViresh Kumar  * CPU that share the same table. This must be balanced by the same number of
255711b9b663SViresh Kumar  * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
255811b9b663SViresh Kumar  *
255911b9b663SViresh Kumar  * This returns a token to the caller, which must be passed to
256011b9b663SViresh Kumar  * dev_pm_opp_clear_config() to free the resources later. The value of the
256111b9b663SViresh Kumar  * returned token will be >= 1 for success and negative for errors. The minimum
256211b9b663SViresh Kumar  * value of 1 is chosen here to make it easy for callers to manage the resource.
256311b9b663SViresh Kumar  */
256411b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
256511b9b663SViresh Kumar {
2566298098e5SViresh Kumar 	struct opp_table *opp_table;
256711b9b663SViresh Kumar 	struct opp_config_data *data;
256811b9b663SViresh Kumar 	unsigned int id;
256911b9b663SViresh Kumar 	int ret;
257011b9b663SViresh Kumar 
257111b9b663SViresh Kumar 	data = kmalloc(sizeof(*data), GFP_KERNEL);
257211b9b663SViresh Kumar 	if (!data)
257311b9b663SViresh Kumar 		return -ENOMEM;
257411b9b663SViresh Kumar 
257511b9b663SViresh Kumar 	opp_table = _add_opp_table(dev, false);
257611b9b663SViresh Kumar 	if (IS_ERR(opp_table)) {
257711b9b663SViresh Kumar 		kfree(data);
257811b9b663SViresh Kumar 		return PTR_ERR(opp_table);
257911b9b663SViresh Kumar 	}
258011b9b663SViresh Kumar 
258111b9b663SViresh Kumar 	data->opp_table = opp_table;
258211b9b663SViresh Kumar 	data->flags = 0;
258311b9b663SViresh Kumar 
258411b9b663SViresh Kumar 	/* This should be called before OPPs are initialized */
258511b9b663SViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
258611b9b663SViresh Kumar 		ret = -EBUSY;
258711b9b663SViresh Kumar 		goto err;
258811b9b663SViresh Kumar 	}
258911b9b663SViresh Kumar 
259011b9b663SViresh Kumar 	/* Configure clocks */
259111b9b663SViresh Kumar 	if (config->clk_names) {
25922083da24SViresh Kumar 		ret = _opp_set_clknames(opp_table, dev, config->clk_names,
25932083da24SViresh Kumar 					config->config_clks);
25942368f576SViresh Kumar 		if (ret)
259511b9b663SViresh Kumar 			goto err;
259611b9b663SViresh Kumar 
259711b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_CLK;
25982083da24SViresh Kumar 	} else if (config->config_clks) {
25992083da24SViresh Kumar 		/* Don't allow config callback without clocks */
26002083da24SViresh Kumar 		ret = -EINVAL;
26012083da24SViresh Kumar 		goto err;
260211b9b663SViresh Kumar 	}
260311b9b663SViresh Kumar 
260411b9b663SViresh Kumar 	/* Configure property names */
260511b9b663SViresh Kumar 	if (config->prop_name) {
2606298098e5SViresh Kumar 		ret = _opp_set_prop_name(opp_table, config->prop_name);
2607298098e5SViresh Kumar 		if (ret)
260811b9b663SViresh Kumar 			goto err;
260911b9b663SViresh Kumar 
261011b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_PROP_NAME;
261111b9b663SViresh Kumar 	}
261211b9b663SViresh Kumar 
2613aee3352fSViresh Kumar 	/* Configure config_regulators helper */
2614aee3352fSViresh Kumar 	if (config->config_regulators) {
2615aee3352fSViresh Kumar 		ret = _opp_set_config_regulators_helper(opp_table, dev,
2616aee3352fSViresh Kumar 						config->config_regulators);
2617aee3352fSViresh Kumar 		if (ret)
2618aee3352fSViresh Kumar 			goto err;
2619aee3352fSViresh Kumar 
2620aee3352fSViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR_HELPER;
2621aee3352fSViresh Kumar 	}
2622aee3352fSViresh Kumar 
262311b9b663SViresh Kumar 	/* Configure supported hardware */
262411b9b663SViresh Kumar 	if (config->supported_hw) {
262589f03984SViresh Kumar 		ret = _opp_set_supported_hw(opp_table, config->supported_hw,
262611b9b663SViresh Kumar 					    config->supported_hw_count);
262789f03984SViresh Kumar 		if (ret)
262811b9b663SViresh Kumar 			goto err;
262911b9b663SViresh Kumar 
263011b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_SUPPORTED_HW;
263111b9b663SViresh Kumar 	}
263211b9b663SViresh Kumar 
263311b9b663SViresh Kumar 	/* Configure supplies */
263411b9b663SViresh Kumar 	if (config->regulator_names) {
2635b0ec0942SViresh Kumar 		ret = _opp_set_regulators(opp_table, dev,
2636b0ec0942SViresh Kumar 					  config->regulator_names);
2637b0ec0942SViresh Kumar 		if (ret)
263811b9b663SViresh Kumar 			goto err;
263911b9b663SViresh Kumar 
264011b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR;
264111b9b663SViresh Kumar 	}
264211b9b663SViresh Kumar 
264311b9b663SViresh Kumar 	/* Attach genpds */
264411b9b663SViresh Kumar 	if (config->genpd_names) {
2645442e7a17SViresh Kumar 		ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
264611b9b663SViresh Kumar 					config->virt_devs);
2647442e7a17SViresh Kumar 		if (ret)
264811b9b663SViresh Kumar 			goto err;
264911b9b663SViresh Kumar 
265011b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_GENPD;
265111b9b663SViresh Kumar 	}
265211b9b663SViresh Kumar 
265311b9b663SViresh Kumar 	ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
265411b9b663SViresh Kumar 		       GFP_KERNEL);
265511b9b663SViresh Kumar 	if (ret)
265611b9b663SViresh Kumar 		goto err;
265711b9b663SViresh Kumar 
265811b9b663SViresh Kumar 	return id;
265911b9b663SViresh Kumar 
266011b9b663SViresh Kumar err:
266111b9b663SViresh Kumar 	_opp_clear_config(data);
266211b9b663SViresh Kumar 	return ret;
266311b9b663SViresh Kumar }
266411b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
266511b9b663SViresh Kumar 
266611b9b663SViresh Kumar /**
266711b9b663SViresh Kumar  * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
266811b9b663SViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_config().
266911b9b663SViresh Kumar  *
267011b9b663SViresh Kumar  * This allows all device OPP configurations to be cleared at once. This must be
267111b9b663SViresh Kumar  * called once for each call made to dev_pm_opp_set_config(), in order to free
267211b9b663SViresh Kumar  * the OPPs properly.
267311b9b663SViresh Kumar  *
267411b9b663SViresh Kumar  * Currently the first call itself ends up freeing all the OPP configurations,
267511b9b663SViresh Kumar  * while the later ones only drop the OPP table reference. This works well for
267611b9b663SViresh Kumar  * now as we would never want to use an half initialized OPP table and want to
267711b9b663SViresh Kumar  * remove the configurations together.
267811b9b663SViresh Kumar  */
267911b9b663SViresh Kumar void dev_pm_opp_clear_config(int token)
268011b9b663SViresh Kumar {
268111b9b663SViresh Kumar 	struct opp_config_data *data;
268211b9b663SViresh Kumar 
268311b9b663SViresh Kumar 	/*
268411b9b663SViresh Kumar 	 * This lets the callers call this unconditionally and keep their code
268511b9b663SViresh Kumar 	 * simple.
268611b9b663SViresh Kumar 	 */
268711b9b663SViresh Kumar 	if (unlikely(token <= 0))
268811b9b663SViresh Kumar 		return;
268911b9b663SViresh Kumar 
269011b9b663SViresh Kumar 	data = xa_erase(&opp_configs, token);
269111b9b663SViresh Kumar 	if (WARN_ON(!data))
269211b9b663SViresh Kumar 		return;
269311b9b663SViresh Kumar 
269411b9b663SViresh Kumar 	_opp_clear_config(data);
269511b9b663SViresh Kumar }
269611b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
269711b9b663SViresh Kumar 
269811b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token)
269911b9b663SViresh Kumar {
270011b9b663SViresh Kumar 	dev_pm_opp_clear_config((unsigned long)token);
270111b9b663SViresh Kumar }
270211b9b663SViresh Kumar 
270311b9b663SViresh Kumar /**
270411b9b663SViresh Kumar  * devm_pm_opp_set_config() - Set OPP configuration for the device.
270511b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
270611b9b663SViresh Kumar  * @config: OPP configuration.
270711b9b663SViresh Kumar  *
270811b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
270911b9b663SViresh Kumar  * This is a resource-managed variant of dev_pm_opp_set_config().
271011b9b663SViresh Kumar  *
271111b9b663SViresh Kumar  * Return: 0 on success and errorno otherwise.
271211b9b663SViresh Kumar  */
271311b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
271411b9b663SViresh Kumar {
271511b9b663SViresh Kumar 	int token = dev_pm_opp_set_config(dev, config);
271611b9b663SViresh Kumar 
271711b9b663SViresh Kumar 	if (token < 0)
271811b9b663SViresh Kumar 		return token;
271911b9b663SViresh Kumar 
272011b9b663SViresh Kumar 	return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
272111b9b663SViresh Kumar 					(void *) ((unsigned long) token));
272211b9b663SViresh Kumar }
272311b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
272411b9b663SViresh Kumar 
27254f018bc0SViresh Kumar /**
27267d8658efSSaravana Kannan  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
27277d8658efSSaravana Kannan  * @src_table: OPP table which has @dst_table as one of its required OPP table.
27287d8658efSSaravana Kannan  * @dst_table: Required OPP table of the @src_table.
27297d8658efSSaravana Kannan  * @src_opp: OPP from the @src_table.
27307d8658efSSaravana Kannan  *
27317d8658efSSaravana Kannan  * This function returns the OPP (present in @dst_table) pointed out by the
27327d8658efSSaravana Kannan  * "required-opps" property of the @src_opp (present in @src_table).
27337d8658efSSaravana Kannan  *
27347d8658efSSaravana Kannan  * The callers are required to call dev_pm_opp_put() for the returned OPP after
27357d8658efSSaravana Kannan  * use.
27367d8658efSSaravana Kannan  *
27377d8658efSSaravana Kannan  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
27387d8658efSSaravana Kannan  */
27397d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
27407d8658efSSaravana Kannan 						 struct opp_table *dst_table,
27417d8658efSSaravana Kannan 						 struct dev_pm_opp *src_opp)
27427d8658efSSaravana Kannan {
27437d8658efSSaravana Kannan 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
27447d8658efSSaravana Kannan 	int i;
27457d8658efSSaravana Kannan 
27467d8658efSSaravana Kannan 	if (!src_table || !dst_table || !src_opp ||
27477d8658efSSaravana Kannan 	    !src_table->required_opp_tables)
27487d8658efSSaravana Kannan 		return ERR_PTR(-EINVAL);
27497d8658efSSaravana Kannan 
27507d8658efSSaravana Kannan 	/* required-opps not fully initialized yet */
27517d8658efSSaravana Kannan 	if (lazy_linking_pending(src_table))
27527d8658efSSaravana Kannan 		return ERR_PTR(-EBUSY);
27537d8658efSSaravana Kannan 
27547d8658efSSaravana Kannan 	for (i = 0; i < src_table->required_opp_count; i++) {
27557d8658efSSaravana Kannan 		if (src_table->required_opp_tables[i] == dst_table) {
27567d8658efSSaravana Kannan 			mutex_lock(&src_table->lock);
27577d8658efSSaravana Kannan 
27587d8658efSSaravana Kannan 			list_for_each_entry(opp, &src_table->opp_list, node) {
27597d8658efSSaravana Kannan 				if (opp == src_opp) {
27607d8658efSSaravana Kannan 					dest_opp = opp->required_opps[i];
27617d8658efSSaravana Kannan 					dev_pm_opp_get(dest_opp);
27627d8658efSSaravana Kannan 					break;
27637d8658efSSaravana Kannan 				}
27647d8658efSSaravana Kannan 			}
27657d8658efSSaravana Kannan 
27667d8658efSSaravana Kannan 			mutex_unlock(&src_table->lock);
27677d8658efSSaravana Kannan 			break;
27687d8658efSSaravana Kannan 		}
27697d8658efSSaravana Kannan 	}
27707d8658efSSaravana Kannan 
27717d8658efSSaravana Kannan 	if (IS_ERR(dest_opp)) {
27727d8658efSSaravana Kannan 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
27737d8658efSSaravana Kannan 		       src_table, dst_table);
27747d8658efSSaravana Kannan 	}
27757d8658efSSaravana Kannan 
27767d8658efSSaravana Kannan 	return dest_opp;
27777d8658efSSaravana Kannan }
27787d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
27797d8658efSSaravana Kannan 
27807d8658efSSaravana Kannan /**
2781c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2782c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2783c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2784c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2785c8a59103SViresh Kumar  *
2786c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2787c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2788c8a59103SViresh Kumar  * performance state set to @pstate.
2789c8a59103SViresh Kumar  *
2790c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2791c8a59103SViresh Kumar  * value on errors.
2792c8a59103SViresh Kumar  */
2793c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2794c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2795c8a59103SViresh Kumar 				       unsigned int pstate)
2796c8a59103SViresh Kumar {
2797c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2798c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2799c8a59103SViresh Kumar 	int i;
2800c8a59103SViresh Kumar 
2801c8a59103SViresh Kumar 	/*
2802c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2803c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2804c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2805c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2806c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2807c8a59103SViresh Kumar 	 */
2808f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2809c8a59103SViresh Kumar 		return pstate;
2810c8a59103SViresh Kumar 
281184cb7ff3SViresh Kumar 	/* Both OPP tables must belong to genpds */
281284cb7ff3SViresh Kumar 	if (unlikely(!src_table->is_genpd || !dst_table->is_genpd)) {
281384cb7ff3SViresh Kumar 		pr_err("%s: Performance state is only valid for genpds.\n", __func__);
281484cb7ff3SViresh Kumar 		return -EINVAL;
281584cb7ff3SViresh Kumar 	}
281684cb7ff3SViresh Kumar 
28177eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
28187eba0c76SViresh Kumar 	if (lazy_linking_pending(src_table))
28197eba0c76SViresh Kumar 		return -EBUSY;
28207eba0c76SViresh Kumar 
2821c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2822c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2823c8a59103SViresh Kumar 			break;
2824c8a59103SViresh Kumar 	}
2825c8a59103SViresh Kumar 
2826c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2827c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2828c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2829c8a59103SViresh Kumar 		return -EINVAL;
2830c8a59103SViresh Kumar 	}
2831c8a59103SViresh Kumar 
2832c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2833c8a59103SViresh Kumar 
2834c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
28357c41cdcdSViresh Kumar 		if (opp->level == pstate) {
28367c41cdcdSViresh Kumar 			dest_pstate = opp->required_opps[i]->level;
2837c8a59103SViresh Kumar 			goto unlock;
2838c8a59103SViresh Kumar 		}
2839c8a59103SViresh Kumar 	}
2840c8a59103SViresh Kumar 
2841c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2842c8a59103SViresh Kumar 	       dst_table);
2843c8a59103SViresh Kumar 
2844c8a59103SViresh Kumar unlock:
2845c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2846c8a59103SViresh Kumar 
2847c8a59103SViresh Kumar 	return dest_pstate;
2848c8a59103SViresh Kumar }
2849c8a59103SViresh Kumar 
2850c8a59103SViresh Kumar /**
28517813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
28527813dd6fSViresh Kumar  * @dev:	device for which we do this operation
28537813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
28547813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
28557813dd6fSViresh Kumar  *
28567813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
28577813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
28587813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
28597813dd6fSViresh Kumar  *
28607813dd6fSViresh Kumar  * Return:
28617813dd6fSViresh Kumar  * 0		On success OR
28627813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
28637813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
28647813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
28657813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
28667813dd6fSViresh Kumar  */
28677813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
28687813dd6fSViresh Kumar {
28697813dd6fSViresh Kumar 	struct opp_table *opp_table;
28707813dd6fSViresh Kumar 	int ret;
28717813dd6fSViresh Kumar 
287232439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2873dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2874dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
28757813dd6fSViresh Kumar 
287646f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
287746f48acaSViresh Kumar 	opp_table->regulator_count = 1;
287846f48acaSViresh Kumar 
28797813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
28800ad8c623SViresh Kumar 	if (ret)
28817813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
28820ad8c623SViresh Kumar 
28837813dd6fSViresh Kumar 	return ret;
28847813dd6fSViresh Kumar }
28857813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
28867813dd6fSViresh Kumar 
28877813dd6fSViresh Kumar /**
28887813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
28897813dd6fSViresh Kumar  * @dev:		device for which we do this operation
28907813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
28917813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
28927813dd6fSViresh Kumar  *
28937813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
28947813dd6fSViresh Kumar  * which is isolated here.
28957813dd6fSViresh Kumar  *
28967813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
28977813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
28987813dd6fSViresh Kumar  * successful.
28997813dd6fSViresh Kumar  */
29007813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
29017813dd6fSViresh Kumar 				 bool availability_req)
29027813dd6fSViresh Kumar {
29037813dd6fSViresh Kumar 	struct opp_table *opp_table;
29047813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
29057813dd6fSViresh Kumar 	int r = 0;
29067813dd6fSViresh Kumar 
29077813dd6fSViresh Kumar 	/* Find the opp_table */
29087813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29097813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
29107813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
29117813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
29127813dd6fSViresh Kumar 		return r;
29137813dd6fSViresh Kumar 	}
29147813dd6fSViresh Kumar 
2915f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table)) {
2916f123ea74SViresh Kumar 		r = -EINVAL;
2917f123ea74SViresh Kumar 		goto put_table;
2918f123ea74SViresh Kumar 	}
2919f123ea74SViresh Kumar 
29207813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
29217813dd6fSViresh Kumar 
29227813dd6fSViresh Kumar 	/* Do we have the frequency? */
29237813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
29242083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
29257813dd6fSViresh Kumar 			opp = tmp_opp;
29267813dd6fSViresh Kumar 			break;
29277813dd6fSViresh Kumar 		}
29287813dd6fSViresh Kumar 	}
29297813dd6fSViresh Kumar 
29307813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
29317813dd6fSViresh Kumar 		r = PTR_ERR(opp);
29327813dd6fSViresh Kumar 		goto unlock;
29337813dd6fSViresh Kumar 	}
29347813dd6fSViresh Kumar 
29357813dd6fSViresh Kumar 	/* Is update really needed? */
29367813dd6fSViresh Kumar 	if (opp->available == availability_req)
29377813dd6fSViresh Kumar 		goto unlock;
29387813dd6fSViresh Kumar 
29397813dd6fSViresh Kumar 	opp->available = availability_req;
29407813dd6fSViresh Kumar 
29417813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
29427813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
29437813dd6fSViresh Kumar 
29447813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
29457813dd6fSViresh Kumar 	if (availability_req)
29467813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
29477813dd6fSViresh Kumar 					     opp);
29487813dd6fSViresh Kumar 	else
29497813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
29507813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
29517813dd6fSViresh Kumar 
29527813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
29537813dd6fSViresh Kumar 	goto put_table;
29547813dd6fSViresh Kumar 
29557813dd6fSViresh Kumar unlock:
29567813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
29577813dd6fSViresh Kumar put_table:
29587813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29597813dd6fSViresh Kumar 	return r;
29607813dd6fSViresh Kumar }
29617813dd6fSViresh Kumar 
29627813dd6fSViresh Kumar /**
296325cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
296425cb20a2SStephen Boyd  * @dev:		device for which we do this operation
296525cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
296625cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
296725cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
296825cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
296925cb20a2SStephen Boyd  *
297025cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
297125cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
297225cb20a2SStephen Boyd  * successful.
297325cb20a2SStephen Boyd  */
297425cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
297525cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
297625cb20a2SStephen Boyd 			      unsigned long u_volt_max)
297725cb20a2SStephen Boyd 
297825cb20a2SStephen Boyd {
297925cb20a2SStephen Boyd 	struct opp_table *opp_table;
298025cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
298125cb20a2SStephen Boyd 	int r = 0;
298225cb20a2SStephen Boyd 
298325cb20a2SStephen Boyd 	/* Find the opp_table */
298425cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
298525cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
298625cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
298725cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
298825cb20a2SStephen Boyd 		return r;
298925cb20a2SStephen Boyd 	}
299025cb20a2SStephen Boyd 
2991f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table)) {
2992f123ea74SViresh Kumar 		r = -EINVAL;
2993f123ea74SViresh Kumar 		goto put_table;
2994f123ea74SViresh Kumar 	}
2995f123ea74SViresh Kumar 
299625cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
299725cb20a2SStephen Boyd 
299825cb20a2SStephen Boyd 	/* Do we have the frequency? */
299925cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
30002083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
300125cb20a2SStephen Boyd 			opp = tmp_opp;
300225cb20a2SStephen Boyd 			break;
300325cb20a2SStephen Boyd 		}
300425cb20a2SStephen Boyd 	}
300525cb20a2SStephen Boyd 
300625cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
300725cb20a2SStephen Boyd 		r = PTR_ERR(opp);
300825cb20a2SStephen Boyd 		goto adjust_unlock;
300925cb20a2SStephen Boyd 	}
301025cb20a2SStephen Boyd 
301125cb20a2SStephen Boyd 	/* Is update really needed? */
301225cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
301325cb20a2SStephen Boyd 		goto adjust_unlock;
301425cb20a2SStephen Boyd 
301525cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
301625cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
301725cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
301825cb20a2SStephen Boyd 
301925cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
302025cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
302125cb20a2SStephen Boyd 
302225cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
302325cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
302425cb20a2SStephen Boyd 				     opp);
302525cb20a2SStephen Boyd 
302625cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
3027f123ea74SViresh Kumar 	goto put_table;
302825cb20a2SStephen Boyd 
302925cb20a2SStephen Boyd adjust_unlock:
303025cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
3031f123ea74SViresh Kumar put_table:
303225cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
303325cb20a2SStephen Boyd 	return r;
303425cb20a2SStephen Boyd }
303503649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
303625cb20a2SStephen Boyd 
303725cb20a2SStephen Boyd /**
30387813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
30397813dd6fSViresh Kumar  * @dev:	device for which we do this operation
30407813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
30417813dd6fSViresh Kumar  *
30427813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
30437813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
30447813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
30457813dd6fSViresh Kumar  *
30467813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
30477813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
30487813dd6fSViresh Kumar  * successful.
30497813dd6fSViresh Kumar  */
30507813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
30517813dd6fSViresh Kumar {
30527813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
30537813dd6fSViresh Kumar }
30547813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
30557813dd6fSViresh Kumar 
30567813dd6fSViresh Kumar /**
30577813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
30587813dd6fSViresh Kumar  * @dev:	device for which we do this operation
30597813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
30607813dd6fSViresh Kumar  *
30617813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
30627813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
30637813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
30647813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
30657813dd6fSViresh Kumar  *
30667813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
30677813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
30687813dd6fSViresh Kumar  * successful.
30697813dd6fSViresh Kumar  */
30707813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
30717813dd6fSViresh Kumar {
30727813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
30737813dd6fSViresh Kumar }
30747813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
30757813dd6fSViresh Kumar 
30767813dd6fSViresh Kumar /**
30777813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
30787813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
30797813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
30807813dd6fSViresh Kumar  *
30817813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
30827813dd6fSViresh Kumar  */
30837813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
30847813dd6fSViresh Kumar {
30857813dd6fSViresh Kumar 	struct opp_table *opp_table;
30867813dd6fSViresh Kumar 	int ret;
30877813dd6fSViresh Kumar 
30887813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
30897813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
30907813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
30917813dd6fSViresh Kumar 
30927813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
30937813dd6fSViresh Kumar 
30947813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
30957813dd6fSViresh Kumar 
30967813dd6fSViresh Kumar 	return ret;
30977813dd6fSViresh Kumar }
30987813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
30997813dd6fSViresh Kumar 
31007813dd6fSViresh Kumar /**
31017813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
31027813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
31037813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
31047813dd6fSViresh Kumar  *
31057813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
31067813dd6fSViresh Kumar  */
31077813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
31087813dd6fSViresh Kumar 				   struct notifier_block *nb)
31097813dd6fSViresh Kumar {
31107813dd6fSViresh Kumar 	struct opp_table *opp_table;
31117813dd6fSViresh Kumar 	int ret;
31127813dd6fSViresh Kumar 
31137813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
31147813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
31157813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
31167813dd6fSViresh Kumar 
31177813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
31187813dd6fSViresh Kumar 
31197813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
31207813dd6fSViresh Kumar 
31217813dd6fSViresh Kumar 	return ret;
31227813dd6fSViresh Kumar }
31237813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
31247813dd6fSViresh Kumar 
31258aaf6264SViresh Kumar /**
31268aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
31278aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
31288aaf6264SViresh Kumar  *
31298aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
31308aaf6264SViresh Kumar  * dynamically added entries.
31318aaf6264SViresh Kumar  */
31328aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
31337813dd6fSViresh Kumar {
31347813dd6fSViresh Kumar 	struct opp_table *opp_table;
31357813dd6fSViresh Kumar 
31367813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
31377813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
31387813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
31397813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
31407813dd6fSViresh Kumar 
31417813dd6fSViresh Kumar 		if (error != -ENODEV)
31427813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
31437813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
31447813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
31457813dd6fSViresh Kumar 			     error);
31467813dd6fSViresh Kumar 		return;
31477813dd6fSViresh Kumar 	}
31487813dd6fSViresh Kumar 
3149922ff075SViresh Kumar 	/*
3150922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
3151922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
3152922ff075SViresh Kumar 	 **/
3153922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
3154cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
3155cdd6ed90SViresh Kumar 
3156922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
31577813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
31587813dd6fSViresh Kumar }
31597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
3160ce8073d8SDmitry Osipenko 
3161ce8073d8SDmitry Osipenko /**
3162ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
3163ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
3164ce8073d8SDmitry Osipenko  *
3165ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
3166ce8073d8SDmitry Osipenko  *
3167ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
3168ce8073d8SDmitry Osipenko  */
3169ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
3170ce8073d8SDmitry Osipenko {
3171ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
3172ce8073d8SDmitry Osipenko 	struct regulator *reg;
3173ce8073d8SDmitry Osipenko 	int i, ret = 0;
3174ce8073d8SDmitry Osipenko 
3175ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
3176ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
3177ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
3178ce8073d8SDmitry Osipenko 		return 0;
3179ce8073d8SDmitry Osipenko 
3180ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
3181ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
3182ce8073d8SDmitry Osipenko 		goto put_table;
3183ce8073d8SDmitry Osipenko 
3184ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
3185ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
3186ce8073d8SDmitry Osipenko 		goto put_table;
3187ce8073d8SDmitry Osipenko 
3188ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
3189ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
3190ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
3191ce8073d8SDmitry Osipenko 		if (ret)
3192ce8073d8SDmitry Osipenko 			break;
3193ce8073d8SDmitry Osipenko 	}
3194ce8073d8SDmitry Osipenko put_table:
3195ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
3196ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
3197ce8073d8SDmitry Osipenko 
3198ce8073d8SDmitry Osipenko 	return ret;
3199ce8073d8SDmitry Osipenko }
3200ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
3201