xref: /openbmc/linux/drivers/opp/core.c (revision 35e74b2ee8ec64da6f8067c5b0744f16ff19915b)
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);
307813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
317813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
3227c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */
3327c09484SViresh Kumar static bool opp_tables_busy;
347813dd6fSViresh Kumar 
359e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
367813dd6fSViresh Kumar {
377813dd6fSViresh Kumar 	struct opp_device *opp_dev;
389e62edacSViresh Kumar 	bool found = false;
397813dd6fSViresh Kumar 
409e62edacSViresh Kumar 	mutex_lock(&opp_table->lock);
417813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
429e62edacSViresh Kumar 		if (opp_dev->dev == dev) {
439e62edacSViresh Kumar 			found = true;
449e62edacSViresh Kumar 			break;
459e62edacSViresh Kumar 		}
467813dd6fSViresh Kumar 
479e62edacSViresh Kumar 	mutex_unlock(&opp_table->lock);
489e62edacSViresh Kumar 	return found;
497813dd6fSViresh Kumar }
507813dd6fSViresh Kumar 
517813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
527813dd6fSViresh Kumar {
537813dd6fSViresh Kumar 	struct opp_table *opp_table;
547813dd6fSViresh Kumar 
557813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
569e62edacSViresh Kumar 		if (_find_opp_dev(dev, opp_table)) {
577813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
587813dd6fSViresh Kumar 			return opp_table;
597813dd6fSViresh Kumar 		}
607813dd6fSViresh Kumar 	}
617813dd6fSViresh Kumar 
627813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
637813dd6fSViresh Kumar }
647813dd6fSViresh Kumar 
657813dd6fSViresh Kumar /**
667813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
677813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
687813dd6fSViresh Kumar  *
697813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
707813dd6fSViresh Kumar  *
717813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
727813dd6fSViresh Kumar  * -EINVAL based on type of error.
737813dd6fSViresh Kumar  *
747813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
757813dd6fSViresh Kumar  */
767813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
777813dd6fSViresh Kumar {
787813dd6fSViresh Kumar 	struct opp_table *opp_table;
797813dd6fSViresh Kumar 
807813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
817813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
827813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
837813dd6fSViresh Kumar 	}
847813dd6fSViresh Kumar 
857813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
867813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
877813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
887813dd6fSViresh Kumar 
897813dd6fSViresh Kumar 	return opp_table;
907813dd6fSViresh Kumar }
917813dd6fSViresh Kumar 
927813dd6fSViresh Kumar /**
937813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
947813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
957813dd6fSViresh Kumar  *
967813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
977813dd6fSViresh Kumar  * return 0
987813dd6fSViresh Kumar  *
997813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1007813dd6fSViresh Kumar  */
1017813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1027813dd6fSViresh Kumar {
1037813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1047813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1057813dd6fSViresh Kumar 		return 0;
1067813dd6fSViresh Kumar 	}
1077813dd6fSViresh Kumar 
1087813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1097813dd6fSViresh Kumar }
1107813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1117813dd6fSViresh Kumar 
1127813dd6fSViresh Kumar /**
1137813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1147813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1157813dd6fSViresh Kumar  *
1167813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1177813dd6fSViresh Kumar  * return 0
1187813dd6fSViresh Kumar  */
1197813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1207813dd6fSViresh Kumar {
12106a8a059SAndrew-sh.Cheng 	if (IS_ERR_OR_NULL(opp)) {
1227813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1237813dd6fSViresh Kumar 		return 0;
1247813dd6fSViresh Kumar 	}
1257813dd6fSViresh Kumar 
1267813dd6fSViresh Kumar 	return opp->rate;
1277813dd6fSViresh Kumar }
1287813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
1297813dd6fSViresh Kumar 
1307813dd6fSViresh Kumar /**
1315b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
1325b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
1335b93ac54SRajendra Nayak  *
1345b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
1355b93ac54SRajendra Nayak  * return 0.
1365b93ac54SRajendra Nayak  */
1375b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
1385b93ac54SRajendra Nayak {
1395b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1405b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
1415b93ac54SRajendra Nayak 		return 0;
1425b93ac54SRajendra Nayak 	}
1435b93ac54SRajendra Nayak 
1445b93ac54SRajendra Nayak 	return opp->level;
1455b93ac54SRajendra Nayak }
1465b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
1475b93ac54SRajendra Nayak 
1485b93ac54SRajendra Nayak /**
149597ff543SDmitry Osipenko  * dev_pm_opp_get_required_pstate() - Gets the required performance state
150597ff543SDmitry Osipenko  *                                    corresponding to an available opp
151597ff543SDmitry Osipenko  * @opp:	opp for which performance state has to be returned for
152597ff543SDmitry Osipenko  * @index:	index of the required opp
153597ff543SDmitry Osipenko  *
154597ff543SDmitry Osipenko  * Return: performance state read from device tree corresponding to the
155597ff543SDmitry Osipenko  * required opp, else return 0.
156597ff543SDmitry Osipenko  */
157597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
158597ff543SDmitry Osipenko 					    unsigned int index)
159597ff543SDmitry Osipenko {
160597ff543SDmitry Osipenko 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
161597ff543SDmitry Osipenko 	    index >= opp->opp_table->required_opp_count) {
162597ff543SDmitry Osipenko 		pr_err("%s: Invalid parameters\n", __func__);
163597ff543SDmitry Osipenko 		return 0;
164597ff543SDmitry Osipenko 	}
165597ff543SDmitry Osipenko 
166597ff543SDmitry Osipenko 	return opp->required_opps[index]->pstate;
167597ff543SDmitry Osipenko }
168597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
169597ff543SDmitry Osipenko 
170597ff543SDmitry Osipenko /**
1717813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
1727813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
1737813dd6fSViresh Kumar  *
1747813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
1757813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
1767813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
1777813dd6fSViresh Kumar  *
1787813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
1797813dd6fSViresh Kumar  */
1807813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
1817813dd6fSViresh Kumar {
1827813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1837813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1847813dd6fSViresh Kumar 		return false;
1857813dd6fSViresh Kumar 	}
1867813dd6fSViresh Kumar 
1877813dd6fSViresh Kumar 	return opp->turbo;
1887813dd6fSViresh Kumar }
1897813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
1907813dd6fSViresh Kumar 
1917813dd6fSViresh Kumar /**
1927813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
1937813dd6fSViresh Kumar  * @dev:	device for which we do this operation
1947813dd6fSViresh Kumar  *
1957813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
1967813dd6fSViresh Kumar  */
1977813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
1987813dd6fSViresh Kumar {
1997813dd6fSViresh Kumar 	struct opp_table *opp_table;
2007813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
2017813dd6fSViresh Kumar 
2027813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2037813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2047813dd6fSViresh Kumar 		return 0;
2057813dd6fSViresh Kumar 
2067813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
2077813dd6fSViresh Kumar 
2087813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2097813dd6fSViresh Kumar 
2107813dd6fSViresh Kumar 	return clock_latency_ns;
2117813dd6fSViresh Kumar }
2127813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
2137813dd6fSViresh Kumar 
2147813dd6fSViresh Kumar /**
2157813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
2167813dd6fSViresh Kumar  * @dev: device for which we do this operation
2177813dd6fSViresh Kumar  *
2187813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
2197813dd6fSViresh Kumar  */
2207813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
2217813dd6fSViresh Kumar {
2227813dd6fSViresh Kumar 	struct opp_table *opp_table;
2237813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
2247813dd6fSViresh Kumar 	struct regulator *reg;
2257813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
2267813dd6fSViresh Kumar 	int ret, i, count;
2277813dd6fSViresh Kumar 	struct {
2287813dd6fSViresh Kumar 		unsigned long min;
2297813dd6fSViresh Kumar 		unsigned long max;
2307813dd6fSViresh Kumar 	} *uV;
2317813dd6fSViresh Kumar 
2327813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2337813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2347813dd6fSViresh Kumar 		return 0;
2357813dd6fSViresh Kumar 
2367813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
23790e3577bSViresh Kumar 	if (!opp_table->regulators)
2387813dd6fSViresh Kumar 		goto put_opp_table;
2397813dd6fSViresh Kumar 
24090e3577bSViresh Kumar 	count = opp_table->regulator_count;
24190e3577bSViresh Kumar 
2427813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
2437813dd6fSViresh Kumar 	if (!uV)
2447813dd6fSViresh Kumar 		goto put_opp_table;
2457813dd6fSViresh Kumar 
2467813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
2477813dd6fSViresh Kumar 
2487813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2497813dd6fSViresh Kumar 		uV[i].min = ~0;
2507813dd6fSViresh Kumar 		uV[i].max = 0;
2517813dd6fSViresh Kumar 
2527813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
2537813dd6fSViresh Kumar 			if (!opp->available)
2547813dd6fSViresh Kumar 				continue;
2557813dd6fSViresh Kumar 
2567813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
2577813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
2587813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
2597813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
2607813dd6fSViresh Kumar 		}
2617813dd6fSViresh Kumar 	}
2627813dd6fSViresh Kumar 
2637813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
2647813dd6fSViresh Kumar 
2657813dd6fSViresh Kumar 	/*
2667813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
2677813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
2687813dd6fSViresh Kumar 	 */
2697813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2707813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
2717813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
2727813dd6fSViresh Kumar 		if (ret > 0)
2737813dd6fSViresh Kumar 			latency_ns += ret * 1000;
2747813dd6fSViresh Kumar 	}
2757813dd6fSViresh Kumar 
2767813dd6fSViresh Kumar 	kfree(uV);
2777813dd6fSViresh Kumar put_opp_table:
2787813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2797813dd6fSViresh Kumar 
2807813dd6fSViresh Kumar 	return latency_ns;
2817813dd6fSViresh Kumar }
2827813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
2837813dd6fSViresh Kumar 
2847813dd6fSViresh Kumar /**
2857813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
2867813dd6fSViresh Kumar  *					     nanoseconds
2877813dd6fSViresh Kumar  * @dev: device for which we do this operation
2887813dd6fSViresh Kumar  *
2897813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
2907813dd6fSViresh Kumar  * switch from one OPP to other.
2917813dd6fSViresh Kumar  */
2927813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
2937813dd6fSViresh Kumar {
2947813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
2957813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
2967813dd6fSViresh Kumar }
2977813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
2987813dd6fSViresh Kumar 
2997813dd6fSViresh Kumar /**
3007813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
3017813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3027813dd6fSViresh Kumar  *
3037813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
3047813dd6fSViresh Kumar  * if one is available, else returns 0;
3057813dd6fSViresh Kumar  */
3067813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
3077813dd6fSViresh Kumar {
3087813dd6fSViresh Kumar 	struct opp_table *opp_table;
3097813dd6fSViresh Kumar 	unsigned long freq = 0;
3107813dd6fSViresh Kumar 
3117813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3127813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3137813dd6fSViresh Kumar 		return 0;
3147813dd6fSViresh Kumar 
3157813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
3167813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
3177813dd6fSViresh Kumar 
3187813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3197813dd6fSViresh Kumar 
3207813dd6fSViresh Kumar 	return freq;
3217813dd6fSViresh Kumar }
3227813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
3237813dd6fSViresh Kumar 
324a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
325a1e8c136SViresh Kumar {
326a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
327a1e8c136SViresh Kumar 	int count = 0;
328a1e8c136SViresh Kumar 
329a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
330a1e8c136SViresh Kumar 
331a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
332a1e8c136SViresh Kumar 		if (opp->available)
333a1e8c136SViresh Kumar 			count++;
334a1e8c136SViresh Kumar 	}
335a1e8c136SViresh Kumar 
336a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
337a1e8c136SViresh Kumar 
338a1e8c136SViresh Kumar 	return count;
339a1e8c136SViresh Kumar }
340a1e8c136SViresh Kumar 
3417813dd6fSViresh Kumar /**
3427813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
3437813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3447813dd6fSViresh Kumar  *
3457813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
3467813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
3477813dd6fSViresh Kumar  */
3487813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
3497813dd6fSViresh Kumar {
3507813dd6fSViresh Kumar 	struct opp_table *opp_table;
351a1e8c136SViresh Kumar 	int count;
3527813dd6fSViresh Kumar 
3537813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3547813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
3557813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
356035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
3577813dd6fSViresh Kumar 			__func__, count);
35809f662f9SViresh Kumar 		return count;
3597813dd6fSViresh Kumar 	}
3607813dd6fSViresh Kumar 
361a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
3627813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3637813dd6fSViresh Kumar 
3647813dd6fSViresh Kumar 	return count;
3657813dd6fSViresh Kumar }
3667813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
3677813dd6fSViresh Kumar 
3687813dd6fSViresh Kumar /**
3697813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
3707813dd6fSViresh Kumar  * @dev:		device for which we do this operation
3717813dd6fSViresh Kumar  * @freq:		frequency to search for
3727813dd6fSViresh Kumar  * @available:		true/false - match for available opp
3737813dd6fSViresh Kumar  *
3747813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
3757813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
3767813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
3777813dd6fSViresh Kumar  * EINVAL:	for bad pointer
3787813dd6fSViresh Kumar  * ERANGE:	no match found for search
3797813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
3807813dd6fSViresh Kumar  *
3817813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
3827813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
3837813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
3847813dd6fSViresh Kumar  *
3857813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
3867813dd6fSViresh Kumar  * or the opposite as well.
3877813dd6fSViresh Kumar  *
3887813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
3897813dd6fSViresh Kumar  * use.
3907813dd6fSViresh Kumar  */
3917813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
3927813dd6fSViresh Kumar 					      unsigned long freq,
3937813dd6fSViresh Kumar 					      bool available)
3947813dd6fSViresh Kumar {
3957813dd6fSViresh Kumar 	struct opp_table *opp_table;
3967813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
3977813dd6fSViresh Kumar 
3987813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3997813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
4007813dd6fSViresh Kumar 		int r = PTR_ERR(opp_table);
4017813dd6fSViresh Kumar 
4027813dd6fSViresh Kumar 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
4037813dd6fSViresh Kumar 		return ERR_PTR(r);
4047813dd6fSViresh Kumar 	}
4057813dd6fSViresh Kumar 
4067813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
4077813dd6fSViresh Kumar 
4087813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
4097813dd6fSViresh Kumar 		if (temp_opp->available == available &&
4107813dd6fSViresh Kumar 				temp_opp->rate == freq) {
4117813dd6fSViresh Kumar 			opp = temp_opp;
4127813dd6fSViresh Kumar 
4137813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
4147813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
4157813dd6fSViresh Kumar 			break;
4167813dd6fSViresh Kumar 		}
4177813dd6fSViresh Kumar 	}
4187813dd6fSViresh Kumar 
4197813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
4207813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4217813dd6fSViresh Kumar 
4227813dd6fSViresh Kumar 	return opp;
4237813dd6fSViresh Kumar }
4247813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
4257813dd6fSViresh Kumar 
42671419d84SNiklas Cassel /**
42771419d84SNiklas Cassel  * dev_pm_opp_find_level_exact() - search for an exact level
42871419d84SNiklas Cassel  * @dev:		device for which we do this operation
42971419d84SNiklas Cassel  * @level:		level to search for
43071419d84SNiklas Cassel  *
43171419d84SNiklas Cassel  * Return: Searches for exact match in the opp table and returns pointer to the
43271419d84SNiklas Cassel  * matching opp if found, else returns ERR_PTR in case of error and should
43371419d84SNiklas Cassel  * be handled using IS_ERR. Error return values can be:
43471419d84SNiklas Cassel  * EINVAL:	for bad pointer
43571419d84SNiklas Cassel  * ERANGE:	no match found for search
43671419d84SNiklas Cassel  * ENODEV:	if device not found in list of registered devices
43771419d84SNiklas Cassel  *
43871419d84SNiklas Cassel  * The callers are required to call dev_pm_opp_put() for the returned OPP after
43971419d84SNiklas Cassel  * use.
44071419d84SNiklas Cassel  */
44171419d84SNiklas Cassel struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
44271419d84SNiklas Cassel 					       unsigned int level)
44371419d84SNiklas Cassel {
44471419d84SNiklas Cassel 	struct opp_table *opp_table;
44571419d84SNiklas Cassel 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
44671419d84SNiklas Cassel 
44771419d84SNiklas Cassel 	opp_table = _find_opp_table(dev);
44871419d84SNiklas Cassel 	if (IS_ERR(opp_table)) {
44971419d84SNiklas Cassel 		int r = PTR_ERR(opp_table);
45071419d84SNiklas Cassel 
45171419d84SNiklas Cassel 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
45271419d84SNiklas Cassel 		return ERR_PTR(r);
45371419d84SNiklas Cassel 	}
45471419d84SNiklas Cassel 
45571419d84SNiklas Cassel 	mutex_lock(&opp_table->lock);
45671419d84SNiklas Cassel 
45771419d84SNiklas Cassel 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
45871419d84SNiklas Cassel 		if (temp_opp->level == level) {
45971419d84SNiklas Cassel 			opp = temp_opp;
46071419d84SNiklas Cassel 
46171419d84SNiklas Cassel 			/* Increment the reference count of OPP */
46271419d84SNiklas Cassel 			dev_pm_opp_get(opp);
46371419d84SNiklas Cassel 			break;
46471419d84SNiklas Cassel 		}
46571419d84SNiklas Cassel 	}
46671419d84SNiklas Cassel 
46771419d84SNiklas Cassel 	mutex_unlock(&opp_table->lock);
46871419d84SNiklas Cassel 	dev_pm_opp_put_opp_table(opp_table);
46971419d84SNiklas Cassel 
47071419d84SNiklas Cassel 	return opp;
47171419d84SNiklas Cassel }
47271419d84SNiklas Cassel EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
47371419d84SNiklas Cassel 
4748dd5cadaSDmitry Osipenko /**
4758dd5cadaSDmitry Osipenko  * dev_pm_opp_find_level_ceil() - search for an rounded up level
4768dd5cadaSDmitry Osipenko  * @dev:		device for which we do this operation
4778dd5cadaSDmitry Osipenko  * @level:		level to search for
4788dd5cadaSDmitry Osipenko  *
4798dd5cadaSDmitry Osipenko  * Return: Searches for rounded up match in the opp table and returns pointer
4808dd5cadaSDmitry Osipenko  * to the  matching opp if found, else returns ERR_PTR in case of error and
4818dd5cadaSDmitry Osipenko  * should be handled using IS_ERR. Error return values can be:
4828dd5cadaSDmitry Osipenko  * EINVAL:	for bad pointer
4838dd5cadaSDmitry Osipenko  * ERANGE:	no match found for search
4848dd5cadaSDmitry Osipenko  * ENODEV:	if device not found in list of registered devices
4858dd5cadaSDmitry Osipenko  *
4868dd5cadaSDmitry Osipenko  * The callers are required to call dev_pm_opp_put() for the returned OPP after
4878dd5cadaSDmitry Osipenko  * use.
4888dd5cadaSDmitry Osipenko  */
4898dd5cadaSDmitry Osipenko struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
4908dd5cadaSDmitry Osipenko 					      unsigned int *level)
4918dd5cadaSDmitry Osipenko {
4928dd5cadaSDmitry Osipenko 	struct opp_table *opp_table;
4938dd5cadaSDmitry Osipenko 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
4948dd5cadaSDmitry Osipenko 
4958dd5cadaSDmitry Osipenko 	opp_table = _find_opp_table(dev);
4968dd5cadaSDmitry Osipenko 	if (IS_ERR(opp_table)) {
4978dd5cadaSDmitry Osipenko 		int r = PTR_ERR(opp_table);
4988dd5cadaSDmitry Osipenko 
4998dd5cadaSDmitry Osipenko 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
5008dd5cadaSDmitry Osipenko 		return ERR_PTR(r);
5018dd5cadaSDmitry Osipenko 	}
5028dd5cadaSDmitry Osipenko 
5038dd5cadaSDmitry Osipenko 	mutex_lock(&opp_table->lock);
5048dd5cadaSDmitry Osipenko 
5058dd5cadaSDmitry Osipenko 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5068dd5cadaSDmitry Osipenko 		if (temp_opp->available && temp_opp->level >= *level) {
5078dd5cadaSDmitry Osipenko 			opp = temp_opp;
5088dd5cadaSDmitry Osipenko 			*level = opp->level;
5098dd5cadaSDmitry Osipenko 
5108dd5cadaSDmitry Osipenko 			/* Increment the reference count of OPP */
5118dd5cadaSDmitry Osipenko 			dev_pm_opp_get(opp);
5128dd5cadaSDmitry Osipenko 			break;
5138dd5cadaSDmitry Osipenko 		}
5148dd5cadaSDmitry Osipenko 	}
5158dd5cadaSDmitry Osipenko 
5168dd5cadaSDmitry Osipenko 	mutex_unlock(&opp_table->lock);
5178dd5cadaSDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
5188dd5cadaSDmitry Osipenko 
5198dd5cadaSDmitry Osipenko 	return opp;
5208dd5cadaSDmitry Osipenko }
5218dd5cadaSDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
5228dd5cadaSDmitry Osipenko 
5237813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
5247813dd6fSViresh Kumar 						   unsigned long *freq)
5257813dd6fSViresh Kumar {
5267813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5277813dd6fSViresh Kumar 
5287813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
5297813dd6fSViresh Kumar 
5307813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5317813dd6fSViresh Kumar 		if (temp_opp->available && temp_opp->rate >= *freq) {
5327813dd6fSViresh Kumar 			opp = temp_opp;
5337813dd6fSViresh Kumar 			*freq = opp->rate;
5347813dd6fSViresh Kumar 
5357813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
5367813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
5377813dd6fSViresh Kumar 			break;
5387813dd6fSViresh Kumar 		}
5397813dd6fSViresh Kumar 	}
5407813dd6fSViresh Kumar 
5417813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
5427813dd6fSViresh Kumar 
5437813dd6fSViresh Kumar 	return opp;
5447813dd6fSViresh Kumar }
5457813dd6fSViresh Kumar 
5467813dd6fSViresh Kumar /**
5477813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
5487813dd6fSViresh Kumar  * @dev:	device for which we do this operation
5497813dd6fSViresh Kumar  * @freq:	Start frequency
5507813dd6fSViresh Kumar  *
5517813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
5527813dd6fSViresh Kumar  * for a device.
5537813dd6fSViresh Kumar  *
5547813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
5557813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
5567813dd6fSViresh Kumar  * values can be:
5577813dd6fSViresh Kumar  * EINVAL:	for bad pointer
5587813dd6fSViresh Kumar  * ERANGE:	no match found for search
5597813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
5607813dd6fSViresh Kumar  *
5617813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5627813dd6fSViresh Kumar  * use.
5637813dd6fSViresh Kumar  */
5647813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
5657813dd6fSViresh Kumar 					     unsigned long *freq)
5667813dd6fSViresh Kumar {
5677813dd6fSViresh Kumar 	struct opp_table *opp_table;
5687813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
5697813dd6fSViresh Kumar 
5707813dd6fSViresh Kumar 	if (!dev || !freq) {
5717813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
5727813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
5737813dd6fSViresh Kumar 	}
5747813dd6fSViresh Kumar 
5757813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
5767813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
5777813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
5787813dd6fSViresh Kumar 
5797813dd6fSViresh Kumar 	opp = _find_freq_ceil(opp_table, freq);
5807813dd6fSViresh Kumar 
5817813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
5827813dd6fSViresh Kumar 
5837813dd6fSViresh Kumar 	return opp;
5847813dd6fSViresh Kumar }
5857813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
5867813dd6fSViresh Kumar 
5877813dd6fSViresh Kumar /**
5887813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
5897813dd6fSViresh Kumar  * @dev:	device for which we do this operation
5907813dd6fSViresh Kumar  * @freq:	Start frequency
5917813dd6fSViresh Kumar  *
5927813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
5937813dd6fSViresh Kumar  * for a device.
5947813dd6fSViresh Kumar  *
5957813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
5967813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
5977813dd6fSViresh Kumar  * values can be:
5987813dd6fSViresh Kumar  * EINVAL:	for bad pointer
5997813dd6fSViresh Kumar  * ERANGE:	no match found for search
6007813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6017813dd6fSViresh Kumar  *
6027813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6037813dd6fSViresh Kumar  * use.
6047813dd6fSViresh Kumar  */
6057813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
6067813dd6fSViresh Kumar 					      unsigned long *freq)
6077813dd6fSViresh Kumar {
6087813dd6fSViresh Kumar 	struct opp_table *opp_table;
6097813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
6107813dd6fSViresh Kumar 
6117813dd6fSViresh Kumar 	if (!dev || !freq) {
6127813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
6137813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
6147813dd6fSViresh Kumar 	}
6157813dd6fSViresh Kumar 
6167813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
6177813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
6187813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
6197813dd6fSViresh Kumar 
6207813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
6217813dd6fSViresh Kumar 
6227813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
6237813dd6fSViresh Kumar 		if (temp_opp->available) {
6247813dd6fSViresh Kumar 			/* go to the next node, before choosing prev */
6257813dd6fSViresh Kumar 			if (temp_opp->rate > *freq)
6267813dd6fSViresh Kumar 				break;
6277813dd6fSViresh Kumar 			else
6287813dd6fSViresh Kumar 				opp = temp_opp;
6297813dd6fSViresh Kumar 		}
6307813dd6fSViresh Kumar 	}
6317813dd6fSViresh Kumar 
6327813dd6fSViresh Kumar 	/* Increment the reference count of OPP */
6337813dd6fSViresh Kumar 	if (!IS_ERR(opp))
6347813dd6fSViresh Kumar 		dev_pm_opp_get(opp);
6357813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
6367813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
6377813dd6fSViresh Kumar 
6387813dd6fSViresh Kumar 	if (!IS_ERR(opp))
6397813dd6fSViresh Kumar 		*freq = opp->rate;
6407813dd6fSViresh Kumar 
6417813dd6fSViresh Kumar 	return opp;
6427813dd6fSViresh Kumar }
6437813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
6447813dd6fSViresh Kumar 
6452f36bde0SAndrew-sh.Cheng /**
6462f36bde0SAndrew-sh.Cheng  * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
6472f36bde0SAndrew-sh.Cheng  *					 target voltage.
6482f36bde0SAndrew-sh.Cheng  * @dev:	Device for which we do this operation.
6492f36bde0SAndrew-sh.Cheng  * @u_volt:	Target voltage.
6502f36bde0SAndrew-sh.Cheng  *
6512f36bde0SAndrew-sh.Cheng  * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
6522f36bde0SAndrew-sh.Cheng  *
6532f36bde0SAndrew-sh.Cheng  * Return: matching *opp, else returns ERR_PTR in case of error which should be
6542f36bde0SAndrew-sh.Cheng  * handled using IS_ERR.
6552f36bde0SAndrew-sh.Cheng  *
6562f36bde0SAndrew-sh.Cheng  * Error return values can be:
6572f36bde0SAndrew-sh.Cheng  * EINVAL:	bad parameters
6582f36bde0SAndrew-sh.Cheng  *
6592f36bde0SAndrew-sh.Cheng  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6602f36bde0SAndrew-sh.Cheng  * use.
6612f36bde0SAndrew-sh.Cheng  */
6622f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
6632f36bde0SAndrew-sh.Cheng 						     unsigned long u_volt)
6642f36bde0SAndrew-sh.Cheng {
6652f36bde0SAndrew-sh.Cheng 	struct opp_table *opp_table;
6662f36bde0SAndrew-sh.Cheng 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
6672f36bde0SAndrew-sh.Cheng 
6682f36bde0SAndrew-sh.Cheng 	if (!dev || !u_volt) {
6692f36bde0SAndrew-sh.Cheng 		dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
6702f36bde0SAndrew-sh.Cheng 			u_volt);
6712f36bde0SAndrew-sh.Cheng 		return ERR_PTR(-EINVAL);
6722f36bde0SAndrew-sh.Cheng 	}
6732f36bde0SAndrew-sh.Cheng 
6742f36bde0SAndrew-sh.Cheng 	opp_table = _find_opp_table(dev);
6752f36bde0SAndrew-sh.Cheng 	if (IS_ERR(opp_table))
6762f36bde0SAndrew-sh.Cheng 		return ERR_CAST(opp_table);
6772f36bde0SAndrew-sh.Cheng 
6782f36bde0SAndrew-sh.Cheng 	mutex_lock(&opp_table->lock);
6792f36bde0SAndrew-sh.Cheng 
6802f36bde0SAndrew-sh.Cheng 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
6812f36bde0SAndrew-sh.Cheng 		if (temp_opp->available) {
6822f36bde0SAndrew-sh.Cheng 			if (temp_opp->supplies[0].u_volt > u_volt)
6832f36bde0SAndrew-sh.Cheng 				break;
6842f36bde0SAndrew-sh.Cheng 			opp = temp_opp;
6852f36bde0SAndrew-sh.Cheng 		}
6862f36bde0SAndrew-sh.Cheng 	}
6872f36bde0SAndrew-sh.Cheng 
6882f36bde0SAndrew-sh.Cheng 	/* Increment the reference count of OPP */
6892f36bde0SAndrew-sh.Cheng 	if (!IS_ERR(opp))
6902f36bde0SAndrew-sh.Cheng 		dev_pm_opp_get(opp);
6912f36bde0SAndrew-sh.Cheng 
6922f36bde0SAndrew-sh.Cheng 	mutex_unlock(&opp_table->lock);
6932f36bde0SAndrew-sh.Cheng 	dev_pm_opp_put_opp_table(opp_table);
6942f36bde0SAndrew-sh.Cheng 
6952f36bde0SAndrew-sh.Cheng 	return opp;
6962f36bde0SAndrew-sh.Cheng }
6972f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
6982f36bde0SAndrew-sh.Cheng 
6997813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
7007813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
7017813dd6fSViresh Kumar {
7027813dd6fSViresh Kumar 	int ret;
7037813dd6fSViresh Kumar 
7047813dd6fSViresh Kumar 	/* Regulator not available for device */
7057813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
7067813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
7077813dd6fSViresh Kumar 			PTR_ERR(reg));
7087813dd6fSViresh Kumar 		return 0;
7097813dd6fSViresh Kumar 	}
7107813dd6fSViresh Kumar 
7117813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
7127813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
7137813dd6fSViresh Kumar 
7147813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
7157813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
7167813dd6fSViresh Kumar 	if (ret)
7177813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
7187813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
7197813dd6fSViresh Kumar 			supply->u_volt_max, ret);
7207813dd6fSViresh Kumar 
7217813dd6fSViresh Kumar 	return ret;
7227813dd6fSViresh Kumar }
7237813dd6fSViresh Kumar 
724285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
725285881b5SViresh Kumar 					    unsigned long freq)
7267813dd6fSViresh Kumar {
7277813dd6fSViresh Kumar 	int ret;
7287813dd6fSViresh Kumar 
729*35e74b2eSViresh Kumar 	/* We may reach here for devices which don't change frequency */
730*35e74b2eSViresh Kumar 	if (IS_ERR(clk))
731*35e74b2eSViresh Kumar 		return 0;
732*35e74b2eSViresh Kumar 
7337813dd6fSViresh Kumar 	ret = clk_set_rate(clk, freq);
7347813dd6fSViresh Kumar 	if (ret) {
7357813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
7367813dd6fSViresh Kumar 			ret);
7377813dd6fSViresh Kumar 	}
7387813dd6fSViresh Kumar 
7397813dd6fSViresh Kumar 	return ret;
7407813dd6fSViresh Kumar }
7417813dd6fSViresh Kumar 
7428d45719cSKamil Konieczny static int _generic_set_opp_regulator(struct opp_table *opp_table,
7437813dd6fSViresh Kumar 				      struct device *dev,
7443f62670fSViresh Kumar 				      struct dev_pm_opp *opp,
7457813dd6fSViresh Kumar 				      unsigned long freq,
7463f62670fSViresh Kumar 				      int scaling_down)
7477813dd6fSViresh Kumar {
7487813dd6fSViresh Kumar 	struct regulator *reg = opp_table->regulators[0];
7493f62670fSViresh Kumar 	struct dev_pm_opp *old_opp = opp_table->current_opp;
7507813dd6fSViresh Kumar 	int ret;
7517813dd6fSViresh Kumar 
7527813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
7537813dd6fSViresh Kumar 	if (WARN_ON(opp_table->regulator_count > 1)) {
7547813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
7557813dd6fSViresh Kumar 		return -EINVAL;
7567813dd6fSViresh Kumar 	}
7577813dd6fSViresh Kumar 
7587813dd6fSViresh Kumar 	/* Scaling up? Scale voltage before frequency */
7593f62670fSViresh Kumar 	if (!scaling_down) {
7603f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
7617813dd6fSViresh Kumar 		if (ret)
7627813dd6fSViresh Kumar 			goto restore_voltage;
7637813dd6fSViresh Kumar 	}
7647813dd6fSViresh Kumar 
7657813dd6fSViresh Kumar 	/* Change frequency */
766285881b5SViresh Kumar 	ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
7677813dd6fSViresh Kumar 	if (ret)
7687813dd6fSViresh Kumar 		goto restore_voltage;
7697813dd6fSViresh Kumar 
7707813dd6fSViresh Kumar 	/* Scaling down? Scale voltage after frequency */
7713f62670fSViresh Kumar 	if (scaling_down) {
7723f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
7737813dd6fSViresh Kumar 		if (ret)
7747813dd6fSViresh Kumar 			goto restore_freq;
7757813dd6fSViresh Kumar 	}
7767813dd6fSViresh Kumar 
7778d45719cSKamil Konieczny 	/*
7788d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
7798d45719cSKamil Konieczny 	 * some boot-enabled regulators.
7808d45719cSKamil Konieczny 	 */
78172f80ce4SViresh Kumar 	if (unlikely(!opp_table->enabled)) {
7828d45719cSKamil Konieczny 		ret = regulator_enable(reg);
7838d45719cSKamil Konieczny 		if (ret < 0)
7848d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
7858d45719cSKamil Konieczny 	}
7868d45719cSKamil Konieczny 
7877813dd6fSViresh Kumar 	return 0;
7887813dd6fSViresh Kumar 
7897813dd6fSViresh Kumar restore_freq:
7903f62670fSViresh Kumar 	if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate))
7917813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
7923f62670fSViresh Kumar 			__func__, old_opp->rate);
7937813dd6fSViresh Kumar restore_voltage:
7947813dd6fSViresh Kumar 	/* This shouldn't harm even if the voltages weren't updated earlier */
7953f62670fSViresh Kumar 	_set_opp_voltage(dev, reg, old_opp->supplies);
7967813dd6fSViresh Kumar 
7977813dd6fSViresh Kumar 	return ret;
7987813dd6fSViresh Kumar }
7997813dd6fSViresh Kumar 
800b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
801b00e667aSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev, bool remove)
802b00e667aSViresh Kumar {
803b00e667aSViresh Kumar 	u32 avg, peak;
804b00e667aSViresh Kumar 	int i, ret;
805b00e667aSViresh Kumar 
806b00e667aSViresh Kumar 	if (!opp_table->paths)
807b00e667aSViresh Kumar 		return 0;
808b00e667aSViresh Kumar 
809b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
810b00e667aSViresh Kumar 		if (remove) {
811b00e667aSViresh Kumar 			avg = 0;
812b00e667aSViresh Kumar 			peak = 0;
813b00e667aSViresh Kumar 		} else {
814b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
815b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
816b00e667aSViresh Kumar 		}
817b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
818b00e667aSViresh Kumar 		if (ret) {
819b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
820b00e667aSViresh Kumar 				remove ? "remove" : "set", i, ret);
821b00e667aSViresh Kumar 			return ret;
822b00e667aSViresh Kumar 		}
823b00e667aSViresh Kumar 	}
824b00e667aSViresh Kumar 
825b00e667aSViresh Kumar 	return 0;
826b00e667aSViresh Kumar }
827b00e667aSViresh Kumar 
8287e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table,
8297e535993SViresh Kumar 			   struct device *dev, unsigned long old_freq,
8307e535993SViresh Kumar 			   unsigned long freq,
8317e535993SViresh Kumar 			   struct dev_pm_opp_supply *old_supply,
8327e535993SViresh Kumar 			   struct dev_pm_opp_supply *new_supply)
8337e535993SViresh Kumar {
83404b447dfSDmitry Osipenko 	struct dev_pm_set_opp_data *data = opp_table->set_opp_data;
8357e535993SViresh Kumar 	int size;
8367e535993SViresh Kumar 
83704b447dfSDmitry Osipenko 	/*
83804b447dfSDmitry Osipenko 	 * We support this only if dev_pm_opp_set_regulators() was called
83904b447dfSDmitry Osipenko 	 * earlier.
84004b447dfSDmitry Osipenko 	 */
84104b447dfSDmitry Osipenko 	if (opp_table->sod_supplies) {
8427e535993SViresh Kumar 		size = sizeof(*old_supply) * opp_table->regulator_count;
8437e535993SViresh Kumar 		memcpy(data->old_opp.supplies, old_supply, size);
8447e535993SViresh Kumar 		memcpy(data->new_opp.supplies, new_supply, size);
84504b447dfSDmitry Osipenko 		data->regulator_count = opp_table->regulator_count;
84604b447dfSDmitry Osipenko 	} else {
84704b447dfSDmitry Osipenko 		data->regulator_count = 0;
84804b447dfSDmitry Osipenko 	}
84904b447dfSDmitry Osipenko 
85004b447dfSDmitry Osipenko 	data->regulators = opp_table->regulators;
85104b447dfSDmitry Osipenko 	data->clk = opp_table->clk;
85204b447dfSDmitry Osipenko 	data->dev = dev;
85304b447dfSDmitry Osipenko 	data->old_opp.rate = old_freq;
85404b447dfSDmitry Osipenko 	data->new_opp.rate = freq;
8557e535993SViresh Kumar 
8567e535993SViresh Kumar 	return opp_table->set_opp(data);
8577e535993SViresh Kumar }
8587e535993SViresh Kumar 
85960cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev,
86060cdeae0SStephan Gerhold 			     struct dev_pm_opp *opp, int i)
86160cdeae0SStephan Gerhold {
86260cdeae0SStephan Gerhold 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
86360cdeae0SStephan Gerhold 	int ret;
86460cdeae0SStephan Gerhold 
86560cdeae0SStephan Gerhold 	if (!pd_dev)
86660cdeae0SStephan Gerhold 		return 0;
86760cdeae0SStephan Gerhold 
86860cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
86960cdeae0SStephan Gerhold 	if (ret) {
87060cdeae0SStephan Gerhold 		dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
87160cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
87260cdeae0SStephan Gerhold 	}
87360cdeae0SStephan Gerhold 
87460cdeae0SStephan Gerhold 	return ret;
87560cdeae0SStephan Gerhold }
87660cdeae0SStephan Gerhold 
877ca1b5d77SViresh Kumar /* This is only called for PM domain for now */
878ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev,
879ca1b5d77SViresh Kumar 			      struct opp_table *opp_table,
8802c59138cSStephan Gerhold 			      struct dev_pm_opp *opp, bool up)
881ca1b5d77SViresh Kumar {
882ca1b5d77SViresh Kumar 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
883ca1b5d77SViresh Kumar 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
884ca1b5d77SViresh Kumar 	int i, ret = 0;
885ca1b5d77SViresh Kumar 
886ca1b5d77SViresh Kumar 	if (!required_opp_tables)
887ca1b5d77SViresh Kumar 		return 0;
888ca1b5d77SViresh Kumar 
889ca1b5d77SViresh Kumar 	/* Single genpd case */
89060cdeae0SStephan Gerhold 	if (!genpd_virt_devs)
89160cdeae0SStephan Gerhold 		return _set_required_opp(dev, dev, opp, 0);
892ca1b5d77SViresh Kumar 
893ca1b5d77SViresh Kumar 	/* Multiple genpd case */
894ca1b5d77SViresh Kumar 
895ca1b5d77SViresh Kumar 	/*
896ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
897ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
898ca1b5d77SViresh Kumar 	 */
899ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
900ca1b5d77SViresh Kumar 
9012c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
9022c59138cSStephan Gerhold 	if (up) {
903ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
90460cdeae0SStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
90560cdeae0SStephan Gerhold 			if (ret)
906ca1b5d77SViresh Kumar 				break;
907ca1b5d77SViresh Kumar 		}
9082c59138cSStephan Gerhold 	} else {
9092c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
9102c59138cSStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
9112c59138cSStephan Gerhold 			if (ret)
912ca1b5d77SViresh Kumar 				break;
913ca1b5d77SViresh Kumar 		}
914ca1b5d77SViresh Kumar 	}
9152c59138cSStephan Gerhold 
916ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
917ca1b5d77SViresh Kumar 
918ca1b5d77SViresh Kumar 	return ret;
919ca1b5d77SViresh Kumar }
920ca1b5d77SViresh Kumar 
9217813dd6fSViresh Kumar /**
9223ae1f39aSSibi Sankar  * dev_pm_opp_set_bw() - sets bandwidth levels corresponding to an opp
9233ae1f39aSSibi Sankar  * @dev:	device for which we do this operation
9243ae1f39aSSibi Sankar  * @opp:	opp based on which the bandwidth levels are to be configured
9253ae1f39aSSibi Sankar  *
9263ae1f39aSSibi Sankar  * This configures the bandwidth to the levels specified by the OPP. However
9273ae1f39aSSibi Sankar  * if the OPP specified is NULL the bandwidth levels are cleared out.
9283ae1f39aSSibi Sankar  *
9293ae1f39aSSibi Sankar  * Return: 0 on success or a negative error value.
9303ae1f39aSSibi Sankar  */
9313ae1f39aSSibi Sankar int dev_pm_opp_set_bw(struct device *dev, struct dev_pm_opp *opp)
9323ae1f39aSSibi Sankar {
9333ae1f39aSSibi Sankar 	struct opp_table *opp_table;
9343ae1f39aSSibi Sankar 	int ret;
9353ae1f39aSSibi Sankar 
9363ae1f39aSSibi Sankar 	opp_table = _find_opp_table(dev);
9373ae1f39aSSibi Sankar 	if (IS_ERR(opp_table)) {
9383ae1f39aSSibi Sankar 		dev_err(dev, "%s: device opp table doesn't exist\n", __func__);
9393ae1f39aSSibi Sankar 		return PTR_ERR(opp_table);
9403ae1f39aSSibi Sankar 	}
9413ae1f39aSSibi Sankar 
9423ae1f39aSSibi Sankar 	if (opp)
9433ae1f39aSSibi Sankar 		ret = _set_opp_bw(opp_table, opp, dev, false);
9443ae1f39aSSibi Sankar 	else
9453ae1f39aSSibi Sankar 		ret = _set_opp_bw(opp_table, NULL, dev, true);
9463ae1f39aSSibi Sankar 
9473ae1f39aSSibi Sankar 	dev_pm_opp_put_opp_table(opp_table);
9483ae1f39aSSibi Sankar 	return ret;
9493ae1f39aSSibi Sankar }
9503ae1f39aSSibi Sankar EXPORT_SYMBOL_GPL(dev_pm_opp_set_bw);
9513ae1f39aSSibi Sankar 
95281c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
95381c4d8a3SViresh Kumar {
95481c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
95581c4d8a3SViresh Kumar 	unsigned long freq;
95681c4d8a3SViresh Kumar 
95781c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
95881c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
95981c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
96081c4d8a3SViresh Kumar 	}
96181c4d8a3SViresh Kumar 
96281c4d8a3SViresh Kumar 	/*
96381c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
96481c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
96581c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
96681c4d8a3SViresh Kumar 	 */
96781c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
96881c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
96981c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
97081c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
97181c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
97281c4d8a3SViresh Kumar 	}
97381c4d8a3SViresh Kumar 
97481c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
97581c4d8a3SViresh Kumar }
97681c4d8a3SViresh Kumar 
9775ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
978f3364e17SViresh Kumar {
979f3364e17SViresh Kumar 	int ret;
980f3364e17SViresh Kumar 
981f3364e17SViresh Kumar 	if (!opp_table->enabled)
982f3364e17SViresh Kumar 		return 0;
983f3364e17SViresh Kumar 
984f3364e17SViresh Kumar 	/*
985f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
986f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
987f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
988f3364e17SViresh Kumar 	 */
989f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
990f3364e17SViresh Kumar 		return 0;
991f3364e17SViresh Kumar 
992f3364e17SViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev, true);
993f3364e17SViresh Kumar 	if (ret)
994f3364e17SViresh Kumar 		return ret;
995f3364e17SViresh Kumar 
996f3364e17SViresh Kumar 	if (opp_table->regulators)
997f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
998f3364e17SViresh Kumar 
9992c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
1000f3364e17SViresh Kumar 
1001f3364e17SViresh Kumar 	opp_table->enabled = false;
1002f3364e17SViresh Kumar 	return ret;
1003f3364e17SViresh Kumar }
1004f3364e17SViresh Kumar 
1005386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
1006386ba854SViresh Kumar 		    struct dev_pm_opp *opp, unsigned long freq)
10077813dd6fSViresh Kumar {
1008386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
1009386ba854SViresh Kumar 	unsigned long old_freq;
1010f0b88fa4SViresh Kumar 	int scaling_down, ret;
10117813dd6fSViresh Kumar 
1012386ba854SViresh Kumar 	if (unlikely(!opp))
1013386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
1014aca48b61SRajendra Nayak 
101581c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
101681c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
101781c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
10187813dd6fSViresh Kumar 
101981c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
102081c4d8a3SViresh Kumar 	old_freq = old_opp->rate;
102181c4d8a3SViresh Kumar 
102281c4d8a3SViresh Kumar 	/* Return early if nothing to do */
102381c4d8a3SViresh Kumar 	if (opp_table->enabled && old_opp == opp) {
102481c4d8a3SViresh Kumar 		dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
1025386ba854SViresh Kumar 		return 0;
10267813dd6fSViresh Kumar 	}
10277813dd6fSViresh Kumar 
1028f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1029f0b88fa4SViresh Kumar 		__func__, old_freq, freq, old_opp->level, opp->level,
1030f0b88fa4SViresh Kumar 		old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1031f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1032f0b88fa4SViresh Kumar 
1033f0b88fa4SViresh Kumar 	scaling_down = _opp_compare_key(old_opp, opp);
1034f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1035f0b88fa4SViresh Kumar 		scaling_down = 0;
10367813dd6fSViresh Kumar 
1037ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1038f0b88fa4SViresh Kumar 	if (!scaling_down) {
10392c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1040ca1b5d77SViresh Kumar 		if (ret)
1041386ba854SViresh Kumar 			return ret;
1042ca1b5d77SViresh Kumar 	}
1043ca1b5d77SViresh Kumar 
10447e535993SViresh Kumar 	if (opp_table->set_opp) {
10457e535993SViresh Kumar 		ret = _set_opp_custom(opp_table, dev, old_freq, freq,
104681c4d8a3SViresh Kumar 				      old_opp->supplies, opp->supplies);
10477e535993SViresh Kumar 	} else if (opp_table->regulators) {
10483f62670fSViresh Kumar 		ret = _generic_set_opp_regulator(opp_table, dev, opp, freq,
10493f62670fSViresh Kumar 						 scaling_down);
10507813dd6fSViresh Kumar 	} else {
10517813dd6fSViresh Kumar 		/* Only frequency scaling */
10521d3c42caSViresh Kumar 		ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
10537813dd6fSViresh Kumar 	}
10547813dd6fSViresh Kumar 
1055ca1b5d77SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1056f0b88fa4SViresh Kumar 	if (!ret && scaling_down) {
10572c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, false);
1058ca1b5d77SViresh Kumar 		if (ret)
1059ca1b5d77SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1060ca1b5d77SViresh Kumar 	}
1061ca1b5d77SViresh Kumar 
106272f80ce4SViresh Kumar 	if (!ret) {
1063b00e667aSViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev, false);
106481c4d8a3SViresh Kumar 		if (!ret) {
106572f80ce4SViresh Kumar 			opp_table->enabled = true;
106681c4d8a3SViresh Kumar 			dev_pm_opp_put(old_opp);
106781c4d8a3SViresh Kumar 
106881c4d8a3SViresh Kumar 			/* Make sure current_opp doesn't get freed */
106981c4d8a3SViresh Kumar 			dev_pm_opp_get(opp);
107081c4d8a3SViresh Kumar 			opp_table->current_opp = opp;
107181c4d8a3SViresh Kumar 		}
107272f80ce4SViresh Kumar 	}
1073fe2af402SGeorgi Djakov 
1074386ba854SViresh Kumar 	return ret;
1075386ba854SViresh Kumar }
1076386ba854SViresh Kumar 
1077386ba854SViresh Kumar /**
1078386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1079386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1080386ba854SViresh Kumar  * @target_freq: frequency to achieve
1081386ba854SViresh Kumar  *
1082386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1083386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1084386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1085386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1086386ba854SViresh Kumar  * frequency.
1087386ba854SViresh Kumar  */
1088386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1089386ba854SViresh Kumar {
1090386ba854SViresh Kumar 	struct opp_table *opp_table;
1091386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1092386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
1093386ba854SViresh Kumar 	int ret;
1094386ba854SViresh Kumar 
1095386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1096386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1097386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1098386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1099386ba854SViresh Kumar 	}
1100386ba854SViresh Kumar 
1101386ba854SViresh Kumar 	if (target_freq) {
1102386ba854SViresh Kumar 		/*
1103386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1104386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1105386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1106386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1107386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1108386ba854SViresh Kumar 		 */
1109386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
1110386ba854SViresh Kumar 			ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq);
1111386ba854SViresh Kumar 			goto put_opp_table;
1112386ba854SViresh Kumar 		}
1113386ba854SViresh Kumar 
1114386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1115386ba854SViresh Kumar 		if ((long)freq <= 0)
1116386ba854SViresh Kumar 			freq = target_freq;
1117386ba854SViresh Kumar 
1118386ba854SViresh Kumar 		/*
1119386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1120386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1121386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1122386ba854SViresh Kumar 		 */
1123386ba854SViresh Kumar 		temp_freq = freq;
1124386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1125386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1126386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1127386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1128386ba854SViresh Kumar 				__func__, freq, ret);
1129386ba854SViresh Kumar 			goto put_opp_table;
1130386ba854SViresh Kumar 		}
1131386ba854SViresh Kumar 	}
1132386ba854SViresh Kumar 
1133386ba854SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, freq);
1134386ba854SViresh Kumar 
1135386ba854SViresh Kumar 	if (target_freq)
11367813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
11377813dd6fSViresh Kumar put_opp_table:
11387813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
11397813dd6fSViresh Kumar 	return ret;
11407813dd6fSViresh Kumar }
11417813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
11427813dd6fSViresh Kumar 
11437813dd6fSViresh Kumar /* OPP-dev Helpers */
11447813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
11457813dd6fSViresh Kumar 			    struct opp_table *opp_table)
11467813dd6fSViresh Kumar {
11477813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
11487813dd6fSViresh Kumar 	list_del(&opp_dev->node);
11497813dd6fSViresh Kumar 	kfree(opp_dev);
11507813dd6fSViresh Kumar }
11517813dd6fSViresh Kumar 
1152ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
11537813dd6fSViresh Kumar 				struct opp_table *opp_table)
11547813dd6fSViresh Kumar {
11557813dd6fSViresh Kumar 	struct opp_device *opp_dev;
11567813dd6fSViresh Kumar 
11577813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
11587813dd6fSViresh Kumar 	if (!opp_dev)
11597813dd6fSViresh Kumar 		return NULL;
11607813dd6fSViresh Kumar 
11617813dd6fSViresh Kumar 	/* Initialize opp-dev */
11627813dd6fSViresh Kumar 	opp_dev->dev = dev;
11633d255699SViresh Kumar 
1164ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
11657813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1166ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
11677813dd6fSViresh Kumar 
11687813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1169a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1170283d55e6SViresh Kumar 
1171283d55e6SViresh Kumar 	return opp_dev;
1172283d55e6SViresh Kumar }
1173283d55e6SViresh Kumar 
1174eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
11757813dd6fSViresh Kumar {
11767813dd6fSViresh Kumar 	struct opp_table *opp_table;
11777813dd6fSViresh Kumar 	struct opp_device *opp_dev;
11787813dd6fSViresh Kumar 	int ret;
11797813dd6fSViresh Kumar 
11807813dd6fSViresh Kumar 	/*
11817813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
11827813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
11837813dd6fSViresh Kumar 	 */
11847813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
11857813dd6fSViresh Kumar 	if (!opp_table)
1186dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
11877813dd6fSViresh Kumar 
11883d255699SViresh Kumar 	mutex_init(&opp_table->lock);
11894f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
11907813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
11917813dd6fSViresh Kumar 
119246f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
119346f48acaSViresh Kumar 	opp_table->regulator_count = -1;
119446f48acaSViresh Kumar 
11957813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
11967813dd6fSViresh Kumar 	if (!opp_dev) {
1197dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1198dd461cd9SStephan Gerhold 		goto err;
11997813dd6fSViresh Kumar 	}
12007813dd6fSViresh Kumar 
1201eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
12027813dd6fSViresh Kumar 
12036d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
12046d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1205dd461cd9SStephan Gerhold 	if (ret) {
1206dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
120732439ac7SViresh Kumar 			goto remove_opp_dev;
1208dd461cd9SStephan Gerhold 
12096d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
12106d3f922cSGeorgi Djakov 			 __func__, ret);
1211dd461cd9SStephan Gerhold 	}
12126d3f922cSGeorgi Djakov 
12137813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
12147813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
12157813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
12167813dd6fSViresh Kumar 
12177813dd6fSViresh Kumar 	return opp_table;
1218dd461cd9SStephan Gerhold 
1219976509bbSQuanyang Wang remove_opp_dev:
1220976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1221dd461cd9SStephan Gerhold err:
1222dd461cd9SStephan Gerhold 	kfree(opp_table);
1223dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
12247813dd6fSViresh Kumar }
12257813dd6fSViresh Kumar 
12267813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
12277813dd6fSViresh Kumar {
12287813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
12297813dd6fSViresh Kumar }
12307813dd6fSViresh Kumar 
123132439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
123232439ac7SViresh Kumar 					       struct opp_table *opp_table,
123332439ac7SViresh Kumar 					       bool getclk)
123432439ac7SViresh Kumar {
123532439ac7SViresh Kumar 	/*
123632439ac7SViresh Kumar 	 * Return early if we don't need to get clk or we have already tried it
123732439ac7SViresh Kumar 	 * earlier.
123832439ac7SViresh Kumar 	 */
123932439ac7SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || opp_table->clk)
124032439ac7SViresh Kumar 		return opp_table;
124132439ac7SViresh Kumar 
124232439ac7SViresh Kumar 	/* Find clk for the device */
124332439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
124432439ac7SViresh Kumar 	if (IS_ERR(opp_table->clk)) {
124532439ac7SViresh Kumar 		int ret = PTR_ERR(opp_table->clk);
124632439ac7SViresh Kumar 
124732439ac7SViresh Kumar 		if (ret == -EPROBE_DEFER) {
124832439ac7SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
124932439ac7SViresh Kumar 			return ERR_PTR(ret);
125032439ac7SViresh Kumar 		}
125132439ac7SViresh Kumar 
125232439ac7SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
125332439ac7SViresh Kumar 	}
125432439ac7SViresh Kumar 
125532439ac7SViresh Kumar 	return opp_table;
125632439ac7SViresh Kumar }
125732439ac7SViresh Kumar 
125827c09484SViresh Kumar /*
125927c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
126027c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
126127c09484SViresh Kumar  *
126227c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
126327c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
126427c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
126527c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
126627c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
126727c09484SViresh Kumar  * indirect users of OPP core.
126827c09484SViresh Kumar  *
126927c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
127027c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
127127c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
127227c09484SViresh Kumar  */
127332439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
127432439ac7SViresh Kumar 					 bool getclk)
12757813dd6fSViresh Kumar {
12767813dd6fSViresh Kumar 	struct opp_table *opp_table;
12777813dd6fSViresh Kumar 
127827c09484SViresh Kumar again:
12797813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
12807813dd6fSViresh Kumar 
12817813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
12827813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
12837813dd6fSViresh Kumar 		goto unlock;
12847813dd6fSViresh Kumar 
128527c09484SViresh Kumar 	/*
128627c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
128727c09484SViresh Kumar 	 * another user, wait for it to finish.
128827c09484SViresh Kumar 	 */
128927c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
129027c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
129127c09484SViresh Kumar 		cpu_relax();
129227c09484SViresh Kumar 		goto again;
129327c09484SViresh Kumar 	}
129427c09484SViresh Kumar 
129527c09484SViresh Kumar 	opp_tables_busy = true;
1296283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
129727c09484SViresh Kumar 
129827c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
129927c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
130027c09484SViresh Kumar 
1301283d55e6SViresh Kumar 	if (opp_table) {
1302ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1303283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1304dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1305283d55e6SViresh Kumar 		}
130627c09484SViresh Kumar 
130727c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
130827c09484SViresh Kumar 	} else {
130927c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
131027c09484SViresh Kumar 
131127c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
131227c09484SViresh Kumar 		if (!IS_ERR(opp_table))
131327c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1314283d55e6SViresh Kumar 	}
1315283d55e6SViresh Kumar 
131627c09484SViresh Kumar 	opp_tables_busy = false;
13177813dd6fSViresh Kumar 
13187813dd6fSViresh Kumar unlock:
13197813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
13207813dd6fSViresh Kumar 
132132439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
13227813dd6fSViresh Kumar }
1323eb7c8743SViresh Kumar 
132432439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1325e77dcb0bSViresh Kumar {
132632439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1327e77dcb0bSViresh Kumar }
1328e77dcb0bSViresh Kumar 
1329eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1330eb7c8743SViresh Kumar {
1331e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1332eb7c8743SViresh Kumar }
13337813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
13347813dd6fSViresh Kumar 
13357813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
13367813dd6fSViresh Kumar {
13377813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1338cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
13396d3f922cSGeorgi Djakov 	int i;
13407813dd6fSViresh Kumar 
1341e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1342e0df59deSViresh Kumar 	list_del(&opp_table->node);
1343e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1344e0df59deSViresh Kumar 
134581c4d8a3SViresh Kumar 	if (opp_table->current_opp)
134681c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
134781c4d8a3SViresh Kumar 
13485d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
13495d6d106fSViresh Kumar 
13507813dd6fSViresh Kumar 	/* Release clk */
13517813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
13527813dd6fSViresh Kumar 		clk_put(opp_table->clk);
13537813dd6fSViresh Kumar 
13546d3f922cSGeorgi Djakov 	if (opp_table->paths) {
13556d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
13566d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
13576d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
13586d3f922cSGeorgi Djakov 	}
13596d3f922cSGeorgi Djakov 
1360cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1361cdd6ed90SViresh Kumar 
1362cdd6ed90SViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
13633d255699SViresh Kumar 		/*
1364cdd6ed90SViresh Kumar 		 * The OPP table is getting removed, drop the performance state
1365cdd6ed90SViresh Kumar 		 * constraints.
13663d255699SViresh Kumar 		 */
1367cdd6ed90SViresh Kumar 		if (opp_table->genpd_performance_state)
1368cdd6ed90SViresh Kumar 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
13697813dd6fSViresh Kumar 
13707813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
1371cdd6ed90SViresh Kumar 	}
13727813dd6fSViresh Kumar 
13734f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
13747813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
13757813dd6fSViresh Kumar 	kfree(opp_table);
13767813dd6fSViresh Kumar }
13777813dd6fSViresh Kumar 
13787813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
13797813dd6fSViresh Kumar {
13807813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
13817813dd6fSViresh Kumar 		       &opp_table_lock);
13827813dd6fSViresh Kumar }
13837813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
13847813dd6fSViresh Kumar 
13857813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
13867813dd6fSViresh Kumar {
13877813dd6fSViresh Kumar 	kfree(opp);
13887813dd6fSViresh Kumar }
13897813dd6fSViresh Kumar 
1390cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
13917813dd6fSViresh Kumar {
1392cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1393cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1394cf1fac94SViresh Kumar 
1395cf1fac94SViresh Kumar 	list_del(&opp->node);
1396cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1397cf1fac94SViresh Kumar 
13987813dd6fSViresh Kumar 	/*
13997813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
14007813dd6fSViresh Kumar 	 * frequency/voltage list.
14017813dd6fSViresh Kumar 	 */
14027813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1403da544b61SViresh Kumar 	_of_opp_free_required_opps(opp_table, opp);
14047813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
14057813dd6fSViresh Kumar 	kfree(opp);
14061690d8bbSViresh Kumar }
14077813dd6fSViresh Kumar 
1408a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
14097813dd6fSViresh Kumar {
14107813dd6fSViresh Kumar 	kref_get(&opp->kref);
14117813dd6fSViresh Kumar }
14127813dd6fSViresh Kumar 
14137813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
14147813dd6fSViresh Kumar {
1415cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
14167813dd6fSViresh Kumar }
14177813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
14187813dd6fSViresh Kumar 
14197813dd6fSViresh Kumar /**
14207813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
14217813dd6fSViresh Kumar  * @dev:	device for which we do this operation
14227813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
14237813dd6fSViresh Kumar  *
14247813dd6fSViresh Kumar  * This function removes an opp from the opp table.
14257813dd6fSViresh Kumar  */
14267813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
14277813dd6fSViresh Kumar {
14287813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
14297813dd6fSViresh Kumar 	struct opp_table *opp_table;
14307813dd6fSViresh Kumar 	bool found = false;
14317813dd6fSViresh Kumar 
14327813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
14337813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
14347813dd6fSViresh Kumar 		return;
14357813dd6fSViresh Kumar 
14367813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
14377813dd6fSViresh Kumar 
14387813dd6fSViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
14397813dd6fSViresh Kumar 		if (opp->rate == freq) {
14407813dd6fSViresh Kumar 			found = true;
14417813dd6fSViresh Kumar 			break;
14427813dd6fSViresh Kumar 		}
14437813dd6fSViresh Kumar 	}
14447813dd6fSViresh Kumar 
14457813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
14467813dd6fSViresh Kumar 
14477813dd6fSViresh Kumar 	if (found) {
14487813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
14490ad8c623SViresh Kumar 
14500ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
14510ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
14527813dd6fSViresh Kumar 	} else {
14537813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
14547813dd6fSViresh Kumar 			 __func__, freq);
14557813dd6fSViresh Kumar 	}
14567813dd6fSViresh Kumar 
14570ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
14587813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
14597813dd6fSViresh Kumar }
14607813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
14617813dd6fSViresh Kumar 
1462cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1463cf1fac94SViresh Kumar 					bool dynamic)
1464cf1fac94SViresh Kumar {
1465cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1466cf1fac94SViresh Kumar 
1467cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1468cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1469cf1fac94SViresh Kumar 		if (dynamic == temp->dynamic) {
1470cf1fac94SViresh Kumar 			opp = temp;
1471cf1fac94SViresh Kumar 			break;
1472cf1fac94SViresh Kumar 		}
1473cf1fac94SViresh Kumar 	}
1474cf1fac94SViresh Kumar 
1475cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1476cf1fac94SViresh Kumar 	return opp;
1477cf1fac94SViresh Kumar }
1478cf1fac94SViresh Kumar 
1479922ff075SViresh Kumar bool _opp_remove_all_static(struct opp_table *opp_table)
148003758d60SViresh Kumar {
1481cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
148203758d60SViresh Kumar 
148303758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
148403758d60SViresh Kumar 
1485922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1486cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1487cf1fac94SViresh Kumar 		return false;
1488922ff075SViresh Kumar 	}
1489922ff075SViresh Kumar 
1490cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1491cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1492cf1fac94SViresh Kumar 		return true;
149303758d60SViresh Kumar 	}
149403758d60SViresh Kumar 
149503758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1496922ff075SViresh Kumar 
1497cf1fac94SViresh Kumar 	/*
1498cf1fac94SViresh Kumar 	 * Can't remove the OPP from under the lock, debugfs removal needs to
1499cf1fac94SViresh Kumar 	 * happen lock less to avoid circular dependency issues.
1500cf1fac94SViresh Kumar 	 */
1501cf1fac94SViresh Kumar 	while ((opp = _opp_get_next(opp_table, false)))
1502cf1fac94SViresh Kumar 		dev_pm_opp_put(opp);
1503cf1fac94SViresh Kumar 
1504cf1fac94SViresh Kumar 	return true;
150503758d60SViresh Kumar }
150603758d60SViresh Kumar 
15071690d8bbSViresh Kumar /**
15081690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
15091690d8bbSViresh Kumar  * @dev:	device for which we do this operation
15101690d8bbSViresh Kumar  *
15111690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
15121690d8bbSViresh Kumar  */
15131690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
15141690d8bbSViresh Kumar {
15151690d8bbSViresh Kumar 	struct opp_table *opp_table;
1516cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
15171690d8bbSViresh Kumar 	int count = 0;
15181690d8bbSViresh Kumar 
15191690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
15201690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
15211690d8bbSViresh Kumar 		return;
15221690d8bbSViresh Kumar 
1523cf1fac94SViresh Kumar 	/*
1524cf1fac94SViresh Kumar 	 * Can't remove the OPP from under the lock, debugfs removal needs to
1525cf1fac94SViresh Kumar 	 * happen lock less to avoid circular dependency issues.
1526cf1fac94SViresh Kumar 	 */
1527cf1fac94SViresh Kumar 	while ((opp = _opp_get_next(opp_table, true))) {
1528cf1fac94SViresh Kumar 		dev_pm_opp_put(opp);
15291690d8bbSViresh Kumar 		count++;
15301690d8bbSViresh Kumar 	}
15311690d8bbSViresh Kumar 
15321690d8bbSViresh Kumar 	/* Drop the references taken by dev_pm_opp_add() */
15331690d8bbSViresh Kumar 	while (count--)
15341690d8bbSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
15351690d8bbSViresh Kumar 
15361690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
15371690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
15381690d8bbSViresh Kumar }
15391690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
15401690d8bbSViresh Kumar 
15417813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table)
15427813dd6fSViresh Kumar {
15437813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
15446d3f922cSGeorgi Djakov 	int supply_count, supply_size, icc_size;
15457813dd6fSViresh Kumar 
15467813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
15476d3f922cSGeorgi Djakov 	supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
15486d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
15496d3f922cSGeorgi Djakov 	icc_size = sizeof(*opp->bandwidth) * table->path_count;
15507813dd6fSViresh Kumar 
15517813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
15526d3f922cSGeorgi Djakov 	opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
15536d3f922cSGeorgi Djakov 
15547813dd6fSViresh Kumar 	if (!opp)
15557813dd6fSViresh Kumar 		return NULL;
15567813dd6fSViresh Kumar 
15577813dd6fSViresh Kumar 	/* Put the supplies at the end of the OPP structure as an empty array */
15587813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
15596d3f922cSGeorgi Djakov 	if (icc_size)
15606d3f922cSGeorgi Djakov 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
15617813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
15627813dd6fSViresh Kumar 
15637813dd6fSViresh Kumar 	return opp;
15647813dd6fSViresh Kumar }
15657813dd6fSViresh Kumar 
15667813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
15677813dd6fSViresh Kumar 					 struct opp_table *opp_table)
15687813dd6fSViresh Kumar {
15697813dd6fSViresh Kumar 	struct regulator *reg;
15707813dd6fSViresh Kumar 	int i;
15717813dd6fSViresh Kumar 
157290e3577bSViresh Kumar 	if (!opp_table->regulators)
157390e3577bSViresh Kumar 		return true;
157490e3577bSViresh Kumar 
15757813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
15767813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
15777813dd6fSViresh Kumar 
15787813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
15797813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
15807813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
15817813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
15827813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
15837813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
15847813dd6fSViresh Kumar 			return false;
15857813dd6fSViresh Kumar 		}
15867813dd6fSViresh Kumar 	}
15877813dd6fSViresh Kumar 
15887813dd6fSViresh Kumar 	return true;
15897813dd6fSViresh Kumar }
15907813dd6fSViresh Kumar 
15916c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
15926c591eecSSaravana Kannan {
15936c591eecSSaravana Kannan 	if (opp1->rate != opp2->rate)
15946c591eecSSaravana Kannan 		return opp1->rate < opp2->rate ? -1 : 1;
15956d3f922cSGeorgi Djakov 	if (opp1->bandwidth && opp2->bandwidth &&
15966d3f922cSGeorgi Djakov 	    opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
15976d3f922cSGeorgi Djakov 		return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
15986c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
15996c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
16006c591eecSSaravana Kannan 	return 0;
16016c591eecSSaravana Kannan }
16026c591eecSSaravana Kannan 
1603a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1604a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1605a1e8c136SViresh Kumar 			     struct list_head **head)
1606a1e8c136SViresh Kumar {
1607a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
16086c591eecSSaravana Kannan 	int opp_cmp;
1609a1e8c136SViresh Kumar 
1610a1e8c136SViresh Kumar 	/*
1611a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1612a1e8c136SViresh Kumar 	 * already present.
1613a1e8c136SViresh Kumar 	 *
1614a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1615a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1616a1e8c136SViresh Kumar 	 * loop.
1617a1e8c136SViresh Kumar 	 */
1618a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
16196c591eecSSaravana Kannan 		opp_cmp = _opp_compare_key(new_opp, opp);
16206c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1621a1e8c136SViresh Kumar 			*head = &opp->node;
1622a1e8c136SViresh Kumar 			continue;
1623a1e8c136SViresh Kumar 		}
1624a1e8c136SViresh Kumar 
16256c591eecSSaravana Kannan 		if (opp_cmp < 0)
1626a1e8c136SViresh Kumar 			return 0;
1627a1e8c136SViresh Kumar 
1628a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1629a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1630a1e8c136SViresh Kumar 			 __func__, opp->rate, opp->supplies[0].u_volt,
1631a1e8c136SViresh Kumar 			 opp->available, new_opp->rate,
1632a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1633a1e8c136SViresh Kumar 
1634a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1635a1e8c136SViresh Kumar 		return opp->available &&
1636a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1637a1e8c136SViresh Kumar 	}
1638a1e8c136SViresh Kumar 
1639a1e8c136SViresh Kumar 	return 0;
1640a1e8c136SViresh Kumar }
1641a1e8c136SViresh Kumar 
16427813dd6fSViresh Kumar /*
16437813dd6fSViresh Kumar  * Returns:
16447813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
16457813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
16467813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
16477813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
16487813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
16497813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
16507813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
16517813dd6fSViresh Kumar  */
16527813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1653a1e8c136SViresh Kumar 	     struct opp_table *opp_table, bool rate_not_available)
16547813dd6fSViresh Kumar {
16557813dd6fSViresh Kumar 	struct list_head *head;
1656cf65948dSDmitry Osipenko 	unsigned int i;
16577813dd6fSViresh Kumar 	int ret;
16587813dd6fSViresh Kumar 
16597813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
16607813dd6fSViresh Kumar 	head = &opp_table->opp_list;
16617813dd6fSViresh Kumar 
1662a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1663a1e8c136SViresh Kumar 	if (ret) {
16647813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
16657813dd6fSViresh Kumar 		return ret;
16667813dd6fSViresh Kumar 	}
16677813dd6fSViresh Kumar 
16687813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
16697813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
16707813dd6fSViresh Kumar 
16717813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
16727813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
16737813dd6fSViresh Kumar 
1674a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
16757813dd6fSViresh Kumar 
16767813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
16777813dd6fSViresh Kumar 		new_opp->available = false;
16787813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
16797813dd6fSViresh Kumar 			 __func__, new_opp->rate);
16807813dd6fSViresh Kumar 	}
16817813dd6fSViresh Kumar 
1682cf65948dSDmitry Osipenko 	for (i = 0; i < opp_table->required_opp_count; i++) {
1683cf65948dSDmitry Osipenko 		if (new_opp->required_opps[i]->available)
1684cf65948dSDmitry Osipenko 			continue;
1685cf65948dSDmitry Osipenko 
1686cf65948dSDmitry Osipenko 		new_opp->available = false;
1687cf65948dSDmitry Osipenko 		dev_warn(dev, "%s: OPP not supported by required OPP %pOF (%lu)\n",
1688cf65948dSDmitry Osipenko 			 __func__, new_opp->required_opps[i]->np, new_opp->rate);
1689cf65948dSDmitry Osipenko 		break;
1690cf65948dSDmitry Osipenko 	}
1691cf65948dSDmitry Osipenko 
16927813dd6fSViresh Kumar 	return 0;
16937813dd6fSViresh Kumar }
16947813dd6fSViresh Kumar 
16957813dd6fSViresh Kumar /**
16967813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
16977813dd6fSViresh Kumar  * @opp_table:	OPP table
16987813dd6fSViresh Kumar  * @dev:	device for which we do this operation
16997813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
17007813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
17017813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
17027813dd6fSViresh Kumar  *
17037813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
17047813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
17057813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
17067813dd6fSViresh Kumar  *
17077813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
17087813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
17097813dd6fSViresh Kumar  *
17107813dd6fSViresh Kumar  * Return:
17117813dd6fSViresh Kumar  * 0		On success OR
17127813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
17137813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
17147813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
17157813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
17167813dd6fSViresh Kumar  */
17177813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
17187813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
17197813dd6fSViresh Kumar {
17207813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
17217813dd6fSViresh Kumar 	unsigned long tol;
17227813dd6fSViresh Kumar 	int ret;
17237813dd6fSViresh Kumar 
17247813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
17257813dd6fSViresh Kumar 	if (!new_opp)
17267813dd6fSViresh Kumar 		return -ENOMEM;
17277813dd6fSViresh Kumar 
17287813dd6fSViresh Kumar 	/* populate the opp table */
17297813dd6fSViresh Kumar 	new_opp->rate = freq;
17307813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
17317813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
17327813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
17337813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
17347813dd6fSViresh Kumar 	new_opp->available = true;
17357813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
17367813dd6fSViresh Kumar 
1737a1e8c136SViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table, false);
17387813dd6fSViresh Kumar 	if (ret) {
17397813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
17407813dd6fSViresh Kumar 		if (ret == -EBUSY)
17417813dd6fSViresh Kumar 			ret = 0;
17427813dd6fSViresh Kumar 		goto free_opp;
17437813dd6fSViresh Kumar 	}
17447813dd6fSViresh Kumar 
17457813dd6fSViresh Kumar 	/*
17467813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
17477813dd6fSViresh Kumar 	 * frequency/voltage list.
17487813dd6fSViresh Kumar 	 */
17497813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
17507813dd6fSViresh Kumar 	return 0;
17517813dd6fSViresh Kumar 
17527813dd6fSViresh Kumar free_opp:
17537813dd6fSViresh Kumar 	_opp_free(new_opp);
17547813dd6fSViresh Kumar 
17557813dd6fSViresh Kumar 	return ret;
17567813dd6fSViresh Kumar }
17577813dd6fSViresh Kumar 
17587813dd6fSViresh Kumar /**
17597813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw() - Set supported platforms
17607813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
17617813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
17627813dd6fSViresh Kumar  * @count: Number of elements in the array.
17637813dd6fSViresh Kumar  *
17647813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
17657813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
17667813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
17677813dd6fSViresh Kumar  * property.
17687813dd6fSViresh Kumar  */
17697813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
17707813dd6fSViresh Kumar 			const u32 *versions, unsigned int count)
17717813dd6fSViresh Kumar {
17727813dd6fSViresh Kumar 	struct opp_table *opp_table;
17737813dd6fSViresh Kumar 
177432439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1775dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1776dd461cd9SStephan Gerhold 		return opp_table;
17777813dd6fSViresh Kumar 
17787813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
17797813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
17807813dd6fSViresh Kumar 
178125419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
178225419de1SViresh Kumar 	if (opp_table->supported_hw)
178325419de1SViresh Kumar 		return opp_table;
17847813dd6fSViresh Kumar 
17857813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
17867813dd6fSViresh Kumar 					GFP_KERNEL);
17877813dd6fSViresh Kumar 	if (!opp_table->supported_hw) {
178825419de1SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
178925419de1SViresh Kumar 		return ERR_PTR(-ENOMEM);
17907813dd6fSViresh Kumar 	}
17917813dd6fSViresh Kumar 
17927813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
17937813dd6fSViresh Kumar 
17947813dd6fSViresh Kumar 	return opp_table;
17957813dd6fSViresh Kumar }
17967813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
17977813dd6fSViresh Kumar 
17987813dd6fSViresh Kumar /**
17997813dd6fSViresh Kumar  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
18007813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
18017813dd6fSViresh Kumar  *
18027813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
18037813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
18047813dd6fSViresh Kumar  * will not be freed.
18057813dd6fSViresh Kumar  */
18067813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
18077813dd6fSViresh Kumar {
1808c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
1809c7bf8758SViresh Kumar 		return;
1810c7bf8758SViresh Kumar 
18117813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
18127813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
18137813dd6fSViresh Kumar 
18147813dd6fSViresh Kumar 	kfree(opp_table->supported_hw);
18157813dd6fSViresh Kumar 	opp_table->supported_hw = NULL;
18167813dd6fSViresh Kumar 	opp_table->supported_hw_count = 0;
18177813dd6fSViresh Kumar 
18187813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
18197813dd6fSViresh Kumar }
18207813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
18217813dd6fSViresh Kumar 
18227813dd6fSViresh Kumar /**
18237813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name() - Set prop-extn name
18247813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
18257813dd6fSViresh Kumar  * @name: name to postfix to properties.
18267813dd6fSViresh Kumar  *
18277813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
18287813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
18297813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
18307813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
18317813dd6fSViresh Kumar  */
18327813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
18337813dd6fSViresh Kumar {
18347813dd6fSViresh Kumar 	struct opp_table *opp_table;
18357813dd6fSViresh Kumar 
183632439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1837dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1838dd461cd9SStephan Gerhold 		return opp_table;
18397813dd6fSViresh Kumar 
18407813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
18417813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
18427813dd6fSViresh Kumar 
1843878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
1844878ec1a9SViresh Kumar 	if (opp_table->prop_name)
1845878ec1a9SViresh Kumar 		return opp_table;
18467813dd6fSViresh Kumar 
18477813dd6fSViresh Kumar 	opp_table->prop_name = kstrdup(name, GFP_KERNEL);
18487813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
1849878ec1a9SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
1850878ec1a9SViresh Kumar 		return ERR_PTR(-ENOMEM);
18517813dd6fSViresh Kumar 	}
18527813dd6fSViresh Kumar 
18537813dd6fSViresh Kumar 	return opp_table;
18547813dd6fSViresh Kumar }
18557813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
18567813dd6fSViresh Kumar 
18577813dd6fSViresh Kumar /**
18587813dd6fSViresh Kumar  * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
18597813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
18607813dd6fSViresh Kumar  *
18617813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
18627813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
18637813dd6fSViresh Kumar  * will not be freed.
18647813dd6fSViresh Kumar  */
18657813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
18667813dd6fSViresh Kumar {
1867c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
1868c7bf8758SViresh Kumar 		return;
1869c7bf8758SViresh Kumar 
18707813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
18717813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
18727813dd6fSViresh Kumar 
18737813dd6fSViresh Kumar 	kfree(opp_table->prop_name);
18747813dd6fSViresh Kumar 	opp_table->prop_name = NULL;
18757813dd6fSViresh Kumar 
18767813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
18777813dd6fSViresh Kumar }
18787813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
18797813dd6fSViresh Kumar 
18807813dd6fSViresh Kumar /**
18817813dd6fSViresh Kumar  * dev_pm_opp_set_regulators() - Set regulator names for the device
18827813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
18837813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
18847813dd6fSViresh Kumar  * @count: Number of regulators.
18857813dd6fSViresh Kumar  *
18867813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
18877813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
18887813dd6fSViresh Kumar  * well.
18897813dd6fSViresh Kumar  *
18907813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
18917813dd6fSViresh Kumar  */
18927813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
18937813dd6fSViresh Kumar 					    const char * const names[],
18947813dd6fSViresh Kumar 					    unsigned int count)
18957813dd6fSViresh Kumar {
189638bb3439SViresh Kumar 	struct dev_pm_opp_supply *supplies;
18977813dd6fSViresh Kumar 	struct opp_table *opp_table;
18987813dd6fSViresh Kumar 	struct regulator *reg;
18997813dd6fSViresh Kumar 	int ret, i;
19007813dd6fSViresh Kumar 
190132439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1902dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1903dd461cd9SStephan Gerhold 		return opp_table;
19047813dd6fSViresh Kumar 
19057813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
19067813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
19077813dd6fSViresh Kumar 		ret = -EBUSY;
19087813dd6fSViresh Kumar 		goto err;
19097813dd6fSViresh Kumar 	}
19107813dd6fSViresh Kumar 
1911779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
1912779b783cSViresh Kumar 	if (opp_table->regulators)
1913779b783cSViresh Kumar 		return opp_table;
19147813dd6fSViresh Kumar 
19157813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
19167813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
19177813dd6fSViresh Kumar 					      GFP_KERNEL);
19187813dd6fSViresh Kumar 	if (!opp_table->regulators) {
19197813dd6fSViresh Kumar 		ret = -ENOMEM;
19207813dd6fSViresh Kumar 		goto err;
19217813dd6fSViresh Kumar 	}
19227813dd6fSViresh Kumar 
19237813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
19247813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
19257813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
19267813dd6fSViresh Kumar 			ret = PTR_ERR(reg);
19277813dd6fSViresh Kumar 			if (ret != -EPROBE_DEFER)
19287813dd6fSViresh Kumar 				dev_err(dev, "%s: no regulator (%s) found: %d\n",
19297813dd6fSViresh Kumar 					__func__, names[i], ret);
19307813dd6fSViresh Kumar 			goto free_regulators;
19317813dd6fSViresh Kumar 		}
19327813dd6fSViresh Kumar 
19337813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
19347813dd6fSViresh Kumar 	}
19357813dd6fSViresh Kumar 
19367813dd6fSViresh Kumar 	opp_table->regulator_count = count;
19377813dd6fSViresh Kumar 
193838bb3439SViresh Kumar 	supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
193938bb3439SViresh Kumar 	if (!supplies) {
194038bb3439SViresh Kumar 		ret = -ENOMEM;
19417813dd6fSViresh Kumar 		goto free_regulators;
194238bb3439SViresh Kumar 	}
194338bb3439SViresh Kumar 
194438bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
194538bb3439SViresh Kumar 	opp_table->sod_supplies = supplies;
194638bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
194738bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = supplies;
194838bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = supplies + count;
194938bb3439SViresh Kumar 	}
195038bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
19517813dd6fSViresh Kumar 
19527813dd6fSViresh Kumar 	return opp_table;
19537813dd6fSViresh Kumar 
19547813dd6fSViresh Kumar free_regulators:
195524957db1SMarek Szyprowski 	while (i != 0)
195624957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
19577813dd6fSViresh Kumar 
19587813dd6fSViresh Kumar 	kfree(opp_table->regulators);
19597813dd6fSViresh Kumar 	opp_table->regulators = NULL;
196046f48acaSViresh Kumar 	opp_table->regulator_count = -1;
19617813dd6fSViresh Kumar err:
19627813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
19637813dd6fSViresh Kumar 
19647813dd6fSViresh Kumar 	return ERR_PTR(ret);
19657813dd6fSViresh Kumar }
19667813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
19677813dd6fSViresh Kumar 
19687813dd6fSViresh Kumar /**
19697813dd6fSViresh Kumar  * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
19707813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
19717813dd6fSViresh Kumar  */
19727813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table)
19737813dd6fSViresh Kumar {
19747813dd6fSViresh Kumar 	int i;
19757813dd6fSViresh Kumar 
1976c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
1977c7bf8758SViresh Kumar 		return;
1978c7bf8758SViresh Kumar 
1979779b783cSViresh Kumar 	if (!opp_table->regulators)
1980779b783cSViresh Kumar 		goto put_opp_table;
19817813dd6fSViresh Kumar 
19827813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
19837813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
19847813dd6fSViresh Kumar 
198572f80ce4SViresh Kumar 	if (opp_table->enabled) {
19868d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
19878d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
19888d45719cSKamil Konieczny 	}
19898d45719cSKamil Konieczny 
199024957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
19917813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
19927813dd6fSViresh Kumar 
199338bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
199438bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
199538bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = NULL;
199638bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = NULL;
199738bb3439SViresh Kumar 	}
199838bb3439SViresh Kumar 
199938bb3439SViresh Kumar 	kfree(opp_table->sod_supplies);
200038bb3439SViresh Kumar 	opp_table->sod_supplies = NULL;
200138bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
20027813dd6fSViresh Kumar 
20037813dd6fSViresh Kumar 	kfree(opp_table->regulators);
20047813dd6fSViresh Kumar 	opp_table->regulators = NULL;
200546f48acaSViresh Kumar 	opp_table->regulator_count = -1;
20067813dd6fSViresh Kumar 
2007779b783cSViresh Kumar put_opp_table:
20087813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
20097813dd6fSViresh Kumar }
20107813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
20117813dd6fSViresh Kumar 
20127813dd6fSViresh Kumar /**
20137813dd6fSViresh Kumar  * dev_pm_opp_set_clkname() - Set clk name for the device
20147813dd6fSViresh Kumar  * @dev: Device for which clk name is being set.
20157813dd6fSViresh Kumar  * @name: Clk name.
20167813dd6fSViresh Kumar  *
20177813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointer to the
20187813dd6fSViresh Kumar  * clock for the device. Simple cases work fine without using this routine (i.e.
20197813dd6fSViresh Kumar  * by passing connection-id as NULL), but for a device with multiple clocks
20207813dd6fSViresh Kumar  * available, the OPP core needs to know the exact name of the clk to use.
20217813dd6fSViresh Kumar  *
20227813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
20237813dd6fSViresh Kumar  */
20247813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
20257813dd6fSViresh Kumar {
20267813dd6fSViresh Kumar 	struct opp_table *opp_table;
20277813dd6fSViresh Kumar 	int ret;
20287813dd6fSViresh Kumar 
202932439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2030dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2031dd461cd9SStephan Gerhold 		return opp_table;
20327813dd6fSViresh Kumar 
20337813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
20347813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
20357813dd6fSViresh Kumar 		ret = -EBUSY;
20367813dd6fSViresh Kumar 		goto err;
20377813dd6fSViresh Kumar 	}
20387813dd6fSViresh Kumar 
203932439ac7SViresh Kumar 	/* clk shouldn't be initialized at this point */
204032439ac7SViresh Kumar 	if (WARN_ON(opp_table->clk)) {
204132439ac7SViresh Kumar 		ret = -EBUSY;
204232439ac7SViresh Kumar 		goto err;
204332439ac7SViresh Kumar 	}
20447813dd6fSViresh Kumar 
20457813dd6fSViresh Kumar 	/* Find clk for the device */
20467813dd6fSViresh Kumar 	opp_table->clk = clk_get(dev, name);
20477813dd6fSViresh Kumar 	if (IS_ERR(opp_table->clk)) {
20487813dd6fSViresh Kumar 		ret = PTR_ERR(opp_table->clk);
20497813dd6fSViresh Kumar 		if (ret != -EPROBE_DEFER) {
20507813dd6fSViresh Kumar 			dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
20517813dd6fSViresh Kumar 				ret);
20527813dd6fSViresh Kumar 		}
20537813dd6fSViresh Kumar 		goto err;
20547813dd6fSViresh Kumar 	}
20557813dd6fSViresh Kumar 
20567813dd6fSViresh Kumar 	return opp_table;
20577813dd6fSViresh Kumar 
20587813dd6fSViresh Kumar err:
20597813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
20607813dd6fSViresh Kumar 
20617813dd6fSViresh Kumar 	return ERR_PTR(ret);
20627813dd6fSViresh Kumar }
20637813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
20647813dd6fSViresh Kumar 
20657813dd6fSViresh Kumar /**
20667813dd6fSViresh Kumar  * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
20677813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
20687813dd6fSViresh Kumar  */
20697813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table)
20707813dd6fSViresh Kumar {
2071c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2072c7bf8758SViresh Kumar 		return;
2073c7bf8758SViresh Kumar 
20747813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
20757813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
20767813dd6fSViresh Kumar 
20777813dd6fSViresh Kumar 	clk_put(opp_table->clk);
20787813dd6fSViresh Kumar 	opp_table->clk = ERR_PTR(-EINVAL);
20797813dd6fSViresh Kumar 
20807813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
20817813dd6fSViresh Kumar }
20827813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
20837813dd6fSViresh Kumar 
20847813dd6fSViresh Kumar /**
20857813dd6fSViresh Kumar  * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
20867813dd6fSViresh Kumar  * @dev: Device for which the helper is getting registered.
20877813dd6fSViresh Kumar  * @set_opp: Custom set OPP helper.
20887813dd6fSViresh Kumar  *
20897813dd6fSViresh Kumar  * This is useful to support complex platforms (like platforms with multiple
20907813dd6fSViresh Kumar  * regulators per device), instead of the generic OPP set rate helper.
20917813dd6fSViresh Kumar  *
20927813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
20937813dd6fSViresh Kumar  */
20947813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
20957813dd6fSViresh Kumar 			int (*set_opp)(struct dev_pm_set_opp_data *data))
20967813dd6fSViresh Kumar {
209738bb3439SViresh Kumar 	struct dev_pm_set_opp_data *data;
20987813dd6fSViresh Kumar 	struct opp_table *opp_table;
20997813dd6fSViresh Kumar 
21007813dd6fSViresh Kumar 	if (!set_opp)
21017813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
21027813dd6fSViresh Kumar 
210332439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
210447efcbcbSViresh Kumar 	if (IS_ERR(opp_table))
2105dd461cd9SStephan Gerhold 		return opp_table;
21067813dd6fSViresh Kumar 
21077813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
21087813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
21095019acc6SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
21105019acc6SViresh Kumar 		return ERR_PTR(-EBUSY);
21117813dd6fSViresh Kumar 	}
21127813dd6fSViresh Kumar 
21135019acc6SViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
211438bb3439SViresh Kumar 	if (opp_table->set_opp)
211538bb3439SViresh Kumar 		return opp_table;
211638bb3439SViresh Kumar 
211738bb3439SViresh Kumar 	data = kzalloc(sizeof(*data), GFP_KERNEL);
211838bb3439SViresh Kumar 	if (!data)
211938bb3439SViresh Kumar 		return ERR_PTR(-ENOMEM);
212038bb3439SViresh Kumar 
212138bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
212238bb3439SViresh Kumar 	opp_table->set_opp_data = data;
212338bb3439SViresh Kumar 	if (opp_table->sod_supplies) {
212438bb3439SViresh Kumar 		data->old_opp.supplies = opp_table->sod_supplies;
212538bb3439SViresh Kumar 		data->new_opp.supplies = opp_table->sod_supplies +
212638bb3439SViresh Kumar 					 opp_table->regulator_count;
212738bb3439SViresh Kumar 	}
212838bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
212938bb3439SViresh Kumar 
21307813dd6fSViresh Kumar 	opp_table->set_opp = set_opp;
21317813dd6fSViresh Kumar 
21327813dd6fSViresh Kumar 	return opp_table;
21337813dd6fSViresh Kumar }
21347813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
21357813dd6fSViresh Kumar 
21367813dd6fSViresh Kumar /**
2137604a7aebSViresh Kumar  * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
21387813dd6fSViresh Kumar  *					   set_opp helper
21397813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
21407813dd6fSViresh Kumar  *
21417813dd6fSViresh Kumar  * Release resources blocked for platform specific set_opp helper.
21427813dd6fSViresh Kumar  */
2143604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
21447813dd6fSViresh Kumar {
2145c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2146c7bf8758SViresh Kumar 		return;
2147c7bf8758SViresh Kumar 
21487813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
21497813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
21507813dd6fSViresh Kumar 
21517813dd6fSViresh Kumar 	opp_table->set_opp = NULL;
215238bb3439SViresh Kumar 
215338bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
215438bb3439SViresh Kumar 	kfree(opp_table->set_opp_data);
215538bb3439SViresh Kumar 	opp_table->set_opp_data = NULL;
215638bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
215738bb3439SViresh Kumar 
21587813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
21597813dd6fSViresh Kumar }
2160604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
21617813dd6fSViresh Kumar 
2162a3c47af6SDmitry Osipenko static void devm_pm_opp_unregister_set_opp_helper(void *data)
2163a3c47af6SDmitry Osipenko {
2164a3c47af6SDmitry Osipenko 	dev_pm_opp_unregister_set_opp_helper(data);
2165a3c47af6SDmitry Osipenko }
2166a3c47af6SDmitry Osipenko 
2167a3c47af6SDmitry Osipenko /**
2168a3c47af6SDmitry Osipenko  * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2169a3c47af6SDmitry Osipenko  * @dev: Device for which the helper is getting registered.
2170a3c47af6SDmitry Osipenko  * @set_opp: Custom set OPP helper.
2171a3c47af6SDmitry Osipenko  *
2172a3c47af6SDmitry Osipenko  * This is a resource-managed version of dev_pm_opp_register_set_opp_helper().
2173a3c47af6SDmitry Osipenko  *
2174a3c47af6SDmitry Osipenko  * Return: pointer to 'struct opp_table' on success and errorno otherwise.
2175a3c47af6SDmitry Osipenko  */
2176a3c47af6SDmitry Osipenko struct opp_table *
2177a3c47af6SDmitry Osipenko devm_pm_opp_register_set_opp_helper(struct device *dev,
2178a3c47af6SDmitry Osipenko 				    int (*set_opp)(struct dev_pm_set_opp_data *data))
2179a3c47af6SDmitry Osipenko {
2180a3c47af6SDmitry Osipenko 	struct opp_table *opp_table;
2181a3c47af6SDmitry Osipenko 	int err;
2182a3c47af6SDmitry Osipenko 
2183a3c47af6SDmitry Osipenko 	opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp);
2184a3c47af6SDmitry Osipenko 	if (IS_ERR(opp_table))
2185a3c47af6SDmitry Osipenko 		return opp_table;
2186a3c47af6SDmitry Osipenko 
2187a3c47af6SDmitry Osipenko 	err = devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper,
2188a3c47af6SDmitry Osipenko 				       opp_table);
2189a3c47af6SDmitry Osipenko 	if (err)
2190a3c47af6SDmitry Osipenko 		return ERR_PTR(err);
2191a3c47af6SDmitry Osipenko 
2192a3c47af6SDmitry Osipenko 	return opp_table;
2193a3c47af6SDmitry Osipenko }
2194a3c47af6SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper);
2195a3c47af6SDmitry Osipenko 
21966319aee1SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
21976319aee1SViresh Kumar {
21986319aee1SViresh Kumar 	int index;
21996319aee1SViresh Kumar 
2200cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2201cb60e960SViresh Kumar 		return;
2202cb60e960SViresh Kumar 
22036319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
22046319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
22056319aee1SViresh Kumar 			continue;
22066319aee1SViresh Kumar 
22076319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
22086319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
22096319aee1SViresh Kumar 	}
2210c0ab9e08SViresh Kumar 
2211c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2212c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
22136319aee1SViresh Kumar }
22146319aee1SViresh Kumar 
22157813dd6fSViresh Kumar /**
22166319aee1SViresh Kumar  * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
22176319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
22186319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
221917a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
22204f018bc0SViresh Kumar  *
22214f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
22224f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
22234f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
22244f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
22256319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
22266319aee1SViresh Kumar  * we don't need to support that separately.
22274f018bc0SViresh Kumar  *
22284f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
22296319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
22304f018bc0SViresh Kumar  *
22316319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
22326319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2233baea35e4SViresh Kumar  *
2234baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2235baea35e4SViresh Kumar  * "required-opps" are added in DT.
22364f018bc0SViresh Kumar  */
223717a8f868SViresh Kumar struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
223817a8f868SViresh Kumar 		const char **names, struct device ***virt_devs)
22394f018bc0SViresh Kumar {
22404f018bc0SViresh Kumar 	struct opp_table *opp_table;
22416319aee1SViresh Kumar 	struct device *virt_dev;
2242baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
22436319aee1SViresh Kumar 	const char **name = names;
22444f018bc0SViresh Kumar 
224532439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2246dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2247dd461cd9SStephan Gerhold 		return opp_table;
22484f018bc0SViresh Kumar 
2249cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2250cb60e960SViresh Kumar 		return opp_table;
22514f018bc0SViresh Kumar 
22526319aee1SViresh Kumar 	/*
22536319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
22546319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
22556319aee1SViresh Kumar 	 * table is added.
22566319aee1SViresh Kumar 	 */
22576319aee1SViresh Kumar 	if (!opp_table->required_opp_count) {
22586319aee1SViresh Kumar 		ret = -EPROBE_DEFER;
22596319aee1SViresh Kumar 		goto put_table;
22606319aee1SViresh Kumar 	}
22616319aee1SViresh Kumar 
22624f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
22634f018bc0SViresh Kumar 
2264c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2265c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2266c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2267c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2268c0ab9e08SViresh Kumar 		goto unlock;
22694f018bc0SViresh Kumar 
22706319aee1SViresh Kumar 	while (*name) {
22716319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
22726319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
22736319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
22746319aee1SViresh Kumar 			goto err;
22756319aee1SViresh Kumar 		}
22764f018bc0SViresh Kumar 
22776319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
22786319aee1SViresh Kumar 		if (IS_ERR(virt_dev)) {
22796319aee1SViresh Kumar 			ret = PTR_ERR(virt_dev);
22806319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
22816319aee1SViresh Kumar 			goto err;
22824f018bc0SViresh Kumar 		}
22834f018bc0SViresh Kumar 
22844f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2285baea35e4SViresh Kumar 		index++;
22866319aee1SViresh Kumar 		name++;
22876319aee1SViresh Kumar 	}
22886319aee1SViresh Kumar 
228917a8f868SViresh Kumar 	if (virt_devs)
229017a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
22914f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
22924f018bc0SViresh Kumar 
22934f018bc0SViresh Kumar 	return opp_table;
22946319aee1SViresh Kumar 
22956319aee1SViresh Kumar err:
22966319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
2297c0ab9e08SViresh Kumar unlock:
22986319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
22996319aee1SViresh Kumar 
23006319aee1SViresh Kumar put_table:
23016319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
23026319aee1SViresh Kumar 
23036319aee1SViresh Kumar 	return ERR_PTR(ret);
23044f018bc0SViresh Kumar }
23056319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
23064f018bc0SViresh Kumar 
23074f018bc0SViresh Kumar /**
23086319aee1SViresh Kumar  * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
23096319aee1SViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
23104f018bc0SViresh Kumar  *
23116319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
23126319aee1SViresh Kumar  * OPP table.
23134f018bc0SViresh Kumar  */
23146319aee1SViresh Kumar void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
23154f018bc0SViresh Kumar {
2316c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2317c7bf8758SViresh Kumar 		return;
2318c7bf8758SViresh Kumar 
23194f018bc0SViresh Kumar 	/*
23204f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
23214f018bc0SViresh Kumar 	 * used in parallel.
23224f018bc0SViresh Kumar 	 */
23234f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
23246319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
23254f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23264f018bc0SViresh Kumar 
23276319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
23284f018bc0SViresh Kumar }
23296319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
23304f018bc0SViresh Kumar 
2331b4b9e223SDmitry Osipenko static void devm_pm_opp_detach_genpd(void *data)
2332b4b9e223SDmitry Osipenko {
2333b4b9e223SDmitry Osipenko 	dev_pm_opp_detach_genpd(data);
2334b4b9e223SDmitry Osipenko }
2335b4b9e223SDmitry Osipenko 
2336b4b9e223SDmitry Osipenko /**
2337b4b9e223SDmitry Osipenko  * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual
2338b4b9e223SDmitry Osipenko  *			      device pointer
2339b4b9e223SDmitry Osipenko  * @dev: Consumer device for which the genpd is getting attached.
2340b4b9e223SDmitry Osipenko  * @names: Null terminated array of pointers containing names of genpd to attach.
2341b4b9e223SDmitry Osipenko  * @virt_devs: Pointer to return the array of virtual devices.
2342b4b9e223SDmitry Osipenko  *
2343b4b9e223SDmitry Osipenko  * This is a resource-managed version of dev_pm_opp_attach_genpd().
2344b4b9e223SDmitry Osipenko  *
2345b4b9e223SDmitry Osipenko  * Return: pointer to 'struct opp_table' on success and errorno otherwise.
2346b4b9e223SDmitry Osipenko  */
2347b4b9e223SDmitry Osipenko struct opp_table *
2348b4b9e223SDmitry Osipenko devm_pm_opp_attach_genpd(struct device *dev, const char **names,
2349b4b9e223SDmitry Osipenko 			 struct device ***virt_devs)
2350b4b9e223SDmitry Osipenko {
2351b4b9e223SDmitry Osipenko 	struct opp_table *opp_table;
2352b4b9e223SDmitry Osipenko 	int err;
2353b4b9e223SDmitry Osipenko 
2354b4b9e223SDmitry Osipenko 	opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs);
2355b4b9e223SDmitry Osipenko 	if (IS_ERR(opp_table))
2356b4b9e223SDmitry Osipenko 		return opp_table;
2357b4b9e223SDmitry Osipenko 
2358b4b9e223SDmitry Osipenko 	err = devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd,
2359b4b9e223SDmitry Osipenko 				       opp_table);
2360b4b9e223SDmitry Osipenko 	if (err)
2361b4b9e223SDmitry Osipenko 		return ERR_PTR(err);
2362b4b9e223SDmitry Osipenko 
2363b4b9e223SDmitry Osipenko 	return opp_table;
2364b4b9e223SDmitry Osipenko }
2365b4b9e223SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd);
2366b4b9e223SDmitry Osipenko 
23674f018bc0SViresh Kumar /**
2368c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2369c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2370c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2371c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2372c8a59103SViresh Kumar  *
2373c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2374c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2375c8a59103SViresh Kumar  * performance state set to @pstate.
2376c8a59103SViresh Kumar  *
2377c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2378c8a59103SViresh Kumar  * value on errors.
2379c8a59103SViresh Kumar  */
2380c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2381c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2382c8a59103SViresh Kumar 				       unsigned int pstate)
2383c8a59103SViresh Kumar {
2384c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2385c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2386c8a59103SViresh Kumar 	int i;
2387c8a59103SViresh Kumar 
2388c8a59103SViresh Kumar 	/*
2389c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2390c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2391c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2392c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2393c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2394c8a59103SViresh Kumar 	 */
2395f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2396c8a59103SViresh Kumar 		return pstate;
2397c8a59103SViresh Kumar 
2398c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2399c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2400c8a59103SViresh Kumar 			break;
2401c8a59103SViresh Kumar 	}
2402c8a59103SViresh Kumar 
2403c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2404c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2405c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2406c8a59103SViresh Kumar 		return -EINVAL;
2407c8a59103SViresh Kumar 	}
2408c8a59103SViresh Kumar 
2409c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2410c8a59103SViresh Kumar 
2411c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
2412c8a59103SViresh Kumar 		if (opp->pstate == pstate) {
2413c8a59103SViresh Kumar 			dest_pstate = opp->required_opps[i]->pstate;
2414c8a59103SViresh Kumar 			goto unlock;
2415c8a59103SViresh Kumar 		}
2416c8a59103SViresh Kumar 	}
2417c8a59103SViresh Kumar 
2418c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2419c8a59103SViresh Kumar 	       dst_table);
2420c8a59103SViresh Kumar 
2421c8a59103SViresh Kumar unlock:
2422c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2423c8a59103SViresh Kumar 
2424c8a59103SViresh Kumar 	return dest_pstate;
2425c8a59103SViresh Kumar }
2426c8a59103SViresh Kumar 
2427c8a59103SViresh Kumar /**
24287813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
24297813dd6fSViresh Kumar  * @dev:	device for which we do this operation
24307813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
24317813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
24327813dd6fSViresh Kumar  *
24337813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
24347813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
24357813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
24367813dd6fSViresh Kumar  *
24377813dd6fSViresh Kumar  * Return:
24387813dd6fSViresh Kumar  * 0		On success OR
24397813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
24407813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
24417813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
24427813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
24437813dd6fSViresh Kumar  */
24447813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
24457813dd6fSViresh Kumar {
24467813dd6fSViresh Kumar 	struct opp_table *opp_table;
24477813dd6fSViresh Kumar 	int ret;
24487813dd6fSViresh Kumar 
244932439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2450dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2451dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
24527813dd6fSViresh Kumar 
245346f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
245446f48acaSViresh Kumar 	opp_table->regulator_count = 1;
245546f48acaSViresh Kumar 
24567813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
24570ad8c623SViresh Kumar 	if (ret)
24587813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
24590ad8c623SViresh Kumar 
24607813dd6fSViresh Kumar 	return ret;
24617813dd6fSViresh Kumar }
24627813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
24637813dd6fSViresh Kumar 
24647813dd6fSViresh Kumar /**
24657813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
24667813dd6fSViresh Kumar  * @dev:		device for which we do this operation
24677813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
24687813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
24697813dd6fSViresh Kumar  *
24707813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
24717813dd6fSViresh Kumar  * which is isolated here.
24727813dd6fSViresh Kumar  *
24737813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
24747813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
24757813dd6fSViresh Kumar  * successful.
24767813dd6fSViresh Kumar  */
24777813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
24787813dd6fSViresh Kumar 				 bool availability_req)
24797813dd6fSViresh Kumar {
24807813dd6fSViresh Kumar 	struct opp_table *opp_table;
24817813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
24827813dd6fSViresh Kumar 	int r = 0;
24837813dd6fSViresh Kumar 
24847813dd6fSViresh Kumar 	/* Find the opp_table */
24857813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
24867813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
24877813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
24887813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
24897813dd6fSViresh Kumar 		return r;
24907813dd6fSViresh Kumar 	}
24917813dd6fSViresh Kumar 
24927813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
24937813dd6fSViresh Kumar 
24947813dd6fSViresh Kumar 	/* Do we have the frequency? */
24957813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
24967813dd6fSViresh Kumar 		if (tmp_opp->rate == freq) {
24977813dd6fSViresh Kumar 			opp = tmp_opp;
24987813dd6fSViresh Kumar 			break;
24997813dd6fSViresh Kumar 		}
25007813dd6fSViresh Kumar 	}
25017813dd6fSViresh Kumar 
25027813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
25037813dd6fSViresh Kumar 		r = PTR_ERR(opp);
25047813dd6fSViresh Kumar 		goto unlock;
25057813dd6fSViresh Kumar 	}
25067813dd6fSViresh Kumar 
25077813dd6fSViresh Kumar 	/* Is update really needed? */
25087813dd6fSViresh Kumar 	if (opp->available == availability_req)
25097813dd6fSViresh Kumar 		goto unlock;
25107813dd6fSViresh Kumar 
25117813dd6fSViresh Kumar 	opp->available = availability_req;
25127813dd6fSViresh Kumar 
25137813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
25147813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
25157813dd6fSViresh Kumar 
25167813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
25177813dd6fSViresh Kumar 	if (availability_req)
25187813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
25197813dd6fSViresh Kumar 					     opp);
25207813dd6fSViresh Kumar 	else
25217813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
25227813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
25237813dd6fSViresh Kumar 
25247813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
25257813dd6fSViresh Kumar 	goto put_table;
25267813dd6fSViresh Kumar 
25277813dd6fSViresh Kumar unlock:
25287813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
25297813dd6fSViresh Kumar put_table:
25307813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
25317813dd6fSViresh Kumar 	return r;
25327813dd6fSViresh Kumar }
25337813dd6fSViresh Kumar 
25347813dd6fSViresh Kumar /**
253525cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
253625cb20a2SStephen Boyd  * @dev:		device for which we do this operation
253725cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
253825cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
253925cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
254025cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
254125cb20a2SStephen Boyd  *
254225cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
254325cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
254425cb20a2SStephen Boyd  * successful.
254525cb20a2SStephen Boyd  */
254625cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
254725cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
254825cb20a2SStephen Boyd 			      unsigned long u_volt_max)
254925cb20a2SStephen Boyd 
255025cb20a2SStephen Boyd {
255125cb20a2SStephen Boyd 	struct opp_table *opp_table;
255225cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
255325cb20a2SStephen Boyd 	int r = 0;
255425cb20a2SStephen Boyd 
255525cb20a2SStephen Boyd 	/* Find the opp_table */
255625cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
255725cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
255825cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
255925cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
256025cb20a2SStephen Boyd 		return r;
256125cb20a2SStephen Boyd 	}
256225cb20a2SStephen Boyd 
256325cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
256425cb20a2SStephen Boyd 
256525cb20a2SStephen Boyd 	/* Do we have the frequency? */
256625cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
256725cb20a2SStephen Boyd 		if (tmp_opp->rate == freq) {
256825cb20a2SStephen Boyd 			opp = tmp_opp;
256925cb20a2SStephen Boyd 			break;
257025cb20a2SStephen Boyd 		}
257125cb20a2SStephen Boyd 	}
257225cb20a2SStephen Boyd 
257325cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
257425cb20a2SStephen Boyd 		r = PTR_ERR(opp);
257525cb20a2SStephen Boyd 		goto adjust_unlock;
257625cb20a2SStephen Boyd 	}
257725cb20a2SStephen Boyd 
257825cb20a2SStephen Boyd 	/* Is update really needed? */
257925cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
258025cb20a2SStephen Boyd 		goto adjust_unlock;
258125cb20a2SStephen Boyd 
258225cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
258325cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
258425cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
258525cb20a2SStephen Boyd 
258625cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
258725cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
258825cb20a2SStephen Boyd 
258925cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
259025cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
259125cb20a2SStephen Boyd 				     opp);
259225cb20a2SStephen Boyd 
259325cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
259425cb20a2SStephen Boyd 	goto adjust_put_table;
259525cb20a2SStephen Boyd 
259625cb20a2SStephen Boyd adjust_unlock:
259725cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
259825cb20a2SStephen Boyd adjust_put_table:
259925cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
260025cb20a2SStephen Boyd 	return r;
260125cb20a2SStephen Boyd }
260203649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
260325cb20a2SStephen Boyd 
260425cb20a2SStephen Boyd /**
26057813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
26067813dd6fSViresh Kumar  * @dev:	device for which we do this operation
26077813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
26087813dd6fSViresh Kumar  *
26097813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
26107813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
26117813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
26127813dd6fSViresh Kumar  *
26137813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
26147813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
26157813dd6fSViresh Kumar  * successful.
26167813dd6fSViresh Kumar  */
26177813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
26187813dd6fSViresh Kumar {
26197813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
26207813dd6fSViresh Kumar }
26217813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
26227813dd6fSViresh Kumar 
26237813dd6fSViresh Kumar /**
26247813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
26257813dd6fSViresh Kumar  * @dev:	device for which we do this operation
26267813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
26277813dd6fSViresh Kumar  *
26287813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
26297813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
26307813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
26317813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
26327813dd6fSViresh Kumar  *
26337813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
26347813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
26357813dd6fSViresh Kumar  * successful.
26367813dd6fSViresh Kumar  */
26377813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
26387813dd6fSViresh Kumar {
26397813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
26407813dd6fSViresh Kumar }
26417813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
26427813dd6fSViresh Kumar 
26437813dd6fSViresh Kumar /**
26447813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
26457813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
26467813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
26477813dd6fSViresh Kumar  *
26487813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
26497813dd6fSViresh Kumar  */
26507813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
26517813dd6fSViresh Kumar {
26527813dd6fSViresh Kumar 	struct opp_table *opp_table;
26537813dd6fSViresh Kumar 	int ret;
26547813dd6fSViresh Kumar 
26557813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
26567813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
26577813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
26587813dd6fSViresh Kumar 
26597813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
26607813dd6fSViresh Kumar 
26617813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
26627813dd6fSViresh Kumar 
26637813dd6fSViresh Kumar 	return ret;
26647813dd6fSViresh Kumar }
26657813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
26667813dd6fSViresh Kumar 
26677813dd6fSViresh Kumar /**
26687813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
26697813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
26707813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
26717813dd6fSViresh Kumar  *
26727813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
26737813dd6fSViresh Kumar  */
26747813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
26757813dd6fSViresh Kumar 				   struct notifier_block *nb)
26767813dd6fSViresh Kumar {
26777813dd6fSViresh Kumar 	struct opp_table *opp_table;
26787813dd6fSViresh Kumar 	int ret;
26797813dd6fSViresh Kumar 
26807813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
26817813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
26827813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
26837813dd6fSViresh Kumar 
26847813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
26857813dd6fSViresh Kumar 
26867813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
26877813dd6fSViresh Kumar 
26887813dd6fSViresh Kumar 	return ret;
26897813dd6fSViresh Kumar }
26907813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
26917813dd6fSViresh Kumar 
26928aaf6264SViresh Kumar /**
26938aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
26948aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
26958aaf6264SViresh Kumar  *
26968aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
26978aaf6264SViresh Kumar  * dynamically added entries.
26988aaf6264SViresh Kumar  */
26998aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
27007813dd6fSViresh Kumar {
27017813dd6fSViresh Kumar 	struct opp_table *opp_table;
27027813dd6fSViresh Kumar 
27037813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
27047813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
27057813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
27067813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
27077813dd6fSViresh Kumar 
27087813dd6fSViresh Kumar 		if (error != -ENODEV)
27097813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
27107813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
27117813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
27127813dd6fSViresh Kumar 			     error);
27137813dd6fSViresh Kumar 		return;
27147813dd6fSViresh Kumar 	}
27157813dd6fSViresh Kumar 
2716922ff075SViresh Kumar 	/*
2717922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
2718922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
2719922ff075SViresh Kumar 	 **/
2720922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
2721cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
2722cdd6ed90SViresh Kumar 
2723922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
27247813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
27257813dd6fSViresh Kumar }
27267813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
2727ce8073d8SDmitry Osipenko 
2728ce8073d8SDmitry Osipenko /**
2729ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2730ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
2731ce8073d8SDmitry Osipenko  *
2732ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
2733ce8073d8SDmitry Osipenko  *
2734ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
2735ce8073d8SDmitry Osipenko  */
2736ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
2737ce8073d8SDmitry Osipenko {
2738ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
2739ce8073d8SDmitry Osipenko 	struct regulator *reg;
2740ce8073d8SDmitry Osipenko 	int i, ret = 0;
2741ce8073d8SDmitry Osipenko 
2742ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
2743ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
2744ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
2745ce8073d8SDmitry Osipenko 		return 0;
2746ce8073d8SDmitry Osipenko 
2747ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
2748ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
2749ce8073d8SDmitry Osipenko 		goto put_table;
2750ce8073d8SDmitry Osipenko 
2751ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
2752ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
2753ce8073d8SDmitry Osipenko 		goto put_table;
2754ce8073d8SDmitry Osipenko 
2755ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
2756ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
2757ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
2758ce8073d8SDmitry Osipenko 		if (ret)
2759ce8073d8SDmitry Osipenko 			break;
2760ce8073d8SDmitry Osipenko 	}
2761ce8073d8SDmitry Osipenko put_table:
2762ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
2763ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
2764ce8073d8SDmitry Osipenko 
2765ce8073d8SDmitry Osipenko 	return ret;
2766ce8073d8SDmitry Osipenko }
2767ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
2768