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 327813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */ 337813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock); 3427c09484SViresh Kumar /* Flag indicating that opp_tables list is being updated at the moment */ 3527c09484SViresh Kumar static bool opp_tables_busy; 367813dd6fSViresh Kumar 3711b9b663SViresh Kumar /* OPP ID allocator */ 3811b9b663SViresh Kumar static DEFINE_XARRAY_ALLOC1(opp_configs); 3911b9b663SViresh Kumar 409e62edacSViresh Kumar static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table) 417813dd6fSViresh Kumar { 427813dd6fSViresh Kumar struct opp_device *opp_dev; 439e62edacSViresh Kumar bool found = false; 447813dd6fSViresh Kumar 459e62edacSViresh Kumar mutex_lock(&opp_table->lock); 467813dd6fSViresh Kumar list_for_each_entry(opp_dev, &opp_table->dev_list, node) 479e62edacSViresh Kumar if (opp_dev->dev == dev) { 489e62edacSViresh Kumar found = true; 499e62edacSViresh Kumar break; 509e62edacSViresh Kumar } 517813dd6fSViresh Kumar 529e62edacSViresh Kumar mutex_unlock(&opp_table->lock); 539e62edacSViresh Kumar return found; 547813dd6fSViresh Kumar } 557813dd6fSViresh Kumar 567813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev) 577813dd6fSViresh Kumar { 587813dd6fSViresh Kumar struct opp_table *opp_table; 597813dd6fSViresh Kumar 607813dd6fSViresh Kumar list_for_each_entry(opp_table, &opp_tables, node) { 619e62edacSViresh Kumar if (_find_opp_dev(dev, opp_table)) { 627813dd6fSViresh Kumar _get_opp_table_kref(opp_table); 637813dd6fSViresh Kumar return opp_table; 647813dd6fSViresh Kumar } 657813dd6fSViresh Kumar } 667813dd6fSViresh Kumar 677813dd6fSViresh Kumar return ERR_PTR(-ENODEV); 687813dd6fSViresh Kumar } 697813dd6fSViresh Kumar 707813dd6fSViresh Kumar /** 717813dd6fSViresh Kumar * _find_opp_table() - find opp_table struct using device pointer 727813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table 737813dd6fSViresh Kumar * 747813dd6fSViresh Kumar * Search OPP table for one containing matching device. 757813dd6fSViresh Kumar * 767813dd6fSViresh Kumar * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or 777813dd6fSViresh Kumar * -EINVAL based on type of error. 787813dd6fSViresh Kumar * 797813dd6fSViresh Kumar * The callers must call dev_pm_opp_put_opp_table() after the table is used. 807813dd6fSViresh Kumar */ 817813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev) 827813dd6fSViresh Kumar { 837813dd6fSViresh Kumar struct opp_table *opp_table; 847813dd6fSViresh Kumar 857813dd6fSViresh Kumar if (IS_ERR_OR_NULL(dev)) { 867813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 877813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 887813dd6fSViresh Kumar } 897813dd6fSViresh Kumar 907813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 917813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 927813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 937813dd6fSViresh Kumar 947813dd6fSViresh Kumar return opp_table; 957813dd6fSViresh Kumar } 967813dd6fSViresh Kumar 97f123ea74SViresh Kumar /* 98f123ea74SViresh Kumar * Returns true if multiple clocks aren't there, else returns false with WARN. 99f123ea74SViresh Kumar * 100f123ea74SViresh Kumar * We don't force clk_count == 1 here as there are users who don't have a clock 101f123ea74SViresh Kumar * representation in the OPP table and manage the clock configuration themselves 102f123ea74SViresh Kumar * in an platform specific way. 103f123ea74SViresh Kumar */ 104f123ea74SViresh Kumar static bool assert_single_clk(struct opp_table *opp_table) 105f123ea74SViresh Kumar { 106f123ea74SViresh Kumar return !WARN_ON(opp_table->clk_count > 1); 107f123ea74SViresh Kumar } 108f123ea74SViresh Kumar 1097813dd6fSViresh Kumar /** 1107813dd6fSViresh Kumar * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp 1117813dd6fSViresh Kumar * @opp: opp for which voltage has to be returned for 1127813dd6fSViresh Kumar * 1137813dd6fSViresh Kumar * Return: voltage in micro volt corresponding to the opp, else 1147813dd6fSViresh Kumar * return 0 1157813dd6fSViresh Kumar * 1167813dd6fSViresh Kumar * This is useful only for devices with single power supply. 1177813dd6fSViresh Kumar */ 1187813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 1197813dd6fSViresh Kumar { 1207813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp)) { 1217813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1227813dd6fSViresh Kumar return 0; 1237813dd6fSViresh Kumar } 1247813dd6fSViresh Kumar 1257813dd6fSViresh Kumar return opp->supplies[0].u_volt; 1267813dd6fSViresh Kumar } 1277813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); 1287813dd6fSViresh Kumar 1297813dd6fSViresh Kumar /** 13069b1af17SViresh Kumar * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp 13169b1af17SViresh Kumar * @opp: opp for which voltage has to be returned for 13269b1af17SViresh Kumar * @supplies: Placeholder for copying the supply information. 13369b1af17SViresh Kumar * 13469b1af17SViresh Kumar * Return: negative error number on failure, 0 otherwise on success after 13569b1af17SViresh Kumar * setting @supplies. 13669b1af17SViresh Kumar * 13769b1af17SViresh Kumar * This can be used for devices with any number of power supplies. The caller 13869b1af17SViresh Kumar * must ensure the @supplies array must contain space for each regulator. 13969b1af17SViresh Kumar */ 14069b1af17SViresh Kumar int dev_pm_opp_get_supplies(struct dev_pm_opp *opp, 14169b1af17SViresh Kumar struct dev_pm_opp_supply *supplies) 14269b1af17SViresh Kumar { 14369b1af17SViresh Kumar if (IS_ERR_OR_NULL(opp) || !supplies) { 14469b1af17SViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 14569b1af17SViresh Kumar return -EINVAL; 14669b1af17SViresh Kumar } 14769b1af17SViresh Kumar 14869b1af17SViresh Kumar memcpy(supplies, opp->supplies, 14969b1af17SViresh Kumar sizeof(*supplies) * opp->opp_table->regulator_count); 15069b1af17SViresh Kumar return 0; 15169b1af17SViresh Kumar } 15269b1af17SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_supplies); 15369b1af17SViresh Kumar 15469b1af17SViresh Kumar /** 1554f9a7a1dSLukasz Luba * dev_pm_opp_get_power() - Gets the power corresponding to an opp 1564f9a7a1dSLukasz Luba * @opp: opp for which power has to be returned for 1574f9a7a1dSLukasz Luba * 1584f9a7a1dSLukasz Luba * Return: power in micro watt corresponding to the opp, else 1594f9a7a1dSLukasz Luba * return 0 1604f9a7a1dSLukasz Luba * 1614f9a7a1dSLukasz Luba * This is useful only for devices with single power supply. 1624f9a7a1dSLukasz Luba */ 1634f9a7a1dSLukasz Luba unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp) 1644f9a7a1dSLukasz Luba { 1654f9a7a1dSLukasz Luba unsigned long opp_power = 0; 1664f9a7a1dSLukasz Luba int i; 1674f9a7a1dSLukasz Luba 1684f9a7a1dSLukasz Luba if (IS_ERR_OR_NULL(opp)) { 1694f9a7a1dSLukasz Luba pr_err("%s: Invalid parameters\n", __func__); 1704f9a7a1dSLukasz Luba return 0; 1714f9a7a1dSLukasz Luba } 1724f9a7a1dSLukasz Luba for (i = 0; i < opp->opp_table->regulator_count; i++) 1734f9a7a1dSLukasz Luba opp_power += opp->supplies[i].u_watt; 1744f9a7a1dSLukasz Luba 1754f9a7a1dSLukasz Luba return opp_power; 1764f9a7a1dSLukasz Luba } 1774f9a7a1dSLukasz Luba EXPORT_SYMBOL_GPL(dev_pm_opp_get_power); 1784f9a7a1dSLukasz Luba 1794f9a7a1dSLukasz Luba /** 1807813dd6fSViresh Kumar * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp 1817813dd6fSViresh Kumar * @opp: opp for which frequency has to be returned for 1827813dd6fSViresh Kumar * 1837813dd6fSViresh Kumar * Return: frequency in hertz corresponding to the opp, else 1847813dd6fSViresh Kumar * return 0 1857813dd6fSViresh Kumar */ 1867813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) 1877813dd6fSViresh Kumar { 18806a8a059SAndrew-sh.Cheng if (IS_ERR_OR_NULL(opp)) { 1897813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1907813dd6fSViresh Kumar return 0; 1917813dd6fSViresh Kumar } 1927813dd6fSViresh Kumar 193f123ea74SViresh Kumar if (!assert_single_clk(opp->opp_table)) 194f123ea74SViresh Kumar return 0; 195f123ea74SViresh Kumar 1962083da24SViresh Kumar return opp->rates[0]; 1977813dd6fSViresh Kumar } 1987813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); 1997813dd6fSViresh Kumar 2007813dd6fSViresh Kumar /** 2015b93ac54SRajendra Nayak * dev_pm_opp_get_level() - Gets the level corresponding to an available opp 2025b93ac54SRajendra Nayak * @opp: opp for which level value has to be returned for 2035b93ac54SRajendra Nayak * 2045b93ac54SRajendra Nayak * Return: level read from device tree corresponding to the opp, else 2055b93ac54SRajendra Nayak * return 0. 2065b93ac54SRajendra Nayak */ 2075b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp) 2085b93ac54SRajendra Nayak { 2095b93ac54SRajendra Nayak if (IS_ERR_OR_NULL(opp) || !opp->available) { 2105b93ac54SRajendra Nayak pr_err("%s: Invalid parameters\n", __func__); 2115b93ac54SRajendra Nayak return 0; 2125b93ac54SRajendra Nayak } 2135b93ac54SRajendra Nayak 2145b93ac54SRajendra Nayak return opp->level; 2155b93ac54SRajendra Nayak } 2165b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level); 2175b93ac54SRajendra Nayak 2185b93ac54SRajendra Nayak /** 219597ff543SDmitry Osipenko * dev_pm_opp_get_required_pstate() - Gets the required performance state 220597ff543SDmitry Osipenko * corresponding to an available opp 221597ff543SDmitry Osipenko * @opp: opp for which performance state has to be returned for 222597ff543SDmitry Osipenko * @index: index of the required opp 223597ff543SDmitry Osipenko * 224597ff543SDmitry Osipenko * Return: performance state read from device tree corresponding to the 225597ff543SDmitry Osipenko * required opp, else return 0. 226597ff543SDmitry Osipenko */ 227597ff543SDmitry Osipenko unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp, 228597ff543SDmitry Osipenko unsigned int index) 229597ff543SDmitry Osipenko { 23084cb7ff3SViresh Kumar struct opp_table *opp_table = opp->opp_table; 23184cb7ff3SViresh Kumar 232597ff543SDmitry Osipenko if (IS_ERR_OR_NULL(opp) || !opp->available || 23384cb7ff3SViresh Kumar index >= opp_table->required_opp_count) { 234597ff543SDmitry Osipenko pr_err("%s: Invalid parameters\n", __func__); 235597ff543SDmitry Osipenko return 0; 236597ff543SDmitry Osipenko } 237597ff543SDmitry Osipenko 2387eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 23984cb7ff3SViresh Kumar if (lazy_linking_pending(opp_table)) 2407eba0c76SViresh Kumar return 0; 2417eba0c76SViresh Kumar 24284cb7ff3SViresh Kumar /* The required OPP table must belong to a genpd */ 24384cb7ff3SViresh Kumar if (unlikely(!opp_table->required_opp_tables[index]->is_genpd)) { 24484cb7ff3SViresh Kumar pr_err("%s: Performance state is only valid for genpds.\n", __func__); 24584cb7ff3SViresh Kumar return 0; 24684cb7ff3SViresh Kumar } 24784cb7ff3SViresh Kumar 2487c41cdcdSViresh Kumar return opp->required_opps[index]->level; 249597ff543SDmitry Osipenko } 250597ff543SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate); 251597ff543SDmitry Osipenko 252597ff543SDmitry Osipenko /** 2537813dd6fSViresh Kumar * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not 2547813dd6fSViresh Kumar * @opp: opp for which turbo mode is being verified 2557813dd6fSViresh Kumar * 2567813dd6fSViresh Kumar * Turbo OPPs are not for normal use, and can be enabled (under certain 2577813dd6fSViresh Kumar * conditions) for short duration of times to finish high throughput work 2587813dd6fSViresh Kumar * quickly. Running on them for longer times may overheat the chip. 2597813dd6fSViresh Kumar * 2607813dd6fSViresh Kumar * Return: true if opp is turbo opp, else false. 2617813dd6fSViresh Kumar */ 2627813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 2637813dd6fSViresh Kumar { 2647813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 2657813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 2667813dd6fSViresh Kumar return false; 2677813dd6fSViresh Kumar } 2687813dd6fSViresh Kumar 2697813dd6fSViresh Kumar return opp->turbo; 2707813dd6fSViresh Kumar } 2717813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 2727813dd6fSViresh Kumar 2737813dd6fSViresh Kumar /** 2747813dd6fSViresh Kumar * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds 2757813dd6fSViresh Kumar * @dev: device for which we do this operation 2767813dd6fSViresh Kumar * 2777813dd6fSViresh Kumar * Return: This function returns the max clock latency in nanoseconds. 2787813dd6fSViresh Kumar */ 2797813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 2807813dd6fSViresh Kumar { 2817813dd6fSViresh Kumar struct opp_table *opp_table; 2827813dd6fSViresh Kumar unsigned long clock_latency_ns; 2837813dd6fSViresh Kumar 2847813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2857813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2867813dd6fSViresh Kumar return 0; 2877813dd6fSViresh Kumar 2887813dd6fSViresh Kumar clock_latency_ns = opp_table->clock_latency_ns_max; 2897813dd6fSViresh Kumar 2907813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2917813dd6fSViresh Kumar 2927813dd6fSViresh Kumar return clock_latency_ns; 2937813dd6fSViresh Kumar } 2947813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 2957813dd6fSViresh Kumar 2967813dd6fSViresh Kumar /** 2977813dd6fSViresh Kumar * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 2987813dd6fSViresh Kumar * @dev: device for which we do this operation 2997813dd6fSViresh Kumar * 3007813dd6fSViresh Kumar * Return: This function returns the max voltage latency in nanoseconds. 3017813dd6fSViresh Kumar */ 3027813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 3037813dd6fSViresh Kumar { 3047813dd6fSViresh Kumar struct opp_table *opp_table; 3057813dd6fSViresh Kumar struct dev_pm_opp *opp; 3067813dd6fSViresh Kumar struct regulator *reg; 3077813dd6fSViresh Kumar unsigned long latency_ns = 0; 3087813dd6fSViresh Kumar int ret, i, count; 3097813dd6fSViresh Kumar struct { 3107813dd6fSViresh Kumar unsigned long min; 3117813dd6fSViresh Kumar unsigned long max; 3127813dd6fSViresh Kumar } *uV; 3137813dd6fSViresh Kumar 3147813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3157813dd6fSViresh Kumar if (IS_ERR(opp_table)) 3167813dd6fSViresh Kumar return 0; 3177813dd6fSViresh Kumar 3187813dd6fSViresh Kumar /* Regulator may not be required for the device */ 31990e3577bSViresh Kumar if (!opp_table->regulators) 3207813dd6fSViresh Kumar goto put_opp_table; 3217813dd6fSViresh Kumar 32290e3577bSViresh Kumar count = opp_table->regulator_count; 32390e3577bSViresh Kumar 3247813dd6fSViresh Kumar uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 3257813dd6fSViresh Kumar if (!uV) 3267813dd6fSViresh Kumar goto put_opp_table; 3277813dd6fSViresh Kumar 3287813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 3297813dd6fSViresh Kumar 3307813dd6fSViresh Kumar for (i = 0; i < count; i++) { 3317813dd6fSViresh Kumar uV[i].min = ~0; 3327813dd6fSViresh Kumar uV[i].max = 0; 3337813dd6fSViresh Kumar 3347813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 3357813dd6fSViresh Kumar if (!opp->available) 3367813dd6fSViresh Kumar continue; 3377813dd6fSViresh Kumar 3387813dd6fSViresh Kumar if (opp->supplies[i].u_volt_min < uV[i].min) 3397813dd6fSViresh Kumar uV[i].min = opp->supplies[i].u_volt_min; 3407813dd6fSViresh Kumar if (opp->supplies[i].u_volt_max > uV[i].max) 3417813dd6fSViresh Kumar uV[i].max = opp->supplies[i].u_volt_max; 3427813dd6fSViresh Kumar } 3437813dd6fSViresh Kumar } 3447813dd6fSViresh Kumar 3457813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 3467813dd6fSViresh Kumar 3477813dd6fSViresh Kumar /* 3487813dd6fSViresh Kumar * The caller needs to ensure that opp_table (and hence the regulator) 3497813dd6fSViresh Kumar * isn't freed, while we are executing this routine. 3507813dd6fSViresh Kumar */ 3517813dd6fSViresh Kumar for (i = 0; i < count; i++) { 3527813dd6fSViresh Kumar reg = opp_table->regulators[i]; 3537813dd6fSViresh Kumar ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); 3547813dd6fSViresh Kumar if (ret > 0) 3557813dd6fSViresh Kumar latency_ns += ret * 1000; 3567813dd6fSViresh Kumar } 3577813dd6fSViresh Kumar 3587813dd6fSViresh Kumar kfree(uV); 3597813dd6fSViresh Kumar put_opp_table: 3607813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3617813dd6fSViresh Kumar 3627813dd6fSViresh Kumar return latency_ns; 3637813dd6fSViresh Kumar } 3647813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); 3657813dd6fSViresh Kumar 3667813dd6fSViresh Kumar /** 3677813dd6fSViresh Kumar * dev_pm_opp_get_max_transition_latency() - Get max transition latency in 3687813dd6fSViresh Kumar * nanoseconds 3697813dd6fSViresh Kumar * @dev: device for which we do this operation 3707813dd6fSViresh Kumar * 3717813dd6fSViresh Kumar * Return: This function returns the max transition latency, in nanoseconds, to 3727813dd6fSViresh Kumar * switch from one OPP to other. 3737813dd6fSViresh Kumar */ 3747813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 3757813dd6fSViresh Kumar { 3767813dd6fSViresh Kumar return dev_pm_opp_get_max_volt_latency(dev) + 3777813dd6fSViresh Kumar dev_pm_opp_get_max_clock_latency(dev); 3787813dd6fSViresh Kumar } 3797813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 3807813dd6fSViresh Kumar 3817813dd6fSViresh Kumar /** 3827813dd6fSViresh Kumar * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 3837813dd6fSViresh Kumar * @dev: device for which we do this operation 3847813dd6fSViresh Kumar * 3857813dd6fSViresh Kumar * Return: This function returns the frequency of the OPP marked as suspend_opp 3867813dd6fSViresh Kumar * if one is available, else returns 0; 3877813dd6fSViresh Kumar */ 3887813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 3897813dd6fSViresh Kumar { 3907813dd6fSViresh Kumar struct opp_table *opp_table; 3917813dd6fSViresh Kumar unsigned long freq = 0; 3927813dd6fSViresh Kumar 3937813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3947813dd6fSViresh Kumar if (IS_ERR(opp_table)) 3957813dd6fSViresh Kumar return 0; 3967813dd6fSViresh Kumar 3977813dd6fSViresh Kumar if (opp_table->suspend_opp && opp_table->suspend_opp->available) 3987813dd6fSViresh Kumar freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 3997813dd6fSViresh Kumar 4007813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4017813dd6fSViresh Kumar 4027813dd6fSViresh Kumar return freq; 4037813dd6fSViresh Kumar } 4047813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 4057813dd6fSViresh Kumar 406a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table) 407a1e8c136SViresh Kumar { 408a1e8c136SViresh Kumar struct dev_pm_opp *opp; 409a1e8c136SViresh Kumar int count = 0; 410a1e8c136SViresh Kumar 411a1e8c136SViresh Kumar mutex_lock(&opp_table->lock); 412a1e8c136SViresh Kumar 413a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 414a1e8c136SViresh Kumar if (opp->available) 415a1e8c136SViresh Kumar count++; 416a1e8c136SViresh Kumar } 417a1e8c136SViresh Kumar 418a1e8c136SViresh Kumar mutex_unlock(&opp_table->lock); 419a1e8c136SViresh Kumar 420a1e8c136SViresh Kumar return count; 421a1e8c136SViresh Kumar } 422a1e8c136SViresh Kumar 4237813dd6fSViresh Kumar /** 4247813dd6fSViresh Kumar * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table 4257813dd6fSViresh Kumar * @dev: device for which we do this operation 4267813dd6fSViresh Kumar * 4277813dd6fSViresh Kumar * Return: This function returns the number of available opps if there are any, 4287813dd6fSViresh Kumar * else returns 0 if none or the corresponding error value. 4297813dd6fSViresh Kumar */ 4307813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev) 4317813dd6fSViresh Kumar { 4327813dd6fSViresh Kumar struct opp_table *opp_table; 433a1e8c136SViresh Kumar int count; 4347813dd6fSViresh Kumar 4357813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 4367813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 4377813dd6fSViresh Kumar count = PTR_ERR(opp_table); 438035ed072SFabio Estevam dev_dbg(dev, "%s: OPP table not found (%d)\n", 4397813dd6fSViresh Kumar __func__, count); 44009f662f9SViresh Kumar return count; 4417813dd6fSViresh Kumar } 4427813dd6fSViresh Kumar 443a1e8c136SViresh Kumar count = _get_opp_count(opp_table); 4447813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4457813dd6fSViresh Kumar 4467813dd6fSViresh Kumar return count; 4477813dd6fSViresh Kumar } 4487813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); 4497813dd6fSViresh Kumar 450aab8ced2SViresh Kumar /* Helpers to read keys */ 451aab8ced2SViresh Kumar static unsigned long _read_freq(struct dev_pm_opp *opp, int index) 452aab8ced2SViresh Kumar { 4532083da24SViresh Kumar return opp->rates[0]; 454aab8ced2SViresh Kumar } 455aab8ced2SViresh Kumar 456c2ab2cb6SViresh Kumar static unsigned long _read_level(struct dev_pm_opp *opp, int index) 457c2ab2cb6SViresh Kumar { 458c2ab2cb6SViresh Kumar return opp->level; 459c2ab2cb6SViresh Kumar } 460c2ab2cb6SViresh Kumar 461add1dc09SViresh Kumar static unsigned long _read_bw(struct dev_pm_opp *opp, int index) 462add1dc09SViresh Kumar { 463add1dc09SViresh Kumar return opp->bandwidth[index].peak; 464add1dc09SViresh Kumar } 465add1dc09SViresh Kumar 466aab8ced2SViresh Kumar /* Generic comparison helpers */ 467aab8ced2SViresh Kumar static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 468aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key) 469aab8ced2SViresh Kumar { 470aab8ced2SViresh Kumar if (opp_key == key) { 471aab8ced2SViresh Kumar *opp = temp_opp; 472aab8ced2SViresh Kumar return true; 473aab8ced2SViresh Kumar } 474aab8ced2SViresh Kumar 475aab8ced2SViresh Kumar return false; 476aab8ced2SViresh Kumar } 477aab8ced2SViresh Kumar 478aab8ced2SViresh Kumar static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 479aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key) 480aab8ced2SViresh Kumar { 481aab8ced2SViresh Kumar if (opp_key >= key) { 482aab8ced2SViresh Kumar *opp = temp_opp; 483aab8ced2SViresh Kumar return true; 484aab8ced2SViresh Kumar } 485aab8ced2SViresh Kumar 486aab8ced2SViresh Kumar return false; 487aab8ced2SViresh Kumar } 488aab8ced2SViresh Kumar 489aab8ced2SViresh Kumar static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 490aab8ced2SViresh Kumar unsigned long opp_key, unsigned long key) 491aab8ced2SViresh Kumar { 492aab8ced2SViresh Kumar if (opp_key > key) 493aab8ced2SViresh Kumar return true; 494aab8ced2SViresh Kumar 495aab8ced2SViresh Kumar *opp = temp_opp; 496aab8ced2SViresh Kumar return false; 497aab8ced2SViresh Kumar } 498aab8ced2SViresh Kumar 499aab8ced2SViresh Kumar /* Generic key finding helpers */ 500aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table, 501aab8ced2SViresh Kumar unsigned long *key, int index, bool available, 502aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 503aab8ced2SViresh Kumar bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 504e10a4644SViresh Kumar unsigned long opp_key, unsigned long key), 505e10a4644SViresh Kumar bool (*assert)(struct opp_table *opp_table)) 506aab8ced2SViresh Kumar { 507aab8ced2SViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 508aab8ced2SViresh Kumar 509e10a4644SViresh Kumar /* Assert that the requirement is met */ 510e10a4644SViresh Kumar if (assert && !assert(opp_table)) 511e10a4644SViresh Kumar return ERR_PTR(-EINVAL); 512e10a4644SViresh Kumar 513aab8ced2SViresh Kumar mutex_lock(&opp_table->lock); 514aab8ced2SViresh Kumar 515aab8ced2SViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 516aab8ced2SViresh Kumar if (temp_opp->available == available) { 517aab8ced2SViresh Kumar if (compare(&opp, temp_opp, read(temp_opp, index), *key)) 518aab8ced2SViresh Kumar break; 519aab8ced2SViresh Kumar } 520aab8ced2SViresh Kumar } 521aab8ced2SViresh Kumar 522aab8ced2SViresh Kumar /* Increment the reference count of OPP */ 523aab8ced2SViresh Kumar if (!IS_ERR(opp)) { 524aab8ced2SViresh Kumar *key = read(opp, index); 525aab8ced2SViresh Kumar dev_pm_opp_get(opp); 526aab8ced2SViresh Kumar } 527aab8ced2SViresh Kumar 528aab8ced2SViresh Kumar mutex_unlock(&opp_table->lock); 529aab8ced2SViresh Kumar 530aab8ced2SViresh Kumar return opp; 531aab8ced2SViresh Kumar } 532aab8ced2SViresh Kumar 533aab8ced2SViresh Kumar static struct dev_pm_opp * 534aab8ced2SViresh Kumar _find_key(struct device *dev, unsigned long *key, int index, bool available, 535aab8ced2SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 536aab8ced2SViresh Kumar bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, 537e10a4644SViresh Kumar unsigned long opp_key, unsigned long key), 538e10a4644SViresh Kumar bool (*assert)(struct opp_table *opp_table)) 539aab8ced2SViresh Kumar { 540aab8ced2SViresh Kumar struct opp_table *opp_table; 541aab8ced2SViresh Kumar struct dev_pm_opp *opp; 542aab8ced2SViresh Kumar 543aab8ced2SViresh Kumar opp_table = _find_opp_table(dev); 544aab8ced2SViresh Kumar if (IS_ERR(opp_table)) { 545aab8ced2SViresh Kumar dev_err(dev, "%s: OPP table not found (%ld)\n", __func__, 546aab8ced2SViresh Kumar PTR_ERR(opp_table)); 547aab8ced2SViresh Kumar return ERR_CAST(opp_table); 548aab8ced2SViresh Kumar } 549aab8ced2SViresh Kumar 550aab8ced2SViresh Kumar opp = _opp_table_find_key(opp_table, key, index, available, read, 551e10a4644SViresh Kumar compare, assert); 552aab8ced2SViresh Kumar 553aab8ced2SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 554aab8ced2SViresh Kumar 555aab8ced2SViresh Kumar return opp; 556aab8ced2SViresh Kumar } 557aab8ced2SViresh Kumar 558aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_exact(struct device *dev, 559aab8ced2SViresh Kumar unsigned long key, int index, bool available, 560e10a4644SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 561e10a4644SViresh Kumar bool (*assert)(struct opp_table *opp_table)) 562aab8ced2SViresh Kumar { 563aab8ced2SViresh Kumar /* 564aab8ced2SViresh Kumar * The value of key will be updated here, but will be ignored as the 565aab8ced2SViresh Kumar * caller doesn't need it. 566aab8ced2SViresh Kumar */ 567e10a4644SViresh Kumar return _find_key(dev, &key, index, available, read, _compare_exact, 568e10a4644SViresh Kumar assert); 569aab8ced2SViresh Kumar } 570aab8ced2SViresh Kumar 571aab8ced2SViresh Kumar static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table, 572aab8ced2SViresh Kumar unsigned long *key, int index, bool available, 573e10a4644SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 574e10a4644SViresh Kumar bool (*assert)(struct opp_table *opp_table)) 575aab8ced2SViresh Kumar { 576aab8ced2SViresh Kumar return _opp_table_find_key(opp_table, key, index, available, read, 577e10a4644SViresh Kumar _compare_ceil, assert); 578aab8ced2SViresh Kumar } 579aab8ced2SViresh Kumar 580aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key, 581aab8ced2SViresh Kumar int index, bool available, 582e10a4644SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 583e10a4644SViresh Kumar bool (*assert)(struct opp_table *opp_table)) 584aab8ced2SViresh Kumar { 585e10a4644SViresh Kumar return _find_key(dev, key, index, available, read, _compare_ceil, 586e10a4644SViresh Kumar assert); 587aab8ced2SViresh Kumar } 588aab8ced2SViresh Kumar 589aab8ced2SViresh Kumar static struct dev_pm_opp *_find_key_floor(struct device *dev, 590aab8ced2SViresh Kumar unsigned long *key, int index, bool available, 591e10a4644SViresh Kumar unsigned long (*read)(struct dev_pm_opp *opp, int index), 592e10a4644SViresh Kumar bool (*assert)(struct opp_table *opp_table)) 593aab8ced2SViresh Kumar { 594e10a4644SViresh Kumar return _find_key(dev, key, index, available, read, _compare_floor, 595e10a4644SViresh Kumar assert); 596aab8ced2SViresh Kumar } 597aab8ced2SViresh Kumar 5987813dd6fSViresh Kumar /** 5997813dd6fSViresh Kumar * dev_pm_opp_find_freq_exact() - search for an exact frequency 6007813dd6fSViresh Kumar * @dev: device for which we do this operation 6017813dd6fSViresh Kumar * @freq: frequency to search for 6027813dd6fSViresh Kumar * @available: true/false - match for available opp 6037813dd6fSViresh Kumar * 6047813dd6fSViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 6057813dd6fSViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 6067813dd6fSViresh Kumar * be handled using IS_ERR. Error return values can be: 6077813dd6fSViresh Kumar * EINVAL: for bad pointer 6087813dd6fSViresh Kumar * ERANGE: no match found for search 6097813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 6107813dd6fSViresh Kumar * 6117813dd6fSViresh Kumar * Note: available is a modifier for the search. if available=true, then the 6127813dd6fSViresh Kumar * match is for exact matching frequency and is available in the stored OPP 6137813dd6fSViresh Kumar * table. if false, the match is for exact frequency which is not available. 6147813dd6fSViresh Kumar * 6157813dd6fSViresh Kumar * This provides a mechanism to enable an opp which is not available currently 6167813dd6fSViresh Kumar * or the opposite as well. 6177813dd6fSViresh Kumar * 6187813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 6197813dd6fSViresh Kumar * use. 6207813dd6fSViresh Kumar */ 6217813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 622aab8ced2SViresh Kumar unsigned long freq, bool available) 6237813dd6fSViresh Kumar { 624f123ea74SViresh Kumar return _find_key_exact(dev, freq, 0, available, _read_freq, 625f123ea74SViresh Kumar assert_single_clk); 6267813dd6fSViresh Kumar } 6277813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 6287813dd6fSViresh Kumar 6297813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 6307813dd6fSViresh Kumar unsigned long *freq) 6317813dd6fSViresh Kumar { 632e10a4644SViresh Kumar return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq, 633f123ea74SViresh Kumar assert_single_clk); 6347813dd6fSViresh Kumar } 6357813dd6fSViresh Kumar 6367813dd6fSViresh Kumar /** 6377813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 6387813dd6fSViresh Kumar * @dev: device for which we do this operation 6397813dd6fSViresh Kumar * @freq: Start frequency 6407813dd6fSViresh Kumar * 6417813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 6427813dd6fSViresh Kumar * for a device. 6437813dd6fSViresh Kumar * 6447813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 6457813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 6467813dd6fSViresh Kumar * values can be: 6477813dd6fSViresh Kumar * EINVAL: for bad pointer 6487813dd6fSViresh Kumar * ERANGE: no match found for search 6497813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 6507813dd6fSViresh Kumar * 6517813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 6527813dd6fSViresh Kumar * use. 6537813dd6fSViresh Kumar */ 6547813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 6557813dd6fSViresh Kumar unsigned long *freq) 6567813dd6fSViresh Kumar { 657f123ea74SViresh Kumar return _find_key_ceil(dev, freq, 0, true, _read_freq, assert_single_clk); 6587813dd6fSViresh Kumar } 6597813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 6607813dd6fSViresh Kumar 6617813dd6fSViresh Kumar /** 662*142e17c1SManivannan Sadhasivam * dev_pm_opp_find_freq_ceil_indexed() - Search for a rounded ceil freq for the 663*142e17c1SManivannan Sadhasivam * clock corresponding to the index 664*142e17c1SManivannan Sadhasivam * @dev: Device for which we do this operation 665*142e17c1SManivannan Sadhasivam * @freq: Start frequency 666*142e17c1SManivannan Sadhasivam * @index: Clock index 667*142e17c1SManivannan Sadhasivam * 668*142e17c1SManivannan Sadhasivam * Search for the matching ceil *available* OPP for the clock corresponding to 669*142e17c1SManivannan Sadhasivam * the specified index from a starting freq for a device. 670*142e17c1SManivannan Sadhasivam * 671*142e17c1SManivannan Sadhasivam * Return: matching *opp and refreshes *freq accordingly, else returns 672*142e17c1SManivannan Sadhasivam * ERR_PTR in case of error and should be handled using IS_ERR. Error return 673*142e17c1SManivannan Sadhasivam * values can be: 674*142e17c1SManivannan Sadhasivam * EINVAL: for bad pointer 675*142e17c1SManivannan Sadhasivam * ERANGE: no match found for search 676*142e17c1SManivannan Sadhasivam * ENODEV: if device not found in list of registered devices 677*142e17c1SManivannan Sadhasivam * 678*142e17c1SManivannan Sadhasivam * The callers are required to call dev_pm_opp_put() for the returned OPP after 679*142e17c1SManivannan Sadhasivam * use. 680*142e17c1SManivannan Sadhasivam */ 681*142e17c1SManivannan Sadhasivam struct dev_pm_opp * 682*142e17c1SManivannan Sadhasivam dev_pm_opp_find_freq_ceil_indexed(struct device *dev, unsigned long *freq, 683*142e17c1SManivannan Sadhasivam u32 index) 684*142e17c1SManivannan Sadhasivam { 685*142e17c1SManivannan Sadhasivam return _find_key_ceil(dev, freq, index, true, _read_freq, NULL); 686*142e17c1SManivannan Sadhasivam } 687*142e17c1SManivannan Sadhasivam EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_indexed); 688*142e17c1SManivannan Sadhasivam 689*142e17c1SManivannan Sadhasivam /** 6907813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 6917813dd6fSViresh Kumar * @dev: device for which we do this operation 6927813dd6fSViresh Kumar * @freq: Start frequency 6937813dd6fSViresh Kumar * 6947813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 6957813dd6fSViresh Kumar * for a device. 6967813dd6fSViresh Kumar * 6977813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 6987813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 6997813dd6fSViresh Kumar * values can be: 7007813dd6fSViresh Kumar * EINVAL: for bad pointer 7017813dd6fSViresh Kumar * ERANGE: no match found for search 7027813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 7037813dd6fSViresh Kumar * 7047813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 7057813dd6fSViresh Kumar * use. 7067813dd6fSViresh Kumar */ 7077813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 7087813dd6fSViresh Kumar unsigned long *freq) 7097813dd6fSViresh Kumar { 710f123ea74SViresh Kumar return _find_key_floor(dev, freq, 0, true, _read_freq, assert_single_clk); 7117813dd6fSViresh Kumar } 7127813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 7137813dd6fSViresh Kumar 7142f36bde0SAndrew-sh.Cheng /** 715*142e17c1SManivannan Sadhasivam * dev_pm_opp_find_freq_floor_indexed() - Search for a rounded floor freq for the 716*142e17c1SManivannan Sadhasivam * clock corresponding to the index 717*142e17c1SManivannan Sadhasivam * @dev: Device for which we do this operation 718*142e17c1SManivannan Sadhasivam * @freq: Start frequency 719*142e17c1SManivannan Sadhasivam * @index: Clock index 720*142e17c1SManivannan Sadhasivam * 721*142e17c1SManivannan Sadhasivam * Search for the matching floor *available* OPP for the clock corresponding to 722*142e17c1SManivannan Sadhasivam * the specified index from a starting freq for a device. 723*142e17c1SManivannan Sadhasivam * 724*142e17c1SManivannan Sadhasivam * Return: matching *opp and refreshes *freq accordingly, else returns 725*142e17c1SManivannan Sadhasivam * ERR_PTR in case of error and should be handled using IS_ERR. Error return 726*142e17c1SManivannan Sadhasivam * values can be: 727*142e17c1SManivannan Sadhasivam * EINVAL: for bad pointer 728*142e17c1SManivannan Sadhasivam * ERANGE: no match found for search 729*142e17c1SManivannan Sadhasivam * ENODEV: if device not found in list of registered devices 730*142e17c1SManivannan Sadhasivam * 731*142e17c1SManivannan Sadhasivam * The callers are required to call dev_pm_opp_put() for the returned OPP after 732*142e17c1SManivannan Sadhasivam * use. 733*142e17c1SManivannan Sadhasivam */ 734*142e17c1SManivannan Sadhasivam struct dev_pm_opp * 735*142e17c1SManivannan Sadhasivam dev_pm_opp_find_freq_floor_indexed(struct device *dev, unsigned long *freq, 736*142e17c1SManivannan Sadhasivam u32 index) 737*142e17c1SManivannan Sadhasivam { 738*142e17c1SManivannan Sadhasivam return _find_key_floor(dev, freq, index, true, _read_freq, NULL); 739*142e17c1SManivannan Sadhasivam } 740*142e17c1SManivannan Sadhasivam EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor_indexed); 741*142e17c1SManivannan Sadhasivam 742*142e17c1SManivannan Sadhasivam /** 74322079af7SViresh Kumar * dev_pm_opp_find_level_exact() - search for an exact level 74422079af7SViresh Kumar * @dev: device for which we do this operation 74522079af7SViresh Kumar * @level: level to search for 74622079af7SViresh Kumar * 74722079af7SViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 74822079af7SViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 74922079af7SViresh Kumar * be handled using IS_ERR. Error return values can be: 75022079af7SViresh Kumar * EINVAL: for bad pointer 75122079af7SViresh Kumar * ERANGE: no match found for search 75222079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 75322079af7SViresh Kumar * 75422079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 75522079af7SViresh Kumar * use. 75622079af7SViresh Kumar */ 75722079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev, 75822079af7SViresh Kumar unsigned int level) 75922079af7SViresh Kumar { 760e10a4644SViresh Kumar return _find_key_exact(dev, level, 0, true, _read_level, NULL); 76122079af7SViresh Kumar } 76222079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact); 76322079af7SViresh Kumar 76422079af7SViresh Kumar /** 76522079af7SViresh Kumar * dev_pm_opp_find_level_ceil() - search for an rounded up level 76622079af7SViresh Kumar * @dev: device for which we do this operation 76722079af7SViresh Kumar * @level: level to search for 76822079af7SViresh Kumar * 76922079af7SViresh Kumar * Return: Searches for rounded up match in the opp table and returns pointer 77022079af7SViresh Kumar * to the matching opp if found, else returns ERR_PTR in case of error and 77122079af7SViresh Kumar * should be handled using IS_ERR. Error return values can be: 77222079af7SViresh Kumar * EINVAL: for bad pointer 77322079af7SViresh Kumar * ERANGE: no match found for search 77422079af7SViresh Kumar * ENODEV: if device not found in list of registered devices 77522079af7SViresh Kumar * 77622079af7SViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 77722079af7SViresh Kumar * use. 77822079af7SViresh Kumar */ 77922079af7SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev, 78022079af7SViresh Kumar unsigned int *level) 78122079af7SViresh Kumar { 782c2ab2cb6SViresh Kumar unsigned long temp = *level; 783c2ab2cb6SViresh Kumar struct dev_pm_opp *opp; 78422079af7SViresh Kumar 785e10a4644SViresh Kumar opp = _find_key_ceil(dev, &temp, 0, true, _read_level, NULL); 786c2ab2cb6SViresh Kumar *level = temp; 78722079af7SViresh Kumar return opp; 78822079af7SViresh Kumar } 78922079af7SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil); 79022079af7SViresh Kumar 79122079af7SViresh Kumar /** 79200ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth 79300ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 794617df304SYang Li * @bw: start bandwidth 79500ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 79600ce3873SKrzysztof Kozlowski * 79700ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 79800ce3873SKrzysztof Kozlowski * for a device. 79900ce3873SKrzysztof Kozlowski * 80000ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 80100ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 80200ce3873SKrzysztof Kozlowski * values can be: 80300ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 80400ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 80500ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 80600ce3873SKrzysztof Kozlowski * 80700ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 80800ce3873SKrzysztof Kozlowski * use. 80900ce3873SKrzysztof Kozlowski */ 810add1dc09SViresh Kumar struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw, 811add1dc09SViresh Kumar int index) 81200ce3873SKrzysztof Kozlowski { 813add1dc09SViresh Kumar unsigned long temp = *bw; 814add1dc09SViresh Kumar struct dev_pm_opp *opp; 81500ce3873SKrzysztof Kozlowski 816e10a4644SViresh Kumar opp = _find_key_ceil(dev, &temp, index, true, _read_bw, NULL); 817add1dc09SViresh Kumar *bw = temp; 81800ce3873SKrzysztof Kozlowski return opp; 81900ce3873SKrzysztof Kozlowski } 82000ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil); 82100ce3873SKrzysztof Kozlowski 82200ce3873SKrzysztof Kozlowski /** 82300ce3873SKrzysztof Kozlowski * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth 82400ce3873SKrzysztof Kozlowski * @dev: device for which we do this operation 825617df304SYang Li * @bw: start bandwidth 82600ce3873SKrzysztof Kozlowski * @index: which bandwidth to compare, in case of OPPs with several values 82700ce3873SKrzysztof Kozlowski * 82800ce3873SKrzysztof Kozlowski * Search for the matching floor *available* OPP from a starting bandwidth 82900ce3873SKrzysztof Kozlowski * for a device. 83000ce3873SKrzysztof Kozlowski * 83100ce3873SKrzysztof Kozlowski * Return: matching *opp and refreshes *bw accordingly, else returns 83200ce3873SKrzysztof Kozlowski * ERR_PTR in case of error and should be handled using IS_ERR. Error return 83300ce3873SKrzysztof Kozlowski * values can be: 83400ce3873SKrzysztof Kozlowski * EINVAL: for bad pointer 83500ce3873SKrzysztof Kozlowski * ERANGE: no match found for search 83600ce3873SKrzysztof Kozlowski * ENODEV: if device not found in list of registered devices 83700ce3873SKrzysztof Kozlowski * 83800ce3873SKrzysztof Kozlowski * The callers are required to call dev_pm_opp_put() for the returned OPP after 83900ce3873SKrzysztof Kozlowski * use. 84000ce3873SKrzysztof Kozlowski */ 84100ce3873SKrzysztof Kozlowski struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev, 84200ce3873SKrzysztof Kozlowski unsigned int *bw, int index) 84300ce3873SKrzysztof Kozlowski { 844add1dc09SViresh Kumar unsigned long temp = *bw; 845add1dc09SViresh Kumar struct dev_pm_opp *opp; 84600ce3873SKrzysztof Kozlowski 847e10a4644SViresh Kumar opp = _find_key_floor(dev, &temp, index, true, _read_bw, NULL); 848add1dc09SViresh Kumar *bw = temp; 84900ce3873SKrzysztof Kozlowski return opp; 85000ce3873SKrzysztof Kozlowski } 85100ce3873SKrzysztof Kozlowski EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor); 85200ce3873SKrzysztof Kozlowski 8537813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 8547813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 8557813dd6fSViresh Kumar { 8567813dd6fSViresh Kumar int ret; 8577813dd6fSViresh Kumar 8587813dd6fSViresh Kumar /* Regulator not available for device */ 8597813dd6fSViresh Kumar if (IS_ERR(reg)) { 8607813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 8617813dd6fSViresh Kumar PTR_ERR(reg)); 8627813dd6fSViresh Kumar return 0; 8637813dd6fSViresh Kumar } 8647813dd6fSViresh Kumar 8657813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 8667813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 8677813dd6fSViresh Kumar 8687813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 8697813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 8707813dd6fSViresh Kumar if (ret) 8717813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 8727813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 8737813dd6fSViresh Kumar supply->u_volt_max, ret); 8747813dd6fSViresh Kumar 8757813dd6fSViresh Kumar return ret; 8767813dd6fSViresh Kumar } 8777813dd6fSViresh Kumar 8782083da24SViresh Kumar static int 8792083da24SViresh Kumar _opp_config_clk_single(struct device *dev, struct opp_table *opp_table, 8802083da24SViresh Kumar struct dev_pm_opp *opp, void *data, bool scaling_down) 8817813dd6fSViresh Kumar { 8821efae8d2SViresh Kumar unsigned long *target = data; 8831efae8d2SViresh Kumar unsigned long freq; 8847813dd6fSViresh Kumar int ret; 8857813dd6fSViresh Kumar 8861efae8d2SViresh Kumar /* One of target and opp must be available */ 8871efae8d2SViresh Kumar if (target) { 8881efae8d2SViresh Kumar freq = *target; 8891efae8d2SViresh Kumar } else if (opp) { 8902083da24SViresh Kumar freq = opp->rates[0]; 8911efae8d2SViresh Kumar } else { 8921efae8d2SViresh Kumar WARN_ON(1); 8931efae8d2SViresh Kumar return -EINVAL; 8941efae8d2SViresh Kumar } 8951efae8d2SViresh Kumar 8961efae8d2SViresh Kumar ret = clk_set_rate(opp_table->clk, freq); 8977813dd6fSViresh Kumar if (ret) { 8987813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 8997813dd6fSViresh Kumar ret); 9001efae8d2SViresh Kumar } else { 9011efae8d2SViresh Kumar opp_table->rate_clk_single = freq; 9027813dd6fSViresh Kumar } 9037813dd6fSViresh Kumar 9047813dd6fSViresh Kumar return ret; 9057813dd6fSViresh Kumar } 9067813dd6fSViresh Kumar 9078174a3a6SViresh Kumar /* 9088174a3a6SViresh Kumar * Simple implementation for configuring multiple clocks. Configure clocks in 9098174a3a6SViresh Kumar * the order in which they are present in the array while scaling up. 9108174a3a6SViresh Kumar */ 9118174a3a6SViresh Kumar int dev_pm_opp_config_clks_simple(struct device *dev, 9128174a3a6SViresh Kumar struct opp_table *opp_table, struct dev_pm_opp *opp, void *data, 9138174a3a6SViresh Kumar bool scaling_down) 9148174a3a6SViresh Kumar { 9158174a3a6SViresh Kumar int ret, i; 9168174a3a6SViresh Kumar 9178174a3a6SViresh Kumar if (scaling_down) { 9188174a3a6SViresh Kumar for (i = opp_table->clk_count - 1; i >= 0; i--) { 9198174a3a6SViresh Kumar ret = clk_set_rate(opp_table->clks[i], opp->rates[i]); 9208174a3a6SViresh Kumar if (ret) { 9218174a3a6SViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 9228174a3a6SViresh Kumar ret); 9238174a3a6SViresh Kumar return ret; 9248174a3a6SViresh Kumar } 9258174a3a6SViresh Kumar } 9268174a3a6SViresh Kumar } else { 9278174a3a6SViresh Kumar for (i = 0; i < opp_table->clk_count; i++) { 9288174a3a6SViresh Kumar ret = clk_set_rate(opp_table->clks[i], opp->rates[i]); 9298174a3a6SViresh Kumar if (ret) { 9308174a3a6SViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 9318174a3a6SViresh Kumar ret); 9328174a3a6SViresh Kumar return ret; 9338174a3a6SViresh Kumar } 9348174a3a6SViresh Kumar } 9358174a3a6SViresh Kumar } 9368174a3a6SViresh Kumar 937d36cb843SChristophe JAILLET return 0; 9388174a3a6SViresh Kumar } 9398174a3a6SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_config_clks_simple); 9408174a3a6SViresh Kumar 941c522ce8aSViresh Kumar static int _opp_config_regulator_single(struct device *dev, 942c522ce8aSViresh Kumar struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp, 943c522ce8aSViresh Kumar struct regulator **regulators, unsigned int count) 9447813dd6fSViresh Kumar { 945c522ce8aSViresh Kumar struct regulator *reg = regulators[0]; 9467813dd6fSViresh Kumar int ret; 9477813dd6fSViresh Kumar 9487813dd6fSViresh Kumar /* This function only supports single regulator per device */ 949c522ce8aSViresh Kumar if (WARN_ON(count > 1)) { 9507813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 9517813dd6fSViresh Kumar return -EINVAL; 9527813dd6fSViresh Kumar } 9537813dd6fSViresh Kumar 954c522ce8aSViresh Kumar ret = _set_opp_voltage(dev, reg, new_opp->supplies); 9557813dd6fSViresh Kumar if (ret) 956c522ce8aSViresh Kumar return ret; 9577813dd6fSViresh Kumar 9588d45719cSKamil Konieczny /* 9598d45719cSKamil Konieczny * Enable the regulator after setting its voltages, otherwise it breaks 9608d45719cSKamil Konieczny * some boot-enabled regulators. 9618d45719cSKamil Konieczny */ 962c522ce8aSViresh Kumar if (unlikely(!new_opp->opp_table->enabled)) { 9638d45719cSKamil Konieczny ret = regulator_enable(reg); 9648d45719cSKamil Konieczny if (ret < 0) 9658d45719cSKamil Konieczny dev_warn(dev, "Failed to enable regulator: %d", ret); 9668d45719cSKamil Konieczny } 9678d45719cSKamil Konieczny 9687813dd6fSViresh Kumar return 0; 9697813dd6fSViresh Kumar } 9707813dd6fSViresh Kumar 971b00e667aSViresh Kumar static int _set_opp_bw(const struct opp_table *opp_table, 972240ae50eSViresh Kumar struct dev_pm_opp *opp, struct device *dev) 973b00e667aSViresh Kumar { 974b00e667aSViresh Kumar u32 avg, peak; 975b00e667aSViresh Kumar int i, ret; 976b00e667aSViresh Kumar 977b00e667aSViresh Kumar if (!opp_table->paths) 978b00e667aSViresh Kumar return 0; 979b00e667aSViresh Kumar 980b00e667aSViresh Kumar for (i = 0; i < opp_table->path_count; i++) { 981240ae50eSViresh Kumar if (!opp) { 982b00e667aSViresh Kumar avg = 0; 983b00e667aSViresh Kumar peak = 0; 984b00e667aSViresh Kumar } else { 985b00e667aSViresh Kumar avg = opp->bandwidth[i].avg; 986b00e667aSViresh Kumar peak = opp->bandwidth[i].peak; 987b00e667aSViresh Kumar } 988b00e667aSViresh Kumar ret = icc_set_bw(opp_table->paths[i], avg, peak); 989b00e667aSViresh Kumar if (ret) { 990b00e667aSViresh Kumar dev_err(dev, "Failed to %s bandwidth[%d]: %d\n", 991240ae50eSViresh Kumar opp ? "set" : "remove", i, ret); 992b00e667aSViresh Kumar return ret; 993b00e667aSViresh Kumar } 994b00e667aSViresh Kumar } 995b00e667aSViresh Kumar 996b00e667aSViresh Kumar return 0; 997b00e667aSViresh Kumar } 998b00e667aSViresh Kumar 999528f2d8dSViresh Kumar static int _set_performance_state(struct device *dev, struct device *pd_dev, 100060cdeae0SStephan Gerhold struct dev_pm_opp *opp, int i) 100160cdeae0SStephan Gerhold { 10027c41cdcdSViresh Kumar unsigned int pstate = likely(opp) ? opp->required_opps[i]->level: 0; 100360cdeae0SStephan Gerhold int ret; 100460cdeae0SStephan Gerhold 100560cdeae0SStephan Gerhold if (!pd_dev) 100660cdeae0SStephan Gerhold return 0; 100760cdeae0SStephan Gerhold 100860cdeae0SStephan Gerhold ret = dev_pm_genpd_set_performance_state(pd_dev, pstate); 100960cdeae0SStephan Gerhold if (ret) { 10109bfb1fffSViresh Kumar dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", 101160cdeae0SStephan Gerhold dev_name(pd_dev), pstate, ret); 101260cdeae0SStephan Gerhold } 101360cdeae0SStephan Gerhold 101460cdeae0SStephan Gerhold return ret; 101560cdeae0SStephan Gerhold } 101660cdeae0SStephan Gerhold 1017528f2d8dSViresh Kumar static int _opp_set_required_opps_generic(struct device *dev, 1018528f2d8dSViresh Kumar struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down) 1019ca1b5d77SViresh Kumar { 1020528f2d8dSViresh Kumar dev_err(dev, "setting required-opps isn't supported for non-genpd devices\n"); 1021528f2d8dSViresh Kumar return -ENOENT; 1022528f2d8dSViresh Kumar } 1023528f2d8dSViresh Kumar 1024528f2d8dSViresh Kumar static int _opp_set_required_opps_genpd(struct device *dev, 1025528f2d8dSViresh Kumar struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down) 1026528f2d8dSViresh Kumar { 102729b1a92eSViresh Kumar struct device **genpd_virt_devs = 102829b1a92eSViresh Kumar opp_table->genpd_virt_devs ? opp_table->genpd_virt_devs : &dev; 1029ca1b5d77SViresh Kumar int i, ret = 0; 1030ca1b5d77SViresh Kumar 1031ca1b5d77SViresh Kumar /* 1032ca1b5d77SViresh Kumar * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 1033ca1b5d77SViresh Kumar * after it is freed from another thread. 1034ca1b5d77SViresh Kumar */ 1035ca1b5d77SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 1036ca1b5d77SViresh Kumar 10372c59138cSStephan Gerhold /* Scaling up? Set required OPPs in normal order, else reverse */ 1038528f2d8dSViresh Kumar if (!scaling_down) { 1039ca1b5d77SViresh Kumar for (i = 0; i < opp_table->required_opp_count; i++) { 1040528f2d8dSViresh Kumar ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i); 104160cdeae0SStephan Gerhold if (ret) 1042ca1b5d77SViresh Kumar break; 1043ca1b5d77SViresh Kumar } 10442c59138cSStephan Gerhold } else { 10452c59138cSStephan Gerhold for (i = opp_table->required_opp_count - 1; i >= 0; i--) { 1046528f2d8dSViresh Kumar ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i); 10472c59138cSStephan Gerhold if (ret) 1048ca1b5d77SViresh Kumar break; 1049ca1b5d77SViresh Kumar } 1050ca1b5d77SViresh Kumar } 10512c59138cSStephan Gerhold 1052ca1b5d77SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 1053ca1b5d77SViresh Kumar 1054ca1b5d77SViresh Kumar return ret; 1055ca1b5d77SViresh Kumar } 1056ca1b5d77SViresh Kumar 1057528f2d8dSViresh Kumar /* This is only called for PM domain for now */ 1058528f2d8dSViresh Kumar static int _set_required_opps(struct device *dev, struct opp_table *opp_table, 1059528f2d8dSViresh Kumar struct dev_pm_opp *opp, bool up) 1060528f2d8dSViresh Kumar { 1061528f2d8dSViresh Kumar /* required-opps not fully initialized yet */ 1062528f2d8dSViresh Kumar if (lazy_linking_pending(opp_table)) 1063528f2d8dSViresh Kumar return -EBUSY; 1064528f2d8dSViresh Kumar 1065528f2d8dSViresh Kumar if (opp_table->set_required_opps) 1066528f2d8dSViresh Kumar return opp_table->set_required_opps(dev, opp_table, opp, up); 1067528f2d8dSViresh Kumar 1068528f2d8dSViresh Kumar return 0; 1069528f2d8dSViresh Kumar } 1070528f2d8dSViresh Kumar 1071528f2d8dSViresh Kumar /* Update set_required_opps handler */ 1072528f2d8dSViresh Kumar void _update_set_required_opps(struct opp_table *opp_table) 1073528f2d8dSViresh Kumar { 1074528f2d8dSViresh Kumar /* Already set */ 1075528f2d8dSViresh Kumar if (opp_table->set_required_opps) 1076528f2d8dSViresh Kumar return; 1077528f2d8dSViresh Kumar 1078528f2d8dSViresh Kumar /* All required OPPs will belong to genpd or none */ 1079528f2d8dSViresh Kumar if (opp_table->required_opp_tables[0]->is_genpd) 1080528f2d8dSViresh Kumar opp_table->set_required_opps = _opp_set_required_opps_genpd; 1081528f2d8dSViresh Kumar else 1082528f2d8dSViresh Kumar opp_table->set_required_opps = _opp_set_required_opps_generic; 1083528f2d8dSViresh Kumar } 1084528f2d8dSViresh Kumar 108581c4d8a3SViresh Kumar static void _find_current_opp(struct device *dev, struct opp_table *opp_table) 108681c4d8a3SViresh Kumar { 108781c4d8a3SViresh Kumar struct dev_pm_opp *opp = ERR_PTR(-ENODEV); 108881c4d8a3SViresh Kumar unsigned long freq; 108981c4d8a3SViresh Kumar 109081c4d8a3SViresh Kumar if (!IS_ERR(opp_table->clk)) { 109181c4d8a3SViresh Kumar freq = clk_get_rate(opp_table->clk); 109281c4d8a3SViresh Kumar opp = _find_freq_ceil(opp_table, &freq); 109381c4d8a3SViresh Kumar } 109481c4d8a3SViresh Kumar 109581c4d8a3SViresh Kumar /* 109681c4d8a3SViresh Kumar * Unable to find the current OPP ? Pick the first from the list since 109781c4d8a3SViresh Kumar * it is in ascending order, otherwise rest of the code will need to 109881c4d8a3SViresh Kumar * make special checks to validate current_opp. 109981c4d8a3SViresh Kumar */ 110081c4d8a3SViresh Kumar if (IS_ERR(opp)) { 110181c4d8a3SViresh Kumar mutex_lock(&opp_table->lock); 110281c4d8a3SViresh Kumar opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node); 110381c4d8a3SViresh Kumar dev_pm_opp_get(opp); 110481c4d8a3SViresh Kumar mutex_unlock(&opp_table->lock); 110581c4d8a3SViresh Kumar } 110681c4d8a3SViresh Kumar 110781c4d8a3SViresh Kumar opp_table->current_opp = opp; 110881c4d8a3SViresh Kumar } 110981c4d8a3SViresh Kumar 11105ad58bbaSViresh Kumar static int _disable_opp_table(struct device *dev, struct opp_table *opp_table) 1111f3364e17SViresh Kumar { 1112f3364e17SViresh Kumar int ret; 1113f3364e17SViresh Kumar 1114f3364e17SViresh Kumar if (!opp_table->enabled) 1115f3364e17SViresh Kumar return 0; 1116f3364e17SViresh Kumar 1117f3364e17SViresh Kumar /* 1118f3364e17SViresh Kumar * Some drivers need to support cases where some platforms may 1119f3364e17SViresh Kumar * have OPP table for the device, while others don't and 1120f3364e17SViresh Kumar * opp_set_rate() just needs to behave like clk_set_rate(). 1121f3364e17SViresh Kumar */ 1122f3364e17SViresh Kumar if (!_get_opp_count(opp_table)) 1123f3364e17SViresh Kumar return 0; 1124f3364e17SViresh Kumar 1125240ae50eSViresh Kumar ret = _set_opp_bw(opp_table, NULL, dev); 1126f3364e17SViresh Kumar if (ret) 1127f3364e17SViresh Kumar return ret; 1128f3364e17SViresh Kumar 1129f3364e17SViresh Kumar if (opp_table->regulators) 1130f3364e17SViresh Kumar regulator_disable(opp_table->regulators[0]); 1131f3364e17SViresh Kumar 11322c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, NULL, false); 1133f3364e17SViresh Kumar 1134f3364e17SViresh Kumar opp_table->enabled = false; 1135f3364e17SViresh Kumar return ret; 1136f3364e17SViresh Kumar } 1137f3364e17SViresh Kumar 1138386ba854SViresh Kumar static int _set_opp(struct device *dev, struct opp_table *opp_table, 11391efae8d2SViresh Kumar struct dev_pm_opp *opp, void *clk_data, bool forced) 11407813dd6fSViresh Kumar { 1141386ba854SViresh Kumar struct dev_pm_opp *old_opp; 1142f0b88fa4SViresh Kumar int scaling_down, ret; 11437813dd6fSViresh Kumar 1144386ba854SViresh Kumar if (unlikely(!opp)) 1145386ba854SViresh Kumar return _disable_opp_table(dev, opp_table); 1146aca48b61SRajendra Nayak 114781c4d8a3SViresh Kumar /* Find the currently set OPP if we don't know already */ 114881c4d8a3SViresh Kumar if (unlikely(!opp_table->current_opp)) 114981c4d8a3SViresh Kumar _find_current_opp(dev, opp_table); 11507813dd6fSViresh Kumar 115181c4d8a3SViresh Kumar old_opp = opp_table->current_opp; 115281c4d8a3SViresh Kumar 115381c4d8a3SViresh Kumar /* Return early if nothing to do */ 11541efae8d2SViresh Kumar if (!forced && old_opp == opp && opp_table->enabled) { 11559e28f7a7SAdrián Larumbe dev_dbg_ratelimited(dev, "%s: OPPs are same, nothing to do\n", __func__); 1156386ba854SViresh Kumar return 0; 11577813dd6fSViresh Kumar } 11587813dd6fSViresh Kumar 1159f0b88fa4SViresh Kumar dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n", 11602083da24SViresh Kumar __func__, old_opp->rates[0], opp->rates[0], old_opp->level, 11612083da24SViresh Kumar opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0, 1162f0b88fa4SViresh Kumar opp->bandwidth ? opp->bandwidth[0].peak : 0); 1163f0b88fa4SViresh Kumar 11642083da24SViresh Kumar scaling_down = _opp_compare_key(opp_table, old_opp, opp); 1165f0b88fa4SViresh Kumar if (scaling_down == -1) 1166f0b88fa4SViresh Kumar scaling_down = 0; 11677813dd6fSViresh Kumar 1168ca1b5d77SViresh Kumar /* Scaling up? Configure required OPPs before frequency */ 1169f0b88fa4SViresh Kumar if (!scaling_down) { 11702c59138cSStephan Gerhold ret = _set_required_opps(dev, opp_table, opp, true); 1171870d5d96SViresh Kumar if (ret) { 1172870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1173386ba854SViresh Kumar return ret; 1174ca1b5d77SViresh Kumar } 1175ca1b5d77SViresh Kumar 1176870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1177870d5d96SViresh Kumar if (ret) { 1178870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1179870d5d96SViresh Kumar return ret; 1180870d5d96SViresh Kumar } 1181aee3352fSViresh Kumar 1182aee3352fSViresh Kumar if (opp_table->config_regulators) { 1183aee3352fSViresh Kumar ret = opp_table->config_regulators(dev, old_opp, opp, 1184aee3352fSViresh Kumar opp_table->regulators, 1185aee3352fSViresh Kumar opp_table->regulator_count); 1186aee3352fSViresh Kumar if (ret) { 1187aee3352fSViresh Kumar dev_err(dev, "Failed to set regulator voltages: %d\n", 1188aee3352fSViresh Kumar ret); 1189aee3352fSViresh Kumar return ret; 1190aee3352fSViresh Kumar } 1191aee3352fSViresh Kumar } 1192870d5d96SViresh Kumar } 1193870d5d96SViresh Kumar 11942083da24SViresh Kumar if (opp_table->config_clks) { 11952083da24SViresh Kumar ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down); 1196ca1b5d77SViresh Kumar if (ret) 1197870d5d96SViresh Kumar return ret; 11982083da24SViresh Kumar } 1199870d5d96SViresh Kumar 1200870d5d96SViresh Kumar /* Scaling down? Configure required OPPs after frequency */ 1201870d5d96SViresh Kumar if (scaling_down) { 1202aee3352fSViresh Kumar if (opp_table->config_regulators) { 1203aee3352fSViresh Kumar ret = opp_table->config_regulators(dev, old_opp, opp, 1204aee3352fSViresh Kumar opp_table->regulators, 1205aee3352fSViresh Kumar opp_table->regulator_count); 1206aee3352fSViresh Kumar if (ret) { 1207aee3352fSViresh Kumar dev_err(dev, "Failed to set regulator voltages: %d\n", 1208aee3352fSViresh Kumar ret); 1209aee3352fSViresh Kumar return ret; 1210aee3352fSViresh Kumar } 1211aee3352fSViresh Kumar } 1212aee3352fSViresh Kumar 1213870d5d96SViresh Kumar ret = _set_opp_bw(opp_table, opp, dev); 1214870d5d96SViresh Kumar if (ret) { 1215870d5d96SViresh Kumar dev_err(dev, "Failed to set bw: %d\n", ret); 1216870d5d96SViresh Kumar return ret; 1217ca1b5d77SViresh Kumar } 1218ca1b5d77SViresh Kumar 1219870d5d96SViresh Kumar ret = _set_required_opps(dev, opp_table, opp, false); 1220870d5d96SViresh Kumar if (ret) { 1221870d5d96SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 1222870d5d96SViresh Kumar return ret; 1223870d5d96SViresh Kumar } 1224870d5d96SViresh Kumar } 1225870d5d96SViresh Kumar 122672f80ce4SViresh Kumar opp_table->enabled = true; 122781c4d8a3SViresh Kumar dev_pm_opp_put(old_opp); 122881c4d8a3SViresh Kumar 122981c4d8a3SViresh Kumar /* Make sure current_opp doesn't get freed */ 123081c4d8a3SViresh Kumar dev_pm_opp_get(opp); 123181c4d8a3SViresh Kumar opp_table->current_opp = opp; 1232fe2af402SGeorgi Djakov 1233386ba854SViresh Kumar return ret; 1234386ba854SViresh Kumar } 1235386ba854SViresh Kumar 1236386ba854SViresh Kumar /** 1237386ba854SViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 1238386ba854SViresh Kumar * @dev: device for which we do this operation 1239386ba854SViresh Kumar * @target_freq: frequency to achieve 1240386ba854SViresh Kumar * 1241386ba854SViresh Kumar * This configures the power-supplies to the levels specified by the OPP 1242386ba854SViresh Kumar * corresponding to the target_freq, and programs the clock to a value <= 1243386ba854SViresh Kumar * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax 1244386ba854SViresh Kumar * provided by the opp, should have already rounded to the target OPP's 1245386ba854SViresh Kumar * frequency. 1246386ba854SViresh Kumar */ 1247386ba854SViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 1248386ba854SViresh Kumar { 1249386ba854SViresh Kumar struct opp_table *opp_table; 1250386ba854SViresh Kumar unsigned long freq = 0, temp_freq; 1251386ba854SViresh Kumar struct dev_pm_opp *opp = NULL; 12521efae8d2SViresh Kumar bool forced = false; 1253386ba854SViresh Kumar int ret; 1254386ba854SViresh Kumar 1255386ba854SViresh Kumar opp_table = _find_opp_table(dev); 1256386ba854SViresh Kumar if (IS_ERR(opp_table)) { 1257386ba854SViresh Kumar dev_err(dev, "%s: device's opp table doesn't exist\n", __func__); 1258386ba854SViresh Kumar return PTR_ERR(opp_table); 1259386ba854SViresh Kumar } 1260386ba854SViresh Kumar 1261386ba854SViresh Kumar if (target_freq) { 1262386ba854SViresh Kumar /* 1263386ba854SViresh Kumar * For IO devices which require an OPP on some platforms/SoCs 1264386ba854SViresh Kumar * while just needing to scale the clock on some others 1265386ba854SViresh Kumar * we look for empty OPP tables with just a clock handle and 1266386ba854SViresh Kumar * scale only the clk. This makes dev_pm_opp_set_rate() 1267386ba854SViresh Kumar * equivalent to a clk_set_rate() 1268386ba854SViresh Kumar */ 1269386ba854SViresh Kumar if (!_get_opp_count(opp_table)) { 12702083da24SViresh Kumar ret = opp_table->config_clks(dev, opp_table, NULL, 12712083da24SViresh Kumar &target_freq, false); 1272386ba854SViresh Kumar goto put_opp_table; 1273386ba854SViresh Kumar } 1274386ba854SViresh Kumar 1275386ba854SViresh Kumar freq = clk_round_rate(opp_table->clk, target_freq); 1276386ba854SViresh Kumar if ((long)freq <= 0) 1277386ba854SViresh Kumar freq = target_freq; 1278386ba854SViresh Kumar 1279386ba854SViresh Kumar /* 1280386ba854SViresh Kumar * The clock driver may support finer resolution of the 1281386ba854SViresh Kumar * frequencies than the OPP table, don't update the frequency we 1282386ba854SViresh Kumar * pass to clk_set_rate() here. 1283386ba854SViresh Kumar */ 1284386ba854SViresh Kumar temp_freq = freq; 1285386ba854SViresh Kumar opp = _find_freq_ceil(opp_table, &temp_freq); 1286386ba854SViresh Kumar if (IS_ERR(opp)) { 1287386ba854SViresh Kumar ret = PTR_ERR(opp); 1288386ba854SViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 1289386ba854SViresh Kumar __func__, freq, ret); 1290386ba854SViresh Kumar goto put_opp_table; 1291386ba854SViresh Kumar } 12921efae8d2SViresh Kumar 12931efae8d2SViresh Kumar /* 12941efae8d2SViresh Kumar * An OPP entry specifies the highest frequency at which other 12951efae8d2SViresh Kumar * properties of the OPP entry apply. Even if the new OPP is 12961efae8d2SViresh Kumar * same as the old one, we may still reach here for a different 12971efae8d2SViresh Kumar * value of the frequency. In such a case, do not abort but 12981efae8d2SViresh Kumar * configure the hardware to the desired frequency forcefully. 12991efae8d2SViresh Kumar */ 13001efae8d2SViresh Kumar forced = opp_table->rate_clk_single != target_freq; 1301386ba854SViresh Kumar } 1302386ba854SViresh Kumar 13031efae8d2SViresh Kumar ret = _set_opp(dev, opp_table, opp, &target_freq, forced); 1304386ba854SViresh Kumar 1305386ba854SViresh Kumar if (target_freq) 13067813dd6fSViresh Kumar dev_pm_opp_put(opp); 13071efae8d2SViresh Kumar 13087813dd6fSViresh Kumar put_opp_table: 13097813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 13107813dd6fSViresh Kumar return ret; 13117813dd6fSViresh Kumar } 13127813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 13137813dd6fSViresh Kumar 1314abbe3483SViresh Kumar /** 1315abbe3483SViresh Kumar * dev_pm_opp_set_opp() - Configure device for OPP 1316abbe3483SViresh Kumar * @dev: device for which we do this operation 1317abbe3483SViresh Kumar * @opp: OPP to set to 1318abbe3483SViresh Kumar * 1319abbe3483SViresh Kumar * This configures the device based on the properties of the OPP passed to this 1320abbe3483SViresh Kumar * routine. 1321abbe3483SViresh Kumar * 1322abbe3483SViresh Kumar * Return: 0 on success, a negative error number otherwise. 1323abbe3483SViresh Kumar */ 1324abbe3483SViresh Kumar int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp) 1325abbe3483SViresh Kumar { 1326abbe3483SViresh Kumar struct opp_table *opp_table; 1327abbe3483SViresh Kumar int ret; 1328abbe3483SViresh Kumar 1329abbe3483SViresh Kumar opp_table = _find_opp_table(dev); 1330abbe3483SViresh Kumar if (IS_ERR(opp_table)) { 1331abbe3483SViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 1332abbe3483SViresh Kumar return PTR_ERR(opp_table); 1333abbe3483SViresh Kumar } 1334abbe3483SViresh Kumar 13351efae8d2SViresh Kumar ret = _set_opp(dev, opp_table, opp, NULL, false); 1336abbe3483SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1337abbe3483SViresh Kumar 1338abbe3483SViresh Kumar return ret; 1339abbe3483SViresh Kumar } 1340abbe3483SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp); 1341abbe3483SViresh Kumar 13427813dd6fSViresh Kumar /* OPP-dev Helpers */ 13437813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 13447813dd6fSViresh Kumar struct opp_table *opp_table) 13457813dd6fSViresh Kumar { 13467813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 13477813dd6fSViresh Kumar list_del(&opp_dev->node); 13487813dd6fSViresh Kumar kfree(opp_dev); 13497813dd6fSViresh Kumar } 13507813dd6fSViresh Kumar 1351ef43f01aSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 13527813dd6fSViresh Kumar struct opp_table *opp_table) 13537813dd6fSViresh Kumar { 13547813dd6fSViresh Kumar struct opp_device *opp_dev; 13557813dd6fSViresh Kumar 13567813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 13577813dd6fSViresh Kumar if (!opp_dev) 13587813dd6fSViresh Kumar return NULL; 13597813dd6fSViresh Kumar 13607813dd6fSViresh Kumar /* Initialize opp-dev */ 13617813dd6fSViresh Kumar opp_dev->dev = dev; 13623d255699SViresh Kumar 1363ef43f01aSViresh Kumar mutex_lock(&opp_table->lock); 13647813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 1365ef43f01aSViresh Kumar mutex_unlock(&opp_table->lock); 13667813dd6fSViresh Kumar 13677813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 1368a2dea4cbSGreg Kroah-Hartman opp_debug_register(opp_dev, opp_table); 1369283d55e6SViresh Kumar 1370283d55e6SViresh Kumar return opp_dev; 1371283d55e6SViresh Kumar } 1372283d55e6SViresh Kumar 1373eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index) 13747813dd6fSViresh Kumar { 13757813dd6fSViresh Kumar struct opp_table *opp_table; 13767813dd6fSViresh Kumar struct opp_device *opp_dev; 13777813dd6fSViresh Kumar int ret; 13787813dd6fSViresh Kumar 13797813dd6fSViresh Kumar /* 13807813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 13817813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 13827813dd6fSViresh Kumar */ 13837813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 13847813dd6fSViresh Kumar if (!opp_table) 1385dd461cd9SStephan Gerhold return ERR_PTR(-ENOMEM); 13867813dd6fSViresh Kumar 13873d255699SViresh Kumar mutex_init(&opp_table->lock); 13884f018bc0SViresh Kumar mutex_init(&opp_table->genpd_virt_dev_lock); 13897813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 13907eba0c76SViresh Kumar INIT_LIST_HEAD(&opp_table->lazy); 13917813dd6fSViresh Kumar 13922083da24SViresh Kumar opp_table->clk = ERR_PTR(-ENODEV); 13932083da24SViresh Kumar 139446f48acaSViresh Kumar /* Mark regulator count uninitialized */ 139546f48acaSViresh Kumar opp_table->regulator_count = -1; 139646f48acaSViresh Kumar 13977813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 13987813dd6fSViresh Kumar if (!opp_dev) { 1399dd461cd9SStephan Gerhold ret = -ENOMEM; 1400dd461cd9SStephan Gerhold goto err; 14017813dd6fSViresh Kumar } 14027813dd6fSViresh Kumar 1403eb7c8743SViresh Kumar _of_init_opp_table(opp_table, dev, index); 14047813dd6fSViresh Kumar 14056d3f922cSGeorgi Djakov /* Find interconnect path(s) for the device */ 14066d3f922cSGeorgi Djakov ret = dev_pm_opp_of_find_icc_paths(dev, opp_table); 1407dd461cd9SStephan Gerhold if (ret) { 1408dd461cd9SStephan Gerhold if (ret == -EPROBE_DEFER) 140932439ac7SViresh Kumar goto remove_opp_dev; 1410dd461cd9SStephan Gerhold 14116d3f922cSGeorgi Djakov dev_warn(dev, "%s: Error finding interconnect paths: %d\n", 14126d3f922cSGeorgi Djakov __func__, ret); 1413dd461cd9SStephan Gerhold } 14146d3f922cSGeorgi Djakov 14157813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 14167813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 14177813dd6fSViresh Kumar kref_init(&opp_table->kref); 14187813dd6fSViresh Kumar 14197813dd6fSViresh Kumar return opp_table; 1420dd461cd9SStephan Gerhold 1421976509bbSQuanyang Wang remove_opp_dev: 1422b2a2ab03SStephan Gerhold _of_clear_opp_table(opp_table); 1423976509bbSQuanyang Wang _remove_opp_dev(opp_dev, opp_table); 1424b2a2ab03SStephan Gerhold mutex_destroy(&opp_table->genpd_virt_dev_lock); 1425b2a2ab03SStephan Gerhold mutex_destroy(&opp_table->lock); 1426dd461cd9SStephan Gerhold err: 1427dd461cd9SStephan Gerhold kfree(opp_table); 1428dd461cd9SStephan Gerhold return ERR_PTR(ret); 14297813dd6fSViresh Kumar } 14307813dd6fSViresh Kumar 14317813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 14327813dd6fSViresh Kumar { 14337813dd6fSViresh Kumar kref_get(&opp_table->kref); 14347813dd6fSViresh Kumar } 14357813dd6fSViresh Kumar 143632439ac7SViresh Kumar static struct opp_table *_update_opp_table_clk(struct device *dev, 143732439ac7SViresh Kumar struct opp_table *opp_table, 143832439ac7SViresh Kumar bool getclk) 143932439ac7SViresh Kumar { 1440d4a4c7a4SViresh Kumar int ret; 1441d4a4c7a4SViresh Kumar 144232439ac7SViresh Kumar /* 14432083da24SViresh Kumar * Return early if we don't need to get clk or we have already done it 144432439ac7SViresh Kumar * earlier. 144532439ac7SViresh Kumar */ 14462083da24SViresh Kumar if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) || 14472083da24SViresh Kumar opp_table->clks) 144832439ac7SViresh Kumar return opp_table; 144932439ac7SViresh Kumar 145032439ac7SViresh Kumar /* Find clk for the device */ 145132439ac7SViresh Kumar opp_table->clk = clk_get(dev, NULL); 145232439ac7SViresh Kumar 1453d4a4c7a4SViresh Kumar ret = PTR_ERR_OR_ZERO(opp_table->clk); 14542083da24SViresh Kumar if (!ret) { 14552083da24SViresh Kumar opp_table->config_clks = _opp_config_clk_single; 14562083da24SViresh Kumar opp_table->clk_count = 1; 145732439ac7SViresh Kumar return opp_table; 14582083da24SViresh Kumar } 1459d4a4c7a4SViresh Kumar 1460d4a4c7a4SViresh Kumar if (ret == -ENOENT) { 14612083da24SViresh Kumar /* 14622083da24SViresh Kumar * There are few platforms which don't want the OPP core to 14632083da24SViresh Kumar * manage device's clock settings. In such cases neither the 14642083da24SViresh Kumar * platform provides the clks explicitly to us, nor the DT 14652083da24SViresh Kumar * contains a valid clk entry. The OPP nodes in DT may still 14662083da24SViresh Kumar * contain "opp-hz" property though, which we need to parse and 14672083da24SViresh Kumar * allow the platform to find an OPP based on freq later on. 14682083da24SViresh Kumar * 14692083da24SViresh Kumar * This is a simple solution to take care of such corner cases, 14702083da24SViresh Kumar * i.e. make the clk_count 1, which lets us allocate space for 14712083da24SViresh Kumar * frequency in opp->rates and also parse the entries in DT. 14722083da24SViresh Kumar */ 14732083da24SViresh Kumar opp_table->clk_count = 1; 14742083da24SViresh Kumar 1475d4a4c7a4SViresh Kumar dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret); 1476d4a4c7a4SViresh Kumar return opp_table; 1477d4a4c7a4SViresh Kumar } 1478d4a4c7a4SViresh Kumar 1479d4a4c7a4SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1480d4a4c7a4SViresh Kumar dev_err_probe(dev, ret, "Couldn't find clock\n"); 1481d4a4c7a4SViresh Kumar 1482d4a4c7a4SViresh Kumar return ERR_PTR(ret); 148332439ac7SViresh Kumar } 148432439ac7SViresh Kumar 148527c09484SViresh Kumar /* 148627c09484SViresh Kumar * We need to make sure that the OPP table for a device doesn't get added twice, 148727c09484SViresh Kumar * if this routine gets called in parallel with the same device pointer. 148827c09484SViresh Kumar * 148927c09484SViresh Kumar * The simplest way to enforce that is to perform everything (find existing 149027c09484SViresh Kumar * table and if not found, create a new one) under the opp_table_lock, so only 149127c09484SViresh Kumar * one creator gets access to the same. But that expands the critical section 149227c09484SViresh Kumar * under the lock and may end up causing circular dependencies with frameworks 149327c09484SViresh Kumar * like debugfs, interconnect or clock framework as they may be direct or 149427c09484SViresh Kumar * indirect users of OPP core. 149527c09484SViresh Kumar * 149627c09484SViresh Kumar * And for that reason we have to go for a bit tricky implementation here, which 149727c09484SViresh Kumar * uses the opp_tables_busy flag to indicate if another creator is in the middle 149827c09484SViresh Kumar * of adding an OPP table and others should wait for it to finish. 149927c09484SViresh Kumar */ 150032439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index, 150132439ac7SViresh Kumar bool getclk) 15027813dd6fSViresh Kumar { 15037813dd6fSViresh Kumar struct opp_table *opp_table; 15047813dd6fSViresh Kumar 150527c09484SViresh Kumar again: 15067813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 15077813dd6fSViresh Kumar 15087813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 15097813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 15107813dd6fSViresh Kumar goto unlock; 15117813dd6fSViresh Kumar 151227c09484SViresh Kumar /* 151327c09484SViresh Kumar * The opp_tables list or an OPP table's dev_list is getting updated by 151427c09484SViresh Kumar * another user, wait for it to finish. 151527c09484SViresh Kumar */ 151627c09484SViresh Kumar if (unlikely(opp_tables_busy)) { 151727c09484SViresh Kumar mutex_unlock(&opp_table_lock); 151827c09484SViresh Kumar cpu_relax(); 151927c09484SViresh Kumar goto again; 152027c09484SViresh Kumar } 152127c09484SViresh Kumar 152227c09484SViresh Kumar opp_tables_busy = true; 1523283d55e6SViresh Kumar opp_table = _managed_opp(dev, index); 152427c09484SViresh Kumar 152527c09484SViresh Kumar /* Drop the lock to reduce the size of critical section */ 152627c09484SViresh Kumar mutex_unlock(&opp_table_lock); 152727c09484SViresh Kumar 1528283d55e6SViresh Kumar if (opp_table) { 1529ef43f01aSViresh Kumar if (!_add_opp_dev(dev, opp_table)) { 1530283d55e6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1531dd461cd9SStephan Gerhold opp_table = ERR_PTR(-ENOMEM); 1532283d55e6SViresh Kumar } 153327c09484SViresh Kumar 153427c09484SViresh Kumar mutex_lock(&opp_table_lock); 153527c09484SViresh Kumar } else { 153627c09484SViresh Kumar opp_table = _allocate_opp_table(dev, index); 153727c09484SViresh Kumar 153827c09484SViresh Kumar mutex_lock(&opp_table_lock); 153927c09484SViresh Kumar if (!IS_ERR(opp_table)) 154027c09484SViresh Kumar list_add(&opp_table->node, &opp_tables); 1541283d55e6SViresh Kumar } 1542283d55e6SViresh Kumar 154327c09484SViresh Kumar opp_tables_busy = false; 15447813dd6fSViresh Kumar 15457813dd6fSViresh Kumar unlock: 15467813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 15477813dd6fSViresh Kumar 154832439ac7SViresh Kumar return _update_opp_table_clk(dev, opp_table, getclk); 15497813dd6fSViresh Kumar } 1550eb7c8743SViresh Kumar 155132439ac7SViresh Kumar static struct opp_table *_add_opp_table(struct device *dev, bool getclk) 1552e77dcb0bSViresh Kumar { 155332439ac7SViresh Kumar return _add_opp_table_indexed(dev, 0, getclk); 1554e77dcb0bSViresh Kumar } 1555e77dcb0bSViresh Kumar 1556eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 1557eb7c8743SViresh Kumar { 1558e77dcb0bSViresh Kumar return _find_opp_table(dev); 1559eb7c8743SViresh Kumar } 15607813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 15617813dd6fSViresh Kumar 15627813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 15637813dd6fSViresh Kumar { 15647813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 1565cdd6ed90SViresh Kumar struct opp_device *opp_dev, *temp; 15666d3f922cSGeorgi Djakov int i; 15677813dd6fSViresh Kumar 1568e0df59deSViresh Kumar /* Drop the lock as soon as we can */ 1569e0df59deSViresh Kumar list_del(&opp_table->node); 1570e0df59deSViresh Kumar mutex_unlock(&opp_table_lock); 1571e0df59deSViresh Kumar 157281c4d8a3SViresh Kumar if (opp_table->current_opp) 157381c4d8a3SViresh Kumar dev_pm_opp_put(opp_table->current_opp); 157481c4d8a3SViresh Kumar 15755d6d106fSViresh Kumar _of_clear_opp_table(opp_table); 15765d6d106fSViresh Kumar 15772083da24SViresh Kumar /* Release automatically acquired single clk */ 15787813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 15797813dd6fSViresh Kumar clk_put(opp_table->clk); 15807813dd6fSViresh Kumar 15816d3f922cSGeorgi Djakov if (opp_table->paths) { 15826d3f922cSGeorgi Djakov for (i = 0; i < opp_table->path_count; i++) 15836d3f922cSGeorgi Djakov icc_put(opp_table->paths[i]); 15846d3f922cSGeorgi Djakov kfree(opp_table->paths); 15856d3f922cSGeorgi Djakov } 15866d3f922cSGeorgi Djakov 1587cdd6ed90SViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 1588cdd6ed90SViresh Kumar 158904bd2eafSViresh Kumar list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) 15907813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 15917813dd6fSViresh Kumar 15924f018bc0SViresh Kumar mutex_destroy(&opp_table->genpd_virt_dev_lock); 15937813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 15947813dd6fSViresh Kumar kfree(opp_table); 15957813dd6fSViresh Kumar } 15967813dd6fSViresh Kumar 15977813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 15987813dd6fSViresh Kumar { 15997813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 16007813dd6fSViresh Kumar &opp_table_lock); 16017813dd6fSViresh Kumar } 16027813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 16037813dd6fSViresh Kumar 16047813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 16057813dd6fSViresh Kumar { 16067813dd6fSViresh Kumar kfree(opp); 16077813dd6fSViresh Kumar } 16087813dd6fSViresh Kumar 1609cf1fac94SViresh Kumar static void _opp_kref_release(struct kref *kref) 16107813dd6fSViresh Kumar { 1611cf1fac94SViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 1612cf1fac94SViresh Kumar struct opp_table *opp_table = opp->opp_table; 1613cf1fac94SViresh Kumar 1614cf1fac94SViresh Kumar list_del(&opp->node); 1615cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1616cf1fac94SViresh Kumar 16177813dd6fSViresh Kumar /* 16187813dd6fSViresh Kumar * Notify the changes in the availability of the operable 16197813dd6fSViresh Kumar * frequency/voltage list. 16207813dd6fSViresh Kumar */ 16217813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 16223466ea2cSLiang He _of_clear_opp(opp_table, opp); 16237813dd6fSViresh Kumar opp_debug_remove_one(opp); 16247813dd6fSViresh Kumar kfree(opp); 16251690d8bbSViresh Kumar } 16267813dd6fSViresh Kumar 1627a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 16287813dd6fSViresh Kumar { 16297813dd6fSViresh Kumar kref_get(&opp->kref); 16307813dd6fSViresh Kumar } 16317813dd6fSViresh Kumar 16327813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 16337813dd6fSViresh Kumar { 1634cf1fac94SViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 16357813dd6fSViresh Kumar } 16367813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 16377813dd6fSViresh Kumar 16387813dd6fSViresh Kumar /** 16397813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 16407813dd6fSViresh Kumar * @dev: device for which we do this operation 16417813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 16427813dd6fSViresh Kumar * 16437813dd6fSViresh Kumar * This function removes an opp from the opp table. 16447813dd6fSViresh Kumar */ 16457813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 16467813dd6fSViresh Kumar { 164795073b72SJakob Koschel struct dev_pm_opp *opp = NULL, *iter; 16487813dd6fSViresh Kumar struct opp_table *opp_table; 16497813dd6fSViresh Kumar 16507813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 16517813dd6fSViresh Kumar if (IS_ERR(opp_table)) 16527813dd6fSViresh Kumar return; 16537813dd6fSViresh Kumar 1654f123ea74SViresh Kumar if (!assert_single_clk(opp_table)) 1655f123ea74SViresh Kumar goto put_table; 1656f123ea74SViresh Kumar 16577813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 16587813dd6fSViresh Kumar 165995073b72SJakob Koschel list_for_each_entry(iter, &opp_table->opp_list, node) { 16602083da24SViresh Kumar if (iter->rates[0] == freq) { 166195073b72SJakob Koschel opp = iter; 16627813dd6fSViresh Kumar break; 16637813dd6fSViresh Kumar } 16647813dd6fSViresh Kumar } 16657813dd6fSViresh Kumar 16667813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 16677813dd6fSViresh Kumar 166895073b72SJakob Koschel if (opp) { 16697813dd6fSViresh Kumar dev_pm_opp_put(opp); 16700ad8c623SViresh Kumar 16710ad8c623SViresh Kumar /* Drop the reference taken by dev_pm_opp_add() */ 16720ad8c623SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16737813dd6fSViresh Kumar } else { 16747813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 16757813dd6fSViresh Kumar __func__, freq); 16767813dd6fSViresh Kumar } 16777813dd6fSViresh Kumar 1678f123ea74SViresh Kumar put_table: 16790ad8c623SViresh Kumar /* Drop the reference taken by _find_opp_table() */ 16807813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16817813dd6fSViresh Kumar } 16827813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 16837813dd6fSViresh Kumar 1684cf1fac94SViresh Kumar static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table, 1685cf1fac94SViresh Kumar bool dynamic) 1686cf1fac94SViresh Kumar { 1687cf1fac94SViresh Kumar struct dev_pm_opp *opp = NULL, *temp; 1688cf1fac94SViresh Kumar 1689cf1fac94SViresh Kumar mutex_lock(&opp_table->lock); 1690cf1fac94SViresh Kumar list_for_each_entry(temp, &opp_table->opp_list, node) { 1691606a5d42SBeata Michalska /* 1692606a5d42SBeata Michalska * Refcount must be dropped only once for each OPP by OPP core, 1693606a5d42SBeata Michalska * do that with help of "removed" flag. 1694606a5d42SBeata Michalska */ 1695606a5d42SBeata Michalska if (!temp->removed && dynamic == temp->dynamic) { 1696cf1fac94SViresh Kumar opp = temp; 1697cf1fac94SViresh Kumar break; 1698cf1fac94SViresh Kumar } 1699cf1fac94SViresh Kumar } 1700cf1fac94SViresh Kumar 1701cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1702cf1fac94SViresh Kumar return opp; 1703cf1fac94SViresh Kumar } 1704cf1fac94SViresh Kumar 1705606a5d42SBeata Michalska /* 1706606a5d42SBeata Michalska * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to 1707606a5d42SBeata Michalska * happen lock less to avoid circular dependency issues. This routine must be 1708606a5d42SBeata Michalska * called without the opp_table->lock held. 1709606a5d42SBeata Michalska */ 1710606a5d42SBeata Michalska static void _opp_remove_all(struct opp_table *opp_table, bool dynamic) 171103758d60SViresh Kumar { 1712cf1fac94SViresh Kumar struct dev_pm_opp *opp; 171303758d60SViresh Kumar 1714606a5d42SBeata Michalska while ((opp = _opp_get_next(opp_table, dynamic))) { 1715606a5d42SBeata Michalska opp->removed = true; 1716606a5d42SBeata Michalska dev_pm_opp_put(opp); 1717606a5d42SBeata Michalska 1718606a5d42SBeata Michalska /* Drop the references taken by dev_pm_opp_add() */ 1719606a5d42SBeata Michalska if (dynamic) 1720606a5d42SBeata Michalska dev_pm_opp_put_opp_table(opp_table); 1721606a5d42SBeata Michalska } 1722606a5d42SBeata Michalska } 1723606a5d42SBeata Michalska 1724606a5d42SBeata Michalska bool _opp_remove_all_static(struct opp_table *opp_table) 1725606a5d42SBeata Michalska { 172603758d60SViresh Kumar mutex_lock(&opp_table->lock); 172703758d60SViresh Kumar 1728922ff075SViresh Kumar if (!opp_table->parsed_static_opps) { 1729cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1730cf1fac94SViresh Kumar return false; 1731922ff075SViresh Kumar } 1732922ff075SViresh Kumar 1733cf1fac94SViresh Kumar if (--opp_table->parsed_static_opps) { 1734cf1fac94SViresh Kumar mutex_unlock(&opp_table->lock); 1735cf1fac94SViresh Kumar return true; 173603758d60SViresh Kumar } 173703758d60SViresh Kumar 173803758d60SViresh Kumar mutex_unlock(&opp_table->lock); 1739922ff075SViresh Kumar 1740606a5d42SBeata Michalska _opp_remove_all(opp_table, false); 1741cf1fac94SViresh Kumar return true; 174203758d60SViresh Kumar } 174303758d60SViresh Kumar 17441690d8bbSViresh Kumar /** 17451690d8bbSViresh Kumar * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 17461690d8bbSViresh Kumar * @dev: device for which we do this operation 17471690d8bbSViresh Kumar * 17481690d8bbSViresh Kumar * This function removes all dynamically created OPPs from the opp table. 17491690d8bbSViresh Kumar */ 17501690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev) 17511690d8bbSViresh Kumar { 17521690d8bbSViresh Kumar struct opp_table *opp_table; 17531690d8bbSViresh Kumar 17541690d8bbSViresh Kumar opp_table = _find_opp_table(dev); 17551690d8bbSViresh Kumar if (IS_ERR(opp_table)) 17561690d8bbSViresh Kumar return; 17571690d8bbSViresh Kumar 1758606a5d42SBeata Michalska _opp_remove_all(opp_table, true); 17591690d8bbSViresh Kumar 17601690d8bbSViresh Kumar /* Drop the reference taken by _find_opp_table() */ 17611690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17621690d8bbSViresh Kumar } 17631690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 17641690d8bbSViresh Kumar 1765d6134583SViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table) 17667813dd6fSViresh Kumar { 17677813dd6fSViresh Kumar struct dev_pm_opp *opp; 17682083da24SViresh Kumar int supply_count, supply_size, icc_size, clk_size; 17697813dd6fSViresh Kumar 17707813dd6fSViresh Kumar /* Allocate space for at least one supply */ 1771d6134583SViresh Kumar supply_count = opp_table->regulator_count > 0 ? 1772d6134583SViresh Kumar opp_table->regulator_count : 1; 17736d3f922cSGeorgi Djakov supply_size = sizeof(*opp->supplies) * supply_count; 17742083da24SViresh Kumar clk_size = sizeof(*opp->rates) * opp_table->clk_count; 1775d6134583SViresh Kumar icc_size = sizeof(*opp->bandwidth) * opp_table->path_count; 17767813dd6fSViresh Kumar 17777813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 17782083da24SViresh Kumar opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL); 17797813dd6fSViresh Kumar if (!opp) 17807813dd6fSViresh Kumar return NULL; 17817813dd6fSViresh Kumar 17822083da24SViresh Kumar /* Put the supplies, bw and clock at the end of the OPP structure */ 17837813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 17842083da24SViresh Kumar 17852083da24SViresh Kumar opp->rates = (unsigned long *)(opp->supplies + supply_count); 17862083da24SViresh Kumar 17876d3f922cSGeorgi Djakov if (icc_size) 17882083da24SViresh Kumar opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count); 17892083da24SViresh Kumar 17907813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 17917813dd6fSViresh Kumar 17927813dd6fSViresh Kumar return opp; 17937813dd6fSViresh Kumar } 17947813dd6fSViresh Kumar 17957813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 17967813dd6fSViresh Kumar struct opp_table *opp_table) 17977813dd6fSViresh Kumar { 17987813dd6fSViresh Kumar struct regulator *reg; 17997813dd6fSViresh Kumar int i; 18007813dd6fSViresh Kumar 180190e3577bSViresh Kumar if (!opp_table->regulators) 180290e3577bSViresh Kumar return true; 180390e3577bSViresh Kumar 18047813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 18057813dd6fSViresh Kumar reg = opp_table->regulators[i]; 18067813dd6fSViresh Kumar 18077813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 18087813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 18097813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 18107813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 18117813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 18127813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 18137813dd6fSViresh Kumar return false; 18147813dd6fSViresh Kumar } 18157813dd6fSViresh Kumar } 18167813dd6fSViresh Kumar 18177813dd6fSViresh Kumar return true; 18187813dd6fSViresh Kumar } 18197813dd6fSViresh Kumar 18202083da24SViresh Kumar static int _opp_compare_rate(struct opp_table *opp_table, 18212083da24SViresh Kumar struct dev_pm_opp *opp1, struct dev_pm_opp *opp2) 18222083da24SViresh Kumar { 18232083da24SViresh Kumar int i; 18242083da24SViresh Kumar 18252083da24SViresh Kumar for (i = 0; i < opp_table->clk_count; i++) { 18262083da24SViresh Kumar if (opp1->rates[i] != opp2->rates[i]) 18272083da24SViresh Kumar return opp1->rates[i] < opp2->rates[i] ? -1 : 1; 18282083da24SViresh Kumar } 18292083da24SViresh Kumar 18302083da24SViresh Kumar /* Same rates for both OPPs */ 18312083da24SViresh Kumar return 0; 18322083da24SViresh Kumar } 18332083da24SViresh Kumar 1834274c3e83SViresh Kumar static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1, 1835274c3e83SViresh Kumar struct dev_pm_opp *opp2) 1836274c3e83SViresh Kumar { 1837274c3e83SViresh Kumar int i; 1838274c3e83SViresh Kumar 1839274c3e83SViresh Kumar for (i = 0; i < opp_table->path_count; i++) { 1840274c3e83SViresh Kumar if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak) 1841274c3e83SViresh Kumar return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1; 1842274c3e83SViresh Kumar } 1843274c3e83SViresh Kumar 1844274c3e83SViresh Kumar /* Same bw for both OPPs */ 1845274c3e83SViresh Kumar return 0; 1846274c3e83SViresh Kumar } 1847274c3e83SViresh Kumar 18488bdac14bSViresh Kumar /* 18498bdac14bSViresh Kumar * Returns 18508bdac14bSViresh Kumar * 0: opp1 == opp2 18518bdac14bSViresh Kumar * 1: opp1 > opp2 18528bdac14bSViresh Kumar * -1: opp1 < opp2 18538bdac14bSViresh Kumar */ 18542083da24SViresh Kumar int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1, 18552083da24SViresh Kumar struct dev_pm_opp *opp2) 18566c591eecSSaravana Kannan { 18572083da24SViresh Kumar int ret; 18582083da24SViresh Kumar 18592083da24SViresh Kumar ret = _opp_compare_rate(opp_table, opp1, opp2); 18602083da24SViresh Kumar if (ret) 18612083da24SViresh Kumar return ret; 18622083da24SViresh Kumar 1863274c3e83SViresh Kumar ret = _opp_compare_bw(opp_table, opp1, opp2); 1864274c3e83SViresh Kumar if (ret) 1865274c3e83SViresh Kumar return ret; 18662083da24SViresh Kumar 18676c591eecSSaravana Kannan if (opp1->level != opp2->level) 18686c591eecSSaravana Kannan return opp1->level < opp2->level ? -1 : 1; 18692083da24SViresh Kumar 18702083da24SViresh Kumar /* Duplicate OPPs */ 18716c591eecSSaravana Kannan return 0; 18726c591eecSSaravana Kannan } 18736c591eecSSaravana Kannan 1874a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1875a1e8c136SViresh Kumar struct opp_table *opp_table, 1876a1e8c136SViresh Kumar struct list_head **head) 1877a1e8c136SViresh Kumar { 1878a1e8c136SViresh Kumar struct dev_pm_opp *opp; 18796c591eecSSaravana Kannan int opp_cmp; 1880a1e8c136SViresh Kumar 1881a1e8c136SViresh Kumar /* 1882a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1883a1e8c136SViresh Kumar * already present. 1884a1e8c136SViresh Kumar * 1885a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1886a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1887a1e8c136SViresh Kumar * loop. 1888a1e8c136SViresh Kumar */ 1889a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 18902083da24SViresh Kumar opp_cmp = _opp_compare_key(opp_table, new_opp, opp); 18916c591eecSSaravana Kannan if (opp_cmp > 0) { 1892a1e8c136SViresh Kumar *head = &opp->node; 1893a1e8c136SViresh Kumar continue; 1894a1e8c136SViresh Kumar } 1895a1e8c136SViresh Kumar 18966c591eecSSaravana Kannan if (opp_cmp < 0) 1897a1e8c136SViresh Kumar return 0; 1898a1e8c136SViresh Kumar 1899a1e8c136SViresh Kumar /* Duplicate OPPs */ 1900a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 19012083da24SViresh Kumar __func__, opp->rates[0], opp->supplies[0].u_volt, 19022083da24SViresh Kumar opp->available, new_opp->rates[0], 1903a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1904a1e8c136SViresh Kumar 1905a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1906a1e8c136SViresh Kumar return opp->available && 1907a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1908a1e8c136SViresh Kumar } 1909a1e8c136SViresh Kumar 1910a1e8c136SViresh Kumar return 0; 1911a1e8c136SViresh Kumar } 1912a1e8c136SViresh Kumar 19137eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count) 19147eba0c76SViresh Kumar { 19157eba0c76SViresh Kumar int i; 19167eba0c76SViresh Kumar 19177eba0c76SViresh Kumar for (i = 0; i < count; i++) { 19187eba0c76SViresh Kumar if (opp->required_opps[i]->available) 19197eba0c76SViresh Kumar continue; 19207eba0c76SViresh Kumar 19217eba0c76SViresh Kumar opp->available = false; 19227eba0c76SViresh Kumar pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n", 19232083da24SViresh Kumar __func__, opp->required_opps[i]->np, opp->rates[0]); 19247eba0c76SViresh Kumar return; 19257eba0c76SViresh Kumar } 19267eba0c76SViresh Kumar } 19277eba0c76SViresh Kumar 19287813dd6fSViresh Kumar /* 19297813dd6fSViresh Kumar * Returns: 19307813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 19317813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 19327813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 19337813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 19347813dd6fSViresh Kumar * kernel try to initialize the OPP table. 19357813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 19367813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 19377813dd6fSViresh Kumar */ 19387813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 19394768914bSViresh Kumar struct opp_table *opp_table) 19407813dd6fSViresh Kumar { 19417813dd6fSViresh Kumar struct list_head *head; 19427813dd6fSViresh Kumar int ret; 19437813dd6fSViresh Kumar 19447813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 19457813dd6fSViresh Kumar head = &opp_table->opp_list; 19467813dd6fSViresh Kumar 1947a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1948a1e8c136SViresh Kumar if (ret) { 19497813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 19507813dd6fSViresh Kumar return ret; 19517813dd6fSViresh Kumar } 19527813dd6fSViresh Kumar 19537813dd6fSViresh Kumar list_add(&new_opp->node, head); 19547813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 19557813dd6fSViresh Kumar 19567813dd6fSViresh Kumar new_opp->opp_table = opp_table; 19577813dd6fSViresh Kumar kref_init(&new_opp->kref); 19587813dd6fSViresh Kumar 1959a2dea4cbSGreg Kroah-Hartman opp_debug_create_one(new_opp, opp_table); 19607813dd6fSViresh Kumar 19617813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 19627813dd6fSViresh Kumar new_opp->available = false; 19637813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 19642083da24SViresh Kumar __func__, new_opp->rates[0]); 19657813dd6fSViresh Kumar } 19667813dd6fSViresh Kumar 19677eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 19687eba0c76SViresh Kumar if (lazy_linking_pending(opp_table)) 19697eba0c76SViresh Kumar return 0; 1970cf65948dSDmitry Osipenko 19717eba0c76SViresh Kumar _required_opps_available(new_opp, opp_table->required_opp_count); 1972cf65948dSDmitry Osipenko 19737813dd6fSViresh Kumar return 0; 19747813dd6fSViresh Kumar } 19757813dd6fSViresh Kumar 19767813dd6fSViresh Kumar /** 19777813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 19787813dd6fSViresh Kumar * @opp_table: OPP table 19797813dd6fSViresh Kumar * @dev: device for which we do this operation 19807813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 19817813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 19827813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 19837813dd6fSViresh Kumar * 19847813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 19857813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 19867813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 19877813dd6fSViresh Kumar * 19887813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 19897813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 19907813dd6fSViresh Kumar * 19917813dd6fSViresh Kumar * Return: 19927813dd6fSViresh Kumar * 0 On success OR 19937813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 19947813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 19957813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 19967813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 19977813dd6fSViresh Kumar */ 19987813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 19997813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 20007813dd6fSViresh Kumar { 20017813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 20027813dd6fSViresh Kumar unsigned long tol; 20037813dd6fSViresh Kumar int ret; 20047813dd6fSViresh Kumar 2005f123ea74SViresh Kumar if (!assert_single_clk(opp_table)) 2006f123ea74SViresh Kumar return -EINVAL; 2007f123ea74SViresh Kumar 20087813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 20097813dd6fSViresh Kumar if (!new_opp) 20107813dd6fSViresh Kumar return -ENOMEM; 20117813dd6fSViresh Kumar 20127813dd6fSViresh Kumar /* populate the opp table */ 20132083da24SViresh Kumar new_opp->rates[0] = freq; 20147813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 20157813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 20167813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 20177813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 20187813dd6fSViresh Kumar new_opp->available = true; 20197813dd6fSViresh Kumar new_opp->dynamic = dynamic; 20207813dd6fSViresh Kumar 20214768914bSViresh Kumar ret = _opp_add(dev, new_opp, opp_table); 20227813dd6fSViresh Kumar if (ret) { 20237813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 20247813dd6fSViresh Kumar if (ret == -EBUSY) 20257813dd6fSViresh Kumar ret = 0; 20267813dd6fSViresh Kumar goto free_opp; 20277813dd6fSViresh Kumar } 20287813dd6fSViresh Kumar 20297813dd6fSViresh Kumar /* 20307813dd6fSViresh Kumar * Notify the changes in the availability of the operable 20317813dd6fSViresh Kumar * frequency/voltage list. 20327813dd6fSViresh Kumar */ 20337813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 20347813dd6fSViresh Kumar return 0; 20357813dd6fSViresh Kumar 20367813dd6fSViresh Kumar free_opp: 20377813dd6fSViresh Kumar _opp_free(new_opp); 20387813dd6fSViresh Kumar 20397813dd6fSViresh Kumar return ret; 20407813dd6fSViresh Kumar } 20417813dd6fSViresh Kumar 20427813dd6fSViresh Kumar /** 204389f03984SViresh Kumar * _opp_set_supported_hw() - Set supported platforms 20447813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 20457813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 20467813dd6fSViresh Kumar * @count: Number of elements in the array. 20477813dd6fSViresh Kumar * 20487813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 20497813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 20507813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 20517813dd6fSViresh Kumar * property. 20527813dd6fSViresh Kumar */ 205389f03984SViresh Kumar static int _opp_set_supported_hw(struct opp_table *opp_table, 20547813dd6fSViresh Kumar const u32 *versions, unsigned int count) 20557813dd6fSViresh Kumar { 205625419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 205725419de1SViresh Kumar if (opp_table->supported_hw) 205889f03984SViresh Kumar return 0; 20597813dd6fSViresh Kumar 20607813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 20617813dd6fSViresh Kumar GFP_KERNEL); 206289f03984SViresh Kumar if (!opp_table->supported_hw) 206389f03984SViresh Kumar return -ENOMEM; 20647813dd6fSViresh Kumar 20657813dd6fSViresh Kumar opp_table->supported_hw_count = count; 20667813dd6fSViresh Kumar 206789f03984SViresh Kumar return 0; 20687813dd6fSViresh Kumar } 20697813dd6fSViresh Kumar 20707813dd6fSViresh Kumar /** 207189f03984SViresh Kumar * _opp_put_supported_hw() - Releases resources blocked for supported hw 207289f03984SViresh Kumar * @opp_table: OPP table returned by _opp_set_supported_hw(). 20737813dd6fSViresh Kumar * 20747813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 207589f03984SViresh Kumar * _opp_set_supported_hw(). Until this is called, the opp_table structure 20767813dd6fSViresh Kumar * will not be freed. 20777813dd6fSViresh Kumar */ 207889f03984SViresh Kumar static void _opp_put_supported_hw(struct opp_table *opp_table) 20797813dd6fSViresh Kumar { 208089f03984SViresh Kumar if (opp_table->supported_hw) { 20817813dd6fSViresh Kumar kfree(opp_table->supported_hw); 20827813dd6fSViresh Kumar opp_table->supported_hw = NULL; 20837813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 20847813dd6fSViresh Kumar } 20859c4f220fSYangtao Li } 20869c4f220fSYangtao Li 20879c4f220fSYangtao Li /** 2088298098e5SViresh Kumar * _opp_set_prop_name() - Set prop-extn name 20897813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 20907813dd6fSViresh Kumar * @name: name to postfix to properties. 20917813dd6fSViresh Kumar * 20927813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 20937813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 20947813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 20957813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 20967813dd6fSViresh Kumar */ 2097298098e5SViresh Kumar static int _opp_set_prop_name(struct opp_table *opp_table, const char *name) 20987813dd6fSViresh Kumar { 2099878ec1a9SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 21007813dd6fSViresh Kumar if (!opp_table->prop_name) { 2101298098e5SViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 2102298098e5SViresh Kumar if (!opp_table->prop_name) 2103298098e5SViresh Kumar return -ENOMEM; 21047813dd6fSViresh Kumar } 21057813dd6fSViresh Kumar 2106298098e5SViresh Kumar return 0; 21077813dd6fSViresh Kumar } 21087813dd6fSViresh Kumar 21097813dd6fSViresh Kumar /** 2110298098e5SViresh Kumar * _opp_put_prop_name() - Releases resources blocked for prop-name 2111298098e5SViresh Kumar * @opp_table: OPP table returned by _opp_set_prop_name(). 21127813dd6fSViresh Kumar * 21137813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 2114298098e5SViresh Kumar * _opp_set_prop_name(). Until this is called, the opp_table structure 21157813dd6fSViresh Kumar * will not be freed. 21167813dd6fSViresh Kumar */ 2117298098e5SViresh Kumar static void _opp_put_prop_name(struct opp_table *opp_table) 21187813dd6fSViresh Kumar { 2119298098e5SViresh Kumar if (opp_table->prop_name) { 21207813dd6fSViresh Kumar kfree(opp_table->prop_name); 21217813dd6fSViresh Kumar opp_table->prop_name = NULL; 21227813dd6fSViresh Kumar } 2123298098e5SViresh Kumar } 21247813dd6fSViresh Kumar 21257813dd6fSViresh Kumar /** 2126b0ec0942SViresh Kumar * _opp_set_regulators() - Set regulator names for the device 21277813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 21287813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 21297813dd6fSViresh Kumar * @count: Number of regulators. 21307813dd6fSViresh Kumar * 21317813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 21327813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 21337813dd6fSViresh Kumar * well. 21347813dd6fSViresh Kumar * 21357813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 21367813dd6fSViresh Kumar */ 2137b0ec0942SViresh Kumar static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev, 213887686cc8SViresh Kumar const char * const names[]) 21397813dd6fSViresh Kumar { 214087686cc8SViresh Kumar const char * const *temp = names; 21417813dd6fSViresh Kumar struct regulator *reg; 214287686cc8SViresh Kumar int count = 0, ret, i; 214387686cc8SViresh Kumar 214487686cc8SViresh Kumar /* Count number of regulators */ 214587686cc8SViresh Kumar while (*temp++) 214687686cc8SViresh Kumar count++; 214787686cc8SViresh Kumar 214887686cc8SViresh Kumar if (!count) 2149b0ec0942SViresh Kumar return -EINVAL; 21507813dd6fSViresh Kumar 2151779b783cSViresh Kumar /* Another CPU that shares the OPP table has set the regulators ? */ 2152779b783cSViresh Kumar if (opp_table->regulators) 2153b0ec0942SViresh Kumar return 0; 21547813dd6fSViresh Kumar 21557813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 21567813dd6fSViresh Kumar sizeof(*opp_table->regulators), 21577813dd6fSViresh Kumar GFP_KERNEL); 2158b0ec0942SViresh Kumar if (!opp_table->regulators) 2159b0ec0942SViresh Kumar return -ENOMEM; 21607813dd6fSViresh Kumar 21617813dd6fSViresh Kumar for (i = 0; i < count; i++) { 21627813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 21637813dd6fSViresh Kumar if (IS_ERR(reg)) { 2164543256d2SKrzysztof Kozlowski ret = dev_err_probe(dev, PTR_ERR(reg), 2165543256d2SKrzysztof Kozlowski "%s: no regulator (%s) found\n", 2166543256d2SKrzysztof Kozlowski __func__, names[i]); 21677813dd6fSViresh Kumar goto free_regulators; 21687813dd6fSViresh Kumar } 21697813dd6fSViresh Kumar 21707813dd6fSViresh Kumar opp_table->regulators[i] = reg; 21717813dd6fSViresh Kumar } 21727813dd6fSViresh Kumar 21737813dd6fSViresh Kumar opp_table->regulator_count = count; 21747813dd6fSViresh Kumar 2175c522ce8aSViresh Kumar /* Set generic config_regulators() for single regulators here */ 2176c522ce8aSViresh Kumar if (count == 1) 2177c522ce8aSViresh Kumar opp_table->config_regulators = _opp_config_regulator_single; 2178c522ce8aSViresh Kumar 2179b0ec0942SViresh Kumar return 0; 21807813dd6fSViresh Kumar 21817813dd6fSViresh Kumar free_regulators: 218224957db1SMarek Szyprowski while (i != 0) 218324957db1SMarek Szyprowski regulator_put(opp_table->regulators[--i]); 21847813dd6fSViresh Kumar 21857813dd6fSViresh Kumar kfree(opp_table->regulators); 21867813dd6fSViresh Kumar opp_table->regulators = NULL; 218746f48acaSViresh Kumar opp_table->regulator_count = -1; 21887813dd6fSViresh Kumar 2189b0ec0942SViresh Kumar return ret; 21907813dd6fSViresh Kumar } 21917813dd6fSViresh Kumar 21927813dd6fSViresh Kumar /** 2193b0ec0942SViresh Kumar * _opp_put_regulators() - Releases resources blocked for regulator 2194b0ec0942SViresh Kumar * @opp_table: OPP table returned from _opp_set_regulators(). 21957813dd6fSViresh Kumar */ 2196b0ec0942SViresh Kumar static void _opp_put_regulators(struct opp_table *opp_table) 21977813dd6fSViresh Kumar { 21987813dd6fSViresh Kumar int i; 21997813dd6fSViresh Kumar 2200779b783cSViresh Kumar if (!opp_table->regulators) 2201b0ec0942SViresh Kumar return; 22027813dd6fSViresh Kumar 220372f80ce4SViresh Kumar if (opp_table->enabled) { 22048d45719cSKamil Konieczny for (i = opp_table->regulator_count - 1; i >= 0; i--) 22058d45719cSKamil Konieczny regulator_disable(opp_table->regulators[i]); 22068d45719cSKamil Konieczny } 22078d45719cSKamil Konieczny 220824957db1SMarek Szyprowski for (i = opp_table->regulator_count - 1; i >= 0; i--) 22097813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 22107813dd6fSViresh Kumar 22117813dd6fSViresh Kumar kfree(opp_table->regulators); 22127813dd6fSViresh Kumar opp_table->regulators = NULL; 221346f48acaSViresh Kumar opp_table->regulator_count = -1; 22147813dd6fSViresh Kumar } 221532aee78bSYangtao Li 22162083da24SViresh Kumar static void _put_clks(struct opp_table *opp_table, int count) 22172083da24SViresh Kumar { 22182083da24SViresh Kumar int i; 22192083da24SViresh Kumar 22202083da24SViresh Kumar for (i = count - 1; i >= 0; i--) 22212083da24SViresh Kumar clk_put(opp_table->clks[i]); 22222083da24SViresh Kumar 22232083da24SViresh Kumar kfree(opp_table->clks); 22242083da24SViresh Kumar opp_table->clks = NULL; 22252083da24SViresh Kumar } 22262083da24SViresh Kumar 22277813dd6fSViresh Kumar /** 22282368f576SViresh Kumar * _opp_set_clknames() - Set clk names for the device 22292368f576SViresh Kumar * @dev: Device for which clk names is being set. 22302368f576SViresh Kumar * @names: Clk names. 22317813dd6fSViresh Kumar * 22322368f576SViresh Kumar * In order to support OPP switching, OPP layer needs to get pointers to the 22332368f576SViresh Kumar * clocks for the device. Simple cases work fine without using this routine 22342368f576SViresh Kumar * (i.e. by passing connection-id as NULL), but for a device with multiple 22352368f576SViresh Kumar * clocks available, the OPP core needs to know the exact names of the clks to 22362368f576SViresh Kumar * use. 22377813dd6fSViresh Kumar * 22387813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 22397813dd6fSViresh Kumar */ 22402368f576SViresh Kumar static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev, 22412083da24SViresh Kumar const char * const names[], 22422083da24SViresh Kumar config_clks_t config_clks) 22437813dd6fSViresh Kumar { 22442368f576SViresh Kumar const char * const *temp = names; 22452083da24SViresh Kumar int count = 0, ret, i; 22462083da24SViresh Kumar struct clk *clk; 22477813dd6fSViresh Kumar 22482368f576SViresh Kumar /* Count number of clks */ 22492368f576SViresh Kumar while (*temp++) 22502368f576SViresh Kumar count++; 22517813dd6fSViresh Kumar 22522368f576SViresh Kumar /* 22532368f576SViresh Kumar * This is a special case where we have a single clock, whose connection 22542368f576SViresh Kumar * id name is NULL, i.e. first two entries are NULL in the array. 22552368f576SViresh Kumar */ 22562368f576SViresh Kumar if (!count && !names[1]) 22572368f576SViresh Kumar count = 1; 22582368f576SViresh Kumar 22592083da24SViresh Kumar /* Fail early for invalid configurations */ 22602f71ae1aSViresh Kumar if (!count || (!config_clks && count > 1)) 22612368f576SViresh Kumar return -EINVAL; 22627813dd6fSViresh Kumar 22630a43452bSViresh Kumar /* Another CPU that shares the OPP table has set the clkname ? */ 22642083da24SViresh Kumar if (opp_table->clks) 22652368f576SViresh Kumar return 0; 22660a43452bSViresh Kumar 22672083da24SViresh Kumar opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks), 22682083da24SViresh Kumar GFP_KERNEL); 22692083da24SViresh Kumar if (!opp_table->clks) 22702083da24SViresh Kumar return -ENOMEM; 22717813dd6fSViresh Kumar 22722083da24SViresh Kumar /* Find clks for the device */ 22732083da24SViresh Kumar for (i = 0; i < count; i++) { 22742083da24SViresh Kumar clk = clk_get(dev, names[i]); 22752083da24SViresh Kumar if (IS_ERR(clk)) { 22762083da24SViresh Kumar ret = dev_err_probe(dev, PTR_ERR(clk), 22772083da24SViresh Kumar "%s: Couldn't find clock with name: %s\n", 22782083da24SViresh Kumar __func__, names[i]); 22792083da24SViresh Kumar goto free_clks; 22807813dd6fSViresh Kumar } 22817813dd6fSViresh Kumar 22822083da24SViresh Kumar opp_table->clks[i] = clk; 22832083da24SViresh Kumar } 22842083da24SViresh Kumar 22852083da24SViresh Kumar opp_table->clk_count = count; 22862f71ae1aSViresh Kumar opp_table->config_clks = config_clks; 22872083da24SViresh Kumar 22882083da24SViresh Kumar /* Set generic single clk set here */ 22892083da24SViresh Kumar if (count == 1) { 22902f71ae1aSViresh Kumar if (!opp_table->config_clks) 22912083da24SViresh Kumar opp_table->config_clks = _opp_config_clk_single; 22922083da24SViresh Kumar 22932083da24SViresh Kumar /* 22942083da24SViresh Kumar * We could have just dropped the "clk" field and used "clks" 22952083da24SViresh Kumar * everywhere. Instead we kept the "clk" field around for 22962083da24SViresh Kumar * following reasons: 22972083da24SViresh Kumar * 22982083da24SViresh Kumar * - avoiding clks[0] everywhere else. 22992083da24SViresh Kumar * - not running single clk helpers for multiple clk usecase by 23002083da24SViresh Kumar * mistake. 23012083da24SViresh Kumar * 23022083da24SViresh Kumar * Since this is single-clk case, just update the clk pointer 23032083da24SViresh Kumar * too. 23042083da24SViresh Kumar */ 23052083da24SViresh Kumar opp_table->clk = opp_table->clks[0]; 23062083da24SViresh Kumar } 23070a43452bSViresh Kumar 23082368f576SViresh Kumar return 0; 23092083da24SViresh Kumar 23102083da24SViresh Kumar free_clks: 23112083da24SViresh Kumar _put_clks(opp_table, i); 23122083da24SViresh Kumar return ret; 23137813dd6fSViresh Kumar } 23147813dd6fSViresh Kumar 23157813dd6fSViresh Kumar /** 23162368f576SViresh Kumar * _opp_put_clknames() - Releases resources blocked for clks. 23172368f576SViresh Kumar * @opp_table: OPP table returned from _opp_set_clknames(). 23187813dd6fSViresh Kumar */ 23192368f576SViresh Kumar static void _opp_put_clknames(struct opp_table *opp_table) 23207813dd6fSViresh Kumar { 23212083da24SViresh Kumar if (!opp_table->clks) 23222083da24SViresh Kumar return; 23232083da24SViresh Kumar 23242083da24SViresh Kumar opp_table->config_clks = NULL; 23252083da24SViresh Kumar opp_table->clk = ERR_PTR(-ENODEV); 23262083da24SViresh Kumar 23272083da24SViresh Kumar _put_clks(opp_table, opp_table->clk_count); 2328a74f681cSYangtao Li } 2329a74f681cSYangtao Li 2330a74f681cSYangtao Li /** 2331aee3352fSViresh Kumar * _opp_set_config_regulators_helper() - Register custom set regulator helper. 2332aee3352fSViresh Kumar * @dev: Device for which the helper is getting registered. 2333aee3352fSViresh Kumar * @config_regulators: Custom set regulator helper. 2334aee3352fSViresh Kumar * 2335aee3352fSViresh Kumar * This is useful to support platforms with multiple regulators per device. 2336aee3352fSViresh Kumar * 2337aee3352fSViresh Kumar * This must be called before any OPPs are initialized for the device. 2338aee3352fSViresh Kumar */ 2339aee3352fSViresh Kumar static int _opp_set_config_regulators_helper(struct opp_table *opp_table, 2340aee3352fSViresh Kumar struct device *dev, config_regulators_t config_regulators) 2341aee3352fSViresh Kumar { 2342aee3352fSViresh Kumar /* Another CPU that shares the OPP table has set the helper ? */ 2343aee3352fSViresh Kumar if (!opp_table->config_regulators) 2344aee3352fSViresh Kumar opp_table->config_regulators = config_regulators; 2345aee3352fSViresh Kumar 2346aee3352fSViresh Kumar return 0; 2347aee3352fSViresh Kumar } 2348aee3352fSViresh Kumar 2349aee3352fSViresh Kumar /** 2350aee3352fSViresh Kumar * _opp_put_config_regulators_helper() - Releases resources blocked for 2351aee3352fSViresh Kumar * config_regulators helper. 2352aee3352fSViresh Kumar * @opp_table: OPP table returned from _opp_set_config_regulators_helper(). 2353aee3352fSViresh Kumar * 2354aee3352fSViresh Kumar * Release resources blocked for platform specific config_regulators helper. 2355aee3352fSViresh Kumar */ 2356aee3352fSViresh Kumar static void _opp_put_config_regulators_helper(struct opp_table *opp_table) 2357aee3352fSViresh Kumar { 2358aee3352fSViresh Kumar if (opp_table->config_regulators) 2359aee3352fSViresh Kumar opp_table->config_regulators = NULL; 2360aee3352fSViresh Kumar } 2361aee3352fSViresh Kumar 2362442e7a17SViresh Kumar static void _detach_genpd(struct opp_table *opp_table) 23636319aee1SViresh Kumar { 23646319aee1SViresh Kumar int index; 23656319aee1SViresh Kumar 2366cb60e960SViresh Kumar if (!opp_table->genpd_virt_devs) 2367cb60e960SViresh Kumar return; 2368cb60e960SViresh Kumar 23696319aee1SViresh Kumar for (index = 0; index < opp_table->required_opp_count; index++) { 23706319aee1SViresh Kumar if (!opp_table->genpd_virt_devs[index]) 23716319aee1SViresh Kumar continue; 23726319aee1SViresh Kumar 23736319aee1SViresh Kumar dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 23746319aee1SViresh Kumar opp_table->genpd_virt_devs[index] = NULL; 23756319aee1SViresh Kumar } 2376c0ab9e08SViresh Kumar 2377c0ab9e08SViresh Kumar kfree(opp_table->genpd_virt_devs); 2378c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = NULL; 23796319aee1SViresh Kumar } 23806319aee1SViresh Kumar 23817813dd6fSViresh Kumar /** 2382442e7a17SViresh Kumar * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 23836319aee1SViresh Kumar * @dev: Consumer device for which the genpd is getting attached. 23846319aee1SViresh Kumar * @names: Null terminated array of pointers containing names of genpd to attach. 238517a8f868SViresh Kumar * @virt_devs: Pointer to return the array of virtual devices. 23864f018bc0SViresh Kumar * 23874f018bc0SViresh Kumar * Multiple generic power domains for a device are supported with the help of 23884f018bc0SViresh Kumar * virtual genpd devices, which are created for each consumer device - genpd 23894f018bc0SViresh Kumar * pair. These are the device structures which are attached to the power domain 23904f018bc0SViresh Kumar * and are required by the OPP core to set the performance state of the genpd. 23916319aee1SViresh Kumar * The same API also works for the case where single genpd is available and so 23926319aee1SViresh Kumar * we don't need to support that separately. 23934f018bc0SViresh Kumar * 23944f018bc0SViresh Kumar * This helper will normally be called by the consumer driver of the device 23956319aee1SViresh Kumar * "dev", as only that has details of the genpd names. 23964f018bc0SViresh Kumar * 23976319aee1SViresh Kumar * This helper needs to be called once with a list of all genpd to attach. 23986319aee1SViresh Kumar * Otherwise the original device structure will be used instead by the OPP core. 2399baea35e4SViresh Kumar * 2400baea35e4SViresh Kumar * The order of entries in the names array must match the order in which 2401baea35e4SViresh Kumar * "required-opps" are added in DT. 24024f018bc0SViresh Kumar */ 2403442e7a17SViresh Kumar static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev, 24043734b9f2SDmitry Osipenko const char * const *names, struct device ***virt_devs) 24054f018bc0SViresh Kumar { 24066319aee1SViresh Kumar struct device *virt_dev; 2407baea35e4SViresh Kumar int index = 0, ret = -EINVAL; 24083734b9f2SDmitry Osipenko const char * const *name = names; 24094f018bc0SViresh Kumar 2410cb60e960SViresh Kumar if (opp_table->genpd_virt_devs) 2411442e7a17SViresh Kumar return 0; 24124f018bc0SViresh Kumar 24136319aee1SViresh Kumar /* 24146319aee1SViresh Kumar * If the genpd's OPP table isn't already initialized, parsing of the 24156319aee1SViresh Kumar * required-opps fail for dev. We should retry this after genpd's OPP 24166319aee1SViresh Kumar * table is added. 24176319aee1SViresh Kumar */ 2418442e7a17SViresh Kumar if (!opp_table->required_opp_count) 2419442e7a17SViresh Kumar return -EPROBE_DEFER; 24206319aee1SViresh Kumar 24214f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 24224f018bc0SViresh Kumar 2423c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, 2424c0ab9e08SViresh Kumar sizeof(*opp_table->genpd_virt_devs), 2425c0ab9e08SViresh Kumar GFP_KERNEL); 2426c0ab9e08SViresh Kumar if (!opp_table->genpd_virt_devs) 2427c0ab9e08SViresh Kumar goto unlock; 24284f018bc0SViresh Kumar 24296319aee1SViresh Kumar while (*name) { 24306319aee1SViresh Kumar if (index >= opp_table->required_opp_count) { 24316319aee1SViresh Kumar dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 24326319aee1SViresh Kumar *name, opp_table->required_opp_count, index); 24336319aee1SViresh Kumar goto err; 24346319aee1SViresh Kumar } 24354f018bc0SViresh Kumar 24366319aee1SViresh Kumar virt_dev = dev_pm_domain_attach_by_name(dev, *name); 24374ea9496cSTang Bin if (IS_ERR_OR_NULL(virt_dev)) { 24384ea9496cSTang Bin ret = PTR_ERR(virt_dev) ? : -ENODEV; 24396319aee1SViresh Kumar dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 24406319aee1SViresh Kumar goto err; 24414f018bc0SViresh Kumar } 24424f018bc0SViresh Kumar 24434f018bc0SViresh Kumar opp_table->genpd_virt_devs[index] = virt_dev; 2444baea35e4SViresh Kumar index++; 24456319aee1SViresh Kumar name++; 24466319aee1SViresh Kumar } 24476319aee1SViresh Kumar 244817a8f868SViresh Kumar if (virt_devs) 244917a8f868SViresh Kumar *virt_devs = opp_table->genpd_virt_devs; 24504f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 24514f018bc0SViresh Kumar 2452442e7a17SViresh Kumar return 0; 24536319aee1SViresh Kumar 24546319aee1SViresh Kumar err: 2455442e7a17SViresh Kumar _detach_genpd(opp_table); 2456c0ab9e08SViresh Kumar unlock: 24576319aee1SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 2458442e7a17SViresh Kumar return ret; 24596319aee1SViresh Kumar 24604f018bc0SViresh Kumar } 24614f018bc0SViresh Kumar 24624f018bc0SViresh Kumar /** 2463442e7a17SViresh Kumar * _opp_detach_genpd() - Detach genpd(s) from the device. 2464442e7a17SViresh Kumar * @opp_table: OPP table returned by _opp_attach_genpd(). 24654f018bc0SViresh Kumar * 24666319aee1SViresh Kumar * This detaches the genpd(s), resets the virtual device pointers, and puts the 24676319aee1SViresh Kumar * OPP table. 24684f018bc0SViresh Kumar */ 2469442e7a17SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table) 24704f018bc0SViresh Kumar { 24714f018bc0SViresh Kumar /* 24724f018bc0SViresh Kumar * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 24734f018bc0SViresh Kumar * used in parallel. 24744f018bc0SViresh Kumar */ 24754f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 2476442e7a17SViresh Kumar _detach_genpd(opp_table); 24774f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 24784f018bc0SViresh Kumar } 2479b4b9e223SDmitry Osipenko 248011b9b663SViresh Kumar static void _opp_clear_config(struct opp_config_data *data) 248111b9b663SViresh Kumar { 248211b9b663SViresh Kumar if (data->flags & OPP_CONFIG_GENPD) 2483442e7a17SViresh Kumar _opp_detach_genpd(data->opp_table); 248411b9b663SViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR) 2485b0ec0942SViresh Kumar _opp_put_regulators(data->opp_table); 248611b9b663SViresh Kumar if (data->flags & OPP_CONFIG_SUPPORTED_HW) 248789f03984SViresh Kumar _opp_put_supported_hw(data->opp_table); 24881f378c6eSViresh Kumar if (data->flags & OPP_CONFIG_REGULATOR_HELPER) 2489aee3352fSViresh Kumar _opp_put_config_regulators_helper(data->opp_table); 249011b9b663SViresh Kumar if (data->flags & OPP_CONFIG_PROP_NAME) 2491298098e5SViresh Kumar _opp_put_prop_name(data->opp_table); 249211b9b663SViresh Kumar if (data->flags & OPP_CONFIG_CLK) 24932368f576SViresh Kumar _opp_put_clknames(data->opp_table); 249411b9b663SViresh Kumar 249511b9b663SViresh Kumar dev_pm_opp_put_opp_table(data->opp_table); 249611b9b663SViresh Kumar kfree(data); 249711b9b663SViresh Kumar } 249811b9b663SViresh Kumar 249911b9b663SViresh Kumar /** 250011b9b663SViresh Kumar * dev_pm_opp_set_config() - Set OPP configuration for the device. 250111b9b663SViresh Kumar * @dev: Device for which configuration is being set. 250211b9b663SViresh Kumar * @config: OPP configuration. 250311b9b663SViresh Kumar * 250411b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 250511b9b663SViresh Kumar * 250611b9b663SViresh Kumar * This must be called before any OPPs are initialized for the device. This may 250711b9b663SViresh Kumar * be called multiple times for the same OPP table, for example once for each 250811b9b663SViresh Kumar * CPU that share the same table. This must be balanced by the same number of 250911b9b663SViresh Kumar * calls to dev_pm_opp_clear_config() in order to free the OPP table properly. 251011b9b663SViresh Kumar * 251111b9b663SViresh Kumar * This returns a token to the caller, which must be passed to 251211b9b663SViresh Kumar * dev_pm_opp_clear_config() to free the resources later. The value of the 251311b9b663SViresh Kumar * returned token will be >= 1 for success and negative for errors. The minimum 251411b9b663SViresh Kumar * value of 1 is chosen here to make it easy for callers to manage the resource. 251511b9b663SViresh Kumar */ 251611b9b663SViresh Kumar int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 251711b9b663SViresh Kumar { 2518298098e5SViresh Kumar struct opp_table *opp_table; 251911b9b663SViresh Kumar struct opp_config_data *data; 252011b9b663SViresh Kumar unsigned int id; 252111b9b663SViresh Kumar int ret; 252211b9b663SViresh Kumar 252311b9b663SViresh Kumar data = kmalloc(sizeof(*data), GFP_KERNEL); 252411b9b663SViresh Kumar if (!data) 252511b9b663SViresh Kumar return -ENOMEM; 252611b9b663SViresh Kumar 252711b9b663SViresh Kumar opp_table = _add_opp_table(dev, false); 252811b9b663SViresh Kumar if (IS_ERR(opp_table)) { 252911b9b663SViresh Kumar kfree(data); 253011b9b663SViresh Kumar return PTR_ERR(opp_table); 253111b9b663SViresh Kumar } 253211b9b663SViresh Kumar 253311b9b663SViresh Kumar data->opp_table = opp_table; 253411b9b663SViresh Kumar data->flags = 0; 253511b9b663SViresh Kumar 253611b9b663SViresh Kumar /* This should be called before OPPs are initialized */ 253711b9b663SViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 253811b9b663SViresh Kumar ret = -EBUSY; 253911b9b663SViresh Kumar goto err; 254011b9b663SViresh Kumar } 254111b9b663SViresh Kumar 254211b9b663SViresh Kumar /* Configure clocks */ 254311b9b663SViresh Kumar if (config->clk_names) { 25442083da24SViresh Kumar ret = _opp_set_clknames(opp_table, dev, config->clk_names, 25452083da24SViresh Kumar config->config_clks); 25462368f576SViresh Kumar if (ret) 254711b9b663SViresh Kumar goto err; 254811b9b663SViresh Kumar 254911b9b663SViresh Kumar data->flags |= OPP_CONFIG_CLK; 25502083da24SViresh Kumar } else if (config->config_clks) { 25512083da24SViresh Kumar /* Don't allow config callback without clocks */ 25522083da24SViresh Kumar ret = -EINVAL; 25532083da24SViresh Kumar goto err; 255411b9b663SViresh Kumar } 255511b9b663SViresh Kumar 255611b9b663SViresh Kumar /* Configure property names */ 255711b9b663SViresh Kumar if (config->prop_name) { 2558298098e5SViresh Kumar ret = _opp_set_prop_name(opp_table, config->prop_name); 2559298098e5SViresh Kumar if (ret) 256011b9b663SViresh Kumar goto err; 256111b9b663SViresh Kumar 256211b9b663SViresh Kumar data->flags |= OPP_CONFIG_PROP_NAME; 256311b9b663SViresh Kumar } 256411b9b663SViresh Kumar 2565aee3352fSViresh Kumar /* Configure config_regulators helper */ 2566aee3352fSViresh Kumar if (config->config_regulators) { 2567aee3352fSViresh Kumar ret = _opp_set_config_regulators_helper(opp_table, dev, 2568aee3352fSViresh Kumar config->config_regulators); 2569aee3352fSViresh Kumar if (ret) 2570aee3352fSViresh Kumar goto err; 2571aee3352fSViresh Kumar 2572aee3352fSViresh Kumar data->flags |= OPP_CONFIG_REGULATOR_HELPER; 2573aee3352fSViresh Kumar } 2574aee3352fSViresh Kumar 257511b9b663SViresh Kumar /* Configure supported hardware */ 257611b9b663SViresh Kumar if (config->supported_hw) { 257789f03984SViresh Kumar ret = _opp_set_supported_hw(opp_table, config->supported_hw, 257811b9b663SViresh Kumar config->supported_hw_count); 257989f03984SViresh Kumar if (ret) 258011b9b663SViresh Kumar goto err; 258111b9b663SViresh Kumar 258211b9b663SViresh Kumar data->flags |= OPP_CONFIG_SUPPORTED_HW; 258311b9b663SViresh Kumar } 258411b9b663SViresh Kumar 258511b9b663SViresh Kumar /* Configure supplies */ 258611b9b663SViresh Kumar if (config->regulator_names) { 2587b0ec0942SViresh Kumar ret = _opp_set_regulators(opp_table, dev, 2588b0ec0942SViresh Kumar config->regulator_names); 2589b0ec0942SViresh Kumar if (ret) 259011b9b663SViresh Kumar goto err; 259111b9b663SViresh Kumar 259211b9b663SViresh Kumar data->flags |= OPP_CONFIG_REGULATOR; 259311b9b663SViresh Kumar } 259411b9b663SViresh Kumar 259511b9b663SViresh Kumar /* Attach genpds */ 259611b9b663SViresh Kumar if (config->genpd_names) { 2597442e7a17SViresh Kumar ret = _opp_attach_genpd(opp_table, dev, config->genpd_names, 259811b9b663SViresh Kumar config->virt_devs); 2599442e7a17SViresh Kumar if (ret) 260011b9b663SViresh Kumar goto err; 260111b9b663SViresh Kumar 260211b9b663SViresh Kumar data->flags |= OPP_CONFIG_GENPD; 260311b9b663SViresh Kumar } 260411b9b663SViresh Kumar 260511b9b663SViresh Kumar ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX), 260611b9b663SViresh Kumar GFP_KERNEL); 260711b9b663SViresh Kumar if (ret) 260811b9b663SViresh Kumar goto err; 260911b9b663SViresh Kumar 261011b9b663SViresh Kumar return id; 261111b9b663SViresh Kumar 261211b9b663SViresh Kumar err: 261311b9b663SViresh Kumar _opp_clear_config(data); 261411b9b663SViresh Kumar return ret; 261511b9b663SViresh Kumar } 261611b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_config); 261711b9b663SViresh Kumar 261811b9b663SViresh Kumar /** 261911b9b663SViresh Kumar * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration. 262011b9b663SViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_config(). 262111b9b663SViresh Kumar * 262211b9b663SViresh Kumar * This allows all device OPP configurations to be cleared at once. This must be 262311b9b663SViresh Kumar * called once for each call made to dev_pm_opp_set_config(), in order to free 262411b9b663SViresh Kumar * the OPPs properly. 262511b9b663SViresh Kumar * 262611b9b663SViresh Kumar * Currently the first call itself ends up freeing all the OPP configurations, 262711b9b663SViresh Kumar * while the later ones only drop the OPP table reference. This works well for 262811b9b663SViresh Kumar * now as we would never want to use an half initialized OPP table and want to 262911b9b663SViresh Kumar * remove the configurations together. 263011b9b663SViresh Kumar */ 263111b9b663SViresh Kumar void dev_pm_opp_clear_config(int token) 263211b9b663SViresh Kumar { 263311b9b663SViresh Kumar struct opp_config_data *data; 263411b9b663SViresh Kumar 263511b9b663SViresh Kumar /* 263611b9b663SViresh Kumar * This lets the callers call this unconditionally and keep their code 263711b9b663SViresh Kumar * simple. 263811b9b663SViresh Kumar */ 263911b9b663SViresh Kumar if (unlikely(token <= 0)) 264011b9b663SViresh Kumar return; 264111b9b663SViresh Kumar 264211b9b663SViresh Kumar data = xa_erase(&opp_configs, token); 264311b9b663SViresh Kumar if (WARN_ON(!data)) 264411b9b663SViresh Kumar return; 264511b9b663SViresh Kumar 264611b9b663SViresh Kumar _opp_clear_config(data); 264711b9b663SViresh Kumar } 264811b9b663SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config); 264911b9b663SViresh Kumar 265011b9b663SViresh Kumar static void devm_pm_opp_config_release(void *token) 265111b9b663SViresh Kumar { 265211b9b663SViresh Kumar dev_pm_opp_clear_config((unsigned long)token); 265311b9b663SViresh Kumar } 265411b9b663SViresh Kumar 265511b9b663SViresh Kumar /** 265611b9b663SViresh Kumar * devm_pm_opp_set_config() - Set OPP configuration for the device. 265711b9b663SViresh Kumar * @dev: Device for which configuration is being set. 265811b9b663SViresh Kumar * @config: OPP configuration. 265911b9b663SViresh Kumar * 266011b9b663SViresh Kumar * This allows all device OPP configurations to be performed at once. 266111b9b663SViresh Kumar * This is a resource-managed variant of dev_pm_opp_set_config(). 266211b9b663SViresh Kumar * 266311b9b663SViresh Kumar * Return: 0 on success and errorno otherwise. 266411b9b663SViresh Kumar */ 266511b9b663SViresh Kumar int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config) 266611b9b663SViresh Kumar { 266711b9b663SViresh Kumar int token = dev_pm_opp_set_config(dev, config); 266811b9b663SViresh Kumar 266911b9b663SViresh Kumar if (token < 0) 267011b9b663SViresh Kumar return token; 267111b9b663SViresh Kumar 267211b9b663SViresh Kumar return devm_add_action_or_reset(dev, devm_pm_opp_config_release, 267311b9b663SViresh Kumar (void *) ((unsigned long) token)); 267411b9b663SViresh Kumar } 267511b9b663SViresh Kumar EXPORT_SYMBOL_GPL(devm_pm_opp_set_config); 267611b9b663SViresh Kumar 26774f018bc0SViresh Kumar /** 26787d8658efSSaravana Kannan * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP. 26797d8658efSSaravana Kannan * @src_table: OPP table which has @dst_table as one of its required OPP table. 26807d8658efSSaravana Kannan * @dst_table: Required OPP table of the @src_table. 26817d8658efSSaravana Kannan * @src_opp: OPP from the @src_table. 26827d8658efSSaravana Kannan * 26837d8658efSSaravana Kannan * This function returns the OPP (present in @dst_table) pointed out by the 26847d8658efSSaravana Kannan * "required-opps" property of the @src_opp (present in @src_table). 26857d8658efSSaravana Kannan * 26867d8658efSSaravana Kannan * The callers are required to call dev_pm_opp_put() for the returned OPP after 26877d8658efSSaravana Kannan * use. 26887d8658efSSaravana Kannan * 26897d8658efSSaravana Kannan * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise. 26907d8658efSSaravana Kannan */ 26917d8658efSSaravana Kannan struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table, 26927d8658efSSaravana Kannan struct opp_table *dst_table, 26937d8658efSSaravana Kannan struct dev_pm_opp *src_opp) 26947d8658efSSaravana Kannan { 26957d8658efSSaravana Kannan struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV); 26967d8658efSSaravana Kannan int i; 26977d8658efSSaravana Kannan 26987d8658efSSaravana Kannan if (!src_table || !dst_table || !src_opp || 26997d8658efSSaravana Kannan !src_table->required_opp_tables) 27007d8658efSSaravana Kannan return ERR_PTR(-EINVAL); 27017d8658efSSaravana Kannan 27027d8658efSSaravana Kannan /* required-opps not fully initialized yet */ 27037d8658efSSaravana Kannan if (lazy_linking_pending(src_table)) 27047d8658efSSaravana Kannan return ERR_PTR(-EBUSY); 27057d8658efSSaravana Kannan 27067d8658efSSaravana Kannan for (i = 0; i < src_table->required_opp_count; i++) { 27077d8658efSSaravana Kannan if (src_table->required_opp_tables[i] == dst_table) { 27087d8658efSSaravana Kannan mutex_lock(&src_table->lock); 27097d8658efSSaravana Kannan 27107d8658efSSaravana Kannan list_for_each_entry(opp, &src_table->opp_list, node) { 27117d8658efSSaravana Kannan if (opp == src_opp) { 27127d8658efSSaravana Kannan dest_opp = opp->required_opps[i]; 27137d8658efSSaravana Kannan dev_pm_opp_get(dest_opp); 27147d8658efSSaravana Kannan break; 27157d8658efSSaravana Kannan } 27167d8658efSSaravana Kannan } 27177d8658efSSaravana Kannan 27187d8658efSSaravana Kannan mutex_unlock(&src_table->lock); 27197d8658efSSaravana Kannan break; 27207d8658efSSaravana Kannan } 27217d8658efSSaravana Kannan } 27227d8658efSSaravana Kannan 27237d8658efSSaravana Kannan if (IS_ERR(dest_opp)) { 27247d8658efSSaravana Kannan pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, 27257d8658efSSaravana Kannan src_table, dst_table); 27267d8658efSSaravana Kannan } 27277d8658efSSaravana Kannan 27287d8658efSSaravana Kannan return dest_opp; 27297d8658efSSaravana Kannan } 27307d8658efSSaravana Kannan EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp); 27317d8658efSSaravana Kannan 27327d8658efSSaravana Kannan /** 2733c8a59103SViresh Kumar * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 2734c8a59103SViresh Kumar * @src_table: OPP table which has dst_table as one of its required OPP table. 2735c8a59103SViresh Kumar * @dst_table: Required OPP table of the src_table. 2736c8a59103SViresh Kumar * @pstate: Current performance state of the src_table. 2737c8a59103SViresh Kumar * 2738c8a59103SViresh Kumar * This Returns pstate of the OPP (present in @dst_table) pointed out by the 2739c8a59103SViresh Kumar * "required-opps" property of the OPP (present in @src_table) which has 2740c8a59103SViresh Kumar * performance state set to @pstate. 2741c8a59103SViresh Kumar * 2742c8a59103SViresh Kumar * Return: Zero or positive performance state on success, otherwise negative 2743c8a59103SViresh Kumar * value on errors. 2744c8a59103SViresh Kumar */ 2745c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 2746c8a59103SViresh Kumar struct opp_table *dst_table, 2747c8a59103SViresh Kumar unsigned int pstate) 2748c8a59103SViresh Kumar { 2749c8a59103SViresh Kumar struct dev_pm_opp *opp; 2750c8a59103SViresh Kumar int dest_pstate = -EINVAL; 2751c8a59103SViresh Kumar int i; 2752c8a59103SViresh Kumar 2753c8a59103SViresh Kumar /* 2754c8a59103SViresh Kumar * Normally the src_table will have the "required_opps" property set to 2755c8a59103SViresh Kumar * point to one of the OPPs in the dst_table, but in some cases the 2756c8a59103SViresh Kumar * genpd and its master have one to one mapping of performance states 2757c8a59103SViresh Kumar * and so none of them have the "required-opps" property set. Return the 2758c8a59103SViresh Kumar * pstate of the src_table as it is in such cases. 2759c8a59103SViresh Kumar */ 2760f2f4d2b8SDmitry Osipenko if (!src_table || !src_table->required_opp_count) 2761c8a59103SViresh Kumar return pstate; 2762c8a59103SViresh Kumar 276384cb7ff3SViresh Kumar /* Both OPP tables must belong to genpds */ 276484cb7ff3SViresh Kumar if (unlikely(!src_table->is_genpd || !dst_table->is_genpd)) { 276584cb7ff3SViresh Kumar pr_err("%s: Performance state is only valid for genpds.\n", __func__); 276684cb7ff3SViresh Kumar return -EINVAL; 276784cb7ff3SViresh Kumar } 276884cb7ff3SViresh Kumar 27697eba0c76SViresh Kumar /* required-opps not fully initialized yet */ 27707eba0c76SViresh Kumar if (lazy_linking_pending(src_table)) 27717eba0c76SViresh Kumar return -EBUSY; 27727eba0c76SViresh Kumar 2773c8a59103SViresh Kumar for (i = 0; i < src_table->required_opp_count; i++) { 2774c8a59103SViresh Kumar if (src_table->required_opp_tables[i]->np == dst_table->np) 2775c8a59103SViresh Kumar break; 2776c8a59103SViresh Kumar } 2777c8a59103SViresh Kumar 2778c8a59103SViresh Kumar if (unlikely(i == src_table->required_opp_count)) { 2779c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 2780c8a59103SViresh Kumar __func__, src_table, dst_table); 2781c8a59103SViresh Kumar return -EINVAL; 2782c8a59103SViresh Kumar } 2783c8a59103SViresh Kumar 2784c8a59103SViresh Kumar mutex_lock(&src_table->lock); 2785c8a59103SViresh Kumar 2786c8a59103SViresh Kumar list_for_each_entry(opp, &src_table->opp_list, node) { 27877c41cdcdSViresh Kumar if (opp->level == pstate) { 27887c41cdcdSViresh Kumar dest_pstate = opp->required_opps[i]->level; 2789c8a59103SViresh Kumar goto unlock; 2790c8a59103SViresh Kumar } 2791c8a59103SViresh Kumar } 2792c8a59103SViresh Kumar 2793c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 2794c8a59103SViresh Kumar dst_table); 2795c8a59103SViresh Kumar 2796c8a59103SViresh Kumar unlock: 2797c8a59103SViresh Kumar mutex_unlock(&src_table->lock); 2798c8a59103SViresh Kumar 2799c8a59103SViresh Kumar return dest_pstate; 2800c8a59103SViresh Kumar } 2801c8a59103SViresh Kumar 2802c8a59103SViresh Kumar /** 28037813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 28047813dd6fSViresh Kumar * @dev: device for which we do this operation 28057813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 28067813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 28077813dd6fSViresh Kumar * 28087813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 28097813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 28107813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 28117813dd6fSViresh Kumar * 28127813dd6fSViresh Kumar * Return: 28137813dd6fSViresh Kumar * 0 On success OR 28147813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 28157813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 28167813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 28177813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 28187813dd6fSViresh Kumar */ 28197813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 28207813dd6fSViresh Kumar { 28217813dd6fSViresh Kumar struct opp_table *opp_table; 28227813dd6fSViresh Kumar int ret; 28237813dd6fSViresh Kumar 282432439ac7SViresh Kumar opp_table = _add_opp_table(dev, true); 2825dd461cd9SStephan Gerhold if (IS_ERR(opp_table)) 2826dd461cd9SStephan Gerhold return PTR_ERR(opp_table); 28277813dd6fSViresh Kumar 282846f48acaSViresh Kumar /* Fix regulator count for dynamic OPPs */ 282946f48acaSViresh Kumar opp_table->regulator_count = 1; 283046f48acaSViresh Kumar 28317813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 28320ad8c623SViresh Kumar if (ret) 28337813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 28340ad8c623SViresh Kumar 28357813dd6fSViresh Kumar return ret; 28367813dd6fSViresh Kumar } 28377813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 28387813dd6fSViresh Kumar 28397813dd6fSViresh Kumar /** 28407813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 28417813dd6fSViresh Kumar * @dev: device for which we do this operation 28427813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 28437813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 28447813dd6fSViresh Kumar * 28457813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 28467813dd6fSViresh Kumar * which is isolated here. 28477813dd6fSViresh Kumar * 28487813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 28497813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 28507813dd6fSViresh Kumar * successful. 28517813dd6fSViresh Kumar */ 28527813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 28537813dd6fSViresh Kumar bool availability_req) 28547813dd6fSViresh Kumar { 28557813dd6fSViresh Kumar struct opp_table *opp_table; 28567813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 28577813dd6fSViresh Kumar int r = 0; 28587813dd6fSViresh Kumar 28597813dd6fSViresh Kumar /* Find the opp_table */ 28607813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 28617813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 28627813dd6fSViresh Kumar r = PTR_ERR(opp_table); 28637813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 28647813dd6fSViresh Kumar return r; 28657813dd6fSViresh Kumar } 28667813dd6fSViresh Kumar 2867f123ea74SViresh Kumar if (!assert_single_clk(opp_table)) { 2868f123ea74SViresh Kumar r = -EINVAL; 2869f123ea74SViresh Kumar goto put_table; 2870f123ea74SViresh Kumar } 2871f123ea74SViresh Kumar 28727813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 28737813dd6fSViresh Kumar 28747813dd6fSViresh Kumar /* Do we have the frequency? */ 28757813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 28762083da24SViresh Kumar if (tmp_opp->rates[0] == freq) { 28777813dd6fSViresh Kumar opp = tmp_opp; 28787813dd6fSViresh Kumar break; 28797813dd6fSViresh Kumar } 28807813dd6fSViresh Kumar } 28817813dd6fSViresh Kumar 28827813dd6fSViresh Kumar if (IS_ERR(opp)) { 28837813dd6fSViresh Kumar r = PTR_ERR(opp); 28847813dd6fSViresh Kumar goto unlock; 28857813dd6fSViresh Kumar } 28867813dd6fSViresh Kumar 28877813dd6fSViresh Kumar /* Is update really needed? */ 28887813dd6fSViresh Kumar if (opp->available == availability_req) 28897813dd6fSViresh Kumar goto unlock; 28907813dd6fSViresh Kumar 28917813dd6fSViresh Kumar opp->available = availability_req; 28927813dd6fSViresh Kumar 28937813dd6fSViresh Kumar dev_pm_opp_get(opp); 28947813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 28957813dd6fSViresh Kumar 28967813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 28977813dd6fSViresh Kumar if (availability_req) 28987813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 28997813dd6fSViresh Kumar opp); 29007813dd6fSViresh Kumar else 29017813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 29027813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 29037813dd6fSViresh Kumar 29047813dd6fSViresh Kumar dev_pm_opp_put(opp); 29057813dd6fSViresh Kumar goto put_table; 29067813dd6fSViresh Kumar 29077813dd6fSViresh Kumar unlock: 29087813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 29097813dd6fSViresh Kumar put_table: 29107813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 29117813dd6fSViresh Kumar return r; 29127813dd6fSViresh Kumar } 29137813dd6fSViresh Kumar 29147813dd6fSViresh Kumar /** 291525cb20a2SStephen Boyd * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP 291625cb20a2SStephen Boyd * @dev: device for which we do this operation 291725cb20a2SStephen Boyd * @freq: OPP frequency to adjust voltage of 291825cb20a2SStephen Boyd * @u_volt: new OPP target voltage 291925cb20a2SStephen Boyd * @u_volt_min: new OPP min voltage 292025cb20a2SStephen Boyd * @u_volt_max: new OPP max voltage 292125cb20a2SStephen Boyd * 292225cb20a2SStephen Boyd * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 292325cb20a2SStephen Boyd * copy operation, returns 0 if no modifcation was done OR modification was 292425cb20a2SStephen Boyd * successful. 292525cb20a2SStephen Boyd */ 292625cb20a2SStephen Boyd int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq, 292725cb20a2SStephen Boyd unsigned long u_volt, unsigned long u_volt_min, 292825cb20a2SStephen Boyd unsigned long u_volt_max) 292925cb20a2SStephen Boyd 293025cb20a2SStephen Boyd { 293125cb20a2SStephen Boyd struct opp_table *opp_table; 293225cb20a2SStephen Boyd struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 293325cb20a2SStephen Boyd int r = 0; 293425cb20a2SStephen Boyd 293525cb20a2SStephen Boyd /* Find the opp_table */ 293625cb20a2SStephen Boyd opp_table = _find_opp_table(dev); 293725cb20a2SStephen Boyd if (IS_ERR(opp_table)) { 293825cb20a2SStephen Boyd r = PTR_ERR(opp_table); 293925cb20a2SStephen Boyd dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 294025cb20a2SStephen Boyd return r; 294125cb20a2SStephen Boyd } 294225cb20a2SStephen Boyd 2943f123ea74SViresh Kumar if (!assert_single_clk(opp_table)) { 2944f123ea74SViresh Kumar r = -EINVAL; 2945f123ea74SViresh Kumar goto put_table; 2946f123ea74SViresh Kumar } 2947f123ea74SViresh Kumar 294825cb20a2SStephen Boyd mutex_lock(&opp_table->lock); 294925cb20a2SStephen Boyd 295025cb20a2SStephen Boyd /* Do we have the frequency? */ 295125cb20a2SStephen Boyd list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 29522083da24SViresh Kumar if (tmp_opp->rates[0] == freq) { 295325cb20a2SStephen Boyd opp = tmp_opp; 295425cb20a2SStephen Boyd break; 295525cb20a2SStephen Boyd } 295625cb20a2SStephen Boyd } 295725cb20a2SStephen Boyd 295825cb20a2SStephen Boyd if (IS_ERR(opp)) { 295925cb20a2SStephen Boyd r = PTR_ERR(opp); 296025cb20a2SStephen Boyd goto adjust_unlock; 296125cb20a2SStephen Boyd } 296225cb20a2SStephen Boyd 296325cb20a2SStephen Boyd /* Is update really needed? */ 296425cb20a2SStephen Boyd if (opp->supplies->u_volt == u_volt) 296525cb20a2SStephen Boyd goto adjust_unlock; 296625cb20a2SStephen Boyd 296725cb20a2SStephen Boyd opp->supplies->u_volt = u_volt; 296825cb20a2SStephen Boyd opp->supplies->u_volt_min = u_volt_min; 296925cb20a2SStephen Boyd opp->supplies->u_volt_max = u_volt_max; 297025cb20a2SStephen Boyd 297125cb20a2SStephen Boyd dev_pm_opp_get(opp); 297225cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 297325cb20a2SStephen Boyd 297425cb20a2SStephen Boyd /* Notify the voltage change of the OPP */ 297525cb20a2SStephen Boyd blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE, 297625cb20a2SStephen Boyd opp); 297725cb20a2SStephen Boyd 297825cb20a2SStephen Boyd dev_pm_opp_put(opp); 2979f123ea74SViresh Kumar goto put_table; 298025cb20a2SStephen Boyd 298125cb20a2SStephen Boyd adjust_unlock: 298225cb20a2SStephen Boyd mutex_unlock(&opp_table->lock); 2983f123ea74SViresh Kumar put_table: 298425cb20a2SStephen Boyd dev_pm_opp_put_opp_table(opp_table); 298525cb20a2SStephen Boyd return r; 298625cb20a2SStephen Boyd } 298703649154SValdis Klētnieks EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage); 298825cb20a2SStephen Boyd 298925cb20a2SStephen Boyd /** 29907813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 29917813dd6fSViresh Kumar * @dev: device for which we do this operation 29927813dd6fSViresh Kumar * @freq: OPP frequency to enable 29937813dd6fSViresh Kumar * 29947813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 29957813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 29967813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 29977813dd6fSViresh Kumar * 29987813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 29997813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 30007813dd6fSViresh Kumar * successful. 30017813dd6fSViresh Kumar */ 30027813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 30037813dd6fSViresh Kumar { 30047813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 30057813dd6fSViresh Kumar } 30067813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 30077813dd6fSViresh Kumar 30087813dd6fSViresh Kumar /** 30097813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 30107813dd6fSViresh Kumar * @dev: device for which we do this operation 30117813dd6fSViresh Kumar * @freq: OPP frequency to disable 30127813dd6fSViresh Kumar * 30137813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 30147813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 30157813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 30167813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 30177813dd6fSViresh Kumar * 30187813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 30197813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 30207813dd6fSViresh Kumar * successful. 30217813dd6fSViresh Kumar */ 30227813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 30237813dd6fSViresh Kumar { 30247813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 30257813dd6fSViresh Kumar } 30267813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 30277813dd6fSViresh Kumar 30287813dd6fSViresh Kumar /** 30297813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 30307813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 30317813dd6fSViresh Kumar * @nb: Notifier block to be registered 30327813dd6fSViresh Kumar * 30337813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 30347813dd6fSViresh Kumar */ 30357813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 30367813dd6fSViresh Kumar { 30377813dd6fSViresh Kumar struct opp_table *opp_table; 30387813dd6fSViresh Kumar int ret; 30397813dd6fSViresh Kumar 30407813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 30417813dd6fSViresh Kumar if (IS_ERR(opp_table)) 30427813dd6fSViresh Kumar return PTR_ERR(opp_table); 30437813dd6fSViresh Kumar 30447813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 30457813dd6fSViresh Kumar 30467813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 30477813dd6fSViresh Kumar 30487813dd6fSViresh Kumar return ret; 30497813dd6fSViresh Kumar } 30507813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 30517813dd6fSViresh Kumar 30527813dd6fSViresh Kumar /** 30537813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 30547813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 30557813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 30567813dd6fSViresh Kumar * 30577813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 30587813dd6fSViresh Kumar */ 30597813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 30607813dd6fSViresh Kumar struct notifier_block *nb) 30617813dd6fSViresh Kumar { 30627813dd6fSViresh Kumar struct opp_table *opp_table; 30637813dd6fSViresh Kumar int ret; 30647813dd6fSViresh Kumar 30657813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 30667813dd6fSViresh Kumar if (IS_ERR(opp_table)) 30677813dd6fSViresh Kumar return PTR_ERR(opp_table); 30687813dd6fSViresh Kumar 30697813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 30707813dd6fSViresh Kumar 30717813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 30727813dd6fSViresh Kumar 30737813dd6fSViresh Kumar return ret; 30747813dd6fSViresh Kumar } 30757813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 30767813dd6fSViresh Kumar 30778aaf6264SViresh Kumar /** 30788aaf6264SViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 30798aaf6264SViresh Kumar * @dev: device pointer used to lookup OPP table. 30808aaf6264SViresh Kumar * 30818aaf6264SViresh Kumar * Free both OPPs created using static entries present in DT and the 30828aaf6264SViresh Kumar * dynamically added entries. 30838aaf6264SViresh Kumar */ 30848aaf6264SViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 30857813dd6fSViresh Kumar { 30867813dd6fSViresh Kumar struct opp_table *opp_table; 30877813dd6fSViresh Kumar 30887813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 30897813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 30907813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 30917813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 30927813dd6fSViresh Kumar 30937813dd6fSViresh Kumar if (error != -ENODEV) 30947813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 30957813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 30967813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 30977813dd6fSViresh Kumar error); 30987813dd6fSViresh Kumar return; 30997813dd6fSViresh Kumar } 31007813dd6fSViresh Kumar 3101922ff075SViresh Kumar /* 3102922ff075SViresh Kumar * Drop the extra reference only if the OPP table was successfully added 3103922ff075SViresh Kumar * with dev_pm_opp_of_add_table() earlier. 3104922ff075SViresh Kumar **/ 3105922ff075SViresh Kumar if (_opp_remove_all_static(opp_table)) 3106cdd6ed90SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3107cdd6ed90SViresh Kumar 3108922ff075SViresh Kumar /* Drop reference taken by _find_opp_table() */ 31097813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 31107813dd6fSViresh Kumar } 31117813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 3112ce8073d8SDmitry Osipenko 3113ce8073d8SDmitry Osipenko /** 3114ce8073d8SDmitry Osipenko * dev_pm_opp_sync_regulators() - Sync state of voltage regulators 3115ce8073d8SDmitry Osipenko * @dev: device for which we do this operation 3116ce8073d8SDmitry Osipenko * 3117ce8073d8SDmitry Osipenko * Sync voltage state of the OPP table regulators. 3118ce8073d8SDmitry Osipenko * 3119ce8073d8SDmitry Osipenko * Return: 0 on success or a negative error value. 3120ce8073d8SDmitry Osipenko */ 3121ce8073d8SDmitry Osipenko int dev_pm_opp_sync_regulators(struct device *dev) 3122ce8073d8SDmitry Osipenko { 3123ce8073d8SDmitry Osipenko struct opp_table *opp_table; 3124ce8073d8SDmitry Osipenko struct regulator *reg; 3125ce8073d8SDmitry Osipenko int i, ret = 0; 3126ce8073d8SDmitry Osipenko 3127ce8073d8SDmitry Osipenko /* Device may not have OPP table */ 3128ce8073d8SDmitry Osipenko opp_table = _find_opp_table(dev); 3129ce8073d8SDmitry Osipenko if (IS_ERR(opp_table)) 3130ce8073d8SDmitry Osipenko return 0; 3131ce8073d8SDmitry Osipenko 3132ce8073d8SDmitry Osipenko /* Regulator may not be required for the device */ 3133ce8073d8SDmitry Osipenko if (unlikely(!opp_table->regulators)) 3134ce8073d8SDmitry Osipenko goto put_table; 3135ce8073d8SDmitry Osipenko 3136ce8073d8SDmitry Osipenko /* Nothing to sync if voltage wasn't changed */ 3137ce8073d8SDmitry Osipenko if (!opp_table->enabled) 3138ce8073d8SDmitry Osipenko goto put_table; 3139ce8073d8SDmitry Osipenko 3140ce8073d8SDmitry Osipenko for (i = 0; i < opp_table->regulator_count; i++) { 3141ce8073d8SDmitry Osipenko reg = opp_table->regulators[i]; 3142ce8073d8SDmitry Osipenko ret = regulator_sync_voltage(reg); 3143ce8073d8SDmitry Osipenko if (ret) 3144ce8073d8SDmitry Osipenko break; 3145ce8073d8SDmitry Osipenko } 3146ce8073d8SDmitry Osipenko put_table: 3147ce8073d8SDmitry Osipenko /* Drop reference taken by _find_opp_table() */ 3148ce8073d8SDmitry Osipenko dev_pm_opp_put_opp_table(opp_table); 3149ce8073d8SDmitry Osipenko 3150ce8073d8SDmitry Osipenko return ret; 3151ce8073d8SDmitry Osipenko } 3152ce8073d8SDmitry Osipenko EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators); 3153