blob: ad763795d9e8ef8912c08d5d4a8f8520caf1b3fd [file] [log] [blame]
Simon Glassf26c8a82015-06-23 15:39:15 -06001/*
2 * Copyright (C) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
Stephen Warren135aa952016-06-17 09:44:00 -06004 * Copyright (c) 2016, NVIDIA CORPORATION.
Philipp Tomsichf4fcba52018-01-08 13:59:18 +01005 * Copyright (c) 2018, Theobroma Systems Design und Consulting GmbH
Simon Glassf26c8a82015-06-23 15:39:15 -06006 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <clk.h>
Stephen Warren135aa952016-06-17 09:44:00 -060012#include <clk-uclass.h>
Simon Glassf26c8a82015-06-23 15:39:15 -060013#include <dm.h>
Philipp Tomsichf4fcba52018-01-08 13:59:18 +010014#include <dm/read.h>
Simon Glass7423daa2016-07-04 11:58:03 -060015#include <dt-structs.h>
Simon Glassf26c8a82015-06-23 15:39:15 -060016#include <errno.h>
Simon Glassf26c8a82015-06-23 15:39:15 -060017
Mario Six268453b2018-01-15 11:06:51 +010018static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
Simon Glassf26c8a82015-06-23 15:39:15 -060019{
Mario Six268453b2018-01-15 11:06:51 +010020 return (const struct clk_ops *)dev->driver->ops;
Simon Glassf26c8a82015-06-23 15:39:15 -060021}
22
Simon Glasse70cc432016-01-20 19:43:02 -070023#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass7423daa2016-07-04 11:58:03 -060024# if CONFIG_IS_ENABLED(OF_PLATDATA)
25int clk_get_by_index_platdata(struct udevice *dev, int index,
Simon Glass0d154632017-08-29 14:15:56 -060026 struct phandle_1_arg *cells, struct clk *clk)
Simon Glass7423daa2016-07-04 11:58:03 -060027{
28 int ret;
29
30 if (index != 0)
31 return -ENOSYS;
32 ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
33 if (ret)
34 return ret;
Simon Glassbc796172017-08-29 14:15:58 -060035 clk->id = cells[0].arg[0];
Simon Glass7423daa2016-07-04 11:58:03 -060036
37 return 0;
38}
39# else
Stephen Warren135aa952016-06-17 09:44:00 -060040static int clk_of_xlate_default(struct clk *clk,
Simon Glassa4e0ef52017-05-18 20:09:40 -060041 struct ofnode_phandle_args *args)
Stephen Warren135aa952016-06-17 09:44:00 -060042{
43 debug("%s(clk=%p)\n", __func__, clk);
44
45 if (args->args_count > 1) {
46 debug("Invaild args_count: %d\n", args->args_count);
47 return -EINVAL;
48 }
49
50 if (args->args_count)
51 clk->id = args->args[0];
52 else
53 clk->id = 0;
54
55 return 0;
56}
57
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +010058static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
59 int index, struct clk *clk)
Stephen Warren135aa952016-06-17 09:44:00 -060060{
61 int ret;
Simon Glassaa9bb092017-05-30 21:47:29 -060062 struct ofnode_phandle_args args;
Stephen Warren135aa952016-06-17 09:44:00 -060063 struct udevice *dev_clk;
Mario Six268453b2018-01-15 11:06:51 +010064 const struct clk_ops *ops;
Stephen Warren135aa952016-06-17 09:44:00 -060065
66 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
67
68 assert(clk);
Patrice Chotard82a8a662017-07-18 11:57:07 +020069 clk->dev = NULL;
70
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +010071 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
Mario Six268453b2018-01-15 11:06:51 +010072 index, &args);
Simon Glasse70cc432016-01-20 19:43:02 -070073 if (ret) {
74 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
75 __func__, ret);
76 return ret;
77 }
78
Simon Glassaa9bb092017-05-30 21:47:29 -060079 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args.node, &dev_clk);
Simon Glasse70cc432016-01-20 19:43:02 -070080 if (ret) {
81 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
82 __func__, ret);
83 return ret;
84 }
Wenyou Yang3f56b132016-09-27 11:00:28 +080085
86 clk->dev = dev_clk;
87
Stephen Warren135aa952016-06-17 09:44:00 -060088 ops = clk_dev_ops(dev_clk);
89
90 if (ops->of_xlate)
Simon Glassaa9bb092017-05-30 21:47:29 -060091 ret = ops->of_xlate(clk, &args);
Stephen Warren135aa952016-06-17 09:44:00 -060092 else
Simon Glassaa9bb092017-05-30 21:47:29 -060093 ret = clk_of_xlate_default(clk, &args);
Stephen Warren135aa952016-06-17 09:44:00 -060094 if (ret) {
95 debug("of_xlate() failed: %d\n", ret);
96 return ret;
97 }
98
99 return clk_request(dev_clk, clk);
100}
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100101
102int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
103{
104 return clk_get_by_indexed_prop(dev, "clocks", index, clk);
105}
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100106
107static int clk_set_default_parents(struct udevice *dev)
108{
109 struct clk clk, parent_clk;
110 int index;
111 int num_parents;
112 int ret;
113
114 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
115 "#clock-cells");
116 if (num_parents < 0) {
117 debug("%s: could not read assigned-clock-parents for %p\n",
118 __func__, dev);
119 return 0;
120 }
121
122 for (index = 0; index < num_parents; index++) {
123 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
124 index, &parent_clk);
125 if (ret) {
126 debug("%s: could not get parent clock %d for %s\n",
127 __func__, index, dev_read_name(dev));
128 return ret;
129 }
130
131 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
132 index, &clk);
133 if (ret) {
134 debug("%s: could not get assigned clock %d for %s\n",
135 __func__, index, dev_read_name(dev));
136 return ret;
137 }
138
139 ret = clk_set_parent(&clk, &parent_clk);
140
141 /*
142 * Not all drivers may support clock-reparenting (as of now).
143 * Ignore errors due to this.
144 */
145 if (ret == -ENOSYS)
146 continue;
147
148 if (ret) {
149 debug("%s: failed to reparent clock %d for %s\n",
150 __func__, index, dev_read_name(dev));
151 return ret;
152 }
153 }
154
155 return 0;
156}
157
158static int clk_set_default_rates(struct udevice *dev)
159{
160 struct clk clk;
161 int index;
162 int num_rates;
163 int size;
164 int ret = 0;
165 u32 *rates = NULL;
166
167 size = dev_read_size(dev, "assigned-clock-rates");
168 if (size < 0)
169 return 0;
170
171 num_rates = size / sizeof(u32);
172 rates = calloc(num_rates, sizeof(u32));
173 if (!rates)
174 return -ENOMEM;
175
176 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
177 if (ret)
178 goto fail;
179
180 for (index = 0; index < num_rates; index++) {
181 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
182 index, &clk);
183 if (ret) {
184 debug("%s: could not get assigned clock %d for %s\n",
185 __func__, index, dev_read_name(dev));
186 continue;
187 }
188
189 ret = clk_set_rate(&clk, rates[index]);
190 if (ret < 0) {
191 debug("%s: failed to set rate on clock %d for %s\n",
192 __func__, index, dev_read_name(dev));
193 break;
194 }
195 }
196
197fail:
198 free(rates);
199 return ret;
200}
201
202int clk_set_defaults(struct udevice *dev)
203{
204 int ret;
205
206 /* If this is running pre-reloc state, don't take any action. */
207 if (!(gd->flags & GD_FLG_RELOC))
208 return 0;
209
210 debug("%s(%s)\n", __func__, dev_read_name(dev));
211
212 ret = clk_set_default_parents(dev);
213 if (ret)
214 return ret;
215
216 ret = clk_set_default_rates(dev);
217 if (ret < 0)
218 return ret;
219
220 return 0;
221}
Michal Simek9e0758b2016-07-14 13:11:37 +0200222# endif /* OF_PLATDATA */
Stephen Warren135aa952016-06-17 09:44:00 -0600223
224int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
225{
226 int index;
227
228 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
Patrice Chotard82a8a662017-07-18 11:57:07 +0200229 clk->dev = NULL;
Stephen Warren135aa952016-06-17 09:44:00 -0600230
Simon Glassaa9bb092017-05-30 21:47:29 -0600231 index = dev_read_stringlist_search(dev, "clock-names", name);
Stephen Warren135aa952016-06-17 09:44:00 -0600232 if (index < 0) {
Simon Glassb02e4042016-10-02 17:59:28 -0600233 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren135aa952016-06-17 09:44:00 -0600234 return index;
235 }
236
237 return clk_get_by_index(dev, index, clk);
Simon Glasse70cc432016-01-20 19:43:02 -0700238}
Patrice Chotardb108d8a2017-07-25 13:24:45 +0200239
240int clk_release_all(struct clk *clk, int count)
241{
242 int i, ret;
243
244 for (i = 0; i < count; i++) {
245 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
246
247 /* check if clock has been previously requested */
248 if (!clk[i].dev)
249 continue;
250
251 ret = clk_disable(&clk[i]);
252 if (ret && ret != -ENOSYS)
253 return ret;
254
255 ret = clk_free(&clk[i]);
256 if (ret && ret != -ENOSYS)
257 return ret;
258 }
259
260 return 0;
261}
262
Simon Glass7423daa2016-07-04 11:58:03 -0600263#endif /* OF_CONTROL */
Stephen Warren135aa952016-06-17 09:44:00 -0600264
265int clk_request(struct udevice *dev, struct clk *clk)
266{
Mario Six268453b2018-01-15 11:06:51 +0100267 const struct clk_ops *ops = clk_dev_ops(dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600268
269 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
270
271 clk->dev = dev;
272
273 if (!ops->request)
274 return 0;
275
276 return ops->request(clk);
277}
278
279int clk_free(struct clk *clk)
280{
Mario Six268453b2018-01-15 11:06:51 +0100281 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600282
283 debug("%s(clk=%p)\n", __func__, clk);
284
285 if (!ops->free)
286 return 0;
287
288 return ops->free(clk);
289}
290
291ulong clk_get_rate(struct clk *clk)
292{
Mario Six268453b2018-01-15 11:06:51 +0100293 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600294
295 debug("%s(clk=%p)\n", __func__, clk);
296
297 if (!ops->get_rate)
298 return -ENOSYS;
299
300 return ops->get_rate(clk);
301}
302
303ulong clk_set_rate(struct clk *clk, ulong rate)
304{
Mario Six268453b2018-01-15 11:06:51 +0100305 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600306
307 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
308
309 if (!ops->set_rate)
310 return -ENOSYS;
311
312 return ops->set_rate(clk, rate);
313}
314
Philipp Tomsichf7d10462018-01-08 11:15:08 +0100315int clk_set_parent(struct clk *clk, struct clk *parent)
316{
317 const struct clk_ops *ops = clk_dev_ops(clk->dev);
318
319 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
320
321 if (!ops->set_parent)
322 return -ENOSYS;
323
324 return ops->set_parent(clk, parent);
325}
326
Stephen Warren135aa952016-06-17 09:44:00 -0600327int clk_enable(struct clk *clk)
328{
Mario Six268453b2018-01-15 11:06:51 +0100329 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600330
331 debug("%s(clk=%p)\n", __func__, clk);
332
333 if (!ops->enable)
334 return -ENOSYS;
335
336 return ops->enable(clk);
337}
338
339int clk_disable(struct clk *clk)
340{
Mario Six268453b2018-01-15 11:06:51 +0100341 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600342
343 debug("%s(clk=%p)\n", __func__, clk);
344
345 if (!ops->disable)
346 return -ENOSYS;
347
348 return ops->disable(clk);
349}
Simon Glasse70cc432016-01-20 19:43:02 -0700350
Simon Glassf26c8a82015-06-23 15:39:15 -0600351UCLASS_DRIVER(clk) = {
352 .id = UCLASS_CLK,
353 .name = "clk",
354};