xref: /openbmc/linux/drivers/opp/core.c (revision a74f681c3710b47a093d910ca7c6666b3d1e3a2c)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27813dd6fSViresh Kumar /*
37813dd6fSViresh Kumar  * Generic OPP Interface
47813dd6fSViresh Kumar  *
57813dd6fSViresh Kumar  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
67813dd6fSViresh Kumar  *	Nishanth Menon
77813dd6fSViresh Kumar  *	Romit Dasgupta
87813dd6fSViresh Kumar  *	Kevin Hilman
97813dd6fSViresh Kumar  */
107813dd6fSViresh Kumar 
117813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
127813dd6fSViresh Kumar 
137813dd6fSViresh Kumar #include <linux/clk.h>
147813dd6fSViresh Kumar #include <linux/errno.h>
157813dd6fSViresh Kumar #include <linux/err.h>
167813dd6fSViresh Kumar #include <linux/slab.h>
177813dd6fSViresh Kumar #include <linux/device.h>
187813dd6fSViresh Kumar #include <linux/export.h>
19009acd19SViresh Kumar #include <linux/pm_domain.h>
207813dd6fSViresh Kumar #include <linux/regulator/consumer.h>
217813dd6fSViresh Kumar 
227813dd6fSViresh Kumar #include "opp.h"
237813dd6fSViresh Kumar 
247813dd6fSViresh Kumar /*
257813dd6fSViresh Kumar  * The root of the list of all opp-tables. All opp_table structures branch off
267813dd6fSViresh Kumar  * from here, with each opp_table containing the list of opps it supports in
277813dd6fSViresh Kumar  * various states of availability.
287813dd6fSViresh Kumar  */
297813dd6fSViresh Kumar LIST_HEAD(opp_tables);
307eba0c76SViresh Kumar 
317eba0c76SViresh Kumar /* OPP tables with uninitialized required OPPs */
327eba0c76SViresh Kumar LIST_HEAD(lazy_opp_tables);
337eba0c76SViresh Kumar 
347813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
357813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
3627c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */
3727c09484SViresh Kumar static bool opp_tables_busy;
387813dd6fSViresh Kumar 
399e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
407813dd6fSViresh Kumar {
417813dd6fSViresh Kumar 	struct opp_device *opp_dev;
429e62edacSViresh Kumar 	bool found = false;
437813dd6fSViresh Kumar 
449e62edacSViresh Kumar 	mutex_lock(&opp_table->lock);
457813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
469e62edacSViresh Kumar 		if (opp_dev->dev == dev) {
479e62edacSViresh Kumar 			found = true;
489e62edacSViresh Kumar 			break;
499e62edacSViresh Kumar 		}
507813dd6fSViresh Kumar 
519e62edacSViresh Kumar 	mutex_unlock(&opp_table->lock);
529e62edacSViresh Kumar 	return found;
537813dd6fSViresh Kumar }
547813dd6fSViresh Kumar 
557813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
567813dd6fSViresh Kumar {
577813dd6fSViresh Kumar 	struct opp_table *opp_table;
587813dd6fSViresh Kumar 
597813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
609e62edacSViresh Kumar 		if (_find_opp_dev(dev, opp_table)) {
617813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
627813dd6fSViresh Kumar 			return opp_table;
637813dd6fSViresh Kumar 		}
647813dd6fSViresh Kumar 	}
657813dd6fSViresh Kumar 
667813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
677813dd6fSViresh Kumar }
687813dd6fSViresh Kumar 
697813dd6fSViresh Kumar /**
707813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
717813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
727813dd6fSViresh Kumar  *
737813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
747813dd6fSViresh Kumar  *
757813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
767813dd6fSViresh Kumar  * -EINVAL based on type of error.
777813dd6fSViresh Kumar  *
787813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
797813dd6fSViresh Kumar  */
807813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
817813dd6fSViresh Kumar {
827813dd6fSViresh Kumar 	struct opp_table *opp_table;
837813dd6fSViresh Kumar 
847813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
857813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
867813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
877813dd6fSViresh Kumar 	}
887813dd6fSViresh Kumar 
897813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
907813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
917813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
927813dd6fSViresh Kumar 
937813dd6fSViresh Kumar 	return opp_table;
947813dd6fSViresh Kumar }
957813dd6fSViresh Kumar 
967813dd6fSViresh Kumar /**
977813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
987813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
997813dd6fSViresh Kumar  *
1007813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
1017813dd6fSViresh Kumar  * return 0
1027813dd6fSViresh Kumar  *
1037813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1047813dd6fSViresh Kumar  */
1057813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1067813dd6fSViresh Kumar {
1077813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1087813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1097813dd6fSViresh Kumar 		return 0;
1107813dd6fSViresh Kumar 	}
1117813dd6fSViresh Kumar 
1127813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1137813dd6fSViresh Kumar }
1147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1157813dd6fSViresh Kumar 
1167813dd6fSViresh Kumar /**
1177813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1187813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1197813dd6fSViresh Kumar  *
1207813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1217813dd6fSViresh Kumar  * return 0
1227813dd6fSViresh Kumar  */
1237813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1247813dd6fSViresh Kumar {
12506a8a059SAndrew-sh.Cheng 	if (IS_ERR_OR_NULL(opp)) {
1267813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1277813dd6fSViresh Kumar 		return 0;
1287813dd6fSViresh Kumar 	}
1297813dd6fSViresh Kumar 
1307813dd6fSViresh Kumar 	return opp->rate;
1317813dd6fSViresh Kumar }
1327813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
1337813dd6fSViresh Kumar 
1347813dd6fSViresh Kumar /**
1355b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
1365b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
1375b93ac54SRajendra Nayak  *
1385b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
1395b93ac54SRajendra Nayak  * return 0.
1405b93ac54SRajendra Nayak  */
1415b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
1425b93ac54SRajendra Nayak {
1435b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1445b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
1455b93ac54SRajendra Nayak 		return 0;
1465b93ac54SRajendra Nayak 	}
1475b93ac54SRajendra Nayak 
1485b93ac54SRajendra Nayak 	return opp->level;
1495b93ac54SRajendra Nayak }
1505b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
1515b93ac54SRajendra Nayak 
1525b93ac54SRajendra Nayak /**
153597ff543SDmitry Osipenko  * dev_pm_opp_get_required_pstate() - Gets the required performance state
154597ff543SDmitry Osipenko  *                                    corresponding to an available opp
155597ff543SDmitry Osipenko  * @opp:	opp for which performance state has to be returned for
156597ff543SDmitry Osipenko  * @index:	index of the required opp
157597ff543SDmitry Osipenko  *
158597ff543SDmitry Osipenko  * Return: performance state read from device tree corresponding to the
159597ff543SDmitry Osipenko  * required opp, else return 0.
160597ff543SDmitry Osipenko  */
161597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
162597ff543SDmitry Osipenko 					    unsigned int index)
163597ff543SDmitry Osipenko {
164597ff543SDmitry Osipenko 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
165597ff543SDmitry Osipenko 	    index >= opp->opp_table->required_opp_count) {
166597ff543SDmitry Osipenko 		pr_err("%s: Invalid parameters\n", __func__);
167597ff543SDmitry Osipenko 		return 0;
168597ff543SDmitry Osipenko 	}
169597ff543SDmitry Osipenko 
1707eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
1717eba0c76SViresh Kumar 	if (lazy_linking_pending(opp->opp_table))
1727eba0c76SViresh Kumar 		return 0;
1737eba0c76SViresh Kumar 
174597ff543SDmitry Osipenko 	return opp->required_opps[index]->pstate;
175597ff543SDmitry Osipenko }
176597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
177597ff543SDmitry Osipenko 
178597ff543SDmitry Osipenko /**
1797813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
1807813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
1817813dd6fSViresh Kumar  *
1827813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
1837813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
1847813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
1857813dd6fSViresh Kumar  *
1867813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
1877813dd6fSViresh Kumar  */
1887813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
1897813dd6fSViresh Kumar {
1907813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1917813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1927813dd6fSViresh Kumar 		return false;
1937813dd6fSViresh Kumar 	}
1947813dd6fSViresh Kumar 
1957813dd6fSViresh Kumar 	return opp->turbo;
1967813dd6fSViresh Kumar }
1977813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
1987813dd6fSViresh Kumar 
1997813dd6fSViresh Kumar /**
2007813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
2017813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2027813dd6fSViresh Kumar  *
2037813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
2047813dd6fSViresh Kumar  */
2057813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
2067813dd6fSViresh Kumar {
2077813dd6fSViresh Kumar 	struct opp_table *opp_table;
2087813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
2097813dd6fSViresh Kumar 
2107813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2117813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2127813dd6fSViresh Kumar 		return 0;
2137813dd6fSViresh Kumar 
2147813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
2157813dd6fSViresh Kumar 
2167813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2177813dd6fSViresh Kumar 
2187813dd6fSViresh Kumar 	return clock_latency_ns;
2197813dd6fSViresh Kumar }
2207813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
2217813dd6fSViresh Kumar 
2227813dd6fSViresh Kumar /**
2237813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
2247813dd6fSViresh Kumar  * @dev: device for which we do this operation
2257813dd6fSViresh Kumar  *
2267813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
2277813dd6fSViresh Kumar  */
2287813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
2297813dd6fSViresh Kumar {
2307813dd6fSViresh Kumar 	struct opp_table *opp_table;
2317813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
2327813dd6fSViresh Kumar 	struct regulator *reg;
2337813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
2347813dd6fSViresh Kumar 	int ret, i, count;
2357813dd6fSViresh Kumar 	struct {
2367813dd6fSViresh Kumar 		unsigned long min;
2377813dd6fSViresh Kumar 		unsigned long max;
2387813dd6fSViresh Kumar 	} *uV;
2397813dd6fSViresh Kumar 
2407813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2417813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2427813dd6fSViresh Kumar 		return 0;
2437813dd6fSViresh Kumar 
2447813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
24590e3577bSViresh Kumar 	if (!opp_table->regulators)
2467813dd6fSViresh Kumar 		goto put_opp_table;
2477813dd6fSViresh Kumar 
24890e3577bSViresh Kumar 	count = opp_table->regulator_count;
24990e3577bSViresh Kumar 
2507813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
2517813dd6fSViresh Kumar 	if (!uV)
2527813dd6fSViresh Kumar 		goto put_opp_table;
2537813dd6fSViresh Kumar 
2547813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
2557813dd6fSViresh Kumar 
2567813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2577813dd6fSViresh Kumar 		uV[i].min = ~0;
2587813dd6fSViresh Kumar 		uV[i].max = 0;
2597813dd6fSViresh Kumar 
2607813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
2617813dd6fSViresh Kumar 			if (!opp->available)
2627813dd6fSViresh Kumar 				continue;
2637813dd6fSViresh Kumar 
2647813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
2657813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
2667813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
2677813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
2687813dd6fSViresh Kumar 		}
2697813dd6fSViresh Kumar 	}
2707813dd6fSViresh Kumar 
2717813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
2727813dd6fSViresh Kumar 
2737813dd6fSViresh Kumar 	/*
2747813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
2757813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
2767813dd6fSViresh Kumar 	 */
2777813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2787813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
2797813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
2807813dd6fSViresh Kumar 		if (ret > 0)
2817813dd6fSViresh Kumar 			latency_ns += ret * 1000;
2827813dd6fSViresh Kumar 	}
2837813dd6fSViresh Kumar 
2847813dd6fSViresh Kumar 	kfree(uV);
2857813dd6fSViresh Kumar put_opp_table:
2867813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2877813dd6fSViresh Kumar 
2887813dd6fSViresh Kumar 	return latency_ns;
2897813dd6fSViresh Kumar }
2907813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
2917813dd6fSViresh Kumar 
2927813dd6fSViresh Kumar /**
2937813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
2947813dd6fSViresh Kumar  *					     nanoseconds
2957813dd6fSViresh Kumar  * @dev: device for which we do this operation
2967813dd6fSViresh Kumar  *
2977813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
2987813dd6fSViresh Kumar  * switch from one OPP to other.
2997813dd6fSViresh Kumar  */
3007813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
3017813dd6fSViresh Kumar {
3027813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
3037813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
3047813dd6fSViresh Kumar }
3057813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
3067813dd6fSViresh Kumar 
3077813dd6fSViresh Kumar /**
3087813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
3097813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3107813dd6fSViresh Kumar  *
3117813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
3127813dd6fSViresh Kumar  * if one is available, else returns 0;
3137813dd6fSViresh Kumar  */
3147813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
3157813dd6fSViresh Kumar {
3167813dd6fSViresh Kumar 	struct opp_table *opp_table;
3177813dd6fSViresh Kumar 	unsigned long freq = 0;
3187813dd6fSViresh Kumar 
3197813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3207813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3217813dd6fSViresh Kumar 		return 0;
3227813dd6fSViresh Kumar 
3237813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
3247813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
3257813dd6fSViresh Kumar 
3267813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3277813dd6fSViresh Kumar 
3287813dd6fSViresh Kumar 	return freq;
3297813dd6fSViresh Kumar }
3307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
3317813dd6fSViresh Kumar 
332a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
333a1e8c136SViresh Kumar {
334a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
335a1e8c136SViresh Kumar 	int count = 0;
336a1e8c136SViresh Kumar 
337a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
338a1e8c136SViresh Kumar 
339a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
340a1e8c136SViresh Kumar 		if (opp->available)
341a1e8c136SViresh Kumar 			count++;
342a1e8c136SViresh Kumar 	}
343a1e8c136SViresh Kumar 
344a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
345a1e8c136SViresh Kumar 
346a1e8c136SViresh Kumar 	return count;
347a1e8c136SViresh Kumar }
348a1e8c136SViresh Kumar 
3497813dd6fSViresh Kumar /**
3507813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
3517813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3527813dd6fSViresh Kumar  *
3537813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
3547813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
3557813dd6fSViresh Kumar  */
3567813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
3577813dd6fSViresh Kumar {
3587813dd6fSViresh Kumar 	struct opp_table *opp_table;
359a1e8c136SViresh Kumar 	int count;
3607813dd6fSViresh Kumar 
3617813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3627813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
3637813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
364035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
3657813dd6fSViresh Kumar 			__func__, count);
36609f662f9SViresh Kumar 		return count;
3677813dd6fSViresh Kumar 	}
3687813dd6fSViresh Kumar 
369a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
3707813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3717813dd6fSViresh Kumar 
3727813dd6fSViresh Kumar 	return count;
3737813dd6fSViresh Kumar }
3747813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
3757813dd6fSViresh Kumar 
3767813dd6fSViresh Kumar /**
3777813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
3787813dd6fSViresh Kumar  * @dev:		device for which we do this operation
3797813dd6fSViresh Kumar  * @freq:		frequency to search for
3807813dd6fSViresh Kumar  * @available:		true/false - match for available opp
3817813dd6fSViresh Kumar  *
3827813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
3837813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
3847813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
3857813dd6fSViresh Kumar  * EINVAL:	for bad pointer
3867813dd6fSViresh Kumar  * ERANGE:	no match found for search
3877813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
3887813dd6fSViresh Kumar  *
3897813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
3907813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
3917813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
3927813dd6fSViresh Kumar  *
3937813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
3947813dd6fSViresh Kumar  * or the opposite as well.
3957813dd6fSViresh Kumar  *
3967813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
3977813dd6fSViresh Kumar  * use.
3987813dd6fSViresh Kumar  */
3997813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
4007813dd6fSViresh Kumar 					      unsigned long freq,
4017813dd6fSViresh Kumar 					      bool available)
4027813dd6fSViresh Kumar {
4037813dd6fSViresh Kumar 	struct opp_table *opp_table;
4047813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
4057813dd6fSViresh Kumar 
4067813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4077813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
4087813dd6fSViresh Kumar 		int r = PTR_ERR(opp_table);
4097813dd6fSViresh Kumar 
4107813dd6fSViresh Kumar 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
4117813dd6fSViresh Kumar 		return ERR_PTR(r);
4127813dd6fSViresh Kumar 	}
4137813dd6fSViresh Kumar 
4147813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
4157813dd6fSViresh Kumar 
4167813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
4177813dd6fSViresh Kumar 		if (temp_opp->available == available &&
4187813dd6fSViresh Kumar 				temp_opp->rate == freq) {
4197813dd6fSViresh Kumar 			opp = temp_opp;
4207813dd6fSViresh Kumar 
4217813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
4227813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
4237813dd6fSViresh Kumar 			break;
4247813dd6fSViresh Kumar 		}
4257813dd6fSViresh Kumar 	}
4267813dd6fSViresh Kumar 
4277813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
4287813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4297813dd6fSViresh Kumar 
4307813dd6fSViresh Kumar 	return opp;
4317813dd6fSViresh Kumar }
4327813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
4337813dd6fSViresh Kumar 
43471419d84SNiklas Cassel /**
43571419d84SNiklas Cassel  * dev_pm_opp_find_level_exact() - search for an exact level
43671419d84SNiklas Cassel  * @dev:		device for which we do this operation
43771419d84SNiklas Cassel  * @level:		level to search for
43871419d84SNiklas Cassel  *
43971419d84SNiklas Cassel  * Return: Searches for exact match in the opp table and returns pointer to the
44071419d84SNiklas Cassel  * matching opp if found, else returns ERR_PTR in case of error and should
44171419d84SNiklas Cassel  * be handled using IS_ERR. Error return values can be:
44271419d84SNiklas Cassel  * EINVAL:	for bad pointer
44371419d84SNiklas Cassel  * ERANGE:	no match found for search
44471419d84SNiklas Cassel  * ENODEV:	if device not found in list of registered devices
44571419d84SNiklas Cassel  *
44671419d84SNiklas Cassel  * The callers are required to call dev_pm_opp_put() for the returned OPP after
44771419d84SNiklas Cassel  * use.
44871419d84SNiklas Cassel  */
44971419d84SNiklas Cassel struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
45071419d84SNiklas Cassel 					       unsigned int level)
45171419d84SNiklas Cassel {
45271419d84SNiklas Cassel 	struct opp_table *opp_table;
45371419d84SNiklas Cassel 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
45471419d84SNiklas Cassel 
45571419d84SNiklas Cassel 	opp_table = _find_opp_table(dev);
45671419d84SNiklas Cassel 	if (IS_ERR(opp_table)) {
45771419d84SNiklas Cassel 		int r = PTR_ERR(opp_table);
45871419d84SNiklas Cassel 
45971419d84SNiklas Cassel 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
46071419d84SNiklas Cassel 		return ERR_PTR(r);
46171419d84SNiklas Cassel 	}
46271419d84SNiklas Cassel 
46371419d84SNiklas Cassel 	mutex_lock(&opp_table->lock);
46471419d84SNiklas Cassel 
46571419d84SNiklas Cassel 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
46671419d84SNiklas Cassel 		if (temp_opp->level == level) {
46771419d84SNiklas Cassel 			opp = temp_opp;
46871419d84SNiklas Cassel 
46971419d84SNiklas Cassel 			/* Increment the reference count of OPP */
47071419d84SNiklas Cassel 			dev_pm_opp_get(opp);
47171419d84SNiklas Cassel 			break;
47271419d84SNiklas Cassel 		}
47371419d84SNiklas Cassel 	}
47471419d84SNiklas Cassel 
47571419d84SNiklas Cassel 	mutex_unlock(&opp_table->lock);
47671419d84SNiklas Cassel 	dev_pm_opp_put_opp_table(opp_table);
47771419d84SNiklas Cassel 
47871419d84SNiklas Cassel 	return opp;
47971419d84SNiklas Cassel }
48071419d84SNiklas Cassel EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
48171419d84SNiklas Cassel 
4828dd5cadaSDmitry Osipenko /**
4838dd5cadaSDmitry Osipenko  * dev_pm_opp_find_level_ceil() - search for an rounded up level
4848dd5cadaSDmitry Osipenko  * @dev:		device for which we do this operation
4858dd5cadaSDmitry Osipenko  * @level:		level to search for
4868dd5cadaSDmitry Osipenko  *
4878dd5cadaSDmitry Osipenko  * Return: Searches for rounded up match in the opp table and returns pointer
4888dd5cadaSDmitry Osipenko  * to the  matching opp if found, else returns ERR_PTR in case of error and
4898dd5cadaSDmitry Osipenko  * should be handled using IS_ERR. Error return values can be:
4908dd5cadaSDmitry Osipenko  * EINVAL:	for bad pointer
4918dd5cadaSDmitry Osipenko  * ERANGE:	no match found for search
4928dd5cadaSDmitry Osipenko  * ENODEV:	if device not found in list of registered devices
4938dd5cadaSDmitry Osipenko  *
4948dd5cadaSDmitry Osipenko  * The callers are required to call dev_pm_opp_put() for the returned OPP after
4958dd5cadaSDmitry Osipenko  * use.
4968dd5cadaSDmitry Osipenko  */
4978dd5cadaSDmitry Osipenko struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
4988dd5cadaSDmitry Osipenko 					      unsigned int *level)
4998dd5cadaSDmitry Osipenko {
5008dd5cadaSDmitry Osipenko 	struct opp_table *opp_table;
5018dd5cadaSDmitry Osipenko 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5028dd5cadaSDmitry Osipenko 
5038dd5cadaSDmitry Osipenko 	opp_table = _find_opp_table(dev);
5048dd5cadaSDmitry Osipenko 	if (IS_ERR(opp_table)) {
5058dd5cadaSDmitry Osipenko 		int r = PTR_ERR(opp_table);
5068dd5cadaSDmitry Osipenko 
5078dd5cadaSDmitry Osipenko 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
5088dd5cadaSDmitry Osipenko 		return ERR_PTR(r);
5098dd5cadaSDmitry Osipenko 	}
5108dd5cadaSDmitry Osipenko 
5118dd5cadaSDmitry Osipenko 	mutex_lock(&opp_table->lock);
5128dd5cadaSDmitry Osipenko 
5138dd5cadaSDmitry Osipenko 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5148dd5cadaSDmitry Osipenko 		if (temp_opp->available && temp_opp->level >= *level) {
5158dd5cadaSDmitry Osipenko 			opp = temp_opp;
5168dd5cadaSDmitry Osipenko 			*level = opp->level;
5178dd5cadaSDmitry Osipenko 
5188dd5cadaSDmitry Osipenko 			/* Increment the reference count of OPP */
5198dd5cadaSDmitry Osipenko 			dev_pm_opp_get(opp);
5208dd5cadaSDmitry Osipenko 			break;
5218dd5cadaSDmitry Osipenko 		}
5228dd5cadaSDmitry Osipenko 	}
5238dd5cadaSDmitry Osipenko 
5248dd5cadaSDmitry Osipenko 	mutex_unlock(&opp_table->lock);
5258dd5cadaSDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
5268dd5cadaSDmitry Osipenko 
5278dd5cadaSDmitry Osipenko 	return opp;
5288dd5cadaSDmitry Osipenko }
5298dd5cadaSDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
5308dd5cadaSDmitry Osipenko 
5317813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
5327813dd6fSViresh Kumar 						   unsigned long *freq)
5337813dd6fSViresh Kumar {
5347813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5357813dd6fSViresh Kumar 
5367813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
5377813dd6fSViresh Kumar 
5387813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5397813dd6fSViresh Kumar 		if (temp_opp->available && temp_opp->rate >= *freq) {
5407813dd6fSViresh Kumar 			opp = temp_opp;
5417813dd6fSViresh Kumar 			*freq = opp->rate;
5427813dd6fSViresh Kumar 
5437813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
5447813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
5457813dd6fSViresh Kumar 			break;
5467813dd6fSViresh Kumar 		}
5477813dd6fSViresh Kumar 	}
5487813dd6fSViresh Kumar 
5497813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
5507813dd6fSViresh Kumar 
5517813dd6fSViresh Kumar 	return opp;
5527813dd6fSViresh Kumar }
5537813dd6fSViresh Kumar 
5547813dd6fSViresh Kumar /**
5557813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
5567813dd6fSViresh Kumar  * @dev:	device for which we do this operation
5577813dd6fSViresh Kumar  * @freq:	Start frequency
5587813dd6fSViresh Kumar  *
5597813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
5607813dd6fSViresh Kumar  * for a device.
5617813dd6fSViresh Kumar  *
5627813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
5637813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
5647813dd6fSViresh Kumar  * values can be:
5657813dd6fSViresh Kumar  * EINVAL:	for bad pointer
5667813dd6fSViresh Kumar  * ERANGE:	no match found for search
5677813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
5687813dd6fSViresh Kumar  *
5697813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5707813dd6fSViresh Kumar  * use.
5717813dd6fSViresh Kumar  */
5727813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
5737813dd6fSViresh Kumar 					     unsigned long *freq)
5747813dd6fSViresh Kumar {
5757813dd6fSViresh Kumar 	struct opp_table *opp_table;
5767813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
5777813dd6fSViresh Kumar 
5787813dd6fSViresh Kumar 	if (!dev || !freq) {
5797813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
5807813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
5817813dd6fSViresh Kumar 	}
5827813dd6fSViresh Kumar 
5837813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
5847813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
5857813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
5867813dd6fSViresh Kumar 
5877813dd6fSViresh Kumar 	opp = _find_freq_ceil(opp_table, freq);
5887813dd6fSViresh Kumar 
5897813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
5907813dd6fSViresh Kumar 
5917813dd6fSViresh Kumar 	return opp;
5927813dd6fSViresh Kumar }
5937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
5947813dd6fSViresh Kumar 
5957813dd6fSViresh Kumar /**
5967813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
5977813dd6fSViresh Kumar  * @dev:	device for which we do this operation
5987813dd6fSViresh Kumar  * @freq:	Start frequency
5997813dd6fSViresh Kumar  *
6007813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
6017813dd6fSViresh Kumar  * for a device.
6027813dd6fSViresh Kumar  *
6037813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6047813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6057813dd6fSViresh Kumar  * values can be:
6067813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6077813dd6fSViresh Kumar  * ERANGE:	no match found for search
6087813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6097813dd6fSViresh Kumar  *
6107813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6117813dd6fSViresh Kumar  * use.
6127813dd6fSViresh Kumar  */
6137813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
6147813dd6fSViresh Kumar 					      unsigned long *freq)
6157813dd6fSViresh Kumar {
6167813dd6fSViresh Kumar 	struct opp_table *opp_table;
6177813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
6187813dd6fSViresh Kumar 
6197813dd6fSViresh Kumar 	if (!dev || !freq) {
6207813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
6217813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
6227813dd6fSViresh Kumar 	}
6237813dd6fSViresh Kumar 
6247813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
6257813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
6267813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
6277813dd6fSViresh Kumar 
6287813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
6297813dd6fSViresh Kumar 
6307813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
6317813dd6fSViresh Kumar 		if (temp_opp->available) {
6327813dd6fSViresh Kumar 			/* go to the next node, before choosing prev */
6337813dd6fSViresh Kumar 			if (temp_opp->rate > *freq)
6347813dd6fSViresh Kumar 				break;
6357813dd6fSViresh Kumar 			else
6367813dd6fSViresh Kumar 				opp = temp_opp;
6377813dd6fSViresh Kumar 		}
6387813dd6fSViresh Kumar 	}
6397813dd6fSViresh Kumar 
6407813dd6fSViresh Kumar 	/* Increment the reference count of OPP */
6417813dd6fSViresh Kumar 	if (!IS_ERR(opp))
6427813dd6fSViresh Kumar 		dev_pm_opp_get(opp);
6437813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
6447813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
6457813dd6fSViresh Kumar 
6467813dd6fSViresh Kumar 	if (!IS_ERR(opp))
6477813dd6fSViresh Kumar 		*freq = opp->rate;
6487813dd6fSViresh Kumar 
6497813dd6fSViresh Kumar 	return opp;
6507813dd6fSViresh Kumar }
6517813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
6527813dd6fSViresh Kumar 
6532f36bde0SAndrew-sh.Cheng /**
6542f36bde0SAndrew-sh.Cheng  * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
6552f36bde0SAndrew-sh.Cheng  *					 target voltage.
6562f36bde0SAndrew-sh.Cheng  * @dev:	Device for which we do this operation.
6572f36bde0SAndrew-sh.Cheng  * @u_volt:	Target voltage.
6582f36bde0SAndrew-sh.Cheng  *
6592f36bde0SAndrew-sh.Cheng  * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
6602f36bde0SAndrew-sh.Cheng  *
6612f36bde0SAndrew-sh.Cheng  * Return: matching *opp, else returns ERR_PTR in case of error which should be
6622f36bde0SAndrew-sh.Cheng  * handled using IS_ERR.
6632f36bde0SAndrew-sh.Cheng  *
6642f36bde0SAndrew-sh.Cheng  * Error return values can be:
6652f36bde0SAndrew-sh.Cheng  * EINVAL:	bad parameters
6662f36bde0SAndrew-sh.Cheng  *
6672f36bde0SAndrew-sh.Cheng  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6682f36bde0SAndrew-sh.Cheng  * use.
6692f36bde0SAndrew-sh.Cheng  */
6702f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
6712f36bde0SAndrew-sh.Cheng 						     unsigned long u_volt)
6722f36bde0SAndrew-sh.Cheng {
6732f36bde0SAndrew-sh.Cheng 	struct opp_table *opp_table;
6742f36bde0SAndrew-sh.Cheng 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
6752f36bde0SAndrew-sh.Cheng 
6762f36bde0SAndrew-sh.Cheng 	if (!dev || !u_volt) {
6772f36bde0SAndrew-sh.Cheng 		dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
6782f36bde0SAndrew-sh.Cheng 			u_volt);
6792f36bde0SAndrew-sh.Cheng 		return ERR_PTR(-EINVAL);
6802f36bde0SAndrew-sh.Cheng 	}
6812f36bde0SAndrew-sh.Cheng 
6822f36bde0SAndrew-sh.Cheng 	opp_table = _find_opp_table(dev);
6832f36bde0SAndrew-sh.Cheng 	if (IS_ERR(opp_table))
6842f36bde0SAndrew-sh.Cheng 		return ERR_CAST(opp_table);
6852f36bde0SAndrew-sh.Cheng 
6862f36bde0SAndrew-sh.Cheng 	mutex_lock(&opp_table->lock);
6872f36bde0SAndrew-sh.Cheng 
6882f36bde0SAndrew-sh.Cheng 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
6892f36bde0SAndrew-sh.Cheng 		if (temp_opp->available) {
6902f36bde0SAndrew-sh.Cheng 			if (temp_opp->supplies[0].u_volt > u_volt)
6912f36bde0SAndrew-sh.Cheng 				break;
6922f36bde0SAndrew-sh.Cheng 			opp = temp_opp;
6932f36bde0SAndrew-sh.Cheng 		}
6942f36bde0SAndrew-sh.Cheng 	}
6952f36bde0SAndrew-sh.Cheng 
6962f36bde0SAndrew-sh.Cheng 	/* Increment the reference count of OPP */
6972f36bde0SAndrew-sh.Cheng 	if (!IS_ERR(opp))
6982f36bde0SAndrew-sh.Cheng 		dev_pm_opp_get(opp);
6992f36bde0SAndrew-sh.Cheng 
7002f36bde0SAndrew-sh.Cheng 	mutex_unlock(&opp_table->lock);
7012f36bde0SAndrew-sh.Cheng 	dev_pm_opp_put_opp_table(opp_table);
7022f36bde0SAndrew-sh.Cheng 
7032f36bde0SAndrew-sh.Cheng 	return opp;
7042f36bde0SAndrew-sh.Cheng }
7052f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
7062f36bde0SAndrew-sh.Cheng 
7077813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
7087813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
7097813dd6fSViresh Kumar {
7107813dd6fSViresh Kumar 	int ret;
7117813dd6fSViresh Kumar 
7127813dd6fSViresh Kumar 	/* Regulator not available for device */
7137813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
7147813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
7157813dd6fSViresh Kumar 			PTR_ERR(reg));
7167813dd6fSViresh Kumar 		return 0;
7177813dd6fSViresh Kumar 	}
7187813dd6fSViresh Kumar 
7197813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
7207813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
7217813dd6fSViresh Kumar 
7227813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
7237813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
7247813dd6fSViresh Kumar 	if (ret)
7257813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
7267813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
7277813dd6fSViresh Kumar 			supply->u_volt_max, ret);
7287813dd6fSViresh Kumar 
7297813dd6fSViresh Kumar 	return ret;
7307813dd6fSViresh Kumar }
7317813dd6fSViresh Kumar 
732285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
733285881b5SViresh Kumar 					    unsigned long freq)
7347813dd6fSViresh Kumar {
7357813dd6fSViresh Kumar 	int ret;
7367813dd6fSViresh Kumar 
73735e74b2eSViresh Kumar 	/* We may reach here for devices which don't change frequency */
73835e74b2eSViresh Kumar 	if (IS_ERR(clk))
73935e74b2eSViresh Kumar 		return 0;
74035e74b2eSViresh Kumar 
7417813dd6fSViresh Kumar 	ret = clk_set_rate(clk, freq);
7427813dd6fSViresh Kumar 	if (ret) {
7437813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
7447813dd6fSViresh Kumar 			ret);
7457813dd6fSViresh Kumar 	}
7467813dd6fSViresh Kumar 
7477813dd6fSViresh Kumar 	return ret;
7487813dd6fSViresh Kumar }
7497813dd6fSViresh Kumar 
7508d45719cSKamil Konieczny static int _generic_set_opp_regulator(struct opp_table *opp_table,
7517813dd6fSViresh Kumar 				      struct device *dev,
7523f62670fSViresh Kumar 				      struct dev_pm_opp *opp,
7537813dd6fSViresh Kumar 				      unsigned long freq,
7543f62670fSViresh Kumar 				      int scaling_down)
7557813dd6fSViresh Kumar {
7567813dd6fSViresh Kumar 	struct regulator *reg = opp_table->regulators[0];
7573f62670fSViresh Kumar 	struct dev_pm_opp *old_opp = opp_table->current_opp;
7587813dd6fSViresh Kumar 	int ret;
7597813dd6fSViresh Kumar 
7607813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
7617813dd6fSViresh Kumar 	if (WARN_ON(opp_table->regulator_count > 1)) {
7627813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
7637813dd6fSViresh Kumar 		return -EINVAL;
7647813dd6fSViresh Kumar 	}
7657813dd6fSViresh Kumar 
7667813dd6fSViresh Kumar 	/* Scaling up? Scale voltage before frequency */
7673f62670fSViresh Kumar 	if (!scaling_down) {
7683f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
7697813dd6fSViresh Kumar 		if (ret)
7707813dd6fSViresh Kumar 			goto restore_voltage;
7717813dd6fSViresh Kumar 	}
7727813dd6fSViresh Kumar 
7737813dd6fSViresh Kumar 	/* Change frequency */
774285881b5SViresh Kumar 	ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
7757813dd6fSViresh Kumar 	if (ret)
7767813dd6fSViresh Kumar 		goto restore_voltage;
7777813dd6fSViresh Kumar 
7787813dd6fSViresh Kumar 	/* Scaling down? Scale voltage after frequency */
7793f62670fSViresh Kumar 	if (scaling_down) {
7803f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
7817813dd6fSViresh Kumar 		if (ret)
7827813dd6fSViresh Kumar 			goto restore_freq;
7837813dd6fSViresh Kumar 	}
7847813dd6fSViresh Kumar 
7858d45719cSKamil Konieczny 	/*
7868d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
7878d45719cSKamil Konieczny 	 * some boot-enabled regulators.
7888d45719cSKamil Konieczny 	 */
78972f80ce4SViresh Kumar 	if (unlikely(!opp_table->enabled)) {
7908d45719cSKamil Konieczny 		ret = regulator_enable(reg);
7918d45719cSKamil Konieczny 		if (ret < 0)
7928d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
7938d45719cSKamil Konieczny 	}
7948d45719cSKamil Konieczny 
7957813dd6fSViresh Kumar 	return 0;
7967813dd6fSViresh Kumar 
7977813dd6fSViresh Kumar restore_freq:
7983f62670fSViresh Kumar 	if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate))
7997813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
8003f62670fSViresh Kumar 			__func__, old_opp->rate);
8017813dd6fSViresh Kumar restore_voltage:
8027813dd6fSViresh Kumar 	/* This shouldn't harm even if the voltages weren't updated earlier */
8033f62670fSViresh Kumar 	_set_opp_voltage(dev, reg, old_opp->supplies);
8047813dd6fSViresh Kumar 
8057813dd6fSViresh Kumar 	return ret;
8067813dd6fSViresh Kumar }
8077813dd6fSViresh Kumar 
808b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
809240ae50eSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev)
810b00e667aSViresh Kumar {
811b00e667aSViresh Kumar 	u32 avg, peak;
812b00e667aSViresh Kumar 	int i, ret;
813b00e667aSViresh Kumar 
814b00e667aSViresh Kumar 	if (!opp_table->paths)
815b00e667aSViresh Kumar 		return 0;
816b00e667aSViresh Kumar 
817b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
818240ae50eSViresh Kumar 		if (!opp) {
819b00e667aSViresh Kumar 			avg = 0;
820b00e667aSViresh Kumar 			peak = 0;
821b00e667aSViresh Kumar 		} else {
822b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
823b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
824b00e667aSViresh Kumar 		}
825b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
826b00e667aSViresh Kumar 		if (ret) {
827b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
828240ae50eSViresh Kumar 				opp ? "set" : "remove", i, ret);
829b00e667aSViresh Kumar 			return ret;
830b00e667aSViresh Kumar 		}
831b00e667aSViresh Kumar 	}
832b00e667aSViresh Kumar 
833b00e667aSViresh Kumar 	return 0;
834b00e667aSViresh Kumar }
835b00e667aSViresh Kumar 
8367e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table,
837509e4777SViresh Kumar 			   struct device *dev, struct dev_pm_opp *opp,
838509e4777SViresh Kumar 			   unsigned long freq)
8397e535993SViresh Kumar {
84004b447dfSDmitry Osipenko 	struct dev_pm_set_opp_data *data = opp_table->set_opp_data;
841509e4777SViresh Kumar 	struct dev_pm_opp *old_opp = opp_table->current_opp;
8427e535993SViresh Kumar 	int size;
8437e535993SViresh Kumar 
84404b447dfSDmitry Osipenko 	/*
84504b447dfSDmitry Osipenko 	 * We support this only if dev_pm_opp_set_regulators() was called
84604b447dfSDmitry Osipenko 	 * earlier.
84704b447dfSDmitry Osipenko 	 */
84804b447dfSDmitry Osipenko 	if (opp_table->sod_supplies) {
849509e4777SViresh Kumar 		size = sizeof(*old_opp->supplies) * opp_table->regulator_count;
850509e4777SViresh Kumar 		memcpy(data->old_opp.supplies, old_opp->supplies, size);
851509e4777SViresh Kumar 		memcpy(data->new_opp.supplies, opp->supplies, size);
85204b447dfSDmitry Osipenko 		data->regulator_count = opp_table->regulator_count;
85304b447dfSDmitry Osipenko 	} else {
85404b447dfSDmitry Osipenko 		data->regulator_count = 0;
85504b447dfSDmitry Osipenko 	}
85604b447dfSDmitry Osipenko 
85704b447dfSDmitry Osipenko 	data->regulators = opp_table->regulators;
85804b447dfSDmitry Osipenko 	data->clk = opp_table->clk;
85904b447dfSDmitry Osipenko 	data->dev = dev;
860509e4777SViresh Kumar 	data->old_opp.rate = old_opp->rate;
86104b447dfSDmitry Osipenko 	data->new_opp.rate = freq;
8627e535993SViresh Kumar 
8637e535993SViresh Kumar 	return opp_table->set_opp(data);
8647e535993SViresh Kumar }
8657e535993SViresh Kumar 
86660cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev,
86760cdeae0SStephan Gerhold 			     struct dev_pm_opp *opp, int i)
86860cdeae0SStephan Gerhold {
86960cdeae0SStephan Gerhold 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
87060cdeae0SStephan Gerhold 	int ret;
87160cdeae0SStephan Gerhold 
87260cdeae0SStephan Gerhold 	if (!pd_dev)
87360cdeae0SStephan Gerhold 		return 0;
87460cdeae0SStephan Gerhold 
87560cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
87660cdeae0SStephan Gerhold 	if (ret) {
87760cdeae0SStephan Gerhold 		dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
87860cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
87960cdeae0SStephan Gerhold 	}
88060cdeae0SStephan Gerhold 
88160cdeae0SStephan Gerhold 	return ret;
88260cdeae0SStephan Gerhold }
88360cdeae0SStephan Gerhold 
884ca1b5d77SViresh Kumar /* This is only called for PM domain for now */
885ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev,
886ca1b5d77SViresh Kumar 			      struct opp_table *opp_table,
8872c59138cSStephan Gerhold 			      struct dev_pm_opp *opp, bool up)
888ca1b5d77SViresh Kumar {
889ca1b5d77SViresh Kumar 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
890ca1b5d77SViresh Kumar 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
891ca1b5d77SViresh Kumar 	int i, ret = 0;
892ca1b5d77SViresh Kumar 
893ca1b5d77SViresh Kumar 	if (!required_opp_tables)
894ca1b5d77SViresh Kumar 		return 0;
895ca1b5d77SViresh Kumar 
8967eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
8977eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
8987eba0c76SViresh Kumar 		return -EBUSY;
8997eba0c76SViresh Kumar 
900ca1b5d77SViresh Kumar 	/* Single genpd case */
90160cdeae0SStephan Gerhold 	if (!genpd_virt_devs)
90260cdeae0SStephan Gerhold 		return _set_required_opp(dev, dev, opp, 0);
903ca1b5d77SViresh Kumar 
904ca1b5d77SViresh Kumar 	/* Multiple genpd case */
905ca1b5d77SViresh Kumar 
906ca1b5d77SViresh Kumar 	/*
907ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
908ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
909ca1b5d77SViresh Kumar 	 */
910ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
911ca1b5d77SViresh Kumar 
9122c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
9132c59138cSStephan Gerhold 	if (up) {
914ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
91560cdeae0SStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
91660cdeae0SStephan Gerhold 			if (ret)
917ca1b5d77SViresh Kumar 				break;
918ca1b5d77SViresh Kumar 		}
9192c59138cSStephan Gerhold 	} else {
9202c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
9212c59138cSStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
9222c59138cSStephan Gerhold 			if (ret)
923ca1b5d77SViresh Kumar 				break;
924ca1b5d77SViresh Kumar 		}
925ca1b5d77SViresh Kumar 	}
9262c59138cSStephan Gerhold 
927ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
928ca1b5d77SViresh Kumar 
929ca1b5d77SViresh Kumar 	return ret;
930ca1b5d77SViresh Kumar }
931ca1b5d77SViresh Kumar 
93281c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
93381c4d8a3SViresh Kumar {
93481c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
93581c4d8a3SViresh Kumar 	unsigned long freq;
93681c4d8a3SViresh Kumar 
93781c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
93881c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
93981c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
94081c4d8a3SViresh Kumar 	}
94181c4d8a3SViresh Kumar 
94281c4d8a3SViresh Kumar 	/*
94381c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
94481c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
94581c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
94681c4d8a3SViresh Kumar 	 */
94781c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
94881c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
94981c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
95081c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
95181c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
95281c4d8a3SViresh Kumar 	}
95381c4d8a3SViresh Kumar 
95481c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
95581c4d8a3SViresh Kumar }
95681c4d8a3SViresh Kumar 
9575ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
958f3364e17SViresh Kumar {
959f3364e17SViresh Kumar 	int ret;
960f3364e17SViresh Kumar 
961f3364e17SViresh Kumar 	if (!opp_table->enabled)
962f3364e17SViresh Kumar 		return 0;
963f3364e17SViresh Kumar 
964f3364e17SViresh Kumar 	/*
965f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
966f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
967f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
968f3364e17SViresh Kumar 	 */
969f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
970f3364e17SViresh Kumar 		return 0;
971f3364e17SViresh Kumar 
972240ae50eSViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev);
973f3364e17SViresh Kumar 	if (ret)
974f3364e17SViresh Kumar 		return ret;
975f3364e17SViresh Kumar 
976f3364e17SViresh Kumar 	if (opp_table->regulators)
977f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
978f3364e17SViresh Kumar 
9792c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
980f3364e17SViresh Kumar 
981f3364e17SViresh Kumar 	opp_table->enabled = false;
982f3364e17SViresh Kumar 	return ret;
983f3364e17SViresh Kumar }
984f3364e17SViresh Kumar 
985386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
986386ba854SViresh Kumar 		    struct dev_pm_opp *opp, unsigned long freq)
9877813dd6fSViresh Kumar {
988386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
989f0b88fa4SViresh Kumar 	int scaling_down, ret;
9907813dd6fSViresh Kumar 
991386ba854SViresh Kumar 	if (unlikely(!opp))
992386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
993aca48b61SRajendra Nayak 
99481c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
99581c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
99681c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
9977813dd6fSViresh Kumar 
99881c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
99981c4d8a3SViresh Kumar 
100081c4d8a3SViresh Kumar 	/* Return early if nothing to do */
1001de04241aSJonathan Marek 	if (old_opp == opp && opp_table->current_rate == freq &&
1002de04241aSJonathan Marek 	    opp_table->enabled) {
100381c4d8a3SViresh Kumar 		dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
1004386ba854SViresh Kumar 		return 0;
10057813dd6fSViresh Kumar 	}
10067813dd6fSViresh Kumar 
1007f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1008de04241aSJonathan Marek 		__func__, opp_table->current_rate, freq, old_opp->level,
1009de04241aSJonathan Marek 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1010f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1011f0b88fa4SViresh Kumar 
1012f0b88fa4SViresh Kumar 	scaling_down = _opp_compare_key(old_opp, opp);
1013f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1014f0b88fa4SViresh Kumar 		scaling_down = 0;
10157813dd6fSViresh Kumar 
1016ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1017f0b88fa4SViresh Kumar 	if (!scaling_down) {
10182c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1019870d5d96SViresh Kumar 		if (ret) {
1020870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1021386ba854SViresh Kumar 			return ret;
1022ca1b5d77SViresh Kumar 		}
1023ca1b5d77SViresh Kumar 
1024870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1025870d5d96SViresh Kumar 		if (ret) {
1026870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1027870d5d96SViresh Kumar 			return ret;
1028870d5d96SViresh Kumar 		}
1029870d5d96SViresh Kumar 	}
1030870d5d96SViresh Kumar 
10317e535993SViresh Kumar 	if (opp_table->set_opp) {
1032509e4777SViresh Kumar 		ret = _set_opp_custom(opp_table, dev, opp, freq);
10337e535993SViresh Kumar 	} else if (opp_table->regulators) {
10343f62670fSViresh Kumar 		ret = _generic_set_opp_regulator(opp_table, dev, opp, freq,
10353f62670fSViresh Kumar 						 scaling_down);
10367813dd6fSViresh Kumar 	} else {
10377813dd6fSViresh Kumar 		/* Only frequency scaling */
10381d3c42caSViresh Kumar 		ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
10397813dd6fSViresh Kumar 	}
10407813dd6fSViresh Kumar 
1041ca1b5d77SViresh Kumar 	if (ret)
1042870d5d96SViresh Kumar 		return ret;
1043870d5d96SViresh Kumar 
1044870d5d96SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1045870d5d96SViresh Kumar 	if (scaling_down) {
1046870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1047870d5d96SViresh Kumar 		if (ret) {
1048870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1049870d5d96SViresh Kumar 			return ret;
1050ca1b5d77SViresh Kumar 		}
1051ca1b5d77SViresh Kumar 
1052870d5d96SViresh Kumar 		ret = _set_required_opps(dev, opp_table, opp, false);
1053870d5d96SViresh Kumar 		if (ret) {
1054870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1055870d5d96SViresh Kumar 			return ret;
1056870d5d96SViresh Kumar 		}
1057870d5d96SViresh Kumar 	}
1058870d5d96SViresh Kumar 
105972f80ce4SViresh Kumar 	opp_table->enabled = true;
106081c4d8a3SViresh Kumar 	dev_pm_opp_put(old_opp);
106181c4d8a3SViresh Kumar 
106281c4d8a3SViresh Kumar 	/* Make sure current_opp doesn't get freed */
106381c4d8a3SViresh Kumar 	dev_pm_opp_get(opp);
106481c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
1065de04241aSJonathan Marek 	opp_table->current_rate = freq;
1066fe2af402SGeorgi Djakov 
1067386ba854SViresh Kumar 	return ret;
1068386ba854SViresh Kumar }
1069386ba854SViresh Kumar 
1070386ba854SViresh Kumar /**
1071386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1072386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1073386ba854SViresh Kumar  * @target_freq: frequency to achieve
1074386ba854SViresh Kumar  *
1075386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1076386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1077386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1078386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1079386ba854SViresh Kumar  * frequency.
1080386ba854SViresh Kumar  */
1081386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1082386ba854SViresh Kumar {
1083386ba854SViresh Kumar 	struct opp_table *opp_table;
1084386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1085386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
1086386ba854SViresh Kumar 	int ret;
1087386ba854SViresh Kumar 
1088386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1089386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1090386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1091386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1092386ba854SViresh Kumar 	}
1093386ba854SViresh Kumar 
1094386ba854SViresh Kumar 	if (target_freq) {
1095386ba854SViresh Kumar 		/*
1096386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1097386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1098386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1099386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1100386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1101386ba854SViresh Kumar 		 */
1102386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
1103386ba854SViresh Kumar 			ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq);
1104386ba854SViresh Kumar 			goto put_opp_table;
1105386ba854SViresh Kumar 		}
1106386ba854SViresh Kumar 
1107386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1108386ba854SViresh Kumar 		if ((long)freq <= 0)
1109386ba854SViresh Kumar 			freq = target_freq;
1110386ba854SViresh Kumar 
1111386ba854SViresh Kumar 		/*
1112386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1113386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1114386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1115386ba854SViresh Kumar 		 */
1116386ba854SViresh Kumar 		temp_freq = freq;
1117386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1118386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1119386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1120386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1121386ba854SViresh Kumar 				__func__, freq, ret);
1122386ba854SViresh Kumar 			goto put_opp_table;
1123386ba854SViresh Kumar 		}
1124386ba854SViresh Kumar 	}
1125386ba854SViresh Kumar 
1126386ba854SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, freq);
1127386ba854SViresh Kumar 
1128386ba854SViresh Kumar 	if (target_freq)
11297813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
11307813dd6fSViresh Kumar put_opp_table:
11317813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
11327813dd6fSViresh Kumar 	return ret;
11337813dd6fSViresh Kumar }
11347813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
11357813dd6fSViresh Kumar 
1136abbe3483SViresh Kumar /**
1137abbe3483SViresh Kumar  * dev_pm_opp_set_opp() - Configure device for OPP
1138abbe3483SViresh Kumar  * @dev: device for which we do this operation
1139abbe3483SViresh Kumar  * @opp: OPP to set to
1140abbe3483SViresh Kumar  *
1141abbe3483SViresh Kumar  * This configures the device based on the properties of the OPP passed to this
1142abbe3483SViresh Kumar  * routine.
1143abbe3483SViresh Kumar  *
1144abbe3483SViresh Kumar  * Return: 0 on success, a negative error number otherwise.
1145abbe3483SViresh Kumar  */
1146abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1147abbe3483SViresh Kumar {
1148abbe3483SViresh Kumar 	struct opp_table *opp_table;
1149abbe3483SViresh Kumar 	int ret;
1150abbe3483SViresh Kumar 
1151abbe3483SViresh Kumar 	opp_table = _find_opp_table(dev);
1152abbe3483SViresh Kumar 	if (IS_ERR(opp_table)) {
1153abbe3483SViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1154abbe3483SViresh Kumar 		return PTR_ERR(opp_table);
1155abbe3483SViresh Kumar 	}
1156abbe3483SViresh Kumar 
1157abbe3483SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0);
1158abbe3483SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1159abbe3483SViresh Kumar 
1160abbe3483SViresh Kumar 	return ret;
1161abbe3483SViresh Kumar }
1162abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1163abbe3483SViresh Kumar 
11647813dd6fSViresh Kumar /* OPP-dev Helpers */
11657813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
11667813dd6fSViresh Kumar 			    struct opp_table *opp_table)
11677813dd6fSViresh Kumar {
11687813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
11697813dd6fSViresh Kumar 	list_del(&opp_dev->node);
11707813dd6fSViresh Kumar 	kfree(opp_dev);
11717813dd6fSViresh Kumar }
11727813dd6fSViresh Kumar 
1173ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
11747813dd6fSViresh Kumar 				struct opp_table *opp_table)
11757813dd6fSViresh Kumar {
11767813dd6fSViresh Kumar 	struct opp_device *opp_dev;
11777813dd6fSViresh Kumar 
11787813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
11797813dd6fSViresh Kumar 	if (!opp_dev)
11807813dd6fSViresh Kumar 		return NULL;
11817813dd6fSViresh Kumar 
11827813dd6fSViresh Kumar 	/* Initialize opp-dev */
11837813dd6fSViresh Kumar 	opp_dev->dev = dev;
11843d255699SViresh Kumar 
1185ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
11867813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1187ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
11887813dd6fSViresh Kumar 
11897813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1190a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1191283d55e6SViresh Kumar 
1192283d55e6SViresh Kumar 	return opp_dev;
1193283d55e6SViresh Kumar }
1194283d55e6SViresh Kumar 
1195eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
11967813dd6fSViresh Kumar {
11977813dd6fSViresh Kumar 	struct opp_table *opp_table;
11987813dd6fSViresh Kumar 	struct opp_device *opp_dev;
11997813dd6fSViresh Kumar 	int ret;
12007813dd6fSViresh Kumar 
12017813dd6fSViresh Kumar 	/*
12027813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
12037813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
12047813dd6fSViresh Kumar 	 */
12057813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
12067813dd6fSViresh Kumar 	if (!opp_table)
1207dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
12087813dd6fSViresh Kumar 
12093d255699SViresh Kumar 	mutex_init(&opp_table->lock);
12104f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
12117813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
12127eba0c76SViresh Kumar 	INIT_LIST_HEAD(&opp_table->lazy);
12137813dd6fSViresh Kumar 
121446f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
121546f48acaSViresh Kumar 	opp_table->regulator_count = -1;
121646f48acaSViresh Kumar 
12177813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
12187813dd6fSViresh Kumar 	if (!opp_dev) {
1219dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1220dd461cd9SStephan Gerhold 		goto err;
12217813dd6fSViresh Kumar 	}
12227813dd6fSViresh Kumar 
1223eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
12247813dd6fSViresh Kumar 
12256d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
12266d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1227dd461cd9SStephan Gerhold 	if (ret) {
1228dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
122932439ac7SViresh Kumar 			goto remove_opp_dev;
1230dd461cd9SStephan Gerhold 
12316d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
12326d3f922cSGeorgi Djakov 			 __func__, ret);
1233dd461cd9SStephan Gerhold 	}
12346d3f922cSGeorgi Djakov 
12357813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
12367813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
12377813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
12387813dd6fSViresh Kumar 
12397813dd6fSViresh Kumar 	return opp_table;
1240dd461cd9SStephan Gerhold 
1241976509bbSQuanyang Wang remove_opp_dev:
1242976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1243dd461cd9SStephan Gerhold err:
1244dd461cd9SStephan Gerhold 	kfree(opp_table);
1245dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
12467813dd6fSViresh Kumar }
12477813dd6fSViresh Kumar 
12487813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
12497813dd6fSViresh Kumar {
12507813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
12517813dd6fSViresh Kumar }
12527813dd6fSViresh Kumar 
125332439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
125432439ac7SViresh Kumar 					       struct opp_table *opp_table,
125532439ac7SViresh Kumar 					       bool getclk)
125632439ac7SViresh Kumar {
1257d4a4c7a4SViresh Kumar 	int ret;
1258d4a4c7a4SViresh Kumar 
125932439ac7SViresh Kumar 	/*
126032439ac7SViresh Kumar 	 * Return early if we don't need to get clk or we have already tried it
126132439ac7SViresh Kumar 	 * earlier.
126232439ac7SViresh Kumar 	 */
126332439ac7SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || opp_table->clk)
126432439ac7SViresh Kumar 		return opp_table;
126532439ac7SViresh Kumar 
126632439ac7SViresh Kumar 	/* Find clk for the device */
126732439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
126832439ac7SViresh Kumar 
1269d4a4c7a4SViresh Kumar 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
1270d4a4c7a4SViresh Kumar 	if (!ret)
127132439ac7SViresh Kumar 		return opp_table;
1272d4a4c7a4SViresh Kumar 
1273d4a4c7a4SViresh Kumar 	if (ret == -ENOENT) {
1274d4a4c7a4SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1275d4a4c7a4SViresh Kumar 		return opp_table;
1276d4a4c7a4SViresh Kumar 	}
1277d4a4c7a4SViresh Kumar 
1278d4a4c7a4SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1279d4a4c7a4SViresh Kumar 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1280d4a4c7a4SViresh Kumar 
1281d4a4c7a4SViresh Kumar 	return ERR_PTR(ret);
128232439ac7SViresh Kumar }
128332439ac7SViresh Kumar 
128427c09484SViresh Kumar /*
128527c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
128627c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
128727c09484SViresh Kumar  *
128827c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
128927c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
129027c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
129127c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
129227c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
129327c09484SViresh Kumar  * indirect users of OPP core.
129427c09484SViresh Kumar  *
129527c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
129627c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
129727c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
129827c09484SViresh Kumar  */
129932439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
130032439ac7SViresh Kumar 					 bool getclk)
13017813dd6fSViresh Kumar {
13027813dd6fSViresh Kumar 	struct opp_table *opp_table;
13037813dd6fSViresh Kumar 
130427c09484SViresh Kumar again:
13057813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
13067813dd6fSViresh Kumar 
13077813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
13087813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
13097813dd6fSViresh Kumar 		goto unlock;
13107813dd6fSViresh Kumar 
131127c09484SViresh Kumar 	/*
131227c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
131327c09484SViresh Kumar 	 * another user, wait for it to finish.
131427c09484SViresh Kumar 	 */
131527c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
131627c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
131727c09484SViresh Kumar 		cpu_relax();
131827c09484SViresh Kumar 		goto again;
131927c09484SViresh Kumar 	}
132027c09484SViresh Kumar 
132127c09484SViresh Kumar 	opp_tables_busy = true;
1322283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
132327c09484SViresh Kumar 
132427c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
132527c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
132627c09484SViresh Kumar 
1327283d55e6SViresh Kumar 	if (opp_table) {
1328ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1329283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1330dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1331283d55e6SViresh Kumar 		}
133227c09484SViresh Kumar 
133327c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
133427c09484SViresh Kumar 	} else {
133527c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
133627c09484SViresh Kumar 
133727c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
133827c09484SViresh Kumar 		if (!IS_ERR(opp_table))
133927c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1340283d55e6SViresh Kumar 	}
1341283d55e6SViresh Kumar 
134227c09484SViresh Kumar 	opp_tables_busy = false;
13437813dd6fSViresh Kumar 
13447813dd6fSViresh Kumar unlock:
13457813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
13467813dd6fSViresh Kumar 
134732439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
13487813dd6fSViresh Kumar }
1349eb7c8743SViresh Kumar 
135032439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1351e77dcb0bSViresh Kumar {
135232439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1353e77dcb0bSViresh Kumar }
1354e77dcb0bSViresh Kumar 
1355eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1356eb7c8743SViresh Kumar {
1357e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1358eb7c8743SViresh Kumar }
13597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
13607813dd6fSViresh Kumar 
13617813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
13627813dd6fSViresh Kumar {
13637813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1364cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
13656d3f922cSGeorgi Djakov 	int i;
13667813dd6fSViresh Kumar 
1367e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1368e0df59deSViresh Kumar 	list_del(&opp_table->node);
1369e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1370e0df59deSViresh Kumar 
137181c4d8a3SViresh Kumar 	if (opp_table->current_opp)
137281c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
137381c4d8a3SViresh Kumar 
13745d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
13755d6d106fSViresh Kumar 
13767813dd6fSViresh Kumar 	/* Release clk */
13777813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
13787813dd6fSViresh Kumar 		clk_put(opp_table->clk);
13797813dd6fSViresh Kumar 
13806d3f922cSGeorgi Djakov 	if (opp_table->paths) {
13816d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
13826d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
13836d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
13846d3f922cSGeorgi Djakov 	}
13856d3f922cSGeorgi Djakov 
1386cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1387cdd6ed90SViresh Kumar 
1388cdd6ed90SViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
13893d255699SViresh Kumar 		/*
1390cdd6ed90SViresh Kumar 		 * The OPP table is getting removed, drop the performance state
1391cdd6ed90SViresh Kumar 		 * constraints.
13923d255699SViresh Kumar 		 */
1393cdd6ed90SViresh Kumar 		if (opp_table->genpd_performance_state)
1394cdd6ed90SViresh Kumar 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
13957813dd6fSViresh Kumar 
13967813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
1397cdd6ed90SViresh Kumar 	}
13987813dd6fSViresh Kumar 
13994f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
14007813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
14017813dd6fSViresh Kumar 	kfree(opp_table);
14027813dd6fSViresh Kumar }
14037813dd6fSViresh Kumar 
14047813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
14057813dd6fSViresh Kumar {
14067813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
14077813dd6fSViresh Kumar 		       &opp_table_lock);
14087813dd6fSViresh Kumar }
14097813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
14107813dd6fSViresh Kumar 
14117813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
14127813dd6fSViresh Kumar {
14137813dd6fSViresh Kumar 	kfree(opp);
14147813dd6fSViresh Kumar }
14157813dd6fSViresh Kumar 
1416cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
14177813dd6fSViresh Kumar {
1418cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1419cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1420cf1fac94SViresh Kumar 
1421cf1fac94SViresh Kumar 	list_del(&opp->node);
1422cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1423cf1fac94SViresh Kumar 
14247813dd6fSViresh Kumar 	/*
14257813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
14267813dd6fSViresh Kumar 	 * frequency/voltage list.
14277813dd6fSViresh Kumar 	 */
14287813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1429da544b61SViresh Kumar 	_of_opp_free_required_opps(opp_table, opp);
14307813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
14317813dd6fSViresh Kumar 	kfree(opp);
14321690d8bbSViresh Kumar }
14337813dd6fSViresh Kumar 
1434a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
14357813dd6fSViresh Kumar {
14367813dd6fSViresh Kumar 	kref_get(&opp->kref);
14377813dd6fSViresh Kumar }
14387813dd6fSViresh Kumar 
14397813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
14407813dd6fSViresh Kumar {
1441cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
14427813dd6fSViresh Kumar }
14437813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
14447813dd6fSViresh Kumar 
14457813dd6fSViresh Kumar /**
14467813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
14477813dd6fSViresh Kumar  * @dev:	device for which we do this operation
14487813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
14497813dd6fSViresh Kumar  *
14507813dd6fSViresh Kumar  * This function removes an opp from the opp table.
14517813dd6fSViresh Kumar  */
14527813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
14537813dd6fSViresh Kumar {
14547813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
14557813dd6fSViresh Kumar 	struct opp_table *opp_table;
14567813dd6fSViresh Kumar 	bool found = false;
14577813dd6fSViresh Kumar 
14587813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
14597813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
14607813dd6fSViresh Kumar 		return;
14617813dd6fSViresh Kumar 
14627813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
14637813dd6fSViresh Kumar 
14647813dd6fSViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
14657813dd6fSViresh Kumar 		if (opp->rate == freq) {
14667813dd6fSViresh Kumar 			found = true;
14677813dd6fSViresh Kumar 			break;
14687813dd6fSViresh Kumar 		}
14697813dd6fSViresh Kumar 	}
14707813dd6fSViresh Kumar 
14717813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
14727813dd6fSViresh Kumar 
14737813dd6fSViresh Kumar 	if (found) {
14747813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
14750ad8c623SViresh Kumar 
14760ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
14770ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
14787813dd6fSViresh Kumar 	} else {
14797813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
14807813dd6fSViresh Kumar 			 __func__, freq);
14817813dd6fSViresh Kumar 	}
14827813dd6fSViresh Kumar 
14830ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
14847813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
14857813dd6fSViresh Kumar }
14867813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
14877813dd6fSViresh Kumar 
1488cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1489cf1fac94SViresh Kumar 					bool dynamic)
1490cf1fac94SViresh Kumar {
1491cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1492cf1fac94SViresh Kumar 
1493cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1494cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1495606a5d42SBeata Michalska 		/*
1496606a5d42SBeata Michalska 		 * Refcount must be dropped only once for each OPP by OPP core,
1497606a5d42SBeata Michalska 		 * do that with help of "removed" flag.
1498606a5d42SBeata Michalska 		 */
1499606a5d42SBeata Michalska 		if (!temp->removed && dynamic == temp->dynamic) {
1500cf1fac94SViresh Kumar 			opp = temp;
1501cf1fac94SViresh Kumar 			break;
1502cf1fac94SViresh Kumar 		}
1503cf1fac94SViresh Kumar 	}
1504cf1fac94SViresh Kumar 
1505cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1506cf1fac94SViresh Kumar 	return opp;
1507cf1fac94SViresh Kumar }
1508cf1fac94SViresh Kumar 
1509606a5d42SBeata Michalska /*
1510606a5d42SBeata Michalska  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1511606a5d42SBeata Michalska  * happen lock less to avoid circular dependency issues. This routine must be
1512606a5d42SBeata Michalska  * called without the opp_table->lock held.
1513606a5d42SBeata Michalska  */
1514606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
151503758d60SViresh Kumar {
1516cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
151703758d60SViresh Kumar 
1518606a5d42SBeata Michalska 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1519606a5d42SBeata Michalska 		opp->removed = true;
1520606a5d42SBeata Michalska 		dev_pm_opp_put(opp);
1521606a5d42SBeata Michalska 
1522606a5d42SBeata Michalska 		/* Drop the references taken by dev_pm_opp_add() */
1523606a5d42SBeata Michalska 		if (dynamic)
1524606a5d42SBeata Michalska 			dev_pm_opp_put_opp_table(opp_table);
1525606a5d42SBeata Michalska 	}
1526606a5d42SBeata Michalska }
1527606a5d42SBeata Michalska 
1528606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table)
1529606a5d42SBeata Michalska {
153003758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
153103758d60SViresh Kumar 
1532922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1533cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1534cf1fac94SViresh Kumar 		return false;
1535922ff075SViresh Kumar 	}
1536922ff075SViresh Kumar 
1537cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1538cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1539cf1fac94SViresh Kumar 		return true;
154003758d60SViresh Kumar 	}
154103758d60SViresh Kumar 
154203758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1543922ff075SViresh Kumar 
1544606a5d42SBeata Michalska 	_opp_remove_all(opp_table, false);
1545cf1fac94SViresh Kumar 	return true;
154603758d60SViresh Kumar }
154703758d60SViresh Kumar 
15481690d8bbSViresh Kumar /**
15491690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
15501690d8bbSViresh Kumar  * @dev:	device for which we do this operation
15511690d8bbSViresh Kumar  *
15521690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
15531690d8bbSViresh Kumar  */
15541690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
15551690d8bbSViresh Kumar {
15561690d8bbSViresh Kumar 	struct opp_table *opp_table;
15571690d8bbSViresh Kumar 
15581690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
15591690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
15601690d8bbSViresh Kumar 		return;
15611690d8bbSViresh Kumar 
1562606a5d42SBeata Michalska 	_opp_remove_all(opp_table, true);
15631690d8bbSViresh Kumar 
15641690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
15651690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
15661690d8bbSViresh Kumar }
15671690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
15681690d8bbSViresh Kumar 
15697813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table)
15707813dd6fSViresh Kumar {
15717813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
15726d3f922cSGeorgi Djakov 	int supply_count, supply_size, icc_size;
15737813dd6fSViresh Kumar 
15747813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
15756d3f922cSGeorgi Djakov 	supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
15766d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
15776d3f922cSGeorgi Djakov 	icc_size = sizeof(*opp->bandwidth) * table->path_count;
15787813dd6fSViresh Kumar 
15797813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
15806d3f922cSGeorgi Djakov 	opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
15816d3f922cSGeorgi Djakov 
15827813dd6fSViresh Kumar 	if (!opp)
15837813dd6fSViresh Kumar 		return NULL;
15847813dd6fSViresh Kumar 
15857813dd6fSViresh Kumar 	/* Put the supplies at the end of the OPP structure as an empty array */
15867813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
15876d3f922cSGeorgi Djakov 	if (icc_size)
15886d3f922cSGeorgi Djakov 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
15897813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
15907813dd6fSViresh Kumar 
15917813dd6fSViresh Kumar 	return opp;
15927813dd6fSViresh Kumar }
15937813dd6fSViresh Kumar 
15947813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
15957813dd6fSViresh Kumar 					 struct opp_table *opp_table)
15967813dd6fSViresh Kumar {
15977813dd6fSViresh Kumar 	struct regulator *reg;
15987813dd6fSViresh Kumar 	int i;
15997813dd6fSViresh Kumar 
160090e3577bSViresh Kumar 	if (!opp_table->regulators)
160190e3577bSViresh Kumar 		return true;
160290e3577bSViresh Kumar 
16037813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
16047813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
16057813dd6fSViresh Kumar 
16067813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
16077813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
16087813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
16097813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
16107813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
16117813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
16127813dd6fSViresh Kumar 			return false;
16137813dd6fSViresh Kumar 		}
16147813dd6fSViresh Kumar 	}
16157813dd6fSViresh Kumar 
16167813dd6fSViresh Kumar 	return true;
16177813dd6fSViresh Kumar }
16187813dd6fSViresh Kumar 
16196c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
16206c591eecSSaravana Kannan {
16216c591eecSSaravana Kannan 	if (opp1->rate != opp2->rate)
16226c591eecSSaravana Kannan 		return opp1->rate < opp2->rate ? -1 : 1;
16236d3f922cSGeorgi Djakov 	if (opp1->bandwidth && opp2->bandwidth &&
16246d3f922cSGeorgi Djakov 	    opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
16256d3f922cSGeorgi Djakov 		return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
16266c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
16276c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
16286c591eecSSaravana Kannan 	return 0;
16296c591eecSSaravana Kannan }
16306c591eecSSaravana Kannan 
1631a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1632a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1633a1e8c136SViresh Kumar 			     struct list_head **head)
1634a1e8c136SViresh Kumar {
1635a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
16366c591eecSSaravana Kannan 	int opp_cmp;
1637a1e8c136SViresh Kumar 
1638a1e8c136SViresh Kumar 	/*
1639a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1640a1e8c136SViresh Kumar 	 * already present.
1641a1e8c136SViresh Kumar 	 *
1642a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1643a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1644a1e8c136SViresh Kumar 	 * loop.
1645a1e8c136SViresh Kumar 	 */
1646a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
16476c591eecSSaravana Kannan 		opp_cmp = _opp_compare_key(new_opp, opp);
16486c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1649a1e8c136SViresh Kumar 			*head = &opp->node;
1650a1e8c136SViresh Kumar 			continue;
1651a1e8c136SViresh Kumar 		}
1652a1e8c136SViresh Kumar 
16536c591eecSSaravana Kannan 		if (opp_cmp < 0)
1654a1e8c136SViresh Kumar 			return 0;
1655a1e8c136SViresh Kumar 
1656a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1657a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1658a1e8c136SViresh Kumar 			 __func__, opp->rate, opp->supplies[0].u_volt,
1659a1e8c136SViresh Kumar 			 opp->available, new_opp->rate,
1660a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1661a1e8c136SViresh Kumar 
1662a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1663a1e8c136SViresh Kumar 		return opp->available &&
1664a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1665a1e8c136SViresh Kumar 	}
1666a1e8c136SViresh Kumar 
1667a1e8c136SViresh Kumar 	return 0;
1668a1e8c136SViresh Kumar }
1669a1e8c136SViresh Kumar 
16707eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count)
16717eba0c76SViresh Kumar {
16727eba0c76SViresh Kumar 	int i;
16737eba0c76SViresh Kumar 
16747eba0c76SViresh Kumar 	for (i = 0; i < count; i++) {
16757eba0c76SViresh Kumar 		if (opp->required_opps[i]->available)
16767eba0c76SViresh Kumar 			continue;
16777eba0c76SViresh Kumar 
16787eba0c76SViresh Kumar 		opp->available = false;
16797eba0c76SViresh Kumar 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
16807eba0c76SViresh Kumar 			 __func__, opp->required_opps[i]->np, opp->rate);
16817eba0c76SViresh Kumar 		return;
16827eba0c76SViresh Kumar 	}
16837eba0c76SViresh Kumar }
16847eba0c76SViresh Kumar 
16857813dd6fSViresh Kumar /*
16867813dd6fSViresh Kumar  * Returns:
16877813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
16887813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
16897813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
16907813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
16917813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
16927813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
16937813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
16947813dd6fSViresh Kumar  */
16957813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1696a1e8c136SViresh Kumar 	     struct opp_table *opp_table, bool rate_not_available)
16977813dd6fSViresh Kumar {
16987813dd6fSViresh Kumar 	struct list_head *head;
16997813dd6fSViresh Kumar 	int ret;
17007813dd6fSViresh Kumar 
17017813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
17027813dd6fSViresh Kumar 	head = &opp_table->opp_list;
17037813dd6fSViresh Kumar 
1704a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1705a1e8c136SViresh Kumar 	if (ret) {
17067813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
17077813dd6fSViresh Kumar 		return ret;
17087813dd6fSViresh Kumar 	}
17097813dd6fSViresh Kumar 
17107813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
17117813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
17127813dd6fSViresh Kumar 
17137813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
17147813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
17157813dd6fSViresh Kumar 
1716a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
17177813dd6fSViresh Kumar 
17187813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
17197813dd6fSViresh Kumar 		new_opp->available = false;
17207813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
17217813dd6fSViresh Kumar 			 __func__, new_opp->rate);
17227813dd6fSViresh Kumar 	}
17237813dd6fSViresh Kumar 
17247eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
17257eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
17267eba0c76SViresh Kumar 		return 0;
1727cf65948dSDmitry Osipenko 
17287eba0c76SViresh Kumar 	_required_opps_available(new_opp, opp_table->required_opp_count);
1729cf65948dSDmitry Osipenko 
17307813dd6fSViresh Kumar 	return 0;
17317813dd6fSViresh Kumar }
17327813dd6fSViresh Kumar 
17337813dd6fSViresh Kumar /**
17347813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
17357813dd6fSViresh Kumar  * @opp_table:	OPP table
17367813dd6fSViresh Kumar  * @dev:	device for which we do this operation
17377813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
17387813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
17397813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
17407813dd6fSViresh Kumar  *
17417813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
17427813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
17437813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
17447813dd6fSViresh Kumar  *
17457813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
17467813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
17477813dd6fSViresh Kumar  *
17487813dd6fSViresh Kumar  * Return:
17497813dd6fSViresh Kumar  * 0		On success OR
17507813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
17517813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
17527813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
17537813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
17547813dd6fSViresh Kumar  */
17557813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
17567813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
17577813dd6fSViresh Kumar {
17587813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
17597813dd6fSViresh Kumar 	unsigned long tol;
17607813dd6fSViresh Kumar 	int ret;
17617813dd6fSViresh Kumar 
17627813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
17637813dd6fSViresh Kumar 	if (!new_opp)
17647813dd6fSViresh Kumar 		return -ENOMEM;
17657813dd6fSViresh Kumar 
17667813dd6fSViresh Kumar 	/* populate the opp table */
17677813dd6fSViresh Kumar 	new_opp->rate = freq;
17687813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
17697813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
17707813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
17717813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
17727813dd6fSViresh Kumar 	new_opp->available = true;
17737813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
17747813dd6fSViresh Kumar 
1775a1e8c136SViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table, false);
17767813dd6fSViresh Kumar 	if (ret) {
17777813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
17787813dd6fSViresh Kumar 		if (ret == -EBUSY)
17797813dd6fSViresh Kumar 			ret = 0;
17807813dd6fSViresh Kumar 		goto free_opp;
17817813dd6fSViresh Kumar 	}
17827813dd6fSViresh Kumar 
17837813dd6fSViresh Kumar 	/*
17847813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
17857813dd6fSViresh Kumar 	 * frequency/voltage list.
17867813dd6fSViresh Kumar 	 */
17877813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
17887813dd6fSViresh Kumar 	return 0;
17897813dd6fSViresh Kumar 
17907813dd6fSViresh Kumar free_opp:
17917813dd6fSViresh Kumar 	_opp_free(new_opp);
17927813dd6fSViresh Kumar 
17937813dd6fSViresh Kumar 	return ret;
17947813dd6fSViresh Kumar }
17957813dd6fSViresh Kumar 
17967813dd6fSViresh Kumar /**
17977813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw() - Set supported platforms
17987813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
17997813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
18007813dd6fSViresh Kumar  * @count: Number of elements in the array.
18017813dd6fSViresh Kumar  *
18027813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
18037813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
18047813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
18057813dd6fSViresh Kumar  * property.
18067813dd6fSViresh Kumar  */
18077813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
18087813dd6fSViresh Kumar 			const u32 *versions, unsigned int count)
18097813dd6fSViresh Kumar {
18107813dd6fSViresh Kumar 	struct opp_table *opp_table;
18117813dd6fSViresh Kumar 
181232439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1813dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1814dd461cd9SStephan Gerhold 		return opp_table;
18157813dd6fSViresh Kumar 
18167813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
18177813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
18187813dd6fSViresh Kumar 
181925419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
182025419de1SViresh Kumar 	if (opp_table->supported_hw)
182125419de1SViresh Kumar 		return opp_table;
18227813dd6fSViresh Kumar 
18237813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
18247813dd6fSViresh Kumar 					GFP_KERNEL);
18257813dd6fSViresh Kumar 	if (!opp_table->supported_hw) {
182625419de1SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
182725419de1SViresh Kumar 		return ERR_PTR(-ENOMEM);
18287813dd6fSViresh Kumar 	}
18297813dd6fSViresh Kumar 
18307813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
18317813dd6fSViresh Kumar 
18327813dd6fSViresh Kumar 	return opp_table;
18337813dd6fSViresh Kumar }
18347813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
18357813dd6fSViresh Kumar 
18367813dd6fSViresh Kumar /**
18377813dd6fSViresh Kumar  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
18387813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
18397813dd6fSViresh Kumar  *
18407813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
18417813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
18427813dd6fSViresh Kumar  * will not be freed.
18437813dd6fSViresh Kumar  */
18447813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
18457813dd6fSViresh Kumar {
1846c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
1847c7bf8758SViresh Kumar 		return;
1848c7bf8758SViresh Kumar 
18497813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
18507813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
18517813dd6fSViresh Kumar 
18527813dd6fSViresh Kumar 	kfree(opp_table->supported_hw);
18537813dd6fSViresh Kumar 	opp_table->supported_hw = NULL;
18547813dd6fSViresh Kumar 	opp_table->supported_hw_count = 0;
18557813dd6fSViresh Kumar 
18567813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
18577813dd6fSViresh Kumar }
18587813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
18597813dd6fSViresh Kumar 
18607813dd6fSViresh Kumar /**
18617813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name() - Set prop-extn name
18627813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
18637813dd6fSViresh Kumar  * @name: name to postfix to properties.
18647813dd6fSViresh Kumar  *
18657813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
18667813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
18677813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
18687813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
18697813dd6fSViresh Kumar  */
18707813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
18717813dd6fSViresh Kumar {
18727813dd6fSViresh Kumar 	struct opp_table *opp_table;
18737813dd6fSViresh Kumar 
187432439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1875dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1876dd461cd9SStephan Gerhold 		return opp_table;
18777813dd6fSViresh Kumar 
18787813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
18797813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
18807813dd6fSViresh Kumar 
1881878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
1882878ec1a9SViresh Kumar 	if (opp_table->prop_name)
1883878ec1a9SViresh Kumar 		return opp_table;
18847813dd6fSViresh Kumar 
18857813dd6fSViresh Kumar 	opp_table->prop_name = kstrdup(name, GFP_KERNEL);
18867813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
1887878ec1a9SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
1888878ec1a9SViresh Kumar 		return ERR_PTR(-ENOMEM);
18897813dd6fSViresh Kumar 	}
18907813dd6fSViresh Kumar 
18917813dd6fSViresh Kumar 	return opp_table;
18927813dd6fSViresh Kumar }
18937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
18947813dd6fSViresh Kumar 
18957813dd6fSViresh Kumar /**
18967813dd6fSViresh Kumar  * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
18977813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
18987813dd6fSViresh Kumar  *
18997813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
19007813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
19017813dd6fSViresh Kumar  * will not be freed.
19027813dd6fSViresh Kumar  */
19037813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
19047813dd6fSViresh Kumar {
1905c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
1906c7bf8758SViresh Kumar 		return;
1907c7bf8758SViresh Kumar 
19087813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
19097813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
19107813dd6fSViresh Kumar 
19117813dd6fSViresh Kumar 	kfree(opp_table->prop_name);
19127813dd6fSViresh Kumar 	opp_table->prop_name = NULL;
19137813dd6fSViresh Kumar 
19147813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
19157813dd6fSViresh Kumar }
19167813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
19177813dd6fSViresh Kumar 
19187813dd6fSViresh Kumar /**
19197813dd6fSViresh Kumar  * dev_pm_opp_set_regulators() - Set regulator names for the device
19207813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
19217813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
19227813dd6fSViresh Kumar  * @count: Number of regulators.
19237813dd6fSViresh Kumar  *
19247813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
19257813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
19267813dd6fSViresh Kumar  * well.
19277813dd6fSViresh Kumar  *
19287813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
19297813dd6fSViresh Kumar  */
19307813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
19317813dd6fSViresh Kumar 					    const char * const names[],
19327813dd6fSViresh Kumar 					    unsigned int count)
19337813dd6fSViresh Kumar {
193438bb3439SViresh Kumar 	struct dev_pm_opp_supply *supplies;
19357813dd6fSViresh Kumar 	struct opp_table *opp_table;
19367813dd6fSViresh Kumar 	struct regulator *reg;
19377813dd6fSViresh Kumar 	int ret, i;
19387813dd6fSViresh Kumar 
193932439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1940dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1941dd461cd9SStephan Gerhold 		return opp_table;
19427813dd6fSViresh Kumar 
19437813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
19447813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
19457813dd6fSViresh Kumar 		ret = -EBUSY;
19467813dd6fSViresh Kumar 		goto err;
19477813dd6fSViresh Kumar 	}
19487813dd6fSViresh Kumar 
1949779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
1950779b783cSViresh Kumar 	if (opp_table->regulators)
1951779b783cSViresh Kumar 		return opp_table;
19527813dd6fSViresh Kumar 
19537813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
19547813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
19557813dd6fSViresh Kumar 					      GFP_KERNEL);
19567813dd6fSViresh Kumar 	if (!opp_table->regulators) {
19577813dd6fSViresh Kumar 		ret = -ENOMEM;
19587813dd6fSViresh Kumar 		goto err;
19597813dd6fSViresh Kumar 	}
19607813dd6fSViresh Kumar 
19617813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
19627813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
19637813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
19647813dd6fSViresh Kumar 			ret = PTR_ERR(reg);
19657813dd6fSViresh Kumar 			if (ret != -EPROBE_DEFER)
19667813dd6fSViresh Kumar 				dev_err(dev, "%s: no regulator (%s) found: %d\n",
19677813dd6fSViresh Kumar 					__func__, names[i], ret);
19687813dd6fSViresh Kumar 			goto free_regulators;
19697813dd6fSViresh Kumar 		}
19707813dd6fSViresh Kumar 
19717813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
19727813dd6fSViresh Kumar 	}
19737813dd6fSViresh Kumar 
19747813dd6fSViresh Kumar 	opp_table->regulator_count = count;
19757813dd6fSViresh Kumar 
197638bb3439SViresh Kumar 	supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
197738bb3439SViresh Kumar 	if (!supplies) {
197838bb3439SViresh Kumar 		ret = -ENOMEM;
19797813dd6fSViresh Kumar 		goto free_regulators;
198038bb3439SViresh Kumar 	}
198138bb3439SViresh Kumar 
198238bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
198338bb3439SViresh Kumar 	opp_table->sod_supplies = supplies;
198438bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
198538bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = supplies;
198638bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = supplies + count;
198738bb3439SViresh Kumar 	}
198838bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
19897813dd6fSViresh Kumar 
19907813dd6fSViresh Kumar 	return opp_table;
19917813dd6fSViresh Kumar 
19927813dd6fSViresh Kumar free_regulators:
199324957db1SMarek Szyprowski 	while (i != 0)
199424957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
19957813dd6fSViresh Kumar 
19967813dd6fSViresh Kumar 	kfree(opp_table->regulators);
19977813dd6fSViresh Kumar 	opp_table->regulators = NULL;
199846f48acaSViresh Kumar 	opp_table->regulator_count = -1;
19997813dd6fSViresh Kumar err:
20007813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
20017813dd6fSViresh Kumar 
20027813dd6fSViresh Kumar 	return ERR_PTR(ret);
20037813dd6fSViresh Kumar }
20047813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
20057813dd6fSViresh Kumar 
20067813dd6fSViresh Kumar /**
20077813dd6fSViresh Kumar  * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
20087813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
20097813dd6fSViresh Kumar  */
20107813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table)
20117813dd6fSViresh Kumar {
20127813dd6fSViresh Kumar 	int i;
20137813dd6fSViresh Kumar 
2014c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2015c7bf8758SViresh Kumar 		return;
2016c7bf8758SViresh Kumar 
2017779b783cSViresh Kumar 	if (!opp_table->regulators)
2018779b783cSViresh Kumar 		goto put_opp_table;
20197813dd6fSViresh Kumar 
20207813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
20217813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
20227813dd6fSViresh Kumar 
202372f80ce4SViresh Kumar 	if (opp_table->enabled) {
20248d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
20258d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
20268d45719cSKamil Konieczny 	}
20278d45719cSKamil Konieczny 
202824957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
20297813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
20307813dd6fSViresh Kumar 
203138bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
203238bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
203338bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = NULL;
203438bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = NULL;
203538bb3439SViresh Kumar 	}
203638bb3439SViresh Kumar 
203738bb3439SViresh Kumar 	kfree(opp_table->sod_supplies);
203838bb3439SViresh Kumar 	opp_table->sod_supplies = NULL;
203938bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
20407813dd6fSViresh Kumar 
20417813dd6fSViresh Kumar 	kfree(opp_table->regulators);
20427813dd6fSViresh Kumar 	opp_table->regulators = NULL;
204346f48acaSViresh Kumar 	opp_table->regulator_count = -1;
20447813dd6fSViresh Kumar 
2045779b783cSViresh Kumar put_opp_table:
20467813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
20477813dd6fSViresh Kumar }
20487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
20497813dd6fSViresh Kumar 
20507813dd6fSViresh Kumar /**
20517813dd6fSViresh Kumar  * dev_pm_opp_set_clkname() - Set clk name for the device
20527813dd6fSViresh Kumar  * @dev: Device for which clk name is being set.
20537813dd6fSViresh Kumar  * @name: Clk name.
20547813dd6fSViresh Kumar  *
20557813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointer to the
20567813dd6fSViresh Kumar  * clock for the device. Simple cases work fine without using this routine (i.e.
20577813dd6fSViresh Kumar  * by passing connection-id as NULL), but for a device with multiple clocks
20587813dd6fSViresh Kumar  * available, the OPP core needs to know the exact name of the clk to use.
20597813dd6fSViresh Kumar  *
20607813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
20617813dd6fSViresh Kumar  */
20627813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
20637813dd6fSViresh Kumar {
20647813dd6fSViresh Kumar 	struct opp_table *opp_table;
20657813dd6fSViresh Kumar 	int ret;
20667813dd6fSViresh Kumar 
206732439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2068dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2069dd461cd9SStephan Gerhold 		return opp_table;
20707813dd6fSViresh Kumar 
20717813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
20727813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
20737813dd6fSViresh Kumar 		ret = -EBUSY;
20747813dd6fSViresh Kumar 		goto err;
20757813dd6fSViresh Kumar 	}
20767813dd6fSViresh Kumar 
207732439ac7SViresh Kumar 	/* clk shouldn't be initialized at this point */
207832439ac7SViresh Kumar 	if (WARN_ON(opp_table->clk)) {
207932439ac7SViresh Kumar 		ret = -EBUSY;
208032439ac7SViresh Kumar 		goto err;
208132439ac7SViresh Kumar 	}
20827813dd6fSViresh Kumar 
20837813dd6fSViresh Kumar 	/* Find clk for the device */
20847813dd6fSViresh Kumar 	opp_table->clk = clk_get(dev, name);
20857813dd6fSViresh Kumar 	if (IS_ERR(opp_table->clk)) {
20867813dd6fSViresh Kumar 		ret = PTR_ERR(opp_table->clk);
20877813dd6fSViresh Kumar 		if (ret != -EPROBE_DEFER) {
20887813dd6fSViresh Kumar 			dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
20897813dd6fSViresh Kumar 				ret);
20907813dd6fSViresh Kumar 		}
20917813dd6fSViresh Kumar 		goto err;
20927813dd6fSViresh Kumar 	}
20937813dd6fSViresh Kumar 
20947813dd6fSViresh Kumar 	return opp_table;
20957813dd6fSViresh Kumar 
20967813dd6fSViresh Kumar err:
20977813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
20987813dd6fSViresh Kumar 
20997813dd6fSViresh Kumar 	return ERR_PTR(ret);
21007813dd6fSViresh Kumar }
21017813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
21027813dd6fSViresh Kumar 
21037813dd6fSViresh Kumar /**
21047813dd6fSViresh Kumar  * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
21057813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
21067813dd6fSViresh Kumar  */
21077813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table)
21087813dd6fSViresh Kumar {
2109c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2110c7bf8758SViresh Kumar 		return;
2111c7bf8758SViresh Kumar 
21127813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
21137813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
21147813dd6fSViresh Kumar 
21157813dd6fSViresh Kumar 	clk_put(opp_table->clk);
21167813dd6fSViresh Kumar 	opp_table->clk = ERR_PTR(-EINVAL);
21177813dd6fSViresh Kumar 
21187813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
21197813dd6fSViresh Kumar }
21207813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
21217813dd6fSViresh Kumar 
2122*a74f681cSYangtao Li static void devm_pm_opp_clkname_release(void *data)
2123*a74f681cSYangtao Li {
2124*a74f681cSYangtao Li 	dev_pm_opp_put_clkname(data);
2125*a74f681cSYangtao Li }
2126*a74f681cSYangtao Li 
2127*a74f681cSYangtao Li /**
2128*a74f681cSYangtao Li  * devm_pm_opp_set_clkname() - Set clk name for the device
2129*a74f681cSYangtao Li  * @dev: Device for which clk name is being set.
2130*a74f681cSYangtao Li  * @name: Clk name.
2131*a74f681cSYangtao Li  *
2132*a74f681cSYangtao Li  * This is a resource-managed variant of dev_pm_opp_set_clkname().
2133*a74f681cSYangtao Li  *
2134*a74f681cSYangtao Li  * Return: 0 on success and errorno otherwise.
2135*a74f681cSYangtao Li  */
2136*a74f681cSYangtao Li int devm_pm_opp_set_clkname(struct device *dev, const char *name)
2137*a74f681cSYangtao Li {
2138*a74f681cSYangtao Li 	struct opp_table *opp_table;
2139*a74f681cSYangtao Li 
2140*a74f681cSYangtao Li 	opp_table = dev_pm_opp_set_clkname(dev, name);
2141*a74f681cSYangtao Li 	if (IS_ERR(opp_table))
2142*a74f681cSYangtao Li 		return PTR_ERR(opp_table);
2143*a74f681cSYangtao Li 
2144*a74f681cSYangtao Li 	return devm_add_action_or_reset(dev, devm_pm_opp_clkname_release,
2145*a74f681cSYangtao Li 					opp_table);
2146*a74f681cSYangtao Li }
2147*a74f681cSYangtao Li EXPORT_SYMBOL_GPL(devm_pm_opp_set_clkname);
2148*a74f681cSYangtao Li 
21497813dd6fSViresh Kumar /**
21507813dd6fSViresh Kumar  * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
21517813dd6fSViresh Kumar  * @dev: Device for which the helper is getting registered.
21527813dd6fSViresh Kumar  * @set_opp: Custom set OPP helper.
21537813dd6fSViresh Kumar  *
21547813dd6fSViresh Kumar  * This is useful to support complex platforms (like platforms with multiple
21557813dd6fSViresh Kumar  * regulators per device), instead of the generic OPP set rate helper.
21567813dd6fSViresh Kumar  *
21577813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
21587813dd6fSViresh Kumar  */
21597813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
21607813dd6fSViresh Kumar 			int (*set_opp)(struct dev_pm_set_opp_data *data))
21617813dd6fSViresh Kumar {
216238bb3439SViresh Kumar 	struct dev_pm_set_opp_data *data;
21637813dd6fSViresh Kumar 	struct opp_table *opp_table;
21647813dd6fSViresh Kumar 
21657813dd6fSViresh Kumar 	if (!set_opp)
21667813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
21677813dd6fSViresh Kumar 
216832439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
216947efcbcbSViresh Kumar 	if (IS_ERR(opp_table))
2170dd461cd9SStephan Gerhold 		return opp_table;
21717813dd6fSViresh Kumar 
21727813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
21737813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
21745019acc6SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
21755019acc6SViresh Kumar 		return ERR_PTR(-EBUSY);
21767813dd6fSViresh Kumar 	}
21777813dd6fSViresh Kumar 
21785019acc6SViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
217938bb3439SViresh Kumar 	if (opp_table->set_opp)
218038bb3439SViresh Kumar 		return opp_table;
218138bb3439SViresh Kumar 
218238bb3439SViresh Kumar 	data = kzalloc(sizeof(*data), GFP_KERNEL);
218338bb3439SViresh Kumar 	if (!data)
218438bb3439SViresh Kumar 		return ERR_PTR(-ENOMEM);
218538bb3439SViresh Kumar 
218638bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
218738bb3439SViresh Kumar 	opp_table->set_opp_data = data;
218838bb3439SViresh Kumar 	if (opp_table->sod_supplies) {
218938bb3439SViresh Kumar 		data->old_opp.supplies = opp_table->sod_supplies;
219038bb3439SViresh Kumar 		data->new_opp.supplies = opp_table->sod_supplies +
219138bb3439SViresh Kumar 					 opp_table->regulator_count;
219238bb3439SViresh Kumar 	}
219338bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
219438bb3439SViresh Kumar 
21957813dd6fSViresh Kumar 	opp_table->set_opp = set_opp;
21967813dd6fSViresh Kumar 
21977813dd6fSViresh Kumar 	return opp_table;
21987813dd6fSViresh Kumar }
21997813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
22007813dd6fSViresh Kumar 
22017813dd6fSViresh Kumar /**
2202604a7aebSViresh Kumar  * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
22037813dd6fSViresh Kumar  *					   set_opp helper
22047813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
22057813dd6fSViresh Kumar  *
22067813dd6fSViresh Kumar  * Release resources blocked for platform specific set_opp helper.
22077813dd6fSViresh Kumar  */
2208604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
22097813dd6fSViresh Kumar {
2210c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2211c7bf8758SViresh Kumar 		return;
2212c7bf8758SViresh Kumar 
22137813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
22147813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
22157813dd6fSViresh Kumar 
22167813dd6fSViresh Kumar 	opp_table->set_opp = NULL;
221738bb3439SViresh Kumar 
221838bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
221938bb3439SViresh Kumar 	kfree(opp_table->set_opp_data);
222038bb3439SViresh Kumar 	opp_table->set_opp_data = NULL;
222138bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
222238bb3439SViresh Kumar 
22237813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
22247813dd6fSViresh Kumar }
2225604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
22267813dd6fSViresh Kumar 
2227a3c47af6SDmitry Osipenko static void devm_pm_opp_unregister_set_opp_helper(void *data)
2228a3c47af6SDmitry Osipenko {
2229a3c47af6SDmitry Osipenko 	dev_pm_opp_unregister_set_opp_helper(data);
2230a3c47af6SDmitry Osipenko }
2231a3c47af6SDmitry Osipenko 
2232a3c47af6SDmitry Osipenko /**
2233a3c47af6SDmitry Osipenko  * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2234a3c47af6SDmitry Osipenko  * @dev: Device for which the helper is getting registered.
2235a3c47af6SDmitry Osipenko  * @set_opp: Custom set OPP helper.
2236a3c47af6SDmitry Osipenko  *
2237a3c47af6SDmitry Osipenko  * This is a resource-managed version of dev_pm_opp_register_set_opp_helper().
2238a3c47af6SDmitry Osipenko  *
2239a3c47af6SDmitry Osipenko  * Return: pointer to 'struct opp_table' on success and errorno otherwise.
2240a3c47af6SDmitry Osipenko  */
2241a3c47af6SDmitry Osipenko struct opp_table *
2242a3c47af6SDmitry Osipenko devm_pm_opp_register_set_opp_helper(struct device *dev,
2243a3c47af6SDmitry Osipenko 				    int (*set_opp)(struct dev_pm_set_opp_data *data))
2244a3c47af6SDmitry Osipenko {
2245a3c47af6SDmitry Osipenko 	struct opp_table *opp_table;
2246a3c47af6SDmitry Osipenko 	int err;
2247a3c47af6SDmitry Osipenko 
2248a3c47af6SDmitry Osipenko 	opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp);
2249a3c47af6SDmitry Osipenko 	if (IS_ERR(opp_table))
2250a3c47af6SDmitry Osipenko 		return opp_table;
2251a3c47af6SDmitry Osipenko 
2252a3c47af6SDmitry Osipenko 	err = devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper,
2253a3c47af6SDmitry Osipenko 				       opp_table);
2254a3c47af6SDmitry Osipenko 	if (err)
2255a3c47af6SDmitry Osipenko 		return ERR_PTR(err);
2256a3c47af6SDmitry Osipenko 
2257a3c47af6SDmitry Osipenko 	return opp_table;
2258a3c47af6SDmitry Osipenko }
2259a3c47af6SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper);
2260a3c47af6SDmitry Osipenko 
22616319aee1SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
22626319aee1SViresh Kumar {
22636319aee1SViresh Kumar 	int index;
22646319aee1SViresh Kumar 
2265cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2266cb60e960SViresh Kumar 		return;
2267cb60e960SViresh Kumar 
22686319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
22696319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
22706319aee1SViresh Kumar 			continue;
22716319aee1SViresh Kumar 
22726319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
22736319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
22746319aee1SViresh Kumar 	}
2275c0ab9e08SViresh Kumar 
2276c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2277c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
22786319aee1SViresh Kumar }
22796319aee1SViresh Kumar 
22807813dd6fSViresh Kumar /**
22816319aee1SViresh Kumar  * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
22826319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
22836319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
228417a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
22854f018bc0SViresh Kumar  *
22864f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
22874f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
22884f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
22894f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
22906319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
22916319aee1SViresh Kumar  * we don't need to support that separately.
22924f018bc0SViresh Kumar  *
22934f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
22946319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
22954f018bc0SViresh Kumar  *
22966319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
22976319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2298baea35e4SViresh Kumar  *
2299baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2300baea35e4SViresh Kumar  * "required-opps" are added in DT.
23014f018bc0SViresh Kumar  */
230217a8f868SViresh Kumar struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
230317a8f868SViresh Kumar 		const char **names, struct device ***virt_devs)
23044f018bc0SViresh Kumar {
23054f018bc0SViresh Kumar 	struct opp_table *opp_table;
23066319aee1SViresh Kumar 	struct device *virt_dev;
2307baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
23086319aee1SViresh Kumar 	const char **name = names;
23094f018bc0SViresh Kumar 
231032439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2311dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2312dd461cd9SStephan Gerhold 		return opp_table;
23134f018bc0SViresh Kumar 
2314cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2315cb60e960SViresh Kumar 		return opp_table;
23164f018bc0SViresh Kumar 
23176319aee1SViresh Kumar 	/*
23186319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
23196319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
23206319aee1SViresh Kumar 	 * table is added.
23216319aee1SViresh Kumar 	 */
23226319aee1SViresh Kumar 	if (!opp_table->required_opp_count) {
23236319aee1SViresh Kumar 		ret = -EPROBE_DEFER;
23246319aee1SViresh Kumar 		goto put_table;
23256319aee1SViresh Kumar 	}
23266319aee1SViresh Kumar 
23274f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
23284f018bc0SViresh Kumar 
2329c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2330c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2331c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2332c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2333c0ab9e08SViresh Kumar 		goto unlock;
23344f018bc0SViresh Kumar 
23356319aee1SViresh Kumar 	while (*name) {
23366319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
23376319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
23386319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
23396319aee1SViresh Kumar 			goto err;
23406319aee1SViresh Kumar 		}
23414f018bc0SViresh Kumar 
23426319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
23436319aee1SViresh Kumar 		if (IS_ERR(virt_dev)) {
23446319aee1SViresh Kumar 			ret = PTR_ERR(virt_dev);
23456319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
23466319aee1SViresh Kumar 			goto err;
23474f018bc0SViresh Kumar 		}
23484f018bc0SViresh Kumar 
23494f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2350baea35e4SViresh Kumar 		index++;
23516319aee1SViresh Kumar 		name++;
23526319aee1SViresh Kumar 	}
23536319aee1SViresh Kumar 
235417a8f868SViresh Kumar 	if (virt_devs)
235517a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
23564f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23574f018bc0SViresh Kumar 
23584f018bc0SViresh Kumar 	return opp_table;
23596319aee1SViresh Kumar 
23606319aee1SViresh Kumar err:
23616319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
2362c0ab9e08SViresh Kumar unlock:
23636319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23646319aee1SViresh Kumar 
23656319aee1SViresh Kumar put_table:
23666319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
23676319aee1SViresh Kumar 
23686319aee1SViresh Kumar 	return ERR_PTR(ret);
23694f018bc0SViresh Kumar }
23706319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
23714f018bc0SViresh Kumar 
23724f018bc0SViresh Kumar /**
23736319aee1SViresh Kumar  * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
23746319aee1SViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
23754f018bc0SViresh Kumar  *
23766319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
23776319aee1SViresh Kumar  * OPP table.
23784f018bc0SViresh Kumar  */
23796319aee1SViresh Kumar void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
23804f018bc0SViresh Kumar {
2381c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2382c7bf8758SViresh Kumar 		return;
2383c7bf8758SViresh Kumar 
23844f018bc0SViresh Kumar 	/*
23854f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
23864f018bc0SViresh Kumar 	 * used in parallel.
23874f018bc0SViresh Kumar 	 */
23884f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
23896319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
23904f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23914f018bc0SViresh Kumar 
23926319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
23934f018bc0SViresh Kumar }
23946319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
23954f018bc0SViresh Kumar 
2396b4b9e223SDmitry Osipenko static void devm_pm_opp_detach_genpd(void *data)
2397b4b9e223SDmitry Osipenko {
2398b4b9e223SDmitry Osipenko 	dev_pm_opp_detach_genpd(data);
2399b4b9e223SDmitry Osipenko }
2400b4b9e223SDmitry Osipenko 
2401b4b9e223SDmitry Osipenko /**
2402b4b9e223SDmitry Osipenko  * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual
2403b4b9e223SDmitry Osipenko  *			      device pointer
2404b4b9e223SDmitry Osipenko  * @dev: Consumer device for which the genpd is getting attached.
2405b4b9e223SDmitry Osipenko  * @names: Null terminated array of pointers containing names of genpd to attach.
2406b4b9e223SDmitry Osipenko  * @virt_devs: Pointer to return the array of virtual devices.
2407b4b9e223SDmitry Osipenko  *
2408b4b9e223SDmitry Osipenko  * This is a resource-managed version of dev_pm_opp_attach_genpd().
2409b4b9e223SDmitry Osipenko  *
2410b4b9e223SDmitry Osipenko  * Return: pointer to 'struct opp_table' on success and errorno otherwise.
2411b4b9e223SDmitry Osipenko  */
2412b4b9e223SDmitry Osipenko struct opp_table *
2413b4b9e223SDmitry Osipenko devm_pm_opp_attach_genpd(struct device *dev, const char **names,
2414b4b9e223SDmitry Osipenko 			 struct device ***virt_devs)
2415b4b9e223SDmitry Osipenko {
2416b4b9e223SDmitry Osipenko 	struct opp_table *opp_table;
2417b4b9e223SDmitry Osipenko 	int err;
2418b4b9e223SDmitry Osipenko 
2419b4b9e223SDmitry Osipenko 	opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs);
2420b4b9e223SDmitry Osipenko 	if (IS_ERR(opp_table))
2421b4b9e223SDmitry Osipenko 		return opp_table;
2422b4b9e223SDmitry Osipenko 
2423b4b9e223SDmitry Osipenko 	err = devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd,
2424b4b9e223SDmitry Osipenko 				       opp_table);
2425b4b9e223SDmitry Osipenko 	if (err)
2426b4b9e223SDmitry Osipenko 		return ERR_PTR(err);
2427b4b9e223SDmitry Osipenko 
2428b4b9e223SDmitry Osipenko 	return opp_table;
2429b4b9e223SDmitry Osipenko }
2430b4b9e223SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd);
2431b4b9e223SDmitry Osipenko 
24324f018bc0SViresh Kumar /**
24337d8658efSSaravana Kannan  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
24347d8658efSSaravana Kannan  * @src_table: OPP table which has @dst_table as one of its required OPP table.
24357d8658efSSaravana Kannan  * @dst_table: Required OPP table of the @src_table.
24367d8658efSSaravana Kannan  * @src_opp: OPP from the @src_table.
24377d8658efSSaravana Kannan  *
24387d8658efSSaravana Kannan  * This function returns the OPP (present in @dst_table) pointed out by the
24397d8658efSSaravana Kannan  * "required-opps" property of the @src_opp (present in @src_table).
24407d8658efSSaravana Kannan  *
24417d8658efSSaravana Kannan  * The callers are required to call dev_pm_opp_put() for the returned OPP after
24427d8658efSSaravana Kannan  * use.
24437d8658efSSaravana Kannan  *
24447d8658efSSaravana Kannan  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
24457d8658efSSaravana Kannan  */
24467d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
24477d8658efSSaravana Kannan 						 struct opp_table *dst_table,
24487d8658efSSaravana Kannan 						 struct dev_pm_opp *src_opp)
24497d8658efSSaravana Kannan {
24507d8658efSSaravana Kannan 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
24517d8658efSSaravana Kannan 	int i;
24527d8658efSSaravana Kannan 
24537d8658efSSaravana Kannan 	if (!src_table || !dst_table || !src_opp ||
24547d8658efSSaravana Kannan 	    !src_table->required_opp_tables)
24557d8658efSSaravana Kannan 		return ERR_PTR(-EINVAL);
24567d8658efSSaravana Kannan 
24577d8658efSSaravana Kannan 	/* required-opps not fully initialized yet */
24587d8658efSSaravana Kannan 	if (lazy_linking_pending(src_table))
24597d8658efSSaravana Kannan 		return ERR_PTR(-EBUSY);
24607d8658efSSaravana Kannan 
24617d8658efSSaravana Kannan 	for (i = 0; i < src_table->required_opp_count; i++) {
24627d8658efSSaravana Kannan 		if (src_table->required_opp_tables[i] == dst_table) {
24637d8658efSSaravana Kannan 			mutex_lock(&src_table->lock);
24647d8658efSSaravana Kannan 
24657d8658efSSaravana Kannan 			list_for_each_entry(opp, &src_table->opp_list, node) {
24667d8658efSSaravana Kannan 				if (opp == src_opp) {
24677d8658efSSaravana Kannan 					dest_opp = opp->required_opps[i];
24687d8658efSSaravana Kannan 					dev_pm_opp_get(dest_opp);
24697d8658efSSaravana Kannan 					break;
24707d8658efSSaravana Kannan 				}
24717d8658efSSaravana Kannan 			}
24727d8658efSSaravana Kannan 
24737d8658efSSaravana Kannan 			mutex_unlock(&src_table->lock);
24747d8658efSSaravana Kannan 			break;
24757d8658efSSaravana Kannan 		}
24767d8658efSSaravana Kannan 	}
24777d8658efSSaravana Kannan 
24787d8658efSSaravana Kannan 	if (IS_ERR(dest_opp)) {
24797d8658efSSaravana Kannan 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
24807d8658efSSaravana Kannan 		       src_table, dst_table);
24817d8658efSSaravana Kannan 	}
24827d8658efSSaravana Kannan 
24837d8658efSSaravana Kannan 	return dest_opp;
24847d8658efSSaravana Kannan }
24857d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
24867d8658efSSaravana Kannan 
24877d8658efSSaravana Kannan /**
2488c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2489c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2490c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2491c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2492c8a59103SViresh Kumar  *
2493c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2494c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2495c8a59103SViresh Kumar  * performance state set to @pstate.
2496c8a59103SViresh Kumar  *
2497c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2498c8a59103SViresh Kumar  * value on errors.
2499c8a59103SViresh Kumar  */
2500c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2501c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2502c8a59103SViresh Kumar 				       unsigned int pstate)
2503c8a59103SViresh Kumar {
2504c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2505c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2506c8a59103SViresh Kumar 	int i;
2507c8a59103SViresh Kumar 
2508c8a59103SViresh Kumar 	/*
2509c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2510c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2511c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2512c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2513c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2514c8a59103SViresh Kumar 	 */
2515f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2516c8a59103SViresh Kumar 		return pstate;
2517c8a59103SViresh Kumar 
25187eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
25197eba0c76SViresh Kumar 	if (lazy_linking_pending(src_table))
25207eba0c76SViresh Kumar 		return -EBUSY;
25217eba0c76SViresh Kumar 
2522c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2523c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2524c8a59103SViresh Kumar 			break;
2525c8a59103SViresh Kumar 	}
2526c8a59103SViresh Kumar 
2527c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2528c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2529c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2530c8a59103SViresh Kumar 		return -EINVAL;
2531c8a59103SViresh Kumar 	}
2532c8a59103SViresh Kumar 
2533c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2534c8a59103SViresh Kumar 
2535c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
2536c8a59103SViresh Kumar 		if (opp->pstate == pstate) {
2537c8a59103SViresh Kumar 			dest_pstate = opp->required_opps[i]->pstate;
2538c8a59103SViresh Kumar 			goto unlock;
2539c8a59103SViresh Kumar 		}
2540c8a59103SViresh Kumar 	}
2541c8a59103SViresh Kumar 
2542c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2543c8a59103SViresh Kumar 	       dst_table);
2544c8a59103SViresh Kumar 
2545c8a59103SViresh Kumar unlock:
2546c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2547c8a59103SViresh Kumar 
2548c8a59103SViresh Kumar 	return dest_pstate;
2549c8a59103SViresh Kumar }
2550c8a59103SViresh Kumar 
2551c8a59103SViresh Kumar /**
25527813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
25537813dd6fSViresh Kumar  * @dev:	device for which we do this operation
25547813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
25557813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
25567813dd6fSViresh Kumar  *
25577813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
25587813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
25597813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
25607813dd6fSViresh Kumar  *
25617813dd6fSViresh Kumar  * Return:
25627813dd6fSViresh Kumar  * 0		On success OR
25637813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
25647813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
25657813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
25667813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
25677813dd6fSViresh Kumar  */
25687813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
25697813dd6fSViresh Kumar {
25707813dd6fSViresh Kumar 	struct opp_table *opp_table;
25717813dd6fSViresh Kumar 	int ret;
25727813dd6fSViresh Kumar 
257332439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2574dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2575dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
25767813dd6fSViresh Kumar 
257746f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
257846f48acaSViresh Kumar 	opp_table->regulator_count = 1;
257946f48acaSViresh Kumar 
25807813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
25810ad8c623SViresh Kumar 	if (ret)
25827813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
25830ad8c623SViresh Kumar 
25847813dd6fSViresh Kumar 	return ret;
25857813dd6fSViresh Kumar }
25867813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
25877813dd6fSViresh Kumar 
25887813dd6fSViresh Kumar /**
25897813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
25907813dd6fSViresh Kumar  * @dev:		device for which we do this operation
25917813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
25927813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
25937813dd6fSViresh Kumar  *
25947813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
25957813dd6fSViresh Kumar  * which is isolated here.
25967813dd6fSViresh Kumar  *
25977813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
25987813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
25997813dd6fSViresh Kumar  * successful.
26007813dd6fSViresh Kumar  */
26017813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
26027813dd6fSViresh Kumar 				 bool availability_req)
26037813dd6fSViresh Kumar {
26047813dd6fSViresh Kumar 	struct opp_table *opp_table;
26057813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
26067813dd6fSViresh Kumar 	int r = 0;
26077813dd6fSViresh Kumar 
26087813dd6fSViresh Kumar 	/* Find the opp_table */
26097813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
26107813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
26117813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
26127813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
26137813dd6fSViresh Kumar 		return r;
26147813dd6fSViresh Kumar 	}
26157813dd6fSViresh Kumar 
26167813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
26177813dd6fSViresh Kumar 
26187813dd6fSViresh Kumar 	/* Do we have the frequency? */
26197813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
26207813dd6fSViresh Kumar 		if (tmp_opp->rate == freq) {
26217813dd6fSViresh Kumar 			opp = tmp_opp;
26227813dd6fSViresh Kumar 			break;
26237813dd6fSViresh Kumar 		}
26247813dd6fSViresh Kumar 	}
26257813dd6fSViresh Kumar 
26267813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
26277813dd6fSViresh Kumar 		r = PTR_ERR(opp);
26287813dd6fSViresh Kumar 		goto unlock;
26297813dd6fSViresh Kumar 	}
26307813dd6fSViresh Kumar 
26317813dd6fSViresh Kumar 	/* Is update really needed? */
26327813dd6fSViresh Kumar 	if (opp->available == availability_req)
26337813dd6fSViresh Kumar 		goto unlock;
26347813dd6fSViresh Kumar 
26357813dd6fSViresh Kumar 	opp->available = availability_req;
26367813dd6fSViresh Kumar 
26377813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
26387813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
26397813dd6fSViresh Kumar 
26407813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
26417813dd6fSViresh Kumar 	if (availability_req)
26427813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
26437813dd6fSViresh Kumar 					     opp);
26447813dd6fSViresh Kumar 	else
26457813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
26467813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
26477813dd6fSViresh Kumar 
26487813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
26497813dd6fSViresh Kumar 	goto put_table;
26507813dd6fSViresh Kumar 
26517813dd6fSViresh Kumar unlock:
26527813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
26537813dd6fSViresh Kumar put_table:
26547813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
26557813dd6fSViresh Kumar 	return r;
26567813dd6fSViresh Kumar }
26577813dd6fSViresh Kumar 
26587813dd6fSViresh Kumar /**
265925cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
266025cb20a2SStephen Boyd  * @dev:		device for which we do this operation
266125cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
266225cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
266325cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
266425cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
266525cb20a2SStephen Boyd  *
266625cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
266725cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
266825cb20a2SStephen Boyd  * successful.
266925cb20a2SStephen Boyd  */
267025cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
267125cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
267225cb20a2SStephen Boyd 			      unsigned long u_volt_max)
267325cb20a2SStephen Boyd 
267425cb20a2SStephen Boyd {
267525cb20a2SStephen Boyd 	struct opp_table *opp_table;
267625cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
267725cb20a2SStephen Boyd 	int r = 0;
267825cb20a2SStephen Boyd 
267925cb20a2SStephen Boyd 	/* Find the opp_table */
268025cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
268125cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
268225cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
268325cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
268425cb20a2SStephen Boyd 		return r;
268525cb20a2SStephen Boyd 	}
268625cb20a2SStephen Boyd 
268725cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
268825cb20a2SStephen Boyd 
268925cb20a2SStephen Boyd 	/* Do we have the frequency? */
269025cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
269125cb20a2SStephen Boyd 		if (tmp_opp->rate == freq) {
269225cb20a2SStephen Boyd 			opp = tmp_opp;
269325cb20a2SStephen Boyd 			break;
269425cb20a2SStephen Boyd 		}
269525cb20a2SStephen Boyd 	}
269625cb20a2SStephen Boyd 
269725cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
269825cb20a2SStephen Boyd 		r = PTR_ERR(opp);
269925cb20a2SStephen Boyd 		goto adjust_unlock;
270025cb20a2SStephen Boyd 	}
270125cb20a2SStephen Boyd 
270225cb20a2SStephen Boyd 	/* Is update really needed? */
270325cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
270425cb20a2SStephen Boyd 		goto adjust_unlock;
270525cb20a2SStephen Boyd 
270625cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
270725cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
270825cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
270925cb20a2SStephen Boyd 
271025cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
271125cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
271225cb20a2SStephen Boyd 
271325cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
271425cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
271525cb20a2SStephen Boyd 				     opp);
271625cb20a2SStephen Boyd 
271725cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
271825cb20a2SStephen Boyd 	goto adjust_put_table;
271925cb20a2SStephen Boyd 
272025cb20a2SStephen Boyd adjust_unlock:
272125cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
272225cb20a2SStephen Boyd adjust_put_table:
272325cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
272425cb20a2SStephen Boyd 	return r;
272525cb20a2SStephen Boyd }
272603649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
272725cb20a2SStephen Boyd 
272825cb20a2SStephen Boyd /**
27297813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
27307813dd6fSViresh Kumar  * @dev:	device for which we do this operation
27317813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
27327813dd6fSViresh Kumar  *
27337813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
27347813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
27357813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
27367813dd6fSViresh Kumar  *
27377813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
27387813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
27397813dd6fSViresh Kumar  * successful.
27407813dd6fSViresh Kumar  */
27417813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
27427813dd6fSViresh Kumar {
27437813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
27447813dd6fSViresh Kumar }
27457813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
27467813dd6fSViresh Kumar 
27477813dd6fSViresh Kumar /**
27487813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
27497813dd6fSViresh Kumar  * @dev:	device for which we do this operation
27507813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
27517813dd6fSViresh Kumar  *
27527813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
27537813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
27547813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
27557813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
27567813dd6fSViresh Kumar  *
27577813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
27587813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
27597813dd6fSViresh Kumar  * successful.
27607813dd6fSViresh Kumar  */
27617813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
27627813dd6fSViresh Kumar {
27637813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
27647813dd6fSViresh Kumar }
27657813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
27667813dd6fSViresh Kumar 
27677813dd6fSViresh Kumar /**
27687813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
27697813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
27707813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
27717813dd6fSViresh Kumar  *
27727813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
27737813dd6fSViresh Kumar  */
27747813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
27757813dd6fSViresh Kumar {
27767813dd6fSViresh Kumar 	struct opp_table *opp_table;
27777813dd6fSViresh Kumar 	int ret;
27787813dd6fSViresh Kumar 
27797813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
27807813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
27817813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
27827813dd6fSViresh Kumar 
27837813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
27847813dd6fSViresh Kumar 
27857813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
27867813dd6fSViresh Kumar 
27877813dd6fSViresh Kumar 	return ret;
27887813dd6fSViresh Kumar }
27897813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
27907813dd6fSViresh Kumar 
27917813dd6fSViresh Kumar /**
27927813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
27937813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
27947813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
27957813dd6fSViresh Kumar  *
27967813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
27977813dd6fSViresh Kumar  */
27987813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
27997813dd6fSViresh Kumar 				   struct notifier_block *nb)
28007813dd6fSViresh Kumar {
28017813dd6fSViresh Kumar 	struct opp_table *opp_table;
28027813dd6fSViresh Kumar 	int ret;
28037813dd6fSViresh Kumar 
28047813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
28057813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
28067813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
28077813dd6fSViresh Kumar 
28087813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
28097813dd6fSViresh Kumar 
28107813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
28117813dd6fSViresh Kumar 
28127813dd6fSViresh Kumar 	return ret;
28137813dd6fSViresh Kumar }
28147813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
28157813dd6fSViresh Kumar 
28168aaf6264SViresh Kumar /**
28178aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
28188aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
28198aaf6264SViresh Kumar  *
28208aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
28218aaf6264SViresh Kumar  * dynamically added entries.
28228aaf6264SViresh Kumar  */
28238aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
28247813dd6fSViresh Kumar {
28257813dd6fSViresh Kumar 	struct opp_table *opp_table;
28267813dd6fSViresh Kumar 
28277813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
28287813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
28297813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
28307813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
28317813dd6fSViresh Kumar 
28327813dd6fSViresh Kumar 		if (error != -ENODEV)
28337813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
28347813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
28357813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
28367813dd6fSViresh Kumar 			     error);
28377813dd6fSViresh Kumar 		return;
28387813dd6fSViresh Kumar 	}
28397813dd6fSViresh Kumar 
2840922ff075SViresh Kumar 	/*
2841922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
2842922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
2843922ff075SViresh Kumar 	 **/
2844922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
2845cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
2846cdd6ed90SViresh Kumar 
2847922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
28487813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
28497813dd6fSViresh Kumar }
28507813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
2851ce8073d8SDmitry Osipenko 
2852ce8073d8SDmitry Osipenko /**
2853ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2854ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
2855ce8073d8SDmitry Osipenko  *
2856ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
2857ce8073d8SDmitry Osipenko  *
2858ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
2859ce8073d8SDmitry Osipenko  */
2860ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
2861ce8073d8SDmitry Osipenko {
2862ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
2863ce8073d8SDmitry Osipenko 	struct regulator *reg;
2864ce8073d8SDmitry Osipenko 	int i, ret = 0;
2865ce8073d8SDmitry Osipenko 
2866ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
2867ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
2868ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
2869ce8073d8SDmitry Osipenko 		return 0;
2870ce8073d8SDmitry Osipenko 
2871ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
2872ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
2873ce8073d8SDmitry Osipenko 		goto put_table;
2874ce8073d8SDmitry Osipenko 
2875ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
2876ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
2877ce8073d8SDmitry Osipenko 		goto put_table;
2878ce8073d8SDmitry Osipenko 
2879ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
2880ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
2881ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
2882ce8073d8SDmitry Osipenko 		if (ret)
2883ce8073d8SDmitry Osipenko 			break;
2884ce8073d8SDmitry Osipenko 	}
2885ce8073d8SDmitry Osipenko put_table:
2886ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
2887ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
2888ce8073d8SDmitry Osipenko 
2889ce8073d8SDmitry Osipenko 	return ret;
2890ce8073d8SDmitry Osipenko }
2891ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
2892