]> Git Repo - u-boot.git/blob - drivers/clk/clk.c
6ede1b4d4dcc4ac16e3d547d825d5bf8088239ff
[u-boot.git] / drivers / clk / clk.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 DENX Software Engineering
4  * Lukasz Majewski, DENX Software Engineering, [email protected]
5  */
6
7 #define LOG_CATEGORY UCLASS_CLK
8
9 #include <common.h>
10 #include <clk.h>
11 #include <clk-uclass.h>
12 #include <log.h>
13 #include <dm/device.h>
14 #include <dm/uclass.h>
15 #include <dm/lists.h>
16 #include <dm/device-internal.h>
17
18 int clk_register(struct clk *clk, const char *drv_name,
19                  const char *name, const char *parent_name)
20 {
21         struct udevice *parent = NULL;
22         struct driver *drv;
23         int ret;
24
25         if (parent_name) {
26                 ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
27                 if (ret) {
28                         log_err("%s: failed to get %s device (parent of %s)\n",
29                                 __func__, parent_name, name);
30                 } else {
31                         log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
32                                   parent->name, parent);
33                 }
34         }
35
36         drv = lists_driver_lookup_name(drv_name);
37         if (!drv) {
38                 log_err("%s: %s is not a valid driver name\n",
39                         __func__, drv_name);
40                 return -ENOENT;
41         }
42
43         ret = device_bind(parent, drv, name, NULL, ofnode_null(), &clk->dev);
44         if (ret) {
45                 log_err("%s: CLK: %s driver bind error [%d]!\n", __func__, name,
46                         ret);
47                 return ret;
48         }
49
50         clk->enable_count = 0;
51
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);
55
56         return 0;
57 }
58
59 ulong clk_generic_get_rate(struct clk *clk)
60 {
61         return clk_get_parent_rate(clk);
62 }
63
64 const char *clk_hw_get_name(const struct clk *hw)
65 {
66         assert(hw);
67         assert(hw->dev);
68
69         return hw->dev->name;
70 }
71
72 bool clk_dev_binded(struct clk *clk)
73 {
74         if (clk->dev && (dev_get_flags(clk->dev) & DM_FLAG_BOUND))
75                 return true;
76
77         return false;
78 }
79
80 /* Helper functions for clock ops */
81
82 ulong ccf_clk_get_rate(struct clk *clk)
83 {
84         struct clk *c;
85         int err = clk_get_by_id(clk->id, &c);
86
87         if (err)
88                 return err;
89         return clk_get_rate(c);
90 }
91
92 ulong ccf_clk_set_rate(struct clk *clk, unsigned long rate)
93 {
94         struct clk *c;
95         int err = clk_get_by_id(clk->id, &c);
96
97         if (err)
98                 return err;
99         return clk_set_rate(c, rate);
100 }
101
102 int ccf_clk_set_parent(struct clk *clk, struct clk *parent)
103 {
104         struct clk *c, *p;
105         int err = clk_get_by_id(clk->id, &c);
106
107         if (err)
108                 return err;
109
110         err = clk_get_by_id(parent->id, &p);
111         if (err)
112                 return err;
113
114         return clk_set_parent(c, p);
115 }
116
117 static int ccf_clk_endisable(struct clk *clk, bool enable)
118 {
119         struct clk *c;
120         int err = clk_get_by_id(clk->id, &c);
121
122         if (err)
123                 return err;
124         return enable ? clk_enable(c) : clk_disable(c);
125 }
126
127 int ccf_clk_enable(struct clk *clk)
128 {
129         return ccf_clk_endisable(clk, true);
130 }
131
132 int ccf_clk_disable(struct clk *clk)
133 {
134         return ccf_clk_endisable(clk, false);
135 }
136
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,
143 };
This page took 0.023507 seconds and 2 git commands to generate.