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); 327813dd6fSViresh Kumar 337813dd6fSViresh Kumar static struct opp_device *_find_opp_dev(const struct device *dev, 347813dd6fSViresh Kumar struct opp_table *opp_table) 357813dd6fSViresh Kumar { 367813dd6fSViresh Kumar struct opp_device *opp_dev; 377813dd6fSViresh Kumar 387813dd6fSViresh Kumar list_for_each_entry(opp_dev, &opp_table->dev_list, node) 397813dd6fSViresh Kumar if (opp_dev->dev == dev) 407813dd6fSViresh Kumar return opp_dev; 417813dd6fSViresh Kumar 427813dd6fSViresh Kumar return NULL; 437813dd6fSViresh Kumar } 447813dd6fSViresh Kumar 457813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev) 467813dd6fSViresh Kumar { 477813dd6fSViresh Kumar struct opp_table *opp_table; 483d255699SViresh Kumar bool found; 497813dd6fSViresh Kumar 507813dd6fSViresh Kumar list_for_each_entry(opp_table, &opp_tables, node) { 513d255699SViresh Kumar mutex_lock(&opp_table->lock); 523d255699SViresh Kumar found = !!_find_opp_dev(dev, opp_table); 533d255699SViresh Kumar mutex_unlock(&opp_table->lock); 543d255699SViresh Kumar 553d255699SViresh Kumar if (found) { 567813dd6fSViresh Kumar _get_opp_table_kref(opp_table); 577813dd6fSViresh Kumar 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 { 1217813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 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 4527813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 4537813dd6fSViresh Kumar unsigned long *freq) 4547813dd6fSViresh Kumar { 4557813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 4567813dd6fSViresh Kumar 4577813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 4587813dd6fSViresh Kumar 4597813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 4607813dd6fSViresh Kumar if (temp_opp->available && temp_opp->rate >= *freq) { 4617813dd6fSViresh Kumar opp = temp_opp; 4627813dd6fSViresh Kumar *freq = opp->rate; 4637813dd6fSViresh Kumar 4647813dd6fSViresh Kumar /* Increment the reference count of OPP */ 4657813dd6fSViresh Kumar dev_pm_opp_get(opp); 4667813dd6fSViresh Kumar break; 4677813dd6fSViresh Kumar } 4687813dd6fSViresh Kumar } 4697813dd6fSViresh Kumar 4707813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 4717813dd6fSViresh Kumar 4727813dd6fSViresh Kumar return opp; 4737813dd6fSViresh Kumar } 4747813dd6fSViresh Kumar 4757813dd6fSViresh Kumar /** 4767813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 4777813dd6fSViresh Kumar * @dev: device for which we do this operation 4787813dd6fSViresh Kumar * @freq: Start frequency 4797813dd6fSViresh Kumar * 4807813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 4817813dd6fSViresh Kumar * for a device. 4827813dd6fSViresh Kumar * 4837813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 4847813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 4857813dd6fSViresh Kumar * values can be: 4867813dd6fSViresh Kumar * EINVAL: for bad pointer 4877813dd6fSViresh Kumar * ERANGE: no match found for search 4887813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 4897813dd6fSViresh Kumar * 4907813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 4917813dd6fSViresh Kumar * use. 4927813dd6fSViresh Kumar */ 4937813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 4947813dd6fSViresh Kumar unsigned long *freq) 4957813dd6fSViresh Kumar { 4967813dd6fSViresh Kumar struct opp_table *opp_table; 4977813dd6fSViresh Kumar struct dev_pm_opp *opp; 4987813dd6fSViresh Kumar 4997813dd6fSViresh Kumar if (!dev || !freq) { 5007813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 5017813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 5027813dd6fSViresh Kumar } 5037813dd6fSViresh Kumar 5047813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 5057813dd6fSViresh Kumar if (IS_ERR(opp_table)) 5067813dd6fSViresh Kumar return ERR_CAST(opp_table); 5077813dd6fSViresh Kumar 5087813dd6fSViresh Kumar opp = _find_freq_ceil(opp_table, freq); 5097813dd6fSViresh Kumar 5107813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 5117813dd6fSViresh Kumar 5127813dd6fSViresh Kumar return opp; 5137813dd6fSViresh Kumar } 5147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 5157813dd6fSViresh Kumar 5167813dd6fSViresh Kumar /** 5177813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 5187813dd6fSViresh Kumar * @dev: device for which we do this operation 5197813dd6fSViresh Kumar * @freq: Start frequency 5207813dd6fSViresh Kumar * 5217813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 5227813dd6fSViresh Kumar * for a device. 5237813dd6fSViresh Kumar * 5247813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 5257813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 5267813dd6fSViresh Kumar * values can be: 5277813dd6fSViresh Kumar * EINVAL: for bad pointer 5287813dd6fSViresh Kumar * ERANGE: no match found for search 5297813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 5307813dd6fSViresh Kumar * 5317813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 5327813dd6fSViresh Kumar * use. 5337813dd6fSViresh Kumar */ 5347813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 5357813dd6fSViresh Kumar unsigned long *freq) 5367813dd6fSViresh Kumar { 5377813dd6fSViresh Kumar struct opp_table *opp_table; 5387813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 5397813dd6fSViresh Kumar 5407813dd6fSViresh Kumar if (!dev || !freq) { 5417813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 5427813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 5437813dd6fSViresh Kumar } 5447813dd6fSViresh Kumar 5457813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 5467813dd6fSViresh Kumar if (IS_ERR(opp_table)) 5477813dd6fSViresh Kumar return ERR_CAST(opp_table); 5487813dd6fSViresh Kumar 5497813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 5507813dd6fSViresh Kumar 5517813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 5527813dd6fSViresh Kumar if (temp_opp->available) { 5537813dd6fSViresh Kumar /* go to the next node, before choosing prev */ 5547813dd6fSViresh Kumar if (temp_opp->rate > *freq) 5557813dd6fSViresh Kumar break; 5567813dd6fSViresh Kumar else 5577813dd6fSViresh Kumar opp = temp_opp; 5587813dd6fSViresh Kumar } 5597813dd6fSViresh Kumar } 5607813dd6fSViresh Kumar 5617813dd6fSViresh Kumar /* Increment the reference count of OPP */ 5627813dd6fSViresh Kumar if (!IS_ERR(opp)) 5637813dd6fSViresh Kumar dev_pm_opp_get(opp); 5647813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 5657813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 5667813dd6fSViresh Kumar 5677813dd6fSViresh Kumar if (!IS_ERR(opp)) 5687813dd6fSViresh Kumar *freq = opp->rate; 5697813dd6fSViresh Kumar 5707813dd6fSViresh Kumar return opp; 5717813dd6fSViresh Kumar } 5727813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 5737813dd6fSViresh Kumar 5742f36bde0SAndrew-sh.Cheng /** 5752f36bde0SAndrew-sh.Cheng * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for 5762f36bde0SAndrew-sh.Cheng * target voltage. 5772f36bde0SAndrew-sh.Cheng * @dev: Device for which we do this operation. 5782f36bde0SAndrew-sh.Cheng * @u_volt: Target voltage. 5792f36bde0SAndrew-sh.Cheng * 5802f36bde0SAndrew-sh.Cheng * Search for OPP with highest (ceil) frequency and has voltage <= u_volt. 5812f36bde0SAndrew-sh.Cheng * 5822f36bde0SAndrew-sh.Cheng * Return: matching *opp, else returns ERR_PTR in case of error which should be 5832f36bde0SAndrew-sh.Cheng * handled using IS_ERR. 5842f36bde0SAndrew-sh.Cheng * 5852f36bde0SAndrew-sh.Cheng * Error return values can be: 5862f36bde0SAndrew-sh.Cheng * EINVAL: bad parameters 5872f36bde0SAndrew-sh.Cheng * 5882f36bde0SAndrew-sh.Cheng * The callers are required to call dev_pm_opp_put() for the returned OPP after 5892f36bde0SAndrew-sh.Cheng * use. 5902f36bde0SAndrew-sh.Cheng */ 5912f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev, 5922f36bde0SAndrew-sh.Cheng unsigned long u_volt) 5932f36bde0SAndrew-sh.Cheng { 5942f36bde0SAndrew-sh.Cheng struct opp_table *opp_table; 5952f36bde0SAndrew-sh.Cheng struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 5962f36bde0SAndrew-sh.Cheng 5972f36bde0SAndrew-sh.Cheng if (!dev || !u_volt) { 5982f36bde0SAndrew-sh.Cheng dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__, 5992f36bde0SAndrew-sh.Cheng u_volt); 6002f36bde0SAndrew-sh.Cheng return ERR_PTR(-EINVAL); 6012f36bde0SAndrew-sh.Cheng } 6022f36bde0SAndrew-sh.Cheng 6032f36bde0SAndrew-sh.Cheng opp_table = _find_opp_table(dev); 6042f36bde0SAndrew-sh.Cheng if (IS_ERR(opp_table)) 6052f36bde0SAndrew-sh.Cheng return ERR_CAST(opp_table); 6062f36bde0SAndrew-sh.Cheng 6072f36bde0SAndrew-sh.Cheng mutex_lock(&opp_table->lock); 6082f36bde0SAndrew-sh.Cheng 6092f36bde0SAndrew-sh.Cheng list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 6102f36bde0SAndrew-sh.Cheng if (temp_opp->available) { 6112f36bde0SAndrew-sh.Cheng if (temp_opp->supplies[0].u_volt > u_volt) 6122f36bde0SAndrew-sh.Cheng break; 6132f36bde0SAndrew-sh.Cheng opp = temp_opp; 6142f36bde0SAndrew-sh.Cheng } 6152f36bde0SAndrew-sh.Cheng } 6162f36bde0SAndrew-sh.Cheng 6172f36bde0SAndrew-sh.Cheng /* Increment the reference count of OPP */ 6182f36bde0SAndrew-sh.Cheng if (!IS_ERR(opp)) 6192f36bde0SAndrew-sh.Cheng dev_pm_opp_get(opp); 6202f36bde0SAndrew-sh.Cheng 6212f36bde0SAndrew-sh.Cheng mutex_unlock(&opp_table->lock); 6222f36bde0SAndrew-sh.Cheng dev_pm_opp_put_opp_table(opp_table); 6232f36bde0SAndrew-sh.Cheng 6242f36bde0SAndrew-sh.Cheng return opp; 6252f36bde0SAndrew-sh.Cheng } 6262f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt); 6272f36bde0SAndrew-sh.Cheng 6287813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 6297813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 6307813dd6fSViresh Kumar { 6317813dd6fSViresh Kumar int ret; 6327813dd6fSViresh Kumar 6337813dd6fSViresh Kumar /* Regulator not available for device */ 6347813dd6fSViresh Kumar if (IS_ERR(reg)) { 6357813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 6367813dd6fSViresh Kumar PTR_ERR(reg)); 6377813dd6fSViresh Kumar return 0; 6387813dd6fSViresh Kumar } 6397813dd6fSViresh Kumar 6407813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 6417813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 6427813dd6fSViresh Kumar 6437813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 6447813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 6457813dd6fSViresh Kumar if (ret) 6467813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 6477813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 6487813dd6fSViresh Kumar supply->u_volt_max, ret); 6497813dd6fSViresh Kumar 6507813dd6fSViresh Kumar return ret; 6517813dd6fSViresh Kumar } 6527813dd6fSViresh Kumar 653285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 654285881b5SViresh Kumar unsigned long freq) 6557813dd6fSViresh Kumar { 6567813dd6fSViresh Kumar int ret; 6577813dd6fSViresh Kumar 6587813dd6fSViresh Kumar ret = clk_set_rate(clk, freq); 6597813dd6fSViresh Kumar if (ret) { 6607813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 6617813dd6fSViresh Kumar ret); 6627813dd6fSViresh Kumar } 6637813dd6fSViresh Kumar 6647813dd6fSViresh Kumar return ret; 6657813dd6fSViresh Kumar } 6667813dd6fSViresh Kumar 6677813dd6fSViresh Kumar static int _generic_set_opp_regulator(const struct opp_table *opp_table, 6687813dd6fSViresh Kumar struct device *dev, 6697813dd6fSViresh Kumar unsigned long old_freq, 6707813dd6fSViresh Kumar unsigned long freq, 6717813dd6fSViresh Kumar struct dev_pm_opp_supply *old_supply, 6727813dd6fSViresh Kumar struct dev_pm_opp_supply *new_supply) 6737813dd6fSViresh Kumar { 6747813dd6fSViresh Kumar struct regulator *reg = opp_table->regulators[0]; 6757813dd6fSViresh Kumar int ret; 6767813dd6fSViresh Kumar 6777813dd6fSViresh Kumar /* This function only supports single regulator per device */ 6787813dd6fSViresh Kumar if (WARN_ON(opp_table->regulator_count > 1)) { 6797813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 6807813dd6fSViresh Kumar return -EINVAL; 6817813dd6fSViresh Kumar } 6827813dd6fSViresh Kumar 6837813dd6fSViresh Kumar /* Scaling up? Scale voltage before frequency */ 684c5c2a97bSWaldemar Rymarkiewicz if (freq >= old_freq) { 6857813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 6867813dd6fSViresh Kumar if (ret) 6877813dd6fSViresh Kumar goto restore_voltage; 6887813dd6fSViresh Kumar } 6897813dd6fSViresh Kumar 6907813dd6fSViresh Kumar /* Change frequency */ 691285881b5SViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 6927813dd6fSViresh Kumar if (ret) 6937813dd6fSViresh Kumar goto restore_voltage; 6947813dd6fSViresh Kumar 6957813dd6fSViresh Kumar /* Scaling down? Scale voltage after frequency */ 6967813dd6fSViresh Kumar if (freq < old_freq) { 6977813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 6987813dd6fSViresh Kumar if (ret) 6997813dd6fSViresh Kumar goto restore_freq; 7007813dd6fSViresh Kumar } 7017813dd6fSViresh Kumar 7027813dd6fSViresh Kumar return 0; 7037813dd6fSViresh Kumar 7047813dd6fSViresh Kumar restore_freq: 705285881b5SViresh Kumar if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq)) 7067813dd6fSViresh Kumar dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 7077813dd6fSViresh Kumar __func__, old_freq); 7087813dd6fSViresh Kumar restore_voltage: 7097813dd6fSViresh Kumar /* This shouldn't harm even if the voltages weren't updated earlier */ 7107813dd6fSViresh Kumar if (old_supply) 7117813dd6fSViresh Kumar _set_opp_voltage(dev, reg, old_supply); 7127813dd6fSViresh Kumar 7137813dd6fSViresh Kumar return ret; 7147813dd6fSViresh Kumar } 7157813dd6fSViresh Kumar 7167e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table, 7177e535993SViresh Kumar struct device *dev, unsigned long old_freq, 7187e535993SViresh Kumar unsigned long freq, 7197e535993SViresh Kumar struct dev_pm_opp_supply *old_supply, 7207e535993SViresh Kumar struct dev_pm_opp_supply *new_supply) 7217e535993SViresh Kumar { 7227e535993SViresh Kumar struct dev_pm_set_opp_data *data; 7237e535993SViresh Kumar int size; 7247e535993SViresh Kumar 7257e535993SViresh Kumar data = opp_table->set_opp_data; 7267e535993SViresh Kumar data->regulators = opp_table->regulators; 7277e535993SViresh Kumar data->regulator_count = opp_table->regulator_count; 7287e535993SViresh Kumar data->clk = opp_table->clk; 7297e535993SViresh Kumar data->dev = dev; 7307e535993SViresh Kumar 7317e535993SViresh Kumar data->old_opp.rate = old_freq; 7327e535993SViresh Kumar size = sizeof(*old_supply) * opp_table->regulator_count; 733560d1bcaSDmitry Osipenko if (!old_supply) 7347e535993SViresh Kumar memset(data->old_opp.supplies, 0, size); 7357e535993SViresh Kumar else 7367e535993SViresh Kumar memcpy(data->old_opp.supplies, old_supply, size); 7377e535993SViresh Kumar 7387e535993SViresh Kumar data->new_opp.rate = freq; 7397e535993SViresh Kumar memcpy(data->new_opp.supplies, new_supply, size); 7407e535993SViresh Kumar 7417e535993SViresh Kumar return opp_table->set_opp(data); 7427e535993SViresh Kumar } 7437e535993SViresh Kumar 744ca1b5d77SViresh Kumar /* This is only called for PM domain for now */ 745ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev, 746ca1b5d77SViresh Kumar struct opp_table *opp_table, 747ca1b5d77SViresh Kumar struct dev_pm_opp *opp) 748ca1b5d77SViresh Kumar { 749ca1b5d77SViresh Kumar struct opp_table **required_opp_tables = opp_table->required_opp_tables; 750ca1b5d77SViresh Kumar struct device **genpd_virt_devs = opp_table->genpd_virt_devs; 751ca1b5d77SViresh Kumar unsigned int pstate; 752ca1b5d77SViresh Kumar int i, ret = 0; 753ca1b5d77SViresh Kumar 754ca1b5d77SViresh Kumar if (!required_opp_tables) 755ca1b5d77SViresh Kumar return 0; 756ca1b5d77SViresh Kumar 757ca1b5d77SViresh Kumar /* Single genpd case */ 758ca1b5d77SViresh Kumar if (!genpd_virt_devs) { 759cd7ea582SRajendra Nayak pstate = likely(opp) ? opp->required_opps[0]->pstate : 0; 760ca1b5d77SViresh Kumar ret = dev_pm_genpd_set_performance_state(dev, pstate); 761ca1b5d77SViresh Kumar if (ret) { 762ca1b5d77SViresh Kumar dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", 763ca1b5d77SViresh Kumar dev_name(dev), pstate, ret); 764ca1b5d77SViresh Kumar } 765ca1b5d77SViresh Kumar return ret; 766ca1b5d77SViresh Kumar } 767ca1b5d77SViresh Kumar 768ca1b5d77SViresh Kumar /* Multiple genpd case */ 769ca1b5d77SViresh Kumar 770ca1b5d77SViresh Kumar /* 771ca1b5d77SViresh Kumar * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 772ca1b5d77SViresh Kumar * after it is freed from another thread. 773ca1b5d77SViresh Kumar */ 774ca1b5d77SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 775ca1b5d77SViresh Kumar 776ca1b5d77SViresh Kumar for (i = 0; i < opp_table->required_opp_count; i++) { 777cd7ea582SRajendra Nayak pstate = likely(opp) ? opp->required_opps[i]->pstate : 0; 778ca1b5d77SViresh Kumar 779ca1b5d77SViresh Kumar if (!genpd_virt_devs[i]) 780ca1b5d77SViresh Kumar continue; 781ca1b5d77SViresh Kumar 782ca1b5d77SViresh Kumar ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate); 783ca1b5d77SViresh Kumar if (ret) { 784ca1b5d77SViresh Kumar dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n", 785ca1b5d77SViresh Kumar dev_name(genpd_virt_devs[i]), pstate, ret); 786ca1b5d77SViresh Kumar break; 787ca1b5d77SViresh Kumar } 788ca1b5d77SViresh Kumar } 789ca1b5d77SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 790ca1b5d77SViresh Kumar 791ca1b5d77SViresh Kumar return ret; 792ca1b5d77SViresh Kumar } 793ca1b5d77SViresh Kumar 7947813dd6fSViresh Kumar /** 7957813dd6fSViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 7967813dd6fSViresh Kumar * @dev: device for which we do this operation 7977813dd6fSViresh Kumar * @target_freq: frequency to achieve 7987813dd6fSViresh Kumar * 799b3e3759eSStephen Boyd * This configures the power-supplies to the levels specified by the OPP 800b3e3759eSStephen Boyd * corresponding to the target_freq, and programs the clock to a value <= 801b3e3759eSStephen Boyd * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax 802b3e3759eSStephen Boyd * provided by the opp, should have already rounded to the target OPP's 803b3e3759eSStephen Boyd * frequency. 8047813dd6fSViresh Kumar */ 8057813dd6fSViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 8067813dd6fSViresh Kumar { 8077813dd6fSViresh Kumar struct opp_table *opp_table; 808b3e3759eSStephen Boyd unsigned long freq, old_freq, temp_freq; 8097813dd6fSViresh Kumar struct dev_pm_opp *old_opp, *opp; 8107813dd6fSViresh Kumar struct clk *clk; 8117e535993SViresh Kumar int ret; 8127813dd6fSViresh Kumar 8137813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 8147813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 8157813dd6fSViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 8167813dd6fSViresh Kumar return PTR_ERR(opp_table); 8177813dd6fSViresh Kumar } 8187813dd6fSViresh Kumar 819cd7ea582SRajendra Nayak if (unlikely(!target_freq)) { 820cd7ea582SRajendra Nayak if (opp_table->required_opp_tables) { 821cd7ea582SRajendra Nayak ret = _set_required_opps(dev, opp_table, NULL); 822aca48b61SRajendra Nayak } else if (!_get_opp_count(opp_table)) { 823aca48b61SRajendra Nayak return 0; 824cd7ea582SRajendra Nayak } else { 825cd7ea582SRajendra Nayak dev_err(dev, "target frequency can't be 0\n"); 826cd7ea582SRajendra Nayak ret = -EINVAL; 827cd7ea582SRajendra Nayak } 828cd7ea582SRajendra Nayak 829cd7ea582SRajendra Nayak goto put_opp_table; 830cd7ea582SRajendra Nayak } 831cd7ea582SRajendra Nayak 8327813dd6fSViresh Kumar clk = opp_table->clk; 8337813dd6fSViresh Kumar if (IS_ERR(clk)) { 8347813dd6fSViresh Kumar dev_err(dev, "%s: No clock available for the device\n", 8357813dd6fSViresh Kumar __func__); 8367813dd6fSViresh Kumar ret = PTR_ERR(clk); 8377813dd6fSViresh Kumar goto put_opp_table; 8387813dd6fSViresh Kumar } 8397813dd6fSViresh Kumar 8407813dd6fSViresh Kumar freq = clk_round_rate(clk, target_freq); 8417813dd6fSViresh Kumar if ((long)freq <= 0) 8427813dd6fSViresh Kumar freq = target_freq; 8437813dd6fSViresh Kumar 8447813dd6fSViresh Kumar old_freq = clk_get_rate(clk); 8457813dd6fSViresh Kumar 8467813dd6fSViresh Kumar /* Return early if nothing to do */ 8477813dd6fSViresh Kumar if (old_freq == freq) { 8487813dd6fSViresh Kumar dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", 8497813dd6fSViresh Kumar __func__, freq); 8507813dd6fSViresh Kumar ret = 0; 8517813dd6fSViresh Kumar goto put_opp_table; 8527813dd6fSViresh Kumar } 8537813dd6fSViresh Kumar 854aca48b61SRajendra Nayak /* 855aca48b61SRajendra Nayak * For IO devices which require an OPP on some platforms/SoCs 856aca48b61SRajendra Nayak * while just needing to scale the clock on some others 857aca48b61SRajendra Nayak * we look for empty OPP tables with just a clock handle and 858aca48b61SRajendra Nayak * scale only the clk. This makes dev_pm_opp_set_rate() 859aca48b61SRajendra Nayak * equivalent to a clk_set_rate() 860aca48b61SRajendra Nayak */ 861aca48b61SRajendra Nayak if (!_get_opp_count(opp_table)) { 862aca48b61SRajendra Nayak ret = _generic_set_opp_clk_only(dev, clk, freq); 863aca48b61SRajendra Nayak goto put_opp_table; 864aca48b61SRajendra Nayak } 865aca48b61SRajendra Nayak 866b3e3759eSStephen Boyd temp_freq = old_freq; 867b3e3759eSStephen Boyd old_opp = _find_freq_ceil(opp_table, &temp_freq); 8687813dd6fSViresh Kumar if (IS_ERR(old_opp)) { 8697813dd6fSViresh Kumar dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", 8707813dd6fSViresh Kumar __func__, old_freq, PTR_ERR(old_opp)); 8717813dd6fSViresh Kumar } 8727813dd6fSViresh Kumar 873b3e3759eSStephen Boyd temp_freq = freq; 874b3e3759eSStephen Boyd opp = _find_freq_ceil(opp_table, &temp_freq); 8757813dd6fSViresh Kumar if (IS_ERR(opp)) { 8767813dd6fSViresh Kumar ret = PTR_ERR(opp); 8777813dd6fSViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 8787813dd6fSViresh Kumar __func__, freq, ret); 8797813dd6fSViresh Kumar goto put_old_opp; 8807813dd6fSViresh Kumar } 8817813dd6fSViresh Kumar 8827813dd6fSViresh Kumar dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, 8837813dd6fSViresh Kumar old_freq, freq); 8847813dd6fSViresh Kumar 885ca1b5d77SViresh Kumar /* Scaling up? Configure required OPPs before frequency */ 886faef080fSViresh Kumar if (freq >= old_freq) { 887ca1b5d77SViresh Kumar ret = _set_required_opps(dev, opp_table, opp); 888ca1b5d77SViresh Kumar if (ret) 889ca1b5d77SViresh Kumar goto put_opp; 890ca1b5d77SViresh Kumar } 891ca1b5d77SViresh Kumar 8927e535993SViresh Kumar if (opp_table->set_opp) { 8937e535993SViresh Kumar ret = _set_opp_custom(opp_table, dev, old_freq, freq, 8947e535993SViresh Kumar IS_ERR(old_opp) ? NULL : old_opp->supplies, 8957e535993SViresh Kumar opp->supplies); 8967e535993SViresh Kumar } else if (opp_table->regulators) { 8977813dd6fSViresh Kumar ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq, 8987813dd6fSViresh Kumar IS_ERR(old_opp) ? NULL : old_opp->supplies, 8997813dd6fSViresh Kumar opp->supplies); 9007813dd6fSViresh Kumar } else { 9017813dd6fSViresh Kumar /* Only frequency scaling */ 902285881b5SViresh Kumar ret = _generic_set_opp_clk_only(dev, clk, freq); 9037813dd6fSViresh Kumar } 9047813dd6fSViresh Kumar 905ca1b5d77SViresh Kumar /* Scaling down? Configure required OPPs after frequency */ 906ca1b5d77SViresh Kumar if (!ret && freq < old_freq) { 907ca1b5d77SViresh Kumar ret = _set_required_opps(dev, opp_table, opp); 908ca1b5d77SViresh Kumar if (ret) 909ca1b5d77SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 910ca1b5d77SViresh Kumar } 911ca1b5d77SViresh Kumar 912ca1b5d77SViresh Kumar put_opp: 9137813dd6fSViresh Kumar dev_pm_opp_put(opp); 9147813dd6fSViresh Kumar put_old_opp: 9157813dd6fSViresh Kumar if (!IS_ERR(old_opp)) 9167813dd6fSViresh Kumar dev_pm_opp_put(old_opp); 9177813dd6fSViresh Kumar put_opp_table: 9187813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 9197813dd6fSViresh Kumar return ret; 9207813dd6fSViresh Kumar } 9217813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 9227813dd6fSViresh Kumar 9237813dd6fSViresh Kumar /* OPP-dev Helpers */ 9247813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 9257813dd6fSViresh Kumar struct opp_table *opp_table) 9267813dd6fSViresh Kumar { 9277813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 9287813dd6fSViresh Kumar list_del(&opp_dev->node); 9297813dd6fSViresh Kumar kfree(opp_dev); 9307813dd6fSViresh Kumar } 9317813dd6fSViresh Kumar 932283d55e6SViresh Kumar static struct opp_device *_add_opp_dev_unlocked(const struct device *dev, 9337813dd6fSViresh Kumar struct opp_table *opp_table) 9347813dd6fSViresh Kumar { 9357813dd6fSViresh Kumar struct opp_device *opp_dev; 9367813dd6fSViresh Kumar 9377813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 9387813dd6fSViresh Kumar if (!opp_dev) 9397813dd6fSViresh Kumar return NULL; 9407813dd6fSViresh Kumar 9417813dd6fSViresh Kumar /* Initialize opp-dev */ 9427813dd6fSViresh Kumar opp_dev->dev = dev; 9433d255699SViresh Kumar 9447813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 9457813dd6fSViresh Kumar 9467813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 947a2dea4cbSGreg Kroah-Hartman opp_debug_register(opp_dev, opp_table); 948283d55e6SViresh Kumar 949283d55e6SViresh Kumar return opp_dev; 950283d55e6SViresh Kumar } 951283d55e6SViresh Kumar 952283d55e6SViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 953283d55e6SViresh Kumar struct opp_table *opp_table) 954283d55e6SViresh Kumar { 955283d55e6SViresh Kumar struct opp_device *opp_dev; 956283d55e6SViresh Kumar 957283d55e6SViresh Kumar mutex_lock(&opp_table->lock); 958283d55e6SViresh Kumar opp_dev = _add_opp_dev_unlocked(dev, opp_table); 9593d255699SViresh Kumar mutex_unlock(&opp_table->lock); 9607813dd6fSViresh Kumar 9617813dd6fSViresh Kumar return opp_dev; 9627813dd6fSViresh Kumar } 9637813dd6fSViresh Kumar 964eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index) 9657813dd6fSViresh Kumar { 9667813dd6fSViresh Kumar struct opp_table *opp_table; 9677813dd6fSViresh Kumar struct opp_device *opp_dev; 9687813dd6fSViresh Kumar int ret; 9697813dd6fSViresh Kumar 9707813dd6fSViresh Kumar /* 9717813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 9727813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 9737813dd6fSViresh Kumar */ 9747813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 9757813dd6fSViresh Kumar if (!opp_table) 9767813dd6fSViresh Kumar return NULL; 9777813dd6fSViresh Kumar 9783d255699SViresh Kumar mutex_init(&opp_table->lock); 9794f018bc0SViresh Kumar mutex_init(&opp_table->genpd_virt_dev_lock); 9807813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 9817813dd6fSViresh Kumar 98246f48acaSViresh Kumar /* Mark regulator count uninitialized */ 98346f48acaSViresh Kumar opp_table->regulator_count = -1; 98446f48acaSViresh Kumar 9857813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 9867813dd6fSViresh Kumar if (!opp_dev) { 9877813dd6fSViresh Kumar kfree(opp_table); 9887813dd6fSViresh Kumar return NULL; 9897813dd6fSViresh Kumar } 9907813dd6fSViresh Kumar 991eb7c8743SViresh Kumar _of_init_opp_table(opp_table, dev, index); 9927813dd6fSViresh Kumar 9937813dd6fSViresh Kumar /* Find clk for the device */ 9947813dd6fSViresh Kumar opp_table->clk = clk_get(dev, NULL); 9957813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 9967813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 9977813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) 9987813dd6fSViresh Kumar dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, 9997813dd6fSViresh Kumar ret); 10007813dd6fSViresh Kumar } 10017813dd6fSViresh Kumar 10027813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 10037813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 10047813dd6fSViresh Kumar kref_init(&opp_table->kref); 10057813dd6fSViresh Kumar 10067813dd6fSViresh Kumar /* Secure the device table modification */ 10077813dd6fSViresh Kumar list_add(&opp_table->node, &opp_tables); 10087813dd6fSViresh Kumar return opp_table; 10097813dd6fSViresh Kumar } 10107813dd6fSViresh Kumar 10117813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 10127813dd6fSViresh Kumar { 10137813dd6fSViresh Kumar kref_get(&opp_table->kref); 10147813dd6fSViresh Kumar } 10157813dd6fSViresh Kumar 1016eb7c8743SViresh Kumar static struct opp_table *_opp_get_opp_table(struct device *dev, int index) 10177813dd6fSViresh Kumar { 10187813dd6fSViresh Kumar struct opp_table *opp_table; 10197813dd6fSViresh Kumar 10207813dd6fSViresh Kumar /* Hold our table modification lock here */ 10217813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 10227813dd6fSViresh Kumar 10237813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 10247813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 10257813dd6fSViresh Kumar goto unlock; 10267813dd6fSViresh Kumar 1027283d55e6SViresh Kumar opp_table = _managed_opp(dev, index); 1028283d55e6SViresh Kumar if (opp_table) { 1029283d55e6SViresh Kumar if (!_add_opp_dev_unlocked(dev, opp_table)) { 1030283d55e6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1031283d55e6SViresh Kumar opp_table = NULL; 1032283d55e6SViresh Kumar } 1033283d55e6SViresh Kumar goto unlock; 1034283d55e6SViresh Kumar } 1035283d55e6SViresh Kumar 1036eb7c8743SViresh Kumar opp_table = _allocate_opp_table(dev, index); 10377813dd6fSViresh Kumar 10387813dd6fSViresh Kumar unlock: 10397813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 10407813dd6fSViresh Kumar 10417813dd6fSViresh Kumar return opp_table; 10427813dd6fSViresh Kumar } 1043eb7c8743SViresh Kumar 1044eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 1045eb7c8743SViresh Kumar { 1046eb7c8743SViresh Kumar return _opp_get_opp_table(dev, 0); 1047eb7c8743SViresh Kumar } 10487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 10497813dd6fSViresh Kumar 1050eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev, 1051eb7c8743SViresh Kumar int index) 1052eb7c8743SViresh Kumar { 1053eb7c8743SViresh Kumar return _opp_get_opp_table(dev, index); 1054eb7c8743SViresh Kumar } 1055eb7c8743SViresh Kumar 10567813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 10577813dd6fSViresh Kumar { 10587813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 1059cdd6ed90SViresh Kumar struct opp_device *opp_dev, *temp; 10607813dd6fSViresh Kumar 10615d6d106fSViresh Kumar _of_clear_opp_table(opp_table); 10625d6d106fSViresh Kumar 10637813dd6fSViresh Kumar /* Release clk */ 10647813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 10657813dd6fSViresh Kumar clk_put(opp_table->clk); 10667813dd6fSViresh Kumar 1067cdd6ed90SViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 1068cdd6ed90SViresh Kumar 1069cdd6ed90SViresh Kumar list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 10703d255699SViresh Kumar /* 1071cdd6ed90SViresh Kumar * The OPP table is getting removed, drop the performance state 1072cdd6ed90SViresh Kumar * constraints. 10733d255699SViresh Kumar */ 1074cdd6ed90SViresh Kumar if (opp_table->genpd_performance_state) 1075cdd6ed90SViresh Kumar dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 10767813dd6fSViresh Kumar 10777813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 1078cdd6ed90SViresh Kumar } 10797813dd6fSViresh Kumar 10804f018bc0SViresh Kumar mutex_destroy(&opp_table->genpd_virt_dev_lock); 10817813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 10827813dd6fSViresh Kumar list_del(&opp_table->node); 10837813dd6fSViresh Kumar kfree(opp_table); 10847813dd6fSViresh Kumar 10857813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 10867813dd6fSViresh Kumar } 10877813dd6fSViresh Kumar 10887813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 10897813dd6fSViresh Kumar { 10907813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 10917813dd6fSViresh Kumar &opp_table_lock); 10927813dd6fSViresh Kumar } 10937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 10947813dd6fSViresh Kumar 10957813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 10967813dd6fSViresh Kumar { 10977813dd6fSViresh Kumar kfree(opp); 10987813dd6fSViresh Kumar } 10997813dd6fSViresh Kumar 11001690d8bbSViresh Kumar static void _opp_kref_release(struct dev_pm_opp *opp, 11011690d8bbSViresh Kumar struct opp_table *opp_table) 11027813dd6fSViresh Kumar { 11037813dd6fSViresh Kumar /* 11047813dd6fSViresh Kumar * Notify the changes in the availability of the operable 11057813dd6fSViresh Kumar * frequency/voltage list. 11067813dd6fSViresh Kumar */ 11077813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 1108da544b61SViresh Kumar _of_opp_free_required_opps(opp_table, opp); 11097813dd6fSViresh Kumar opp_debug_remove_one(opp); 11107813dd6fSViresh Kumar list_del(&opp->node); 11117813dd6fSViresh Kumar kfree(opp); 11121690d8bbSViresh Kumar } 11137813dd6fSViresh Kumar 11141690d8bbSViresh Kumar static void _opp_kref_release_unlocked(struct kref *kref) 11151690d8bbSViresh Kumar { 11161690d8bbSViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 11171690d8bbSViresh Kumar struct opp_table *opp_table = opp->opp_table; 11181690d8bbSViresh Kumar 11191690d8bbSViresh Kumar _opp_kref_release(opp, opp_table); 11201690d8bbSViresh Kumar } 11211690d8bbSViresh Kumar 11221690d8bbSViresh Kumar static void _opp_kref_release_locked(struct kref *kref) 11231690d8bbSViresh Kumar { 11241690d8bbSViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 11251690d8bbSViresh Kumar struct opp_table *opp_table = opp->opp_table; 11261690d8bbSViresh Kumar 11271690d8bbSViresh Kumar _opp_kref_release(opp, opp_table); 11287813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 11297813dd6fSViresh Kumar } 11307813dd6fSViresh Kumar 1131a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 11327813dd6fSViresh Kumar { 11337813dd6fSViresh Kumar kref_get(&opp->kref); 11347813dd6fSViresh Kumar } 11357813dd6fSViresh Kumar 11367813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 11377813dd6fSViresh Kumar { 11381690d8bbSViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release_locked, 11391690d8bbSViresh Kumar &opp->opp_table->lock); 11407813dd6fSViresh Kumar } 11417813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 11427813dd6fSViresh Kumar 11431690d8bbSViresh Kumar static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp) 11441690d8bbSViresh Kumar { 11451690d8bbSViresh Kumar kref_put(&opp->kref, _opp_kref_release_unlocked); 11461690d8bbSViresh Kumar } 11471690d8bbSViresh Kumar 11487813dd6fSViresh Kumar /** 11497813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 11507813dd6fSViresh Kumar * @dev: device for which we do this operation 11517813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 11527813dd6fSViresh Kumar * 11537813dd6fSViresh Kumar * This function removes an opp from the opp table. 11547813dd6fSViresh Kumar */ 11557813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 11567813dd6fSViresh Kumar { 11577813dd6fSViresh Kumar struct dev_pm_opp *opp; 11587813dd6fSViresh Kumar struct opp_table *opp_table; 11597813dd6fSViresh Kumar bool found = false; 11607813dd6fSViresh Kumar 11617813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 11627813dd6fSViresh Kumar if (IS_ERR(opp_table)) 11637813dd6fSViresh Kumar return; 11647813dd6fSViresh Kumar 11657813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 11667813dd6fSViresh Kumar 11677813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 11687813dd6fSViresh Kumar if (opp->rate == freq) { 11697813dd6fSViresh Kumar found = true; 11707813dd6fSViresh Kumar break; 11717813dd6fSViresh Kumar } 11727813dd6fSViresh Kumar } 11737813dd6fSViresh Kumar 11747813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 11757813dd6fSViresh Kumar 11767813dd6fSViresh Kumar if (found) { 11777813dd6fSViresh Kumar dev_pm_opp_put(opp); 11780ad8c623SViresh Kumar 11790ad8c623SViresh Kumar /* Drop the reference taken by dev_pm_opp_add() */ 11800ad8c623SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 11817813dd6fSViresh Kumar } else { 11827813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 11837813dd6fSViresh Kumar __func__, freq); 11847813dd6fSViresh Kumar } 11857813dd6fSViresh Kumar 11860ad8c623SViresh Kumar /* Drop the reference taken by _find_opp_table() */ 11877813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 11887813dd6fSViresh Kumar } 11897813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 11907813dd6fSViresh Kumar 119103758d60SViresh Kumar void _opp_remove_all_static(struct opp_table *opp_table) 119203758d60SViresh Kumar { 119303758d60SViresh Kumar struct dev_pm_opp *opp, *tmp; 119403758d60SViresh Kumar 119503758d60SViresh Kumar mutex_lock(&opp_table->lock); 119603758d60SViresh Kumar 119703758d60SViresh Kumar if (!opp_table->parsed_static_opps || --opp_table->parsed_static_opps) 119803758d60SViresh Kumar goto unlock; 119903758d60SViresh Kumar 120003758d60SViresh Kumar list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { 120103758d60SViresh Kumar if (!opp->dynamic) 120203758d60SViresh Kumar dev_pm_opp_put_unlocked(opp); 120303758d60SViresh Kumar } 120403758d60SViresh Kumar 120503758d60SViresh Kumar unlock: 120603758d60SViresh Kumar mutex_unlock(&opp_table->lock); 120703758d60SViresh Kumar } 120803758d60SViresh Kumar 12091690d8bbSViresh Kumar /** 12101690d8bbSViresh Kumar * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 12111690d8bbSViresh Kumar * @dev: device for which we do this operation 12121690d8bbSViresh Kumar * 12131690d8bbSViresh Kumar * This function removes all dynamically created OPPs from the opp table. 12141690d8bbSViresh Kumar */ 12151690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev) 12161690d8bbSViresh Kumar { 12171690d8bbSViresh Kumar struct opp_table *opp_table; 12181690d8bbSViresh Kumar struct dev_pm_opp *opp, *temp; 12191690d8bbSViresh Kumar int count = 0; 12201690d8bbSViresh Kumar 12211690d8bbSViresh Kumar opp_table = _find_opp_table(dev); 12221690d8bbSViresh Kumar if (IS_ERR(opp_table)) 12231690d8bbSViresh Kumar return; 12241690d8bbSViresh Kumar 12251690d8bbSViresh Kumar mutex_lock(&opp_table->lock); 12261690d8bbSViresh Kumar list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) { 12271690d8bbSViresh Kumar if (opp->dynamic) { 12281690d8bbSViresh Kumar dev_pm_opp_put_unlocked(opp); 12291690d8bbSViresh Kumar count++; 12301690d8bbSViresh Kumar } 12311690d8bbSViresh Kumar } 12321690d8bbSViresh Kumar mutex_unlock(&opp_table->lock); 12331690d8bbSViresh Kumar 12341690d8bbSViresh Kumar /* Drop the references taken by dev_pm_opp_add() */ 12351690d8bbSViresh Kumar while (count--) 12361690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 12371690d8bbSViresh Kumar 12381690d8bbSViresh Kumar /* Drop the reference taken by _find_opp_table() */ 12391690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 12401690d8bbSViresh Kumar } 12411690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 12421690d8bbSViresh Kumar 12437813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table) 12447813dd6fSViresh Kumar { 12457813dd6fSViresh Kumar struct dev_pm_opp *opp; 12467813dd6fSViresh Kumar int count, supply_size; 12477813dd6fSViresh Kumar 12487813dd6fSViresh Kumar /* Allocate space for at least one supply */ 124946f48acaSViresh Kumar count = table->regulator_count > 0 ? table->regulator_count : 1; 12507813dd6fSViresh Kumar supply_size = sizeof(*opp->supplies) * count; 12517813dd6fSViresh Kumar 12527813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 12537813dd6fSViresh Kumar opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); 12547813dd6fSViresh Kumar if (!opp) 12557813dd6fSViresh Kumar return NULL; 12567813dd6fSViresh Kumar 12577813dd6fSViresh Kumar /* Put the supplies at the end of the OPP structure as an empty array */ 12587813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 12597813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 12607813dd6fSViresh Kumar 12617813dd6fSViresh Kumar return opp; 12627813dd6fSViresh Kumar } 12637813dd6fSViresh Kumar 12647813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 12657813dd6fSViresh Kumar struct opp_table *opp_table) 12667813dd6fSViresh Kumar { 12677813dd6fSViresh Kumar struct regulator *reg; 12687813dd6fSViresh Kumar int i; 12697813dd6fSViresh Kumar 127090e3577bSViresh Kumar if (!opp_table->regulators) 127190e3577bSViresh Kumar return true; 127290e3577bSViresh Kumar 12737813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 12747813dd6fSViresh Kumar reg = opp_table->regulators[i]; 12757813dd6fSViresh Kumar 12767813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 12777813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 12787813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 12797813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 12807813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 12817813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 12827813dd6fSViresh Kumar return false; 12837813dd6fSViresh Kumar } 12847813dd6fSViresh Kumar } 12857813dd6fSViresh Kumar 12867813dd6fSViresh Kumar return true; 12877813dd6fSViresh Kumar } 12887813dd6fSViresh Kumar 1289*6c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2) 1290*6c591eecSSaravana Kannan { 1291*6c591eecSSaravana Kannan if (opp1->rate != opp2->rate) 1292*6c591eecSSaravana Kannan return opp1->rate < opp2->rate ? -1 : 1; 1293*6c591eecSSaravana Kannan if (opp1->level != opp2->level) 1294*6c591eecSSaravana Kannan return opp1->level < opp2->level ? -1 : 1; 1295*6c591eecSSaravana Kannan return 0; 1296*6c591eecSSaravana Kannan } 1297*6c591eecSSaravana Kannan 1298a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1299a1e8c136SViresh Kumar struct opp_table *opp_table, 1300a1e8c136SViresh Kumar struct list_head **head) 1301a1e8c136SViresh Kumar { 1302a1e8c136SViresh Kumar struct dev_pm_opp *opp; 1303*6c591eecSSaravana Kannan int opp_cmp; 1304a1e8c136SViresh Kumar 1305a1e8c136SViresh Kumar /* 1306a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1307a1e8c136SViresh Kumar * already present. 1308a1e8c136SViresh Kumar * 1309a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1310a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1311a1e8c136SViresh Kumar * loop. 1312a1e8c136SViresh Kumar */ 1313a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 1314*6c591eecSSaravana Kannan opp_cmp = _opp_compare_key(new_opp, opp); 1315*6c591eecSSaravana Kannan if (opp_cmp > 0) { 1316a1e8c136SViresh Kumar *head = &opp->node; 1317a1e8c136SViresh Kumar continue; 1318a1e8c136SViresh Kumar } 1319a1e8c136SViresh Kumar 1320*6c591eecSSaravana Kannan if (opp_cmp < 0) 1321a1e8c136SViresh Kumar return 0; 1322a1e8c136SViresh Kumar 1323a1e8c136SViresh Kumar /* Duplicate OPPs */ 1324a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1325a1e8c136SViresh Kumar __func__, opp->rate, opp->supplies[0].u_volt, 1326a1e8c136SViresh Kumar opp->available, new_opp->rate, 1327a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1328a1e8c136SViresh Kumar 1329a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1330a1e8c136SViresh Kumar return opp->available && 1331a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1332a1e8c136SViresh Kumar } 1333a1e8c136SViresh Kumar 1334a1e8c136SViresh Kumar return 0; 1335a1e8c136SViresh Kumar } 1336a1e8c136SViresh Kumar 13377813dd6fSViresh Kumar /* 13387813dd6fSViresh Kumar * Returns: 13397813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 13407813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 13417813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 13427813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 13437813dd6fSViresh Kumar * kernel try to initialize the OPP table. 13447813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 13457813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 13467813dd6fSViresh Kumar */ 13477813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1348a1e8c136SViresh Kumar struct opp_table *opp_table, bool rate_not_available) 13497813dd6fSViresh Kumar { 13507813dd6fSViresh Kumar struct list_head *head; 13517813dd6fSViresh Kumar int ret; 13527813dd6fSViresh Kumar 13537813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 13547813dd6fSViresh Kumar head = &opp_table->opp_list; 13557813dd6fSViresh Kumar 1356a1e8c136SViresh Kumar if (likely(!rate_not_available)) { 1357a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1358a1e8c136SViresh Kumar if (ret) { 13597813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 13607813dd6fSViresh Kumar return ret; 13617813dd6fSViresh Kumar } 1362a1e8c136SViresh Kumar } 13637813dd6fSViresh Kumar 13647813dd6fSViresh Kumar list_add(&new_opp->node, head); 13657813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 13667813dd6fSViresh Kumar 13677813dd6fSViresh Kumar new_opp->opp_table = opp_table; 13687813dd6fSViresh Kumar kref_init(&new_opp->kref); 13697813dd6fSViresh Kumar 1370a2dea4cbSGreg Kroah-Hartman opp_debug_create_one(new_opp, opp_table); 13717813dd6fSViresh Kumar 13727813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 13737813dd6fSViresh Kumar new_opp->available = false; 13747813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 13757813dd6fSViresh Kumar __func__, new_opp->rate); 13767813dd6fSViresh Kumar } 13777813dd6fSViresh Kumar 13787813dd6fSViresh Kumar return 0; 13797813dd6fSViresh Kumar } 13807813dd6fSViresh Kumar 13817813dd6fSViresh Kumar /** 13827813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 13837813dd6fSViresh Kumar * @opp_table: OPP table 13847813dd6fSViresh Kumar * @dev: device for which we do this operation 13857813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 13867813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 13877813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 13887813dd6fSViresh Kumar * 13897813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 13907813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 13917813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 13927813dd6fSViresh Kumar * 13937813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 13947813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 13957813dd6fSViresh Kumar * 13967813dd6fSViresh Kumar * Return: 13977813dd6fSViresh Kumar * 0 On success OR 13987813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 13997813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 14007813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 14017813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 14027813dd6fSViresh Kumar */ 14037813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 14047813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 14057813dd6fSViresh Kumar { 14067813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 14077813dd6fSViresh Kumar unsigned long tol; 14087813dd6fSViresh Kumar int ret; 14097813dd6fSViresh Kumar 14107813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 14117813dd6fSViresh Kumar if (!new_opp) 14127813dd6fSViresh Kumar return -ENOMEM; 14137813dd6fSViresh Kumar 14147813dd6fSViresh Kumar /* populate the opp table */ 14157813dd6fSViresh Kumar new_opp->rate = freq; 14167813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 14177813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 14187813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 14197813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 14207813dd6fSViresh Kumar new_opp->available = true; 14217813dd6fSViresh Kumar new_opp->dynamic = dynamic; 14227813dd6fSViresh Kumar 1423a1e8c136SViresh Kumar ret = _opp_add(dev, new_opp, opp_table, false); 14247813dd6fSViresh Kumar if (ret) { 14257813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 14267813dd6fSViresh Kumar if (ret == -EBUSY) 14277813dd6fSViresh Kumar ret = 0; 14287813dd6fSViresh Kumar goto free_opp; 14297813dd6fSViresh Kumar } 14307813dd6fSViresh Kumar 14317813dd6fSViresh Kumar /* 14327813dd6fSViresh Kumar * Notify the changes in the availability of the operable 14337813dd6fSViresh Kumar * frequency/voltage list. 14347813dd6fSViresh Kumar */ 14357813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 14367813dd6fSViresh Kumar return 0; 14377813dd6fSViresh Kumar 14387813dd6fSViresh Kumar free_opp: 14397813dd6fSViresh Kumar _opp_free(new_opp); 14407813dd6fSViresh Kumar 14417813dd6fSViresh Kumar return ret; 14427813dd6fSViresh Kumar } 14437813dd6fSViresh Kumar 14447813dd6fSViresh Kumar /** 14457813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw() - Set supported platforms 14467813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 14477813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 14487813dd6fSViresh Kumar * @count: Number of elements in the array. 14497813dd6fSViresh Kumar * 14507813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 14517813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 14527813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 14537813dd6fSViresh Kumar * property. 14547813dd6fSViresh Kumar */ 14557813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, 14567813dd6fSViresh Kumar const u32 *versions, unsigned int count) 14577813dd6fSViresh Kumar { 14587813dd6fSViresh Kumar struct opp_table *opp_table; 14597813dd6fSViresh Kumar 14607813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 14617813dd6fSViresh Kumar if (!opp_table) 14627813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 14637813dd6fSViresh Kumar 14647813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 14657813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 14667813dd6fSViresh Kumar 146725419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 146825419de1SViresh Kumar if (opp_table->supported_hw) 146925419de1SViresh Kumar return opp_table; 14707813dd6fSViresh Kumar 14717813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 14727813dd6fSViresh Kumar GFP_KERNEL); 14737813dd6fSViresh Kumar if (!opp_table->supported_hw) { 147425419de1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 147525419de1SViresh Kumar return ERR_PTR(-ENOMEM); 14767813dd6fSViresh Kumar } 14777813dd6fSViresh Kumar 14787813dd6fSViresh Kumar opp_table->supported_hw_count = count; 14797813dd6fSViresh Kumar 14807813dd6fSViresh Kumar return opp_table; 14817813dd6fSViresh Kumar } 14827813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); 14837813dd6fSViresh Kumar 14847813dd6fSViresh Kumar /** 14857813dd6fSViresh Kumar * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw 14867813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). 14877813dd6fSViresh Kumar * 14887813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 14897813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure 14907813dd6fSViresh Kumar * will not be freed. 14917813dd6fSViresh Kumar */ 14927813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) 14937813dd6fSViresh Kumar { 14947813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 14957813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 14967813dd6fSViresh Kumar 14977813dd6fSViresh Kumar kfree(opp_table->supported_hw); 14987813dd6fSViresh Kumar opp_table->supported_hw = NULL; 14997813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 15007813dd6fSViresh Kumar 15017813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15027813dd6fSViresh Kumar } 15037813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); 15047813dd6fSViresh Kumar 15057813dd6fSViresh Kumar /** 15067813dd6fSViresh Kumar * dev_pm_opp_set_prop_name() - Set prop-extn name 15077813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 15087813dd6fSViresh Kumar * @name: name to postfix to properties. 15097813dd6fSViresh Kumar * 15107813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 15117813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 15127813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 15137813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 15147813dd6fSViresh Kumar */ 15157813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 15167813dd6fSViresh Kumar { 15177813dd6fSViresh Kumar struct opp_table *opp_table; 15187813dd6fSViresh Kumar 15197813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 15207813dd6fSViresh Kumar if (!opp_table) 15217813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 15227813dd6fSViresh Kumar 15237813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 15247813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 15257813dd6fSViresh Kumar 1526878ec1a9SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 1527878ec1a9SViresh Kumar if (opp_table->prop_name) 1528878ec1a9SViresh Kumar return opp_table; 15297813dd6fSViresh Kumar 15307813dd6fSViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 15317813dd6fSViresh Kumar if (!opp_table->prop_name) { 1532878ec1a9SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1533878ec1a9SViresh Kumar return ERR_PTR(-ENOMEM); 15347813dd6fSViresh Kumar } 15357813dd6fSViresh Kumar 15367813dd6fSViresh Kumar return opp_table; 15377813dd6fSViresh Kumar } 15387813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); 15397813dd6fSViresh Kumar 15407813dd6fSViresh Kumar /** 15417813dd6fSViresh Kumar * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name 15427813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). 15437813dd6fSViresh Kumar * 15447813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 15457813dd6fSViresh Kumar * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure 15467813dd6fSViresh Kumar * will not be freed. 15477813dd6fSViresh Kumar */ 15487813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table) 15497813dd6fSViresh Kumar { 15507813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 15517813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 15527813dd6fSViresh Kumar 15537813dd6fSViresh Kumar kfree(opp_table->prop_name); 15547813dd6fSViresh Kumar opp_table->prop_name = NULL; 15557813dd6fSViresh Kumar 15567813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15577813dd6fSViresh Kumar } 15587813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); 15597813dd6fSViresh Kumar 15607813dd6fSViresh Kumar static int _allocate_set_opp_data(struct opp_table *opp_table) 15617813dd6fSViresh Kumar { 15627813dd6fSViresh Kumar struct dev_pm_set_opp_data *data; 15637813dd6fSViresh Kumar int len, count = opp_table->regulator_count; 15647813dd6fSViresh Kumar 156590e3577bSViresh Kumar if (WARN_ON(!opp_table->regulators)) 15667813dd6fSViresh Kumar return -EINVAL; 15677813dd6fSViresh Kumar 15687813dd6fSViresh Kumar /* space for set_opp_data */ 15697813dd6fSViresh Kumar len = sizeof(*data); 15707813dd6fSViresh Kumar 15717813dd6fSViresh Kumar /* space for old_opp.supplies and new_opp.supplies */ 15727813dd6fSViresh Kumar len += 2 * sizeof(struct dev_pm_opp_supply) * count; 15737813dd6fSViresh Kumar 15747813dd6fSViresh Kumar data = kzalloc(len, GFP_KERNEL); 15757813dd6fSViresh Kumar if (!data) 15767813dd6fSViresh Kumar return -ENOMEM; 15777813dd6fSViresh Kumar 15787813dd6fSViresh Kumar data->old_opp.supplies = (void *)(data + 1); 15797813dd6fSViresh Kumar data->new_opp.supplies = data->old_opp.supplies + count; 15807813dd6fSViresh Kumar 15817813dd6fSViresh Kumar opp_table->set_opp_data = data; 15827813dd6fSViresh Kumar 15837813dd6fSViresh Kumar return 0; 15847813dd6fSViresh Kumar } 15857813dd6fSViresh Kumar 15867813dd6fSViresh Kumar static void _free_set_opp_data(struct opp_table *opp_table) 15877813dd6fSViresh Kumar { 15887813dd6fSViresh Kumar kfree(opp_table->set_opp_data); 15897813dd6fSViresh Kumar opp_table->set_opp_data = NULL; 15907813dd6fSViresh Kumar } 15917813dd6fSViresh Kumar 15927813dd6fSViresh Kumar /** 15937813dd6fSViresh Kumar * dev_pm_opp_set_regulators() - Set regulator names for the device 15947813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 15957813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 15967813dd6fSViresh Kumar * @count: Number of regulators. 15977813dd6fSViresh Kumar * 15987813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 15997813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 16007813dd6fSViresh Kumar * well. 16017813dd6fSViresh Kumar * 16027813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 16037813dd6fSViresh Kumar */ 16047813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev, 16057813dd6fSViresh Kumar const char * const names[], 16067813dd6fSViresh Kumar unsigned int count) 16077813dd6fSViresh Kumar { 16087813dd6fSViresh Kumar struct opp_table *opp_table; 16097813dd6fSViresh Kumar struct regulator *reg; 16107813dd6fSViresh Kumar int ret, i; 16117813dd6fSViresh Kumar 16127813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 16137813dd6fSViresh Kumar if (!opp_table) 16147813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 16157813dd6fSViresh Kumar 16167813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 16177813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 16187813dd6fSViresh Kumar ret = -EBUSY; 16197813dd6fSViresh Kumar goto err; 16207813dd6fSViresh Kumar } 16217813dd6fSViresh Kumar 1622779b783cSViresh Kumar /* Another CPU that shares the OPP table has set the regulators ? */ 1623779b783cSViresh Kumar if (opp_table->regulators) 1624779b783cSViresh Kumar return opp_table; 16257813dd6fSViresh Kumar 16267813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 16277813dd6fSViresh Kumar sizeof(*opp_table->regulators), 16287813dd6fSViresh Kumar GFP_KERNEL); 16297813dd6fSViresh Kumar if (!opp_table->regulators) { 16307813dd6fSViresh Kumar ret = -ENOMEM; 16317813dd6fSViresh Kumar goto err; 16327813dd6fSViresh Kumar } 16337813dd6fSViresh Kumar 16347813dd6fSViresh Kumar for (i = 0; i < count; i++) { 16357813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 16367813dd6fSViresh Kumar if (IS_ERR(reg)) { 16377813dd6fSViresh Kumar ret = PTR_ERR(reg); 16387813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) 16397813dd6fSViresh Kumar dev_err(dev, "%s: no regulator (%s) found: %d\n", 16407813dd6fSViresh Kumar __func__, names[i], ret); 16417813dd6fSViresh Kumar goto free_regulators; 16427813dd6fSViresh Kumar } 16437813dd6fSViresh Kumar 16447813dd6fSViresh Kumar opp_table->regulators[i] = reg; 16457813dd6fSViresh Kumar } 16467813dd6fSViresh Kumar 16477813dd6fSViresh Kumar opp_table->regulator_count = count; 16487813dd6fSViresh Kumar 16497813dd6fSViresh Kumar /* Allocate block only once to pass to set_opp() routines */ 16507813dd6fSViresh Kumar ret = _allocate_set_opp_data(opp_table); 16517813dd6fSViresh Kumar if (ret) 16527813dd6fSViresh Kumar goto free_regulators; 16537813dd6fSViresh Kumar 16547813dd6fSViresh Kumar return opp_table; 16557813dd6fSViresh Kumar 16567813dd6fSViresh Kumar free_regulators: 165724957db1SMarek Szyprowski while (i != 0) 165824957db1SMarek Szyprowski regulator_put(opp_table->regulators[--i]); 16597813dd6fSViresh Kumar 16607813dd6fSViresh Kumar kfree(opp_table->regulators); 16617813dd6fSViresh Kumar opp_table->regulators = NULL; 166246f48acaSViresh Kumar opp_table->regulator_count = -1; 16637813dd6fSViresh Kumar err: 16647813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16657813dd6fSViresh Kumar 16667813dd6fSViresh Kumar return ERR_PTR(ret); 16677813dd6fSViresh Kumar } 16687813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); 16697813dd6fSViresh Kumar 16707813dd6fSViresh Kumar /** 16717813dd6fSViresh Kumar * dev_pm_opp_put_regulators() - Releases resources blocked for regulator 16727813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). 16737813dd6fSViresh Kumar */ 16747813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table) 16757813dd6fSViresh Kumar { 16767813dd6fSViresh Kumar int i; 16777813dd6fSViresh Kumar 1678779b783cSViresh Kumar if (!opp_table->regulators) 1679779b783cSViresh Kumar goto put_opp_table; 16807813dd6fSViresh Kumar 16817813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 16827813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 16837813dd6fSViresh Kumar 168424957db1SMarek Szyprowski for (i = opp_table->regulator_count - 1; i >= 0; i--) 16857813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 16867813dd6fSViresh Kumar 16877813dd6fSViresh Kumar _free_set_opp_data(opp_table); 16887813dd6fSViresh Kumar 16897813dd6fSViresh Kumar kfree(opp_table->regulators); 16907813dd6fSViresh Kumar opp_table->regulators = NULL; 169146f48acaSViresh Kumar opp_table->regulator_count = -1; 16927813dd6fSViresh Kumar 1693779b783cSViresh Kumar put_opp_table: 16947813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16957813dd6fSViresh Kumar } 16967813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); 16977813dd6fSViresh Kumar 16987813dd6fSViresh Kumar /** 16997813dd6fSViresh Kumar * dev_pm_opp_set_clkname() - Set clk name for the device 17007813dd6fSViresh Kumar * @dev: Device for which clk name is being set. 17017813dd6fSViresh Kumar * @name: Clk name. 17027813dd6fSViresh Kumar * 17037813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to get pointer to the 17047813dd6fSViresh Kumar * clock for the device. Simple cases work fine without using this routine (i.e. 17057813dd6fSViresh Kumar * by passing connection-id as NULL), but for a device with multiple clocks 17067813dd6fSViresh Kumar * available, the OPP core needs to know the exact name of the clk to use. 17077813dd6fSViresh Kumar * 17087813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 17097813dd6fSViresh Kumar */ 17107813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) 17117813dd6fSViresh Kumar { 17127813dd6fSViresh Kumar struct opp_table *opp_table; 17137813dd6fSViresh Kumar int ret; 17147813dd6fSViresh Kumar 17157813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 17167813dd6fSViresh Kumar if (!opp_table) 17177813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 17187813dd6fSViresh Kumar 17197813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 17207813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 17217813dd6fSViresh Kumar ret = -EBUSY; 17227813dd6fSViresh Kumar goto err; 17237813dd6fSViresh Kumar } 17247813dd6fSViresh Kumar 17257813dd6fSViresh Kumar /* Already have default clk set, free it */ 17267813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 17277813dd6fSViresh Kumar clk_put(opp_table->clk); 17287813dd6fSViresh Kumar 17297813dd6fSViresh Kumar /* Find clk for the device */ 17307813dd6fSViresh Kumar opp_table->clk = clk_get(dev, name); 17317813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 17327813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 17337813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) { 17347813dd6fSViresh Kumar dev_err(dev, "%s: Couldn't find clock: %d\n", __func__, 17357813dd6fSViresh Kumar ret); 17367813dd6fSViresh Kumar } 17377813dd6fSViresh Kumar goto err; 17387813dd6fSViresh Kumar } 17397813dd6fSViresh Kumar 17407813dd6fSViresh Kumar return opp_table; 17417813dd6fSViresh Kumar 17427813dd6fSViresh Kumar err: 17437813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17447813dd6fSViresh Kumar 17457813dd6fSViresh Kumar return ERR_PTR(ret); 17467813dd6fSViresh Kumar } 17477813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); 17487813dd6fSViresh Kumar 17497813dd6fSViresh Kumar /** 17507813dd6fSViresh Kumar * dev_pm_opp_put_clkname() - Releases resources blocked for clk. 17517813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). 17527813dd6fSViresh Kumar */ 17537813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table) 17547813dd6fSViresh Kumar { 17557813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 17567813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 17577813dd6fSViresh Kumar 17587813dd6fSViresh Kumar clk_put(opp_table->clk); 17597813dd6fSViresh Kumar opp_table->clk = ERR_PTR(-EINVAL); 17607813dd6fSViresh Kumar 17617813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17627813dd6fSViresh Kumar } 17637813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); 17647813dd6fSViresh Kumar 17657813dd6fSViresh Kumar /** 17667813dd6fSViresh Kumar * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper 17677813dd6fSViresh Kumar * @dev: Device for which the helper is getting registered. 17687813dd6fSViresh Kumar * @set_opp: Custom set OPP helper. 17697813dd6fSViresh Kumar * 17707813dd6fSViresh Kumar * This is useful to support complex platforms (like platforms with multiple 17717813dd6fSViresh Kumar * regulators per device), instead of the generic OPP set rate helper. 17727813dd6fSViresh Kumar * 17737813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 17747813dd6fSViresh Kumar */ 17757813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 17767813dd6fSViresh Kumar int (*set_opp)(struct dev_pm_set_opp_data *data)) 17777813dd6fSViresh Kumar { 17787813dd6fSViresh Kumar struct opp_table *opp_table; 17797813dd6fSViresh Kumar 17807813dd6fSViresh Kumar if (!set_opp) 17817813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 17827813dd6fSViresh Kumar 17837813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 17847813dd6fSViresh Kumar if (!opp_table) 17857813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 17867813dd6fSViresh Kumar 17877813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 17887813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 17895019acc6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17905019acc6SViresh Kumar return ERR_PTR(-EBUSY); 17917813dd6fSViresh Kumar } 17927813dd6fSViresh Kumar 17935019acc6SViresh Kumar /* Another CPU that shares the OPP table has set the helper ? */ 17945019acc6SViresh Kumar if (!opp_table->set_opp) 17957813dd6fSViresh Kumar opp_table->set_opp = set_opp; 17967813dd6fSViresh Kumar 17977813dd6fSViresh Kumar return opp_table; 17987813dd6fSViresh Kumar } 17997813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); 18007813dd6fSViresh Kumar 18017813dd6fSViresh Kumar /** 1802604a7aebSViresh Kumar * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for 18037813dd6fSViresh Kumar * set_opp helper 18047813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). 18057813dd6fSViresh Kumar * 18067813dd6fSViresh Kumar * Release resources blocked for platform specific set_opp helper. 18077813dd6fSViresh Kumar */ 1808604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) 18097813dd6fSViresh Kumar { 18107813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 18117813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 18127813dd6fSViresh Kumar 18137813dd6fSViresh Kumar opp_table->set_opp = NULL; 18147813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 18157813dd6fSViresh Kumar } 1816604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); 18177813dd6fSViresh Kumar 18186319aee1SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table) 18196319aee1SViresh Kumar { 18206319aee1SViresh Kumar int index; 18216319aee1SViresh Kumar 18226319aee1SViresh Kumar for (index = 0; index < opp_table->required_opp_count; index++) { 18236319aee1SViresh Kumar if (!opp_table->genpd_virt_devs[index]) 18246319aee1SViresh Kumar continue; 18256319aee1SViresh Kumar 18266319aee1SViresh Kumar dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 18276319aee1SViresh Kumar opp_table->genpd_virt_devs[index] = NULL; 18286319aee1SViresh Kumar } 1829c0ab9e08SViresh Kumar 1830c0ab9e08SViresh Kumar kfree(opp_table->genpd_virt_devs); 1831c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = NULL; 18326319aee1SViresh Kumar } 18336319aee1SViresh Kumar 18347813dd6fSViresh Kumar /** 18356319aee1SViresh Kumar * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 18366319aee1SViresh Kumar * @dev: Consumer device for which the genpd is getting attached. 18376319aee1SViresh Kumar * @names: Null terminated array of pointers containing names of genpd to attach. 183817a8f868SViresh Kumar * @virt_devs: Pointer to return the array of virtual devices. 18394f018bc0SViresh Kumar * 18404f018bc0SViresh Kumar * Multiple generic power domains for a device are supported with the help of 18414f018bc0SViresh Kumar * virtual genpd devices, which are created for each consumer device - genpd 18424f018bc0SViresh Kumar * pair. These are the device structures which are attached to the power domain 18434f018bc0SViresh Kumar * and are required by the OPP core to set the performance state of the genpd. 18446319aee1SViresh Kumar * The same API also works for the case where single genpd is available and so 18456319aee1SViresh Kumar * we don't need to support that separately. 18464f018bc0SViresh Kumar * 18474f018bc0SViresh Kumar * This helper will normally be called by the consumer driver of the device 18486319aee1SViresh Kumar * "dev", as only that has details of the genpd names. 18494f018bc0SViresh Kumar * 18506319aee1SViresh Kumar * This helper needs to be called once with a list of all genpd to attach. 18516319aee1SViresh Kumar * Otherwise the original device structure will be used instead by the OPP core. 1852baea35e4SViresh Kumar * 1853baea35e4SViresh Kumar * The order of entries in the names array must match the order in which 1854baea35e4SViresh Kumar * "required-opps" are added in DT. 18554f018bc0SViresh Kumar */ 185617a8f868SViresh Kumar struct opp_table *dev_pm_opp_attach_genpd(struct device *dev, 185717a8f868SViresh Kumar const char **names, struct device ***virt_devs) 18584f018bc0SViresh Kumar { 18594f018bc0SViresh Kumar struct opp_table *opp_table; 18606319aee1SViresh Kumar struct device *virt_dev; 1861baea35e4SViresh Kumar int index = 0, ret = -EINVAL; 18626319aee1SViresh Kumar const char **name = names; 18634f018bc0SViresh Kumar 18644f018bc0SViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 18654f018bc0SViresh Kumar if (!opp_table) 18664f018bc0SViresh Kumar return ERR_PTR(-ENOMEM); 18674f018bc0SViresh Kumar 18686319aee1SViresh Kumar /* 18696319aee1SViresh Kumar * If the genpd's OPP table isn't already initialized, parsing of the 18706319aee1SViresh Kumar * required-opps fail for dev. We should retry this after genpd's OPP 18716319aee1SViresh Kumar * table is added. 18726319aee1SViresh Kumar */ 18736319aee1SViresh Kumar if (!opp_table->required_opp_count) { 18746319aee1SViresh Kumar ret = -EPROBE_DEFER; 18756319aee1SViresh Kumar goto put_table; 18766319aee1SViresh Kumar } 18776319aee1SViresh Kumar 18784f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 18794f018bc0SViresh Kumar 1880c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, 1881c0ab9e08SViresh Kumar sizeof(*opp_table->genpd_virt_devs), 1882c0ab9e08SViresh Kumar GFP_KERNEL); 1883c0ab9e08SViresh Kumar if (!opp_table->genpd_virt_devs) 1884c0ab9e08SViresh Kumar goto unlock; 18854f018bc0SViresh Kumar 18866319aee1SViresh Kumar while (*name) { 18876319aee1SViresh Kumar if (index >= opp_table->required_opp_count) { 18886319aee1SViresh Kumar dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 18896319aee1SViresh Kumar *name, opp_table->required_opp_count, index); 18906319aee1SViresh Kumar goto err; 18916319aee1SViresh Kumar } 18924f018bc0SViresh Kumar 18936319aee1SViresh Kumar if (opp_table->genpd_virt_devs[index]) { 18946319aee1SViresh Kumar dev_err(dev, "Genpd virtual device already set %s\n", 18956319aee1SViresh Kumar *name); 18966319aee1SViresh Kumar goto err; 18976319aee1SViresh Kumar } 18986319aee1SViresh Kumar 18996319aee1SViresh Kumar virt_dev = dev_pm_domain_attach_by_name(dev, *name); 19006319aee1SViresh Kumar if (IS_ERR(virt_dev)) { 19016319aee1SViresh Kumar ret = PTR_ERR(virt_dev); 19026319aee1SViresh Kumar dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 19036319aee1SViresh Kumar goto err; 19044f018bc0SViresh Kumar } 19054f018bc0SViresh Kumar 19064f018bc0SViresh Kumar opp_table->genpd_virt_devs[index] = virt_dev; 1907baea35e4SViresh Kumar index++; 19086319aee1SViresh Kumar name++; 19096319aee1SViresh Kumar } 19106319aee1SViresh Kumar 191117a8f868SViresh Kumar if (virt_devs) 191217a8f868SViresh Kumar *virt_devs = opp_table->genpd_virt_devs; 19134f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 19144f018bc0SViresh Kumar 19154f018bc0SViresh Kumar return opp_table; 19166319aee1SViresh Kumar 19176319aee1SViresh Kumar err: 19186319aee1SViresh Kumar _opp_detach_genpd(opp_table); 1919c0ab9e08SViresh Kumar unlock: 19206319aee1SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 19216319aee1SViresh Kumar 19226319aee1SViresh Kumar put_table: 19236319aee1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 19246319aee1SViresh Kumar 19256319aee1SViresh Kumar return ERR_PTR(ret); 19264f018bc0SViresh Kumar } 19276319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd); 19284f018bc0SViresh Kumar 19294f018bc0SViresh Kumar /** 19306319aee1SViresh Kumar * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device. 19316319aee1SViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_attach_genpd(). 19324f018bc0SViresh Kumar * 19336319aee1SViresh Kumar * This detaches the genpd(s), resets the virtual device pointers, and puts the 19346319aee1SViresh Kumar * OPP table. 19354f018bc0SViresh Kumar */ 19366319aee1SViresh Kumar void dev_pm_opp_detach_genpd(struct opp_table *opp_table) 19374f018bc0SViresh Kumar { 19384f018bc0SViresh Kumar /* 19394f018bc0SViresh Kumar * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 19404f018bc0SViresh Kumar * used in parallel. 19414f018bc0SViresh Kumar */ 19424f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 19436319aee1SViresh Kumar _opp_detach_genpd(opp_table); 19444f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 19454f018bc0SViresh Kumar 19466319aee1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 19474f018bc0SViresh Kumar } 19486319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd); 19494f018bc0SViresh Kumar 19504f018bc0SViresh Kumar /** 1951c8a59103SViresh Kumar * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 1952c8a59103SViresh Kumar * @src_table: OPP table which has dst_table as one of its required OPP table. 1953c8a59103SViresh Kumar * @dst_table: Required OPP table of the src_table. 1954c8a59103SViresh Kumar * @pstate: Current performance state of the src_table. 1955c8a59103SViresh Kumar * 1956c8a59103SViresh Kumar * This Returns pstate of the OPP (present in @dst_table) pointed out by the 1957c8a59103SViresh Kumar * "required-opps" property of the OPP (present in @src_table) which has 1958c8a59103SViresh Kumar * performance state set to @pstate. 1959c8a59103SViresh Kumar * 1960c8a59103SViresh Kumar * Return: Zero or positive performance state on success, otherwise negative 1961c8a59103SViresh Kumar * value on errors. 1962c8a59103SViresh Kumar */ 1963c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 1964c8a59103SViresh Kumar struct opp_table *dst_table, 1965c8a59103SViresh Kumar unsigned int pstate) 1966c8a59103SViresh Kumar { 1967c8a59103SViresh Kumar struct dev_pm_opp *opp; 1968c8a59103SViresh Kumar int dest_pstate = -EINVAL; 1969c8a59103SViresh Kumar int i; 1970c8a59103SViresh Kumar 1971c8a59103SViresh Kumar if (!pstate) 1972c8a59103SViresh Kumar return 0; 1973c8a59103SViresh Kumar 1974c8a59103SViresh Kumar /* 1975c8a59103SViresh Kumar * Normally the src_table will have the "required_opps" property set to 1976c8a59103SViresh Kumar * point to one of the OPPs in the dst_table, but in some cases the 1977c8a59103SViresh Kumar * genpd and its master have one to one mapping of performance states 1978c8a59103SViresh Kumar * and so none of them have the "required-opps" property set. Return the 1979c8a59103SViresh Kumar * pstate of the src_table as it is in such cases. 1980c8a59103SViresh Kumar */ 1981c8a59103SViresh Kumar if (!src_table->required_opp_count) 1982c8a59103SViresh Kumar return pstate; 1983c8a59103SViresh Kumar 1984c8a59103SViresh Kumar for (i = 0; i < src_table->required_opp_count; i++) { 1985c8a59103SViresh Kumar if (src_table->required_opp_tables[i]->np == dst_table->np) 1986c8a59103SViresh Kumar break; 1987c8a59103SViresh Kumar } 1988c8a59103SViresh Kumar 1989c8a59103SViresh Kumar if (unlikely(i == src_table->required_opp_count)) { 1990c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 1991c8a59103SViresh Kumar __func__, src_table, dst_table); 1992c8a59103SViresh Kumar return -EINVAL; 1993c8a59103SViresh Kumar } 1994c8a59103SViresh Kumar 1995c8a59103SViresh Kumar mutex_lock(&src_table->lock); 1996c8a59103SViresh Kumar 1997c8a59103SViresh Kumar list_for_each_entry(opp, &src_table->opp_list, node) { 1998c8a59103SViresh Kumar if (opp->pstate == pstate) { 1999c8a59103SViresh Kumar dest_pstate = opp->required_opps[i]->pstate; 2000c8a59103SViresh Kumar goto unlock; 2001c8a59103SViresh Kumar } 2002c8a59103SViresh Kumar } 2003c8a59103SViresh Kumar 2004c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 2005c8a59103SViresh Kumar dst_table); 2006c8a59103SViresh Kumar 2007c8a59103SViresh Kumar unlock: 2008c8a59103SViresh Kumar mutex_unlock(&src_table->lock); 2009c8a59103SViresh Kumar 2010c8a59103SViresh Kumar return dest_pstate; 2011c8a59103SViresh Kumar } 2012c8a59103SViresh Kumar 2013c8a59103SViresh Kumar /** 20147813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 20157813dd6fSViresh Kumar * @dev: device for which we do this operation 20167813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 20177813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 20187813dd6fSViresh Kumar * 20197813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 20207813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 20217813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 20227813dd6fSViresh Kumar * 20237813dd6fSViresh Kumar * Return: 20247813dd6fSViresh Kumar * 0 On success OR 20257813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 20267813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 20277813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 20287813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 20297813dd6fSViresh Kumar */ 20307813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 20317813dd6fSViresh Kumar { 20327813dd6fSViresh Kumar struct opp_table *opp_table; 20337813dd6fSViresh Kumar int ret; 20347813dd6fSViresh Kumar 20357813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 20367813dd6fSViresh Kumar if (!opp_table) 20377813dd6fSViresh Kumar return -ENOMEM; 20387813dd6fSViresh Kumar 203946f48acaSViresh Kumar /* Fix regulator count for dynamic OPPs */ 204046f48acaSViresh Kumar opp_table->regulator_count = 1; 204146f48acaSViresh Kumar 20427813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 20430ad8c623SViresh Kumar if (ret) 20447813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 20450ad8c623SViresh Kumar 20467813dd6fSViresh Kumar return ret; 20477813dd6fSViresh Kumar } 20487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 20497813dd6fSViresh Kumar 20507813dd6fSViresh Kumar /** 20517813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 20527813dd6fSViresh Kumar * @dev: device for which we do this operation 20537813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 20547813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 20557813dd6fSViresh Kumar * 20567813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 20577813dd6fSViresh Kumar * which is isolated here. 20587813dd6fSViresh Kumar * 20597813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 20607813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 20617813dd6fSViresh Kumar * successful. 20627813dd6fSViresh Kumar */ 20637813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 20647813dd6fSViresh Kumar bool availability_req) 20657813dd6fSViresh Kumar { 20667813dd6fSViresh Kumar struct opp_table *opp_table; 20677813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 20687813dd6fSViresh Kumar int r = 0; 20697813dd6fSViresh Kumar 20707813dd6fSViresh Kumar /* Find the opp_table */ 20717813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 20727813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 20737813dd6fSViresh Kumar r = PTR_ERR(opp_table); 20747813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 20757813dd6fSViresh Kumar return r; 20767813dd6fSViresh Kumar } 20777813dd6fSViresh Kumar 20787813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 20797813dd6fSViresh Kumar 20807813dd6fSViresh Kumar /* Do we have the frequency? */ 20817813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 20827813dd6fSViresh Kumar if (tmp_opp->rate == freq) { 20837813dd6fSViresh Kumar opp = tmp_opp; 20847813dd6fSViresh Kumar break; 20857813dd6fSViresh Kumar } 20867813dd6fSViresh Kumar } 20877813dd6fSViresh Kumar 20887813dd6fSViresh Kumar if (IS_ERR(opp)) { 20897813dd6fSViresh Kumar r = PTR_ERR(opp); 20907813dd6fSViresh Kumar goto unlock; 20917813dd6fSViresh Kumar } 20927813dd6fSViresh Kumar 20937813dd6fSViresh Kumar /* Is update really needed? */ 20947813dd6fSViresh Kumar if (opp->available == availability_req) 20957813dd6fSViresh Kumar goto unlock; 20967813dd6fSViresh Kumar 20977813dd6fSViresh Kumar opp->available = availability_req; 20987813dd6fSViresh Kumar 20997813dd6fSViresh Kumar dev_pm_opp_get(opp); 21007813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 21017813dd6fSViresh Kumar 21027813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 21037813dd6fSViresh Kumar if (availability_req) 21047813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 21057813dd6fSViresh Kumar opp); 21067813dd6fSViresh Kumar else 21077813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 21087813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 21097813dd6fSViresh Kumar 21107813dd6fSViresh Kumar dev_pm_opp_put(opp); 21117813dd6fSViresh Kumar goto put_table; 21127813dd6fSViresh Kumar 21137813dd6fSViresh Kumar unlock: 21147813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 21157813dd6fSViresh Kumar put_table: 21167813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 21177813dd6fSViresh Kumar return r; 21187813dd6fSViresh Kumar } 21197813dd6fSViresh Kumar 21207813dd6fSViresh Kumar /** 212125cb20a2SStephen Boyd * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP 212225cb20a2SStephen Boyd * @dev: device for which we do this operation 212325cb20a2SStephen Boyd * @freq: OPP frequency to adjust voltage of 212425cb20a2SStephen Boyd * @u_volt: new OPP target voltage 212525cb20a2SStephen Boyd * @u_volt_min: new OPP min voltage 212625cb20a2SStephen Boyd * @u_volt_max: new OPP max voltage 212725cb20a2SStephen Boyd * 212825cb20a2SStephen Boyd * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 212925cb20a2SStephen Boyd * copy operation, returns 0 if no modifcation was done OR modification was 213025cb20a2SStephen Boyd * successful. 213125cb20a2SStephen Boyd */ 213225cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq, 213325cb20a2SStephen Boyd unsigned long u_volt, unsigned long u_volt_min, 213425cb20a2SStephen Boyd unsigned long u_volt_max) 213525cb20a2SStephen Boyd 213625cb20a2SStephen Boyd { 213725cb20a2SStephen Boyd struct opp_table *opp_table; 213825cb20a2SStephen Boyd struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 213925cb20a2SStephen Boyd int r = 0; 214025cb20a2SStephen Boyd 214125cb20a2SStephen Boyd /* Find the opp_table */ 214225cb20a2SStephen Boyd opp_table = _find_opp_table(dev); 214325cb20a2SStephen Boyd if (IS_ERR(opp_table)) { 214425cb20a2SStephen Boyd r = PTR_ERR(opp_table); 214525cb20a2SStephen Boyd dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 214625cb20a2SStephen Boyd return r; 214725cb20a2SStephen Boyd } 214825cb20a2SStephen Boyd 214925cb20a2SStephen Boyd mutex_lock(&opp_table->lock); 215025cb20a2SStephen Boyd 215125cb20a2SStephen Boyd /* Do we have the frequency? */ 215225cb20a2SStephen Boyd list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 215325cb20a2SStephen Boyd if (tmp_opp->rate == freq) { 215425cb20a2SStephen Boyd opp = tmp_opp; 215525cb20a2SStephen Boyd break; 215625cb20a2SStephen Boyd } 215725cb20a2SStephen Boyd } 215825cb20a2SStephen Boyd 215925cb20a2SStephen Boyd if (IS_ERR(opp)) { 216025cb20a2SStephen Boyd r = PTR_ERR(opp); 216125cb20a2SStephen Boyd goto adjust_unlock; 216225cb20a2SStephen Boyd } 216325cb20a2SStephen Boyd 216425cb20a2SStephen Boyd /* Is update really needed? */ 216525cb20a2SStephen Boyd if (opp->supplies->u_volt == u_volt) 216625cb20a2SStephen Boyd goto adjust_unlock; 216725cb20a2SStephen Boyd 216825cb20a2SStephen Boyd opp->supplies->u_volt = u_volt; 216925cb20a2SStephen Boyd opp->supplies->u_volt_min = u_volt_min; 217025cb20a2SStephen Boyd opp->supplies->u_volt_max = u_volt_max; 217125cb20a2SStephen Boyd 217225cb20a2SStephen Boyd dev_pm_opp_get(opp); 217325cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 217425cb20a2SStephen Boyd 217525cb20a2SStephen Boyd /* Notify the voltage change of the OPP */ 217625cb20a2SStephen Boyd blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE, 217725cb20a2SStephen Boyd opp); 217825cb20a2SStephen Boyd 217925cb20a2SStephen Boyd dev_pm_opp_put(opp); 218025cb20a2SStephen Boyd goto adjust_put_table; 218125cb20a2SStephen Boyd 218225cb20a2SStephen Boyd adjust_unlock: 218325cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 218425cb20a2SStephen Boyd adjust_put_table: 218525cb20a2SStephen Boyd dev_pm_opp_put_opp_table(opp_table); 218625cb20a2SStephen Boyd return r; 218725cb20a2SStephen Boyd } 218825cb20a2SStephen Boyd 218925cb20a2SStephen Boyd /** 21907813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 21917813dd6fSViresh Kumar * @dev: device for which we do this operation 21927813dd6fSViresh Kumar * @freq: OPP frequency to enable 21937813dd6fSViresh Kumar * 21947813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 21957813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 21967813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 21977813dd6fSViresh Kumar * 21987813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 21997813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 22007813dd6fSViresh Kumar * successful. 22017813dd6fSViresh Kumar */ 22027813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 22037813dd6fSViresh Kumar { 22047813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 22057813dd6fSViresh Kumar } 22067813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 22077813dd6fSViresh Kumar 22087813dd6fSViresh Kumar /** 22097813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 22107813dd6fSViresh Kumar * @dev: device for which we do this operation 22117813dd6fSViresh Kumar * @freq: OPP frequency to disable 22127813dd6fSViresh Kumar * 22137813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 22147813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 22157813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 22167813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 22177813dd6fSViresh Kumar * 22187813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 22197813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 22207813dd6fSViresh Kumar * successful. 22217813dd6fSViresh Kumar */ 22227813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 22237813dd6fSViresh Kumar { 22247813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 22257813dd6fSViresh Kumar } 22267813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 22277813dd6fSViresh Kumar 22287813dd6fSViresh Kumar /** 22297813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 22307813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 22317813dd6fSViresh Kumar * @nb: Notifier block to be registered 22327813dd6fSViresh Kumar * 22337813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 22347813dd6fSViresh Kumar */ 22357813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 22367813dd6fSViresh Kumar { 22377813dd6fSViresh Kumar struct opp_table *opp_table; 22387813dd6fSViresh Kumar int ret; 22397813dd6fSViresh Kumar 22407813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 22417813dd6fSViresh Kumar if (IS_ERR(opp_table)) 22427813dd6fSViresh Kumar return PTR_ERR(opp_table); 22437813dd6fSViresh Kumar 22447813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 22457813dd6fSViresh Kumar 22467813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 22477813dd6fSViresh Kumar 22487813dd6fSViresh Kumar return ret; 22497813dd6fSViresh Kumar } 22507813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 22517813dd6fSViresh Kumar 22527813dd6fSViresh Kumar /** 22537813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 22547813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 22557813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 22567813dd6fSViresh Kumar * 22577813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 22587813dd6fSViresh Kumar */ 22597813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 22607813dd6fSViresh Kumar struct notifier_block *nb) 22617813dd6fSViresh Kumar { 22627813dd6fSViresh Kumar struct opp_table *opp_table; 22637813dd6fSViresh Kumar int ret; 22647813dd6fSViresh Kumar 22657813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 22667813dd6fSViresh Kumar if (IS_ERR(opp_table)) 22677813dd6fSViresh Kumar return PTR_ERR(opp_table); 22687813dd6fSViresh Kumar 22697813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 22707813dd6fSViresh Kumar 22717813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 22727813dd6fSViresh Kumar 22737813dd6fSViresh Kumar return ret; 22747813dd6fSViresh Kumar } 22757813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 22767813dd6fSViresh Kumar 22772a4eb735SViresh Kumar void _dev_pm_opp_find_and_remove_table(struct device *dev) 22787813dd6fSViresh Kumar { 22797813dd6fSViresh Kumar struct opp_table *opp_table; 22807813dd6fSViresh Kumar 22817813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 22827813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 22837813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 22847813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 22857813dd6fSViresh Kumar 22867813dd6fSViresh Kumar if (error != -ENODEV) 22877813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 22887813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 22897813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 22907813dd6fSViresh Kumar error); 22917813dd6fSViresh Kumar return; 22927813dd6fSViresh Kumar } 22937813dd6fSViresh Kumar 229403758d60SViresh Kumar _opp_remove_all_static(opp_table); 22957813dd6fSViresh Kumar 2296cdd6ed90SViresh Kumar /* Drop reference taken by _find_opp_table() */ 2297cdd6ed90SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2298cdd6ed90SViresh Kumar 2299cdd6ed90SViresh Kumar /* Drop reference taken while the OPP table was added */ 23007813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 23017813dd6fSViresh Kumar } 23027813dd6fSViresh Kumar 23037813dd6fSViresh Kumar /** 23047813dd6fSViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 23057813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table. 23067813dd6fSViresh Kumar * 23077813dd6fSViresh Kumar * Free both OPPs created using static entries present in DT and the 23087813dd6fSViresh Kumar * dynamically added entries. 23097813dd6fSViresh Kumar */ 23107813dd6fSViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 23117813dd6fSViresh Kumar { 23122a4eb735SViresh Kumar _dev_pm_opp_find_and_remove_table(dev); 23137813dd6fSViresh Kumar } 23147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 2315