1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 DENX Software Engineering
4 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
6 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
7 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
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.
13 * Gated clock implementation
20 #include <clk-uclass.h>
21 #include <dm/device.h>
22 #include <dm/devres.h>
23 #include <linux/clk-provider.h>
26 #include <linux/err.h>
28 #define UBOOT_DM_CLK_IMX_GATE2 "imx_clk_gate2"
38 #define to_clk_gate2(_clk) container_of(_clk, struct clk_gate2, clk)
40 static int clk_gate2_enable(struct clk *clk)
42 struct clk_gate2 *gate = to_clk_gate2(dev_get_clk_ptr(clk->dev));
45 reg = readl(gate->reg);
46 reg &= ~(3 << gate->bit_idx);
47 reg |= gate->cgr_val << gate->bit_idx;
48 writel(reg, gate->reg);
53 static int clk_gate2_disable(struct clk *clk)
55 struct clk_gate2 *gate = to_clk_gate2(dev_get_clk_ptr(clk->dev));
58 reg = readl(gate->reg);
59 reg &= ~(3 << gate->bit_idx);
60 writel(reg, gate->reg);
65 static ulong clk_gate2_set_rate(struct clk *clk, ulong rate)
67 struct clk *parent = clk_get_parent(clk);
70 return clk_set_rate(parent, rate);
75 static const struct clk_ops clk_gate2_ops = {
76 .set_rate = clk_gate2_set_rate,
77 .enable = clk_gate2_enable,
78 .disable = clk_gate2_disable,
79 .get_rate = clk_generic_get_rate,
82 struct clk *clk_register_gate2(struct device *dev, const char *name,
83 const char *parent_name, unsigned long flags,
84 void __iomem *reg, u8 bit_idx, u8 cgr_val,
87 struct clk_gate2 *gate;
91 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
93 return ERR_PTR(-ENOMEM);
96 gate->bit_idx = bit_idx;
97 gate->cgr_val = cgr_val;
98 gate->flags = clk_gate2_flags;
102 ret = clk_register(clk, UBOOT_DM_CLK_IMX_GATE2, name, parent_name);
111 U_BOOT_DRIVER(clk_gate2) = {
112 .name = UBOOT_DM_CLK_IMX_GATE2,
114 .ops = &clk_gate2_ops,
115 .flags = DM_FLAG_PRE_RELOC,