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 430*aab8ced2SViresh Kumar /* Helpers to read keys */ 431*aab8ced2SViresh Kumar static unsigned long _read_freq(struct dev_pm_opp *opp, int index) 432*aab8ced2SViresh Kumar { 433*aab8ced2SViresh Kumar return opp->rate; 434*aab8ced2SViresh Kumar } 435*aab8ced2SViresh Kumar 436*aab8ced2SViresh Kumar /* Generic comparison helpers */ 437*aab8ced2SViresh Kumar static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 438*aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key) 439*aab8ced2SViresh Kumar { 440*aab8ced2SViresh Kumar if (opp_key == key) { 441*aab8ced2SViresh Kumar *opp = temp_opp; 442*aab8ced2SViresh Kumar return true; 443*aab8ced2SViresh Kumar } 444*aab8ced2SViresh Kumar 445*aab8ced2SViresh Kumar return false; 446*aab8ced2SViresh Kumar } 447*aab8ced2SViresh Kumar 448*aab8ced2SViresh Kumar static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 449*aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key) 450*aab8ced2SViresh Kumar { 451*aab8ced2SViresh Kumar if (opp_key >= key) { 452*aab8ced2SViresh Kumar *opp = temp_opp; 453*aab8ced2SViresh Kumar return true; 454*aab8ced2SViresh Kumar } 455*aab8ced2SViresh Kumar 456*aab8ced2SViresh Kumar return false; 457*aab8ced2SViresh Kumar } 458*aab8ced2SViresh Kumar 459*aab8ced2SViresh Kumar static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 460*aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key) 461*aab8ced2SViresh Kumar { 462*aab8ced2SViresh Kumar if (opp_key > key) 463*aab8ced2SViresh Kumar return true; 464*aab8ced2SViresh Kumar 465*aab8ced2SViresh Kumar *opp = temp_opp; 466*aab8ced2SViresh Kumar return false; 467*aab8ced2SViresh Kumar } 468*aab8ced2SViresh Kumar 469*aab8ced2SViresh Kumar /* Generic key finding helpers */ 470*aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table, 471*aab8ced2SViresh Kumar unsigned long *key, int index, bool available, 472*aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 473*aab8ced2SViresh Kumar bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 474*aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key)) 475*aab8ced2SViresh Kumar { 476*aab8ced2SViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 477*aab8ced2SViresh Kumar 478*aab8ced2SViresh Kumar mutex_lock(&opp_table->lock); 479*aab8ced2SViresh Kumar 480*aab8ced2SViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 481*aab8ced2SViresh Kumar if (temp_opp->available == available) { 482*aab8ced2SViresh Kumar if (compare(&opp, temp_opp, read(temp_opp, index), *key)) 483*aab8ced2SViresh Kumar break; 484*aab8ced2SViresh Kumar } 485*aab8ced2SViresh Kumar } 486*aab8ced2SViresh Kumar 487*aab8ced2SViresh Kumar /* Increment the reference count of OPP */ 488*aab8ced2SViresh Kumar if (!IS_ERR(opp)) { 489*aab8ced2SViresh Kumar *key = read(opp, index); 490*aab8ced2SViresh Kumar dev_pm_opp_get(opp); 491*aab8ced2SViresh Kumar } 492*aab8ced2SViresh Kumar 493*aab8ced2SViresh Kumar mutex_unlock(&opp_table->lock); 494*aab8ced2SViresh Kumar 495*aab8ced2SViresh Kumar return opp; 496*aab8ced2SViresh Kumar } 497*aab8ced2SViresh Kumar 498*aab8ced2SViresh Kumar static struct dev_pm_opp * 499*aab8ced2SViresh Kumar _find_key(struct device *dev, unsigned long *key, int index, bool available, 500*aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 501*aab8ced2SViresh Kumar bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 502*aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key)) 503*aab8ced2SViresh Kumar { 504*aab8ced2SViresh Kumar struct opp_table *opp_table; 505*aab8ced2SViresh Kumar struct dev_pm_opp *opp; 506*aab8ced2SViresh Kumar 507*aab8ced2SViresh Kumar opp_table = _find_opp_table(dev); 508*aab8ced2SViresh Kumar if (IS_ERR(opp_table)) { 509*aab8ced2SViresh Kumar dev_err(dev, "%s: OPP table not found (%ld)\n", __func__, 510*aab8ced2SViresh Kumar PTR_ERR(opp_table)); 511*aab8ced2SViresh Kumar return ERR_CAST(opp_table); 512*aab8ced2SViresh Kumar } 513*aab8ced2SViresh Kumar 514*aab8ced2SViresh Kumar opp = _opp_table_find_key(opp_table, key, index, available, read, 515*aab8ced2SViresh Kumar compare); 516*aab8ced2SViresh Kumar 517*aab8ced2SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 518*aab8ced2SViresh Kumar 519*aab8ced2SViresh Kumar return opp; 520*aab8ced2SViresh Kumar } 521*aab8ced2SViresh Kumar 522*aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_exact(struct device *dev, 523*aab8ced2SViresh Kumar unsigned long key, int index, bool available, 524*aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index)) 525*aab8ced2SViresh Kumar { 526*aab8ced2SViresh Kumar /* 527*aab8ced2SViresh Kumar * The value of key will be updated here, but will be ignored as the 528*aab8ced2SViresh Kumar * caller doesn't need it. 529*aab8ced2SViresh Kumar */ 530*aab8ced2SViresh Kumar return _find_key(dev, &key, index, available, read, _compare_exact); 531*aab8ced2SViresh Kumar } 532*aab8ced2SViresh Kumar 533*aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table, 534*aab8ced2SViresh Kumar unsigned long *key, int index, bool available, 535*aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index)) 536*aab8ced2SViresh Kumar { 537*aab8ced2SViresh Kumar return _opp_table_find_key(opp_table, key, index, available, read, 538*aab8ced2SViresh Kumar _compare_ceil); 539*aab8ced2SViresh Kumar } 540*aab8ced2SViresh Kumar 541*aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key, 542*aab8ced2SViresh Kumar int index, bool available, 543*aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index)) 544*aab8ced2SViresh Kumar { 545*aab8ced2SViresh Kumar return _find_key(dev, key, index, available, read, _compare_ceil); 546*aab8ced2SViresh Kumar } 547*aab8ced2SViresh Kumar 548*aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_floor(struct device *dev, 549*aab8ced2SViresh Kumar unsigned long *key, int index, bool available, 550*aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index)) 551*aab8ced2SViresh Kumar { 552*aab8ced2SViresh Kumar return _find_key(dev, key, index, available, read, _compare_floor); 553*aab8ced2SViresh Kumar } 554*aab8ced2SViresh Kumar 5557813dd6fSViresh Kumar /** 5567813dd6fSViresh Kumar * dev_pm_opp_find_freq_exact() - search for an exact frequency 5577813dd6fSViresh Kumar * @dev: device for which we do this operation 5587813dd6fSViresh Kumar * @freq: frequency to search for 5597813dd6fSViresh Kumar * @available: true/false - match for available opp 5607813dd6fSViresh Kumar * 5617813dd6fSViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 5627813dd6fSViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 5637813dd6fSViresh Kumar * be handled using IS_ERR. Error return values can be: 5647813dd6fSViresh Kumar * EINVAL: for bad pointer 5657813dd6fSViresh Kumar * ERANGE: no match found for search 5667813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 5677813dd6fSViresh Kumar * 5687813dd6fSViresh Kumar * Note: available is a modifier for the search. if available=true, then the 5697813dd6fSViresh Kumar * match is for exact matching frequency and is available in the stored OPP 5707813dd6fSViresh Kumar * table. if false, the match is for exact frequency which is not available. 5717813dd6fSViresh Kumar * 5727813dd6fSViresh Kumar * This provides a mechanism to enable an opp which is not available currently 5737813dd6fSViresh Kumar * or the opposite as well. 5747813dd6fSViresh Kumar * 5757813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 5767813dd6fSViresh Kumar * use. 5777813dd6fSViresh Kumar */ 5787813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 579*aab8ced2SViresh Kumar unsigned long freq, bool available) 5807813dd6fSViresh Kumar { 581*aab8ced2SViresh Kumar return _find_key_exact(dev, freq, 0, available, _read_freq); 5827813dd6fSViresh Kumar } 5837813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 5847813dd6fSViresh Kumar 5857813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 5867813dd6fSViresh Kumar unsigned long *freq) 5877813dd6fSViresh Kumar { 588*aab8ced2SViresh Kumar return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq); 5897813dd6fSViresh Kumar } 5907813dd6fSViresh Kumar 5917813dd6fSViresh Kumar /** 5927813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 5937813dd6fSViresh Kumar * @dev: device for which we do this operation 5947813dd6fSViresh Kumar * @freq: Start frequency 5957813dd6fSViresh Kumar * 5967813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 5977813dd6fSViresh Kumar * for a device. 5987813dd6fSViresh Kumar * 5997813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 6007813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 6017813dd6fSViresh Kumar * values can be: 6027813dd6fSViresh Kumar * EINVAL: for bad pointer 6037813dd6fSViresh Kumar * ERANGE: no match found for search 6047813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 6057813dd6fSViresh Kumar * 6067813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 6077813dd6fSViresh Kumar * use. 6087813dd6fSViresh Kumar */ 6097813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 6107813dd6fSViresh Kumar unsigned long *freq) 6117813dd6fSViresh Kumar { 612*aab8ced2SViresh Kumar return _find_key_ceil(dev, freq, 0, true, _read_freq); 6137813dd6fSViresh Kumar } 6147813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 6157813dd6fSViresh Kumar 6167813dd6fSViresh Kumar /** 6177813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 6187813dd6fSViresh Kumar * @dev: device for which we do this operation 6197813dd6fSViresh Kumar * @freq: Start frequency 6207813dd6fSViresh Kumar * 6217813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 6227813dd6fSViresh Kumar * for a device. 6237813dd6fSViresh Kumar * 6247813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 6257813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 6267813dd6fSViresh Kumar * values can be: 6277813dd6fSViresh Kumar * EINVAL: for bad pointer 6287813dd6fSViresh Kumar * ERANGE: no match found for search 6297813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 6307813dd6fSViresh Kumar * 6317813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 6327813dd6fSViresh Kumar * use. 6337813dd6fSViresh Kumar */ 6347813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 6357813dd6fSViresh Kumar unsigned long *freq) 6367813dd6fSViresh Kumar { 637*aab8ced2SViresh Kumar return _find_key_floor(dev, freq, 0, true, _read_freq); 6387813dd6fSViresh Kumar } 6397813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 6407813dd6fSViresh Kumar 6412f36bde0SAndrew-sh.Cheng /** 64222079af7SViresh Kumar * dev_pm_opp_find_level_exact() - search for an exact level 64322079af7SViresh Kumar * @dev: device for which we do this operation 64422079af7SViresh Kumar * @level: level to search for 64522079af7SViresh Kumar * 64622079af7SViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 64722079af7SViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 64822079af7SViresh Kumar * be handled using IS_ERR. Error return values can be: 64922079af7SViresh Kumar * EINVAL: for bad pointer 65022079af7SViresh Kumar * ERANGE: no match found for search 65122079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 65222079af7SViresh Kumar * 65322079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 65422079af7SViresh Kumar * use. 65522079af7SViresh Kumar */ 65622079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev, 65722079af7SViresh Kumar unsigned int level) 65822079af7SViresh Kumar { 65922079af7SViresh Kumar struct opp_table *opp_table; 66022079af7SViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 66122079af7SViresh Kumar 66222079af7SViresh Kumar opp_table = _find_opp_table(dev); 66322079af7SViresh Kumar if (IS_ERR(opp_table)) { 66422079af7SViresh Kumar int r = PTR_ERR(opp_table); 66522079af7SViresh Kumar 66622079af7SViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 66722079af7SViresh Kumar return ERR_PTR(r); 66822079af7SViresh Kumar } 66922079af7SViresh Kumar 67022079af7SViresh Kumar mutex_lock(&opp_table->lock); 67122079af7SViresh Kumar 67222079af7SViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 67322079af7SViresh Kumar if (temp_opp->level == level) { 67422079af7SViresh Kumar opp = temp_opp; 67522079af7SViresh Kumar 67622079af7SViresh Kumar /* Increment the reference count of OPP */ 67722079af7SViresh Kumar dev_pm_opp_get(opp); 67822079af7SViresh Kumar break; 67922079af7SViresh Kumar } 68022079af7SViresh Kumar } 68122079af7SViresh Kumar 68222079af7SViresh Kumar mutex_unlock(&opp_table->lock); 68322079af7SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 68422079af7SViresh Kumar 68522079af7SViresh Kumar return opp; 68622079af7SViresh Kumar } 68722079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact); 68822079af7SViresh Kumar 68922079af7SViresh Kumar /** 69022079af7SViresh Kumar * dev_pm_opp_find_level_ceil() - search for an rounded up level 69122079af7SViresh Kumar * @dev: device for which we do this operation 69222079af7SViresh Kumar * @level: level to search for 69322079af7SViresh Kumar * 69422079af7SViresh Kumar * Return: Searches for rounded up match in the opp table and returns pointer 69522079af7SViresh Kumar * to the matching opp if found, else returns ERR_PTR in case of error and 69622079af7SViresh Kumar * should be handled using IS_ERR. Error return values can be: 69722079af7SViresh Kumar * EINVAL: for bad pointer 69822079af7SViresh Kumar * ERANGE: no match found for search 69922079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 70022079af7SViresh Kumar * 70122079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 70222079af7SViresh Kumar * use. 70322079af7SViresh Kumar */ 70422079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev, 70522079af7SViresh Kumar unsigned int *level) 70622079af7SViresh Kumar { 70722079af7SViresh Kumar struct opp_table *opp_table; 70822079af7SViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 70922079af7SViresh Kumar 71022079af7SViresh Kumar opp_table = _find_opp_table(dev); 71122079af7SViresh Kumar if (IS_ERR(opp_table)) { 71222079af7SViresh Kumar int r = PTR_ERR(opp_table); 71322079af7SViresh Kumar 71422079af7SViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 71522079af7SViresh Kumar return ERR_PTR(r); 71622079af7SViresh Kumar } 71722079af7SViresh Kumar 71822079af7SViresh Kumar mutex_lock(&opp_table->lock); 71922079af7SViresh Kumar 72022079af7SViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 72122079af7SViresh Kumar if (temp_opp->available && temp_opp->level >= *level) { 72222079af7SViresh Kumar opp = temp_opp; 72322079af7SViresh Kumar *level = opp->level; 72422079af7SViresh Kumar 72522079af7SViresh Kumar /* Increment the reference count of OPP */ 72622079af7SViresh Kumar dev_pm_opp_get(opp); 72722079af7SViresh Kumar break; 72822079af7SViresh Kumar } 72922079af7SViresh Kumar } 73022079af7SViresh Kumar 73122079af7SViresh Kumar mutex_unlock(&opp_table->lock); 73222079af7SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 73322079af7SViresh Kumar 73422079af7SViresh Kumar return opp; 73522079af7SViresh Kumar } 73622079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil); 73722079af7SViresh Kumar 73822079af7SViresh Kumar /** 73900ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth 74000ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 741617df304SYang Li * @bw: start bandwidth 74200ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 74300ce3873SKrzysztof Kozlowski * 74400ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 74500ce3873SKrzysztof Kozlowski * for a device. 74600ce3873SKrzysztof Kozlowski * 74700ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 74800ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 74900ce3873SKrzysztof Kozlowski * values can be: 75000ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 75100ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 75200ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 75300ce3873SKrzysztof Kozlowski * 75400ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 75500ce3873SKrzysztof Kozlowski * use. 75600ce3873SKrzysztof Kozlowski */ 75700ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, 75800ce3873SKrzysztof Kozlowski unsigned int *bw, int index) 75900ce3873SKrzysztof Kozlowski { 76000ce3873SKrzysztof Kozlowski struct opp_table *opp_table; 76100ce3873SKrzysztof Kozlowski struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 76200ce3873SKrzysztof Kozlowski 76300ce3873SKrzysztof Kozlowski if (!dev || !bw) { 76400ce3873SKrzysztof Kozlowski dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw); 76500ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 76600ce3873SKrzysztof Kozlowski } 76700ce3873SKrzysztof Kozlowski 76800ce3873SKrzysztof Kozlowski opp_table = _find_opp_table(dev); 76900ce3873SKrzysztof Kozlowski if (IS_ERR(opp_table)) 77000ce3873SKrzysztof Kozlowski return ERR_CAST(opp_table); 77100ce3873SKrzysztof Kozlowski 77200ce3873SKrzysztof Kozlowski if (index >= opp_table->path_count) 77300ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 77400ce3873SKrzysztof Kozlowski 77500ce3873SKrzysztof Kozlowski mutex_lock(&opp_table->lock); 77600ce3873SKrzysztof Kozlowski 77700ce3873SKrzysztof Kozlowski list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 77800ce3873SKrzysztof Kozlowski if (temp_opp->available && temp_opp->bandwidth) { 77900ce3873SKrzysztof Kozlowski if (temp_opp->bandwidth[index].peak >= *bw) { 78000ce3873SKrzysztof Kozlowski opp = temp_opp; 78100ce3873SKrzysztof Kozlowski *bw = opp->bandwidth[index].peak; 78200ce3873SKrzysztof Kozlowski 78300ce3873SKrzysztof Kozlowski /* Increment the reference count of OPP */ 78400ce3873SKrzysztof Kozlowski dev_pm_opp_get(opp); 78500ce3873SKrzysztof Kozlowski break; 78600ce3873SKrzysztof Kozlowski } 78700ce3873SKrzysztof Kozlowski } 78800ce3873SKrzysztof Kozlowski } 78900ce3873SKrzysztof Kozlowski 79000ce3873SKrzysztof Kozlowski mutex_unlock(&opp_table->lock); 79100ce3873SKrzysztof Kozlowski dev_pm_opp_put_opp_table(opp_table); 79200ce3873SKrzysztof Kozlowski 79300ce3873SKrzysztof Kozlowski return opp; 79400ce3873SKrzysztof Kozlowski } 79500ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil); 79600ce3873SKrzysztof Kozlowski 79700ce3873SKrzysztof Kozlowski /** 79800ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth 79900ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 800617df304SYang Li * @bw: start bandwidth 80100ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 80200ce3873SKrzysztof Kozlowski * 80300ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 80400ce3873SKrzysztof Kozlowski * for a device. 80500ce3873SKrzysztof Kozlowski * 80600ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 80700ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 80800ce3873SKrzysztof Kozlowski * values can be: 80900ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 81000ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 81100ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 81200ce3873SKrzysztof Kozlowski * 81300ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 81400ce3873SKrzysztof Kozlowski * use. 81500ce3873SKrzysztof Kozlowski */ 81600ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev, 81700ce3873SKrzysztof Kozlowski unsigned int *bw, int index) 81800ce3873SKrzysztof Kozlowski { 81900ce3873SKrzysztof Kozlowski struct opp_table *opp_table; 82000ce3873SKrzysztof Kozlowski struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 82100ce3873SKrzysztof Kozlowski 82200ce3873SKrzysztof Kozlowski if (!dev || !bw) { 82300ce3873SKrzysztof Kozlowski dev_err(dev, "%s: Invalid argument bw=%p\n", __func__, bw); 82400ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 82500ce3873SKrzysztof Kozlowski } 82600ce3873SKrzysztof Kozlowski 82700ce3873SKrzysztof Kozlowski opp_table = _find_opp_table(dev); 82800ce3873SKrzysztof Kozlowski if (IS_ERR(opp_table)) 82900ce3873SKrzysztof Kozlowski return ERR_CAST(opp_table); 83000ce3873SKrzysztof Kozlowski 83100ce3873SKrzysztof Kozlowski if (index >= opp_table->path_count) 83200ce3873SKrzysztof Kozlowski return ERR_PTR(-EINVAL); 83300ce3873SKrzysztof Kozlowski 83400ce3873SKrzysztof Kozlowski mutex_lock(&opp_table->lock); 83500ce3873SKrzysztof Kozlowski 83600ce3873SKrzysztof Kozlowski list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 83700ce3873SKrzysztof Kozlowski if (temp_opp->available && temp_opp->bandwidth) { 83800ce3873SKrzysztof Kozlowski /* go to the next node, before choosing prev */ 83900ce3873SKrzysztof Kozlowski if (temp_opp->bandwidth[index].peak > *bw) 84000ce3873SKrzysztof Kozlowski break; 84100ce3873SKrzysztof Kozlowski opp = temp_opp; 84200ce3873SKrzysztof Kozlowski } 84300ce3873SKrzysztof Kozlowski } 84400ce3873SKrzysztof Kozlowski 84500ce3873SKrzysztof Kozlowski /* Increment the reference count of OPP */ 84600ce3873SKrzysztof Kozlowski if (!IS_ERR(opp)) 84700ce3873SKrzysztof Kozlowski dev_pm_opp_get(opp); 84800ce3873SKrzysztof Kozlowski mutex_unlock(&opp_table->lock); 84900ce3873SKrzysztof Kozlowski dev_pm_opp_put_opp_table(opp_table); 85000ce3873SKrzysztof Kozlowski 85100ce3873SKrzysztof Kozlowski if (!IS_ERR(opp)) 85200ce3873SKrzysztof Kozlowski *bw = opp->bandwidth[index].peak; 85300ce3873SKrzysztof Kozlowski 85400ce3873SKrzysztof Kozlowski return opp; 85500ce3873SKrzysztof Kozlowski } 85600ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor); 85700ce3873SKrzysztof Kozlowski 8587813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 8597813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 8607813dd6fSViresh Kumar { 8617813dd6fSViresh Kumar int ret; 8627813dd6fSViresh Kumar 8637813dd6fSViresh Kumar /* Regulator not available for device */ 8647813dd6fSViresh Kumar if (IS_ERR(reg)) { 8657813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 8667813dd6fSViresh Kumar PTR_ERR(reg)); 8677813dd6fSViresh Kumar return 0; 8687813dd6fSViresh Kumar } 8697813dd6fSViresh Kumar 8707813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 8717813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 8727813dd6fSViresh Kumar 8737813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 8747813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 8757813dd6fSViresh Kumar if (ret) 8767813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 8777813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 8787813dd6fSViresh Kumar supply->u_volt_max, ret); 8797813dd6fSViresh Kumar 8807813dd6fSViresh Kumar return ret; 8817813dd6fSViresh Kumar } 8827813dd6fSViresh Kumar 883285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 884285881b5SViresh Kumar unsigned long freq) 8857813dd6fSViresh Kumar { 8867813dd6fSViresh Kumar int ret; 8877813dd6fSViresh Kumar 88835e74b2eSViresh Kumar /* We may reach here for devices which don't change frequency */ 88935e74b2eSViresh Kumar if (IS_ERR(clk)) 89035e74b2eSViresh Kumar return 0; 89135e74b2eSViresh Kumar 8927813dd6fSViresh Kumar ret = clk_set_rate(clk, freq); 8937813dd6fSViresh Kumar if (ret) { 8947813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 8957813dd6fSViresh Kumar ret); 8967813dd6fSViresh Kumar } 8977813dd6fSViresh Kumar 8987813dd6fSViresh Kumar return ret; 8997813dd6fSViresh Kumar } 9007813dd6fSViresh Kumar 901c522ce8aSViresh Kumar static int _opp_config_regulator_single(struct device *dev, 902c522ce8aSViresh Kumar struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp, 903c522ce8aSViresh Kumar struct regulator **regulators, unsigned int count) 9047813dd6fSViresh Kumar { 905c522ce8aSViresh Kumar struct regulator *reg = regulators[0]; 9067813dd6fSViresh Kumar int ret; 9077813dd6fSViresh Kumar 9087813dd6fSViresh Kumar /* This function only supports single regulator per device */ 909c522ce8aSViresh Kumar if (WARN_ON(count > 1)) { 9107813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 9117813dd6fSViresh Kumar return -EINVAL; 9127813dd6fSViresh Kumar } 9137813dd6fSViresh Kumar 914c522ce8aSViresh Kumar ret = _set_opp_voltage(dev, reg, new_opp->supplies); 9157813dd6fSViresh Kumar if (ret) 916c522ce8aSViresh Kumar return ret; 9177813dd6fSViresh Kumar 9188d45719cSKamil Konieczny /* 9198d45719cSKamil Konieczny * Enable the regulator after setting its voltages, otherwise it breaks 9208d45719cSKamil Konieczny * some boot-enabled regulators. 9218d45719cSKamil Konieczny */ 922c522ce8aSViresh Kumar if (unlikely(!new_opp->opp_table->enabled)) { 9238d45719cSKamil Konieczny ret = regulator_enable(reg); 9248d45719cSKamil Konieczny if (ret < 0) 9258d45719cSKamil Konieczny dev_warn(dev, "Failed to enable regulator: %d", ret); 9268d45719cSKamil Konieczny } 9278d45719cSKamil Konieczny 9287813dd6fSViresh Kumar return 0; 9297813dd6fSViresh Kumar } 9307813dd6fSViresh Kumar 931b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table, 932240ae50eSViresh Kumar struct dev_pm_opp *opp, struct device *dev) 933b00e667aSViresh Kumar { 934b00e667aSViresh Kumar u32 avg, peak; 935b00e667aSViresh Kumar int i, ret; 936b00e667aSViresh Kumar 937b00e667aSViresh Kumar if (!opp_table->paths) 938b00e667aSViresh Kumar return 0; 939b00e667aSViresh Kumar 940b00e667aSViresh Kumar for (i = 0; i < opp_table->path_count; i++) { 941240ae50eSViresh Kumar if (!opp) { 942b00e667aSViresh Kumar avg = 0; 943b00e667aSViresh Kumar peak = 0; 944b00e667aSViresh Kumar } else { 945b00e667aSViresh Kumar avg = opp->bandwidth[i].avg; 946b00e667aSViresh Kumar peak = opp->bandwidth[i].peak; 947b00e667aSViresh Kumar } 948b00e667aSViresh Kumar ret = icc_set_bw(opp_table->paths[i], avg, peak); 949b00e667aSViresh Kumar if (ret) { 950b00e667aSViresh Kumar dev_err(dev, "Failed to %s bandwidth[%d]: %d\n", 951240ae50eSViresh Kumar opp ? "set" : "remove", i, ret); 952b00e667aSViresh Kumar return ret; 953b00e667aSViresh Kumar } 954b00e667aSViresh Kumar } 955b00e667aSViresh Kumar 956b00e667aSViresh Kumar return 0; 957b00e667aSViresh Kumar } 958b00e667aSViresh Kumar 95960cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev, 96060cdeae0SStephan Gerhold struct dev_pm_opp *opp, int i) 96160cdeae0SStephan Gerhold { 96260cdeae0SStephan Gerhold unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0; 96360cdeae0SStephan Gerhold int ret; 96460cdeae0SStephan Gerhold 96560cdeae0SStephan Gerhold if (!pd_dev) 96660cdeae0SStephan Gerhold return 0; 96760cdeae0SStephan Gerhold 96860cdeae0SStephan Gerhold ret = dev_pm_genpd_set_performance_state(pd_dev, pstate); 96960cdeae0SStephan Gerhold if (ret) { 9709bfb1fffSViresh Kumar dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", 97160cdeae0SStephan Gerhold dev_name(pd_dev), pstate, ret); 97260cdeae0SStephan Gerhold } 97360cdeae0SStephan Gerhold 97460cdeae0SStephan Gerhold return ret; 97560cdeae0SStephan Gerhold } 97660cdeae0SStephan Gerhold 977ca1b5d77SViresh Kumar /* This is only called for PM domain for now */ 978ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev, 979ca1b5d77SViresh Kumar struct opp_table *opp_table, 9802c59138cSStephan Gerhold struct dev_pm_opp *opp, bool up) 981ca1b5d77SViresh Kumar { 982ca1b5d77SViresh Kumar struct opp_table **required_opp_tables = opp_table->required_opp_tables; 983ca1b5d77SViresh Kumar struct device **genpd_virt_devs = opp_table->genpd_virt_devs; 984ca1b5d77SViresh Kumar int i, ret = 0; 985ca1b5d77SViresh Kumar 986ca1b5d77SViresh Kumar if (!required_opp_tables) 987ca1b5d77SViresh Kumar return 0; 988ca1b5d77SViresh Kumar 98919526d09SMarijn Suijten /* required-opps not fully initialized yet */ 99019526d09SMarijn Suijten if (lazy_linking_pending(opp_table)) 99119526d09SMarijn Suijten return -EBUSY; 99219526d09SMarijn Suijten 9934fa82a87SHsin-Yi Wang /* 9944fa82a87SHsin-Yi Wang * We only support genpd's OPPs in the "required-opps" for now, as we 9954fa82a87SHsin-Yi Wang * don't know much about other use cases. Error out if the required OPP 9964fa82a87SHsin-Yi Wang * doesn't belong to a genpd. 9974fa82a87SHsin-Yi Wang */ 9984fa82a87SHsin-Yi Wang if (unlikely(!required_opp_tables[0]->is_genpd)) { 9994fa82a87SHsin-Yi Wang dev_err(dev, "required-opps don't belong to a genpd\n"); 10004fa82a87SHsin-Yi Wang return -ENOENT; 10014fa82a87SHsin-Yi Wang } 10024fa82a87SHsin-Yi Wang 1003ca1b5d77SViresh Kumar /* Single genpd case */ 100460cdeae0SStephan Gerhold if (!genpd_virt_devs) 100560cdeae0SStephan Gerhold return _set_required_opp(dev, dev, opp, 0); 1006ca1b5d77SViresh Kumar 1007ca1b5d77SViresh Kumar /* Multiple genpd case */ 1008ca1b5d77SViresh Kumar 1009ca1b5d77SViresh Kumar /* 1010ca1b5d77SViresh Kumar * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 1011ca1b5d77SViresh Kumar * after it is freed from another thread. 1012ca1b5d77SViresh Kumar */ 1013ca1b5d77SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 1014ca1b5d77SViresh Kumar 10152c59138cSStephan Gerhold /* Scaling up? Set required OPPs in normal order, else reverse */ 10162c59138cSStephan Gerhold if (up) { 1017ca1b5d77SViresh Kumar for (i = 0; i < opp_table->required_opp_count; i++) { 101860cdeae0SStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 101960cdeae0SStephan Gerhold if (ret) 1020ca1b5d77SViresh Kumar break; 1021ca1b5d77SViresh Kumar } 10222c59138cSStephan Gerhold } else { 10232c59138cSStephan Gerhold for (i = opp_table->required_opp_count - 1; i >= 0; i--) { 10242c59138cSStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 10252c59138cSStephan Gerhold if (ret) 1026ca1b5d77SViresh Kumar break; 1027ca1b5d77SViresh Kumar } 1028ca1b5d77SViresh Kumar } 10292c59138cSStephan Gerhold 1030ca1b5d77SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 1031ca1b5d77SViresh Kumar 1032ca1b5d77SViresh Kumar return ret; 1033ca1b5d77SViresh Kumar } 1034ca1b5d77SViresh Kumar 103581c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table) 103681c4d8a3SViresh Kumar { 103781c4d8a3SViresh Kumar struct dev_pm_opp *opp = ERR_PTR(-ENODEV); 103881c4d8a3SViresh Kumar unsigned long freq; 103981c4d8a3SViresh Kumar 104081c4d8a3SViresh Kumar if (!IS_ERR(opp_table->clk)) { 104181c4d8a3SViresh Kumar freq = clk_get_rate(opp_table->clk); 104281c4d8a3SViresh Kumar opp = _find_freq_ceil(opp_table, &freq); 104381c4d8a3SViresh Kumar } 104481c4d8a3SViresh Kumar 104581c4d8a3SViresh Kumar /* 104681c4d8a3SViresh Kumar * Unable to find the current OPP ? Pick the first from the list since 104781c4d8a3SViresh Kumar * it is in ascending order, otherwise rest of the code will need to 104881c4d8a3SViresh Kumar * make special checks to validate current_opp. 104981c4d8a3SViresh Kumar */ 105081c4d8a3SViresh Kumar if (IS_ERR(opp)) { 105181c4d8a3SViresh Kumar mutex_lock(&opp_table->lock); 105281c4d8a3SViresh Kumar opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node); 105381c4d8a3SViresh Kumar dev_pm_opp_get(opp); 105481c4d8a3SViresh Kumar mutex_unlock(&opp_table->lock); 105581c4d8a3SViresh Kumar } 105681c4d8a3SViresh Kumar 105781c4d8a3SViresh Kumar opp_table->current_opp = opp; 105881c4d8a3SViresh Kumar } 105981c4d8a3SViresh Kumar 10605ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table) 1061f3364e17SViresh Kumar { 1062f3364e17SViresh Kumar int ret; 1063f3364e17SViresh Kumar 1064f3364e17SViresh Kumar if (!opp_table->enabled) 1065f3364e17SViresh Kumar return 0; 1066f3364e17SViresh Kumar 1067f3364e17SViresh Kumar /* 1068f3364e17SViresh Kumar * Some drivers need to support cases where some platforms may 1069f3364e17SViresh Kumar * have OPP table for the device, while others don't and 1070f3364e17SViresh Kumar * opp_set_rate() just needs to behave like clk_set_rate(). 1071f3364e17SViresh Kumar */ 1072f3364e17SViresh Kumar if (!_get_opp_count(opp_table)) 1073f3364e17SViresh Kumar return 0; 1074f3364e17SViresh Kumar 1075240ae50eSViresh Kumar ret = _set_opp_bw(opp_table, NULL, dev); 1076f3364e17SViresh Kumar if (ret) 1077f3364e17SViresh Kumar return ret; 1078f3364e17SViresh Kumar 1079f3364e17SViresh Kumar if (opp_table->regulators) 1080f3364e17SViresh Kumar regulator_disable(opp_table->regulators[0]); 1081f3364e17SViresh Kumar 10822c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, NULL, false); 1083f3364e17SViresh Kumar 1084f3364e17SViresh Kumar opp_table->enabled = false; 1085f3364e17SViresh Kumar return ret; 1086f3364e17SViresh Kumar } 1087f3364e17SViresh Kumar 1088386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table, 1089386ba854SViresh Kumar struct dev_pm_opp *opp, unsigned long freq) 10907813dd6fSViresh Kumar { 1091386ba854SViresh Kumar struct dev_pm_opp *old_opp; 1092f0b88fa4SViresh Kumar int scaling_down, ret; 10937813dd6fSViresh Kumar 1094386ba854SViresh Kumar if (unlikely(!opp)) 1095386ba854SViresh Kumar return _disable_opp_table(dev, opp_table); 1096aca48b61SRajendra Nayak 109781c4d8a3SViresh Kumar /* Find the currently set OPP if we don't know already */ 109881c4d8a3SViresh Kumar if (unlikely(!opp_table->current_opp)) 109981c4d8a3SViresh Kumar _find_current_opp(dev, opp_table); 11007813dd6fSViresh Kumar 110181c4d8a3SViresh Kumar old_opp = opp_table->current_opp; 110281c4d8a3SViresh Kumar 110381c4d8a3SViresh Kumar /* Return early if nothing to do */ 1104de04241aSJonathan Marek if (old_opp == opp && opp_table->current_rate == freq && 1105de04241aSJonathan Marek opp_table->enabled) { 110681c4d8a3SViresh Kumar dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__); 1107386ba854SViresh Kumar return 0; 11087813dd6fSViresh Kumar } 11097813dd6fSViresh Kumar 1110f0b88fa4SViresh Kumar dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n", 1111de04241aSJonathan Marek __func__, opp_table->current_rate, freq, old_opp->level, 1112de04241aSJonathan Marek opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0, 1113f0b88fa4SViresh Kumar opp->bandwidth ? opp->bandwidth[0].peak : 0); 1114f0b88fa4SViresh Kumar 1115f0b88fa4SViresh Kumar scaling_down = _opp_compare_key(old_opp, opp); 1116f0b88fa4SViresh Kumar if (scaling_down == -1) 1117f0b88fa4SViresh Kumar scaling_down = 0; 11187813dd6fSViresh Kumar 1119ca1b5d77SViresh Kumar /* Scaling up? Configure required OPPs before frequency */ 1120f0b88fa4SViresh Kumar if (!scaling_down) { 11212c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, opp, true); 1122870d5d96SViresh Kumar if (ret) { 1123870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1124386ba854SViresh Kumar return ret; 1125ca1b5d77SViresh Kumar } 1126ca1b5d77SViresh Kumar 1127870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1128870d5d96SViresh Kumar if (ret) { 1129870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1130870d5d96SViresh Kumar return ret; 1131870d5d96SViresh Kumar } 1132aee3352fSViresh Kumar 1133aee3352fSViresh Kumar if (opp_table->config_regulators) { 1134aee3352fSViresh Kumar ret = opp_table->config_regulators(dev, old_opp, opp, 1135aee3352fSViresh Kumar opp_table->regulators, 1136aee3352fSViresh Kumar opp_table->regulator_count); 1137aee3352fSViresh Kumar if (ret) { 1138aee3352fSViresh Kumar dev_err(dev, "Failed to set regulator voltages: %d\n", 1139aee3352fSViresh Kumar ret); 1140aee3352fSViresh Kumar return ret; 1141aee3352fSViresh Kumar } 1142aee3352fSViresh Kumar } 1143870d5d96SViresh Kumar } 1144870d5d96SViresh Kumar 11451d3c42caSViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 1146ca1b5d77SViresh Kumar if (ret) 1147870d5d96SViresh Kumar return ret; 1148870d5d96SViresh Kumar 1149870d5d96SViresh Kumar /* Scaling down? Configure required OPPs after frequency */ 1150870d5d96SViresh Kumar if (scaling_down) { 1151aee3352fSViresh Kumar if (opp_table->config_regulators) { 1152aee3352fSViresh Kumar ret = opp_table->config_regulators(dev, old_opp, opp, 1153aee3352fSViresh Kumar opp_table->regulators, 1154aee3352fSViresh Kumar opp_table->regulator_count); 1155aee3352fSViresh Kumar if (ret) { 1156aee3352fSViresh Kumar dev_err(dev, "Failed to set regulator voltages: %d\n", 1157aee3352fSViresh Kumar ret); 1158aee3352fSViresh Kumar return ret; 1159aee3352fSViresh Kumar } 1160aee3352fSViresh Kumar } 1161aee3352fSViresh Kumar 1162870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1163870d5d96SViresh Kumar if (ret) { 1164870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1165870d5d96SViresh Kumar return ret; 1166ca1b5d77SViresh Kumar } 1167ca1b5d77SViresh Kumar 1168870d5d96SViresh Kumar ret = _set_required_opps(dev, opp_table, opp, false); 1169870d5d96SViresh Kumar if (ret) { 1170870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1171870d5d96SViresh Kumar return ret; 1172870d5d96SViresh Kumar } 1173870d5d96SViresh Kumar } 1174870d5d96SViresh Kumar 117572f80ce4SViresh Kumar opp_table->enabled = true; 117681c4d8a3SViresh Kumar dev_pm_opp_put(old_opp); 117781c4d8a3SViresh Kumar 117881c4d8a3SViresh Kumar /* Make sure current_opp doesn't get freed */ 117981c4d8a3SViresh Kumar dev_pm_opp_get(opp); 118081c4d8a3SViresh Kumar opp_table->current_opp = opp; 1181de04241aSJonathan Marek opp_table->current_rate = freq; 1182fe2af402SGeorgi Djakov 1183386ba854SViresh Kumar return ret; 1184386ba854SViresh Kumar } 1185386ba854SViresh Kumar 1186386ba854SViresh Kumar /** 1187386ba854SViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 1188386ba854SViresh Kumar * @dev: device for which we do this operation 1189386ba854SViresh Kumar * @target_freq: frequency to achieve 1190386ba854SViresh Kumar * 1191386ba854SViresh Kumar * This configures the power-supplies to the levels specified by the OPP 1192386ba854SViresh Kumar * corresponding to the target_freq, and programs the clock to a value <= 1193386ba854SViresh Kumar * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax 1194386ba854SViresh Kumar * provided by the opp, should have already rounded to the target OPP's 1195386ba854SViresh Kumar * frequency. 1196386ba854SViresh Kumar */ 1197386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 1198386ba854SViresh Kumar { 1199386ba854SViresh Kumar struct opp_table *opp_table; 1200386ba854SViresh Kumar unsigned long freq = 0, temp_freq; 1201386ba854SViresh Kumar struct dev_pm_opp *opp = NULL; 1202386ba854SViresh Kumar int ret; 1203386ba854SViresh Kumar 1204386ba854SViresh Kumar opp_table = _find_opp_table(dev); 1205386ba854SViresh Kumar if (IS_ERR(opp_table)) { 1206386ba854SViresh Kumar dev_err(dev, "%s: device's opp table doesn't exist\n", __func__); 1207386ba854SViresh Kumar return PTR_ERR(opp_table); 1208386ba854SViresh Kumar } 1209386ba854SViresh Kumar 1210386ba854SViresh Kumar if (target_freq) { 1211386ba854SViresh Kumar /* 1212386ba854SViresh Kumar * For IO devices which require an OPP on some platforms/SoCs 1213386ba854SViresh Kumar * while just needing to scale the clock on some others 1214386ba854SViresh Kumar * we look for empty OPP tables with just a clock handle and 1215386ba854SViresh Kumar * scale only the clk. This makes dev_pm_opp_set_rate() 1216386ba854SViresh Kumar * equivalent to a clk_set_rate() 1217386ba854SViresh Kumar */ 1218386ba854SViresh Kumar if (!_get_opp_count(opp_table)) { 1219386ba854SViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq); 1220386ba854SViresh Kumar goto put_opp_table; 1221386ba854SViresh Kumar } 1222386ba854SViresh Kumar 1223386ba854SViresh Kumar freq = clk_round_rate(opp_table->clk, target_freq); 1224386ba854SViresh Kumar if ((long)freq <= 0) 1225386ba854SViresh Kumar freq = target_freq; 1226386ba854SViresh Kumar 1227386ba854SViresh Kumar /* 1228386ba854SViresh Kumar * The clock driver may support finer resolution of the 1229386ba854SViresh Kumar * frequencies than the OPP table, don't update the frequency we 1230386ba854SViresh Kumar * pass to clk_set_rate() here. 1231386ba854SViresh Kumar */ 1232386ba854SViresh Kumar temp_freq = freq; 1233386ba854SViresh Kumar opp = _find_freq_ceil(opp_table, &temp_freq); 1234386ba854SViresh Kumar if (IS_ERR(opp)) { 1235386ba854SViresh Kumar ret = PTR_ERR(opp); 1236386ba854SViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 1237386ba854SViresh Kumar __func__, freq, ret); 1238386ba854SViresh Kumar goto put_opp_table; 1239386ba854SViresh Kumar } 1240386ba854SViresh Kumar } 1241386ba854SViresh Kumar 1242386ba854SViresh Kumar ret = _set_opp(dev, opp_table, opp, freq); 1243386ba854SViresh Kumar 1244386ba854SViresh Kumar if (target_freq) 12457813dd6fSViresh Kumar dev_pm_opp_put(opp); 12467813dd6fSViresh Kumar put_opp_table: 12477813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 12487813dd6fSViresh Kumar return ret; 12497813dd6fSViresh Kumar } 12507813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 12517813dd6fSViresh Kumar 1252abbe3483SViresh Kumar /** 1253abbe3483SViresh Kumar * dev_pm_opp_set_opp() - Configure device for OPP 1254abbe3483SViresh Kumar * @dev: device for which we do this operation 1255abbe3483SViresh Kumar * @opp: OPP to set to 1256abbe3483SViresh Kumar * 1257abbe3483SViresh Kumar * This configures the device based on the properties of the OPP passed to this 1258abbe3483SViresh Kumar * routine. 1259abbe3483SViresh Kumar * 1260abbe3483SViresh Kumar * Return: 0 on success, a negative error number otherwise. 1261abbe3483SViresh Kumar */ 1262abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp) 1263abbe3483SViresh Kumar { 1264abbe3483SViresh Kumar struct opp_table *opp_table; 1265abbe3483SViresh Kumar int ret; 1266abbe3483SViresh Kumar 1267abbe3483SViresh Kumar opp_table = _find_opp_table(dev); 1268abbe3483SViresh Kumar if (IS_ERR(opp_table)) { 1269abbe3483SViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 1270abbe3483SViresh Kumar return PTR_ERR(opp_table); 1271abbe3483SViresh Kumar } 1272abbe3483SViresh Kumar 1273abbe3483SViresh Kumar ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0); 1274abbe3483SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1275abbe3483SViresh Kumar 1276abbe3483SViresh Kumar return ret; 1277abbe3483SViresh Kumar } 1278abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp); 1279abbe3483SViresh Kumar 12807813dd6fSViresh Kumar /* OPP-dev Helpers */ 12817813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 12827813dd6fSViresh Kumar struct opp_table *opp_table) 12837813dd6fSViresh Kumar { 12847813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 12857813dd6fSViresh Kumar list_del(&opp_dev->node); 12867813dd6fSViresh Kumar kfree(opp_dev); 12877813dd6fSViresh Kumar } 12887813dd6fSViresh Kumar 1289ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 12907813dd6fSViresh Kumar struct opp_table *opp_table) 12917813dd6fSViresh Kumar { 12927813dd6fSViresh Kumar struct opp_device *opp_dev; 12937813dd6fSViresh Kumar 12947813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 12957813dd6fSViresh Kumar if (!opp_dev) 12967813dd6fSViresh Kumar return NULL; 12977813dd6fSViresh Kumar 12987813dd6fSViresh Kumar /* Initialize opp-dev */ 12997813dd6fSViresh Kumar opp_dev->dev = dev; 13003d255699SViresh Kumar 1301ef43f01aSViresh Kumar mutex_lock(&opp_table->lock); 13027813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 1303ef43f01aSViresh Kumar mutex_unlock(&opp_table->lock); 13047813dd6fSViresh Kumar 13057813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 1306a2dea4cbSGreg Kroah-Hartman opp_debug_register(opp_dev, opp_table); 1307283d55e6SViresh Kumar 1308283d55e6SViresh Kumar return opp_dev; 1309283d55e6SViresh Kumar } 1310283d55e6SViresh Kumar 1311eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index) 13127813dd6fSViresh Kumar { 13137813dd6fSViresh Kumar struct opp_table *opp_table; 13147813dd6fSViresh Kumar struct opp_device *opp_dev; 13157813dd6fSViresh Kumar int ret; 13167813dd6fSViresh Kumar 13177813dd6fSViresh Kumar /* 13187813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 13197813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 13207813dd6fSViresh Kumar */ 13217813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 13227813dd6fSViresh Kumar if (!opp_table) 1323dd461cd9SStephan Gerhold return ERR_PTR(-ENOMEM); 13247813dd6fSViresh Kumar 13253d255699SViresh Kumar mutex_init(&opp_table->lock); 13264f018bc0SViresh Kumar mutex_init(&opp_table->genpd_virt_dev_lock); 13277813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 13287eba0c76SViresh Kumar INIT_LIST_HEAD(&opp_table->lazy); 13297813dd6fSViresh Kumar 133046f48acaSViresh Kumar /* Mark regulator count uninitialized */ 133146f48acaSViresh Kumar opp_table->regulator_count = -1; 133246f48acaSViresh Kumar 13337813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 13347813dd6fSViresh Kumar if (!opp_dev) { 1335dd461cd9SStephan Gerhold ret = -ENOMEM; 1336dd461cd9SStephan Gerhold goto err; 13377813dd6fSViresh Kumar } 13387813dd6fSViresh Kumar 1339eb7c8743SViresh Kumar _of_init_opp_table(opp_table, dev, index); 13407813dd6fSViresh Kumar 13416d3f922cSGeorgi Djakov /* Find interconnect path(s) for the device */ 13426d3f922cSGeorgi Djakov ret = dev_pm_opp_of_find_icc_paths(dev, opp_table); 1343dd461cd9SStephan Gerhold if (ret) { 1344dd461cd9SStephan Gerhold if (ret == -EPROBE_DEFER) 134532439ac7SViresh Kumar goto remove_opp_dev; 1346dd461cd9SStephan Gerhold 13476d3f922cSGeorgi Djakov dev_warn(dev, "%s: Error finding interconnect paths: %d\n", 13486d3f922cSGeorgi Djakov __func__, ret); 1349dd461cd9SStephan Gerhold } 13506d3f922cSGeorgi Djakov 13517813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 13527813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 13537813dd6fSViresh Kumar kref_init(&opp_table->kref); 13547813dd6fSViresh Kumar 13557813dd6fSViresh Kumar return opp_table; 1356dd461cd9SStephan Gerhold 1357976509bbSQuanyang Wang remove_opp_dev: 1358976509bbSQuanyang Wang _remove_opp_dev(opp_dev, opp_table); 1359dd461cd9SStephan Gerhold err: 1360dd461cd9SStephan Gerhold kfree(opp_table); 1361dd461cd9SStephan Gerhold return ERR_PTR(ret); 13627813dd6fSViresh Kumar } 13637813dd6fSViresh Kumar 13647813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 13657813dd6fSViresh Kumar { 13667813dd6fSViresh Kumar kref_get(&opp_table->kref); 13677813dd6fSViresh Kumar } 13687813dd6fSViresh Kumar 136932439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev, 137032439ac7SViresh Kumar struct opp_table *opp_table, 137132439ac7SViresh Kumar bool getclk) 137232439ac7SViresh Kumar { 1373d4a4c7a4SViresh Kumar int ret; 1374d4a4c7a4SViresh Kumar 137532439ac7SViresh Kumar /* 137632439ac7SViresh Kumar * Return early if we don't need to get clk or we have already tried it 137732439ac7SViresh Kumar * earlier. 137832439ac7SViresh Kumar */ 137932439ac7SViresh Kumar if (!getclk || IS_ERR(opp_table) || opp_table->clk) 138032439ac7SViresh Kumar return opp_table; 138132439ac7SViresh Kumar 138232439ac7SViresh Kumar /* Find clk for the device */ 138332439ac7SViresh Kumar opp_table->clk = clk_get(dev, NULL); 138432439ac7SViresh Kumar 1385d4a4c7a4SViresh Kumar ret = PTR_ERR_OR_ZERO(opp_table->clk); 1386d4a4c7a4SViresh Kumar if (!ret) 138732439ac7SViresh Kumar return opp_table; 1388d4a4c7a4SViresh Kumar 1389d4a4c7a4SViresh Kumar if (ret == -ENOENT) { 1390d4a4c7a4SViresh Kumar dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret); 1391d4a4c7a4SViresh Kumar return opp_table; 1392d4a4c7a4SViresh Kumar } 1393d4a4c7a4SViresh Kumar 1394d4a4c7a4SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1395d4a4c7a4SViresh Kumar dev_err_probe(dev, ret, "Couldn't find clock\n"); 1396d4a4c7a4SViresh Kumar 1397d4a4c7a4SViresh Kumar return ERR_PTR(ret); 139832439ac7SViresh Kumar } 139932439ac7SViresh Kumar 140027c09484SViresh Kumar /* 140127c09484SViresh Kumar * We need to make sure that the OPP table for a device doesn't get added twice, 140227c09484SViresh Kumar * if this routine gets called in parallel with the same device pointer. 140327c09484SViresh Kumar * 140427c09484SViresh Kumar * The simplest way to enforce that is to perform everything (find existing 140527c09484SViresh Kumar * table and if not found, create a new one) under the opp_table_lock, so only 140627c09484SViresh Kumar * one creator gets access to the same. But that expands the critical section 140727c09484SViresh Kumar * under the lock and may end up causing circular dependencies with frameworks 140827c09484SViresh Kumar * like debugfs, interconnect or clock framework as they may be direct or 140927c09484SViresh Kumar * indirect users of OPP core. 141027c09484SViresh Kumar * 141127c09484SViresh Kumar * And for that reason we have to go for a bit tricky implementation here, which 141227c09484SViresh Kumar * uses the opp_tables_busy flag to indicate if another creator is in the middle 141327c09484SViresh Kumar * of adding an OPP table and others should wait for it to finish. 141427c09484SViresh Kumar */ 141532439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index, 141632439ac7SViresh Kumar bool getclk) 14177813dd6fSViresh Kumar { 14187813dd6fSViresh Kumar struct opp_table *opp_table; 14197813dd6fSViresh Kumar 142027c09484SViresh Kumar again: 14217813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 14227813dd6fSViresh Kumar 14237813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 14247813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 14257813dd6fSViresh Kumar goto unlock; 14267813dd6fSViresh Kumar 142727c09484SViresh Kumar /* 142827c09484SViresh Kumar * The opp_tables list or an OPP table's dev_list is getting updated by 142927c09484SViresh Kumar * another user, wait for it to finish. 143027c09484SViresh Kumar */ 143127c09484SViresh Kumar if (unlikely(opp_tables_busy)) { 143227c09484SViresh Kumar mutex_unlock(&opp_table_lock); 143327c09484SViresh Kumar cpu_relax(); 143427c09484SViresh Kumar goto again; 143527c09484SViresh Kumar } 143627c09484SViresh Kumar 143727c09484SViresh Kumar opp_tables_busy = true; 1438283d55e6SViresh Kumar opp_table = _managed_opp(dev, index); 143927c09484SViresh Kumar 144027c09484SViresh Kumar /* Drop the lock to reduce the size of critical section */ 144127c09484SViresh Kumar mutex_unlock(&opp_table_lock); 144227c09484SViresh Kumar 1443283d55e6SViresh Kumar if (opp_table) { 1444ef43f01aSViresh Kumar if (!_add_opp_dev(dev, opp_table)) { 1445283d55e6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1446dd461cd9SStephan Gerhold opp_table = ERR_PTR(-ENOMEM); 1447283d55e6SViresh Kumar } 144827c09484SViresh Kumar 144927c09484SViresh Kumar mutex_lock(&opp_table_lock); 145027c09484SViresh Kumar } else { 145127c09484SViresh Kumar opp_table = _allocate_opp_table(dev, index); 145227c09484SViresh Kumar 145327c09484SViresh Kumar mutex_lock(&opp_table_lock); 145427c09484SViresh Kumar if (!IS_ERR(opp_table)) 145527c09484SViresh Kumar list_add(&opp_table->node, &opp_tables); 1456283d55e6SViresh Kumar } 1457283d55e6SViresh Kumar 145827c09484SViresh Kumar opp_tables_busy = false; 14597813dd6fSViresh Kumar 14607813dd6fSViresh Kumar unlock: 14617813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 14627813dd6fSViresh Kumar 146332439ac7SViresh Kumar return _update_opp_table_clk(dev, opp_table, getclk); 14647813dd6fSViresh Kumar } 1465eb7c8743SViresh Kumar 146632439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk) 1467e77dcb0bSViresh Kumar { 146832439ac7SViresh Kumar return _add_opp_table_indexed(dev, 0, getclk); 1469e77dcb0bSViresh Kumar } 1470e77dcb0bSViresh Kumar 1471eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 1472eb7c8743SViresh Kumar { 1473e77dcb0bSViresh Kumar return _find_opp_table(dev); 1474eb7c8743SViresh Kumar } 14757813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 14767813dd6fSViresh Kumar 14777813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 14787813dd6fSViresh Kumar { 14797813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 1480cdd6ed90SViresh Kumar struct opp_device *opp_dev, *temp; 14816d3f922cSGeorgi Djakov int i; 14827813dd6fSViresh Kumar 1483e0df59deSViresh Kumar /* Drop the lock as soon as we can */ 1484e0df59deSViresh Kumar list_del(&opp_table->node); 1485e0df59deSViresh Kumar mutex_unlock(&opp_table_lock); 1486e0df59deSViresh Kumar 148781c4d8a3SViresh Kumar if (opp_table->current_opp) 148881c4d8a3SViresh Kumar dev_pm_opp_put(opp_table->current_opp); 148981c4d8a3SViresh Kumar 14905d6d106fSViresh Kumar _of_clear_opp_table(opp_table); 14915d6d106fSViresh Kumar 14927813dd6fSViresh Kumar /* Release clk */ 14937813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 14947813dd6fSViresh Kumar clk_put(opp_table->clk); 14957813dd6fSViresh Kumar 14966d3f922cSGeorgi Djakov if (opp_table->paths) { 14976d3f922cSGeorgi Djakov for (i = 0; i < opp_table->path_count; i++) 14986d3f922cSGeorgi Djakov icc_put(opp_table->paths[i]); 14996d3f922cSGeorgi Djakov kfree(opp_table->paths); 15006d3f922cSGeorgi Djakov } 15016d3f922cSGeorgi Djakov 1502cdd6ed90SViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 1503cdd6ed90SViresh Kumar 1504cdd6ed90SViresh Kumar list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 15053d255699SViresh Kumar /* 1506cdd6ed90SViresh Kumar * The OPP table is getting removed, drop the performance state 1507cdd6ed90SViresh Kumar * constraints. 15083d255699SViresh Kumar */ 1509cdd6ed90SViresh Kumar if (opp_table->genpd_performance_state) 1510cdd6ed90SViresh Kumar dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 15117813dd6fSViresh Kumar 15127813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 1513cdd6ed90SViresh Kumar } 15147813dd6fSViresh Kumar 15154f018bc0SViresh Kumar mutex_destroy(&opp_table->genpd_virt_dev_lock); 15167813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 15177813dd6fSViresh Kumar kfree(opp_table); 15187813dd6fSViresh Kumar } 15197813dd6fSViresh Kumar 15207813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 15217813dd6fSViresh Kumar { 15227813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 15237813dd6fSViresh Kumar &opp_table_lock); 15247813dd6fSViresh Kumar } 15257813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 15267813dd6fSViresh Kumar 15277813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 15287813dd6fSViresh Kumar { 15297813dd6fSViresh Kumar kfree(opp); 15307813dd6fSViresh Kumar } 15317813dd6fSViresh Kumar 1532cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref) 15337813dd6fSViresh Kumar { 1534cf1fac94SViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 1535cf1fac94SViresh Kumar struct opp_table *opp_table = opp->opp_table; 1536cf1fac94SViresh Kumar 1537cf1fac94SViresh Kumar list_del(&opp->node); 1538cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1539cf1fac94SViresh Kumar 15407813dd6fSViresh Kumar /* 15417813dd6fSViresh Kumar * Notify the changes in the availability of the operable 15427813dd6fSViresh Kumar * frequency/voltage list. 15437813dd6fSViresh Kumar */ 15447813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 1545da544b61SViresh Kumar _of_opp_free_required_opps(opp_table, opp); 15467813dd6fSViresh Kumar opp_debug_remove_one(opp); 15477813dd6fSViresh Kumar kfree(opp); 15481690d8bbSViresh Kumar } 15497813dd6fSViresh Kumar 1550a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 15517813dd6fSViresh Kumar { 15527813dd6fSViresh Kumar kref_get(&opp->kref); 15537813dd6fSViresh Kumar } 15547813dd6fSViresh Kumar 15557813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 15567813dd6fSViresh Kumar { 1557cf1fac94SViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 15587813dd6fSViresh Kumar } 15597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 15607813dd6fSViresh Kumar 15617813dd6fSViresh Kumar /** 15627813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 15637813dd6fSViresh Kumar * @dev: device for which we do this operation 15647813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 15657813dd6fSViresh Kumar * 15667813dd6fSViresh Kumar * This function removes an opp from the opp table. 15677813dd6fSViresh Kumar */ 15687813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 15697813dd6fSViresh Kumar { 157095073b72SJakob Koschel struct dev_pm_opp *opp = NULL, *iter; 15717813dd6fSViresh Kumar struct opp_table *opp_table; 15727813dd6fSViresh Kumar 15737813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 15747813dd6fSViresh Kumar if (IS_ERR(opp_table)) 15757813dd6fSViresh Kumar return; 15767813dd6fSViresh Kumar 15777813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 15787813dd6fSViresh Kumar 157995073b72SJakob Koschel list_for_each_entry(iter, &opp_table->opp_list, node) { 158095073b72SJakob Koschel if (iter->rate == freq) { 158195073b72SJakob Koschel opp = iter; 15827813dd6fSViresh Kumar break; 15837813dd6fSViresh Kumar } 15847813dd6fSViresh Kumar } 15857813dd6fSViresh Kumar 15867813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 15877813dd6fSViresh Kumar 158895073b72SJakob Koschel if (opp) { 15897813dd6fSViresh Kumar dev_pm_opp_put(opp); 15900ad8c623SViresh Kumar 15910ad8c623SViresh Kumar /* Drop the reference taken by dev_pm_opp_add() */ 15920ad8c623SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15937813dd6fSViresh Kumar } else { 15947813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 15957813dd6fSViresh Kumar __func__, freq); 15967813dd6fSViresh Kumar } 15977813dd6fSViresh Kumar 15980ad8c623SViresh Kumar /* Drop the reference taken by _find_opp_table() */ 15997813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16007813dd6fSViresh Kumar } 16017813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 16027813dd6fSViresh Kumar 1603cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table, 1604cf1fac94SViresh Kumar bool dynamic) 1605cf1fac94SViresh Kumar { 1606cf1fac94SViresh Kumar struct dev_pm_opp *opp = NULL, *temp; 1607cf1fac94SViresh Kumar 1608cf1fac94SViresh Kumar mutex_lock(&opp_table->lock); 1609cf1fac94SViresh Kumar list_for_each_entry(temp, &opp_table->opp_list, node) { 1610606a5d42SBeata Michalska /* 1611606a5d42SBeata Michalska * Refcount must be dropped only once for each OPP by OPP core, 1612606a5d42SBeata Michalska * do that with help of "removed" flag. 1613606a5d42SBeata Michalska */ 1614606a5d42SBeata Michalska if (!temp->removed && dynamic == temp->dynamic) { 1615cf1fac94SViresh Kumar opp = temp; 1616cf1fac94SViresh Kumar break; 1617cf1fac94SViresh Kumar } 1618cf1fac94SViresh Kumar } 1619cf1fac94SViresh Kumar 1620cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1621cf1fac94SViresh Kumar return opp; 1622cf1fac94SViresh Kumar } 1623cf1fac94SViresh Kumar 1624606a5d42SBeata Michalska /* 1625606a5d42SBeata Michalska * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to 1626606a5d42SBeata Michalska * happen lock less to avoid circular dependency issues. This routine must be 1627606a5d42SBeata Michalska * called without the opp_table->lock held. 1628606a5d42SBeata Michalska */ 1629606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic) 163003758d60SViresh Kumar { 1631cf1fac94SViresh Kumar struct dev_pm_opp *opp; 163203758d60SViresh Kumar 1633606a5d42SBeata Michalska while ((opp = _opp_get_next(opp_table, dynamic))) { 1634606a5d42SBeata Michalska opp->removed = true; 1635606a5d42SBeata Michalska dev_pm_opp_put(opp); 1636606a5d42SBeata Michalska 1637606a5d42SBeata Michalska /* Drop the references taken by dev_pm_opp_add() */ 1638606a5d42SBeata Michalska if (dynamic) 1639606a5d42SBeata Michalska dev_pm_opp_put_opp_table(opp_table); 1640606a5d42SBeata Michalska } 1641606a5d42SBeata Michalska } 1642606a5d42SBeata Michalska 1643606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table) 1644606a5d42SBeata Michalska { 164503758d60SViresh Kumar mutex_lock(&opp_table->lock); 164603758d60SViresh Kumar 1647922ff075SViresh Kumar if (!opp_table->parsed_static_opps) { 1648cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1649cf1fac94SViresh Kumar return false; 1650922ff075SViresh Kumar } 1651922ff075SViresh Kumar 1652cf1fac94SViresh Kumar if (--opp_table->parsed_static_opps) { 1653cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1654cf1fac94SViresh Kumar return true; 165503758d60SViresh Kumar } 165603758d60SViresh Kumar 165703758d60SViresh Kumar mutex_unlock(&opp_table->lock); 1658922ff075SViresh Kumar 1659606a5d42SBeata Michalska _opp_remove_all(opp_table, false); 1660cf1fac94SViresh Kumar return true; 166103758d60SViresh Kumar } 166203758d60SViresh Kumar 16631690d8bbSViresh Kumar /** 16641690d8bbSViresh Kumar * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 16651690d8bbSViresh Kumar * @dev: device for which we do this operation 16661690d8bbSViresh Kumar * 16671690d8bbSViresh Kumar * This function removes all dynamically created OPPs from the opp table. 16681690d8bbSViresh Kumar */ 16691690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev) 16701690d8bbSViresh Kumar { 16711690d8bbSViresh Kumar struct opp_table *opp_table; 16721690d8bbSViresh Kumar 16731690d8bbSViresh Kumar opp_table = _find_opp_table(dev); 16741690d8bbSViresh Kumar if (IS_ERR(opp_table)) 16751690d8bbSViresh Kumar return; 16761690d8bbSViresh Kumar 1677606a5d42SBeata Michalska _opp_remove_all(opp_table, true); 16781690d8bbSViresh Kumar 16791690d8bbSViresh Kumar /* Drop the reference taken by _find_opp_table() */ 16801690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16811690d8bbSViresh Kumar } 16821690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 16831690d8bbSViresh Kumar 16847813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table) 16857813dd6fSViresh Kumar { 16867813dd6fSViresh Kumar struct dev_pm_opp *opp; 16876d3f922cSGeorgi Djakov int supply_count, supply_size, icc_size; 16887813dd6fSViresh Kumar 16897813dd6fSViresh Kumar /* Allocate space for at least one supply */ 16906d3f922cSGeorgi Djakov supply_count = table->regulator_count > 0 ? table->regulator_count : 1; 16916d3f922cSGeorgi Djakov supply_size = sizeof(*opp->supplies) * supply_count; 16926d3f922cSGeorgi Djakov icc_size = sizeof(*opp->bandwidth) * table->path_count; 16937813dd6fSViresh Kumar 16947813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 16956d3f922cSGeorgi Djakov opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL); 16966d3f922cSGeorgi Djakov 16977813dd6fSViresh Kumar if (!opp) 16987813dd6fSViresh Kumar return NULL; 16997813dd6fSViresh Kumar 17007813dd6fSViresh Kumar /* Put the supplies at the end of the OPP structure as an empty array */ 17017813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 17026d3f922cSGeorgi Djakov if (icc_size) 17036d3f922cSGeorgi Djakov opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count); 17047813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 17057813dd6fSViresh Kumar 17067813dd6fSViresh Kumar return opp; 17077813dd6fSViresh Kumar } 17087813dd6fSViresh Kumar 17097813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 17107813dd6fSViresh Kumar struct opp_table *opp_table) 17117813dd6fSViresh Kumar { 17127813dd6fSViresh Kumar struct regulator *reg; 17137813dd6fSViresh Kumar int i; 17147813dd6fSViresh Kumar 171590e3577bSViresh Kumar if (!opp_table->regulators) 171690e3577bSViresh Kumar return true; 171790e3577bSViresh Kumar 17187813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 17197813dd6fSViresh Kumar reg = opp_table->regulators[i]; 17207813dd6fSViresh Kumar 17217813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 17227813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 17237813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 17247813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 17257813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 17267813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 17277813dd6fSViresh Kumar return false; 17287813dd6fSViresh Kumar } 17297813dd6fSViresh Kumar } 17307813dd6fSViresh Kumar 17317813dd6fSViresh Kumar return true; 17327813dd6fSViresh Kumar } 17337813dd6fSViresh Kumar 17346c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2) 17356c591eecSSaravana Kannan { 17366c591eecSSaravana Kannan if (opp1->rate != opp2->rate) 17376c591eecSSaravana Kannan return opp1->rate < opp2->rate ? -1 : 1; 17386d3f922cSGeorgi Djakov if (opp1->bandwidth && opp2->bandwidth && 17396d3f922cSGeorgi Djakov opp1->bandwidth[0].peak != opp2->bandwidth[0].peak) 17406d3f922cSGeorgi Djakov return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1; 17416c591eecSSaravana Kannan if (opp1->level != opp2->level) 17426c591eecSSaravana Kannan return opp1->level < opp2->level ? -1 : 1; 17436c591eecSSaravana Kannan return 0; 17446c591eecSSaravana Kannan } 17456c591eecSSaravana Kannan 1746a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1747a1e8c136SViresh Kumar struct opp_table *opp_table, 1748a1e8c136SViresh Kumar struct list_head **head) 1749a1e8c136SViresh Kumar { 1750a1e8c136SViresh Kumar struct dev_pm_opp *opp; 17516c591eecSSaravana Kannan int opp_cmp; 1752a1e8c136SViresh Kumar 1753a1e8c136SViresh Kumar /* 1754a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1755a1e8c136SViresh Kumar * already present. 1756a1e8c136SViresh Kumar * 1757a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1758a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1759a1e8c136SViresh Kumar * loop. 1760a1e8c136SViresh Kumar */ 1761a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 17626c591eecSSaravana Kannan opp_cmp = _opp_compare_key(new_opp, opp); 17636c591eecSSaravana Kannan if (opp_cmp > 0) { 1764a1e8c136SViresh Kumar *head = &opp->node; 1765a1e8c136SViresh Kumar continue; 1766a1e8c136SViresh Kumar } 1767a1e8c136SViresh Kumar 17686c591eecSSaravana Kannan if (opp_cmp < 0) 1769a1e8c136SViresh Kumar return 0; 1770a1e8c136SViresh Kumar 1771a1e8c136SViresh Kumar /* Duplicate OPPs */ 1772a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1773a1e8c136SViresh Kumar __func__, opp->rate, opp->supplies[0].u_volt, 1774a1e8c136SViresh Kumar opp->available, new_opp->rate, 1775a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1776a1e8c136SViresh Kumar 1777a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1778a1e8c136SViresh Kumar return opp->available && 1779a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1780a1e8c136SViresh Kumar } 1781a1e8c136SViresh Kumar 1782a1e8c136SViresh Kumar return 0; 1783a1e8c136SViresh Kumar } 1784a1e8c136SViresh Kumar 17857eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count) 17867eba0c76SViresh Kumar { 17877eba0c76SViresh Kumar int i; 17887eba0c76SViresh Kumar 17897eba0c76SViresh Kumar for (i = 0; i < count; i++) { 17907eba0c76SViresh Kumar if (opp->required_opps[i]->available) 17917eba0c76SViresh Kumar continue; 17927eba0c76SViresh Kumar 17937eba0c76SViresh Kumar opp->available = false; 17947eba0c76SViresh Kumar pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n", 17957eba0c76SViresh Kumar __func__, opp->required_opps[i]->np, opp->rate); 17967eba0c76SViresh Kumar return; 17977eba0c76SViresh Kumar } 17987eba0c76SViresh Kumar } 17997eba0c76SViresh Kumar 18007813dd6fSViresh Kumar /* 18017813dd6fSViresh Kumar * Returns: 18027813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 18037813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 18047813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 18057813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 18067813dd6fSViresh Kumar * kernel try to initialize the OPP table. 18077813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 18087813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 18097813dd6fSViresh Kumar */ 18107813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1811a1e8c136SViresh Kumar struct opp_table *opp_table, bool rate_not_available) 18127813dd6fSViresh Kumar { 18137813dd6fSViresh Kumar struct list_head *head; 18147813dd6fSViresh Kumar int ret; 18157813dd6fSViresh Kumar 18167813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 18177813dd6fSViresh Kumar head = &opp_table->opp_list; 18187813dd6fSViresh Kumar 1819a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1820a1e8c136SViresh Kumar if (ret) { 18217813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 18227813dd6fSViresh Kumar return ret; 18237813dd6fSViresh Kumar } 18247813dd6fSViresh Kumar 18257813dd6fSViresh Kumar list_add(&new_opp->node, head); 18267813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 18277813dd6fSViresh Kumar 18287813dd6fSViresh Kumar new_opp->opp_table = opp_table; 18297813dd6fSViresh Kumar kref_init(&new_opp->kref); 18307813dd6fSViresh Kumar 1831a2dea4cbSGreg Kroah-Hartman opp_debug_create_one(new_opp, opp_table); 18327813dd6fSViresh Kumar 18337813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 18347813dd6fSViresh Kumar new_opp->available = false; 18357813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 18367813dd6fSViresh Kumar __func__, new_opp->rate); 18377813dd6fSViresh Kumar } 18387813dd6fSViresh Kumar 18397eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 18407eba0c76SViresh Kumar if (lazy_linking_pending(opp_table)) 18417eba0c76SViresh Kumar return 0; 1842cf65948dSDmitry Osipenko 18437eba0c76SViresh Kumar _required_opps_available(new_opp, opp_table->required_opp_count); 1844cf65948dSDmitry Osipenko 18457813dd6fSViresh Kumar return 0; 18467813dd6fSViresh Kumar } 18477813dd6fSViresh Kumar 18487813dd6fSViresh Kumar /** 18497813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 18507813dd6fSViresh Kumar * @opp_table: OPP table 18517813dd6fSViresh Kumar * @dev: device for which we do this operation 18527813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 18537813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 18547813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 18557813dd6fSViresh Kumar * 18567813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 18577813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 18587813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 18597813dd6fSViresh Kumar * 18607813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 18617813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 18627813dd6fSViresh Kumar * 18637813dd6fSViresh Kumar * Return: 18647813dd6fSViresh Kumar * 0 On success OR 18657813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 18667813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 18677813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 18687813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 18697813dd6fSViresh Kumar */ 18707813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 18717813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 18727813dd6fSViresh Kumar { 18737813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 18747813dd6fSViresh Kumar unsigned long tol; 18757813dd6fSViresh Kumar int ret; 18767813dd6fSViresh Kumar 18777813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 18787813dd6fSViresh Kumar if (!new_opp) 18797813dd6fSViresh Kumar return -ENOMEM; 18807813dd6fSViresh Kumar 18817813dd6fSViresh Kumar /* populate the opp table */ 18827813dd6fSViresh Kumar new_opp->rate = freq; 18837813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 18847813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 18857813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 18867813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 18877813dd6fSViresh Kumar new_opp->available = true; 18887813dd6fSViresh Kumar new_opp->dynamic = dynamic; 18897813dd6fSViresh Kumar 1890a1e8c136SViresh Kumar ret = _opp_add(dev, new_opp, opp_table, false); 18917813dd6fSViresh Kumar if (ret) { 18927813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 18937813dd6fSViresh Kumar if (ret == -EBUSY) 18947813dd6fSViresh Kumar ret = 0; 18957813dd6fSViresh Kumar goto free_opp; 18967813dd6fSViresh Kumar } 18977813dd6fSViresh Kumar 18987813dd6fSViresh Kumar /* 18997813dd6fSViresh Kumar * Notify the changes in the availability of the operable 19007813dd6fSViresh Kumar * frequency/voltage list. 19017813dd6fSViresh Kumar */ 19027813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 19037813dd6fSViresh Kumar return 0; 19047813dd6fSViresh Kumar 19057813dd6fSViresh Kumar free_opp: 19067813dd6fSViresh Kumar _opp_free(new_opp); 19077813dd6fSViresh Kumar 19087813dd6fSViresh Kumar return ret; 19097813dd6fSViresh Kumar } 19107813dd6fSViresh Kumar 19117813dd6fSViresh Kumar /** 191289f03984SViresh Kumar * _opp_set_supported_hw() - Set supported platforms 19137813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 19147813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 19157813dd6fSViresh Kumar * @count: Number of elements in the array. 19167813dd6fSViresh Kumar * 19177813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 19187813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 19197813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 19207813dd6fSViresh Kumar * property. 19217813dd6fSViresh Kumar */ 192289f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table, 19237813dd6fSViresh Kumar const u32 *versions, unsigned int count) 19247813dd6fSViresh Kumar { 192525419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 192625419de1SViresh Kumar if (opp_table->supported_hw) 192789f03984SViresh Kumar return 0; 19287813dd6fSViresh Kumar 19297813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 19307813dd6fSViresh Kumar GFP_KERNEL); 193189f03984SViresh Kumar if (!opp_table->supported_hw) 193289f03984SViresh Kumar return -ENOMEM; 19337813dd6fSViresh Kumar 19347813dd6fSViresh Kumar opp_table->supported_hw_count = count; 19357813dd6fSViresh Kumar 193689f03984SViresh Kumar return 0; 19377813dd6fSViresh Kumar } 19387813dd6fSViresh Kumar 19397813dd6fSViresh Kumar /** 194089f03984SViresh Kumar * _opp_put_supported_hw() - Releases resources blocked for supported hw 194189f03984SViresh Kumar * @opp_table: OPP table returned by _opp_set_supported_hw(). 19427813dd6fSViresh Kumar * 19437813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 194489f03984SViresh Kumar * _opp_set_supported_hw(). Until this is called, the opp_table structure 19457813dd6fSViresh Kumar * will not be freed. 19467813dd6fSViresh Kumar */ 194789f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table) 19487813dd6fSViresh Kumar { 194989f03984SViresh Kumar if (opp_table->supported_hw) { 19507813dd6fSViresh Kumar kfree(opp_table->supported_hw); 19517813dd6fSViresh Kumar opp_table->supported_hw = NULL; 19527813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 19537813dd6fSViresh Kumar } 19549c4f220fSYangtao Li } 19559c4f220fSYangtao Li 19569c4f220fSYangtao Li /** 1957298098e5SViresh Kumar * _opp_set_prop_name() - Set prop-extn name 19587813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 19597813dd6fSViresh Kumar * @name: name to postfix to properties. 19607813dd6fSViresh Kumar * 19617813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 19627813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 19637813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 19647813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 19657813dd6fSViresh Kumar */ 1966298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name) 19677813dd6fSViresh Kumar { 1968878ec1a9SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 19697813dd6fSViresh Kumar if (!opp_table->prop_name) { 1970298098e5SViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 1971298098e5SViresh Kumar if (!opp_table->prop_name) 1972298098e5SViresh Kumar return -ENOMEM; 19737813dd6fSViresh Kumar } 19747813dd6fSViresh Kumar 1975298098e5SViresh Kumar return 0; 19767813dd6fSViresh Kumar } 19777813dd6fSViresh Kumar 19787813dd6fSViresh Kumar /** 1979298098e5SViresh Kumar * _opp_put_prop_name() - Releases resources blocked for prop-name 1980298098e5SViresh Kumar * @opp_table: OPP table returned by _opp_set_prop_name(). 19817813dd6fSViresh Kumar * 19827813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 1983298098e5SViresh Kumar * _opp_set_prop_name(). Until this is called, the opp_table structure 19847813dd6fSViresh Kumar * will not be freed. 19857813dd6fSViresh Kumar */ 1986298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table) 19877813dd6fSViresh Kumar { 1988298098e5SViresh Kumar if (opp_table->prop_name) { 19897813dd6fSViresh Kumar kfree(opp_table->prop_name); 19907813dd6fSViresh Kumar opp_table->prop_name = NULL; 19917813dd6fSViresh Kumar } 1992298098e5SViresh Kumar } 19937813dd6fSViresh Kumar 19947813dd6fSViresh Kumar /** 1995b0ec0942SViresh Kumar * _opp_set_regulators() - Set regulator names for the device 19967813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 19977813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 19987813dd6fSViresh Kumar * @count: Number of regulators. 19997813dd6fSViresh Kumar * 20007813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 20017813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 20027813dd6fSViresh Kumar * well. 20037813dd6fSViresh Kumar * 20047813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 20057813dd6fSViresh Kumar */ 2006b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev, 200787686cc8SViresh Kumar const char * const names[]) 20087813dd6fSViresh Kumar { 200987686cc8SViresh Kumar const char * const *temp = names; 20107813dd6fSViresh Kumar struct regulator *reg; 201187686cc8SViresh Kumar int count = 0, ret, i; 201287686cc8SViresh Kumar 201387686cc8SViresh Kumar /* Count number of regulators */ 201487686cc8SViresh Kumar while (*temp++) 201587686cc8SViresh Kumar count++; 201687686cc8SViresh Kumar 201787686cc8SViresh Kumar if (!count) 2018b0ec0942SViresh Kumar return -EINVAL; 20197813dd6fSViresh Kumar 2020779b783cSViresh Kumar /* Another CPU that shares the OPP table has set the regulators ? */ 2021779b783cSViresh Kumar if (opp_table->regulators) 2022b0ec0942SViresh Kumar return 0; 20237813dd6fSViresh Kumar 20247813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 20257813dd6fSViresh Kumar sizeof(*opp_table->regulators), 20267813dd6fSViresh Kumar GFP_KERNEL); 2027b0ec0942SViresh Kumar if (!opp_table->regulators) 2028b0ec0942SViresh Kumar return -ENOMEM; 20297813dd6fSViresh Kumar 20307813dd6fSViresh Kumar for (i = 0; i < count; i++) { 20317813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 20327813dd6fSViresh Kumar if (IS_ERR(reg)) { 2033543256d2SKrzysztof Kozlowski ret = dev_err_probe(dev, PTR_ERR(reg), 2034543256d2SKrzysztof Kozlowski "%s: no regulator (%s) found\n", 2035543256d2SKrzysztof Kozlowski __func__, names[i]); 20367813dd6fSViresh Kumar goto free_regulators; 20377813dd6fSViresh Kumar } 20387813dd6fSViresh Kumar 20397813dd6fSViresh Kumar opp_table->regulators[i] = reg; 20407813dd6fSViresh Kumar } 20417813dd6fSViresh Kumar 20427813dd6fSViresh Kumar opp_table->regulator_count = count; 20437813dd6fSViresh Kumar 2044c522ce8aSViresh Kumar /* Set generic config_regulators() for single regulators here */ 2045c522ce8aSViresh Kumar if (count == 1) 2046c522ce8aSViresh Kumar opp_table->config_regulators = _opp_config_regulator_single; 2047c522ce8aSViresh Kumar 2048b0ec0942SViresh Kumar return 0; 20497813dd6fSViresh Kumar 20507813dd6fSViresh Kumar free_regulators: 205124957db1SMarek Szyprowski while (i != 0) 205224957db1SMarek Szyprowski regulator_put(opp_table->regulators[--i]); 20537813dd6fSViresh Kumar 20547813dd6fSViresh Kumar kfree(opp_table->regulators); 20557813dd6fSViresh Kumar opp_table->regulators = NULL; 205646f48acaSViresh Kumar opp_table->regulator_count = -1; 20577813dd6fSViresh Kumar 2058b0ec0942SViresh Kumar return ret; 20597813dd6fSViresh Kumar } 20607813dd6fSViresh Kumar 20617813dd6fSViresh Kumar /** 2062b0ec0942SViresh Kumar * _opp_put_regulators() - Releases resources blocked for regulator 2063b0ec0942SViresh Kumar * @opp_table: OPP table returned from _opp_set_regulators(). 20647813dd6fSViresh Kumar */ 2065b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table) 20667813dd6fSViresh Kumar { 20677813dd6fSViresh Kumar int i; 20687813dd6fSViresh Kumar 2069779b783cSViresh Kumar if (!opp_table->regulators) 2070b0ec0942SViresh Kumar return; 20717813dd6fSViresh Kumar 207272f80ce4SViresh Kumar if (opp_table->enabled) { 20738d45719cSKamil Konieczny for (i = opp_table->regulator_count - 1; i >= 0; i--) 20748d45719cSKamil Konieczny regulator_disable(opp_table->regulators[i]); 20758d45719cSKamil Konieczny } 20768d45719cSKamil Konieczny 207724957db1SMarek Szyprowski for (i = opp_table->regulator_count - 1; i >= 0; i--) 20787813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 20797813dd6fSViresh Kumar 20807813dd6fSViresh Kumar kfree(opp_table->regulators); 20817813dd6fSViresh Kumar opp_table->regulators = NULL; 208246f48acaSViresh Kumar opp_table->regulator_count = -1; 20837813dd6fSViresh Kumar } 208432aee78bSYangtao Li 20857813dd6fSViresh Kumar /** 20862368f576SViresh Kumar * _opp_set_clknames() - Set clk names for the device 20872368f576SViresh Kumar * @dev: Device for which clk names is being set. 20882368f576SViresh Kumar * @names: Clk names. 20897813dd6fSViresh Kumar * 20902368f576SViresh Kumar * In order to support OPP switching, OPP layer needs to get pointers to the 20912368f576SViresh Kumar * clocks for the device. Simple cases work fine without using this routine 20922368f576SViresh Kumar * (i.e. by passing connection-id as NULL), but for a device with multiple 20932368f576SViresh Kumar * clocks available, the OPP core needs to know the exact names of the clks to 20942368f576SViresh Kumar * use. 20957813dd6fSViresh Kumar * 20967813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 20977813dd6fSViresh Kumar */ 20982368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev, 20992368f576SViresh Kumar const char * const names[]) 21007813dd6fSViresh Kumar { 21012368f576SViresh Kumar const char * const *temp = names; 21022368f576SViresh Kumar int count = 0; 21037813dd6fSViresh Kumar 21042368f576SViresh Kumar /* Count number of clks */ 21052368f576SViresh Kumar while (*temp++) 21062368f576SViresh Kumar count++; 21077813dd6fSViresh Kumar 21082368f576SViresh Kumar /* 21092368f576SViresh Kumar * This is a special case where we have a single clock, whose connection 21102368f576SViresh Kumar * id name is NULL, i.e. first two entries are NULL in the array. 21112368f576SViresh Kumar */ 21122368f576SViresh Kumar if (!count && !names[1]) 21132368f576SViresh Kumar count = 1; 21142368f576SViresh Kumar 21152368f576SViresh Kumar /* We support only one clock name for now */ 21162368f576SViresh Kumar if (count != 1) 21172368f576SViresh Kumar return -EINVAL; 21187813dd6fSViresh Kumar 21190a43452bSViresh Kumar /* Another CPU that shares the OPP table has set the clkname ? */ 21200a43452bSViresh Kumar if (opp_table->clk_configured) 21212368f576SViresh Kumar return 0; 21220a43452bSViresh Kumar 212332439ac7SViresh Kumar /* clk shouldn't be initialized at this point */ 21242368f576SViresh Kumar if (WARN_ON(opp_table->clk)) 21252368f576SViresh Kumar return -EBUSY; 21267813dd6fSViresh Kumar 21277813dd6fSViresh Kumar /* Find clk for the device */ 21282368f576SViresh Kumar opp_table->clk = clk_get(dev, names[0]); 21297813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 21302368f576SViresh Kumar return dev_err_probe(dev, PTR_ERR(opp_table->clk), 2131543256d2SKrzysztof Kozlowski "%s: Couldn't find clock\n", __func__); 21327813dd6fSViresh Kumar } 21337813dd6fSViresh Kumar 21340a43452bSViresh Kumar opp_table->clk_configured = true; 21350a43452bSViresh Kumar 21362368f576SViresh Kumar return 0; 21377813dd6fSViresh Kumar } 21387813dd6fSViresh Kumar 21397813dd6fSViresh Kumar /** 21402368f576SViresh Kumar * _opp_put_clknames() - Releases resources blocked for clks. 21412368f576SViresh Kumar * @opp_table: OPP table returned from _opp_set_clknames(). 21427813dd6fSViresh Kumar */ 21432368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table) 21447813dd6fSViresh Kumar { 21452368f576SViresh Kumar if (opp_table->clk_configured) { 21467813dd6fSViresh Kumar clk_put(opp_table->clk); 21477813dd6fSViresh Kumar opp_table->clk = ERR_PTR(-EINVAL); 21480a43452bSViresh Kumar opp_table->clk_configured = false; 21497813dd6fSViresh Kumar } 2150a74f681cSYangtao Li } 2151a74f681cSYangtao Li 2152a74f681cSYangtao Li /** 2153aee3352fSViresh Kumar * _opp_set_config_regulators_helper() - Register custom set regulator helper. 2154aee3352fSViresh Kumar * @dev: Device for which the helper is getting registered. 2155aee3352fSViresh Kumar * @config_regulators: Custom set regulator helper. 2156aee3352fSViresh Kumar * 2157aee3352fSViresh Kumar * This is useful to support platforms with multiple regulators per device. 2158aee3352fSViresh Kumar * 2159aee3352fSViresh Kumar * This must be called before any OPPs are initialized for the device. 2160aee3352fSViresh Kumar */ 2161aee3352fSViresh Kumar static int _opp_set_config_regulators_helper(struct opp_table *opp_table, 2162aee3352fSViresh Kumar struct device *dev, config_regulators_t config_regulators) 2163aee3352fSViresh Kumar { 2164aee3352fSViresh Kumar /* Another CPU that shares the OPP table has set the helper ? */ 2165aee3352fSViresh Kumar if (!opp_table->config_regulators) 2166aee3352fSViresh Kumar opp_table->config_regulators = config_regulators; 2167aee3352fSViresh Kumar 2168aee3352fSViresh Kumar return 0; 2169aee3352fSViresh Kumar } 2170aee3352fSViresh Kumar 2171aee3352fSViresh Kumar /** 2172aee3352fSViresh Kumar * _opp_put_config_regulators_helper() - Releases resources blocked for 2173aee3352fSViresh Kumar * config_regulators helper. 2174aee3352fSViresh Kumar * @opp_table: OPP table returned from _opp_set_config_regulators_helper(). 2175aee3352fSViresh Kumar * 2176aee3352fSViresh Kumar * Release resources blocked for platform specific config_regulators helper. 2177aee3352fSViresh Kumar */ 2178aee3352fSViresh Kumar static void _opp_put_config_regulators_helper(struct opp_table *opp_table) 2179aee3352fSViresh Kumar { 2180aee3352fSViresh Kumar if (opp_table->config_regulators) 2181aee3352fSViresh Kumar opp_table->config_regulators = NULL; 2182aee3352fSViresh Kumar } 2183aee3352fSViresh Kumar 2184442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table) 21856319aee1SViresh Kumar { 21866319aee1SViresh Kumar int index; 21876319aee1SViresh Kumar 2188cb60e960SViresh Kumar if (!opp_table->genpd_virt_devs) 2189cb60e960SViresh Kumar return; 2190cb60e960SViresh Kumar 21916319aee1SViresh Kumar for (index = 0; index < opp_table->required_opp_count; index++) { 21926319aee1SViresh Kumar if (!opp_table->genpd_virt_devs[index]) 21936319aee1SViresh Kumar continue; 21946319aee1SViresh Kumar 21956319aee1SViresh Kumar dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 21966319aee1SViresh Kumar opp_table->genpd_virt_devs[index] = NULL; 21976319aee1SViresh Kumar } 2198c0ab9e08SViresh Kumar 2199c0ab9e08SViresh Kumar kfree(opp_table->genpd_virt_devs); 2200c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = NULL; 22016319aee1SViresh Kumar } 22026319aee1SViresh Kumar 22037813dd6fSViresh Kumar /** 2204442e7a17SViresh Kumar * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 22056319aee1SViresh Kumar * @dev: Consumer device for which the genpd is getting attached. 22066319aee1SViresh Kumar * @names: Null terminated array of pointers containing names of genpd to attach. 220717a8f868SViresh Kumar * @virt_devs: Pointer to return the array of virtual devices. 22084f018bc0SViresh Kumar * 22094f018bc0SViresh Kumar * Multiple generic power domains for a device are supported with the help of 22104f018bc0SViresh Kumar * virtual genpd devices, which are created for each consumer device - genpd 22114f018bc0SViresh Kumar * pair. These are the device structures which are attached to the power domain 22124f018bc0SViresh Kumar * and are required by the OPP core to set the performance state of the genpd. 22136319aee1SViresh Kumar * The same API also works for the case where single genpd is available and so 22146319aee1SViresh Kumar * we don't need to support that separately. 22154f018bc0SViresh Kumar * 22164f018bc0SViresh Kumar * This helper will normally be called by the consumer driver of the device 22176319aee1SViresh Kumar * "dev", as only that has details of the genpd names. 22184f018bc0SViresh Kumar * 22196319aee1SViresh Kumar * This helper needs to be called once with a list of all genpd to attach. 22206319aee1SViresh Kumar * Otherwise the original device structure will be used instead by the OPP core. 2221baea35e4SViresh Kumar * 2222baea35e4SViresh Kumar * The order of entries in the names array must match the order in which 2223baea35e4SViresh Kumar * "required-opps" are added in DT. 22244f018bc0SViresh Kumar */ 2225442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev, 22263734b9f2SDmitry Osipenko const char * const *names, struct device ***virt_devs) 22274f018bc0SViresh Kumar { 22286319aee1SViresh Kumar struct device *virt_dev; 2229baea35e4SViresh Kumar int index = 0, ret = -EINVAL; 22303734b9f2SDmitry Osipenko const char * const *name = names; 22314f018bc0SViresh Kumar 2232cb60e960SViresh Kumar if (opp_table->genpd_virt_devs) 2233442e7a17SViresh Kumar return 0; 22344f018bc0SViresh Kumar 22356319aee1SViresh Kumar /* 22366319aee1SViresh Kumar * If the genpd's OPP table isn't already initialized, parsing of the 22376319aee1SViresh Kumar * required-opps fail for dev. We should retry this after genpd's OPP 22386319aee1SViresh Kumar * table is added. 22396319aee1SViresh Kumar */ 2240442e7a17SViresh Kumar if (!opp_table->required_opp_count) 2241442e7a17SViresh Kumar return -EPROBE_DEFER; 22426319aee1SViresh Kumar 22434f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 22444f018bc0SViresh Kumar 2245c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, 2246c0ab9e08SViresh Kumar sizeof(*opp_table->genpd_virt_devs), 2247c0ab9e08SViresh Kumar GFP_KERNEL); 2248c0ab9e08SViresh Kumar if (!opp_table->genpd_virt_devs) 2249c0ab9e08SViresh Kumar goto unlock; 22504f018bc0SViresh Kumar 22516319aee1SViresh Kumar while (*name) { 22526319aee1SViresh Kumar if (index >= opp_table->required_opp_count) { 22536319aee1SViresh Kumar dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 22546319aee1SViresh Kumar *name, opp_table->required_opp_count, index); 22556319aee1SViresh Kumar goto err; 22566319aee1SViresh Kumar } 22574f018bc0SViresh Kumar 22586319aee1SViresh Kumar virt_dev = dev_pm_domain_attach_by_name(dev, *name); 22594ea9496cSTang Bin if (IS_ERR_OR_NULL(virt_dev)) { 22604ea9496cSTang Bin ret = PTR_ERR(virt_dev) ? : -ENODEV; 22616319aee1SViresh Kumar dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 22626319aee1SViresh Kumar goto err; 22634f018bc0SViresh Kumar } 22644f018bc0SViresh Kumar 22654f018bc0SViresh Kumar opp_table->genpd_virt_devs[index] = virt_dev; 2266baea35e4SViresh Kumar index++; 22676319aee1SViresh Kumar name++; 22686319aee1SViresh Kumar } 22696319aee1SViresh Kumar 227017a8f868SViresh Kumar if (virt_devs) 227117a8f868SViresh Kumar *virt_devs = opp_table->genpd_virt_devs; 22724f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 22734f018bc0SViresh Kumar 2274442e7a17SViresh Kumar return 0; 22756319aee1SViresh Kumar 22766319aee1SViresh Kumar err: 2277442e7a17SViresh Kumar _detach_genpd(opp_table); 2278c0ab9e08SViresh Kumar unlock: 22796319aee1SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 2280442e7a17SViresh Kumar return ret; 22816319aee1SViresh Kumar 22824f018bc0SViresh Kumar } 22834f018bc0SViresh Kumar 22844f018bc0SViresh Kumar /** 2285442e7a17SViresh Kumar * _opp_detach_genpd() - Detach genpd(s) from the device. 2286442e7a17SViresh Kumar * @opp_table: OPP table returned by _opp_attach_genpd(). 22874f018bc0SViresh Kumar * 22886319aee1SViresh Kumar * This detaches the genpd(s), resets the virtual device pointers, and puts the 22896319aee1SViresh Kumar * OPP table. 22904f018bc0SViresh Kumar */ 2291442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table) 22924f018bc0SViresh Kumar { 22934f018bc0SViresh Kumar /* 22944f018bc0SViresh Kumar * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 22954f018bc0SViresh Kumar * used in parallel. 22964f018bc0SViresh Kumar */ 22974f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 2298442e7a17SViresh Kumar _detach_genpd(opp_table); 22994f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 23004f018bc0SViresh Kumar } 2301b4b9e223SDmitry Osipenko 230211b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data) 230311b9b663SViresh Kumar { 230411b9b663SViresh Kumar if (data->flags & OPP_CONFIG_GENPD) 2305442e7a17SViresh Kumar _opp_detach_genpd(data->opp_table); 230611b9b663SViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR) 2307b0ec0942SViresh Kumar _opp_put_regulators(data->opp_table); 230811b9b663SViresh Kumar if (data->flags & OPP_CONFIG_SUPPORTED_HW) 230989f03984SViresh Kumar _opp_put_supported_hw(data->opp_table); 23101f378c6eSViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR_HELPER) 2311aee3352fSViresh Kumar _opp_put_config_regulators_helper(data->opp_table); 231211b9b663SViresh Kumar if (data->flags & OPP_CONFIG_PROP_NAME) 2313298098e5SViresh Kumar _opp_put_prop_name(data->opp_table); 231411b9b663SViresh Kumar if (data->flags & OPP_CONFIG_CLK) 23152368f576SViresh Kumar _opp_put_clknames(data->opp_table); 231611b9b663SViresh Kumar 231711b9b663SViresh Kumar dev_pm_opp_put_opp_table(data->opp_table); 231811b9b663SViresh Kumar kfree(data); 231911b9b663SViresh Kumar } 232011b9b663SViresh Kumar 232111b9b663SViresh Kumar /** 232211b9b663SViresh Kumar * dev_pm_opp_set_config() - Set OPP configuration for the device. 232311b9b663SViresh Kumar * @dev: Device for which configuration is being set. 232411b9b663SViresh Kumar * @config: OPP configuration. 232511b9b663SViresh Kumar * 232611b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 232711b9b663SViresh Kumar * 232811b9b663SViresh Kumar * This must be called before any OPPs are initialized for the device. This may 232911b9b663SViresh Kumar * be called multiple times for the same OPP table, for example once for each 233011b9b663SViresh Kumar * CPU that share the same table. This must be balanced by the same number of 233111b9b663SViresh Kumar * calls to dev_pm_opp_clear_config() in order to free the OPP table properly. 233211b9b663SViresh Kumar * 233311b9b663SViresh Kumar * This returns a token to the caller, which must be passed to 233411b9b663SViresh Kumar * dev_pm_opp_clear_config() to free the resources later. The value of the 233511b9b663SViresh Kumar * returned token will be >= 1 for success and negative for errors. The minimum 233611b9b663SViresh Kumar * value of 1 is chosen here to make it easy for callers to manage the resource. 233711b9b663SViresh Kumar */ 233811b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 233911b9b663SViresh Kumar { 2340298098e5SViresh Kumar struct opp_table *opp_table; 234111b9b663SViresh Kumar struct opp_config_data *data; 234211b9b663SViresh Kumar unsigned int id; 234311b9b663SViresh Kumar int ret; 234411b9b663SViresh Kumar 234511b9b663SViresh Kumar data = kmalloc(sizeof(*data), GFP_KERNEL); 234611b9b663SViresh Kumar if (!data) 234711b9b663SViresh Kumar return -ENOMEM; 234811b9b663SViresh Kumar 234911b9b663SViresh Kumar opp_table = _add_opp_table(dev, false); 235011b9b663SViresh Kumar if (IS_ERR(opp_table)) { 235111b9b663SViresh Kumar kfree(data); 235211b9b663SViresh Kumar return PTR_ERR(opp_table); 235311b9b663SViresh Kumar } 235411b9b663SViresh Kumar 235511b9b663SViresh Kumar data->opp_table = opp_table; 235611b9b663SViresh Kumar data->flags = 0; 235711b9b663SViresh Kumar 235811b9b663SViresh Kumar /* This should be called before OPPs are initialized */ 235911b9b663SViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 236011b9b663SViresh Kumar ret = -EBUSY; 236111b9b663SViresh Kumar goto err; 236211b9b663SViresh Kumar } 236311b9b663SViresh Kumar 236411b9b663SViresh Kumar /* Configure clocks */ 236511b9b663SViresh Kumar if (config->clk_names) { 23662368f576SViresh Kumar ret = _opp_set_clknames(opp_table, dev, config->clk_names); 23672368f576SViresh Kumar if (ret) 236811b9b663SViresh Kumar goto err; 236911b9b663SViresh Kumar 237011b9b663SViresh Kumar data->flags |= OPP_CONFIG_CLK; 237111b9b663SViresh Kumar } 237211b9b663SViresh Kumar 237311b9b663SViresh Kumar /* Configure property names */ 237411b9b663SViresh Kumar if (config->prop_name) { 2375298098e5SViresh Kumar ret = _opp_set_prop_name(opp_table, config->prop_name); 2376298098e5SViresh Kumar if (ret) 237711b9b663SViresh Kumar goto err; 237811b9b663SViresh Kumar 237911b9b663SViresh Kumar data->flags |= OPP_CONFIG_PROP_NAME; 238011b9b663SViresh Kumar } 238111b9b663SViresh Kumar 2382aee3352fSViresh Kumar /* Configure config_regulators helper */ 2383aee3352fSViresh Kumar if (config->config_regulators) { 2384aee3352fSViresh Kumar ret = _opp_set_config_regulators_helper(opp_table, dev, 2385aee3352fSViresh Kumar config->config_regulators); 2386aee3352fSViresh Kumar if (ret) 2387aee3352fSViresh Kumar goto err; 2388aee3352fSViresh Kumar 2389aee3352fSViresh Kumar data->flags |= OPP_CONFIG_REGULATOR_HELPER; 2390aee3352fSViresh Kumar } 2391aee3352fSViresh Kumar 239211b9b663SViresh Kumar /* Configure supported hardware */ 239311b9b663SViresh Kumar if (config->supported_hw) { 239489f03984SViresh Kumar ret = _opp_set_supported_hw(opp_table, config->supported_hw, 239511b9b663SViresh Kumar config->supported_hw_count); 239689f03984SViresh Kumar if (ret) 239711b9b663SViresh Kumar goto err; 239811b9b663SViresh Kumar 239911b9b663SViresh Kumar data->flags |= OPP_CONFIG_SUPPORTED_HW; 240011b9b663SViresh Kumar } 240111b9b663SViresh Kumar 240211b9b663SViresh Kumar /* Configure supplies */ 240311b9b663SViresh Kumar if (config->regulator_names) { 2404b0ec0942SViresh Kumar ret = _opp_set_regulators(opp_table, dev, 2405b0ec0942SViresh Kumar config->regulator_names); 2406b0ec0942SViresh Kumar if (ret) 240711b9b663SViresh Kumar goto err; 240811b9b663SViresh Kumar 240911b9b663SViresh Kumar data->flags |= OPP_CONFIG_REGULATOR; 241011b9b663SViresh Kumar } 241111b9b663SViresh Kumar 241211b9b663SViresh Kumar /* Attach genpds */ 241311b9b663SViresh Kumar if (config->genpd_names) { 2414442e7a17SViresh Kumar ret = _opp_attach_genpd(opp_table, dev, config->genpd_names, 241511b9b663SViresh Kumar config->virt_devs); 2416442e7a17SViresh Kumar if (ret) 241711b9b663SViresh Kumar goto err; 241811b9b663SViresh Kumar 241911b9b663SViresh Kumar data->flags |= OPP_CONFIG_GENPD; 242011b9b663SViresh Kumar } 242111b9b663SViresh Kumar 242211b9b663SViresh Kumar ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX), 242311b9b663SViresh Kumar GFP_KERNEL); 242411b9b663SViresh Kumar if (ret) 242511b9b663SViresh Kumar goto err; 242611b9b663SViresh Kumar 242711b9b663SViresh Kumar return id; 242811b9b663SViresh Kumar 242911b9b663SViresh Kumar err: 243011b9b663SViresh Kumar _opp_clear_config(data); 243111b9b663SViresh Kumar return ret; 243211b9b663SViresh Kumar } 243311b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config); 243411b9b663SViresh Kumar 243511b9b663SViresh Kumar /** 243611b9b663SViresh Kumar * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration. 243711b9b663SViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_config(). 243811b9b663SViresh Kumar * 243911b9b663SViresh Kumar * This allows all device OPP configurations to be cleared at once. This must be 244011b9b663SViresh Kumar * called once for each call made to dev_pm_opp_set_config(), in order to free 244111b9b663SViresh Kumar * the OPPs properly. 244211b9b663SViresh Kumar * 244311b9b663SViresh Kumar * Currently the first call itself ends up freeing all the OPP configurations, 244411b9b663SViresh Kumar * while the later ones only drop the OPP table reference. This works well for 244511b9b663SViresh Kumar * now as we would never want to use an half initialized OPP table and want to 244611b9b663SViresh Kumar * remove the configurations together. 244711b9b663SViresh Kumar */ 244811b9b663SViresh Kumar void dev_pm_opp_clear_config(int token) 244911b9b663SViresh Kumar { 245011b9b663SViresh Kumar struct opp_config_data *data; 245111b9b663SViresh Kumar 245211b9b663SViresh Kumar /* 245311b9b663SViresh Kumar * This lets the callers call this unconditionally and keep their code 245411b9b663SViresh Kumar * simple. 245511b9b663SViresh Kumar */ 245611b9b663SViresh Kumar if (unlikely(token <= 0)) 245711b9b663SViresh Kumar return; 245811b9b663SViresh Kumar 245911b9b663SViresh Kumar data = xa_erase(&opp_configs, token); 246011b9b663SViresh Kumar if (WARN_ON(!data)) 246111b9b663SViresh Kumar return; 246211b9b663SViresh Kumar 246311b9b663SViresh Kumar _opp_clear_config(data); 246411b9b663SViresh Kumar } 246511b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config); 246611b9b663SViresh Kumar 246711b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token) 246811b9b663SViresh Kumar { 246911b9b663SViresh Kumar dev_pm_opp_clear_config((unsigned long)token); 247011b9b663SViresh Kumar } 247111b9b663SViresh Kumar 247211b9b663SViresh Kumar /** 247311b9b663SViresh Kumar * devm_pm_opp_set_config() - Set OPP configuration for the device. 247411b9b663SViresh Kumar * @dev: Device for which configuration is being set. 247511b9b663SViresh Kumar * @config: OPP configuration. 247611b9b663SViresh Kumar * 247711b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 247811b9b663SViresh Kumar * This is a resource-managed variant of dev_pm_opp_set_config(). 247911b9b663SViresh Kumar * 248011b9b663SViresh Kumar * Return: 0 on success and errorno otherwise. 248111b9b663SViresh Kumar */ 248211b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 248311b9b663SViresh Kumar { 248411b9b663SViresh Kumar int token = dev_pm_opp_set_config(dev, config); 248511b9b663SViresh Kumar 248611b9b663SViresh Kumar if (token < 0) 248711b9b663SViresh Kumar return token; 248811b9b663SViresh Kumar 248911b9b663SViresh Kumar return devm_add_action_or_reset(dev, devm_pm_opp_config_release, 249011b9b663SViresh Kumar (void *) ((unsigned long) token)); 249111b9b663SViresh Kumar } 249211b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config); 249311b9b663SViresh Kumar 24944f018bc0SViresh Kumar /** 24957d8658efSSaravana Kannan * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP. 24967d8658efSSaravana Kannan * @src_table: OPP table which has @dst_table as one of its required OPP table. 24977d8658efSSaravana Kannan * @dst_table: Required OPP table of the @src_table. 24987d8658efSSaravana Kannan * @src_opp: OPP from the @src_table. 24997d8658efSSaravana Kannan * 25007d8658efSSaravana Kannan * This function returns the OPP (present in @dst_table) pointed out by the 25017d8658efSSaravana Kannan * "required-opps" property of the @src_opp (present in @src_table). 25027d8658efSSaravana Kannan * 25037d8658efSSaravana Kannan * The callers are required to call dev_pm_opp_put() for the returned OPP after 25047d8658efSSaravana Kannan * use. 25057d8658efSSaravana Kannan * 25067d8658efSSaravana Kannan * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise. 25077d8658efSSaravana Kannan */ 25087d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table, 25097d8658efSSaravana Kannan struct opp_table *dst_table, 25107d8658efSSaravana Kannan struct dev_pm_opp *src_opp) 25117d8658efSSaravana Kannan { 25127d8658efSSaravana Kannan struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV); 25137d8658efSSaravana Kannan int i; 25147d8658efSSaravana Kannan 25157d8658efSSaravana Kannan if (!src_table || !dst_table || !src_opp || 25167d8658efSSaravana Kannan !src_table->required_opp_tables) 25177d8658efSSaravana Kannan return ERR_PTR(-EINVAL); 25187d8658efSSaravana Kannan 25197d8658efSSaravana Kannan /* required-opps not fully initialized yet */ 25207d8658efSSaravana Kannan if (lazy_linking_pending(src_table)) 25217d8658efSSaravana Kannan return ERR_PTR(-EBUSY); 25227d8658efSSaravana Kannan 25237d8658efSSaravana Kannan for (i = 0; i < src_table->required_opp_count; i++) { 25247d8658efSSaravana Kannan if (src_table->required_opp_tables[i] == dst_table) { 25257d8658efSSaravana Kannan mutex_lock(&src_table->lock); 25267d8658efSSaravana Kannan 25277d8658efSSaravana Kannan list_for_each_entry(opp, &src_table->opp_list, node) { 25287d8658efSSaravana Kannan if (opp == src_opp) { 25297d8658efSSaravana Kannan dest_opp = opp->required_opps[i]; 25307d8658efSSaravana Kannan dev_pm_opp_get(dest_opp); 25317d8658efSSaravana Kannan break; 25327d8658efSSaravana Kannan } 25337d8658efSSaravana Kannan } 25347d8658efSSaravana Kannan 25357d8658efSSaravana Kannan mutex_unlock(&src_table->lock); 25367d8658efSSaravana Kannan break; 25377d8658efSSaravana Kannan } 25387d8658efSSaravana Kannan } 25397d8658efSSaravana Kannan 25407d8658efSSaravana Kannan if (IS_ERR(dest_opp)) { 25417d8658efSSaravana Kannan pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, 25427d8658efSSaravana Kannan src_table, dst_table); 25437d8658efSSaravana Kannan } 25447d8658efSSaravana Kannan 25457d8658efSSaravana Kannan return dest_opp; 25467d8658efSSaravana Kannan } 25477d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp); 25487d8658efSSaravana Kannan 25497d8658efSSaravana Kannan /** 2550c8a59103SViresh Kumar * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 2551c8a59103SViresh Kumar * @src_table: OPP table which has dst_table as one of its required OPP table. 2552c8a59103SViresh Kumar * @dst_table: Required OPP table of the src_table. 2553c8a59103SViresh Kumar * @pstate: Current performance state of the src_table. 2554c8a59103SViresh Kumar * 2555c8a59103SViresh Kumar * This Returns pstate of the OPP (present in @dst_table) pointed out by the 2556c8a59103SViresh Kumar * "required-opps" property of the OPP (present in @src_table) which has 2557c8a59103SViresh Kumar * performance state set to @pstate. 2558c8a59103SViresh Kumar * 2559c8a59103SViresh Kumar * Return: Zero or positive performance state on success, otherwise negative 2560c8a59103SViresh Kumar * value on errors. 2561c8a59103SViresh Kumar */ 2562c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 2563c8a59103SViresh Kumar struct opp_table *dst_table, 2564c8a59103SViresh Kumar unsigned int pstate) 2565c8a59103SViresh Kumar { 2566c8a59103SViresh Kumar struct dev_pm_opp *opp; 2567c8a59103SViresh Kumar int dest_pstate = -EINVAL; 2568c8a59103SViresh Kumar int i; 2569c8a59103SViresh Kumar 2570c8a59103SViresh Kumar /* 2571c8a59103SViresh Kumar * Normally the src_table will have the "required_opps" property set to 2572c8a59103SViresh Kumar * point to one of the OPPs in the dst_table, but in some cases the 2573c8a59103SViresh Kumar * genpd and its master have one to one mapping of performance states 2574c8a59103SViresh Kumar * and so none of them have the "required-opps" property set. Return the 2575c8a59103SViresh Kumar * pstate of the src_table as it is in such cases. 2576c8a59103SViresh Kumar */ 2577f2f4d2b8SDmitry Osipenko if (!src_table || !src_table->required_opp_count) 2578c8a59103SViresh Kumar return pstate; 2579c8a59103SViresh Kumar 25807eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 25817eba0c76SViresh Kumar if (lazy_linking_pending(src_table)) 25827eba0c76SViresh Kumar return -EBUSY; 25837eba0c76SViresh Kumar 2584c8a59103SViresh Kumar for (i = 0; i < src_table->required_opp_count; i++) { 2585c8a59103SViresh Kumar if (src_table->required_opp_tables[i]->np == dst_table->np) 2586c8a59103SViresh Kumar break; 2587c8a59103SViresh Kumar } 2588c8a59103SViresh Kumar 2589c8a59103SViresh Kumar if (unlikely(i == src_table->required_opp_count)) { 2590c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 2591c8a59103SViresh Kumar __func__, src_table, dst_table); 2592c8a59103SViresh Kumar return -EINVAL; 2593c8a59103SViresh Kumar } 2594c8a59103SViresh Kumar 2595c8a59103SViresh Kumar mutex_lock(&src_table->lock); 2596c8a59103SViresh Kumar 2597c8a59103SViresh Kumar list_for_each_entry(opp, &src_table->opp_list, node) { 2598c8a59103SViresh Kumar if (opp->pstate == pstate) { 2599c8a59103SViresh Kumar dest_pstate = opp->required_opps[i]->pstate; 2600c8a59103SViresh Kumar goto unlock; 2601c8a59103SViresh Kumar } 2602c8a59103SViresh Kumar } 2603c8a59103SViresh Kumar 2604c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 2605c8a59103SViresh Kumar dst_table); 2606c8a59103SViresh Kumar 2607c8a59103SViresh Kumar unlock: 2608c8a59103SViresh Kumar mutex_unlock(&src_table->lock); 2609c8a59103SViresh Kumar 2610c8a59103SViresh Kumar return dest_pstate; 2611c8a59103SViresh Kumar } 2612c8a59103SViresh Kumar 2613c8a59103SViresh Kumar /** 26147813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 26157813dd6fSViresh Kumar * @dev: device for which we do this operation 26167813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 26177813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 26187813dd6fSViresh Kumar * 26197813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 26207813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 26217813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 26227813dd6fSViresh Kumar * 26237813dd6fSViresh Kumar * Return: 26247813dd6fSViresh Kumar * 0 On success OR 26257813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 26267813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 26277813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 26287813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 26297813dd6fSViresh Kumar */ 26307813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 26317813dd6fSViresh Kumar { 26327813dd6fSViresh Kumar struct opp_table *opp_table; 26337813dd6fSViresh Kumar int ret; 26347813dd6fSViresh Kumar 263532439ac7SViresh Kumar opp_table = _add_opp_table(dev, true); 2636dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2637dd461cd9SStephan Gerhold return PTR_ERR(opp_table); 26387813dd6fSViresh Kumar 263946f48acaSViresh Kumar /* Fix regulator count for dynamic OPPs */ 264046f48acaSViresh Kumar opp_table->regulator_count = 1; 264146f48acaSViresh Kumar 26427813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 26430ad8c623SViresh Kumar if (ret) 26447813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 26450ad8c623SViresh Kumar 26467813dd6fSViresh Kumar return ret; 26477813dd6fSViresh Kumar } 26487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 26497813dd6fSViresh Kumar 26507813dd6fSViresh Kumar /** 26517813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 26527813dd6fSViresh Kumar * @dev: device for which we do this operation 26537813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 26547813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 26557813dd6fSViresh Kumar * 26567813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 26577813dd6fSViresh Kumar * which is isolated here. 26587813dd6fSViresh Kumar * 26597813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 26607813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 26617813dd6fSViresh Kumar * successful. 26627813dd6fSViresh Kumar */ 26637813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 26647813dd6fSViresh Kumar bool availability_req) 26657813dd6fSViresh Kumar { 26667813dd6fSViresh Kumar struct opp_table *opp_table; 26677813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 26687813dd6fSViresh Kumar int r = 0; 26697813dd6fSViresh Kumar 26707813dd6fSViresh Kumar /* Find the opp_table */ 26717813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 26727813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 26737813dd6fSViresh Kumar r = PTR_ERR(opp_table); 26747813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 26757813dd6fSViresh Kumar return r; 26767813dd6fSViresh Kumar } 26777813dd6fSViresh Kumar 26787813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 26797813dd6fSViresh Kumar 26807813dd6fSViresh Kumar /* Do we have the frequency? */ 26817813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 26827813dd6fSViresh Kumar if (tmp_opp->rate == freq) { 26837813dd6fSViresh Kumar opp = tmp_opp; 26847813dd6fSViresh Kumar break; 26857813dd6fSViresh Kumar } 26867813dd6fSViresh Kumar } 26877813dd6fSViresh Kumar 26887813dd6fSViresh Kumar if (IS_ERR(opp)) { 26897813dd6fSViresh Kumar r = PTR_ERR(opp); 26907813dd6fSViresh Kumar goto unlock; 26917813dd6fSViresh Kumar } 26927813dd6fSViresh Kumar 26937813dd6fSViresh Kumar /* Is update really needed? */ 26947813dd6fSViresh Kumar if (opp->available == availability_req) 26957813dd6fSViresh Kumar goto unlock; 26967813dd6fSViresh Kumar 26977813dd6fSViresh Kumar opp->available = availability_req; 26987813dd6fSViresh Kumar 26997813dd6fSViresh Kumar dev_pm_opp_get(opp); 27007813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 27017813dd6fSViresh Kumar 27027813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 27037813dd6fSViresh Kumar if (availability_req) 27047813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 27057813dd6fSViresh Kumar opp); 27067813dd6fSViresh Kumar else 27077813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 27087813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 27097813dd6fSViresh Kumar 27107813dd6fSViresh Kumar dev_pm_opp_put(opp); 27117813dd6fSViresh Kumar goto put_table; 27127813dd6fSViresh Kumar 27137813dd6fSViresh Kumar unlock: 27147813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 27157813dd6fSViresh Kumar put_table: 27167813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 27177813dd6fSViresh Kumar return r; 27187813dd6fSViresh Kumar } 27197813dd6fSViresh Kumar 27207813dd6fSViresh Kumar /** 272125cb20a2SStephen Boyd * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP 272225cb20a2SStephen Boyd * @dev: device for which we do this operation 272325cb20a2SStephen Boyd * @freq: OPP frequency to adjust voltage of 272425cb20a2SStephen Boyd * @u_volt: new OPP target voltage 272525cb20a2SStephen Boyd * @u_volt_min: new OPP min voltage 272625cb20a2SStephen Boyd * @u_volt_max: new OPP max voltage 272725cb20a2SStephen Boyd * 272825cb20a2SStephen Boyd * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 272925cb20a2SStephen Boyd * copy operation, returns 0 if no modifcation was done OR modification was 273025cb20a2SStephen Boyd * successful. 273125cb20a2SStephen Boyd */ 273225cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq, 273325cb20a2SStephen Boyd unsigned long u_volt, unsigned long u_volt_min, 273425cb20a2SStephen Boyd unsigned long u_volt_max) 273525cb20a2SStephen Boyd 273625cb20a2SStephen Boyd { 273725cb20a2SStephen Boyd struct opp_table *opp_table; 273825cb20a2SStephen Boyd struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 273925cb20a2SStephen Boyd int r = 0; 274025cb20a2SStephen Boyd 274125cb20a2SStephen Boyd /* Find the opp_table */ 274225cb20a2SStephen Boyd opp_table = _find_opp_table(dev); 274325cb20a2SStephen Boyd if (IS_ERR(opp_table)) { 274425cb20a2SStephen Boyd r = PTR_ERR(opp_table); 274525cb20a2SStephen Boyd dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 274625cb20a2SStephen Boyd return r; 274725cb20a2SStephen Boyd } 274825cb20a2SStephen Boyd 274925cb20a2SStephen Boyd mutex_lock(&opp_table->lock); 275025cb20a2SStephen Boyd 275125cb20a2SStephen Boyd /* Do we have the frequency? */ 275225cb20a2SStephen Boyd list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 275325cb20a2SStephen Boyd if (tmp_opp->rate == freq) { 275425cb20a2SStephen Boyd opp = tmp_opp; 275525cb20a2SStephen Boyd break; 275625cb20a2SStephen Boyd } 275725cb20a2SStephen Boyd } 275825cb20a2SStephen Boyd 275925cb20a2SStephen Boyd if (IS_ERR(opp)) { 276025cb20a2SStephen Boyd r = PTR_ERR(opp); 276125cb20a2SStephen Boyd goto adjust_unlock; 276225cb20a2SStephen Boyd } 276325cb20a2SStephen Boyd 276425cb20a2SStephen Boyd /* Is update really needed? */ 276525cb20a2SStephen Boyd if (opp->supplies->u_volt == u_volt) 276625cb20a2SStephen Boyd goto adjust_unlock; 276725cb20a2SStephen Boyd 276825cb20a2SStephen Boyd opp->supplies->u_volt = u_volt; 276925cb20a2SStephen Boyd opp->supplies->u_volt_min = u_volt_min; 277025cb20a2SStephen Boyd opp->supplies->u_volt_max = u_volt_max; 277125cb20a2SStephen Boyd 277225cb20a2SStephen Boyd dev_pm_opp_get(opp); 277325cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 277425cb20a2SStephen Boyd 277525cb20a2SStephen Boyd /* Notify the voltage change of the OPP */ 277625cb20a2SStephen Boyd blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE, 277725cb20a2SStephen Boyd opp); 277825cb20a2SStephen Boyd 277925cb20a2SStephen Boyd dev_pm_opp_put(opp); 278025cb20a2SStephen Boyd goto adjust_put_table; 278125cb20a2SStephen Boyd 278225cb20a2SStephen Boyd adjust_unlock: 278325cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 278425cb20a2SStephen Boyd adjust_put_table: 278525cb20a2SStephen Boyd dev_pm_opp_put_opp_table(opp_table); 278625cb20a2SStephen Boyd return r; 278725cb20a2SStephen Boyd } 278803649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage); 278925cb20a2SStephen Boyd 279025cb20a2SStephen Boyd /** 27917813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 27927813dd6fSViresh Kumar * @dev: device for which we do this operation 27937813dd6fSViresh Kumar * @freq: OPP frequency to enable 27947813dd6fSViresh Kumar * 27957813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 27967813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 27977813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 27987813dd6fSViresh Kumar * 27997813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 28007813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 28017813dd6fSViresh Kumar * successful. 28027813dd6fSViresh Kumar */ 28037813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 28047813dd6fSViresh Kumar { 28057813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 28067813dd6fSViresh Kumar } 28077813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 28087813dd6fSViresh Kumar 28097813dd6fSViresh Kumar /** 28107813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 28117813dd6fSViresh Kumar * @dev: device for which we do this operation 28127813dd6fSViresh Kumar * @freq: OPP frequency to disable 28137813dd6fSViresh Kumar * 28147813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 28157813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 28167813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 28177813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 28187813dd6fSViresh Kumar * 28197813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 28207813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 28217813dd6fSViresh Kumar * successful. 28227813dd6fSViresh Kumar */ 28237813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 28247813dd6fSViresh Kumar { 28257813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 28267813dd6fSViresh Kumar } 28277813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 28287813dd6fSViresh Kumar 28297813dd6fSViresh Kumar /** 28307813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 28317813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 28327813dd6fSViresh Kumar * @nb: Notifier block to be registered 28337813dd6fSViresh Kumar * 28347813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 28357813dd6fSViresh Kumar */ 28367813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 28377813dd6fSViresh Kumar { 28387813dd6fSViresh Kumar struct opp_table *opp_table; 28397813dd6fSViresh Kumar int ret; 28407813dd6fSViresh Kumar 28417813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 28427813dd6fSViresh Kumar if (IS_ERR(opp_table)) 28437813dd6fSViresh Kumar return PTR_ERR(opp_table); 28447813dd6fSViresh Kumar 28457813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 28467813dd6fSViresh Kumar 28477813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 28487813dd6fSViresh Kumar 28497813dd6fSViresh Kumar return ret; 28507813dd6fSViresh Kumar } 28517813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 28527813dd6fSViresh Kumar 28537813dd6fSViresh Kumar /** 28547813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 28557813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 28567813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 28577813dd6fSViresh Kumar * 28587813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 28597813dd6fSViresh Kumar */ 28607813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 28617813dd6fSViresh Kumar struct notifier_block *nb) 28627813dd6fSViresh Kumar { 28637813dd6fSViresh Kumar struct opp_table *opp_table; 28647813dd6fSViresh Kumar int ret; 28657813dd6fSViresh Kumar 28667813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 28677813dd6fSViresh Kumar if (IS_ERR(opp_table)) 28687813dd6fSViresh Kumar return PTR_ERR(opp_table); 28697813dd6fSViresh Kumar 28707813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 28717813dd6fSViresh Kumar 28727813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 28737813dd6fSViresh Kumar 28747813dd6fSViresh Kumar return ret; 28757813dd6fSViresh Kumar } 28767813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 28777813dd6fSViresh Kumar 28788aaf6264SViresh Kumar /** 28798aaf6264SViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 28808aaf6264SViresh Kumar * @dev: device pointer used to lookup OPP table. 28818aaf6264SViresh Kumar * 28828aaf6264SViresh Kumar * Free both OPPs created using static entries present in DT and the 28838aaf6264SViresh Kumar * dynamically added entries. 28848aaf6264SViresh Kumar */ 28858aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 28867813dd6fSViresh Kumar { 28877813dd6fSViresh Kumar struct opp_table *opp_table; 28887813dd6fSViresh Kumar 28897813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 28907813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 28917813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 28927813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 28937813dd6fSViresh Kumar 28947813dd6fSViresh Kumar if (error != -ENODEV) 28957813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 28967813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 28977813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 28987813dd6fSViresh Kumar error); 28997813dd6fSViresh Kumar return; 29007813dd6fSViresh Kumar } 29017813dd6fSViresh Kumar 2902922ff075SViresh Kumar /* 2903922ff075SViresh Kumar * Drop the extra reference only if the OPP table was successfully added 2904922ff075SViresh Kumar * with dev_pm_opp_of_add_table() earlier. 2905922ff075SViresh Kumar **/ 2906922ff075SViresh Kumar if (_opp_remove_all_static(opp_table)) 2907cdd6ed90SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2908cdd6ed90SViresh Kumar 2909922ff075SViresh Kumar /* Drop reference taken by _find_opp_table() */ 29107813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 29117813dd6fSViresh Kumar } 29127813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 2913ce8073d8SDmitry Osipenko 2914ce8073d8SDmitry Osipenko /** 2915ce8073d8SDmitry Osipenko * dev_pm_opp_sync_regulators() - Sync state of voltage regulators 2916ce8073d8SDmitry Osipenko * @dev: device for which we do this operation 2917ce8073d8SDmitry Osipenko * 2918ce8073d8SDmitry Osipenko * Sync voltage state of the OPP table regulators. 2919ce8073d8SDmitry Osipenko * 2920ce8073d8SDmitry Osipenko * Return: 0 on success or a negative error value. 2921ce8073d8SDmitry Osipenko */ 2922ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev) 2923ce8073d8SDmitry Osipenko { 2924ce8073d8SDmitry Osipenko struct opp_table *opp_table; 2925ce8073d8SDmitry Osipenko struct regulator *reg; 2926ce8073d8SDmitry Osipenko int i, ret = 0; 2927ce8073d8SDmitry Osipenko 2928ce8073d8SDmitry Osipenko /* Device may not have OPP table */ 2929ce8073d8SDmitry Osipenko opp_table = _find_opp_table(dev); 2930ce8073d8SDmitry Osipenko if (IS_ERR(opp_table)) 2931ce8073d8SDmitry Osipenko return 0; 2932ce8073d8SDmitry Osipenko 2933ce8073d8SDmitry Osipenko /* Regulator may not be required for the device */ 2934ce8073d8SDmitry Osipenko if (unlikely(!opp_table->regulators)) 2935ce8073d8SDmitry Osipenko goto put_table; 2936ce8073d8SDmitry Osipenko 2937ce8073d8SDmitry Osipenko /* Nothing to sync if voltage wasn't changed */ 2938ce8073d8SDmitry Osipenko if (!opp_table->enabled) 2939ce8073d8SDmitry Osipenko goto put_table; 2940ce8073d8SDmitry Osipenko 2941ce8073d8SDmitry Osipenko for (i = 0; i < opp_table->regulator_count; i++) { 2942ce8073d8SDmitry Osipenko reg = opp_table->regulators[i]; 2943ce8073d8SDmitry Osipenko ret = regulator_sync_voltage(reg); 2944ce8073d8SDmitry Osipenko if (ret) 2945ce8073d8SDmitry Osipenko break; 2946ce8073d8SDmitry Osipenko } 2947ce8073d8SDmitry Osipenko put_table: 2948ce8073d8SDmitry Osipenko /* Drop reference taken by _find_opp_table() */ 2949ce8073d8SDmitry Osipenko dev_pm_opp_put_opp_table(opp_table); 2950ce8073d8SDmitry Osipenko 2951ce8073d8SDmitry Osipenko return ret; 2952ce8073d8SDmitry Osipenko } 2953ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators); 2954