115-01-devfreq-add-ipq806x-fabric-scaling-driver.patch 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. From 13f075999935bb696dbab63243923179f06fa05e Mon Sep 17 00:00:00 2001
  2. From: Christian Marangi <[email protected]>
  3. Date: Thu, 16 Jun 2022 19:56:08 +0200
  4. Subject: [PATCH 3/4] devfreq: add ipq806x fabric scaling driver
  5. Add ipq806x fabric scaling driver using the devfreq passive governor.
  6. Signed-off-by: Christian Marangi <[email protected]>
  7. ---
  8. drivers/devfreq/Kconfig | 11 ++
  9. drivers/devfreq/Makefile | 1 +
  10. drivers/devfreq/ipq806x-fab-devfreq.c | 155 ++++++++++++++++++++++++++
  11. 3 files changed, 167 insertions(+)
  12. create mode 100644 drivers/devfreq/ipq806x-fab-devfreq.c
  13. --- a/drivers/devfreq/Kconfig
  14. +++ b/drivers/devfreq/Kconfig
  15. @@ -162,6 +162,17 @@ config ARM_KRAIT_CACHE_DEVFREQ
  16. based on the max frequency across all core and the range set in the device
  17. dts. If provided this scale also the regulator attached to the l2 cache.
  18. +config ARM_IPQ806X_FAB_DEVFREQ
  19. + tristate "Scaling support for ipq806x Soc Fabric"
  20. + depends on ARCH_QCOM || COMPILE_TEST
  21. + select DEVFREQ_GOV_PASSIVE
  22. + help
  23. + This adds the DEVFREQ driver for the ipq806x Soc Fabric.
  24. +
  25. + The driver register with the cpufreq notifier and find the right frequency
  26. + based on the max frequency across all core and the range set in the device
  27. + dts.
  28. +
  29. source "drivers/devfreq/event/Kconfig"
  30. endif # PM_DEVFREQ
  31. --- a/drivers/devfreq/Makefile
  32. +++ b/drivers/devfreq/Makefile
  33. @@ -16,6 +16,7 @@ obj-$(CONFIG_ARM_RK3399_DMC_DEVFREQ) +=
  34. obj-$(CONFIG_ARM_SUN8I_A33_MBUS_DEVFREQ) += sun8i-a33-mbus.o
  35. obj-$(CONFIG_ARM_TEGRA_DEVFREQ) += tegra30-devfreq.o
  36. obj-$(CONFIG_ARM_KRAIT_CACHE_DEVFREQ) += krait-cache-devfreq.o
  37. +obj-$(CONFIG_ARM_IPQ806X_FAB_DEVFREQ) += ipq806x-fab-devfreq.o
  38. # DEVFREQ Event Drivers
  39. obj-$(CONFIG_PM_DEVFREQ_EVENT) += event/
  40. --- /dev/null
  41. +++ b/drivers/devfreq/ipq806x-fab-devfreq.c
  42. @@ -0,0 +1,155 @@
  43. +// SPDX-License-Identifier: GPL-2.0
  44. +
  45. +#include <linux/kernel.h>
  46. +#include <linux/init.h>
  47. +#include <linux/module.h>
  48. +#include <linux/cpufreq.h>
  49. +#include <linux/devfreq.h>
  50. +#include <linux/of.h>
  51. +#include <linux/platform_device.h>
  52. +#include <linux/clk.h>
  53. +#include <linux/slab.h>
  54. +#include <linux/pm_opp.h>
  55. +
  56. +#include "governor.h"
  57. +
  58. +struct ipq806x_fab_data {
  59. + struct clk *fab_clk;
  60. + struct clk *ddr_clk;
  61. +};
  62. +
  63. +static int ipq806x_fab_get_cur_freq(struct device *dev, unsigned long *freq)
  64. +{
  65. + struct ipq806x_fab_data *data = dev_get_drvdata(dev);
  66. +
  67. + *freq = clk_get_rate(data->fab_clk);
  68. +
  69. + return 0;
  70. +};
  71. +
  72. +static int ipq806x_fab_target(struct device *dev, unsigned long *freq,
  73. + u32 flags)
  74. +{
  75. + struct ipq806x_fab_data *data = dev_get_drvdata(dev);
  76. + struct dev_pm_opp *opp;
  77. + int ret;
  78. +
  79. + opp = dev_pm_opp_find_freq_ceil(dev, freq);
  80. + if (unlikely(IS_ERR(opp)))
  81. + return PTR_ERR(opp);
  82. +
  83. + dev_pm_opp_put(opp);
  84. +
  85. + ret = clk_set_rate(data->fab_clk, *freq);
  86. + if (ret)
  87. + return ret;
  88. +
  89. + return clk_set_rate(data->ddr_clk, *freq);
  90. +};
  91. +
  92. +static int ipq806x_fab_get_dev_status(struct device *dev,
  93. + struct devfreq_dev_status *stat)
  94. +{
  95. + struct ipq806x_fab_data *data = dev_get_drvdata(dev);
  96. +
  97. + stat->busy_time = 0;
  98. + stat->total_time = 0;
  99. + stat->current_frequency = clk_get_rate(data->fab_clk);
  100. +
  101. + return 0;
  102. +};
  103. +
  104. +static struct devfreq_dev_profile ipq806x_fab_devfreq_profile = {
  105. + .target = ipq806x_fab_target,
  106. + .get_dev_status = ipq806x_fab_get_dev_status,
  107. + .get_cur_freq = ipq806x_fab_get_cur_freq
  108. +};
  109. +
  110. +static struct devfreq_passive_data devfreq_gov_data = {
  111. + .parent_type = CPUFREQ_PARENT_DEV,
  112. +};
  113. +
  114. +static int ipq806x_fab_probe(struct platform_device *pdev)
  115. +{
  116. + struct device *dev = &pdev->dev;
  117. + struct ipq806x_fab_data *data;
  118. + struct devfreq *devfreq;
  119. + struct clk *clk;
  120. + int ret;
  121. +
  122. + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  123. + if (!data)
  124. + return -ENOMEM;
  125. +
  126. + clk = devm_clk_get(dev, "apps-fab-clk");
  127. + if (IS_ERR(clk)) {
  128. + dev_err_probe(dev, PTR_ERR(clk), "failed to get apps fab clk\n");
  129. + return PTR_ERR(clk);
  130. + }
  131. +
  132. + clk_prepare_enable(clk);
  133. + data->fab_clk = clk;
  134. +
  135. + clk = devm_clk_get(dev, "ddr-fab-clk");
  136. + if (IS_ERR(clk)) {
  137. + dev_err_probe(dev, PTR_ERR(clk), "failed to get ddr fab clk\n");
  138. + goto err_ddr;
  139. + }
  140. +
  141. + clk_prepare_enable(clk);
  142. + data->ddr_clk = clk;
  143. +
  144. + ret = dev_pm_opp_of_add_table(dev);
  145. + if (ret) {
  146. + dev_err(dev, "failed to parse fab freq thresholds\n");
  147. + return ret;
  148. + }
  149. +
  150. + dev_set_drvdata(dev, data);
  151. +
  152. + devfreq = devm_devfreq_add_device(&pdev->dev, &ipq806x_fab_devfreq_profile,
  153. + DEVFREQ_GOV_PASSIVE, &devfreq_gov_data);
  154. + if (IS_ERR(devfreq))
  155. + dev_pm_opp_remove_table(dev);
  156. +
  157. + return PTR_ERR_OR_ZERO(devfreq);
  158. +
  159. +err_ddr:
  160. + clk_unprepare(data->fab_clk);
  161. + clk_put(data->fab_clk);
  162. + return PTR_ERR(clk);
  163. +};
  164. +
  165. +static int ipq806x_fab_remove(struct platform_device *pdev)
  166. +{
  167. + struct ipq806x_fab_data *data = dev_get_drvdata(&pdev->dev);
  168. +
  169. + clk_unprepare(data->fab_clk);
  170. + clk_put(data->fab_clk);
  171. +
  172. + clk_unprepare(data->ddr_clk);
  173. + clk_put(data->ddr_clk);
  174. +
  175. + dev_pm_opp_remove_table(&pdev->dev);
  176. +
  177. + return 0;
  178. +};
  179. +
  180. +static const struct of_device_id ipq806x_fab_match_table[] = {
  181. + { .compatible = "qcom,fab-scaling" },
  182. + {}
  183. +};
  184. +
  185. +static struct platform_driver ipq806x_fab_driver = {
  186. + .probe = ipq806x_fab_probe,
  187. + .remove = ipq806x_fab_remove,
  188. + .driver = {
  189. + .name = "ipq806x-fab-scaling",
  190. + .of_match_table = ipq806x_fab_match_table,
  191. + },
  192. +};
  193. +module_platform_driver(ipq806x_fab_driver);
  194. +
  195. +MODULE_DESCRIPTION("ipq806x Fab Scaling driver");
  196. +MODULE_AUTHOR("Christian Marangi <[email protected]>");
  197. +MODULE_LICENSE("GPL v2");