xref: /openbmc/linux/drivers/opp/core.c (revision 274c3e83e7d9bf4361fd30648f5c9414c806135c)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27813dd6fSViresh Kumar /*
37813dd6fSViresh Kumar  * Generic OPP Interface
47813dd6fSViresh Kumar  *
57813dd6fSViresh Kumar  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
67813dd6fSViresh Kumar  *	Nishanth Menon
77813dd6fSViresh Kumar  *	Romit Dasgupta
87813dd6fSViresh Kumar  *	Kevin Hilman
97813dd6fSViresh Kumar  */
107813dd6fSViresh Kumar 
117813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
127813dd6fSViresh Kumar 
137813dd6fSViresh Kumar #include <linux/clk.h>
147813dd6fSViresh Kumar #include <linux/errno.h>
157813dd6fSViresh Kumar #include <linux/err.h>
167813dd6fSViresh Kumar #include <linux/device.h>
177813dd6fSViresh Kumar #include <linux/export.h>
18009acd19SViresh Kumar #include <linux/pm_domain.h>
197813dd6fSViresh Kumar #include <linux/regulator/consumer.h>
2011b9b663SViresh Kumar #include <linux/slab.h>
2111b9b663SViresh Kumar #include <linux/xarray.h>
227813dd6fSViresh Kumar 
237813dd6fSViresh Kumar #include "opp.h"
247813dd6fSViresh Kumar 
257813dd6fSViresh Kumar /*
267813dd6fSViresh Kumar  * The root of the list of all opp-tables. All opp_table structures branch off
277813dd6fSViresh Kumar  * from here, with each opp_table containing the list of opps it supports in
287813dd6fSViresh Kumar  * various states of availability.
297813dd6fSViresh Kumar  */
307813dd6fSViresh Kumar LIST_HEAD(opp_tables);
317eba0c76SViresh Kumar 
327eba0c76SViresh Kumar /* OPP tables with uninitialized required OPPs */
337eba0c76SViresh Kumar LIST_HEAD(lazy_opp_tables);
347eba0c76SViresh Kumar 
357813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
367813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
3727c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */
3827c09484SViresh Kumar static bool opp_tables_busy;
397813dd6fSViresh Kumar 
4011b9b663SViresh Kumar /* OPP ID allocator */
4111b9b663SViresh Kumar static DEFINE_XARRAY_ALLOC1(opp_configs);
4211b9b663SViresh Kumar 
439e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
447813dd6fSViresh Kumar {
457813dd6fSViresh Kumar 	struct opp_device *opp_dev;
469e62edacSViresh Kumar 	bool found = false;
477813dd6fSViresh Kumar 
489e62edacSViresh Kumar 	mutex_lock(&opp_table->lock);
497813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
509e62edacSViresh Kumar 		if (opp_dev->dev == dev) {
519e62edacSViresh Kumar 			found = true;
529e62edacSViresh Kumar 			break;
539e62edacSViresh Kumar 		}
547813dd6fSViresh Kumar 
559e62edacSViresh Kumar 	mutex_unlock(&opp_table->lock);
569e62edacSViresh Kumar 	return found;
577813dd6fSViresh Kumar }
587813dd6fSViresh Kumar 
597813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
607813dd6fSViresh Kumar {
617813dd6fSViresh Kumar 	struct opp_table *opp_table;
627813dd6fSViresh Kumar 
637813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
649e62edacSViresh Kumar 		if (_find_opp_dev(dev, opp_table)) {
657813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
667813dd6fSViresh Kumar 			return opp_table;
677813dd6fSViresh Kumar 		}
687813dd6fSViresh Kumar 	}
697813dd6fSViresh Kumar 
707813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
717813dd6fSViresh Kumar }
727813dd6fSViresh Kumar 
737813dd6fSViresh Kumar /**
747813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
757813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
767813dd6fSViresh Kumar  *
777813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
787813dd6fSViresh Kumar  *
797813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
807813dd6fSViresh Kumar  * -EINVAL based on type of error.
817813dd6fSViresh Kumar  *
827813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
837813dd6fSViresh Kumar  */
847813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
857813dd6fSViresh Kumar {
867813dd6fSViresh Kumar 	struct opp_table *opp_table;
877813dd6fSViresh Kumar 
887813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
897813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
907813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
917813dd6fSViresh Kumar 	}
927813dd6fSViresh Kumar 
937813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
947813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
957813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
967813dd6fSViresh Kumar 
977813dd6fSViresh Kumar 	return opp_table;
987813dd6fSViresh Kumar }
997813dd6fSViresh Kumar 
1007813dd6fSViresh Kumar /**
1017813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
1027813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
1037813dd6fSViresh Kumar  *
1047813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
1057813dd6fSViresh Kumar  * return 0
1067813dd6fSViresh Kumar  *
1077813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1087813dd6fSViresh Kumar  */
1097813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1107813dd6fSViresh Kumar {
1117813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1127813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1137813dd6fSViresh Kumar 		return 0;
1147813dd6fSViresh Kumar 	}
1157813dd6fSViresh Kumar 
1167813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1177813dd6fSViresh Kumar }
1187813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1197813dd6fSViresh Kumar 
1207813dd6fSViresh Kumar /**
12169b1af17SViresh Kumar  * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp
12269b1af17SViresh Kumar  * @opp:	opp for which voltage has to be returned for
12369b1af17SViresh Kumar  * @supplies:	Placeholder for copying the supply information.
12469b1af17SViresh Kumar  *
12569b1af17SViresh Kumar  * Return: negative error number on failure, 0 otherwise on success after
12669b1af17SViresh Kumar  * setting @supplies.
12769b1af17SViresh Kumar  *
12869b1af17SViresh Kumar  * This can be used for devices with any number of power supplies. The caller
12969b1af17SViresh Kumar  * must ensure the @supplies array must contain space for each regulator.
13069b1af17SViresh Kumar  */
13169b1af17SViresh Kumar int dev_pm_opp_get_supplies(struct dev_pm_opp *opp,
13269b1af17SViresh Kumar 			    struct dev_pm_opp_supply *supplies)
13369b1af17SViresh Kumar {
13469b1af17SViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !supplies) {
13569b1af17SViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
13669b1af17SViresh Kumar 		return -EINVAL;
13769b1af17SViresh Kumar 	}
13869b1af17SViresh Kumar 
13969b1af17SViresh Kumar 	memcpy(supplies, opp->supplies,
14069b1af17SViresh Kumar 	       sizeof(*supplies) * opp->opp_table->regulator_count);
14169b1af17SViresh Kumar 	return 0;
14269b1af17SViresh Kumar }
14369b1af17SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_supplies);
14469b1af17SViresh Kumar 
14569b1af17SViresh Kumar /**
1464f9a7a1dSLukasz Luba  * dev_pm_opp_get_power() - Gets the power corresponding to an opp
1474f9a7a1dSLukasz Luba  * @opp:	opp for which power has to be returned for
1484f9a7a1dSLukasz Luba  *
1494f9a7a1dSLukasz Luba  * Return: power in micro watt corresponding to the opp, else
1504f9a7a1dSLukasz Luba  * return 0
1514f9a7a1dSLukasz Luba  *
1524f9a7a1dSLukasz Luba  * This is useful only for devices with single power supply.
1534f9a7a1dSLukasz Luba  */
1544f9a7a1dSLukasz Luba unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
1554f9a7a1dSLukasz Luba {
1564f9a7a1dSLukasz Luba 	unsigned long opp_power = 0;
1574f9a7a1dSLukasz Luba 	int i;
1584f9a7a1dSLukasz Luba 
1594f9a7a1dSLukasz Luba 	if (IS_ERR_OR_NULL(opp)) {
1604f9a7a1dSLukasz Luba 		pr_err("%s: Invalid parameters\n", __func__);
1614f9a7a1dSLukasz Luba 		return 0;
1624f9a7a1dSLukasz Luba 	}
1634f9a7a1dSLukasz Luba 	for (i = 0; i < opp->opp_table->regulator_count; i++)
1644f9a7a1dSLukasz Luba 		opp_power += opp->supplies[i].u_watt;
1654f9a7a1dSLukasz Luba 
1664f9a7a1dSLukasz Luba 	return opp_power;
1674f9a7a1dSLukasz Luba }
1684f9a7a1dSLukasz Luba EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
1694f9a7a1dSLukasz Luba 
1704f9a7a1dSLukasz Luba /**
1717813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1727813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1737813dd6fSViresh Kumar  *
1747813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1757813dd6fSViresh Kumar  * return 0
1767813dd6fSViresh Kumar  */
1777813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1787813dd6fSViresh Kumar {
17906a8a059SAndrew-sh.Cheng 	if (IS_ERR_OR_NULL(opp)) {
1807813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1817813dd6fSViresh Kumar 		return 0;
1827813dd6fSViresh Kumar 	}
1837813dd6fSViresh Kumar 
1842083da24SViresh Kumar 	return opp->rates[0];
1857813dd6fSViresh Kumar }
1867813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
1877813dd6fSViresh Kumar 
1887813dd6fSViresh Kumar /**
1895b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
1905b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
1915b93ac54SRajendra Nayak  *
1925b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
1935b93ac54SRajendra Nayak  * return 0.
1945b93ac54SRajendra Nayak  */
1955b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
1965b93ac54SRajendra Nayak {
1975b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1985b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
1995b93ac54SRajendra Nayak 		return 0;
2005b93ac54SRajendra Nayak 	}
2015b93ac54SRajendra Nayak 
2025b93ac54SRajendra Nayak 	return opp->level;
2035b93ac54SRajendra Nayak }
2045b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
2055b93ac54SRajendra Nayak 
2065b93ac54SRajendra Nayak /**
207597ff543SDmitry Osipenko  * dev_pm_opp_get_required_pstate() - Gets the required performance state
208597ff543SDmitry Osipenko  *                                    corresponding to an available opp
209597ff543SDmitry Osipenko  * @opp:	opp for which performance state has to be returned for
210597ff543SDmitry Osipenko  * @index:	index of the required opp
211597ff543SDmitry Osipenko  *
212597ff543SDmitry Osipenko  * Return: performance state read from device tree corresponding to the
213597ff543SDmitry Osipenko  * required opp, else return 0.
214597ff543SDmitry Osipenko  */
215597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
216597ff543SDmitry Osipenko 					    unsigned int index)
217597ff543SDmitry Osipenko {
218597ff543SDmitry Osipenko 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
219597ff543SDmitry Osipenko 	    index >= opp->opp_table->required_opp_count) {
220597ff543SDmitry Osipenko 		pr_err("%s: Invalid parameters\n", __func__);
221597ff543SDmitry Osipenko 		return 0;
222597ff543SDmitry Osipenko 	}
223597ff543SDmitry Osipenko 
2247eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
2257eba0c76SViresh Kumar 	if (lazy_linking_pending(opp->opp_table))
2267eba0c76SViresh Kumar 		return 0;
2277eba0c76SViresh Kumar 
228597ff543SDmitry Osipenko 	return opp->required_opps[index]->pstate;
229597ff543SDmitry Osipenko }
230597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
231597ff543SDmitry Osipenko 
232597ff543SDmitry Osipenko /**
2337813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
2347813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
2357813dd6fSViresh Kumar  *
2367813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
2377813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
2387813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
2397813dd6fSViresh Kumar  *
2407813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
2417813dd6fSViresh Kumar  */
2427813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
2437813dd6fSViresh Kumar {
2447813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2457813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
2467813dd6fSViresh Kumar 		return false;
2477813dd6fSViresh Kumar 	}
2487813dd6fSViresh Kumar 
2497813dd6fSViresh Kumar 	return opp->turbo;
2507813dd6fSViresh Kumar }
2517813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
2527813dd6fSViresh Kumar 
2537813dd6fSViresh Kumar /**
2547813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
2557813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2567813dd6fSViresh Kumar  *
2577813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
2587813dd6fSViresh Kumar  */
2597813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
2607813dd6fSViresh Kumar {
2617813dd6fSViresh Kumar 	struct opp_table *opp_table;
2627813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
2637813dd6fSViresh Kumar 
2647813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2657813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2667813dd6fSViresh Kumar 		return 0;
2677813dd6fSViresh Kumar 
2687813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
2697813dd6fSViresh Kumar 
2707813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2717813dd6fSViresh Kumar 
2727813dd6fSViresh Kumar 	return clock_latency_ns;
2737813dd6fSViresh Kumar }
2747813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
2757813dd6fSViresh Kumar 
2767813dd6fSViresh Kumar /**
2777813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
2787813dd6fSViresh Kumar  * @dev: device for which we do this operation
2797813dd6fSViresh Kumar  *
2807813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
2817813dd6fSViresh Kumar  */
2827813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
2837813dd6fSViresh Kumar {
2847813dd6fSViresh Kumar 	struct opp_table *opp_table;
2857813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
2867813dd6fSViresh Kumar 	struct regulator *reg;
2877813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
2887813dd6fSViresh Kumar 	int ret, i, count;
2897813dd6fSViresh Kumar 	struct {
2907813dd6fSViresh Kumar 		unsigned long min;
2917813dd6fSViresh Kumar 		unsigned long max;
2927813dd6fSViresh Kumar 	} *uV;
2937813dd6fSViresh Kumar 
2947813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2957813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2967813dd6fSViresh Kumar 		return 0;
2977813dd6fSViresh Kumar 
2987813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
29990e3577bSViresh Kumar 	if (!opp_table->regulators)
3007813dd6fSViresh Kumar 		goto put_opp_table;
3017813dd6fSViresh Kumar 
30290e3577bSViresh Kumar 	count = opp_table->regulator_count;
30390e3577bSViresh Kumar 
3047813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
3057813dd6fSViresh Kumar 	if (!uV)
3067813dd6fSViresh Kumar 		goto put_opp_table;
3077813dd6fSViresh Kumar 
3087813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
3097813dd6fSViresh Kumar 
3107813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3117813dd6fSViresh Kumar 		uV[i].min = ~0;
3127813dd6fSViresh Kumar 		uV[i].max = 0;
3137813dd6fSViresh Kumar 
3147813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
3157813dd6fSViresh Kumar 			if (!opp->available)
3167813dd6fSViresh Kumar 				continue;
3177813dd6fSViresh Kumar 
3187813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
3197813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
3207813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
3217813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
3227813dd6fSViresh Kumar 		}
3237813dd6fSViresh Kumar 	}
3247813dd6fSViresh Kumar 
3257813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
3267813dd6fSViresh Kumar 
3277813dd6fSViresh Kumar 	/*
3287813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
3297813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
3307813dd6fSViresh Kumar 	 */
3317813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3327813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
3337813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
3347813dd6fSViresh Kumar 		if (ret > 0)
3357813dd6fSViresh Kumar 			latency_ns += ret * 1000;
3367813dd6fSViresh Kumar 	}
3377813dd6fSViresh Kumar 
3387813dd6fSViresh Kumar 	kfree(uV);
3397813dd6fSViresh Kumar put_opp_table:
3407813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3417813dd6fSViresh Kumar 
3427813dd6fSViresh Kumar 	return latency_ns;
3437813dd6fSViresh Kumar }
3447813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
3457813dd6fSViresh Kumar 
3467813dd6fSViresh Kumar /**
3477813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
3487813dd6fSViresh Kumar  *					     nanoseconds
3497813dd6fSViresh Kumar  * @dev: device for which we do this operation
3507813dd6fSViresh Kumar  *
3517813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
3527813dd6fSViresh Kumar  * switch from one OPP to other.
3537813dd6fSViresh Kumar  */
3547813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
3557813dd6fSViresh Kumar {
3567813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
3577813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
3587813dd6fSViresh Kumar }
3597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
3607813dd6fSViresh Kumar 
3617813dd6fSViresh Kumar /**
3627813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
3637813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3647813dd6fSViresh Kumar  *
3657813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
3667813dd6fSViresh Kumar  * if one is available, else returns 0;
3677813dd6fSViresh Kumar  */
3687813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
3697813dd6fSViresh Kumar {
3707813dd6fSViresh Kumar 	struct opp_table *opp_table;
3717813dd6fSViresh Kumar 	unsigned long freq = 0;
3727813dd6fSViresh Kumar 
3737813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3747813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3757813dd6fSViresh Kumar 		return 0;
3767813dd6fSViresh Kumar 
3777813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
3787813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
3797813dd6fSViresh Kumar 
3807813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3817813dd6fSViresh Kumar 
3827813dd6fSViresh Kumar 	return freq;
3837813dd6fSViresh Kumar }
3847813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
3857813dd6fSViresh Kumar 
386a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
387a1e8c136SViresh Kumar {
388a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
389a1e8c136SViresh Kumar 	int count = 0;
390a1e8c136SViresh Kumar 
391a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
392a1e8c136SViresh Kumar 
393a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
394a1e8c136SViresh Kumar 		if (opp->available)
395a1e8c136SViresh Kumar 			count++;
396a1e8c136SViresh Kumar 	}
397a1e8c136SViresh Kumar 
398a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
399a1e8c136SViresh Kumar 
400a1e8c136SViresh Kumar 	return count;
401a1e8c136SViresh Kumar }
402a1e8c136SViresh Kumar 
4037813dd6fSViresh Kumar /**
4047813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
4057813dd6fSViresh Kumar  * @dev:	device for which we do this operation
4067813dd6fSViresh Kumar  *
4077813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
4087813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
4097813dd6fSViresh Kumar  */
4107813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
4117813dd6fSViresh Kumar {
4127813dd6fSViresh Kumar 	struct opp_table *opp_table;
413a1e8c136SViresh Kumar 	int count;
4147813dd6fSViresh Kumar 
4157813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4167813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
4177813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
418035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
4197813dd6fSViresh Kumar 			__func__, count);
42009f662f9SViresh Kumar 		return count;
4217813dd6fSViresh Kumar 	}
4227813dd6fSViresh Kumar 
423a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
4247813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4257813dd6fSViresh Kumar 
4267813dd6fSViresh Kumar 	return count;
4277813dd6fSViresh Kumar }
4287813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
4297813dd6fSViresh Kumar 
430aab8ced2SViresh Kumar /* Helpers to read keys */
431aab8ced2SViresh Kumar static unsigned long _read_freq(struct dev_pm_opp *opp, int index)
432aab8ced2SViresh Kumar {
4332083da24SViresh Kumar 	return opp->rates[0];
434aab8ced2SViresh Kumar }
435aab8ced2SViresh Kumar 
436c2ab2cb6SViresh Kumar static unsigned long _read_level(struct dev_pm_opp *opp, int index)
437c2ab2cb6SViresh Kumar {
438c2ab2cb6SViresh Kumar 	return opp->level;
439c2ab2cb6SViresh Kumar }
440c2ab2cb6SViresh Kumar 
441add1dc09SViresh Kumar static unsigned long _read_bw(struct dev_pm_opp *opp, int index)
442add1dc09SViresh Kumar {
443add1dc09SViresh Kumar 	return opp->bandwidth[index].peak;
444add1dc09SViresh Kumar }
445add1dc09SViresh Kumar 
446aab8ced2SViresh Kumar /* Generic comparison helpers */
447aab8ced2SViresh Kumar static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
448aab8ced2SViresh Kumar 			   unsigned long opp_key, unsigned long key)
449aab8ced2SViresh Kumar {
450aab8ced2SViresh Kumar 	if (opp_key == key) {
451aab8ced2SViresh Kumar 		*opp = temp_opp;
452aab8ced2SViresh Kumar 		return true;
453aab8ced2SViresh Kumar 	}
454aab8ced2SViresh Kumar 
455aab8ced2SViresh Kumar 	return false;
456aab8ced2SViresh Kumar }
457aab8ced2SViresh Kumar 
458aab8ced2SViresh Kumar static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
459aab8ced2SViresh Kumar 			  unsigned long opp_key, unsigned long key)
460aab8ced2SViresh Kumar {
461aab8ced2SViresh Kumar 	if (opp_key >= key) {
462aab8ced2SViresh Kumar 		*opp = temp_opp;
463aab8ced2SViresh Kumar 		return true;
464aab8ced2SViresh Kumar 	}
465aab8ced2SViresh Kumar 
466aab8ced2SViresh Kumar 	return false;
467aab8ced2SViresh Kumar }
468aab8ced2SViresh Kumar 
469aab8ced2SViresh Kumar static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
470aab8ced2SViresh Kumar 			   unsigned long opp_key, unsigned long key)
471aab8ced2SViresh Kumar {
472aab8ced2SViresh Kumar 	if (opp_key > key)
473aab8ced2SViresh Kumar 		return true;
474aab8ced2SViresh Kumar 
475aab8ced2SViresh Kumar 	*opp = temp_opp;
476aab8ced2SViresh Kumar 	return false;
477aab8ced2SViresh Kumar }
478aab8ced2SViresh Kumar 
479aab8ced2SViresh Kumar /* Generic key finding helpers */
480aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
481aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
482aab8ced2SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
483aab8ced2SViresh Kumar 		bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
484aab8ced2SViresh Kumar 				unsigned long opp_key, unsigned long key))
485aab8ced2SViresh Kumar {
486aab8ced2SViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
487aab8ced2SViresh Kumar 
488aab8ced2SViresh Kumar 	mutex_lock(&opp_table->lock);
489aab8ced2SViresh Kumar 
490aab8ced2SViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
491aab8ced2SViresh Kumar 		if (temp_opp->available == available) {
492aab8ced2SViresh Kumar 			if (compare(&opp, temp_opp, read(temp_opp, index), *key))
493aab8ced2SViresh Kumar 				break;
494aab8ced2SViresh Kumar 		}
495aab8ced2SViresh Kumar 	}
496aab8ced2SViresh Kumar 
497aab8ced2SViresh Kumar 	/* Increment the reference count of OPP */
498aab8ced2SViresh Kumar 	if (!IS_ERR(opp)) {
499aab8ced2SViresh Kumar 		*key = read(opp, index);
500aab8ced2SViresh Kumar 		dev_pm_opp_get(opp);
501aab8ced2SViresh Kumar 	}
502aab8ced2SViresh Kumar 
503aab8ced2SViresh Kumar 	mutex_unlock(&opp_table->lock);
504aab8ced2SViresh Kumar 
505aab8ced2SViresh Kumar 	return opp;
506aab8ced2SViresh Kumar }
507aab8ced2SViresh Kumar 
508aab8ced2SViresh Kumar static struct dev_pm_opp *
509aab8ced2SViresh Kumar _find_key(struct device *dev, unsigned long *key, int index, bool available,
510aab8ced2SViresh Kumar 	  unsigned long (*read)(struct dev_pm_opp *opp, int index),
511aab8ced2SViresh Kumar 	  bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
512aab8ced2SViresh Kumar 			  unsigned long opp_key, unsigned long key))
513aab8ced2SViresh Kumar {
514aab8ced2SViresh Kumar 	struct opp_table *opp_table;
515aab8ced2SViresh Kumar 	struct dev_pm_opp *opp;
516aab8ced2SViresh Kumar 
517aab8ced2SViresh Kumar 	opp_table = _find_opp_table(dev);
518aab8ced2SViresh Kumar 	if (IS_ERR(opp_table)) {
519aab8ced2SViresh Kumar 		dev_err(dev, "%s: OPP table not found (%ld)\n", __func__,
520aab8ced2SViresh Kumar 			PTR_ERR(opp_table));
521aab8ced2SViresh Kumar 		return ERR_CAST(opp_table);
522aab8ced2SViresh Kumar 	}
523aab8ced2SViresh Kumar 
524aab8ced2SViresh Kumar 	opp = _opp_table_find_key(opp_table, key, index, available, read,
525aab8ced2SViresh Kumar 				  compare);
526aab8ced2SViresh Kumar 
527aab8ced2SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
528aab8ced2SViresh Kumar 
529aab8ced2SViresh Kumar 	return opp;
530aab8ced2SViresh Kumar }
531aab8ced2SViresh Kumar 
532aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_exact(struct device *dev,
533aab8ced2SViresh Kumar 		unsigned long key, int index, bool available,
534aab8ced2SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index))
535aab8ced2SViresh Kumar {
536aab8ced2SViresh Kumar 	/*
537aab8ced2SViresh Kumar 	 * The value of key will be updated here, but will be ignored as the
538aab8ced2SViresh Kumar 	 * caller doesn't need it.
539aab8ced2SViresh Kumar 	 */
540aab8ced2SViresh Kumar 	return _find_key(dev, &key, index, available, read, _compare_exact);
541aab8ced2SViresh Kumar }
542aab8ced2SViresh Kumar 
543aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
544aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
545aab8ced2SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index))
546aab8ced2SViresh Kumar {
547aab8ced2SViresh Kumar 	return _opp_table_find_key(opp_table, key, index, available, read,
548aab8ced2SViresh Kumar 				   _compare_ceil);
549aab8ced2SViresh Kumar }
550aab8ced2SViresh Kumar 
551aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
552aab8ced2SViresh Kumar 		int index, bool available,
553aab8ced2SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index))
554aab8ced2SViresh Kumar {
555aab8ced2SViresh Kumar 	return _find_key(dev, key, index, available, read, _compare_ceil);
556aab8ced2SViresh Kumar }
557aab8ced2SViresh Kumar 
558aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_floor(struct device *dev,
559aab8ced2SViresh Kumar 		unsigned long *key, int index, bool available,
560aab8ced2SViresh Kumar 		unsigned long (*read)(struct dev_pm_opp *opp, int index))
561aab8ced2SViresh Kumar {
562aab8ced2SViresh Kumar 	return _find_key(dev, key, index, available, read, _compare_floor);
563aab8ced2SViresh Kumar }
564aab8ced2SViresh Kumar 
5657813dd6fSViresh Kumar /**
5667813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
5677813dd6fSViresh Kumar  * @dev:		device for which we do this operation
5687813dd6fSViresh Kumar  * @freq:		frequency to search for
5697813dd6fSViresh Kumar  * @available:		true/false - match for available opp
5707813dd6fSViresh Kumar  *
5717813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
5727813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
5737813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
5747813dd6fSViresh Kumar  * EINVAL:	for bad pointer
5757813dd6fSViresh Kumar  * ERANGE:	no match found for search
5767813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
5777813dd6fSViresh Kumar  *
5787813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
5797813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
5807813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
5817813dd6fSViresh Kumar  *
5827813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
5837813dd6fSViresh Kumar  * or the opposite as well.
5847813dd6fSViresh Kumar  *
5857813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5867813dd6fSViresh Kumar  * use.
5877813dd6fSViresh Kumar  */
5887813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
589aab8ced2SViresh Kumar 		unsigned long freq, bool available)
5907813dd6fSViresh Kumar {
591aab8ced2SViresh Kumar 	return _find_key_exact(dev, freq, 0, available, _read_freq);
5927813dd6fSViresh Kumar }
5937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
5947813dd6fSViresh Kumar 
5957813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
5967813dd6fSViresh Kumar 						   unsigned long *freq)
5977813dd6fSViresh Kumar {
598aab8ced2SViresh Kumar 	return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq);
5997813dd6fSViresh Kumar }
6007813dd6fSViresh Kumar 
6017813dd6fSViresh Kumar /**
6027813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
6037813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6047813dd6fSViresh Kumar  * @freq:	Start frequency
6057813dd6fSViresh Kumar  *
6067813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
6077813dd6fSViresh Kumar  * for a device.
6087813dd6fSViresh Kumar  *
6097813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6107813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6117813dd6fSViresh Kumar  * values can be:
6127813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6137813dd6fSViresh Kumar  * ERANGE:	no match found for search
6147813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6157813dd6fSViresh Kumar  *
6167813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6177813dd6fSViresh Kumar  * use.
6187813dd6fSViresh Kumar  */
6197813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
6207813dd6fSViresh Kumar 					     unsigned long *freq)
6217813dd6fSViresh Kumar {
622aab8ced2SViresh Kumar 	return _find_key_ceil(dev, freq, 0, true, _read_freq);
6237813dd6fSViresh Kumar }
6247813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
6257813dd6fSViresh Kumar 
6267813dd6fSViresh Kumar /**
6277813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
6287813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6297813dd6fSViresh Kumar  * @freq:	Start frequency
6307813dd6fSViresh Kumar  *
6317813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
6327813dd6fSViresh Kumar  * for a device.
6337813dd6fSViresh Kumar  *
6347813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6357813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6367813dd6fSViresh Kumar  * values can be:
6377813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6387813dd6fSViresh Kumar  * ERANGE:	no match found for search
6397813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6407813dd6fSViresh Kumar  *
6417813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6427813dd6fSViresh Kumar  * use.
6437813dd6fSViresh Kumar  */
6447813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
6457813dd6fSViresh Kumar 					      unsigned long *freq)
6467813dd6fSViresh Kumar {
647aab8ced2SViresh Kumar 	return _find_key_floor(dev, freq, 0, true, _read_freq);
6487813dd6fSViresh Kumar }
6497813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
6507813dd6fSViresh Kumar 
6512f36bde0SAndrew-sh.Cheng /**
65222079af7SViresh Kumar  * dev_pm_opp_find_level_exact() - search for an exact level
65322079af7SViresh Kumar  * @dev:		device for which we do this operation
65422079af7SViresh Kumar  * @level:		level to search for
65522079af7SViresh Kumar  *
65622079af7SViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
65722079af7SViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
65822079af7SViresh Kumar  * be handled using IS_ERR. Error return values can be:
65922079af7SViresh Kumar  * EINVAL:	for bad pointer
66022079af7SViresh Kumar  * ERANGE:	no match found for search
66122079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
66222079af7SViresh Kumar  *
66322079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
66422079af7SViresh Kumar  * use.
66522079af7SViresh Kumar  */
66622079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
66722079af7SViresh Kumar 					       unsigned int level)
66822079af7SViresh Kumar {
669c2ab2cb6SViresh Kumar 	return _find_key_exact(dev, level, 0, true, _read_level);
67022079af7SViresh Kumar }
67122079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
67222079af7SViresh Kumar 
67322079af7SViresh Kumar /**
67422079af7SViresh Kumar  * dev_pm_opp_find_level_ceil() - search for an rounded up level
67522079af7SViresh Kumar  * @dev:		device for which we do this operation
67622079af7SViresh Kumar  * @level:		level to search for
67722079af7SViresh Kumar  *
67822079af7SViresh Kumar  * Return: Searches for rounded up match in the opp table and returns pointer
67922079af7SViresh Kumar  * to the  matching opp if found, else returns ERR_PTR in case of error and
68022079af7SViresh Kumar  * should be handled using IS_ERR. Error return values can be:
68122079af7SViresh Kumar  * EINVAL:	for bad pointer
68222079af7SViresh Kumar  * ERANGE:	no match found for search
68322079af7SViresh Kumar  * ENODEV:	if device not found in list of registered devices
68422079af7SViresh Kumar  *
68522079af7SViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
68622079af7SViresh Kumar  * use.
68722079af7SViresh Kumar  */
68822079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
68922079af7SViresh Kumar 					      unsigned int *level)
69022079af7SViresh Kumar {
691c2ab2cb6SViresh Kumar 	unsigned long temp = *level;
692c2ab2cb6SViresh Kumar 	struct dev_pm_opp *opp;
69322079af7SViresh Kumar 
694c2ab2cb6SViresh Kumar 	opp = _find_key_ceil(dev, &temp, 0, true, _read_level);
695c2ab2cb6SViresh Kumar 	*level = temp;
69622079af7SViresh Kumar 	return opp;
69722079af7SViresh Kumar }
69822079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
69922079af7SViresh Kumar 
70022079af7SViresh Kumar /**
70100ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
70200ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
703617df304SYang Li  * @bw:	start bandwidth
70400ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
70500ce3873SKrzysztof Kozlowski  *
70600ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
70700ce3873SKrzysztof Kozlowski  * for a device.
70800ce3873SKrzysztof Kozlowski  *
70900ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
71000ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
71100ce3873SKrzysztof Kozlowski  * values can be:
71200ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
71300ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
71400ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
71500ce3873SKrzysztof Kozlowski  *
71600ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
71700ce3873SKrzysztof Kozlowski  * use.
71800ce3873SKrzysztof Kozlowski  */
719add1dc09SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
720add1dc09SViresh Kumar 					   int index)
72100ce3873SKrzysztof Kozlowski {
722add1dc09SViresh Kumar 	unsigned long temp = *bw;
723add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
72400ce3873SKrzysztof Kozlowski 
725add1dc09SViresh Kumar 	opp = _find_key_ceil(dev, &temp, index, true, _read_bw);
726add1dc09SViresh Kumar 	*bw = temp;
72700ce3873SKrzysztof Kozlowski 	return opp;
72800ce3873SKrzysztof Kozlowski }
72900ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
73000ce3873SKrzysztof Kozlowski 
73100ce3873SKrzysztof Kozlowski /**
73200ce3873SKrzysztof Kozlowski  * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
73300ce3873SKrzysztof Kozlowski  * @dev:	device for which we do this operation
734617df304SYang Li  * @bw:	start bandwidth
73500ce3873SKrzysztof Kozlowski  * @index:	which bandwidth to compare, in case of OPPs with several values
73600ce3873SKrzysztof Kozlowski  *
73700ce3873SKrzysztof Kozlowski  * Search for the matching floor *available* OPP from a starting bandwidth
73800ce3873SKrzysztof Kozlowski  * for a device.
73900ce3873SKrzysztof Kozlowski  *
74000ce3873SKrzysztof Kozlowski  * Return: matching *opp and refreshes *bw accordingly, else returns
74100ce3873SKrzysztof Kozlowski  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
74200ce3873SKrzysztof Kozlowski  * values can be:
74300ce3873SKrzysztof Kozlowski  * EINVAL:	for bad pointer
74400ce3873SKrzysztof Kozlowski  * ERANGE:	no match found for search
74500ce3873SKrzysztof Kozlowski  * ENODEV:	if device not found in list of registered devices
74600ce3873SKrzysztof Kozlowski  *
74700ce3873SKrzysztof Kozlowski  * The callers are required to call dev_pm_opp_put() for the returned OPP after
74800ce3873SKrzysztof Kozlowski  * use.
74900ce3873SKrzysztof Kozlowski  */
75000ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
75100ce3873SKrzysztof Kozlowski 					    unsigned int *bw, int index)
75200ce3873SKrzysztof Kozlowski {
753add1dc09SViresh Kumar 	unsigned long temp = *bw;
754add1dc09SViresh Kumar 	struct dev_pm_opp *opp;
75500ce3873SKrzysztof Kozlowski 
756add1dc09SViresh Kumar 	opp = _find_key_floor(dev, &temp, index, true, _read_bw);
757add1dc09SViresh Kumar 	*bw = temp;
75800ce3873SKrzysztof Kozlowski 	return opp;
75900ce3873SKrzysztof Kozlowski }
76000ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
76100ce3873SKrzysztof Kozlowski 
7627813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
7637813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
7647813dd6fSViresh Kumar {
7657813dd6fSViresh Kumar 	int ret;
7667813dd6fSViresh Kumar 
7677813dd6fSViresh Kumar 	/* Regulator not available for device */
7687813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
7697813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
7707813dd6fSViresh Kumar 			PTR_ERR(reg));
7717813dd6fSViresh Kumar 		return 0;
7727813dd6fSViresh Kumar 	}
7737813dd6fSViresh Kumar 
7747813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
7757813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
7767813dd6fSViresh Kumar 
7777813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
7787813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
7797813dd6fSViresh Kumar 	if (ret)
7807813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
7817813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
7827813dd6fSViresh Kumar 			supply->u_volt_max, ret);
7837813dd6fSViresh Kumar 
7847813dd6fSViresh Kumar 	return ret;
7857813dd6fSViresh Kumar }
7867813dd6fSViresh Kumar 
7872083da24SViresh Kumar static int
7882083da24SViresh Kumar _opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
7892083da24SViresh Kumar 		       struct dev_pm_opp *opp, void *data, bool scaling_down)
7907813dd6fSViresh Kumar {
7911efae8d2SViresh Kumar 	unsigned long *target = data;
7921efae8d2SViresh Kumar 	unsigned long freq;
7937813dd6fSViresh Kumar 	int ret;
7947813dd6fSViresh Kumar 
7951efae8d2SViresh Kumar 	/* One of target and opp must be available */
7961efae8d2SViresh Kumar 	if (target) {
7971efae8d2SViresh Kumar 		freq = *target;
7981efae8d2SViresh Kumar 	} else if (opp) {
7992083da24SViresh Kumar 		freq = opp->rates[0];
8001efae8d2SViresh Kumar 	} else {
8011efae8d2SViresh Kumar 		WARN_ON(1);
8021efae8d2SViresh Kumar 		return -EINVAL;
8031efae8d2SViresh Kumar 	}
8041efae8d2SViresh Kumar 
8051efae8d2SViresh Kumar 	ret = clk_set_rate(opp_table->clk, freq);
8067813dd6fSViresh Kumar 	if (ret) {
8077813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
8087813dd6fSViresh Kumar 			ret);
8091efae8d2SViresh Kumar 	} else {
8101efae8d2SViresh Kumar 		opp_table->rate_clk_single = freq;
8117813dd6fSViresh Kumar 	}
8127813dd6fSViresh Kumar 
8137813dd6fSViresh Kumar 	return ret;
8147813dd6fSViresh Kumar }
8157813dd6fSViresh Kumar 
816c522ce8aSViresh Kumar static int _opp_config_regulator_single(struct device *dev,
817c522ce8aSViresh Kumar 			struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
818c522ce8aSViresh Kumar 			struct regulator **regulators, unsigned int count)
8197813dd6fSViresh Kumar {
820c522ce8aSViresh Kumar 	struct regulator *reg = regulators[0];
8217813dd6fSViresh Kumar 	int ret;
8227813dd6fSViresh Kumar 
8237813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
824c522ce8aSViresh Kumar 	if (WARN_ON(count > 1)) {
8257813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
8267813dd6fSViresh Kumar 		return -EINVAL;
8277813dd6fSViresh Kumar 	}
8287813dd6fSViresh Kumar 
829c522ce8aSViresh Kumar 	ret = _set_opp_voltage(dev, reg, new_opp->supplies);
8307813dd6fSViresh Kumar 	if (ret)
831c522ce8aSViresh Kumar 		return ret;
8327813dd6fSViresh Kumar 
8338d45719cSKamil Konieczny 	/*
8348d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
8358d45719cSKamil Konieczny 	 * some boot-enabled regulators.
8368d45719cSKamil Konieczny 	 */
837c522ce8aSViresh Kumar 	if (unlikely(!new_opp->opp_table->enabled)) {
8388d45719cSKamil Konieczny 		ret = regulator_enable(reg);
8398d45719cSKamil Konieczny 		if (ret < 0)
8408d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
8418d45719cSKamil Konieczny 	}
8428d45719cSKamil Konieczny 
8437813dd6fSViresh Kumar 	return 0;
8447813dd6fSViresh Kumar }
8457813dd6fSViresh Kumar 
846b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
847240ae50eSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev)
848b00e667aSViresh Kumar {
849b00e667aSViresh Kumar 	u32 avg, peak;
850b00e667aSViresh Kumar 	int i, ret;
851b00e667aSViresh Kumar 
852b00e667aSViresh Kumar 	if (!opp_table->paths)
853b00e667aSViresh Kumar 		return 0;
854b00e667aSViresh Kumar 
855b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
856240ae50eSViresh Kumar 		if (!opp) {
857b00e667aSViresh Kumar 			avg = 0;
858b00e667aSViresh Kumar 			peak = 0;
859b00e667aSViresh Kumar 		} else {
860b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
861b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
862b00e667aSViresh Kumar 		}
863b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
864b00e667aSViresh Kumar 		if (ret) {
865b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
866240ae50eSViresh Kumar 				opp ? "set" : "remove", i, ret);
867b00e667aSViresh Kumar 			return ret;
868b00e667aSViresh Kumar 		}
869b00e667aSViresh Kumar 	}
870b00e667aSViresh Kumar 
871b00e667aSViresh Kumar 	return 0;
872b00e667aSViresh Kumar }
873b00e667aSViresh Kumar 
87460cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev,
87560cdeae0SStephan Gerhold 			     struct dev_pm_opp *opp, int i)
87660cdeae0SStephan Gerhold {
87760cdeae0SStephan Gerhold 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
87860cdeae0SStephan Gerhold 	int ret;
87960cdeae0SStephan Gerhold 
88060cdeae0SStephan Gerhold 	if (!pd_dev)
88160cdeae0SStephan Gerhold 		return 0;
88260cdeae0SStephan Gerhold 
88360cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
88460cdeae0SStephan Gerhold 	if (ret) {
8859bfb1fffSViresh Kumar 		dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
88660cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
88760cdeae0SStephan Gerhold 	}
88860cdeae0SStephan Gerhold 
88960cdeae0SStephan Gerhold 	return ret;
89060cdeae0SStephan Gerhold }
89160cdeae0SStephan Gerhold 
892ca1b5d77SViresh Kumar /* This is only called for PM domain for now */
893ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev,
894ca1b5d77SViresh Kumar 			      struct opp_table *opp_table,
8952c59138cSStephan Gerhold 			      struct dev_pm_opp *opp, bool up)
896ca1b5d77SViresh Kumar {
897ca1b5d77SViresh Kumar 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
898ca1b5d77SViresh Kumar 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
899ca1b5d77SViresh Kumar 	int i, ret = 0;
900ca1b5d77SViresh Kumar 
901ca1b5d77SViresh Kumar 	if (!required_opp_tables)
902ca1b5d77SViresh Kumar 		return 0;
903ca1b5d77SViresh Kumar 
90419526d09SMarijn Suijten 	/* required-opps not fully initialized yet */
90519526d09SMarijn Suijten 	if (lazy_linking_pending(opp_table))
90619526d09SMarijn Suijten 		return -EBUSY;
90719526d09SMarijn Suijten 
9084fa82a87SHsin-Yi Wang 	/*
9094fa82a87SHsin-Yi Wang 	 * We only support genpd's OPPs in the "required-opps" for now, as we
9104fa82a87SHsin-Yi Wang 	 * don't know much about other use cases. Error out if the required OPP
9114fa82a87SHsin-Yi Wang 	 * doesn't belong to a genpd.
9124fa82a87SHsin-Yi Wang 	 */
9134fa82a87SHsin-Yi Wang 	if (unlikely(!required_opp_tables[0]->is_genpd)) {
9144fa82a87SHsin-Yi Wang 		dev_err(dev, "required-opps don't belong to a genpd\n");
9154fa82a87SHsin-Yi Wang 		return -ENOENT;
9164fa82a87SHsin-Yi Wang 	}
9174fa82a87SHsin-Yi Wang 
918ca1b5d77SViresh Kumar 	/* Single genpd case */
91960cdeae0SStephan Gerhold 	if (!genpd_virt_devs)
92060cdeae0SStephan Gerhold 		return _set_required_opp(dev, dev, opp, 0);
921ca1b5d77SViresh Kumar 
922ca1b5d77SViresh Kumar 	/* Multiple genpd case */
923ca1b5d77SViresh Kumar 
924ca1b5d77SViresh Kumar 	/*
925ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
926ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
927ca1b5d77SViresh Kumar 	 */
928ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
929ca1b5d77SViresh Kumar 
9302c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
9312c59138cSStephan Gerhold 	if (up) {
932ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
93360cdeae0SStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
93460cdeae0SStephan Gerhold 			if (ret)
935ca1b5d77SViresh Kumar 				break;
936ca1b5d77SViresh Kumar 		}
9372c59138cSStephan Gerhold 	} else {
9382c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
9392c59138cSStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
9402c59138cSStephan Gerhold 			if (ret)
941ca1b5d77SViresh Kumar 				break;
942ca1b5d77SViresh Kumar 		}
943ca1b5d77SViresh Kumar 	}
9442c59138cSStephan Gerhold 
945ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
946ca1b5d77SViresh Kumar 
947ca1b5d77SViresh Kumar 	return ret;
948ca1b5d77SViresh Kumar }
949ca1b5d77SViresh Kumar 
95081c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
95181c4d8a3SViresh Kumar {
95281c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
95381c4d8a3SViresh Kumar 	unsigned long freq;
95481c4d8a3SViresh Kumar 
95581c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
95681c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
95781c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
95881c4d8a3SViresh Kumar 	}
95981c4d8a3SViresh Kumar 
96081c4d8a3SViresh Kumar 	/*
96181c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
96281c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
96381c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
96481c4d8a3SViresh Kumar 	 */
96581c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
96681c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
96781c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
96881c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
96981c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
97081c4d8a3SViresh Kumar 	}
97181c4d8a3SViresh Kumar 
97281c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
97381c4d8a3SViresh Kumar }
97481c4d8a3SViresh Kumar 
9755ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
976f3364e17SViresh Kumar {
977f3364e17SViresh Kumar 	int ret;
978f3364e17SViresh Kumar 
979f3364e17SViresh Kumar 	if (!opp_table->enabled)
980f3364e17SViresh Kumar 		return 0;
981f3364e17SViresh Kumar 
982f3364e17SViresh Kumar 	/*
983f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
984f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
985f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
986f3364e17SViresh Kumar 	 */
987f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
988f3364e17SViresh Kumar 		return 0;
989f3364e17SViresh Kumar 
990240ae50eSViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev);
991f3364e17SViresh Kumar 	if (ret)
992f3364e17SViresh Kumar 		return ret;
993f3364e17SViresh Kumar 
994f3364e17SViresh Kumar 	if (opp_table->regulators)
995f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
996f3364e17SViresh Kumar 
9972c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
998f3364e17SViresh Kumar 
999f3364e17SViresh Kumar 	opp_table->enabled = false;
1000f3364e17SViresh Kumar 	return ret;
1001f3364e17SViresh Kumar }
1002f3364e17SViresh Kumar 
1003386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
10041efae8d2SViresh Kumar 		    struct dev_pm_opp *opp, void *clk_data, bool forced)
10057813dd6fSViresh Kumar {
1006386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
1007f0b88fa4SViresh Kumar 	int scaling_down, ret;
10087813dd6fSViresh Kumar 
1009386ba854SViresh Kumar 	if (unlikely(!opp))
1010386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
1011aca48b61SRajendra Nayak 
101281c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
101381c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
101481c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
10157813dd6fSViresh Kumar 
101681c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
101781c4d8a3SViresh Kumar 
101881c4d8a3SViresh Kumar 	/* Return early if nothing to do */
10191efae8d2SViresh Kumar 	if (!forced && old_opp == opp && opp_table->enabled) {
102081c4d8a3SViresh Kumar 		dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
1021386ba854SViresh Kumar 		return 0;
10227813dd6fSViresh Kumar 	}
10237813dd6fSViresh Kumar 
1024f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
10252083da24SViresh Kumar 		__func__, old_opp->rates[0], opp->rates[0], old_opp->level,
10262083da24SViresh Kumar 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1027f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1028f0b88fa4SViresh Kumar 
10292083da24SViresh Kumar 	scaling_down = _opp_compare_key(opp_table, old_opp, opp);
1030f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1031f0b88fa4SViresh Kumar 		scaling_down = 0;
10327813dd6fSViresh Kumar 
1033ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1034f0b88fa4SViresh Kumar 	if (!scaling_down) {
10352c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1036870d5d96SViresh Kumar 		if (ret) {
1037870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1038386ba854SViresh Kumar 			return ret;
1039ca1b5d77SViresh Kumar 		}
1040ca1b5d77SViresh Kumar 
1041870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1042870d5d96SViresh Kumar 		if (ret) {
1043870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1044870d5d96SViresh Kumar 			return ret;
1045870d5d96SViresh Kumar 		}
1046aee3352fSViresh Kumar 
1047aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1048aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1049aee3352fSViresh Kumar 							   opp_table->regulators,
1050aee3352fSViresh Kumar 							   opp_table->regulator_count);
1051aee3352fSViresh Kumar 			if (ret) {
1052aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1053aee3352fSViresh Kumar 					ret);
1054aee3352fSViresh Kumar 				return ret;
1055aee3352fSViresh Kumar 			}
1056aee3352fSViresh Kumar 		}
1057870d5d96SViresh Kumar 	}
1058870d5d96SViresh Kumar 
10592083da24SViresh Kumar 	if (opp_table->config_clks) {
10602083da24SViresh Kumar 		ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
1061ca1b5d77SViresh Kumar 		if (ret)
1062870d5d96SViresh Kumar 			return ret;
10632083da24SViresh Kumar 	}
1064870d5d96SViresh Kumar 
1065870d5d96SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1066870d5d96SViresh Kumar 	if (scaling_down) {
1067aee3352fSViresh Kumar 		if (opp_table->config_regulators) {
1068aee3352fSViresh Kumar 			ret = opp_table->config_regulators(dev, old_opp, opp,
1069aee3352fSViresh Kumar 							   opp_table->regulators,
1070aee3352fSViresh Kumar 							   opp_table->regulator_count);
1071aee3352fSViresh Kumar 			if (ret) {
1072aee3352fSViresh Kumar 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1073aee3352fSViresh Kumar 					ret);
1074aee3352fSViresh Kumar 				return ret;
1075aee3352fSViresh Kumar 			}
1076aee3352fSViresh Kumar 		}
1077aee3352fSViresh Kumar 
1078870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1079870d5d96SViresh Kumar 		if (ret) {
1080870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1081870d5d96SViresh Kumar 			return ret;
1082ca1b5d77SViresh Kumar 		}
1083ca1b5d77SViresh Kumar 
1084870d5d96SViresh Kumar 		ret = _set_required_opps(dev, opp_table, opp, false);
1085870d5d96SViresh Kumar 		if (ret) {
1086870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1087870d5d96SViresh Kumar 			return ret;
1088870d5d96SViresh Kumar 		}
1089870d5d96SViresh Kumar 	}
1090870d5d96SViresh Kumar 
109172f80ce4SViresh Kumar 	opp_table->enabled = true;
109281c4d8a3SViresh Kumar 	dev_pm_opp_put(old_opp);
109381c4d8a3SViresh Kumar 
109481c4d8a3SViresh Kumar 	/* Make sure current_opp doesn't get freed */
109581c4d8a3SViresh Kumar 	dev_pm_opp_get(opp);
109681c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
1097fe2af402SGeorgi Djakov 
1098386ba854SViresh Kumar 	return ret;
1099386ba854SViresh Kumar }
1100386ba854SViresh Kumar 
1101386ba854SViresh Kumar /**
1102386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1103386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1104386ba854SViresh Kumar  * @target_freq: frequency to achieve
1105386ba854SViresh Kumar  *
1106386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1107386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1108386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1109386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1110386ba854SViresh Kumar  * frequency.
1111386ba854SViresh Kumar  */
1112386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1113386ba854SViresh Kumar {
1114386ba854SViresh Kumar 	struct opp_table *opp_table;
1115386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1116386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
11171efae8d2SViresh Kumar 	bool forced = false;
1118386ba854SViresh Kumar 	int ret;
1119386ba854SViresh Kumar 
1120386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1121386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1122386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1123386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1124386ba854SViresh Kumar 	}
1125386ba854SViresh Kumar 
1126386ba854SViresh Kumar 	if (target_freq) {
1127386ba854SViresh Kumar 		/*
1128386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1129386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1130386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1131386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1132386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1133386ba854SViresh Kumar 		 */
1134386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
11352083da24SViresh Kumar 			ret = opp_table->config_clks(dev, opp_table, NULL,
11362083da24SViresh Kumar 						     &target_freq, false);
1137386ba854SViresh Kumar 			goto put_opp_table;
1138386ba854SViresh Kumar 		}
1139386ba854SViresh Kumar 
1140386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1141386ba854SViresh Kumar 		if ((long)freq <= 0)
1142386ba854SViresh Kumar 			freq = target_freq;
1143386ba854SViresh Kumar 
1144386ba854SViresh Kumar 		/*
1145386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1146386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1147386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1148386ba854SViresh Kumar 		 */
1149386ba854SViresh Kumar 		temp_freq = freq;
1150386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1151386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1152386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1153386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1154386ba854SViresh Kumar 				__func__, freq, ret);
1155386ba854SViresh Kumar 			goto put_opp_table;
1156386ba854SViresh Kumar 		}
11571efae8d2SViresh Kumar 
11581efae8d2SViresh Kumar 		/*
11591efae8d2SViresh Kumar 		 * An OPP entry specifies the highest frequency at which other
11601efae8d2SViresh Kumar 		 * properties of the OPP entry apply. Even if the new OPP is
11611efae8d2SViresh Kumar 		 * same as the old one, we may still reach here for a different
11621efae8d2SViresh Kumar 		 * value of the frequency. In such a case, do not abort but
11631efae8d2SViresh Kumar 		 * configure the hardware to the desired frequency forcefully.
11641efae8d2SViresh Kumar 		 */
11651efae8d2SViresh Kumar 		forced = opp_table->rate_clk_single != target_freq;
1166386ba854SViresh Kumar 	}
1167386ba854SViresh Kumar 
11681efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, &target_freq, forced);
1169386ba854SViresh Kumar 
1170386ba854SViresh Kumar 	if (target_freq)
11717813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
11721efae8d2SViresh Kumar 
11737813dd6fSViresh Kumar put_opp_table:
11747813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
11757813dd6fSViresh Kumar 	return ret;
11767813dd6fSViresh Kumar }
11777813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
11787813dd6fSViresh Kumar 
1179abbe3483SViresh Kumar /**
1180abbe3483SViresh Kumar  * dev_pm_opp_set_opp() - Configure device for OPP
1181abbe3483SViresh Kumar  * @dev: device for which we do this operation
1182abbe3483SViresh Kumar  * @opp: OPP to set to
1183abbe3483SViresh Kumar  *
1184abbe3483SViresh Kumar  * This configures the device based on the properties of the OPP passed to this
1185abbe3483SViresh Kumar  * routine.
1186abbe3483SViresh Kumar  *
1187abbe3483SViresh Kumar  * Return: 0 on success, a negative error number otherwise.
1188abbe3483SViresh Kumar  */
1189abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1190abbe3483SViresh Kumar {
1191abbe3483SViresh Kumar 	struct opp_table *opp_table;
1192abbe3483SViresh Kumar 	int ret;
1193abbe3483SViresh Kumar 
1194abbe3483SViresh Kumar 	opp_table = _find_opp_table(dev);
1195abbe3483SViresh Kumar 	if (IS_ERR(opp_table)) {
1196abbe3483SViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1197abbe3483SViresh Kumar 		return PTR_ERR(opp_table);
1198abbe3483SViresh Kumar 	}
1199abbe3483SViresh Kumar 
12001efae8d2SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, NULL, false);
1201abbe3483SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1202abbe3483SViresh Kumar 
1203abbe3483SViresh Kumar 	return ret;
1204abbe3483SViresh Kumar }
1205abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1206abbe3483SViresh Kumar 
12077813dd6fSViresh Kumar /* OPP-dev Helpers */
12087813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
12097813dd6fSViresh Kumar 			    struct opp_table *opp_table)
12107813dd6fSViresh Kumar {
12117813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
12127813dd6fSViresh Kumar 	list_del(&opp_dev->node);
12137813dd6fSViresh Kumar 	kfree(opp_dev);
12147813dd6fSViresh Kumar }
12157813dd6fSViresh Kumar 
1216ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
12177813dd6fSViresh Kumar 				struct opp_table *opp_table)
12187813dd6fSViresh Kumar {
12197813dd6fSViresh Kumar 	struct opp_device *opp_dev;
12207813dd6fSViresh Kumar 
12217813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
12227813dd6fSViresh Kumar 	if (!opp_dev)
12237813dd6fSViresh Kumar 		return NULL;
12247813dd6fSViresh Kumar 
12257813dd6fSViresh Kumar 	/* Initialize opp-dev */
12267813dd6fSViresh Kumar 	opp_dev->dev = dev;
12273d255699SViresh Kumar 
1228ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
12297813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1230ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
12317813dd6fSViresh Kumar 
12327813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1233a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1234283d55e6SViresh Kumar 
1235283d55e6SViresh Kumar 	return opp_dev;
1236283d55e6SViresh Kumar }
1237283d55e6SViresh Kumar 
1238eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
12397813dd6fSViresh Kumar {
12407813dd6fSViresh Kumar 	struct opp_table *opp_table;
12417813dd6fSViresh Kumar 	struct opp_device *opp_dev;
12427813dd6fSViresh Kumar 	int ret;
12437813dd6fSViresh Kumar 
12447813dd6fSViresh Kumar 	/*
12457813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
12467813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
12477813dd6fSViresh Kumar 	 */
12487813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
12497813dd6fSViresh Kumar 	if (!opp_table)
1250dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
12517813dd6fSViresh Kumar 
12523d255699SViresh Kumar 	mutex_init(&opp_table->lock);
12534f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
12547813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
12557eba0c76SViresh Kumar 	INIT_LIST_HEAD(&opp_table->lazy);
12567813dd6fSViresh Kumar 
12572083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
12582083da24SViresh Kumar 
125946f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
126046f48acaSViresh Kumar 	opp_table->regulator_count = -1;
126146f48acaSViresh Kumar 
12627813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
12637813dd6fSViresh Kumar 	if (!opp_dev) {
1264dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1265dd461cd9SStephan Gerhold 		goto err;
12667813dd6fSViresh Kumar 	}
12677813dd6fSViresh Kumar 
1268eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
12697813dd6fSViresh Kumar 
12706d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
12716d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1272dd461cd9SStephan Gerhold 	if (ret) {
1273dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
127432439ac7SViresh Kumar 			goto remove_opp_dev;
1275dd461cd9SStephan Gerhold 
12766d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
12776d3f922cSGeorgi Djakov 			 __func__, ret);
1278dd461cd9SStephan Gerhold 	}
12796d3f922cSGeorgi Djakov 
12807813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
12817813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
12827813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
12837813dd6fSViresh Kumar 
12847813dd6fSViresh Kumar 	return opp_table;
1285dd461cd9SStephan Gerhold 
1286976509bbSQuanyang Wang remove_opp_dev:
1287976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1288dd461cd9SStephan Gerhold err:
1289dd461cd9SStephan Gerhold 	kfree(opp_table);
1290dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
12917813dd6fSViresh Kumar }
12927813dd6fSViresh Kumar 
12937813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
12947813dd6fSViresh Kumar {
12957813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
12967813dd6fSViresh Kumar }
12977813dd6fSViresh Kumar 
129832439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
129932439ac7SViresh Kumar 					       struct opp_table *opp_table,
130032439ac7SViresh Kumar 					       bool getclk)
130132439ac7SViresh Kumar {
1302d4a4c7a4SViresh Kumar 	int ret;
1303d4a4c7a4SViresh Kumar 
130432439ac7SViresh Kumar 	/*
13052083da24SViresh Kumar 	 * Return early if we don't need to get clk or we have already done it
130632439ac7SViresh Kumar 	 * earlier.
130732439ac7SViresh Kumar 	 */
13082083da24SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
13092083da24SViresh Kumar 	    opp_table->clks)
131032439ac7SViresh Kumar 		return opp_table;
131132439ac7SViresh Kumar 
131232439ac7SViresh Kumar 	/* Find clk for the device */
131332439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
131432439ac7SViresh Kumar 
1315d4a4c7a4SViresh Kumar 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
13162083da24SViresh Kumar 	if (!ret) {
13172083da24SViresh Kumar 		opp_table->config_clks = _opp_config_clk_single;
13182083da24SViresh Kumar 		opp_table->clk_count = 1;
131932439ac7SViresh Kumar 		return opp_table;
13202083da24SViresh Kumar 	}
1321d4a4c7a4SViresh Kumar 
1322d4a4c7a4SViresh Kumar 	if (ret == -ENOENT) {
13232083da24SViresh Kumar 		/*
13242083da24SViresh Kumar 		 * There are few platforms which don't want the OPP core to
13252083da24SViresh Kumar 		 * manage device's clock settings. In such cases neither the
13262083da24SViresh Kumar 		 * platform provides the clks explicitly to us, nor the DT
13272083da24SViresh Kumar 		 * contains a valid clk entry. The OPP nodes in DT may still
13282083da24SViresh Kumar 		 * contain "opp-hz" property though, which we need to parse and
13292083da24SViresh Kumar 		 * allow the platform to find an OPP based on freq later on.
13302083da24SViresh Kumar 		 *
13312083da24SViresh Kumar 		 * This is a simple solution to take care of such corner cases,
13322083da24SViresh Kumar 		 * i.e. make the clk_count 1, which lets us allocate space for
13332083da24SViresh Kumar 		 * frequency in opp->rates and also parse the entries in DT.
13342083da24SViresh Kumar 		 */
13352083da24SViresh Kumar 		opp_table->clk_count = 1;
13362083da24SViresh Kumar 
1337d4a4c7a4SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1338d4a4c7a4SViresh Kumar 		return opp_table;
1339d4a4c7a4SViresh Kumar 	}
1340d4a4c7a4SViresh Kumar 
1341d4a4c7a4SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1342d4a4c7a4SViresh Kumar 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1343d4a4c7a4SViresh Kumar 
1344d4a4c7a4SViresh Kumar 	return ERR_PTR(ret);
134532439ac7SViresh Kumar }
134632439ac7SViresh Kumar 
134727c09484SViresh Kumar /*
134827c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
134927c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
135027c09484SViresh Kumar  *
135127c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
135227c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
135327c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
135427c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
135527c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
135627c09484SViresh Kumar  * indirect users of OPP core.
135727c09484SViresh Kumar  *
135827c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
135927c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
136027c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
136127c09484SViresh Kumar  */
136232439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
136332439ac7SViresh Kumar 					 bool getclk)
13647813dd6fSViresh Kumar {
13657813dd6fSViresh Kumar 	struct opp_table *opp_table;
13667813dd6fSViresh Kumar 
136727c09484SViresh Kumar again:
13687813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
13697813dd6fSViresh Kumar 
13707813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
13717813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
13727813dd6fSViresh Kumar 		goto unlock;
13737813dd6fSViresh Kumar 
137427c09484SViresh Kumar 	/*
137527c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
137627c09484SViresh Kumar 	 * another user, wait for it to finish.
137727c09484SViresh Kumar 	 */
137827c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
137927c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
138027c09484SViresh Kumar 		cpu_relax();
138127c09484SViresh Kumar 		goto again;
138227c09484SViresh Kumar 	}
138327c09484SViresh Kumar 
138427c09484SViresh Kumar 	opp_tables_busy = true;
1385283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
138627c09484SViresh Kumar 
138727c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
138827c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
138927c09484SViresh Kumar 
1390283d55e6SViresh Kumar 	if (opp_table) {
1391ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1392283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1393dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1394283d55e6SViresh Kumar 		}
139527c09484SViresh Kumar 
139627c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
139727c09484SViresh Kumar 	} else {
139827c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
139927c09484SViresh Kumar 
140027c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
140127c09484SViresh Kumar 		if (!IS_ERR(opp_table))
140227c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1403283d55e6SViresh Kumar 	}
1404283d55e6SViresh Kumar 
140527c09484SViresh Kumar 	opp_tables_busy = false;
14067813dd6fSViresh Kumar 
14077813dd6fSViresh Kumar unlock:
14087813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
14097813dd6fSViresh Kumar 
141032439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
14117813dd6fSViresh Kumar }
1412eb7c8743SViresh Kumar 
141332439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1414e77dcb0bSViresh Kumar {
141532439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1416e77dcb0bSViresh Kumar }
1417e77dcb0bSViresh Kumar 
1418eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1419eb7c8743SViresh Kumar {
1420e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1421eb7c8743SViresh Kumar }
14227813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
14237813dd6fSViresh Kumar 
14247813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
14257813dd6fSViresh Kumar {
14267813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1427cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
14286d3f922cSGeorgi Djakov 	int i;
14297813dd6fSViresh Kumar 
1430e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1431e0df59deSViresh Kumar 	list_del(&opp_table->node);
1432e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1433e0df59deSViresh Kumar 
143481c4d8a3SViresh Kumar 	if (opp_table->current_opp)
143581c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
143681c4d8a3SViresh Kumar 
14375d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
14385d6d106fSViresh Kumar 
14392083da24SViresh Kumar 	/* Release automatically acquired single clk */
14407813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
14417813dd6fSViresh Kumar 		clk_put(opp_table->clk);
14427813dd6fSViresh Kumar 
14436d3f922cSGeorgi Djakov 	if (opp_table->paths) {
14446d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
14456d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
14466d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
14476d3f922cSGeorgi Djakov 	}
14486d3f922cSGeorgi Djakov 
1449cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1450cdd6ed90SViresh Kumar 
1451cdd6ed90SViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
14523d255699SViresh Kumar 		/*
1453cdd6ed90SViresh Kumar 		 * The OPP table is getting removed, drop the performance state
1454cdd6ed90SViresh Kumar 		 * constraints.
14553d255699SViresh Kumar 		 */
1456cdd6ed90SViresh Kumar 		if (opp_table->genpd_performance_state)
1457cdd6ed90SViresh Kumar 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
14587813dd6fSViresh Kumar 
14597813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
1460cdd6ed90SViresh Kumar 	}
14617813dd6fSViresh Kumar 
14624f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
14637813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
14647813dd6fSViresh Kumar 	kfree(opp_table);
14657813dd6fSViresh Kumar }
14667813dd6fSViresh Kumar 
14677813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
14687813dd6fSViresh Kumar {
14697813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
14707813dd6fSViresh Kumar 		       &opp_table_lock);
14717813dd6fSViresh Kumar }
14727813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
14737813dd6fSViresh Kumar 
14747813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
14757813dd6fSViresh Kumar {
14767813dd6fSViresh Kumar 	kfree(opp);
14777813dd6fSViresh Kumar }
14787813dd6fSViresh Kumar 
1479cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
14807813dd6fSViresh Kumar {
1481cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1482cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1483cf1fac94SViresh Kumar 
1484cf1fac94SViresh Kumar 	list_del(&opp->node);
1485cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1486cf1fac94SViresh Kumar 
14877813dd6fSViresh Kumar 	/*
14887813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
14897813dd6fSViresh Kumar 	 * frequency/voltage list.
14907813dd6fSViresh Kumar 	 */
14917813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1492da544b61SViresh Kumar 	_of_opp_free_required_opps(opp_table, opp);
14937813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
14947813dd6fSViresh Kumar 	kfree(opp);
14951690d8bbSViresh Kumar }
14967813dd6fSViresh Kumar 
1497a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
14987813dd6fSViresh Kumar {
14997813dd6fSViresh Kumar 	kref_get(&opp->kref);
15007813dd6fSViresh Kumar }
15017813dd6fSViresh Kumar 
15027813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
15037813dd6fSViresh Kumar {
1504cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
15057813dd6fSViresh Kumar }
15067813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
15077813dd6fSViresh Kumar 
15087813dd6fSViresh Kumar /**
15097813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
15107813dd6fSViresh Kumar  * @dev:	device for which we do this operation
15117813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
15127813dd6fSViresh Kumar  *
15137813dd6fSViresh Kumar  * This function removes an opp from the opp table.
15147813dd6fSViresh Kumar  */
15157813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
15167813dd6fSViresh Kumar {
151795073b72SJakob Koschel 	struct dev_pm_opp *opp = NULL, *iter;
15187813dd6fSViresh Kumar 	struct opp_table *opp_table;
15197813dd6fSViresh Kumar 
15207813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
15217813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
15227813dd6fSViresh Kumar 		return;
15237813dd6fSViresh Kumar 
15247813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
15257813dd6fSViresh Kumar 
152695073b72SJakob Koschel 	list_for_each_entry(iter, &opp_table->opp_list, node) {
15272083da24SViresh Kumar 		if (iter->rates[0] == freq) {
152895073b72SJakob Koschel 			opp = iter;
15297813dd6fSViresh Kumar 			break;
15307813dd6fSViresh Kumar 		}
15317813dd6fSViresh Kumar 	}
15327813dd6fSViresh Kumar 
15337813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
15347813dd6fSViresh Kumar 
153595073b72SJakob Koschel 	if (opp) {
15367813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
15370ad8c623SViresh Kumar 
15380ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
15390ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
15407813dd6fSViresh Kumar 	} else {
15417813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
15427813dd6fSViresh Kumar 			 __func__, freq);
15437813dd6fSViresh Kumar 	}
15447813dd6fSViresh Kumar 
15450ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
15467813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
15477813dd6fSViresh Kumar }
15487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
15497813dd6fSViresh Kumar 
1550cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1551cf1fac94SViresh Kumar 					bool dynamic)
1552cf1fac94SViresh Kumar {
1553cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1554cf1fac94SViresh Kumar 
1555cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1556cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1557606a5d42SBeata Michalska 		/*
1558606a5d42SBeata Michalska 		 * Refcount must be dropped only once for each OPP by OPP core,
1559606a5d42SBeata Michalska 		 * do that with help of "removed" flag.
1560606a5d42SBeata Michalska 		 */
1561606a5d42SBeata Michalska 		if (!temp->removed && dynamic == temp->dynamic) {
1562cf1fac94SViresh Kumar 			opp = temp;
1563cf1fac94SViresh Kumar 			break;
1564cf1fac94SViresh Kumar 		}
1565cf1fac94SViresh Kumar 	}
1566cf1fac94SViresh Kumar 
1567cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1568cf1fac94SViresh Kumar 	return opp;
1569cf1fac94SViresh Kumar }
1570cf1fac94SViresh Kumar 
1571606a5d42SBeata Michalska /*
1572606a5d42SBeata Michalska  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1573606a5d42SBeata Michalska  * happen lock less to avoid circular dependency issues. This routine must be
1574606a5d42SBeata Michalska  * called without the opp_table->lock held.
1575606a5d42SBeata Michalska  */
1576606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
157703758d60SViresh Kumar {
1578cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
157903758d60SViresh Kumar 
1580606a5d42SBeata Michalska 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1581606a5d42SBeata Michalska 		opp->removed = true;
1582606a5d42SBeata Michalska 		dev_pm_opp_put(opp);
1583606a5d42SBeata Michalska 
1584606a5d42SBeata Michalska 		/* Drop the references taken by dev_pm_opp_add() */
1585606a5d42SBeata Michalska 		if (dynamic)
1586606a5d42SBeata Michalska 			dev_pm_opp_put_opp_table(opp_table);
1587606a5d42SBeata Michalska 	}
1588606a5d42SBeata Michalska }
1589606a5d42SBeata Michalska 
1590606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table)
1591606a5d42SBeata Michalska {
159203758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
159303758d60SViresh Kumar 
1594922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1595cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1596cf1fac94SViresh Kumar 		return false;
1597922ff075SViresh Kumar 	}
1598922ff075SViresh Kumar 
1599cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1600cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1601cf1fac94SViresh Kumar 		return true;
160203758d60SViresh Kumar 	}
160303758d60SViresh Kumar 
160403758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1605922ff075SViresh Kumar 
1606606a5d42SBeata Michalska 	_opp_remove_all(opp_table, false);
1607cf1fac94SViresh Kumar 	return true;
160803758d60SViresh Kumar }
160903758d60SViresh Kumar 
16101690d8bbSViresh Kumar /**
16111690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
16121690d8bbSViresh Kumar  * @dev:	device for which we do this operation
16131690d8bbSViresh Kumar  *
16141690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
16151690d8bbSViresh Kumar  */
16161690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
16171690d8bbSViresh Kumar {
16181690d8bbSViresh Kumar 	struct opp_table *opp_table;
16191690d8bbSViresh Kumar 
16201690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
16211690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
16221690d8bbSViresh Kumar 		return;
16231690d8bbSViresh Kumar 
1624606a5d42SBeata Michalska 	_opp_remove_all(opp_table, true);
16251690d8bbSViresh Kumar 
16261690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
16271690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
16281690d8bbSViresh Kumar }
16291690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
16301690d8bbSViresh Kumar 
1631d6134583SViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table)
16327813dd6fSViresh Kumar {
16337813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
16342083da24SViresh Kumar 	int supply_count, supply_size, icc_size, clk_size;
16357813dd6fSViresh Kumar 
16367813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
1637d6134583SViresh Kumar 	supply_count = opp_table->regulator_count > 0 ?
1638d6134583SViresh Kumar 			opp_table->regulator_count : 1;
16396d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
16402083da24SViresh Kumar 	clk_size = sizeof(*opp->rates) * opp_table->clk_count;
1641d6134583SViresh Kumar 	icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
16427813dd6fSViresh Kumar 
16437813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
16442083da24SViresh Kumar 	opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL);
16457813dd6fSViresh Kumar 	if (!opp)
16467813dd6fSViresh Kumar 		return NULL;
16477813dd6fSViresh Kumar 
16482083da24SViresh Kumar 	/* Put the supplies, bw and clock at the end of the OPP structure */
16497813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
16502083da24SViresh Kumar 
16512083da24SViresh Kumar 	opp->rates = (unsigned long *)(opp->supplies + supply_count);
16522083da24SViresh Kumar 
16536d3f922cSGeorgi Djakov 	if (icc_size)
16542083da24SViresh Kumar 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
16552083da24SViresh Kumar 
16567813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
16577813dd6fSViresh Kumar 
16587813dd6fSViresh Kumar 	return opp;
16597813dd6fSViresh Kumar }
16607813dd6fSViresh Kumar 
16617813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
16627813dd6fSViresh Kumar 					 struct opp_table *opp_table)
16637813dd6fSViresh Kumar {
16647813dd6fSViresh Kumar 	struct regulator *reg;
16657813dd6fSViresh Kumar 	int i;
16667813dd6fSViresh Kumar 
166790e3577bSViresh Kumar 	if (!opp_table->regulators)
166890e3577bSViresh Kumar 		return true;
166990e3577bSViresh Kumar 
16707813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
16717813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
16727813dd6fSViresh Kumar 
16737813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
16747813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
16757813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
16767813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
16777813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
16787813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
16797813dd6fSViresh Kumar 			return false;
16807813dd6fSViresh Kumar 		}
16817813dd6fSViresh Kumar 	}
16827813dd6fSViresh Kumar 
16837813dd6fSViresh Kumar 	return true;
16847813dd6fSViresh Kumar }
16857813dd6fSViresh Kumar 
16862083da24SViresh Kumar static int _opp_compare_rate(struct opp_table *opp_table,
16872083da24SViresh Kumar 			     struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
16882083da24SViresh Kumar {
16892083da24SViresh Kumar 	int i;
16902083da24SViresh Kumar 
16912083da24SViresh Kumar 	for (i = 0; i < opp_table->clk_count; i++) {
16922083da24SViresh Kumar 		if (opp1->rates[i] != opp2->rates[i])
16932083da24SViresh Kumar 			return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
16942083da24SViresh Kumar 	}
16952083da24SViresh Kumar 
16962083da24SViresh Kumar 	/* Same rates for both OPPs */
16972083da24SViresh Kumar 	return 0;
16982083da24SViresh Kumar }
16992083da24SViresh Kumar 
1700*274c3e83SViresh Kumar static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1701*274c3e83SViresh Kumar 			   struct dev_pm_opp *opp2)
1702*274c3e83SViresh Kumar {
1703*274c3e83SViresh Kumar 	int i;
1704*274c3e83SViresh Kumar 
1705*274c3e83SViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
1706*274c3e83SViresh Kumar 		if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
1707*274c3e83SViresh Kumar 			return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
1708*274c3e83SViresh Kumar 	}
1709*274c3e83SViresh Kumar 
1710*274c3e83SViresh Kumar 	/* Same bw for both OPPs */
1711*274c3e83SViresh Kumar 	return 0;
1712*274c3e83SViresh Kumar }
1713*274c3e83SViresh Kumar 
17148bdac14bSViresh Kumar /*
17158bdac14bSViresh Kumar  * Returns
17168bdac14bSViresh Kumar  * 0: opp1 == opp2
17178bdac14bSViresh Kumar  * 1: opp1 > opp2
17188bdac14bSViresh Kumar  * -1: opp1 < opp2
17198bdac14bSViresh Kumar  */
17202083da24SViresh Kumar int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1,
17212083da24SViresh Kumar 		     struct dev_pm_opp *opp2)
17226c591eecSSaravana Kannan {
17232083da24SViresh Kumar 	int ret;
17242083da24SViresh Kumar 
17252083da24SViresh Kumar 	ret = _opp_compare_rate(opp_table, opp1, opp2);
17262083da24SViresh Kumar 	if (ret)
17272083da24SViresh Kumar 		return ret;
17282083da24SViresh Kumar 
1729*274c3e83SViresh Kumar 	ret = _opp_compare_bw(opp_table, opp1, opp2);
1730*274c3e83SViresh Kumar 	if (ret)
1731*274c3e83SViresh Kumar 		return ret;
17322083da24SViresh Kumar 
17336c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
17346c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
17352083da24SViresh Kumar 
17362083da24SViresh Kumar 	/* Duplicate OPPs */
17376c591eecSSaravana Kannan 	return 0;
17386c591eecSSaravana Kannan }
17396c591eecSSaravana Kannan 
1740a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1741a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1742a1e8c136SViresh Kumar 			     struct list_head **head)
1743a1e8c136SViresh Kumar {
1744a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
17456c591eecSSaravana Kannan 	int opp_cmp;
1746a1e8c136SViresh Kumar 
1747a1e8c136SViresh Kumar 	/*
1748a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1749a1e8c136SViresh Kumar 	 * already present.
1750a1e8c136SViresh Kumar 	 *
1751a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1752a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1753a1e8c136SViresh Kumar 	 * loop.
1754a1e8c136SViresh Kumar 	 */
1755a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
17562083da24SViresh Kumar 		opp_cmp = _opp_compare_key(opp_table, new_opp, opp);
17576c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1758a1e8c136SViresh Kumar 			*head = &opp->node;
1759a1e8c136SViresh Kumar 			continue;
1760a1e8c136SViresh Kumar 		}
1761a1e8c136SViresh Kumar 
17626c591eecSSaravana Kannan 		if (opp_cmp < 0)
1763a1e8c136SViresh Kumar 			return 0;
1764a1e8c136SViresh Kumar 
1765a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1766a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
17672083da24SViresh Kumar 			 __func__, opp->rates[0], opp->supplies[0].u_volt,
17682083da24SViresh Kumar 			 opp->available, new_opp->rates[0],
1769a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1770a1e8c136SViresh Kumar 
1771a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1772a1e8c136SViresh Kumar 		return opp->available &&
1773a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1774a1e8c136SViresh Kumar 	}
1775a1e8c136SViresh Kumar 
1776a1e8c136SViresh Kumar 	return 0;
1777a1e8c136SViresh Kumar }
1778a1e8c136SViresh Kumar 
17797eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count)
17807eba0c76SViresh Kumar {
17817eba0c76SViresh Kumar 	int i;
17827eba0c76SViresh Kumar 
17837eba0c76SViresh Kumar 	for (i = 0; i < count; i++) {
17847eba0c76SViresh Kumar 		if (opp->required_opps[i]->available)
17857eba0c76SViresh Kumar 			continue;
17867eba0c76SViresh Kumar 
17877eba0c76SViresh Kumar 		opp->available = false;
17887eba0c76SViresh Kumar 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
17892083da24SViresh Kumar 			 __func__, opp->required_opps[i]->np, opp->rates[0]);
17907eba0c76SViresh Kumar 		return;
17917eba0c76SViresh Kumar 	}
17927eba0c76SViresh Kumar }
17937eba0c76SViresh Kumar 
17947813dd6fSViresh Kumar /*
17957813dd6fSViresh Kumar  * Returns:
17967813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
17977813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
17987813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
17997813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
18007813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
18017813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
18027813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
18037813dd6fSViresh Kumar  */
18047813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
18054768914bSViresh Kumar 	     struct opp_table *opp_table)
18067813dd6fSViresh Kumar {
18077813dd6fSViresh Kumar 	struct list_head *head;
18087813dd6fSViresh Kumar 	int ret;
18097813dd6fSViresh Kumar 
18107813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
18117813dd6fSViresh Kumar 	head = &opp_table->opp_list;
18127813dd6fSViresh Kumar 
1813a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1814a1e8c136SViresh Kumar 	if (ret) {
18157813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
18167813dd6fSViresh Kumar 		return ret;
18177813dd6fSViresh Kumar 	}
18187813dd6fSViresh Kumar 
18197813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
18207813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
18217813dd6fSViresh Kumar 
18227813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
18237813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
18247813dd6fSViresh Kumar 
1825a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
18267813dd6fSViresh Kumar 
18277813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
18287813dd6fSViresh Kumar 		new_opp->available = false;
18297813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
18302083da24SViresh Kumar 			 __func__, new_opp->rates[0]);
18317813dd6fSViresh Kumar 	}
18327813dd6fSViresh Kumar 
18337eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
18347eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
18357eba0c76SViresh Kumar 		return 0;
1836cf65948dSDmitry Osipenko 
18377eba0c76SViresh Kumar 	_required_opps_available(new_opp, opp_table->required_opp_count);
1838cf65948dSDmitry Osipenko 
18397813dd6fSViresh Kumar 	return 0;
18407813dd6fSViresh Kumar }
18417813dd6fSViresh Kumar 
18427813dd6fSViresh Kumar /**
18437813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
18447813dd6fSViresh Kumar  * @opp_table:	OPP table
18457813dd6fSViresh Kumar  * @dev:	device for which we do this operation
18467813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
18477813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
18487813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
18497813dd6fSViresh Kumar  *
18507813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
18517813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
18527813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
18537813dd6fSViresh Kumar  *
18547813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
18557813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
18567813dd6fSViresh Kumar  *
18577813dd6fSViresh Kumar  * Return:
18587813dd6fSViresh Kumar  * 0		On success OR
18597813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
18607813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
18617813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
18627813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
18637813dd6fSViresh Kumar  */
18647813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
18657813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
18667813dd6fSViresh Kumar {
18677813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
18687813dd6fSViresh Kumar 	unsigned long tol;
18697813dd6fSViresh Kumar 	int ret;
18707813dd6fSViresh Kumar 
18717813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
18727813dd6fSViresh Kumar 	if (!new_opp)
18737813dd6fSViresh Kumar 		return -ENOMEM;
18747813dd6fSViresh Kumar 
18757813dd6fSViresh Kumar 	/* populate the opp table */
18762083da24SViresh Kumar 	new_opp->rates[0] = freq;
18777813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
18787813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
18797813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
18807813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
18817813dd6fSViresh Kumar 	new_opp->available = true;
18827813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
18837813dd6fSViresh Kumar 
18844768914bSViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table);
18857813dd6fSViresh Kumar 	if (ret) {
18867813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
18877813dd6fSViresh Kumar 		if (ret == -EBUSY)
18887813dd6fSViresh Kumar 			ret = 0;
18897813dd6fSViresh Kumar 		goto free_opp;
18907813dd6fSViresh Kumar 	}
18917813dd6fSViresh Kumar 
18927813dd6fSViresh Kumar 	/*
18937813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
18947813dd6fSViresh Kumar 	 * frequency/voltage list.
18957813dd6fSViresh Kumar 	 */
18967813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
18977813dd6fSViresh Kumar 	return 0;
18987813dd6fSViresh Kumar 
18997813dd6fSViresh Kumar free_opp:
19007813dd6fSViresh Kumar 	_opp_free(new_opp);
19017813dd6fSViresh Kumar 
19027813dd6fSViresh Kumar 	return ret;
19037813dd6fSViresh Kumar }
19047813dd6fSViresh Kumar 
19057813dd6fSViresh Kumar /**
190689f03984SViresh Kumar  * _opp_set_supported_hw() - Set supported platforms
19077813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
19087813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
19097813dd6fSViresh Kumar  * @count: Number of elements in the array.
19107813dd6fSViresh Kumar  *
19117813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
19127813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
19137813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
19147813dd6fSViresh Kumar  * property.
19157813dd6fSViresh Kumar  */
191689f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table,
19177813dd6fSViresh Kumar 				 const u32 *versions, unsigned int count)
19187813dd6fSViresh Kumar {
191925419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
192025419de1SViresh Kumar 	if (opp_table->supported_hw)
192189f03984SViresh Kumar 		return 0;
19227813dd6fSViresh Kumar 
19237813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
19247813dd6fSViresh Kumar 					GFP_KERNEL);
192589f03984SViresh Kumar 	if (!opp_table->supported_hw)
192689f03984SViresh Kumar 		return -ENOMEM;
19277813dd6fSViresh Kumar 
19287813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
19297813dd6fSViresh Kumar 
193089f03984SViresh Kumar 	return 0;
19317813dd6fSViresh Kumar }
19327813dd6fSViresh Kumar 
19337813dd6fSViresh Kumar /**
193489f03984SViresh Kumar  * _opp_put_supported_hw() - Releases resources blocked for supported hw
193589f03984SViresh Kumar  * @opp_table: OPP table returned by _opp_set_supported_hw().
19367813dd6fSViresh Kumar  *
19377813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
193889f03984SViresh Kumar  * _opp_set_supported_hw(). Until this is called, the opp_table structure
19397813dd6fSViresh Kumar  * will not be freed.
19407813dd6fSViresh Kumar  */
194189f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table)
19427813dd6fSViresh Kumar {
194389f03984SViresh Kumar 	if (opp_table->supported_hw) {
19447813dd6fSViresh Kumar 		kfree(opp_table->supported_hw);
19457813dd6fSViresh Kumar 		opp_table->supported_hw = NULL;
19467813dd6fSViresh Kumar 		opp_table->supported_hw_count = 0;
19477813dd6fSViresh Kumar 	}
19489c4f220fSYangtao Li }
19499c4f220fSYangtao Li 
19509c4f220fSYangtao Li /**
1951298098e5SViresh Kumar  * _opp_set_prop_name() - Set prop-extn name
19527813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
19537813dd6fSViresh Kumar  * @name: name to postfix to properties.
19547813dd6fSViresh Kumar  *
19557813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
19567813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
19577813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
19587813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
19597813dd6fSViresh Kumar  */
1960298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
19617813dd6fSViresh Kumar {
1962878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
19637813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
1964298098e5SViresh Kumar 		opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1965298098e5SViresh Kumar 		if (!opp_table->prop_name)
1966298098e5SViresh Kumar 			return -ENOMEM;
19677813dd6fSViresh Kumar 	}
19687813dd6fSViresh Kumar 
1969298098e5SViresh Kumar 	return 0;
19707813dd6fSViresh Kumar }
19717813dd6fSViresh Kumar 
19727813dd6fSViresh Kumar /**
1973298098e5SViresh Kumar  * _opp_put_prop_name() - Releases resources blocked for prop-name
1974298098e5SViresh Kumar  * @opp_table: OPP table returned by _opp_set_prop_name().
19757813dd6fSViresh Kumar  *
19767813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
1977298098e5SViresh Kumar  * _opp_set_prop_name(). Until this is called, the opp_table structure
19787813dd6fSViresh Kumar  * will not be freed.
19797813dd6fSViresh Kumar  */
1980298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table)
19817813dd6fSViresh Kumar {
1982298098e5SViresh Kumar 	if (opp_table->prop_name) {
19837813dd6fSViresh Kumar 		kfree(opp_table->prop_name);
19847813dd6fSViresh Kumar 		opp_table->prop_name = NULL;
19857813dd6fSViresh Kumar 	}
1986298098e5SViresh Kumar }
19877813dd6fSViresh Kumar 
19887813dd6fSViresh Kumar /**
1989b0ec0942SViresh Kumar  * _opp_set_regulators() - Set regulator names for the device
19907813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
19917813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
19927813dd6fSViresh Kumar  * @count: Number of regulators.
19937813dd6fSViresh Kumar  *
19947813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
19957813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
19967813dd6fSViresh Kumar  * well.
19977813dd6fSViresh Kumar  *
19987813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
19997813dd6fSViresh Kumar  */
2000b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
200187686cc8SViresh Kumar 			       const char * const names[])
20027813dd6fSViresh Kumar {
200387686cc8SViresh Kumar 	const char * const *temp = names;
20047813dd6fSViresh Kumar 	struct regulator *reg;
200587686cc8SViresh Kumar 	int count = 0, ret, i;
200687686cc8SViresh Kumar 
200787686cc8SViresh Kumar 	/* Count number of regulators */
200887686cc8SViresh Kumar 	while (*temp++)
200987686cc8SViresh Kumar 		count++;
201087686cc8SViresh Kumar 
201187686cc8SViresh Kumar 	if (!count)
2012b0ec0942SViresh Kumar 		return -EINVAL;
20137813dd6fSViresh Kumar 
2014779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
2015779b783cSViresh Kumar 	if (opp_table->regulators)
2016b0ec0942SViresh Kumar 		return 0;
20177813dd6fSViresh Kumar 
20187813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
20197813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
20207813dd6fSViresh Kumar 					      GFP_KERNEL);
2021b0ec0942SViresh Kumar 	if (!opp_table->regulators)
2022b0ec0942SViresh Kumar 		return -ENOMEM;
20237813dd6fSViresh Kumar 
20247813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
20257813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
20267813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
2027543256d2SKrzysztof Kozlowski 			ret = dev_err_probe(dev, PTR_ERR(reg),
2028543256d2SKrzysztof Kozlowski 					    "%s: no regulator (%s) found\n",
2029543256d2SKrzysztof Kozlowski 					    __func__, names[i]);
20307813dd6fSViresh Kumar 			goto free_regulators;
20317813dd6fSViresh Kumar 		}
20327813dd6fSViresh Kumar 
20337813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
20347813dd6fSViresh Kumar 	}
20357813dd6fSViresh Kumar 
20367813dd6fSViresh Kumar 	opp_table->regulator_count = count;
20377813dd6fSViresh Kumar 
2038c522ce8aSViresh Kumar 	/* Set generic config_regulators() for single regulators here */
2039c522ce8aSViresh Kumar 	if (count == 1)
2040c522ce8aSViresh Kumar 		opp_table->config_regulators = _opp_config_regulator_single;
2041c522ce8aSViresh Kumar 
2042b0ec0942SViresh Kumar 	return 0;
20437813dd6fSViresh Kumar 
20447813dd6fSViresh Kumar free_regulators:
204524957db1SMarek Szyprowski 	while (i != 0)
204624957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
20477813dd6fSViresh Kumar 
20487813dd6fSViresh Kumar 	kfree(opp_table->regulators);
20497813dd6fSViresh Kumar 	opp_table->regulators = NULL;
205046f48acaSViresh Kumar 	opp_table->regulator_count = -1;
20517813dd6fSViresh Kumar 
2052b0ec0942SViresh Kumar 	return ret;
20537813dd6fSViresh Kumar }
20547813dd6fSViresh Kumar 
20557813dd6fSViresh Kumar /**
2056b0ec0942SViresh Kumar  * _opp_put_regulators() - Releases resources blocked for regulator
2057b0ec0942SViresh Kumar  * @opp_table: OPP table returned from _opp_set_regulators().
20587813dd6fSViresh Kumar  */
2059b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table)
20607813dd6fSViresh Kumar {
20617813dd6fSViresh Kumar 	int i;
20627813dd6fSViresh Kumar 
2063779b783cSViresh Kumar 	if (!opp_table->regulators)
2064b0ec0942SViresh Kumar 		return;
20657813dd6fSViresh Kumar 
206672f80ce4SViresh Kumar 	if (opp_table->enabled) {
20678d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
20688d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
20698d45719cSKamil Konieczny 	}
20708d45719cSKamil Konieczny 
207124957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
20727813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
20737813dd6fSViresh Kumar 
20747813dd6fSViresh Kumar 	kfree(opp_table->regulators);
20757813dd6fSViresh Kumar 	opp_table->regulators = NULL;
207646f48acaSViresh Kumar 	opp_table->regulator_count = -1;
20777813dd6fSViresh Kumar }
207832aee78bSYangtao Li 
20792083da24SViresh Kumar static void _put_clks(struct opp_table *opp_table, int count)
20802083da24SViresh Kumar {
20812083da24SViresh Kumar 	int i;
20822083da24SViresh Kumar 
20832083da24SViresh Kumar 	for (i = count - 1; i >= 0; i--)
20842083da24SViresh Kumar 		clk_put(opp_table->clks[i]);
20852083da24SViresh Kumar 
20862083da24SViresh Kumar 	kfree(opp_table->clks);
20872083da24SViresh Kumar 	opp_table->clks = NULL;
20882083da24SViresh Kumar }
20892083da24SViresh Kumar 
20907813dd6fSViresh Kumar /**
20912368f576SViresh Kumar  * _opp_set_clknames() - Set clk names for the device
20922368f576SViresh Kumar  * @dev: Device for which clk names is being set.
20932368f576SViresh Kumar  * @names: Clk names.
20947813dd6fSViresh Kumar  *
20952368f576SViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointers to the
20962368f576SViresh Kumar  * clocks for the device. Simple cases work fine without using this routine
20972368f576SViresh Kumar  * (i.e. by passing connection-id as NULL), but for a device with multiple
20982368f576SViresh Kumar  * clocks available, the OPP core needs to know the exact names of the clks to
20992368f576SViresh Kumar  * use.
21007813dd6fSViresh Kumar  *
21017813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
21027813dd6fSViresh Kumar  */
21032368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
21042083da24SViresh Kumar 			     const char * const names[],
21052083da24SViresh Kumar 			     config_clks_t config_clks)
21067813dd6fSViresh Kumar {
21072368f576SViresh Kumar 	const char * const *temp = names;
21082083da24SViresh Kumar 	int count = 0, ret, i;
21092083da24SViresh Kumar 	struct clk *clk;
21107813dd6fSViresh Kumar 
21112368f576SViresh Kumar 	/* Count number of clks */
21122368f576SViresh Kumar 	while (*temp++)
21132368f576SViresh Kumar 		count++;
21147813dd6fSViresh Kumar 
21152368f576SViresh Kumar 	/*
21162368f576SViresh Kumar 	 * This is a special case where we have a single clock, whose connection
21172368f576SViresh Kumar 	 * id name is NULL, i.e. first two entries are NULL in the array.
21182368f576SViresh Kumar 	 */
21192368f576SViresh Kumar 	if (!count && !names[1])
21202368f576SViresh Kumar 		count = 1;
21212368f576SViresh Kumar 
21222083da24SViresh Kumar 	/* Fail early for invalid configurations */
21232083da24SViresh Kumar 	if (!count || (config_clks && count == 1) || (!config_clks && count > 1))
21242368f576SViresh Kumar 		return -EINVAL;
21257813dd6fSViresh Kumar 
21260a43452bSViresh Kumar 	/* Another CPU that shares the OPP table has set the clkname ? */
21272083da24SViresh Kumar 	if (opp_table->clks)
21282368f576SViresh Kumar 		return 0;
21290a43452bSViresh Kumar 
21302083da24SViresh Kumar 	opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks),
21312083da24SViresh Kumar 					GFP_KERNEL);
21322083da24SViresh Kumar 	if (!opp_table->clks)
21332083da24SViresh Kumar 		return -ENOMEM;
21347813dd6fSViresh Kumar 
21352083da24SViresh Kumar 	/* Find clks for the device */
21362083da24SViresh Kumar 	for (i = 0; i < count; i++) {
21372083da24SViresh Kumar 		clk = clk_get(dev, names[i]);
21382083da24SViresh Kumar 		if (IS_ERR(clk)) {
21392083da24SViresh Kumar 			ret = dev_err_probe(dev, PTR_ERR(clk),
21402083da24SViresh Kumar 					    "%s: Couldn't find clock with name: %s\n",
21412083da24SViresh Kumar 					    __func__, names[i]);
21422083da24SViresh Kumar 			goto free_clks;
21437813dd6fSViresh Kumar 		}
21447813dd6fSViresh Kumar 
21452083da24SViresh Kumar 		opp_table->clks[i] = clk;
21462083da24SViresh Kumar 	}
21472083da24SViresh Kumar 
21482083da24SViresh Kumar 	opp_table->clk_count = count;
21492083da24SViresh Kumar 
21502083da24SViresh Kumar 	/* Set generic single clk set here */
21512083da24SViresh Kumar 	if (count == 1) {
21522083da24SViresh Kumar 		opp_table->config_clks = _opp_config_clk_single;
21532083da24SViresh Kumar 
21542083da24SViresh Kumar 		/*
21552083da24SViresh Kumar 		 * We could have just dropped the "clk" field and used "clks"
21562083da24SViresh Kumar 		 * everywhere. Instead we kept the "clk" field around for
21572083da24SViresh Kumar 		 * following reasons:
21582083da24SViresh Kumar 		 *
21592083da24SViresh Kumar 		 * - avoiding clks[0] everywhere else.
21602083da24SViresh Kumar 		 * - not running single clk helpers for multiple clk usecase by
21612083da24SViresh Kumar 		 *   mistake.
21622083da24SViresh Kumar 		 *
21632083da24SViresh Kumar 		 * Since this is single-clk case, just update the clk pointer
21642083da24SViresh Kumar 		 * too.
21652083da24SViresh Kumar 		 */
21662083da24SViresh Kumar 		opp_table->clk = opp_table->clks[0];
21672083da24SViresh Kumar 	} else {
21682083da24SViresh Kumar 		opp_table->config_clks = config_clks;
21692083da24SViresh Kumar 	}
21700a43452bSViresh Kumar 
21712368f576SViresh Kumar 	return 0;
21722083da24SViresh Kumar 
21732083da24SViresh Kumar free_clks:
21742083da24SViresh Kumar 	_put_clks(opp_table, i);
21752083da24SViresh Kumar 	return ret;
21767813dd6fSViresh Kumar }
21777813dd6fSViresh Kumar 
21787813dd6fSViresh Kumar /**
21792368f576SViresh Kumar  * _opp_put_clknames() - Releases resources blocked for clks.
21802368f576SViresh Kumar  * @opp_table: OPP table returned from _opp_set_clknames().
21817813dd6fSViresh Kumar  */
21822368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table)
21837813dd6fSViresh Kumar {
21842083da24SViresh Kumar 	if (!opp_table->clks)
21852083da24SViresh Kumar 		return;
21862083da24SViresh Kumar 
21872083da24SViresh Kumar 	opp_table->config_clks = NULL;
21882083da24SViresh Kumar 	opp_table->clk = ERR_PTR(-ENODEV);
21892083da24SViresh Kumar 
21902083da24SViresh Kumar 	_put_clks(opp_table, opp_table->clk_count);
2191a74f681cSYangtao Li }
2192a74f681cSYangtao Li 
2193a74f681cSYangtao Li /**
2194aee3352fSViresh Kumar  * _opp_set_config_regulators_helper() - Register custom set regulator helper.
2195aee3352fSViresh Kumar  * @dev: Device for which the helper is getting registered.
2196aee3352fSViresh Kumar  * @config_regulators: Custom set regulator helper.
2197aee3352fSViresh Kumar  *
2198aee3352fSViresh Kumar  * This is useful to support platforms with multiple regulators per device.
2199aee3352fSViresh Kumar  *
2200aee3352fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
2201aee3352fSViresh Kumar  */
2202aee3352fSViresh Kumar static int _opp_set_config_regulators_helper(struct opp_table *opp_table,
2203aee3352fSViresh Kumar 		struct device *dev, config_regulators_t config_regulators)
2204aee3352fSViresh Kumar {
2205aee3352fSViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
2206aee3352fSViresh Kumar 	if (!opp_table->config_regulators)
2207aee3352fSViresh Kumar 		opp_table->config_regulators = config_regulators;
2208aee3352fSViresh Kumar 
2209aee3352fSViresh Kumar 	return 0;
2210aee3352fSViresh Kumar }
2211aee3352fSViresh Kumar 
2212aee3352fSViresh Kumar /**
2213aee3352fSViresh Kumar  * _opp_put_config_regulators_helper() - Releases resources blocked for
2214aee3352fSViresh Kumar  *					 config_regulators helper.
2215aee3352fSViresh Kumar  * @opp_table: OPP table returned from _opp_set_config_regulators_helper().
2216aee3352fSViresh Kumar  *
2217aee3352fSViresh Kumar  * Release resources blocked for platform specific config_regulators helper.
2218aee3352fSViresh Kumar  */
2219aee3352fSViresh Kumar static void _opp_put_config_regulators_helper(struct opp_table *opp_table)
2220aee3352fSViresh Kumar {
2221aee3352fSViresh Kumar 	if (opp_table->config_regulators)
2222aee3352fSViresh Kumar 		opp_table->config_regulators = NULL;
2223aee3352fSViresh Kumar }
2224aee3352fSViresh Kumar 
2225442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table)
22266319aee1SViresh Kumar {
22276319aee1SViresh Kumar 	int index;
22286319aee1SViresh Kumar 
2229cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2230cb60e960SViresh Kumar 		return;
2231cb60e960SViresh Kumar 
22326319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
22336319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
22346319aee1SViresh Kumar 			continue;
22356319aee1SViresh Kumar 
22366319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
22376319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
22386319aee1SViresh Kumar 	}
2239c0ab9e08SViresh Kumar 
2240c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2241c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
22426319aee1SViresh Kumar }
22436319aee1SViresh Kumar 
22447813dd6fSViresh Kumar /**
2245442e7a17SViresh Kumar  * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
22466319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
22476319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
224817a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
22494f018bc0SViresh Kumar  *
22504f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
22514f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
22524f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
22534f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
22546319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
22556319aee1SViresh Kumar  * we don't need to support that separately.
22564f018bc0SViresh Kumar  *
22574f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
22586319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
22594f018bc0SViresh Kumar  *
22606319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
22616319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2262baea35e4SViresh Kumar  *
2263baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2264baea35e4SViresh Kumar  * "required-opps" are added in DT.
22654f018bc0SViresh Kumar  */
2266442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
22673734b9f2SDmitry Osipenko 			const char * const *names, struct device ***virt_devs)
22684f018bc0SViresh Kumar {
22696319aee1SViresh Kumar 	struct device *virt_dev;
2270baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
22713734b9f2SDmitry Osipenko 	const char * const *name = names;
22724f018bc0SViresh Kumar 
2273cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2274442e7a17SViresh Kumar 		return 0;
22754f018bc0SViresh Kumar 
22766319aee1SViresh Kumar 	/*
22776319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
22786319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
22796319aee1SViresh Kumar 	 * table is added.
22806319aee1SViresh Kumar 	 */
2281442e7a17SViresh Kumar 	if (!opp_table->required_opp_count)
2282442e7a17SViresh Kumar 		return -EPROBE_DEFER;
22836319aee1SViresh Kumar 
22844f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
22854f018bc0SViresh Kumar 
2286c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2287c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2288c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2289c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2290c0ab9e08SViresh Kumar 		goto unlock;
22914f018bc0SViresh Kumar 
22926319aee1SViresh Kumar 	while (*name) {
22936319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
22946319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
22956319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
22966319aee1SViresh Kumar 			goto err;
22976319aee1SViresh Kumar 		}
22984f018bc0SViresh Kumar 
22996319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
23004ea9496cSTang Bin 		if (IS_ERR_OR_NULL(virt_dev)) {
23014ea9496cSTang Bin 			ret = PTR_ERR(virt_dev) ? : -ENODEV;
23026319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
23036319aee1SViresh Kumar 			goto err;
23044f018bc0SViresh Kumar 		}
23054f018bc0SViresh Kumar 
23064f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2307baea35e4SViresh Kumar 		index++;
23086319aee1SViresh Kumar 		name++;
23096319aee1SViresh Kumar 	}
23106319aee1SViresh Kumar 
231117a8f868SViresh Kumar 	if (virt_devs)
231217a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
23134f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23144f018bc0SViresh Kumar 
2315442e7a17SViresh Kumar 	return 0;
23166319aee1SViresh Kumar 
23176319aee1SViresh Kumar err:
2318442e7a17SViresh Kumar 	_detach_genpd(opp_table);
2319c0ab9e08SViresh Kumar unlock:
23206319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2321442e7a17SViresh Kumar 	return ret;
23226319aee1SViresh Kumar 
23234f018bc0SViresh Kumar }
23244f018bc0SViresh Kumar 
23254f018bc0SViresh Kumar /**
2326442e7a17SViresh Kumar  * _opp_detach_genpd() - Detach genpd(s) from the device.
2327442e7a17SViresh Kumar  * @opp_table: OPP table returned by _opp_attach_genpd().
23284f018bc0SViresh Kumar  *
23296319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
23306319aee1SViresh Kumar  * OPP table.
23314f018bc0SViresh Kumar  */
2332442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
23334f018bc0SViresh Kumar {
23344f018bc0SViresh Kumar 	/*
23354f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
23364f018bc0SViresh Kumar 	 * used in parallel.
23374f018bc0SViresh Kumar 	 */
23384f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2339442e7a17SViresh Kumar 	_detach_genpd(opp_table);
23404f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
23414f018bc0SViresh Kumar }
2342b4b9e223SDmitry Osipenko 
234311b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data)
234411b9b663SViresh Kumar {
234511b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_GENPD)
2346442e7a17SViresh Kumar 		_opp_detach_genpd(data->opp_table);
234711b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR)
2348b0ec0942SViresh Kumar 		_opp_put_regulators(data->opp_table);
234911b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_SUPPORTED_HW)
235089f03984SViresh Kumar 		_opp_put_supported_hw(data->opp_table);
23511f378c6eSViresh Kumar 	if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
2352aee3352fSViresh Kumar 		_opp_put_config_regulators_helper(data->opp_table);
235311b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_PROP_NAME)
2354298098e5SViresh Kumar 		_opp_put_prop_name(data->opp_table);
235511b9b663SViresh Kumar 	if (data->flags & OPP_CONFIG_CLK)
23562368f576SViresh Kumar 		_opp_put_clknames(data->opp_table);
235711b9b663SViresh Kumar 
235811b9b663SViresh Kumar 	dev_pm_opp_put_opp_table(data->opp_table);
235911b9b663SViresh Kumar 	kfree(data);
236011b9b663SViresh Kumar }
236111b9b663SViresh Kumar 
236211b9b663SViresh Kumar /**
236311b9b663SViresh Kumar  * dev_pm_opp_set_config() - Set OPP configuration for the device.
236411b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
236511b9b663SViresh Kumar  * @config: OPP configuration.
236611b9b663SViresh Kumar  *
236711b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
236811b9b663SViresh Kumar  *
236911b9b663SViresh Kumar  * This must be called before any OPPs are initialized for the device. This may
237011b9b663SViresh Kumar  * be called multiple times for the same OPP table, for example once for each
237111b9b663SViresh Kumar  * CPU that share the same table. This must be balanced by the same number of
237211b9b663SViresh Kumar  * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
237311b9b663SViresh Kumar  *
237411b9b663SViresh Kumar  * This returns a token to the caller, which must be passed to
237511b9b663SViresh Kumar  * dev_pm_opp_clear_config() to free the resources later. The value of the
237611b9b663SViresh Kumar  * returned token will be >= 1 for success and negative for errors. The minimum
237711b9b663SViresh Kumar  * value of 1 is chosen here to make it easy for callers to manage the resource.
237811b9b663SViresh Kumar  */
237911b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
238011b9b663SViresh Kumar {
2381298098e5SViresh Kumar 	struct opp_table *opp_table;
238211b9b663SViresh Kumar 	struct opp_config_data *data;
238311b9b663SViresh Kumar 	unsigned int id;
238411b9b663SViresh Kumar 	int ret;
238511b9b663SViresh Kumar 
238611b9b663SViresh Kumar 	data = kmalloc(sizeof(*data), GFP_KERNEL);
238711b9b663SViresh Kumar 	if (!data)
238811b9b663SViresh Kumar 		return -ENOMEM;
238911b9b663SViresh Kumar 
239011b9b663SViresh Kumar 	opp_table = _add_opp_table(dev, false);
239111b9b663SViresh Kumar 	if (IS_ERR(opp_table)) {
239211b9b663SViresh Kumar 		kfree(data);
239311b9b663SViresh Kumar 		return PTR_ERR(opp_table);
239411b9b663SViresh Kumar 	}
239511b9b663SViresh Kumar 
239611b9b663SViresh Kumar 	data->opp_table = opp_table;
239711b9b663SViresh Kumar 	data->flags = 0;
239811b9b663SViresh Kumar 
239911b9b663SViresh Kumar 	/* This should be called before OPPs are initialized */
240011b9b663SViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
240111b9b663SViresh Kumar 		ret = -EBUSY;
240211b9b663SViresh Kumar 		goto err;
240311b9b663SViresh Kumar 	}
240411b9b663SViresh Kumar 
240511b9b663SViresh Kumar 	/* Configure clocks */
240611b9b663SViresh Kumar 	if (config->clk_names) {
24072083da24SViresh Kumar 		ret = _opp_set_clknames(opp_table, dev, config->clk_names,
24082083da24SViresh Kumar 					config->config_clks);
24092368f576SViresh Kumar 		if (ret)
241011b9b663SViresh Kumar 			goto err;
241111b9b663SViresh Kumar 
241211b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_CLK;
24132083da24SViresh Kumar 	} else if (config->config_clks) {
24142083da24SViresh Kumar 		/* Don't allow config callback without clocks */
24152083da24SViresh Kumar 		ret = -EINVAL;
24162083da24SViresh Kumar 		goto err;
241711b9b663SViresh Kumar 	}
241811b9b663SViresh Kumar 
241911b9b663SViresh Kumar 	/* Configure property names */
242011b9b663SViresh Kumar 	if (config->prop_name) {
2421298098e5SViresh Kumar 		ret = _opp_set_prop_name(opp_table, config->prop_name);
2422298098e5SViresh Kumar 		if (ret)
242311b9b663SViresh Kumar 			goto err;
242411b9b663SViresh Kumar 
242511b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_PROP_NAME;
242611b9b663SViresh Kumar 	}
242711b9b663SViresh Kumar 
2428aee3352fSViresh Kumar 	/* Configure config_regulators helper */
2429aee3352fSViresh Kumar 	if (config->config_regulators) {
2430aee3352fSViresh Kumar 		ret = _opp_set_config_regulators_helper(opp_table, dev,
2431aee3352fSViresh Kumar 						config->config_regulators);
2432aee3352fSViresh Kumar 		if (ret)
2433aee3352fSViresh Kumar 			goto err;
2434aee3352fSViresh Kumar 
2435aee3352fSViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR_HELPER;
2436aee3352fSViresh Kumar 	}
2437aee3352fSViresh Kumar 
243811b9b663SViresh Kumar 	/* Configure supported hardware */
243911b9b663SViresh Kumar 	if (config->supported_hw) {
244089f03984SViresh Kumar 		ret = _opp_set_supported_hw(opp_table, config->supported_hw,
244111b9b663SViresh Kumar 					    config->supported_hw_count);
244289f03984SViresh Kumar 		if (ret)
244311b9b663SViresh Kumar 			goto err;
244411b9b663SViresh Kumar 
244511b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_SUPPORTED_HW;
244611b9b663SViresh Kumar 	}
244711b9b663SViresh Kumar 
244811b9b663SViresh Kumar 	/* Configure supplies */
244911b9b663SViresh Kumar 	if (config->regulator_names) {
2450b0ec0942SViresh Kumar 		ret = _opp_set_regulators(opp_table, dev,
2451b0ec0942SViresh Kumar 					  config->regulator_names);
2452b0ec0942SViresh Kumar 		if (ret)
245311b9b663SViresh Kumar 			goto err;
245411b9b663SViresh Kumar 
245511b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_REGULATOR;
245611b9b663SViresh Kumar 	}
245711b9b663SViresh Kumar 
245811b9b663SViresh Kumar 	/* Attach genpds */
245911b9b663SViresh Kumar 	if (config->genpd_names) {
2460442e7a17SViresh Kumar 		ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
246111b9b663SViresh Kumar 					config->virt_devs);
2462442e7a17SViresh Kumar 		if (ret)
246311b9b663SViresh Kumar 			goto err;
246411b9b663SViresh Kumar 
246511b9b663SViresh Kumar 		data->flags |= OPP_CONFIG_GENPD;
246611b9b663SViresh Kumar 	}
246711b9b663SViresh Kumar 
246811b9b663SViresh Kumar 	ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
246911b9b663SViresh Kumar 		       GFP_KERNEL);
247011b9b663SViresh Kumar 	if (ret)
247111b9b663SViresh Kumar 		goto err;
247211b9b663SViresh Kumar 
247311b9b663SViresh Kumar 	return id;
247411b9b663SViresh Kumar 
247511b9b663SViresh Kumar err:
247611b9b663SViresh Kumar 	_opp_clear_config(data);
247711b9b663SViresh Kumar 	return ret;
247811b9b663SViresh Kumar }
247911b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
248011b9b663SViresh Kumar 
248111b9b663SViresh Kumar /**
248211b9b663SViresh Kumar  * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
248311b9b663SViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_config().
248411b9b663SViresh Kumar  *
248511b9b663SViresh Kumar  * This allows all device OPP configurations to be cleared at once. This must be
248611b9b663SViresh Kumar  * called once for each call made to dev_pm_opp_set_config(), in order to free
248711b9b663SViresh Kumar  * the OPPs properly.
248811b9b663SViresh Kumar  *
248911b9b663SViresh Kumar  * Currently the first call itself ends up freeing all the OPP configurations,
249011b9b663SViresh Kumar  * while the later ones only drop the OPP table reference. This works well for
249111b9b663SViresh Kumar  * now as we would never want to use an half initialized OPP table and want to
249211b9b663SViresh Kumar  * remove the configurations together.
249311b9b663SViresh Kumar  */
249411b9b663SViresh Kumar void dev_pm_opp_clear_config(int token)
249511b9b663SViresh Kumar {
249611b9b663SViresh Kumar 	struct opp_config_data *data;
249711b9b663SViresh Kumar 
249811b9b663SViresh Kumar 	/*
249911b9b663SViresh Kumar 	 * This lets the callers call this unconditionally and keep their code
250011b9b663SViresh Kumar 	 * simple.
250111b9b663SViresh Kumar 	 */
250211b9b663SViresh Kumar 	if (unlikely(token <= 0))
250311b9b663SViresh Kumar 		return;
250411b9b663SViresh Kumar 
250511b9b663SViresh Kumar 	data = xa_erase(&opp_configs, token);
250611b9b663SViresh Kumar 	if (WARN_ON(!data))
250711b9b663SViresh Kumar 		return;
250811b9b663SViresh Kumar 
250911b9b663SViresh Kumar 	_opp_clear_config(data);
251011b9b663SViresh Kumar }
251111b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
251211b9b663SViresh Kumar 
251311b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token)
251411b9b663SViresh Kumar {
251511b9b663SViresh Kumar 	dev_pm_opp_clear_config((unsigned long)token);
251611b9b663SViresh Kumar }
251711b9b663SViresh Kumar 
251811b9b663SViresh Kumar /**
251911b9b663SViresh Kumar  * devm_pm_opp_set_config() - Set OPP configuration for the device.
252011b9b663SViresh Kumar  * @dev: Device for which configuration is being set.
252111b9b663SViresh Kumar  * @config: OPP configuration.
252211b9b663SViresh Kumar  *
252311b9b663SViresh Kumar  * This allows all device OPP configurations to be performed at once.
252411b9b663SViresh Kumar  * This is a resource-managed variant of dev_pm_opp_set_config().
252511b9b663SViresh Kumar  *
252611b9b663SViresh Kumar  * Return: 0 on success and errorno otherwise.
252711b9b663SViresh Kumar  */
252811b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
252911b9b663SViresh Kumar {
253011b9b663SViresh Kumar 	int token = dev_pm_opp_set_config(dev, config);
253111b9b663SViresh Kumar 
253211b9b663SViresh Kumar 	if (token < 0)
253311b9b663SViresh Kumar 		return token;
253411b9b663SViresh Kumar 
253511b9b663SViresh Kumar 	return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
253611b9b663SViresh Kumar 					(void *) ((unsigned long) token));
253711b9b663SViresh Kumar }
253811b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
253911b9b663SViresh Kumar 
25404f018bc0SViresh Kumar /**
25417d8658efSSaravana Kannan  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
25427d8658efSSaravana Kannan  * @src_table: OPP table which has @dst_table as one of its required OPP table.
25437d8658efSSaravana Kannan  * @dst_table: Required OPP table of the @src_table.
25447d8658efSSaravana Kannan  * @src_opp: OPP from the @src_table.
25457d8658efSSaravana Kannan  *
25467d8658efSSaravana Kannan  * This function returns the OPP (present in @dst_table) pointed out by the
25477d8658efSSaravana Kannan  * "required-opps" property of the @src_opp (present in @src_table).
25487d8658efSSaravana Kannan  *
25497d8658efSSaravana Kannan  * The callers are required to call dev_pm_opp_put() for the returned OPP after
25507d8658efSSaravana Kannan  * use.
25517d8658efSSaravana Kannan  *
25527d8658efSSaravana Kannan  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
25537d8658efSSaravana Kannan  */
25547d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
25557d8658efSSaravana Kannan 						 struct opp_table *dst_table,
25567d8658efSSaravana Kannan 						 struct dev_pm_opp *src_opp)
25577d8658efSSaravana Kannan {
25587d8658efSSaravana Kannan 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
25597d8658efSSaravana Kannan 	int i;
25607d8658efSSaravana Kannan 
25617d8658efSSaravana Kannan 	if (!src_table || !dst_table || !src_opp ||
25627d8658efSSaravana Kannan 	    !src_table->required_opp_tables)
25637d8658efSSaravana Kannan 		return ERR_PTR(-EINVAL);
25647d8658efSSaravana Kannan 
25657d8658efSSaravana Kannan 	/* required-opps not fully initialized yet */
25667d8658efSSaravana Kannan 	if (lazy_linking_pending(src_table))
25677d8658efSSaravana Kannan 		return ERR_PTR(-EBUSY);
25687d8658efSSaravana Kannan 
25697d8658efSSaravana Kannan 	for (i = 0; i < src_table->required_opp_count; i++) {
25707d8658efSSaravana Kannan 		if (src_table->required_opp_tables[i] == dst_table) {
25717d8658efSSaravana Kannan 			mutex_lock(&src_table->lock);
25727d8658efSSaravana Kannan 
25737d8658efSSaravana Kannan 			list_for_each_entry(opp, &src_table->opp_list, node) {
25747d8658efSSaravana Kannan 				if (opp == src_opp) {
25757d8658efSSaravana Kannan 					dest_opp = opp->required_opps[i];
25767d8658efSSaravana Kannan 					dev_pm_opp_get(dest_opp);
25777d8658efSSaravana Kannan 					break;
25787d8658efSSaravana Kannan 				}
25797d8658efSSaravana Kannan 			}
25807d8658efSSaravana Kannan 
25817d8658efSSaravana Kannan 			mutex_unlock(&src_table->lock);
25827d8658efSSaravana Kannan 			break;
25837d8658efSSaravana Kannan 		}
25847d8658efSSaravana Kannan 	}
25857d8658efSSaravana Kannan 
25867d8658efSSaravana Kannan 	if (IS_ERR(dest_opp)) {
25877d8658efSSaravana Kannan 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
25887d8658efSSaravana Kannan 		       src_table, dst_table);
25897d8658efSSaravana Kannan 	}
25907d8658efSSaravana Kannan 
25917d8658efSSaravana Kannan 	return dest_opp;
25927d8658efSSaravana Kannan }
25937d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
25947d8658efSSaravana Kannan 
25957d8658efSSaravana Kannan /**
2596c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2597c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2598c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2599c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2600c8a59103SViresh Kumar  *
2601c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2602c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2603c8a59103SViresh Kumar  * performance state set to @pstate.
2604c8a59103SViresh Kumar  *
2605c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2606c8a59103SViresh Kumar  * value on errors.
2607c8a59103SViresh Kumar  */
2608c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2609c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2610c8a59103SViresh Kumar 				       unsigned int pstate)
2611c8a59103SViresh Kumar {
2612c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2613c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2614c8a59103SViresh Kumar 	int i;
2615c8a59103SViresh Kumar 
2616c8a59103SViresh Kumar 	/*
2617c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2618c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2619c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2620c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2621c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2622c8a59103SViresh Kumar 	 */
2623f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2624c8a59103SViresh Kumar 		return pstate;
2625c8a59103SViresh Kumar 
26267eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
26277eba0c76SViresh Kumar 	if (lazy_linking_pending(src_table))
26287eba0c76SViresh Kumar 		return -EBUSY;
26297eba0c76SViresh Kumar 
2630c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2631c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2632c8a59103SViresh Kumar 			break;
2633c8a59103SViresh Kumar 	}
2634c8a59103SViresh Kumar 
2635c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2636c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2637c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2638c8a59103SViresh Kumar 		return -EINVAL;
2639c8a59103SViresh Kumar 	}
2640c8a59103SViresh Kumar 
2641c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2642c8a59103SViresh Kumar 
2643c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
2644c8a59103SViresh Kumar 		if (opp->pstate == pstate) {
2645c8a59103SViresh Kumar 			dest_pstate = opp->required_opps[i]->pstate;
2646c8a59103SViresh Kumar 			goto unlock;
2647c8a59103SViresh Kumar 		}
2648c8a59103SViresh Kumar 	}
2649c8a59103SViresh Kumar 
2650c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2651c8a59103SViresh Kumar 	       dst_table);
2652c8a59103SViresh Kumar 
2653c8a59103SViresh Kumar unlock:
2654c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2655c8a59103SViresh Kumar 
2656c8a59103SViresh Kumar 	return dest_pstate;
2657c8a59103SViresh Kumar }
2658c8a59103SViresh Kumar 
2659c8a59103SViresh Kumar /**
26607813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
26617813dd6fSViresh Kumar  * @dev:	device for which we do this operation
26627813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
26637813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
26647813dd6fSViresh Kumar  *
26657813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
26667813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
26677813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
26687813dd6fSViresh Kumar  *
26697813dd6fSViresh Kumar  * Return:
26707813dd6fSViresh Kumar  * 0		On success OR
26717813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
26727813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
26737813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
26747813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
26757813dd6fSViresh Kumar  */
26767813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
26777813dd6fSViresh Kumar {
26787813dd6fSViresh Kumar 	struct opp_table *opp_table;
26797813dd6fSViresh Kumar 	int ret;
26807813dd6fSViresh Kumar 
268132439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2682dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2683dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
26847813dd6fSViresh Kumar 
268546f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
268646f48acaSViresh Kumar 	opp_table->regulator_count = 1;
268746f48acaSViresh Kumar 
26887813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
26890ad8c623SViresh Kumar 	if (ret)
26907813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
26910ad8c623SViresh Kumar 
26927813dd6fSViresh Kumar 	return ret;
26937813dd6fSViresh Kumar }
26947813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
26957813dd6fSViresh Kumar 
26967813dd6fSViresh Kumar /**
26977813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
26987813dd6fSViresh Kumar  * @dev:		device for which we do this operation
26997813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
27007813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
27017813dd6fSViresh Kumar  *
27027813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
27037813dd6fSViresh Kumar  * which is isolated here.
27047813dd6fSViresh Kumar  *
27057813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
27067813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
27077813dd6fSViresh Kumar  * successful.
27087813dd6fSViresh Kumar  */
27097813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
27107813dd6fSViresh Kumar 				 bool availability_req)
27117813dd6fSViresh Kumar {
27127813dd6fSViresh Kumar 	struct opp_table *opp_table;
27137813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
27147813dd6fSViresh Kumar 	int r = 0;
27157813dd6fSViresh Kumar 
27167813dd6fSViresh Kumar 	/* Find the opp_table */
27177813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
27187813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
27197813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
27207813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
27217813dd6fSViresh Kumar 		return r;
27227813dd6fSViresh Kumar 	}
27237813dd6fSViresh Kumar 
27247813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
27257813dd6fSViresh Kumar 
27267813dd6fSViresh Kumar 	/* Do we have the frequency? */
27277813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
27282083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
27297813dd6fSViresh Kumar 			opp = tmp_opp;
27307813dd6fSViresh Kumar 			break;
27317813dd6fSViresh Kumar 		}
27327813dd6fSViresh Kumar 	}
27337813dd6fSViresh Kumar 
27347813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
27357813dd6fSViresh Kumar 		r = PTR_ERR(opp);
27367813dd6fSViresh Kumar 		goto unlock;
27377813dd6fSViresh Kumar 	}
27387813dd6fSViresh Kumar 
27397813dd6fSViresh Kumar 	/* Is update really needed? */
27407813dd6fSViresh Kumar 	if (opp->available == availability_req)
27417813dd6fSViresh Kumar 		goto unlock;
27427813dd6fSViresh Kumar 
27437813dd6fSViresh Kumar 	opp->available = availability_req;
27447813dd6fSViresh Kumar 
27457813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
27467813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
27477813dd6fSViresh Kumar 
27487813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
27497813dd6fSViresh Kumar 	if (availability_req)
27507813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
27517813dd6fSViresh Kumar 					     opp);
27527813dd6fSViresh Kumar 	else
27537813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
27547813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
27557813dd6fSViresh Kumar 
27567813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
27577813dd6fSViresh Kumar 	goto put_table;
27587813dd6fSViresh Kumar 
27597813dd6fSViresh Kumar unlock:
27607813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
27617813dd6fSViresh Kumar put_table:
27627813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
27637813dd6fSViresh Kumar 	return r;
27647813dd6fSViresh Kumar }
27657813dd6fSViresh Kumar 
27667813dd6fSViresh Kumar /**
276725cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
276825cb20a2SStephen Boyd  * @dev:		device for which we do this operation
276925cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
277025cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
277125cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
277225cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
277325cb20a2SStephen Boyd  *
277425cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
277525cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
277625cb20a2SStephen Boyd  * successful.
277725cb20a2SStephen Boyd  */
277825cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
277925cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
278025cb20a2SStephen Boyd 			      unsigned long u_volt_max)
278125cb20a2SStephen Boyd 
278225cb20a2SStephen Boyd {
278325cb20a2SStephen Boyd 	struct opp_table *opp_table;
278425cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
278525cb20a2SStephen Boyd 	int r = 0;
278625cb20a2SStephen Boyd 
278725cb20a2SStephen Boyd 	/* Find the opp_table */
278825cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
278925cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
279025cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
279125cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
279225cb20a2SStephen Boyd 		return r;
279325cb20a2SStephen Boyd 	}
279425cb20a2SStephen Boyd 
279525cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
279625cb20a2SStephen Boyd 
279725cb20a2SStephen Boyd 	/* Do we have the frequency? */
279825cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
27992083da24SViresh Kumar 		if (tmp_opp->rates[0] == freq) {
280025cb20a2SStephen Boyd 			opp = tmp_opp;
280125cb20a2SStephen Boyd 			break;
280225cb20a2SStephen Boyd 		}
280325cb20a2SStephen Boyd 	}
280425cb20a2SStephen Boyd 
280525cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
280625cb20a2SStephen Boyd 		r = PTR_ERR(opp);
280725cb20a2SStephen Boyd 		goto adjust_unlock;
280825cb20a2SStephen Boyd 	}
280925cb20a2SStephen Boyd 
281025cb20a2SStephen Boyd 	/* Is update really needed? */
281125cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
281225cb20a2SStephen Boyd 		goto adjust_unlock;
281325cb20a2SStephen Boyd 
281425cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
281525cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
281625cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
281725cb20a2SStephen Boyd 
281825cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
281925cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
282025cb20a2SStephen Boyd 
282125cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
282225cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
282325cb20a2SStephen Boyd 				     opp);
282425cb20a2SStephen Boyd 
282525cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
282625cb20a2SStephen Boyd 	goto adjust_put_table;
282725cb20a2SStephen Boyd 
282825cb20a2SStephen Boyd adjust_unlock:
282925cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
283025cb20a2SStephen Boyd adjust_put_table:
283125cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
283225cb20a2SStephen Boyd 	return r;
283325cb20a2SStephen Boyd }
283403649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
283525cb20a2SStephen Boyd 
283625cb20a2SStephen Boyd /**
28377813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
28387813dd6fSViresh Kumar  * @dev:	device for which we do this operation
28397813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
28407813dd6fSViresh Kumar  *
28417813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
28427813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
28437813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
28447813dd6fSViresh Kumar  *
28457813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
28467813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
28477813dd6fSViresh Kumar  * successful.
28487813dd6fSViresh Kumar  */
28497813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
28507813dd6fSViresh Kumar {
28517813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
28527813dd6fSViresh Kumar }
28537813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
28547813dd6fSViresh Kumar 
28557813dd6fSViresh Kumar /**
28567813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
28577813dd6fSViresh Kumar  * @dev:	device for which we do this operation
28587813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
28597813dd6fSViresh Kumar  *
28607813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
28617813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
28627813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
28637813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
28647813dd6fSViresh Kumar  *
28657813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
28667813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
28677813dd6fSViresh Kumar  * successful.
28687813dd6fSViresh Kumar  */
28697813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
28707813dd6fSViresh Kumar {
28717813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
28727813dd6fSViresh Kumar }
28737813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
28747813dd6fSViresh Kumar 
28757813dd6fSViresh Kumar /**
28767813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
28777813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
28787813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
28797813dd6fSViresh Kumar  *
28807813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
28817813dd6fSViresh Kumar  */
28827813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
28837813dd6fSViresh Kumar {
28847813dd6fSViresh Kumar 	struct opp_table *opp_table;
28857813dd6fSViresh Kumar 	int ret;
28867813dd6fSViresh Kumar 
28877813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
28887813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
28897813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
28907813dd6fSViresh Kumar 
28917813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
28927813dd6fSViresh Kumar 
28937813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
28947813dd6fSViresh Kumar 
28957813dd6fSViresh Kumar 	return ret;
28967813dd6fSViresh Kumar }
28977813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
28987813dd6fSViresh Kumar 
28997813dd6fSViresh Kumar /**
29007813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
29017813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
29027813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
29037813dd6fSViresh Kumar  *
29047813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
29057813dd6fSViresh Kumar  */
29067813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
29077813dd6fSViresh Kumar 				   struct notifier_block *nb)
29087813dd6fSViresh Kumar {
29097813dd6fSViresh Kumar 	struct opp_table *opp_table;
29107813dd6fSViresh Kumar 	int ret;
29117813dd6fSViresh Kumar 
29127813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29137813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
29147813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
29157813dd6fSViresh Kumar 
29167813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
29177813dd6fSViresh Kumar 
29187813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29197813dd6fSViresh Kumar 
29207813dd6fSViresh Kumar 	return ret;
29217813dd6fSViresh Kumar }
29227813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
29237813dd6fSViresh Kumar 
29248aaf6264SViresh Kumar /**
29258aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
29268aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
29278aaf6264SViresh Kumar  *
29288aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
29298aaf6264SViresh Kumar  * dynamically added entries.
29308aaf6264SViresh Kumar  */
29318aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
29327813dd6fSViresh Kumar {
29337813dd6fSViresh Kumar 	struct opp_table *opp_table;
29347813dd6fSViresh Kumar 
29357813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
29367813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
29377813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
29387813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
29397813dd6fSViresh Kumar 
29407813dd6fSViresh Kumar 		if (error != -ENODEV)
29417813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
29427813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
29437813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
29447813dd6fSViresh Kumar 			     error);
29457813dd6fSViresh Kumar 		return;
29467813dd6fSViresh Kumar 	}
29477813dd6fSViresh Kumar 
2948922ff075SViresh Kumar 	/*
2949922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
2950922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
2951922ff075SViresh Kumar 	 **/
2952922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
2953cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
2954cdd6ed90SViresh Kumar 
2955922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
29567813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29577813dd6fSViresh Kumar }
29587813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
2959ce8073d8SDmitry Osipenko 
2960ce8073d8SDmitry Osipenko /**
2961ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2962ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
2963ce8073d8SDmitry Osipenko  *
2964ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
2965ce8073d8SDmitry Osipenko  *
2966ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
2967ce8073d8SDmitry Osipenko  */
2968ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
2969ce8073d8SDmitry Osipenko {
2970ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
2971ce8073d8SDmitry Osipenko 	struct regulator *reg;
2972ce8073d8SDmitry Osipenko 	int i, ret = 0;
2973ce8073d8SDmitry Osipenko 
2974ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
2975ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
2976ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
2977ce8073d8SDmitry Osipenko 		return 0;
2978ce8073d8SDmitry Osipenko 
2979ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
2980ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
2981ce8073d8SDmitry Osipenko 		goto put_table;
2982ce8073d8SDmitry Osipenko 
2983ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
2984ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
2985ce8073d8SDmitry Osipenko 		goto put_table;
2986ce8073d8SDmitry Osipenko 
2987ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
2988ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
2989ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
2990ce8073d8SDmitry Osipenko 		if (ret)
2991ce8073d8SDmitry Osipenko 			break;
2992ce8073d8SDmitry Osipenko 	}
2993ce8073d8SDmitry Osipenko put_table:
2994ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
2995ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
2996ce8073d8SDmitry Osipenko 
2997ce8073d8SDmitry Osipenko 	return ret;
2998ce8073d8SDmitry Osipenko }
2999ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
3000