1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27813dd6fSViresh Kumar /* 37813dd6fSViresh Kumar * Generic OPP Interface 47813dd6fSViresh Kumar * 57813dd6fSViresh Kumar * Copyright (C) 2009-2010 Texas Instruments Incorporated. 67813dd6fSViresh Kumar * Nishanth Menon 77813dd6fSViresh Kumar * Romit Dasgupta 87813dd6fSViresh Kumar * Kevin Hilman 97813dd6fSViresh Kumar */ 107813dd6fSViresh Kumar 117813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 127813dd6fSViresh Kumar 137813dd6fSViresh Kumar #include <linux/clk.h> 147813dd6fSViresh Kumar #include <linux/errno.h> 157813dd6fSViresh Kumar #include <linux/err.h> 167813dd6fSViresh Kumar #include <linux/device.h> 177813dd6fSViresh Kumar #include <linux/export.h> 18009acd19SViresh Kumar #include <linux/pm_domain.h> 197813dd6fSViresh Kumar #include <linux/regulator/consumer.h> 2011b9b663SViresh Kumar #include <linux/slab.h> 2111b9b663SViresh Kumar #include <linux/xarray.h> 227813dd6fSViresh Kumar 237813dd6fSViresh Kumar #include "opp.h" 247813dd6fSViresh Kumar 257813dd6fSViresh Kumar /* 267813dd6fSViresh Kumar * The root of the list of all opp-tables. All opp_table structures branch off 277813dd6fSViresh Kumar * from here, with each opp_table containing the list of opps it supports in 287813dd6fSViresh Kumar * various states of availability. 297813dd6fSViresh Kumar */ 307813dd6fSViresh Kumar LIST_HEAD(opp_tables); 317eba0c76SViresh Kumar 327eba0c76SViresh Kumar /* OPP tables with uninitialized required OPPs */ 337eba0c76SViresh Kumar LIST_HEAD(lazy_opp_tables); 347eba0c76SViresh Kumar 357813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */ 367813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock); 3727c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */ 3827c09484SViresh Kumar static bool opp_tables_busy; 397813dd6fSViresh Kumar 4011b9b663SViresh Kumar /* OPP ID allocator */ 4111b9b663SViresh Kumar static DEFINE_XARRAY_ALLOC1(opp_configs); 4211b9b663SViresh Kumar 439e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table) 447813dd6fSViresh Kumar { 457813dd6fSViresh Kumar struct opp_device *opp_dev; 469e62edacSViresh Kumar bool found = false; 477813dd6fSViresh Kumar 489e62edacSViresh Kumar mutex_lock(&opp_table->lock); 497813dd6fSViresh Kumar list_for_each_entry(opp_dev, &opp_table->dev_list, node) 509e62edacSViresh Kumar if (opp_dev->dev == dev) { 519e62edacSViresh Kumar found = true; 529e62edacSViresh Kumar break; 539e62edacSViresh Kumar } 547813dd6fSViresh Kumar 559e62edacSViresh Kumar mutex_unlock(&opp_table->lock); 569e62edacSViresh Kumar return found; 577813dd6fSViresh Kumar } 587813dd6fSViresh Kumar 597813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev) 607813dd6fSViresh Kumar { 617813dd6fSViresh Kumar struct opp_table *opp_table; 627813dd6fSViresh Kumar 637813dd6fSViresh Kumar list_for_each_entry(opp_table, &opp_tables, node) { 649e62edacSViresh Kumar if (_find_opp_dev(dev, opp_table)) { 657813dd6fSViresh Kumar _get_opp_table_kref(opp_table); 667813dd6fSViresh Kumar return opp_table; 677813dd6fSViresh Kumar } 687813dd6fSViresh Kumar } 697813dd6fSViresh Kumar 707813dd6fSViresh Kumar return ERR_PTR(-ENODEV); 717813dd6fSViresh Kumar } 727813dd6fSViresh Kumar 737813dd6fSViresh Kumar /** 747813dd6fSViresh Kumar * _find_opp_table() - find opp_table struct using device pointer 757813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table 767813dd6fSViresh Kumar * 777813dd6fSViresh Kumar * Search OPP table for one containing matching device. 787813dd6fSViresh Kumar * 797813dd6fSViresh Kumar * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or 807813dd6fSViresh Kumar * -EINVAL based on type of error. 817813dd6fSViresh Kumar * 827813dd6fSViresh Kumar * The callers must call dev_pm_opp_put_opp_table() after the table is used. 837813dd6fSViresh Kumar */ 847813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev) 857813dd6fSViresh Kumar { 867813dd6fSViresh Kumar struct opp_table *opp_table; 877813dd6fSViresh Kumar 887813dd6fSViresh Kumar if (IS_ERR_OR_NULL(dev)) { 897813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 907813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 917813dd6fSViresh Kumar } 927813dd6fSViresh Kumar 937813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 947813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 957813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 967813dd6fSViresh Kumar 977813dd6fSViresh Kumar return opp_table; 987813dd6fSViresh Kumar } 997813dd6fSViresh Kumar 1007813dd6fSViresh Kumar /** 1017813dd6fSViresh Kumar * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp 1027813dd6fSViresh Kumar * @opp: opp for which voltage has to be returned for 1037813dd6fSViresh Kumar * 1047813dd6fSViresh Kumar * Return: voltage in micro volt corresponding to the opp, else 1057813dd6fSViresh Kumar * return 0 1067813dd6fSViresh Kumar * 1077813dd6fSViresh Kumar * This is useful only for devices with single power supply. 1087813dd6fSViresh Kumar */ 1097813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 1107813dd6fSViresh Kumar { 1117813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp)) { 1127813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1137813dd6fSViresh Kumar return 0; 1147813dd6fSViresh Kumar } 1157813dd6fSViresh Kumar 1167813dd6fSViresh Kumar return opp->supplies[0].u_volt; 1177813dd6fSViresh Kumar } 1187813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); 1197813dd6fSViresh Kumar 1207813dd6fSViresh Kumar /** 12169b1af17SViresh Kumar * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp 12269b1af17SViresh Kumar * @opp: opp for which voltage has to be returned for 12369b1af17SViresh Kumar * @supplies: Placeholder for copying the supply information. 12469b1af17SViresh Kumar * 12569b1af17SViresh Kumar * Return: negative error number on failure, 0 otherwise on success after 12669b1af17SViresh Kumar * setting @supplies. 12769b1af17SViresh Kumar * 12869b1af17SViresh Kumar * This can be used for devices with any number of power supplies. The caller 12969b1af17SViresh Kumar * must ensure the @supplies array must contain space for each regulator. 13069b1af17SViresh Kumar */ 13169b1af17SViresh Kumar int dev_pm_opp_get_supplies(struct dev_pm_opp *opp, 13269b1af17SViresh Kumar struct dev_pm_opp_supply *supplies) 13369b1af17SViresh Kumar { 13469b1af17SViresh Kumar if (IS_ERR_OR_NULL(opp) || !supplies) { 13569b1af17SViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 13669b1af17SViresh Kumar return -EINVAL; 13769b1af17SViresh Kumar } 13869b1af17SViresh Kumar 13969b1af17SViresh Kumar memcpy(supplies, opp->supplies, 14069b1af17SViresh Kumar sizeof(*supplies) * opp->opp_table->regulator_count); 14169b1af17SViresh Kumar return 0; 14269b1af17SViresh Kumar } 14369b1af17SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_supplies); 14469b1af17SViresh Kumar 14569b1af17SViresh Kumar /** 1464f9a7a1dSLukasz Luba * dev_pm_opp_get_power() - Gets the power corresponding to an opp 1474f9a7a1dSLukasz Luba * @opp: opp for which power has to be returned for 1484f9a7a1dSLukasz Luba * 1494f9a7a1dSLukasz Luba * Return: power in micro watt corresponding to the opp, else 1504f9a7a1dSLukasz Luba * return 0 1514f9a7a1dSLukasz Luba * 1524f9a7a1dSLukasz Luba * This is useful only for devices with single power supply. 1534f9a7a1dSLukasz Luba */ 1544f9a7a1dSLukasz Luba unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp) 1554f9a7a1dSLukasz Luba { 1564f9a7a1dSLukasz Luba unsigned long opp_power = 0; 1574f9a7a1dSLukasz Luba int i; 1584f9a7a1dSLukasz Luba 1594f9a7a1dSLukasz Luba if (IS_ERR_OR_NULL(opp)) { 1604f9a7a1dSLukasz Luba pr_err("%s: Invalid parameters\n", __func__); 1614f9a7a1dSLukasz Luba return 0; 1624f9a7a1dSLukasz Luba } 1634f9a7a1dSLukasz Luba for (i = 0; i < opp->opp_table->regulator_count; i++) 1644f9a7a1dSLukasz Luba opp_power += opp->supplies[i].u_watt; 1654f9a7a1dSLukasz Luba 1664f9a7a1dSLukasz Luba return opp_power; 1674f9a7a1dSLukasz Luba } 1684f9a7a1dSLukasz Luba EXPORT_SYMBOL_GPL(dev_pm_opp_get_power); 1694f9a7a1dSLukasz Luba 1704f9a7a1dSLukasz Luba /** 1717813dd6fSViresh Kumar * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp 1727813dd6fSViresh Kumar * @opp: opp for which frequency has to be returned for 1737813dd6fSViresh Kumar * 1747813dd6fSViresh Kumar * Return: frequency in hertz corresponding to the opp, else 1757813dd6fSViresh Kumar * return 0 1767813dd6fSViresh Kumar */ 1777813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) 1787813dd6fSViresh Kumar { 17906a8a059SAndrew-sh.Cheng if (IS_ERR_OR_NULL(opp)) { 1807813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1817813dd6fSViresh Kumar return 0; 1827813dd6fSViresh Kumar } 1837813dd6fSViresh Kumar 1847813dd6fSViresh Kumar return opp->rate; 1857813dd6fSViresh Kumar } 1867813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); 1877813dd6fSViresh Kumar 1887813dd6fSViresh Kumar /** 1895b93ac54SRajendra Nayak * dev_pm_opp_get_level() - Gets the level corresponding to an available opp 1905b93ac54SRajendra Nayak * @opp: opp for which level value has to be returned for 1915b93ac54SRajendra Nayak * 1925b93ac54SRajendra Nayak * Return: level read from device tree corresponding to the opp, else 1935b93ac54SRajendra Nayak * return 0. 1945b93ac54SRajendra Nayak */ 1955b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp) 1965b93ac54SRajendra Nayak { 1975b93ac54SRajendra Nayak if (IS_ERR_OR_NULL(opp) || !opp->available) { 1985b93ac54SRajendra Nayak pr_err("%s: Invalid parameters\n", __func__); 1995b93ac54SRajendra Nayak return 0; 2005b93ac54SRajendra Nayak } 2015b93ac54SRajendra Nayak 2025b93ac54SRajendra Nayak return opp->level; 2035b93ac54SRajendra Nayak } 2045b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level); 2055b93ac54SRajendra Nayak 2065b93ac54SRajendra Nayak /** 207597ff543SDmitry Osipenko * dev_pm_opp_get_required_pstate() - Gets the required performance state 208597ff543SDmitry Osipenko * corresponding to an available opp 209597ff543SDmitry Osipenko * @opp: opp for which performance state has to be returned for 210597ff543SDmitry Osipenko * @index: index of the required opp 211597ff543SDmitry Osipenko * 212597ff543SDmitry Osipenko * Return: performance state read from device tree corresponding to the 213597ff543SDmitry Osipenko * required opp, else return 0. 214597ff543SDmitry Osipenko */ 215597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp, 216597ff543SDmitry Osipenko unsigned int index) 217597ff543SDmitry Osipenko { 218597ff543SDmitry Osipenko if (IS_ERR_OR_NULL(opp) || !opp->available || 219597ff543SDmitry Osipenko index >= opp->opp_table->required_opp_count) { 220597ff543SDmitry Osipenko pr_err("%s: Invalid parameters\n", __func__); 221597ff543SDmitry Osipenko return 0; 222597ff543SDmitry Osipenko } 223597ff543SDmitry Osipenko 2247eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 2257eba0c76SViresh Kumar if (lazy_linking_pending(opp->opp_table)) 2267eba0c76SViresh Kumar return 0; 2277eba0c76SViresh Kumar 228597ff543SDmitry Osipenko return opp->required_opps[index]->pstate; 229597ff543SDmitry Osipenko } 230597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate); 231597ff543SDmitry Osipenko 232597ff543SDmitry Osipenko /** 2337813dd6fSViresh Kumar * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not 2347813dd6fSViresh Kumar * @opp: opp for which turbo mode is being verified 2357813dd6fSViresh Kumar * 2367813dd6fSViresh Kumar * Turbo OPPs are not for normal use, and can be enabled (under certain 2377813dd6fSViresh Kumar * conditions) for short duration of times to finish high throughput work 2387813dd6fSViresh Kumar * quickly. Running on them for longer times may overheat the chip. 2397813dd6fSViresh Kumar * 2407813dd6fSViresh Kumar * Return: true if opp is turbo opp, else false. 2417813dd6fSViresh Kumar */ 2427813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 2437813dd6fSViresh Kumar { 2447813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 2457813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 2467813dd6fSViresh Kumar return false; 2477813dd6fSViresh Kumar } 2487813dd6fSViresh Kumar 2497813dd6fSViresh Kumar return opp->turbo; 2507813dd6fSViresh Kumar } 2517813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 2527813dd6fSViresh Kumar 2537813dd6fSViresh Kumar /** 2547813dd6fSViresh Kumar * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds 2557813dd6fSViresh Kumar * @dev: device for which we do this operation 2567813dd6fSViresh Kumar * 2577813dd6fSViresh Kumar * Return: This function returns the max clock latency in nanoseconds. 2587813dd6fSViresh Kumar */ 2597813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 2607813dd6fSViresh Kumar { 2617813dd6fSViresh Kumar struct opp_table *opp_table; 2627813dd6fSViresh Kumar unsigned long clock_latency_ns; 2637813dd6fSViresh Kumar 2647813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2657813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2667813dd6fSViresh Kumar return 0; 2677813dd6fSViresh Kumar 2687813dd6fSViresh Kumar clock_latency_ns = opp_table->clock_latency_ns_max; 2697813dd6fSViresh Kumar 2707813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2717813dd6fSViresh Kumar 2727813dd6fSViresh Kumar return clock_latency_ns; 2737813dd6fSViresh Kumar } 2747813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 2757813dd6fSViresh Kumar 2767813dd6fSViresh Kumar /** 2777813dd6fSViresh Kumar * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 2787813dd6fSViresh Kumar * @dev: device for which we do this operation 2797813dd6fSViresh Kumar * 2807813dd6fSViresh Kumar * Return: This function returns the max voltage latency in nanoseconds. 2817813dd6fSViresh Kumar */ 2827813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 2837813dd6fSViresh Kumar { 2847813dd6fSViresh Kumar struct opp_table *opp_table; 2857813dd6fSViresh Kumar struct dev_pm_opp *opp; 2867813dd6fSViresh Kumar struct regulator *reg; 2877813dd6fSViresh Kumar unsigned long latency_ns = 0; 2887813dd6fSViresh Kumar int ret, i, count; 2897813dd6fSViresh Kumar struct { 2907813dd6fSViresh Kumar unsigned long min; 2917813dd6fSViresh Kumar unsigned long max; 2927813dd6fSViresh Kumar } *uV; 2937813dd6fSViresh Kumar 2947813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2957813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2967813dd6fSViresh Kumar return 0; 2977813dd6fSViresh Kumar 2987813dd6fSViresh Kumar /* Regulator may not be required for the device */ 29990e3577bSViresh Kumar if (!opp_table->regulators) 3007813dd6fSViresh Kumar goto put_opp_table; 3017813dd6fSViresh Kumar 30290e3577bSViresh Kumar count = opp_table->regulator_count; 30390e3577bSViresh Kumar 3047813dd6fSViresh Kumar uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 3057813dd6fSViresh Kumar if (!uV) 3067813dd6fSViresh Kumar goto put_opp_table; 3077813dd6fSViresh Kumar 3087813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 3097813dd6fSViresh Kumar 3107813dd6fSViresh Kumar for (i = 0; i < count; i++) { 3117813dd6fSViresh Kumar uV[i].min = ~0; 3127813dd6fSViresh Kumar uV[i].max = 0; 3137813dd6fSViresh Kumar 3147813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 3157813dd6fSViresh Kumar if (!opp->available) 3167813dd6fSViresh Kumar continue; 3177813dd6fSViresh Kumar 3187813dd6fSViresh Kumar if (opp->supplies[i].u_volt_min < uV[i].min) 3197813dd6fSViresh Kumar uV[i].min = opp->supplies[i].u_volt_min; 3207813dd6fSViresh Kumar if (opp->supplies[i].u_volt_max > uV[i].max) 3217813dd6fSViresh Kumar uV[i].max = opp->supplies[i].u_volt_max; 3227813dd6fSViresh Kumar } 3237813dd6fSViresh Kumar } 3247813dd6fSViresh Kumar 3257813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 3267813dd6fSViresh Kumar 3277813dd6fSViresh Kumar /* 3287813dd6fSViresh Kumar * The caller needs to ensure that opp_table (and hence the regulator) 3297813dd6fSViresh Kumar * isn't freed, while we are executing this routine. 3307813dd6fSViresh Kumar */ 3317813dd6fSViresh Kumar for (i = 0; i < count; i++) { 3327813dd6fSViresh Kumar reg = opp_table->regulators[i]; 3337813dd6fSViresh Kumar ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); 3347813dd6fSViresh Kumar if (ret > 0) 3357813dd6fSViresh Kumar latency_ns += ret * 1000; 3367813dd6fSViresh Kumar } 3377813dd6fSViresh Kumar 3387813dd6fSViresh Kumar kfree(uV); 3397813dd6fSViresh Kumar put_opp_table: 3407813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3417813dd6fSViresh Kumar 3427813dd6fSViresh Kumar return latency_ns; 3437813dd6fSViresh Kumar } 3447813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); 3457813dd6fSViresh Kumar 3467813dd6fSViresh Kumar /** 3477813dd6fSViresh Kumar * dev_pm_opp_get_max_transition_latency() - Get max transition latency in 3487813dd6fSViresh Kumar * nanoseconds 3497813dd6fSViresh Kumar * @dev: device for which we do this operation 3507813dd6fSViresh Kumar * 3517813dd6fSViresh Kumar * Return: This function returns the max transition latency, in nanoseconds, to 3527813dd6fSViresh Kumar * switch from one OPP to other. 3537813dd6fSViresh Kumar */ 3547813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 3557813dd6fSViresh Kumar { 3567813dd6fSViresh Kumar return dev_pm_opp_get_max_volt_latency(dev) + 3577813dd6fSViresh Kumar dev_pm_opp_get_max_clock_latency(dev); 3587813dd6fSViresh Kumar } 3597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 3607813dd6fSViresh Kumar 3617813dd6fSViresh Kumar /** 3627813dd6fSViresh Kumar * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 3637813dd6fSViresh Kumar * @dev: device for which we do this operation 3647813dd6fSViresh Kumar * 3657813dd6fSViresh Kumar * Return: This function returns the frequency of the OPP marked as suspend_opp 3667813dd6fSViresh Kumar * if one is available, else returns 0; 3677813dd6fSViresh Kumar */ 3687813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 3697813dd6fSViresh Kumar { 3707813dd6fSViresh Kumar struct opp_table *opp_table; 3717813dd6fSViresh Kumar unsigned long freq = 0; 3727813dd6fSViresh Kumar 3737813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3747813dd6fSViresh Kumar if (IS_ERR(opp_table)) 3757813dd6fSViresh Kumar return 0; 3767813dd6fSViresh Kumar 3777813dd6fSViresh Kumar if (opp_table->suspend_opp && opp_table->suspend_opp->available) 3787813dd6fSViresh Kumar freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 3797813dd6fSViresh Kumar 3807813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3817813dd6fSViresh Kumar 3827813dd6fSViresh Kumar return freq; 3837813dd6fSViresh Kumar } 3847813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 3857813dd6fSViresh Kumar 386a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table) 387a1e8c136SViresh Kumar { 388a1e8c136SViresh Kumar struct dev_pm_opp *opp; 389a1e8c136SViresh Kumar int count = 0; 390a1e8c136SViresh Kumar 391a1e8c136SViresh Kumar mutex_lock(&opp_table->lock); 392a1e8c136SViresh Kumar 393a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 394a1e8c136SViresh Kumar if (opp->available) 395a1e8c136SViresh Kumar count++; 396a1e8c136SViresh Kumar } 397a1e8c136SViresh Kumar 398a1e8c136SViresh Kumar mutex_unlock(&opp_table->lock); 399a1e8c136SViresh Kumar 400a1e8c136SViresh Kumar return count; 401a1e8c136SViresh Kumar } 402a1e8c136SViresh Kumar 4037813dd6fSViresh Kumar /** 4047813dd6fSViresh Kumar * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table 4057813dd6fSViresh Kumar * @dev: device for which we do this operation 4067813dd6fSViresh Kumar * 4077813dd6fSViresh Kumar * Return: This function returns the number of available opps if there are any, 4087813dd6fSViresh Kumar * else returns 0 if none or the corresponding error value. 4097813dd6fSViresh Kumar */ 4107813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev) 4117813dd6fSViresh Kumar { 4127813dd6fSViresh Kumar struct opp_table *opp_table; 413a1e8c136SViresh Kumar int count; 4147813dd6fSViresh Kumar 4157813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 4167813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 4177813dd6fSViresh Kumar count = PTR_ERR(opp_table); 418035ed072SFabio Estevam dev_dbg(dev, "%s: OPP table not found (%d)\n", 4197813dd6fSViresh Kumar __func__, count); 42009f662f9SViresh Kumar return count; 4217813dd6fSViresh Kumar } 4227813dd6fSViresh Kumar 423a1e8c136SViresh Kumar count = _get_opp_count(opp_table); 4247813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4257813dd6fSViresh Kumar 4267813dd6fSViresh Kumar return count; 4277813dd6fSViresh Kumar } 4287813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); 4297813dd6fSViresh Kumar 4307813dd6fSViresh Kumar /** 4317813dd6fSViresh Kumar * dev_pm_opp_find_freq_exact() - search for an exact frequency 4327813dd6fSViresh Kumar * @dev: device for which we do this operation 4337813dd6fSViresh Kumar * @freq: frequency to search for 4347813dd6fSViresh Kumar * @available: true/false - match for available opp 4357813dd6fSViresh Kumar * 4367813dd6fSViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 4377813dd6fSViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 4387813dd6fSViresh Kumar * be handled using IS_ERR. Error return values can be: 4397813dd6fSViresh Kumar * EINVAL: for bad pointer 4407813dd6fSViresh Kumar * ERANGE: no match found for search 4417813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 4427813dd6fSViresh Kumar * 4437813dd6fSViresh Kumar * Note: available is a modifier for the search. if available=true, then the 4447813dd6fSViresh Kumar * match is for exact matching frequency and is available in the stored OPP 4457813dd6fSViresh Kumar * table. if false, the match is for exact frequency which is not available. 4467813dd6fSViresh Kumar * 4477813dd6fSViresh Kumar * This provides a mechanism to enable an opp which is not available currently 4487813dd6fSViresh Kumar * or the opposite as well. 4497813dd6fSViresh Kumar * 4507813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 4517813dd6fSViresh Kumar * use. 4527813dd6fSViresh Kumar */ 4537813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 4547813dd6fSViresh Kumar unsigned long freq, 4557813dd6fSViresh Kumar bool available) 4567813dd6fSViresh Kumar { 4577813dd6fSViresh Kumar struct opp_table *opp_table; 4587813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 4597813dd6fSViresh Kumar 4607813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 4617813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 4627813dd6fSViresh Kumar int r = PTR_ERR(opp_table); 4637813dd6fSViresh Kumar 4647813dd6fSViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 4657813dd6fSViresh Kumar return ERR_PTR(r); 4667813dd6fSViresh Kumar } 4677813dd6fSViresh Kumar 4687813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 4697813dd6fSViresh Kumar 4707813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 4717813dd6fSViresh Kumar if (temp_opp->available == available && 4727813dd6fSViresh Kumar temp_opp->rate == freq) { 4737813dd6fSViresh Kumar opp = temp_opp; 4747813dd6fSViresh Kumar 4757813dd6fSViresh Kumar /* Increment the reference count of OPP */ 4767813dd6fSViresh Kumar dev_pm_opp_get(opp); 4777813dd6fSViresh Kumar break; 4787813dd6fSViresh Kumar } 4797813dd6fSViresh Kumar } 4807813dd6fSViresh Kumar 4817813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 4827813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4837813dd6fSViresh Kumar 4847813dd6fSViresh Kumar return opp; 4857813dd6fSViresh Kumar } 4867813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 4877813dd6fSViresh Kumar 4887813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 4897813dd6fSViresh Kumar unsigned long *freq) 4907813dd6fSViresh Kumar { 4917813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 4927813dd6fSViresh Kumar 4937813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 4947813dd6fSViresh Kumar 4957813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 4967813dd6fSViresh Kumar if (temp_opp->available && temp_opp->rate >= *freq) { 4977813dd6fSViresh Kumar opp = temp_opp; 4987813dd6fSViresh Kumar *freq = opp->rate; 4997813dd6fSViresh Kumar 5007813dd6fSViresh Kumar /* Increment the reference count of OPP */ 5017813dd6fSViresh Kumar dev_pm_opp_get(opp); 5027813dd6fSViresh Kumar break; 5037813dd6fSViresh Kumar } 5047813dd6fSViresh Kumar } 5057813dd6fSViresh Kumar 5067813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 5077813dd6fSViresh Kumar 5087813dd6fSViresh Kumar return opp; 5097813dd6fSViresh Kumar } 5107813dd6fSViresh Kumar 5117813dd6fSViresh Kumar /** 5127813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 5137813dd6fSViresh Kumar * @dev: device for which we do this operation 5147813dd6fSViresh Kumar * @freq: Start frequency 5157813dd6fSViresh Kumar * 5167813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 5177813dd6fSViresh Kumar * for a device. 5187813dd6fSViresh Kumar * 5197813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 5207813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 5217813dd6fSViresh Kumar * values can be: 5227813dd6fSViresh Kumar * EINVAL: for bad pointer 5237813dd6fSViresh Kumar * ERANGE: no match found for search 5247813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 5257813dd6fSViresh Kumar * 5267813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 5277813dd6fSViresh Kumar * use. 5287813dd6fSViresh Kumar */ 5297813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 5307813dd6fSViresh Kumar unsigned long *freq) 5317813dd6fSViresh Kumar { 5327813dd6fSViresh Kumar struct opp_table *opp_table; 5337813dd6fSViresh Kumar struct dev_pm_opp *opp; 5347813dd6fSViresh Kumar 5357813dd6fSViresh Kumar if (!dev || !freq) { 5367813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 5377813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 5387813dd6fSViresh Kumar } 5397813dd6fSViresh Kumar 5407813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 5417813dd6fSViresh Kumar if (IS_ERR(opp_table)) 5427813dd6fSViresh Kumar return ERR_CAST(opp_table); 5437813dd6fSViresh Kumar 5447813dd6fSViresh Kumar opp = _find_freq_ceil(opp_table, freq); 5457813dd6fSViresh Kumar 5467813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 5477813dd6fSViresh Kumar 5487813dd6fSViresh Kumar return opp; 5497813dd6fSViresh Kumar } 5507813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 5517813dd6fSViresh Kumar 5527813dd6fSViresh Kumar /** 5537813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 5547813dd6fSViresh Kumar * @dev: device for which we do this operation 5557813dd6fSViresh Kumar * @freq: Start frequency 5567813dd6fSViresh Kumar * 5577813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 5587813dd6fSViresh Kumar * for a device. 5597813dd6fSViresh Kumar * 5607813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 5617813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 5627813dd6fSViresh Kumar * values can be: 5637813dd6fSViresh Kumar * EINVAL: for bad pointer 5647813dd6fSViresh Kumar * ERANGE: no match found for search 5657813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 5667813dd6fSViresh Kumar * 5677813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 5687813dd6fSViresh Kumar * use. 5697813dd6fSViresh Kumar */ 5707813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 5717813dd6fSViresh Kumar unsigned long *freq) 5727813dd6fSViresh Kumar { 5737813dd6fSViresh Kumar struct opp_table *opp_table; 5747813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 5757813dd6fSViresh Kumar 5767813dd6fSViresh Kumar if (!dev || !freq) { 5777813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 5787813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 5797813dd6fSViresh Kumar } 5807813dd6fSViresh Kumar 5817813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 5827813dd6fSViresh Kumar if (IS_ERR(opp_table)) 5837813dd6fSViresh Kumar return ERR_CAST(opp_table); 5847813dd6fSViresh Kumar 5857813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 5867813dd6fSViresh Kumar 5877813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 5887813dd6fSViresh Kumar if (temp_opp->available) { 5897813dd6fSViresh Kumar /* go to the next node, before choosing prev */ 5907813dd6fSViresh Kumar if (temp_opp->rate > *freq) 5917813dd6fSViresh Kumar break; 5927813dd6fSViresh Kumar else 5937813dd6fSViresh Kumar opp = temp_opp; 5947813dd6fSViresh Kumar } 5957813dd6fSViresh Kumar } 5967813dd6fSViresh Kumar 5977813dd6fSViresh Kumar /* Increment the reference count of OPP */ 5987813dd6fSViresh Kumar if (!IS_ERR(opp)) 5997813dd6fSViresh Kumar dev_pm_opp_get(opp); 6007813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 6017813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 6027813dd6fSViresh Kumar 6037813dd6fSViresh Kumar if (!IS_ERR(opp)) 6047813dd6fSViresh Kumar *freq = opp->rate; 6057813dd6fSViresh Kumar 6067813dd6fSViresh Kumar return opp; 6077813dd6fSViresh Kumar } 6087813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 6097813dd6fSViresh Kumar 6102f36bde0SAndrew-sh.Cheng /** 6112f36bde0SAndrew-sh.Cheng * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for 6122f36bde0SAndrew-sh.Cheng * target voltage. 6132f36bde0SAndrew-sh.Cheng * @dev: Device for which we do this operation. 6142f36bde0SAndrew-sh.Cheng * @u_volt: Target voltage. 6152f36bde0SAndrew-sh.Cheng * 6162f36bde0SAndrew-sh.Cheng * Search for OPP with highest (ceil) frequency and has voltage <= u_volt. 6172f36bde0SAndrew-sh.Cheng * 6182f36bde0SAndrew-sh.Cheng * Return: matching *opp, else returns ERR_PTR in case of error which should be 6192f36bde0SAndrew-sh.Cheng * handled using IS_ERR. 6202f36bde0SAndrew-sh.Cheng * 6212f36bde0SAndrew-sh.Cheng * Error return values can be: 6222f36bde0SAndrew-sh.Cheng * EINVAL: bad parameters 6232f36bde0SAndrew-sh.Cheng * 6242f36bde0SAndrew-sh.Cheng * The callers are required to call dev_pm_opp_put() for the returned OPP after 6252f36bde0SAndrew-sh.Cheng * use. 6262f36bde0SAndrew-sh.Cheng */ 6272f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev, 6282f36bde0SAndrew-sh.Cheng unsigned long u_volt) 6292f36bde0SAndrew-sh.Cheng { 6302f36bde0SAndrew-sh.Cheng struct opp_table *opp_table; 6312f36bde0SAndrew-sh.Cheng struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 6322f36bde0SAndrew-sh.Cheng 6332f36bde0SAndrew-sh.Cheng if (!dev || !u_volt) { 6342f36bde0SAndrew-sh.Cheng dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__, 6352f36bde0SAndrew-sh.Cheng u_volt); 6362f36bde0SAndrew-sh.Cheng return ERR_PTR(-EINVAL); 6372f36bde0SAndrew-sh.Cheng } 6382f36bde0SAndrew-sh.Cheng 6392f36bde0SAndrew-sh.Cheng opp_table = _find_opp_table(dev); 6402f36bde0SAndrew-sh.Cheng if (IS_ERR(opp_table)) 6412f36bde0SAndrew-sh.Cheng return ERR_CAST(opp_table); 6422f36bde0SAndrew-sh.Cheng 6432f36bde0SAndrew-sh.Cheng mutex_lock(&opp_table->lock); 6442f36bde0SAndrew-sh.Cheng 6452f36bde0SAndrew-sh.Cheng list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 6462f36bde0SAndrew-sh.Cheng if (temp_opp->available) { 6472f36bde0SAndrew-sh.Cheng if (temp_opp->supplies[0].u_volt > u_volt) 6482f36bde0SAndrew-sh.Cheng break; 6492f36bde0SAndrew-sh.Cheng opp = temp_opp; 6502f36bde0SAndrew-sh.Cheng } 6512f36bde0SAndrew-sh.Cheng } 6522f36bde0SAndrew-sh.Cheng 6532f36bde0SAndrew-sh.Cheng /* Increment the reference count of OPP */ 6542f36bde0SAndrew-sh.Cheng if (!IS_ERR(opp)) 6552f36bde0SAndrew-sh.Cheng dev_pm_opp_get(opp); 6562f36bde0SAndrew-sh.Cheng 6572f36bde0SAndrew-sh.Cheng mutex_unlock(&opp_table->lock); 6582f36bde0SAndrew-sh.Cheng dev_pm_opp_put_opp_table(opp_table); 6592f36bde0SAndrew-sh.Cheng 6602f36bde0SAndrew-sh.Cheng return opp; 6612f36bde0SAndrew-sh.Cheng } 6622f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt); 6632f36bde0SAndrew-sh.Cheng 66400ce3873SKrzysztof Kozlowski /** 66522079af7SViresh Kumar * dev_pm_opp_find_level_exact() - search for an exact level 66622079af7SViresh Kumar * @dev: device for which we do this operation 66722079af7SViresh Kumar * @level: level to search for 66822079af7SViresh Kumar * 66922079af7SViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 67022079af7SViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 67122079af7SViresh Kumar * be handled using IS_ERR. Error return values can be: 67222079af7SViresh Kumar * EINVAL: for bad pointer 67322079af7SViresh Kumar * ERANGE: no match found for search 67422079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 67522079af7SViresh Kumar * 67622079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 67722079af7SViresh Kumar * use. 67822079af7SViresh Kumar */ 67922079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev, 68022079af7SViresh Kumar unsigned int level) 68122079af7SViresh Kumar { 68222079af7SViresh Kumar struct opp_table *opp_table; 68322079af7SViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 68422079af7SViresh Kumar 68522079af7SViresh Kumar opp_table = _find_opp_table(dev); 68622079af7SViresh Kumar if (IS_ERR(opp_table)) { 68722079af7SViresh Kumar int r = PTR_ERR(opp_table); 68822079af7SViresh Kumar 68922079af7SViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 69022079af7SViresh Kumar return ERR_PTR(r); 69122079af7SViresh Kumar } 69222079af7SViresh Kumar 69322079af7SViresh Kumar mutex_lock(&opp_table->lock); 69422079af7SViresh Kumar 69522079af7SViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 69622079af7SViresh Kumar if (temp_opp->level == level) { 69722079af7SViresh Kumar opp = temp_opp; 69822079af7SViresh Kumar 69922079af7SViresh Kumar /* Increment the reference count of OPP */ 70022079af7SViresh Kumar dev_pm_opp_get(opp); 70122079af7SViresh Kumar break; 70222079af7SViresh Kumar } 70322079af7SViresh Kumar } 70422079af7SViresh Kumar 70522079af7SViresh Kumar mutex_unlock(&opp_table->lock); 70622079af7SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 70722079af7SViresh Kumar 70822079af7SViresh Kumar return opp; 70922079af7SViresh Kumar } 71022079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact); 71122079af7SViresh Kumar 71222079af7SViresh Kumar /** 71322079af7SViresh Kumar * dev_pm_opp_find_level_ceil() - search for an rounded up level 71422079af7SViresh Kumar * @dev: device for which we do this operation 71522079af7SViresh Kumar * @level: level to search for 71622079af7SViresh Kumar * 71722079af7SViresh Kumar * Return: Searches for rounded up match in the opp table and returns pointer 71822079af7SViresh Kumar * to the matching opp if found, else returns ERR_PTR in case of error and 71922079af7SViresh Kumar * should be handled using IS_ERR. Error return values can be: 72022079af7SViresh Kumar * EINVAL: for bad pointer 72122079af7SViresh Kumar * ERANGE: no match found for search 72222079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 72322079af7SViresh Kumar * 72422079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 72522079af7SViresh Kumar * use. 72622079af7SViresh Kumar */ 72722079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev, 72822079af7SViresh Kumar unsigned int *level) 72922079af7SViresh Kumar { 73022079af7SViresh Kumar struct opp_table *opp_table; 73122079af7SViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 73222079af7SViresh Kumar 73322079af7SViresh Kumar opp_table = _find_opp_table(dev); 73422079af7SViresh Kumar if (IS_ERR(opp_table)) { 73522079af7SViresh Kumar int r = PTR_ERR(opp_table); 73622079af7SViresh Kumar 73722079af7SViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 73822079af7SViresh Kumar return ERR_PTR(r); 73922079af7SViresh Kumar } 74022079af7SViresh Kumar 74122079af7SViresh Kumar mutex_lock(&opp_table->lock); 74222079af7SViresh Kumar 74322079af7SViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 74422079af7SViresh Kumar if (temp_opp->available && temp_opp->level >= *level) { 74522079af7SViresh Kumar opp = temp_opp; 74622079af7SViresh Kumar *level = opp->level; 74722079af7SViresh Kumar 74822079af7SViresh Kumar /* Increment the reference count of OPP */ 74922079af7SViresh Kumar dev_pm_opp_get(opp); 75022079af7SViresh Kumar break; 75122079af7SViresh Kumar } 75222079af7SViresh Kumar } 75322079af7SViresh Kumar 75422079af7SViresh Kumar mutex_unlock(&opp_table->lock); 75522079af7SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 75622079af7SViresh Kumar 75722079af7SViresh Kumar return opp; 75822079af7SViresh Kumar } 75922079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil); 76022079af7SViresh Kumar 76122079af7SViresh Kumar /** 76200ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth 76300ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 764617df304SYang Li * @bw: start bandwidth 76500ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 76600ce3873SKrzysztof Kozlowski * 76700ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 76800ce3873SKrzysztof Kozlowski * for a device. 76900ce3873SKrzysztof Kozlowski * 77000ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 77100ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 77200ce3873SKrzysztof Kozlowski * values can be: 77300ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 77400ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 77500ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 77600ce3873SKrzysztof Kozlowski * 77700ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 77800ce3873SKrzysztof Kozlowski * use. 77900ce3873SKrzysztof Kozlowski */ 78000ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, 78100ce3873SKrzysztof Kozlowski unsigned int *bw, int index) 78200ce3873SKrzysztof Kozlowski { 78300ce3873SKrzysztof Kozlowski struct opp_table *opp_table; 78400ce3873SKrzysztof Kozlowski struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 78500ce3873SKrzysztof Kozlowski 78600ce3873SKrzysztof Kozlowski if (!dev || !bw) { 78700ce3873SKrzysztof Kozlowski dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw); 78800ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 78900ce3873SKrzysztof Kozlowski } 79000ce3873SKrzysztof Kozlowski 79100ce3873SKrzysztof Kozlowski opp_table = _find_opp_table(dev); 79200ce3873SKrzysztof Kozlowski if (IS_ERR(opp_table)) 79300ce3873SKrzysztof Kozlowski return ERR_CAST(opp_table); 79400ce3873SKrzysztof Kozlowski 79500ce3873SKrzysztof Kozlowski if (index >= opp_table->path_count) 79600ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 79700ce3873SKrzysztof Kozlowski 79800ce3873SKrzysztof Kozlowski mutex_lock(&opp_table->lock); 79900ce3873SKrzysztof Kozlowski 80000ce3873SKrzysztof Kozlowski list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 80100ce3873SKrzysztof Kozlowski if (temp_opp->available && temp_opp->bandwidth) { 80200ce3873SKrzysztof Kozlowski if (temp_opp->bandwidth[index].peak >= *bw) { 80300ce3873SKrzysztof Kozlowski opp = temp_opp; 80400ce3873SKrzysztof Kozlowski *bw = opp->bandwidth[index].peak; 80500ce3873SKrzysztof Kozlowski 80600ce3873SKrzysztof Kozlowski /* Increment the reference count of OPP */ 80700ce3873SKrzysztof Kozlowski dev_pm_opp_get(opp); 80800ce3873SKrzysztof Kozlowski break; 80900ce3873SKrzysztof Kozlowski } 81000ce3873SKrzysztof Kozlowski } 81100ce3873SKrzysztof Kozlowski } 81200ce3873SKrzysztof Kozlowski 81300ce3873SKrzysztof Kozlowski mutex_unlock(&opp_table->lock); 81400ce3873SKrzysztof Kozlowski dev_pm_opp_put_opp_table(opp_table); 81500ce3873SKrzysztof Kozlowski 81600ce3873SKrzysztof Kozlowski return opp; 81700ce3873SKrzysztof Kozlowski } 81800ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil); 81900ce3873SKrzysztof Kozlowski 82000ce3873SKrzysztof Kozlowski /** 82100ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth 82200ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 823617df304SYang Li * @bw: start bandwidth 82400ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 82500ce3873SKrzysztof Kozlowski * 82600ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 82700ce3873SKrzysztof Kozlowski * for a device. 82800ce3873SKrzysztof Kozlowski * 82900ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 83000ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 83100ce3873SKrzysztof Kozlowski * values can be: 83200ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 83300ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 83400ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 83500ce3873SKrzysztof Kozlowski * 83600ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 83700ce3873SKrzysztof Kozlowski * use. 83800ce3873SKrzysztof Kozlowski */ 83900ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev, 84000ce3873SKrzysztof Kozlowski unsigned int *bw, int index) 84100ce3873SKrzysztof Kozlowski { 84200ce3873SKrzysztof Kozlowski struct opp_table *opp_table; 84300ce3873SKrzysztof Kozlowski struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 84400ce3873SKrzysztof Kozlowski 84500ce3873SKrzysztof Kozlowski if (!dev || !bw) { 84600ce3873SKrzysztof Kozlowski dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw); 84700ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 84800ce3873SKrzysztof Kozlowski } 84900ce3873SKrzysztof Kozlowski 85000ce3873SKrzysztof Kozlowski opp_table = _find_opp_table(dev); 85100ce3873SKrzysztof Kozlowski if (IS_ERR(opp_table)) 85200ce3873SKrzysztof Kozlowski return ERR_CAST(opp_table); 85300ce3873SKrzysztof Kozlowski 85400ce3873SKrzysztof Kozlowski if (index >= opp_table->path_count) 85500ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 85600ce3873SKrzysztof Kozlowski 85700ce3873SKrzysztof Kozlowski mutex_lock(&opp_table->lock); 85800ce3873SKrzysztof Kozlowski 85900ce3873SKrzysztof Kozlowski list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 86000ce3873SKrzysztof Kozlowski if (temp_opp->available && temp_opp->bandwidth) { 86100ce3873SKrzysztof Kozlowski /* go to the next node, before choosing prev */ 86200ce3873SKrzysztof Kozlowski if (temp_opp->bandwidth[index].peak > *bw) 86300ce3873SKrzysztof Kozlowski break; 86400ce3873SKrzysztof Kozlowski opp = temp_opp; 86500ce3873SKrzysztof Kozlowski } 86600ce3873SKrzysztof Kozlowski } 86700ce3873SKrzysztof Kozlowski 86800ce3873SKrzysztof Kozlowski /* Increment the reference count of OPP */ 86900ce3873SKrzysztof Kozlowski if (!IS_ERR(opp)) 87000ce3873SKrzysztof Kozlowski dev_pm_opp_get(opp); 87100ce3873SKrzysztof Kozlowski mutex_unlock(&opp_table->lock); 87200ce3873SKrzysztof Kozlowski dev_pm_opp_put_opp_table(opp_table); 87300ce3873SKrzysztof Kozlowski 87400ce3873SKrzysztof Kozlowski if (!IS_ERR(opp)) 87500ce3873SKrzysztof Kozlowski *bw = opp->bandwidth[index].peak; 87600ce3873SKrzysztof Kozlowski 87700ce3873SKrzysztof Kozlowski return opp; 87800ce3873SKrzysztof Kozlowski } 87900ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor); 88000ce3873SKrzysztof Kozlowski 8817813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 8827813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 8837813dd6fSViresh Kumar { 8847813dd6fSViresh Kumar int ret; 8857813dd6fSViresh Kumar 8867813dd6fSViresh Kumar /* Regulator not available for device */ 8877813dd6fSViresh Kumar if (IS_ERR(reg)) { 8887813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 8897813dd6fSViresh Kumar PTR_ERR(reg)); 8907813dd6fSViresh Kumar return 0; 8917813dd6fSViresh Kumar } 8927813dd6fSViresh Kumar 8937813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 8947813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 8957813dd6fSViresh Kumar 8967813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 8977813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 8987813dd6fSViresh Kumar if (ret) 8997813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 9007813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 9017813dd6fSViresh Kumar supply->u_volt_max, ret); 9027813dd6fSViresh Kumar 9037813dd6fSViresh Kumar return ret; 9047813dd6fSViresh Kumar } 9057813dd6fSViresh Kumar 906285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 907285881b5SViresh Kumar unsigned long freq) 9087813dd6fSViresh Kumar { 9097813dd6fSViresh Kumar int ret; 9107813dd6fSViresh Kumar 91135e74b2eSViresh Kumar /* We may reach here for devices which don't change frequency */ 91235e74b2eSViresh Kumar if (IS_ERR(clk)) 91335e74b2eSViresh Kumar return 0; 91435e74b2eSViresh Kumar 9157813dd6fSViresh Kumar ret = clk_set_rate(clk, freq); 9167813dd6fSViresh Kumar if (ret) { 9177813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 9187813dd6fSViresh Kumar ret); 9197813dd6fSViresh Kumar } 9207813dd6fSViresh Kumar 9217813dd6fSViresh Kumar return ret; 9227813dd6fSViresh Kumar } 9237813dd6fSViresh Kumar 924c522ce8aSViresh Kumar static int _opp_config_regulator_single(struct device *dev, 925c522ce8aSViresh Kumar struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp, 926c522ce8aSViresh Kumar struct regulator **regulators, unsigned int count) 9277813dd6fSViresh Kumar { 928c522ce8aSViresh Kumar struct regulator *reg = regulators[0]; 9297813dd6fSViresh Kumar int ret; 9307813dd6fSViresh Kumar 9317813dd6fSViresh Kumar /* This function only supports single regulator per device */ 932c522ce8aSViresh Kumar if (WARN_ON(count > 1)) { 9337813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 9347813dd6fSViresh Kumar return -EINVAL; 9357813dd6fSViresh Kumar } 9367813dd6fSViresh Kumar 937c522ce8aSViresh Kumar ret = _set_opp_voltage(dev, reg, new_opp->supplies); 9387813dd6fSViresh Kumar if (ret) 939c522ce8aSViresh Kumar return ret; 9407813dd6fSViresh Kumar 9418d45719cSKamil Konieczny /* 9428d45719cSKamil Konieczny * Enable the regulator after setting its voltages, otherwise it breaks 9438d45719cSKamil Konieczny * some boot-enabled regulators. 9448d45719cSKamil Konieczny */ 945c522ce8aSViresh Kumar if (unlikely(!new_opp->opp_table->enabled)) { 9468d45719cSKamil Konieczny ret = regulator_enable(reg); 9478d45719cSKamil Konieczny if (ret < 0) 9488d45719cSKamil Konieczny dev_warn(dev, "Failed to enable regulator: %d", ret); 9498d45719cSKamil Konieczny } 9508d45719cSKamil Konieczny 9517813dd6fSViresh Kumar return 0; 9527813dd6fSViresh Kumar } 9537813dd6fSViresh Kumar 954b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table, 955240ae50eSViresh Kumar struct dev_pm_opp *opp, struct device *dev) 956b00e667aSViresh Kumar { 957b00e667aSViresh Kumar u32 avg, peak; 958b00e667aSViresh Kumar int i, ret; 959b00e667aSViresh Kumar 960b00e667aSViresh Kumar if (!opp_table->paths) 961b00e667aSViresh Kumar return 0; 962b00e667aSViresh Kumar 963b00e667aSViresh Kumar for (i = 0; i < opp_table->path_count; i++) { 964240ae50eSViresh Kumar if (!opp) { 965b00e667aSViresh Kumar avg = 0; 966b00e667aSViresh Kumar peak = 0; 967b00e667aSViresh Kumar } else { 968b00e667aSViresh Kumar avg = opp->bandwidth[i].avg; 969b00e667aSViresh Kumar peak = opp->bandwidth[i].peak; 970b00e667aSViresh Kumar } 971b00e667aSViresh Kumar ret = icc_set_bw(opp_table->paths[i], avg, peak); 972b00e667aSViresh Kumar if (ret) { 973b00e667aSViresh Kumar dev_err(dev, "Failed to %s bandwidth[%d]: %d\n", 974240ae50eSViresh Kumar opp ? "set" : "remove", i, ret); 975b00e667aSViresh Kumar return ret; 976b00e667aSViresh Kumar } 977b00e667aSViresh Kumar } 978b00e667aSViresh Kumar 979b00e667aSViresh Kumar return 0; 980b00e667aSViresh Kumar } 981b00e667aSViresh Kumar 98260cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev, 98360cdeae0SStephan Gerhold struct dev_pm_opp *opp, int i) 98460cdeae0SStephan Gerhold { 98560cdeae0SStephan Gerhold unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0; 98660cdeae0SStephan Gerhold int ret; 98760cdeae0SStephan Gerhold 98860cdeae0SStephan Gerhold if (!pd_dev) 98960cdeae0SStephan Gerhold return 0; 99060cdeae0SStephan Gerhold 99160cdeae0SStephan Gerhold ret = dev_pm_genpd_set_performance_state(pd_dev, pstate); 99260cdeae0SStephan Gerhold if (ret) { 9939bfb1fffSViresh Kumar dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", 99460cdeae0SStephan Gerhold dev_name(pd_dev), pstate, ret); 99560cdeae0SStephan Gerhold } 99660cdeae0SStephan Gerhold 99760cdeae0SStephan Gerhold return ret; 99860cdeae0SStephan Gerhold } 99960cdeae0SStephan Gerhold 1000ca1b5d77SViresh Kumar /* This is only called for PM domain for now */ 1001ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev, 1002ca1b5d77SViresh Kumar struct opp_table *opp_table, 10032c59138cSStephan Gerhold struct dev_pm_opp *opp, bool up) 1004ca1b5d77SViresh Kumar { 1005ca1b5d77SViresh Kumar struct opp_table **required_opp_tables = opp_table->required_opp_tables; 1006ca1b5d77SViresh Kumar struct device **genpd_virt_devs = opp_table->genpd_virt_devs; 1007ca1b5d77SViresh Kumar int i, ret = 0; 1008ca1b5d77SViresh Kumar 1009ca1b5d77SViresh Kumar if (!required_opp_tables) 1010ca1b5d77SViresh Kumar return 0; 1011ca1b5d77SViresh Kumar 101219526d09SMarijn Suijten /* required-opps not fully initialized yet */ 101319526d09SMarijn Suijten if (lazy_linking_pending(opp_table)) 101419526d09SMarijn Suijten return -EBUSY; 101519526d09SMarijn Suijten 10164fa82a87SHsin-Yi Wang /* 10174fa82a87SHsin-Yi Wang * We only support genpd's OPPs in the "required-opps" for now, as we 10184fa82a87SHsin-Yi Wang * don't know much about other use cases. Error out if the required OPP 10194fa82a87SHsin-Yi Wang * doesn't belong to a genpd. 10204fa82a87SHsin-Yi Wang */ 10214fa82a87SHsin-Yi Wang if (unlikely(!required_opp_tables[0]->is_genpd)) { 10224fa82a87SHsin-Yi Wang dev_err(dev, "required-opps don't belong to a genpd\n"); 10234fa82a87SHsin-Yi Wang return -ENOENT; 10244fa82a87SHsin-Yi Wang } 10254fa82a87SHsin-Yi Wang 1026ca1b5d77SViresh Kumar /* Single genpd case */ 102760cdeae0SStephan Gerhold if (!genpd_virt_devs) 102860cdeae0SStephan Gerhold return _set_required_opp(dev, dev, opp, 0); 1029ca1b5d77SViresh Kumar 1030ca1b5d77SViresh Kumar /* Multiple genpd case */ 1031ca1b5d77SViresh Kumar 1032ca1b5d77SViresh Kumar /* 1033ca1b5d77SViresh Kumar * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 1034ca1b5d77SViresh Kumar * after it is freed from another thread. 1035ca1b5d77SViresh Kumar */ 1036ca1b5d77SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 1037ca1b5d77SViresh Kumar 10382c59138cSStephan Gerhold /* Scaling up? Set required OPPs in normal order, else reverse */ 10392c59138cSStephan Gerhold if (up) { 1040ca1b5d77SViresh Kumar for (i = 0; i < opp_table->required_opp_count; i++) { 104160cdeae0SStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 104260cdeae0SStephan Gerhold if (ret) 1043ca1b5d77SViresh Kumar break; 1044ca1b5d77SViresh Kumar } 10452c59138cSStephan Gerhold } else { 10462c59138cSStephan Gerhold for (i = opp_table->required_opp_count - 1; i >= 0; i--) { 10472c59138cSStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 10482c59138cSStephan Gerhold if (ret) 1049ca1b5d77SViresh Kumar break; 1050ca1b5d77SViresh Kumar } 1051ca1b5d77SViresh Kumar } 10522c59138cSStephan Gerhold 1053ca1b5d77SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 1054ca1b5d77SViresh Kumar 1055ca1b5d77SViresh Kumar return ret; 1056ca1b5d77SViresh Kumar } 1057ca1b5d77SViresh Kumar 105881c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table) 105981c4d8a3SViresh Kumar { 106081c4d8a3SViresh Kumar struct dev_pm_opp *opp = ERR_PTR(-ENODEV); 106181c4d8a3SViresh Kumar unsigned long freq; 106281c4d8a3SViresh Kumar 106381c4d8a3SViresh Kumar if (!IS_ERR(opp_table->clk)) { 106481c4d8a3SViresh Kumar freq = clk_get_rate(opp_table->clk); 106581c4d8a3SViresh Kumar opp = _find_freq_ceil(opp_table, &freq); 106681c4d8a3SViresh Kumar } 106781c4d8a3SViresh Kumar 106881c4d8a3SViresh Kumar /* 106981c4d8a3SViresh Kumar * Unable to find the current OPP ? Pick the first from the list since 107081c4d8a3SViresh Kumar * it is in ascending order, otherwise rest of the code will need to 107181c4d8a3SViresh Kumar * make special checks to validate current_opp. 107281c4d8a3SViresh Kumar */ 107381c4d8a3SViresh Kumar if (IS_ERR(opp)) { 107481c4d8a3SViresh Kumar mutex_lock(&opp_table->lock); 107581c4d8a3SViresh Kumar opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node); 107681c4d8a3SViresh Kumar dev_pm_opp_get(opp); 107781c4d8a3SViresh Kumar mutex_unlock(&opp_table->lock); 107881c4d8a3SViresh Kumar } 107981c4d8a3SViresh Kumar 108081c4d8a3SViresh Kumar opp_table->current_opp = opp; 108181c4d8a3SViresh Kumar } 108281c4d8a3SViresh Kumar 10835ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table) 1084f3364e17SViresh Kumar { 1085f3364e17SViresh Kumar int ret; 1086f3364e17SViresh Kumar 1087f3364e17SViresh Kumar if (!opp_table->enabled) 1088f3364e17SViresh Kumar return 0; 1089f3364e17SViresh Kumar 1090f3364e17SViresh Kumar /* 1091f3364e17SViresh Kumar * Some drivers need to support cases where some platforms may 1092f3364e17SViresh Kumar * have OPP table for the device, while others don't and 1093f3364e17SViresh Kumar * opp_set_rate() just needs to behave like clk_set_rate(). 1094f3364e17SViresh Kumar */ 1095f3364e17SViresh Kumar if (!_get_opp_count(opp_table)) 1096f3364e17SViresh Kumar return 0; 1097f3364e17SViresh Kumar 1098240ae50eSViresh Kumar ret = _set_opp_bw(opp_table, NULL, dev); 1099f3364e17SViresh Kumar if (ret) 1100f3364e17SViresh Kumar return ret; 1101f3364e17SViresh Kumar 1102f3364e17SViresh Kumar if (opp_table->regulators) 1103f3364e17SViresh Kumar regulator_disable(opp_table->regulators[0]); 1104f3364e17SViresh Kumar 11052c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, NULL, false); 1106f3364e17SViresh Kumar 1107f3364e17SViresh Kumar opp_table->enabled = false; 1108f3364e17SViresh Kumar return ret; 1109f3364e17SViresh Kumar } 1110f3364e17SViresh Kumar 1111386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table, 1112386ba854SViresh Kumar struct dev_pm_opp *opp, unsigned long freq) 11137813dd6fSViresh Kumar { 1114386ba854SViresh Kumar struct dev_pm_opp *old_opp; 1115f0b88fa4SViresh Kumar int scaling_down, ret; 11167813dd6fSViresh Kumar 1117386ba854SViresh Kumar if (unlikely(!opp)) 1118386ba854SViresh Kumar return _disable_opp_table(dev, opp_table); 1119aca48b61SRajendra Nayak 112081c4d8a3SViresh Kumar /* Find the currently set OPP if we don't know already */ 112181c4d8a3SViresh Kumar if (unlikely(!opp_table->current_opp)) 112281c4d8a3SViresh Kumar _find_current_opp(dev, opp_table); 11237813dd6fSViresh Kumar 112481c4d8a3SViresh Kumar old_opp = opp_table->current_opp; 112581c4d8a3SViresh Kumar 112681c4d8a3SViresh Kumar /* Return early if nothing to do */ 1127de04241aSJonathan Marek if (old_opp == opp && opp_table->current_rate == freq && 1128de04241aSJonathan Marek opp_table->enabled) { 112981c4d8a3SViresh Kumar dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__); 1130386ba854SViresh Kumar return 0; 11317813dd6fSViresh Kumar } 11327813dd6fSViresh Kumar 1133f0b88fa4SViresh Kumar dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n", 1134de04241aSJonathan Marek __func__, opp_table->current_rate, freq, old_opp->level, 1135de04241aSJonathan Marek opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0, 1136f0b88fa4SViresh Kumar opp->bandwidth ? opp->bandwidth[0].peak : 0); 1137f0b88fa4SViresh Kumar 1138f0b88fa4SViresh Kumar scaling_down = _opp_compare_key(old_opp, opp); 1139f0b88fa4SViresh Kumar if (scaling_down == -1) 1140f0b88fa4SViresh Kumar scaling_down = 0; 11417813dd6fSViresh Kumar 1142ca1b5d77SViresh Kumar /* Scaling up? Configure required OPPs before frequency */ 1143f0b88fa4SViresh Kumar if (!scaling_down) { 11442c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, opp, true); 1145870d5d96SViresh Kumar if (ret) { 1146870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1147386ba854SViresh Kumar return ret; 1148ca1b5d77SViresh Kumar } 1149ca1b5d77SViresh Kumar 1150870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1151870d5d96SViresh Kumar if (ret) { 1152870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1153870d5d96SViresh Kumar return ret; 1154870d5d96SViresh Kumar } 1155aee3352fSViresh Kumar 1156aee3352fSViresh Kumar if (opp_table->config_regulators) { 1157aee3352fSViresh Kumar ret = opp_table->config_regulators(dev, old_opp, opp, 1158aee3352fSViresh Kumar opp_table->regulators, 1159aee3352fSViresh Kumar opp_table->regulator_count); 1160aee3352fSViresh Kumar if (ret) { 1161aee3352fSViresh Kumar dev_err(dev, "Failed to set regulator voltages: %d\n", 1162aee3352fSViresh Kumar ret); 1163aee3352fSViresh Kumar return ret; 1164aee3352fSViresh Kumar } 1165aee3352fSViresh Kumar } 1166870d5d96SViresh Kumar } 1167870d5d96SViresh Kumar 11681d3c42caSViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 1169ca1b5d77SViresh Kumar if (ret) 1170870d5d96SViresh Kumar return ret; 1171870d5d96SViresh Kumar 1172870d5d96SViresh Kumar /* Scaling down? Configure required OPPs after frequency */ 1173870d5d96SViresh Kumar if (scaling_down) { 1174aee3352fSViresh Kumar if (opp_table->config_regulators) { 1175aee3352fSViresh Kumar ret = opp_table->config_regulators(dev, old_opp, opp, 1176aee3352fSViresh Kumar opp_table->regulators, 1177aee3352fSViresh Kumar opp_table->regulator_count); 1178aee3352fSViresh Kumar if (ret) { 1179aee3352fSViresh Kumar dev_err(dev, "Failed to set regulator voltages: %d\n", 1180aee3352fSViresh Kumar ret); 1181aee3352fSViresh Kumar return ret; 1182aee3352fSViresh Kumar } 1183aee3352fSViresh Kumar } 1184aee3352fSViresh Kumar 1185870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1186870d5d96SViresh Kumar if (ret) { 1187870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1188870d5d96SViresh Kumar return ret; 1189ca1b5d77SViresh Kumar } 1190ca1b5d77SViresh Kumar 1191870d5d96SViresh Kumar ret = _set_required_opps(dev, opp_table, opp, false); 1192870d5d96SViresh Kumar if (ret) { 1193870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1194870d5d96SViresh Kumar return ret; 1195870d5d96SViresh Kumar } 1196870d5d96SViresh Kumar } 1197870d5d96SViresh Kumar 119872f80ce4SViresh Kumar opp_table->enabled = true; 119981c4d8a3SViresh Kumar dev_pm_opp_put(old_opp); 120081c4d8a3SViresh Kumar 120181c4d8a3SViresh Kumar /* Make sure current_opp doesn't get freed */ 120281c4d8a3SViresh Kumar dev_pm_opp_get(opp); 120381c4d8a3SViresh Kumar opp_table->current_opp = opp; 1204de04241aSJonathan Marek opp_table->current_rate = freq; 1205fe2af402SGeorgi Djakov 1206386ba854SViresh Kumar return ret; 1207386ba854SViresh Kumar } 1208386ba854SViresh Kumar 1209386ba854SViresh Kumar /** 1210386ba854SViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 1211386ba854SViresh Kumar * @dev: device for which we do this operation 1212386ba854SViresh Kumar * @target_freq: frequency to achieve 1213386ba854SViresh Kumar * 1214386ba854SViresh Kumar * This configures the power-supplies to the levels specified by the OPP 1215386ba854SViresh Kumar * corresponding to the target_freq, and programs the clock to a value <= 1216386ba854SViresh Kumar * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax 1217386ba854SViresh Kumar * provided by the opp, should have already rounded to the target OPP's 1218386ba854SViresh Kumar * frequency. 1219386ba854SViresh Kumar */ 1220386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 1221386ba854SViresh Kumar { 1222386ba854SViresh Kumar struct opp_table *opp_table; 1223386ba854SViresh Kumar unsigned long freq = 0, temp_freq; 1224386ba854SViresh Kumar struct dev_pm_opp *opp = NULL; 1225386ba854SViresh Kumar int ret; 1226386ba854SViresh Kumar 1227386ba854SViresh Kumar opp_table = _find_opp_table(dev); 1228386ba854SViresh Kumar if (IS_ERR(opp_table)) { 1229386ba854SViresh Kumar dev_err(dev, "%s: device's opp table doesn't exist\n", __func__); 1230386ba854SViresh Kumar return PTR_ERR(opp_table); 1231386ba854SViresh Kumar } 1232386ba854SViresh Kumar 1233386ba854SViresh Kumar if (target_freq) { 1234386ba854SViresh Kumar /* 1235386ba854SViresh Kumar * For IO devices which require an OPP on some platforms/SoCs 1236386ba854SViresh Kumar * while just needing to scale the clock on some others 1237386ba854SViresh Kumar * we look for empty OPP tables with just a clock handle and 1238386ba854SViresh Kumar * scale only the clk. This makes dev_pm_opp_set_rate() 1239386ba854SViresh Kumar * equivalent to a clk_set_rate() 1240386ba854SViresh Kumar */ 1241386ba854SViresh Kumar if (!_get_opp_count(opp_table)) { 1242386ba854SViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq); 1243386ba854SViresh Kumar goto put_opp_table; 1244386ba854SViresh Kumar } 1245386ba854SViresh Kumar 1246386ba854SViresh Kumar freq = clk_round_rate(opp_table->clk, target_freq); 1247386ba854SViresh Kumar if ((long)freq <= 0) 1248386ba854SViresh Kumar freq = target_freq; 1249386ba854SViresh Kumar 1250386ba854SViresh Kumar /* 1251386ba854SViresh Kumar * The clock driver may support finer resolution of the 1252386ba854SViresh Kumar * frequencies than the OPP table, don't update the frequency we 1253386ba854SViresh Kumar * pass to clk_set_rate() here. 1254386ba854SViresh Kumar */ 1255386ba854SViresh Kumar temp_freq = freq; 1256386ba854SViresh Kumar opp = _find_freq_ceil(opp_table, &temp_freq); 1257386ba854SViresh Kumar if (IS_ERR(opp)) { 1258386ba854SViresh Kumar ret = PTR_ERR(opp); 1259386ba854SViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 1260386ba854SViresh Kumar __func__, freq, ret); 1261386ba854SViresh Kumar goto put_opp_table; 1262386ba854SViresh Kumar } 1263386ba854SViresh Kumar } 1264386ba854SViresh Kumar 1265386ba854SViresh Kumar ret = _set_opp(dev, opp_table, opp, freq); 1266386ba854SViresh Kumar 1267386ba854SViresh Kumar if (target_freq) 12687813dd6fSViresh Kumar dev_pm_opp_put(opp); 12697813dd6fSViresh Kumar put_opp_table: 12707813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 12717813dd6fSViresh Kumar return ret; 12727813dd6fSViresh Kumar } 12737813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 12747813dd6fSViresh Kumar 1275abbe3483SViresh Kumar /** 1276abbe3483SViresh Kumar * dev_pm_opp_set_opp() - Configure device for OPP 1277abbe3483SViresh Kumar * @dev: device for which we do this operation 1278abbe3483SViresh Kumar * @opp: OPP to set to 1279abbe3483SViresh Kumar * 1280abbe3483SViresh Kumar * This configures the device based on the properties of the OPP passed to this 1281abbe3483SViresh Kumar * routine. 1282abbe3483SViresh Kumar * 1283abbe3483SViresh Kumar * Return: 0 on success, a negative error number otherwise. 1284abbe3483SViresh Kumar */ 1285abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp) 1286abbe3483SViresh Kumar { 1287abbe3483SViresh Kumar struct opp_table *opp_table; 1288abbe3483SViresh Kumar int ret; 1289abbe3483SViresh Kumar 1290abbe3483SViresh Kumar opp_table = _find_opp_table(dev); 1291abbe3483SViresh Kumar if (IS_ERR(opp_table)) { 1292abbe3483SViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 1293abbe3483SViresh Kumar return PTR_ERR(opp_table); 1294abbe3483SViresh Kumar } 1295abbe3483SViresh Kumar 1296abbe3483SViresh Kumar ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0); 1297abbe3483SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1298abbe3483SViresh Kumar 1299abbe3483SViresh Kumar return ret; 1300abbe3483SViresh Kumar } 1301abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp); 1302abbe3483SViresh Kumar 13037813dd6fSViresh Kumar /* OPP-dev Helpers */ 13047813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 13057813dd6fSViresh Kumar struct opp_table *opp_table) 13067813dd6fSViresh Kumar { 13077813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 13087813dd6fSViresh Kumar list_del(&opp_dev->node); 13097813dd6fSViresh Kumar kfree(opp_dev); 13107813dd6fSViresh Kumar } 13117813dd6fSViresh Kumar 1312ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 13137813dd6fSViresh Kumar struct opp_table *opp_table) 13147813dd6fSViresh Kumar { 13157813dd6fSViresh Kumar struct opp_device *opp_dev; 13167813dd6fSViresh Kumar 13177813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 13187813dd6fSViresh Kumar if (!opp_dev) 13197813dd6fSViresh Kumar return NULL; 13207813dd6fSViresh Kumar 13217813dd6fSViresh Kumar /* Initialize opp-dev */ 13227813dd6fSViresh Kumar opp_dev->dev = dev; 13233d255699SViresh Kumar 1324ef43f01aSViresh Kumar mutex_lock(&opp_table->lock); 13257813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 1326ef43f01aSViresh Kumar mutex_unlock(&opp_table->lock); 13277813dd6fSViresh Kumar 13287813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 1329a2dea4cbSGreg Kroah-Hartman opp_debug_register(opp_dev, opp_table); 1330283d55e6SViresh Kumar 1331283d55e6SViresh Kumar return opp_dev; 1332283d55e6SViresh Kumar } 1333283d55e6SViresh Kumar 1334eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index) 13357813dd6fSViresh Kumar { 13367813dd6fSViresh Kumar struct opp_table *opp_table; 13377813dd6fSViresh Kumar struct opp_device *opp_dev; 13387813dd6fSViresh Kumar int ret; 13397813dd6fSViresh Kumar 13407813dd6fSViresh Kumar /* 13417813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 13427813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 13437813dd6fSViresh Kumar */ 13447813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 13457813dd6fSViresh Kumar if (!opp_table) 1346dd461cd9SStephan Gerhold return ERR_PTR(-ENOMEM); 13477813dd6fSViresh Kumar 13483d255699SViresh Kumar mutex_init(&opp_table->lock); 13494f018bc0SViresh Kumar mutex_init(&opp_table->genpd_virt_dev_lock); 13507813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 13517eba0c76SViresh Kumar INIT_LIST_HEAD(&opp_table->lazy); 13527813dd6fSViresh Kumar 135346f48acaSViresh Kumar /* Mark regulator count uninitialized */ 135446f48acaSViresh Kumar opp_table->regulator_count = -1; 135546f48acaSViresh Kumar 13567813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 13577813dd6fSViresh Kumar if (!opp_dev) { 1358dd461cd9SStephan Gerhold ret = -ENOMEM; 1359dd461cd9SStephan Gerhold goto err; 13607813dd6fSViresh Kumar } 13617813dd6fSViresh Kumar 1362eb7c8743SViresh Kumar _of_init_opp_table(opp_table, dev, index); 13637813dd6fSViresh Kumar 13646d3f922cSGeorgi Djakov /* Find interconnect path(s) for the device */ 13656d3f922cSGeorgi Djakov ret = dev_pm_opp_of_find_icc_paths(dev, opp_table); 1366dd461cd9SStephan Gerhold if (ret) { 1367dd461cd9SStephan Gerhold if (ret == -EPROBE_DEFER) 136832439ac7SViresh Kumar goto remove_opp_dev; 1369dd461cd9SStephan Gerhold 13706d3f922cSGeorgi Djakov dev_warn(dev, "%s: Error finding interconnect paths: %d\n", 13716d3f922cSGeorgi Djakov __func__, ret); 1372dd461cd9SStephan Gerhold } 13736d3f922cSGeorgi Djakov 13747813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 13757813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 13767813dd6fSViresh Kumar kref_init(&opp_table->kref); 13777813dd6fSViresh Kumar 13787813dd6fSViresh Kumar return opp_table; 1379dd461cd9SStephan Gerhold 1380976509bbSQuanyang Wang remove_opp_dev: 1381976509bbSQuanyang Wang _remove_opp_dev(opp_dev, opp_table); 1382dd461cd9SStephan Gerhold err: 1383dd461cd9SStephan Gerhold kfree(opp_table); 1384dd461cd9SStephan Gerhold return ERR_PTR(ret); 13857813dd6fSViresh Kumar } 13867813dd6fSViresh Kumar 13877813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 13887813dd6fSViresh Kumar { 13897813dd6fSViresh Kumar kref_get(&opp_table->kref); 13907813dd6fSViresh Kumar } 13917813dd6fSViresh Kumar 139232439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev, 139332439ac7SViresh Kumar struct opp_table *opp_table, 139432439ac7SViresh Kumar bool getclk) 139532439ac7SViresh Kumar { 1396d4a4c7a4SViresh Kumar int ret; 1397d4a4c7a4SViresh Kumar 139832439ac7SViresh Kumar /* 139932439ac7SViresh Kumar * Return early if we don't need to get clk or we have already tried it 140032439ac7SViresh Kumar * earlier. 140132439ac7SViresh Kumar */ 140232439ac7SViresh Kumar if (!getclk || IS_ERR(opp_table) || opp_table->clk) 140332439ac7SViresh Kumar return opp_table; 140432439ac7SViresh Kumar 140532439ac7SViresh Kumar /* Find clk for the device */ 140632439ac7SViresh Kumar opp_table->clk = clk_get(dev, NULL); 140732439ac7SViresh Kumar 1408d4a4c7a4SViresh Kumar ret = PTR_ERR_OR_ZERO(opp_table->clk); 1409d4a4c7a4SViresh Kumar if (!ret) 141032439ac7SViresh Kumar return opp_table; 1411d4a4c7a4SViresh Kumar 1412d4a4c7a4SViresh Kumar if (ret == -ENOENT) { 1413d4a4c7a4SViresh Kumar dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret); 1414d4a4c7a4SViresh Kumar return opp_table; 1415d4a4c7a4SViresh Kumar } 1416d4a4c7a4SViresh Kumar 1417d4a4c7a4SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1418d4a4c7a4SViresh Kumar dev_err_probe(dev, ret, "Couldn't find clock\n"); 1419d4a4c7a4SViresh Kumar 1420d4a4c7a4SViresh Kumar return ERR_PTR(ret); 142132439ac7SViresh Kumar } 142232439ac7SViresh Kumar 142327c09484SViresh Kumar /* 142427c09484SViresh Kumar * We need to make sure that the OPP table for a device doesn't get added twice, 142527c09484SViresh Kumar * if this routine gets called in parallel with the same device pointer. 142627c09484SViresh Kumar * 142727c09484SViresh Kumar * The simplest way to enforce that is to perform everything (find existing 142827c09484SViresh Kumar * table and if not found, create a new one) under the opp_table_lock, so only 142927c09484SViresh Kumar * one creator gets access to the same. But that expands the critical section 143027c09484SViresh Kumar * under the lock and may end up causing circular dependencies with frameworks 143127c09484SViresh Kumar * like debugfs, interconnect or clock framework as they may be direct or 143227c09484SViresh Kumar * indirect users of OPP core. 143327c09484SViresh Kumar * 143427c09484SViresh Kumar * And for that reason we have to go for a bit tricky implementation here, which 143527c09484SViresh Kumar * uses the opp_tables_busy flag to indicate if another creator is in the middle 143627c09484SViresh Kumar * of adding an OPP table and others should wait for it to finish. 143727c09484SViresh Kumar */ 143832439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index, 143932439ac7SViresh Kumar bool getclk) 14407813dd6fSViresh Kumar { 14417813dd6fSViresh Kumar struct opp_table *opp_table; 14427813dd6fSViresh Kumar 144327c09484SViresh Kumar again: 14447813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 14457813dd6fSViresh Kumar 14467813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 14477813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 14487813dd6fSViresh Kumar goto unlock; 14497813dd6fSViresh Kumar 145027c09484SViresh Kumar /* 145127c09484SViresh Kumar * The opp_tables list or an OPP table's dev_list is getting updated by 145227c09484SViresh Kumar * another user, wait for it to finish. 145327c09484SViresh Kumar */ 145427c09484SViresh Kumar if (unlikely(opp_tables_busy)) { 145527c09484SViresh Kumar mutex_unlock(&opp_table_lock); 145627c09484SViresh Kumar cpu_relax(); 145727c09484SViresh Kumar goto again; 145827c09484SViresh Kumar } 145927c09484SViresh Kumar 146027c09484SViresh Kumar opp_tables_busy = true; 1461283d55e6SViresh Kumar opp_table = _managed_opp(dev, index); 146227c09484SViresh Kumar 146327c09484SViresh Kumar /* Drop the lock to reduce the size of critical section */ 146427c09484SViresh Kumar mutex_unlock(&opp_table_lock); 146527c09484SViresh Kumar 1466283d55e6SViresh Kumar if (opp_table) { 1467ef43f01aSViresh Kumar if (!_add_opp_dev(dev, opp_table)) { 1468283d55e6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1469dd461cd9SStephan Gerhold opp_table = ERR_PTR(-ENOMEM); 1470283d55e6SViresh Kumar } 147127c09484SViresh Kumar 147227c09484SViresh Kumar mutex_lock(&opp_table_lock); 147327c09484SViresh Kumar } else { 147427c09484SViresh Kumar opp_table = _allocate_opp_table(dev, index); 147527c09484SViresh Kumar 147627c09484SViresh Kumar mutex_lock(&opp_table_lock); 147727c09484SViresh Kumar if (!IS_ERR(opp_table)) 147827c09484SViresh Kumar list_add(&opp_table->node, &opp_tables); 1479283d55e6SViresh Kumar } 1480283d55e6SViresh Kumar 148127c09484SViresh Kumar opp_tables_busy = false; 14827813dd6fSViresh Kumar 14837813dd6fSViresh Kumar unlock: 14847813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 14857813dd6fSViresh Kumar 148632439ac7SViresh Kumar return _update_opp_table_clk(dev, opp_table, getclk); 14877813dd6fSViresh Kumar } 1488eb7c8743SViresh Kumar 148932439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk) 1490e77dcb0bSViresh Kumar { 149132439ac7SViresh Kumar return _add_opp_table_indexed(dev, 0, getclk); 1492e77dcb0bSViresh Kumar } 1493e77dcb0bSViresh Kumar 1494eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 1495eb7c8743SViresh Kumar { 1496e77dcb0bSViresh Kumar return _find_opp_table(dev); 1497eb7c8743SViresh Kumar } 14987813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 14997813dd6fSViresh Kumar 15007813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 15017813dd6fSViresh Kumar { 15027813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 1503cdd6ed90SViresh Kumar struct opp_device *opp_dev, *temp; 15046d3f922cSGeorgi Djakov int i; 15057813dd6fSViresh Kumar 1506e0df59deSViresh Kumar /* Drop the lock as soon as we can */ 1507e0df59deSViresh Kumar list_del(&opp_table->node); 1508e0df59deSViresh Kumar mutex_unlock(&opp_table_lock); 1509e0df59deSViresh Kumar 151081c4d8a3SViresh Kumar if (opp_table->current_opp) 151181c4d8a3SViresh Kumar dev_pm_opp_put(opp_table->current_opp); 151281c4d8a3SViresh Kumar 15135d6d106fSViresh Kumar _of_clear_opp_table(opp_table); 15145d6d106fSViresh Kumar 15157813dd6fSViresh Kumar /* Release clk */ 15167813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 15177813dd6fSViresh Kumar clk_put(opp_table->clk); 15187813dd6fSViresh Kumar 15196d3f922cSGeorgi Djakov if (opp_table->paths) { 15206d3f922cSGeorgi Djakov for (i = 0; i < opp_table->path_count; i++) 15216d3f922cSGeorgi Djakov icc_put(opp_table->paths[i]); 15226d3f922cSGeorgi Djakov kfree(opp_table->paths); 15236d3f922cSGeorgi Djakov } 15246d3f922cSGeorgi Djakov 1525cdd6ed90SViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 1526cdd6ed90SViresh Kumar 1527cdd6ed90SViresh Kumar list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 15283d255699SViresh Kumar /* 1529cdd6ed90SViresh Kumar * The OPP table is getting removed, drop the performance state 1530cdd6ed90SViresh Kumar * constraints. 15313d255699SViresh Kumar */ 1532cdd6ed90SViresh Kumar if (opp_table->genpd_performance_state) 1533cdd6ed90SViresh Kumar dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 15347813dd6fSViresh Kumar 15357813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 1536cdd6ed90SViresh Kumar } 15377813dd6fSViresh Kumar 15384f018bc0SViresh Kumar mutex_destroy(&opp_table->genpd_virt_dev_lock); 15397813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 15407813dd6fSViresh Kumar kfree(opp_table); 15417813dd6fSViresh Kumar } 15427813dd6fSViresh Kumar 15437813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 15447813dd6fSViresh Kumar { 15457813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 15467813dd6fSViresh Kumar &opp_table_lock); 15477813dd6fSViresh Kumar } 15487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 15497813dd6fSViresh Kumar 15507813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 15517813dd6fSViresh Kumar { 15527813dd6fSViresh Kumar kfree(opp); 15537813dd6fSViresh Kumar } 15547813dd6fSViresh Kumar 1555cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref) 15567813dd6fSViresh Kumar { 1557cf1fac94SViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 1558cf1fac94SViresh Kumar struct opp_table *opp_table = opp->opp_table; 1559cf1fac94SViresh Kumar 1560cf1fac94SViresh Kumar list_del(&opp->node); 1561cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1562cf1fac94SViresh Kumar 15637813dd6fSViresh Kumar /* 15647813dd6fSViresh Kumar * Notify the changes in the availability of the operable 15657813dd6fSViresh Kumar * frequency/voltage list. 15667813dd6fSViresh Kumar */ 15677813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 1568da544b61SViresh Kumar _of_opp_free_required_opps(opp_table, opp); 15697813dd6fSViresh Kumar opp_debug_remove_one(opp); 15707813dd6fSViresh Kumar kfree(opp); 15711690d8bbSViresh Kumar } 15727813dd6fSViresh Kumar 1573a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 15747813dd6fSViresh Kumar { 15757813dd6fSViresh Kumar kref_get(&opp->kref); 15767813dd6fSViresh Kumar } 15777813dd6fSViresh Kumar 15787813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 15797813dd6fSViresh Kumar { 1580cf1fac94SViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 15817813dd6fSViresh Kumar } 15827813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 15837813dd6fSViresh Kumar 15847813dd6fSViresh Kumar /** 15857813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 15867813dd6fSViresh Kumar * @dev: device for which we do this operation 15877813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 15887813dd6fSViresh Kumar * 15897813dd6fSViresh Kumar * This function removes an opp from the opp table. 15907813dd6fSViresh Kumar */ 15917813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 15927813dd6fSViresh Kumar { 159395073b72SJakob Koschel struct dev_pm_opp *opp = NULL, *iter; 15947813dd6fSViresh Kumar struct opp_table *opp_table; 15957813dd6fSViresh Kumar 15967813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 15977813dd6fSViresh Kumar if (IS_ERR(opp_table)) 15987813dd6fSViresh Kumar return; 15997813dd6fSViresh Kumar 16007813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 16017813dd6fSViresh Kumar 160295073b72SJakob Koschel list_for_each_entry(iter, &opp_table->opp_list, node) { 160395073b72SJakob Koschel if (iter->rate == freq) { 160495073b72SJakob Koschel opp = iter; 16057813dd6fSViresh Kumar break; 16067813dd6fSViresh Kumar } 16077813dd6fSViresh Kumar } 16087813dd6fSViresh Kumar 16097813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 16107813dd6fSViresh Kumar 161195073b72SJakob Koschel if (opp) { 16127813dd6fSViresh Kumar dev_pm_opp_put(opp); 16130ad8c623SViresh Kumar 16140ad8c623SViresh Kumar /* Drop the reference taken by dev_pm_opp_add() */ 16150ad8c623SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16167813dd6fSViresh Kumar } else { 16177813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 16187813dd6fSViresh Kumar __func__, freq); 16197813dd6fSViresh Kumar } 16207813dd6fSViresh Kumar 16210ad8c623SViresh Kumar /* Drop the reference taken by _find_opp_table() */ 16227813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16237813dd6fSViresh Kumar } 16247813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 16257813dd6fSViresh Kumar 1626cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table, 1627cf1fac94SViresh Kumar bool dynamic) 1628cf1fac94SViresh Kumar { 1629cf1fac94SViresh Kumar struct dev_pm_opp *opp = NULL, *temp; 1630cf1fac94SViresh Kumar 1631cf1fac94SViresh Kumar mutex_lock(&opp_table->lock); 1632cf1fac94SViresh Kumar list_for_each_entry(temp, &opp_table->opp_list, node) { 1633606a5d42SBeata Michalska /* 1634606a5d42SBeata Michalska * Refcount must be dropped only once for each OPP by OPP core, 1635606a5d42SBeata Michalska * do that with help of "removed" flag. 1636606a5d42SBeata Michalska */ 1637606a5d42SBeata Michalska if (!temp->removed && dynamic == temp->dynamic) { 1638cf1fac94SViresh Kumar opp = temp; 1639cf1fac94SViresh Kumar break; 1640cf1fac94SViresh Kumar } 1641cf1fac94SViresh Kumar } 1642cf1fac94SViresh Kumar 1643cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1644cf1fac94SViresh Kumar return opp; 1645cf1fac94SViresh Kumar } 1646cf1fac94SViresh Kumar 1647606a5d42SBeata Michalska /* 1648606a5d42SBeata Michalska * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to 1649606a5d42SBeata Michalska * happen lock less to avoid circular dependency issues. This routine must be 1650606a5d42SBeata Michalska * called without the opp_table->lock held. 1651606a5d42SBeata Michalska */ 1652606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic) 165303758d60SViresh Kumar { 1654cf1fac94SViresh Kumar struct dev_pm_opp *opp; 165503758d60SViresh Kumar 1656606a5d42SBeata Michalska while ((opp = _opp_get_next(opp_table, dynamic))) { 1657606a5d42SBeata Michalska opp->removed = true; 1658606a5d42SBeata Michalska dev_pm_opp_put(opp); 1659606a5d42SBeata Michalska 1660606a5d42SBeata Michalska /* Drop the references taken by dev_pm_opp_add() */ 1661606a5d42SBeata Michalska if (dynamic) 1662606a5d42SBeata Michalska dev_pm_opp_put_opp_table(opp_table); 1663606a5d42SBeata Michalska } 1664606a5d42SBeata Michalska } 1665606a5d42SBeata Michalska 1666606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table) 1667606a5d42SBeata Michalska { 166803758d60SViresh Kumar mutex_lock(&opp_table->lock); 166903758d60SViresh Kumar 1670922ff075SViresh Kumar if (!opp_table->parsed_static_opps) { 1671cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1672cf1fac94SViresh Kumar return false; 1673922ff075SViresh Kumar } 1674922ff075SViresh Kumar 1675cf1fac94SViresh Kumar if (--opp_table->parsed_static_opps) { 1676cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1677cf1fac94SViresh Kumar return true; 167803758d60SViresh Kumar } 167903758d60SViresh Kumar 168003758d60SViresh Kumar mutex_unlock(&opp_table->lock); 1681922ff075SViresh Kumar 1682606a5d42SBeata Michalska _opp_remove_all(opp_table, false); 1683cf1fac94SViresh Kumar return true; 168403758d60SViresh Kumar } 168503758d60SViresh Kumar 16861690d8bbSViresh Kumar /** 16871690d8bbSViresh Kumar * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 16881690d8bbSViresh Kumar * @dev: device for which we do this operation 16891690d8bbSViresh Kumar * 16901690d8bbSViresh Kumar * This function removes all dynamically created OPPs from the opp table. 16911690d8bbSViresh Kumar */ 16921690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev) 16931690d8bbSViresh Kumar { 16941690d8bbSViresh Kumar struct opp_table *opp_table; 16951690d8bbSViresh Kumar 16961690d8bbSViresh Kumar opp_table = _find_opp_table(dev); 16971690d8bbSViresh Kumar if (IS_ERR(opp_table)) 16981690d8bbSViresh Kumar return; 16991690d8bbSViresh Kumar 1700606a5d42SBeata Michalska _opp_remove_all(opp_table, true); 17011690d8bbSViresh Kumar 17021690d8bbSViresh Kumar /* Drop the reference taken by _find_opp_table() */ 17031690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17041690d8bbSViresh Kumar } 17051690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 17061690d8bbSViresh Kumar 17077813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table) 17087813dd6fSViresh Kumar { 17097813dd6fSViresh Kumar struct dev_pm_opp *opp; 17106d3f922cSGeorgi Djakov int supply_count, supply_size, icc_size; 17117813dd6fSViresh Kumar 17127813dd6fSViresh Kumar /* Allocate space for at least one supply */ 17136d3f922cSGeorgi Djakov supply_count = table->regulator_count > 0 ? table->regulator_count : 1; 17146d3f922cSGeorgi Djakov supply_size = sizeof(*opp->supplies) * supply_count; 17156d3f922cSGeorgi Djakov icc_size = sizeof(*opp->bandwidth) * table->path_count; 17167813dd6fSViresh Kumar 17177813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 17186d3f922cSGeorgi Djakov opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL); 17196d3f922cSGeorgi Djakov 17207813dd6fSViresh Kumar if (!opp) 17217813dd6fSViresh Kumar return NULL; 17227813dd6fSViresh Kumar 17237813dd6fSViresh Kumar /* Put the supplies at the end of the OPP structure as an empty array */ 17247813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 17256d3f922cSGeorgi Djakov if (icc_size) 17266d3f922cSGeorgi Djakov opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count); 17277813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 17287813dd6fSViresh Kumar 17297813dd6fSViresh Kumar return opp; 17307813dd6fSViresh Kumar } 17317813dd6fSViresh Kumar 17327813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 17337813dd6fSViresh Kumar struct opp_table *opp_table) 17347813dd6fSViresh Kumar { 17357813dd6fSViresh Kumar struct regulator *reg; 17367813dd6fSViresh Kumar int i; 17377813dd6fSViresh Kumar 173890e3577bSViresh Kumar if (!opp_table->regulators) 173990e3577bSViresh Kumar return true; 174090e3577bSViresh Kumar 17417813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 17427813dd6fSViresh Kumar reg = opp_table->regulators[i]; 17437813dd6fSViresh Kumar 17447813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 17457813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 17467813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 17477813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 17487813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 17497813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 17507813dd6fSViresh Kumar return false; 17517813dd6fSViresh Kumar } 17527813dd6fSViresh Kumar } 17537813dd6fSViresh Kumar 17547813dd6fSViresh Kumar return true; 17557813dd6fSViresh Kumar } 17567813dd6fSViresh Kumar 17576c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2) 17586c591eecSSaravana Kannan { 17596c591eecSSaravana Kannan if (opp1->rate != opp2->rate) 17606c591eecSSaravana Kannan return opp1->rate < opp2->rate ? -1 : 1; 17616d3f922cSGeorgi Djakov if (opp1->bandwidth && opp2->bandwidth && 17626d3f922cSGeorgi Djakov opp1->bandwidth[0].peak != opp2->bandwidth[0].peak) 17636d3f922cSGeorgi Djakov return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1; 17646c591eecSSaravana Kannan if (opp1->level != opp2->level) 17656c591eecSSaravana Kannan return opp1->level < opp2->level ? -1 : 1; 17666c591eecSSaravana Kannan return 0; 17676c591eecSSaravana Kannan } 17686c591eecSSaravana Kannan 1769a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1770a1e8c136SViresh Kumar struct opp_table *opp_table, 1771a1e8c136SViresh Kumar struct list_head **head) 1772a1e8c136SViresh Kumar { 1773a1e8c136SViresh Kumar struct dev_pm_opp *opp; 17746c591eecSSaravana Kannan int opp_cmp; 1775a1e8c136SViresh Kumar 1776a1e8c136SViresh Kumar /* 1777a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1778a1e8c136SViresh Kumar * already present. 1779a1e8c136SViresh Kumar * 1780a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1781a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1782a1e8c136SViresh Kumar * loop. 1783a1e8c136SViresh Kumar */ 1784a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 17856c591eecSSaravana Kannan opp_cmp = _opp_compare_key(new_opp, opp); 17866c591eecSSaravana Kannan if (opp_cmp > 0) { 1787a1e8c136SViresh Kumar *head = &opp->node; 1788a1e8c136SViresh Kumar continue; 1789a1e8c136SViresh Kumar } 1790a1e8c136SViresh Kumar 17916c591eecSSaravana Kannan if (opp_cmp < 0) 1792a1e8c136SViresh Kumar return 0; 1793a1e8c136SViresh Kumar 1794a1e8c136SViresh Kumar /* Duplicate OPPs */ 1795a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1796a1e8c136SViresh Kumar __func__, opp->rate, opp->supplies[0].u_volt, 1797a1e8c136SViresh Kumar opp->available, new_opp->rate, 1798a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1799a1e8c136SViresh Kumar 1800a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1801a1e8c136SViresh Kumar return opp->available && 1802a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1803a1e8c136SViresh Kumar } 1804a1e8c136SViresh Kumar 1805a1e8c136SViresh Kumar return 0; 1806a1e8c136SViresh Kumar } 1807a1e8c136SViresh Kumar 18087eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count) 18097eba0c76SViresh Kumar { 18107eba0c76SViresh Kumar int i; 18117eba0c76SViresh Kumar 18127eba0c76SViresh Kumar for (i = 0; i < count; i++) { 18137eba0c76SViresh Kumar if (opp->required_opps[i]->available) 18147eba0c76SViresh Kumar continue; 18157eba0c76SViresh Kumar 18167eba0c76SViresh Kumar opp->available = false; 18177eba0c76SViresh Kumar pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n", 18187eba0c76SViresh Kumar __func__, opp->required_opps[i]->np, opp->rate); 18197eba0c76SViresh Kumar return; 18207eba0c76SViresh Kumar } 18217eba0c76SViresh Kumar } 18227eba0c76SViresh Kumar 18237813dd6fSViresh Kumar /* 18247813dd6fSViresh Kumar * Returns: 18257813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 18267813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 18277813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 18287813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 18297813dd6fSViresh Kumar * kernel try to initialize the OPP table. 18307813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 18317813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 18327813dd6fSViresh Kumar */ 18337813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1834a1e8c136SViresh Kumar struct opp_table *opp_table, bool rate_not_available) 18357813dd6fSViresh Kumar { 18367813dd6fSViresh Kumar struct list_head *head; 18377813dd6fSViresh Kumar int ret; 18387813dd6fSViresh Kumar 18397813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 18407813dd6fSViresh Kumar head = &opp_table->opp_list; 18417813dd6fSViresh Kumar 1842a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1843a1e8c136SViresh Kumar if (ret) { 18447813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 18457813dd6fSViresh Kumar return ret; 18467813dd6fSViresh Kumar } 18477813dd6fSViresh Kumar 18487813dd6fSViresh Kumar list_add(&new_opp->node, head); 18497813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 18507813dd6fSViresh Kumar 18517813dd6fSViresh Kumar new_opp->opp_table = opp_table; 18527813dd6fSViresh Kumar kref_init(&new_opp->kref); 18537813dd6fSViresh Kumar 1854a2dea4cbSGreg Kroah-Hartman opp_debug_create_one(new_opp, opp_table); 18557813dd6fSViresh Kumar 18567813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 18577813dd6fSViresh Kumar new_opp->available = false; 18587813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 18597813dd6fSViresh Kumar __func__, new_opp->rate); 18607813dd6fSViresh Kumar } 18617813dd6fSViresh Kumar 18627eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 18637eba0c76SViresh Kumar if (lazy_linking_pending(opp_table)) 18647eba0c76SViresh Kumar return 0; 1865cf65948dSDmitry Osipenko 18667eba0c76SViresh Kumar _required_opps_available(new_opp, opp_table->required_opp_count); 1867cf65948dSDmitry Osipenko 18687813dd6fSViresh Kumar return 0; 18697813dd6fSViresh Kumar } 18707813dd6fSViresh Kumar 18717813dd6fSViresh Kumar /** 18727813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 18737813dd6fSViresh Kumar * @opp_table: OPP table 18747813dd6fSViresh Kumar * @dev: device for which we do this operation 18757813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 18767813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 18777813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 18787813dd6fSViresh Kumar * 18797813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 18807813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 18817813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 18827813dd6fSViresh Kumar * 18837813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 18847813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 18857813dd6fSViresh Kumar * 18867813dd6fSViresh Kumar * Return: 18877813dd6fSViresh Kumar * 0 On success OR 18887813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 18897813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 18907813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 18917813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 18927813dd6fSViresh Kumar */ 18937813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 18947813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 18957813dd6fSViresh Kumar { 18967813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 18977813dd6fSViresh Kumar unsigned long tol; 18987813dd6fSViresh Kumar int ret; 18997813dd6fSViresh Kumar 19007813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 19017813dd6fSViresh Kumar if (!new_opp) 19027813dd6fSViresh Kumar return -ENOMEM; 19037813dd6fSViresh Kumar 19047813dd6fSViresh Kumar /* populate the opp table */ 19057813dd6fSViresh Kumar new_opp->rate = freq; 19067813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 19077813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 19087813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 19097813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 19107813dd6fSViresh Kumar new_opp->available = true; 19117813dd6fSViresh Kumar new_opp->dynamic = dynamic; 19127813dd6fSViresh Kumar 1913a1e8c136SViresh Kumar ret = _opp_add(dev, new_opp, opp_table, false); 19147813dd6fSViresh Kumar if (ret) { 19157813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 19167813dd6fSViresh Kumar if (ret == -EBUSY) 19177813dd6fSViresh Kumar ret = 0; 19187813dd6fSViresh Kumar goto free_opp; 19197813dd6fSViresh Kumar } 19207813dd6fSViresh Kumar 19217813dd6fSViresh Kumar /* 19227813dd6fSViresh Kumar * Notify the changes in the availability of the operable 19237813dd6fSViresh Kumar * frequency/voltage list. 19247813dd6fSViresh Kumar */ 19257813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 19267813dd6fSViresh Kumar return 0; 19277813dd6fSViresh Kumar 19287813dd6fSViresh Kumar free_opp: 19297813dd6fSViresh Kumar _opp_free(new_opp); 19307813dd6fSViresh Kumar 19317813dd6fSViresh Kumar return ret; 19327813dd6fSViresh Kumar } 19337813dd6fSViresh Kumar 19347813dd6fSViresh Kumar /** 193589f03984SViresh Kumar * _opp_set_supported_hw() - Set supported platforms 19367813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 19377813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 19387813dd6fSViresh Kumar * @count: Number of elements in the array. 19397813dd6fSViresh Kumar * 19407813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 19417813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 19427813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 19437813dd6fSViresh Kumar * property. 19447813dd6fSViresh Kumar */ 194589f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table, 19467813dd6fSViresh Kumar const u32 *versions, unsigned int count) 19477813dd6fSViresh Kumar { 194825419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 194925419de1SViresh Kumar if (opp_table->supported_hw) 195089f03984SViresh Kumar return 0; 19517813dd6fSViresh Kumar 19527813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 19537813dd6fSViresh Kumar GFP_KERNEL); 195489f03984SViresh Kumar if (!opp_table->supported_hw) 195589f03984SViresh Kumar return -ENOMEM; 19567813dd6fSViresh Kumar 19577813dd6fSViresh Kumar opp_table->supported_hw_count = count; 19587813dd6fSViresh Kumar 195989f03984SViresh Kumar return 0; 19607813dd6fSViresh Kumar } 19617813dd6fSViresh Kumar 19627813dd6fSViresh Kumar /** 196389f03984SViresh Kumar * _opp_put_supported_hw() - Releases resources blocked for supported hw 196489f03984SViresh Kumar * @opp_table: OPP table returned by _opp_set_supported_hw(). 19657813dd6fSViresh Kumar * 19667813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 196789f03984SViresh Kumar * _opp_set_supported_hw(). Until this is called, the opp_table structure 19687813dd6fSViresh Kumar * will not be freed. 19697813dd6fSViresh Kumar */ 197089f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table) 19717813dd6fSViresh Kumar { 197289f03984SViresh Kumar if (opp_table->supported_hw) { 19737813dd6fSViresh Kumar kfree(opp_table->supported_hw); 19747813dd6fSViresh Kumar opp_table->supported_hw = NULL; 19757813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 19767813dd6fSViresh Kumar } 19779c4f220fSYangtao Li } 19789c4f220fSYangtao Li 19799c4f220fSYangtao Li /** 1980298098e5SViresh Kumar * _opp_set_prop_name() - Set prop-extn name 19817813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 19827813dd6fSViresh Kumar * @name: name to postfix to properties. 19837813dd6fSViresh Kumar * 19847813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 19857813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 19867813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 19877813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 19887813dd6fSViresh Kumar */ 1989298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name) 19907813dd6fSViresh Kumar { 1991878ec1a9SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 19927813dd6fSViresh Kumar if (!opp_table->prop_name) { 1993298098e5SViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 1994298098e5SViresh Kumar if (!opp_table->prop_name) 1995298098e5SViresh Kumar return -ENOMEM; 19967813dd6fSViresh Kumar } 19977813dd6fSViresh Kumar 1998298098e5SViresh Kumar return 0; 19997813dd6fSViresh Kumar } 20007813dd6fSViresh Kumar 20017813dd6fSViresh Kumar /** 2002298098e5SViresh Kumar * _opp_put_prop_name() - Releases resources blocked for prop-name 2003298098e5SViresh Kumar * @opp_table: OPP table returned by _opp_set_prop_name(). 20047813dd6fSViresh Kumar * 20057813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 2006298098e5SViresh Kumar * _opp_set_prop_name(). Until this is called, the opp_table structure 20077813dd6fSViresh Kumar * will not be freed. 20087813dd6fSViresh Kumar */ 2009298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table) 20107813dd6fSViresh Kumar { 2011298098e5SViresh Kumar if (opp_table->prop_name) { 20127813dd6fSViresh Kumar kfree(opp_table->prop_name); 20137813dd6fSViresh Kumar opp_table->prop_name = NULL; 20147813dd6fSViresh Kumar } 2015298098e5SViresh Kumar } 20167813dd6fSViresh Kumar 20177813dd6fSViresh Kumar /** 2018b0ec0942SViresh Kumar * _opp_set_regulators() - Set regulator names for the device 20197813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 20207813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 20217813dd6fSViresh Kumar * @count: Number of regulators. 20227813dd6fSViresh Kumar * 20237813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 20247813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 20257813dd6fSViresh Kumar * well. 20267813dd6fSViresh Kumar * 20277813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 20287813dd6fSViresh Kumar */ 2029b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev, 203087686cc8SViresh Kumar const char * const names[]) 20317813dd6fSViresh Kumar { 203287686cc8SViresh Kumar const char * const *temp = names; 20337813dd6fSViresh Kumar struct regulator *reg; 203487686cc8SViresh Kumar int count = 0, ret, i; 203587686cc8SViresh Kumar 203687686cc8SViresh Kumar /* Count number of regulators */ 203787686cc8SViresh Kumar while (*temp++) 203887686cc8SViresh Kumar count++; 203987686cc8SViresh Kumar 204087686cc8SViresh Kumar if (!count) 2041b0ec0942SViresh Kumar return -EINVAL; 20427813dd6fSViresh Kumar 2043779b783cSViresh Kumar /* Another CPU that shares the OPP table has set the regulators ? */ 2044779b783cSViresh Kumar if (opp_table->regulators) 2045b0ec0942SViresh Kumar return 0; 20467813dd6fSViresh Kumar 20477813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 20487813dd6fSViresh Kumar sizeof(*opp_table->regulators), 20497813dd6fSViresh Kumar GFP_KERNEL); 2050b0ec0942SViresh Kumar if (!opp_table->regulators) 2051b0ec0942SViresh Kumar return -ENOMEM; 20527813dd6fSViresh Kumar 20537813dd6fSViresh Kumar for (i = 0; i < count; i++) { 20547813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 20557813dd6fSViresh Kumar if (IS_ERR(reg)) { 2056543256d2SKrzysztof Kozlowski ret = dev_err_probe(dev, PTR_ERR(reg), 2057543256d2SKrzysztof Kozlowski "%s: no regulator (%s) found\n", 2058543256d2SKrzysztof Kozlowski __func__, names[i]); 20597813dd6fSViresh Kumar goto free_regulators; 20607813dd6fSViresh Kumar } 20617813dd6fSViresh Kumar 20627813dd6fSViresh Kumar opp_table->regulators[i] = reg; 20637813dd6fSViresh Kumar } 20647813dd6fSViresh Kumar 20657813dd6fSViresh Kumar opp_table->regulator_count = count; 20667813dd6fSViresh Kumar 2067c522ce8aSViresh Kumar /* Set generic config_regulators() for single regulators here */ 2068c522ce8aSViresh Kumar if (count == 1) 2069c522ce8aSViresh Kumar opp_table->config_regulators = _opp_config_regulator_single; 2070c522ce8aSViresh Kumar 2071b0ec0942SViresh Kumar return 0; 20727813dd6fSViresh Kumar 20737813dd6fSViresh Kumar free_regulators: 207424957db1SMarek Szyprowski while (i != 0) 207524957db1SMarek Szyprowski regulator_put(opp_table->regulators[--i]); 20767813dd6fSViresh Kumar 20777813dd6fSViresh Kumar kfree(opp_table->regulators); 20787813dd6fSViresh Kumar opp_table->regulators = NULL; 207946f48acaSViresh Kumar opp_table->regulator_count = -1; 20807813dd6fSViresh Kumar 2081b0ec0942SViresh Kumar return ret; 20827813dd6fSViresh Kumar } 20837813dd6fSViresh Kumar 20847813dd6fSViresh Kumar /** 2085b0ec0942SViresh Kumar * _opp_put_regulators() - Releases resources blocked for regulator 2086b0ec0942SViresh Kumar * @opp_table: OPP table returned from _opp_set_regulators(). 20877813dd6fSViresh Kumar */ 2088b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table) 20897813dd6fSViresh Kumar { 20907813dd6fSViresh Kumar int i; 20917813dd6fSViresh Kumar 2092779b783cSViresh Kumar if (!opp_table->regulators) 2093b0ec0942SViresh Kumar return; 20947813dd6fSViresh Kumar 209572f80ce4SViresh Kumar if (opp_table->enabled) { 20968d45719cSKamil Konieczny for (i = opp_table->regulator_count - 1; i >= 0; i--) 20978d45719cSKamil Konieczny regulator_disable(opp_table->regulators[i]); 20988d45719cSKamil Konieczny } 20998d45719cSKamil Konieczny 210024957db1SMarek Szyprowski for (i = opp_table->regulator_count - 1; i >= 0; i--) 21017813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 21027813dd6fSViresh Kumar 21037813dd6fSViresh Kumar kfree(opp_table->regulators); 21047813dd6fSViresh Kumar opp_table->regulators = NULL; 210546f48acaSViresh Kumar opp_table->regulator_count = -1; 21067813dd6fSViresh Kumar } 210732aee78bSYangtao Li 21087813dd6fSViresh Kumar /** 21092368f576SViresh Kumar * _opp_set_clknames() - Set clk names for the device 21102368f576SViresh Kumar * @dev: Device for which clk names is being set. 21112368f576SViresh Kumar * @names: Clk names. 21127813dd6fSViresh Kumar * 21132368f576SViresh Kumar * In order to support OPP switching, OPP layer needs to get pointers to the 21142368f576SViresh Kumar * clocks for the device. Simple cases work fine without using this routine 21152368f576SViresh Kumar * (i.e. by passing connection-id as NULL), but for a device with multiple 21162368f576SViresh Kumar * clocks available, the OPP core needs to know the exact names of the clks to 21172368f576SViresh Kumar * use. 21187813dd6fSViresh Kumar * 21197813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 21207813dd6fSViresh Kumar */ 21212368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev, 21222368f576SViresh Kumar const char * const names[]) 21237813dd6fSViresh Kumar { 21242368f576SViresh Kumar const char * const *temp = names; 21252368f576SViresh Kumar int count = 0; 21267813dd6fSViresh Kumar 21272368f576SViresh Kumar /* Count number of clks */ 21282368f576SViresh Kumar while (*temp++) 21292368f576SViresh Kumar count++; 21307813dd6fSViresh Kumar 21312368f576SViresh Kumar /* 21322368f576SViresh Kumar * This is a special case where we have a single clock, whose connection 21332368f576SViresh Kumar * id name is NULL, i.e. first two entries are NULL in the array. 21342368f576SViresh Kumar */ 21352368f576SViresh Kumar if (!count && !names[1]) 21362368f576SViresh Kumar count = 1; 21372368f576SViresh Kumar 21382368f576SViresh Kumar /* We support only one clock name for now */ 21392368f576SViresh Kumar if (count != 1) 21402368f576SViresh Kumar return -EINVAL; 21417813dd6fSViresh Kumar 21420a43452bSViresh Kumar /* Another CPU that shares the OPP table has set the clkname ? */ 21430a43452bSViresh Kumar if (opp_table->clk_configured) 21442368f576SViresh Kumar return 0; 21450a43452bSViresh Kumar 214632439ac7SViresh Kumar /* clk shouldn't be initialized at this point */ 21472368f576SViresh Kumar if (WARN_ON(opp_table->clk)) 21482368f576SViresh Kumar return -EBUSY; 21497813dd6fSViresh Kumar 21507813dd6fSViresh Kumar /* Find clk for the device */ 21512368f576SViresh Kumar opp_table->clk = clk_get(dev, names[0]); 21527813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 21532368f576SViresh Kumar return dev_err_probe(dev, PTR_ERR(opp_table->clk), 2154543256d2SKrzysztof Kozlowski "%s: Couldn't find clock\n", __func__); 21557813dd6fSViresh Kumar } 21567813dd6fSViresh Kumar 21570a43452bSViresh Kumar opp_table->clk_configured = true; 21580a43452bSViresh Kumar 21592368f576SViresh Kumar return 0; 21607813dd6fSViresh Kumar } 21617813dd6fSViresh Kumar 21627813dd6fSViresh Kumar /** 21632368f576SViresh Kumar * _opp_put_clknames() - Releases resources blocked for clks. 21642368f576SViresh Kumar * @opp_table: OPP table returned from _opp_set_clknames(). 21657813dd6fSViresh Kumar */ 21662368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table) 21677813dd6fSViresh Kumar { 21682368f576SViresh Kumar if (opp_table->clk_configured) { 21697813dd6fSViresh Kumar clk_put(opp_table->clk); 21707813dd6fSViresh Kumar opp_table->clk = ERR_PTR(-EINVAL); 21710a43452bSViresh Kumar opp_table->clk_configured = false; 21727813dd6fSViresh Kumar } 2173a74f681cSYangtao Li } 2174a74f681cSYangtao Li 2175a74f681cSYangtao Li /** 2176aee3352fSViresh Kumar * _opp_set_config_regulators_helper() - Register custom set regulator helper. 2177aee3352fSViresh Kumar * @dev: Device for which the helper is getting registered. 2178aee3352fSViresh Kumar * @config_regulators: Custom set regulator helper. 2179aee3352fSViresh Kumar * 2180aee3352fSViresh Kumar * This is useful to support platforms with multiple regulators per device. 2181aee3352fSViresh Kumar * 2182aee3352fSViresh Kumar * This must be called before any OPPs are initialized for the device. 2183aee3352fSViresh Kumar */ 2184aee3352fSViresh Kumar static int _opp_set_config_regulators_helper(struct opp_table *opp_table, 2185aee3352fSViresh Kumar struct device *dev, config_regulators_t config_regulators) 2186aee3352fSViresh Kumar { 2187aee3352fSViresh Kumar /* Another CPU that shares the OPP table has set the helper ? */ 2188aee3352fSViresh Kumar if (!opp_table->config_regulators) 2189aee3352fSViresh Kumar opp_table->config_regulators = config_regulators; 2190aee3352fSViresh Kumar 2191aee3352fSViresh Kumar return 0; 2192aee3352fSViresh Kumar } 2193aee3352fSViresh Kumar 2194aee3352fSViresh Kumar /** 2195aee3352fSViresh Kumar * _opp_put_config_regulators_helper() - Releases resources blocked for 2196aee3352fSViresh Kumar * config_regulators helper. 2197aee3352fSViresh Kumar * @opp_table: OPP table returned from _opp_set_config_regulators_helper(). 2198aee3352fSViresh Kumar * 2199aee3352fSViresh Kumar * Release resources blocked for platform specific config_regulators helper. 2200aee3352fSViresh Kumar */ 2201aee3352fSViresh Kumar static void _opp_put_config_regulators_helper(struct opp_table *opp_table) 2202aee3352fSViresh Kumar { 2203aee3352fSViresh Kumar if (opp_table->config_regulators) 2204aee3352fSViresh Kumar opp_table->config_regulators = NULL; 2205aee3352fSViresh Kumar } 2206aee3352fSViresh Kumar 2207442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table) 22086319aee1SViresh Kumar { 22096319aee1SViresh Kumar int index; 22106319aee1SViresh Kumar 2211cb60e960SViresh Kumar if (!opp_table->genpd_virt_devs) 2212cb60e960SViresh Kumar return; 2213cb60e960SViresh Kumar 22146319aee1SViresh Kumar for (index = 0; index < opp_table->required_opp_count; index++) { 22156319aee1SViresh Kumar if (!opp_table->genpd_virt_devs[index]) 22166319aee1SViresh Kumar continue; 22176319aee1SViresh Kumar 22186319aee1SViresh Kumar dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 22196319aee1SViresh Kumar opp_table->genpd_virt_devs[index] = NULL; 22206319aee1SViresh Kumar } 2221c0ab9e08SViresh Kumar 2222c0ab9e08SViresh Kumar kfree(opp_table->genpd_virt_devs); 2223c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = NULL; 22246319aee1SViresh Kumar } 22256319aee1SViresh Kumar 22267813dd6fSViresh Kumar /** 2227442e7a17SViresh Kumar * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 22286319aee1SViresh Kumar * @dev: Consumer device for which the genpd is getting attached. 22296319aee1SViresh Kumar * @names: Null terminated array of pointers containing names of genpd to attach. 223017a8f868SViresh Kumar * @virt_devs: Pointer to return the array of virtual devices. 22314f018bc0SViresh Kumar * 22324f018bc0SViresh Kumar * Multiple generic power domains for a device are supported with the help of 22334f018bc0SViresh Kumar * virtual genpd devices, which are created for each consumer device - genpd 22344f018bc0SViresh Kumar * pair. These are the device structures which are attached to the power domain 22354f018bc0SViresh Kumar * and are required by the OPP core to set the performance state of the genpd. 22366319aee1SViresh Kumar * The same API also works for the case where single genpd is available and so 22376319aee1SViresh Kumar * we don't need to support that separately. 22384f018bc0SViresh Kumar * 22394f018bc0SViresh Kumar * This helper will normally be called by the consumer driver of the device 22406319aee1SViresh Kumar * "dev", as only that has details of the genpd names. 22414f018bc0SViresh Kumar * 22426319aee1SViresh Kumar * This helper needs to be called once with a list of all genpd to attach. 22436319aee1SViresh Kumar * Otherwise the original device structure will be used instead by the OPP core. 2244baea35e4SViresh Kumar * 2245baea35e4SViresh Kumar * The order of entries in the names array must match the order in which 2246baea35e4SViresh Kumar * "required-opps" are added in DT. 22474f018bc0SViresh Kumar */ 2248442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev, 22493734b9f2SDmitry Osipenko const char * const *names, struct device ***virt_devs) 22504f018bc0SViresh Kumar { 22516319aee1SViresh Kumar struct device *virt_dev; 2252baea35e4SViresh Kumar int index = 0, ret = -EINVAL; 22533734b9f2SDmitry Osipenko const char * const *name = names; 22544f018bc0SViresh Kumar 2255cb60e960SViresh Kumar if (opp_table->genpd_virt_devs) 2256442e7a17SViresh Kumar return 0; 22574f018bc0SViresh Kumar 22586319aee1SViresh Kumar /* 22596319aee1SViresh Kumar * If the genpd's OPP table isn't already initialized, parsing of the 22606319aee1SViresh Kumar * required-opps fail for dev. We should retry this after genpd's OPP 22616319aee1SViresh Kumar * table is added. 22626319aee1SViresh Kumar */ 2263442e7a17SViresh Kumar if (!opp_table->required_opp_count) 2264442e7a17SViresh Kumar return -EPROBE_DEFER; 22656319aee1SViresh Kumar 22664f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 22674f018bc0SViresh Kumar 2268c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, 2269c0ab9e08SViresh Kumar sizeof(*opp_table->genpd_virt_devs), 2270c0ab9e08SViresh Kumar GFP_KERNEL); 2271c0ab9e08SViresh Kumar if (!opp_table->genpd_virt_devs) 2272c0ab9e08SViresh Kumar goto unlock; 22734f018bc0SViresh Kumar 22746319aee1SViresh Kumar while (*name) { 22756319aee1SViresh Kumar if (index >= opp_table->required_opp_count) { 22766319aee1SViresh Kumar dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 22776319aee1SViresh Kumar *name, opp_table->required_opp_count, index); 22786319aee1SViresh Kumar goto err; 22796319aee1SViresh Kumar } 22804f018bc0SViresh Kumar 22816319aee1SViresh Kumar virt_dev = dev_pm_domain_attach_by_name(dev, *name); 22824ea9496cSTang Bin if (IS_ERR_OR_NULL(virt_dev)) { 22834ea9496cSTang Bin ret = PTR_ERR(virt_dev) ? : -ENODEV; 22846319aee1SViresh Kumar dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 22856319aee1SViresh Kumar goto err; 22864f018bc0SViresh Kumar } 22874f018bc0SViresh Kumar 22884f018bc0SViresh Kumar opp_table->genpd_virt_devs[index] = virt_dev; 2289baea35e4SViresh Kumar index++; 22906319aee1SViresh Kumar name++; 22916319aee1SViresh Kumar } 22926319aee1SViresh Kumar 229317a8f868SViresh Kumar if (virt_devs) 229417a8f868SViresh Kumar *virt_devs = opp_table->genpd_virt_devs; 22954f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 22964f018bc0SViresh Kumar 2297442e7a17SViresh Kumar return 0; 22986319aee1SViresh Kumar 22996319aee1SViresh Kumar err: 2300442e7a17SViresh Kumar _detach_genpd(opp_table); 2301c0ab9e08SViresh Kumar unlock: 23026319aee1SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 2303442e7a17SViresh Kumar return ret; 23046319aee1SViresh Kumar 23054f018bc0SViresh Kumar } 23064f018bc0SViresh Kumar 23074f018bc0SViresh Kumar /** 2308442e7a17SViresh Kumar * _opp_detach_genpd() - Detach genpd(s) from the device. 2309442e7a17SViresh Kumar * @opp_table: OPP table returned by _opp_attach_genpd(). 23104f018bc0SViresh Kumar * 23116319aee1SViresh Kumar * This detaches the genpd(s), resets the virtual device pointers, and puts the 23126319aee1SViresh Kumar * OPP table. 23134f018bc0SViresh Kumar */ 2314442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table) 23154f018bc0SViresh Kumar { 23164f018bc0SViresh Kumar /* 23174f018bc0SViresh Kumar * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 23184f018bc0SViresh Kumar * used in parallel. 23194f018bc0SViresh Kumar */ 23204f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 2321442e7a17SViresh Kumar _detach_genpd(opp_table); 23224f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 23234f018bc0SViresh Kumar } 2324b4b9e223SDmitry Osipenko 232511b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data) 232611b9b663SViresh Kumar { 232711b9b663SViresh Kumar if (data->flags & OPP_CONFIG_GENPD) 2328442e7a17SViresh Kumar _opp_detach_genpd(data->opp_table); 232911b9b663SViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR) 2330b0ec0942SViresh Kumar _opp_put_regulators(data->opp_table); 233111b9b663SViresh Kumar if (data->flags & OPP_CONFIG_SUPPORTED_HW) 233289f03984SViresh Kumar _opp_put_supported_hw(data->opp_table); 2333*1f378c6eSViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR_HELPER) 2334aee3352fSViresh Kumar _opp_put_config_regulators_helper(data->opp_table); 233511b9b663SViresh Kumar if (data->flags & OPP_CONFIG_PROP_NAME) 2336298098e5SViresh Kumar _opp_put_prop_name(data->opp_table); 233711b9b663SViresh Kumar if (data->flags & OPP_CONFIG_CLK) 23382368f576SViresh Kumar _opp_put_clknames(data->opp_table); 233911b9b663SViresh Kumar 234011b9b663SViresh Kumar dev_pm_opp_put_opp_table(data->opp_table); 234111b9b663SViresh Kumar kfree(data); 234211b9b663SViresh Kumar } 234311b9b663SViresh Kumar 234411b9b663SViresh Kumar /** 234511b9b663SViresh Kumar * dev_pm_opp_set_config() - Set OPP configuration for the device. 234611b9b663SViresh Kumar * @dev: Device for which configuration is being set. 234711b9b663SViresh Kumar * @config: OPP configuration. 234811b9b663SViresh Kumar * 234911b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 235011b9b663SViresh Kumar * 235111b9b663SViresh Kumar * This must be called before any OPPs are initialized for the device. This may 235211b9b663SViresh Kumar * be called multiple times for the same OPP table, for example once for each 235311b9b663SViresh Kumar * CPU that share the same table. This must be balanced by the same number of 235411b9b663SViresh Kumar * calls to dev_pm_opp_clear_config() in order to free the OPP table properly. 235511b9b663SViresh Kumar * 235611b9b663SViresh Kumar * This returns a token to the caller, which must be passed to 235711b9b663SViresh Kumar * dev_pm_opp_clear_config() to free the resources later. The value of the 235811b9b663SViresh Kumar * returned token will be >= 1 for success and negative for errors. The minimum 235911b9b663SViresh Kumar * value of 1 is chosen here to make it easy for callers to manage the resource. 236011b9b663SViresh Kumar */ 236111b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 236211b9b663SViresh Kumar { 2363298098e5SViresh Kumar struct opp_table *opp_table; 236411b9b663SViresh Kumar struct opp_config_data *data; 236511b9b663SViresh Kumar unsigned int id; 236611b9b663SViresh Kumar int ret; 236711b9b663SViresh Kumar 236811b9b663SViresh Kumar data = kmalloc(sizeof(*data), GFP_KERNEL); 236911b9b663SViresh Kumar if (!data) 237011b9b663SViresh Kumar return -ENOMEM; 237111b9b663SViresh Kumar 237211b9b663SViresh Kumar opp_table = _add_opp_table(dev, false); 237311b9b663SViresh Kumar if (IS_ERR(opp_table)) { 237411b9b663SViresh Kumar kfree(data); 237511b9b663SViresh Kumar return PTR_ERR(opp_table); 237611b9b663SViresh Kumar } 237711b9b663SViresh Kumar 237811b9b663SViresh Kumar data->opp_table = opp_table; 237911b9b663SViresh Kumar data->flags = 0; 238011b9b663SViresh Kumar 238111b9b663SViresh Kumar /* This should be called before OPPs are initialized */ 238211b9b663SViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 238311b9b663SViresh Kumar ret = -EBUSY; 238411b9b663SViresh Kumar goto err; 238511b9b663SViresh Kumar } 238611b9b663SViresh Kumar 238711b9b663SViresh Kumar /* Configure clocks */ 238811b9b663SViresh Kumar if (config->clk_names) { 23892368f576SViresh Kumar ret = _opp_set_clknames(opp_table, dev, config->clk_names); 23902368f576SViresh Kumar if (ret) 239111b9b663SViresh Kumar goto err; 239211b9b663SViresh Kumar 239311b9b663SViresh Kumar data->flags |= OPP_CONFIG_CLK; 239411b9b663SViresh Kumar } 239511b9b663SViresh Kumar 239611b9b663SViresh Kumar /* Configure property names */ 239711b9b663SViresh Kumar if (config->prop_name) { 2398298098e5SViresh Kumar ret = _opp_set_prop_name(opp_table, config->prop_name); 2399298098e5SViresh Kumar if (ret) 240011b9b663SViresh Kumar goto err; 240111b9b663SViresh Kumar 240211b9b663SViresh Kumar data->flags |= OPP_CONFIG_PROP_NAME; 240311b9b663SViresh Kumar } 240411b9b663SViresh Kumar 2405aee3352fSViresh Kumar /* Configure config_regulators helper */ 2406aee3352fSViresh Kumar if (config->config_regulators) { 2407aee3352fSViresh Kumar ret = _opp_set_config_regulators_helper(opp_table, dev, 2408aee3352fSViresh Kumar config->config_regulators); 2409aee3352fSViresh Kumar if (ret) 2410aee3352fSViresh Kumar goto err; 2411aee3352fSViresh Kumar 2412aee3352fSViresh Kumar data->flags |= OPP_CONFIG_REGULATOR_HELPER; 2413aee3352fSViresh Kumar } 2414aee3352fSViresh Kumar 241511b9b663SViresh Kumar /* Configure supported hardware */ 241611b9b663SViresh Kumar if (config->supported_hw) { 241789f03984SViresh Kumar ret = _opp_set_supported_hw(opp_table, config->supported_hw, 241811b9b663SViresh Kumar config->supported_hw_count); 241989f03984SViresh Kumar if (ret) 242011b9b663SViresh Kumar goto err; 242111b9b663SViresh Kumar 242211b9b663SViresh Kumar data->flags |= OPP_CONFIG_SUPPORTED_HW; 242311b9b663SViresh Kumar } 242411b9b663SViresh Kumar 242511b9b663SViresh Kumar /* Configure supplies */ 242611b9b663SViresh Kumar if (config->regulator_names) { 2427b0ec0942SViresh Kumar ret = _opp_set_regulators(opp_table, dev, 2428b0ec0942SViresh Kumar config->regulator_names); 2429b0ec0942SViresh Kumar if (ret) 243011b9b663SViresh Kumar goto err; 243111b9b663SViresh Kumar 243211b9b663SViresh Kumar data->flags |= OPP_CONFIG_REGULATOR; 243311b9b663SViresh Kumar } 243411b9b663SViresh Kumar 243511b9b663SViresh Kumar /* Attach genpds */ 243611b9b663SViresh Kumar if (config->genpd_names) { 2437442e7a17SViresh Kumar ret = _opp_attach_genpd(opp_table, dev, config->genpd_names, 243811b9b663SViresh Kumar config->virt_devs); 2439442e7a17SViresh Kumar if (ret) 244011b9b663SViresh Kumar goto err; 244111b9b663SViresh Kumar 244211b9b663SViresh Kumar data->flags |= OPP_CONFIG_GENPD; 244311b9b663SViresh Kumar } 244411b9b663SViresh Kumar 244511b9b663SViresh Kumar ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX), 244611b9b663SViresh Kumar GFP_KERNEL); 244711b9b663SViresh Kumar if (ret) 244811b9b663SViresh Kumar goto err; 244911b9b663SViresh Kumar 245011b9b663SViresh Kumar return id; 245111b9b663SViresh Kumar 245211b9b663SViresh Kumar err: 245311b9b663SViresh Kumar _opp_clear_config(data); 245411b9b663SViresh Kumar return ret; 245511b9b663SViresh Kumar } 245611b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config); 245711b9b663SViresh Kumar 245811b9b663SViresh Kumar /** 245911b9b663SViresh Kumar * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration. 246011b9b663SViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_config(). 246111b9b663SViresh Kumar * 246211b9b663SViresh Kumar * This allows all device OPP configurations to be cleared at once. This must be 246311b9b663SViresh Kumar * called once for each call made to dev_pm_opp_set_config(), in order to free 246411b9b663SViresh Kumar * the OPPs properly. 246511b9b663SViresh Kumar * 246611b9b663SViresh Kumar * Currently the first call itself ends up freeing all the OPP configurations, 246711b9b663SViresh Kumar * while the later ones only drop the OPP table reference. This works well for 246811b9b663SViresh Kumar * now as we would never want to use an half initialized OPP table and want to 246911b9b663SViresh Kumar * remove the configurations together. 247011b9b663SViresh Kumar */ 247111b9b663SViresh Kumar void dev_pm_opp_clear_config(int token) 247211b9b663SViresh Kumar { 247311b9b663SViresh Kumar struct opp_config_data *data; 247411b9b663SViresh Kumar 247511b9b663SViresh Kumar /* 247611b9b663SViresh Kumar * This lets the callers call this unconditionally and keep their code 247711b9b663SViresh Kumar * simple. 247811b9b663SViresh Kumar */ 247911b9b663SViresh Kumar if (unlikely(token <= 0)) 248011b9b663SViresh Kumar return; 248111b9b663SViresh Kumar 248211b9b663SViresh Kumar data = xa_erase(&opp_configs, token); 248311b9b663SViresh Kumar if (WARN_ON(!data)) 248411b9b663SViresh Kumar return; 248511b9b663SViresh Kumar 248611b9b663SViresh Kumar _opp_clear_config(data); 248711b9b663SViresh Kumar } 248811b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config); 248911b9b663SViresh Kumar 249011b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token) 249111b9b663SViresh Kumar { 249211b9b663SViresh Kumar dev_pm_opp_clear_config((unsigned long)token); 249311b9b663SViresh Kumar } 249411b9b663SViresh Kumar 249511b9b663SViresh Kumar /** 249611b9b663SViresh Kumar * devm_pm_opp_set_config() - Set OPP configuration for the device. 249711b9b663SViresh Kumar * @dev: Device for which configuration is being set. 249811b9b663SViresh Kumar * @config: OPP configuration. 249911b9b663SViresh Kumar * 250011b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 250111b9b663SViresh Kumar * This is a resource-managed variant of dev_pm_opp_set_config(). 250211b9b663SViresh Kumar * 250311b9b663SViresh Kumar * Return: 0 on success and errorno otherwise. 250411b9b663SViresh Kumar */ 250511b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 250611b9b663SViresh Kumar { 250711b9b663SViresh Kumar int token = dev_pm_opp_set_config(dev, config); 250811b9b663SViresh Kumar 250911b9b663SViresh Kumar if (token < 0) 251011b9b663SViresh Kumar return token; 251111b9b663SViresh Kumar 251211b9b663SViresh Kumar return devm_add_action_or_reset(dev, devm_pm_opp_config_release, 251311b9b663SViresh Kumar (void *) ((unsigned long) token)); 251411b9b663SViresh Kumar } 251511b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config); 251611b9b663SViresh Kumar 25174f018bc0SViresh Kumar /** 25187d8658efSSaravana Kannan * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP. 25197d8658efSSaravana Kannan * @src_table: OPP table which has @dst_table as one of its required OPP table. 25207d8658efSSaravana Kannan * @dst_table: Required OPP table of the @src_table. 25217d8658efSSaravana Kannan * @src_opp: OPP from the @src_table. 25227d8658efSSaravana Kannan * 25237d8658efSSaravana Kannan * This function returns the OPP (present in @dst_table) pointed out by the 25247d8658efSSaravana Kannan * "required-opps" property of the @src_opp (present in @src_table). 25257d8658efSSaravana Kannan * 25267d8658efSSaravana Kannan * The callers are required to call dev_pm_opp_put() for the returned OPP after 25277d8658efSSaravana Kannan * use. 25287d8658efSSaravana Kannan * 25297d8658efSSaravana Kannan * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise. 25307d8658efSSaravana Kannan */ 25317d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table, 25327d8658efSSaravana Kannan struct opp_table *dst_table, 25337d8658efSSaravana Kannan struct dev_pm_opp *src_opp) 25347d8658efSSaravana Kannan { 25357d8658efSSaravana Kannan struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV); 25367d8658efSSaravana Kannan int i; 25377d8658efSSaravana Kannan 25387d8658efSSaravana Kannan if (!src_table || !dst_table || !src_opp || 25397d8658efSSaravana Kannan !src_table->required_opp_tables) 25407d8658efSSaravana Kannan return ERR_PTR(-EINVAL); 25417d8658efSSaravana Kannan 25427d8658efSSaravana Kannan /* required-opps not fully initialized yet */ 25437d8658efSSaravana Kannan if (lazy_linking_pending(src_table)) 25447d8658efSSaravana Kannan return ERR_PTR(-EBUSY); 25457d8658efSSaravana Kannan 25467d8658efSSaravana Kannan for (i = 0; i < src_table->required_opp_count; i++) { 25477d8658efSSaravana Kannan if (src_table->required_opp_tables[i] == dst_table) { 25487d8658efSSaravana Kannan mutex_lock(&src_table->lock); 25497d8658efSSaravana Kannan 25507d8658efSSaravana Kannan list_for_each_entry(opp, &src_table->opp_list, node) { 25517d8658efSSaravana Kannan if (opp == src_opp) { 25527d8658efSSaravana Kannan dest_opp = opp->required_opps[i]; 25537d8658efSSaravana Kannan dev_pm_opp_get(dest_opp); 25547d8658efSSaravana Kannan break; 25557d8658efSSaravana Kannan } 25567d8658efSSaravana Kannan } 25577d8658efSSaravana Kannan 25587d8658efSSaravana Kannan mutex_unlock(&src_table->lock); 25597d8658efSSaravana Kannan break; 25607d8658efSSaravana Kannan } 25617d8658efSSaravana Kannan } 25627d8658efSSaravana Kannan 25637d8658efSSaravana Kannan if (IS_ERR(dest_opp)) { 25647d8658efSSaravana Kannan pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, 25657d8658efSSaravana Kannan src_table, dst_table); 25667d8658efSSaravana Kannan } 25677d8658efSSaravana Kannan 25687d8658efSSaravana Kannan return dest_opp; 25697d8658efSSaravana Kannan } 25707d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp); 25717d8658efSSaravana Kannan 25727d8658efSSaravana Kannan /** 2573c8a59103SViresh Kumar * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 2574c8a59103SViresh Kumar * @src_table: OPP table which has dst_table as one of its required OPP table. 2575c8a59103SViresh Kumar * @dst_table: Required OPP table of the src_table. 2576c8a59103SViresh Kumar * @pstate: Current performance state of the src_table. 2577c8a59103SViresh Kumar * 2578c8a59103SViresh Kumar * This Returns pstate of the OPP (present in @dst_table) pointed out by the 2579c8a59103SViresh Kumar * "required-opps" property of the OPP (present in @src_table) which has 2580c8a59103SViresh Kumar * performance state set to @pstate. 2581c8a59103SViresh Kumar * 2582c8a59103SViresh Kumar * Return: Zero or positive performance state on success, otherwise negative 2583c8a59103SViresh Kumar * value on errors. 2584c8a59103SViresh Kumar */ 2585c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 2586c8a59103SViresh Kumar struct opp_table *dst_table, 2587c8a59103SViresh Kumar unsigned int pstate) 2588c8a59103SViresh Kumar { 2589c8a59103SViresh Kumar struct dev_pm_opp *opp; 2590c8a59103SViresh Kumar int dest_pstate = -EINVAL; 2591c8a59103SViresh Kumar int i; 2592c8a59103SViresh Kumar 2593c8a59103SViresh Kumar /* 2594c8a59103SViresh Kumar * Normally the src_table will have the "required_opps" property set to 2595c8a59103SViresh Kumar * point to one of the OPPs in the dst_table, but in some cases the 2596c8a59103SViresh Kumar * genpd and its master have one to one mapping of performance states 2597c8a59103SViresh Kumar * and so none of them have the "required-opps" property set. Return the 2598c8a59103SViresh Kumar * pstate of the src_table as it is in such cases. 2599c8a59103SViresh Kumar */ 2600f2f4d2b8SDmitry Osipenko if (!src_table || !src_table->required_opp_count) 2601c8a59103SViresh Kumar return pstate; 2602c8a59103SViresh Kumar 26037eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 26047eba0c76SViresh Kumar if (lazy_linking_pending(src_table)) 26057eba0c76SViresh Kumar return -EBUSY; 26067eba0c76SViresh Kumar 2607c8a59103SViresh Kumar for (i = 0; i < src_table->required_opp_count; i++) { 2608c8a59103SViresh Kumar if (src_table->required_opp_tables[i]->np == dst_table->np) 2609c8a59103SViresh Kumar break; 2610c8a59103SViresh Kumar } 2611c8a59103SViresh Kumar 2612c8a59103SViresh Kumar if (unlikely(i == src_table->required_opp_count)) { 2613c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 2614c8a59103SViresh Kumar __func__, src_table, dst_table); 2615c8a59103SViresh Kumar return -EINVAL; 2616c8a59103SViresh Kumar } 2617c8a59103SViresh Kumar 2618c8a59103SViresh Kumar mutex_lock(&src_table->lock); 2619c8a59103SViresh Kumar 2620c8a59103SViresh Kumar list_for_each_entry(opp, &src_table->opp_list, node) { 2621c8a59103SViresh Kumar if (opp->pstate == pstate) { 2622c8a59103SViresh Kumar dest_pstate = opp->required_opps[i]->pstate; 2623c8a59103SViresh Kumar goto unlock; 2624c8a59103SViresh Kumar } 2625c8a59103SViresh Kumar } 2626c8a59103SViresh Kumar 2627c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 2628c8a59103SViresh Kumar dst_table); 2629c8a59103SViresh Kumar 2630c8a59103SViresh Kumar unlock: 2631c8a59103SViresh Kumar mutex_unlock(&src_table->lock); 2632c8a59103SViresh Kumar 2633c8a59103SViresh Kumar return dest_pstate; 2634c8a59103SViresh Kumar } 2635c8a59103SViresh Kumar 2636c8a59103SViresh Kumar /** 26377813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 26387813dd6fSViresh Kumar * @dev: device for which we do this operation 26397813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 26407813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 26417813dd6fSViresh Kumar * 26427813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 26437813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 26447813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 26457813dd6fSViresh Kumar * 26467813dd6fSViresh Kumar * Return: 26477813dd6fSViresh Kumar * 0 On success OR 26487813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 26497813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 26507813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 26517813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 26527813dd6fSViresh Kumar */ 26537813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 26547813dd6fSViresh Kumar { 26557813dd6fSViresh Kumar struct opp_table *opp_table; 26567813dd6fSViresh Kumar int ret; 26577813dd6fSViresh Kumar 265832439ac7SViresh Kumar opp_table = _add_opp_table(dev, true); 2659dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2660dd461cd9SStephan Gerhold return PTR_ERR(opp_table); 26617813dd6fSViresh Kumar 266246f48acaSViresh Kumar /* Fix regulator count for dynamic OPPs */ 266346f48acaSViresh Kumar opp_table->regulator_count = 1; 266446f48acaSViresh Kumar 26657813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 26660ad8c623SViresh Kumar if (ret) 26677813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 26680ad8c623SViresh Kumar 26697813dd6fSViresh Kumar return ret; 26707813dd6fSViresh Kumar } 26717813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 26727813dd6fSViresh Kumar 26737813dd6fSViresh Kumar /** 26747813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 26757813dd6fSViresh Kumar * @dev: device for which we do this operation 26767813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 26777813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 26787813dd6fSViresh Kumar * 26797813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 26807813dd6fSViresh Kumar * which is isolated here. 26817813dd6fSViresh Kumar * 26827813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 26837813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 26847813dd6fSViresh Kumar * successful. 26857813dd6fSViresh Kumar */ 26867813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 26877813dd6fSViresh Kumar bool availability_req) 26887813dd6fSViresh Kumar { 26897813dd6fSViresh Kumar struct opp_table *opp_table; 26907813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 26917813dd6fSViresh Kumar int r = 0; 26927813dd6fSViresh Kumar 26937813dd6fSViresh Kumar /* Find the opp_table */ 26947813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 26957813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 26967813dd6fSViresh Kumar r = PTR_ERR(opp_table); 26977813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 26987813dd6fSViresh Kumar return r; 26997813dd6fSViresh Kumar } 27007813dd6fSViresh Kumar 27017813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 27027813dd6fSViresh Kumar 27037813dd6fSViresh Kumar /* Do we have the frequency? */ 27047813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 27057813dd6fSViresh Kumar if (tmp_opp->rate == freq) { 27067813dd6fSViresh Kumar opp = tmp_opp; 27077813dd6fSViresh Kumar break; 27087813dd6fSViresh Kumar } 27097813dd6fSViresh Kumar } 27107813dd6fSViresh Kumar 27117813dd6fSViresh Kumar if (IS_ERR(opp)) { 27127813dd6fSViresh Kumar r = PTR_ERR(opp); 27137813dd6fSViresh Kumar goto unlock; 27147813dd6fSViresh Kumar } 27157813dd6fSViresh Kumar 27167813dd6fSViresh Kumar /* Is update really needed? */ 27177813dd6fSViresh Kumar if (opp->available == availability_req) 27187813dd6fSViresh Kumar goto unlock; 27197813dd6fSViresh Kumar 27207813dd6fSViresh Kumar opp->available = availability_req; 27217813dd6fSViresh Kumar 27227813dd6fSViresh Kumar dev_pm_opp_get(opp); 27237813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 27247813dd6fSViresh Kumar 27257813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 27267813dd6fSViresh Kumar if (availability_req) 27277813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 27287813dd6fSViresh Kumar opp); 27297813dd6fSViresh Kumar else 27307813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 27317813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 27327813dd6fSViresh Kumar 27337813dd6fSViresh Kumar dev_pm_opp_put(opp); 27347813dd6fSViresh Kumar goto put_table; 27357813dd6fSViresh Kumar 27367813dd6fSViresh Kumar unlock: 27377813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 27387813dd6fSViresh Kumar put_table: 27397813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 27407813dd6fSViresh Kumar return r; 27417813dd6fSViresh Kumar } 27427813dd6fSViresh Kumar 27437813dd6fSViresh Kumar /** 274425cb20a2SStephen Boyd * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP 274525cb20a2SStephen Boyd * @dev: device for which we do this operation 274625cb20a2SStephen Boyd * @freq: OPP frequency to adjust voltage of 274725cb20a2SStephen Boyd * @u_volt: new OPP target voltage 274825cb20a2SStephen Boyd * @u_volt_min: new OPP min voltage 274925cb20a2SStephen Boyd * @u_volt_max: new OPP max voltage 275025cb20a2SStephen Boyd * 275125cb20a2SStephen Boyd * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 275225cb20a2SStephen Boyd * copy operation, returns 0 if no modifcation was done OR modification was 275325cb20a2SStephen Boyd * successful. 275425cb20a2SStephen Boyd */ 275525cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq, 275625cb20a2SStephen Boyd unsigned long u_volt, unsigned long u_volt_min, 275725cb20a2SStephen Boyd unsigned long u_volt_max) 275825cb20a2SStephen Boyd 275925cb20a2SStephen Boyd { 276025cb20a2SStephen Boyd struct opp_table *opp_table; 276125cb20a2SStephen Boyd struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 276225cb20a2SStephen Boyd int r = 0; 276325cb20a2SStephen Boyd 276425cb20a2SStephen Boyd /* Find the opp_table */ 276525cb20a2SStephen Boyd opp_table = _find_opp_table(dev); 276625cb20a2SStephen Boyd if (IS_ERR(opp_table)) { 276725cb20a2SStephen Boyd r = PTR_ERR(opp_table); 276825cb20a2SStephen Boyd dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 276925cb20a2SStephen Boyd return r; 277025cb20a2SStephen Boyd } 277125cb20a2SStephen Boyd 277225cb20a2SStephen Boyd mutex_lock(&opp_table->lock); 277325cb20a2SStephen Boyd 277425cb20a2SStephen Boyd /* Do we have the frequency? */ 277525cb20a2SStephen Boyd list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 277625cb20a2SStephen Boyd if (tmp_opp->rate == freq) { 277725cb20a2SStephen Boyd opp = tmp_opp; 277825cb20a2SStephen Boyd break; 277925cb20a2SStephen Boyd } 278025cb20a2SStephen Boyd } 278125cb20a2SStephen Boyd 278225cb20a2SStephen Boyd if (IS_ERR(opp)) { 278325cb20a2SStephen Boyd r = PTR_ERR(opp); 278425cb20a2SStephen Boyd goto adjust_unlock; 278525cb20a2SStephen Boyd } 278625cb20a2SStephen Boyd 278725cb20a2SStephen Boyd /* Is update really needed? */ 278825cb20a2SStephen Boyd if (opp->supplies->u_volt == u_volt) 278925cb20a2SStephen Boyd goto adjust_unlock; 279025cb20a2SStephen Boyd 279125cb20a2SStephen Boyd opp->supplies->u_volt = u_volt; 279225cb20a2SStephen Boyd opp->supplies->u_volt_min = u_volt_min; 279325cb20a2SStephen Boyd opp->supplies->u_volt_max = u_volt_max; 279425cb20a2SStephen Boyd 279525cb20a2SStephen Boyd dev_pm_opp_get(opp); 279625cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 279725cb20a2SStephen Boyd 279825cb20a2SStephen Boyd /* Notify the voltage change of the OPP */ 279925cb20a2SStephen Boyd blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE, 280025cb20a2SStephen Boyd opp); 280125cb20a2SStephen Boyd 280225cb20a2SStephen Boyd dev_pm_opp_put(opp); 280325cb20a2SStephen Boyd goto adjust_put_table; 280425cb20a2SStephen Boyd 280525cb20a2SStephen Boyd adjust_unlock: 280625cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 280725cb20a2SStephen Boyd adjust_put_table: 280825cb20a2SStephen Boyd dev_pm_opp_put_opp_table(opp_table); 280925cb20a2SStephen Boyd return r; 281025cb20a2SStephen Boyd } 281103649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage); 281225cb20a2SStephen Boyd 281325cb20a2SStephen Boyd /** 28147813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 28157813dd6fSViresh Kumar * @dev: device for which we do this operation 28167813dd6fSViresh Kumar * @freq: OPP frequency to enable 28177813dd6fSViresh Kumar * 28187813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 28197813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 28207813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 28217813dd6fSViresh Kumar * 28227813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 28237813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 28247813dd6fSViresh Kumar * successful. 28257813dd6fSViresh Kumar */ 28267813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 28277813dd6fSViresh Kumar { 28287813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 28297813dd6fSViresh Kumar } 28307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 28317813dd6fSViresh Kumar 28327813dd6fSViresh Kumar /** 28337813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 28347813dd6fSViresh Kumar * @dev: device for which we do this operation 28357813dd6fSViresh Kumar * @freq: OPP frequency to disable 28367813dd6fSViresh Kumar * 28377813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 28387813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 28397813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 28407813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 28417813dd6fSViresh Kumar * 28427813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 28437813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 28447813dd6fSViresh Kumar * successful. 28457813dd6fSViresh Kumar */ 28467813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 28477813dd6fSViresh Kumar { 28487813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 28497813dd6fSViresh Kumar } 28507813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 28517813dd6fSViresh Kumar 28527813dd6fSViresh Kumar /** 28537813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 28547813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 28557813dd6fSViresh Kumar * @nb: Notifier block to be registered 28567813dd6fSViresh Kumar * 28577813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 28587813dd6fSViresh Kumar */ 28597813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 28607813dd6fSViresh Kumar { 28617813dd6fSViresh Kumar struct opp_table *opp_table; 28627813dd6fSViresh Kumar int ret; 28637813dd6fSViresh Kumar 28647813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 28657813dd6fSViresh Kumar if (IS_ERR(opp_table)) 28667813dd6fSViresh Kumar return PTR_ERR(opp_table); 28677813dd6fSViresh Kumar 28687813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 28697813dd6fSViresh Kumar 28707813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 28717813dd6fSViresh Kumar 28727813dd6fSViresh Kumar return ret; 28737813dd6fSViresh Kumar } 28747813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 28757813dd6fSViresh Kumar 28767813dd6fSViresh Kumar /** 28777813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 28787813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 28797813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 28807813dd6fSViresh Kumar * 28817813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 28827813dd6fSViresh Kumar */ 28837813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 28847813dd6fSViresh Kumar struct notifier_block *nb) 28857813dd6fSViresh Kumar { 28867813dd6fSViresh Kumar struct opp_table *opp_table; 28877813dd6fSViresh Kumar int ret; 28887813dd6fSViresh Kumar 28897813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 28907813dd6fSViresh Kumar if (IS_ERR(opp_table)) 28917813dd6fSViresh Kumar return PTR_ERR(opp_table); 28927813dd6fSViresh Kumar 28937813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 28947813dd6fSViresh Kumar 28957813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 28967813dd6fSViresh Kumar 28977813dd6fSViresh Kumar return ret; 28987813dd6fSViresh Kumar } 28997813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 29007813dd6fSViresh Kumar 29018aaf6264SViresh Kumar /** 29028aaf6264SViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 29038aaf6264SViresh Kumar * @dev: device pointer used to lookup OPP table. 29048aaf6264SViresh Kumar * 29058aaf6264SViresh Kumar * Free both OPPs created using static entries present in DT and the 29068aaf6264SViresh Kumar * dynamically added entries. 29078aaf6264SViresh Kumar */ 29088aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 29097813dd6fSViresh Kumar { 29107813dd6fSViresh Kumar struct opp_table *opp_table; 29117813dd6fSViresh Kumar 29127813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 29137813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 29147813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 29157813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 29167813dd6fSViresh Kumar 29177813dd6fSViresh Kumar if (error != -ENODEV) 29187813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 29197813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 29207813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 29217813dd6fSViresh Kumar error); 29227813dd6fSViresh Kumar return; 29237813dd6fSViresh Kumar } 29247813dd6fSViresh Kumar 2925922ff075SViresh Kumar /* 2926922ff075SViresh Kumar * Drop the extra reference only if the OPP table was successfully added 2927922ff075SViresh Kumar * with dev_pm_opp_of_add_table() earlier. 2928922ff075SViresh Kumar **/ 2929922ff075SViresh Kumar if (_opp_remove_all_static(opp_table)) 2930cdd6ed90SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2931cdd6ed90SViresh Kumar 2932922ff075SViresh Kumar /* Drop reference taken by _find_opp_table() */ 29337813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 29347813dd6fSViresh Kumar } 29357813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 2936ce8073d8SDmitry Osipenko 2937ce8073d8SDmitry Osipenko /** 2938ce8073d8SDmitry Osipenko * dev_pm_opp_sync_regulators() - Sync state of voltage regulators 2939ce8073d8SDmitry Osipenko * @dev: device for which we do this operation 2940ce8073d8SDmitry Osipenko * 2941ce8073d8SDmitry Osipenko * Sync voltage state of the OPP table regulators. 2942ce8073d8SDmitry Osipenko * 2943ce8073d8SDmitry Osipenko * Return: 0 on success or a negative error value. 2944ce8073d8SDmitry Osipenko */ 2945ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev) 2946ce8073d8SDmitry Osipenko { 2947ce8073d8SDmitry Osipenko struct opp_table *opp_table; 2948ce8073d8SDmitry Osipenko struct regulator *reg; 2949ce8073d8SDmitry Osipenko int i, ret = 0; 2950ce8073d8SDmitry Osipenko 2951ce8073d8SDmitry Osipenko /* Device may not have OPP table */ 2952ce8073d8SDmitry Osipenko opp_table = _find_opp_table(dev); 2953ce8073d8SDmitry Osipenko if (IS_ERR(opp_table)) 2954ce8073d8SDmitry Osipenko return 0; 2955ce8073d8SDmitry Osipenko 2956ce8073d8SDmitry Osipenko /* Regulator may not be required for the device */ 2957ce8073d8SDmitry Osipenko if (unlikely(!opp_table->regulators)) 2958ce8073d8SDmitry Osipenko goto put_table; 2959ce8073d8SDmitry Osipenko 2960ce8073d8SDmitry Osipenko /* Nothing to sync if voltage wasn't changed */ 2961ce8073d8SDmitry Osipenko if (!opp_table->enabled) 2962ce8073d8SDmitry Osipenko goto put_table; 2963ce8073d8SDmitry Osipenko 2964ce8073d8SDmitry Osipenko for (i = 0; i < opp_table->regulator_count; i++) { 2965ce8073d8SDmitry Osipenko reg = opp_table->regulators[i]; 2966ce8073d8SDmitry Osipenko ret = regulator_sync_voltage(reg); 2967ce8073d8SDmitry Osipenko if (ret) 2968ce8073d8SDmitry Osipenko break; 2969ce8073d8SDmitry Osipenko } 2970ce8073d8SDmitry Osipenko put_table: 2971ce8073d8SDmitry Osipenko /* Drop reference taken by _find_opp_table() */ 2972ce8073d8SDmitry Osipenko dev_pm_opp_put_opp_table(opp_table); 2973ce8073d8SDmitry Osipenko 2974ce8073d8SDmitry Osipenko return ret; 2975ce8073d8SDmitry Osipenko } 2976ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators); 2977