blob: d9236c5b510490fc1eea5bb70d8c05d74729f94e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassf26c8a82015-06-23 15:39:15 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Stephen Warren135aa952016-06-17 09:44:00 -06005 * Copyright (c) 2016, NVIDIA CORPORATION.
Philipp Tomsichf4fcba52018-01-08 13:59:18 +01006 * Copyright (c) 2018, Theobroma Systems Design und Consulting GmbH
Simon Glassf26c8a82015-06-23 15:39:15 -06007 */
8
9#include <common.h>
10#include <clk.h>
Stephen Warren135aa952016-06-17 09:44:00 -060011#include <clk-uclass.h>
Simon Glassf26c8a82015-06-23 15:39:15 -060012#include <dm.h>
Philipp Tomsichf4fcba52018-01-08 13:59:18 +010013#include <dm/read.h>
Simon Glass7423daa2016-07-04 11:58:03 -060014#include <dt-structs.h>
Simon Glassf26c8a82015-06-23 15:39:15 -060015#include <errno.h>
Simon Glassf26c8a82015-06-23 15:39:15 -060016
Mario Six268453b2018-01-15 11:06:51 +010017static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
Simon Glassf26c8a82015-06-23 15:39:15 -060018{
Mario Six268453b2018-01-15 11:06:51 +010019 return (const struct clk_ops *)dev->driver->ops;
Simon Glassf26c8a82015-06-23 15:39:15 -060020}
21
Simon Glasse70cc432016-01-20 19:43:02 -070022#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass7423daa2016-07-04 11:58:03 -060023# if CONFIG_IS_ENABLED(OF_PLATDATA)
24int clk_get_by_index_platdata(struct udevice *dev, int index,
Simon Glass0d154632017-08-29 14:15:56 -060025 struct phandle_1_arg *cells, struct clk *clk)
Simon Glass7423daa2016-07-04 11:58:03 -060026{
27 int ret;
28
29 if (index != 0)
30 return -ENOSYS;
31 ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
32 if (ret)
33 return ret;
Simon Glassbc796172017-08-29 14:15:58 -060034 clk->id = cells[0].arg[0];
Simon Glass7423daa2016-07-04 11:58:03 -060035
36 return 0;
37}
38# else
Stephen Warren135aa952016-06-17 09:44:00 -060039static int clk_of_xlate_default(struct clk *clk,
Simon Glassa4e0ef52017-05-18 20:09:40 -060040 struct ofnode_phandle_args *args)
Stephen Warren135aa952016-06-17 09:44:00 -060041{
42 debug("%s(clk=%p)\n", __func__, clk);
43
44 if (args->args_count > 1) {
45 debug("Invaild args_count: %d\n", args->args_count);
46 return -EINVAL;
47 }
48
49 if (args->args_count)
50 clk->id = args->args[0];
51 else
52 clk->id = 0;
53
54 return 0;
55}
56
Jagan Teki75f98312019-02-28 00:26:52 +053057static int clk_get_by_index_tail(int ret, ofnode node,
58 struct ofnode_phandle_args *args,
59 const char *list_name, int index,
60 struct clk *clk)
61{
62 struct udevice *dev_clk;
63 const struct clk_ops *ops;
64
65 assert(clk);
66 clk->dev = NULL;
67 if (ret)
68 goto err;
69
70 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
71 if (ret) {
72 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
73 __func__, ret);
74 return ret;
75 }
76
77 clk->dev = dev_clk;
78
79 ops = clk_dev_ops(dev_clk);
80
81 if (ops->of_xlate)
82 ret = ops->of_xlate(clk, args);
83 else
84 ret = clk_of_xlate_default(clk, args);
85 if (ret) {
86 debug("of_xlate() failed: %d\n", ret);
87 return ret;
88 }
89
90 return clk_request(dev_clk, clk);
91err:
92 debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n",
93 __func__, ofnode_get_name(node), list_name, index, ret);
94 return ret;
95}
96
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +010097static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
98 int index, struct clk *clk)
Stephen Warren135aa952016-06-17 09:44:00 -060099{
100 int ret;
Simon Glassaa9bb092017-05-30 21:47:29 -0600101 struct ofnode_phandle_args args;
Stephen Warren135aa952016-06-17 09:44:00 -0600102 struct udevice *dev_clk;
Mario Six268453b2018-01-15 11:06:51 +0100103 const struct clk_ops *ops;
Stephen Warren135aa952016-06-17 09:44:00 -0600104
105 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
106
107 assert(clk);
Patrice Chotard82a8a662017-07-18 11:57:07 +0200108 clk->dev = NULL;
109
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100110 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
Mario Six268453b2018-01-15 11:06:51 +0100111 index, &args);
Simon Glasse70cc432016-01-20 19:43:02 -0700112 if (ret) {
113 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
114 __func__, ret);
115 return ret;
116 }
117
Simon Glassaa9bb092017-05-30 21:47:29 -0600118 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args.node, &dev_clk);
Simon Glasse70cc432016-01-20 19:43:02 -0700119 if (ret) {
120 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
121 __func__, ret);
122 return ret;
123 }
Wenyou Yang3f56b132016-09-27 11:00:28 +0800124
125 clk->dev = dev_clk;
126
Stephen Warren135aa952016-06-17 09:44:00 -0600127 ops = clk_dev_ops(dev_clk);
128
129 if (ops->of_xlate)
Simon Glassaa9bb092017-05-30 21:47:29 -0600130 ret = ops->of_xlate(clk, &args);
Stephen Warren135aa952016-06-17 09:44:00 -0600131 else
Simon Glassaa9bb092017-05-30 21:47:29 -0600132 ret = clk_of_xlate_default(clk, &args);
Stephen Warren135aa952016-06-17 09:44:00 -0600133 if (ret) {
134 debug("of_xlate() failed: %d\n", ret);
135 return ret;
136 }
137
138 return clk_request(dev_clk, clk);
139}
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100140
141int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
142{
Jagan Teki75f98312019-02-28 00:26:52 +0530143 struct ofnode_phandle_args args;
144 int ret;
145
146 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
147 index, &args);
148
149 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
150 index > 0, clk);
151}
152
153int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
154{
155 struct ofnode_phandle_args args;
156 int ret;
157
158 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
159 index > 0, &args);
160
161 return clk_get_by_index_tail(ret, node, &args, "clocks",
162 index > 0, clk);
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100163}
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100164
Neil Armstronga855be82018-04-03 11:44:18 +0200165int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
166{
167 int i, ret, err, count;
168
169 bulk->count = 0;
170
171 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
Neil Armstrong721881c2018-04-17 11:30:31 +0200172 if (count < 1)
173 return count;
Neil Armstronga855be82018-04-03 11:44:18 +0200174
175 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
176 if (!bulk->clks)
177 return -ENOMEM;
178
179 for (i = 0; i < count; i++) {
180 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
181 if (ret < 0)
182 goto bulk_get_err;
183
184 ++bulk->count;
185 }
186
187 return 0;
188
189bulk_get_err:
190 err = clk_release_all(bulk->clks, bulk->count);
191 if (err)
192 debug("%s: could release all clocks for %p\n",
193 __func__, dev);
194
195 return ret;
196}
197
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100198static int clk_set_default_parents(struct udevice *dev)
199{
200 struct clk clk, parent_clk;
201 int index;
202 int num_parents;
203 int ret;
204
205 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
206 "#clock-cells");
207 if (num_parents < 0) {
208 debug("%s: could not read assigned-clock-parents for %p\n",
209 __func__, dev);
210 return 0;
211 }
212
213 for (index = 0; index < num_parents; index++) {
214 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
215 index, &parent_clk);
Neil Armstrongd64caaf2018-07-26 15:19:32 +0200216 /* If -ENOENT, this is a no-op entry */
217 if (ret == -ENOENT)
218 continue;
219
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100220 if (ret) {
221 debug("%s: could not get parent clock %d for %s\n",
222 __func__, index, dev_read_name(dev));
223 return ret;
224 }
225
226 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
227 index, &clk);
228 if (ret) {
229 debug("%s: could not get assigned clock %d for %s\n",
230 __func__, index, dev_read_name(dev));
231 return ret;
232 }
233
234 ret = clk_set_parent(&clk, &parent_clk);
235
236 /*
237 * Not all drivers may support clock-reparenting (as of now).
238 * Ignore errors due to this.
239 */
240 if (ret == -ENOSYS)
241 continue;
242
243 if (ret) {
244 debug("%s: failed to reparent clock %d for %s\n",
245 __func__, index, dev_read_name(dev));
246 return ret;
247 }
248 }
249
250 return 0;
251}
252
253static int clk_set_default_rates(struct udevice *dev)
254{
255 struct clk clk;
256 int index;
257 int num_rates;
258 int size;
259 int ret = 0;
260 u32 *rates = NULL;
261
262 size = dev_read_size(dev, "assigned-clock-rates");
263 if (size < 0)
264 return 0;
265
266 num_rates = size / sizeof(u32);
267 rates = calloc(num_rates, sizeof(u32));
268 if (!rates)
269 return -ENOMEM;
270
271 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
272 if (ret)
273 goto fail;
274
275 for (index = 0; index < num_rates; index++) {
Neil Armstrongd64caaf2018-07-26 15:19:32 +0200276 /* If 0 is passed, this is a no-op */
277 if (!rates[index])
278 continue;
279
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100280 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
281 index, &clk);
282 if (ret) {
283 debug("%s: could not get assigned clock %d for %s\n",
284 __func__, index, dev_read_name(dev));
285 continue;
286 }
287
288 ret = clk_set_rate(&clk, rates[index]);
289 if (ret < 0) {
Simon Glass68316162019-01-21 14:53:19 -0700290 debug("%s: failed to set rate on clock index %d (%ld) for %s\n",
291 __func__, index, clk.id, dev_read_name(dev));
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100292 break;
293 }
294 }
295
296fail:
297 free(rates);
298 return ret;
299}
300
301int clk_set_defaults(struct udevice *dev)
302{
303 int ret;
304
Philipp Tomsich291da962018-11-26 20:20:19 +0100305 /* If this not in SPL and pre-reloc state, don't take any action. */
306 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
307 return 0;
308
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100309 debug("%s(%s)\n", __func__, dev_read_name(dev));
310
311 ret = clk_set_default_parents(dev);
312 if (ret)
313 return ret;
314
315 ret = clk_set_default_rates(dev);
316 if (ret < 0)
317 return ret;
318
319 return 0;
320}
Michal Simek9e0758b2016-07-14 13:11:37 +0200321# endif /* OF_PLATDATA */
Stephen Warren135aa952016-06-17 09:44:00 -0600322
323int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
324{
325 int index;
326
327 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
Patrice Chotard82a8a662017-07-18 11:57:07 +0200328 clk->dev = NULL;
Stephen Warren135aa952016-06-17 09:44:00 -0600329
Simon Glassaa9bb092017-05-30 21:47:29 -0600330 index = dev_read_stringlist_search(dev, "clock-names", name);
Stephen Warren135aa952016-06-17 09:44:00 -0600331 if (index < 0) {
Simon Glassb02e4042016-10-02 17:59:28 -0600332 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren135aa952016-06-17 09:44:00 -0600333 return index;
334 }
335
336 return clk_get_by_index(dev, index, clk);
Simon Glasse70cc432016-01-20 19:43:02 -0700337}
Patrice Chotardb108d8a2017-07-25 13:24:45 +0200338
339int clk_release_all(struct clk *clk, int count)
340{
341 int i, ret;
342
343 for (i = 0; i < count; i++) {
344 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
345
346 /* check if clock has been previously requested */
347 if (!clk[i].dev)
348 continue;
349
350 ret = clk_disable(&clk[i]);
351 if (ret && ret != -ENOSYS)
352 return ret;
353
354 ret = clk_free(&clk[i]);
355 if (ret && ret != -ENOSYS)
356 return ret;
357 }
358
359 return 0;
360}
361
Simon Glass7423daa2016-07-04 11:58:03 -0600362#endif /* OF_CONTROL */
Stephen Warren135aa952016-06-17 09:44:00 -0600363
364int clk_request(struct udevice *dev, struct clk *clk)
365{
Mario Six268453b2018-01-15 11:06:51 +0100366 const struct clk_ops *ops = clk_dev_ops(dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600367
368 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
369
370 clk->dev = dev;
371
372 if (!ops->request)
373 return 0;
374
375 return ops->request(clk);
376}
377
378int clk_free(struct clk *clk)
379{
Mario Six268453b2018-01-15 11:06:51 +0100380 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600381
382 debug("%s(clk=%p)\n", __func__, clk);
383
384 if (!ops->free)
385 return 0;
386
387 return ops->free(clk);
388}
389
390ulong clk_get_rate(struct clk *clk)
391{
Mario Six268453b2018-01-15 11:06:51 +0100392 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600393
394 debug("%s(clk=%p)\n", __func__, clk);
395
396 if (!ops->get_rate)
397 return -ENOSYS;
398
399 return ops->get_rate(clk);
400}
401
402ulong clk_set_rate(struct clk *clk, ulong rate)
403{
Mario Six268453b2018-01-15 11:06:51 +0100404 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600405
406 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
407
408 if (!ops->set_rate)
409 return -ENOSYS;
410
411 return ops->set_rate(clk, rate);
412}
413
Philipp Tomsichf7d10462018-01-08 11:15:08 +0100414int clk_set_parent(struct clk *clk, struct clk *parent)
415{
416 const struct clk_ops *ops = clk_dev_ops(clk->dev);
417
418 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
419
420 if (!ops->set_parent)
421 return -ENOSYS;
422
423 return ops->set_parent(clk, parent);
424}
425
Stephen Warren135aa952016-06-17 09:44:00 -0600426int clk_enable(struct clk *clk)
427{
Mario Six268453b2018-01-15 11:06:51 +0100428 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600429
430 debug("%s(clk=%p)\n", __func__, clk);
431
432 if (!ops->enable)
433 return -ENOSYS;
434
435 return ops->enable(clk);
436}
437
Neil Armstronga855be82018-04-03 11:44:18 +0200438int clk_enable_bulk(struct clk_bulk *bulk)
439{
440 int i, ret;
441
442 for (i = 0; i < bulk->count; i++) {
443 ret = clk_enable(&bulk->clks[i]);
444 if (ret < 0 && ret != -ENOSYS)
445 return ret;
446 }
447
448 return 0;
449}
450
Stephen Warren135aa952016-06-17 09:44:00 -0600451int clk_disable(struct clk *clk)
452{
Mario Six268453b2018-01-15 11:06:51 +0100453 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600454
455 debug("%s(clk=%p)\n", __func__, clk);
456
457 if (!ops->disable)
458 return -ENOSYS;
459
460 return ops->disable(clk);
461}
Simon Glasse70cc432016-01-20 19:43:02 -0700462
Neil Armstronga855be82018-04-03 11:44:18 +0200463int clk_disable_bulk(struct clk_bulk *bulk)
464{
465 int i, ret;
466
467 for (i = 0; i < bulk->count; i++) {
468 ret = clk_disable(&bulk->clks[i]);
469 if (ret < 0 && ret != -ENOSYS)
470 return ret;
471 }
472
473 return 0;
474}
475
Simon Glassf26c8a82015-06-23 15:39:15 -0600476UCLASS_DRIVER(clk) = {
477 .id = UCLASS_CLK,
478 .name = "clk",
479};