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 |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 54 | * @turbo: true if turbo (boost) OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 55 | * @rate: Frequency in hertz |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 56 | * @u_volt: Target voltage in microvolts corresponding to this OPP |
| 57 | * @u_volt_min: Minimum voltage in microvolts corresponding to this OPP |
| 58 | * @u_volt_max: Maximum voltage in microvolts corresponding to this OPP |
| 59 | * @u_amp: Maximum current drawn by the device in microamperes |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 60 | * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's |
| 61 | * frequency from any other OPP's frequency. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 62 | * @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] | 63 | * @rcu_head: RCU callback head used for deferred freeing |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 64 | * @np: OPP's device node. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 65 | * |
| 66 | * This structure stores the OPP information for a given device. |
| 67 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 68 | struct dev_pm_opp { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 69 | struct list_head node; |
| 70 | |
| 71 | bool available; |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 72 | bool dynamic; |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 73 | bool turbo; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 74 | unsigned long rate; |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 75 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 76 | unsigned long u_volt; |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 77 | unsigned long u_volt_min; |
| 78 | unsigned long u_volt_max; |
| 79 | unsigned long u_amp; |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 80 | unsigned long clock_latency_ns; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 81 | |
| 82 | struct device_opp *dev_opp; |
Viresh Kumar | cd1a068 | 2014-11-25 16:04:16 +0530 | [diff] [blame] | 83 | struct rcu_head rcu_head; |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 84 | |
| 85 | struct device_node *np; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | /** |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 89 | * struct device_list_opp - devices managed by 'struct device_opp' |
| 90 | * @node: list node |
| 91 | * @dev: device to which the struct object belongs |
| 92 | * @rcu_head: RCU callback head used for deferred freeing |
| 93 | * |
| 94 | * This is an internal data structure maintaining the list of devices that are |
| 95 | * managed by 'struct device_opp'. |
| 96 | */ |
| 97 | struct device_list_opp { |
| 98 | struct list_head node; |
| 99 | const struct device *dev; |
| 100 | struct rcu_head rcu_head; |
| 101 | }; |
| 102 | |
| 103 | /** |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 104 | * struct device_opp - Device opp structure |
| 105 | * @node: list node - contains the devices with OPPs that |
| 106 | * have been registered. Nodes once added are not modified in this |
| 107 | * list. |
| 108 | * RCU usage: nodes are not modified in the list of device_opp, |
| 109 | * however addition is possible and is secured by dev_opp_list_lock |
Viresh Kumar | cd1a068 | 2014-11-25 16:04:16 +0530 | [diff] [blame] | 110 | * @srcu_head: notifier head to notify the OPP availability changes. |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 111 | * @rcu_head: RCU callback head used for deferred freeing |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 112 | * @dev_list: list of devices that share these OPPs |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 113 | * @opp_list: list of opps |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 114 | * @np: struct device_node pointer for opp's DT node. |
| 115 | * @shared_opp: OPP is shared between multiple devices. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 116 | * |
| 117 | * This is an internal data structure maintaining the link to opps attached to |
| 118 | * 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] | 119 | * meant for book keeping and private to OPP library. |
| 120 | * |
| 121 | * Because the opp structures can be used from both rcu and srcu readers, we |
| 122 | * need to wait for the grace period of both of them before freeing any |
| 123 | * 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] | 124 | */ |
| 125 | struct device_opp { |
| 126 | struct list_head node; |
| 127 | |
Viresh Kumar | cd1a068 | 2014-11-25 16:04:16 +0530 | [diff] [blame] | 128 | struct srcu_notifier_head srcu_head; |
Viresh Kumar | 129eec5 | 2014-11-27 08:54:06 +0530 | [diff] [blame] | 129 | struct rcu_head rcu_head; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 130 | struct list_head dev_list; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 131 | struct list_head opp_list; |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 132 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 133 | struct device_node *np; |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 134 | unsigned long clock_latency_ns_max; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 135 | bool shared_opp; |
Viresh Kumar | ad656a6 | 2015-06-13 15:10:21 +0530 | [diff] [blame^] | 136 | struct dev_pm_opp *suspend_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /* |
| 140 | * The root of the list of all devices. All device_opp structures branch off |
| 141 | * from here, with each device_opp containing the list of opp it supports in |
| 142 | * various states of availability. |
| 143 | */ |
| 144 | static LIST_HEAD(dev_opp_list); |
| 145 | /* Lock to allow exclusive modification to the device and opp lists */ |
| 146 | static DEFINE_MUTEX(dev_opp_list_lock); |
| 147 | |
Dmitry Torokhov | b02ded2 | 2014-12-16 15:09:36 -0800 | [diff] [blame] | 148 | #define opp_rcu_lockdep_assert() \ |
| 149 | do { \ |
| 150 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
| 151 | lockdep_is_held(&dev_opp_list_lock), \ |
| 152 | "Missing rcu_read_lock() or " \ |
| 153 | "dev_opp_list_lock protection"); \ |
| 154 | } while (0) |
| 155 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 156 | static struct device_list_opp *_find_list_dev(const struct device *dev, |
| 157 | struct device_opp *dev_opp) |
| 158 | { |
| 159 | struct device_list_opp *list_dev; |
| 160 | |
| 161 | list_for_each_entry(list_dev, &dev_opp->dev_list, node) |
| 162 | if (list_dev->dev == dev) |
| 163 | return list_dev; |
| 164 | |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | static struct device_opp *_managed_opp(const struct device_node *np) |
| 169 | { |
| 170 | struct device_opp *dev_opp; |
| 171 | |
| 172 | list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) { |
| 173 | if (dev_opp->np == np) { |
| 174 | /* |
| 175 | * Multiple devices can point to the same OPP table and |
| 176 | * so will have same node-pointer, np. |
| 177 | * |
| 178 | * But the OPPs will be considered as shared only if the |
| 179 | * OPP table contains a "opp-shared" property. |
| 180 | */ |
| 181 | return dev_opp->shared_opp ? dev_opp : NULL; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return NULL; |
| 186 | } |
| 187 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 188 | /** |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 189 | * _find_device_opp() - find device_opp struct using device pointer |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 190 | * @dev: device pointer used to lookup device OPPs |
| 191 | * |
| 192 | * Search list of device OPPs for one containing matching device. Does a RCU |
| 193 | * reader operation to grab the pointer needed. |
| 194 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 195 | * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 196 | * -EINVAL based on type of error. |
| 197 | * |
| 198 | * Locking: This function must be called under rcu_read_lock(). device_opp |
| 199 | * is a RCU protected pointer. This means that device_opp is valid as long |
| 200 | * as we are under RCU lock. |
| 201 | */ |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 202 | static struct device_opp *_find_device_opp(struct device *dev) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 203 | { |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 204 | struct device_opp *dev_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 205 | |
| 206 | if (unlikely(IS_ERR_OR_NULL(dev))) { |
| 207 | pr_err("%s: Invalid parameters\n", __func__); |
| 208 | return ERR_PTR(-EINVAL); |
| 209 | } |
| 210 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 211 | list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) |
| 212 | if (_find_list_dev(dev, dev_opp)) |
| 213 | return dev_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 214 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 215 | return ERR_PTR(-ENODEV); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 219 | * 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] | 220 | * @opp: opp for which voltage has to be returned for |
| 221 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 222 | * Return: voltage in micro volt corresponding to the opp, else |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 223 | * return 0 |
| 224 | * |
| 225 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 226 | * protected pointer. This means that opp which could have been fetched by |
| 227 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are |
| 228 | * under RCU lock. The pointer returned by the opp_find_freq family must be |
| 229 | * used in the same section as the usage of this function with the pointer |
| 230 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the |
| 231 | * pointer. |
| 232 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 233 | unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 234 | { |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 235 | struct dev_pm_opp *tmp_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 236 | unsigned long v = 0; |
| 237 | |
Krzysztof Kozlowski | 04bf1c7 | 2015-01-09 09:27:57 +0100 | [diff] [blame] | 238 | opp_rcu_lockdep_assert(); |
| 239 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 240 | tmp_opp = rcu_dereference(opp); |
| 241 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) |
| 242 | pr_err("%s: Invalid parameters\n", __func__); |
| 243 | else |
| 244 | v = tmp_opp->u_volt; |
| 245 | |
| 246 | return v; |
| 247 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 248 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 249 | |
| 250 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 251 | * 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] | 252 | * @opp: opp for which frequency has to be returned for |
| 253 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 254 | * Return: frequency in hertz corresponding to the opp, else |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 255 | * return 0 |
| 256 | * |
| 257 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 258 | * protected pointer. This means that opp which could have been fetched by |
| 259 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are |
| 260 | * under RCU lock. The pointer returned by the opp_find_freq family must be |
| 261 | * used in the same section as the usage of this function with the pointer |
| 262 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the |
| 263 | * pointer. |
| 264 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 265 | unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 266 | { |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 267 | struct dev_pm_opp *tmp_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 268 | unsigned long f = 0; |
| 269 | |
Krzysztof Kozlowski | 04bf1c7 | 2015-01-09 09:27:57 +0100 | [diff] [blame] | 270 | opp_rcu_lockdep_assert(); |
| 271 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 272 | tmp_opp = rcu_dereference(opp); |
| 273 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) |
| 274 | pr_err("%s: Invalid parameters\n", __func__); |
| 275 | else |
| 276 | f = tmp_opp->rate; |
| 277 | |
| 278 | return f; |
| 279 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 280 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 281 | |
| 282 | /** |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 283 | * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds |
| 284 | * @dev: device for which we do this operation |
| 285 | * |
| 286 | * Return: This function returns the max clock latency in nanoseconds. |
| 287 | * |
| 288 | * Locking: This function takes rcu_read_lock(). |
| 289 | */ |
| 290 | unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) |
| 291 | { |
| 292 | struct device_opp *dev_opp; |
| 293 | unsigned long clock_latency_ns; |
| 294 | |
| 295 | rcu_read_lock(); |
| 296 | |
| 297 | dev_opp = _find_device_opp(dev); |
| 298 | if (IS_ERR(dev_opp)) |
| 299 | clock_latency_ns = 0; |
| 300 | else |
| 301 | clock_latency_ns = dev_opp->clock_latency_ns_max; |
| 302 | |
| 303 | rcu_read_unlock(); |
| 304 | return clock_latency_ns; |
| 305 | } |
| 306 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); |
| 307 | |
| 308 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 309 | * 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] | 310 | * @dev: device for which we do this operation |
| 311 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 312 | * 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] | 313 | * else returns 0 if none or the corresponding error value. |
| 314 | * |
Dmitry Torokhov | b4718c0 | 2014-12-16 15:09:38 -0800 | [diff] [blame] | 315 | * Locking: This function takes rcu_read_lock(). |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 316 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 317 | int dev_pm_opp_get_opp_count(struct device *dev) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 318 | { |
| 319 | struct device_opp *dev_opp; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 320 | struct dev_pm_opp *temp_opp; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 321 | int count = 0; |
| 322 | |
Dmitry Torokhov | b4718c0 | 2014-12-16 15:09:38 -0800 | [diff] [blame] | 323 | rcu_read_lock(); |
Dmitry Torokhov | b02ded2 | 2014-12-16 15:09:36 -0800 | [diff] [blame] | 324 | |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 325 | dev_opp = _find_device_opp(dev); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 326 | if (IS_ERR(dev_opp)) { |
Dmitry Torokhov | b4718c0 | 2014-12-16 15:09:38 -0800 | [diff] [blame] | 327 | count = PTR_ERR(dev_opp); |
| 328 | dev_err(dev, "%s: device OPP not found (%d)\n", |
| 329 | __func__, count); |
| 330 | goto out_unlock; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { |
| 334 | if (temp_opp->available) |
| 335 | count++; |
| 336 | } |
| 337 | |
Dmitry Torokhov | b4718c0 | 2014-12-16 15:09:38 -0800 | [diff] [blame] | 338 | out_unlock: |
| 339 | rcu_read_unlock(); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 340 | return count; |
| 341 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 342 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 343 | |
| 344 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 345 | * dev_pm_opp_find_freq_exact() - search for an exact frequency |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 346 | * @dev: device for which we do this operation |
| 347 | * @freq: frequency to search for |
Nishanth Menon | 7ae4961 | 2011-02-25 23:46:18 +0100 | [diff] [blame] | 348 | * @available: true/false - match for available opp |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 349 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 350 | * Return: Searches for exact match in the opp list and returns pointer to the |
| 351 | * matching opp if found, else returns ERR_PTR in case of error and should |
| 352 | * be handled using IS_ERR. Error return values can be: |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 353 | * EINVAL: for bad pointer |
| 354 | * ERANGE: no match found for search |
| 355 | * ENODEV: if device not found in list of registered devices |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 356 | * |
| 357 | * Note: available is a modifier for the search. if available=true, then the |
| 358 | * match is for exact matching frequency and is available in the stored OPP |
| 359 | * table. if false, the match is for exact frequency which is not available. |
| 360 | * |
| 361 | * This provides a mechanism to enable an opp which is not available currently |
| 362 | * or the opposite as well. |
| 363 | * |
| 364 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 365 | * protected pointer. The reason for the same is that the opp pointer which is |
| 366 | * returned will remain valid for use with opp_get_{voltage, freq} only while |
| 367 | * under the locked area. The pointer returned must be used prior to unlocking |
| 368 | * with rcu_read_unlock() to maintain the integrity of the pointer. |
| 369 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 370 | struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, |
| 371 | unsigned long freq, |
| 372 | bool available) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 373 | { |
| 374 | struct device_opp *dev_opp; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 375 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 376 | |
Dmitry Torokhov | b02ded2 | 2014-12-16 15:09:36 -0800 | [diff] [blame] | 377 | opp_rcu_lockdep_assert(); |
| 378 | |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 379 | dev_opp = _find_device_opp(dev); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 380 | if (IS_ERR(dev_opp)) { |
| 381 | int r = PTR_ERR(dev_opp); |
| 382 | dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r); |
| 383 | return ERR_PTR(r); |
| 384 | } |
| 385 | |
| 386 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { |
| 387 | if (temp_opp->available == available && |
| 388 | temp_opp->rate == freq) { |
| 389 | opp = temp_opp; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return opp; |
| 395 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 396 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 397 | |
| 398 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 399 | * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 400 | * @dev: device for which we do this operation |
| 401 | * @freq: Start frequency |
| 402 | * |
| 403 | * Search for the matching ceil *available* OPP from a starting freq |
| 404 | * for a device. |
| 405 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 406 | * Return: matching *opp and refreshes *freq accordingly, else returns |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 407 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
| 408 | * values can be: |
| 409 | * EINVAL: for bad pointer |
| 410 | * ERANGE: no match found for search |
| 411 | * ENODEV: if device not found in list of registered devices |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 412 | * |
| 413 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 414 | * protected pointer. The reason for the same is that the opp pointer which is |
| 415 | * returned will remain valid for use with opp_get_{voltage, freq} only while |
| 416 | * under the locked area. The pointer returned must be used prior to unlocking |
| 417 | * with rcu_read_unlock() to maintain the integrity of the pointer. |
| 418 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 419 | struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, |
| 420 | unsigned long *freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 421 | { |
| 422 | struct device_opp *dev_opp; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 423 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 424 | |
Dmitry Torokhov | b02ded2 | 2014-12-16 15:09:36 -0800 | [diff] [blame] | 425 | opp_rcu_lockdep_assert(); |
| 426 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 427 | if (!dev || !freq) { |
| 428 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); |
| 429 | return ERR_PTR(-EINVAL); |
| 430 | } |
| 431 | |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 432 | dev_opp = _find_device_opp(dev); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 433 | if (IS_ERR(dev_opp)) |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 434 | return ERR_CAST(dev_opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 435 | |
| 436 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { |
| 437 | if (temp_opp->available && temp_opp->rate >= *freq) { |
| 438 | opp = temp_opp; |
| 439 | *freq = opp->rate; |
| 440 | break; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return opp; |
| 445 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 446 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 447 | |
| 448 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 449 | * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 450 | * @dev: device for which we do this operation |
| 451 | * @freq: Start frequency |
| 452 | * |
| 453 | * Search for the matching floor *available* OPP from a starting freq |
| 454 | * for a device. |
| 455 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 456 | * Return: matching *opp and refreshes *freq accordingly, else returns |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 457 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
| 458 | * values can be: |
| 459 | * EINVAL: for bad pointer |
| 460 | * ERANGE: no match found for search |
| 461 | * ENODEV: if device not found in list of registered devices |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 462 | * |
| 463 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
| 464 | * protected pointer. The reason for the same is that the opp pointer which is |
| 465 | * returned will remain valid for use with opp_get_{voltage, freq} only while |
| 466 | * under the locked area. The pointer returned must be used prior to unlocking |
| 467 | * with rcu_read_unlock() to maintain the integrity of the pointer. |
| 468 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 469 | struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, |
| 470 | unsigned long *freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 471 | { |
| 472 | struct device_opp *dev_opp; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 473 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 474 | |
Dmitry Torokhov | b02ded2 | 2014-12-16 15:09:36 -0800 | [diff] [blame] | 475 | opp_rcu_lockdep_assert(); |
| 476 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 477 | if (!dev || !freq) { |
| 478 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); |
| 479 | return ERR_PTR(-EINVAL); |
| 480 | } |
| 481 | |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 482 | dev_opp = _find_device_opp(dev); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 483 | if (IS_ERR(dev_opp)) |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 484 | return ERR_CAST(dev_opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 485 | |
| 486 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { |
| 487 | if (temp_opp->available) { |
| 488 | /* go to the next node, before choosing prev */ |
| 489 | if (temp_opp->rate > *freq) |
| 490 | break; |
| 491 | else |
| 492 | opp = temp_opp; |
| 493 | } |
| 494 | } |
| 495 | if (!IS_ERR(opp)) |
| 496 | *freq = opp->rate; |
| 497 | |
| 498 | return opp; |
| 499 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 500 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 501 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 502 | /* List-dev Helpers */ |
| 503 | static void _kfree_list_dev_rcu(struct rcu_head *head) |
| 504 | { |
| 505 | struct device_list_opp *list_dev; |
| 506 | |
| 507 | list_dev = container_of(head, struct device_list_opp, rcu_head); |
| 508 | kfree_rcu(list_dev, rcu_head); |
| 509 | } |
| 510 | |
| 511 | static void _remove_list_dev(struct device_list_opp *list_dev, |
| 512 | struct device_opp *dev_opp) |
| 513 | { |
| 514 | list_del(&list_dev->node); |
| 515 | call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head, |
| 516 | _kfree_list_dev_rcu); |
| 517 | } |
| 518 | |
| 519 | static struct device_list_opp *_add_list_dev(const struct device *dev, |
| 520 | struct device_opp *dev_opp) |
| 521 | { |
| 522 | struct device_list_opp *list_dev; |
| 523 | |
| 524 | list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL); |
| 525 | if (!list_dev) |
| 526 | return NULL; |
| 527 | |
| 528 | /* Initialize list-dev */ |
| 529 | list_dev->dev = dev; |
| 530 | list_add_rcu(&list_dev->node, &dev_opp->dev_list); |
| 531 | |
| 532 | return list_dev; |
| 533 | } |
| 534 | |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 535 | /** |
Viresh Kumar | aa5f2f8 | 2015-07-29 16:23:00 +0530 | [diff] [blame] | 536 | * _add_device_opp() - Find device OPP table or allocate a new one |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 537 | * @dev: device for which we do this operation |
| 538 | * |
Viresh Kumar | aa5f2f8 | 2015-07-29 16:23:00 +0530 | [diff] [blame] | 539 | * It tries to find an existing table first, if it couldn't find one, it |
| 540 | * allocates a new OPP table and returns that. |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 541 | * |
| 542 | * Return: valid device_opp pointer if success, else NULL. |
| 543 | */ |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 544 | static struct device_opp *_add_device_opp(struct device *dev) |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 545 | { |
| 546 | struct device_opp *dev_opp; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 547 | struct device_list_opp *list_dev; |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 548 | |
Viresh Kumar | aa5f2f8 | 2015-07-29 16:23:00 +0530 | [diff] [blame] | 549 | /* Check for existing list for 'dev' first */ |
| 550 | dev_opp = _find_device_opp(dev); |
| 551 | if (!IS_ERR(dev_opp)) |
| 552 | return dev_opp; |
| 553 | |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 554 | /* |
| 555 | * Allocate a new device OPP table. In the infrequent case where a new |
| 556 | * device is needed to be added, we pay this penalty. |
| 557 | */ |
| 558 | dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL); |
| 559 | if (!dev_opp) |
| 560 | return NULL; |
| 561 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 562 | INIT_LIST_HEAD(&dev_opp->dev_list); |
| 563 | |
| 564 | list_dev = _add_list_dev(dev, dev_opp); |
| 565 | if (!list_dev) { |
| 566 | kfree(dev_opp); |
| 567 | return NULL; |
| 568 | } |
| 569 | |
Viresh Kumar | 07cce74 | 2014-12-10 09:45:34 +0530 | [diff] [blame] | 570 | srcu_init_notifier_head(&dev_opp->srcu_head); |
| 571 | INIT_LIST_HEAD(&dev_opp->opp_list); |
| 572 | |
| 573 | /* Secure the device list modification */ |
| 574 | list_add_rcu(&dev_opp->node, &dev_opp_list); |
| 575 | return dev_opp; |
| 576 | } |
| 577 | |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 578 | /** |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 579 | * _kfree_device_rcu() - Free device_opp RCU handler |
| 580 | * @head: RCU head |
| 581 | */ |
| 582 | static void _kfree_device_rcu(struct rcu_head *head) |
| 583 | { |
| 584 | struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head); |
| 585 | |
| 586 | kfree_rcu(device_opp, rcu_head); |
| 587 | } |
| 588 | |
| 589 | /** |
Viresh Kumar | 3bac42c | 2015-07-29 16:22:59 +0530 | [diff] [blame] | 590 | * _remove_device_opp() - Removes a device OPP table |
| 591 | * @dev_opp: device OPP table to be removed. |
| 592 | * |
| 593 | * Removes/frees device OPP table it it doesn't contain any OPPs. |
| 594 | */ |
| 595 | static void _remove_device_opp(struct device_opp *dev_opp) |
| 596 | { |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 597 | struct device_list_opp *list_dev; |
| 598 | |
Viresh Kumar | 3bac42c | 2015-07-29 16:22:59 +0530 | [diff] [blame] | 599 | if (!list_empty(&dev_opp->opp_list)) |
| 600 | return; |
| 601 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 602 | list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp, |
| 603 | node); |
| 604 | |
| 605 | _remove_list_dev(list_dev, dev_opp); |
| 606 | |
| 607 | /* dev_list must be empty now */ |
| 608 | WARN_ON(!list_empty(&dev_opp->dev_list)); |
| 609 | |
Viresh Kumar | 3bac42c | 2015-07-29 16:22:59 +0530 | [diff] [blame] | 610 | list_del_rcu(&dev_opp->node); |
| 611 | call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head, |
| 612 | _kfree_device_rcu); |
| 613 | } |
| 614 | |
| 615 | /** |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 616 | * _kfree_opp_rcu() - Free OPP RCU handler |
| 617 | * @head: RCU head |
| 618 | */ |
| 619 | static void _kfree_opp_rcu(struct rcu_head *head) |
| 620 | { |
| 621 | struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head); |
| 622 | |
| 623 | kfree_rcu(opp, rcu_head); |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * _opp_remove() - Remove an OPP from a table definition |
| 628 | * @dev_opp: points back to the device_opp struct this opp belongs to |
| 629 | * @opp: pointer to the OPP to remove |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 630 | * @notify: OPP_EVENT_REMOVE notification should be sent or not |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 631 | * |
| 632 | * This function removes an opp definition from the opp list. |
| 633 | * |
| 634 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 635 | * It is assumed that the caller holds required mutex for an RCU updater |
| 636 | * strategy. |
| 637 | */ |
| 638 | static void _opp_remove(struct device_opp *dev_opp, |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 639 | struct dev_pm_opp *opp, bool notify) |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 640 | { |
| 641 | /* |
| 642 | * Notify the changes in the availability of the operable |
| 643 | * frequency/voltage list. |
| 644 | */ |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 645 | if (notify) |
| 646 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 647 | list_del_rcu(&opp->node); |
| 648 | call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); |
| 649 | |
Viresh Kumar | 3bac42c | 2015-07-29 16:22:59 +0530 | [diff] [blame] | 650 | _remove_device_opp(dev_opp); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /** |
| 654 | * dev_pm_opp_remove() - Remove an OPP from OPP list |
| 655 | * @dev: device for which we do this operation |
| 656 | * @freq: OPP to remove with matching 'freq' |
| 657 | * |
| 658 | * This function removes an opp from the opp list. |
| 659 | * |
| 660 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 661 | * Hence this function internally uses RCU updater strategy with mutex locks |
| 662 | * to keep the integrity of the internal data structures. Callers should ensure |
| 663 | * that this function is *NOT* called under RCU protection or in contexts where |
| 664 | * mutex cannot be locked. |
| 665 | */ |
| 666 | void dev_pm_opp_remove(struct device *dev, unsigned long freq) |
| 667 | { |
| 668 | struct dev_pm_opp *opp; |
| 669 | struct device_opp *dev_opp; |
| 670 | bool found = false; |
| 671 | |
| 672 | /* Hold our list modification lock here */ |
| 673 | mutex_lock(&dev_opp_list_lock); |
| 674 | |
| 675 | dev_opp = _find_device_opp(dev); |
| 676 | if (IS_ERR(dev_opp)) |
| 677 | goto unlock; |
| 678 | |
| 679 | list_for_each_entry(opp, &dev_opp->opp_list, node) { |
| 680 | if (opp->rate == freq) { |
| 681 | found = true; |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | if (!found) { |
| 687 | dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", |
| 688 | __func__, freq); |
| 689 | goto unlock; |
| 690 | } |
| 691 | |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 692 | _opp_remove(dev_opp, opp, true); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 693 | unlock: |
| 694 | mutex_unlock(&dev_opp_list_lock); |
| 695 | } |
| 696 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove); |
| 697 | |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 698 | static struct dev_pm_opp *_allocate_opp(struct device *dev, |
| 699 | struct device_opp **dev_opp) |
| 700 | { |
| 701 | struct dev_pm_opp *opp; |
| 702 | |
| 703 | /* allocate new OPP node */ |
| 704 | opp = kzalloc(sizeof(*opp), GFP_KERNEL); |
| 705 | if (!opp) |
| 706 | return NULL; |
| 707 | |
| 708 | INIT_LIST_HEAD(&opp->node); |
| 709 | |
| 710 | *dev_opp = _add_device_opp(dev); |
| 711 | if (!*dev_opp) { |
| 712 | kfree(opp); |
| 713 | return NULL; |
| 714 | } |
| 715 | |
| 716 | return opp; |
| 717 | } |
| 718 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 719 | static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, |
| 720 | struct device_opp *dev_opp) |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 721 | { |
| 722 | struct dev_pm_opp *opp; |
| 723 | struct list_head *head = &dev_opp->opp_list; |
| 724 | |
| 725 | /* |
| 726 | * Insert new OPP in order of increasing frequency and discard if |
| 727 | * already present. |
| 728 | * |
| 729 | * Need to use &dev_opp->opp_list in the condition part of the 'for' |
| 730 | * loop, don't replace it with head otherwise it will become an infinite |
| 731 | * loop. |
| 732 | */ |
| 733 | list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) { |
| 734 | if (new_opp->rate > opp->rate) { |
| 735 | head = &opp->node; |
| 736 | continue; |
| 737 | } |
| 738 | |
| 739 | if (new_opp->rate < opp->rate) |
| 740 | break; |
| 741 | |
| 742 | /* Duplicate OPPs */ |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 743 | dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 744 | __func__, opp->rate, opp->u_volt, opp->available, |
| 745 | new_opp->rate, new_opp->u_volt, new_opp->available); |
| 746 | |
| 747 | return opp->available && new_opp->u_volt == opp->u_volt ? |
| 748 | 0 : -EEXIST; |
| 749 | } |
| 750 | |
| 751 | new_opp->dev_opp = dev_opp; |
| 752 | list_add_rcu(&new_opp->node, head); |
| 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 757 | /** |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 758 | * _opp_add_dynamic() - Allocate a dynamic OPP. |
| 759 | * @dev: device for which we do this operation |
| 760 | * @freq: Frequency in Hz for this OPP |
| 761 | * @u_volt: Voltage in uVolts for this OPP |
| 762 | * @dynamic: Dynamically added OPPs. |
| 763 | * |
| 764 | * This function adds an opp definition to the opp list and returns status. |
| 765 | * The opp is made available by default and it can be controlled using |
| 766 | * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. |
| 767 | * |
| 768 | * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and |
| 769 | * freed by of_free_opp_table. |
| 770 | * |
| 771 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 772 | * Hence this function internally uses RCU updater strategy with mutex locks |
| 773 | * to keep the integrity of the internal data structures. Callers should ensure |
| 774 | * that this function is *NOT* called under RCU protection or in contexts where |
| 775 | * mutex cannot be locked. |
| 776 | * |
| 777 | * Return: |
| 778 | * 0 On success OR |
| 779 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 780 | * -EEXIST Freq are same and volt are different OR |
| 781 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 782 | * -ENOMEM Memory allocation failure |
| 783 | */ |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 784 | static int _opp_add_dynamic(struct device *dev, unsigned long freq, |
| 785 | long u_volt, bool dynamic) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 786 | { |
Viresh Kumar | aa5f2f8 | 2015-07-29 16:23:00 +0530 | [diff] [blame] | 787 | struct device_opp *dev_opp; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 788 | struct dev_pm_opp *new_opp; |
Viresh Kumar | 6ce4184 | 2014-12-10 09:45:35 +0530 | [diff] [blame] | 789 | int ret; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 790 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 791 | /* Hold our list modification lock here */ |
| 792 | mutex_lock(&dev_opp_list_lock); |
| 793 | |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 794 | new_opp = _allocate_opp(dev, &dev_opp); |
| 795 | if (!new_opp) { |
| 796 | ret = -ENOMEM; |
| 797 | goto unlock; |
| 798 | } |
| 799 | |
Viresh Kumar | a7470db | 2014-11-25 16:04:17 +0530 | [diff] [blame] | 800 | /* populate the opp table */ |
Viresh Kumar | a7470db | 2014-11-25 16:04:17 +0530 | [diff] [blame] | 801 | new_opp->rate = freq; |
| 802 | new_opp->u_volt = u_volt; |
| 803 | new_opp->available = true; |
Viresh Kumar | aa5f2f8 | 2015-07-29 16:23:00 +0530 | [diff] [blame] | 804 | new_opp->dynamic = dynamic; |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 805 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 806 | ret = _opp_add(dev, new_opp, dev_opp); |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 807 | if (ret) |
| 808 | goto free_opp; |
| 809 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 810 | mutex_unlock(&dev_opp_list_lock); |
| 811 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 812 | /* |
| 813 | * Notify the changes in the availability of the operable |
| 814 | * frequency/voltage list. |
| 815 | */ |
Viresh Kumar | cd1a068 | 2014-11-25 16:04:16 +0530 | [diff] [blame] | 816 | 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] | 817 | return 0; |
Viresh Kumar | 6ce4184 | 2014-12-10 09:45:35 +0530 | [diff] [blame] | 818 | |
| 819 | free_opp: |
Viresh Kumar | 23dacf6 | 2015-07-29 16:23:01 +0530 | [diff] [blame] | 820 | _opp_remove(dev_opp, new_opp, false); |
| 821 | unlock: |
Viresh Kumar | 6ce4184 | 2014-12-10 09:45:35 +0530 | [diff] [blame] | 822 | mutex_unlock(&dev_opp_list_lock); |
Viresh Kumar | 6ce4184 | 2014-12-10 09:45:35 +0530 | [diff] [blame] | 823 | return ret; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 824 | } |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 825 | |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 826 | /* TODO: Support multiple regulators */ |
| 827 | static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev) |
| 828 | { |
| 829 | u32 microvolt[3] = {0}; |
| 830 | int count, ret; |
| 831 | |
| 832 | count = of_property_count_u32_elems(opp->np, "opp-microvolt"); |
| 833 | if (!count) |
| 834 | return 0; |
| 835 | |
| 836 | /* There can be one or three elements here */ |
| 837 | if (count != 1 && count != 3) { |
| 838 | dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n", |
| 839 | __func__, count); |
| 840 | return -EINVAL; |
| 841 | } |
| 842 | |
| 843 | ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt, |
| 844 | count); |
| 845 | if (ret) { |
| 846 | dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__, |
| 847 | ret); |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | |
| 851 | opp->u_volt = microvolt[0]; |
| 852 | opp->u_volt_min = microvolt[1]; |
| 853 | opp->u_volt_max = microvolt[2]; |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) |
| 860 | * @dev: device for which we do this operation |
| 861 | * @np: device node |
| 862 | * |
| 863 | * This function adds an opp definition to the opp list and returns status. The |
| 864 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be |
| 865 | * removed by dev_pm_opp_remove. |
| 866 | * |
| 867 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 868 | * Hence this function internally uses RCU updater strategy with mutex locks |
| 869 | * to keep the integrity of the internal data structures. Callers should ensure |
| 870 | * that this function is *NOT* called under RCU protection or in contexts where |
| 871 | * mutex cannot be locked. |
| 872 | * |
| 873 | * Return: |
| 874 | * 0 On success OR |
| 875 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 876 | * -EEXIST Freq are same and volt are different OR |
| 877 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 878 | * -ENOMEM Memory allocation failure |
| 879 | * -EINVAL Failed parsing the OPP node |
| 880 | */ |
| 881 | static int _opp_add_static_v2(struct device *dev, struct device_node *np) |
| 882 | { |
| 883 | struct device_opp *dev_opp; |
| 884 | struct dev_pm_opp *new_opp; |
| 885 | u64 rate; |
| 886 | int ret; |
| 887 | |
| 888 | /* Hold our list modification lock here */ |
| 889 | mutex_lock(&dev_opp_list_lock); |
| 890 | |
| 891 | new_opp = _allocate_opp(dev, &dev_opp); |
| 892 | if (!new_opp) { |
| 893 | ret = -ENOMEM; |
| 894 | goto unlock; |
| 895 | } |
| 896 | |
| 897 | ret = of_property_read_u64(np, "opp-hz", &rate); |
| 898 | if (ret < 0) { |
| 899 | dev_err(dev, "%s: opp-hz not found\n", __func__); |
| 900 | goto free_opp; |
| 901 | } |
| 902 | |
| 903 | /* |
| 904 | * Rate is defined as an unsigned long in clk API, and so casting |
| 905 | * explicitly to its type. Must be fixed once rate is 64 bit |
| 906 | * guaranteed in clk API. |
| 907 | */ |
| 908 | new_opp->rate = (unsigned long)rate; |
| 909 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); |
| 910 | |
| 911 | new_opp->np = np; |
| 912 | new_opp->dynamic = false; |
| 913 | new_opp->available = true; |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 914 | of_property_read_u32(np, "clock-latency-ns", |
| 915 | (u32 *)&new_opp->clock_latency_ns); |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 916 | |
| 917 | ret = opp_get_microvolt(new_opp, dev); |
| 918 | if (ret) |
| 919 | goto free_opp; |
| 920 | |
| 921 | of_property_read_u32(np, "opp-microamp", (u32 *)&new_opp->u_amp); |
| 922 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 923 | ret = _opp_add(dev, new_opp, dev_opp); |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 924 | if (ret) |
| 925 | goto free_opp; |
| 926 | |
Viresh Kumar | ad656a6 | 2015-06-13 15:10:21 +0530 | [diff] [blame^] | 927 | /* OPP to select on device suspend */ |
| 928 | if (of_property_read_bool(np, "opp-suspend")) { |
| 929 | if (dev_opp->suspend_opp) |
| 930 | dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n", |
| 931 | __func__, dev_opp->suspend_opp->rate, |
| 932 | new_opp->rate); |
| 933 | else |
| 934 | dev_opp->suspend_opp = new_opp; |
| 935 | } |
| 936 | |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 937 | if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max) |
| 938 | dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns; |
| 939 | |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 940 | mutex_unlock(&dev_opp_list_lock); |
| 941 | |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 942 | pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 943 | __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt, |
Viresh Kumar | 3ca9bb3 | 2015-07-29 16:23:03 +0530 | [diff] [blame] | 944 | new_opp->u_volt_min, new_opp->u_volt_max, |
| 945 | new_opp->clock_latency_ns); |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 946 | |
| 947 | /* |
| 948 | * Notify the changes in the availability of the operable |
| 949 | * frequency/voltage list. |
| 950 | */ |
| 951 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp); |
| 952 | return 0; |
| 953 | |
| 954 | free_opp: |
| 955 | _opp_remove(dev_opp, new_opp, false); |
| 956 | unlock: |
| 957 | mutex_unlock(&dev_opp_list_lock); |
| 958 | return ret; |
| 959 | } |
| 960 | |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 961 | /** |
| 962 | * dev_pm_opp_add() - Add an OPP table from a table definitions |
| 963 | * @dev: device for which we do this operation |
| 964 | * @freq: Frequency in Hz for this OPP |
| 965 | * @u_volt: Voltage in uVolts for this OPP |
| 966 | * |
| 967 | * This function adds an opp definition to the opp list and returns status. |
| 968 | * The opp is made available by default and it can be controlled using |
| 969 | * dev_pm_opp_enable/disable functions. |
| 970 | * |
| 971 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 972 | * Hence this function internally uses RCU updater strategy with mutex locks |
| 973 | * to keep the integrity of the internal data structures. Callers should ensure |
| 974 | * that this function is *NOT* called under RCU protection or in contexts where |
| 975 | * mutex cannot be locked. |
| 976 | * |
| 977 | * Return: |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 978 | * 0 On success OR |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 979 | * Duplicate OPPs (both freq and volt are same) and opp->available |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 980 | * -EEXIST Freq are same and volt are different OR |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 981 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 982 | * -ENOMEM Memory allocation failure |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 983 | */ |
| 984 | int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) |
| 985 | { |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 986 | return _opp_add_dynamic(dev, freq, u_volt, true); |
Viresh Kumar | 38393409 | 2014-11-25 16:04:18 +0530 | [diff] [blame] | 987 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 988 | EXPORT_SYMBOL_GPL(dev_pm_opp_add); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 989 | |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 990 | /** |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 991 | * _opp_set_availability() - helper to set the availability of an opp |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 992 | * @dev: device for which we do this operation |
| 993 | * @freq: OPP frequency to modify availability |
| 994 | * @availability_req: availability status requested for this opp |
| 995 | * |
| 996 | * Set the availability of an OPP with an RCU operation, opp_{enable,disable} |
| 997 | * share a common logic which is isolated here. |
| 998 | * |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 999 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1000 | * copy operation, returns 0 if no modifcation was done OR modification was |
| 1001 | * successful. |
| 1002 | * |
| 1003 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 1004 | * Hence this function internally uses RCU updater strategy with mutex locks to |
| 1005 | * keep the integrity of the internal data structures. Callers should ensure |
| 1006 | * that this function is *NOT* called under RCU protection or in contexts where |
| 1007 | * mutex locking or synchronize_rcu() blocking calls cannot be used. |
| 1008 | */ |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1009 | static int _opp_set_availability(struct device *dev, unsigned long freq, |
| 1010 | bool availability_req) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1011 | { |
Viresh Kumar | 29df0ee | 2014-12-10 09:45:33 +0530 | [diff] [blame] | 1012 | struct device_opp *dev_opp; |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 1013 | struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1014 | int r = 0; |
| 1015 | |
| 1016 | /* keep the node allocated */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 1017 | new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL); |
Quentin Lambert | 59d84ca | 2015-02-09 10:45:32 +0100 | [diff] [blame] | 1018 | if (!new_opp) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1019 | return -ENOMEM; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1020 | |
| 1021 | mutex_lock(&dev_opp_list_lock); |
| 1022 | |
| 1023 | /* Find the device_opp */ |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1024 | dev_opp = _find_device_opp(dev); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1025 | if (IS_ERR(dev_opp)) { |
| 1026 | r = PTR_ERR(dev_opp); |
| 1027 | dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); |
| 1028 | goto unlock; |
| 1029 | } |
| 1030 | |
| 1031 | /* Do we have the frequency? */ |
| 1032 | list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) { |
| 1033 | if (tmp_opp->rate == freq) { |
| 1034 | opp = tmp_opp; |
| 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | if (IS_ERR(opp)) { |
| 1039 | r = PTR_ERR(opp); |
| 1040 | goto unlock; |
| 1041 | } |
| 1042 | |
| 1043 | /* Is update really needed? */ |
| 1044 | if (opp->available == availability_req) |
| 1045 | goto unlock; |
| 1046 | /* copy the old data over */ |
| 1047 | *new_opp = *opp; |
| 1048 | |
| 1049 | /* plug in new node */ |
| 1050 | new_opp->available = availability_req; |
| 1051 | |
| 1052 | list_replace_rcu(&opp->node, &new_opp->node); |
| 1053 | mutex_unlock(&dev_opp_list_lock); |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1054 | 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] | 1055 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1056 | /* Notify the change of the OPP availability */ |
| 1057 | if (availability_req) |
Viresh Kumar | cd1a068 | 2014-11-25 16:04:16 +0530 | [diff] [blame] | 1058 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE, |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1059 | new_opp); |
| 1060 | else |
Viresh Kumar | cd1a068 | 2014-11-25 16:04:16 +0530 | [diff] [blame] | 1061 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE, |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1062 | new_opp); |
| 1063 | |
Vincent Guittot | dde8437 | 2012-10-23 01:21:49 +0200 | [diff] [blame] | 1064 | return 0; |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1065 | |
| 1066 | unlock: |
| 1067 | mutex_unlock(&dev_opp_list_lock); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1068 | kfree(new_opp); |
| 1069 | return r; |
| 1070 | } |
| 1071 | |
| 1072 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1073 | * dev_pm_opp_enable() - Enable a specific OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1074 | * @dev: device for which we do this operation |
| 1075 | * @freq: OPP frequency to enable |
| 1076 | * |
| 1077 | * Enables a provided opp. If the operation is valid, this returns 0, else the |
| 1078 | * 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] | 1079 | * after being temporarily made unavailable with dev_pm_opp_disable. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1080 | * |
| 1081 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 1082 | * Hence this function indirectly uses RCU and mutex locks to keep the |
| 1083 | * integrity of the internal data structures. Callers should ensure that |
| 1084 | * this function is *NOT* called under RCU protection or in contexts where |
| 1085 | * mutex locking or synchronize_rcu() blocking calls cannot be used. |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1086 | * |
| 1087 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
| 1088 | * copy operation, returns 0 if no modifcation was done OR modification was |
| 1089 | * successful. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1090 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1091 | int dev_pm_opp_enable(struct device *dev, unsigned long freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1092 | { |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1093 | return _opp_set_availability(dev, freq, true); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1094 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1095 | EXPORT_SYMBOL_GPL(dev_pm_opp_enable); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1096 | |
| 1097 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1098 | * dev_pm_opp_disable() - Disable a specific OPP |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1099 | * @dev: device for which we do this operation |
| 1100 | * @freq: OPP frequency to disable |
| 1101 | * |
| 1102 | * Disables a provided opp. If the operation is valid, this returns |
| 1103 | * 0, else the corresponding error value. It is meant to be a temporary |
| 1104 | * 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] | 1105 | * 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] | 1106 | * |
| 1107 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 1108 | * Hence this function indirectly uses RCU and mutex locks to keep the |
| 1109 | * integrity of the internal data structures. Callers should ensure that |
| 1110 | * this function is *NOT* called under RCU protection or in contexts where |
| 1111 | * mutex locking or synchronize_rcu() blocking calls cannot be used. |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1112 | * |
| 1113 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
| 1114 | * copy operation, returns 0 if no modifcation was done OR modification was |
| 1115 | * successful. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1116 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1117 | int dev_pm_opp_disable(struct device *dev, unsigned long freq) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1118 | { |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1119 | return _opp_set_availability(dev, freq, false); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1120 | } |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1121 | EXPORT_SYMBOL_GPL(dev_pm_opp_disable); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 1122 | |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1123 | /** |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1124 | * 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] | 1125 | * @dev: device pointer used to lookup device OPPs. |
Nishanth Menon | 984f16c | 2014-12-24 11:22:57 -0600 | [diff] [blame] | 1126 | * |
| 1127 | * Return: pointer to notifier head if found, otherwise -ENODEV or |
| 1128 | * -EINVAL based on type of error casted as pointer. value must be checked |
| 1129 | * with IS_ERR to determine valid pointer or error result. |
| 1130 | * |
| 1131 | * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU |
| 1132 | * protected pointer. The reason for the same is that the opp pointer which is |
| 1133 | * returned will remain valid for use with opp_get_{voltage, freq} only while |
| 1134 | * under the locked area. The pointer returned must be used prior to unlocking |
| 1135 | * with rcu_read_unlock() to maintain the integrity of the pointer. |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1136 | */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1137 | struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev) |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1138 | { |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1139 | struct device_opp *dev_opp = _find_device_opp(dev); |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1140 | |
| 1141 | if (IS_ERR(dev_opp)) |
Thomas Meyer | 156acb1 | 2011-11-08 22:34:00 +0100 | [diff] [blame] | 1142 | return ERR_CAST(dev_opp); /* matching type */ |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1143 | |
Viresh Kumar | cd1a068 | 2014-11-25 16:04:16 +0530 | [diff] [blame] | 1144 | return &dev_opp->srcu_head; |
MyungJoo Ham | 03ca370 | 2011-09-30 22:35:12 +0200 | [diff] [blame] | 1145 | } |
Nishanth Menon | 4679ec3 | 2014-12-24 11:22:55 -0600 | [diff] [blame] | 1146 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier); |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 1147 | |
| 1148 | #ifdef CONFIG_OF |
| 1149 | /** |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1150 | * of_free_opp_table() - Free OPP table entries created from static DT entries |
| 1151 | * @dev: device pointer used to lookup device OPPs. |
| 1152 | * |
| 1153 | * Free OPPs created using static entries present in DT. |
| 1154 | * |
| 1155 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 1156 | * Hence this function indirectly uses RCU updater strategy with mutex locks |
| 1157 | * to keep the integrity of the internal data structures. Callers should ensure |
| 1158 | * that this function is *NOT* called under RCU protection or in contexts where |
| 1159 | * mutex cannot be locked. |
| 1160 | */ |
| 1161 | void of_free_opp_table(struct device *dev) |
| 1162 | { |
| 1163 | struct device_opp *dev_opp; |
| 1164 | struct dev_pm_opp *opp, *tmp; |
| 1165 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 1166 | /* Hold our list modification lock here */ |
| 1167 | mutex_lock(&dev_opp_list_lock); |
| 1168 | |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1169 | /* Check for existing list for 'dev' */ |
| 1170 | dev_opp = _find_device_opp(dev); |
| 1171 | if (IS_ERR(dev_opp)) { |
| 1172 | int error = PTR_ERR(dev_opp); |
| 1173 | |
| 1174 | if (error != -ENODEV) |
| 1175 | WARN(1, "%s: dev_opp: %d\n", |
| 1176 | IS_ERR_OR_NULL(dev) ? |
| 1177 | "Invalid device" : dev_name(dev), |
| 1178 | error); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 1179 | goto unlock; |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1180 | } |
| 1181 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 1182 | /* Find if dev_opp manages a single device */ |
| 1183 | if (list_is_singular(&dev_opp->dev_list)) { |
| 1184 | /* Free static OPPs */ |
| 1185 | list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) { |
| 1186 | if (!opp->dynamic) |
| 1187 | _opp_remove(dev_opp, opp, true); |
| 1188 | } |
| 1189 | } else { |
| 1190 | _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp); |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1191 | } |
| 1192 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 1193 | unlock: |
Viresh Kumar | 737002b | 2015-07-29 16:22:58 +0530 | [diff] [blame] | 1194 | mutex_unlock(&dev_opp_list_lock); |
| 1195 | } |
| 1196 | EXPORT_SYMBOL_GPL(of_free_opp_table); |
| 1197 | |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 1198 | /* Returns opp descriptor node from its phandle. Caller must do of_node_put() */ |
| 1199 | static struct device_node * |
| 1200 | _of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop) |
| 1201 | { |
| 1202 | struct device_node *opp_np; |
| 1203 | |
| 1204 | opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value)); |
| 1205 | if (!opp_np) { |
| 1206 | dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n", |
| 1207 | __func__, prop->name); |
| 1208 | return ERR_PTR(-EINVAL); |
| 1209 | } |
| 1210 | |
| 1211 | return opp_np; |
| 1212 | } |
| 1213 | |
| 1214 | /* Initializes OPP tables based on new bindings */ |
| 1215 | static int _of_init_opp_table_v2(struct device *dev, |
| 1216 | const struct property *prop) |
| 1217 | { |
| 1218 | struct device_node *opp_np, *np; |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 1219 | struct device_opp *dev_opp; |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 1220 | int ret = 0, count = 0; |
| 1221 | |
| 1222 | if (!prop->value) |
| 1223 | return -ENODATA; |
| 1224 | |
| 1225 | /* Get opp node */ |
| 1226 | opp_np = _of_get_opp_desc_node_from_prop(dev, prop); |
| 1227 | if (IS_ERR(opp_np)) |
| 1228 | return PTR_ERR(opp_np); |
| 1229 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 1230 | dev_opp = _managed_opp(opp_np); |
| 1231 | if (dev_opp) { |
| 1232 | /* OPPs are already managed */ |
| 1233 | if (!_add_list_dev(dev, dev_opp)) |
| 1234 | ret = -ENOMEM; |
| 1235 | goto put_opp_np; |
| 1236 | } |
| 1237 | |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 1238 | /* We have opp-list node now, iterate over it and add OPPs */ |
| 1239 | for_each_available_child_of_node(opp_np, np) { |
| 1240 | count++; |
| 1241 | |
| 1242 | ret = _opp_add_static_v2(dev, np); |
| 1243 | if (ret) { |
| 1244 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, |
| 1245 | ret); |
| 1246 | break; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | /* There should be one of more OPP defined */ |
| 1251 | if (WARN_ON(!count)) |
| 1252 | goto put_opp_np; |
| 1253 | |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 1254 | if (!ret) { |
| 1255 | if (!dev_opp) { |
| 1256 | dev_opp = _find_device_opp(dev); |
| 1257 | if (WARN_ON(!dev_opp)) |
| 1258 | goto put_opp_np; |
| 1259 | } |
| 1260 | |
| 1261 | dev_opp->np = opp_np; |
| 1262 | dev_opp->shared_opp = of_property_read_bool(opp_np, |
| 1263 | "opp-shared"); |
| 1264 | } else { |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 1265 | of_free_opp_table(dev); |
Viresh Kumar | 0644165 | 2015-07-29 16:23:04 +0530 | [diff] [blame] | 1266 | } |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 1267 | |
| 1268 | put_opp_np: |
| 1269 | of_node_put(opp_np); |
| 1270 | |
| 1271 | return ret; |
| 1272 | } |
| 1273 | |
| 1274 | /* Initializes OPP tables based on old-deprecated bindings */ |
| 1275 | static int _of_init_opp_table_v1(struct device *dev) |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 1276 | { |
| 1277 | const struct property *prop; |
| 1278 | const __be32 *val; |
| 1279 | int nr; |
| 1280 | |
| 1281 | prop = of_find_property(dev->of_node, "operating-points", NULL); |
| 1282 | if (!prop) |
| 1283 | return -ENODEV; |
| 1284 | if (!prop->value) |
| 1285 | return -ENODATA; |
| 1286 | |
| 1287 | /* |
| 1288 | * Each OPP is a set of tuples consisting of frequency and |
| 1289 | * voltage like <freq-kHz vol-uV>. |
| 1290 | */ |
| 1291 | nr = prop->length / sizeof(u32); |
| 1292 | if (nr % 2) { |
| 1293 | dev_err(dev, "%s: Invalid OPP list\n", __func__); |
| 1294 | return -EINVAL; |
| 1295 | } |
| 1296 | |
| 1297 | val = prop->value; |
| 1298 | while (nr) { |
| 1299 | unsigned long freq = be32_to_cpup(val++) * 1000; |
| 1300 | unsigned long volt = be32_to_cpup(val++); |
| 1301 | |
Nishanth Menon | 327854c | 2014-12-24 11:22:56 -0600 | [diff] [blame] | 1302 | if (_opp_add_dynamic(dev, freq, volt, false)) |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 1303 | dev_warn(dev, "%s: Failed to add OPP %ld\n", |
| 1304 | __func__, freq); |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 1305 | nr -= 2; |
| 1306 | } |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
Viresh Kumar | 2746590 | 2015-07-29 16:23:02 +0530 | [diff] [blame] | 1310 | |
| 1311 | /** |
| 1312 | * of_init_opp_table() - Initialize opp table from device tree |
| 1313 | * @dev: device pointer used to lookup device OPPs. |
| 1314 | * |
| 1315 | * Register the initial OPP table with the OPP library for given device. |
| 1316 | * |
| 1317 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 1318 | * Hence this function indirectly uses RCU updater strategy with mutex locks |
| 1319 | * to keep the integrity of the internal data structures. Callers should ensure |
| 1320 | * that this function is *NOT* called under RCU protection or in contexts where |
| 1321 | * mutex cannot be locked. |
| 1322 | * |
| 1323 | * Return: |
| 1324 | * 0 On success OR |
| 1325 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 1326 | * -EEXIST Freq are same and volt are different OR |
| 1327 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 1328 | * -ENOMEM Memory allocation failure |
| 1329 | * -ENODEV when 'operating-points' property is not found or is invalid data |
| 1330 | * in device node. |
| 1331 | * -ENODATA when empty 'operating-points' property is found |
| 1332 | * -EINVAL when invalid entries are found in opp-v2 table |
| 1333 | */ |
| 1334 | int of_init_opp_table(struct device *dev) |
| 1335 | { |
| 1336 | const struct property *prop; |
| 1337 | |
| 1338 | /* |
| 1339 | * OPPs have two version of bindings now. The older one is deprecated, |
| 1340 | * try for the new binding first. |
| 1341 | */ |
| 1342 | prop = of_find_property(dev->of_node, "operating-points-v2", NULL); |
| 1343 | if (!prop) { |
| 1344 | /* |
| 1345 | * Try old-deprecated bindings for backward compatibility with |
| 1346 | * older dtbs. |
| 1347 | */ |
| 1348 | return _of_init_opp_table_v1(dev); |
| 1349 | } |
| 1350 | |
| 1351 | return _of_init_opp_table_v2(dev, prop); |
| 1352 | } |
Mark Langsdorf | 74c46c6 | 2013-01-28 18:26:16 +0000 | [diff] [blame] | 1353 | EXPORT_SYMBOL_GPL(of_init_opp_table); |
Shawn Guo | b496dfb | 2012-09-05 01:09:12 +0200 | [diff] [blame] | 1354 | #endif |