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