Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Generic OPP Interface |
| 3 | * |
| 4 | * Copyright (C) 2009-2010 Texas Instruments Incorporated. |
| 5 | * Nishanth Menon |
| 6 | * Romit Dasgupta |
| 7 | * Kevin Hilman |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Viresh Kumar | d6d2a52 | 2015-10-17 09:45:18 +0530 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 16 | #include <linux/clk.h> |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/err.h> |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 19 | #include <linux/slab.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 20 | #include <linux/device.h> |
Liam Girdwood | 80126ce | 2012-10-23 01:27:44 +0200 | [diff] [blame] | 21 | #include <linux/export.h> |
Viresh Kumar | 009acd1 | 2017-10-11 12:54:14 +0530 | [diff] [blame] | 22 | #include <linux/pm_domain.h> |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 23 | #include <linux/regulator/consumer.h> |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 24 | |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 25 | #include "opp.h" |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 26 | |
| 27 | /* |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 28 | * The root of the list of all opp-tables. All opp_table structures branch off |
| 29 | * from here, with each opp_table containing the list of opps it supports in |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 30 | * various states of availability. |
| 31 | */ |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 32 | LIST_HEAD(opp_tables); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 33 | /* Lock to allow exclusive modification to the device and opp lists */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 34 | DEFINE_MUTEX(opp_table_lock); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 35 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 36 | static struct opp_device *_find_opp_dev(const struct device *dev, |
| 37 | struct opp_table *opp_table) |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 38 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 39 | struct opp_device *opp_dev; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 40 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 41 | list_for_each_entry(opp_dev, &opp_table->dev_list, node) |
| 42 | if (opp_dev->dev == dev) |
| 43 | return opp_dev; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 44 | |
| 45 | return NULL; |
| 46 | } |
| 47 | |
Wei Yongjun | 6ac4239 | 2017-02-06 14:29:55 +0000 | [diff] [blame] | 48 | static struct opp_table *_find_opp_table_unlocked(struct device *dev) |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 49 | { |
| 50 | struct opp_table *opp_table; |
Viresh Kumar | 3d25569 | 2018-08-03 07:05:21 +0530 | [diff] [blame] | 51 | bool found; |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 52 | |
| 53 | list_for_each_entry(opp_table, &opp_tables, node) { |
Viresh Kumar | 3d25569 | 2018-08-03 07:05:21 +0530 | [diff] [blame] | 54 | mutex_lock(&opp_table->lock); |
| 55 | found = !!_find_opp_dev(dev, opp_table); |
| 56 | mutex_unlock(&opp_table->lock); |
| 57 | |
| 58 | if (found) { |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 59 | _get_opp_table_kref(opp_table); |
| 60 | |
| 61 | return opp_table; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return ERR_PTR(-ENODEV); |
| 66 | } |
| 67 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 68 | /** |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 69 | * _find_opp_table() - find opp_table struct using device pointer |
| 70 | * @dev: device pointer used to lookup OPP table |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 71 | * |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 72 | * Search OPP table for one containing matching device. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 73 | * |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 74 | * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 75 | * -EINVAL based on type of error. |
| 76 | * |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 77 | * The callers must call dev_pm_opp_put_opp_table() after the table is used. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 78 | */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 79 | struct opp_table *_find_opp_table(struct device *dev) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 80 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 81 | struct opp_table *opp_table; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 82 | |
Viresh Kumar | 50a3cb0 | 2015-08-12 15:59:39 +0530 | [diff] [blame] | 83 | if (IS_ERR_OR_NULL(dev)) { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 84 | pr_err("%s: Invalid parameters\n", __func__); |
| 85 | return ERR_PTR(-EINVAL); |
| 86 | } |
| 87 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 88 | mutex_lock(&opp_table_lock); |
| 89 | opp_table = _find_opp_table_unlocked(dev); |
| 90 | mutex_unlock(&opp_table_lock); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 91 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 92 | return opp_table; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Linus Torvalds | baf51c4 | 2015-11-11 09:03:01 -0800 | [diff] [blame] | 96 | * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 97 | * @opp: opp for which voltage has to be returned for |
| 98 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 99 | * Return: voltage in micro volt corresponding to the opp, else |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 100 | * return 0 |
| 101 | * |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 102 | * This is useful only for devices with single power supply. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 103 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 104 | unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 105 | { |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 106 | if (IS_ERR_OR_NULL(opp)) { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 107 | pr_err("%s: Invalid parameters\n", __func__); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 108 | return 0; |
| 109 | } |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 110 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 111 | return opp->supplies[0].u_volt; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 112 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 113 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 114 | |
| 115 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 116 | * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 117 | * @opp: opp for which frequency has to be returned for |
| 118 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 119 | * Return: frequency in hertz corresponding to the opp, else |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 120 | * return 0 |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 121 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 122 | unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 123 | { |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 124 | if (IS_ERR_OR_NULL(opp) || !opp->available) { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 125 | pr_err("%s: Invalid parameters\n", __func__); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 126 | return 0; |
| 127 | } |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 128 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 129 | return opp->rate; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 130 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 131 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 132 | |
| 133 | /** |
Bartlomiej Zolnierkiewicz | 19445b2 | 2015-07-09 17:43:35 +0200 | [diff] [blame] | 134 | * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not |
| 135 | * @opp: opp for which turbo mode is being verified |
| 136 | * |
| 137 | * Turbo OPPs are not for normal use, and can be enabled (under certain |
| 138 | * conditions) for short duration of times to finish high throughput work |
| 139 | * quickly. Running on them for longer times may overheat the chip. |
| 140 | * |
| 141 | * Return: true if opp is turbo opp, else false. |
Bartlomiej Zolnierkiewicz | 19445b2 | 2015-07-09 17:43:35 +0200 | [diff] [blame] | 142 | */ |
| 143 | bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) |
| 144 | { |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 145 | if (IS_ERR_OR_NULL(opp) || !opp->available) { |
Bartlomiej Zolnierkiewicz | 19445b2 | 2015-07-09 17:43:35 +0200 | [diff] [blame] | 146 | pr_err("%s: Invalid parameters\n", __func__); |
| 147 | return false; |
| 148 | } |
| 149 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 150 | return opp->turbo; |
Bartlomiej Zolnierkiewicz | 19445b2 | 2015-07-09 17:43:35 +0200 | [diff] [blame] | 151 | } |
| 152 | EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); |
| 153 | |
| 154 | /** |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 155 | * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds |
| 156 | * @dev: device for which we do this operation |
| 157 | * |
| 158 | * Return: This function returns the max clock latency in nanoseconds. |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 159 | */ |
| 160 | unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) |
| 161 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 162 | struct opp_table *opp_table; |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 163 | unsigned long clock_latency_ns; |
| 164 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 165 | opp_table = _find_opp_table(dev); |
| 166 | if (IS_ERR(opp_table)) |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 167 | return 0; |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 168 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 169 | clock_latency_ns = opp_table->clock_latency_ns_max; |
| 170 | |
| 171 | dev_pm_opp_put_opp_table(opp_table); |
| 172 | |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 173 | return clock_latency_ns; |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); |
| 176 | |
| 177 | /** |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 178 | * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds |
| 179 | * @dev: device for which we do this operation |
| 180 | * |
| 181 | * Return: This function returns the max voltage latency in nanoseconds. |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 182 | */ |
| 183 | unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) |
| 184 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 185 | struct opp_table *opp_table; |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 186 | struct dev_pm_opp *opp; |
Viresh Kumar | 478256b | 2017-05-23 09:32:11 +0530 | [diff] [blame] | 187 | struct regulator *reg; |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 188 | unsigned long latency_ns = 0; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 189 | int ret, i, count; |
| 190 | struct { |
| 191 | unsigned long min; |
| 192 | unsigned long max; |
| 193 | } *uV; |
| 194 | |
Viresh Kumar | cdd3e61 | 2017-01-23 10:11:51 +0530 | [diff] [blame] | 195 | opp_table = _find_opp_table(dev); |
| 196 | if (IS_ERR(opp_table)) |
| 197 | return 0; |
| 198 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 199 | /* Regulator may not be required for the device */ |
Viresh Kumar | 90e3577 | 2018-12-11 16:32:47 +0530 | [diff] [blame] | 200 | if (!opp_table->regulators) |
Viresh Kumar | cdd3e61 | 2017-01-23 10:11:51 +0530 | [diff] [blame] | 201 | goto put_opp_table; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 202 | |
Viresh Kumar | 90e3577 | 2018-12-11 16:32:47 +0530 | [diff] [blame] | 203 | count = opp_table->regulator_count; |
| 204 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 205 | uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); |
| 206 | if (!uV) |
Viresh Kumar | 478256b | 2017-05-23 09:32:11 +0530 | [diff] [blame] | 207 | goto put_opp_table; |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 208 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 209 | mutex_lock(&opp_table->lock); |
| 210 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 211 | for (i = 0; i < count; i++) { |
| 212 | uV[i].min = ~0; |
| 213 | uV[i].max = 0; |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 214 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 215 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 216 | if (!opp->available) |
| 217 | continue; |
| 218 | |
| 219 | if (opp->supplies[i].u_volt_min < uV[i].min) |
| 220 | uV[i].min = opp->supplies[i].u_volt_min; |
| 221 | if (opp->supplies[i].u_volt_max > uV[i].max) |
| 222 | uV[i].max = opp->supplies[i].u_volt_max; |
| 223 | } |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 224 | } |
| 225 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 226 | mutex_unlock(&opp_table->lock); |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 227 | |
| 228 | /* |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 229 | * The caller needs to ensure that opp_table (and hence the regulator) |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 230 | * isn't freed, while we are executing this routine. |
| 231 | */ |
Andrzej Hajda | 8cc3111 | 2017-02-20 19:57:57 +0100 | [diff] [blame] | 232 | for (i = 0; i < count; i++) { |
Viresh Kumar | 478256b | 2017-05-23 09:32:11 +0530 | [diff] [blame] | 233 | reg = opp_table->regulators[i]; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 234 | ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); |
| 235 | if (ret > 0) |
| 236 | latency_ns += ret * 1000; |
| 237 | } |
| 238 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 239 | kfree(uV); |
Viresh Kumar | cdd3e61 | 2017-01-23 10:11:51 +0530 | [diff] [blame] | 240 | put_opp_table: |
| 241 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 655c9df | 2016-02-09 10:30:35 +0530 | [diff] [blame] | 242 | |
| 243 | return latency_ns; |
| 244 | } |
| 245 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); |
| 246 | |
| 247 | /** |
Viresh Kumar | 2174344 | 2016-02-09 10:30:36 +0530 | [diff] [blame] | 248 | * dev_pm_opp_get_max_transition_latency() - Get max transition latency in |
| 249 | * nanoseconds |
| 250 | * @dev: device for which we do this operation |
| 251 | * |
| 252 | * Return: This function returns the max transition latency, in nanoseconds, to |
| 253 | * switch from one OPP to other. |
Viresh Kumar | 2174344 | 2016-02-09 10:30:36 +0530 | [diff] [blame] | 254 | */ |
| 255 | unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) |
| 256 | { |
| 257 | return dev_pm_opp_get_max_volt_latency(dev) + |
| 258 | dev_pm_opp_get_max_clock_latency(dev); |
| 259 | } |
| 260 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); |
| 261 | |
| 262 | /** |
Viresh Kumar | 3aa26a3 | 2017-01-02 14:41:02 +0530 | [diff] [blame] | 263 | * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz |
Bartlomiej Zolnierkiewicz | 4eafbd1 | 2015-09-08 18:41:01 +0200 | [diff] [blame] | 264 | * @dev: device for which we do this operation |
| 265 | * |
Viresh Kumar | 3aa26a3 | 2017-01-02 14:41:02 +0530 | [diff] [blame] | 266 | * Return: This function returns the frequency of the OPP marked as suspend_opp |
| 267 | * if one is available, else returns 0; |
Bartlomiej Zolnierkiewicz | 4eafbd1 | 2015-09-08 18:41:01 +0200 | [diff] [blame] | 268 | */ |
Viresh Kumar | 3aa26a3 | 2017-01-02 14:41:02 +0530 | [diff] [blame] | 269 | unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) |
Bartlomiej Zolnierkiewicz | 4eafbd1 | 2015-09-08 18:41:01 +0200 | [diff] [blame] | 270 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 271 | struct opp_table *opp_table; |
Viresh Kumar | 3aa26a3 | 2017-01-02 14:41:02 +0530 | [diff] [blame] | 272 | unsigned long freq = 0; |
Bartlomiej Zolnierkiewicz | 4eafbd1 | 2015-09-08 18:41:01 +0200 | [diff] [blame] | 273 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 274 | opp_table = _find_opp_table(dev); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 275 | if (IS_ERR(opp_table)) |
| 276 | return 0; |
Bartlomiej Zolnierkiewicz | 4eafbd1 | 2015-09-08 18:41:01 +0200 | [diff] [blame] | 277 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 278 | if (opp_table->suspend_opp && opp_table->suspend_opp->available) |
| 279 | freq = dev_pm_opp_get_freq(opp_table->suspend_opp); |
Viresh Kumar | 3aa26a3 | 2017-01-02 14:41:02 +0530 | [diff] [blame] | 280 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 281 | dev_pm_opp_put_opp_table(opp_table); |
| 282 | |
Viresh Kumar | 3aa26a3 | 2017-01-02 14:41:02 +0530 | [diff] [blame] | 283 | return freq; |
Bartlomiej Zolnierkiewicz | 4eafbd1 | 2015-09-08 18:41:01 +0200 | [diff] [blame] | 284 | } |
Viresh Kumar | 3aa26a3 | 2017-01-02 14:41:02 +0530 | [diff] [blame] | 285 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); |
Bartlomiej Zolnierkiewicz | 4eafbd1 | 2015-09-08 18:41:01 +0200 | [diff] [blame] | 286 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 287 | int _get_opp_count(struct opp_table *opp_table) |
| 288 | { |
| 289 | struct dev_pm_opp *opp; |
| 290 | int count = 0; |
| 291 | |
| 292 | mutex_lock(&opp_table->lock); |
| 293 | |
| 294 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
| 295 | if (opp->available) |
| 296 | count++; |
| 297 | } |
| 298 | |
| 299 | mutex_unlock(&opp_table->lock); |
| 300 | |
| 301 | return count; |
| 302 | } |
| 303 | |
Bartlomiej Zolnierkiewicz | 4eafbd1 | 2015-09-08 18:41:01 +0200 | [diff] [blame] | 304 | /** |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 305 | * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 306 | * @dev: device for which we do this operation |
| 307 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 308 | * Return: This function returns the number of available opps if there are any, |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 309 | * else returns 0 if none or the corresponding error value. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 310 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 311 | int dev_pm_opp_get_opp_count(struct device *dev) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 312 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 313 | struct opp_table *opp_table; |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 314 | int count; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 315 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 316 | opp_table = _find_opp_table(dev); |
| 317 | if (IS_ERR(opp_table)) { |
| 318 | count = PTR_ERR(opp_table); |
Fabio Estevam | 035ed07 | 2017-09-29 14:39:49 -0300 | [diff] [blame] | 319 | dev_dbg(dev, "%s: OPP table not found (%d)\n", |
Dmitry Torokhov | b4718c0 | 2014-12-16 15:09:38 -0800 | [diff] [blame] | 320 | __func__, count); |
Viresh Kumar | 09f662f | 2018-10-03 15:22:03 +0530 | [diff] [blame] | 321 | return count; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 324 | count = _get_opp_count(opp_table); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 325 | dev_pm_opp_put_opp_table(opp_table); |
| 326 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 327 | return count; |
| 328 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 329 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 330 | |
| 331 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 332 | * dev_pm_opp_find_freq_exact() - search for an exact frequency |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 333 | * @dev: device for which we do this operation |
| 334 | * @freq: frequency to search for |
Nishanth Menon | 7ae4961 | 2011-02-25 23:46:18 +0100 | [diff] [blame] | 335 | * @available: true/false - match for available opp |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 336 | * |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 337 | * Return: Searches for exact match in the opp table and returns pointer to the |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 338 | * matching opp if found, else returns ERR_PTR in case of error and should |
| 339 | * be handled using IS_ERR. Error return values can be: |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 340 | * EINVAL: for bad pointer |
| 341 | * ERANGE: no match found for search |
| 342 | * ENODEV: if device not found in list of registered devices |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 343 | * |
| 344 | * Note: available is a modifier for the search. if available=true, then the |
| 345 | * match is for exact matching frequency and is available in the stored OPP |
| 346 | * table. if false, the match is for exact frequency which is not available. |
| 347 | * |
| 348 | * This provides a mechanism to enable an opp which is not available currently |
| 349 | * or the opposite as well. |
| 350 | * |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 351 | * The callers are required to call dev_pm_opp_put() for the returned OPP after |
| 352 | * use. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 353 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 354 | struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, |
| 355 | unsigned long freq, |
| 356 | bool available) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 357 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 358 | struct opp_table *opp_table; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 359 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 360 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 361 | opp_table = _find_opp_table(dev); |
| 362 | if (IS_ERR(opp_table)) { |
| 363 | int r = PTR_ERR(opp_table); |
| 364 | |
| 365 | dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 366 | return ERR_PTR(r); |
| 367 | } |
| 368 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 369 | mutex_lock(&opp_table->lock); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 370 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 371 | list_for_each_entry(temp_opp, &opp_table->opp_list, node) { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 372 | if (temp_opp->available == available && |
| 373 | temp_opp->rate == freq) { |
| 374 | opp = temp_opp; |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 375 | |
| 376 | /* Increment the reference count of OPP */ |
| 377 | dev_pm_opp_get(opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 382 | mutex_unlock(&opp_table->lock); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 383 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 384 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 385 | return opp; |
| 386 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 387 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 388 | |
Jisheng Zhang | 067b7ce | 2016-07-25 14:11:16 +0800 | [diff] [blame] | 389 | static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, |
| 390 | unsigned long *freq) |
| 391 | { |
| 392 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
| 393 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 394 | mutex_lock(&opp_table->lock); |
| 395 | |
| 396 | list_for_each_entry(temp_opp, &opp_table->opp_list, node) { |
Jisheng Zhang | 067b7ce | 2016-07-25 14:11:16 +0800 | [diff] [blame] | 397 | if (temp_opp->available && temp_opp->rate >= *freq) { |
| 398 | opp = temp_opp; |
| 399 | *freq = opp->rate; |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 400 | |
| 401 | /* Increment the reference count of OPP */ |
| 402 | dev_pm_opp_get(opp); |
Jisheng Zhang | 067b7ce | 2016-07-25 14:11:16 +0800 | [diff] [blame] | 403 | break; |
| 404 | } |
| 405 | } |
| 406 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 407 | mutex_unlock(&opp_table->lock); |
| 408 | |
Jisheng Zhang | 067b7ce | 2016-07-25 14:11:16 +0800 | [diff] [blame] | 409 | return opp; |
| 410 | } |
| 411 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 412 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 413 | * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 414 | * @dev: device for which we do this operation |
| 415 | * @freq: Start frequency |
| 416 | * |
| 417 | * Search for the matching ceil *available* OPP from a starting freq |
| 418 | * for a device. |
| 419 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 420 | * Return: matching *opp and refreshes *freq accordingly, else returns |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 421 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
| 422 | * values can be: |
| 423 | * EINVAL: for bad pointer |
| 424 | * ERANGE: no match found for search |
| 425 | * ENODEV: if device not found in list of registered devices |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 426 | * |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 427 | * The callers are required to call dev_pm_opp_put() for the returned OPP after |
| 428 | * use. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 429 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 430 | struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, |
| 431 | unsigned long *freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 432 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 433 | struct opp_table *opp_table; |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 434 | struct dev_pm_opp *opp; |
Dmitry Torokhov | b02ded2 | 2014-12-16 15:09:36 -0800 | [diff] [blame] | 435 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 436 | if (!dev || !freq) { |
| 437 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); |
| 438 | return ERR_PTR(-EINVAL); |
| 439 | } |
| 440 | |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 441 | opp_table = _find_opp_table(dev); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 442 | if (IS_ERR(opp_table)) |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 443 | return ERR_CAST(opp_table); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 444 | |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 445 | opp = _find_freq_ceil(opp_table, freq); |
| 446 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 447 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 448 | |
| 449 | return opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 450 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 451 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 452 | |
| 453 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 454 | * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 455 | * @dev: device for which we do this operation |
| 456 | * @freq: Start frequency |
| 457 | * |
| 458 | * Search for the matching floor *available* OPP from a starting freq |
| 459 | * for a device. |
| 460 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 461 | * Return: matching *opp and refreshes *freq accordingly, else returns |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 462 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
| 463 | * values can be: |
| 464 | * EINVAL: for bad pointer |
| 465 | * ERANGE: no match found for search |
| 466 | * ENODEV: if device not found in list of registered devices |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 467 | * |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 468 | * The callers are required to call dev_pm_opp_put() for the returned OPP after |
| 469 | * use. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 470 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 471 | struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, |
| 472 | unsigned long *freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 473 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 474 | struct opp_table *opp_table; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 475 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 476 | |
| 477 | if (!dev || !freq) { |
| 478 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); |
| 479 | return ERR_PTR(-EINVAL); |
| 480 | } |
| 481 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 482 | opp_table = _find_opp_table(dev); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 483 | if (IS_ERR(opp_table)) |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 484 | return ERR_CAST(opp_table); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 485 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 486 | mutex_lock(&opp_table->lock); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 487 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 488 | list_for_each_entry(temp_opp, &opp_table->opp_list, node) { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 489 | if (temp_opp->available) { |
| 490 | /* go to the next node, before choosing prev */ |
| 491 | if (temp_opp->rate > *freq) |
| 492 | break; |
| 493 | else |
| 494 | opp = temp_opp; |
| 495 | } |
| 496 | } |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 497 | |
| 498 | /* Increment the reference count of OPP */ |
| 499 | if (!IS_ERR(opp)) |
| 500 | dev_pm_opp_get(opp); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 501 | mutex_unlock(&opp_table->lock); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 502 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 503 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 504 | if (!IS_ERR(opp)) |
| 505 | *freq = opp->rate; |
| 506 | |
| 507 | return opp; |
| 508 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 509 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 510 | |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 511 | static int _set_opp_voltage(struct device *dev, struct regulator *reg, |
Viresh Kumar | ce31781 | 2016-12-01 16:28:18 +0530 | [diff] [blame] | 512 | struct dev_pm_opp_supply *supply) |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 513 | { |
| 514 | int ret; |
| 515 | |
| 516 | /* Regulator not available for device */ |
| 517 | if (IS_ERR(reg)) { |
| 518 | dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, |
| 519 | PTR_ERR(reg)); |
| 520 | return 0; |
| 521 | } |
| 522 | |
Viresh Kumar | ce31781 | 2016-12-01 16:28:18 +0530 | [diff] [blame] | 523 | dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, |
| 524 | supply->u_volt_min, supply->u_volt, supply->u_volt_max); |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 525 | |
Viresh Kumar | ce31781 | 2016-12-01 16:28:18 +0530 | [diff] [blame] | 526 | ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, |
| 527 | supply->u_volt, supply->u_volt_max); |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 528 | if (ret) |
| 529 | dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", |
Viresh Kumar | ce31781 | 2016-12-01 16:28:18 +0530 | [diff] [blame] | 530 | __func__, supply->u_volt_min, supply->u_volt, |
| 531 | supply->u_volt_max, ret); |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 532 | |
| 533 | return ret; |
| 534 | } |
| 535 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 536 | static inline int |
| 537 | _generic_set_opp_clk_only(struct device *dev, struct clk *clk, |
| 538 | unsigned long old_freq, unsigned long freq) |
| 539 | { |
| 540 | int ret; |
| 541 | |
| 542 | ret = clk_set_rate(clk, freq); |
| 543 | if (ret) { |
| 544 | dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, |
| 545 | ret); |
| 546 | } |
| 547 | |
| 548 | return ret; |
| 549 | } |
| 550 | |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 551 | static int _generic_set_opp_regulator(const struct opp_table *opp_table, |
| 552 | struct device *dev, |
| 553 | unsigned long old_freq, |
| 554 | unsigned long freq, |
| 555 | struct dev_pm_opp_supply *old_supply, |
| 556 | struct dev_pm_opp_supply *new_supply) |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 557 | { |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 558 | struct regulator *reg = opp_table->regulators[0]; |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 559 | int ret; |
| 560 | |
| 561 | /* This function only supports single regulator per device */ |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 562 | if (WARN_ON(opp_table->regulator_count > 1)) { |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 563 | dev_err(dev, "multiple regulators are not supported\n"); |
| 564 | return -EINVAL; |
| 565 | } |
| 566 | |
| 567 | /* Scaling up? Scale voltage before frequency */ |
Waldemar Rymarkiewicz | c5c2a97 | 2018-06-14 15:56:08 +0200 | [diff] [blame] | 568 | if (freq >= old_freq) { |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 569 | ret = _set_opp_voltage(dev, reg, new_supply); |
| 570 | if (ret) |
| 571 | goto restore_voltage; |
| 572 | } |
| 573 | |
| 574 | /* Change frequency */ |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 575 | ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq); |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 576 | if (ret) |
| 577 | goto restore_voltage; |
| 578 | |
| 579 | /* Scaling down? Scale voltage after frequency */ |
| 580 | if (freq < old_freq) { |
| 581 | ret = _set_opp_voltage(dev, reg, new_supply); |
| 582 | if (ret) |
| 583 | goto restore_freq; |
| 584 | } |
| 585 | |
| 586 | return 0; |
| 587 | |
| 588 | restore_freq: |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 589 | if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq)) |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 590 | dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", |
| 591 | __func__, old_freq); |
| 592 | restore_voltage: |
| 593 | /* This shouldn't harm even if the voltages weren't updated earlier */ |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 594 | if (old_supply) |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 595 | _set_opp_voltage(dev, reg, old_supply); |
| 596 | |
| 597 | return ret; |
| 598 | } |
| 599 | |
Viresh Kumar | 7e535993 | 2018-06-12 14:47:03 +0530 | [diff] [blame] | 600 | static int _set_opp_custom(const struct opp_table *opp_table, |
| 601 | struct device *dev, unsigned long old_freq, |
| 602 | unsigned long freq, |
| 603 | struct dev_pm_opp_supply *old_supply, |
| 604 | struct dev_pm_opp_supply *new_supply) |
| 605 | { |
| 606 | struct dev_pm_set_opp_data *data; |
| 607 | int size; |
| 608 | |
| 609 | data = opp_table->set_opp_data; |
| 610 | data->regulators = opp_table->regulators; |
| 611 | data->regulator_count = opp_table->regulator_count; |
| 612 | data->clk = opp_table->clk; |
| 613 | data->dev = dev; |
| 614 | |
| 615 | data->old_opp.rate = old_freq; |
| 616 | size = sizeof(*old_supply) * opp_table->regulator_count; |
| 617 | if (IS_ERR(old_supply)) |
| 618 | memset(data->old_opp.supplies, 0, size); |
| 619 | else |
| 620 | memcpy(data->old_opp.supplies, old_supply, size); |
| 621 | |
| 622 | data->new_opp.rate = freq; |
| 623 | memcpy(data->new_opp.supplies, new_supply, size); |
| 624 | |
| 625 | return opp_table->set_opp(data); |
| 626 | } |
| 627 | |
Viresh Kumar | ca1b5d7 | 2018-06-14 10:03:26 +0530 | [diff] [blame] | 628 | /* This is only called for PM domain for now */ |
| 629 | static int _set_required_opps(struct device *dev, |
| 630 | struct opp_table *opp_table, |
| 631 | struct dev_pm_opp *opp) |
| 632 | { |
| 633 | struct opp_table **required_opp_tables = opp_table->required_opp_tables; |
| 634 | struct device **genpd_virt_devs = opp_table->genpd_virt_devs; |
| 635 | unsigned int pstate; |
| 636 | int i, ret = 0; |
| 637 | |
| 638 | if (!required_opp_tables) |
| 639 | return 0; |
| 640 | |
| 641 | /* Single genpd case */ |
| 642 | if (!genpd_virt_devs) { |
| 643 | pstate = opp->required_opps[0]->pstate; |
| 644 | ret = dev_pm_genpd_set_performance_state(dev, pstate); |
| 645 | if (ret) { |
| 646 | dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", |
| 647 | dev_name(dev), pstate, ret); |
| 648 | } |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | /* Multiple genpd case */ |
| 653 | |
| 654 | /* |
| 655 | * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev |
| 656 | * after it is freed from another thread. |
| 657 | */ |
| 658 | mutex_lock(&opp_table->genpd_virt_dev_lock); |
| 659 | |
| 660 | for (i = 0; i < opp_table->required_opp_count; i++) { |
| 661 | pstate = opp->required_opps[i]->pstate; |
| 662 | |
| 663 | if (!genpd_virt_devs[i]) |
| 664 | continue; |
| 665 | |
| 666 | ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate); |
| 667 | if (ret) { |
| 668 | dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n", |
| 669 | dev_name(genpd_virt_devs[i]), pstate, ret); |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | mutex_unlock(&opp_table->genpd_virt_dev_lock); |
| 674 | |
| 675 | return ret; |
| 676 | } |
| 677 | |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 678 | /** |
| 679 | * dev_pm_opp_set_rate() - Configure new OPP based on frequency |
| 680 | * @dev: device for which we do this operation |
| 681 | * @target_freq: frequency to achieve |
| 682 | * |
| 683 | * This configures the power-supplies and clock source to the levels specified |
| 684 | * by the OPP corresponding to the target_freq. |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 685 | */ |
| 686 | int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) |
| 687 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 688 | struct opp_table *opp_table; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 689 | unsigned long freq, old_freq; |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 690 | struct dev_pm_opp *old_opp, *opp; |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 691 | struct clk *clk; |
Viresh Kumar | 7e535993 | 2018-06-12 14:47:03 +0530 | [diff] [blame] | 692 | int ret; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 693 | |
| 694 | if (unlikely(!target_freq)) { |
| 695 | dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, |
| 696 | target_freq); |
| 697 | return -EINVAL; |
| 698 | } |
| 699 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 700 | opp_table = _find_opp_table(dev); |
| 701 | if (IS_ERR(opp_table)) { |
| 702 | dev_err(dev, "%s: device opp doesn't exist\n", __func__); |
| 703 | return PTR_ERR(opp_table); |
| 704 | } |
| 705 | |
| 706 | clk = opp_table->clk; |
| 707 | if (IS_ERR(clk)) { |
| 708 | dev_err(dev, "%s: No clock available for the device\n", |
| 709 | __func__); |
| 710 | ret = PTR_ERR(clk); |
| 711 | goto put_opp_table; |
| 712 | } |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 713 | |
| 714 | freq = clk_round_rate(clk, target_freq); |
| 715 | if ((long)freq <= 0) |
| 716 | freq = target_freq; |
| 717 | |
| 718 | old_freq = clk_get_rate(clk); |
| 719 | |
| 720 | /* Return early if nothing to do */ |
| 721 | if (old_freq == freq) { |
| 722 | dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", |
| 723 | __func__, freq); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 724 | ret = 0; |
| 725 | goto put_opp_table; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 726 | } |
| 727 | |
Jisheng Zhang | 067b7ce | 2016-07-25 14:11:16 +0800 | [diff] [blame] | 728 | old_opp = _find_freq_ceil(opp_table, &old_freq); |
Arnd Bergmann | 4df27c9 | 2016-09-15 17:38:38 +0200 | [diff] [blame] | 729 | if (IS_ERR(old_opp)) { |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 730 | dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", |
| 731 | __func__, old_freq, PTR_ERR(old_opp)); |
| 732 | } |
| 733 | |
Jisheng Zhang | 067b7ce | 2016-07-25 14:11:16 +0800 | [diff] [blame] | 734 | opp = _find_freq_ceil(opp_table, &freq); |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 735 | if (IS_ERR(opp)) { |
| 736 | ret = PTR_ERR(opp); |
| 737 | dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", |
| 738 | __func__, freq, ret); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 739 | goto put_old_opp; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 740 | } |
| 741 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 742 | dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, |
| 743 | old_freq, freq); |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 744 | |
Viresh Kumar | ca1b5d7 | 2018-06-14 10:03:26 +0530 | [diff] [blame] | 745 | /* Scaling up? Configure required OPPs before frequency */ |
| 746 | if (freq > old_freq) { |
| 747 | ret = _set_required_opps(dev, opp_table, opp); |
| 748 | if (ret) |
| 749 | goto put_opp; |
| 750 | } |
| 751 | |
Viresh Kumar | 7e535993 | 2018-06-12 14:47:03 +0530 | [diff] [blame] | 752 | if (opp_table->set_opp) { |
| 753 | ret = _set_opp_custom(opp_table, dev, old_freq, freq, |
| 754 | IS_ERR(old_opp) ? NULL : old_opp->supplies, |
| 755 | opp->supplies); |
| 756 | } else if (opp_table->regulators) { |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 757 | ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq, |
| 758 | IS_ERR(old_opp) ? NULL : old_opp->supplies, |
| 759 | opp->supplies); |
| 760 | } else { |
Viresh Kumar | 7e535993 | 2018-06-12 14:47:03 +0530 | [diff] [blame] | 761 | /* Only frequency scaling */ |
Viresh Kumar | ca1b5d7 | 2018-06-14 10:03:26 +0530 | [diff] [blame] | 762 | ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq); |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 763 | } |
| 764 | |
Viresh Kumar | ca1b5d7 | 2018-06-14 10:03:26 +0530 | [diff] [blame] | 765 | /* Scaling down? Configure required OPPs after frequency */ |
| 766 | if (!ret && freq < old_freq) { |
| 767 | ret = _set_required_opps(dev, opp_table, opp); |
| 768 | if (ret) |
| 769 | dev_err(dev, "Failed to set required opps: %d\n", ret); |
| 770 | } |
| 771 | |
| 772 | put_opp: |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 773 | dev_pm_opp_put(opp); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 774 | put_old_opp: |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 775 | if (!IS_ERR(old_opp)) |
| 776 | dev_pm_opp_put(old_opp); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 777 | put_opp_table: |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 778 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 779 | return ret; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 780 | } |
| 781 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); |
| 782 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 783 | /* OPP-dev Helpers */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 784 | static void _remove_opp_dev(struct opp_device *opp_dev, |
| 785 | struct opp_table *opp_table) |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 786 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 787 | opp_debug_unregister(opp_dev, opp_table); |
| 788 | list_del(&opp_dev->node); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 789 | kfree(opp_dev); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 790 | } |
| 791 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 792 | static struct opp_device *_add_opp_dev_unlocked(const struct device *dev, |
| 793 | struct opp_table *opp_table) |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 794 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 795 | struct opp_device *opp_dev; |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 796 | int ret; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 797 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 798 | opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); |
| 799 | if (!opp_dev) |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 800 | return NULL; |
| 801 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 802 | /* Initialize opp-dev */ |
| 803 | opp_dev->dev = dev; |
Viresh Kumar | 3d25569 | 2018-08-03 07:05:21 +0530 | [diff] [blame] | 804 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 805 | list_add(&opp_dev->node, &opp_table->dev_list); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 806 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 807 | /* Create debugfs entries for the opp_table */ |
| 808 | ret = opp_debug_register(opp_dev, opp_table); |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 809 | if (ret) |
| 810 | dev_err(dev, "%s: Failed to register opp debugfs (%d)\n", |
| 811 | __func__, ret); |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 812 | |
| 813 | return opp_dev; |
| 814 | } |
| 815 | |
| 816 | struct opp_device *_add_opp_dev(const struct device *dev, |
| 817 | struct opp_table *opp_table) |
| 818 | { |
| 819 | struct opp_device *opp_dev; |
| 820 | |
| 821 | mutex_lock(&opp_table->lock); |
| 822 | opp_dev = _add_opp_dev_unlocked(dev, opp_table); |
Viresh Kumar | 3d25569 | 2018-08-03 07:05:21 +0530 | [diff] [blame] | 823 | mutex_unlock(&opp_table->lock); |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 824 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 825 | return opp_dev; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 826 | } |
| 827 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 828 | static struct opp_table *_allocate_opp_table(struct device *dev, int index) |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 829 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 830 | struct opp_table *opp_table; |
| 831 | struct opp_device *opp_dev; |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 832 | int ret; |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 833 | |
| 834 | /* |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 835 | * Allocate a new OPP table. In the infrequent case where a new |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 836 | * device is needed to be added, we pay this penalty. |
| 837 | */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 838 | opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); |
| 839 | if (!opp_table) |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 840 | return NULL; |
| 841 | |
Viresh Kumar | 3d25569 | 2018-08-03 07:05:21 +0530 | [diff] [blame] | 842 | mutex_init(&opp_table->lock); |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 843 | mutex_init(&opp_table->genpd_virt_dev_lock); |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 844 | INIT_LIST_HEAD(&opp_table->dev_list); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 845 | |
Viresh Kumar | 46f48ac | 2018-12-11 16:39:36 +0530 | [diff] [blame] | 846 | /* Mark regulator count uninitialized */ |
| 847 | opp_table->regulator_count = -1; |
| 848 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 849 | opp_dev = _add_opp_dev(dev, opp_table); |
| 850 | if (!opp_dev) { |
| 851 | kfree(opp_table); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 852 | return NULL; |
| 853 | } |
| 854 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 855 | _of_init_opp_table(opp_table, dev, index); |
Viresh Kumar | 50f8cfb | 2016-02-09 10:30:37 +0530 | [diff] [blame] | 856 | |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 857 | /* Find clk for the device */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 858 | opp_table->clk = clk_get(dev, NULL); |
| 859 | if (IS_ERR(opp_table->clk)) { |
| 860 | ret = PTR_ERR(opp_table->clk); |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 861 | if (ret != -EPROBE_DEFER) |
| 862 | dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, |
| 863 | ret); |
| 864 | } |
| 865 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 866 | BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 867 | INIT_LIST_HEAD(&opp_table->opp_list); |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 868 | kref_init(&opp_table->kref); |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 869 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 870 | /* Secure the device table modification */ |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 871 | list_add(&opp_table->node, &opp_tables); |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 872 | return opp_table; |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 873 | } |
| 874 | |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 875 | void _get_opp_table_kref(struct opp_table *opp_table) |
Viresh Kumar | 3bac42c | 2015-07-29 16:22:59 +0530 | [diff] [blame] | 876 | { |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 877 | kref_get(&opp_table->kref); |
| 878 | } |
| 879 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 880 | static struct opp_table *_opp_get_opp_table(struct device *dev, int index) |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 881 | { |
| 882 | struct opp_table *opp_table; |
| 883 | |
| 884 | /* Hold our table modification lock here */ |
| 885 | mutex_lock(&opp_table_lock); |
| 886 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 887 | opp_table = _find_opp_table_unlocked(dev); |
| 888 | if (!IS_ERR(opp_table)) |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 889 | goto unlock; |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 890 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 891 | opp_table = _managed_opp(dev, index); |
| 892 | if (opp_table) { |
| 893 | if (!_add_opp_dev_unlocked(dev, opp_table)) { |
| 894 | dev_pm_opp_put_opp_table(opp_table); |
| 895 | opp_table = NULL; |
| 896 | } |
| 897 | goto unlock; |
| 898 | } |
| 899 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 900 | opp_table = _allocate_opp_table(dev, index); |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 901 | |
| 902 | unlock: |
| 903 | mutex_unlock(&opp_table_lock); |
| 904 | |
| 905 | return opp_table; |
| 906 | } |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 907 | |
| 908 | struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) |
| 909 | { |
| 910 | return _opp_get_opp_table(dev, 0); |
| 911 | } |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 912 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); |
| 913 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 914 | struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev, |
| 915 | int index) |
| 916 | { |
| 917 | return _opp_get_opp_table(dev, index); |
| 918 | } |
| 919 | |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 920 | static void _opp_table_kref_release(struct kref *kref) |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 921 | { |
| 922 | struct opp_table *opp_table = container_of(kref, struct opp_table, kref); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 923 | struct opp_device *opp_dev, *temp; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 924 | |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 925 | _of_clear_opp_table(opp_table); |
| 926 | |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 927 | /* Release clk */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 928 | if (!IS_ERR(opp_table->clk)) |
| 929 | clk_put(opp_table->clk); |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 930 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 931 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 932 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 933 | list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { |
| 934 | /* |
| 935 | * The OPP table is getting removed, drop the performance state |
| 936 | * constraints. |
| 937 | */ |
| 938 | if (opp_table->genpd_performance_state) |
| 939 | dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 940 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 941 | _remove_opp_dev(opp_dev, opp_table); |
| 942 | } |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 943 | |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 944 | mutex_destroy(&opp_table->genpd_virt_dev_lock); |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 945 | mutex_destroy(&opp_table->lock); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 946 | list_del(&opp_table->node); |
| 947 | kfree(opp_table); |
Viresh Kumar | 3bac42c | 2015-07-29 16:22:59 +0530 | [diff] [blame] | 948 | |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 949 | mutex_unlock(&opp_table_lock); |
| 950 | } |
| 951 | |
Viresh Kumar | d0e8ae6 | 2018-09-11 11:14:11 +0530 | [diff] [blame] | 952 | void _opp_remove_all_static(struct opp_table *opp_table) |
| 953 | { |
| 954 | struct dev_pm_opp *opp, *tmp; |
| 955 | |
| 956 | list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { |
| 957 | if (!opp->dynamic) |
| 958 | dev_pm_opp_put(opp); |
| 959 | } |
| 960 | |
| 961 | opp_table->parsed_static_opps = false; |
| 962 | } |
| 963 | |
| 964 | static void _opp_table_list_kref_release(struct kref *kref) |
| 965 | { |
| 966 | struct opp_table *opp_table = container_of(kref, struct opp_table, |
| 967 | list_kref); |
| 968 | |
| 969 | _opp_remove_all_static(opp_table); |
| 970 | mutex_unlock(&opp_table_lock); |
| 971 | } |
| 972 | |
| 973 | void _put_opp_list_kref(struct opp_table *opp_table) |
| 974 | { |
| 975 | kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release, |
| 976 | &opp_table_lock); |
| 977 | } |
| 978 | |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 979 | void dev_pm_opp_put_opp_table(struct opp_table *opp_table) |
| 980 | { |
| 981 | kref_put_mutex(&opp_table->kref, _opp_table_kref_release, |
| 982 | &opp_table_lock); |
| 983 | } |
| 984 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); |
| 985 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 986 | void _opp_free(struct dev_pm_opp *opp) |
Viresh Kumar | 969fceb | 2017-01-02 14:40:59 +0530 | [diff] [blame] | 987 | { |
| 988 | kfree(opp); |
Viresh Kumar | 969fceb | 2017-01-02 14:40:59 +0530 | [diff] [blame] | 989 | } |
| 990 | |
Viresh Kumar | 7034764 | 2017-01-23 10:11:46 +0530 | [diff] [blame] | 991 | static void _opp_kref_release(struct kref *kref) |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 992 | { |
Viresh Kumar | 7034764 | 2017-01-23 10:11:46 +0530 | [diff] [blame] | 993 | struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); |
| 994 | struct opp_table *opp_table = opp->opp_table; |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 995 | |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 996 | /* |
| 997 | * Notify the changes in the availability of the operable |
| 998 | * frequency/voltage list. |
| 999 | */ |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1000 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); |
Viresh Kumar | da544b6 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 1001 | _of_opp_free_required_opps(opp_table, opp); |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 1002 | opp_debug_remove_one(opp); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1003 | list_del(&opp->node); |
| 1004 | kfree(opp); |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1005 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1006 | mutex_unlock(&opp_table->lock); |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1007 | } |
| 1008 | |
Viresh Kumar | a88bd2a | 2017-11-29 15:18:36 +0530 | [diff] [blame] | 1009 | void dev_pm_opp_get(struct dev_pm_opp *opp) |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 1010 | { |
| 1011 | kref_get(&opp->kref); |
| 1012 | } |
| 1013 | |
Viresh Kumar | 7034764 | 2017-01-23 10:11:46 +0530 | [diff] [blame] | 1014 | void dev_pm_opp_put(struct dev_pm_opp *opp) |
| 1015 | { |
| 1016 | kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); |
| 1017 | } |
| 1018 | EXPORT_SYMBOL_GPL(dev_pm_opp_put); |
| 1019 | |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1020 | /** |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1021 | * dev_pm_opp_remove() - Remove an OPP from OPP table |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1022 | * @dev: device for which we do this operation |
| 1023 | * @freq: OPP to remove with matching 'freq' |
| 1024 | * |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1025 | * This function removes an opp from the opp table. |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1026 | */ |
| 1027 | void dev_pm_opp_remove(struct device *dev, unsigned long freq) |
| 1028 | { |
| 1029 | struct dev_pm_opp *opp; |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1030 | struct opp_table *opp_table; |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1031 | bool found = false; |
| 1032 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1033 | opp_table = _find_opp_table(dev); |
| 1034 | if (IS_ERR(opp_table)) |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1035 | return; |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1036 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1037 | mutex_lock(&opp_table->lock); |
| 1038 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1039 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1040 | if (opp->rate == freq) { |
| 1041 | found = true; |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1046 | mutex_unlock(&opp_table->lock); |
| 1047 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1048 | if (found) { |
| 1049 | dev_pm_opp_put(opp); |
Viresh Kumar | 0ad8c62 | 2018-08-02 14:23:09 +0530 | [diff] [blame] | 1050 | |
| 1051 | /* Drop the reference taken by dev_pm_opp_add() */ |
| 1052 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1053 | } else { |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1054 | dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", |
| 1055 | __func__, freq); |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1056 | } |
| 1057 | |
Viresh Kumar | 0ad8c62 | 2018-08-02 14:23:09 +0530 | [diff] [blame] | 1058 | /* Drop the reference taken by _find_opp_table() */ |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1059 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1060 | } |
| 1061 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove); |
| 1062 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1063 | struct dev_pm_opp *_opp_allocate(struct opp_table *table) |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1064 | { |
| 1065 | struct dev_pm_opp *opp; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1066 | int count, supply_size; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1067 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1068 | /* Allocate space for at least one supply */ |
Viresh Kumar | 46f48ac | 2018-12-11 16:39:36 +0530 | [diff] [blame] | 1069 | count = table->regulator_count > 0 ? table->regulator_count : 1; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1070 | supply_size = sizeof(*opp->supplies) * count; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1071 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1072 | /* allocate new OPP node and supplies structures */ |
| 1073 | opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1074 | if (!opp) |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1075 | return NULL; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1076 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1077 | /* Put the supplies at the end of the OPP structure as an empty array */ |
| 1078 | opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); |
| 1079 | INIT_LIST_HEAD(&opp->node); |
| 1080 | |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1081 | return opp; |
| 1082 | } |
| 1083 | |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1084 | static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1085 | struct opp_table *opp_table) |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1086 | { |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1087 | struct regulator *reg; |
| 1088 | int i; |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1089 | |
Viresh Kumar | 90e3577 | 2018-12-11 16:32:47 +0530 | [diff] [blame] | 1090 | if (!opp_table->regulators) |
| 1091 | return true; |
| 1092 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1093 | for (i = 0; i < opp_table->regulator_count; i++) { |
| 1094 | reg = opp_table->regulators[i]; |
| 1095 | |
| 1096 | if (!regulator_is_supported_voltage(reg, |
| 1097 | opp->supplies[i].u_volt_min, |
| 1098 | opp->supplies[i].u_volt_max)) { |
| 1099 | pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", |
| 1100 | __func__, opp->supplies[i].u_volt_min, |
| 1101 | opp->supplies[i].u_volt_max); |
| 1102 | return false; |
| 1103 | } |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | return true; |
| 1107 | } |
| 1108 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 1109 | static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, |
| 1110 | struct opp_table *opp_table, |
| 1111 | struct list_head **head) |
| 1112 | { |
| 1113 | struct dev_pm_opp *opp; |
| 1114 | |
| 1115 | /* |
| 1116 | * Insert new OPP in order of increasing frequency and discard if |
| 1117 | * already present. |
| 1118 | * |
| 1119 | * Need to use &opp_table->opp_list in the condition part of the 'for' |
| 1120 | * loop, don't replace it with head otherwise it will become an infinite |
| 1121 | * loop. |
| 1122 | */ |
| 1123 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
| 1124 | if (new_opp->rate > opp->rate) { |
| 1125 | *head = &opp->node; |
| 1126 | continue; |
| 1127 | } |
| 1128 | |
| 1129 | if (new_opp->rate < opp->rate) |
| 1130 | return 0; |
| 1131 | |
| 1132 | /* Duplicate OPPs */ |
| 1133 | dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", |
| 1134 | __func__, opp->rate, opp->supplies[0].u_volt, |
| 1135 | opp->available, new_opp->rate, |
| 1136 | new_opp->supplies[0].u_volt, new_opp->available); |
| 1137 | |
| 1138 | /* Should we compare voltages for all regulators here ? */ |
| 1139 | return opp->available && |
| 1140 | new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; |
| 1141 | } |
| 1142 | |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 1146 | /* |
| 1147 | * Returns: |
| 1148 | * 0: On success. And appropriate error message for duplicate OPPs. |
| 1149 | * -EBUSY: For OPP with same freq/volt and is available. The callers of |
| 1150 | * _opp_add() must return 0 if they receive -EBUSY from it. This is to make |
| 1151 | * sure we don't print error messages unnecessarily if different parts of |
| 1152 | * kernel try to initialize the OPP table. |
| 1153 | * -EEXIST: For OPP with same freq but different volt or is unavailable. This |
| 1154 | * should be considered an error by the callers of _opp_add(). |
| 1155 | */ |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 1156 | int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 1157 | struct opp_table *opp_table, bool rate_not_available) |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1158 | { |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1159 | struct list_head *head; |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 1160 | int ret; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1161 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1162 | mutex_lock(&opp_table->lock); |
| 1163 | head = &opp_table->opp_list; |
| 1164 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 1165 | if (likely(!rate_not_available)) { |
| 1166 | ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); |
| 1167 | if (ret) { |
| 1168 | mutex_unlock(&opp_table->lock); |
| 1169 | return ret; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1170 | } |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1171 | } |
| 1172 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1173 | list_add(&new_opp->node, head); |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1174 | mutex_unlock(&opp_table->lock); |
| 1175 | |
| 1176 | new_opp->opp_table = opp_table; |
Viresh Kumar | 7034764 | 2017-01-23 10:11:46 +0530 | [diff] [blame] | 1177 | kref_init(&new_opp->kref); |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1178 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1179 | ret = opp_debug_create_one(new_opp, opp_table); |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 1180 | if (ret) |
| 1181 | dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n", |
| 1182 | __func__, ret); |
| 1183 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1184 | if (!_opp_supported_by_regulators(new_opp, opp_table)) { |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1185 | new_opp->available = false; |
| 1186 | dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", |
| 1187 | __func__, new_opp->rate); |
| 1188 | } |
| 1189 | |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1193 | /** |
Viresh Kumar | b64b9c3f | 2015-10-15 21:42:44 +0530 | [diff] [blame] | 1194 | * _opp_add_v1() - Allocate a OPP based on v1 bindings. |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1195 | * @opp_table: OPP table |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1196 | * @dev: device for which we do this operation |
| 1197 | * @freq: Frequency in Hz for this OPP |
| 1198 | * @u_volt: Voltage in uVolts for this OPP |
| 1199 | * @dynamic: Dynamically added OPPs. |
| 1200 | * |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1201 | * This function adds an opp definition to the opp table and returns status. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1202 | * The opp is made available by default and it can be controlled using |
| 1203 | * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. |
| 1204 | * |
Viresh Kumar | 8f8d37b | 2015-09-04 13:47:24 +0530 | [diff] [blame] | 1205 | * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table |
| 1206 | * and freed by dev_pm_opp_of_remove_table. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1207 | * |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1208 | * Return: |
| 1209 | * 0 On success OR |
| 1210 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 1211 | * -EEXIST Freq are same and volt are different OR |
| 1212 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 1213 | * -ENOMEM Memory allocation failure |
| 1214 | */ |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1215 | int _opp_add_v1(struct opp_table *opp_table, struct device *dev, |
| 1216 | unsigned long freq, long u_volt, bool dynamic) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1217 | { |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1218 | struct dev_pm_opp *new_opp; |
Viresh Kumar | 50f8cfb | 2016-02-09 10:30:37 +0530 | [diff] [blame] | 1219 | unsigned long tol; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1220 | int ret; |
| 1221 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1222 | new_opp = _opp_allocate(opp_table); |
| 1223 | if (!new_opp) |
| 1224 | return -ENOMEM; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1225 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1226 | /* populate the opp table */ |
| 1227 | new_opp->rate = freq; |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1228 | tol = u_volt * opp_table->voltage_tolerance_v1 / 100; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1229 | new_opp->supplies[0].u_volt = u_volt; |
| 1230 | new_opp->supplies[0].u_volt_min = u_volt - tol; |
| 1231 | new_opp->supplies[0].u_volt_max = u_volt + tol; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1232 | new_opp->available = true; |
Viresh Kumar | aa5f2f8 | 2015-07-29 16:23:00 +0530 | [diff] [blame] | 1233 | new_opp->dynamic = dynamic; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1234 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 1235 | ret = _opp_add(dev, new_opp, opp_table, false); |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 1236 | if (ret) { |
| 1237 | /* Don't return error for duplicate OPPs */ |
| 1238 | if (ret == -EBUSY) |
| 1239 | ret = 0; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1240 | goto free_opp; |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 1241 | } |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1242 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1243 | /* |
| 1244 | * Notify the changes in the availability of the operable |
| 1245 | * frequency/voltage list. |
| 1246 | */ |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1247 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1248 | return 0; |
| 1249 | |
| 1250 | free_opp: |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1251 | _opp_free(new_opp); |
| 1252 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1253 | return ret; |
| 1254 | } |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 1255 | |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 1256 | /** |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1257 | * dev_pm_opp_set_supported_hw() - Set supported platforms |
| 1258 | * @dev: Device for which supported-hw has to be set. |
| 1259 | * @versions: Array of hierarchy of versions to match. |
| 1260 | * @count: Number of elements in the array. |
| 1261 | * |
| 1262 | * This is required only for the V2 bindings, and it enables a platform to |
| 1263 | * specify the hierarchy of versions it supports. OPP layer will then enable |
| 1264 | * OPPs, which are available for those versions, based on its 'opp-supported-hw' |
| 1265 | * property. |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1266 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1267 | struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, |
| 1268 | const u32 *versions, unsigned int count) |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1269 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1270 | struct opp_table *opp_table; |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1271 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1272 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1273 | if (!opp_table) |
| 1274 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1275 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1276 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1277 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1278 | |
Viresh Kumar | 25419de | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1279 | /* Another CPU that shares the OPP table has set the property ? */ |
| 1280 | if (opp_table->supported_hw) |
| 1281 | return opp_table; |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1282 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1283 | opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1284 | GFP_KERNEL); |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1285 | if (!opp_table->supported_hw) { |
Viresh Kumar | 25419de | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1286 | dev_pm_opp_put_opp_table(opp_table); |
| 1287 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1288 | } |
| 1289 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1290 | opp_table->supported_hw_count = count; |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1291 | |
| 1292 | return opp_table; |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1293 | } |
| 1294 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); |
| 1295 | |
| 1296 | /** |
| 1297 | * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1298 | * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1299 | * |
| 1300 | * This is required only for the V2 bindings, and is called for a matching |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1301 | * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1302 | * will not be freed. |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1303 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1304 | void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1305 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1306 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1307 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1308 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1309 | kfree(opp_table->supported_hw); |
| 1310 | opp_table->supported_hw = NULL; |
| 1311 | opp_table->supported_hw_count = 0; |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1312 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1313 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1314 | } |
| 1315 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); |
| 1316 | |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1317 | /** |
| 1318 | * dev_pm_opp_set_prop_name() - Set prop-extn name |
Viresh Kumar | a5da644 | 2016-02-16 14:17:52 +0530 | [diff] [blame] | 1319 | * @dev: Device for which the prop-name has to be set. |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1320 | * @name: name to postfix to properties. |
| 1321 | * |
| 1322 | * This is required only for the V2 bindings, and it enables a platform to |
| 1323 | * specify the extn to be used for certain property names. The properties to |
| 1324 | * which the extension will apply are opp-microvolt and opp-microamp. OPP core |
| 1325 | * should postfix the property name with -<name> while looking for them. |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1326 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1327 | struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1328 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1329 | struct opp_table *opp_table; |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1330 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1331 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1332 | if (!opp_table) |
| 1333 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1334 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1335 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1336 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1337 | |
Viresh Kumar | 878ec1a | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1338 | /* Another CPU that shares the OPP table has set the property ? */ |
| 1339 | if (opp_table->prop_name) |
| 1340 | return opp_table; |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1341 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1342 | opp_table->prop_name = kstrdup(name, GFP_KERNEL); |
| 1343 | if (!opp_table->prop_name) { |
Viresh Kumar | 878ec1a | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1344 | dev_pm_opp_put_opp_table(opp_table); |
| 1345 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1346 | } |
| 1347 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1348 | return opp_table; |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1349 | } |
| 1350 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); |
| 1351 | |
| 1352 | /** |
| 1353 | * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1354 | * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1355 | * |
| 1356 | * This is required only for the V2 bindings, and is called for a matching |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1357 | * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1358 | * will not be freed. |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1359 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1360 | void dev_pm_opp_put_prop_name(struct opp_table *opp_table) |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1361 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1362 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1363 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1364 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1365 | kfree(opp_table->prop_name); |
| 1366 | opp_table->prop_name = NULL; |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1367 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1368 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1369 | } |
| 1370 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); |
| 1371 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 1372 | static int _allocate_set_opp_data(struct opp_table *opp_table) |
| 1373 | { |
| 1374 | struct dev_pm_set_opp_data *data; |
| 1375 | int len, count = opp_table->regulator_count; |
| 1376 | |
Viresh Kumar | 90e3577 | 2018-12-11 16:32:47 +0530 | [diff] [blame] | 1377 | if (WARN_ON(!opp_table->regulators)) |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 1378 | return -EINVAL; |
| 1379 | |
| 1380 | /* space for set_opp_data */ |
| 1381 | len = sizeof(*data); |
| 1382 | |
| 1383 | /* space for old_opp.supplies and new_opp.supplies */ |
| 1384 | len += 2 * sizeof(struct dev_pm_opp_supply) * count; |
| 1385 | |
| 1386 | data = kzalloc(len, GFP_KERNEL); |
| 1387 | if (!data) |
| 1388 | return -ENOMEM; |
| 1389 | |
| 1390 | data->old_opp.supplies = (void *)(data + 1); |
| 1391 | data->new_opp.supplies = data->old_opp.supplies + count; |
| 1392 | |
| 1393 | opp_table->set_opp_data = data; |
| 1394 | |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
| 1398 | static void _free_set_opp_data(struct opp_table *opp_table) |
| 1399 | { |
| 1400 | kfree(opp_table->set_opp_data); |
| 1401 | opp_table->set_opp_data = NULL; |
| 1402 | } |
| 1403 | |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1404 | /** |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1405 | * dev_pm_opp_set_regulators() - Set regulator names for the device |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1406 | * @dev: Device for which regulator name is being set. |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1407 | * @names: Array of pointers to the names of the regulator. |
| 1408 | * @count: Number of regulators. |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1409 | * |
| 1410 | * In order to support OPP switching, OPP layer needs to know the name of the |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1411 | * device's regulators, as the core would be required to switch voltages as |
| 1412 | * well. |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1413 | * |
| 1414 | * This must be called before any OPPs are initialized for the device. |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1415 | */ |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1416 | struct opp_table *dev_pm_opp_set_regulators(struct device *dev, |
| 1417 | const char * const names[], |
| 1418 | unsigned int count) |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1419 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1420 | struct opp_table *opp_table; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1421 | struct regulator *reg; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1422 | int ret, i; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1423 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1424 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1425 | if (!opp_table) |
| 1426 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1427 | |
| 1428 | /* This should be called before OPPs are initialized */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1429 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1430 | ret = -EBUSY; |
| 1431 | goto err; |
| 1432 | } |
| 1433 | |
Viresh Kumar | 779b783 | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1434 | /* Another CPU that shares the OPP table has set the regulators ? */ |
| 1435 | if (opp_table->regulators) |
| 1436 | return opp_table; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1437 | |
| 1438 | opp_table->regulators = kmalloc_array(count, |
| 1439 | sizeof(*opp_table->regulators), |
| 1440 | GFP_KERNEL); |
| 1441 | if (!opp_table->regulators) { |
| 1442 | ret = -ENOMEM; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1443 | goto err; |
| 1444 | } |
| 1445 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1446 | for (i = 0; i < count; i++) { |
| 1447 | reg = regulator_get_optional(dev, names[i]); |
| 1448 | if (IS_ERR(reg)) { |
| 1449 | ret = PTR_ERR(reg); |
| 1450 | if (ret != -EPROBE_DEFER) |
| 1451 | dev_err(dev, "%s: no regulator (%s) found: %d\n", |
| 1452 | __func__, names[i], ret); |
| 1453 | goto free_regulators; |
| 1454 | } |
| 1455 | |
| 1456 | opp_table->regulators[i] = reg; |
| 1457 | } |
| 1458 | |
| 1459 | opp_table->regulator_count = count; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1460 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 1461 | /* Allocate block only once to pass to set_opp() routines */ |
| 1462 | ret = _allocate_set_opp_data(opp_table); |
| 1463 | if (ret) |
| 1464 | goto free_regulators; |
| 1465 | |
Stephen Boyd | 91291d9 | 2016-11-30 16:21:25 +0530 | [diff] [blame] | 1466 | return opp_table; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1467 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1468 | free_regulators: |
| 1469 | while (i != 0) |
| 1470 | regulator_put(opp_table->regulators[--i]); |
| 1471 | |
| 1472 | kfree(opp_table->regulators); |
| 1473 | opp_table->regulators = NULL; |
Viresh Kumar | 46f48ac | 2018-12-11 16:39:36 +0530 | [diff] [blame] | 1474 | opp_table->regulator_count = -1; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1475 | err: |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1476 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1477 | |
Stephen Boyd | 91291d9 | 2016-11-30 16:21:25 +0530 | [diff] [blame] | 1478 | return ERR_PTR(ret); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1479 | } |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1480 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1481 | |
| 1482 | /** |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1483 | * dev_pm_opp_put_regulators() - Releases resources blocked for regulator |
| 1484 | * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1485 | */ |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1486 | void dev_pm_opp_put_regulators(struct opp_table *opp_table) |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1487 | { |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1488 | int i; |
| 1489 | |
Viresh Kumar | 779b783 | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1490 | if (!opp_table->regulators) |
| 1491 | goto put_opp_table; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1492 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1493 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1494 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1495 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1496 | for (i = opp_table->regulator_count - 1; i >= 0; i--) |
| 1497 | regulator_put(opp_table->regulators[i]); |
| 1498 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 1499 | _free_set_opp_data(opp_table); |
| 1500 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1501 | kfree(opp_table->regulators); |
| 1502 | opp_table->regulators = NULL; |
Viresh Kumar | 46f48ac | 2018-12-11 16:39:36 +0530 | [diff] [blame] | 1503 | opp_table->regulator_count = -1; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1504 | |
Viresh Kumar | 779b783 | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1505 | put_opp_table: |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1506 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1507 | } |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1508 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1509 | |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 1510 | /** |
Viresh Kumar | 829a4e8 | 2017-06-21 10:29:13 +0530 | [diff] [blame] | 1511 | * dev_pm_opp_set_clkname() - Set clk name for the device |
| 1512 | * @dev: Device for which clk name is being set. |
| 1513 | * @name: Clk name. |
| 1514 | * |
| 1515 | * In order to support OPP switching, OPP layer needs to get pointer to the |
| 1516 | * clock for the device. Simple cases work fine without using this routine (i.e. |
| 1517 | * by passing connection-id as NULL), but for a device with multiple clocks |
| 1518 | * available, the OPP core needs to know the exact name of the clk to use. |
| 1519 | * |
| 1520 | * This must be called before any OPPs are initialized for the device. |
| 1521 | */ |
| 1522 | struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) |
| 1523 | { |
| 1524 | struct opp_table *opp_table; |
| 1525 | int ret; |
| 1526 | |
| 1527 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1528 | if (!opp_table) |
| 1529 | return ERR_PTR(-ENOMEM); |
| 1530 | |
| 1531 | /* This should be called before OPPs are initialized */ |
| 1532 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { |
| 1533 | ret = -EBUSY; |
| 1534 | goto err; |
| 1535 | } |
| 1536 | |
| 1537 | /* Already have default clk set, free it */ |
| 1538 | if (!IS_ERR(opp_table->clk)) |
| 1539 | clk_put(opp_table->clk); |
| 1540 | |
| 1541 | /* Find clk for the device */ |
| 1542 | opp_table->clk = clk_get(dev, name); |
| 1543 | if (IS_ERR(opp_table->clk)) { |
| 1544 | ret = PTR_ERR(opp_table->clk); |
| 1545 | if (ret != -EPROBE_DEFER) { |
| 1546 | dev_err(dev, "%s: Couldn't find clock: %d\n", __func__, |
| 1547 | ret); |
| 1548 | } |
| 1549 | goto err; |
| 1550 | } |
| 1551 | |
| 1552 | return opp_table; |
| 1553 | |
| 1554 | err: |
| 1555 | dev_pm_opp_put_opp_table(opp_table); |
| 1556 | |
| 1557 | return ERR_PTR(ret); |
| 1558 | } |
| 1559 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); |
| 1560 | |
| 1561 | /** |
| 1562 | * dev_pm_opp_put_clkname() - Releases resources blocked for clk. |
| 1563 | * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). |
| 1564 | */ |
| 1565 | void dev_pm_opp_put_clkname(struct opp_table *opp_table) |
| 1566 | { |
| 1567 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1568 | WARN_ON(!list_empty(&opp_table->opp_list)); |
| 1569 | |
| 1570 | clk_put(opp_table->clk); |
| 1571 | opp_table->clk = ERR_PTR(-EINVAL); |
| 1572 | |
| 1573 | dev_pm_opp_put_opp_table(opp_table); |
| 1574 | } |
| 1575 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); |
| 1576 | |
| 1577 | /** |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1578 | * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper |
| 1579 | * @dev: Device for which the helper is getting registered. |
| 1580 | * @set_opp: Custom set OPP helper. |
| 1581 | * |
| 1582 | * This is useful to support complex platforms (like platforms with multiple |
| 1583 | * regulators per device), instead of the generic OPP set rate helper. |
| 1584 | * |
| 1585 | * This must be called before any OPPs are initialized for the device. |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1586 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1587 | struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1588 | int (*set_opp)(struct dev_pm_set_opp_data *data)) |
| 1589 | { |
| 1590 | struct opp_table *opp_table; |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1591 | |
| 1592 | if (!set_opp) |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1593 | return ERR_PTR(-EINVAL); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1594 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1595 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1596 | if (!opp_table) |
| 1597 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1598 | |
| 1599 | /* This should be called before OPPs are initialized */ |
| 1600 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { |
Viresh Kumar | 5019acc | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1601 | dev_pm_opp_put_opp_table(opp_table); |
| 1602 | return ERR_PTR(-EBUSY); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1603 | } |
| 1604 | |
Viresh Kumar | 5019acc | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1605 | /* Another CPU that shares the OPP table has set the helper ? */ |
| 1606 | if (!opp_table->set_opp) |
| 1607 | opp_table->set_opp = set_opp; |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1608 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1609 | return opp_table; |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1610 | } |
| 1611 | EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); |
| 1612 | |
| 1613 | /** |
Viresh Kumar | 604a7ae | 2017-10-05 17:26:21 +0530 | [diff] [blame] | 1614 | * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1615 | * set_opp helper |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1616 | * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1617 | * |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1618 | * Release resources blocked for platform specific set_opp helper. |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1619 | */ |
Viresh Kumar | 604a7ae | 2017-10-05 17:26:21 +0530 | [diff] [blame] | 1620 | void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1621 | { |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1622 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1623 | WARN_ON(!list_empty(&opp_table->opp_list)); |
| 1624 | |
| 1625 | opp_table->set_opp = NULL; |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1626 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1627 | } |
Viresh Kumar | 604a7ae | 2017-10-05 17:26:21 +0530 | [diff] [blame] | 1628 | EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1629 | |
| 1630 | /** |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 1631 | * dev_pm_opp_set_genpd_virt_dev - Set virtual genpd device for an index |
| 1632 | * @dev: Consumer device for which the genpd device is getting set. |
| 1633 | * @virt_dev: virtual genpd device. |
| 1634 | * @index: index. |
| 1635 | * |
| 1636 | * Multiple generic power domains for a device are supported with the help of |
| 1637 | * virtual genpd devices, which are created for each consumer device - genpd |
| 1638 | * pair. These are the device structures which are attached to the power domain |
| 1639 | * and are required by the OPP core to set the performance state of the genpd. |
| 1640 | * |
| 1641 | * This helper will normally be called by the consumer driver of the device |
| 1642 | * "dev", as only that has details of the genpd devices. |
| 1643 | * |
| 1644 | * This helper needs to be called once for each of those virtual devices, but |
| 1645 | * only if multiple domains are available for a device. Otherwise the original |
| 1646 | * device structure will be used instead by the OPP core. |
| 1647 | */ |
| 1648 | struct opp_table *dev_pm_opp_set_genpd_virt_dev(struct device *dev, |
| 1649 | struct device *virt_dev, |
| 1650 | int index) |
| 1651 | { |
| 1652 | struct opp_table *opp_table; |
| 1653 | |
| 1654 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1655 | if (!opp_table) |
| 1656 | return ERR_PTR(-ENOMEM); |
| 1657 | |
| 1658 | mutex_lock(&opp_table->genpd_virt_dev_lock); |
| 1659 | |
| 1660 | if (unlikely(!opp_table->genpd_virt_devs || |
| 1661 | index >= opp_table->required_opp_count || |
| 1662 | opp_table->genpd_virt_devs[index])) { |
| 1663 | |
| 1664 | dev_err(dev, "Invalid request to set required device\n"); |
| 1665 | dev_pm_opp_put_opp_table(opp_table); |
| 1666 | mutex_unlock(&opp_table->genpd_virt_dev_lock); |
| 1667 | |
| 1668 | return ERR_PTR(-EINVAL); |
| 1669 | } |
| 1670 | |
| 1671 | opp_table->genpd_virt_devs[index] = virt_dev; |
| 1672 | mutex_unlock(&opp_table->genpd_virt_dev_lock); |
| 1673 | |
| 1674 | return opp_table; |
| 1675 | } |
| 1676 | |
| 1677 | /** |
| 1678 | * dev_pm_opp_put_genpd_virt_dev() - Releases resources blocked for genpd device. |
| 1679 | * @opp_table: OPP table returned by dev_pm_opp_set_genpd_virt_dev(). |
| 1680 | * @virt_dev: virtual genpd device. |
| 1681 | * |
| 1682 | * This releases the resource previously acquired with a call to |
| 1683 | * dev_pm_opp_set_genpd_virt_dev(). The consumer driver shall call this helper |
| 1684 | * if it doesn't want OPP core to update performance state of a power domain |
| 1685 | * anymore. |
| 1686 | */ |
| 1687 | void dev_pm_opp_put_genpd_virt_dev(struct opp_table *opp_table, |
| 1688 | struct device *virt_dev) |
| 1689 | { |
| 1690 | int i; |
| 1691 | |
| 1692 | /* |
| 1693 | * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting |
| 1694 | * used in parallel. |
| 1695 | */ |
| 1696 | mutex_lock(&opp_table->genpd_virt_dev_lock); |
| 1697 | |
| 1698 | for (i = 0; i < opp_table->required_opp_count; i++) { |
| 1699 | if (opp_table->genpd_virt_devs[i] != virt_dev) |
| 1700 | continue; |
| 1701 | |
| 1702 | opp_table->genpd_virt_devs[i] = NULL; |
| 1703 | dev_pm_opp_put_opp_table(opp_table); |
| 1704 | |
| 1705 | /* Drop the vote */ |
| 1706 | dev_pm_genpd_set_performance_state(virt_dev, 0); |
| 1707 | break; |
| 1708 | } |
| 1709 | |
| 1710 | mutex_unlock(&opp_table->genpd_virt_dev_lock); |
| 1711 | |
| 1712 | if (unlikely(i == opp_table->required_opp_count)) |
| 1713 | dev_err(virt_dev, "Failed to find required device entry\n"); |
| 1714 | } |
| 1715 | |
| 1716 | /** |
Viresh Kumar | c8a5910 | 2018-11-02 14:36:42 +0530 | [diff] [blame] | 1717 | * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. |
| 1718 | * @src_table: OPP table which has dst_table as one of its required OPP table. |
| 1719 | * @dst_table: Required OPP table of the src_table. |
| 1720 | * @pstate: Current performance state of the src_table. |
| 1721 | * |
| 1722 | * This Returns pstate of the OPP (present in @dst_table) pointed out by the |
| 1723 | * "required-opps" property of the OPP (present in @src_table) which has |
| 1724 | * performance state set to @pstate. |
| 1725 | * |
| 1726 | * Return: Zero or positive performance state on success, otherwise negative |
| 1727 | * value on errors. |
| 1728 | */ |
| 1729 | int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, |
| 1730 | struct opp_table *dst_table, |
| 1731 | unsigned int pstate) |
| 1732 | { |
| 1733 | struct dev_pm_opp *opp; |
| 1734 | int dest_pstate = -EINVAL; |
| 1735 | int i; |
| 1736 | |
| 1737 | if (!pstate) |
| 1738 | return 0; |
| 1739 | |
| 1740 | /* |
| 1741 | * Normally the src_table will have the "required_opps" property set to |
| 1742 | * point to one of the OPPs in the dst_table, but in some cases the |
| 1743 | * genpd and its master have one to one mapping of performance states |
| 1744 | * and so none of them have the "required-opps" property set. Return the |
| 1745 | * pstate of the src_table as it is in such cases. |
| 1746 | */ |
| 1747 | if (!src_table->required_opp_count) |
| 1748 | return pstate; |
| 1749 | |
| 1750 | for (i = 0; i < src_table->required_opp_count; i++) { |
| 1751 | if (src_table->required_opp_tables[i]->np == dst_table->np) |
| 1752 | break; |
| 1753 | } |
| 1754 | |
| 1755 | if (unlikely(i == src_table->required_opp_count)) { |
| 1756 | pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", |
| 1757 | __func__, src_table, dst_table); |
| 1758 | return -EINVAL; |
| 1759 | } |
| 1760 | |
| 1761 | mutex_lock(&src_table->lock); |
| 1762 | |
| 1763 | list_for_each_entry(opp, &src_table->opp_list, node) { |
| 1764 | if (opp->pstate == pstate) { |
| 1765 | dest_pstate = opp->required_opps[i]->pstate; |
| 1766 | goto unlock; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, |
| 1771 | dst_table); |
| 1772 | |
| 1773 | unlock: |
| 1774 | mutex_unlock(&src_table->lock); |
| 1775 | |
| 1776 | return dest_pstate; |
| 1777 | } |
| 1778 | |
| 1779 | /** |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1780 | * dev_pm_opp_add() - Add an OPP table from a table definitions |
| 1781 | * @dev: device for which we do this operation |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 1782 | * @freq: Frequency in Hz for this OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1783 | * @u_volt: Voltage in uVolts for this OPP |
| 1784 | * |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1785 | * This function adds an opp definition to the opp table and returns status. |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 1786 | * The opp is made available by default and it can be controlled using |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1787 | * dev_pm_opp_enable/disable functions. |
| 1788 | * |
Viresh Kumar | a7470db | 2014-11-25 16:04:17 +0530 | [diff] [blame] | 1789 | * Return: |
| 1790 | * 0 On success OR |
| 1791 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 1792 | * -EEXIST Freq are same and volt are different OR |
| 1793 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 1794 | * -ENOMEM Memory allocation failure |
Viresh Kumar | a7470db | 2014-11-25 16:04:17 +0530 | [diff] [blame] | 1795 | */ |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1796 | int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) |
| 1797 | { |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1798 | struct opp_table *opp_table; |
| 1799 | int ret; |
| 1800 | |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 1801 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1802 | if (!opp_table) |
| 1803 | return -ENOMEM; |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1804 | |
Viresh Kumar | 46f48ac | 2018-12-11 16:39:36 +0530 | [diff] [blame] | 1805 | /* Fix regulator count for dynamic OPPs */ |
| 1806 | opp_table->regulator_count = 1; |
| 1807 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1808 | ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); |
Viresh Kumar | 0ad8c62 | 2018-08-02 14:23:09 +0530 | [diff] [blame] | 1809 | if (ret) |
| 1810 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1811 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1812 | return ret; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1813 | } |
| 1814 | EXPORT_SYMBOL_GPL(dev_pm_opp_add); |
| 1815 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1816 | /** |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1817 | * _opp_set_availability() - helper to set the availability of an opp |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1818 | * @dev: device for which we do this operation |
| 1819 | * @freq: OPP frequency to modify availability |
| 1820 | * @availability_req: availability status requested for this opp |
| 1821 | * |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1822 | * Set the availability of an OPP, opp_{enable,disable} share a common logic |
| 1823 | * which is isolated here. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1824 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1825 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
Stephen Boyd | e1a2d49 | 2015-09-24 12:28:44 -0700 | [diff] [blame] | 1826 | * copy operation, returns 0 if no modification was done OR modification was |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1827 | * successful. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1828 | */ |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1829 | static int _opp_set_availability(struct device *dev, unsigned long freq, |
| 1830 | bool availability_req) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1831 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1832 | struct opp_table *opp_table; |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1833 | struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1834 | int r = 0; |
| 1835 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1836 | /* Find the opp_table */ |
| 1837 | opp_table = _find_opp_table(dev); |
| 1838 | if (IS_ERR(opp_table)) { |
| 1839 | r = PTR_ERR(opp_table); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1840 | dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1841 | return r; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1842 | } |
| 1843 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1844 | mutex_lock(&opp_table->lock); |
| 1845 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1846 | /* Do we have the frequency? */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1847 | list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1848 | if (tmp_opp->rate == freq) { |
| 1849 | opp = tmp_opp; |
| 1850 | break; |
| 1851 | } |
| 1852 | } |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1853 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1854 | if (IS_ERR(opp)) { |
| 1855 | r = PTR_ERR(opp); |
| 1856 | goto unlock; |
| 1857 | } |
| 1858 | |
| 1859 | /* Is update really needed? */ |
| 1860 | if (opp->available == availability_req) |
| 1861 | goto unlock; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1862 | |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1863 | opp->available = availability_req; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1864 | |
Viresh Kumar | e4d8ae0 | 2017-09-21 10:44:36 -0700 | [diff] [blame] | 1865 | dev_pm_opp_get(opp); |
| 1866 | mutex_unlock(&opp_table->lock); |
| 1867 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1868 | /* Notify the change of the OPP availability */ |
| 1869 | if (availability_req) |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1870 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1871 | opp); |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1872 | else |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1873 | blocking_notifier_call_chain(&opp_table->head, |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1874 | OPP_EVENT_DISABLE, opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1875 | |
Viresh Kumar | e4d8ae0 | 2017-09-21 10:44:36 -0700 | [diff] [blame] | 1876 | dev_pm_opp_put(opp); |
| 1877 | goto put_table; |
| 1878 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1879 | unlock: |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1880 | mutex_unlock(&opp_table->lock); |
Viresh Kumar | e4d8ae0 | 2017-09-21 10:44:36 -0700 | [diff] [blame] | 1881 | put_table: |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1882 | dev_pm_opp_put_opp_table(opp_table); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1883 | return r; |
| 1884 | } |
| 1885 | |
| 1886 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1887 | * dev_pm_opp_enable() - Enable a specific OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1888 | * @dev: device for which we do this operation |
| 1889 | * @freq: OPP frequency to enable |
| 1890 | * |
| 1891 | * Enables a provided opp. If the operation is valid, this returns 0, else the |
| 1892 | * corresponding error value. It is meant to be used for users an OPP available |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1893 | * after being temporarily made unavailable with dev_pm_opp_disable. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1894 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1895 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
Stephen Boyd | e1a2d49 | 2015-09-24 12:28:44 -0700 | [diff] [blame] | 1896 | * copy operation, returns 0 if no modification was done OR modification was |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1897 | * successful. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1898 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1899 | int dev_pm_opp_enable(struct device *dev, unsigned long freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1900 | { |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1901 | return _opp_set_availability(dev, freq, true); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1902 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1903 | EXPORT_SYMBOL_GPL(dev_pm_opp_enable); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1904 | |
| 1905 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1906 | * dev_pm_opp_disable() - Disable a specific OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1907 | * @dev: device for which we do this operation |
| 1908 | * @freq: OPP frequency to disable |
| 1909 | * |
| 1910 | * Disables a provided opp. If the operation is valid, this returns |
| 1911 | * 0, else the corresponding error value. It is meant to be a temporary |
| 1912 | * control by users to make this OPP not available until the circumstances are |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1913 | * right to make it available again (with a call to dev_pm_opp_enable). |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1914 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1915 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
Stephen Boyd | e1a2d49 | 2015-09-24 12:28:44 -0700 | [diff] [blame] | 1916 | * copy operation, returns 0 if no modification was done OR modification was |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1917 | * successful. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1918 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1919 | int dev_pm_opp_disable(struct device *dev, unsigned long freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1920 | { |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1921 | return _opp_set_availability(dev, freq, false); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1922 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1923 | EXPORT_SYMBOL_GPL(dev_pm_opp_disable); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1924 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1925 | /** |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1926 | * dev_pm_opp_register_notifier() - Register OPP notifier for the device |
| 1927 | * @dev: Device for which notifier needs to be registered |
| 1928 | * @nb: Notifier block to be registered |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1929 | * |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1930 | * Return: 0 on success or a negative error value. |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1931 | */ |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1932 | int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1933 | { |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1934 | struct opp_table *opp_table; |
| 1935 | int ret; |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1936 | |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1937 | opp_table = _find_opp_table(dev); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1938 | if (IS_ERR(opp_table)) |
| 1939 | return PTR_ERR(opp_table); |
| 1940 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1941 | ret = blocking_notifier_chain_register(&opp_table->head, nb); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1942 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1943 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1944 | |
| 1945 | return ret; |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1946 | } |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1947 | EXPORT_SYMBOL(dev_pm_opp_register_notifier); |
| 1948 | |
| 1949 | /** |
| 1950 | * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device |
| 1951 | * @dev: Device for which notifier needs to be unregistered |
| 1952 | * @nb: Notifier block to be unregistered |
| 1953 | * |
| 1954 | * Return: 0 on success or a negative error value. |
| 1955 | */ |
| 1956 | int dev_pm_opp_unregister_notifier(struct device *dev, |
| 1957 | struct notifier_block *nb) |
| 1958 | { |
| 1959 | struct opp_table *opp_table; |
| 1960 | int ret; |
| 1961 | |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1962 | opp_table = _find_opp_table(dev); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1963 | if (IS_ERR(opp_table)) |
| 1964 | return PTR_ERR(opp_table); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1965 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1966 | ret = blocking_notifier_chain_unregister(&opp_table->head, nb); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1967 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1968 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1969 | |
| 1970 | return ret; |
| 1971 | } |
| 1972 | EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 1973 | |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 1974 | void _dev_pm_opp_find_and_remove_table(struct device *dev) |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1975 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1976 | struct opp_table *opp_table; |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1977 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1978 | /* Check for existing table for 'dev' */ |
| 1979 | opp_table = _find_opp_table(dev); |
| 1980 | if (IS_ERR(opp_table)) { |
| 1981 | int error = PTR_ERR(opp_table); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1982 | |
| 1983 | if (error != -ENODEV) |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1984 | WARN(1, "%s: opp_table: %d\n", |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1985 | IS_ERR_OR_NULL(dev) ? |
| 1986 | "Invalid device" : dev_name(dev), |
| 1987 | error); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1988 | return; |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1989 | } |
| 1990 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 1991 | _put_opp_list_kref(opp_table); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1992 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 1993 | /* Drop reference taken by _find_opp_table() */ |
| 1994 | dev_pm_opp_put_opp_table(opp_table); |
| 1995 | |
| 1996 | /* Drop reference taken while the OPP table was added */ |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1997 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1998 | } |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1999 | |
| 2000 | /** |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 2001 | * dev_pm_opp_remove_table() - Free all OPPs associated with the device |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 2002 | * @dev: device pointer used to lookup OPP table. |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 2003 | * |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 2004 | * Free both OPPs created using static entries present in DT and the |
| 2005 | * dynamically added entries. |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 2006 | */ |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 2007 | void dev_pm_opp_remove_table(struct device *dev) |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 2008 | { |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 2009 | _dev_pm_opp_find_and_remove_table(dev); |
Viresh Kumar | 8d4d4e9 | 2015-06-12 17:10:38 +0530 | [diff] [blame] | 2010 | } |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 2011 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); |