David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Generic OPP OF helpers |
| 4 | * |
| 5 | * Copyright (C) 2009-2010 Texas Instruments Incorporated. |
| 6 | * Nishanth Menon |
| 7 | * Romit Dasgupta |
| 8 | * Kevin Hilman |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
| 13 | #include <linux/cpu.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/of_device.h> |
| 17 | #include <linux/pm_domain.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/export.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 20 | #include <linux/energy_model.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 21 | |
| 22 | #include "opp.h" |
| 23 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 24 | /* |
| 25 | * Returns opp descriptor node for a device node, caller must |
| 26 | * do of_node_put(). |
| 27 | */ |
| 28 | static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np, |
| 29 | int index) |
| 30 | { |
| 31 | /* "operating-points-v2" can be an array for power domain providers */ |
| 32 | return of_parse_phandle(np, "operating-points-v2", index); |
| 33 | } |
| 34 | |
| 35 | /* Returns opp descriptor node for a device, caller must do of_node_put() */ |
| 36 | struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) |
| 37 | { |
| 38 | return _opp_of_get_opp_desc_node(dev->of_node, 0); |
| 39 | } |
| 40 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node); |
| 41 | |
| 42 | struct opp_table *_managed_opp(struct device *dev, int index) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 43 | { |
| 44 | struct opp_table *opp_table, *managed_table = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 45 | struct device_node *np; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 46 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 47 | np = _opp_of_get_opp_desc_node(dev->of_node, index); |
| 48 | if (!np) |
| 49 | return NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 50 | |
| 51 | list_for_each_entry(opp_table, &opp_tables, node) { |
| 52 | if (opp_table->np == np) { |
| 53 | /* |
| 54 | * Multiple devices can point to the same OPP table and |
| 55 | * so will have same node-pointer, np. |
| 56 | * |
| 57 | * But the OPPs will be considered as shared only if the |
| 58 | * OPP table contains a "opp-shared" property. |
| 59 | */ |
| 60 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { |
| 61 | _get_opp_table_kref(opp_table); |
| 62 | managed_table = opp_table; |
| 63 | } |
| 64 | |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 69 | of_node_put(np); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 70 | |
| 71 | return managed_table; |
| 72 | } |
| 73 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 74 | /* The caller must call dev_pm_opp_put() after the OPP is used */ |
| 75 | static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table, |
| 76 | struct device_node *opp_np) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 77 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 78 | struct dev_pm_opp *opp; |
| 79 | |
| 80 | mutex_lock(&opp_table->lock); |
| 81 | |
| 82 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
| 83 | if (opp->np == opp_np) { |
| 84 | dev_pm_opp_get(opp); |
| 85 | mutex_unlock(&opp_table->lock); |
| 86 | return opp; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | mutex_unlock(&opp_table->lock); |
| 91 | |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | static struct device_node *of_parse_required_opp(struct device_node *np, |
| 96 | int index) |
| 97 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 98 | return of_parse_phandle(np, "required-opps", index); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* The caller must call dev_pm_opp_put_opp_table() after the table is used */ |
| 102 | static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np) |
| 103 | { |
| 104 | struct opp_table *opp_table; |
| 105 | struct device_node *opp_table_np; |
| 106 | |
| 107 | lockdep_assert_held(&opp_table_lock); |
| 108 | |
| 109 | opp_table_np = of_get_parent(opp_np); |
| 110 | if (!opp_table_np) |
| 111 | goto err; |
| 112 | |
| 113 | /* It is safe to put the node now as all we need now is its address */ |
| 114 | of_node_put(opp_table_np); |
| 115 | |
| 116 | list_for_each_entry(opp_table, &opp_tables, node) { |
| 117 | if (opp_table_np == opp_table->np) { |
| 118 | _get_opp_table_kref(opp_table); |
| 119 | return opp_table; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | err: |
| 124 | return ERR_PTR(-ENODEV); |
| 125 | } |
| 126 | |
| 127 | /* Free resources previously acquired by _opp_table_alloc_required_tables() */ |
| 128 | static void _opp_table_free_required_tables(struct opp_table *opp_table) |
| 129 | { |
| 130 | struct opp_table **required_opp_tables = opp_table->required_opp_tables; |
| 131 | int i; |
| 132 | |
| 133 | if (!required_opp_tables) |
| 134 | return; |
| 135 | |
| 136 | for (i = 0; i < opp_table->required_opp_count; i++) { |
| 137 | if (IS_ERR_OR_NULL(required_opp_tables[i])) |
| 138 | break; |
| 139 | |
| 140 | dev_pm_opp_put_opp_table(required_opp_tables[i]); |
| 141 | } |
| 142 | |
| 143 | kfree(required_opp_tables); |
| 144 | |
| 145 | opp_table->required_opp_count = 0; |
| 146 | opp_table->required_opp_tables = NULL; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Populate all devices and opp tables which are part of "required-opps" list. |
| 151 | * Checking only the first OPP node should be enough. |
| 152 | */ |
| 153 | static void _opp_table_alloc_required_tables(struct opp_table *opp_table, |
| 154 | struct device *dev, |
| 155 | struct device_node *opp_np) |
| 156 | { |
| 157 | struct opp_table **required_opp_tables; |
| 158 | struct device_node *required_np, *np; |
| 159 | int count, i; |
| 160 | |
| 161 | /* Traversing the first OPP node is all we need */ |
| 162 | np = of_get_next_available_child(opp_np, NULL); |
| 163 | if (!np) { |
| 164 | dev_err(dev, "Empty OPP table\n"); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | count = of_count_phandle_with_args(np, "required-opps", NULL); |
| 169 | if (!count) |
| 170 | goto put_np; |
| 171 | |
| 172 | required_opp_tables = kcalloc(count, sizeof(*required_opp_tables), |
| 173 | GFP_KERNEL); |
| 174 | if (!required_opp_tables) |
| 175 | goto put_np; |
| 176 | |
| 177 | opp_table->required_opp_tables = required_opp_tables; |
| 178 | opp_table->required_opp_count = count; |
| 179 | |
| 180 | for (i = 0; i < count; i++) { |
| 181 | required_np = of_parse_required_opp(np, i); |
| 182 | if (!required_np) |
| 183 | goto free_required_tables; |
| 184 | |
| 185 | required_opp_tables[i] = _find_table_of_opp_np(required_np); |
| 186 | of_node_put(required_np); |
| 187 | |
| 188 | if (IS_ERR(required_opp_tables[i])) |
| 189 | goto free_required_tables; |
| 190 | |
| 191 | /* |
| 192 | * We only support genpd's OPPs in the "required-opps" for now, |
| 193 | * as we don't know how much about other cases. Error out if the |
| 194 | * required OPP doesn't belong to a genpd. |
| 195 | */ |
| 196 | if (!required_opp_tables[i]->is_genpd) { |
| 197 | dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n", |
| 198 | required_np); |
| 199 | goto free_required_tables; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | goto put_np; |
| 204 | |
| 205 | free_required_tables: |
| 206 | _opp_table_free_required_tables(opp_table); |
| 207 | put_np: |
| 208 | of_node_put(np); |
| 209 | } |
| 210 | |
| 211 | void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, |
| 212 | int index) |
| 213 | { |
| 214 | struct device_node *np, *opp_np; |
| 215 | u32 val; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Only required for backward compatibility with v1 bindings, but isn't |
| 219 | * harmful for other cases. And so we do it unconditionally. |
| 220 | */ |
| 221 | np = of_node_get(dev->of_node); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 222 | if (!np) |
| 223 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 224 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 225 | if (!of_property_read_u32(np, "clock-latency", &val)) |
| 226 | opp_table->clock_latency_ns_max = val; |
| 227 | of_property_read_u32(np, "voltage-tolerance", |
| 228 | &opp_table->voltage_tolerance_v1); |
| 229 | |
| 230 | if (of_find_property(np, "#power-domain-cells", NULL)) |
| 231 | opp_table->is_genpd = true; |
| 232 | |
| 233 | /* Get OPP table node */ |
| 234 | opp_np = _opp_of_get_opp_desc_node(np, index); |
| 235 | of_node_put(np); |
| 236 | |
| 237 | if (!opp_np) |
| 238 | return; |
| 239 | |
| 240 | if (of_property_read_bool(opp_np, "opp-shared")) |
| 241 | opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; |
| 242 | else |
| 243 | opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; |
| 244 | |
| 245 | opp_table->np = opp_np; |
| 246 | |
| 247 | _opp_table_alloc_required_tables(opp_table, dev, opp_np); |
| 248 | of_node_put(opp_np); |
| 249 | } |
| 250 | |
| 251 | void _of_clear_opp_table(struct opp_table *opp_table) |
| 252 | { |
| 253 | _opp_table_free_required_tables(opp_table); |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Release all resources previously acquired with a call to |
| 258 | * _of_opp_alloc_required_opps(). |
| 259 | */ |
| 260 | void _of_opp_free_required_opps(struct opp_table *opp_table, |
| 261 | struct dev_pm_opp *opp) |
| 262 | { |
| 263 | struct dev_pm_opp **required_opps = opp->required_opps; |
| 264 | int i; |
| 265 | |
| 266 | if (!required_opps) |
| 267 | return; |
| 268 | |
| 269 | for (i = 0; i < opp_table->required_opp_count; i++) { |
| 270 | if (!required_opps[i]) |
| 271 | break; |
| 272 | |
| 273 | /* Put the reference back */ |
| 274 | dev_pm_opp_put(required_opps[i]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 275 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 276 | |
| 277 | kfree(required_opps); |
| 278 | opp->required_opps = NULL; |
| 279 | } |
| 280 | |
| 281 | /* Populate all required OPPs which are part of "required-opps" list */ |
| 282 | static int _of_opp_alloc_required_opps(struct opp_table *opp_table, |
| 283 | struct dev_pm_opp *opp) |
| 284 | { |
| 285 | struct dev_pm_opp **required_opps; |
| 286 | struct opp_table *required_table; |
| 287 | struct device_node *np; |
| 288 | int i, ret, count = opp_table->required_opp_count; |
| 289 | |
| 290 | if (!count) |
| 291 | return 0; |
| 292 | |
| 293 | required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL); |
| 294 | if (!required_opps) |
| 295 | return -ENOMEM; |
| 296 | |
| 297 | opp->required_opps = required_opps; |
| 298 | |
| 299 | for (i = 0; i < count; i++) { |
| 300 | required_table = opp_table->required_opp_tables[i]; |
| 301 | |
| 302 | np = of_parse_required_opp(opp->np, i); |
| 303 | if (unlikely(!np)) { |
| 304 | ret = -ENODEV; |
| 305 | goto free_required_opps; |
| 306 | } |
| 307 | |
| 308 | required_opps[i] = _find_opp_of_np(required_table, np); |
| 309 | of_node_put(np); |
| 310 | |
| 311 | if (!required_opps[i]) { |
| 312 | pr_err("%s: Unable to find required OPP node: %pOF (%d)\n", |
| 313 | __func__, opp->np, i); |
| 314 | ret = -ENODEV; |
| 315 | goto free_required_opps; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | |
| 321 | free_required_opps: |
| 322 | _of_opp_free_required_opps(opp_table, opp); |
| 323 | |
| 324 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 327 | static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table) |
| 328 | { |
| 329 | struct device_node *np, *opp_np; |
| 330 | struct property *prop; |
| 331 | |
| 332 | if (!opp_table) { |
| 333 | np = of_node_get(dev->of_node); |
| 334 | if (!np) |
| 335 | return -ENODEV; |
| 336 | |
| 337 | opp_np = _opp_of_get_opp_desc_node(np, 0); |
| 338 | of_node_put(np); |
| 339 | } else { |
| 340 | opp_np = of_node_get(opp_table->np); |
| 341 | } |
| 342 | |
| 343 | /* Lets not fail in case we are parsing opp-v1 bindings */ |
| 344 | if (!opp_np) |
| 345 | return 0; |
| 346 | |
| 347 | /* Checking only first OPP is sufficient */ |
| 348 | np = of_get_next_available_child(opp_np, NULL); |
| 349 | if (!np) { |
| 350 | dev_err(dev, "OPP table empty\n"); |
| 351 | return -EINVAL; |
| 352 | } |
| 353 | of_node_put(opp_np); |
| 354 | |
| 355 | prop = of_find_property(np, "opp-peak-kBps", NULL); |
| 356 | of_node_put(np); |
| 357 | |
| 358 | if (!prop || !prop->length) |
| 359 | return 0; |
| 360 | |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | int dev_pm_opp_of_find_icc_paths(struct device *dev, |
| 365 | struct opp_table *opp_table) |
| 366 | { |
| 367 | struct device_node *np; |
| 368 | int ret, i, count, num_paths; |
| 369 | struct icc_path **paths; |
| 370 | |
| 371 | ret = _bandwidth_supported(dev, opp_table); |
| 372 | if (ret <= 0) |
| 373 | return ret; |
| 374 | |
| 375 | ret = 0; |
| 376 | |
| 377 | np = of_node_get(dev->of_node); |
| 378 | if (!np) |
| 379 | return 0; |
| 380 | |
| 381 | count = of_count_phandle_with_args(np, "interconnects", |
| 382 | "#interconnect-cells"); |
| 383 | of_node_put(np); |
| 384 | if (count < 0) |
| 385 | return 0; |
| 386 | |
| 387 | /* two phandles when #interconnect-cells = <1> */ |
| 388 | if (count % 2) { |
| 389 | dev_err(dev, "%s: Invalid interconnects values\n", __func__); |
| 390 | return -EINVAL; |
| 391 | } |
| 392 | |
| 393 | num_paths = count / 2; |
| 394 | paths = kcalloc(num_paths, sizeof(*paths), GFP_KERNEL); |
| 395 | if (!paths) |
| 396 | return -ENOMEM; |
| 397 | |
| 398 | for (i = 0; i < num_paths; i++) { |
| 399 | paths[i] = of_icc_get_by_index(dev, i); |
| 400 | if (IS_ERR(paths[i])) { |
| 401 | ret = PTR_ERR(paths[i]); |
| 402 | if (ret != -EPROBE_DEFER) { |
| 403 | dev_err(dev, "%s: Unable to get path%d: %d\n", |
| 404 | __func__, i, ret); |
| 405 | } |
| 406 | goto err; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (opp_table) { |
| 411 | opp_table->paths = paths; |
| 412 | opp_table->path_count = num_paths; |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | err: |
| 417 | while (i--) |
| 418 | icc_put(paths[i]); |
| 419 | |
| 420 | kfree(paths); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths); |
| 425 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 426 | static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table, |
| 427 | struct device_node *np) |
| 428 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 429 | unsigned int levels = opp_table->supported_hw_count; |
| 430 | int count, versions, ret, i, j; |
| 431 | u32 val; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 432 | |
| 433 | if (!opp_table->supported_hw) { |
| 434 | /* |
| 435 | * In the case that no supported_hw has been set by the |
| 436 | * platform but there is an opp-supported-hw value set for |
| 437 | * an OPP then the OPP should not be enabled as there is |
| 438 | * no way to see if the hardware supports it. |
| 439 | */ |
| 440 | if (of_find_property(np, "opp-supported-hw", NULL)) |
| 441 | return false; |
| 442 | else |
| 443 | return true; |
| 444 | } |
| 445 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 446 | count = of_property_count_u32_elems(np, "opp-supported-hw"); |
| 447 | if (count <= 0 || count % levels) { |
| 448 | dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n", |
| 449 | __func__, count); |
| 450 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 453 | versions = count / levels; |
| 454 | |
| 455 | /* All levels in at least one of the versions should match */ |
| 456 | for (i = 0; i < versions; i++) { |
| 457 | bool supported = true; |
| 458 | |
| 459 | for (j = 0; j < levels; j++) { |
| 460 | ret = of_property_read_u32_index(np, "opp-supported-hw", |
| 461 | i * levels + j, &val); |
| 462 | if (ret) { |
| 463 | dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n", |
| 464 | __func__, i * levels + j, ret); |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | /* Check if the level is supported */ |
| 469 | if (!(val & opp_table->supported_hw[j])) { |
| 470 | supported = false; |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (supported) |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, |
| 483 | struct opp_table *opp_table) |
| 484 | { |
| 485 | u32 *microvolt, *microamp = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 486 | int supplies = opp_table->regulator_count, vcount, icount, ret, i, j; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 487 | struct property *prop = NULL; |
| 488 | char name[NAME_MAX]; |
| 489 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 490 | /* Search for "opp-microvolt-<name>" */ |
| 491 | if (opp_table->prop_name) { |
| 492 | snprintf(name, sizeof(name), "opp-microvolt-%s", |
| 493 | opp_table->prop_name); |
| 494 | prop = of_find_property(opp->np, name, NULL); |
| 495 | } |
| 496 | |
| 497 | if (!prop) { |
| 498 | /* Search for "opp-microvolt" */ |
| 499 | sprintf(name, "opp-microvolt"); |
| 500 | prop = of_find_property(opp->np, name, NULL); |
| 501 | |
| 502 | /* Missing property isn't a problem, but an invalid entry is */ |
| 503 | if (!prop) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 504 | if (unlikely(supplies == -1)) { |
| 505 | /* Initialize regulator_count */ |
| 506 | opp_table->regulator_count = 0; |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | if (!supplies) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 511 | return 0; |
| 512 | |
| 513 | dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n", |
| 514 | __func__); |
| 515 | return -EINVAL; |
| 516 | } |
| 517 | } |
| 518 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 519 | if (unlikely(supplies == -1)) { |
| 520 | /* Initialize regulator_count */ |
| 521 | supplies = opp_table->regulator_count = 1; |
| 522 | } else if (unlikely(!supplies)) { |
| 523 | dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__); |
| 524 | return -EINVAL; |
| 525 | } |
| 526 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 527 | vcount = of_property_count_u32_elems(opp->np, name); |
| 528 | if (vcount < 0) { |
| 529 | dev_err(dev, "%s: Invalid %s property (%d)\n", |
| 530 | __func__, name, vcount); |
| 531 | return vcount; |
| 532 | } |
| 533 | |
| 534 | /* There can be one or three elements per supply */ |
| 535 | if (vcount != supplies && vcount != supplies * 3) { |
| 536 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", |
| 537 | __func__, name, vcount, supplies); |
| 538 | return -EINVAL; |
| 539 | } |
| 540 | |
| 541 | microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL); |
| 542 | if (!microvolt) |
| 543 | return -ENOMEM; |
| 544 | |
| 545 | ret = of_property_read_u32_array(opp->np, name, microvolt, vcount); |
| 546 | if (ret) { |
| 547 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret); |
| 548 | ret = -EINVAL; |
| 549 | goto free_microvolt; |
| 550 | } |
| 551 | |
| 552 | /* Search for "opp-microamp-<name>" */ |
| 553 | prop = NULL; |
| 554 | if (opp_table->prop_name) { |
| 555 | snprintf(name, sizeof(name), "opp-microamp-%s", |
| 556 | opp_table->prop_name); |
| 557 | prop = of_find_property(opp->np, name, NULL); |
| 558 | } |
| 559 | |
| 560 | if (!prop) { |
| 561 | /* Search for "opp-microamp" */ |
| 562 | sprintf(name, "opp-microamp"); |
| 563 | prop = of_find_property(opp->np, name, NULL); |
| 564 | } |
| 565 | |
| 566 | if (prop) { |
| 567 | icount = of_property_count_u32_elems(opp->np, name); |
| 568 | if (icount < 0) { |
| 569 | dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, |
| 570 | name, icount); |
| 571 | ret = icount; |
| 572 | goto free_microvolt; |
| 573 | } |
| 574 | |
| 575 | if (icount != supplies) { |
| 576 | dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n", |
| 577 | __func__, name, icount, supplies); |
| 578 | ret = -EINVAL; |
| 579 | goto free_microvolt; |
| 580 | } |
| 581 | |
| 582 | microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL); |
| 583 | if (!microamp) { |
| 584 | ret = -EINVAL; |
| 585 | goto free_microvolt; |
| 586 | } |
| 587 | |
| 588 | ret = of_property_read_u32_array(opp->np, name, microamp, |
| 589 | icount); |
| 590 | if (ret) { |
| 591 | dev_err(dev, "%s: error parsing %s: %d\n", __func__, |
| 592 | name, ret); |
| 593 | ret = -EINVAL; |
| 594 | goto free_microamp; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | for (i = 0, j = 0; i < supplies; i++) { |
| 599 | opp->supplies[i].u_volt = microvolt[j++]; |
| 600 | |
| 601 | if (vcount == supplies) { |
| 602 | opp->supplies[i].u_volt_min = opp->supplies[i].u_volt; |
| 603 | opp->supplies[i].u_volt_max = opp->supplies[i].u_volt; |
| 604 | } else { |
| 605 | opp->supplies[i].u_volt_min = microvolt[j++]; |
| 606 | opp->supplies[i].u_volt_max = microvolt[j++]; |
| 607 | } |
| 608 | |
| 609 | if (microamp) |
| 610 | opp->supplies[i].u_amp = microamp[i]; |
| 611 | } |
| 612 | |
| 613 | free_microamp: |
| 614 | kfree(microamp); |
| 615 | free_microvolt: |
| 616 | kfree(microvolt); |
| 617 | |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT |
| 623 | * entries |
| 624 | * @dev: device pointer used to lookup OPP table. |
| 625 | * |
| 626 | * Free OPPs created using static entries present in DT. |
| 627 | */ |
| 628 | void dev_pm_opp_of_remove_table(struct device *dev) |
| 629 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 630 | dev_pm_opp_remove_table(dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 631 | } |
| 632 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table); |
| 633 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 634 | static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *table, |
| 635 | struct device_node *np, bool peak) |
| 636 | { |
| 637 | const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps"; |
| 638 | struct property *prop; |
| 639 | int i, count, ret; |
| 640 | u32 *bw; |
| 641 | |
| 642 | prop = of_find_property(np, name, NULL); |
| 643 | if (!prop) |
| 644 | return -ENODEV; |
| 645 | |
| 646 | count = prop->length / sizeof(u32); |
| 647 | if (table->path_count != count) { |
| 648 | pr_err("%s: Mismatch between %s and paths (%d %d)\n", |
| 649 | __func__, name, count, table->path_count); |
| 650 | return -EINVAL; |
| 651 | } |
| 652 | |
| 653 | bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL); |
| 654 | if (!bw) |
| 655 | return -ENOMEM; |
| 656 | |
| 657 | ret = of_property_read_u32_array(np, name, bw, count); |
| 658 | if (ret) { |
| 659 | pr_err("%s: Error parsing %s: %d\n", __func__, name, ret); |
| 660 | goto out; |
| 661 | } |
| 662 | |
| 663 | for (i = 0; i < count; i++) { |
| 664 | if (peak) |
| 665 | new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]); |
| 666 | else |
| 667 | new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]); |
| 668 | } |
| 669 | |
| 670 | out: |
| 671 | kfree(bw); |
| 672 | return ret; |
| 673 | } |
| 674 | |
| 675 | static int _read_opp_key(struct dev_pm_opp *new_opp, struct opp_table *table, |
| 676 | struct device_node *np, bool *rate_not_available) |
| 677 | { |
| 678 | bool found = false; |
| 679 | u64 rate; |
| 680 | int ret; |
| 681 | |
| 682 | ret = of_property_read_u64(np, "opp-hz", &rate); |
| 683 | if (!ret) { |
| 684 | /* |
| 685 | * Rate is defined as an unsigned long in clk API, and so |
| 686 | * casting explicitly to its type. Must be fixed once rate is 64 |
| 687 | * bit guaranteed in clk API. |
| 688 | */ |
| 689 | new_opp->rate = (unsigned long)rate; |
| 690 | found = true; |
| 691 | } |
| 692 | *rate_not_available = !!ret; |
| 693 | |
| 694 | /* |
| 695 | * Bandwidth consists of peak and average (optional) values: |
| 696 | * opp-peak-kBps = <path1_value path2_value>; |
| 697 | * opp-avg-kBps = <path1_value path2_value>; |
| 698 | */ |
| 699 | ret = _read_bw(new_opp, table, np, true); |
| 700 | if (!ret) { |
| 701 | found = true; |
| 702 | ret = _read_bw(new_opp, table, np, false); |
| 703 | } |
| 704 | |
| 705 | /* The properties were found but we failed to parse them */ |
| 706 | if (ret && ret != -ENODEV) |
| 707 | return ret; |
| 708 | |
| 709 | if (!of_property_read_u32(np, "opp-level", &new_opp->level)) |
| 710 | found = true; |
| 711 | |
| 712 | if (found) |
| 713 | return 0; |
| 714 | |
| 715 | return ret; |
| 716 | } |
| 717 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 718 | /** |
| 719 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) |
| 720 | * @opp_table: OPP table |
| 721 | * @dev: device for which we do this operation |
| 722 | * @np: device node |
| 723 | * |
| 724 | * This function adds an opp definition to the opp table and returns status. The |
| 725 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be |
| 726 | * removed by dev_pm_opp_remove. |
| 727 | * |
| 728 | * Return: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 729 | * Valid OPP pointer: |
| 730 | * On success |
| 731 | * NULL: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 732 | * Duplicate OPPs (both freq and volt are same) and opp->available |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 733 | * OR if the OPP is not supported by hardware. |
| 734 | * ERR_PTR(-EEXIST): |
| 735 | * Freq are same and volt are different OR |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 736 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 737 | * ERR_PTR(-ENOMEM): |
| 738 | * Memory allocation failure |
| 739 | * ERR_PTR(-EINVAL): |
| 740 | * Failed parsing the OPP node |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 741 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 742 | static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table, |
| 743 | struct device *dev, struct device_node *np) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 744 | { |
| 745 | struct dev_pm_opp *new_opp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 746 | u32 val; |
| 747 | int ret; |
| 748 | bool rate_not_available = false; |
| 749 | |
| 750 | new_opp = _opp_allocate(opp_table); |
| 751 | if (!new_opp) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 752 | return ERR_PTR(-ENOMEM); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 753 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 754 | ret = _read_opp_key(new_opp, opp_table, np, &rate_not_available); |
| 755 | if (ret < 0 && !opp_table->is_genpd) { |
| 756 | dev_err(dev, "%s: opp key field not found\n", __func__); |
| 757 | goto free_opp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* Check if the OPP supports hardware's hierarchy of versions or not */ |
| 761 | if (!_opp_is_supported(dev, opp_table, np)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 762 | dev_dbg(dev, "OPP not supported by hardware: %lu\n", |
| 763 | new_opp->rate); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 764 | goto free_opp; |
| 765 | } |
| 766 | |
| 767 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); |
| 768 | |
| 769 | new_opp->np = np; |
| 770 | new_opp->dynamic = false; |
| 771 | new_opp->available = true; |
| 772 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 773 | ret = _of_opp_alloc_required_opps(opp_table, new_opp); |
| 774 | if (ret) |
| 775 | goto free_opp; |
| 776 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 777 | if (!of_property_read_u32(np, "clock-latency-ns", &val)) |
| 778 | new_opp->clock_latency_ns = val; |
| 779 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 780 | ret = opp_parse_supplies(new_opp, dev, opp_table); |
| 781 | if (ret) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 782 | goto free_required_opps; |
| 783 | |
| 784 | if (opp_table->is_genpd) |
| 785 | new_opp->pstate = pm_genpd_opp_to_performance_state(dev, new_opp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 786 | |
| 787 | ret = _opp_add(dev, new_opp, opp_table, rate_not_available); |
| 788 | if (ret) { |
| 789 | /* Don't return error for duplicate OPPs */ |
| 790 | if (ret == -EBUSY) |
| 791 | ret = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 792 | goto free_required_opps; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /* OPP to select on device suspend */ |
| 796 | if (of_property_read_bool(np, "opp-suspend")) { |
| 797 | if (opp_table->suspend_opp) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 798 | /* Pick the OPP with higher rate as suspend OPP */ |
| 799 | if (new_opp->rate > opp_table->suspend_opp->rate) { |
| 800 | opp_table->suspend_opp->suspend = false; |
| 801 | new_opp->suspend = true; |
| 802 | opp_table->suspend_opp = new_opp; |
| 803 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 804 | } else { |
| 805 | new_opp->suspend = true; |
| 806 | opp_table->suspend_opp = new_opp; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max) |
| 811 | opp_table->clock_latency_ns_max = new_opp->clock_latency_ns; |
| 812 | |
| 813 | pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", |
| 814 | __func__, new_opp->turbo, new_opp->rate, |
| 815 | new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min, |
| 816 | new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns); |
| 817 | |
| 818 | /* |
| 819 | * Notify the changes in the availability of the operable |
| 820 | * frequency/voltage list. |
| 821 | */ |
| 822 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 823 | return new_opp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 824 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 825 | free_required_opps: |
| 826 | _of_opp_free_required_opps(opp_table, new_opp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 827 | free_opp: |
| 828 | _opp_free(new_opp); |
| 829 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 830 | return ret ? ERR_PTR(ret) : NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /* Initializes OPP tables based on new bindings */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 834 | static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 835 | { |
| 836 | struct device_node *np; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 837 | int ret, count = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 838 | struct dev_pm_opp *opp; |
| 839 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 840 | /* OPP table is already initialized for the device */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 841 | mutex_lock(&opp_table->lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 842 | if (opp_table->parsed_static_opps) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 843 | opp_table->parsed_static_opps++; |
| 844 | mutex_unlock(&opp_table->lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 845 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 848 | opp_table->parsed_static_opps = 1; |
| 849 | mutex_unlock(&opp_table->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 850 | |
| 851 | /* We have opp-table node now, iterate over it and add OPPs */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 852 | for_each_available_child_of_node(opp_table->np, np) { |
| 853 | opp = _opp_add_static_v2(opp_table, dev, np); |
| 854 | if (IS_ERR(opp)) { |
| 855 | ret = PTR_ERR(opp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 856 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, |
| 857 | ret); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 858 | of_node_put(np); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 859 | goto remove_static_opp; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 860 | } else if (opp) { |
| 861 | count++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 865 | /* There should be one or more OPPs defined */ |
| 866 | if (!count) { |
| 867 | dev_err(dev, "%s: no supported OPPs", __func__); |
| 868 | ret = -ENOENT; |
| 869 | goto remove_static_opp; |
| 870 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 871 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 872 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
| 873 | /* Any non-zero performance state would enable the feature */ |
| 874 | if (opp->pstate) { |
| 875 | opp_table->genpd_performance_state = true; |
| 876 | break; |
| 877 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 878 | } |
| 879 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 880 | return 0; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 881 | |
| 882 | remove_static_opp: |
| 883 | _opp_remove_all_static(opp_table); |
| 884 | |
| 885 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* Initializes OPP tables based on old-deprecated bindings */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 889 | static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 890 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 891 | const struct property *prop; |
| 892 | const __be32 *val; |
| 893 | int nr, ret = 0; |
| 894 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 895 | mutex_lock(&opp_table->lock); |
| 896 | if (opp_table->parsed_static_opps) { |
| 897 | opp_table->parsed_static_opps++; |
| 898 | mutex_unlock(&opp_table->lock); |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | opp_table->parsed_static_opps = 1; |
| 903 | mutex_unlock(&opp_table->lock); |
| 904 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 905 | prop = of_find_property(dev->of_node, "operating-points", NULL); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 906 | if (!prop) { |
| 907 | ret = -ENODEV; |
| 908 | goto remove_static_opp; |
| 909 | } |
| 910 | if (!prop->value) { |
| 911 | ret = -ENODATA; |
| 912 | goto remove_static_opp; |
| 913 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 914 | |
| 915 | /* |
| 916 | * Each OPP is a set of tuples consisting of frequency and |
| 917 | * voltage like <freq-kHz vol-uV>. |
| 918 | */ |
| 919 | nr = prop->length / sizeof(u32); |
| 920 | if (nr % 2) { |
| 921 | dev_err(dev, "%s: Invalid OPP table\n", __func__); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 922 | ret = -EINVAL; |
| 923 | goto remove_static_opp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 926 | val = prop->value; |
| 927 | while (nr) { |
| 928 | unsigned long freq = be32_to_cpup(val++) * 1000; |
| 929 | unsigned long volt = be32_to_cpup(val++); |
| 930 | |
| 931 | ret = _opp_add_v1(opp_table, dev, freq, volt, false); |
| 932 | if (ret) { |
| 933 | dev_err(dev, "%s: Failed to add OPP %ld (%d)\n", |
| 934 | __func__, freq, ret); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 935 | goto remove_static_opp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 936 | } |
| 937 | nr -= 2; |
| 938 | } |
| 939 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 940 | return 0; |
| 941 | |
| 942 | remove_static_opp: |
| 943 | _opp_remove_all_static(opp_table); |
| 944 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 945 | return ret; |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * dev_pm_opp_of_add_table() - Initialize opp table from device tree |
| 950 | * @dev: device pointer used to lookup OPP table. |
| 951 | * |
| 952 | * Register the initial OPP table with the OPP library for given device. |
| 953 | * |
| 954 | * Return: |
| 955 | * 0 On success OR |
| 956 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 957 | * -EEXIST Freq are same and volt are different OR |
| 958 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 959 | * -ENOMEM Memory allocation failure |
| 960 | * -ENODEV when 'operating-points' property is not found or is invalid data |
| 961 | * in device node. |
| 962 | * -ENODATA when empty 'operating-points' property is found |
| 963 | * -EINVAL when invalid entries are found in opp-v2 table |
| 964 | */ |
| 965 | int dev_pm_opp_of_add_table(struct device *dev) |
| 966 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 967 | struct opp_table *opp_table; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 968 | int ret; |
| 969 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 970 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 971 | if (IS_ERR(opp_table)) |
| 972 | return PTR_ERR(opp_table); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 973 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 974 | /* |
| 975 | * OPPs have two version of bindings now. Also try the old (v1) |
| 976 | * bindings for backward compatibility with older dtbs. |
| 977 | */ |
| 978 | if (opp_table->np) |
| 979 | ret = _of_add_opp_table_v2(dev, opp_table); |
| 980 | else |
| 981 | ret = _of_add_opp_table_v1(dev, opp_table); |
| 982 | |
| 983 | if (ret) |
| 984 | dev_pm_opp_put_opp_table(opp_table); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 985 | |
| 986 | return ret; |
| 987 | } |
| 988 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table); |
| 989 | |
| 990 | /** |
| 991 | * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree |
| 992 | * @dev: device pointer used to lookup OPP table. |
| 993 | * @index: Index number. |
| 994 | * |
| 995 | * Register the initial OPP table with the OPP library for given device only |
| 996 | * using the "operating-points-v2" property. |
| 997 | * |
| 998 | * Return: |
| 999 | * 0 On success OR |
| 1000 | * Duplicate OPPs (both freq and volt are same) and opp->available |
| 1001 | * -EEXIST Freq are same and volt are different OR |
| 1002 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
| 1003 | * -ENOMEM Memory allocation failure |
| 1004 | * -ENODEV when 'operating-points' property is not found or is invalid data |
| 1005 | * in device node. |
| 1006 | * -ENODATA when empty 'operating-points' property is found |
| 1007 | * -EINVAL when invalid entries are found in opp-v2 table |
| 1008 | */ |
| 1009 | int dev_pm_opp_of_add_table_indexed(struct device *dev, int index) |
| 1010 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1011 | struct opp_table *opp_table; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1012 | int ret, count; |
| 1013 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1014 | if (index) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1015 | /* |
| 1016 | * If only one phandle is present, then the same OPP table |
| 1017 | * applies for all index requests. |
| 1018 | */ |
| 1019 | count = of_count_phandle_with_args(dev->of_node, |
| 1020 | "operating-points-v2", NULL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1021 | if (count == 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1022 | index = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1025 | opp_table = dev_pm_opp_get_opp_table_indexed(dev, index); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1026 | if (IS_ERR(opp_table)) |
| 1027 | return PTR_ERR(opp_table); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1028 | |
| 1029 | ret = _of_add_opp_table_v2(dev, opp_table); |
| 1030 | if (ret) |
| 1031 | dev_pm_opp_put_opp_table(opp_table); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1032 | |
| 1033 | return ret; |
| 1034 | } |
| 1035 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed); |
| 1036 | |
| 1037 | /* CPU device specific helpers */ |
| 1038 | |
| 1039 | /** |
| 1040 | * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask |
| 1041 | * @cpumask: cpumask for which OPP table needs to be removed |
| 1042 | * |
| 1043 | * This removes the OPP tables for CPUs present in the @cpumask. |
| 1044 | * This should be used only to remove static entries created from DT. |
| 1045 | */ |
| 1046 | void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask) |
| 1047 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1048 | _dev_pm_opp_cpumask_remove_table(cpumask, -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1049 | } |
| 1050 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table); |
| 1051 | |
| 1052 | /** |
| 1053 | * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask |
| 1054 | * @cpumask: cpumask for which OPP table needs to be added. |
| 1055 | * |
| 1056 | * This adds the OPP tables for CPUs present in the @cpumask. |
| 1057 | */ |
| 1058 | int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) |
| 1059 | { |
| 1060 | struct device *cpu_dev; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1061 | int cpu, ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1062 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1063 | if (WARN_ON(cpumask_empty(cpumask))) |
| 1064 | return -ENODEV; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1065 | |
| 1066 | for_each_cpu(cpu, cpumask) { |
| 1067 | cpu_dev = get_cpu_device(cpu); |
| 1068 | if (!cpu_dev) { |
| 1069 | pr_err("%s: failed to get cpu%d device\n", __func__, |
| 1070 | cpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1071 | ret = -ENODEV; |
| 1072 | goto remove_table; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | ret = dev_pm_opp_of_add_table(cpu_dev); |
| 1076 | if (ret) { |
| 1077 | /* |
| 1078 | * OPP may get registered dynamically, don't print error |
| 1079 | * message here. |
| 1080 | */ |
| 1081 | pr_debug("%s: couldn't find opp table for cpu:%d, %d\n", |
| 1082 | __func__, cpu, ret); |
| 1083 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1084 | goto remove_table; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1088 | return 0; |
| 1089 | |
| 1090 | remove_table: |
| 1091 | /* Free all other OPPs */ |
| 1092 | _dev_pm_opp_cpumask_remove_table(cpumask, cpu); |
| 1093 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1094 | return ret; |
| 1095 | } |
| 1096 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table); |
| 1097 | |
| 1098 | /* |
| 1099 | * Works only for OPP v2 bindings. |
| 1100 | * |
| 1101 | * Returns -ENOENT if operating-points-v2 bindings aren't supported. |
| 1102 | */ |
| 1103 | /** |
| 1104 | * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with |
| 1105 | * @cpu_dev using operating-points-v2 |
| 1106 | * bindings. |
| 1107 | * |
| 1108 | * @cpu_dev: CPU device for which we do this operation |
| 1109 | * @cpumask: cpumask to update with information of sharing CPUs |
| 1110 | * |
| 1111 | * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. |
| 1112 | * |
| 1113 | * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev. |
| 1114 | */ |
| 1115 | int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, |
| 1116 | struct cpumask *cpumask) |
| 1117 | { |
| 1118 | struct device_node *np, *tmp_np, *cpu_np; |
| 1119 | int cpu, ret = 0; |
| 1120 | |
| 1121 | /* Get OPP descriptor node */ |
| 1122 | np = dev_pm_opp_of_get_opp_desc_node(cpu_dev); |
| 1123 | if (!np) { |
| 1124 | dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__); |
| 1125 | return -ENOENT; |
| 1126 | } |
| 1127 | |
| 1128 | cpumask_set_cpu(cpu_dev->id, cpumask); |
| 1129 | |
| 1130 | /* OPPs are shared ? */ |
| 1131 | if (!of_property_read_bool(np, "opp-shared")) |
| 1132 | goto put_cpu_node; |
| 1133 | |
| 1134 | for_each_possible_cpu(cpu) { |
| 1135 | if (cpu == cpu_dev->id) |
| 1136 | continue; |
| 1137 | |
| 1138 | cpu_np = of_cpu_device_node_get(cpu); |
| 1139 | if (!cpu_np) { |
| 1140 | dev_err(cpu_dev, "%s: failed to get cpu%d node\n", |
| 1141 | __func__, cpu); |
| 1142 | ret = -ENOENT; |
| 1143 | goto put_cpu_node; |
| 1144 | } |
| 1145 | |
| 1146 | /* Get OPP descriptor node */ |
| 1147 | tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0); |
| 1148 | of_node_put(cpu_np); |
| 1149 | if (!tmp_np) { |
| 1150 | pr_err("%pOF: Couldn't find opp node\n", cpu_np); |
| 1151 | ret = -ENOENT; |
| 1152 | goto put_cpu_node; |
| 1153 | } |
| 1154 | |
| 1155 | /* CPUs are sharing opp node */ |
| 1156 | if (np == tmp_np) |
| 1157 | cpumask_set_cpu(cpu, cpumask); |
| 1158 | |
| 1159 | of_node_put(tmp_np); |
| 1160 | } |
| 1161 | |
| 1162 | put_cpu_node: |
| 1163 | of_node_put(np); |
| 1164 | return ret; |
| 1165 | } |
| 1166 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus); |
| 1167 | |
| 1168 | /** |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1169 | * of_get_required_opp_performance_state() - Search for required OPP and return its performance state. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1170 | * @np: Node that contains the "required-opps" property. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1171 | * @index: Index of the phandle to parse. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1172 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1173 | * Returns the performance state of the OPP pointed out by the "required-opps" |
| 1174 | * property at @index in @np. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1175 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1176 | * Return: Zero or positive performance state on success, otherwise negative |
| 1177 | * value on errors. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1178 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1179 | int of_get_required_opp_performance_state(struct device_node *np, int index) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1180 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1181 | struct dev_pm_opp *opp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1182 | struct device_node *required_np; |
| 1183 | struct opp_table *opp_table; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1184 | int pstate = -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1185 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1186 | required_np = of_parse_required_opp(np, index); |
| 1187 | if (!required_np) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1188 | return -ENODEV; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1189 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1190 | opp_table = _find_table_of_opp_np(required_np); |
| 1191 | if (IS_ERR(opp_table)) { |
| 1192 | pr_err("%s: Failed to find required OPP table %pOF: %ld\n", |
| 1193 | __func__, np, PTR_ERR(opp_table)); |
| 1194 | goto put_required_np; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1197 | opp = _find_opp_of_np(opp_table, required_np); |
| 1198 | if (opp) { |
| 1199 | pstate = opp->pstate; |
| 1200 | dev_pm_opp_put(opp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1203 | dev_pm_opp_put_opp_table(opp_table); |
| 1204 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1205 | put_required_np: |
| 1206 | of_node_put(required_np); |
| 1207 | |
| 1208 | return pstate; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1209 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1210 | EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1211 | |
| 1212 | /** |
| 1213 | * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp |
| 1214 | * @opp: opp for which DT node has to be returned for |
| 1215 | * |
| 1216 | * Return: DT node corresponding to the opp, else 0 on success. |
| 1217 | * |
| 1218 | * The caller needs to put the node with of_node_put() after using it. |
| 1219 | */ |
| 1220 | struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp) |
| 1221 | { |
| 1222 | if (IS_ERR_OR_NULL(opp)) { |
| 1223 | pr_err("%s: Invalid parameters\n", __func__); |
| 1224 | return NULL; |
| 1225 | } |
| 1226 | |
| 1227 | return of_node_get(opp->np); |
| 1228 | } |
| 1229 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1230 | |
| 1231 | /* |
| 1232 | * Callback function provided to the Energy Model framework upon registration. |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1233 | * This computes the power estimated by @dev at @kHz if it is the frequency |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1234 | * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise |
| 1235 | * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled |
| 1236 | * frequency and @mW to the associated power. The power is estimated as |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1237 | * P = C * V^2 * f with C being the device's capacitance and V and f |
| 1238 | * respectively the voltage and frequency of the OPP. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1239 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1240 | * Returns -EINVAL if the power calculation failed because of missing |
| 1241 | * parameters, 0 otherwise. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1242 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1243 | static int __maybe_unused _get_power(unsigned long *mW, unsigned long *kHz, |
| 1244 | struct device *dev) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1245 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1246 | struct dev_pm_opp *opp; |
| 1247 | struct device_node *np; |
| 1248 | unsigned long mV, Hz; |
| 1249 | u32 cap; |
| 1250 | u64 tmp; |
| 1251 | int ret; |
| 1252 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1253 | np = of_node_get(dev->of_node); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1254 | if (!np) |
| 1255 | return -EINVAL; |
| 1256 | |
| 1257 | ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap); |
| 1258 | of_node_put(np); |
| 1259 | if (ret) |
| 1260 | return -EINVAL; |
| 1261 | |
| 1262 | Hz = *kHz * 1000; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1263 | opp = dev_pm_opp_find_freq_ceil(dev, &Hz); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1264 | if (IS_ERR(opp)) |
| 1265 | return -EINVAL; |
| 1266 | |
| 1267 | mV = dev_pm_opp_get_voltage(opp) / 1000; |
| 1268 | dev_pm_opp_put(opp); |
| 1269 | if (!mV) |
| 1270 | return -EINVAL; |
| 1271 | |
| 1272 | tmp = (u64)cap * mV * mV * (Hz / 1000000); |
| 1273 | do_div(tmp, 1000000000); |
| 1274 | |
| 1275 | *mW = (unsigned long)tmp; |
| 1276 | *kHz = Hz / 1000; |
| 1277 | |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * dev_pm_opp_of_register_em() - Attempt to register an Energy Model |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1283 | * @dev : Device for which an Energy Model has to be registered |
| 1284 | * @cpus : CPUs for which an Energy Model has to be registered. For |
| 1285 | * other type of devices it should be set to NULL. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1286 | * |
| 1287 | * This checks whether the "dynamic-power-coefficient" devicetree property has |
| 1288 | * been specified, and tries to register an Energy Model with it if it has. |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1289 | * Having this property means the voltages are known for OPPs and the EM |
| 1290 | * might be calculated. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1291 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1292 | int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1293 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1294 | struct em_data_callback em_cb = EM_DATA_CB(_get_power); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1295 | struct device_node *np; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1296 | int ret, nr_opp; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1297 | u32 cap; |
| 1298 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1299 | if (IS_ERR_OR_NULL(dev)) { |
| 1300 | ret = -EINVAL; |
| 1301 | goto failed; |
| 1302 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1303 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1304 | nr_opp = dev_pm_opp_get_opp_count(dev); |
| 1305 | if (nr_opp <= 0) { |
| 1306 | ret = -EINVAL; |
| 1307 | goto failed; |
| 1308 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1309 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1310 | np = of_node_get(dev->of_node); |
| 1311 | if (!np) { |
| 1312 | ret = -EINVAL; |
| 1313 | goto failed; |
| 1314 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* |
| 1317 | * Register an EM only if the 'dynamic-power-coefficient' property is |
| 1318 | * set in devicetree. It is assumed the voltage values are known if that |
| 1319 | * property is set since it is useless otherwise. If voltages are not |
| 1320 | * known, just let the EM registration fail with an error to alert the |
| 1321 | * user about the inconsistent configuration. |
| 1322 | */ |
| 1323 | ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap); |
| 1324 | of_node_put(np); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1325 | if (ret || !cap) { |
| 1326 | dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n"); |
| 1327 | ret = -EINVAL; |
| 1328 | goto failed; |
| 1329 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1330 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1331 | ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus); |
| 1332 | if (ret) |
| 1333 | goto failed; |
| 1334 | |
| 1335 | return 0; |
| 1336 | |
| 1337 | failed: |
| 1338 | dev_dbg(dev, "Couldn't register Energy Model %d\n", ret); |
| 1339 | return ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1340 | } |
| 1341 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em); |