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 | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 76 | /* The caller must call dev_pm_opp_put() after the OPP is used */ |
| 77 | static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table, |
| 78 | struct device_node *opp_np) |
| 79 | { |
| 80 | struct dev_pm_opp *opp; |
| 81 | |
| 82 | lockdep_assert_held(&opp_table_lock); |
| 83 | |
| 84 | mutex_lock(&opp_table->lock); |
| 85 | |
| 86 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
| 87 | if (opp->np == opp_np) { |
| 88 | dev_pm_opp_get(opp); |
| 89 | mutex_unlock(&opp_table->lock); |
| 90 | return opp; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | mutex_unlock(&opp_table->lock); |
| 95 | |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | static struct device_node *of_parse_required_opp(struct device_node *np, |
| 100 | int index) |
| 101 | { |
| 102 | struct device_node *required_np; |
| 103 | |
| 104 | required_np = of_parse_phandle(np, "required-opps", index); |
| 105 | if (unlikely(!required_np)) { |
| 106 | pr_err("%s: Unable to parse required-opps: %pOF, index: %d\n", |
| 107 | __func__, np, index); |
| 108 | } |
| 109 | |
| 110 | return required_np; |
| 111 | } |
| 112 | |
| 113 | /* The caller must call dev_pm_opp_put_opp_table() after the table is used */ |
| 114 | static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np) |
| 115 | { |
| 116 | struct opp_table *opp_table; |
| 117 | struct dev_pm_opp *opp; |
| 118 | |
| 119 | lockdep_assert_held(&opp_table_lock); |
| 120 | |
| 121 | list_for_each_entry(opp_table, &opp_tables, node) { |
| 122 | opp = _find_opp_of_np(opp_table, opp_np); |
| 123 | if (opp) { |
| 124 | dev_pm_opp_put(opp); |
| 125 | _get_opp_table_kref(opp_table); |
| 126 | return opp_table; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return ERR_PTR(-ENODEV); |
| 131 | } |
| 132 | |
| 133 | /* Free resources previously acquired by _opp_table_alloc_required_tables() */ |
| 134 | static void _opp_table_free_required_tables(struct opp_table *opp_table) |
| 135 | { |
| 136 | struct opp_table **required_opp_tables = opp_table->required_opp_tables; |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 137 | struct device **genpd_virt_devs = opp_table->genpd_virt_devs; |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 138 | int i; |
| 139 | |
| 140 | if (!required_opp_tables) |
| 141 | return; |
| 142 | |
| 143 | for (i = 0; i < opp_table->required_opp_count; i++) { |
| 144 | if (IS_ERR_OR_NULL(required_opp_tables[i])) |
| 145 | break; |
| 146 | |
| 147 | dev_pm_opp_put_opp_table(required_opp_tables[i]); |
| 148 | } |
| 149 | |
| 150 | kfree(required_opp_tables); |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 151 | kfree(genpd_virt_devs); |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 152 | |
| 153 | opp_table->required_opp_count = 0; |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 154 | opp_table->genpd_virt_devs = NULL; |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 155 | opp_table->required_opp_tables = NULL; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Populate all devices and opp tables which are part of "required-opps" list. |
| 160 | * Checking only the first OPP node should be enough. |
| 161 | */ |
| 162 | static void _opp_table_alloc_required_tables(struct opp_table *opp_table, |
| 163 | struct device *dev, |
| 164 | struct device_node *opp_np) |
| 165 | { |
| 166 | struct opp_table **required_opp_tables; |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 167 | struct device **genpd_virt_devs = NULL; |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 168 | struct device_node *required_np, *np; |
| 169 | int count, i; |
| 170 | |
| 171 | /* Traversing the first OPP node is all we need */ |
| 172 | np = of_get_next_available_child(opp_np, NULL); |
| 173 | if (!np) { |
| 174 | dev_err(dev, "Empty OPP table\n"); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | count = of_count_phandle_with_args(np, "required-opps", NULL); |
| 179 | if (!count) |
| 180 | goto put_np; |
| 181 | |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 182 | if (count > 1) { |
| 183 | genpd_virt_devs = kcalloc(count, sizeof(*genpd_virt_devs), |
| 184 | GFP_KERNEL); |
| 185 | if (!genpd_virt_devs) |
| 186 | goto put_np; |
| 187 | } |
| 188 | |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 189 | required_opp_tables = kcalloc(count, sizeof(*required_opp_tables), |
| 190 | GFP_KERNEL); |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 191 | if (!required_opp_tables) { |
| 192 | kfree(genpd_virt_devs); |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 193 | goto put_np; |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 194 | } |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 195 | |
Viresh Kumar | 4f018bc | 2018-06-26 16:29:34 +0530 | [diff] [blame] | 196 | opp_table->genpd_virt_devs = genpd_virt_devs; |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 197 | opp_table->required_opp_tables = required_opp_tables; |
| 198 | opp_table->required_opp_count = count; |
| 199 | |
| 200 | for (i = 0; i < count; i++) { |
| 201 | required_np = of_parse_required_opp(np, i); |
| 202 | if (!required_np) |
| 203 | goto free_required_tables; |
| 204 | |
| 205 | required_opp_tables[i] = _find_table_of_opp_np(required_np); |
| 206 | of_node_put(required_np); |
| 207 | |
| 208 | if (IS_ERR(required_opp_tables[i])) |
| 209 | goto free_required_tables; |
| 210 | |
| 211 | /* |
| 212 | * We only support genpd's OPPs in the "required-opps" for now, |
| 213 | * as we don't know how much about other cases. Error out if the |
| 214 | * required OPP doesn't belong to a genpd. |
| 215 | */ |
| 216 | if (!required_opp_tables[i]->is_genpd) { |
| 217 | dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n", |
| 218 | required_np); |
| 219 | goto free_required_tables; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | goto put_np; |
| 224 | |
| 225 | free_required_tables: |
| 226 | _opp_table_free_required_tables(opp_table); |
| 227 | put_np: |
| 228 | of_node_put(np); |
| 229 | } |
| 230 | |
Viresh Kumar | eb7c8743 | 2018-09-05 16:17:14 +0530 | [diff] [blame] | 231 | void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, |
| 232 | int index) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 233 | { |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 234 | struct device_node *np, *opp_np; |
| 235 | u32 val; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * Only required for backward compatibility with v1 bindings, but isn't |
| 239 | * harmful for other cases. And so we do it unconditionally. |
| 240 | */ |
| 241 | np = of_node_get(dev->of_node); |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 242 | if (!np) |
| 243 | return; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 244 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 245 | if (!of_property_read_u32(np, "clock-latency", &val)) |
| 246 | opp_table->clock_latency_ns_max = val; |
| 247 | of_property_read_u32(np, "voltage-tolerance", |
| 248 | &opp_table->voltage_tolerance_v1); |
| 249 | |
Viresh Kumar | 61d8e7c | 2018-06-13 16:00:11 +0530 | [diff] [blame] | 250 | if (of_find_property(np, "#power-domain-cells", NULL)) |
| 251 | opp_table->is_genpd = true; |
| 252 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 253 | /* Get OPP table node */ |
| 254 | opp_np = _opp_of_get_opp_desc_node(np, index); |
| 255 | of_node_put(np); |
| 256 | |
| 257 | if (!opp_np) |
| 258 | return; |
| 259 | |
| 260 | if (of_property_read_bool(opp_np, "opp-shared")) |
| 261 | opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; |
| 262 | else |
| 263 | opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; |
| 264 | |
| 265 | opp_table->np = opp_np; |
| 266 | |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 267 | _opp_table_alloc_required_tables(opp_table, dev, opp_np); |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 268 | of_node_put(opp_np); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 269 | } |
| 270 | |
Viresh Kumar | 5d6d106 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 271 | void _of_clear_opp_table(struct opp_table *opp_table) |
| 272 | { |
| 273 | _opp_table_free_required_tables(opp_table); |
| 274 | } |
| 275 | |
Viresh Kumar | da544b6 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 276 | /* |
| 277 | * Release all resources previously acquired with a call to |
| 278 | * _of_opp_alloc_required_opps(). |
| 279 | */ |
| 280 | void _of_opp_free_required_opps(struct opp_table *opp_table, |
| 281 | struct dev_pm_opp *opp) |
| 282 | { |
| 283 | struct dev_pm_opp **required_opps = opp->required_opps; |
| 284 | int i; |
| 285 | |
| 286 | if (!required_opps) |
| 287 | return; |
| 288 | |
| 289 | for (i = 0; i < opp_table->required_opp_count; i++) { |
| 290 | if (!required_opps[i]) |
| 291 | break; |
| 292 | |
| 293 | /* Put the reference back */ |
| 294 | dev_pm_opp_put(required_opps[i]); |
| 295 | } |
| 296 | |
| 297 | kfree(required_opps); |
| 298 | opp->required_opps = NULL; |
| 299 | } |
| 300 | |
| 301 | /* Populate all required OPPs which are part of "required-opps" list */ |
| 302 | static int _of_opp_alloc_required_opps(struct opp_table *opp_table, |
| 303 | struct dev_pm_opp *opp) |
| 304 | { |
| 305 | struct dev_pm_opp **required_opps; |
| 306 | struct opp_table *required_table; |
| 307 | struct device_node *np; |
| 308 | int i, ret, count = opp_table->required_opp_count; |
| 309 | |
| 310 | if (!count) |
| 311 | return 0; |
| 312 | |
| 313 | required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL); |
| 314 | if (!required_opps) |
| 315 | return -ENOMEM; |
| 316 | |
| 317 | opp->required_opps = required_opps; |
| 318 | |
| 319 | for (i = 0; i < count; i++) { |
| 320 | required_table = opp_table->required_opp_tables[i]; |
| 321 | |
| 322 | np = of_parse_required_opp(opp->np, i); |
| 323 | if (unlikely(!np)) { |
| 324 | ret = -ENODEV; |
| 325 | goto free_required_opps; |
| 326 | } |
| 327 | |
| 328 | required_opps[i] = _find_opp_of_np(required_table, np); |
| 329 | of_node_put(np); |
| 330 | |
| 331 | if (!required_opps[i]) { |
| 332 | pr_err("%s: Unable to find required OPP node: %pOF (%d)\n", |
| 333 | __func__, opp->np, i); |
| 334 | ret = -ENODEV; |
| 335 | goto free_required_opps; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | |
| 341 | free_required_opps: |
| 342 | _of_opp_free_required_opps(opp_table, opp); |
| 343 | |
| 344 | return ret; |
| 345 | } |
| 346 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 347 | static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table, |
| 348 | struct device_node *np) |
| 349 | { |
| 350 | unsigned int count = opp_table->supported_hw_count; |
| 351 | u32 version; |
| 352 | int ret; |
| 353 | |
Dave Gerlach | a4ee454 | 2016-09-23 15:07:47 -0500 | [diff] [blame] | 354 | if (!opp_table->supported_hw) { |
| 355 | /* |
| 356 | * In the case that no supported_hw has been set by the |
| 357 | * platform but there is an opp-supported-hw value set for |
| 358 | * an OPP then the OPP should not be enabled as there is |
| 359 | * no way to see if the hardware supports it. |
| 360 | */ |
| 361 | if (of_find_property(np, "opp-supported-hw", NULL)) |
| 362 | return false; |
| 363 | else |
| 364 | return true; |
| 365 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 366 | |
| 367 | while (count--) { |
| 368 | ret = of_property_read_u32_index(np, "opp-supported-hw", count, |
| 369 | &version); |
| 370 | if (ret) { |
| 371 | dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n", |
| 372 | __func__, count, ret); |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | /* Both of these are bitwise masks of the versions */ |
| 377 | if (!(version & opp_table->supported_hw[count])) |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | return true; |
| 382 | } |
| 383 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 384 | static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, |
| 385 | struct opp_table *opp_table) |
| 386 | { |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 387 | u32 *microvolt, *microamp = NULL; |
| 388 | int supplies, vcount, icount, ret, i, j; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 389 | struct property *prop = NULL; |
| 390 | char name[NAME_MAX]; |
| 391 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 392 | supplies = opp_table->regulator_count ? opp_table->regulator_count : 1; |
| 393 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 394 | /* Search for "opp-microvolt-<name>" */ |
| 395 | if (opp_table->prop_name) { |
| 396 | snprintf(name, sizeof(name), "opp-microvolt-%s", |
| 397 | opp_table->prop_name); |
| 398 | prop = of_find_property(opp->np, name, NULL); |
| 399 | } |
| 400 | |
| 401 | if (!prop) { |
| 402 | /* Search for "opp-microvolt" */ |
| 403 | sprintf(name, "opp-microvolt"); |
| 404 | prop = of_find_property(opp->np, name, NULL); |
| 405 | |
| 406 | /* Missing property isn't a problem, but an invalid entry is */ |
Viresh Kumar | 688a48b | 2017-05-23 09:32:12 +0530 | [diff] [blame] | 407 | if (!prop) { |
| 408 | if (!opp_table->regulator_count) |
| 409 | return 0; |
| 410 | |
| 411 | dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n", |
| 412 | __func__); |
| 413 | return -EINVAL; |
| 414 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 415 | } |
| 416 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 417 | vcount = of_property_count_u32_elems(opp->np, name); |
| 418 | if (vcount < 0) { |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 419 | dev_err(dev, "%s: Invalid %s property (%d)\n", |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 420 | __func__, name, vcount); |
| 421 | return vcount; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 422 | } |
| 423 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 424 | /* There can be one or three elements per supply */ |
| 425 | if (vcount != supplies && vcount != supplies * 3) { |
| 426 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", |
| 427 | __func__, name, vcount, supplies); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 428 | return -EINVAL; |
| 429 | } |
| 430 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 431 | microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL); |
| 432 | if (!microvolt) |
| 433 | return -ENOMEM; |
| 434 | |
| 435 | ret = of_property_read_u32_array(opp->np, name, microvolt, vcount); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 436 | if (ret) { |
| 437 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret); |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 438 | ret = -EINVAL; |
| 439 | goto free_microvolt; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* Search for "opp-microamp-<name>" */ |
| 443 | prop = NULL; |
| 444 | if (opp_table->prop_name) { |
| 445 | snprintf(name, sizeof(name), "opp-microamp-%s", |
| 446 | opp_table->prop_name); |
| 447 | prop = of_find_property(opp->np, name, NULL); |
| 448 | } |
| 449 | |
| 450 | if (!prop) { |
| 451 | /* Search for "opp-microamp" */ |
| 452 | sprintf(name, "opp-microamp"); |
| 453 | prop = of_find_property(opp->np, name, NULL); |
| 454 | } |
| 455 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 456 | if (prop) { |
| 457 | icount = of_property_count_u32_elems(opp->np, name); |
| 458 | if (icount < 0) { |
| 459 | dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, |
| 460 | name, icount); |
| 461 | ret = icount; |
| 462 | goto free_microvolt; |
| 463 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 464 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 465 | if (icount != supplies) { |
| 466 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", |
| 467 | __func__, name, icount, supplies); |
| 468 | ret = -EINVAL; |
| 469 | goto free_microvolt; |
| 470 | } |
| 471 | |
| 472 | microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL); |
| 473 | if (!microamp) { |
| 474 | ret = -EINVAL; |
| 475 | goto free_microvolt; |
| 476 | } |
| 477 | |
| 478 | ret = of_property_read_u32_array(opp->np, name, microamp, |
| 479 | icount); |
| 480 | if (ret) { |
| 481 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, |
| 482 | name, ret); |
| 483 | ret = -EINVAL; |
| 484 | goto free_microamp; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | for (i = 0, j = 0; i < supplies; i++) { |
| 489 | opp->supplies[i].u_volt = microvolt[j++]; |
| 490 | |
| 491 | if (vcount == supplies) { |
| 492 | opp->supplies[i].u_volt_min = opp->supplies[i].u_volt; |
| 493 | opp->supplies[i].u_volt_max = opp->supplies[i].u_volt; |
| 494 | } else { |
| 495 | opp->supplies[i].u_volt_min = microvolt[j++]; |
| 496 | opp->supplies[i].u_volt_max = microvolt[j++]; |
| 497 | } |
| 498 | |
| 499 | if (microamp) |
| 500 | opp->supplies[i].u_amp = microamp[i]; |
| 501 | } |
| 502 | |
| 503 | free_microamp: |
| 504 | kfree(microamp); |
| 505 | free_microvolt: |
| 506 | kfree(microvolt); |
| 507 | |
| 508 | return ret; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
| 512 | * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT |
| 513 | * entries |
| 514 | * @dev: device pointer used to lookup OPP table. |
| 515 | * |
| 516 | * Free OPPs created using static entries present in DT. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 517 | */ |
| 518 | void dev_pm_opp_of_remove_table(struct device *dev) |
| 519 | { |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 520 | _dev_pm_opp_find_and_remove_table(dev); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 521 | } |
| 522 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table); |
| 523 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 524 | /** |
| 525 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 526 | * @opp_table: OPP table |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 527 | * @dev: device for which we do this operation |
| 528 | * @np: device node |
| 529 | * |
| 530 | * This function adds an opp definition to the opp table and returns status. The |
| 531 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be |
| 532 | * removed by dev_pm_opp_remove. |
| 533 | * |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 534 | * Return: |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 535 | * Valid OPP pointer: |
| 536 | * On success |
| 537 | * NULL: |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 538 | * Duplicate OPPs (both freq and volt are same) and opp->available |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 539 | * OR if the OPP is not supported by hardware. |
| 540 | * ERR_PTR(-EEXIST): |
| 541 | * Freq are same and volt are different OR |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 542 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 543 | * ERR_PTR(-ENOMEM): |
| 544 | * Memory allocation failure |
| 545 | * ERR_PTR(-EINVAL): |
| 546 | * Failed parsing the OPP node |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 547 | */ |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 548 | static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table, |
| 549 | struct device *dev, struct device_node *np) |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 550 | { |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 551 | struct dev_pm_opp *new_opp; |
Dan Carpenter | 6a89e01 | 2018-05-16 12:52:41 +0300 | [diff] [blame] | 552 | u64 rate = 0; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 553 | u32 val; |
| 554 | int ret; |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 555 | bool rate_not_available = false; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 556 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 557 | new_opp = _opp_allocate(opp_table); |
| 558 | if (!new_opp) |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 559 | return ERR_PTR(-ENOMEM); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 560 | |
| 561 | ret = of_property_read_u64(np, "opp-hz", &rate); |
| 562 | if (ret < 0) { |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 563 | /* "opp-hz" is optional for devices like power domains. */ |
Viresh Kumar | 61d8e7c | 2018-06-13 16:00:11 +0530 | [diff] [blame] | 564 | if (!opp_table->is_genpd) { |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 565 | dev_err(dev, "%s: opp-hz not found\n", __func__); |
| 566 | goto free_opp; |
| 567 | } |
| 568 | |
| 569 | rate_not_available = true; |
| 570 | } else { |
| 571 | /* |
| 572 | * Rate is defined as an unsigned long in clk API, and so |
| 573 | * casting explicitly to its type. Must be fixed once rate is 64 |
| 574 | * bit guaranteed in clk API. |
| 575 | */ |
| 576 | new_opp->rate = (unsigned long)rate; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* Check if the OPP supports hardware's hierarchy of versions or not */ |
| 580 | if (!_opp_is_supported(dev, opp_table, np)) { |
| 581 | dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate); |
| 582 | goto free_opp; |
| 583 | } |
| 584 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 585 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); |
| 586 | |
| 587 | new_opp->np = np; |
| 588 | new_opp->dynamic = false; |
| 589 | new_opp->available = true; |
| 590 | |
Viresh Kumar | da544b6 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 591 | ret = _of_opp_alloc_required_opps(opp_table, new_opp); |
| 592 | if (ret) |
| 593 | goto free_opp; |
| 594 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 595 | if (!of_property_read_u32(np, "clock-latency-ns", &val)) |
| 596 | new_opp->clock_latency_ns = val; |
| 597 | |
| 598 | ret = opp_parse_supplies(new_opp, dev, opp_table); |
| 599 | if (ret) |
Viresh Kumar | da544b6 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 600 | goto free_required_opps; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 601 | |
Viresh Kumar | ca1b5d7 | 2018-06-14 10:03:26 +0530 | [diff] [blame^] | 602 | if (opp_table->is_genpd) |
| 603 | new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp); |
| 604 | |
Viresh Kumar | a1e8c13 | 2018-04-06 14:35:45 +0530 | [diff] [blame] | 605 | ret = _opp_add(dev, new_opp, opp_table, rate_not_available); |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 606 | if (ret) { |
| 607 | /* Don't return error for duplicate OPPs */ |
| 608 | if (ret == -EBUSY) |
| 609 | ret = 0; |
Viresh Kumar | da544b6 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 610 | goto free_required_opps; |
Viresh Kumar | 7f8538e | 2017-01-02 14:40:55 +0530 | [diff] [blame] | 611 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 612 | |
| 613 | /* OPP to select on device suspend */ |
| 614 | if (of_property_read_bool(np, "opp-suspend")) { |
| 615 | if (opp_table->suspend_opp) { |
| 616 | dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n", |
| 617 | __func__, opp_table->suspend_opp->rate, |
| 618 | new_opp->rate); |
| 619 | } else { |
| 620 | new_opp->suspend = true; |
| 621 | opp_table->suspend_opp = new_opp; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max) |
| 626 | opp_table->clock_latency_ns_max = new_opp->clock_latency_ns; |
| 627 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 628 | 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] | 629 | __func__, new_opp->turbo, new_opp->rate, |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 630 | new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min, |
| 631 | new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 632 | |
| 633 | /* |
| 634 | * Notify the changes in the availability of the operable |
| 635 | * frequency/voltage list. |
| 636 | */ |
Viresh Kumar | 052c6f1 | 2017-01-23 10:11:49 +0530 | [diff] [blame] | 637 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 638 | return new_opp; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 639 | |
Viresh Kumar | da544b6 | 2018-06-07 14:50:43 +0530 | [diff] [blame] | 640 | free_required_opps: |
| 641 | _of_opp_free_required_opps(opp_table, new_opp); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 642 | free_opp: |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 643 | _opp_free(new_opp); |
| 644 | |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 645 | return ERR_PTR(ret); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* Initializes OPP tables based on new bindings */ |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 649 | 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] | 650 | { |
| 651 | struct device_node *np; |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 652 | int ret, count = 0, pstate_count = 0; |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 653 | struct dev_pm_opp *opp; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 654 | |
Viresh Kumar | 283d55e | 2018-09-07 09:01:54 +0530 | [diff] [blame] | 655 | /* OPP table is already initialized for the device */ |
| 656 | if (opp_table->parsed_static_opps) { |
| 657 | kref_get(&opp_table->list_kref); |
| 658 | return 0; |
| 659 | } |
| 660 | |
Viresh Kumar | d0e8ae6 | 2018-09-11 11:14:11 +0530 | [diff] [blame] | 661 | kref_init(&opp_table->list_kref); |
| 662 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 663 | /* We have opp-table node now, iterate over it and add OPPs */ |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 664 | for_each_available_child_of_node(opp_table->np, np) { |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 665 | opp = _opp_add_static_v2(opp_table, dev, np); |
| 666 | if (IS_ERR(opp)) { |
| 667 | ret = PTR_ERR(opp); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 668 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, |
| 669 | ret); |
Tobias Jordan | 7978db3 | 2017-10-04 11:35:03 +0530 | [diff] [blame] | 670 | of_node_put(np); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 671 | goto put_list_kref; |
Dave Gerlach | deac870 | 2018-10-03 16:13:15 +0530 | [diff] [blame] | 672 | } else if (opp) { |
| 673 | count++; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
| 677 | /* There should be one of more OPP defined */ |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 678 | if (WARN_ON(!count)) { |
| 679 | ret = -ENOENT; |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 680 | goto put_list_kref; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 681 | } |
| 682 | |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 683 | list_for_each_entry(opp, &opp_table->opp_list, node) |
| 684 | pstate_count += !!opp->pstate; |
| 685 | |
| 686 | /* Either all or none of the nodes shall have performance state set */ |
| 687 | if (pstate_count && pstate_count != count) { |
| 688 | dev_err(dev, "Not all nodes have performance state set (%d: %d)\n", |
| 689 | count, pstate_count); |
| 690 | ret = -ENOENT; |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 691 | goto put_list_kref; |
Viresh Kumar | 3ba9832 | 2016-11-18 15:47:46 +0530 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | if (pstate_count) |
| 695 | opp_table->genpd_performance_state = true; |
| 696 | |
Viresh Kumar | f06ed90 | 2018-06-14 12:04:43 +0530 | [diff] [blame] | 697 | opp_table->parsed_static_opps = true; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 698 | |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 699 | return 0; |
| 700 | |
| 701 | put_list_kref: |
| 702 | _put_opp_list_kref(opp_table); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 703 | |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | /* Initializes OPP tables based on old-deprecated bindings */ |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 708 | 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] | 709 | { |
| 710 | const struct property *prop; |
| 711 | const __be32 *val; |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 712 | int nr, ret = 0; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 713 | |
| 714 | prop = of_find_property(dev->of_node, "operating-points", NULL); |
| 715 | if (!prop) |
| 716 | return -ENODEV; |
| 717 | if (!prop->value) |
| 718 | return -ENODATA; |
| 719 | |
| 720 | /* |
| 721 | * Each OPP is a set of tuples consisting of frequency and |
| 722 | * voltage like <freq-kHz vol-uV>. |
| 723 | */ |
| 724 | nr = prop->length / sizeof(u32); |
| 725 | if (nr % 2) { |
| 726 | dev_err(dev, "%s: Invalid OPP table\n", __func__); |
| 727 | return -EINVAL; |
| 728 | } |
| 729 | |
Viresh Kumar | d0e8ae6 | 2018-09-11 11:14:11 +0530 | [diff] [blame] | 730 | kref_init(&opp_table->list_kref); |
| 731 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 732 | val = prop->value; |
| 733 | while (nr) { |
| 734 | unsigned long freq = be32_to_cpup(val++) * 1000; |
| 735 | unsigned long volt = be32_to_cpup(val++); |
| 736 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 737 | ret = _opp_add_v1(opp_table, dev, freq, volt, false); |
Viresh Kumar | 04a86a8 | 2017-01-02 14:40:58 +0530 | [diff] [blame] | 738 | if (ret) { |
| 739 | dev_err(dev, "%s: Failed to add OPP %ld (%d)\n", |
| 740 | __func__, freq, ret); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 741 | _put_opp_list_kref(opp_table); |
Viresh Kumar | cdd6ed9 | 2018-09-12 12:35:19 +0530 | [diff] [blame] | 742 | return ret; |
Viresh Kumar | 04a86a8 | 2017-01-02 14:40:58 +0530 | [diff] [blame] | 743 | } |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 744 | nr -= 2; |
| 745 | } |
| 746 | |
Viresh Kumar | 8cd2f6e | 2017-01-02 14:41:01 +0530 | [diff] [blame] | 747 | return ret; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /** |
| 751 | * dev_pm_opp_of_add_table() - Initialize opp table from device tree |
| 752 | * @dev: device pointer used to lookup OPP table. |
| 753 | * |
| 754 | * Register the initial OPP table with the OPP library for given device. |
| 755 | * |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 756 | * Return: |
| 757 | * 0 On success OR |
| 758 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 759 | * -EEXIST Freq are same and volt are different OR |
| 760 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 761 | * -ENOMEM Memory allocation failure |
| 762 | * -ENODEV when 'operating-points' property is not found or is invalid data |
| 763 | * in device node. |
| 764 | * -ENODATA when empty 'operating-points' property is found |
| 765 | * -EINVAL when invalid entries are found in opp-v2 table |
| 766 | */ |
| 767 | int dev_pm_opp_of_add_table(struct device *dev) |
| 768 | { |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 769 | struct opp_table *opp_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 770 | int ret; |
| 771 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 772 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0); |
| 773 | if (!opp_table) |
| 774 | return -ENOMEM; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 775 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 776 | /* |
| 777 | * OPPs have two version of bindings now. Also try the old (v1) |
| 778 | * bindings for backward compatibility with older dtbs. |
| 779 | */ |
| 780 | if (opp_table->np) |
| 781 | ret = _of_add_opp_table_v2(dev, opp_table); |
| 782 | else |
| 783 | ret = _of_add_opp_table_v1(dev, opp_table); |
| 784 | |
| 785 | if (ret) |
| 786 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 787 | |
| 788 | return ret; |
| 789 | } |
| 790 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table); |
| 791 | |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 792 | /** |
| 793 | * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree |
| 794 | * @dev: device pointer used to lookup OPP table. |
| 795 | * @index: Index number. |
| 796 | * |
| 797 | * Register the initial OPP table with the OPP library for given device only |
| 798 | * using the "operating-points-v2" property. |
| 799 | * |
| 800 | * Return: |
| 801 | * 0 On success OR |
| 802 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 803 | * -EEXIST Freq are same and volt are different OR |
| 804 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 805 | * -ENOMEM Memory allocation failure |
| 806 | * -ENODEV when 'operating-points' property is not found or is invalid data |
| 807 | * in device node. |
| 808 | * -ENODATA when empty 'operating-points' property is found |
| 809 | * -EINVAL when invalid entries are found in opp-v2 table |
| 810 | */ |
| 811 | int dev_pm_opp_of_add_table_indexed(struct device *dev, int index) |
| 812 | { |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 813 | struct opp_table *opp_table; |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 814 | int ret, count; |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 815 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 816 | if (index) { |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 817 | /* |
| 818 | * If only one phandle is present, then the same OPP table |
| 819 | * applies for all index requests. |
| 820 | */ |
| 821 | count = of_count_phandle_with_args(dev->of_node, |
| 822 | "operating-points-v2", NULL); |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 823 | if (count != 1) |
| 824 | return -ENODEV; |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 825 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 826 | index = 0; |
Viresh Kumar | 8a352fd | 2018-05-30 15:19:38 +0530 | [diff] [blame] | 827 | } |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 828 | |
Viresh Kumar | 5ed4cec | 2018-09-12 11:21:17 +0530 | [diff] [blame] | 829 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, index); |
| 830 | if (!opp_table) |
| 831 | return -ENOMEM; |
| 832 | |
| 833 | ret = _of_add_opp_table_v2(dev, opp_table); |
| 834 | if (ret) |
| 835 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 836 | |
| 837 | return ret; |
| 838 | } |
| 839 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed); |
| 840 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 841 | /* CPU device specific helpers */ |
| 842 | |
| 843 | /** |
| 844 | * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask |
| 845 | * @cpumask: cpumask for which OPP table needs to be removed |
| 846 | * |
| 847 | * This removes the OPP tables for CPUs present in the @cpumask. |
| 848 | * This should be used only to remove static entries created from DT. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 849 | */ |
| 850 | void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask) |
| 851 | { |
Viresh Kumar | 2a4eb73 | 2018-09-13 13:14:36 +0530 | [diff] [blame] | 852 | _dev_pm_opp_cpumask_remove_table(cpumask, -1); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 853 | } |
| 854 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table); |
| 855 | |
| 856 | /** |
| 857 | * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask |
| 858 | * @cpumask: cpumask for which OPP table needs to be added. |
| 859 | * |
| 860 | * This adds the OPP tables for CPUs present in the @cpumask. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 861 | */ |
| 862 | int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) |
| 863 | { |
| 864 | struct device *cpu_dev; |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 865 | int cpu, ret; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 866 | |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 867 | if (WARN_ON(cpumask_empty(cpumask))) |
| 868 | return -ENODEV; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 869 | |
| 870 | for_each_cpu(cpu, cpumask) { |
| 871 | cpu_dev = get_cpu_device(cpu); |
| 872 | if (!cpu_dev) { |
| 873 | pr_err("%s: failed to get cpu%d device\n", __func__, |
| 874 | cpu); |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 875 | ret = -ENODEV; |
| 876 | goto remove_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | ret = dev_pm_opp_of_add_table(cpu_dev); |
| 880 | if (ret) { |
Viresh Kumar | 5b60697 | 2017-07-12 09:21:49 +0530 | [diff] [blame] | 881 | /* |
| 882 | * OPP may get registered dynamically, don't print error |
| 883 | * message here. |
| 884 | */ |
| 885 | pr_debug("%s: couldn't find opp table for cpu:%d, %d\n", |
| 886 | __func__, cpu, ret); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 887 | |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 888 | goto remove_table; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
Viresh Kumar | 50b6b87 | 2018-10-03 15:12:06 +0530 | [diff] [blame] | 892 | return 0; |
| 893 | |
| 894 | remove_table: |
| 895 | /* Free all other OPPs */ |
| 896 | _dev_pm_opp_cpumask_remove_table(cpumask, cpu); |
| 897 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 898 | return ret; |
| 899 | } |
| 900 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table); |
| 901 | |
| 902 | /* |
| 903 | * Works only for OPP v2 bindings. |
| 904 | * |
| 905 | * Returns -ENOENT if operating-points-v2 bindings aren't supported. |
| 906 | */ |
| 907 | /** |
| 908 | * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with |
| 909 | * @cpu_dev using operating-points-v2 |
| 910 | * bindings. |
| 911 | * |
| 912 | * @cpu_dev: CPU device for which we do this operation |
| 913 | * @cpumask: cpumask to update with information of sharing CPUs |
| 914 | * |
| 915 | * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. |
| 916 | * |
| 917 | * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev. |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 918 | */ |
| 919 | int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, |
| 920 | struct cpumask *cpumask) |
| 921 | { |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 922 | struct device_node *np, *tmp_np, *cpu_np; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 923 | int cpu, ret = 0; |
| 924 | |
| 925 | /* Get OPP descriptor node */ |
Dave Gerlach | 0764c60 | 2017-02-03 11:29:26 -0600 | [diff] [blame] | 926 | np = dev_pm_opp_of_get_opp_desc_node(cpu_dev); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 927 | if (!np) { |
Masahiro Yamada | 349aa92 | 2016-10-20 16:12:49 +0900 | [diff] [blame] | 928 | dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 929 | return -ENOENT; |
| 930 | } |
| 931 | |
| 932 | cpumask_set_cpu(cpu_dev->id, cpumask); |
| 933 | |
| 934 | /* OPPs are shared ? */ |
| 935 | if (!of_property_read_bool(np, "opp-shared")) |
| 936 | goto put_cpu_node; |
| 937 | |
| 938 | for_each_possible_cpu(cpu) { |
| 939 | if (cpu == cpu_dev->id) |
| 940 | continue; |
| 941 | |
Sudeep Holla | 9867999 | 2017-10-12 11:32:23 +0100 | [diff] [blame] | 942 | cpu_np = of_cpu_device_node_get(cpu); |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 943 | if (!cpu_np) { |
| 944 | dev_err(cpu_dev, "%s: failed to get cpu%d node\n", |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 945 | __func__, cpu); |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 946 | ret = -ENOENT; |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 947 | goto put_cpu_node; |
| 948 | } |
| 949 | |
| 950 | /* Get OPP descriptor node */ |
Viresh Kumar | fa9b274 | 2017-04-26 10:45:46 +0530 | [diff] [blame] | 951 | tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0); |
Sudeep Holla | 9867999 | 2017-10-12 11:32:23 +0100 | [diff] [blame] | 952 | of_node_put(cpu_np); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 953 | if (!tmp_np) { |
Waldemar Rymarkiewicz | 7627929 | 2017-07-27 12:01:17 +0200 | [diff] [blame] | 954 | pr_err("%pOF: Couldn't find opp node\n", cpu_np); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 955 | ret = -ENOENT; |
| 956 | goto put_cpu_node; |
| 957 | } |
| 958 | |
| 959 | /* CPUs are sharing opp node */ |
| 960 | if (np == tmp_np) |
| 961 | cpumask_set_cpu(cpu, cpumask); |
| 962 | |
| 963 | of_node_put(tmp_np); |
| 964 | } |
| 965 | |
| 966 | put_cpu_node: |
| 967 | of_node_put(np); |
| 968 | return ret; |
| 969 | } |
| 970 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus); |
Viresh Kumar | a88bd2a | 2017-11-29 15:18:36 +0530 | [diff] [blame] | 971 | |
| 972 | /** |
| 973 | * of_dev_pm_opp_find_required_opp() - Search for required OPP. |
| 974 | * @dev: The device whose OPP node is referenced by the 'np' DT node. |
| 975 | * @np: Node that contains the "required-opps" property. |
| 976 | * |
| 977 | * Returns the OPP of the device 'dev', whose phandle is present in the "np" |
| 978 | * node. Although the "required-opps" property supports having multiple |
| 979 | * phandles, this helper routine only parses the very first phandle in the list. |
| 980 | * |
| 981 | * Return: Matching opp, else returns ERR_PTR in case of error and should be |
| 982 | * handled using IS_ERR. |
| 983 | * |
| 984 | * The callers are required to call dev_pm_opp_put() for the returned OPP after |
| 985 | * use. |
| 986 | */ |
| 987 | struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev, |
| 988 | struct device_node *np) |
| 989 | { |
| 990 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV); |
| 991 | struct device_node *required_np; |
| 992 | struct opp_table *opp_table; |
| 993 | |
| 994 | opp_table = _find_opp_table(dev); |
| 995 | if (IS_ERR(opp_table)) |
| 996 | return ERR_CAST(opp_table); |
| 997 | |
| 998 | required_np = of_parse_phandle(np, "required-opps", 0); |
| 999 | if (unlikely(!required_np)) { |
| 1000 | dev_err(dev, "Unable to parse required-opps\n"); |
| 1001 | goto put_opp_table; |
| 1002 | } |
| 1003 | |
| 1004 | mutex_lock(&opp_table->lock); |
| 1005 | |
| 1006 | list_for_each_entry(temp_opp, &opp_table->opp_list, node) { |
| 1007 | if (temp_opp->available && temp_opp->np == required_np) { |
| 1008 | opp = temp_opp; |
| 1009 | |
| 1010 | /* Increment the reference count of OPP */ |
| 1011 | dev_pm_opp_get(opp); |
| 1012 | break; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | mutex_unlock(&opp_table->lock); |
| 1017 | |
| 1018 | of_node_put(required_np); |
| 1019 | put_opp_table: |
| 1020 | dev_pm_opp_put_opp_table(opp_table); |
| 1021 | |
| 1022 | return opp; |
| 1023 | } |
| 1024 | EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp); |
Viresh Kumar | e2f4b5f | 2018-01-12 10:03:45 +0530 | [diff] [blame] | 1025 | |
| 1026 | /** |
| 1027 | * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp |
| 1028 | * @opp: opp for which DT node has to be returned for |
| 1029 | * |
| 1030 | * Return: DT node corresponding to the opp, else 0 on success. |
| 1031 | * |
| 1032 | * The caller needs to put the node with of_node_put() after using it. |
| 1033 | */ |
| 1034 | struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp) |
| 1035 | { |
| 1036 | if (IS_ERR_OR_NULL(opp)) { |
| 1037 | pr_err("%s: Invalid parameters\n", __func__); |
| 1038 | return NULL; |
| 1039 | } |
| 1040 | |
| 1041 | return of_node_get(opp->np); |
| 1042 | } |
| 1043 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node); |