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