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