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 | |
| 199 | count = opp_table->regulator_count; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 200 | |
| 201 | /* Regulator may not be required for the device */ |
| 202 | if (!count) |
Viresh Kumar | cdd3e61 | 2017-01-23 10:11:51 +0530 | [diff] [blame] | 203 | goto put_opp_table; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 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 | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 321 | return 0; |
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 | 009acd1 | 2017-10-11 12:54:14 +0530 | [diff] [blame] | 551 | static inline int |
| 552 | _generic_set_opp_domain(struct device *dev, struct clk *clk, |
| 553 | unsigned long old_freq, unsigned long freq, |
| 554 | unsigned int old_pstate, unsigned int new_pstate) |
| 555 | { |
| 556 | int ret; |
| 557 | |
| 558 | /* Scaling up? Scale domain performance state before frequency */ |
| 559 | if (freq > old_freq) { |
| 560 | ret = dev_pm_genpd_set_performance_state(dev, new_pstate); |
| 561 | if (ret) |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq); |
| 566 | if (ret) |
| 567 | goto restore_domain_state; |
| 568 | |
| 569 | /* Scaling down? Scale domain performance state after frequency */ |
| 570 | if (freq < old_freq) { |
| 571 | ret = dev_pm_genpd_set_performance_state(dev, new_pstate); |
| 572 | if (ret) |
| 573 | goto restore_freq; |
| 574 | } |
| 575 | |
| 576 | return 0; |
| 577 | |
| 578 | restore_freq: |
| 579 | if (_generic_set_opp_clk_only(dev, clk, freq, old_freq)) |
| 580 | dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", |
| 581 | __func__, old_freq); |
| 582 | restore_domain_state: |
| 583 | if (freq > old_freq) |
| 584 | dev_pm_genpd_set_performance_state(dev, old_pstate); |
| 585 | |
| 586 | return ret; |
| 587 | } |
| 588 | |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 589 | static int _generic_set_opp_regulator(const struct opp_table *opp_table, |
| 590 | struct device *dev, |
| 591 | unsigned long old_freq, |
| 592 | unsigned long freq, |
| 593 | struct dev_pm_opp_supply *old_supply, |
| 594 | struct dev_pm_opp_supply *new_supply) |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 595 | { |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 596 | struct regulator *reg = opp_table->regulators[0]; |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 597 | int ret; |
| 598 | |
| 599 | /* This function only supports single regulator per device */ |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 600 | if (WARN_ON(opp_table->regulator_count > 1)) { |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 601 | dev_err(dev, "multiple regulators are not supported\n"); |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | |
| 605 | /* Scaling up? Scale voltage before frequency */ |
Waldemar Rymarkiewicz | c5c2a97 | 2018-06-14 15:56:08 +0200 | [diff] [blame] | 606 | if (freq >= old_freq) { |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 607 | ret = _set_opp_voltage(dev, reg, new_supply); |
| 608 | if (ret) |
| 609 | goto restore_voltage; |
| 610 | } |
| 611 | |
| 612 | /* Change frequency */ |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 613 | 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] | 614 | if (ret) |
| 615 | goto restore_voltage; |
| 616 | |
| 617 | /* Scaling down? Scale voltage after frequency */ |
| 618 | if (freq < old_freq) { |
| 619 | ret = _set_opp_voltage(dev, reg, new_supply); |
| 620 | if (ret) |
| 621 | goto restore_freq; |
| 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | |
| 626 | restore_freq: |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 627 | 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] | 628 | dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", |
| 629 | __func__, old_freq); |
| 630 | restore_voltage: |
| 631 | /* This shouldn't harm even if the voltages weren't updated earlier */ |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 632 | if (old_supply) |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 633 | _set_opp_voltage(dev, reg, old_supply); |
| 634 | |
| 635 | return ret; |
| 636 | } |
| 637 | |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 638 | /** |
| 639 | * dev_pm_opp_set_rate() - Configure new OPP based on frequency |
| 640 | * @dev: device for which we do this operation |
| 641 | * @target_freq: frequency to achieve |
| 642 | * |
| 643 | * This configures the power-supplies and clock source to the levels specified |
| 644 | * by the OPP corresponding to the target_freq. |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 645 | */ |
| 646 | int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) |
| 647 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 648 | struct opp_table *opp_table; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 649 | unsigned long freq, old_freq; |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 650 | struct dev_pm_opp *old_opp, *opp; |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 651 | struct clk *clk; |
| 652 | int ret, size; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 653 | |
| 654 | if (unlikely(!target_freq)) { |
| 655 | dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, |
| 656 | target_freq); |
| 657 | return -EINVAL; |
| 658 | } |
| 659 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 660 | opp_table = _find_opp_table(dev); |
| 661 | if (IS_ERR(opp_table)) { |
| 662 | dev_err(dev, "%s: device opp doesn't exist\n", __func__); |
| 663 | return PTR_ERR(opp_table); |
| 664 | } |
| 665 | |
| 666 | clk = opp_table->clk; |
| 667 | if (IS_ERR(clk)) { |
| 668 | dev_err(dev, "%s: No clock available for the device\n", |
| 669 | __func__); |
| 670 | ret = PTR_ERR(clk); |
| 671 | goto put_opp_table; |
| 672 | } |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 673 | |
| 674 | freq = clk_round_rate(clk, target_freq); |
| 675 | if ((long)freq <= 0) |
| 676 | freq = target_freq; |
| 677 | |
| 678 | old_freq = clk_get_rate(clk); |
| 679 | |
| 680 | /* Return early if nothing to do */ |
| 681 | if (old_freq == freq) { |
| 682 | dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", |
| 683 | __func__, freq); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 684 | ret = 0; |
| 685 | goto put_opp_table; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 686 | } |
| 687 | |
Jisheng Zhang | 067b7ce | 2016-07-25 14:11:16 +0800 | [diff] [blame] | 688 | old_opp = _find_freq_ceil(opp_table, &old_freq); |
Arnd Bergmann | 4df27c9 | 2016-09-15 17:38:38 +0200 | [diff] [blame] | 689 | if (IS_ERR(old_opp)) { |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 690 | dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", |
| 691 | __func__, old_freq, PTR_ERR(old_opp)); |
| 692 | } |
| 693 | |
Jisheng Zhang | 067b7ce | 2016-07-25 14:11:16 +0800 | [diff] [blame] | 694 | opp = _find_freq_ceil(opp_table, &freq); |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 695 | if (IS_ERR(opp)) { |
| 696 | ret = PTR_ERR(opp); |
| 697 | dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", |
| 698 | __func__, freq, ret); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 699 | goto put_old_opp; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 700 | } |
| 701 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 702 | dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, |
| 703 | old_freq, freq); |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 704 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 705 | /* Only frequency scaling */ |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 706 | if (!opp_table->regulators) { |
Viresh Kumar | 009acd1 | 2017-10-11 12:54:14 +0530 | [diff] [blame] | 707 | /* |
| 708 | * We don't support devices with both regulator and |
| 709 | * domain performance-state for now. |
| 710 | */ |
| 711 | if (opp_table->genpd_performance_state) |
| 712 | ret = _generic_set_opp_domain(dev, clk, old_freq, freq, |
| 713 | IS_ERR(old_opp) ? 0 : old_opp->pstate, |
| 714 | opp->pstate); |
| 715 | else |
| 716 | ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq); |
Viresh Kumar | c74b32f | 2017-05-23 09:32:10 +0530 | [diff] [blame] | 717 | } else if (!opp_table->set_opp) { |
| 718 | ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq, |
| 719 | IS_ERR(old_opp) ? NULL : old_opp->supplies, |
| 720 | opp->supplies); |
| 721 | } else { |
| 722 | struct dev_pm_set_opp_data *data; |
| 723 | |
| 724 | data = opp_table->set_opp_data; |
| 725 | data->regulators = opp_table->regulators; |
| 726 | data->regulator_count = opp_table->regulator_count; |
| 727 | data->clk = clk; |
| 728 | data->dev = dev; |
| 729 | |
| 730 | data->old_opp.rate = old_freq; |
| 731 | size = sizeof(*opp->supplies) * opp_table->regulator_count; |
| 732 | if (IS_ERR(old_opp)) |
| 733 | memset(data->old_opp.supplies, 0, size); |
| 734 | else |
| 735 | memcpy(data->old_opp.supplies, old_opp->supplies, size); |
| 736 | |
| 737 | data->new_opp.rate = freq; |
| 738 | memcpy(data->new_opp.supplies, opp->supplies, size); |
| 739 | |
| 740 | ret = opp_table->set_opp(data); |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 741 | } |
| 742 | |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 743 | dev_pm_opp_put(opp); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 744 | put_old_opp: |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 745 | if (!IS_ERR(old_opp)) |
| 746 | dev_pm_opp_put(old_opp); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 747 | put_opp_table: |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 748 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 749 | return ret; |
Viresh Kumar | 6a0712f | 2016-02-09 10:30:39 +0530 | [diff] [blame] | 750 | } |
| 751 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); |
| 752 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 753 | /* OPP-dev Helpers */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 754 | static void _remove_opp_dev(struct opp_device *opp_dev, |
| 755 | struct opp_table *opp_table) |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 756 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 757 | opp_debug_unregister(opp_dev, opp_table); |
| 758 | list_del(&opp_dev->node); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 759 | kfree(opp_dev); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 760 | } |
| 761 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame^] | 762 | static struct opp_device *_add_opp_dev_unlocked(const struct device *dev, |
| 763 | struct opp_table *opp_table) |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 764 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 765 | struct opp_device *opp_dev; |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 766 | int ret; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 767 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 768 | opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); |
| 769 | if (!opp_dev) |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 770 | return NULL; |
| 771 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 772 | /* Initialize opp-dev */ |
| 773 | opp_dev->dev = dev; |
Viresh Kumar | 3d25569 | 2018-08-03 07:05:21 +0530 | [diff] [blame] | 774 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 775 | list_add(&opp_dev->node, &opp_table->dev_list); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 776 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 777 | /* Create debugfs entries for the opp_table */ |
| 778 | ret = opp_debug_register(opp_dev, opp_table); |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 779 | if (ret) |
| 780 | dev_err(dev, "%s: Failed to register opp debugfs (%d)\n", |
| 781 | __func__, ret); |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame^] | 782 | |
| 783 | return opp_dev; |
| 784 | } |
| 785 | |
| 786 | struct opp_device *_add_opp_dev(const struct device *dev, |
| 787 | struct opp_table *opp_table) |
| 788 | { |
| 789 | struct opp_device *opp_dev; |
| 790 | |
| 791 | mutex_lock(&opp_table->lock); |
| 792 | opp_dev = _add_opp_dev_unlocked(dev, opp_table); |
Viresh Kumar | 3d25569 | 2018-08-03 07:05:21 +0530 | [diff] [blame] | 793 | mutex_unlock(&opp_table->lock); |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 794 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 795 | return opp_dev; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 796 | } |
| 797 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 798 | static struct opp_table *_allocate_opp_table(struct device *dev, int index) |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 799 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 800 | struct opp_table *opp_table; |
| 801 | struct opp_device *opp_dev; |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 802 | int ret; |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 803 | |
| 804 | /* |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 805 | * Allocate a new OPP table. In the infrequent case where a new |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 806 | * device is needed to be added, we pay this penalty. |
| 807 | */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 808 | opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); |
| 809 | if (!opp_table) |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 810 | return NULL; |
| 811 | |
Viresh Kumar | 3d25569 | 2018-08-03 07:05:21 +0530 | [diff] [blame] | 812 | mutex_init(&opp_table->lock); |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 813 | INIT_LIST_HEAD(&opp_table->dev_list); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 814 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 815 | opp_dev = _add_opp_dev(dev, opp_table); |
| 816 | if (!opp_dev) { |
| 817 | kfree(opp_table); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 818 | return NULL; |
| 819 | } |
| 820 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 821 | _of_init_opp_table(opp_table, dev, index); |
Viresh Kumar | 50f8cfb | 2016-02-09 10:30:37 +0530 | [diff] [blame] | 822 | |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 823 | /* Find clk for the device */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 824 | opp_table->clk = clk_get(dev, NULL); |
| 825 | if (IS_ERR(opp_table->clk)) { |
| 826 | ret = PTR_ERR(opp_table->clk); |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 827 | if (ret != -EPROBE_DEFER) |
| 828 | dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, |
| 829 | ret); |
| 830 | } |
| 831 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 832 | BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 833 | INIT_LIST_HEAD(&opp_table->opp_list); |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 834 | kref_init(&opp_table->kref); |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 835 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 836 | /* Secure the device table modification */ |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 837 | list_add(&opp_table->node, &opp_tables); |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 838 | return opp_table; |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 839 | } |
| 840 | |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 841 | void _get_opp_table_kref(struct opp_table *opp_table) |
Viresh Kumar | 3bac42c | 2015-07-29 16:22:59 +0530 | [diff] [blame] | 842 | { |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 843 | kref_get(&opp_table->kref); |
| 844 | } |
| 845 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 846 | 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] | 847 | { |
| 848 | struct opp_table *opp_table; |
| 849 | |
| 850 | /* Hold our table modification lock here */ |
| 851 | mutex_lock(&opp_table_lock); |
| 852 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 853 | opp_table = _find_opp_table_unlocked(dev); |
| 854 | if (!IS_ERR(opp_table)) |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 855 | goto unlock; |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 856 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame^] | 857 | opp_table = _managed_opp(dev, index); |
| 858 | if (opp_table) { |
| 859 | if (!_add_opp_dev_unlocked(dev, opp_table)) { |
| 860 | dev_pm_opp_put_opp_table(opp_table); |
| 861 | opp_table = NULL; |
| 862 | } |
| 863 | goto unlock; |
| 864 | } |
| 865 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 866 | opp_table = _allocate_opp_table(dev, index); |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 867 | |
| 868 | unlock: |
| 869 | mutex_unlock(&opp_table_lock); |
| 870 | |
| 871 | return opp_table; |
| 872 | } |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 873 | |
| 874 | struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) |
| 875 | { |
| 876 | return _opp_get_opp_table(dev, 0); |
| 877 | } |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 878 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); |
| 879 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 880 | struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev, |
| 881 | int index) |
| 882 | { |
| 883 | return _opp_get_opp_table(dev, index); |
| 884 | } |
| 885 | |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 886 | static void _opp_table_kref_release(struct kref *kref) |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 887 | { |
| 888 | struct opp_table *opp_table = container_of(kref, struct opp_table, kref); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 889 | struct opp_device *opp_dev, *temp; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 890 | |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 891 | /* Release clk */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 892 | if (!IS_ERR(opp_table->clk)) |
| 893 | clk_put(opp_table->clk); |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 894 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 895 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 896 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 897 | list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { |
| 898 | /* |
| 899 | * The OPP table is getting removed, drop the performance state |
| 900 | * constraints. |
| 901 | */ |
| 902 | if (opp_table->genpd_performance_state) |
| 903 | dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 904 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 905 | _remove_opp_dev(opp_dev, opp_table); |
| 906 | } |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 907 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 908 | mutex_destroy(&opp_table->lock); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 909 | list_del(&opp_table->node); |
| 910 | kfree(opp_table); |
Viresh Kumar | 3bac42c | 2015-07-29 16:22:59 +0530 | [diff] [blame] | 911 | |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 912 | mutex_unlock(&opp_table_lock); |
| 913 | } |
| 914 | |
Viresh Kumar | d0e8ae6 | 2018-09-11 11:14:11 +0530 | [diff] [blame] | 915 | void _opp_remove_all_static(struct opp_table *opp_table) |
| 916 | { |
| 917 | struct dev_pm_opp *opp, *tmp; |
| 918 | |
| 919 | list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { |
| 920 | if (!opp->dynamic) |
| 921 | dev_pm_opp_put(opp); |
| 922 | } |
| 923 | |
| 924 | opp_table->parsed_static_opps = false; |
| 925 | } |
| 926 | |
| 927 | static void _opp_table_list_kref_release(struct kref *kref) |
| 928 | { |
| 929 | struct opp_table *opp_table = container_of(kref, struct opp_table, |
| 930 | list_kref); |
| 931 | |
| 932 | _opp_remove_all_static(opp_table); |
| 933 | mutex_unlock(&opp_table_lock); |
| 934 | } |
| 935 | |
| 936 | void _put_opp_list_kref(struct opp_table *opp_table) |
| 937 | { |
| 938 | kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release, |
| 939 | &opp_table_lock); |
| 940 | } |
| 941 | |
Viresh Kumar | f067a98 | 2017-01-23 10:11:42 +0530 | [diff] [blame] | 942 | void dev_pm_opp_put_opp_table(struct opp_table *opp_table) |
| 943 | { |
| 944 | kref_put_mutex(&opp_table->kref, _opp_table_kref_release, |
| 945 | &opp_table_lock); |
| 946 | } |
| 947 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); |
| 948 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 949 | void _opp_free(struct dev_pm_opp *opp) |
Viresh Kumar | 969fceb | 2017-01-02 14:40:59 +0530 | [diff] [blame] | 950 | { |
| 951 | kfree(opp); |
Viresh Kumar | 969fceb | 2017-01-02 14:40:59 +0530 | [diff] [blame] | 952 | } |
| 953 | |
Viresh Kumar | 7034764 | 2017-01-23 10:11:46 +0530 | [diff] [blame] | 954 | static void _opp_kref_release(struct kref *kref) |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 955 | { |
Viresh Kumar | 7034764 | 2017-01-23 10:11:46 +0530 | [diff] [blame] | 956 | struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); |
| 957 | struct opp_table *opp_table = opp->opp_table; |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 958 | |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 959 | /* |
| 960 | * Notify the changes in the availability of the operable |
| 961 | * frequency/voltage list. |
| 962 | */ |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 963 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 964 | opp_debug_remove_one(opp); |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 965 | list_del(&opp->node); |
| 966 | kfree(opp); |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 967 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 968 | mutex_unlock(&opp_table->lock); |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 969 | } |
| 970 | |
Viresh Kumar | a88bd2a | 2017-11-29 15:18:36 +0530 | [diff] [blame] | 971 | void dev_pm_opp_get(struct dev_pm_opp *opp) |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 972 | { |
| 973 | kref_get(&opp->kref); |
| 974 | } |
| 975 | |
Viresh Kumar | 7034764 | 2017-01-23 10:11:46 +0530 | [diff] [blame] | 976 | void dev_pm_opp_put(struct dev_pm_opp *opp) |
| 977 | { |
| 978 | kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); |
| 979 | } |
| 980 | EXPORT_SYMBOL_GPL(dev_pm_opp_put); |
| 981 | |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 982 | /** |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 983 | * dev_pm_opp_remove() - Remove an OPP from OPP table |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 984 | * @dev: device for which we do this operation |
| 985 | * @freq: OPP to remove with matching 'freq' |
| 986 | * |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 987 | * This function removes an opp from the opp table. |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 988 | */ |
| 989 | void dev_pm_opp_remove(struct device *dev, unsigned long freq) |
| 990 | { |
| 991 | struct dev_pm_opp *opp; |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 992 | struct opp_table *opp_table; |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 993 | bool found = false; |
| 994 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 995 | opp_table = _find_opp_table(dev); |
| 996 | if (IS_ERR(opp_table)) |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 997 | return; |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 998 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 999 | mutex_lock(&opp_table->lock); |
| 1000 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1001 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1002 | if (opp->rate == freq) { |
| 1003 | found = true; |
| 1004 | break; |
| 1005 | } |
| 1006 | } |
| 1007 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1008 | mutex_unlock(&opp_table->lock); |
| 1009 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1010 | if (found) { |
| 1011 | dev_pm_opp_put(opp); |
Viresh Kumar | 0ad8c62 | 2018-08-02 14:23:09 +0530 | [diff] [blame] | 1012 | |
| 1013 | /* Drop the reference taken by dev_pm_opp_add() */ |
| 1014 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1015 | } else { |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1016 | dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", |
| 1017 | __func__, freq); |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1018 | } |
| 1019 | |
Viresh Kumar | 0ad8c62 | 2018-08-02 14:23:09 +0530 | [diff] [blame] | 1020 | /* Drop the reference taken by _find_opp_table() */ |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1021 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1022 | } |
| 1023 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove); |
| 1024 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1025 | struct dev_pm_opp *_opp_allocate(struct opp_table *table) |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1026 | { |
| 1027 | struct dev_pm_opp *opp; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1028 | int count, supply_size; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1029 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1030 | /* Allocate space for at least one supply */ |
| 1031 | count = table->regulator_count ? table->regulator_count : 1; |
| 1032 | supply_size = sizeof(*opp->supplies) * count; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1033 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1034 | /* allocate new OPP node and supplies structures */ |
| 1035 | opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1036 | if (!opp) |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1037 | return NULL; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1038 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1039 | /* Put the supplies at the end of the OPP structure as an empty array */ |
| 1040 | opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); |
| 1041 | INIT_LIST_HEAD(&opp->node); |
| 1042 | |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1043 | return opp; |
| 1044 | } |
| 1045 | |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1046 | static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1047 | struct opp_table *opp_table) |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1048 | { |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1049 | struct regulator *reg; |
| 1050 | int i; |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1051 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1052 | for (i = 0; i < opp_table->regulator_count; i++) { |
| 1053 | reg = opp_table->regulators[i]; |
| 1054 | |
| 1055 | if (!regulator_is_supported_voltage(reg, |
| 1056 | opp->supplies[i].u_volt_min, |
| 1057 | opp->supplies[i].u_volt_max)) { |
| 1058 | pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", |
| 1059 | __func__, opp->supplies[i].u_volt_min, |
| 1060 | opp->supplies[i].u_volt_max); |
| 1061 | return false; |
| 1062 | } |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 1068 | static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, |
| 1069 | struct opp_table *opp_table, |
| 1070 | struct list_head **head) |
| 1071 | { |
| 1072 | struct dev_pm_opp *opp; |
| 1073 | |
| 1074 | /* |
| 1075 | * Insert new OPP in order of increasing frequency and discard if |
| 1076 | * already present. |
| 1077 | * |
| 1078 | * Need to use &opp_table->opp_list in the condition part of the 'for' |
| 1079 | * loop, don't replace it with head otherwise it will become an infinite |
| 1080 | * loop. |
| 1081 | */ |
| 1082 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
| 1083 | if (new_opp->rate > opp->rate) { |
| 1084 | *head = &opp->node; |
| 1085 | continue; |
| 1086 | } |
| 1087 | |
| 1088 | if (new_opp->rate < opp->rate) |
| 1089 | return 0; |
| 1090 | |
| 1091 | /* Duplicate OPPs */ |
| 1092 | dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", |
| 1093 | __func__, opp->rate, opp->supplies[0].u_volt, |
| 1094 | opp->available, new_opp->rate, |
| 1095 | new_opp->supplies[0].u_volt, new_opp->available); |
| 1096 | |
| 1097 | /* Should we compare voltages for all regulators here ? */ |
| 1098 | return opp->available && |
| 1099 | new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; |
| 1100 | } |
| 1101 | |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 1105 | /* |
| 1106 | * Returns: |
| 1107 | * 0: On success. And appropriate error message for duplicate OPPs. |
| 1108 | * -EBUSY: For OPP with same freq/volt and is available. The callers of |
| 1109 | * _opp_add() must return 0 if they receive -EBUSY from it. This is to make |
| 1110 | * sure we don't print error messages unnecessarily if different parts of |
| 1111 | * kernel try to initialize the OPP table. |
| 1112 | * -EEXIST: For OPP with same freq but different volt or is unavailable. This |
| 1113 | * should be considered an error by the callers of _opp_add(). |
| 1114 | */ |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 1115 | int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 1116 | struct opp_table *opp_table, bool rate_not_available) |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1117 | { |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1118 | struct list_head *head; |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 1119 | int ret; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1120 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1121 | mutex_lock(&opp_table->lock); |
| 1122 | head = &opp_table->opp_list; |
| 1123 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 1124 | if (likely(!rate_not_available)) { |
| 1125 | ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); |
| 1126 | if (ret) { |
| 1127 | mutex_unlock(&opp_table->lock); |
| 1128 | return ret; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1129 | } |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1130 | } |
| 1131 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1132 | list_add(&new_opp->node, head); |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1133 | mutex_unlock(&opp_table->lock); |
| 1134 | |
| 1135 | new_opp->opp_table = opp_table; |
Viresh Kumar | 7034764 | 2017-01-23 10:11:46 +0530 | [diff] [blame] | 1136 | kref_init(&new_opp->kref); |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1137 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1138 | ret = opp_debug_create_one(new_opp, opp_table); |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 1139 | if (ret) |
| 1140 | dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n", |
| 1141 | __func__, ret); |
| 1142 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1143 | if (!_opp_supported_by_regulators(new_opp, opp_table)) { |
Viresh Kumar | 7d34d56 | 2016-02-09 10:30:34 +0530 | [diff] [blame] | 1144 | new_opp->available = false; |
| 1145 | dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", |
| 1146 | __func__, new_opp->rate); |
| 1147 | } |
| 1148 | |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1149 | return 0; |
| 1150 | } |
| 1151 | |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1152 | /** |
Viresh Kumar | b64b9c3f | 2015-10-15 21:42:44 +0530 | [diff] [blame] | 1153 | * _opp_add_v1() - Allocate a OPP based on v1 bindings. |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1154 | * @opp_table: OPP table |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1155 | * @dev: device for which we do this operation |
| 1156 | * @freq: Frequency in Hz for this OPP |
| 1157 | * @u_volt: Voltage in uVolts for this OPP |
| 1158 | * @dynamic: Dynamically added OPPs. |
| 1159 | * |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1160 | * 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] | 1161 | * The opp is made available by default and it can be controlled using |
| 1162 | * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. |
| 1163 | * |
Viresh Kumar | 8f8d37b | 2015-09-04 13:47:24 +0530 | [diff] [blame] | 1164 | * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table |
| 1165 | * and freed by dev_pm_opp_of_remove_table. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1166 | * |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1167 | * Return: |
| 1168 | * 0 On success OR |
| 1169 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 1170 | * -EEXIST Freq are same and volt are different OR |
| 1171 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 1172 | * -ENOMEM Memory allocation failure |
| 1173 | */ |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1174 | int _opp_add_v1(struct opp_table *opp_table, struct device *dev, |
| 1175 | unsigned long freq, long u_volt, bool dynamic) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1176 | { |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1177 | struct dev_pm_opp *new_opp; |
Viresh Kumar | 50f8cfb | 2016-02-09 10:30:37 +0530 | [diff] [blame] | 1178 | unsigned long tol; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1179 | int ret; |
| 1180 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1181 | new_opp = _opp_allocate(opp_table); |
| 1182 | if (!new_opp) |
| 1183 | return -ENOMEM; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1184 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1185 | /* populate the opp table */ |
| 1186 | new_opp->rate = freq; |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1187 | tol = u_volt * opp_table->voltage_tolerance_v1 / 100; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1188 | new_opp->supplies[0].u_volt = u_volt; |
| 1189 | new_opp->supplies[0].u_volt_min = u_volt - tol; |
| 1190 | new_opp->supplies[0].u_volt_max = u_volt + tol; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1191 | new_opp->available = true; |
Viresh Kumar | aa5f2f8 | 2015-07-29 16:23:00 +0530 | [diff] [blame] | 1192 | new_opp->dynamic = dynamic; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1193 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 1194 | ret = _opp_add(dev, new_opp, opp_table, false); |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 1195 | if (ret) { |
| 1196 | /* Don't return error for duplicate OPPs */ |
| 1197 | if (ret == -EBUSY) |
| 1198 | ret = 0; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1199 | goto free_opp; |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 1200 | } |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 1201 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1202 | /* |
| 1203 | * Notify the changes in the availability of the operable |
| 1204 | * frequency/voltage list. |
| 1205 | */ |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1206 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1207 | return 0; |
| 1208 | |
| 1209 | free_opp: |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1210 | _opp_free(new_opp); |
| 1211 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1212 | return ret; |
| 1213 | } |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 1214 | |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 1215 | /** |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1216 | * dev_pm_opp_set_supported_hw() - Set supported platforms |
| 1217 | * @dev: Device for which supported-hw has to be set. |
| 1218 | * @versions: Array of hierarchy of versions to match. |
| 1219 | * @count: Number of elements in the array. |
| 1220 | * |
| 1221 | * This is required only for the V2 bindings, and it enables a platform to |
| 1222 | * specify the hierarchy of versions it supports. OPP layer will then enable |
| 1223 | * OPPs, which are available for those versions, based on its 'opp-supported-hw' |
| 1224 | * property. |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1225 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1226 | struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, |
| 1227 | const u32 *versions, unsigned int count) |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1228 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1229 | struct opp_table *opp_table; |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1230 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1231 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1232 | if (!opp_table) |
| 1233 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1234 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1235 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1236 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1237 | |
Viresh Kumar | 25419de | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1238 | /* Another CPU that shares the OPP table has set the property ? */ |
| 1239 | if (opp_table->supported_hw) |
| 1240 | return opp_table; |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1241 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1242 | opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1243 | GFP_KERNEL); |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1244 | if (!opp_table->supported_hw) { |
Viresh Kumar | 25419de | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1245 | dev_pm_opp_put_opp_table(opp_table); |
| 1246 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1247 | } |
| 1248 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1249 | opp_table->supported_hw_count = count; |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1250 | |
| 1251 | return opp_table; |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1252 | } |
| 1253 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); |
| 1254 | |
| 1255 | /** |
| 1256 | * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1257 | * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1258 | * |
| 1259 | * 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] | 1260 | * 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] | 1261 | * will not be freed. |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1262 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1263 | void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1264 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1265 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1266 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1267 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1268 | kfree(opp_table->supported_hw); |
| 1269 | opp_table->supported_hw = NULL; |
| 1270 | opp_table->supported_hw_count = 0; |
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 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 1273 | } |
| 1274 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); |
| 1275 | |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1276 | /** |
| 1277 | * dev_pm_opp_set_prop_name() - Set prop-extn name |
Viresh Kumar | a5da644 | 2016-02-16 14:17:52 +0530 | [diff] [blame] | 1278 | * @dev: Device for which the prop-name has to be set. |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1279 | * @name: name to postfix to properties. |
| 1280 | * |
| 1281 | * This is required only for the V2 bindings, and it enables a platform to |
| 1282 | * specify the extn to be used for certain property names. The properties to |
| 1283 | * which the extension will apply are opp-microvolt and opp-microamp. OPP core |
| 1284 | * should postfix the property name with -<name> while looking for them. |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1285 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1286 | 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] | 1287 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1288 | struct opp_table *opp_table; |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1289 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1290 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1291 | if (!opp_table) |
| 1292 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1293 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1294 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1295 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1296 | |
Viresh Kumar | 878ec1a | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1297 | /* Another CPU that shares the OPP table has set the property ? */ |
| 1298 | if (opp_table->prop_name) |
| 1299 | return opp_table; |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1300 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1301 | opp_table->prop_name = kstrdup(name, GFP_KERNEL); |
| 1302 | if (!opp_table->prop_name) { |
Viresh Kumar | 878ec1a | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1303 | dev_pm_opp_put_opp_table(opp_table); |
| 1304 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1305 | } |
| 1306 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1307 | return opp_table; |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1308 | } |
| 1309 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); |
| 1310 | |
| 1311 | /** |
| 1312 | * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1313 | * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1314 | * |
| 1315 | * 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] | 1316 | * 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] | 1317 | * will not be freed. |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1318 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1319 | void dev_pm_opp_put_prop_name(struct opp_table *opp_table) |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1320 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1321 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1322 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1323 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1324 | kfree(opp_table->prop_name); |
| 1325 | opp_table->prop_name = NULL; |
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 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 1328 | } |
| 1329 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); |
| 1330 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 1331 | static int _allocate_set_opp_data(struct opp_table *opp_table) |
| 1332 | { |
| 1333 | struct dev_pm_set_opp_data *data; |
| 1334 | int len, count = opp_table->regulator_count; |
| 1335 | |
| 1336 | if (WARN_ON(!count)) |
| 1337 | return -EINVAL; |
| 1338 | |
| 1339 | /* space for set_opp_data */ |
| 1340 | len = sizeof(*data); |
| 1341 | |
| 1342 | /* space for old_opp.supplies and new_opp.supplies */ |
| 1343 | len += 2 * sizeof(struct dev_pm_opp_supply) * count; |
| 1344 | |
| 1345 | data = kzalloc(len, GFP_KERNEL); |
| 1346 | if (!data) |
| 1347 | return -ENOMEM; |
| 1348 | |
| 1349 | data->old_opp.supplies = (void *)(data + 1); |
| 1350 | data->new_opp.supplies = data->old_opp.supplies + count; |
| 1351 | |
| 1352 | opp_table->set_opp_data = data; |
| 1353 | |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | static void _free_set_opp_data(struct opp_table *opp_table) |
| 1358 | { |
| 1359 | kfree(opp_table->set_opp_data); |
| 1360 | opp_table->set_opp_data = NULL; |
| 1361 | } |
| 1362 | |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1363 | /** |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1364 | * dev_pm_opp_set_regulators() - Set regulator names for the device |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1365 | * @dev: Device for which regulator name is being set. |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1366 | * @names: Array of pointers to the names of the regulator. |
| 1367 | * @count: Number of regulators. |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1368 | * |
| 1369 | * 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] | 1370 | * device's regulators, as the core would be required to switch voltages as |
| 1371 | * well. |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1372 | * |
| 1373 | * This must be called before any OPPs are initialized for the device. |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1374 | */ |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1375 | struct opp_table *dev_pm_opp_set_regulators(struct device *dev, |
| 1376 | const char * const names[], |
| 1377 | unsigned int count) |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1378 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1379 | struct opp_table *opp_table; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1380 | struct regulator *reg; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1381 | int ret, i; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1382 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1383 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1384 | if (!opp_table) |
| 1385 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1386 | |
| 1387 | /* This should be called before OPPs are initialized */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1388 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1389 | ret = -EBUSY; |
| 1390 | goto err; |
| 1391 | } |
| 1392 | |
Viresh Kumar | 779b783 | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1393 | /* Another CPU that shares the OPP table has set the regulators ? */ |
| 1394 | if (opp_table->regulators) |
| 1395 | return opp_table; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1396 | |
| 1397 | opp_table->regulators = kmalloc_array(count, |
| 1398 | sizeof(*opp_table->regulators), |
| 1399 | GFP_KERNEL); |
| 1400 | if (!opp_table->regulators) { |
| 1401 | ret = -ENOMEM; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1402 | goto err; |
| 1403 | } |
| 1404 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1405 | for (i = 0; i < count; i++) { |
| 1406 | reg = regulator_get_optional(dev, names[i]); |
| 1407 | if (IS_ERR(reg)) { |
| 1408 | ret = PTR_ERR(reg); |
| 1409 | if (ret != -EPROBE_DEFER) |
| 1410 | dev_err(dev, "%s: no regulator (%s) found: %d\n", |
| 1411 | __func__, names[i], ret); |
| 1412 | goto free_regulators; |
| 1413 | } |
| 1414 | |
| 1415 | opp_table->regulators[i] = reg; |
| 1416 | } |
| 1417 | |
| 1418 | opp_table->regulator_count = count; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1419 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 1420 | /* Allocate block only once to pass to set_opp() routines */ |
| 1421 | ret = _allocate_set_opp_data(opp_table); |
| 1422 | if (ret) |
| 1423 | goto free_regulators; |
| 1424 | |
Stephen Boyd | 91291d9 | 2016-11-30 16:21:25 +0530 | [diff] [blame] | 1425 | return opp_table; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1426 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1427 | free_regulators: |
| 1428 | while (i != 0) |
| 1429 | regulator_put(opp_table->regulators[--i]); |
| 1430 | |
| 1431 | kfree(opp_table->regulators); |
| 1432 | opp_table->regulators = NULL; |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 1433 | opp_table->regulator_count = 0; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1434 | err: |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1435 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1436 | |
Stephen Boyd | 91291d9 | 2016-11-30 16:21:25 +0530 | [diff] [blame] | 1437 | return ERR_PTR(ret); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1438 | } |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1439 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1440 | |
| 1441 | /** |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1442 | * dev_pm_opp_put_regulators() - Releases resources blocked for regulator |
| 1443 | * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1444 | */ |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1445 | void dev_pm_opp_put_regulators(struct opp_table *opp_table) |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1446 | { |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1447 | int i; |
| 1448 | |
Viresh Kumar | 779b783 | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1449 | if (!opp_table->regulators) |
| 1450 | goto put_opp_table; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1451 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1452 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1453 | WARN_ON(!list_empty(&opp_table->opp_list)); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1454 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1455 | for (i = opp_table->regulator_count - 1; i >= 0; i--) |
| 1456 | regulator_put(opp_table->regulators[i]); |
| 1457 | |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 1458 | _free_set_opp_data(opp_table); |
| 1459 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1460 | kfree(opp_table->regulators); |
| 1461 | opp_table->regulators = NULL; |
| 1462 | opp_table->regulator_count = 0; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1463 | |
Viresh Kumar | 779b783 | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1464 | put_opp_table: |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1465 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1466 | } |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 1467 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 1468 | |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 1469 | /** |
Viresh Kumar | 829a4e8 | 2017-06-21 10:29:13 +0530 | [diff] [blame] | 1470 | * dev_pm_opp_set_clkname() - Set clk name for the device |
| 1471 | * @dev: Device for which clk name is being set. |
| 1472 | * @name: Clk name. |
| 1473 | * |
| 1474 | * In order to support OPP switching, OPP layer needs to get pointer to the |
| 1475 | * clock for the device. Simple cases work fine without using this routine (i.e. |
| 1476 | * by passing connection-id as NULL), but for a device with multiple clocks |
| 1477 | * available, the OPP core needs to know the exact name of the clk to use. |
| 1478 | * |
| 1479 | * This must be called before any OPPs are initialized for the device. |
| 1480 | */ |
| 1481 | struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) |
| 1482 | { |
| 1483 | struct opp_table *opp_table; |
| 1484 | int ret; |
| 1485 | |
| 1486 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1487 | if (!opp_table) |
| 1488 | return ERR_PTR(-ENOMEM); |
| 1489 | |
| 1490 | /* This should be called before OPPs are initialized */ |
| 1491 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { |
| 1492 | ret = -EBUSY; |
| 1493 | goto err; |
| 1494 | } |
| 1495 | |
| 1496 | /* Already have default clk set, free it */ |
| 1497 | if (!IS_ERR(opp_table->clk)) |
| 1498 | clk_put(opp_table->clk); |
| 1499 | |
| 1500 | /* Find clk for the device */ |
| 1501 | opp_table->clk = clk_get(dev, name); |
| 1502 | if (IS_ERR(opp_table->clk)) { |
| 1503 | ret = PTR_ERR(opp_table->clk); |
| 1504 | if (ret != -EPROBE_DEFER) { |
| 1505 | dev_err(dev, "%s: Couldn't find clock: %d\n", __func__, |
| 1506 | ret); |
| 1507 | } |
| 1508 | goto err; |
| 1509 | } |
| 1510 | |
| 1511 | return opp_table; |
| 1512 | |
| 1513 | err: |
| 1514 | dev_pm_opp_put_opp_table(opp_table); |
| 1515 | |
| 1516 | return ERR_PTR(ret); |
| 1517 | } |
| 1518 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); |
| 1519 | |
| 1520 | /** |
| 1521 | * dev_pm_opp_put_clkname() - Releases resources blocked for clk. |
| 1522 | * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). |
| 1523 | */ |
| 1524 | void dev_pm_opp_put_clkname(struct opp_table *opp_table) |
| 1525 | { |
| 1526 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1527 | WARN_ON(!list_empty(&opp_table->opp_list)); |
| 1528 | |
| 1529 | clk_put(opp_table->clk); |
| 1530 | opp_table->clk = ERR_PTR(-EINVAL); |
| 1531 | |
| 1532 | dev_pm_opp_put_opp_table(opp_table); |
| 1533 | } |
| 1534 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); |
| 1535 | |
| 1536 | /** |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1537 | * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper |
| 1538 | * @dev: Device for which the helper is getting registered. |
| 1539 | * @set_opp: Custom set OPP helper. |
| 1540 | * |
| 1541 | * This is useful to support complex platforms (like platforms with multiple |
| 1542 | * regulators per device), instead of the generic OPP set rate helper. |
| 1543 | * |
| 1544 | * This must be called before any OPPs are initialized for the device. |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1545 | */ |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1546 | 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] | 1547 | int (*set_opp)(struct dev_pm_set_opp_data *data)) |
| 1548 | { |
| 1549 | struct opp_table *opp_table; |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1550 | |
| 1551 | if (!set_opp) |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1552 | return ERR_PTR(-EINVAL); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1553 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1554 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1555 | if (!opp_table) |
| 1556 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1557 | |
| 1558 | /* This should be called before OPPs are initialized */ |
| 1559 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { |
Viresh Kumar | 5019acc | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1560 | dev_pm_opp_put_opp_table(opp_table); |
| 1561 | return ERR_PTR(-EBUSY); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1562 | } |
| 1563 | |
Viresh Kumar | 5019acc | 2018-05-22 16:38:08 +0530 | [diff] [blame] | 1564 | /* Another CPU that shares the OPP table has set the helper ? */ |
| 1565 | if (!opp_table->set_opp) |
| 1566 | opp_table->set_opp = set_opp; |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1567 | |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1568 | return opp_table; |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1569 | } |
| 1570 | EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); |
| 1571 | |
| 1572 | /** |
Viresh Kumar | 604a7ae | 2017-10-05 17:26:21 +0530 | [diff] [blame] | 1573 | * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1574 | * set_opp helper |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1575 | * @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] | 1576 | * |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1577 | * Release resources blocked for platform specific set_opp helper. |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1578 | */ |
Viresh Kumar | 604a7ae | 2017-10-05 17:26:21 +0530 | [diff] [blame] | 1579 | 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] | 1580 | { |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1581 | /* Make sure there are no concurrent readers while updating opp_table */ |
| 1582 | WARN_ON(!list_empty(&opp_table->opp_list)); |
| 1583 | |
| 1584 | opp_table->set_opp = NULL; |
Viresh Kumar | fa30184 | 2017-01-23 10:11:43 +0530 | [diff] [blame] | 1585 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1586 | } |
Viresh Kumar | 604a7ae | 2017-10-05 17:26:21 +0530 | [diff] [blame] | 1587 | EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 1588 | |
| 1589 | /** |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1590 | * dev_pm_opp_add() - Add an OPP table from a table definitions |
| 1591 | * @dev: device for which we do this operation |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 1592 | * @freq: Frequency in Hz for this OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1593 | * @u_volt: Voltage in uVolts for this OPP |
| 1594 | * |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1595 | * 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] | 1596 | * 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] | 1597 | * dev_pm_opp_enable/disable functions. |
| 1598 | * |
Viresh Kumar | a7470db | 2014-11-25 16:04:17 +0530 | [diff] [blame] | 1599 | * Return: |
| 1600 | * 0 On success OR |
| 1601 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 1602 | * -EEXIST Freq are same and volt are different OR |
| 1603 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 1604 | * -ENOMEM Memory allocation failure |
Viresh Kumar | a7470db | 2014-11-25 16:04:17 +0530 | [diff] [blame] | 1605 | */ |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1606 | int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) |
| 1607 | { |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1608 | struct opp_table *opp_table; |
| 1609 | int ret; |
| 1610 | |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 1611 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1612 | if (!opp_table) |
| 1613 | return -ENOMEM; |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1614 | |
| 1615 | ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); |
Viresh Kumar | 0ad8c62 | 2018-08-02 14:23:09 +0530 | [diff] [blame] | 1616 | if (ret) |
| 1617 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1618 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 1619 | return ret; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1620 | } |
| 1621 | EXPORT_SYMBOL_GPL(dev_pm_opp_add); |
| 1622 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1623 | /** |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1624 | * _opp_set_availability() - helper to set the availability of an opp |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1625 | * @dev: device for which we do this operation |
| 1626 | * @freq: OPP frequency to modify availability |
| 1627 | * @availability_req: availability status requested for this opp |
| 1628 | * |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1629 | * Set the availability of an OPP, opp_{enable,disable} share a common logic |
| 1630 | * which is isolated here. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1631 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1632 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
Stephen Boyd | e1a2d49 | 2015-09-24 12:28:44 -0700 | [diff] [blame] | 1633 | * copy operation, returns 0 if no modification was done OR modification was |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1634 | * successful. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1635 | */ |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1636 | static int _opp_set_availability(struct device *dev, unsigned long freq, |
| 1637 | bool availability_req) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1638 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1639 | struct opp_table *opp_table; |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1640 | struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1641 | int r = 0; |
| 1642 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1643 | /* Find the opp_table */ |
| 1644 | opp_table = _find_opp_table(dev); |
| 1645 | if (IS_ERR(opp_table)) { |
| 1646 | r = PTR_ERR(opp_table); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1647 | dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1648 | return r; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1649 | } |
| 1650 | |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1651 | mutex_lock(&opp_table->lock); |
| 1652 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1653 | /* Do we have the frequency? */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1654 | list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1655 | if (tmp_opp->rate == freq) { |
| 1656 | opp = tmp_opp; |
| 1657 | break; |
| 1658 | } |
| 1659 | } |
Viresh Kumar | 37a73ec | 2017-01-23 10:11:41 +0530 | [diff] [blame] | 1660 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1661 | if (IS_ERR(opp)) { |
| 1662 | r = PTR_ERR(opp); |
| 1663 | goto unlock; |
| 1664 | } |
| 1665 | |
| 1666 | /* Is update really needed? */ |
| 1667 | if (opp->available == availability_req) |
| 1668 | goto unlock; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1669 | |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1670 | opp->available = availability_req; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1671 | |
Viresh Kumar | e4d8ae0 | 2017-09-21 10:44:36 -0700 | [diff] [blame] | 1672 | dev_pm_opp_get(opp); |
| 1673 | mutex_unlock(&opp_table->lock); |
| 1674 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1675 | /* Notify the change of the OPP availability */ |
| 1676 | if (availability_req) |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1677 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1678 | opp); |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1679 | else |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1680 | blocking_notifier_call_chain(&opp_table->head, |
Viresh Kumar | a7f3987 | 2017-01-23 10:11:50 +0530 | [diff] [blame] | 1681 | OPP_EVENT_DISABLE, opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1682 | |
Viresh Kumar | e4d8ae0 | 2017-09-21 10:44:36 -0700 | [diff] [blame] | 1683 | dev_pm_opp_put(opp); |
| 1684 | goto put_table; |
| 1685 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1686 | unlock: |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1687 | mutex_unlock(&opp_table->lock); |
Viresh Kumar | e4d8ae0 | 2017-09-21 10:44:36 -0700 | [diff] [blame] | 1688 | put_table: |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1689 | dev_pm_opp_put_opp_table(opp_table); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1690 | return r; |
| 1691 | } |
| 1692 | |
| 1693 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1694 | * dev_pm_opp_enable() - Enable a specific OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1695 | * @dev: device for which we do this operation |
| 1696 | * @freq: OPP frequency to enable |
| 1697 | * |
| 1698 | * Enables a provided opp. If the operation is valid, this returns 0, else the |
| 1699 | * 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] | 1700 | * after being temporarily made unavailable with dev_pm_opp_disable. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1701 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1702 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
Stephen Boyd | e1a2d49 | 2015-09-24 12:28:44 -0700 | [diff] [blame] | 1703 | * copy operation, returns 0 if no modification was done OR modification was |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1704 | * successful. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1705 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1706 | int dev_pm_opp_enable(struct device *dev, unsigned long freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1707 | { |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1708 | return _opp_set_availability(dev, freq, true); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1709 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1710 | EXPORT_SYMBOL_GPL(dev_pm_opp_enable); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1711 | |
| 1712 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1713 | * dev_pm_opp_disable() - Disable a specific OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1714 | * @dev: device for which we do this operation |
| 1715 | * @freq: OPP frequency to disable |
| 1716 | * |
| 1717 | * Disables a provided opp. If the operation is valid, this returns |
| 1718 | * 0, else the corresponding error value. It is meant to be a temporary |
| 1719 | * 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] | 1720 | * 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] | 1721 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1722 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
Stephen Boyd | e1a2d49 | 2015-09-24 12:28:44 -0700 | [diff] [blame] | 1723 | * copy operation, returns 0 if no modification was done OR modification was |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1724 | * successful. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1725 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1726 | int dev_pm_opp_disable(struct device *dev, unsigned long freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1727 | { |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1728 | return _opp_set_availability(dev, freq, false); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1729 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1730 | EXPORT_SYMBOL_GPL(dev_pm_opp_disable); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1731 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1732 | /** |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1733 | * dev_pm_opp_register_notifier() - Register OPP notifier for the device |
| 1734 | * @dev: Device for which notifier needs to be registered |
| 1735 | * @nb: Notifier block to be registered |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1736 | * |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1737 | * Return: 0 on success or a negative error value. |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1738 | */ |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1739 | 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] | 1740 | { |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1741 | struct opp_table *opp_table; |
| 1742 | int ret; |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1743 | |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1744 | opp_table = _find_opp_table(dev); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1745 | if (IS_ERR(opp_table)) |
| 1746 | return PTR_ERR(opp_table); |
| 1747 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1748 | ret = blocking_notifier_chain_register(&opp_table->head, nb); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1749 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1750 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1751 | |
| 1752 | return ret; |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1753 | } |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1754 | EXPORT_SYMBOL(dev_pm_opp_register_notifier); |
| 1755 | |
| 1756 | /** |
| 1757 | * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device |
| 1758 | * @dev: Device for which notifier needs to be unregistered |
| 1759 | * @nb: Notifier block to be unregistered |
| 1760 | * |
| 1761 | * Return: 0 on success or a negative error value. |
| 1762 | */ |
| 1763 | int dev_pm_opp_unregister_notifier(struct device *dev, |
| 1764 | struct notifier_block *nb) |
| 1765 | { |
| 1766 | struct opp_table *opp_table; |
| 1767 | int ret; |
| 1768 | |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1769 | opp_table = _find_opp_table(dev); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1770 | if (IS_ERR(opp_table)) |
| 1771 | return PTR_ERR(opp_table); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1772 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 1773 | ret = blocking_notifier_chain_unregister(&opp_table->head, nb); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1774 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1775 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1776 | |
| 1777 | return ret; |
| 1778 | } |
| 1779 | EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 1780 | |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 1781 | void _dev_pm_opp_find_and_remove_table(struct device *dev) |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1782 | { |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1783 | struct opp_table *opp_table; |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1784 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1785 | /* Check for existing table for 'dev' */ |
| 1786 | opp_table = _find_opp_table(dev); |
| 1787 | if (IS_ERR(opp_table)) { |
| 1788 | int error = PTR_ERR(opp_table); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1789 | |
| 1790 | if (error != -ENODEV) |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1791 | WARN(1, "%s: opp_table: %d\n", |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1792 | IS_ERR_OR_NULL(dev) ? |
| 1793 | "Invalid device" : dev_name(dev), |
| 1794 | error); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1795 | return; |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1796 | } |
| 1797 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 1798 | _put_opp_list_kref(opp_table); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1799 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 1800 | /* Drop reference taken by _find_opp_table() */ |
| 1801 | dev_pm_opp_put_opp_table(opp_table); |
| 1802 | |
| 1803 | /* Drop reference taken while the OPP table was added */ |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 1804 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1805 | } |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1806 | |
| 1807 | /** |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 1808 | * dev_pm_opp_remove_table() - Free all OPPs associated with the device |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 1809 | * @dev: device pointer used to lookup OPP table. |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1810 | * |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 1811 | * Free both OPPs created using static entries present in DT and the |
| 1812 | * dynamically added entries. |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1813 | */ |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 1814 | void dev_pm_opp_remove_table(struct device *dev) |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 1815 | { |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 1816 | _dev_pm_opp_find_and_remove_table(dev); |
Viresh Kumar | 8d4d4e9 | 2015-06-12 17:10:38 +0530 | [diff] [blame] | 1817 | } |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 1818 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); |