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; 513d255699SViresh Kumar bool found; 527813dd6fSViresh Kumar 537813dd6fSViresh Kumar list_for_each_entry(opp_table, &opp_tables, node) { 543d255699SViresh Kumar mutex_lock(&opp_table->lock); 553d255699SViresh Kumar found = !!_find_opp_dev(dev, opp_table); 563d255699SViresh Kumar mutex_unlock(&opp_table->lock); 573d255699SViresh Kumar 583d255699SViresh Kumar if (found) { 597813dd6fSViresh Kumar _get_opp_table_kref(opp_table); 607813dd6fSViresh Kumar 617813dd6fSViresh Kumar return opp_table; 627813dd6fSViresh Kumar } 637813dd6fSViresh Kumar } 647813dd6fSViresh Kumar 657813dd6fSViresh Kumar return ERR_PTR(-ENODEV); 667813dd6fSViresh Kumar } 677813dd6fSViresh Kumar 687813dd6fSViresh Kumar /** 697813dd6fSViresh Kumar * _find_opp_table() - find opp_table struct using device pointer 707813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table 717813dd6fSViresh Kumar * 727813dd6fSViresh Kumar * Search OPP table for one containing matching device. 737813dd6fSViresh Kumar * 747813dd6fSViresh Kumar * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or 757813dd6fSViresh Kumar * -EINVAL based on type of error. 767813dd6fSViresh Kumar * 777813dd6fSViresh Kumar * The callers must call dev_pm_opp_put_opp_table() after the table is used. 787813dd6fSViresh Kumar */ 797813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev) 807813dd6fSViresh Kumar { 817813dd6fSViresh Kumar struct opp_table *opp_table; 827813dd6fSViresh Kumar 837813dd6fSViresh Kumar if (IS_ERR_OR_NULL(dev)) { 847813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 857813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 867813dd6fSViresh Kumar } 877813dd6fSViresh Kumar 887813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 897813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 907813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 917813dd6fSViresh Kumar 927813dd6fSViresh Kumar return opp_table; 937813dd6fSViresh Kumar } 947813dd6fSViresh Kumar 957813dd6fSViresh Kumar /** 967813dd6fSViresh Kumar * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp 977813dd6fSViresh Kumar * @opp: opp for which voltage has to be returned for 987813dd6fSViresh Kumar * 997813dd6fSViresh Kumar * Return: voltage in micro volt corresponding to the opp, else 1007813dd6fSViresh Kumar * return 0 1017813dd6fSViresh Kumar * 1027813dd6fSViresh Kumar * This is useful only for devices with single power supply. 1037813dd6fSViresh Kumar */ 1047813dd6fSViresh Kumar unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 1057813dd6fSViresh Kumar { 1067813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp)) { 1077813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1087813dd6fSViresh Kumar return 0; 1097813dd6fSViresh Kumar } 1107813dd6fSViresh Kumar 1117813dd6fSViresh Kumar return opp->supplies[0].u_volt; 1127813dd6fSViresh Kumar } 1137813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); 1147813dd6fSViresh Kumar 1157813dd6fSViresh Kumar /** 1167813dd6fSViresh Kumar * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp 1177813dd6fSViresh Kumar * @opp: opp for which frequency has to be returned for 1187813dd6fSViresh Kumar * 1197813dd6fSViresh Kumar * Return: frequency in hertz corresponding to the opp, else 1207813dd6fSViresh Kumar * return 0 1217813dd6fSViresh Kumar */ 1227813dd6fSViresh Kumar unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) 1237813dd6fSViresh Kumar { 1247813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 1257813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1267813dd6fSViresh Kumar return 0; 1277813dd6fSViresh Kumar } 1287813dd6fSViresh Kumar 1297813dd6fSViresh Kumar return opp->rate; 1307813dd6fSViresh Kumar } 1317813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); 1327813dd6fSViresh Kumar 1337813dd6fSViresh Kumar /** 1347813dd6fSViresh Kumar * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not 1357813dd6fSViresh Kumar * @opp: opp for which turbo mode is being verified 1367813dd6fSViresh Kumar * 1377813dd6fSViresh Kumar * Turbo OPPs are not for normal use, and can be enabled (under certain 1387813dd6fSViresh Kumar * conditions) for short duration of times to finish high throughput work 1397813dd6fSViresh Kumar * quickly. Running on them for longer times may overheat the chip. 1407813dd6fSViresh Kumar * 1417813dd6fSViresh Kumar * Return: true if opp is turbo opp, else false. 1427813dd6fSViresh Kumar */ 1437813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 1447813dd6fSViresh Kumar { 1457813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 1467813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1477813dd6fSViresh Kumar return false; 1487813dd6fSViresh Kumar } 1497813dd6fSViresh Kumar 1507813dd6fSViresh Kumar return opp->turbo; 1517813dd6fSViresh Kumar } 1527813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 1537813dd6fSViresh Kumar 1547813dd6fSViresh Kumar /** 1557813dd6fSViresh Kumar * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds 1567813dd6fSViresh Kumar * @dev: device for which we do this operation 1577813dd6fSViresh Kumar * 1587813dd6fSViresh Kumar * Return: This function returns the max clock latency in nanoseconds. 1597813dd6fSViresh Kumar */ 1607813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 1617813dd6fSViresh Kumar { 1627813dd6fSViresh Kumar struct opp_table *opp_table; 1637813dd6fSViresh Kumar unsigned long clock_latency_ns; 1647813dd6fSViresh Kumar 1657813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 1667813dd6fSViresh Kumar if (IS_ERR(opp_table)) 1677813dd6fSViresh Kumar return 0; 1687813dd6fSViresh Kumar 1697813dd6fSViresh Kumar clock_latency_ns = opp_table->clock_latency_ns_max; 1707813dd6fSViresh Kumar 1717813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1727813dd6fSViresh Kumar 1737813dd6fSViresh Kumar return clock_latency_ns; 1747813dd6fSViresh Kumar } 1757813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 1767813dd6fSViresh Kumar 1777813dd6fSViresh Kumar /** 1787813dd6fSViresh Kumar * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 1797813dd6fSViresh Kumar * @dev: device for which we do this operation 1807813dd6fSViresh Kumar * 1817813dd6fSViresh Kumar * Return: This function returns the max voltage latency in nanoseconds. 1827813dd6fSViresh Kumar */ 1837813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 1847813dd6fSViresh Kumar { 1857813dd6fSViresh Kumar struct opp_table *opp_table; 1867813dd6fSViresh Kumar struct dev_pm_opp *opp; 1877813dd6fSViresh Kumar struct regulator *reg; 1887813dd6fSViresh Kumar unsigned long latency_ns = 0; 1897813dd6fSViresh Kumar int ret, i, count; 1907813dd6fSViresh Kumar struct { 1917813dd6fSViresh Kumar unsigned long min; 1927813dd6fSViresh Kumar unsigned long max; 1937813dd6fSViresh Kumar } *uV; 1947813dd6fSViresh Kumar 1957813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 1967813dd6fSViresh Kumar if (IS_ERR(opp_table)) 1977813dd6fSViresh Kumar return 0; 1987813dd6fSViresh Kumar 1997813dd6fSViresh Kumar /* Regulator may not be required for the device */ 200*90e3577bSViresh Kumar if (!opp_table->regulators) 2017813dd6fSViresh Kumar goto put_opp_table; 2027813dd6fSViresh Kumar 203*90e3577bSViresh Kumar count = opp_table->regulator_count; 204*90e3577bSViresh Kumar 2057813dd6fSViresh Kumar uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 2067813dd6fSViresh Kumar if (!uV) 2077813dd6fSViresh Kumar goto put_opp_table; 2087813dd6fSViresh Kumar 2097813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 2107813dd6fSViresh Kumar 2117813dd6fSViresh Kumar for (i = 0; i < count; i++) { 2127813dd6fSViresh Kumar uV[i].min = ~0; 2137813dd6fSViresh Kumar uV[i].max = 0; 2147813dd6fSViresh Kumar 2157813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 2167813dd6fSViresh Kumar if (!opp->available) 2177813dd6fSViresh Kumar continue; 2187813dd6fSViresh Kumar 2197813dd6fSViresh Kumar if (opp->supplies[i].u_volt_min < uV[i].min) 2207813dd6fSViresh Kumar uV[i].min = opp->supplies[i].u_volt_min; 2217813dd6fSViresh Kumar if (opp->supplies[i].u_volt_max > uV[i].max) 2227813dd6fSViresh Kumar uV[i].max = opp->supplies[i].u_volt_max; 2237813dd6fSViresh Kumar } 2247813dd6fSViresh Kumar } 2257813dd6fSViresh Kumar 2267813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 2277813dd6fSViresh Kumar 2287813dd6fSViresh Kumar /* 2297813dd6fSViresh Kumar * The caller needs to ensure that opp_table (and hence the regulator) 2307813dd6fSViresh Kumar * isn't freed, while we are executing this routine. 2317813dd6fSViresh Kumar */ 2327813dd6fSViresh Kumar for (i = 0; i < count; i++) { 2337813dd6fSViresh Kumar reg = opp_table->regulators[i]; 2347813dd6fSViresh Kumar ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); 2357813dd6fSViresh Kumar if (ret > 0) 2367813dd6fSViresh Kumar latency_ns += ret * 1000; 2377813dd6fSViresh Kumar } 2387813dd6fSViresh Kumar 2397813dd6fSViresh Kumar kfree(uV); 2407813dd6fSViresh Kumar put_opp_table: 2417813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2427813dd6fSViresh Kumar 2437813dd6fSViresh Kumar return latency_ns; 2447813dd6fSViresh Kumar } 2457813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); 2467813dd6fSViresh Kumar 2477813dd6fSViresh Kumar /** 2487813dd6fSViresh Kumar * dev_pm_opp_get_max_transition_latency() - Get max transition latency in 2497813dd6fSViresh Kumar * nanoseconds 2507813dd6fSViresh Kumar * @dev: device for which we do this operation 2517813dd6fSViresh Kumar * 2527813dd6fSViresh Kumar * Return: This function returns the max transition latency, in nanoseconds, to 2537813dd6fSViresh Kumar * switch from one OPP to other. 2547813dd6fSViresh Kumar */ 2557813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 2567813dd6fSViresh Kumar { 2577813dd6fSViresh Kumar return dev_pm_opp_get_max_volt_latency(dev) + 2587813dd6fSViresh Kumar dev_pm_opp_get_max_clock_latency(dev); 2597813dd6fSViresh Kumar } 2607813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 2617813dd6fSViresh Kumar 2627813dd6fSViresh Kumar /** 2637813dd6fSViresh Kumar * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 2647813dd6fSViresh Kumar * @dev: device for which we do this operation 2657813dd6fSViresh Kumar * 2667813dd6fSViresh Kumar * Return: This function returns the frequency of the OPP marked as suspend_opp 2677813dd6fSViresh Kumar * if one is available, else returns 0; 2687813dd6fSViresh Kumar */ 2697813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 2707813dd6fSViresh Kumar { 2717813dd6fSViresh Kumar struct opp_table *opp_table; 2727813dd6fSViresh Kumar unsigned long freq = 0; 2737813dd6fSViresh Kumar 2747813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2757813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2767813dd6fSViresh Kumar return 0; 2777813dd6fSViresh Kumar 2787813dd6fSViresh Kumar if (opp_table->suspend_opp && opp_table->suspend_opp->available) 2797813dd6fSViresh Kumar freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 2807813dd6fSViresh Kumar 2817813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2827813dd6fSViresh Kumar 2837813dd6fSViresh Kumar return freq; 2847813dd6fSViresh Kumar } 2857813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 2867813dd6fSViresh Kumar 287a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table) 288a1e8c136SViresh Kumar { 289a1e8c136SViresh Kumar struct dev_pm_opp *opp; 290a1e8c136SViresh Kumar int count = 0; 291a1e8c136SViresh Kumar 292a1e8c136SViresh Kumar mutex_lock(&opp_table->lock); 293a1e8c136SViresh Kumar 294a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 295a1e8c136SViresh Kumar if (opp->available) 296a1e8c136SViresh Kumar count++; 297a1e8c136SViresh Kumar } 298a1e8c136SViresh Kumar 299a1e8c136SViresh Kumar mutex_unlock(&opp_table->lock); 300a1e8c136SViresh Kumar 301a1e8c136SViresh Kumar return count; 302a1e8c136SViresh Kumar } 303a1e8c136SViresh Kumar 3047813dd6fSViresh Kumar /** 3057813dd6fSViresh Kumar * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table 3067813dd6fSViresh Kumar * @dev: device for which we do this operation 3077813dd6fSViresh Kumar * 3087813dd6fSViresh Kumar * Return: This function returns the number of available opps if there are any, 3097813dd6fSViresh Kumar * else returns 0 if none or the corresponding error value. 3107813dd6fSViresh Kumar */ 3117813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev) 3127813dd6fSViresh Kumar { 3137813dd6fSViresh Kumar struct opp_table *opp_table; 314a1e8c136SViresh Kumar int count; 3157813dd6fSViresh Kumar 3167813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3177813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 3187813dd6fSViresh Kumar count = PTR_ERR(opp_table); 319035ed072SFabio Estevam dev_dbg(dev, "%s: OPP table not found (%d)\n", 3207813dd6fSViresh Kumar __func__, count); 32109f662f9SViresh Kumar return count; 3227813dd6fSViresh Kumar } 3237813dd6fSViresh Kumar 324a1e8c136SViresh Kumar count = _get_opp_count(opp_table); 3257813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3267813dd6fSViresh Kumar 3277813dd6fSViresh Kumar return count; 3287813dd6fSViresh Kumar } 3297813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); 3307813dd6fSViresh Kumar 3317813dd6fSViresh Kumar /** 3327813dd6fSViresh Kumar * dev_pm_opp_find_freq_exact() - search for an exact frequency 3337813dd6fSViresh Kumar * @dev: device for which we do this operation 3347813dd6fSViresh Kumar * @freq: frequency to search for 3357813dd6fSViresh Kumar * @available: true/false - match for available opp 3367813dd6fSViresh Kumar * 3377813dd6fSViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 3387813dd6fSViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 3397813dd6fSViresh Kumar * be handled using IS_ERR. Error return values can be: 3407813dd6fSViresh Kumar * EINVAL: for bad pointer 3417813dd6fSViresh Kumar * ERANGE: no match found for search 3427813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 3437813dd6fSViresh Kumar * 3447813dd6fSViresh Kumar * Note: available is a modifier for the search. if available=true, then the 3457813dd6fSViresh Kumar * match is for exact matching frequency and is available in the stored OPP 3467813dd6fSViresh Kumar * table. if false, the match is for exact frequency which is not available. 3477813dd6fSViresh Kumar * 3487813dd6fSViresh Kumar * This provides a mechanism to enable an opp which is not available currently 3497813dd6fSViresh Kumar * or the opposite as well. 3507813dd6fSViresh Kumar * 3517813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 3527813dd6fSViresh Kumar * use. 3537813dd6fSViresh Kumar */ 3547813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 3557813dd6fSViresh Kumar unsigned long freq, 3567813dd6fSViresh Kumar bool available) 3577813dd6fSViresh Kumar { 3587813dd6fSViresh Kumar struct opp_table *opp_table; 3597813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 3607813dd6fSViresh Kumar 3617813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3627813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 3637813dd6fSViresh Kumar int r = PTR_ERR(opp_table); 3647813dd6fSViresh Kumar 3657813dd6fSViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 3667813dd6fSViresh Kumar return ERR_PTR(r); 3677813dd6fSViresh Kumar } 3687813dd6fSViresh Kumar 3697813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 3707813dd6fSViresh Kumar 3717813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 3727813dd6fSViresh Kumar if (temp_opp->available == available && 3737813dd6fSViresh Kumar temp_opp->rate == freq) { 3747813dd6fSViresh Kumar opp = temp_opp; 3757813dd6fSViresh Kumar 3767813dd6fSViresh Kumar /* Increment the reference count of OPP */ 3777813dd6fSViresh Kumar dev_pm_opp_get(opp); 3787813dd6fSViresh Kumar break; 3797813dd6fSViresh Kumar } 3807813dd6fSViresh Kumar } 3817813dd6fSViresh Kumar 3827813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 3837813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3847813dd6fSViresh Kumar 3857813dd6fSViresh Kumar return opp; 3867813dd6fSViresh Kumar } 3877813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 3887813dd6fSViresh Kumar 3897813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 3907813dd6fSViresh Kumar unsigned long *freq) 3917813dd6fSViresh Kumar { 3927813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 3937813dd6fSViresh Kumar 3947813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 3957813dd6fSViresh Kumar 3967813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 3977813dd6fSViresh Kumar if (temp_opp->available && temp_opp->rate >= *freq) { 3987813dd6fSViresh Kumar opp = temp_opp; 3997813dd6fSViresh Kumar *freq = opp->rate; 4007813dd6fSViresh Kumar 4017813dd6fSViresh Kumar /* Increment the reference count of OPP */ 4027813dd6fSViresh Kumar dev_pm_opp_get(opp); 4037813dd6fSViresh Kumar break; 4047813dd6fSViresh Kumar } 4057813dd6fSViresh Kumar } 4067813dd6fSViresh Kumar 4077813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 4087813dd6fSViresh Kumar 4097813dd6fSViresh Kumar return opp; 4107813dd6fSViresh Kumar } 4117813dd6fSViresh Kumar 4127813dd6fSViresh Kumar /** 4137813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 4147813dd6fSViresh Kumar * @dev: device for which we do this operation 4157813dd6fSViresh Kumar * @freq: Start frequency 4167813dd6fSViresh Kumar * 4177813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 4187813dd6fSViresh Kumar * for a device. 4197813dd6fSViresh Kumar * 4207813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 4217813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 4227813dd6fSViresh Kumar * values can be: 4237813dd6fSViresh Kumar * EINVAL: for bad pointer 4247813dd6fSViresh Kumar * ERANGE: no match found for search 4257813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 4267813dd6fSViresh Kumar * 4277813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 4287813dd6fSViresh Kumar * use. 4297813dd6fSViresh Kumar */ 4307813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 4317813dd6fSViresh Kumar unsigned long *freq) 4327813dd6fSViresh Kumar { 4337813dd6fSViresh Kumar struct opp_table *opp_table; 4347813dd6fSViresh Kumar struct dev_pm_opp *opp; 4357813dd6fSViresh Kumar 4367813dd6fSViresh Kumar if (!dev || !freq) { 4377813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 4387813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 4397813dd6fSViresh Kumar } 4407813dd6fSViresh Kumar 4417813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 4427813dd6fSViresh Kumar if (IS_ERR(opp_table)) 4437813dd6fSViresh Kumar return ERR_CAST(opp_table); 4447813dd6fSViresh Kumar 4457813dd6fSViresh Kumar opp = _find_freq_ceil(opp_table, freq); 4467813dd6fSViresh Kumar 4477813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4487813dd6fSViresh Kumar 4497813dd6fSViresh Kumar return opp; 4507813dd6fSViresh Kumar } 4517813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 4527813dd6fSViresh Kumar 4537813dd6fSViresh Kumar /** 4547813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 4557813dd6fSViresh Kumar * @dev: device for which we do this operation 4567813dd6fSViresh Kumar * @freq: Start frequency 4577813dd6fSViresh Kumar * 4587813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 4597813dd6fSViresh Kumar * for a device. 4607813dd6fSViresh Kumar * 4617813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 4627813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 4637813dd6fSViresh Kumar * values can be: 4647813dd6fSViresh Kumar * EINVAL: for bad pointer 4657813dd6fSViresh Kumar * ERANGE: no match found for search 4667813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 4677813dd6fSViresh Kumar * 4687813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 4697813dd6fSViresh Kumar * use. 4707813dd6fSViresh Kumar */ 4717813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 4727813dd6fSViresh Kumar unsigned long *freq) 4737813dd6fSViresh Kumar { 4747813dd6fSViresh Kumar struct opp_table *opp_table; 4757813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 4767813dd6fSViresh Kumar 4777813dd6fSViresh Kumar if (!dev || !freq) { 4787813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 4797813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 4807813dd6fSViresh Kumar } 4817813dd6fSViresh Kumar 4827813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 4837813dd6fSViresh Kumar if (IS_ERR(opp_table)) 4847813dd6fSViresh Kumar return ERR_CAST(opp_table); 4857813dd6fSViresh Kumar 4867813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 4877813dd6fSViresh Kumar 4887813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 4897813dd6fSViresh Kumar if (temp_opp->available) { 4907813dd6fSViresh Kumar /* go to the next node, before choosing prev */ 4917813dd6fSViresh Kumar if (temp_opp->rate > *freq) 4927813dd6fSViresh Kumar break; 4937813dd6fSViresh Kumar else 4947813dd6fSViresh Kumar opp = temp_opp; 4957813dd6fSViresh Kumar } 4967813dd6fSViresh Kumar } 4977813dd6fSViresh Kumar 4987813dd6fSViresh Kumar /* Increment the reference count of OPP */ 4997813dd6fSViresh Kumar if (!IS_ERR(opp)) 5007813dd6fSViresh Kumar dev_pm_opp_get(opp); 5017813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 5027813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 5037813dd6fSViresh Kumar 5047813dd6fSViresh Kumar if (!IS_ERR(opp)) 5057813dd6fSViresh Kumar *freq = opp->rate; 5067813dd6fSViresh Kumar 5077813dd6fSViresh Kumar return opp; 5087813dd6fSViresh Kumar } 5097813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 5107813dd6fSViresh Kumar 5117813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 5127813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 5137813dd6fSViresh Kumar { 5147813dd6fSViresh Kumar int ret; 5157813dd6fSViresh Kumar 5167813dd6fSViresh Kumar /* Regulator not available for device */ 5177813dd6fSViresh Kumar if (IS_ERR(reg)) { 5187813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 5197813dd6fSViresh Kumar PTR_ERR(reg)); 5207813dd6fSViresh Kumar return 0; 5217813dd6fSViresh Kumar } 5227813dd6fSViresh Kumar 5237813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 5247813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 5257813dd6fSViresh Kumar 5267813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 5277813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 5287813dd6fSViresh Kumar if (ret) 5297813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 5307813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 5317813dd6fSViresh Kumar supply->u_volt_max, ret); 5327813dd6fSViresh Kumar 5337813dd6fSViresh Kumar return ret; 5347813dd6fSViresh Kumar } 5357813dd6fSViresh Kumar 5367813dd6fSViresh Kumar static inline int 5377813dd6fSViresh Kumar _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 5387813dd6fSViresh Kumar unsigned long old_freq, unsigned long freq) 5397813dd6fSViresh Kumar { 5407813dd6fSViresh Kumar int ret; 5417813dd6fSViresh Kumar 5427813dd6fSViresh Kumar ret = clk_set_rate(clk, freq); 5437813dd6fSViresh Kumar if (ret) { 5447813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 5457813dd6fSViresh Kumar ret); 5467813dd6fSViresh Kumar } 5477813dd6fSViresh Kumar 5487813dd6fSViresh Kumar return ret; 5497813dd6fSViresh Kumar } 5507813dd6fSViresh Kumar 551009acd19SViresh Kumar static inline int 552009acd19SViresh Kumar _generic_set_opp_domain(struct device *dev, struct clk *clk, 553009acd19SViresh Kumar unsigned long old_freq, unsigned long freq, 554009acd19SViresh Kumar unsigned int old_pstate, unsigned int new_pstate) 555009acd19SViresh Kumar { 556009acd19SViresh Kumar int ret; 557009acd19SViresh Kumar 558009acd19SViresh Kumar /* Scaling up? Scale domain performance state before frequency */ 559009acd19SViresh Kumar if (freq > old_freq) { 560009acd19SViresh Kumar ret = dev_pm_genpd_set_performance_state(dev, new_pstate); 561009acd19SViresh Kumar if (ret) 562009acd19SViresh Kumar return ret; 563009acd19SViresh Kumar } 564009acd19SViresh Kumar 565009acd19SViresh Kumar ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq); 566009acd19SViresh Kumar if (ret) 567009acd19SViresh Kumar goto restore_domain_state; 568009acd19SViresh Kumar 569009acd19SViresh Kumar /* Scaling down? Scale domain performance state after frequency */ 570009acd19SViresh Kumar if (freq < old_freq) { 571009acd19SViresh Kumar ret = dev_pm_genpd_set_performance_state(dev, new_pstate); 572009acd19SViresh Kumar if (ret) 573009acd19SViresh Kumar goto restore_freq; 574009acd19SViresh Kumar } 575009acd19SViresh Kumar 576009acd19SViresh Kumar return 0; 577009acd19SViresh Kumar 578009acd19SViresh Kumar restore_freq: 579009acd19SViresh Kumar if (_generic_set_opp_clk_only(dev, clk, freq, old_freq)) 580009acd19SViresh Kumar dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 581009acd19SViresh Kumar __func__, old_freq); 582009acd19SViresh Kumar restore_domain_state: 583009acd19SViresh Kumar if (freq > old_freq) 584009acd19SViresh Kumar dev_pm_genpd_set_performance_state(dev, old_pstate); 585009acd19SViresh Kumar 586009acd19SViresh Kumar return ret; 587009acd19SViresh Kumar } 588009acd19SViresh Kumar 5897813dd6fSViresh Kumar static int _generic_set_opp_regulator(const struct opp_table *opp_table, 5907813dd6fSViresh Kumar struct device *dev, 5917813dd6fSViresh Kumar unsigned long old_freq, 5927813dd6fSViresh Kumar unsigned long freq, 5937813dd6fSViresh Kumar struct dev_pm_opp_supply *old_supply, 5947813dd6fSViresh Kumar struct dev_pm_opp_supply *new_supply) 5957813dd6fSViresh Kumar { 5967813dd6fSViresh Kumar struct regulator *reg = opp_table->regulators[0]; 5977813dd6fSViresh Kumar int ret; 5987813dd6fSViresh Kumar 5997813dd6fSViresh Kumar /* This function only supports single regulator per device */ 6007813dd6fSViresh Kumar if (WARN_ON(opp_table->regulator_count > 1)) { 6017813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 6027813dd6fSViresh Kumar return -EINVAL; 6037813dd6fSViresh Kumar } 6047813dd6fSViresh Kumar 6057813dd6fSViresh Kumar /* Scaling up? Scale voltage before frequency */ 606c5c2a97bSWaldemar Rymarkiewicz if (freq >= old_freq) { 6077813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 6087813dd6fSViresh Kumar if (ret) 6097813dd6fSViresh Kumar goto restore_voltage; 6107813dd6fSViresh Kumar } 6117813dd6fSViresh Kumar 6127813dd6fSViresh Kumar /* Change frequency */ 6137813dd6fSViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq); 6147813dd6fSViresh Kumar if (ret) 6157813dd6fSViresh Kumar goto restore_voltage; 6167813dd6fSViresh Kumar 6177813dd6fSViresh Kumar /* Scaling down? Scale voltage after frequency */ 6187813dd6fSViresh Kumar if (freq < old_freq) { 6197813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 6207813dd6fSViresh Kumar if (ret) 6217813dd6fSViresh Kumar goto restore_freq; 6227813dd6fSViresh Kumar } 6237813dd6fSViresh Kumar 6247813dd6fSViresh Kumar return 0; 6257813dd6fSViresh Kumar 6267813dd6fSViresh Kumar restore_freq: 6277813dd6fSViresh Kumar if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq)) 6287813dd6fSViresh Kumar dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 6297813dd6fSViresh Kumar __func__, old_freq); 6307813dd6fSViresh Kumar restore_voltage: 6317813dd6fSViresh Kumar /* This shouldn't harm even if the voltages weren't updated earlier */ 6327813dd6fSViresh Kumar if (old_supply) 6337813dd6fSViresh Kumar _set_opp_voltage(dev, reg, old_supply); 6347813dd6fSViresh Kumar 6357813dd6fSViresh Kumar return ret; 6367813dd6fSViresh Kumar } 6377813dd6fSViresh Kumar 6387813dd6fSViresh Kumar /** 6397813dd6fSViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 6407813dd6fSViresh Kumar * @dev: device for which we do this operation 6417813dd6fSViresh Kumar * @target_freq: frequency to achieve 6427813dd6fSViresh Kumar * 6437813dd6fSViresh Kumar * This configures the power-supplies and clock source to the levels specified 6447813dd6fSViresh Kumar * by the OPP corresponding to the target_freq. 6457813dd6fSViresh Kumar */ 6467813dd6fSViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 6477813dd6fSViresh Kumar { 6487813dd6fSViresh Kumar struct opp_table *opp_table; 6497813dd6fSViresh Kumar unsigned long freq, old_freq; 6507813dd6fSViresh Kumar struct dev_pm_opp *old_opp, *opp; 6517813dd6fSViresh Kumar struct clk *clk; 6527813dd6fSViresh Kumar int ret, size; 6537813dd6fSViresh Kumar 6547813dd6fSViresh Kumar if (unlikely(!target_freq)) { 6557813dd6fSViresh Kumar dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, 6567813dd6fSViresh Kumar target_freq); 6577813dd6fSViresh Kumar return -EINVAL; 6587813dd6fSViresh Kumar } 6597813dd6fSViresh Kumar 6607813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 6617813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 6627813dd6fSViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 6637813dd6fSViresh Kumar return PTR_ERR(opp_table); 6647813dd6fSViresh Kumar } 6657813dd6fSViresh Kumar 6667813dd6fSViresh Kumar clk = opp_table->clk; 6677813dd6fSViresh Kumar if (IS_ERR(clk)) { 6687813dd6fSViresh Kumar dev_err(dev, "%s: No clock available for the device\n", 6697813dd6fSViresh Kumar __func__); 6707813dd6fSViresh Kumar ret = PTR_ERR(clk); 6717813dd6fSViresh Kumar goto put_opp_table; 6727813dd6fSViresh Kumar } 6737813dd6fSViresh Kumar 6747813dd6fSViresh Kumar freq = clk_round_rate(clk, target_freq); 6757813dd6fSViresh Kumar if ((long)freq <= 0) 6767813dd6fSViresh Kumar freq = target_freq; 6777813dd6fSViresh Kumar 6787813dd6fSViresh Kumar old_freq = clk_get_rate(clk); 6797813dd6fSViresh Kumar 6807813dd6fSViresh Kumar /* Return early if nothing to do */ 6817813dd6fSViresh Kumar if (old_freq == freq) { 6827813dd6fSViresh Kumar dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", 6837813dd6fSViresh Kumar __func__, freq); 6847813dd6fSViresh Kumar ret = 0; 6857813dd6fSViresh Kumar goto put_opp_table; 6867813dd6fSViresh Kumar } 6877813dd6fSViresh Kumar 6887813dd6fSViresh Kumar old_opp = _find_freq_ceil(opp_table, &old_freq); 6897813dd6fSViresh Kumar if (IS_ERR(old_opp)) { 6907813dd6fSViresh Kumar dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", 6917813dd6fSViresh Kumar __func__, old_freq, PTR_ERR(old_opp)); 6927813dd6fSViresh Kumar } 6937813dd6fSViresh Kumar 6947813dd6fSViresh Kumar opp = _find_freq_ceil(opp_table, &freq); 6957813dd6fSViresh Kumar if (IS_ERR(opp)) { 6967813dd6fSViresh Kumar ret = PTR_ERR(opp); 6977813dd6fSViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 6987813dd6fSViresh Kumar __func__, freq, ret); 6997813dd6fSViresh Kumar goto put_old_opp; 7007813dd6fSViresh Kumar } 7017813dd6fSViresh Kumar 7027813dd6fSViresh Kumar dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, 7037813dd6fSViresh Kumar old_freq, freq); 7047813dd6fSViresh Kumar 7057813dd6fSViresh Kumar /* Only frequency scaling */ 7067813dd6fSViresh Kumar if (!opp_table->regulators) { 707009acd19SViresh Kumar /* 708009acd19SViresh Kumar * We don't support devices with both regulator and 709009acd19SViresh Kumar * domain performance-state for now. 710009acd19SViresh Kumar */ 711009acd19SViresh Kumar if (opp_table->genpd_performance_state) 712009acd19SViresh Kumar ret = _generic_set_opp_domain(dev, clk, old_freq, freq, 713009acd19SViresh Kumar IS_ERR(old_opp) ? 0 : old_opp->pstate, 714009acd19SViresh Kumar opp->pstate); 715009acd19SViresh Kumar else 7167813dd6fSViresh Kumar ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq); 7177813dd6fSViresh Kumar } else if (!opp_table->set_opp) { 7187813dd6fSViresh Kumar ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq, 7197813dd6fSViresh Kumar IS_ERR(old_opp) ? NULL : old_opp->supplies, 7207813dd6fSViresh Kumar opp->supplies); 7217813dd6fSViresh Kumar } else { 7227813dd6fSViresh Kumar struct dev_pm_set_opp_data *data; 7237813dd6fSViresh Kumar 7247813dd6fSViresh Kumar data = opp_table->set_opp_data; 7257813dd6fSViresh Kumar data->regulators = opp_table->regulators; 7267813dd6fSViresh Kumar data->regulator_count = opp_table->regulator_count; 7277813dd6fSViresh Kumar data->clk = clk; 7287813dd6fSViresh Kumar data->dev = dev; 7297813dd6fSViresh Kumar 7307813dd6fSViresh Kumar data->old_opp.rate = old_freq; 7317813dd6fSViresh Kumar size = sizeof(*opp->supplies) * opp_table->regulator_count; 7327813dd6fSViresh Kumar if (IS_ERR(old_opp)) 7337813dd6fSViresh Kumar memset(data->old_opp.supplies, 0, size); 7347813dd6fSViresh Kumar else 7357813dd6fSViresh Kumar memcpy(data->old_opp.supplies, old_opp->supplies, size); 7367813dd6fSViresh Kumar 7377813dd6fSViresh Kumar data->new_opp.rate = freq; 7387813dd6fSViresh Kumar memcpy(data->new_opp.supplies, opp->supplies, size); 7397813dd6fSViresh Kumar 7407813dd6fSViresh Kumar ret = opp_table->set_opp(data); 7417813dd6fSViresh Kumar } 7427813dd6fSViresh Kumar 7437813dd6fSViresh Kumar dev_pm_opp_put(opp); 7447813dd6fSViresh Kumar put_old_opp: 7457813dd6fSViresh Kumar if (!IS_ERR(old_opp)) 7467813dd6fSViresh Kumar dev_pm_opp_put(old_opp); 7477813dd6fSViresh Kumar put_opp_table: 7487813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 7497813dd6fSViresh Kumar return ret; 7507813dd6fSViresh Kumar } 7517813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 7527813dd6fSViresh Kumar 7537813dd6fSViresh Kumar /* OPP-dev Helpers */ 7547813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 7557813dd6fSViresh Kumar struct opp_table *opp_table) 7567813dd6fSViresh Kumar { 7577813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 7587813dd6fSViresh Kumar list_del(&opp_dev->node); 7597813dd6fSViresh Kumar kfree(opp_dev); 7607813dd6fSViresh Kumar } 7617813dd6fSViresh Kumar 762283d55e6SViresh Kumar static struct opp_device *_add_opp_dev_unlocked(const struct device *dev, 7637813dd6fSViresh Kumar struct opp_table *opp_table) 7647813dd6fSViresh Kumar { 7657813dd6fSViresh Kumar struct opp_device *opp_dev; 7667813dd6fSViresh Kumar int ret; 7677813dd6fSViresh Kumar 7687813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 7697813dd6fSViresh Kumar if (!opp_dev) 7707813dd6fSViresh Kumar return NULL; 7717813dd6fSViresh Kumar 7727813dd6fSViresh Kumar /* Initialize opp-dev */ 7737813dd6fSViresh Kumar opp_dev->dev = dev; 7743d255699SViresh Kumar 7757813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 7767813dd6fSViresh Kumar 7777813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 7787813dd6fSViresh Kumar ret = opp_debug_register(opp_dev, opp_table); 7797813dd6fSViresh Kumar if (ret) 7807813dd6fSViresh Kumar dev_err(dev, "%s: Failed to register opp debugfs (%d)\n", 7817813dd6fSViresh Kumar __func__, ret); 782283d55e6SViresh Kumar 783283d55e6SViresh Kumar return opp_dev; 784283d55e6SViresh Kumar } 785283d55e6SViresh Kumar 786283d55e6SViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 787283d55e6SViresh Kumar struct opp_table *opp_table) 788283d55e6SViresh Kumar { 789283d55e6SViresh Kumar struct opp_device *opp_dev; 790283d55e6SViresh Kumar 791283d55e6SViresh Kumar mutex_lock(&opp_table->lock); 792283d55e6SViresh Kumar opp_dev = _add_opp_dev_unlocked(dev, opp_table); 7933d255699SViresh Kumar mutex_unlock(&opp_table->lock); 7947813dd6fSViresh Kumar 7957813dd6fSViresh Kumar return opp_dev; 7967813dd6fSViresh Kumar } 7977813dd6fSViresh Kumar 798eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index) 7997813dd6fSViresh Kumar { 8007813dd6fSViresh Kumar struct opp_table *opp_table; 8017813dd6fSViresh Kumar struct opp_device *opp_dev; 8027813dd6fSViresh Kumar int ret; 8037813dd6fSViresh Kumar 8047813dd6fSViresh Kumar /* 8057813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 8067813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 8077813dd6fSViresh Kumar */ 8087813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 8097813dd6fSViresh Kumar if (!opp_table) 8107813dd6fSViresh Kumar return NULL; 8117813dd6fSViresh Kumar 8123d255699SViresh Kumar mutex_init(&opp_table->lock); 8137813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 8147813dd6fSViresh Kumar 8157813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 8167813dd6fSViresh Kumar if (!opp_dev) { 8177813dd6fSViresh Kumar kfree(opp_table); 8187813dd6fSViresh Kumar return NULL; 8197813dd6fSViresh Kumar } 8207813dd6fSViresh Kumar 821eb7c8743SViresh Kumar _of_init_opp_table(opp_table, dev, index); 8227813dd6fSViresh Kumar 8237813dd6fSViresh Kumar /* Find clk for the device */ 8247813dd6fSViresh Kumar opp_table->clk = clk_get(dev, NULL); 8257813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 8267813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 8277813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) 8287813dd6fSViresh Kumar dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, 8297813dd6fSViresh Kumar ret); 8307813dd6fSViresh Kumar } 8317813dd6fSViresh Kumar 8327813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 8337813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 8347813dd6fSViresh Kumar kref_init(&opp_table->kref); 8357813dd6fSViresh Kumar 8367813dd6fSViresh Kumar /* Secure the device table modification */ 8377813dd6fSViresh Kumar list_add(&opp_table->node, &opp_tables); 8387813dd6fSViresh Kumar return opp_table; 8397813dd6fSViresh Kumar } 8407813dd6fSViresh Kumar 8417813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 8427813dd6fSViresh Kumar { 8437813dd6fSViresh Kumar kref_get(&opp_table->kref); 8447813dd6fSViresh Kumar } 8457813dd6fSViresh Kumar 846eb7c8743SViresh Kumar static struct opp_table *_opp_get_opp_table(struct device *dev, int index) 8477813dd6fSViresh Kumar { 8487813dd6fSViresh Kumar struct opp_table *opp_table; 8497813dd6fSViresh Kumar 8507813dd6fSViresh Kumar /* Hold our table modification lock here */ 8517813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 8527813dd6fSViresh Kumar 8537813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 8547813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 8557813dd6fSViresh Kumar goto unlock; 8567813dd6fSViresh Kumar 857283d55e6SViresh Kumar opp_table = _managed_opp(dev, index); 858283d55e6SViresh Kumar if (opp_table) { 859283d55e6SViresh Kumar if (!_add_opp_dev_unlocked(dev, opp_table)) { 860283d55e6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 861283d55e6SViresh Kumar opp_table = NULL; 862283d55e6SViresh Kumar } 863283d55e6SViresh Kumar goto unlock; 864283d55e6SViresh Kumar } 865283d55e6SViresh Kumar 866eb7c8743SViresh Kumar opp_table = _allocate_opp_table(dev, index); 8677813dd6fSViresh Kumar 8687813dd6fSViresh Kumar unlock: 8697813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 8707813dd6fSViresh Kumar 8717813dd6fSViresh Kumar return opp_table; 8727813dd6fSViresh Kumar } 873eb7c8743SViresh Kumar 874eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 875eb7c8743SViresh Kumar { 876eb7c8743SViresh Kumar return _opp_get_opp_table(dev, 0); 877eb7c8743SViresh Kumar } 8787813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 8797813dd6fSViresh Kumar 880eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev, 881eb7c8743SViresh Kumar int index) 882eb7c8743SViresh Kumar { 883eb7c8743SViresh Kumar return _opp_get_opp_table(dev, index); 884eb7c8743SViresh Kumar } 885eb7c8743SViresh Kumar 8867813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 8877813dd6fSViresh Kumar { 8887813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 889cdd6ed90SViresh Kumar struct opp_device *opp_dev, *temp; 8907813dd6fSViresh Kumar 8917813dd6fSViresh Kumar /* Release clk */ 8927813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 8937813dd6fSViresh Kumar clk_put(opp_table->clk); 8947813dd6fSViresh Kumar 895cdd6ed90SViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 896cdd6ed90SViresh Kumar 897cdd6ed90SViresh Kumar list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 8983d255699SViresh Kumar /* 899cdd6ed90SViresh Kumar * The OPP table is getting removed, drop the performance state 900cdd6ed90SViresh Kumar * constraints. 9013d255699SViresh Kumar */ 902cdd6ed90SViresh Kumar if (opp_table->genpd_performance_state) 903cdd6ed90SViresh Kumar dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 9047813dd6fSViresh Kumar 9057813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 906cdd6ed90SViresh Kumar } 9077813dd6fSViresh Kumar 9087813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 9097813dd6fSViresh Kumar list_del(&opp_table->node); 9107813dd6fSViresh Kumar kfree(opp_table); 9117813dd6fSViresh Kumar 9127813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 9137813dd6fSViresh Kumar } 9147813dd6fSViresh Kumar 915d0e8ae6cSViresh Kumar void _opp_remove_all_static(struct opp_table *opp_table) 916d0e8ae6cSViresh Kumar { 917d0e8ae6cSViresh Kumar struct dev_pm_opp *opp, *tmp; 918d0e8ae6cSViresh Kumar 919d0e8ae6cSViresh Kumar list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { 920d0e8ae6cSViresh Kumar if (!opp->dynamic) 921d0e8ae6cSViresh Kumar dev_pm_opp_put(opp); 922d0e8ae6cSViresh Kumar } 923d0e8ae6cSViresh Kumar 924d0e8ae6cSViresh Kumar opp_table->parsed_static_opps = false; 925d0e8ae6cSViresh Kumar } 926d0e8ae6cSViresh Kumar 927d0e8ae6cSViresh Kumar static void _opp_table_list_kref_release(struct kref *kref) 928d0e8ae6cSViresh Kumar { 929d0e8ae6cSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, 930d0e8ae6cSViresh Kumar list_kref); 931d0e8ae6cSViresh Kumar 932d0e8ae6cSViresh Kumar _opp_remove_all_static(opp_table); 933d0e8ae6cSViresh Kumar mutex_unlock(&opp_table_lock); 934d0e8ae6cSViresh Kumar } 935d0e8ae6cSViresh Kumar 936d0e8ae6cSViresh Kumar void _put_opp_list_kref(struct opp_table *opp_table) 937d0e8ae6cSViresh Kumar { 938d0e8ae6cSViresh Kumar kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release, 939d0e8ae6cSViresh Kumar &opp_table_lock); 940d0e8ae6cSViresh Kumar } 941d0e8ae6cSViresh Kumar 9427813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 9437813dd6fSViresh Kumar { 9447813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 9457813dd6fSViresh Kumar &opp_table_lock); 9467813dd6fSViresh Kumar } 9477813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 9487813dd6fSViresh Kumar 9497813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 9507813dd6fSViresh Kumar { 9517813dd6fSViresh Kumar kfree(opp); 9527813dd6fSViresh Kumar } 9537813dd6fSViresh Kumar 9547813dd6fSViresh Kumar static void _opp_kref_release(struct kref *kref) 9557813dd6fSViresh Kumar { 9567813dd6fSViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 9577813dd6fSViresh Kumar struct opp_table *opp_table = opp->opp_table; 9587813dd6fSViresh Kumar 9597813dd6fSViresh Kumar /* 9607813dd6fSViresh Kumar * Notify the changes in the availability of the operable 9617813dd6fSViresh Kumar * frequency/voltage list. 9627813dd6fSViresh Kumar */ 9637813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 9647813dd6fSViresh Kumar opp_debug_remove_one(opp); 9657813dd6fSViresh Kumar list_del(&opp->node); 9667813dd6fSViresh Kumar kfree(opp); 9677813dd6fSViresh Kumar 9687813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 9697813dd6fSViresh Kumar } 9707813dd6fSViresh Kumar 971a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 9727813dd6fSViresh Kumar { 9737813dd6fSViresh Kumar kref_get(&opp->kref); 9747813dd6fSViresh Kumar } 9757813dd6fSViresh Kumar 9767813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 9777813dd6fSViresh Kumar { 9787813dd6fSViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 9797813dd6fSViresh Kumar } 9807813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 9817813dd6fSViresh Kumar 9827813dd6fSViresh Kumar /** 9837813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 9847813dd6fSViresh Kumar * @dev: device for which we do this operation 9857813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 9867813dd6fSViresh Kumar * 9877813dd6fSViresh Kumar * This function removes an opp from the opp table. 9887813dd6fSViresh Kumar */ 9897813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 9907813dd6fSViresh Kumar { 9917813dd6fSViresh Kumar struct dev_pm_opp *opp; 9927813dd6fSViresh Kumar struct opp_table *opp_table; 9937813dd6fSViresh Kumar bool found = false; 9947813dd6fSViresh Kumar 9957813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 9967813dd6fSViresh Kumar if (IS_ERR(opp_table)) 9977813dd6fSViresh Kumar return; 9987813dd6fSViresh Kumar 9997813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 10007813dd6fSViresh Kumar 10017813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 10027813dd6fSViresh Kumar if (opp->rate == freq) { 10037813dd6fSViresh Kumar found = true; 10047813dd6fSViresh Kumar break; 10057813dd6fSViresh Kumar } 10067813dd6fSViresh Kumar } 10077813dd6fSViresh Kumar 10087813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 10097813dd6fSViresh Kumar 10107813dd6fSViresh Kumar if (found) { 10117813dd6fSViresh Kumar dev_pm_opp_put(opp); 10120ad8c623SViresh Kumar 10130ad8c623SViresh Kumar /* Drop the reference taken by dev_pm_opp_add() */ 10140ad8c623SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 10157813dd6fSViresh Kumar } else { 10167813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 10177813dd6fSViresh Kumar __func__, freq); 10187813dd6fSViresh Kumar } 10197813dd6fSViresh Kumar 10200ad8c623SViresh Kumar /* Drop the reference taken by _find_opp_table() */ 10217813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 10227813dd6fSViresh Kumar } 10237813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 10247813dd6fSViresh Kumar 10257813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table) 10267813dd6fSViresh Kumar { 10277813dd6fSViresh Kumar struct dev_pm_opp *opp; 10287813dd6fSViresh Kumar int count, supply_size; 10297813dd6fSViresh Kumar 10307813dd6fSViresh Kumar /* Allocate space for at least one supply */ 10317813dd6fSViresh Kumar count = table->regulator_count ? table->regulator_count : 1; 10327813dd6fSViresh Kumar supply_size = sizeof(*opp->supplies) * count; 10337813dd6fSViresh Kumar 10347813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 10357813dd6fSViresh Kumar opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); 10367813dd6fSViresh Kumar if (!opp) 10377813dd6fSViresh Kumar return NULL; 10387813dd6fSViresh Kumar 10397813dd6fSViresh Kumar /* Put the supplies at the end of the OPP structure as an empty array */ 10407813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 10417813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 10427813dd6fSViresh Kumar 10437813dd6fSViresh Kumar return opp; 10447813dd6fSViresh Kumar } 10457813dd6fSViresh Kumar 10467813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 10477813dd6fSViresh Kumar struct opp_table *opp_table) 10487813dd6fSViresh Kumar { 10497813dd6fSViresh Kumar struct regulator *reg; 10507813dd6fSViresh Kumar int i; 10517813dd6fSViresh Kumar 1052*90e3577bSViresh Kumar if (!opp_table->regulators) 1053*90e3577bSViresh Kumar return true; 1054*90e3577bSViresh Kumar 10557813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 10567813dd6fSViresh Kumar reg = opp_table->regulators[i]; 10577813dd6fSViresh Kumar 10587813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 10597813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 10607813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 10617813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 10627813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 10637813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 10647813dd6fSViresh Kumar return false; 10657813dd6fSViresh Kumar } 10667813dd6fSViresh Kumar } 10677813dd6fSViresh Kumar 10687813dd6fSViresh Kumar return true; 10697813dd6fSViresh Kumar } 10707813dd6fSViresh Kumar 1071a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1072a1e8c136SViresh Kumar struct opp_table *opp_table, 1073a1e8c136SViresh Kumar struct list_head **head) 1074a1e8c136SViresh Kumar { 1075a1e8c136SViresh Kumar struct dev_pm_opp *opp; 1076a1e8c136SViresh Kumar 1077a1e8c136SViresh Kumar /* 1078a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1079a1e8c136SViresh Kumar * already present. 1080a1e8c136SViresh Kumar * 1081a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1082a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1083a1e8c136SViresh Kumar * loop. 1084a1e8c136SViresh Kumar */ 1085a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 1086a1e8c136SViresh Kumar if (new_opp->rate > opp->rate) { 1087a1e8c136SViresh Kumar *head = &opp->node; 1088a1e8c136SViresh Kumar continue; 1089a1e8c136SViresh Kumar } 1090a1e8c136SViresh Kumar 1091a1e8c136SViresh Kumar if (new_opp->rate < opp->rate) 1092a1e8c136SViresh Kumar return 0; 1093a1e8c136SViresh Kumar 1094a1e8c136SViresh Kumar /* Duplicate OPPs */ 1095a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1096a1e8c136SViresh Kumar __func__, opp->rate, opp->supplies[0].u_volt, 1097a1e8c136SViresh Kumar opp->available, new_opp->rate, 1098a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1099a1e8c136SViresh Kumar 1100a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1101a1e8c136SViresh Kumar return opp->available && 1102a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1103a1e8c136SViresh Kumar } 1104a1e8c136SViresh Kumar 1105a1e8c136SViresh Kumar return 0; 1106a1e8c136SViresh Kumar } 1107a1e8c136SViresh Kumar 11087813dd6fSViresh Kumar /* 11097813dd6fSViresh Kumar * Returns: 11107813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 11117813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 11127813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 11137813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 11147813dd6fSViresh Kumar * kernel try to initialize the OPP table. 11157813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 11167813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 11177813dd6fSViresh Kumar */ 11187813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1119a1e8c136SViresh Kumar struct opp_table *opp_table, bool rate_not_available) 11207813dd6fSViresh Kumar { 11217813dd6fSViresh Kumar struct list_head *head; 11227813dd6fSViresh Kumar int ret; 11237813dd6fSViresh Kumar 11247813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 11257813dd6fSViresh Kumar head = &opp_table->opp_list; 11267813dd6fSViresh Kumar 1127a1e8c136SViresh Kumar if (likely(!rate_not_available)) { 1128a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1129a1e8c136SViresh Kumar if (ret) { 11307813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 11317813dd6fSViresh Kumar return ret; 11327813dd6fSViresh Kumar } 1133a1e8c136SViresh Kumar } 11347813dd6fSViresh Kumar 11357813dd6fSViresh Kumar list_add(&new_opp->node, head); 11367813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 11377813dd6fSViresh Kumar 11387813dd6fSViresh Kumar new_opp->opp_table = opp_table; 11397813dd6fSViresh Kumar kref_init(&new_opp->kref); 11407813dd6fSViresh Kumar 11417813dd6fSViresh Kumar ret = opp_debug_create_one(new_opp, opp_table); 11427813dd6fSViresh Kumar if (ret) 11437813dd6fSViresh Kumar dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n", 11447813dd6fSViresh Kumar __func__, ret); 11457813dd6fSViresh Kumar 11467813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 11477813dd6fSViresh Kumar new_opp->available = false; 11487813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 11497813dd6fSViresh Kumar __func__, new_opp->rate); 11507813dd6fSViresh Kumar } 11517813dd6fSViresh Kumar 11527813dd6fSViresh Kumar return 0; 11537813dd6fSViresh Kumar } 11547813dd6fSViresh Kumar 11557813dd6fSViresh Kumar /** 11567813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 11577813dd6fSViresh Kumar * @opp_table: OPP table 11587813dd6fSViresh Kumar * @dev: device for which we do this operation 11597813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 11607813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 11617813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 11627813dd6fSViresh Kumar * 11637813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 11647813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 11657813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 11667813dd6fSViresh Kumar * 11677813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 11687813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 11697813dd6fSViresh Kumar * 11707813dd6fSViresh Kumar * Return: 11717813dd6fSViresh Kumar * 0 On success OR 11727813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 11737813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 11747813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 11757813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 11767813dd6fSViresh Kumar */ 11777813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 11787813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 11797813dd6fSViresh Kumar { 11807813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 11817813dd6fSViresh Kumar unsigned long tol; 11827813dd6fSViresh Kumar int ret; 11837813dd6fSViresh Kumar 11847813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 11857813dd6fSViresh Kumar if (!new_opp) 11867813dd6fSViresh Kumar return -ENOMEM; 11877813dd6fSViresh Kumar 11887813dd6fSViresh Kumar /* populate the opp table */ 11897813dd6fSViresh Kumar new_opp->rate = freq; 11907813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 11917813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 11927813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 11937813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 11947813dd6fSViresh Kumar new_opp->available = true; 11957813dd6fSViresh Kumar new_opp->dynamic = dynamic; 11967813dd6fSViresh Kumar 1197a1e8c136SViresh Kumar ret = _opp_add(dev, new_opp, opp_table, false); 11987813dd6fSViresh Kumar if (ret) { 11997813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 12007813dd6fSViresh Kumar if (ret == -EBUSY) 12017813dd6fSViresh Kumar ret = 0; 12027813dd6fSViresh Kumar goto free_opp; 12037813dd6fSViresh Kumar } 12047813dd6fSViresh Kumar 12057813dd6fSViresh Kumar /* 12067813dd6fSViresh Kumar * Notify the changes in the availability of the operable 12077813dd6fSViresh Kumar * frequency/voltage list. 12087813dd6fSViresh Kumar */ 12097813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 12107813dd6fSViresh Kumar return 0; 12117813dd6fSViresh Kumar 12127813dd6fSViresh Kumar free_opp: 12137813dd6fSViresh Kumar _opp_free(new_opp); 12147813dd6fSViresh Kumar 12157813dd6fSViresh Kumar return ret; 12167813dd6fSViresh Kumar } 12177813dd6fSViresh Kumar 12187813dd6fSViresh Kumar /** 12197813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw() - Set supported platforms 12207813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 12217813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 12227813dd6fSViresh Kumar * @count: Number of elements in the array. 12237813dd6fSViresh Kumar * 12247813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 12257813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 12267813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 12277813dd6fSViresh Kumar * property. 12287813dd6fSViresh Kumar */ 12297813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, 12307813dd6fSViresh Kumar const u32 *versions, unsigned int count) 12317813dd6fSViresh Kumar { 12327813dd6fSViresh Kumar struct opp_table *opp_table; 12337813dd6fSViresh Kumar 12347813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 12357813dd6fSViresh Kumar if (!opp_table) 12367813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 12377813dd6fSViresh Kumar 12387813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 12397813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 12407813dd6fSViresh Kumar 124125419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 124225419de1SViresh Kumar if (opp_table->supported_hw) 124325419de1SViresh Kumar return opp_table; 12447813dd6fSViresh Kumar 12457813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 12467813dd6fSViresh Kumar GFP_KERNEL); 12477813dd6fSViresh Kumar if (!opp_table->supported_hw) { 124825419de1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 124925419de1SViresh Kumar return ERR_PTR(-ENOMEM); 12507813dd6fSViresh Kumar } 12517813dd6fSViresh Kumar 12527813dd6fSViresh Kumar opp_table->supported_hw_count = count; 12537813dd6fSViresh Kumar 12547813dd6fSViresh Kumar return opp_table; 12557813dd6fSViresh Kumar } 12567813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); 12577813dd6fSViresh Kumar 12587813dd6fSViresh Kumar /** 12597813dd6fSViresh Kumar * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw 12607813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). 12617813dd6fSViresh Kumar * 12627813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 12637813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure 12647813dd6fSViresh Kumar * will not be freed. 12657813dd6fSViresh Kumar */ 12667813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) 12677813dd6fSViresh Kumar { 12687813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 12697813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 12707813dd6fSViresh Kumar 12717813dd6fSViresh Kumar kfree(opp_table->supported_hw); 12727813dd6fSViresh Kumar opp_table->supported_hw = NULL; 12737813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 12747813dd6fSViresh Kumar 12757813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 12767813dd6fSViresh Kumar } 12777813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); 12787813dd6fSViresh Kumar 12797813dd6fSViresh Kumar /** 12807813dd6fSViresh Kumar * dev_pm_opp_set_prop_name() - Set prop-extn name 12817813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 12827813dd6fSViresh Kumar * @name: name to postfix to properties. 12837813dd6fSViresh Kumar * 12847813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 12857813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 12867813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 12877813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 12887813dd6fSViresh Kumar */ 12897813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 12907813dd6fSViresh Kumar { 12917813dd6fSViresh Kumar struct opp_table *opp_table; 12927813dd6fSViresh Kumar 12937813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 12947813dd6fSViresh Kumar if (!opp_table) 12957813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 12967813dd6fSViresh Kumar 12977813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 12987813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 12997813dd6fSViresh Kumar 1300878ec1a9SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 1301878ec1a9SViresh Kumar if (opp_table->prop_name) 1302878ec1a9SViresh Kumar return opp_table; 13037813dd6fSViresh Kumar 13047813dd6fSViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 13057813dd6fSViresh Kumar if (!opp_table->prop_name) { 1306878ec1a9SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1307878ec1a9SViresh Kumar return ERR_PTR(-ENOMEM); 13087813dd6fSViresh Kumar } 13097813dd6fSViresh Kumar 13107813dd6fSViresh Kumar return opp_table; 13117813dd6fSViresh Kumar } 13127813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); 13137813dd6fSViresh Kumar 13147813dd6fSViresh Kumar /** 13157813dd6fSViresh Kumar * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name 13167813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). 13177813dd6fSViresh Kumar * 13187813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 13197813dd6fSViresh Kumar * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure 13207813dd6fSViresh Kumar * will not be freed. 13217813dd6fSViresh Kumar */ 13227813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table) 13237813dd6fSViresh Kumar { 13247813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 13257813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 13267813dd6fSViresh Kumar 13277813dd6fSViresh Kumar kfree(opp_table->prop_name); 13287813dd6fSViresh Kumar opp_table->prop_name = NULL; 13297813dd6fSViresh Kumar 13307813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 13317813dd6fSViresh Kumar } 13327813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); 13337813dd6fSViresh Kumar 13347813dd6fSViresh Kumar static int _allocate_set_opp_data(struct opp_table *opp_table) 13357813dd6fSViresh Kumar { 13367813dd6fSViresh Kumar struct dev_pm_set_opp_data *data; 13377813dd6fSViresh Kumar int len, count = opp_table->regulator_count; 13387813dd6fSViresh Kumar 1339*90e3577bSViresh Kumar if (WARN_ON(!opp_table->regulators)) 13407813dd6fSViresh Kumar return -EINVAL; 13417813dd6fSViresh Kumar 13427813dd6fSViresh Kumar /* space for set_opp_data */ 13437813dd6fSViresh Kumar len = sizeof(*data); 13447813dd6fSViresh Kumar 13457813dd6fSViresh Kumar /* space for old_opp.supplies and new_opp.supplies */ 13467813dd6fSViresh Kumar len += 2 * sizeof(struct dev_pm_opp_supply) * count; 13477813dd6fSViresh Kumar 13487813dd6fSViresh Kumar data = kzalloc(len, GFP_KERNEL); 13497813dd6fSViresh Kumar if (!data) 13507813dd6fSViresh Kumar return -ENOMEM; 13517813dd6fSViresh Kumar 13527813dd6fSViresh Kumar data->old_opp.supplies = (void *)(data + 1); 13537813dd6fSViresh Kumar data->new_opp.supplies = data->old_opp.supplies + count; 13547813dd6fSViresh Kumar 13557813dd6fSViresh Kumar opp_table->set_opp_data = data; 13567813dd6fSViresh Kumar 13577813dd6fSViresh Kumar return 0; 13587813dd6fSViresh Kumar } 13597813dd6fSViresh Kumar 13607813dd6fSViresh Kumar static void _free_set_opp_data(struct opp_table *opp_table) 13617813dd6fSViresh Kumar { 13627813dd6fSViresh Kumar kfree(opp_table->set_opp_data); 13637813dd6fSViresh Kumar opp_table->set_opp_data = NULL; 13647813dd6fSViresh Kumar } 13657813dd6fSViresh Kumar 13667813dd6fSViresh Kumar /** 13677813dd6fSViresh Kumar * dev_pm_opp_set_regulators() - Set regulator names for the device 13687813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 13697813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 13707813dd6fSViresh Kumar * @count: Number of regulators. 13717813dd6fSViresh Kumar * 13727813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 13737813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 13747813dd6fSViresh Kumar * well. 13757813dd6fSViresh Kumar * 13767813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 13777813dd6fSViresh Kumar */ 13787813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev, 13797813dd6fSViresh Kumar const char * const names[], 13807813dd6fSViresh Kumar unsigned int count) 13817813dd6fSViresh Kumar { 13827813dd6fSViresh Kumar struct opp_table *opp_table; 13837813dd6fSViresh Kumar struct regulator *reg; 13847813dd6fSViresh Kumar int ret, i; 13857813dd6fSViresh Kumar 13867813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 13877813dd6fSViresh Kumar if (!opp_table) 13887813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 13897813dd6fSViresh Kumar 13907813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 13917813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 13927813dd6fSViresh Kumar ret = -EBUSY; 13937813dd6fSViresh Kumar goto err; 13947813dd6fSViresh Kumar } 13957813dd6fSViresh Kumar 1396779b783cSViresh Kumar /* Another CPU that shares the OPP table has set the regulators ? */ 1397779b783cSViresh Kumar if (opp_table->regulators) 1398779b783cSViresh Kumar return opp_table; 13997813dd6fSViresh Kumar 14007813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 14017813dd6fSViresh Kumar sizeof(*opp_table->regulators), 14027813dd6fSViresh Kumar GFP_KERNEL); 14037813dd6fSViresh Kumar if (!opp_table->regulators) { 14047813dd6fSViresh Kumar ret = -ENOMEM; 14057813dd6fSViresh Kumar goto err; 14067813dd6fSViresh Kumar } 14077813dd6fSViresh Kumar 14087813dd6fSViresh Kumar for (i = 0; i < count; i++) { 14097813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 14107813dd6fSViresh Kumar if (IS_ERR(reg)) { 14117813dd6fSViresh Kumar ret = PTR_ERR(reg); 14127813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) 14137813dd6fSViresh Kumar dev_err(dev, "%s: no regulator (%s) found: %d\n", 14147813dd6fSViresh Kumar __func__, names[i], ret); 14157813dd6fSViresh Kumar goto free_regulators; 14167813dd6fSViresh Kumar } 14177813dd6fSViresh Kumar 14187813dd6fSViresh Kumar opp_table->regulators[i] = reg; 14197813dd6fSViresh Kumar } 14207813dd6fSViresh Kumar 14217813dd6fSViresh Kumar opp_table->regulator_count = count; 14227813dd6fSViresh Kumar 14237813dd6fSViresh Kumar /* Allocate block only once to pass to set_opp() routines */ 14247813dd6fSViresh Kumar ret = _allocate_set_opp_data(opp_table); 14257813dd6fSViresh Kumar if (ret) 14267813dd6fSViresh Kumar goto free_regulators; 14277813dd6fSViresh Kumar 14287813dd6fSViresh Kumar return opp_table; 14297813dd6fSViresh Kumar 14307813dd6fSViresh Kumar free_regulators: 14317813dd6fSViresh Kumar while (i != 0) 14327813dd6fSViresh Kumar regulator_put(opp_table->regulators[--i]); 14337813dd6fSViresh Kumar 14347813dd6fSViresh Kumar kfree(opp_table->regulators); 14357813dd6fSViresh Kumar opp_table->regulators = NULL; 14367813dd6fSViresh Kumar opp_table->regulator_count = 0; 14377813dd6fSViresh Kumar err: 14387813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 14397813dd6fSViresh Kumar 14407813dd6fSViresh Kumar return ERR_PTR(ret); 14417813dd6fSViresh Kumar } 14427813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); 14437813dd6fSViresh Kumar 14447813dd6fSViresh Kumar /** 14457813dd6fSViresh Kumar * dev_pm_opp_put_regulators() - Releases resources blocked for regulator 14467813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). 14477813dd6fSViresh Kumar */ 14487813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table) 14497813dd6fSViresh Kumar { 14507813dd6fSViresh Kumar int i; 14517813dd6fSViresh Kumar 1452779b783cSViresh Kumar if (!opp_table->regulators) 1453779b783cSViresh Kumar goto put_opp_table; 14547813dd6fSViresh Kumar 14557813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 14567813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 14577813dd6fSViresh Kumar 14587813dd6fSViresh Kumar for (i = opp_table->regulator_count - 1; i >= 0; i--) 14597813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 14607813dd6fSViresh Kumar 14617813dd6fSViresh Kumar _free_set_opp_data(opp_table); 14627813dd6fSViresh Kumar 14637813dd6fSViresh Kumar kfree(opp_table->regulators); 14647813dd6fSViresh Kumar opp_table->regulators = NULL; 14657813dd6fSViresh Kumar opp_table->regulator_count = 0; 14667813dd6fSViresh Kumar 1467779b783cSViresh Kumar put_opp_table: 14687813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 14697813dd6fSViresh Kumar } 14707813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); 14717813dd6fSViresh Kumar 14727813dd6fSViresh Kumar /** 14737813dd6fSViresh Kumar * dev_pm_opp_set_clkname() - Set clk name for the device 14747813dd6fSViresh Kumar * @dev: Device for which clk name is being set. 14757813dd6fSViresh Kumar * @name: Clk name. 14767813dd6fSViresh Kumar * 14777813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to get pointer to the 14787813dd6fSViresh Kumar * clock for the device. Simple cases work fine without using this routine (i.e. 14797813dd6fSViresh Kumar * by passing connection-id as NULL), but for a device with multiple clocks 14807813dd6fSViresh Kumar * available, the OPP core needs to know the exact name of the clk to use. 14817813dd6fSViresh Kumar * 14827813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 14837813dd6fSViresh Kumar */ 14847813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) 14857813dd6fSViresh Kumar { 14867813dd6fSViresh Kumar struct opp_table *opp_table; 14877813dd6fSViresh Kumar int ret; 14887813dd6fSViresh Kumar 14897813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 14907813dd6fSViresh Kumar if (!opp_table) 14917813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 14927813dd6fSViresh Kumar 14937813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 14947813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 14957813dd6fSViresh Kumar ret = -EBUSY; 14967813dd6fSViresh Kumar goto err; 14977813dd6fSViresh Kumar } 14987813dd6fSViresh Kumar 14997813dd6fSViresh Kumar /* Already have default clk set, free it */ 15007813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 15017813dd6fSViresh Kumar clk_put(opp_table->clk); 15027813dd6fSViresh Kumar 15037813dd6fSViresh Kumar /* Find clk for the device */ 15047813dd6fSViresh Kumar opp_table->clk = clk_get(dev, name); 15057813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 15067813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 15077813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) { 15087813dd6fSViresh Kumar dev_err(dev, "%s: Couldn't find clock: %d\n", __func__, 15097813dd6fSViresh Kumar ret); 15107813dd6fSViresh Kumar } 15117813dd6fSViresh Kumar goto err; 15127813dd6fSViresh Kumar } 15137813dd6fSViresh Kumar 15147813dd6fSViresh Kumar return opp_table; 15157813dd6fSViresh Kumar 15167813dd6fSViresh Kumar err: 15177813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15187813dd6fSViresh Kumar 15197813dd6fSViresh Kumar return ERR_PTR(ret); 15207813dd6fSViresh Kumar } 15217813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); 15227813dd6fSViresh Kumar 15237813dd6fSViresh Kumar /** 15247813dd6fSViresh Kumar * dev_pm_opp_put_clkname() - Releases resources blocked for clk. 15257813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). 15267813dd6fSViresh Kumar */ 15277813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table) 15287813dd6fSViresh Kumar { 15297813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 15307813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 15317813dd6fSViresh Kumar 15327813dd6fSViresh Kumar clk_put(opp_table->clk); 15337813dd6fSViresh Kumar opp_table->clk = ERR_PTR(-EINVAL); 15347813dd6fSViresh Kumar 15357813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15367813dd6fSViresh Kumar } 15377813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); 15387813dd6fSViresh Kumar 15397813dd6fSViresh Kumar /** 15407813dd6fSViresh Kumar * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper 15417813dd6fSViresh Kumar * @dev: Device for which the helper is getting registered. 15427813dd6fSViresh Kumar * @set_opp: Custom set OPP helper. 15437813dd6fSViresh Kumar * 15447813dd6fSViresh Kumar * This is useful to support complex platforms (like platforms with multiple 15457813dd6fSViresh Kumar * regulators per device), instead of the generic OPP set rate helper. 15467813dd6fSViresh Kumar * 15477813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 15487813dd6fSViresh Kumar */ 15497813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 15507813dd6fSViresh Kumar int (*set_opp)(struct dev_pm_set_opp_data *data)) 15517813dd6fSViresh Kumar { 15527813dd6fSViresh Kumar struct opp_table *opp_table; 15537813dd6fSViresh Kumar 15547813dd6fSViresh Kumar if (!set_opp) 15557813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 15567813dd6fSViresh Kumar 15577813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 15587813dd6fSViresh Kumar if (!opp_table) 15597813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 15607813dd6fSViresh Kumar 15617813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 15627813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 15635019acc6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15645019acc6SViresh Kumar return ERR_PTR(-EBUSY); 15657813dd6fSViresh Kumar } 15667813dd6fSViresh Kumar 15675019acc6SViresh Kumar /* Another CPU that shares the OPP table has set the helper ? */ 15685019acc6SViresh Kumar if (!opp_table->set_opp) 15697813dd6fSViresh Kumar opp_table->set_opp = set_opp; 15707813dd6fSViresh Kumar 15717813dd6fSViresh Kumar return opp_table; 15727813dd6fSViresh Kumar } 15737813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); 15747813dd6fSViresh Kumar 15757813dd6fSViresh Kumar /** 1576604a7aebSViresh Kumar * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for 15777813dd6fSViresh Kumar * set_opp helper 15787813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). 15797813dd6fSViresh Kumar * 15807813dd6fSViresh Kumar * Release resources blocked for platform specific set_opp helper. 15817813dd6fSViresh Kumar */ 1582604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) 15837813dd6fSViresh Kumar { 15847813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 15857813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 15867813dd6fSViresh Kumar 15877813dd6fSViresh Kumar opp_table->set_opp = NULL; 15887813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15897813dd6fSViresh Kumar } 1590604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); 15917813dd6fSViresh Kumar 15927813dd6fSViresh Kumar /** 15937813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 15947813dd6fSViresh Kumar * @dev: device for which we do this operation 15957813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 15967813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 15977813dd6fSViresh Kumar * 15987813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 15997813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 16007813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 16017813dd6fSViresh Kumar * 16027813dd6fSViresh Kumar * Return: 16037813dd6fSViresh Kumar * 0 On success OR 16047813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 16057813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 16067813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 16077813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 16087813dd6fSViresh Kumar */ 16097813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 16107813dd6fSViresh Kumar { 16117813dd6fSViresh Kumar struct opp_table *opp_table; 16127813dd6fSViresh Kumar int ret; 16137813dd6fSViresh Kumar 16147813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 16157813dd6fSViresh Kumar if (!opp_table) 16167813dd6fSViresh Kumar return -ENOMEM; 16177813dd6fSViresh Kumar 16187813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 16190ad8c623SViresh Kumar if (ret) 16207813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16210ad8c623SViresh Kumar 16227813dd6fSViresh Kumar return ret; 16237813dd6fSViresh Kumar } 16247813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 16257813dd6fSViresh Kumar 16267813dd6fSViresh Kumar /** 16277813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 16287813dd6fSViresh Kumar * @dev: device for which we do this operation 16297813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 16307813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 16317813dd6fSViresh Kumar * 16327813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 16337813dd6fSViresh Kumar * which is isolated here. 16347813dd6fSViresh Kumar * 16357813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 16367813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 16377813dd6fSViresh Kumar * successful. 16387813dd6fSViresh Kumar */ 16397813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 16407813dd6fSViresh Kumar bool availability_req) 16417813dd6fSViresh Kumar { 16427813dd6fSViresh Kumar struct opp_table *opp_table; 16437813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 16447813dd6fSViresh Kumar int r = 0; 16457813dd6fSViresh Kumar 16467813dd6fSViresh Kumar /* Find the opp_table */ 16477813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 16487813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 16497813dd6fSViresh Kumar r = PTR_ERR(opp_table); 16507813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 16517813dd6fSViresh Kumar return r; 16527813dd6fSViresh Kumar } 16537813dd6fSViresh Kumar 16547813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 16557813dd6fSViresh Kumar 16567813dd6fSViresh Kumar /* Do we have the frequency? */ 16577813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 16587813dd6fSViresh Kumar if (tmp_opp->rate == freq) { 16597813dd6fSViresh Kumar opp = tmp_opp; 16607813dd6fSViresh Kumar break; 16617813dd6fSViresh Kumar } 16627813dd6fSViresh Kumar } 16637813dd6fSViresh Kumar 16647813dd6fSViresh Kumar if (IS_ERR(opp)) { 16657813dd6fSViresh Kumar r = PTR_ERR(opp); 16667813dd6fSViresh Kumar goto unlock; 16677813dd6fSViresh Kumar } 16687813dd6fSViresh Kumar 16697813dd6fSViresh Kumar /* Is update really needed? */ 16707813dd6fSViresh Kumar if (opp->available == availability_req) 16717813dd6fSViresh Kumar goto unlock; 16727813dd6fSViresh Kumar 16737813dd6fSViresh Kumar opp->available = availability_req; 16747813dd6fSViresh Kumar 16757813dd6fSViresh Kumar dev_pm_opp_get(opp); 16767813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 16777813dd6fSViresh Kumar 16787813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 16797813dd6fSViresh Kumar if (availability_req) 16807813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 16817813dd6fSViresh Kumar opp); 16827813dd6fSViresh Kumar else 16837813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 16847813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 16857813dd6fSViresh Kumar 16867813dd6fSViresh Kumar dev_pm_opp_put(opp); 16877813dd6fSViresh Kumar goto put_table; 16887813dd6fSViresh Kumar 16897813dd6fSViresh Kumar unlock: 16907813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 16917813dd6fSViresh Kumar put_table: 16927813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16937813dd6fSViresh Kumar return r; 16947813dd6fSViresh Kumar } 16957813dd6fSViresh Kumar 16967813dd6fSViresh Kumar /** 16977813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 16987813dd6fSViresh Kumar * @dev: device for which we do this operation 16997813dd6fSViresh Kumar * @freq: OPP frequency to enable 17007813dd6fSViresh Kumar * 17017813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 17027813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 17037813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 17047813dd6fSViresh Kumar * 17057813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 17067813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 17077813dd6fSViresh Kumar * successful. 17087813dd6fSViresh Kumar */ 17097813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 17107813dd6fSViresh Kumar { 17117813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 17127813dd6fSViresh Kumar } 17137813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 17147813dd6fSViresh Kumar 17157813dd6fSViresh Kumar /** 17167813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 17177813dd6fSViresh Kumar * @dev: device for which we do this operation 17187813dd6fSViresh Kumar * @freq: OPP frequency to disable 17197813dd6fSViresh Kumar * 17207813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 17217813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 17227813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 17237813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 17247813dd6fSViresh Kumar * 17257813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 17267813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 17277813dd6fSViresh Kumar * successful. 17287813dd6fSViresh Kumar */ 17297813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 17307813dd6fSViresh Kumar { 17317813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 17327813dd6fSViresh Kumar } 17337813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 17347813dd6fSViresh Kumar 17357813dd6fSViresh Kumar /** 17367813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 17377813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 17387813dd6fSViresh Kumar * @nb: Notifier block to be registered 17397813dd6fSViresh Kumar * 17407813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 17417813dd6fSViresh Kumar */ 17427813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 17437813dd6fSViresh Kumar { 17447813dd6fSViresh Kumar struct opp_table *opp_table; 17457813dd6fSViresh Kumar int ret; 17467813dd6fSViresh Kumar 17477813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 17487813dd6fSViresh Kumar if (IS_ERR(opp_table)) 17497813dd6fSViresh Kumar return PTR_ERR(opp_table); 17507813dd6fSViresh Kumar 17517813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 17527813dd6fSViresh Kumar 17537813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17547813dd6fSViresh Kumar 17557813dd6fSViresh Kumar return ret; 17567813dd6fSViresh Kumar } 17577813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 17587813dd6fSViresh Kumar 17597813dd6fSViresh Kumar /** 17607813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 17617813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 17627813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 17637813dd6fSViresh Kumar * 17647813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 17657813dd6fSViresh Kumar */ 17667813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 17677813dd6fSViresh Kumar struct notifier_block *nb) 17687813dd6fSViresh Kumar { 17697813dd6fSViresh Kumar struct opp_table *opp_table; 17707813dd6fSViresh Kumar int ret; 17717813dd6fSViresh Kumar 17727813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 17737813dd6fSViresh Kumar if (IS_ERR(opp_table)) 17747813dd6fSViresh Kumar return PTR_ERR(opp_table); 17757813dd6fSViresh Kumar 17767813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 17777813dd6fSViresh Kumar 17787813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17797813dd6fSViresh Kumar 17807813dd6fSViresh Kumar return ret; 17817813dd6fSViresh Kumar } 17827813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 17837813dd6fSViresh Kumar 17842a4eb735SViresh Kumar void _dev_pm_opp_find_and_remove_table(struct device *dev) 17857813dd6fSViresh Kumar { 17867813dd6fSViresh Kumar struct opp_table *opp_table; 17877813dd6fSViresh Kumar 17887813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 17897813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 17907813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 17917813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 17927813dd6fSViresh Kumar 17937813dd6fSViresh Kumar if (error != -ENODEV) 17947813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 17957813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 17967813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 17977813dd6fSViresh Kumar error); 17987813dd6fSViresh Kumar return; 17997813dd6fSViresh Kumar } 18007813dd6fSViresh Kumar 1801cdd6ed90SViresh Kumar _put_opp_list_kref(opp_table); 18027813dd6fSViresh Kumar 1803cdd6ed90SViresh Kumar /* Drop reference taken by _find_opp_table() */ 1804cdd6ed90SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1805cdd6ed90SViresh Kumar 1806cdd6ed90SViresh Kumar /* Drop reference taken while the OPP table was added */ 18077813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 18087813dd6fSViresh Kumar } 18097813dd6fSViresh Kumar 18107813dd6fSViresh Kumar /** 18117813dd6fSViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 18127813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table. 18137813dd6fSViresh Kumar * 18147813dd6fSViresh Kumar * Free both OPPs created using static entries present in DT and the 18157813dd6fSViresh Kumar * dynamically added entries. 18167813dd6fSViresh Kumar */ 18177813dd6fSViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 18187813dd6fSViresh Kumar { 18192a4eb735SViresh Kumar _dev_pm_opp_find_and_remove_table(dev); 18207813dd6fSViresh Kumar } 18217813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 1822