Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Generic OPP OF helpers |
| 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 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/cpu.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/device.h> |
Sudeep Holla | 9867999 | 2017-10-12 11:32:23 +0100 | [diff] [blame] | 19 | #include <linux/of_device.h> |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 20 | #include <linux/pm_domain.h> |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 21 | #include <linux/slab.h> |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 22 | #include <linux/export.h> |
| 23 | |
| 24 | #include "opp.h" |
| 25 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 26 | /* |
| 27 | * Returns opp descriptor node for a device node, caller must |
| 28 | * do of_node_put(). |
| 29 | */ |
| 30 | static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np, |
| 31 | int index) |
| 32 | { |
| 33 | /* "operating-points-v2" can be an array for power domain providers */ |
| 34 | return of_parse_phandle(np, "operating-points-v2", index); |
| 35 | } |
| 36 | |
| 37 | /* Returns opp descriptor node for a device, caller must do of_node_put() */ |
| 38 | struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) |
| 39 | { |
| 40 | return _opp_of_get_opp_desc_node(dev->of_node, 0); |
| 41 | } |
| 42 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node); |
| 43 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 44 | struct opp_table *_managed_opp(struct device *dev, int index) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 45 | { |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 46 | struct opp_table *opp_table, *managed_table = NULL; |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 47 | struct device_node *np; |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 48 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 49 | np = _opp_of_get_opp_desc_node(dev->of_node, index); |
| 50 | if (!np) |
| 51 | return NULL; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 52 | |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 53 | list_for_each_entry(opp_table, &opp_tables, node) { |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 54 | if (opp_table->np == np) { |
| 55 | /* |
| 56 | * Multiple devices can point to the same OPP table and |
| 57 | * so will have same node-pointer, np. |
| 58 | * |
| 59 | * But the OPPs will be considered as shared only if the |
| 60 | * OPP table contains a "opp-shared" property. |
| 61 | */ |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 62 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { |
| 63 | _get_opp_table_kref(opp_table); |
| 64 | managed_table = opp_table; |
| 65 | } |
Viresh Kumar | 79ee2e8 | 2016-06-16 19:03:11 +0530 | [diff] [blame] | 66 | |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 67 | break; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 71 | of_node_put(np); |
Viresh Kumar | b83c189 | 2017-01-23 10:11:45 +0530 | [diff] [blame] | 72 | |
| 73 | return managed_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 74 | } |
| 75 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 76 | void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, |
| 77 | int index) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 78 | { |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 79 | struct device_node *np, *opp_np; |
| 80 | u32 val; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * Only required for backward compatibility with v1 bindings, but isn't |
| 84 | * harmful for other cases. And so we do it unconditionally. |
| 85 | */ |
| 86 | np = of_node_get(dev->of_node); |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 87 | if (!np) |
| 88 | return; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 89 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 90 | if (!of_property_read_u32(np, "clock-latency", &val)) |
| 91 | opp_table->clock_latency_ns_max = val; |
| 92 | of_property_read_u32(np, "voltage-tolerance", |
| 93 | &opp_table->voltage_tolerance_v1); |
| 94 | |
Viresh Kumar | 61d8e7c | 2018-06-13 16:00:11 +0530 | [diff] [blame] | 95 | if (of_find_property(np, "#power-domain-cells", NULL)) |
| 96 | opp_table->is_genpd = true; |
| 97 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 98 | /* Get OPP table node */ |
| 99 | opp_np = _opp_of_get_opp_desc_node(np, index); |
| 100 | of_node_put(np); |
| 101 | |
| 102 | if (!opp_np) |
| 103 | return; |
| 104 | |
| 105 | if (of_property_read_bool(opp_np, "opp-shared")) |
| 106 | opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; |
| 107 | else |
| 108 | opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; |
| 109 | |
| 110 | opp_table->np = opp_np; |
| 111 | |
| 112 | of_node_put(opp_np); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table, |
| 116 | struct device_node *np) |
| 117 | { |
| 118 | unsigned int count = opp_table->supported_hw_count; |
| 119 | u32 version; |
| 120 | int ret; |
| 121 | |
Dave Gerlach | a4ee454 | 2016-09-23 15:07:47 -0500 | [diff] [blame] | 122 | if (!opp_table->supported_hw) { |
| 123 | /* |
| 124 | * In the case that no supported_hw has been set by the |
| 125 | * platform but there is an opp-supported-hw value set for |
| 126 | * an OPP then the OPP should not be enabled as there is |
| 127 | * no way to see if the hardware supports it. |
| 128 | */ |
| 129 | if (of_find_property(np, "opp-supported-hw", NULL)) |
| 130 | return false; |
| 131 | else |
| 132 | return true; |
| 133 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 134 | |
| 135 | while (count--) { |
| 136 | ret = of_property_read_u32_index(np, "opp-supported-hw", count, |
| 137 | &version); |
| 138 | if (ret) { |
| 139 | dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n", |
| 140 | __func__, count, ret); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | /* Both of these are bitwise masks of the versions */ |
| 145 | if (!(version & opp_table->supported_hw[count])) |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 152 | static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, |
| 153 | struct opp_table *opp_table) |
| 154 | { |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 155 | u32 *microvolt, *microamp = NULL; |
| 156 | int supplies, vcount, icount, ret, i, j; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 157 | struct property *prop = NULL; |
| 158 | char name[NAME_MAX]; |
| 159 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 160 | supplies = opp_table->regulator_count ? opp_table->regulator_count : 1; |
| 161 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 162 | /* Search for "opp-microvolt-<name>" */ |
| 163 | if (opp_table->prop_name) { |
| 164 | snprintf(name, sizeof(name), "opp-microvolt-%s", |
| 165 | opp_table->prop_name); |
| 166 | prop = of_find_property(opp->np, name, NULL); |
| 167 | } |
| 168 | |
| 169 | if (!prop) { |
| 170 | /* Search for "opp-microvolt" */ |
| 171 | sprintf(name, "opp-microvolt"); |
| 172 | prop = of_find_property(opp->np, name, NULL); |
| 173 | |
| 174 | /* Missing property isn't a problem, but an invalid entry is */ |
Viresh Kumar | 688a48b | 2017-05-23 09:32:12 +0530 | [diff] [blame] | 175 | if (!prop) { |
| 176 | if (!opp_table->regulator_count) |
| 177 | return 0; |
| 178 | |
| 179 | dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n", |
| 180 | __func__); |
| 181 | return -EINVAL; |
| 182 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 183 | } |
| 184 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 185 | vcount = of_property_count_u32_elems(opp->np, name); |
| 186 | if (vcount < 0) { |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 187 | dev_err(dev, "%s: Invalid %s property (%d)\n", |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 188 | __func__, name, vcount); |
| 189 | return vcount; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 190 | } |
| 191 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 192 | /* There can be one or three elements per supply */ |
| 193 | if (vcount != supplies && vcount != supplies * 3) { |
| 194 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", |
| 195 | __func__, name, vcount, supplies); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 196 | return -EINVAL; |
| 197 | } |
| 198 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 199 | microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL); |
| 200 | if (!microvolt) |
| 201 | return -ENOMEM; |
| 202 | |
| 203 | ret = of_property_read_u32_array(opp->np, name, microvolt, vcount); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 204 | if (ret) { |
| 205 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret); |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 206 | ret = -EINVAL; |
| 207 | goto free_microvolt; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* Search for "opp-microamp-<name>" */ |
| 211 | prop = NULL; |
| 212 | if (opp_table->prop_name) { |
| 213 | snprintf(name, sizeof(name), "opp-microamp-%s", |
| 214 | opp_table->prop_name); |
| 215 | prop = of_find_property(opp->np, name, NULL); |
| 216 | } |
| 217 | |
| 218 | if (!prop) { |
| 219 | /* Search for "opp-microamp" */ |
| 220 | sprintf(name, "opp-microamp"); |
| 221 | prop = of_find_property(opp->np, name, NULL); |
| 222 | } |
| 223 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 224 | if (prop) { |
| 225 | icount = of_property_count_u32_elems(opp->np, name); |
| 226 | if (icount < 0) { |
| 227 | dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, |
| 228 | name, icount); |
| 229 | ret = icount; |
| 230 | goto free_microvolt; |
| 231 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 232 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 233 | if (icount != supplies) { |
| 234 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", |
| 235 | __func__, name, icount, supplies); |
| 236 | ret = -EINVAL; |
| 237 | goto free_microvolt; |
| 238 | } |
| 239 | |
| 240 | microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL); |
| 241 | if (!microamp) { |
| 242 | ret = -EINVAL; |
| 243 | goto free_microvolt; |
| 244 | } |
| 245 | |
| 246 | ret = of_property_read_u32_array(opp->np, name, microamp, |
| 247 | icount); |
| 248 | if (ret) { |
| 249 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, |
| 250 | name, ret); |
| 251 | ret = -EINVAL; |
| 252 | goto free_microamp; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | for (i = 0, j = 0; i < supplies; i++) { |
| 257 | opp->supplies[i].u_volt = microvolt[j++]; |
| 258 | |
| 259 | if (vcount == supplies) { |
| 260 | opp->supplies[i].u_volt_min = opp->supplies[i].u_volt; |
| 261 | opp->supplies[i].u_volt_max = opp->supplies[i].u_volt; |
| 262 | } else { |
| 263 | opp->supplies[i].u_volt_min = microvolt[j++]; |
| 264 | opp->supplies[i].u_volt_max = microvolt[j++]; |
| 265 | } |
| 266 | |
| 267 | if (microamp) |
| 268 | opp->supplies[i].u_amp = microamp[i]; |
| 269 | } |
| 270 | |
| 271 | free_microamp: |
| 272 | kfree(microamp); |
| 273 | free_microvolt: |
| 274 | kfree(microvolt); |
| 275 | |
| 276 | return ret; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
| 280 | * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT |
| 281 | * entries |
| 282 | * @dev: device pointer used to lookup OPP table. |
| 283 | * |
| 284 | * Free OPPs created using static entries present in DT. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 285 | */ |
| 286 | void dev_pm_opp_of_remove_table(struct device *dev) |
| 287 | { |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 288 | _dev_pm_opp_find_and_remove_table(dev); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 289 | } |
| 290 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table); |
| 291 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 292 | /** |
| 293 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 294 | * @opp_table: OPP table |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 295 | * @dev: device for which we do this operation |
| 296 | * @np: device node |
| 297 | * |
| 298 | * This function adds an opp definition to the opp table and returns status. The |
| 299 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be |
| 300 | * removed by dev_pm_opp_remove. |
| 301 | * |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 302 | * Return: |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 303 | * Valid OPP pointer: |
| 304 | * On success |
| 305 | * NULL: |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 306 | * Duplicate OPPs (both freq and volt are same) and opp->available |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 307 | * OR if the OPP is not supported by hardware. |
| 308 | * ERR_PTR(-EEXIST): |
| 309 | * Freq are same and volt are different OR |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 310 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 311 | * ERR_PTR(-ENOMEM): |
| 312 | * Memory allocation failure |
| 313 | * ERR_PTR(-EINVAL): |
| 314 | * Failed parsing the OPP node |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 315 | */ |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 316 | static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table, |
| 317 | struct device *dev, struct device_node *np) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 318 | { |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 319 | struct dev_pm_opp *new_opp; |
Dan Carpenter | 6a89e01 | 2018-05-16 12:52:41 +0300 | [diff] [blame] | 320 | u64 rate = 0; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 321 | u32 val; |
| 322 | int ret; |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 323 | bool rate_not_available = false; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 324 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 325 | new_opp = _opp_allocate(opp_table); |
| 326 | if (!new_opp) |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 327 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 328 | |
| 329 | ret = of_property_read_u64(np, "opp-hz", &rate); |
| 330 | if (ret < 0) { |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 331 | /* "opp-hz" is optional for devices like power domains. */ |
Viresh Kumar | 61d8e7c | 2018-06-13 16:00:11 +0530 | [diff] [blame] | 332 | if (!opp_table->is_genpd) { |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 333 | dev_err(dev, "%s: opp-hz not found\n", __func__); |
| 334 | goto free_opp; |
| 335 | } |
| 336 | |
| 337 | rate_not_available = true; |
| 338 | } else { |
| 339 | /* |
| 340 | * Rate is defined as an unsigned long in clk API, and so |
| 341 | * casting explicitly to its type. Must be fixed once rate is 64 |
| 342 | * bit guaranteed in clk API. |
| 343 | */ |
| 344 | new_opp->rate = (unsigned long)rate; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* Check if the OPP supports hardware's hierarchy of versions or not */ |
| 348 | if (!_opp_is_supported(dev, opp_table, np)) { |
| 349 | dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate); |
| 350 | goto free_opp; |
| 351 | } |
| 352 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 353 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); |
| 354 | |
| 355 | new_opp->np = np; |
| 356 | new_opp->dynamic = false; |
| 357 | new_opp->available = true; |
| 358 | |
| 359 | if (!of_property_read_u32(np, "clock-latency-ns", &val)) |
| 360 | new_opp->clock_latency_ns = val; |
| 361 | |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 362 | new_opp->pstate = of_genpd_opp_to_performance_state(dev, np); |
| 363 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 364 | ret = opp_parse_supplies(new_opp, dev, opp_table); |
| 365 | if (ret) |
| 366 | goto free_opp; |
| 367 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 368 | ret = _opp_add(dev, new_opp, opp_table, rate_not_available); |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 369 | if (ret) { |
| 370 | /* Don't return error for duplicate OPPs */ |
| 371 | if (ret == -EBUSY) |
| 372 | ret = 0; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 373 | goto free_opp; |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 374 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 375 | |
| 376 | /* OPP to select on device suspend */ |
| 377 | if (of_property_read_bool(np, "opp-suspend")) { |
| 378 | if (opp_table->suspend_opp) { |
| 379 | dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n", |
| 380 | __func__, opp_table->suspend_opp->rate, |
| 381 | new_opp->rate); |
| 382 | } else { |
| 383 | new_opp->suspend = true; |
| 384 | opp_table->suspend_opp = new_opp; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max) |
| 389 | opp_table->clock_latency_ns_max = new_opp->clock_latency_ns; |
| 390 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 391 | pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", |
Viresh Kumar | 0f0fe7e | 2016-12-01 16:28:17 +0530 | [diff] [blame] | 392 | __func__, new_opp->turbo, new_opp->rate, |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 393 | new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min, |
| 394 | new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 395 | |
| 396 | /* |
| 397 | * Notify the changes in the availability of the operable |
| 398 | * frequency/voltage list. |
| 399 | */ |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 400 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 401 | return new_opp; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 402 | |
| 403 | free_opp: |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 404 | _opp_free(new_opp); |
| 405 | |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 406 | return ERR_PTR(ret); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* Initializes OPP tables based on new bindings */ |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 410 | static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 411 | { |
| 412 | struct device_node *np; |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 413 | int ret, count = 0, pstate_count = 0; |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 414 | struct dev_pm_opp *opp; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 415 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 416 | /* OPP table is already initialized for the device */ |
| 417 | if (opp_table->parsed_static_opps) { |
| 418 | kref_get(&opp_table->list_kref); |
| 419 | return 0; |
| 420 | } |
| 421 | |
Viresh Kumar | d0e8ae6 | 2018-09-11 11:14:11 +0530 | [diff] [blame] | 422 | kref_init(&opp_table->list_kref); |
| 423 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 424 | /* We have opp-table node now, iterate over it and add OPPs */ |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 425 | for_each_available_child_of_node(opp_table->np, np) { |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 426 | opp = _opp_add_static_v2(opp_table, dev, np); |
| 427 | if (IS_ERR(opp)) { |
| 428 | ret = PTR_ERR(opp); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 429 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, |
| 430 | ret); |
Tobias Jordan | 7978db3 | 2017-10-04 11:35:03 +0530 | [diff] [blame] | 431 | of_node_put(np); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 432 | goto put_list_kref; |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 433 | } else if (opp) { |
| 434 | count++; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | /* There should be one of more OPP defined */ |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 439 | if (WARN_ON(!count)) { |
| 440 | ret = -ENOENT; |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 441 | goto put_list_kref; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 442 | } |
| 443 | |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 444 | list_for_each_entry(opp, &opp_table->opp_list, node) |
| 445 | pstate_count += !!opp->pstate; |
| 446 | |
| 447 | /* Either all or none of the nodes shall have performance state set */ |
| 448 | if (pstate_count && pstate_count != count) { |
| 449 | dev_err(dev, "Not all nodes have performance state set (%d: %d)\n", |
| 450 | count, pstate_count); |
| 451 | ret = -ENOENT; |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 452 | goto put_list_kref; |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | if (pstate_count) |
| 456 | opp_table->genpd_performance_state = true; |
| 457 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 458 | opp_table->parsed_static_opps = true; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 459 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 460 | return 0; |
| 461 | |
| 462 | put_list_kref: |
| 463 | _put_opp_list_kref(opp_table); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 464 | |
| 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | /* Initializes OPP tables based on old-deprecated bindings */ |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 469 | static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 470 | { |
| 471 | const struct property *prop; |
| 472 | const __be32 *val; |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 473 | int nr, ret = 0; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 474 | |
| 475 | prop = of_find_property(dev->of_node, "operating-points", NULL); |
| 476 | if (!prop) |
| 477 | return -ENODEV; |
| 478 | if (!prop->value) |
| 479 | return -ENODATA; |
| 480 | |
| 481 | /* |
| 482 | * Each OPP is a set of tuples consisting of frequency and |
| 483 | * voltage like <freq-kHz vol-uV>. |
| 484 | */ |
| 485 | nr = prop->length / sizeof(u32); |
| 486 | if (nr % 2) { |
| 487 | dev_err(dev, "%s: Invalid OPP table\n", __func__); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
Viresh Kumar | d0e8ae6 | 2018-09-11 11:14:11 +0530 | [diff] [blame] | 491 | kref_init(&opp_table->list_kref); |
| 492 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 493 | val = prop->value; |
| 494 | while (nr) { |
| 495 | unsigned long freq = be32_to_cpup(val++) * 1000; |
| 496 | unsigned long volt = be32_to_cpup(val++); |
| 497 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 498 | ret = _opp_add_v1(opp_table, dev, freq, volt, false); |
Viresh Kumar | 04a86a8 | 2017-01-02 14:40:58 +0530 | [diff] [blame] | 499 | if (ret) { |
| 500 | dev_err(dev, "%s: Failed to add OPP %ld (%d)\n", |
| 501 | __func__, freq, ret); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 502 | _put_opp_list_kref(opp_table); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 503 | return ret; |
Viresh Kumar | 04a86a8 | 2017-01-02 14:40:58 +0530 | [diff] [blame] | 504 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 505 | nr -= 2; |
| 506 | } |
| 507 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 508 | return ret; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
| 512 | * dev_pm_opp_of_add_table() - Initialize opp table from device tree |
| 513 | * @dev: device pointer used to lookup OPP table. |
| 514 | * |
| 515 | * Register the initial OPP table with the OPP library for given device. |
| 516 | * |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 517 | * Return: |
| 518 | * 0 On success OR |
| 519 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 520 | * -EEXIST Freq are same and volt are different OR |
| 521 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 522 | * -ENOMEM Memory allocation failure |
| 523 | * -ENODEV when 'operating-points' property is not found or is invalid data |
| 524 | * in device node. |
| 525 | * -ENODATA when empty 'operating-points' property is found |
| 526 | * -EINVAL when invalid entries are found in opp-v2 table |
| 527 | */ |
| 528 | int dev_pm_opp_of_add_table(struct device *dev) |
| 529 | { |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 530 | struct opp_table *opp_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 531 | int ret; |
| 532 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 533 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0); |
| 534 | if (!opp_table) |
| 535 | return -ENOMEM; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 536 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 537 | /* |
| 538 | * OPPs have two version of bindings now. Also try the old (v1) |
| 539 | * bindings for backward compatibility with older dtbs. |
| 540 | */ |
| 541 | if (opp_table->np) |
| 542 | ret = _of_add_opp_table_v2(dev, opp_table); |
| 543 | else |
| 544 | ret = _of_add_opp_table_v1(dev, opp_table); |
| 545 | |
| 546 | if (ret) |
| 547 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 548 | |
| 549 | return ret; |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table); |
| 552 | |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 553 | /** |
| 554 | * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree |
| 555 | * @dev: device pointer used to lookup OPP table. |
| 556 | * @index: Index number. |
| 557 | * |
| 558 | * Register the initial OPP table with the OPP library for given device only |
| 559 | * using the "operating-points-v2" property. |
| 560 | * |
| 561 | * Return: |
| 562 | * 0 On success OR |
| 563 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 564 | * -EEXIST Freq are same and volt are different OR |
| 565 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 566 | * -ENOMEM Memory allocation failure |
| 567 | * -ENODEV when 'operating-points' property is not found or is invalid data |
| 568 | * in device node. |
| 569 | * -ENODATA when empty 'operating-points' property is found |
| 570 | * -EINVAL when invalid entries are found in opp-v2 table |
| 571 | */ |
| 572 | int dev_pm_opp_of_add_table_indexed(struct device *dev, int index) |
| 573 | { |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 574 | struct opp_table *opp_table; |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 575 | int ret, count; |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 576 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 577 | if (index) { |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 578 | /* |
| 579 | * If only one phandle is present, then the same OPP table |
| 580 | * applies for all index requests. |
| 581 | */ |
| 582 | count = of_count_phandle_with_args(dev->of_node, |
| 583 | "operating-points-v2", NULL); |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 584 | if (count != 1) |
| 585 | return -ENODEV; |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 586 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 587 | index = 0; |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 588 | } |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 589 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 590 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, index); |
| 591 | if (!opp_table) |
| 592 | return -ENOMEM; |
| 593 | |
| 594 | ret = _of_add_opp_table_v2(dev, opp_table); |
| 595 | if (ret) |
| 596 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 597 | |
| 598 | return ret; |
| 599 | } |
| 600 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed); |
| 601 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 602 | /* CPU device specific helpers */ |
| 603 | |
| 604 | /** |
| 605 | * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask |
| 606 | * @cpumask: cpumask for which OPP table needs to be removed |
| 607 | * |
| 608 | * This removes the OPP tables for CPUs present in the @cpumask. |
| 609 | * This should be used only to remove static entries created from DT. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 610 | */ |
| 611 | void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask) |
| 612 | { |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 613 | _dev_pm_opp_cpumask_remove_table(cpumask, -1); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 614 | } |
| 615 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table); |
| 616 | |
| 617 | /** |
| 618 | * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask |
| 619 | * @cpumask: cpumask for which OPP table needs to be added. |
| 620 | * |
| 621 | * This adds the OPP tables for CPUs present in the @cpumask. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 622 | */ |
| 623 | int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) |
| 624 | { |
| 625 | struct device *cpu_dev; |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 626 | int cpu, ret; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 627 | |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 628 | if (WARN_ON(cpumask_empty(cpumask))) |
| 629 | return -ENODEV; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 630 | |
| 631 | for_each_cpu(cpu, cpumask) { |
| 632 | cpu_dev = get_cpu_device(cpu); |
| 633 | if (!cpu_dev) { |
| 634 | pr_err("%s: failed to get cpu%d device\n", __func__, |
| 635 | cpu); |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 636 | ret = -ENODEV; |
| 637 | goto remove_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | ret = dev_pm_opp_of_add_table(cpu_dev); |
| 641 | if (ret) { |
Viresh Kumar | 5b60697 | 2017-07-12 09:21:49 +0530 | [diff] [blame] | 642 | /* |
| 643 | * OPP may get registered dynamically, don't print error |
| 644 | * message here. |
| 645 | */ |
| 646 | pr_debug("%s: couldn't find opp table for cpu:%d, %d\n", |
| 647 | __func__, cpu, ret); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 648 | |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 649 | goto remove_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 653 | return 0; |
| 654 | |
| 655 | remove_table: |
| 656 | /* Free all other OPPs */ |
| 657 | _dev_pm_opp_cpumask_remove_table(cpumask, cpu); |
| 658 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 659 | return ret; |
| 660 | } |
| 661 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table); |
| 662 | |
| 663 | /* |
| 664 | * Works only for OPP v2 bindings. |
| 665 | * |
| 666 | * Returns -ENOENT if operating-points-v2 bindings aren't supported. |
| 667 | */ |
| 668 | /** |
| 669 | * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with |
| 670 | * @cpu_dev using operating-points-v2 |
| 671 | * bindings. |
| 672 | * |
| 673 | * @cpu_dev: CPU device for which we do this operation |
| 674 | * @cpumask: cpumask to update with information of sharing CPUs |
| 675 | * |
| 676 | * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. |
| 677 | * |
| 678 | * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 679 | */ |
| 680 | int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, |
| 681 | struct cpumask *cpumask) |
| 682 | { |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 683 | struct device_node *np, *tmp_np, *cpu_np; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 684 | int cpu, ret = 0; |
| 685 | |
| 686 | /* Get OPP descriptor node */ |
Dave Gerlach | 0764c60 | 2017-02-03 11:29:26 -0600 | [diff] [blame] | 687 | np = dev_pm_opp_of_get_opp_desc_node(cpu_dev); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 688 | if (!np) { |
Masahiro Yamada | 349aa92 | 2016-10-20 16:12:49 +0900 | [diff] [blame] | 689 | dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 690 | return -ENOENT; |
| 691 | } |
| 692 | |
| 693 | cpumask_set_cpu(cpu_dev->id, cpumask); |
| 694 | |
| 695 | /* OPPs are shared ? */ |
| 696 | if (!of_property_read_bool(np, "opp-shared")) |
| 697 | goto put_cpu_node; |
| 698 | |
| 699 | for_each_possible_cpu(cpu) { |
| 700 | if (cpu == cpu_dev->id) |
| 701 | continue; |
| 702 | |
Sudeep Holla | 9867999 | 2017-10-12 11:32:23 +0100 | [diff] [blame] | 703 | cpu_np = of_cpu_device_node_get(cpu); |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 704 | if (!cpu_np) { |
| 705 | dev_err(cpu_dev, "%s: failed to get cpu%d node\n", |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 706 | __func__, cpu); |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 707 | ret = -ENOENT; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 708 | goto put_cpu_node; |
| 709 | } |
| 710 | |
| 711 | /* Get OPP descriptor node */ |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 712 | tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0); |
Sudeep Holla | 9867999 | 2017-10-12 11:32:23 +0100 | [diff] [blame] | 713 | of_node_put(cpu_np); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 714 | if (!tmp_np) { |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 715 | pr_err("%pOF: Couldn't find opp node\n", cpu_np); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 716 | ret = -ENOENT; |
| 717 | goto put_cpu_node; |
| 718 | } |
| 719 | |
| 720 | /* CPUs are sharing opp node */ |
| 721 | if (np == tmp_np) |
| 722 | cpumask_set_cpu(cpu, cpumask); |
| 723 | |
| 724 | of_node_put(tmp_np); |
| 725 | } |
| 726 | |
| 727 | put_cpu_node: |
| 728 | of_node_put(np); |
| 729 | return ret; |
| 730 | } |
| 731 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus); |
Viresh Kumar | a88bd2a | 2017-11-29 15:18:36 +0530 | [diff] [blame] | 732 | |
| 733 | /** |
| 734 | * of_dev_pm_opp_find_required_opp() - Search for required OPP. |
| 735 | * @dev: The device whose OPP node is referenced by the 'np' DT node. |
| 736 | * @np: Node that contains the "required-opps" property. |
| 737 | * |
| 738 | * Returns the OPP of the device 'dev', whose phandle is present in the "np" |
| 739 | * node. Although the "required-opps" property supports having multiple |
| 740 | * phandles, this helper routine only parses the very first phandle in the list. |
| 741 | * |
| 742 | * Return: Matching opp, else returns ERR_PTR in case of error and should be |
| 743 | * handled using IS_ERR. |
| 744 | * |
| 745 | * The callers are required to call dev_pm_opp_put() for the returned OPP after |
| 746 | * use. |
| 747 | */ |
| 748 | struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev, |
| 749 | struct device_node *np) |
| 750 | { |
| 751 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV); |
| 752 | struct device_node *required_np; |
| 753 | struct opp_table *opp_table; |
| 754 | |
| 755 | opp_table = _find_opp_table(dev); |
| 756 | if (IS_ERR(opp_table)) |
| 757 | return ERR_CAST(opp_table); |
| 758 | |
| 759 | required_np = of_parse_phandle(np, "required-opps", 0); |
| 760 | if (unlikely(!required_np)) { |
| 761 | dev_err(dev, "Unable to parse required-opps\n"); |
| 762 | goto put_opp_table; |
| 763 | } |
| 764 | |
| 765 | mutex_lock(&opp_table->lock); |
| 766 | |
| 767 | list_for_each_entry(temp_opp, &opp_table->opp_list, node) { |
| 768 | if (temp_opp->available && temp_opp->np == required_np) { |
| 769 | opp = temp_opp; |
| 770 | |
| 771 | /* Increment the reference count of OPP */ |
| 772 | dev_pm_opp_get(opp); |
| 773 | break; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | mutex_unlock(&opp_table->lock); |
| 778 | |
| 779 | of_node_put(required_np); |
| 780 | put_opp_table: |
| 781 | dev_pm_opp_put_opp_table(opp_table); |
| 782 | |
| 783 | return opp; |
| 784 | } |
| 785 | EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp); |
Viresh Kumar | e2f4b5f | 2018-01-12 10:03:45 +0530 | [diff] [blame] | 786 | |
| 787 | /** |
| 788 | * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp |
| 789 | * @opp: opp for which DT node has to be returned for |
| 790 | * |
| 791 | * Return: DT node corresponding to the opp, else 0 on success. |
| 792 | * |
| 793 | * The caller needs to put the node with of_node_put() after using it. |
| 794 | */ |
| 795 | struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp) |
| 796 | { |
| 797 | if (IS_ERR_OR_NULL(opp)) { |
| 798 | pr_err("%s: Invalid parameters\n", __func__); |
| 799 | return NULL; |
| 800 | } |
| 801 | |
| 802 | return of_node_get(opp->np); |
| 803 | } |
| 804 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node); |