1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27813dd6fSViresh Kumar /* 37813dd6fSViresh Kumar * Generic OPP Interface 47813dd6fSViresh Kumar * 57813dd6fSViresh Kumar * Copyright (C) 2009-2010 Texas Instruments Incorporated. 67813dd6fSViresh Kumar * Nishanth Menon 77813dd6fSViresh Kumar * Romit Dasgupta 87813dd6fSViresh Kumar * Kevin Hilman 97813dd6fSViresh Kumar */ 107813dd6fSViresh Kumar 117813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 127813dd6fSViresh Kumar 137813dd6fSViresh Kumar #include <linux/clk.h> 147813dd6fSViresh Kumar #include <linux/errno.h> 157813dd6fSViresh Kumar #include <linux/err.h> 167813dd6fSViresh Kumar #include <linux/device.h> 177813dd6fSViresh Kumar #include <linux/export.h> 18009acd19SViresh Kumar #include <linux/pm_domain.h> 197813dd6fSViresh Kumar #include <linux/regulator/consumer.h> 2011b9b663SViresh Kumar #include <linux/slab.h> 2111b9b663SViresh Kumar #include <linux/xarray.h> 227813dd6fSViresh Kumar 237813dd6fSViresh Kumar #include "opp.h" 247813dd6fSViresh Kumar 257813dd6fSViresh Kumar /* 267813dd6fSViresh Kumar * The root of the list of all opp-tables. All opp_table structures branch off 277813dd6fSViresh Kumar * from here, with each opp_table containing the list of opps it supports in 287813dd6fSViresh Kumar * various states of availability. 297813dd6fSViresh Kumar */ 307813dd6fSViresh Kumar LIST_HEAD(opp_tables); 317eba0c76SViresh Kumar 327eba0c76SViresh Kumar /* OPP tables with uninitialized required OPPs */ 337eba0c76SViresh Kumar LIST_HEAD(lazy_opp_tables); 347eba0c76SViresh Kumar 357813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */ 367813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock); 3727c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */ 3827c09484SViresh Kumar static bool opp_tables_busy; 397813dd6fSViresh Kumar 4011b9b663SViresh Kumar /* OPP ID allocator */ 4111b9b663SViresh Kumar static DEFINE_XARRAY_ALLOC1(opp_configs); 4211b9b663SViresh Kumar 439e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table) 447813dd6fSViresh Kumar { 457813dd6fSViresh Kumar struct opp_device *opp_dev; 469e62edacSViresh Kumar bool found = false; 477813dd6fSViresh Kumar 489e62edacSViresh Kumar mutex_lock(&opp_table->lock); 497813dd6fSViresh Kumar list_for_each_entry(opp_dev, &opp_table->dev_list, node) 509e62edacSViresh Kumar if (opp_dev->dev == dev) { 519e62edacSViresh Kumar found = true; 529e62edacSViresh Kumar break; 539e62edacSViresh Kumar } 547813dd6fSViresh Kumar 559e62edacSViresh Kumar mutex_unlock(&opp_table->lock); 569e62edacSViresh Kumar return found; 577813dd6fSViresh Kumar } 587813dd6fSViresh Kumar 597813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev) 607813dd6fSViresh Kumar { 617813dd6fSViresh Kumar struct opp_table *opp_table; 627813dd6fSViresh Kumar 637813dd6fSViresh Kumar list_for_each_entry(opp_table, &opp_tables, node) { 649e62edacSViresh Kumar if (_find_opp_dev(dev, opp_table)) { 657813dd6fSViresh Kumar _get_opp_table_kref(opp_table); 667813dd6fSViresh Kumar return opp_table; 677813dd6fSViresh Kumar } 687813dd6fSViresh Kumar } 697813dd6fSViresh Kumar 707813dd6fSViresh Kumar return ERR_PTR(-ENODEV); 717813dd6fSViresh Kumar } 727813dd6fSViresh Kumar 737813dd6fSViresh Kumar /** 747813dd6fSViresh Kumar * _find_opp_table() - find opp_table struct using device pointer 757813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table 767813dd6fSViresh Kumar * 777813dd6fSViresh Kumar * Search OPP table for one containing matching device. 787813dd6fSViresh Kumar * 797813dd6fSViresh Kumar * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or 807813dd6fSViresh Kumar * -EINVAL based on type of error. 817813dd6fSViresh Kumar * 827813dd6fSViresh Kumar * The callers must call dev_pm_opp_put_opp_table() after the table is used. 837813dd6fSViresh Kumar */ 847813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev) 857813dd6fSViresh Kumar { 867813dd6fSViresh Kumar struct opp_table *opp_table; 877813dd6fSViresh Kumar 887813dd6fSViresh Kumar if (IS_ERR_OR_NULL(dev)) { 897813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 907813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 917813dd6fSViresh Kumar } 927813dd6fSViresh Kumar 937813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 947813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 957813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 967813dd6fSViresh Kumar 977813dd6fSViresh Kumar return opp_table; 987813dd6fSViresh Kumar } 997813dd6fSViresh Kumar 1007813dd6fSViresh Kumar /** 1017813dd6fSViresh Kumar * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp 1027813dd6fSViresh Kumar * @opp: opp for which voltage has to be returned for 1037813dd6fSViresh Kumar * 1047813dd6fSViresh Kumar * Return: voltage in micro volt corresponding to the opp, else 1057813dd6fSViresh Kumar * return 0 1067813dd6fSViresh Kumar * 1077813dd6fSViresh Kumar * This is useful only for devices with single power supply. 1087813dd6fSViresh Kumar */ 1097813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 1107813dd6fSViresh Kumar { 1117813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp)) { 1127813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1137813dd6fSViresh Kumar return 0; 1147813dd6fSViresh Kumar } 1157813dd6fSViresh Kumar 1167813dd6fSViresh Kumar return opp->supplies[0].u_volt; 1177813dd6fSViresh Kumar } 1187813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); 1197813dd6fSViresh Kumar 1207813dd6fSViresh Kumar /** 1214f9a7a1dSLukasz Luba * dev_pm_opp_get_power() - Gets the power corresponding to an opp 1224f9a7a1dSLukasz Luba * @opp: opp for which power has to be returned for 1234f9a7a1dSLukasz Luba * 1244f9a7a1dSLukasz Luba * Return: power in micro watt corresponding to the opp, else 1254f9a7a1dSLukasz Luba * return 0 1264f9a7a1dSLukasz Luba * 1274f9a7a1dSLukasz Luba * This is useful only for devices with single power supply. 1284f9a7a1dSLukasz Luba */ 1294f9a7a1dSLukasz Luba unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp) 1304f9a7a1dSLukasz Luba { 1314f9a7a1dSLukasz Luba unsigned long opp_power = 0; 1324f9a7a1dSLukasz Luba int i; 1334f9a7a1dSLukasz Luba 1344f9a7a1dSLukasz Luba if (IS_ERR_OR_NULL(opp)) { 1354f9a7a1dSLukasz Luba pr_err("%s: Invalid parameters\n", __func__); 1364f9a7a1dSLukasz Luba return 0; 1374f9a7a1dSLukasz Luba } 1384f9a7a1dSLukasz Luba for (i = 0; i < opp->opp_table->regulator_count; i++) 1394f9a7a1dSLukasz Luba opp_power += opp->supplies[i].u_watt; 1404f9a7a1dSLukasz Luba 1414f9a7a1dSLukasz Luba return opp_power; 1424f9a7a1dSLukasz Luba } 1434f9a7a1dSLukasz Luba EXPORT_SYMBOL_GPL(dev_pm_opp_get_power); 1444f9a7a1dSLukasz Luba 1454f9a7a1dSLukasz Luba /** 1467813dd6fSViresh Kumar * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp 1477813dd6fSViresh Kumar * @opp: opp for which frequency has to be returned for 1487813dd6fSViresh Kumar * 1497813dd6fSViresh Kumar * Return: frequency in hertz corresponding to the opp, else 1507813dd6fSViresh Kumar * return 0 1517813dd6fSViresh Kumar */ 1527813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) 1537813dd6fSViresh Kumar { 15406a8a059SAndrew-sh.Cheng if (IS_ERR_OR_NULL(opp)) { 1557813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1567813dd6fSViresh Kumar return 0; 1577813dd6fSViresh Kumar } 1587813dd6fSViresh Kumar 1597813dd6fSViresh Kumar return opp->rate; 1607813dd6fSViresh Kumar } 1617813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); 1627813dd6fSViresh Kumar 1637813dd6fSViresh Kumar /** 1645b93ac54SRajendra Nayak * dev_pm_opp_get_level() - Gets the level corresponding to an available opp 1655b93ac54SRajendra Nayak * @opp: opp for which level value has to be returned for 1665b93ac54SRajendra Nayak * 1675b93ac54SRajendra Nayak * Return: level read from device tree corresponding to the opp, else 1685b93ac54SRajendra Nayak * return 0. 1695b93ac54SRajendra Nayak */ 1705b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp) 1715b93ac54SRajendra Nayak { 1725b93ac54SRajendra Nayak if (IS_ERR_OR_NULL(opp) || !opp->available) { 1735b93ac54SRajendra Nayak pr_err("%s: Invalid parameters\n", __func__); 1745b93ac54SRajendra Nayak return 0; 1755b93ac54SRajendra Nayak } 1765b93ac54SRajendra Nayak 1775b93ac54SRajendra Nayak return opp->level; 1785b93ac54SRajendra Nayak } 1795b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level); 1805b93ac54SRajendra Nayak 1815b93ac54SRajendra Nayak /** 182597ff543SDmitry Osipenko * dev_pm_opp_get_required_pstate() - Gets the required performance state 183597ff543SDmitry Osipenko * corresponding to an available opp 184597ff543SDmitry Osipenko * @opp: opp for which performance state has to be returned for 185597ff543SDmitry Osipenko * @index: index of the required opp 186597ff543SDmitry Osipenko * 187597ff543SDmitry Osipenko * Return: performance state read from device tree corresponding to the 188597ff543SDmitry Osipenko * required opp, else return 0. 189597ff543SDmitry Osipenko */ 190597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp, 191597ff543SDmitry Osipenko unsigned int index) 192597ff543SDmitry Osipenko { 193597ff543SDmitry Osipenko if (IS_ERR_OR_NULL(opp) || !opp->available || 194597ff543SDmitry Osipenko index >= opp->opp_table->required_opp_count) { 195597ff543SDmitry Osipenko pr_err("%s: Invalid parameters\n", __func__); 196597ff543SDmitry Osipenko return 0; 197597ff543SDmitry Osipenko } 198597ff543SDmitry Osipenko 1997eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 2007eba0c76SViresh Kumar if (lazy_linking_pending(opp->opp_table)) 2017eba0c76SViresh Kumar return 0; 2027eba0c76SViresh Kumar 203597ff543SDmitry Osipenko return opp->required_opps[index]->pstate; 204597ff543SDmitry Osipenko } 205597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate); 206597ff543SDmitry Osipenko 207597ff543SDmitry Osipenko /** 2087813dd6fSViresh Kumar * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not 2097813dd6fSViresh Kumar * @opp: opp for which turbo mode is being verified 2107813dd6fSViresh Kumar * 2117813dd6fSViresh Kumar * Turbo OPPs are not for normal use, and can be enabled (under certain 2127813dd6fSViresh Kumar * conditions) for short duration of times to finish high throughput work 2137813dd6fSViresh Kumar * quickly. Running on them for longer times may overheat the chip. 2147813dd6fSViresh Kumar * 2157813dd6fSViresh Kumar * Return: true if opp is turbo opp, else false. 2167813dd6fSViresh Kumar */ 2177813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 2187813dd6fSViresh Kumar { 2197813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 2207813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 2217813dd6fSViresh Kumar return false; 2227813dd6fSViresh Kumar } 2237813dd6fSViresh Kumar 2247813dd6fSViresh Kumar return opp->turbo; 2257813dd6fSViresh Kumar } 2267813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 2277813dd6fSViresh Kumar 2287813dd6fSViresh Kumar /** 2297813dd6fSViresh Kumar * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds 2307813dd6fSViresh Kumar * @dev: device for which we do this operation 2317813dd6fSViresh Kumar * 2327813dd6fSViresh Kumar * Return: This function returns the max clock latency in nanoseconds. 2337813dd6fSViresh Kumar */ 2347813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 2357813dd6fSViresh Kumar { 2367813dd6fSViresh Kumar struct opp_table *opp_table; 2377813dd6fSViresh Kumar unsigned long clock_latency_ns; 2387813dd6fSViresh Kumar 2397813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2407813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2417813dd6fSViresh Kumar return 0; 2427813dd6fSViresh Kumar 2437813dd6fSViresh Kumar clock_latency_ns = opp_table->clock_latency_ns_max; 2447813dd6fSViresh Kumar 2457813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2467813dd6fSViresh Kumar 2477813dd6fSViresh Kumar return clock_latency_ns; 2487813dd6fSViresh Kumar } 2497813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 2507813dd6fSViresh Kumar 2517813dd6fSViresh Kumar /** 2527813dd6fSViresh Kumar * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 2537813dd6fSViresh Kumar * @dev: device for which we do this operation 2547813dd6fSViresh Kumar * 2557813dd6fSViresh Kumar * Return: This function returns the max voltage latency in nanoseconds. 2567813dd6fSViresh Kumar */ 2577813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 2587813dd6fSViresh Kumar { 2597813dd6fSViresh Kumar struct opp_table *opp_table; 2607813dd6fSViresh Kumar struct dev_pm_opp *opp; 2617813dd6fSViresh Kumar struct regulator *reg; 2627813dd6fSViresh Kumar unsigned long latency_ns = 0; 2637813dd6fSViresh Kumar int ret, i, count; 2647813dd6fSViresh Kumar struct { 2657813dd6fSViresh Kumar unsigned long min; 2667813dd6fSViresh Kumar unsigned long max; 2677813dd6fSViresh Kumar } *uV; 2687813dd6fSViresh Kumar 2697813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2707813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2717813dd6fSViresh Kumar return 0; 2727813dd6fSViresh Kumar 2737813dd6fSViresh Kumar /* Regulator may not be required for the device */ 27490e3577bSViresh Kumar if (!opp_table->regulators) 2757813dd6fSViresh Kumar goto put_opp_table; 2767813dd6fSViresh Kumar 27790e3577bSViresh Kumar count = opp_table->regulator_count; 27890e3577bSViresh Kumar 2797813dd6fSViresh Kumar uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 2807813dd6fSViresh Kumar if (!uV) 2817813dd6fSViresh Kumar goto put_opp_table; 2827813dd6fSViresh Kumar 2837813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 2847813dd6fSViresh Kumar 2857813dd6fSViresh Kumar for (i = 0; i < count; i++) { 2867813dd6fSViresh Kumar uV[i].min = ~0; 2877813dd6fSViresh Kumar uV[i].max = 0; 2887813dd6fSViresh Kumar 2897813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 2907813dd6fSViresh Kumar if (!opp->available) 2917813dd6fSViresh Kumar continue; 2927813dd6fSViresh Kumar 2937813dd6fSViresh Kumar if (opp->supplies[i].u_volt_min < uV[i].min) 2947813dd6fSViresh Kumar uV[i].min = opp->supplies[i].u_volt_min; 2957813dd6fSViresh Kumar if (opp->supplies[i].u_volt_max > uV[i].max) 2967813dd6fSViresh Kumar uV[i].max = opp->supplies[i].u_volt_max; 2977813dd6fSViresh Kumar } 2987813dd6fSViresh Kumar } 2997813dd6fSViresh Kumar 3007813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 3017813dd6fSViresh Kumar 3027813dd6fSViresh Kumar /* 3037813dd6fSViresh Kumar * The caller needs to ensure that opp_table (and hence the regulator) 3047813dd6fSViresh Kumar * isn't freed, while we are executing this routine. 3057813dd6fSViresh Kumar */ 3067813dd6fSViresh Kumar for (i = 0; i < count; i++) { 3077813dd6fSViresh Kumar reg = opp_table->regulators[i]; 3087813dd6fSViresh Kumar ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); 3097813dd6fSViresh Kumar if (ret > 0) 3107813dd6fSViresh Kumar latency_ns += ret * 1000; 3117813dd6fSViresh Kumar } 3127813dd6fSViresh Kumar 3137813dd6fSViresh Kumar kfree(uV); 3147813dd6fSViresh Kumar put_opp_table: 3157813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3167813dd6fSViresh Kumar 3177813dd6fSViresh Kumar return latency_ns; 3187813dd6fSViresh Kumar } 3197813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); 3207813dd6fSViresh Kumar 3217813dd6fSViresh Kumar /** 3227813dd6fSViresh Kumar * dev_pm_opp_get_max_transition_latency() - Get max transition latency in 3237813dd6fSViresh Kumar * nanoseconds 3247813dd6fSViresh Kumar * @dev: device for which we do this operation 3257813dd6fSViresh Kumar * 3267813dd6fSViresh Kumar * Return: This function returns the max transition latency, in nanoseconds, to 3277813dd6fSViresh Kumar * switch from one OPP to other. 3287813dd6fSViresh Kumar */ 3297813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 3307813dd6fSViresh Kumar { 3317813dd6fSViresh Kumar return dev_pm_opp_get_max_volt_latency(dev) + 3327813dd6fSViresh Kumar dev_pm_opp_get_max_clock_latency(dev); 3337813dd6fSViresh Kumar } 3347813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 3357813dd6fSViresh Kumar 3367813dd6fSViresh Kumar /** 3377813dd6fSViresh Kumar * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 3387813dd6fSViresh Kumar * @dev: device for which we do this operation 3397813dd6fSViresh Kumar * 3407813dd6fSViresh Kumar * Return: This function returns the frequency of the OPP marked as suspend_opp 3417813dd6fSViresh Kumar * if one is available, else returns 0; 3427813dd6fSViresh Kumar */ 3437813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 3447813dd6fSViresh Kumar { 3457813dd6fSViresh Kumar struct opp_table *opp_table; 3467813dd6fSViresh Kumar unsigned long freq = 0; 3477813dd6fSViresh Kumar 3487813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3497813dd6fSViresh Kumar if (IS_ERR(opp_table)) 3507813dd6fSViresh Kumar return 0; 3517813dd6fSViresh Kumar 3527813dd6fSViresh Kumar if (opp_table->suspend_opp && opp_table->suspend_opp->available) 3537813dd6fSViresh Kumar freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 3547813dd6fSViresh Kumar 3557813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3567813dd6fSViresh Kumar 3577813dd6fSViresh Kumar return freq; 3587813dd6fSViresh Kumar } 3597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 3607813dd6fSViresh Kumar 361a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table) 362a1e8c136SViresh Kumar { 363a1e8c136SViresh Kumar struct dev_pm_opp *opp; 364a1e8c136SViresh Kumar int count = 0; 365a1e8c136SViresh Kumar 366a1e8c136SViresh Kumar mutex_lock(&opp_table->lock); 367a1e8c136SViresh Kumar 368a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 369a1e8c136SViresh Kumar if (opp->available) 370a1e8c136SViresh Kumar count++; 371a1e8c136SViresh Kumar } 372a1e8c136SViresh Kumar 373a1e8c136SViresh Kumar mutex_unlock(&opp_table->lock); 374a1e8c136SViresh Kumar 375a1e8c136SViresh Kumar return count; 376a1e8c136SViresh Kumar } 377a1e8c136SViresh Kumar 3787813dd6fSViresh Kumar /** 3797813dd6fSViresh Kumar * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table 3807813dd6fSViresh Kumar * @dev: device for which we do this operation 3817813dd6fSViresh Kumar * 3827813dd6fSViresh Kumar * Return: This function returns the number of available opps if there are any, 3837813dd6fSViresh Kumar * else returns 0 if none or the corresponding error value. 3847813dd6fSViresh Kumar */ 3857813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev) 3867813dd6fSViresh Kumar { 3877813dd6fSViresh Kumar struct opp_table *opp_table; 388a1e8c136SViresh Kumar int count; 3897813dd6fSViresh Kumar 3907813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3917813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 3927813dd6fSViresh Kumar count = PTR_ERR(opp_table); 393035ed072SFabio Estevam dev_dbg(dev, "%s: OPP table not found (%d)\n", 3947813dd6fSViresh Kumar __func__, count); 39509f662f9SViresh Kumar return count; 3967813dd6fSViresh Kumar } 3977813dd6fSViresh Kumar 398a1e8c136SViresh Kumar count = _get_opp_count(opp_table); 3997813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4007813dd6fSViresh Kumar 4017813dd6fSViresh Kumar return count; 4027813dd6fSViresh Kumar } 4037813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); 4047813dd6fSViresh Kumar 4057813dd6fSViresh Kumar /** 4067813dd6fSViresh Kumar * dev_pm_opp_find_freq_exact() - search for an exact frequency 4077813dd6fSViresh Kumar * @dev: device for which we do this operation 4087813dd6fSViresh Kumar * @freq: frequency to search for 4097813dd6fSViresh Kumar * @available: true/false - match for available opp 4107813dd6fSViresh Kumar * 4117813dd6fSViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 4127813dd6fSViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 4137813dd6fSViresh Kumar * be handled using IS_ERR. Error return values can be: 4147813dd6fSViresh Kumar * EINVAL: for bad pointer 4157813dd6fSViresh Kumar * ERANGE: no match found for search 4167813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 4177813dd6fSViresh Kumar * 4187813dd6fSViresh Kumar * Note: available is a modifier for the search. if available=true, then the 4197813dd6fSViresh Kumar * match is for exact matching frequency and is available in the stored OPP 4207813dd6fSViresh Kumar * table. if false, the match is for exact frequency which is not available. 4217813dd6fSViresh Kumar * 4227813dd6fSViresh Kumar * This provides a mechanism to enable an opp which is not available currently 4237813dd6fSViresh Kumar * or the opposite as well. 4247813dd6fSViresh Kumar * 4257813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 4267813dd6fSViresh Kumar * use. 4277813dd6fSViresh Kumar */ 4287813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 4297813dd6fSViresh Kumar unsigned long freq, 4307813dd6fSViresh Kumar bool available) 4317813dd6fSViresh Kumar { 4327813dd6fSViresh Kumar struct opp_table *opp_table; 4337813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 4347813dd6fSViresh Kumar 4357813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 4367813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 4377813dd6fSViresh Kumar int r = PTR_ERR(opp_table); 4387813dd6fSViresh Kumar 4397813dd6fSViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 4407813dd6fSViresh Kumar return ERR_PTR(r); 4417813dd6fSViresh Kumar } 4427813dd6fSViresh Kumar 4437813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 4447813dd6fSViresh Kumar 4457813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 4467813dd6fSViresh Kumar if (temp_opp->available == available && 4477813dd6fSViresh Kumar temp_opp->rate == freq) { 4487813dd6fSViresh Kumar opp = temp_opp; 4497813dd6fSViresh Kumar 4507813dd6fSViresh Kumar /* Increment the reference count of OPP */ 4517813dd6fSViresh Kumar dev_pm_opp_get(opp); 4527813dd6fSViresh Kumar break; 4537813dd6fSViresh Kumar } 4547813dd6fSViresh Kumar } 4557813dd6fSViresh Kumar 4567813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 4577813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4587813dd6fSViresh Kumar 4597813dd6fSViresh Kumar return opp; 4607813dd6fSViresh Kumar } 4617813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 4627813dd6fSViresh Kumar 4637813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 4647813dd6fSViresh Kumar unsigned long *freq) 4657813dd6fSViresh Kumar { 4667813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 4677813dd6fSViresh Kumar 4687813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 4697813dd6fSViresh Kumar 4707813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 4717813dd6fSViresh Kumar if (temp_opp->available && temp_opp->rate >= *freq) { 4727813dd6fSViresh Kumar opp = temp_opp; 4737813dd6fSViresh Kumar *freq = opp->rate; 4747813dd6fSViresh Kumar 4757813dd6fSViresh Kumar /* Increment the reference count of OPP */ 4767813dd6fSViresh Kumar dev_pm_opp_get(opp); 4777813dd6fSViresh Kumar break; 4787813dd6fSViresh Kumar } 4797813dd6fSViresh Kumar } 4807813dd6fSViresh Kumar 4817813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 4827813dd6fSViresh Kumar 4837813dd6fSViresh Kumar return opp; 4847813dd6fSViresh Kumar } 4857813dd6fSViresh Kumar 4867813dd6fSViresh Kumar /** 4877813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 4887813dd6fSViresh Kumar * @dev: device for which we do this operation 4897813dd6fSViresh Kumar * @freq: Start frequency 4907813dd6fSViresh Kumar * 4917813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 4927813dd6fSViresh Kumar * for a device. 4937813dd6fSViresh Kumar * 4947813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 4957813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 4967813dd6fSViresh Kumar * values can be: 4977813dd6fSViresh Kumar * EINVAL: for bad pointer 4987813dd6fSViresh Kumar * ERANGE: no match found for search 4997813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 5007813dd6fSViresh Kumar * 5017813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 5027813dd6fSViresh Kumar * use. 5037813dd6fSViresh Kumar */ 5047813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 5057813dd6fSViresh Kumar unsigned long *freq) 5067813dd6fSViresh Kumar { 5077813dd6fSViresh Kumar struct opp_table *opp_table; 5087813dd6fSViresh Kumar struct dev_pm_opp *opp; 5097813dd6fSViresh Kumar 5107813dd6fSViresh Kumar if (!dev || !freq) { 5117813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 5127813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 5137813dd6fSViresh Kumar } 5147813dd6fSViresh Kumar 5157813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 5167813dd6fSViresh Kumar if (IS_ERR(opp_table)) 5177813dd6fSViresh Kumar return ERR_CAST(opp_table); 5187813dd6fSViresh Kumar 5197813dd6fSViresh Kumar opp = _find_freq_ceil(opp_table, freq); 5207813dd6fSViresh Kumar 5217813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 5227813dd6fSViresh Kumar 5237813dd6fSViresh Kumar return opp; 5247813dd6fSViresh Kumar } 5257813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 5267813dd6fSViresh Kumar 5277813dd6fSViresh Kumar /** 5287813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 5297813dd6fSViresh Kumar * @dev: device for which we do this operation 5307813dd6fSViresh Kumar * @freq: Start frequency 5317813dd6fSViresh Kumar * 5327813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 5337813dd6fSViresh Kumar * for a device. 5347813dd6fSViresh Kumar * 5357813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 5367813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 5377813dd6fSViresh Kumar * values can be: 5387813dd6fSViresh Kumar * EINVAL: for bad pointer 5397813dd6fSViresh Kumar * ERANGE: no match found for search 5407813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 5417813dd6fSViresh Kumar * 5427813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 5437813dd6fSViresh Kumar * use. 5447813dd6fSViresh Kumar */ 5457813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 5467813dd6fSViresh Kumar unsigned long *freq) 5477813dd6fSViresh Kumar { 5487813dd6fSViresh Kumar struct opp_table *opp_table; 5497813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 5507813dd6fSViresh Kumar 5517813dd6fSViresh Kumar if (!dev || !freq) { 5527813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 5537813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 5547813dd6fSViresh Kumar } 5557813dd6fSViresh Kumar 5567813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 5577813dd6fSViresh Kumar if (IS_ERR(opp_table)) 5587813dd6fSViresh Kumar return ERR_CAST(opp_table); 5597813dd6fSViresh Kumar 5607813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 5617813dd6fSViresh Kumar 5627813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 5637813dd6fSViresh Kumar if (temp_opp->available) { 5647813dd6fSViresh Kumar /* go to the next node, before choosing prev */ 5657813dd6fSViresh Kumar if (temp_opp->rate > *freq) 5667813dd6fSViresh Kumar break; 5677813dd6fSViresh Kumar else 5687813dd6fSViresh Kumar opp = temp_opp; 5697813dd6fSViresh Kumar } 5707813dd6fSViresh Kumar } 5717813dd6fSViresh Kumar 5727813dd6fSViresh Kumar /* Increment the reference count of OPP */ 5737813dd6fSViresh Kumar if (!IS_ERR(opp)) 5747813dd6fSViresh Kumar dev_pm_opp_get(opp); 5757813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 5767813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 5777813dd6fSViresh Kumar 5787813dd6fSViresh Kumar if (!IS_ERR(opp)) 5797813dd6fSViresh Kumar *freq = opp->rate; 5807813dd6fSViresh Kumar 5817813dd6fSViresh Kumar return opp; 5827813dd6fSViresh Kumar } 5837813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 5847813dd6fSViresh Kumar 5852f36bde0SAndrew-sh.Cheng /** 5862f36bde0SAndrew-sh.Cheng * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for 5872f36bde0SAndrew-sh.Cheng * target voltage. 5882f36bde0SAndrew-sh.Cheng * @dev: Device for which we do this operation. 5892f36bde0SAndrew-sh.Cheng * @u_volt: Target voltage. 5902f36bde0SAndrew-sh.Cheng * 5912f36bde0SAndrew-sh.Cheng * Search for OPP with highest (ceil) frequency and has voltage <= u_volt. 5922f36bde0SAndrew-sh.Cheng * 5932f36bde0SAndrew-sh.Cheng * Return: matching *opp, else returns ERR_PTR in case of error which should be 5942f36bde0SAndrew-sh.Cheng * handled using IS_ERR. 5952f36bde0SAndrew-sh.Cheng * 5962f36bde0SAndrew-sh.Cheng * Error return values can be: 5972f36bde0SAndrew-sh.Cheng * EINVAL: bad parameters 5982f36bde0SAndrew-sh.Cheng * 5992f36bde0SAndrew-sh.Cheng * The callers are required to call dev_pm_opp_put() for the returned OPP after 6002f36bde0SAndrew-sh.Cheng * use. 6012f36bde0SAndrew-sh.Cheng */ 6022f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev, 6032f36bde0SAndrew-sh.Cheng unsigned long u_volt) 6042f36bde0SAndrew-sh.Cheng { 6052f36bde0SAndrew-sh.Cheng struct opp_table *opp_table; 6062f36bde0SAndrew-sh.Cheng struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 6072f36bde0SAndrew-sh.Cheng 6082f36bde0SAndrew-sh.Cheng if (!dev || !u_volt) { 6092f36bde0SAndrew-sh.Cheng dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__, 6102f36bde0SAndrew-sh.Cheng u_volt); 6112f36bde0SAndrew-sh.Cheng return ERR_PTR(-EINVAL); 6122f36bde0SAndrew-sh.Cheng } 6132f36bde0SAndrew-sh.Cheng 6142f36bde0SAndrew-sh.Cheng opp_table = _find_opp_table(dev); 6152f36bde0SAndrew-sh.Cheng if (IS_ERR(opp_table)) 6162f36bde0SAndrew-sh.Cheng return ERR_CAST(opp_table); 6172f36bde0SAndrew-sh.Cheng 6182f36bde0SAndrew-sh.Cheng mutex_lock(&opp_table->lock); 6192f36bde0SAndrew-sh.Cheng 6202f36bde0SAndrew-sh.Cheng list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 6212f36bde0SAndrew-sh.Cheng if (temp_opp->available) { 6222f36bde0SAndrew-sh.Cheng if (temp_opp->supplies[0].u_volt > u_volt) 6232f36bde0SAndrew-sh.Cheng break; 6242f36bde0SAndrew-sh.Cheng opp = temp_opp; 6252f36bde0SAndrew-sh.Cheng } 6262f36bde0SAndrew-sh.Cheng } 6272f36bde0SAndrew-sh.Cheng 6282f36bde0SAndrew-sh.Cheng /* Increment the reference count of OPP */ 6292f36bde0SAndrew-sh.Cheng if (!IS_ERR(opp)) 6302f36bde0SAndrew-sh.Cheng dev_pm_opp_get(opp); 6312f36bde0SAndrew-sh.Cheng 6322f36bde0SAndrew-sh.Cheng mutex_unlock(&opp_table->lock); 6332f36bde0SAndrew-sh.Cheng dev_pm_opp_put_opp_table(opp_table); 6342f36bde0SAndrew-sh.Cheng 6352f36bde0SAndrew-sh.Cheng return opp; 6362f36bde0SAndrew-sh.Cheng } 6372f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt); 6382f36bde0SAndrew-sh.Cheng 63900ce3873SKrzysztof Kozlowski /** 64022079af7SViresh Kumar * dev_pm_opp_find_level_exact() - search for an exact level 64122079af7SViresh Kumar * @dev: device for which we do this operation 64222079af7SViresh Kumar * @level: level to search for 64322079af7SViresh Kumar * 64422079af7SViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 64522079af7SViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 64622079af7SViresh Kumar * be handled using IS_ERR. Error return values can be: 64722079af7SViresh Kumar * EINVAL: for bad pointer 64822079af7SViresh Kumar * ERANGE: no match found for search 64922079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 65022079af7SViresh Kumar * 65122079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 65222079af7SViresh Kumar * use. 65322079af7SViresh Kumar */ 65422079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev, 65522079af7SViresh Kumar unsigned int level) 65622079af7SViresh Kumar { 65722079af7SViresh Kumar struct opp_table *opp_table; 65822079af7SViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 65922079af7SViresh Kumar 66022079af7SViresh Kumar opp_table = _find_opp_table(dev); 66122079af7SViresh Kumar if (IS_ERR(opp_table)) { 66222079af7SViresh Kumar int r = PTR_ERR(opp_table); 66322079af7SViresh Kumar 66422079af7SViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 66522079af7SViresh Kumar return ERR_PTR(r); 66622079af7SViresh Kumar } 66722079af7SViresh Kumar 66822079af7SViresh Kumar mutex_lock(&opp_table->lock); 66922079af7SViresh Kumar 67022079af7SViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 67122079af7SViresh Kumar if (temp_opp->level == level) { 67222079af7SViresh Kumar opp = temp_opp; 67322079af7SViresh Kumar 67422079af7SViresh Kumar /* Increment the reference count of OPP */ 67522079af7SViresh Kumar dev_pm_opp_get(opp); 67622079af7SViresh Kumar break; 67722079af7SViresh Kumar } 67822079af7SViresh Kumar } 67922079af7SViresh Kumar 68022079af7SViresh Kumar mutex_unlock(&opp_table->lock); 68122079af7SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 68222079af7SViresh Kumar 68322079af7SViresh Kumar return opp; 68422079af7SViresh Kumar } 68522079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact); 68622079af7SViresh Kumar 68722079af7SViresh Kumar /** 68822079af7SViresh Kumar * dev_pm_opp_find_level_ceil() - search for an rounded up level 68922079af7SViresh Kumar * @dev: device for which we do this operation 69022079af7SViresh Kumar * @level: level to search for 69122079af7SViresh Kumar * 69222079af7SViresh Kumar * Return: Searches for rounded up match in the opp table and returns pointer 69322079af7SViresh Kumar * to the matching opp if found, else returns ERR_PTR in case of error and 69422079af7SViresh Kumar * should be handled using IS_ERR. Error return values can be: 69522079af7SViresh Kumar * EINVAL: for bad pointer 69622079af7SViresh Kumar * ERANGE: no match found for search 69722079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 69822079af7SViresh Kumar * 69922079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 70022079af7SViresh Kumar * use. 70122079af7SViresh Kumar */ 70222079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev, 70322079af7SViresh Kumar unsigned int *level) 70422079af7SViresh Kumar { 70522079af7SViresh Kumar struct opp_table *opp_table; 70622079af7SViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 70722079af7SViresh Kumar 70822079af7SViresh Kumar opp_table = _find_opp_table(dev); 70922079af7SViresh Kumar if (IS_ERR(opp_table)) { 71022079af7SViresh Kumar int r = PTR_ERR(opp_table); 71122079af7SViresh Kumar 71222079af7SViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 71322079af7SViresh Kumar return ERR_PTR(r); 71422079af7SViresh Kumar } 71522079af7SViresh Kumar 71622079af7SViresh Kumar mutex_lock(&opp_table->lock); 71722079af7SViresh Kumar 71822079af7SViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 71922079af7SViresh Kumar if (temp_opp->available && temp_opp->level >= *level) { 72022079af7SViresh Kumar opp = temp_opp; 72122079af7SViresh Kumar *level = opp->level; 72222079af7SViresh Kumar 72322079af7SViresh Kumar /* Increment the reference count of OPP */ 72422079af7SViresh Kumar dev_pm_opp_get(opp); 72522079af7SViresh Kumar break; 72622079af7SViresh Kumar } 72722079af7SViresh Kumar } 72822079af7SViresh Kumar 72922079af7SViresh Kumar mutex_unlock(&opp_table->lock); 73022079af7SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 73122079af7SViresh Kumar 73222079af7SViresh Kumar return opp; 73322079af7SViresh Kumar } 73422079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil); 73522079af7SViresh Kumar 73622079af7SViresh Kumar /** 73700ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth 73800ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 739617df304SYang Li * @bw: start bandwidth 74000ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 74100ce3873SKrzysztof Kozlowski * 74200ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 74300ce3873SKrzysztof Kozlowski * for a device. 74400ce3873SKrzysztof Kozlowski * 74500ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 74600ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 74700ce3873SKrzysztof Kozlowski * values can be: 74800ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 74900ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 75000ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 75100ce3873SKrzysztof Kozlowski * 75200ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 75300ce3873SKrzysztof Kozlowski * use. 75400ce3873SKrzysztof Kozlowski */ 75500ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, 75600ce3873SKrzysztof Kozlowski unsigned int *bw, int index) 75700ce3873SKrzysztof Kozlowski { 75800ce3873SKrzysztof Kozlowski struct opp_table *opp_table; 75900ce3873SKrzysztof Kozlowski struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 76000ce3873SKrzysztof Kozlowski 76100ce3873SKrzysztof Kozlowski if (!dev || !bw) { 76200ce3873SKrzysztof Kozlowski dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw); 76300ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 76400ce3873SKrzysztof Kozlowski } 76500ce3873SKrzysztof Kozlowski 76600ce3873SKrzysztof Kozlowski opp_table = _find_opp_table(dev); 76700ce3873SKrzysztof Kozlowski if (IS_ERR(opp_table)) 76800ce3873SKrzysztof Kozlowski return ERR_CAST(opp_table); 76900ce3873SKrzysztof Kozlowski 77000ce3873SKrzysztof Kozlowski if (index >= opp_table->path_count) 77100ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 77200ce3873SKrzysztof Kozlowski 77300ce3873SKrzysztof Kozlowski mutex_lock(&opp_table->lock); 77400ce3873SKrzysztof Kozlowski 77500ce3873SKrzysztof Kozlowski list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 77600ce3873SKrzysztof Kozlowski if (temp_opp->available && temp_opp->bandwidth) { 77700ce3873SKrzysztof Kozlowski if (temp_opp->bandwidth[index].peak >= *bw) { 77800ce3873SKrzysztof Kozlowski opp = temp_opp; 77900ce3873SKrzysztof Kozlowski *bw = opp->bandwidth[index].peak; 78000ce3873SKrzysztof Kozlowski 78100ce3873SKrzysztof Kozlowski /* Increment the reference count of OPP */ 78200ce3873SKrzysztof Kozlowski dev_pm_opp_get(opp); 78300ce3873SKrzysztof Kozlowski break; 78400ce3873SKrzysztof Kozlowski } 78500ce3873SKrzysztof Kozlowski } 78600ce3873SKrzysztof Kozlowski } 78700ce3873SKrzysztof Kozlowski 78800ce3873SKrzysztof Kozlowski mutex_unlock(&opp_table->lock); 78900ce3873SKrzysztof Kozlowski dev_pm_opp_put_opp_table(opp_table); 79000ce3873SKrzysztof Kozlowski 79100ce3873SKrzysztof Kozlowski return opp; 79200ce3873SKrzysztof Kozlowski } 79300ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil); 79400ce3873SKrzysztof Kozlowski 79500ce3873SKrzysztof Kozlowski /** 79600ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth 79700ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 798617df304SYang Li * @bw: start bandwidth 79900ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 80000ce3873SKrzysztof Kozlowski * 80100ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 80200ce3873SKrzysztof Kozlowski * for a device. 80300ce3873SKrzysztof Kozlowski * 80400ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 80500ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 80600ce3873SKrzysztof Kozlowski * values can be: 80700ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 80800ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 80900ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 81000ce3873SKrzysztof Kozlowski * 81100ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 81200ce3873SKrzysztof Kozlowski * use. 81300ce3873SKrzysztof Kozlowski */ 81400ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev, 81500ce3873SKrzysztof Kozlowski unsigned int *bw, int index) 81600ce3873SKrzysztof Kozlowski { 81700ce3873SKrzysztof Kozlowski struct opp_table *opp_table; 81800ce3873SKrzysztof Kozlowski struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 81900ce3873SKrzysztof Kozlowski 82000ce3873SKrzysztof Kozlowski if (!dev || !bw) { 82100ce3873SKrzysztof Kozlowski dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw); 82200ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 82300ce3873SKrzysztof Kozlowski } 82400ce3873SKrzysztof Kozlowski 82500ce3873SKrzysztof Kozlowski opp_table = _find_opp_table(dev); 82600ce3873SKrzysztof Kozlowski if (IS_ERR(opp_table)) 82700ce3873SKrzysztof Kozlowski return ERR_CAST(opp_table); 82800ce3873SKrzysztof Kozlowski 82900ce3873SKrzysztof Kozlowski if (index >= opp_table->path_count) 83000ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 83100ce3873SKrzysztof Kozlowski 83200ce3873SKrzysztof Kozlowski mutex_lock(&opp_table->lock); 83300ce3873SKrzysztof Kozlowski 83400ce3873SKrzysztof Kozlowski list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 83500ce3873SKrzysztof Kozlowski if (temp_opp->available && temp_opp->bandwidth) { 83600ce3873SKrzysztof Kozlowski /* go to the next node, before choosing prev */ 83700ce3873SKrzysztof Kozlowski if (temp_opp->bandwidth[index].peak > *bw) 83800ce3873SKrzysztof Kozlowski break; 83900ce3873SKrzysztof Kozlowski opp = temp_opp; 84000ce3873SKrzysztof Kozlowski } 84100ce3873SKrzysztof Kozlowski } 84200ce3873SKrzysztof Kozlowski 84300ce3873SKrzysztof Kozlowski /* Increment the reference count of OPP */ 84400ce3873SKrzysztof Kozlowski if (!IS_ERR(opp)) 84500ce3873SKrzysztof Kozlowski dev_pm_opp_get(opp); 84600ce3873SKrzysztof Kozlowski mutex_unlock(&opp_table->lock); 84700ce3873SKrzysztof Kozlowski dev_pm_opp_put_opp_table(opp_table); 84800ce3873SKrzysztof Kozlowski 84900ce3873SKrzysztof Kozlowski if (!IS_ERR(opp)) 85000ce3873SKrzysztof Kozlowski *bw = opp->bandwidth[index].peak; 85100ce3873SKrzysztof Kozlowski 85200ce3873SKrzysztof Kozlowski return opp; 85300ce3873SKrzysztof Kozlowski } 85400ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor); 85500ce3873SKrzysztof Kozlowski 8567813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 8577813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 8587813dd6fSViresh Kumar { 8597813dd6fSViresh Kumar int ret; 8607813dd6fSViresh Kumar 8617813dd6fSViresh Kumar /* Regulator not available for device */ 8627813dd6fSViresh Kumar if (IS_ERR(reg)) { 8637813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 8647813dd6fSViresh Kumar PTR_ERR(reg)); 8657813dd6fSViresh Kumar return 0; 8667813dd6fSViresh Kumar } 8677813dd6fSViresh Kumar 8687813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 8697813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 8707813dd6fSViresh Kumar 8717813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 8727813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 8737813dd6fSViresh Kumar if (ret) 8747813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 8757813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 8767813dd6fSViresh Kumar supply->u_volt_max, ret); 8777813dd6fSViresh Kumar 8787813dd6fSViresh Kumar return ret; 8797813dd6fSViresh Kumar } 8807813dd6fSViresh Kumar 881285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 882285881b5SViresh Kumar unsigned long freq) 8837813dd6fSViresh Kumar { 8847813dd6fSViresh Kumar int ret; 8857813dd6fSViresh Kumar 88635e74b2eSViresh Kumar /* We may reach here for devices which don't change frequency */ 88735e74b2eSViresh Kumar if (IS_ERR(clk)) 88835e74b2eSViresh Kumar return 0; 88935e74b2eSViresh Kumar 8907813dd6fSViresh Kumar ret = clk_set_rate(clk, freq); 8917813dd6fSViresh Kumar if (ret) { 8927813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 8937813dd6fSViresh Kumar ret); 8947813dd6fSViresh Kumar } 8957813dd6fSViresh Kumar 8967813dd6fSViresh Kumar return ret; 8977813dd6fSViresh Kumar } 8987813dd6fSViresh Kumar 8998d45719cSKamil Konieczny static int _generic_set_opp_regulator(struct opp_table *opp_table, 9007813dd6fSViresh Kumar struct device *dev, 9013f62670fSViresh Kumar struct dev_pm_opp *opp, 9027813dd6fSViresh Kumar unsigned long freq, 9033f62670fSViresh Kumar int scaling_down) 9047813dd6fSViresh Kumar { 9057813dd6fSViresh Kumar struct regulator *reg = opp_table->regulators[0]; 9063f62670fSViresh Kumar struct dev_pm_opp *old_opp = opp_table->current_opp; 9077813dd6fSViresh Kumar int ret; 9087813dd6fSViresh Kumar 9097813dd6fSViresh Kumar /* This function only supports single regulator per device */ 9107813dd6fSViresh Kumar if (WARN_ON(opp_table->regulator_count > 1)) { 9117813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 9127813dd6fSViresh Kumar return -EINVAL; 9137813dd6fSViresh Kumar } 9147813dd6fSViresh Kumar 9157813dd6fSViresh Kumar /* Scaling up? Scale voltage before frequency */ 9163f62670fSViresh Kumar if (!scaling_down) { 9173f62670fSViresh Kumar ret = _set_opp_voltage(dev, reg, opp->supplies); 9187813dd6fSViresh Kumar if (ret) 9197813dd6fSViresh Kumar goto restore_voltage; 9207813dd6fSViresh Kumar } 9217813dd6fSViresh Kumar 9227813dd6fSViresh Kumar /* Change frequency */ 923285881b5SViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 9247813dd6fSViresh Kumar if (ret) 9257813dd6fSViresh Kumar goto restore_voltage; 9267813dd6fSViresh Kumar 9277813dd6fSViresh Kumar /* Scaling down? Scale voltage after frequency */ 9283f62670fSViresh Kumar if (scaling_down) { 9293f62670fSViresh Kumar ret = _set_opp_voltage(dev, reg, opp->supplies); 9307813dd6fSViresh Kumar if (ret) 9317813dd6fSViresh Kumar goto restore_freq; 9327813dd6fSViresh Kumar } 9337813dd6fSViresh Kumar 9348d45719cSKamil Konieczny /* 9358d45719cSKamil Konieczny * Enable the regulator after setting its voltages, otherwise it breaks 9368d45719cSKamil Konieczny * some boot-enabled regulators. 9378d45719cSKamil Konieczny */ 93872f80ce4SViresh Kumar if (unlikely(!opp_table->enabled)) { 9398d45719cSKamil Konieczny ret = regulator_enable(reg); 9408d45719cSKamil Konieczny if (ret < 0) 9418d45719cSKamil Konieczny dev_warn(dev, "Failed to enable regulator: %d", ret); 9428d45719cSKamil Konieczny } 9438d45719cSKamil Konieczny 9447813dd6fSViresh Kumar return 0; 9457813dd6fSViresh Kumar 9467813dd6fSViresh Kumar restore_freq: 9473f62670fSViresh Kumar if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate)) 9487813dd6fSViresh Kumar dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 9493f62670fSViresh Kumar __func__, old_opp->rate); 9507813dd6fSViresh Kumar restore_voltage: 9517813dd6fSViresh Kumar /* This shouldn't harm even if the voltages weren't updated earlier */ 9523f62670fSViresh Kumar _set_opp_voltage(dev, reg, old_opp->supplies); 9537813dd6fSViresh Kumar 9547813dd6fSViresh Kumar return ret; 9557813dd6fSViresh Kumar } 9567813dd6fSViresh Kumar 957b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table, 958240ae50eSViresh Kumar struct dev_pm_opp *opp, struct device *dev) 959b00e667aSViresh Kumar { 960b00e667aSViresh Kumar u32 avg, peak; 961b00e667aSViresh Kumar int i, ret; 962b00e667aSViresh Kumar 963b00e667aSViresh Kumar if (!opp_table->paths) 964b00e667aSViresh Kumar return 0; 965b00e667aSViresh Kumar 966b00e667aSViresh Kumar for (i = 0; i < opp_table->path_count; i++) { 967240ae50eSViresh Kumar if (!opp) { 968b00e667aSViresh Kumar avg = 0; 969b00e667aSViresh Kumar peak = 0; 970b00e667aSViresh Kumar } else { 971b00e667aSViresh Kumar avg = opp->bandwidth[i].avg; 972b00e667aSViresh Kumar peak = opp->bandwidth[i].peak; 973b00e667aSViresh Kumar } 974b00e667aSViresh Kumar ret = icc_set_bw(opp_table->paths[i], avg, peak); 975b00e667aSViresh Kumar if (ret) { 976b00e667aSViresh Kumar dev_err(dev, "Failed to %s bandwidth[%d]: %d\n", 977240ae50eSViresh Kumar opp ? "set" : "remove", i, ret); 978b00e667aSViresh Kumar return ret; 979b00e667aSViresh Kumar } 980b00e667aSViresh Kumar } 981b00e667aSViresh Kumar 982b00e667aSViresh Kumar return 0; 983b00e667aSViresh Kumar } 984b00e667aSViresh Kumar 9857e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table, 986509e4777SViresh Kumar struct device *dev, struct dev_pm_opp *opp, 987509e4777SViresh Kumar unsigned long freq) 9887e535993SViresh Kumar { 98904b447dfSDmitry Osipenko struct dev_pm_set_opp_data *data = opp_table->set_opp_data; 990509e4777SViresh Kumar struct dev_pm_opp *old_opp = opp_table->current_opp; 9917e535993SViresh Kumar int size; 9927e535993SViresh Kumar 99304b447dfSDmitry Osipenko /* 994b0ec0942SViresh Kumar * We support this only if dev_pm_opp_set_config() was called 995b0ec0942SViresh Kumar * earlier to set regulators. 99604b447dfSDmitry Osipenko */ 99704b447dfSDmitry Osipenko if (opp_table->sod_supplies) { 998509e4777SViresh Kumar size = sizeof(*old_opp->supplies) * opp_table->regulator_count; 999509e4777SViresh Kumar memcpy(data->old_opp.supplies, old_opp->supplies, size); 1000509e4777SViresh Kumar memcpy(data->new_opp.supplies, opp->supplies, size); 100104b447dfSDmitry Osipenko data->regulator_count = opp_table->regulator_count; 100204b447dfSDmitry Osipenko } else { 100304b447dfSDmitry Osipenko data->regulator_count = 0; 100404b447dfSDmitry Osipenko } 100504b447dfSDmitry Osipenko 100604b447dfSDmitry Osipenko data->regulators = opp_table->regulators; 100704b447dfSDmitry Osipenko data->clk = opp_table->clk; 100804b447dfSDmitry Osipenko data->dev = dev; 1009509e4777SViresh Kumar data->old_opp.rate = old_opp->rate; 101004b447dfSDmitry Osipenko data->new_opp.rate = freq; 10117e535993SViresh Kumar 10127e535993SViresh Kumar return opp_table->set_opp(data); 10137e535993SViresh Kumar } 10147e535993SViresh Kumar 101560cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev, 101660cdeae0SStephan Gerhold struct dev_pm_opp *opp, int i) 101760cdeae0SStephan Gerhold { 101860cdeae0SStephan Gerhold unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0; 101960cdeae0SStephan Gerhold int ret; 102060cdeae0SStephan Gerhold 102160cdeae0SStephan Gerhold if (!pd_dev) 102260cdeae0SStephan Gerhold return 0; 102360cdeae0SStephan Gerhold 102460cdeae0SStephan Gerhold ret = dev_pm_genpd_set_performance_state(pd_dev, pstate); 102560cdeae0SStephan Gerhold if (ret) { 10269bfb1fffSViresh Kumar dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", 102760cdeae0SStephan Gerhold dev_name(pd_dev), pstate, ret); 102860cdeae0SStephan Gerhold } 102960cdeae0SStephan Gerhold 103060cdeae0SStephan Gerhold return ret; 103160cdeae0SStephan Gerhold } 103260cdeae0SStephan Gerhold 1033ca1b5d77SViresh Kumar /* This is only called for PM domain for now */ 1034ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev, 1035ca1b5d77SViresh Kumar struct opp_table *opp_table, 10362c59138cSStephan Gerhold struct dev_pm_opp *opp, bool up) 1037ca1b5d77SViresh Kumar { 1038ca1b5d77SViresh Kumar struct opp_table **required_opp_tables = opp_table->required_opp_tables; 1039ca1b5d77SViresh Kumar struct device **genpd_virt_devs = opp_table->genpd_virt_devs; 1040ca1b5d77SViresh Kumar int i, ret = 0; 1041ca1b5d77SViresh Kumar 1042ca1b5d77SViresh Kumar if (!required_opp_tables) 1043ca1b5d77SViresh Kumar return 0; 1044ca1b5d77SViresh Kumar 104519526d09SMarijn Suijten /* required-opps not fully initialized yet */ 104619526d09SMarijn Suijten if (lazy_linking_pending(opp_table)) 104719526d09SMarijn Suijten return -EBUSY; 104819526d09SMarijn Suijten 10494fa82a87SHsin-Yi Wang /* 10504fa82a87SHsin-Yi Wang * We only support genpd's OPPs in the "required-opps" for now, as we 10514fa82a87SHsin-Yi Wang * don't know much about other use cases. Error out if the required OPP 10524fa82a87SHsin-Yi Wang * doesn't belong to a genpd. 10534fa82a87SHsin-Yi Wang */ 10544fa82a87SHsin-Yi Wang if (unlikely(!required_opp_tables[0]->is_genpd)) { 10554fa82a87SHsin-Yi Wang dev_err(dev, "required-opps don't belong to a genpd\n"); 10564fa82a87SHsin-Yi Wang return -ENOENT; 10574fa82a87SHsin-Yi Wang } 10584fa82a87SHsin-Yi Wang 1059ca1b5d77SViresh Kumar /* Single genpd case */ 106060cdeae0SStephan Gerhold if (!genpd_virt_devs) 106160cdeae0SStephan Gerhold return _set_required_opp(dev, dev, opp, 0); 1062ca1b5d77SViresh Kumar 1063ca1b5d77SViresh Kumar /* Multiple genpd case */ 1064ca1b5d77SViresh Kumar 1065ca1b5d77SViresh Kumar /* 1066ca1b5d77SViresh Kumar * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 1067ca1b5d77SViresh Kumar * after it is freed from another thread. 1068ca1b5d77SViresh Kumar */ 1069ca1b5d77SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 1070ca1b5d77SViresh Kumar 10712c59138cSStephan Gerhold /* Scaling up? Set required OPPs in normal order, else reverse */ 10722c59138cSStephan Gerhold if (up) { 1073ca1b5d77SViresh Kumar for (i = 0; i < opp_table->required_opp_count; i++) { 107460cdeae0SStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 107560cdeae0SStephan Gerhold if (ret) 1076ca1b5d77SViresh Kumar break; 1077ca1b5d77SViresh Kumar } 10782c59138cSStephan Gerhold } else { 10792c59138cSStephan Gerhold for (i = opp_table->required_opp_count - 1; i >= 0; i--) { 10802c59138cSStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 10812c59138cSStephan Gerhold if (ret) 1082ca1b5d77SViresh Kumar break; 1083ca1b5d77SViresh Kumar } 1084ca1b5d77SViresh Kumar } 10852c59138cSStephan Gerhold 1086ca1b5d77SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 1087ca1b5d77SViresh Kumar 1088ca1b5d77SViresh Kumar return ret; 1089ca1b5d77SViresh Kumar } 1090ca1b5d77SViresh Kumar 109181c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table) 109281c4d8a3SViresh Kumar { 109381c4d8a3SViresh Kumar struct dev_pm_opp *opp = ERR_PTR(-ENODEV); 109481c4d8a3SViresh Kumar unsigned long freq; 109581c4d8a3SViresh Kumar 109681c4d8a3SViresh Kumar if (!IS_ERR(opp_table->clk)) { 109781c4d8a3SViresh Kumar freq = clk_get_rate(opp_table->clk); 109881c4d8a3SViresh Kumar opp = _find_freq_ceil(opp_table, &freq); 109981c4d8a3SViresh Kumar } 110081c4d8a3SViresh Kumar 110181c4d8a3SViresh Kumar /* 110281c4d8a3SViresh Kumar * Unable to find the current OPP ? Pick the first from the list since 110381c4d8a3SViresh Kumar * it is in ascending order, otherwise rest of the code will need to 110481c4d8a3SViresh Kumar * make special checks to validate current_opp. 110581c4d8a3SViresh Kumar */ 110681c4d8a3SViresh Kumar if (IS_ERR(opp)) { 110781c4d8a3SViresh Kumar mutex_lock(&opp_table->lock); 110881c4d8a3SViresh Kumar opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node); 110981c4d8a3SViresh Kumar dev_pm_opp_get(opp); 111081c4d8a3SViresh Kumar mutex_unlock(&opp_table->lock); 111181c4d8a3SViresh Kumar } 111281c4d8a3SViresh Kumar 111381c4d8a3SViresh Kumar opp_table->current_opp = opp; 111481c4d8a3SViresh Kumar } 111581c4d8a3SViresh Kumar 11165ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table) 1117f3364e17SViresh Kumar { 1118f3364e17SViresh Kumar int ret; 1119f3364e17SViresh Kumar 1120f3364e17SViresh Kumar if (!opp_table->enabled) 1121f3364e17SViresh Kumar return 0; 1122f3364e17SViresh Kumar 1123f3364e17SViresh Kumar /* 1124f3364e17SViresh Kumar * Some drivers need to support cases where some platforms may 1125f3364e17SViresh Kumar * have OPP table for the device, while others don't and 1126f3364e17SViresh Kumar * opp_set_rate() just needs to behave like clk_set_rate(). 1127f3364e17SViresh Kumar */ 1128f3364e17SViresh Kumar if (!_get_opp_count(opp_table)) 1129f3364e17SViresh Kumar return 0; 1130f3364e17SViresh Kumar 1131240ae50eSViresh Kumar ret = _set_opp_bw(opp_table, NULL, dev); 1132f3364e17SViresh Kumar if (ret) 1133f3364e17SViresh Kumar return ret; 1134f3364e17SViresh Kumar 1135f3364e17SViresh Kumar if (opp_table->regulators) 1136f3364e17SViresh Kumar regulator_disable(opp_table->regulators[0]); 1137f3364e17SViresh Kumar 11382c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, NULL, false); 1139f3364e17SViresh Kumar 1140f3364e17SViresh Kumar opp_table->enabled = false; 1141f3364e17SViresh Kumar return ret; 1142f3364e17SViresh Kumar } 1143f3364e17SViresh Kumar 1144386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table, 1145386ba854SViresh Kumar struct dev_pm_opp *opp, unsigned long freq) 11467813dd6fSViresh Kumar { 1147386ba854SViresh Kumar struct dev_pm_opp *old_opp; 1148f0b88fa4SViresh Kumar int scaling_down, ret; 11497813dd6fSViresh Kumar 1150386ba854SViresh Kumar if (unlikely(!opp)) 1151386ba854SViresh Kumar return _disable_opp_table(dev, opp_table); 1152aca48b61SRajendra Nayak 115381c4d8a3SViresh Kumar /* Find the currently set OPP if we don't know already */ 115481c4d8a3SViresh Kumar if (unlikely(!opp_table->current_opp)) 115581c4d8a3SViresh Kumar _find_current_opp(dev, opp_table); 11567813dd6fSViresh Kumar 115781c4d8a3SViresh Kumar old_opp = opp_table->current_opp; 115881c4d8a3SViresh Kumar 115981c4d8a3SViresh Kumar /* Return early if nothing to do */ 1160de04241aSJonathan Marek if (old_opp == opp && opp_table->current_rate == freq && 1161de04241aSJonathan Marek opp_table->enabled) { 116281c4d8a3SViresh Kumar dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__); 1163386ba854SViresh Kumar return 0; 11647813dd6fSViresh Kumar } 11657813dd6fSViresh Kumar 1166f0b88fa4SViresh Kumar dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n", 1167de04241aSJonathan Marek __func__, opp_table->current_rate, freq, old_opp->level, 1168de04241aSJonathan Marek opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0, 1169f0b88fa4SViresh Kumar opp->bandwidth ? opp->bandwidth[0].peak : 0); 1170f0b88fa4SViresh Kumar 1171f0b88fa4SViresh Kumar scaling_down = _opp_compare_key(old_opp, opp); 1172f0b88fa4SViresh Kumar if (scaling_down == -1) 1173f0b88fa4SViresh Kumar scaling_down = 0; 11747813dd6fSViresh Kumar 1175ca1b5d77SViresh Kumar /* Scaling up? Configure required OPPs before frequency */ 1176f0b88fa4SViresh Kumar if (!scaling_down) { 11772c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, opp, true); 1178870d5d96SViresh Kumar if (ret) { 1179870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1180386ba854SViresh Kumar return ret; 1181ca1b5d77SViresh Kumar } 1182ca1b5d77SViresh Kumar 1183870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1184870d5d96SViresh Kumar if (ret) { 1185870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1186870d5d96SViresh Kumar return ret; 1187870d5d96SViresh Kumar } 1188870d5d96SViresh Kumar } 1189870d5d96SViresh Kumar 11907e535993SViresh Kumar if (opp_table->set_opp) { 1191509e4777SViresh Kumar ret = _set_opp_custom(opp_table, dev, opp, freq); 11927e535993SViresh Kumar } else if (opp_table->regulators) { 11933f62670fSViresh Kumar ret = _generic_set_opp_regulator(opp_table, dev, opp, freq, 11943f62670fSViresh Kumar scaling_down); 11957813dd6fSViresh Kumar } else { 11967813dd6fSViresh Kumar /* Only frequency scaling */ 11971d3c42caSViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 11987813dd6fSViresh Kumar } 11997813dd6fSViresh Kumar 1200ca1b5d77SViresh Kumar if (ret) 1201870d5d96SViresh Kumar return ret; 1202870d5d96SViresh Kumar 1203870d5d96SViresh Kumar /* Scaling down? Configure required OPPs after frequency */ 1204870d5d96SViresh Kumar if (scaling_down) { 1205870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1206870d5d96SViresh Kumar if (ret) { 1207870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1208870d5d96SViresh Kumar return ret; 1209ca1b5d77SViresh Kumar } 1210ca1b5d77SViresh Kumar 1211870d5d96SViresh Kumar ret = _set_required_opps(dev, opp_table, opp, false); 1212870d5d96SViresh Kumar if (ret) { 1213870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1214870d5d96SViresh Kumar return ret; 1215870d5d96SViresh Kumar } 1216870d5d96SViresh Kumar } 1217870d5d96SViresh Kumar 121872f80ce4SViresh Kumar opp_table->enabled = true; 121981c4d8a3SViresh Kumar dev_pm_opp_put(old_opp); 122081c4d8a3SViresh Kumar 122181c4d8a3SViresh Kumar /* Make sure current_opp doesn't get freed */ 122281c4d8a3SViresh Kumar dev_pm_opp_get(opp); 122381c4d8a3SViresh Kumar opp_table->current_opp = opp; 1224de04241aSJonathan Marek opp_table->current_rate = freq; 1225fe2af402SGeorgi Djakov 1226386ba854SViresh Kumar return ret; 1227386ba854SViresh Kumar } 1228386ba854SViresh Kumar 1229386ba854SViresh Kumar /** 1230386ba854SViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 1231386ba854SViresh Kumar * @dev: device for which we do this operation 1232386ba854SViresh Kumar * @target_freq: frequency to achieve 1233386ba854SViresh Kumar * 1234386ba854SViresh Kumar * This configures the power-supplies to the levels specified by the OPP 1235386ba854SViresh Kumar * corresponding to the target_freq, and programs the clock to a value <= 1236386ba854SViresh Kumar * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax 1237386ba854SViresh Kumar * provided by the opp, should have already rounded to the target OPP's 1238386ba854SViresh Kumar * frequency. 1239386ba854SViresh Kumar */ 1240386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 1241386ba854SViresh Kumar { 1242386ba854SViresh Kumar struct opp_table *opp_table; 1243386ba854SViresh Kumar unsigned long freq = 0, temp_freq; 1244386ba854SViresh Kumar struct dev_pm_opp *opp = NULL; 1245386ba854SViresh Kumar int ret; 1246386ba854SViresh Kumar 1247386ba854SViresh Kumar opp_table = _find_opp_table(dev); 1248386ba854SViresh Kumar if (IS_ERR(opp_table)) { 1249386ba854SViresh Kumar dev_err(dev, "%s: device's opp table doesn't exist\n", __func__); 1250386ba854SViresh Kumar return PTR_ERR(opp_table); 1251386ba854SViresh Kumar } 1252386ba854SViresh Kumar 1253386ba854SViresh Kumar if (target_freq) { 1254386ba854SViresh Kumar /* 1255386ba854SViresh Kumar * For IO devices which require an OPP on some platforms/SoCs 1256386ba854SViresh Kumar * while just needing to scale the clock on some others 1257386ba854SViresh Kumar * we look for empty OPP tables with just a clock handle and 1258386ba854SViresh Kumar * scale only the clk. This makes dev_pm_opp_set_rate() 1259386ba854SViresh Kumar * equivalent to a clk_set_rate() 1260386ba854SViresh Kumar */ 1261386ba854SViresh Kumar if (!_get_opp_count(opp_table)) { 1262386ba854SViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq); 1263386ba854SViresh Kumar goto put_opp_table; 1264386ba854SViresh Kumar } 1265386ba854SViresh Kumar 1266386ba854SViresh Kumar freq = clk_round_rate(opp_table->clk, target_freq); 1267386ba854SViresh Kumar if ((long)freq <= 0) 1268386ba854SViresh Kumar freq = target_freq; 1269386ba854SViresh Kumar 1270386ba854SViresh Kumar /* 1271386ba854SViresh Kumar * The clock driver may support finer resolution of the 1272386ba854SViresh Kumar * frequencies than the OPP table, don't update the frequency we 1273386ba854SViresh Kumar * pass to clk_set_rate() here. 1274386ba854SViresh Kumar */ 1275386ba854SViresh Kumar temp_freq = freq; 1276386ba854SViresh Kumar opp = _find_freq_ceil(opp_table, &temp_freq); 1277386ba854SViresh Kumar if (IS_ERR(opp)) { 1278386ba854SViresh Kumar ret = PTR_ERR(opp); 1279386ba854SViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 1280386ba854SViresh Kumar __func__, freq, ret); 1281386ba854SViresh Kumar goto put_opp_table; 1282386ba854SViresh Kumar } 1283386ba854SViresh Kumar } 1284386ba854SViresh Kumar 1285386ba854SViresh Kumar ret = _set_opp(dev, opp_table, opp, freq); 1286386ba854SViresh Kumar 1287386ba854SViresh Kumar if (target_freq) 12887813dd6fSViresh Kumar dev_pm_opp_put(opp); 12897813dd6fSViresh Kumar put_opp_table: 12907813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 12917813dd6fSViresh Kumar return ret; 12927813dd6fSViresh Kumar } 12937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 12947813dd6fSViresh Kumar 1295abbe3483SViresh Kumar /** 1296abbe3483SViresh Kumar * dev_pm_opp_set_opp() - Configure device for OPP 1297abbe3483SViresh Kumar * @dev: device for which we do this operation 1298abbe3483SViresh Kumar * @opp: OPP to set to 1299abbe3483SViresh Kumar * 1300abbe3483SViresh Kumar * This configures the device based on the properties of the OPP passed to this 1301abbe3483SViresh Kumar * routine. 1302abbe3483SViresh Kumar * 1303abbe3483SViresh Kumar * Return: 0 on success, a negative error number otherwise. 1304abbe3483SViresh Kumar */ 1305abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp) 1306abbe3483SViresh Kumar { 1307abbe3483SViresh Kumar struct opp_table *opp_table; 1308abbe3483SViresh Kumar int ret; 1309abbe3483SViresh Kumar 1310abbe3483SViresh Kumar opp_table = _find_opp_table(dev); 1311abbe3483SViresh Kumar if (IS_ERR(opp_table)) { 1312abbe3483SViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 1313abbe3483SViresh Kumar return PTR_ERR(opp_table); 1314abbe3483SViresh Kumar } 1315abbe3483SViresh Kumar 1316abbe3483SViresh Kumar ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0); 1317abbe3483SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1318abbe3483SViresh Kumar 1319abbe3483SViresh Kumar return ret; 1320abbe3483SViresh Kumar } 1321abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp); 1322abbe3483SViresh Kumar 13237813dd6fSViresh Kumar /* OPP-dev Helpers */ 13247813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 13257813dd6fSViresh Kumar struct opp_table *opp_table) 13267813dd6fSViresh Kumar { 13277813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 13287813dd6fSViresh Kumar list_del(&opp_dev->node); 13297813dd6fSViresh Kumar kfree(opp_dev); 13307813dd6fSViresh Kumar } 13317813dd6fSViresh Kumar 1332ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 13337813dd6fSViresh Kumar struct opp_table *opp_table) 13347813dd6fSViresh Kumar { 13357813dd6fSViresh Kumar struct opp_device *opp_dev; 13367813dd6fSViresh Kumar 13377813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 13387813dd6fSViresh Kumar if (!opp_dev) 13397813dd6fSViresh Kumar return NULL; 13407813dd6fSViresh Kumar 13417813dd6fSViresh Kumar /* Initialize opp-dev */ 13427813dd6fSViresh Kumar opp_dev->dev = dev; 13433d255699SViresh Kumar 1344ef43f01aSViresh Kumar mutex_lock(&opp_table->lock); 13457813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 1346ef43f01aSViresh Kumar mutex_unlock(&opp_table->lock); 13477813dd6fSViresh Kumar 13487813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 1349a2dea4cbSGreg Kroah-Hartman opp_debug_register(opp_dev, opp_table); 1350283d55e6SViresh Kumar 1351283d55e6SViresh Kumar return opp_dev; 1352283d55e6SViresh Kumar } 1353283d55e6SViresh Kumar 1354eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index) 13557813dd6fSViresh Kumar { 13567813dd6fSViresh Kumar struct opp_table *opp_table; 13577813dd6fSViresh Kumar struct opp_device *opp_dev; 13587813dd6fSViresh Kumar int ret; 13597813dd6fSViresh Kumar 13607813dd6fSViresh Kumar /* 13617813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 13627813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 13637813dd6fSViresh Kumar */ 13647813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 13657813dd6fSViresh Kumar if (!opp_table) 1366dd461cd9SStephan Gerhold return ERR_PTR(-ENOMEM); 13677813dd6fSViresh Kumar 13683d255699SViresh Kumar mutex_init(&opp_table->lock); 13694f018bc0SViresh Kumar mutex_init(&opp_table->genpd_virt_dev_lock); 13707813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 13717eba0c76SViresh Kumar INIT_LIST_HEAD(&opp_table->lazy); 13727813dd6fSViresh Kumar 137346f48acaSViresh Kumar /* Mark regulator count uninitialized */ 137446f48acaSViresh Kumar opp_table->regulator_count = -1; 137546f48acaSViresh Kumar 13767813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 13777813dd6fSViresh Kumar if (!opp_dev) { 1378dd461cd9SStephan Gerhold ret = -ENOMEM; 1379dd461cd9SStephan Gerhold goto err; 13807813dd6fSViresh Kumar } 13817813dd6fSViresh Kumar 1382eb7c8743SViresh Kumar _of_init_opp_table(opp_table, dev, index); 13837813dd6fSViresh Kumar 13846d3f922cSGeorgi Djakov /* Find interconnect path(s) for the device */ 13856d3f922cSGeorgi Djakov ret = dev_pm_opp_of_find_icc_paths(dev, opp_table); 1386dd461cd9SStephan Gerhold if (ret) { 1387dd461cd9SStephan Gerhold if (ret == -EPROBE_DEFER) 138832439ac7SViresh Kumar goto remove_opp_dev; 1389dd461cd9SStephan Gerhold 13906d3f922cSGeorgi Djakov dev_warn(dev, "%s: Error finding interconnect paths: %d\n", 13916d3f922cSGeorgi Djakov __func__, ret); 1392dd461cd9SStephan Gerhold } 13936d3f922cSGeorgi Djakov 13947813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 13957813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 13967813dd6fSViresh Kumar kref_init(&opp_table->kref); 13977813dd6fSViresh Kumar 13987813dd6fSViresh Kumar return opp_table; 1399dd461cd9SStephan Gerhold 1400976509bbSQuanyang Wang remove_opp_dev: 1401976509bbSQuanyang Wang _remove_opp_dev(opp_dev, opp_table); 1402dd461cd9SStephan Gerhold err: 1403dd461cd9SStephan Gerhold kfree(opp_table); 1404dd461cd9SStephan Gerhold return ERR_PTR(ret); 14057813dd6fSViresh Kumar } 14067813dd6fSViresh Kumar 14077813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 14087813dd6fSViresh Kumar { 14097813dd6fSViresh Kumar kref_get(&opp_table->kref); 14107813dd6fSViresh Kumar } 14117813dd6fSViresh Kumar 141232439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev, 141332439ac7SViresh Kumar struct opp_table *opp_table, 141432439ac7SViresh Kumar bool getclk) 141532439ac7SViresh Kumar { 1416d4a4c7a4SViresh Kumar int ret; 1417d4a4c7a4SViresh Kumar 141832439ac7SViresh Kumar /* 141932439ac7SViresh Kumar * Return early if we don't need to get clk or we have already tried it 142032439ac7SViresh Kumar * earlier. 142132439ac7SViresh Kumar */ 142232439ac7SViresh Kumar if (!getclk || IS_ERR(opp_table) || opp_table->clk) 142332439ac7SViresh Kumar return opp_table; 142432439ac7SViresh Kumar 142532439ac7SViresh Kumar /* Find clk for the device */ 142632439ac7SViresh Kumar opp_table->clk = clk_get(dev, NULL); 142732439ac7SViresh Kumar 1428d4a4c7a4SViresh Kumar ret = PTR_ERR_OR_ZERO(opp_table->clk); 1429d4a4c7a4SViresh Kumar if (!ret) 143032439ac7SViresh Kumar return opp_table; 1431d4a4c7a4SViresh Kumar 1432d4a4c7a4SViresh Kumar if (ret == -ENOENT) { 1433d4a4c7a4SViresh Kumar dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret); 1434d4a4c7a4SViresh Kumar return opp_table; 1435d4a4c7a4SViresh Kumar } 1436d4a4c7a4SViresh Kumar 1437d4a4c7a4SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1438d4a4c7a4SViresh Kumar dev_err_probe(dev, ret, "Couldn't find clock\n"); 1439d4a4c7a4SViresh Kumar 1440d4a4c7a4SViresh Kumar return ERR_PTR(ret); 144132439ac7SViresh Kumar } 144232439ac7SViresh Kumar 144327c09484SViresh Kumar /* 144427c09484SViresh Kumar * We need to make sure that the OPP table for a device doesn't get added twice, 144527c09484SViresh Kumar * if this routine gets called in parallel with the same device pointer. 144627c09484SViresh Kumar * 144727c09484SViresh Kumar * The simplest way to enforce that is to perform everything (find existing 144827c09484SViresh Kumar * table and if not found, create a new one) under the opp_table_lock, so only 144927c09484SViresh Kumar * one creator gets access to the same. But that expands the critical section 145027c09484SViresh Kumar * under the lock and may end up causing circular dependencies with frameworks 145127c09484SViresh Kumar * like debugfs, interconnect or clock framework as they may be direct or 145227c09484SViresh Kumar * indirect users of OPP core. 145327c09484SViresh Kumar * 145427c09484SViresh Kumar * And for that reason we have to go for a bit tricky implementation here, which 145527c09484SViresh Kumar * uses the opp_tables_busy flag to indicate if another creator is in the middle 145627c09484SViresh Kumar * of adding an OPP table and others should wait for it to finish. 145727c09484SViresh Kumar */ 145832439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index, 145932439ac7SViresh Kumar bool getclk) 14607813dd6fSViresh Kumar { 14617813dd6fSViresh Kumar struct opp_table *opp_table; 14627813dd6fSViresh Kumar 146327c09484SViresh Kumar again: 14647813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 14657813dd6fSViresh Kumar 14667813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 14677813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 14687813dd6fSViresh Kumar goto unlock; 14697813dd6fSViresh Kumar 147027c09484SViresh Kumar /* 147127c09484SViresh Kumar * The opp_tables list or an OPP table's dev_list is getting updated by 147227c09484SViresh Kumar * another user, wait for it to finish. 147327c09484SViresh Kumar */ 147427c09484SViresh Kumar if (unlikely(opp_tables_busy)) { 147527c09484SViresh Kumar mutex_unlock(&opp_table_lock); 147627c09484SViresh Kumar cpu_relax(); 147727c09484SViresh Kumar goto again; 147827c09484SViresh Kumar } 147927c09484SViresh Kumar 148027c09484SViresh Kumar opp_tables_busy = true; 1481283d55e6SViresh Kumar opp_table = _managed_opp(dev, index); 148227c09484SViresh Kumar 148327c09484SViresh Kumar /* Drop the lock to reduce the size of critical section */ 148427c09484SViresh Kumar mutex_unlock(&opp_table_lock); 148527c09484SViresh Kumar 1486283d55e6SViresh Kumar if (opp_table) { 1487ef43f01aSViresh Kumar if (!_add_opp_dev(dev, opp_table)) { 1488283d55e6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1489dd461cd9SStephan Gerhold opp_table = ERR_PTR(-ENOMEM); 1490283d55e6SViresh Kumar } 149127c09484SViresh Kumar 149227c09484SViresh Kumar mutex_lock(&opp_table_lock); 149327c09484SViresh Kumar } else { 149427c09484SViresh Kumar opp_table = _allocate_opp_table(dev, index); 149527c09484SViresh Kumar 149627c09484SViresh Kumar mutex_lock(&opp_table_lock); 149727c09484SViresh Kumar if (!IS_ERR(opp_table)) 149827c09484SViresh Kumar list_add(&opp_table->node, &opp_tables); 1499283d55e6SViresh Kumar } 1500283d55e6SViresh Kumar 150127c09484SViresh Kumar opp_tables_busy = false; 15027813dd6fSViresh Kumar 15037813dd6fSViresh Kumar unlock: 15047813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 15057813dd6fSViresh Kumar 150632439ac7SViresh Kumar return _update_opp_table_clk(dev, opp_table, getclk); 15077813dd6fSViresh Kumar } 1508eb7c8743SViresh Kumar 150932439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk) 1510e77dcb0bSViresh Kumar { 151132439ac7SViresh Kumar return _add_opp_table_indexed(dev, 0, getclk); 1512e77dcb0bSViresh Kumar } 1513e77dcb0bSViresh Kumar 1514eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 1515eb7c8743SViresh Kumar { 1516e77dcb0bSViresh Kumar return _find_opp_table(dev); 1517eb7c8743SViresh Kumar } 15187813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 15197813dd6fSViresh Kumar 15207813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 15217813dd6fSViresh Kumar { 15227813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 1523cdd6ed90SViresh Kumar struct opp_device *opp_dev, *temp; 15246d3f922cSGeorgi Djakov int i; 15257813dd6fSViresh Kumar 1526e0df59deSViresh Kumar /* Drop the lock as soon as we can */ 1527e0df59deSViresh Kumar list_del(&opp_table->node); 1528e0df59deSViresh Kumar mutex_unlock(&opp_table_lock); 1529e0df59deSViresh Kumar 153081c4d8a3SViresh Kumar if (opp_table->current_opp) 153181c4d8a3SViresh Kumar dev_pm_opp_put(opp_table->current_opp); 153281c4d8a3SViresh Kumar 15335d6d106fSViresh Kumar _of_clear_opp_table(opp_table); 15345d6d106fSViresh Kumar 15357813dd6fSViresh Kumar /* Release clk */ 15367813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 15377813dd6fSViresh Kumar clk_put(opp_table->clk); 15387813dd6fSViresh Kumar 15396d3f922cSGeorgi Djakov if (opp_table->paths) { 15406d3f922cSGeorgi Djakov for (i = 0; i < opp_table->path_count; i++) 15416d3f922cSGeorgi Djakov icc_put(opp_table->paths[i]); 15426d3f922cSGeorgi Djakov kfree(opp_table->paths); 15436d3f922cSGeorgi Djakov } 15446d3f922cSGeorgi Djakov 1545cdd6ed90SViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 1546cdd6ed90SViresh Kumar 1547cdd6ed90SViresh Kumar list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 15483d255699SViresh Kumar /* 1549cdd6ed90SViresh Kumar * The OPP table is getting removed, drop the performance state 1550cdd6ed90SViresh Kumar * constraints. 15513d255699SViresh Kumar */ 1552cdd6ed90SViresh Kumar if (opp_table->genpd_performance_state) 1553cdd6ed90SViresh Kumar dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 15547813dd6fSViresh Kumar 15557813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 1556cdd6ed90SViresh Kumar } 15577813dd6fSViresh Kumar 15584f018bc0SViresh Kumar mutex_destroy(&opp_table->genpd_virt_dev_lock); 15597813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 15607813dd6fSViresh Kumar kfree(opp_table); 15617813dd6fSViresh Kumar } 15627813dd6fSViresh Kumar 15637813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 15647813dd6fSViresh Kumar { 15657813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 15667813dd6fSViresh Kumar &opp_table_lock); 15677813dd6fSViresh Kumar } 15687813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 15697813dd6fSViresh Kumar 15707813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 15717813dd6fSViresh Kumar { 15727813dd6fSViresh Kumar kfree(opp); 15737813dd6fSViresh Kumar } 15747813dd6fSViresh Kumar 1575cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref) 15767813dd6fSViresh Kumar { 1577cf1fac94SViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 1578cf1fac94SViresh Kumar struct opp_table *opp_table = opp->opp_table; 1579cf1fac94SViresh Kumar 1580cf1fac94SViresh Kumar list_del(&opp->node); 1581cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1582cf1fac94SViresh Kumar 15837813dd6fSViresh Kumar /* 15847813dd6fSViresh Kumar * Notify the changes in the availability of the operable 15857813dd6fSViresh Kumar * frequency/voltage list. 15867813dd6fSViresh Kumar */ 15877813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 1588da544b61SViresh Kumar _of_opp_free_required_opps(opp_table, opp); 15897813dd6fSViresh Kumar opp_debug_remove_one(opp); 15907813dd6fSViresh Kumar kfree(opp); 15911690d8bbSViresh Kumar } 15927813dd6fSViresh Kumar 1593a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 15947813dd6fSViresh Kumar { 15957813dd6fSViresh Kumar kref_get(&opp->kref); 15967813dd6fSViresh Kumar } 15977813dd6fSViresh Kumar 15987813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 15997813dd6fSViresh Kumar { 1600cf1fac94SViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 16017813dd6fSViresh Kumar } 16027813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 16037813dd6fSViresh Kumar 16047813dd6fSViresh Kumar /** 16057813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 16067813dd6fSViresh Kumar * @dev: device for which we do this operation 16077813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 16087813dd6fSViresh Kumar * 16097813dd6fSViresh Kumar * This function removes an opp from the opp table. 16107813dd6fSViresh Kumar */ 16117813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 16127813dd6fSViresh Kumar { 161395073b72SJakob Koschel struct dev_pm_opp *opp = NULL, *iter; 16147813dd6fSViresh Kumar struct opp_table *opp_table; 16157813dd6fSViresh Kumar 16167813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 16177813dd6fSViresh Kumar if (IS_ERR(opp_table)) 16187813dd6fSViresh Kumar return; 16197813dd6fSViresh Kumar 16207813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 16217813dd6fSViresh Kumar 162295073b72SJakob Koschel list_for_each_entry(iter, &opp_table->opp_list, node) { 162395073b72SJakob Koschel if (iter->rate == freq) { 162495073b72SJakob Koschel opp = iter; 16257813dd6fSViresh Kumar break; 16267813dd6fSViresh Kumar } 16277813dd6fSViresh Kumar } 16287813dd6fSViresh Kumar 16297813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 16307813dd6fSViresh Kumar 163195073b72SJakob Koschel if (opp) { 16327813dd6fSViresh Kumar dev_pm_opp_put(opp); 16330ad8c623SViresh Kumar 16340ad8c623SViresh Kumar /* Drop the reference taken by dev_pm_opp_add() */ 16350ad8c623SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16367813dd6fSViresh Kumar } else { 16377813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 16387813dd6fSViresh Kumar __func__, freq); 16397813dd6fSViresh Kumar } 16407813dd6fSViresh Kumar 16410ad8c623SViresh Kumar /* Drop the reference taken by _find_opp_table() */ 16427813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16437813dd6fSViresh Kumar } 16447813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 16457813dd6fSViresh Kumar 1646cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table, 1647cf1fac94SViresh Kumar bool dynamic) 1648cf1fac94SViresh Kumar { 1649cf1fac94SViresh Kumar struct dev_pm_opp *opp = NULL, *temp; 1650cf1fac94SViresh Kumar 1651cf1fac94SViresh Kumar mutex_lock(&opp_table->lock); 1652cf1fac94SViresh Kumar list_for_each_entry(temp, &opp_table->opp_list, node) { 1653606a5d42SBeata Michalska /* 1654606a5d42SBeata Michalska * Refcount must be dropped only once for each OPP by OPP core, 1655606a5d42SBeata Michalska * do that with help of "removed" flag. 1656606a5d42SBeata Michalska */ 1657606a5d42SBeata Michalska if (!temp->removed && dynamic == temp->dynamic) { 1658cf1fac94SViresh Kumar opp = temp; 1659cf1fac94SViresh Kumar break; 1660cf1fac94SViresh Kumar } 1661cf1fac94SViresh Kumar } 1662cf1fac94SViresh Kumar 1663cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1664cf1fac94SViresh Kumar return opp; 1665cf1fac94SViresh Kumar } 1666cf1fac94SViresh Kumar 1667606a5d42SBeata Michalska /* 1668606a5d42SBeata Michalska * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to 1669606a5d42SBeata Michalska * happen lock less to avoid circular dependency issues. This routine must be 1670606a5d42SBeata Michalska * called without the opp_table->lock held. 1671606a5d42SBeata Michalska */ 1672606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic) 167303758d60SViresh Kumar { 1674cf1fac94SViresh Kumar struct dev_pm_opp *opp; 167503758d60SViresh Kumar 1676606a5d42SBeata Michalska while ((opp = _opp_get_next(opp_table, dynamic))) { 1677606a5d42SBeata Michalska opp->removed = true; 1678606a5d42SBeata Michalska dev_pm_opp_put(opp); 1679606a5d42SBeata Michalska 1680606a5d42SBeata Michalska /* Drop the references taken by dev_pm_opp_add() */ 1681606a5d42SBeata Michalska if (dynamic) 1682606a5d42SBeata Michalska dev_pm_opp_put_opp_table(opp_table); 1683606a5d42SBeata Michalska } 1684606a5d42SBeata Michalska } 1685606a5d42SBeata Michalska 1686606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table) 1687606a5d42SBeata Michalska { 168803758d60SViresh Kumar mutex_lock(&opp_table->lock); 168903758d60SViresh Kumar 1690922ff075SViresh Kumar if (!opp_table->parsed_static_opps) { 1691cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1692cf1fac94SViresh Kumar return false; 1693922ff075SViresh Kumar } 1694922ff075SViresh Kumar 1695cf1fac94SViresh Kumar if (--opp_table->parsed_static_opps) { 1696cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1697cf1fac94SViresh Kumar return true; 169803758d60SViresh Kumar } 169903758d60SViresh Kumar 170003758d60SViresh Kumar mutex_unlock(&opp_table->lock); 1701922ff075SViresh Kumar 1702606a5d42SBeata Michalska _opp_remove_all(opp_table, false); 1703cf1fac94SViresh Kumar return true; 170403758d60SViresh Kumar } 170503758d60SViresh Kumar 17061690d8bbSViresh Kumar /** 17071690d8bbSViresh Kumar * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 17081690d8bbSViresh Kumar * @dev: device for which we do this operation 17091690d8bbSViresh Kumar * 17101690d8bbSViresh Kumar * This function removes all dynamically created OPPs from the opp table. 17111690d8bbSViresh Kumar */ 17121690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev) 17131690d8bbSViresh Kumar { 17141690d8bbSViresh Kumar struct opp_table *opp_table; 17151690d8bbSViresh Kumar 17161690d8bbSViresh Kumar opp_table = _find_opp_table(dev); 17171690d8bbSViresh Kumar if (IS_ERR(opp_table)) 17181690d8bbSViresh Kumar return; 17191690d8bbSViresh Kumar 1720606a5d42SBeata Michalska _opp_remove_all(opp_table, true); 17211690d8bbSViresh Kumar 17221690d8bbSViresh Kumar /* Drop the reference taken by _find_opp_table() */ 17231690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17241690d8bbSViresh Kumar } 17251690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 17261690d8bbSViresh Kumar 17277813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table) 17287813dd6fSViresh Kumar { 17297813dd6fSViresh Kumar struct dev_pm_opp *opp; 17306d3f922cSGeorgi Djakov int supply_count, supply_size, icc_size; 17317813dd6fSViresh Kumar 17327813dd6fSViresh Kumar /* Allocate space for at least one supply */ 17336d3f922cSGeorgi Djakov supply_count = table->regulator_count > 0 ? table->regulator_count : 1; 17346d3f922cSGeorgi Djakov supply_size = sizeof(*opp->supplies) * supply_count; 17356d3f922cSGeorgi Djakov icc_size = sizeof(*opp->bandwidth) * table->path_count; 17367813dd6fSViresh Kumar 17377813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 17386d3f922cSGeorgi Djakov opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL); 17396d3f922cSGeorgi Djakov 17407813dd6fSViresh Kumar if (!opp) 17417813dd6fSViresh Kumar return NULL; 17427813dd6fSViresh Kumar 17437813dd6fSViresh Kumar /* Put the supplies at the end of the OPP structure as an empty array */ 17447813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 17456d3f922cSGeorgi Djakov if (icc_size) 17466d3f922cSGeorgi Djakov opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count); 17477813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 17487813dd6fSViresh Kumar 17497813dd6fSViresh Kumar return opp; 17507813dd6fSViresh Kumar } 17517813dd6fSViresh Kumar 17527813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 17537813dd6fSViresh Kumar struct opp_table *opp_table) 17547813dd6fSViresh Kumar { 17557813dd6fSViresh Kumar struct regulator *reg; 17567813dd6fSViresh Kumar int i; 17577813dd6fSViresh Kumar 175890e3577bSViresh Kumar if (!opp_table->regulators) 175990e3577bSViresh Kumar return true; 176090e3577bSViresh Kumar 17617813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 17627813dd6fSViresh Kumar reg = opp_table->regulators[i]; 17637813dd6fSViresh Kumar 17647813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 17657813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 17667813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 17677813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 17687813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 17697813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 17707813dd6fSViresh Kumar return false; 17717813dd6fSViresh Kumar } 17727813dd6fSViresh Kumar } 17737813dd6fSViresh Kumar 17747813dd6fSViresh Kumar return true; 17757813dd6fSViresh Kumar } 17767813dd6fSViresh Kumar 17776c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2) 17786c591eecSSaravana Kannan { 17796c591eecSSaravana Kannan if (opp1->rate != opp2->rate) 17806c591eecSSaravana Kannan return opp1->rate < opp2->rate ? -1 : 1; 17816d3f922cSGeorgi Djakov if (opp1->bandwidth && opp2->bandwidth && 17826d3f922cSGeorgi Djakov opp1->bandwidth[0].peak != opp2->bandwidth[0].peak) 17836d3f922cSGeorgi Djakov return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1; 17846c591eecSSaravana Kannan if (opp1->level != opp2->level) 17856c591eecSSaravana Kannan return opp1->level < opp2->level ? -1 : 1; 17866c591eecSSaravana Kannan return 0; 17876c591eecSSaravana Kannan } 17886c591eecSSaravana Kannan 1789a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1790a1e8c136SViresh Kumar struct opp_table *opp_table, 1791a1e8c136SViresh Kumar struct list_head **head) 1792a1e8c136SViresh Kumar { 1793a1e8c136SViresh Kumar struct dev_pm_opp *opp; 17946c591eecSSaravana Kannan int opp_cmp; 1795a1e8c136SViresh Kumar 1796a1e8c136SViresh Kumar /* 1797a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1798a1e8c136SViresh Kumar * already present. 1799a1e8c136SViresh Kumar * 1800a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1801a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1802a1e8c136SViresh Kumar * loop. 1803a1e8c136SViresh Kumar */ 1804a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 18056c591eecSSaravana Kannan opp_cmp = _opp_compare_key(new_opp, opp); 18066c591eecSSaravana Kannan if (opp_cmp > 0) { 1807a1e8c136SViresh Kumar *head = &opp->node; 1808a1e8c136SViresh Kumar continue; 1809a1e8c136SViresh Kumar } 1810a1e8c136SViresh Kumar 18116c591eecSSaravana Kannan if (opp_cmp < 0) 1812a1e8c136SViresh Kumar return 0; 1813a1e8c136SViresh Kumar 1814a1e8c136SViresh Kumar /* Duplicate OPPs */ 1815a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1816a1e8c136SViresh Kumar __func__, opp->rate, opp->supplies[0].u_volt, 1817a1e8c136SViresh Kumar opp->available, new_opp->rate, 1818a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1819a1e8c136SViresh Kumar 1820a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1821a1e8c136SViresh Kumar return opp->available && 1822a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1823a1e8c136SViresh Kumar } 1824a1e8c136SViresh Kumar 1825a1e8c136SViresh Kumar return 0; 1826a1e8c136SViresh Kumar } 1827a1e8c136SViresh Kumar 18287eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count) 18297eba0c76SViresh Kumar { 18307eba0c76SViresh Kumar int i; 18317eba0c76SViresh Kumar 18327eba0c76SViresh Kumar for (i = 0; i < count; i++) { 18337eba0c76SViresh Kumar if (opp->required_opps[i]->available) 18347eba0c76SViresh Kumar continue; 18357eba0c76SViresh Kumar 18367eba0c76SViresh Kumar opp->available = false; 18377eba0c76SViresh Kumar pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n", 18387eba0c76SViresh Kumar __func__, opp->required_opps[i]->np, opp->rate); 18397eba0c76SViresh Kumar return; 18407eba0c76SViresh Kumar } 18417eba0c76SViresh Kumar } 18427eba0c76SViresh Kumar 18437813dd6fSViresh Kumar /* 18447813dd6fSViresh Kumar * Returns: 18457813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 18467813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 18477813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 18487813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 18497813dd6fSViresh Kumar * kernel try to initialize the OPP table. 18507813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 18517813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 18527813dd6fSViresh Kumar */ 18537813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1854a1e8c136SViresh Kumar struct opp_table *opp_table, bool rate_not_available) 18557813dd6fSViresh Kumar { 18567813dd6fSViresh Kumar struct list_head *head; 18577813dd6fSViresh Kumar int ret; 18587813dd6fSViresh Kumar 18597813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 18607813dd6fSViresh Kumar head = &opp_table->opp_list; 18617813dd6fSViresh Kumar 1862a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1863a1e8c136SViresh Kumar if (ret) { 18647813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 18657813dd6fSViresh Kumar return ret; 18667813dd6fSViresh Kumar } 18677813dd6fSViresh Kumar 18687813dd6fSViresh Kumar list_add(&new_opp->node, head); 18697813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 18707813dd6fSViresh Kumar 18717813dd6fSViresh Kumar new_opp->opp_table = opp_table; 18727813dd6fSViresh Kumar kref_init(&new_opp->kref); 18737813dd6fSViresh Kumar 1874a2dea4cbSGreg Kroah-Hartman opp_debug_create_one(new_opp, opp_table); 18757813dd6fSViresh Kumar 18767813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 18777813dd6fSViresh Kumar new_opp->available = false; 18787813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 18797813dd6fSViresh Kumar __func__, new_opp->rate); 18807813dd6fSViresh Kumar } 18817813dd6fSViresh Kumar 18827eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 18837eba0c76SViresh Kumar if (lazy_linking_pending(opp_table)) 18847eba0c76SViresh Kumar return 0; 1885cf65948dSDmitry Osipenko 18867eba0c76SViresh Kumar _required_opps_available(new_opp, opp_table->required_opp_count); 1887cf65948dSDmitry Osipenko 18887813dd6fSViresh Kumar return 0; 18897813dd6fSViresh Kumar } 18907813dd6fSViresh Kumar 18917813dd6fSViresh Kumar /** 18927813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 18937813dd6fSViresh Kumar * @opp_table: OPP table 18947813dd6fSViresh Kumar * @dev: device for which we do this operation 18957813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 18967813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 18977813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 18987813dd6fSViresh Kumar * 18997813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 19007813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 19017813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 19027813dd6fSViresh Kumar * 19037813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 19047813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 19057813dd6fSViresh Kumar * 19067813dd6fSViresh Kumar * Return: 19077813dd6fSViresh Kumar * 0 On success OR 19087813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 19097813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 19107813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 19117813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 19127813dd6fSViresh Kumar */ 19137813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 19147813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 19157813dd6fSViresh Kumar { 19167813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 19177813dd6fSViresh Kumar unsigned long tol; 19187813dd6fSViresh Kumar int ret; 19197813dd6fSViresh Kumar 19207813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 19217813dd6fSViresh Kumar if (!new_opp) 19227813dd6fSViresh Kumar return -ENOMEM; 19237813dd6fSViresh Kumar 19247813dd6fSViresh Kumar /* populate the opp table */ 19257813dd6fSViresh Kumar new_opp->rate = freq; 19267813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 19277813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 19287813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 19297813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 19307813dd6fSViresh Kumar new_opp->available = true; 19317813dd6fSViresh Kumar new_opp->dynamic = dynamic; 19327813dd6fSViresh Kumar 1933a1e8c136SViresh Kumar ret = _opp_add(dev, new_opp, opp_table, false); 19347813dd6fSViresh Kumar if (ret) { 19357813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 19367813dd6fSViresh Kumar if (ret == -EBUSY) 19377813dd6fSViresh Kumar ret = 0; 19387813dd6fSViresh Kumar goto free_opp; 19397813dd6fSViresh Kumar } 19407813dd6fSViresh Kumar 19417813dd6fSViresh Kumar /* 19427813dd6fSViresh Kumar * Notify the changes in the availability of the operable 19437813dd6fSViresh Kumar * frequency/voltage list. 19447813dd6fSViresh Kumar */ 19457813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 19467813dd6fSViresh Kumar return 0; 19477813dd6fSViresh Kumar 19487813dd6fSViresh Kumar free_opp: 19497813dd6fSViresh Kumar _opp_free(new_opp); 19507813dd6fSViresh Kumar 19517813dd6fSViresh Kumar return ret; 19527813dd6fSViresh Kumar } 19537813dd6fSViresh Kumar 19547813dd6fSViresh Kumar /** 1955*89f03984SViresh Kumar * _opp_set_supported_hw() - Set supported platforms 19567813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 19577813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 19587813dd6fSViresh Kumar * @count: Number of elements in the array. 19597813dd6fSViresh Kumar * 19607813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 19617813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 19627813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 19637813dd6fSViresh Kumar * property. 19647813dd6fSViresh Kumar */ 1965*89f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table, 19667813dd6fSViresh Kumar const u32 *versions, unsigned int count) 19677813dd6fSViresh Kumar { 196825419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 196925419de1SViresh Kumar if (opp_table->supported_hw) 1970*89f03984SViresh Kumar return 0; 19717813dd6fSViresh Kumar 19727813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 19737813dd6fSViresh Kumar GFP_KERNEL); 1974*89f03984SViresh Kumar if (!opp_table->supported_hw) 1975*89f03984SViresh Kumar return -ENOMEM; 19767813dd6fSViresh Kumar 19777813dd6fSViresh Kumar opp_table->supported_hw_count = count; 19787813dd6fSViresh Kumar 1979*89f03984SViresh Kumar return 0; 19807813dd6fSViresh Kumar } 19817813dd6fSViresh Kumar 19827813dd6fSViresh Kumar /** 1983*89f03984SViresh Kumar * _opp_put_supported_hw() - Releases resources blocked for supported hw 1984*89f03984SViresh Kumar * @opp_table: OPP table returned by _opp_set_supported_hw(). 19857813dd6fSViresh Kumar * 19867813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 1987*89f03984SViresh Kumar * _opp_set_supported_hw(). Until this is called, the opp_table structure 19887813dd6fSViresh Kumar * will not be freed. 19897813dd6fSViresh Kumar */ 1990*89f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table) 19917813dd6fSViresh Kumar { 1992*89f03984SViresh Kumar if (opp_table->supported_hw) { 19937813dd6fSViresh Kumar kfree(opp_table->supported_hw); 19947813dd6fSViresh Kumar opp_table->supported_hw = NULL; 19957813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 19967813dd6fSViresh Kumar } 19979c4f220fSYangtao Li } 19989c4f220fSYangtao Li 19999c4f220fSYangtao Li /** 20007813dd6fSViresh Kumar * dev_pm_opp_set_prop_name() - Set prop-extn name 20017813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 20027813dd6fSViresh Kumar * @name: name to postfix to properties. 20037813dd6fSViresh Kumar * 20047813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 20057813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 20067813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 20077813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 20087813dd6fSViresh Kumar */ 20097813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 20107813dd6fSViresh Kumar { 20117813dd6fSViresh Kumar struct opp_table *opp_table; 20127813dd6fSViresh Kumar 201332439ac7SViresh Kumar opp_table = _add_opp_table(dev, false); 2014dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2015dd461cd9SStephan Gerhold return opp_table; 20167813dd6fSViresh Kumar 20177813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 20187813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 20197813dd6fSViresh Kumar 2020878ec1a9SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 2021878ec1a9SViresh Kumar if (opp_table->prop_name) 2022878ec1a9SViresh Kumar return opp_table; 20237813dd6fSViresh Kumar 20247813dd6fSViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 20257813dd6fSViresh Kumar if (!opp_table->prop_name) { 2026878ec1a9SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2027878ec1a9SViresh Kumar return ERR_PTR(-ENOMEM); 20287813dd6fSViresh Kumar } 20297813dd6fSViresh Kumar 20307813dd6fSViresh Kumar return opp_table; 20317813dd6fSViresh Kumar } 20327813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); 20337813dd6fSViresh Kumar 20347813dd6fSViresh Kumar /** 20357813dd6fSViresh Kumar * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name 20367813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). 20377813dd6fSViresh Kumar * 20387813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 20397813dd6fSViresh Kumar * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure 20407813dd6fSViresh Kumar * will not be freed. 20417813dd6fSViresh Kumar */ 20427813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table) 20437813dd6fSViresh Kumar { 2044c7bf8758SViresh Kumar if (unlikely(!opp_table)) 2045c7bf8758SViresh Kumar return; 2046c7bf8758SViresh Kumar 20477813dd6fSViresh Kumar kfree(opp_table->prop_name); 20487813dd6fSViresh Kumar opp_table->prop_name = NULL; 20497813dd6fSViresh Kumar 20507813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 20517813dd6fSViresh Kumar } 20527813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); 20537813dd6fSViresh Kumar 20547813dd6fSViresh Kumar /** 2055b0ec0942SViresh Kumar * _opp_set_regulators() - Set regulator names for the device 20567813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 20577813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 20587813dd6fSViresh Kumar * @count: Number of regulators. 20597813dd6fSViresh Kumar * 20607813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 20617813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 20627813dd6fSViresh Kumar * well. 20637813dd6fSViresh Kumar * 20647813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 20657813dd6fSViresh Kumar */ 2066b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev, 206787686cc8SViresh Kumar const char * const names[]) 20687813dd6fSViresh Kumar { 206938bb3439SViresh Kumar struct dev_pm_opp_supply *supplies; 207087686cc8SViresh Kumar const char * const *temp = names; 20717813dd6fSViresh Kumar struct regulator *reg; 207287686cc8SViresh Kumar int count = 0, ret, i; 207387686cc8SViresh Kumar 207487686cc8SViresh Kumar /* Count number of regulators */ 207587686cc8SViresh Kumar while (*temp++) 207687686cc8SViresh Kumar count++; 207787686cc8SViresh Kumar 207887686cc8SViresh Kumar if (!count) 2079b0ec0942SViresh Kumar return -EINVAL; 20807813dd6fSViresh Kumar 2081779b783cSViresh Kumar /* Another CPU that shares the OPP table has set the regulators ? */ 2082779b783cSViresh Kumar if (opp_table->regulators) 2083b0ec0942SViresh Kumar return 0; 20847813dd6fSViresh Kumar 20857813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 20867813dd6fSViresh Kumar sizeof(*opp_table->regulators), 20877813dd6fSViresh Kumar GFP_KERNEL); 2088b0ec0942SViresh Kumar if (!opp_table->regulators) 2089b0ec0942SViresh Kumar return -ENOMEM; 20907813dd6fSViresh Kumar 20917813dd6fSViresh Kumar for (i = 0; i < count; i++) { 20927813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 20937813dd6fSViresh Kumar if (IS_ERR(reg)) { 2094543256d2SKrzysztof Kozlowski ret = dev_err_probe(dev, PTR_ERR(reg), 2095543256d2SKrzysztof Kozlowski "%s: no regulator (%s) found\n", 2096543256d2SKrzysztof Kozlowski __func__, names[i]); 20977813dd6fSViresh Kumar goto free_regulators; 20987813dd6fSViresh Kumar } 20997813dd6fSViresh Kumar 21007813dd6fSViresh Kumar opp_table->regulators[i] = reg; 21017813dd6fSViresh Kumar } 21027813dd6fSViresh Kumar 21037813dd6fSViresh Kumar opp_table->regulator_count = count; 21047813dd6fSViresh Kumar 210538bb3439SViresh Kumar supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL); 210638bb3439SViresh Kumar if (!supplies) { 210738bb3439SViresh Kumar ret = -ENOMEM; 21087813dd6fSViresh Kumar goto free_regulators; 210938bb3439SViresh Kumar } 211038bb3439SViresh Kumar 211138bb3439SViresh Kumar mutex_lock(&opp_table->lock); 211238bb3439SViresh Kumar opp_table->sod_supplies = supplies; 211338bb3439SViresh Kumar if (opp_table->set_opp_data) { 211438bb3439SViresh Kumar opp_table->set_opp_data->old_opp.supplies = supplies; 211538bb3439SViresh Kumar opp_table->set_opp_data->new_opp.supplies = supplies + count; 211638bb3439SViresh Kumar } 211738bb3439SViresh Kumar mutex_unlock(&opp_table->lock); 21187813dd6fSViresh Kumar 2119b0ec0942SViresh Kumar return 0; 21207813dd6fSViresh Kumar 21217813dd6fSViresh Kumar free_regulators: 212224957db1SMarek Szyprowski while (i != 0) 212324957db1SMarek Szyprowski regulator_put(opp_table->regulators[--i]); 21247813dd6fSViresh Kumar 21257813dd6fSViresh Kumar kfree(opp_table->regulators); 21267813dd6fSViresh Kumar opp_table->regulators = NULL; 212746f48acaSViresh Kumar opp_table->regulator_count = -1; 21287813dd6fSViresh Kumar 2129b0ec0942SViresh Kumar return ret; 21307813dd6fSViresh Kumar } 21317813dd6fSViresh Kumar 21327813dd6fSViresh Kumar /** 2133b0ec0942SViresh Kumar * _opp_put_regulators() - Releases resources blocked for regulator 2134b0ec0942SViresh Kumar * @opp_table: OPP table returned from _opp_set_regulators(). 21357813dd6fSViresh Kumar */ 2136b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table) 21377813dd6fSViresh Kumar { 21387813dd6fSViresh Kumar int i; 21397813dd6fSViresh Kumar 2140779b783cSViresh Kumar if (!opp_table->regulators) 2141b0ec0942SViresh Kumar return; 21427813dd6fSViresh Kumar 214372f80ce4SViresh Kumar if (opp_table->enabled) { 21448d45719cSKamil Konieczny for (i = opp_table->regulator_count - 1; i >= 0; i--) 21458d45719cSKamil Konieczny regulator_disable(opp_table->regulators[i]); 21468d45719cSKamil Konieczny } 21478d45719cSKamil Konieczny 214824957db1SMarek Szyprowski for (i = opp_table->regulator_count - 1; i >= 0; i--) 21497813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 21507813dd6fSViresh Kumar 215138bb3439SViresh Kumar mutex_lock(&opp_table->lock); 215238bb3439SViresh Kumar if (opp_table->set_opp_data) { 215338bb3439SViresh Kumar opp_table->set_opp_data->old_opp.supplies = NULL; 215438bb3439SViresh Kumar opp_table->set_opp_data->new_opp.supplies = NULL; 215538bb3439SViresh Kumar } 215638bb3439SViresh Kumar 215738bb3439SViresh Kumar kfree(opp_table->sod_supplies); 215838bb3439SViresh Kumar opp_table->sod_supplies = NULL; 215938bb3439SViresh Kumar mutex_unlock(&opp_table->lock); 21607813dd6fSViresh Kumar 21617813dd6fSViresh Kumar kfree(opp_table->regulators); 21627813dd6fSViresh Kumar opp_table->regulators = NULL; 216346f48acaSViresh Kumar opp_table->regulator_count = -1; 21647813dd6fSViresh Kumar } 216532aee78bSYangtao Li 21667813dd6fSViresh Kumar /** 21677813dd6fSViresh Kumar * dev_pm_opp_set_clkname() - Set clk name for the device 21687813dd6fSViresh Kumar * @dev: Device for which clk name is being set. 21697813dd6fSViresh Kumar * @name: Clk name. 21707813dd6fSViresh Kumar * 21717813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to get pointer to the 21727813dd6fSViresh Kumar * clock for the device. Simple cases work fine without using this routine (i.e. 21737813dd6fSViresh Kumar * by passing connection-id as NULL), but for a device with multiple clocks 21747813dd6fSViresh Kumar * available, the OPP core needs to know the exact name of the clk to use. 21757813dd6fSViresh Kumar * 21767813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 21777813dd6fSViresh Kumar */ 21787813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) 21797813dd6fSViresh Kumar { 21807813dd6fSViresh Kumar struct opp_table *opp_table; 21817813dd6fSViresh Kumar int ret; 21827813dd6fSViresh Kumar 218332439ac7SViresh Kumar opp_table = _add_opp_table(dev, false); 2184dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2185dd461cd9SStephan Gerhold return opp_table; 21867813dd6fSViresh Kumar 21877813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 21887813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 21897813dd6fSViresh Kumar ret = -EBUSY; 21907813dd6fSViresh Kumar goto err; 21917813dd6fSViresh Kumar } 21927813dd6fSViresh Kumar 21930a43452bSViresh Kumar /* Another CPU that shares the OPP table has set the clkname ? */ 21940a43452bSViresh Kumar if (opp_table->clk_configured) 21950a43452bSViresh Kumar return opp_table; 21960a43452bSViresh Kumar 219732439ac7SViresh Kumar /* clk shouldn't be initialized at this point */ 219832439ac7SViresh Kumar if (WARN_ON(opp_table->clk)) { 219932439ac7SViresh Kumar ret = -EBUSY; 220032439ac7SViresh Kumar goto err; 220132439ac7SViresh Kumar } 22027813dd6fSViresh Kumar 22037813dd6fSViresh Kumar /* Find clk for the device */ 22047813dd6fSViresh Kumar opp_table->clk = clk_get(dev, name); 22057813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 2206543256d2SKrzysztof Kozlowski ret = dev_err_probe(dev, PTR_ERR(opp_table->clk), 2207543256d2SKrzysztof Kozlowski "%s: Couldn't find clock\n", __func__); 22087813dd6fSViresh Kumar goto err; 22097813dd6fSViresh Kumar } 22107813dd6fSViresh Kumar 22110a43452bSViresh Kumar opp_table->clk_configured = true; 22120a43452bSViresh Kumar 22137813dd6fSViresh Kumar return opp_table; 22147813dd6fSViresh Kumar 22157813dd6fSViresh Kumar err: 22167813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 22177813dd6fSViresh Kumar 22187813dd6fSViresh Kumar return ERR_PTR(ret); 22197813dd6fSViresh Kumar } 22207813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); 22217813dd6fSViresh Kumar 22227813dd6fSViresh Kumar /** 22237813dd6fSViresh Kumar * dev_pm_opp_put_clkname() - Releases resources blocked for clk. 22247813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). 22257813dd6fSViresh Kumar */ 22267813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table) 22277813dd6fSViresh Kumar { 2228c7bf8758SViresh Kumar if (unlikely(!opp_table)) 2229c7bf8758SViresh Kumar return; 2230c7bf8758SViresh Kumar 22317813dd6fSViresh Kumar clk_put(opp_table->clk); 22327813dd6fSViresh Kumar opp_table->clk = ERR_PTR(-EINVAL); 22330a43452bSViresh Kumar opp_table->clk_configured = false; 22347813dd6fSViresh Kumar 22357813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 22367813dd6fSViresh Kumar } 22377813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); 22387813dd6fSViresh Kumar 2239a74f681cSYangtao Li static void devm_pm_opp_clkname_release(void *data) 2240a74f681cSYangtao Li { 2241a74f681cSYangtao Li dev_pm_opp_put_clkname(data); 2242a74f681cSYangtao Li } 2243a74f681cSYangtao Li 2244a74f681cSYangtao Li /** 2245a74f681cSYangtao Li * devm_pm_opp_set_clkname() - Set clk name for the device 2246a74f681cSYangtao Li * @dev: Device for which clk name is being set. 2247a74f681cSYangtao Li * @name: Clk name. 2248a74f681cSYangtao Li * 2249a74f681cSYangtao Li * This is a resource-managed variant of dev_pm_opp_set_clkname(). 2250a74f681cSYangtao Li * 2251a74f681cSYangtao Li * Return: 0 on success and errorno otherwise. 2252a74f681cSYangtao Li */ 2253a74f681cSYangtao Li int devm_pm_opp_set_clkname(struct device *dev, const char *name) 2254a74f681cSYangtao Li { 2255a74f681cSYangtao Li struct opp_table *opp_table; 2256a74f681cSYangtao Li 2257a74f681cSYangtao Li opp_table = dev_pm_opp_set_clkname(dev, name); 2258a74f681cSYangtao Li if (IS_ERR(opp_table)) 2259a74f681cSYangtao Li return PTR_ERR(opp_table); 2260a74f681cSYangtao Li 2261a74f681cSYangtao Li return devm_add_action_or_reset(dev, devm_pm_opp_clkname_release, 2262a74f681cSYangtao Li opp_table); 2263a74f681cSYangtao Li } 2264a74f681cSYangtao Li EXPORT_SYMBOL_GPL(devm_pm_opp_set_clkname); 2265a74f681cSYangtao Li 22667813dd6fSViresh Kumar /** 22677813dd6fSViresh Kumar * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper 22687813dd6fSViresh Kumar * @dev: Device for which the helper is getting registered. 22697813dd6fSViresh Kumar * @set_opp: Custom set OPP helper. 22707813dd6fSViresh Kumar * 22717813dd6fSViresh Kumar * This is useful to support complex platforms (like platforms with multiple 22727813dd6fSViresh Kumar * regulators per device), instead of the generic OPP set rate helper. 22737813dd6fSViresh Kumar * 22747813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 22757813dd6fSViresh Kumar */ 22767813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 22777813dd6fSViresh Kumar int (*set_opp)(struct dev_pm_set_opp_data *data)) 22787813dd6fSViresh Kumar { 227938bb3439SViresh Kumar struct dev_pm_set_opp_data *data; 22807813dd6fSViresh Kumar struct opp_table *opp_table; 22817813dd6fSViresh Kumar 22827813dd6fSViresh Kumar if (!set_opp) 22837813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 22847813dd6fSViresh Kumar 228532439ac7SViresh Kumar opp_table = _add_opp_table(dev, false); 228647efcbcbSViresh Kumar if (IS_ERR(opp_table)) 2287dd461cd9SStephan Gerhold return opp_table; 22887813dd6fSViresh Kumar 22897813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 22907813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 22915019acc6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 22925019acc6SViresh Kumar return ERR_PTR(-EBUSY); 22937813dd6fSViresh Kumar } 22947813dd6fSViresh Kumar 22955019acc6SViresh Kumar /* Another CPU that shares the OPP table has set the helper ? */ 229638bb3439SViresh Kumar if (opp_table->set_opp) 229738bb3439SViresh Kumar return opp_table; 229838bb3439SViresh Kumar 229938bb3439SViresh Kumar data = kzalloc(sizeof(*data), GFP_KERNEL); 230038bb3439SViresh Kumar if (!data) 230138bb3439SViresh Kumar return ERR_PTR(-ENOMEM); 230238bb3439SViresh Kumar 230338bb3439SViresh Kumar mutex_lock(&opp_table->lock); 230438bb3439SViresh Kumar opp_table->set_opp_data = data; 230538bb3439SViresh Kumar if (opp_table->sod_supplies) { 230638bb3439SViresh Kumar data->old_opp.supplies = opp_table->sod_supplies; 230738bb3439SViresh Kumar data->new_opp.supplies = opp_table->sod_supplies + 230838bb3439SViresh Kumar opp_table->regulator_count; 230938bb3439SViresh Kumar } 231038bb3439SViresh Kumar mutex_unlock(&opp_table->lock); 231138bb3439SViresh Kumar 23127813dd6fSViresh Kumar opp_table->set_opp = set_opp; 23137813dd6fSViresh Kumar 23147813dd6fSViresh Kumar return opp_table; 23157813dd6fSViresh Kumar } 23167813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); 23177813dd6fSViresh Kumar 23187813dd6fSViresh Kumar /** 2319604a7aebSViresh Kumar * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for 23207813dd6fSViresh Kumar * set_opp helper 23217813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). 23227813dd6fSViresh Kumar * 23237813dd6fSViresh Kumar * Release resources blocked for platform specific set_opp helper. 23247813dd6fSViresh Kumar */ 2325604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) 23267813dd6fSViresh Kumar { 2327c7bf8758SViresh Kumar if (unlikely(!opp_table)) 2328c7bf8758SViresh Kumar return; 2329c7bf8758SViresh Kumar 23307813dd6fSViresh Kumar opp_table->set_opp = NULL; 233138bb3439SViresh Kumar 233238bb3439SViresh Kumar mutex_lock(&opp_table->lock); 233338bb3439SViresh Kumar kfree(opp_table->set_opp_data); 233438bb3439SViresh Kumar opp_table->set_opp_data = NULL; 233538bb3439SViresh Kumar mutex_unlock(&opp_table->lock); 233638bb3439SViresh Kumar 23377813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 23387813dd6fSViresh Kumar } 2339604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); 23407813dd6fSViresh Kumar 2341a3c47af6SDmitry Osipenko static void devm_pm_opp_unregister_set_opp_helper(void *data) 2342a3c47af6SDmitry Osipenko { 2343a3c47af6SDmitry Osipenko dev_pm_opp_unregister_set_opp_helper(data); 2344a3c47af6SDmitry Osipenko } 2345a3c47af6SDmitry Osipenko 2346a3c47af6SDmitry Osipenko /** 2347a3c47af6SDmitry Osipenko * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper 2348a3c47af6SDmitry Osipenko * @dev: Device for which the helper is getting registered. 2349a3c47af6SDmitry Osipenko * @set_opp: Custom set OPP helper. 2350a3c47af6SDmitry Osipenko * 2351a3c47af6SDmitry Osipenko * This is a resource-managed version of dev_pm_opp_register_set_opp_helper(). 2352a3c47af6SDmitry Osipenko * 2353c41c8a34SDmitry Osipenko * Return: 0 on success and errorno otherwise. 2354a3c47af6SDmitry Osipenko */ 2355c41c8a34SDmitry Osipenko int devm_pm_opp_register_set_opp_helper(struct device *dev, 2356a3c47af6SDmitry Osipenko int (*set_opp)(struct dev_pm_set_opp_data *data)) 2357a3c47af6SDmitry Osipenko { 2358a3c47af6SDmitry Osipenko struct opp_table *opp_table; 2359a3c47af6SDmitry Osipenko 2360a3c47af6SDmitry Osipenko opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp); 2361a3c47af6SDmitry Osipenko if (IS_ERR(opp_table)) 2362c41c8a34SDmitry Osipenko return PTR_ERR(opp_table); 2363a3c47af6SDmitry Osipenko 2364c41c8a34SDmitry Osipenko return devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper, 2365a3c47af6SDmitry Osipenko opp_table); 2366a3c47af6SDmitry Osipenko } 2367a3c47af6SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper); 2368a3c47af6SDmitry Osipenko 23696319aee1SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table) 23706319aee1SViresh Kumar { 23716319aee1SViresh Kumar int index; 23726319aee1SViresh Kumar 2373cb60e960SViresh Kumar if (!opp_table->genpd_virt_devs) 2374cb60e960SViresh Kumar return; 2375cb60e960SViresh Kumar 23766319aee1SViresh Kumar for (index = 0; index < opp_table->required_opp_count; index++) { 23776319aee1SViresh Kumar if (!opp_table->genpd_virt_devs[index]) 23786319aee1SViresh Kumar continue; 23796319aee1SViresh Kumar 23806319aee1SViresh Kumar dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 23816319aee1SViresh Kumar opp_table->genpd_virt_devs[index] = NULL; 23826319aee1SViresh Kumar } 2383c0ab9e08SViresh Kumar 2384c0ab9e08SViresh Kumar kfree(opp_table->genpd_virt_devs); 2385c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = NULL; 23866319aee1SViresh Kumar } 23876319aee1SViresh Kumar 23887813dd6fSViresh Kumar /** 23896319aee1SViresh Kumar * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 23906319aee1SViresh Kumar * @dev: Consumer device for which the genpd is getting attached. 23916319aee1SViresh Kumar * @names: Null terminated array of pointers containing names of genpd to attach. 239217a8f868SViresh Kumar * @virt_devs: Pointer to return the array of virtual devices. 23934f018bc0SViresh Kumar * 23944f018bc0SViresh Kumar * Multiple generic power domains for a device are supported with the help of 23954f018bc0SViresh Kumar * virtual genpd devices, which are created for each consumer device - genpd 23964f018bc0SViresh Kumar * pair. These are the device structures which are attached to the power domain 23974f018bc0SViresh Kumar * and are required by the OPP core to set the performance state of the genpd. 23986319aee1SViresh Kumar * The same API also works for the case where single genpd is available and so 23996319aee1SViresh Kumar * we don't need to support that separately. 24004f018bc0SViresh Kumar * 24014f018bc0SViresh Kumar * This helper will normally be called by the consumer driver of the device 24026319aee1SViresh Kumar * "dev", as only that has details of the genpd names. 24034f018bc0SViresh Kumar * 24046319aee1SViresh Kumar * This helper needs to be called once with a list of all genpd to attach. 24056319aee1SViresh Kumar * Otherwise the original device structure will be used instead by the OPP core. 2406baea35e4SViresh Kumar * 2407baea35e4SViresh Kumar * The order of entries in the names array must match the order in which 2408baea35e4SViresh Kumar * "required-opps" are added in DT. 24094f018bc0SViresh Kumar */ 241017a8f868SViresh Kumar struct opp_table *dev_pm_opp_attach_genpd(struct device *dev, 24113734b9f2SDmitry Osipenko const char * const *names, struct device ***virt_devs) 24124f018bc0SViresh Kumar { 24134f018bc0SViresh Kumar struct opp_table *opp_table; 24146319aee1SViresh Kumar struct device *virt_dev; 2415baea35e4SViresh Kumar int index = 0, ret = -EINVAL; 24163734b9f2SDmitry Osipenko const char * const *name = names; 24174f018bc0SViresh Kumar 241832439ac7SViresh Kumar opp_table = _add_opp_table(dev, false); 2419dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2420dd461cd9SStephan Gerhold return opp_table; 24214f018bc0SViresh Kumar 2422cb60e960SViresh Kumar if (opp_table->genpd_virt_devs) 2423cb60e960SViresh Kumar return opp_table; 24244f018bc0SViresh Kumar 24256319aee1SViresh Kumar /* 24266319aee1SViresh Kumar * If the genpd's OPP table isn't already initialized, parsing of the 24276319aee1SViresh Kumar * required-opps fail for dev. We should retry this after genpd's OPP 24286319aee1SViresh Kumar * table is added. 24296319aee1SViresh Kumar */ 24306319aee1SViresh Kumar if (!opp_table->required_opp_count) { 24316319aee1SViresh Kumar ret = -EPROBE_DEFER; 24326319aee1SViresh Kumar goto put_table; 24336319aee1SViresh Kumar } 24346319aee1SViresh Kumar 24354f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 24364f018bc0SViresh Kumar 2437c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, 2438c0ab9e08SViresh Kumar sizeof(*opp_table->genpd_virt_devs), 2439c0ab9e08SViresh Kumar GFP_KERNEL); 2440c0ab9e08SViresh Kumar if (!opp_table->genpd_virt_devs) 2441c0ab9e08SViresh Kumar goto unlock; 24424f018bc0SViresh Kumar 24436319aee1SViresh Kumar while (*name) { 24446319aee1SViresh Kumar if (index >= opp_table->required_opp_count) { 24456319aee1SViresh Kumar dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 24466319aee1SViresh Kumar *name, opp_table->required_opp_count, index); 24476319aee1SViresh Kumar goto err; 24486319aee1SViresh Kumar } 24494f018bc0SViresh Kumar 24506319aee1SViresh Kumar virt_dev = dev_pm_domain_attach_by_name(dev, *name); 24514ea9496cSTang Bin if (IS_ERR_OR_NULL(virt_dev)) { 24524ea9496cSTang Bin ret = PTR_ERR(virt_dev) ? : -ENODEV; 24536319aee1SViresh Kumar dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 24546319aee1SViresh Kumar goto err; 24554f018bc0SViresh Kumar } 24564f018bc0SViresh Kumar 24574f018bc0SViresh Kumar opp_table->genpd_virt_devs[index] = virt_dev; 2458baea35e4SViresh Kumar index++; 24596319aee1SViresh Kumar name++; 24606319aee1SViresh Kumar } 24616319aee1SViresh Kumar 246217a8f868SViresh Kumar if (virt_devs) 246317a8f868SViresh Kumar *virt_devs = opp_table->genpd_virt_devs; 24644f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 24654f018bc0SViresh Kumar 24664f018bc0SViresh Kumar return opp_table; 24676319aee1SViresh Kumar 24686319aee1SViresh Kumar err: 24696319aee1SViresh Kumar _opp_detach_genpd(opp_table); 2470c0ab9e08SViresh Kumar unlock: 24716319aee1SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 24726319aee1SViresh Kumar 24736319aee1SViresh Kumar put_table: 24746319aee1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 24756319aee1SViresh Kumar 24766319aee1SViresh Kumar return ERR_PTR(ret); 24774f018bc0SViresh Kumar } 24786319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd); 24794f018bc0SViresh Kumar 24804f018bc0SViresh Kumar /** 24816319aee1SViresh Kumar * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device. 24826319aee1SViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_attach_genpd(). 24834f018bc0SViresh Kumar * 24846319aee1SViresh Kumar * This detaches the genpd(s), resets the virtual device pointers, and puts the 24856319aee1SViresh Kumar * OPP table. 24864f018bc0SViresh Kumar */ 24876319aee1SViresh Kumar void dev_pm_opp_detach_genpd(struct opp_table *opp_table) 24884f018bc0SViresh Kumar { 2489c7bf8758SViresh Kumar if (unlikely(!opp_table)) 2490c7bf8758SViresh Kumar return; 2491c7bf8758SViresh Kumar 24924f018bc0SViresh Kumar /* 24934f018bc0SViresh Kumar * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 24944f018bc0SViresh Kumar * used in parallel. 24954f018bc0SViresh Kumar */ 24964f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 24976319aee1SViresh Kumar _opp_detach_genpd(opp_table); 24984f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 24994f018bc0SViresh Kumar 25006319aee1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 25014f018bc0SViresh Kumar } 25026319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd); 25034f018bc0SViresh Kumar 2504b4b9e223SDmitry Osipenko static void devm_pm_opp_detach_genpd(void *data) 2505b4b9e223SDmitry Osipenko { 2506b4b9e223SDmitry Osipenko dev_pm_opp_detach_genpd(data); 2507b4b9e223SDmitry Osipenko } 2508b4b9e223SDmitry Osipenko 2509b4b9e223SDmitry Osipenko /** 2510b4b9e223SDmitry Osipenko * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual 2511b4b9e223SDmitry Osipenko * device pointer 2512b4b9e223SDmitry Osipenko * @dev: Consumer device for which the genpd is getting attached. 2513b4b9e223SDmitry Osipenko * @names: Null terminated array of pointers containing names of genpd to attach. 2514b4b9e223SDmitry Osipenko * @virt_devs: Pointer to return the array of virtual devices. 2515b4b9e223SDmitry Osipenko * 2516b4b9e223SDmitry Osipenko * This is a resource-managed version of dev_pm_opp_attach_genpd(). 2517b4b9e223SDmitry Osipenko * 25189edf48a4SDmitry Osipenko * Return: 0 on success and errorno otherwise. 2519b4b9e223SDmitry Osipenko */ 25203734b9f2SDmitry Osipenko int devm_pm_opp_attach_genpd(struct device *dev, const char * const *names, 2521b4b9e223SDmitry Osipenko struct device ***virt_devs) 2522b4b9e223SDmitry Osipenko { 2523b4b9e223SDmitry Osipenko struct opp_table *opp_table; 2524b4b9e223SDmitry Osipenko 2525b4b9e223SDmitry Osipenko opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs); 2526b4b9e223SDmitry Osipenko if (IS_ERR(opp_table)) 25279edf48a4SDmitry Osipenko return PTR_ERR(opp_table); 2528b4b9e223SDmitry Osipenko 25299edf48a4SDmitry Osipenko return devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd, 2530b4b9e223SDmitry Osipenko opp_table); 2531b4b9e223SDmitry Osipenko } 2532b4b9e223SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd); 2533b4b9e223SDmitry Osipenko 253411b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data) 253511b9b663SViresh Kumar { 253611b9b663SViresh Kumar if (data->flags & OPP_CONFIG_GENPD) 253711b9b663SViresh Kumar dev_pm_opp_detach_genpd(data->opp_table); 253811b9b663SViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR) 2539b0ec0942SViresh Kumar _opp_put_regulators(data->opp_table); 254011b9b663SViresh Kumar if (data->flags & OPP_CONFIG_SUPPORTED_HW) 2541*89f03984SViresh Kumar _opp_put_supported_hw(data->opp_table); 254211b9b663SViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR_HELPER) 254311b9b663SViresh Kumar dev_pm_opp_unregister_set_opp_helper(data->opp_table); 254411b9b663SViresh Kumar if (data->flags & OPP_CONFIG_PROP_NAME) 254511b9b663SViresh Kumar dev_pm_opp_put_prop_name(data->opp_table); 254611b9b663SViresh Kumar if (data->flags & OPP_CONFIG_CLK) 254711b9b663SViresh Kumar dev_pm_opp_put_clkname(data->opp_table); 254811b9b663SViresh Kumar 254911b9b663SViresh Kumar dev_pm_opp_put_opp_table(data->opp_table); 255011b9b663SViresh Kumar kfree(data); 255111b9b663SViresh Kumar } 255211b9b663SViresh Kumar 255311b9b663SViresh Kumar /** 255411b9b663SViresh Kumar * dev_pm_opp_set_config() - Set OPP configuration for the device. 255511b9b663SViresh Kumar * @dev: Device for which configuration is being set. 255611b9b663SViresh Kumar * @config: OPP configuration. 255711b9b663SViresh Kumar * 255811b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 255911b9b663SViresh Kumar * 256011b9b663SViresh Kumar * This must be called before any OPPs are initialized for the device. This may 256111b9b663SViresh Kumar * be called multiple times for the same OPP table, for example once for each 256211b9b663SViresh Kumar * CPU that share the same table. This must be balanced by the same number of 256311b9b663SViresh Kumar * calls to dev_pm_opp_clear_config() in order to free the OPP table properly. 256411b9b663SViresh Kumar * 256511b9b663SViresh Kumar * This returns a token to the caller, which must be passed to 256611b9b663SViresh Kumar * dev_pm_opp_clear_config() to free the resources later. The value of the 256711b9b663SViresh Kumar * returned token will be >= 1 for success and negative for errors. The minimum 256811b9b663SViresh Kumar * value of 1 is chosen here to make it easy for callers to manage the resource. 256911b9b663SViresh Kumar */ 257011b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 257111b9b663SViresh Kumar { 257211b9b663SViresh Kumar struct opp_table *opp_table, *err; 257311b9b663SViresh Kumar struct opp_config_data *data; 257411b9b663SViresh Kumar unsigned int id; 257511b9b663SViresh Kumar int ret; 257611b9b663SViresh Kumar 257711b9b663SViresh Kumar data = kmalloc(sizeof(*data), GFP_KERNEL); 257811b9b663SViresh Kumar if (!data) 257911b9b663SViresh Kumar return -ENOMEM; 258011b9b663SViresh Kumar 258111b9b663SViresh Kumar opp_table = _add_opp_table(dev, false); 258211b9b663SViresh Kumar if (IS_ERR(opp_table)) { 258311b9b663SViresh Kumar kfree(data); 258411b9b663SViresh Kumar return PTR_ERR(opp_table); 258511b9b663SViresh Kumar } 258611b9b663SViresh Kumar 258711b9b663SViresh Kumar data->opp_table = opp_table; 258811b9b663SViresh Kumar data->flags = 0; 258911b9b663SViresh Kumar 259011b9b663SViresh Kumar /* This should be called before OPPs are initialized */ 259111b9b663SViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 259211b9b663SViresh Kumar ret = -EBUSY; 259311b9b663SViresh Kumar goto err; 259411b9b663SViresh Kumar } 259511b9b663SViresh Kumar 259611b9b663SViresh Kumar /* Configure clocks */ 259711b9b663SViresh Kumar if (config->clk_names) { 259811b9b663SViresh Kumar const char * const *temp = config->clk_names; 259911b9b663SViresh Kumar int count = 0; 260011b9b663SViresh Kumar 260111b9b663SViresh Kumar /* Count number of clks */ 260211b9b663SViresh Kumar while (*temp++) 260311b9b663SViresh Kumar count++; 260411b9b663SViresh Kumar 260511b9b663SViresh Kumar /* 260611b9b663SViresh Kumar * This is a special case where we have a single clock, whose 260711b9b663SViresh Kumar * connection id name is NULL, i.e. first two entries are NULL 260811b9b663SViresh Kumar * in the array. 260911b9b663SViresh Kumar */ 261011b9b663SViresh Kumar if (!count && !config->clk_names[1]) 261111b9b663SViresh Kumar count = 1; 261211b9b663SViresh Kumar 261311b9b663SViresh Kumar /* We support only one clock name for now */ 261411b9b663SViresh Kumar if (count != 1) { 261511b9b663SViresh Kumar ret = -EINVAL; 261611b9b663SViresh Kumar goto err; 261711b9b663SViresh Kumar } 261811b9b663SViresh Kumar 261911b9b663SViresh Kumar err = dev_pm_opp_set_clkname(dev, config->clk_names[0]); 262011b9b663SViresh Kumar if (IS_ERR(err)) { 262111b9b663SViresh Kumar ret = PTR_ERR(err); 262211b9b663SViresh Kumar goto err; 262311b9b663SViresh Kumar } 262411b9b663SViresh Kumar 262511b9b663SViresh Kumar data->flags |= OPP_CONFIG_CLK; 262611b9b663SViresh Kumar } 262711b9b663SViresh Kumar 262811b9b663SViresh Kumar /* Configure property names */ 262911b9b663SViresh Kumar if (config->prop_name) { 263011b9b663SViresh Kumar err = dev_pm_opp_set_prop_name(dev, config->prop_name); 263111b9b663SViresh Kumar if (IS_ERR(err)) { 263211b9b663SViresh Kumar ret = PTR_ERR(err); 263311b9b663SViresh Kumar goto err; 263411b9b663SViresh Kumar } 263511b9b663SViresh Kumar 263611b9b663SViresh Kumar data->flags |= OPP_CONFIG_PROP_NAME; 263711b9b663SViresh Kumar } 263811b9b663SViresh Kumar 263911b9b663SViresh Kumar /* Configure opp helper */ 264011b9b663SViresh Kumar if (config->set_opp) { 264111b9b663SViresh Kumar err = dev_pm_opp_register_set_opp_helper(dev, config->set_opp); 264211b9b663SViresh Kumar if (IS_ERR(err)) { 264311b9b663SViresh Kumar ret = PTR_ERR(err); 264411b9b663SViresh Kumar goto err; 264511b9b663SViresh Kumar } 264611b9b663SViresh Kumar 264711b9b663SViresh Kumar data->flags |= OPP_CONFIG_REGULATOR_HELPER; 264811b9b663SViresh Kumar } 264911b9b663SViresh Kumar 265011b9b663SViresh Kumar /* Configure supported hardware */ 265111b9b663SViresh Kumar if (config->supported_hw) { 2652*89f03984SViresh Kumar ret = _opp_set_supported_hw(opp_table, config->supported_hw, 265311b9b663SViresh Kumar config->supported_hw_count); 2654*89f03984SViresh Kumar if (ret) 265511b9b663SViresh Kumar goto err; 265611b9b663SViresh Kumar 265711b9b663SViresh Kumar data->flags |= OPP_CONFIG_SUPPORTED_HW; 265811b9b663SViresh Kumar } 265911b9b663SViresh Kumar 266011b9b663SViresh Kumar /* Configure supplies */ 266111b9b663SViresh Kumar if (config->regulator_names) { 2662b0ec0942SViresh Kumar ret = _opp_set_regulators(opp_table, dev, 2663b0ec0942SViresh Kumar config->regulator_names); 2664b0ec0942SViresh Kumar if (ret) 266511b9b663SViresh Kumar goto err; 266611b9b663SViresh Kumar 266711b9b663SViresh Kumar data->flags |= OPP_CONFIG_REGULATOR; 266811b9b663SViresh Kumar } 266911b9b663SViresh Kumar 267011b9b663SViresh Kumar /* Attach genpds */ 267111b9b663SViresh Kumar if (config->genpd_names) { 267211b9b663SViresh Kumar err = dev_pm_opp_attach_genpd(dev, config->genpd_names, 267311b9b663SViresh Kumar config->virt_devs); 267411b9b663SViresh Kumar if (IS_ERR(err)) { 267511b9b663SViresh Kumar ret = PTR_ERR(err); 267611b9b663SViresh Kumar goto err; 267711b9b663SViresh Kumar } 267811b9b663SViresh Kumar 267911b9b663SViresh Kumar data->flags |= OPP_CONFIG_GENPD; 268011b9b663SViresh Kumar } 268111b9b663SViresh Kumar 268211b9b663SViresh Kumar ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX), 268311b9b663SViresh Kumar GFP_KERNEL); 268411b9b663SViresh Kumar if (ret) 268511b9b663SViresh Kumar goto err; 268611b9b663SViresh Kumar 268711b9b663SViresh Kumar return id; 268811b9b663SViresh Kumar 268911b9b663SViresh Kumar err: 269011b9b663SViresh Kumar _opp_clear_config(data); 269111b9b663SViresh Kumar return ret; 269211b9b663SViresh Kumar } 269311b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config); 269411b9b663SViresh Kumar 269511b9b663SViresh Kumar /** 269611b9b663SViresh Kumar * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration. 269711b9b663SViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_config(). 269811b9b663SViresh Kumar * 269911b9b663SViresh Kumar * This allows all device OPP configurations to be cleared at once. This must be 270011b9b663SViresh Kumar * called once for each call made to dev_pm_opp_set_config(), in order to free 270111b9b663SViresh Kumar * the OPPs properly. 270211b9b663SViresh Kumar * 270311b9b663SViresh Kumar * Currently the first call itself ends up freeing all the OPP configurations, 270411b9b663SViresh Kumar * while the later ones only drop the OPP table reference. This works well for 270511b9b663SViresh Kumar * now as we would never want to use an half initialized OPP table and want to 270611b9b663SViresh Kumar * remove the configurations together. 270711b9b663SViresh Kumar */ 270811b9b663SViresh Kumar void dev_pm_opp_clear_config(int token) 270911b9b663SViresh Kumar { 271011b9b663SViresh Kumar struct opp_config_data *data; 271111b9b663SViresh Kumar 271211b9b663SViresh Kumar /* 271311b9b663SViresh Kumar * This lets the callers call this unconditionally and keep their code 271411b9b663SViresh Kumar * simple. 271511b9b663SViresh Kumar */ 271611b9b663SViresh Kumar if (unlikely(token <= 0)) 271711b9b663SViresh Kumar return; 271811b9b663SViresh Kumar 271911b9b663SViresh Kumar data = xa_erase(&opp_configs, token); 272011b9b663SViresh Kumar if (WARN_ON(!data)) 272111b9b663SViresh Kumar return; 272211b9b663SViresh Kumar 272311b9b663SViresh Kumar _opp_clear_config(data); 272411b9b663SViresh Kumar } 272511b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config); 272611b9b663SViresh Kumar 272711b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token) 272811b9b663SViresh Kumar { 272911b9b663SViresh Kumar dev_pm_opp_clear_config((unsigned long)token); 273011b9b663SViresh Kumar } 273111b9b663SViresh Kumar 273211b9b663SViresh Kumar /** 273311b9b663SViresh Kumar * devm_pm_opp_set_config() - Set OPP configuration for the device. 273411b9b663SViresh Kumar * @dev: Device for which configuration is being set. 273511b9b663SViresh Kumar * @config: OPP configuration. 273611b9b663SViresh Kumar * 273711b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 273811b9b663SViresh Kumar * This is a resource-managed variant of dev_pm_opp_set_config(). 273911b9b663SViresh Kumar * 274011b9b663SViresh Kumar * Return: 0 on success and errorno otherwise. 274111b9b663SViresh Kumar */ 274211b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 274311b9b663SViresh Kumar { 274411b9b663SViresh Kumar int token = dev_pm_opp_set_config(dev, config); 274511b9b663SViresh Kumar 274611b9b663SViresh Kumar if (token < 0) 274711b9b663SViresh Kumar return token; 274811b9b663SViresh Kumar 274911b9b663SViresh Kumar return devm_add_action_or_reset(dev, devm_pm_opp_config_release, 275011b9b663SViresh Kumar (void *) ((unsigned long) token)); 275111b9b663SViresh Kumar } 275211b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config); 275311b9b663SViresh Kumar 27544f018bc0SViresh Kumar /** 27557d8658efSSaravana Kannan * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP. 27567d8658efSSaravana Kannan * @src_table: OPP table which has @dst_table as one of its required OPP table. 27577d8658efSSaravana Kannan * @dst_table: Required OPP table of the @src_table. 27587d8658efSSaravana Kannan * @src_opp: OPP from the @src_table. 27597d8658efSSaravana Kannan * 27607d8658efSSaravana Kannan * This function returns the OPP (present in @dst_table) pointed out by the 27617d8658efSSaravana Kannan * "required-opps" property of the @src_opp (present in @src_table). 27627d8658efSSaravana Kannan * 27637d8658efSSaravana Kannan * The callers are required to call dev_pm_opp_put() for the returned OPP after 27647d8658efSSaravana Kannan * use. 27657d8658efSSaravana Kannan * 27667d8658efSSaravana Kannan * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise. 27677d8658efSSaravana Kannan */ 27687d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table, 27697d8658efSSaravana Kannan struct opp_table *dst_table, 27707d8658efSSaravana Kannan struct dev_pm_opp *src_opp) 27717d8658efSSaravana Kannan { 27727d8658efSSaravana Kannan struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV); 27737d8658efSSaravana Kannan int i; 27747d8658efSSaravana Kannan 27757d8658efSSaravana Kannan if (!src_table || !dst_table || !src_opp || 27767d8658efSSaravana Kannan !src_table->required_opp_tables) 27777d8658efSSaravana Kannan return ERR_PTR(-EINVAL); 27787d8658efSSaravana Kannan 27797d8658efSSaravana Kannan /* required-opps not fully initialized yet */ 27807d8658efSSaravana Kannan if (lazy_linking_pending(src_table)) 27817d8658efSSaravana Kannan return ERR_PTR(-EBUSY); 27827d8658efSSaravana Kannan 27837d8658efSSaravana Kannan for (i = 0; i < src_table->required_opp_count; i++) { 27847d8658efSSaravana Kannan if (src_table->required_opp_tables[i] == dst_table) { 27857d8658efSSaravana Kannan mutex_lock(&src_table->lock); 27867d8658efSSaravana Kannan 27877d8658efSSaravana Kannan list_for_each_entry(opp, &src_table->opp_list, node) { 27887d8658efSSaravana Kannan if (opp == src_opp) { 27897d8658efSSaravana Kannan dest_opp = opp->required_opps[i]; 27907d8658efSSaravana Kannan dev_pm_opp_get(dest_opp); 27917d8658efSSaravana Kannan break; 27927d8658efSSaravana Kannan } 27937d8658efSSaravana Kannan } 27947d8658efSSaravana Kannan 27957d8658efSSaravana Kannan mutex_unlock(&src_table->lock); 27967d8658efSSaravana Kannan break; 27977d8658efSSaravana Kannan } 27987d8658efSSaravana Kannan } 27997d8658efSSaravana Kannan 28007d8658efSSaravana Kannan if (IS_ERR(dest_opp)) { 28017d8658efSSaravana Kannan pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, 28027d8658efSSaravana Kannan src_table, dst_table); 28037d8658efSSaravana Kannan } 28047d8658efSSaravana Kannan 28057d8658efSSaravana Kannan return dest_opp; 28067d8658efSSaravana Kannan } 28077d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp); 28087d8658efSSaravana Kannan 28097d8658efSSaravana Kannan /** 2810c8a59103SViresh Kumar * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 2811c8a59103SViresh Kumar * @src_table: OPP table which has dst_table as one of its required OPP table. 2812c8a59103SViresh Kumar * @dst_table: Required OPP table of the src_table. 2813c8a59103SViresh Kumar * @pstate: Current performance state of the src_table. 2814c8a59103SViresh Kumar * 2815c8a59103SViresh Kumar * This Returns pstate of the OPP (present in @dst_table) pointed out by the 2816c8a59103SViresh Kumar * "required-opps" property of the OPP (present in @src_table) which has 2817c8a59103SViresh Kumar * performance state set to @pstate. 2818c8a59103SViresh Kumar * 2819c8a59103SViresh Kumar * Return: Zero or positive performance state on success, otherwise negative 2820c8a59103SViresh Kumar * value on errors. 2821c8a59103SViresh Kumar */ 2822c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 2823c8a59103SViresh Kumar struct opp_table *dst_table, 2824c8a59103SViresh Kumar unsigned int pstate) 2825c8a59103SViresh Kumar { 2826c8a59103SViresh Kumar struct dev_pm_opp *opp; 2827c8a59103SViresh Kumar int dest_pstate = -EINVAL; 2828c8a59103SViresh Kumar int i; 2829c8a59103SViresh Kumar 2830c8a59103SViresh Kumar /* 2831c8a59103SViresh Kumar * Normally the src_table will have the "required_opps" property set to 2832c8a59103SViresh Kumar * point to one of the OPPs in the dst_table, but in some cases the 2833c8a59103SViresh Kumar * genpd and its master have one to one mapping of performance states 2834c8a59103SViresh Kumar * and so none of them have the "required-opps" property set. Return the 2835c8a59103SViresh Kumar * pstate of the src_table as it is in such cases. 2836c8a59103SViresh Kumar */ 2837f2f4d2b8SDmitry Osipenko if (!src_table || !src_table->required_opp_count) 2838c8a59103SViresh Kumar return pstate; 2839c8a59103SViresh Kumar 28407eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 28417eba0c76SViresh Kumar if (lazy_linking_pending(src_table)) 28427eba0c76SViresh Kumar return -EBUSY; 28437eba0c76SViresh Kumar 2844c8a59103SViresh Kumar for (i = 0; i < src_table->required_opp_count; i++) { 2845c8a59103SViresh Kumar if (src_table->required_opp_tables[i]->np == dst_table->np) 2846c8a59103SViresh Kumar break; 2847c8a59103SViresh Kumar } 2848c8a59103SViresh Kumar 2849c8a59103SViresh Kumar if (unlikely(i == src_table->required_opp_count)) { 2850c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 2851c8a59103SViresh Kumar __func__, src_table, dst_table); 2852c8a59103SViresh Kumar return -EINVAL; 2853c8a59103SViresh Kumar } 2854c8a59103SViresh Kumar 2855c8a59103SViresh Kumar mutex_lock(&src_table->lock); 2856c8a59103SViresh Kumar 2857c8a59103SViresh Kumar list_for_each_entry(opp, &src_table->opp_list, node) { 2858c8a59103SViresh Kumar if (opp->pstate == pstate) { 2859c8a59103SViresh Kumar dest_pstate = opp->required_opps[i]->pstate; 2860c8a59103SViresh Kumar goto unlock; 2861c8a59103SViresh Kumar } 2862c8a59103SViresh Kumar } 2863c8a59103SViresh Kumar 2864c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 2865c8a59103SViresh Kumar dst_table); 2866c8a59103SViresh Kumar 2867c8a59103SViresh Kumar unlock: 2868c8a59103SViresh Kumar mutex_unlock(&src_table->lock); 2869c8a59103SViresh Kumar 2870c8a59103SViresh Kumar return dest_pstate; 2871c8a59103SViresh Kumar } 2872c8a59103SViresh Kumar 2873c8a59103SViresh Kumar /** 28747813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 28757813dd6fSViresh Kumar * @dev: device for which we do this operation 28767813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 28777813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 28787813dd6fSViresh Kumar * 28797813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 28807813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 28817813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 28827813dd6fSViresh Kumar * 28837813dd6fSViresh Kumar * Return: 28847813dd6fSViresh Kumar * 0 On success OR 28857813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 28867813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 28877813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 28887813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 28897813dd6fSViresh Kumar */ 28907813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 28917813dd6fSViresh Kumar { 28927813dd6fSViresh Kumar struct opp_table *opp_table; 28937813dd6fSViresh Kumar int ret; 28947813dd6fSViresh Kumar 289532439ac7SViresh Kumar opp_table = _add_opp_table(dev, true); 2896dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2897dd461cd9SStephan Gerhold return PTR_ERR(opp_table); 28987813dd6fSViresh Kumar 289946f48acaSViresh Kumar /* Fix regulator count for dynamic OPPs */ 290046f48acaSViresh Kumar opp_table->regulator_count = 1; 290146f48acaSViresh Kumar 29027813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 29030ad8c623SViresh Kumar if (ret) 29047813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 29050ad8c623SViresh Kumar 29067813dd6fSViresh Kumar return ret; 29077813dd6fSViresh Kumar } 29087813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 29097813dd6fSViresh Kumar 29107813dd6fSViresh Kumar /** 29117813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 29127813dd6fSViresh Kumar * @dev: device for which we do this operation 29137813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 29147813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 29157813dd6fSViresh Kumar * 29167813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 29177813dd6fSViresh Kumar * which is isolated here. 29187813dd6fSViresh Kumar * 29197813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 29207813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 29217813dd6fSViresh Kumar * successful. 29227813dd6fSViresh Kumar */ 29237813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 29247813dd6fSViresh Kumar bool availability_req) 29257813dd6fSViresh Kumar { 29267813dd6fSViresh Kumar struct opp_table *opp_table; 29277813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 29287813dd6fSViresh Kumar int r = 0; 29297813dd6fSViresh Kumar 29307813dd6fSViresh Kumar /* Find the opp_table */ 29317813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 29327813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 29337813dd6fSViresh Kumar r = PTR_ERR(opp_table); 29347813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 29357813dd6fSViresh Kumar return r; 29367813dd6fSViresh Kumar } 29377813dd6fSViresh Kumar 29387813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 29397813dd6fSViresh Kumar 29407813dd6fSViresh Kumar /* Do we have the frequency? */ 29417813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 29427813dd6fSViresh Kumar if (tmp_opp->rate == freq) { 29437813dd6fSViresh Kumar opp = tmp_opp; 29447813dd6fSViresh Kumar break; 29457813dd6fSViresh Kumar } 29467813dd6fSViresh Kumar } 29477813dd6fSViresh Kumar 29487813dd6fSViresh Kumar if (IS_ERR(opp)) { 29497813dd6fSViresh Kumar r = PTR_ERR(opp); 29507813dd6fSViresh Kumar goto unlock; 29517813dd6fSViresh Kumar } 29527813dd6fSViresh Kumar 29537813dd6fSViresh Kumar /* Is update really needed? */ 29547813dd6fSViresh Kumar if (opp->available == availability_req) 29557813dd6fSViresh Kumar goto unlock; 29567813dd6fSViresh Kumar 29577813dd6fSViresh Kumar opp->available = availability_req; 29587813dd6fSViresh Kumar 29597813dd6fSViresh Kumar dev_pm_opp_get(opp); 29607813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 29617813dd6fSViresh Kumar 29627813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 29637813dd6fSViresh Kumar if (availability_req) 29647813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 29657813dd6fSViresh Kumar opp); 29667813dd6fSViresh Kumar else 29677813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 29687813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 29697813dd6fSViresh Kumar 29707813dd6fSViresh Kumar dev_pm_opp_put(opp); 29717813dd6fSViresh Kumar goto put_table; 29727813dd6fSViresh Kumar 29737813dd6fSViresh Kumar unlock: 29747813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 29757813dd6fSViresh Kumar put_table: 29767813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 29777813dd6fSViresh Kumar return r; 29787813dd6fSViresh Kumar } 29797813dd6fSViresh Kumar 29807813dd6fSViresh Kumar /** 298125cb20a2SStephen Boyd * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP 298225cb20a2SStephen Boyd * @dev: device for which we do this operation 298325cb20a2SStephen Boyd * @freq: OPP frequency to adjust voltage of 298425cb20a2SStephen Boyd * @u_volt: new OPP target voltage 298525cb20a2SStephen Boyd * @u_volt_min: new OPP min voltage 298625cb20a2SStephen Boyd * @u_volt_max: new OPP max voltage 298725cb20a2SStephen Boyd * 298825cb20a2SStephen Boyd * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 298925cb20a2SStephen Boyd * copy operation, returns 0 if no modifcation was done OR modification was 299025cb20a2SStephen Boyd * successful. 299125cb20a2SStephen Boyd */ 299225cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq, 299325cb20a2SStephen Boyd unsigned long u_volt, unsigned long u_volt_min, 299425cb20a2SStephen Boyd unsigned long u_volt_max) 299525cb20a2SStephen Boyd 299625cb20a2SStephen Boyd { 299725cb20a2SStephen Boyd struct opp_table *opp_table; 299825cb20a2SStephen Boyd struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 299925cb20a2SStephen Boyd int r = 0; 300025cb20a2SStephen Boyd 300125cb20a2SStephen Boyd /* Find the opp_table */ 300225cb20a2SStephen Boyd opp_table = _find_opp_table(dev); 300325cb20a2SStephen Boyd if (IS_ERR(opp_table)) { 300425cb20a2SStephen Boyd r = PTR_ERR(opp_table); 300525cb20a2SStephen Boyd dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 300625cb20a2SStephen Boyd return r; 300725cb20a2SStephen Boyd } 300825cb20a2SStephen Boyd 300925cb20a2SStephen Boyd mutex_lock(&opp_table->lock); 301025cb20a2SStephen Boyd 301125cb20a2SStephen Boyd /* Do we have the frequency? */ 301225cb20a2SStephen Boyd list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 301325cb20a2SStephen Boyd if (tmp_opp->rate == freq) { 301425cb20a2SStephen Boyd opp = tmp_opp; 301525cb20a2SStephen Boyd break; 301625cb20a2SStephen Boyd } 301725cb20a2SStephen Boyd } 301825cb20a2SStephen Boyd 301925cb20a2SStephen Boyd if (IS_ERR(opp)) { 302025cb20a2SStephen Boyd r = PTR_ERR(opp); 302125cb20a2SStephen Boyd goto adjust_unlock; 302225cb20a2SStephen Boyd } 302325cb20a2SStephen Boyd 302425cb20a2SStephen Boyd /* Is update really needed? */ 302525cb20a2SStephen Boyd if (opp->supplies->u_volt == u_volt) 302625cb20a2SStephen Boyd goto adjust_unlock; 302725cb20a2SStephen Boyd 302825cb20a2SStephen Boyd opp->supplies->u_volt = u_volt; 302925cb20a2SStephen Boyd opp->supplies->u_volt_min = u_volt_min; 303025cb20a2SStephen Boyd opp->supplies->u_volt_max = u_volt_max; 303125cb20a2SStephen Boyd 303225cb20a2SStephen Boyd dev_pm_opp_get(opp); 303325cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 303425cb20a2SStephen Boyd 303525cb20a2SStephen Boyd /* Notify the voltage change of the OPP */ 303625cb20a2SStephen Boyd blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE, 303725cb20a2SStephen Boyd opp); 303825cb20a2SStephen Boyd 303925cb20a2SStephen Boyd dev_pm_opp_put(opp); 304025cb20a2SStephen Boyd goto adjust_put_table; 304125cb20a2SStephen Boyd 304225cb20a2SStephen Boyd adjust_unlock: 304325cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 304425cb20a2SStephen Boyd adjust_put_table: 304525cb20a2SStephen Boyd dev_pm_opp_put_opp_table(opp_table); 304625cb20a2SStephen Boyd return r; 304725cb20a2SStephen Boyd } 304803649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage); 304925cb20a2SStephen Boyd 305025cb20a2SStephen Boyd /** 30517813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 30527813dd6fSViresh Kumar * @dev: device for which we do this operation 30537813dd6fSViresh Kumar * @freq: OPP frequency to enable 30547813dd6fSViresh Kumar * 30557813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 30567813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 30577813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 30587813dd6fSViresh Kumar * 30597813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 30607813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 30617813dd6fSViresh Kumar * successful. 30627813dd6fSViresh Kumar */ 30637813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 30647813dd6fSViresh Kumar { 30657813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 30667813dd6fSViresh Kumar } 30677813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 30687813dd6fSViresh Kumar 30697813dd6fSViresh Kumar /** 30707813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 30717813dd6fSViresh Kumar * @dev: device for which we do this operation 30727813dd6fSViresh Kumar * @freq: OPP frequency to disable 30737813dd6fSViresh Kumar * 30747813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 30757813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 30767813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 30777813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 30787813dd6fSViresh Kumar * 30797813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 30807813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 30817813dd6fSViresh Kumar * successful. 30827813dd6fSViresh Kumar */ 30837813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 30847813dd6fSViresh Kumar { 30857813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 30867813dd6fSViresh Kumar } 30877813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 30887813dd6fSViresh Kumar 30897813dd6fSViresh Kumar /** 30907813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 30917813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 30927813dd6fSViresh Kumar * @nb: Notifier block to be registered 30937813dd6fSViresh Kumar * 30947813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 30957813dd6fSViresh Kumar */ 30967813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 30977813dd6fSViresh Kumar { 30987813dd6fSViresh Kumar struct opp_table *opp_table; 30997813dd6fSViresh Kumar int ret; 31007813dd6fSViresh Kumar 31017813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 31027813dd6fSViresh Kumar if (IS_ERR(opp_table)) 31037813dd6fSViresh Kumar return PTR_ERR(opp_table); 31047813dd6fSViresh Kumar 31057813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 31067813dd6fSViresh Kumar 31077813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 31087813dd6fSViresh Kumar 31097813dd6fSViresh Kumar return ret; 31107813dd6fSViresh Kumar } 31117813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 31127813dd6fSViresh Kumar 31137813dd6fSViresh Kumar /** 31147813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 31157813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 31167813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 31177813dd6fSViresh Kumar * 31187813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 31197813dd6fSViresh Kumar */ 31207813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 31217813dd6fSViresh Kumar struct notifier_block *nb) 31227813dd6fSViresh Kumar { 31237813dd6fSViresh Kumar struct opp_table *opp_table; 31247813dd6fSViresh Kumar int ret; 31257813dd6fSViresh Kumar 31267813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 31277813dd6fSViresh Kumar if (IS_ERR(opp_table)) 31287813dd6fSViresh Kumar return PTR_ERR(opp_table); 31297813dd6fSViresh Kumar 31307813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 31317813dd6fSViresh Kumar 31327813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 31337813dd6fSViresh Kumar 31347813dd6fSViresh Kumar return ret; 31357813dd6fSViresh Kumar } 31367813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 31377813dd6fSViresh Kumar 31388aaf6264SViresh Kumar /** 31398aaf6264SViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 31408aaf6264SViresh Kumar * @dev: device pointer used to lookup OPP table. 31418aaf6264SViresh Kumar * 31428aaf6264SViresh Kumar * Free both OPPs created using static entries present in DT and the 31438aaf6264SViresh Kumar * dynamically added entries. 31448aaf6264SViresh Kumar */ 31458aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 31467813dd6fSViresh Kumar { 31477813dd6fSViresh Kumar struct opp_table *opp_table; 31487813dd6fSViresh Kumar 31497813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 31507813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 31517813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 31527813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 31537813dd6fSViresh Kumar 31547813dd6fSViresh Kumar if (error != -ENODEV) 31557813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 31567813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 31577813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 31587813dd6fSViresh Kumar error); 31597813dd6fSViresh Kumar return; 31607813dd6fSViresh Kumar } 31617813dd6fSViresh Kumar 3162922ff075SViresh Kumar /* 3163922ff075SViresh Kumar * Drop the extra reference only if the OPP table was successfully added 3164922ff075SViresh Kumar * with dev_pm_opp_of_add_table() earlier. 3165922ff075SViresh Kumar **/ 3166922ff075SViresh Kumar if (_opp_remove_all_static(opp_table)) 3167cdd6ed90SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3168cdd6ed90SViresh Kumar 3169922ff075SViresh Kumar /* Drop reference taken by _find_opp_table() */ 31707813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 31717813dd6fSViresh Kumar } 31727813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 3173ce8073d8SDmitry Osipenko 3174ce8073d8SDmitry Osipenko /** 3175ce8073d8SDmitry Osipenko * dev_pm_opp_sync_regulators() - Sync state of voltage regulators 3176ce8073d8SDmitry Osipenko * @dev: device for which we do this operation 3177ce8073d8SDmitry Osipenko * 3178ce8073d8SDmitry Osipenko * Sync voltage state of the OPP table regulators. 3179ce8073d8SDmitry Osipenko * 3180ce8073d8SDmitry Osipenko * Return: 0 on success or a negative error value. 3181ce8073d8SDmitry Osipenko */ 3182ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev) 3183ce8073d8SDmitry Osipenko { 3184ce8073d8SDmitry Osipenko struct opp_table *opp_table; 3185ce8073d8SDmitry Osipenko struct regulator *reg; 3186ce8073d8SDmitry Osipenko int i, ret = 0; 3187ce8073d8SDmitry Osipenko 3188ce8073d8SDmitry Osipenko /* Device may not have OPP table */ 3189ce8073d8SDmitry Osipenko opp_table = _find_opp_table(dev); 3190ce8073d8SDmitry Osipenko if (IS_ERR(opp_table)) 3191ce8073d8SDmitry Osipenko return 0; 3192ce8073d8SDmitry Osipenko 3193ce8073d8SDmitry Osipenko /* Regulator may not be required for the device */ 3194ce8073d8SDmitry Osipenko if (unlikely(!opp_table->regulators)) 3195ce8073d8SDmitry Osipenko goto put_table; 3196ce8073d8SDmitry Osipenko 3197ce8073d8SDmitry Osipenko /* Nothing to sync if voltage wasn't changed */ 3198ce8073d8SDmitry Osipenko if (!opp_table->enabled) 3199ce8073d8SDmitry Osipenko goto put_table; 3200ce8073d8SDmitry Osipenko 3201ce8073d8SDmitry Osipenko for (i = 0; i < opp_table->regulator_count; i++) { 3202ce8073d8SDmitry Osipenko reg = opp_table->regulators[i]; 3203ce8073d8SDmitry Osipenko ret = regulator_sync_voltage(reg); 3204ce8073d8SDmitry Osipenko if (ret) 3205ce8073d8SDmitry Osipenko break; 3206ce8073d8SDmitry Osipenko } 3207ce8073d8SDmitry Osipenko put_table: 3208ce8073d8SDmitry Osipenko /* Drop reference taken by _find_opp_table() */ 3209ce8073d8SDmitry Osipenko dev_pm_opp_put_opp_table(opp_table); 3210ce8073d8SDmitry Osipenko 3211ce8073d8SDmitry Osipenko return ret; 3212ce8073d8SDmitry Osipenko } 3213ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators); 3214