xref: /openbmc/linux/drivers/opp/core.c (revision 95073b721c03c0438b7ee692c20ad2075d3a937a)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27813dd6fSViresh Kumar /*
37813dd6fSViresh Kumar  * Generic OPP Interface
47813dd6fSViresh Kumar  *
57813dd6fSViresh Kumar  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
67813dd6fSViresh Kumar  *	Nishanth Menon
77813dd6fSViresh Kumar  *	Romit Dasgupta
87813dd6fSViresh Kumar  *	Kevin Hilman
97813dd6fSViresh Kumar  */
107813dd6fSViresh Kumar 
117813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
127813dd6fSViresh Kumar 
137813dd6fSViresh Kumar #include <linux/clk.h>
147813dd6fSViresh Kumar #include <linux/errno.h>
157813dd6fSViresh Kumar #include <linux/err.h>
167813dd6fSViresh Kumar #include <linux/slab.h>
177813dd6fSViresh Kumar #include <linux/device.h>
187813dd6fSViresh Kumar #include <linux/export.h>
19009acd19SViresh Kumar #include <linux/pm_domain.h>
207813dd6fSViresh Kumar #include <linux/regulator/consumer.h>
217813dd6fSViresh Kumar 
227813dd6fSViresh Kumar #include "opp.h"
237813dd6fSViresh Kumar 
247813dd6fSViresh Kumar /*
257813dd6fSViresh Kumar  * The root of the list of all opp-tables. All opp_table structures branch off
267813dd6fSViresh Kumar  * from here, with each opp_table containing the list of opps it supports in
277813dd6fSViresh Kumar  * various states of availability.
287813dd6fSViresh Kumar  */
297813dd6fSViresh Kumar LIST_HEAD(opp_tables);
307eba0c76SViresh Kumar 
317eba0c76SViresh Kumar /* OPP tables with uninitialized required OPPs */
327eba0c76SViresh Kumar LIST_HEAD(lazy_opp_tables);
337eba0c76SViresh Kumar 
347813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
357813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock);
3627c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */
3727c09484SViresh Kumar static bool opp_tables_busy;
387813dd6fSViresh Kumar 
399e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
407813dd6fSViresh Kumar {
417813dd6fSViresh Kumar 	struct opp_device *opp_dev;
429e62edacSViresh Kumar 	bool found = false;
437813dd6fSViresh Kumar 
449e62edacSViresh Kumar 	mutex_lock(&opp_table->lock);
457813dd6fSViresh Kumar 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
469e62edacSViresh Kumar 		if (opp_dev->dev == dev) {
479e62edacSViresh Kumar 			found = true;
489e62edacSViresh Kumar 			break;
499e62edacSViresh Kumar 		}
507813dd6fSViresh Kumar 
519e62edacSViresh Kumar 	mutex_unlock(&opp_table->lock);
529e62edacSViresh Kumar 	return found;
537813dd6fSViresh Kumar }
547813dd6fSViresh Kumar 
557813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev)
567813dd6fSViresh Kumar {
577813dd6fSViresh Kumar 	struct opp_table *opp_table;
587813dd6fSViresh Kumar 
597813dd6fSViresh Kumar 	list_for_each_entry(opp_table, &opp_tables, node) {
609e62edacSViresh Kumar 		if (_find_opp_dev(dev, opp_table)) {
617813dd6fSViresh Kumar 			_get_opp_table_kref(opp_table);
627813dd6fSViresh Kumar 			return opp_table;
637813dd6fSViresh Kumar 		}
647813dd6fSViresh Kumar 	}
657813dd6fSViresh Kumar 
667813dd6fSViresh Kumar 	return ERR_PTR(-ENODEV);
677813dd6fSViresh Kumar }
687813dd6fSViresh Kumar 
697813dd6fSViresh Kumar /**
707813dd6fSViresh Kumar  * _find_opp_table() - find opp_table struct using device pointer
717813dd6fSViresh Kumar  * @dev:	device pointer used to lookup OPP table
727813dd6fSViresh Kumar  *
737813dd6fSViresh Kumar  * Search OPP table for one containing matching device.
747813dd6fSViresh Kumar  *
757813dd6fSViresh Kumar  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
767813dd6fSViresh Kumar  * -EINVAL based on type of error.
777813dd6fSViresh Kumar  *
787813dd6fSViresh Kumar  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
797813dd6fSViresh Kumar  */
807813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev)
817813dd6fSViresh Kumar {
827813dd6fSViresh Kumar 	struct opp_table *opp_table;
837813dd6fSViresh Kumar 
847813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(dev)) {
857813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
867813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
877813dd6fSViresh Kumar 	}
887813dd6fSViresh Kumar 
897813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
907813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
917813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
927813dd6fSViresh Kumar 
937813dd6fSViresh Kumar 	return opp_table;
947813dd6fSViresh Kumar }
957813dd6fSViresh Kumar 
967813dd6fSViresh Kumar /**
977813dd6fSViresh Kumar  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
987813dd6fSViresh Kumar  * @opp:	opp for which voltage has to be returned for
997813dd6fSViresh Kumar  *
1007813dd6fSViresh Kumar  * Return: voltage in micro volt corresponding to the opp, else
1017813dd6fSViresh Kumar  * return 0
1027813dd6fSViresh Kumar  *
1037813dd6fSViresh Kumar  * This is useful only for devices with single power supply.
1047813dd6fSViresh Kumar  */
1057813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
1067813dd6fSViresh Kumar {
1077813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp)) {
1087813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1097813dd6fSViresh Kumar 		return 0;
1107813dd6fSViresh Kumar 	}
1117813dd6fSViresh Kumar 
1127813dd6fSViresh Kumar 	return opp->supplies[0].u_volt;
1137813dd6fSViresh Kumar }
1147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
1157813dd6fSViresh Kumar 
1167813dd6fSViresh Kumar /**
1174f9a7a1dSLukasz Luba  * dev_pm_opp_get_power() - Gets the power corresponding to an opp
1184f9a7a1dSLukasz Luba  * @opp:	opp for which power has to be returned for
1194f9a7a1dSLukasz Luba  *
1204f9a7a1dSLukasz Luba  * Return: power in micro watt corresponding to the opp, else
1214f9a7a1dSLukasz Luba  * return 0
1224f9a7a1dSLukasz Luba  *
1234f9a7a1dSLukasz Luba  * This is useful only for devices with single power supply.
1244f9a7a1dSLukasz Luba  */
1254f9a7a1dSLukasz Luba unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
1264f9a7a1dSLukasz Luba {
1274f9a7a1dSLukasz Luba 	unsigned long opp_power = 0;
1284f9a7a1dSLukasz Luba 	int i;
1294f9a7a1dSLukasz Luba 
1304f9a7a1dSLukasz Luba 	if (IS_ERR_OR_NULL(opp)) {
1314f9a7a1dSLukasz Luba 		pr_err("%s: Invalid parameters\n", __func__);
1324f9a7a1dSLukasz Luba 		return 0;
1334f9a7a1dSLukasz Luba 	}
1344f9a7a1dSLukasz Luba 	for (i = 0; i < opp->opp_table->regulator_count; i++)
1354f9a7a1dSLukasz Luba 		opp_power += opp->supplies[i].u_watt;
1364f9a7a1dSLukasz Luba 
1374f9a7a1dSLukasz Luba 	return opp_power;
1384f9a7a1dSLukasz Luba }
1394f9a7a1dSLukasz Luba EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
1404f9a7a1dSLukasz Luba 
1414f9a7a1dSLukasz Luba /**
1427813dd6fSViresh Kumar  * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
1437813dd6fSViresh Kumar  * @opp:	opp for which frequency has to be returned for
1447813dd6fSViresh Kumar  *
1457813dd6fSViresh Kumar  * Return: frequency in hertz corresponding to the opp, else
1467813dd6fSViresh Kumar  * return 0
1477813dd6fSViresh Kumar  */
1487813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
1497813dd6fSViresh Kumar {
15006a8a059SAndrew-sh.Cheng 	if (IS_ERR_OR_NULL(opp)) {
1517813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
1527813dd6fSViresh Kumar 		return 0;
1537813dd6fSViresh Kumar 	}
1547813dd6fSViresh Kumar 
1557813dd6fSViresh Kumar 	return opp->rate;
1567813dd6fSViresh Kumar }
1577813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
1587813dd6fSViresh Kumar 
1597813dd6fSViresh Kumar /**
1605b93ac54SRajendra Nayak  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
1615b93ac54SRajendra Nayak  * @opp:	opp for which level value has to be returned for
1625b93ac54SRajendra Nayak  *
1635b93ac54SRajendra Nayak  * Return: level read from device tree corresponding to the opp, else
1645b93ac54SRajendra Nayak  * return 0.
1655b93ac54SRajendra Nayak  */
1665b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
1675b93ac54SRajendra Nayak {
1685b93ac54SRajendra Nayak 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
1695b93ac54SRajendra Nayak 		pr_err("%s: Invalid parameters\n", __func__);
1705b93ac54SRajendra Nayak 		return 0;
1715b93ac54SRajendra Nayak 	}
1725b93ac54SRajendra Nayak 
1735b93ac54SRajendra Nayak 	return opp->level;
1745b93ac54SRajendra Nayak }
1755b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
1765b93ac54SRajendra Nayak 
1775b93ac54SRajendra Nayak /**
178597ff543SDmitry Osipenko  * dev_pm_opp_get_required_pstate() - Gets the required performance state
179597ff543SDmitry Osipenko  *                                    corresponding to an available opp
180597ff543SDmitry Osipenko  * @opp:	opp for which performance state has to be returned for
181597ff543SDmitry Osipenko  * @index:	index of the required opp
182597ff543SDmitry Osipenko  *
183597ff543SDmitry Osipenko  * Return: performance state read from device tree corresponding to the
184597ff543SDmitry Osipenko  * required opp, else return 0.
185597ff543SDmitry Osipenko  */
186597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
187597ff543SDmitry Osipenko 					    unsigned int index)
188597ff543SDmitry Osipenko {
189597ff543SDmitry Osipenko 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
190597ff543SDmitry Osipenko 	    index >= opp->opp_table->required_opp_count) {
191597ff543SDmitry Osipenko 		pr_err("%s: Invalid parameters\n", __func__);
192597ff543SDmitry Osipenko 		return 0;
193597ff543SDmitry Osipenko 	}
194597ff543SDmitry Osipenko 
1957eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
1967eba0c76SViresh Kumar 	if (lazy_linking_pending(opp->opp_table))
1977eba0c76SViresh Kumar 		return 0;
1987eba0c76SViresh Kumar 
199597ff543SDmitry Osipenko 	return opp->required_opps[index]->pstate;
200597ff543SDmitry Osipenko }
201597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
202597ff543SDmitry Osipenko 
203597ff543SDmitry Osipenko /**
2047813dd6fSViresh Kumar  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
2057813dd6fSViresh Kumar  * @opp: opp for which turbo mode is being verified
2067813dd6fSViresh Kumar  *
2077813dd6fSViresh Kumar  * Turbo OPPs are not for normal use, and can be enabled (under certain
2087813dd6fSViresh Kumar  * conditions) for short duration of times to finish high throughput work
2097813dd6fSViresh Kumar  * quickly. Running on them for longer times may overheat the chip.
2107813dd6fSViresh Kumar  *
2117813dd6fSViresh Kumar  * Return: true if opp is turbo opp, else false.
2127813dd6fSViresh Kumar  */
2137813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
2147813dd6fSViresh Kumar {
2157813dd6fSViresh Kumar 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
2167813dd6fSViresh Kumar 		pr_err("%s: Invalid parameters\n", __func__);
2177813dd6fSViresh Kumar 		return false;
2187813dd6fSViresh Kumar 	}
2197813dd6fSViresh Kumar 
2207813dd6fSViresh Kumar 	return opp->turbo;
2217813dd6fSViresh Kumar }
2227813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
2237813dd6fSViresh Kumar 
2247813dd6fSViresh Kumar /**
2257813dd6fSViresh Kumar  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
2267813dd6fSViresh Kumar  * @dev:	device for which we do this operation
2277813dd6fSViresh Kumar  *
2287813dd6fSViresh Kumar  * Return: This function returns the max clock latency in nanoseconds.
2297813dd6fSViresh Kumar  */
2307813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
2317813dd6fSViresh Kumar {
2327813dd6fSViresh Kumar 	struct opp_table *opp_table;
2337813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
2347813dd6fSViresh Kumar 
2357813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2367813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2377813dd6fSViresh Kumar 		return 0;
2387813dd6fSViresh Kumar 
2397813dd6fSViresh Kumar 	clock_latency_ns = opp_table->clock_latency_ns_max;
2407813dd6fSViresh Kumar 
2417813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
2427813dd6fSViresh Kumar 
2437813dd6fSViresh Kumar 	return clock_latency_ns;
2447813dd6fSViresh Kumar }
2457813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
2467813dd6fSViresh Kumar 
2477813dd6fSViresh Kumar /**
2487813dd6fSViresh Kumar  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
2497813dd6fSViresh Kumar  * @dev: device for which we do this operation
2507813dd6fSViresh Kumar  *
2517813dd6fSViresh Kumar  * Return: This function returns the max voltage latency in nanoseconds.
2527813dd6fSViresh Kumar  */
2537813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
2547813dd6fSViresh Kumar {
2557813dd6fSViresh Kumar 	struct opp_table *opp_table;
2567813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
2577813dd6fSViresh Kumar 	struct regulator *reg;
2587813dd6fSViresh Kumar 	unsigned long latency_ns = 0;
2597813dd6fSViresh Kumar 	int ret, i, count;
2607813dd6fSViresh Kumar 	struct {
2617813dd6fSViresh Kumar 		unsigned long min;
2627813dd6fSViresh Kumar 		unsigned long max;
2637813dd6fSViresh Kumar 	} *uV;
2647813dd6fSViresh Kumar 
2657813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
2667813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
2677813dd6fSViresh Kumar 		return 0;
2687813dd6fSViresh Kumar 
2697813dd6fSViresh Kumar 	/* Regulator may not be required for the device */
27090e3577bSViresh Kumar 	if (!opp_table->regulators)
2717813dd6fSViresh Kumar 		goto put_opp_table;
2727813dd6fSViresh Kumar 
27390e3577bSViresh Kumar 	count = opp_table->regulator_count;
27490e3577bSViresh Kumar 
2757813dd6fSViresh Kumar 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
2767813dd6fSViresh Kumar 	if (!uV)
2777813dd6fSViresh Kumar 		goto put_opp_table;
2787813dd6fSViresh Kumar 
2797813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
2807813dd6fSViresh Kumar 
2817813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
2827813dd6fSViresh Kumar 		uV[i].min = ~0;
2837813dd6fSViresh Kumar 		uV[i].max = 0;
2847813dd6fSViresh Kumar 
2857813dd6fSViresh Kumar 		list_for_each_entry(opp, &opp_table->opp_list, node) {
2867813dd6fSViresh Kumar 			if (!opp->available)
2877813dd6fSViresh Kumar 				continue;
2887813dd6fSViresh Kumar 
2897813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_min < uV[i].min)
2907813dd6fSViresh Kumar 				uV[i].min = opp->supplies[i].u_volt_min;
2917813dd6fSViresh Kumar 			if (opp->supplies[i].u_volt_max > uV[i].max)
2927813dd6fSViresh Kumar 				uV[i].max = opp->supplies[i].u_volt_max;
2937813dd6fSViresh Kumar 		}
2947813dd6fSViresh Kumar 	}
2957813dd6fSViresh Kumar 
2967813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
2977813dd6fSViresh Kumar 
2987813dd6fSViresh Kumar 	/*
2997813dd6fSViresh Kumar 	 * The caller needs to ensure that opp_table (and hence the regulator)
3007813dd6fSViresh Kumar 	 * isn't freed, while we are executing this routine.
3017813dd6fSViresh Kumar 	 */
3027813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
3037813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
3047813dd6fSViresh Kumar 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
3057813dd6fSViresh Kumar 		if (ret > 0)
3067813dd6fSViresh Kumar 			latency_ns += ret * 1000;
3077813dd6fSViresh Kumar 	}
3087813dd6fSViresh Kumar 
3097813dd6fSViresh Kumar 	kfree(uV);
3107813dd6fSViresh Kumar put_opp_table:
3117813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3127813dd6fSViresh Kumar 
3137813dd6fSViresh Kumar 	return latency_ns;
3147813dd6fSViresh Kumar }
3157813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
3167813dd6fSViresh Kumar 
3177813dd6fSViresh Kumar /**
3187813dd6fSViresh Kumar  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
3197813dd6fSViresh Kumar  *					     nanoseconds
3207813dd6fSViresh Kumar  * @dev: device for which we do this operation
3217813dd6fSViresh Kumar  *
3227813dd6fSViresh Kumar  * Return: This function returns the max transition latency, in nanoseconds, to
3237813dd6fSViresh Kumar  * switch from one OPP to other.
3247813dd6fSViresh Kumar  */
3257813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
3267813dd6fSViresh Kumar {
3277813dd6fSViresh Kumar 	return dev_pm_opp_get_max_volt_latency(dev) +
3287813dd6fSViresh Kumar 		dev_pm_opp_get_max_clock_latency(dev);
3297813dd6fSViresh Kumar }
3307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
3317813dd6fSViresh Kumar 
3327813dd6fSViresh Kumar /**
3337813dd6fSViresh Kumar  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
3347813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3357813dd6fSViresh Kumar  *
3367813dd6fSViresh Kumar  * Return: This function returns the frequency of the OPP marked as suspend_opp
3377813dd6fSViresh Kumar  * if one is available, else returns 0;
3387813dd6fSViresh Kumar  */
3397813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
3407813dd6fSViresh Kumar {
3417813dd6fSViresh Kumar 	struct opp_table *opp_table;
3427813dd6fSViresh Kumar 	unsigned long freq = 0;
3437813dd6fSViresh Kumar 
3447813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3457813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
3467813dd6fSViresh Kumar 		return 0;
3477813dd6fSViresh Kumar 
3487813dd6fSViresh Kumar 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
3497813dd6fSViresh Kumar 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
3507813dd6fSViresh Kumar 
3517813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3527813dd6fSViresh Kumar 
3537813dd6fSViresh Kumar 	return freq;
3547813dd6fSViresh Kumar }
3557813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
3567813dd6fSViresh Kumar 
357a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table)
358a1e8c136SViresh Kumar {
359a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
360a1e8c136SViresh Kumar 	int count = 0;
361a1e8c136SViresh Kumar 
362a1e8c136SViresh Kumar 	mutex_lock(&opp_table->lock);
363a1e8c136SViresh Kumar 
364a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
365a1e8c136SViresh Kumar 		if (opp->available)
366a1e8c136SViresh Kumar 			count++;
367a1e8c136SViresh Kumar 	}
368a1e8c136SViresh Kumar 
369a1e8c136SViresh Kumar 	mutex_unlock(&opp_table->lock);
370a1e8c136SViresh Kumar 
371a1e8c136SViresh Kumar 	return count;
372a1e8c136SViresh Kumar }
373a1e8c136SViresh Kumar 
3747813dd6fSViresh Kumar /**
3757813dd6fSViresh Kumar  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
3767813dd6fSViresh Kumar  * @dev:	device for which we do this operation
3777813dd6fSViresh Kumar  *
3787813dd6fSViresh Kumar  * Return: This function returns the number of available opps if there are any,
3797813dd6fSViresh Kumar  * else returns 0 if none or the corresponding error value.
3807813dd6fSViresh Kumar  */
3817813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev)
3827813dd6fSViresh Kumar {
3837813dd6fSViresh Kumar 	struct opp_table *opp_table;
384a1e8c136SViresh Kumar 	int count;
3857813dd6fSViresh Kumar 
3867813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
3877813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
3887813dd6fSViresh Kumar 		count = PTR_ERR(opp_table);
389035ed072SFabio Estevam 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
3907813dd6fSViresh Kumar 			__func__, count);
39109f662f9SViresh Kumar 		return count;
3927813dd6fSViresh Kumar 	}
3937813dd6fSViresh Kumar 
394a1e8c136SViresh Kumar 	count = _get_opp_count(opp_table);
3957813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
3967813dd6fSViresh Kumar 
3977813dd6fSViresh Kumar 	return count;
3987813dd6fSViresh Kumar }
3997813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
4007813dd6fSViresh Kumar 
4017813dd6fSViresh Kumar /**
4027813dd6fSViresh Kumar  * dev_pm_opp_find_freq_exact() - search for an exact frequency
4037813dd6fSViresh Kumar  * @dev:		device for which we do this operation
4047813dd6fSViresh Kumar  * @freq:		frequency to search for
4057813dd6fSViresh Kumar  * @available:		true/false - match for available opp
4067813dd6fSViresh Kumar  *
4077813dd6fSViresh Kumar  * Return: Searches for exact match in the opp table and returns pointer to the
4087813dd6fSViresh Kumar  * matching opp if found, else returns ERR_PTR in case of error and should
4097813dd6fSViresh Kumar  * be handled using IS_ERR. Error return values can be:
4107813dd6fSViresh Kumar  * EINVAL:	for bad pointer
4117813dd6fSViresh Kumar  * ERANGE:	no match found for search
4127813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
4137813dd6fSViresh Kumar  *
4147813dd6fSViresh Kumar  * Note: available is a modifier for the search. if available=true, then the
4157813dd6fSViresh Kumar  * match is for exact matching frequency and is available in the stored OPP
4167813dd6fSViresh Kumar  * table. if false, the match is for exact frequency which is not available.
4177813dd6fSViresh Kumar  *
4187813dd6fSViresh Kumar  * This provides a mechanism to enable an opp which is not available currently
4197813dd6fSViresh Kumar  * or the opposite as well.
4207813dd6fSViresh Kumar  *
4217813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
4227813dd6fSViresh Kumar  * use.
4237813dd6fSViresh Kumar  */
4247813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
4257813dd6fSViresh Kumar 					      unsigned long freq,
4267813dd6fSViresh Kumar 					      bool available)
4277813dd6fSViresh Kumar {
4287813dd6fSViresh Kumar 	struct opp_table *opp_table;
4297813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
4307813dd6fSViresh Kumar 
4317813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
4327813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
4337813dd6fSViresh Kumar 		int r = PTR_ERR(opp_table);
4347813dd6fSViresh Kumar 
4357813dd6fSViresh Kumar 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
4367813dd6fSViresh Kumar 		return ERR_PTR(r);
4377813dd6fSViresh Kumar 	}
4387813dd6fSViresh Kumar 
4397813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
4407813dd6fSViresh Kumar 
4417813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
4427813dd6fSViresh Kumar 		if (temp_opp->available == available &&
4437813dd6fSViresh Kumar 				temp_opp->rate == freq) {
4447813dd6fSViresh Kumar 			opp = temp_opp;
4457813dd6fSViresh Kumar 
4467813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
4477813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
4487813dd6fSViresh Kumar 			break;
4497813dd6fSViresh Kumar 		}
4507813dd6fSViresh Kumar 	}
4517813dd6fSViresh Kumar 
4527813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
4537813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
4547813dd6fSViresh Kumar 
4557813dd6fSViresh Kumar 	return opp;
4567813dd6fSViresh Kumar }
4577813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
4587813dd6fSViresh Kumar 
45971419d84SNiklas Cassel /**
46071419d84SNiklas Cassel  * dev_pm_opp_find_level_exact() - search for an exact level
46171419d84SNiklas Cassel  * @dev:		device for which we do this operation
46271419d84SNiklas Cassel  * @level:		level to search for
46371419d84SNiklas Cassel  *
46471419d84SNiklas Cassel  * Return: Searches for exact match in the opp table and returns pointer to the
46571419d84SNiklas Cassel  * matching opp if found, else returns ERR_PTR in case of error and should
46671419d84SNiklas Cassel  * be handled using IS_ERR. Error return values can be:
46771419d84SNiklas Cassel  * EINVAL:	for bad pointer
46871419d84SNiklas Cassel  * ERANGE:	no match found for search
46971419d84SNiklas Cassel  * ENODEV:	if device not found in list of registered devices
47071419d84SNiklas Cassel  *
47171419d84SNiklas Cassel  * The callers are required to call dev_pm_opp_put() for the returned OPP after
47271419d84SNiklas Cassel  * use.
47371419d84SNiklas Cassel  */
47471419d84SNiklas Cassel struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
47571419d84SNiklas Cassel 					       unsigned int level)
47671419d84SNiklas Cassel {
47771419d84SNiklas Cassel 	struct opp_table *opp_table;
47871419d84SNiklas Cassel 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
47971419d84SNiklas Cassel 
48071419d84SNiklas Cassel 	opp_table = _find_opp_table(dev);
48171419d84SNiklas Cassel 	if (IS_ERR(opp_table)) {
48271419d84SNiklas Cassel 		int r = PTR_ERR(opp_table);
48371419d84SNiklas Cassel 
48471419d84SNiklas Cassel 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
48571419d84SNiklas Cassel 		return ERR_PTR(r);
48671419d84SNiklas Cassel 	}
48771419d84SNiklas Cassel 
48871419d84SNiklas Cassel 	mutex_lock(&opp_table->lock);
48971419d84SNiklas Cassel 
49071419d84SNiklas Cassel 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
49171419d84SNiklas Cassel 		if (temp_opp->level == level) {
49271419d84SNiklas Cassel 			opp = temp_opp;
49371419d84SNiklas Cassel 
49471419d84SNiklas Cassel 			/* Increment the reference count of OPP */
49571419d84SNiklas Cassel 			dev_pm_opp_get(opp);
49671419d84SNiklas Cassel 			break;
49771419d84SNiklas Cassel 		}
49871419d84SNiklas Cassel 	}
49971419d84SNiklas Cassel 
50071419d84SNiklas Cassel 	mutex_unlock(&opp_table->lock);
50171419d84SNiklas Cassel 	dev_pm_opp_put_opp_table(opp_table);
50271419d84SNiklas Cassel 
50371419d84SNiklas Cassel 	return opp;
50471419d84SNiklas Cassel }
50571419d84SNiklas Cassel EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
50671419d84SNiklas Cassel 
5078dd5cadaSDmitry Osipenko /**
5088dd5cadaSDmitry Osipenko  * dev_pm_opp_find_level_ceil() - search for an rounded up level
5098dd5cadaSDmitry Osipenko  * @dev:		device for which we do this operation
5108dd5cadaSDmitry Osipenko  * @level:		level to search for
5118dd5cadaSDmitry Osipenko  *
5128dd5cadaSDmitry Osipenko  * Return: Searches for rounded up match in the opp table and returns pointer
5138dd5cadaSDmitry Osipenko  * to the  matching opp if found, else returns ERR_PTR in case of error and
5148dd5cadaSDmitry Osipenko  * should be handled using IS_ERR. Error return values can be:
5158dd5cadaSDmitry Osipenko  * EINVAL:	for bad pointer
5168dd5cadaSDmitry Osipenko  * ERANGE:	no match found for search
5178dd5cadaSDmitry Osipenko  * ENODEV:	if device not found in list of registered devices
5188dd5cadaSDmitry Osipenko  *
5198dd5cadaSDmitry Osipenko  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5208dd5cadaSDmitry Osipenko  * use.
5218dd5cadaSDmitry Osipenko  */
5228dd5cadaSDmitry Osipenko struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
5238dd5cadaSDmitry Osipenko 					      unsigned int *level)
5248dd5cadaSDmitry Osipenko {
5258dd5cadaSDmitry Osipenko 	struct opp_table *opp_table;
5268dd5cadaSDmitry Osipenko 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5278dd5cadaSDmitry Osipenko 
5288dd5cadaSDmitry Osipenko 	opp_table = _find_opp_table(dev);
5298dd5cadaSDmitry Osipenko 	if (IS_ERR(opp_table)) {
5308dd5cadaSDmitry Osipenko 		int r = PTR_ERR(opp_table);
5318dd5cadaSDmitry Osipenko 
5328dd5cadaSDmitry Osipenko 		dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
5338dd5cadaSDmitry Osipenko 		return ERR_PTR(r);
5348dd5cadaSDmitry Osipenko 	}
5358dd5cadaSDmitry Osipenko 
5368dd5cadaSDmitry Osipenko 	mutex_lock(&opp_table->lock);
5378dd5cadaSDmitry Osipenko 
5388dd5cadaSDmitry Osipenko 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5398dd5cadaSDmitry Osipenko 		if (temp_opp->available && temp_opp->level >= *level) {
5408dd5cadaSDmitry Osipenko 			opp = temp_opp;
5418dd5cadaSDmitry Osipenko 			*level = opp->level;
5428dd5cadaSDmitry Osipenko 
5438dd5cadaSDmitry Osipenko 			/* Increment the reference count of OPP */
5448dd5cadaSDmitry Osipenko 			dev_pm_opp_get(opp);
5458dd5cadaSDmitry Osipenko 			break;
5468dd5cadaSDmitry Osipenko 		}
5478dd5cadaSDmitry Osipenko 	}
5488dd5cadaSDmitry Osipenko 
5498dd5cadaSDmitry Osipenko 	mutex_unlock(&opp_table->lock);
5508dd5cadaSDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
5518dd5cadaSDmitry Osipenko 
5528dd5cadaSDmitry Osipenko 	return opp;
5538dd5cadaSDmitry Osipenko }
5548dd5cadaSDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
5558dd5cadaSDmitry Osipenko 
5567813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
5577813dd6fSViresh Kumar 						   unsigned long *freq)
5587813dd6fSViresh Kumar {
5597813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
5607813dd6fSViresh Kumar 
5617813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
5627813dd6fSViresh Kumar 
5637813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
5647813dd6fSViresh Kumar 		if (temp_opp->available && temp_opp->rate >= *freq) {
5657813dd6fSViresh Kumar 			opp = temp_opp;
5667813dd6fSViresh Kumar 			*freq = opp->rate;
5677813dd6fSViresh Kumar 
5687813dd6fSViresh Kumar 			/* Increment the reference count of OPP */
5697813dd6fSViresh Kumar 			dev_pm_opp_get(opp);
5707813dd6fSViresh Kumar 			break;
5717813dd6fSViresh Kumar 		}
5727813dd6fSViresh Kumar 	}
5737813dd6fSViresh Kumar 
5747813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
5757813dd6fSViresh Kumar 
5767813dd6fSViresh Kumar 	return opp;
5777813dd6fSViresh Kumar }
5787813dd6fSViresh Kumar 
5797813dd6fSViresh Kumar /**
5807813dd6fSViresh Kumar  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
5817813dd6fSViresh Kumar  * @dev:	device for which we do this operation
5827813dd6fSViresh Kumar  * @freq:	Start frequency
5837813dd6fSViresh Kumar  *
5847813dd6fSViresh Kumar  * Search for the matching ceil *available* OPP from a starting freq
5857813dd6fSViresh Kumar  * for a device.
5867813dd6fSViresh Kumar  *
5877813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
5887813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
5897813dd6fSViresh Kumar  * values can be:
5907813dd6fSViresh Kumar  * EINVAL:	for bad pointer
5917813dd6fSViresh Kumar  * ERANGE:	no match found for search
5927813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
5937813dd6fSViresh Kumar  *
5947813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
5957813dd6fSViresh Kumar  * use.
5967813dd6fSViresh Kumar  */
5977813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
5987813dd6fSViresh Kumar 					     unsigned long *freq)
5997813dd6fSViresh Kumar {
6007813dd6fSViresh Kumar 	struct opp_table *opp_table;
6017813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
6027813dd6fSViresh Kumar 
6037813dd6fSViresh Kumar 	if (!dev || !freq) {
6047813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
6057813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
6067813dd6fSViresh Kumar 	}
6077813dd6fSViresh Kumar 
6087813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
6097813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
6107813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
6117813dd6fSViresh Kumar 
6127813dd6fSViresh Kumar 	opp = _find_freq_ceil(opp_table, freq);
6137813dd6fSViresh Kumar 
6147813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
6157813dd6fSViresh Kumar 
6167813dd6fSViresh Kumar 	return opp;
6177813dd6fSViresh Kumar }
6187813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
6197813dd6fSViresh Kumar 
6207813dd6fSViresh Kumar /**
6217813dd6fSViresh Kumar  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
6227813dd6fSViresh Kumar  * @dev:	device for which we do this operation
6237813dd6fSViresh Kumar  * @freq:	Start frequency
6247813dd6fSViresh Kumar  *
6257813dd6fSViresh Kumar  * Search for the matching floor *available* OPP from a starting freq
6267813dd6fSViresh Kumar  * for a device.
6277813dd6fSViresh Kumar  *
6287813dd6fSViresh Kumar  * Return: matching *opp and refreshes *freq accordingly, else returns
6297813dd6fSViresh Kumar  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
6307813dd6fSViresh Kumar  * values can be:
6317813dd6fSViresh Kumar  * EINVAL:	for bad pointer
6327813dd6fSViresh Kumar  * ERANGE:	no match found for search
6337813dd6fSViresh Kumar  * ENODEV:	if device not found in list of registered devices
6347813dd6fSViresh Kumar  *
6357813dd6fSViresh Kumar  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6367813dd6fSViresh Kumar  * use.
6377813dd6fSViresh Kumar  */
6387813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
6397813dd6fSViresh Kumar 					      unsigned long *freq)
6407813dd6fSViresh Kumar {
6417813dd6fSViresh Kumar 	struct opp_table *opp_table;
6427813dd6fSViresh Kumar 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
6437813dd6fSViresh Kumar 
6447813dd6fSViresh Kumar 	if (!dev || !freq) {
6457813dd6fSViresh Kumar 		dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
6467813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
6477813dd6fSViresh Kumar 	}
6487813dd6fSViresh Kumar 
6497813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
6507813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
6517813dd6fSViresh Kumar 		return ERR_CAST(opp_table);
6527813dd6fSViresh Kumar 
6537813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
6547813dd6fSViresh Kumar 
6557813dd6fSViresh Kumar 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
6567813dd6fSViresh Kumar 		if (temp_opp->available) {
6577813dd6fSViresh Kumar 			/* go to the next node, before choosing prev */
6587813dd6fSViresh Kumar 			if (temp_opp->rate > *freq)
6597813dd6fSViresh Kumar 				break;
6607813dd6fSViresh Kumar 			else
6617813dd6fSViresh Kumar 				opp = temp_opp;
6627813dd6fSViresh Kumar 		}
6637813dd6fSViresh Kumar 	}
6647813dd6fSViresh Kumar 
6657813dd6fSViresh Kumar 	/* Increment the reference count of OPP */
6667813dd6fSViresh Kumar 	if (!IS_ERR(opp))
6677813dd6fSViresh Kumar 		dev_pm_opp_get(opp);
6687813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
6697813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
6707813dd6fSViresh Kumar 
6717813dd6fSViresh Kumar 	if (!IS_ERR(opp))
6727813dd6fSViresh Kumar 		*freq = opp->rate;
6737813dd6fSViresh Kumar 
6747813dd6fSViresh Kumar 	return opp;
6757813dd6fSViresh Kumar }
6767813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
6777813dd6fSViresh Kumar 
6782f36bde0SAndrew-sh.Cheng /**
6792f36bde0SAndrew-sh.Cheng  * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
6802f36bde0SAndrew-sh.Cheng  *					 target voltage.
6812f36bde0SAndrew-sh.Cheng  * @dev:	Device for which we do this operation.
6822f36bde0SAndrew-sh.Cheng  * @u_volt:	Target voltage.
6832f36bde0SAndrew-sh.Cheng  *
6842f36bde0SAndrew-sh.Cheng  * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
6852f36bde0SAndrew-sh.Cheng  *
6862f36bde0SAndrew-sh.Cheng  * Return: matching *opp, else returns ERR_PTR in case of error which should be
6872f36bde0SAndrew-sh.Cheng  * handled using IS_ERR.
6882f36bde0SAndrew-sh.Cheng  *
6892f36bde0SAndrew-sh.Cheng  * Error return values can be:
6902f36bde0SAndrew-sh.Cheng  * EINVAL:	bad parameters
6912f36bde0SAndrew-sh.Cheng  *
6922f36bde0SAndrew-sh.Cheng  * The callers are required to call dev_pm_opp_put() for the returned OPP after
6932f36bde0SAndrew-sh.Cheng  * use.
6942f36bde0SAndrew-sh.Cheng  */
6952f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
6962f36bde0SAndrew-sh.Cheng 						     unsigned long u_volt)
6972f36bde0SAndrew-sh.Cheng {
6982f36bde0SAndrew-sh.Cheng 	struct opp_table *opp_table;
6992f36bde0SAndrew-sh.Cheng 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
7002f36bde0SAndrew-sh.Cheng 
7012f36bde0SAndrew-sh.Cheng 	if (!dev || !u_volt) {
7022f36bde0SAndrew-sh.Cheng 		dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
7032f36bde0SAndrew-sh.Cheng 			u_volt);
7042f36bde0SAndrew-sh.Cheng 		return ERR_PTR(-EINVAL);
7052f36bde0SAndrew-sh.Cheng 	}
7062f36bde0SAndrew-sh.Cheng 
7072f36bde0SAndrew-sh.Cheng 	opp_table = _find_opp_table(dev);
7082f36bde0SAndrew-sh.Cheng 	if (IS_ERR(opp_table))
7092f36bde0SAndrew-sh.Cheng 		return ERR_CAST(opp_table);
7102f36bde0SAndrew-sh.Cheng 
7112f36bde0SAndrew-sh.Cheng 	mutex_lock(&opp_table->lock);
7122f36bde0SAndrew-sh.Cheng 
7132f36bde0SAndrew-sh.Cheng 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
7142f36bde0SAndrew-sh.Cheng 		if (temp_opp->available) {
7152f36bde0SAndrew-sh.Cheng 			if (temp_opp->supplies[0].u_volt > u_volt)
7162f36bde0SAndrew-sh.Cheng 				break;
7172f36bde0SAndrew-sh.Cheng 			opp = temp_opp;
7182f36bde0SAndrew-sh.Cheng 		}
7192f36bde0SAndrew-sh.Cheng 	}
7202f36bde0SAndrew-sh.Cheng 
7212f36bde0SAndrew-sh.Cheng 	/* Increment the reference count of OPP */
7222f36bde0SAndrew-sh.Cheng 	if (!IS_ERR(opp))
7232f36bde0SAndrew-sh.Cheng 		dev_pm_opp_get(opp);
7242f36bde0SAndrew-sh.Cheng 
7252f36bde0SAndrew-sh.Cheng 	mutex_unlock(&opp_table->lock);
7262f36bde0SAndrew-sh.Cheng 	dev_pm_opp_put_opp_table(opp_table);
7272f36bde0SAndrew-sh.Cheng 
7282f36bde0SAndrew-sh.Cheng 	return opp;
7292f36bde0SAndrew-sh.Cheng }
7302f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
7312f36bde0SAndrew-sh.Cheng 
7327813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg,
7337813dd6fSViresh Kumar 			    struct dev_pm_opp_supply *supply)
7347813dd6fSViresh Kumar {
7357813dd6fSViresh Kumar 	int ret;
7367813dd6fSViresh Kumar 
7377813dd6fSViresh Kumar 	/* Regulator not available for device */
7387813dd6fSViresh Kumar 	if (IS_ERR(reg)) {
7397813dd6fSViresh Kumar 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
7407813dd6fSViresh Kumar 			PTR_ERR(reg));
7417813dd6fSViresh Kumar 		return 0;
7427813dd6fSViresh Kumar 	}
7437813dd6fSViresh Kumar 
7447813dd6fSViresh Kumar 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
7457813dd6fSViresh Kumar 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
7467813dd6fSViresh Kumar 
7477813dd6fSViresh Kumar 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
7487813dd6fSViresh Kumar 					    supply->u_volt, supply->u_volt_max);
7497813dd6fSViresh Kumar 	if (ret)
7507813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
7517813dd6fSViresh Kumar 			__func__, supply->u_volt_min, supply->u_volt,
7527813dd6fSViresh Kumar 			supply->u_volt_max, ret);
7537813dd6fSViresh Kumar 
7547813dd6fSViresh Kumar 	return ret;
7557813dd6fSViresh Kumar }
7567813dd6fSViresh Kumar 
757285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
758285881b5SViresh Kumar 					    unsigned long freq)
7597813dd6fSViresh Kumar {
7607813dd6fSViresh Kumar 	int ret;
7617813dd6fSViresh Kumar 
76235e74b2eSViresh Kumar 	/* We may reach here for devices which don't change frequency */
76335e74b2eSViresh Kumar 	if (IS_ERR(clk))
76435e74b2eSViresh Kumar 		return 0;
76535e74b2eSViresh Kumar 
7667813dd6fSViresh Kumar 	ret = clk_set_rate(clk, freq);
7677813dd6fSViresh Kumar 	if (ret) {
7687813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
7697813dd6fSViresh Kumar 			ret);
7707813dd6fSViresh Kumar 	}
7717813dd6fSViresh Kumar 
7727813dd6fSViresh Kumar 	return ret;
7737813dd6fSViresh Kumar }
7747813dd6fSViresh Kumar 
7758d45719cSKamil Konieczny static int _generic_set_opp_regulator(struct opp_table *opp_table,
7767813dd6fSViresh Kumar 				      struct device *dev,
7773f62670fSViresh Kumar 				      struct dev_pm_opp *opp,
7787813dd6fSViresh Kumar 				      unsigned long freq,
7793f62670fSViresh Kumar 				      int scaling_down)
7807813dd6fSViresh Kumar {
7817813dd6fSViresh Kumar 	struct regulator *reg = opp_table->regulators[0];
7823f62670fSViresh Kumar 	struct dev_pm_opp *old_opp = opp_table->current_opp;
7837813dd6fSViresh Kumar 	int ret;
7847813dd6fSViresh Kumar 
7857813dd6fSViresh Kumar 	/* This function only supports single regulator per device */
7867813dd6fSViresh Kumar 	if (WARN_ON(opp_table->regulator_count > 1)) {
7877813dd6fSViresh Kumar 		dev_err(dev, "multiple regulators are not supported\n");
7887813dd6fSViresh Kumar 		return -EINVAL;
7897813dd6fSViresh Kumar 	}
7907813dd6fSViresh Kumar 
7917813dd6fSViresh Kumar 	/* Scaling up? Scale voltage before frequency */
7923f62670fSViresh Kumar 	if (!scaling_down) {
7933f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
7947813dd6fSViresh Kumar 		if (ret)
7957813dd6fSViresh Kumar 			goto restore_voltage;
7967813dd6fSViresh Kumar 	}
7977813dd6fSViresh Kumar 
7987813dd6fSViresh Kumar 	/* Change frequency */
799285881b5SViresh Kumar 	ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
8007813dd6fSViresh Kumar 	if (ret)
8017813dd6fSViresh Kumar 		goto restore_voltage;
8027813dd6fSViresh Kumar 
8037813dd6fSViresh Kumar 	/* Scaling down? Scale voltage after frequency */
8043f62670fSViresh Kumar 	if (scaling_down) {
8053f62670fSViresh Kumar 		ret = _set_opp_voltage(dev, reg, opp->supplies);
8067813dd6fSViresh Kumar 		if (ret)
8077813dd6fSViresh Kumar 			goto restore_freq;
8087813dd6fSViresh Kumar 	}
8097813dd6fSViresh Kumar 
8108d45719cSKamil Konieczny 	/*
8118d45719cSKamil Konieczny 	 * Enable the regulator after setting its voltages, otherwise it breaks
8128d45719cSKamil Konieczny 	 * some boot-enabled regulators.
8138d45719cSKamil Konieczny 	 */
81472f80ce4SViresh Kumar 	if (unlikely(!opp_table->enabled)) {
8158d45719cSKamil Konieczny 		ret = regulator_enable(reg);
8168d45719cSKamil Konieczny 		if (ret < 0)
8178d45719cSKamil Konieczny 			dev_warn(dev, "Failed to enable regulator: %d", ret);
8188d45719cSKamil Konieczny 	}
8198d45719cSKamil Konieczny 
8207813dd6fSViresh Kumar 	return 0;
8217813dd6fSViresh Kumar 
8227813dd6fSViresh Kumar restore_freq:
8233f62670fSViresh Kumar 	if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate))
8247813dd6fSViresh Kumar 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
8253f62670fSViresh Kumar 			__func__, old_opp->rate);
8267813dd6fSViresh Kumar restore_voltage:
8277813dd6fSViresh Kumar 	/* This shouldn't harm even if the voltages weren't updated earlier */
8283f62670fSViresh Kumar 	_set_opp_voltage(dev, reg, old_opp->supplies);
8297813dd6fSViresh Kumar 
8307813dd6fSViresh Kumar 	return ret;
8317813dd6fSViresh Kumar }
8327813dd6fSViresh Kumar 
833b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table,
834240ae50eSViresh Kumar 		       struct dev_pm_opp *opp, struct device *dev)
835b00e667aSViresh Kumar {
836b00e667aSViresh Kumar 	u32 avg, peak;
837b00e667aSViresh Kumar 	int i, ret;
838b00e667aSViresh Kumar 
839b00e667aSViresh Kumar 	if (!opp_table->paths)
840b00e667aSViresh Kumar 		return 0;
841b00e667aSViresh Kumar 
842b00e667aSViresh Kumar 	for (i = 0; i < opp_table->path_count; i++) {
843240ae50eSViresh Kumar 		if (!opp) {
844b00e667aSViresh Kumar 			avg = 0;
845b00e667aSViresh Kumar 			peak = 0;
846b00e667aSViresh Kumar 		} else {
847b00e667aSViresh Kumar 			avg = opp->bandwidth[i].avg;
848b00e667aSViresh Kumar 			peak = opp->bandwidth[i].peak;
849b00e667aSViresh Kumar 		}
850b00e667aSViresh Kumar 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
851b00e667aSViresh Kumar 		if (ret) {
852b00e667aSViresh Kumar 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
853240ae50eSViresh Kumar 				opp ? "set" : "remove", i, ret);
854b00e667aSViresh Kumar 			return ret;
855b00e667aSViresh Kumar 		}
856b00e667aSViresh Kumar 	}
857b00e667aSViresh Kumar 
858b00e667aSViresh Kumar 	return 0;
859b00e667aSViresh Kumar }
860b00e667aSViresh Kumar 
8617e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table,
862509e4777SViresh Kumar 			   struct device *dev, struct dev_pm_opp *opp,
863509e4777SViresh Kumar 			   unsigned long freq)
8647e535993SViresh Kumar {
86504b447dfSDmitry Osipenko 	struct dev_pm_set_opp_data *data = opp_table->set_opp_data;
866509e4777SViresh Kumar 	struct dev_pm_opp *old_opp = opp_table->current_opp;
8677e535993SViresh Kumar 	int size;
8687e535993SViresh Kumar 
86904b447dfSDmitry Osipenko 	/*
87004b447dfSDmitry Osipenko 	 * We support this only if dev_pm_opp_set_regulators() was called
87104b447dfSDmitry Osipenko 	 * earlier.
87204b447dfSDmitry Osipenko 	 */
87304b447dfSDmitry Osipenko 	if (opp_table->sod_supplies) {
874509e4777SViresh Kumar 		size = sizeof(*old_opp->supplies) * opp_table->regulator_count;
875509e4777SViresh Kumar 		memcpy(data->old_opp.supplies, old_opp->supplies, size);
876509e4777SViresh Kumar 		memcpy(data->new_opp.supplies, opp->supplies, size);
87704b447dfSDmitry Osipenko 		data->regulator_count = opp_table->regulator_count;
87804b447dfSDmitry Osipenko 	} else {
87904b447dfSDmitry Osipenko 		data->regulator_count = 0;
88004b447dfSDmitry Osipenko 	}
88104b447dfSDmitry Osipenko 
88204b447dfSDmitry Osipenko 	data->regulators = opp_table->regulators;
88304b447dfSDmitry Osipenko 	data->clk = opp_table->clk;
88404b447dfSDmitry Osipenko 	data->dev = dev;
885509e4777SViresh Kumar 	data->old_opp.rate = old_opp->rate;
88604b447dfSDmitry Osipenko 	data->new_opp.rate = freq;
8877e535993SViresh Kumar 
8887e535993SViresh Kumar 	return opp_table->set_opp(data);
8897e535993SViresh Kumar }
8907e535993SViresh Kumar 
89160cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev,
89260cdeae0SStephan Gerhold 			     struct dev_pm_opp *opp, int i)
89360cdeae0SStephan Gerhold {
89460cdeae0SStephan Gerhold 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
89560cdeae0SStephan Gerhold 	int ret;
89660cdeae0SStephan Gerhold 
89760cdeae0SStephan Gerhold 	if (!pd_dev)
89860cdeae0SStephan Gerhold 		return 0;
89960cdeae0SStephan Gerhold 
90060cdeae0SStephan Gerhold 	ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
90160cdeae0SStephan Gerhold 	if (ret) {
90260cdeae0SStephan Gerhold 		dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
90360cdeae0SStephan Gerhold 			dev_name(pd_dev), pstate, ret);
90460cdeae0SStephan Gerhold 	}
90560cdeae0SStephan Gerhold 
90660cdeae0SStephan Gerhold 	return ret;
90760cdeae0SStephan Gerhold }
90860cdeae0SStephan Gerhold 
909ca1b5d77SViresh Kumar /* This is only called for PM domain for now */
910ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev,
911ca1b5d77SViresh Kumar 			      struct opp_table *opp_table,
9122c59138cSStephan Gerhold 			      struct dev_pm_opp *opp, bool up)
913ca1b5d77SViresh Kumar {
914ca1b5d77SViresh Kumar 	struct opp_table **required_opp_tables = opp_table->required_opp_tables;
915ca1b5d77SViresh Kumar 	struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
916ca1b5d77SViresh Kumar 	int i, ret = 0;
917ca1b5d77SViresh Kumar 
918ca1b5d77SViresh Kumar 	if (!required_opp_tables)
919ca1b5d77SViresh Kumar 		return 0;
920ca1b5d77SViresh Kumar 
92119526d09SMarijn Suijten 	/* required-opps not fully initialized yet */
92219526d09SMarijn Suijten 	if (lazy_linking_pending(opp_table))
92319526d09SMarijn Suijten 		return -EBUSY;
92419526d09SMarijn Suijten 
9254fa82a87SHsin-Yi Wang 	/*
9264fa82a87SHsin-Yi Wang 	 * We only support genpd's OPPs in the "required-opps" for now, as we
9274fa82a87SHsin-Yi Wang 	 * don't know much about other use cases. Error out if the required OPP
9284fa82a87SHsin-Yi Wang 	 * doesn't belong to a genpd.
9294fa82a87SHsin-Yi Wang 	 */
9304fa82a87SHsin-Yi Wang 	if (unlikely(!required_opp_tables[0]->is_genpd)) {
9314fa82a87SHsin-Yi Wang 		dev_err(dev, "required-opps don't belong to a genpd\n");
9324fa82a87SHsin-Yi Wang 		return -ENOENT;
9334fa82a87SHsin-Yi Wang 	}
9344fa82a87SHsin-Yi Wang 
935ca1b5d77SViresh Kumar 	/* Single genpd case */
93660cdeae0SStephan Gerhold 	if (!genpd_virt_devs)
93760cdeae0SStephan Gerhold 		return _set_required_opp(dev, dev, opp, 0);
938ca1b5d77SViresh Kumar 
939ca1b5d77SViresh Kumar 	/* Multiple genpd case */
940ca1b5d77SViresh Kumar 
941ca1b5d77SViresh Kumar 	/*
942ca1b5d77SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
943ca1b5d77SViresh Kumar 	 * after it is freed from another thread.
944ca1b5d77SViresh Kumar 	 */
945ca1b5d77SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
946ca1b5d77SViresh Kumar 
9472c59138cSStephan Gerhold 	/* Scaling up? Set required OPPs in normal order, else reverse */
9482c59138cSStephan Gerhold 	if (up) {
949ca1b5d77SViresh Kumar 		for (i = 0; i < opp_table->required_opp_count; i++) {
95060cdeae0SStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
95160cdeae0SStephan Gerhold 			if (ret)
952ca1b5d77SViresh Kumar 				break;
953ca1b5d77SViresh Kumar 		}
9542c59138cSStephan Gerhold 	} else {
9552c59138cSStephan Gerhold 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
9562c59138cSStephan Gerhold 			ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
9572c59138cSStephan Gerhold 			if (ret)
958ca1b5d77SViresh Kumar 				break;
959ca1b5d77SViresh Kumar 		}
960ca1b5d77SViresh Kumar 	}
9612c59138cSStephan Gerhold 
962ca1b5d77SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
963ca1b5d77SViresh Kumar 
964ca1b5d77SViresh Kumar 	return ret;
965ca1b5d77SViresh Kumar }
966ca1b5d77SViresh Kumar 
96781c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
96881c4d8a3SViresh Kumar {
96981c4d8a3SViresh Kumar 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
97081c4d8a3SViresh Kumar 	unsigned long freq;
97181c4d8a3SViresh Kumar 
97281c4d8a3SViresh Kumar 	if (!IS_ERR(opp_table->clk)) {
97381c4d8a3SViresh Kumar 		freq = clk_get_rate(opp_table->clk);
97481c4d8a3SViresh Kumar 		opp = _find_freq_ceil(opp_table, &freq);
97581c4d8a3SViresh Kumar 	}
97681c4d8a3SViresh Kumar 
97781c4d8a3SViresh Kumar 	/*
97881c4d8a3SViresh Kumar 	 * Unable to find the current OPP ? Pick the first from the list since
97981c4d8a3SViresh Kumar 	 * it is in ascending order, otherwise rest of the code will need to
98081c4d8a3SViresh Kumar 	 * make special checks to validate current_opp.
98181c4d8a3SViresh Kumar 	 */
98281c4d8a3SViresh Kumar 	if (IS_ERR(opp)) {
98381c4d8a3SViresh Kumar 		mutex_lock(&opp_table->lock);
98481c4d8a3SViresh Kumar 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
98581c4d8a3SViresh Kumar 		dev_pm_opp_get(opp);
98681c4d8a3SViresh Kumar 		mutex_unlock(&opp_table->lock);
98781c4d8a3SViresh Kumar 	}
98881c4d8a3SViresh Kumar 
98981c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
99081c4d8a3SViresh Kumar }
99181c4d8a3SViresh Kumar 
9925ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
993f3364e17SViresh Kumar {
994f3364e17SViresh Kumar 	int ret;
995f3364e17SViresh Kumar 
996f3364e17SViresh Kumar 	if (!opp_table->enabled)
997f3364e17SViresh Kumar 		return 0;
998f3364e17SViresh Kumar 
999f3364e17SViresh Kumar 	/*
1000f3364e17SViresh Kumar 	 * Some drivers need to support cases where some platforms may
1001f3364e17SViresh Kumar 	 * have OPP table for the device, while others don't and
1002f3364e17SViresh Kumar 	 * opp_set_rate() just needs to behave like clk_set_rate().
1003f3364e17SViresh Kumar 	 */
1004f3364e17SViresh Kumar 	if (!_get_opp_count(opp_table))
1005f3364e17SViresh Kumar 		return 0;
1006f3364e17SViresh Kumar 
1007240ae50eSViresh Kumar 	ret = _set_opp_bw(opp_table, NULL, dev);
1008f3364e17SViresh Kumar 	if (ret)
1009f3364e17SViresh Kumar 		return ret;
1010f3364e17SViresh Kumar 
1011f3364e17SViresh Kumar 	if (opp_table->regulators)
1012f3364e17SViresh Kumar 		regulator_disable(opp_table->regulators[0]);
1013f3364e17SViresh Kumar 
10142c59138cSStephan Gerhold 	ret = _set_required_opps(dev, opp_table, NULL, false);
1015f3364e17SViresh Kumar 
1016f3364e17SViresh Kumar 	opp_table->enabled = false;
1017f3364e17SViresh Kumar 	return ret;
1018f3364e17SViresh Kumar }
1019f3364e17SViresh Kumar 
1020386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table,
1021386ba854SViresh Kumar 		    struct dev_pm_opp *opp, unsigned long freq)
10227813dd6fSViresh Kumar {
1023386ba854SViresh Kumar 	struct dev_pm_opp *old_opp;
1024f0b88fa4SViresh Kumar 	int scaling_down, ret;
10257813dd6fSViresh Kumar 
1026386ba854SViresh Kumar 	if (unlikely(!opp))
1027386ba854SViresh Kumar 		return _disable_opp_table(dev, opp_table);
1028aca48b61SRajendra Nayak 
102981c4d8a3SViresh Kumar 	/* Find the currently set OPP if we don't know already */
103081c4d8a3SViresh Kumar 	if (unlikely(!opp_table->current_opp))
103181c4d8a3SViresh Kumar 		_find_current_opp(dev, opp_table);
10327813dd6fSViresh Kumar 
103381c4d8a3SViresh Kumar 	old_opp = opp_table->current_opp;
103481c4d8a3SViresh Kumar 
103581c4d8a3SViresh Kumar 	/* Return early if nothing to do */
1036de04241aSJonathan Marek 	if (old_opp == opp && opp_table->current_rate == freq &&
1037de04241aSJonathan Marek 	    opp_table->enabled) {
103881c4d8a3SViresh Kumar 		dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
1039386ba854SViresh Kumar 		return 0;
10407813dd6fSViresh Kumar 	}
10417813dd6fSViresh Kumar 
1042f0b88fa4SViresh Kumar 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1043de04241aSJonathan Marek 		__func__, opp_table->current_rate, freq, old_opp->level,
1044de04241aSJonathan Marek 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1045f0b88fa4SViresh Kumar 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1046f0b88fa4SViresh Kumar 
1047f0b88fa4SViresh Kumar 	scaling_down = _opp_compare_key(old_opp, opp);
1048f0b88fa4SViresh Kumar 	if (scaling_down == -1)
1049f0b88fa4SViresh Kumar 		scaling_down = 0;
10507813dd6fSViresh Kumar 
1051ca1b5d77SViresh Kumar 	/* Scaling up? Configure required OPPs before frequency */
1052f0b88fa4SViresh Kumar 	if (!scaling_down) {
10532c59138cSStephan Gerhold 		ret = _set_required_opps(dev, opp_table, opp, true);
1054870d5d96SViresh Kumar 		if (ret) {
1055870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1056386ba854SViresh Kumar 			return ret;
1057ca1b5d77SViresh Kumar 		}
1058ca1b5d77SViresh Kumar 
1059870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1060870d5d96SViresh Kumar 		if (ret) {
1061870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1062870d5d96SViresh Kumar 			return ret;
1063870d5d96SViresh Kumar 		}
1064870d5d96SViresh Kumar 	}
1065870d5d96SViresh Kumar 
10667e535993SViresh Kumar 	if (opp_table->set_opp) {
1067509e4777SViresh Kumar 		ret = _set_opp_custom(opp_table, dev, opp, freq);
10687e535993SViresh Kumar 	} else if (opp_table->regulators) {
10693f62670fSViresh Kumar 		ret = _generic_set_opp_regulator(opp_table, dev, opp, freq,
10703f62670fSViresh Kumar 						 scaling_down);
10717813dd6fSViresh Kumar 	} else {
10727813dd6fSViresh Kumar 		/* Only frequency scaling */
10731d3c42caSViresh Kumar 		ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
10747813dd6fSViresh Kumar 	}
10757813dd6fSViresh Kumar 
1076ca1b5d77SViresh Kumar 	if (ret)
1077870d5d96SViresh Kumar 		return ret;
1078870d5d96SViresh Kumar 
1079870d5d96SViresh Kumar 	/* Scaling down? Configure required OPPs after frequency */
1080870d5d96SViresh Kumar 	if (scaling_down) {
1081870d5d96SViresh Kumar 		ret = _set_opp_bw(opp_table, opp, dev);
1082870d5d96SViresh Kumar 		if (ret) {
1083870d5d96SViresh Kumar 			dev_err(dev, "Failed to set bw: %d\n", ret);
1084870d5d96SViresh Kumar 			return ret;
1085ca1b5d77SViresh Kumar 		}
1086ca1b5d77SViresh Kumar 
1087870d5d96SViresh Kumar 		ret = _set_required_opps(dev, opp_table, opp, false);
1088870d5d96SViresh Kumar 		if (ret) {
1089870d5d96SViresh Kumar 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1090870d5d96SViresh Kumar 			return ret;
1091870d5d96SViresh Kumar 		}
1092870d5d96SViresh Kumar 	}
1093870d5d96SViresh Kumar 
109472f80ce4SViresh Kumar 	opp_table->enabled = true;
109581c4d8a3SViresh Kumar 	dev_pm_opp_put(old_opp);
109681c4d8a3SViresh Kumar 
109781c4d8a3SViresh Kumar 	/* Make sure current_opp doesn't get freed */
109881c4d8a3SViresh Kumar 	dev_pm_opp_get(opp);
109981c4d8a3SViresh Kumar 	opp_table->current_opp = opp;
1100de04241aSJonathan Marek 	opp_table->current_rate = freq;
1101fe2af402SGeorgi Djakov 
1102386ba854SViresh Kumar 	return ret;
1103386ba854SViresh Kumar }
1104386ba854SViresh Kumar 
1105386ba854SViresh Kumar /**
1106386ba854SViresh Kumar  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1107386ba854SViresh Kumar  * @dev:	 device for which we do this operation
1108386ba854SViresh Kumar  * @target_freq: frequency to achieve
1109386ba854SViresh Kumar  *
1110386ba854SViresh Kumar  * This configures the power-supplies to the levels specified by the OPP
1111386ba854SViresh Kumar  * corresponding to the target_freq, and programs the clock to a value <=
1112386ba854SViresh Kumar  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1113386ba854SViresh Kumar  * provided by the opp, should have already rounded to the target OPP's
1114386ba854SViresh Kumar  * frequency.
1115386ba854SViresh Kumar  */
1116386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1117386ba854SViresh Kumar {
1118386ba854SViresh Kumar 	struct opp_table *opp_table;
1119386ba854SViresh Kumar 	unsigned long freq = 0, temp_freq;
1120386ba854SViresh Kumar 	struct dev_pm_opp *opp = NULL;
1121386ba854SViresh Kumar 	int ret;
1122386ba854SViresh Kumar 
1123386ba854SViresh Kumar 	opp_table = _find_opp_table(dev);
1124386ba854SViresh Kumar 	if (IS_ERR(opp_table)) {
1125386ba854SViresh Kumar 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1126386ba854SViresh Kumar 		return PTR_ERR(opp_table);
1127386ba854SViresh Kumar 	}
1128386ba854SViresh Kumar 
1129386ba854SViresh Kumar 	if (target_freq) {
1130386ba854SViresh Kumar 		/*
1131386ba854SViresh Kumar 		 * For IO devices which require an OPP on some platforms/SoCs
1132386ba854SViresh Kumar 		 * while just needing to scale the clock on some others
1133386ba854SViresh Kumar 		 * we look for empty OPP tables with just a clock handle and
1134386ba854SViresh Kumar 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1135386ba854SViresh Kumar 		 * equivalent to a clk_set_rate()
1136386ba854SViresh Kumar 		 */
1137386ba854SViresh Kumar 		if (!_get_opp_count(opp_table)) {
1138386ba854SViresh Kumar 			ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq);
1139386ba854SViresh Kumar 			goto put_opp_table;
1140386ba854SViresh Kumar 		}
1141386ba854SViresh Kumar 
1142386ba854SViresh Kumar 		freq = clk_round_rate(opp_table->clk, target_freq);
1143386ba854SViresh Kumar 		if ((long)freq <= 0)
1144386ba854SViresh Kumar 			freq = target_freq;
1145386ba854SViresh Kumar 
1146386ba854SViresh Kumar 		/*
1147386ba854SViresh Kumar 		 * The clock driver may support finer resolution of the
1148386ba854SViresh Kumar 		 * frequencies than the OPP table, don't update the frequency we
1149386ba854SViresh Kumar 		 * pass to clk_set_rate() here.
1150386ba854SViresh Kumar 		 */
1151386ba854SViresh Kumar 		temp_freq = freq;
1152386ba854SViresh Kumar 		opp = _find_freq_ceil(opp_table, &temp_freq);
1153386ba854SViresh Kumar 		if (IS_ERR(opp)) {
1154386ba854SViresh Kumar 			ret = PTR_ERR(opp);
1155386ba854SViresh Kumar 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1156386ba854SViresh Kumar 				__func__, freq, ret);
1157386ba854SViresh Kumar 			goto put_opp_table;
1158386ba854SViresh Kumar 		}
1159386ba854SViresh Kumar 	}
1160386ba854SViresh Kumar 
1161386ba854SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, freq);
1162386ba854SViresh Kumar 
1163386ba854SViresh Kumar 	if (target_freq)
11647813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
11657813dd6fSViresh Kumar put_opp_table:
11667813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
11677813dd6fSViresh Kumar 	return ret;
11687813dd6fSViresh Kumar }
11697813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
11707813dd6fSViresh Kumar 
1171abbe3483SViresh Kumar /**
1172abbe3483SViresh Kumar  * dev_pm_opp_set_opp() - Configure device for OPP
1173abbe3483SViresh Kumar  * @dev: device for which we do this operation
1174abbe3483SViresh Kumar  * @opp: OPP to set to
1175abbe3483SViresh Kumar  *
1176abbe3483SViresh Kumar  * This configures the device based on the properties of the OPP passed to this
1177abbe3483SViresh Kumar  * routine.
1178abbe3483SViresh Kumar  *
1179abbe3483SViresh Kumar  * Return: 0 on success, a negative error number otherwise.
1180abbe3483SViresh Kumar  */
1181abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1182abbe3483SViresh Kumar {
1183abbe3483SViresh Kumar 	struct opp_table *opp_table;
1184abbe3483SViresh Kumar 	int ret;
1185abbe3483SViresh Kumar 
1186abbe3483SViresh Kumar 	opp_table = _find_opp_table(dev);
1187abbe3483SViresh Kumar 	if (IS_ERR(opp_table)) {
1188abbe3483SViresh Kumar 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1189abbe3483SViresh Kumar 		return PTR_ERR(opp_table);
1190abbe3483SViresh Kumar 	}
1191abbe3483SViresh Kumar 
1192abbe3483SViresh Kumar 	ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0);
1193abbe3483SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1194abbe3483SViresh Kumar 
1195abbe3483SViresh Kumar 	return ret;
1196abbe3483SViresh Kumar }
1197abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1198abbe3483SViresh Kumar 
11997813dd6fSViresh Kumar /* OPP-dev Helpers */
12007813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev,
12017813dd6fSViresh Kumar 			    struct opp_table *opp_table)
12027813dd6fSViresh Kumar {
12037813dd6fSViresh Kumar 	opp_debug_unregister(opp_dev, opp_table);
12047813dd6fSViresh Kumar 	list_del(&opp_dev->node);
12057813dd6fSViresh Kumar 	kfree(opp_dev);
12067813dd6fSViresh Kumar }
12077813dd6fSViresh Kumar 
1208ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev,
12097813dd6fSViresh Kumar 				struct opp_table *opp_table)
12107813dd6fSViresh Kumar {
12117813dd6fSViresh Kumar 	struct opp_device *opp_dev;
12127813dd6fSViresh Kumar 
12137813dd6fSViresh Kumar 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
12147813dd6fSViresh Kumar 	if (!opp_dev)
12157813dd6fSViresh Kumar 		return NULL;
12167813dd6fSViresh Kumar 
12177813dd6fSViresh Kumar 	/* Initialize opp-dev */
12187813dd6fSViresh Kumar 	opp_dev->dev = dev;
12193d255699SViresh Kumar 
1220ef43f01aSViresh Kumar 	mutex_lock(&opp_table->lock);
12217813dd6fSViresh Kumar 	list_add(&opp_dev->node, &opp_table->dev_list);
1222ef43f01aSViresh Kumar 	mutex_unlock(&opp_table->lock);
12237813dd6fSViresh Kumar 
12247813dd6fSViresh Kumar 	/* Create debugfs entries for the opp_table */
1225a2dea4cbSGreg Kroah-Hartman 	opp_debug_register(opp_dev, opp_table);
1226283d55e6SViresh Kumar 
1227283d55e6SViresh Kumar 	return opp_dev;
1228283d55e6SViresh Kumar }
1229283d55e6SViresh Kumar 
1230eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index)
12317813dd6fSViresh Kumar {
12327813dd6fSViresh Kumar 	struct opp_table *opp_table;
12337813dd6fSViresh Kumar 	struct opp_device *opp_dev;
12347813dd6fSViresh Kumar 	int ret;
12357813dd6fSViresh Kumar 
12367813dd6fSViresh Kumar 	/*
12377813dd6fSViresh Kumar 	 * Allocate a new OPP table. In the infrequent case where a new
12387813dd6fSViresh Kumar 	 * device is needed to be added, we pay this penalty.
12397813dd6fSViresh Kumar 	 */
12407813dd6fSViresh Kumar 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
12417813dd6fSViresh Kumar 	if (!opp_table)
1242dd461cd9SStephan Gerhold 		return ERR_PTR(-ENOMEM);
12437813dd6fSViresh Kumar 
12443d255699SViresh Kumar 	mutex_init(&opp_table->lock);
12454f018bc0SViresh Kumar 	mutex_init(&opp_table->genpd_virt_dev_lock);
12467813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->dev_list);
12477eba0c76SViresh Kumar 	INIT_LIST_HEAD(&opp_table->lazy);
12487813dd6fSViresh Kumar 
124946f48acaSViresh Kumar 	/* Mark regulator count uninitialized */
125046f48acaSViresh Kumar 	opp_table->regulator_count = -1;
125146f48acaSViresh Kumar 
12527813dd6fSViresh Kumar 	opp_dev = _add_opp_dev(dev, opp_table);
12537813dd6fSViresh Kumar 	if (!opp_dev) {
1254dd461cd9SStephan Gerhold 		ret = -ENOMEM;
1255dd461cd9SStephan Gerhold 		goto err;
12567813dd6fSViresh Kumar 	}
12577813dd6fSViresh Kumar 
1258eb7c8743SViresh Kumar 	_of_init_opp_table(opp_table, dev, index);
12597813dd6fSViresh Kumar 
12606d3f922cSGeorgi Djakov 	/* Find interconnect path(s) for the device */
12616d3f922cSGeorgi Djakov 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1262dd461cd9SStephan Gerhold 	if (ret) {
1263dd461cd9SStephan Gerhold 		if (ret == -EPROBE_DEFER)
126432439ac7SViresh Kumar 			goto remove_opp_dev;
1265dd461cd9SStephan Gerhold 
12666d3f922cSGeorgi Djakov 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
12676d3f922cSGeorgi Djakov 			 __func__, ret);
1268dd461cd9SStephan Gerhold 	}
12696d3f922cSGeorgi Djakov 
12707813dd6fSViresh Kumar 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
12717813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp_table->opp_list);
12727813dd6fSViresh Kumar 	kref_init(&opp_table->kref);
12737813dd6fSViresh Kumar 
12747813dd6fSViresh Kumar 	return opp_table;
1275dd461cd9SStephan Gerhold 
1276976509bbSQuanyang Wang remove_opp_dev:
1277976509bbSQuanyang Wang 	_remove_opp_dev(opp_dev, opp_table);
1278dd461cd9SStephan Gerhold err:
1279dd461cd9SStephan Gerhold 	kfree(opp_table);
1280dd461cd9SStephan Gerhold 	return ERR_PTR(ret);
12817813dd6fSViresh Kumar }
12827813dd6fSViresh Kumar 
12837813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table)
12847813dd6fSViresh Kumar {
12857813dd6fSViresh Kumar 	kref_get(&opp_table->kref);
12867813dd6fSViresh Kumar }
12877813dd6fSViresh Kumar 
128832439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev,
128932439ac7SViresh Kumar 					       struct opp_table *opp_table,
129032439ac7SViresh Kumar 					       bool getclk)
129132439ac7SViresh Kumar {
1292d4a4c7a4SViresh Kumar 	int ret;
1293d4a4c7a4SViresh Kumar 
129432439ac7SViresh Kumar 	/*
129532439ac7SViresh Kumar 	 * Return early if we don't need to get clk or we have already tried it
129632439ac7SViresh Kumar 	 * earlier.
129732439ac7SViresh Kumar 	 */
129832439ac7SViresh Kumar 	if (!getclk || IS_ERR(opp_table) || opp_table->clk)
129932439ac7SViresh Kumar 		return opp_table;
130032439ac7SViresh Kumar 
130132439ac7SViresh Kumar 	/* Find clk for the device */
130232439ac7SViresh Kumar 	opp_table->clk = clk_get(dev, NULL);
130332439ac7SViresh Kumar 
1304d4a4c7a4SViresh Kumar 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
1305d4a4c7a4SViresh Kumar 	if (!ret)
130632439ac7SViresh Kumar 		return opp_table;
1307d4a4c7a4SViresh Kumar 
1308d4a4c7a4SViresh Kumar 	if (ret == -ENOENT) {
1309d4a4c7a4SViresh Kumar 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1310d4a4c7a4SViresh Kumar 		return opp_table;
1311d4a4c7a4SViresh Kumar 	}
1312d4a4c7a4SViresh Kumar 
1313d4a4c7a4SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
1314d4a4c7a4SViresh Kumar 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1315d4a4c7a4SViresh Kumar 
1316d4a4c7a4SViresh Kumar 	return ERR_PTR(ret);
131732439ac7SViresh Kumar }
131832439ac7SViresh Kumar 
131927c09484SViresh Kumar /*
132027c09484SViresh Kumar  * We need to make sure that the OPP table for a device doesn't get added twice,
132127c09484SViresh Kumar  * if this routine gets called in parallel with the same device pointer.
132227c09484SViresh Kumar  *
132327c09484SViresh Kumar  * The simplest way to enforce that is to perform everything (find existing
132427c09484SViresh Kumar  * table and if not found, create a new one) under the opp_table_lock, so only
132527c09484SViresh Kumar  * one creator gets access to the same. But that expands the critical section
132627c09484SViresh Kumar  * under the lock and may end up causing circular dependencies with frameworks
132727c09484SViresh Kumar  * like debugfs, interconnect or clock framework as they may be direct or
132827c09484SViresh Kumar  * indirect users of OPP core.
132927c09484SViresh Kumar  *
133027c09484SViresh Kumar  * And for that reason we have to go for a bit tricky implementation here, which
133127c09484SViresh Kumar  * uses the opp_tables_busy flag to indicate if another creator is in the middle
133227c09484SViresh Kumar  * of adding an OPP table and others should wait for it to finish.
133327c09484SViresh Kumar  */
133432439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
133532439ac7SViresh Kumar 					 bool getclk)
13367813dd6fSViresh Kumar {
13377813dd6fSViresh Kumar 	struct opp_table *opp_table;
13387813dd6fSViresh Kumar 
133927c09484SViresh Kumar again:
13407813dd6fSViresh Kumar 	mutex_lock(&opp_table_lock);
13417813dd6fSViresh Kumar 
13427813dd6fSViresh Kumar 	opp_table = _find_opp_table_unlocked(dev);
13437813dd6fSViresh Kumar 	if (!IS_ERR(opp_table))
13447813dd6fSViresh Kumar 		goto unlock;
13457813dd6fSViresh Kumar 
134627c09484SViresh Kumar 	/*
134727c09484SViresh Kumar 	 * The opp_tables list or an OPP table's dev_list is getting updated by
134827c09484SViresh Kumar 	 * another user, wait for it to finish.
134927c09484SViresh Kumar 	 */
135027c09484SViresh Kumar 	if (unlikely(opp_tables_busy)) {
135127c09484SViresh Kumar 		mutex_unlock(&opp_table_lock);
135227c09484SViresh Kumar 		cpu_relax();
135327c09484SViresh Kumar 		goto again;
135427c09484SViresh Kumar 	}
135527c09484SViresh Kumar 
135627c09484SViresh Kumar 	opp_tables_busy = true;
1357283d55e6SViresh Kumar 	opp_table = _managed_opp(dev, index);
135827c09484SViresh Kumar 
135927c09484SViresh Kumar 	/* Drop the lock to reduce the size of critical section */
136027c09484SViresh Kumar 	mutex_unlock(&opp_table_lock);
136127c09484SViresh Kumar 
1362283d55e6SViresh Kumar 	if (opp_table) {
1363ef43f01aSViresh Kumar 		if (!_add_opp_dev(dev, opp_table)) {
1364283d55e6SViresh Kumar 			dev_pm_opp_put_opp_table(opp_table);
1365dd461cd9SStephan Gerhold 			opp_table = ERR_PTR(-ENOMEM);
1366283d55e6SViresh Kumar 		}
136727c09484SViresh Kumar 
136827c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
136927c09484SViresh Kumar 	} else {
137027c09484SViresh Kumar 		opp_table = _allocate_opp_table(dev, index);
137127c09484SViresh Kumar 
137227c09484SViresh Kumar 		mutex_lock(&opp_table_lock);
137327c09484SViresh Kumar 		if (!IS_ERR(opp_table))
137427c09484SViresh Kumar 			list_add(&opp_table->node, &opp_tables);
1375283d55e6SViresh Kumar 	}
1376283d55e6SViresh Kumar 
137727c09484SViresh Kumar 	opp_tables_busy = false;
13787813dd6fSViresh Kumar 
13797813dd6fSViresh Kumar unlock:
13807813dd6fSViresh Kumar 	mutex_unlock(&opp_table_lock);
13817813dd6fSViresh Kumar 
138232439ac7SViresh Kumar 	return _update_opp_table_clk(dev, opp_table, getclk);
13837813dd6fSViresh Kumar }
1384eb7c8743SViresh Kumar 
138532439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1386e77dcb0bSViresh Kumar {
138732439ac7SViresh Kumar 	return _add_opp_table_indexed(dev, 0, getclk);
1388e77dcb0bSViresh Kumar }
1389e77dcb0bSViresh Kumar 
1390eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1391eb7c8743SViresh Kumar {
1392e77dcb0bSViresh Kumar 	return _find_opp_table(dev);
1393eb7c8743SViresh Kumar }
13947813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
13957813dd6fSViresh Kumar 
13967813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref)
13977813dd6fSViresh Kumar {
13987813dd6fSViresh Kumar 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1399cdd6ed90SViresh Kumar 	struct opp_device *opp_dev, *temp;
14006d3f922cSGeorgi Djakov 	int i;
14017813dd6fSViresh Kumar 
1402e0df59deSViresh Kumar 	/* Drop the lock as soon as we can */
1403e0df59deSViresh Kumar 	list_del(&opp_table->node);
1404e0df59deSViresh Kumar 	mutex_unlock(&opp_table_lock);
1405e0df59deSViresh Kumar 
140681c4d8a3SViresh Kumar 	if (opp_table->current_opp)
140781c4d8a3SViresh Kumar 		dev_pm_opp_put(opp_table->current_opp);
140881c4d8a3SViresh Kumar 
14095d6d106fSViresh Kumar 	_of_clear_opp_table(opp_table);
14105d6d106fSViresh Kumar 
14117813dd6fSViresh Kumar 	/* Release clk */
14127813dd6fSViresh Kumar 	if (!IS_ERR(opp_table->clk))
14137813dd6fSViresh Kumar 		clk_put(opp_table->clk);
14147813dd6fSViresh Kumar 
14156d3f922cSGeorgi Djakov 	if (opp_table->paths) {
14166d3f922cSGeorgi Djakov 		for (i = 0; i < opp_table->path_count; i++)
14176d3f922cSGeorgi Djakov 			icc_put(opp_table->paths[i]);
14186d3f922cSGeorgi Djakov 		kfree(opp_table->paths);
14196d3f922cSGeorgi Djakov 	}
14206d3f922cSGeorgi Djakov 
1421cdd6ed90SViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
1422cdd6ed90SViresh Kumar 
1423cdd6ed90SViresh Kumar 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
14243d255699SViresh Kumar 		/*
1425cdd6ed90SViresh Kumar 		 * The OPP table is getting removed, drop the performance state
1426cdd6ed90SViresh Kumar 		 * constraints.
14273d255699SViresh Kumar 		 */
1428cdd6ed90SViresh Kumar 		if (opp_table->genpd_performance_state)
1429cdd6ed90SViresh Kumar 			dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
14307813dd6fSViresh Kumar 
14317813dd6fSViresh Kumar 		_remove_opp_dev(opp_dev, opp_table);
1432cdd6ed90SViresh Kumar 	}
14337813dd6fSViresh Kumar 
14344f018bc0SViresh Kumar 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
14357813dd6fSViresh Kumar 	mutex_destroy(&opp_table->lock);
14367813dd6fSViresh Kumar 	kfree(opp_table);
14377813dd6fSViresh Kumar }
14387813dd6fSViresh Kumar 
14397813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
14407813dd6fSViresh Kumar {
14417813dd6fSViresh Kumar 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
14427813dd6fSViresh Kumar 		       &opp_table_lock);
14437813dd6fSViresh Kumar }
14447813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
14457813dd6fSViresh Kumar 
14467813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp)
14477813dd6fSViresh Kumar {
14487813dd6fSViresh Kumar 	kfree(opp);
14497813dd6fSViresh Kumar }
14507813dd6fSViresh Kumar 
1451cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref)
14527813dd6fSViresh Kumar {
1453cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1454cf1fac94SViresh Kumar 	struct opp_table *opp_table = opp->opp_table;
1455cf1fac94SViresh Kumar 
1456cf1fac94SViresh Kumar 	list_del(&opp->node);
1457cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1458cf1fac94SViresh Kumar 
14597813dd6fSViresh Kumar 	/*
14607813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
14617813dd6fSViresh Kumar 	 * frequency/voltage list.
14627813dd6fSViresh Kumar 	 */
14637813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1464da544b61SViresh Kumar 	_of_opp_free_required_opps(opp_table, opp);
14657813dd6fSViresh Kumar 	opp_debug_remove_one(opp);
14667813dd6fSViresh Kumar 	kfree(opp);
14671690d8bbSViresh Kumar }
14687813dd6fSViresh Kumar 
1469a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp)
14707813dd6fSViresh Kumar {
14717813dd6fSViresh Kumar 	kref_get(&opp->kref);
14727813dd6fSViresh Kumar }
14737813dd6fSViresh Kumar 
14747813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp)
14757813dd6fSViresh Kumar {
1476cf1fac94SViresh Kumar 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
14777813dd6fSViresh Kumar }
14787813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put);
14797813dd6fSViresh Kumar 
14807813dd6fSViresh Kumar /**
14817813dd6fSViresh Kumar  * dev_pm_opp_remove()  - Remove an OPP from OPP table
14827813dd6fSViresh Kumar  * @dev:	device for which we do this operation
14837813dd6fSViresh Kumar  * @freq:	OPP to remove with matching 'freq'
14847813dd6fSViresh Kumar  *
14857813dd6fSViresh Kumar  * This function removes an opp from the opp table.
14867813dd6fSViresh Kumar  */
14877813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq)
14887813dd6fSViresh Kumar {
1489*95073b72SJakob Koschel 	struct dev_pm_opp *opp = NULL, *iter;
14907813dd6fSViresh Kumar 	struct opp_table *opp_table;
14917813dd6fSViresh Kumar 
14927813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
14937813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
14947813dd6fSViresh Kumar 		return;
14957813dd6fSViresh Kumar 
14967813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
14977813dd6fSViresh Kumar 
1498*95073b72SJakob Koschel 	list_for_each_entry(iter, &opp_table->opp_list, node) {
1499*95073b72SJakob Koschel 		if (iter->rate == freq) {
1500*95073b72SJakob Koschel 			opp = iter;
15017813dd6fSViresh Kumar 			break;
15027813dd6fSViresh Kumar 		}
15037813dd6fSViresh Kumar 	}
15047813dd6fSViresh Kumar 
15057813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
15067813dd6fSViresh Kumar 
1507*95073b72SJakob Koschel 	if (opp) {
15087813dd6fSViresh Kumar 		dev_pm_opp_put(opp);
15090ad8c623SViresh Kumar 
15100ad8c623SViresh Kumar 		/* Drop the reference taken by dev_pm_opp_add() */
15110ad8c623SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
15127813dd6fSViresh Kumar 	} else {
15137813dd6fSViresh Kumar 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
15147813dd6fSViresh Kumar 			 __func__, freq);
15157813dd6fSViresh Kumar 	}
15167813dd6fSViresh Kumar 
15170ad8c623SViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
15187813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
15197813dd6fSViresh Kumar }
15207813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
15217813dd6fSViresh Kumar 
1522cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1523cf1fac94SViresh Kumar 					bool dynamic)
1524cf1fac94SViresh Kumar {
1525cf1fac94SViresh Kumar 	struct dev_pm_opp *opp = NULL, *temp;
1526cf1fac94SViresh Kumar 
1527cf1fac94SViresh Kumar 	mutex_lock(&opp_table->lock);
1528cf1fac94SViresh Kumar 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1529606a5d42SBeata Michalska 		/*
1530606a5d42SBeata Michalska 		 * Refcount must be dropped only once for each OPP by OPP core,
1531606a5d42SBeata Michalska 		 * do that with help of "removed" flag.
1532606a5d42SBeata Michalska 		 */
1533606a5d42SBeata Michalska 		if (!temp->removed && dynamic == temp->dynamic) {
1534cf1fac94SViresh Kumar 			opp = temp;
1535cf1fac94SViresh Kumar 			break;
1536cf1fac94SViresh Kumar 		}
1537cf1fac94SViresh Kumar 	}
1538cf1fac94SViresh Kumar 
1539cf1fac94SViresh Kumar 	mutex_unlock(&opp_table->lock);
1540cf1fac94SViresh Kumar 	return opp;
1541cf1fac94SViresh Kumar }
1542cf1fac94SViresh Kumar 
1543606a5d42SBeata Michalska /*
1544606a5d42SBeata Michalska  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1545606a5d42SBeata Michalska  * happen lock less to avoid circular dependency issues. This routine must be
1546606a5d42SBeata Michalska  * called without the opp_table->lock held.
1547606a5d42SBeata Michalska  */
1548606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
154903758d60SViresh Kumar {
1550cf1fac94SViresh Kumar 	struct dev_pm_opp *opp;
155103758d60SViresh Kumar 
1552606a5d42SBeata Michalska 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1553606a5d42SBeata Michalska 		opp->removed = true;
1554606a5d42SBeata Michalska 		dev_pm_opp_put(opp);
1555606a5d42SBeata Michalska 
1556606a5d42SBeata Michalska 		/* Drop the references taken by dev_pm_opp_add() */
1557606a5d42SBeata Michalska 		if (dynamic)
1558606a5d42SBeata Michalska 			dev_pm_opp_put_opp_table(opp_table);
1559606a5d42SBeata Michalska 	}
1560606a5d42SBeata Michalska }
1561606a5d42SBeata Michalska 
1562606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table)
1563606a5d42SBeata Michalska {
156403758d60SViresh Kumar 	mutex_lock(&opp_table->lock);
156503758d60SViresh Kumar 
1566922ff075SViresh Kumar 	if (!opp_table->parsed_static_opps) {
1567cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1568cf1fac94SViresh Kumar 		return false;
1569922ff075SViresh Kumar 	}
1570922ff075SViresh Kumar 
1571cf1fac94SViresh Kumar 	if (--opp_table->parsed_static_opps) {
1572cf1fac94SViresh Kumar 		mutex_unlock(&opp_table->lock);
1573cf1fac94SViresh Kumar 		return true;
157403758d60SViresh Kumar 	}
157503758d60SViresh Kumar 
157603758d60SViresh Kumar 	mutex_unlock(&opp_table->lock);
1577922ff075SViresh Kumar 
1578606a5d42SBeata Michalska 	_opp_remove_all(opp_table, false);
1579cf1fac94SViresh Kumar 	return true;
158003758d60SViresh Kumar }
158103758d60SViresh Kumar 
15821690d8bbSViresh Kumar /**
15831690d8bbSViresh Kumar  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
15841690d8bbSViresh Kumar  * @dev:	device for which we do this operation
15851690d8bbSViresh Kumar  *
15861690d8bbSViresh Kumar  * This function removes all dynamically created OPPs from the opp table.
15871690d8bbSViresh Kumar  */
15881690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev)
15891690d8bbSViresh Kumar {
15901690d8bbSViresh Kumar 	struct opp_table *opp_table;
15911690d8bbSViresh Kumar 
15921690d8bbSViresh Kumar 	opp_table = _find_opp_table(dev);
15931690d8bbSViresh Kumar 	if (IS_ERR(opp_table))
15941690d8bbSViresh Kumar 		return;
15951690d8bbSViresh Kumar 
1596606a5d42SBeata Michalska 	_opp_remove_all(opp_table, true);
15971690d8bbSViresh Kumar 
15981690d8bbSViresh Kumar 	/* Drop the reference taken by _find_opp_table() */
15991690d8bbSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
16001690d8bbSViresh Kumar }
16011690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
16021690d8bbSViresh Kumar 
16037813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table)
16047813dd6fSViresh Kumar {
16057813dd6fSViresh Kumar 	struct dev_pm_opp *opp;
16066d3f922cSGeorgi Djakov 	int supply_count, supply_size, icc_size;
16077813dd6fSViresh Kumar 
16087813dd6fSViresh Kumar 	/* Allocate space for at least one supply */
16096d3f922cSGeorgi Djakov 	supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
16106d3f922cSGeorgi Djakov 	supply_size = sizeof(*opp->supplies) * supply_count;
16116d3f922cSGeorgi Djakov 	icc_size = sizeof(*opp->bandwidth) * table->path_count;
16127813dd6fSViresh Kumar 
16137813dd6fSViresh Kumar 	/* allocate new OPP node and supplies structures */
16146d3f922cSGeorgi Djakov 	opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
16156d3f922cSGeorgi Djakov 
16167813dd6fSViresh Kumar 	if (!opp)
16177813dd6fSViresh Kumar 		return NULL;
16187813dd6fSViresh Kumar 
16197813dd6fSViresh Kumar 	/* Put the supplies at the end of the OPP structure as an empty array */
16207813dd6fSViresh Kumar 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
16216d3f922cSGeorgi Djakov 	if (icc_size)
16226d3f922cSGeorgi Djakov 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
16237813dd6fSViresh Kumar 	INIT_LIST_HEAD(&opp->node);
16247813dd6fSViresh Kumar 
16257813dd6fSViresh Kumar 	return opp;
16267813dd6fSViresh Kumar }
16277813dd6fSViresh Kumar 
16287813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
16297813dd6fSViresh Kumar 					 struct opp_table *opp_table)
16307813dd6fSViresh Kumar {
16317813dd6fSViresh Kumar 	struct regulator *reg;
16327813dd6fSViresh Kumar 	int i;
16337813dd6fSViresh Kumar 
163490e3577bSViresh Kumar 	if (!opp_table->regulators)
163590e3577bSViresh Kumar 		return true;
163690e3577bSViresh Kumar 
16377813dd6fSViresh Kumar 	for (i = 0; i < opp_table->regulator_count; i++) {
16387813dd6fSViresh Kumar 		reg = opp_table->regulators[i];
16397813dd6fSViresh Kumar 
16407813dd6fSViresh Kumar 		if (!regulator_is_supported_voltage(reg,
16417813dd6fSViresh Kumar 					opp->supplies[i].u_volt_min,
16427813dd6fSViresh Kumar 					opp->supplies[i].u_volt_max)) {
16437813dd6fSViresh Kumar 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
16447813dd6fSViresh Kumar 				__func__, opp->supplies[i].u_volt_min,
16457813dd6fSViresh Kumar 				opp->supplies[i].u_volt_max);
16467813dd6fSViresh Kumar 			return false;
16477813dd6fSViresh Kumar 		}
16487813dd6fSViresh Kumar 	}
16497813dd6fSViresh Kumar 
16507813dd6fSViresh Kumar 	return true;
16517813dd6fSViresh Kumar }
16527813dd6fSViresh Kumar 
16536c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
16546c591eecSSaravana Kannan {
16556c591eecSSaravana Kannan 	if (opp1->rate != opp2->rate)
16566c591eecSSaravana Kannan 		return opp1->rate < opp2->rate ? -1 : 1;
16576d3f922cSGeorgi Djakov 	if (opp1->bandwidth && opp2->bandwidth &&
16586d3f922cSGeorgi Djakov 	    opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
16596d3f922cSGeorgi Djakov 		return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
16606c591eecSSaravana Kannan 	if (opp1->level != opp2->level)
16616c591eecSSaravana Kannan 		return opp1->level < opp2->level ? -1 : 1;
16626c591eecSSaravana Kannan 	return 0;
16636c591eecSSaravana Kannan }
16646c591eecSSaravana Kannan 
1665a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1666a1e8c136SViresh Kumar 			     struct opp_table *opp_table,
1667a1e8c136SViresh Kumar 			     struct list_head **head)
1668a1e8c136SViresh Kumar {
1669a1e8c136SViresh Kumar 	struct dev_pm_opp *opp;
16706c591eecSSaravana Kannan 	int opp_cmp;
1671a1e8c136SViresh Kumar 
1672a1e8c136SViresh Kumar 	/*
1673a1e8c136SViresh Kumar 	 * Insert new OPP in order of increasing frequency and discard if
1674a1e8c136SViresh Kumar 	 * already present.
1675a1e8c136SViresh Kumar 	 *
1676a1e8c136SViresh Kumar 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1677a1e8c136SViresh Kumar 	 * loop, don't replace it with head otherwise it will become an infinite
1678a1e8c136SViresh Kumar 	 * loop.
1679a1e8c136SViresh Kumar 	 */
1680a1e8c136SViresh Kumar 	list_for_each_entry(opp, &opp_table->opp_list, node) {
16816c591eecSSaravana Kannan 		opp_cmp = _opp_compare_key(new_opp, opp);
16826c591eecSSaravana Kannan 		if (opp_cmp > 0) {
1683a1e8c136SViresh Kumar 			*head = &opp->node;
1684a1e8c136SViresh Kumar 			continue;
1685a1e8c136SViresh Kumar 		}
1686a1e8c136SViresh Kumar 
16876c591eecSSaravana Kannan 		if (opp_cmp < 0)
1688a1e8c136SViresh Kumar 			return 0;
1689a1e8c136SViresh Kumar 
1690a1e8c136SViresh Kumar 		/* Duplicate OPPs */
1691a1e8c136SViresh Kumar 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1692a1e8c136SViresh Kumar 			 __func__, opp->rate, opp->supplies[0].u_volt,
1693a1e8c136SViresh Kumar 			 opp->available, new_opp->rate,
1694a1e8c136SViresh Kumar 			 new_opp->supplies[0].u_volt, new_opp->available);
1695a1e8c136SViresh Kumar 
1696a1e8c136SViresh Kumar 		/* Should we compare voltages for all regulators here ? */
1697a1e8c136SViresh Kumar 		return opp->available &&
1698a1e8c136SViresh Kumar 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1699a1e8c136SViresh Kumar 	}
1700a1e8c136SViresh Kumar 
1701a1e8c136SViresh Kumar 	return 0;
1702a1e8c136SViresh Kumar }
1703a1e8c136SViresh Kumar 
17047eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count)
17057eba0c76SViresh Kumar {
17067eba0c76SViresh Kumar 	int i;
17077eba0c76SViresh Kumar 
17087eba0c76SViresh Kumar 	for (i = 0; i < count; i++) {
17097eba0c76SViresh Kumar 		if (opp->required_opps[i]->available)
17107eba0c76SViresh Kumar 			continue;
17117eba0c76SViresh Kumar 
17127eba0c76SViresh Kumar 		opp->available = false;
17137eba0c76SViresh Kumar 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
17147eba0c76SViresh Kumar 			 __func__, opp->required_opps[i]->np, opp->rate);
17157eba0c76SViresh Kumar 		return;
17167eba0c76SViresh Kumar 	}
17177eba0c76SViresh Kumar }
17187eba0c76SViresh Kumar 
17197813dd6fSViresh Kumar /*
17207813dd6fSViresh Kumar  * Returns:
17217813dd6fSViresh Kumar  * 0: On success. And appropriate error message for duplicate OPPs.
17227813dd6fSViresh Kumar  * -EBUSY: For OPP with same freq/volt and is available. The callers of
17237813dd6fSViresh Kumar  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
17247813dd6fSViresh Kumar  *  sure we don't print error messages unnecessarily if different parts of
17257813dd6fSViresh Kumar  *  kernel try to initialize the OPP table.
17267813dd6fSViresh Kumar  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
17277813dd6fSViresh Kumar  *  should be considered an error by the callers of _opp_add().
17287813dd6fSViresh Kumar  */
17297813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1730a1e8c136SViresh Kumar 	     struct opp_table *opp_table, bool rate_not_available)
17317813dd6fSViresh Kumar {
17327813dd6fSViresh Kumar 	struct list_head *head;
17337813dd6fSViresh Kumar 	int ret;
17347813dd6fSViresh Kumar 
17357813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
17367813dd6fSViresh Kumar 	head = &opp_table->opp_list;
17377813dd6fSViresh Kumar 
1738a1e8c136SViresh Kumar 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1739a1e8c136SViresh Kumar 	if (ret) {
17407813dd6fSViresh Kumar 		mutex_unlock(&opp_table->lock);
17417813dd6fSViresh Kumar 		return ret;
17427813dd6fSViresh Kumar 	}
17437813dd6fSViresh Kumar 
17447813dd6fSViresh Kumar 	list_add(&new_opp->node, head);
17457813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
17467813dd6fSViresh Kumar 
17477813dd6fSViresh Kumar 	new_opp->opp_table = opp_table;
17487813dd6fSViresh Kumar 	kref_init(&new_opp->kref);
17497813dd6fSViresh Kumar 
1750a2dea4cbSGreg Kroah-Hartman 	opp_debug_create_one(new_opp, opp_table);
17517813dd6fSViresh Kumar 
17527813dd6fSViresh Kumar 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
17537813dd6fSViresh Kumar 		new_opp->available = false;
17547813dd6fSViresh Kumar 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
17557813dd6fSViresh Kumar 			 __func__, new_opp->rate);
17567813dd6fSViresh Kumar 	}
17577813dd6fSViresh Kumar 
17587eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
17597eba0c76SViresh Kumar 	if (lazy_linking_pending(opp_table))
17607eba0c76SViresh Kumar 		return 0;
1761cf65948dSDmitry Osipenko 
17627eba0c76SViresh Kumar 	_required_opps_available(new_opp, opp_table->required_opp_count);
1763cf65948dSDmitry Osipenko 
17647813dd6fSViresh Kumar 	return 0;
17657813dd6fSViresh Kumar }
17667813dd6fSViresh Kumar 
17677813dd6fSViresh Kumar /**
17687813dd6fSViresh Kumar  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
17697813dd6fSViresh Kumar  * @opp_table:	OPP table
17707813dd6fSViresh Kumar  * @dev:	device for which we do this operation
17717813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
17727813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
17737813dd6fSViresh Kumar  * @dynamic:	Dynamically added OPPs.
17747813dd6fSViresh Kumar  *
17757813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
17767813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
17777813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
17787813dd6fSViresh Kumar  *
17797813dd6fSViresh Kumar  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
17807813dd6fSViresh Kumar  * and freed by dev_pm_opp_of_remove_table.
17817813dd6fSViresh Kumar  *
17827813dd6fSViresh Kumar  * Return:
17837813dd6fSViresh Kumar  * 0		On success OR
17847813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
17857813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
17867813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
17877813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
17887813dd6fSViresh Kumar  */
17897813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
17907813dd6fSViresh Kumar 		unsigned long freq, long u_volt, bool dynamic)
17917813dd6fSViresh Kumar {
17927813dd6fSViresh Kumar 	struct dev_pm_opp *new_opp;
17937813dd6fSViresh Kumar 	unsigned long tol;
17947813dd6fSViresh Kumar 	int ret;
17957813dd6fSViresh Kumar 
17967813dd6fSViresh Kumar 	new_opp = _opp_allocate(opp_table);
17977813dd6fSViresh Kumar 	if (!new_opp)
17987813dd6fSViresh Kumar 		return -ENOMEM;
17997813dd6fSViresh Kumar 
18007813dd6fSViresh Kumar 	/* populate the opp table */
18017813dd6fSViresh Kumar 	new_opp->rate = freq;
18027813dd6fSViresh Kumar 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
18037813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt = u_volt;
18047813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_min = u_volt - tol;
18057813dd6fSViresh Kumar 	new_opp->supplies[0].u_volt_max = u_volt + tol;
18067813dd6fSViresh Kumar 	new_opp->available = true;
18077813dd6fSViresh Kumar 	new_opp->dynamic = dynamic;
18087813dd6fSViresh Kumar 
1809a1e8c136SViresh Kumar 	ret = _opp_add(dev, new_opp, opp_table, false);
18107813dd6fSViresh Kumar 	if (ret) {
18117813dd6fSViresh Kumar 		/* Don't return error for duplicate OPPs */
18127813dd6fSViresh Kumar 		if (ret == -EBUSY)
18137813dd6fSViresh Kumar 			ret = 0;
18147813dd6fSViresh Kumar 		goto free_opp;
18157813dd6fSViresh Kumar 	}
18167813dd6fSViresh Kumar 
18177813dd6fSViresh Kumar 	/*
18187813dd6fSViresh Kumar 	 * Notify the changes in the availability of the operable
18197813dd6fSViresh Kumar 	 * frequency/voltage list.
18207813dd6fSViresh Kumar 	 */
18217813dd6fSViresh Kumar 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
18227813dd6fSViresh Kumar 	return 0;
18237813dd6fSViresh Kumar 
18247813dd6fSViresh Kumar free_opp:
18257813dd6fSViresh Kumar 	_opp_free(new_opp);
18267813dd6fSViresh Kumar 
18277813dd6fSViresh Kumar 	return ret;
18287813dd6fSViresh Kumar }
18297813dd6fSViresh Kumar 
18307813dd6fSViresh Kumar /**
18317813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw() - Set supported platforms
18327813dd6fSViresh Kumar  * @dev: Device for which supported-hw has to be set.
18337813dd6fSViresh Kumar  * @versions: Array of hierarchy of versions to match.
18347813dd6fSViresh Kumar  * @count: Number of elements in the array.
18357813dd6fSViresh Kumar  *
18367813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
18377813dd6fSViresh Kumar  * specify the hierarchy of versions it supports. OPP layer will then enable
18387813dd6fSViresh Kumar  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
18397813dd6fSViresh Kumar  * property.
18407813dd6fSViresh Kumar  */
18417813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
18427813dd6fSViresh Kumar 			const u32 *versions, unsigned int count)
18437813dd6fSViresh Kumar {
18447813dd6fSViresh Kumar 	struct opp_table *opp_table;
18457813dd6fSViresh Kumar 
184632439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1847dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1848dd461cd9SStephan Gerhold 		return opp_table;
18497813dd6fSViresh Kumar 
18507813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
18517813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
18527813dd6fSViresh Kumar 
185325419de1SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
185425419de1SViresh Kumar 	if (opp_table->supported_hw)
185525419de1SViresh Kumar 		return opp_table;
18567813dd6fSViresh Kumar 
18577813dd6fSViresh Kumar 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
18587813dd6fSViresh Kumar 					GFP_KERNEL);
18597813dd6fSViresh Kumar 	if (!opp_table->supported_hw) {
186025419de1SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
186125419de1SViresh Kumar 		return ERR_PTR(-ENOMEM);
18627813dd6fSViresh Kumar 	}
18637813dd6fSViresh Kumar 
18647813dd6fSViresh Kumar 	opp_table->supported_hw_count = count;
18657813dd6fSViresh Kumar 
18667813dd6fSViresh Kumar 	return opp_table;
18677813dd6fSViresh Kumar }
18687813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
18697813dd6fSViresh Kumar 
18707813dd6fSViresh Kumar /**
18717813dd6fSViresh Kumar  * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
18727813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
18737813dd6fSViresh Kumar  *
18747813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
18757813dd6fSViresh Kumar  * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
18767813dd6fSViresh Kumar  * will not be freed.
18777813dd6fSViresh Kumar  */
18787813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
18797813dd6fSViresh Kumar {
1880c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
1881c7bf8758SViresh Kumar 		return;
1882c7bf8758SViresh Kumar 
18837813dd6fSViresh Kumar 	kfree(opp_table->supported_hw);
18847813dd6fSViresh Kumar 	opp_table->supported_hw = NULL;
18857813dd6fSViresh Kumar 	opp_table->supported_hw_count = 0;
18867813dd6fSViresh Kumar 
18877813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
18887813dd6fSViresh Kumar }
18897813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
18907813dd6fSViresh Kumar 
18919c4f220fSYangtao Li static void devm_pm_opp_supported_hw_release(void *data)
18929c4f220fSYangtao Li {
18939c4f220fSYangtao Li 	dev_pm_opp_put_supported_hw(data);
18949c4f220fSYangtao Li }
18959c4f220fSYangtao Li 
18969c4f220fSYangtao Li /**
18979c4f220fSYangtao Li  * devm_pm_opp_set_supported_hw() - Set supported platforms
18989c4f220fSYangtao Li  * @dev: Device for which supported-hw has to be set.
18999c4f220fSYangtao Li  * @versions: Array of hierarchy of versions to match.
19009c4f220fSYangtao Li  * @count: Number of elements in the array.
19019c4f220fSYangtao Li  *
19029c4f220fSYangtao Li  * This is a resource-managed variant of dev_pm_opp_set_supported_hw().
19039c4f220fSYangtao Li  *
19049c4f220fSYangtao Li  * Return: 0 on success and errorno otherwise.
19059c4f220fSYangtao Li  */
19069c4f220fSYangtao Li int devm_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
19079c4f220fSYangtao Li 				 unsigned int count)
19089c4f220fSYangtao Li {
19099c4f220fSYangtao Li 	struct opp_table *opp_table;
19109c4f220fSYangtao Li 
19119c4f220fSYangtao Li 	opp_table = dev_pm_opp_set_supported_hw(dev, versions, count);
19129c4f220fSYangtao Li 	if (IS_ERR(opp_table))
19139c4f220fSYangtao Li 		return PTR_ERR(opp_table);
19149c4f220fSYangtao Li 
19159c4f220fSYangtao Li 	return devm_add_action_or_reset(dev, devm_pm_opp_supported_hw_release,
19169c4f220fSYangtao Li 					opp_table);
19179c4f220fSYangtao Li }
19189c4f220fSYangtao Li EXPORT_SYMBOL_GPL(devm_pm_opp_set_supported_hw);
19199c4f220fSYangtao Li 
19207813dd6fSViresh Kumar /**
19217813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name() - Set prop-extn name
19227813dd6fSViresh Kumar  * @dev: Device for which the prop-name has to be set.
19237813dd6fSViresh Kumar  * @name: name to postfix to properties.
19247813dd6fSViresh Kumar  *
19257813dd6fSViresh Kumar  * This is required only for the V2 bindings, and it enables a platform to
19267813dd6fSViresh Kumar  * specify the extn to be used for certain property names. The properties to
19277813dd6fSViresh Kumar  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
19287813dd6fSViresh Kumar  * should postfix the property name with -<name> while looking for them.
19297813dd6fSViresh Kumar  */
19307813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
19317813dd6fSViresh Kumar {
19327813dd6fSViresh Kumar 	struct opp_table *opp_table;
19337813dd6fSViresh Kumar 
193432439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1935dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1936dd461cd9SStephan Gerhold 		return opp_table;
19377813dd6fSViresh Kumar 
19387813dd6fSViresh Kumar 	/* Make sure there are no concurrent readers while updating opp_table */
19397813dd6fSViresh Kumar 	WARN_ON(!list_empty(&opp_table->opp_list));
19407813dd6fSViresh Kumar 
1941878ec1a9SViresh Kumar 	/* Another CPU that shares the OPP table has set the property ? */
1942878ec1a9SViresh Kumar 	if (opp_table->prop_name)
1943878ec1a9SViresh Kumar 		return opp_table;
19447813dd6fSViresh Kumar 
19457813dd6fSViresh Kumar 	opp_table->prop_name = kstrdup(name, GFP_KERNEL);
19467813dd6fSViresh Kumar 	if (!opp_table->prop_name) {
1947878ec1a9SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
1948878ec1a9SViresh Kumar 		return ERR_PTR(-ENOMEM);
19497813dd6fSViresh Kumar 	}
19507813dd6fSViresh Kumar 
19517813dd6fSViresh Kumar 	return opp_table;
19527813dd6fSViresh Kumar }
19537813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
19547813dd6fSViresh Kumar 
19557813dd6fSViresh Kumar /**
19567813dd6fSViresh Kumar  * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
19577813dd6fSViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
19587813dd6fSViresh Kumar  *
19597813dd6fSViresh Kumar  * This is required only for the V2 bindings, and is called for a matching
19607813dd6fSViresh Kumar  * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
19617813dd6fSViresh Kumar  * will not be freed.
19627813dd6fSViresh Kumar  */
19637813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
19647813dd6fSViresh Kumar {
1965c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
1966c7bf8758SViresh Kumar 		return;
1967c7bf8758SViresh Kumar 
19687813dd6fSViresh Kumar 	kfree(opp_table->prop_name);
19697813dd6fSViresh Kumar 	opp_table->prop_name = NULL;
19707813dd6fSViresh Kumar 
19717813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
19727813dd6fSViresh Kumar }
19737813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
19747813dd6fSViresh Kumar 
19757813dd6fSViresh Kumar /**
19767813dd6fSViresh Kumar  * dev_pm_opp_set_regulators() - Set regulator names for the device
19777813dd6fSViresh Kumar  * @dev: Device for which regulator name is being set.
19787813dd6fSViresh Kumar  * @names: Array of pointers to the names of the regulator.
19797813dd6fSViresh Kumar  * @count: Number of regulators.
19807813dd6fSViresh Kumar  *
19817813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to know the name of the
19827813dd6fSViresh Kumar  * device's regulators, as the core would be required to switch voltages as
19837813dd6fSViresh Kumar  * well.
19847813dd6fSViresh Kumar  *
19857813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
19867813dd6fSViresh Kumar  */
19877813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
19887813dd6fSViresh Kumar 					    const char * const names[],
19897813dd6fSViresh Kumar 					    unsigned int count)
19907813dd6fSViresh Kumar {
199138bb3439SViresh Kumar 	struct dev_pm_opp_supply *supplies;
19927813dd6fSViresh Kumar 	struct opp_table *opp_table;
19937813dd6fSViresh Kumar 	struct regulator *reg;
19947813dd6fSViresh Kumar 	int ret, i;
19957813dd6fSViresh Kumar 
199632439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
1997dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
1998dd461cd9SStephan Gerhold 		return opp_table;
19997813dd6fSViresh Kumar 
20007813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
20017813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
20027813dd6fSViresh Kumar 		ret = -EBUSY;
20037813dd6fSViresh Kumar 		goto err;
20047813dd6fSViresh Kumar 	}
20057813dd6fSViresh Kumar 
2006779b783cSViresh Kumar 	/* Another CPU that shares the OPP table has set the regulators ? */
2007779b783cSViresh Kumar 	if (opp_table->regulators)
2008779b783cSViresh Kumar 		return opp_table;
20097813dd6fSViresh Kumar 
20107813dd6fSViresh Kumar 	opp_table->regulators = kmalloc_array(count,
20117813dd6fSViresh Kumar 					      sizeof(*opp_table->regulators),
20127813dd6fSViresh Kumar 					      GFP_KERNEL);
20137813dd6fSViresh Kumar 	if (!opp_table->regulators) {
20147813dd6fSViresh Kumar 		ret = -ENOMEM;
20157813dd6fSViresh Kumar 		goto err;
20167813dd6fSViresh Kumar 	}
20177813dd6fSViresh Kumar 
20187813dd6fSViresh Kumar 	for (i = 0; i < count; i++) {
20197813dd6fSViresh Kumar 		reg = regulator_get_optional(dev, names[i]);
20207813dd6fSViresh Kumar 		if (IS_ERR(reg)) {
2021543256d2SKrzysztof Kozlowski 			ret = dev_err_probe(dev, PTR_ERR(reg),
2022543256d2SKrzysztof Kozlowski 					    "%s: no regulator (%s) found\n",
2023543256d2SKrzysztof Kozlowski 					    __func__, names[i]);
20247813dd6fSViresh Kumar 			goto free_regulators;
20257813dd6fSViresh Kumar 		}
20267813dd6fSViresh Kumar 
20277813dd6fSViresh Kumar 		opp_table->regulators[i] = reg;
20287813dd6fSViresh Kumar 	}
20297813dd6fSViresh Kumar 
20307813dd6fSViresh Kumar 	opp_table->regulator_count = count;
20317813dd6fSViresh Kumar 
203238bb3439SViresh Kumar 	supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
203338bb3439SViresh Kumar 	if (!supplies) {
203438bb3439SViresh Kumar 		ret = -ENOMEM;
20357813dd6fSViresh Kumar 		goto free_regulators;
203638bb3439SViresh Kumar 	}
203738bb3439SViresh Kumar 
203838bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
203938bb3439SViresh Kumar 	opp_table->sod_supplies = supplies;
204038bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
204138bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = supplies;
204238bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = supplies + count;
204338bb3439SViresh Kumar 	}
204438bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
20457813dd6fSViresh Kumar 
20467813dd6fSViresh Kumar 	return opp_table;
20477813dd6fSViresh Kumar 
20487813dd6fSViresh Kumar free_regulators:
204924957db1SMarek Szyprowski 	while (i != 0)
205024957db1SMarek Szyprowski 		regulator_put(opp_table->regulators[--i]);
20517813dd6fSViresh Kumar 
20527813dd6fSViresh Kumar 	kfree(opp_table->regulators);
20537813dd6fSViresh Kumar 	opp_table->regulators = NULL;
205446f48acaSViresh Kumar 	opp_table->regulator_count = -1;
20557813dd6fSViresh Kumar err:
20567813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
20577813dd6fSViresh Kumar 
20587813dd6fSViresh Kumar 	return ERR_PTR(ret);
20597813dd6fSViresh Kumar }
20607813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
20617813dd6fSViresh Kumar 
20627813dd6fSViresh Kumar /**
20637813dd6fSViresh Kumar  * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
20647813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
20657813dd6fSViresh Kumar  */
20667813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table)
20677813dd6fSViresh Kumar {
20687813dd6fSViresh Kumar 	int i;
20697813dd6fSViresh Kumar 
2070c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2071c7bf8758SViresh Kumar 		return;
2072c7bf8758SViresh Kumar 
2073779b783cSViresh Kumar 	if (!opp_table->regulators)
2074779b783cSViresh Kumar 		goto put_opp_table;
20757813dd6fSViresh Kumar 
207672f80ce4SViresh Kumar 	if (opp_table->enabled) {
20778d45719cSKamil Konieczny 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
20788d45719cSKamil Konieczny 			regulator_disable(opp_table->regulators[i]);
20798d45719cSKamil Konieczny 	}
20808d45719cSKamil Konieczny 
208124957db1SMarek Szyprowski 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
20827813dd6fSViresh Kumar 		regulator_put(opp_table->regulators[i]);
20837813dd6fSViresh Kumar 
208438bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
208538bb3439SViresh Kumar 	if (opp_table->set_opp_data) {
208638bb3439SViresh Kumar 		opp_table->set_opp_data->old_opp.supplies = NULL;
208738bb3439SViresh Kumar 		opp_table->set_opp_data->new_opp.supplies = NULL;
208838bb3439SViresh Kumar 	}
208938bb3439SViresh Kumar 
209038bb3439SViresh Kumar 	kfree(opp_table->sod_supplies);
209138bb3439SViresh Kumar 	opp_table->sod_supplies = NULL;
209238bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
20937813dd6fSViresh Kumar 
20947813dd6fSViresh Kumar 	kfree(opp_table->regulators);
20957813dd6fSViresh Kumar 	opp_table->regulators = NULL;
209646f48acaSViresh Kumar 	opp_table->regulator_count = -1;
20977813dd6fSViresh Kumar 
2098779b783cSViresh Kumar put_opp_table:
20997813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
21007813dd6fSViresh Kumar }
21017813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
21027813dd6fSViresh Kumar 
210332aee78bSYangtao Li static void devm_pm_opp_regulators_release(void *data)
210432aee78bSYangtao Li {
210532aee78bSYangtao Li 	dev_pm_opp_put_regulators(data);
210632aee78bSYangtao Li }
210732aee78bSYangtao Li 
210832aee78bSYangtao Li /**
210932aee78bSYangtao Li  * devm_pm_opp_set_regulators() - Set regulator names for the device
211032aee78bSYangtao Li  * @dev: Device for which regulator name is being set.
211132aee78bSYangtao Li  * @names: Array of pointers to the names of the regulator.
211232aee78bSYangtao Li  * @count: Number of regulators.
211332aee78bSYangtao Li  *
211432aee78bSYangtao Li  * This is a resource-managed variant of dev_pm_opp_set_regulators().
211532aee78bSYangtao Li  *
211632aee78bSYangtao Li  * Return: 0 on success and errorno otherwise.
211732aee78bSYangtao Li  */
211832aee78bSYangtao Li int devm_pm_opp_set_regulators(struct device *dev,
211932aee78bSYangtao Li 			       const char * const names[],
212032aee78bSYangtao Li 			       unsigned int count)
212132aee78bSYangtao Li {
212232aee78bSYangtao Li 	struct opp_table *opp_table;
212332aee78bSYangtao Li 
212432aee78bSYangtao Li 	opp_table = dev_pm_opp_set_regulators(dev, names, count);
212532aee78bSYangtao Li 	if (IS_ERR(opp_table))
212632aee78bSYangtao Li 		return PTR_ERR(opp_table);
212732aee78bSYangtao Li 
212832aee78bSYangtao Li 	return devm_add_action_or_reset(dev, devm_pm_opp_regulators_release,
212932aee78bSYangtao Li 					opp_table);
213032aee78bSYangtao Li }
213132aee78bSYangtao Li EXPORT_SYMBOL_GPL(devm_pm_opp_set_regulators);
213232aee78bSYangtao Li 
21337813dd6fSViresh Kumar /**
21347813dd6fSViresh Kumar  * dev_pm_opp_set_clkname() - Set clk name for the device
21357813dd6fSViresh Kumar  * @dev: Device for which clk name is being set.
21367813dd6fSViresh Kumar  * @name: Clk name.
21377813dd6fSViresh Kumar  *
21387813dd6fSViresh Kumar  * In order to support OPP switching, OPP layer needs to get pointer to the
21397813dd6fSViresh Kumar  * clock for the device. Simple cases work fine without using this routine (i.e.
21407813dd6fSViresh Kumar  * by passing connection-id as NULL), but for a device with multiple clocks
21417813dd6fSViresh Kumar  * available, the OPP core needs to know the exact name of the clk to use.
21427813dd6fSViresh Kumar  *
21437813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
21447813dd6fSViresh Kumar  */
21457813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
21467813dd6fSViresh Kumar {
21477813dd6fSViresh Kumar 	struct opp_table *opp_table;
21487813dd6fSViresh Kumar 	int ret;
21497813dd6fSViresh Kumar 
215032439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2151dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2152dd461cd9SStephan Gerhold 		return opp_table;
21537813dd6fSViresh Kumar 
21547813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
21557813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
21567813dd6fSViresh Kumar 		ret = -EBUSY;
21577813dd6fSViresh Kumar 		goto err;
21587813dd6fSViresh Kumar 	}
21597813dd6fSViresh Kumar 
216032439ac7SViresh Kumar 	/* clk shouldn't be initialized at this point */
216132439ac7SViresh Kumar 	if (WARN_ON(opp_table->clk)) {
216232439ac7SViresh Kumar 		ret = -EBUSY;
216332439ac7SViresh Kumar 		goto err;
216432439ac7SViresh Kumar 	}
21657813dd6fSViresh Kumar 
21667813dd6fSViresh Kumar 	/* Find clk for the device */
21677813dd6fSViresh Kumar 	opp_table->clk = clk_get(dev, name);
21687813dd6fSViresh Kumar 	if (IS_ERR(opp_table->clk)) {
2169543256d2SKrzysztof Kozlowski 		ret = dev_err_probe(dev, PTR_ERR(opp_table->clk),
2170543256d2SKrzysztof Kozlowski 				    "%s: Couldn't find clock\n", __func__);
21717813dd6fSViresh Kumar 		goto err;
21727813dd6fSViresh Kumar 	}
21737813dd6fSViresh Kumar 
21747813dd6fSViresh Kumar 	return opp_table;
21757813dd6fSViresh Kumar 
21767813dd6fSViresh Kumar err:
21777813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
21787813dd6fSViresh Kumar 
21797813dd6fSViresh Kumar 	return ERR_PTR(ret);
21807813dd6fSViresh Kumar }
21817813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
21827813dd6fSViresh Kumar 
21837813dd6fSViresh Kumar /**
21847813dd6fSViresh Kumar  * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
21857813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
21867813dd6fSViresh Kumar  */
21877813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table)
21887813dd6fSViresh Kumar {
2189c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2190c7bf8758SViresh Kumar 		return;
2191c7bf8758SViresh Kumar 
21927813dd6fSViresh Kumar 	clk_put(opp_table->clk);
21937813dd6fSViresh Kumar 	opp_table->clk = ERR_PTR(-EINVAL);
21947813dd6fSViresh Kumar 
21957813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
21967813dd6fSViresh Kumar }
21977813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
21987813dd6fSViresh Kumar 
2199a74f681cSYangtao Li static void devm_pm_opp_clkname_release(void *data)
2200a74f681cSYangtao Li {
2201a74f681cSYangtao Li 	dev_pm_opp_put_clkname(data);
2202a74f681cSYangtao Li }
2203a74f681cSYangtao Li 
2204a74f681cSYangtao Li /**
2205a74f681cSYangtao Li  * devm_pm_opp_set_clkname() - Set clk name for the device
2206a74f681cSYangtao Li  * @dev: Device for which clk name is being set.
2207a74f681cSYangtao Li  * @name: Clk name.
2208a74f681cSYangtao Li  *
2209a74f681cSYangtao Li  * This is a resource-managed variant of dev_pm_opp_set_clkname().
2210a74f681cSYangtao Li  *
2211a74f681cSYangtao Li  * Return: 0 on success and errorno otherwise.
2212a74f681cSYangtao Li  */
2213a74f681cSYangtao Li int devm_pm_opp_set_clkname(struct device *dev, const char *name)
2214a74f681cSYangtao Li {
2215a74f681cSYangtao Li 	struct opp_table *opp_table;
2216a74f681cSYangtao Li 
2217a74f681cSYangtao Li 	opp_table = dev_pm_opp_set_clkname(dev, name);
2218a74f681cSYangtao Li 	if (IS_ERR(opp_table))
2219a74f681cSYangtao Li 		return PTR_ERR(opp_table);
2220a74f681cSYangtao Li 
2221a74f681cSYangtao Li 	return devm_add_action_or_reset(dev, devm_pm_opp_clkname_release,
2222a74f681cSYangtao Li 					opp_table);
2223a74f681cSYangtao Li }
2224a74f681cSYangtao Li EXPORT_SYMBOL_GPL(devm_pm_opp_set_clkname);
2225a74f681cSYangtao Li 
22267813dd6fSViresh Kumar /**
22277813dd6fSViresh Kumar  * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
22287813dd6fSViresh Kumar  * @dev: Device for which the helper is getting registered.
22297813dd6fSViresh Kumar  * @set_opp: Custom set OPP helper.
22307813dd6fSViresh Kumar  *
22317813dd6fSViresh Kumar  * This is useful to support complex platforms (like platforms with multiple
22327813dd6fSViresh Kumar  * regulators per device), instead of the generic OPP set rate helper.
22337813dd6fSViresh Kumar  *
22347813dd6fSViresh Kumar  * This must be called before any OPPs are initialized for the device.
22357813dd6fSViresh Kumar  */
22367813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
22377813dd6fSViresh Kumar 			int (*set_opp)(struct dev_pm_set_opp_data *data))
22387813dd6fSViresh Kumar {
223938bb3439SViresh Kumar 	struct dev_pm_set_opp_data *data;
22407813dd6fSViresh Kumar 	struct opp_table *opp_table;
22417813dd6fSViresh Kumar 
22427813dd6fSViresh Kumar 	if (!set_opp)
22437813dd6fSViresh Kumar 		return ERR_PTR(-EINVAL);
22447813dd6fSViresh Kumar 
224532439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
224647efcbcbSViresh Kumar 	if (IS_ERR(opp_table))
2247dd461cd9SStephan Gerhold 		return opp_table;
22487813dd6fSViresh Kumar 
22497813dd6fSViresh Kumar 	/* This should be called before OPPs are initialized */
22507813dd6fSViresh Kumar 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
22515019acc6SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
22525019acc6SViresh Kumar 		return ERR_PTR(-EBUSY);
22537813dd6fSViresh Kumar 	}
22547813dd6fSViresh Kumar 
22555019acc6SViresh Kumar 	/* Another CPU that shares the OPP table has set the helper ? */
225638bb3439SViresh Kumar 	if (opp_table->set_opp)
225738bb3439SViresh Kumar 		return opp_table;
225838bb3439SViresh Kumar 
225938bb3439SViresh Kumar 	data = kzalloc(sizeof(*data), GFP_KERNEL);
226038bb3439SViresh Kumar 	if (!data)
226138bb3439SViresh Kumar 		return ERR_PTR(-ENOMEM);
226238bb3439SViresh Kumar 
226338bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
226438bb3439SViresh Kumar 	opp_table->set_opp_data = data;
226538bb3439SViresh Kumar 	if (opp_table->sod_supplies) {
226638bb3439SViresh Kumar 		data->old_opp.supplies = opp_table->sod_supplies;
226738bb3439SViresh Kumar 		data->new_opp.supplies = opp_table->sod_supplies +
226838bb3439SViresh Kumar 					 opp_table->regulator_count;
226938bb3439SViresh Kumar 	}
227038bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
227138bb3439SViresh Kumar 
22727813dd6fSViresh Kumar 	opp_table->set_opp = set_opp;
22737813dd6fSViresh Kumar 
22747813dd6fSViresh Kumar 	return opp_table;
22757813dd6fSViresh Kumar }
22767813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
22777813dd6fSViresh Kumar 
22787813dd6fSViresh Kumar /**
2279604a7aebSViresh Kumar  * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
22807813dd6fSViresh Kumar  *					   set_opp helper
22817813dd6fSViresh Kumar  * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
22827813dd6fSViresh Kumar  *
22837813dd6fSViresh Kumar  * Release resources blocked for platform specific set_opp helper.
22847813dd6fSViresh Kumar  */
2285604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
22867813dd6fSViresh Kumar {
2287c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2288c7bf8758SViresh Kumar 		return;
2289c7bf8758SViresh Kumar 
22907813dd6fSViresh Kumar 	opp_table->set_opp = NULL;
229138bb3439SViresh Kumar 
229238bb3439SViresh Kumar 	mutex_lock(&opp_table->lock);
229338bb3439SViresh Kumar 	kfree(opp_table->set_opp_data);
229438bb3439SViresh Kumar 	opp_table->set_opp_data = NULL;
229538bb3439SViresh Kumar 	mutex_unlock(&opp_table->lock);
229638bb3439SViresh Kumar 
22977813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
22987813dd6fSViresh Kumar }
2299604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
23007813dd6fSViresh Kumar 
2301a3c47af6SDmitry Osipenko static void devm_pm_opp_unregister_set_opp_helper(void *data)
2302a3c47af6SDmitry Osipenko {
2303a3c47af6SDmitry Osipenko 	dev_pm_opp_unregister_set_opp_helper(data);
2304a3c47af6SDmitry Osipenko }
2305a3c47af6SDmitry Osipenko 
2306a3c47af6SDmitry Osipenko /**
2307a3c47af6SDmitry Osipenko  * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2308a3c47af6SDmitry Osipenko  * @dev: Device for which the helper is getting registered.
2309a3c47af6SDmitry Osipenko  * @set_opp: Custom set OPP helper.
2310a3c47af6SDmitry Osipenko  *
2311a3c47af6SDmitry Osipenko  * This is a resource-managed version of dev_pm_opp_register_set_opp_helper().
2312a3c47af6SDmitry Osipenko  *
2313c41c8a34SDmitry Osipenko  * Return: 0 on success and errorno otherwise.
2314a3c47af6SDmitry Osipenko  */
2315c41c8a34SDmitry Osipenko int devm_pm_opp_register_set_opp_helper(struct device *dev,
2316a3c47af6SDmitry Osipenko 					int (*set_opp)(struct dev_pm_set_opp_data *data))
2317a3c47af6SDmitry Osipenko {
2318a3c47af6SDmitry Osipenko 	struct opp_table *opp_table;
2319a3c47af6SDmitry Osipenko 
2320a3c47af6SDmitry Osipenko 	opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp);
2321a3c47af6SDmitry Osipenko 	if (IS_ERR(opp_table))
2322c41c8a34SDmitry Osipenko 		return PTR_ERR(opp_table);
2323a3c47af6SDmitry Osipenko 
2324c41c8a34SDmitry Osipenko 	return devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper,
2325a3c47af6SDmitry Osipenko 					opp_table);
2326a3c47af6SDmitry Osipenko }
2327a3c47af6SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper);
2328a3c47af6SDmitry Osipenko 
23296319aee1SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table)
23306319aee1SViresh Kumar {
23316319aee1SViresh Kumar 	int index;
23326319aee1SViresh Kumar 
2333cb60e960SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2334cb60e960SViresh Kumar 		return;
2335cb60e960SViresh Kumar 
23366319aee1SViresh Kumar 	for (index = 0; index < opp_table->required_opp_count; index++) {
23376319aee1SViresh Kumar 		if (!opp_table->genpd_virt_devs[index])
23386319aee1SViresh Kumar 			continue;
23396319aee1SViresh Kumar 
23406319aee1SViresh Kumar 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
23416319aee1SViresh Kumar 		opp_table->genpd_virt_devs[index] = NULL;
23426319aee1SViresh Kumar 	}
2343c0ab9e08SViresh Kumar 
2344c0ab9e08SViresh Kumar 	kfree(opp_table->genpd_virt_devs);
2345c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = NULL;
23466319aee1SViresh Kumar }
23476319aee1SViresh Kumar 
23487813dd6fSViresh Kumar /**
23496319aee1SViresh Kumar  * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
23506319aee1SViresh Kumar  * @dev: Consumer device for which the genpd is getting attached.
23516319aee1SViresh Kumar  * @names: Null terminated array of pointers containing names of genpd to attach.
235217a8f868SViresh Kumar  * @virt_devs: Pointer to return the array of virtual devices.
23534f018bc0SViresh Kumar  *
23544f018bc0SViresh Kumar  * Multiple generic power domains for a device are supported with the help of
23554f018bc0SViresh Kumar  * virtual genpd devices, which are created for each consumer device - genpd
23564f018bc0SViresh Kumar  * pair. These are the device structures which are attached to the power domain
23574f018bc0SViresh Kumar  * and are required by the OPP core to set the performance state of the genpd.
23586319aee1SViresh Kumar  * The same API also works for the case where single genpd is available and so
23596319aee1SViresh Kumar  * we don't need to support that separately.
23604f018bc0SViresh Kumar  *
23614f018bc0SViresh Kumar  * This helper will normally be called by the consumer driver of the device
23626319aee1SViresh Kumar  * "dev", as only that has details of the genpd names.
23634f018bc0SViresh Kumar  *
23646319aee1SViresh Kumar  * This helper needs to be called once with a list of all genpd to attach.
23656319aee1SViresh Kumar  * Otherwise the original device structure will be used instead by the OPP core.
2366baea35e4SViresh Kumar  *
2367baea35e4SViresh Kumar  * The order of entries in the names array must match the order in which
2368baea35e4SViresh Kumar  * "required-opps" are added in DT.
23694f018bc0SViresh Kumar  */
237017a8f868SViresh Kumar struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
23713734b9f2SDmitry Osipenko 		const char * const *names, struct device ***virt_devs)
23724f018bc0SViresh Kumar {
23734f018bc0SViresh Kumar 	struct opp_table *opp_table;
23746319aee1SViresh Kumar 	struct device *virt_dev;
2375baea35e4SViresh Kumar 	int index = 0, ret = -EINVAL;
23763734b9f2SDmitry Osipenko 	const char * const *name = names;
23774f018bc0SViresh Kumar 
237832439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, false);
2379dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2380dd461cd9SStephan Gerhold 		return opp_table;
23814f018bc0SViresh Kumar 
2382cb60e960SViresh Kumar 	if (opp_table->genpd_virt_devs)
2383cb60e960SViresh Kumar 		return opp_table;
23844f018bc0SViresh Kumar 
23856319aee1SViresh Kumar 	/*
23866319aee1SViresh Kumar 	 * If the genpd's OPP table isn't already initialized, parsing of the
23876319aee1SViresh Kumar 	 * required-opps fail for dev. We should retry this after genpd's OPP
23886319aee1SViresh Kumar 	 * table is added.
23896319aee1SViresh Kumar 	 */
23906319aee1SViresh Kumar 	if (!opp_table->required_opp_count) {
23916319aee1SViresh Kumar 		ret = -EPROBE_DEFER;
23926319aee1SViresh Kumar 		goto put_table;
23936319aee1SViresh Kumar 	}
23946319aee1SViresh Kumar 
23954f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
23964f018bc0SViresh Kumar 
2397c0ab9e08SViresh Kumar 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2398c0ab9e08SViresh Kumar 					     sizeof(*opp_table->genpd_virt_devs),
2399c0ab9e08SViresh Kumar 					     GFP_KERNEL);
2400c0ab9e08SViresh Kumar 	if (!opp_table->genpd_virt_devs)
2401c0ab9e08SViresh Kumar 		goto unlock;
24024f018bc0SViresh Kumar 
24036319aee1SViresh Kumar 	while (*name) {
24046319aee1SViresh Kumar 		if (index >= opp_table->required_opp_count) {
24056319aee1SViresh Kumar 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
24066319aee1SViresh Kumar 				*name, opp_table->required_opp_count, index);
24076319aee1SViresh Kumar 			goto err;
24086319aee1SViresh Kumar 		}
24094f018bc0SViresh Kumar 
24106319aee1SViresh Kumar 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
24116319aee1SViresh Kumar 		if (IS_ERR(virt_dev)) {
24126319aee1SViresh Kumar 			ret = PTR_ERR(virt_dev);
24136319aee1SViresh Kumar 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
24146319aee1SViresh Kumar 			goto err;
24154f018bc0SViresh Kumar 		}
24164f018bc0SViresh Kumar 
24174f018bc0SViresh Kumar 		opp_table->genpd_virt_devs[index] = virt_dev;
2418baea35e4SViresh Kumar 		index++;
24196319aee1SViresh Kumar 		name++;
24206319aee1SViresh Kumar 	}
24216319aee1SViresh Kumar 
242217a8f868SViresh Kumar 	if (virt_devs)
242317a8f868SViresh Kumar 		*virt_devs = opp_table->genpd_virt_devs;
24244f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
24254f018bc0SViresh Kumar 
24264f018bc0SViresh Kumar 	return opp_table;
24276319aee1SViresh Kumar 
24286319aee1SViresh Kumar err:
24296319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
2430c0ab9e08SViresh Kumar unlock:
24316319aee1SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
24326319aee1SViresh Kumar 
24336319aee1SViresh Kumar put_table:
24346319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
24356319aee1SViresh Kumar 
24366319aee1SViresh Kumar 	return ERR_PTR(ret);
24374f018bc0SViresh Kumar }
24386319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
24394f018bc0SViresh Kumar 
24404f018bc0SViresh Kumar /**
24416319aee1SViresh Kumar  * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
24426319aee1SViresh Kumar  * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
24434f018bc0SViresh Kumar  *
24446319aee1SViresh Kumar  * This detaches the genpd(s), resets the virtual device pointers, and puts the
24456319aee1SViresh Kumar  * OPP table.
24464f018bc0SViresh Kumar  */
24476319aee1SViresh Kumar void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
24484f018bc0SViresh Kumar {
2449c7bf8758SViresh Kumar 	if (unlikely(!opp_table))
2450c7bf8758SViresh Kumar 		return;
2451c7bf8758SViresh Kumar 
24524f018bc0SViresh Kumar 	/*
24534f018bc0SViresh Kumar 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
24544f018bc0SViresh Kumar 	 * used in parallel.
24554f018bc0SViresh Kumar 	 */
24564f018bc0SViresh Kumar 	mutex_lock(&opp_table->genpd_virt_dev_lock);
24576319aee1SViresh Kumar 	_opp_detach_genpd(opp_table);
24584f018bc0SViresh Kumar 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
24594f018bc0SViresh Kumar 
24606319aee1SViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
24614f018bc0SViresh Kumar }
24626319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
24634f018bc0SViresh Kumar 
2464b4b9e223SDmitry Osipenko static void devm_pm_opp_detach_genpd(void *data)
2465b4b9e223SDmitry Osipenko {
2466b4b9e223SDmitry Osipenko 	dev_pm_opp_detach_genpd(data);
2467b4b9e223SDmitry Osipenko }
2468b4b9e223SDmitry Osipenko 
2469b4b9e223SDmitry Osipenko /**
2470b4b9e223SDmitry Osipenko  * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual
2471b4b9e223SDmitry Osipenko  *			      device pointer
2472b4b9e223SDmitry Osipenko  * @dev: Consumer device for which the genpd is getting attached.
2473b4b9e223SDmitry Osipenko  * @names: Null terminated array of pointers containing names of genpd to attach.
2474b4b9e223SDmitry Osipenko  * @virt_devs: Pointer to return the array of virtual devices.
2475b4b9e223SDmitry Osipenko  *
2476b4b9e223SDmitry Osipenko  * This is a resource-managed version of dev_pm_opp_attach_genpd().
2477b4b9e223SDmitry Osipenko  *
24789edf48a4SDmitry Osipenko  * Return: 0 on success and errorno otherwise.
2479b4b9e223SDmitry Osipenko  */
24803734b9f2SDmitry Osipenko int devm_pm_opp_attach_genpd(struct device *dev, const char * const *names,
2481b4b9e223SDmitry Osipenko 			     struct device ***virt_devs)
2482b4b9e223SDmitry Osipenko {
2483b4b9e223SDmitry Osipenko 	struct opp_table *opp_table;
2484b4b9e223SDmitry Osipenko 
2485b4b9e223SDmitry Osipenko 	opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs);
2486b4b9e223SDmitry Osipenko 	if (IS_ERR(opp_table))
24879edf48a4SDmitry Osipenko 		return PTR_ERR(opp_table);
2488b4b9e223SDmitry Osipenko 
24899edf48a4SDmitry Osipenko 	return devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd,
2490b4b9e223SDmitry Osipenko 					opp_table);
2491b4b9e223SDmitry Osipenko }
2492b4b9e223SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd);
2493b4b9e223SDmitry Osipenko 
24944f018bc0SViresh Kumar /**
24957d8658efSSaravana Kannan  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
24967d8658efSSaravana Kannan  * @src_table: OPP table which has @dst_table as one of its required OPP table.
24977d8658efSSaravana Kannan  * @dst_table: Required OPP table of the @src_table.
24987d8658efSSaravana Kannan  * @src_opp: OPP from the @src_table.
24997d8658efSSaravana Kannan  *
25007d8658efSSaravana Kannan  * This function returns the OPP (present in @dst_table) pointed out by the
25017d8658efSSaravana Kannan  * "required-opps" property of the @src_opp (present in @src_table).
25027d8658efSSaravana Kannan  *
25037d8658efSSaravana Kannan  * The callers are required to call dev_pm_opp_put() for the returned OPP after
25047d8658efSSaravana Kannan  * use.
25057d8658efSSaravana Kannan  *
25067d8658efSSaravana Kannan  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
25077d8658efSSaravana Kannan  */
25087d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
25097d8658efSSaravana Kannan 						 struct opp_table *dst_table,
25107d8658efSSaravana Kannan 						 struct dev_pm_opp *src_opp)
25117d8658efSSaravana Kannan {
25127d8658efSSaravana Kannan 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
25137d8658efSSaravana Kannan 	int i;
25147d8658efSSaravana Kannan 
25157d8658efSSaravana Kannan 	if (!src_table || !dst_table || !src_opp ||
25167d8658efSSaravana Kannan 	    !src_table->required_opp_tables)
25177d8658efSSaravana Kannan 		return ERR_PTR(-EINVAL);
25187d8658efSSaravana Kannan 
25197d8658efSSaravana Kannan 	/* required-opps not fully initialized yet */
25207d8658efSSaravana Kannan 	if (lazy_linking_pending(src_table))
25217d8658efSSaravana Kannan 		return ERR_PTR(-EBUSY);
25227d8658efSSaravana Kannan 
25237d8658efSSaravana Kannan 	for (i = 0; i < src_table->required_opp_count; i++) {
25247d8658efSSaravana Kannan 		if (src_table->required_opp_tables[i] == dst_table) {
25257d8658efSSaravana Kannan 			mutex_lock(&src_table->lock);
25267d8658efSSaravana Kannan 
25277d8658efSSaravana Kannan 			list_for_each_entry(opp, &src_table->opp_list, node) {
25287d8658efSSaravana Kannan 				if (opp == src_opp) {
25297d8658efSSaravana Kannan 					dest_opp = opp->required_opps[i];
25307d8658efSSaravana Kannan 					dev_pm_opp_get(dest_opp);
25317d8658efSSaravana Kannan 					break;
25327d8658efSSaravana Kannan 				}
25337d8658efSSaravana Kannan 			}
25347d8658efSSaravana Kannan 
25357d8658efSSaravana Kannan 			mutex_unlock(&src_table->lock);
25367d8658efSSaravana Kannan 			break;
25377d8658efSSaravana Kannan 		}
25387d8658efSSaravana Kannan 	}
25397d8658efSSaravana Kannan 
25407d8658efSSaravana Kannan 	if (IS_ERR(dest_opp)) {
25417d8658efSSaravana Kannan 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
25427d8658efSSaravana Kannan 		       src_table, dst_table);
25437d8658efSSaravana Kannan 	}
25447d8658efSSaravana Kannan 
25457d8658efSSaravana Kannan 	return dest_opp;
25467d8658efSSaravana Kannan }
25477d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
25487d8658efSSaravana Kannan 
25497d8658efSSaravana Kannan /**
2550c8a59103SViresh Kumar  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2551c8a59103SViresh Kumar  * @src_table: OPP table which has dst_table as one of its required OPP table.
2552c8a59103SViresh Kumar  * @dst_table: Required OPP table of the src_table.
2553c8a59103SViresh Kumar  * @pstate: Current performance state of the src_table.
2554c8a59103SViresh Kumar  *
2555c8a59103SViresh Kumar  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2556c8a59103SViresh Kumar  * "required-opps" property of the OPP (present in @src_table) which has
2557c8a59103SViresh Kumar  * performance state set to @pstate.
2558c8a59103SViresh Kumar  *
2559c8a59103SViresh Kumar  * Return: Zero or positive performance state on success, otherwise negative
2560c8a59103SViresh Kumar  * value on errors.
2561c8a59103SViresh Kumar  */
2562c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2563c8a59103SViresh Kumar 				       struct opp_table *dst_table,
2564c8a59103SViresh Kumar 				       unsigned int pstate)
2565c8a59103SViresh Kumar {
2566c8a59103SViresh Kumar 	struct dev_pm_opp *opp;
2567c8a59103SViresh Kumar 	int dest_pstate = -EINVAL;
2568c8a59103SViresh Kumar 	int i;
2569c8a59103SViresh Kumar 
2570c8a59103SViresh Kumar 	/*
2571c8a59103SViresh Kumar 	 * Normally the src_table will have the "required_opps" property set to
2572c8a59103SViresh Kumar 	 * point to one of the OPPs in the dst_table, but in some cases the
2573c8a59103SViresh Kumar 	 * genpd and its master have one to one mapping of performance states
2574c8a59103SViresh Kumar 	 * and so none of them have the "required-opps" property set. Return the
2575c8a59103SViresh Kumar 	 * pstate of the src_table as it is in such cases.
2576c8a59103SViresh Kumar 	 */
2577f2f4d2b8SDmitry Osipenko 	if (!src_table || !src_table->required_opp_count)
2578c8a59103SViresh Kumar 		return pstate;
2579c8a59103SViresh Kumar 
25807eba0c76SViresh Kumar 	/* required-opps not fully initialized yet */
25817eba0c76SViresh Kumar 	if (lazy_linking_pending(src_table))
25827eba0c76SViresh Kumar 		return -EBUSY;
25837eba0c76SViresh Kumar 
2584c8a59103SViresh Kumar 	for (i = 0; i < src_table->required_opp_count; i++) {
2585c8a59103SViresh Kumar 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2586c8a59103SViresh Kumar 			break;
2587c8a59103SViresh Kumar 	}
2588c8a59103SViresh Kumar 
2589c8a59103SViresh Kumar 	if (unlikely(i == src_table->required_opp_count)) {
2590c8a59103SViresh Kumar 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2591c8a59103SViresh Kumar 		       __func__, src_table, dst_table);
2592c8a59103SViresh Kumar 		return -EINVAL;
2593c8a59103SViresh Kumar 	}
2594c8a59103SViresh Kumar 
2595c8a59103SViresh Kumar 	mutex_lock(&src_table->lock);
2596c8a59103SViresh Kumar 
2597c8a59103SViresh Kumar 	list_for_each_entry(opp, &src_table->opp_list, node) {
2598c8a59103SViresh Kumar 		if (opp->pstate == pstate) {
2599c8a59103SViresh Kumar 			dest_pstate = opp->required_opps[i]->pstate;
2600c8a59103SViresh Kumar 			goto unlock;
2601c8a59103SViresh Kumar 		}
2602c8a59103SViresh Kumar 	}
2603c8a59103SViresh Kumar 
2604c8a59103SViresh Kumar 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2605c8a59103SViresh Kumar 	       dst_table);
2606c8a59103SViresh Kumar 
2607c8a59103SViresh Kumar unlock:
2608c8a59103SViresh Kumar 	mutex_unlock(&src_table->lock);
2609c8a59103SViresh Kumar 
2610c8a59103SViresh Kumar 	return dest_pstate;
2611c8a59103SViresh Kumar }
2612c8a59103SViresh Kumar 
2613c8a59103SViresh Kumar /**
26147813dd6fSViresh Kumar  * dev_pm_opp_add()  - Add an OPP table from a table definitions
26157813dd6fSViresh Kumar  * @dev:	device for which we do this operation
26167813dd6fSViresh Kumar  * @freq:	Frequency in Hz for this OPP
26177813dd6fSViresh Kumar  * @u_volt:	Voltage in uVolts for this OPP
26187813dd6fSViresh Kumar  *
26197813dd6fSViresh Kumar  * This function adds an opp definition to the opp table and returns status.
26207813dd6fSViresh Kumar  * The opp is made available by default and it can be controlled using
26217813dd6fSViresh Kumar  * dev_pm_opp_enable/disable functions.
26227813dd6fSViresh Kumar  *
26237813dd6fSViresh Kumar  * Return:
26247813dd6fSViresh Kumar  * 0		On success OR
26257813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and opp->available
26267813dd6fSViresh Kumar  * -EEXIST	Freq are same and volt are different OR
26277813dd6fSViresh Kumar  *		Duplicate OPPs (both freq and volt are same) and !opp->available
26287813dd6fSViresh Kumar  * -ENOMEM	Memory allocation failure
26297813dd6fSViresh Kumar  */
26307813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
26317813dd6fSViresh Kumar {
26327813dd6fSViresh Kumar 	struct opp_table *opp_table;
26337813dd6fSViresh Kumar 	int ret;
26347813dd6fSViresh Kumar 
263532439ac7SViresh Kumar 	opp_table = _add_opp_table(dev, true);
2636dd461cd9SStephan Gerhold 	if (IS_ERR(opp_table))
2637dd461cd9SStephan Gerhold 		return PTR_ERR(opp_table);
26387813dd6fSViresh Kumar 
263946f48acaSViresh Kumar 	/* Fix regulator count for dynamic OPPs */
264046f48acaSViresh Kumar 	opp_table->regulator_count = 1;
264146f48acaSViresh Kumar 
26427813dd6fSViresh Kumar 	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
26430ad8c623SViresh Kumar 	if (ret)
26447813dd6fSViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
26450ad8c623SViresh Kumar 
26467813dd6fSViresh Kumar 	return ret;
26477813dd6fSViresh Kumar }
26487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add);
26497813dd6fSViresh Kumar 
26507813dd6fSViresh Kumar /**
26517813dd6fSViresh Kumar  * _opp_set_availability() - helper to set the availability of an opp
26527813dd6fSViresh Kumar  * @dev:		device for which we do this operation
26537813dd6fSViresh Kumar  * @freq:		OPP frequency to modify availability
26547813dd6fSViresh Kumar  * @availability_req:	availability status requested for this opp
26557813dd6fSViresh Kumar  *
26567813dd6fSViresh Kumar  * Set the availability of an OPP, opp_{enable,disable} share a common logic
26577813dd6fSViresh Kumar  * which is isolated here.
26587813dd6fSViresh Kumar  *
26597813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
26607813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
26617813dd6fSViresh Kumar  * successful.
26627813dd6fSViresh Kumar  */
26637813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq,
26647813dd6fSViresh Kumar 				 bool availability_req)
26657813dd6fSViresh Kumar {
26667813dd6fSViresh Kumar 	struct opp_table *opp_table;
26677813dd6fSViresh Kumar 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
26687813dd6fSViresh Kumar 	int r = 0;
26697813dd6fSViresh Kumar 
26707813dd6fSViresh Kumar 	/* Find the opp_table */
26717813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
26727813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
26737813dd6fSViresh Kumar 		r = PTR_ERR(opp_table);
26747813dd6fSViresh Kumar 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
26757813dd6fSViresh Kumar 		return r;
26767813dd6fSViresh Kumar 	}
26777813dd6fSViresh Kumar 
26787813dd6fSViresh Kumar 	mutex_lock(&opp_table->lock);
26797813dd6fSViresh Kumar 
26807813dd6fSViresh Kumar 	/* Do we have the frequency? */
26817813dd6fSViresh Kumar 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
26827813dd6fSViresh Kumar 		if (tmp_opp->rate == freq) {
26837813dd6fSViresh Kumar 			opp = tmp_opp;
26847813dd6fSViresh Kumar 			break;
26857813dd6fSViresh Kumar 		}
26867813dd6fSViresh Kumar 	}
26877813dd6fSViresh Kumar 
26887813dd6fSViresh Kumar 	if (IS_ERR(opp)) {
26897813dd6fSViresh Kumar 		r = PTR_ERR(opp);
26907813dd6fSViresh Kumar 		goto unlock;
26917813dd6fSViresh Kumar 	}
26927813dd6fSViresh Kumar 
26937813dd6fSViresh Kumar 	/* Is update really needed? */
26947813dd6fSViresh Kumar 	if (opp->available == availability_req)
26957813dd6fSViresh Kumar 		goto unlock;
26967813dd6fSViresh Kumar 
26977813dd6fSViresh Kumar 	opp->available = availability_req;
26987813dd6fSViresh Kumar 
26997813dd6fSViresh Kumar 	dev_pm_opp_get(opp);
27007813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
27017813dd6fSViresh Kumar 
27027813dd6fSViresh Kumar 	/* Notify the change of the OPP availability */
27037813dd6fSViresh Kumar 	if (availability_req)
27047813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
27057813dd6fSViresh Kumar 					     opp);
27067813dd6fSViresh Kumar 	else
27077813dd6fSViresh Kumar 		blocking_notifier_call_chain(&opp_table->head,
27087813dd6fSViresh Kumar 					     OPP_EVENT_DISABLE, opp);
27097813dd6fSViresh Kumar 
27107813dd6fSViresh Kumar 	dev_pm_opp_put(opp);
27117813dd6fSViresh Kumar 	goto put_table;
27127813dd6fSViresh Kumar 
27137813dd6fSViresh Kumar unlock:
27147813dd6fSViresh Kumar 	mutex_unlock(&opp_table->lock);
27157813dd6fSViresh Kumar put_table:
27167813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
27177813dd6fSViresh Kumar 	return r;
27187813dd6fSViresh Kumar }
27197813dd6fSViresh Kumar 
27207813dd6fSViresh Kumar /**
272125cb20a2SStephen Boyd  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
272225cb20a2SStephen Boyd  * @dev:		device for which we do this operation
272325cb20a2SStephen Boyd  * @freq:		OPP frequency to adjust voltage of
272425cb20a2SStephen Boyd  * @u_volt:		new OPP target voltage
272525cb20a2SStephen Boyd  * @u_volt_min:		new OPP min voltage
272625cb20a2SStephen Boyd  * @u_volt_max:		new OPP max voltage
272725cb20a2SStephen Boyd  *
272825cb20a2SStephen Boyd  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
272925cb20a2SStephen Boyd  * copy operation, returns 0 if no modifcation was done OR modification was
273025cb20a2SStephen Boyd  * successful.
273125cb20a2SStephen Boyd  */
273225cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
273325cb20a2SStephen Boyd 			      unsigned long u_volt, unsigned long u_volt_min,
273425cb20a2SStephen Boyd 			      unsigned long u_volt_max)
273525cb20a2SStephen Boyd 
273625cb20a2SStephen Boyd {
273725cb20a2SStephen Boyd 	struct opp_table *opp_table;
273825cb20a2SStephen Boyd 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
273925cb20a2SStephen Boyd 	int r = 0;
274025cb20a2SStephen Boyd 
274125cb20a2SStephen Boyd 	/* Find the opp_table */
274225cb20a2SStephen Boyd 	opp_table = _find_opp_table(dev);
274325cb20a2SStephen Boyd 	if (IS_ERR(opp_table)) {
274425cb20a2SStephen Boyd 		r = PTR_ERR(opp_table);
274525cb20a2SStephen Boyd 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
274625cb20a2SStephen Boyd 		return r;
274725cb20a2SStephen Boyd 	}
274825cb20a2SStephen Boyd 
274925cb20a2SStephen Boyd 	mutex_lock(&opp_table->lock);
275025cb20a2SStephen Boyd 
275125cb20a2SStephen Boyd 	/* Do we have the frequency? */
275225cb20a2SStephen Boyd 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
275325cb20a2SStephen Boyd 		if (tmp_opp->rate == freq) {
275425cb20a2SStephen Boyd 			opp = tmp_opp;
275525cb20a2SStephen Boyd 			break;
275625cb20a2SStephen Boyd 		}
275725cb20a2SStephen Boyd 	}
275825cb20a2SStephen Boyd 
275925cb20a2SStephen Boyd 	if (IS_ERR(opp)) {
276025cb20a2SStephen Boyd 		r = PTR_ERR(opp);
276125cb20a2SStephen Boyd 		goto adjust_unlock;
276225cb20a2SStephen Boyd 	}
276325cb20a2SStephen Boyd 
276425cb20a2SStephen Boyd 	/* Is update really needed? */
276525cb20a2SStephen Boyd 	if (opp->supplies->u_volt == u_volt)
276625cb20a2SStephen Boyd 		goto adjust_unlock;
276725cb20a2SStephen Boyd 
276825cb20a2SStephen Boyd 	opp->supplies->u_volt = u_volt;
276925cb20a2SStephen Boyd 	opp->supplies->u_volt_min = u_volt_min;
277025cb20a2SStephen Boyd 	opp->supplies->u_volt_max = u_volt_max;
277125cb20a2SStephen Boyd 
277225cb20a2SStephen Boyd 	dev_pm_opp_get(opp);
277325cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
277425cb20a2SStephen Boyd 
277525cb20a2SStephen Boyd 	/* Notify the voltage change of the OPP */
277625cb20a2SStephen Boyd 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
277725cb20a2SStephen Boyd 				     opp);
277825cb20a2SStephen Boyd 
277925cb20a2SStephen Boyd 	dev_pm_opp_put(opp);
278025cb20a2SStephen Boyd 	goto adjust_put_table;
278125cb20a2SStephen Boyd 
278225cb20a2SStephen Boyd adjust_unlock:
278325cb20a2SStephen Boyd 	mutex_unlock(&opp_table->lock);
278425cb20a2SStephen Boyd adjust_put_table:
278525cb20a2SStephen Boyd 	dev_pm_opp_put_opp_table(opp_table);
278625cb20a2SStephen Boyd 	return r;
278725cb20a2SStephen Boyd }
278803649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
278925cb20a2SStephen Boyd 
279025cb20a2SStephen Boyd /**
27917813dd6fSViresh Kumar  * dev_pm_opp_enable() - Enable a specific OPP
27927813dd6fSViresh Kumar  * @dev:	device for which we do this operation
27937813dd6fSViresh Kumar  * @freq:	OPP frequency to enable
27947813dd6fSViresh Kumar  *
27957813dd6fSViresh Kumar  * Enables a provided opp. If the operation is valid, this returns 0, else the
27967813dd6fSViresh Kumar  * corresponding error value. It is meant to be used for users an OPP available
27977813dd6fSViresh Kumar  * after being temporarily made unavailable with dev_pm_opp_disable.
27987813dd6fSViresh Kumar  *
27997813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
28007813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
28017813dd6fSViresh Kumar  * successful.
28027813dd6fSViresh Kumar  */
28037813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq)
28047813dd6fSViresh Kumar {
28057813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, true);
28067813dd6fSViresh Kumar }
28077813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
28087813dd6fSViresh Kumar 
28097813dd6fSViresh Kumar /**
28107813dd6fSViresh Kumar  * dev_pm_opp_disable() - Disable a specific OPP
28117813dd6fSViresh Kumar  * @dev:	device for which we do this operation
28127813dd6fSViresh Kumar  * @freq:	OPP frequency to disable
28137813dd6fSViresh Kumar  *
28147813dd6fSViresh Kumar  * Disables a provided opp. If the operation is valid, this returns
28157813dd6fSViresh Kumar  * 0, else the corresponding error value. It is meant to be a temporary
28167813dd6fSViresh Kumar  * control by users to make this OPP not available until the circumstances are
28177813dd6fSViresh Kumar  * right to make it available again (with a call to dev_pm_opp_enable).
28187813dd6fSViresh Kumar  *
28197813dd6fSViresh Kumar  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
28207813dd6fSViresh Kumar  * copy operation, returns 0 if no modification was done OR modification was
28217813dd6fSViresh Kumar  * successful.
28227813dd6fSViresh Kumar  */
28237813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq)
28247813dd6fSViresh Kumar {
28257813dd6fSViresh Kumar 	return _opp_set_availability(dev, freq, false);
28267813dd6fSViresh Kumar }
28277813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
28287813dd6fSViresh Kumar 
28297813dd6fSViresh Kumar /**
28307813dd6fSViresh Kumar  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
28317813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be registered
28327813dd6fSViresh Kumar  * @nb:		Notifier block to be registered
28337813dd6fSViresh Kumar  *
28347813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
28357813dd6fSViresh Kumar  */
28367813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
28377813dd6fSViresh Kumar {
28387813dd6fSViresh Kumar 	struct opp_table *opp_table;
28397813dd6fSViresh Kumar 	int ret;
28407813dd6fSViresh Kumar 
28417813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
28427813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
28437813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
28447813dd6fSViresh Kumar 
28457813dd6fSViresh Kumar 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
28467813dd6fSViresh Kumar 
28477813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
28487813dd6fSViresh Kumar 
28497813dd6fSViresh Kumar 	return ret;
28507813dd6fSViresh Kumar }
28517813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier);
28527813dd6fSViresh Kumar 
28537813dd6fSViresh Kumar /**
28547813dd6fSViresh Kumar  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
28557813dd6fSViresh Kumar  * @dev:	Device for which notifier needs to be unregistered
28567813dd6fSViresh Kumar  * @nb:		Notifier block to be unregistered
28577813dd6fSViresh Kumar  *
28587813dd6fSViresh Kumar  * Return: 0 on success or a negative error value.
28597813dd6fSViresh Kumar  */
28607813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev,
28617813dd6fSViresh Kumar 				   struct notifier_block *nb)
28627813dd6fSViresh Kumar {
28637813dd6fSViresh Kumar 	struct opp_table *opp_table;
28647813dd6fSViresh Kumar 	int ret;
28657813dd6fSViresh Kumar 
28667813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
28677813dd6fSViresh Kumar 	if (IS_ERR(opp_table))
28687813dd6fSViresh Kumar 		return PTR_ERR(opp_table);
28697813dd6fSViresh Kumar 
28707813dd6fSViresh Kumar 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
28717813dd6fSViresh Kumar 
28727813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
28737813dd6fSViresh Kumar 
28747813dd6fSViresh Kumar 	return ret;
28757813dd6fSViresh Kumar }
28767813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
28777813dd6fSViresh Kumar 
28788aaf6264SViresh Kumar /**
28798aaf6264SViresh Kumar  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
28808aaf6264SViresh Kumar  * @dev:	device pointer used to lookup OPP table.
28818aaf6264SViresh Kumar  *
28828aaf6264SViresh Kumar  * Free both OPPs created using static entries present in DT and the
28838aaf6264SViresh Kumar  * dynamically added entries.
28848aaf6264SViresh Kumar  */
28858aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev)
28867813dd6fSViresh Kumar {
28877813dd6fSViresh Kumar 	struct opp_table *opp_table;
28887813dd6fSViresh Kumar 
28897813dd6fSViresh Kumar 	/* Check for existing table for 'dev' */
28907813dd6fSViresh Kumar 	opp_table = _find_opp_table(dev);
28917813dd6fSViresh Kumar 	if (IS_ERR(opp_table)) {
28927813dd6fSViresh Kumar 		int error = PTR_ERR(opp_table);
28937813dd6fSViresh Kumar 
28947813dd6fSViresh Kumar 		if (error != -ENODEV)
28957813dd6fSViresh Kumar 			WARN(1, "%s: opp_table: %d\n",
28967813dd6fSViresh Kumar 			     IS_ERR_OR_NULL(dev) ?
28977813dd6fSViresh Kumar 					"Invalid device" : dev_name(dev),
28987813dd6fSViresh Kumar 			     error);
28997813dd6fSViresh Kumar 		return;
29007813dd6fSViresh Kumar 	}
29017813dd6fSViresh Kumar 
2902922ff075SViresh Kumar 	/*
2903922ff075SViresh Kumar 	 * Drop the extra reference only if the OPP table was successfully added
2904922ff075SViresh Kumar 	 * with dev_pm_opp_of_add_table() earlier.
2905922ff075SViresh Kumar 	 **/
2906922ff075SViresh Kumar 	if (_opp_remove_all_static(opp_table))
2907cdd6ed90SViresh Kumar 		dev_pm_opp_put_opp_table(opp_table);
2908cdd6ed90SViresh Kumar 
2909922ff075SViresh Kumar 	/* Drop reference taken by _find_opp_table() */
29107813dd6fSViresh Kumar 	dev_pm_opp_put_opp_table(opp_table);
29117813dd6fSViresh Kumar }
29127813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
2913ce8073d8SDmitry Osipenko 
2914ce8073d8SDmitry Osipenko /**
2915ce8073d8SDmitry Osipenko  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2916ce8073d8SDmitry Osipenko  * @dev:	device for which we do this operation
2917ce8073d8SDmitry Osipenko  *
2918ce8073d8SDmitry Osipenko  * Sync voltage state of the OPP table regulators.
2919ce8073d8SDmitry Osipenko  *
2920ce8073d8SDmitry Osipenko  * Return: 0 on success or a negative error value.
2921ce8073d8SDmitry Osipenko  */
2922ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev)
2923ce8073d8SDmitry Osipenko {
2924ce8073d8SDmitry Osipenko 	struct opp_table *opp_table;
2925ce8073d8SDmitry Osipenko 	struct regulator *reg;
2926ce8073d8SDmitry Osipenko 	int i, ret = 0;
2927ce8073d8SDmitry Osipenko 
2928ce8073d8SDmitry Osipenko 	/* Device may not have OPP table */
2929ce8073d8SDmitry Osipenko 	opp_table = _find_opp_table(dev);
2930ce8073d8SDmitry Osipenko 	if (IS_ERR(opp_table))
2931ce8073d8SDmitry Osipenko 		return 0;
2932ce8073d8SDmitry Osipenko 
2933ce8073d8SDmitry Osipenko 	/* Regulator may not be required for the device */
2934ce8073d8SDmitry Osipenko 	if (unlikely(!opp_table->regulators))
2935ce8073d8SDmitry Osipenko 		goto put_table;
2936ce8073d8SDmitry Osipenko 
2937ce8073d8SDmitry Osipenko 	/* Nothing to sync if voltage wasn't changed */
2938ce8073d8SDmitry Osipenko 	if (!opp_table->enabled)
2939ce8073d8SDmitry Osipenko 		goto put_table;
2940ce8073d8SDmitry Osipenko 
2941ce8073d8SDmitry Osipenko 	for (i = 0; i < opp_table->regulator_count; i++) {
2942ce8073d8SDmitry Osipenko 		reg = opp_table->regulators[i];
2943ce8073d8SDmitry Osipenko 		ret = regulator_sync_voltage(reg);
2944ce8073d8SDmitry Osipenko 		if (ret)
2945ce8073d8SDmitry Osipenko 			break;
2946ce8073d8SDmitry Osipenko 	}
2947ce8073d8SDmitry Osipenko put_table:
2948ce8073d8SDmitry Osipenko 	/* Drop reference taken by _find_opp_table() */
2949ce8073d8SDmitry Osipenko 	dev_pm_opp_put_opp_table(opp_table);
2950ce8073d8SDmitry Osipenko 
2951ce8073d8SDmitry Osipenko 	return ret;
2952ce8073d8SDmitry Osipenko }
2953ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
2954