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