030-18-v6.14-clk-rockchip-implement-linked-gate-clock-support.patch 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. From c62fa612cfa66ab58ab215e5afc95c43c613b513 Mon Sep 17 00:00:00 2001
  2. From: Sebastian Reichel <[email protected]>
  3. Date: Wed, 11 Dec 2024 17:58:53 +0100
  4. Subject: [PATCH] clk: rockchip: implement linked gate clock support
  5. Recent Rockchip SoCs have a new hardware block called Native Interface
  6. Unit (NIU), which gates clocks to devices behind them. These clock
  7. gates will only have a running output clock when all of the following
  8. conditions are met:
  9. 1. the parent clock is enabled
  10. 2. the enable bit is set correctly
  11. 3. the linked clock is enabled
  12. To handle them this code registers them as a normal gate type clock,
  13. which takes care of condition 1 + 2. The linked clock is handled by
  14. using runtime PM clocks. Handling it via runtime PM requires setting
  15. up a struct device for each of these clocks with a driver attached
  16. to use the correct runtime PM operations. Thus the complete handling
  17. of these clocks has been moved into its own driver.
  18. Signed-off-by: Sebastian Reichel <[email protected]>
  19. Link: https://lore.kernel.org/r/[email protected]
  20. Signed-off-by: Heiko Stuebner <[email protected]>
  21. ---
  22. drivers/clk/rockchip/Makefile | 1 +
  23. drivers/clk/rockchip/clk-rk3588.c | 23 +--------
  24. drivers/clk/rockchip/clk.c | 52 +++++++++++++++++++
  25. drivers/clk/rockchip/clk.h | 25 +++++++++
  26. drivers/clk/rockchip/gate-link.c | 85 +++++++++++++++++++++++++++++++
  27. 5 files changed, 165 insertions(+), 21 deletions(-)
  28. create mode 100644 drivers/clk/rockchip/gate-link.c
  29. --- a/drivers/clk/rockchip/Makefile
  30. +++ b/drivers/clk/rockchip/Makefile
  31. @@ -13,6 +13,7 @@ clk-rockchip-y += clk-inverter.o
  32. clk-rockchip-y += clk-mmc-phase.o
  33. clk-rockchip-y += clk-muxgrf.o
  34. clk-rockchip-y += clk-ddr.o
  35. +clk-rockchip-y += gate-link.o
  36. clk-rockchip-$(CONFIG_RESET_CONTROLLER) += softrst.o
  37. obj-$(CONFIG_CLK_PX30) += clk-px30.o
  38. --- a/drivers/clk/rockchip/clk-rk3588.c
  39. +++ b/drivers/clk/rockchip/clk-rk3588.c
  40. @@ -12,25 +12,6 @@
  41. #include <dt-bindings/clock/rockchip,rk3588-cru.h>
  42. #include "clk.h"
  43. -/*
  44. - * Recent Rockchip SoCs have a new hardware block called Native Interface
  45. - * Unit (NIU), which gates clocks to devices behind them. These effectively
  46. - * need two parent clocks.
  47. - *
  48. - * Downstream enables the linked clock via runtime PM whenever the gate is
  49. - * enabled. This implementation uses separate clock nodes for each of the
  50. - * linked gate clocks, which leaks parts of the clock tree into DT.
  51. - *
  52. - * The GATE_LINK macro instead takes the second parent via 'linkname', but
  53. - * ignores the information. Once the clock framework is ready to handle it, the
  54. - * information should be passed on here. But since these clocks are required to
  55. - * access multiple relevant IP blocks, such as PCIe or USB, we mark all linked
  56. - * clocks critical until a better solution is available. This will waste some
  57. - * power, but avoids leaking implementation details into DT or hanging the
  58. - * system.
  59. - */
  60. -#define GATE_LINK(_id, cname, pname, linkedclk, f, o, b, gf) \
  61. - GATE(_id, cname, pname, f, o, b, gf)
  62. #define RK3588_LINKED_CLK CLK_IS_CRITICAL
  63. @@ -2513,8 +2494,8 @@ static int clk_rk3588_probe(struct platf
  64. struct device *dev = &pdev->dev;
  65. struct device_node *np = dev->of_node;
  66. - rockchip_clk_register_branches(ctx, rk3588_clk_branches,
  67. - ARRAY_SIZE(rk3588_clk_branches));
  68. + rockchip_clk_register_late_branches(dev, ctx, rk3588_clk_branches,
  69. + ARRAY_SIZE(rk3588_clk_branches));
  70. rockchip_clk_finalize(ctx);
  71. --- a/drivers/clk/rockchip/clk.c
  72. +++ b/drivers/clk/rockchip/clk.c
  73. @@ -19,6 +19,7 @@
  74. #include <linux/clk-provider.h>
  75. #include <linux/io.h>
  76. #include <linux/mfd/syscon.h>
  77. +#include <linux/platform_device.h>
  78. #include <linux/regmap.h>
  79. #include <linux/reboot.h>
  80. @@ -468,6 +469,29 @@ unsigned long rockchip_clk_find_max_clk_
  81. }
  82. EXPORT_SYMBOL_GPL(rockchip_clk_find_max_clk_id);
  83. +static struct platform_device *rockchip_clk_register_gate_link(
  84. + struct device *parent_dev,
  85. + struct rockchip_clk_provider *ctx,
  86. + struct rockchip_clk_branch *clkbr)
  87. +{
  88. + struct rockchip_gate_link_platdata gate_link_pdata = {
  89. + .ctx = ctx,
  90. + .clkbr = clkbr,
  91. + };
  92. +
  93. + struct platform_device_info pdevinfo = {
  94. + .parent = parent_dev,
  95. + .name = "rockchip-gate-link-clk",
  96. + .id = clkbr->id,
  97. + .fwnode = dev_fwnode(parent_dev),
  98. + .of_node_reused = true,
  99. + .data = &gate_link_pdata,
  100. + .size_data = sizeof(gate_link_pdata),
  101. + };
  102. +
  103. + return platform_device_register_full(&pdevinfo);
  104. +}
  105. +
  106. void rockchip_clk_register_branches(struct rockchip_clk_provider *ctx,
  107. struct rockchip_clk_branch *list,
  108. unsigned int nr_clk)
  109. @@ -593,6 +617,9 @@ void rockchip_clk_register_branches(stru
  110. list->div_width, list->div_flags,
  111. ctx->reg_base, &ctx->lock);
  112. break;
  113. + case branch_linked_gate:
  114. + /* must be registered late, fall-through for error message */
  115. + break;
  116. }
  117. /* none of the cases above matched */
  118. @@ -613,6 +640,31 @@ void rockchip_clk_register_branches(stru
  119. }
  120. EXPORT_SYMBOL_GPL(rockchip_clk_register_branches);
  121. +void rockchip_clk_register_late_branches(struct device *dev,
  122. + struct rockchip_clk_provider *ctx,
  123. + struct rockchip_clk_branch *list,
  124. + unsigned int nr_clk)
  125. +{
  126. + unsigned int idx;
  127. +
  128. + for (idx = 0; idx < nr_clk; idx++, list++) {
  129. + struct platform_device *pdev = NULL;
  130. +
  131. + switch (list->branch_type) {
  132. + case branch_linked_gate:
  133. + pdev = rockchip_clk_register_gate_link(dev, ctx, list);
  134. + break;
  135. + default:
  136. + dev_err(dev, "unknown clock type %d\n", list->branch_type);
  137. + break;
  138. + }
  139. +
  140. + if (!pdev)
  141. + dev_err(dev, "failed to register device for clock %s\n", list->name);
  142. + }
  143. +}
  144. +EXPORT_SYMBOL_GPL(rockchip_clk_register_late_branches);
  145. +
  146. void rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx,
  147. unsigned int lookup_id,
  148. const char *name, const char *const *parent_names,
  149. --- a/drivers/clk/rockchip/clk.h
  150. +++ b/drivers/clk/rockchip/clk.h
  151. @@ -518,6 +518,7 @@ enum rockchip_clk_branch_type {
  152. branch_divider,
  153. branch_fraction_divider,
  154. branch_gate,
  155. + branch_linked_gate,
  156. branch_mmc,
  157. branch_inverter,
  158. branch_factor,
  159. @@ -545,6 +546,7 @@ struct rockchip_clk_branch {
  160. int gate_offset;
  161. u8 gate_shift;
  162. u8 gate_flags;
  163. + unsigned int linked_clk_id;
  164. struct rockchip_clk_branch *child;
  165. };
  166. @@ -843,6 +845,20 @@ struct rockchip_clk_branch {
  167. .gate_flags = gf, \
  168. }
  169. +#define GATE_LINK(_id, cname, pname, linkedclk, f, o, b, gf) \
  170. + { \
  171. + .id = _id, \
  172. + .branch_type = branch_linked_gate, \
  173. + .name = cname, \
  174. + .parent_names = (const char *[]){ pname }, \
  175. + .linked_clk_id = linkedclk, \
  176. + .num_parents = 1, \
  177. + .flags = f, \
  178. + .gate_offset = o, \
  179. + .gate_shift = b, \
  180. + .gate_flags = gf, \
  181. + }
  182. +
  183. #define MMC(_id, cname, pname, offset, shift) \
  184. { \
  185. .id = _id, \
  186. @@ -982,6 +998,11 @@ static inline void rockchip_clk_set_look
  187. ctx->clk_data.clks[id] = clk;
  188. }
  189. +struct rockchip_gate_link_platdata {
  190. + struct rockchip_clk_provider *ctx;
  191. + struct rockchip_clk_branch *clkbr;
  192. +};
  193. +
  194. struct rockchip_clk_provider *rockchip_clk_init(struct device_node *np,
  195. void __iomem *base, unsigned long nr_clks);
  196. struct rockchip_clk_provider *rockchip_clk_init_early(struct device_node *np,
  197. @@ -994,6 +1015,10 @@ unsigned long rockchip_clk_find_max_clk_
  198. void rockchip_clk_register_branches(struct rockchip_clk_provider *ctx,
  199. struct rockchip_clk_branch *list,
  200. unsigned int nr_clk);
  201. +void rockchip_clk_register_late_branches(struct device *dev,
  202. + struct rockchip_clk_provider *ctx,
  203. + struct rockchip_clk_branch *list,
  204. + unsigned int nr_clk);
  205. void rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
  206. struct rockchip_pll_clock *pll_list,
  207. unsigned int nr_pll, int grf_lock_offset);
  208. --- /dev/null
  209. +++ b/drivers/clk/rockchip/gate-link.c
  210. @@ -0,0 +1,85 @@
  211. +// SPDX-License-Identifier: GPL-2.0-or-later
  212. +/*
  213. + * Copyright (c) 2024 Collabora Ltd.
  214. + * Author: Sebastian Reichel <[email protected]>
  215. + */
  216. +
  217. +#include <linux/clk.h>
  218. +#include <linux/platform_device.h>
  219. +#include <linux/pm_clock.h>
  220. +#include <linux/pm_runtime.h>
  221. +#include <linux/property.h>
  222. +#include "clk.h"
  223. +
  224. +static int rk_clk_gate_link_register(struct device *dev,
  225. + struct rockchip_clk_provider *ctx,
  226. + struct rockchip_clk_branch *clkbr)
  227. +{
  228. + unsigned long flags = clkbr->flags | CLK_SET_RATE_PARENT;
  229. + struct clk *clk;
  230. +
  231. + clk = clk_register_gate(dev, clkbr->name, clkbr->parent_names[0],
  232. + flags, ctx->reg_base + clkbr->gate_offset,
  233. + clkbr->gate_shift, clkbr->gate_flags,
  234. + &ctx->lock);
  235. +
  236. + if (IS_ERR(clk))
  237. + return PTR_ERR(clk);
  238. +
  239. + rockchip_clk_set_lookup(ctx, clk, clkbr->id);
  240. + return 0;
  241. +}
  242. +
  243. +static int rk_clk_gate_link_probe(struct platform_device *pdev)
  244. +{
  245. + struct rockchip_gate_link_platdata *pdata;
  246. + struct device *dev = &pdev->dev;
  247. + struct clk *linked_clk;
  248. + int ret;
  249. +
  250. + pdata = dev_get_platdata(dev);
  251. + if (!pdata)
  252. + return dev_err_probe(dev, -ENODEV, "missing platform data");
  253. +
  254. + ret = devm_pm_runtime_enable(dev);
  255. + if (ret)
  256. + return ret;
  257. +
  258. + ret = devm_pm_clk_create(dev);
  259. + if (ret)
  260. + return ret;
  261. +
  262. + linked_clk = rockchip_clk_get_lookup(pdata->ctx, pdata->clkbr->linked_clk_id);
  263. + ret = pm_clk_add_clk(dev, linked_clk);
  264. + if (ret)
  265. + return ret;
  266. +
  267. + ret = rk_clk_gate_link_register(dev, pdata->ctx, pdata->clkbr);
  268. + if (ret)
  269. + goto err;
  270. +
  271. + return 0;
  272. +
  273. +err:
  274. + pm_clk_remove_clk(dev, linked_clk);
  275. + return ret;
  276. +}
  277. +
  278. +static const struct dev_pm_ops rk_clk_gate_link_pm_ops = {
  279. + SET_RUNTIME_PM_OPS(pm_clk_suspend, pm_clk_resume, NULL)
  280. +};
  281. +
  282. +static struct platform_driver rk_clk_gate_link_driver = {
  283. + .probe = rk_clk_gate_link_probe,
  284. + .driver = {
  285. + .name = "rockchip-gate-link-clk",
  286. + .pm = &rk_clk_gate_link_pm_ops,
  287. + .suppress_bind_attrs = true,
  288. + },
  289. +};
  290. +
  291. +static int __init rk_clk_gate_link_drv_register(void)
  292. +{
  293. + return platform_driver_register(&rk_clk_gate_link_driver);
  294. +}
  295. +core_initcall(rk_clk_gate_link_drv_register);