xref: /openbmc/u-boot/drivers/clk/clk-uclass.c (revision 3c216834)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  * Copyright (c) 2016, NVIDIA CORPORATION.
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <clk.h>
11 #include <clk-uclass.h>
12 #include <dm.h>
13 #include <dt-structs.h>
14 #include <errno.h>
15 
16 static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
17 {
18 	return (const struct clk_ops *)dev->driver->ops;
19 }
20 
21 #if CONFIG_IS_ENABLED(OF_CONTROL)
22 # if CONFIG_IS_ENABLED(OF_PLATDATA)
23 int clk_get_by_index_platdata(struct udevice *dev, int index,
24 			      struct phandle_1_arg *cells, struct clk *clk)
25 {
26 	int ret;
27 
28 	if (index != 0)
29 		return -ENOSYS;
30 	ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
31 	if (ret)
32 		return ret;
33 	clk->id = cells[0].arg[0];
34 
35 	return 0;
36 }
37 # else
38 static int clk_of_xlate_default(struct clk *clk,
39 				struct ofnode_phandle_args *args)
40 {
41 	debug("%s(clk=%p)\n", __func__, clk);
42 
43 	if (args->args_count > 1) {
44 		debug("Invaild args_count: %d\n", args->args_count);
45 		return -EINVAL;
46 	}
47 
48 	if (args->args_count)
49 		clk->id = args->args[0];
50 	else
51 		clk->id = 0;
52 
53 	return 0;
54 }
55 
56 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
57 {
58 	int ret;
59 	struct ofnode_phandle_args args;
60 	struct udevice *dev_clk;
61 	const struct clk_ops *ops;
62 
63 	debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
64 
65 	assert(clk);
66 	clk->dev = NULL;
67 
68 	ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
69 					 index, &args);
70 	if (ret) {
71 		debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
72 		      __func__, ret);
73 		return ret;
74 	}
75 
76 	ret = uclass_get_device_by_ofnode(UCLASS_CLK, args.node, &dev_clk);
77 	if (ret) {
78 		debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
79 		      __func__, ret);
80 		return ret;
81 	}
82 
83 	clk->dev = dev_clk;
84 
85 	ops = clk_dev_ops(dev_clk);
86 
87 	if (ops->of_xlate)
88 		ret = ops->of_xlate(clk, &args);
89 	else
90 		ret = clk_of_xlate_default(clk, &args);
91 	if (ret) {
92 		debug("of_xlate() failed: %d\n", ret);
93 		return ret;
94 	}
95 
96 	return clk_request(dev_clk, clk);
97 }
98 # endif /* OF_PLATDATA */
99 
100 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
101 {
102 	int index;
103 
104 	debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
105 	clk->dev = NULL;
106 
107 	index = dev_read_stringlist_search(dev, "clock-names", name);
108 	if (index < 0) {
109 		debug("fdt_stringlist_search() failed: %d\n", index);
110 		return index;
111 	}
112 
113 	return clk_get_by_index(dev, index, clk);
114 }
115 
116 int clk_release_all(struct clk *clk, int count)
117 {
118 	int i, ret;
119 
120 	for (i = 0; i < count; i++) {
121 		debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
122 
123 		/* check if clock has been previously requested */
124 		if (!clk[i].dev)
125 			continue;
126 
127 		ret = clk_disable(&clk[i]);
128 		if (ret && ret != -ENOSYS)
129 			return ret;
130 
131 		ret = clk_free(&clk[i]);
132 		if (ret && ret != -ENOSYS)
133 			return ret;
134 	}
135 
136 	return 0;
137 }
138 
139 #endif /* OF_CONTROL */
140 
141 int clk_request(struct udevice *dev, struct clk *clk)
142 {
143 	const struct clk_ops *ops = clk_dev_ops(dev);
144 
145 	debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
146 
147 	clk->dev = dev;
148 
149 	if (!ops->request)
150 		return 0;
151 
152 	return ops->request(clk);
153 }
154 
155 int clk_free(struct clk *clk)
156 {
157 	const struct clk_ops *ops = clk_dev_ops(clk->dev);
158 
159 	debug("%s(clk=%p)\n", __func__, clk);
160 
161 	if (!ops->free)
162 		return 0;
163 
164 	return ops->free(clk);
165 }
166 
167 ulong clk_get_rate(struct clk *clk)
168 {
169 	const struct clk_ops *ops = clk_dev_ops(clk->dev);
170 
171 	debug("%s(clk=%p)\n", __func__, clk);
172 
173 	if (!ops->get_rate)
174 		return -ENOSYS;
175 
176 	return ops->get_rate(clk);
177 }
178 
179 ulong clk_set_rate(struct clk *clk, ulong rate)
180 {
181 	const struct clk_ops *ops = clk_dev_ops(clk->dev);
182 
183 	debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
184 
185 	if (!ops->set_rate)
186 		return -ENOSYS;
187 
188 	return ops->set_rate(clk, rate);
189 }
190 
191 int clk_enable(struct clk *clk)
192 {
193 	const struct clk_ops *ops = clk_dev_ops(clk->dev);
194 
195 	debug("%s(clk=%p)\n", __func__, clk);
196 
197 	if (!ops->enable)
198 		return -ENOSYS;
199 
200 	return ops->enable(clk);
201 }
202 
203 int clk_disable(struct clk *clk)
204 {
205 	const struct clk_ops *ops = clk_dev_ops(clk->dev);
206 
207 	debug("%s(clk=%p)\n", __func__, clk);
208 
209 	if (!ops->disable)
210 		return -ENOSYS;
211 
212 	return ops->disable(clk);
213 }
214 
215 UCLASS_DRIVER(clk) = {
216 	.id		= UCLASS_CLK,
217 	.name		= "clk",
218 };
219