common.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #define dev_fmt(fmt) "tegra-soc: " fmt
  6. #include <linux/clk.h>
  7. #include <linux/device.h>
  8. #include <linux/export.h>
  9. #include <linux/of.h>
  10. #include <linux/pm_opp.h>
  11. #include <linux/pm_runtime.h>
  12. #include <soc/tegra/common.h>
  13. #include <soc/tegra/fuse.h>
  14. static const struct of_device_id tegra_machine_match[] = {
  15. { .compatible = "nvidia,tegra20", },
  16. { .compatible = "nvidia,tegra30", },
  17. { .compatible = "nvidia,tegra114", },
  18. { .compatible = "nvidia,tegra124", },
  19. { .compatible = "nvidia,tegra132", },
  20. { .compatible = "nvidia,tegra210", },
  21. { }
  22. };
  23. bool soc_is_tegra(void)
  24. {
  25. const struct of_device_id *match;
  26. struct device_node *root;
  27. root = of_find_node_by_path("/");
  28. if (!root)
  29. return false;
  30. match = of_match_node(tegra_machine_match, root);
  31. of_node_put(root);
  32. return match != NULL;
  33. }
  34. static int tegra_core_dev_init_opp_state(struct device *dev)
  35. {
  36. unsigned long rate;
  37. struct clk *clk;
  38. bool rpm_enabled;
  39. int err;
  40. clk = devm_clk_get(dev, NULL);
  41. if (IS_ERR(clk)) {
  42. dev_err(dev, "failed to get clk: %pe\n", clk);
  43. return PTR_ERR(clk);
  44. }
  45. rate = clk_get_rate(clk);
  46. if (!rate) {
  47. dev_err(dev, "failed to get clk rate\n");
  48. return -EINVAL;
  49. }
  50. /*
  51. * Runtime PM of the device must be enabled in order to set up
  52. * GENPD's performance properly because GENPD core checks whether
  53. * device is suspended and this check doesn't work while RPM is
  54. * disabled. This makes sure the OPP vote below gets cached in
  55. * GENPD for the device. Instead, the vote is done the next time
  56. * the device gets runtime resumed.
  57. */
  58. rpm_enabled = pm_runtime_enabled(dev);
  59. if (!rpm_enabled)
  60. pm_runtime_enable(dev);
  61. /* should never happen in practice */
  62. if (!pm_runtime_enabled(dev)) {
  63. dev_WARN(dev, "failed to enable runtime PM\n");
  64. pm_runtime_disable(dev);
  65. return -EINVAL;
  66. }
  67. /* first dummy rate-setting initializes voltage vote */
  68. err = dev_pm_opp_set_rate(dev, rate);
  69. if (!rpm_enabled)
  70. pm_runtime_disable(dev);
  71. if (err) {
  72. dev_err(dev, "failed to initialize OPP clock: %d\n", err);
  73. return err;
  74. }
  75. return 0;
  76. }
  77. /**
  78. * devm_tegra_core_dev_init_opp_table() - initialize OPP table
  79. * @dev: device for which OPP table is initialized
  80. * @params: pointer to the OPP table configuration
  81. *
  82. * This function will initialize OPP table and sync OPP state of a Tegra SoC
  83. * core device.
  84. *
  85. * Return: 0 on success or errorno.
  86. */
  87. int devm_tegra_core_dev_init_opp_table(struct device *dev,
  88. struct tegra_core_opp_params *params)
  89. {
  90. u32 hw_version;
  91. int err;
  92. /*
  93. * The clk's connection id to set is NULL and this is a NULL terminated
  94. * array, hence two NULL entries.
  95. */
  96. const char *clk_names[] = { NULL, NULL };
  97. struct dev_pm_opp_config config = {
  98. /*
  99. * For some devices we don't have any OPP table in the DT, and
  100. * in order to use the same code path for all the devices, we
  101. * create a dummy OPP table for them via this. The dummy OPP
  102. * table is only capable of doing clk_set_rate() on invocation
  103. * of dev_pm_opp_set_rate() and doesn't provide any other
  104. * functionality.
  105. */
  106. .clk_names = clk_names,
  107. };
  108. if (of_machine_is_compatible("nvidia,tegra20")) {
  109. hw_version = BIT(tegra_sku_info.soc_process_id);
  110. config.supported_hw = &hw_version;
  111. config.supported_hw_count = 1;
  112. } else if (of_machine_is_compatible("nvidia,tegra30")) {
  113. hw_version = BIT(tegra_sku_info.soc_speedo_id);
  114. config.supported_hw = &hw_version;
  115. config.supported_hw_count = 1;
  116. }
  117. err = devm_pm_opp_set_config(dev, &config);
  118. if (err) {
  119. dev_err(dev, "failed to set OPP config: %d\n", err);
  120. return err;
  121. }
  122. /*
  123. * Tegra114+ doesn't support OPP yet, return early for non tegra20/30
  124. * case.
  125. */
  126. if (!config.supported_hw)
  127. return -ENODEV;
  128. /*
  129. * Older device-trees have an empty OPP table, we will get
  130. * -ENODEV from devm_pm_opp_of_add_table() in this case.
  131. */
  132. err = devm_pm_opp_of_add_table(dev);
  133. if (err) {
  134. if (err != -ENODEV)
  135. dev_err(dev, "failed to add OPP table: %d\n", err);
  136. return err;
  137. }
  138. if (params->init_state) {
  139. err = tegra_core_dev_init_opp_state(dev);
  140. if (err)
  141. return err;
  142. }
  143. return 0;
  144. }
  145. EXPORT_SYMBOL_GPL(devm_tegra_core_dev_init_opp_table);