xref: /openbmc/linux/drivers/opp/core.c (revision 298098e55a6fcc176a5af52cd689f33577ead5ca)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27813dd6fSViresh Kumar /*
37813dd6fSViresh Kumar  * Generic OPP Interface
47813dd6fSViresh Kumar  *
57813dd6fSViresh Kumar  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
67813dd6fSViresh Kumar  *	Nishanth Menon
77813dd6fSViresh Kumar  *	Romit Dasgupta
87813dd6fSViresh Kumar  *	Kevin Hilman
97813dd6fSViresh Kumar  */
107813dd6fSViresh Kumar 
117813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
127813dd6fSViresh Kumar 
137813dd6fSViresh Kumar #include <linux/clk.h>
147813dd6fSViresh Kumar #include <linux/errno.h>
157813dd6fSViresh Kumar #include <linux/err.h>
167813dd6fSViresh Kumar #include <linux/device.h>
177813dd6fSViresh Kumar #include <linux/export.h>
18009acd19SViresh Kumar #include <linux/pm_domain.h>
197813dd6fSViresh Kumar #include <linux/regulator/consumer.h>
2011b9b663SViresh Kumar #include <linux/slab.h>
2111b9b663SViresh Kumar #include <linux/xarray.h>
227813dd6fSViresh Kumar 
237813dd6fSViresh Kumar #include "opp.h"
247813dd6fSViresh Kumar 
257813dd6fSViresh Kumar /*
267813dd6fSViresh Kumar  * The root of the list of all opp-tables. All opp_table structures branch off
277813dd6fSViresh Kumar  * from here, with each opp_table containing the list of opps it supports in
287813dd6fSViresh Kumar  * various states of availability.
297813dd6fSViresh Kumar  */
307813dd6fSViresh Kumar LIST_HEAD(opp_tables);
317eba0c76SViresh Kumar 
327eba0c76SViresh Kumar /* OPP tables with uninitialized required OPPs */
337eba0c76SViresh Kumar LIST_HEAD(lazy_opp_tables);
347eba0c76SViresh Kumar 
357813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
367813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
3727c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */
3827c09484SViresh Kumar static bool opp_tables_busy;
397813dd6fSViresh Kumar 
4011b9b663SViresh Kumar /* OPP ID allocator */
4111b9b663SViresh Kumar static DEFINE_XARRAY_ALLOC1(opp_configs);
4211b9b663SViresh Kumar 
439e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
447813dd6fSViresh Kumar {
457813dd6fSViresh Kumar 	struct opp_device *opp_dev;
469e62edacSViresh Kumar 	bool found = false;
477813dd6fSViresh Kumar 
489e62edacSViresh Kumar 	mutex_lock(&opp_table->lock);
497813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
509e62edacSViresh Kumar 		if (opp_dev->dev == dev) {
519e62edacSViresh Kumar 			found = true;
529e62edacSViresh Kumar 			break;
539e62edacSViresh Kumar 		}
547813dd6fSViresh Kumar 
559e62edacSViresh Kumar 	mutex_unlock(&opp_table->lock);
569e62edacSViresh Kumar 	return found;
577813dd6fSViresh Kumar }
587813dd6fSViresh Kumar 
597813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
607813dd6fSViresh Kumar {
617813dd6fSViresh Kumar 	struct opp_table *opp_table;
627813dd6fSViresh Kumar 
637813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
649e62edacSViresh Kumar 		if (_find_opp_dev(dev, opp_table)) {
657813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
667813dd6fSViresh Kumar 			return opp_table;
677813dd6fSViresh Kumar 		}
687813dd6fSViresh Kumar 	}
697813dd6fSViresh Kumar 
707813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
717813dd6fSViresh Kumar }
727813dd6fSViresh Kumar 
737813dd6fSViresh Kumar /**
747813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
757813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
767813dd6fSViresh Kumar  *
777813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
787813dd6fSViresh Kumar  *
797813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
807813dd6fSViresh Kumar  * -EINVAL based on type of error.
817813dd6fSViresh Kumar  *
827813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
837813dd6fSViresh Kumar  */
847813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
857813dd6fSViresh Kumar {
867813dd6fSViresh Kumar 	struct opp_table *opp_table;
877813dd6fSViresh Kumar 
887813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
897813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
907813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
917813dd6fSViresh Kumar 	}
927813dd6fSViresh Kumar 
937813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
947813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
957813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
967813dd6fSViresh Kumar 
977813dd6fSViresh Kumar 	return opp_table;
987813dd6fSViresh Kumar }
997813dd6fSViresh Kumar 
1007813dd6fSViresh Kumar /**
1017813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
1027813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
1037813dd6fSViresh Kumar  *
1047813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
1057813dd6fSViresh Kumar  * return 0
1067813dd6fSViresh Kumar  *
1077813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1087813dd6fSViresh Kumar  */
1097813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1107813dd6fSViresh Kumar {
1117813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1127813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1137813dd6fSViresh Kumar 		return 0;
1147813dd6fSViresh Kumar 	}
1157813dd6fSViresh Kumar 
1167813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1177813dd6fSViresh Kumar }
1187813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1197813dd6fSViresh Kumar 
1207813dd6fSViresh Kumar /**
1214f9a7a1dSLukasz Luba  * dev_pm_opp_get_power() - Gets the power corresponding to an opp
1224f9a7a1dSLukasz Luba  * @opp:	opp for which power has to be returned for
1234f9a7a1dSLukasz Luba  *
1244f9a7a1dSLukasz Luba  * Return: power in micro watt corresponding to the opp, else
1254f9a7a1dSLukasz Luba  * return 0
1264f9a7a1dSLukasz Luba  *
1274f9a7a1dSLukasz Luba  * This is useful only for devices with single power supply.
1284f9a7a1dSLukasz Luba  */
1294f9a7a1dSLukasz Luba unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
1304f9a7a1dSLukasz Luba {
1314f9a7a1dSLukasz Luba 	unsigned long opp_power = 0;
1324f9a7a1dSLukasz Luba 	int i;
1334f9a7a1dSLukasz Luba 
1344f9a7a1dSLukasz Luba 	if (IS_ERR_OR_NULL(opp)) {
1354f9a7a1dSLukasz Luba 		pr_err("%s: Invalid parameters\n", __func__);
1364f9a7a1dSLukasz Luba 		return 0;
1374f9a7a1dSLukasz Luba 	}
1384f9a7a1dSLukasz Luba 	for (i = 0; i < opp->opp_table->regulator_count; i++)
1394f9a7a1dSLukasz Luba 		opp_power += opp->supplies[i].u_watt;
1404f9a7a1dSLukasz Luba 
1414f9a7a1dSLukasz Luba 	return opp_power;
1424f9a7a1dSLukasz Luba }
1434f9a7a1dSLukasz Luba EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
1444f9a7a1dSLukasz Luba 
1454f9a7a1dSLukasz Luba /**
1467813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1477813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1487813dd6fSViresh Kumar  *
1497813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1507813dd6fSViresh Kumar  * return 0
1517813dd6fSViresh Kumar  */
1527813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1537813dd6fSViresh Kumar {
15406a8a059SAndrew-sh.Cheng 	if (IS_ERR_OR_NULL(opp)) {
1557813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1567813dd6fSViresh Kumar 		return 0;
1577813dd6fSViresh Kumar 	}
1587813dd6fSViresh Kumar 
1597813dd6fSViresh Kumar 	return opp->rate;
1607813dd6fSViresh Kumar }
1617813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
1627813dd6fSViresh Kumar 
1637813dd6fSViresh Kumar /**
1645b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
1655b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
1665b93ac54SRajendra Nayak  *
1675b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
1685b93ac54SRajendra Nayak  * return 0.
1695b93ac54SRajendra Nayak  */
1705b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
1715b93ac54SRajendra Nayak {
1725b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1735b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
1745b93ac54SRajendra Nayak 		return 0;
1755b93ac54SRajendra Nayak 	}
1765b93ac54SRajendra Nayak 
1775b93ac54SRajendra Nayak 	return opp->level;
1785b93ac54SRajendra Nayak }
1795b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
1805b93ac54SRajendra Nayak 
1815b93ac54SRajendra Nayak /**
182597ff543SDmitry Osipenko  * dev_pm_opp_get_required_pstate() - Gets the required performance state
183597ff543SDmitry Osipenko  *                                    corresponding to an available opp
184597ff543SDmitry Osipenko  * @opp:	opp for which performance state has to be returned for
185597ff543SDmitry Osipenko  * @index:	index of the required opp
186597ff543SDmitry Osipenko  *
187597ff543SDmitry Osipenko  * Return: performance state read from device tree corresponding to the
188597ff543SDmitry Osipenko  * required opp, else return 0.
189597ff543SDmitry Osipenko  */
190597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
191597ff543SDmitry Osipenko 					    unsigned int index)
192597ff543SDmitry Osipenko {
193597ff543SDmitry Osipenko 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
194597ff543SDmitry Osipenko 	    index >= opp->opp_table->required_opp_count) {
195597ff543SDmitry Osipenko 		pr_err("%s: Invalid parameters\n", __func__);
196597ff543SDmitry Osipenko 		return 0;
197597ff543SDmitry Osipenko 	}
198597ff543SDmitry Osipenko 
1997eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
2007eba0c76SViresh Kumar 	if (lazy_linking_pending(opp->opp_table))
2017eba0c76SViresh Kumar 		return 0;
2027eba0c76SViresh Kumar 
203597ff543SDmitry Osipenko 	return opp->required_opps[index]->pstate;
204597ff543SDmitry Osipenko }
205597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
206597ff543SDmitry Osipenko 
207597ff543SDmitry Osipenko /**
2087813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
2097813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
2107813dd6fSViresh Kumar  *
2117813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
2127813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
2137813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
2147813dd6fSViresh Kumar  *
2157813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
2167813dd6fSViresh Kumar  */
2177813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
2187813dd6fSViresh Kumar {
2197813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2207813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
2217813dd6fSViresh Kumar 		return false;
2227813dd6fSViresh Kumar 	}
2237813dd6fSViresh Kumar 
2247813dd6fSViresh Kumar 	return opp->turbo;
2257813dd6fSViresh Kumar }
2267813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
2277813dd6fSViresh Kumar 
2287813dd6fSViresh Kumar /**
2297813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
2307813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2317813dd6fSViresh Kumar  *
2327813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
2337813dd6fSViresh Kumar  */
2347813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
2357813dd6fSViresh Kumar {
2367813dd6fSViresh Kumar 	struct opp_table *opp_table;
2377813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
2387813dd6fSViresh Kumar 
2397813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2407813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2417813dd6fSViresh Kumar 		return 0;
2427813dd6fSViresh Kumar 
2437813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
2447813dd6fSViresh Kumar 
2457813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2467813dd6fSViresh Kumar 
2477813dd6fSViresh Kumar 	return clock_latency_ns;
2487813dd6fSViresh Kumar }
2497813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
2507813dd6fSViresh Kumar 
2517813dd6fSViresh Kumar /**
2527813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
2537813dd6fSViresh Kumar  * @dev: device for which we do this operation
2547813dd6fSViresh Kumar  *
2557813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
2567813dd6fSViresh Kumar  */
2577813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
2587813dd6fSViresh Kumar {
2597813dd6fSViresh Kumar 	struct opp_table *opp_table;
2607813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
2617813dd6fSViresh Kumar 	struct regulator *reg;
2627813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
2637813dd6fSViresh Kumar 	int ret, i, count;
2647813dd6fSViresh Kumar 	struct {
2657813dd6fSViresh Kumar 		unsigned long min;
2667813dd6fSViresh Kumar 		unsigned long max;
2677813dd6fSViresh Kumar 	} *uV;
2687813dd6fSViresh Kumar 
2697813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2707813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2717813dd6fSViresh Kumar 		return 0;
2727813dd6fSViresh Kumar 
2737813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
27490e3577bSViresh Kumar 	if (!opp_table->regulators)
2757813dd6fSViresh Kumar 		goto put_opp_table;
2767813dd6fSViresh Kumar 
27790e3577bSViresh Kumar 	count = opp_table->regulator_count;
27890e3577bSViresh Kumar 
2797813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
2807813dd6fSViresh Kumar 	if (!uV)
2817813dd6fSViresh Kumar 		goto put_opp_table;
2827813dd6fSViresh Kumar 
2837813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
2847813dd6fSViresh Kumar 
2857813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2867813dd6fSViresh Kumar 		uV[i].min = ~0;
2877813dd6fSViresh Kumar 		uV[i].max = 0;
2887813dd6fSViresh Kumar 
2897813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
2907813dd6fSViresh Kumar 			if (!opp->available)
2917813dd6fSViresh Kumar 				continue;
2927813dd6fSViresh Kumar 
2937813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
2947813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
2957813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
2967813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
2977813dd6fSViresh Kumar 		}
2987813dd6fSViresh Kumar 	}
2997813dd6fSViresh Kumar 
3007813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
3017813dd6fSViresh Kumar 
3027813dd6fSViresh Kumar 	/*
3037813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
3047813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
3057813dd6fSViresh Kumar 	 */
3067813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3077813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
3087813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
3097813dd6fSViresh Kumar 		if (ret > 0)
3107813dd6fSViresh Kumar 			latency_ns += ret * 1000;
3117813dd6fSViresh Kumar 	}
3127813dd6fSViresh Kumar 
3137813dd6fSViresh Kumar 	kfree(uV);
3147813dd6fSViresh Kumar put_opp_table:
3157813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3167813dd6fSViresh Kumar 
3177813dd6fSViresh Kumar 	return latency_ns;
3187813dd6fSViresh Kumar }
3197813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
3207813dd6fSViresh Kumar 
3217813dd6fSViresh Kumar /**
3227813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
3237813dd6fSViresh Kumar  *					     nanoseconds
3247813dd6fSViresh Kumar  * @dev: device for which we do this operation
3257813dd6fSViresh Kumar  *
3267813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
3277813dd6fSViresh Kumar  * switch from one OPP to other.
3287813dd6fSViresh Kumar  */
3297813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
3307813dd6fSViresh Kumar {
3317813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
3327813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
3337813dd6fSViresh Kumar }
3347813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
3357813dd6fSViresh Kumar 
3367813dd6fSViresh Kumar /**
3377813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
3387813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3397813dd6fSViresh Kumar  *
3407813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
3417813dd6fSViresh Kumar  * if one is available, else returns 0;
3427813dd6fSViresh Kumar  */
3437813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
3447813dd6fSViresh Kumar {
3457813dd6fSViresh Kumar 	struct opp_table *opp_table;
3467813dd6fSViresh Kumar 	unsigned long freq = 0;
3477813dd6fSViresh Kumar 
3487813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3497813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3507813dd6fSViresh Kumar 		return 0;
3517813dd6fSViresh Kumar 
3527813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
3537813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
3547813dd6fSViresh Kumar 
3557813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3567813dd6fSViresh Kumar 
3577813dd6fSViresh Kumar 	return freq;
3587813dd6fSViresh Kumar }
3597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
3607813dd6fSViresh Kumar 
361a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
362a1e8c136SViresh Kumar {
363a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
364a1e8c136SViresh Kumar 	int count = 0;
365a1e8c136SViresh Kumar 
366a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
367a1e8c136SViresh Kumar 
368a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
369a1e8c136SViresh Kumar 		if (opp->available)
370a1e8c136SViresh Kumar 			count++;
371a1e8c136SViresh Kumar 	}
372a1e8c136SViresh Kumar 
373a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
374a1e8c136SViresh Kumar 
375a1e8c136SViresh Kumar 	return count;
376a1e8c136SViresh Kumar }
377a1e8c136SViresh Kumar 
3787813dd6fSViresh Kumar /**
3797813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
3807813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3817813dd6fSViresh Kumar  *
3827813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
3837813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
3847813dd6fSViresh Kumar  */
3857813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
3867813dd6fSViresh Kumar {
3877813dd6fSViresh Kumar 	struct opp_table *opp_table;
388a1e8c136SViresh Kumar 	int count;
3897813dd6fSViresh Kumar 
3907813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3917813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
3927813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
393035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
3947813dd6fSViresh Kumar 			__func__, count);
39509f662f9SViresh Kumar 		return count;
3967813dd6fSViresh Kumar 	}
3977813dd6fSViresh Kumar 
398a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
3997813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4007813dd6fSViresh Kumar 
4017813dd6fSViresh Kumar 	return count;
4027813dd6fSViresh Kumar }
4037813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
4047813dd6fSViresh Kumar 
4057813dd6fSViresh Kumar /**
4067813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
4077813dd6fSViresh Kumar  * @dev:		device for which we do this operation
4087813dd6fSViresh Kumar  * @freq:		frequency to search for
4097813dd6fSViresh Kumar  * @available:		true/false - match for available opp
4107813dd6fSViresh Kumar  *
4117813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
4127813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
4137813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
4147813dd6fSViresh Kumar  * EINVAL:	for bad pointer
4157813dd6fSViresh Kumar  * ERANGE:	no match found for search
4167813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
4177813dd6fSViresh Kumar  *
4187813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
4197813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
4207813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
4217813dd6fSViresh Kumar  *
4227813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
4237813dd6fSViresh Kumar  * or the opposite as well.
4247813dd6fSViresh Kumar  *
4257813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
4267813dd6fSViresh Kumar  * use.
4277813dd6fSViresh Kumar  */
4287813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
4297813dd6fSViresh Kumar 					      unsigned long freq,
4307813dd6fSViresh Kumar 					      bool available)
4317813dd6fSViresh Kumar {
4327813dd6fSViresh Kumar 	struct opp_table *opp_table;
4337813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
4347813dd6fSViresh Kumar 
4357813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4367813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
4377813dd6fSViresh Kumar 		int r = PTR_ERR(opp_table);
4387813dd6fSViresh Kumar 
4397813dd6fSViresh Kumar 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
4407813dd6fSViresh Kumar 		return ERR_PTR(r);
4417813dd6fSViresh Kumar 	}
4427813dd6fSViresh Kumar 
4437813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
4447813dd6fSViresh Kumar 
4457813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
4467813dd6fSViresh Kumar 		if (temp_opp->available == available &&
4477813dd6fSViresh Kumar 				temp_opp->rate == freq) {
4487813dd6fSViresh Kumar 			opp = temp_opp;
4497813dd6fSViresh Kumar 
4507813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
4517813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
4527813dd6fSViresh Kumar 			break;
4537813dd6fSViresh Kumar 		}
4547813dd6fSViresh Kumar 	}
4557813dd6fSViresh Kumar 
4567813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
4577813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4587813dd6fSViresh Kumar 
4597813dd6fSViresh Kumar 	return opp;
4607813dd6fSViresh Kumar }
4617813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
4627813dd6fSViresh Kumar 
4637813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
4647813dd6fSViresh Kumar 						   unsigned long *freq)
4657813dd6fSViresh Kumar {
4667813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
4677813dd6fSViresh Kumar 
4687813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
4697813dd6fSViresh Kumar 
4707813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
4717813dd6fSViresh Kumar 		if (temp_opp->available && temp_opp->rate >= *freq) {
4727813dd6fSViresh Kumar 			opp = temp_opp;
4737813dd6fSViresh Kumar 			*freq = opp->rate;
4747813dd6fSViresh Kumar 
4757813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
4767813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
4777813dd6fSViresh Kumar 			break;
4787813dd6fSViresh Kumar 		}
4797813dd6fSViresh Kumar 	}
4807813dd6fSViresh Kumar 
4817813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
4827813dd6fSViresh Kumar 
4837813dd6fSViresh Kumar 	return opp;
4847813dd6fSViresh Kumar }
4857813dd6fSViresh Kumar 
4867813dd6fSViresh Kumar /**
4877813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
4887813dd6fSViresh Kumar  * @dev:	device for which we do this operation
4897813dd6fSViresh Kumar  * @freq:	Start frequency
4907813dd6fSViresh Kumar  *
4917813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
4927813dd6fSViresh Kumar  * for a device.
4937813dd6fSViresh Kumar  *
4947813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
4957813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
4967813dd6fSViresh Kumar  * values can be:
4977813dd6fSViresh Kumar  * EINVAL:	for bad pointer
4987813dd6fSViresh Kumar  * ERANGE:	no match found for search
4997813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
5007813dd6fSViresh Kumar  *
5017813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5027813dd6fSViresh Kumar  * use.
5037813dd6fSViresh Kumar  */
5047813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
5057813dd6fSViresh Kumar 					     unsigned long *freq)
5067813dd6fSViresh Kumar {
5077813dd6fSViresh Kumar 	struct opp_table *opp_table;
5087813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
5097813dd6fSViresh Kumar 
5107813dd6fSViresh Kumar 	if (!dev || !freq) {
5117813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
5127813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
5137813dd6fSViresh Kumar 	}
5147813dd6fSViresh Kumar 
5157813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
5167813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
5177813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
5187813dd6fSViresh Kumar 
5197813dd6fSViresh Kumar 	opp = _find_freq_ceil(opp_table, freq);
5207813dd6fSViresh Kumar 
5217813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
5227813dd6fSViresh Kumar 
5237813dd6fSViresh Kumar 	return opp;
5247813dd6fSViresh Kumar }
5257813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
5267813dd6fSViresh Kumar 
5277813dd6fSViresh Kumar /**
5287813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
5297813dd6fSViresh Kumar  * @dev:	device for which we do this operation
5307813dd6fSViresh Kumar  * @freq:	Start frequency
5317813dd6fSViresh Kumar  *
5327813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
5337813dd6fSViresh Kumar  * for a device.
5347813dd6fSViresh Kumar  *
5357813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
5367813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
5377813dd6fSViresh Kumar  * values can be:
5387813dd6fSViresh Kumar  * EINVAL:	for bad pointer
5397813dd6fSViresh Kumar  * ERANGE:	no match found for search
5407813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
5417813dd6fSViresh Kumar  *
5427813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5437813dd6fSViresh Kumar  * use.
5447813dd6fSViresh Kumar  */
5457813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
5467813dd6fSViresh Kumar 					      unsigned long *freq)
5477813dd6fSViresh Kumar {
5487813dd6fSViresh Kumar 	struct opp_table *opp_table;
5497813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5507813dd6fSViresh Kumar 
5517813dd6fSViresh Kumar 	if (!dev || !freq) {
5527813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
5537813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
5547813dd6fSViresh Kumar 	}
5557813dd6fSViresh Kumar 
5567813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
5577813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
5587813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
5597813dd6fSViresh Kumar 
5607813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
5617813dd6fSViresh Kumar 
5627813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5637813dd6fSViresh Kumar 		if (temp_opp->available) {
5647813dd6fSViresh Kumar 			/* go to the next node, before choosing prev */
5657813dd6fSViresh Kumar 			if (temp_opp->rate > *freq)
5667813dd6fSViresh Kumar 				break;
5677813dd6fSViresh Kumar 			else
5687813dd6fSViresh Kumar 				opp = temp_opp;
5697813dd6fSViresh Kumar 		}
5707813dd6fSViresh Kumar 	}
5717813dd6fSViresh Kumar 
5727813dd6fSViresh Kumar 	/* Increment the reference count of OPP */
5737813dd6fSViresh Kumar 	if (!IS_ERR(opp))
5747813dd6fSViresh Kumar 		dev_pm_opp_get(opp);
5757813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
5767813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
5777813dd6fSViresh Kumar 
5787813dd6fSViresh Kumar 	if (!IS_ERR(opp))
5797813dd6fSViresh Kumar 		*freq = opp->rate;
5807813dd6fSViresh Kumar 
5817813dd6fSViresh Kumar 	return opp;
5827813dd6fSViresh Kumar }
5837813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
5847813dd6fSViresh Kumar 
5852f36bde0SAndrew-sh.Cheng /**
5862f36bde0SAndrew-sh.Cheng  * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
5872f36bde0SAndrew-sh.Cheng  *					 target voltage.
5882f36bde0SAndrew-sh.Cheng  * @dev:	Device for which we do this operation.
5892f36bde0SAndrew-sh.Cheng  * @u_volt:	Target voltage.
5902f36bde0SAndrew-sh.Cheng  *
5912f36bde0SAndrew-sh.Cheng  * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
5922f36bde0SAndrew-sh.Cheng  *
5932f36bde0SAndrew-sh.Cheng  * Return: matching *opp, else returns ERR_PTR in case of error which should be
5942f36bde0SAndrew-sh.Cheng  * handled using IS_ERR.
5952f36bde0SAndrew-sh.Cheng  *
5962f36bde0SAndrew-sh.Cheng  * Error return values can be:
5972f36bde0SAndrew-sh.Cheng  * EINVAL:	bad parameters
5982f36bde0SAndrew-sh.Cheng  *
5992f36bde0SAndrew-sh.Cheng  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6002f36bde0SAndrew-sh.Cheng  * use.
6012f36bde0SAndrew-sh.Cheng  */
6022f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
6032f36bde0SAndrew-sh.Cheng 						     unsigned long u_volt)
6042f36bde0SAndrew-sh.Cheng {
6052f36bde0SAndrew-sh.Cheng 	struct opp_table *opp_table;
6062f36bde0SAndrew-sh.Cheng 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
6072f36bde0SAndrew-sh.Cheng 
6082f36bde0SAndrew-sh.Cheng 	if (!dev || !u_volt) {
6092f36bde0SAndrew-sh.Cheng 		dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
6102f36bde0SAndrew-sh.Cheng 			u_volt);
6112f36bde0SAndrew-sh.Cheng 		return ERR_PTR(-EINVAL);
6122f36bde0SAndrew-sh.Cheng 	}
6132f36bde0SAndrew-sh.Cheng 
6142f36bde0SAndrew-sh.Cheng 	opp_table = _find_opp_table(dev);
6152f36bde0SAndrew-sh.Cheng 	if (IS_ERR(opp_table))
6162f36bde0SAndrew-sh.Cheng 		return ERR_CAST(opp_table);
6172f36bde0SAndrew-sh.Cheng 
6182f36bde0SAndrew-sh.Cheng 	mutex_lock(&opp_table->lock);
6192f36bde0SAndrew-sh.Cheng 
6202f36bde0SAndrew-sh.Cheng 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
6212f36bde0SAndrew-sh.Cheng 		if (temp_opp->available) {
6222f36bde0SAndrew-sh.Cheng 			if (temp_opp->supplies[0].u_volt > u_volt)
6232f36bde0SAndrew-sh.Cheng 				break;
6242f36bde0SAndrew-sh.Cheng 			opp = temp_opp;
6252f36bde0SAndrew-sh.Cheng 		}
6262f36bde0SAndrew-sh.Cheng 	}
6272f36bde0SAndrew-sh.Cheng 
6282f36bde0SAndrew-sh.Cheng 	/* Increment the reference count of OPP */
6292f36bde0SAndrew-sh.Cheng 	if (!IS_ERR(opp))
6302f36bde0SAndrew-sh.Cheng 		dev_pm_opp_get(opp);
6312f36bde0SAndrew-sh.Cheng 
6322f36bde0SAndrew-sh.Cheng 	mutex_unlock(&opp_table->lock);
6332f36bde0SAndrew-sh.Cheng 	dev_pm_opp_put_opp_table(opp_table);
6342f36bde0SAndrew-sh.Cheng 
6352f36bde0SAndrew-sh.Cheng 	return opp;
6362f36bde0SAndrew-sh.Cheng }
6372f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
6382f36bde0SAndrew-sh.Cheng 
63900ce3873SKrzysztof Kozlowski /**
64022079af7SViresh Kumar  * dev_pm_opp_find_level_exact() - search for an exact level
64122079af7SViresh Kumar  * @dev:		device for which we do this operation
64222079af7SViresh Kumar  * @level:		level to search for
64322079af7SViresh Kumar  *
64422079af7SViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
64522079af7SViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
64622079af7SViresh Kumar  * be handled using IS_ERR. Error return values can be:
64722079af7SViresh Kumar  * EINVAL:	for bad pointer
64822079af7SViresh Kumar  * ERANGE:	no match found for search
64922079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
65022079af7SViresh Kumar  *
65122079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
65222079af7SViresh Kumar  * use.
65322079af7SViresh Kumar  */
65422079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
65522079af7SViresh Kumar 					       unsigned int level)
65622079af7SViresh Kumar {
65722079af7SViresh Kumar 	struct opp_table *opp_table;
65822079af7SViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
65922079af7SViresh Kumar 
66022079af7SViresh Kumar 	opp_table = _find_opp_table(dev);
66122079af7SViresh Kumar 	if (IS_ERR(opp_table)) {
66222079af7SViresh Kumar 		int r = PTR_ERR(opp_table);
66322079af7SViresh Kumar 
66422079af7SViresh Kumar 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
66522079af7SViresh Kumar 		return ERR_PTR(r);
66622079af7SViresh Kumar 	}
66722079af7SViresh Kumar 
66822079af7SViresh Kumar 	mutex_lock(&opp_table->lock);
66922079af7SViresh Kumar 
67022079af7SViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
67122079af7SViresh Kumar 		if (temp_opp->level == level) {
67222079af7SViresh Kumar 			opp = temp_opp;
67322079af7SViresh Kumar 
67422079af7SViresh Kumar 			/* Increment the reference count of OPP */
67522079af7SViresh Kumar 			dev_pm_opp_get(opp);
67622079af7SViresh Kumar 			break;
67722079af7SViresh Kumar 		}
67822079af7SViresh Kumar 	}
67922079af7SViresh Kumar 
68022079af7SViresh Kumar 	mutex_unlock(&opp_table->lock);
68122079af7SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
68222079af7SViresh Kumar 
68322079af7SViresh Kumar 	return opp;
68422079af7SViresh Kumar }
68522079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
68622079af7SViresh Kumar 
68722079af7SViresh Kumar /**
68822079af7SViresh Kumar  * dev_pm_opp_find_level_ceil() - search for an rounded up level
68922079af7SViresh Kumar  * @dev:		device for which we do this operation
69022079af7SViresh Kumar  * @level:		level to search for
69122079af7SViresh Kumar  *
69222079af7SViresh Kumar  * Return: Searches for rounded up match in the opp table and returns pointer
69322079af7SViresh Kumar  * to the  matching opp if found, else returns ERR_PTR in case of error and
69422079af7SViresh Kumar  * should be handled using IS_ERR. Error return values can be:
69522079af7SViresh Kumar  * EINVAL:	for bad pointer
69622079af7SViresh Kumar  * ERANGE:	no match found for search
69722079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
69822079af7SViresh Kumar  *
69922079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
70022079af7SViresh Kumar  * use.
70122079af7SViresh Kumar  */
70222079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
70322079af7SViresh Kumar 					      unsigned int *level)
70422079af7SViresh Kumar {
70522079af7SViresh Kumar 	struct opp_table *opp_table;
70622079af7SViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
70722079af7SViresh Kumar 
70822079af7SViresh Kumar 	opp_table = _find_opp_table(dev);
70922079af7SViresh Kumar 	if (IS_ERR(opp_table)) {
71022079af7SViresh Kumar 		int r = PTR_ERR(opp_table);
71122079af7SViresh Kumar 
71222079af7SViresh Kumar 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
71322079af7SViresh Kumar 		return ERR_PTR(r);
71422079af7SViresh Kumar 	}
71522079af7SViresh Kumar 
71622079af7SViresh Kumar 	mutex_lock(&opp_table->lock);
71722079af7SViresh Kumar 
71822079af7SViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
71922079af7SViresh Kumar 		if (temp_opp->available && temp_opp->level >= *level) {
72022079af7SViresh Kumar 			opp = temp_opp;
72122079af7SViresh Kumar 			*level = opp->level;
72222079af7SViresh Kumar 
72322079af7SViresh Kumar 			/* Increment the reference count of OPP */
72422079af7SViresh Kumar 			dev_pm_opp_get(opp);
72522079af7SViresh Kumar 			break;
72622079af7SViresh Kumar 		}
72722079af7SViresh Kumar 	}
72822079af7SViresh Kumar 
72922079af7SViresh Kumar 	mutex_unlock(&opp_table->lock);
73022079af7SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
73122079af7SViresh Kumar 
73222079af7SViresh Kumar 	return opp;
73322079af7SViresh Kumar }
73422079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
73522079af7SViresh Kumar 
73622079af7SViresh Kumar /**
73700ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
73800ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
739617df304SYang Li  * @bw:	start bandwidth
74000ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
74100ce3873SKrzysztof Kozlowski  *
74200ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
74300ce3873SKrzysztof Kozlowski  * for a device.
74400ce3873SKrzysztof Kozlowski  *
74500ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
74600ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
74700ce3873SKrzysztof Kozlowski  * values can be:
74800ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
74900ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
75000ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
75100ce3873SKrzysztof Kozlowski  *
75200ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
75300ce3873SKrzysztof Kozlowski  * use.
75400ce3873SKrzysztof Kozlowski  */
75500ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev,
75600ce3873SKrzysztof Kozlowski 					   unsigned int *bw, int index)
75700ce3873SKrzysztof Kozlowski {
75800ce3873SKrzysztof Kozlowski 	struct opp_table *opp_table;
75900ce3873SKrzysztof Kozlowski 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
76000ce3873SKrzysztof Kozlowski 
76100ce3873SKrzysztof Kozlowski 	if (!dev || !bw) {
76200ce3873SKrzysztof Kozlowski 		dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw);
76300ce3873SKrzysztof Kozlowski 		return ERR_PTR(-EINVAL);
76400ce3873SKrzysztof Kozlowski 	}
76500ce3873SKrzysztof Kozlowski 
76600ce3873SKrzysztof Kozlowski 	opp_table = _find_opp_table(dev);
76700ce3873SKrzysztof Kozlowski 	if (IS_ERR(opp_table))
76800ce3873SKrzysztof Kozlowski 		return ERR_CAST(opp_table);
76900ce3873SKrzysztof Kozlowski 
77000ce3873SKrzysztof Kozlowski 	if (index >= opp_table->path_count)
77100ce3873SKrzysztof Kozlowski 		return ERR_PTR(-EINVAL);
77200ce3873SKrzysztof Kozlowski 
77300ce3873SKrzysztof Kozlowski 	mutex_lock(&opp_table->lock);
77400ce3873SKrzysztof Kozlowski 
77500ce3873SKrzysztof Kozlowski 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
77600ce3873SKrzysztof Kozlowski 		if (temp_opp->available && temp_opp->bandwidth) {
77700ce3873SKrzysztof Kozlowski 			if (temp_opp->bandwidth[index].peak >= *bw) {
77800ce3873SKrzysztof Kozlowski 				opp = temp_opp;
77900ce3873SKrzysztof Kozlowski 				*bw = opp->bandwidth[index].peak;
78000ce3873SKrzysztof Kozlowski 
78100ce3873SKrzysztof Kozlowski 				/* Increment the reference count of OPP */
78200ce3873SKrzysztof Kozlowski 				dev_pm_opp_get(opp);
78300ce3873SKrzysztof Kozlowski 				break;
78400ce3873SKrzysztof Kozlowski 			}
78500ce3873SKrzysztof Kozlowski 		}
78600ce3873SKrzysztof Kozlowski 	}
78700ce3873SKrzysztof Kozlowski 
78800ce3873SKrzysztof Kozlowski 	mutex_unlock(&opp_table->lock);
78900ce3873SKrzysztof Kozlowski 	dev_pm_opp_put_opp_table(opp_table);
79000ce3873SKrzysztof Kozlowski 
79100ce3873SKrzysztof Kozlowski 	return opp;
79200ce3873SKrzysztof Kozlowski }
79300ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
79400ce3873SKrzysztof Kozlowski 
79500ce3873SKrzysztof Kozlowski /**
79600ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
79700ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
798617df304SYang Li  * @bw:	start bandwidth
79900ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
80000ce3873SKrzysztof Kozlowski  *
80100ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
80200ce3873SKrzysztof Kozlowski  * for a device.
80300ce3873SKrzysztof Kozlowski  *
80400ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
80500ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
80600ce3873SKrzysztof Kozlowski  * values can be:
80700ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
80800ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
80900ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
81000ce3873SKrzysztof Kozlowski  *
81100ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
81200ce3873SKrzysztof Kozlowski  * use.
81300ce3873SKrzysztof Kozlowski  */
81400ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
81500ce3873SKrzysztof Kozlowski 					    unsigned int *bw, int index)
81600ce3873SKrzysztof Kozlowski {
81700ce3873SKrzysztof Kozlowski 	struct opp_table *opp_table;
81800ce3873SKrzysztof Kozlowski 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
81900ce3873SKrzysztof Kozlowski 
82000ce3873SKrzysztof Kozlowski 	if (!dev || !bw) {
82100ce3873SKrzysztof Kozlowski 		dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw);
82200ce3873SKrzysztof Kozlowski 		return ERR_PTR(-EINVAL);
82300ce3873SKrzysztof Kozlowski 	}
82400ce3873SKrzysztof Kozlowski 
82500ce3873SKrzysztof Kozlowski 	opp_table = _find_opp_table(dev);
82600ce3873SKrzysztof Kozlowski 	if (IS_ERR(opp_table))
82700ce3873SKrzysztof Kozlowski 		return ERR_CAST(opp_table);
82800ce3873SKrzysztof Kozlowski 
82900ce3873SKrzysztof Kozlowski 	if (index >= opp_table->path_count)
83000ce3873SKrzysztof Kozlowski 		return ERR_PTR(-EINVAL);
83100ce3873SKrzysztof Kozlowski 
83200ce3873SKrzysztof Kozlowski 	mutex_lock(&opp_table->lock);
83300ce3873SKrzysztof Kozlowski 
83400ce3873SKrzysztof Kozlowski 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
83500ce3873SKrzysztof Kozlowski 		if (temp_opp->available && temp_opp->bandwidth) {
83600ce3873SKrzysztof Kozlowski 			/* go to the next node, before choosing prev */
83700ce3873SKrzysztof Kozlowski 			if (temp_opp->bandwidth[index].peak > *bw)
83800ce3873SKrzysztof Kozlowski 				break;
83900ce3873SKrzysztof Kozlowski 			opp = temp_opp;
84000ce3873SKrzysztof Kozlowski 		}
84100ce3873SKrzysztof Kozlowski 	}
84200ce3873SKrzysztof Kozlowski 
84300ce3873SKrzysztof Kozlowski 	/* Increment the reference count of OPP */
84400ce3873SKrzysztof Kozlowski 	if (!IS_ERR(opp))
84500ce3873SKrzysztof Kozlowski 		dev_pm_opp_get(opp);
84600ce3873SKrzysztof Kozlowski 	mutex_unlock(&opp_table->lock);
84700ce3873SKrzysztof Kozlowski 	dev_pm_opp_put_opp_table(opp_table);
84800ce3873SKrzysztof Kozlowski 
84900ce3873SKrzysztof Kozlowski 	if (!IS_ERR(opp))
85000ce3873SKrzysztof Kozlowski 		*bw = opp->bandwidth[index].peak;
85100ce3873SKrzysztof Kozlowski 
85200ce3873SKrzysztof Kozlowski 	return opp;
85300ce3873SKrzysztof Kozlowski }
85400ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
85500ce3873SKrzysztof Kozlowski 
8567813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
8577813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
8587813dd6fSViresh Kumar {
8597813dd6fSViresh Kumar 	int ret;
8607813dd6fSViresh Kumar 
8617813dd6fSViresh Kumar 	/* Regulator not available for device */
8627813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
8637813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
8647813dd6fSViresh Kumar 			PTR_ERR(reg));
8657813dd6fSViresh Kumar 		return 0;
8667813dd6fSViresh Kumar 	}
8677813dd6fSViresh Kumar 
8687813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
8697813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
8707813dd6fSViresh Kumar 
8717813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
8727813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
8737813dd6fSViresh Kumar 	if (ret)
8747813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
8757813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
8767813dd6fSViresh Kumar 			supply->u_volt_max, ret);
8777813dd6fSViresh Kumar 
8787813dd6fSViresh Kumar 	return ret;
8797813dd6fSViresh Kumar }
8807813dd6fSViresh Kumar 
881285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
882285881b5SViresh Kumar 					    unsigned long freq)
8837813dd6fSViresh Kumar {
8847813dd6fSViresh Kumar 	int ret;
8857813dd6fSViresh Kumar 
88635e74b2eSViresh Kumar 	/* We may reach here for devices which don't change frequency */
88735e74b2eSViresh Kumar 	if (IS_ERR(clk))
88835e74b2eSViresh Kumar 		return 0;
88935e74b2eSViresh Kumar 
8907813dd6fSViresh Kumar 	ret = clk_set_rate(clk, freq);
8917813dd6fSViresh Kumar 	if (ret) {
8927813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
8937813dd6fSViresh Kumar 			ret);
8947813dd6fSViresh Kumar 	}
8957813dd6fSViresh Kumar 
8967813dd6fSViresh Kumar 	return ret;
8977813dd6fSViresh Kumar }
8987813dd6fSViresh Kumar 
8998d45719cSKamil Konieczny static int _generic_set_opp_regulator(struct opp_table *opp_table,
9007813dd6fSViresh Kumar 				      struct device *dev,
9013f62670fSViresh Kumar 				      struct dev_pm_opp *opp,
9027813dd6fSViresh Kumar 				      unsigned long freq,
9033f62670fSViresh Kumar 				      int scaling_down)
9047813dd6fSViresh Kumar {
9057813dd6fSViresh Kumar 	struct regulator *reg = opp_table->regulators[0];
9063f62670fSViresh Kumar 	struct dev_pm_opp *old_opp = opp_table->current_opp;
9077813dd6fSViresh Kumar 	int ret;
9087813dd6fSViresh Kumar 
9097813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
9107813dd6fSViresh Kumar 	if (WARN_ON(opp_table->regulator_count > 1)) {
9117813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
9127813dd6fSViresh Kumar 		return -EINVAL;
9137813dd6fSViresh Kumar 	}
9147813dd6fSViresh Kumar 
9157813dd6fSViresh Kumar 	/* Scaling up? Scale voltage before frequency */
9163f62670fSViresh Kumar 	if (!scaling_down) {
9173f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
9187813dd6fSViresh Kumar 		if (ret)
9197813dd6fSViresh Kumar 			goto restore_voltage;
9207813dd6fSViresh Kumar 	}
9217813dd6fSViresh Kumar 
9227813dd6fSViresh Kumar 	/* Change frequency */
923285881b5SViresh Kumar 	ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
9247813dd6fSViresh Kumar 	if (ret)
9257813dd6fSViresh Kumar 		goto restore_voltage;
9267813dd6fSViresh Kumar 
9277813dd6fSViresh Kumar 	/* Scaling down? Scale voltage after frequency */
9283f62670fSViresh Kumar 	if (scaling_down) {
9293f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
9307813dd6fSViresh Kumar 		if (ret)
9317813dd6fSViresh Kumar 			goto restore_freq;
9327813dd6fSViresh Kumar 	}
9337813dd6fSViresh Kumar 
9348d45719cSKamil Konieczny 	/*
9358d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
9368d45719cSKamil Konieczny 	 * some boot-enabled regulators.
9378d45719cSKamil Konieczny 	 */
93872f80ce4SViresh Kumar 	if (unlikely(!opp_table->enabled)) {
9398d45719cSKamil Konieczny 		ret = regulator_enable(reg);
9408d45719cSKamil Konieczny 		if (ret < 0)
9418d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
9428d45719cSKamil Konieczny 	}
9438d45719cSKamil Konieczny 
9447813dd6fSViresh Kumar 	return 0;
9457813dd6fSViresh Kumar 
9467813dd6fSViresh Kumar restore_freq:
9473f62670fSViresh Kumar 	if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate))
9487813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
9493f62670fSViresh Kumar 			__func__, old_opp->rate);
9507813dd6fSViresh Kumar restore_voltage:
9517813dd6fSViresh Kumar 	/* This shouldn't harm even if the voltages weren't updated earlier */
9523f62670fSViresh Kumar 	_set_opp_voltage(dev, reg, old_opp->supplies);
9537813dd6fSViresh Kumar 
9547813dd6fSViresh Kumar 	return ret;
9557813dd6fSViresh Kumar }
9567813dd6fSViresh Kumar 
957b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
958240ae50eSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev)
959b00e667aSViresh Kumar {
960b00e667aSViresh Kumar 	u32 avg, peak;
961b00e667aSViresh Kumar 	int i, ret;
962b00e667aSViresh Kumar 
963b00e667aSViresh Kumar 	if (!opp_table->paths)
964b00e667aSViresh Kumar 		return 0;
965b00e667aSViresh Kumar 
966b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
967240ae50eSViresh Kumar 		if (!opp) {
968b00e667aSViresh Kumar 			avg = 0;
969b00e667aSViresh Kumar 			peak = 0;
970b00e667aSViresh Kumar 		} else {
971b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
972b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
973b00e667aSViresh Kumar 		}
974b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
975b00e667aSViresh Kumar 		if (ret) {
976b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
977240ae50eSViresh Kumar 				opp ? "set" : "remove", i, ret);
978b00e667aSViresh Kumar 			return ret;
979b00e667aSViresh Kumar 		}
980b00e667aSViresh Kumar 	}
981b00e667aSViresh Kumar 
982b00e667aSViresh Kumar 	return 0;
983b00e667aSViresh Kumar }
984b00e667aSViresh Kumar 
9857e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table,
986509e4777SViresh Kumar 			   struct device *dev, struct dev_pm_opp *opp,
987509e4777SViresh Kumar 			   unsigned long freq)
9887e535993SViresh Kumar {
98904b447dfSDmitry Osipenko 	struct dev_pm_set_opp_data *data = opp_table->set_opp_data;
990509e4777SViresh Kumar 	struct dev_pm_opp *old_opp = opp_table->current_opp;
9917e535993SViresh Kumar 	int size;
9927e535993SViresh Kumar 
99304b447dfSDmitry Osipenko 	/*
994b0ec0942SViresh Kumar 	 * We support this only if dev_pm_opp_set_config() was called
995b0ec0942SViresh Kumar 	 * earlier to set regulators.
99604b447dfSDmitry Osipenko 	 */
99704b447dfSDmitry Osipenko 	if (opp_table->sod_supplies) {
998509e4777SViresh Kumar 		size = sizeof(*old_opp->supplies) * opp_table->regulator_count;
999509e4777SViresh Kumar 		memcpy(data->old_opp.supplies, old_opp->supplies, size);
1000509e4777SViresh Kumar 		memcpy(data->new_opp.supplies, opp->supplies, size);
100104b447dfSDmitry Osipenko 		data->regulator_count = opp_table->regulator_count;
100204b447dfSDmitry Osipenko 	} else {
100304b447dfSDmitry Osipenko 		data->regulator_count = 0;
100404b447dfSDmitry Osipenko 	}
100504b447dfSDmitry Osipenko 
100604b447dfSDmitry Osipenko 	data->regulators = opp_table->regulators;
100704b447dfSDmitry Osipenko 	data->clk = opp_table->clk;
100804b447dfSDmitry Osipenko 	data->dev = dev;
1009509e4777SViresh Kumar 	data->old_opp.rate = old_opp->rate;
101004b447dfSDmitry Osipenko 	data->new_opp.rate = freq;
10117e535993SViresh Kumar 
10127e535993SViresh Kumar 	return opp_table->set_opp(data);
10137e535993SViresh Kumar }
10147e535993SViresh Kumar 
101560cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev,
101660cdeae0SStephan Gerhold 			     struct dev_pm_opp *opp, int i)
101760cdeae0SStephan Gerhold {
101860cdeae0SStephan Gerhold 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
101960cdeae0SStephan Gerhold 	int ret;
102060cdeae0SStephan Gerhold 
102160cdeae0SStephan Gerhold 	if (!pd_dev)
102260cdeae0SStephan Gerhold 		return 0;
102360cdeae0SStephan Gerhold 
102460cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
102560cdeae0SStephan Gerhold 	if (ret) {
10269bfb1fffSViresh Kumar 		dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
102760cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
102860cdeae0SStephan Gerhold 	}
102960cdeae0SStephan Gerhold 
103060cdeae0SStephan Gerhold 	return ret;
103160cdeae0SStephan Gerhold }
103260cdeae0SStephan Gerhold 
1033ca1b5d77SViresh Kumar /* This is only called for PM domain for now */
1034ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev,
1035ca1b5d77SViresh Kumar 			      struct opp_table *opp_table,
10362c59138cSStephan Gerhold 			      struct dev_pm_opp *opp, bool up)
1037ca1b5d77SViresh Kumar {
1038ca1b5d77SViresh Kumar 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
1039ca1b5d77SViresh Kumar 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
1040ca1b5d77SViresh Kumar 	int i, ret = 0;
1041ca1b5d77SViresh Kumar 
1042ca1b5d77SViresh Kumar 	if (!required_opp_tables)
1043ca1b5d77SViresh Kumar 		return 0;
1044ca1b5d77SViresh Kumar 
104519526d09SMarijn Suijten 	/* required-opps not fully initialized yet */
104619526d09SMarijn Suijten 	if (lazy_linking_pending(opp_table))
104719526d09SMarijn Suijten 		return -EBUSY;
104819526d09SMarijn Suijten 
10494fa82a87SHsin-Yi Wang 	/*
10504fa82a87SHsin-Yi Wang 	 * We only support genpd's OPPs in the "required-opps" for now, as we
10514fa82a87SHsin-Yi Wang 	 * don't know much about other use cases. Error out if the required OPP
10524fa82a87SHsin-Yi Wang 	 * doesn't belong to a genpd.
10534fa82a87SHsin-Yi Wang 	 */
10544fa82a87SHsin-Yi Wang 	if (unlikely(!required_opp_tables[0]->is_genpd)) {
10554fa82a87SHsin-Yi Wang 		dev_err(dev, "required-opps don't belong to a genpd\n");
10564fa82a87SHsin-Yi Wang 		return -ENOENT;
10574fa82a87SHsin-Yi Wang 	}
10584fa82a87SHsin-Yi Wang 
1059ca1b5d77SViresh Kumar 	/* Single genpd case */
106060cdeae0SStephan Gerhold 	if (!genpd_virt_devs)
106160cdeae0SStephan Gerhold 		return _set_required_opp(dev, dev, opp, 0);
1062ca1b5d77SViresh Kumar 
1063ca1b5d77SViresh Kumar 	/* Multiple genpd case */
1064ca1b5d77SViresh Kumar 
1065ca1b5d77SViresh Kumar 	/*
1066ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
1067ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
1068ca1b5d77SViresh Kumar 	 */
1069ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
1070ca1b5d77SViresh Kumar 
10712c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
10722c59138cSStephan Gerhold 	if (up) {
1073ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
107460cdeae0SStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
107560cdeae0SStephan Gerhold 			if (ret)
1076ca1b5d77SViresh Kumar 				break;
1077ca1b5d77SViresh Kumar 		}
10782c59138cSStephan Gerhold 	} else {
10792c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
10802c59138cSStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
10812c59138cSStephan Gerhold 			if (ret)
1082ca1b5d77SViresh Kumar 				break;
1083ca1b5d77SViresh Kumar 		}
1084ca1b5d77SViresh Kumar 	}
10852c59138cSStephan Gerhold 
1086ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
1087ca1b5d77SViresh Kumar 
1088ca1b5d77SViresh Kumar 	return ret;
1089ca1b5d77SViresh Kumar }
1090ca1b5d77SViresh Kumar 
109181c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
109281c4d8a3SViresh Kumar {
109381c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
109481c4d8a3SViresh Kumar 	unsigned long freq;
109581c4d8a3SViresh Kumar 
109681c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
109781c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
109881c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
109981c4d8a3SViresh Kumar 	}
110081c4d8a3SViresh Kumar 
110181c4d8a3SViresh Kumar 	/*
110281c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
110381c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
110481c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
110581c4d8a3SViresh Kumar 	 */
110681c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
110781c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
110881c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
110981c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
111081c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
111181c4d8a3SViresh Kumar 	}
111281c4d8a3SViresh Kumar 
111381c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
111481c4d8a3SViresh Kumar }
111581c4d8a3SViresh Kumar 
11165ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
1117f3364e17SViresh Kumar {
1118f3364e17SViresh Kumar 	int ret;
1119f3364e17SViresh Kumar 
1120f3364e17SViresh Kumar 	if (!opp_table->enabled)
1121f3364e17SViresh Kumar 		return 0;
1122f3364e17SViresh Kumar 
1123f3364e17SViresh Kumar 	/*
1124f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
1125f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
1126f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
1127f3364e17SViresh Kumar 	 */
1128f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
1129f3364e17SViresh Kumar 		return 0;
1130f3364e17SViresh Kumar 
1131240ae50eSViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev);
1132f3364e17SViresh Kumar 	if (ret)
1133f3364e17SViresh Kumar 		return ret;
1134f3364e17SViresh Kumar 
1135f3364e17SViresh Kumar 	if (opp_table->regulators)
1136f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
1137f3364e17SViresh Kumar 
11382c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
1139f3364e17SViresh Kumar 
1140f3364e17SViresh Kumar 	opp_table->enabled = false;
1141f3364e17SViresh Kumar 	return ret;
1142f3364e17SViresh Kumar }
1143f3364e17SViresh Kumar 
1144386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
1145386ba854SViresh Kumar 		    struct dev_pm_opp *opp, unsigned long freq)
11467813dd6fSViresh Kumar {
1147386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
1148f0b88fa4SViresh Kumar 	int scaling_down, ret;
11497813dd6fSViresh Kumar 
1150386ba854SViresh Kumar 	if (unlikely(!opp))
1151386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
1152aca48b61SRajendra Nayak 
115381c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
115481c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
115581c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
11567813dd6fSViresh Kumar 
115781c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
115881c4d8a3SViresh Kumar 
115981c4d8a3SViresh Kumar 	/* Return early if nothing to do */
1160de04241aSJonathan Marek 	if (old_opp == opp && opp_table->current_rate == freq &&
1161de04241aSJonathan Marek 	    opp_table->enabled) {
116281c4d8a3SViresh Kumar 		dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
1163386ba854SViresh Kumar 		return 0;
11647813dd6fSViresh Kumar 	}
11657813dd6fSViresh Kumar 
1166f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1167de04241aSJonathan Marek 		__func__, opp_table->current_rate, freq, old_opp->level,
1168de04241aSJonathan Marek 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1169f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1170f0b88fa4SViresh Kumar 
1171f0b88fa4SViresh Kumar 	scaling_down = _opp_compare_key(old_opp, opp);
1172f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1173f0b88fa4SViresh Kumar 		scaling_down = 0;
11747813dd6fSViresh Kumar 
1175ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1176f0b88fa4SViresh Kumar 	if (!scaling_down) {
11772c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1178870d5d96SViresh Kumar 		if (ret) {
1179870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1180386ba854SViresh Kumar 			return ret;
1181ca1b5d77SViresh Kumar 		}
1182ca1b5d77SViresh Kumar 
1183870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1184870d5d96SViresh Kumar 		if (ret) {
1185870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1186870d5d96SViresh Kumar 			return ret;
1187870d5d96SViresh Kumar 		}
1188870d5d96SViresh Kumar 	}
1189870d5d96SViresh Kumar 
11907e535993SViresh Kumar 	if (opp_table->set_opp) {
1191509e4777SViresh Kumar 		ret = _set_opp_custom(opp_table, dev, opp, freq);
11927e535993SViresh Kumar 	} else if (opp_table->regulators) {
11933f62670fSViresh Kumar 		ret = _generic_set_opp_regulator(opp_table, dev, opp, freq,
11943f62670fSViresh Kumar 						 scaling_down);
11957813dd6fSViresh Kumar 	} else {
11967813dd6fSViresh Kumar 		/* Only frequency scaling */
11971d3c42caSViresh Kumar 		ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
11987813dd6fSViresh Kumar 	}
11997813dd6fSViresh Kumar 
1200ca1b5d77SViresh Kumar 	if (ret)
1201870d5d96SViresh Kumar 		return ret;
1202870d5d96SViresh Kumar 
1203870d5d96SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1204870d5d96SViresh Kumar 	if (scaling_down) {
1205870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1206870d5d96SViresh Kumar 		if (ret) {
1207870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1208870d5d96SViresh Kumar 			return ret;
1209ca1b5d77SViresh Kumar 		}
1210ca1b5d77SViresh Kumar 
1211870d5d96SViresh Kumar 		ret = _set_required_opps(dev, opp_table, opp, false);
1212870d5d96SViresh Kumar 		if (ret) {
1213870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1214870d5d96SViresh Kumar 			return ret;
1215870d5d96SViresh Kumar 		}
1216870d5d96SViresh Kumar 	}
1217870d5d96SViresh Kumar 
121872f80ce4SViresh Kumar 	opp_table->enabled = true;
121981c4d8a3SViresh Kumar 	dev_pm_opp_put(old_opp);
122081c4d8a3SViresh Kumar 
122181c4d8a3SViresh Kumar 	/* Make sure current_opp doesn't get freed */
122281c4d8a3SViresh Kumar 	dev_pm_opp_get(opp);
122381c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
1224de04241aSJonathan Marek 	opp_table->current_rate = freq;
1225fe2af402SGeorgi Djakov 
1226386ba854SViresh Kumar 	return ret;
1227386ba854SViresh Kumar }
1228386ba854SViresh Kumar 
1229386ba854SViresh Kumar /**
1230386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1231386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1232386ba854SViresh Kumar  * @target_freq: frequency to achieve
1233386ba854SViresh Kumar  *
1234386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1235386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1236386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1237386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1238386ba854SViresh Kumar  * frequency.
1239386ba854SViresh Kumar  */
1240386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1241386ba854SViresh Kumar {
1242386ba854SViresh Kumar 	struct opp_table *opp_table;
1243386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1244386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
1245386ba854SViresh Kumar 	int ret;
1246386ba854SViresh Kumar 
1247386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1248386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1249386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1250386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1251386ba854SViresh Kumar 	}
1252386ba854SViresh Kumar 
1253386ba854SViresh Kumar 	if (target_freq) {
1254386ba854SViresh Kumar 		/*
1255386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1256386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1257386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1258386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1259386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1260386ba854SViresh Kumar 		 */
1261386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
1262386ba854SViresh Kumar 			ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq);
1263386ba854SViresh Kumar 			goto put_opp_table;
1264386ba854SViresh Kumar 		}
1265386ba854SViresh Kumar 
1266386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1267386ba854SViresh Kumar 		if ((long)freq <= 0)
1268386ba854SViresh Kumar 			freq = target_freq;
1269386ba854SViresh Kumar 
1270386ba854SViresh Kumar 		/*
1271386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1272386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1273386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1274386ba854SViresh Kumar 		 */
1275386ba854SViresh Kumar 		temp_freq = freq;
1276386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1277386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1278386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1279386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1280386ba854SViresh Kumar 				__func__, freq, ret);
1281386ba854SViresh Kumar 			goto put_opp_table;
1282386ba854SViresh Kumar 		}
1283386ba854SViresh Kumar 	}
1284386ba854SViresh Kumar 
1285386ba854SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, freq);
1286386ba854SViresh Kumar 
1287386ba854SViresh Kumar 	if (target_freq)
12887813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
12897813dd6fSViresh Kumar put_opp_table:
12907813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
12917813dd6fSViresh Kumar 	return ret;
12927813dd6fSViresh Kumar }
12937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
12947813dd6fSViresh Kumar 
1295abbe3483SViresh Kumar /**
1296abbe3483SViresh Kumar  * dev_pm_opp_set_opp() - Configure device for OPP
1297abbe3483SViresh Kumar  * @dev: device for which we do this operation
1298abbe3483SViresh Kumar  * @opp: OPP to set to
1299abbe3483SViresh Kumar  *
1300abbe3483SViresh Kumar  * This configures the device based on the properties of the OPP passed to this
1301abbe3483SViresh Kumar  * routine.
1302abbe3483SViresh Kumar  *
1303abbe3483SViresh Kumar  * Return: 0 on success, a negative error number otherwise.
1304abbe3483SViresh Kumar  */
1305abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1306abbe3483SViresh Kumar {
1307abbe3483SViresh Kumar 	struct opp_table *opp_table;
1308abbe3483SViresh Kumar 	int ret;
1309abbe3483SViresh Kumar 
1310abbe3483SViresh Kumar 	opp_table = _find_opp_table(dev);
1311abbe3483SViresh Kumar 	if (IS_ERR(opp_table)) {
1312abbe3483SViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1313abbe3483SViresh Kumar 		return PTR_ERR(opp_table);
1314abbe3483SViresh Kumar 	}
1315abbe3483SViresh Kumar 
1316abbe3483SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0);
1317abbe3483SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1318abbe3483SViresh Kumar 
1319abbe3483SViresh Kumar 	return ret;
1320abbe3483SViresh Kumar }
1321abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1322abbe3483SViresh Kumar 
13237813dd6fSViresh Kumar /* OPP-dev Helpers */
13247813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
13257813dd6fSViresh Kumar 			    struct opp_table *opp_table)
13267813dd6fSViresh Kumar {
13277813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
13287813dd6fSViresh Kumar 	list_del(&opp_dev->node);
13297813dd6fSViresh Kumar 	kfree(opp_dev);
13307813dd6fSViresh Kumar }
13317813dd6fSViresh Kumar 
1332ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
13337813dd6fSViresh Kumar 				struct opp_table *opp_table)
13347813dd6fSViresh Kumar {
13357813dd6fSViresh Kumar 	struct opp_device *opp_dev;
13367813dd6fSViresh Kumar 
13377813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
13387813dd6fSViresh Kumar 	if (!opp_dev)
13397813dd6fSViresh Kumar 		return NULL;
13407813dd6fSViresh Kumar 
13417813dd6fSViresh Kumar 	/* Initialize opp-dev */
13427813dd6fSViresh Kumar 	opp_dev->dev = dev;
13433d255699SViresh Kumar 
1344ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
13457813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1346ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
13477813dd6fSViresh Kumar 
13487813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1349a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1350283d55e6SViresh Kumar 
1351283d55e6SViresh Kumar 	return opp_dev;
1352283d55e6SViresh Kumar }
1353283d55e6SViresh Kumar 
1354eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
13557813dd6fSViresh Kumar {
13567813dd6fSViresh Kumar 	struct opp_table *opp_table;
13577813dd6fSViresh Kumar 	struct opp_device *opp_dev;
13587813dd6fSViresh Kumar 	int ret;
13597813dd6fSViresh Kumar 
13607813dd6fSViresh Kumar 	/*
13617813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
13627813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
13637813dd6fSViresh Kumar 	 */
13647813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
13657813dd6fSViresh Kumar 	if (!opp_table)
1366dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
13677813dd6fSViresh Kumar 
13683d255699SViresh Kumar 	mutex_init(&opp_table->lock);
13694f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
13707813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
13717eba0c76SViresh Kumar 	INIT_LIST_HEAD(&opp_table->lazy);
13727813dd6fSViresh Kumar 
137346f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
137446f48acaSViresh Kumar 	opp_table->regulator_count = -1;
137546f48acaSViresh Kumar 
13767813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
13777813dd6fSViresh Kumar 	if (!opp_dev) {
1378dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1379dd461cd9SStephan Gerhold 		goto err;
13807813dd6fSViresh Kumar 	}
13817813dd6fSViresh Kumar 
1382eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
13837813dd6fSViresh Kumar 
13846d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
13856d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1386dd461cd9SStephan Gerhold 	if (ret) {
1387dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
138832439ac7SViresh Kumar 			goto remove_opp_dev;
1389dd461cd9SStephan Gerhold 
13906d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
13916d3f922cSGeorgi Djakov 			 __func__, ret);
1392dd461cd9SStephan Gerhold 	}
13936d3f922cSGeorgi Djakov 
13947813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
13957813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
13967813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
13977813dd6fSViresh Kumar 
13987813dd6fSViresh Kumar 	return opp_table;
1399dd461cd9SStephan Gerhold 
1400976509bbSQuanyang Wang remove_opp_dev:
1401976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1402dd461cd9SStephan Gerhold err:
1403dd461cd9SStephan Gerhold 	kfree(opp_table);
1404dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
14057813dd6fSViresh Kumar }
14067813dd6fSViresh Kumar 
14077813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
14087813dd6fSViresh Kumar {
14097813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
14107813dd6fSViresh Kumar }
14117813dd6fSViresh Kumar 
141232439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
141332439ac7SViresh Kumar 					       struct opp_table *opp_table,
141432439ac7SViresh Kumar 					       bool getclk)
141532439ac7SViresh Kumar {
1416d4a4c7a4SViresh Kumar 	int ret;
1417d4a4c7a4SViresh Kumar 
141832439ac7SViresh Kumar 	/*
141932439ac7SViresh Kumar 	 * Return early if we don't need to get clk or we have already tried it
142032439ac7SViresh Kumar 	 * earlier.
142132439ac7SViresh Kumar 	 */
142232439ac7SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || opp_table->clk)
142332439ac7SViresh Kumar 		return opp_table;
142432439ac7SViresh Kumar 
142532439ac7SViresh Kumar 	/* Find clk for the device */
142632439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
142732439ac7SViresh Kumar 
1428d4a4c7a4SViresh Kumar 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
1429d4a4c7a4SViresh Kumar 	if (!ret)
143032439ac7SViresh Kumar 		return opp_table;
1431d4a4c7a4SViresh Kumar 
1432d4a4c7a4SViresh Kumar 	if (ret == -ENOENT) {
1433d4a4c7a4SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1434d4a4c7a4SViresh Kumar 		return opp_table;
1435d4a4c7a4SViresh Kumar 	}
1436d4a4c7a4SViresh Kumar 
1437d4a4c7a4SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1438d4a4c7a4SViresh Kumar 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1439d4a4c7a4SViresh Kumar 
1440d4a4c7a4SViresh Kumar 	return ERR_PTR(ret);
144132439ac7SViresh Kumar }
144232439ac7SViresh Kumar 
144327c09484SViresh Kumar /*
144427c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
144527c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
144627c09484SViresh Kumar  *
144727c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
144827c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
144927c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
145027c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
145127c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
145227c09484SViresh Kumar  * indirect users of OPP core.
145327c09484SViresh Kumar  *
145427c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
145527c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
145627c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
145727c09484SViresh Kumar  */
145832439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
145932439ac7SViresh Kumar 					 bool getclk)
14607813dd6fSViresh Kumar {
14617813dd6fSViresh Kumar 	struct opp_table *opp_table;
14627813dd6fSViresh Kumar 
146327c09484SViresh Kumar again:
14647813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
14657813dd6fSViresh Kumar 
14667813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
14677813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
14687813dd6fSViresh Kumar 		goto unlock;
14697813dd6fSViresh Kumar 
147027c09484SViresh Kumar 	/*
147127c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
147227c09484SViresh Kumar 	 * another user, wait for it to finish.
147327c09484SViresh Kumar 	 */
147427c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
147527c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
147627c09484SViresh Kumar 		cpu_relax();
147727c09484SViresh Kumar 		goto again;
147827c09484SViresh Kumar 	}
147927c09484SViresh Kumar 
148027c09484SViresh Kumar 	opp_tables_busy = true;
1481283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
148227c09484SViresh Kumar 
148327c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
148427c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
148527c09484SViresh Kumar 
1486283d55e6SViresh Kumar 	if (opp_table) {
1487ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1488283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1489dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1490283d55e6SViresh Kumar 		}
149127c09484SViresh Kumar 
149227c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
149327c09484SViresh Kumar 	} else {
149427c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
149527c09484SViresh Kumar 
149627c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
149727c09484SViresh Kumar 		if (!IS_ERR(opp_table))
149827c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1499283d55e6SViresh Kumar 	}
1500283d55e6SViresh Kumar 
150127c09484SViresh Kumar 	opp_tables_busy = false;
15027813dd6fSViresh Kumar 
15037813dd6fSViresh Kumar unlock:
15047813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
15057813dd6fSViresh Kumar 
150632439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
15077813dd6fSViresh Kumar }
1508eb7c8743SViresh Kumar 
150932439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1510e77dcb0bSViresh Kumar {
151132439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1512e77dcb0bSViresh Kumar }
1513e77dcb0bSViresh Kumar 
1514eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1515eb7c8743SViresh Kumar {
1516e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1517eb7c8743SViresh Kumar }
15187813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
15197813dd6fSViresh Kumar 
15207813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
15217813dd6fSViresh Kumar {
15227813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1523cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
15246d3f922cSGeorgi Djakov 	int i;
15257813dd6fSViresh Kumar 
1526e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1527e0df59deSViresh Kumar 	list_del(&opp_table->node);
1528e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1529e0df59deSViresh Kumar 
153081c4d8a3SViresh Kumar 	if (opp_table->current_opp)
153181c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
153281c4d8a3SViresh Kumar 
15335d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
15345d6d106fSViresh Kumar 
15357813dd6fSViresh Kumar 	/* Release clk */
15367813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
15377813dd6fSViresh Kumar 		clk_put(opp_table->clk);
15387813dd6fSViresh Kumar 
15396d3f922cSGeorgi Djakov 	if (opp_table->paths) {
15406d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
15416d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
15426d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
15436d3f922cSGeorgi Djakov 	}
15446d3f922cSGeorgi Djakov 
1545cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1546cdd6ed90SViresh Kumar 
1547cdd6ed90SViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
15483d255699SViresh Kumar 		/*
1549cdd6ed90SViresh Kumar 		 * The OPP table is getting removed, drop the performance state
1550cdd6ed90SViresh Kumar 		 * constraints.
15513d255699SViresh Kumar 		 */
1552cdd6ed90SViresh Kumar 		if (opp_table->genpd_performance_state)
1553cdd6ed90SViresh Kumar 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
15547813dd6fSViresh Kumar 
15557813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
1556cdd6ed90SViresh Kumar 	}
15577813dd6fSViresh Kumar 
15584f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
15597813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
15607813dd6fSViresh Kumar 	kfree(opp_table);
15617813dd6fSViresh Kumar }
15627813dd6fSViresh Kumar 
15637813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
15647813dd6fSViresh Kumar {
15657813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
15667813dd6fSViresh Kumar 		       &opp_table_lock);
15677813dd6fSViresh Kumar }
15687813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
15697813dd6fSViresh Kumar 
15707813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
15717813dd6fSViresh Kumar {
15727813dd6fSViresh Kumar 	kfree(opp);
15737813dd6fSViresh Kumar }
15747813dd6fSViresh Kumar 
1575cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
15767813dd6fSViresh Kumar {
1577cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1578cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1579cf1fac94SViresh Kumar 
1580cf1fac94SViresh Kumar 	list_del(&opp->node);
1581cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1582cf1fac94SViresh Kumar 
15837813dd6fSViresh Kumar 	/*
15847813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
15857813dd6fSViresh Kumar 	 * frequency/voltage list.
15867813dd6fSViresh Kumar 	 */
15877813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1588da544b61SViresh Kumar 	_of_opp_free_required_opps(opp_table, opp);
15897813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
15907813dd6fSViresh Kumar 	kfree(opp);
15911690d8bbSViresh Kumar }
15927813dd6fSViresh Kumar 
1593a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
15947813dd6fSViresh Kumar {
15957813dd6fSViresh Kumar 	kref_get(&opp->kref);
15967813dd6fSViresh Kumar }
15977813dd6fSViresh Kumar 
15987813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
15997813dd6fSViresh Kumar {
1600cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
16017813dd6fSViresh Kumar }
16027813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
16037813dd6fSViresh Kumar 
16047813dd6fSViresh Kumar /**
16057813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
16067813dd6fSViresh Kumar  * @dev:	device for which we do this operation
16077813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
16087813dd6fSViresh Kumar  *
16097813dd6fSViresh Kumar  * This function removes an opp from the opp table.
16107813dd6fSViresh Kumar  */
16117813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
16127813dd6fSViresh Kumar {
161395073b72SJakob Koschel 	struct dev_pm_opp *opp = NULL, *iter;
16147813dd6fSViresh Kumar 	struct opp_table *opp_table;
16157813dd6fSViresh Kumar 
16167813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
16177813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
16187813dd6fSViresh Kumar 		return;
16197813dd6fSViresh Kumar 
16207813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
16217813dd6fSViresh Kumar 
162295073b72SJakob Koschel 	list_for_each_entry(iter, &opp_table->opp_list, node) {
162395073b72SJakob Koschel 		if (iter->rate == freq) {
162495073b72SJakob Koschel 			opp = iter;
16257813dd6fSViresh Kumar 			break;
16267813dd6fSViresh Kumar 		}
16277813dd6fSViresh Kumar 	}
16287813dd6fSViresh Kumar 
16297813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
16307813dd6fSViresh Kumar 
163195073b72SJakob Koschel 	if (opp) {
16327813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
16330ad8c623SViresh Kumar 
16340ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
16350ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
16367813dd6fSViresh Kumar 	} else {
16377813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
16387813dd6fSViresh Kumar 			 __func__, freq);
16397813dd6fSViresh Kumar 	}
16407813dd6fSViresh Kumar 
16410ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
16427813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
16437813dd6fSViresh Kumar }
16447813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
16457813dd6fSViresh Kumar 
1646cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1647cf1fac94SViresh Kumar 					bool dynamic)
1648cf1fac94SViresh Kumar {
1649cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1650cf1fac94SViresh Kumar 
1651cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1652cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1653606a5d42SBeata Michalska 		/*
1654606a5d42SBeata Michalska 		 * Refcount must be dropped only once for each OPP by OPP core,
1655606a5d42SBeata Michalska 		 * do that with help of "removed" flag.
1656606a5d42SBeata Michalska 		 */
1657606a5d42SBeata Michalska 		if (!temp->removed && dynamic == temp->dynamic) {
1658cf1fac94SViresh Kumar 			opp = temp;
1659cf1fac94SViresh Kumar 			break;
1660cf1fac94SViresh Kumar 		}
1661cf1fac94SViresh Kumar 	}
1662cf1fac94SViresh Kumar 
1663cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1664cf1fac94SViresh Kumar 	return opp;
1665cf1fac94SViresh Kumar }
1666cf1fac94SViresh Kumar 
1667606a5d42SBeata Michalska /*
1668606a5d42SBeata Michalska  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1669606a5d42SBeata Michalska  * happen lock less to avoid circular dependency issues. This routine must be
1670606a5d42SBeata Michalska  * called without the opp_table->lock held.
1671606a5d42SBeata Michalska  */
1672606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
167303758d60SViresh Kumar {
1674cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
167503758d60SViresh Kumar 
1676606a5d42SBeata Michalska 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1677606a5d42SBeata Michalska 		opp->removed = true;
1678606a5d42SBeata Michalska 		dev_pm_opp_put(opp);
1679606a5d42SBeata Michalska 
1680606a5d42SBeata Michalska 		/* Drop the references taken by dev_pm_opp_add() */
1681606a5d42SBeata Michalska 		if (dynamic)
1682606a5d42SBeata Michalska 			dev_pm_opp_put_opp_table(opp_table);
1683606a5d42SBeata Michalska 	}
1684606a5d42SBeata Michalska }
1685606a5d42SBeata Michalska 
1686606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table)
1687606a5d42SBeata Michalska {
168803758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
168903758d60SViresh Kumar 
1690922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1691cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1692cf1fac94SViresh Kumar 		return false;
1693922ff075SViresh Kumar 	}
1694922ff075SViresh Kumar 
1695cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1696cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1697cf1fac94SViresh Kumar 		return true;
169803758d60SViresh Kumar 	}
169903758d60SViresh Kumar 
170003758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1701922ff075SViresh Kumar 
1702606a5d42SBeata Michalska 	_opp_remove_all(opp_table, false);
1703cf1fac94SViresh Kumar 	return true;
170403758d60SViresh Kumar }
170503758d60SViresh Kumar 
17061690d8bbSViresh Kumar /**
17071690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
17081690d8bbSViresh Kumar  * @dev:	device for which we do this operation
17091690d8bbSViresh Kumar  *
17101690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
17111690d8bbSViresh Kumar  */
17121690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
17131690d8bbSViresh Kumar {
17141690d8bbSViresh Kumar 	struct opp_table *opp_table;
17151690d8bbSViresh Kumar 
17161690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
17171690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
17181690d8bbSViresh Kumar 		return;
17191690d8bbSViresh Kumar 
1720606a5d42SBeata Michalska 	_opp_remove_all(opp_table, true);
17211690d8bbSViresh Kumar 
17221690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
17231690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17241690d8bbSViresh Kumar }
17251690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
17261690d8bbSViresh Kumar 
17277813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table)
17287813dd6fSViresh Kumar {
17297813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
17306d3f922cSGeorgi Djakov 	int supply_count, supply_size, icc_size;
17317813dd6fSViresh Kumar 
17327813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
17336d3f922cSGeorgi Djakov 	supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
17346d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
17356d3f922cSGeorgi Djakov 	icc_size = sizeof(*opp->bandwidth) * table->path_count;
17367813dd6fSViresh Kumar 
17377813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
17386d3f922cSGeorgi Djakov 	opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
17396d3f922cSGeorgi Djakov 
17407813dd6fSViresh Kumar 	if (!opp)
17417813dd6fSViresh Kumar 		return NULL;
17427813dd6fSViresh Kumar 
17437813dd6fSViresh Kumar 	/* Put the supplies at the end of the OPP structure as an empty array */
17447813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
17456d3f922cSGeorgi Djakov 	if (icc_size)
17466d3f922cSGeorgi Djakov 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
17477813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
17487813dd6fSViresh Kumar 
17497813dd6fSViresh Kumar 	return opp;
17507813dd6fSViresh Kumar }
17517813dd6fSViresh Kumar 
17527813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
17537813dd6fSViresh Kumar 					 struct opp_table *opp_table)
17547813dd6fSViresh Kumar {
17557813dd6fSViresh Kumar 	struct regulator *reg;
17567813dd6fSViresh Kumar 	int i;
17577813dd6fSViresh Kumar 
175890e3577bSViresh Kumar 	if (!opp_table->regulators)
175990e3577bSViresh Kumar 		return true;
176090e3577bSViresh Kumar 
17617813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
17627813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
17637813dd6fSViresh Kumar 
17647813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
17657813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
17667813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
17677813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
17687813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
17697813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
17707813dd6fSViresh Kumar 			return false;
17717813dd6fSViresh Kumar 		}
17727813dd6fSViresh Kumar 	}
17737813dd6fSViresh Kumar 
17747813dd6fSViresh Kumar 	return true;
17757813dd6fSViresh Kumar }
17767813dd6fSViresh Kumar 
17776c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
17786c591eecSSaravana Kannan {
17796c591eecSSaravana Kannan 	if (opp1->rate != opp2->rate)
17806c591eecSSaravana Kannan 		return opp1->rate < opp2->rate ? -1 : 1;
17816d3f922cSGeorgi Djakov 	if (opp1->bandwidth && opp2->bandwidth &&
17826d3f922cSGeorgi Djakov 	    opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
17836d3f922cSGeorgi Djakov 		return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
17846c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
17856c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
17866c591eecSSaravana Kannan 	return 0;
17876c591eecSSaravana Kannan }
17886c591eecSSaravana Kannan 
1789a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1790a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1791a1e8c136SViresh Kumar 			     struct list_head **head)
1792a1e8c136SViresh Kumar {
1793a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
17946c591eecSSaravana Kannan 	int opp_cmp;
1795a1e8c136SViresh Kumar 
1796a1e8c136SViresh Kumar 	/*
1797a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1798a1e8c136SViresh Kumar 	 * already present.
1799a1e8c136SViresh Kumar 	 *
1800a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1801a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1802a1e8c136SViresh Kumar 	 * loop.
1803a1e8c136SViresh Kumar 	 */
1804a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
18056c591eecSSaravana Kannan 		opp_cmp = _opp_compare_key(new_opp, opp);
18066c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1807a1e8c136SViresh Kumar 			*head = &opp->node;
1808a1e8c136SViresh Kumar 			continue;
1809a1e8c136SViresh Kumar 		}
1810a1e8c136SViresh Kumar 
18116c591eecSSaravana Kannan 		if (opp_cmp < 0)
1812a1e8c136SViresh Kumar 			return 0;
1813a1e8c136SViresh Kumar 
1814a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1815a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1816a1e8c136SViresh Kumar 			 __func__, opp->rate, opp->supplies[0].u_volt,
1817a1e8c136SViresh Kumar 			 opp->available, new_opp->rate,
1818a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1819a1e8c136SViresh Kumar 
1820a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1821a1e8c136SViresh Kumar 		return opp->available &&
1822a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1823a1e8c136SViresh Kumar 	}
1824a1e8c136SViresh Kumar 
1825a1e8c136SViresh Kumar 	return 0;
1826a1e8c136SViresh Kumar }
1827a1e8c136SViresh Kumar 
18287eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count)
18297eba0c76SViresh Kumar {
18307eba0c76SViresh Kumar 	int i;
18317eba0c76SViresh Kumar 
18327eba0c76SViresh Kumar 	for (i = 0; i < count; i++) {
18337eba0c76SViresh Kumar 		if (opp->required_opps[i]->available)
18347eba0c76SViresh Kumar 			continue;
18357eba0c76SViresh Kumar 
18367eba0c76SViresh Kumar 		opp->available = false;
18377eba0c76SViresh Kumar 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
18387eba0c76SViresh Kumar 			 __func__, opp->required_opps[i]->np, opp->rate);
18397eba0c76SViresh Kumar 		return;
18407eba0c76SViresh Kumar 	}
18417eba0c76SViresh Kumar }
18427eba0c76SViresh Kumar 
18437813dd6fSViresh Kumar /*
18447813dd6fSViresh Kumar  * Returns:
18457813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
18467813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
18477813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
18487813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
18497813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
18507813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
18517813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
18527813dd6fSViresh Kumar  */
18537813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1854a1e8c136SViresh Kumar 	     struct opp_table *opp_table, bool rate_not_available)
18557813dd6fSViresh Kumar {
18567813dd6fSViresh Kumar 	struct list_head *head;
18577813dd6fSViresh Kumar 	int ret;
18587813dd6fSViresh Kumar 
18597813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
18607813dd6fSViresh Kumar 	head = &opp_table->opp_list;
18617813dd6fSViresh Kumar 
1862a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1863a1e8c136SViresh Kumar 	if (ret) {
18647813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
18657813dd6fSViresh Kumar 		return ret;
18667813dd6fSViresh Kumar 	}
18677813dd6fSViresh Kumar 
18687813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
18697813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
18707813dd6fSViresh Kumar 
18717813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
18727813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
18737813dd6fSViresh Kumar 
1874a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
18757813dd6fSViresh Kumar 
18767813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
18777813dd6fSViresh Kumar 		new_opp->available = false;
18787813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
18797813dd6fSViresh Kumar 			 __func__, new_opp->rate);
18807813dd6fSViresh Kumar 	}
18817813dd6fSViresh Kumar 
18827eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
18837eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
18847eba0c76SViresh Kumar 		return 0;
1885cf65948dSDmitry Osipenko 
18867eba0c76SViresh Kumar 	_required_opps_available(new_opp, opp_table->required_opp_count);
1887cf65948dSDmitry Osipenko 
18887813dd6fSViresh Kumar 	return 0;
18897813dd6fSViresh Kumar }
18907813dd6fSViresh Kumar 
18917813dd6fSViresh Kumar /**
18927813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
18937813dd6fSViresh Kumar  * @opp_table:	OPP table
18947813dd6fSViresh Kumar  * @dev:	device for which we do this operation
18957813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
18967813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
18977813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
18987813dd6fSViresh Kumar  *
18997813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
19007813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
19017813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
19027813dd6fSViresh Kumar  *
19037813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
19047813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
19057813dd6fSViresh Kumar  *
19067813dd6fSViresh Kumar  * Return:
19077813dd6fSViresh Kumar  * 0		On success OR
19087813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
19097813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
19107813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
19117813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
19127813dd6fSViresh Kumar  */
19137813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
19147813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
19157813dd6fSViresh Kumar {
19167813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
19177813dd6fSViresh Kumar 	unsigned long tol;
19187813dd6fSViresh Kumar 	int ret;
19197813dd6fSViresh Kumar 
19207813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
19217813dd6fSViresh Kumar 	if (!new_opp)
19227813dd6fSViresh Kumar 		return -ENOMEM;
19237813dd6fSViresh Kumar 
19247813dd6fSViresh Kumar 	/* populate the opp table */
19257813dd6fSViresh Kumar 	new_opp->rate = freq;
19267813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
19277813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
19287813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
19297813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
19307813dd6fSViresh Kumar 	new_opp->available = true;
19317813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
19327813dd6fSViresh Kumar 
1933a1e8c136SViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table, false);
19347813dd6fSViresh Kumar 	if (ret) {
19357813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
19367813dd6fSViresh Kumar 		if (ret == -EBUSY)
19377813dd6fSViresh Kumar 			ret = 0;
19387813dd6fSViresh Kumar 		goto free_opp;
19397813dd6fSViresh Kumar 	}
19407813dd6fSViresh Kumar 
19417813dd6fSViresh Kumar 	/*
19427813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
19437813dd6fSViresh Kumar 	 * frequency/voltage list.
19447813dd6fSViresh Kumar 	 */
19457813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
19467813dd6fSViresh Kumar 	return 0;
19477813dd6fSViresh Kumar 
19487813dd6fSViresh Kumar free_opp:
19497813dd6fSViresh Kumar 	_opp_free(new_opp);
19507813dd6fSViresh Kumar 
19517813dd6fSViresh Kumar 	return ret;
19527813dd6fSViresh Kumar }
19537813dd6fSViresh Kumar 
19547813dd6fSViresh Kumar /**
195589f03984SViresh Kumar  * _opp_set_supported_hw() - Set supported platforms
19567813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
19577813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
19587813dd6fSViresh Kumar  * @count: Number of elements in the array.
19597813dd6fSViresh Kumar  *
19607813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
19617813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
19627813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
19637813dd6fSViresh Kumar  * property.
19647813dd6fSViresh Kumar  */
196589f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table,
19667813dd6fSViresh Kumar 				 const u32 *versions, unsigned int count)
19677813dd6fSViresh Kumar {
196825419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
196925419de1SViresh Kumar 	if (opp_table->supported_hw)
197089f03984SViresh Kumar 		return 0;
19717813dd6fSViresh Kumar 
19727813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
19737813dd6fSViresh Kumar 					GFP_KERNEL);
197489f03984SViresh Kumar 	if (!opp_table->supported_hw)
197589f03984SViresh Kumar 		return -ENOMEM;
19767813dd6fSViresh Kumar 
19777813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
19787813dd6fSViresh Kumar 
197989f03984SViresh Kumar 	return 0;
19807813dd6fSViresh Kumar }
19817813dd6fSViresh Kumar 
19827813dd6fSViresh Kumar /**
198389f03984SViresh Kumar  * _opp_put_supported_hw() - Releases resources blocked for supported hw
198489f03984SViresh Kumar  * @opp_table: OPP table returned by _opp_set_supported_hw().
19857813dd6fSViresh Kumar  *
19867813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
198789f03984SViresh Kumar  * _opp_set_supported_hw(). Until this is called, the opp_table structure
19887813dd6fSViresh Kumar  * will not be freed.
19897813dd6fSViresh Kumar  */
199089f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table)
19917813dd6fSViresh Kumar {
199289f03984SViresh Kumar 	if (opp_table->supported_hw) {
19937813dd6fSViresh Kumar 		kfree(opp_table->supported_hw);
19947813dd6fSViresh Kumar 		opp_table->supported_hw = NULL;
19957813dd6fSViresh Kumar 		opp_table->supported_hw_count = 0;
19967813dd6fSViresh Kumar 	}
19979c4f220fSYangtao Li }
19989c4f220fSYangtao Li 
19999c4f220fSYangtao Li /**
2000*298098e5SViresh Kumar  * _opp_set_prop_name() - Set prop-extn name
20017813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
20027813dd6fSViresh Kumar  * @name: name to postfix to properties.
20037813dd6fSViresh Kumar  *
20047813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
20057813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
20067813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
20077813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
20087813dd6fSViresh Kumar  */
2009*298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
20107813dd6fSViresh Kumar {
2011878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
20127813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
2013*298098e5SViresh Kumar 		opp_table->prop_name = kstrdup(name, GFP_KERNEL);
2014*298098e5SViresh Kumar 		if (!opp_table->prop_name)
2015*298098e5SViresh Kumar 			return -ENOMEM;
20167813dd6fSViresh Kumar 	}
20177813dd6fSViresh Kumar 
2018*298098e5SViresh Kumar 	return 0;
20197813dd6fSViresh Kumar }
20207813dd6fSViresh Kumar 
20217813dd6fSViresh Kumar /**
2022*298098e5SViresh Kumar  * _opp_put_prop_name() - Releases resources blocked for prop-name
2023*298098e5SViresh Kumar  * @opp_table: OPP table returned by _opp_set_prop_name().
20247813dd6fSViresh Kumar  *
20257813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
2026*298098e5SViresh Kumar  * _opp_set_prop_name(). Until this is called, the opp_table structure
20277813dd6fSViresh Kumar  * will not be freed.
20287813dd6fSViresh Kumar  */
2029*298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table)
20307813dd6fSViresh Kumar {
2031*298098e5SViresh Kumar 	if (opp_table->prop_name) {
20327813dd6fSViresh Kumar 		kfree(opp_table->prop_name);
20337813dd6fSViresh Kumar 		opp_table->prop_name = NULL;
20347813dd6fSViresh Kumar 	}
2035*298098e5SViresh Kumar }
20367813dd6fSViresh Kumar 
20377813dd6fSViresh Kumar /**
2038b0ec0942SViresh Kumar  * _opp_set_regulators() - Set regulator names for the device
20397813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
20407813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
20417813dd6fSViresh Kumar  * @count: Number of regulators.
20427813dd6fSViresh Kumar  *
20437813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
20447813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
20457813dd6fSViresh Kumar  * well.
20467813dd6fSViresh Kumar  *
20477813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
20487813dd6fSViresh Kumar  */
2049b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
205087686cc8SViresh Kumar 			       const char * const names[])
20517813dd6fSViresh Kumar {
205238bb3439SViresh Kumar 	struct dev_pm_opp_supply *supplies;
205387686cc8SViresh Kumar 	const char * const *temp = names;
20547813dd6fSViresh Kumar 	struct regulator *reg;
205587686cc8SViresh Kumar 	int count = 0, ret, i;
205687686cc8SViresh Kumar 
205787686cc8SViresh Kumar 	/* Count number of regulators */
205887686cc8SViresh Kumar 	while (*temp++)
205987686cc8SViresh Kumar 		count++;
206087686cc8SViresh Kumar 
206187686cc8SViresh Kumar 	if (!count)
2062b0ec0942SViresh Kumar 		return -EINVAL;
20637813dd6fSViresh Kumar 
2064779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
2065779b783cSViresh Kumar 	if (opp_table->regulators)
2066b0ec0942SViresh Kumar 		return 0;
20677813dd6fSViresh Kumar 
20687813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
20697813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
20707813dd6fSViresh Kumar 					      GFP_KERNEL);
2071b0ec0942SViresh Kumar 	if (!opp_table->regulators)
2072b0ec0942SViresh Kumar 		return -ENOMEM;
20737813dd6fSViresh Kumar 
20747813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
20757813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
20767813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
2077543256d2SKrzysztof Kozlowski 			ret = dev_err_probe(dev, PTR_ERR(reg),
2078543256d2SKrzysztof Kozlowski 					    "%s: no regulator (%s) found\n",
2079543256d2SKrzysztof Kozlowski 					    __func__, names[i]);
20807813dd6fSViresh Kumar 			goto free_regulators;
20817813dd6fSViresh Kumar 		}
20827813dd6fSViresh Kumar 
20837813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
20847813dd6fSViresh Kumar 	}
20857813dd6fSViresh Kumar 
20867813dd6fSViresh Kumar 	opp_table->regulator_count = count;
20877813dd6fSViresh Kumar 
208838bb3439SViresh Kumar 	supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
208938bb3439SViresh Kumar 	if (!supplies) {
209038bb3439SViresh Kumar 		ret = -ENOMEM;
20917813dd6fSViresh Kumar 		goto free_regulators;
209238bb3439SViresh Kumar 	}
209338bb3439SViresh Kumar 
209438bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
209538bb3439SViresh Kumar 	opp_table->sod_supplies = supplies;
209638bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
209738bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = supplies;
209838bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = supplies + count;
209938bb3439SViresh Kumar 	}
210038bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
21017813dd6fSViresh Kumar 
2102b0ec0942SViresh Kumar 	return 0;
21037813dd6fSViresh Kumar 
21047813dd6fSViresh Kumar free_regulators:
210524957db1SMarek Szyprowski 	while (i != 0)
210624957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
21077813dd6fSViresh Kumar 
21087813dd6fSViresh Kumar 	kfree(opp_table->regulators);
21097813dd6fSViresh Kumar 	opp_table->regulators = NULL;
211046f48acaSViresh Kumar 	opp_table->regulator_count = -1;
21117813dd6fSViresh Kumar 
2112b0ec0942SViresh Kumar 	return ret;
21137813dd6fSViresh Kumar }
21147813dd6fSViresh Kumar 
21157813dd6fSViresh Kumar /**
2116b0ec0942SViresh Kumar  * _opp_put_regulators() - Releases resources blocked for regulator
2117b0ec0942SViresh Kumar  * @opp_table: OPP table returned from _opp_set_regulators().
21187813dd6fSViresh Kumar  */
2119b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table)
21207813dd6fSViresh Kumar {
21217813dd6fSViresh Kumar 	int i;
21227813dd6fSViresh Kumar 
2123779b783cSViresh Kumar 	if (!opp_table->regulators)
2124b0ec0942SViresh Kumar 		return;
21257813dd6fSViresh Kumar 
212672f80ce4SViresh Kumar 	if (opp_table->enabled) {
21278d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
21288d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
21298d45719cSKamil Konieczny 	}
21308d45719cSKamil Konieczny 
213124957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
21327813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
21337813dd6fSViresh Kumar 
213438bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
213538bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
213638bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = NULL;
213738bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = NULL;
213838bb3439SViresh Kumar 	}
213938bb3439SViresh Kumar 
214038bb3439SViresh Kumar 	kfree(opp_table->sod_supplies);
214138bb3439SViresh Kumar 	opp_table->sod_supplies = NULL;
214238bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
21437813dd6fSViresh Kumar 
21447813dd6fSViresh Kumar 	kfree(opp_table->regulators);
21457813dd6fSViresh Kumar 	opp_table->regulators = NULL;
214646f48acaSViresh Kumar 	opp_table->regulator_count = -1;
21477813dd6fSViresh Kumar }
214832aee78bSYangtao Li 
21497813dd6fSViresh Kumar /**
21502368f576SViresh Kumar  * _opp_set_clknames() - Set clk names for the device
21512368f576SViresh Kumar  * @dev: Device for which clk names is being set.
21522368f576SViresh Kumar  * @names: Clk names.
21537813dd6fSViresh Kumar  *
21542368f576SViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointers to the
21552368f576SViresh Kumar  * clocks for the device. Simple cases work fine without using this routine
21562368f576SViresh Kumar  * (i.e. by passing connection-id as NULL), but for a device with multiple
21572368f576SViresh Kumar  * clocks available, the OPP core needs to know the exact names of the clks to
21582368f576SViresh Kumar  * use.
21597813dd6fSViresh Kumar  *
21607813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
21617813dd6fSViresh Kumar  */
21622368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
21632368f576SViresh Kumar 			     const char * const names[])
21647813dd6fSViresh Kumar {
21652368f576SViresh Kumar 	const char * const *temp = names;
21662368f576SViresh Kumar 	int count = 0;
21677813dd6fSViresh Kumar 
21682368f576SViresh Kumar 	/* Count number of clks */
21692368f576SViresh Kumar 	while (*temp++)
21702368f576SViresh Kumar 		count++;
21717813dd6fSViresh Kumar 
21722368f576SViresh Kumar 	/*
21732368f576SViresh Kumar 	 * This is a special case where we have a single clock, whose connection
21742368f576SViresh Kumar 	 * id name is NULL, i.e. first two entries are NULL in the array.
21752368f576SViresh Kumar 	 */
21762368f576SViresh Kumar 	if (!count && !names[1])
21772368f576SViresh Kumar 		count = 1;
21782368f576SViresh Kumar 
21792368f576SViresh Kumar 	/* We support only one clock name for now */
21802368f576SViresh Kumar 	if (count != 1)
21812368f576SViresh Kumar 		return -EINVAL;
21827813dd6fSViresh Kumar 
21830a43452bSViresh Kumar 	/* Another CPU that shares the OPP table has set the clkname ? */
21840a43452bSViresh Kumar 	if (opp_table->clk_configured)
21852368f576SViresh Kumar 		return 0;
21860a43452bSViresh Kumar 
218732439ac7SViresh Kumar 	/* clk shouldn't be initialized at this point */
21882368f576SViresh Kumar 	if (WARN_ON(opp_table->clk))
21892368f576SViresh Kumar 		return -EBUSY;
21907813dd6fSViresh Kumar 
21917813dd6fSViresh Kumar 	/* Find clk for the device */
21922368f576SViresh Kumar 	opp_table->clk = clk_get(dev, names[0]);
21937813dd6fSViresh Kumar 	if (IS_ERR(opp_table->clk)) {
21942368f576SViresh Kumar 		return dev_err_probe(dev, PTR_ERR(opp_table->clk),
2195543256d2SKrzysztof Kozlowski 				    "%s: Couldn't find clock\n", __func__);
21967813dd6fSViresh Kumar 	}
21977813dd6fSViresh Kumar 
21980a43452bSViresh Kumar 	opp_table->clk_configured = true;
21990a43452bSViresh Kumar 
22002368f576SViresh Kumar 	return 0;
22017813dd6fSViresh Kumar }
22027813dd6fSViresh Kumar 
22037813dd6fSViresh Kumar /**
22042368f576SViresh Kumar  * _opp_put_clknames() - Releases resources blocked for clks.
22052368f576SViresh Kumar  * @opp_table: OPP table returned from _opp_set_clknames().
22067813dd6fSViresh Kumar  */
22072368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table)
22087813dd6fSViresh Kumar {
22092368f576SViresh Kumar 	if (opp_table->clk_configured) {
22107813dd6fSViresh Kumar 		clk_put(opp_table->clk);
22117813dd6fSViresh Kumar 		opp_table->clk = ERR_PTR(-EINVAL);
22120a43452bSViresh Kumar 		opp_table->clk_configured = false;
22137813dd6fSViresh Kumar 	}
2214a74f681cSYangtao Li }
2215a74f681cSYangtao Li 
2216a74f681cSYangtao Li /**
22173c543b42SViresh Kumar  * _opp_register_set_opp_helper() - Register custom set OPP helper
22187813dd6fSViresh Kumar  * @dev: Device for which the helper is getting registered.
22197813dd6fSViresh Kumar  * @set_opp: Custom set OPP helper.
22207813dd6fSViresh Kumar  *
22217813dd6fSViresh Kumar  * This is useful to support complex platforms (like platforms with multiple
22227813dd6fSViresh Kumar  * regulators per device), instead of the generic OPP set rate helper.
22237813dd6fSViresh Kumar  *
22247813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
22257813dd6fSViresh Kumar  */
22263c543b42SViresh Kumar static int _opp_register_set_opp_helper(struct opp_table *opp_table,
22273c543b42SViresh Kumar 	struct device *dev, int (*set_opp)(struct dev_pm_set_opp_data *data))
22287813dd6fSViresh Kumar {
222938bb3439SViresh Kumar 	struct dev_pm_set_opp_data *data;
22307813dd6fSViresh Kumar 
22315019acc6SViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
223238bb3439SViresh Kumar 	if (opp_table->set_opp)
22333c543b42SViresh Kumar 		return 0;
223438bb3439SViresh Kumar 
223538bb3439SViresh Kumar 	data = kzalloc(sizeof(*data), GFP_KERNEL);
223638bb3439SViresh Kumar 	if (!data)
22373c543b42SViresh Kumar 		return -ENOMEM;
223838bb3439SViresh Kumar 
223938bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
224038bb3439SViresh Kumar 	opp_table->set_opp_data = data;
224138bb3439SViresh Kumar 	if (opp_table->sod_supplies) {
224238bb3439SViresh Kumar 		data->old_opp.supplies = opp_table->sod_supplies;
224338bb3439SViresh Kumar 		data->new_opp.supplies = opp_table->sod_supplies +
224438bb3439SViresh Kumar 					 opp_table->regulator_count;
224538bb3439SViresh Kumar 	}
224638bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
224738bb3439SViresh Kumar 
22487813dd6fSViresh Kumar 	opp_table->set_opp = set_opp;
22497813dd6fSViresh Kumar 
22503c543b42SViresh Kumar 	return 0;
22517813dd6fSViresh Kumar }
22527813dd6fSViresh Kumar 
22537813dd6fSViresh Kumar /**
22543c543b42SViresh Kumar  * _opp_unregister_set_opp_helper() - Releases resources blocked for set_opp helper
22553c543b42SViresh Kumar  * @opp_table: OPP table returned from _opp_register_set_opp_helper().
22567813dd6fSViresh Kumar  *
22577813dd6fSViresh Kumar  * Release resources blocked for platform specific set_opp helper.
22587813dd6fSViresh Kumar  */
22593c543b42SViresh Kumar static void _opp_unregister_set_opp_helper(struct opp_table *opp_table)
22607813dd6fSViresh Kumar {
22613c543b42SViresh Kumar 	if (opp_table->set_opp) {
22627813dd6fSViresh Kumar 		opp_table->set_opp = NULL;
226338bb3439SViresh Kumar 
226438bb3439SViresh Kumar 		mutex_lock(&opp_table->lock);
226538bb3439SViresh Kumar 		kfree(opp_table->set_opp_data);
226638bb3439SViresh Kumar 		opp_table->set_opp_data = NULL;
226738bb3439SViresh Kumar 		mutex_unlock(&opp_table->lock);
22687813dd6fSViresh Kumar 	}
2269a3c47af6SDmitry Osipenko }
2270a3c47af6SDmitry Osipenko 
2271442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table)
22726319aee1SViresh Kumar {
22736319aee1SViresh Kumar 	int index;
22746319aee1SViresh Kumar 
2275cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2276cb60e960SViresh Kumar 		return;
2277cb60e960SViresh Kumar 
22786319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
22796319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
22806319aee1SViresh Kumar 			continue;
22816319aee1SViresh Kumar 
22826319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
22836319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
22846319aee1SViresh Kumar 	}
2285c0ab9e08SViresh Kumar 
2286c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2287c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
22886319aee1SViresh Kumar }
22896319aee1SViresh Kumar 
22907813dd6fSViresh Kumar /**
2291442e7a17SViresh Kumar  * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
22926319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
22936319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
229417a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
22954f018bc0SViresh Kumar  *
22964f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
22974f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
22984f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
22994f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
23006319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
23016319aee1SViresh Kumar  * we don't need to support that separately.
23024f018bc0SViresh Kumar  *
23034f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
23046319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
23054f018bc0SViresh Kumar  *
23066319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
23076319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2308baea35e4SViresh Kumar  *
2309baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2310baea35e4SViresh Kumar  * "required-opps" are added in DT.
23114f018bc0SViresh Kumar  */
2312442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
23133734b9f2SDmitry Osipenko 			const char * const *names, struct device ***virt_devs)
23144f018bc0SViresh Kumar {
23156319aee1SViresh Kumar 	struct device *virt_dev;
2316baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
23173734b9f2SDmitry Osipenko 	const char * const *name = names;
23184f018bc0SViresh Kumar 
2319cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2320442e7a17SViresh Kumar 		return 0;
23214f018bc0SViresh Kumar 
23226319aee1SViresh Kumar 	/*
23236319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
23246319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
23256319aee1SViresh Kumar 	 * table is added.
23266319aee1SViresh Kumar 	 */
2327442e7a17SViresh Kumar 	if (!opp_table->required_opp_count)
2328442e7a17SViresh Kumar 		return -EPROBE_DEFER;
23296319aee1SViresh Kumar 
23304f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
23314f018bc0SViresh Kumar 
2332c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2333c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2334c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2335c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2336c0ab9e08SViresh Kumar 		goto unlock;
23374f018bc0SViresh Kumar 
23386319aee1SViresh Kumar 	while (*name) {
23396319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
23406319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
23416319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
23426319aee1SViresh Kumar 			goto err;
23436319aee1SViresh Kumar 		}
23444f018bc0SViresh Kumar 
23456319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
23464ea9496cSTang Bin 		if (IS_ERR_OR_NULL(virt_dev)) {
23474ea9496cSTang Bin 			ret = PTR_ERR(virt_dev) ? : -ENODEV;
23486319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
23496319aee1SViresh Kumar 			goto err;
23504f018bc0SViresh Kumar 		}
23514f018bc0SViresh Kumar 
23524f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2353baea35e4SViresh Kumar 		index++;
23546319aee1SViresh Kumar 		name++;
23556319aee1SViresh Kumar 	}
23566319aee1SViresh Kumar 
235717a8f868SViresh Kumar 	if (virt_devs)
235817a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
23594f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23604f018bc0SViresh Kumar 
2361442e7a17SViresh Kumar 	return 0;
23626319aee1SViresh Kumar 
23636319aee1SViresh Kumar err:
2364442e7a17SViresh Kumar 	_detach_genpd(opp_table);
2365c0ab9e08SViresh Kumar unlock:
23666319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2367442e7a17SViresh Kumar 	return ret;
23686319aee1SViresh Kumar 
23694f018bc0SViresh Kumar }
23704f018bc0SViresh Kumar 
23714f018bc0SViresh Kumar /**
2372442e7a17SViresh Kumar  * _opp_detach_genpd() - Detach genpd(s) from the device.
2373442e7a17SViresh Kumar  * @opp_table: OPP table returned by _opp_attach_genpd().
23744f018bc0SViresh Kumar  *
23756319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
23766319aee1SViresh Kumar  * OPP table.
23774f018bc0SViresh Kumar  */
2378442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
23794f018bc0SViresh Kumar {
23804f018bc0SViresh Kumar 	/*
23814f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
23824f018bc0SViresh Kumar 	 * used in parallel.
23834f018bc0SViresh Kumar 	 */
23844f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2385442e7a17SViresh Kumar 	_detach_genpd(opp_table);
23864f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23874f018bc0SViresh Kumar }
2388b4b9e223SDmitry Osipenko 
238911b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data)
239011b9b663SViresh Kumar {
239111b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_GENPD)
2392442e7a17SViresh Kumar 		_opp_detach_genpd(data->opp_table);
239311b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR)
2394b0ec0942SViresh Kumar 		_opp_put_regulators(data->opp_table);
239511b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_SUPPORTED_HW)
239689f03984SViresh Kumar 		_opp_put_supported_hw(data->opp_table);
239711b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
23983c543b42SViresh Kumar 		_opp_unregister_set_opp_helper(data->opp_table);
239911b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_PROP_NAME)
2400*298098e5SViresh Kumar 		_opp_put_prop_name(data->opp_table);
240111b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_CLK)
24022368f576SViresh Kumar 		_opp_put_clknames(data->opp_table);
240311b9b663SViresh Kumar 
240411b9b663SViresh Kumar 	dev_pm_opp_put_opp_table(data->opp_table);
240511b9b663SViresh Kumar 	kfree(data);
240611b9b663SViresh Kumar }
240711b9b663SViresh Kumar 
240811b9b663SViresh Kumar /**
240911b9b663SViresh Kumar  * dev_pm_opp_set_config() - Set OPP configuration for the device.
241011b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
241111b9b663SViresh Kumar  * @config: OPP configuration.
241211b9b663SViresh Kumar  *
241311b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
241411b9b663SViresh Kumar  *
241511b9b663SViresh Kumar  * This must be called before any OPPs are initialized for the device. This may
241611b9b663SViresh Kumar  * be called multiple times for the same OPP table, for example once for each
241711b9b663SViresh Kumar  * CPU that share the same table. This must be balanced by the same number of
241811b9b663SViresh Kumar  * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
241911b9b663SViresh Kumar  *
242011b9b663SViresh Kumar  * This returns a token to the caller, which must be passed to
242111b9b663SViresh Kumar  * dev_pm_opp_clear_config() to free the resources later. The value of the
242211b9b663SViresh Kumar  * returned token will be >= 1 for success and negative for errors. The minimum
242311b9b663SViresh Kumar  * value of 1 is chosen here to make it easy for callers to manage the resource.
242411b9b663SViresh Kumar  */
242511b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
242611b9b663SViresh Kumar {
2427*298098e5SViresh Kumar 	struct opp_table *opp_table;
242811b9b663SViresh Kumar 	struct opp_config_data *data;
242911b9b663SViresh Kumar 	unsigned int id;
243011b9b663SViresh Kumar 	int ret;
243111b9b663SViresh Kumar 
243211b9b663SViresh Kumar 	data = kmalloc(sizeof(*data), GFP_KERNEL);
243311b9b663SViresh Kumar 	if (!data)
243411b9b663SViresh Kumar 		return -ENOMEM;
243511b9b663SViresh Kumar 
243611b9b663SViresh Kumar 	opp_table = _add_opp_table(dev, false);
243711b9b663SViresh Kumar 	if (IS_ERR(opp_table)) {
243811b9b663SViresh Kumar 		kfree(data);
243911b9b663SViresh Kumar 		return PTR_ERR(opp_table);
244011b9b663SViresh Kumar 	}
244111b9b663SViresh Kumar 
244211b9b663SViresh Kumar 	data->opp_table = opp_table;
244311b9b663SViresh Kumar 	data->flags = 0;
244411b9b663SViresh Kumar 
244511b9b663SViresh Kumar 	/* This should be called before OPPs are initialized */
244611b9b663SViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
244711b9b663SViresh Kumar 		ret = -EBUSY;
244811b9b663SViresh Kumar 		goto err;
244911b9b663SViresh Kumar 	}
245011b9b663SViresh Kumar 
245111b9b663SViresh Kumar 	/* Configure clocks */
245211b9b663SViresh Kumar 	if (config->clk_names) {
24532368f576SViresh Kumar 		ret = _opp_set_clknames(opp_table, dev, config->clk_names);
24542368f576SViresh Kumar 		if (ret)
245511b9b663SViresh Kumar 			goto err;
245611b9b663SViresh Kumar 
245711b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_CLK;
245811b9b663SViresh Kumar 	}
245911b9b663SViresh Kumar 
246011b9b663SViresh Kumar 	/* Configure property names */
246111b9b663SViresh Kumar 	if (config->prop_name) {
2462*298098e5SViresh Kumar 		ret = _opp_set_prop_name(opp_table, config->prop_name);
2463*298098e5SViresh Kumar 		if (ret)
246411b9b663SViresh Kumar 			goto err;
246511b9b663SViresh Kumar 
246611b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_PROP_NAME;
246711b9b663SViresh Kumar 	}
246811b9b663SViresh Kumar 
246911b9b663SViresh Kumar 	/* Configure opp helper */
247011b9b663SViresh Kumar 	if (config->set_opp) {
24713c543b42SViresh Kumar 		ret = _opp_register_set_opp_helper(opp_table, dev,
24723c543b42SViresh Kumar 						   config->set_opp);
24733c543b42SViresh Kumar 		if (ret)
247411b9b663SViresh Kumar 			goto err;
247511b9b663SViresh Kumar 
247611b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR_HELPER;
247711b9b663SViresh Kumar 	}
247811b9b663SViresh Kumar 
247911b9b663SViresh Kumar 	/* Configure supported hardware */
248011b9b663SViresh Kumar 	if (config->supported_hw) {
248189f03984SViresh Kumar 		ret = _opp_set_supported_hw(opp_table, config->supported_hw,
248211b9b663SViresh Kumar 					    config->supported_hw_count);
248389f03984SViresh Kumar 		if (ret)
248411b9b663SViresh Kumar 			goto err;
248511b9b663SViresh Kumar 
248611b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_SUPPORTED_HW;
248711b9b663SViresh Kumar 	}
248811b9b663SViresh Kumar 
248911b9b663SViresh Kumar 	/* Configure supplies */
249011b9b663SViresh Kumar 	if (config->regulator_names) {
2491b0ec0942SViresh Kumar 		ret = _opp_set_regulators(opp_table, dev,
2492b0ec0942SViresh Kumar 					  config->regulator_names);
2493b0ec0942SViresh Kumar 		if (ret)
249411b9b663SViresh Kumar 			goto err;
249511b9b663SViresh Kumar 
249611b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR;
249711b9b663SViresh Kumar 	}
249811b9b663SViresh Kumar 
249911b9b663SViresh Kumar 	/* Attach genpds */
250011b9b663SViresh Kumar 	if (config->genpd_names) {
2501442e7a17SViresh Kumar 		ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
250211b9b663SViresh Kumar 					config->virt_devs);
2503442e7a17SViresh Kumar 		if (ret)
250411b9b663SViresh Kumar 			goto err;
250511b9b663SViresh Kumar 
250611b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_GENPD;
250711b9b663SViresh Kumar 	}
250811b9b663SViresh Kumar 
250911b9b663SViresh Kumar 	ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
251011b9b663SViresh Kumar 		       GFP_KERNEL);
251111b9b663SViresh Kumar 	if (ret)
251211b9b663SViresh Kumar 		goto err;
251311b9b663SViresh Kumar 
251411b9b663SViresh Kumar 	return id;
251511b9b663SViresh Kumar 
251611b9b663SViresh Kumar err:
251711b9b663SViresh Kumar 	_opp_clear_config(data);
251811b9b663SViresh Kumar 	return ret;
251911b9b663SViresh Kumar }
252011b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
252111b9b663SViresh Kumar 
252211b9b663SViresh Kumar /**
252311b9b663SViresh Kumar  * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
252411b9b663SViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_config().
252511b9b663SViresh Kumar  *
252611b9b663SViresh Kumar  * This allows all device OPP configurations to be cleared at once. This must be
252711b9b663SViresh Kumar  * called once for each call made to dev_pm_opp_set_config(), in order to free
252811b9b663SViresh Kumar  * the OPPs properly.
252911b9b663SViresh Kumar  *
253011b9b663SViresh Kumar  * Currently the first call itself ends up freeing all the OPP configurations,
253111b9b663SViresh Kumar  * while the later ones only drop the OPP table reference. This works well for
253211b9b663SViresh Kumar  * now as we would never want to use an half initialized OPP table and want to
253311b9b663SViresh Kumar  * remove the configurations together.
253411b9b663SViresh Kumar  */
253511b9b663SViresh Kumar void dev_pm_opp_clear_config(int token)
253611b9b663SViresh Kumar {
253711b9b663SViresh Kumar 	struct opp_config_data *data;
253811b9b663SViresh Kumar 
253911b9b663SViresh Kumar 	/*
254011b9b663SViresh Kumar 	 * This lets the callers call this unconditionally and keep their code
254111b9b663SViresh Kumar 	 * simple.
254211b9b663SViresh Kumar 	 */
254311b9b663SViresh Kumar 	if (unlikely(token <= 0))
254411b9b663SViresh Kumar 		return;
254511b9b663SViresh Kumar 
254611b9b663SViresh Kumar 	data = xa_erase(&opp_configs, token);
254711b9b663SViresh Kumar 	if (WARN_ON(!data))
254811b9b663SViresh Kumar 		return;
254911b9b663SViresh Kumar 
255011b9b663SViresh Kumar 	_opp_clear_config(data);
255111b9b663SViresh Kumar }
255211b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
255311b9b663SViresh Kumar 
255411b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token)
255511b9b663SViresh Kumar {
255611b9b663SViresh Kumar 	dev_pm_opp_clear_config((unsigned long)token);
255711b9b663SViresh Kumar }
255811b9b663SViresh Kumar 
255911b9b663SViresh Kumar /**
256011b9b663SViresh Kumar  * devm_pm_opp_set_config() - Set OPP configuration for the device.
256111b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
256211b9b663SViresh Kumar  * @config: OPP configuration.
256311b9b663SViresh Kumar  *
256411b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
256511b9b663SViresh Kumar  * This is a resource-managed variant of dev_pm_opp_set_config().
256611b9b663SViresh Kumar  *
256711b9b663SViresh Kumar  * Return: 0 on success and errorno otherwise.
256811b9b663SViresh Kumar  */
256911b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
257011b9b663SViresh Kumar {
257111b9b663SViresh Kumar 	int token = dev_pm_opp_set_config(dev, config);
257211b9b663SViresh Kumar 
257311b9b663SViresh Kumar 	if (token < 0)
257411b9b663SViresh Kumar 		return token;
257511b9b663SViresh Kumar 
257611b9b663SViresh Kumar 	return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
257711b9b663SViresh Kumar 					(void *) ((unsigned long) token));
257811b9b663SViresh Kumar }
257911b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
258011b9b663SViresh Kumar 
25814f018bc0SViresh Kumar /**
25827d8658efSSaravana Kannan  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
25837d8658efSSaravana Kannan  * @src_table: OPP table which has @dst_table as one of its required OPP table.
25847d8658efSSaravana Kannan  * @dst_table: Required OPP table of the @src_table.
25857d8658efSSaravana Kannan  * @src_opp: OPP from the @src_table.
25867d8658efSSaravana Kannan  *
25877d8658efSSaravana Kannan  * This function returns the OPP (present in @dst_table) pointed out by the
25887d8658efSSaravana Kannan  * "required-opps" property of the @src_opp (present in @src_table).
25897d8658efSSaravana Kannan  *
25907d8658efSSaravana Kannan  * The callers are required to call dev_pm_opp_put() for the returned OPP after
25917d8658efSSaravana Kannan  * use.
25927d8658efSSaravana Kannan  *
25937d8658efSSaravana Kannan  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
25947d8658efSSaravana Kannan  */
25957d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
25967d8658efSSaravana Kannan 						 struct opp_table *dst_table,
25977d8658efSSaravana Kannan 						 struct dev_pm_opp *src_opp)
25987d8658efSSaravana Kannan {
25997d8658efSSaravana Kannan 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
26007d8658efSSaravana Kannan 	int i;
26017d8658efSSaravana Kannan 
26027d8658efSSaravana Kannan 	if (!src_table || !dst_table || !src_opp ||
26037d8658efSSaravana Kannan 	    !src_table->required_opp_tables)
26047d8658efSSaravana Kannan 		return ERR_PTR(-EINVAL);
26057d8658efSSaravana Kannan 
26067d8658efSSaravana Kannan 	/* required-opps not fully initialized yet */
26077d8658efSSaravana Kannan 	if (lazy_linking_pending(src_table))
26087d8658efSSaravana Kannan 		return ERR_PTR(-EBUSY);
26097d8658efSSaravana Kannan 
26107d8658efSSaravana Kannan 	for (i = 0; i < src_table->required_opp_count; i++) {
26117d8658efSSaravana Kannan 		if (src_table->required_opp_tables[i] == dst_table) {
26127d8658efSSaravana Kannan 			mutex_lock(&src_table->lock);
26137d8658efSSaravana Kannan 
26147d8658efSSaravana Kannan 			list_for_each_entry(opp, &src_table->opp_list, node) {
26157d8658efSSaravana Kannan 				if (opp == src_opp) {
26167d8658efSSaravana Kannan 					dest_opp = opp->required_opps[i];
26177d8658efSSaravana Kannan 					dev_pm_opp_get(dest_opp);
26187d8658efSSaravana Kannan 					break;
26197d8658efSSaravana Kannan 				}
26207d8658efSSaravana Kannan 			}
26217d8658efSSaravana Kannan 
26227d8658efSSaravana Kannan 			mutex_unlock(&src_table->lock);
26237d8658efSSaravana Kannan 			break;
26247d8658efSSaravana Kannan 		}
26257d8658efSSaravana Kannan 	}
26267d8658efSSaravana Kannan 
26277d8658efSSaravana Kannan 	if (IS_ERR(dest_opp)) {
26287d8658efSSaravana Kannan 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
26297d8658efSSaravana Kannan 		       src_table, dst_table);
26307d8658efSSaravana Kannan 	}
26317d8658efSSaravana Kannan 
26327d8658efSSaravana Kannan 	return dest_opp;
26337d8658efSSaravana Kannan }
26347d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
26357d8658efSSaravana Kannan 
26367d8658efSSaravana Kannan /**
2637c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2638c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2639c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2640c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2641c8a59103SViresh Kumar  *
2642c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2643c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2644c8a59103SViresh Kumar  * performance state set to @pstate.
2645c8a59103SViresh Kumar  *
2646c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2647c8a59103SViresh Kumar  * value on errors.
2648c8a59103SViresh Kumar  */
2649c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2650c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2651c8a59103SViresh Kumar 				       unsigned int pstate)
2652c8a59103SViresh Kumar {
2653c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2654c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2655c8a59103SViresh Kumar 	int i;
2656c8a59103SViresh Kumar 
2657c8a59103SViresh Kumar 	/*
2658c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2659c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2660c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2661c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2662c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2663c8a59103SViresh Kumar 	 */
2664f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2665c8a59103SViresh Kumar 		return pstate;
2666c8a59103SViresh Kumar 
26677eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
26687eba0c76SViresh Kumar 	if (lazy_linking_pending(src_table))
26697eba0c76SViresh Kumar 		return -EBUSY;
26707eba0c76SViresh Kumar 
2671c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2672c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2673c8a59103SViresh Kumar 			break;
2674c8a59103SViresh Kumar 	}
2675c8a59103SViresh Kumar 
2676c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2677c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2678c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2679c8a59103SViresh Kumar 		return -EINVAL;
2680c8a59103SViresh Kumar 	}
2681c8a59103SViresh Kumar 
2682c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2683c8a59103SViresh Kumar 
2684c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
2685c8a59103SViresh Kumar 		if (opp->pstate == pstate) {
2686c8a59103SViresh Kumar 			dest_pstate = opp->required_opps[i]->pstate;
2687c8a59103SViresh Kumar 			goto unlock;
2688c8a59103SViresh Kumar 		}
2689c8a59103SViresh Kumar 	}
2690c8a59103SViresh Kumar 
2691c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2692c8a59103SViresh Kumar 	       dst_table);
2693c8a59103SViresh Kumar 
2694c8a59103SViresh Kumar unlock:
2695c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2696c8a59103SViresh Kumar 
2697c8a59103SViresh Kumar 	return dest_pstate;
2698c8a59103SViresh Kumar }
2699c8a59103SViresh Kumar 
2700c8a59103SViresh Kumar /**
27017813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
27027813dd6fSViresh Kumar  * @dev:	device for which we do this operation
27037813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
27047813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
27057813dd6fSViresh Kumar  *
27067813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
27077813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
27087813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
27097813dd6fSViresh Kumar  *
27107813dd6fSViresh Kumar  * Return:
27117813dd6fSViresh Kumar  * 0		On success OR
27127813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
27137813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
27147813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
27157813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
27167813dd6fSViresh Kumar  */
27177813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
27187813dd6fSViresh Kumar {
27197813dd6fSViresh Kumar 	struct opp_table *opp_table;
27207813dd6fSViresh Kumar 	int ret;
27217813dd6fSViresh Kumar 
272232439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2723dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2724dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
27257813dd6fSViresh Kumar 
272646f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
272746f48acaSViresh Kumar 	opp_table->regulator_count = 1;
272846f48acaSViresh Kumar 
27297813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
27300ad8c623SViresh Kumar 	if (ret)
27317813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
27320ad8c623SViresh Kumar 
27337813dd6fSViresh Kumar 	return ret;
27347813dd6fSViresh Kumar }
27357813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
27367813dd6fSViresh Kumar 
27377813dd6fSViresh Kumar /**
27387813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
27397813dd6fSViresh Kumar  * @dev:		device for which we do this operation
27407813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
27417813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
27427813dd6fSViresh Kumar  *
27437813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
27447813dd6fSViresh Kumar  * which is isolated here.
27457813dd6fSViresh Kumar  *
27467813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
27477813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
27487813dd6fSViresh Kumar  * successful.
27497813dd6fSViresh Kumar  */
27507813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
27517813dd6fSViresh Kumar 				 bool availability_req)
27527813dd6fSViresh Kumar {
27537813dd6fSViresh Kumar 	struct opp_table *opp_table;
27547813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
27557813dd6fSViresh Kumar 	int r = 0;
27567813dd6fSViresh Kumar 
27577813dd6fSViresh Kumar 	/* Find the opp_table */
27587813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
27597813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
27607813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
27617813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
27627813dd6fSViresh Kumar 		return r;
27637813dd6fSViresh Kumar 	}
27647813dd6fSViresh Kumar 
27657813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
27667813dd6fSViresh Kumar 
27677813dd6fSViresh Kumar 	/* Do we have the frequency? */
27687813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
27697813dd6fSViresh Kumar 		if (tmp_opp->rate == freq) {
27707813dd6fSViresh Kumar 			opp = tmp_opp;
27717813dd6fSViresh Kumar 			break;
27727813dd6fSViresh Kumar 		}
27737813dd6fSViresh Kumar 	}
27747813dd6fSViresh Kumar 
27757813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
27767813dd6fSViresh Kumar 		r = PTR_ERR(opp);
27777813dd6fSViresh Kumar 		goto unlock;
27787813dd6fSViresh Kumar 	}
27797813dd6fSViresh Kumar 
27807813dd6fSViresh Kumar 	/* Is update really needed? */
27817813dd6fSViresh Kumar 	if (opp->available == availability_req)
27827813dd6fSViresh Kumar 		goto unlock;
27837813dd6fSViresh Kumar 
27847813dd6fSViresh Kumar 	opp->available = availability_req;
27857813dd6fSViresh Kumar 
27867813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
27877813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
27887813dd6fSViresh Kumar 
27897813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
27907813dd6fSViresh Kumar 	if (availability_req)
27917813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
27927813dd6fSViresh Kumar 					     opp);
27937813dd6fSViresh Kumar 	else
27947813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
27957813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
27967813dd6fSViresh Kumar 
27977813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
27987813dd6fSViresh Kumar 	goto put_table;
27997813dd6fSViresh Kumar 
28007813dd6fSViresh Kumar unlock:
28017813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
28027813dd6fSViresh Kumar put_table:
28037813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
28047813dd6fSViresh Kumar 	return r;
28057813dd6fSViresh Kumar }
28067813dd6fSViresh Kumar 
28077813dd6fSViresh Kumar /**
280825cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
280925cb20a2SStephen Boyd  * @dev:		device for which we do this operation
281025cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
281125cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
281225cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
281325cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
281425cb20a2SStephen Boyd  *
281525cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
281625cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
281725cb20a2SStephen Boyd  * successful.
281825cb20a2SStephen Boyd  */
281925cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
282025cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
282125cb20a2SStephen Boyd 			      unsigned long u_volt_max)
282225cb20a2SStephen Boyd 
282325cb20a2SStephen Boyd {
282425cb20a2SStephen Boyd 	struct opp_table *opp_table;
282525cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
282625cb20a2SStephen Boyd 	int r = 0;
282725cb20a2SStephen Boyd 
282825cb20a2SStephen Boyd 	/* Find the opp_table */
282925cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
283025cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
283125cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
283225cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
283325cb20a2SStephen Boyd 		return r;
283425cb20a2SStephen Boyd 	}
283525cb20a2SStephen Boyd 
283625cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
283725cb20a2SStephen Boyd 
283825cb20a2SStephen Boyd 	/* Do we have the frequency? */
283925cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
284025cb20a2SStephen Boyd 		if (tmp_opp->rate == freq) {
284125cb20a2SStephen Boyd 			opp = tmp_opp;
284225cb20a2SStephen Boyd 			break;
284325cb20a2SStephen Boyd 		}
284425cb20a2SStephen Boyd 	}
284525cb20a2SStephen Boyd 
284625cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
284725cb20a2SStephen Boyd 		r = PTR_ERR(opp);
284825cb20a2SStephen Boyd 		goto adjust_unlock;
284925cb20a2SStephen Boyd 	}
285025cb20a2SStephen Boyd 
285125cb20a2SStephen Boyd 	/* Is update really needed? */
285225cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
285325cb20a2SStephen Boyd 		goto adjust_unlock;
285425cb20a2SStephen Boyd 
285525cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
285625cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
285725cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
285825cb20a2SStephen Boyd 
285925cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
286025cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
286125cb20a2SStephen Boyd 
286225cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
286325cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
286425cb20a2SStephen Boyd 				     opp);
286525cb20a2SStephen Boyd 
286625cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
286725cb20a2SStephen Boyd 	goto adjust_put_table;
286825cb20a2SStephen Boyd 
286925cb20a2SStephen Boyd adjust_unlock:
287025cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
287125cb20a2SStephen Boyd adjust_put_table:
287225cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
287325cb20a2SStephen Boyd 	return r;
287425cb20a2SStephen Boyd }
287503649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
287625cb20a2SStephen Boyd 
287725cb20a2SStephen Boyd /**
28787813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
28797813dd6fSViresh Kumar  * @dev:	device for which we do this operation
28807813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
28817813dd6fSViresh Kumar  *
28827813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
28837813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
28847813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
28857813dd6fSViresh Kumar  *
28867813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
28877813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
28887813dd6fSViresh Kumar  * successful.
28897813dd6fSViresh Kumar  */
28907813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
28917813dd6fSViresh Kumar {
28927813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
28937813dd6fSViresh Kumar }
28947813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
28957813dd6fSViresh Kumar 
28967813dd6fSViresh Kumar /**
28977813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
28987813dd6fSViresh Kumar  * @dev:	device for which we do this operation
28997813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
29007813dd6fSViresh Kumar  *
29017813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
29027813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
29037813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
29047813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
29057813dd6fSViresh Kumar  *
29067813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
29077813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
29087813dd6fSViresh Kumar  * successful.
29097813dd6fSViresh Kumar  */
29107813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
29117813dd6fSViresh Kumar {
29127813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
29137813dd6fSViresh Kumar }
29147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
29157813dd6fSViresh Kumar 
29167813dd6fSViresh Kumar /**
29177813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
29187813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
29197813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
29207813dd6fSViresh Kumar  *
29217813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
29227813dd6fSViresh Kumar  */
29237813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
29247813dd6fSViresh Kumar {
29257813dd6fSViresh Kumar 	struct opp_table *opp_table;
29267813dd6fSViresh Kumar 	int ret;
29277813dd6fSViresh Kumar 
29287813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29297813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
29307813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
29317813dd6fSViresh Kumar 
29327813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
29337813dd6fSViresh Kumar 
29347813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29357813dd6fSViresh Kumar 
29367813dd6fSViresh Kumar 	return ret;
29377813dd6fSViresh Kumar }
29387813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
29397813dd6fSViresh Kumar 
29407813dd6fSViresh Kumar /**
29417813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
29427813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
29437813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
29447813dd6fSViresh Kumar  *
29457813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
29467813dd6fSViresh Kumar  */
29477813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
29487813dd6fSViresh Kumar 				   struct notifier_block *nb)
29497813dd6fSViresh Kumar {
29507813dd6fSViresh Kumar 	struct opp_table *opp_table;
29517813dd6fSViresh Kumar 	int ret;
29527813dd6fSViresh Kumar 
29537813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29547813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
29557813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
29567813dd6fSViresh Kumar 
29577813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
29587813dd6fSViresh Kumar 
29597813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29607813dd6fSViresh Kumar 
29617813dd6fSViresh Kumar 	return ret;
29627813dd6fSViresh Kumar }
29637813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
29647813dd6fSViresh Kumar 
29658aaf6264SViresh Kumar /**
29668aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
29678aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
29688aaf6264SViresh Kumar  *
29698aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
29708aaf6264SViresh Kumar  * dynamically added entries.
29718aaf6264SViresh Kumar  */
29728aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
29737813dd6fSViresh Kumar {
29747813dd6fSViresh Kumar 	struct opp_table *opp_table;
29757813dd6fSViresh Kumar 
29767813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
29777813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29787813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
29797813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
29807813dd6fSViresh Kumar 
29817813dd6fSViresh Kumar 		if (error != -ENODEV)
29827813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
29837813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
29847813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
29857813dd6fSViresh Kumar 			     error);
29867813dd6fSViresh Kumar 		return;
29877813dd6fSViresh Kumar 	}
29887813dd6fSViresh Kumar 
2989922ff075SViresh Kumar 	/*
2990922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
2991922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
2992922ff075SViresh Kumar 	 **/
2993922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
2994cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
2995cdd6ed90SViresh Kumar 
2996922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
29977813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29987813dd6fSViresh Kumar }
29997813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
3000ce8073d8SDmitry Osipenko 
3001ce8073d8SDmitry Osipenko /**
3002ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
3003ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
3004ce8073d8SDmitry Osipenko  *
3005ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
3006ce8073d8SDmitry Osipenko  *
3007ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
3008ce8073d8SDmitry Osipenko  */
3009ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
3010ce8073d8SDmitry Osipenko {
3011ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
3012ce8073d8SDmitry Osipenko 	struct regulator *reg;
3013ce8073d8SDmitry Osipenko 	int i, ret = 0;
3014ce8073d8SDmitry Osipenko 
3015ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
3016ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
3017ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
3018ce8073d8SDmitry Osipenko 		return 0;
3019ce8073d8SDmitry Osipenko 
3020ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
3021ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
3022ce8073d8SDmitry Osipenko 		goto put_table;
3023ce8073d8SDmitry Osipenko 
3024ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
3025ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
3026ce8073d8SDmitry Osipenko 		goto put_table;
3027ce8073d8SDmitry Osipenko 
3028ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
3029ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
3030ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
3031ce8073d8SDmitry Osipenko 		if (ret)
3032ce8073d8SDmitry Osipenko 			break;
3033ce8073d8SDmitry Osipenko 	}
3034ce8073d8SDmitry Osipenko put_table:
3035ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
3036ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
3037ce8073d8SDmitry Osipenko 
3038ce8073d8SDmitry Osipenko 	return ret;
3039ce8073d8SDmitry Osipenko }
3040ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
3041