xref: /openbmc/linux/drivers/opp/core.c (revision 7e535993fa4f671dbbd7fd95c93ce90181ddd9e1)
17813dd6fSViresh Kumar /*
27813dd6fSViresh Kumar  * Generic OPP Interface
37813dd6fSViresh Kumar  *
47813dd6fSViresh Kumar  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
57813dd6fSViresh Kumar  *	Nishanth Menon
67813dd6fSViresh Kumar  *	Romit Dasgupta
77813dd6fSViresh Kumar  *	Kevin Hilman
87813dd6fSViresh Kumar  *
97813dd6fSViresh Kumar  * This program is free software; you can redistribute it and/or modify
107813dd6fSViresh Kumar  * it under the terms of the GNU General Public License version 2 as
117813dd6fSViresh Kumar  * published by the Free Software Foundation.
127813dd6fSViresh Kumar  */
137813dd6fSViresh Kumar 
147813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
157813dd6fSViresh Kumar 
167813dd6fSViresh Kumar #include <linux/clk.h>
177813dd6fSViresh Kumar #include <linux/errno.h>
187813dd6fSViresh Kumar #include <linux/err.h>
197813dd6fSViresh Kumar #include <linux/slab.h>
207813dd6fSViresh Kumar #include <linux/device.h>
217813dd6fSViresh Kumar #include <linux/export.h>
22009acd19SViresh Kumar #include <linux/pm_domain.h>
237813dd6fSViresh Kumar #include <linux/regulator/consumer.h>
247813dd6fSViresh Kumar 
257813dd6fSViresh Kumar #include "opp.h"
267813dd6fSViresh Kumar 
277813dd6fSViresh Kumar /*
287813dd6fSViresh Kumar  * The root of the list of all opp-tables. All opp_table structures branch off
297813dd6fSViresh Kumar  * from here, with each opp_table containing the list of opps it supports in
307813dd6fSViresh Kumar  * various states of availability.
317813dd6fSViresh Kumar  */
327813dd6fSViresh Kumar LIST_HEAD(opp_tables);
337813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
347813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
357813dd6fSViresh Kumar 
367813dd6fSViresh Kumar static struct opp_device *_find_opp_dev(const struct device *dev,
377813dd6fSViresh Kumar 					struct opp_table *opp_table)
387813dd6fSViresh Kumar {
397813dd6fSViresh Kumar 	struct opp_device *opp_dev;
407813dd6fSViresh Kumar 
417813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
427813dd6fSViresh Kumar 		if (opp_dev->dev == dev)
437813dd6fSViresh Kumar 			return opp_dev;
447813dd6fSViresh Kumar 
457813dd6fSViresh Kumar 	return NULL;
467813dd6fSViresh Kumar }
477813dd6fSViresh Kumar 
487813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
497813dd6fSViresh Kumar {
507813dd6fSViresh Kumar 	struct opp_table *opp_table;
513d255699SViresh Kumar 	bool found;
527813dd6fSViresh Kumar 
537813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
543d255699SViresh Kumar 		mutex_lock(&opp_table->lock);
553d255699SViresh Kumar 		found = !!_find_opp_dev(dev, opp_table);
563d255699SViresh Kumar 		mutex_unlock(&opp_table->lock);
573d255699SViresh Kumar 
583d255699SViresh Kumar 		if (found) {
597813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
607813dd6fSViresh Kumar 
617813dd6fSViresh Kumar 			return opp_table;
627813dd6fSViresh Kumar 		}
637813dd6fSViresh Kumar 	}
647813dd6fSViresh Kumar 
657813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
667813dd6fSViresh Kumar }
677813dd6fSViresh Kumar 
687813dd6fSViresh Kumar /**
697813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
707813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
717813dd6fSViresh Kumar  *
727813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
737813dd6fSViresh Kumar  *
747813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
757813dd6fSViresh Kumar  * -EINVAL based on type of error.
767813dd6fSViresh Kumar  *
777813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
787813dd6fSViresh Kumar  */
797813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
807813dd6fSViresh Kumar {
817813dd6fSViresh Kumar 	struct opp_table *opp_table;
827813dd6fSViresh Kumar 
837813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
847813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
857813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
867813dd6fSViresh Kumar 	}
877813dd6fSViresh Kumar 
887813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
897813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
907813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
917813dd6fSViresh Kumar 
927813dd6fSViresh Kumar 	return opp_table;
937813dd6fSViresh Kumar }
947813dd6fSViresh Kumar 
957813dd6fSViresh Kumar /**
967813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
977813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
987813dd6fSViresh Kumar  *
997813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
1007813dd6fSViresh Kumar  * return 0
1017813dd6fSViresh Kumar  *
1027813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1037813dd6fSViresh Kumar  */
1047813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1057813dd6fSViresh Kumar {
1067813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1077813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1087813dd6fSViresh Kumar 		return 0;
1097813dd6fSViresh Kumar 	}
1107813dd6fSViresh Kumar 
1117813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1127813dd6fSViresh Kumar }
1137813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1147813dd6fSViresh Kumar 
1157813dd6fSViresh Kumar /**
1167813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1177813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1187813dd6fSViresh Kumar  *
1197813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1207813dd6fSViresh Kumar  * return 0
1217813dd6fSViresh Kumar  */
1227813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1237813dd6fSViresh Kumar {
1247813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1257813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1267813dd6fSViresh Kumar 		return 0;
1277813dd6fSViresh Kumar 	}
1287813dd6fSViresh Kumar 
1297813dd6fSViresh Kumar 	return opp->rate;
1307813dd6fSViresh Kumar }
1317813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
1327813dd6fSViresh Kumar 
1337813dd6fSViresh Kumar /**
1347813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
1357813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
1367813dd6fSViresh Kumar  *
1377813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
1387813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
1397813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
1407813dd6fSViresh Kumar  *
1417813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
1427813dd6fSViresh Kumar  */
1437813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
1447813dd6fSViresh Kumar {
1457813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1467813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1477813dd6fSViresh Kumar 		return false;
1487813dd6fSViresh Kumar 	}
1497813dd6fSViresh Kumar 
1507813dd6fSViresh Kumar 	return opp->turbo;
1517813dd6fSViresh Kumar }
1527813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
1537813dd6fSViresh Kumar 
1547813dd6fSViresh Kumar /**
1557813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
1567813dd6fSViresh Kumar  * @dev:	device for which we do this operation
1577813dd6fSViresh Kumar  *
1587813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
1597813dd6fSViresh Kumar  */
1607813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
1617813dd6fSViresh Kumar {
1627813dd6fSViresh Kumar 	struct opp_table *opp_table;
1637813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
1647813dd6fSViresh Kumar 
1657813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
1667813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
1677813dd6fSViresh Kumar 		return 0;
1687813dd6fSViresh Kumar 
1697813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
1707813dd6fSViresh Kumar 
1717813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1727813dd6fSViresh Kumar 
1737813dd6fSViresh Kumar 	return clock_latency_ns;
1747813dd6fSViresh Kumar }
1757813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
1767813dd6fSViresh Kumar 
1777813dd6fSViresh Kumar /**
1787813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
1797813dd6fSViresh Kumar  * @dev: device for which we do this operation
1807813dd6fSViresh Kumar  *
1817813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
1827813dd6fSViresh Kumar  */
1837813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
1847813dd6fSViresh Kumar {
1857813dd6fSViresh Kumar 	struct opp_table *opp_table;
1867813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
1877813dd6fSViresh Kumar 	struct regulator *reg;
1887813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
1897813dd6fSViresh Kumar 	int ret, i, count;
1907813dd6fSViresh Kumar 	struct {
1917813dd6fSViresh Kumar 		unsigned long min;
1927813dd6fSViresh Kumar 		unsigned long max;
1937813dd6fSViresh Kumar 	} *uV;
1947813dd6fSViresh Kumar 
1957813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
1967813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
1977813dd6fSViresh Kumar 		return 0;
1987813dd6fSViresh Kumar 
1997813dd6fSViresh Kumar 	count = opp_table->regulator_count;
2007813dd6fSViresh Kumar 
2017813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
2027813dd6fSViresh Kumar 	if (!count)
2037813dd6fSViresh Kumar 		goto put_opp_table;
2047813dd6fSViresh Kumar 
2057813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
2067813dd6fSViresh Kumar 	if (!uV)
2077813dd6fSViresh Kumar 		goto put_opp_table;
2087813dd6fSViresh Kumar 
2097813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
2107813dd6fSViresh Kumar 
2117813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2127813dd6fSViresh Kumar 		uV[i].min = ~0;
2137813dd6fSViresh Kumar 		uV[i].max = 0;
2147813dd6fSViresh Kumar 
2157813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
2167813dd6fSViresh Kumar 			if (!opp->available)
2177813dd6fSViresh Kumar 				continue;
2187813dd6fSViresh Kumar 
2197813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
2207813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
2217813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
2227813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
2237813dd6fSViresh Kumar 		}
2247813dd6fSViresh Kumar 	}
2257813dd6fSViresh Kumar 
2267813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
2277813dd6fSViresh Kumar 
2287813dd6fSViresh Kumar 	/*
2297813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
2307813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
2317813dd6fSViresh Kumar 	 */
2327813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2337813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
2347813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
2357813dd6fSViresh Kumar 		if (ret > 0)
2367813dd6fSViresh Kumar 			latency_ns += ret * 1000;
2377813dd6fSViresh Kumar 	}
2387813dd6fSViresh Kumar 
2397813dd6fSViresh Kumar 	kfree(uV);
2407813dd6fSViresh Kumar put_opp_table:
2417813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2427813dd6fSViresh Kumar 
2437813dd6fSViresh Kumar 	return latency_ns;
2447813dd6fSViresh Kumar }
2457813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
2467813dd6fSViresh Kumar 
2477813dd6fSViresh Kumar /**
2487813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
2497813dd6fSViresh Kumar  *					     nanoseconds
2507813dd6fSViresh Kumar  * @dev: device for which we do this operation
2517813dd6fSViresh Kumar  *
2527813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
2537813dd6fSViresh Kumar  * switch from one OPP to other.
2547813dd6fSViresh Kumar  */
2557813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
2567813dd6fSViresh Kumar {
2577813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
2587813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
2597813dd6fSViresh Kumar }
2607813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
2617813dd6fSViresh Kumar 
2627813dd6fSViresh Kumar /**
2637813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
2647813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2657813dd6fSViresh Kumar  *
2667813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
2677813dd6fSViresh Kumar  * if one is available, else returns 0;
2687813dd6fSViresh Kumar  */
2697813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
2707813dd6fSViresh Kumar {
2717813dd6fSViresh Kumar 	struct opp_table *opp_table;
2727813dd6fSViresh Kumar 	unsigned long freq = 0;
2737813dd6fSViresh Kumar 
2747813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2757813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2767813dd6fSViresh Kumar 		return 0;
2777813dd6fSViresh Kumar 
2787813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
2797813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
2807813dd6fSViresh Kumar 
2817813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2827813dd6fSViresh Kumar 
2837813dd6fSViresh Kumar 	return freq;
2847813dd6fSViresh Kumar }
2857813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
2867813dd6fSViresh Kumar 
287a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
288a1e8c136SViresh Kumar {
289a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
290a1e8c136SViresh Kumar 	int count = 0;
291a1e8c136SViresh Kumar 
292a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
293a1e8c136SViresh Kumar 
294a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
295a1e8c136SViresh Kumar 		if (opp->available)
296a1e8c136SViresh Kumar 			count++;
297a1e8c136SViresh Kumar 	}
298a1e8c136SViresh Kumar 
299a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
300a1e8c136SViresh Kumar 
301a1e8c136SViresh Kumar 	return count;
302a1e8c136SViresh Kumar }
303a1e8c136SViresh Kumar 
3047813dd6fSViresh Kumar /**
3057813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
3067813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3077813dd6fSViresh Kumar  *
3087813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
3097813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
3107813dd6fSViresh Kumar  */
3117813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
3127813dd6fSViresh Kumar {
3137813dd6fSViresh Kumar 	struct opp_table *opp_table;
314a1e8c136SViresh Kumar 	int count;
3157813dd6fSViresh Kumar 
3167813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3177813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
3187813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
319035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
3207813dd6fSViresh Kumar 			__func__, count);
32109f662f9SViresh Kumar 		return count;
3227813dd6fSViresh Kumar 	}
3237813dd6fSViresh Kumar 
324a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
3257813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3267813dd6fSViresh Kumar 
3277813dd6fSViresh Kumar 	return count;
3287813dd6fSViresh Kumar }
3297813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
3307813dd6fSViresh Kumar 
3317813dd6fSViresh Kumar /**
3327813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
3337813dd6fSViresh Kumar  * @dev:		device for which we do this operation
3347813dd6fSViresh Kumar  * @freq:		frequency to search for
3357813dd6fSViresh Kumar  * @available:		true/false - match for available opp
3367813dd6fSViresh Kumar  *
3377813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
3387813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
3397813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
3407813dd6fSViresh Kumar  * EINVAL:	for bad pointer
3417813dd6fSViresh Kumar  * ERANGE:	no match found for search
3427813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
3437813dd6fSViresh Kumar  *
3447813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
3457813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
3467813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
3477813dd6fSViresh Kumar  *
3487813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
3497813dd6fSViresh Kumar  * or the opposite as well.
3507813dd6fSViresh Kumar  *
3517813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
3527813dd6fSViresh Kumar  * use.
3537813dd6fSViresh Kumar  */
3547813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
3557813dd6fSViresh Kumar 					      unsigned long freq,
3567813dd6fSViresh Kumar 					      bool available)
3577813dd6fSViresh Kumar {
3587813dd6fSViresh Kumar 	struct opp_table *opp_table;
3597813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
3607813dd6fSViresh Kumar 
3617813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3627813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
3637813dd6fSViresh Kumar 		int r = PTR_ERR(opp_table);
3647813dd6fSViresh Kumar 
3657813dd6fSViresh Kumar 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
3667813dd6fSViresh Kumar 		return ERR_PTR(r);
3677813dd6fSViresh Kumar 	}
3687813dd6fSViresh Kumar 
3697813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
3707813dd6fSViresh Kumar 
3717813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
3727813dd6fSViresh Kumar 		if (temp_opp->available == available &&
3737813dd6fSViresh Kumar 				temp_opp->rate == freq) {
3747813dd6fSViresh Kumar 			opp = temp_opp;
3757813dd6fSViresh Kumar 
3767813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
3777813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
3787813dd6fSViresh Kumar 			break;
3797813dd6fSViresh Kumar 		}
3807813dd6fSViresh Kumar 	}
3817813dd6fSViresh Kumar 
3827813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
3837813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3847813dd6fSViresh Kumar 
3857813dd6fSViresh Kumar 	return opp;
3867813dd6fSViresh Kumar }
3877813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
3887813dd6fSViresh Kumar 
3897813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
3907813dd6fSViresh Kumar 						   unsigned long *freq)
3917813dd6fSViresh Kumar {
3927813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
3937813dd6fSViresh Kumar 
3947813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
3957813dd6fSViresh Kumar 
3967813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
3977813dd6fSViresh Kumar 		if (temp_opp->available && temp_opp->rate >= *freq) {
3987813dd6fSViresh Kumar 			opp = temp_opp;
3997813dd6fSViresh Kumar 			*freq = opp->rate;
4007813dd6fSViresh Kumar 
4017813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
4027813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
4037813dd6fSViresh Kumar 			break;
4047813dd6fSViresh Kumar 		}
4057813dd6fSViresh Kumar 	}
4067813dd6fSViresh Kumar 
4077813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
4087813dd6fSViresh Kumar 
4097813dd6fSViresh Kumar 	return opp;
4107813dd6fSViresh Kumar }
4117813dd6fSViresh Kumar 
4127813dd6fSViresh Kumar /**
4137813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
4147813dd6fSViresh Kumar  * @dev:	device for which we do this operation
4157813dd6fSViresh Kumar  * @freq:	Start frequency
4167813dd6fSViresh Kumar  *
4177813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
4187813dd6fSViresh Kumar  * for a device.
4197813dd6fSViresh Kumar  *
4207813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
4217813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
4227813dd6fSViresh Kumar  * values can be:
4237813dd6fSViresh Kumar  * EINVAL:	for bad pointer
4247813dd6fSViresh Kumar  * ERANGE:	no match found for search
4257813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
4267813dd6fSViresh Kumar  *
4277813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
4287813dd6fSViresh Kumar  * use.
4297813dd6fSViresh Kumar  */
4307813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
4317813dd6fSViresh Kumar 					     unsigned long *freq)
4327813dd6fSViresh Kumar {
4337813dd6fSViresh Kumar 	struct opp_table *opp_table;
4347813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
4357813dd6fSViresh Kumar 
4367813dd6fSViresh Kumar 	if (!dev || !freq) {
4377813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
4387813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
4397813dd6fSViresh Kumar 	}
4407813dd6fSViresh Kumar 
4417813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4427813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
4437813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
4447813dd6fSViresh Kumar 
4457813dd6fSViresh Kumar 	opp = _find_freq_ceil(opp_table, freq);
4467813dd6fSViresh Kumar 
4477813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4487813dd6fSViresh Kumar 
4497813dd6fSViresh Kumar 	return opp;
4507813dd6fSViresh Kumar }
4517813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
4527813dd6fSViresh Kumar 
4537813dd6fSViresh Kumar /**
4547813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
4557813dd6fSViresh Kumar  * @dev:	device for which we do this operation
4567813dd6fSViresh Kumar  * @freq:	Start frequency
4577813dd6fSViresh Kumar  *
4587813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
4597813dd6fSViresh Kumar  * for a device.
4607813dd6fSViresh Kumar  *
4617813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
4627813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
4637813dd6fSViresh Kumar  * values can be:
4647813dd6fSViresh Kumar  * EINVAL:	for bad pointer
4657813dd6fSViresh Kumar  * ERANGE:	no match found for search
4667813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
4677813dd6fSViresh Kumar  *
4687813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
4697813dd6fSViresh Kumar  * use.
4707813dd6fSViresh Kumar  */
4717813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
4727813dd6fSViresh Kumar 					      unsigned long *freq)
4737813dd6fSViresh Kumar {
4747813dd6fSViresh Kumar 	struct opp_table *opp_table;
4757813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
4767813dd6fSViresh Kumar 
4777813dd6fSViresh Kumar 	if (!dev || !freq) {
4787813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
4797813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
4807813dd6fSViresh Kumar 	}
4817813dd6fSViresh Kumar 
4827813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4837813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
4847813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
4857813dd6fSViresh Kumar 
4867813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
4877813dd6fSViresh Kumar 
4887813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
4897813dd6fSViresh Kumar 		if (temp_opp->available) {
4907813dd6fSViresh Kumar 			/* go to the next node, before choosing prev */
4917813dd6fSViresh Kumar 			if (temp_opp->rate > *freq)
4927813dd6fSViresh Kumar 				break;
4937813dd6fSViresh Kumar 			else
4947813dd6fSViresh Kumar 				opp = temp_opp;
4957813dd6fSViresh Kumar 		}
4967813dd6fSViresh Kumar 	}
4977813dd6fSViresh Kumar 
4987813dd6fSViresh Kumar 	/* Increment the reference count of OPP */
4997813dd6fSViresh Kumar 	if (!IS_ERR(opp))
5007813dd6fSViresh Kumar 		dev_pm_opp_get(opp);
5017813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
5027813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
5037813dd6fSViresh Kumar 
5047813dd6fSViresh Kumar 	if (!IS_ERR(opp))
5057813dd6fSViresh Kumar 		*freq = opp->rate;
5067813dd6fSViresh Kumar 
5077813dd6fSViresh Kumar 	return opp;
5087813dd6fSViresh Kumar }
5097813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
5107813dd6fSViresh Kumar 
5117813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
5127813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
5137813dd6fSViresh Kumar {
5147813dd6fSViresh Kumar 	int ret;
5157813dd6fSViresh Kumar 
5167813dd6fSViresh Kumar 	/* Regulator not available for device */
5177813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
5187813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
5197813dd6fSViresh Kumar 			PTR_ERR(reg));
5207813dd6fSViresh Kumar 		return 0;
5217813dd6fSViresh Kumar 	}
5227813dd6fSViresh Kumar 
5237813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
5247813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
5257813dd6fSViresh Kumar 
5267813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
5277813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
5287813dd6fSViresh Kumar 	if (ret)
5297813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
5307813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
5317813dd6fSViresh Kumar 			supply->u_volt_max, ret);
5327813dd6fSViresh Kumar 
5337813dd6fSViresh Kumar 	return ret;
5347813dd6fSViresh Kumar }
5357813dd6fSViresh Kumar 
5367813dd6fSViresh Kumar static inline int
5377813dd6fSViresh Kumar _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
5387813dd6fSViresh Kumar 			  unsigned long old_freq, unsigned long freq)
5397813dd6fSViresh Kumar {
5407813dd6fSViresh Kumar 	int ret;
5417813dd6fSViresh Kumar 
5427813dd6fSViresh Kumar 	ret = clk_set_rate(clk, freq);
5437813dd6fSViresh Kumar 	if (ret) {
5447813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
5457813dd6fSViresh Kumar 			ret);
5467813dd6fSViresh Kumar 	}
5477813dd6fSViresh Kumar 
5487813dd6fSViresh Kumar 	return ret;
5497813dd6fSViresh Kumar }
5507813dd6fSViresh Kumar 
551009acd19SViresh Kumar static inline int
552009acd19SViresh Kumar _generic_set_opp_domain(struct device *dev, struct clk *clk,
553009acd19SViresh Kumar 			unsigned long old_freq, unsigned long freq,
554009acd19SViresh Kumar 			unsigned int old_pstate, unsigned int new_pstate)
555009acd19SViresh Kumar {
556009acd19SViresh Kumar 	int ret;
557009acd19SViresh Kumar 
558009acd19SViresh Kumar 	/* Scaling up? Scale domain performance state before frequency */
559009acd19SViresh Kumar 	if (freq > old_freq) {
560009acd19SViresh Kumar 		ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
561009acd19SViresh Kumar 		if (ret)
562009acd19SViresh Kumar 			return ret;
563009acd19SViresh Kumar 	}
564009acd19SViresh Kumar 
565009acd19SViresh Kumar 	ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
566009acd19SViresh Kumar 	if (ret)
567009acd19SViresh Kumar 		goto restore_domain_state;
568009acd19SViresh Kumar 
569009acd19SViresh Kumar 	/* Scaling down? Scale domain performance state after frequency */
570009acd19SViresh Kumar 	if (freq < old_freq) {
571009acd19SViresh Kumar 		ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
572009acd19SViresh Kumar 		if (ret)
573009acd19SViresh Kumar 			goto restore_freq;
574009acd19SViresh Kumar 	}
575009acd19SViresh Kumar 
576009acd19SViresh Kumar 	return 0;
577009acd19SViresh Kumar 
578009acd19SViresh Kumar restore_freq:
579009acd19SViresh Kumar 	if (_generic_set_opp_clk_only(dev, clk, freq, old_freq))
580009acd19SViresh Kumar 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
581009acd19SViresh Kumar 			__func__, old_freq);
582009acd19SViresh Kumar restore_domain_state:
583009acd19SViresh Kumar 	if (freq > old_freq)
584009acd19SViresh Kumar 		dev_pm_genpd_set_performance_state(dev, old_pstate);
585009acd19SViresh Kumar 
586009acd19SViresh Kumar 	return ret;
587009acd19SViresh Kumar }
588009acd19SViresh Kumar 
5897813dd6fSViresh Kumar static int _generic_set_opp_regulator(const struct opp_table *opp_table,
5907813dd6fSViresh Kumar 				      struct device *dev,
5917813dd6fSViresh Kumar 				      unsigned long old_freq,
5927813dd6fSViresh Kumar 				      unsigned long freq,
5937813dd6fSViresh Kumar 				      struct dev_pm_opp_supply *old_supply,
5947813dd6fSViresh Kumar 				      struct dev_pm_opp_supply *new_supply)
5957813dd6fSViresh Kumar {
5967813dd6fSViresh Kumar 	struct regulator *reg = opp_table->regulators[0];
5977813dd6fSViresh Kumar 	int ret;
5987813dd6fSViresh Kumar 
5997813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
6007813dd6fSViresh Kumar 	if (WARN_ON(opp_table->regulator_count > 1)) {
6017813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
6027813dd6fSViresh Kumar 		return -EINVAL;
6037813dd6fSViresh Kumar 	}
6047813dd6fSViresh Kumar 
6057813dd6fSViresh Kumar 	/* Scaling up? Scale voltage before frequency */
606c5c2a97bSWaldemar Rymarkiewicz 	if (freq >= old_freq) {
6077813dd6fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, new_supply);
6087813dd6fSViresh Kumar 		if (ret)
6097813dd6fSViresh Kumar 			goto restore_voltage;
6107813dd6fSViresh Kumar 	}
6117813dd6fSViresh Kumar 
6127813dd6fSViresh Kumar 	/* Change frequency */
6137813dd6fSViresh Kumar 	ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq);
6147813dd6fSViresh Kumar 	if (ret)
6157813dd6fSViresh Kumar 		goto restore_voltage;
6167813dd6fSViresh Kumar 
6177813dd6fSViresh Kumar 	/* Scaling down? Scale voltage after frequency */
6187813dd6fSViresh Kumar 	if (freq < old_freq) {
6197813dd6fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, new_supply);
6207813dd6fSViresh Kumar 		if (ret)
6217813dd6fSViresh Kumar 			goto restore_freq;
6227813dd6fSViresh Kumar 	}
6237813dd6fSViresh Kumar 
6247813dd6fSViresh Kumar 	return 0;
6257813dd6fSViresh Kumar 
6267813dd6fSViresh Kumar restore_freq:
6277813dd6fSViresh Kumar 	if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq))
6287813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
6297813dd6fSViresh Kumar 			__func__, old_freq);
6307813dd6fSViresh Kumar restore_voltage:
6317813dd6fSViresh Kumar 	/* This shouldn't harm even if the voltages weren't updated earlier */
6327813dd6fSViresh Kumar 	if (old_supply)
6337813dd6fSViresh Kumar 		_set_opp_voltage(dev, reg, old_supply);
6347813dd6fSViresh Kumar 
6357813dd6fSViresh Kumar 	return ret;
6367813dd6fSViresh Kumar }
6377813dd6fSViresh Kumar 
638*7e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table,
639*7e535993SViresh Kumar 			   struct device *dev, unsigned long old_freq,
640*7e535993SViresh Kumar 			   unsigned long freq,
641*7e535993SViresh Kumar 			   struct dev_pm_opp_supply *old_supply,
642*7e535993SViresh Kumar 			   struct dev_pm_opp_supply *new_supply)
643*7e535993SViresh Kumar {
644*7e535993SViresh Kumar 	struct dev_pm_set_opp_data *data;
645*7e535993SViresh Kumar 	int size;
646*7e535993SViresh Kumar 
647*7e535993SViresh Kumar 	data = opp_table->set_opp_data;
648*7e535993SViresh Kumar 	data->regulators = opp_table->regulators;
649*7e535993SViresh Kumar 	data->regulator_count = opp_table->regulator_count;
650*7e535993SViresh Kumar 	data->clk = opp_table->clk;
651*7e535993SViresh Kumar 	data->dev = dev;
652*7e535993SViresh Kumar 
653*7e535993SViresh Kumar 	data->old_opp.rate = old_freq;
654*7e535993SViresh Kumar 	size = sizeof(*old_supply) * opp_table->regulator_count;
655*7e535993SViresh Kumar 	if (IS_ERR(old_supply))
656*7e535993SViresh Kumar 		memset(data->old_opp.supplies, 0, size);
657*7e535993SViresh Kumar 	else
658*7e535993SViresh Kumar 		memcpy(data->old_opp.supplies, old_supply, size);
659*7e535993SViresh Kumar 
660*7e535993SViresh Kumar 	data->new_opp.rate = freq;
661*7e535993SViresh Kumar 	memcpy(data->new_opp.supplies, new_supply, size);
662*7e535993SViresh Kumar 
663*7e535993SViresh Kumar 	return opp_table->set_opp(data);
664*7e535993SViresh Kumar }
665*7e535993SViresh Kumar 
6667813dd6fSViresh Kumar /**
6677813dd6fSViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
6687813dd6fSViresh Kumar  * @dev:	 device for which we do this operation
6697813dd6fSViresh Kumar  * @target_freq: frequency to achieve
6707813dd6fSViresh Kumar  *
6717813dd6fSViresh Kumar  * This configures the power-supplies and clock source to the levels specified
6727813dd6fSViresh Kumar  * by the OPP corresponding to the target_freq.
6737813dd6fSViresh Kumar  */
6747813dd6fSViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
6757813dd6fSViresh Kumar {
6767813dd6fSViresh Kumar 	struct opp_table *opp_table;
6777813dd6fSViresh Kumar 	unsigned long freq, old_freq;
6787813dd6fSViresh Kumar 	struct dev_pm_opp *old_opp, *opp;
6797813dd6fSViresh Kumar 	struct clk *clk;
680*7e535993SViresh Kumar 	int ret;
6817813dd6fSViresh Kumar 
6827813dd6fSViresh Kumar 	if (unlikely(!target_freq)) {
6837813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
6847813dd6fSViresh Kumar 			target_freq);
6857813dd6fSViresh Kumar 		return -EINVAL;
6867813dd6fSViresh Kumar 	}
6877813dd6fSViresh Kumar 
6887813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
6897813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
6907813dd6fSViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
6917813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
6927813dd6fSViresh Kumar 	}
6937813dd6fSViresh Kumar 
6947813dd6fSViresh Kumar 	clk = opp_table->clk;
6957813dd6fSViresh Kumar 	if (IS_ERR(clk)) {
6967813dd6fSViresh Kumar 		dev_err(dev, "%s: No clock available for the device\n",
6977813dd6fSViresh Kumar 			__func__);
6987813dd6fSViresh Kumar 		ret = PTR_ERR(clk);
6997813dd6fSViresh Kumar 		goto put_opp_table;
7007813dd6fSViresh Kumar 	}
7017813dd6fSViresh Kumar 
7027813dd6fSViresh Kumar 	freq = clk_round_rate(clk, target_freq);
7037813dd6fSViresh Kumar 	if ((long)freq <= 0)
7047813dd6fSViresh Kumar 		freq = target_freq;
7057813dd6fSViresh Kumar 
7067813dd6fSViresh Kumar 	old_freq = clk_get_rate(clk);
7077813dd6fSViresh Kumar 
7087813dd6fSViresh Kumar 	/* Return early if nothing to do */
7097813dd6fSViresh Kumar 	if (old_freq == freq) {
7107813dd6fSViresh Kumar 		dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
7117813dd6fSViresh Kumar 			__func__, freq);
7127813dd6fSViresh Kumar 		ret = 0;
7137813dd6fSViresh Kumar 		goto put_opp_table;
7147813dd6fSViresh Kumar 	}
7157813dd6fSViresh Kumar 
7167813dd6fSViresh Kumar 	old_opp = _find_freq_ceil(opp_table, &old_freq);
7177813dd6fSViresh Kumar 	if (IS_ERR(old_opp)) {
7187813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
7197813dd6fSViresh Kumar 			__func__, old_freq, PTR_ERR(old_opp));
7207813dd6fSViresh Kumar 	}
7217813dd6fSViresh Kumar 
7227813dd6fSViresh Kumar 	opp = _find_freq_ceil(opp_table, &freq);
7237813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
7247813dd6fSViresh Kumar 		ret = PTR_ERR(opp);
7257813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
7267813dd6fSViresh Kumar 			__func__, freq, ret);
7277813dd6fSViresh Kumar 		goto put_old_opp;
7287813dd6fSViresh Kumar 	}
7297813dd6fSViresh Kumar 
7307813dd6fSViresh Kumar 	dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
7317813dd6fSViresh Kumar 		old_freq, freq);
7327813dd6fSViresh Kumar 
733*7e535993SViresh Kumar 	if (opp_table->set_opp) {
734*7e535993SViresh Kumar 		ret = _set_opp_custom(opp_table, dev, old_freq, freq,
735*7e535993SViresh Kumar 				      IS_ERR(old_opp) ? NULL : old_opp->supplies,
736*7e535993SViresh Kumar 				      opp->supplies);
737*7e535993SViresh Kumar 	} else if (opp_table->regulators) {
738*7e535993SViresh Kumar 		ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
739*7e535993SViresh Kumar 						 IS_ERR(old_opp) ? NULL : old_opp->supplies,
740*7e535993SViresh Kumar 						 opp->supplies);
741*7e535993SViresh Kumar 	} else {
7427813dd6fSViresh Kumar 		/* Only frequency scaling */
743*7e535993SViresh Kumar 
744009acd19SViresh Kumar 		/*
745009acd19SViresh Kumar 		 * We don't support devices with both regulator and
746009acd19SViresh Kumar 		 * domain performance-state for now.
747009acd19SViresh Kumar 		 */
748009acd19SViresh Kumar 		if (opp_table->genpd_performance_state)
749009acd19SViresh Kumar 			ret = _generic_set_opp_domain(dev, clk, old_freq, freq,
750009acd19SViresh Kumar 						      IS_ERR(old_opp) ? 0 : old_opp->pstate,
751009acd19SViresh Kumar 						      opp->pstate);
752009acd19SViresh Kumar 		else
7537813dd6fSViresh Kumar 			ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
7547813dd6fSViresh Kumar 	}
7557813dd6fSViresh Kumar 
7567813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
7577813dd6fSViresh Kumar put_old_opp:
7587813dd6fSViresh Kumar 	if (!IS_ERR(old_opp))
7597813dd6fSViresh Kumar 		dev_pm_opp_put(old_opp);
7607813dd6fSViresh Kumar put_opp_table:
7617813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
7627813dd6fSViresh Kumar 	return ret;
7637813dd6fSViresh Kumar }
7647813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
7657813dd6fSViresh Kumar 
7667813dd6fSViresh Kumar /* OPP-dev Helpers */
7677813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
7687813dd6fSViresh Kumar 			    struct opp_table *opp_table)
7697813dd6fSViresh Kumar {
7707813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
7717813dd6fSViresh Kumar 	list_del(&opp_dev->node);
7727813dd6fSViresh Kumar 	kfree(opp_dev);
7737813dd6fSViresh Kumar }
7747813dd6fSViresh Kumar 
775283d55e6SViresh Kumar static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
7767813dd6fSViresh Kumar 						struct opp_table *opp_table)
7777813dd6fSViresh Kumar {
7787813dd6fSViresh Kumar 	struct opp_device *opp_dev;
7797813dd6fSViresh Kumar 	int ret;
7807813dd6fSViresh Kumar 
7817813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
7827813dd6fSViresh Kumar 	if (!opp_dev)
7837813dd6fSViresh Kumar 		return NULL;
7847813dd6fSViresh Kumar 
7857813dd6fSViresh Kumar 	/* Initialize opp-dev */
7867813dd6fSViresh Kumar 	opp_dev->dev = dev;
7873d255699SViresh Kumar 
7887813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
7897813dd6fSViresh Kumar 
7907813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
7917813dd6fSViresh Kumar 	ret = opp_debug_register(opp_dev, opp_table);
7927813dd6fSViresh Kumar 	if (ret)
7937813dd6fSViresh Kumar 		dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
7947813dd6fSViresh Kumar 			__func__, ret);
795283d55e6SViresh Kumar 
796283d55e6SViresh Kumar 	return opp_dev;
797283d55e6SViresh Kumar }
798283d55e6SViresh Kumar 
799283d55e6SViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
800283d55e6SViresh Kumar 				struct opp_table *opp_table)
801283d55e6SViresh Kumar {
802283d55e6SViresh Kumar 	struct opp_device *opp_dev;
803283d55e6SViresh Kumar 
804283d55e6SViresh Kumar 	mutex_lock(&opp_table->lock);
805283d55e6SViresh Kumar 	opp_dev = _add_opp_dev_unlocked(dev, opp_table);
8063d255699SViresh Kumar 	mutex_unlock(&opp_table->lock);
8077813dd6fSViresh Kumar 
8087813dd6fSViresh Kumar 	return opp_dev;
8097813dd6fSViresh Kumar }
8107813dd6fSViresh Kumar 
811eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
8127813dd6fSViresh Kumar {
8137813dd6fSViresh Kumar 	struct opp_table *opp_table;
8147813dd6fSViresh Kumar 	struct opp_device *opp_dev;
8157813dd6fSViresh Kumar 	int ret;
8167813dd6fSViresh Kumar 
8177813dd6fSViresh Kumar 	/*
8187813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
8197813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
8207813dd6fSViresh Kumar 	 */
8217813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
8227813dd6fSViresh Kumar 	if (!opp_table)
8237813dd6fSViresh Kumar 		return NULL;
8247813dd6fSViresh Kumar 
8253d255699SViresh Kumar 	mutex_init(&opp_table->lock);
8267813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
8277813dd6fSViresh Kumar 
8287813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
8297813dd6fSViresh Kumar 	if (!opp_dev) {
8307813dd6fSViresh Kumar 		kfree(opp_table);
8317813dd6fSViresh Kumar 		return NULL;
8327813dd6fSViresh Kumar 	}
8337813dd6fSViresh Kumar 
834eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
8357813dd6fSViresh Kumar 
8367813dd6fSViresh Kumar 	/* Find clk for the device */
8377813dd6fSViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
8387813dd6fSViresh Kumar 	if (IS_ERR(opp_table->clk)) {
8397813dd6fSViresh Kumar 		ret = PTR_ERR(opp_table->clk);
8407813dd6fSViresh Kumar 		if (ret != -EPROBE_DEFER)
8417813dd6fSViresh Kumar 			dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
8427813dd6fSViresh Kumar 				ret);
8437813dd6fSViresh Kumar 	}
8447813dd6fSViresh Kumar 
8457813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
8467813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
8477813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
8487813dd6fSViresh Kumar 
8497813dd6fSViresh Kumar 	/* Secure the device table modification */
8507813dd6fSViresh Kumar 	list_add(&opp_table->node, &opp_tables);
8517813dd6fSViresh Kumar 	return opp_table;
8527813dd6fSViresh Kumar }
8537813dd6fSViresh Kumar 
8547813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
8557813dd6fSViresh Kumar {
8567813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
8577813dd6fSViresh Kumar }
8587813dd6fSViresh Kumar 
859eb7c8743SViresh Kumar static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
8607813dd6fSViresh Kumar {
8617813dd6fSViresh Kumar 	struct opp_table *opp_table;
8627813dd6fSViresh Kumar 
8637813dd6fSViresh Kumar 	/* Hold our table modification lock here */
8647813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
8657813dd6fSViresh Kumar 
8667813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
8677813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
8687813dd6fSViresh Kumar 		goto unlock;
8697813dd6fSViresh Kumar 
870283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
871283d55e6SViresh Kumar 	if (opp_table) {
872283d55e6SViresh Kumar 		if (!_add_opp_dev_unlocked(dev, opp_table)) {
873283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
874283d55e6SViresh Kumar 			opp_table = NULL;
875283d55e6SViresh Kumar 		}
876283d55e6SViresh Kumar 		goto unlock;
877283d55e6SViresh Kumar 	}
878283d55e6SViresh Kumar 
879eb7c8743SViresh Kumar 	opp_table = _allocate_opp_table(dev, index);
8807813dd6fSViresh Kumar 
8817813dd6fSViresh Kumar unlock:
8827813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
8837813dd6fSViresh Kumar 
8847813dd6fSViresh Kumar 	return opp_table;
8857813dd6fSViresh Kumar }
886eb7c8743SViresh Kumar 
887eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
888eb7c8743SViresh Kumar {
889eb7c8743SViresh Kumar 	return _opp_get_opp_table(dev, 0);
890eb7c8743SViresh Kumar }
8917813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
8927813dd6fSViresh Kumar 
893eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
894eb7c8743SViresh Kumar 						   int index)
895eb7c8743SViresh Kumar {
896eb7c8743SViresh Kumar 	return _opp_get_opp_table(dev, index);
897eb7c8743SViresh Kumar }
898eb7c8743SViresh Kumar 
8997813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
9007813dd6fSViresh Kumar {
9017813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
902cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
9037813dd6fSViresh Kumar 
9047813dd6fSViresh Kumar 	/* Release clk */
9057813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
9067813dd6fSViresh Kumar 		clk_put(opp_table->clk);
9077813dd6fSViresh Kumar 
908cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
909cdd6ed90SViresh Kumar 
910cdd6ed90SViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
9113d255699SViresh Kumar 		/*
912cdd6ed90SViresh Kumar 		 * The OPP table is getting removed, drop the performance state
913cdd6ed90SViresh Kumar 		 * constraints.
9143d255699SViresh Kumar 		 */
915cdd6ed90SViresh Kumar 		if (opp_table->genpd_performance_state)
916cdd6ed90SViresh Kumar 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
9177813dd6fSViresh Kumar 
9187813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
919cdd6ed90SViresh Kumar 	}
9207813dd6fSViresh Kumar 
9217813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
9227813dd6fSViresh Kumar 	list_del(&opp_table->node);
9237813dd6fSViresh Kumar 	kfree(opp_table);
9247813dd6fSViresh Kumar 
9257813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
9267813dd6fSViresh Kumar }
9277813dd6fSViresh Kumar 
928d0e8ae6cSViresh Kumar void _opp_remove_all_static(struct opp_table *opp_table)
929d0e8ae6cSViresh Kumar {
930d0e8ae6cSViresh Kumar 	struct dev_pm_opp *opp, *tmp;
931d0e8ae6cSViresh Kumar 
932d0e8ae6cSViresh Kumar 	list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
933d0e8ae6cSViresh Kumar 		if (!opp->dynamic)
934d0e8ae6cSViresh Kumar 			dev_pm_opp_put(opp);
935d0e8ae6cSViresh Kumar 	}
936d0e8ae6cSViresh Kumar 
937d0e8ae6cSViresh Kumar 	opp_table->parsed_static_opps = false;
938d0e8ae6cSViresh Kumar }
939d0e8ae6cSViresh Kumar 
940d0e8ae6cSViresh Kumar static void _opp_table_list_kref_release(struct kref *kref)
941d0e8ae6cSViresh Kumar {
942d0e8ae6cSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table,
943d0e8ae6cSViresh Kumar 						   list_kref);
944d0e8ae6cSViresh Kumar 
945d0e8ae6cSViresh Kumar 	_opp_remove_all_static(opp_table);
946d0e8ae6cSViresh Kumar 	mutex_unlock(&opp_table_lock);
947d0e8ae6cSViresh Kumar }
948d0e8ae6cSViresh Kumar 
949d0e8ae6cSViresh Kumar void _put_opp_list_kref(struct opp_table *opp_table)
950d0e8ae6cSViresh Kumar {
951d0e8ae6cSViresh Kumar 	kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
952d0e8ae6cSViresh Kumar 		       &opp_table_lock);
953d0e8ae6cSViresh Kumar }
954d0e8ae6cSViresh Kumar 
9557813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
9567813dd6fSViresh Kumar {
9577813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
9587813dd6fSViresh Kumar 		       &opp_table_lock);
9597813dd6fSViresh Kumar }
9607813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
9617813dd6fSViresh Kumar 
9627813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
9637813dd6fSViresh Kumar {
9647813dd6fSViresh Kumar 	kfree(opp);
9657813dd6fSViresh Kumar }
9667813dd6fSViresh Kumar 
9677813dd6fSViresh Kumar static void _opp_kref_release(struct kref *kref)
9687813dd6fSViresh Kumar {
9697813dd6fSViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
9707813dd6fSViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
9717813dd6fSViresh Kumar 
9727813dd6fSViresh Kumar 	/*
9737813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
9747813dd6fSViresh Kumar 	 * frequency/voltage list.
9757813dd6fSViresh Kumar 	 */
9767813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
9777813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
9787813dd6fSViresh Kumar 	list_del(&opp->node);
9797813dd6fSViresh Kumar 	kfree(opp);
9807813dd6fSViresh Kumar 
9817813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
9827813dd6fSViresh Kumar }
9837813dd6fSViresh Kumar 
984a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
9857813dd6fSViresh Kumar {
9867813dd6fSViresh Kumar 	kref_get(&opp->kref);
9877813dd6fSViresh Kumar }
9887813dd6fSViresh Kumar 
9897813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
9907813dd6fSViresh Kumar {
9917813dd6fSViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
9927813dd6fSViresh Kumar }
9937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
9947813dd6fSViresh Kumar 
9957813dd6fSViresh Kumar /**
9967813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
9977813dd6fSViresh Kumar  * @dev:	device for which we do this operation
9987813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
9997813dd6fSViresh Kumar  *
10007813dd6fSViresh Kumar  * This function removes an opp from the opp table.
10017813dd6fSViresh Kumar  */
10027813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
10037813dd6fSViresh Kumar {
10047813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
10057813dd6fSViresh Kumar 	struct opp_table *opp_table;
10067813dd6fSViresh Kumar 	bool found = false;
10077813dd6fSViresh Kumar 
10087813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
10097813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
10107813dd6fSViresh Kumar 		return;
10117813dd6fSViresh Kumar 
10127813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
10137813dd6fSViresh Kumar 
10147813dd6fSViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
10157813dd6fSViresh Kumar 		if (opp->rate == freq) {
10167813dd6fSViresh Kumar 			found = true;
10177813dd6fSViresh Kumar 			break;
10187813dd6fSViresh Kumar 		}
10197813dd6fSViresh Kumar 	}
10207813dd6fSViresh Kumar 
10217813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
10227813dd6fSViresh Kumar 
10237813dd6fSViresh Kumar 	if (found) {
10247813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
10250ad8c623SViresh Kumar 
10260ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
10270ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
10287813dd6fSViresh Kumar 	} else {
10297813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
10307813dd6fSViresh Kumar 			 __func__, freq);
10317813dd6fSViresh Kumar 	}
10327813dd6fSViresh Kumar 
10330ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
10347813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
10357813dd6fSViresh Kumar }
10367813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
10377813dd6fSViresh Kumar 
10387813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table)
10397813dd6fSViresh Kumar {
10407813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
10417813dd6fSViresh Kumar 	int count, supply_size;
10427813dd6fSViresh Kumar 
10437813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
10447813dd6fSViresh Kumar 	count = table->regulator_count ? table->regulator_count : 1;
10457813dd6fSViresh Kumar 	supply_size = sizeof(*opp->supplies) * count;
10467813dd6fSViresh Kumar 
10477813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
10487813dd6fSViresh Kumar 	opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
10497813dd6fSViresh Kumar 	if (!opp)
10507813dd6fSViresh Kumar 		return NULL;
10517813dd6fSViresh Kumar 
10527813dd6fSViresh Kumar 	/* Put the supplies at the end of the OPP structure as an empty array */
10537813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
10547813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
10557813dd6fSViresh Kumar 
10567813dd6fSViresh Kumar 	return opp;
10577813dd6fSViresh Kumar }
10587813dd6fSViresh Kumar 
10597813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
10607813dd6fSViresh Kumar 					 struct opp_table *opp_table)
10617813dd6fSViresh Kumar {
10627813dd6fSViresh Kumar 	struct regulator *reg;
10637813dd6fSViresh Kumar 	int i;
10647813dd6fSViresh Kumar 
10657813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
10667813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
10677813dd6fSViresh Kumar 
10687813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
10697813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
10707813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
10717813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
10727813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
10737813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
10747813dd6fSViresh Kumar 			return false;
10757813dd6fSViresh Kumar 		}
10767813dd6fSViresh Kumar 	}
10777813dd6fSViresh Kumar 
10787813dd6fSViresh Kumar 	return true;
10797813dd6fSViresh Kumar }
10807813dd6fSViresh Kumar 
1081a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1082a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1083a1e8c136SViresh Kumar 			     struct list_head **head)
1084a1e8c136SViresh Kumar {
1085a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
1086a1e8c136SViresh Kumar 
1087a1e8c136SViresh Kumar 	/*
1088a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1089a1e8c136SViresh Kumar 	 * already present.
1090a1e8c136SViresh Kumar 	 *
1091a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1092a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1093a1e8c136SViresh Kumar 	 * loop.
1094a1e8c136SViresh Kumar 	 */
1095a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
1096a1e8c136SViresh Kumar 		if (new_opp->rate > opp->rate) {
1097a1e8c136SViresh Kumar 			*head = &opp->node;
1098a1e8c136SViresh Kumar 			continue;
1099a1e8c136SViresh Kumar 		}
1100a1e8c136SViresh Kumar 
1101a1e8c136SViresh Kumar 		if (new_opp->rate < opp->rate)
1102a1e8c136SViresh Kumar 			return 0;
1103a1e8c136SViresh Kumar 
1104a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1105a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1106a1e8c136SViresh Kumar 			 __func__, opp->rate, opp->supplies[0].u_volt,
1107a1e8c136SViresh Kumar 			 opp->available, new_opp->rate,
1108a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1109a1e8c136SViresh Kumar 
1110a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1111a1e8c136SViresh Kumar 		return opp->available &&
1112a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1113a1e8c136SViresh Kumar 	}
1114a1e8c136SViresh Kumar 
1115a1e8c136SViresh Kumar 	return 0;
1116a1e8c136SViresh Kumar }
1117a1e8c136SViresh Kumar 
11187813dd6fSViresh Kumar /*
11197813dd6fSViresh Kumar  * Returns:
11207813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
11217813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
11227813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
11237813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
11247813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
11257813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
11267813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
11277813dd6fSViresh Kumar  */
11287813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1129a1e8c136SViresh Kumar 	     struct opp_table *opp_table, bool rate_not_available)
11307813dd6fSViresh Kumar {
11317813dd6fSViresh Kumar 	struct list_head *head;
11327813dd6fSViresh Kumar 	int ret;
11337813dd6fSViresh Kumar 
11347813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
11357813dd6fSViresh Kumar 	head = &opp_table->opp_list;
11367813dd6fSViresh Kumar 
1137a1e8c136SViresh Kumar 	if (likely(!rate_not_available)) {
1138a1e8c136SViresh Kumar 		ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1139a1e8c136SViresh Kumar 		if (ret) {
11407813dd6fSViresh Kumar 			mutex_unlock(&opp_table->lock);
11417813dd6fSViresh Kumar 			return ret;
11427813dd6fSViresh Kumar 		}
1143a1e8c136SViresh Kumar 	}
11447813dd6fSViresh Kumar 
11457813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
11467813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
11477813dd6fSViresh Kumar 
11487813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
11497813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
11507813dd6fSViresh Kumar 
11517813dd6fSViresh Kumar 	ret = opp_debug_create_one(new_opp, opp_table);
11527813dd6fSViresh Kumar 	if (ret)
11537813dd6fSViresh Kumar 		dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
11547813dd6fSViresh Kumar 			__func__, ret);
11557813dd6fSViresh Kumar 
11567813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
11577813dd6fSViresh Kumar 		new_opp->available = false;
11587813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
11597813dd6fSViresh Kumar 			 __func__, new_opp->rate);
11607813dd6fSViresh Kumar 	}
11617813dd6fSViresh Kumar 
11627813dd6fSViresh Kumar 	return 0;
11637813dd6fSViresh Kumar }
11647813dd6fSViresh Kumar 
11657813dd6fSViresh Kumar /**
11667813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
11677813dd6fSViresh Kumar  * @opp_table:	OPP table
11687813dd6fSViresh Kumar  * @dev:	device for which we do this operation
11697813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
11707813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
11717813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
11727813dd6fSViresh Kumar  *
11737813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
11747813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
11757813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
11767813dd6fSViresh Kumar  *
11777813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
11787813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
11797813dd6fSViresh Kumar  *
11807813dd6fSViresh Kumar  * Return:
11817813dd6fSViresh Kumar  * 0		On success OR
11827813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
11837813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
11847813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
11857813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
11867813dd6fSViresh Kumar  */
11877813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
11887813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
11897813dd6fSViresh Kumar {
11907813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
11917813dd6fSViresh Kumar 	unsigned long tol;
11927813dd6fSViresh Kumar 	int ret;
11937813dd6fSViresh Kumar 
11947813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
11957813dd6fSViresh Kumar 	if (!new_opp)
11967813dd6fSViresh Kumar 		return -ENOMEM;
11977813dd6fSViresh Kumar 
11987813dd6fSViresh Kumar 	/* populate the opp table */
11997813dd6fSViresh Kumar 	new_opp->rate = freq;
12007813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
12017813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
12027813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
12037813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
12047813dd6fSViresh Kumar 	new_opp->available = true;
12057813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
12067813dd6fSViresh Kumar 
1207a1e8c136SViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table, false);
12087813dd6fSViresh Kumar 	if (ret) {
12097813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
12107813dd6fSViresh Kumar 		if (ret == -EBUSY)
12117813dd6fSViresh Kumar 			ret = 0;
12127813dd6fSViresh Kumar 		goto free_opp;
12137813dd6fSViresh Kumar 	}
12147813dd6fSViresh Kumar 
12157813dd6fSViresh Kumar 	/*
12167813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
12177813dd6fSViresh Kumar 	 * frequency/voltage list.
12187813dd6fSViresh Kumar 	 */
12197813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
12207813dd6fSViresh Kumar 	return 0;
12217813dd6fSViresh Kumar 
12227813dd6fSViresh Kumar free_opp:
12237813dd6fSViresh Kumar 	_opp_free(new_opp);
12247813dd6fSViresh Kumar 
12257813dd6fSViresh Kumar 	return ret;
12267813dd6fSViresh Kumar }
12277813dd6fSViresh Kumar 
12287813dd6fSViresh Kumar /**
12297813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw() - Set supported platforms
12307813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
12317813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
12327813dd6fSViresh Kumar  * @count: Number of elements in the array.
12337813dd6fSViresh Kumar  *
12347813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
12357813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
12367813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
12377813dd6fSViresh Kumar  * property.
12387813dd6fSViresh Kumar  */
12397813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
12407813dd6fSViresh Kumar 			const u32 *versions, unsigned int count)
12417813dd6fSViresh Kumar {
12427813dd6fSViresh Kumar 	struct opp_table *opp_table;
12437813dd6fSViresh Kumar 
12447813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
12457813dd6fSViresh Kumar 	if (!opp_table)
12467813dd6fSViresh Kumar 		return ERR_PTR(-ENOMEM);
12477813dd6fSViresh Kumar 
12487813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
12497813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
12507813dd6fSViresh Kumar 
125125419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
125225419de1SViresh Kumar 	if (opp_table->supported_hw)
125325419de1SViresh Kumar 		return opp_table;
12547813dd6fSViresh Kumar 
12557813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
12567813dd6fSViresh Kumar 					GFP_KERNEL);
12577813dd6fSViresh Kumar 	if (!opp_table->supported_hw) {
125825419de1SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
125925419de1SViresh Kumar 		return ERR_PTR(-ENOMEM);
12607813dd6fSViresh Kumar 	}
12617813dd6fSViresh Kumar 
12627813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
12637813dd6fSViresh Kumar 
12647813dd6fSViresh Kumar 	return opp_table;
12657813dd6fSViresh Kumar }
12667813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
12677813dd6fSViresh Kumar 
12687813dd6fSViresh Kumar /**
12697813dd6fSViresh Kumar  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
12707813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
12717813dd6fSViresh Kumar  *
12727813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
12737813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
12747813dd6fSViresh Kumar  * will not be freed.
12757813dd6fSViresh Kumar  */
12767813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
12777813dd6fSViresh Kumar {
12787813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
12797813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
12807813dd6fSViresh Kumar 
12817813dd6fSViresh Kumar 	kfree(opp_table->supported_hw);
12827813dd6fSViresh Kumar 	opp_table->supported_hw = NULL;
12837813dd6fSViresh Kumar 	opp_table->supported_hw_count = 0;
12847813dd6fSViresh Kumar 
12857813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
12867813dd6fSViresh Kumar }
12877813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
12887813dd6fSViresh Kumar 
12897813dd6fSViresh Kumar /**
12907813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name() - Set prop-extn name
12917813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
12927813dd6fSViresh Kumar  * @name: name to postfix to properties.
12937813dd6fSViresh Kumar  *
12947813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
12957813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
12967813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
12977813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
12987813dd6fSViresh Kumar  */
12997813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
13007813dd6fSViresh Kumar {
13017813dd6fSViresh Kumar 	struct opp_table *opp_table;
13027813dd6fSViresh Kumar 
13037813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
13047813dd6fSViresh Kumar 	if (!opp_table)
13057813dd6fSViresh Kumar 		return ERR_PTR(-ENOMEM);
13067813dd6fSViresh Kumar 
13077813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
13087813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
13097813dd6fSViresh Kumar 
1310878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
1311878ec1a9SViresh Kumar 	if (opp_table->prop_name)
1312878ec1a9SViresh Kumar 		return opp_table;
13137813dd6fSViresh Kumar 
13147813dd6fSViresh Kumar 	opp_table->prop_name = kstrdup(name, GFP_KERNEL);
13157813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
1316878ec1a9SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
1317878ec1a9SViresh Kumar 		return ERR_PTR(-ENOMEM);
13187813dd6fSViresh Kumar 	}
13197813dd6fSViresh Kumar 
13207813dd6fSViresh Kumar 	return opp_table;
13217813dd6fSViresh Kumar }
13227813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
13237813dd6fSViresh Kumar 
13247813dd6fSViresh Kumar /**
13257813dd6fSViresh Kumar  * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
13267813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
13277813dd6fSViresh Kumar  *
13287813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
13297813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
13307813dd6fSViresh Kumar  * will not be freed.
13317813dd6fSViresh Kumar  */
13327813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
13337813dd6fSViresh Kumar {
13347813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
13357813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
13367813dd6fSViresh Kumar 
13377813dd6fSViresh Kumar 	kfree(opp_table->prop_name);
13387813dd6fSViresh Kumar 	opp_table->prop_name = NULL;
13397813dd6fSViresh Kumar 
13407813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
13417813dd6fSViresh Kumar }
13427813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
13437813dd6fSViresh Kumar 
13447813dd6fSViresh Kumar static int _allocate_set_opp_data(struct opp_table *opp_table)
13457813dd6fSViresh Kumar {
13467813dd6fSViresh Kumar 	struct dev_pm_set_opp_data *data;
13477813dd6fSViresh Kumar 	int len, count = opp_table->regulator_count;
13487813dd6fSViresh Kumar 
13497813dd6fSViresh Kumar 	if (WARN_ON(!count))
13507813dd6fSViresh Kumar 		return -EINVAL;
13517813dd6fSViresh Kumar 
13527813dd6fSViresh Kumar 	/* space for set_opp_data */
13537813dd6fSViresh Kumar 	len = sizeof(*data);
13547813dd6fSViresh Kumar 
13557813dd6fSViresh Kumar 	/* space for old_opp.supplies and new_opp.supplies */
13567813dd6fSViresh Kumar 	len += 2 * sizeof(struct dev_pm_opp_supply) * count;
13577813dd6fSViresh Kumar 
13587813dd6fSViresh Kumar 	data = kzalloc(len, GFP_KERNEL);
13597813dd6fSViresh Kumar 	if (!data)
13607813dd6fSViresh Kumar 		return -ENOMEM;
13617813dd6fSViresh Kumar 
13627813dd6fSViresh Kumar 	data->old_opp.supplies = (void *)(data + 1);
13637813dd6fSViresh Kumar 	data->new_opp.supplies = data->old_opp.supplies + count;
13647813dd6fSViresh Kumar 
13657813dd6fSViresh Kumar 	opp_table->set_opp_data = data;
13667813dd6fSViresh Kumar 
13677813dd6fSViresh Kumar 	return 0;
13687813dd6fSViresh Kumar }
13697813dd6fSViresh Kumar 
13707813dd6fSViresh Kumar static void _free_set_opp_data(struct opp_table *opp_table)
13717813dd6fSViresh Kumar {
13727813dd6fSViresh Kumar 	kfree(opp_table->set_opp_data);
13737813dd6fSViresh Kumar 	opp_table->set_opp_data = NULL;
13747813dd6fSViresh Kumar }
13757813dd6fSViresh Kumar 
13767813dd6fSViresh Kumar /**
13777813dd6fSViresh Kumar  * dev_pm_opp_set_regulators() - Set regulator names for the device
13787813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
13797813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
13807813dd6fSViresh Kumar  * @count: Number of regulators.
13817813dd6fSViresh Kumar  *
13827813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
13837813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
13847813dd6fSViresh Kumar  * well.
13857813dd6fSViresh Kumar  *
13867813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
13877813dd6fSViresh Kumar  */
13887813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
13897813dd6fSViresh Kumar 					    const char * const names[],
13907813dd6fSViresh Kumar 					    unsigned int count)
13917813dd6fSViresh Kumar {
13927813dd6fSViresh Kumar 	struct opp_table *opp_table;
13937813dd6fSViresh Kumar 	struct regulator *reg;
13947813dd6fSViresh Kumar 	int ret, i;
13957813dd6fSViresh Kumar 
13967813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
13977813dd6fSViresh Kumar 	if (!opp_table)
13987813dd6fSViresh Kumar 		return ERR_PTR(-ENOMEM);
13997813dd6fSViresh Kumar 
14007813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
14017813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
14027813dd6fSViresh Kumar 		ret = -EBUSY;
14037813dd6fSViresh Kumar 		goto err;
14047813dd6fSViresh Kumar 	}
14057813dd6fSViresh Kumar 
1406779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
1407779b783cSViresh Kumar 	if (opp_table->regulators)
1408779b783cSViresh Kumar 		return opp_table;
14097813dd6fSViresh Kumar 
14107813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
14117813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
14127813dd6fSViresh Kumar 					      GFP_KERNEL);
14137813dd6fSViresh Kumar 	if (!opp_table->regulators) {
14147813dd6fSViresh Kumar 		ret = -ENOMEM;
14157813dd6fSViresh Kumar 		goto err;
14167813dd6fSViresh Kumar 	}
14177813dd6fSViresh Kumar 
14187813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
14197813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
14207813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
14217813dd6fSViresh Kumar 			ret = PTR_ERR(reg);
14227813dd6fSViresh Kumar 			if (ret != -EPROBE_DEFER)
14237813dd6fSViresh Kumar 				dev_err(dev, "%s: no regulator (%s) found: %d\n",
14247813dd6fSViresh Kumar 					__func__, names[i], ret);
14257813dd6fSViresh Kumar 			goto free_regulators;
14267813dd6fSViresh Kumar 		}
14277813dd6fSViresh Kumar 
14287813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
14297813dd6fSViresh Kumar 	}
14307813dd6fSViresh Kumar 
14317813dd6fSViresh Kumar 	opp_table->regulator_count = count;
14327813dd6fSViresh Kumar 
14337813dd6fSViresh Kumar 	/* Allocate block only once to pass to set_opp() routines */
14347813dd6fSViresh Kumar 	ret = _allocate_set_opp_data(opp_table);
14357813dd6fSViresh Kumar 	if (ret)
14367813dd6fSViresh Kumar 		goto free_regulators;
14377813dd6fSViresh Kumar 
14387813dd6fSViresh Kumar 	return opp_table;
14397813dd6fSViresh Kumar 
14407813dd6fSViresh Kumar free_regulators:
14417813dd6fSViresh Kumar 	while (i != 0)
14427813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[--i]);
14437813dd6fSViresh Kumar 
14447813dd6fSViresh Kumar 	kfree(opp_table->regulators);
14457813dd6fSViresh Kumar 	opp_table->regulators = NULL;
14467813dd6fSViresh Kumar 	opp_table->regulator_count = 0;
14477813dd6fSViresh Kumar err:
14487813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
14497813dd6fSViresh Kumar 
14507813dd6fSViresh Kumar 	return ERR_PTR(ret);
14517813dd6fSViresh Kumar }
14527813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
14537813dd6fSViresh Kumar 
14547813dd6fSViresh Kumar /**
14557813dd6fSViresh Kumar  * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
14567813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
14577813dd6fSViresh Kumar  */
14587813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table)
14597813dd6fSViresh Kumar {
14607813dd6fSViresh Kumar 	int i;
14617813dd6fSViresh Kumar 
1462779b783cSViresh Kumar 	if (!opp_table->regulators)
1463779b783cSViresh Kumar 		goto put_opp_table;
14647813dd6fSViresh Kumar 
14657813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
14667813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
14677813dd6fSViresh Kumar 
14687813dd6fSViresh Kumar 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
14697813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
14707813dd6fSViresh Kumar 
14717813dd6fSViresh Kumar 	_free_set_opp_data(opp_table);
14727813dd6fSViresh Kumar 
14737813dd6fSViresh Kumar 	kfree(opp_table->regulators);
14747813dd6fSViresh Kumar 	opp_table->regulators = NULL;
14757813dd6fSViresh Kumar 	opp_table->regulator_count = 0;
14767813dd6fSViresh Kumar 
1477779b783cSViresh Kumar put_opp_table:
14787813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
14797813dd6fSViresh Kumar }
14807813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
14817813dd6fSViresh Kumar 
14827813dd6fSViresh Kumar /**
14837813dd6fSViresh Kumar  * dev_pm_opp_set_clkname() - Set clk name for the device
14847813dd6fSViresh Kumar  * @dev: Device for which clk name is being set.
14857813dd6fSViresh Kumar  * @name: Clk name.
14867813dd6fSViresh Kumar  *
14877813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointer to the
14887813dd6fSViresh Kumar  * clock for the device. Simple cases work fine without using this routine (i.e.
14897813dd6fSViresh Kumar  * by passing connection-id as NULL), but for a device with multiple clocks
14907813dd6fSViresh Kumar  * available, the OPP core needs to know the exact name of the clk to use.
14917813dd6fSViresh Kumar  *
14927813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
14937813dd6fSViresh Kumar  */
14947813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
14957813dd6fSViresh Kumar {
14967813dd6fSViresh Kumar 	struct opp_table *opp_table;
14977813dd6fSViresh Kumar 	int ret;
14987813dd6fSViresh Kumar 
14997813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
15007813dd6fSViresh Kumar 	if (!opp_table)
15017813dd6fSViresh Kumar 		return ERR_PTR(-ENOMEM);
15027813dd6fSViresh Kumar 
15037813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
15047813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
15057813dd6fSViresh Kumar 		ret = -EBUSY;
15067813dd6fSViresh Kumar 		goto err;
15077813dd6fSViresh Kumar 	}
15087813dd6fSViresh Kumar 
15097813dd6fSViresh Kumar 	/* Already have default clk set, free it */
15107813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
15117813dd6fSViresh Kumar 		clk_put(opp_table->clk);
15127813dd6fSViresh Kumar 
15137813dd6fSViresh Kumar 	/* Find clk for the device */
15147813dd6fSViresh Kumar 	opp_table->clk = clk_get(dev, name);
15157813dd6fSViresh Kumar 	if (IS_ERR(opp_table->clk)) {
15167813dd6fSViresh Kumar 		ret = PTR_ERR(opp_table->clk);
15177813dd6fSViresh Kumar 		if (ret != -EPROBE_DEFER) {
15187813dd6fSViresh Kumar 			dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
15197813dd6fSViresh Kumar 				ret);
15207813dd6fSViresh Kumar 		}
15217813dd6fSViresh Kumar 		goto err;
15227813dd6fSViresh Kumar 	}
15237813dd6fSViresh Kumar 
15247813dd6fSViresh Kumar 	return opp_table;
15257813dd6fSViresh Kumar 
15267813dd6fSViresh Kumar err:
15277813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
15287813dd6fSViresh Kumar 
15297813dd6fSViresh Kumar 	return ERR_PTR(ret);
15307813dd6fSViresh Kumar }
15317813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
15327813dd6fSViresh Kumar 
15337813dd6fSViresh Kumar /**
15347813dd6fSViresh Kumar  * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
15357813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
15367813dd6fSViresh Kumar  */
15377813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table)
15387813dd6fSViresh Kumar {
15397813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
15407813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
15417813dd6fSViresh Kumar 
15427813dd6fSViresh Kumar 	clk_put(opp_table->clk);
15437813dd6fSViresh Kumar 	opp_table->clk = ERR_PTR(-EINVAL);
15447813dd6fSViresh Kumar 
15457813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
15467813dd6fSViresh Kumar }
15477813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
15487813dd6fSViresh Kumar 
15497813dd6fSViresh Kumar /**
15507813dd6fSViresh Kumar  * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
15517813dd6fSViresh Kumar  * @dev: Device for which the helper is getting registered.
15527813dd6fSViresh Kumar  * @set_opp: Custom set OPP helper.
15537813dd6fSViresh Kumar  *
15547813dd6fSViresh Kumar  * This is useful to support complex platforms (like platforms with multiple
15557813dd6fSViresh Kumar  * regulators per device), instead of the generic OPP set rate helper.
15567813dd6fSViresh Kumar  *
15577813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
15587813dd6fSViresh Kumar  */
15597813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
15607813dd6fSViresh Kumar 			int (*set_opp)(struct dev_pm_set_opp_data *data))
15617813dd6fSViresh Kumar {
15627813dd6fSViresh Kumar 	struct opp_table *opp_table;
15637813dd6fSViresh Kumar 
15647813dd6fSViresh Kumar 	if (!set_opp)
15657813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
15667813dd6fSViresh Kumar 
15677813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
15687813dd6fSViresh Kumar 	if (!opp_table)
15697813dd6fSViresh Kumar 		return ERR_PTR(-ENOMEM);
15707813dd6fSViresh Kumar 
15717813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
15727813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
15735019acc6SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
15745019acc6SViresh Kumar 		return ERR_PTR(-EBUSY);
15757813dd6fSViresh Kumar 	}
15767813dd6fSViresh Kumar 
15775019acc6SViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
15785019acc6SViresh Kumar 	if (!opp_table->set_opp)
15797813dd6fSViresh Kumar 		opp_table->set_opp = set_opp;
15807813dd6fSViresh Kumar 
15817813dd6fSViresh Kumar 	return opp_table;
15827813dd6fSViresh Kumar }
15837813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
15847813dd6fSViresh Kumar 
15857813dd6fSViresh Kumar /**
1586604a7aebSViresh Kumar  * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
15877813dd6fSViresh Kumar  *					   set_opp helper
15887813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
15897813dd6fSViresh Kumar  *
15907813dd6fSViresh Kumar  * Release resources blocked for platform specific set_opp helper.
15917813dd6fSViresh Kumar  */
1592604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
15937813dd6fSViresh Kumar {
15947813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
15957813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
15967813dd6fSViresh Kumar 
15977813dd6fSViresh Kumar 	opp_table->set_opp = NULL;
15987813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
15997813dd6fSViresh Kumar }
1600604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
16017813dd6fSViresh Kumar 
16027813dd6fSViresh Kumar /**
16037813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
16047813dd6fSViresh Kumar  * @dev:	device for which we do this operation
16057813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
16067813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
16077813dd6fSViresh Kumar  *
16087813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
16097813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
16107813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
16117813dd6fSViresh Kumar  *
16127813dd6fSViresh Kumar  * Return:
16137813dd6fSViresh Kumar  * 0		On success OR
16147813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
16157813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
16167813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
16177813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
16187813dd6fSViresh Kumar  */
16197813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
16207813dd6fSViresh Kumar {
16217813dd6fSViresh Kumar 	struct opp_table *opp_table;
16227813dd6fSViresh Kumar 	int ret;
16237813dd6fSViresh Kumar 
16247813dd6fSViresh Kumar 	opp_table = dev_pm_opp_get_opp_table(dev);
16257813dd6fSViresh Kumar 	if (!opp_table)
16267813dd6fSViresh Kumar 		return -ENOMEM;
16277813dd6fSViresh Kumar 
16287813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
16290ad8c623SViresh Kumar 	if (ret)
16307813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
16310ad8c623SViresh Kumar 
16327813dd6fSViresh Kumar 	return ret;
16337813dd6fSViresh Kumar }
16347813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
16357813dd6fSViresh Kumar 
16367813dd6fSViresh Kumar /**
16377813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
16387813dd6fSViresh Kumar  * @dev:		device for which we do this operation
16397813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
16407813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
16417813dd6fSViresh Kumar  *
16427813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
16437813dd6fSViresh Kumar  * which is isolated here.
16447813dd6fSViresh Kumar  *
16457813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
16467813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
16477813dd6fSViresh Kumar  * successful.
16487813dd6fSViresh Kumar  */
16497813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
16507813dd6fSViresh Kumar 				 bool availability_req)
16517813dd6fSViresh Kumar {
16527813dd6fSViresh Kumar 	struct opp_table *opp_table;
16537813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
16547813dd6fSViresh Kumar 	int r = 0;
16557813dd6fSViresh Kumar 
16567813dd6fSViresh Kumar 	/* Find the opp_table */
16577813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
16587813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
16597813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
16607813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
16617813dd6fSViresh Kumar 		return r;
16627813dd6fSViresh Kumar 	}
16637813dd6fSViresh Kumar 
16647813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
16657813dd6fSViresh Kumar 
16667813dd6fSViresh Kumar 	/* Do we have the frequency? */
16677813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
16687813dd6fSViresh Kumar 		if (tmp_opp->rate == freq) {
16697813dd6fSViresh Kumar 			opp = tmp_opp;
16707813dd6fSViresh Kumar 			break;
16717813dd6fSViresh Kumar 		}
16727813dd6fSViresh Kumar 	}
16737813dd6fSViresh Kumar 
16747813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
16757813dd6fSViresh Kumar 		r = PTR_ERR(opp);
16767813dd6fSViresh Kumar 		goto unlock;
16777813dd6fSViresh Kumar 	}
16787813dd6fSViresh Kumar 
16797813dd6fSViresh Kumar 	/* Is update really needed? */
16807813dd6fSViresh Kumar 	if (opp->available == availability_req)
16817813dd6fSViresh Kumar 		goto unlock;
16827813dd6fSViresh Kumar 
16837813dd6fSViresh Kumar 	opp->available = availability_req;
16847813dd6fSViresh Kumar 
16857813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
16867813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
16877813dd6fSViresh Kumar 
16887813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
16897813dd6fSViresh Kumar 	if (availability_req)
16907813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
16917813dd6fSViresh Kumar 					     opp);
16927813dd6fSViresh Kumar 	else
16937813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
16947813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
16957813dd6fSViresh Kumar 
16967813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
16977813dd6fSViresh Kumar 	goto put_table;
16987813dd6fSViresh Kumar 
16997813dd6fSViresh Kumar unlock:
17007813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
17017813dd6fSViresh Kumar put_table:
17027813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17037813dd6fSViresh Kumar 	return r;
17047813dd6fSViresh Kumar }
17057813dd6fSViresh Kumar 
17067813dd6fSViresh Kumar /**
17077813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
17087813dd6fSViresh Kumar  * @dev:	device for which we do this operation
17097813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
17107813dd6fSViresh Kumar  *
17117813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
17127813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
17137813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
17147813dd6fSViresh Kumar  *
17157813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
17167813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
17177813dd6fSViresh Kumar  * successful.
17187813dd6fSViresh Kumar  */
17197813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
17207813dd6fSViresh Kumar {
17217813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
17227813dd6fSViresh Kumar }
17237813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
17247813dd6fSViresh Kumar 
17257813dd6fSViresh Kumar /**
17267813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
17277813dd6fSViresh Kumar  * @dev:	device for which we do this operation
17287813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
17297813dd6fSViresh Kumar  *
17307813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
17317813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
17327813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
17337813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
17347813dd6fSViresh Kumar  *
17357813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
17367813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
17377813dd6fSViresh Kumar  * successful.
17387813dd6fSViresh Kumar  */
17397813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
17407813dd6fSViresh Kumar {
17417813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
17427813dd6fSViresh Kumar }
17437813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
17447813dd6fSViresh Kumar 
17457813dd6fSViresh Kumar /**
17467813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
17477813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
17487813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
17497813dd6fSViresh Kumar  *
17507813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
17517813dd6fSViresh Kumar  */
17527813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
17537813dd6fSViresh Kumar {
17547813dd6fSViresh Kumar 	struct opp_table *opp_table;
17557813dd6fSViresh Kumar 	int ret;
17567813dd6fSViresh Kumar 
17577813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
17587813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
17597813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
17607813dd6fSViresh Kumar 
17617813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
17627813dd6fSViresh Kumar 
17637813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17647813dd6fSViresh Kumar 
17657813dd6fSViresh Kumar 	return ret;
17667813dd6fSViresh Kumar }
17677813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
17687813dd6fSViresh Kumar 
17697813dd6fSViresh Kumar /**
17707813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
17717813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
17727813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
17737813dd6fSViresh Kumar  *
17747813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
17757813dd6fSViresh Kumar  */
17767813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
17777813dd6fSViresh Kumar 				   struct notifier_block *nb)
17787813dd6fSViresh Kumar {
17797813dd6fSViresh Kumar 	struct opp_table *opp_table;
17807813dd6fSViresh Kumar 	int ret;
17817813dd6fSViresh Kumar 
17827813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
17837813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
17847813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
17857813dd6fSViresh Kumar 
17867813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
17877813dd6fSViresh Kumar 
17887813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
17897813dd6fSViresh Kumar 
17907813dd6fSViresh Kumar 	return ret;
17917813dd6fSViresh Kumar }
17927813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
17937813dd6fSViresh Kumar 
17942a4eb735SViresh Kumar void _dev_pm_opp_find_and_remove_table(struct device *dev)
17957813dd6fSViresh Kumar {
17967813dd6fSViresh Kumar 	struct opp_table *opp_table;
17977813dd6fSViresh Kumar 
17987813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
17997813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
18007813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
18017813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
18027813dd6fSViresh Kumar 
18037813dd6fSViresh Kumar 		if (error != -ENODEV)
18047813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
18057813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
18067813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
18077813dd6fSViresh Kumar 			     error);
18087813dd6fSViresh Kumar 		return;
18097813dd6fSViresh Kumar 	}
18107813dd6fSViresh Kumar 
1811cdd6ed90SViresh Kumar 	_put_opp_list_kref(opp_table);
18127813dd6fSViresh Kumar 
1813cdd6ed90SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
1814cdd6ed90SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1815cdd6ed90SViresh Kumar 
1816cdd6ed90SViresh Kumar 	/* Drop reference taken while the OPP table was added */
18177813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
18187813dd6fSViresh Kumar }
18197813dd6fSViresh Kumar 
18207813dd6fSViresh Kumar /**
18217813dd6fSViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
18227813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table.
18237813dd6fSViresh Kumar  *
18247813dd6fSViresh Kumar  * Free both OPPs created using static entries present in DT and the
18257813dd6fSViresh Kumar  * dynamically added entries.
18267813dd6fSViresh Kumar  */
18277813dd6fSViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
18287813dd6fSViresh Kumar {
18292a4eb735SViresh Kumar 	_dev_pm_opp_find_and_remove_table(dev);
18307813dd6fSViresh Kumar }
18317813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
1832