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