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