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