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 430aab8ced2SViresh Kumar /* Helpers to read keys */ 431aab8ced2SViresh Kumar static unsigned long _read_freq(struct dev_pm_opp *opp, int index) 432aab8ced2SViresh Kumar { 433aab8ced2SViresh Kumar return opp->rate; 434aab8ced2SViresh Kumar } 435aab8ced2SViresh Kumar 436c2ab2cb6SViresh Kumar static unsigned long _read_level(struct dev_pm_opp *opp, int index) 437c2ab2cb6SViresh Kumar { 438c2ab2cb6SViresh Kumar return opp->level; 439c2ab2cb6SViresh Kumar } 440c2ab2cb6SViresh Kumar 441add1dc09SViresh Kumar static unsigned long _read_bw(struct dev_pm_opp *opp, int index) 442add1dc09SViresh Kumar { 443add1dc09SViresh Kumar return opp->bandwidth[index].peak; 444add1dc09SViresh Kumar } 445add1dc09SViresh Kumar 446aab8ced2SViresh Kumar /* Generic comparison helpers */ 447aab8ced2SViresh Kumar static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 448aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key) 449aab8ced2SViresh Kumar { 450aab8ced2SViresh Kumar if (opp_key == key) { 451aab8ced2SViresh Kumar *opp = temp_opp; 452aab8ced2SViresh Kumar return true; 453aab8ced2SViresh Kumar } 454aab8ced2SViresh Kumar 455aab8ced2SViresh Kumar return false; 456aab8ced2SViresh Kumar } 457aab8ced2SViresh Kumar 458aab8ced2SViresh Kumar static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 459aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key) 460aab8ced2SViresh Kumar { 461aab8ced2SViresh Kumar if (opp_key >= key) { 462aab8ced2SViresh Kumar *opp = temp_opp; 463aab8ced2SViresh Kumar return true; 464aab8ced2SViresh Kumar } 465aab8ced2SViresh Kumar 466aab8ced2SViresh Kumar return false; 467aab8ced2SViresh Kumar } 468aab8ced2SViresh Kumar 469aab8ced2SViresh Kumar static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 470aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key) 471aab8ced2SViresh Kumar { 472aab8ced2SViresh Kumar if (opp_key > key) 473aab8ced2SViresh Kumar return true; 474aab8ced2SViresh Kumar 475aab8ced2SViresh Kumar *opp = temp_opp; 476aab8ced2SViresh Kumar return false; 477aab8ced2SViresh Kumar } 478aab8ced2SViresh Kumar 479aab8ced2SViresh Kumar /* Generic key finding helpers */ 480aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table, 481aab8ced2SViresh Kumar unsigned long *key, int index, bool available, 482aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 483aab8ced2SViresh Kumar bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 484aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key)) 485aab8ced2SViresh Kumar { 486aab8ced2SViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 487aab8ced2SViresh Kumar 488aab8ced2SViresh Kumar mutex_lock(&opp_table->lock); 489aab8ced2SViresh Kumar 490aab8ced2SViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 491aab8ced2SViresh Kumar if (temp_opp->available == available) { 492aab8ced2SViresh Kumar if (compare(&opp, temp_opp, read(temp_opp, index), *key)) 493aab8ced2SViresh Kumar break; 494aab8ced2SViresh Kumar } 495aab8ced2SViresh Kumar } 496aab8ced2SViresh Kumar 497aab8ced2SViresh Kumar /* Increment the reference count of OPP */ 498aab8ced2SViresh Kumar if (!IS_ERR(opp)) { 499aab8ced2SViresh Kumar *key = read(opp, index); 500aab8ced2SViresh Kumar dev_pm_opp_get(opp); 501aab8ced2SViresh Kumar } 502aab8ced2SViresh Kumar 503aab8ced2SViresh Kumar mutex_unlock(&opp_table->lock); 504aab8ced2SViresh Kumar 505aab8ced2SViresh Kumar return opp; 506aab8ced2SViresh Kumar } 507aab8ced2SViresh Kumar 508aab8ced2SViresh Kumar static struct dev_pm_opp * 509aab8ced2SViresh Kumar _find_key(struct device *dev, unsigned long *key, int index, bool available, 510aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 511aab8ced2SViresh Kumar bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 512aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key)) 513aab8ced2SViresh Kumar { 514aab8ced2SViresh Kumar struct opp_table *opp_table; 515aab8ced2SViresh Kumar struct dev_pm_opp *opp; 516aab8ced2SViresh Kumar 517aab8ced2SViresh Kumar opp_table = _find_opp_table(dev); 518aab8ced2SViresh Kumar if (IS_ERR(opp_table)) { 519aab8ced2SViresh Kumar dev_err(dev, "%s: OPP table not found (%ld)\n", __func__, 520aab8ced2SViresh Kumar PTR_ERR(opp_table)); 521aab8ced2SViresh Kumar return ERR_CAST(opp_table); 522aab8ced2SViresh Kumar } 523aab8ced2SViresh Kumar 524aab8ced2SViresh Kumar opp = _opp_table_find_key(opp_table, key, index, available, read, 525aab8ced2SViresh Kumar compare); 526aab8ced2SViresh Kumar 527aab8ced2SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 528aab8ced2SViresh Kumar 529aab8ced2SViresh Kumar return opp; 530aab8ced2SViresh Kumar } 531aab8ced2SViresh Kumar 532aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_exact(struct device *dev, 533aab8ced2SViresh Kumar unsigned long key, int index, bool available, 534aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index)) 535aab8ced2SViresh Kumar { 536aab8ced2SViresh Kumar /* 537aab8ced2SViresh Kumar * The value of key will be updated here, but will be ignored as the 538aab8ced2SViresh Kumar * caller doesn't need it. 539aab8ced2SViresh Kumar */ 540aab8ced2SViresh Kumar return _find_key(dev, &key, index, available, read, _compare_exact); 541aab8ced2SViresh Kumar } 542aab8ced2SViresh Kumar 543aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table, 544aab8ced2SViresh Kumar unsigned long *key, int index, bool available, 545aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index)) 546aab8ced2SViresh Kumar { 547aab8ced2SViresh Kumar return _opp_table_find_key(opp_table, key, index, available, read, 548aab8ced2SViresh Kumar _compare_ceil); 549aab8ced2SViresh Kumar } 550aab8ced2SViresh Kumar 551aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key, 552aab8ced2SViresh Kumar int index, bool available, 553aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index)) 554aab8ced2SViresh Kumar { 555aab8ced2SViresh Kumar return _find_key(dev, key, index, available, read, _compare_ceil); 556aab8ced2SViresh Kumar } 557aab8ced2SViresh Kumar 558aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_floor(struct device *dev, 559aab8ced2SViresh Kumar unsigned long *key, int index, bool available, 560aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index)) 561aab8ced2SViresh Kumar { 562aab8ced2SViresh Kumar return _find_key(dev, key, index, available, read, _compare_floor); 563aab8ced2SViresh Kumar } 564aab8ced2SViresh Kumar 5657813dd6fSViresh Kumar /** 5667813dd6fSViresh Kumar * dev_pm_opp_find_freq_exact() - search for an exact frequency 5677813dd6fSViresh Kumar * @dev: device for which we do this operation 5687813dd6fSViresh Kumar * @freq: frequency to search for 5697813dd6fSViresh Kumar * @available: true/false - match for available opp 5707813dd6fSViresh Kumar * 5717813dd6fSViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 5727813dd6fSViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 5737813dd6fSViresh Kumar * be handled using IS_ERR. Error return values can be: 5747813dd6fSViresh Kumar * EINVAL: for bad pointer 5757813dd6fSViresh Kumar * ERANGE: no match found for search 5767813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 5777813dd6fSViresh Kumar * 5787813dd6fSViresh Kumar * Note: available is a modifier for the search. if available=true, then the 5797813dd6fSViresh Kumar * match is for exact matching frequency and is available in the stored OPP 5807813dd6fSViresh Kumar * table. if false, the match is for exact frequency which is not available. 5817813dd6fSViresh Kumar * 5827813dd6fSViresh Kumar * This provides a mechanism to enable an opp which is not available currently 5837813dd6fSViresh Kumar * or the opposite as well. 5847813dd6fSViresh Kumar * 5857813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 5867813dd6fSViresh Kumar * use. 5877813dd6fSViresh Kumar */ 5887813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 589aab8ced2SViresh Kumar unsigned long freq, bool available) 5907813dd6fSViresh Kumar { 591aab8ced2SViresh Kumar return _find_key_exact(dev, freq, 0, available, _read_freq); 5927813dd6fSViresh Kumar } 5937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 5947813dd6fSViresh Kumar 5957813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 5967813dd6fSViresh Kumar unsigned long *freq) 5977813dd6fSViresh Kumar { 598aab8ced2SViresh Kumar return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq); 5997813dd6fSViresh Kumar } 6007813dd6fSViresh Kumar 6017813dd6fSViresh Kumar /** 6027813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 6037813dd6fSViresh Kumar * @dev: device for which we do this operation 6047813dd6fSViresh Kumar * @freq: Start frequency 6057813dd6fSViresh Kumar * 6067813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 6077813dd6fSViresh Kumar * for a device. 6087813dd6fSViresh Kumar * 6097813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 6107813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 6117813dd6fSViresh Kumar * values can be: 6127813dd6fSViresh Kumar * EINVAL: for bad pointer 6137813dd6fSViresh Kumar * ERANGE: no match found for search 6147813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 6157813dd6fSViresh Kumar * 6167813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 6177813dd6fSViresh Kumar * use. 6187813dd6fSViresh Kumar */ 6197813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 6207813dd6fSViresh Kumar unsigned long *freq) 6217813dd6fSViresh Kumar { 622aab8ced2SViresh Kumar return _find_key_ceil(dev, freq, 0, true, _read_freq); 6237813dd6fSViresh Kumar } 6247813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 6257813dd6fSViresh Kumar 6267813dd6fSViresh Kumar /** 6277813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 6287813dd6fSViresh Kumar * @dev: device for which we do this operation 6297813dd6fSViresh Kumar * @freq: Start frequency 6307813dd6fSViresh Kumar * 6317813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 6327813dd6fSViresh Kumar * for a device. 6337813dd6fSViresh Kumar * 6347813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 6357813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 6367813dd6fSViresh Kumar * values can be: 6377813dd6fSViresh Kumar * EINVAL: for bad pointer 6387813dd6fSViresh Kumar * ERANGE: no match found for search 6397813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 6407813dd6fSViresh Kumar * 6417813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 6427813dd6fSViresh Kumar * use. 6437813dd6fSViresh Kumar */ 6447813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 6457813dd6fSViresh Kumar unsigned long *freq) 6467813dd6fSViresh Kumar { 647aab8ced2SViresh Kumar return _find_key_floor(dev, freq, 0, true, _read_freq); 6487813dd6fSViresh Kumar } 6497813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 6507813dd6fSViresh Kumar 6512f36bde0SAndrew-sh.Cheng /** 65222079af7SViresh Kumar * dev_pm_opp_find_level_exact() - search for an exact level 65322079af7SViresh Kumar * @dev: device for which we do this operation 65422079af7SViresh Kumar * @level: level to search for 65522079af7SViresh Kumar * 65622079af7SViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 65722079af7SViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 65822079af7SViresh Kumar * be handled using IS_ERR. Error return values can be: 65922079af7SViresh Kumar * EINVAL: for bad pointer 66022079af7SViresh Kumar * ERANGE: no match found for search 66122079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 66222079af7SViresh Kumar * 66322079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 66422079af7SViresh Kumar * use. 66522079af7SViresh Kumar */ 66622079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev, 66722079af7SViresh Kumar unsigned int level) 66822079af7SViresh Kumar { 669c2ab2cb6SViresh Kumar return _find_key_exact(dev, level, 0, true, _read_level); 67022079af7SViresh Kumar } 67122079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact); 67222079af7SViresh Kumar 67322079af7SViresh Kumar /** 67422079af7SViresh Kumar * dev_pm_opp_find_level_ceil() - search for an rounded up level 67522079af7SViresh Kumar * @dev: device for which we do this operation 67622079af7SViresh Kumar * @level: level to search for 67722079af7SViresh Kumar * 67822079af7SViresh Kumar * Return: Searches for rounded up match in the opp table and returns pointer 67922079af7SViresh Kumar * to the matching opp if found, else returns ERR_PTR in case of error and 68022079af7SViresh Kumar * should be handled using IS_ERR. Error return values can be: 68122079af7SViresh Kumar * EINVAL: for bad pointer 68222079af7SViresh Kumar * ERANGE: no match found for search 68322079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 68422079af7SViresh Kumar * 68522079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 68622079af7SViresh Kumar * use. 68722079af7SViresh Kumar */ 68822079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev, 68922079af7SViresh Kumar unsigned int *level) 69022079af7SViresh Kumar { 691c2ab2cb6SViresh Kumar unsigned long temp = *level; 692c2ab2cb6SViresh Kumar struct dev_pm_opp *opp; 69322079af7SViresh Kumar 694c2ab2cb6SViresh Kumar opp = _find_key_ceil(dev, &temp, 0, true, _read_level); 695c2ab2cb6SViresh Kumar *level = temp; 69622079af7SViresh Kumar return opp; 69722079af7SViresh Kumar } 69822079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil); 69922079af7SViresh Kumar 70022079af7SViresh Kumar /** 70100ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth 70200ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 703617df304SYang Li * @bw: start bandwidth 70400ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 70500ce3873SKrzysztof Kozlowski * 70600ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 70700ce3873SKrzysztof Kozlowski * for a device. 70800ce3873SKrzysztof Kozlowski * 70900ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 71000ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 71100ce3873SKrzysztof Kozlowski * values can be: 71200ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 71300ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 71400ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 71500ce3873SKrzysztof Kozlowski * 71600ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 71700ce3873SKrzysztof Kozlowski * use. 71800ce3873SKrzysztof Kozlowski */ 719add1dc09SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw, 720add1dc09SViresh Kumar int index) 72100ce3873SKrzysztof Kozlowski { 722add1dc09SViresh Kumar unsigned long temp = *bw; 723add1dc09SViresh Kumar struct dev_pm_opp *opp; 72400ce3873SKrzysztof Kozlowski 725add1dc09SViresh Kumar opp = _find_key_ceil(dev, &temp, index, true, _read_bw); 726add1dc09SViresh Kumar *bw = temp; 72700ce3873SKrzysztof Kozlowski return opp; 72800ce3873SKrzysztof Kozlowski } 72900ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil); 73000ce3873SKrzysztof Kozlowski 73100ce3873SKrzysztof Kozlowski /** 73200ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth 73300ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 734617df304SYang Li * @bw: start bandwidth 73500ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 73600ce3873SKrzysztof Kozlowski * 73700ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 73800ce3873SKrzysztof Kozlowski * for a device. 73900ce3873SKrzysztof Kozlowski * 74000ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 74100ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 74200ce3873SKrzysztof Kozlowski * values can be: 74300ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 74400ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 74500ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 74600ce3873SKrzysztof Kozlowski * 74700ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 74800ce3873SKrzysztof Kozlowski * use. 74900ce3873SKrzysztof Kozlowski */ 75000ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev, 75100ce3873SKrzysztof Kozlowski unsigned int *bw, int index) 75200ce3873SKrzysztof Kozlowski { 753add1dc09SViresh Kumar unsigned long temp = *bw; 754add1dc09SViresh Kumar struct dev_pm_opp *opp; 75500ce3873SKrzysztof Kozlowski 756add1dc09SViresh Kumar opp = _find_key_floor(dev, &temp, index, true, _read_bw); 757add1dc09SViresh Kumar *bw = temp; 75800ce3873SKrzysztof Kozlowski return opp; 75900ce3873SKrzysztof Kozlowski } 76000ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor); 76100ce3873SKrzysztof Kozlowski 7627813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 7637813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 7647813dd6fSViresh Kumar { 7657813dd6fSViresh Kumar int ret; 7667813dd6fSViresh Kumar 7677813dd6fSViresh Kumar /* Regulator not available for device */ 7687813dd6fSViresh Kumar if (IS_ERR(reg)) { 7697813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 7707813dd6fSViresh Kumar PTR_ERR(reg)); 7717813dd6fSViresh Kumar return 0; 7727813dd6fSViresh Kumar } 7737813dd6fSViresh Kumar 7747813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 7757813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 7767813dd6fSViresh Kumar 7777813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 7787813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 7797813dd6fSViresh Kumar if (ret) 7807813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 7817813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 7827813dd6fSViresh Kumar supply->u_volt_max, ret); 7837813dd6fSViresh Kumar 7847813dd6fSViresh Kumar return ret; 7857813dd6fSViresh Kumar } 7867813dd6fSViresh Kumar 787*1efae8d2SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, 788*1efae8d2SViresh Kumar struct opp_table *opp_table, struct dev_pm_opp *opp, void *data) 7897813dd6fSViresh Kumar { 790*1efae8d2SViresh Kumar unsigned long *target = data; 791*1efae8d2SViresh Kumar unsigned long freq; 7927813dd6fSViresh Kumar int ret; 7937813dd6fSViresh Kumar 79435e74b2eSViresh Kumar /* We may reach here for devices which don't change frequency */ 795*1efae8d2SViresh Kumar if (IS_ERR(opp_table->clk)) 79635e74b2eSViresh Kumar return 0; 79735e74b2eSViresh Kumar 798*1efae8d2SViresh Kumar /* One of target and opp must be available */ 799*1efae8d2SViresh Kumar if (target) { 800*1efae8d2SViresh Kumar freq = *target; 801*1efae8d2SViresh Kumar } else if (opp) { 802*1efae8d2SViresh Kumar freq = opp->rate; 803*1efae8d2SViresh Kumar } else { 804*1efae8d2SViresh Kumar WARN_ON(1); 805*1efae8d2SViresh Kumar return -EINVAL; 806*1efae8d2SViresh Kumar } 807*1efae8d2SViresh Kumar 808*1efae8d2SViresh Kumar ret = clk_set_rate(opp_table->clk, freq); 8097813dd6fSViresh Kumar if (ret) { 8107813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 8117813dd6fSViresh Kumar ret); 812*1efae8d2SViresh Kumar } else { 813*1efae8d2SViresh Kumar opp_table->rate_clk_single = freq; 8147813dd6fSViresh Kumar } 8157813dd6fSViresh Kumar 8167813dd6fSViresh Kumar return ret; 8177813dd6fSViresh Kumar } 8187813dd6fSViresh Kumar 819c522ce8aSViresh Kumar static int _opp_config_regulator_single(struct device *dev, 820c522ce8aSViresh Kumar struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp, 821c522ce8aSViresh Kumar struct regulator **regulators, unsigned int count) 8227813dd6fSViresh Kumar { 823c522ce8aSViresh Kumar struct regulator *reg = regulators[0]; 8247813dd6fSViresh Kumar int ret; 8257813dd6fSViresh Kumar 8267813dd6fSViresh Kumar /* This function only supports single regulator per device */ 827c522ce8aSViresh Kumar if (WARN_ON(count > 1)) { 8287813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 8297813dd6fSViresh Kumar return -EINVAL; 8307813dd6fSViresh Kumar } 8317813dd6fSViresh Kumar 832c522ce8aSViresh Kumar ret = _set_opp_voltage(dev, reg, new_opp->supplies); 8337813dd6fSViresh Kumar if (ret) 834c522ce8aSViresh Kumar return ret; 8357813dd6fSViresh Kumar 8368d45719cSKamil Konieczny /* 8378d45719cSKamil Konieczny * Enable the regulator after setting its voltages, otherwise it breaks 8388d45719cSKamil Konieczny * some boot-enabled regulators. 8398d45719cSKamil Konieczny */ 840c522ce8aSViresh Kumar if (unlikely(!new_opp->opp_table->enabled)) { 8418d45719cSKamil Konieczny ret = regulator_enable(reg); 8428d45719cSKamil Konieczny if (ret < 0) 8438d45719cSKamil Konieczny dev_warn(dev, "Failed to enable regulator: %d", ret); 8448d45719cSKamil Konieczny } 8458d45719cSKamil Konieczny 8467813dd6fSViresh Kumar return 0; 8477813dd6fSViresh Kumar } 8487813dd6fSViresh Kumar 849b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table, 850240ae50eSViresh Kumar struct dev_pm_opp *opp, struct device *dev) 851b00e667aSViresh Kumar { 852b00e667aSViresh Kumar u32 avg, peak; 853b00e667aSViresh Kumar int i, ret; 854b00e667aSViresh Kumar 855b00e667aSViresh Kumar if (!opp_table->paths) 856b00e667aSViresh Kumar return 0; 857b00e667aSViresh Kumar 858b00e667aSViresh Kumar for (i = 0; i < opp_table->path_count; i++) { 859240ae50eSViresh Kumar if (!opp) { 860b00e667aSViresh Kumar avg = 0; 861b00e667aSViresh Kumar peak = 0; 862b00e667aSViresh Kumar } else { 863b00e667aSViresh Kumar avg = opp->bandwidth[i].avg; 864b00e667aSViresh Kumar peak = opp->bandwidth[i].peak; 865b00e667aSViresh Kumar } 866b00e667aSViresh Kumar ret = icc_set_bw(opp_table->paths[i], avg, peak); 867b00e667aSViresh Kumar if (ret) { 868b00e667aSViresh Kumar dev_err(dev, "Failed to %s bandwidth[%d]: %d\n", 869240ae50eSViresh Kumar opp ? "set" : "remove", i, ret); 870b00e667aSViresh Kumar return ret; 871b00e667aSViresh Kumar } 872b00e667aSViresh Kumar } 873b00e667aSViresh Kumar 874b00e667aSViresh Kumar return 0; 875b00e667aSViresh Kumar } 876b00e667aSViresh Kumar 87760cdeae0SStephan Gerhold static int _set_required_opp(struct device *dev, struct device *pd_dev, 87860cdeae0SStephan Gerhold struct dev_pm_opp *opp, int i) 87960cdeae0SStephan Gerhold { 88060cdeae0SStephan Gerhold unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0; 88160cdeae0SStephan Gerhold int ret; 88260cdeae0SStephan Gerhold 88360cdeae0SStephan Gerhold if (!pd_dev) 88460cdeae0SStephan Gerhold return 0; 88560cdeae0SStephan Gerhold 88660cdeae0SStephan Gerhold ret = dev_pm_genpd_set_performance_state(pd_dev, pstate); 88760cdeae0SStephan Gerhold if (ret) { 8889bfb1fffSViresh Kumar dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", 88960cdeae0SStephan Gerhold dev_name(pd_dev), pstate, ret); 89060cdeae0SStephan Gerhold } 89160cdeae0SStephan Gerhold 89260cdeae0SStephan Gerhold return ret; 89360cdeae0SStephan Gerhold } 89460cdeae0SStephan Gerhold 895ca1b5d77SViresh Kumar /* This is only called for PM domain for now */ 896ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev, 897ca1b5d77SViresh Kumar struct opp_table *opp_table, 8982c59138cSStephan Gerhold struct dev_pm_opp *opp, bool up) 899ca1b5d77SViresh Kumar { 900ca1b5d77SViresh Kumar struct opp_table **required_opp_tables = opp_table->required_opp_tables; 901ca1b5d77SViresh Kumar struct device **genpd_virt_devs = opp_table->genpd_virt_devs; 902ca1b5d77SViresh Kumar int i, ret = 0; 903ca1b5d77SViresh Kumar 904ca1b5d77SViresh Kumar if (!required_opp_tables) 905ca1b5d77SViresh Kumar return 0; 906ca1b5d77SViresh Kumar 90719526d09SMarijn Suijten /* required-opps not fully initialized yet */ 90819526d09SMarijn Suijten if (lazy_linking_pending(opp_table)) 90919526d09SMarijn Suijten return -EBUSY; 91019526d09SMarijn Suijten 9114fa82a87SHsin-Yi Wang /* 9124fa82a87SHsin-Yi Wang * We only support genpd's OPPs in the "required-opps" for now, as we 9134fa82a87SHsin-Yi Wang * don't know much about other use cases. Error out if the required OPP 9144fa82a87SHsin-Yi Wang * doesn't belong to a genpd. 9154fa82a87SHsin-Yi Wang */ 9164fa82a87SHsin-Yi Wang if (unlikely(!required_opp_tables[0]->is_genpd)) { 9174fa82a87SHsin-Yi Wang dev_err(dev, "required-opps don't belong to a genpd\n"); 9184fa82a87SHsin-Yi Wang return -ENOENT; 9194fa82a87SHsin-Yi Wang } 9204fa82a87SHsin-Yi Wang 921ca1b5d77SViresh Kumar /* Single genpd case */ 92260cdeae0SStephan Gerhold if (!genpd_virt_devs) 92360cdeae0SStephan Gerhold return _set_required_opp(dev, dev, opp, 0); 924ca1b5d77SViresh Kumar 925ca1b5d77SViresh Kumar /* Multiple genpd case */ 926ca1b5d77SViresh Kumar 927ca1b5d77SViresh Kumar /* 928ca1b5d77SViresh Kumar * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 929ca1b5d77SViresh Kumar * after it is freed from another thread. 930ca1b5d77SViresh Kumar */ 931ca1b5d77SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 932ca1b5d77SViresh Kumar 9332c59138cSStephan Gerhold /* Scaling up? Set required OPPs in normal order, else reverse */ 9342c59138cSStephan Gerhold if (up) { 935ca1b5d77SViresh Kumar for (i = 0; i < opp_table->required_opp_count; i++) { 93660cdeae0SStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 93760cdeae0SStephan Gerhold if (ret) 938ca1b5d77SViresh Kumar break; 939ca1b5d77SViresh Kumar } 9402c59138cSStephan Gerhold } else { 9412c59138cSStephan Gerhold for (i = opp_table->required_opp_count - 1; i >= 0; i--) { 9422c59138cSStephan Gerhold ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 9432c59138cSStephan Gerhold if (ret) 944ca1b5d77SViresh Kumar break; 945ca1b5d77SViresh Kumar } 946ca1b5d77SViresh Kumar } 9472c59138cSStephan Gerhold 948ca1b5d77SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 949ca1b5d77SViresh Kumar 950ca1b5d77SViresh Kumar return ret; 951ca1b5d77SViresh Kumar } 952ca1b5d77SViresh Kumar 95381c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table) 95481c4d8a3SViresh Kumar { 95581c4d8a3SViresh Kumar struct dev_pm_opp *opp = ERR_PTR(-ENODEV); 95681c4d8a3SViresh Kumar unsigned long freq; 95781c4d8a3SViresh Kumar 95881c4d8a3SViresh Kumar if (!IS_ERR(opp_table->clk)) { 95981c4d8a3SViresh Kumar freq = clk_get_rate(opp_table->clk); 96081c4d8a3SViresh Kumar opp = _find_freq_ceil(opp_table, &freq); 96181c4d8a3SViresh Kumar } 96281c4d8a3SViresh Kumar 96381c4d8a3SViresh Kumar /* 96481c4d8a3SViresh Kumar * Unable to find the current OPP ? Pick the first from the list since 96581c4d8a3SViresh Kumar * it is in ascending order, otherwise rest of the code will need to 96681c4d8a3SViresh Kumar * make special checks to validate current_opp. 96781c4d8a3SViresh Kumar */ 96881c4d8a3SViresh Kumar if (IS_ERR(opp)) { 96981c4d8a3SViresh Kumar mutex_lock(&opp_table->lock); 97081c4d8a3SViresh Kumar opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node); 97181c4d8a3SViresh Kumar dev_pm_opp_get(opp); 97281c4d8a3SViresh Kumar mutex_unlock(&opp_table->lock); 97381c4d8a3SViresh Kumar } 97481c4d8a3SViresh Kumar 97581c4d8a3SViresh Kumar opp_table->current_opp = opp; 97681c4d8a3SViresh Kumar } 97781c4d8a3SViresh Kumar 9785ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table) 979f3364e17SViresh Kumar { 980f3364e17SViresh Kumar int ret; 981f3364e17SViresh Kumar 982f3364e17SViresh Kumar if (!opp_table->enabled) 983f3364e17SViresh Kumar return 0; 984f3364e17SViresh Kumar 985f3364e17SViresh Kumar /* 986f3364e17SViresh Kumar * Some drivers need to support cases where some platforms may 987f3364e17SViresh Kumar * have OPP table for the device, while others don't and 988f3364e17SViresh Kumar * opp_set_rate() just needs to behave like clk_set_rate(). 989f3364e17SViresh Kumar */ 990f3364e17SViresh Kumar if (!_get_opp_count(opp_table)) 991f3364e17SViresh Kumar return 0; 992f3364e17SViresh Kumar 993240ae50eSViresh Kumar ret = _set_opp_bw(opp_table, NULL, dev); 994f3364e17SViresh Kumar if (ret) 995f3364e17SViresh Kumar return ret; 996f3364e17SViresh Kumar 997f3364e17SViresh Kumar if (opp_table->regulators) 998f3364e17SViresh Kumar regulator_disable(opp_table->regulators[0]); 999f3364e17SViresh Kumar 10002c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, NULL, false); 1001f3364e17SViresh Kumar 1002f3364e17SViresh Kumar opp_table->enabled = false; 1003f3364e17SViresh Kumar return ret; 1004f3364e17SViresh Kumar } 1005f3364e17SViresh Kumar 1006386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table, 1007*1efae8d2SViresh Kumar struct dev_pm_opp *opp, void *clk_data, bool forced) 10087813dd6fSViresh Kumar { 1009386ba854SViresh Kumar struct dev_pm_opp *old_opp; 1010f0b88fa4SViresh Kumar int scaling_down, ret; 10117813dd6fSViresh Kumar 1012386ba854SViresh Kumar if (unlikely(!opp)) 1013386ba854SViresh Kumar return _disable_opp_table(dev, opp_table); 1014aca48b61SRajendra Nayak 101581c4d8a3SViresh Kumar /* Find the currently set OPP if we don't know already */ 101681c4d8a3SViresh Kumar if (unlikely(!opp_table->current_opp)) 101781c4d8a3SViresh Kumar _find_current_opp(dev, opp_table); 10187813dd6fSViresh Kumar 101981c4d8a3SViresh Kumar old_opp = opp_table->current_opp; 102081c4d8a3SViresh Kumar 102181c4d8a3SViresh Kumar /* Return early if nothing to do */ 1022*1efae8d2SViresh Kumar if (!forced && old_opp == opp && opp_table->enabled) { 102381c4d8a3SViresh Kumar dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__); 1024386ba854SViresh Kumar return 0; 10257813dd6fSViresh Kumar } 10267813dd6fSViresh Kumar 1027f0b88fa4SViresh Kumar dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n", 1028*1efae8d2SViresh Kumar __func__, old_opp->rate, opp->rate, old_opp->level, opp->level, 1029*1efae8d2SViresh Kumar old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0, 1030f0b88fa4SViresh Kumar opp->bandwidth ? opp->bandwidth[0].peak : 0); 1031f0b88fa4SViresh Kumar 1032f0b88fa4SViresh Kumar scaling_down = _opp_compare_key(old_opp, opp); 1033f0b88fa4SViresh Kumar if (scaling_down == -1) 1034f0b88fa4SViresh Kumar scaling_down = 0; 10357813dd6fSViresh Kumar 1036ca1b5d77SViresh Kumar /* Scaling up? Configure required OPPs before frequency */ 1037f0b88fa4SViresh Kumar if (!scaling_down) { 10382c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, opp, true); 1039870d5d96SViresh Kumar if (ret) { 1040870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1041386ba854SViresh Kumar return ret; 1042ca1b5d77SViresh Kumar } 1043ca1b5d77SViresh Kumar 1044870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1045870d5d96SViresh Kumar if (ret) { 1046870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1047870d5d96SViresh Kumar return ret; 1048870d5d96SViresh Kumar } 1049aee3352fSViresh Kumar 1050aee3352fSViresh Kumar if (opp_table->config_regulators) { 1051aee3352fSViresh Kumar ret = opp_table->config_regulators(dev, old_opp, opp, 1052aee3352fSViresh Kumar opp_table->regulators, 1053aee3352fSViresh Kumar opp_table->regulator_count); 1054aee3352fSViresh Kumar if (ret) { 1055aee3352fSViresh Kumar dev_err(dev, "Failed to set regulator voltages: %d\n", 1056aee3352fSViresh Kumar ret); 1057aee3352fSViresh Kumar return ret; 1058aee3352fSViresh Kumar } 1059aee3352fSViresh Kumar } 1060870d5d96SViresh Kumar } 1061870d5d96SViresh Kumar 1062*1efae8d2SViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table, opp, clk_data); 1063ca1b5d77SViresh Kumar if (ret) 1064870d5d96SViresh Kumar return ret; 1065870d5d96SViresh Kumar 1066870d5d96SViresh Kumar /* Scaling down? Configure required OPPs after frequency */ 1067870d5d96SViresh Kumar if (scaling_down) { 1068aee3352fSViresh Kumar if (opp_table->config_regulators) { 1069aee3352fSViresh Kumar ret = opp_table->config_regulators(dev, old_opp, opp, 1070aee3352fSViresh Kumar opp_table->regulators, 1071aee3352fSViresh Kumar opp_table->regulator_count); 1072aee3352fSViresh Kumar if (ret) { 1073aee3352fSViresh Kumar dev_err(dev, "Failed to set regulator voltages: %d\n", 1074aee3352fSViresh Kumar ret); 1075aee3352fSViresh Kumar return ret; 1076aee3352fSViresh Kumar } 1077aee3352fSViresh Kumar } 1078aee3352fSViresh Kumar 1079870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1080870d5d96SViresh Kumar if (ret) { 1081870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1082870d5d96SViresh Kumar return ret; 1083ca1b5d77SViresh Kumar } 1084ca1b5d77SViresh Kumar 1085870d5d96SViresh Kumar ret = _set_required_opps(dev, opp_table, opp, false); 1086870d5d96SViresh Kumar if (ret) { 1087870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1088870d5d96SViresh Kumar return ret; 1089870d5d96SViresh Kumar } 1090870d5d96SViresh Kumar } 1091870d5d96SViresh Kumar 109272f80ce4SViresh Kumar opp_table->enabled = true; 109381c4d8a3SViresh Kumar dev_pm_opp_put(old_opp); 109481c4d8a3SViresh Kumar 109581c4d8a3SViresh Kumar /* Make sure current_opp doesn't get freed */ 109681c4d8a3SViresh Kumar dev_pm_opp_get(opp); 109781c4d8a3SViresh Kumar opp_table->current_opp = opp; 1098fe2af402SGeorgi Djakov 1099386ba854SViresh Kumar return ret; 1100386ba854SViresh Kumar } 1101386ba854SViresh Kumar 1102386ba854SViresh Kumar /** 1103386ba854SViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 1104386ba854SViresh Kumar * @dev: device for which we do this operation 1105386ba854SViresh Kumar * @target_freq: frequency to achieve 1106386ba854SViresh Kumar * 1107386ba854SViresh Kumar * This configures the power-supplies to the levels specified by the OPP 1108386ba854SViresh Kumar * corresponding to the target_freq, and programs the clock to a value <= 1109386ba854SViresh Kumar * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax 1110386ba854SViresh Kumar * provided by the opp, should have already rounded to the target OPP's 1111386ba854SViresh Kumar * frequency. 1112386ba854SViresh Kumar */ 1113386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 1114386ba854SViresh Kumar { 1115386ba854SViresh Kumar struct opp_table *opp_table; 1116386ba854SViresh Kumar unsigned long freq = 0, temp_freq; 1117386ba854SViresh Kumar struct dev_pm_opp *opp = NULL; 1118*1efae8d2SViresh Kumar bool forced = false; 1119386ba854SViresh Kumar int ret; 1120386ba854SViresh Kumar 1121386ba854SViresh Kumar opp_table = _find_opp_table(dev); 1122386ba854SViresh Kumar if (IS_ERR(opp_table)) { 1123386ba854SViresh Kumar dev_err(dev, "%s: device's opp table doesn't exist\n", __func__); 1124386ba854SViresh Kumar return PTR_ERR(opp_table); 1125386ba854SViresh Kumar } 1126386ba854SViresh Kumar 1127386ba854SViresh Kumar if (target_freq) { 1128386ba854SViresh Kumar /* 1129386ba854SViresh Kumar * For IO devices which require an OPP on some platforms/SoCs 1130386ba854SViresh Kumar * while just needing to scale the clock on some others 1131386ba854SViresh Kumar * we look for empty OPP tables with just a clock handle and 1132386ba854SViresh Kumar * scale only the clk. This makes dev_pm_opp_set_rate() 1133386ba854SViresh Kumar * equivalent to a clk_set_rate() 1134386ba854SViresh Kumar */ 1135386ba854SViresh Kumar if (!_get_opp_count(opp_table)) { 1136*1efae8d2SViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table, NULL, 1137*1efae8d2SViresh Kumar &target_freq); 1138386ba854SViresh Kumar goto put_opp_table; 1139386ba854SViresh Kumar } 1140386ba854SViresh Kumar 1141386ba854SViresh Kumar freq = clk_round_rate(opp_table->clk, target_freq); 1142386ba854SViresh Kumar if ((long)freq <= 0) 1143386ba854SViresh Kumar freq = target_freq; 1144386ba854SViresh Kumar 1145386ba854SViresh Kumar /* 1146386ba854SViresh Kumar * The clock driver may support finer resolution of the 1147386ba854SViresh Kumar * frequencies than the OPP table, don't update the frequency we 1148386ba854SViresh Kumar * pass to clk_set_rate() here. 1149386ba854SViresh Kumar */ 1150386ba854SViresh Kumar temp_freq = freq; 1151386ba854SViresh Kumar opp = _find_freq_ceil(opp_table, &temp_freq); 1152386ba854SViresh Kumar if (IS_ERR(opp)) { 1153386ba854SViresh Kumar ret = PTR_ERR(opp); 1154386ba854SViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 1155386ba854SViresh Kumar __func__, freq, ret); 1156386ba854SViresh Kumar goto put_opp_table; 1157386ba854SViresh Kumar } 1158*1efae8d2SViresh Kumar 1159*1efae8d2SViresh Kumar /* 1160*1efae8d2SViresh Kumar * An OPP entry specifies the highest frequency at which other 1161*1efae8d2SViresh Kumar * properties of the OPP entry apply. Even if the new OPP is 1162*1efae8d2SViresh Kumar * same as the old one, we may still reach here for a different 1163*1efae8d2SViresh Kumar * value of the frequency. In such a case, do not abort but 1164*1efae8d2SViresh Kumar * configure the hardware to the desired frequency forcefully. 1165*1efae8d2SViresh Kumar */ 1166*1efae8d2SViresh Kumar forced = opp_table->rate_clk_single != target_freq; 1167386ba854SViresh Kumar } 1168386ba854SViresh Kumar 1169*1efae8d2SViresh Kumar ret = _set_opp(dev, opp_table, opp, &target_freq, forced); 1170386ba854SViresh Kumar 1171386ba854SViresh Kumar if (target_freq) 11727813dd6fSViresh Kumar dev_pm_opp_put(opp); 1173*1efae8d2SViresh Kumar 11747813dd6fSViresh Kumar put_opp_table: 11757813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 11767813dd6fSViresh Kumar return ret; 11777813dd6fSViresh Kumar } 11787813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 11797813dd6fSViresh Kumar 1180abbe3483SViresh Kumar /** 1181abbe3483SViresh Kumar * dev_pm_opp_set_opp() - Configure device for OPP 1182abbe3483SViresh Kumar * @dev: device for which we do this operation 1183abbe3483SViresh Kumar * @opp: OPP to set to 1184abbe3483SViresh Kumar * 1185abbe3483SViresh Kumar * This configures the device based on the properties of the OPP passed to this 1186abbe3483SViresh Kumar * routine. 1187abbe3483SViresh Kumar * 1188abbe3483SViresh Kumar * Return: 0 on success, a negative error number otherwise. 1189abbe3483SViresh Kumar */ 1190abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp) 1191abbe3483SViresh Kumar { 1192abbe3483SViresh Kumar struct opp_table *opp_table; 1193abbe3483SViresh Kumar int ret; 1194abbe3483SViresh Kumar 1195abbe3483SViresh Kumar opp_table = _find_opp_table(dev); 1196abbe3483SViresh Kumar if (IS_ERR(opp_table)) { 1197abbe3483SViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 1198abbe3483SViresh Kumar return PTR_ERR(opp_table); 1199abbe3483SViresh Kumar } 1200abbe3483SViresh Kumar 1201*1efae8d2SViresh Kumar ret = _set_opp(dev, opp_table, opp, NULL, false); 1202abbe3483SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1203abbe3483SViresh Kumar 1204abbe3483SViresh Kumar return ret; 1205abbe3483SViresh Kumar } 1206abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp); 1207abbe3483SViresh Kumar 12087813dd6fSViresh Kumar /* OPP-dev Helpers */ 12097813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 12107813dd6fSViresh Kumar struct opp_table *opp_table) 12117813dd6fSViresh Kumar { 12127813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 12137813dd6fSViresh Kumar list_del(&opp_dev->node); 12147813dd6fSViresh Kumar kfree(opp_dev); 12157813dd6fSViresh Kumar } 12167813dd6fSViresh Kumar 1217ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 12187813dd6fSViresh Kumar struct opp_table *opp_table) 12197813dd6fSViresh Kumar { 12207813dd6fSViresh Kumar struct opp_device *opp_dev; 12217813dd6fSViresh Kumar 12227813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 12237813dd6fSViresh Kumar if (!opp_dev) 12247813dd6fSViresh Kumar return NULL; 12257813dd6fSViresh Kumar 12267813dd6fSViresh Kumar /* Initialize opp-dev */ 12277813dd6fSViresh Kumar opp_dev->dev = dev; 12283d255699SViresh Kumar 1229ef43f01aSViresh Kumar mutex_lock(&opp_table->lock); 12307813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 1231ef43f01aSViresh Kumar mutex_unlock(&opp_table->lock); 12327813dd6fSViresh Kumar 12337813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 1234a2dea4cbSGreg Kroah-Hartman opp_debug_register(opp_dev, opp_table); 1235283d55e6SViresh Kumar 1236283d55e6SViresh Kumar return opp_dev; 1237283d55e6SViresh Kumar } 1238283d55e6SViresh Kumar 1239eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index) 12407813dd6fSViresh Kumar { 12417813dd6fSViresh Kumar struct opp_table *opp_table; 12427813dd6fSViresh Kumar struct opp_device *opp_dev; 12437813dd6fSViresh Kumar int ret; 12447813dd6fSViresh Kumar 12457813dd6fSViresh Kumar /* 12467813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 12477813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 12487813dd6fSViresh Kumar */ 12497813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 12507813dd6fSViresh Kumar if (!opp_table) 1251dd461cd9SStephan Gerhold return ERR_PTR(-ENOMEM); 12527813dd6fSViresh Kumar 12533d255699SViresh Kumar mutex_init(&opp_table->lock); 12544f018bc0SViresh Kumar mutex_init(&opp_table->genpd_virt_dev_lock); 12557813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 12567eba0c76SViresh Kumar INIT_LIST_HEAD(&opp_table->lazy); 12577813dd6fSViresh Kumar 125846f48acaSViresh Kumar /* Mark regulator count uninitialized */ 125946f48acaSViresh Kumar opp_table->regulator_count = -1; 126046f48acaSViresh Kumar 12617813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 12627813dd6fSViresh Kumar if (!opp_dev) { 1263dd461cd9SStephan Gerhold ret = -ENOMEM; 1264dd461cd9SStephan Gerhold goto err; 12657813dd6fSViresh Kumar } 12667813dd6fSViresh Kumar 1267eb7c8743SViresh Kumar _of_init_opp_table(opp_table, dev, index); 12687813dd6fSViresh Kumar 12696d3f922cSGeorgi Djakov /* Find interconnect path(s) for the device */ 12706d3f922cSGeorgi Djakov ret = dev_pm_opp_of_find_icc_paths(dev, opp_table); 1271dd461cd9SStephan Gerhold if (ret) { 1272dd461cd9SStephan Gerhold if (ret == -EPROBE_DEFER) 127332439ac7SViresh Kumar goto remove_opp_dev; 1274dd461cd9SStephan Gerhold 12756d3f922cSGeorgi Djakov dev_warn(dev, "%s: Error finding interconnect paths: %d\n", 12766d3f922cSGeorgi Djakov __func__, ret); 1277dd461cd9SStephan Gerhold } 12786d3f922cSGeorgi Djakov 12797813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 12807813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 12817813dd6fSViresh Kumar kref_init(&opp_table->kref); 12827813dd6fSViresh Kumar 12837813dd6fSViresh Kumar return opp_table; 1284dd461cd9SStephan Gerhold 1285976509bbSQuanyang Wang remove_opp_dev: 1286976509bbSQuanyang Wang _remove_opp_dev(opp_dev, opp_table); 1287dd461cd9SStephan Gerhold err: 1288dd461cd9SStephan Gerhold kfree(opp_table); 1289dd461cd9SStephan Gerhold return ERR_PTR(ret); 12907813dd6fSViresh Kumar } 12917813dd6fSViresh Kumar 12927813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 12937813dd6fSViresh Kumar { 12947813dd6fSViresh Kumar kref_get(&opp_table->kref); 12957813dd6fSViresh Kumar } 12967813dd6fSViresh Kumar 129732439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev, 129832439ac7SViresh Kumar struct opp_table *opp_table, 129932439ac7SViresh Kumar bool getclk) 130032439ac7SViresh Kumar { 1301d4a4c7a4SViresh Kumar int ret; 1302d4a4c7a4SViresh Kumar 130332439ac7SViresh Kumar /* 130432439ac7SViresh Kumar * Return early if we don't need to get clk or we have already tried it 130532439ac7SViresh Kumar * earlier. 130632439ac7SViresh Kumar */ 130732439ac7SViresh Kumar if (!getclk || IS_ERR(opp_table) || opp_table->clk) 130832439ac7SViresh Kumar return opp_table; 130932439ac7SViresh Kumar 131032439ac7SViresh Kumar /* Find clk for the device */ 131132439ac7SViresh Kumar opp_table->clk = clk_get(dev, NULL); 131232439ac7SViresh Kumar 1313d4a4c7a4SViresh Kumar ret = PTR_ERR_OR_ZERO(opp_table->clk); 1314d4a4c7a4SViresh Kumar if (!ret) 131532439ac7SViresh Kumar return opp_table; 1316d4a4c7a4SViresh Kumar 1317d4a4c7a4SViresh Kumar if (ret == -ENOENT) { 1318d4a4c7a4SViresh Kumar dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret); 1319d4a4c7a4SViresh Kumar return opp_table; 1320d4a4c7a4SViresh Kumar } 1321d4a4c7a4SViresh Kumar 1322d4a4c7a4SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1323d4a4c7a4SViresh Kumar dev_err_probe(dev, ret, "Couldn't find clock\n"); 1324d4a4c7a4SViresh Kumar 1325d4a4c7a4SViresh Kumar return ERR_PTR(ret); 132632439ac7SViresh Kumar } 132732439ac7SViresh Kumar 132827c09484SViresh Kumar /* 132927c09484SViresh Kumar * We need to make sure that the OPP table for a device doesn't get added twice, 133027c09484SViresh Kumar * if this routine gets called in parallel with the same device pointer. 133127c09484SViresh Kumar * 133227c09484SViresh Kumar * The simplest way to enforce that is to perform everything (find existing 133327c09484SViresh Kumar * table and if not found, create a new one) under the opp_table_lock, so only 133427c09484SViresh Kumar * one creator gets access to the same. But that expands the critical section 133527c09484SViresh Kumar * under the lock and may end up causing circular dependencies with frameworks 133627c09484SViresh Kumar * like debugfs, interconnect or clock framework as they may be direct or 133727c09484SViresh Kumar * indirect users of OPP core. 133827c09484SViresh Kumar * 133927c09484SViresh Kumar * And for that reason we have to go for a bit tricky implementation here, which 134027c09484SViresh Kumar * uses the opp_tables_busy flag to indicate if another creator is in the middle 134127c09484SViresh Kumar * of adding an OPP table and others should wait for it to finish. 134227c09484SViresh Kumar */ 134332439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index, 134432439ac7SViresh Kumar bool getclk) 13457813dd6fSViresh Kumar { 13467813dd6fSViresh Kumar struct opp_table *opp_table; 13477813dd6fSViresh Kumar 134827c09484SViresh Kumar again: 13497813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 13507813dd6fSViresh Kumar 13517813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 13527813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 13537813dd6fSViresh Kumar goto unlock; 13547813dd6fSViresh Kumar 135527c09484SViresh Kumar /* 135627c09484SViresh Kumar * The opp_tables list or an OPP table's dev_list is getting updated by 135727c09484SViresh Kumar * another user, wait for it to finish. 135827c09484SViresh Kumar */ 135927c09484SViresh Kumar if (unlikely(opp_tables_busy)) { 136027c09484SViresh Kumar mutex_unlock(&opp_table_lock); 136127c09484SViresh Kumar cpu_relax(); 136227c09484SViresh Kumar goto again; 136327c09484SViresh Kumar } 136427c09484SViresh Kumar 136527c09484SViresh Kumar opp_tables_busy = true; 1366283d55e6SViresh Kumar opp_table = _managed_opp(dev, index); 136727c09484SViresh Kumar 136827c09484SViresh Kumar /* Drop the lock to reduce the size of critical section */ 136927c09484SViresh Kumar mutex_unlock(&opp_table_lock); 137027c09484SViresh Kumar 1371283d55e6SViresh Kumar if (opp_table) { 1372ef43f01aSViresh Kumar if (!_add_opp_dev(dev, opp_table)) { 1373283d55e6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1374dd461cd9SStephan Gerhold opp_table = ERR_PTR(-ENOMEM); 1375283d55e6SViresh Kumar } 137627c09484SViresh Kumar 137727c09484SViresh Kumar mutex_lock(&opp_table_lock); 137827c09484SViresh Kumar } else { 137927c09484SViresh Kumar opp_table = _allocate_opp_table(dev, index); 138027c09484SViresh Kumar 138127c09484SViresh Kumar mutex_lock(&opp_table_lock); 138227c09484SViresh Kumar if (!IS_ERR(opp_table)) 138327c09484SViresh Kumar list_add(&opp_table->node, &opp_tables); 1384283d55e6SViresh Kumar } 1385283d55e6SViresh Kumar 138627c09484SViresh Kumar opp_tables_busy = false; 13877813dd6fSViresh Kumar 13887813dd6fSViresh Kumar unlock: 13897813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 13907813dd6fSViresh Kumar 139132439ac7SViresh Kumar return _update_opp_table_clk(dev, opp_table, getclk); 13927813dd6fSViresh Kumar } 1393eb7c8743SViresh Kumar 139432439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk) 1395e77dcb0bSViresh Kumar { 139632439ac7SViresh Kumar return _add_opp_table_indexed(dev, 0, getclk); 1397e77dcb0bSViresh Kumar } 1398e77dcb0bSViresh Kumar 1399eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 1400eb7c8743SViresh Kumar { 1401e77dcb0bSViresh Kumar return _find_opp_table(dev); 1402eb7c8743SViresh Kumar } 14037813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 14047813dd6fSViresh Kumar 14057813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 14067813dd6fSViresh Kumar { 14077813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 1408cdd6ed90SViresh Kumar struct opp_device *opp_dev, *temp; 14096d3f922cSGeorgi Djakov int i; 14107813dd6fSViresh Kumar 1411e0df59deSViresh Kumar /* Drop the lock as soon as we can */ 1412e0df59deSViresh Kumar list_del(&opp_table->node); 1413e0df59deSViresh Kumar mutex_unlock(&opp_table_lock); 1414e0df59deSViresh Kumar 141581c4d8a3SViresh Kumar if (opp_table->current_opp) 141681c4d8a3SViresh Kumar dev_pm_opp_put(opp_table->current_opp); 141781c4d8a3SViresh Kumar 14185d6d106fSViresh Kumar _of_clear_opp_table(opp_table); 14195d6d106fSViresh Kumar 14207813dd6fSViresh Kumar /* Release clk */ 14217813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 14227813dd6fSViresh Kumar clk_put(opp_table->clk); 14237813dd6fSViresh Kumar 14246d3f922cSGeorgi Djakov if (opp_table->paths) { 14256d3f922cSGeorgi Djakov for (i = 0; i < opp_table->path_count; i++) 14266d3f922cSGeorgi Djakov icc_put(opp_table->paths[i]); 14276d3f922cSGeorgi Djakov kfree(opp_table->paths); 14286d3f922cSGeorgi Djakov } 14296d3f922cSGeorgi Djakov 1430cdd6ed90SViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 1431cdd6ed90SViresh Kumar 1432cdd6ed90SViresh Kumar list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 14333d255699SViresh Kumar /* 1434cdd6ed90SViresh Kumar * The OPP table is getting removed, drop the performance state 1435cdd6ed90SViresh Kumar * constraints. 14363d255699SViresh Kumar */ 1437cdd6ed90SViresh Kumar if (opp_table->genpd_performance_state) 1438cdd6ed90SViresh Kumar dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 14397813dd6fSViresh Kumar 14407813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 1441cdd6ed90SViresh Kumar } 14427813dd6fSViresh Kumar 14434f018bc0SViresh Kumar mutex_destroy(&opp_table->genpd_virt_dev_lock); 14447813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 14457813dd6fSViresh Kumar kfree(opp_table); 14467813dd6fSViresh Kumar } 14477813dd6fSViresh Kumar 14487813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 14497813dd6fSViresh Kumar { 14507813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 14517813dd6fSViresh Kumar &opp_table_lock); 14527813dd6fSViresh Kumar } 14537813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 14547813dd6fSViresh Kumar 14557813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 14567813dd6fSViresh Kumar { 14577813dd6fSViresh Kumar kfree(opp); 14587813dd6fSViresh Kumar } 14597813dd6fSViresh Kumar 1460cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref) 14617813dd6fSViresh Kumar { 1462cf1fac94SViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 1463cf1fac94SViresh Kumar struct opp_table *opp_table = opp->opp_table; 1464cf1fac94SViresh Kumar 1465cf1fac94SViresh Kumar list_del(&opp->node); 1466cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1467cf1fac94SViresh Kumar 14687813dd6fSViresh Kumar /* 14697813dd6fSViresh Kumar * Notify the changes in the availability of the operable 14707813dd6fSViresh Kumar * frequency/voltage list. 14717813dd6fSViresh Kumar */ 14727813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 1473da544b61SViresh Kumar _of_opp_free_required_opps(opp_table, opp); 14747813dd6fSViresh Kumar opp_debug_remove_one(opp); 14757813dd6fSViresh Kumar kfree(opp); 14761690d8bbSViresh Kumar } 14777813dd6fSViresh Kumar 1478a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 14797813dd6fSViresh Kumar { 14807813dd6fSViresh Kumar kref_get(&opp->kref); 14817813dd6fSViresh Kumar } 14827813dd6fSViresh Kumar 14837813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 14847813dd6fSViresh Kumar { 1485cf1fac94SViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 14867813dd6fSViresh Kumar } 14877813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 14887813dd6fSViresh Kumar 14897813dd6fSViresh Kumar /** 14907813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 14917813dd6fSViresh Kumar * @dev: device for which we do this operation 14927813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 14937813dd6fSViresh Kumar * 14947813dd6fSViresh Kumar * This function removes an opp from the opp table. 14957813dd6fSViresh Kumar */ 14967813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 14977813dd6fSViresh Kumar { 149895073b72SJakob Koschel struct dev_pm_opp *opp = NULL, *iter; 14997813dd6fSViresh Kumar struct opp_table *opp_table; 15007813dd6fSViresh Kumar 15017813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 15027813dd6fSViresh Kumar if (IS_ERR(opp_table)) 15037813dd6fSViresh Kumar return; 15047813dd6fSViresh Kumar 15057813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 15067813dd6fSViresh Kumar 150795073b72SJakob Koschel list_for_each_entry(iter, &opp_table->opp_list, node) { 150895073b72SJakob Koschel if (iter->rate == freq) { 150995073b72SJakob Koschel opp = iter; 15107813dd6fSViresh Kumar break; 15117813dd6fSViresh Kumar } 15127813dd6fSViresh Kumar } 15137813dd6fSViresh Kumar 15147813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 15157813dd6fSViresh Kumar 151695073b72SJakob Koschel if (opp) { 15177813dd6fSViresh Kumar dev_pm_opp_put(opp); 15180ad8c623SViresh Kumar 15190ad8c623SViresh Kumar /* Drop the reference taken by dev_pm_opp_add() */ 15200ad8c623SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15217813dd6fSViresh Kumar } else { 15227813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 15237813dd6fSViresh Kumar __func__, freq); 15247813dd6fSViresh Kumar } 15257813dd6fSViresh Kumar 15260ad8c623SViresh Kumar /* Drop the reference taken by _find_opp_table() */ 15277813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15287813dd6fSViresh Kumar } 15297813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 15307813dd6fSViresh Kumar 1531cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table, 1532cf1fac94SViresh Kumar bool dynamic) 1533cf1fac94SViresh Kumar { 1534cf1fac94SViresh Kumar struct dev_pm_opp *opp = NULL, *temp; 1535cf1fac94SViresh Kumar 1536cf1fac94SViresh Kumar mutex_lock(&opp_table->lock); 1537cf1fac94SViresh Kumar list_for_each_entry(temp, &opp_table->opp_list, node) { 1538606a5d42SBeata Michalska /* 1539606a5d42SBeata Michalska * Refcount must be dropped only once for each OPP by OPP core, 1540606a5d42SBeata Michalska * do that with help of "removed" flag. 1541606a5d42SBeata Michalska */ 1542606a5d42SBeata Michalska if (!temp->removed && dynamic == temp->dynamic) { 1543cf1fac94SViresh Kumar opp = temp; 1544cf1fac94SViresh Kumar break; 1545cf1fac94SViresh Kumar } 1546cf1fac94SViresh Kumar } 1547cf1fac94SViresh Kumar 1548cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1549cf1fac94SViresh Kumar return opp; 1550cf1fac94SViresh Kumar } 1551cf1fac94SViresh Kumar 1552606a5d42SBeata Michalska /* 1553606a5d42SBeata Michalska * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to 1554606a5d42SBeata Michalska * happen lock less to avoid circular dependency issues. This routine must be 1555606a5d42SBeata Michalska * called without the opp_table->lock held. 1556606a5d42SBeata Michalska */ 1557606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic) 155803758d60SViresh Kumar { 1559cf1fac94SViresh Kumar struct dev_pm_opp *opp; 156003758d60SViresh Kumar 1561606a5d42SBeata Michalska while ((opp = _opp_get_next(opp_table, dynamic))) { 1562606a5d42SBeata Michalska opp->removed = true; 1563606a5d42SBeata Michalska dev_pm_opp_put(opp); 1564606a5d42SBeata Michalska 1565606a5d42SBeata Michalska /* Drop the references taken by dev_pm_opp_add() */ 1566606a5d42SBeata Michalska if (dynamic) 1567606a5d42SBeata Michalska dev_pm_opp_put_opp_table(opp_table); 1568606a5d42SBeata Michalska } 1569606a5d42SBeata Michalska } 1570606a5d42SBeata Michalska 1571606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table) 1572606a5d42SBeata Michalska { 157303758d60SViresh Kumar mutex_lock(&opp_table->lock); 157403758d60SViresh Kumar 1575922ff075SViresh Kumar if (!opp_table->parsed_static_opps) { 1576cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1577cf1fac94SViresh Kumar return false; 1578922ff075SViresh Kumar } 1579922ff075SViresh Kumar 1580cf1fac94SViresh Kumar if (--opp_table->parsed_static_opps) { 1581cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1582cf1fac94SViresh Kumar return true; 158303758d60SViresh Kumar } 158403758d60SViresh Kumar 158503758d60SViresh Kumar mutex_unlock(&opp_table->lock); 1586922ff075SViresh Kumar 1587606a5d42SBeata Michalska _opp_remove_all(opp_table, false); 1588cf1fac94SViresh Kumar return true; 158903758d60SViresh Kumar } 159003758d60SViresh Kumar 15911690d8bbSViresh Kumar /** 15921690d8bbSViresh Kumar * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 15931690d8bbSViresh Kumar * @dev: device for which we do this operation 15941690d8bbSViresh Kumar * 15951690d8bbSViresh Kumar * This function removes all dynamically created OPPs from the opp table. 15961690d8bbSViresh Kumar */ 15971690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev) 15981690d8bbSViresh Kumar { 15991690d8bbSViresh Kumar struct opp_table *opp_table; 16001690d8bbSViresh Kumar 16011690d8bbSViresh Kumar opp_table = _find_opp_table(dev); 16021690d8bbSViresh Kumar if (IS_ERR(opp_table)) 16031690d8bbSViresh Kumar return; 16041690d8bbSViresh Kumar 1605606a5d42SBeata Michalska _opp_remove_all(opp_table, true); 16061690d8bbSViresh Kumar 16071690d8bbSViresh Kumar /* Drop the reference taken by _find_opp_table() */ 16081690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16091690d8bbSViresh Kumar } 16101690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 16111690d8bbSViresh Kumar 1612d6134583SViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table) 16137813dd6fSViresh Kumar { 16147813dd6fSViresh Kumar struct dev_pm_opp *opp; 16156d3f922cSGeorgi Djakov int supply_count, supply_size, icc_size; 16167813dd6fSViresh Kumar 16177813dd6fSViresh Kumar /* Allocate space for at least one supply */ 1618d6134583SViresh Kumar supply_count = opp_table->regulator_count > 0 ? 1619d6134583SViresh Kumar opp_table->regulator_count : 1; 16206d3f922cSGeorgi Djakov supply_size = sizeof(*opp->supplies) * supply_count; 1621d6134583SViresh Kumar icc_size = sizeof(*opp->bandwidth) * opp_table->path_count; 16227813dd6fSViresh Kumar 16237813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 16246d3f922cSGeorgi Djakov opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL); 16256d3f922cSGeorgi Djakov 16267813dd6fSViresh Kumar if (!opp) 16277813dd6fSViresh Kumar return NULL; 16287813dd6fSViresh Kumar 16297813dd6fSViresh Kumar /* Put the supplies at the end of the OPP structure as an empty array */ 16307813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 16316d3f922cSGeorgi Djakov if (icc_size) 16326d3f922cSGeorgi Djakov opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count); 16337813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 16347813dd6fSViresh Kumar 16357813dd6fSViresh Kumar return opp; 16367813dd6fSViresh Kumar } 16377813dd6fSViresh Kumar 16387813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 16397813dd6fSViresh Kumar struct opp_table *opp_table) 16407813dd6fSViresh Kumar { 16417813dd6fSViresh Kumar struct regulator *reg; 16427813dd6fSViresh Kumar int i; 16437813dd6fSViresh Kumar 164490e3577bSViresh Kumar if (!opp_table->regulators) 164590e3577bSViresh Kumar return true; 164690e3577bSViresh Kumar 16477813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 16487813dd6fSViresh Kumar reg = opp_table->regulators[i]; 16497813dd6fSViresh Kumar 16507813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 16517813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 16527813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 16537813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 16547813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 16557813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 16567813dd6fSViresh Kumar return false; 16577813dd6fSViresh Kumar } 16587813dd6fSViresh Kumar } 16597813dd6fSViresh Kumar 16607813dd6fSViresh Kumar return true; 16617813dd6fSViresh Kumar } 16627813dd6fSViresh Kumar 16638bdac14bSViresh Kumar /* 16648bdac14bSViresh Kumar * Returns 16658bdac14bSViresh Kumar * 0: opp1 == opp2 16668bdac14bSViresh Kumar * 1: opp1 > opp2 16678bdac14bSViresh Kumar * -1: opp1 < opp2 16688bdac14bSViresh Kumar */ 16696c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2) 16706c591eecSSaravana Kannan { 16716c591eecSSaravana Kannan if (opp1->rate != opp2->rate) 16726c591eecSSaravana Kannan return opp1->rate < opp2->rate ? -1 : 1; 16736d3f922cSGeorgi Djakov if (opp1->bandwidth && opp2->bandwidth && 16746d3f922cSGeorgi Djakov opp1->bandwidth[0].peak != opp2->bandwidth[0].peak) 16756d3f922cSGeorgi Djakov return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1; 16766c591eecSSaravana Kannan if (opp1->level != opp2->level) 16776c591eecSSaravana Kannan return opp1->level < opp2->level ? -1 : 1; 16786c591eecSSaravana Kannan return 0; 16796c591eecSSaravana Kannan } 16806c591eecSSaravana Kannan 1681a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1682a1e8c136SViresh Kumar struct opp_table *opp_table, 1683a1e8c136SViresh Kumar struct list_head **head) 1684a1e8c136SViresh Kumar { 1685a1e8c136SViresh Kumar struct dev_pm_opp *opp; 16866c591eecSSaravana Kannan int opp_cmp; 1687a1e8c136SViresh Kumar 1688a1e8c136SViresh Kumar /* 1689a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1690a1e8c136SViresh Kumar * already present. 1691a1e8c136SViresh Kumar * 1692a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1693a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1694a1e8c136SViresh Kumar * loop. 1695a1e8c136SViresh Kumar */ 1696a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 16976c591eecSSaravana Kannan opp_cmp = _opp_compare_key(new_opp, opp); 16986c591eecSSaravana Kannan if (opp_cmp > 0) { 1699a1e8c136SViresh Kumar *head = &opp->node; 1700a1e8c136SViresh Kumar continue; 1701a1e8c136SViresh Kumar } 1702a1e8c136SViresh Kumar 17036c591eecSSaravana Kannan if (opp_cmp < 0) 1704a1e8c136SViresh Kumar return 0; 1705a1e8c136SViresh Kumar 1706a1e8c136SViresh Kumar /* Duplicate OPPs */ 1707a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1708a1e8c136SViresh Kumar __func__, opp->rate, opp->supplies[0].u_volt, 1709a1e8c136SViresh Kumar opp->available, new_opp->rate, 1710a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1711a1e8c136SViresh Kumar 1712a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1713a1e8c136SViresh Kumar return opp->available && 1714a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1715a1e8c136SViresh Kumar } 1716a1e8c136SViresh Kumar 1717a1e8c136SViresh Kumar return 0; 1718a1e8c136SViresh Kumar } 1719a1e8c136SViresh Kumar 17207eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count) 17217eba0c76SViresh Kumar { 17227eba0c76SViresh Kumar int i; 17237eba0c76SViresh Kumar 17247eba0c76SViresh Kumar for (i = 0; i < count; i++) { 17257eba0c76SViresh Kumar if (opp->required_opps[i]->available) 17267eba0c76SViresh Kumar continue; 17277eba0c76SViresh Kumar 17287eba0c76SViresh Kumar opp->available = false; 17297eba0c76SViresh Kumar pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n", 17307eba0c76SViresh Kumar __func__, opp->required_opps[i]->np, opp->rate); 17317eba0c76SViresh Kumar return; 17327eba0c76SViresh Kumar } 17337eba0c76SViresh Kumar } 17347eba0c76SViresh Kumar 17357813dd6fSViresh Kumar /* 17367813dd6fSViresh Kumar * Returns: 17377813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 17387813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 17397813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 17407813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 17417813dd6fSViresh Kumar * kernel try to initialize the OPP table. 17427813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 17437813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 17447813dd6fSViresh Kumar */ 17457813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 17464768914bSViresh Kumar struct opp_table *opp_table) 17477813dd6fSViresh Kumar { 17487813dd6fSViresh Kumar struct list_head *head; 17497813dd6fSViresh Kumar int ret; 17507813dd6fSViresh Kumar 17517813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 17527813dd6fSViresh Kumar head = &opp_table->opp_list; 17537813dd6fSViresh Kumar 1754a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1755a1e8c136SViresh Kumar if (ret) { 17567813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 17577813dd6fSViresh Kumar return ret; 17587813dd6fSViresh Kumar } 17597813dd6fSViresh Kumar 17607813dd6fSViresh Kumar list_add(&new_opp->node, head); 17617813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 17627813dd6fSViresh Kumar 17637813dd6fSViresh Kumar new_opp->opp_table = opp_table; 17647813dd6fSViresh Kumar kref_init(&new_opp->kref); 17657813dd6fSViresh Kumar 1766a2dea4cbSGreg Kroah-Hartman opp_debug_create_one(new_opp, opp_table); 17677813dd6fSViresh Kumar 17687813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 17697813dd6fSViresh Kumar new_opp->available = false; 17707813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 17717813dd6fSViresh Kumar __func__, new_opp->rate); 17727813dd6fSViresh Kumar } 17737813dd6fSViresh Kumar 17747eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 17757eba0c76SViresh Kumar if (lazy_linking_pending(opp_table)) 17767eba0c76SViresh Kumar return 0; 1777cf65948dSDmitry Osipenko 17787eba0c76SViresh Kumar _required_opps_available(new_opp, opp_table->required_opp_count); 1779cf65948dSDmitry Osipenko 17807813dd6fSViresh Kumar return 0; 17817813dd6fSViresh Kumar } 17827813dd6fSViresh Kumar 17837813dd6fSViresh Kumar /** 17847813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 17857813dd6fSViresh Kumar * @opp_table: OPP table 17867813dd6fSViresh Kumar * @dev: device for which we do this operation 17877813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 17887813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 17897813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 17907813dd6fSViresh Kumar * 17917813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 17927813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 17937813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 17947813dd6fSViresh Kumar * 17957813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 17967813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 17977813dd6fSViresh Kumar * 17987813dd6fSViresh Kumar * Return: 17997813dd6fSViresh Kumar * 0 On success OR 18007813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 18017813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 18027813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 18037813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 18047813dd6fSViresh Kumar */ 18057813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 18067813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 18077813dd6fSViresh Kumar { 18087813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 18097813dd6fSViresh Kumar unsigned long tol; 18107813dd6fSViresh Kumar int ret; 18117813dd6fSViresh Kumar 18127813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 18137813dd6fSViresh Kumar if (!new_opp) 18147813dd6fSViresh Kumar return -ENOMEM; 18157813dd6fSViresh Kumar 18167813dd6fSViresh Kumar /* populate the opp table */ 18177813dd6fSViresh Kumar new_opp->rate = freq; 18187813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 18197813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 18207813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 18217813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 18227813dd6fSViresh Kumar new_opp->available = true; 18237813dd6fSViresh Kumar new_opp->dynamic = dynamic; 18247813dd6fSViresh Kumar 18254768914bSViresh Kumar ret = _opp_add(dev, new_opp, opp_table); 18267813dd6fSViresh Kumar if (ret) { 18277813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 18287813dd6fSViresh Kumar if (ret == -EBUSY) 18297813dd6fSViresh Kumar ret = 0; 18307813dd6fSViresh Kumar goto free_opp; 18317813dd6fSViresh Kumar } 18327813dd6fSViresh Kumar 18337813dd6fSViresh Kumar /* 18347813dd6fSViresh Kumar * Notify the changes in the availability of the operable 18357813dd6fSViresh Kumar * frequency/voltage list. 18367813dd6fSViresh Kumar */ 18377813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 18387813dd6fSViresh Kumar return 0; 18397813dd6fSViresh Kumar 18407813dd6fSViresh Kumar free_opp: 18417813dd6fSViresh Kumar _opp_free(new_opp); 18427813dd6fSViresh Kumar 18437813dd6fSViresh Kumar return ret; 18447813dd6fSViresh Kumar } 18457813dd6fSViresh Kumar 18467813dd6fSViresh Kumar /** 184789f03984SViresh Kumar * _opp_set_supported_hw() - Set supported platforms 18487813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 18497813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 18507813dd6fSViresh Kumar * @count: Number of elements in the array. 18517813dd6fSViresh Kumar * 18527813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 18537813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 18547813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 18557813dd6fSViresh Kumar * property. 18567813dd6fSViresh Kumar */ 185789f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table, 18587813dd6fSViresh Kumar const u32 *versions, unsigned int count) 18597813dd6fSViresh Kumar { 186025419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 186125419de1SViresh Kumar if (opp_table->supported_hw) 186289f03984SViresh Kumar return 0; 18637813dd6fSViresh Kumar 18647813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 18657813dd6fSViresh Kumar GFP_KERNEL); 186689f03984SViresh Kumar if (!opp_table->supported_hw) 186789f03984SViresh Kumar return -ENOMEM; 18687813dd6fSViresh Kumar 18697813dd6fSViresh Kumar opp_table->supported_hw_count = count; 18707813dd6fSViresh Kumar 187189f03984SViresh Kumar return 0; 18727813dd6fSViresh Kumar } 18737813dd6fSViresh Kumar 18747813dd6fSViresh Kumar /** 187589f03984SViresh Kumar * _opp_put_supported_hw() - Releases resources blocked for supported hw 187689f03984SViresh Kumar * @opp_table: OPP table returned by _opp_set_supported_hw(). 18777813dd6fSViresh Kumar * 18787813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 187989f03984SViresh Kumar * _opp_set_supported_hw(). Until this is called, the opp_table structure 18807813dd6fSViresh Kumar * will not be freed. 18817813dd6fSViresh Kumar */ 188289f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table) 18837813dd6fSViresh Kumar { 188489f03984SViresh Kumar if (opp_table->supported_hw) { 18857813dd6fSViresh Kumar kfree(opp_table->supported_hw); 18867813dd6fSViresh Kumar opp_table->supported_hw = NULL; 18877813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 18887813dd6fSViresh Kumar } 18899c4f220fSYangtao Li } 18909c4f220fSYangtao Li 18919c4f220fSYangtao Li /** 1892298098e5SViresh Kumar * _opp_set_prop_name() - Set prop-extn name 18937813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 18947813dd6fSViresh Kumar * @name: name to postfix to properties. 18957813dd6fSViresh Kumar * 18967813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 18977813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 18987813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 18997813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 19007813dd6fSViresh Kumar */ 1901298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name) 19027813dd6fSViresh Kumar { 1903878ec1a9SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 19047813dd6fSViresh Kumar if (!opp_table->prop_name) { 1905298098e5SViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 1906298098e5SViresh Kumar if (!opp_table->prop_name) 1907298098e5SViresh Kumar return -ENOMEM; 19087813dd6fSViresh Kumar } 19097813dd6fSViresh Kumar 1910298098e5SViresh Kumar return 0; 19117813dd6fSViresh Kumar } 19127813dd6fSViresh Kumar 19137813dd6fSViresh Kumar /** 1914298098e5SViresh Kumar * _opp_put_prop_name() - Releases resources blocked for prop-name 1915298098e5SViresh Kumar * @opp_table: OPP table returned by _opp_set_prop_name(). 19167813dd6fSViresh Kumar * 19177813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 1918298098e5SViresh Kumar * _opp_set_prop_name(). Until this is called, the opp_table structure 19197813dd6fSViresh Kumar * will not be freed. 19207813dd6fSViresh Kumar */ 1921298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table) 19227813dd6fSViresh Kumar { 1923298098e5SViresh Kumar if (opp_table->prop_name) { 19247813dd6fSViresh Kumar kfree(opp_table->prop_name); 19257813dd6fSViresh Kumar opp_table->prop_name = NULL; 19267813dd6fSViresh Kumar } 1927298098e5SViresh Kumar } 19287813dd6fSViresh Kumar 19297813dd6fSViresh Kumar /** 1930b0ec0942SViresh Kumar * _opp_set_regulators() - Set regulator names for the device 19317813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 19327813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 19337813dd6fSViresh Kumar * @count: Number of regulators. 19347813dd6fSViresh Kumar * 19357813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 19367813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 19377813dd6fSViresh Kumar * well. 19387813dd6fSViresh Kumar * 19397813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 19407813dd6fSViresh Kumar */ 1941b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev, 194287686cc8SViresh Kumar const char * const names[]) 19437813dd6fSViresh Kumar { 194487686cc8SViresh Kumar const char * const *temp = names; 19457813dd6fSViresh Kumar struct regulator *reg; 194687686cc8SViresh Kumar int count = 0, ret, i; 194787686cc8SViresh Kumar 194887686cc8SViresh Kumar /* Count number of regulators */ 194987686cc8SViresh Kumar while (*temp++) 195087686cc8SViresh Kumar count++; 195187686cc8SViresh Kumar 195287686cc8SViresh Kumar if (!count) 1953b0ec0942SViresh Kumar return -EINVAL; 19547813dd6fSViresh Kumar 1955779b783cSViresh Kumar /* Another CPU that shares the OPP table has set the regulators ? */ 1956779b783cSViresh Kumar if (opp_table->regulators) 1957b0ec0942SViresh Kumar return 0; 19587813dd6fSViresh Kumar 19597813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 19607813dd6fSViresh Kumar sizeof(*opp_table->regulators), 19617813dd6fSViresh Kumar GFP_KERNEL); 1962b0ec0942SViresh Kumar if (!opp_table->regulators) 1963b0ec0942SViresh Kumar return -ENOMEM; 19647813dd6fSViresh Kumar 19657813dd6fSViresh Kumar for (i = 0; i < count; i++) { 19667813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 19677813dd6fSViresh Kumar if (IS_ERR(reg)) { 1968543256d2SKrzysztof Kozlowski ret = dev_err_probe(dev, PTR_ERR(reg), 1969543256d2SKrzysztof Kozlowski "%s: no regulator (%s) found\n", 1970543256d2SKrzysztof Kozlowski __func__, names[i]); 19717813dd6fSViresh Kumar goto free_regulators; 19727813dd6fSViresh Kumar } 19737813dd6fSViresh Kumar 19747813dd6fSViresh Kumar opp_table->regulators[i] = reg; 19757813dd6fSViresh Kumar } 19767813dd6fSViresh Kumar 19777813dd6fSViresh Kumar opp_table->regulator_count = count; 19787813dd6fSViresh Kumar 1979c522ce8aSViresh Kumar /* Set generic config_regulators() for single regulators here */ 1980c522ce8aSViresh Kumar if (count == 1) 1981c522ce8aSViresh Kumar opp_table->config_regulators = _opp_config_regulator_single; 1982c522ce8aSViresh Kumar 1983b0ec0942SViresh Kumar return 0; 19847813dd6fSViresh Kumar 19857813dd6fSViresh Kumar free_regulators: 198624957db1SMarek Szyprowski while (i != 0) 198724957db1SMarek Szyprowski regulator_put(opp_table->regulators[--i]); 19887813dd6fSViresh Kumar 19897813dd6fSViresh Kumar kfree(opp_table->regulators); 19907813dd6fSViresh Kumar opp_table->regulators = NULL; 199146f48acaSViresh Kumar opp_table->regulator_count = -1; 19927813dd6fSViresh Kumar 1993b0ec0942SViresh Kumar return ret; 19947813dd6fSViresh Kumar } 19957813dd6fSViresh Kumar 19967813dd6fSViresh Kumar /** 1997b0ec0942SViresh Kumar * _opp_put_regulators() - Releases resources blocked for regulator 1998b0ec0942SViresh Kumar * @opp_table: OPP table returned from _opp_set_regulators(). 19997813dd6fSViresh Kumar */ 2000b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table) 20017813dd6fSViresh Kumar { 20027813dd6fSViresh Kumar int i; 20037813dd6fSViresh Kumar 2004779b783cSViresh Kumar if (!opp_table->regulators) 2005b0ec0942SViresh Kumar return; 20067813dd6fSViresh Kumar 200772f80ce4SViresh Kumar if (opp_table->enabled) { 20088d45719cSKamil Konieczny for (i = opp_table->regulator_count - 1; i >= 0; i--) 20098d45719cSKamil Konieczny regulator_disable(opp_table->regulators[i]); 20108d45719cSKamil Konieczny } 20118d45719cSKamil Konieczny 201224957db1SMarek Szyprowski for (i = opp_table->regulator_count - 1; i >= 0; i--) 20137813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 20147813dd6fSViresh Kumar 20157813dd6fSViresh Kumar kfree(opp_table->regulators); 20167813dd6fSViresh Kumar opp_table->regulators = NULL; 201746f48acaSViresh Kumar opp_table->regulator_count = -1; 20187813dd6fSViresh Kumar } 201932aee78bSYangtao Li 20207813dd6fSViresh Kumar /** 20212368f576SViresh Kumar * _opp_set_clknames() - Set clk names for the device 20222368f576SViresh Kumar * @dev: Device for which clk names is being set. 20232368f576SViresh Kumar * @names: Clk names. 20247813dd6fSViresh Kumar * 20252368f576SViresh Kumar * In order to support OPP switching, OPP layer needs to get pointers to the 20262368f576SViresh Kumar * clocks for the device. Simple cases work fine without using this routine 20272368f576SViresh Kumar * (i.e. by passing connection-id as NULL), but for a device with multiple 20282368f576SViresh Kumar * clocks available, the OPP core needs to know the exact names of the clks to 20292368f576SViresh Kumar * use. 20307813dd6fSViresh Kumar * 20317813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 20327813dd6fSViresh Kumar */ 20332368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev, 20342368f576SViresh Kumar const char * const names[]) 20357813dd6fSViresh Kumar { 20362368f576SViresh Kumar const char * const *temp = names; 20372368f576SViresh Kumar int count = 0; 20387813dd6fSViresh Kumar 20392368f576SViresh Kumar /* Count number of clks */ 20402368f576SViresh Kumar while (*temp++) 20412368f576SViresh Kumar count++; 20427813dd6fSViresh Kumar 20432368f576SViresh Kumar /* 20442368f576SViresh Kumar * This is a special case where we have a single clock, whose connection 20452368f576SViresh Kumar * id name is NULL, i.e. first two entries are NULL in the array. 20462368f576SViresh Kumar */ 20472368f576SViresh Kumar if (!count && !names[1]) 20482368f576SViresh Kumar count = 1; 20492368f576SViresh Kumar 20502368f576SViresh Kumar /* We support only one clock name for now */ 20512368f576SViresh Kumar if (count != 1) 20522368f576SViresh Kumar return -EINVAL; 20537813dd6fSViresh Kumar 20540a43452bSViresh Kumar /* Another CPU that shares the OPP table has set the clkname ? */ 20550a43452bSViresh Kumar if (opp_table->clk_configured) 20562368f576SViresh Kumar return 0; 20570a43452bSViresh Kumar 205832439ac7SViresh Kumar /* clk shouldn't be initialized at this point */ 20592368f576SViresh Kumar if (WARN_ON(opp_table->clk)) 20602368f576SViresh Kumar return -EBUSY; 20617813dd6fSViresh Kumar 20627813dd6fSViresh Kumar /* Find clk for the device */ 20632368f576SViresh Kumar opp_table->clk = clk_get(dev, names[0]); 20647813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 20652368f576SViresh Kumar return dev_err_probe(dev, PTR_ERR(opp_table->clk), 2066543256d2SKrzysztof Kozlowski "%s: Couldn't find clock\n", __func__); 20677813dd6fSViresh Kumar } 20687813dd6fSViresh Kumar 20690a43452bSViresh Kumar opp_table->clk_configured = true; 20700a43452bSViresh Kumar 20712368f576SViresh Kumar return 0; 20727813dd6fSViresh Kumar } 20737813dd6fSViresh Kumar 20747813dd6fSViresh Kumar /** 20752368f576SViresh Kumar * _opp_put_clknames() - Releases resources blocked for clks. 20762368f576SViresh Kumar * @opp_table: OPP table returned from _opp_set_clknames(). 20777813dd6fSViresh Kumar */ 20782368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table) 20797813dd6fSViresh Kumar { 20802368f576SViresh Kumar if (opp_table->clk_configured) { 20817813dd6fSViresh Kumar clk_put(opp_table->clk); 20827813dd6fSViresh Kumar opp_table->clk = ERR_PTR(-EINVAL); 20830a43452bSViresh Kumar opp_table->clk_configured = false; 20847813dd6fSViresh Kumar } 2085a74f681cSYangtao Li } 2086a74f681cSYangtao Li 2087a74f681cSYangtao Li /** 2088aee3352fSViresh Kumar * _opp_set_config_regulators_helper() - Register custom set regulator helper. 2089aee3352fSViresh Kumar * @dev: Device for which the helper is getting registered. 2090aee3352fSViresh Kumar * @config_regulators: Custom set regulator helper. 2091aee3352fSViresh Kumar * 2092aee3352fSViresh Kumar * This is useful to support platforms with multiple regulators per device. 2093aee3352fSViresh Kumar * 2094aee3352fSViresh Kumar * This must be called before any OPPs are initialized for the device. 2095aee3352fSViresh Kumar */ 2096aee3352fSViresh Kumar static int _opp_set_config_regulators_helper(struct opp_table *opp_table, 2097aee3352fSViresh Kumar struct device *dev, config_regulators_t config_regulators) 2098aee3352fSViresh Kumar { 2099aee3352fSViresh Kumar /* Another CPU that shares the OPP table has set the helper ? */ 2100aee3352fSViresh Kumar if (!opp_table->config_regulators) 2101aee3352fSViresh Kumar opp_table->config_regulators = config_regulators; 2102aee3352fSViresh Kumar 2103aee3352fSViresh Kumar return 0; 2104aee3352fSViresh Kumar } 2105aee3352fSViresh Kumar 2106aee3352fSViresh Kumar /** 2107aee3352fSViresh Kumar * _opp_put_config_regulators_helper() - Releases resources blocked for 2108aee3352fSViresh Kumar * config_regulators helper. 2109aee3352fSViresh Kumar * @opp_table: OPP table returned from _opp_set_config_regulators_helper(). 2110aee3352fSViresh Kumar * 2111aee3352fSViresh Kumar * Release resources blocked for platform specific config_regulators helper. 2112aee3352fSViresh Kumar */ 2113aee3352fSViresh Kumar static void _opp_put_config_regulators_helper(struct opp_table *opp_table) 2114aee3352fSViresh Kumar { 2115aee3352fSViresh Kumar if (opp_table->config_regulators) 2116aee3352fSViresh Kumar opp_table->config_regulators = NULL; 2117aee3352fSViresh Kumar } 2118aee3352fSViresh Kumar 2119442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table) 21206319aee1SViresh Kumar { 21216319aee1SViresh Kumar int index; 21226319aee1SViresh Kumar 2123cb60e960SViresh Kumar if (!opp_table->genpd_virt_devs) 2124cb60e960SViresh Kumar return; 2125cb60e960SViresh Kumar 21266319aee1SViresh Kumar for (index = 0; index < opp_table->required_opp_count; index++) { 21276319aee1SViresh Kumar if (!opp_table->genpd_virt_devs[index]) 21286319aee1SViresh Kumar continue; 21296319aee1SViresh Kumar 21306319aee1SViresh Kumar dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 21316319aee1SViresh Kumar opp_table->genpd_virt_devs[index] = NULL; 21326319aee1SViresh Kumar } 2133c0ab9e08SViresh Kumar 2134c0ab9e08SViresh Kumar kfree(opp_table->genpd_virt_devs); 2135c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = NULL; 21366319aee1SViresh Kumar } 21376319aee1SViresh Kumar 21387813dd6fSViresh Kumar /** 2139442e7a17SViresh Kumar * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 21406319aee1SViresh Kumar * @dev: Consumer device for which the genpd is getting attached. 21416319aee1SViresh Kumar * @names: Null terminated array of pointers containing names of genpd to attach. 214217a8f868SViresh Kumar * @virt_devs: Pointer to return the array of virtual devices. 21434f018bc0SViresh Kumar * 21444f018bc0SViresh Kumar * Multiple generic power domains for a device are supported with the help of 21454f018bc0SViresh Kumar * virtual genpd devices, which are created for each consumer device - genpd 21464f018bc0SViresh Kumar * pair. These are the device structures which are attached to the power domain 21474f018bc0SViresh Kumar * and are required by the OPP core to set the performance state of the genpd. 21486319aee1SViresh Kumar * The same API also works for the case where single genpd is available and so 21496319aee1SViresh Kumar * we don't need to support that separately. 21504f018bc0SViresh Kumar * 21514f018bc0SViresh Kumar * This helper will normally be called by the consumer driver of the device 21526319aee1SViresh Kumar * "dev", as only that has details of the genpd names. 21534f018bc0SViresh Kumar * 21546319aee1SViresh Kumar * This helper needs to be called once with a list of all genpd to attach. 21556319aee1SViresh Kumar * Otherwise the original device structure will be used instead by the OPP core. 2156baea35e4SViresh Kumar * 2157baea35e4SViresh Kumar * The order of entries in the names array must match the order in which 2158baea35e4SViresh Kumar * "required-opps" are added in DT. 21594f018bc0SViresh Kumar */ 2160442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev, 21613734b9f2SDmitry Osipenko const char * const *names, struct device ***virt_devs) 21624f018bc0SViresh Kumar { 21636319aee1SViresh Kumar struct device *virt_dev; 2164baea35e4SViresh Kumar int index = 0, ret = -EINVAL; 21653734b9f2SDmitry Osipenko const char * const *name = names; 21664f018bc0SViresh Kumar 2167cb60e960SViresh Kumar if (opp_table->genpd_virt_devs) 2168442e7a17SViresh Kumar return 0; 21694f018bc0SViresh Kumar 21706319aee1SViresh Kumar /* 21716319aee1SViresh Kumar * If the genpd's OPP table isn't already initialized, parsing of the 21726319aee1SViresh Kumar * required-opps fail for dev. We should retry this after genpd's OPP 21736319aee1SViresh Kumar * table is added. 21746319aee1SViresh Kumar */ 2175442e7a17SViresh Kumar if (!opp_table->required_opp_count) 2176442e7a17SViresh Kumar return -EPROBE_DEFER; 21776319aee1SViresh Kumar 21784f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 21794f018bc0SViresh Kumar 2180c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, 2181c0ab9e08SViresh Kumar sizeof(*opp_table->genpd_virt_devs), 2182c0ab9e08SViresh Kumar GFP_KERNEL); 2183c0ab9e08SViresh Kumar if (!opp_table->genpd_virt_devs) 2184c0ab9e08SViresh Kumar goto unlock; 21854f018bc0SViresh Kumar 21866319aee1SViresh Kumar while (*name) { 21876319aee1SViresh Kumar if (index >= opp_table->required_opp_count) { 21886319aee1SViresh Kumar dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 21896319aee1SViresh Kumar *name, opp_table->required_opp_count, index); 21906319aee1SViresh Kumar goto err; 21916319aee1SViresh Kumar } 21924f018bc0SViresh Kumar 21936319aee1SViresh Kumar virt_dev = dev_pm_domain_attach_by_name(dev, *name); 21944ea9496cSTang Bin if (IS_ERR_OR_NULL(virt_dev)) { 21954ea9496cSTang Bin ret = PTR_ERR(virt_dev) ? : -ENODEV; 21966319aee1SViresh Kumar dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 21976319aee1SViresh Kumar goto err; 21984f018bc0SViresh Kumar } 21994f018bc0SViresh Kumar 22004f018bc0SViresh Kumar opp_table->genpd_virt_devs[index] = virt_dev; 2201baea35e4SViresh Kumar index++; 22026319aee1SViresh Kumar name++; 22036319aee1SViresh Kumar } 22046319aee1SViresh Kumar 220517a8f868SViresh Kumar if (virt_devs) 220617a8f868SViresh Kumar *virt_devs = opp_table->genpd_virt_devs; 22074f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 22084f018bc0SViresh Kumar 2209442e7a17SViresh Kumar return 0; 22106319aee1SViresh Kumar 22116319aee1SViresh Kumar err: 2212442e7a17SViresh Kumar _detach_genpd(opp_table); 2213c0ab9e08SViresh Kumar unlock: 22146319aee1SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 2215442e7a17SViresh Kumar return ret; 22166319aee1SViresh Kumar 22174f018bc0SViresh Kumar } 22184f018bc0SViresh Kumar 22194f018bc0SViresh Kumar /** 2220442e7a17SViresh Kumar * _opp_detach_genpd() - Detach genpd(s) from the device. 2221442e7a17SViresh Kumar * @opp_table: OPP table returned by _opp_attach_genpd(). 22224f018bc0SViresh Kumar * 22236319aee1SViresh Kumar * This detaches the genpd(s), resets the virtual device pointers, and puts the 22246319aee1SViresh Kumar * OPP table. 22254f018bc0SViresh Kumar */ 2226442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table) 22274f018bc0SViresh Kumar { 22284f018bc0SViresh Kumar /* 22294f018bc0SViresh Kumar * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 22304f018bc0SViresh Kumar * used in parallel. 22314f018bc0SViresh Kumar */ 22324f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 2233442e7a17SViresh Kumar _detach_genpd(opp_table); 22344f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 22354f018bc0SViresh Kumar } 2236b4b9e223SDmitry Osipenko 223711b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data) 223811b9b663SViresh Kumar { 223911b9b663SViresh Kumar if (data->flags & OPP_CONFIG_GENPD) 2240442e7a17SViresh Kumar _opp_detach_genpd(data->opp_table); 224111b9b663SViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR) 2242b0ec0942SViresh Kumar _opp_put_regulators(data->opp_table); 224311b9b663SViresh Kumar if (data->flags & OPP_CONFIG_SUPPORTED_HW) 224489f03984SViresh Kumar _opp_put_supported_hw(data->opp_table); 22451f378c6eSViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR_HELPER) 2246aee3352fSViresh Kumar _opp_put_config_regulators_helper(data->opp_table); 224711b9b663SViresh Kumar if (data->flags & OPP_CONFIG_PROP_NAME) 2248298098e5SViresh Kumar _opp_put_prop_name(data->opp_table); 224911b9b663SViresh Kumar if (data->flags & OPP_CONFIG_CLK) 22502368f576SViresh Kumar _opp_put_clknames(data->opp_table); 225111b9b663SViresh Kumar 225211b9b663SViresh Kumar dev_pm_opp_put_opp_table(data->opp_table); 225311b9b663SViresh Kumar kfree(data); 225411b9b663SViresh Kumar } 225511b9b663SViresh Kumar 225611b9b663SViresh Kumar /** 225711b9b663SViresh Kumar * dev_pm_opp_set_config() - Set OPP configuration for the device. 225811b9b663SViresh Kumar * @dev: Device for which configuration is being set. 225911b9b663SViresh Kumar * @config: OPP configuration. 226011b9b663SViresh Kumar * 226111b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 226211b9b663SViresh Kumar * 226311b9b663SViresh Kumar * This must be called before any OPPs are initialized for the device. This may 226411b9b663SViresh Kumar * be called multiple times for the same OPP table, for example once for each 226511b9b663SViresh Kumar * CPU that share the same table. This must be balanced by the same number of 226611b9b663SViresh Kumar * calls to dev_pm_opp_clear_config() in order to free the OPP table properly. 226711b9b663SViresh Kumar * 226811b9b663SViresh Kumar * This returns a token to the caller, which must be passed to 226911b9b663SViresh Kumar * dev_pm_opp_clear_config() to free the resources later. The value of the 227011b9b663SViresh Kumar * returned token will be >= 1 for success and negative for errors. The minimum 227111b9b663SViresh Kumar * value of 1 is chosen here to make it easy for callers to manage the resource. 227211b9b663SViresh Kumar */ 227311b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 227411b9b663SViresh Kumar { 2275298098e5SViresh Kumar struct opp_table *opp_table; 227611b9b663SViresh Kumar struct opp_config_data *data; 227711b9b663SViresh Kumar unsigned int id; 227811b9b663SViresh Kumar int ret; 227911b9b663SViresh Kumar 228011b9b663SViresh Kumar data = kmalloc(sizeof(*data), GFP_KERNEL); 228111b9b663SViresh Kumar if (!data) 228211b9b663SViresh Kumar return -ENOMEM; 228311b9b663SViresh Kumar 228411b9b663SViresh Kumar opp_table = _add_opp_table(dev, false); 228511b9b663SViresh Kumar if (IS_ERR(opp_table)) { 228611b9b663SViresh Kumar kfree(data); 228711b9b663SViresh Kumar return PTR_ERR(opp_table); 228811b9b663SViresh Kumar } 228911b9b663SViresh Kumar 229011b9b663SViresh Kumar data->opp_table = opp_table; 229111b9b663SViresh Kumar data->flags = 0; 229211b9b663SViresh Kumar 229311b9b663SViresh Kumar /* This should be called before OPPs are initialized */ 229411b9b663SViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 229511b9b663SViresh Kumar ret = -EBUSY; 229611b9b663SViresh Kumar goto err; 229711b9b663SViresh Kumar } 229811b9b663SViresh Kumar 229911b9b663SViresh Kumar /* Configure clocks */ 230011b9b663SViresh Kumar if (config->clk_names) { 23012368f576SViresh Kumar ret = _opp_set_clknames(opp_table, dev, config->clk_names); 23022368f576SViresh Kumar if (ret) 230311b9b663SViresh Kumar goto err; 230411b9b663SViresh Kumar 230511b9b663SViresh Kumar data->flags |= OPP_CONFIG_CLK; 230611b9b663SViresh Kumar } 230711b9b663SViresh Kumar 230811b9b663SViresh Kumar /* Configure property names */ 230911b9b663SViresh Kumar if (config->prop_name) { 2310298098e5SViresh Kumar ret = _opp_set_prop_name(opp_table, config->prop_name); 2311298098e5SViresh Kumar if (ret) 231211b9b663SViresh Kumar goto err; 231311b9b663SViresh Kumar 231411b9b663SViresh Kumar data->flags |= OPP_CONFIG_PROP_NAME; 231511b9b663SViresh Kumar } 231611b9b663SViresh Kumar 2317aee3352fSViresh Kumar /* Configure config_regulators helper */ 2318aee3352fSViresh Kumar if (config->config_regulators) { 2319aee3352fSViresh Kumar ret = _opp_set_config_regulators_helper(opp_table, dev, 2320aee3352fSViresh Kumar config->config_regulators); 2321aee3352fSViresh Kumar if (ret) 2322aee3352fSViresh Kumar goto err; 2323aee3352fSViresh Kumar 2324aee3352fSViresh Kumar data->flags |= OPP_CONFIG_REGULATOR_HELPER; 2325aee3352fSViresh Kumar } 2326aee3352fSViresh Kumar 232711b9b663SViresh Kumar /* Configure supported hardware */ 232811b9b663SViresh Kumar if (config->supported_hw) { 232989f03984SViresh Kumar ret = _opp_set_supported_hw(opp_table, config->supported_hw, 233011b9b663SViresh Kumar config->supported_hw_count); 233189f03984SViresh Kumar if (ret) 233211b9b663SViresh Kumar goto err; 233311b9b663SViresh Kumar 233411b9b663SViresh Kumar data->flags |= OPP_CONFIG_SUPPORTED_HW; 233511b9b663SViresh Kumar } 233611b9b663SViresh Kumar 233711b9b663SViresh Kumar /* Configure supplies */ 233811b9b663SViresh Kumar if (config->regulator_names) { 2339b0ec0942SViresh Kumar ret = _opp_set_regulators(opp_table, dev, 2340b0ec0942SViresh Kumar config->regulator_names); 2341b0ec0942SViresh Kumar if (ret) 234211b9b663SViresh Kumar goto err; 234311b9b663SViresh Kumar 234411b9b663SViresh Kumar data->flags |= OPP_CONFIG_REGULATOR; 234511b9b663SViresh Kumar } 234611b9b663SViresh Kumar 234711b9b663SViresh Kumar /* Attach genpds */ 234811b9b663SViresh Kumar if (config->genpd_names) { 2349442e7a17SViresh Kumar ret = _opp_attach_genpd(opp_table, dev, config->genpd_names, 235011b9b663SViresh Kumar config->virt_devs); 2351442e7a17SViresh Kumar if (ret) 235211b9b663SViresh Kumar goto err; 235311b9b663SViresh Kumar 235411b9b663SViresh Kumar data->flags |= OPP_CONFIG_GENPD; 235511b9b663SViresh Kumar } 235611b9b663SViresh Kumar 235711b9b663SViresh Kumar ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX), 235811b9b663SViresh Kumar GFP_KERNEL); 235911b9b663SViresh Kumar if (ret) 236011b9b663SViresh Kumar goto err; 236111b9b663SViresh Kumar 236211b9b663SViresh Kumar return id; 236311b9b663SViresh Kumar 236411b9b663SViresh Kumar err: 236511b9b663SViresh Kumar _opp_clear_config(data); 236611b9b663SViresh Kumar return ret; 236711b9b663SViresh Kumar } 236811b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config); 236911b9b663SViresh Kumar 237011b9b663SViresh Kumar /** 237111b9b663SViresh Kumar * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration. 237211b9b663SViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_config(). 237311b9b663SViresh Kumar * 237411b9b663SViresh Kumar * This allows all device OPP configurations to be cleared at once. This must be 237511b9b663SViresh Kumar * called once for each call made to dev_pm_opp_set_config(), in order to free 237611b9b663SViresh Kumar * the OPPs properly. 237711b9b663SViresh Kumar * 237811b9b663SViresh Kumar * Currently the first call itself ends up freeing all the OPP configurations, 237911b9b663SViresh Kumar * while the later ones only drop the OPP table reference. This works well for 238011b9b663SViresh Kumar * now as we would never want to use an half initialized OPP table and want to 238111b9b663SViresh Kumar * remove the configurations together. 238211b9b663SViresh Kumar */ 238311b9b663SViresh Kumar void dev_pm_opp_clear_config(int token) 238411b9b663SViresh Kumar { 238511b9b663SViresh Kumar struct opp_config_data *data; 238611b9b663SViresh Kumar 238711b9b663SViresh Kumar /* 238811b9b663SViresh Kumar * This lets the callers call this unconditionally and keep their code 238911b9b663SViresh Kumar * simple. 239011b9b663SViresh Kumar */ 239111b9b663SViresh Kumar if (unlikely(token <= 0)) 239211b9b663SViresh Kumar return; 239311b9b663SViresh Kumar 239411b9b663SViresh Kumar data = xa_erase(&opp_configs, token); 239511b9b663SViresh Kumar if (WARN_ON(!data)) 239611b9b663SViresh Kumar return; 239711b9b663SViresh Kumar 239811b9b663SViresh Kumar _opp_clear_config(data); 239911b9b663SViresh Kumar } 240011b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config); 240111b9b663SViresh Kumar 240211b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token) 240311b9b663SViresh Kumar { 240411b9b663SViresh Kumar dev_pm_opp_clear_config((unsigned long)token); 240511b9b663SViresh Kumar } 240611b9b663SViresh Kumar 240711b9b663SViresh Kumar /** 240811b9b663SViresh Kumar * devm_pm_opp_set_config() - Set OPP configuration for the device. 240911b9b663SViresh Kumar * @dev: Device for which configuration is being set. 241011b9b663SViresh Kumar * @config: OPP configuration. 241111b9b663SViresh Kumar * 241211b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 241311b9b663SViresh Kumar * This is a resource-managed variant of dev_pm_opp_set_config(). 241411b9b663SViresh Kumar * 241511b9b663SViresh Kumar * Return: 0 on success and errorno otherwise. 241611b9b663SViresh Kumar */ 241711b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 241811b9b663SViresh Kumar { 241911b9b663SViresh Kumar int token = dev_pm_opp_set_config(dev, config); 242011b9b663SViresh Kumar 242111b9b663SViresh Kumar if (token < 0) 242211b9b663SViresh Kumar return token; 242311b9b663SViresh Kumar 242411b9b663SViresh Kumar return devm_add_action_or_reset(dev, devm_pm_opp_config_release, 242511b9b663SViresh Kumar (void *) ((unsigned long) token)); 242611b9b663SViresh Kumar } 242711b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config); 242811b9b663SViresh Kumar 24294f018bc0SViresh Kumar /** 24307d8658efSSaravana Kannan * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP. 24317d8658efSSaravana Kannan * @src_table: OPP table which has @dst_table as one of its required OPP table. 24327d8658efSSaravana Kannan * @dst_table: Required OPP table of the @src_table. 24337d8658efSSaravana Kannan * @src_opp: OPP from the @src_table. 24347d8658efSSaravana Kannan * 24357d8658efSSaravana Kannan * This function returns the OPP (present in @dst_table) pointed out by the 24367d8658efSSaravana Kannan * "required-opps" property of the @src_opp (present in @src_table). 24377d8658efSSaravana Kannan * 24387d8658efSSaravana Kannan * The callers are required to call dev_pm_opp_put() for the returned OPP after 24397d8658efSSaravana Kannan * use. 24407d8658efSSaravana Kannan * 24417d8658efSSaravana Kannan * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise. 24427d8658efSSaravana Kannan */ 24437d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table, 24447d8658efSSaravana Kannan struct opp_table *dst_table, 24457d8658efSSaravana Kannan struct dev_pm_opp *src_opp) 24467d8658efSSaravana Kannan { 24477d8658efSSaravana Kannan struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV); 24487d8658efSSaravana Kannan int i; 24497d8658efSSaravana Kannan 24507d8658efSSaravana Kannan if (!src_table || !dst_table || !src_opp || 24517d8658efSSaravana Kannan !src_table->required_opp_tables) 24527d8658efSSaravana Kannan return ERR_PTR(-EINVAL); 24537d8658efSSaravana Kannan 24547d8658efSSaravana Kannan /* required-opps not fully initialized yet */ 24557d8658efSSaravana Kannan if (lazy_linking_pending(src_table)) 24567d8658efSSaravana Kannan return ERR_PTR(-EBUSY); 24577d8658efSSaravana Kannan 24587d8658efSSaravana Kannan for (i = 0; i < src_table->required_opp_count; i++) { 24597d8658efSSaravana Kannan if (src_table->required_opp_tables[i] == dst_table) { 24607d8658efSSaravana Kannan mutex_lock(&src_table->lock); 24617d8658efSSaravana Kannan 24627d8658efSSaravana Kannan list_for_each_entry(opp, &src_table->opp_list, node) { 24637d8658efSSaravana Kannan if (opp == src_opp) { 24647d8658efSSaravana Kannan dest_opp = opp->required_opps[i]; 24657d8658efSSaravana Kannan dev_pm_opp_get(dest_opp); 24667d8658efSSaravana Kannan break; 24677d8658efSSaravana Kannan } 24687d8658efSSaravana Kannan } 24697d8658efSSaravana Kannan 24707d8658efSSaravana Kannan mutex_unlock(&src_table->lock); 24717d8658efSSaravana Kannan break; 24727d8658efSSaravana Kannan } 24737d8658efSSaravana Kannan } 24747d8658efSSaravana Kannan 24757d8658efSSaravana Kannan if (IS_ERR(dest_opp)) { 24767d8658efSSaravana Kannan pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, 24777d8658efSSaravana Kannan src_table, dst_table); 24787d8658efSSaravana Kannan } 24797d8658efSSaravana Kannan 24807d8658efSSaravana Kannan return dest_opp; 24817d8658efSSaravana Kannan } 24827d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp); 24837d8658efSSaravana Kannan 24847d8658efSSaravana Kannan /** 2485c8a59103SViresh Kumar * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 2486c8a59103SViresh Kumar * @src_table: OPP table which has dst_table as one of its required OPP table. 2487c8a59103SViresh Kumar * @dst_table: Required OPP table of the src_table. 2488c8a59103SViresh Kumar * @pstate: Current performance state of the src_table. 2489c8a59103SViresh Kumar * 2490c8a59103SViresh Kumar * This Returns pstate of the OPP (present in @dst_table) pointed out by the 2491c8a59103SViresh Kumar * "required-opps" property of the OPP (present in @src_table) which has 2492c8a59103SViresh Kumar * performance state set to @pstate. 2493c8a59103SViresh Kumar * 2494c8a59103SViresh Kumar * Return: Zero or positive performance state on success, otherwise negative 2495c8a59103SViresh Kumar * value on errors. 2496c8a59103SViresh Kumar */ 2497c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 2498c8a59103SViresh Kumar struct opp_table *dst_table, 2499c8a59103SViresh Kumar unsigned int pstate) 2500c8a59103SViresh Kumar { 2501c8a59103SViresh Kumar struct dev_pm_opp *opp; 2502c8a59103SViresh Kumar int dest_pstate = -EINVAL; 2503c8a59103SViresh Kumar int i; 2504c8a59103SViresh Kumar 2505c8a59103SViresh Kumar /* 2506c8a59103SViresh Kumar * Normally the src_table will have the "required_opps" property set to 2507c8a59103SViresh Kumar * point to one of the OPPs in the dst_table, but in some cases the 2508c8a59103SViresh Kumar * genpd and its master have one to one mapping of performance states 2509c8a59103SViresh Kumar * and so none of them have the "required-opps" property set. Return the 2510c8a59103SViresh Kumar * pstate of the src_table as it is in such cases. 2511c8a59103SViresh Kumar */ 2512f2f4d2b8SDmitry Osipenko if (!src_table || !src_table->required_opp_count) 2513c8a59103SViresh Kumar return pstate; 2514c8a59103SViresh Kumar 25157eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 25167eba0c76SViresh Kumar if (lazy_linking_pending(src_table)) 25177eba0c76SViresh Kumar return -EBUSY; 25187eba0c76SViresh Kumar 2519c8a59103SViresh Kumar for (i = 0; i < src_table->required_opp_count; i++) { 2520c8a59103SViresh Kumar if (src_table->required_opp_tables[i]->np == dst_table->np) 2521c8a59103SViresh Kumar break; 2522c8a59103SViresh Kumar } 2523c8a59103SViresh Kumar 2524c8a59103SViresh Kumar if (unlikely(i == src_table->required_opp_count)) { 2525c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 2526c8a59103SViresh Kumar __func__, src_table, dst_table); 2527c8a59103SViresh Kumar return -EINVAL; 2528c8a59103SViresh Kumar } 2529c8a59103SViresh Kumar 2530c8a59103SViresh Kumar mutex_lock(&src_table->lock); 2531c8a59103SViresh Kumar 2532c8a59103SViresh Kumar list_for_each_entry(opp, &src_table->opp_list, node) { 2533c8a59103SViresh Kumar if (opp->pstate == pstate) { 2534c8a59103SViresh Kumar dest_pstate = opp->required_opps[i]->pstate; 2535c8a59103SViresh Kumar goto unlock; 2536c8a59103SViresh Kumar } 2537c8a59103SViresh Kumar } 2538c8a59103SViresh Kumar 2539c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 2540c8a59103SViresh Kumar dst_table); 2541c8a59103SViresh Kumar 2542c8a59103SViresh Kumar unlock: 2543c8a59103SViresh Kumar mutex_unlock(&src_table->lock); 2544c8a59103SViresh Kumar 2545c8a59103SViresh Kumar return dest_pstate; 2546c8a59103SViresh Kumar } 2547c8a59103SViresh Kumar 2548c8a59103SViresh Kumar /** 25497813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 25507813dd6fSViresh Kumar * @dev: device for which we do this operation 25517813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 25527813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 25537813dd6fSViresh Kumar * 25547813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 25557813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 25567813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 25577813dd6fSViresh Kumar * 25587813dd6fSViresh Kumar * Return: 25597813dd6fSViresh Kumar * 0 On success OR 25607813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 25617813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 25627813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 25637813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 25647813dd6fSViresh Kumar */ 25657813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 25667813dd6fSViresh Kumar { 25677813dd6fSViresh Kumar struct opp_table *opp_table; 25687813dd6fSViresh Kumar int ret; 25697813dd6fSViresh Kumar 257032439ac7SViresh Kumar opp_table = _add_opp_table(dev, true); 2571dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2572dd461cd9SStephan Gerhold return PTR_ERR(opp_table); 25737813dd6fSViresh Kumar 257446f48acaSViresh Kumar /* Fix regulator count for dynamic OPPs */ 257546f48acaSViresh Kumar opp_table->regulator_count = 1; 257646f48acaSViresh Kumar 25777813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 25780ad8c623SViresh Kumar if (ret) 25797813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 25800ad8c623SViresh Kumar 25817813dd6fSViresh Kumar return ret; 25827813dd6fSViresh Kumar } 25837813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 25847813dd6fSViresh Kumar 25857813dd6fSViresh Kumar /** 25867813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 25877813dd6fSViresh Kumar * @dev: device for which we do this operation 25887813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 25897813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 25907813dd6fSViresh Kumar * 25917813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 25927813dd6fSViresh Kumar * which is isolated here. 25937813dd6fSViresh Kumar * 25947813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 25957813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 25967813dd6fSViresh Kumar * successful. 25977813dd6fSViresh Kumar */ 25987813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 25997813dd6fSViresh Kumar bool availability_req) 26007813dd6fSViresh Kumar { 26017813dd6fSViresh Kumar struct opp_table *opp_table; 26027813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 26037813dd6fSViresh Kumar int r = 0; 26047813dd6fSViresh Kumar 26057813dd6fSViresh Kumar /* Find the opp_table */ 26067813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 26077813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 26087813dd6fSViresh Kumar r = PTR_ERR(opp_table); 26097813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 26107813dd6fSViresh Kumar return r; 26117813dd6fSViresh Kumar } 26127813dd6fSViresh Kumar 26137813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 26147813dd6fSViresh Kumar 26157813dd6fSViresh Kumar /* Do we have the frequency? */ 26167813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 26177813dd6fSViresh Kumar if (tmp_opp->rate == freq) { 26187813dd6fSViresh Kumar opp = tmp_opp; 26197813dd6fSViresh Kumar break; 26207813dd6fSViresh Kumar } 26217813dd6fSViresh Kumar } 26227813dd6fSViresh Kumar 26237813dd6fSViresh Kumar if (IS_ERR(opp)) { 26247813dd6fSViresh Kumar r = PTR_ERR(opp); 26257813dd6fSViresh Kumar goto unlock; 26267813dd6fSViresh Kumar } 26277813dd6fSViresh Kumar 26287813dd6fSViresh Kumar /* Is update really needed? */ 26297813dd6fSViresh Kumar if (opp->available == availability_req) 26307813dd6fSViresh Kumar goto unlock; 26317813dd6fSViresh Kumar 26327813dd6fSViresh Kumar opp->available = availability_req; 26337813dd6fSViresh Kumar 26347813dd6fSViresh Kumar dev_pm_opp_get(opp); 26357813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 26367813dd6fSViresh Kumar 26377813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 26387813dd6fSViresh Kumar if (availability_req) 26397813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 26407813dd6fSViresh Kumar opp); 26417813dd6fSViresh Kumar else 26427813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 26437813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 26447813dd6fSViresh Kumar 26457813dd6fSViresh Kumar dev_pm_opp_put(opp); 26467813dd6fSViresh Kumar goto put_table; 26477813dd6fSViresh Kumar 26487813dd6fSViresh Kumar unlock: 26497813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 26507813dd6fSViresh Kumar put_table: 26517813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 26527813dd6fSViresh Kumar return r; 26537813dd6fSViresh Kumar } 26547813dd6fSViresh Kumar 26557813dd6fSViresh Kumar /** 265625cb20a2SStephen Boyd * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP 265725cb20a2SStephen Boyd * @dev: device for which we do this operation 265825cb20a2SStephen Boyd * @freq: OPP frequency to adjust voltage of 265925cb20a2SStephen Boyd * @u_volt: new OPP target voltage 266025cb20a2SStephen Boyd * @u_volt_min: new OPP min voltage 266125cb20a2SStephen Boyd * @u_volt_max: new OPP max voltage 266225cb20a2SStephen Boyd * 266325cb20a2SStephen Boyd * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 266425cb20a2SStephen Boyd * copy operation, returns 0 if no modifcation was done OR modification was 266525cb20a2SStephen Boyd * successful. 266625cb20a2SStephen Boyd */ 266725cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq, 266825cb20a2SStephen Boyd unsigned long u_volt, unsigned long u_volt_min, 266925cb20a2SStephen Boyd unsigned long u_volt_max) 267025cb20a2SStephen Boyd 267125cb20a2SStephen Boyd { 267225cb20a2SStephen Boyd struct opp_table *opp_table; 267325cb20a2SStephen Boyd struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 267425cb20a2SStephen Boyd int r = 0; 267525cb20a2SStephen Boyd 267625cb20a2SStephen Boyd /* Find the opp_table */ 267725cb20a2SStephen Boyd opp_table = _find_opp_table(dev); 267825cb20a2SStephen Boyd if (IS_ERR(opp_table)) { 267925cb20a2SStephen Boyd r = PTR_ERR(opp_table); 268025cb20a2SStephen Boyd dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 268125cb20a2SStephen Boyd return r; 268225cb20a2SStephen Boyd } 268325cb20a2SStephen Boyd 268425cb20a2SStephen Boyd mutex_lock(&opp_table->lock); 268525cb20a2SStephen Boyd 268625cb20a2SStephen Boyd /* Do we have the frequency? */ 268725cb20a2SStephen Boyd list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 268825cb20a2SStephen Boyd if (tmp_opp->rate == freq) { 268925cb20a2SStephen Boyd opp = tmp_opp; 269025cb20a2SStephen Boyd break; 269125cb20a2SStephen Boyd } 269225cb20a2SStephen Boyd } 269325cb20a2SStephen Boyd 269425cb20a2SStephen Boyd if (IS_ERR(opp)) { 269525cb20a2SStephen Boyd r = PTR_ERR(opp); 269625cb20a2SStephen Boyd goto adjust_unlock; 269725cb20a2SStephen Boyd } 269825cb20a2SStephen Boyd 269925cb20a2SStephen Boyd /* Is update really needed? */ 270025cb20a2SStephen Boyd if (opp->supplies->u_volt == u_volt) 270125cb20a2SStephen Boyd goto adjust_unlock; 270225cb20a2SStephen Boyd 270325cb20a2SStephen Boyd opp->supplies->u_volt = u_volt; 270425cb20a2SStephen Boyd opp->supplies->u_volt_min = u_volt_min; 270525cb20a2SStephen Boyd opp->supplies->u_volt_max = u_volt_max; 270625cb20a2SStephen Boyd 270725cb20a2SStephen Boyd dev_pm_opp_get(opp); 270825cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 270925cb20a2SStephen Boyd 271025cb20a2SStephen Boyd /* Notify the voltage change of the OPP */ 271125cb20a2SStephen Boyd blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE, 271225cb20a2SStephen Boyd opp); 271325cb20a2SStephen Boyd 271425cb20a2SStephen Boyd dev_pm_opp_put(opp); 271525cb20a2SStephen Boyd goto adjust_put_table; 271625cb20a2SStephen Boyd 271725cb20a2SStephen Boyd adjust_unlock: 271825cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 271925cb20a2SStephen Boyd adjust_put_table: 272025cb20a2SStephen Boyd dev_pm_opp_put_opp_table(opp_table); 272125cb20a2SStephen Boyd return r; 272225cb20a2SStephen Boyd } 272303649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage); 272425cb20a2SStephen Boyd 272525cb20a2SStephen Boyd /** 27267813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 27277813dd6fSViresh Kumar * @dev: device for which we do this operation 27287813dd6fSViresh Kumar * @freq: OPP frequency to enable 27297813dd6fSViresh Kumar * 27307813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 27317813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 27327813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 27337813dd6fSViresh Kumar * 27347813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 27357813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 27367813dd6fSViresh Kumar * successful. 27377813dd6fSViresh Kumar */ 27387813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 27397813dd6fSViresh Kumar { 27407813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 27417813dd6fSViresh Kumar } 27427813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 27437813dd6fSViresh Kumar 27447813dd6fSViresh Kumar /** 27457813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 27467813dd6fSViresh Kumar * @dev: device for which we do this operation 27477813dd6fSViresh Kumar * @freq: OPP frequency to disable 27487813dd6fSViresh Kumar * 27497813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 27507813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 27517813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 27527813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 27537813dd6fSViresh Kumar * 27547813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 27557813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 27567813dd6fSViresh Kumar * successful. 27577813dd6fSViresh Kumar */ 27587813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 27597813dd6fSViresh Kumar { 27607813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 27617813dd6fSViresh Kumar } 27627813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 27637813dd6fSViresh Kumar 27647813dd6fSViresh Kumar /** 27657813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 27667813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 27677813dd6fSViresh Kumar * @nb: Notifier block to be registered 27687813dd6fSViresh Kumar * 27697813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 27707813dd6fSViresh Kumar */ 27717813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 27727813dd6fSViresh Kumar { 27737813dd6fSViresh Kumar struct opp_table *opp_table; 27747813dd6fSViresh Kumar int ret; 27757813dd6fSViresh Kumar 27767813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 27777813dd6fSViresh Kumar if (IS_ERR(opp_table)) 27787813dd6fSViresh Kumar return PTR_ERR(opp_table); 27797813dd6fSViresh Kumar 27807813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 27817813dd6fSViresh Kumar 27827813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 27837813dd6fSViresh Kumar 27847813dd6fSViresh Kumar return ret; 27857813dd6fSViresh Kumar } 27867813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 27877813dd6fSViresh Kumar 27887813dd6fSViresh Kumar /** 27897813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 27907813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 27917813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 27927813dd6fSViresh Kumar * 27937813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 27947813dd6fSViresh Kumar */ 27957813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 27967813dd6fSViresh Kumar struct notifier_block *nb) 27977813dd6fSViresh Kumar { 27987813dd6fSViresh Kumar struct opp_table *opp_table; 27997813dd6fSViresh Kumar int ret; 28007813dd6fSViresh Kumar 28017813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 28027813dd6fSViresh Kumar if (IS_ERR(opp_table)) 28037813dd6fSViresh Kumar return PTR_ERR(opp_table); 28047813dd6fSViresh Kumar 28057813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 28067813dd6fSViresh Kumar 28077813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 28087813dd6fSViresh Kumar 28097813dd6fSViresh Kumar return ret; 28107813dd6fSViresh Kumar } 28117813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 28127813dd6fSViresh Kumar 28138aaf6264SViresh Kumar /** 28148aaf6264SViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 28158aaf6264SViresh Kumar * @dev: device pointer used to lookup OPP table. 28168aaf6264SViresh Kumar * 28178aaf6264SViresh Kumar * Free both OPPs created using static entries present in DT and the 28188aaf6264SViresh Kumar * dynamically added entries. 28198aaf6264SViresh Kumar */ 28208aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 28217813dd6fSViresh Kumar { 28227813dd6fSViresh Kumar struct opp_table *opp_table; 28237813dd6fSViresh Kumar 28247813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 28257813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 28267813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 28277813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 28287813dd6fSViresh Kumar 28297813dd6fSViresh Kumar if (error != -ENODEV) 28307813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 28317813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 28327813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 28337813dd6fSViresh Kumar error); 28347813dd6fSViresh Kumar return; 28357813dd6fSViresh Kumar } 28367813dd6fSViresh Kumar 2837922ff075SViresh Kumar /* 2838922ff075SViresh Kumar * Drop the extra reference only if the OPP table was successfully added 2839922ff075SViresh Kumar * with dev_pm_opp_of_add_table() earlier. 2840922ff075SViresh Kumar **/ 2841922ff075SViresh Kumar if (_opp_remove_all_static(opp_table)) 2842cdd6ed90SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2843cdd6ed90SViresh Kumar 2844922ff075SViresh Kumar /* Drop reference taken by _find_opp_table() */ 28457813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 28467813dd6fSViresh Kumar } 28477813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 2848ce8073d8SDmitry Osipenko 2849ce8073d8SDmitry Osipenko /** 2850ce8073d8SDmitry Osipenko * dev_pm_opp_sync_regulators() - Sync state of voltage regulators 2851ce8073d8SDmitry Osipenko * @dev: device for which we do this operation 2852ce8073d8SDmitry Osipenko * 2853ce8073d8SDmitry Osipenko * Sync voltage state of the OPP table regulators. 2854ce8073d8SDmitry Osipenko * 2855ce8073d8SDmitry Osipenko * Return: 0 on success or a negative error value. 2856ce8073d8SDmitry Osipenko */ 2857ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev) 2858ce8073d8SDmitry Osipenko { 2859ce8073d8SDmitry Osipenko struct opp_table *opp_table; 2860ce8073d8SDmitry Osipenko struct regulator *reg; 2861ce8073d8SDmitry Osipenko int i, ret = 0; 2862ce8073d8SDmitry Osipenko 2863ce8073d8SDmitry Osipenko /* Device may not have OPP table */ 2864ce8073d8SDmitry Osipenko opp_table = _find_opp_table(dev); 2865ce8073d8SDmitry Osipenko if (IS_ERR(opp_table)) 2866ce8073d8SDmitry Osipenko return 0; 2867ce8073d8SDmitry Osipenko 2868ce8073d8SDmitry Osipenko /* Regulator may not be required for the device */ 2869ce8073d8SDmitry Osipenko if (unlikely(!opp_table->regulators)) 2870ce8073d8SDmitry Osipenko goto put_table; 2871ce8073d8SDmitry Osipenko 2872ce8073d8SDmitry Osipenko /* Nothing to sync if voltage wasn't changed */ 2873ce8073d8SDmitry Osipenko if (!opp_table->enabled) 2874ce8073d8SDmitry Osipenko goto put_table; 2875ce8073d8SDmitry Osipenko 2876ce8073d8SDmitry Osipenko for (i = 0; i < opp_table->regulator_count; i++) { 2877ce8073d8SDmitry Osipenko reg = opp_table->regulators[i]; 2878ce8073d8SDmitry Osipenko ret = regulator_sync_voltage(reg); 2879ce8073d8SDmitry Osipenko if (ret) 2880ce8073d8SDmitry Osipenko break; 2881ce8073d8SDmitry Osipenko } 2882ce8073d8SDmitry Osipenko put_table: 2883ce8073d8SDmitry Osipenko /* Drop reference taken by _find_opp_table() */ 2884ce8073d8SDmitry Osipenko dev_pm_opp_put_opp_table(opp_table); 2885ce8073d8SDmitry Osipenko 2886ce8073d8SDmitry Osipenko return ret; 2887ce8073d8SDmitry Osipenko } 2888ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators); 2889