xref: /openbmc/linux/drivers/opp/core.c (revision f123ea74511dfab70598cd584a11ad596454a689)
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 
327eba0c76SViresh Kumar /* OPP tables with uninitialized required OPPs */
337eba0c76SViresh Kumar LIST_HEAD(lazy_opp_tables);
347eba0c76SViresh Kumar 
357813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
367813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
3727c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */
3827c09484SViresh Kumar static bool opp_tables_busy;
397813dd6fSViresh Kumar 
4011b9b663SViresh Kumar /* OPP ID allocator */
4111b9b663SViresh Kumar static DEFINE_XARRAY_ALLOC1(opp_configs);
4211b9b663SViresh Kumar 
439e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
447813dd6fSViresh Kumar {
457813dd6fSViresh Kumar 	struct opp_device *opp_dev;
469e62edacSViresh Kumar 	bool found = false;
477813dd6fSViresh Kumar 
489e62edacSViresh Kumar 	mutex_lock(&opp_table->lock);
497813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
509e62edacSViresh Kumar 		if (opp_dev->dev == dev) {
519e62edacSViresh Kumar 			found = true;
529e62edacSViresh Kumar 			break;
539e62edacSViresh Kumar 		}
547813dd6fSViresh Kumar 
559e62edacSViresh Kumar 	mutex_unlock(&opp_table->lock);
569e62edacSViresh Kumar 	return found;
577813dd6fSViresh Kumar }
587813dd6fSViresh Kumar 
597813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
607813dd6fSViresh Kumar {
617813dd6fSViresh Kumar 	struct opp_table *opp_table;
627813dd6fSViresh Kumar 
637813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
649e62edacSViresh Kumar 		if (_find_opp_dev(dev, opp_table)) {
657813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
667813dd6fSViresh Kumar 			return opp_table;
677813dd6fSViresh Kumar 		}
687813dd6fSViresh Kumar 	}
697813dd6fSViresh Kumar 
707813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
717813dd6fSViresh Kumar }
727813dd6fSViresh Kumar 
737813dd6fSViresh Kumar /**
747813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
757813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
767813dd6fSViresh Kumar  *
777813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
787813dd6fSViresh Kumar  *
797813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
807813dd6fSViresh Kumar  * -EINVAL based on type of error.
817813dd6fSViresh Kumar  *
827813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
837813dd6fSViresh Kumar  */
847813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
857813dd6fSViresh Kumar {
867813dd6fSViresh Kumar 	struct opp_table *opp_table;
877813dd6fSViresh Kumar 
887813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
897813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
907813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
917813dd6fSViresh Kumar 	}
927813dd6fSViresh Kumar 
937813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
947813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
957813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
967813dd6fSViresh Kumar 
977813dd6fSViresh Kumar 	return opp_table;
987813dd6fSViresh Kumar }
997813dd6fSViresh Kumar 
100*f123ea74SViresh Kumar /*
101*f123ea74SViresh Kumar  * Returns true if multiple clocks aren't there, else returns false with WARN.
102*f123ea74SViresh Kumar  *
103*f123ea74SViresh Kumar  * We don't force clk_count == 1 here as there are users who don't have a clock
104*f123ea74SViresh Kumar  * representation in the OPP table and manage the clock configuration themselves
105*f123ea74SViresh Kumar  * in an platform specific way.
106*f123ea74SViresh Kumar  */
107*f123ea74SViresh Kumar static bool assert_single_clk(struct opp_table *opp_table)
108*f123ea74SViresh Kumar {
109*f123ea74SViresh Kumar 	return !WARN_ON(opp_table->clk_count > 1);
110*f123ea74SViresh Kumar }
111*f123ea74SViresh Kumar 
1127813dd6fSViresh Kumar /**
1137813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
1147813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
1157813dd6fSViresh Kumar  *
1167813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
1177813dd6fSViresh Kumar  * return 0
1187813dd6fSViresh Kumar  *
1197813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1207813dd6fSViresh Kumar  */
1217813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1227813dd6fSViresh Kumar {
1237813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1247813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1257813dd6fSViresh Kumar 		return 0;
1267813dd6fSViresh Kumar 	}
1277813dd6fSViresh Kumar 
1287813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1297813dd6fSViresh Kumar }
1307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1317813dd6fSViresh Kumar 
1327813dd6fSViresh Kumar /**
13369b1af17SViresh Kumar  * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp
13469b1af17SViresh Kumar  * @opp:	opp for which voltage has to be returned for
13569b1af17SViresh Kumar  * @supplies:	Placeholder for copying the supply information.
13669b1af17SViresh Kumar  *
13769b1af17SViresh Kumar  * Return: negative error number on failure, 0 otherwise on success after
13869b1af17SViresh Kumar  * setting @supplies.
13969b1af17SViresh Kumar  *
14069b1af17SViresh Kumar  * This can be used for devices with any number of power supplies. The caller
14169b1af17SViresh Kumar  * must ensure the @supplies array must contain space for each regulator.
14269b1af17SViresh Kumar  */
14369b1af17SViresh Kumar int dev_pm_opp_get_supplies(struct dev_pm_opp *opp,
14469b1af17SViresh Kumar 			    struct dev_pm_opp_supply *supplies)
14569b1af17SViresh Kumar {
14669b1af17SViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !supplies) {
14769b1af17SViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
14869b1af17SViresh Kumar 		return -EINVAL;
14969b1af17SViresh Kumar 	}
15069b1af17SViresh Kumar 
15169b1af17SViresh Kumar 	memcpy(supplies, opp->supplies,
15269b1af17SViresh Kumar 	       sizeof(*supplies) * opp->opp_table->regulator_count);
15369b1af17SViresh Kumar 	return 0;
15469b1af17SViresh Kumar }
15569b1af17SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_supplies);
15669b1af17SViresh Kumar 
15769b1af17SViresh Kumar /**
1584f9a7a1dSLukasz Luba  * dev_pm_opp_get_power() - Gets the power corresponding to an opp
1594f9a7a1dSLukasz Luba  * @opp:	opp for which power has to be returned for
1604f9a7a1dSLukasz Luba  *
1614f9a7a1dSLukasz Luba  * Return: power in micro watt corresponding to the opp, else
1624f9a7a1dSLukasz Luba  * return 0
1634f9a7a1dSLukasz Luba  *
1644f9a7a1dSLukasz Luba  * This is useful only for devices with single power supply.
1654f9a7a1dSLukasz Luba  */
1664f9a7a1dSLukasz Luba unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
1674f9a7a1dSLukasz Luba {
1684f9a7a1dSLukasz Luba 	unsigned long opp_power = 0;
1694f9a7a1dSLukasz Luba 	int i;
1704f9a7a1dSLukasz Luba 
1714f9a7a1dSLukasz Luba 	if (IS_ERR_OR_NULL(opp)) {
1724f9a7a1dSLukasz Luba 		pr_err("%s: Invalid parameters\n", __func__);
1734f9a7a1dSLukasz Luba 		return 0;
1744f9a7a1dSLukasz Luba 	}
1754f9a7a1dSLukasz Luba 	for (i = 0; i < opp->opp_table->regulator_count; i++)
1764f9a7a1dSLukasz Luba 		opp_power += opp->supplies[i].u_watt;
1774f9a7a1dSLukasz Luba 
1784f9a7a1dSLukasz Luba 	return opp_power;
1794f9a7a1dSLukasz Luba }
1804f9a7a1dSLukasz Luba EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
1814f9a7a1dSLukasz Luba 
1824f9a7a1dSLukasz Luba /**
1837813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1847813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1857813dd6fSViresh Kumar  *
1867813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1877813dd6fSViresh Kumar  * return 0
1887813dd6fSViresh Kumar  */
1897813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1907813dd6fSViresh Kumar {
19106a8a059SAndrew-sh.Cheng 	if (IS_ERR_OR_NULL(opp)) {
1927813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1937813dd6fSViresh Kumar 		return 0;
1947813dd6fSViresh Kumar 	}
1957813dd6fSViresh Kumar 
196*f123ea74SViresh Kumar 	if (!assert_single_clk(opp->opp_table))
197*f123ea74SViresh Kumar 		return 0;
198*f123ea74SViresh Kumar 
1992083da24SViresh Kumar 	return opp->rates[0];
2007813dd6fSViresh Kumar }
2017813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
2027813dd6fSViresh Kumar 
2037813dd6fSViresh Kumar /**
2045b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
2055b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
2065b93ac54SRajendra Nayak  *
2075b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
2085b93ac54SRajendra Nayak  * return 0.
2095b93ac54SRajendra Nayak  */
2105b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
2115b93ac54SRajendra Nayak {
2125b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2135b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
2145b93ac54SRajendra Nayak 		return 0;
2155b93ac54SRajendra Nayak 	}
2165b93ac54SRajendra Nayak 
2175b93ac54SRajendra Nayak 	return opp->level;
2185b93ac54SRajendra Nayak }
2195b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
2205b93ac54SRajendra Nayak 
2215b93ac54SRajendra Nayak /**
222597ff543SDmitry Osipenko  * dev_pm_opp_get_required_pstate() - Gets the required performance state
223597ff543SDmitry Osipenko  *                                    corresponding to an available opp
224597ff543SDmitry Osipenko  * @opp:	opp for which performance state has to be returned for
225597ff543SDmitry Osipenko  * @index:	index of the required opp
226597ff543SDmitry Osipenko  *
227597ff543SDmitry Osipenko  * Return: performance state read from device tree corresponding to the
228597ff543SDmitry Osipenko  * required opp, else return 0.
229597ff543SDmitry Osipenko  */
230597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
231597ff543SDmitry Osipenko 					    unsigned int index)
232597ff543SDmitry Osipenko {
233597ff543SDmitry Osipenko 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
234597ff543SDmitry Osipenko 	    index >= opp->opp_table->required_opp_count) {
235597ff543SDmitry Osipenko 		pr_err("%s: Invalid parameters\n", __func__);
236597ff543SDmitry Osipenko 		return 0;
237597ff543SDmitry Osipenko 	}
238597ff543SDmitry Osipenko 
2397eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
2407eba0c76SViresh Kumar 	if (lazy_linking_pending(opp->opp_table))
2417eba0c76SViresh Kumar 		return 0;
2427eba0c76SViresh Kumar 
243597ff543SDmitry Osipenko 	return opp->required_opps[index]->pstate;
244597ff543SDmitry Osipenko }
245597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
246597ff543SDmitry Osipenko 
247597ff543SDmitry Osipenko /**
2487813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
2497813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
2507813dd6fSViresh Kumar  *
2517813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
2527813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
2537813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
2547813dd6fSViresh Kumar  *
2557813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
2567813dd6fSViresh Kumar  */
2577813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
2587813dd6fSViresh Kumar {
2597813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2607813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
2617813dd6fSViresh Kumar 		return false;
2627813dd6fSViresh Kumar 	}
2637813dd6fSViresh Kumar 
2647813dd6fSViresh Kumar 	return opp->turbo;
2657813dd6fSViresh Kumar }
2667813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
2677813dd6fSViresh Kumar 
2687813dd6fSViresh Kumar /**
2697813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
2707813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2717813dd6fSViresh Kumar  *
2727813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
2737813dd6fSViresh Kumar  */
2747813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
2757813dd6fSViresh Kumar {
2767813dd6fSViresh Kumar 	struct opp_table *opp_table;
2777813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
2787813dd6fSViresh Kumar 
2797813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2807813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2817813dd6fSViresh Kumar 		return 0;
2827813dd6fSViresh Kumar 
2837813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
2847813dd6fSViresh Kumar 
2857813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2867813dd6fSViresh Kumar 
2877813dd6fSViresh Kumar 	return clock_latency_ns;
2887813dd6fSViresh Kumar }
2897813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
2907813dd6fSViresh Kumar 
2917813dd6fSViresh Kumar /**
2927813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
2937813dd6fSViresh Kumar  * @dev: device for which we do this operation
2947813dd6fSViresh Kumar  *
2957813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
2967813dd6fSViresh Kumar  */
2977813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
2987813dd6fSViresh Kumar {
2997813dd6fSViresh Kumar 	struct opp_table *opp_table;
3007813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
3017813dd6fSViresh Kumar 	struct regulator *reg;
3027813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
3037813dd6fSViresh Kumar 	int ret, i, count;
3047813dd6fSViresh Kumar 	struct {
3057813dd6fSViresh Kumar 		unsigned long min;
3067813dd6fSViresh Kumar 		unsigned long max;
3077813dd6fSViresh Kumar 	} *uV;
3087813dd6fSViresh Kumar 
3097813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3107813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3117813dd6fSViresh Kumar 		return 0;
3127813dd6fSViresh Kumar 
3137813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
31490e3577bSViresh Kumar 	if (!opp_table->regulators)
3157813dd6fSViresh Kumar 		goto put_opp_table;
3167813dd6fSViresh Kumar 
31790e3577bSViresh Kumar 	count = opp_table->regulator_count;
31890e3577bSViresh Kumar 
3197813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
3207813dd6fSViresh Kumar 	if (!uV)
3217813dd6fSViresh Kumar 		goto put_opp_table;
3227813dd6fSViresh Kumar 
3237813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
3247813dd6fSViresh Kumar 
3257813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3267813dd6fSViresh Kumar 		uV[i].min = ~0;
3277813dd6fSViresh Kumar 		uV[i].max = 0;
3287813dd6fSViresh Kumar 
3297813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
3307813dd6fSViresh Kumar 			if (!opp->available)
3317813dd6fSViresh Kumar 				continue;
3327813dd6fSViresh Kumar 
3337813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
3347813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
3357813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
3367813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
3377813dd6fSViresh Kumar 		}
3387813dd6fSViresh Kumar 	}
3397813dd6fSViresh Kumar 
3407813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
3417813dd6fSViresh Kumar 
3427813dd6fSViresh Kumar 	/*
3437813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
3447813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
3457813dd6fSViresh Kumar 	 */
3467813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3477813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
3487813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
3497813dd6fSViresh Kumar 		if (ret > 0)
3507813dd6fSViresh Kumar 			latency_ns += ret * 1000;
3517813dd6fSViresh Kumar 	}
3527813dd6fSViresh Kumar 
3537813dd6fSViresh Kumar 	kfree(uV);
3547813dd6fSViresh Kumar put_opp_table:
3557813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3567813dd6fSViresh Kumar 
3577813dd6fSViresh Kumar 	return latency_ns;
3587813dd6fSViresh Kumar }
3597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
3607813dd6fSViresh Kumar 
3617813dd6fSViresh Kumar /**
3627813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
3637813dd6fSViresh Kumar  *					     nanoseconds
3647813dd6fSViresh Kumar  * @dev: device for which we do this operation
3657813dd6fSViresh Kumar  *
3667813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
3677813dd6fSViresh Kumar  * switch from one OPP to other.
3687813dd6fSViresh Kumar  */
3697813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
3707813dd6fSViresh Kumar {
3717813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
3727813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
3737813dd6fSViresh Kumar }
3747813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
3757813dd6fSViresh Kumar 
3767813dd6fSViresh Kumar /**
3777813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
3787813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3797813dd6fSViresh Kumar  *
3807813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
3817813dd6fSViresh Kumar  * if one is available, else returns 0;
3827813dd6fSViresh Kumar  */
3837813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
3847813dd6fSViresh Kumar {
3857813dd6fSViresh Kumar 	struct opp_table *opp_table;
3867813dd6fSViresh Kumar 	unsigned long freq = 0;
3877813dd6fSViresh Kumar 
3887813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3897813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3907813dd6fSViresh Kumar 		return 0;
3917813dd6fSViresh Kumar 
3927813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
3937813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
3947813dd6fSViresh Kumar 
3957813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3967813dd6fSViresh Kumar 
3977813dd6fSViresh Kumar 	return freq;
3987813dd6fSViresh Kumar }
3997813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
4007813dd6fSViresh Kumar 
401a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
402a1e8c136SViresh Kumar {
403a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
404a1e8c136SViresh Kumar 	int count = 0;
405a1e8c136SViresh Kumar 
406a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
407a1e8c136SViresh Kumar 
408a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
409a1e8c136SViresh Kumar 		if (opp->available)
410a1e8c136SViresh Kumar 			count++;
411a1e8c136SViresh Kumar 	}
412a1e8c136SViresh Kumar 
413a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
414a1e8c136SViresh Kumar 
415a1e8c136SViresh Kumar 	return count;
416a1e8c136SViresh Kumar }
417a1e8c136SViresh Kumar 
4187813dd6fSViresh Kumar /**
4197813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
4207813dd6fSViresh Kumar  * @dev:	device for which we do this operation
4217813dd6fSViresh Kumar  *
4227813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
4237813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
4247813dd6fSViresh Kumar  */
4257813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
4267813dd6fSViresh Kumar {
4277813dd6fSViresh Kumar 	struct opp_table *opp_table;
428a1e8c136SViresh Kumar 	int count;
4297813dd6fSViresh Kumar 
4307813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4317813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
4327813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
433035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
4347813dd6fSViresh Kumar 			__func__, count);
43509f662f9SViresh Kumar 		return count;
4367813dd6fSViresh Kumar 	}
4377813dd6fSViresh Kumar 
438a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
4397813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4407813dd6fSViresh Kumar 
4417813dd6fSViresh Kumar 	return count;
4427813dd6fSViresh Kumar }
4437813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
4447813dd6fSViresh Kumar 
445aab8ced2SViresh Kumar /* Helpers to read keys */
446aab8ced2SViresh Kumar static unsigned long _read_freq(struct dev_pm_opp *opp, int index)
447aab8ced2SViresh Kumar {
4482083da24SViresh Kumar 	return opp->rates[0];
449aab8ced2SViresh Kumar }
450aab8ced2SViresh Kumar 
451c2ab2cb6SViresh Kumar static unsigned long _read_level(struct dev_pm_opp *opp, int index)
452c2ab2cb6SViresh Kumar {
453c2ab2cb6SViresh Kumar 	return opp->level;
454c2ab2cb6SViresh Kumar }
455c2ab2cb6SViresh Kumar 
456add1dc09SViresh Kumar static unsigned long _read_bw(struct dev_pm_opp *opp, int index)
457add1dc09SViresh Kumar {
458add1dc09SViresh Kumar 	return opp->bandwidth[index].peak;
459add1dc09SViresh Kumar }
460add1dc09SViresh Kumar 
461aab8ced2SViresh Kumar /* Generic comparison helpers */
462aab8ced2SViresh Kumar static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
463aab8ced2SViresh Kumar 			   unsigned long opp_key, unsigned long key)
464aab8ced2SViresh Kumar {
465aab8ced2SViresh Kumar 	if (opp_key == key) {
466aab8ced2SViresh Kumar 		*opp = temp_opp;
467aab8ced2SViresh Kumar 		return true;
468aab8ced2SViresh Kumar 	}
469aab8ced2SViresh Kumar 
470aab8ced2SViresh Kumar 	return false;
471aab8ced2SViresh Kumar }
472aab8ced2SViresh Kumar 
473aab8ced2SViresh Kumar static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
474aab8ced2SViresh Kumar 			  unsigned long opp_key, unsigned long key)
475aab8ced2SViresh Kumar {
476aab8ced2SViresh Kumar 	if (opp_key >= key) {
477aab8ced2SViresh Kumar 		*opp = temp_opp;
478aab8ced2SViresh Kumar 		return true;
479aab8ced2SViresh Kumar 	}
480aab8ced2SViresh Kumar 
481aab8ced2SViresh Kumar 	return false;
482aab8ced2SViresh Kumar }
483aab8ced2SViresh Kumar 
484aab8ced2SViresh Kumar static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
485aab8ced2SViresh Kumar 			   unsigned long opp_key, unsigned long key)
486aab8ced2SViresh Kumar {
487aab8ced2SViresh Kumar 	if (opp_key > key)
488aab8ced2SViresh Kumar 		return true;
489aab8ced2SViresh Kumar 
490aab8ced2SViresh Kumar 	*opp = temp_opp;
491aab8ced2SViresh Kumar 	return false;
492aab8ced2SViresh Kumar }
493aab8ced2SViresh Kumar 
494aab8ced2SViresh Kumar /* Generic key finding helpers */
495aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
496aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
497aab8ced2SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
498aab8ced2SViresh Kumar 		bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
499e10a4644SViresh Kumar 				unsigned long opp_key, unsigned long key),
500e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
501aab8ced2SViresh Kumar {
502aab8ced2SViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
503aab8ced2SViresh Kumar 
504e10a4644SViresh Kumar 	/* Assert that the requirement is met */
505e10a4644SViresh Kumar 	if (assert && !assert(opp_table))
506e10a4644SViresh Kumar 		return ERR_PTR(-EINVAL);
507e10a4644SViresh Kumar 
508aab8ced2SViresh Kumar 	mutex_lock(&opp_table->lock);
509aab8ced2SViresh Kumar 
510aab8ced2SViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
511aab8ced2SViresh Kumar 		if (temp_opp->available == available) {
512aab8ced2SViresh Kumar 			if (compare(&opp, temp_opp, read(temp_opp, index), *key))
513aab8ced2SViresh Kumar 				break;
514aab8ced2SViresh Kumar 		}
515aab8ced2SViresh Kumar 	}
516aab8ced2SViresh Kumar 
517aab8ced2SViresh Kumar 	/* Increment the reference count of OPP */
518aab8ced2SViresh Kumar 	if (!IS_ERR(opp)) {
519aab8ced2SViresh Kumar 		*key = read(opp, index);
520aab8ced2SViresh Kumar 		dev_pm_opp_get(opp);
521aab8ced2SViresh Kumar 	}
522aab8ced2SViresh Kumar 
523aab8ced2SViresh Kumar 	mutex_unlock(&opp_table->lock);
524aab8ced2SViresh Kumar 
525aab8ced2SViresh Kumar 	return opp;
526aab8ced2SViresh Kumar }
527aab8ced2SViresh Kumar 
528aab8ced2SViresh Kumar static struct dev_pm_opp *
529aab8ced2SViresh Kumar _find_key(struct device *dev, unsigned long *key, int index, bool available,
530aab8ced2SViresh Kumar 	  unsigned long (*read)(struct dev_pm_opp *opp, int index),
531aab8ced2SViresh Kumar 	  bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
532e10a4644SViresh Kumar 			  unsigned long opp_key, unsigned long key),
533e10a4644SViresh Kumar 	  bool (*assert)(struct opp_table *opp_table))
534aab8ced2SViresh Kumar {
535aab8ced2SViresh Kumar 	struct opp_table *opp_table;
536aab8ced2SViresh Kumar 	struct dev_pm_opp *opp;
537aab8ced2SViresh Kumar 
538aab8ced2SViresh Kumar 	opp_table = _find_opp_table(dev);
539aab8ced2SViresh Kumar 	if (IS_ERR(opp_table)) {
540aab8ced2SViresh Kumar 		dev_err(dev, "%s: OPP table not found (%ld)\n", __func__,
541aab8ced2SViresh Kumar 			PTR_ERR(opp_table));
542aab8ced2SViresh Kumar 		return ERR_CAST(opp_table);
543aab8ced2SViresh Kumar 	}
544aab8ced2SViresh Kumar 
545aab8ced2SViresh Kumar 	opp = _opp_table_find_key(opp_table, key, index, available, read,
546e10a4644SViresh Kumar 				  compare, assert);
547aab8ced2SViresh Kumar 
548aab8ced2SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
549aab8ced2SViresh Kumar 
550aab8ced2SViresh Kumar 	return opp;
551aab8ced2SViresh Kumar }
552aab8ced2SViresh Kumar 
553aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_exact(struct device *dev,
554aab8ced2SViresh Kumar 		unsigned long key, int index, bool available,
555e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
556e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
557aab8ced2SViresh Kumar {
558aab8ced2SViresh Kumar 	/*
559aab8ced2SViresh Kumar 	 * The value of key will be updated here, but will be ignored as the
560aab8ced2SViresh Kumar 	 * caller doesn't need it.
561aab8ced2SViresh Kumar 	 */
562e10a4644SViresh Kumar 	return _find_key(dev, &key, index, available, read, _compare_exact,
563e10a4644SViresh Kumar 			 assert);
564aab8ced2SViresh Kumar }
565aab8ced2SViresh Kumar 
566aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
567aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
568e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
569e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
570aab8ced2SViresh Kumar {
571aab8ced2SViresh Kumar 	return _opp_table_find_key(opp_table, key, index, available, read,
572e10a4644SViresh Kumar 				   _compare_ceil, assert);
573aab8ced2SViresh Kumar }
574aab8ced2SViresh Kumar 
575aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
576aab8ced2SViresh Kumar 		int index, bool available,
577e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
578e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
579aab8ced2SViresh Kumar {
580e10a4644SViresh Kumar 	return _find_key(dev, key, index, available, read, _compare_ceil,
581e10a4644SViresh Kumar 			 assert);
582aab8ced2SViresh Kumar }
583aab8ced2SViresh Kumar 
584aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_floor(struct device *dev,
585aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
586e10a4644SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
587e10a4644SViresh Kumar 		bool (*assert)(struct opp_table *opp_table))
588aab8ced2SViresh Kumar {
589e10a4644SViresh Kumar 	return _find_key(dev, key, index, available, read, _compare_floor,
590e10a4644SViresh Kumar 			 assert);
591aab8ced2SViresh Kumar }
592aab8ced2SViresh Kumar 
5937813dd6fSViresh Kumar /**
5947813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
5957813dd6fSViresh Kumar  * @dev:		device for which we do this operation
5967813dd6fSViresh Kumar  * @freq:		frequency to search for
5977813dd6fSViresh Kumar  * @available:		true/false - match for available opp
5987813dd6fSViresh Kumar  *
5997813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
6007813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
6017813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
6027813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6037813dd6fSViresh Kumar  * ERANGE:	no match found for search
6047813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6057813dd6fSViresh Kumar  *
6067813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
6077813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
6087813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
6097813dd6fSViresh Kumar  *
6107813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
6117813dd6fSViresh Kumar  * or the opposite as well.
6127813dd6fSViresh Kumar  *
6137813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6147813dd6fSViresh Kumar  * use.
6157813dd6fSViresh Kumar  */
6167813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
617aab8ced2SViresh Kumar 		unsigned long freq, bool available)
6187813dd6fSViresh Kumar {
619*f123ea74SViresh Kumar 	return _find_key_exact(dev, freq, 0, available, _read_freq,
620*f123ea74SViresh Kumar 			       assert_single_clk);
6217813dd6fSViresh Kumar }
6227813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
6237813dd6fSViresh Kumar 
6247813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
6257813dd6fSViresh Kumar 						   unsigned long *freq)
6267813dd6fSViresh Kumar {
627e10a4644SViresh Kumar 	return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq,
628*f123ea74SViresh Kumar 					assert_single_clk);
6297813dd6fSViresh Kumar }
6307813dd6fSViresh Kumar 
6317813dd6fSViresh Kumar /**
6327813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
6337813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6347813dd6fSViresh Kumar  * @freq:	Start frequency
6357813dd6fSViresh Kumar  *
6367813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
6377813dd6fSViresh Kumar  * for a device.
6387813dd6fSViresh Kumar  *
6397813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6407813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6417813dd6fSViresh Kumar  * values can be:
6427813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6437813dd6fSViresh Kumar  * ERANGE:	no match found for search
6447813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6457813dd6fSViresh Kumar  *
6467813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6477813dd6fSViresh Kumar  * use.
6487813dd6fSViresh Kumar  */
6497813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
6507813dd6fSViresh Kumar 					     unsigned long *freq)
6517813dd6fSViresh Kumar {
652*f123ea74SViresh Kumar 	return _find_key_ceil(dev, freq, 0, true, _read_freq, assert_single_clk);
6537813dd6fSViresh Kumar }
6547813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
6557813dd6fSViresh Kumar 
6567813dd6fSViresh Kumar /**
6577813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
6587813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6597813dd6fSViresh Kumar  * @freq:	Start frequency
6607813dd6fSViresh Kumar  *
6617813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
6627813dd6fSViresh Kumar  * for a device.
6637813dd6fSViresh Kumar  *
6647813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6657813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6667813dd6fSViresh Kumar  * values can be:
6677813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6687813dd6fSViresh Kumar  * ERANGE:	no match found for search
6697813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6707813dd6fSViresh Kumar  *
6717813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6727813dd6fSViresh Kumar  * use.
6737813dd6fSViresh Kumar  */
6747813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
6757813dd6fSViresh Kumar 					      unsigned long *freq)
6767813dd6fSViresh Kumar {
677*f123ea74SViresh Kumar 	return _find_key_floor(dev, freq, 0, true, _read_freq, assert_single_clk);
6787813dd6fSViresh Kumar }
6797813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
6807813dd6fSViresh Kumar 
6812f36bde0SAndrew-sh.Cheng /**
68222079af7SViresh Kumar  * dev_pm_opp_find_level_exact() - search for an exact level
68322079af7SViresh Kumar  * @dev:		device for which we do this operation
68422079af7SViresh Kumar  * @level:		level to search for
68522079af7SViresh Kumar  *
68622079af7SViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
68722079af7SViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
68822079af7SViresh Kumar  * be handled using IS_ERR. Error return values can be:
68922079af7SViresh Kumar  * EINVAL:	for bad pointer
69022079af7SViresh Kumar  * ERANGE:	no match found for search
69122079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
69222079af7SViresh Kumar  *
69322079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
69422079af7SViresh Kumar  * use.
69522079af7SViresh Kumar  */
69622079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
69722079af7SViresh Kumar 					       unsigned int level)
69822079af7SViresh Kumar {
699e10a4644SViresh Kumar 	return _find_key_exact(dev, level, 0, true, _read_level, NULL);
70022079af7SViresh Kumar }
70122079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
70222079af7SViresh Kumar 
70322079af7SViresh Kumar /**
70422079af7SViresh Kumar  * dev_pm_opp_find_level_ceil() - search for an rounded up level
70522079af7SViresh Kumar  * @dev:		device for which we do this operation
70622079af7SViresh Kumar  * @level:		level to search for
70722079af7SViresh Kumar  *
70822079af7SViresh Kumar  * Return: Searches for rounded up match in the opp table and returns pointer
70922079af7SViresh Kumar  * to the  matching opp if found, else returns ERR_PTR in case of error and
71022079af7SViresh Kumar  * should be handled using IS_ERR. Error return values can be:
71122079af7SViresh Kumar  * EINVAL:	for bad pointer
71222079af7SViresh Kumar  * ERANGE:	no match found for search
71322079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
71422079af7SViresh Kumar  *
71522079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
71622079af7SViresh Kumar  * use.
71722079af7SViresh Kumar  */
71822079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
71922079af7SViresh Kumar 					      unsigned int *level)
72022079af7SViresh Kumar {
721c2ab2cb6SViresh Kumar 	unsigned long temp = *level;
722c2ab2cb6SViresh Kumar 	struct dev_pm_opp *opp;
72322079af7SViresh Kumar 
724e10a4644SViresh Kumar 	opp = _find_key_ceil(dev, &temp, 0, true, _read_level, NULL);
725c2ab2cb6SViresh Kumar 	*level = temp;
72622079af7SViresh Kumar 	return opp;
72722079af7SViresh Kumar }
72822079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
72922079af7SViresh Kumar 
73022079af7SViresh Kumar /**
73100ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
73200ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
733617df304SYang Li  * @bw:	start bandwidth
73400ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
73500ce3873SKrzysztof Kozlowski  *
73600ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
73700ce3873SKrzysztof Kozlowski  * for a device.
73800ce3873SKrzysztof Kozlowski  *
73900ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
74000ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
74100ce3873SKrzysztof Kozlowski  * values can be:
74200ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
74300ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
74400ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
74500ce3873SKrzysztof Kozlowski  *
74600ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
74700ce3873SKrzysztof Kozlowski  * use.
74800ce3873SKrzysztof Kozlowski  */
749add1dc09SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
750add1dc09SViresh Kumar 					   int index)
75100ce3873SKrzysztof Kozlowski {
752add1dc09SViresh Kumar 	unsigned long temp = *bw;
753add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
75400ce3873SKrzysztof Kozlowski 
755e10a4644SViresh Kumar 	opp = _find_key_ceil(dev, &temp, index, true, _read_bw, NULL);
756add1dc09SViresh Kumar 	*bw = temp;
75700ce3873SKrzysztof Kozlowski 	return opp;
75800ce3873SKrzysztof Kozlowski }
75900ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
76000ce3873SKrzysztof Kozlowski 
76100ce3873SKrzysztof Kozlowski /**
76200ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
76300ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
764617df304SYang Li  * @bw:	start bandwidth
76500ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
76600ce3873SKrzysztof Kozlowski  *
76700ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
76800ce3873SKrzysztof Kozlowski  * for a device.
76900ce3873SKrzysztof Kozlowski  *
77000ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
77100ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
77200ce3873SKrzysztof Kozlowski  * values can be:
77300ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
77400ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
77500ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
77600ce3873SKrzysztof Kozlowski  *
77700ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
77800ce3873SKrzysztof Kozlowski  * use.
77900ce3873SKrzysztof Kozlowski  */
78000ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
78100ce3873SKrzysztof Kozlowski 					    unsigned int *bw, int index)
78200ce3873SKrzysztof Kozlowski {
783add1dc09SViresh Kumar 	unsigned long temp = *bw;
784add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
78500ce3873SKrzysztof Kozlowski 
786e10a4644SViresh Kumar 	opp = _find_key_floor(dev, &temp, index, true, _read_bw, NULL);
787add1dc09SViresh Kumar 	*bw = temp;
78800ce3873SKrzysztof Kozlowski 	return opp;
78900ce3873SKrzysztof Kozlowski }
79000ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
79100ce3873SKrzysztof Kozlowski 
7927813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
7937813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
7947813dd6fSViresh Kumar {
7957813dd6fSViresh Kumar 	int ret;
7967813dd6fSViresh Kumar 
7977813dd6fSViresh Kumar 	/* Regulator not available for device */
7987813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
7997813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
8007813dd6fSViresh Kumar 			PTR_ERR(reg));
8017813dd6fSViresh Kumar 		return 0;
8027813dd6fSViresh Kumar 	}
8037813dd6fSViresh Kumar 
8047813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
8057813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
8067813dd6fSViresh Kumar 
8077813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
8087813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
8097813dd6fSViresh Kumar 	if (ret)
8107813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
8117813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
8127813dd6fSViresh Kumar 			supply->u_volt_max, ret);
8137813dd6fSViresh Kumar 
8147813dd6fSViresh Kumar 	return ret;
8157813dd6fSViresh Kumar }
8167813dd6fSViresh Kumar 
8172083da24SViresh Kumar static int
8182083da24SViresh Kumar _opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
8192083da24SViresh Kumar 		       struct dev_pm_opp *opp, void *data, bool scaling_down)
8207813dd6fSViresh Kumar {
8211efae8d2SViresh Kumar 	unsigned long *target = data;
8221efae8d2SViresh Kumar 	unsigned long freq;
8237813dd6fSViresh Kumar 	int ret;
8247813dd6fSViresh Kumar 
8251efae8d2SViresh Kumar 	/* One of target and opp must be available */
8261efae8d2SViresh Kumar 	if (target) {
8271efae8d2SViresh Kumar 		freq = *target;
8281efae8d2SViresh Kumar 	} else if (opp) {
8292083da24SViresh Kumar 		freq = opp->rates[0];
8301efae8d2SViresh Kumar 	} else {
8311efae8d2SViresh Kumar 		WARN_ON(1);
8321efae8d2SViresh Kumar 		return -EINVAL;
8331efae8d2SViresh Kumar 	}
8341efae8d2SViresh Kumar 
8351efae8d2SViresh Kumar 	ret = clk_set_rate(opp_table->clk, freq);
8367813dd6fSViresh Kumar 	if (ret) {
8377813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
8387813dd6fSViresh Kumar 			ret);
8391efae8d2SViresh Kumar 	} else {
8401efae8d2SViresh Kumar 		opp_table->rate_clk_single = freq;
8417813dd6fSViresh Kumar 	}
8427813dd6fSViresh Kumar 
8437813dd6fSViresh Kumar 	return ret;
8447813dd6fSViresh Kumar }
8457813dd6fSViresh Kumar 
846c522ce8aSViresh Kumar static int _opp_config_regulator_single(struct device *dev,
847c522ce8aSViresh Kumar 			struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
848c522ce8aSViresh Kumar 			struct regulator **regulators, unsigned int count)
8497813dd6fSViresh Kumar {
850c522ce8aSViresh Kumar 	struct regulator *reg = regulators[0];
8517813dd6fSViresh Kumar 	int ret;
8527813dd6fSViresh Kumar 
8537813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
854c522ce8aSViresh Kumar 	if (WARN_ON(count > 1)) {
8557813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
8567813dd6fSViresh Kumar 		return -EINVAL;
8577813dd6fSViresh Kumar 	}
8587813dd6fSViresh Kumar 
859c522ce8aSViresh Kumar 	ret = _set_opp_voltage(dev, reg, new_opp->supplies);
8607813dd6fSViresh Kumar 	if (ret)
861c522ce8aSViresh Kumar 		return ret;
8627813dd6fSViresh Kumar 
8638d45719cSKamil Konieczny 	/*
8648d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
8658d45719cSKamil Konieczny 	 * some boot-enabled regulators.
8668d45719cSKamil Konieczny 	 */
867c522ce8aSViresh Kumar 	if (unlikely(!new_opp->opp_table->enabled)) {
8688d45719cSKamil Konieczny 		ret = regulator_enable(reg);
8698d45719cSKamil Konieczny 		if (ret < 0)
8708d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
8718d45719cSKamil Konieczny 	}
8728d45719cSKamil Konieczny 
8737813dd6fSViresh Kumar 	return 0;
8747813dd6fSViresh Kumar }
8757813dd6fSViresh Kumar 
876b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
877240ae50eSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev)
878b00e667aSViresh Kumar {
879b00e667aSViresh Kumar 	u32 avg, peak;
880b00e667aSViresh Kumar 	int i, ret;
881b00e667aSViresh Kumar 
882b00e667aSViresh Kumar 	if (!opp_table->paths)
883b00e667aSViresh Kumar 		return 0;
884b00e667aSViresh Kumar 
885b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
886240ae50eSViresh Kumar 		if (!opp) {
887b00e667aSViresh Kumar 			avg = 0;
888b00e667aSViresh Kumar 			peak = 0;
889b00e667aSViresh Kumar 		} else {
890b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
891b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
892b00e667aSViresh Kumar 		}
893b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
894b00e667aSViresh Kumar 		if (ret) {
895b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
896240ae50eSViresh Kumar 				opp ? "set" : "remove", i, ret);
897b00e667aSViresh Kumar 			return ret;
898b00e667aSViresh Kumar 		}
899b00e667aSViresh Kumar 	}
900b00e667aSViresh Kumar 
901b00e667aSViresh Kumar 	return 0;
902b00e667aSViresh Kumar }
903b00e667aSViresh Kumar 
90460cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev,
90560cdeae0SStephan Gerhold 			     struct dev_pm_opp *opp, int i)
90660cdeae0SStephan Gerhold {
90760cdeae0SStephan Gerhold 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
90860cdeae0SStephan Gerhold 	int ret;
90960cdeae0SStephan Gerhold 
91060cdeae0SStephan Gerhold 	if (!pd_dev)
91160cdeae0SStephan Gerhold 		return 0;
91260cdeae0SStephan Gerhold 
91360cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
91460cdeae0SStephan Gerhold 	if (ret) {
9159bfb1fffSViresh Kumar 		dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
91660cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
91760cdeae0SStephan Gerhold 	}
91860cdeae0SStephan Gerhold 
91960cdeae0SStephan Gerhold 	return ret;
92060cdeae0SStephan Gerhold }
92160cdeae0SStephan Gerhold 
922ca1b5d77SViresh Kumar /* This is only called for PM domain for now */
923ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev,
924ca1b5d77SViresh Kumar 			      struct opp_table *opp_table,
9252c59138cSStephan Gerhold 			      struct dev_pm_opp *opp, bool up)
926ca1b5d77SViresh Kumar {
927ca1b5d77SViresh Kumar 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
928ca1b5d77SViresh Kumar 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
929ca1b5d77SViresh Kumar 	int i, ret = 0;
930ca1b5d77SViresh Kumar 
931ca1b5d77SViresh Kumar 	if (!required_opp_tables)
932ca1b5d77SViresh Kumar 		return 0;
933ca1b5d77SViresh Kumar 
93419526d09SMarijn Suijten 	/* required-opps not fully initialized yet */
93519526d09SMarijn Suijten 	if (lazy_linking_pending(opp_table))
93619526d09SMarijn Suijten 		return -EBUSY;
93719526d09SMarijn Suijten 
9384fa82a87SHsin-Yi Wang 	/*
9394fa82a87SHsin-Yi Wang 	 * We only support genpd's OPPs in the "required-opps" for now, as we
9404fa82a87SHsin-Yi Wang 	 * don't know much about other use cases. Error out if the required OPP
9414fa82a87SHsin-Yi Wang 	 * doesn't belong to a genpd.
9424fa82a87SHsin-Yi Wang 	 */
9434fa82a87SHsin-Yi Wang 	if (unlikely(!required_opp_tables[0]->is_genpd)) {
9444fa82a87SHsin-Yi Wang 		dev_err(dev, "required-opps don't belong to a genpd\n");
9454fa82a87SHsin-Yi Wang 		return -ENOENT;
9464fa82a87SHsin-Yi Wang 	}
9474fa82a87SHsin-Yi Wang 
948ca1b5d77SViresh Kumar 	/* Single genpd case */
94960cdeae0SStephan Gerhold 	if (!genpd_virt_devs)
95060cdeae0SStephan Gerhold 		return _set_required_opp(dev, dev, opp, 0);
951ca1b5d77SViresh Kumar 
952ca1b5d77SViresh Kumar 	/* Multiple genpd case */
953ca1b5d77SViresh Kumar 
954ca1b5d77SViresh Kumar 	/*
955ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
956ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
957ca1b5d77SViresh Kumar 	 */
958ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
959ca1b5d77SViresh Kumar 
9602c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
9612c59138cSStephan Gerhold 	if (up) {
962ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
96360cdeae0SStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
96460cdeae0SStephan Gerhold 			if (ret)
965ca1b5d77SViresh Kumar 				break;
966ca1b5d77SViresh Kumar 		}
9672c59138cSStephan Gerhold 	} else {
9682c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
9692c59138cSStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
9702c59138cSStephan Gerhold 			if (ret)
971ca1b5d77SViresh Kumar 				break;
972ca1b5d77SViresh Kumar 		}
973ca1b5d77SViresh Kumar 	}
9742c59138cSStephan Gerhold 
975ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
976ca1b5d77SViresh Kumar 
977ca1b5d77SViresh Kumar 	return ret;
978ca1b5d77SViresh Kumar }
979ca1b5d77SViresh Kumar 
98081c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
98181c4d8a3SViresh Kumar {
98281c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
98381c4d8a3SViresh Kumar 	unsigned long freq;
98481c4d8a3SViresh Kumar 
98581c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
98681c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
98781c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
98881c4d8a3SViresh Kumar 	}
98981c4d8a3SViresh Kumar 
99081c4d8a3SViresh Kumar 	/*
99181c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
99281c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
99381c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
99481c4d8a3SViresh Kumar 	 */
99581c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
99681c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
99781c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
99881c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
99981c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
100081c4d8a3SViresh Kumar 	}
100181c4d8a3SViresh Kumar 
100281c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
100381c4d8a3SViresh Kumar }
100481c4d8a3SViresh Kumar 
10055ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
1006f3364e17SViresh Kumar {
1007f3364e17SViresh Kumar 	int ret;
1008f3364e17SViresh Kumar 
1009f3364e17SViresh Kumar 	if (!opp_table->enabled)
1010f3364e17SViresh Kumar 		return 0;
1011f3364e17SViresh Kumar 
1012f3364e17SViresh Kumar 	/*
1013f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
1014f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
1015f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
1016f3364e17SViresh Kumar 	 */
1017f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
1018f3364e17SViresh Kumar 		return 0;
1019f3364e17SViresh Kumar 
1020240ae50eSViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev);
1021f3364e17SViresh Kumar 	if (ret)
1022f3364e17SViresh Kumar 		return ret;
1023f3364e17SViresh Kumar 
1024f3364e17SViresh Kumar 	if (opp_table->regulators)
1025f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
1026f3364e17SViresh Kumar 
10272c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
1028f3364e17SViresh Kumar 
1029f3364e17SViresh Kumar 	opp_table->enabled = false;
1030f3364e17SViresh Kumar 	return ret;
1031f3364e17SViresh Kumar }
1032f3364e17SViresh Kumar 
1033386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
10341efae8d2SViresh Kumar 		    struct dev_pm_opp *opp, void *clk_data, bool forced)
10357813dd6fSViresh Kumar {
1036386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
1037f0b88fa4SViresh Kumar 	int scaling_down, ret;
10387813dd6fSViresh Kumar 
1039386ba854SViresh Kumar 	if (unlikely(!opp))
1040386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
1041aca48b61SRajendra Nayak 
104281c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
104381c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
104481c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
10457813dd6fSViresh Kumar 
104681c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
104781c4d8a3SViresh Kumar 
104881c4d8a3SViresh Kumar 	/* Return early if nothing to do */
10491efae8d2SViresh Kumar 	if (!forced && old_opp == opp && opp_table->enabled) {
105081c4d8a3SViresh Kumar 		dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
1051386ba854SViresh Kumar 		return 0;
10527813dd6fSViresh Kumar 	}
10537813dd6fSViresh Kumar 
1054f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
10552083da24SViresh Kumar 		__func__, old_opp->rates[0], opp->rates[0], old_opp->level,
10562083da24SViresh Kumar 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1057f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1058f0b88fa4SViresh Kumar 
10592083da24SViresh Kumar 	scaling_down = _opp_compare_key(opp_table, old_opp, opp);
1060f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1061f0b88fa4SViresh Kumar 		scaling_down = 0;
10627813dd6fSViresh Kumar 
1063ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1064f0b88fa4SViresh Kumar 	if (!scaling_down) {
10652c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1066870d5d96SViresh Kumar 		if (ret) {
1067870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1068386ba854SViresh Kumar 			return ret;
1069ca1b5d77SViresh Kumar 		}
1070ca1b5d77SViresh Kumar 
1071870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1072870d5d96SViresh Kumar 		if (ret) {
1073870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1074870d5d96SViresh Kumar 			return ret;
1075870d5d96SViresh Kumar 		}
1076aee3352fSViresh Kumar 
1077aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1078aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1079aee3352fSViresh Kumar 							   opp_table->regulators,
1080aee3352fSViresh Kumar 							   opp_table->regulator_count);
1081aee3352fSViresh Kumar 			if (ret) {
1082aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1083aee3352fSViresh Kumar 					ret);
1084aee3352fSViresh Kumar 				return ret;
1085aee3352fSViresh Kumar 			}
1086aee3352fSViresh Kumar 		}
1087870d5d96SViresh Kumar 	}
1088870d5d96SViresh Kumar 
10892083da24SViresh Kumar 	if (opp_table->config_clks) {
10902083da24SViresh Kumar 		ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
1091ca1b5d77SViresh Kumar 		if (ret)
1092870d5d96SViresh Kumar 			return ret;
10932083da24SViresh Kumar 	}
1094870d5d96SViresh Kumar 
1095870d5d96SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1096870d5d96SViresh Kumar 	if (scaling_down) {
1097aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1098aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1099aee3352fSViresh Kumar 							   opp_table->regulators,
1100aee3352fSViresh Kumar 							   opp_table->regulator_count);
1101aee3352fSViresh Kumar 			if (ret) {
1102aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1103aee3352fSViresh Kumar 					ret);
1104aee3352fSViresh Kumar 				return ret;
1105aee3352fSViresh Kumar 			}
1106aee3352fSViresh Kumar 		}
1107aee3352fSViresh Kumar 
1108870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1109870d5d96SViresh Kumar 		if (ret) {
1110870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1111870d5d96SViresh Kumar 			return ret;
1112ca1b5d77SViresh Kumar 		}
1113ca1b5d77SViresh Kumar 
1114870d5d96SViresh Kumar 		ret = _set_required_opps(dev, opp_table, opp, false);
1115870d5d96SViresh Kumar 		if (ret) {
1116870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1117870d5d96SViresh Kumar 			return ret;
1118870d5d96SViresh Kumar 		}
1119870d5d96SViresh Kumar 	}
1120870d5d96SViresh Kumar 
112172f80ce4SViresh Kumar 	opp_table->enabled = true;
112281c4d8a3SViresh Kumar 	dev_pm_opp_put(old_opp);
112381c4d8a3SViresh Kumar 
112481c4d8a3SViresh Kumar 	/* Make sure current_opp doesn't get freed */
112581c4d8a3SViresh Kumar 	dev_pm_opp_get(opp);
112681c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
1127fe2af402SGeorgi Djakov 
1128386ba854SViresh Kumar 	return ret;
1129386ba854SViresh Kumar }
1130386ba854SViresh Kumar 
1131386ba854SViresh Kumar /**
1132386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1133386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1134386ba854SViresh Kumar  * @target_freq: frequency to achieve
1135386ba854SViresh Kumar  *
1136386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1137386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1138386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1139386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1140386ba854SViresh Kumar  * frequency.
1141386ba854SViresh Kumar  */
1142386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1143386ba854SViresh Kumar {
1144386ba854SViresh Kumar 	struct opp_table *opp_table;
1145386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1146386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
11471efae8d2SViresh Kumar 	bool forced = false;
1148386ba854SViresh Kumar 	int ret;
1149386ba854SViresh Kumar 
1150386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1151386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1152386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1153386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1154386ba854SViresh Kumar 	}
1155386ba854SViresh Kumar 
1156386ba854SViresh Kumar 	if (target_freq) {
1157386ba854SViresh Kumar 		/*
1158386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1159386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1160386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1161386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1162386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1163386ba854SViresh Kumar 		 */
1164386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
11652083da24SViresh Kumar 			ret = opp_table->config_clks(dev, opp_table, NULL,
11662083da24SViresh Kumar 						     &target_freq, false);
1167386ba854SViresh Kumar 			goto put_opp_table;
1168386ba854SViresh Kumar 		}
1169386ba854SViresh Kumar 
1170386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1171386ba854SViresh Kumar 		if ((long)freq <= 0)
1172386ba854SViresh Kumar 			freq = target_freq;
1173386ba854SViresh Kumar 
1174386ba854SViresh Kumar 		/*
1175386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1176386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1177386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1178386ba854SViresh Kumar 		 */
1179386ba854SViresh Kumar 		temp_freq = freq;
1180386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1181386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1182386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1183386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1184386ba854SViresh Kumar 				__func__, freq, ret);
1185386ba854SViresh Kumar 			goto put_opp_table;
1186386ba854SViresh Kumar 		}
11871efae8d2SViresh Kumar 
11881efae8d2SViresh Kumar 		/*
11891efae8d2SViresh Kumar 		 * An OPP entry specifies the highest frequency at which other
11901efae8d2SViresh Kumar 		 * properties of the OPP entry apply. Even if the new OPP is
11911efae8d2SViresh Kumar 		 * same as the old one, we may still reach here for a different
11921efae8d2SViresh Kumar 		 * value of the frequency. In such a case, do not abort but
11931efae8d2SViresh Kumar 		 * configure the hardware to the desired frequency forcefully.
11941efae8d2SViresh Kumar 		 */
11951efae8d2SViresh Kumar 		forced = opp_table->rate_clk_single != target_freq;
1196386ba854SViresh Kumar 	}
1197386ba854SViresh Kumar 
11981efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, &target_freq, forced);
1199386ba854SViresh Kumar 
1200386ba854SViresh Kumar 	if (target_freq)
12017813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
12021efae8d2SViresh Kumar 
12037813dd6fSViresh Kumar put_opp_table:
12047813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
12057813dd6fSViresh Kumar 	return ret;
12067813dd6fSViresh Kumar }
12077813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
12087813dd6fSViresh Kumar 
1209abbe3483SViresh Kumar /**
1210abbe3483SViresh Kumar  * dev_pm_opp_set_opp() - Configure device for OPP
1211abbe3483SViresh Kumar  * @dev: device for which we do this operation
1212abbe3483SViresh Kumar  * @opp: OPP to set to
1213abbe3483SViresh Kumar  *
1214abbe3483SViresh Kumar  * This configures the device based on the properties of the OPP passed to this
1215abbe3483SViresh Kumar  * routine.
1216abbe3483SViresh Kumar  *
1217abbe3483SViresh Kumar  * Return: 0 on success, a negative error number otherwise.
1218abbe3483SViresh Kumar  */
1219abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1220abbe3483SViresh Kumar {
1221abbe3483SViresh Kumar 	struct opp_table *opp_table;
1222abbe3483SViresh Kumar 	int ret;
1223abbe3483SViresh Kumar 
1224abbe3483SViresh Kumar 	opp_table = _find_opp_table(dev);
1225abbe3483SViresh Kumar 	if (IS_ERR(opp_table)) {
1226abbe3483SViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1227abbe3483SViresh Kumar 		return PTR_ERR(opp_table);
1228abbe3483SViresh Kumar 	}
1229abbe3483SViresh Kumar 
12301efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, NULL, false);
1231abbe3483SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1232abbe3483SViresh Kumar 
1233abbe3483SViresh Kumar 	return ret;
1234abbe3483SViresh Kumar }
1235abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1236abbe3483SViresh Kumar 
12377813dd6fSViresh Kumar /* OPP-dev Helpers */
12387813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
12397813dd6fSViresh Kumar 			    struct opp_table *opp_table)
12407813dd6fSViresh Kumar {
12417813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
12427813dd6fSViresh Kumar 	list_del(&opp_dev->node);
12437813dd6fSViresh Kumar 	kfree(opp_dev);
12447813dd6fSViresh Kumar }
12457813dd6fSViresh Kumar 
1246ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
12477813dd6fSViresh Kumar 				struct opp_table *opp_table)
12487813dd6fSViresh Kumar {
12497813dd6fSViresh Kumar 	struct opp_device *opp_dev;
12507813dd6fSViresh Kumar 
12517813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
12527813dd6fSViresh Kumar 	if (!opp_dev)
12537813dd6fSViresh Kumar 		return NULL;
12547813dd6fSViresh Kumar 
12557813dd6fSViresh Kumar 	/* Initialize opp-dev */
12567813dd6fSViresh Kumar 	opp_dev->dev = dev;
12573d255699SViresh Kumar 
1258ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
12597813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1260ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
12617813dd6fSViresh Kumar 
12627813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1263a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1264283d55e6SViresh Kumar 
1265283d55e6SViresh Kumar 	return opp_dev;
1266283d55e6SViresh Kumar }
1267283d55e6SViresh Kumar 
1268eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
12697813dd6fSViresh Kumar {
12707813dd6fSViresh Kumar 	struct opp_table *opp_table;
12717813dd6fSViresh Kumar 	struct opp_device *opp_dev;
12727813dd6fSViresh Kumar 	int ret;
12737813dd6fSViresh Kumar 
12747813dd6fSViresh Kumar 	/*
12757813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
12767813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
12777813dd6fSViresh Kumar 	 */
12787813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
12797813dd6fSViresh Kumar 	if (!opp_table)
1280dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
12817813dd6fSViresh Kumar 
12823d255699SViresh Kumar 	mutex_init(&opp_table->lock);
12834f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
12847813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
12857eba0c76SViresh Kumar 	INIT_LIST_HEAD(&opp_table->lazy);
12867813dd6fSViresh Kumar 
12872083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
12882083da24SViresh Kumar 
128946f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
129046f48acaSViresh Kumar 	opp_table->regulator_count = -1;
129146f48acaSViresh Kumar 
12927813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
12937813dd6fSViresh Kumar 	if (!opp_dev) {
1294dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1295dd461cd9SStephan Gerhold 		goto err;
12967813dd6fSViresh Kumar 	}
12977813dd6fSViresh Kumar 
1298eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
12997813dd6fSViresh Kumar 
13006d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
13016d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1302dd461cd9SStephan Gerhold 	if (ret) {
1303dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
130432439ac7SViresh Kumar 			goto remove_opp_dev;
1305dd461cd9SStephan Gerhold 
13066d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
13076d3f922cSGeorgi Djakov 			 __func__, ret);
1308dd461cd9SStephan Gerhold 	}
13096d3f922cSGeorgi Djakov 
13107813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
13117813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
13127813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
13137813dd6fSViresh Kumar 
13147813dd6fSViresh Kumar 	return opp_table;
1315dd461cd9SStephan Gerhold 
1316976509bbSQuanyang Wang remove_opp_dev:
1317976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1318dd461cd9SStephan Gerhold err:
1319dd461cd9SStephan Gerhold 	kfree(opp_table);
1320dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
13217813dd6fSViresh Kumar }
13227813dd6fSViresh Kumar 
13237813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
13247813dd6fSViresh Kumar {
13257813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
13267813dd6fSViresh Kumar }
13277813dd6fSViresh Kumar 
132832439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
132932439ac7SViresh Kumar 					       struct opp_table *opp_table,
133032439ac7SViresh Kumar 					       bool getclk)
133132439ac7SViresh Kumar {
1332d4a4c7a4SViresh Kumar 	int ret;
1333d4a4c7a4SViresh Kumar 
133432439ac7SViresh Kumar 	/*
13352083da24SViresh Kumar 	 * Return early if we don't need to get clk or we have already done it
133632439ac7SViresh Kumar 	 * earlier.
133732439ac7SViresh Kumar 	 */
13382083da24SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
13392083da24SViresh Kumar 	    opp_table->clks)
134032439ac7SViresh Kumar 		return opp_table;
134132439ac7SViresh Kumar 
134232439ac7SViresh Kumar 	/* Find clk for the device */
134332439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
134432439ac7SViresh Kumar 
1345d4a4c7a4SViresh Kumar 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
13462083da24SViresh Kumar 	if (!ret) {
13472083da24SViresh Kumar 		opp_table->config_clks = _opp_config_clk_single;
13482083da24SViresh Kumar 		opp_table->clk_count = 1;
134932439ac7SViresh Kumar 		return opp_table;
13502083da24SViresh Kumar 	}
1351d4a4c7a4SViresh Kumar 
1352d4a4c7a4SViresh Kumar 	if (ret == -ENOENT) {
13532083da24SViresh Kumar 		/*
13542083da24SViresh Kumar 		 * There are few platforms which don't want the OPP core to
13552083da24SViresh Kumar 		 * manage device's clock settings. In such cases neither the
13562083da24SViresh Kumar 		 * platform provides the clks explicitly to us, nor the DT
13572083da24SViresh Kumar 		 * contains a valid clk entry. The OPP nodes in DT may still
13582083da24SViresh Kumar 		 * contain "opp-hz" property though, which we need to parse and
13592083da24SViresh Kumar 		 * allow the platform to find an OPP based on freq later on.
13602083da24SViresh Kumar 		 *
13612083da24SViresh Kumar 		 * This is a simple solution to take care of such corner cases,
13622083da24SViresh Kumar 		 * i.e. make the clk_count 1, which lets us allocate space for
13632083da24SViresh Kumar 		 * frequency in opp->rates and also parse the entries in DT.
13642083da24SViresh Kumar 		 */
13652083da24SViresh Kumar 		opp_table->clk_count = 1;
13662083da24SViresh Kumar 
1367d4a4c7a4SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1368d4a4c7a4SViresh Kumar 		return opp_table;
1369d4a4c7a4SViresh Kumar 	}
1370d4a4c7a4SViresh Kumar 
1371d4a4c7a4SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1372d4a4c7a4SViresh Kumar 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1373d4a4c7a4SViresh Kumar 
1374d4a4c7a4SViresh Kumar 	return ERR_PTR(ret);
137532439ac7SViresh Kumar }
137632439ac7SViresh Kumar 
137727c09484SViresh Kumar /*
137827c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
137927c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
138027c09484SViresh Kumar  *
138127c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
138227c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
138327c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
138427c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
138527c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
138627c09484SViresh Kumar  * indirect users of OPP core.
138727c09484SViresh Kumar  *
138827c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
138927c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
139027c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
139127c09484SViresh Kumar  */
139232439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
139332439ac7SViresh Kumar 					 bool getclk)
13947813dd6fSViresh Kumar {
13957813dd6fSViresh Kumar 	struct opp_table *opp_table;
13967813dd6fSViresh Kumar 
139727c09484SViresh Kumar again:
13987813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
13997813dd6fSViresh Kumar 
14007813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
14017813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
14027813dd6fSViresh Kumar 		goto unlock;
14037813dd6fSViresh Kumar 
140427c09484SViresh Kumar 	/*
140527c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
140627c09484SViresh Kumar 	 * another user, wait for it to finish.
140727c09484SViresh Kumar 	 */
140827c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
140927c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
141027c09484SViresh Kumar 		cpu_relax();
141127c09484SViresh Kumar 		goto again;
141227c09484SViresh Kumar 	}
141327c09484SViresh Kumar 
141427c09484SViresh Kumar 	opp_tables_busy = true;
1415283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
141627c09484SViresh Kumar 
141727c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
141827c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
141927c09484SViresh Kumar 
1420283d55e6SViresh Kumar 	if (opp_table) {
1421ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1422283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1423dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1424283d55e6SViresh Kumar 		}
142527c09484SViresh Kumar 
142627c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
142727c09484SViresh Kumar 	} else {
142827c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
142927c09484SViresh Kumar 
143027c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
143127c09484SViresh Kumar 		if (!IS_ERR(opp_table))
143227c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1433283d55e6SViresh Kumar 	}
1434283d55e6SViresh Kumar 
143527c09484SViresh Kumar 	opp_tables_busy = false;
14367813dd6fSViresh Kumar 
14377813dd6fSViresh Kumar unlock:
14387813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
14397813dd6fSViresh Kumar 
144032439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
14417813dd6fSViresh Kumar }
1442eb7c8743SViresh Kumar 
144332439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1444e77dcb0bSViresh Kumar {
144532439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1446e77dcb0bSViresh Kumar }
1447e77dcb0bSViresh Kumar 
1448eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1449eb7c8743SViresh Kumar {
1450e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1451eb7c8743SViresh Kumar }
14527813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
14537813dd6fSViresh Kumar 
14547813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
14557813dd6fSViresh Kumar {
14567813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1457cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
14586d3f922cSGeorgi Djakov 	int i;
14597813dd6fSViresh Kumar 
1460e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1461e0df59deSViresh Kumar 	list_del(&opp_table->node);
1462e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1463e0df59deSViresh Kumar 
146481c4d8a3SViresh Kumar 	if (opp_table->current_opp)
146581c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
146681c4d8a3SViresh Kumar 
14675d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
14685d6d106fSViresh Kumar 
14692083da24SViresh Kumar 	/* Release automatically acquired single clk */
14707813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
14717813dd6fSViresh Kumar 		clk_put(opp_table->clk);
14727813dd6fSViresh Kumar 
14736d3f922cSGeorgi Djakov 	if (opp_table->paths) {
14746d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
14756d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
14766d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
14776d3f922cSGeorgi Djakov 	}
14786d3f922cSGeorgi Djakov 
1479cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1480cdd6ed90SViresh Kumar 
1481cdd6ed90SViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
14823d255699SViresh Kumar 		/*
1483cdd6ed90SViresh Kumar 		 * The OPP table is getting removed, drop the performance state
1484cdd6ed90SViresh Kumar 		 * constraints.
14853d255699SViresh Kumar 		 */
1486cdd6ed90SViresh Kumar 		if (opp_table->genpd_performance_state)
1487cdd6ed90SViresh Kumar 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
14887813dd6fSViresh Kumar 
14897813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
1490cdd6ed90SViresh Kumar 	}
14917813dd6fSViresh Kumar 
14924f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
14937813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
14947813dd6fSViresh Kumar 	kfree(opp_table);
14957813dd6fSViresh Kumar }
14967813dd6fSViresh Kumar 
14977813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
14987813dd6fSViresh Kumar {
14997813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
15007813dd6fSViresh Kumar 		       &opp_table_lock);
15017813dd6fSViresh Kumar }
15027813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
15037813dd6fSViresh Kumar 
15047813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
15057813dd6fSViresh Kumar {
15067813dd6fSViresh Kumar 	kfree(opp);
15077813dd6fSViresh Kumar }
15087813dd6fSViresh Kumar 
1509cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
15107813dd6fSViresh Kumar {
1511cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1512cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1513cf1fac94SViresh Kumar 
1514cf1fac94SViresh Kumar 	list_del(&opp->node);
1515cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1516cf1fac94SViresh Kumar 
15177813dd6fSViresh Kumar 	/*
15187813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
15197813dd6fSViresh Kumar 	 * frequency/voltage list.
15207813dd6fSViresh Kumar 	 */
15217813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1522da544b61SViresh Kumar 	_of_opp_free_required_opps(opp_table, opp);
15237813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
15247813dd6fSViresh Kumar 	kfree(opp);
15251690d8bbSViresh Kumar }
15267813dd6fSViresh Kumar 
1527a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
15287813dd6fSViresh Kumar {
15297813dd6fSViresh Kumar 	kref_get(&opp->kref);
15307813dd6fSViresh Kumar }
15317813dd6fSViresh Kumar 
15327813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
15337813dd6fSViresh Kumar {
1534cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
15357813dd6fSViresh Kumar }
15367813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
15377813dd6fSViresh Kumar 
15387813dd6fSViresh Kumar /**
15397813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
15407813dd6fSViresh Kumar  * @dev:	device for which we do this operation
15417813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
15427813dd6fSViresh Kumar  *
15437813dd6fSViresh Kumar  * This function removes an opp from the opp table.
15447813dd6fSViresh Kumar  */
15457813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
15467813dd6fSViresh Kumar {
154795073b72SJakob Koschel 	struct dev_pm_opp *opp = NULL, *iter;
15487813dd6fSViresh Kumar 	struct opp_table *opp_table;
15497813dd6fSViresh Kumar 
15507813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
15517813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
15527813dd6fSViresh Kumar 		return;
15537813dd6fSViresh Kumar 
1554*f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table))
1555*f123ea74SViresh Kumar 		goto put_table;
1556*f123ea74SViresh Kumar 
15577813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
15587813dd6fSViresh Kumar 
155995073b72SJakob Koschel 	list_for_each_entry(iter, &opp_table->opp_list, node) {
15602083da24SViresh Kumar 		if (iter->rates[0] == freq) {
156195073b72SJakob Koschel 			opp = iter;
15627813dd6fSViresh Kumar 			break;
15637813dd6fSViresh Kumar 		}
15647813dd6fSViresh Kumar 	}
15657813dd6fSViresh Kumar 
15667813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
15677813dd6fSViresh Kumar 
156895073b72SJakob Koschel 	if (opp) {
15697813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
15700ad8c623SViresh Kumar 
15710ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
15720ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
15737813dd6fSViresh Kumar 	} else {
15747813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
15757813dd6fSViresh Kumar 			 __func__, freq);
15767813dd6fSViresh Kumar 	}
15777813dd6fSViresh Kumar 
1578*f123ea74SViresh Kumar put_table:
15790ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
15807813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
15817813dd6fSViresh Kumar }
15827813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
15837813dd6fSViresh Kumar 
1584cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1585cf1fac94SViresh Kumar 					bool dynamic)
1586cf1fac94SViresh Kumar {
1587cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1588cf1fac94SViresh Kumar 
1589cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1590cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1591606a5d42SBeata Michalska 		/*
1592606a5d42SBeata Michalska 		 * Refcount must be dropped only once for each OPP by OPP core,
1593606a5d42SBeata Michalska 		 * do that with help of "removed" flag.
1594606a5d42SBeata Michalska 		 */
1595606a5d42SBeata Michalska 		if (!temp->removed && dynamic == temp->dynamic) {
1596cf1fac94SViresh Kumar 			opp = temp;
1597cf1fac94SViresh Kumar 			break;
1598cf1fac94SViresh Kumar 		}
1599cf1fac94SViresh Kumar 	}
1600cf1fac94SViresh Kumar 
1601cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1602cf1fac94SViresh Kumar 	return opp;
1603cf1fac94SViresh Kumar }
1604cf1fac94SViresh Kumar 
1605606a5d42SBeata Michalska /*
1606606a5d42SBeata Michalska  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1607606a5d42SBeata Michalska  * happen lock less to avoid circular dependency issues. This routine must be
1608606a5d42SBeata Michalska  * called without the opp_table->lock held.
1609606a5d42SBeata Michalska  */
1610606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
161103758d60SViresh Kumar {
1612cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
161303758d60SViresh Kumar 
1614606a5d42SBeata Michalska 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1615606a5d42SBeata Michalska 		opp->removed = true;
1616606a5d42SBeata Michalska 		dev_pm_opp_put(opp);
1617606a5d42SBeata Michalska 
1618606a5d42SBeata Michalska 		/* Drop the references taken by dev_pm_opp_add() */
1619606a5d42SBeata Michalska 		if (dynamic)
1620606a5d42SBeata Michalska 			dev_pm_opp_put_opp_table(opp_table);
1621606a5d42SBeata Michalska 	}
1622606a5d42SBeata Michalska }
1623606a5d42SBeata Michalska 
1624606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table)
1625606a5d42SBeata Michalska {
162603758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
162703758d60SViresh Kumar 
1628922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1629cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1630cf1fac94SViresh Kumar 		return false;
1631922ff075SViresh Kumar 	}
1632922ff075SViresh Kumar 
1633cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1634cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1635cf1fac94SViresh Kumar 		return true;
163603758d60SViresh Kumar 	}
163703758d60SViresh Kumar 
163803758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1639922ff075SViresh Kumar 
1640606a5d42SBeata Michalska 	_opp_remove_all(opp_table, false);
1641cf1fac94SViresh Kumar 	return true;
164203758d60SViresh Kumar }
164303758d60SViresh Kumar 
16441690d8bbSViresh Kumar /**
16451690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
16461690d8bbSViresh Kumar  * @dev:	device for which we do this operation
16471690d8bbSViresh Kumar  *
16481690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
16491690d8bbSViresh Kumar  */
16501690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
16511690d8bbSViresh Kumar {
16521690d8bbSViresh Kumar 	struct opp_table *opp_table;
16531690d8bbSViresh Kumar 
16541690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
16551690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
16561690d8bbSViresh Kumar 		return;
16571690d8bbSViresh Kumar 
1658606a5d42SBeata Michalska 	_opp_remove_all(opp_table, true);
16591690d8bbSViresh Kumar 
16601690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
16611690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
16621690d8bbSViresh Kumar }
16631690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
16641690d8bbSViresh Kumar 
1665d6134583SViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table)
16667813dd6fSViresh Kumar {
16677813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
16682083da24SViresh Kumar 	int supply_count, supply_size, icc_size, clk_size;
16697813dd6fSViresh Kumar 
16707813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
1671d6134583SViresh Kumar 	supply_count = opp_table->regulator_count > 0 ?
1672d6134583SViresh Kumar 			opp_table->regulator_count : 1;
16736d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
16742083da24SViresh Kumar 	clk_size = sizeof(*opp->rates) * opp_table->clk_count;
1675d6134583SViresh Kumar 	icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
16767813dd6fSViresh Kumar 
16777813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
16782083da24SViresh Kumar 	opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL);
16797813dd6fSViresh Kumar 	if (!opp)
16807813dd6fSViresh Kumar 		return NULL;
16817813dd6fSViresh Kumar 
16822083da24SViresh Kumar 	/* Put the supplies, bw and clock at the end of the OPP structure */
16837813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
16842083da24SViresh Kumar 
16852083da24SViresh Kumar 	opp->rates = (unsigned long *)(opp->supplies + supply_count);
16862083da24SViresh Kumar 
16876d3f922cSGeorgi Djakov 	if (icc_size)
16882083da24SViresh Kumar 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
16892083da24SViresh Kumar 
16907813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
16917813dd6fSViresh Kumar 
16927813dd6fSViresh Kumar 	return opp;
16937813dd6fSViresh Kumar }
16947813dd6fSViresh Kumar 
16957813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
16967813dd6fSViresh Kumar 					 struct opp_table *opp_table)
16977813dd6fSViresh Kumar {
16987813dd6fSViresh Kumar 	struct regulator *reg;
16997813dd6fSViresh Kumar 	int i;
17007813dd6fSViresh Kumar 
170190e3577bSViresh Kumar 	if (!opp_table->regulators)
170290e3577bSViresh Kumar 		return true;
170390e3577bSViresh Kumar 
17047813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
17057813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
17067813dd6fSViresh Kumar 
17077813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
17087813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
17097813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
17107813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
17117813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
17127813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
17137813dd6fSViresh Kumar 			return false;
17147813dd6fSViresh Kumar 		}
17157813dd6fSViresh Kumar 	}
17167813dd6fSViresh Kumar 
17177813dd6fSViresh Kumar 	return true;
17187813dd6fSViresh Kumar }
17197813dd6fSViresh Kumar 
17202083da24SViresh Kumar static int _opp_compare_rate(struct opp_table *opp_table,
17212083da24SViresh Kumar 			     struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
17222083da24SViresh Kumar {
17232083da24SViresh Kumar 	int i;
17242083da24SViresh Kumar 
17252083da24SViresh Kumar 	for (i = 0; i < opp_table->clk_count; i++) {
17262083da24SViresh Kumar 		if (opp1->rates[i] != opp2->rates[i])
17272083da24SViresh Kumar 			return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
17282083da24SViresh Kumar 	}
17292083da24SViresh Kumar 
17302083da24SViresh Kumar 	/* Same rates for both OPPs */
17312083da24SViresh Kumar 	return 0;
17322083da24SViresh Kumar }
17332083da24SViresh Kumar 
1734274c3e83SViresh Kumar static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1735274c3e83SViresh Kumar 			   struct dev_pm_opp *opp2)
1736274c3e83SViresh Kumar {
1737274c3e83SViresh Kumar 	int i;
1738274c3e83SViresh Kumar 
1739274c3e83SViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
1740274c3e83SViresh Kumar 		if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
1741274c3e83SViresh Kumar 			return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
1742274c3e83SViresh Kumar 	}
1743274c3e83SViresh Kumar 
1744274c3e83SViresh Kumar 	/* Same bw for both OPPs */
1745274c3e83SViresh Kumar 	return 0;
1746274c3e83SViresh Kumar }
1747274c3e83SViresh Kumar 
17488bdac14bSViresh Kumar /*
17498bdac14bSViresh Kumar  * Returns
17508bdac14bSViresh Kumar  * 0: opp1 == opp2
17518bdac14bSViresh Kumar  * 1: opp1 > opp2
17528bdac14bSViresh Kumar  * -1: opp1 < opp2
17538bdac14bSViresh Kumar  */
17542083da24SViresh Kumar int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1,
17552083da24SViresh Kumar 		     struct dev_pm_opp *opp2)
17566c591eecSSaravana Kannan {
17572083da24SViresh Kumar 	int ret;
17582083da24SViresh Kumar 
17592083da24SViresh Kumar 	ret = _opp_compare_rate(opp_table, opp1, opp2);
17602083da24SViresh Kumar 	if (ret)
17612083da24SViresh Kumar 		return ret;
17622083da24SViresh Kumar 
1763274c3e83SViresh Kumar 	ret = _opp_compare_bw(opp_table, opp1, opp2);
1764274c3e83SViresh Kumar 	if (ret)
1765274c3e83SViresh Kumar 		return ret;
17662083da24SViresh Kumar 
17676c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
17686c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
17692083da24SViresh Kumar 
17702083da24SViresh Kumar 	/* Duplicate OPPs */
17716c591eecSSaravana Kannan 	return 0;
17726c591eecSSaravana Kannan }
17736c591eecSSaravana Kannan 
1774a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1775a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1776a1e8c136SViresh Kumar 			     struct list_head **head)
1777a1e8c136SViresh Kumar {
1778a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
17796c591eecSSaravana Kannan 	int opp_cmp;
1780a1e8c136SViresh Kumar 
1781a1e8c136SViresh Kumar 	/*
1782a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1783a1e8c136SViresh Kumar 	 * already present.
1784a1e8c136SViresh Kumar 	 *
1785a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1786a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1787a1e8c136SViresh Kumar 	 * loop.
1788a1e8c136SViresh Kumar 	 */
1789a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
17902083da24SViresh Kumar 		opp_cmp = _opp_compare_key(opp_table, new_opp, opp);
17916c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1792a1e8c136SViresh Kumar 			*head = &opp->node;
1793a1e8c136SViresh Kumar 			continue;
1794a1e8c136SViresh Kumar 		}
1795a1e8c136SViresh Kumar 
17966c591eecSSaravana Kannan 		if (opp_cmp < 0)
1797a1e8c136SViresh Kumar 			return 0;
1798a1e8c136SViresh Kumar 
1799a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1800a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
18012083da24SViresh Kumar 			 __func__, opp->rates[0], opp->supplies[0].u_volt,
18022083da24SViresh Kumar 			 opp->available, new_opp->rates[0],
1803a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1804a1e8c136SViresh Kumar 
1805a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1806a1e8c136SViresh Kumar 		return opp->available &&
1807a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1808a1e8c136SViresh Kumar 	}
1809a1e8c136SViresh Kumar 
1810a1e8c136SViresh Kumar 	return 0;
1811a1e8c136SViresh Kumar }
1812a1e8c136SViresh Kumar 
18137eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count)
18147eba0c76SViresh Kumar {
18157eba0c76SViresh Kumar 	int i;
18167eba0c76SViresh Kumar 
18177eba0c76SViresh Kumar 	for (i = 0; i < count; i++) {
18187eba0c76SViresh Kumar 		if (opp->required_opps[i]->available)
18197eba0c76SViresh Kumar 			continue;
18207eba0c76SViresh Kumar 
18217eba0c76SViresh Kumar 		opp->available = false;
18227eba0c76SViresh Kumar 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
18232083da24SViresh Kumar 			 __func__, opp->required_opps[i]->np, opp->rates[0]);
18247eba0c76SViresh Kumar 		return;
18257eba0c76SViresh Kumar 	}
18267eba0c76SViresh Kumar }
18277eba0c76SViresh Kumar 
18287813dd6fSViresh Kumar /*
18297813dd6fSViresh Kumar  * Returns:
18307813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
18317813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
18327813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
18337813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
18347813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
18357813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
18367813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
18377813dd6fSViresh Kumar  */
18387813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
18394768914bSViresh Kumar 	     struct opp_table *opp_table)
18407813dd6fSViresh Kumar {
18417813dd6fSViresh Kumar 	struct list_head *head;
18427813dd6fSViresh Kumar 	int ret;
18437813dd6fSViresh Kumar 
18447813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
18457813dd6fSViresh Kumar 	head = &opp_table->opp_list;
18467813dd6fSViresh Kumar 
1847a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1848a1e8c136SViresh Kumar 	if (ret) {
18497813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
18507813dd6fSViresh Kumar 		return ret;
18517813dd6fSViresh Kumar 	}
18527813dd6fSViresh Kumar 
18537813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
18547813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
18557813dd6fSViresh Kumar 
18567813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
18577813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
18587813dd6fSViresh Kumar 
1859a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
18607813dd6fSViresh Kumar 
18617813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
18627813dd6fSViresh Kumar 		new_opp->available = false;
18637813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
18642083da24SViresh Kumar 			 __func__, new_opp->rates[0]);
18657813dd6fSViresh Kumar 	}
18667813dd6fSViresh Kumar 
18677eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
18687eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
18697eba0c76SViresh Kumar 		return 0;
1870cf65948dSDmitry Osipenko 
18717eba0c76SViresh Kumar 	_required_opps_available(new_opp, opp_table->required_opp_count);
1872cf65948dSDmitry Osipenko 
18737813dd6fSViresh Kumar 	return 0;
18747813dd6fSViresh Kumar }
18757813dd6fSViresh Kumar 
18767813dd6fSViresh Kumar /**
18777813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
18787813dd6fSViresh Kumar  * @opp_table:	OPP table
18797813dd6fSViresh Kumar  * @dev:	device for which we do this operation
18807813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
18817813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
18827813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
18837813dd6fSViresh Kumar  *
18847813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
18857813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
18867813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
18877813dd6fSViresh Kumar  *
18887813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
18897813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
18907813dd6fSViresh Kumar  *
18917813dd6fSViresh Kumar  * Return:
18927813dd6fSViresh Kumar  * 0		On success OR
18937813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
18947813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
18957813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
18967813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
18977813dd6fSViresh Kumar  */
18987813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
18997813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
19007813dd6fSViresh Kumar {
19017813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
19027813dd6fSViresh Kumar 	unsigned long tol;
19037813dd6fSViresh Kumar 	int ret;
19047813dd6fSViresh Kumar 
1905*f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table))
1906*f123ea74SViresh Kumar 		return -EINVAL;
1907*f123ea74SViresh Kumar 
19087813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
19097813dd6fSViresh Kumar 	if (!new_opp)
19107813dd6fSViresh Kumar 		return -ENOMEM;
19117813dd6fSViresh Kumar 
19127813dd6fSViresh Kumar 	/* populate the opp table */
19132083da24SViresh Kumar 	new_opp->rates[0] = freq;
19147813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
19157813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
19167813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
19177813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
19187813dd6fSViresh Kumar 	new_opp->available = true;
19197813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
19207813dd6fSViresh Kumar 
19214768914bSViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table);
19227813dd6fSViresh Kumar 	if (ret) {
19237813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
19247813dd6fSViresh Kumar 		if (ret == -EBUSY)
19257813dd6fSViresh Kumar 			ret = 0;
19267813dd6fSViresh Kumar 		goto free_opp;
19277813dd6fSViresh Kumar 	}
19287813dd6fSViresh Kumar 
19297813dd6fSViresh Kumar 	/*
19307813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
19317813dd6fSViresh Kumar 	 * frequency/voltage list.
19327813dd6fSViresh Kumar 	 */
19337813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
19347813dd6fSViresh Kumar 	return 0;
19357813dd6fSViresh Kumar 
19367813dd6fSViresh Kumar free_opp:
19377813dd6fSViresh Kumar 	_opp_free(new_opp);
19387813dd6fSViresh Kumar 
19397813dd6fSViresh Kumar 	return ret;
19407813dd6fSViresh Kumar }
19417813dd6fSViresh Kumar 
19427813dd6fSViresh Kumar /**
194389f03984SViresh Kumar  * _opp_set_supported_hw() - Set supported platforms
19447813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
19457813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
19467813dd6fSViresh Kumar  * @count: Number of elements in the array.
19477813dd6fSViresh Kumar  *
19487813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
19497813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
19507813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
19517813dd6fSViresh Kumar  * property.
19527813dd6fSViresh Kumar  */
195389f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table,
19547813dd6fSViresh Kumar 				 const u32 *versions, unsigned int count)
19557813dd6fSViresh Kumar {
195625419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
195725419de1SViresh Kumar 	if (opp_table->supported_hw)
195889f03984SViresh Kumar 		return 0;
19597813dd6fSViresh Kumar 
19607813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
19617813dd6fSViresh Kumar 					GFP_KERNEL);
196289f03984SViresh Kumar 	if (!opp_table->supported_hw)
196389f03984SViresh Kumar 		return -ENOMEM;
19647813dd6fSViresh Kumar 
19657813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
19667813dd6fSViresh Kumar 
196789f03984SViresh Kumar 	return 0;
19687813dd6fSViresh Kumar }
19697813dd6fSViresh Kumar 
19707813dd6fSViresh Kumar /**
197189f03984SViresh Kumar  * _opp_put_supported_hw() - Releases resources blocked for supported hw
197289f03984SViresh Kumar  * @opp_table: OPP table returned by _opp_set_supported_hw().
19737813dd6fSViresh Kumar  *
19747813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
197589f03984SViresh Kumar  * _opp_set_supported_hw(). Until this is called, the opp_table structure
19767813dd6fSViresh Kumar  * will not be freed.
19777813dd6fSViresh Kumar  */
197889f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table)
19797813dd6fSViresh Kumar {
198089f03984SViresh Kumar 	if (opp_table->supported_hw) {
19817813dd6fSViresh Kumar 		kfree(opp_table->supported_hw);
19827813dd6fSViresh Kumar 		opp_table->supported_hw = NULL;
19837813dd6fSViresh Kumar 		opp_table->supported_hw_count = 0;
19847813dd6fSViresh Kumar 	}
19859c4f220fSYangtao Li }
19869c4f220fSYangtao Li 
19879c4f220fSYangtao Li /**
1988298098e5SViresh Kumar  * _opp_set_prop_name() - Set prop-extn name
19897813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
19907813dd6fSViresh Kumar  * @name: name to postfix to properties.
19917813dd6fSViresh Kumar  *
19927813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
19937813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
19947813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
19957813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
19967813dd6fSViresh Kumar  */
1997298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
19987813dd6fSViresh Kumar {
1999878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
20007813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
2001298098e5SViresh Kumar 		opp_table->prop_name = kstrdup(name, GFP_KERNEL);
2002298098e5SViresh Kumar 		if (!opp_table->prop_name)
2003298098e5SViresh Kumar 			return -ENOMEM;
20047813dd6fSViresh Kumar 	}
20057813dd6fSViresh Kumar 
2006298098e5SViresh Kumar 	return 0;
20077813dd6fSViresh Kumar }
20087813dd6fSViresh Kumar 
20097813dd6fSViresh Kumar /**
2010298098e5SViresh Kumar  * _opp_put_prop_name() - Releases resources blocked for prop-name
2011298098e5SViresh Kumar  * @opp_table: OPP table returned by _opp_set_prop_name().
20127813dd6fSViresh Kumar  *
20137813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
2014298098e5SViresh Kumar  * _opp_set_prop_name(). Until this is called, the opp_table structure
20157813dd6fSViresh Kumar  * will not be freed.
20167813dd6fSViresh Kumar  */
2017298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table)
20187813dd6fSViresh Kumar {
2019298098e5SViresh Kumar 	if (opp_table->prop_name) {
20207813dd6fSViresh Kumar 		kfree(opp_table->prop_name);
20217813dd6fSViresh Kumar 		opp_table->prop_name = NULL;
20227813dd6fSViresh Kumar 	}
2023298098e5SViresh Kumar }
20247813dd6fSViresh Kumar 
20257813dd6fSViresh Kumar /**
2026b0ec0942SViresh Kumar  * _opp_set_regulators() - Set regulator names for the device
20277813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
20287813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
20297813dd6fSViresh Kumar  * @count: Number of regulators.
20307813dd6fSViresh Kumar  *
20317813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
20327813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
20337813dd6fSViresh Kumar  * well.
20347813dd6fSViresh Kumar  *
20357813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
20367813dd6fSViresh Kumar  */
2037b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
203887686cc8SViresh Kumar 			       const char * const names[])
20397813dd6fSViresh Kumar {
204087686cc8SViresh Kumar 	const char * const *temp = names;
20417813dd6fSViresh Kumar 	struct regulator *reg;
204287686cc8SViresh Kumar 	int count = 0, ret, i;
204387686cc8SViresh Kumar 
204487686cc8SViresh Kumar 	/* Count number of regulators */
204587686cc8SViresh Kumar 	while (*temp++)
204687686cc8SViresh Kumar 		count++;
204787686cc8SViresh Kumar 
204887686cc8SViresh Kumar 	if (!count)
2049b0ec0942SViresh Kumar 		return -EINVAL;
20507813dd6fSViresh Kumar 
2051779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
2052779b783cSViresh Kumar 	if (opp_table->regulators)
2053b0ec0942SViresh Kumar 		return 0;
20547813dd6fSViresh Kumar 
20557813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
20567813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
20577813dd6fSViresh Kumar 					      GFP_KERNEL);
2058b0ec0942SViresh Kumar 	if (!opp_table->regulators)
2059b0ec0942SViresh Kumar 		return -ENOMEM;
20607813dd6fSViresh Kumar 
20617813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
20627813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
20637813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
2064543256d2SKrzysztof Kozlowski 			ret = dev_err_probe(dev, PTR_ERR(reg),
2065543256d2SKrzysztof Kozlowski 					    "%s: no regulator (%s) found\n",
2066543256d2SKrzysztof Kozlowski 					    __func__, names[i]);
20677813dd6fSViresh Kumar 			goto free_regulators;
20687813dd6fSViresh Kumar 		}
20697813dd6fSViresh Kumar 
20707813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
20717813dd6fSViresh Kumar 	}
20727813dd6fSViresh Kumar 
20737813dd6fSViresh Kumar 	opp_table->regulator_count = count;
20747813dd6fSViresh Kumar 
2075c522ce8aSViresh Kumar 	/* Set generic config_regulators() for single regulators here */
2076c522ce8aSViresh Kumar 	if (count == 1)
2077c522ce8aSViresh Kumar 		opp_table->config_regulators = _opp_config_regulator_single;
2078c522ce8aSViresh Kumar 
2079b0ec0942SViresh Kumar 	return 0;
20807813dd6fSViresh Kumar 
20817813dd6fSViresh Kumar free_regulators:
208224957db1SMarek Szyprowski 	while (i != 0)
208324957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
20847813dd6fSViresh Kumar 
20857813dd6fSViresh Kumar 	kfree(opp_table->regulators);
20867813dd6fSViresh Kumar 	opp_table->regulators = NULL;
208746f48acaSViresh Kumar 	opp_table->regulator_count = -1;
20887813dd6fSViresh Kumar 
2089b0ec0942SViresh Kumar 	return ret;
20907813dd6fSViresh Kumar }
20917813dd6fSViresh Kumar 
20927813dd6fSViresh Kumar /**
2093b0ec0942SViresh Kumar  * _opp_put_regulators() - Releases resources blocked for regulator
2094b0ec0942SViresh Kumar  * @opp_table: OPP table returned from _opp_set_regulators().
20957813dd6fSViresh Kumar  */
2096b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table)
20977813dd6fSViresh Kumar {
20987813dd6fSViresh Kumar 	int i;
20997813dd6fSViresh Kumar 
2100779b783cSViresh Kumar 	if (!opp_table->regulators)
2101b0ec0942SViresh Kumar 		return;
21027813dd6fSViresh Kumar 
210372f80ce4SViresh Kumar 	if (opp_table->enabled) {
21048d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
21058d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
21068d45719cSKamil Konieczny 	}
21078d45719cSKamil Konieczny 
210824957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
21097813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
21107813dd6fSViresh Kumar 
21117813dd6fSViresh Kumar 	kfree(opp_table->regulators);
21127813dd6fSViresh Kumar 	opp_table->regulators = NULL;
211346f48acaSViresh Kumar 	opp_table->regulator_count = -1;
21147813dd6fSViresh Kumar }
211532aee78bSYangtao Li 
21162083da24SViresh Kumar static void _put_clks(struct opp_table *opp_table, int count)
21172083da24SViresh Kumar {
21182083da24SViresh Kumar 	int i;
21192083da24SViresh Kumar 
21202083da24SViresh Kumar 	for (i = count - 1; i >= 0; i--)
21212083da24SViresh Kumar 		clk_put(opp_table->clks[i]);
21222083da24SViresh Kumar 
21232083da24SViresh Kumar 	kfree(opp_table->clks);
21242083da24SViresh Kumar 	opp_table->clks = NULL;
21252083da24SViresh Kumar }
21262083da24SViresh Kumar 
21277813dd6fSViresh Kumar /**
21282368f576SViresh Kumar  * _opp_set_clknames() - Set clk names for the device
21292368f576SViresh Kumar  * @dev: Device for which clk names is being set.
21302368f576SViresh Kumar  * @names: Clk names.
21317813dd6fSViresh Kumar  *
21322368f576SViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointers to the
21332368f576SViresh Kumar  * clocks for the device. Simple cases work fine without using this routine
21342368f576SViresh Kumar  * (i.e. by passing connection-id as NULL), but for a device with multiple
21352368f576SViresh Kumar  * clocks available, the OPP core needs to know the exact names of the clks to
21362368f576SViresh Kumar  * use.
21377813dd6fSViresh Kumar  *
21387813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
21397813dd6fSViresh Kumar  */
21402368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
21412083da24SViresh Kumar 			     const char * const names[],
21422083da24SViresh Kumar 			     config_clks_t config_clks)
21437813dd6fSViresh Kumar {
21442368f576SViresh Kumar 	const char * const *temp = names;
21452083da24SViresh Kumar 	int count = 0, ret, i;
21462083da24SViresh Kumar 	struct clk *clk;
21477813dd6fSViresh Kumar 
21482368f576SViresh Kumar 	/* Count number of clks */
21492368f576SViresh Kumar 	while (*temp++)
21502368f576SViresh Kumar 		count++;
21517813dd6fSViresh Kumar 
21522368f576SViresh Kumar 	/*
21532368f576SViresh Kumar 	 * This is a special case where we have a single clock, whose connection
21542368f576SViresh Kumar 	 * id name is NULL, i.e. first two entries are NULL in the array.
21552368f576SViresh Kumar 	 */
21562368f576SViresh Kumar 	if (!count && !names[1])
21572368f576SViresh Kumar 		count = 1;
21582368f576SViresh Kumar 
21592083da24SViresh Kumar 	/* Fail early for invalid configurations */
21602083da24SViresh Kumar 	if (!count || (config_clks && count == 1) || (!config_clks && count > 1))
21612368f576SViresh Kumar 		return -EINVAL;
21627813dd6fSViresh Kumar 
21630a43452bSViresh Kumar 	/* Another CPU that shares the OPP table has set the clkname ? */
21642083da24SViresh Kumar 	if (opp_table->clks)
21652368f576SViresh Kumar 		return 0;
21660a43452bSViresh Kumar 
21672083da24SViresh Kumar 	opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks),
21682083da24SViresh Kumar 					GFP_KERNEL);
21692083da24SViresh Kumar 	if (!opp_table->clks)
21702083da24SViresh Kumar 		return -ENOMEM;
21717813dd6fSViresh Kumar 
21722083da24SViresh Kumar 	/* Find clks for the device */
21732083da24SViresh Kumar 	for (i = 0; i < count; i++) {
21742083da24SViresh Kumar 		clk = clk_get(dev, names[i]);
21752083da24SViresh Kumar 		if (IS_ERR(clk)) {
21762083da24SViresh Kumar 			ret = dev_err_probe(dev, PTR_ERR(clk),
21772083da24SViresh Kumar 					    "%s: Couldn't find clock with name: %s\n",
21782083da24SViresh Kumar 					    __func__, names[i]);
21792083da24SViresh Kumar 			goto free_clks;
21807813dd6fSViresh Kumar 		}
21817813dd6fSViresh Kumar 
21822083da24SViresh Kumar 		opp_table->clks[i] = clk;
21832083da24SViresh Kumar 	}
21842083da24SViresh Kumar 
21852083da24SViresh Kumar 	opp_table->clk_count = count;
21862083da24SViresh Kumar 
21872083da24SViresh Kumar 	/* Set generic single clk set here */
21882083da24SViresh Kumar 	if (count == 1) {
21892083da24SViresh Kumar 		opp_table->config_clks = _opp_config_clk_single;
21902083da24SViresh Kumar 
21912083da24SViresh Kumar 		/*
21922083da24SViresh Kumar 		 * We could have just dropped the "clk" field and used "clks"
21932083da24SViresh Kumar 		 * everywhere. Instead we kept the "clk" field around for
21942083da24SViresh Kumar 		 * following reasons:
21952083da24SViresh Kumar 		 *
21962083da24SViresh Kumar 		 * - avoiding clks[0] everywhere else.
21972083da24SViresh Kumar 		 * - not running single clk helpers for multiple clk usecase by
21982083da24SViresh Kumar 		 *   mistake.
21992083da24SViresh Kumar 		 *
22002083da24SViresh Kumar 		 * Since this is single-clk case, just update the clk pointer
22012083da24SViresh Kumar 		 * too.
22022083da24SViresh Kumar 		 */
22032083da24SViresh Kumar 		opp_table->clk = opp_table->clks[0];
22042083da24SViresh Kumar 	} else {
22052083da24SViresh Kumar 		opp_table->config_clks = config_clks;
22062083da24SViresh Kumar 	}
22070a43452bSViresh Kumar 
22082368f576SViresh Kumar 	return 0;
22092083da24SViresh Kumar 
22102083da24SViresh Kumar free_clks:
22112083da24SViresh Kumar 	_put_clks(opp_table, i);
22122083da24SViresh Kumar 	return ret;
22137813dd6fSViresh Kumar }
22147813dd6fSViresh Kumar 
22157813dd6fSViresh Kumar /**
22162368f576SViresh Kumar  * _opp_put_clknames() - Releases resources blocked for clks.
22172368f576SViresh Kumar  * @opp_table: OPP table returned from _opp_set_clknames().
22187813dd6fSViresh Kumar  */
22192368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table)
22207813dd6fSViresh Kumar {
22212083da24SViresh Kumar 	if (!opp_table->clks)
22222083da24SViresh Kumar 		return;
22232083da24SViresh Kumar 
22242083da24SViresh Kumar 	opp_table->config_clks = NULL;
22252083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
22262083da24SViresh Kumar 
22272083da24SViresh Kumar 	_put_clks(opp_table, opp_table->clk_count);
2228a74f681cSYangtao Li }
2229a74f681cSYangtao Li 
2230a74f681cSYangtao Li /**
2231aee3352fSViresh Kumar  * _opp_set_config_regulators_helper() - Register custom set regulator helper.
2232aee3352fSViresh Kumar  * @dev: Device for which the helper is getting registered.
2233aee3352fSViresh Kumar  * @config_regulators: Custom set regulator helper.
2234aee3352fSViresh Kumar  *
2235aee3352fSViresh Kumar  * This is useful to support platforms with multiple regulators per device.
2236aee3352fSViresh Kumar  *
2237aee3352fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
2238aee3352fSViresh Kumar  */
2239aee3352fSViresh Kumar static int _opp_set_config_regulators_helper(struct opp_table *opp_table,
2240aee3352fSViresh Kumar 		struct device *dev, config_regulators_t config_regulators)
2241aee3352fSViresh Kumar {
2242aee3352fSViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
2243aee3352fSViresh Kumar 	if (!opp_table->config_regulators)
2244aee3352fSViresh Kumar 		opp_table->config_regulators = config_regulators;
2245aee3352fSViresh Kumar 
2246aee3352fSViresh Kumar 	return 0;
2247aee3352fSViresh Kumar }
2248aee3352fSViresh Kumar 
2249aee3352fSViresh Kumar /**
2250aee3352fSViresh Kumar  * _opp_put_config_regulators_helper() - Releases resources blocked for
2251aee3352fSViresh Kumar  *					 config_regulators helper.
2252aee3352fSViresh Kumar  * @opp_table: OPP table returned from _opp_set_config_regulators_helper().
2253aee3352fSViresh Kumar  *
2254aee3352fSViresh Kumar  * Release resources blocked for platform specific config_regulators helper.
2255aee3352fSViresh Kumar  */
2256aee3352fSViresh Kumar static void _opp_put_config_regulators_helper(struct opp_table *opp_table)
2257aee3352fSViresh Kumar {
2258aee3352fSViresh Kumar 	if (opp_table->config_regulators)
2259aee3352fSViresh Kumar 		opp_table->config_regulators = NULL;
2260aee3352fSViresh Kumar }
2261aee3352fSViresh Kumar 
2262442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table)
22636319aee1SViresh Kumar {
22646319aee1SViresh Kumar 	int index;
22656319aee1SViresh Kumar 
2266cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2267cb60e960SViresh Kumar 		return;
2268cb60e960SViresh Kumar 
22696319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
22706319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
22716319aee1SViresh Kumar 			continue;
22726319aee1SViresh Kumar 
22736319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
22746319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
22756319aee1SViresh Kumar 	}
2276c0ab9e08SViresh Kumar 
2277c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2278c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
22796319aee1SViresh Kumar }
22806319aee1SViresh Kumar 
22817813dd6fSViresh Kumar /**
2282442e7a17SViresh Kumar  * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
22836319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
22846319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
228517a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
22864f018bc0SViresh Kumar  *
22874f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
22884f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
22894f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
22904f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
22916319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
22926319aee1SViresh Kumar  * we don't need to support that separately.
22934f018bc0SViresh Kumar  *
22944f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
22956319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
22964f018bc0SViresh Kumar  *
22976319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
22986319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2299baea35e4SViresh Kumar  *
2300baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2301baea35e4SViresh Kumar  * "required-opps" are added in DT.
23024f018bc0SViresh Kumar  */
2303442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
23043734b9f2SDmitry Osipenko 			const char * const *names, struct device ***virt_devs)
23054f018bc0SViresh Kumar {
23066319aee1SViresh Kumar 	struct device *virt_dev;
2307baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
23083734b9f2SDmitry Osipenko 	const char * const *name = names;
23094f018bc0SViresh Kumar 
2310cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2311442e7a17SViresh Kumar 		return 0;
23124f018bc0SViresh Kumar 
23136319aee1SViresh Kumar 	/*
23146319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
23156319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
23166319aee1SViresh Kumar 	 * table is added.
23176319aee1SViresh Kumar 	 */
2318442e7a17SViresh Kumar 	if (!opp_table->required_opp_count)
2319442e7a17SViresh Kumar 		return -EPROBE_DEFER;
23206319aee1SViresh Kumar 
23214f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
23224f018bc0SViresh Kumar 
2323c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2324c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2325c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2326c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2327c0ab9e08SViresh Kumar 		goto unlock;
23284f018bc0SViresh Kumar 
23296319aee1SViresh Kumar 	while (*name) {
23306319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
23316319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
23326319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
23336319aee1SViresh Kumar 			goto err;
23346319aee1SViresh Kumar 		}
23354f018bc0SViresh Kumar 
23366319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
23374ea9496cSTang Bin 		if (IS_ERR_OR_NULL(virt_dev)) {
23384ea9496cSTang Bin 			ret = PTR_ERR(virt_dev) ? : -ENODEV;
23396319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
23406319aee1SViresh Kumar 			goto err;
23414f018bc0SViresh Kumar 		}
23424f018bc0SViresh Kumar 
23434f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2344baea35e4SViresh Kumar 		index++;
23456319aee1SViresh Kumar 		name++;
23466319aee1SViresh Kumar 	}
23476319aee1SViresh Kumar 
234817a8f868SViresh Kumar 	if (virt_devs)
234917a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
23504f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23514f018bc0SViresh Kumar 
2352442e7a17SViresh Kumar 	return 0;
23536319aee1SViresh Kumar 
23546319aee1SViresh Kumar err:
2355442e7a17SViresh Kumar 	_detach_genpd(opp_table);
2356c0ab9e08SViresh Kumar unlock:
23576319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2358442e7a17SViresh Kumar 	return ret;
23596319aee1SViresh Kumar 
23604f018bc0SViresh Kumar }
23614f018bc0SViresh Kumar 
23624f018bc0SViresh Kumar /**
2363442e7a17SViresh Kumar  * _opp_detach_genpd() - Detach genpd(s) from the device.
2364442e7a17SViresh Kumar  * @opp_table: OPP table returned by _opp_attach_genpd().
23654f018bc0SViresh Kumar  *
23666319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
23676319aee1SViresh Kumar  * OPP table.
23684f018bc0SViresh Kumar  */
2369442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
23704f018bc0SViresh Kumar {
23714f018bc0SViresh Kumar 	/*
23724f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
23734f018bc0SViresh Kumar 	 * used in parallel.
23744f018bc0SViresh Kumar 	 */
23754f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2376442e7a17SViresh Kumar 	_detach_genpd(opp_table);
23774f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23784f018bc0SViresh Kumar }
2379b4b9e223SDmitry Osipenko 
238011b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data)
238111b9b663SViresh Kumar {
238211b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_GENPD)
2383442e7a17SViresh Kumar 		_opp_detach_genpd(data->opp_table);
238411b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR)
2385b0ec0942SViresh Kumar 		_opp_put_regulators(data->opp_table);
238611b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_SUPPORTED_HW)
238789f03984SViresh Kumar 		_opp_put_supported_hw(data->opp_table);
23881f378c6eSViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
2389aee3352fSViresh Kumar 		_opp_put_config_regulators_helper(data->opp_table);
239011b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_PROP_NAME)
2391298098e5SViresh Kumar 		_opp_put_prop_name(data->opp_table);
239211b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_CLK)
23932368f576SViresh Kumar 		_opp_put_clknames(data->opp_table);
239411b9b663SViresh Kumar 
239511b9b663SViresh Kumar 	dev_pm_opp_put_opp_table(data->opp_table);
239611b9b663SViresh Kumar 	kfree(data);
239711b9b663SViresh Kumar }
239811b9b663SViresh Kumar 
239911b9b663SViresh Kumar /**
240011b9b663SViresh Kumar  * dev_pm_opp_set_config() - Set OPP configuration for the device.
240111b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
240211b9b663SViresh Kumar  * @config: OPP configuration.
240311b9b663SViresh Kumar  *
240411b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
240511b9b663SViresh Kumar  *
240611b9b663SViresh Kumar  * This must be called before any OPPs are initialized for the device. This may
240711b9b663SViresh Kumar  * be called multiple times for the same OPP table, for example once for each
240811b9b663SViresh Kumar  * CPU that share the same table. This must be balanced by the same number of
240911b9b663SViresh Kumar  * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
241011b9b663SViresh Kumar  *
241111b9b663SViresh Kumar  * This returns a token to the caller, which must be passed to
241211b9b663SViresh Kumar  * dev_pm_opp_clear_config() to free the resources later. The value of the
241311b9b663SViresh Kumar  * returned token will be >= 1 for success and negative for errors. The minimum
241411b9b663SViresh Kumar  * value of 1 is chosen here to make it easy for callers to manage the resource.
241511b9b663SViresh Kumar  */
241611b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
241711b9b663SViresh Kumar {
2418298098e5SViresh Kumar 	struct opp_table *opp_table;
241911b9b663SViresh Kumar 	struct opp_config_data *data;
242011b9b663SViresh Kumar 	unsigned int id;
242111b9b663SViresh Kumar 	int ret;
242211b9b663SViresh Kumar 
242311b9b663SViresh Kumar 	data = kmalloc(sizeof(*data), GFP_KERNEL);
242411b9b663SViresh Kumar 	if (!data)
242511b9b663SViresh Kumar 		return -ENOMEM;
242611b9b663SViresh Kumar 
242711b9b663SViresh Kumar 	opp_table = _add_opp_table(dev, false);
242811b9b663SViresh Kumar 	if (IS_ERR(opp_table)) {
242911b9b663SViresh Kumar 		kfree(data);
243011b9b663SViresh Kumar 		return PTR_ERR(opp_table);
243111b9b663SViresh Kumar 	}
243211b9b663SViresh Kumar 
243311b9b663SViresh Kumar 	data->opp_table = opp_table;
243411b9b663SViresh Kumar 	data->flags = 0;
243511b9b663SViresh Kumar 
243611b9b663SViresh Kumar 	/* This should be called before OPPs are initialized */
243711b9b663SViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
243811b9b663SViresh Kumar 		ret = -EBUSY;
243911b9b663SViresh Kumar 		goto err;
244011b9b663SViresh Kumar 	}
244111b9b663SViresh Kumar 
244211b9b663SViresh Kumar 	/* Configure clocks */
244311b9b663SViresh Kumar 	if (config->clk_names) {
24442083da24SViresh Kumar 		ret = _opp_set_clknames(opp_table, dev, config->clk_names,
24452083da24SViresh Kumar 					config->config_clks);
24462368f576SViresh Kumar 		if (ret)
244711b9b663SViresh Kumar 			goto err;
244811b9b663SViresh Kumar 
244911b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_CLK;
24502083da24SViresh Kumar 	} else if (config->config_clks) {
24512083da24SViresh Kumar 		/* Don't allow config callback without clocks */
24522083da24SViresh Kumar 		ret = -EINVAL;
24532083da24SViresh Kumar 		goto err;
245411b9b663SViresh Kumar 	}
245511b9b663SViresh Kumar 
245611b9b663SViresh Kumar 	/* Configure property names */
245711b9b663SViresh Kumar 	if (config->prop_name) {
2458298098e5SViresh Kumar 		ret = _opp_set_prop_name(opp_table, config->prop_name);
2459298098e5SViresh Kumar 		if (ret)
246011b9b663SViresh Kumar 			goto err;
246111b9b663SViresh Kumar 
246211b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_PROP_NAME;
246311b9b663SViresh Kumar 	}
246411b9b663SViresh Kumar 
2465aee3352fSViresh Kumar 	/* Configure config_regulators helper */
2466aee3352fSViresh Kumar 	if (config->config_regulators) {
2467aee3352fSViresh Kumar 		ret = _opp_set_config_regulators_helper(opp_table, dev,
2468aee3352fSViresh Kumar 						config->config_regulators);
2469aee3352fSViresh Kumar 		if (ret)
2470aee3352fSViresh Kumar 			goto err;
2471aee3352fSViresh Kumar 
2472aee3352fSViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR_HELPER;
2473aee3352fSViresh Kumar 	}
2474aee3352fSViresh Kumar 
247511b9b663SViresh Kumar 	/* Configure supported hardware */
247611b9b663SViresh Kumar 	if (config->supported_hw) {
247789f03984SViresh Kumar 		ret = _opp_set_supported_hw(opp_table, config->supported_hw,
247811b9b663SViresh Kumar 					    config->supported_hw_count);
247989f03984SViresh Kumar 		if (ret)
248011b9b663SViresh Kumar 			goto err;
248111b9b663SViresh Kumar 
248211b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_SUPPORTED_HW;
248311b9b663SViresh Kumar 	}
248411b9b663SViresh Kumar 
248511b9b663SViresh Kumar 	/* Configure supplies */
248611b9b663SViresh Kumar 	if (config->regulator_names) {
2487b0ec0942SViresh Kumar 		ret = _opp_set_regulators(opp_table, dev,
2488b0ec0942SViresh Kumar 					  config->regulator_names);
2489b0ec0942SViresh Kumar 		if (ret)
249011b9b663SViresh Kumar 			goto err;
249111b9b663SViresh Kumar 
249211b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR;
249311b9b663SViresh Kumar 	}
249411b9b663SViresh Kumar 
249511b9b663SViresh Kumar 	/* Attach genpds */
249611b9b663SViresh Kumar 	if (config->genpd_names) {
2497442e7a17SViresh Kumar 		ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
249811b9b663SViresh Kumar 					config->virt_devs);
2499442e7a17SViresh Kumar 		if (ret)
250011b9b663SViresh Kumar 			goto err;
250111b9b663SViresh Kumar 
250211b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_GENPD;
250311b9b663SViresh Kumar 	}
250411b9b663SViresh Kumar 
250511b9b663SViresh Kumar 	ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
250611b9b663SViresh Kumar 		       GFP_KERNEL);
250711b9b663SViresh Kumar 	if (ret)
250811b9b663SViresh Kumar 		goto err;
250911b9b663SViresh Kumar 
251011b9b663SViresh Kumar 	return id;
251111b9b663SViresh Kumar 
251211b9b663SViresh Kumar err:
251311b9b663SViresh Kumar 	_opp_clear_config(data);
251411b9b663SViresh Kumar 	return ret;
251511b9b663SViresh Kumar }
251611b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
251711b9b663SViresh Kumar 
251811b9b663SViresh Kumar /**
251911b9b663SViresh Kumar  * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
252011b9b663SViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_config().
252111b9b663SViresh Kumar  *
252211b9b663SViresh Kumar  * This allows all device OPP configurations to be cleared at once. This must be
252311b9b663SViresh Kumar  * called once for each call made to dev_pm_opp_set_config(), in order to free
252411b9b663SViresh Kumar  * the OPPs properly.
252511b9b663SViresh Kumar  *
252611b9b663SViresh Kumar  * Currently the first call itself ends up freeing all the OPP configurations,
252711b9b663SViresh Kumar  * while the later ones only drop the OPP table reference. This works well for
252811b9b663SViresh Kumar  * now as we would never want to use an half initialized OPP table and want to
252911b9b663SViresh Kumar  * remove the configurations together.
253011b9b663SViresh Kumar  */
253111b9b663SViresh Kumar void dev_pm_opp_clear_config(int token)
253211b9b663SViresh Kumar {
253311b9b663SViresh Kumar 	struct opp_config_data *data;
253411b9b663SViresh Kumar 
253511b9b663SViresh Kumar 	/*
253611b9b663SViresh Kumar 	 * This lets the callers call this unconditionally and keep their code
253711b9b663SViresh Kumar 	 * simple.
253811b9b663SViresh Kumar 	 */
253911b9b663SViresh Kumar 	if (unlikely(token <= 0))
254011b9b663SViresh Kumar 		return;
254111b9b663SViresh Kumar 
254211b9b663SViresh Kumar 	data = xa_erase(&opp_configs, token);
254311b9b663SViresh Kumar 	if (WARN_ON(!data))
254411b9b663SViresh Kumar 		return;
254511b9b663SViresh Kumar 
254611b9b663SViresh Kumar 	_opp_clear_config(data);
254711b9b663SViresh Kumar }
254811b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
254911b9b663SViresh Kumar 
255011b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token)
255111b9b663SViresh Kumar {
255211b9b663SViresh Kumar 	dev_pm_opp_clear_config((unsigned long)token);
255311b9b663SViresh Kumar }
255411b9b663SViresh Kumar 
255511b9b663SViresh Kumar /**
255611b9b663SViresh Kumar  * devm_pm_opp_set_config() - Set OPP configuration for the device.
255711b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
255811b9b663SViresh Kumar  * @config: OPP configuration.
255911b9b663SViresh Kumar  *
256011b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
256111b9b663SViresh Kumar  * This is a resource-managed variant of dev_pm_opp_set_config().
256211b9b663SViresh Kumar  *
256311b9b663SViresh Kumar  * Return: 0 on success and errorno otherwise.
256411b9b663SViresh Kumar  */
256511b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
256611b9b663SViresh Kumar {
256711b9b663SViresh Kumar 	int token = dev_pm_opp_set_config(dev, config);
256811b9b663SViresh Kumar 
256911b9b663SViresh Kumar 	if (token < 0)
257011b9b663SViresh Kumar 		return token;
257111b9b663SViresh Kumar 
257211b9b663SViresh Kumar 	return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
257311b9b663SViresh Kumar 					(void *) ((unsigned long) token));
257411b9b663SViresh Kumar }
257511b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
257611b9b663SViresh Kumar 
25774f018bc0SViresh Kumar /**
25787d8658efSSaravana Kannan  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
25797d8658efSSaravana Kannan  * @src_table: OPP table which has @dst_table as one of its required OPP table.
25807d8658efSSaravana Kannan  * @dst_table: Required OPP table of the @src_table.
25817d8658efSSaravana Kannan  * @src_opp: OPP from the @src_table.
25827d8658efSSaravana Kannan  *
25837d8658efSSaravana Kannan  * This function returns the OPP (present in @dst_table) pointed out by the
25847d8658efSSaravana Kannan  * "required-opps" property of the @src_opp (present in @src_table).
25857d8658efSSaravana Kannan  *
25867d8658efSSaravana Kannan  * The callers are required to call dev_pm_opp_put() for the returned OPP after
25877d8658efSSaravana Kannan  * use.
25887d8658efSSaravana Kannan  *
25897d8658efSSaravana Kannan  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
25907d8658efSSaravana Kannan  */
25917d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
25927d8658efSSaravana Kannan 						 struct opp_table *dst_table,
25937d8658efSSaravana Kannan 						 struct dev_pm_opp *src_opp)
25947d8658efSSaravana Kannan {
25957d8658efSSaravana Kannan 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
25967d8658efSSaravana Kannan 	int i;
25977d8658efSSaravana Kannan 
25987d8658efSSaravana Kannan 	if (!src_table || !dst_table || !src_opp ||
25997d8658efSSaravana Kannan 	    !src_table->required_opp_tables)
26007d8658efSSaravana Kannan 		return ERR_PTR(-EINVAL);
26017d8658efSSaravana Kannan 
26027d8658efSSaravana Kannan 	/* required-opps not fully initialized yet */
26037d8658efSSaravana Kannan 	if (lazy_linking_pending(src_table))
26047d8658efSSaravana Kannan 		return ERR_PTR(-EBUSY);
26057d8658efSSaravana Kannan 
26067d8658efSSaravana Kannan 	for (i = 0; i < src_table->required_opp_count; i++) {
26077d8658efSSaravana Kannan 		if (src_table->required_opp_tables[i] == dst_table) {
26087d8658efSSaravana Kannan 			mutex_lock(&src_table->lock);
26097d8658efSSaravana Kannan 
26107d8658efSSaravana Kannan 			list_for_each_entry(opp, &src_table->opp_list, node) {
26117d8658efSSaravana Kannan 				if (opp == src_opp) {
26127d8658efSSaravana Kannan 					dest_opp = opp->required_opps[i];
26137d8658efSSaravana Kannan 					dev_pm_opp_get(dest_opp);
26147d8658efSSaravana Kannan 					break;
26157d8658efSSaravana Kannan 				}
26167d8658efSSaravana Kannan 			}
26177d8658efSSaravana Kannan 
26187d8658efSSaravana Kannan 			mutex_unlock(&src_table->lock);
26197d8658efSSaravana Kannan 			break;
26207d8658efSSaravana Kannan 		}
26217d8658efSSaravana Kannan 	}
26227d8658efSSaravana Kannan 
26237d8658efSSaravana Kannan 	if (IS_ERR(dest_opp)) {
26247d8658efSSaravana Kannan 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
26257d8658efSSaravana Kannan 		       src_table, dst_table);
26267d8658efSSaravana Kannan 	}
26277d8658efSSaravana Kannan 
26287d8658efSSaravana Kannan 	return dest_opp;
26297d8658efSSaravana Kannan }
26307d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
26317d8658efSSaravana Kannan 
26327d8658efSSaravana Kannan /**
2633c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2634c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2635c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2636c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2637c8a59103SViresh Kumar  *
2638c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2639c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2640c8a59103SViresh Kumar  * performance state set to @pstate.
2641c8a59103SViresh Kumar  *
2642c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2643c8a59103SViresh Kumar  * value on errors.
2644c8a59103SViresh Kumar  */
2645c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2646c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2647c8a59103SViresh Kumar 				       unsigned int pstate)
2648c8a59103SViresh Kumar {
2649c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2650c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2651c8a59103SViresh Kumar 	int i;
2652c8a59103SViresh Kumar 
2653c8a59103SViresh Kumar 	/*
2654c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2655c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2656c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2657c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2658c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2659c8a59103SViresh Kumar 	 */
2660f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2661c8a59103SViresh Kumar 		return pstate;
2662c8a59103SViresh Kumar 
26637eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
26647eba0c76SViresh Kumar 	if (lazy_linking_pending(src_table))
26657eba0c76SViresh Kumar 		return -EBUSY;
26667eba0c76SViresh Kumar 
2667c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2668c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2669c8a59103SViresh Kumar 			break;
2670c8a59103SViresh Kumar 	}
2671c8a59103SViresh Kumar 
2672c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2673c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2674c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2675c8a59103SViresh Kumar 		return -EINVAL;
2676c8a59103SViresh Kumar 	}
2677c8a59103SViresh Kumar 
2678c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2679c8a59103SViresh Kumar 
2680c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
2681c8a59103SViresh Kumar 		if (opp->pstate == pstate) {
2682c8a59103SViresh Kumar 			dest_pstate = opp->required_opps[i]->pstate;
2683c8a59103SViresh Kumar 			goto unlock;
2684c8a59103SViresh Kumar 		}
2685c8a59103SViresh Kumar 	}
2686c8a59103SViresh Kumar 
2687c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2688c8a59103SViresh Kumar 	       dst_table);
2689c8a59103SViresh Kumar 
2690c8a59103SViresh Kumar unlock:
2691c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2692c8a59103SViresh Kumar 
2693c8a59103SViresh Kumar 	return dest_pstate;
2694c8a59103SViresh Kumar }
2695c8a59103SViresh Kumar 
2696c8a59103SViresh Kumar /**
26977813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
26987813dd6fSViresh Kumar  * @dev:	device for which we do this operation
26997813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
27007813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
27017813dd6fSViresh Kumar  *
27027813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
27037813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
27047813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
27057813dd6fSViresh Kumar  *
27067813dd6fSViresh Kumar  * Return:
27077813dd6fSViresh Kumar  * 0		On success OR
27087813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
27097813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
27107813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
27117813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
27127813dd6fSViresh Kumar  */
27137813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
27147813dd6fSViresh Kumar {
27157813dd6fSViresh Kumar 	struct opp_table *opp_table;
27167813dd6fSViresh Kumar 	int ret;
27177813dd6fSViresh Kumar 
271832439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2719dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2720dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
27217813dd6fSViresh Kumar 
272246f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
272346f48acaSViresh Kumar 	opp_table->regulator_count = 1;
272446f48acaSViresh Kumar 
27257813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
27260ad8c623SViresh Kumar 	if (ret)
27277813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
27280ad8c623SViresh Kumar 
27297813dd6fSViresh Kumar 	return ret;
27307813dd6fSViresh Kumar }
27317813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
27327813dd6fSViresh Kumar 
27337813dd6fSViresh Kumar /**
27347813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
27357813dd6fSViresh Kumar  * @dev:		device for which we do this operation
27367813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
27377813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
27387813dd6fSViresh Kumar  *
27397813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
27407813dd6fSViresh Kumar  * which is isolated here.
27417813dd6fSViresh Kumar  *
27427813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
27437813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
27447813dd6fSViresh Kumar  * successful.
27457813dd6fSViresh Kumar  */
27467813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
27477813dd6fSViresh Kumar 				 bool availability_req)
27487813dd6fSViresh Kumar {
27497813dd6fSViresh Kumar 	struct opp_table *opp_table;
27507813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
27517813dd6fSViresh Kumar 	int r = 0;
27527813dd6fSViresh Kumar 
27537813dd6fSViresh Kumar 	/* Find the opp_table */
27547813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
27557813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
27567813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
27577813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
27587813dd6fSViresh Kumar 		return r;
27597813dd6fSViresh Kumar 	}
27607813dd6fSViresh Kumar 
2761*f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table)) {
2762*f123ea74SViresh Kumar 		r = -EINVAL;
2763*f123ea74SViresh Kumar 		goto put_table;
2764*f123ea74SViresh Kumar 	}
2765*f123ea74SViresh Kumar 
27667813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
27677813dd6fSViresh Kumar 
27687813dd6fSViresh Kumar 	/* Do we have the frequency? */
27697813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
27702083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
27717813dd6fSViresh Kumar 			opp = tmp_opp;
27727813dd6fSViresh Kumar 			break;
27737813dd6fSViresh Kumar 		}
27747813dd6fSViresh Kumar 	}
27757813dd6fSViresh Kumar 
27767813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
27777813dd6fSViresh Kumar 		r = PTR_ERR(opp);
27787813dd6fSViresh Kumar 		goto unlock;
27797813dd6fSViresh Kumar 	}
27807813dd6fSViresh Kumar 
27817813dd6fSViresh Kumar 	/* Is update really needed? */
27827813dd6fSViresh Kumar 	if (opp->available == availability_req)
27837813dd6fSViresh Kumar 		goto unlock;
27847813dd6fSViresh Kumar 
27857813dd6fSViresh Kumar 	opp->available = availability_req;
27867813dd6fSViresh Kumar 
27877813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
27887813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
27897813dd6fSViresh Kumar 
27907813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
27917813dd6fSViresh Kumar 	if (availability_req)
27927813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
27937813dd6fSViresh Kumar 					     opp);
27947813dd6fSViresh Kumar 	else
27957813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
27967813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
27977813dd6fSViresh Kumar 
27987813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
27997813dd6fSViresh Kumar 	goto put_table;
28007813dd6fSViresh Kumar 
28017813dd6fSViresh Kumar unlock:
28027813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
28037813dd6fSViresh Kumar put_table:
28047813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
28057813dd6fSViresh Kumar 	return r;
28067813dd6fSViresh Kumar }
28077813dd6fSViresh Kumar 
28087813dd6fSViresh Kumar /**
280925cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
281025cb20a2SStephen Boyd  * @dev:		device for which we do this operation
281125cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
281225cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
281325cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
281425cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
281525cb20a2SStephen Boyd  *
281625cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
281725cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
281825cb20a2SStephen Boyd  * successful.
281925cb20a2SStephen Boyd  */
282025cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
282125cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
282225cb20a2SStephen Boyd 			      unsigned long u_volt_max)
282325cb20a2SStephen Boyd 
282425cb20a2SStephen Boyd {
282525cb20a2SStephen Boyd 	struct opp_table *opp_table;
282625cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
282725cb20a2SStephen Boyd 	int r = 0;
282825cb20a2SStephen Boyd 
282925cb20a2SStephen Boyd 	/* Find the opp_table */
283025cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
283125cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
283225cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
283325cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
283425cb20a2SStephen Boyd 		return r;
283525cb20a2SStephen Boyd 	}
283625cb20a2SStephen Boyd 
2837*f123ea74SViresh Kumar 	if (!assert_single_clk(opp_table)) {
2838*f123ea74SViresh Kumar 		r = -EINVAL;
2839*f123ea74SViresh Kumar 		goto put_table;
2840*f123ea74SViresh Kumar 	}
2841*f123ea74SViresh Kumar 
284225cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
284325cb20a2SStephen Boyd 
284425cb20a2SStephen Boyd 	/* Do we have the frequency? */
284525cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
28462083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
284725cb20a2SStephen Boyd 			opp = tmp_opp;
284825cb20a2SStephen Boyd 			break;
284925cb20a2SStephen Boyd 		}
285025cb20a2SStephen Boyd 	}
285125cb20a2SStephen Boyd 
285225cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
285325cb20a2SStephen Boyd 		r = PTR_ERR(opp);
285425cb20a2SStephen Boyd 		goto adjust_unlock;
285525cb20a2SStephen Boyd 	}
285625cb20a2SStephen Boyd 
285725cb20a2SStephen Boyd 	/* Is update really needed? */
285825cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
285925cb20a2SStephen Boyd 		goto adjust_unlock;
286025cb20a2SStephen Boyd 
286125cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
286225cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
286325cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
286425cb20a2SStephen Boyd 
286525cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
286625cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
286725cb20a2SStephen Boyd 
286825cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
286925cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
287025cb20a2SStephen Boyd 				     opp);
287125cb20a2SStephen Boyd 
287225cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
2873*f123ea74SViresh Kumar 	goto put_table;
287425cb20a2SStephen Boyd 
287525cb20a2SStephen Boyd adjust_unlock:
287625cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
2877*f123ea74SViresh Kumar put_table:
287825cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
287925cb20a2SStephen Boyd 	return r;
288025cb20a2SStephen Boyd }
288103649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
288225cb20a2SStephen Boyd 
288325cb20a2SStephen Boyd /**
28847813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
28857813dd6fSViresh Kumar  * @dev:	device for which we do this operation
28867813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
28877813dd6fSViresh Kumar  *
28887813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
28897813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
28907813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
28917813dd6fSViresh Kumar  *
28927813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
28937813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
28947813dd6fSViresh Kumar  * successful.
28957813dd6fSViresh Kumar  */
28967813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
28977813dd6fSViresh Kumar {
28987813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
28997813dd6fSViresh Kumar }
29007813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
29017813dd6fSViresh Kumar 
29027813dd6fSViresh Kumar /**
29037813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
29047813dd6fSViresh Kumar  * @dev:	device for which we do this operation
29057813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
29067813dd6fSViresh Kumar  *
29077813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
29087813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
29097813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
29107813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
29117813dd6fSViresh Kumar  *
29127813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
29137813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
29147813dd6fSViresh Kumar  * successful.
29157813dd6fSViresh Kumar  */
29167813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
29177813dd6fSViresh Kumar {
29187813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
29197813dd6fSViresh Kumar }
29207813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
29217813dd6fSViresh Kumar 
29227813dd6fSViresh Kumar /**
29237813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
29247813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
29257813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
29267813dd6fSViresh Kumar  *
29277813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
29287813dd6fSViresh Kumar  */
29297813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
29307813dd6fSViresh Kumar {
29317813dd6fSViresh Kumar 	struct opp_table *opp_table;
29327813dd6fSViresh Kumar 	int ret;
29337813dd6fSViresh Kumar 
29347813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29357813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
29367813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
29377813dd6fSViresh Kumar 
29387813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
29397813dd6fSViresh Kumar 
29407813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29417813dd6fSViresh Kumar 
29427813dd6fSViresh Kumar 	return ret;
29437813dd6fSViresh Kumar }
29447813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
29457813dd6fSViresh Kumar 
29467813dd6fSViresh Kumar /**
29477813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
29487813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
29497813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
29507813dd6fSViresh Kumar  *
29517813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
29527813dd6fSViresh Kumar  */
29537813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
29547813dd6fSViresh Kumar 				   struct notifier_block *nb)
29557813dd6fSViresh Kumar {
29567813dd6fSViresh Kumar 	struct opp_table *opp_table;
29577813dd6fSViresh Kumar 	int ret;
29587813dd6fSViresh Kumar 
29597813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29607813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
29617813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
29627813dd6fSViresh Kumar 
29637813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
29647813dd6fSViresh Kumar 
29657813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29667813dd6fSViresh Kumar 
29677813dd6fSViresh Kumar 	return ret;
29687813dd6fSViresh Kumar }
29697813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
29707813dd6fSViresh Kumar 
29718aaf6264SViresh Kumar /**
29728aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
29738aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
29748aaf6264SViresh Kumar  *
29758aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
29768aaf6264SViresh Kumar  * dynamically added entries.
29778aaf6264SViresh Kumar  */
29788aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
29797813dd6fSViresh Kumar {
29807813dd6fSViresh Kumar 	struct opp_table *opp_table;
29817813dd6fSViresh Kumar 
29827813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
29837813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29847813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
29857813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
29867813dd6fSViresh Kumar 
29877813dd6fSViresh Kumar 		if (error != -ENODEV)
29887813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
29897813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
29907813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
29917813dd6fSViresh Kumar 			     error);
29927813dd6fSViresh Kumar 		return;
29937813dd6fSViresh Kumar 	}
29947813dd6fSViresh Kumar 
2995922ff075SViresh Kumar 	/*
2996922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
2997922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
2998922ff075SViresh Kumar 	 **/
2999922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
3000cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
3001cdd6ed90SViresh Kumar 
3002922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
30037813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
30047813dd6fSViresh Kumar }
30057813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
3006ce8073d8SDmitry Osipenko 
3007ce8073d8SDmitry Osipenko /**
3008ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
3009ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
3010ce8073d8SDmitry Osipenko  *
3011ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
3012ce8073d8SDmitry Osipenko  *
3013ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
3014ce8073d8SDmitry Osipenko  */
3015ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
3016ce8073d8SDmitry Osipenko {
3017ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
3018ce8073d8SDmitry Osipenko 	struct regulator *reg;
3019ce8073d8SDmitry Osipenko 	int i, ret = 0;
3020ce8073d8SDmitry Osipenko 
3021ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
3022ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
3023ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
3024ce8073d8SDmitry Osipenko 		return 0;
3025ce8073d8SDmitry Osipenko 
3026ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
3027ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
3028ce8073d8SDmitry Osipenko 		goto put_table;
3029ce8073d8SDmitry Osipenko 
3030ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
3031ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
3032ce8073d8SDmitry Osipenko 		goto put_table;
3033ce8073d8SDmitry Osipenko 
3034ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
3035ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
3036ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
3037ce8073d8SDmitry Osipenko 		if (ret)
3038ce8073d8SDmitry Osipenko 			break;
3039ce8073d8SDmitry Osipenko 	}
3040ce8073d8SDmitry Osipenko put_table:
3041ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
3042ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
3043ce8073d8SDmitry Osipenko 
3044ce8073d8SDmitry Osipenko 	return ret;
3045ce8073d8SDmitry Osipenko }
3046ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
3047