xref: /openbmc/linux/drivers/opp/core.c (revision 00ce3873f730fb24c657854ec04374358e711838)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27813dd6fSViresh Kumar /*
37813dd6fSViresh Kumar  * Generic OPP Interface
47813dd6fSViresh Kumar  *
57813dd6fSViresh Kumar  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
67813dd6fSViresh Kumar  *	Nishanth Menon
77813dd6fSViresh Kumar  *	Romit Dasgupta
87813dd6fSViresh Kumar  *	Kevin Hilman
97813dd6fSViresh Kumar  */
107813dd6fSViresh Kumar 
117813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
127813dd6fSViresh Kumar 
137813dd6fSViresh Kumar #include <linux/clk.h>
147813dd6fSViresh Kumar #include <linux/errno.h>
157813dd6fSViresh Kumar #include <linux/err.h>
167813dd6fSViresh Kumar #include <linux/slab.h>
177813dd6fSViresh Kumar #include <linux/device.h>
187813dd6fSViresh Kumar #include <linux/export.h>
19009acd19SViresh Kumar #include <linux/pm_domain.h>
207813dd6fSViresh Kumar #include <linux/regulator/consumer.h>
217813dd6fSViresh Kumar 
227813dd6fSViresh Kumar #include "opp.h"
237813dd6fSViresh Kumar 
247813dd6fSViresh Kumar /*
257813dd6fSViresh Kumar  * The root of the list of all opp-tables. All opp_table structures branch off
267813dd6fSViresh Kumar  * from here, with each opp_table containing the list of opps it supports in
277813dd6fSViresh Kumar  * various states of availability.
287813dd6fSViresh Kumar  */
297813dd6fSViresh Kumar LIST_HEAD(opp_tables);
307eba0c76SViresh Kumar 
317eba0c76SViresh Kumar /* OPP tables with uninitialized required OPPs */
327eba0c76SViresh Kumar LIST_HEAD(lazy_opp_tables);
337eba0c76SViresh Kumar 
347813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
357813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
3627c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */
3727c09484SViresh Kumar static bool opp_tables_busy;
387813dd6fSViresh Kumar 
399e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
407813dd6fSViresh Kumar {
417813dd6fSViresh Kumar 	struct opp_device *opp_dev;
429e62edacSViresh Kumar 	bool found = false;
437813dd6fSViresh Kumar 
449e62edacSViresh Kumar 	mutex_lock(&opp_table->lock);
457813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
469e62edacSViresh Kumar 		if (opp_dev->dev == dev) {
479e62edacSViresh Kumar 			found = true;
489e62edacSViresh Kumar 			break;
499e62edacSViresh Kumar 		}
507813dd6fSViresh Kumar 
519e62edacSViresh Kumar 	mutex_unlock(&opp_table->lock);
529e62edacSViresh Kumar 	return found;
537813dd6fSViresh Kumar }
547813dd6fSViresh Kumar 
557813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
567813dd6fSViresh Kumar {
577813dd6fSViresh Kumar 	struct opp_table *opp_table;
587813dd6fSViresh Kumar 
597813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
609e62edacSViresh Kumar 		if (_find_opp_dev(dev, opp_table)) {
617813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
627813dd6fSViresh Kumar 			return opp_table;
637813dd6fSViresh Kumar 		}
647813dd6fSViresh Kumar 	}
657813dd6fSViresh Kumar 
667813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
677813dd6fSViresh Kumar }
687813dd6fSViresh Kumar 
697813dd6fSViresh Kumar /**
707813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
717813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
727813dd6fSViresh Kumar  *
737813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
747813dd6fSViresh Kumar  *
757813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
767813dd6fSViresh Kumar  * -EINVAL based on type of error.
777813dd6fSViresh Kumar  *
787813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
797813dd6fSViresh Kumar  */
807813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
817813dd6fSViresh Kumar {
827813dd6fSViresh Kumar 	struct opp_table *opp_table;
837813dd6fSViresh Kumar 
847813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
857813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
867813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
877813dd6fSViresh Kumar 	}
887813dd6fSViresh Kumar 
897813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
907813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
917813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
927813dd6fSViresh Kumar 
937813dd6fSViresh Kumar 	return opp_table;
947813dd6fSViresh Kumar }
957813dd6fSViresh Kumar 
967813dd6fSViresh Kumar /**
977813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
987813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
997813dd6fSViresh Kumar  *
1007813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
1017813dd6fSViresh Kumar  * return 0
1027813dd6fSViresh Kumar  *
1037813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1047813dd6fSViresh Kumar  */
1057813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1067813dd6fSViresh Kumar {
1077813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1087813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1097813dd6fSViresh Kumar 		return 0;
1107813dd6fSViresh Kumar 	}
1117813dd6fSViresh Kumar 
1127813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1137813dd6fSViresh Kumar }
1147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1157813dd6fSViresh Kumar 
1167813dd6fSViresh Kumar /**
1174f9a7a1dSLukasz Luba  * dev_pm_opp_get_power() - Gets the power corresponding to an opp
1184f9a7a1dSLukasz Luba  * @opp:	opp for which power has to be returned for
1194f9a7a1dSLukasz Luba  *
1204f9a7a1dSLukasz Luba  * Return: power in micro watt corresponding to the opp, else
1214f9a7a1dSLukasz Luba  * return 0
1224f9a7a1dSLukasz Luba  *
1234f9a7a1dSLukasz Luba  * This is useful only for devices with single power supply.
1244f9a7a1dSLukasz Luba  */
1254f9a7a1dSLukasz Luba unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
1264f9a7a1dSLukasz Luba {
1274f9a7a1dSLukasz Luba 	unsigned long opp_power = 0;
1284f9a7a1dSLukasz Luba 	int i;
1294f9a7a1dSLukasz Luba 
1304f9a7a1dSLukasz Luba 	if (IS_ERR_OR_NULL(opp)) {
1314f9a7a1dSLukasz Luba 		pr_err("%s: Invalid parameters\n", __func__);
1324f9a7a1dSLukasz Luba 		return 0;
1334f9a7a1dSLukasz Luba 	}
1344f9a7a1dSLukasz Luba 	for (i = 0; i < opp->opp_table->regulator_count; i++)
1354f9a7a1dSLukasz Luba 		opp_power += opp->supplies[i].u_watt;
1364f9a7a1dSLukasz Luba 
1374f9a7a1dSLukasz Luba 	return opp_power;
1384f9a7a1dSLukasz Luba }
1394f9a7a1dSLukasz Luba EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
1404f9a7a1dSLukasz Luba 
1414f9a7a1dSLukasz Luba /**
1427813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1437813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1447813dd6fSViresh Kumar  *
1457813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1467813dd6fSViresh Kumar  * return 0
1477813dd6fSViresh Kumar  */
1487813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1497813dd6fSViresh Kumar {
15006a8a059SAndrew-sh.Cheng 	if (IS_ERR_OR_NULL(opp)) {
1517813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1527813dd6fSViresh Kumar 		return 0;
1537813dd6fSViresh Kumar 	}
1547813dd6fSViresh Kumar 
1557813dd6fSViresh Kumar 	return opp->rate;
1567813dd6fSViresh Kumar }
1577813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
1587813dd6fSViresh Kumar 
1597813dd6fSViresh Kumar /**
1605b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
1615b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
1625b93ac54SRajendra Nayak  *
1635b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
1645b93ac54SRajendra Nayak  * return 0.
1655b93ac54SRajendra Nayak  */
1665b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
1675b93ac54SRajendra Nayak {
1685b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1695b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
1705b93ac54SRajendra Nayak 		return 0;
1715b93ac54SRajendra Nayak 	}
1725b93ac54SRajendra Nayak 
1735b93ac54SRajendra Nayak 	return opp->level;
1745b93ac54SRajendra Nayak }
1755b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
1765b93ac54SRajendra Nayak 
1775b93ac54SRajendra Nayak /**
178597ff543SDmitry Osipenko  * dev_pm_opp_get_required_pstate() - Gets the required performance state
179597ff543SDmitry Osipenko  *                                    corresponding to an available opp
180597ff543SDmitry Osipenko  * @opp:	opp for which performance state has to be returned for
181597ff543SDmitry Osipenko  * @index:	index of the required opp
182597ff543SDmitry Osipenko  *
183597ff543SDmitry Osipenko  * Return: performance state read from device tree corresponding to the
184597ff543SDmitry Osipenko  * required opp, else return 0.
185597ff543SDmitry Osipenko  */
186597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
187597ff543SDmitry Osipenko 					    unsigned int index)
188597ff543SDmitry Osipenko {
189597ff543SDmitry Osipenko 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
190597ff543SDmitry Osipenko 	    index >= opp->opp_table->required_opp_count) {
191597ff543SDmitry Osipenko 		pr_err("%s: Invalid parameters\n", __func__);
192597ff543SDmitry Osipenko 		return 0;
193597ff543SDmitry Osipenko 	}
194597ff543SDmitry Osipenko 
1957eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
1967eba0c76SViresh Kumar 	if (lazy_linking_pending(opp->opp_table))
1977eba0c76SViresh Kumar 		return 0;
1987eba0c76SViresh Kumar 
199597ff543SDmitry Osipenko 	return opp->required_opps[index]->pstate;
200597ff543SDmitry Osipenko }
201597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
202597ff543SDmitry Osipenko 
203597ff543SDmitry Osipenko /**
2047813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
2057813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
2067813dd6fSViresh Kumar  *
2077813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
2087813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
2097813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
2107813dd6fSViresh Kumar  *
2117813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
2127813dd6fSViresh Kumar  */
2137813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
2147813dd6fSViresh Kumar {
2157813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2167813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
2177813dd6fSViresh Kumar 		return false;
2187813dd6fSViresh Kumar 	}
2197813dd6fSViresh Kumar 
2207813dd6fSViresh Kumar 	return opp->turbo;
2217813dd6fSViresh Kumar }
2227813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
2237813dd6fSViresh Kumar 
2247813dd6fSViresh Kumar /**
2257813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
2267813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2277813dd6fSViresh Kumar  *
2287813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
2297813dd6fSViresh Kumar  */
2307813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
2317813dd6fSViresh Kumar {
2327813dd6fSViresh Kumar 	struct opp_table *opp_table;
2337813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
2347813dd6fSViresh Kumar 
2357813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2367813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2377813dd6fSViresh Kumar 		return 0;
2387813dd6fSViresh Kumar 
2397813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
2407813dd6fSViresh Kumar 
2417813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2427813dd6fSViresh Kumar 
2437813dd6fSViresh Kumar 	return clock_latency_ns;
2447813dd6fSViresh Kumar }
2457813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
2467813dd6fSViresh Kumar 
2477813dd6fSViresh Kumar /**
2487813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
2497813dd6fSViresh Kumar  * @dev: device for which we do this operation
2507813dd6fSViresh Kumar  *
2517813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
2527813dd6fSViresh Kumar  */
2537813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
2547813dd6fSViresh Kumar {
2557813dd6fSViresh Kumar 	struct opp_table *opp_table;
2567813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
2577813dd6fSViresh Kumar 	struct regulator *reg;
2587813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
2597813dd6fSViresh Kumar 	int ret, i, count;
2607813dd6fSViresh Kumar 	struct {
2617813dd6fSViresh Kumar 		unsigned long min;
2627813dd6fSViresh Kumar 		unsigned long max;
2637813dd6fSViresh Kumar 	} *uV;
2647813dd6fSViresh Kumar 
2657813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2667813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2677813dd6fSViresh Kumar 		return 0;
2687813dd6fSViresh Kumar 
2697813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
27090e3577bSViresh Kumar 	if (!opp_table->regulators)
2717813dd6fSViresh Kumar 		goto put_opp_table;
2727813dd6fSViresh Kumar 
27390e3577bSViresh Kumar 	count = opp_table->regulator_count;
27490e3577bSViresh Kumar 
2757813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
2767813dd6fSViresh Kumar 	if (!uV)
2777813dd6fSViresh Kumar 		goto put_opp_table;
2787813dd6fSViresh Kumar 
2797813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
2807813dd6fSViresh Kumar 
2817813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2827813dd6fSViresh Kumar 		uV[i].min = ~0;
2837813dd6fSViresh Kumar 		uV[i].max = 0;
2847813dd6fSViresh Kumar 
2857813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
2867813dd6fSViresh Kumar 			if (!opp->available)
2877813dd6fSViresh Kumar 				continue;
2887813dd6fSViresh Kumar 
2897813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
2907813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
2917813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
2927813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
2937813dd6fSViresh Kumar 		}
2947813dd6fSViresh Kumar 	}
2957813dd6fSViresh Kumar 
2967813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
2977813dd6fSViresh Kumar 
2987813dd6fSViresh Kumar 	/*
2997813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
3007813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
3017813dd6fSViresh Kumar 	 */
3027813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3037813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
3047813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
3057813dd6fSViresh Kumar 		if (ret > 0)
3067813dd6fSViresh Kumar 			latency_ns += ret * 1000;
3077813dd6fSViresh Kumar 	}
3087813dd6fSViresh Kumar 
3097813dd6fSViresh Kumar 	kfree(uV);
3107813dd6fSViresh Kumar put_opp_table:
3117813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3127813dd6fSViresh Kumar 
3137813dd6fSViresh Kumar 	return latency_ns;
3147813dd6fSViresh Kumar }
3157813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
3167813dd6fSViresh Kumar 
3177813dd6fSViresh Kumar /**
3187813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
3197813dd6fSViresh Kumar  *					     nanoseconds
3207813dd6fSViresh Kumar  * @dev: device for which we do this operation
3217813dd6fSViresh Kumar  *
3227813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
3237813dd6fSViresh Kumar  * switch from one OPP to other.
3247813dd6fSViresh Kumar  */
3257813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
3267813dd6fSViresh Kumar {
3277813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
3287813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
3297813dd6fSViresh Kumar }
3307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
3317813dd6fSViresh Kumar 
3327813dd6fSViresh Kumar /**
3337813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
3347813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3357813dd6fSViresh Kumar  *
3367813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
3377813dd6fSViresh Kumar  * if one is available, else returns 0;
3387813dd6fSViresh Kumar  */
3397813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
3407813dd6fSViresh Kumar {
3417813dd6fSViresh Kumar 	struct opp_table *opp_table;
3427813dd6fSViresh Kumar 	unsigned long freq = 0;
3437813dd6fSViresh Kumar 
3447813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3457813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3467813dd6fSViresh Kumar 		return 0;
3477813dd6fSViresh Kumar 
3487813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
3497813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
3507813dd6fSViresh Kumar 
3517813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3527813dd6fSViresh Kumar 
3537813dd6fSViresh Kumar 	return freq;
3547813dd6fSViresh Kumar }
3557813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
3567813dd6fSViresh Kumar 
357a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
358a1e8c136SViresh Kumar {
359a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
360a1e8c136SViresh Kumar 	int count = 0;
361a1e8c136SViresh Kumar 
362a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
363a1e8c136SViresh Kumar 
364a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
365a1e8c136SViresh Kumar 		if (opp->available)
366a1e8c136SViresh Kumar 			count++;
367a1e8c136SViresh Kumar 	}
368a1e8c136SViresh Kumar 
369a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
370a1e8c136SViresh Kumar 
371a1e8c136SViresh Kumar 	return count;
372a1e8c136SViresh Kumar }
373a1e8c136SViresh Kumar 
3747813dd6fSViresh Kumar /**
3757813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
3767813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3777813dd6fSViresh Kumar  *
3787813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
3797813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
3807813dd6fSViresh Kumar  */
3817813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
3827813dd6fSViresh Kumar {
3837813dd6fSViresh Kumar 	struct opp_table *opp_table;
384a1e8c136SViresh Kumar 	int count;
3857813dd6fSViresh Kumar 
3867813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3877813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
3887813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
389035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
3907813dd6fSViresh Kumar 			__func__, count);
39109f662f9SViresh Kumar 		return count;
3927813dd6fSViresh Kumar 	}
3937813dd6fSViresh Kumar 
394a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
3957813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3967813dd6fSViresh Kumar 
3977813dd6fSViresh Kumar 	return count;
3987813dd6fSViresh Kumar }
3997813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
4007813dd6fSViresh Kumar 
4017813dd6fSViresh Kumar /**
4027813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
4037813dd6fSViresh Kumar  * @dev:		device for which we do this operation
4047813dd6fSViresh Kumar  * @freq:		frequency to search for
4057813dd6fSViresh Kumar  * @available:		true/false - match for available opp
4067813dd6fSViresh Kumar  *
4077813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
4087813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
4097813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
4107813dd6fSViresh Kumar  * EINVAL:	for bad pointer
4117813dd6fSViresh Kumar  * ERANGE:	no match found for search
4127813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
4137813dd6fSViresh Kumar  *
4147813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
4157813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
4167813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
4177813dd6fSViresh Kumar  *
4187813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
4197813dd6fSViresh Kumar  * or the opposite as well.
4207813dd6fSViresh Kumar  *
4217813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
4227813dd6fSViresh Kumar  * use.
4237813dd6fSViresh Kumar  */
4247813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
4257813dd6fSViresh Kumar 					      unsigned long freq,
4267813dd6fSViresh Kumar 					      bool available)
4277813dd6fSViresh Kumar {
4287813dd6fSViresh Kumar 	struct opp_table *opp_table;
4297813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
4307813dd6fSViresh Kumar 
4317813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4327813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
4337813dd6fSViresh Kumar 		int r = PTR_ERR(opp_table);
4347813dd6fSViresh Kumar 
4357813dd6fSViresh Kumar 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
4367813dd6fSViresh Kumar 		return ERR_PTR(r);
4377813dd6fSViresh Kumar 	}
4387813dd6fSViresh Kumar 
4397813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
4407813dd6fSViresh Kumar 
4417813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
4427813dd6fSViresh Kumar 		if (temp_opp->available == available &&
4437813dd6fSViresh Kumar 				temp_opp->rate == freq) {
4447813dd6fSViresh Kumar 			opp = temp_opp;
4457813dd6fSViresh Kumar 
4467813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
4477813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
4487813dd6fSViresh Kumar 			break;
4497813dd6fSViresh Kumar 		}
4507813dd6fSViresh Kumar 	}
4517813dd6fSViresh Kumar 
4527813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
4537813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4547813dd6fSViresh Kumar 
4557813dd6fSViresh Kumar 	return opp;
4567813dd6fSViresh Kumar }
4577813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
4587813dd6fSViresh Kumar 
45971419d84SNiklas Cassel /**
46071419d84SNiklas Cassel  * dev_pm_opp_find_level_exact() - search for an exact level
46171419d84SNiklas Cassel  * @dev:		device for which we do this operation
46271419d84SNiklas Cassel  * @level:		level to search for
46371419d84SNiklas Cassel  *
46471419d84SNiklas Cassel  * Return: Searches for exact match in the opp table and returns pointer to the
46571419d84SNiklas Cassel  * matching opp if found, else returns ERR_PTR in case of error and should
46671419d84SNiklas Cassel  * be handled using IS_ERR. Error return values can be:
46771419d84SNiklas Cassel  * EINVAL:	for bad pointer
46871419d84SNiklas Cassel  * ERANGE:	no match found for search
46971419d84SNiklas Cassel  * ENODEV:	if device not found in list of registered devices
47071419d84SNiklas Cassel  *
47171419d84SNiklas Cassel  * The callers are required to call dev_pm_opp_put() for the returned OPP after
47271419d84SNiklas Cassel  * use.
47371419d84SNiklas Cassel  */
47471419d84SNiklas Cassel struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
47571419d84SNiklas Cassel 					       unsigned int level)
47671419d84SNiklas Cassel {
47771419d84SNiklas Cassel 	struct opp_table *opp_table;
47871419d84SNiklas Cassel 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
47971419d84SNiklas Cassel 
48071419d84SNiklas Cassel 	opp_table = _find_opp_table(dev);
48171419d84SNiklas Cassel 	if (IS_ERR(opp_table)) {
48271419d84SNiklas Cassel 		int r = PTR_ERR(opp_table);
48371419d84SNiklas Cassel 
48471419d84SNiklas Cassel 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
48571419d84SNiklas Cassel 		return ERR_PTR(r);
48671419d84SNiklas Cassel 	}
48771419d84SNiklas Cassel 
48871419d84SNiklas Cassel 	mutex_lock(&opp_table->lock);
48971419d84SNiklas Cassel 
49071419d84SNiklas Cassel 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
49171419d84SNiklas Cassel 		if (temp_opp->level == level) {
49271419d84SNiklas Cassel 			opp = temp_opp;
49371419d84SNiklas Cassel 
49471419d84SNiklas Cassel 			/* Increment the reference count of OPP */
49571419d84SNiklas Cassel 			dev_pm_opp_get(opp);
49671419d84SNiklas Cassel 			break;
49771419d84SNiklas Cassel 		}
49871419d84SNiklas Cassel 	}
49971419d84SNiklas Cassel 
50071419d84SNiklas Cassel 	mutex_unlock(&opp_table->lock);
50171419d84SNiklas Cassel 	dev_pm_opp_put_opp_table(opp_table);
50271419d84SNiklas Cassel 
50371419d84SNiklas Cassel 	return opp;
50471419d84SNiklas Cassel }
50571419d84SNiklas Cassel EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
50671419d84SNiklas Cassel 
5078dd5cadaSDmitry Osipenko /**
5088dd5cadaSDmitry Osipenko  * dev_pm_opp_find_level_ceil() - search for an rounded up level
5098dd5cadaSDmitry Osipenko  * @dev:		device for which we do this operation
5108dd5cadaSDmitry Osipenko  * @level:		level to search for
5118dd5cadaSDmitry Osipenko  *
5128dd5cadaSDmitry Osipenko  * Return: Searches for rounded up match in the opp table and returns pointer
5138dd5cadaSDmitry Osipenko  * to the  matching opp if found, else returns ERR_PTR in case of error and
5148dd5cadaSDmitry Osipenko  * should be handled using IS_ERR. Error return values can be:
5158dd5cadaSDmitry Osipenko  * EINVAL:	for bad pointer
5168dd5cadaSDmitry Osipenko  * ERANGE:	no match found for search
5178dd5cadaSDmitry Osipenko  * ENODEV:	if device not found in list of registered devices
5188dd5cadaSDmitry Osipenko  *
5198dd5cadaSDmitry Osipenko  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5208dd5cadaSDmitry Osipenko  * use.
5218dd5cadaSDmitry Osipenko  */
5228dd5cadaSDmitry Osipenko struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
5238dd5cadaSDmitry Osipenko 					      unsigned int *level)
5248dd5cadaSDmitry Osipenko {
5258dd5cadaSDmitry Osipenko 	struct opp_table *opp_table;
5268dd5cadaSDmitry Osipenko 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5278dd5cadaSDmitry Osipenko 
5288dd5cadaSDmitry Osipenko 	opp_table = _find_opp_table(dev);
5298dd5cadaSDmitry Osipenko 	if (IS_ERR(opp_table)) {
5308dd5cadaSDmitry Osipenko 		int r = PTR_ERR(opp_table);
5318dd5cadaSDmitry Osipenko 
5328dd5cadaSDmitry Osipenko 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
5338dd5cadaSDmitry Osipenko 		return ERR_PTR(r);
5348dd5cadaSDmitry Osipenko 	}
5358dd5cadaSDmitry Osipenko 
5368dd5cadaSDmitry Osipenko 	mutex_lock(&opp_table->lock);
5378dd5cadaSDmitry Osipenko 
5388dd5cadaSDmitry Osipenko 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5398dd5cadaSDmitry Osipenko 		if (temp_opp->available && temp_opp->level >= *level) {
5408dd5cadaSDmitry Osipenko 			opp = temp_opp;
5418dd5cadaSDmitry Osipenko 			*level = opp->level;
5428dd5cadaSDmitry Osipenko 
5438dd5cadaSDmitry Osipenko 			/* Increment the reference count of OPP */
5448dd5cadaSDmitry Osipenko 			dev_pm_opp_get(opp);
5458dd5cadaSDmitry Osipenko 			break;
5468dd5cadaSDmitry Osipenko 		}
5478dd5cadaSDmitry Osipenko 	}
5488dd5cadaSDmitry Osipenko 
5498dd5cadaSDmitry Osipenko 	mutex_unlock(&opp_table->lock);
5508dd5cadaSDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
5518dd5cadaSDmitry Osipenko 
5528dd5cadaSDmitry Osipenko 	return opp;
5538dd5cadaSDmitry Osipenko }
5548dd5cadaSDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
5558dd5cadaSDmitry Osipenko 
5567813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
5577813dd6fSViresh Kumar 						   unsigned long *freq)
5587813dd6fSViresh Kumar {
5597813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5607813dd6fSViresh Kumar 
5617813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
5627813dd6fSViresh Kumar 
5637813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5647813dd6fSViresh Kumar 		if (temp_opp->available && temp_opp->rate >= *freq) {
5657813dd6fSViresh Kumar 			opp = temp_opp;
5667813dd6fSViresh Kumar 			*freq = opp->rate;
5677813dd6fSViresh Kumar 
5687813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
5697813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
5707813dd6fSViresh Kumar 			break;
5717813dd6fSViresh Kumar 		}
5727813dd6fSViresh Kumar 	}
5737813dd6fSViresh Kumar 
5747813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
5757813dd6fSViresh Kumar 
5767813dd6fSViresh Kumar 	return opp;
5777813dd6fSViresh Kumar }
5787813dd6fSViresh Kumar 
5797813dd6fSViresh Kumar /**
5807813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
5817813dd6fSViresh Kumar  * @dev:	device for which we do this operation
5827813dd6fSViresh Kumar  * @freq:	Start frequency
5837813dd6fSViresh Kumar  *
5847813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
5857813dd6fSViresh Kumar  * for a device.
5867813dd6fSViresh Kumar  *
5877813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
5887813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
5897813dd6fSViresh Kumar  * values can be:
5907813dd6fSViresh Kumar  * EINVAL:	for bad pointer
5917813dd6fSViresh Kumar  * ERANGE:	no match found for search
5927813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
5937813dd6fSViresh Kumar  *
5947813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5957813dd6fSViresh Kumar  * use.
5967813dd6fSViresh Kumar  */
5977813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
5987813dd6fSViresh Kumar 					     unsigned long *freq)
5997813dd6fSViresh Kumar {
6007813dd6fSViresh Kumar 	struct opp_table *opp_table;
6017813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
6027813dd6fSViresh Kumar 
6037813dd6fSViresh Kumar 	if (!dev || !freq) {
6047813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
6057813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
6067813dd6fSViresh Kumar 	}
6077813dd6fSViresh Kumar 
6087813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
6097813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
6107813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
6117813dd6fSViresh Kumar 
6127813dd6fSViresh Kumar 	opp = _find_freq_ceil(opp_table, freq);
6137813dd6fSViresh Kumar 
6147813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
6157813dd6fSViresh Kumar 
6167813dd6fSViresh Kumar 	return opp;
6177813dd6fSViresh Kumar }
6187813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
6197813dd6fSViresh Kumar 
6207813dd6fSViresh Kumar /**
6217813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
6227813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6237813dd6fSViresh Kumar  * @freq:	Start frequency
6247813dd6fSViresh Kumar  *
6257813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
6267813dd6fSViresh Kumar  * for a device.
6277813dd6fSViresh Kumar  *
6287813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6297813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6307813dd6fSViresh Kumar  * values can be:
6317813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6327813dd6fSViresh Kumar  * ERANGE:	no match found for search
6337813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6347813dd6fSViresh Kumar  *
6357813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6367813dd6fSViresh Kumar  * use.
6377813dd6fSViresh Kumar  */
6387813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
6397813dd6fSViresh Kumar 					      unsigned long *freq)
6407813dd6fSViresh Kumar {
6417813dd6fSViresh Kumar 	struct opp_table *opp_table;
6427813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
6437813dd6fSViresh Kumar 
6447813dd6fSViresh Kumar 	if (!dev || !freq) {
6457813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
6467813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
6477813dd6fSViresh Kumar 	}
6487813dd6fSViresh Kumar 
6497813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
6507813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
6517813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
6527813dd6fSViresh Kumar 
6537813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
6547813dd6fSViresh Kumar 
6557813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
6567813dd6fSViresh Kumar 		if (temp_opp->available) {
6577813dd6fSViresh Kumar 			/* go to the next node, before choosing prev */
6587813dd6fSViresh Kumar 			if (temp_opp->rate > *freq)
6597813dd6fSViresh Kumar 				break;
6607813dd6fSViresh Kumar 			else
6617813dd6fSViresh Kumar 				opp = temp_opp;
6627813dd6fSViresh Kumar 		}
6637813dd6fSViresh Kumar 	}
6647813dd6fSViresh Kumar 
6657813dd6fSViresh Kumar 	/* Increment the reference count of OPP */
6667813dd6fSViresh Kumar 	if (!IS_ERR(opp))
6677813dd6fSViresh Kumar 		dev_pm_opp_get(opp);
6687813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
6697813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
6707813dd6fSViresh Kumar 
6717813dd6fSViresh Kumar 	if (!IS_ERR(opp))
6727813dd6fSViresh Kumar 		*freq = opp->rate;
6737813dd6fSViresh Kumar 
6747813dd6fSViresh Kumar 	return opp;
6757813dd6fSViresh Kumar }
6767813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
6777813dd6fSViresh Kumar 
6782f36bde0SAndrew-sh.Cheng /**
6792f36bde0SAndrew-sh.Cheng  * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
6802f36bde0SAndrew-sh.Cheng  *					 target voltage.
6812f36bde0SAndrew-sh.Cheng  * @dev:	Device for which we do this operation.
6822f36bde0SAndrew-sh.Cheng  * @u_volt:	Target voltage.
6832f36bde0SAndrew-sh.Cheng  *
6842f36bde0SAndrew-sh.Cheng  * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
6852f36bde0SAndrew-sh.Cheng  *
6862f36bde0SAndrew-sh.Cheng  * Return: matching *opp, else returns ERR_PTR in case of error which should be
6872f36bde0SAndrew-sh.Cheng  * handled using IS_ERR.
6882f36bde0SAndrew-sh.Cheng  *
6892f36bde0SAndrew-sh.Cheng  * Error return values can be:
6902f36bde0SAndrew-sh.Cheng  * EINVAL:	bad parameters
6912f36bde0SAndrew-sh.Cheng  *
6922f36bde0SAndrew-sh.Cheng  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6932f36bde0SAndrew-sh.Cheng  * use.
6942f36bde0SAndrew-sh.Cheng  */
6952f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
6962f36bde0SAndrew-sh.Cheng 						     unsigned long u_volt)
6972f36bde0SAndrew-sh.Cheng {
6982f36bde0SAndrew-sh.Cheng 	struct opp_table *opp_table;
6992f36bde0SAndrew-sh.Cheng 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
7002f36bde0SAndrew-sh.Cheng 
7012f36bde0SAndrew-sh.Cheng 	if (!dev || !u_volt) {
7022f36bde0SAndrew-sh.Cheng 		dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
7032f36bde0SAndrew-sh.Cheng 			u_volt);
7042f36bde0SAndrew-sh.Cheng 		return ERR_PTR(-EINVAL);
7052f36bde0SAndrew-sh.Cheng 	}
7062f36bde0SAndrew-sh.Cheng 
7072f36bde0SAndrew-sh.Cheng 	opp_table = _find_opp_table(dev);
7082f36bde0SAndrew-sh.Cheng 	if (IS_ERR(opp_table))
7092f36bde0SAndrew-sh.Cheng 		return ERR_CAST(opp_table);
7102f36bde0SAndrew-sh.Cheng 
7112f36bde0SAndrew-sh.Cheng 	mutex_lock(&opp_table->lock);
7122f36bde0SAndrew-sh.Cheng 
7132f36bde0SAndrew-sh.Cheng 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
7142f36bde0SAndrew-sh.Cheng 		if (temp_opp->available) {
7152f36bde0SAndrew-sh.Cheng 			if (temp_opp->supplies[0].u_volt > u_volt)
7162f36bde0SAndrew-sh.Cheng 				break;
7172f36bde0SAndrew-sh.Cheng 			opp = temp_opp;
7182f36bde0SAndrew-sh.Cheng 		}
7192f36bde0SAndrew-sh.Cheng 	}
7202f36bde0SAndrew-sh.Cheng 
7212f36bde0SAndrew-sh.Cheng 	/* Increment the reference count of OPP */
7222f36bde0SAndrew-sh.Cheng 	if (!IS_ERR(opp))
7232f36bde0SAndrew-sh.Cheng 		dev_pm_opp_get(opp);
7242f36bde0SAndrew-sh.Cheng 
7252f36bde0SAndrew-sh.Cheng 	mutex_unlock(&opp_table->lock);
7262f36bde0SAndrew-sh.Cheng 	dev_pm_opp_put_opp_table(opp_table);
7272f36bde0SAndrew-sh.Cheng 
7282f36bde0SAndrew-sh.Cheng 	return opp;
7292f36bde0SAndrew-sh.Cheng }
7302f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
7312f36bde0SAndrew-sh.Cheng 
732*00ce3873SKrzysztof Kozlowski /**
733*00ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
734*00ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
735*00ce3873SKrzysztof Kozlowski  * @freq:	start bandwidth
736*00ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
737*00ce3873SKrzysztof Kozlowski  *
738*00ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
739*00ce3873SKrzysztof Kozlowski  * for a device.
740*00ce3873SKrzysztof Kozlowski  *
741*00ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
742*00ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
743*00ce3873SKrzysztof Kozlowski  * values can be:
744*00ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
745*00ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
746*00ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
747*00ce3873SKrzysztof Kozlowski  *
748*00ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
749*00ce3873SKrzysztof Kozlowski  * use.
750*00ce3873SKrzysztof Kozlowski  */
751*00ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev,
752*00ce3873SKrzysztof Kozlowski 					   unsigned int *bw, int index)
753*00ce3873SKrzysztof Kozlowski {
754*00ce3873SKrzysztof Kozlowski 	struct opp_table *opp_table;
755*00ce3873SKrzysztof Kozlowski 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
756*00ce3873SKrzysztof Kozlowski 
757*00ce3873SKrzysztof Kozlowski 	if (!dev || !bw) {
758*00ce3873SKrzysztof Kozlowski 		dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw);
759*00ce3873SKrzysztof Kozlowski 		return ERR_PTR(-EINVAL);
760*00ce3873SKrzysztof Kozlowski 	}
761*00ce3873SKrzysztof Kozlowski 
762*00ce3873SKrzysztof Kozlowski 	opp_table = _find_opp_table(dev);
763*00ce3873SKrzysztof Kozlowski 	if (IS_ERR(opp_table))
764*00ce3873SKrzysztof Kozlowski 		return ERR_CAST(opp_table);
765*00ce3873SKrzysztof Kozlowski 
766*00ce3873SKrzysztof Kozlowski 	if (index >= opp_table->path_count)
767*00ce3873SKrzysztof Kozlowski 		return ERR_PTR(-EINVAL);
768*00ce3873SKrzysztof Kozlowski 
769*00ce3873SKrzysztof Kozlowski 	mutex_lock(&opp_table->lock);
770*00ce3873SKrzysztof Kozlowski 
771*00ce3873SKrzysztof Kozlowski 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
772*00ce3873SKrzysztof Kozlowski 		if (temp_opp->available && temp_opp->bandwidth) {
773*00ce3873SKrzysztof Kozlowski 			if (temp_opp->bandwidth[index].peak >= *bw) {
774*00ce3873SKrzysztof Kozlowski 				opp = temp_opp;
775*00ce3873SKrzysztof Kozlowski 				*bw = opp->bandwidth[index].peak;
776*00ce3873SKrzysztof Kozlowski 
777*00ce3873SKrzysztof Kozlowski 				/* Increment the reference count of OPP */
778*00ce3873SKrzysztof Kozlowski 				dev_pm_opp_get(opp);
779*00ce3873SKrzysztof Kozlowski 				break;
780*00ce3873SKrzysztof Kozlowski 			}
781*00ce3873SKrzysztof Kozlowski 		}
782*00ce3873SKrzysztof Kozlowski 	}
783*00ce3873SKrzysztof Kozlowski 
784*00ce3873SKrzysztof Kozlowski 	mutex_unlock(&opp_table->lock);
785*00ce3873SKrzysztof Kozlowski 	dev_pm_opp_put_opp_table(opp_table);
786*00ce3873SKrzysztof Kozlowski 
787*00ce3873SKrzysztof Kozlowski 	return opp;
788*00ce3873SKrzysztof Kozlowski }
789*00ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
790*00ce3873SKrzysztof Kozlowski 
791*00ce3873SKrzysztof Kozlowski /**
792*00ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
793*00ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
794*00ce3873SKrzysztof Kozlowski  * @freq:	start bandwidth
795*00ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
796*00ce3873SKrzysztof Kozlowski  *
797*00ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
798*00ce3873SKrzysztof Kozlowski  * for a device.
799*00ce3873SKrzysztof Kozlowski  *
800*00ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
801*00ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
802*00ce3873SKrzysztof Kozlowski  * values can be:
803*00ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
804*00ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
805*00ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
806*00ce3873SKrzysztof Kozlowski  *
807*00ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
808*00ce3873SKrzysztof Kozlowski  * use.
809*00ce3873SKrzysztof Kozlowski  */
810*00ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
811*00ce3873SKrzysztof Kozlowski 					    unsigned int *bw, int index)
812*00ce3873SKrzysztof Kozlowski {
813*00ce3873SKrzysztof Kozlowski 	struct opp_table *opp_table;
814*00ce3873SKrzysztof Kozlowski 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
815*00ce3873SKrzysztof Kozlowski 
816*00ce3873SKrzysztof Kozlowski 	if (!dev || !bw) {
817*00ce3873SKrzysztof Kozlowski 		dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw);
818*00ce3873SKrzysztof Kozlowski 		return ERR_PTR(-EINVAL);
819*00ce3873SKrzysztof Kozlowski 	}
820*00ce3873SKrzysztof Kozlowski 
821*00ce3873SKrzysztof Kozlowski 	opp_table = _find_opp_table(dev);
822*00ce3873SKrzysztof Kozlowski 	if (IS_ERR(opp_table))
823*00ce3873SKrzysztof Kozlowski 		return ERR_CAST(opp_table);
824*00ce3873SKrzysztof Kozlowski 
825*00ce3873SKrzysztof Kozlowski 	if (index >= opp_table->path_count)
826*00ce3873SKrzysztof Kozlowski 		return ERR_PTR(-EINVAL);
827*00ce3873SKrzysztof Kozlowski 
828*00ce3873SKrzysztof Kozlowski 	mutex_lock(&opp_table->lock);
829*00ce3873SKrzysztof Kozlowski 
830*00ce3873SKrzysztof Kozlowski 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
831*00ce3873SKrzysztof Kozlowski 		if (temp_opp->available && temp_opp->bandwidth) {
832*00ce3873SKrzysztof Kozlowski 			/* go to the next node, before choosing prev */
833*00ce3873SKrzysztof Kozlowski 			if (temp_opp->bandwidth[index].peak > *bw)
834*00ce3873SKrzysztof Kozlowski 				break;
835*00ce3873SKrzysztof Kozlowski 			opp = temp_opp;
836*00ce3873SKrzysztof Kozlowski 		}
837*00ce3873SKrzysztof Kozlowski 	}
838*00ce3873SKrzysztof Kozlowski 
839*00ce3873SKrzysztof Kozlowski 	/* Increment the reference count of OPP */
840*00ce3873SKrzysztof Kozlowski 	if (!IS_ERR(opp))
841*00ce3873SKrzysztof Kozlowski 		dev_pm_opp_get(opp);
842*00ce3873SKrzysztof Kozlowski 	mutex_unlock(&opp_table->lock);
843*00ce3873SKrzysztof Kozlowski 	dev_pm_opp_put_opp_table(opp_table);
844*00ce3873SKrzysztof Kozlowski 
845*00ce3873SKrzysztof Kozlowski 	if (!IS_ERR(opp))
846*00ce3873SKrzysztof Kozlowski 		*bw = opp->bandwidth[index].peak;
847*00ce3873SKrzysztof Kozlowski 
848*00ce3873SKrzysztof Kozlowski 	return opp;
849*00ce3873SKrzysztof Kozlowski }
850*00ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
851*00ce3873SKrzysztof Kozlowski 
8527813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
8537813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
8547813dd6fSViresh Kumar {
8557813dd6fSViresh Kumar 	int ret;
8567813dd6fSViresh Kumar 
8577813dd6fSViresh Kumar 	/* Regulator not available for device */
8587813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
8597813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
8607813dd6fSViresh Kumar 			PTR_ERR(reg));
8617813dd6fSViresh Kumar 		return 0;
8627813dd6fSViresh Kumar 	}
8637813dd6fSViresh Kumar 
8647813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
8657813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
8667813dd6fSViresh Kumar 
8677813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
8687813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
8697813dd6fSViresh Kumar 	if (ret)
8707813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
8717813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
8727813dd6fSViresh Kumar 			supply->u_volt_max, ret);
8737813dd6fSViresh Kumar 
8747813dd6fSViresh Kumar 	return ret;
8757813dd6fSViresh Kumar }
8767813dd6fSViresh Kumar 
877285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
878285881b5SViresh Kumar 					    unsigned long freq)
8797813dd6fSViresh Kumar {
8807813dd6fSViresh Kumar 	int ret;
8817813dd6fSViresh Kumar 
88235e74b2eSViresh Kumar 	/* We may reach here for devices which don't change frequency */
88335e74b2eSViresh Kumar 	if (IS_ERR(clk))
88435e74b2eSViresh Kumar 		return 0;
88535e74b2eSViresh Kumar 
8867813dd6fSViresh Kumar 	ret = clk_set_rate(clk, freq);
8877813dd6fSViresh Kumar 	if (ret) {
8887813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
8897813dd6fSViresh Kumar 			ret);
8907813dd6fSViresh Kumar 	}
8917813dd6fSViresh Kumar 
8927813dd6fSViresh Kumar 	return ret;
8937813dd6fSViresh Kumar }
8947813dd6fSViresh Kumar 
8958d45719cSKamil Konieczny static int _generic_set_opp_regulator(struct opp_table *opp_table,
8967813dd6fSViresh Kumar 				      struct device *dev,
8973f62670fSViresh Kumar 				      struct dev_pm_opp *opp,
8987813dd6fSViresh Kumar 				      unsigned long freq,
8993f62670fSViresh Kumar 				      int scaling_down)
9007813dd6fSViresh Kumar {
9017813dd6fSViresh Kumar 	struct regulator *reg = opp_table->regulators[0];
9023f62670fSViresh Kumar 	struct dev_pm_opp *old_opp = opp_table->current_opp;
9037813dd6fSViresh Kumar 	int ret;
9047813dd6fSViresh Kumar 
9057813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
9067813dd6fSViresh Kumar 	if (WARN_ON(opp_table->regulator_count > 1)) {
9077813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
9087813dd6fSViresh Kumar 		return -EINVAL;
9097813dd6fSViresh Kumar 	}
9107813dd6fSViresh Kumar 
9117813dd6fSViresh Kumar 	/* Scaling up? Scale voltage before frequency */
9123f62670fSViresh Kumar 	if (!scaling_down) {
9133f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
9147813dd6fSViresh Kumar 		if (ret)
9157813dd6fSViresh Kumar 			goto restore_voltage;
9167813dd6fSViresh Kumar 	}
9177813dd6fSViresh Kumar 
9187813dd6fSViresh Kumar 	/* Change frequency */
919285881b5SViresh Kumar 	ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
9207813dd6fSViresh Kumar 	if (ret)
9217813dd6fSViresh Kumar 		goto restore_voltage;
9227813dd6fSViresh Kumar 
9237813dd6fSViresh Kumar 	/* Scaling down? Scale voltage after frequency */
9243f62670fSViresh Kumar 	if (scaling_down) {
9253f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
9267813dd6fSViresh Kumar 		if (ret)
9277813dd6fSViresh Kumar 			goto restore_freq;
9287813dd6fSViresh Kumar 	}
9297813dd6fSViresh Kumar 
9308d45719cSKamil Konieczny 	/*
9318d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
9328d45719cSKamil Konieczny 	 * some boot-enabled regulators.
9338d45719cSKamil Konieczny 	 */
93472f80ce4SViresh Kumar 	if (unlikely(!opp_table->enabled)) {
9358d45719cSKamil Konieczny 		ret = regulator_enable(reg);
9368d45719cSKamil Konieczny 		if (ret < 0)
9378d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
9388d45719cSKamil Konieczny 	}
9398d45719cSKamil Konieczny 
9407813dd6fSViresh Kumar 	return 0;
9417813dd6fSViresh Kumar 
9427813dd6fSViresh Kumar restore_freq:
9433f62670fSViresh Kumar 	if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate))
9447813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
9453f62670fSViresh Kumar 			__func__, old_opp->rate);
9467813dd6fSViresh Kumar restore_voltage:
9477813dd6fSViresh Kumar 	/* This shouldn't harm even if the voltages weren't updated earlier */
9483f62670fSViresh Kumar 	_set_opp_voltage(dev, reg, old_opp->supplies);
9497813dd6fSViresh Kumar 
9507813dd6fSViresh Kumar 	return ret;
9517813dd6fSViresh Kumar }
9527813dd6fSViresh Kumar 
953b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
954240ae50eSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev)
955b00e667aSViresh Kumar {
956b00e667aSViresh Kumar 	u32 avg, peak;
957b00e667aSViresh Kumar 	int i, ret;
958b00e667aSViresh Kumar 
959b00e667aSViresh Kumar 	if (!opp_table->paths)
960b00e667aSViresh Kumar 		return 0;
961b00e667aSViresh Kumar 
962b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
963240ae50eSViresh Kumar 		if (!opp) {
964b00e667aSViresh Kumar 			avg = 0;
965b00e667aSViresh Kumar 			peak = 0;
966b00e667aSViresh Kumar 		} else {
967b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
968b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
969b00e667aSViresh Kumar 		}
970b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
971b00e667aSViresh Kumar 		if (ret) {
972b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
973240ae50eSViresh Kumar 				opp ? "set" : "remove", i, ret);
974b00e667aSViresh Kumar 			return ret;
975b00e667aSViresh Kumar 		}
976b00e667aSViresh Kumar 	}
977b00e667aSViresh Kumar 
978b00e667aSViresh Kumar 	return 0;
979b00e667aSViresh Kumar }
980b00e667aSViresh Kumar 
9817e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table,
982509e4777SViresh Kumar 			   struct device *dev, struct dev_pm_opp *opp,
983509e4777SViresh Kumar 			   unsigned long freq)
9847e535993SViresh Kumar {
98504b447dfSDmitry Osipenko 	struct dev_pm_set_opp_data *data = opp_table->set_opp_data;
986509e4777SViresh Kumar 	struct dev_pm_opp *old_opp = opp_table->current_opp;
9877e535993SViresh Kumar 	int size;
9887e535993SViresh Kumar 
98904b447dfSDmitry Osipenko 	/*
99004b447dfSDmitry Osipenko 	 * We support this only if dev_pm_opp_set_regulators() was called
99104b447dfSDmitry Osipenko 	 * earlier.
99204b447dfSDmitry Osipenko 	 */
99304b447dfSDmitry Osipenko 	if (opp_table->sod_supplies) {
994509e4777SViresh Kumar 		size = sizeof(*old_opp->supplies) * opp_table->regulator_count;
995509e4777SViresh Kumar 		memcpy(data->old_opp.supplies, old_opp->supplies, size);
996509e4777SViresh Kumar 		memcpy(data->new_opp.supplies, opp->supplies, size);
99704b447dfSDmitry Osipenko 		data->regulator_count = opp_table->regulator_count;
99804b447dfSDmitry Osipenko 	} else {
99904b447dfSDmitry Osipenko 		data->regulator_count = 0;
100004b447dfSDmitry Osipenko 	}
100104b447dfSDmitry Osipenko 
100204b447dfSDmitry Osipenko 	data->regulators = opp_table->regulators;
100304b447dfSDmitry Osipenko 	data->clk = opp_table->clk;
100404b447dfSDmitry Osipenko 	data->dev = dev;
1005509e4777SViresh Kumar 	data->old_opp.rate = old_opp->rate;
100604b447dfSDmitry Osipenko 	data->new_opp.rate = freq;
10077e535993SViresh Kumar 
10087e535993SViresh Kumar 	return opp_table->set_opp(data);
10097e535993SViresh Kumar }
10107e535993SViresh Kumar 
101160cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev,
101260cdeae0SStephan Gerhold 			     struct dev_pm_opp *opp, int i)
101360cdeae0SStephan Gerhold {
101460cdeae0SStephan Gerhold 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
101560cdeae0SStephan Gerhold 	int ret;
101660cdeae0SStephan Gerhold 
101760cdeae0SStephan Gerhold 	if (!pd_dev)
101860cdeae0SStephan Gerhold 		return 0;
101960cdeae0SStephan Gerhold 
102060cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
102160cdeae0SStephan Gerhold 	if (ret) {
102260cdeae0SStephan Gerhold 		dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
102360cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
102460cdeae0SStephan Gerhold 	}
102560cdeae0SStephan Gerhold 
102660cdeae0SStephan Gerhold 	return ret;
102760cdeae0SStephan Gerhold }
102860cdeae0SStephan Gerhold 
1029ca1b5d77SViresh Kumar /* This is only called for PM domain for now */
1030ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev,
1031ca1b5d77SViresh Kumar 			      struct opp_table *opp_table,
10322c59138cSStephan Gerhold 			      struct dev_pm_opp *opp, bool up)
1033ca1b5d77SViresh Kumar {
1034ca1b5d77SViresh Kumar 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
1035ca1b5d77SViresh Kumar 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
1036ca1b5d77SViresh Kumar 	int i, ret = 0;
1037ca1b5d77SViresh Kumar 
1038ca1b5d77SViresh Kumar 	if (!required_opp_tables)
1039ca1b5d77SViresh Kumar 		return 0;
1040ca1b5d77SViresh Kumar 
104119526d09SMarijn Suijten 	/* required-opps not fully initialized yet */
104219526d09SMarijn Suijten 	if (lazy_linking_pending(opp_table))
104319526d09SMarijn Suijten 		return -EBUSY;
104419526d09SMarijn Suijten 
10454fa82a87SHsin-Yi Wang 	/*
10464fa82a87SHsin-Yi Wang 	 * We only support genpd's OPPs in the "required-opps" for now, as we
10474fa82a87SHsin-Yi Wang 	 * don't know much about other use cases. Error out if the required OPP
10484fa82a87SHsin-Yi Wang 	 * doesn't belong to a genpd.
10494fa82a87SHsin-Yi Wang 	 */
10504fa82a87SHsin-Yi Wang 	if (unlikely(!required_opp_tables[0]->is_genpd)) {
10514fa82a87SHsin-Yi Wang 		dev_err(dev, "required-opps don't belong to a genpd\n");
10524fa82a87SHsin-Yi Wang 		return -ENOENT;
10534fa82a87SHsin-Yi Wang 	}
10544fa82a87SHsin-Yi Wang 
1055ca1b5d77SViresh Kumar 	/* Single genpd case */
105660cdeae0SStephan Gerhold 	if (!genpd_virt_devs)
105760cdeae0SStephan Gerhold 		return _set_required_opp(dev, dev, opp, 0);
1058ca1b5d77SViresh Kumar 
1059ca1b5d77SViresh Kumar 	/* Multiple genpd case */
1060ca1b5d77SViresh Kumar 
1061ca1b5d77SViresh Kumar 	/*
1062ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
1063ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
1064ca1b5d77SViresh Kumar 	 */
1065ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
1066ca1b5d77SViresh Kumar 
10672c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
10682c59138cSStephan Gerhold 	if (up) {
1069ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
107060cdeae0SStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
107160cdeae0SStephan Gerhold 			if (ret)
1072ca1b5d77SViresh Kumar 				break;
1073ca1b5d77SViresh Kumar 		}
10742c59138cSStephan Gerhold 	} else {
10752c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
10762c59138cSStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
10772c59138cSStephan Gerhold 			if (ret)
1078ca1b5d77SViresh Kumar 				break;
1079ca1b5d77SViresh Kumar 		}
1080ca1b5d77SViresh Kumar 	}
10812c59138cSStephan Gerhold 
1082ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
1083ca1b5d77SViresh Kumar 
1084ca1b5d77SViresh Kumar 	return ret;
1085ca1b5d77SViresh Kumar }
1086ca1b5d77SViresh Kumar 
108781c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
108881c4d8a3SViresh Kumar {
108981c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
109081c4d8a3SViresh Kumar 	unsigned long freq;
109181c4d8a3SViresh Kumar 
109281c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
109381c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
109481c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
109581c4d8a3SViresh Kumar 	}
109681c4d8a3SViresh Kumar 
109781c4d8a3SViresh Kumar 	/*
109881c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
109981c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
110081c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
110181c4d8a3SViresh Kumar 	 */
110281c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
110381c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
110481c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
110581c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
110681c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
110781c4d8a3SViresh Kumar 	}
110881c4d8a3SViresh Kumar 
110981c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
111081c4d8a3SViresh Kumar }
111181c4d8a3SViresh Kumar 
11125ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
1113f3364e17SViresh Kumar {
1114f3364e17SViresh Kumar 	int ret;
1115f3364e17SViresh Kumar 
1116f3364e17SViresh Kumar 	if (!opp_table->enabled)
1117f3364e17SViresh Kumar 		return 0;
1118f3364e17SViresh Kumar 
1119f3364e17SViresh Kumar 	/*
1120f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
1121f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
1122f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
1123f3364e17SViresh Kumar 	 */
1124f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
1125f3364e17SViresh Kumar 		return 0;
1126f3364e17SViresh Kumar 
1127240ae50eSViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev);
1128f3364e17SViresh Kumar 	if (ret)
1129f3364e17SViresh Kumar 		return ret;
1130f3364e17SViresh Kumar 
1131f3364e17SViresh Kumar 	if (opp_table->regulators)
1132f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
1133f3364e17SViresh Kumar 
11342c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
1135f3364e17SViresh Kumar 
1136f3364e17SViresh Kumar 	opp_table->enabled = false;
1137f3364e17SViresh Kumar 	return ret;
1138f3364e17SViresh Kumar }
1139f3364e17SViresh Kumar 
1140386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
1141386ba854SViresh Kumar 		    struct dev_pm_opp *opp, unsigned long freq)
11427813dd6fSViresh Kumar {
1143386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
1144f0b88fa4SViresh Kumar 	int scaling_down, ret;
11457813dd6fSViresh Kumar 
1146386ba854SViresh Kumar 	if (unlikely(!opp))
1147386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
1148aca48b61SRajendra Nayak 
114981c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
115081c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
115181c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
11527813dd6fSViresh Kumar 
115381c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
115481c4d8a3SViresh Kumar 
115581c4d8a3SViresh Kumar 	/* Return early if nothing to do */
1156de04241aSJonathan Marek 	if (old_opp == opp && opp_table->current_rate == freq &&
1157de04241aSJonathan Marek 	    opp_table->enabled) {
115881c4d8a3SViresh Kumar 		dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
1159386ba854SViresh Kumar 		return 0;
11607813dd6fSViresh Kumar 	}
11617813dd6fSViresh Kumar 
1162f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1163de04241aSJonathan Marek 		__func__, opp_table->current_rate, freq, old_opp->level,
1164de04241aSJonathan Marek 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1165f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1166f0b88fa4SViresh Kumar 
1167f0b88fa4SViresh Kumar 	scaling_down = _opp_compare_key(old_opp, opp);
1168f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1169f0b88fa4SViresh Kumar 		scaling_down = 0;
11707813dd6fSViresh Kumar 
1171ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1172f0b88fa4SViresh Kumar 	if (!scaling_down) {
11732c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1174870d5d96SViresh Kumar 		if (ret) {
1175870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1176386ba854SViresh Kumar 			return ret;
1177ca1b5d77SViresh Kumar 		}
1178ca1b5d77SViresh Kumar 
1179870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1180870d5d96SViresh Kumar 		if (ret) {
1181870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1182870d5d96SViresh Kumar 			return ret;
1183870d5d96SViresh Kumar 		}
1184870d5d96SViresh Kumar 	}
1185870d5d96SViresh Kumar 
11867e535993SViresh Kumar 	if (opp_table->set_opp) {
1187509e4777SViresh Kumar 		ret = _set_opp_custom(opp_table, dev, opp, freq);
11887e535993SViresh Kumar 	} else if (opp_table->regulators) {
11893f62670fSViresh Kumar 		ret = _generic_set_opp_regulator(opp_table, dev, opp, freq,
11903f62670fSViresh Kumar 						 scaling_down);
11917813dd6fSViresh Kumar 	} else {
11927813dd6fSViresh Kumar 		/* Only frequency scaling */
11931d3c42caSViresh Kumar 		ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
11947813dd6fSViresh Kumar 	}
11957813dd6fSViresh Kumar 
1196ca1b5d77SViresh Kumar 	if (ret)
1197870d5d96SViresh Kumar 		return ret;
1198870d5d96SViresh Kumar 
1199870d5d96SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1200870d5d96SViresh Kumar 	if (scaling_down) {
1201870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1202870d5d96SViresh Kumar 		if (ret) {
1203870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1204870d5d96SViresh Kumar 			return ret;
1205ca1b5d77SViresh Kumar 		}
1206ca1b5d77SViresh Kumar 
1207870d5d96SViresh Kumar 		ret = _set_required_opps(dev, opp_table, opp, false);
1208870d5d96SViresh Kumar 		if (ret) {
1209870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1210870d5d96SViresh Kumar 			return ret;
1211870d5d96SViresh Kumar 		}
1212870d5d96SViresh Kumar 	}
1213870d5d96SViresh Kumar 
121472f80ce4SViresh Kumar 	opp_table->enabled = true;
121581c4d8a3SViresh Kumar 	dev_pm_opp_put(old_opp);
121681c4d8a3SViresh Kumar 
121781c4d8a3SViresh Kumar 	/* Make sure current_opp doesn't get freed */
121881c4d8a3SViresh Kumar 	dev_pm_opp_get(opp);
121981c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
1220de04241aSJonathan Marek 	opp_table->current_rate = freq;
1221fe2af402SGeorgi Djakov 
1222386ba854SViresh Kumar 	return ret;
1223386ba854SViresh Kumar }
1224386ba854SViresh Kumar 
1225386ba854SViresh Kumar /**
1226386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1227386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1228386ba854SViresh Kumar  * @target_freq: frequency to achieve
1229386ba854SViresh Kumar  *
1230386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1231386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1232386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1233386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1234386ba854SViresh Kumar  * frequency.
1235386ba854SViresh Kumar  */
1236386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1237386ba854SViresh Kumar {
1238386ba854SViresh Kumar 	struct opp_table *opp_table;
1239386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1240386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
1241386ba854SViresh Kumar 	int ret;
1242386ba854SViresh Kumar 
1243386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1244386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1245386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1246386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1247386ba854SViresh Kumar 	}
1248386ba854SViresh Kumar 
1249386ba854SViresh Kumar 	if (target_freq) {
1250386ba854SViresh Kumar 		/*
1251386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1252386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1253386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1254386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1255386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1256386ba854SViresh Kumar 		 */
1257386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
1258386ba854SViresh Kumar 			ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq);
1259386ba854SViresh Kumar 			goto put_opp_table;
1260386ba854SViresh Kumar 		}
1261386ba854SViresh Kumar 
1262386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1263386ba854SViresh Kumar 		if ((long)freq <= 0)
1264386ba854SViresh Kumar 			freq = target_freq;
1265386ba854SViresh Kumar 
1266386ba854SViresh Kumar 		/*
1267386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1268386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1269386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1270386ba854SViresh Kumar 		 */
1271386ba854SViresh Kumar 		temp_freq = freq;
1272386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1273386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1274386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1275386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1276386ba854SViresh Kumar 				__func__, freq, ret);
1277386ba854SViresh Kumar 			goto put_opp_table;
1278386ba854SViresh Kumar 		}
1279386ba854SViresh Kumar 	}
1280386ba854SViresh Kumar 
1281386ba854SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, freq);
1282386ba854SViresh Kumar 
1283386ba854SViresh Kumar 	if (target_freq)
12847813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
12857813dd6fSViresh Kumar put_opp_table:
12867813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
12877813dd6fSViresh Kumar 	return ret;
12887813dd6fSViresh Kumar }
12897813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
12907813dd6fSViresh Kumar 
1291abbe3483SViresh Kumar /**
1292abbe3483SViresh Kumar  * dev_pm_opp_set_opp() - Configure device for OPP
1293abbe3483SViresh Kumar  * @dev: device for which we do this operation
1294abbe3483SViresh Kumar  * @opp: OPP to set to
1295abbe3483SViresh Kumar  *
1296abbe3483SViresh Kumar  * This configures the device based on the properties of the OPP passed to this
1297abbe3483SViresh Kumar  * routine.
1298abbe3483SViresh Kumar  *
1299abbe3483SViresh Kumar  * Return: 0 on success, a negative error number otherwise.
1300abbe3483SViresh Kumar  */
1301abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1302abbe3483SViresh Kumar {
1303abbe3483SViresh Kumar 	struct opp_table *opp_table;
1304abbe3483SViresh Kumar 	int ret;
1305abbe3483SViresh Kumar 
1306abbe3483SViresh Kumar 	opp_table = _find_opp_table(dev);
1307abbe3483SViresh Kumar 	if (IS_ERR(opp_table)) {
1308abbe3483SViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1309abbe3483SViresh Kumar 		return PTR_ERR(opp_table);
1310abbe3483SViresh Kumar 	}
1311abbe3483SViresh Kumar 
1312abbe3483SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0);
1313abbe3483SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1314abbe3483SViresh Kumar 
1315abbe3483SViresh Kumar 	return ret;
1316abbe3483SViresh Kumar }
1317abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1318abbe3483SViresh Kumar 
13197813dd6fSViresh Kumar /* OPP-dev Helpers */
13207813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
13217813dd6fSViresh Kumar 			    struct opp_table *opp_table)
13227813dd6fSViresh Kumar {
13237813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
13247813dd6fSViresh Kumar 	list_del(&opp_dev->node);
13257813dd6fSViresh Kumar 	kfree(opp_dev);
13267813dd6fSViresh Kumar }
13277813dd6fSViresh Kumar 
1328ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
13297813dd6fSViresh Kumar 				struct opp_table *opp_table)
13307813dd6fSViresh Kumar {
13317813dd6fSViresh Kumar 	struct opp_device *opp_dev;
13327813dd6fSViresh Kumar 
13337813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
13347813dd6fSViresh Kumar 	if (!opp_dev)
13357813dd6fSViresh Kumar 		return NULL;
13367813dd6fSViresh Kumar 
13377813dd6fSViresh Kumar 	/* Initialize opp-dev */
13387813dd6fSViresh Kumar 	opp_dev->dev = dev;
13393d255699SViresh Kumar 
1340ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
13417813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1342ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
13437813dd6fSViresh Kumar 
13447813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1345a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1346283d55e6SViresh Kumar 
1347283d55e6SViresh Kumar 	return opp_dev;
1348283d55e6SViresh Kumar }
1349283d55e6SViresh Kumar 
1350eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
13517813dd6fSViresh Kumar {
13527813dd6fSViresh Kumar 	struct opp_table *opp_table;
13537813dd6fSViresh Kumar 	struct opp_device *opp_dev;
13547813dd6fSViresh Kumar 	int ret;
13557813dd6fSViresh Kumar 
13567813dd6fSViresh Kumar 	/*
13577813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
13587813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
13597813dd6fSViresh Kumar 	 */
13607813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
13617813dd6fSViresh Kumar 	if (!opp_table)
1362dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
13637813dd6fSViresh Kumar 
13643d255699SViresh Kumar 	mutex_init(&opp_table->lock);
13654f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
13667813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
13677eba0c76SViresh Kumar 	INIT_LIST_HEAD(&opp_table->lazy);
13687813dd6fSViresh Kumar 
136946f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
137046f48acaSViresh Kumar 	opp_table->regulator_count = -1;
137146f48acaSViresh Kumar 
13727813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
13737813dd6fSViresh Kumar 	if (!opp_dev) {
1374dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1375dd461cd9SStephan Gerhold 		goto err;
13767813dd6fSViresh Kumar 	}
13777813dd6fSViresh Kumar 
1378eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
13797813dd6fSViresh Kumar 
13806d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
13816d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1382dd461cd9SStephan Gerhold 	if (ret) {
1383dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
138432439ac7SViresh Kumar 			goto remove_opp_dev;
1385dd461cd9SStephan Gerhold 
13866d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
13876d3f922cSGeorgi Djakov 			 __func__, ret);
1388dd461cd9SStephan Gerhold 	}
13896d3f922cSGeorgi Djakov 
13907813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
13917813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
13927813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
13937813dd6fSViresh Kumar 
13947813dd6fSViresh Kumar 	return opp_table;
1395dd461cd9SStephan Gerhold 
1396976509bbSQuanyang Wang remove_opp_dev:
1397976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1398dd461cd9SStephan Gerhold err:
1399dd461cd9SStephan Gerhold 	kfree(opp_table);
1400dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
14017813dd6fSViresh Kumar }
14027813dd6fSViresh Kumar 
14037813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
14047813dd6fSViresh Kumar {
14057813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
14067813dd6fSViresh Kumar }
14077813dd6fSViresh Kumar 
140832439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
140932439ac7SViresh Kumar 					       struct opp_table *opp_table,
141032439ac7SViresh Kumar 					       bool getclk)
141132439ac7SViresh Kumar {
1412d4a4c7a4SViresh Kumar 	int ret;
1413d4a4c7a4SViresh Kumar 
141432439ac7SViresh Kumar 	/*
141532439ac7SViresh Kumar 	 * Return early if we don't need to get clk or we have already tried it
141632439ac7SViresh Kumar 	 * earlier.
141732439ac7SViresh Kumar 	 */
141832439ac7SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || opp_table->clk)
141932439ac7SViresh Kumar 		return opp_table;
142032439ac7SViresh Kumar 
142132439ac7SViresh Kumar 	/* Find clk for the device */
142232439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
142332439ac7SViresh Kumar 
1424d4a4c7a4SViresh Kumar 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
1425d4a4c7a4SViresh Kumar 	if (!ret)
142632439ac7SViresh Kumar 		return opp_table;
1427d4a4c7a4SViresh Kumar 
1428d4a4c7a4SViresh Kumar 	if (ret == -ENOENT) {
1429d4a4c7a4SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1430d4a4c7a4SViresh Kumar 		return opp_table;
1431d4a4c7a4SViresh Kumar 	}
1432d4a4c7a4SViresh Kumar 
1433d4a4c7a4SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1434d4a4c7a4SViresh Kumar 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1435d4a4c7a4SViresh Kumar 
1436d4a4c7a4SViresh Kumar 	return ERR_PTR(ret);
143732439ac7SViresh Kumar }
143832439ac7SViresh Kumar 
143927c09484SViresh Kumar /*
144027c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
144127c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
144227c09484SViresh Kumar  *
144327c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
144427c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
144527c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
144627c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
144727c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
144827c09484SViresh Kumar  * indirect users of OPP core.
144927c09484SViresh Kumar  *
145027c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
145127c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
145227c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
145327c09484SViresh Kumar  */
145432439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
145532439ac7SViresh Kumar 					 bool getclk)
14567813dd6fSViresh Kumar {
14577813dd6fSViresh Kumar 	struct opp_table *opp_table;
14587813dd6fSViresh Kumar 
145927c09484SViresh Kumar again:
14607813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
14617813dd6fSViresh Kumar 
14627813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
14637813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
14647813dd6fSViresh Kumar 		goto unlock;
14657813dd6fSViresh Kumar 
146627c09484SViresh Kumar 	/*
146727c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
146827c09484SViresh Kumar 	 * another user, wait for it to finish.
146927c09484SViresh Kumar 	 */
147027c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
147127c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
147227c09484SViresh Kumar 		cpu_relax();
147327c09484SViresh Kumar 		goto again;
147427c09484SViresh Kumar 	}
147527c09484SViresh Kumar 
147627c09484SViresh Kumar 	opp_tables_busy = true;
1477283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
147827c09484SViresh Kumar 
147927c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
148027c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
148127c09484SViresh Kumar 
1482283d55e6SViresh Kumar 	if (opp_table) {
1483ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1484283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1485dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1486283d55e6SViresh Kumar 		}
148727c09484SViresh Kumar 
148827c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
148927c09484SViresh Kumar 	} else {
149027c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
149127c09484SViresh Kumar 
149227c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
149327c09484SViresh Kumar 		if (!IS_ERR(opp_table))
149427c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1495283d55e6SViresh Kumar 	}
1496283d55e6SViresh Kumar 
149727c09484SViresh Kumar 	opp_tables_busy = false;
14987813dd6fSViresh Kumar 
14997813dd6fSViresh Kumar unlock:
15007813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
15017813dd6fSViresh Kumar 
150232439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
15037813dd6fSViresh Kumar }
1504eb7c8743SViresh Kumar 
150532439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1506e77dcb0bSViresh Kumar {
150732439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1508e77dcb0bSViresh Kumar }
1509e77dcb0bSViresh Kumar 
1510eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1511eb7c8743SViresh Kumar {
1512e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1513eb7c8743SViresh Kumar }
15147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
15157813dd6fSViresh Kumar 
15167813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
15177813dd6fSViresh Kumar {
15187813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1519cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
15206d3f922cSGeorgi Djakov 	int i;
15217813dd6fSViresh Kumar 
1522e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1523e0df59deSViresh Kumar 	list_del(&opp_table->node);
1524e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1525e0df59deSViresh Kumar 
152681c4d8a3SViresh Kumar 	if (opp_table->current_opp)
152781c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
152881c4d8a3SViresh Kumar 
15295d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
15305d6d106fSViresh Kumar 
15317813dd6fSViresh Kumar 	/* Release clk */
15327813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
15337813dd6fSViresh Kumar 		clk_put(opp_table->clk);
15347813dd6fSViresh Kumar 
15356d3f922cSGeorgi Djakov 	if (opp_table->paths) {
15366d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
15376d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
15386d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
15396d3f922cSGeorgi Djakov 	}
15406d3f922cSGeorgi Djakov 
1541cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1542cdd6ed90SViresh Kumar 
1543cdd6ed90SViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
15443d255699SViresh Kumar 		/*
1545cdd6ed90SViresh Kumar 		 * The OPP table is getting removed, drop the performance state
1546cdd6ed90SViresh Kumar 		 * constraints.
15473d255699SViresh Kumar 		 */
1548cdd6ed90SViresh Kumar 		if (opp_table->genpd_performance_state)
1549cdd6ed90SViresh Kumar 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
15507813dd6fSViresh Kumar 
15517813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
1552cdd6ed90SViresh Kumar 	}
15537813dd6fSViresh Kumar 
15544f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
15557813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
15567813dd6fSViresh Kumar 	kfree(opp_table);
15577813dd6fSViresh Kumar }
15587813dd6fSViresh Kumar 
15597813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
15607813dd6fSViresh Kumar {
15617813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
15627813dd6fSViresh Kumar 		       &opp_table_lock);
15637813dd6fSViresh Kumar }
15647813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
15657813dd6fSViresh Kumar 
15667813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
15677813dd6fSViresh Kumar {
15687813dd6fSViresh Kumar 	kfree(opp);
15697813dd6fSViresh Kumar }
15707813dd6fSViresh Kumar 
1571cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
15727813dd6fSViresh Kumar {
1573cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1574cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1575cf1fac94SViresh Kumar 
1576cf1fac94SViresh Kumar 	list_del(&opp->node);
1577cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1578cf1fac94SViresh Kumar 
15797813dd6fSViresh Kumar 	/*
15807813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
15817813dd6fSViresh Kumar 	 * frequency/voltage list.
15827813dd6fSViresh Kumar 	 */
15837813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1584da544b61SViresh Kumar 	_of_opp_free_required_opps(opp_table, opp);
15857813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
15867813dd6fSViresh Kumar 	kfree(opp);
15871690d8bbSViresh Kumar }
15887813dd6fSViresh Kumar 
1589a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
15907813dd6fSViresh Kumar {
15917813dd6fSViresh Kumar 	kref_get(&opp->kref);
15927813dd6fSViresh Kumar }
15937813dd6fSViresh Kumar 
15947813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
15957813dd6fSViresh Kumar {
1596cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
15977813dd6fSViresh Kumar }
15987813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
15997813dd6fSViresh Kumar 
16007813dd6fSViresh Kumar /**
16017813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
16027813dd6fSViresh Kumar  * @dev:	device for which we do this operation
16037813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
16047813dd6fSViresh Kumar  *
16057813dd6fSViresh Kumar  * This function removes an opp from the opp table.
16067813dd6fSViresh Kumar  */
16077813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
16087813dd6fSViresh Kumar {
160995073b72SJakob Koschel 	struct dev_pm_opp *opp = NULL, *iter;
16107813dd6fSViresh Kumar 	struct opp_table *opp_table;
16117813dd6fSViresh Kumar 
16127813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
16137813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
16147813dd6fSViresh Kumar 		return;
16157813dd6fSViresh Kumar 
16167813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
16177813dd6fSViresh Kumar 
161895073b72SJakob Koschel 	list_for_each_entry(iter, &opp_table->opp_list, node) {
161995073b72SJakob Koschel 		if (iter->rate == freq) {
162095073b72SJakob Koschel 			opp = iter;
16217813dd6fSViresh Kumar 			break;
16227813dd6fSViresh Kumar 		}
16237813dd6fSViresh Kumar 	}
16247813dd6fSViresh Kumar 
16257813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
16267813dd6fSViresh Kumar 
162795073b72SJakob Koschel 	if (opp) {
16287813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
16290ad8c623SViresh Kumar 
16300ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
16310ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
16327813dd6fSViresh Kumar 	} else {
16337813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
16347813dd6fSViresh Kumar 			 __func__, freq);
16357813dd6fSViresh Kumar 	}
16367813dd6fSViresh Kumar 
16370ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
16387813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
16397813dd6fSViresh Kumar }
16407813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
16417813dd6fSViresh Kumar 
1642cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1643cf1fac94SViresh Kumar 					bool dynamic)
1644cf1fac94SViresh Kumar {
1645cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1646cf1fac94SViresh Kumar 
1647cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1648cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1649606a5d42SBeata Michalska 		/*
1650606a5d42SBeata Michalska 		 * Refcount must be dropped only once for each OPP by OPP core,
1651606a5d42SBeata Michalska 		 * do that with help of "removed" flag.
1652606a5d42SBeata Michalska 		 */
1653606a5d42SBeata Michalska 		if (!temp->removed && dynamic == temp->dynamic) {
1654cf1fac94SViresh Kumar 			opp = temp;
1655cf1fac94SViresh Kumar 			break;
1656cf1fac94SViresh Kumar 		}
1657cf1fac94SViresh Kumar 	}
1658cf1fac94SViresh Kumar 
1659cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1660cf1fac94SViresh Kumar 	return opp;
1661cf1fac94SViresh Kumar }
1662cf1fac94SViresh Kumar 
1663606a5d42SBeata Michalska /*
1664606a5d42SBeata Michalska  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1665606a5d42SBeata Michalska  * happen lock less to avoid circular dependency issues. This routine must be
1666606a5d42SBeata Michalska  * called without the opp_table->lock held.
1667606a5d42SBeata Michalska  */
1668606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
166903758d60SViresh Kumar {
1670cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
167103758d60SViresh Kumar 
1672606a5d42SBeata Michalska 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1673606a5d42SBeata Michalska 		opp->removed = true;
1674606a5d42SBeata Michalska 		dev_pm_opp_put(opp);
1675606a5d42SBeata Michalska 
1676606a5d42SBeata Michalska 		/* Drop the references taken by dev_pm_opp_add() */
1677606a5d42SBeata Michalska 		if (dynamic)
1678606a5d42SBeata Michalska 			dev_pm_opp_put_opp_table(opp_table);
1679606a5d42SBeata Michalska 	}
1680606a5d42SBeata Michalska }
1681606a5d42SBeata Michalska 
1682606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table)
1683606a5d42SBeata Michalska {
168403758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
168503758d60SViresh Kumar 
1686922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1687cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1688cf1fac94SViresh Kumar 		return false;
1689922ff075SViresh Kumar 	}
1690922ff075SViresh Kumar 
1691cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1692cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1693cf1fac94SViresh Kumar 		return true;
169403758d60SViresh Kumar 	}
169503758d60SViresh Kumar 
169603758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1697922ff075SViresh Kumar 
1698606a5d42SBeata Michalska 	_opp_remove_all(opp_table, false);
1699cf1fac94SViresh Kumar 	return true;
170003758d60SViresh Kumar }
170103758d60SViresh Kumar 
17021690d8bbSViresh Kumar /**
17031690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
17041690d8bbSViresh Kumar  * @dev:	device for which we do this operation
17051690d8bbSViresh Kumar  *
17061690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
17071690d8bbSViresh Kumar  */
17081690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
17091690d8bbSViresh Kumar {
17101690d8bbSViresh Kumar 	struct opp_table *opp_table;
17111690d8bbSViresh Kumar 
17121690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
17131690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
17141690d8bbSViresh Kumar 		return;
17151690d8bbSViresh Kumar 
1716606a5d42SBeata Michalska 	_opp_remove_all(opp_table, true);
17171690d8bbSViresh Kumar 
17181690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
17191690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17201690d8bbSViresh Kumar }
17211690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
17221690d8bbSViresh Kumar 
17237813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table)
17247813dd6fSViresh Kumar {
17257813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
17266d3f922cSGeorgi Djakov 	int supply_count, supply_size, icc_size;
17277813dd6fSViresh Kumar 
17287813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
17296d3f922cSGeorgi Djakov 	supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
17306d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
17316d3f922cSGeorgi Djakov 	icc_size = sizeof(*opp->bandwidth) * table->path_count;
17327813dd6fSViresh Kumar 
17337813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
17346d3f922cSGeorgi Djakov 	opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
17356d3f922cSGeorgi Djakov 
17367813dd6fSViresh Kumar 	if (!opp)
17377813dd6fSViresh Kumar 		return NULL;
17387813dd6fSViresh Kumar 
17397813dd6fSViresh Kumar 	/* Put the supplies at the end of the OPP structure as an empty array */
17407813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
17416d3f922cSGeorgi Djakov 	if (icc_size)
17426d3f922cSGeorgi Djakov 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
17437813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
17447813dd6fSViresh Kumar 
17457813dd6fSViresh Kumar 	return opp;
17467813dd6fSViresh Kumar }
17477813dd6fSViresh Kumar 
17487813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
17497813dd6fSViresh Kumar 					 struct opp_table *opp_table)
17507813dd6fSViresh Kumar {
17517813dd6fSViresh Kumar 	struct regulator *reg;
17527813dd6fSViresh Kumar 	int i;
17537813dd6fSViresh Kumar 
175490e3577bSViresh Kumar 	if (!opp_table->regulators)
175590e3577bSViresh Kumar 		return true;
175690e3577bSViresh Kumar 
17577813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
17587813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
17597813dd6fSViresh Kumar 
17607813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
17617813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
17627813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
17637813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
17647813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
17657813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
17667813dd6fSViresh Kumar 			return false;
17677813dd6fSViresh Kumar 		}
17687813dd6fSViresh Kumar 	}
17697813dd6fSViresh Kumar 
17707813dd6fSViresh Kumar 	return true;
17717813dd6fSViresh Kumar }
17727813dd6fSViresh Kumar 
17736c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
17746c591eecSSaravana Kannan {
17756c591eecSSaravana Kannan 	if (opp1->rate != opp2->rate)
17766c591eecSSaravana Kannan 		return opp1->rate < opp2->rate ? -1 : 1;
17776d3f922cSGeorgi Djakov 	if (opp1->bandwidth && opp2->bandwidth &&
17786d3f922cSGeorgi Djakov 	    opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
17796d3f922cSGeorgi Djakov 		return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
17806c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
17816c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
17826c591eecSSaravana Kannan 	return 0;
17836c591eecSSaravana Kannan }
17846c591eecSSaravana Kannan 
1785a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1786a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1787a1e8c136SViresh Kumar 			     struct list_head **head)
1788a1e8c136SViresh Kumar {
1789a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
17906c591eecSSaravana Kannan 	int opp_cmp;
1791a1e8c136SViresh Kumar 
1792a1e8c136SViresh Kumar 	/*
1793a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1794a1e8c136SViresh Kumar 	 * already present.
1795a1e8c136SViresh Kumar 	 *
1796a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1797a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1798a1e8c136SViresh Kumar 	 * loop.
1799a1e8c136SViresh Kumar 	 */
1800a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
18016c591eecSSaravana Kannan 		opp_cmp = _opp_compare_key(new_opp, opp);
18026c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1803a1e8c136SViresh Kumar 			*head = &opp->node;
1804a1e8c136SViresh Kumar 			continue;
1805a1e8c136SViresh Kumar 		}
1806a1e8c136SViresh Kumar 
18076c591eecSSaravana Kannan 		if (opp_cmp < 0)
1808a1e8c136SViresh Kumar 			return 0;
1809a1e8c136SViresh Kumar 
1810a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1811a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1812a1e8c136SViresh Kumar 			 __func__, opp->rate, opp->supplies[0].u_volt,
1813a1e8c136SViresh Kumar 			 opp->available, new_opp->rate,
1814a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1815a1e8c136SViresh Kumar 
1816a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1817a1e8c136SViresh Kumar 		return opp->available &&
1818a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1819a1e8c136SViresh Kumar 	}
1820a1e8c136SViresh Kumar 
1821a1e8c136SViresh Kumar 	return 0;
1822a1e8c136SViresh Kumar }
1823a1e8c136SViresh Kumar 
18247eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count)
18257eba0c76SViresh Kumar {
18267eba0c76SViresh Kumar 	int i;
18277eba0c76SViresh Kumar 
18287eba0c76SViresh Kumar 	for (i = 0; i < count; i++) {
18297eba0c76SViresh Kumar 		if (opp->required_opps[i]->available)
18307eba0c76SViresh Kumar 			continue;
18317eba0c76SViresh Kumar 
18327eba0c76SViresh Kumar 		opp->available = false;
18337eba0c76SViresh Kumar 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
18347eba0c76SViresh Kumar 			 __func__, opp->required_opps[i]->np, opp->rate);
18357eba0c76SViresh Kumar 		return;
18367eba0c76SViresh Kumar 	}
18377eba0c76SViresh Kumar }
18387eba0c76SViresh Kumar 
18397813dd6fSViresh Kumar /*
18407813dd6fSViresh Kumar  * Returns:
18417813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
18427813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
18437813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
18447813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
18457813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
18467813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
18477813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
18487813dd6fSViresh Kumar  */
18497813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1850a1e8c136SViresh Kumar 	     struct opp_table *opp_table, bool rate_not_available)
18517813dd6fSViresh Kumar {
18527813dd6fSViresh Kumar 	struct list_head *head;
18537813dd6fSViresh Kumar 	int ret;
18547813dd6fSViresh Kumar 
18557813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
18567813dd6fSViresh Kumar 	head = &opp_table->opp_list;
18577813dd6fSViresh Kumar 
1858a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1859a1e8c136SViresh Kumar 	if (ret) {
18607813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
18617813dd6fSViresh Kumar 		return ret;
18627813dd6fSViresh Kumar 	}
18637813dd6fSViresh Kumar 
18647813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
18657813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
18667813dd6fSViresh Kumar 
18677813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
18687813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
18697813dd6fSViresh Kumar 
1870a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
18717813dd6fSViresh Kumar 
18727813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
18737813dd6fSViresh Kumar 		new_opp->available = false;
18747813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
18757813dd6fSViresh Kumar 			 __func__, new_opp->rate);
18767813dd6fSViresh Kumar 	}
18777813dd6fSViresh Kumar 
18787eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
18797eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
18807eba0c76SViresh Kumar 		return 0;
1881cf65948dSDmitry Osipenko 
18827eba0c76SViresh Kumar 	_required_opps_available(new_opp, opp_table->required_opp_count);
1883cf65948dSDmitry Osipenko 
18847813dd6fSViresh Kumar 	return 0;
18857813dd6fSViresh Kumar }
18867813dd6fSViresh Kumar 
18877813dd6fSViresh Kumar /**
18887813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
18897813dd6fSViresh Kumar  * @opp_table:	OPP table
18907813dd6fSViresh Kumar  * @dev:	device for which we do this operation
18917813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
18927813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
18937813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
18947813dd6fSViresh Kumar  *
18957813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
18967813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
18977813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
18987813dd6fSViresh Kumar  *
18997813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
19007813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
19017813dd6fSViresh Kumar  *
19027813dd6fSViresh Kumar  * Return:
19037813dd6fSViresh Kumar  * 0		On success OR
19047813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
19057813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
19067813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
19077813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
19087813dd6fSViresh Kumar  */
19097813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
19107813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
19117813dd6fSViresh Kumar {
19127813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
19137813dd6fSViresh Kumar 	unsigned long tol;
19147813dd6fSViresh Kumar 	int ret;
19157813dd6fSViresh Kumar 
19167813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
19177813dd6fSViresh Kumar 	if (!new_opp)
19187813dd6fSViresh Kumar 		return -ENOMEM;
19197813dd6fSViresh Kumar 
19207813dd6fSViresh Kumar 	/* populate the opp table */
19217813dd6fSViresh Kumar 	new_opp->rate = freq;
19227813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
19237813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
19247813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
19257813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
19267813dd6fSViresh Kumar 	new_opp->available = true;
19277813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
19287813dd6fSViresh Kumar 
1929a1e8c136SViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table, false);
19307813dd6fSViresh Kumar 	if (ret) {
19317813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
19327813dd6fSViresh Kumar 		if (ret == -EBUSY)
19337813dd6fSViresh Kumar 			ret = 0;
19347813dd6fSViresh Kumar 		goto free_opp;
19357813dd6fSViresh Kumar 	}
19367813dd6fSViresh Kumar 
19377813dd6fSViresh Kumar 	/*
19387813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
19397813dd6fSViresh Kumar 	 * frequency/voltage list.
19407813dd6fSViresh Kumar 	 */
19417813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
19427813dd6fSViresh Kumar 	return 0;
19437813dd6fSViresh Kumar 
19447813dd6fSViresh Kumar free_opp:
19457813dd6fSViresh Kumar 	_opp_free(new_opp);
19467813dd6fSViresh Kumar 
19477813dd6fSViresh Kumar 	return ret;
19487813dd6fSViresh Kumar }
19497813dd6fSViresh Kumar 
19507813dd6fSViresh Kumar /**
19517813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw() - Set supported platforms
19527813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
19537813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
19547813dd6fSViresh Kumar  * @count: Number of elements in the array.
19557813dd6fSViresh Kumar  *
19567813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
19577813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
19587813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
19597813dd6fSViresh Kumar  * property.
19607813dd6fSViresh Kumar  */
19617813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
19627813dd6fSViresh Kumar 			const u32 *versions, unsigned int count)
19637813dd6fSViresh Kumar {
19647813dd6fSViresh Kumar 	struct opp_table *opp_table;
19657813dd6fSViresh Kumar 
196632439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1967dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1968dd461cd9SStephan Gerhold 		return opp_table;
19697813dd6fSViresh Kumar 
19707813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
19717813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
19727813dd6fSViresh Kumar 
197325419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
197425419de1SViresh Kumar 	if (opp_table->supported_hw)
197525419de1SViresh Kumar 		return opp_table;
19767813dd6fSViresh Kumar 
19777813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
19787813dd6fSViresh Kumar 					GFP_KERNEL);
19797813dd6fSViresh Kumar 	if (!opp_table->supported_hw) {
198025419de1SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
198125419de1SViresh Kumar 		return ERR_PTR(-ENOMEM);
19827813dd6fSViresh Kumar 	}
19837813dd6fSViresh Kumar 
19847813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
19857813dd6fSViresh Kumar 
19867813dd6fSViresh Kumar 	return opp_table;
19877813dd6fSViresh Kumar }
19887813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
19897813dd6fSViresh Kumar 
19907813dd6fSViresh Kumar /**
19917813dd6fSViresh Kumar  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
19927813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
19937813dd6fSViresh Kumar  *
19947813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
19957813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
19967813dd6fSViresh Kumar  * will not be freed.
19977813dd6fSViresh Kumar  */
19987813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
19997813dd6fSViresh Kumar {
2000c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2001c7bf8758SViresh Kumar 		return;
2002c7bf8758SViresh Kumar 
20037813dd6fSViresh Kumar 	kfree(opp_table->supported_hw);
20047813dd6fSViresh Kumar 	opp_table->supported_hw = NULL;
20057813dd6fSViresh Kumar 	opp_table->supported_hw_count = 0;
20067813dd6fSViresh Kumar 
20077813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
20087813dd6fSViresh Kumar }
20097813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
20107813dd6fSViresh Kumar 
20119c4f220fSYangtao Li static void devm_pm_opp_supported_hw_release(void *data)
20129c4f220fSYangtao Li {
20139c4f220fSYangtao Li 	dev_pm_opp_put_supported_hw(data);
20149c4f220fSYangtao Li }
20159c4f220fSYangtao Li 
20169c4f220fSYangtao Li /**
20179c4f220fSYangtao Li  * devm_pm_opp_set_supported_hw() - Set supported platforms
20189c4f220fSYangtao Li  * @dev: Device for which supported-hw has to be set.
20199c4f220fSYangtao Li  * @versions: Array of hierarchy of versions to match.
20209c4f220fSYangtao Li  * @count: Number of elements in the array.
20219c4f220fSYangtao Li  *
20229c4f220fSYangtao Li  * This is a resource-managed variant of dev_pm_opp_set_supported_hw().
20239c4f220fSYangtao Li  *
20249c4f220fSYangtao Li  * Return: 0 on success and errorno otherwise.
20259c4f220fSYangtao Li  */
20269c4f220fSYangtao Li int devm_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
20279c4f220fSYangtao Li 				 unsigned int count)
20289c4f220fSYangtao Li {
20299c4f220fSYangtao Li 	struct opp_table *opp_table;
20309c4f220fSYangtao Li 
20319c4f220fSYangtao Li 	opp_table = dev_pm_opp_set_supported_hw(dev, versions, count);
20329c4f220fSYangtao Li 	if (IS_ERR(opp_table))
20339c4f220fSYangtao Li 		return PTR_ERR(opp_table);
20349c4f220fSYangtao Li 
20359c4f220fSYangtao Li 	return devm_add_action_or_reset(dev, devm_pm_opp_supported_hw_release,
20369c4f220fSYangtao Li 					opp_table);
20379c4f220fSYangtao Li }
20389c4f220fSYangtao Li EXPORT_SYMBOL_GPL(devm_pm_opp_set_supported_hw);
20399c4f220fSYangtao Li 
20407813dd6fSViresh Kumar /**
20417813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name() - Set prop-extn name
20427813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
20437813dd6fSViresh Kumar  * @name: name to postfix to properties.
20447813dd6fSViresh Kumar  *
20457813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
20467813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
20477813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
20487813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
20497813dd6fSViresh Kumar  */
20507813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
20517813dd6fSViresh Kumar {
20527813dd6fSViresh Kumar 	struct opp_table *opp_table;
20537813dd6fSViresh Kumar 
205432439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2055dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2056dd461cd9SStephan Gerhold 		return opp_table;
20577813dd6fSViresh Kumar 
20587813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
20597813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
20607813dd6fSViresh Kumar 
2061878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
2062878ec1a9SViresh Kumar 	if (opp_table->prop_name)
2063878ec1a9SViresh Kumar 		return opp_table;
20647813dd6fSViresh Kumar 
20657813dd6fSViresh Kumar 	opp_table->prop_name = kstrdup(name, GFP_KERNEL);
20667813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
2067878ec1a9SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
2068878ec1a9SViresh Kumar 		return ERR_PTR(-ENOMEM);
20697813dd6fSViresh Kumar 	}
20707813dd6fSViresh Kumar 
20717813dd6fSViresh Kumar 	return opp_table;
20727813dd6fSViresh Kumar }
20737813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
20747813dd6fSViresh Kumar 
20757813dd6fSViresh Kumar /**
20767813dd6fSViresh Kumar  * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
20777813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
20787813dd6fSViresh Kumar  *
20797813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
20807813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
20817813dd6fSViresh Kumar  * will not be freed.
20827813dd6fSViresh Kumar  */
20837813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
20847813dd6fSViresh Kumar {
2085c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2086c7bf8758SViresh Kumar 		return;
2087c7bf8758SViresh Kumar 
20887813dd6fSViresh Kumar 	kfree(opp_table->prop_name);
20897813dd6fSViresh Kumar 	opp_table->prop_name = NULL;
20907813dd6fSViresh Kumar 
20917813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
20927813dd6fSViresh Kumar }
20937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
20947813dd6fSViresh Kumar 
20957813dd6fSViresh Kumar /**
20967813dd6fSViresh Kumar  * dev_pm_opp_set_regulators() - Set regulator names for the device
20977813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
20987813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
20997813dd6fSViresh Kumar  * @count: Number of regulators.
21007813dd6fSViresh Kumar  *
21017813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
21027813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
21037813dd6fSViresh Kumar  * well.
21047813dd6fSViresh Kumar  *
21057813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
21067813dd6fSViresh Kumar  */
21077813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
21087813dd6fSViresh Kumar 					    const char * const names[],
21097813dd6fSViresh Kumar 					    unsigned int count)
21107813dd6fSViresh Kumar {
211138bb3439SViresh Kumar 	struct dev_pm_opp_supply *supplies;
21127813dd6fSViresh Kumar 	struct opp_table *opp_table;
21137813dd6fSViresh Kumar 	struct regulator *reg;
21147813dd6fSViresh Kumar 	int ret, i;
21157813dd6fSViresh Kumar 
211632439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2117dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2118dd461cd9SStephan Gerhold 		return opp_table;
21197813dd6fSViresh Kumar 
21207813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
21217813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
21227813dd6fSViresh Kumar 		ret = -EBUSY;
21237813dd6fSViresh Kumar 		goto err;
21247813dd6fSViresh Kumar 	}
21257813dd6fSViresh Kumar 
2126779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
2127779b783cSViresh Kumar 	if (opp_table->regulators)
2128779b783cSViresh Kumar 		return opp_table;
21297813dd6fSViresh Kumar 
21307813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
21317813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
21327813dd6fSViresh Kumar 					      GFP_KERNEL);
21337813dd6fSViresh Kumar 	if (!opp_table->regulators) {
21347813dd6fSViresh Kumar 		ret = -ENOMEM;
21357813dd6fSViresh Kumar 		goto err;
21367813dd6fSViresh Kumar 	}
21377813dd6fSViresh Kumar 
21387813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
21397813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
21407813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
2141543256d2SKrzysztof Kozlowski 			ret = dev_err_probe(dev, PTR_ERR(reg),
2142543256d2SKrzysztof Kozlowski 					    "%s: no regulator (%s) found\n",
2143543256d2SKrzysztof Kozlowski 					    __func__, names[i]);
21447813dd6fSViresh Kumar 			goto free_regulators;
21457813dd6fSViresh Kumar 		}
21467813dd6fSViresh Kumar 
21477813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
21487813dd6fSViresh Kumar 	}
21497813dd6fSViresh Kumar 
21507813dd6fSViresh Kumar 	opp_table->regulator_count = count;
21517813dd6fSViresh Kumar 
215238bb3439SViresh Kumar 	supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
215338bb3439SViresh Kumar 	if (!supplies) {
215438bb3439SViresh Kumar 		ret = -ENOMEM;
21557813dd6fSViresh Kumar 		goto free_regulators;
215638bb3439SViresh Kumar 	}
215738bb3439SViresh Kumar 
215838bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
215938bb3439SViresh Kumar 	opp_table->sod_supplies = supplies;
216038bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
216138bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = supplies;
216238bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = supplies + count;
216338bb3439SViresh Kumar 	}
216438bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
21657813dd6fSViresh Kumar 
21667813dd6fSViresh Kumar 	return opp_table;
21677813dd6fSViresh Kumar 
21687813dd6fSViresh Kumar free_regulators:
216924957db1SMarek Szyprowski 	while (i != 0)
217024957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
21717813dd6fSViresh Kumar 
21727813dd6fSViresh Kumar 	kfree(opp_table->regulators);
21737813dd6fSViresh Kumar 	opp_table->regulators = NULL;
217446f48acaSViresh Kumar 	opp_table->regulator_count = -1;
21757813dd6fSViresh Kumar err:
21767813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
21777813dd6fSViresh Kumar 
21787813dd6fSViresh Kumar 	return ERR_PTR(ret);
21797813dd6fSViresh Kumar }
21807813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
21817813dd6fSViresh Kumar 
21827813dd6fSViresh Kumar /**
21837813dd6fSViresh Kumar  * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
21847813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
21857813dd6fSViresh Kumar  */
21867813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table)
21877813dd6fSViresh Kumar {
21887813dd6fSViresh Kumar 	int i;
21897813dd6fSViresh Kumar 
2190c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2191c7bf8758SViresh Kumar 		return;
2192c7bf8758SViresh Kumar 
2193779b783cSViresh Kumar 	if (!opp_table->regulators)
2194779b783cSViresh Kumar 		goto put_opp_table;
21957813dd6fSViresh Kumar 
219672f80ce4SViresh Kumar 	if (opp_table->enabled) {
21978d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
21988d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
21998d45719cSKamil Konieczny 	}
22008d45719cSKamil Konieczny 
220124957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
22027813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
22037813dd6fSViresh Kumar 
220438bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
220538bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
220638bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = NULL;
220738bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = NULL;
220838bb3439SViresh Kumar 	}
220938bb3439SViresh Kumar 
221038bb3439SViresh Kumar 	kfree(opp_table->sod_supplies);
221138bb3439SViresh Kumar 	opp_table->sod_supplies = NULL;
221238bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
22137813dd6fSViresh Kumar 
22147813dd6fSViresh Kumar 	kfree(opp_table->regulators);
22157813dd6fSViresh Kumar 	opp_table->regulators = NULL;
221646f48acaSViresh Kumar 	opp_table->regulator_count = -1;
22177813dd6fSViresh Kumar 
2218779b783cSViresh Kumar put_opp_table:
22197813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
22207813dd6fSViresh Kumar }
22217813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
22227813dd6fSViresh Kumar 
222332aee78bSYangtao Li static void devm_pm_opp_regulators_release(void *data)
222432aee78bSYangtao Li {
222532aee78bSYangtao Li 	dev_pm_opp_put_regulators(data);
222632aee78bSYangtao Li }
222732aee78bSYangtao Li 
222832aee78bSYangtao Li /**
222932aee78bSYangtao Li  * devm_pm_opp_set_regulators() - Set regulator names for the device
223032aee78bSYangtao Li  * @dev: Device for which regulator name is being set.
223132aee78bSYangtao Li  * @names: Array of pointers to the names of the regulator.
223232aee78bSYangtao Li  * @count: Number of regulators.
223332aee78bSYangtao Li  *
223432aee78bSYangtao Li  * This is a resource-managed variant of dev_pm_opp_set_regulators().
223532aee78bSYangtao Li  *
223632aee78bSYangtao Li  * Return: 0 on success and errorno otherwise.
223732aee78bSYangtao Li  */
223832aee78bSYangtao Li int devm_pm_opp_set_regulators(struct device *dev,
223932aee78bSYangtao Li 			       const char * const names[],
224032aee78bSYangtao Li 			       unsigned int count)
224132aee78bSYangtao Li {
224232aee78bSYangtao Li 	struct opp_table *opp_table;
224332aee78bSYangtao Li 
224432aee78bSYangtao Li 	opp_table = dev_pm_opp_set_regulators(dev, names, count);
224532aee78bSYangtao Li 	if (IS_ERR(opp_table))
224632aee78bSYangtao Li 		return PTR_ERR(opp_table);
224732aee78bSYangtao Li 
224832aee78bSYangtao Li 	return devm_add_action_or_reset(dev, devm_pm_opp_regulators_release,
224932aee78bSYangtao Li 					opp_table);
225032aee78bSYangtao Li }
225132aee78bSYangtao Li EXPORT_SYMBOL_GPL(devm_pm_opp_set_regulators);
225232aee78bSYangtao Li 
22537813dd6fSViresh Kumar /**
22547813dd6fSViresh Kumar  * dev_pm_opp_set_clkname() - Set clk name for the device
22557813dd6fSViresh Kumar  * @dev: Device for which clk name is being set.
22567813dd6fSViresh Kumar  * @name: Clk name.
22577813dd6fSViresh Kumar  *
22587813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointer to the
22597813dd6fSViresh Kumar  * clock for the device. Simple cases work fine without using this routine (i.e.
22607813dd6fSViresh Kumar  * by passing connection-id as NULL), but for a device with multiple clocks
22617813dd6fSViresh Kumar  * available, the OPP core needs to know the exact name of the clk to use.
22627813dd6fSViresh Kumar  *
22637813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
22647813dd6fSViresh Kumar  */
22657813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
22667813dd6fSViresh Kumar {
22677813dd6fSViresh Kumar 	struct opp_table *opp_table;
22687813dd6fSViresh Kumar 	int ret;
22697813dd6fSViresh Kumar 
227032439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2271dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2272dd461cd9SStephan Gerhold 		return opp_table;
22737813dd6fSViresh Kumar 
22747813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
22757813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
22767813dd6fSViresh Kumar 		ret = -EBUSY;
22777813dd6fSViresh Kumar 		goto err;
22787813dd6fSViresh Kumar 	}
22797813dd6fSViresh Kumar 
228032439ac7SViresh Kumar 	/* clk shouldn't be initialized at this point */
228132439ac7SViresh Kumar 	if (WARN_ON(opp_table->clk)) {
228232439ac7SViresh Kumar 		ret = -EBUSY;
228332439ac7SViresh Kumar 		goto err;
228432439ac7SViresh Kumar 	}
22857813dd6fSViresh Kumar 
22867813dd6fSViresh Kumar 	/* Find clk for the device */
22877813dd6fSViresh Kumar 	opp_table->clk = clk_get(dev, name);
22887813dd6fSViresh Kumar 	if (IS_ERR(opp_table->clk)) {
2289543256d2SKrzysztof Kozlowski 		ret = dev_err_probe(dev, PTR_ERR(opp_table->clk),
2290543256d2SKrzysztof Kozlowski 				    "%s: Couldn't find clock\n", __func__);
22917813dd6fSViresh Kumar 		goto err;
22927813dd6fSViresh Kumar 	}
22937813dd6fSViresh Kumar 
22947813dd6fSViresh Kumar 	return opp_table;
22957813dd6fSViresh Kumar 
22967813dd6fSViresh Kumar err:
22977813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
22987813dd6fSViresh Kumar 
22997813dd6fSViresh Kumar 	return ERR_PTR(ret);
23007813dd6fSViresh Kumar }
23017813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
23027813dd6fSViresh Kumar 
23037813dd6fSViresh Kumar /**
23047813dd6fSViresh Kumar  * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
23057813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
23067813dd6fSViresh Kumar  */
23077813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table)
23087813dd6fSViresh Kumar {
2309c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2310c7bf8758SViresh Kumar 		return;
2311c7bf8758SViresh Kumar 
23127813dd6fSViresh Kumar 	clk_put(opp_table->clk);
23137813dd6fSViresh Kumar 	opp_table->clk = ERR_PTR(-EINVAL);
23147813dd6fSViresh Kumar 
23157813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
23167813dd6fSViresh Kumar }
23177813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
23187813dd6fSViresh Kumar 
2319a74f681cSYangtao Li static void devm_pm_opp_clkname_release(void *data)
2320a74f681cSYangtao Li {
2321a74f681cSYangtao Li 	dev_pm_opp_put_clkname(data);
2322a74f681cSYangtao Li }
2323a74f681cSYangtao Li 
2324a74f681cSYangtao Li /**
2325a74f681cSYangtao Li  * devm_pm_opp_set_clkname() - Set clk name for the device
2326a74f681cSYangtao Li  * @dev: Device for which clk name is being set.
2327a74f681cSYangtao Li  * @name: Clk name.
2328a74f681cSYangtao Li  *
2329a74f681cSYangtao Li  * This is a resource-managed variant of dev_pm_opp_set_clkname().
2330a74f681cSYangtao Li  *
2331a74f681cSYangtao Li  * Return: 0 on success and errorno otherwise.
2332a74f681cSYangtao Li  */
2333a74f681cSYangtao Li int devm_pm_opp_set_clkname(struct device *dev, const char *name)
2334a74f681cSYangtao Li {
2335a74f681cSYangtao Li 	struct opp_table *opp_table;
2336a74f681cSYangtao Li 
2337a74f681cSYangtao Li 	opp_table = dev_pm_opp_set_clkname(dev, name);
2338a74f681cSYangtao Li 	if (IS_ERR(opp_table))
2339a74f681cSYangtao Li 		return PTR_ERR(opp_table);
2340a74f681cSYangtao Li 
2341a74f681cSYangtao Li 	return devm_add_action_or_reset(dev, devm_pm_opp_clkname_release,
2342a74f681cSYangtao Li 					opp_table);
2343a74f681cSYangtao Li }
2344a74f681cSYangtao Li EXPORT_SYMBOL_GPL(devm_pm_opp_set_clkname);
2345a74f681cSYangtao Li 
23467813dd6fSViresh Kumar /**
23477813dd6fSViresh Kumar  * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
23487813dd6fSViresh Kumar  * @dev: Device for which the helper is getting registered.
23497813dd6fSViresh Kumar  * @set_opp: Custom set OPP helper.
23507813dd6fSViresh Kumar  *
23517813dd6fSViresh Kumar  * This is useful to support complex platforms (like platforms with multiple
23527813dd6fSViresh Kumar  * regulators per device), instead of the generic OPP set rate helper.
23537813dd6fSViresh Kumar  *
23547813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
23557813dd6fSViresh Kumar  */
23567813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
23577813dd6fSViresh Kumar 			int (*set_opp)(struct dev_pm_set_opp_data *data))
23587813dd6fSViresh Kumar {
235938bb3439SViresh Kumar 	struct dev_pm_set_opp_data *data;
23607813dd6fSViresh Kumar 	struct opp_table *opp_table;
23617813dd6fSViresh Kumar 
23627813dd6fSViresh Kumar 	if (!set_opp)
23637813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
23647813dd6fSViresh Kumar 
236532439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
236647efcbcbSViresh Kumar 	if (IS_ERR(opp_table))
2367dd461cd9SStephan Gerhold 		return opp_table;
23687813dd6fSViresh Kumar 
23697813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
23707813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
23715019acc6SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
23725019acc6SViresh Kumar 		return ERR_PTR(-EBUSY);
23737813dd6fSViresh Kumar 	}
23747813dd6fSViresh Kumar 
23755019acc6SViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
237638bb3439SViresh Kumar 	if (opp_table->set_opp)
237738bb3439SViresh Kumar 		return opp_table;
237838bb3439SViresh Kumar 
237938bb3439SViresh Kumar 	data = kzalloc(sizeof(*data), GFP_KERNEL);
238038bb3439SViresh Kumar 	if (!data)
238138bb3439SViresh Kumar 		return ERR_PTR(-ENOMEM);
238238bb3439SViresh Kumar 
238338bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
238438bb3439SViresh Kumar 	opp_table->set_opp_data = data;
238538bb3439SViresh Kumar 	if (opp_table->sod_supplies) {
238638bb3439SViresh Kumar 		data->old_opp.supplies = opp_table->sod_supplies;
238738bb3439SViresh Kumar 		data->new_opp.supplies = opp_table->sod_supplies +
238838bb3439SViresh Kumar 					 opp_table->regulator_count;
238938bb3439SViresh Kumar 	}
239038bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
239138bb3439SViresh Kumar 
23927813dd6fSViresh Kumar 	opp_table->set_opp = set_opp;
23937813dd6fSViresh Kumar 
23947813dd6fSViresh Kumar 	return opp_table;
23957813dd6fSViresh Kumar }
23967813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
23977813dd6fSViresh Kumar 
23987813dd6fSViresh Kumar /**
2399604a7aebSViresh Kumar  * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
24007813dd6fSViresh Kumar  *					   set_opp helper
24017813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
24027813dd6fSViresh Kumar  *
24037813dd6fSViresh Kumar  * Release resources blocked for platform specific set_opp helper.
24047813dd6fSViresh Kumar  */
2405604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
24067813dd6fSViresh Kumar {
2407c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2408c7bf8758SViresh Kumar 		return;
2409c7bf8758SViresh Kumar 
24107813dd6fSViresh Kumar 	opp_table->set_opp = NULL;
241138bb3439SViresh Kumar 
241238bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
241338bb3439SViresh Kumar 	kfree(opp_table->set_opp_data);
241438bb3439SViresh Kumar 	opp_table->set_opp_data = NULL;
241538bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
241638bb3439SViresh Kumar 
24177813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
24187813dd6fSViresh Kumar }
2419604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
24207813dd6fSViresh Kumar 
2421a3c47af6SDmitry Osipenko static void devm_pm_opp_unregister_set_opp_helper(void *data)
2422a3c47af6SDmitry Osipenko {
2423a3c47af6SDmitry Osipenko 	dev_pm_opp_unregister_set_opp_helper(data);
2424a3c47af6SDmitry Osipenko }
2425a3c47af6SDmitry Osipenko 
2426a3c47af6SDmitry Osipenko /**
2427a3c47af6SDmitry Osipenko  * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2428a3c47af6SDmitry Osipenko  * @dev: Device for which the helper is getting registered.
2429a3c47af6SDmitry Osipenko  * @set_opp: Custom set OPP helper.
2430a3c47af6SDmitry Osipenko  *
2431a3c47af6SDmitry Osipenko  * This is a resource-managed version of dev_pm_opp_register_set_opp_helper().
2432a3c47af6SDmitry Osipenko  *
2433c41c8a34SDmitry Osipenko  * Return: 0 on success and errorno otherwise.
2434a3c47af6SDmitry Osipenko  */
2435c41c8a34SDmitry Osipenko int devm_pm_opp_register_set_opp_helper(struct device *dev,
2436a3c47af6SDmitry Osipenko 					int (*set_opp)(struct dev_pm_set_opp_data *data))
2437a3c47af6SDmitry Osipenko {
2438a3c47af6SDmitry Osipenko 	struct opp_table *opp_table;
2439a3c47af6SDmitry Osipenko 
2440a3c47af6SDmitry Osipenko 	opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp);
2441a3c47af6SDmitry Osipenko 	if (IS_ERR(opp_table))
2442c41c8a34SDmitry Osipenko 		return PTR_ERR(opp_table);
2443a3c47af6SDmitry Osipenko 
2444c41c8a34SDmitry Osipenko 	return devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper,
2445a3c47af6SDmitry Osipenko 					opp_table);
2446a3c47af6SDmitry Osipenko }
2447a3c47af6SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper);
2448a3c47af6SDmitry Osipenko 
24496319aee1SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
24506319aee1SViresh Kumar {
24516319aee1SViresh Kumar 	int index;
24526319aee1SViresh Kumar 
2453cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2454cb60e960SViresh Kumar 		return;
2455cb60e960SViresh Kumar 
24566319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
24576319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
24586319aee1SViresh Kumar 			continue;
24596319aee1SViresh Kumar 
24606319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
24616319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
24626319aee1SViresh Kumar 	}
2463c0ab9e08SViresh Kumar 
2464c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2465c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
24666319aee1SViresh Kumar }
24676319aee1SViresh Kumar 
24687813dd6fSViresh Kumar /**
24696319aee1SViresh Kumar  * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
24706319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
24716319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
247217a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
24734f018bc0SViresh Kumar  *
24744f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
24754f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
24764f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
24774f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
24786319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
24796319aee1SViresh Kumar  * we don't need to support that separately.
24804f018bc0SViresh Kumar  *
24814f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
24826319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
24834f018bc0SViresh Kumar  *
24846319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
24856319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2486baea35e4SViresh Kumar  *
2487baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2488baea35e4SViresh Kumar  * "required-opps" are added in DT.
24894f018bc0SViresh Kumar  */
249017a8f868SViresh Kumar struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
24913734b9f2SDmitry Osipenko 		const char * const *names, struct device ***virt_devs)
24924f018bc0SViresh Kumar {
24934f018bc0SViresh Kumar 	struct opp_table *opp_table;
24946319aee1SViresh Kumar 	struct device *virt_dev;
2495baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
24963734b9f2SDmitry Osipenko 	const char * const *name = names;
24974f018bc0SViresh Kumar 
249832439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2499dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2500dd461cd9SStephan Gerhold 		return opp_table;
25014f018bc0SViresh Kumar 
2502cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2503cb60e960SViresh Kumar 		return opp_table;
25044f018bc0SViresh Kumar 
25056319aee1SViresh Kumar 	/*
25066319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
25076319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
25086319aee1SViresh Kumar 	 * table is added.
25096319aee1SViresh Kumar 	 */
25106319aee1SViresh Kumar 	if (!opp_table->required_opp_count) {
25116319aee1SViresh Kumar 		ret = -EPROBE_DEFER;
25126319aee1SViresh Kumar 		goto put_table;
25136319aee1SViresh Kumar 	}
25146319aee1SViresh Kumar 
25154f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
25164f018bc0SViresh Kumar 
2517c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2518c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2519c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2520c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2521c0ab9e08SViresh Kumar 		goto unlock;
25224f018bc0SViresh Kumar 
25236319aee1SViresh Kumar 	while (*name) {
25246319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
25256319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
25266319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
25276319aee1SViresh Kumar 			goto err;
25286319aee1SViresh Kumar 		}
25294f018bc0SViresh Kumar 
25306319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
25316319aee1SViresh Kumar 		if (IS_ERR(virt_dev)) {
25326319aee1SViresh Kumar 			ret = PTR_ERR(virt_dev);
25336319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
25346319aee1SViresh Kumar 			goto err;
25354f018bc0SViresh Kumar 		}
25364f018bc0SViresh Kumar 
25374f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2538baea35e4SViresh Kumar 		index++;
25396319aee1SViresh Kumar 		name++;
25406319aee1SViresh Kumar 	}
25416319aee1SViresh Kumar 
254217a8f868SViresh Kumar 	if (virt_devs)
254317a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
25444f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
25454f018bc0SViresh Kumar 
25464f018bc0SViresh Kumar 	return opp_table;
25476319aee1SViresh Kumar 
25486319aee1SViresh Kumar err:
25496319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
2550c0ab9e08SViresh Kumar unlock:
25516319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
25526319aee1SViresh Kumar 
25536319aee1SViresh Kumar put_table:
25546319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
25556319aee1SViresh Kumar 
25566319aee1SViresh Kumar 	return ERR_PTR(ret);
25574f018bc0SViresh Kumar }
25586319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
25594f018bc0SViresh Kumar 
25604f018bc0SViresh Kumar /**
25616319aee1SViresh Kumar  * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
25626319aee1SViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
25634f018bc0SViresh Kumar  *
25646319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
25656319aee1SViresh Kumar  * OPP table.
25664f018bc0SViresh Kumar  */
25676319aee1SViresh Kumar void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
25684f018bc0SViresh Kumar {
2569c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2570c7bf8758SViresh Kumar 		return;
2571c7bf8758SViresh Kumar 
25724f018bc0SViresh Kumar 	/*
25734f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
25744f018bc0SViresh Kumar 	 * used in parallel.
25754f018bc0SViresh Kumar 	 */
25764f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
25776319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
25784f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
25794f018bc0SViresh Kumar 
25806319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
25814f018bc0SViresh Kumar }
25826319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
25834f018bc0SViresh Kumar 
2584b4b9e223SDmitry Osipenko static void devm_pm_opp_detach_genpd(void *data)
2585b4b9e223SDmitry Osipenko {
2586b4b9e223SDmitry Osipenko 	dev_pm_opp_detach_genpd(data);
2587b4b9e223SDmitry Osipenko }
2588b4b9e223SDmitry Osipenko 
2589b4b9e223SDmitry Osipenko /**
2590b4b9e223SDmitry Osipenko  * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual
2591b4b9e223SDmitry Osipenko  *			      device pointer
2592b4b9e223SDmitry Osipenko  * @dev: Consumer device for which the genpd is getting attached.
2593b4b9e223SDmitry Osipenko  * @names: Null terminated array of pointers containing names of genpd to attach.
2594b4b9e223SDmitry Osipenko  * @virt_devs: Pointer to return the array of virtual devices.
2595b4b9e223SDmitry Osipenko  *
2596b4b9e223SDmitry Osipenko  * This is a resource-managed version of dev_pm_opp_attach_genpd().
2597b4b9e223SDmitry Osipenko  *
25989edf48a4SDmitry Osipenko  * Return: 0 on success and errorno otherwise.
2599b4b9e223SDmitry Osipenko  */
26003734b9f2SDmitry Osipenko int devm_pm_opp_attach_genpd(struct device *dev, const char * const *names,
2601b4b9e223SDmitry Osipenko 			     struct device ***virt_devs)
2602b4b9e223SDmitry Osipenko {
2603b4b9e223SDmitry Osipenko 	struct opp_table *opp_table;
2604b4b9e223SDmitry Osipenko 
2605b4b9e223SDmitry Osipenko 	opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs);
2606b4b9e223SDmitry Osipenko 	if (IS_ERR(opp_table))
26079edf48a4SDmitry Osipenko 		return PTR_ERR(opp_table);
2608b4b9e223SDmitry Osipenko 
26099edf48a4SDmitry Osipenko 	return devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd,
2610b4b9e223SDmitry Osipenko 					opp_table);
2611b4b9e223SDmitry Osipenko }
2612b4b9e223SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd);
2613b4b9e223SDmitry Osipenko 
26144f018bc0SViresh Kumar /**
26157d8658efSSaravana Kannan  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
26167d8658efSSaravana Kannan  * @src_table: OPP table which has @dst_table as one of its required OPP table.
26177d8658efSSaravana Kannan  * @dst_table: Required OPP table of the @src_table.
26187d8658efSSaravana Kannan  * @src_opp: OPP from the @src_table.
26197d8658efSSaravana Kannan  *
26207d8658efSSaravana Kannan  * This function returns the OPP (present in @dst_table) pointed out by the
26217d8658efSSaravana Kannan  * "required-opps" property of the @src_opp (present in @src_table).
26227d8658efSSaravana Kannan  *
26237d8658efSSaravana Kannan  * The callers are required to call dev_pm_opp_put() for the returned OPP after
26247d8658efSSaravana Kannan  * use.
26257d8658efSSaravana Kannan  *
26267d8658efSSaravana Kannan  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
26277d8658efSSaravana Kannan  */
26287d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
26297d8658efSSaravana Kannan 						 struct opp_table *dst_table,
26307d8658efSSaravana Kannan 						 struct dev_pm_opp *src_opp)
26317d8658efSSaravana Kannan {
26327d8658efSSaravana Kannan 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
26337d8658efSSaravana Kannan 	int i;
26347d8658efSSaravana Kannan 
26357d8658efSSaravana Kannan 	if (!src_table || !dst_table || !src_opp ||
26367d8658efSSaravana Kannan 	    !src_table->required_opp_tables)
26377d8658efSSaravana Kannan 		return ERR_PTR(-EINVAL);
26387d8658efSSaravana Kannan 
26397d8658efSSaravana Kannan 	/* required-opps not fully initialized yet */
26407d8658efSSaravana Kannan 	if (lazy_linking_pending(src_table))
26417d8658efSSaravana Kannan 		return ERR_PTR(-EBUSY);
26427d8658efSSaravana Kannan 
26437d8658efSSaravana Kannan 	for (i = 0; i < src_table->required_opp_count; i++) {
26447d8658efSSaravana Kannan 		if (src_table->required_opp_tables[i] == dst_table) {
26457d8658efSSaravana Kannan 			mutex_lock(&src_table->lock);
26467d8658efSSaravana Kannan 
26477d8658efSSaravana Kannan 			list_for_each_entry(opp, &src_table->opp_list, node) {
26487d8658efSSaravana Kannan 				if (opp == src_opp) {
26497d8658efSSaravana Kannan 					dest_opp = opp->required_opps[i];
26507d8658efSSaravana Kannan 					dev_pm_opp_get(dest_opp);
26517d8658efSSaravana Kannan 					break;
26527d8658efSSaravana Kannan 				}
26537d8658efSSaravana Kannan 			}
26547d8658efSSaravana Kannan 
26557d8658efSSaravana Kannan 			mutex_unlock(&src_table->lock);
26567d8658efSSaravana Kannan 			break;
26577d8658efSSaravana Kannan 		}
26587d8658efSSaravana Kannan 	}
26597d8658efSSaravana Kannan 
26607d8658efSSaravana Kannan 	if (IS_ERR(dest_opp)) {
26617d8658efSSaravana Kannan 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
26627d8658efSSaravana Kannan 		       src_table, dst_table);
26637d8658efSSaravana Kannan 	}
26647d8658efSSaravana Kannan 
26657d8658efSSaravana Kannan 	return dest_opp;
26667d8658efSSaravana Kannan }
26677d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
26687d8658efSSaravana Kannan 
26697d8658efSSaravana Kannan /**
2670c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2671c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2672c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2673c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2674c8a59103SViresh Kumar  *
2675c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2676c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2677c8a59103SViresh Kumar  * performance state set to @pstate.
2678c8a59103SViresh Kumar  *
2679c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2680c8a59103SViresh Kumar  * value on errors.
2681c8a59103SViresh Kumar  */
2682c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2683c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2684c8a59103SViresh Kumar 				       unsigned int pstate)
2685c8a59103SViresh Kumar {
2686c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2687c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2688c8a59103SViresh Kumar 	int i;
2689c8a59103SViresh Kumar 
2690c8a59103SViresh Kumar 	/*
2691c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2692c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2693c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2694c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2695c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2696c8a59103SViresh Kumar 	 */
2697f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2698c8a59103SViresh Kumar 		return pstate;
2699c8a59103SViresh Kumar 
27007eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
27017eba0c76SViresh Kumar 	if (lazy_linking_pending(src_table))
27027eba0c76SViresh Kumar 		return -EBUSY;
27037eba0c76SViresh Kumar 
2704c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2705c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2706c8a59103SViresh Kumar 			break;
2707c8a59103SViresh Kumar 	}
2708c8a59103SViresh Kumar 
2709c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2710c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2711c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2712c8a59103SViresh Kumar 		return -EINVAL;
2713c8a59103SViresh Kumar 	}
2714c8a59103SViresh Kumar 
2715c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2716c8a59103SViresh Kumar 
2717c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
2718c8a59103SViresh Kumar 		if (opp->pstate == pstate) {
2719c8a59103SViresh Kumar 			dest_pstate = opp->required_opps[i]->pstate;
2720c8a59103SViresh Kumar 			goto unlock;
2721c8a59103SViresh Kumar 		}
2722c8a59103SViresh Kumar 	}
2723c8a59103SViresh Kumar 
2724c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2725c8a59103SViresh Kumar 	       dst_table);
2726c8a59103SViresh Kumar 
2727c8a59103SViresh Kumar unlock:
2728c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2729c8a59103SViresh Kumar 
2730c8a59103SViresh Kumar 	return dest_pstate;
2731c8a59103SViresh Kumar }
2732c8a59103SViresh Kumar 
2733c8a59103SViresh Kumar /**
27347813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
27357813dd6fSViresh Kumar  * @dev:	device for which we do this operation
27367813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
27377813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
27387813dd6fSViresh Kumar  *
27397813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
27407813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
27417813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
27427813dd6fSViresh Kumar  *
27437813dd6fSViresh Kumar  * Return:
27447813dd6fSViresh Kumar  * 0		On success OR
27457813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
27467813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
27477813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
27487813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
27497813dd6fSViresh Kumar  */
27507813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
27517813dd6fSViresh Kumar {
27527813dd6fSViresh Kumar 	struct opp_table *opp_table;
27537813dd6fSViresh Kumar 	int ret;
27547813dd6fSViresh Kumar 
275532439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2756dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2757dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
27587813dd6fSViresh Kumar 
275946f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
276046f48acaSViresh Kumar 	opp_table->regulator_count = 1;
276146f48acaSViresh Kumar 
27627813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
27630ad8c623SViresh Kumar 	if (ret)
27647813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
27650ad8c623SViresh Kumar 
27667813dd6fSViresh Kumar 	return ret;
27677813dd6fSViresh Kumar }
27687813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
27697813dd6fSViresh Kumar 
27707813dd6fSViresh Kumar /**
27717813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
27727813dd6fSViresh Kumar  * @dev:		device for which we do this operation
27737813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
27747813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
27757813dd6fSViresh Kumar  *
27767813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
27777813dd6fSViresh Kumar  * which is isolated here.
27787813dd6fSViresh Kumar  *
27797813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
27807813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
27817813dd6fSViresh Kumar  * successful.
27827813dd6fSViresh Kumar  */
27837813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
27847813dd6fSViresh Kumar 				 bool availability_req)
27857813dd6fSViresh Kumar {
27867813dd6fSViresh Kumar 	struct opp_table *opp_table;
27877813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
27887813dd6fSViresh Kumar 	int r = 0;
27897813dd6fSViresh Kumar 
27907813dd6fSViresh Kumar 	/* Find the opp_table */
27917813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
27927813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
27937813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
27947813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
27957813dd6fSViresh Kumar 		return r;
27967813dd6fSViresh Kumar 	}
27977813dd6fSViresh Kumar 
27987813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
27997813dd6fSViresh Kumar 
28007813dd6fSViresh Kumar 	/* Do we have the frequency? */
28017813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
28027813dd6fSViresh Kumar 		if (tmp_opp->rate == freq) {
28037813dd6fSViresh Kumar 			opp = tmp_opp;
28047813dd6fSViresh Kumar 			break;
28057813dd6fSViresh Kumar 		}
28067813dd6fSViresh Kumar 	}
28077813dd6fSViresh Kumar 
28087813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
28097813dd6fSViresh Kumar 		r = PTR_ERR(opp);
28107813dd6fSViresh Kumar 		goto unlock;
28117813dd6fSViresh Kumar 	}
28127813dd6fSViresh Kumar 
28137813dd6fSViresh Kumar 	/* Is update really needed? */
28147813dd6fSViresh Kumar 	if (opp->available == availability_req)
28157813dd6fSViresh Kumar 		goto unlock;
28167813dd6fSViresh Kumar 
28177813dd6fSViresh Kumar 	opp->available = availability_req;
28187813dd6fSViresh Kumar 
28197813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
28207813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
28217813dd6fSViresh Kumar 
28227813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
28237813dd6fSViresh Kumar 	if (availability_req)
28247813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
28257813dd6fSViresh Kumar 					     opp);
28267813dd6fSViresh Kumar 	else
28277813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
28287813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
28297813dd6fSViresh Kumar 
28307813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
28317813dd6fSViresh Kumar 	goto put_table;
28327813dd6fSViresh Kumar 
28337813dd6fSViresh Kumar unlock:
28347813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
28357813dd6fSViresh Kumar put_table:
28367813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
28377813dd6fSViresh Kumar 	return r;
28387813dd6fSViresh Kumar }
28397813dd6fSViresh Kumar 
28407813dd6fSViresh Kumar /**
284125cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
284225cb20a2SStephen Boyd  * @dev:		device for which we do this operation
284325cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
284425cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
284525cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
284625cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
284725cb20a2SStephen Boyd  *
284825cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
284925cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
285025cb20a2SStephen Boyd  * successful.
285125cb20a2SStephen Boyd  */
285225cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
285325cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
285425cb20a2SStephen Boyd 			      unsigned long u_volt_max)
285525cb20a2SStephen Boyd 
285625cb20a2SStephen Boyd {
285725cb20a2SStephen Boyd 	struct opp_table *opp_table;
285825cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
285925cb20a2SStephen Boyd 	int r = 0;
286025cb20a2SStephen Boyd 
286125cb20a2SStephen Boyd 	/* Find the opp_table */
286225cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
286325cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
286425cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
286525cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
286625cb20a2SStephen Boyd 		return r;
286725cb20a2SStephen Boyd 	}
286825cb20a2SStephen Boyd 
286925cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
287025cb20a2SStephen Boyd 
287125cb20a2SStephen Boyd 	/* Do we have the frequency? */
287225cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
287325cb20a2SStephen Boyd 		if (tmp_opp->rate == freq) {
287425cb20a2SStephen Boyd 			opp = tmp_opp;
287525cb20a2SStephen Boyd 			break;
287625cb20a2SStephen Boyd 		}
287725cb20a2SStephen Boyd 	}
287825cb20a2SStephen Boyd 
287925cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
288025cb20a2SStephen Boyd 		r = PTR_ERR(opp);
288125cb20a2SStephen Boyd 		goto adjust_unlock;
288225cb20a2SStephen Boyd 	}
288325cb20a2SStephen Boyd 
288425cb20a2SStephen Boyd 	/* Is update really needed? */
288525cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
288625cb20a2SStephen Boyd 		goto adjust_unlock;
288725cb20a2SStephen Boyd 
288825cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
288925cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
289025cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
289125cb20a2SStephen Boyd 
289225cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
289325cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
289425cb20a2SStephen Boyd 
289525cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
289625cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
289725cb20a2SStephen Boyd 				     opp);
289825cb20a2SStephen Boyd 
289925cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
290025cb20a2SStephen Boyd 	goto adjust_put_table;
290125cb20a2SStephen Boyd 
290225cb20a2SStephen Boyd adjust_unlock:
290325cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
290425cb20a2SStephen Boyd adjust_put_table:
290525cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
290625cb20a2SStephen Boyd 	return r;
290725cb20a2SStephen Boyd }
290803649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
290925cb20a2SStephen Boyd 
291025cb20a2SStephen Boyd /**
29117813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
29127813dd6fSViresh Kumar  * @dev:	device for which we do this operation
29137813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
29147813dd6fSViresh Kumar  *
29157813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
29167813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
29177813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
29187813dd6fSViresh Kumar  *
29197813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
29207813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
29217813dd6fSViresh Kumar  * successful.
29227813dd6fSViresh Kumar  */
29237813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
29247813dd6fSViresh Kumar {
29257813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
29267813dd6fSViresh Kumar }
29277813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
29287813dd6fSViresh Kumar 
29297813dd6fSViresh Kumar /**
29307813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
29317813dd6fSViresh Kumar  * @dev:	device for which we do this operation
29327813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
29337813dd6fSViresh Kumar  *
29347813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
29357813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
29367813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
29377813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
29387813dd6fSViresh Kumar  *
29397813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
29407813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
29417813dd6fSViresh Kumar  * successful.
29427813dd6fSViresh Kumar  */
29437813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
29447813dd6fSViresh Kumar {
29457813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
29467813dd6fSViresh Kumar }
29477813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
29487813dd6fSViresh Kumar 
29497813dd6fSViresh Kumar /**
29507813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
29517813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
29527813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
29537813dd6fSViresh Kumar  *
29547813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
29557813dd6fSViresh Kumar  */
29567813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
29577813dd6fSViresh Kumar {
29587813dd6fSViresh Kumar 	struct opp_table *opp_table;
29597813dd6fSViresh Kumar 	int ret;
29607813dd6fSViresh Kumar 
29617813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29627813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
29637813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
29647813dd6fSViresh Kumar 
29657813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
29667813dd6fSViresh Kumar 
29677813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29687813dd6fSViresh Kumar 
29697813dd6fSViresh Kumar 	return ret;
29707813dd6fSViresh Kumar }
29717813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
29727813dd6fSViresh Kumar 
29737813dd6fSViresh Kumar /**
29747813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
29757813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
29767813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
29777813dd6fSViresh Kumar  *
29787813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
29797813dd6fSViresh Kumar  */
29807813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
29817813dd6fSViresh Kumar 				   struct notifier_block *nb)
29827813dd6fSViresh Kumar {
29837813dd6fSViresh Kumar 	struct opp_table *opp_table;
29847813dd6fSViresh Kumar 	int ret;
29857813dd6fSViresh Kumar 
29867813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29877813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
29887813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
29897813dd6fSViresh Kumar 
29907813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
29917813dd6fSViresh Kumar 
29927813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29937813dd6fSViresh Kumar 
29947813dd6fSViresh Kumar 	return ret;
29957813dd6fSViresh Kumar }
29967813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
29977813dd6fSViresh Kumar 
29988aaf6264SViresh Kumar /**
29998aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
30008aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
30018aaf6264SViresh Kumar  *
30028aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
30038aaf6264SViresh Kumar  * dynamically added entries.
30048aaf6264SViresh Kumar  */
30058aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
30067813dd6fSViresh Kumar {
30077813dd6fSViresh Kumar 	struct opp_table *opp_table;
30087813dd6fSViresh Kumar 
30097813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
30107813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
30117813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
30127813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
30137813dd6fSViresh Kumar 
30147813dd6fSViresh Kumar 		if (error != -ENODEV)
30157813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
30167813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
30177813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
30187813dd6fSViresh Kumar 			     error);
30197813dd6fSViresh Kumar 		return;
30207813dd6fSViresh Kumar 	}
30217813dd6fSViresh Kumar 
3022922ff075SViresh Kumar 	/*
3023922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
3024922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
3025922ff075SViresh Kumar 	 **/
3026922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
3027cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
3028cdd6ed90SViresh Kumar 
3029922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
30307813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
30317813dd6fSViresh Kumar }
30327813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
3033ce8073d8SDmitry Osipenko 
3034ce8073d8SDmitry Osipenko /**
3035ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
3036ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
3037ce8073d8SDmitry Osipenko  *
3038ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
3039ce8073d8SDmitry Osipenko  *
3040ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
3041ce8073d8SDmitry Osipenko  */
3042ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
3043ce8073d8SDmitry Osipenko {
3044ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
3045ce8073d8SDmitry Osipenko 	struct regulator *reg;
3046ce8073d8SDmitry Osipenko 	int i, ret = 0;
3047ce8073d8SDmitry Osipenko 
3048ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
3049ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
3050ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
3051ce8073d8SDmitry Osipenko 		return 0;
3052ce8073d8SDmitry Osipenko 
3053ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
3054ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
3055ce8073d8SDmitry Osipenko 		goto put_table;
3056ce8073d8SDmitry Osipenko 
3057ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
3058ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
3059ce8073d8SDmitry Osipenko 		goto put_table;
3060ce8073d8SDmitry Osipenko 
3061ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
3062ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
3063ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
3064ce8073d8SDmitry Osipenko 		if (ret)
3065ce8073d8SDmitry Osipenko 			break;
3066ce8073d8SDmitry Osipenko 	}
3067ce8073d8SDmitry Osipenko put_table:
3068ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
3069ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
3070ce8073d8SDmitry Osipenko 
3071ce8073d8SDmitry Osipenko 	return ret;
3072ce8073d8SDmitry Osipenko }
3073ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
3074