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