Recent Rockchip SoCs have a new hardware block called Native Interface
Unit (NIU), which gates clocks to devices behind them. These clock
gates will only have a running output clock when all of the following
conditions are met:

1. the parent clock is enabled
2. the enable bit is set correctly
3. the linked clock is enabled

To handle them this code registers them as a normal gate type clock,
which takes care of condition 1 + 2. Thus the complete handling
of these clocks has been moved into its own driver.

Signed-off-by: Alexander Shiyan <eagle.alexander...@gmail.com>
---
 drivers/clk/rockchip/Makefile    |  1 +
 drivers/clk/rockchip/clk.c       | 67 ++++++++++++++++++++++++++------
 drivers/clk/rockchip/clk.h       | 25 +++++++++++-
 drivers/clk/rockchip/gate-link.c | 44 +++++++++++++++++++++
 4 files changed, 123 insertions(+), 14 deletions(-)
 create mode 100644 drivers/clk/rockchip/gate-link.c

diff --git a/drivers/clk/rockchip/Makefile b/drivers/clk/rockchip/Makefile
index 8d752d11b9..b1ab402003 100644
--- a/drivers/clk/rockchip/Makefile
+++ b/drivers/clk/rockchip/Makefile
@@ -12,6 +12,7 @@ clk-rockchip-y += clk-inverter.o
 clk-rockchip-y += clk-mmc-phase.o
 clk-rockchip-y += clk-muxgrf.o
 clk-rockchip-y += clk-ddr.o
+clk-rockchip-y += gate-link.o
 
 clk-rockchip-$(CONFIG_RESET_CONTROLLER) += softrst.o
 
diff --git a/drivers/clk/rockchip/clk.c b/drivers/clk/rockchip/clk.c
index 127b79e2c8..b162ca9798 100644
--- a/drivers/clk/rockchip/clk.c
+++ b/drivers/clk/rockchip/clk.c
@@ -240,7 +240,7 @@ static struct clk *rockchip_clk_register_frac_branch(
                        return mux_clk;
                }
 
-               rockchip_clk_add_lookup(ctx, mux_clk, child->id);
+               rockchip_clk_set_lookup(ctx, mux_clk, child->id);
 
                /* notifier on the fraction divider to catch rate changes */
                if (frac->mux_frac_idx >= 0) {
@@ -350,14 +350,6 @@ void rockchip_clk_of_add_provider(struct device_node *np,
 }
 EXPORT_SYMBOL_GPL(rockchip_clk_of_add_provider);
 
-void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx,
-                            struct clk *clk, unsigned int id)
-{
-       if (ctx->clk_data.clks && id)
-               ctx->clk_data.clks[id] = clk;
-}
-EXPORT_SYMBOL_GPL(rockchip_clk_add_lookup);
-
 void rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
                                struct rockchip_pll_clock *list,
                                unsigned int nr_pll, int grf_lock_offset)
@@ -378,7 +370,7 @@ void rockchip_clk_register_plls(struct 
rockchip_clk_provider *ctx,
                        continue;
                }
 
-               rockchip_clk_add_lookup(ctx, clk, list->id);
+               rockchip_clk_set_lookup(ctx, clk, list->id);
        }
 }
 EXPORT_SYMBOL_GPL(rockchip_clk_register_plls);
@@ -400,6 +392,29 @@ unsigned long rockchip_clk_find_max_clk_id(struct 
rockchip_clk_branch *list,
 }
 EXPORT_SYMBOL_GPL(rockchip_clk_find_max_clk_id);
 
+static struct device *rockchip_clk_register_gate_link(
+               struct device *parent_dev,
+               struct rockchip_clk_provider *ctx,
+               struct rockchip_clk_branch *clkbr)
+{
+       struct rockchip_gate_link_platdata gate_link_pdata = {
+               .ctx = ctx,
+               .clkbr = clkbr,
+       };
+       struct device *dev;
+       int ret;
+
+       dev = device_alloc("rockchip-gate-link-clk", clkbr->id);
+       dev->parent = parent_dev;
+       dev->platform_data = &gate_link_pdata;
+
+       ret = platform_device_register(dev);
+       if (ret)
+               return ERR_PTR(ret);
+
+       return dev;
+}
+
 void rockchip_clk_register_branches(struct rockchip_clk_provider *ctx,
                                    struct rockchip_clk_branch *list,
                                    unsigned int nr_clk)
@@ -505,6 +520,9 @@ void rockchip_clk_register_branches(struct 
rockchip_clk_provider *ctx,
                                list->div_width, list->div_flags,
                                ctx->reg_base, &ctx->lock);
                        break;
+               case branch_linked_gate:
+                       /* must be registered late, fall-through for error 
message */
+                       break;
                }
 
                /* none of the cases above matched */
@@ -520,11 +538,36 @@ void rockchip_clk_register_branches(struct 
rockchip_clk_provider *ctx,
                        continue;
                }
 
-               rockchip_clk_add_lookup(ctx, clk, list->id);
+               rockchip_clk_set_lookup(ctx, clk, list->id);
        }
 }
 EXPORT_SYMBOL_GPL(rockchip_clk_register_branches);
 
+void rockchip_clk_register_late_branches(struct device *dev,
+                                        struct rockchip_clk_provider *ctx,
+                                        struct rockchip_clk_branch *list,
+                                        unsigned int nr_clk)
+{
+       unsigned int idx;
+
+       for (idx = 0; idx < nr_clk; idx++, list++) {
+               struct device *pdev = NULL;
+
+               switch (list->branch_type) {
+               case branch_linked_gate:
+                       pdev = rockchip_clk_register_gate_link(dev, ctx, list);
+                       break;
+               default:
+                       dev_err(dev, "unknown clock type %d\n", 
list->branch_type);
+                       break;
+               }
+
+               if (!pdev)
+                       dev_err(dev, "failed to register device for clock 
%s\n", list->name);
+       }
+}
+EXPORT_SYMBOL_GPL(rockchip_clk_register_late_branches);
+
 void rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx,
                                  unsigned int lookup_id,
                                  const char *name, const char *const 
*parent_names,
@@ -544,7 +587,7 @@ void rockchip_clk_register_armclk(struct 
rockchip_clk_provider *ctx,
                return;
        }
 
-       rockchip_clk_add_lookup(ctx, clk, lookup_id);
+       rockchip_clk_set_lookup(ctx, clk, lookup_id);
 }
 EXPORT_SYMBOL_GPL(rockchip_clk_register_armclk);
 
diff --git a/drivers/clk/rockchip/clk.h b/drivers/clk/rockchip/clk.h
index 6665f8ac90..d201e753ef 100644
--- a/drivers/clk/rockchip/clk.h
+++ b/drivers/clk/rockchip/clk.h
@@ -500,6 +500,7 @@ enum rockchip_clk_branch_type {
        branch_divider,
        branch_fraction_divider,
        branch_gate,
+       branch_linked_gate,
        branch_mmc,
        branch_inverter,
        branch_factor,
@@ -883,17 +884,37 @@ struct rockchip_clk_branch {
 #define SGRF_GATE(_id, cname, pname)                           \
                FACTOR(_id, cname, pname, 0, 1, 1)
 
+static inline struct clk *rockchip_clk_get_lookup(struct rockchip_clk_provider 
*ctx,
+                                                 unsigned int id)
+{
+       return ctx->clk_data.clks[id];
+}
+
+static inline void rockchip_clk_set_lookup(struct rockchip_clk_provider *ctx,
+                                          struct clk *clk, unsigned int id)
+{
+       ctx->clk_data.clks[id] = clk;
+}
+
+struct rockchip_gate_link_platdata {
+       struct rockchip_clk_provider *ctx;
+       struct rockchip_clk_branch *clkbr;
+};
+
 struct rockchip_clk_provider *rockchip_clk_init(struct device_node *np,
                        void __iomem *base, unsigned long nr_clks);
 void rockchip_clk_of_add_provider(struct device_node *np,
                                struct rockchip_clk_provider *ctx);
-void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx,
-                            struct clk *clk, unsigned int id);
 unsigned long rockchip_clk_find_max_clk_id(struct rockchip_clk_branch *list,
                                           unsigned int nr_clk);
 void rockchip_clk_register_branches(struct rockchip_clk_provider *ctx,
                                    struct rockchip_clk_branch *list,
                                    unsigned int nr_clk);
+void rockchip_clk_register_late_branches(struct device *dev,
+                                        struct rockchip_clk_provider *ctx,
+                                        struct rockchip_clk_branch *list,
+                                        unsigned int nr_clk);
+
 void rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
                                struct rockchip_pll_clock *pll_list,
                                unsigned int nr_pll, int grf_lock_offset);
diff --git a/drivers/clk/rockchip/gate-link.c b/drivers/clk/rockchip/gate-link.c
new file mode 100644
index 0000000000..710e77892a
--- /dev/null
+++ b/drivers/clk/rockchip/gate-link.c
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Collabora Ltd.
+ * Author: Sebastian Reichel <sebastian.reic...@collabora.com>
+ */
+
+#include <common.h>
+
+#include "clk.h"
+
+static int rk_clk_gate_link_register(struct device *dev,
+                                    struct rockchip_clk_provider *ctx,
+                                    struct rockchip_clk_branch *clkbr)
+{
+       unsigned long flags = clkbr->flags | CLK_SET_RATE_PARENT;
+       struct clk *clk;
+
+       clk = clk_register_gate(dev, clkbr->name, clkbr->parent_names[0],
+                               flags, ctx->reg_base + clkbr->gate_offset,
+                               clkbr->gate_shift, clkbr->gate_flags,
+                               &ctx->lock);
+
+       if (IS_ERR(clk))
+               return PTR_ERR(clk);
+
+       rockchip_clk_set_lookup(ctx, clk, clkbr->id);
+
+       return 0;
+}
+
+static int rk_clk_gate_link_probe(struct device *dev)
+{
+       struct rockchip_gate_link_platdata *pdata = dev->platform_data;
+       if (!pdata)
+               return dev_err_probe(dev, -ENODEV, "missing platform data");
+
+       return rk_clk_gate_link_register(dev, pdata->ctx, pdata->clkbr);
+}
+
+static struct driver rk_clk_gate_link_driver = {
+       .probe  = rk_clk_gate_link_probe,
+       .name   = "rockchip-gate-link-clk",
+};
+core_platform_driver(rk_clk_gate_link_driver);
-- 
2.39.1


Reply via email to