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 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/err.h> |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 17 | #include <linux/slab.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 18 | #include <linux/device.h> |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 19 | #include <linux/list.h> |
| 20 | #include <linux/rculist.h> |
| 21 | #include <linux/rcupdate.h> |
Nishanth Menon | e4db1c7 | 2013-09-19 16:03:52 -0500 | [diff] [blame] | 22 | #include <linux/pm_opp.h> |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 23 | #include <linux/of.h> |
Liam Girdwood | 80126ce | 2012-10-23 01:27:44 +0200 | [diff] [blame] | 24 | #include <linux/export.h> |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Internal data structure organization with the OPP layer library is as |
| 28 | * follows: |
| 29 | * dev_opp_list (root) |
| 30 | * |- device 1 (represents voltage domain 1) |
| 31 | * | |- opp 1 (availability, freq, voltage) |
| 32 | * | |- opp 2 .. |
| 33 | * ... ... |
| 34 | * | `- opp n .. |
| 35 | * |- device 2 (represents the next voltage domain) |
| 36 | * ... |
| 37 | * `- device m (represents mth voltage domain) |
| 38 | * device 1, 2.. are represented by dev_opp structure while each opp |
| 39 | * is represented by the opp structure. |
| 40 | */ |
| 41 | |
| 42 | /** |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 43 | * struct dev_pm_opp - Generic OPP description structure |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 44 | * @node: opp list node. The nodes are maintained throughout the lifetime |
| 45 | * of boot. It is expected only an optimal set of OPPs are |
| 46 | * added to the library by the SoC framework. |
| 47 | * RCU usage: opp list is traversed with RCU locks. node |
| 48 | * modification is possible realtime, hence the modifications |
| 49 | * are protected by the dev_opp_list_lock for integrity. |
| 50 | * IMPORTANT: the opp nodes should be maintained in increasing |
| 51 | * order. |
| 52 | * @available: true/false - marks if this OPP as available or not |
| 53 | * @rate: Frequency in hertz |
| 54 | * @u_volt: Nominal voltage in microvolts corresponding to this OPP |
| 55 | * @dev_opp: points back to the device_opp struct this opp belongs to |
Nishanth Menon | cd787b34 | 2013-03-26 18:01:49 +0000 | [diff] [blame] | 56 | * @head: RCU callback head used for deferred freeing |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 57 | * |
| 58 | * This structure stores the OPP information for a given device. |
| 59 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 60 | struct dev_pm_opp { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 61 | struct list_head node; |
| 62 | |
| 63 | bool available; |
| 64 | unsigned long rate; |
| 65 | unsigned long u_volt; |
| 66 | |
| 67 | struct device_opp *dev_opp; |
Vincent Guittot | dde8437 | 2012-10-23 01:21:49 +0200 | [diff] [blame] | 68 | struct rcu_head head; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * struct device_opp - Device opp structure |
| 73 | * @node: list node - contains the devices with OPPs that |
| 74 | * have been registered. Nodes once added are not modified in this |
| 75 | * list. |
| 76 | * RCU usage: nodes are not modified in the list of device_opp, |
| 77 | * however addition is possible and is secured by dev_opp_list_lock |
| 78 | * @dev: device pointer |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 79 | * @head: notifier head to notify the OPP availability changes. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 80 | * @opp_list: list of opps |
| 81 | * |
| 82 | * This is an internal data structure maintaining the link to opps attached to |
| 83 | * a device. This structure is not meant to be shared to users as it is |
| 84 | * meant for book keeping and private to OPP library |
| 85 | */ |
| 86 | struct device_opp { |
| 87 | struct list_head node; |
| 88 | |
| 89 | struct device *dev; |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 90 | struct srcu_notifier_head head; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 91 | struct list_head opp_list; |
| 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * The root of the list of all devices. All device_opp structures branch off |
| 96 | * from here, with each device_opp containing the list of opp it supports in |
| 97 | * various states of availability. |
| 98 | */ |
| 99 | static LIST_HEAD(dev_opp_list); |
| 100 | /* Lock to allow exclusive modification to the device and opp lists */ |
| 101 | static DEFINE_MUTEX(dev_opp_list_lock); |
| 102 | |
| 103 | /** |
| 104 | * find_device_opp() - find device_opp struct using device pointer |
| 105 | * @dev: device pointer used to lookup device OPPs |
| 106 | * |
| 107 | * Search list of device OPPs for one containing matching device. Does a RCU |
| 108 | * reader operation to grab the pointer needed. |
| 109 | * |
| 110 | * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or |
| 111 | * -EINVAL based on type of error. |
| 112 | * |
| 113 | * Locking: This function must be called under rcu_read_lock(). device_opp |
| 114 | * is a RCU protected pointer. This means that device_opp is valid as long |
| 115 | * as we are under RCU lock. |
| 116 | */ |
| 117 | static struct device_opp *find_device_opp(struct device *dev) |
| 118 | { |
| 119 | struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV); |
| 120 | |
| 121 | if (unlikely(IS_ERR_OR_NULL(dev))) { |
| 122 | pr_err("%s: Invalid parameters\n", __func__); |
| 123 | return ERR_PTR(-EINVAL); |
| 124 | } |
| 125 | |
| 126 | list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) { |
| 127 | if (tmp_dev_opp->dev == dev) { |
| 128 | dev_opp = tmp_dev_opp; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return dev_opp; |
| 134 | } |
| 135 | |
| 136 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 137 | * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 138 | * @opp: opp for which voltage has to be returned for |
| 139 | * |
| 140 | * Return voltage in micro volt corresponding to the opp, else |
| 141 | * return 0 |
| 142 | * |
| 143 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 144 | * protected pointer. This means that opp which could have been fetched by |
| 145 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are |
| 146 | * under RCU lock. The pointer returned by the opp_find_freq family must be |
| 147 | * used in the same section as the usage of this function with the pointer |
| 148 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the |
| 149 | * pointer. |
| 150 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 151 | unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 152 | { |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 153 | struct dev_pm_opp *tmp_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 154 | unsigned long v = 0; |
| 155 | |
| 156 | tmp_opp = rcu_dereference(opp); |
| 157 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) |
| 158 | pr_err("%s: Invalid parameters\n", __func__); |
| 159 | else |
| 160 | v = tmp_opp->u_volt; |
| 161 | |
| 162 | return v; |
| 163 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 164 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 165 | |
| 166 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 167 | * 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] | 168 | * @opp: opp for which frequency has to be returned for |
| 169 | * |
| 170 | * Return frequency in hertz corresponding to the opp, else |
| 171 | * return 0 |
| 172 | * |
| 173 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 174 | * protected pointer. This means that opp which could have been fetched by |
| 175 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are |
| 176 | * under RCU lock. The pointer returned by the opp_find_freq family must be |
| 177 | * used in the same section as the usage of this function with the pointer |
| 178 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the |
| 179 | * pointer. |
| 180 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 181 | unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 182 | { |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 183 | struct dev_pm_opp *tmp_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 184 | unsigned long f = 0; |
| 185 | |
| 186 | tmp_opp = rcu_dereference(opp); |
| 187 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) |
| 188 | pr_err("%s: Invalid parameters\n", __func__); |
| 189 | else |
| 190 | f = tmp_opp->rate; |
| 191 | |
| 192 | return f; |
| 193 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 194 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 195 | |
| 196 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 197 | * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 198 | * @dev: device for which we do this operation |
| 199 | * |
| 200 | * This function returns the number of available opps if there are any, |
| 201 | * else returns 0 if none or the corresponding error value. |
| 202 | * |
| 203 | * Locking: This function must be called under rcu_read_lock(). This function |
| 204 | * internally references two RCU protected structures: device_opp and opp which |
| 205 | * are safe as long as we are under a common RCU locked section. |
| 206 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 207 | int dev_pm_opp_get_opp_count(struct device *dev) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 208 | { |
| 209 | struct device_opp *dev_opp; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 210 | struct dev_pm_opp *temp_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 211 | int count = 0; |
| 212 | |
| 213 | dev_opp = find_device_opp(dev); |
| 214 | if (IS_ERR(dev_opp)) { |
| 215 | int r = PTR_ERR(dev_opp); |
| 216 | dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r); |
| 217 | return r; |
| 218 | } |
| 219 | |
| 220 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { |
| 221 | if (temp_opp->available) |
| 222 | count++; |
| 223 | } |
| 224 | |
| 225 | return count; |
| 226 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 228 | |
| 229 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 230 | * dev_pm_opp_find_freq_exact() - search for an exact frequency |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 231 | * @dev: device for which we do this operation |
| 232 | * @freq: frequency to search for |
Nishanth Menon | 7ae4961 | 2011-02-25 23:46:18 +0100 | [diff] [blame] | 233 | * @available: true/false - match for available opp |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 234 | * |
| 235 | * Searches for exact match in the opp list and returns pointer to the matching |
| 236 | * opp if found, else returns ERR_PTR in case of error and should be handled |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 237 | * using IS_ERR. Error return values can be: |
| 238 | * EINVAL: for bad pointer |
| 239 | * ERANGE: no match found for search |
| 240 | * ENODEV: if device not found in list of registered devices |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 241 | * |
| 242 | * Note: available is a modifier for the search. if available=true, then the |
| 243 | * match is for exact matching frequency and is available in the stored OPP |
| 244 | * table. if false, the match is for exact frequency which is not available. |
| 245 | * |
| 246 | * This provides a mechanism to enable an opp which is not available currently |
| 247 | * or the opposite as well. |
| 248 | * |
| 249 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 250 | * protected pointer. The reason for the same is that the opp pointer which is |
| 251 | * returned will remain valid for use with opp_get_{voltage, freq} only while |
| 252 | * under the locked area. The pointer returned must be used prior to unlocking |
| 253 | * with rcu_read_unlock() to maintain the integrity of the pointer. |
| 254 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 255 | struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, |
| 256 | unsigned long freq, |
| 257 | bool available) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 258 | { |
| 259 | struct device_opp *dev_opp; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 260 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 261 | |
| 262 | dev_opp = find_device_opp(dev); |
| 263 | if (IS_ERR(dev_opp)) { |
| 264 | int r = PTR_ERR(dev_opp); |
| 265 | dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r); |
| 266 | return ERR_PTR(r); |
| 267 | } |
| 268 | |
| 269 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { |
| 270 | if (temp_opp->available == available && |
| 271 | temp_opp->rate == freq) { |
| 272 | opp = temp_opp; |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return opp; |
| 278 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 280 | |
| 281 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 282 | * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 283 | * @dev: device for which we do this operation |
| 284 | * @freq: Start frequency |
| 285 | * |
| 286 | * Search for the matching ceil *available* OPP from a starting freq |
| 287 | * for a device. |
| 288 | * |
| 289 | * Returns matching *opp and refreshes *freq accordingly, else returns |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 290 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
| 291 | * values can be: |
| 292 | * EINVAL: for bad pointer |
| 293 | * ERANGE: no match found for search |
| 294 | * ENODEV: if device not found in list of registered devices |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 295 | * |
| 296 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 297 | * protected pointer. The reason for the same is that the opp pointer which is |
| 298 | * returned will remain valid for use with opp_get_{voltage, freq} only while |
| 299 | * under the locked area. The pointer returned must be used prior to unlocking |
| 300 | * with rcu_read_unlock() to maintain the integrity of the pointer. |
| 301 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 302 | struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, |
| 303 | unsigned long *freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 304 | { |
| 305 | struct device_opp *dev_opp; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 306 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 307 | |
| 308 | if (!dev || !freq) { |
| 309 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); |
| 310 | return ERR_PTR(-EINVAL); |
| 311 | } |
| 312 | |
| 313 | dev_opp = find_device_opp(dev); |
| 314 | if (IS_ERR(dev_opp)) |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 315 | return ERR_CAST(dev_opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 316 | |
| 317 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { |
| 318 | if (temp_opp->available && temp_opp->rate >= *freq) { |
| 319 | opp = temp_opp; |
| 320 | *freq = opp->rate; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return opp; |
| 326 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 327 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 328 | |
| 329 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 330 | * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 331 | * @dev: device for which we do this operation |
| 332 | * @freq: Start frequency |
| 333 | * |
| 334 | * Search for the matching floor *available* OPP from a starting freq |
| 335 | * for a device. |
| 336 | * |
| 337 | * Returns matching *opp and refreshes *freq accordingly, else returns |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 338 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
| 339 | * values can be: |
| 340 | * EINVAL: for bad pointer |
| 341 | * ERANGE: no match found for search |
| 342 | * ENODEV: if device not found in list of registered devices |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 343 | * |
| 344 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 345 | * protected pointer. The reason for the same is that the opp pointer which is |
| 346 | * returned will remain valid for use with opp_get_{voltage, freq} only while |
| 347 | * under the locked area. The pointer returned must be used prior to unlocking |
| 348 | * with rcu_read_unlock() to maintain the integrity of the pointer. |
| 349 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 350 | struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, |
| 351 | unsigned long *freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 352 | { |
| 353 | struct device_opp *dev_opp; |
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 | |
| 356 | if (!dev || !freq) { |
| 357 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); |
| 358 | return ERR_PTR(-EINVAL); |
| 359 | } |
| 360 | |
| 361 | dev_opp = find_device_opp(dev); |
| 362 | if (IS_ERR(dev_opp)) |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 363 | return ERR_CAST(dev_opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 364 | |
| 365 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { |
| 366 | if (temp_opp->available) { |
| 367 | /* go to the next node, before choosing prev */ |
| 368 | if (temp_opp->rate > *freq) |
| 369 | break; |
| 370 | else |
| 371 | opp = temp_opp; |
| 372 | } |
| 373 | } |
| 374 | if (!IS_ERR(opp)) |
| 375 | *freq = opp->rate; |
| 376 | |
| 377 | return opp; |
| 378 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 379 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 380 | |
| 381 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 382 | * dev_pm_opp_add() - Add an OPP table from a table definitions |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 383 | * @dev: device for which we do this operation |
| 384 | * @freq: Frequency in Hz for this OPP |
| 385 | * @u_volt: Voltage in uVolts for this OPP |
| 386 | * |
| 387 | * This function adds an opp definition to the opp list and returns status. |
| 388 | * The opp is made available by default and it can be controlled using |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 389 | * dev_pm_opp_enable/disable functions. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 390 | * |
| 391 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 392 | * Hence this function internally uses RCU updater strategy with mutex locks |
| 393 | * to keep the integrity of the internal data structures. Callers should ensure |
| 394 | * that this function is *NOT* called under RCU protection or in contexts where |
| 395 | * mutex cannot be locked. |
Chander Kashyap | 64ce854 | 2014-05-22 10:36:26 +0530 | [diff] [blame] | 396 | * |
| 397 | * Return: |
| 398 | * 0: On success OR |
| 399 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 400 | * -EEXIST: Freq are same and volt are different OR |
| 401 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 402 | * -ENOMEM: Memory allocation failure |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 403 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 404 | int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 405 | { |
| 406 | struct device_opp *dev_opp = NULL; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 407 | struct dev_pm_opp *opp, *new_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 408 | struct list_head *head; |
| 409 | |
| 410 | /* allocate new OPP node */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 411 | new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 412 | if (!new_opp) { |
| 413 | dev_warn(dev, "%s: Unable to create new OPP node\n", __func__); |
| 414 | return -ENOMEM; |
| 415 | } |
| 416 | |
| 417 | /* Hold our list modification lock here */ |
| 418 | mutex_lock(&dev_opp_list_lock); |
| 419 | |
| 420 | /* Check for existing list for 'dev' */ |
| 421 | dev_opp = find_device_opp(dev); |
| 422 | if (IS_ERR(dev_opp)) { |
| 423 | /* |
| 424 | * Allocate a new device OPP table. In the infrequent case |
| 425 | * where a new device is needed to be added, we pay this |
| 426 | * penalty. |
| 427 | */ |
| 428 | dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL); |
| 429 | if (!dev_opp) { |
| 430 | mutex_unlock(&dev_opp_list_lock); |
| 431 | kfree(new_opp); |
| 432 | dev_warn(dev, |
| 433 | "%s: Unable to create device OPP structure\n", |
| 434 | __func__); |
| 435 | return -ENOMEM; |
| 436 | } |
| 437 | |
| 438 | dev_opp->dev = dev; |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 439 | srcu_init_notifier_head(&dev_opp->head); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 440 | INIT_LIST_HEAD(&dev_opp->opp_list); |
| 441 | |
| 442 | /* Secure the device list modification */ |
| 443 | list_add_rcu(&dev_opp->node, &dev_opp_list); |
| 444 | } |
| 445 | |
| 446 | /* populate the opp table */ |
| 447 | new_opp->dev_opp = dev_opp; |
| 448 | new_opp->rate = freq; |
| 449 | new_opp->u_volt = u_volt; |
| 450 | new_opp->available = true; |
| 451 | |
Chander Kashyap | 64ce854 | 2014-05-22 10:36:26 +0530 | [diff] [blame] | 452 | /* |
| 453 | * Insert new OPP in order of increasing frequency |
| 454 | * and discard if already present |
| 455 | */ |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 456 | head = &dev_opp->opp_list; |
| 457 | list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) { |
Chander Kashyap | 64ce854 | 2014-05-22 10:36:26 +0530 | [diff] [blame] | 458 | if (new_opp->rate <= opp->rate) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 459 | break; |
| 460 | else |
| 461 | head = &opp->node; |
| 462 | } |
| 463 | |
Chander Kashyap | 64ce854 | 2014-05-22 10:36:26 +0530 | [diff] [blame] | 464 | /* Duplicate OPPs ? */ |
| 465 | if (new_opp->rate == opp->rate) { |
| 466 | int ret = opp->available && new_opp->u_volt == opp->u_volt ? |
| 467 | 0 : -EEXIST; |
| 468 | |
| 469 | dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", |
| 470 | __func__, opp->rate, opp->u_volt, opp->available, |
| 471 | new_opp->rate, new_opp->u_volt, new_opp->available); |
| 472 | mutex_unlock(&dev_opp_list_lock); |
| 473 | kfree(new_opp); |
| 474 | return ret; |
| 475 | } |
| 476 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 477 | list_add_rcu(&new_opp->node, head); |
| 478 | mutex_unlock(&dev_opp_list_lock); |
| 479 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 480 | /* |
| 481 | * Notify the changes in the availability of the operable |
| 482 | * frequency/voltage list. |
| 483 | */ |
| 484 | srcu_notifier_call_chain(&dev_opp->head, OPP_EVENT_ADD, new_opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 485 | return 0; |
| 486 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 487 | EXPORT_SYMBOL_GPL(dev_pm_opp_add); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 488 | |
| 489 | /** |
| 490 | * opp_set_availability() - helper to set the availability of an opp |
| 491 | * @dev: device for which we do this operation |
| 492 | * @freq: OPP frequency to modify availability |
| 493 | * @availability_req: availability status requested for this opp |
| 494 | * |
| 495 | * Set the availability of an OPP with an RCU operation, opp_{enable,disable} |
| 496 | * share a common logic which is isolated here. |
| 497 | * |
| 498 | * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the |
| 499 | * copy operation, returns 0 if no modifcation was done OR modification was |
| 500 | * successful. |
| 501 | * |
| 502 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 503 | * Hence this function internally uses RCU updater strategy with mutex locks to |
| 504 | * keep the integrity of the internal data structures. Callers should ensure |
| 505 | * that this function is *NOT* called under RCU protection or in contexts where |
| 506 | * mutex locking or synchronize_rcu() blocking calls cannot be used. |
| 507 | */ |
| 508 | static int opp_set_availability(struct device *dev, unsigned long freq, |
| 509 | bool availability_req) |
| 510 | { |
Jonghwan Choi | fc92805 | 2011-07-26 16:08:16 -0700 | [diff] [blame] | 511 | struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV); |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 512 | struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 513 | int r = 0; |
| 514 | |
| 515 | /* keep the node allocated */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 516 | new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 517 | if (!new_opp) { |
| 518 | dev_warn(dev, "%s: Unable to create OPP\n", __func__); |
| 519 | return -ENOMEM; |
| 520 | } |
| 521 | |
| 522 | mutex_lock(&dev_opp_list_lock); |
| 523 | |
| 524 | /* Find the device_opp */ |
| 525 | list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) { |
| 526 | if (dev == tmp_dev_opp->dev) { |
| 527 | dev_opp = tmp_dev_opp; |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | if (IS_ERR(dev_opp)) { |
| 532 | r = PTR_ERR(dev_opp); |
| 533 | dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); |
| 534 | goto unlock; |
| 535 | } |
| 536 | |
| 537 | /* Do we have the frequency? */ |
| 538 | list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) { |
| 539 | if (tmp_opp->rate == freq) { |
| 540 | opp = tmp_opp; |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | if (IS_ERR(opp)) { |
| 545 | r = PTR_ERR(opp); |
| 546 | goto unlock; |
| 547 | } |
| 548 | |
| 549 | /* Is update really needed? */ |
| 550 | if (opp->available == availability_req) |
| 551 | goto unlock; |
| 552 | /* copy the old data over */ |
| 553 | *new_opp = *opp; |
| 554 | |
| 555 | /* plug in new node */ |
| 556 | new_opp->available = availability_req; |
| 557 | |
| 558 | list_replace_rcu(&opp->node, &new_opp->node); |
| 559 | mutex_unlock(&dev_opp_list_lock); |
Wei Yongjun | ea83f81 | 2012-10-31 01:29:17 +0100 | [diff] [blame] | 560 | kfree_rcu(opp, head); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 561 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 562 | /* Notify the change of the OPP availability */ |
| 563 | if (availability_req) |
| 564 | srcu_notifier_call_chain(&dev_opp->head, OPP_EVENT_ENABLE, |
| 565 | new_opp); |
| 566 | else |
| 567 | srcu_notifier_call_chain(&dev_opp->head, OPP_EVENT_DISABLE, |
| 568 | new_opp); |
| 569 | |
Vincent Guittot | dde8437 | 2012-10-23 01:21:49 +0200 | [diff] [blame] | 570 | return 0; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 571 | |
| 572 | unlock: |
| 573 | mutex_unlock(&dev_opp_list_lock); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 574 | kfree(new_opp); |
| 575 | return r; |
| 576 | } |
| 577 | |
| 578 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 579 | * dev_pm_opp_enable() - Enable a specific OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 580 | * @dev: device for which we do this operation |
| 581 | * @freq: OPP frequency to enable |
| 582 | * |
| 583 | * Enables a provided opp. If the operation is valid, this returns 0, else the |
| 584 | * 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] | 585 | * after being temporarily made unavailable with dev_pm_opp_disable. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 586 | * |
| 587 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 588 | * Hence this function indirectly uses RCU and mutex locks to keep the |
| 589 | * integrity of the internal data structures. Callers should ensure that |
| 590 | * this function is *NOT* called under RCU protection or in contexts where |
| 591 | * mutex locking or synchronize_rcu() blocking calls cannot be used. |
| 592 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 593 | int dev_pm_opp_enable(struct device *dev, unsigned long freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 594 | { |
| 595 | return opp_set_availability(dev, freq, true); |
| 596 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 597 | EXPORT_SYMBOL_GPL(dev_pm_opp_enable); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 598 | |
| 599 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 600 | * dev_pm_opp_disable() - Disable a specific OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 601 | * @dev: device for which we do this operation |
| 602 | * @freq: OPP frequency to disable |
| 603 | * |
| 604 | * Disables a provided opp. If the operation is valid, this returns |
| 605 | * 0, else the corresponding error value. It is meant to be a temporary |
| 606 | * 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] | 607 | * 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] | 608 | * |
| 609 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 610 | * Hence this function indirectly uses RCU and mutex locks to keep the |
| 611 | * integrity of the internal data structures. Callers should ensure that |
| 612 | * this function is *NOT* called under RCU protection or in contexts where |
| 613 | * mutex locking or synchronize_rcu() blocking calls cannot be used. |
| 614 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 615 | int dev_pm_opp_disable(struct device *dev, unsigned long freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 616 | { |
| 617 | return opp_set_availability(dev, freq, false); |
| 618 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 619 | EXPORT_SYMBOL_GPL(dev_pm_opp_disable); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 620 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 621 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 622 | * dev_pm_opp_get_notifier() - find notifier_head of the device with opp |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 623 | * @dev: device pointer used to lookup device OPPs. |
| 624 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 625 | struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev) |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 626 | { |
| 627 | struct device_opp *dev_opp = find_device_opp(dev); |
| 628 | |
| 629 | if (IS_ERR(dev_opp)) |
Thomas Meyer | 156acb1 | 2011-11-08 22:34:00 +0100 | [diff] [blame] | 630 | return ERR_CAST(dev_opp); /* matching type */ |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 631 | |
| 632 | return &dev_opp->head; |
| 633 | } |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 634 | |
| 635 | #ifdef CONFIG_OF |
| 636 | /** |
| 637 | * of_init_opp_table() - Initialize opp table from device tree |
| 638 | * @dev: device pointer used to lookup device OPPs. |
| 639 | * |
| 640 | * Register the initial OPP table with the OPP library for given device. |
| 641 | */ |
| 642 | int of_init_opp_table(struct device *dev) |
| 643 | { |
| 644 | const struct property *prop; |
| 645 | const __be32 *val; |
| 646 | int nr; |
| 647 | |
| 648 | prop = of_find_property(dev->of_node, "operating-points", NULL); |
| 649 | if (!prop) |
| 650 | return -ENODEV; |
| 651 | if (!prop->value) |
| 652 | return -ENODATA; |
| 653 | |
| 654 | /* |
| 655 | * Each OPP is a set of tuples consisting of frequency and |
| 656 | * voltage like <freq-kHz vol-uV>. |
| 657 | */ |
| 658 | nr = prop->length / sizeof(u32); |
| 659 | if (nr % 2) { |
| 660 | dev_err(dev, "%s: Invalid OPP list\n", __func__); |
| 661 | return -EINVAL; |
| 662 | } |
| 663 | |
| 664 | val = prop->value; |
| 665 | while (nr) { |
| 666 | unsigned long freq = be32_to_cpup(val++) * 1000; |
| 667 | unsigned long volt = be32_to_cpup(val++); |
| 668 | |
Chander Kashyap | 086abb5 | 2014-05-16 16:21:17 +0530 | [diff] [blame] | 669 | if (dev_pm_opp_add(dev, freq, volt)) |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 670 | dev_warn(dev, "%s: Failed to add OPP %ld\n", |
| 671 | __func__, freq); |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 672 | nr -= 2; |
| 673 | } |
| 674 | |
| 675 | return 0; |
| 676 | } |
Mark Langsdorf | 74c46c6 | 2013-01-28 18:26:16 +0000 | [diff] [blame] | 677 | EXPORT_SYMBOL_GPL(of_init_opp_table); |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 678 | #endif |