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