1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27813dd6fSViresh Kumar /* 37813dd6fSViresh Kumar * Generic OPP Interface 47813dd6fSViresh Kumar * 57813dd6fSViresh Kumar * Copyright (C) 2009-2010 Texas Instruments Incorporated. 67813dd6fSViresh Kumar * Nishanth Menon 77813dd6fSViresh Kumar * Romit Dasgupta 87813dd6fSViresh Kumar * Kevin Hilman 97813dd6fSViresh Kumar */ 107813dd6fSViresh Kumar 117813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 127813dd6fSViresh Kumar 137813dd6fSViresh Kumar #include <linux/clk.h> 147813dd6fSViresh Kumar #include <linux/errno.h> 157813dd6fSViresh Kumar #include <linux/err.h> 167813dd6fSViresh Kumar #include <linux/slab.h> 177813dd6fSViresh Kumar #include <linux/device.h> 187813dd6fSViresh Kumar #include <linux/export.h> 19009acd19SViresh Kumar #include <linux/pm_domain.h> 207813dd6fSViresh Kumar #include <linux/regulator/consumer.h> 217813dd6fSViresh Kumar 227813dd6fSViresh Kumar #include "opp.h" 237813dd6fSViresh Kumar 247813dd6fSViresh Kumar /* 257813dd6fSViresh Kumar * The root of the list of all opp-tables. All opp_table structures branch off 267813dd6fSViresh Kumar * from here, with each opp_table containing the list of opps it supports in 277813dd6fSViresh Kumar * various states of availability. 287813dd6fSViresh Kumar */ 297813dd6fSViresh Kumar LIST_HEAD(opp_tables); 307813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */ 317813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock); 3227c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */ 3327c09484SViresh Kumar static bool opp_tables_busy; 347813dd6fSViresh Kumar 359e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table) 367813dd6fSViresh Kumar { 377813dd6fSViresh Kumar struct opp_device *opp_dev; 389e62edacSViresh Kumar bool found = false; 397813dd6fSViresh Kumar 409e62edacSViresh Kumar mutex_lock(&opp_table->lock); 417813dd6fSViresh Kumar list_for_each_entry(opp_dev, &opp_table->dev_list, node) 429e62edacSViresh Kumar if (opp_dev->dev == dev) { 439e62edacSViresh Kumar found = true; 449e62edacSViresh Kumar break; 459e62edacSViresh Kumar } 467813dd6fSViresh Kumar 479e62edacSViresh Kumar mutex_unlock(&opp_table->lock); 489e62edacSViresh Kumar return found; 497813dd6fSViresh Kumar } 507813dd6fSViresh Kumar 517813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev) 527813dd6fSViresh Kumar { 537813dd6fSViresh Kumar struct opp_table *opp_table; 547813dd6fSViresh Kumar 557813dd6fSViresh Kumar list_for_each_entry(opp_table, &opp_tables, node) { 569e62edacSViresh Kumar if (_find_opp_dev(dev, opp_table)) { 577813dd6fSViresh Kumar _get_opp_table_kref(opp_table); 587813dd6fSViresh Kumar return opp_table; 597813dd6fSViresh Kumar } 607813dd6fSViresh Kumar } 617813dd6fSViresh Kumar 627813dd6fSViresh Kumar return ERR_PTR(-ENODEV); 637813dd6fSViresh Kumar } 647813dd6fSViresh Kumar 657813dd6fSViresh Kumar /** 667813dd6fSViresh Kumar * _find_opp_table() - find opp_table struct using device pointer 677813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table 687813dd6fSViresh Kumar * 697813dd6fSViresh Kumar * Search OPP table for one containing matching device. 707813dd6fSViresh Kumar * 717813dd6fSViresh Kumar * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or 727813dd6fSViresh Kumar * -EINVAL based on type of error. 737813dd6fSViresh Kumar * 747813dd6fSViresh Kumar * The callers must call dev_pm_opp_put_opp_table() after the table is used. 757813dd6fSViresh Kumar */ 767813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev) 777813dd6fSViresh Kumar { 787813dd6fSViresh Kumar struct opp_table *opp_table; 797813dd6fSViresh Kumar 807813dd6fSViresh Kumar if (IS_ERR_OR_NULL(dev)) { 817813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 827813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 837813dd6fSViresh Kumar } 847813dd6fSViresh Kumar 857813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 867813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 877813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 887813dd6fSViresh Kumar 897813dd6fSViresh Kumar return opp_table; 907813dd6fSViresh Kumar } 917813dd6fSViresh Kumar 927813dd6fSViresh Kumar /** 937813dd6fSViresh Kumar * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp 947813dd6fSViresh Kumar * @opp: opp for which voltage has to be returned for 957813dd6fSViresh Kumar * 967813dd6fSViresh Kumar * Return: voltage in micro volt corresponding to the opp, else 977813dd6fSViresh Kumar * return 0 987813dd6fSViresh Kumar * 997813dd6fSViresh Kumar * This is useful only for devices with single power supply. 1007813dd6fSViresh Kumar */ 1017813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 1027813dd6fSViresh Kumar { 1037813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp)) { 1047813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1057813dd6fSViresh Kumar return 0; 1067813dd6fSViresh Kumar } 1077813dd6fSViresh Kumar 1087813dd6fSViresh Kumar return opp->supplies[0].u_volt; 1097813dd6fSViresh Kumar } 1107813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); 1117813dd6fSViresh Kumar 1127813dd6fSViresh Kumar /** 1137813dd6fSViresh Kumar * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp 1147813dd6fSViresh Kumar * @opp: opp for which frequency has to be returned for 1157813dd6fSViresh Kumar * 1167813dd6fSViresh Kumar * Return: frequency in hertz corresponding to the opp, else 1177813dd6fSViresh Kumar * return 0 1187813dd6fSViresh Kumar */ 1197813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) 1207813dd6fSViresh Kumar { 12106a8a059SAndrew-sh.Cheng if (IS_ERR_OR_NULL(opp)) { 1227813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1237813dd6fSViresh Kumar return 0; 1247813dd6fSViresh Kumar } 1257813dd6fSViresh Kumar 1267813dd6fSViresh Kumar return opp->rate; 1277813dd6fSViresh Kumar } 1287813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); 1297813dd6fSViresh Kumar 1307813dd6fSViresh Kumar /** 1315b93ac54SRajendra Nayak * dev_pm_opp_get_level() - Gets the level corresponding to an available opp 1325b93ac54SRajendra Nayak * @opp: opp for which level value has to be returned for 1335b93ac54SRajendra Nayak * 1345b93ac54SRajendra Nayak * Return: level read from device tree corresponding to the opp, else 1355b93ac54SRajendra Nayak * return 0. 1365b93ac54SRajendra Nayak */ 1375b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp) 1385b93ac54SRajendra Nayak { 1395b93ac54SRajendra Nayak if (IS_ERR_OR_NULL(opp) || !opp->available) { 1405b93ac54SRajendra Nayak pr_err("%s: Invalid parameters\n", __func__); 1415b93ac54SRajendra Nayak return 0; 1425b93ac54SRajendra Nayak } 1435b93ac54SRajendra Nayak 1445b93ac54SRajendra Nayak return opp->level; 1455b93ac54SRajendra Nayak } 1465b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level); 1475b93ac54SRajendra Nayak 1485b93ac54SRajendra Nayak /** 1497813dd6fSViresh Kumar * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not 1507813dd6fSViresh Kumar * @opp: opp for which turbo mode is being verified 1517813dd6fSViresh Kumar * 1527813dd6fSViresh Kumar * Turbo OPPs are not for normal use, and can be enabled (under certain 1537813dd6fSViresh Kumar * conditions) for short duration of times to finish high throughput work 1547813dd6fSViresh Kumar * quickly. Running on them for longer times may overheat the chip. 1557813dd6fSViresh Kumar * 1567813dd6fSViresh Kumar * Return: true if opp is turbo opp, else false. 1577813dd6fSViresh Kumar */ 1587813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 1597813dd6fSViresh Kumar { 1607813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 1617813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1627813dd6fSViresh Kumar return false; 1637813dd6fSViresh Kumar } 1647813dd6fSViresh Kumar 1657813dd6fSViresh Kumar return opp->turbo; 1667813dd6fSViresh Kumar } 1677813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 1687813dd6fSViresh Kumar 1697813dd6fSViresh Kumar /** 1707813dd6fSViresh Kumar * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds 1717813dd6fSViresh Kumar * @dev: device for which we do this operation 1727813dd6fSViresh Kumar * 1737813dd6fSViresh Kumar * Return: This function returns the max clock latency in nanoseconds. 1747813dd6fSViresh Kumar */ 1757813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 1767813dd6fSViresh Kumar { 1777813dd6fSViresh Kumar struct opp_table *opp_table; 1787813dd6fSViresh Kumar unsigned long clock_latency_ns; 1797813dd6fSViresh Kumar 1807813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 1817813dd6fSViresh Kumar if (IS_ERR(opp_table)) 1827813dd6fSViresh Kumar return 0; 1837813dd6fSViresh Kumar 1847813dd6fSViresh Kumar clock_latency_ns = opp_table->clock_latency_ns_max; 1857813dd6fSViresh Kumar 1867813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1877813dd6fSViresh Kumar 1887813dd6fSViresh Kumar return clock_latency_ns; 1897813dd6fSViresh Kumar } 1907813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 1917813dd6fSViresh Kumar 1927813dd6fSViresh Kumar /** 1937813dd6fSViresh Kumar * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 1947813dd6fSViresh Kumar * @dev: device for which we do this operation 1957813dd6fSViresh Kumar * 1967813dd6fSViresh Kumar * Return: This function returns the max voltage latency in nanoseconds. 1977813dd6fSViresh Kumar */ 1987813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 1997813dd6fSViresh Kumar { 2007813dd6fSViresh Kumar struct opp_table *opp_table; 2017813dd6fSViresh Kumar struct dev_pm_opp *opp; 2027813dd6fSViresh Kumar struct regulator *reg; 2037813dd6fSViresh Kumar unsigned long latency_ns = 0; 2047813dd6fSViresh Kumar int ret, i, count; 2057813dd6fSViresh Kumar struct { 2067813dd6fSViresh Kumar unsigned long min; 2077813dd6fSViresh Kumar unsigned long max; 2087813dd6fSViresh Kumar } *uV; 2097813dd6fSViresh Kumar 2107813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2117813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2127813dd6fSViresh Kumar return 0; 2137813dd6fSViresh Kumar 2147813dd6fSViresh Kumar /* Regulator may not be required for the device */ 21590e3577bSViresh Kumar if (!opp_table->regulators) 2167813dd6fSViresh Kumar goto put_opp_table; 2177813dd6fSViresh Kumar 21890e3577bSViresh Kumar count = opp_table->regulator_count; 21990e3577bSViresh Kumar 2207813dd6fSViresh Kumar uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 2217813dd6fSViresh Kumar if (!uV) 2227813dd6fSViresh Kumar goto put_opp_table; 2237813dd6fSViresh Kumar 2247813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 2257813dd6fSViresh Kumar 2267813dd6fSViresh Kumar for (i = 0; i < count; i++) { 2277813dd6fSViresh Kumar uV[i].min = ~0; 2287813dd6fSViresh Kumar uV[i].max = 0; 2297813dd6fSViresh Kumar 2307813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 2317813dd6fSViresh Kumar if (!opp->available) 2327813dd6fSViresh Kumar continue; 2337813dd6fSViresh Kumar 2347813dd6fSViresh Kumar if (opp->supplies[i].u_volt_min < uV[i].min) 2357813dd6fSViresh Kumar uV[i].min = opp->supplies[i].u_volt_min; 2367813dd6fSViresh Kumar if (opp->supplies[i].u_volt_max > uV[i].max) 2377813dd6fSViresh Kumar uV[i].max = opp->supplies[i].u_volt_max; 2387813dd6fSViresh Kumar } 2397813dd6fSViresh Kumar } 2407813dd6fSViresh Kumar 2417813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 2427813dd6fSViresh Kumar 2437813dd6fSViresh Kumar /* 2447813dd6fSViresh Kumar * The caller needs to ensure that opp_table (and hence the regulator) 2457813dd6fSViresh Kumar * isn't freed, while we are executing this routine. 2467813dd6fSViresh Kumar */ 2477813dd6fSViresh Kumar for (i = 0; i < count; i++) { 2487813dd6fSViresh Kumar reg = opp_table->regulators[i]; 2497813dd6fSViresh Kumar ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); 2507813dd6fSViresh Kumar if (ret > 0) 2517813dd6fSViresh Kumar latency_ns += ret * 1000; 2527813dd6fSViresh Kumar } 2537813dd6fSViresh Kumar 2547813dd6fSViresh Kumar kfree(uV); 2557813dd6fSViresh Kumar put_opp_table: 2567813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2577813dd6fSViresh Kumar 2587813dd6fSViresh Kumar return latency_ns; 2597813dd6fSViresh Kumar } 2607813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); 2617813dd6fSViresh Kumar 2627813dd6fSViresh Kumar /** 2637813dd6fSViresh Kumar * dev_pm_opp_get_max_transition_latency() - Get max transition latency in 2647813dd6fSViresh Kumar * nanoseconds 2657813dd6fSViresh Kumar * @dev: device for which we do this operation 2667813dd6fSViresh Kumar * 2677813dd6fSViresh Kumar * Return: This function returns the max transition latency, in nanoseconds, to 2687813dd6fSViresh Kumar * switch from one OPP to other. 2697813dd6fSViresh Kumar */ 2707813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 2717813dd6fSViresh Kumar { 2727813dd6fSViresh Kumar return dev_pm_opp_get_max_volt_latency(dev) + 2737813dd6fSViresh Kumar dev_pm_opp_get_max_clock_latency(dev); 2747813dd6fSViresh Kumar } 2757813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 2767813dd6fSViresh Kumar 2777813dd6fSViresh Kumar /** 2787813dd6fSViresh Kumar * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 2797813dd6fSViresh Kumar * @dev: device for which we do this operation 2807813dd6fSViresh Kumar * 2817813dd6fSViresh Kumar * Return: This function returns the frequency of the OPP marked as suspend_opp 2827813dd6fSViresh Kumar * if one is available, else returns 0; 2837813dd6fSViresh Kumar */ 2847813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 2857813dd6fSViresh Kumar { 2867813dd6fSViresh Kumar struct opp_table *opp_table; 2877813dd6fSViresh Kumar unsigned long freq = 0; 2887813dd6fSViresh Kumar 2897813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2907813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2917813dd6fSViresh Kumar return 0; 2927813dd6fSViresh Kumar 2937813dd6fSViresh Kumar if (opp_table->suspend_opp && opp_table->suspend_opp->available) 2947813dd6fSViresh Kumar freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 2957813dd6fSViresh Kumar 2967813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2977813dd6fSViresh Kumar 2987813dd6fSViresh Kumar return freq; 2997813dd6fSViresh Kumar } 3007813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 3017813dd6fSViresh Kumar 302a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table) 303a1e8c136SViresh Kumar { 304a1e8c136SViresh Kumar struct dev_pm_opp *opp; 305a1e8c136SViresh Kumar int count = 0; 306a1e8c136SViresh Kumar 307a1e8c136SViresh Kumar mutex_lock(&opp_table->lock); 308a1e8c136SViresh Kumar 309a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 310a1e8c136SViresh Kumar if (opp->available) 311a1e8c136SViresh Kumar count++; 312a1e8c136SViresh Kumar } 313a1e8c136SViresh Kumar 314a1e8c136SViresh Kumar mutex_unlock(&opp_table->lock); 315a1e8c136SViresh Kumar 316a1e8c136SViresh Kumar return count; 317a1e8c136SViresh Kumar } 318a1e8c136SViresh Kumar 3197813dd6fSViresh Kumar /** 3207813dd6fSViresh Kumar * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table 3217813dd6fSViresh Kumar * @dev: device for which we do this operation 3227813dd6fSViresh Kumar * 3237813dd6fSViresh Kumar * Return: This function returns the number of available opps if there are any, 3247813dd6fSViresh Kumar * else returns 0 if none or the corresponding error value. 3257813dd6fSViresh Kumar */ 3267813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev) 3277813dd6fSViresh Kumar { 3287813dd6fSViresh Kumar struct opp_table *opp_table; 329a1e8c136SViresh Kumar int count; 3307813dd6fSViresh Kumar 3317813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3327813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 3337813dd6fSViresh Kumar count = PTR_ERR(opp_table); 334035ed072SFabio Estevam dev_dbg(dev, "%s: OPP table not found (%d)\n", 3357813dd6fSViresh Kumar __func__, count); 33609f662f9SViresh Kumar return count; 3377813dd6fSViresh Kumar } 3387813dd6fSViresh Kumar 339a1e8c136SViresh Kumar count = _get_opp_count(opp_table); 3407813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3417813dd6fSViresh Kumar 3427813dd6fSViresh Kumar return count; 3437813dd6fSViresh Kumar } 3447813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); 3457813dd6fSViresh Kumar 3467813dd6fSViresh Kumar /** 3477813dd6fSViresh Kumar * dev_pm_opp_find_freq_exact() - search for an exact frequency 3487813dd6fSViresh Kumar * @dev: device for which we do this operation 3497813dd6fSViresh Kumar * @freq: frequency to search for 3507813dd6fSViresh Kumar * @available: true/false - match for available opp 3517813dd6fSViresh Kumar * 3527813dd6fSViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 3537813dd6fSViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 3547813dd6fSViresh Kumar * be handled using IS_ERR. Error return values can be: 3557813dd6fSViresh Kumar * EINVAL: for bad pointer 3567813dd6fSViresh Kumar * ERANGE: no match found for search 3577813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 3587813dd6fSViresh Kumar * 3597813dd6fSViresh Kumar * Note: available is a modifier for the search. if available=true, then the 3607813dd6fSViresh Kumar * match is for exact matching frequency and is available in the stored OPP 3617813dd6fSViresh Kumar * table. if false, the match is for exact frequency which is not available. 3627813dd6fSViresh Kumar * 3637813dd6fSViresh Kumar * This provides a mechanism to enable an opp which is not available currently 3647813dd6fSViresh Kumar * or the opposite as well. 3657813dd6fSViresh Kumar * 3667813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 3677813dd6fSViresh Kumar * use. 3687813dd6fSViresh Kumar */ 3697813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 3707813dd6fSViresh Kumar unsigned long freq, 3717813dd6fSViresh Kumar bool available) 3727813dd6fSViresh Kumar { 3737813dd6fSViresh Kumar struct opp_table *opp_table; 3747813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 3757813dd6fSViresh Kumar 3767813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3777813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 3787813dd6fSViresh Kumar int r = PTR_ERR(opp_table); 3797813dd6fSViresh Kumar 3807813dd6fSViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 3817813dd6fSViresh Kumar return ERR_PTR(r); 3827813dd6fSViresh Kumar } 3837813dd6fSViresh Kumar 3847813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 3857813dd6fSViresh Kumar 3867813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 3877813dd6fSViresh Kumar if (temp_opp->available == available && 3887813dd6fSViresh Kumar temp_opp->rate == freq) { 3897813dd6fSViresh Kumar opp = temp_opp; 3907813dd6fSViresh Kumar 3917813dd6fSViresh Kumar /* Increment the reference count of OPP */ 3927813dd6fSViresh Kumar dev_pm_opp_get(opp); 3937813dd6fSViresh Kumar break; 3947813dd6fSViresh Kumar } 3957813dd6fSViresh Kumar } 3967813dd6fSViresh Kumar 3977813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 3987813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3997813dd6fSViresh Kumar 4007813dd6fSViresh Kumar return opp; 4017813dd6fSViresh Kumar } 4027813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 4037813dd6fSViresh Kumar 40471419d84SNiklas Cassel /** 40571419d84SNiklas Cassel * dev_pm_opp_find_level_exact() - search for an exact level 40671419d84SNiklas Cassel * @dev: device for which we do this operation 40771419d84SNiklas Cassel * @level: level to search for 40871419d84SNiklas Cassel * 40971419d84SNiklas Cassel * Return: Searches for exact match in the opp table and returns pointer to the 41071419d84SNiklas Cassel * matching opp if found, else returns ERR_PTR in case of error and should 41171419d84SNiklas Cassel * be handled using IS_ERR. Error return values can be: 41271419d84SNiklas Cassel * EINVAL: for bad pointer 41371419d84SNiklas Cassel * ERANGE: no match found for search 41471419d84SNiklas Cassel * ENODEV: if device not found in list of registered devices 41571419d84SNiklas Cassel * 41671419d84SNiklas Cassel * The callers are required to call dev_pm_opp_put() for the returned OPP after 41771419d84SNiklas Cassel * use. 41871419d84SNiklas Cassel */ 41971419d84SNiklas Cassel struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev, 42071419d84SNiklas Cassel unsigned int level) 42171419d84SNiklas Cassel { 42271419d84SNiklas Cassel struct opp_table *opp_table; 42371419d84SNiklas Cassel struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 42471419d84SNiklas Cassel 42571419d84SNiklas Cassel opp_table = _find_opp_table(dev); 42671419d84SNiklas Cassel if (IS_ERR(opp_table)) { 42771419d84SNiklas Cassel int r = PTR_ERR(opp_table); 42871419d84SNiklas Cassel 42971419d84SNiklas Cassel dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 43071419d84SNiklas Cassel return ERR_PTR(r); 43171419d84SNiklas Cassel } 43271419d84SNiklas Cassel 43371419d84SNiklas Cassel mutex_lock(&opp_table->lock); 43471419d84SNiklas Cassel 43571419d84SNiklas Cassel list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 43671419d84SNiklas Cassel if (temp_opp->level == level) { 43771419d84SNiklas Cassel opp = temp_opp; 43871419d84SNiklas Cassel 43971419d84SNiklas Cassel /* Increment the reference count of OPP */ 44071419d84SNiklas Cassel dev_pm_opp_get(opp); 44171419d84SNiklas Cassel break; 44271419d84SNiklas Cassel } 44371419d84SNiklas Cassel } 44471419d84SNiklas Cassel 44571419d84SNiklas Cassel mutex_unlock(&opp_table->lock); 44671419d84SNiklas Cassel dev_pm_opp_put_opp_table(opp_table); 44771419d84SNiklas Cassel 44871419d84SNiklas Cassel return opp; 44971419d84SNiklas Cassel } 45071419d84SNiklas Cassel EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact); 45171419d84SNiklas Cassel 452*8dd5cadaSDmitry Osipenko /** 453*8dd5cadaSDmitry Osipenko * dev_pm_opp_find_level_ceil() - search for an rounded up level 454*8dd5cadaSDmitry Osipenko * @dev: device for which we do this operation 455*8dd5cadaSDmitry Osipenko * @level: level to search for 456*8dd5cadaSDmitry Osipenko * 457*8dd5cadaSDmitry Osipenko * Return: Searches for rounded up match in the opp table and returns pointer 458*8dd5cadaSDmitry Osipenko * to the matching opp if found, else returns ERR_PTR in case of error and 459*8dd5cadaSDmitry Osipenko * should be handled using IS_ERR. Error return values can be: 460*8dd5cadaSDmitry Osipenko * EINVAL: for bad pointer 461*8dd5cadaSDmitry Osipenko * ERANGE: no match found for search 462*8dd5cadaSDmitry Osipenko * ENODEV: if device not found in list of registered devices 463*8dd5cadaSDmitry Osipenko * 464*8dd5cadaSDmitry Osipenko * The callers are required to call dev_pm_opp_put() for the returned OPP after 465*8dd5cadaSDmitry Osipenko * use. 466*8dd5cadaSDmitry Osipenko */ 467*8dd5cadaSDmitry Osipenko struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev, 468*8dd5cadaSDmitry Osipenko unsigned int *level) 469*8dd5cadaSDmitry Osipenko { 470*8dd5cadaSDmitry Osipenko struct opp_table *opp_table; 471*8dd5cadaSDmitry Osipenko struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 472*8dd5cadaSDmitry Osipenko 473*8dd5cadaSDmitry Osipenko opp_table = _find_opp_table(dev); 474*8dd5cadaSDmitry Osipenko if (IS_ERR(opp_table)) { 475*8dd5cadaSDmitry Osipenko int r = PTR_ERR(opp_table); 476*8dd5cadaSDmitry Osipenko 477*8dd5cadaSDmitry Osipenko dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 478*8dd5cadaSDmitry Osipenko return ERR_PTR(r); 479*8dd5cadaSDmitry Osipenko } 480*8dd5cadaSDmitry Osipenko 481*8dd5cadaSDmitry Osipenko mutex_lock(&opp_table->lock); 482*8dd5cadaSDmitry Osipenko 483*8dd5cadaSDmitry Osipenko list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 484*8dd5cadaSDmitry Osipenko if (temp_opp->available && temp_opp->level >= *level) { 485*8dd5cadaSDmitry Osipenko opp = temp_opp; 486*8dd5cadaSDmitry Osipenko *level = opp->level; 487*8dd5cadaSDmitry Osipenko 488*8dd5cadaSDmitry Osipenko /* Increment the reference count of OPP */ 489*8dd5cadaSDmitry Osipenko dev_pm_opp_get(opp); 490*8dd5cadaSDmitry Osipenko break; 491*8dd5cadaSDmitry Osipenko } 492*8dd5cadaSDmitry Osipenko } 493*8dd5cadaSDmitry Osipenko 494*8dd5cadaSDmitry Osipenko mutex_unlock(&opp_table->lock); 495*8dd5cadaSDmitry Osipenko dev_pm_opp_put_opp_table(opp_table); 496*8dd5cadaSDmitry Osipenko 497*8dd5cadaSDmitry Osipenko return opp; 498*8dd5cadaSDmitry Osipenko } 499*8dd5cadaSDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil); 500*8dd5cadaSDmitry Osipenko 5017813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 5027813dd6fSViresh Kumar unsigned long *freq) 5037813dd6fSViresh Kumar { 5047813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 5057813dd6fSViresh Kumar 5067813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 5077813dd6fSViresh Kumar 5087813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 5097813dd6fSViresh Kumar if (temp_opp->available && temp_opp->rate >= *freq) { 5107813dd6fSViresh Kumar opp = temp_opp; 5117813dd6fSViresh Kumar *freq = opp->rate; 5127813dd6fSViresh Kumar 5137813dd6fSViresh Kumar /* Increment the reference count of OPP */ 5147813dd6fSViresh Kumar dev_pm_opp_get(opp); 5157813dd6fSViresh Kumar break; 5167813dd6fSViresh Kumar } 5177813dd6fSViresh Kumar } 5187813dd6fSViresh Kumar 5197813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 5207813dd6fSViresh Kumar 5217813dd6fSViresh Kumar return opp; 5227813dd6fSViresh Kumar } 5237813dd6fSViresh Kumar 5247813dd6fSViresh Kumar /** 5257813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 5267813dd6fSViresh Kumar * @dev: device for which we do this operation 5277813dd6fSViresh Kumar * @freq: Start frequency 5287813dd6fSViresh Kumar * 5297813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 5307813dd6fSViresh Kumar * for a device. 5317813dd6fSViresh Kumar * 5327813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 5337813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 5347813dd6fSViresh Kumar * values can be: 5357813dd6fSViresh Kumar * EINVAL: for bad pointer 5367813dd6fSViresh Kumar * ERANGE: no match found for search 5377813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 5387813dd6fSViresh Kumar * 5397813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 5407813dd6fSViresh Kumar * use. 5417813dd6fSViresh Kumar */ 5427813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 5437813dd6fSViresh Kumar unsigned long *freq) 5447813dd6fSViresh Kumar { 5457813dd6fSViresh Kumar struct opp_table *opp_table; 5467813dd6fSViresh Kumar struct dev_pm_opp *opp; 5477813dd6fSViresh Kumar 5487813dd6fSViresh Kumar if (!dev || !freq) { 5497813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 5507813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 5517813dd6fSViresh Kumar } 5527813dd6fSViresh Kumar 5537813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 5547813dd6fSViresh Kumar if (IS_ERR(opp_table)) 5557813dd6fSViresh Kumar return ERR_CAST(opp_table); 5567813dd6fSViresh Kumar 5577813dd6fSViresh Kumar opp = _find_freq_ceil(opp_table, freq); 5587813dd6fSViresh Kumar 5597813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 5607813dd6fSViresh Kumar 5617813dd6fSViresh Kumar return opp; 5627813dd6fSViresh Kumar } 5637813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 5647813dd6fSViresh Kumar 5657813dd6fSViresh Kumar /** 5667813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 5677813dd6fSViresh Kumar * @dev: device for which we do this operation 5687813dd6fSViresh Kumar * @freq: Start frequency 5697813dd6fSViresh Kumar * 5707813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 5717813dd6fSViresh Kumar * for a device. 5727813dd6fSViresh Kumar * 5737813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 5747813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 5757813dd6fSViresh Kumar * values can be: 5767813dd6fSViresh Kumar * EINVAL: for bad pointer 5777813dd6fSViresh Kumar * ERANGE: no match found for search 5787813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 5797813dd6fSViresh Kumar * 5807813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 5817813dd6fSViresh Kumar * use. 5827813dd6fSViresh Kumar */ 5837813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 5847813dd6fSViresh Kumar unsigned long *freq) 5857813dd6fSViresh Kumar { 5867813dd6fSViresh Kumar struct opp_table *opp_table; 5877813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 5887813dd6fSViresh Kumar 5897813dd6fSViresh Kumar if (!dev || !freq) { 5907813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 5917813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 5927813dd6fSViresh Kumar } 5937813dd6fSViresh Kumar 5947813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 5957813dd6fSViresh Kumar if (IS_ERR(opp_table)) 5967813dd6fSViresh Kumar return ERR_CAST(opp_table); 5977813dd6fSViresh Kumar 5987813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 5997813dd6fSViresh Kumar 6007813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 6017813dd6fSViresh Kumar if (temp_opp->available) { 6027813dd6fSViresh Kumar /* go to the next node, before choosing prev */ 6037813dd6fSViresh Kumar if (temp_opp->rate > *freq) 6047813dd6fSViresh Kumar break; 6057813dd6fSViresh Kumar else 6067813dd6fSViresh Kumar opp = temp_opp; 6077813dd6fSViresh Kumar } 6087813dd6fSViresh Kumar } 6097813dd6fSViresh Kumar 6107813dd6fSViresh Kumar /* Increment the reference count of OPP */ 6117813dd6fSViresh Kumar if (!IS_ERR(opp)) 6127813dd6fSViresh Kumar dev_pm_opp_get(opp); 6137813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 6147813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 6157813dd6fSViresh Kumar 6167813dd6fSViresh Kumar if (!IS_ERR(opp)) 6177813dd6fSViresh Kumar *freq = opp->rate; 6187813dd6fSViresh Kumar 6197813dd6fSViresh Kumar return opp; 6207813dd6fSViresh Kumar } 6217813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 6227813dd6fSViresh Kumar 6232f36bde0SAndrew-sh.Cheng /** 6242f36bde0SAndrew-sh.Cheng * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for 6252f36bde0SAndrew-sh.Cheng * target voltage. 6262f36bde0SAndrew-sh.Cheng * @dev: Device for which we do this operation. 6272f36bde0SAndrew-sh.Cheng * @u_volt: Target voltage. 6282f36bde0SAndrew-sh.Cheng * 6292f36bde0SAndrew-sh.Cheng * Search for OPP with highest (ceil) frequency and has voltage <= u_volt. 6302f36bde0SAndrew-sh.Cheng * 6312f36bde0SAndrew-sh.Cheng * Return: matching *opp, else returns ERR_PTR in case of error which should be 6322f36bde0SAndrew-sh.Cheng * handled using IS_ERR. 6332f36bde0SAndrew-sh.Cheng * 6342f36bde0SAndrew-sh.Cheng * Error return values can be: 6352f36bde0SAndrew-sh.Cheng * EINVAL: bad parameters 6362f36bde0SAndrew-sh.Cheng * 6372f36bde0SAndrew-sh.Cheng * The callers are required to call dev_pm_opp_put() for the returned OPP after 6382f36bde0SAndrew-sh.Cheng * use. 6392f36bde0SAndrew-sh.Cheng */ 6402f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev, 6412f36bde0SAndrew-sh.Cheng unsigned long u_volt) 6422f36bde0SAndrew-sh.Cheng { 6432f36bde0SAndrew-sh.Cheng struct opp_table *opp_table; 6442f36bde0SAndrew-sh.Cheng struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 6452f36bde0SAndrew-sh.Cheng 6462f36bde0SAndrew-sh.Cheng if (!dev || !u_volt) { 6472f36bde0SAndrew-sh.Cheng dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__, 6482f36bde0SAndrew-sh.Cheng u_volt); 6492f36bde0SAndrew-sh.Cheng return ERR_PTR(-EINVAL); 6502f36bde0SAndrew-sh.Cheng } 6512f36bde0SAndrew-sh.Cheng 6522f36bde0SAndrew-sh.Cheng opp_table = _find_opp_table(dev); 6532f36bde0SAndrew-sh.Cheng if (IS_ERR(opp_table)) 6542f36bde0SAndrew-sh.Cheng return ERR_CAST(opp_table); 6552f36bde0SAndrew-sh.Cheng 6562f36bde0SAndrew-sh.Cheng mutex_lock(&opp_table->lock); 6572f36bde0SAndrew-sh.Cheng 6582f36bde0SAndrew-sh.Cheng list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 6592f36bde0SAndrew-sh.Cheng if (temp_opp->available) { 6602f36bde0SAndrew-sh.Cheng if (temp_opp->supplies[0].u_volt > u_volt) 6612f36bde0SAndrew-sh.Cheng break; 6622f36bde0SAndrew-sh.Cheng opp = temp_opp; 6632f36bde0SAndrew-sh.Cheng } 6642f36bde0SAndrew-sh.Cheng } 6652f36bde0SAndrew-sh.Cheng 6662f36bde0SAndrew-sh.Cheng /* Increment the reference count of OPP */ 6672f36bde0SAndrew-sh.Cheng if (!IS_ERR(opp)) 6682f36bde0SAndrew-sh.Cheng dev_pm_opp_get(opp); 6692f36bde0SAndrew-sh.Cheng 6702f36bde0SAndrew-sh.Cheng mutex_unlock(&opp_table->lock); 6712f36bde0SAndrew-sh.Cheng dev_pm_opp_put_opp_table(opp_table); 6722f36bde0SAndrew-sh.Cheng 6732f36bde0SAndrew-sh.Cheng return opp; 6742f36bde0SAndrew-sh.Cheng } 6752f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt); 6762f36bde0SAndrew-sh.Cheng 6777813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 6787813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 6797813dd6fSViresh Kumar { 6807813dd6fSViresh Kumar int ret; 6817813dd6fSViresh Kumar 6827813dd6fSViresh Kumar /* Regulator not available for device */ 6837813dd6fSViresh Kumar if (IS_ERR(reg)) { 6847813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 6857813dd6fSViresh Kumar PTR_ERR(reg)); 6867813dd6fSViresh Kumar return 0; 6877813dd6fSViresh Kumar } 6887813dd6fSViresh Kumar 6897813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 6907813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 6917813dd6fSViresh Kumar 6927813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 6937813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 6947813dd6fSViresh Kumar if (ret) 6957813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 6967813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 6977813dd6fSViresh Kumar supply->u_volt_max, ret); 6987813dd6fSViresh Kumar 6997813dd6fSViresh Kumar return ret; 7007813dd6fSViresh Kumar } 7017813dd6fSViresh Kumar 702285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 703285881b5SViresh Kumar unsigned long freq) 7047813dd6fSViresh Kumar { 7057813dd6fSViresh Kumar int ret; 7067813dd6fSViresh Kumar 7077813dd6fSViresh Kumar ret = clk_set_rate(clk, freq); 7087813dd6fSViresh Kumar if (ret) { 7097813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 7107813dd6fSViresh Kumar ret); 7117813dd6fSViresh Kumar } 7127813dd6fSViresh Kumar 7137813dd6fSViresh Kumar return ret; 7147813dd6fSViresh Kumar } 7157813dd6fSViresh Kumar 7168d45719cSKamil Konieczny static int _generic_set_opp_regulator(struct opp_table *opp_table, 7177813dd6fSViresh Kumar struct device *dev, 7187813dd6fSViresh Kumar unsigned long old_freq, 7197813dd6fSViresh Kumar unsigned long freq, 7207813dd6fSViresh Kumar struct dev_pm_opp_supply *old_supply, 7217813dd6fSViresh Kumar struct dev_pm_opp_supply *new_supply) 7227813dd6fSViresh Kumar { 7237813dd6fSViresh Kumar struct regulator *reg = opp_table->regulators[0]; 7247813dd6fSViresh Kumar int ret; 7257813dd6fSViresh Kumar 7267813dd6fSViresh Kumar /* This function only supports single regulator per device */ 7277813dd6fSViresh Kumar if (WARN_ON(opp_table->regulator_count > 1)) { 7287813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 7297813dd6fSViresh Kumar return -EINVAL; 7307813dd6fSViresh Kumar } 7317813dd6fSViresh Kumar 7327813dd6fSViresh Kumar /* Scaling up? Scale voltage before frequency */ 733c5c2a97bSWaldemar Rymarkiewicz if (freq >= old_freq) { 7347813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 7357813dd6fSViresh Kumar if (ret) 7367813dd6fSViresh Kumar goto restore_voltage; 7377813dd6fSViresh Kumar } 7387813dd6fSViresh Kumar 7397813dd6fSViresh Kumar /* Change frequency */ 740285881b5SViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 7417813dd6fSViresh Kumar if (ret) 7427813dd6fSViresh Kumar goto restore_voltage; 7437813dd6fSViresh Kumar 7447813dd6fSViresh Kumar /* Scaling down? Scale voltage after frequency */ 7457813dd6fSViresh Kumar if (freq < old_freq) { 7467813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 7477813dd6fSViresh Kumar if (ret) 7487813dd6fSViresh Kumar goto restore_freq; 7497813dd6fSViresh Kumar } 7507813dd6fSViresh Kumar 7518d45719cSKamil Konieczny /* 7528d45719cSKamil Konieczny * Enable the regulator after setting its voltages, otherwise it breaks 7538d45719cSKamil Konieczny * some boot-enabled regulators. 7548d45719cSKamil Konieczny */ 75572f80ce4SViresh Kumar if (unlikely(!opp_table->enabled)) { 7568d45719cSKamil Konieczny ret = regulator_enable(reg); 7578d45719cSKamil Konieczny if (ret < 0) 7588d45719cSKamil Konieczny dev_warn(dev, "Failed to enable regulator: %d", ret); 7598d45719cSKamil Konieczny } 7608d45719cSKamil Konieczny 7617813dd6fSViresh Kumar return 0; 7627813dd6fSViresh Kumar 7637813dd6fSViresh Kumar restore_freq: 764285881b5SViresh Kumar if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq)) 7657813dd6fSViresh Kumar dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 7667813dd6fSViresh Kumar __func__, old_freq); 7677813dd6fSViresh Kumar restore_voltage: 7687813dd6fSViresh Kumar /* This shouldn't harm even if the voltages weren't updated earlier */ 7697813dd6fSViresh Kumar if (old_supply) 7707813dd6fSViresh Kumar _set_opp_voltage(dev, reg, old_supply); 7717813dd6fSViresh Kumar 7727813dd6fSViresh Kumar return ret; 7737813dd6fSViresh Kumar } 7747813dd6fSViresh Kumar 775b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table, 776b00e667aSViresh Kumar struct dev_pm_opp *opp, struct device *dev, bool remove) 777b00e667aSViresh Kumar { 778b00e667aSViresh Kumar u32 avg, peak; 779b00e667aSViresh Kumar int i, ret; 780b00e667aSViresh Kumar 781b00e667aSViresh Kumar if (!opp_table->paths) 782b00e667aSViresh Kumar return 0; 783b00e667aSViresh Kumar 784b00e667aSViresh Kumar for (i = 0; i < opp_table->path_count; i++) { 785b00e667aSViresh Kumar if (remove) { 786b00e667aSViresh Kumar avg = 0; 787b00e667aSViresh Kumar peak = 0; 788b00e667aSViresh Kumar } else { 789b00e667aSViresh Kumar avg = opp->bandwidth[i].avg; 790b00e667aSViresh Kumar peak = opp->bandwidth[i].peak; 791b00e667aSViresh Kumar } 792b00e667aSViresh Kumar ret = icc_set_bw(opp_table->paths[i], avg, peak); 793b00e667aSViresh Kumar if (ret) { 794b00e667aSViresh Kumar dev_err(dev, "Failed to %s bandwidth[%d]: %d\n", 795b00e667aSViresh Kumar remove ? "remove" : "set", i, ret); 796b00e667aSViresh Kumar return ret; 797b00e667aSViresh Kumar } 798b00e667aSViresh Kumar } 799b00e667aSViresh Kumar 800b00e667aSViresh Kumar return 0; 801b00e667aSViresh Kumar } 802b00e667aSViresh Kumar 8037e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table, 8047e535993SViresh Kumar struct device *dev, unsigned long old_freq, 8057e535993SViresh Kumar unsigned long freq, 8067e535993SViresh Kumar struct dev_pm_opp_supply *old_supply, 8077e535993SViresh Kumar struct dev_pm_opp_supply *new_supply) 8087e535993SViresh Kumar { 8097e535993SViresh Kumar struct dev_pm_set_opp_data *data; 8107e535993SViresh Kumar int size; 8117e535993SViresh Kumar 8127e535993SViresh Kumar data = opp_table->set_opp_data; 8137e535993SViresh Kumar data->regulators = opp_table->regulators; 8147e535993SViresh Kumar data->regulator_count = opp_table->regulator_count; 8157e535993SViresh Kumar data->clk = opp_table->clk; 8167e535993SViresh Kumar data->dev = dev; 8177e535993SViresh Kumar 8187e535993SViresh Kumar data->old_opp.rate = old_freq; 8197e535993SViresh Kumar size = sizeof(*old_supply) * opp_table->regulator_count; 820560d1bcaSDmitry Osipenko if (!old_supply) 8217e535993SViresh Kumar memset(data->old_opp.supplies, 0, size); 8227e535993SViresh Kumar else 8237e535993SViresh Kumar memcpy(data->old_opp.supplies, old_supply, size); 8247e535993SViresh Kumar 8257e535993SViresh Kumar data->new_opp.rate = freq; 8267e535993SViresh Kumar memcpy(data->new_opp.supplies, new_supply, size); 8277e535993SViresh Kumar 8287e535993SViresh Kumar return opp_table->set_opp(data); 8297e535993SViresh Kumar } 8307e535993SViresh Kumar 83160cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev, 83260cdeae0SStephan Gerhold struct dev_pm_opp *opp, int i) 83360cdeae0SStephan Gerhold { 83460cdeae0SStephan Gerhold unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0; 83560cdeae0SStephan Gerhold int ret; 83660cdeae0SStephan Gerhold 83760cdeae0SStephan Gerhold if (!pd_dev) 83860cdeae0SStephan Gerhold return 0; 83960cdeae0SStephan Gerhold 84060cdeae0SStephan Gerhold ret = dev_pm_genpd_set_performance_state(pd_dev, pstate); 84160cdeae0SStephan Gerhold if (ret) { 84260cdeae0SStephan Gerhold dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n", 84360cdeae0SStephan Gerhold dev_name(pd_dev), pstate, ret); 84460cdeae0SStephan Gerhold } 84560cdeae0SStephan Gerhold 84660cdeae0SStephan Gerhold return ret; 84760cdeae0SStephan Gerhold } 84860cdeae0SStephan Gerhold 849ca1b5d77SViresh Kumar /* This is only called for PM domain for now */ 850ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev, 851ca1b5d77SViresh Kumar struct opp_table *opp_table, 8522c59138cSStephan Gerhold struct dev_pm_opp *opp, bool up) 853ca1b5d77SViresh Kumar { 854ca1b5d77SViresh Kumar struct opp_table **required_opp_tables = opp_table->required_opp_tables; 855ca1b5d77SViresh Kumar struct device **genpd_virt_devs = opp_table->genpd_virt_devs; 856ca1b5d77SViresh Kumar int i, ret = 0; 857ca1b5d77SViresh Kumar 858ca1b5d77SViresh Kumar if (!required_opp_tables) 859ca1b5d77SViresh Kumar return 0; 860ca1b5d77SViresh Kumar 861ca1b5d77SViresh Kumar /* Single genpd case */ 86260cdeae0SStephan Gerhold if (!genpd_virt_devs) 86360cdeae0SStephan Gerhold return _set_required_opp(dev, dev, opp, 0); 864ca1b5d77SViresh Kumar 865ca1b5d77SViresh Kumar /* Multiple genpd case */ 866ca1b5d77SViresh Kumar 867ca1b5d77SViresh Kumar /* 868ca1b5d77SViresh Kumar * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 869ca1b5d77SViresh Kumar * after it is freed from another thread. 870ca1b5d77SViresh Kumar */ 871ca1b5d77SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 872ca1b5d77SViresh Kumar 8732c59138cSStephan Gerhold /* Scaling up? Set required OPPs in normal order, else reverse */ 8742c59138cSStephan Gerhold if (up) { 875ca1b5d77SViresh Kumar for (i = 0; i < opp_table->required_opp_count; i++) { 87660cdeae0SStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 87760cdeae0SStephan Gerhold if (ret) 878ca1b5d77SViresh Kumar break; 879ca1b5d77SViresh Kumar } 8802c59138cSStephan Gerhold } else { 8812c59138cSStephan Gerhold for (i = opp_table->required_opp_count - 1; i >= 0; i--) { 8822c59138cSStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 8832c59138cSStephan Gerhold if (ret) 884ca1b5d77SViresh Kumar break; 885ca1b5d77SViresh Kumar } 886ca1b5d77SViresh Kumar } 8872c59138cSStephan Gerhold 888ca1b5d77SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 889ca1b5d77SViresh Kumar 890ca1b5d77SViresh Kumar return ret; 891ca1b5d77SViresh Kumar } 892ca1b5d77SViresh Kumar 8937813dd6fSViresh Kumar /** 8943ae1f39aSSibi Sankar * dev_pm_opp_set_bw() - sets bandwidth levels corresponding to an opp 8953ae1f39aSSibi Sankar * @dev: device for which we do this operation 8963ae1f39aSSibi Sankar * @opp: opp based on which the bandwidth levels are to be configured 8973ae1f39aSSibi Sankar * 8983ae1f39aSSibi Sankar * This configures the bandwidth to the levels specified by the OPP. However 8993ae1f39aSSibi Sankar * if the OPP specified is NULL the bandwidth levels are cleared out. 9003ae1f39aSSibi Sankar * 9013ae1f39aSSibi Sankar * Return: 0 on success or a negative error value. 9023ae1f39aSSibi Sankar */ 9033ae1f39aSSibi Sankar int dev_pm_opp_set_bw(struct device *dev, struct dev_pm_opp *opp) 9043ae1f39aSSibi Sankar { 9053ae1f39aSSibi Sankar struct opp_table *opp_table; 9063ae1f39aSSibi Sankar int ret; 9073ae1f39aSSibi Sankar 9083ae1f39aSSibi Sankar opp_table = _find_opp_table(dev); 9093ae1f39aSSibi Sankar if (IS_ERR(opp_table)) { 9103ae1f39aSSibi Sankar dev_err(dev, "%s: device opp table doesn't exist\n", __func__); 9113ae1f39aSSibi Sankar return PTR_ERR(opp_table); 9123ae1f39aSSibi Sankar } 9133ae1f39aSSibi Sankar 9143ae1f39aSSibi Sankar if (opp) 9153ae1f39aSSibi Sankar ret = _set_opp_bw(opp_table, opp, dev, false); 9163ae1f39aSSibi Sankar else 9173ae1f39aSSibi Sankar ret = _set_opp_bw(opp_table, NULL, dev, true); 9183ae1f39aSSibi Sankar 9193ae1f39aSSibi Sankar dev_pm_opp_put_opp_table(opp_table); 9203ae1f39aSSibi Sankar return ret; 9213ae1f39aSSibi Sankar } 9223ae1f39aSSibi Sankar EXPORT_SYMBOL_GPL(dev_pm_opp_set_bw); 9233ae1f39aSSibi Sankar 924f3364e17SViresh Kumar static int _opp_set_rate_zero(struct device *dev, struct opp_table *opp_table) 925f3364e17SViresh Kumar { 926f3364e17SViresh Kumar int ret; 927f3364e17SViresh Kumar 928f3364e17SViresh Kumar if (!opp_table->enabled) 929f3364e17SViresh Kumar return 0; 930f3364e17SViresh Kumar 931f3364e17SViresh Kumar /* 932f3364e17SViresh Kumar * Some drivers need to support cases where some platforms may 933f3364e17SViresh Kumar * have OPP table for the device, while others don't and 934f3364e17SViresh Kumar * opp_set_rate() just needs to behave like clk_set_rate(). 935f3364e17SViresh Kumar */ 936f3364e17SViresh Kumar if (!_get_opp_count(opp_table)) 937f3364e17SViresh Kumar return 0; 938f3364e17SViresh Kumar 939f3364e17SViresh Kumar ret = _set_opp_bw(opp_table, NULL, dev, true); 940f3364e17SViresh Kumar if (ret) 941f3364e17SViresh Kumar return ret; 942f3364e17SViresh Kumar 943f3364e17SViresh Kumar if (opp_table->regulators) 944f3364e17SViresh Kumar regulator_disable(opp_table->regulators[0]); 945f3364e17SViresh Kumar 9462c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, NULL, false); 947f3364e17SViresh Kumar 948f3364e17SViresh Kumar opp_table->enabled = false; 949f3364e17SViresh Kumar return ret; 950f3364e17SViresh Kumar } 951f3364e17SViresh Kumar 9523ae1f39aSSibi Sankar /** 9537813dd6fSViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 9547813dd6fSViresh Kumar * @dev: device for which we do this operation 9557813dd6fSViresh Kumar * @target_freq: frequency to achieve 9567813dd6fSViresh Kumar * 957b3e3759eSStephen Boyd * This configures the power-supplies to the levels specified by the OPP 958b3e3759eSStephen Boyd * corresponding to the target_freq, and programs the clock to a value <= 959b3e3759eSStephen Boyd * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax 960b3e3759eSStephen Boyd * provided by the opp, should have already rounded to the target OPP's 961b3e3759eSStephen Boyd * frequency. 9627813dd6fSViresh Kumar */ 9637813dd6fSViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 9647813dd6fSViresh Kumar { 9657813dd6fSViresh Kumar struct opp_table *opp_table; 966b3e3759eSStephen Boyd unsigned long freq, old_freq, temp_freq; 9677813dd6fSViresh Kumar struct dev_pm_opp *old_opp, *opp; 9687813dd6fSViresh Kumar struct clk *clk; 969b00e667aSViresh Kumar int ret; 9707813dd6fSViresh Kumar 9717813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 9727813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 9737813dd6fSViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 9747813dd6fSViresh Kumar return PTR_ERR(opp_table); 9757813dd6fSViresh Kumar } 9767813dd6fSViresh Kumar 977cd7ea582SRajendra Nayak if (unlikely(!target_freq)) { 978f3364e17SViresh Kumar ret = _opp_set_rate_zero(dev, opp_table); 979cd7ea582SRajendra Nayak goto put_opp_table; 980cd7ea582SRajendra Nayak } 981cd7ea582SRajendra Nayak 9827813dd6fSViresh Kumar clk = opp_table->clk; 9837813dd6fSViresh Kumar if (IS_ERR(clk)) { 9847813dd6fSViresh Kumar dev_err(dev, "%s: No clock available for the device\n", 9857813dd6fSViresh Kumar __func__); 9867813dd6fSViresh Kumar ret = PTR_ERR(clk); 9877813dd6fSViresh Kumar goto put_opp_table; 9887813dd6fSViresh Kumar } 9897813dd6fSViresh Kumar 9907813dd6fSViresh Kumar freq = clk_round_rate(clk, target_freq); 9917813dd6fSViresh Kumar if ((long)freq <= 0) 9927813dd6fSViresh Kumar freq = target_freq; 9937813dd6fSViresh Kumar 9947813dd6fSViresh Kumar old_freq = clk_get_rate(clk); 9957813dd6fSViresh Kumar 9967813dd6fSViresh Kumar /* Return early if nothing to do */ 99710b21736SViresh Kumar if (opp_table->enabled && old_freq == freq) { 9987813dd6fSViresh Kumar dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", 9997813dd6fSViresh Kumar __func__, freq); 10007813dd6fSViresh Kumar ret = 0; 10017813dd6fSViresh Kumar goto put_opp_table; 10027813dd6fSViresh Kumar } 10037813dd6fSViresh Kumar 1004aca48b61SRajendra Nayak /* 1005aca48b61SRajendra Nayak * For IO devices which require an OPP on some platforms/SoCs 1006aca48b61SRajendra Nayak * while just needing to scale the clock on some others 1007aca48b61SRajendra Nayak * we look for empty OPP tables with just a clock handle and 1008aca48b61SRajendra Nayak * scale only the clk. This makes dev_pm_opp_set_rate() 1009aca48b61SRajendra Nayak * equivalent to a clk_set_rate() 1010aca48b61SRajendra Nayak */ 1011aca48b61SRajendra Nayak if (!_get_opp_count(opp_table)) { 1012aca48b61SRajendra Nayak ret = _generic_set_opp_clk_only(dev, clk, freq); 1013aca48b61SRajendra Nayak goto put_opp_table; 1014aca48b61SRajendra Nayak } 1015aca48b61SRajendra Nayak 1016b3e3759eSStephen Boyd temp_freq = old_freq; 1017b3e3759eSStephen Boyd old_opp = _find_freq_ceil(opp_table, &temp_freq); 10187813dd6fSViresh Kumar if (IS_ERR(old_opp)) { 10197813dd6fSViresh Kumar dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", 10207813dd6fSViresh Kumar __func__, old_freq, PTR_ERR(old_opp)); 10217813dd6fSViresh Kumar } 10227813dd6fSViresh Kumar 1023b3e3759eSStephen Boyd temp_freq = freq; 1024b3e3759eSStephen Boyd opp = _find_freq_ceil(opp_table, &temp_freq); 10257813dd6fSViresh Kumar if (IS_ERR(opp)) { 10267813dd6fSViresh Kumar ret = PTR_ERR(opp); 10277813dd6fSViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 10287813dd6fSViresh Kumar __func__, freq, ret); 10297813dd6fSViresh Kumar goto put_old_opp; 10307813dd6fSViresh Kumar } 10317813dd6fSViresh Kumar 10327813dd6fSViresh Kumar dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, 10337813dd6fSViresh Kumar old_freq, freq); 10347813dd6fSViresh Kumar 1035ca1b5d77SViresh Kumar /* Scaling up? Configure required OPPs before frequency */ 1036faef080fSViresh Kumar if (freq >= old_freq) { 10372c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, opp, true); 1038ca1b5d77SViresh Kumar if (ret) 1039ca1b5d77SViresh Kumar goto put_opp; 1040ca1b5d77SViresh Kumar } 1041ca1b5d77SViresh Kumar 10427e535993SViresh Kumar if (opp_table->set_opp) { 10437e535993SViresh Kumar ret = _set_opp_custom(opp_table, dev, old_freq, freq, 10447e535993SViresh Kumar IS_ERR(old_opp) ? NULL : old_opp->supplies, 10457e535993SViresh Kumar opp->supplies); 10467e535993SViresh Kumar } else if (opp_table->regulators) { 10477813dd6fSViresh Kumar ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq, 10487813dd6fSViresh Kumar IS_ERR(old_opp) ? NULL : old_opp->supplies, 10497813dd6fSViresh Kumar opp->supplies); 10507813dd6fSViresh Kumar } else { 10517813dd6fSViresh Kumar /* Only frequency scaling */ 1052285881b5SViresh Kumar ret = _generic_set_opp_clk_only(dev, clk, freq); 10537813dd6fSViresh Kumar } 10547813dd6fSViresh Kumar 1055ca1b5d77SViresh Kumar /* Scaling down? Configure required OPPs after frequency */ 1056ca1b5d77SViresh Kumar if (!ret && freq < old_freq) { 10572c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, opp, false); 1058ca1b5d77SViresh Kumar if (ret) 1059ca1b5d77SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1060ca1b5d77SViresh Kumar } 1061ca1b5d77SViresh Kumar 106272f80ce4SViresh Kumar if (!ret) { 1063b00e667aSViresh Kumar ret = _set_opp_bw(opp_table, opp, dev, false); 106472f80ce4SViresh Kumar if (!ret) 106572f80ce4SViresh Kumar opp_table->enabled = true; 106672f80ce4SViresh Kumar } 1067fe2af402SGeorgi Djakov 1068ca1b5d77SViresh Kumar put_opp: 10697813dd6fSViresh Kumar dev_pm_opp_put(opp); 10707813dd6fSViresh Kumar put_old_opp: 10717813dd6fSViresh Kumar if (!IS_ERR(old_opp)) 10727813dd6fSViresh Kumar dev_pm_opp_put(old_opp); 10737813dd6fSViresh Kumar put_opp_table: 10747813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 10757813dd6fSViresh Kumar return ret; 10767813dd6fSViresh Kumar } 10777813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 10787813dd6fSViresh Kumar 10797813dd6fSViresh Kumar /* OPP-dev Helpers */ 10807813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 10817813dd6fSViresh Kumar struct opp_table *opp_table) 10827813dd6fSViresh Kumar { 10837813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 10847813dd6fSViresh Kumar list_del(&opp_dev->node); 10857813dd6fSViresh Kumar kfree(opp_dev); 10867813dd6fSViresh Kumar } 10877813dd6fSViresh Kumar 1088ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 10897813dd6fSViresh Kumar struct opp_table *opp_table) 10907813dd6fSViresh Kumar { 10917813dd6fSViresh Kumar struct opp_device *opp_dev; 10927813dd6fSViresh Kumar 10937813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 10947813dd6fSViresh Kumar if (!opp_dev) 10957813dd6fSViresh Kumar return NULL; 10967813dd6fSViresh Kumar 10977813dd6fSViresh Kumar /* Initialize opp-dev */ 10987813dd6fSViresh Kumar opp_dev->dev = dev; 10993d255699SViresh Kumar 1100ef43f01aSViresh Kumar mutex_lock(&opp_table->lock); 11017813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 1102ef43f01aSViresh Kumar mutex_unlock(&opp_table->lock); 11037813dd6fSViresh Kumar 11047813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 1105a2dea4cbSGreg Kroah-Hartman opp_debug_register(opp_dev, opp_table); 1106283d55e6SViresh Kumar 1107283d55e6SViresh Kumar return opp_dev; 1108283d55e6SViresh Kumar } 1109283d55e6SViresh Kumar 1110eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index) 11117813dd6fSViresh Kumar { 11127813dd6fSViresh Kumar struct opp_table *opp_table; 11137813dd6fSViresh Kumar struct opp_device *opp_dev; 11147813dd6fSViresh Kumar int ret; 11157813dd6fSViresh Kumar 11167813dd6fSViresh Kumar /* 11177813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 11187813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 11197813dd6fSViresh Kumar */ 11207813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 11217813dd6fSViresh Kumar if (!opp_table) 1122dd461cd9SStephan Gerhold return ERR_PTR(-ENOMEM); 11237813dd6fSViresh Kumar 11243d255699SViresh Kumar mutex_init(&opp_table->lock); 11254f018bc0SViresh Kumar mutex_init(&opp_table->genpd_virt_dev_lock); 11267813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 11277813dd6fSViresh Kumar 112846f48acaSViresh Kumar /* Mark regulator count uninitialized */ 112946f48acaSViresh Kumar opp_table->regulator_count = -1; 113046f48acaSViresh Kumar 11317813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 11327813dd6fSViresh Kumar if (!opp_dev) { 1133dd461cd9SStephan Gerhold ret = -ENOMEM; 1134dd461cd9SStephan Gerhold goto err; 11357813dd6fSViresh Kumar } 11367813dd6fSViresh Kumar 1137eb7c8743SViresh Kumar _of_init_opp_table(opp_table, dev, index); 11387813dd6fSViresh Kumar 11397813dd6fSViresh Kumar /* Find clk for the device */ 11407813dd6fSViresh Kumar opp_table->clk = clk_get(dev, NULL); 11417813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 11427813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 1143dd461cd9SStephan Gerhold if (ret == -EPROBE_DEFER) 1144976509bbSQuanyang Wang goto remove_opp_dev; 1145dd461cd9SStephan Gerhold 1146dd461cd9SStephan Gerhold dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret); 11477813dd6fSViresh Kumar } 11487813dd6fSViresh Kumar 11496d3f922cSGeorgi Djakov /* Find interconnect path(s) for the device */ 11506d3f922cSGeorgi Djakov ret = dev_pm_opp_of_find_icc_paths(dev, opp_table); 1151dd461cd9SStephan Gerhold if (ret) { 1152dd461cd9SStephan Gerhold if (ret == -EPROBE_DEFER) 11530e1d9ca1SViresh Kumar goto put_clk; 1154dd461cd9SStephan Gerhold 11556d3f922cSGeorgi Djakov dev_warn(dev, "%s: Error finding interconnect paths: %d\n", 11566d3f922cSGeorgi Djakov __func__, ret); 1157dd461cd9SStephan Gerhold } 11586d3f922cSGeorgi Djakov 11597813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 11607813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 11617813dd6fSViresh Kumar kref_init(&opp_table->kref); 11627813dd6fSViresh Kumar 11637813dd6fSViresh Kumar return opp_table; 1164dd461cd9SStephan Gerhold 11650e1d9ca1SViresh Kumar put_clk: 11660e1d9ca1SViresh Kumar if (!IS_ERR(opp_table->clk)) 11670e1d9ca1SViresh Kumar clk_put(opp_table->clk); 1168976509bbSQuanyang Wang remove_opp_dev: 1169976509bbSQuanyang Wang _remove_opp_dev(opp_dev, opp_table); 1170dd461cd9SStephan Gerhold err: 1171dd461cd9SStephan Gerhold kfree(opp_table); 1172dd461cd9SStephan Gerhold return ERR_PTR(ret); 11737813dd6fSViresh Kumar } 11747813dd6fSViresh Kumar 11757813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 11767813dd6fSViresh Kumar { 11777813dd6fSViresh Kumar kref_get(&opp_table->kref); 11787813dd6fSViresh Kumar } 11797813dd6fSViresh Kumar 118027c09484SViresh Kumar /* 118127c09484SViresh Kumar * We need to make sure that the OPP table for a device doesn't get added twice, 118227c09484SViresh Kumar * if this routine gets called in parallel with the same device pointer. 118327c09484SViresh Kumar * 118427c09484SViresh Kumar * The simplest way to enforce that is to perform everything (find existing 118527c09484SViresh Kumar * table and if not found, create a new one) under the opp_table_lock, so only 118627c09484SViresh Kumar * one creator gets access to the same. But that expands the critical section 118727c09484SViresh Kumar * under the lock and may end up causing circular dependencies with frameworks 118827c09484SViresh Kumar * like debugfs, interconnect or clock framework as they may be direct or 118927c09484SViresh Kumar * indirect users of OPP core. 119027c09484SViresh Kumar * 119127c09484SViresh Kumar * And for that reason we have to go for a bit tricky implementation here, which 119227c09484SViresh Kumar * uses the opp_tables_busy flag to indicate if another creator is in the middle 119327c09484SViresh Kumar * of adding an OPP table and others should wait for it to finish. 119427c09484SViresh Kumar */ 1195e77dcb0bSViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index) 11967813dd6fSViresh Kumar { 11977813dd6fSViresh Kumar struct opp_table *opp_table; 11987813dd6fSViresh Kumar 119927c09484SViresh Kumar again: 12007813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 12017813dd6fSViresh Kumar 12027813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 12037813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 12047813dd6fSViresh Kumar goto unlock; 12057813dd6fSViresh Kumar 120627c09484SViresh Kumar /* 120727c09484SViresh Kumar * The opp_tables list or an OPP table's dev_list is getting updated by 120827c09484SViresh Kumar * another user, wait for it to finish. 120927c09484SViresh Kumar */ 121027c09484SViresh Kumar if (unlikely(opp_tables_busy)) { 121127c09484SViresh Kumar mutex_unlock(&opp_table_lock); 121227c09484SViresh Kumar cpu_relax(); 121327c09484SViresh Kumar goto again; 121427c09484SViresh Kumar } 121527c09484SViresh Kumar 121627c09484SViresh Kumar opp_tables_busy = true; 1217283d55e6SViresh Kumar opp_table = _managed_opp(dev, index); 121827c09484SViresh Kumar 121927c09484SViresh Kumar /* Drop the lock to reduce the size of critical section */ 122027c09484SViresh Kumar mutex_unlock(&opp_table_lock); 122127c09484SViresh Kumar 1222283d55e6SViresh Kumar if (opp_table) { 1223ef43f01aSViresh Kumar if (!_add_opp_dev(dev, opp_table)) { 1224283d55e6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1225dd461cd9SStephan Gerhold opp_table = ERR_PTR(-ENOMEM); 1226283d55e6SViresh Kumar } 122727c09484SViresh Kumar 122827c09484SViresh Kumar mutex_lock(&opp_table_lock); 122927c09484SViresh Kumar } else { 123027c09484SViresh Kumar opp_table = _allocate_opp_table(dev, index); 123127c09484SViresh Kumar 123227c09484SViresh Kumar mutex_lock(&opp_table_lock); 123327c09484SViresh Kumar if (!IS_ERR(opp_table)) 123427c09484SViresh Kumar list_add(&opp_table->node, &opp_tables); 1235283d55e6SViresh Kumar } 1236283d55e6SViresh Kumar 123727c09484SViresh Kumar opp_tables_busy = false; 12387813dd6fSViresh Kumar 12397813dd6fSViresh Kumar unlock: 12407813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 12417813dd6fSViresh Kumar 12427813dd6fSViresh Kumar return opp_table; 12437813dd6fSViresh Kumar } 1244eb7c8743SViresh Kumar 1245d758eaf5SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev) 1246e77dcb0bSViresh Kumar { 1247e77dcb0bSViresh Kumar return _add_opp_table_indexed(dev, 0); 1248e77dcb0bSViresh Kumar } 1249e77dcb0bSViresh Kumar 1250eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 1251eb7c8743SViresh Kumar { 1252e77dcb0bSViresh Kumar return _find_opp_table(dev); 1253eb7c8743SViresh Kumar } 12547813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 12557813dd6fSViresh Kumar 12567813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 12577813dd6fSViresh Kumar { 12587813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 1259cdd6ed90SViresh Kumar struct opp_device *opp_dev, *temp; 12606d3f922cSGeorgi Djakov int i; 12617813dd6fSViresh Kumar 1262e0df59deSViresh Kumar /* Drop the lock as soon as we can */ 1263e0df59deSViresh Kumar list_del(&opp_table->node); 1264e0df59deSViresh Kumar mutex_unlock(&opp_table_lock); 1265e0df59deSViresh Kumar 12665d6d106fSViresh Kumar _of_clear_opp_table(opp_table); 12675d6d106fSViresh Kumar 12687813dd6fSViresh Kumar /* Release clk */ 12697813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 12707813dd6fSViresh Kumar clk_put(opp_table->clk); 12717813dd6fSViresh Kumar 12726d3f922cSGeorgi Djakov if (opp_table->paths) { 12736d3f922cSGeorgi Djakov for (i = 0; i < opp_table->path_count; i++) 12746d3f922cSGeorgi Djakov icc_put(opp_table->paths[i]); 12756d3f922cSGeorgi Djakov kfree(opp_table->paths); 12766d3f922cSGeorgi Djakov } 12776d3f922cSGeorgi Djakov 1278cdd6ed90SViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 1279cdd6ed90SViresh Kumar 1280cdd6ed90SViresh Kumar list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 12813d255699SViresh Kumar /* 1282cdd6ed90SViresh Kumar * The OPP table is getting removed, drop the performance state 1283cdd6ed90SViresh Kumar * constraints. 12843d255699SViresh Kumar */ 1285cdd6ed90SViresh Kumar if (opp_table->genpd_performance_state) 1286cdd6ed90SViresh Kumar dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 12877813dd6fSViresh Kumar 12887813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 1289cdd6ed90SViresh Kumar } 12907813dd6fSViresh Kumar 12914f018bc0SViresh Kumar mutex_destroy(&opp_table->genpd_virt_dev_lock); 12927813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 12937813dd6fSViresh Kumar kfree(opp_table); 12947813dd6fSViresh Kumar } 12957813dd6fSViresh Kumar 12967813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 12977813dd6fSViresh Kumar { 12987813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 12997813dd6fSViresh Kumar &opp_table_lock); 13007813dd6fSViresh Kumar } 13017813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 13027813dd6fSViresh Kumar 13037813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 13047813dd6fSViresh Kumar { 13057813dd6fSViresh Kumar kfree(opp); 13067813dd6fSViresh Kumar } 13077813dd6fSViresh Kumar 1308cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref) 13097813dd6fSViresh Kumar { 1310cf1fac94SViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 1311cf1fac94SViresh Kumar struct opp_table *opp_table = opp->opp_table; 1312cf1fac94SViresh Kumar 1313cf1fac94SViresh Kumar list_del(&opp->node); 1314cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1315cf1fac94SViresh Kumar 13167813dd6fSViresh Kumar /* 13177813dd6fSViresh Kumar * Notify the changes in the availability of the operable 13187813dd6fSViresh Kumar * frequency/voltage list. 13197813dd6fSViresh Kumar */ 13207813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 1321da544b61SViresh Kumar _of_opp_free_required_opps(opp_table, opp); 13227813dd6fSViresh Kumar opp_debug_remove_one(opp); 13237813dd6fSViresh Kumar kfree(opp); 13241690d8bbSViresh Kumar } 13257813dd6fSViresh Kumar 1326a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 13277813dd6fSViresh Kumar { 13287813dd6fSViresh Kumar kref_get(&opp->kref); 13297813dd6fSViresh Kumar } 13307813dd6fSViresh Kumar 13317813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 13327813dd6fSViresh Kumar { 1333cf1fac94SViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 13347813dd6fSViresh Kumar } 13357813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 13367813dd6fSViresh Kumar 13377813dd6fSViresh Kumar /** 13387813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 13397813dd6fSViresh Kumar * @dev: device for which we do this operation 13407813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 13417813dd6fSViresh Kumar * 13427813dd6fSViresh Kumar * This function removes an opp from the opp table. 13437813dd6fSViresh Kumar */ 13447813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 13457813dd6fSViresh Kumar { 13467813dd6fSViresh Kumar struct dev_pm_opp *opp; 13477813dd6fSViresh Kumar struct opp_table *opp_table; 13487813dd6fSViresh Kumar bool found = false; 13497813dd6fSViresh Kumar 13507813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 13517813dd6fSViresh Kumar if (IS_ERR(opp_table)) 13527813dd6fSViresh Kumar return; 13537813dd6fSViresh Kumar 13547813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 13557813dd6fSViresh Kumar 13567813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 13577813dd6fSViresh Kumar if (opp->rate == freq) { 13587813dd6fSViresh Kumar found = true; 13597813dd6fSViresh Kumar break; 13607813dd6fSViresh Kumar } 13617813dd6fSViresh Kumar } 13627813dd6fSViresh Kumar 13637813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 13647813dd6fSViresh Kumar 13657813dd6fSViresh Kumar if (found) { 13667813dd6fSViresh Kumar dev_pm_opp_put(opp); 13670ad8c623SViresh Kumar 13680ad8c623SViresh Kumar /* Drop the reference taken by dev_pm_opp_add() */ 13690ad8c623SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 13707813dd6fSViresh Kumar } else { 13717813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 13727813dd6fSViresh Kumar __func__, freq); 13737813dd6fSViresh Kumar } 13747813dd6fSViresh Kumar 13750ad8c623SViresh Kumar /* Drop the reference taken by _find_opp_table() */ 13767813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 13777813dd6fSViresh Kumar } 13787813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 13797813dd6fSViresh Kumar 1380cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table, 1381cf1fac94SViresh Kumar bool dynamic) 1382cf1fac94SViresh Kumar { 1383cf1fac94SViresh Kumar struct dev_pm_opp *opp = NULL, *temp; 1384cf1fac94SViresh Kumar 1385cf1fac94SViresh Kumar mutex_lock(&opp_table->lock); 1386cf1fac94SViresh Kumar list_for_each_entry(temp, &opp_table->opp_list, node) { 1387cf1fac94SViresh Kumar if (dynamic == temp->dynamic) { 1388cf1fac94SViresh Kumar opp = temp; 1389cf1fac94SViresh Kumar break; 1390cf1fac94SViresh Kumar } 1391cf1fac94SViresh Kumar } 1392cf1fac94SViresh Kumar 1393cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1394cf1fac94SViresh Kumar return opp; 1395cf1fac94SViresh Kumar } 1396cf1fac94SViresh Kumar 1397922ff075SViresh Kumar bool _opp_remove_all_static(struct opp_table *opp_table) 139803758d60SViresh Kumar { 1399cf1fac94SViresh Kumar struct dev_pm_opp *opp; 140003758d60SViresh Kumar 140103758d60SViresh Kumar mutex_lock(&opp_table->lock); 140203758d60SViresh Kumar 1403922ff075SViresh Kumar if (!opp_table->parsed_static_opps) { 1404cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1405cf1fac94SViresh Kumar return false; 1406922ff075SViresh Kumar } 1407922ff075SViresh Kumar 1408cf1fac94SViresh Kumar if (--opp_table->parsed_static_opps) { 1409cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1410cf1fac94SViresh Kumar return true; 141103758d60SViresh Kumar } 141203758d60SViresh Kumar 141303758d60SViresh Kumar mutex_unlock(&opp_table->lock); 1414922ff075SViresh Kumar 1415cf1fac94SViresh Kumar /* 1416cf1fac94SViresh Kumar * Can't remove the OPP from under the lock, debugfs removal needs to 1417cf1fac94SViresh Kumar * happen lock less to avoid circular dependency issues. 1418cf1fac94SViresh Kumar */ 1419cf1fac94SViresh Kumar while ((opp = _opp_get_next(opp_table, false))) 1420cf1fac94SViresh Kumar dev_pm_opp_put(opp); 1421cf1fac94SViresh Kumar 1422cf1fac94SViresh Kumar return true; 142303758d60SViresh Kumar } 142403758d60SViresh Kumar 14251690d8bbSViresh Kumar /** 14261690d8bbSViresh Kumar * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 14271690d8bbSViresh Kumar * @dev: device for which we do this operation 14281690d8bbSViresh Kumar * 14291690d8bbSViresh Kumar * This function removes all dynamically created OPPs from the opp table. 14301690d8bbSViresh Kumar */ 14311690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev) 14321690d8bbSViresh Kumar { 14331690d8bbSViresh Kumar struct opp_table *opp_table; 1434cf1fac94SViresh Kumar struct dev_pm_opp *opp; 14351690d8bbSViresh Kumar int count = 0; 14361690d8bbSViresh Kumar 14371690d8bbSViresh Kumar opp_table = _find_opp_table(dev); 14381690d8bbSViresh Kumar if (IS_ERR(opp_table)) 14391690d8bbSViresh Kumar return; 14401690d8bbSViresh Kumar 1441cf1fac94SViresh Kumar /* 1442cf1fac94SViresh Kumar * Can't remove the OPP from under the lock, debugfs removal needs to 1443cf1fac94SViresh Kumar * happen lock less to avoid circular dependency issues. 1444cf1fac94SViresh Kumar */ 1445cf1fac94SViresh Kumar while ((opp = _opp_get_next(opp_table, true))) { 1446cf1fac94SViresh Kumar dev_pm_opp_put(opp); 14471690d8bbSViresh Kumar count++; 14481690d8bbSViresh Kumar } 14491690d8bbSViresh Kumar 14501690d8bbSViresh Kumar /* Drop the references taken by dev_pm_opp_add() */ 14511690d8bbSViresh Kumar while (count--) 14521690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 14531690d8bbSViresh Kumar 14541690d8bbSViresh Kumar /* Drop the reference taken by _find_opp_table() */ 14551690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 14561690d8bbSViresh Kumar } 14571690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 14581690d8bbSViresh Kumar 14597813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table) 14607813dd6fSViresh Kumar { 14617813dd6fSViresh Kumar struct dev_pm_opp *opp; 14626d3f922cSGeorgi Djakov int supply_count, supply_size, icc_size; 14637813dd6fSViresh Kumar 14647813dd6fSViresh Kumar /* Allocate space for at least one supply */ 14656d3f922cSGeorgi Djakov supply_count = table->regulator_count > 0 ? table->regulator_count : 1; 14666d3f922cSGeorgi Djakov supply_size = sizeof(*opp->supplies) * supply_count; 14676d3f922cSGeorgi Djakov icc_size = sizeof(*opp->bandwidth) * table->path_count; 14687813dd6fSViresh Kumar 14697813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 14706d3f922cSGeorgi Djakov opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL); 14716d3f922cSGeorgi Djakov 14727813dd6fSViresh Kumar if (!opp) 14737813dd6fSViresh Kumar return NULL; 14747813dd6fSViresh Kumar 14757813dd6fSViresh Kumar /* Put the supplies at the end of the OPP structure as an empty array */ 14767813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 14776d3f922cSGeorgi Djakov if (icc_size) 14786d3f922cSGeorgi Djakov opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count); 14797813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 14807813dd6fSViresh Kumar 14817813dd6fSViresh Kumar return opp; 14827813dd6fSViresh Kumar } 14837813dd6fSViresh Kumar 14847813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 14857813dd6fSViresh Kumar struct opp_table *opp_table) 14867813dd6fSViresh Kumar { 14877813dd6fSViresh Kumar struct regulator *reg; 14887813dd6fSViresh Kumar int i; 14897813dd6fSViresh Kumar 149090e3577bSViresh Kumar if (!opp_table->regulators) 149190e3577bSViresh Kumar return true; 149290e3577bSViresh Kumar 14937813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 14947813dd6fSViresh Kumar reg = opp_table->regulators[i]; 14957813dd6fSViresh Kumar 14967813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 14977813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 14987813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 14997813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 15007813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 15017813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 15027813dd6fSViresh Kumar return false; 15037813dd6fSViresh Kumar } 15047813dd6fSViresh Kumar } 15057813dd6fSViresh Kumar 15067813dd6fSViresh Kumar return true; 15077813dd6fSViresh Kumar } 15087813dd6fSViresh Kumar 15096c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2) 15106c591eecSSaravana Kannan { 15116c591eecSSaravana Kannan if (opp1->rate != opp2->rate) 15126c591eecSSaravana Kannan return opp1->rate < opp2->rate ? -1 : 1; 15136d3f922cSGeorgi Djakov if (opp1->bandwidth && opp2->bandwidth && 15146d3f922cSGeorgi Djakov opp1->bandwidth[0].peak != opp2->bandwidth[0].peak) 15156d3f922cSGeorgi Djakov return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1; 15166c591eecSSaravana Kannan if (opp1->level != opp2->level) 15176c591eecSSaravana Kannan return opp1->level < opp2->level ? -1 : 1; 15186c591eecSSaravana Kannan return 0; 15196c591eecSSaravana Kannan } 15206c591eecSSaravana Kannan 1521a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1522a1e8c136SViresh Kumar struct opp_table *opp_table, 1523a1e8c136SViresh Kumar struct list_head **head) 1524a1e8c136SViresh Kumar { 1525a1e8c136SViresh Kumar struct dev_pm_opp *opp; 15266c591eecSSaravana Kannan int opp_cmp; 1527a1e8c136SViresh Kumar 1528a1e8c136SViresh Kumar /* 1529a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1530a1e8c136SViresh Kumar * already present. 1531a1e8c136SViresh Kumar * 1532a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1533a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1534a1e8c136SViresh Kumar * loop. 1535a1e8c136SViresh Kumar */ 1536a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 15376c591eecSSaravana Kannan opp_cmp = _opp_compare_key(new_opp, opp); 15386c591eecSSaravana Kannan if (opp_cmp > 0) { 1539a1e8c136SViresh Kumar *head = &opp->node; 1540a1e8c136SViresh Kumar continue; 1541a1e8c136SViresh Kumar } 1542a1e8c136SViresh Kumar 15436c591eecSSaravana Kannan if (opp_cmp < 0) 1544a1e8c136SViresh Kumar return 0; 1545a1e8c136SViresh Kumar 1546a1e8c136SViresh Kumar /* Duplicate OPPs */ 1547a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1548a1e8c136SViresh Kumar __func__, opp->rate, opp->supplies[0].u_volt, 1549a1e8c136SViresh Kumar opp->available, new_opp->rate, 1550a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1551a1e8c136SViresh Kumar 1552a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1553a1e8c136SViresh Kumar return opp->available && 1554a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1555a1e8c136SViresh Kumar } 1556a1e8c136SViresh Kumar 1557a1e8c136SViresh Kumar return 0; 1558a1e8c136SViresh Kumar } 1559a1e8c136SViresh Kumar 15607813dd6fSViresh Kumar /* 15617813dd6fSViresh Kumar * Returns: 15627813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 15637813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 15647813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 15657813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 15667813dd6fSViresh Kumar * kernel try to initialize the OPP table. 15677813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 15687813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 15697813dd6fSViresh Kumar */ 15707813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1571a1e8c136SViresh Kumar struct opp_table *opp_table, bool rate_not_available) 15727813dd6fSViresh Kumar { 15737813dd6fSViresh Kumar struct list_head *head; 1574cf65948dSDmitry Osipenko unsigned int i; 15757813dd6fSViresh Kumar int ret; 15767813dd6fSViresh Kumar 15777813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 15787813dd6fSViresh Kumar head = &opp_table->opp_list; 15797813dd6fSViresh Kumar 1580a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1581a1e8c136SViresh Kumar if (ret) { 15827813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 15837813dd6fSViresh Kumar return ret; 15847813dd6fSViresh Kumar } 15857813dd6fSViresh Kumar 15867813dd6fSViresh Kumar list_add(&new_opp->node, head); 15877813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 15887813dd6fSViresh Kumar 15897813dd6fSViresh Kumar new_opp->opp_table = opp_table; 15907813dd6fSViresh Kumar kref_init(&new_opp->kref); 15917813dd6fSViresh Kumar 1592a2dea4cbSGreg Kroah-Hartman opp_debug_create_one(new_opp, opp_table); 15937813dd6fSViresh Kumar 15947813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 15957813dd6fSViresh Kumar new_opp->available = false; 15967813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 15977813dd6fSViresh Kumar __func__, new_opp->rate); 15987813dd6fSViresh Kumar } 15997813dd6fSViresh Kumar 1600cf65948dSDmitry Osipenko for (i = 0; i < opp_table->required_opp_count; i++) { 1601cf65948dSDmitry Osipenko if (new_opp->required_opps[i]->available) 1602cf65948dSDmitry Osipenko continue; 1603cf65948dSDmitry Osipenko 1604cf65948dSDmitry Osipenko new_opp->available = false; 1605cf65948dSDmitry Osipenko dev_warn(dev, "%s: OPP not supported by required OPP %pOF (%lu)\n", 1606cf65948dSDmitry Osipenko __func__, new_opp->required_opps[i]->np, new_opp->rate); 1607cf65948dSDmitry Osipenko break; 1608cf65948dSDmitry Osipenko } 1609cf65948dSDmitry Osipenko 16107813dd6fSViresh Kumar return 0; 16117813dd6fSViresh Kumar } 16127813dd6fSViresh Kumar 16137813dd6fSViresh Kumar /** 16147813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 16157813dd6fSViresh Kumar * @opp_table: OPP table 16167813dd6fSViresh Kumar * @dev: device for which we do this operation 16177813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 16187813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 16197813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 16207813dd6fSViresh Kumar * 16217813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 16227813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 16237813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 16247813dd6fSViresh Kumar * 16257813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 16267813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 16277813dd6fSViresh Kumar * 16287813dd6fSViresh Kumar * Return: 16297813dd6fSViresh Kumar * 0 On success OR 16307813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 16317813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 16327813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 16337813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 16347813dd6fSViresh Kumar */ 16357813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 16367813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 16377813dd6fSViresh Kumar { 16387813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 16397813dd6fSViresh Kumar unsigned long tol; 16407813dd6fSViresh Kumar int ret; 16417813dd6fSViresh Kumar 16427813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 16437813dd6fSViresh Kumar if (!new_opp) 16447813dd6fSViresh Kumar return -ENOMEM; 16457813dd6fSViresh Kumar 16467813dd6fSViresh Kumar /* populate the opp table */ 16477813dd6fSViresh Kumar new_opp->rate = freq; 16487813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 16497813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 16507813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 16517813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 16527813dd6fSViresh Kumar new_opp->available = true; 16537813dd6fSViresh Kumar new_opp->dynamic = dynamic; 16547813dd6fSViresh Kumar 1655a1e8c136SViresh Kumar ret = _opp_add(dev, new_opp, opp_table, false); 16567813dd6fSViresh Kumar if (ret) { 16577813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 16587813dd6fSViresh Kumar if (ret == -EBUSY) 16597813dd6fSViresh Kumar ret = 0; 16607813dd6fSViresh Kumar goto free_opp; 16617813dd6fSViresh Kumar } 16627813dd6fSViresh Kumar 16637813dd6fSViresh Kumar /* 16647813dd6fSViresh Kumar * Notify the changes in the availability of the operable 16657813dd6fSViresh Kumar * frequency/voltage list. 16667813dd6fSViresh Kumar */ 16677813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 16687813dd6fSViresh Kumar return 0; 16697813dd6fSViresh Kumar 16707813dd6fSViresh Kumar free_opp: 16717813dd6fSViresh Kumar _opp_free(new_opp); 16727813dd6fSViresh Kumar 16737813dd6fSViresh Kumar return ret; 16747813dd6fSViresh Kumar } 16757813dd6fSViresh Kumar 16767813dd6fSViresh Kumar /** 16777813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw() - Set supported platforms 16787813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 16797813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 16807813dd6fSViresh Kumar * @count: Number of elements in the array. 16817813dd6fSViresh Kumar * 16827813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 16837813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 16847813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 16857813dd6fSViresh Kumar * property. 16867813dd6fSViresh Kumar */ 16877813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, 16887813dd6fSViresh Kumar const u32 *versions, unsigned int count) 16897813dd6fSViresh Kumar { 16907813dd6fSViresh Kumar struct opp_table *opp_table; 16917813dd6fSViresh Kumar 1692e77dcb0bSViresh Kumar opp_table = _add_opp_table(dev); 1693dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 1694dd461cd9SStephan Gerhold return opp_table; 16957813dd6fSViresh Kumar 16967813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 16977813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 16987813dd6fSViresh Kumar 169925419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 170025419de1SViresh Kumar if (opp_table->supported_hw) 170125419de1SViresh Kumar return opp_table; 17027813dd6fSViresh Kumar 17037813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 17047813dd6fSViresh Kumar GFP_KERNEL); 17057813dd6fSViresh Kumar if (!opp_table->supported_hw) { 170625419de1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 170725419de1SViresh Kumar return ERR_PTR(-ENOMEM); 17087813dd6fSViresh Kumar } 17097813dd6fSViresh Kumar 17107813dd6fSViresh Kumar opp_table->supported_hw_count = count; 17117813dd6fSViresh Kumar 17127813dd6fSViresh Kumar return opp_table; 17137813dd6fSViresh Kumar } 17147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); 17157813dd6fSViresh Kumar 17167813dd6fSViresh Kumar /** 17177813dd6fSViresh Kumar * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw 17187813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). 17197813dd6fSViresh Kumar * 17207813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 17217813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure 17227813dd6fSViresh Kumar * will not be freed. 17237813dd6fSViresh Kumar */ 17247813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) 17257813dd6fSViresh Kumar { 1726c7bf8758SViresh Kumar if (unlikely(!opp_table)) 1727c7bf8758SViresh Kumar return; 1728c7bf8758SViresh Kumar 17297813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 17307813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 17317813dd6fSViresh Kumar 17327813dd6fSViresh Kumar kfree(opp_table->supported_hw); 17337813dd6fSViresh Kumar opp_table->supported_hw = NULL; 17347813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 17357813dd6fSViresh Kumar 17367813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17377813dd6fSViresh Kumar } 17387813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); 17397813dd6fSViresh Kumar 17407813dd6fSViresh Kumar /** 17417813dd6fSViresh Kumar * dev_pm_opp_set_prop_name() - Set prop-extn name 17427813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 17437813dd6fSViresh Kumar * @name: name to postfix to properties. 17447813dd6fSViresh Kumar * 17457813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 17467813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 17477813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 17487813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 17497813dd6fSViresh Kumar */ 17507813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 17517813dd6fSViresh Kumar { 17527813dd6fSViresh Kumar struct opp_table *opp_table; 17537813dd6fSViresh Kumar 1754e77dcb0bSViresh Kumar opp_table = _add_opp_table(dev); 1755dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 1756dd461cd9SStephan Gerhold return opp_table; 17577813dd6fSViresh Kumar 17587813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 17597813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 17607813dd6fSViresh Kumar 1761878ec1a9SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 1762878ec1a9SViresh Kumar if (opp_table->prop_name) 1763878ec1a9SViresh Kumar return opp_table; 17647813dd6fSViresh Kumar 17657813dd6fSViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 17667813dd6fSViresh Kumar if (!opp_table->prop_name) { 1767878ec1a9SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1768878ec1a9SViresh Kumar return ERR_PTR(-ENOMEM); 17697813dd6fSViresh Kumar } 17707813dd6fSViresh Kumar 17717813dd6fSViresh Kumar return opp_table; 17727813dd6fSViresh Kumar } 17737813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); 17747813dd6fSViresh Kumar 17757813dd6fSViresh Kumar /** 17767813dd6fSViresh Kumar * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name 17777813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). 17787813dd6fSViresh Kumar * 17797813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 17807813dd6fSViresh Kumar * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure 17817813dd6fSViresh Kumar * will not be freed. 17827813dd6fSViresh Kumar */ 17837813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table) 17847813dd6fSViresh Kumar { 1785c7bf8758SViresh Kumar if (unlikely(!opp_table)) 1786c7bf8758SViresh Kumar return; 1787c7bf8758SViresh Kumar 17887813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 17897813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 17907813dd6fSViresh Kumar 17917813dd6fSViresh Kumar kfree(opp_table->prop_name); 17927813dd6fSViresh Kumar opp_table->prop_name = NULL; 17937813dd6fSViresh Kumar 17947813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17957813dd6fSViresh Kumar } 17967813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); 17977813dd6fSViresh Kumar 17987813dd6fSViresh Kumar static int _allocate_set_opp_data(struct opp_table *opp_table) 17997813dd6fSViresh Kumar { 18007813dd6fSViresh Kumar struct dev_pm_set_opp_data *data; 18017813dd6fSViresh Kumar int len, count = opp_table->regulator_count; 18027813dd6fSViresh Kumar 180390e3577bSViresh Kumar if (WARN_ON(!opp_table->regulators)) 18047813dd6fSViresh Kumar return -EINVAL; 18057813dd6fSViresh Kumar 18067813dd6fSViresh Kumar /* space for set_opp_data */ 18077813dd6fSViresh Kumar len = sizeof(*data); 18087813dd6fSViresh Kumar 18097813dd6fSViresh Kumar /* space for old_opp.supplies and new_opp.supplies */ 18107813dd6fSViresh Kumar len += 2 * sizeof(struct dev_pm_opp_supply) * count; 18117813dd6fSViresh Kumar 18127813dd6fSViresh Kumar data = kzalloc(len, GFP_KERNEL); 18137813dd6fSViresh Kumar if (!data) 18147813dd6fSViresh Kumar return -ENOMEM; 18157813dd6fSViresh Kumar 18167813dd6fSViresh Kumar data->old_opp.supplies = (void *)(data + 1); 18177813dd6fSViresh Kumar data->new_opp.supplies = data->old_opp.supplies + count; 18187813dd6fSViresh Kumar 18197813dd6fSViresh Kumar opp_table->set_opp_data = data; 18207813dd6fSViresh Kumar 18217813dd6fSViresh Kumar return 0; 18227813dd6fSViresh Kumar } 18237813dd6fSViresh Kumar 18247813dd6fSViresh Kumar static void _free_set_opp_data(struct opp_table *opp_table) 18257813dd6fSViresh Kumar { 18267813dd6fSViresh Kumar kfree(opp_table->set_opp_data); 18277813dd6fSViresh Kumar opp_table->set_opp_data = NULL; 18287813dd6fSViresh Kumar } 18297813dd6fSViresh Kumar 18307813dd6fSViresh Kumar /** 18317813dd6fSViresh Kumar * dev_pm_opp_set_regulators() - Set regulator names for the device 18327813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 18337813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 18347813dd6fSViresh Kumar * @count: Number of regulators. 18357813dd6fSViresh Kumar * 18367813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 18377813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 18387813dd6fSViresh Kumar * well. 18397813dd6fSViresh Kumar * 18407813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 18417813dd6fSViresh Kumar */ 18427813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev, 18437813dd6fSViresh Kumar const char * const names[], 18447813dd6fSViresh Kumar unsigned int count) 18457813dd6fSViresh Kumar { 18467813dd6fSViresh Kumar struct opp_table *opp_table; 18477813dd6fSViresh Kumar struct regulator *reg; 18487813dd6fSViresh Kumar int ret, i; 18497813dd6fSViresh Kumar 1850e77dcb0bSViresh Kumar opp_table = _add_opp_table(dev); 1851dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 1852dd461cd9SStephan Gerhold return opp_table; 18537813dd6fSViresh Kumar 18547813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 18557813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 18567813dd6fSViresh Kumar ret = -EBUSY; 18577813dd6fSViresh Kumar goto err; 18587813dd6fSViresh Kumar } 18597813dd6fSViresh Kumar 1860779b783cSViresh Kumar /* Another CPU that shares the OPP table has set the regulators ? */ 1861779b783cSViresh Kumar if (opp_table->regulators) 1862779b783cSViresh Kumar return opp_table; 18637813dd6fSViresh Kumar 18647813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 18657813dd6fSViresh Kumar sizeof(*opp_table->regulators), 18667813dd6fSViresh Kumar GFP_KERNEL); 18677813dd6fSViresh Kumar if (!opp_table->regulators) { 18687813dd6fSViresh Kumar ret = -ENOMEM; 18697813dd6fSViresh Kumar goto err; 18707813dd6fSViresh Kumar } 18717813dd6fSViresh Kumar 18727813dd6fSViresh Kumar for (i = 0; i < count; i++) { 18737813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 18747813dd6fSViresh Kumar if (IS_ERR(reg)) { 18757813dd6fSViresh Kumar ret = PTR_ERR(reg); 18767813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) 18777813dd6fSViresh Kumar dev_err(dev, "%s: no regulator (%s) found: %d\n", 18787813dd6fSViresh Kumar __func__, names[i], ret); 18797813dd6fSViresh Kumar goto free_regulators; 18807813dd6fSViresh Kumar } 18817813dd6fSViresh Kumar 18827813dd6fSViresh Kumar opp_table->regulators[i] = reg; 18837813dd6fSViresh Kumar } 18847813dd6fSViresh Kumar 18857813dd6fSViresh Kumar opp_table->regulator_count = count; 18867813dd6fSViresh Kumar 18877813dd6fSViresh Kumar /* Allocate block only once to pass to set_opp() routines */ 18887813dd6fSViresh Kumar ret = _allocate_set_opp_data(opp_table); 18897813dd6fSViresh Kumar if (ret) 18907813dd6fSViresh Kumar goto free_regulators; 18917813dd6fSViresh Kumar 18927813dd6fSViresh Kumar return opp_table; 18937813dd6fSViresh Kumar 18947813dd6fSViresh Kumar free_regulators: 189524957db1SMarek Szyprowski while (i != 0) 189624957db1SMarek Szyprowski regulator_put(opp_table->regulators[--i]); 18977813dd6fSViresh Kumar 18987813dd6fSViresh Kumar kfree(opp_table->regulators); 18997813dd6fSViresh Kumar opp_table->regulators = NULL; 190046f48acaSViresh Kumar opp_table->regulator_count = -1; 19017813dd6fSViresh Kumar err: 19027813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 19037813dd6fSViresh Kumar 19047813dd6fSViresh Kumar return ERR_PTR(ret); 19057813dd6fSViresh Kumar } 19067813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); 19077813dd6fSViresh Kumar 19087813dd6fSViresh Kumar /** 19097813dd6fSViresh Kumar * dev_pm_opp_put_regulators() - Releases resources blocked for regulator 19107813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). 19117813dd6fSViresh Kumar */ 19127813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table) 19137813dd6fSViresh Kumar { 19147813dd6fSViresh Kumar int i; 19157813dd6fSViresh Kumar 1916c7bf8758SViresh Kumar if (unlikely(!opp_table)) 1917c7bf8758SViresh Kumar return; 1918c7bf8758SViresh Kumar 1919779b783cSViresh Kumar if (!opp_table->regulators) 1920779b783cSViresh Kumar goto put_opp_table; 19217813dd6fSViresh Kumar 19227813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 19237813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 19247813dd6fSViresh Kumar 192572f80ce4SViresh Kumar if (opp_table->enabled) { 19268d45719cSKamil Konieczny for (i = opp_table->regulator_count - 1; i >= 0; i--) 19278d45719cSKamil Konieczny regulator_disable(opp_table->regulators[i]); 19288d45719cSKamil Konieczny } 19298d45719cSKamil Konieczny 193024957db1SMarek Szyprowski for (i = opp_table->regulator_count - 1; i >= 0; i--) 19317813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 19327813dd6fSViresh Kumar 19337813dd6fSViresh Kumar _free_set_opp_data(opp_table); 19347813dd6fSViresh Kumar 19357813dd6fSViresh Kumar kfree(opp_table->regulators); 19367813dd6fSViresh Kumar opp_table->regulators = NULL; 193746f48acaSViresh Kumar opp_table->regulator_count = -1; 19387813dd6fSViresh Kumar 1939779b783cSViresh Kumar put_opp_table: 19407813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 19417813dd6fSViresh Kumar } 19427813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); 19437813dd6fSViresh Kumar 19447813dd6fSViresh Kumar /** 19457813dd6fSViresh Kumar * dev_pm_opp_set_clkname() - Set clk name for the device 19467813dd6fSViresh Kumar * @dev: Device for which clk name is being set. 19477813dd6fSViresh Kumar * @name: Clk name. 19487813dd6fSViresh Kumar * 19497813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to get pointer to the 19507813dd6fSViresh Kumar * clock for the device. Simple cases work fine without using this routine (i.e. 19517813dd6fSViresh Kumar * by passing connection-id as NULL), but for a device with multiple clocks 19527813dd6fSViresh Kumar * available, the OPP core needs to know the exact name of the clk to use. 19537813dd6fSViresh Kumar * 19547813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 19557813dd6fSViresh Kumar */ 19567813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) 19577813dd6fSViresh Kumar { 19587813dd6fSViresh Kumar struct opp_table *opp_table; 19597813dd6fSViresh Kumar int ret; 19607813dd6fSViresh Kumar 1961e77dcb0bSViresh Kumar opp_table = _add_opp_table(dev); 1962dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 1963dd461cd9SStephan Gerhold return opp_table; 19647813dd6fSViresh Kumar 19657813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 19667813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 19677813dd6fSViresh Kumar ret = -EBUSY; 19687813dd6fSViresh Kumar goto err; 19697813dd6fSViresh Kumar } 19707813dd6fSViresh Kumar 19717813dd6fSViresh Kumar /* Already have default clk set, free it */ 19727813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 19737813dd6fSViresh Kumar clk_put(opp_table->clk); 19747813dd6fSViresh Kumar 19757813dd6fSViresh Kumar /* Find clk for the device */ 19767813dd6fSViresh Kumar opp_table->clk = clk_get(dev, name); 19777813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 19787813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 19797813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) { 19807813dd6fSViresh Kumar dev_err(dev, "%s: Couldn't find clock: %d\n", __func__, 19817813dd6fSViresh Kumar ret); 19827813dd6fSViresh Kumar } 19837813dd6fSViresh Kumar goto err; 19847813dd6fSViresh Kumar } 19857813dd6fSViresh Kumar 19867813dd6fSViresh Kumar return opp_table; 19877813dd6fSViresh Kumar 19887813dd6fSViresh Kumar err: 19897813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 19907813dd6fSViresh Kumar 19917813dd6fSViresh Kumar return ERR_PTR(ret); 19927813dd6fSViresh Kumar } 19937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); 19947813dd6fSViresh Kumar 19957813dd6fSViresh Kumar /** 19967813dd6fSViresh Kumar * dev_pm_opp_put_clkname() - Releases resources blocked for clk. 19977813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). 19987813dd6fSViresh Kumar */ 19997813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table) 20007813dd6fSViresh Kumar { 2001c7bf8758SViresh Kumar if (unlikely(!opp_table)) 2002c7bf8758SViresh Kumar return; 2003c7bf8758SViresh Kumar 20047813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 20057813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 20067813dd6fSViresh Kumar 20077813dd6fSViresh Kumar clk_put(opp_table->clk); 20087813dd6fSViresh Kumar opp_table->clk = ERR_PTR(-EINVAL); 20097813dd6fSViresh Kumar 20107813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 20117813dd6fSViresh Kumar } 20127813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); 20137813dd6fSViresh Kumar 20147813dd6fSViresh Kumar /** 20157813dd6fSViresh Kumar * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper 20167813dd6fSViresh Kumar * @dev: Device for which the helper is getting registered. 20177813dd6fSViresh Kumar * @set_opp: Custom set OPP helper. 20187813dd6fSViresh Kumar * 20197813dd6fSViresh Kumar * This is useful to support complex platforms (like platforms with multiple 20207813dd6fSViresh Kumar * regulators per device), instead of the generic OPP set rate helper. 20217813dd6fSViresh Kumar * 20227813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 20237813dd6fSViresh Kumar */ 20247813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 20257813dd6fSViresh Kumar int (*set_opp)(struct dev_pm_set_opp_data *data)) 20267813dd6fSViresh Kumar { 20277813dd6fSViresh Kumar struct opp_table *opp_table; 20287813dd6fSViresh Kumar 20297813dd6fSViresh Kumar if (!set_opp) 20307813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 20317813dd6fSViresh Kumar 2032e77dcb0bSViresh Kumar opp_table = _add_opp_table(dev); 203347efcbcbSViresh Kumar if (IS_ERR(opp_table)) 2034dd461cd9SStephan Gerhold return opp_table; 20357813dd6fSViresh Kumar 20367813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 20377813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 20385019acc6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 20395019acc6SViresh Kumar return ERR_PTR(-EBUSY); 20407813dd6fSViresh Kumar } 20417813dd6fSViresh Kumar 20425019acc6SViresh Kumar /* Another CPU that shares the OPP table has set the helper ? */ 20435019acc6SViresh Kumar if (!opp_table->set_opp) 20447813dd6fSViresh Kumar opp_table->set_opp = set_opp; 20457813dd6fSViresh Kumar 20467813dd6fSViresh Kumar return opp_table; 20477813dd6fSViresh Kumar } 20487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); 20497813dd6fSViresh Kumar 20507813dd6fSViresh Kumar /** 2051604a7aebSViresh Kumar * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for 20527813dd6fSViresh Kumar * set_opp helper 20537813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). 20547813dd6fSViresh Kumar * 20557813dd6fSViresh Kumar * Release resources blocked for platform specific set_opp helper. 20567813dd6fSViresh Kumar */ 2057604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) 20587813dd6fSViresh Kumar { 2059c7bf8758SViresh Kumar if (unlikely(!opp_table)) 2060c7bf8758SViresh Kumar return; 2061c7bf8758SViresh Kumar 20627813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 20637813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 20647813dd6fSViresh Kumar 20657813dd6fSViresh Kumar opp_table->set_opp = NULL; 20667813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 20677813dd6fSViresh Kumar } 2068604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); 20697813dd6fSViresh Kumar 20706319aee1SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table) 20716319aee1SViresh Kumar { 20726319aee1SViresh Kumar int index; 20736319aee1SViresh Kumar 2074cb60e960SViresh Kumar if (!opp_table->genpd_virt_devs) 2075cb60e960SViresh Kumar return; 2076cb60e960SViresh Kumar 20776319aee1SViresh Kumar for (index = 0; index < opp_table->required_opp_count; index++) { 20786319aee1SViresh Kumar if (!opp_table->genpd_virt_devs[index]) 20796319aee1SViresh Kumar continue; 20806319aee1SViresh Kumar 20816319aee1SViresh Kumar dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 20826319aee1SViresh Kumar opp_table->genpd_virt_devs[index] = NULL; 20836319aee1SViresh Kumar } 2084c0ab9e08SViresh Kumar 2085c0ab9e08SViresh Kumar kfree(opp_table->genpd_virt_devs); 2086c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = NULL; 20876319aee1SViresh Kumar } 20886319aee1SViresh Kumar 20897813dd6fSViresh Kumar /** 20906319aee1SViresh Kumar * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 20916319aee1SViresh Kumar * @dev: Consumer device for which the genpd is getting attached. 20926319aee1SViresh Kumar * @names: Null terminated array of pointers containing names of genpd to attach. 209317a8f868SViresh Kumar * @virt_devs: Pointer to return the array of virtual devices. 20944f018bc0SViresh Kumar * 20954f018bc0SViresh Kumar * Multiple generic power domains for a device are supported with the help of 20964f018bc0SViresh Kumar * virtual genpd devices, which are created for each consumer device - genpd 20974f018bc0SViresh Kumar * pair. These are the device structures which are attached to the power domain 20984f018bc0SViresh Kumar * and are required by the OPP core to set the performance state of the genpd. 20996319aee1SViresh Kumar * The same API also works for the case where single genpd is available and so 21006319aee1SViresh Kumar * we don't need to support that separately. 21014f018bc0SViresh Kumar * 21024f018bc0SViresh Kumar * This helper will normally be called by the consumer driver of the device 21036319aee1SViresh Kumar * "dev", as only that has details of the genpd names. 21044f018bc0SViresh Kumar * 21056319aee1SViresh Kumar * This helper needs to be called once with a list of all genpd to attach. 21066319aee1SViresh Kumar * Otherwise the original device structure will be used instead by the OPP core. 2107baea35e4SViresh Kumar * 2108baea35e4SViresh Kumar * The order of entries in the names array must match the order in which 2109baea35e4SViresh Kumar * "required-opps" are added in DT. 21104f018bc0SViresh Kumar */ 211117a8f868SViresh Kumar struct opp_table *dev_pm_opp_attach_genpd(struct device *dev, 211217a8f868SViresh Kumar const char **names, struct device ***virt_devs) 21134f018bc0SViresh Kumar { 21144f018bc0SViresh Kumar struct opp_table *opp_table; 21156319aee1SViresh Kumar struct device *virt_dev; 2116baea35e4SViresh Kumar int index = 0, ret = -EINVAL; 21176319aee1SViresh Kumar const char **name = names; 21184f018bc0SViresh Kumar 2119e77dcb0bSViresh Kumar opp_table = _add_opp_table(dev); 2120dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2121dd461cd9SStephan Gerhold return opp_table; 21224f018bc0SViresh Kumar 2123cb60e960SViresh Kumar if (opp_table->genpd_virt_devs) 2124cb60e960SViresh Kumar return opp_table; 21254f018bc0SViresh Kumar 21266319aee1SViresh Kumar /* 21276319aee1SViresh Kumar * If the genpd's OPP table isn't already initialized, parsing of the 21286319aee1SViresh Kumar * required-opps fail for dev. We should retry this after genpd's OPP 21296319aee1SViresh Kumar * table is added. 21306319aee1SViresh Kumar */ 21316319aee1SViresh Kumar if (!opp_table->required_opp_count) { 21326319aee1SViresh Kumar ret = -EPROBE_DEFER; 21336319aee1SViresh Kumar goto put_table; 21346319aee1SViresh Kumar } 21356319aee1SViresh Kumar 21364f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 21374f018bc0SViresh Kumar 2138c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, 2139c0ab9e08SViresh Kumar sizeof(*opp_table->genpd_virt_devs), 2140c0ab9e08SViresh Kumar GFP_KERNEL); 2141c0ab9e08SViresh Kumar if (!opp_table->genpd_virt_devs) 2142c0ab9e08SViresh Kumar goto unlock; 21434f018bc0SViresh Kumar 21446319aee1SViresh Kumar while (*name) { 21456319aee1SViresh Kumar if (index >= opp_table->required_opp_count) { 21466319aee1SViresh Kumar dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 21476319aee1SViresh Kumar *name, opp_table->required_opp_count, index); 21486319aee1SViresh Kumar goto err; 21496319aee1SViresh Kumar } 21504f018bc0SViresh Kumar 21516319aee1SViresh Kumar virt_dev = dev_pm_domain_attach_by_name(dev, *name); 21526319aee1SViresh Kumar if (IS_ERR(virt_dev)) { 21536319aee1SViresh Kumar ret = PTR_ERR(virt_dev); 21546319aee1SViresh Kumar dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 21556319aee1SViresh Kumar goto err; 21564f018bc0SViresh Kumar } 21574f018bc0SViresh Kumar 21584f018bc0SViresh Kumar opp_table->genpd_virt_devs[index] = virt_dev; 2159baea35e4SViresh Kumar index++; 21606319aee1SViresh Kumar name++; 21616319aee1SViresh Kumar } 21626319aee1SViresh Kumar 216317a8f868SViresh Kumar if (virt_devs) 216417a8f868SViresh Kumar *virt_devs = opp_table->genpd_virt_devs; 21654f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 21664f018bc0SViresh Kumar 21674f018bc0SViresh Kumar return opp_table; 21686319aee1SViresh Kumar 21696319aee1SViresh Kumar err: 21706319aee1SViresh Kumar _opp_detach_genpd(opp_table); 2171c0ab9e08SViresh Kumar unlock: 21726319aee1SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 21736319aee1SViresh Kumar 21746319aee1SViresh Kumar put_table: 21756319aee1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 21766319aee1SViresh Kumar 21776319aee1SViresh Kumar return ERR_PTR(ret); 21784f018bc0SViresh Kumar } 21796319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd); 21804f018bc0SViresh Kumar 21814f018bc0SViresh Kumar /** 21826319aee1SViresh Kumar * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device. 21836319aee1SViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_attach_genpd(). 21844f018bc0SViresh Kumar * 21856319aee1SViresh Kumar * This detaches the genpd(s), resets the virtual device pointers, and puts the 21866319aee1SViresh Kumar * OPP table. 21874f018bc0SViresh Kumar */ 21886319aee1SViresh Kumar void dev_pm_opp_detach_genpd(struct opp_table *opp_table) 21894f018bc0SViresh Kumar { 2190c7bf8758SViresh Kumar if (unlikely(!opp_table)) 2191c7bf8758SViresh Kumar return; 2192c7bf8758SViresh Kumar 21934f018bc0SViresh Kumar /* 21944f018bc0SViresh Kumar * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 21954f018bc0SViresh Kumar * used in parallel. 21964f018bc0SViresh Kumar */ 21974f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 21986319aee1SViresh Kumar _opp_detach_genpd(opp_table); 21994f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 22004f018bc0SViresh Kumar 22016319aee1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 22024f018bc0SViresh Kumar } 22036319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd); 22044f018bc0SViresh Kumar 22054f018bc0SViresh Kumar /** 2206c8a59103SViresh Kumar * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 2207c8a59103SViresh Kumar * @src_table: OPP table which has dst_table as one of its required OPP table. 2208c8a59103SViresh Kumar * @dst_table: Required OPP table of the src_table. 2209c8a59103SViresh Kumar * @pstate: Current performance state of the src_table. 2210c8a59103SViresh Kumar * 2211c8a59103SViresh Kumar * This Returns pstate of the OPP (present in @dst_table) pointed out by the 2212c8a59103SViresh Kumar * "required-opps" property of the OPP (present in @src_table) which has 2213c8a59103SViresh Kumar * performance state set to @pstate. 2214c8a59103SViresh Kumar * 2215c8a59103SViresh Kumar * Return: Zero or positive performance state on success, otherwise negative 2216c8a59103SViresh Kumar * value on errors. 2217c8a59103SViresh Kumar */ 2218c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 2219c8a59103SViresh Kumar struct opp_table *dst_table, 2220c8a59103SViresh Kumar unsigned int pstate) 2221c8a59103SViresh Kumar { 2222c8a59103SViresh Kumar struct dev_pm_opp *opp; 2223c8a59103SViresh Kumar int dest_pstate = -EINVAL; 2224c8a59103SViresh Kumar int i; 2225c8a59103SViresh Kumar 2226c8a59103SViresh Kumar /* 2227c8a59103SViresh Kumar * Normally the src_table will have the "required_opps" property set to 2228c8a59103SViresh Kumar * point to one of the OPPs in the dst_table, but in some cases the 2229c8a59103SViresh Kumar * genpd and its master have one to one mapping of performance states 2230c8a59103SViresh Kumar * and so none of them have the "required-opps" property set. Return the 2231c8a59103SViresh Kumar * pstate of the src_table as it is in such cases. 2232c8a59103SViresh Kumar */ 2233c8a59103SViresh Kumar if (!src_table->required_opp_count) 2234c8a59103SViresh Kumar return pstate; 2235c8a59103SViresh Kumar 2236c8a59103SViresh Kumar for (i = 0; i < src_table->required_opp_count; i++) { 2237c8a59103SViresh Kumar if (src_table->required_opp_tables[i]->np == dst_table->np) 2238c8a59103SViresh Kumar break; 2239c8a59103SViresh Kumar } 2240c8a59103SViresh Kumar 2241c8a59103SViresh Kumar if (unlikely(i == src_table->required_opp_count)) { 2242c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 2243c8a59103SViresh Kumar __func__, src_table, dst_table); 2244c8a59103SViresh Kumar return -EINVAL; 2245c8a59103SViresh Kumar } 2246c8a59103SViresh Kumar 2247c8a59103SViresh Kumar mutex_lock(&src_table->lock); 2248c8a59103SViresh Kumar 2249c8a59103SViresh Kumar list_for_each_entry(opp, &src_table->opp_list, node) { 2250c8a59103SViresh Kumar if (opp->pstate == pstate) { 2251c8a59103SViresh Kumar dest_pstate = opp->required_opps[i]->pstate; 2252c8a59103SViresh Kumar goto unlock; 2253c8a59103SViresh Kumar } 2254c8a59103SViresh Kumar } 2255c8a59103SViresh Kumar 2256c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 2257c8a59103SViresh Kumar dst_table); 2258c8a59103SViresh Kumar 2259c8a59103SViresh Kumar unlock: 2260c8a59103SViresh Kumar mutex_unlock(&src_table->lock); 2261c8a59103SViresh Kumar 2262c8a59103SViresh Kumar return dest_pstate; 2263c8a59103SViresh Kumar } 2264c8a59103SViresh Kumar 2265c8a59103SViresh Kumar /** 22667813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 22677813dd6fSViresh Kumar * @dev: device for which we do this operation 22687813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 22697813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 22707813dd6fSViresh Kumar * 22717813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 22727813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 22737813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 22747813dd6fSViresh Kumar * 22757813dd6fSViresh Kumar * Return: 22767813dd6fSViresh Kumar * 0 On success OR 22777813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 22787813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 22797813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 22807813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 22817813dd6fSViresh Kumar */ 22827813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 22837813dd6fSViresh Kumar { 22847813dd6fSViresh Kumar struct opp_table *opp_table; 22857813dd6fSViresh Kumar int ret; 22867813dd6fSViresh Kumar 2287e77dcb0bSViresh Kumar opp_table = _add_opp_table(dev); 2288dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2289dd461cd9SStephan Gerhold return PTR_ERR(opp_table); 22907813dd6fSViresh Kumar 229146f48acaSViresh Kumar /* Fix regulator count for dynamic OPPs */ 229246f48acaSViresh Kumar opp_table->regulator_count = 1; 229346f48acaSViresh Kumar 22947813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 22950ad8c623SViresh Kumar if (ret) 22967813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 22970ad8c623SViresh Kumar 22987813dd6fSViresh Kumar return ret; 22997813dd6fSViresh Kumar } 23007813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 23017813dd6fSViresh Kumar 23027813dd6fSViresh Kumar /** 23037813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 23047813dd6fSViresh Kumar * @dev: device for which we do this operation 23057813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 23067813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 23077813dd6fSViresh Kumar * 23087813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 23097813dd6fSViresh Kumar * which is isolated here. 23107813dd6fSViresh Kumar * 23117813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 23127813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 23137813dd6fSViresh Kumar * successful. 23147813dd6fSViresh Kumar */ 23157813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 23167813dd6fSViresh Kumar bool availability_req) 23177813dd6fSViresh Kumar { 23187813dd6fSViresh Kumar struct opp_table *opp_table; 23197813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 23207813dd6fSViresh Kumar int r = 0; 23217813dd6fSViresh Kumar 23227813dd6fSViresh Kumar /* Find the opp_table */ 23237813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 23247813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 23257813dd6fSViresh Kumar r = PTR_ERR(opp_table); 23267813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 23277813dd6fSViresh Kumar return r; 23287813dd6fSViresh Kumar } 23297813dd6fSViresh Kumar 23307813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 23317813dd6fSViresh Kumar 23327813dd6fSViresh Kumar /* Do we have the frequency? */ 23337813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 23347813dd6fSViresh Kumar if (tmp_opp->rate == freq) { 23357813dd6fSViresh Kumar opp = tmp_opp; 23367813dd6fSViresh Kumar break; 23377813dd6fSViresh Kumar } 23387813dd6fSViresh Kumar } 23397813dd6fSViresh Kumar 23407813dd6fSViresh Kumar if (IS_ERR(opp)) { 23417813dd6fSViresh Kumar r = PTR_ERR(opp); 23427813dd6fSViresh Kumar goto unlock; 23437813dd6fSViresh Kumar } 23447813dd6fSViresh Kumar 23457813dd6fSViresh Kumar /* Is update really needed? */ 23467813dd6fSViresh Kumar if (opp->available == availability_req) 23477813dd6fSViresh Kumar goto unlock; 23487813dd6fSViresh Kumar 23497813dd6fSViresh Kumar opp->available = availability_req; 23507813dd6fSViresh Kumar 23517813dd6fSViresh Kumar dev_pm_opp_get(opp); 23527813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 23537813dd6fSViresh Kumar 23547813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 23557813dd6fSViresh Kumar if (availability_req) 23567813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 23577813dd6fSViresh Kumar opp); 23587813dd6fSViresh Kumar else 23597813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 23607813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 23617813dd6fSViresh Kumar 23627813dd6fSViresh Kumar dev_pm_opp_put(opp); 23637813dd6fSViresh Kumar goto put_table; 23647813dd6fSViresh Kumar 23657813dd6fSViresh Kumar unlock: 23667813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 23677813dd6fSViresh Kumar put_table: 23687813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 23697813dd6fSViresh Kumar return r; 23707813dd6fSViresh Kumar } 23717813dd6fSViresh Kumar 23727813dd6fSViresh Kumar /** 237325cb20a2SStephen Boyd * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP 237425cb20a2SStephen Boyd * @dev: device for which we do this operation 237525cb20a2SStephen Boyd * @freq: OPP frequency to adjust voltage of 237625cb20a2SStephen Boyd * @u_volt: new OPP target voltage 237725cb20a2SStephen Boyd * @u_volt_min: new OPP min voltage 237825cb20a2SStephen Boyd * @u_volt_max: new OPP max voltage 237925cb20a2SStephen Boyd * 238025cb20a2SStephen Boyd * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 238125cb20a2SStephen Boyd * copy operation, returns 0 if no modifcation was done OR modification was 238225cb20a2SStephen Boyd * successful. 238325cb20a2SStephen Boyd */ 238425cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq, 238525cb20a2SStephen Boyd unsigned long u_volt, unsigned long u_volt_min, 238625cb20a2SStephen Boyd unsigned long u_volt_max) 238725cb20a2SStephen Boyd 238825cb20a2SStephen Boyd { 238925cb20a2SStephen Boyd struct opp_table *opp_table; 239025cb20a2SStephen Boyd struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 239125cb20a2SStephen Boyd int r = 0; 239225cb20a2SStephen Boyd 239325cb20a2SStephen Boyd /* Find the opp_table */ 239425cb20a2SStephen Boyd opp_table = _find_opp_table(dev); 239525cb20a2SStephen Boyd if (IS_ERR(opp_table)) { 239625cb20a2SStephen Boyd r = PTR_ERR(opp_table); 239725cb20a2SStephen Boyd dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 239825cb20a2SStephen Boyd return r; 239925cb20a2SStephen Boyd } 240025cb20a2SStephen Boyd 240125cb20a2SStephen Boyd mutex_lock(&opp_table->lock); 240225cb20a2SStephen Boyd 240325cb20a2SStephen Boyd /* Do we have the frequency? */ 240425cb20a2SStephen Boyd list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 240525cb20a2SStephen Boyd if (tmp_opp->rate == freq) { 240625cb20a2SStephen Boyd opp = tmp_opp; 240725cb20a2SStephen Boyd break; 240825cb20a2SStephen Boyd } 240925cb20a2SStephen Boyd } 241025cb20a2SStephen Boyd 241125cb20a2SStephen Boyd if (IS_ERR(opp)) { 241225cb20a2SStephen Boyd r = PTR_ERR(opp); 241325cb20a2SStephen Boyd goto adjust_unlock; 241425cb20a2SStephen Boyd } 241525cb20a2SStephen Boyd 241625cb20a2SStephen Boyd /* Is update really needed? */ 241725cb20a2SStephen Boyd if (opp->supplies->u_volt == u_volt) 241825cb20a2SStephen Boyd goto adjust_unlock; 241925cb20a2SStephen Boyd 242025cb20a2SStephen Boyd opp->supplies->u_volt = u_volt; 242125cb20a2SStephen Boyd opp->supplies->u_volt_min = u_volt_min; 242225cb20a2SStephen Boyd opp->supplies->u_volt_max = u_volt_max; 242325cb20a2SStephen Boyd 242425cb20a2SStephen Boyd dev_pm_opp_get(opp); 242525cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 242625cb20a2SStephen Boyd 242725cb20a2SStephen Boyd /* Notify the voltage change of the OPP */ 242825cb20a2SStephen Boyd blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE, 242925cb20a2SStephen Boyd opp); 243025cb20a2SStephen Boyd 243125cb20a2SStephen Boyd dev_pm_opp_put(opp); 243225cb20a2SStephen Boyd goto adjust_put_table; 243325cb20a2SStephen Boyd 243425cb20a2SStephen Boyd adjust_unlock: 243525cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 243625cb20a2SStephen Boyd adjust_put_table: 243725cb20a2SStephen Boyd dev_pm_opp_put_opp_table(opp_table); 243825cb20a2SStephen Boyd return r; 243925cb20a2SStephen Boyd } 244003649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage); 244125cb20a2SStephen Boyd 244225cb20a2SStephen Boyd /** 24437813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 24447813dd6fSViresh Kumar * @dev: device for which we do this operation 24457813dd6fSViresh Kumar * @freq: OPP frequency to enable 24467813dd6fSViresh Kumar * 24477813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 24487813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 24497813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 24507813dd6fSViresh Kumar * 24517813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 24527813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 24537813dd6fSViresh Kumar * successful. 24547813dd6fSViresh Kumar */ 24557813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 24567813dd6fSViresh Kumar { 24577813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 24587813dd6fSViresh Kumar } 24597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 24607813dd6fSViresh Kumar 24617813dd6fSViresh Kumar /** 24627813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 24637813dd6fSViresh Kumar * @dev: device for which we do this operation 24647813dd6fSViresh Kumar * @freq: OPP frequency to disable 24657813dd6fSViresh Kumar * 24667813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 24677813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 24687813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 24697813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 24707813dd6fSViresh Kumar * 24717813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 24727813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 24737813dd6fSViresh Kumar * successful. 24747813dd6fSViresh Kumar */ 24757813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 24767813dd6fSViresh Kumar { 24777813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 24787813dd6fSViresh Kumar } 24797813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 24807813dd6fSViresh Kumar 24817813dd6fSViresh Kumar /** 24827813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 24837813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 24847813dd6fSViresh Kumar * @nb: Notifier block to be registered 24857813dd6fSViresh Kumar * 24867813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 24877813dd6fSViresh Kumar */ 24887813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 24897813dd6fSViresh Kumar { 24907813dd6fSViresh Kumar struct opp_table *opp_table; 24917813dd6fSViresh Kumar int ret; 24927813dd6fSViresh Kumar 24937813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 24947813dd6fSViresh Kumar if (IS_ERR(opp_table)) 24957813dd6fSViresh Kumar return PTR_ERR(opp_table); 24967813dd6fSViresh Kumar 24977813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 24987813dd6fSViresh Kumar 24997813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 25007813dd6fSViresh Kumar 25017813dd6fSViresh Kumar return ret; 25027813dd6fSViresh Kumar } 25037813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 25047813dd6fSViresh Kumar 25057813dd6fSViresh Kumar /** 25067813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 25077813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 25087813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 25097813dd6fSViresh Kumar * 25107813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 25117813dd6fSViresh Kumar */ 25127813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 25137813dd6fSViresh Kumar struct notifier_block *nb) 25147813dd6fSViresh Kumar { 25157813dd6fSViresh Kumar struct opp_table *opp_table; 25167813dd6fSViresh Kumar int ret; 25177813dd6fSViresh Kumar 25187813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 25197813dd6fSViresh Kumar if (IS_ERR(opp_table)) 25207813dd6fSViresh Kumar return PTR_ERR(opp_table); 25217813dd6fSViresh Kumar 25227813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 25237813dd6fSViresh Kumar 25247813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 25257813dd6fSViresh Kumar 25267813dd6fSViresh Kumar return ret; 25277813dd6fSViresh Kumar } 25287813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 25297813dd6fSViresh Kumar 25308aaf6264SViresh Kumar /** 25318aaf6264SViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 25328aaf6264SViresh Kumar * @dev: device pointer used to lookup OPP table. 25338aaf6264SViresh Kumar * 25348aaf6264SViresh Kumar * Free both OPPs created using static entries present in DT and the 25358aaf6264SViresh Kumar * dynamically added entries. 25368aaf6264SViresh Kumar */ 25378aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 25387813dd6fSViresh Kumar { 25397813dd6fSViresh Kumar struct opp_table *opp_table; 25407813dd6fSViresh Kumar 25417813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 25427813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 25437813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 25447813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 25457813dd6fSViresh Kumar 25467813dd6fSViresh Kumar if (error != -ENODEV) 25477813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 25487813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 25497813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 25507813dd6fSViresh Kumar error); 25517813dd6fSViresh Kumar return; 25527813dd6fSViresh Kumar } 25537813dd6fSViresh Kumar 2554922ff075SViresh Kumar /* 2555922ff075SViresh Kumar * Drop the extra reference only if the OPP table was successfully added 2556922ff075SViresh Kumar * with dev_pm_opp_of_add_table() earlier. 2557922ff075SViresh Kumar **/ 2558922ff075SViresh Kumar if (_opp_remove_all_static(opp_table)) 2559cdd6ed90SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2560cdd6ed90SViresh Kumar 2561922ff075SViresh Kumar /* Drop reference taken by _find_opp_table() */ 25627813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 25637813dd6fSViresh Kumar } 25647813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 2565