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