1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 DENX Software Engineering
7 #define LOG_CATEGORY UCLASS_CLK
11 #include <clk-uclass.h>
13 #include <dm/device.h>
14 #include <dm/uclass.h>
16 #include <dm/device-internal.h>
18 int clk_register(struct clk *clk, const char *drv_name,
19 const char *name, const char *parent_name)
21 struct udevice *parent = NULL;
26 ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
28 log_err("%s: failed to get %s device (parent of %s)\n",
29 __func__, parent_name, name);
31 log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
32 parent->name, parent);
36 drv = lists_driver_lookup_name(drv_name);
38 log_err("%s: %s is not a valid driver name\n",
43 ret = device_bind(parent, drv, name, NULL, ofnode_null(), &clk->dev);
45 log_err("%s: CLK: %s driver bind error [%d]!\n", __func__, name,
50 clk->enable_count = 0;
52 /* Store back pointer to clk from udevice */
53 /* FIXME: This is not allowed...should be allocated by driver model */
54 dev_set_uclass_priv(clk->dev, clk);
59 ulong clk_generic_get_rate(struct clk *clk)
61 return clk_get_parent_rate(clk);
64 const char *clk_hw_get_name(const struct clk *hw)
72 bool clk_dev_binded(struct clk *clk)
74 if (clk->dev && (dev_get_flags(clk->dev) & DM_FLAG_BOUND))
80 /* Helper functions for clock ops */
82 ulong ccf_clk_get_rate(struct clk *clk)
85 int err = clk_get_by_id(clk->id, &c);
89 return clk_get_rate(c);
92 ulong ccf_clk_set_rate(struct clk *clk, unsigned long rate)
95 int err = clk_get_by_id(clk->id, &c);
99 return clk_set_rate(c, rate);
102 int ccf_clk_set_parent(struct clk *clk, struct clk *parent)
105 int err = clk_get_by_id(clk->id, &c);
110 err = clk_get_by_id(parent->id, &p);
114 return clk_set_parent(c, p);
117 static int ccf_clk_endisable(struct clk *clk, bool enable)
120 int err = clk_get_by_id(clk->id, &c);
124 return enable ? clk_enable(c) : clk_disable(c);
127 int ccf_clk_enable(struct clk *clk)
129 return ccf_clk_endisable(clk, true);
132 int ccf_clk_disable(struct clk *clk)
134 return ccf_clk_endisable(clk, false);
137 const struct clk_ops ccf_clk_ops = {
138 .set_rate = ccf_clk_set_rate,
139 .get_rate = ccf_clk_get_rate,
140 .set_parent = ccf_clk_set_parent,
141 .enable = ccf_clk_enable,
142 .disable = ccf_clk_disable,