17813dd6fSViresh Kumar /* 27813dd6fSViresh Kumar * Generic OPP Interface 37813dd6fSViresh Kumar * 47813dd6fSViresh Kumar * Copyright (C) 2009-2010 Texas Instruments Incorporated. 57813dd6fSViresh Kumar * Nishanth Menon 67813dd6fSViresh Kumar * Romit Dasgupta 77813dd6fSViresh Kumar * Kevin Hilman 87813dd6fSViresh Kumar * 97813dd6fSViresh Kumar * This program is free software; you can redistribute it and/or modify 107813dd6fSViresh Kumar * it under the terms of the GNU General Public License version 2 as 117813dd6fSViresh Kumar * published by the Free Software Foundation. 127813dd6fSViresh Kumar */ 137813dd6fSViresh Kumar 147813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 157813dd6fSViresh Kumar 167813dd6fSViresh Kumar #include <linux/clk.h> 177813dd6fSViresh Kumar #include <linux/errno.h> 187813dd6fSViresh Kumar #include <linux/err.h> 197813dd6fSViresh Kumar #include <linux/slab.h> 207813dd6fSViresh Kumar #include <linux/device.h> 217813dd6fSViresh Kumar #include <linux/export.h> 22009acd19SViresh Kumar #include <linux/pm_domain.h> 237813dd6fSViresh Kumar #include <linux/regulator/consumer.h> 247813dd6fSViresh Kumar 257813dd6fSViresh Kumar #include "opp.h" 267813dd6fSViresh Kumar 277813dd6fSViresh Kumar /* 287813dd6fSViresh Kumar * The root of the list of all opp-tables. All opp_table structures branch off 297813dd6fSViresh Kumar * from here, with each opp_table containing the list of opps it supports in 307813dd6fSViresh Kumar * various states of availability. 317813dd6fSViresh Kumar */ 327813dd6fSViresh Kumar LIST_HEAD(opp_tables); 337813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */ 347813dd6fSViresh Kumar DEFINE_MUTEX(opp_table_lock); 357813dd6fSViresh Kumar 367813dd6fSViresh Kumar static struct opp_device *_find_opp_dev(const struct device *dev, 377813dd6fSViresh Kumar struct opp_table *opp_table) 387813dd6fSViresh Kumar { 397813dd6fSViresh Kumar struct opp_device *opp_dev; 407813dd6fSViresh Kumar 417813dd6fSViresh Kumar list_for_each_entry(opp_dev, &opp_table->dev_list, node) 427813dd6fSViresh Kumar if (opp_dev->dev == dev) 437813dd6fSViresh Kumar return opp_dev; 447813dd6fSViresh Kumar 457813dd6fSViresh Kumar return NULL; 467813dd6fSViresh Kumar } 477813dd6fSViresh Kumar 487813dd6fSViresh Kumar static struct opp_table *_find_opp_table_unlocked(struct device *dev) 497813dd6fSViresh Kumar { 507813dd6fSViresh Kumar struct opp_table *opp_table; 517813dd6fSViresh Kumar 527813dd6fSViresh Kumar list_for_each_entry(opp_table, &opp_tables, node) { 537813dd6fSViresh Kumar if (_find_opp_dev(dev, opp_table)) { 547813dd6fSViresh Kumar _get_opp_table_kref(opp_table); 557813dd6fSViresh Kumar 567813dd6fSViresh Kumar return opp_table; 577813dd6fSViresh Kumar } 587813dd6fSViresh Kumar } 597813dd6fSViresh Kumar 607813dd6fSViresh Kumar return ERR_PTR(-ENODEV); 617813dd6fSViresh Kumar } 627813dd6fSViresh Kumar 637813dd6fSViresh Kumar /** 647813dd6fSViresh Kumar * _find_opp_table() - find opp_table struct using device pointer 657813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table 667813dd6fSViresh Kumar * 677813dd6fSViresh Kumar * Search OPP table for one containing matching device. 687813dd6fSViresh Kumar * 697813dd6fSViresh Kumar * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or 707813dd6fSViresh Kumar * -EINVAL based on type of error. 717813dd6fSViresh Kumar * 727813dd6fSViresh Kumar * The callers must call dev_pm_opp_put_opp_table() after the table is used. 737813dd6fSViresh Kumar */ 747813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev) 757813dd6fSViresh Kumar { 767813dd6fSViresh Kumar struct opp_table *opp_table; 777813dd6fSViresh Kumar 787813dd6fSViresh Kumar if (IS_ERR_OR_NULL(dev)) { 797813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 807813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 817813dd6fSViresh Kumar } 827813dd6fSViresh Kumar 837813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 847813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 857813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 867813dd6fSViresh Kumar 877813dd6fSViresh Kumar return opp_table; 887813dd6fSViresh Kumar } 897813dd6fSViresh Kumar 907813dd6fSViresh Kumar /** 917813dd6fSViresh Kumar * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp 927813dd6fSViresh Kumar * @opp: opp for which voltage has to be returned for 937813dd6fSViresh Kumar * 947813dd6fSViresh Kumar * Return: voltage in micro volt corresponding to the opp, else 957813dd6fSViresh Kumar * return 0 967813dd6fSViresh Kumar * 977813dd6fSViresh Kumar * This is useful only for devices with single power supply. 987813dd6fSViresh Kumar */ 997813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 1007813dd6fSViresh Kumar { 1017813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp)) { 1027813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1037813dd6fSViresh Kumar return 0; 1047813dd6fSViresh Kumar } 1057813dd6fSViresh Kumar 1067813dd6fSViresh Kumar return opp->supplies[0].u_volt; 1077813dd6fSViresh Kumar } 1087813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); 1097813dd6fSViresh Kumar 1107813dd6fSViresh Kumar /** 1117813dd6fSViresh Kumar * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp 1127813dd6fSViresh Kumar * @opp: opp for which frequency has to be returned for 1137813dd6fSViresh Kumar * 1147813dd6fSViresh Kumar * Return: frequency in hertz corresponding to the opp, else 1157813dd6fSViresh Kumar * return 0 1167813dd6fSViresh Kumar */ 1177813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) 1187813dd6fSViresh Kumar { 1197813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 1207813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1217813dd6fSViresh Kumar return 0; 1227813dd6fSViresh Kumar } 1237813dd6fSViresh Kumar 1247813dd6fSViresh Kumar return opp->rate; 1257813dd6fSViresh Kumar } 1267813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); 1277813dd6fSViresh Kumar 1287813dd6fSViresh Kumar /** 1297813dd6fSViresh Kumar * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not 1307813dd6fSViresh Kumar * @opp: opp for which turbo mode is being verified 1317813dd6fSViresh Kumar * 1327813dd6fSViresh Kumar * Turbo OPPs are not for normal use, and can be enabled (under certain 1337813dd6fSViresh Kumar * conditions) for short duration of times to finish high throughput work 1347813dd6fSViresh Kumar * quickly. Running on them for longer times may overheat the chip. 1357813dd6fSViresh Kumar * 1367813dd6fSViresh Kumar * Return: true if opp is turbo opp, else false. 1377813dd6fSViresh Kumar */ 1387813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 1397813dd6fSViresh Kumar { 1407813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 1417813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1427813dd6fSViresh Kumar return false; 1437813dd6fSViresh Kumar } 1447813dd6fSViresh Kumar 1457813dd6fSViresh Kumar return opp->turbo; 1467813dd6fSViresh Kumar } 1477813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 1487813dd6fSViresh Kumar 1497813dd6fSViresh Kumar /** 1507813dd6fSViresh Kumar * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds 1517813dd6fSViresh Kumar * @dev: device for which we do this operation 1527813dd6fSViresh Kumar * 1537813dd6fSViresh Kumar * Return: This function returns the max clock latency in nanoseconds. 1547813dd6fSViresh Kumar */ 1557813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 1567813dd6fSViresh Kumar { 1577813dd6fSViresh Kumar struct opp_table *opp_table; 1587813dd6fSViresh Kumar unsigned long clock_latency_ns; 1597813dd6fSViresh Kumar 1607813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 1617813dd6fSViresh Kumar if (IS_ERR(opp_table)) 1627813dd6fSViresh Kumar return 0; 1637813dd6fSViresh Kumar 1647813dd6fSViresh Kumar clock_latency_ns = opp_table->clock_latency_ns_max; 1657813dd6fSViresh Kumar 1667813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1677813dd6fSViresh Kumar 1687813dd6fSViresh Kumar return clock_latency_ns; 1697813dd6fSViresh Kumar } 1707813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 1717813dd6fSViresh Kumar 1727813dd6fSViresh Kumar /** 1737813dd6fSViresh Kumar * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 1747813dd6fSViresh Kumar * @dev: device for which we do this operation 1757813dd6fSViresh Kumar * 1767813dd6fSViresh Kumar * Return: This function returns the max voltage latency in nanoseconds. 1777813dd6fSViresh Kumar */ 1787813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 1797813dd6fSViresh Kumar { 1807813dd6fSViresh Kumar struct opp_table *opp_table; 1817813dd6fSViresh Kumar struct dev_pm_opp *opp; 1827813dd6fSViresh Kumar struct regulator *reg; 1837813dd6fSViresh Kumar unsigned long latency_ns = 0; 1847813dd6fSViresh Kumar int ret, i, count; 1857813dd6fSViresh Kumar struct { 1867813dd6fSViresh Kumar unsigned long min; 1877813dd6fSViresh Kumar unsigned long max; 1887813dd6fSViresh Kumar } *uV; 1897813dd6fSViresh Kumar 1907813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 1917813dd6fSViresh Kumar if (IS_ERR(opp_table)) 1927813dd6fSViresh Kumar return 0; 1937813dd6fSViresh Kumar 1947813dd6fSViresh Kumar count = opp_table->regulator_count; 1957813dd6fSViresh Kumar 1967813dd6fSViresh Kumar /* Regulator may not be required for the device */ 1977813dd6fSViresh Kumar if (!count) 1987813dd6fSViresh Kumar goto put_opp_table; 1997813dd6fSViresh Kumar 2007813dd6fSViresh Kumar uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 2017813dd6fSViresh Kumar if (!uV) 2027813dd6fSViresh Kumar goto put_opp_table; 2037813dd6fSViresh Kumar 2047813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 2057813dd6fSViresh Kumar 2067813dd6fSViresh Kumar for (i = 0; i < count; i++) { 2077813dd6fSViresh Kumar uV[i].min = ~0; 2087813dd6fSViresh Kumar uV[i].max = 0; 2097813dd6fSViresh Kumar 2107813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 2117813dd6fSViresh Kumar if (!opp->available) 2127813dd6fSViresh Kumar continue; 2137813dd6fSViresh Kumar 2147813dd6fSViresh Kumar if (opp->supplies[i].u_volt_min < uV[i].min) 2157813dd6fSViresh Kumar uV[i].min = opp->supplies[i].u_volt_min; 2167813dd6fSViresh Kumar if (opp->supplies[i].u_volt_max > uV[i].max) 2177813dd6fSViresh Kumar uV[i].max = opp->supplies[i].u_volt_max; 2187813dd6fSViresh Kumar } 2197813dd6fSViresh Kumar } 2207813dd6fSViresh Kumar 2217813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 2227813dd6fSViresh Kumar 2237813dd6fSViresh Kumar /* 2247813dd6fSViresh Kumar * The caller needs to ensure that opp_table (and hence the regulator) 2257813dd6fSViresh Kumar * isn't freed, while we are executing this routine. 2267813dd6fSViresh Kumar */ 2277813dd6fSViresh Kumar for (i = 0; i < count; i++) { 2287813dd6fSViresh Kumar reg = opp_table->regulators[i]; 2297813dd6fSViresh Kumar ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); 2307813dd6fSViresh Kumar if (ret > 0) 2317813dd6fSViresh Kumar latency_ns += ret * 1000; 2327813dd6fSViresh Kumar } 2337813dd6fSViresh Kumar 2347813dd6fSViresh Kumar kfree(uV); 2357813dd6fSViresh Kumar put_opp_table: 2367813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2377813dd6fSViresh Kumar 2387813dd6fSViresh Kumar return latency_ns; 2397813dd6fSViresh Kumar } 2407813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); 2417813dd6fSViresh Kumar 2427813dd6fSViresh Kumar /** 2437813dd6fSViresh Kumar * dev_pm_opp_get_max_transition_latency() - Get max transition latency in 2447813dd6fSViresh Kumar * nanoseconds 2457813dd6fSViresh Kumar * @dev: device for which we do this operation 2467813dd6fSViresh Kumar * 2477813dd6fSViresh Kumar * Return: This function returns the max transition latency, in nanoseconds, to 2487813dd6fSViresh Kumar * switch from one OPP to other. 2497813dd6fSViresh Kumar */ 2507813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 2517813dd6fSViresh Kumar { 2527813dd6fSViresh Kumar return dev_pm_opp_get_max_volt_latency(dev) + 2537813dd6fSViresh Kumar dev_pm_opp_get_max_clock_latency(dev); 2547813dd6fSViresh Kumar } 2557813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 2567813dd6fSViresh Kumar 2577813dd6fSViresh Kumar /** 2587813dd6fSViresh Kumar * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 2597813dd6fSViresh Kumar * @dev: device for which we do this operation 2607813dd6fSViresh Kumar * 2617813dd6fSViresh Kumar * Return: This function returns the frequency of the OPP marked as suspend_opp 2627813dd6fSViresh Kumar * if one is available, else returns 0; 2637813dd6fSViresh Kumar */ 2647813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 2657813dd6fSViresh Kumar { 2667813dd6fSViresh Kumar struct opp_table *opp_table; 2677813dd6fSViresh Kumar unsigned long freq = 0; 2687813dd6fSViresh Kumar 2697813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2707813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2717813dd6fSViresh Kumar return 0; 2727813dd6fSViresh Kumar 2737813dd6fSViresh Kumar if (opp_table->suspend_opp && opp_table->suspend_opp->available) 2747813dd6fSViresh Kumar freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 2757813dd6fSViresh Kumar 2767813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2777813dd6fSViresh Kumar 2787813dd6fSViresh Kumar return freq; 2797813dd6fSViresh Kumar } 2807813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 2817813dd6fSViresh Kumar 282a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table) 283a1e8c136SViresh Kumar { 284a1e8c136SViresh Kumar struct dev_pm_opp *opp; 285a1e8c136SViresh Kumar int count = 0; 286a1e8c136SViresh Kumar 287a1e8c136SViresh Kumar mutex_lock(&opp_table->lock); 288a1e8c136SViresh Kumar 289a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 290a1e8c136SViresh Kumar if (opp->available) 291a1e8c136SViresh Kumar count++; 292a1e8c136SViresh Kumar } 293a1e8c136SViresh Kumar 294a1e8c136SViresh Kumar mutex_unlock(&opp_table->lock); 295a1e8c136SViresh Kumar 296a1e8c136SViresh Kumar return count; 297a1e8c136SViresh Kumar } 298a1e8c136SViresh Kumar 2997813dd6fSViresh Kumar /** 3007813dd6fSViresh Kumar * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table 3017813dd6fSViresh Kumar * @dev: device for which we do this operation 3027813dd6fSViresh Kumar * 3037813dd6fSViresh Kumar * Return: This function returns the number of available opps if there are any, 3047813dd6fSViresh Kumar * else returns 0 if none or the corresponding error value. 3057813dd6fSViresh Kumar */ 3067813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev) 3077813dd6fSViresh Kumar { 3087813dd6fSViresh Kumar struct opp_table *opp_table; 309a1e8c136SViresh Kumar int count; 3107813dd6fSViresh Kumar 3117813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3127813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 3137813dd6fSViresh Kumar count = PTR_ERR(opp_table); 314035ed072SFabio Estevam dev_dbg(dev, "%s: OPP table not found (%d)\n", 3157813dd6fSViresh Kumar __func__, count); 316a1e8c136SViresh Kumar return 0; 3177813dd6fSViresh Kumar } 3187813dd6fSViresh Kumar 319a1e8c136SViresh Kumar count = _get_opp_count(opp_table); 3207813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3217813dd6fSViresh Kumar 3227813dd6fSViresh Kumar return count; 3237813dd6fSViresh Kumar } 3247813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); 3257813dd6fSViresh Kumar 3267813dd6fSViresh Kumar /** 3277813dd6fSViresh Kumar * dev_pm_opp_find_freq_exact() - search for an exact frequency 3287813dd6fSViresh Kumar * @dev: device for which we do this operation 3297813dd6fSViresh Kumar * @freq: frequency to search for 3307813dd6fSViresh Kumar * @available: true/false - match for available opp 3317813dd6fSViresh Kumar * 3327813dd6fSViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 3337813dd6fSViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 3347813dd6fSViresh Kumar * be handled using IS_ERR. Error return values can be: 3357813dd6fSViresh Kumar * EINVAL: for bad pointer 3367813dd6fSViresh Kumar * ERANGE: no match found for search 3377813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 3387813dd6fSViresh Kumar * 3397813dd6fSViresh Kumar * Note: available is a modifier for the search. if available=true, then the 3407813dd6fSViresh Kumar * match is for exact matching frequency and is available in the stored OPP 3417813dd6fSViresh Kumar * table. if false, the match is for exact frequency which is not available. 3427813dd6fSViresh Kumar * 3437813dd6fSViresh Kumar * This provides a mechanism to enable an opp which is not available currently 3447813dd6fSViresh Kumar * or the opposite as well. 3457813dd6fSViresh Kumar * 3467813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 3477813dd6fSViresh Kumar * use. 3487813dd6fSViresh Kumar */ 3497813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 3507813dd6fSViresh Kumar unsigned long freq, 3517813dd6fSViresh Kumar bool available) 3527813dd6fSViresh Kumar { 3537813dd6fSViresh Kumar struct opp_table *opp_table; 3547813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 3557813dd6fSViresh Kumar 3567813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3577813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 3587813dd6fSViresh Kumar int r = PTR_ERR(opp_table); 3597813dd6fSViresh Kumar 3607813dd6fSViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 3617813dd6fSViresh Kumar return ERR_PTR(r); 3627813dd6fSViresh Kumar } 3637813dd6fSViresh Kumar 3647813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 3657813dd6fSViresh Kumar 3667813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 3677813dd6fSViresh Kumar if (temp_opp->available == available && 3687813dd6fSViresh Kumar temp_opp->rate == freq) { 3697813dd6fSViresh Kumar opp = temp_opp; 3707813dd6fSViresh Kumar 3717813dd6fSViresh Kumar /* Increment the reference count of OPP */ 3727813dd6fSViresh Kumar dev_pm_opp_get(opp); 3737813dd6fSViresh Kumar break; 3747813dd6fSViresh Kumar } 3757813dd6fSViresh Kumar } 3767813dd6fSViresh Kumar 3777813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 3787813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3797813dd6fSViresh Kumar 3807813dd6fSViresh Kumar return opp; 3817813dd6fSViresh Kumar } 3827813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 3837813dd6fSViresh Kumar 3847813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 3857813dd6fSViresh Kumar unsigned long *freq) 3867813dd6fSViresh Kumar { 3877813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 3887813dd6fSViresh Kumar 3897813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 3907813dd6fSViresh Kumar 3917813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 3927813dd6fSViresh Kumar if (temp_opp->available && temp_opp->rate >= *freq) { 3937813dd6fSViresh Kumar opp = temp_opp; 3947813dd6fSViresh Kumar *freq = opp->rate; 3957813dd6fSViresh Kumar 3967813dd6fSViresh Kumar /* Increment the reference count of OPP */ 3977813dd6fSViresh Kumar dev_pm_opp_get(opp); 3987813dd6fSViresh Kumar break; 3997813dd6fSViresh Kumar } 4007813dd6fSViresh Kumar } 4017813dd6fSViresh Kumar 4027813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 4037813dd6fSViresh Kumar 4047813dd6fSViresh Kumar return opp; 4057813dd6fSViresh Kumar } 4067813dd6fSViresh Kumar 4077813dd6fSViresh Kumar /** 4087813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 4097813dd6fSViresh Kumar * @dev: device for which we do this operation 4107813dd6fSViresh Kumar * @freq: Start frequency 4117813dd6fSViresh Kumar * 4127813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 4137813dd6fSViresh Kumar * for a device. 4147813dd6fSViresh Kumar * 4157813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 4167813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 4177813dd6fSViresh Kumar * values can be: 4187813dd6fSViresh Kumar * EINVAL: for bad pointer 4197813dd6fSViresh Kumar * ERANGE: no match found for search 4207813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 4217813dd6fSViresh Kumar * 4227813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 4237813dd6fSViresh Kumar * use. 4247813dd6fSViresh Kumar */ 4257813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 4267813dd6fSViresh Kumar unsigned long *freq) 4277813dd6fSViresh Kumar { 4287813dd6fSViresh Kumar struct opp_table *opp_table; 4297813dd6fSViresh Kumar struct dev_pm_opp *opp; 4307813dd6fSViresh Kumar 4317813dd6fSViresh Kumar if (!dev || !freq) { 4327813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 4337813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 4347813dd6fSViresh Kumar } 4357813dd6fSViresh Kumar 4367813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 4377813dd6fSViresh Kumar if (IS_ERR(opp_table)) 4387813dd6fSViresh Kumar return ERR_CAST(opp_table); 4397813dd6fSViresh Kumar 4407813dd6fSViresh Kumar opp = _find_freq_ceil(opp_table, freq); 4417813dd6fSViresh Kumar 4427813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4437813dd6fSViresh Kumar 4447813dd6fSViresh Kumar return opp; 4457813dd6fSViresh Kumar } 4467813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 4477813dd6fSViresh Kumar 4487813dd6fSViresh Kumar /** 4497813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 4507813dd6fSViresh Kumar * @dev: device for which we do this operation 4517813dd6fSViresh Kumar * @freq: Start frequency 4527813dd6fSViresh Kumar * 4537813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 4547813dd6fSViresh Kumar * for a device. 4557813dd6fSViresh Kumar * 4567813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 4577813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 4587813dd6fSViresh Kumar * values can be: 4597813dd6fSViresh Kumar * EINVAL: for bad pointer 4607813dd6fSViresh Kumar * ERANGE: no match found for search 4617813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 4627813dd6fSViresh Kumar * 4637813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 4647813dd6fSViresh Kumar * use. 4657813dd6fSViresh Kumar */ 4667813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 4677813dd6fSViresh Kumar unsigned long *freq) 4687813dd6fSViresh Kumar { 4697813dd6fSViresh Kumar struct opp_table *opp_table; 4707813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 4717813dd6fSViresh Kumar 4727813dd6fSViresh Kumar if (!dev || !freq) { 4737813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 4747813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 4757813dd6fSViresh Kumar } 4767813dd6fSViresh Kumar 4777813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 4787813dd6fSViresh Kumar if (IS_ERR(opp_table)) 4797813dd6fSViresh Kumar return ERR_CAST(opp_table); 4807813dd6fSViresh Kumar 4817813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 4827813dd6fSViresh Kumar 4837813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 4847813dd6fSViresh Kumar if (temp_opp->available) { 4857813dd6fSViresh Kumar /* go to the next node, before choosing prev */ 4867813dd6fSViresh Kumar if (temp_opp->rate > *freq) 4877813dd6fSViresh Kumar break; 4887813dd6fSViresh Kumar else 4897813dd6fSViresh Kumar opp = temp_opp; 4907813dd6fSViresh Kumar } 4917813dd6fSViresh Kumar } 4927813dd6fSViresh Kumar 4937813dd6fSViresh Kumar /* Increment the reference count of OPP */ 4947813dd6fSViresh Kumar if (!IS_ERR(opp)) 4957813dd6fSViresh Kumar dev_pm_opp_get(opp); 4967813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 4977813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4987813dd6fSViresh Kumar 4997813dd6fSViresh Kumar if (!IS_ERR(opp)) 5007813dd6fSViresh Kumar *freq = opp->rate; 5017813dd6fSViresh Kumar 5027813dd6fSViresh Kumar return opp; 5037813dd6fSViresh Kumar } 5047813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 5057813dd6fSViresh Kumar 5067813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 5077813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 5087813dd6fSViresh Kumar { 5097813dd6fSViresh Kumar int ret; 5107813dd6fSViresh Kumar 5117813dd6fSViresh Kumar /* Regulator not available for device */ 5127813dd6fSViresh Kumar if (IS_ERR(reg)) { 5137813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 5147813dd6fSViresh Kumar PTR_ERR(reg)); 5157813dd6fSViresh Kumar return 0; 5167813dd6fSViresh Kumar } 5177813dd6fSViresh Kumar 5187813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 5197813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 5207813dd6fSViresh Kumar 5217813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 5227813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 5237813dd6fSViresh Kumar if (ret) 5247813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 5257813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 5267813dd6fSViresh Kumar supply->u_volt_max, ret); 5277813dd6fSViresh Kumar 5287813dd6fSViresh Kumar return ret; 5297813dd6fSViresh Kumar } 5307813dd6fSViresh Kumar 5317813dd6fSViresh Kumar static inline int 5327813dd6fSViresh Kumar _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 5337813dd6fSViresh Kumar unsigned long old_freq, unsigned long freq) 5347813dd6fSViresh Kumar { 5357813dd6fSViresh Kumar int ret; 5367813dd6fSViresh Kumar 5377813dd6fSViresh Kumar ret = clk_set_rate(clk, freq); 5387813dd6fSViresh Kumar if (ret) { 5397813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 5407813dd6fSViresh Kumar ret); 5417813dd6fSViresh Kumar } 5427813dd6fSViresh Kumar 5437813dd6fSViresh Kumar return ret; 5447813dd6fSViresh Kumar } 5457813dd6fSViresh Kumar 546009acd19SViresh Kumar static inline int 547009acd19SViresh Kumar _generic_set_opp_domain(struct device *dev, struct clk *clk, 548009acd19SViresh Kumar unsigned long old_freq, unsigned long freq, 549009acd19SViresh Kumar unsigned int old_pstate, unsigned int new_pstate) 550009acd19SViresh Kumar { 551009acd19SViresh Kumar int ret; 552009acd19SViresh Kumar 553009acd19SViresh Kumar /* Scaling up? Scale domain performance state before frequency */ 554009acd19SViresh Kumar if (freq > old_freq) { 555009acd19SViresh Kumar ret = dev_pm_genpd_set_performance_state(dev, new_pstate); 556009acd19SViresh Kumar if (ret) 557009acd19SViresh Kumar return ret; 558009acd19SViresh Kumar } 559009acd19SViresh Kumar 560009acd19SViresh Kumar ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq); 561009acd19SViresh Kumar if (ret) 562009acd19SViresh Kumar goto restore_domain_state; 563009acd19SViresh Kumar 564009acd19SViresh Kumar /* Scaling down? Scale domain performance state after frequency */ 565009acd19SViresh Kumar if (freq < old_freq) { 566009acd19SViresh Kumar ret = dev_pm_genpd_set_performance_state(dev, new_pstate); 567009acd19SViresh Kumar if (ret) 568009acd19SViresh Kumar goto restore_freq; 569009acd19SViresh Kumar } 570009acd19SViresh Kumar 571009acd19SViresh Kumar return 0; 572009acd19SViresh Kumar 573009acd19SViresh Kumar restore_freq: 574009acd19SViresh Kumar if (_generic_set_opp_clk_only(dev, clk, freq, old_freq)) 575009acd19SViresh Kumar dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 576009acd19SViresh Kumar __func__, old_freq); 577009acd19SViresh Kumar restore_domain_state: 578009acd19SViresh Kumar if (freq > old_freq) 579009acd19SViresh Kumar dev_pm_genpd_set_performance_state(dev, old_pstate); 580009acd19SViresh Kumar 581009acd19SViresh Kumar return ret; 582009acd19SViresh Kumar } 583009acd19SViresh Kumar 5847813dd6fSViresh Kumar static int _generic_set_opp_regulator(const struct opp_table *opp_table, 5857813dd6fSViresh Kumar struct device *dev, 5867813dd6fSViresh Kumar unsigned long old_freq, 5877813dd6fSViresh Kumar unsigned long freq, 5887813dd6fSViresh Kumar struct dev_pm_opp_supply *old_supply, 5897813dd6fSViresh Kumar struct dev_pm_opp_supply *new_supply) 5907813dd6fSViresh Kumar { 5917813dd6fSViresh Kumar struct regulator *reg = opp_table->regulators[0]; 5927813dd6fSViresh Kumar int ret; 5937813dd6fSViresh Kumar 5947813dd6fSViresh Kumar /* This function only supports single regulator per device */ 5957813dd6fSViresh Kumar if (WARN_ON(opp_table->regulator_count > 1)) { 5967813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 5977813dd6fSViresh Kumar return -EINVAL; 5987813dd6fSViresh Kumar } 5997813dd6fSViresh Kumar 6007813dd6fSViresh Kumar /* Scaling up? Scale voltage before frequency */ 6017813dd6fSViresh Kumar if (freq > old_freq) { 6027813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 6037813dd6fSViresh Kumar if (ret) 6047813dd6fSViresh Kumar goto restore_voltage; 6057813dd6fSViresh Kumar } 6067813dd6fSViresh Kumar 6077813dd6fSViresh Kumar /* Change frequency */ 6087813dd6fSViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq); 6097813dd6fSViresh Kumar if (ret) 6107813dd6fSViresh Kumar goto restore_voltage; 6117813dd6fSViresh Kumar 6127813dd6fSViresh Kumar /* Scaling down? Scale voltage after frequency */ 6137813dd6fSViresh Kumar if (freq < old_freq) { 6147813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 6157813dd6fSViresh Kumar if (ret) 6167813dd6fSViresh Kumar goto restore_freq; 6177813dd6fSViresh Kumar } 6187813dd6fSViresh Kumar 6197813dd6fSViresh Kumar return 0; 6207813dd6fSViresh Kumar 6217813dd6fSViresh Kumar restore_freq: 6227813dd6fSViresh Kumar if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq)) 6237813dd6fSViresh Kumar dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 6247813dd6fSViresh Kumar __func__, old_freq); 6257813dd6fSViresh Kumar restore_voltage: 6267813dd6fSViresh Kumar /* This shouldn't harm even if the voltages weren't updated earlier */ 6277813dd6fSViresh Kumar if (old_supply) 6287813dd6fSViresh Kumar _set_opp_voltage(dev, reg, old_supply); 6297813dd6fSViresh Kumar 6307813dd6fSViresh Kumar return ret; 6317813dd6fSViresh Kumar } 6327813dd6fSViresh Kumar 6337813dd6fSViresh Kumar /** 6347813dd6fSViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 6357813dd6fSViresh Kumar * @dev: device for which we do this operation 6367813dd6fSViresh Kumar * @target_freq: frequency to achieve 6377813dd6fSViresh Kumar * 6387813dd6fSViresh Kumar * This configures the power-supplies and clock source to the levels specified 6397813dd6fSViresh Kumar * by the OPP corresponding to the target_freq. 6407813dd6fSViresh Kumar */ 6417813dd6fSViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 6427813dd6fSViresh Kumar { 6437813dd6fSViresh Kumar struct opp_table *opp_table; 6447813dd6fSViresh Kumar unsigned long freq, old_freq; 6457813dd6fSViresh Kumar struct dev_pm_opp *old_opp, *opp; 6467813dd6fSViresh Kumar struct clk *clk; 6477813dd6fSViresh Kumar int ret, size; 6487813dd6fSViresh Kumar 6497813dd6fSViresh Kumar if (unlikely(!target_freq)) { 6507813dd6fSViresh Kumar dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, 6517813dd6fSViresh Kumar target_freq); 6527813dd6fSViresh Kumar return -EINVAL; 6537813dd6fSViresh Kumar } 6547813dd6fSViresh Kumar 6557813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 6567813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 6577813dd6fSViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 6587813dd6fSViresh Kumar return PTR_ERR(opp_table); 6597813dd6fSViresh Kumar } 6607813dd6fSViresh Kumar 6617813dd6fSViresh Kumar clk = opp_table->clk; 6627813dd6fSViresh Kumar if (IS_ERR(clk)) { 6637813dd6fSViresh Kumar dev_err(dev, "%s: No clock available for the device\n", 6647813dd6fSViresh Kumar __func__); 6657813dd6fSViresh Kumar ret = PTR_ERR(clk); 6667813dd6fSViresh Kumar goto put_opp_table; 6677813dd6fSViresh Kumar } 6687813dd6fSViresh Kumar 6697813dd6fSViresh Kumar freq = clk_round_rate(clk, target_freq); 6707813dd6fSViresh Kumar if ((long)freq <= 0) 6717813dd6fSViresh Kumar freq = target_freq; 6727813dd6fSViresh Kumar 6737813dd6fSViresh Kumar old_freq = clk_get_rate(clk); 6747813dd6fSViresh Kumar 6757813dd6fSViresh Kumar /* Return early if nothing to do */ 6767813dd6fSViresh Kumar if (old_freq == freq) { 6777813dd6fSViresh Kumar dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", 6787813dd6fSViresh Kumar __func__, freq); 6797813dd6fSViresh Kumar ret = 0; 6807813dd6fSViresh Kumar goto put_opp_table; 6817813dd6fSViresh Kumar } 6827813dd6fSViresh Kumar 6837813dd6fSViresh Kumar old_opp = _find_freq_ceil(opp_table, &old_freq); 6847813dd6fSViresh Kumar if (IS_ERR(old_opp)) { 6857813dd6fSViresh Kumar dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", 6867813dd6fSViresh Kumar __func__, old_freq, PTR_ERR(old_opp)); 6877813dd6fSViresh Kumar } 6887813dd6fSViresh Kumar 6897813dd6fSViresh Kumar opp = _find_freq_ceil(opp_table, &freq); 6907813dd6fSViresh Kumar if (IS_ERR(opp)) { 6917813dd6fSViresh Kumar ret = PTR_ERR(opp); 6927813dd6fSViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 6937813dd6fSViresh Kumar __func__, freq, ret); 6947813dd6fSViresh Kumar goto put_old_opp; 6957813dd6fSViresh Kumar } 6967813dd6fSViresh Kumar 6977813dd6fSViresh Kumar dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, 6987813dd6fSViresh Kumar old_freq, freq); 6997813dd6fSViresh Kumar 7007813dd6fSViresh Kumar /* Only frequency scaling */ 7017813dd6fSViresh Kumar if (!opp_table->regulators) { 702009acd19SViresh Kumar /* 703009acd19SViresh Kumar * We don't support devices with both regulator and 704009acd19SViresh Kumar * domain performance-state for now. 705009acd19SViresh Kumar */ 706009acd19SViresh Kumar if (opp_table->genpd_performance_state) 707009acd19SViresh Kumar ret = _generic_set_opp_domain(dev, clk, old_freq, freq, 708009acd19SViresh Kumar IS_ERR(old_opp) ? 0 : old_opp->pstate, 709009acd19SViresh Kumar opp->pstate); 710009acd19SViresh Kumar else 7117813dd6fSViresh Kumar ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq); 7127813dd6fSViresh Kumar } else if (!opp_table->set_opp) { 7137813dd6fSViresh Kumar ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq, 7147813dd6fSViresh Kumar IS_ERR(old_opp) ? NULL : old_opp->supplies, 7157813dd6fSViresh Kumar opp->supplies); 7167813dd6fSViresh Kumar } else { 7177813dd6fSViresh Kumar struct dev_pm_set_opp_data *data; 7187813dd6fSViresh Kumar 7197813dd6fSViresh Kumar data = opp_table->set_opp_data; 7207813dd6fSViresh Kumar data->regulators = opp_table->regulators; 7217813dd6fSViresh Kumar data->regulator_count = opp_table->regulator_count; 7227813dd6fSViresh Kumar data->clk = clk; 7237813dd6fSViresh Kumar data->dev = dev; 7247813dd6fSViresh Kumar 7257813dd6fSViresh Kumar data->old_opp.rate = old_freq; 7267813dd6fSViresh Kumar size = sizeof(*opp->supplies) * opp_table->regulator_count; 7277813dd6fSViresh Kumar if (IS_ERR(old_opp)) 7287813dd6fSViresh Kumar memset(data->old_opp.supplies, 0, size); 7297813dd6fSViresh Kumar else 7307813dd6fSViresh Kumar memcpy(data->old_opp.supplies, old_opp->supplies, size); 7317813dd6fSViresh Kumar 7327813dd6fSViresh Kumar data->new_opp.rate = freq; 7337813dd6fSViresh Kumar memcpy(data->new_opp.supplies, opp->supplies, size); 7347813dd6fSViresh Kumar 7357813dd6fSViresh Kumar ret = opp_table->set_opp(data); 7367813dd6fSViresh Kumar } 7377813dd6fSViresh Kumar 7387813dd6fSViresh Kumar dev_pm_opp_put(opp); 7397813dd6fSViresh Kumar put_old_opp: 7407813dd6fSViresh Kumar if (!IS_ERR(old_opp)) 7417813dd6fSViresh Kumar dev_pm_opp_put(old_opp); 7427813dd6fSViresh Kumar put_opp_table: 7437813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 7447813dd6fSViresh Kumar return ret; 7457813dd6fSViresh Kumar } 7467813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 7477813dd6fSViresh Kumar 7487813dd6fSViresh Kumar /* OPP-dev Helpers */ 7497813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 7507813dd6fSViresh Kumar struct opp_table *opp_table) 7517813dd6fSViresh Kumar { 7527813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 7537813dd6fSViresh Kumar list_del(&opp_dev->node); 7547813dd6fSViresh Kumar kfree(opp_dev); 7557813dd6fSViresh Kumar } 7567813dd6fSViresh Kumar 7577813dd6fSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 7587813dd6fSViresh Kumar struct opp_table *opp_table) 7597813dd6fSViresh Kumar { 7607813dd6fSViresh Kumar struct opp_device *opp_dev; 7617813dd6fSViresh Kumar int ret; 7627813dd6fSViresh Kumar 7637813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 7647813dd6fSViresh Kumar if (!opp_dev) 7657813dd6fSViresh Kumar return NULL; 7667813dd6fSViresh Kumar 7677813dd6fSViresh Kumar /* Initialize opp-dev */ 7687813dd6fSViresh Kumar opp_dev->dev = dev; 7697813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 7707813dd6fSViresh Kumar 7717813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 7727813dd6fSViresh Kumar ret = opp_debug_register(opp_dev, opp_table); 7737813dd6fSViresh Kumar if (ret) 7747813dd6fSViresh Kumar dev_err(dev, "%s: Failed to register opp debugfs (%d)\n", 7757813dd6fSViresh Kumar __func__, ret); 7767813dd6fSViresh Kumar 7777813dd6fSViresh Kumar return opp_dev; 7787813dd6fSViresh Kumar } 7797813dd6fSViresh Kumar 7807813dd6fSViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev) 7817813dd6fSViresh Kumar { 7827813dd6fSViresh Kumar struct opp_table *opp_table; 7837813dd6fSViresh Kumar struct opp_device *opp_dev; 7847813dd6fSViresh Kumar int ret; 7857813dd6fSViresh Kumar 7867813dd6fSViresh Kumar /* 7877813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 7887813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 7897813dd6fSViresh Kumar */ 7907813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 7917813dd6fSViresh Kumar if (!opp_table) 7927813dd6fSViresh Kumar return NULL; 7937813dd6fSViresh Kumar 7947813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 7957813dd6fSViresh Kumar 7967813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 7977813dd6fSViresh Kumar if (!opp_dev) { 7987813dd6fSViresh Kumar kfree(opp_table); 7997813dd6fSViresh Kumar return NULL; 8007813dd6fSViresh Kumar } 8017813dd6fSViresh Kumar 8027813dd6fSViresh Kumar _of_init_opp_table(opp_table, dev); 8037813dd6fSViresh Kumar 8047813dd6fSViresh Kumar /* Find clk for the device */ 8057813dd6fSViresh Kumar opp_table->clk = clk_get(dev, NULL); 8067813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 8077813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 8087813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) 8097813dd6fSViresh Kumar dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, 8107813dd6fSViresh Kumar ret); 8117813dd6fSViresh Kumar } 8127813dd6fSViresh Kumar 8137813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 8147813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 8157813dd6fSViresh Kumar mutex_init(&opp_table->lock); 8167813dd6fSViresh Kumar kref_init(&opp_table->kref); 8177813dd6fSViresh Kumar 8187813dd6fSViresh Kumar /* Secure the device table modification */ 8197813dd6fSViresh Kumar list_add(&opp_table->node, &opp_tables); 8207813dd6fSViresh Kumar return opp_table; 8217813dd6fSViresh Kumar } 8227813dd6fSViresh Kumar 8237813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 8247813dd6fSViresh Kumar { 8257813dd6fSViresh Kumar kref_get(&opp_table->kref); 8267813dd6fSViresh Kumar } 8277813dd6fSViresh Kumar 8287813dd6fSViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 8297813dd6fSViresh Kumar { 8307813dd6fSViresh Kumar struct opp_table *opp_table; 8317813dd6fSViresh Kumar 8327813dd6fSViresh Kumar /* Hold our table modification lock here */ 8337813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 8347813dd6fSViresh Kumar 8357813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 8367813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 8377813dd6fSViresh Kumar goto unlock; 8387813dd6fSViresh Kumar 8397813dd6fSViresh Kumar opp_table = _allocate_opp_table(dev); 8407813dd6fSViresh Kumar 8417813dd6fSViresh Kumar unlock: 8427813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 8437813dd6fSViresh Kumar 8447813dd6fSViresh Kumar return opp_table; 8457813dd6fSViresh Kumar } 8467813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 8477813dd6fSViresh Kumar 8487813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 8497813dd6fSViresh Kumar { 8507813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 8517813dd6fSViresh Kumar struct opp_device *opp_dev; 8527813dd6fSViresh Kumar 8537813dd6fSViresh Kumar /* Release clk */ 8547813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 8557813dd6fSViresh Kumar clk_put(opp_table->clk); 8567813dd6fSViresh Kumar 8577813dd6fSViresh Kumar opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device, 8587813dd6fSViresh Kumar node); 8597813dd6fSViresh Kumar 8607813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 8617813dd6fSViresh Kumar 8627813dd6fSViresh Kumar /* dev_list must be empty now */ 8637813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->dev_list)); 8647813dd6fSViresh Kumar 8657813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 8667813dd6fSViresh Kumar list_del(&opp_table->node); 8677813dd6fSViresh Kumar kfree(opp_table); 8687813dd6fSViresh Kumar 8697813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 8707813dd6fSViresh Kumar } 8717813dd6fSViresh Kumar 8727813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 8737813dd6fSViresh Kumar { 8747813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 8757813dd6fSViresh Kumar &opp_table_lock); 8767813dd6fSViresh Kumar } 8777813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 8787813dd6fSViresh Kumar 8797813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 8807813dd6fSViresh Kumar { 8817813dd6fSViresh Kumar kfree(opp); 8827813dd6fSViresh Kumar } 8837813dd6fSViresh Kumar 8847813dd6fSViresh Kumar static void _opp_kref_release(struct kref *kref) 8857813dd6fSViresh Kumar { 8867813dd6fSViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 8877813dd6fSViresh Kumar struct opp_table *opp_table = opp->opp_table; 8887813dd6fSViresh Kumar 8897813dd6fSViresh Kumar /* 8907813dd6fSViresh Kumar * Notify the changes in the availability of the operable 8917813dd6fSViresh Kumar * frequency/voltage list. 8927813dd6fSViresh Kumar */ 8937813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 8947813dd6fSViresh Kumar opp_debug_remove_one(opp); 8957813dd6fSViresh Kumar list_del(&opp->node); 8967813dd6fSViresh Kumar kfree(opp); 8977813dd6fSViresh Kumar 8987813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 8997813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 9007813dd6fSViresh Kumar } 9017813dd6fSViresh Kumar 902a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 9037813dd6fSViresh Kumar { 9047813dd6fSViresh Kumar kref_get(&opp->kref); 9057813dd6fSViresh Kumar } 9067813dd6fSViresh Kumar 9077813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 9087813dd6fSViresh Kumar { 9097813dd6fSViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 9107813dd6fSViresh Kumar } 9117813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 9127813dd6fSViresh Kumar 9137813dd6fSViresh Kumar /** 9147813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 9157813dd6fSViresh Kumar * @dev: device for which we do this operation 9167813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 9177813dd6fSViresh Kumar * 9187813dd6fSViresh Kumar * This function removes an opp from the opp table. 9197813dd6fSViresh Kumar */ 9207813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 9217813dd6fSViresh Kumar { 9227813dd6fSViresh Kumar struct dev_pm_opp *opp; 9237813dd6fSViresh Kumar struct opp_table *opp_table; 9247813dd6fSViresh Kumar bool found = false; 9257813dd6fSViresh Kumar 9267813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 9277813dd6fSViresh Kumar if (IS_ERR(opp_table)) 9287813dd6fSViresh Kumar return; 9297813dd6fSViresh Kumar 9307813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 9317813dd6fSViresh Kumar 9327813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 9337813dd6fSViresh Kumar if (opp->rate == freq) { 9347813dd6fSViresh Kumar found = true; 9357813dd6fSViresh Kumar break; 9367813dd6fSViresh Kumar } 9377813dd6fSViresh Kumar } 9387813dd6fSViresh Kumar 9397813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 9407813dd6fSViresh Kumar 9417813dd6fSViresh Kumar if (found) { 9427813dd6fSViresh Kumar dev_pm_opp_put(opp); 9437813dd6fSViresh Kumar } else { 9447813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 9457813dd6fSViresh Kumar __func__, freq); 9467813dd6fSViresh Kumar } 9477813dd6fSViresh Kumar 9487813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 9497813dd6fSViresh Kumar } 9507813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 9517813dd6fSViresh Kumar 9527813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table) 9537813dd6fSViresh Kumar { 9547813dd6fSViresh Kumar struct dev_pm_opp *opp; 9557813dd6fSViresh Kumar int count, supply_size; 9567813dd6fSViresh Kumar 9577813dd6fSViresh Kumar /* Allocate space for at least one supply */ 9587813dd6fSViresh Kumar count = table->regulator_count ? table->regulator_count : 1; 9597813dd6fSViresh Kumar supply_size = sizeof(*opp->supplies) * count; 9607813dd6fSViresh Kumar 9617813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 9627813dd6fSViresh Kumar opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); 9637813dd6fSViresh Kumar if (!opp) 9647813dd6fSViresh Kumar return NULL; 9657813dd6fSViresh Kumar 9667813dd6fSViresh Kumar /* Put the supplies at the end of the OPP structure as an empty array */ 9677813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 9687813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 9697813dd6fSViresh Kumar 9707813dd6fSViresh Kumar return opp; 9717813dd6fSViresh Kumar } 9727813dd6fSViresh Kumar 9737813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 9747813dd6fSViresh Kumar struct opp_table *opp_table) 9757813dd6fSViresh Kumar { 9767813dd6fSViresh Kumar struct regulator *reg; 9777813dd6fSViresh Kumar int i; 9787813dd6fSViresh Kumar 9797813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 9807813dd6fSViresh Kumar reg = opp_table->regulators[i]; 9817813dd6fSViresh Kumar 9827813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 9837813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 9847813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 9857813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 9867813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 9877813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 9887813dd6fSViresh Kumar return false; 9897813dd6fSViresh Kumar } 9907813dd6fSViresh Kumar } 9917813dd6fSViresh Kumar 9927813dd6fSViresh Kumar return true; 9937813dd6fSViresh Kumar } 9947813dd6fSViresh Kumar 995a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 996a1e8c136SViresh Kumar struct opp_table *opp_table, 997a1e8c136SViresh Kumar struct list_head **head) 998a1e8c136SViresh Kumar { 999a1e8c136SViresh Kumar struct dev_pm_opp *opp; 1000a1e8c136SViresh Kumar 1001a1e8c136SViresh Kumar /* 1002a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1003a1e8c136SViresh Kumar * already present. 1004a1e8c136SViresh Kumar * 1005a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1006a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1007a1e8c136SViresh Kumar * loop. 1008a1e8c136SViresh Kumar */ 1009a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 1010a1e8c136SViresh Kumar if (new_opp->rate > opp->rate) { 1011a1e8c136SViresh Kumar *head = &opp->node; 1012a1e8c136SViresh Kumar continue; 1013a1e8c136SViresh Kumar } 1014a1e8c136SViresh Kumar 1015a1e8c136SViresh Kumar if (new_opp->rate < opp->rate) 1016a1e8c136SViresh Kumar return 0; 1017a1e8c136SViresh Kumar 1018a1e8c136SViresh Kumar /* Duplicate OPPs */ 1019a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1020a1e8c136SViresh Kumar __func__, opp->rate, opp->supplies[0].u_volt, 1021a1e8c136SViresh Kumar opp->available, new_opp->rate, 1022a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1023a1e8c136SViresh Kumar 1024a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1025a1e8c136SViresh Kumar return opp->available && 1026a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1027a1e8c136SViresh Kumar } 1028a1e8c136SViresh Kumar 1029a1e8c136SViresh Kumar return 0; 1030a1e8c136SViresh Kumar } 1031a1e8c136SViresh Kumar 10327813dd6fSViresh Kumar /* 10337813dd6fSViresh Kumar * Returns: 10347813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 10357813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 10367813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 10377813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 10387813dd6fSViresh Kumar * kernel try to initialize the OPP table. 10397813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 10407813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 10417813dd6fSViresh Kumar */ 10427813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1043a1e8c136SViresh Kumar struct opp_table *opp_table, bool rate_not_available) 10447813dd6fSViresh Kumar { 10457813dd6fSViresh Kumar struct list_head *head; 10467813dd6fSViresh Kumar int ret; 10477813dd6fSViresh Kumar 10487813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 10497813dd6fSViresh Kumar head = &opp_table->opp_list; 10507813dd6fSViresh Kumar 1051a1e8c136SViresh Kumar if (likely(!rate_not_available)) { 1052a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1053a1e8c136SViresh Kumar if (ret) { 10547813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 10557813dd6fSViresh Kumar return ret; 10567813dd6fSViresh Kumar } 1057a1e8c136SViresh Kumar } 10587813dd6fSViresh Kumar 10597813dd6fSViresh Kumar list_add(&new_opp->node, head); 10607813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 10617813dd6fSViresh Kumar 10627813dd6fSViresh Kumar new_opp->opp_table = opp_table; 10637813dd6fSViresh Kumar kref_init(&new_opp->kref); 10647813dd6fSViresh Kumar 10657813dd6fSViresh Kumar /* Get a reference to the OPP table */ 10667813dd6fSViresh Kumar _get_opp_table_kref(opp_table); 10677813dd6fSViresh Kumar 10687813dd6fSViresh Kumar ret = opp_debug_create_one(new_opp, opp_table); 10697813dd6fSViresh Kumar if (ret) 10707813dd6fSViresh Kumar dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n", 10717813dd6fSViresh Kumar __func__, ret); 10727813dd6fSViresh Kumar 10737813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 10747813dd6fSViresh Kumar new_opp->available = false; 10757813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 10767813dd6fSViresh Kumar __func__, new_opp->rate); 10777813dd6fSViresh Kumar } 10787813dd6fSViresh Kumar 10797813dd6fSViresh Kumar return 0; 10807813dd6fSViresh Kumar } 10817813dd6fSViresh Kumar 10827813dd6fSViresh Kumar /** 10837813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 10847813dd6fSViresh Kumar * @opp_table: OPP table 10857813dd6fSViresh Kumar * @dev: device for which we do this operation 10867813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 10877813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 10887813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 10897813dd6fSViresh Kumar * 10907813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 10917813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 10927813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 10937813dd6fSViresh Kumar * 10947813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 10957813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 10967813dd6fSViresh Kumar * 10977813dd6fSViresh Kumar * Return: 10987813dd6fSViresh Kumar * 0 On success OR 10997813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 11007813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 11017813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 11027813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 11037813dd6fSViresh Kumar */ 11047813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 11057813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 11067813dd6fSViresh Kumar { 11077813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 11087813dd6fSViresh Kumar unsigned long tol; 11097813dd6fSViresh Kumar int ret; 11107813dd6fSViresh Kumar 11117813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 11127813dd6fSViresh Kumar if (!new_opp) 11137813dd6fSViresh Kumar return -ENOMEM; 11147813dd6fSViresh Kumar 11157813dd6fSViresh Kumar /* populate the opp table */ 11167813dd6fSViresh Kumar new_opp->rate = freq; 11177813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 11187813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 11197813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 11207813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 11217813dd6fSViresh Kumar new_opp->available = true; 11227813dd6fSViresh Kumar new_opp->dynamic = dynamic; 11237813dd6fSViresh Kumar 1124a1e8c136SViresh Kumar ret = _opp_add(dev, new_opp, opp_table, false); 11257813dd6fSViresh Kumar if (ret) { 11267813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 11277813dd6fSViresh Kumar if (ret == -EBUSY) 11287813dd6fSViresh Kumar ret = 0; 11297813dd6fSViresh Kumar goto free_opp; 11307813dd6fSViresh Kumar } 11317813dd6fSViresh Kumar 11327813dd6fSViresh Kumar /* 11337813dd6fSViresh Kumar * Notify the changes in the availability of the operable 11347813dd6fSViresh Kumar * frequency/voltage list. 11357813dd6fSViresh Kumar */ 11367813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 11377813dd6fSViresh Kumar return 0; 11387813dd6fSViresh Kumar 11397813dd6fSViresh Kumar free_opp: 11407813dd6fSViresh Kumar _opp_free(new_opp); 11417813dd6fSViresh Kumar 11427813dd6fSViresh Kumar return ret; 11437813dd6fSViresh Kumar } 11447813dd6fSViresh Kumar 11457813dd6fSViresh Kumar /** 11467813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw() - Set supported platforms 11477813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 11487813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 11497813dd6fSViresh Kumar * @count: Number of elements in the array. 11507813dd6fSViresh Kumar * 11517813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 11527813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 11537813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 11547813dd6fSViresh Kumar * property. 11557813dd6fSViresh Kumar */ 11567813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, 11577813dd6fSViresh Kumar const u32 *versions, unsigned int count) 11587813dd6fSViresh Kumar { 11597813dd6fSViresh Kumar struct opp_table *opp_table; 11607813dd6fSViresh Kumar 11617813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 11627813dd6fSViresh Kumar if (!opp_table) 11637813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 11647813dd6fSViresh Kumar 11657813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 11667813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 11677813dd6fSViresh Kumar 1168*25419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 1169*25419de1SViresh Kumar if (opp_table->supported_hw) 1170*25419de1SViresh Kumar return opp_table; 11717813dd6fSViresh Kumar 11727813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 11737813dd6fSViresh Kumar GFP_KERNEL); 11747813dd6fSViresh Kumar if (!opp_table->supported_hw) { 1175*25419de1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1176*25419de1SViresh Kumar return ERR_PTR(-ENOMEM); 11777813dd6fSViresh Kumar } 11787813dd6fSViresh Kumar 11797813dd6fSViresh Kumar opp_table->supported_hw_count = count; 11807813dd6fSViresh Kumar 11817813dd6fSViresh Kumar return opp_table; 11827813dd6fSViresh Kumar } 11837813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); 11847813dd6fSViresh Kumar 11857813dd6fSViresh Kumar /** 11867813dd6fSViresh Kumar * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw 11877813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). 11887813dd6fSViresh Kumar * 11897813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 11907813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure 11917813dd6fSViresh Kumar * will not be freed. 11927813dd6fSViresh Kumar */ 11937813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) 11947813dd6fSViresh Kumar { 11957813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 11967813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 11977813dd6fSViresh Kumar 11987813dd6fSViresh Kumar kfree(opp_table->supported_hw); 11997813dd6fSViresh Kumar opp_table->supported_hw = NULL; 12007813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 12017813dd6fSViresh Kumar 12027813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 12037813dd6fSViresh Kumar } 12047813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); 12057813dd6fSViresh Kumar 12067813dd6fSViresh Kumar /** 12077813dd6fSViresh Kumar * dev_pm_opp_set_prop_name() - Set prop-extn name 12087813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 12097813dd6fSViresh Kumar * @name: name to postfix to properties. 12107813dd6fSViresh Kumar * 12117813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 12127813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 12137813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 12147813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 12157813dd6fSViresh Kumar */ 12167813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 12177813dd6fSViresh Kumar { 12187813dd6fSViresh Kumar struct opp_table *opp_table; 12197813dd6fSViresh Kumar int ret; 12207813dd6fSViresh Kumar 12217813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 12227813dd6fSViresh Kumar if (!opp_table) 12237813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 12247813dd6fSViresh Kumar 12257813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 12267813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 12277813dd6fSViresh Kumar 12287813dd6fSViresh Kumar /* Do we already have a prop-name associated with opp_table? */ 12297813dd6fSViresh Kumar if (opp_table->prop_name) { 12307813dd6fSViresh Kumar dev_err(dev, "%s: Already have prop-name %s\n", __func__, 12317813dd6fSViresh Kumar opp_table->prop_name); 12327813dd6fSViresh Kumar ret = -EBUSY; 12337813dd6fSViresh Kumar goto err; 12347813dd6fSViresh Kumar } 12357813dd6fSViresh Kumar 12367813dd6fSViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 12377813dd6fSViresh Kumar if (!opp_table->prop_name) { 12387813dd6fSViresh Kumar ret = -ENOMEM; 12397813dd6fSViresh Kumar goto err; 12407813dd6fSViresh Kumar } 12417813dd6fSViresh Kumar 12427813dd6fSViresh Kumar return opp_table; 12437813dd6fSViresh Kumar 12447813dd6fSViresh Kumar err: 12457813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 12467813dd6fSViresh Kumar 12477813dd6fSViresh Kumar return ERR_PTR(ret); 12487813dd6fSViresh Kumar } 12497813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); 12507813dd6fSViresh Kumar 12517813dd6fSViresh Kumar /** 12527813dd6fSViresh Kumar * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name 12537813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). 12547813dd6fSViresh Kumar * 12557813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 12567813dd6fSViresh Kumar * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure 12577813dd6fSViresh Kumar * will not be freed. 12587813dd6fSViresh Kumar */ 12597813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table) 12607813dd6fSViresh Kumar { 12617813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 12627813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 12637813dd6fSViresh Kumar 12647813dd6fSViresh Kumar if (!opp_table->prop_name) { 12657813dd6fSViresh Kumar pr_err("%s: Doesn't have a prop-name\n", __func__); 12667813dd6fSViresh Kumar return; 12677813dd6fSViresh Kumar } 12687813dd6fSViresh Kumar 12697813dd6fSViresh Kumar kfree(opp_table->prop_name); 12707813dd6fSViresh Kumar opp_table->prop_name = NULL; 12717813dd6fSViresh Kumar 12727813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 12737813dd6fSViresh Kumar } 12747813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); 12757813dd6fSViresh Kumar 12767813dd6fSViresh Kumar static int _allocate_set_opp_data(struct opp_table *opp_table) 12777813dd6fSViresh Kumar { 12787813dd6fSViresh Kumar struct dev_pm_set_opp_data *data; 12797813dd6fSViresh Kumar int len, count = opp_table->regulator_count; 12807813dd6fSViresh Kumar 12817813dd6fSViresh Kumar if (WARN_ON(!count)) 12827813dd6fSViresh Kumar return -EINVAL; 12837813dd6fSViresh Kumar 12847813dd6fSViresh Kumar /* space for set_opp_data */ 12857813dd6fSViresh Kumar len = sizeof(*data); 12867813dd6fSViresh Kumar 12877813dd6fSViresh Kumar /* space for old_opp.supplies and new_opp.supplies */ 12887813dd6fSViresh Kumar len += 2 * sizeof(struct dev_pm_opp_supply) * count; 12897813dd6fSViresh Kumar 12907813dd6fSViresh Kumar data = kzalloc(len, GFP_KERNEL); 12917813dd6fSViresh Kumar if (!data) 12927813dd6fSViresh Kumar return -ENOMEM; 12937813dd6fSViresh Kumar 12947813dd6fSViresh Kumar data->old_opp.supplies = (void *)(data + 1); 12957813dd6fSViresh Kumar data->new_opp.supplies = data->old_opp.supplies + count; 12967813dd6fSViresh Kumar 12977813dd6fSViresh Kumar opp_table->set_opp_data = data; 12987813dd6fSViresh Kumar 12997813dd6fSViresh Kumar return 0; 13007813dd6fSViresh Kumar } 13017813dd6fSViresh Kumar 13027813dd6fSViresh Kumar static void _free_set_opp_data(struct opp_table *opp_table) 13037813dd6fSViresh Kumar { 13047813dd6fSViresh Kumar kfree(opp_table->set_opp_data); 13057813dd6fSViresh Kumar opp_table->set_opp_data = NULL; 13067813dd6fSViresh Kumar } 13077813dd6fSViresh Kumar 13087813dd6fSViresh Kumar /** 13097813dd6fSViresh Kumar * dev_pm_opp_set_regulators() - Set regulator names for the device 13107813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 13117813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 13127813dd6fSViresh Kumar * @count: Number of regulators. 13137813dd6fSViresh Kumar * 13147813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 13157813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 13167813dd6fSViresh Kumar * well. 13177813dd6fSViresh Kumar * 13187813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 13197813dd6fSViresh Kumar */ 13207813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev, 13217813dd6fSViresh Kumar const char * const names[], 13227813dd6fSViresh Kumar unsigned int count) 13237813dd6fSViresh Kumar { 13247813dd6fSViresh Kumar struct opp_table *opp_table; 13257813dd6fSViresh Kumar struct regulator *reg; 13267813dd6fSViresh Kumar int ret, i; 13277813dd6fSViresh Kumar 13287813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 13297813dd6fSViresh Kumar if (!opp_table) 13307813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 13317813dd6fSViresh Kumar 13327813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 13337813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 13347813dd6fSViresh Kumar ret = -EBUSY; 13357813dd6fSViresh Kumar goto err; 13367813dd6fSViresh Kumar } 13377813dd6fSViresh Kumar 13387813dd6fSViresh Kumar /* Already have regulators set */ 13397813dd6fSViresh Kumar if (opp_table->regulators) { 13407813dd6fSViresh Kumar ret = -EBUSY; 13417813dd6fSViresh Kumar goto err; 13427813dd6fSViresh Kumar } 13437813dd6fSViresh Kumar 13447813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 13457813dd6fSViresh Kumar sizeof(*opp_table->regulators), 13467813dd6fSViresh Kumar GFP_KERNEL); 13477813dd6fSViresh Kumar if (!opp_table->regulators) { 13487813dd6fSViresh Kumar ret = -ENOMEM; 13497813dd6fSViresh Kumar goto err; 13507813dd6fSViresh Kumar } 13517813dd6fSViresh Kumar 13527813dd6fSViresh Kumar for (i = 0; i < count; i++) { 13537813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 13547813dd6fSViresh Kumar if (IS_ERR(reg)) { 13557813dd6fSViresh Kumar ret = PTR_ERR(reg); 13567813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) 13577813dd6fSViresh Kumar dev_err(dev, "%s: no regulator (%s) found: %d\n", 13587813dd6fSViresh Kumar __func__, names[i], ret); 13597813dd6fSViresh Kumar goto free_regulators; 13607813dd6fSViresh Kumar } 13617813dd6fSViresh Kumar 13627813dd6fSViresh Kumar opp_table->regulators[i] = reg; 13637813dd6fSViresh Kumar } 13647813dd6fSViresh Kumar 13657813dd6fSViresh Kumar opp_table->regulator_count = count; 13667813dd6fSViresh Kumar 13677813dd6fSViresh Kumar /* Allocate block only once to pass to set_opp() routines */ 13687813dd6fSViresh Kumar ret = _allocate_set_opp_data(opp_table); 13697813dd6fSViresh Kumar if (ret) 13707813dd6fSViresh Kumar goto free_regulators; 13717813dd6fSViresh Kumar 13727813dd6fSViresh Kumar return opp_table; 13737813dd6fSViresh Kumar 13747813dd6fSViresh Kumar free_regulators: 13757813dd6fSViresh Kumar while (i != 0) 13767813dd6fSViresh Kumar regulator_put(opp_table->regulators[--i]); 13777813dd6fSViresh Kumar 13787813dd6fSViresh Kumar kfree(opp_table->regulators); 13797813dd6fSViresh Kumar opp_table->regulators = NULL; 13807813dd6fSViresh Kumar opp_table->regulator_count = 0; 13817813dd6fSViresh Kumar err: 13827813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 13837813dd6fSViresh Kumar 13847813dd6fSViresh Kumar return ERR_PTR(ret); 13857813dd6fSViresh Kumar } 13867813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); 13877813dd6fSViresh Kumar 13887813dd6fSViresh Kumar /** 13897813dd6fSViresh Kumar * dev_pm_opp_put_regulators() - Releases resources blocked for regulator 13907813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). 13917813dd6fSViresh Kumar */ 13927813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table) 13937813dd6fSViresh Kumar { 13947813dd6fSViresh Kumar int i; 13957813dd6fSViresh Kumar 13967813dd6fSViresh Kumar if (!opp_table->regulators) { 13977813dd6fSViresh Kumar pr_err("%s: Doesn't have regulators set\n", __func__); 13987813dd6fSViresh Kumar return; 13997813dd6fSViresh Kumar } 14007813dd6fSViresh Kumar 14017813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 14027813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 14037813dd6fSViresh Kumar 14047813dd6fSViresh Kumar for (i = opp_table->regulator_count - 1; i >= 0; i--) 14057813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 14067813dd6fSViresh Kumar 14077813dd6fSViresh Kumar _free_set_opp_data(opp_table); 14087813dd6fSViresh Kumar 14097813dd6fSViresh Kumar kfree(opp_table->regulators); 14107813dd6fSViresh Kumar opp_table->regulators = NULL; 14117813dd6fSViresh Kumar opp_table->regulator_count = 0; 14127813dd6fSViresh Kumar 14137813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 14147813dd6fSViresh Kumar } 14157813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); 14167813dd6fSViresh Kumar 14177813dd6fSViresh Kumar /** 14187813dd6fSViresh Kumar * dev_pm_opp_set_clkname() - Set clk name for the device 14197813dd6fSViresh Kumar * @dev: Device for which clk name is being set. 14207813dd6fSViresh Kumar * @name: Clk name. 14217813dd6fSViresh Kumar * 14227813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to get pointer to the 14237813dd6fSViresh Kumar * clock for the device. Simple cases work fine without using this routine (i.e. 14247813dd6fSViresh Kumar * by passing connection-id as NULL), but for a device with multiple clocks 14257813dd6fSViresh Kumar * available, the OPP core needs to know the exact name of the clk to use. 14267813dd6fSViresh Kumar * 14277813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 14287813dd6fSViresh Kumar */ 14297813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) 14307813dd6fSViresh Kumar { 14317813dd6fSViresh Kumar struct opp_table *opp_table; 14327813dd6fSViresh Kumar int ret; 14337813dd6fSViresh Kumar 14347813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 14357813dd6fSViresh Kumar if (!opp_table) 14367813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 14377813dd6fSViresh Kumar 14387813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 14397813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 14407813dd6fSViresh Kumar ret = -EBUSY; 14417813dd6fSViresh Kumar goto err; 14427813dd6fSViresh Kumar } 14437813dd6fSViresh Kumar 14447813dd6fSViresh Kumar /* Already have default clk set, free it */ 14457813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 14467813dd6fSViresh Kumar clk_put(opp_table->clk); 14477813dd6fSViresh Kumar 14487813dd6fSViresh Kumar /* Find clk for the device */ 14497813dd6fSViresh Kumar opp_table->clk = clk_get(dev, name); 14507813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 14517813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 14527813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) { 14537813dd6fSViresh Kumar dev_err(dev, "%s: Couldn't find clock: %d\n", __func__, 14547813dd6fSViresh Kumar ret); 14557813dd6fSViresh Kumar } 14567813dd6fSViresh Kumar goto err; 14577813dd6fSViresh Kumar } 14587813dd6fSViresh Kumar 14597813dd6fSViresh Kumar return opp_table; 14607813dd6fSViresh Kumar 14617813dd6fSViresh Kumar err: 14627813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 14637813dd6fSViresh Kumar 14647813dd6fSViresh Kumar return ERR_PTR(ret); 14657813dd6fSViresh Kumar } 14667813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); 14677813dd6fSViresh Kumar 14687813dd6fSViresh Kumar /** 14697813dd6fSViresh Kumar * dev_pm_opp_put_clkname() - Releases resources blocked for clk. 14707813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). 14717813dd6fSViresh Kumar */ 14727813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table) 14737813dd6fSViresh Kumar { 14747813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 14757813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 14767813dd6fSViresh Kumar 14777813dd6fSViresh Kumar clk_put(opp_table->clk); 14787813dd6fSViresh Kumar opp_table->clk = ERR_PTR(-EINVAL); 14797813dd6fSViresh Kumar 14807813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 14817813dd6fSViresh Kumar } 14827813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); 14837813dd6fSViresh Kumar 14847813dd6fSViresh Kumar /** 14857813dd6fSViresh Kumar * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper 14867813dd6fSViresh Kumar * @dev: Device for which the helper is getting registered. 14877813dd6fSViresh Kumar * @set_opp: Custom set OPP helper. 14887813dd6fSViresh Kumar * 14897813dd6fSViresh Kumar * This is useful to support complex platforms (like platforms with multiple 14907813dd6fSViresh Kumar * regulators per device), instead of the generic OPP set rate helper. 14917813dd6fSViresh Kumar * 14927813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 14937813dd6fSViresh Kumar */ 14947813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 14957813dd6fSViresh Kumar int (*set_opp)(struct dev_pm_set_opp_data *data)) 14967813dd6fSViresh Kumar { 14977813dd6fSViresh Kumar struct opp_table *opp_table; 14987813dd6fSViresh Kumar int ret; 14997813dd6fSViresh Kumar 15007813dd6fSViresh Kumar if (!set_opp) 15017813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 15027813dd6fSViresh Kumar 15037813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 15047813dd6fSViresh Kumar if (!opp_table) 15057813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 15067813dd6fSViresh Kumar 15077813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 15087813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 15097813dd6fSViresh Kumar ret = -EBUSY; 15107813dd6fSViresh Kumar goto err; 15117813dd6fSViresh Kumar } 15127813dd6fSViresh Kumar 15137813dd6fSViresh Kumar /* Already have custom set_opp helper */ 15147813dd6fSViresh Kumar if (WARN_ON(opp_table->set_opp)) { 15157813dd6fSViresh Kumar ret = -EBUSY; 15167813dd6fSViresh Kumar goto err; 15177813dd6fSViresh Kumar } 15187813dd6fSViresh Kumar 15197813dd6fSViresh Kumar opp_table->set_opp = set_opp; 15207813dd6fSViresh Kumar 15217813dd6fSViresh Kumar return opp_table; 15227813dd6fSViresh Kumar 15237813dd6fSViresh Kumar err: 15247813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15257813dd6fSViresh Kumar 15267813dd6fSViresh Kumar return ERR_PTR(ret); 15277813dd6fSViresh Kumar } 15287813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); 15297813dd6fSViresh Kumar 15307813dd6fSViresh Kumar /** 1531604a7aebSViresh Kumar * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for 15327813dd6fSViresh Kumar * set_opp helper 15337813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). 15347813dd6fSViresh Kumar * 15357813dd6fSViresh Kumar * Release resources blocked for platform specific set_opp helper. 15367813dd6fSViresh Kumar */ 1537604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) 15387813dd6fSViresh Kumar { 15397813dd6fSViresh Kumar if (!opp_table->set_opp) { 15407813dd6fSViresh Kumar pr_err("%s: Doesn't have custom set_opp helper set\n", 15417813dd6fSViresh Kumar __func__); 15427813dd6fSViresh Kumar return; 15437813dd6fSViresh Kumar } 15447813dd6fSViresh Kumar 15457813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 15467813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 15477813dd6fSViresh Kumar 15487813dd6fSViresh Kumar opp_table->set_opp = NULL; 15497813dd6fSViresh Kumar 15507813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15517813dd6fSViresh Kumar } 1552604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); 15537813dd6fSViresh Kumar 15547813dd6fSViresh Kumar /** 15557813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 15567813dd6fSViresh Kumar * @dev: device for which we do this operation 15577813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 15587813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 15597813dd6fSViresh Kumar * 15607813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 15617813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 15627813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 15637813dd6fSViresh Kumar * 15647813dd6fSViresh Kumar * Return: 15657813dd6fSViresh Kumar * 0 On success OR 15667813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 15677813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 15687813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 15697813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 15707813dd6fSViresh Kumar */ 15717813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 15727813dd6fSViresh Kumar { 15737813dd6fSViresh Kumar struct opp_table *opp_table; 15747813dd6fSViresh Kumar int ret; 15757813dd6fSViresh Kumar 15767813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 15777813dd6fSViresh Kumar if (!opp_table) 15787813dd6fSViresh Kumar return -ENOMEM; 15797813dd6fSViresh Kumar 15807813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 15817813dd6fSViresh Kumar 15827813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15837813dd6fSViresh Kumar return ret; 15847813dd6fSViresh Kumar } 15857813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 15867813dd6fSViresh Kumar 15877813dd6fSViresh Kumar /** 15887813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 15897813dd6fSViresh Kumar * @dev: device for which we do this operation 15907813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 15917813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 15927813dd6fSViresh Kumar * 15937813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 15947813dd6fSViresh Kumar * which is isolated here. 15957813dd6fSViresh Kumar * 15967813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 15977813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 15987813dd6fSViresh Kumar * successful. 15997813dd6fSViresh Kumar */ 16007813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 16017813dd6fSViresh Kumar bool availability_req) 16027813dd6fSViresh Kumar { 16037813dd6fSViresh Kumar struct opp_table *opp_table; 16047813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 16057813dd6fSViresh Kumar int r = 0; 16067813dd6fSViresh Kumar 16077813dd6fSViresh Kumar /* Find the opp_table */ 16087813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 16097813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 16107813dd6fSViresh Kumar r = PTR_ERR(opp_table); 16117813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 16127813dd6fSViresh Kumar return r; 16137813dd6fSViresh Kumar } 16147813dd6fSViresh Kumar 16157813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 16167813dd6fSViresh Kumar 16177813dd6fSViresh Kumar /* Do we have the frequency? */ 16187813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 16197813dd6fSViresh Kumar if (tmp_opp->rate == freq) { 16207813dd6fSViresh Kumar opp = tmp_opp; 16217813dd6fSViresh Kumar break; 16227813dd6fSViresh Kumar } 16237813dd6fSViresh Kumar } 16247813dd6fSViresh Kumar 16257813dd6fSViresh Kumar if (IS_ERR(opp)) { 16267813dd6fSViresh Kumar r = PTR_ERR(opp); 16277813dd6fSViresh Kumar goto unlock; 16287813dd6fSViresh Kumar } 16297813dd6fSViresh Kumar 16307813dd6fSViresh Kumar /* Is update really needed? */ 16317813dd6fSViresh Kumar if (opp->available == availability_req) 16327813dd6fSViresh Kumar goto unlock; 16337813dd6fSViresh Kumar 16347813dd6fSViresh Kumar opp->available = availability_req; 16357813dd6fSViresh Kumar 16367813dd6fSViresh Kumar dev_pm_opp_get(opp); 16377813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 16387813dd6fSViresh Kumar 16397813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 16407813dd6fSViresh Kumar if (availability_req) 16417813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 16427813dd6fSViresh Kumar opp); 16437813dd6fSViresh Kumar else 16447813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 16457813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 16467813dd6fSViresh Kumar 16477813dd6fSViresh Kumar dev_pm_opp_put(opp); 16487813dd6fSViresh Kumar goto put_table; 16497813dd6fSViresh Kumar 16507813dd6fSViresh Kumar unlock: 16517813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 16527813dd6fSViresh Kumar put_table: 16537813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16547813dd6fSViresh Kumar return r; 16557813dd6fSViresh Kumar } 16567813dd6fSViresh Kumar 16577813dd6fSViresh Kumar /** 16587813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 16597813dd6fSViresh Kumar * @dev: device for which we do this operation 16607813dd6fSViresh Kumar * @freq: OPP frequency to enable 16617813dd6fSViresh Kumar * 16627813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 16637813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 16647813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 16657813dd6fSViresh Kumar * 16667813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 16677813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 16687813dd6fSViresh Kumar * successful. 16697813dd6fSViresh Kumar */ 16707813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 16717813dd6fSViresh Kumar { 16727813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 16737813dd6fSViresh Kumar } 16747813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 16757813dd6fSViresh Kumar 16767813dd6fSViresh Kumar /** 16777813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 16787813dd6fSViresh Kumar * @dev: device for which we do this operation 16797813dd6fSViresh Kumar * @freq: OPP frequency to disable 16807813dd6fSViresh Kumar * 16817813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 16827813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 16837813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 16847813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 16857813dd6fSViresh Kumar * 16867813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 16877813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 16887813dd6fSViresh Kumar * successful. 16897813dd6fSViresh Kumar */ 16907813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 16917813dd6fSViresh Kumar { 16927813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 16937813dd6fSViresh Kumar } 16947813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 16957813dd6fSViresh Kumar 16967813dd6fSViresh Kumar /** 16977813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 16987813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 16997813dd6fSViresh Kumar * @nb: Notifier block to be registered 17007813dd6fSViresh Kumar * 17017813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 17027813dd6fSViresh Kumar */ 17037813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 17047813dd6fSViresh Kumar { 17057813dd6fSViresh Kumar struct opp_table *opp_table; 17067813dd6fSViresh Kumar int ret; 17077813dd6fSViresh Kumar 17087813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 17097813dd6fSViresh Kumar if (IS_ERR(opp_table)) 17107813dd6fSViresh Kumar return PTR_ERR(opp_table); 17117813dd6fSViresh Kumar 17127813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 17137813dd6fSViresh Kumar 17147813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17157813dd6fSViresh Kumar 17167813dd6fSViresh Kumar return ret; 17177813dd6fSViresh Kumar } 17187813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 17197813dd6fSViresh Kumar 17207813dd6fSViresh Kumar /** 17217813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 17227813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 17237813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 17247813dd6fSViresh Kumar * 17257813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 17267813dd6fSViresh Kumar */ 17277813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 17287813dd6fSViresh Kumar struct notifier_block *nb) 17297813dd6fSViresh Kumar { 17307813dd6fSViresh Kumar struct opp_table *opp_table; 17317813dd6fSViresh Kumar int ret; 17327813dd6fSViresh Kumar 17337813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 17347813dd6fSViresh Kumar if (IS_ERR(opp_table)) 17357813dd6fSViresh Kumar return PTR_ERR(opp_table); 17367813dd6fSViresh Kumar 17377813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 17387813dd6fSViresh Kumar 17397813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17407813dd6fSViresh Kumar 17417813dd6fSViresh Kumar return ret; 17427813dd6fSViresh Kumar } 17437813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 17447813dd6fSViresh Kumar 17457813dd6fSViresh Kumar /* 17467813dd6fSViresh Kumar * Free OPPs either created using static entries present in DT or even the 17477813dd6fSViresh Kumar * dynamically added entries based on remove_all param. 17487813dd6fSViresh Kumar */ 17497813dd6fSViresh Kumar void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, 17507813dd6fSViresh Kumar bool remove_all) 17517813dd6fSViresh Kumar { 17527813dd6fSViresh Kumar struct dev_pm_opp *opp, *tmp; 17537813dd6fSViresh Kumar 17547813dd6fSViresh Kumar /* Find if opp_table manages a single device */ 17557813dd6fSViresh Kumar if (list_is_singular(&opp_table->dev_list)) { 17567813dd6fSViresh Kumar /* Free static OPPs */ 17577813dd6fSViresh Kumar list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { 17587813dd6fSViresh Kumar if (remove_all || !opp->dynamic) 17597813dd6fSViresh Kumar dev_pm_opp_put(opp); 17607813dd6fSViresh Kumar } 1761009acd19SViresh Kumar 1762009acd19SViresh Kumar /* 1763009acd19SViresh Kumar * The OPP table is getting removed, drop the performance state 1764009acd19SViresh Kumar * constraints. 1765009acd19SViresh Kumar */ 1766009acd19SViresh Kumar if (opp_table->genpd_performance_state) 1767009acd19SViresh Kumar dev_pm_genpd_set_performance_state(dev, 0); 17687813dd6fSViresh Kumar } else { 17697813dd6fSViresh Kumar _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table); 17707813dd6fSViresh Kumar } 17717813dd6fSViresh Kumar } 17727813dd6fSViresh Kumar 17737813dd6fSViresh Kumar void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all) 17747813dd6fSViresh Kumar { 17757813dd6fSViresh Kumar struct opp_table *opp_table; 17767813dd6fSViresh Kumar 17777813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 17787813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 17797813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 17807813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 17817813dd6fSViresh Kumar 17827813dd6fSViresh Kumar if (error != -ENODEV) 17837813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 17847813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 17857813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 17867813dd6fSViresh Kumar error); 17877813dd6fSViresh Kumar return; 17887813dd6fSViresh Kumar } 17897813dd6fSViresh Kumar 17907813dd6fSViresh Kumar _dev_pm_opp_remove_table(opp_table, dev, remove_all); 17917813dd6fSViresh Kumar 17927813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17937813dd6fSViresh Kumar } 17947813dd6fSViresh Kumar 17957813dd6fSViresh Kumar /** 17967813dd6fSViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 17977813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table. 17987813dd6fSViresh Kumar * 17997813dd6fSViresh Kumar * Free both OPPs created using static entries present in DT and the 18007813dd6fSViresh Kumar * dynamically added entries. 18017813dd6fSViresh Kumar */ 18027813dd6fSViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 18037813dd6fSViresh Kumar { 18047813dd6fSViresh Kumar _dev_pm_opp_find_and_remove_table(dev, true); 18057813dd6fSViresh Kumar } 18067813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 1807