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 /** 1345b93ac54SRajendra Nayak * dev_pm_opp_get_level() - Gets the level corresponding to an available opp 1355b93ac54SRajendra Nayak * @opp: opp for which level value has to be returned for 1365b93ac54SRajendra Nayak * 1375b93ac54SRajendra Nayak * Return: level read from device tree corresponding to the opp, else 1385b93ac54SRajendra Nayak * return 0. 1395b93ac54SRajendra Nayak */ 1405b93ac54SRajendra Nayak unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp) 1415b93ac54SRajendra Nayak { 1425b93ac54SRajendra Nayak if (IS_ERR_OR_NULL(opp) || !opp->available) { 1435b93ac54SRajendra Nayak pr_err("%s: Invalid parameters\n", __func__); 1445b93ac54SRajendra Nayak return 0; 1455b93ac54SRajendra Nayak } 1465b93ac54SRajendra Nayak 1475b93ac54SRajendra Nayak return opp->level; 1485b93ac54SRajendra Nayak } 1495b93ac54SRajendra Nayak EXPORT_SYMBOL_GPL(dev_pm_opp_get_level); 1505b93ac54SRajendra Nayak 1515b93ac54SRajendra Nayak /** 1527813dd6fSViresh Kumar * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not 1537813dd6fSViresh Kumar * @opp: opp for which turbo mode is being verified 1547813dd6fSViresh Kumar * 1557813dd6fSViresh Kumar * Turbo OPPs are not for normal use, and can be enabled (under certain 1567813dd6fSViresh Kumar * conditions) for short duration of times to finish high throughput work 1577813dd6fSViresh Kumar * quickly. Running on them for longer times may overheat the chip. 1587813dd6fSViresh Kumar * 1597813dd6fSViresh Kumar * Return: true if opp is turbo opp, else false. 1607813dd6fSViresh Kumar */ 1617813dd6fSViresh Kumar bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 1627813dd6fSViresh Kumar { 1637813dd6fSViresh Kumar if (IS_ERR_OR_NULL(opp) || !opp->available) { 1647813dd6fSViresh Kumar pr_err("%s: Invalid parameters\n", __func__); 1657813dd6fSViresh Kumar return false; 1667813dd6fSViresh Kumar } 1677813dd6fSViresh Kumar 1687813dd6fSViresh Kumar return opp->turbo; 1697813dd6fSViresh Kumar } 1707813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 1717813dd6fSViresh Kumar 1727813dd6fSViresh Kumar /** 1737813dd6fSViresh Kumar * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds 1747813dd6fSViresh Kumar * @dev: device for which we do this operation 1757813dd6fSViresh Kumar * 1767813dd6fSViresh Kumar * Return: This function returns the max clock latency in nanoseconds. 1777813dd6fSViresh Kumar */ 1787813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 1797813dd6fSViresh Kumar { 1807813dd6fSViresh Kumar struct opp_table *opp_table; 1817813dd6fSViresh Kumar unsigned long clock_latency_ns; 1827813dd6fSViresh Kumar 1837813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 1847813dd6fSViresh Kumar if (IS_ERR(opp_table)) 1857813dd6fSViresh Kumar return 0; 1867813dd6fSViresh Kumar 1877813dd6fSViresh Kumar clock_latency_ns = opp_table->clock_latency_ns_max; 1887813dd6fSViresh Kumar 1897813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1907813dd6fSViresh Kumar 1917813dd6fSViresh Kumar return clock_latency_ns; 1927813dd6fSViresh Kumar } 1937813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 1947813dd6fSViresh Kumar 1957813dd6fSViresh Kumar /** 1967813dd6fSViresh Kumar * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 1977813dd6fSViresh Kumar * @dev: device for which we do this operation 1987813dd6fSViresh Kumar * 1997813dd6fSViresh Kumar * Return: This function returns the max voltage latency in nanoseconds. 2007813dd6fSViresh Kumar */ 2017813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 2027813dd6fSViresh Kumar { 2037813dd6fSViresh Kumar struct opp_table *opp_table; 2047813dd6fSViresh Kumar struct dev_pm_opp *opp; 2057813dd6fSViresh Kumar struct regulator *reg; 2067813dd6fSViresh Kumar unsigned long latency_ns = 0; 2077813dd6fSViresh Kumar int ret, i, count; 2087813dd6fSViresh Kumar struct { 2097813dd6fSViresh Kumar unsigned long min; 2107813dd6fSViresh Kumar unsigned long max; 2117813dd6fSViresh Kumar } *uV; 2127813dd6fSViresh Kumar 2137813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2147813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2157813dd6fSViresh Kumar return 0; 2167813dd6fSViresh Kumar 2177813dd6fSViresh Kumar /* Regulator may not be required for the device */ 21890e3577bSViresh Kumar if (!opp_table->regulators) 2197813dd6fSViresh Kumar goto put_opp_table; 2207813dd6fSViresh Kumar 22190e3577bSViresh Kumar count = opp_table->regulator_count; 22290e3577bSViresh Kumar 2237813dd6fSViresh Kumar uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 2247813dd6fSViresh Kumar if (!uV) 2257813dd6fSViresh Kumar goto put_opp_table; 2267813dd6fSViresh Kumar 2277813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 2287813dd6fSViresh Kumar 2297813dd6fSViresh Kumar for (i = 0; i < count; i++) { 2307813dd6fSViresh Kumar uV[i].min = ~0; 2317813dd6fSViresh Kumar uV[i].max = 0; 2327813dd6fSViresh Kumar 2337813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 2347813dd6fSViresh Kumar if (!opp->available) 2357813dd6fSViresh Kumar continue; 2367813dd6fSViresh Kumar 2377813dd6fSViresh Kumar if (opp->supplies[i].u_volt_min < uV[i].min) 2387813dd6fSViresh Kumar uV[i].min = opp->supplies[i].u_volt_min; 2397813dd6fSViresh Kumar if (opp->supplies[i].u_volt_max > uV[i].max) 2407813dd6fSViresh Kumar uV[i].max = opp->supplies[i].u_volt_max; 2417813dd6fSViresh Kumar } 2427813dd6fSViresh Kumar } 2437813dd6fSViresh Kumar 2447813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 2457813dd6fSViresh Kumar 2467813dd6fSViresh Kumar /* 2477813dd6fSViresh Kumar * The caller needs to ensure that opp_table (and hence the regulator) 2487813dd6fSViresh Kumar * isn't freed, while we are executing this routine. 2497813dd6fSViresh Kumar */ 2507813dd6fSViresh Kumar for (i = 0; i < count; i++) { 2517813dd6fSViresh Kumar reg = opp_table->regulators[i]; 2527813dd6fSViresh Kumar ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); 2537813dd6fSViresh Kumar if (ret > 0) 2547813dd6fSViresh Kumar latency_ns += ret * 1000; 2557813dd6fSViresh Kumar } 2567813dd6fSViresh Kumar 2577813dd6fSViresh Kumar kfree(uV); 2587813dd6fSViresh Kumar put_opp_table: 2597813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2607813dd6fSViresh Kumar 2617813dd6fSViresh Kumar return latency_ns; 2627813dd6fSViresh Kumar } 2637813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); 2647813dd6fSViresh Kumar 2657813dd6fSViresh Kumar /** 2667813dd6fSViresh Kumar * dev_pm_opp_get_max_transition_latency() - Get max transition latency in 2677813dd6fSViresh Kumar * nanoseconds 2687813dd6fSViresh Kumar * @dev: device for which we do this operation 2697813dd6fSViresh Kumar * 2707813dd6fSViresh Kumar * Return: This function returns the max transition latency, in nanoseconds, to 2717813dd6fSViresh Kumar * switch from one OPP to other. 2727813dd6fSViresh Kumar */ 2737813dd6fSViresh Kumar unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 2747813dd6fSViresh Kumar { 2757813dd6fSViresh Kumar return dev_pm_opp_get_max_volt_latency(dev) + 2767813dd6fSViresh Kumar dev_pm_opp_get_max_clock_latency(dev); 2777813dd6fSViresh Kumar } 2787813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 2797813dd6fSViresh Kumar 2807813dd6fSViresh Kumar /** 2817813dd6fSViresh Kumar * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 2827813dd6fSViresh Kumar * @dev: device for which we do this operation 2837813dd6fSViresh Kumar * 2847813dd6fSViresh Kumar * Return: This function returns the frequency of the OPP marked as suspend_opp 2857813dd6fSViresh Kumar * if one is available, else returns 0; 2867813dd6fSViresh Kumar */ 2877813dd6fSViresh Kumar unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 2887813dd6fSViresh Kumar { 2897813dd6fSViresh Kumar struct opp_table *opp_table; 2907813dd6fSViresh Kumar unsigned long freq = 0; 2917813dd6fSViresh Kumar 2927813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 2937813dd6fSViresh Kumar if (IS_ERR(opp_table)) 2947813dd6fSViresh Kumar return 0; 2957813dd6fSViresh Kumar 2967813dd6fSViresh Kumar if (opp_table->suspend_opp && opp_table->suspend_opp->available) 2977813dd6fSViresh Kumar freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 2987813dd6fSViresh Kumar 2997813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3007813dd6fSViresh Kumar 3017813dd6fSViresh Kumar return freq; 3027813dd6fSViresh Kumar } 3037813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 3047813dd6fSViresh Kumar 305a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table) 306a1e8c136SViresh Kumar { 307a1e8c136SViresh Kumar struct dev_pm_opp *opp; 308a1e8c136SViresh Kumar int count = 0; 309a1e8c136SViresh Kumar 310a1e8c136SViresh Kumar mutex_lock(&opp_table->lock); 311a1e8c136SViresh Kumar 312a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 313a1e8c136SViresh Kumar if (opp->available) 314a1e8c136SViresh Kumar count++; 315a1e8c136SViresh Kumar } 316a1e8c136SViresh Kumar 317a1e8c136SViresh Kumar mutex_unlock(&opp_table->lock); 318a1e8c136SViresh Kumar 319a1e8c136SViresh Kumar return count; 320a1e8c136SViresh Kumar } 321a1e8c136SViresh Kumar 3227813dd6fSViresh Kumar /** 3237813dd6fSViresh Kumar * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table 3247813dd6fSViresh Kumar * @dev: device for which we do this operation 3257813dd6fSViresh Kumar * 3267813dd6fSViresh Kumar * Return: This function returns the number of available opps if there are any, 3277813dd6fSViresh Kumar * else returns 0 if none or the corresponding error value. 3287813dd6fSViresh Kumar */ 3297813dd6fSViresh Kumar int dev_pm_opp_get_opp_count(struct device *dev) 3307813dd6fSViresh Kumar { 3317813dd6fSViresh Kumar struct opp_table *opp_table; 332a1e8c136SViresh Kumar int count; 3337813dd6fSViresh Kumar 3347813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3357813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 3367813dd6fSViresh Kumar count = PTR_ERR(opp_table); 337035ed072SFabio Estevam dev_dbg(dev, "%s: OPP table not found (%d)\n", 3387813dd6fSViresh Kumar __func__, count); 33909f662f9SViresh Kumar return count; 3407813dd6fSViresh Kumar } 3417813dd6fSViresh Kumar 342a1e8c136SViresh Kumar count = _get_opp_count(opp_table); 3437813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 3447813dd6fSViresh Kumar 3457813dd6fSViresh Kumar return count; 3467813dd6fSViresh Kumar } 3477813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); 3487813dd6fSViresh Kumar 3497813dd6fSViresh Kumar /** 3507813dd6fSViresh Kumar * dev_pm_opp_find_freq_exact() - search for an exact frequency 3517813dd6fSViresh Kumar * @dev: device for which we do this operation 3527813dd6fSViresh Kumar * @freq: frequency to search for 3537813dd6fSViresh Kumar * @available: true/false - match for available opp 3547813dd6fSViresh Kumar * 3557813dd6fSViresh Kumar * Return: Searches for exact match in the opp table and returns pointer to the 3567813dd6fSViresh Kumar * matching opp if found, else returns ERR_PTR in case of error and should 3577813dd6fSViresh Kumar * be handled using IS_ERR. Error return values can be: 3587813dd6fSViresh Kumar * EINVAL: for bad pointer 3597813dd6fSViresh Kumar * ERANGE: no match found for search 3607813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 3617813dd6fSViresh Kumar * 3627813dd6fSViresh Kumar * Note: available is a modifier for the search. if available=true, then the 3637813dd6fSViresh Kumar * match is for exact matching frequency and is available in the stored OPP 3647813dd6fSViresh Kumar * table. if false, the match is for exact frequency which is not available. 3657813dd6fSViresh Kumar * 3667813dd6fSViresh Kumar * This provides a mechanism to enable an opp which is not available currently 3677813dd6fSViresh Kumar * or the opposite as well. 3687813dd6fSViresh Kumar * 3697813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 3707813dd6fSViresh Kumar * use. 3717813dd6fSViresh Kumar */ 3727813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 3737813dd6fSViresh Kumar unsigned long freq, 3747813dd6fSViresh Kumar bool available) 3757813dd6fSViresh Kumar { 3767813dd6fSViresh Kumar struct opp_table *opp_table; 3777813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 3787813dd6fSViresh Kumar 3797813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 3807813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 3817813dd6fSViresh Kumar int r = PTR_ERR(opp_table); 3827813dd6fSViresh Kumar 3837813dd6fSViresh Kumar dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 3847813dd6fSViresh Kumar return ERR_PTR(r); 3857813dd6fSViresh Kumar } 3867813dd6fSViresh Kumar 3877813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 3887813dd6fSViresh Kumar 3897813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 3907813dd6fSViresh Kumar if (temp_opp->available == available && 3917813dd6fSViresh Kumar temp_opp->rate == freq) { 3927813dd6fSViresh Kumar opp = temp_opp; 3937813dd6fSViresh Kumar 3947813dd6fSViresh Kumar /* Increment the reference count of OPP */ 3957813dd6fSViresh Kumar dev_pm_opp_get(opp); 3967813dd6fSViresh Kumar break; 3977813dd6fSViresh Kumar } 3987813dd6fSViresh Kumar } 3997813dd6fSViresh Kumar 4007813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 4017813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4027813dd6fSViresh Kumar 4037813dd6fSViresh Kumar return opp; 4047813dd6fSViresh Kumar } 4057813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 4067813dd6fSViresh Kumar 4077813dd6fSViresh Kumar static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 4087813dd6fSViresh Kumar unsigned long *freq) 4097813dd6fSViresh Kumar { 4107813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 4117813dd6fSViresh Kumar 4127813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 4137813dd6fSViresh Kumar 4147813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 4157813dd6fSViresh Kumar if (temp_opp->available && temp_opp->rate >= *freq) { 4167813dd6fSViresh Kumar opp = temp_opp; 4177813dd6fSViresh Kumar *freq = opp->rate; 4187813dd6fSViresh Kumar 4197813dd6fSViresh Kumar /* Increment the reference count of OPP */ 4207813dd6fSViresh Kumar dev_pm_opp_get(opp); 4217813dd6fSViresh Kumar break; 4227813dd6fSViresh Kumar } 4237813dd6fSViresh Kumar } 4247813dd6fSViresh Kumar 4257813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 4267813dd6fSViresh Kumar 4277813dd6fSViresh Kumar return opp; 4287813dd6fSViresh Kumar } 4297813dd6fSViresh Kumar 4307813dd6fSViresh Kumar /** 4317813dd6fSViresh Kumar * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 4327813dd6fSViresh Kumar * @dev: device for which we do this operation 4337813dd6fSViresh Kumar * @freq: Start frequency 4347813dd6fSViresh Kumar * 4357813dd6fSViresh Kumar * Search for the matching ceil *available* OPP from a starting freq 4367813dd6fSViresh Kumar * for a device. 4377813dd6fSViresh Kumar * 4387813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 4397813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 4407813dd6fSViresh Kumar * values can be: 4417813dd6fSViresh Kumar * EINVAL: for bad pointer 4427813dd6fSViresh Kumar * ERANGE: no match found for search 4437813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 4447813dd6fSViresh Kumar * 4457813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 4467813dd6fSViresh Kumar * use. 4477813dd6fSViresh Kumar */ 4487813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 4497813dd6fSViresh Kumar unsigned long *freq) 4507813dd6fSViresh Kumar { 4517813dd6fSViresh Kumar struct opp_table *opp_table; 4527813dd6fSViresh Kumar struct dev_pm_opp *opp; 4537813dd6fSViresh Kumar 4547813dd6fSViresh Kumar if (!dev || !freq) { 4557813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 4567813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 4577813dd6fSViresh Kumar } 4587813dd6fSViresh Kumar 4597813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 4607813dd6fSViresh Kumar if (IS_ERR(opp_table)) 4617813dd6fSViresh Kumar return ERR_CAST(opp_table); 4627813dd6fSViresh Kumar 4637813dd6fSViresh Kumar opp = _find_freq_ceil(opp_table, freq); 4647813dd6fSViresh Kumar 4657813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 4667813dd6fSViresh Kumar 4677813dd6fSViresh Kumar return opp; 4687813dd6fSViresh Kumar } 4697813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 4707813dd6fSViresh Kumar 4717813dd6fSViresh Kumar /** 4727813dd6fSViresh Kumar * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 4737813dd6fSViresh Kumar * @dev: device for which we do this operation 4747813dd6fSViresh Kumar * @freq: Start frequency 4757813dd6fSViresh Kumar * 4767813dd6fSViresh Kumar * Search for the matching floor *available* OPP from a starting freq 4777813dd6fSViresh Kumar * for a device. 4787813dd6fSViresh Kumar * 4797813dd6fSViresh Kumar * Return: matching *opp and refreshes *freq accordingly, else returns 4807813dd6fSViresh Kumar * ERR_PTR in case of error and should be handled using IS_ERR. Error return 4817813dd6fSViresh Kumar * values can be: 4827813dd6fSViresh Kumar * EINVAL: for bad pointer 4837813dd6fSViresh Kumar * ERANGE: no match found for search 4847813dd6fSViresh Kumar * ENODEV: if device not found in list of registered devices 4857813dd6fSViresh Kumar * 4867813dd6fSViresh Kumar * The callers are required to call dev_pm_opp_put() for the returned OPP after 4877813dd6fSViresh Kumar * use. 4887813dd6fSViresh Kumar */ 4897813dd6fSViresh Kumar struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 4907813dd6fSViresh Kumar unsigned long *freq) 4917813dd6fSViresh Kumar { 4927813dd6fSViresh Kumar struct opp_table *opp_table; 4937813dd6fSViresh Kumar struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 4947813dd6fSViresh Kumar 4957813dd6fSViresh Kumar if (!dev || !freq) { 4967813dd6fSViresh Kumar dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 4977813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 4987813dd6fSViresh Kumar } 4997813dd6fSViresh Kumar 5007813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 5017813dd6fSViresh Kumar if (IS_ERR(opp_table)) 5027813dd6fSViresh Kumar return ERR_CAST(opp_table); 5037813dd6fSViresh Kumar 5047813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 5057813dd6fSViresh Kumar 5067813dd6fSViresh Kumar list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 5077813dd6fSViresh Kumar if (temp_opp->available) { 5087813dd6fSViresh Kumar /* go to the next node, before choosing prev */ 5097813dd6fSViresh Kumar if (temp_opp->rate > *freq) 5107813dd6fSViresh Kumar break; 5117813dd6fSViresh Kumar else 5127813dd6fSViresh Kumar opp = temp_opp; 5137813dd6fSViresh Kumar } 5147813dd6fSViresh Kumar } 5157813dd6fSViresh Kumar 5167813dd6fSViresh Kumar /* Increment the reference count of OPP */ 5177813dd6fSViresh Kumar if (!IS_ERR(opp)) 5187813dd6fSViresh Kumar dev_pm_opp_get(opp); 5197813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 5207813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 5217813dd6fSViresh Kumar 5227813dd6fSViresh Kumar if (!IS_ERR(opp)) 5237813dd6fSViresh Kumar *freq = opp->rate; 5247813dd6fSViresh Kumar 5257813dd6fSViresh Kumar return opp; 5267813dd6fSViresh Kumar } 5277813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 5287813dd6fSViresh Kumar 5292f36bde0SAndrew-sh.Cheng /** 5302f36bde0SAndrew-sh.Cheng * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for 5312f36bde0SAndrew-sh.Cheng * target voltage. 5322f36bde0SAndrew-sh.Cheng * @dev: Device for which we do this operation. 5332f36bde0SAndrew-sh.Cheng * @u_volt: Target voltage. 5342f36bde0SAndrew-sh.Cheng * 5352f36bde0SAndrew-sh.Cheng * Search for OPP with highest (ceil) frequency and has voltage <= u_volt. 5362f36bde0SAndrew-sh.Cheng * 5372f36bde0SAndrew-sh.Cheng * Return: matching *opp, else returns ERR_PTR in case of error which should be 5382f36bde0SAndrew-sh.Cheng * handled using IS_ERR. 5392f36bde0SAndrew-sh.Cheng * 5402f36bde0SAndrew-sh.Cheng * Error return values can be: 5412f36bde0SAndrew-sh.Cheng * EINVAL: bad parameters 5422f36bde0SAndrew-sh.Cheng * 5432f36bde0SAndrew-sh.Cheng * The callers are required to call dev_pm_opp_put() for the returned OPP after 5442f36bde0SAndrew-sh.Cheng * use. 5452f36bde0SAndrew-sh.Cheng */ 5462f36bde0SAndrew-sh.Cheng struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev, 5472f36bde0SAndrew-sh.Cheng unsigned long u_volt) 5482f36bde0SAndrew-sh.Cheng { 5492f36bde0SAndrew-sh.Cheng struct opp_table *opp_table; 5502f36bde0SAndrew-sh.Cheng struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 5512f36bde0SAndrew-sh.Cheng 5522f36bde0SAndrew-sh.Cheng if (!dev || !u_volt) { 5532f36bde0SAndrew-sh.Cheng dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__, 5542f36bde0SAndrew-sh.Cheng u_volt); 5552f36bde0SAndrew-sh.Cheng return ERR_PTR(-EINVAL); 5562f36bde0SAndrew-sh.Cheng } 5572f36bde0SAndrew-sh.Cheng 5582f36bde0SAndrew-sh.Cheng opp_table = _find_opp_table(dev); 5592f36bde0SAndrew-sh.Cheng if (IS_ERR(opp_table)) 5602f36bde0SAndrew-sh.Cheng return ERR_CAST(opp_table); 5612f36bde0SAndrew-sh.Cheng 5622f36bde0SAndrew-sh.Cheng mutex_lock(&opp_table->lock); 5632f36bde0SAndrew-sh.Cheng 5642f36bde0SAndrew-sh.Cheng list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 5652f36bde0SAndrew-sh.Cheng if (temp_opp->available) { 5662f36bde0SAndrew-sh.Cheng if (temp_opp->supplies[0].u_volt > u_volt) 5672f36bde0SAndrew-sh.Cheng break; 5682f36bde0SAndrew-sh.Cheng opp = temp_opp; 5692f36bde0SAndrew-sh.Cheng } 5702f36bde0SAndrew-sh.Cheng } 5712f36bde0SAndrew-sh.Cheng 5722f36bde0SAndrew-sh.Cheng /* Increment the reference count of OPP */ 5732f36bde0SAndrew-sh.Cheng if (!IS_ERR(opp)) 5742f36bde0SAndrew-sh.Cheng dev_pm_opp_get(opp); 5752f36bde0SAndrew-sh.Cheng 5762f36bde0SAndrew-sh.Cheng mutex_unlock(&opp_table->lock); 5772f36bde0SAndrew-sh.Cheng dev_pm_opp_put_opp_table(opp_table); 5782f36bde0SAndrew-sh.Cheng 5792f36bde0SAndrew-sh.Cheng return opp; 5802f36bde0SAndrew-sh.Cheng } 5812f36bde0SAndrew-sh.Cheng EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt); 5822f36bde0SAndrew-sh.Cheng 5837813dd6fSViresh Kumar static int _set_opp_voltage(struct device *dev, struct regulator *reg, 5847813dd6fSViresh Kumar struct dev_pm_opp_supply *supply) 5857813dd6fSViresh Kumar { 5867813dd6fSViresh Kumar int ret; 5877813dd6fSViresh Kumar 5887813dd6fSViresh Kumar /* Regulator not available for device */ 5897813dd6fSViresh Kumar if (IS_ERR(reg)) { 5907813dd6fSViresh Kumar dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 5917813dd6fSViresh Kumar PTR_ERR(reg)); 5927813dd6fSViresh Kumar return 0; 5937813dd6fSViresh Kumar } 5947813dd6fSViresh Kumar 5957813dd6fSViresh Kumar dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 5967813dd6fSViresh Kumar supply->u_volt_min, supply->u_volt, supply->u_volt_max); 5977813dd6fSViresh Kumar 5987813dd6fSViresh Kumar ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 5997813dd6fSViresh Kumar supply->u_volt, supply->u_volt_max); 6007813dd6fSViresh Kumar if (ret) 6017813dd6fSViresh Kumar dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 6027813dd6fSViresh Kumar __func__, supply->u_volt_min, supply->u_volt, 6037813dd6fSViresh Kumar supply->u_volt_max, ret); 6047813dd6fSViresh Kumar 6057813dd6fSViresh Kumar return ret; 6067813dd6fSViresh Kumar } 6077813dd6fSViresh Kumar 608285881b5SViresh Kumar static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 609285881b5SViresh Kumar unsigned long freq) 6107813dd6fSViresh Kumar { 6117813dd6fSViresh Kumar int ret; 6127813dd6fSViresh Kumar 6137813dd6fSViresh Kumar ret = clk_set_rate(clk, freq); 6147813dd6fSViresh Kumar if (ret) { 6157813dd6fSViresh Kumar dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 6167813dd6fSViresh Kumar ret); 6177813dd6fSViresh Kumar } 6187813dd6fSViresh Kumar 6197813dd6fSViresh Kumar return ret; 6207813dd6fSViresh Kumar } 6217813dd6fSViresh Kumar 6227813dd6fSViresh Kumar static int _generic_set_opp_regulator(const struct opp_table *opp_table, 6237813dd6fSViresh Kumar struct device *dev, 6247813dd6fSViresh Kumar unsigned long old_freq, 6257813dd6fSViresh Kumar unsigned long freq, 6267813dd6fSViresh Kumar struct dev_pm_opp_supply *old_supply, 6277813dd6fSViresh Kumar struct dev_pm_opp_supply *new_supply) 6287813dd6fSViresh Kumar { 6297813dd6fSViresh Kumar struct regulator *reg = opp_table->regulators[0]; 6307813dd6fSViresh Kumar int ret; 6317813dd6fSViresh Kumar 6327813dd6fSViresh Kumar /* This function only supports single regulator per device */ 6337813dd6fSViresh Kumar if (WARN_ON(opp_table->regulator_count > 1)) { 6347813dd6fSViresh Kumar dev_err(dev, "multiple regulators are not supported\n"); 6357813dd6fSViresh Kumar return -EINVAL; 6367813dd6fSViresh Kumar } 6377813dd6fSViresh Kumar 6387813dd6fSViresh Kumar /* Scaling up? Scale voltage before frequency */ 639c5c2a97bSWaldemar Rymarkiewicz if (freq >= old_freq) { 6407813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 6417813dd6fSViresh Kumar if (ret) 6427813dd6fSViresh Kumar goto restore_voltage; 6437813dd6fSViresh Kumar } 6447813dd6fSViresh Kumar 6457813dd6fSViresh Kumar /* Change frequency */ 646285881b5SViresh Kumar ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 6477813dd6fSViresh Kumar if (ret) 6487813dd6fSViresh Kumar goto restore_voltage; 6497813dd6fSViresh Kumar 6507813dd6fSViresh Kumar /* Scaling down? Scale voltage after frequency */ 6517813dd6fSViresh Kumar if (freq < old_freq) { 6527813dd6fSViresh Kumar ret = _set_opp_voltage(dev, reg, new_supply); 6537813dd6fSViresh Kumar if (ret) 6547813dd6fSViresh Kumar goto restore_freq; 6557813dd6fSViresh Kumar } 6567813dd6fSViresh Kumar 6577813dd6fSViresh Kumar return 0; 6587813dd6fSViresh Kumar 6597813dd6fSViresh Kumar restore_freq: 660285881b5SViresh Kumar if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq)) 6617813dd6fSViresh Kumar dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 6627813dd6fSViresh Kumar __func__, old_freq); 6637813dd6fSViresh Kumar restore_voltage: 6647813dd6fSViresh Kumar /* This shouldn't harm even if the voltages weren't updated earlier */ 6657813dd6fSViresh Kumar if (old_supply) 6667813dd6fSViresh Kumar _set_opp_voltage(dev, reg, old_supply); 6677813dd6fSViresh Kumar 6687813dd6fSViresh Kumar return ret; 6697813dd6fSViresh Kumar } 6707813dd6fSViresh Kumar 6717e535993SViresh Kumar static int _set_opp_custom(const struct opp_table *opp_table, 6727e535993SViresh Kumar struct device *dev, unsigned long old_freq, 6737e535993SViresh Kumar unsigned long freq, 6747e535993SViresh Kumar struct dev_pm_opp_supply *old_supply, 6757e535993SViresh Kumar struct dev_pm_opp_supply *new_supply) 6767e535993SViresh Kumar { 6777e535993SViresh Kumar struct dev_pm_set_opp_data *data; 6787e535993SViresh Kumar int size; 6797e535993SViresh Kumar 6807e535993SViresh Kumar data = opp_table->set_opp_data; 6817e535993SViresh Kumar data->regulators = opp_table->regulators; 6827e535993SViresh Kumar data->regulator_count = opp_table->regulator_count; 6837e535993SViresh Kumar data->clk = opp_table->clk; 6847e535993SViresh Kumar data->dev = dev; 6857e535993SViresh Kumar 6867e535993SViresh Kumar data->old_opp.rate = old_freq; 6877e535993SViresh Kumar size = sizeof(*old_supply) * opp_table->regulator_count; 6887e535993SViresh Kumar if (IS_ERR(old_supply)) 6897e535993SViresh Kumar memset(data->old_opp.supplies, 0, size); 6907e535993SViresh Kumar else 6917e535993SViresh Kumar memcpy(data->old_opp.supplies, old_supply, size); 6927e535993SViresh Kumar 6937e535993SViresh Kumar data->new_opp.rate = freq; 6947e535993SViresh Kumar memcpy(data->new_opp.supplies, new_supply, size); 6957e535993SViresh Kumar 6967e535993SViresh Kumar return opp_table->set_opp(data); 6977e535993SViresh Kumar } 6987e535993SViresh Kumar 699ca1b5d77SViresh Kumar /* This is only called for PM domain for now */ 700ca1b5d77SViresh Kumar static int _set_required_opps(struct device *dev, 701ca1b5d77SViresh Kumar struct opp_table *opp_table, 702ca1b5d77SViresh Kumar struct dev_pm_opp *opp) 703ca1b5d77SViresh Kumar { 704ca1b5d77SViresh Kumar struct opp_table **required_opp_tables = opp_table->required_opp_tables; 705ca1b5d77SViresh Kumar struct device **genpd_virt_devs = opp_table->genpd_virt_devs; 706ca1b5d77SViresh Kumar unsigned int pstate; 707ca1b5d77SViresh Kumar int i, ret = 0; 708ca1b5d77SViresh Kumar 709ca1b5d77SViresh Kumar if (!required_opp_tables) 710ca1b5d77SViresh Kumar return 0; 711ca1b5d77SViresh Kumar 712ca1b5d77SViresh Kumar /* Single genpd case */ 713ca1b5d77SViresh Kumar if (!genpd_virt_devs) { 714ca1b5d77SViresh Kumar pstate = opp->required_opps[0]->pstate; 715ca1b5d77SViresh Kumar ret = dev_pm_genpd_set_performance_state(dev, pstate); 716ca1b5d77SViresh Kumar if (ret) { 717ca1b5d77SViresh Kumar dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", 718ca1b5d77SViresh Kumar dev_name(dev), pstate, ret); 719ca1b5d77SViresh Kumar } 720ca1b5d77SViresh Kumar return ret; 721ca1b5d77SViresh Kumar } 722ca1b5d77SViresh Kumar 723ca1b5d77SViresh Kumar /* Multiple genpd case */ 724ca1b5d77SViresh Kumar 725ca1b5d77SViresh Kumar /* 726ca1b5d77SViresh Kumar * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 727ca1b5d77SViresh Kumar * after it is freed from another thread. 728ca1b5d77SViresh Kumar */ 729ca1b5d77SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 730ca1b5d77SViresh Kumar 731ca1b5d77SViresh Kumar for (i = 0; i < opp_table->required_opp_count; i++) { 732ca1b5d77SViresh Kumar pstate = opp->required_opps[i]->pstate; 733ca1b5d77SViresh Kumar 734ca1b5d77SViresh Kumar if (!genpd_virt_devs[i]) 735ca1b5d77SViresh Kumar continue; 736ca1b5d77SViresh Kumar 737ca1b5d77SViresh Kumar ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate); 738ca1b5d77SViresh Kumar if (ret) { 739ca1b5d77SViresh Kumar dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n", 740ca1b5d77SViresh Kumar dev_name(genpd_virt_devs[i]), pstate, ret); 741ca1b5d77SViresh Kumar break; 742ca1b5d77SViresh Kumar } 743ca1b5d77SViresh Kumar } 744ca1b5d77SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 745ca1b5d77SViresh Kumar 746ca1b5d77SViresh Kumar return ret; 747ca1b5d77SViresh Kumar } 748ca1b5d77SViresh Kumar 7497813dd6fSViresh Kumar /** 7507813dd6fSViresh Kumar * dev_pm_opp_set_rate() - Configure new OPP based on frequency 7517813dd6fSViresh Kumar * @dev: device for which we do this operation 7527813dd6fSViresh Kumar * @target_freq: frequency to achieve 7537813dd6fSViresh Kumar * 754*b3e3759eSStephen Boyd * This configures the power-supplies to the levels specified by the OPP 755*b3e3759eSStephen Boyd * corresponding to the target_freq, and programs the clock to a value <= 756*b3e3759eSStephen Boyd * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax 757*b3e3759eSStephen Boyd * provided by the opp, should have already rounded to the target OPP's 758*b3e3759eSStephen Boyd * frequency. 7597813dd6fSViresh Kumar */ 7607813dd6fSViresh Kumar int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 7617813dd6fSViresh Kumar { 7627813dd6fSViresh Kumar struct opp_table *opp_table; 763*b3e3759eSStephen Boyd unsigned long freq, old_freq, temp_freq; 7647813dd6fSViresh Kumar struct dev_pm_opp *old_opp, *opp; 7657813dd6fSViresh Kumar struct clk *clk; 7667e535993SViresh Kumar int ret; 7677813dd6fSViresh Kumar 7687813dd6fSViresh Kumar if (unlikely(!target_freq)) { 7697813dd6fSViresh Kumar dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, 7707813dd6fSViresh Kumar target_freq); 7717813dd6fSViresh Kumar return -EINVAL; 7727813dd6fSViresh Kumar } 7737813dd6fSViresh Kumar 7747813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 7757813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 7767813dd6fSViresh Kumar dev_err(dev, "%s: device opp doesn't exist\n", __func__); 7777813dd6fSViresh Kumar return PTR_ERR(opp_table); 7787813dd6fSViresh Kumar } 7797813dd6fSViresh Kumar 7807813dd6fSViresh Kumar clk = opp_table->clk; 7817813dd6fSViresh Kumar if (IS_ERR(clk)) { 7827813dd6fSViresh Kumar dev_err(dev, "%s: No clock available for the device\n", 7837813dd6fSViresh Kumar __func__); 7847813dd6fSViresh Kumar ret = PTR_ERR(clk); 7857813dd6fSViresh Kumar goto put_opp_table; 7867813dd6fSViresh Kumar } 7877813dd6fSViresh Kumar 7887813dd6fSViresh Kumar freq = clk_round_rate(clk, target_freq); 7897813dd6fSViresh Kumar if ((long)freq <= 0) 7907813dd6fSViresh Kumar freq = target_freq; 7917813dd6fSViresh Kumar 7927813dd6fSViresh Kumar old_freq = clk_get_rate(clk); 7937813dd6fSViresh Kumar 7947813dd6fSViresh Kumar /* Return early if nothing to do */ 7957813dd6fSViresh Kumar if (old_freq == freq) { 7967813dd6fSViresh Kumar dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", 7977813dd6fSViresh Kumar __func__, freq); 7987813dd6fSViresh Kumar ret = 0; 7997813dd6fSViresh Kumar goto put_opp_table; 8007813dd6fSViresh Kumar } 8017813dd6fSViresh Kumar 802*b3e3759eSStephen Boyd temp_freq = old_freq; 803*b3e3759eSStephen Boyd old_opp = _find_freq_ceil(opp_table, &temp_freq); 8047813dd6fSViresh Kumar if (IS_ERR(old_opp)) { 8057813dd6fSViresh Kumar dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", 8067813dd6fSViresh Kumar __func__, old_freq, PTR_ERR(old_opp)); 8077813dd6fSViresh Kumar } 8087813dd6fSViresh Kumar 809*b3e3759eSStephen Boyd temp_freq = freq; 810*b3e3759eSStephen Boyd opp = _find_freq_ceil(opp_table, &temp_freq); 8117813dd6fSViresh Kumar if (IS_ERR(opp)) { 8127813dd6fSViresh Kumar ret = PTR_ERR(opp); 8137813dd6fSViresh Kumar dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 8147813dd6fSViresh Kumar __func__, freq, ret); 8157813dd6fSViresh Kumar goto put_old_opp; 8167813dd6fSViresh Kumar } 8177813dd6fSViresh Kumar 8187813dd6fSViresh Kumar dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, 8197813dd6fSViresh Kumar old_freq, freq); 8207813dd6fSViresh Kumar 821ca1b5d77SViresh Kumar /* Scaling up? Configure required OPPs before frequency */ 822faef080fSViresh Kumar if (freq >= old_freq) { 823ca1b5d77SViresh Kumar ret = _set_required_opps(dev, opp_table, opp); 824ca1b5d77SViresh Kumar if (ret) 825ca1b5d77SViresh Kumar goto put_opp; 826ca1b5d77SViresh Kumar } 827ca1b5d77SViresh Kumar 8287e535993SViresh Kumar if (opp_table->set_opp) { 8297e535993SViresh Kumar ret = _set_opp_custom(opp_table, dev, old_freq, freq, 8307e535993SViresh Kumar IS_ERR(old_opp) ? NULL : old_opp->supplies, 8317e535993SViresh Kumar opp->supplies); 8327e535993SViresh Kumar } else if (opp_table->regulators) { 8337813dd6fSViresh Kumar ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq, 8347813dd6fSViresh Kumar IS_ERR(old_opp) ? NULL : old_opp->supplies, 8357813dd6fSViresh Kumar opp->supplies); 8367813dd6fSViresh Kumar } else { 8377813dd6fSViresh Kumar /* Only frequency scaling */ 838285881b5SViresh Kumar ret = _generic_set_opp_clk_only(dev, clk, freq); 8397813dd6fSViresh Kumar } 8407813dd6fSViresh Kumar 841ca1b5d77SViresh Kumar /* Scaling down? Configure required OPPs after frequency */ 842ca1b5d77SViresh Kumar if (!ret && freq < old_freq) { 843ca1b5d77SViresh Kumar ret = _set_required_opps(dev, opp_table, opp); 844ca1b5d77SViresh Kumar if (ret) 845ca1b5d77SViresh Kumar dev_err(dev, "Failed to set required opps: %d\n", ret); 846ca1b5d77SViresh Kumar } 847ca1b5d77SViresh Kumar 848ca1b5d77SViresh Kumar put_opp: 8497813dd6fSViresh Kumar dev_pm_opp_put(opp); 8507813dd6fSViresh Kumar put_old_opp: 8517813dd6fSViresh Kumar if (!IS_ERR(old_opp)) 8527813dd6fSViresh Kumar dev_pm_opp_put(old_opp); 8537813dd6fSViresh Kumar put_opp_table: 8547813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 8557813dd6fSViresh Kumar return ret; 8567813dd6fSViresh Kumar } 8577813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 8587813dd6fSViresh Kumar 8597813dd6fSViresh Kumar /* OPP-dev Helpers */ 8607813dd6fSViresh Kumar static void _remove_opp_dev(struct opp_device *opp_dev, 8617813dd6fSViresh Kumar struct opp_table *opp_table) 8627813dd6fSViresh Kumar { 8637813dd6fSViresh Kumar opp_debug_unregister(opp_dev, opp_table); 8647813dd6fSViresh Kumar list_del(&opp_dev->node); 8657813dd6fSViresh Kumar kfree(opp_dev); 8667813dd6fSViresh Kumar } 8677813dd6fSViresh Kumar 868283d55e6SViresh Kumar static struct opp_device *_add_opp_dev_unlocked(const struct device *dev, 8697813dd6fSViresh Kumar struct opp_table *opp_table) 8707813dd6fSViresh Kumar { 8717813dd6fSViresh Kumar struct opp_device *opp_dev; 8727813dd6fSViresh Kumar 8737813dd6fSViresh Kumar opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 8747813dd6fSViresh Kumar if (!opp_dev) 8757813dd6fSViresh Kumar return NULL; 8767813dd6fSViresh Kumar 8777813dd6fSViresh Kumar /* Initialize opp-dev */ 8787813dd6fSViresh Kumar opp_dev->dev = dev; 8793d255699SViresh Kumar 8807813dd6fSViresh Kumar list_add(&opp_dev->node, &opp_table->dev_list); 8817813dd6fSViresh Kumar 8827813dd6fSViresh Kumar /* Create debugfs entries for the opp_table */ 883a2dea4cbSGreg Kroah-Hartman opp_debug_register(opp_dev, opp_table); 884283d55e6SViresh Kumar 885283d55e6SViresh Kumar return opp_dev; 886283d55e6SViresh Kumar } 887283d55e6SViresh Kumar 888283d55e6SViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, 889283d55e6SViresh Kumar struct opp_table *opp_table) 890283d55e6SViresh Kumar { 891283d55e6SViresh Kumar struct opp_device *opp_dev; 892283d55e6SViresh Kumar 893283d55e6SViresh Kumar mutex_lock(&opp_table->lock); 894283d55e6SViresh Kumar opp_dev = _add_opp_dev_unlocked(dev, opp_table); 8953d255699SViresh Kumar mutex_unlock(&opp_table->lock); 8967813dd6fSViresh Kumar 8977813dd6fSViresh Kumar return opp_dev; 8987813dd6fSViresh Kumar } 8997813dd6fSViresh Kumar 900eb7c8743SViresh Kumar static struct opp_table *_allocate_opp_table(struct device *dev, int index) 9017813dd6fSViresh Kumar { 9027813dd6fSViresh Kumar struct opp_table *opp_table; 9037813dd6fSViresh Kumar struct opp_device *opp_dev; 9047813dd6fSViresh Kumar int ret; 9057813dd6fSViresh Kumar 9067813dd6fSViresh Kumar /* 9077813dd6fSViresh Kumar * Allocate a new OPP table. In the infrequent case where a new 9087813dd6fSViresh Kumar * device is needed to be added, we pay this penalty. 9097813dd6fSViresh Kumar */ 9107813dd6fSViresh Kumar opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 9117813dd6fSViresh Kumar if (!opp_table) 9127813dd6fSViresh Kumar return NULL; 9137813dd6fSViresh Kumar 9143d255699SViresh Kumar mutex_init(&opp_table->lock); 9154f018bc0SViresh Kumar mutex_init(&opp_table->genpd_virt_dev_lock); 9167813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->dev_list); 9177813dd6fSViresh Kumar 91846f48acaSViresh Kumar /* Mark regulator count uninitialized */ 91946f48acaSViresh Kumar opp_table->regulator_count = -1; 92046f48acaSViresh Kumar 9217813dd6fSViresh Kumar opp_dev = _add_opp_dev(dev, opp_table); 9227813dd6fSViresh Kumar if (!opp_dev) { 9237813dd6fSViresh Kumar kfree(opp_table); 9247813dd6fSViresh Kumar return NULL; 9257813dd6fSViresh Kumar } 9267813dd6fSViresh Kumar 927eb7c8743SViresh Kumar _of_init_opp_table(opp_table, dev, index); 9287813dd6fSViresh Kumar 9297813dd6fSViresh Kumar /* Find clk for the device */ 9307813dd6fSViresh Kumar opp_table->clk = clk_get(dev, NULL); 9317813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 9327813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 9337813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) 9347813dd6fSViresh Kumar dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, 9357813dd6fSViresh Kumar ret); 9367813dd6fSViresh Kumar } 9377813dd6fSViresh Kumar 9387813dd6fSViresh Kumar BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 9397813dd6fSViresh Kumar INIT_LIST_HEAD(&opp_table->opp_list); 9407813dd6fSViresh Kumar kref_init(&opp_table->kref); 9417813dd6fSViresh Kumar 9427813dd6fSViresh Kumar /* Secure the device table modification */ 9437813dd6fSViresh Kumar list_add(&opp_table->node, &opp_tables); 9447813dd6fSViresh Kumar return opp_table; 9457813dd6fSViresh Kumar } 9467813dd6fSViresh Kumar 9477813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table) 9487813dd6fSViresh Kumar { 9497813dd6fSViresh Kumar kref_get(&opp_table->kref); 9507813dd6fSViresh Kumar } 9517813dd6fSViresh Kumar 952eb7c8743SViresh Kumar static struct opp_table *_opp_get_opp_table(struct device *dev, int index) 9537813dd6fSViresh Kumar { 9547813dd6fSViresh Kumar struct opp_table *opp_table; 9557813dd6fSViresh Kumar 9567813dd6fSViresh Kumar /* Hold our table modification lock here */ 9577813dd6fSViresh Kumar mutex_lock(&opp_table_lock); 9587813dd6fSViresh Kumar 9597813dd6fSViresh Kumar opp_table = _find_opp_table_unlocked(dev); 9607813dd6fSViresh Kumar if (!IS_ERR(opp_table)) 9617813dd6fSViresh Kumar goto unlock; 9627813dd6fSViresh Kumar 963283d55e6SViresh Kumar opp_table = _managed_opp(dev, index); 964283d55e6SViresh Kumar if (opp_table) { 965283d55e6SViresh Kumar if (!_add_opp_dev_unlocked(dev, opp_table)) { 966283d55e6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 967283d55e6SViresh Kumar opp_table = NULL; 968283d55e6SViresh Kumar } 969283d55e6SViresh Kumar goto unlock; 970283d55e6SViresh Kumar } 971283d55e6SViresh Kumar 972eb7c8743SViresh Kumar opp_table = _allocate_opp_table(dev, index); 9737813dd6fSViresh Kumar 9747813dd6fSViresh Kumar unlock: 9757813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 9767813dd6fSViresh Kumar 9777813dd6fSViresh Kumar return opp_table; 9787813dd6fSViresh Kumar } 979eb7c8743SViresh Kumar 980eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 981eb7c8743SViresh Kumar { 982eb7c8743SViresh Kumar return _opp_get_opp_table(dev, 0); 983eb7c8743SViresh Kumar } 9847813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 9857813dd6fSViresh Kumar 986eb7c8743SViresh Kumar struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev, 987eb7c8743SViresh Kumar int index) 988eb7c8743SViresh Kumar { 989eb7c8743SViresh Kumar return _opp_get_opp_table(dev, index); 990eb7c8743SViresh Kumar } 991eb7c8743SViresh Kumar 9927813dd6fSViresh Kumar static void _opp_table_kref_release(struct kref *kref) 9937813dd6fSViresh Kumar { 9947813dd6fSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 995cdd6ed90SViresh Kumar struct opp_device *opp_dev, *temp; 9967813dd6fSViresh Kumar 9975d6d106fSViresh Kumar _of_clear_opp_table(opp_table); 9985d6d106fSViresh Kumar 9997813dd6fSViresh Kumar /* Release clk */ 10007813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 10017813dd6fSViresh Kumar clk_put(opp_table->clk); 10027813dd6fSViresh Kumar 1003cdd6ed90SViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 1004cdd6ed90SViresh Kumar 1005cdd6ed90SViresh Kumar list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 10063d255699SViresh Kumar /* 1007cdd6ed90SViresh Kumar * The OPP table is getting removed, drop the performance state 1008cdd6ed90SViresh Kumar * constraints. 10093d255699SViresh Kumar */ 1010cdd6ed90SViresh Kumar if (opp_table->genpd_performance_state) 1011cdd6ed90SViresh Kumar dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 10127813dd6fSViresh Kumar 10137813dd6fSViresh Kumar _remove_opp_dev(opp_dev, opp_table); 1014cdd6ed90SViresh Kumar } 10157813dd6fSViresh Kumar 10164f018bc0SViresh Kumar mutex_destroy(&opp_table->genpd_virt_dev_lock); 10177813dd6fSViresh Kumar mutex_destroy(&opp_table->lock); 10187813dd6fSViresh Kumar list_del(&opp_table->node); 10197813dd6fSViresh Kumar kfree(opp_table); 10207813dd6fSViresh Kumar 10217813dd6fSViresh Kumar mutex_unlock(&opp_table_lock); 10227813dd6fSViresh Kumar } 10237813dd6fSViresh Kumar 1024d0e8ae6cSViresh Kumar void _opp_remove_all_static(struct opp_table *opp_table) 1025d0e8ae6cSViresh Kumar { 1026d0e8ae6cSViresh Kumar struct dev_pm_opp *opp, *tmp; 1027d0e8ae6cSViresh Kumar 1028d0e8ae6cSViresh Kumar list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { 1029d0e8ae6cSViresh Kumar if (!opp->dynamic) 1030d0e8ae6cSViresh Kumar dev_pm_opp_put(opp); 1031d0e8ae6cSViresh Kumar } 1032d0e8ae6cSViresh Kumar 1033d0e8ae6cSViresh Kumar opp_table->parsed_static_opps = false; 1034d0e8ae6cSViresh Kumar } 1035d0e8ae6cSViresh Kumar 1036d0e8ae6cSViresh Kumar static void _opp_table_list_kref_release(struct kref *kref) 1037d0e8ae6cSViresh Kumar { 1038d0e8ae6cSViresh Kumar struct opp_table *opp_table = container_of(kref, struct opp_table, 1039d0e8ae6cSViresh Kumar list_kref); 1040d0e8ae6cSViresh Kumar 1041d0e8ae6cSViresh Kumar _opp_remove_all_static(opp_table); 1042d0e8ae6cSViresh Kumar mutex_unlock(&opp_table_lock); 1043d0e8ae6cSViresh Kumar } 1044d0e8ae6cSViresh Kumar 1045d0e8ae6cSViresh Kumar void _put_opp_list_kref(struct opp_table *opp_table) 1046d0e8ae6cSViresh Kumar { 1047d0e8ae6cSViresh Kumar kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release, 1048d0e8ae6cSViresh Kumar &opp_table_lock); 1049d0e8ae6cSViresh Kumar } 1050d0e8ae6cSViresh Kumar 10517813dd6fSViresh Kumar void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 10527813dd6fSViresh Kumar { 10537813dd6fSViresh Kumar kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 10547813dd6fSViresh Kumar &opp_table_lock); 10557813dd6fSViresh Kumar } 10567813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 10577813dd6fSViresh Kumar 10587813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp) 10597813dd6fSViresh Kumar { 10607813dd6fSViresh Kumar kfree(opp); 10617813dd6fSViresh Kumar } 10627813dd6fSViresh Kumar 10631690d8bbSViresh Kumar static void _opp_kref_release(struct dev_pm_opp *opp, 10641690d8bbSViresh Kumar struct opp_table *opp_table) 10657813dd6fSViresh Kumar { 10667813dd6fSViresh Kumar /* 10677813dd6fSViresh Kumar * Notify the changes in the availability of the operable 10687813dd6fSViresh Kumar * frequency/voltage list. 10697813dd6fSViresh Kumar */ 10707813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 1071da544b61SViresh Kumar _of_opp_free_required_opps(opp_table, opp); 10727813dd6fSViresh Kumar opp_debug_remove_one(opp); 10737813dd6fSViresh Kumar list_del(&opp->node); 10747813dd6fSViresh Kumar kfree(opp); 10751690d8bbSViresh Kumar } 10767813dd6fSViresh Kumar 10771690d8bbSViresh Kumar static void _opp_kref_release_unlocked(struct kref *kref) 10781690d8bbSViresh Kumar { 10791690d8bbSViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 10801690d8bbSViresh Kumar struct opp_table *opp_table = opp->opp_table; 10811690d8bbSViresh Kumar 10821690d8bbSViresh Kumar _opp_kref_release(opp, opp_table); 10831690d8bbSViresh Kumar } 10841690d8bbSViresh Kumar 10851690d8bbSViresh Kumar static void _opp_kref_release_locked(struct kref *kref) 10861690d8bbSViresh Kumar { 10871690d8bbSViresh Kumar struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 10881690d8bbSViresh Kumar struct opp_table *opp_table = opp->opp_table; 10891690d8bbSViresh Kumar 10901690d8bbSViresh Kumar _opp_kref_release(opp, opp_table); 10917813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 10927813dd6fSViresh Kumar } 10937813dd6fSViresh Kumar 1094a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp) 10957813dd6fSViresh Kumar { 10967813dd6fSViresh Kumar kref_get(&opp->kref); 10977813dd6fSViresh Kumar } 10987813dd6fSViresh Kumar 10997813dd6fSViresh Kumar void dev_pm_opp_put(struct dev_pm_opp *opp) 11007813dd6fSViresh Kumar { 11011690d8bbSViresh Kumar kref_put_mutex(&opp->kref, _opp_kref_release_locked, 11021690d8bbSViresh Kumar &opp->opp_table->lock); 11037813dd6fSViresh Kumar } 11047813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put); 11057813dd6fSViresh Kumar 11061690d8bbSViresh Kumar static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp) 11071690d8bbSViresh Kumar { 11081690d8bbSViresh Kumar kref_put(&opp->kref, _opp_kref_release_unlocked); 11091690d8bbSViresh Kumar } 11101690d8bbSViresh Kumar 11117813dd6fSViresh Kumar /** 11127813dd6fSViresh Kumar * dev_pm_opp_remove() - Remove an OPP from OPP table 11137813dd6fSViresh Kumar * @dev: device for which we do this operation 11147813dd6fSViresh Kumar * @freq: OPP to remove with matching 'freq' 11157813dd6fSViresh Kumar * 11167813dd6fSViresh Kumar * This function removes an opp from the opp table. 11177813dd6fSViresh Kumar */ 11187813dd6fSViresh Kumar void dev_pm_opp_remove(struct device *dev, unsigned long freq) 11197813dd6fSViresh Kumar { 11207813dd6fSViresh Kumar struct dev_pm_opp *opp; 11217813dd6fSViresh Kumar struct opp_table *opp_table; 11227813dd6fSViresh Kumar bool found = false; 11237813dd6fSViresh Kumar 11247813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 11257813dd6fSViresh Kumar if (IS_ERR(opp_table)) 11267813dd6fSViresh Kumar return; 11277813dd6fSViresh Kumar 11287813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 11297813dd6fSViresh Kumar 11307813dd6fSViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 11317813dd6fSViresh Kumar if (opp->rate == freq) { 11327813dd6fSViresh Kumar found = true; 11337813dd6fSViresh Kumar break; 11347813dd6fSViresh Kumar } 11357813dd6fSViresh Kumar } 11367813dd6fSViresh Kumar 11377813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 11387813dd6fSViresh Kumar 11397813dd6fSViresh Kumar if (found) { 11407813dd6fSViresh Kumar dev_pm_opp_put(opp); 11410ad8c623SViresh Kumar 11420ad8c623SViresh Kumar /* Drop the reference taken by dev_pm_opp_add() */ 11430ad8c623SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 11447813dd6fSViresh Kumar } else { 11457813dd6fSViresh Kumar dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 11467813dd6fSViresh Kumar __func__, freq); 11477813dd6fSViresh Kumar } 11487813dd6fSViresh Kumar 11490ad8c623SViresh Kumar /* Drop the reference taken by _find_opp_table() */ 11507813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 11517813dd6fSViresh Kumar } 11527813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 11537813dd6fSViresh Kumar 11541690d8bbSViresh Kumar /** 11551690d8bbSViresh Kumar * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 11561690d8bbSViresh Kumar * @dev: device for which we do this operation 11571690d8bbSViresh Kumar * 11581690d8bbSViresh Kumar * This function removes all dynamically created OPPs from the opp table. 11591690d8bbSViresh Kumar */ 11601690d8bbSViresh Kumar void dev_pm_opp_remove_all_dynamic(struct device *dev) 11611690d8bbSViresh Kumar { 11621690d8bbSViresh Kumar struct opp_table *opp_table; 11631690d8bbSViresh Kumar struct dev_pm_opp *opp, *temp; 11641690d8bbSViresh Kumar int count = 0; 11651690d8bbSViresh Kumar 11661690d8bbSViresh Kumar opp_table = _find_opp_table(dev); 11671690d8bbSViresh Kumar if (IS_ERR(opp_table)) 11681690d8bbSViresh Kumar return; 11691690d8bbSViresh Kumar 11701690d8bbSViresh Kumar mutex_lock(&opp_table->lock); 11711690d8bbSViresh Kumar list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) { 11721690d8bbSViresh Kumar if (opp->dynamic) { 11731690d8bbSViresh Kumar dev_pm_opp_put_unlocked(opp); 11741690d8bbSViresh Kumar count++; 11751690d8bbSViresh Kumar } 11761690d8bbSViresh Kumar } 11771690d8bbSViresh Kumar mutex_unlock(&opp_table->lock); 11781690d8bbSViresh Kumar 11791690d8bbSViresh Kumar /* Drop the references taken by dev_pm_opp_add() */ 11801690d8bbSViresh Kumar while (count--) 11811690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 11821690d8bbSViresh Kumar 11831690d8bbSViresh Kumar /* Drop the reference taken by _find_opp_table() */ 11841690d8bbSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 11851690d8bbSViresh Kumar } 11861690d8bbSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 11871690d8bbSViresh Kumar 11887813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *table) 11897813dd6fSViresh Kumar { 11907813dd6fSViresh Kumar struct dev_pm_opp *opp; 11917813dd6fSViresh Kumar int count, supply_size; 11927813dd6fSViresh Kumar 11937813dd6fSViresh Kumar /* Allocate space for at least one supply */ 119446f48acaSViresh Kumar count = table->regulator_count > 0 ? table->regulator_count : 1; 11957813dd6fSViresh Kumar supply_size = sizeof(*opp->supplies) * count; 11967813dd6fSViresh Kumar 11977813dd6fSViresh Kumar /* allocate new OPP node and supplies structures */ 11987813dd6fSViresh Kumar opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); 11997813dd6fSViresh Kumar if (!opp) 12007813dd6fSViresh Kumar return NULL; 12017813dd6fSViresh Kumar 12027813dd6fSViresh Kumar /* Put the supplies at the end of the OPP structure as an empty array */ 12037813dd6fSViresh Kumar opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 12047813dd6fSViresh Kumar INIT_LIST_HEAD(&opp->node); 12057813dd6fSViresh Kumar 12067813dd6fSViresh Kumar return opp; 12077813dd6fSViresh Kumar } 12087813dd6fSViresh Kumar 12097813dd6fSViresh Kumar static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 12107813dd6fSViresh Kumar struct opp_table *opp_table) 12117813dd6fSViresh Kumar { 12127813dd6fSViresh Kumar struct regulator *reg; 12137813dd6fSViresh Kumar int i; 12147813dd6fSViresh Kumar 121590e3577bSViresh Kumar if (!opp_table->regulators) 121690e3577bSViresh Kumar return true; 121790e3577bSViresh Kumar 12187813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) { 12197813dd6fSViresh Kumar reg = opp_table->regulators[i]; 12207813dd6fSViresh Kumar 12217813dd6fSViresh Kumar if (!regulator_is_supported_voltage(reg, 12227813dd6fSViresh Kumar opp->supplies[i].u_volt_min, 12237813dd6fSViresh Kumar opp->supplies[i].u_volt_max)) { 12247813dd6fSViresh Kumar pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 12257813dd6fSViresh Kumar __func__, opp->supplies[i].u_volt_min, 12267813dd6fSViresh Kumar opp->supplies[i].u_volt_max); 12277813dd6fSViresh Kumar return false; 12287813dd6fSViresh Kumar } 12297813dd6fSViresh Kumar } 12307813dd6fSViresh Kumar 12317813dd6fSViresh Kumar return true; 12327813dd6fSViresh Kumar } 12337813dd6fSViresh Kumar 1234a1e8c136SViresh Kumar static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1235a1e8c136SViresh Kumar struct opp_table *opp_table, 1236a1e8c136SViresh Kumar struct list_head **head) 1237a1e8c136SViresh Kumar { 1238a1e8c136SViresh Kumar struct dev_pm_opp *opp; 1239a1e8c136SViresh Kumar 1240a1e8c136SViresh Kumar /* 1241a1e8c136SViresh Kumar * Insert new OPP in order of increasing frequency and discard if 1242a1e8c136SViresh Kumar * already present. 1243a1e8c136SViresh Kumar * 1244a1e8c136SViresh Kumar * Need to use &opp_table->opp_list in the condition part of the 'for' 1245a1e8c136SViresh Kumar * loop, don't replace it with head otherwise it will become an infinite 1246a1e8c136SViresh Kumar * loop. 1247a1e8c136SViresh Kumar */ 1248a1e8c136SViresh Kumar list_for_each_entry(opp, &opp_table->opp_list, node) { 1249a1e8c136SViresh Kumar if (new_opp->rate > opp->rate) { 1250a1e8c136SViresh Kumar *head = &opp->node; 1251a1e8c136SViresh Kumar continue; 1252a1e8c136SViresh Kumar } 1253a1e8c136SViresh Kumar 1254a1e8c136SViresh Kumar if (new_opp->rate < opp->rate) 1255a1e8c136SViresh Kumar return 0; 1256a1e8c136SViresh Kumar 1257a1e8c136SViresh Kumar /* Duplicate OPPs */ 1258a1e8c136SViresh Kumar dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1259a1e8c136SViresh Kumar __func__, opp->rate, opp->supplies[0].u_volt, 1260a1e8c136SViresh Kumar opp->available, new_opp->rate, 1261a1e8c136SViresh Kumar new_opp->supplies[0].u_volt, new_opp->available); 1262a1e8c136SViresh Kumar 1263a1e8c136SViresh Kumar /* Should we compare voltages for all regulators here ? */ 1264a1e8c136SViresh Kumar return opp->available && 1265a1e8c136SViresh Kumar new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1266a1e8c136SViresh Kumar } 1267a1e8c136SViresh Kumar 1268a1e8c136SViresh Kumar return 0; 1269a1e8c136SViresh Kumar } 1270a1e8c136SViresh Kumar 12717813dd6fSViresh Kumar /* 12727813dd6fSViresh Kumar * Returns: 12737813dd6fSViresh Kumar * 0: On success. And appropriate error message for duplicate OPPs. 12747813dd6fSViresh Kumar * -EBUSY: For OPP with same freq/volt and is available. The callers of 12757813dd6fSViresh Kumar * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 12767813dd6fSViresh Kumar * sure we don't print error messages unnecessarily if different parts of 12777813dd6fSViresh Kumar * kernel try to initialize the OPP table. 12787813dd6fSViresh Kumar * -EEXIST: For OPP with same freq but different volt or is unavailable. This 12797813dd6fSViresh Kumar * should be considered an error by the callers of _opp_add(). 12807813dd6fSViresh Kumar */ 12817813dd6fSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1282a1e8c136SViresh Kumar struct opp_table *opp_table, bool rate_not_available) 12837813dd6fSViresh Kumar { 12847813dd6fSViresh Kumar struct list_head *head; 12857813dd6fSViresh Kumar int ret; 12867813dd6fSViresh Kumar 12877813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 12887813dd6fSViresh Kumar head = &opp_table->opp_list; 12897813dd6fSViresh Kumar 1290a1e8c136SViresh Kumar if (likely(!rate_not_available)) { 1291a1e8c136SViresh Kumar ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1292a1e8c136SViresh Kumar if (ret) { 12937813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 12947813dd6fSViresh Kumar return ret; 12957813dd6fSViresh Kumar } 1296a1e8c136SViresh Kumar } 12977813dd6fSViresh Kumar 12987813dd6fSViresh Kumar list_add(&new_opp->node, head); 12997813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 13007813dd6fSViresh Kumar 13017813dd6fSViresh Kumar new_opp->opp_table = opp_table; 13027813dd6fSViresh Kumar kref_init(&new_opp->kref); 13037813dd6fSViresh Kumar 1304a2dea4cbSGreg Kroah-Hartman opp_debug_create_one(new_opp, opp_table); 13057813dd6fSViresh Kumar 13067813dd6fSViresh Kumar if (!_opp_supported_by_regulators(new_opp, opp_table)) { 13077813dd6fSViresh Kumar new_opp->available = false; 13087813dd6fSViresh Kumar dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 13097813dd6fSViresh Kumar __func__, new_opp->rate); 13107813dd6fSViresh Kumar } 13117813dd6fSViresh Kumar 13127813dd6fSViresh Kumar return 0; 13137813dd6fSViresh Kumar } 13147813dd6fSViresh Kumar 13157813dd6fSViresh Kumar /** 13167813dd6fSViresh Kumar * _opp_add_v1() - Allocate a OPP based on v1 bindings. 13177813dd6fSViresh Kumar * @opp_table: OPP table 13187813dd6fSViresh Kumar * @dev: device for which we do this operation 13197813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 13207813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 13217813dd6fSViresh Kumar * @dynamic: Dynamically added OPPs. 13227813dd6fSViresh Kumar * 13237813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 13247813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 13257813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 13267813dd6fSViresh Kumar * 13277813dd6fSViresh Kumar * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 13287813dd6fSViresh Kumar * and freed by dev_pm_opp_of_remove_table. 13297813dd6fSViresh Kumar * 13307813dd6fSViresh Kumar * Return: 13317813dd6fSViresh Kumar * 0 On success OR 13327813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 13337813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 13347813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 13357813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 13367813dd6fSViresh Kumar */ 13377813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 13387813dd6fSViresh Kumar unsigned long freq, long u_volt, bool dynamic) 13397813dd6fSViresh Kumar { 13407813dd6fSViresh Kumar struct dev_pm_opp *new_opp; 13417813dd6fSViresh Kumar unsigned long tol; 13427813dd6fSViresh Kumar int ret; 13437813dd6fSViresh Kumar 13447813dd6fSViresh Kumar new_opp = _opp_allocate(opp_table); 13457813dd6fSViresh Kumar if (!new_opp) 13467813dd6fSViresh Kumar return -ENOMEM; 13477813dd6fSViresh Kumar 13487813dd6fSViresh Kumar /* populate the opp table */ 13497813dd6fSViresh Kumar new_opp->rate = freq; 13507813dd6fSViresh Kumar tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 13517813dd6fSViresh Kumar new_opp->supplies[0].u_volt = u_volt; 13527813dd6fSViresh Kumar new_opp->supplies[0].u_volt_min = u_volt - tol; 13537813dd6fSViresh Kumar new_opp->supplies[0].u_volt_max = u_volt + tol; 13547813dd6fSViresh Kumar new_opp->available = true; 13557813dd6fSViresh Kumar new_opp->dynamic = dynamic; 13567813dd6fSViresh Kumar 1357a1e8c136SViresh Kumar ret = _opp_add(dev, new_opp, opp_table, false); 13587813dd6fSViresh Kumar if (ret) { 13597813dd6fSViresh Kumar /* Don't return error for duplicate OPPs */ 13607813dd6fSViresh Kumar if (ret == -EBUSY) 13617813dd6fSViresh Kumar ret = 0; 13627813dd6fSViresh Kumar goto free_opp; 13637813dd6fSViresh Kumar } 13647813dd6fSViresh Kumar 13657813dd6fSViresh Kumar /* 13667813dd6fSViresh Kumar * Notify the changes in the availability of the operable 13677813dd6fSViresh Kumar * frequency/voltage list. 13687813dd6fSViresh Kumar */ 13697813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 13707813dd6fSViresh Kumar return 0; 13717813dd6fSViresh Kumar 13727813dd6fSViresh Kumar free_opp: 13737813dd6fSViresh Kumar _opp_free(new_opp); 13747813dd6fSViresh Kumar 13757813dd6fSViresh Kumar return ret; 13767813dd6fSViresh Kumar } 13777813dd6fSViresh Kumar 13787813dd6fSViresh Kumar /** 13797813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw() - Set supported platforms 13807813dd6fSViresh Kumar * @dev: Device for which supported-hw has to be set. 13817813dd6fSViresh Kumar * @versions: Array of hierarchy of versions to match. 13827813dd6fSViresh Kumar * @count: Number of elements in the array. 13837813dd6fSViresh Kumar * 13847813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 13857813dd6fSViresh Kumar * specify the hierarchy of versions it supports. OPP layer will then enable 13867813dd6fSViresh Kumar * OPPs, which are available for those versions, based on its 'opp-supported-hw' 13877813dd6fSViresh Kumar * property. 13887813dd6fSViresh Kumar */ 13897813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, 13907813dd6fSViresh Kumar const u32 *versions, unsigned int count) 13917813dd6fSViresh Kumar { 13927813dd6fSViresh Kumar struct opp_table *opp_table; 13937813dd6fSViresh Kumar 13947813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 13957813dd6fSViresh Kumar if (!opp_table) 13967813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 13977813dd6fSViresh Kumar 13987813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 13997813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 14007813dd6fSViresh Kumar 140125419de1SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 140225419de1SViresh Kumar if (opp_table->supported_hw) 140325419de1SViresh Kumar return opp_table; 14047813dd6fSViresh Kumar 14057813dd6fSViresh Kumar opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 14067813dd6fSViresh Kumar GFP_KERNEL); 14077813dd6fSViresh Kumar if (!opp_table->supported_hw) { 140825419de1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 140925419de1SViresh Kumar return ERR_PTR(-ENOMEM); 14107813dd6fSViresh Kumar } 14117813dd6fSViresh Kumar 14127813dd6fSViresh Kumar opp_table->supported_hw_count = count; 14137813dd6fSViresh Kumar 14147813dd6fSViresh Kumar return opp_table; 14157813dd6fSViresh Kumar } 14167813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); 14177813dd6fSViresh Kumar 14187813dd6fSViresh Kumar /** 14197813dd6fSViresh Kumar * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw 14207813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). 14217813dd6fSViresh Kumar * 14227813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 14237813dd6fSViresh Kumar * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure 14247813dd6fSViresh Kumar * will not be freed. 14257813dd6fSViresh Kumar */ 14267813dd6fSViresh Kumar void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) 14277813dd6fSViresh Kumar { 14287813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 14297813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 14307813dd6fSViresh Kumar 14317813dd6fSViresh Kumar kfree(opp_table->supported_hw); 14327813dd6fSViresh Kumar opp_table->supported_hw = NULL; 14337813dd6fSViresh Kumar opp_table->supported_hw_count = 0; 14347813dd6fSViresh Kumar 14357813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 14367813dd6fSViresh Kumar } 14377813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); 14387813dd6fSViresh Kumar 14397813dd6fSViresh Kumar /** 14407813dd6fSViresh Kumar * dev_pm_opp_set_prop_name() - Set prop-extn name 14417813dd6fSViresh Kumar * @dev: Device for which the prop-name has to be set. 14427813dd6fSViresh Kumar * @name: name to postfix to properties. 14437813dd6fSViresh Kumar * 14447813dd6fSViresh Kumar * This is required only for the V2 bindings, and it enables a platform to 14457813dd6fSViresh Kumar * specify the extn to be used for certain property names. The properties to 14467813dd6fSViresh Kumar * which the extension will apply are opp-microvolt and opp-microamp. OPP core 14477813dd6fSViresh Kumar * should postfix the property name with -<name> while looking for them. 14487813dd6fSViresh Kumar */ 14497813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 14507813dd6fSViresh Kumar { 14517813dd6fSViresh Kumar struct opp_table *opp_table; 14527813dd6fSViresh Kumar 14537813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 14547813dd6fSViresh Kumar if (!opp_table) 14557813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 14567813dd6fSViresh Kumar 14577813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 14587813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 14597813dd6fSViresh Kumar 1460878ec1a9SViresh Kumar /* Another CPU that shares the OPP table has set the property ? */ 1461878ec1a9SViresh Kumar if (opp_table->prop_name) 1462878ec1a9SViresh Kumar return opp_table; 14637813dd6fSViresh Kumar 14647813dd6fSViresh Kumar opp_table->prop_name = kstrdup(name, GFP_KERNEL); 14657813dd6fSViresh Kumar if (!opp_table->prop_name) { 1466878ec1a9SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 1467878ec1a9SViresh Kumar return ERR_PTR(-ENOMEM); 14687813dd6fSViresh Kumar } 14697813dd6fSViresh Kumar 14707813dd6fSViresh Kumar return opp_table; 14717813dd6fSViresh Kumar } 14727813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); 14737813dd6fSViresh Kumar 14747813dd6fSViresh Kumar /** 14757813dd6fSViresh Kumar * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name 14767813dd6fSViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). 14777813dd6fSViresh Kumar * 14787813dd6fSViresh Kumar * This is required only for the V2 bindings, and is called for a matching 14797813dd6fSViresh Kumar * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure 14807813dd6fSViresh Kumar * will not be freed. 14817813dd6fSViresh Kumar */ 14827813dd6fSViresh Kumar void dev_pm_opp_put_prop_name(struct opp_table *opp_table) 14837813dd6fSViresh Kumar { 14847813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 14857813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 14867813dd6fSViresh Kumar 14877813dd6fSViresh Kumar kfree(opp_table->prop_name); 14887813dd6fSViresh Kumar opp_table->prop_name = NULL; 14897813dd6fSViresh Kumar 14907813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 14917813dd6fSViresh Kumar } 14927813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); 14937813dd6fSViresh Kumar 14947813dd6fSViresh Kumar static int _allocate_set_opp_data(struct opp_table *opp_table) 14957813dd6fSViresh Kumar { 14967813dd6fSViresh Kumar struct dev_pm_set_opp_data *data; 14977813dd6fSViresh Kumar int len, count = opp_table->regulator_count; 14987813dd6fSViresh Kumar 149990e3577bSViresh Kumar if (WARN_ON(!opp_table->regulators)) 15007813dd6fSViresh Kumar return -EINVAL; 15017813dd6fSViresh Kumar 15027813dd6fSViresh Kumar /* space for set_opp_data */ 15037813dd6fSViresh Kumar len = sizeof(*data); 15047813dd6fSViresh Kumar 15057813dd6fSViresh Kumar /* space for old_opp.supplies and new_opp.supplies */ 15067813dd6fSViresh Kumar len += 2 * sizeof(struct dev_pm_opp_supply) * count; 15077813dd6fSViresh Kumar 15087813dd6fSViresh Kumar data = kzalloc(len, GFP_KERNEL); 15097813dd6fSViresh Kumar if (!data) 15107813dd6fSViresh Kumar return -ENOMEM; 15117813dd6fSViresh Kumar 15127813dd6fSViresh Kumar data->old_opp.supplies = (void *)(data + 1); 15137813dd6fSViresh Kumar data->new_opp.supplies = data->old_opp.supplies + count; 15147813dd6fSViresh Kumar 15157813dd6fSViresh Kumar opp_table->set_opp_data = data; 15167813dd6fSViresh Kumar 15177813dd6fSViresh Kumar return 0; 15187813dd6fSViresh Kumar } 15197813dd6fSViresh Kumar 15207813dd6fSViresh Kumar static void _free_set_opp_data(struct opp_table *opp_table) 15217813dd6fSViresh Kumar { 15227813dd6fSViresh Kumar kfree(opp_table->set_opp_data); 15237813dd6fSViresh Kumar opp_table->set_opp_data = NULL; 15247813dd6fSViresh Kumar } 15257813dd6fSViresh Kumar 15267813dd6fSViresh Kumar /** 15277813dd6fSViresh Kumar * dev_pm_opp_set_regulators() - Set regulator names for the device 15287813dd6fSViresh Kumar * @dev: Device for which regulator name is being set. 15297813dd6fSViresh Kumar * @names: Array of pointers to the names of the regulator. 15307813dd6fSViresh Kumar * @count: Number of regulators. 15317813dd6fSViresh Kumar * 15327813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to know the name of the 15337813dd6fSViresh Kumar * device's regulators, as the core would be required to switch voltages as 15347813dd6fSViresh Kumar * well. 15357813dd6fSViresh Kumar * 15367813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 15377813dd6fSViresh Kumar */ 15387813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_regulators(struct device *dev, 15397813dd6fSViresh Kumar const char * const names[], 15407813dd6fSViresh Kumar unsigned int count) 15417813dd6fSViresh Kumar { 15427813dd6fSViresh Kumar struct opp_table *opp_table; 15437813dd6fSViresh Kumar struct regulator *reg; 15447813dd6fSViresh Kumar int ret, i; 15457813dd6fSViresh Kumar 15467813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 15477813dd6fSViresh Kumar if (!opp_table) 15487813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 15497813dd6fSViresh Kumar 15507813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 15517813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 15527813dd6fSViresh Kumar ret = -EBUSY; 15537813dd6fSViresh Kumar goto err; 15547813dd6fSViresh Kumar } 15557813dd6fSViresh Kumar 1556779b783cSViresh Kumar /* Another CPU that shares the OPP table has set the regulators ? */ 1557779b783cSViresh Kumar if (opp_table->regulators) 1558779b783cSViresh Kumar return opp_table; 15597813dd6fSViresh Kumar 15607813dd6fSViresh Kumar opp_table->regulators = kmalloc_array(count, 15617813dd6fSViresh Kumar sizeof(*opp_table->regulators), 15627813dd6fSViresh Kumar GFP_KERNEL); 15637813dd6fSViresh Kumar if (!opp_table->regulators) { 15647813dd6fSViresh Kumar ret = -ENOMEM; 15657813dd6fSViresh Kumar goto err; 15667813dd6fSViresh Kumar } 15677813dd6fSViresh Kumar 15687813dd6fSViresh Kumar for (i = 0; i < count; i++) { 15697813dd6fSViresh Kumar reg = regulator_get_optional(dev, names[i]); 15707813dd6fSViresh Kumar if (IS_ERR(reg)) { 15717813dd6fSViresh Kumar ret = PTR_ERR(reg); 15727813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) 15737813dd6fSViresh Kumar dev_err(dev, "%s: no regulator (%s) found: %d\n", 15747813dd6fSViresh Kumar __func__, names[i], ret); 15757813dd6fSViresh Kumar goto free_regulators; 15767813dd6fSViresh Kumar } 15777813dd6fSViresh Kumar 15787813dd6fSViresh Kumar opp_table->regulators[i] = reg; 15797813dd6fSViresh Kumar } 15807813dd6fSViresh Kumar 15817813dd6fSViresh Kumar opp_table->regulator_count = count; 15827813dd6fSViresh Kumar 15837813dd6fSViresh Kumar /* Allocate block only once to pass to set_opp() routines */ 15847813dd6fSViresh Kumar ret = _allocate_set_opp_data(opp_table); 15857813dd6fSViresh Kumar if (ret) 15867813dd6fSViresh Kumar goto free_regulators; 15877813dd6fSViresh Kumar 15887813dd6fSViresh Kumar return opp_table; 15897813dd6fSViresh Kumar 15907813dd6fSViresh Kumar free_regulators: 15917813dd6fSViresh Kumar while (i != 0) 15927813dd6fSViresh Kumar regulator_put(opp_table->regulators[--i]); 15937813dd6fSViresh Kumar 15947813dd6fSViresh Kumar kfree(opp_table->regulators); 15957813dd6fSViresh Kumar opp_table->regulators = NULL; 159646f48acaSViresh Kumar opp_table->regulator_count = -1; 15977813dd6fSViresh Kumar err: 15987813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 15997813dd6fSViresh Kumar 16007813dd6fSViresh Kumar return ERR_PTR(ret); 16017813dd6fSViresh Kumar } 16027813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); 16037813dd6fSViresh Kumar 16047813dd6fSViresh Kumar /** 16057813dd6fSViresh Kumar * dev_pm_opp_put_regulators() - Releases resources blocked for regulator 16067813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). 16077813dd6fSViresh Kumar */ 16087813dd6fSViresh Kumar void dev_pm_opp_put_regulators(struct opp_table *opp_table) 16097813dd6fSViresh Kumar { 16107813dd6fSViresh Kumar int i; 16117813dd6fSViresh Kumar 1612779b783cSViresh Kumar if (!opp_table->regulators) 1613779b783cSViresh Kumar goto put_opp_table; 16147813dd6fSViresh Kumar 16157813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 16167813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 16177813dd6fSViresh Kumar 16187813dd6fSViresh Kumar for (i = opp_table->regulator_count - 1; i >= 0; i--) 16197813dd6fSViresh Kumar regulator_put(opp_table->regulators[i]); 16207813dd6fSViresh Kumar 16217813dd6fSViresh Kumar _free_set_opp_data(opp_table); 16227813dd6fSViresh Kumar 16237813dd6fSViresh Kumar kfree(opp_table->regulators); 16247813dd6fSViresh Kumar opp_table->regulators = NULL; 162546f48acaSViresh Kumar opp_table->regulator_count = -1; 16267813dd6fSViresh Kumar 1627779b783cSViresh Kumar put_opp_table: 16287813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16297813dd6fSViresh Kumar } 16307813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); 16317813dd6fSViresh Kumar 16327813dd6fSViresh Kumar /** 16337813dd6fSViresh Kumar * dev_pm_opp_set_clkname() - Set clk name for the device 16347813dd6fSViresh Kumar * @dev: Device for which clk name is being set. 16357813dd6fSViresh Kumar * @name: Clk name. 16367813dd6fSViresh Kumar * 16377813dd6fSViresh Kumar * In order to support OPP switching, OPP layer needs to get pointer to the 16387813dd6fSViresh Kumar * clock for the device. Simple cases work fine without using this routine (i.e. 16397813dd6fSViresh Kumar * by passing connection-id as NULL), but for a device with multiple clocks 16407813dd6fSViresh Kumar * available, the OPP core needs to know the exact name of the clk to use. 16417813dd6fSViresh Kumar * 16427813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 16437813dd6fSViresh Kumar */ 16447813dd6fSViresh Kumar struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) 16457813dd6fSViresh Kumar { 16467813dd6fSViresh Kumar struct opp_table *opp_table; 16477813dd6fSViresh Kumar int ret; 16487813dd6fSViresh Kumar 16497813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 16507813dd6fSViresh Kumar if (!opp_table) 16517813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 16527813dd6fSViresh Kumar 16537813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 16547813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 16557813dd6fSViresh Kumar ret = -EBUSY; 16567813dd6fSViresh Kumar goto err; 16577813dd6fSViresh Kumar } 16587813dd6fSViresh Kumar 16597813dd6fSViresh Kumar /* Already have default clk set, free it */ 16607813dd6fSViresh Kumar if (!IS_ERR(opp_table->clk)) 16617813dd6fSViresh Kumar clk_put(opp_table->clk); 16627813dd6fSViresh Kumar 16637813dd6fSViresh Kumar /* Find clk for the device */ 16647813dd6fSViresh Kumar opp_table->clk = clk_get(dev, name); 16657813dd6fSViresh Kumar if (IS_ERR(opp_table->clk)) { 16667813dd6fSViresh Kumar ret = PTR_ERR(opp_table->clk); 16677813dd6fSViresh Kumar if (ret != -EPROBE_DEFER) { 16687813dd6fSViresh Kumar dev_err(dev, "%s: Couldn't find clock: %d\n", __func__, 16697813dd6fSViresh Kumar ret); 16707813dd6fSViresh Kumar } 16717813dd6fSViresh Kumar goto err; 16727813dd6fSViresh Kumar } 16737813dd6fSViresh Kumar 16747813dd6fSViresh Kumar return opp_table; 16757813dd6fSViresh Kumar 16767813dd6fSViresh Kumar err: 16777813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16787813dd6fSViresh Kumar 16797813dd6fSViresh Kumar return ERR_PTR(ret); 16807813dd6fSViresh Kumar } 16817813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); 16827813dd6fSViresh Kumar 16837813dd6fSViresh Kumar /** 16847813dd6fSViresh Kumar * dev_pm_opp_put_clkname() - Releases resources blocked for clk. 16857813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). 16867813dd6fSViresh Kumar */ 16877813dd6fSViresh Kumar void dev_pm_opp_put_clkname(struct opp_table *opp_table) 16887813dd6fSViresh Kumar { 16897813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 16907813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 16917813dd6fSViresh Kumar 16927813dd6fSViresh Kumar clk_put(opp_table->clk); 16937813dd6fSViresh Kumar opp_table->clk = ERR_PTR(-EINVAL); 16947813dd6fSViresh Kumar 16957813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 16967813dd6fSViresh Kumar } 16977813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); 16987813dd6fSViresh Kumar 16997813dd6fSViresh Kumar /** 17007813dd6fSViresh Kumar * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper 17017813dd6fSViresh Kumar * @dev: Device for which the helper is getting registered. 17027813dd6fSViresh Kumar * @set_opp: Custom set OPP helper. 17037813dd6fSViresh Kumar * 17047813dd6fSViresh Kumar * This is useful to support complex platforms (like platforms with multiple 17057813dd6fSViresh Kumar * regulators per device), instead of the generic OPP set rate helper. 17067813dd6fSViresh Kumar * 17077813dd6fSViresh Kumar * This must be called before any OPPs are initialized for the device. 17087813dd6fSViresh Kumar */ 17097813dd6fSViresh Kumar struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 17107813dd6fSViresh Kumar int (*set_opp)(struct dev_pm_set_opp_data *data)) 17117813dd6fSViresh Kumar { 17127813dd6fSViresh Kumar struct opp_table *opp_table; 17137813dd6fSViresh Kumar 17147813dd6fSViresh Kumar if (!set_opp) 17157813dd6fSViresh Kumar return ERR_PTR(-EINVAL); 17167813dd6fSViresh Kumar 17177813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 17187813dd6fSViresh Kumar if (!opp_table) 17197813dd6fSViresh Kumar return ERR_PTR(-ENOMEM); 17207813dd6fSViresh Kumar 17217813dd6fSViresh Kumar /* This should be called before OPPs are initialized */ 17227813dd6fSViresh Kumar if (WARN_ON(!list_empty(&opp_table->opp_list))) { 17235019acc6SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17245019acc6SViresh Kumar return ERR_PTR(-EBUSY); 17257813dd6fSViresh Kumar } 17267813dd6fSViresh Kumar 17275019acc6SViresh Kumar /* Another CPU that shares the OPP table has set the helper ? */ 17285019acc6SViresh Kumar if (!opp_table->set_opp) 17297813dd6fSViresh Kumar opp_table->set_opp = set_opp; 17307813dd6fSViresh Kumar 17317813dd6fSViresh Kumar return opp_table; 17327813dd6fSViresh Kumar } 17337813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); 17347813dd6fSViresh Kumar 17357813dd6fSViresh Kumar /** 1736604a7aebSViresh Kumar * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for 17377813dd6fSViresh Kumar * set_opp helper 17387813dd6fSViresh Kumar * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). 17397813dd6fSViresh Kumar * 17407813dd6fSViresh Kumar * Release resources blocked for platform specific set_opp helper. 17417813dd6fSViresh Kumar */ 1742604a7aebSViresh Kumar void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) 17437813dd6fSViresh Kumar { 17447813dd6fSViresh Kumar /* Make sure there are no concurrent readers while updating opp_table */ 17457813dd6fSViresh Kumar WARN_ON(!list_empty(&opp_table->opp_list)); 17467813dd6fSViresh Kumar 17477813dd6fSViresh Kumar opp_table->set_opp = NULL; 17487813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 17497813dd6fSViresh Kumar } 1750604a7aebSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); 17517813dd6fSViresh Kumar 17526319aee1SViresh Kumar static void _opp_detach_genpd(struct opp_table *opp_table) 17536319aee1SViresh Kumar { 17546319aee1SViresh Kumar int index; 17556319aee1SViresh Kumar 17566319aee1SViresh Kumar for (index = 0; index < opp_table->required_opp_count; index++) { 17576319aee1SViresh Kumar if (!opp_table->genpd_virt_devs[index]) 17586319aee1SViresh Kumar continue; 17596319aee1SViresh Kumar 17606319aee1SViresh Kumar dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 17616319aee1SViresh Kumar opp_table->genpd_virt_devs[index] = NULL; 17626319aee1SViresh Kumar } 1763c0ab9e08SViresh Kumar 1764c0ab9e08SViresh Kumar kfree(opp_table->genpd_virt_devs); 1765c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = NULL; 17666319aee1SViresh Kumar } 17676319aee1SViresh Kumar 17687813dd6fSViresh Kumar /** 17696319aee1SViresh Kumar * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 17706319aee1SViresh Kumar * @dev: Consumer device for which the genpd is getting attached. 17716319aee1SViresh Kumar * @names: Null terminated array of pointers containing names of genpd to attach. 17724f018bc0SViresh Kumar * 17734f018bc0SViresh Kumar * Multiple generic power domains for a device are supported with the help of 17744f018bc0SViresh Kumar * virtual genpd devices, which are created for each consumer device - genpd 17754f018bc0SViresh Kumar * pair. These are the device structures which are attached to the power domain 17764f018bc0SViresh Kumar * and are required by the OPP core to set the performance state of the genpd. 17776319aee1SViresh Kumar * The same API also works for the case where single genpd is available and so 17786319aee1SViresh Kumar * we don't need to support that separately. 17794f018bc0SViresh Kumar * 17804f018bc0SViresh Kumar * This helper will normally be called by the consumer driver of the device 17816319aee1SViresh Kumar * "dev", as only that has details of the genpd names. 17824f018bc0SViresh Kumar * 17836319aee1SViresh Kumar * This helper needs to be called once with a list of all genpd to attach. 17846319aee1SViresh Kumar * Otherwise the original device structure will be used instead by the OPP core. 17854f018bc0SViresh Kumar */ 17866319aee1SViresh Kumar struct opp_table *dev_pm_opp_attach_genpd(struct device *dev, const char **names) 17874f018bc0SViresh Kumar { 17884f018bc0SViresh Kumar struct opp_table *opp_table; 17896319aee1SViresh Kumar struct device *virt_dev; 17906319aee1SViresh Kumar int index, ret = -EINVAL; 17916319aee1SViresh Kumar const char **name = names; 17924f018bc0SViresh Kumar 17934f018bc0SViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 17944f018bc0SViresh Kumar if (!opp_table) 17954f018bc0SViresh Kumar return ERR_PTR(-ENOMEM); 17964f018bc0SViresh Kumar 17976319aee1SViresh Kumar /* 17986319aee1SViresh Kumar * If the genpd's OPP table isn't already initialized, parsing of the 17996319aee1SViresh Kumar * required-opps fail for dev. We should retry this after genpd's OPP 18006319aee1SViresh Kumar * table is added. 18016319aee1SViresh Kumar */ 18026319aee1SViresh Kumar if (!opp_table->required_opp_count) { 18036319aee1SViresh Kumar ret = -EPROBE_DEFER; 18046319aee1SViresh Kumar goto put_table; 18056319aee1SViresh Kumar } 18066319aee1SViresh Kumar 18074f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 18084f018bc0SViresh Kumar 1809c0ab9e08SViresh Kumar opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, 1810c0ab9e08SViresh Kumar sizeof(*opp_table->genpd_virt_devs), 1811c0ab9e08SViresh Kumar GFP_KERNEL); 1812c0ab9e08SViresh Kumar if (!opp_table->genpd_virt_devs) 1813c0ab9e08SViresh Kumar goto unlock; 1814c0ab9e08SViresh Kumar 18156319aee1SViresh Kumar while (*name) { 18166319aee1SViresh Kumar index = of_property_match_string(dev->of_node, 18176319aee1SViresh Kumar "power-domain-names", *name); 18186319aee1SViresh Kumar if (index < 0) { 18196319aee1SViresh Kumar dev_err(dev, "Failed to find power domain: %s (%d)\n", 18206319aee1SViresh Kumar *name, index); 18216319aee1SViresh Kumar goto err; 18226319aee1SViresh Kumar } 18234f018bc0SViresh Kumar 18246319aee1SViresh Kumar if (index >= opp_table->required_opp_count) { 18256319aee1SViresh Kumar dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 18266319aee1SViresh Kumar *name, opp_table->required_opp_count, index); 18276319aee1SViresh Kumar goto err; 18286319aee1SViresh Kumar } 18294f018bc0SViresh Kumar 18306319aee1SViresh Kumar if (opp_table->genpd_virt_devs[index]) { 18316319aee1SViresh Kumar dev_err(dev, "Genpd virtual device already set %s\n", 18326319aee1SViresh Kumar *name); 18336319aee1SViresh Kumar goto err; 18346319aee1SViresh Kumar } 18356319aee1SViresh Kumar 18366319aee1SViresh Kumar virt_dev = dev_pm_domain_attach_by_name(dev, *name); 18376319aee1SViresh Kumar if (IS_ERR(virt_dev)) { 18386319aee1SViresh Kumar ret = PTR_ERR(virt_dev); 18396319aee1SViresh Kumar dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 18406319aee1SViresh Kumar goto err; 18414f018bc0SViresh Kumar } 18424f018bc0SViresh Kumar 18434f018bc0SViresh Kumar opp_table->genpd_virt_devs[index] = virt_dev; 18446319aee1SViresh Kumar name++; 18456319aee1SViresh Kumar } 18466319aee1SViresh Kumar 18474f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 18484f018bc0SViresh Kumar 18494f018bc0SViresh Kumar return opp_table; 18506319aee1SViresh Kumar 18516319aee1SViresh Kumar err: 18526319aee1SViresh Kumar _opp_detach_genpd(opp_table); 1853c0ab9e08SViresh Kumar unlock: 18546319aee1SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 18556319aee1SViresh Kumar 18566319aee1SViresh Kumar put_table: 18576319aee1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 18586319aee1SViresh Kumar 18596319aee1SViresh Kumar return ERR_PTR(ret); 18604f018bc0SViresh Kumar } 18616319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd); 18624f018bc0SViresh Kumar 18634f018bc0SViresh Kumar /** 18646319aee1SViresh Kumar * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device. 18656319aee1SViresh Kumar * @opp_table: OPP table returned by dev_pm_opp_attach_genpd(). 18664f018bc0SViresh Kumar * 18676319aee1SViresh Kumar * This detaches the genpd(s), resets the virtual device pointers, and puts the 18686319aee1SViresh Kumar * OPP table. 18694f018bc0SViresh Kumar */ 18706319aee1SViresh Kumar void dev_pm_opp_detach_genpd(struct opp_table *opp_table) 18714f018bc0SViresh Kumar { 18724f018bc0SViresh Kumar /* 18734f018bc0SViresh Kumar * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 18744f018bc0SViresh Kumar * used in parallel. 18754f018bc0SViresh Kumar */ 18764f018bc0SViresh Kumar mutex_lock(&opp_table->genpd_virt_dev_lock); 18776319aee1SViresh Kumar _opp_detach_genpd(opp_table); 18784f018bc0SViresh Kumar mutex_unlock(&opp_table->genpd_virt_dev_lock); 18794f018bc0SViresh Kumar 18806319aee1SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 18814f018bc0SViresh Kumar } 18826319aee1SViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd); 18834f018bc0SViresh Kumar 18844f018bc0SViresh Kumar /** 1885c8a59103SViresh Kumar * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 1886c8a59103SViresh Kumar * @src_table: OPP table which has dst_table as one of its required OPP table. 1887c8a59103SViresh Kumar * @dst_table: Required OPP table of the src_table. 1888c8a59103SViresh Kumar * @pstate: Current performance state of the src_table. 1889c8a59103SViresh Kumar * 1890c8a59103SViresh Kumar * This Returns pstate of the OPP (present in @dst_table) pointed out by the 1891c8a59103SViresh Kumar * "required-opps" property of the OPP (present in @src_table) which has 1892c8a59103SViresh Kumar * performance state set to @pstate. 1893c8a59103SViresh Kumar * 1894c8a59103SViresh Kumar * Return: Zero or positive performance state on success, otherwise negative 1895c8a59103SViresh Kumar * value on errors. 1896c8a59103SViresh Kumar */ 1897c8a59103SViresh Kumar int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 1898c8a59103SViresh Kumar struct opp_table *dst_table, 1899c8a59103SViresh Kumar unsigned int pstate) 1900c8a59103SViresh Kumar { 1901c8a59103SViresh Kumar struct dev_pm_opp *opp; 1902c8a59103SViresh Kumar int dest_pstate = -EINVAL; 1903c8a59103SViresh Kumar int i; 1904c8a59103SViresh Kumar 1905c8a59103SViresh Kumar if (!pstate) 1906c8a59103SViresh Kumar return 0; 1907c8a59103SViresh Kumar 1908c8a59103SViresh Kumar /* 1909c8a59103SViresh Kumar * Normally the src_table will have the "required_opps" property set to 1910c8a59103SViresh Kumar * point to one of the OPPs in the dst_table, but in some cases the 1911c8a59103SViresh Kumar * genpd and its master have one to one mapping of performance states 1912c8a59103SViresh Kumar * and so none of them have the "required-opps" property set. Return the 1913c8a59103SViresh Kumar * pstate of the src_table as it is in such cases. 1914c8a59103SViresh Kumar */ 1915c8a59103SViresh Kumar if (!src_table->required_opp_count) 1916c8a59103SViresh Kumar return pstate; 1917c8a59103SViresh Kumar 1918c8a59103SViresh Kumar for (i = 0; i < src_table->required_opp_count; i++) { 1919c8a59103SViresh Kumar if (src_table->required_opp_tables[i]->np == dst_table->np) 1920c8a59103SViresh Kumar break; 1921c8a59103SViresh Kumar } 1922c8a59103SViresh Kumar 1923c8a59103SViresh Kumar if (unlikely(i == src_table->required_opp_count)) { 1924c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 1925c8a59103SViresh Kumar __func__, src_table, dst_table); 1926c8a59103SViresh Kumar return -EINVAL; 1927c8a59103SViresh Kumar } 1928c8a59103SViresh Kumar 1929c8a59103SViresh Kumar mutex_lock(&src_table->lock); 1930c8a59103SViresh Kumar 1931c8a59103SViresh Kumar list_for_each_entry(opp, &src_table->opp_list, node) { 1932c8a59103SViresh Kumar if (opp->pstate == pstate) { 1933c8a59103SViresh Kumar dest_pstate = opp->required_opps[i]->pstate; 1934c8a59103SViresh Kumar goto unlock; 1935c8a59103SViresh Kumar } 1936c8a59103SViresh Kumar } 1937c8a59103SViresh Kumar 1938c8a59103SViresh Kumar pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 1939c8a59103SViresh Kumar dst_table); 1940c8a59103SViresh Kumar 1941c8a59103SViresh Kumar unlock: 1942c8a59103SViresh Kumar mutex_unlock(&src_table->lock); 1943c8a59103SViresh Kumar 1944c8a59103SViresh Kumar return dest_pstate; 1945c8a59103SViresh Kumar } 1946c8a59103SViresh Kumar 1947c8a59103SViresh Kumar /** 19487813dd6fSViresh Kumar * dev_pm_opp_add() - Add an OPP table from a table definitions 19497813dd6fSViresh Kumar * @dev: device for which we do this operation 19507813dd6fSViresh Kumar * @freq: Frequency in Hz for this OPP 19517813dd6fSViresh Kumar * @u_volt: Voltage in uVolts for this OPP 19527813dd6fSViresh Kumar * 19537813dd6fSViresh Kumar * This function adds an opp definition to the opp table and returns status. 19547813dd6fSViresh Kumar * The opp is made available by default and it can be controlled using 19557813dd6fSViresh Kumar * dev_pm_opp_enable/disable functions. 19567813dd6fSViresh Kumar * 19577813dd6fSViresh Kumar * Return: 19587813dd6fSViresh Kumar * 0 On success OR 19597813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and opp->available 19607813dd6fSViresh Kumar * -EEXIST Freq are same and volt are different OR 19617813dd6fSViresh Kumar * Duplicate OPPs (both freq and volt are same) and !opp->available 19627813dd6fSViresh Kumar * -ENOMEM Memory allocation failure 19637813dd6fSViresh Kumar */ 19647813dd6fSViresh Kumar int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 19657813dd6fSViresh Kumar { 19667813dd6fSViresh Kumar struct opp_table *opp_table; 19677813dd6fSViresh Kumar int ret; 19687813dd6fSViresh Kumar 19697813dd6fSViresh Kumar opp_table = dev_pm_opp_get_opp_table(dev); 19707813dd6fSViresh Kumar if (!opp_table) 19717813dd6fSViresh Kumar return -ENOMEM; 19727813dd6fSViresh Kumar 197346f48acaSViresh Kumar /* Fix regulator count for dynamic OPPs */ 197446f48acaSViresh Kumar opp_table->regulator_count = 1; 197546f48acaSViresh Kumar 19767813dd6fSViresh Kumar ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 19770ad8c623SViresh Kumar if (ret) 19787813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 19790ad8c623SViresh Kumar 19807813dd6fSViresh Kumar return ret; 19817813dd6fSViresh Kumar } 19827813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_add); 19837813dd6fSViresh Kumar 19847813dd6fSViresh Kumar /** 19857813dd6fSViresh Kumar * _opp_set_availability() - helper to set the availability of an opp 19867813dd6fSViresh Kumar * @dev: device for which we do this operation 19877813dd6fSViresh Kumar * @freq: OPP frequency to modify availability 19887813dd6fSViresh Kumar * @availability_req: availability status requested for this opp 19897813dd6fSViresh Kumar * 19907813dd6fSViresh Kumar * Set the availability of an OPP, opp_{enable,disable} share a common logic 19917813dd6fSViresh Kumar * which is isolated here. 19927813dd6fSViresh Kumar * 19937813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 19947813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 19957813dd6fSViresh Kumar * successful. 19967813dd6fSViresh Kumar */ 19977813dd6fSViresh Kumar static int _opp_set_availability(struct device *dev, unsigned long freq, 19987813dd6fSViresh Kumar bool availability_req) 19997813dd6fSViresh Kumar { 20007813dd6fSViresh Kumar struct opp_table *opp_table; 20017813dd6fSViresh Kumar struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 20027813dd6fSViresh Kumar int r = 0; 20037813dd6fSViresh Kumar 20047813dd6fSViresh Kumar /* Find the opp_table */ 20057813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 20067813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 20077813dd6fSViresh Kumar r = PTR_ERR(opp_table); 20087813dd6fSViresh Kumar dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 20097813dd6fSViresh Kumar return r; 20107813dd6fSViresh Kumar } 20117813dd6fSViresh Kumar 20127813dd6fSViresh Kumar mutex_lock(&opp_table->lock); 20137813dd6fSViresh Kumar 20147813dd6fSViresh Kumar /* Do we have the frequency? */ 20157813dd6fSViresh Kumar list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 20167813dd6fSViresh Kumar if (tmp_opp->rate == freq) { 20177813dd6fSViresh Kumar opp = tmp_opp; 20187813dd6fSViresh Kumar break; 20197813dd6fSViresh Kumar } 20207813dd6fSViresh Kumar } 20217813dd6fSViresh Kumar 20227813dd6fSViresh Kumar if (IS_ERR(opp)) { 20237813dd6fSViresh Kumar r = PTR_ERR(opp); 20247813dd6fSViresh Kumar goto unlock; 20257813dd6fSViresh Kumar } 20267813dd6fSViresh Kumar 20277813dd6fSViresh Kumar /* Is update really needed? */ 20287813dd6fSViresh Kumar if (opp->available == availability_req) 20297813dd6fSViresh Kumar goto unlock; 20307813dd6fSViresh Kumar 20317813dd6fSViresh Kumar opp->available = availability_req; 20327813dd6fSViresh Kumar 20337813dd6fSViresh Kumar dev_pm_opp_get(opp); 20347813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 20357813dd6fSViresh Kumar 20367813dd6fSViresh Kumar /* Notify the change of the OPP availability */ 20377813dd6fSViresh Kumar if (availability_req) 20387813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 20397813dd6fSViresh Kumar opp); 20407813dd6fSViresh Kumar else 20417813dd6fSViresh Kumar blocking_notifier_call_chain(&opp_table->head, 20427813dd6fSViresh Kumar OPP_EVENT_DISABLE, opp); 20437813dd6fSViresh Kumar 20447813dd6fSViresh Kumar dev_pm_opp_put(opp); 20457813dd6fSViresh Kumar goto put_table; 20467813dd6fSViresh Kumar 20477813dd6fSViresh Kumar unlock: 20487813dd6fSViresh Kumar mutex_unlock(&opp_table->lock); 20497813dd6fSViresh Kumar put_table: 20507813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 20517813dd6fSViresh Kumar return r; 20527813dd6fSViresh Kumar } 20537813dd6fSViresh Kumar 20547813dd6fSViresh Kumar /** 20557813dd6fSViresh Kumar * dev_pm_opp_enable() - Enable a specific OPP 20567813dd6fSViresh Kumar * @dev: device for which we do this operation 20577813dd6fSViresh Kumar * @freq: OPP frequency to enable 20587813dd6fSViresh Kumar * 20597813dd6fSViresh Kumar * Enables a provided opp. If the operation is valid, this returns 0, else the 20607813dd6fSViresh Kumar * corresponding error value. It is meant to be used for users an OPP available 20617813dd6fSViresh Kumar * after being temporarily made unavailable with dev_pm_opp_disable. 20627813dd6fSViresh Kumar * 20637813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 20647813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 20657813dd6fSViresh Kumar * successful. 20667813dd6fSViresh Kumar */ 20677813dd6fSViresh Kumar int dev_pm_opp_enable(struct device *dev, unsigned long freq) 20687813dd6fSViresh Kumar { 20697813dd6fSViresh Kumar return _opp_set_availability(dev, freq, true); 20707813dd6fSViresh Kumar } 20717813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 20727813dd6fSViresh Kumar 20737813dd6fSViresh Kumar /** 20747813dd6fSViresh Kumar * dev_pm_opp_disable() - Disable a specific OPP 20757813dd6fSViresh Kumar * @dev: device for which we do this operation 20767813dd6fSViresh Kumar * @freq: OPP frequency to disable 20777813dd6fSViresh Kumar * 20787813dd6fSViresh Kumar * Disables a provided opp. If the operation is valid, this returns 20797813dd6fSViresh Kumar * 0, else the corresponding error value. It is meant to be a temporary 20807813dd6fSViresh Kumar * control by users to make this OPP not available until the circumstances are 20817813dd6fSViresh Kumar * right to make it available again (with a call to dev_pm_opp_enable). 20827813dd6fSViresh Kumar * 20837813dd6fSViresh Kumar * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 20847813dd6fSViresh Kumar * copy operation, returns 0 if no modification was done OR modification was 20857813dd6fSViresh Kumar * successful. 20867813dd6fSViresh Kumar */ 20877813dd6fSViresh Kumar int dev_pm_opp_disable(struct device *dev, unsigned long freq) 20887813dd6fSViresh Kumar { 20897813dd6fSViresh Kumar return _opp_set_availability(dev, freq, false); 20907813dd6fSViresh Kumar } 20917813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 20927813dd6fSViresh Kumar 20937813dd6fSViresh Kumar /** 20947813dd6fSViresh Kumar * dev_pm_opp_register_notifier() - Register OPP notifier for the device 20957813dd6fSViresh Kumar * @dev: Device for which notifier needs to be registered 20967813dd6fSViresh Kumar * @nb: Notifier block to be registered 20977813dd6fSViresh Kumar * 20987813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 20997813dd6fSViresh Kumar */ 21007813dd6fSViresh Kumar int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 21017813dd6fSViresh Kumar { 21027813dd6fSViresh Kumar struct opp_table *opp_table; 21037813dd6fSViresh Kumar int ret; 21047813dd6fSViresh Kumar 21057813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 21067813dd6fSViresh Kumar if (IS_ERR(opp_table)) 21077813dd6fSViresh Kumar return PTR_ERR(opp_table); 21087813dd6fSViresh Kumar 21097813dd6fSViresh Kumar ret = blocking_notifier_chain_register(&opp_table->head, nb); 21107813dd6fSViresh Kumar 21117813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 21127813dd6fSViresh Kumar 21137813dd6fSViresh Kumar return ret; 21147813dd6fSViresh Kumar } 21157813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_register_notifier); 21167813dd6fSViresh Kumar 21177813dd6fSViresh Kumar /** 21187813dd6fSViresh Kumar * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 21197813dd6fSViresh Kumar * @dev: Device for which notifier needs to be unregistered 21207813dd6fSViresh Kumar * @nb: Notifier block to be unregistered 21217813dd6fSViresh Kumar * 21227813dd6fSViresh Kumar * Return: 0 on success or a negative error value. 21237813dd6fSViresh Kumar */ 21247813dd6fSViresh Kumar int dev_pm_opp_unregister_notifier(struct device *dev, 21257813dd6fSViresh Kumar struct notifier_block *nb) 21267813dd6fSViresh Kumar { 21277813dd6fSViresh Kumar struct opp_table *opp_table; 21287813dd6fSViresh Kumar int ret; 21297813dd6fSViresh Kumar 21307813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 21317813dd6fSViresh Kumar if (IS_ERR(opp_table)) 21327813dd6fSViresh Kumar return PTR_ERR(opp_table); 21337813dd6fSViresh Kumar 21347813dd6fSViresh Kumar ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 21357813dd6fSViresh Kumar 21367813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 21377813dd6fSViresh Kumar 21387813dd6fSViresh Kumar return ret; 21397813dd6fSViresh Kumar } 21407813dd6fSViresh Kumar EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 21417813dd6fSViresh Kumar 21422a4eb735SViresh Kumar void _dev_pm_opp_find_and_remove_table(struct device *dev) 21437813dd6fSViresh Kumar { 21447813dd6fSViresh Kumar struct opp_table *opp_table; 21457813dd6fSViresh Kumar 21467813dd6fSViresh Kumar /* Check for existing table for 'dev' */ 21477813dd6fSViresh Kumar opp_table = _find_opp_table(dev); 21487813dd6fSViresh Kumar if (IS_ERR(opp_table)) { 21497813dd6fSViresh Kumar int error = PTR_ERR(opp_table); 21507813dd6fSViresh Kumar 21517813dd6fSViresh Kumar if (error != -ENODEV) 21527813dd6fSViresh Kumar WARN(1, "%s: opp_table: %d\n", 21537813dd6fSViresh Kumar IS_ERR_OR_NULL(dev) ? 21547813dd6fSViresh Kumar "Invalid device" : dev_name(dev), 21557813dd6fSViresh Kumar error); 21567813dd6fSViresh Kumar return; 21577813dd6fSViresh Kumar } 21587813dd6fSViresh Kumar 2159cdd6ed90SViresh Kumar _put_opp_list_kref(opp_table); 21607813dd6fSViresh Kumar 2161cdd6ed90SViresh Kumar /* Drop reference taken by _find_opp_table() */ 2162cdd6ed90SViresh Kumar dev_pm_opp_put_opp_table(opp_table); 2163cdd6ed90SViresh Kumar 2164cdd6ed90SViresh Kumar /* Drop reference taken while the OPP table was added */ 21657813dd6fSViresh Kumar dev_pm_opp_put_opp_table(opp_table); 21667813dd6fSViresh Kumar } 21677813dd6fSViresh Kumar 21687813dd6fSViresh Kumar /** 21697813dd6fSViresh Kumar * dev_pm_opp_remove_table() - Free all OPPs associated with the device 21707813dd6fSViresh Kumar * @dev: device pointer used to lookup OPP table. 21717813dd6fSViresh Kumar * 21727813dd6fSViresh Kumar * Free both OPPs created using static entries present in DT and the 21737813dd6fSViresh Kumar * dynamically added entries. 21747813dd6fSViresh Kumar */ 21757813dd6fSViresh Kumar void dev_pm_opp_remove_table(struct device *dev) 21767813dd6fSViresh Kumar { 21772a4eb735SViresh Kumar _dev_pm_opp_find_and_remove_table(dev); 21787813dd6fSViresh Kumar } 21797813dd6fSViresh Kumar EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 2180