xref: /openbmc/linux/drivers/opp/core.c (revision 7c41cdcd3bbee5d49de9d4821b15e49d155ff22b)
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 /**
2015b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
2025b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
2035b93ac54SRajendra Nayak  *
2045b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
2055b93ac54SRajendra Nayak  * return 0.
2065b93ac54SRajendra Nayak  */
2075b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
2085b93ac54SRajendra Nayak {
2095b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2105b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
2115b93ac54SRajendra Nayak 		return 0;
2125b93ac54SRajendra Nayak 	}
2135b93ac54SRajendra Nayak 
2145b93ac54SRajendra Nayak 	return opp->level;
2155b93ac54SRajendra Nayak }
2165b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
2175b93ac54SRajendra Nayak 
2185b93ac54SRajendra Nayak /**
219597ff543SDmitry Osipenko  * dev_pm_opp_get_required_pstate() - Gets the required performance state
220597ff543SDmitry Osipenko  *                                    corresponding to an available opp
221597ff543SDmitry Osipenko  * @opp:	opp for which performance state has to be returned for
222597ff543SDmitry Osipenko  * @index:	index of the required opp
223597ff543SDmitry Osipenko  *
224597ff543SDmitry Osipenko  * Return: performance state read from device tree corresponding to the
225597ff543SDmitry Osipenko  * required opp, else return 0.
226597ff543SDmitry Osipenko  */
227597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
228597ff543SDmitry Osipenko 					    unsigned int index)
229597ff543SDmitry Osipenko {
23084cb7ff3SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
23184cb7ff3SViresh Kumar 
232597ff543SDmitry Osipenko 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
23384cb7ff3SViresh Kumar 	    index >= opp_table->required_opp_count) {
234597ff543SDmitry Osipenko 		pr_err("%s: Invalid parameters\n", __func__);
235597ff543SDmitry Osipenko 		return 0;
236597ff543SDmitry Osipenko 	}
237597ff543SDmitry Osipenko 
2387eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
23984cb7ff3SViresh Kumar 	if (lazy_linking_pending(opp_table))
2407eba0c76SViresh Kumar 		return 0;
2417eba0c76SViresh Kumar 
24284cb7ff3SViresh Kumar 	/* The required OPP table must belong to a genpd */
24384cb7ff3SViresh Kumar 	if (unlikely(!opp_table->required_opp_tables[index]->is_genpd)) {
24484cb7ff3SViresh Kumar 		pr_err("%s: Performance state is only valid for genpds.\n", __func__);
24584cb7ff3SViresh Kumar 		return 0;
24684cb7ff3SViresh Kumar 	}
24784cb7ff3SViresh Kumar 
248*7c41cdcdSViresh Kumar 	return opp->required_opps[index]->level;
249597ff543SDmitry Osipenko }
250597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
251597ff543SDmitry Osipenko 
252597ff543SDmitry Osipenko /**
2537813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
2547813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
2557813dd6fSViresh Kumar  *
2567813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
2577813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
2587813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
2597813dd6fSViresh Kumar  *
2607813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
2617813dd6fSViresh Kumar  */
2627813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
2637813dd6fSViresh Kumar {
2647813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2657813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
2667813dd6fSViresh Kumar 		return false;
2677813dd6fSViresh Kumar 	}
2687813dd6fSViresh Kumar 
2697813dd6fSViresh Kumar 	return opp->turbo;
2707813dd6fSViresh Kumar }
2717813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
2727813dd6fSViresh Kumar 
2737813dd6fSViresh Kumar /**
2747813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
2757813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2767813dd6fSViresh Kumar  *
2777813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
2787813dd6fSViresh Kumar  */
2797813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
2807813dd6fSViresh Kumar {
2817813dd6fSViresh Kumar 	struct opp_table *opp_table;
2827813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
2837813dd6fSViresh Kumar 
2847813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2857813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2867813dd6fSViresh Kumar 		return 0;
2877813dd6fSViresh Kumar 
2887813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
2897813dd6fSViresh Kumar 
2907813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2917813dd6fSViresh Kumar 
2927813dd6fSViresh Kumar 	return clock_latency_ns;
2937813dd6fSViresh Kumar }
2947813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
2957813dd6fSViresh Kumar 
2967813dd6fSViresh Kumar /**
2977813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
2987813dd6fSViresh Kumar  * @dev: device for which we do this operation
2997813dd6fSViresh Kumar  *
3007813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
3017813dd6fSViresh Kumar  */
3027813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
3037813dd6fSViresh Kumar {
3047813dd6fSViresh Kumar 	struct opp_table *opp_table;
3057813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
3067813dd6fSViresh Kumar 	struct regulator *reg;
3077813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
3087813dd6fSViresh Kumar 	int ret, i, count;
3097813dd6fSViresh Kumar 	struct {
3107813dd6fSViresh Kumar 		unsigned long min;
3117813dd6fSViresh Kumar 		unsigned long max;
3127813dd6fSViresh Kumar 	} *uV;
3137813dd6fSViresh Kumar 
3147813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3157813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3167813dd6fSViresh Kumar 		return 0;
3177813dd6fSViresh Kumar 
3187813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
31990e3577bSViresh Kumar 	if (!opp_table->regulators)
3207813dd6fSViresh Kumar 		goto put_opp_table;
3217813dd6fSViresh Kumar 
32290e3577bSViresh Kumar 	count = opp_table->regulator_count;
32390e3577bSViresh Kumar 
3247813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
3257813dd6fSViresh Kumar 	if (!uV)
3267813dd6fSViresh Kumar 		goto put_opp_table;
3277813dd6fSViresh Kumar 
3287813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
3297813dd6fSViresh Kumar 
3307813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3317813dd6fSViresh Kumar 		uV[i].min = ~0;
3327813dd6fSViresh Kumar 		uV[i].max = 0;
3337813dd6fSViresh Kumar 
3347813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
3357813dd6fSViresh Kumar 			if (!opp->available)
3367813dd6fSViresh Kumar 				continue;
3377813dd6fSViresh Kumar 
3387813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
3397813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
3407813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
3417813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
3427813dd6fSViresh Kumar 		}
3437813dd6fSViresh Kumar 	}
3447813dd6fSViresh Kumar 
3457813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
3467813dd6fSViresh Kumar 
3477813dd6fSViresh Kumar 	/*
3487813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
3497813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
3507813dd6fSViresh Kumar 	 */
3517813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3527813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
3537813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
3547813dd6fSViresh Kumar 		if (ret > 0)
3557813dd6fSViresh Kumar 			latency_ns += ret * 1000;
3567813dd6fSViresh Kumar 	}
3577813dd6fSViresh Kumar 
3587813dd6fSViresh Kumar 	kfree(uV);
3597813dd6fSViresh Kumar put_opp_table:
3607813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3617813dd6fSViresh Kumar 
3627813dd6fSViresh Kumar 	return latency_ns;
3637813dd6fSViresh Kumar }
3647813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
3657813dd6fSViresh Kumar 
3667813dd6fSViresh Kumar /**
3677813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
3687813dd6fSViresh Kumar  *					     nanoseconds
3697813dd6fSViresh Kumar  * @dev: device for which we do this operation
3707813dd6fSViresh Kumar  *
3717813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
3727813dd6fSViresh Kumar  * switch from one OPP to other.
3737813dd6fSViresh Kumar  */
3747813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
3757813dd6fSViresh Kumar {
3767813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
3777813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
3787813dd6fSViresh Kumar }
3797813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
3807813dd6fSViresh Kumar 
3817813dd6fSViresh Kumar /**
3827813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
3837813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3847813dd6fSViresh Kumar  *
3857813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
3867813dd6fSViresh Kumar  * if one is available, else returns 0;
3877813dd6fSViresh Kumar  */
3887813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
3897813dd6fSViresh Kumar {
3907813dd6fSViresh Kumar 	struct opp_table *opp_table;
3917813dd6fSViresh Kumar 	unsigned long freq = 0;
3927813dd6fSViresh Kumar 
3937813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3947813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3957813dd6fSViresh Kumar 		return 0;
3967813dd6fSViresh Kumar 
3977813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
3987813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
3997813dd6fSViresh Kumar 
4007813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4017813dd6fSViresh Kumar 
4027813dd6fSViresh Kumar 	return freq;
4037813dd6fSViresh Kumar }
4047813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
4057813dd6fSViresh Kumar 
406a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
407a1e8c136SViresh Kumar {
408a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
409a1e8c136SViresh Kumar 	int count = 0;
410a1e8c136SViresh Kumar 
411a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
412a1e8c136SViresh Kumar 
413a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
414a1e8c136SViresh Kumar 		if (opp->available)
415a1e8c136SViresh Kumar 			count++;
416a1e8c136SViresh Kumar 	}
417a1e8c136SViresh Kumar 
418a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
419a1e8c136SViresh Kumar 
420a1e8c136SViresh Kumar 	return count;
421a1e8c136SViresh Kumar }
422a1e8c136SViresh Kumar 
4237813dd6fSViresh Kumar /**
4247813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
4257813dd6fSViresh Kumar  * @dev:	device for which we do this operation
4267813dd6fSViresh Kumar  *
4277813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
4287813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
4297813dd6fSViresh Kumar  */
4307813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
4317813dd6fSViresh Kumar {
4327813dd6fSViresh Kumar 	struct opp_table *opp_table;
433a1e8c136SViresh Kumar 	int count;
4347813dd6fSViresh Kumar 
4357813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4367813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
4377813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
438035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
4397813dd6fSViresh Kumar 			__func__, count);
44009f662f9SViresh Kumar 		return count;
4417813dd6fSViresh Kumar 	}
4427813dd6fSViresh Kumar 
443a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
4447813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4457813dd6fSViresh Kumar 
4467813dd6fSViresh Kumar 	return count;
4477813dd6fSViresh Kumar }
4487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
4497813dd6fSViresh Kumar 
450aab8ced2SViresh Kumar /* Helpers to read keys */
451aab8ced2SViresh Kumar static unsigned long _read_freq(struct dev_pm_opp *opp, int index)
452aab8ced2SViresh Kumar {
4532083da24SViresh Kumar 	return opp->rates[0];
454aab8ced2SViresh Kumar }
455aab8ced2SViresh Kumar 
456c2ab2cb6SViresh Kumar static unsigned long _read_level(struct dev_pm_opp *opp, int index)
457c2ab2cb6SViresh Kumar {
458c2ab2cb6SViresh Kumar 	return opp->level;
459c2ab2cb6SViresh Kumar }
460c2ab2cb6SViresh Kumar 
461add1dc09SViresh Kumar static unsigned long _read_bw(struct dev_pm_opp *opp, int index)
462add1dc09SViresh Kumar {
463add1dc09SViresh Kumar 	return opp->bandwidth[index].peak;
464add1dc09SViresh Kumar }
465add1dc09SViresh Kumar 
466aab8ced2SViresh Kumar /* Generic comparison helpers */
467aab8ced2SViresh Kumar static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
468aab8ced2SViresh Kumar 			   unsigned long opp_key, unsigned long key)
469aab8ced2SViresh Kumar {
470aab8ced2SViresh Kumar 	if (opp_key == key) {
471aab8ced2SViresh Kumar 		*opp = temp_opp;
472aab8ced2SViresh Kumar 		return true;
473aab8ced2SViresh Kumar 	}
474aab8ced2SViresh Kumar 
475aab8ced2SViresh Kumar 	return false;
476aab8ced2SViresh Kumar }
477aab8ced2SViresh Kumar 
478aab8ced2SViresh Kumar static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
479aab8ced2SViresh Kumar 			  unsigned long opp_key, unsigned long key)
480aab8ced2SViresh Kumar {
481aab8ced2SViresh Kumar 	if (opp_key >= key) {
482aab8ced2SViresh Kumar 		*opp = temp_opp;
483aab8ced2SViresh Kumar 		return true;
484aab8ced2SViresh Kumar 	}
485aab8ced2SViresh Kumar 
486aab8ced2SViresh Kumar 	return false;
487aab8ced2SViresh Kumar }
488aab8ced2SViresh Kumar 
489aab8ced2SViresh Kumar static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
490aab8ced2SViresh Kumar 			   unsigned long opp_key, unsigned long key)
491aab8ced2SViresh Kumar {
492aab8ced2SViresh Kumar 	if (opp_key > key)
493aab8ced2SViresh Kumar 		return true;
494aab8ced2SViresh Kumar 
495aab8ced2SViresh Kumar 	*opp = temp_opp;
496aab8ced2SViresh Kumar 	return false;
497aab8ced2SViresh Kumar }
498aab8ced2SViresh Kumar 
499aab8ced2SViresh Kumar /* Generic key finding helpers */
500aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
501aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
502aab8ced2SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
503aab8ced2SViresh Kumar 		bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
504e10a4644SViresh Kumar 				unsigned long opp_key, unsigned long key),
505e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
506aab8ced2SViresh Kumar {
507aab8ced2SViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
508aab8ced2SViresh Kumar 
509e10a4644SViresh Kumar 	/* Assert that the requirement is met */
510e10a4644SViresh Kumar 	if (assert && !assert(opp_table))
511e10a4644SViresh Kumar 		return ERR_PTR(-EINVAL);
512e10a4644SViresh Kumar 
513aab8ced2SViresh Kumar 	mutex_lock(&opp_table->lock);
514aab8ced2SViresh Kumar 
515aab8ced2SViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
516aab8ced2SViresh Kumar 		if (temp_opp->available == available) {
517aab8ced2SViresh Kumar 			if (compare(&opp, temp_opp, read(temp_opp, index), *key))
518aab8ced2SViresh Kumar 				break;
519aab8ced2SViresh Kumar 		}
520aab8ced2SViresh Kumar 	}
521aab8ced2SViresh Kumar 
522aab8ced2SViresh Kumar 	/* Increment the reference count of OPP */
523aab8ced2SViresh Kumar 	if (!IS_ERR(opp)) {
524aab8ced2SViresh Kumar 		*key = read(opp, index);
525aab8ced2SViresh Kumar 		dev_pm_opp_get(opp);
526aab8ced2SViresh Kumar 	}
527aab8ced2SViresh Kumar 
528aab8ced2SViresh Kumar 	mutex_unlock(&opp_table->lock);
529aab8ced2SViresh Kumar 
530aab8ced2SViresh Kumar 	return opp;
531aab8ced2SViresh Kumar }
532aab8ced2SViresh Kumar 
533aab8ced2SViresh Kumar static struct dev_pm_opp *
534aab8ced2SViresh Kumar _find_key(struct device *dev, unsigned long *key, int index, bool available,
535aab8ced2SViresh Kumar 	  unsigned long (*read)(struct dev_pm_opp *opp, int index),
536aab8ced2SViresh Kumar 	  bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
537e10a4644SViresh Kumar 			  unsigned long opp_key, unsigned long key),
538e10a4644SViresh Kumar 	  bool (*assert)(struct opp_table *opp_table))
539aab8ced2SViresh Kumar {
540aab8ced2SViresh Kumar 	struct opp_table *opp_table;
541aab8ced2SViresh Kumar 	struct dev_pm_opp *opp;
542aab8ced2SViresh Kumar 
543aab8ced2SViresh Kumar 	opp_table = _find_opp_table(dev);
544aab8ced2SViresh Kumar 	if (IS_ERR(opp_table)) {
545aab8ced2SViresh Kumar 		dev_err(dev, "%s: OPP table not found (%ld)\n", __func__,
546aab8ced2SViresh Kumar 			PTR_ERR(opp_table));
547aab8ced2SViresh Kumar 		return ERR_CAST(opp_table);
548aab8ced2SViresh Kumar 	}
549aab8ced2SViresh Kumar 
550aab8ced2SViresh Kumar 	opp = _opp_table_find_key(opp_table, key, index, available, read,
551e10a4644SViresh Kumar 				  compare, assert);
552aab8ced2SViresh Kumar 
553aab8ced2SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
554aab8ced2SViresh Kumar 
555aab8ced2SViresh Kumar 	return opp;
556aab8ced2SViresh Kumar }
557aab8ced2SViresh Kumar 
558aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_exact(struct device *dev,
559aab8ced2SViresh Kumar 		unsigned long key, int index, bool available,
560e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
561e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
562aab8ced2SViresh Kumar {
563aab8ced2SViresh Kumar 	/*
564aab8ced2SViresh Kumar 	 * The value of key will be updated here, but will be ignored as the
565aab8ced2SViresh Kumar 	 * caller doesn't need it.
566aab8ced2SViresh Kumar 	 */
567e10a4644SViresh Kumar 	return _find_key(dev, &key, index, available, read, _compare_exact,
568e10a4644SViresh Kumar 			 assert);
569aab8ced2SViresh Kumar }
570aab8ced2SViresh Kumar 
571aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
572aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
573e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
574e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
575aab8ced2SViresh Kumar {
576aab8ced2SViresh Kumar 	return _opp_table_find_key(opp_table, key, index, available, read,
577e10a4644SViresh Kumar 				   _compare_ceil, assert);
578aab8ced2SViresh Kumar }
579aab8ced2SViresh Kumar 
580aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
581aab8ced2SViresh Kumar 		int index, bool available,
582e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
583e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
584aab8ced2SViresh Kumar {
585e10a4644SViresh Kumar 	return _find_key(dev, key, index, available, read, _compare_ceil,
586e10a4644SViresh Kumar 			 assert);
587aab8ced2SViresh Kumar }
588aab8ced2SViresh Kumar 
589aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_floor(struct device *dev,
590aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
591e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
592e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
593aab8ced2SViresh Kumar {
594e10a4644SViresh Kumar 	return _find_key(dev, key, index, available, read, _compare_floor,
595e10a4644SViresh Kumar 			 assert);
596aab8ced2SViresh Kumar }
597aab8ced2SViresh Kumar 
5987813dd6fSViresh Kumar /**
5997813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
6007813dd6fSViresh Kumar  * @dev:		device for which we do this operation
6017813dd6fSViresh Kumar  * @freq:		frequency to search for
6027813dd6fSViresh Kumar  * @available:		true/false - match for available opp
6037813dd6fSViresh Kumar  *
6047813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
6057813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
6067813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
6077813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6087813dd6fSViresh Kumar  * ERANGE:	no match found for search
6097813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6107813dd6fSViresh Kumar  *
6117813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
6127813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
6137813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
6147813dd6fSViresh Kumar  *
6157813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
6167813dd6fSViresh Kumar  * or the opposite as well.
6177813dd6fSViresh Kumar  *
6187813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6197813dd6fSViresh Kumar  * use.
6207813dd6fSViresh Kumar  */
6217813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
622aab8ced2SViresh Kumar 		unsigned long freq, bool available)
6237813dd6fSViresh Kumar {
624f123ea74SViresh Kumar 	return _find_key_exact(dev, freq, 0, available, _read_freq,
625f123ea74SViresh Kumar 			       assert_single_clk);
6267813dd6fSViresh Kumar }
6277813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
6287813dd6fSViresh Kumar 
6297813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
6307813dd6fSViresh Kumar 						   unsigned long *freq)
6317813dd6fSViresh Kumar {
632e10a4644SViresh Kumar 	return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq,
633f123ea74SViresh Kumar 					assert_single_clk);
6347813dd6fSViresh Kumar }
6357813dd6fSViresh Kumar 
6367813dd6fSViresh Kumar /**
6377813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
6387813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6397813dd6fSViresh Kumar  * @freq:	Start frequency
6407813dd6fSViresh Kumar  *
6417813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
6427813dd6fSViresh Kumar  * for a device.
6437813dd6fSViresh Kumar  *
6447813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6457813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6467813dd6fSViresh Kumar  * values can be:
6477813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6487813dd6fSViresh Kumar  * ERANGE:	no match found for search
6497813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6507813dd6fSViresh Kumar  *
6517813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6527813dd6fSViresh Kumar  * use.
6537813dd6fSViresh Kumar  */
6547813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
6557813dd6fSViresh Kumar 					     unsigned long *freq)
6567813dd6fSViresh Kumar {
657f123ea74SViresh Kumar 	return _find_key_ceil(dev, freq, 0, true, _read_freq, assert_single_clk);
6587813dd6fSViresh Kumar }
6597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
6607813dd6fSViresh Kumar 
6617813dd6fSViresh Kumar /**
6627813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
6637813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6647813dd6fSViresh Kumar  * @freq:	Start frequency
6657813dd6fSViresh Kumar  *
6667813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
6677813dd6fSViresh Kumar  * for a device.
6687813dd6fSViresh Kumar  *
6697813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6707813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6717813dd6fSViresh Kumar  * values can be:
6727813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6737813dd6fSViresh Kumar  * ERANGE:	no match found for search
6747813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6757813dd6fSViresh Kumar  *
6767813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6777813dd6fSViresh Kumar  * use.
6787813dd6fSViresh Kumar  */
6797813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
6807813dd6fSViresh Kumar 					      unsigned long *freq)
6817813dd6fSViresh Kumar {
682f123ea74SViresh Kumar 	return _find_key_floor(dev, freq, 0, true, _read_freq, assert_single_clk);
6837813dd6fSViresh Kumar }
6847813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
6857813dd6fSViresh Kumar 
6862f36bde0SAndrew-sh.Cheng /**
68722079af7SViresh Kumar  * dev_pm_opp_find_level_exact() - search for an exact level
68822079af7SViresh Kumar  * @dev:		device for which we do this operation
68922079af7SViresh Kumar  * @level:		level to search for
69022079af7SViresh Kumar  *
69122079af7SViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
69222079af7SViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
69322079af7SViresh Kumar  * be handled using IS_ERR. Error return values can be:
69422079af7SViresh Kumar  * EINVAL:	for bad pointer
69522079af7SViresh Kumar  * ERANGE:	no match found for search
69622079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
69722079af7SViresh Kumar  *
69822079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
69922079af7SViresh Kumar  * use.
70022079af7SViresh Kumar  */
70122079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
70222079af7SViresh Kumar 					       unsigned int level)
70322079af7SViresh Kumar {
704e10a4644SViresh Kumar 	return _find_key_exact(dev, level, 0, true, _read_level, NULL);
70522079af7SViresh Kumar }
70622079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
70722079af7SViresh Kumar 
70822079af7SViresh Kumar /**
70922079af7SViresh Kumar  * dev_pm_opp_find_level_ceil() - search for an rounded up level
71022079af7SViresh Kumar  * @dev:		device for which we do this operation
71122079af7SViresh Kumar  * @level:		level to search for
71222079af7SViresh Kumar  *
71322079af7SViresh Kumar  * Return: Searches for rounded up match in the opp table and returns pointer
71422079af7SViresh Kumar  * to the  matching opp if found, else returns ERR_PTR in case of error and
71522079af7SViresh Kumar  * should be handled using IS_ERR. Error return values can be:
71622079af7SViresh Kumar  * EINVAL:	for bad pointer
71722079af7SViresh Kumar  * ERANGE:	no match found for search
71822079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
71922079af7SViresh Kumar  *
72022079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
72122079af7SViresh Kumar  * use.
72222079af7SViresh Kumar  */
72322079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
72422079af7SViresh Kumar 					      unsigned int *level)
72522079af7SViresh Kumar {
726c2ab2cb6SViresh Kumar 	unsigned long temp = *level;
727c2ab2cb6SViresh Kumar 	struct dev_pm_opp *opp;
72822079af7SViresh Kumar 
729e10a4644SViresh Kumar 	opp = _find_key_ceil(dev, &temp, 0, true, _read_level, NULL);
730c2ab2cb6SViresh Kumar 	*level = temp;
73122079af7SViresh Kumar 	return opp;
73222079af7SViresh Kumar }
73322079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
73422079af7SViresh Kumar 
73522079af7SViresh Kumar /**
73600ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
73700ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
738617df304SYang Li  * @bw:	start bandwidth
73900ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
74000ce3873SKrzysztof Kozlowski  *
74100ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
74200ce3873SKrzysztof Kozlowski  * for a device.
74300ce3873SKrzysztof Kozlowski  *
74400ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
74500ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
74600ce3873SKrzysztof Kozlowski  * values can be:
74700ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
74800ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
74900ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
75000ce3873SKrzysztof Kozlowski  *
75100ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
75200ce3873SKrzysztof Kozlowski  * use.
75300ce3873SKrzysztof Kozlowski  */
754add1dc09SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
755add1dc09SViresh Kumar 					   int index)
75600ce3873SKrzysztof Kozlowski {
757add1dc09SViresh Kumar 	unsigned long temp = *bw;
758add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
75900ce3873SKrzysztof Kozlowski 
760e10a4644SViresh Kumar 	opp = _find_key_ceil(dev, &temp, index, true, _read_bw, NULL);
761add1dc09SViresh Kumar 	*bw = temp;
76200ce3873SKrzysztof Kozlowski 	return opp;
76300ce3873SKrzysztof Kozlowski }
76400ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
76500ce3873SKrzysztof Kozlowski 
76600ce3873SKrzysztof Kozlowski /**
76700ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
76800ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
769617df304SYang Li  * @bw:	start bandwidth
77000ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
77100ce3873SKrzysztof Kozlowski  *
77200ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
77300ce3873SKrzysztof Kozlowski  * for a device.
77400ce3873SKrzysztof Kozlowski  *
77500ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
77600ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
77700ce3873SKrzysztof Kozlowski  * values can be:
77800ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
77900ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
78000ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
78100ce3873SKrzysztof Kozlowski  *
78200ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
78300ce3873SKrzysztof Kozlowski  * use.
78400ce3873SKrzysztof Kozlowski  */
78500ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
78600ce3873SKrzysztof Kozlowski 					    unsigned int *bw, int index)
78700ce3873SKrzysztof Kozlowski {
788add1dc09SViresh Kumar 	unsigned long temp = *bw;
789add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
79000ce3873SKrzysztof Kozlowski 
791e10a4644SViresh Kumar 	opp = _find_key_floor(dev, &temp, index, true, _read_bw, NULL);
792add1dc09SViresh Kumar 	*bw = temp;
79300ce3873SKrzysztof Kozlowski 	return opp;
79400ce3873SKrzysztof Kozlowski }
79500ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
79600ce3873SKrzysztof Kozlowski 
7977813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
7987813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
7997813dd6fSViresh Kumar {
8007813dd6fSViresh Kumar 	int ret;
8017813dd6fSViresh Kumar 
8027813dd6fSViresh Kumar 	/* Regulator not available for device */
8037813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
8047813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
8057813dd6fSViresh Kumar 			PTR_ERR(reg));
8067813dd6fSViresh Kumar 		return 0;
8077813dd6fSViresh Kumar 	}
8087813dd6fSViresh Kumar 
8097813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
8107813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
8117813dd6fSViresh Kumar 
8127813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
8137813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
8147813dd6fSViresh Kumar 	if (ret)
8157813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
8167813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
8177813dd6fSViresh Kumar 			supply->u_volt_max, ret);
8187813dd6fSViresh Kumar 
8197813dd6fSViresh Kumar 	return ret;
8207813dd6fSViresh Kumar }
8217813dd6fSViresh Kumar 
8222083da24SViresh Kumar static int
8232083da24SViresh Kumar _opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
8242083da24SViresh Kumar 		       struct dev_pm_opp *opp, void *data, bool scaling_down)
8257813dd6fSViresh Kumar {
8261efae8d2SViresh Kumar 	unsigned long *target = data;
8271efae8d2SViresh Kumar 	unsigned long freq;
8287813dd6fSViresh Kumar 	int ret;
8297813dd6fSViresh Kumar 
8301efae8d2SViresh Kumar 	/* One of target and opp must be available */
8311efae8d2SViresh Kumar 	if (target) {
8321efae8d2SViresh Kumar 		freq = *target;
8331efae8d2SViresh Kumar 	} else if (opp) {
8342083da24SViresh Kumar 		freq = opp->rates[0];
8351efae8d2SViresh Kumar 	} else {
8361efae8d2SViresh Kumar 		WARN_ON(1);
8371efae8d2SViresh Kumar 		return -EINVAL;
8381efae8d2SViresh Kumar 	}
8391efae8d2SViresh Kumar 
8401efae8d2SViresh Kumar 	ret = clk_set_rate(opp_table->clk, freq);
8417813dd6fSViresh Kumar 	if (ret) {
8427813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
8437813dd6fSViresh Kumar 			ret);
8441efae8d2SViresh Kumar 	} else {
8451efae8d2SViresh Kumar 		opp_table->rate_clk_single = freq;
8467813dd6fSViresh Kumar 	}
8477813dd6fSViresh Kumar 
8487813dd6fSViresh Kumar 	return ret;
8497813dd6fSViresh Kumar }
8507813dd6fSViresh Kumar 
8518174a3a6SViresh Kumar /*
8528174a3a6SViresh Kumar  * Simple implementation for configuring multiple clocks. Configure clocks in
8538174a3a6SViresh Kumar  * the order in which they are present in the array while scaling up.
8548174a3a6SViresh Kumar  */
8558174a3a6SViresh Kumar int dev_pm_opp_config_clks_simple(struct device *dev,
8568174a3a6SViresh Kumar 		struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
8578174a3a6SViresh Kumar 		bool scaling_down)
8588174a3a6SViresh Kumar {
8598174a3a6SViresh Kumar 	int ret, i;
8608174a3a6SViresh Kumar 
8618174a3a6SViresh Kumar 	if (scaling_down) {
8628174a3a6SViresh Kumar 		for (i = opp_table->clk_count - 1; i >= 0; i--) {
8638174a3a6SViresh Kumar 			ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
8648174a3a6SViresh Kumar 			if (ret) {
8658174a3a6SViresh Kumar 				dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
8668174a3a6SViresh Kumar 					ret);
8678174a3a6SViresh Kumar 				return ret;
8688174a3a6SViresh Kumar 			}
8698174a3a6SViresh Kumar 		}
8708174a3a6SViresh Kumar 	} else {
8718174a3a6SViresh Kumar 		for (i = 0; i < opp_table->clk_count; i++) {
8728174a3a6SViresh Kumar 			ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
8738174a3a6SViresh Kumar 			if (ret) {
8748174a3a6SViresh Kumar 				dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
8758174a3a6SViresh Kumar 					ret);
8768174a3a6SViresh Kumar 				return ret;
8778174a3a6SViresh Kumar 			}
8788174a3a6SViresh Kumar 		}
8798174a3a6SViresh Kumar 	}
8808174a3a6SViresh Kumar 
881d36cb843SChristophe JAILLET 	return 0;
8828174a3a6SViresh Kumar }
8838174a3a6SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_config_clks_simple);
8848174a3a6SViresh Kumar 
885c522ce8aSViresh Kumar static int _opp_config_regulator_single(struct device *dev,
886c522ce8aSViresh Kumar 			struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
887c522ce8aSViresh Kumar 			struct regulator **regulators, unsigned int count)
8887813dd6fSViresh Kumar {
889c522ce8aSViresh Kumar 	struct regulator *reg = regulators[0];
8907813dd6fSViresh Kumar 	int ret;
8917813dd6fSViresh Kumar 
8927813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
893c522ce8aSViresh Kumar 	if (WARN_ON(count > 1)) {
8947813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
8957813dd6fSViresh Kumar 		return -EINVAL;
8967813dd6fSViresh Kumar 	}
8977813dd6fSViresh Kumar 
898c522ce8aSViresh Kumar 	ret = _set_opp_voltage(dev, reg, new_opp->supplies);
8997813dd6fSViresh Kumar 	if (ret)
900c522ce8aSViresh Kumar 		return ret;
9017813dd6fSViresh Kumar 
9028d45719cSKamil Konieczny 	/*
9038d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
9048d45719cSKamil Konieczny 	 * some boot-enabled regulators.
9058d45719cSKamil Konieczny 	 */
906c522ce8aSViresh Kumar 	if (unlikely(!new_opp->opp_table->enabled)) {
9078d45719cSKamil Konieczny 		ret = regulator_enable(reg);
9088d45719cSKamil Konieczny 		if (ret < 0)
9098d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
9108d45719cSKamil Konieczny 	}
9118d45719cSKamil Konieczny 
9127813dd6fSViresh Kumar 	return 0;
9137813dd6fSViresh Kumar }
9147813dd6fSViresh Kumar 
915b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
916240ae50eSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev)
917b00e667aSViresh Kumar {
918b00e667aSViresh Kumar 	u32 avg, peak;
919b00e667aSViresh Kumar 	int i, ret;
920b00e667aSViresh Kumar 
921b00e667aSViresh Kumar 	if (!opp_table->paths)
922b00e667aSViresh Kumar 		return 0;
923b00e667aSViresh Kumar 
924b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
925240ae50eSViresh Kumar 		if (!opp) {
926b00e667aSViresh Kumar 			avg = 0;
927b00e667aSViresh Kumar 			peak = 0;
928b00e667aSViresh Kumar 		} else {
929b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
930b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
931b00e667aSViresh Kumar 		}
932b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
933b00e667aSViresh Kumar 		if (ret) {
934b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
935240ae50eSViresh Kumar 				opp ? "set" : "remove", i, ret);
936b00e667aSViresh Kumar 			return ret;
937b00e667aSViresh Kumar 		}
938b00e667aSViresh Kumar 	}
939b00e667aSViresh Kumar 
940b00e667aSViresh Kumar 	return 0;
941b00e667aSViresh Kumar }
942b00e667aSViresh Kumar 
943528f2d8dSViresh Kumar static int _set_performance_state(struct device *dev, struct device *pd_dev,
94460cdeae0SStephan Gerhold 				  struct dev_pm_opp *opp, int i)
94560cdeae0SStephan Gerhold {
946*7c41cdcdSViresh Kumar 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->level: 0;
94760cdeae0SStephan Gerhold 	int ret;
94860cdeae0SStephan Gerhold 
94960cdeae0SStephan Gerhold 	if (!pd_dev)
95060cdeae0SStephan Gerhold 		return 0;
95160cdeae0SStephan Gerhold 
95260cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
95360cdeae0SStephan Gerhold 	if (ret) {
9549bfb1fffSViresh Kumar 		dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
95560cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
95660cdeae0SStephan Gerhold 	}
95760cdeae0SStephan Gerhold 
95860cdeae0SStephan Gerhold 	return ret;
95960cdeae0SStephan Gerhold }
96060cdeae0SStephan Gerhold 
961528f2d8dSViresh Kumar static int _opp_set_required_opps_generic(struct device *dev,
962528f2d8dSViresh Kumar 	struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
963ca1b5d77SViresh Kumar {
964528f2d8dSViresh Kumar 	dev_err(dev, "setting required-opps isn't supported for non-genpd devices\n");
965528f2d8dSViresh Kumar 	return -ENOENT;
966528f2d8dSViresh Kumar }
967528f2d8dSViresh Kumar 
968528f2d8dSViresh Kumar static int _opp_set_required_opps_genpd(struct device *dev,
969528f2d8dSViresh Kumar 	struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
970528f2d8dSViresh Kumar {
97129b1a92eSViresh Kumar 	struct device **genpd_virt_devs =
97229b1a92eSViresh Kumar 		opp_table->genpd_virt_devs ? opp_table->genpd_virt_devs : &dev;
973ca1b5d77SViresh Kumar 	int i, ret = 0;
974ca1b5d77SViresh Kumar 
975ca1b5d77SViresh Kumar 	/*
976ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
977ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
978ca1b5d77SViresh Kumar 	 */
979ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
980ca1b5d77SViresh Kumar 
9812c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
982528f2d8dSViresh Kumar 	if (!scaling_down) {
983ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
984528f2d8dSViresh Kumar 			ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
98560cdeae0SStephan Gerhold 			if (ret)
986ca1b5d77SViresh Kumar 				break;
987ca1b5d77SViresh Kumar 		}
9882c59138cSStephan Gerhold 	} else {
9892c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
990528f2d8dSViresh Kumar 			ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
9912c59138cSStephan Gerhold 			if (ret)
992ca1b5d77SViresh Kumar 				break;
993ca1b5d77SViresh Kumar 		}
994ca1b5d77SViresh Kumar 	}
9952c59138cSStephan Gerhold 
996ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
997ca1b5d77SViresh Kumar 
998ca1b5d77SViresh Kumar 	return ret;
999ca1b5d77SViresh Kumar }
1000ca1b5d77SViresh Kumar 
1001528f2d8dSViresh Kumar /* This is only called for PM domain for now */
1002528f2d8dSViresh Kumar static int _set_required_opps(struct device *dev, struct opp_table *opp_table,
1003528f2d8dSViresh Kumar 			      struct dev_pm_opp *opp, bool up)
1004528f2d8dSViresh Kumar {
1005528f2d8dSViresh Kumar 	/* required-opps not fully initialized yet */
1006528f2d8dSViresh Kumar 	if (lazy_linking_pending(opp_table))
1007528f2d8dSViresh Kumar 		return -EBUSY;
1008528f2d8dSViresh Kumar 
1009528f2d8dSViresh Kumar 	if (opp_table->set_required_opps)
1010528f2d8dSViresh Kumar 		return opp_table->set_required_opps(dev, opp_table, opp, up);
1011528f2d8dSViresh Kumar 
1012528f2d8dSViresh Kumar 	return 0;
1013528f2d8dSViresh Kumar }
1014528f2d8dSViresh Kumar 
1015528f2d8dSViresh Kumar /* Update set_required_opps handler */
1016528f2d8dSViresh Kumar void _update_set_required_opps(struct opp_table *opp_table)
1017528f2d8dSViresh Kumar {
1018528f2d8dSViresh Kumar 	/* Already set */
1019528f2d8dSViresh Kumar 	if (opp_table->set_required_opps)
1020528f2d8dSViresh Kumar 		return;
1021528f2d8dSViresh Kumar 
1022528f2d8dSViresh Kumar 	/* All required OPPs will belong to genpd or none */
1023528f2d8dSViresh Kumar 	if (opp_table->required_opp_tables[0]->is_genpd)
1024528f2d8dSViresh Kumar 		opp_table->set_required_opps = _opp_set_required_opps_genpd;
1025528f2d8dSViresh Kumar 	else
1026528f2d8dSViresh Kumar 		opp_table->set_required_opps = _opp_set_required_opps_generic;
1027528f2d8dSViresh Kumar }
1028528f2d8dSViresh Kumar 
102981c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
103081c4d8a3SViresh Kumar {
103181c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
103281c4d8a3SViresh Kumar 	unsigned long freq;
103381c4d8a3SViresh Kumar 
103481c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
103581c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
103681c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
103781c4d8a3SViresh Kumar 	}
103881c4d8a3SViresh Kumar 
103981c4d8a3SViresh Kumar 	/*
104081c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
104181c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
104281c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
104381c4d8a3SViresh Kumar 	 */
104481c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
104581c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
104681c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
104781c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
104881c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
104981c4d8a3SViresh Kumar 	}
105081c4d8a3SViresh Kumar 
105181c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
105281c4d8a3SViresh Kumar }
105381c4d8a3SViresh Kumar 
10545ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
1055f3364e17SViresh Kumar {
1056f3364e17SViresh Kumar 	int ret;
1057f3364e17SViresh Kumar 
1058f3364e17SViresh Kumar 	if (!opp_table->enabled)
1059f3364e17SViresh Kumar 		return 0;
1060f3364e17SViresh Kumar 
1061f3364e17SViresh Kumar 	/*
1062f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
1063f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
1064f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
1065f3364e17SViresh Kumar 	 */
1066f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
1067f3364e17SViresh Kumar 		return 0;
1068f3364e17SViresh Kumar 
1069240ae50eSViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev);
1070f3364e17SViresh Kumar 	if (ret)
1071f3364e17SViresh Kumar 		return ret;
1072f3364e17SViresh Kumar 
1073f3364e17SViresh Kumar 	if (opp_table->regulators)
1074f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
1075f3364e17SViresh Kumar 
10762c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
1077f3364e17SViresh Kumar 
1078f3364e17SViresh Kumar 	opp_table->enabled = false;
1079f3364e17SViresh Kumar 	return ret;
1080f3364e17SViresh Kumar }
1081f3364e17SViresh Kumar 
1082386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
10831efae8d2SViresh Kumar 		    struct dev_pm_opp *opp, void *clk_data, bool forced)
10847813dd6fSViresh Kumar {
1085386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
1086f0b88fa4SViresh Kumar 	int scaling_down, ret;
10877813dd6fSViresh Kumar 
1088386ba854SViresh Kumar 	if (unlikely(!opp))
1089386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
1090aca48b61SRajendra Nayak 
109181c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
109281c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
109381c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
10947813dd6fSViresh Kumar 
109581c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
109681c4d8a3SViresh Kumar 
109781c4d8a3SViresh Kumar 	/* Return early if nothing to do */
10981efae8d2SViresh Kumar 	if (!forced && old_opp == opp && opp_table->enabled) {
10999e28f7a7SAdrián Larumbe 		dev_dbg_ratelimited(dev, "%s: OPPs are same, nothing to do\n", __func__);
1100386ba854SViresh Kumar 		return 0;
11017813dd6fSViresh Kumar 	}
11027813dd6fSViresh Kumar 
1103f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
11042083da24SViresh Kumar 		__func__, old_opp->rates[0], opp->rates[0], old_opp->level,
11052083da24SViresh Kumar 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1106f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1107f0b88fa4SViresh Kumar 
11082083da24SViresh Kumar 	scaling_down = _opp_compare_key(opp_table, old_opp, opp);
1109f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1110f0b88fa4SViresh Kumar 		scaling_down = 0;
11117813dd6fSViresh Kumar 
1112ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1113f0b88fa4SViresh Kumar 	if (!scaling_down) {
11142c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1115870d5d96SViresh Kumar 		if (ret) {
1116870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1117386ba854SViresh Kumar 			return ret;
1118ca1b5d77SViresh Kumar 		}
1119ca1b5d77SViresh Kumar 
1120870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1121870d5d96SViresh Kumar 		if (ret) {
1122870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1123870d5d96SViresh Kumar 			return ret;
1124870d5d96SViresh Kumar 		}
1125aee3352fSViresh Kumar 
1126aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1127aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1128aee3352fSViresh Kumar 							   opp_table->regulators,
1129aee3352fSViresh Kumar 							   opp_table->regulator_count);
1130aee3352fSViresh Kumar 			if (ret) {
1131aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1132aee3352fSViresh Kumar 					ret);
1133aee3352fSViresh Kumar 				return ret;
1134aee3352fSViresh Kumar 			}
1135aee3352fSViresh Kumar 		}
1136870d5d96SViresh Kumar 	}
1137870d5d96SViresh Kumar 
11382083da24SViresh Kumar 	if (opp_table->config_clks) {
11392083da24SViresh Kumar 		ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
1140ca1b5d77SViresh Kumar 		if (ret)
1141870d5d96SViresh Kumar 			return ret;
11422083da24SViresh Kumar 	}
1143870d5d96SViresh Kumar 
1144870d5d96SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1145870d5d96SViresh Kumar 	if (scaling_down) {
1146aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1147aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1148aee3352fSViresh Kumar 							   opp_table->regulators,
1149aee3352fSViresh Kumar 							   opp_table->regulator_count);
1150aee3352fSViresh Kumar 			if (ret) {
1151aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1152aee3352fSViresh Kumar 					ret);
1153aee3352fSViresh Kumar 				return ret;
1154aee3352fSViresh Kumar 			}
1155aee3352fSViresh Kumar 		}
1156aee3352fSViresh Kumar 
1157870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1158870d5d96SViresh Kumar 		if (ret) {
1159870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1160870d5d96SViresh Kumar 			return ret;
1161ca1b5d77SViresh Kumar 		}
1162ca1b5d77SViresh Kumar 
1163870d5d96SViresh Kumar 		ret = _set_required_opps(dev, opp_table, opp, false);
1164870d5d96SViresh Kumar 		if (ret) {
1165870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1166870d5d96SViresh Kumar 			return ret;
1167870d5d96SViresh Kumar 		}
1168870d5d96SViresh Kumar 	}
1169870d5d96SViresh Kumar 
117072f80ce4SViresh Kumar 	opp_table->enabled = true;
117181c4d8a3SViresh Kumar 	dev_pm_opp_put(old_opp);
117281c4d8a3SViresh Kumar 
117381c4d8a3SViresh Kumar 	/* Make sure current_opp doesn't get freed */
117481c4d8a3SViresh Kumar 	dev_pm_opp_get(opp);
117581c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
1176fe2af402SGeorgi Djakov 
1177386ba854SViresh Kumar 	return ret;
1178386ba854SViresh Kumar }
1179386ba854SViresh Kumar 
1180386ba854SViresh Kumar /**
1181386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1182386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1183386ba854SViresh Kumar  * @target_freq: frequency to achieve
1184386ba854SViresh Kumar  *
1185386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1186386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1187386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1188386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1189386ba854SViresh Kumar  * frequency.
1190386ba854SViresh Kumar  */
1191386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1192386ba854SViresh Kumar {
1193386ba854SViresh Kumar 	struct opp_table *opp_table;
1194386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1195386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
11961efae8d2SViresh Kumar 	bool forced = false;
1197386ba854SViresh Kumar 	int ret;
1198386ba854SViresh Kumar 
1199386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1200386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1201386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1202386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1203386ba854SViresh Kumar 	}
1204386ba854SViresh Kumar 
1205386ba854SViresh Kumar 	if (target_freq) {
1206386ba854SViresh Kumar 		/*
1207386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1208386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1209386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1210386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1211386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1212386ba854SViresh Kumar 		 */
1213386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
12142083da24SViresh Kumar 			ret = opp_table->config_clks(dev, opp_table, NULL,
12152083da24SViresh Kumar 						     &target_freq, false);
1216386ba854SViresh Kumar 			goto put_opp_table;
1217386ba854SViresh Kumar 		}
1218386ba854SViresh Kumar 
1219386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1220386ba854SViresh Kumar 		if ((long)freq <= 0)
1221386ba854SViresh Kumar 			freq = target_freq;
1222386ba854SViresh Kumar 
1223386ba854SViresh Kumar 		/*
1224386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1225386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1226386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1227386ba854SViresh Kumar 		 */
1228386ba854SViresh Kumar 		temp_freq = freq;
1229386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1230386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1231386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1232386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1233386ba854SViresh Kumar 				__func__, freq, ret);
1234386ba854SViresh Kumar 			goto put_opp_table;
1235386ba854SViresh Kumar 		}
12361efae8d2SViresh Kumar 
12371efae8d2SViresh Kumar 		/*
12381efae8d2SViresh Kumar 		 * An OPP entry specifies the highest frequency at which other
12391efae8d2SViresh Kumar 		 * properties of the OPP entry apply. Even if the new OPP is
12401efae8d2SViresh Kumar 		 * same as the old one, we may still reach here for a different
12411efae8d2SViresh Kumar 		 * value of the frequency. In such a case, do not abort but
12421efae8d2SViresh Kumar 		 * configure the hardware to the desired frequency forcefully.
12431efae8d2SViresh Kumar 		 */
12441efae8d2SViresh Kumar 		forced = opp_table->rate_clk_single != target_freq;
1245386ba854SViresh Kumar 	}
1246386ba854SViresh Kumar 
12471efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, &target_freq, forced);
1248386ba854SViresh Kumar 
1249386ba854SViresh Kumar 	if (target_freq)
12507813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
12511efae8d2SViresh Kumar 
12527813dd6fSViresh Kumar put_opp_table:
12537813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
12547813dd6fSViresh Kumar 	return ret;
12557813dd6fSViresh Kumar }
12567813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
12577813dd6fSViresh Kumar 
1258abbe3483SViresh Kumar /**
1259abbe3483SViresh Kumar  * dev_pm_opp_set_opp() - Configure device for OPP
1260abbe3483SViresh Kumar  * @dev: device for which we do this operation
1261abbe3483SViresh Kumar  * @opp: OPP to set to
1262abbe3483SViresh Kumar  *
1263abbe3483SViresh Kumar  * This configures the device based on the properties of the OPP passed to this
1264abbe3483SViresh Kumar  * routine.
1265abbe3483SViresh Kumar  *
1266abbe3483SViresh Kumar  * Return: 0 on success, a negative error number otherwise.
1267abbe3483SViresh Kumar  */
1268abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1269abbe3483SViresh Kumar {
1270abbe3483SViresh Kumar 	struct opp_table *opp_table;
1271abbe3483SViresh Kumar 	int ret;
1272abbe3483SViresh Kumar 
1273abbe3483SViresh Kumar 	opp_table = _find_opp_table(dev);
1274abbe3483SViresh Kumar 	if (IS_ERR(opp_table)) {
1275abbe3483SViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1276abbe3483SViresh Kumar 		return PTR_ERR(opp_table);
1277abbe3483SViresh Kumar 	}
1278abbe3483SViresh Kumar 
12791efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, NULL, false);
1280abbe3483SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1281abbe3483SViresh Kumar 
1282abbe3483SViresh Kumar 	return ret;
1283abbe3483SViresh Kumar }
1284abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1285abbe3483SViresh Kumar 
12867813dd6fSViresh Kumar /* OPP-dev Helpers */
12877813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
12887813dd6fSViresh Kumar 			    struct opp_table *opp_table)
12897813dd6fSViresh Kumar {
12907813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
12917813dd6fSViresh Kumar 	list_del(&opp_dev->node);
12927813dd6fSViresh Kumar 	kfree(opp_dev);
12937813dd6fSViresh Kumar }
12947813dd6fSViresh Kumar 
1295ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
12967813dd6fSViresh Kumar 				struct opp_table *opp_table)
12977813dd6fSViresh Kumar {
12987813dd6fSViresh Kumar 	struct opp_device *opp_dev;
12997813dd6fSViresh Kumar 
13007813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
13017813dd6fSViresh Kumar 	if (!opp_dev)
13027813dd6fSViresh Kumar 		return NULL;
13037813dd6fSViresh Kumar 
13047813dd6fSViresh Kumar 	/* Initialize opp-dev */
13057813dd6fSViresh Kumar 	opp_dev->dev = dev;
13063d255699SViresh Kumar 
1307ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
13087813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1309ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
13107813dd6fSViresh Kumar 
13117813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1312a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1313283d55e6SViresh Kumar 
1314283d55e6SViresh Kumar 	return opp_dev;
1315283d55e6SViresh Kumar }
1316283d55e6SViresh Kumar 
1317eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
13187813dd6fSViresh Kumar {
13197813dd6fSViresh Kumar 	struct opp_table *opp_table;
13207813dd6fSViresh Kumar 	struct opp_device *opp_dev;
13217813dd6fSViresh Kumar 	int ret;
13227813dd6fSViresh Kumar 
13237813dd6fSViresh Kumar 	/*
13247813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
13257813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
13267813dd6fSViresh Kumar 	 */
13277813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
13287813dd6fSViresh Kumar 	if (!opp_table)
1329dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
13307813dd6fSViresh Kumar 
13313d255699SViresh Kumar 	mutex_init(&opp_table->lock);
13324f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
13337813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
13347eba0c76SViresh Kumar 	INIT_LIST_HEAD(&opp_table->lazy);
13357813dd6fSViresh Kumar 
13362083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
13372083da24SViresh Kumar 
133846f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
133946f48acaSViresh Kumar 	opp_table->regulator_count = -1;
134046f48acaSViresh Kumar 
13417813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
13427813dd6fSViresh Kumar 	if (!opp_dev) {
1343dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1344dd461cd9SStephan Gerhold 		goto err;
13457813dd6fSViresh Kumar 	}
13467813dd6fSViresh Kumar 
1347eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
13487813dd6fSViresh Kumar 
13496d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
13506d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1351dd461cd9SStephan Gerhold 	if (ret) {
1352dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
135332439ac7SViresh Kumar 			goto remove_opp_dev;
1354dd461cd9SStephan Gerhold 
13556d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
13566d3f922cSGeorgi Djakov 			 __func__, ret);
1357dd461cd9SStephan Gerhold 	}
13586d3f922cSGeorgi Djakov 
13597813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
13607813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
13617813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
13627813dd6fSViresh Kumar 
13637813dd6fSViresh Kumar 	return opp_table;
1364dd461cd9SStephan Gerhold 
1365976509bbSQuanyang Wang remove_opp_dev:
1366b2a2ab03SStephan Gerhold 	_of_clear_opp_table(opp_table);
1367976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1368b2a2ab03SStephan Gerhold 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
1369b2a2ab03SStephan Gerhold 	mutex_destroy(&opp_table->lock);
1370dd461cd9SStephan Gerhold err:
1371dd461cd9SStephan Gerhold 	kfree(opp_table);
1372dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
13737813dd6fSViresh Kumar }
13747813dd6fSViresh Kumar 
13757813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
13767813dd6fSViresh Kumar {
13777813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
13787813dd6fSViresh Kumar }
13797813dd6fSViresh Kumar 
138032439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
138132439ac7SViresh Kumar 					       struct opp_table *opp_table,
138232439ac7SViresh Kumar 					       bool getclk)
138332439ac7SViresh Kumar {
1384d4a4c7a4SViresh Kumar 	int ret;
1385d4a4c7a4SViresh Kumar 
138632439ac7SViresh Kumar 	/*
13872083da24SViresh Kumar 	 * Return early if we don't need to get clk or we have already done it
138832439ac7SViresh Kumar 	 * earlier.
138932439ac7SViresh Kumar 	 */
13902083da24SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
13912083da24SViresh Kumar 	    opp_table->clks)
139232439ac7SViresh Kumar 		return opp_table;
139332439ac7SViresh Kumar 
139432439ac7SViresh Kumar 	/* Find clk for the device */
139532439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
139632439ac7SViresh Kumar 
1397d4a4c7a4SViresh Kumar 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
13982083da24SViresh Kumar 	if (!ret) {
13992083da24SViresh Kumar 		opp_table->config_clks = _opp_config_clk_single;
14002083da24SViresh Kumar 		opp_table->clk_count = 1;
140132439ac7SViresh Kumar 		return opp_table;
14022083da24SViresh Kumar 	}
1403d4a4c7a4SViresh Kumar 
1404d4a4c7a4SViresh Kumar 	if (ret == -ENOENT) {
14052083da24SViresh Kumar 		/*
14062083da24SViresh Kumar 		 * There are few platforms which don't want the OPP core to
14072083da24SViresh Kumar 		 * manage device's clock settings. In such cases neither the
14082083da24SViresh Kumar 		 * platform provides the clks explicitly to us, nor the DT
14092083da24SViresh Kumar 		 * contains a valid clk entry. The OPP nodes in DT may still
14102083da24SViresh Kumar 		 * contain "opp-hz" property though, which we need to parse and
14112083da24SViresh Kumar 		 * allow the platform to find an OPP based on freq later on.
14122083da24SViresh Kumar 		 *
14132083da24SViresh Kumar 		 * This is a simple solution to take care of such corner cases,
14142083da24SViresh Kumar 		 * i.e. make the clk_count 1, which lets us allocate space for
14152083da24SViresh Kumar 		 * frequency in opp->rates and also parse the entries in DT.
14162083da24SViresh Kumar 		 */
14172083da24SViresh Kumar 		opp_table->clk_count = 1;
14182083da24SViresh Kumar 
1419d4a4c7a4SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1420d4a4c7a4SViresh Kumar 		return opp_table;
1421d4a4c7a4SViresh Kumar 	}
1422d4a4c7a4SViresh Kumar 
1423d4a4c7a4SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1424d4a4c7a4SViresh Kumar 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1425d4a4c7a4SViresh Kumar 
1426d4a4c7a4SViresh Kumar 	return ERR_PTR(ret);
142732439ac7SViresh Kumar }
142832439ac7SViresh Kumar 
142927c09484SViresh Kumar /*
143027c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
143127c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
143227c09484SViresh Kumar  *
143327c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
143427c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
143527c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
143627c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
143727c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
143827c09484SViresh Kumar  * indirect users of OPP core.
143927c09484SViresh Kumar  *
144027c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
144127c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
144227c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
144327c09484SViresh Kumar  */
144432439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
144532439ac7SViresh Kumar 					 bool getclk)
14467813dd6fSViresh Kumar {
14477813dd6fSViresh Kumar 	struct opp_table *opp_table;
14487813dd6fSViresh Kumar 
144927c09484SViresh Kumar again:
14507813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
14517813dd6fSViresh Kumar 
14527813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
14537813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
14547813dd6fSViresh Kumar 		goto unlock;
14557813dd6fSViresh Kumar 
145627c09484SViresh Kumar 	/*
145727c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
145827c09484SViresh Kumar 	 * another user, wait for it to finish.
145927c09484SViresh Kumar 	 */
146027c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
146127c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
146227c09484SViresh Kumar 		cpu_relax();
146327c09484SViresh Kumar 		goto again;
146427c09484SViresh Kumar 	}
146527c09484SViresh Kumar 
146627c09484SViresh Kumar 	opp_tables_busy = true;
1467283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
146827c09484SViresh Kumar 
146927c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
147027c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
147127c09484SViresh Kumar 
1472283d55e6SViresh Kumar 	if (opp_table) {
1473ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1474283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1475dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1476283d55e6SViresh Kumar 		}
147727c09484SViresh Kumar 
147827c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
147927c09484SViresh Kumar 	} else {
148027c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
148127c09484SViresh Kumar 
148227c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
148327c09484SViresh Kumar 		if (!IS_ERR(opp_table))
148427c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1485283d55e6SViresh Kumar 	}
1486283d55e6SViresh Kumar 
148727c09484SViresh Kumar 	opp_tables_busy = false;
14887813dd6fSViresh Kumar 
14897813dd6fSViresh Kumar unlock:
14907813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
14917813dd6fSViresh Kumar 
149232439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
14937813dd6fSViresh Kumar }
1494eb7c8743SViresh Kumar 
149532439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1496e77dcb0bSViresh Kumar {
149732439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1498e77dcb0bSViresh Kumar }
1499e77dcb0bSViresh Kumar 
1500eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1501eb7c8743SViresh Kumar {
1502e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1503eb7c8743SViresh Kumar }
15047813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
15057813dd6fSViresh Kumar 
15067813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
15077813dd6fSViresh Kumar {
15087813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1509cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
15106d3f922cSGeorgi Djakov 	int i;
15117813dd6fSViresh Kumar 
1512e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1513e0df59deSViresh Kumar 	list_del(&opp_table->node);
1514e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1515e0df59deSViresh Kumar 
151681c4d8a3SViresh Kumar 	if (opp_table->current_opp)
151781c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
151881c4d8a3SViresh Kumar 
15195d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
15205d6d106fSViresh Kumar 
15212083da24SViresh Kumar 	/* Release automatically acquired single clk */
15227813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
15237813dd6fSViresh Kumar 		clk_put(opp_table->clk);
15247813dd6fSViresh Kumar 
15256d3f922cSGeorgi Djakov 	if (opp_table->paths) {
15266d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
15276d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
15286d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
15296d3f922cSGeorgi Djakov 	}
15306d3f922cSGeorgi Djakov 
1531cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1532cdd6ed90SViresh Kumar 
153304bd2eafSViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node)
15347813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
15357813dd6fSViresh Kumar 
15364f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
15377813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
15387813dd6fSViresh Kumar 	kfree(opp_table);
15397813dd6fSViresh Kumar }
15407813dd6fSViresh Kumar 
15417813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
15427813dd6fSViresh Kumar {
15437813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
15447813dd6fSViresh Kumar 		       &opp_table_lock);
15457813dd6fSViresh Kumar }
15467813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
15477813dd6fSViresh Kumar 
15487813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
15497813dd6fSViresh Kumar {
15507813dd6fSViresh Kumar 	kfree(opp);
15517813dd6fSViresh Kumar }
15527813dd6fSViresh Kumar 
1553cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
15547813dd6fSViresh Kumar {
1555cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1556cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1557cf1fac94SViresh Kumar 
1558cf1fac94SViresh Kumar 	list_del(&opp->node);
1559cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1560cf1fac94SViresh Kumar 
15617813dd6fSViresh Kumar 	/*
15627813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
15637813dd6fSViresh Kumar 	 * frequency/voltage list.
15647813dd6fSViresh Kumar 	 */
15657813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
15663466ea2cSLiang He 	_of_clear_opp(opp_table, opp);
15677813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
15687813dd6fSViresh Kumar 	kfree(opp);
15691690d8bbSViresh Kumar }
15707813dd6fSViresh Kumar 
1571a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
15727813dd6fSViresh Kumar {
15737813dd6fSViresh Kumar 	kref_get(&opp->kref);
15747813dd6fSViresh Kumar }
15757813dd6fSViresh Kumar 
15767813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
15777813dd6fSViresh Kumar {
1578cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
15797813dd6fSViresh Kumar }
15807813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
15817813dd6fSViresh Kumar 
15827813dd6fSViresh Kumar /**
15837813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
15847813dd6fSViresh Kumar  * @dev:	device for which we do this operation
15857813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
15867813dd6fSViresh Kumar  *
15877813dd6fSViresh Kumar  * This function removes an opp from the opp table.
15887813dd6fSViresh Kumar  */
15897813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
15907813dd6fSViresh Kumar {
159195073b72SJakob Koschel 	struct dev_pm_opp *opp = NULL, *iter;
15927813dd6fSViresh Kumar 	struct opp_table *opp_table;
15937813dd6fSViresh Kumar 
15947813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
15957813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
15967813dd6fSViresh Kumar 		return;
15977813dd6fSViresh Kumar 
1598f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table))
1599f123ea74SViresh Kumar 		goto put_table;
1600f123ea74SViresh Kumar 
16017813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
16027813dd6fSViresh Kumar 
160395073b72SJakob Koschel 	list_for_each_entry(iter, &opp_table->opp_list, node) {
16042083da24SViresh Kumar 		if (iter->rates[0] == freq) {
160595073b72SJakob Koschel 			opp = iter;
16067813dd6fSViresh Kumar 			break;
16077813dd6fSViresh Kumar 		}
16087813dd6fSViresh Kumar 	}
16097813dd6fSViresh Kumar 
16107813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
16117813dd6fSViresh Kumar 
161295073b72SJakob Koschel 	if (opp) {
16137813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
16140ad8c623SViresh Kumar 
16150ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
16160ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
16177813dd6fSViresh Kumar 	} else {
16187813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
16197813dd6fSViresh Kumar 			 __func__, freq);
16207813dd6fSViresh Kumar 	}
16217813dd6fSViresh Kumar 
1622f123ea74SViresh Kumar put_table:
16230ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
16247813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
16257813dd6fSViresh Kumar }
16267813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
16277813dd6fSViresh Kumar 
1628cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1629cf1fac94SViresh Kumar 					bool dynamic)
1630cf1fac94SViresh Kumar {
1631cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1632cf1fac94SViresh Kumar 
1633cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1634cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1635606a5d42SBeata Michalska 		/*
1636606a5d42SBeata Michalska 		 * Refcount must be dropped only once for each OPP by OPP core,
1637606a5d42SBeata Michalska 		 * do that with help of "removed" flag.
1638606a5d42SBeata Michalska 		 */
1639606a5d42SBeata Michalska 		if (!temp->removed && dynamic == temp->dynamic) {
1640cf1fac94SViresh Kumar 			opp = temp;
1641cf1fac94SViresh Kumar 			break;
1642cf1fac94SViresh Kumar 		}
1643cf1fac94SViresh Kumar 	}
1644cf1fac94SViresh Kumar 
1645cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1646cf1fac94SViresh Kumar 	return opp;
1647cf1fac94SViresh Kumar }
1648cf1fac94SViresh Kumar 
1649606a5d42SBeata Michalska /*
1650606a5d42SBeata Michalska  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1651606a5d42SBeata Michalska  * happen lock less to avoid circular dependency issues. This routine must be
1652606a5d42SBeata Michalska  * called without the opp_table->lock held.
1653606a5d42SBeata Michalska  */
1654606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
165503758d60SViresh Kumar {
1656cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
165703758d60SViresh Kumar 
1658606a5d42SBeata Michalska 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1659606a5d42SBeata Michalska 		opp->removed = true;
1660606a5d42SBeata Michalska 		dev_pm_opp_put(opp);
1661606a5d42SBeata Michalska 
1662606a5d42SBeata Michalska 		/* Drop the references taken by dev_pm_opp_add() */
1663606a5d42SBeata Michalska 		if (dynamic)
1664606a5d42SBeata Michalska 			dev_pm_opp_put_opp_table(opp_table);
1665606a5d42SBeata Michalska 	}
1666606a5d42SBeata Michalska }
1667606a5d42SBeata Michalska 
1668606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table)
1669606a5d42SBeata Michalska {
167003758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
167103758d60SViresh Kumar 
1672922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1673cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1674cf1fac94SViresh Kumar 		return false;
1675922ff075SViresh Kumar 	}
1676922ff075SViresh Kumar 
1677cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1678cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1679cf1fac94SViresh Kumar 		return true;
168003758d60SViresh Kumar 	}
168103758d60SViresh Kumar 
168203758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1683922ff075SViresh Kumar 
1684606a5d42SBeata Michalska 	_opp_remove_all(opp_table, false);
1685cf1fac94SViresh Kumar 	return true;
168603758d60SViresh Kumar }
168703758d60SViresh Kumar 
16881690d8bbSViresh Kumar /**
16891690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
16901690d8bbSViresh Kumar  * @dev:	device for which we do this operation
16911690d8bbSViresh Kumar  *
16921690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
16931690d8bbSViresh Kumar  */
16941690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
16951690d8bbSViresh Kumar {
16961690d8bbSViresh Kumar 	struct opp_table *opp_table;
16971690d8bbSViresh Kumar 
16981690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
16991690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
17001690d8bbSViresh Kumar 		return;
17011690d8bbSViresh Kumar 
1702606a5d42SBeata Michalska 	_opp_remove_all(opp_table, true);
17031690d8bbSViresh Kumar 
17041690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
17051690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17061690d8bbSViresh Kumar }
17071690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
17081690d8bbSViresh Kumar 
1709d6134583SViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table)
17107813dd6fSViresh Kumar {
17117813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
17122083da24SViresh Kumar 	int supply_count, supply_size, icc_size, clk_size;
17137813dd6fSViresh Kumar 
17147813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
1715d6134583SViresh Kumar 	supply_count = opp_table->regulator_count > 0 ?
1716d6134583SViresh Kumar 			opp_table->regulator_count : 1;
17176d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
17182083da24SViresh Kumar 	clk_size = sizeof(*opp->rates) * opp_table->clk_count;
1719d6134583SViresh Kumar 	icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
17207813dd6fSViresh Kumar 
17217813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
17222083da24SViresh Kumar 	opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL);
17237813dd6fSViresh Kumar 	if (!opp)
17247813dd6fSViresh Kumar 		return NULL;
17257813dd6fSViresh Kumar 
17262083da24SViresh Kumar 	/* Put the supplies, bw and clock at the end of the OPP structure */
17277813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
17282083da24SViresh Kumar 
17292083da24SViresh Kumar 	opp->rates = (unsigned long *)(opp->supplies + supply_count);
17302083da24SViresh Kumar 
17316d3f922cSGeorgi Djakov 	if (icc_size)
17322083da24SViresh Kumar 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
17332083da24SViresh Kumar 
17347813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
17357813dd6fSViresh Kumar 
17367813dd6fSViresh Kumar 	return opp;
17377813dd6fSViresh Kumar }
17387813dd6fSViresh Kumar 
17397813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
17407813dd6fSViresh Kumar 					 struct opp_table *opp_table)
17417813dd6fSViresh Kumar {
17427813dd6fSViresh Kumar 	struct regulator *reg;
17437813dd6fSViresh Kumar 	int i;
17447813dd6fSViresh Kumar 
174590e3577bSViresh Kumar 	if (!opp_table->regulators)
174690e3577bSViresh Kumar 		return true;
174790e3577bSViresh Kumar 
17487813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
17497813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
17507813dd6fSViresh Kumar 
17517813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
17527813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
17537813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
17547813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
17557813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
17567813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
17577813dd6fSViresh Kumar 			return false;
17587813dd6fSViresh Kumar 		}
17597813dd6fSViresh Kumar 	}
17607813dd6fSViresh Kumar 
17617813dd6fSViresh Kumar 	return true;
17627813dd6fSViresh Kumar }
17637813dd6fSViresh Kumar 
17642083da24SViresh Kumar static int _opp_compare_rate(struct opp_table *opp_table,
17652083da24SViresh Kumar 			     struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
17662083da24SViresh Kumar {
17672083da24SViresh Kumar 	int i;
17682083da24SViresh Kumar 
17692083da24SViresh Kumar 	for (i = 0; i < opp_table->clk_count; i++) {
17702083da24SViresh Kumar 		if (opp1->rates[i] != opp2->rates[i])
17712083da24SViresh Kumar 			return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
17722083da24SViresh Kumar 	}
17732083da24SViresh Kumar 
17742083da24SViresh Kumar 	/* Same rates for both OPPs */
17752083da24SViresh Kumar 	return 0;
17762083da24SViresh Kumar }
17772083da24SViresh Kumar 
1778274c3e83SViresh Kumar static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1779274c3e83SViresh Kumar 			   struct dev_pm_opp *opp2)
1780274c3e83SViresh Kumar {
1781274c3e83SViresh Kumar 	int i;
1782274c3e83SViresh Kumar 
1783274c3e83SViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
1784274c3e83SViresh Kumar 		if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
1785274c3e83SViresh Kumar 			return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
1786274c3e83SViresh Kumar 	}
1787274c3e83SViresh Kumar 
1788274c3e83SViresh Kumar 	/* Same bw for both OPPs */
1789274c3e83SViresh Kumar 	return 0;
1790274c3e83SViresh Kumar }
1791274c3e83SViresh Kumar 
17928bdac14bSViresh Kumar /*
17938bdac14bSViresh Kumar  * Returns
17948bdac14bSViresh Kumar  * 0: opp1 == opp2
17958bdac14bSViresh Kumar  * 1: opp1 > opp2
17968bdac14bSViresh Kumar  * -1: opp1 < opp2
17978bdac14bSViresh Kumar  */
17982083da24SViresh Kumar int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1,
17992083da24SViresh Kumar 		     struct dev_pm_opp *opp2)
18006c591eecSSaravana Kannan {
18012083da24SViresh Kumar 	int ret;
18022083da24SViresh Kumar 
18032083da24SViresh Kumar 	ret = _opp_compare_rate(opp_table, opp1, opp2);
18042083da24SViresh Kumar 	if (ret)
18052083da24SViresh Kumar 		return ret;
18062083da24SViresh Kumar 
1807274c3e83SViresh Kumar 	ret = _opp_compare_bw(opp_table, opp1, opp2);
1808274c3e83SViresh Kumar 	if (ret)
1809274c3e83SViresh Kumar 		return ret;
18102083da24SViresh Kumar 
18116c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
18126c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
18132083da24SViresh Kumar 
18142083da24SViresh Kumar 	/* Duplicate OPPs */
18156c591eecSSaravana Kannan 	return 0;
18166c591eecSSaravana Kannan }
18176c591eecSSaravana Kannan 
1818a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1819a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1820a1e8c136SViresh Kumar 			     struct list_head **head)
1821a1e8c136SViresh Kumar {
1822a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
18236c591eecSSaravana Kannan 	int opp_cmp;
1824a1e8c136SViresh Kumar 
1825a1e8c136SViresh Kumar 	/*
1826a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1827a1e8c136SViresh Kumar 	 * already present.
1828a1e8c136SViresh Kumar 	 *
1829a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1830a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1831a1e8c136SViresh Kumar 	 * loop.
1832a1e8c136SViresh Kumar 	 */
1833a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
18342083da24SViresh Kumar 		opp_cmp = _opp_compare_key(opp_table, new_opp, opp);
18356c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1836a1e8c136SViresh Kumar 			*head = &opp->node;
1837a1e8c136SViresh Kumar 			continue;
1838a1e8c136SViresh Kumar 		}
1839a1e8c136SViresh Kumar 
18406c591eecSSaravana Kannan 		if (opp_cmp < 0)
1841a1e8c136SViresh Kumar 			return 0;
1842a1e8c136SViresh Kumar 
1843a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1844a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
18452083da24SViresh Kumar 			 __func__, opp->rates[0], opp->supplies[0].u_volt,
18462083da24SViresh Kumar 			 opp->available, new_opp->rates[0],
1847a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1848a1e8c136SViresh Kumar 
1849a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1850a1e8c136SViresh Kumar 		return opp->available &&
1851a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1852a1e8c136SViresh Kumar 	}
1853a1e8c136SViresh Kumar 
1854a1e8c136SViresh Kumar 	return 0;
1855a1e8c136SViresh Kumar }
1856a1e8c136SViresh Kumar 
18577eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count)
18587eba0c76SViresh Kumar {
18597eba0c76SViresh Kumar 	int i;
18607eba0c76SViresh Kumar 
18617eba0c76SViresh Kumar 	for (i = 0; i < count; i++) {
18627eba0c76SViresh Kumar 		if (opp->required_opps[i]->available)
18637eba0c76SViresh Kumar 			continue;
18647eba0c76SViresh Kumar 
18657eba0c76SViresh Kumar 		opp->available = false;
18667eba0c76SViresh Kumar 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
18672083da24SViresh Kumar 			 __func__, opp->required_opps[i]->np, opp->rates[0]);
18687eba0c76SViresh Kumar 		return;
18697eba0c76SViresh Kumar 	}
18707eba0c76SViresh Kumar }
18717eba0c76SViresh Kumar 
18727813dd6fSViresh Kumar /*
18737813dd6fSViresh Kumar  * Returns:
18747813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
18757813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
18767813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
18777813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
18787813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
18797813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
18807813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
18817813dd6fSViresh Kumar  */
18827813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
18834768914bSViresh Kumar 	     struct opp_table *opp_table)
18847813dd6fSViresh Kumar {
18857813dd6fSViresh Kumar 	struct list_head *head;
18867813dd6fSViresh Kumar 	int ret;
18877813dd6fSViresh Kumar 
18887813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
18897813dd6fSViresh Kumar 	head = &opp_table->opp_list;
18907813dd6fSViresh Kumar 
1891a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1892a1e8c136SViresh Kumar 	if (ret) {
18937813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
18947813dd6fSViresh Kumar 		return ret;
18957813dd6fSViresh Kumar 	}
18967813dd6fSViresh Kumar 
18977813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
18987813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
18997813dd6fSViresh Kumar 
19007813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
19017813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
19027813dd6fSViresh Kumar 
1903a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
19047813dd6fSViresh Kumar 
19057813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
19067813dd6fSViresh Kumar 		new_opp->available = false;
19077813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
19082083da24SViresh Kumar 			 __func__, new_opp->rates[0]);
19097813dd6fSViresh Kumar 	}
19107813dd6fSViresh Kumar 
19117eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
19127eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
19137eba0c76SViresh Kumar 		return 0;
1914cf65948dSDmitry Osipenko 
19157eba0c76SViresh Kumar 	_required_opps_available(new_opp, opp_table->required_opp_count);
1916cf65948dSDmitry Osipenko 
19177813dd6fSViresh Kumar 	return 0;
19187813dd6fSViresh Kumar }
19197813dd6fSViresh Kumar 
19207813dd6fSViresh Kumar /**
19217813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
19227813dd6fSViresh Kumar  * @opp_table:	OPP table
19237813dd6fSViresh Kumar  * @dev:	device for which we do this operation
19247813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
19257813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
19267813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
19277813dd6fSViresh Kumar  *
19287813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
19297813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
19307813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
19317813dd6fSViresh Kumar  *
19327813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
19337813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
19347813dd6fSViresh Kumar  *
19357813dd6fSViresh Kumar  * Return:
19367813dd6fSViresh Kumar  * 0		On success OR
19377813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
19387813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
19397813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
19407813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
19417813dd6fSViresh Kumar  */
19427813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
19437813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
19447813dd6fSViresh Kumar {
19457813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
19467813dd6fSViresh Kumar 	unsigned long tol;
19477813dd6fSViresh Kumar 	int ret;
19487813dd6fSViresh Kumar 
1949f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table))
1950f123ea74SViresh Kumar 		return -EINVAL;
1951f123ea74SViresh Kumar 
19527813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
19537813dd6fSViresh Kumar 	if (!new_opp)
19547813dd6fSViresh Kumar 		return -ENOMEM;
19557813dd6fSViresh Kumar 
19567813dd6fSViresh Kumar 	/* populate the opp table */
19572083da24SViresh Kumar 	new_opp->rates[0] = freq;
19587813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
19597813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
19607813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
19617813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
19627813dd6fSViresh Kumar 	new_opp->available = true;
19637813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
19647813dd6fSViresh Kumar 
19654768914bSViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table);
19667813dd6fSViresh Kumar 	if (ret) {
19677813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
19687813dd6fSViresh Kumar 		if (ret == -EBUSY)
19697813dd6fSViresh Kumar 			ret = 0;
19707813dd6fSViresh Kumar 		goto free_opp;
19717813dd6fSViresh Kumar 	}
19727813dd6fSViresh Kumar 
19737813dd6fSViresh Kumar 	/*
19747813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
19757813dd6fSViresh Kumar 	 * frequency/voltage list.
19767813dd6fSViresh Kumar 	 */
19777813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
19787813dd6fSViresh Kumar 	return 0;
19797813dd6fSViresh Kumar 
19807813dd6fSViresh Kumar free_opp:
19817813dd6fSViresh Kumar 	_opp_free(new_opp);
19827813dd6fSViresh Kumar 
19837813dd6fSViresh Kumar 	return ret;
19847813dd6fSViresh Kumar }
19857813dd6fSViresh Kumar 
19867813dd6fSViresh Kumar /**
198789f03984SViresh Kumar  * _opp_set_supported_hw() - Set supported platforms
19887813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
19897813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
19907813dd6fSViresh Kumar  * @count: Number of elements in the array.
19917813dd6fSViresh Kumar  *
19927813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
19937813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
19947813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
19957813dd6fSViresh Kumar  * property.
19967813dd6fSViresh Kumar  */
199789f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table,
19987813dd6fSViresh Kumar 				 const u32 *versions, unsigned int count)
19997813dd6fSViresh Kumar {
200025419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
200125419de1SViresh Kumar 	if (opp_table->supported_hw)
200289f03984SViresh Kumar 		return 0;
20037813dd6fSViresh Kumar 
20047813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
20057813dd6fSViresh Kumar 					GFP_KERNEL);
200689f03984SViresh Kumar 	if (!opp_table->supported_hw)
200789f03984SViresh Kumar 		return -ENOMEM;
20087813dd6fSViresh Kumar 
20097813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
20107813dd6fSViresh Kumar 
201189f03984SViresh Kumar 	return 0;
20127813dd6fSViresh Kumar }
20137813dd6fSViresh Kumar 
20147813dd6fSViresh Kumar /**
201589f03984SViresh Kumar  * _opp_put_supported_hw() - Releases resources blocked for supported hw
201689f03984SViresh Kumar  * @opp_table: OPP table returned by _opp_set_supported_hw().
20177813dd6fSViresh Kumar  *
20187813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
201989f03984SViresh Kumar  * _opp_set_supported_hw(). Until this is called, the opp_table structure
20207813dd6fSViresh Kumar  * will not be freed.
20217813dd6fSViresh Kumar  */
202289f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table)
20237813dd6fSViresh Kumar {
202489f03984SViresh Kumar 	if (opp_table->supported_hw) {
20257813dd6fSViresh Kumar 		kfree(opp_table->supported_hw);
20267813dd6fSViresh Kumar 		opp_table->supported_hw = NULL;
20277813dd6fSViresh Kumar 		opp_table->supported_hw_count = 0;
20287813dd6fSViresh Kumar 	}
20299c4f220fSYangtao Li }
20309c4f220fSYangtao Li 
20319c4f220fSYangtao Li /**
2032298098e5SViresh Kumar  * _opp_set_prop_name() - Set prop-extn name
20337813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
20347813dd6fSViresh Kumar  * @name: name to postfix to properties.
20357813dd6fSViresh Kumar  *
20367813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
20377813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
20387813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
20397813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
20407813dd6fSViresh Kumar  */
2041298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
20427813dd6fSViresh Kumar {
2043878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
20447813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
2045298098e5SViresh Kumar 		opp_table->prop_name = kstrdup(name, GFP_KERNEL);
2046298098e5SViresh Kumar 		if (!opp_table->prop_name)
2047298098e5SViresh Kumar 			return -ENOMEM;
20487813dd6fSViresh Kumar 	}
20497813dd6fSViresh Kumar 
2050298098e5SViresh Kumar 	return 0;
20517813dd6fSViresh Kumar }
20527813dd6fSViresh Kumar 
20537813dd6fSViresh Kumar /**
2054298098e5SViresh Kumar  * _opp_put_prop_name() - Releases resources blocked for prop-name
2055298098e5SViresh Kumar  * @opp_table: OPP table returned by _opp_set_prop_name().
20567813dd6fSViresh Kumar  *
20577813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
2058298098e5SViresh Kumar  * _opp_set_prop_name(). Until this is called, the opp_table structure
20597813dd6fSViresh Kumar  * will not be freed.
20607813dd6fSViresh Kumar  */
2061298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table)
20627813dd6fSViresh Kumar {
2063298098e5SViresh Kumar 	if (opp_table->prop_name) {
20647813dd6fSViresh Kumar 		kfree(opp_table->prop_name);
20657813dd6fSViresh Kumar 		opp_table->prop_name = NULL;
20667813dd6fSViresh Kumar 	}
2067298098e5SViresh Kumar }
20687813dd6fSViresh Kumar 
20697813dd6fSViresh Kumar /**
2070b0ec0942SViresh Kumar  * _opp_set_regulators() - Set regulator names for the device
20717813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
20727813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
20737813dd6fSViresh Kumar  * @count: Number of regulators.
20747813dd6fSViresh Kumar  *
20757813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
20767813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
20777813dd6fSViresh Kumar  * well.
20787813dd6fSViresh Kumar  *
20797813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
20807813dd6fSViresh Kumar  */
2081b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
208287686cc8SViresh Kumar 			       const char * const names[])
20837813dd6fSViresh Kumar {
208487686cc8SViresh Kumar 	const char * const *temp = names;
20857813dd6fSViresh Kumar 	struct regulator *reg;
208687686cc8SViresh Kumar 	int count = 0, ret, i;
208787686cc8SViresh Kumar 
208887686cc8SViresh Kumar 	/* Count number of regulators */
208987686cc8SViresh Kumar 	while (*temp++)
209087686cc8SViresh Kumar 		count++;
209187686cc8SViresh Kumar 
209287686cc8SViresh Kumar 	if (!count)
2093b0ec0942SViresh Kumar 		return -EINVAL;
20947813dd6fSViresh Kumar 
2095779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
2096779b783cSViresh Kumar 	if (opp_table->regulators)
2097b0ec0942SViresh Kumar 		return 0;
20987813dd6fSViresh Kumar 
20997813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
21007813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
21017813dd6fSViresh Kumar 					      GFP_KERNEL);
2102b0ec0942SViresh Kumar 	if (!opp_table->regulators)
2103b0ec0942SViresh Kumar 		return -ENOMEM;
21047813dd6fSViresh Kumar 
21057813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
21067813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
21077813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
2108543256d2SKrzysztof Kozlowski 			ret = dev_err_probe(dev, PTR_ERR(reg),
2109543256d2SKrzysztof Kozlowski 					    "%s: no regulator (%s) found\n",
2110543256d2SKrzysztof Kozlowski 					    __func__, names[i]);
21117813dd6fSViresh Kumar 			goto free_regulators;
21127813dd6fSViresh Kumar 		}
21137813dd6fSViresh Kumar 
21147813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
21157813dd6fSViresh Kumar 	}
21167813dd6fSViresh Kumar 
21177813dd6fSViresh Kumar 	opp_table->regulator_count = count;
21187813dd6fSViresh Kumar 
2119c522ce8aSViresh Kumar 	/* Set generic config_regulators() for single regulators here */
2120c522ce8aSViresh Kumar 	if (count == 1)
2121c522ce8aSViresh Kumar 		opp_table->config_regulators = _opp_config_regulator_single;
2122c522ce8aSViresh Kumar 
2123b0ec0942SViresh Kumar 	return 0;
21247813dd6fSViresh Kumar 
21257813dd6fSViresh Kumar free_regulators:
212624957db1SMarek Szyprowski 	while (i != 0)
212724957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
21287813dd6fSViresh Kumar 
21297813dd6fSViresh Kumar 	kfree(opp_table->regulators);
21307813dd6fSViresh Kumar 	opp_table->regulators = NULL;
213146f48acaSViresh Kumar 	opp_table->regulator_count = -1;
21327813dd6fSViresh Kumar 
2133b0ec0942SViresh Kumar 	return ret;
21347813dd6fSViresh Kumar }
21357813dd6fSViresh Kumar 
21367813dd6fSViresh Kumar /**
2137b0ec0942SViresh Kumar  * _opp_put_regulators() - Releases resources blocked for regulator
2138b0ec0942SViresh Kumar  * @opp_table: OPP table returned from _opp_set_regulators().
21397813dd6fSViresh Kumar  */
2140b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table)
21417813dd6fSViresh Kumar {
21427813dd6fSViresh Kumar 	int i;
21437813dd6fSViresh Kumar 
2144779b783cSViresh Kumar 	if (!opp_table->regulators)
2145b0ec0942SViresh Kumar 		return;
21467813dd6fSViresh Kumar 
214772f80ce4SViresh Kumar 	if (opp_table->enabled) {
21488d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
21498d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
21508d45719cSKamil Konieczny 	}
21518d45719cSKamil Konieczny 
215224957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
21537813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
21547813dd6fSViresh Kumar 
21557813dd6fSViresh Kumar 	kfree(opp_table->regulators);
21567813dd6fSViresh Kumar 	opp_table->regulators = NULL;
215746f48acaSViresh Kumar 	opp_table->regulator_count = -1;
21587813dd6fSViresh Kumar }
215932aee78bSYangtao Li 
21602083da24SViresh Kumar static void _put_clks(struct opp_table *opp_table, int count)
21612083da24SViresh Kumar {
21622083da24SViresh Kumar 	int i;
21632083da24SViresh Kumar 
21642083da24SViresh Kumar 	for (i = count - 1; i >= 0; i--)
21652083da24SViresh Kumar 		clk_put(opp_table->clks[i]);
21662083da24SViresh Kumar 
21672083da24SViresh Kumar 	kfree(opp_table->clks);
21682083da24SViresh Kumar 	opp_table->clks = NULL;
21692083da24SViresh Kumar }
21702083da24SViresh Kumar 
21717813dd6fSViresh Kumar /**
21722368f576SViresh Kumar  * _opp_set_clknames() - Set clk names for the device
21732368f576SViresh Kumar  * @dev: Device for which clk names is being set.
21742368f576SViresh Kumar  * @names: Clk names.
21757813dd6fSViresh Kumar  *
21762368f576SViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointers to the
21772368f576SViresh Kumar  * clocks for the device. Simple cases work fine without using this routine
21782368f576SViresh Kumar  * (i.e. by passing connection-id as NULL), but for a device with multiple
21792368f576SViresh Kumar  * clocks available, the OPP core needs to know the exact names of the clks to
21802368f576SViresh Kumar  * use.
21817813dd6fSViresh Kumar  *
21827813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
21837813dd6fSViresh Kumar  */
21842368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
21852083da24SViresh Kumar 			     const char * const names[],
21862083da24SViresh Kumar 			     config_clks_t config_clks)
21877813dd6fSViresh Kumar {
21882368f576SViresh Kumar 	const char * const *temp = names;
21892083da24SViresh Kumar 	int count = 0, ret, i;
21902083da24SViresh Kumar 	struct clk *clk;
21917813dd6fSViresh Kumar 
21922368f576SViresh Kumar 	/* Count number of clks */
21932368f576SViresh Kumar 	while (*temp++)
21942368f576SViresh Kumar 		count++;
21957813dd6fSViresh Kumar 
21962368f576SViresh Kumar 	/*
21972368f576SViresh Kumar 	 * This is a special case where we have a single clock, whose connection
21982368f576SViresh Kumar 	 * id name is NULL, i.e. first two entries are NULL in the array.
21992368f576SViresh Kumar 	 */
22002368f576SViresh Kumar 	if (!count && !names[1])
22012368f576SViresh Kumar 		count = 1;
22022368f576SViresh Kumar 
22032083da24SViresh Kumar 	/* Fail early for invalid configurations */
22042f71ae1aSViresh Kumar 	if (!count || (!config_clks && count > 1))
22052368f576SViresh Kumar 		return -EINVAL;
22067813dd6fSViresh Kumar 
22070a43452bSViresh Kumar 	/* Another CPU that shares the OPP table has set the clkname ? */
22082083da24SViresh Kumar 	if (opp_table->clks)
22092368f576SViresh Kumar 		return 0;
22100a43452bSViresh Kumar 
22112083da24SViresh Kumar 	opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks),
22122083da24SViresh Kumar 					GFP_KERNEL);
22132083da24SViresh Kumar 	if (!opp_table->clks)
22142083da24SViresh Kumar 		return -ENOMEM;
22157813dd6fSViresh Kumar 
22162083da24SViresh Kumar 	/* Find clks for the device */
22172083da24SViresh Kumar 	for (i = 0; i < count; i++) {
22182083da24SViresh Kumar 		clk = clk_get(dev, names[i]);
22192083da24SViresh Kumar 		if (IS_ERR(clk)) {
22202083da24SViresh Kumar 			ret = dev_err_probe(dev, PTR_ERR(clk),
22212083da24SViresh Kumar 					    "%s: Couldn't find clock with name: %s\n",
22222083da24SViresh Kumar 					    __func__, names[i]);
22232083da24SViresh Kumar 			goto free_clks;
22247813dd6fSViresh Kumar 		}
22257813dd6fSViresh Kumar 
22262083da24SViresh Kumar 		opp_table->clks[i] = clk;
22272083da24SViresh Kumar 	}
22282083da24SViresh Kumar 
22292083da24SViresh Kumar 	opp_table->clk_count = count;
22302f71ae1aSViresh Kumar 	opp_table->config_clks = config_clks;
22312083da24SViresh Kumar 
22322083da24SViresh Kumar 	/* Set generic single clk set here */
22332083da24SViresh Kumar 	if (count == 1) {
22342f71ae1aSViresh Kumar 		if (!opp_table->config_clks)
22352083da24SViresh Kumar 			opp_table->config_clks = _opp_config_clk_single;
22362083da24SViresh Kumar 
22372083da24SViresh Kumar 		/*
22382083da24SViresh Kumar 		 * We could have just dropped the "clk" field and used "clks"
22392083da24SViresh Kumar 		 * everywhere. Instead we kept the "clk" field around for
22402083da24SViresh Kumar 		 * following reasons:
22412083da24SViresh Kumar 		 *
22422083da24SViresh Kumar 		 * - avoiding clks[0] everywhere else.
22432083da24SViresh Kumar 		 * - not running single clk helpers for multiple clk usecase by
22442083da24SViresh Kumar 		 *   mistake.
22452083da24SViresh Kumar 		 *
22462083da24SViresh Kumar 		 * Since this is single-clk case, just update the clk pointer
22472083da24SViresh Kumar 		 * too.
22482083da24SViresh Kumar 		 */
22492083da24SViresh Kumar 		opp_table->clk = opp_table->clks[0];
22502083da24SViresh Kumar 	}
22510a43452bSViresh Kumar 
22522368f576SViresh Kumar 	return 0;
22532083da24SViresh Kumar 
22542083da24SViresh Kumar free_clks:
22552083da24SViresh Kumar 	_put_clks(opp_table, i);
22562083da24SViresh Kumar 	return ret;
22577813dd6fSViresh Kumar }
22587813dd6fSViresh Kumar 
22597813dd6fSViresh Kumar /**
22602368f576SViresh Kumar  * _opp_put_clknames() - Releases resources blocked for clks.
22612368f576SViresh Kumar  * @opp_table: OPP table returned from _opp_set_clknames().
22627813dd6fSViresh Kumar  */
22632368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table)
22647813dd6fSViresh Kumar {
22652083da24SViresh Kumar 	if (!opp_table->clks)
22662083da24SViresh Kumar 		return;
22672083da24SViresh Kumar 
22682083da24SViresh Kumar 	opp_table->config_clks = NULL;
22692083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
22702083da24SViresh Kumar 
22712083da24SViresh Kumar 	_put_clks(opp_table, opp_table->clk_count);
2272a74f681cSYangtao Li }
2273a74f681cSYangtao Li 
2274a74f681cSYangtao Li /**
2275aee3352fSViresh Kumar  * _opp_set_config_regulators_helper() - Register custom set regulator helper.
2276aee3352fSViresh Kumar  * @dev: Device for which the helper is getting registered.
2277aee3352fSViresh Kumar  * @config_regulators: Custom set regulator helper.
2278aee3352fSViresh Kumar  *
2279aee3352fSViresh Kumar  * This is useful to support platforms with multiple regulators per device.
2280aee3352fSViresh Kumar  *
2281aee3352fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
2282aee3352fSViresh Kumar  */
2283aee3352fSViresh Kumar static int _opp_set_config_regulators_helper(struct opp_table *opp_table,
2284aee3352fSViresh Kumar 		struct device *dev, config_regulators_t config_regulators)
2285aee3352fSViresh Kumar {
2286aee3352fSViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
2287aee3352fSViresh Kumar 	if (!opp_table->config_regulators)
2288aee3352fSViresh Kumar 		opp_table->config_regulators = config_regulators;
2289aee3352fSViresh Kumar 
2290aee3352fSViresh Kumar 	return 0;
2291aee3352fSViresh Kumar }
2292aee3352fSViresh Kumar 
2293aee3352fSViresh Kumar /**
2294aee3352fSViresh Kumar  * _opp_put_config_regulators_helper() - Releases resources blocked for
2295aee3352fSViresh Kumar  *					 config_regulators helper.
2296aee3352fSViresh Kumar  * @opp_table: OPP table returned from _opp_set_config_regulators_helper().
2297aee3352fSViresh Kumar  *
2298aee3352fSViresh Kumar  * Release resources blocked for platform specific config_regulators helper.
2299aee3352fSViresh Kumar  */
2300aee3352fSViresh Kumar static void _opp_put_config_regulators_helper(struct opp_table *opp_table)
2301aee3352fSViresh Kumar {
2302aee3352fSViresh Kumar 	if (opp_table->config_regulators)
2303aee3352fSViresh Kumar 		opp_table->config_regulators = NULL;
2304aee3352fSViresh Kumar }
2305aee3352fSViresh Kumar 
2306442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table)
23076319aee1SViresh Kumar {
23086319aee1SViresh Kumar 	int index;
23096319aee1SViresh Kumar 
2310cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2311cb60e960SViresh Kumar 		return;
2312cb60e960SViresh Kumar 
23136319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
23146319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
23156319aee1SViresh Kumar 			continue;
23166319aee1SViresh Kumar 
23176319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
23186319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
23196319aee1SViresh Kumar 	}
2320c0ab9e08SViresh Kumar 
2321c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2322c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
23236319aee1SViresh Kumar }
23246319aee1SViresh Kumar 
23257813dd6fSViresh Kumar /**
2326442e7a17SViresh Kumar  * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
23276319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
23286319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
232917a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
23304f018bc0SViresh Kumar  *
23314f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
23324f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
23334f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
23344f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
23356319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
23366319aee1SViresh Kumar  * we don't need to support that separately.
23374f018bc0SViresh Kumar  *
23384f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
23396319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
23404f018bc0SViresh Kumar  *
23416319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
23426319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2343baea35e4SViresh Kumar  *
2344baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2345baea35e4SViresh Kumar  * "required-opps" are added in DT.
23464f018bc0SViresh Kumar  */
2347442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
23483734b9f2SDmitry Osipenko 			const char * const *names, struct device ***virt_devs)
23494f018bc0SViresh Kumar {
23506319aee1SViresh Kumar 	struct device *virt_dev;
2351baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
23523734b9f2SDmitry Osipenko 	const char * const *name = names;
23534f018bc0SViresh Kumar 
2354cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2355442e7a17SViresh Kumar 		return 0;
23564f018bc0SViresh Kumar 
23576319aee1SViresh Kumar 	/*
23586319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
23596319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
23606319aee1SViresh Kumar 	 * table is added.
23616319aee1SViresh Kumar 	 */
2362442e7a17SViresh Kumar 	if (!opp_table->required_opp_count)
2363442e7a17SViresh Kumar 		return -EPROBE_DEFER;
23646319aee1SViresh Kumar 
23654f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
23664f018bc0SViresh Kumar 
2367c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2368c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2369c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2370c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2371c0ab9e08SViresh Kumar 		goto unlock;
23724f018bc0SViresh Kumar 
23736319aee1SViresh Kumar 	while (*name) {
23746319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
23756319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
23766319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
23776319aee1SViresh Kumar 			goto err;
23786319aee1SViresh Kumar 		}
23794f018bc0SViresh Kumar 
23806319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
23814ea9496cSTang Bin 		if (IS_ERR_OR_NULL(virt_dev)) {
23824ea9496cSTang Bin 			ret = PTR_ERR(virt_dev) ? : -ENODEV;
23836319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
23846319aee1SViresh Kumar 			goto err;
23854f018bc0SViresh Kumar 		}
23864f018bc0SViresh Kumar 
23874f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2388baea35e4SViresh Kumar 		index++;
23896319aee1SViresh Kumar 		name++;
23906319aee1SViresh Kumar 	}
23916319aee1SViresh Kumar 
239217a8f868SViresh Kumar 	if (virt_devs)
239317a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
23944f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23954f018bc0SViresh Kumar 
2396442e7a17SViresh Kumar 	return 0;
23976319aee1SViresh Kumar 
23986319aee1SViresh Kumar err:
2399442e7a17SViresh Kumar 	_detach_genpd(opp_table);
2400c0ab9e08SViresh Kumar unlock:
24016319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2402442e7a17SViresh Kumar 	return ret;
24036319aee1SViresh Kumar 
24044f018bc0SViresh Kumar }
24054f018bc0SViresh Kumar 
24064f018bc0SViresh Kumar /**
2407442e7a17SViresh Kumar  * _opp_detach_genpd() - Detach genpd(s) from the device.
2408442e7a17SViresh Kumar  * @opp_table: OPP table returned by _opp_attach_genpd().
24094f018bc0SViresh Kumar  *
24106319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
24116319aee1SViresh Kumar  * OPP table.
24124f018bc0SViresh Kumar  */
2413442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
24144f018bc0SViresh Kumar {
24154f018bc0SViresh Kumar 	/*
24164f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
24174f018bc0SViresh Kumar 	 * used in parallel.
24184f018bc0SViresh Kumar 	 */
24194f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2420442e7a17SViresh Kumar 	_detach_genpd(opp_table);
24214f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
24224f018bc0SViresh Kumar }
2423b4b9e223SDmitry Osipenko 
242411b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data)
242511b9b663SViresh Kumar {
242611b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_GENPD)
2427442e7a17SViresh Kumar 		_opp_detach_genpd(data->opp_table);
242811b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR)
2429b0ec0942SViresh Kumar 		_opp_put_regulators(data->opp_table);
243011b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_SUPPORTED_HW)
243189f03984SViresh Kumar 		_opp_put_supported_hw(data->opp_table);
24321f378c6eSViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
2433aee3352fSViresh Kumar 		_opp_put_config_regulators_helper(data->opp_table);
243411b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_PROP_NAME)
2435298098e5SViresh Kumar 		_opp_put_prop_name(data->opp_table);
243611b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_CLK)
24372368f576SViresh Kumar 		_opp_put_clknames(data->opp_table);
243811b9b663SViresh Kumar 
243911b9b663SViresh Kumar 	dev_pm_opp_put_opp_table(data->opp_table);
244011b9b663SViresh Kumar 	kfree(data);
244111b9b663SViresh Kumar }
244211b9b663SViresh Kumar 
244311b9b663SViresh Kumar /**
244411b9b663SViresh Kumar  * dev_pm_opp_set_config() - Set OPP configuration for the device.
244511b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
244611b9b663SViresh Kumar  * @config: OPP configuration.
244711b9b663SViresh Kumar  *
244811b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
244911b9b663SViresh Kumar  *
245011b9b663SViresh Kumar  * This must be called before any OPPs are initialized for the device. This may
245111b9b663SViresh Kumar  * be called multiple times for the same OPP table, for example once for each
245211b9b663SViresh Kumar  * CPU that share the same table. This must be balanced by the same number of
245311b9b663SViresh Kumar  * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
245411b9b663SViresh Kumar  *
245511b9b663SViresh Kumar  * This returns a token to the caller, which must be passed to
245611b9b663SViresh Kumar  * dev_pm_opp_clear_config() to free the resources later. The value of the
245711b9b663SViresh Kumar  * returned token will be >= 1 for success and negative for errors. The minimum
245811b9b663SViresh Kumar  * value of 1 is chosen here to make it easy for callers to manage the resource.
245911b9b663SViresh Kumar  */
246011b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
246111b9b663SViresh Kumar {
2462298098e5SViresh Kumar 	struct opp_table *opp_table;
246311b9b663SViresh Kumar 	struct opp_config_data *data;
246411b9b663SViresh Kumar 	unsigned int id;
246511b9b663SViresh Kumar 	int ret;
246611b9b663SViresh Kumar 
246711b9b663SViresh Kumar 	data = kmalloc(sizeof(*data), GFP_KERNEL);
246811b9b663SViresh Kumar 	if (!data)
246911b9b663SViresh Kumar 		return -ENOMEM;
247011b9b663SViresh Kumar 
247111b9b663SViresh Kumar 	opp_table = _add_opp_table(dev, false);
247211b9b663SViresh Kumar 	if (IS_ERR(opp_table)) {
247311b9b663SViresh Kumar 		kfree(data);
247411b9b663SViresh Kumar 		return PTR_ERR(opp_table);
247511b9b663SViresh Kumar 	}
247611b9b663SViresh Kumar 
247711b9b663SViresh Kumar 	data->opp_table = opp_table;
247811b9b663SViresh Kumar 	data->flags = 0;
247911b9b663SViresh Kumar 
248011b9b663SViresh Kumar 	/* This should be called before OPPs are initialized */
248111b9b663SViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
248211b9b663SViresh Kumar 		ret = -EBUSY;
248311b9b663SViresh Kumar 		goto err;
248411b9b663SViresh Kumar 	}
248511b9b663SViresh Kumar 
248611b9b663SViresh Kumar 	/* Configure clocks */
248711b9b663SViresh Kumar 	if (config->clk_names) {
24882083da24SViresh Kumar 		ret = _opp_set_clknames(opp_table, dev, config->clk_names,
24892083da24SViresh Kumar 					config->config_clks);
24902368f576SViresh Kumar 		if (ret)
249111b9b663SViresh Kumar 			goto err;
249211b9b663SViresh Kumar 
249311b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_CLK;
24942083da24SViresh Kumar 	} else if (config->config_clks) {
24952083da24SViresh Kumar 		/* Don't allow config callback without clocks */
24962083da24SViresh Kumar 		ret = -EINVAL;
24972083da24SViresh Kumar 		goto err;
249811b9b663SViresh Kumar 	}
249911b9b663SViresh Kumar 
250011b9b663SViresh Kumar 	/* Configure property names */
250111b9b663SViresh Kumar 	if (config->prop_name) {
2502298098e5SViresh Kumar 		ret = _opp_set_prop_name(opp_table, config->prop_name);
2503298098e5SViresh Kumar 		if (ret)
250411b9b663SViresh Kumar 			goto err;
250511b9b663SViresh Kumar 
250611b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_PROP_NAME;
250711b9b663SViresh Kumar 	}
250811b9b663SViresh Kumar 
2509aee3352fSViresh Kumar 	/* Configure config_regulators helper */
2510aee3352fSViresh Kumar 	if (config->config_regulators) {
2511aee3352fSViresh Kumar 		ret = _opp_set_config_regulators_helper(opp_table, dev,
2512aee3352fSViresh Kumar 						config->config_regulators);
2513aee3352fSViresh Kumar 		if (ret)
2514aee3352fSViresh Kumar 			goto err;
2515aee3352fSViresh Kumar 
2516aee3352fSViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR_HELPER;
2517aee3352fSViresh Kumar 	}
2518aee3352fSViresh Kumar 
251911b9b663SViresh Kumar 	/* Configure supported hardware */
252011b9b663SViresh Kumar 	if (config->supported_hw) {
252189f03984SViresh Kumar 		ret = _opp_set_supported_hw(opp_table, config->supported_hw,
252211b9b663SViresh Kumar 					    config->supported_hw_count);
252389f03984SViresh Kumar 		if (ret)
252411b9b663SViresh Kumar 			goto err;
252511b9b663SViresh Kumar 
252611b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_SUPPORTED_HW;
252711b9b663SViresh Kumar 	}
252811b9b663SViresh Kumar 
252911b9b663SViresh Kumar 	/* Configure supplies */
253011b9b663SViresh Kumar 	if (config->regulator_names) {
2531b0ec0942SViresh Kumar 		ret = _opp_set_regulators(opp_table, dev,
2532b0ec0942SViresh Kumar 					  config->regulator_names);
2533b0ec0942SViresh Kumar 		if (ret)
253411b9b663SViresh Kumar 			goto err;
253511b9b663SViresh Kumar 
253611b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR;
253711b9b663SViresh Kumar 	}
253811b9b663SViresh Kumar 
253911b9b663SViresh Kumar 	/* Attach genpds */
254011b9b663SViresh Kumar 	if (config->genpd_names) {
2541442e7a17SViresh Kumar 		ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
254211b9b663SViresh Kumar 					config->virt_devs);
2543442e7a17SViresh Kumar 		if (ret)
254411b9b663SViresh Kumar 			goto err;
254511b9b663SViresh Kumar 
254611b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_GENPD;
254711b9b663SViresh Kumar 	}
254811b9b663SViresh Kumar 
254911b9b663SViresh Kumar 	ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
255011b9b663SViresh Kumar 		       GFP_KERNEL);
255111b9b663SViresh Kumar 	if (ret)
255211b9b663SViresh Kumar 		goto err;
255311b9b663SViresh Kumar 
255411b9b663SViresh Kumar 	return id;
255511b9b663SViresh Kumar 
255611b9b663SViresh Kumar err:
255711b9b663SViresh Kumar 	_opp_clear_config(data);
255811b9b663SViresh Kumar 	return ret;
255911b9b663SViresh Kumar }
256011b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
256111b9b663SViresh Kumar 
256211b9b663SViresh Kumar /**
256311b9b663SViresh Kumar  * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
256411b9b663SViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_config().
256511b9b663SViresh Kumar  *
256611b9b663SViresh Kumar  * This allows all device OPP configurations to be cleared at once. This must be
256711b9b663SViresh Kumar  * called once for each call made to dev_pm_opp_set_config(), in order to free
256811b9b663SViresh Kumar  * the OPPs properly.
256911b9b663SViresh Kumar  *
257011b9b663SViresh Kumar  * Currently the first call itself ends up freeing all the OPP configurations,
257111b9b663SViresh Kumar  * while the later ones only drop the OPP table reference. This works well for
257211b9b663SViresh Kumar  * now as we would never want to use an half initialized OPP table and want to
257311b9b663SViresh Kumar  * remove the configurations together.
257411b9b663SViresh Kumar  */
257511b9b663SViresh Kumar void dev_pm_opp_clear_config(int token)
257611b9b663SViresh Kumar {
257711b9b663SViresh Kumar 	struct opp_config_data *data;
257811b9b663SViresh Kumar 
257911b9b663SViresh Kumar 	/*
258011b9b663SViresh Kumar 	 * This lets the callers call this unconditionally and keep their code
258111b9b663SViresh Kumar 	 * simple.
258211b9b663SViresh Kumar 	 */
258311b9b663SViresh Kumar 	if (unlikely(token <= 0))
258411b9b663SViresh Kumar 		return;
258511b9b663SViresh Kumar 
258611b9b663SViresh Kumar 	data = xa_erase(&opp_configs, token);
258711b9b663SViresh Kumar 	if (WARN_ON(!data))
258811b9b663SViresh Kumar 		return;
258911b9b663SViresh Kumar 
259011b9b663SViresh Kumar 	_opp_clear_config(data);
259111b9b663SViresh Kumar }
259211b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
259311b9b663SViresh Kumar 
259411b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token)
259511b9b663SViresh Kumar {
259611b9b663SViresh Kumar 	dev_pm_opp_clear_config((unsigned long)token);
259711b9b663SViresh Kumar }
259811b9b663SViresh Kumar 
259911b9b663SViresh Kumar /**
260011b9b663SViresh Kumar  * devm_pm_opp_set_config() - Set OPP configuration for the device.
260111b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
260211b9b663SViresh Kumar  * @config: OPP configuration.
260311b9b663SViresh Kumar  *
260411b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
260511b9b663SViresh Kumar  * This is a resource-managed variant of dev_pm_opp_set_config().
260611b9b663SViresh Kumar  *
260711b9b663SViresh Kumar  * Return: 0 on success and errorno otherwise.
260811b9b663SViresh Kumar  */
260911b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
261011b9b663SViresh Kumar {
261111b9b663SViresh Kumar 	int token = dev_pm_opp_set_config(dev, config);
261211b9b663SViresh Kumar 
261311b9b663SViresh Kumar 	if (token < 0)
261411b9b663SViresh Kumar 		return token;
261511b9b663SViresh Kumar 
261611b9b663SViresh Kumar 	return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
261711b9b663SViresh Kumar 					(void *) ((unsigned long) token));
261811b9b663SViresh Kumar }
261911b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
262011b9b663SViresh Kumar 
26214f018bc0SViresh Kumar /**
26227d8658efSSaravana Kannan  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
26237d8658efSSaravana Kannan  * @src_table: OPP table which has @dst_table as one of its required OPP table.
26247d8658efSSaravana Kannan  * @dst_table: Required OPP table of the @src_table.
26257d8658efSSaravana Kannan  * @src_opp: OPP from the @src_table.
26267d8658efSSaravana Kannan  *
26277d8658efSSaravana Kannan  * This function returns the OPP (present in @dst_table) pointed out by the
26287d8658efSSaravana Kannan  * "required-opps" property of the @src_opp (present in @src_table).
26297d8658efSSaravana Kannan  *
26307d8658efSSaravana Kannan  * The callers are required to call dev_pm_opp_put() for the returned OPP after
26317d8658efSSaravana Kannan  * use.
26327d8658efSSaravana Kannan  *
26337d8658efSSaravana Kannan  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
26347d8658efSSaravana Kannan  */
26357d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
26367d8658efSSaravana Kannan 						 struct opp_table *dst_table,
26377d8658efSSaravana Kannan 						 struct dev_pm_opp *src_opp)
26387d8658efSSaravana Kannan {
26397d8658efSSaravana Kannan 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
26407d8658efSSaravana Kannan 	int i;
26417d8658efSSaravana Kannan 
26427d8658efSSaravana Kannan 	if (!src_table || !dst_table || !src_opp ||
26437d8658efSSaravana Kannan 	    !src_table->required_opp_tables)
26447d8658efSSaravana Kannan 		return ERR_PTR(-EINVAL);
26457d8658efSSaravana Kannan 
26467d8658efSSaravana Kannan 	/* required-opps not fully initialized yet */
26477d8658efSSaravana Kannan 	if (lazy_linking_pending(src_table))
26487d8658efSSaravana Kannan 		return ERR_PTR(-EBUSY);
26497d8658efSSaravana Kannan 
26507d8658efSSaravana Kannan 	for (i = 0; i < src_table->required_opp_count; i++) {
26517d8658efSSaravana Kannan 		if (src_table->required_opp_tables[i] == dst_table) {
26527d8658efSSaravana Kannan 			mutex_lock(&src_table->lock);
26537d8658efSSaravana Kannan 
26547d8658efSSaravana Kannan 			list_for_each_entry(opp, &src_table->opp_list, node) {
26557d8658efSSaravana Kannan 				if (opp == src_opp) {
26567d8658efSSaravana Kannan 					dest_opp = opp->required_opps[i];
26577d8658efSSaravana Kannan 					dev_pm_opp_get(dest_opp);
26587d8658efSSaravana Kannan 					break;
26597d8658efSSaravana Kannan 				}
26607d8658efSSaravana Kannan 			}
26617d8658efSSaravana Kannan 
26627d8658efSSaravana Kannan 			mutex_unlock(&src_table->lock);
26637d8658efSSaravana Kannan 			break;
26647d8658efSSaravana Kannan 		}
26657d8658efSSaravana Kannan 	}
26667d8658efSSaravana Kannan 
26677d8658efSSaravana Kannan 	if (IS_ERR(dest_opp)) {
26687d8658efSSaravana Kannan 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
26697d8658efSSaravana Kannan 		       src_table, dst_table);
26707d8658efSSaravana Kannan 	}
26717d8658efSSaravana Kannan 
26727d8658efSSaravana Kannan 	return dest_opp;
26737d8658efSSaravana Kannan }
26747d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
26757d8658efSSaravana Kannan 
26767d8658efSSaravana Kannan /**
2677c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2678c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2679c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2680c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2681c8a59103SViresh Kumar  *
2682c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2683c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2684c8a59103SViresh Kumar  * performance state set to @pstate.
2685c8a59103SViresh Kumar  *
2686c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2687c8a59103SViresh Kumar  * value on errors.
2688c8a59103SViresh Kumar  */
2689c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2690c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2691c8a59103SViresh Kumar 				       unsigned int pstate)
2692c8a59103SViresh Kumar {
2693c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2694c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2695c8a59103SViresh Kumar 	int i;
2696c8a59103SViresh Kumar 
2697c8a59103SViresh Kumar 	/*
2698c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2699c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2700c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2701c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2702c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2703c8a59103SViresh Kumar 	 */
2704f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2705c8a59103SViresh Kumar 		return pstate;
2706c8a59103SViresh Kumar 
270784cb7ff3SViresh Kumar 	/* Both OPP tables must belong to genpds */
270884cb7ff3SViresh Kumar 	if (unlikely(!src_table->is_genpd || !dst_table->is_genpd)) {
270984cb7ff3SViresh Kumar 		pr_err("%s: Performance state is only valid for genpds.\n", __func__);
271084cb7ff3SViresh Kumar 		return -EINVAL;
271184cb7ff3SViresh Kumar 	}
271284cb7ff3SViresh Kumar 
27137eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
27147eba0c76SViresh Kumar 	if (lazy_linking_pending(src_table))
27157eba0c76SViresh Kumar 		return -EBUSY;
27167eba0c76SViresh Kumar 
2717c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2718c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2719c8a59103SViresh Kumar 			break;
2720c8a59103SViresh Kumar 	}
2721c8a59103SViresh Kumar 
2722c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2723c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2724c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2725c8a59103SViresh Kumar 		return -EINVAL;
2726c8a59103SViresh Kumar 	}
2727c8a59103SViresh Kumar 
2728c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2729c8a59103SViresh Kumar 
2730c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
2731*7c41cdcdSViresh Kumar 		if (opp->level == pstate) {
2732*7c41cdcdSViresh Kumar 			dest_pstate = opp->required_opps[i]->level;
2733c8a59103SViresh Kumar 			goto unlock;
2734c8a59103SViresh Kumar 		}
2735c8a59103SViresh Kumar 	}
2736c8a59103SViresh Kumar 
2737c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2738c8a59103SViresh Kumar 	       dst_table);
2739c8a59103SViresh Kumar 
2740c8a59103SViresh Kumar unlock:
2741c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2742c8a59103SViresh Kumar 
2743c8a59103SViresh Kumar 	return dest_pstate;
2744c8a59103SViresh Kumar }
2745c8a59103SViresh Kumar 
2746c8a59103SViresh Kumar /**
27477813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
27487813dd6fSViresh Kumar  * @dev:	device for which we do this operation
27497813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
27507813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
27517813dd6fSViresh Kumar  *
27527813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
27537813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
27547813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
27557813dd6fSViresh Kumar  *
27567813dd6fSViresh Kumar  * Return:
27577813dd6fSViresh Kumar  * 0		On success OR
27587813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
27597813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
27607813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
27617813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
27627813dd6fSViresh Kumar  */
27637813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
27647813dd6fSViresh Kumar {
27657813dd6fSViresh Kumar 	struct opp_table *opp_table;
27667813dd6fSViresh Kumar 	int ret;
27677813dd6fSViresh Kumar 
276832439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2769dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2770dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
27717813dd6fSViresh Kumar 
277246f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
277346f48acaSViresh Kumar 	opp_table->regulator_count = 1;
277446f48acaSViresh Kumar 
27757813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
27760ad8c623SViresh Kumar 	if (ret)
27777813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
27780ad8c623SViresh Kumar 
27797813dd6fSViresh Kumar 	return ret;
27807813dd6fSViresh Kumar }
27817813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
27827813dd6fSViresh Kumar 
27837813dd6fSViresh Kumar /**
27847813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
27857813dd6fSViresh Kumar  * @dev:		device for which we do this operation
27867813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
27877813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
27887813dd6fSViresh Kumar  *
27897813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
27907813dd6fSViresh Kumar  * which is isolated here.
27917813dd6fSViresh Kumar  *
27927813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
27937813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
27947813dd6fSViresh Kumar  * successful.
27957813dd6fSViresh Kumar  */
27967813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
27977813dd6fSViresh Kumar 				 bool availability_req)
27987813dd6fSViresh Kumar {
27997813dd6fSViresh Kumar 	struct opp_table *opp_table;
28007813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
28017813dd6fSViresh Kumar 	int r = 0;
28027813dd6fSViresh Kumar 
28037813dd6fSViresh Kumar 	/* Find the opp_table */
28047813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
28057813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
28067813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
28077813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
28087813dd6fSViresh Kumar 		return r;
28097813dd6fSViresh Kumar 	}
28107813dd6fSViresh Kumar 
2811f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table)) {
2812f123ea74SViresh Kumar 		r = -EINVAL;
2813f123ea74SViresh Kumar 		goto put_table;
2814f123ea74SViresh Kumar 	}
2815f123ea74SViresh Kumar 
28167813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
28177813dd6fSViresh Kumar 
28187813dd6fSViresh Kumar 	/* Do we have the frequency? */
28197813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
28202083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
28217813dd6fSViresh Kumar 			opp = tmp_opp;
28227813dd6fSViresh Kumar 			break;
28237813dd6fSViresh Kumar 		}
28247813dd6fSViresh Kumar 	}
28257813dd6fSViresh Kumar 
28267813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
28277813dd6fSViresh Kumar 		r = PTR_ERR(opp);
28287813dd6fSViresh Kumar 		goto unlock;
28297813dd6fSViresh Kumar 	}
28307813dd6fSViresh Kumar 
28317813dd6fSViresh Kumar 	/* Is update really needed? */
28327813dd6fSViresh Kumar 	if (opp->available == availability_req)
28337813dd6fSViresh Kumar 		goto unlock;
28347813dd6fSViresh Kumar 
28357813dd6fSViresh Kumar 	opp->available = availability_req;
28367813dd6fSViresh Kumar 
28377813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
28387813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
28397813dd6fSViresh Kumar 
28407813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
28417813dd6fSViresh Kumar 	if (availability_req)
28427813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
28437813dd6fSViresh Kumar 					     opp);
28447813dd6fSViresh Kumar 	else
28457813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
28467813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
28477813dd6fSViresh Kumar 
28487813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
28497813dd6fSViresh Kumar 	goto put_table;
28507813dd6fSViresh Kumar 
28517813dd6fSViresh Kumar unlock:
28527813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
28537813dd6fSViresh Kumar put_table:
28547813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
28557813dd6fSViresh Kumar 	return r;
28567813dd6fSViresh Kumar }
28577813dd6fSViresh Kumar 
28587813dd6fSViresh Kumar /**
285925cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
286025cb20a2SStephen Boyd  * @dev:		device for which we do this operation
286125cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
286225cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
286325cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
286425cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
286525cb20a2SStephen Boyd  *
286625cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
286725cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
286825cb20a2SStephen Boyd  * successful.
286925cb20a2SStephen Boyd  */
287025cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
287125cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
287225cb20a2SStephen Boyd 			      unsigned long u_volt_max)
287325cb20a2SStephen Boyd 
287425cb20a2SStephen Boyd {
287525cb20a2SStephen Boyd 	struct opp_table *opp_table;
287625cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
287725cb20a2SStephen Boyd 	int r = 0;
287825cb20a2SStephen Boyd 
287925cb20a2SStephen Boyd 	/* Find the opp_table */
288025cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
288125cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
288225cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
288325cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
288425cb20a2SStephen Boyd 		return r;
288525cb20a2SStephen Boyd 	}
288625cb20a2SStephen Boyd 
2887f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table)) {
2888f123ea74SViresh Kumar 		r = -EINVAL;
2889f123ea74SViresh Kumar 		goto put_table;
2890f123ea74SViresh Kumar 	}
2891f123ea74SViresh Kumar 
289225cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
289325cb20a2SStephen Boyd 
289425cb20a2SStephen Boyd 	/* Do we have the frequency? */
289525cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
28962083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
289725cb20a2SStephen Boyd 			opp = tmp_opp;
289825cb20a2SStephen Boyd 			break;
289925cb20a2SStephen Boyd 		}
290025cb20a2SStephen Boyd 	}
290125cb20a2SStephen Boyd 
290225cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
290325cb20a2SStephen Boyd 		r = PTR_ERR(opp);
290425cb20a2SStephen Boyd 		goto adjust_unlock;
290525cb20a2SStephen Boyd 	}
290625cb20a2SStephen Boyd 
290725cb20a2SStephen Boyd 	/* Is update really needed? */
290825cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
290925cb20a2SStephen Boyd 		goto adjust_unlock;
291025cb20a2SStephen Boyd 
291125cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
291225cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
291325cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
291425cb20a2SStephen Boyd 
291525cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
291625cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
291725cb20a2SStephen Boyd 
291825cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
291925cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
292025cb20a2SStephen Boyd 				     opp);
292125cb20a2SStephen Boyd 
292225cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
2923f123ea74SViresh Kumar 	goto put_table;
292425cb20a2SStephen Boyd 
292525cb20a2SStephen Boyd adjust_unlock:
292625cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
2927f123ea74SViresh Kumar put_table:
292825cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
292925cb20a2SStephen Boyd 	return r;
293025cb20a2SStephen Boyd }
293103649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
293225cb20a2SStephen Boyd 
293325cb20a2SStephen Boyd /**
29347813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
29357813dd6fSViresh Kumar  * @dev:	device for which we do this operation
29367813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
29377813dd6fSViresh Kumar  *
29387813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
29397813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
29407813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
29417813dd6fSViresh Kumar  *
29427813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
29437813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
29447813dd6fSViresh Kumar  * successful.
29457813dd6fSViresh Kumar  */
29467813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
29477813dd6fSViresh Kumar {
29487813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
29497813dd6fSViresh Kumar }
29507813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
29517813dd6fSViresh Kumar 
29527813dd6fSViresh Kumar /**
29537813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
29547813dd6fSViresh Kumar  * @dev:	device for which we do this operation
29557813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
29567813dd6fSViresh Kumar  *
29577813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
29587813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
29597813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
29607813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
29617813dd6fSViresh Kumar  *
29627813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
29637813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
29647813dd6fSViresh Kumar  * successful.
29657813dd6fSViresh Kumar  */
29667813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
29677813dd6fSViresh Kumar {
29687813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
29697813dd6fSViresh Kumar }
29707813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
29717813dd6fSViresh Kumar 
29727813dd6fSViresh Kumar /**
29737813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
29747813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
29757813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
29767813dd6fSViresh Kumar  *
29777813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
29787813dd6fSViresh Kumar  */
29797813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
29807813dd6fSViresh Kumar {
29817813dd6fSViresh Kumar 	struct opp_table *opp_table;
29827813dd6fSViresh Kumar 	int ret;
29837813dd6fSViresh Kumar 
29847813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29857813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
29867813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
29877813dd6fSViresh Kumar 
29887813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
29897813dd6fSViresh Kumar 
29907813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29917813dd6fSViresh Kumar 
29927813dd6fSViresh Kumar 	return ret;
29937813dd6fSViresh Kumar }
29947813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
29957813dd6fSViresh Kumar 
29967813dd6fSViresh Kumar /**
29977813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
29987813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
29997813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
30007813dd6fSViresh Kumar  *
30017813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
30027813dd6fSViresh Kumar  */
30037813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
30047813dd6fSViresh Kumar 				   struct notifier_block *nb)
30057813dd6fSViresh Kumar {
30067813dd6fSViresh Kumar 	struct opp_table *opp_table;
30077813dd6fSViresh Kumar 	int ret;
30087813dd6fSViresh Kumar 
30097813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
30107813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
30117813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
30127813dd6fSViresh Kumar 
30137813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
30147813dd6fSViresh Kumar 
30157813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
30167813dd6fSViresh Kumar 
30177813dd6fSViresh Kumar 	return ret;
30187813dd6fSViresh Kumar }
30197813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
30207813dd6fSViresh Kumar 
30218aaf6264SViresh Kumar /**
30228aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
30238aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
30248aaf6264SViresh Kumar  *
30258aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
30268aaf6264SViresh Kumar  * dynamically added entries.
30278aaf6264SViresh Kumar  */
30288aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
30297813dd6fSViresh Kumar {
30307813dd6fSViresh Kumar 	struct opp_table *opp_table;
30317813dd6fSViresh Kumar 
30327813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
30337813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
30347813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
30357813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
30367813dd6fSViresh Kumar 
30377813dd6fSViresh Kumar 		if (error != -ENODEV)
30387813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
30397813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
30407813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
30417813dd6fSViresh Kumar 			     error);
30427813dd6fSViresh Kumar 		return;
30437813dd6fSViresh Kumar 	}
30447813dd6fSViresh Kumar 
3045922ff075SViresh Kumar 	/*
3046922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
3047922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
3048922ff075SViresh Kumar 	 **/
3049922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
3050cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
3051cdd6ed90SViresh Kumar 
3052922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
30537813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
30547813dd6fSViresh Kumar }
30557813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
3056ce8073d8SDmitry Osipenko 
3057ce8073d8SDmitry Osipenko /**
3058ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
3059ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
3060ce8073d8SDmitry Osipenko  *
3061ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
3062ce8073d8SDmitry Osipenko  *
3063ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
3064ce8073d8SDmitry Osipenko  */
3065ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
3066ce8073d8SDmitry Osipenko {
3067ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
3068ce8073d8SDmitry Osipenko 	struct regulator *reg;
3069ce8073d8SDmitry Osipenko 	int i, ret = 0;
3070ce8073d8SDmitry Osipenko 
3071ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
3072ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
3073ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
3074ce8073d8SDmitry Osipenko 		return 0;
3075ce8073d8SDmitry Osipenko 
3076ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
3077ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
3078ce8073d8SDmitry Osipenko 		goto put_table;
3079ce8073d8SDmitry Osipenko 
3080ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
3081ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
3082ce8073d8SDmitry Osipenko 		goto put_table;
3083ce8073d8SDmitry Osipenko 
3084ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
3085ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
3086ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
3087ce8073d8SDmitry Osipenko 		if (ret)
3088ce8073d8SDmitry Osipenko 			break;
3089ce8073d8SDmitry Osipenko 	}
3090ce8073d8SDmitry Osipenko put_table:
3091ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
3092ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
3093ce8073d8SDmitry Osipenko 
3094ce8073d8SDmitry Osipenko 	return ret;
3095ce8073d8SDmitry Osipenko }
3096ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
3097