Lines Matching +full:device +full:- +full:level
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
16 #include <linux/device.h>
26 * The root of the list of all opp-tables. All opp_table structures branch off
32 /* Lock to allow exclusive modification to the device and opp lists */
40 static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
44 guard(mutex)(&opp_table->lock);
46 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
47 if (opp_dev->dev == dev)
53 static struct opp_table *_find_opp_table_unlocked(struct device *dev)
62 return ERR_PTR(-ENODEV);
66 * _find_opp_table() - find opp_table struct using device pointer
67 * @dev: device pointer used to lookup OPP table
69 * Search OPP table for one containing matching device.
71 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
72 * -EINVAL based on type of error.
76 struct opp_table *_find_opp_table(struct device *dev)
80 return ERR_PTR(-EINVAL);
97 return !WARN_ON(opp_table->clk_count > 1);
106 return opp_table->clk_count > index;
115 return opp_table->path_count > index;
119 * dev_pm_opp_get_bw() - Gets the bandwidth corresponding to an opp
133 if (index >= opp->opp_table->path_count)
136 if (!opp->bandwidth)
139 return peak ? opp->bandwidth[index].peak : opp->bandwidth[index].avg;
144 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
159 return opp->supplies[0].u_volt;
164 * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp
179 return -EINVAL;
182 memcpy(supplies, opp->supplies,
183 sizeof(*supplies) * opp->opp_table->regulator_count);
189 * dev_pm_opp_get_power() - Gets the power corresponding to an opp
206 for (i = 0; i < opp->opp_table->regulator_count; i++)
207 opp_power += opp->supplies[i].u_watt;
214 * dev_pm_opp_get_freq_indexed() - Gets the frequency corresponding to an
224 if (IS_ERR_OR_NULL(opp) || index >= opp->opp_table->clk_count) {
229 return opp->rates[index];
234 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
235 * @opp: opp for which level value has to be returned for
237 * Return: level read from device tree corresponding to the opp, else
242 if (IS_ERR_OR_NULL(opp) || !opp->available) {
247 return opp->level;
252 * dev_pm_opp_get_required_pstate() - Gets the required performance state
257 * Return: performance state read from device tree corresponding to the
263 if (IS_ERR_OR_NULL(opp) || !opp->available ||
264 index >= opp->opp_table->required_opp_count) {
269 /* required-opps not fully initialized yet */
270 if (lazy_linking_pending(opp->opp_table))
274 if (unlikely(!opp->opp_table->required_opp_tables[index]->is_genpd)) {
279 return opp->required_opps[index]->level;
284 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
295 if (IS_ERR_OR_NULL(opp) || !opp->available) {
300 return opp->turbo;
305 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
306 * @dev: device for which we do this operation
310 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
318 return opp_table->clock_latency_ns_max;
323 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
324 * @dev: device for which we do this operation
328 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
345 /* Regulator may not be required for the device */
346 if (!opp_table->regulators)
349 count = opp_table->regulator_count;
355 scoped_guard(mutex, &opp_table->lock) {
360 list_for_each_entry(opp, &opp_table->opp_list, node) {
361 if (!opp->available)
364 if (opp->supplies[i].u_volt_min < uV[i].min)
365 uV[i].min = opp->supplies[i].u_volt_min;
366 if (opp->supplies[i].u_volt_max > uV[i].max)
367 uV[i].max = opp->supplies[i].u_volt_max;
377 reg = opp_table->regulators[i];
390 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
392 * @dev: device for which we do this operation
397 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
405 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
406 * @dev: device for which we do this operation
411 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
421 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
422 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
433 guard(mutex)(&opp_table->lock);
435 list_for_each_entry(opp, &opp_table->opp_list, node) {
436 if (opp->available)
444 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
445 * @dev: device for which we do this operation
450 int dev_pm_opp_get_opp_count(struct device *dev)
468 return opp->rates[index];
473 return opp->level;
478 return opp->bandwidth[index].peak;
484 key->bw = opp->bandwidth ? opp->bandwidth[index].peak : 0;
485 key->freq = opp->rates[index];
486 key->level = opp->level;
528 bool level_match = (key->level == OPP_LEVEL_UNSET || opp_key->level == key->level);
529 bool freq_match = (key->freq == 0 || opp_key->freq == key->freq);
530 bool bw_match = (key->bw == 0 || opp_key->bw == key->bw);
548 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
552 return ERR_PTR(-EINVAL);
554 guard(mutex)(&opp_table->lock);
556 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
557 if (temp_opp->available == available) {
580 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
585 return ERR_PTR(-EINVAL);
587 guard(mutex)(&opp_table->lock);
589 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
590 if (temp_opp->available == available) {
604 _find_key(struct device *dev, unsigned long *key, int index, bool available,
623 static struct dev_pm_opp *_find_key_exact(struct device *dev,
645 static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
654 static struct dev_pm_opp *_find_key_floor(struct device *dev,
664 * dev_pm_opp_find_freq_exact() - search for an exact frequency
665 * @dev: device for which we do this operation
667 * @available: true/false - match for available opp
674 * ENODEV: if device not found in list of registered devices
686 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
695 * dev_pm_opp_find_key_exact() - Search for an OPP with exact key set
696 * @dev: Device for which the OPP is being searched
698 * @available: true/false - match for available OPP
706 * ENODEV: if device not found in list of registered devices
718 struct dev_pm_opp *dev_pm_opp_find_key_exact(struct device *dev,
737 * dev_pm_opp_find_freq_exact_indexed() - Search for an exact freq for the
739 * @dev: Device for which we do this operation
742 * @available: true/false - match for available opp
745 * specified index from a starting freq for a device.
751 * ENODEV: if device not found in list of registered devices
757 dev_pm_opp_find_freq_exact_indexed(struct device *dev, unsigned long freq,
773 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
774 * @dev: device for which we do this operation
778 * for a device.
785 * ENODEV: if device not found in list of registered devices
790 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
798 * dev_pm_opp_find_freq_ceil_indexed() - Search for a rounded ceil freq for the
800 * @dev: Device for which we do this operation
805 * the specified index from a starting freq for a device.
812 * ENODEV: if device not found in list of registered devices
818 dev_pm_opp_find_freq_ceil_indexed(struct device *dev, unsigned long *freq,
827 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
828 * @dev: device for which we do this operation
832 * for a device.
839 * ENODEV: if device not found in list of registered devices
844 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
852 * dev_pm_opp_find_freq_floor_indexed() - Search for a rounded floor freq for the
854 * @dev: Device for which we do this operation
859 * the specified index from a starting freq for a device.
866 * ENODEV: if device not found in list of registered devices
872 dev_pm_opp_find_freq_floor_indexed(struct device *dev, unsigned long *freq,
880 * dev_pm_opp_find_level_exact() - search for an exact level
881 * @dev: device for which we do this operation
882 * @level: level to search for
889 * ENODEV: if device not found in list of registered devices
894 struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
895 unsigned int level)
897 return _find_key_exact(dev, level, 0, true, _read_level, NULL);
902 * dev_pm_opp_find_level_ceil() - search for an rounded up level
903 * @dev: device for which we do this operation
904 * @level: level to search for
911 * ENODEV: if device not found in list of registered devices
916 struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
917 unsigned int *level)
919 unsigned long temp = *level;
930 return ERR_PTR(-ENODEV);
933 *level = temp;
939 * dev_pm_opp_find_level_floor() - Search for a rounded floor level
940 * @dev: device for which we do this operation
941 * @level: Start level
943 * Search for the matching floor *available* OPP from a starting level
944 * for a device.
946 * Return: matching *opp and refreshes *level accordingly, else returns
951 * ENODEV: if device not found in list of registered devices
956 struct dev_pm_opp *dev_pm_opp_find_level_floor(struct device *dev,
957 unsigned int *level)
959 unsigned long temp = *level;
963 *level = temp;
969 * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
970 * @dev: device for which we do this operation
975 * for a device.
982 * ENODEV: if device not found in list of registered devices
987 struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
1001 * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
1002 * @dev: device for which we do this operation
1007 * for a device.
1014 * ENODEV: if device not found in list of registered devices
1019 struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
1032 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
1037 /* Regulator not available for device */
1045 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
1047 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
1048 supply->u_volt, supply->u_volt_max);
1051 __func__, supply->u_volt_min, supply->u_volt,
1052 supply->u_volt_max, ret);
1058 _opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
1069 freq = opp->rates[0];
1072 return -EINVAL;
1075 ret = clk_set_rate(opp_table->clk, freq);
1080 opp_table->current_rate_single_clk = freq;
1090 int dev_pm_opp_config_clks_simple(struct device *dev,
1097 for (i = opp_table->clk_count - 1; i >= 0; i--) {
1098 ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
1106 for (i = 0; i < opp_table->clk_count; i++) {
1107 ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
1120 static int _opp_config_regulator_single(struct device *dev,
1127 /* This function only supports single regulator per device */
1130 return -EINVAL;
1133 ret = _set_opp_voltage(dev, reg, new_opp->supplies);
1139 * some boot-enabled regulators.
1141 if (unlikely(!new_opp->opp_table->enabled)) {
1151 struct dev_pm_opp *opp, struct device *dev)
1156 if (!opp_table->paths)
1159 for (i = 0; i < opp_table->path_count; i++) {
1164 avg = opp->bandwidth[i].avg;
1165 peak = opp->bandwidth[i].peak;
1167 ret = icc_set_bw(opp_table->paths[i], avg, peak);
1178 static int _set_opp_level(struct device *dev, struct dev_pm_opp *opp)
1180 unsigned int level = 0;
1184 if (opp->level == OPP_LEVEL_UNSET)
1187 level = opp->level;
1190 /* Request a new performance state through the device's PM domain. */
1191 ret = dev_pm_domain_set_performance_state(dev, level);
1193 dev_err(dev, "Failed to set performance state %u (%d)\n", level,
1200 static int _set_required_opps(struct device *dev, struct opp_table *opp_table,
1203 struct device **devs = opp_table->required_devs;
1210 /* required-opps not fully initialized yet */
1212 return -EBUSY;
1217 target = opp_table->required_opp_count;
1220 index = opp_table->required_opp_count - 1;
1221 target = -1;
1222 delta = -1;
1227 required_opp = opp ? opp->required_opps[index] : NULL;
1240 static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
1242 struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
1245 if (!IS_ERR(opp_table->clk)) {
1246 freq = clk_get_rate(opp_table->clk);
1256 guard(mutex)(&opp_table->lock);
1257 opp = dev_pm_opp_get(list_first_entry(&opp_table->opp_list,
1261 opp_table->current_opp = opp;
1264 static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
1268 if (!opp_table->enabled)
1273 * have OPP table for the device, while others don't and
1283 if (opp_table->regulators)
1284 regulator_disable(opp_table->regulators[0]);
1293 opp_table->enabled = false;
1297 static int _set_opp(struct device *dev, struct opp_table *opp_table,
1307 if (unlikely(!opp_table->current_opp))
1310 old_opp = opp_table->current_opp;
1313 if (!forced && old_opp == opp && opp_table->enabled) {
1318 dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1319 __func__, old_opp->rates[0], opp->rates[0], old_opp->level,
1320 opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1321 opp->bandwidth ? opp->bandwidth[0].peak : 0);
1324 if (scaling_down == -1)
1345 if (opp_table->config_regulators) {
1346 ret = opp_table->config_regulators(dev, old_opp, opp,
1347 opp_table->regulators,
1348 opp_table->regulator_count);
1357 if (opp_table->config_clks) {
1358 ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
1365 if (opp_table->config_regulators) {
1366 ret = opp_table->config_regulators(dev, old_opp, opp,
1367 opp_table->regulators,
1368 opp_table->regulator_count);
1393 opp_table->enabled = true;
1397 opp_table->current_opp = dev_pm_opp_get(opp);
1403 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1404 * @dev: device for which we do this operation
1407 * This configures the power-supplies to the levels specified by the OPP
1409 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1413 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1422 dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1435 return opp_table->config_clks(dev, opp_table, NULL,
1439 freq = clk_round_rate(opp_table->clk, target_freq);
1463 forced = opp_table->current_rate_single_clk != freq;
1471 * dev_pm_opp_set_opp() - Configure device for OPP
1472 * @dev: device for which we do this operation
1475 * This configures the device based on the properties of the OPP passed to this
1480 int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1486 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1494 /* OPP-dev Helpers */
1499 list_del(&opp_dev->node);
1503 struct opp_device *_add_opp_dev(const struct device *dev,
1512 /* Initialize opp-dev */
1513 opp_dev->dev = dev;
1515 scoped_guard(mutex, &opp_table->lock)
1516 list_add(&opp_dev->node, &opp_table->dev_list);
1524 static struct opp_table *_allocate_opp_table(struct device *dev, int index)
1532 * device is needed to be added, we pay this penalty.
1536 return ERR_PTR(-ENOMEM);
1538 mutex_init(&opp_table->lock);
1539 INIT_LIST_HEAD(&opp_table->dev_list);
1540 INIT_LIST_HEAD(&opp_table->lazy);
1542 opp_table->clk = ERR_PTR(-ENODEV);
1545 opp_table->regulator_count = -1;
1549 ret = -ENOMEM;
1555 /* Find interconnect path(s) for the device */
1558 if (ret == -EPROBE_DEFER)
1565 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
1566 INIT_LIST_HEAD(&opp_table->opp_list);
1567 kref_init(&opp_table->kref);
1574 mutex_destroy(&opp_table->lock);
1580 static struct opp_table *_update_opp_table_clk(struct device *dev,
1590 if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
1591 opp_table->clks)
1596 * device's clock settings. In such cases neither the platform
1598 * clk entry. The OPP nodes in DT may still contain "opp-hz" property
1604 * in opp->rates and also parse the entries in DT. Use
1605 * clk_get_optional() instead of clk_get() so opp_table->clk stays
1606 * NULL for such devices, instead of holding an ERR_PTR(-ENOENT) that
1607 * consumers must remember to special-case.
1609 opp_table->clk = clk_get_optional(dev, NULL);
1611 if (IS_ERR(opp_table->clk)) {
1612 ret = dev_err_probe(dev, PTR_ERR(opp_table->clk), "Couldn't find clock\n");
1617 if (opp_table->clk)
1618 opp_table->config_clks = _opp_config_clk_single;
1620 opp_table->clk_count = 1;
1626 * We need to make sure that the OPP table for a device doesn't get added twice,
1627 * if this routine gets called in parallel with the same device pointer.
1640 struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
1671 opp_table = ERR_PTR(-ENOMEM);
1680 list_add(&opp_table->node, &opp_tables);
1691 static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1696 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1709 list_del(&opp_table->node);
1712 if (opp_table->current_opp)
1713 dev_pm_opp_put(opp_table->current_opp);
1718 if (!IS_ERR(opp_table->clk))
1719 clk_put(opp_table->clk);
1721 if (opp_table->paths) {
1722 for (i = 0; i < opp_table->path_count; i++)
1723 icc_put(opp_table->paths[i]);
1724 kfree(opp_table->paths);
1727 WARN_ON(!list_empty(&opp_table->opp_list));
1729 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node)
1732 mutex_destroy(&opp_table->lock);
1738 kref_get(&opp_table->kref);
1745 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1758 struct opp_table *opp_table = opp->opp_table;
1760 list_del(&opp->node);
1761 mutex_unlock(&opp_table->lock);
1767 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1775 kref_get(&opp->kref);
1782 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
1787 * dev_pm_opp_remove() - Remove an OPP from OPP table
1788 * @dev: device for which we do this operation
1793 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1806 scoped_guard(mutex, &opp_table->lock) {
1807 list_for_each_entry(iter, &opp_table->opp_list, node) {
1808 if (iter->rates[0] == freq) {
1832 guard(mutex)(&opp_table->lock);
1834 list_for_each_entry(opp, &opp_table->opp_list, node) {
1839 if (!opp->removed && dynamic == opp->dynamic)
1849 * called without the opp_table->lock held.
1856 opp->removed = true;
1867 scoped_guard(mutex, &opp_table->lock) {
1868 if (!opp_table->parsed_static_opps)
1871 if (--opp_table->parsed_static_opps)
1880 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1881 * @dev: device for which we do this operation
1885 void dev_pm_opp_remove_all_dynamic(struct device *dev)
1903 supply_count = opp_table->regulator_count > 0 ?
1904 opp_table->regulator_count : 1;
1905 supply_size = sizeof(*opp->supplies) * supply_count;
1906 clk_size = sizeof(*opp->rates) * opp_table->clk_count;
1907 icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
1915 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1917 opp->rates = (unsigned long *)(opp->supplies + supply_count);
1920 opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
1922 INIT_LIST_HEAD(&opp->node);
1924 opp->level = OPP_LEVEL_UNSET;
1935 if (!opp_table->regulators)
1938 for (i = 0; i < opp_table->regulator_count; i++) {
1939 reg = opp_table->regulators[i];
1942 opp->supplies[i].u_volt_min,
1943 opp->supplies[i].u_volt_max)) {
1945 __func__, opp->supplies[i].u_volt_min,
1946 opp->supplies[i].u_volt_max);
1959 for (i = 0; i < opp_table->clk_count; i++) {
1960 if (opp1->rates[i] != opp2->rates[i])
1961 return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
1973 for (i = 0; i < opp_table->path_count; i++) {
1974 if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
1975 return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
1986 * -1: opp1 < opp2
2001 if (opp1->level != opp2->level)
2002 return opp1->level < opp2->level ? -1 : 1;
2008 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
2019 * Need to use &opp_table->opp_list in the condition part of the 'for'
2023 list_for_each_entry(opp, &opp_table->opp_list, node) {
2026 *head = &opp->node;
2035 __func__, opp->rates[0], opp->supplies[0].u_volt,
2036 opp->available, new_opp->rates[0],
2037 new_opp->supplies[0].u_volt, new_opp->available);
2040 return opp->available &&
2041 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
2052 if (opp->required_opps[i]->available)
2055 opp->available = false;
2057 __func__, opp->required_opps[i]->np, opp->rates[0]);
2065 * -EBUSY: For OPP with same freq/volt and is available. The callers of
2066 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
2069 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
2072 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
2078 scoped_guard(mutex, &opp_table->lock) {
2079 head = &opp_table->opp_list;
2085 list_add(&new_opp->node, head);
2086 new_opp->opp_table = opp_table;
2087 kref_init(&new_opp->kref);
2093 new_opp->available = false;
2095 __func__, new_opp->rates[0]);
2098 /* required-opps not fully initialized yet */
2102 _required_opps_available(new_opp, opp_table->required_opp_count);
2108 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
2110 * @dev: device for which we do this operation
2123 * Duplicate OPPs (both freq and volt are same) and opp->available
2124 * -EEXIST Freq are same and volt are different OR
2125 * Duplicate OPPs (both freq and volt are same) and !opp->available
2126 * -ENOMEM Memory allocation failure
2128 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
2132 unsigned long tol, u_volt = data->u_volt;
2136 return -EINVAL;
2140 return -ENOMEM;
2143 new_opp->rates[0] = data->freq;
2144 new_opp->level = data->level;
2145 new_opp->turbo = data->turbo;
2146 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
2147 new_opp->supplies[0].u_volt = u_volt;
2148 new_opp->supplies[0].u_volt_min = u_volt - tol;
2149 new_opp->supplies[0].u_volt_max = u_volt + tol;
2150 new_opp->available = true;
2151 new_opp->dynamic = dynamic;
2156 if (ret == -EBUSY)
2165 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
2177 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
2184 if (opp_table->supported_hw)
2187 opp_table->supported_hw = kmemdup_array(versions, count,
2189 if (!opp_table->supported_hw)
2190 return -ENOMEM;
2192 opp_table->supported_hw_count = count;
2199 if (opp_table->supported_hw) {
2200 kfree(opp_table->supported_hw);
2201 opp_table->supported_hw = NULL;
2202 opp_table->supported_hw_count = 0;
2209 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
2210 * should postfix the property name with -<name> while looking for them.
2215 if (!opp_table->prop_name) {
2216 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
2217 if (!opp_table->prop_name)
2218 return -ENOMEM;
2226 if (opp_table->prop_name) {
2227 kfree(opp_table->prop_name);
2228 opp_table->prop_name = NULL;
2234 * device's regulators, as the core would be required to switch voltages as
2237 * This must be called before any OPPs are initialized for the device.
2239 static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
2251 return -EINVAL;
2254 if (opp_table->regulators)
2257 opp_table->regulators = kmalloc_objs(*opp_table->regulators, count);
2258 if (!opp_table->regulators)
2259 return -ENOMEM;
2270 opp_table->regulators[i] = reg;
2273 opp_table->regulator_count = count;
2277 opp_table->config_regulators = _opp_config_regulator_single;
2283 regulator_put(opp_table->regulators[--i]);
2285 kfree(opp_table->regulators);
2286 opp_table->regulators = NULL;
2287 opp_table->regulator_count = -1;
2296 if (!opp_table->regulators)
2299 if (opp_table->enabled) {
2300 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2301 regulator_disable(opp_table->regulators[i]);
2304 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2305 regulator_put(opp_table->regulators[i]);
2307 kfree(opp_table->regulators);
2308 opp_table->regulators = NULL;
2309 opp_table->regulator_count = -1;
2316 for (i = count - 1; i >= 0; i--)
2317 clk_put(opp_table->clks[i]);
2319 kfree(opp_table->clks);
2320 opp_table->clks = NULL;
2325 * clocks for the device. Simple cases work fine without using this routine
2326 * (i.e. by passing connection-id as NULL), but for a device with multiple
2330 * This must be called before any OPPs are initialized for the device.
2332 static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
2353 return -EINVAL;
2356 if (opp_table->clks)
2359 opp_table->clks = kmalloc_objs(*opp_table->clks, count);
2360 if (!opp_table->clks)
2361 return -ENOMEM;
2363 /* Find clks for the device */
2373 opp_table->clks[i] = clk;
2376 opp_table->clk_count = count;
2377 opp_table->config_clks = config_clks;
2381 if (!opp_table->config_clks)
2382 opp_table->config_clks = _opp_config_clk_single;
2389 * - avoiding clks[0] everywhere else.
2390 * - not running single clk helpers for multiple clk usecase by
2393 * Since this is single-clk case, just update the clk pointer
2396 opp_table->clk = opp_table->clks[0];
2408 if (!opp_table->clks)
2411 opp_table->config_clks = NULL;
2412 opp_table->clk = ERR_PTR(-ENODEV);
2414 _put_clks(opp_table, opp_table->clk_count);
2418 * This is useful to support platforms with multiple regulators per device.
2420 * This must be called before any OPPs are initialized for the device.
2423 struct device *dev, config_regulators_t config_regulators)
2426 if (!opp_table->config_regulators)
2427 opp_table->config_regulators = config_regulators;
2434 if (opp_table->config_regulators)
2435 opp_table->config_regulators = NULL;
2439 struct device *dev,
2440 struct device *required_dev,
2444 struct device *gdev;
2447 if (opp_table->is_genpd) {
2449 return -EOPNOTSUPP;
2452 if (index >= opp_table->required_opp_count) {
2454 return -EINVAL;
2457 required_table = opp_table->required_opp_tables[index];
2460 return -ENODEV;
2472 * incorrect sibling device.
2484 opp_table->required_opp_tables[index] = pd_table;
2490 opp_table->required_devs[index] = required_dev;
2497 opp_table->required_devs[index] = NULL;
2502 if (data->flags & OPP_CONFIG_REQUIRED_DEV)
2503 _opp_put_required_dev(data->opp_table,
2504 data->required_dev_index);
2505 if (data->flags & OPP_CONFIG_REGULATOR)
2506 _opp_put_regulators(data->opp_table);
2507 if (data->flags & OPP_CONFIG_SUPPORTED_HW)
2508 _opp_put_supported_hw(data->opp_table);
2509 if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
2510 _opp_put_config_regulators_helper(data->opp_table);
2511 if (data->flags & OPP_CONFIG_PROP_NAME)
2512 _opp_put_prop_name(data->opp_table);
2513 if (data->flags & OPP_CONFIG_CLK)
2514 _opp_put_clknames(data->opp_table);
2516 dev_pm_opp_put_opp_table(data->opp_table);
2521 * dev_pm_opp_set_config() - Set OPP configuration for the device.
2522 * @dev: Device for which configuration is being set.
2525 * This allows all device OPP configurations to be performed at once.
2527 * This must be called before any OPPs are initialized for the device. This may
2537 int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
2546 return -ENOMEM;
2554 data->opp_table = opp_table;
2555 data->flags = 0;
2558 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2559 ret = -EBUSY;
2564 if (config->clk_names) {
2565 ret = _opp_set_clknames(opp_table, dev, config->clk_names,
2566 config->config_clks);
2570 data->flags |= OPP_CONFIG_CLK;
2571 } else if (config->config_clks) {
2573 ret = -EINVAL;
2578 if (config->prop_name) {
2579 ret = _opp_set_prop_name(opp_table, config->prop_name);
2583 data->flags |= OPP_CONFIG_PROP_NAME;
2587 if (config->config_regulators) {
2589 config->config_regulators);
2593 data->flags |= OPP_CONFIG_REGULATOR_HELPER;
2597 if (config->supported_hw) {
2598 ret = _opp_set_supported_hw(opp_table, config->supported_hw,
2599 config->supported_hw_count);
2603 data->flags |= OPP_CONFIG_SUPPORTED_HW;
2607 if (config->regulator_names) {
2609 config->regulator_names);
2613 data->flags |= OPP_CONFIG_REGULATOR;
2616 if (config->required_dev) {
2618 config->required_dev,
2619 config->required_dev_index);
2623 data->required_dev_index = config->required_dev_index;
2624 data->flags |= OPP_CONFIG_REQUIRED_DEV;
2641 * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
2644 * This allows all device OPP configurations to be cleared at once. This must be
2678 * devm_pm_opp_set_config() - Set OPP configuration for the device.
2679 * @dev: Device for which configuration is being set.
2682 * This allows all device OPP configurations to be performed at once.
2683 * This is a resource-managed variant of dev_pm_opp_set_config().
2687 int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
2700 * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
2706 * "required-opps" property of the @src_opp (present in @src_table).
2717 struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
2721 !src_table->required_opp_tables)
2722 return ERR_PTR(-EINVAL);
2724 /* required-opps not fully initialized yet */
2726 return ERR_PTR(-EBUSY);
2728 for (i = 0; i < src_table->required_opp_count; i++) {
2729 if (src_table->required_opp_tables[i] != dst_table)
2732 scoped_guard(mutex, &src_table->lock) {
2733 list_for_each_entry(opp, &src_table->opp_list, node) {
2735 dest_opp = dev_pm_opp_get(opp->required_opps[i]);
2753 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2759 * "required-opps" property of the OPP (present in @src_table) which has
2776 * and so none of them have the "required-opps" property set. Return the
2779 if (!src_table || !src_table->required_opp_count)
2783 if (unlikely(!src_table->is_genpd || !dst_table->is_genpd)) {
2785 return -EINVAL;
2788 /* required-opps not fully initialized yet */
2790 return -EBUSY;
2792 for (i = 0; i < src_table->required_opp_count; i++) {
2793 if (src_table->required_opp_tables[i]->np == dst_table->np)
2797 if (unlikely(i == src_table->required_opp_count)) {
2800 return -EINVAL;
2803 guard(mutex)(&src_table->lock);
2805 list_for_each_entry(opp, &src_table->opp_list, node) {
2806 if (opp->level == pstate)
2807 return opp->required_opps[i]->level;
2813 return -EINVAL;
2817 * dev_pm_opp_add_dynamic() - Add an OPP table from a table definitions
2818 * @dev: The device for which we do this operation
2827 * Duplicate OPPs (both freq and volt are same) and opp->available
2828 * -EEXIST Freq are same and volt are different OR
2829 * Duplicate OPPs (both freq and volt are same) and !opp->available
2830 * -ENOMEM Memory allocation failure
2832 int dev_pm_opp_add_dynamic(struct device *dev, struct dev_pm_opp_data *data)
2842 opp_table->regulator_count = 1;
2853 * _opp_set_availability() - helper to set the availability of an opp
2854 * @dev: device for which we do this operation
2861 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2865 static int _opp_set_availability(struct device *dev, unsigned long freq,
2871 struct dev_pm_opp *opp __free(put_opp) = ERR_PTR(-ENODEV), *tmp_opp;
2874 dev_warn(dev, "%s: Device OPP not found (%pe)\n", __func__,
2880 return -EINVAL;
2882 scoped_guard(mutex, &opp_table->lock) {
2884 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2885 if (tmp_opp->rates[0] == freq) {
2889 if (opp->available == availability_req)
2892 opp->available = availability_req;
2903 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
2906 blocking_notifier_call_chain(&opp_table->head,
2913 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2914 * @dev: device for which we do this operation
2920 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2924 int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2932 struct dev_pm_opp *opp __free(put_opp) = ERR_PTR(-ENODEV), *tmp_opp;
2937 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2942 return -EINVAL;
2944 scoped_guard(mutex, &opp_table->lock) {
2946 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2947 if (tmp_opp->rates[0] == freq) {
2951 if (opp->supplies->u_volt == u_volt)
2954 opp->supplies->u_volt = u_volt;
2955 opp->supplies->u_volt_min = u_volt_min;
2956 opp->supplies->u_volt_max = u_volt_max;
2967 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
2975 * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2976 * @dev: device for which we do this operation
2982 int dev_pm_opp_sync_regulators(struct device *dev)
2987 /* Device may not have OPP table */
2994 /* Regulator may not be required for the device */
2995 if (unlikely(!opp_table->regulators))
2999 if (!opp_table->enabled)
3002 for (i = 0; i < opp_table->regulator_count; i++) {
3003 reg = opp_table->regulators[i];
3014 * dev_pm_opp_enable() - Enable a specific OPP
3015 * @dev: device for which we do this operation
3022 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
3026 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
3033 * dev_pm_opp_disable() - Disable a specific OPP
3034 * @dev: device for which we do this operation
3042 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
3046 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
3053 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
3054 * @dev: Device for which notifier needs to be registered
3059 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
3067 return blocking_notifier_chain_register(&opp_table->head, nb);
3072 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
3073 * @dev: Device for which notifier needs to be unregistered
3078 int dev_pm_opp_unregister_notifier(struct device *dev,
3087 return blocking_notifier_chain_unregister(&opp_table->head, nb);
3092 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
3093 * @dev: device pointer used to lookup OPP table.
3098 void dev_pm_opp_remove_table(struct device *dev)
3107 if (error != -ENODEV)
3110 "Invalid device" : dev_name(dev),