blob: bf1adc09ab15e314d89f40bf5223b6493d5ea8c9 [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>
Lukasz Majewski0c660c22019-06-24 15:50:42 +020016#include <linux/clk-provider.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
Sekhar Norie497fab2019-07-11 14:30:24 +053055 clk->data = 0;
56
Stephen Warren135aa952016-06-17 09:44:00 -060057 return 0;
58}
59
Jagan Teki75f98312019-02-28 00:26:52 +053060static int clk_get_by_index_tail(int ret, ofnode node,
61 struct ofnode_phandle_args *args,
62 const char *list_name, int index,
63 struct clk *clk)
64{
65 struct udevice *dev_clk;
66 const struct clk_ops *ops;
67
68 assert(clk);
69 clk->dev = NULL;
70 if (ret)
71 goto err;
72
73 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
74 if (ret) {
75 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
76 __func__, ret);
77 return ret;
78 }
79
80 clk->dev = dev_clk;
81
82 ops = clk_dev_ops(dev_clk);
83
84 if (ops->of_xlate)
85 ret = ops->of_xlate(clk, args);
86 else
87 ret = clk_of_xlate_default(clk, args);
88 if (ret) {
89 debug("of_xlate() failed: %d\n", ret);
90 return ret;
91 }
92
93 return clk_request(dev_clk, clk);
94err:
95 debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n",
96 __func__, ofnode_get_name(node), list_name, index, ret);
97 return ret;
98}
99
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100100static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
101 int index, struct clk *clk)
Stephen Warren135aa952016-06-17 09:44:00 -0600102{
103 int ret;
Simon Glassaa9bb092017-05-30 21:47:29 -0600104 struct ofnode_phandle_args args;
Stephen Warren135aa952016-06-17 09:44:00 -0600105
106 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
107
108 assert(clk);
Patrice Chotard82a8a662017-07-18 11:57:07 +0200109 clk->dev = NULL;
110
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100111 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
Mario Six268453b2018-01-15 11:06:51 +0100112 index, &args);
Simon Glasse70cc432016-01-20 19:43:02 -0700113 if (ret) {
114 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
115 __func__, ret);
116 return ret;
117 }
118
Wenyou Yang3f56b132016-09-27 11:00:28 +0800119
Jagan Tekidcb63fc2019-02-28 00:26:53 +0530120 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
121 index > 0, clk);
Stephen Warren135aa952016-06-17 09:44:00 -0600122}
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100123
124int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
125{
Jagan Teki75f98312019-02-28 00:26:52 +0530126 struct ofnode_phandle_args args;
127 int ret;
128
129 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
130 index, &args);
131
132 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
133 index > 0, clk);
134}
135
136int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
137{
138 struct ofnode_phandle_args args;
139 int ret;
140
141 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
142 index > 0, &args);
143
144 return clk_get_by_index_tail(ret, node, &args, "clocks",
145 index > 0, clk);
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100146}
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100147
Neil Armstronga855be82018-04-03 11:44:18 +0200148int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
149{
150 int i, ret, err, count;
151
152 bulk->count = 0;
153
154 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
Neil Armstrong721881c2018-04-17 11:30:31 +0200155 if (count < 1)
156 return count;
Neil Armstronga855be82018-04-03 11:44:18 +0200157
158 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
159 if (!bulk->clks)
160 return -ENOMEM;
161
162 for (i = 0; i < count; i++) {
163 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
164 if (ret < 0)
165 goto bulk_get_err;
166
167 ++bulk->count;
168 }
169
170 return 0;
171
172bulk_get_err:
173 err = clk_release_all(bulk->clks, bulk->count);
174 if (err)
175 debug("%s: could release all clocks for %p\n",
176 __func__, dev);
177
178 return ret;
179}
180
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100181static int clk_set_default_parents(struct udevice *dev)
182{
183 struct clk clk, parent_clk;
184 int index;
185 int num_parents;
186 int ret;
187
188 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
189 "#clock-cells");
190 if (num_parents < 0) {
191 debug("%s: could not read assigned-clock-parents for %p\n",
192 __func__, dev);
193 return 0;
194 }
195
196 for (index = 0; index < num_parents; index++) {
197 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
198 index, &parent_clk);
Neil Armstrongd64caaf2018-07-26 15:19:32 +0200199 /* If -ENOENT, this is a no-op entry */
200 if (ret == -ENOENT)
201 continue;
202
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100203 if (ret) {
204 debug("%s: could not get parent clock %d for %s\n",
205 __func__, index, dev_read_name(dev));
206 return ret;
207 }
208
209 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
210 index, &clk);
211 if (ret) {
212 debug("%s: could not get assigned clock %d for %s\n",
213 __func__, index, dev_read_name(dev));
214 return ret;
215 }
216
217 ret = clk_set_parent(&clk, &parent_clk);
218
219 /*
220 * Not all drivers may support clock-reparenting (as of now).
221 * Ignore errors due to this.
222 */
223 if (ret == -ENOSYS)
224 continue;
225
Jean-Jacques Hiblot02e2a2a2019-09-26 15:42:42 +0200226 if (ret < 0) {
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100227 debug("%s: failed to reparent clock %d for %s\n",
228 __func__, index, dev_read_name(dev));
229 return ret;
230 }
231 }
232
233 return 0;
234}
235
236static int clk_set_default_rates(struct udevice *dev)
237{
238 struct clk clk;
239 int index;
240 int num_rates;
241 int size;
242 int ret = 0;
243 u32 *rates = NULL;
244
245 size = dev_read_size(dev, "assigned-clock-rates");
246 if (size < 0)
247 return 0;
248
249 num_rates = size / sizeof(u32);
250 rates = calloc(num_rates, sizeof(u32));
251 if (!rates)
252 return -ENOMEM;
253
254 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
255 if (ret)
256 goto fail;
257
258 for (index = 0; index < num_rates; index++) {
Neil Armstrongd64caaf2018-07-26 15:19:32 +0200259 /* If 0 is passed, this is a no-op */
260 if (!rates[index])
261 continue;
262
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100263 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
264 index, &clk);
265 if (ret) {
266 debug("%s: could not get assigned clock %d for %s\n",
267 __func__, index, dev_read_name(dev));
268 continue;
269 }
270
271 ret = clk_set_rate(&clk, rates[index]);
272 if (ret < 0) {
Simon Glass68316162019-01-21 14:53:19 -0700273 debug("%s: failed to set rate on clock index %d (%ld) for %s\n",
274 __func__, index, clk.id, dev_read_name(dev));
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100275 break;
276 }
277 }
278
279fail:
280 free(rates);
281 return ret;
282}
283
284int clk_set_defaults(struct udevice *dev)
285{
286 int ret;
287
Peng Fan91944ef2019-07-31 07:01:49 +0000288 if (!dev_of_valid(dev))
289 return 0;
290
Philipp Tomsich291da962018-11-26 20:20:19 +0100291 /* If this not in SPL and pre-reloc state, don't take any action. */
292 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
293 return 0;
294
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100295 debug("%s(%s)\n", __func__, dev_read_name(dev));
296
297 ret = clk_set_default_parents(dev);
298 if (ret)
299 return ret;
300
301 ret = clk_set_default_rates(dev);
302 if (ret < 0)
303 return ret;
304
305 return 0;
306}
Michal Simek9e0758b2016-07-14 13:11:37 +0200307# endif /* OF_PLATDATA */
Stephen Warren135aa952016-06-17 09:44:00 -0600308
309int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
310{
311 int index;
312
313 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
Patrice Chotard82a8a662017-07-18 11:57:07 +0200314 clk->dev = NULL;
Stephen Warren135aa952016-06-17 09:44:00 -0600315
Simon Glassaa9bb092017-05-30 21:47:29 -0600316 index = dev_read_stringlist_search(dev, "clock-names", name);
Stephen Warren135aa952016-06-17 09:44:00 -0600317 if (index < 0) {
Simon Glassb02e4042016-10-02 17:59:28 -0600318 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren135aa952016-06-17 09:44:00 -0600319 return index;
320 }
321
322 return clk_get_by_index(dev, index, clk);
Simon Glasse70cc432016-01-20 19:43:02 -0700323}
Patrice Chotardb108d8a2017-07-25 13:24:45 +0200324
325int clk_release_all(struct clk *clk, int count)
326{
327 int i, ret;
328
329 for (i = 0; i < count; i++) {
330 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
331
332 /* check if clock has been previously requested */
333 if (!clk[i].dev)
334 continue;
335
336 ret = clk_disable(&clk[i]);
337 if (ret && ret != -ENOSYS)
338 return ret;
339
340 ret = clk_free(&clk[i]);
341 if (ret && ret != -ENOSYS)
342 return ret;
343 }
344
345 return 0;
346}
347
Simon Glass7423daa2016-07-04 11:58:03 -0600348#endif /* OF_CONTROL */
Stephen Warren135aa952016-06-17 09:44:00 -0600349
350int clk_request(struct udevice *dev, struct clk *clk)
351{
Mario Six268453b2018-01-15 11:06:51 +0100352 const struct clk_ops *ops = clk_dev_ops(dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600353
354 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
355
356 clk->dev = dev;
357
358 if (!ops->request)
359 return 0;
360
361 return ops->request(clk);
362}
363
364int clk_free(struct clk *clk)
365{
Mario Six268453b2018-01-15 11:06:51 +0100366 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600367
368 debug("%s(clk=%p)\n", __func__, clk);
369
370 if (!ops->free)
371 return 0;
372
373 return ops->free(clk);
374}
375
376ulong clk_get_rate(struct clk *clk)
377{
Mario Six268453b2018-01-15 11:06:51 +0100378 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600379
380 debug("%s(clk=%p)\n", __func__, clk);
381
382 if (!ops->get_rate)
383 return -ENOSYS;
384
385 return ops->get_rate(clk);
386}
387
Lukasz Majewski0c660c22019-06-24 15:50:42 +0200388struct clk *clk_get_parent(struct clk *clk)
389{
390 struct udevice *pdev;
391 struct clk *pclk;
392
393 debug("%s(clk=%p)\n", __func__, clk);
394
395 pdev = dev_get_parent(clk->dev);
396 pclk = dev_get_clk_ptr(pdev);
397 if (!pclk)
398 return ERR_PTR(-ENODEV);
399
400 return pclk;
401}
402
Lukasz Majewski4aa78302019-06-24 15:50:43 +0200403long long clk_get_parent_rate(struct clk *clk)
404{
405 const struct clk_ops *ops;
406 struct clk *pclk;
407
408 debug("%s(clk=%p)\n", __func__, clk);
409
410 pclk = clk_get_parent(clk);
411 if (IS_ERR(pclk))
412 return -ENODEV;
413
414 ops = clk_dev_ops(pclk->dev);
415 if (!ops->get_rate)
416 return -ENOSYS;
417
Lukasz Majewski1a961c92019-06-24 15:50:46 +0200418 /* Read the 'rate' if not already set or if proper flag set*/
419 if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
Lukasz Majewski4aa78302019-06-24 15:50:43 +0200420 pclk->rate = clk_get_rate(pclk);
421
422 return pclk->rate;
423}
424
Stephen Warren135aa952016-06-17 09:44:00 -0600425ulong clk_set_rate(struct clk *clk, ulong rate)
426{
Mario Six268453b2018-01-15 11:06:51 +0100427 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600428
429 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
430
431 if (!ops->set_rate)
432 return -ENOSYS;
433
434 return ops->set_rate(clk, rate);
435}
436
Philipp Tomsichf7d10462018-01-08 11:15:08 +0100437int clk_set_parent(struct clk *clk, struct clk *parent)
438{
439 const struct clk_ops *ops = clk_dev_ops(clk->dev);
440
441 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
442
443 if (!ops->set_parent)
444 return -ENOSYS;
445
446 return ops->set_parent(clk, parent);
447}
448
Stephen Warren135aa952016-06-17 09:44:00 -0600449int clk_enable(struct clk *clk)
450{
Mario Six268453b2018-01-15 11:06:51 +0100451 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Peng Fan0520be02019-08-21 13:35:09 +0000452 struct clk *clkp = NULL;
453 int ret;
Stephen Warren135aa952016-06-17 09:44:00 -0600454
455 debug("%s(clk=%p)\n", __func__, clk);
456
Peng Fan0520be02019-08-21 13:35:09 +0000457 if (CONFIG_IS_ENABLED(CLK_CCF)) {
458 /* Take id 0 as a non-valid clk, such as dummy */
459 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
460 if (clkp->enable_count) {
461 clkp->enable_count++;
462 return 0;
463 }
464 if (clkp->dev->parent &&
465 device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
466 ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
467 if (ret) {
468 printf("Enable %s failed\n",
469 clkp->dev->parent->name);
470 return ret;
471 }
472 }
473 }
Stephen Warren135aa952016-06-17 09:44:00 -0600474
Peng Fan0520be02019-08-21 13:35:09 +0000475 if (ops->enable) {
476 ret = ops->enable(clk);
477 if (ret) {
478 printf("Enable %s failed\n", clk->dev->name);
479 return ret;
480 }
481 }
482 if (clkp)
483 clkp->enable_count++;
484 } else {
485 if (!ops->enable)
486 return -ENOSYS;
487 return ops->enable(clk);
488 }
489
490 return 0;
Stephen Warren135aa952016-06-17 09:44:00 -0600491}
492
Neil Armstronga855be82018-04-03 11:44:18 +0200493int clk_enable_bulk(struct clk_bulk *bulk)
494{
495 int i, ret;
496
497 for (i = 0; i < bulk->count; i++) {
498 ret = clk_enable(&bulk->clks[i]);
499 if (ret < 0 && ret != -ENOSYS)
500 return ret;
501 }
502
503 return 0;
504}
505
Stephen Warren135aa952016-06-17 09:44:00 -0600506int clk_disable(struct clk *clk)
507{
Mario Six268453b2018-01-15 11:06:51 +0100508 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Peng Fan0520be02019-08-21 13:35:09 +0000509 struct clk *clkp = NULL;
510 int ret;
Stephen Warren135aa952016-06-17 09:44:00 -0600511
512 debug("%s(clk=%p)\n", __func__, clk);
513
Peng Fan0520be02019-08-21 13:35:09 +0000514 if (CONFIG_IS_ENABLED(CLK_CCF)) {
515 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
516 if (clkp->enable_count == 0) {
517 printf("clk %s already disabled\n",
518 clkp->dev->name);
519 return 0;
520 }
Stephen Warren135aa952016-06-17 09:44:00 -0600521
Peng Fan0520be02019-08-21 13:35:09 +0000522 if (--clkp->enable_count > 0)
523 return 0;
524 }
525
526 if (ops->disable) {
527 ret = ops->disable(clk);
528 if (ret)
529 return ret;
530 }
531
532 if (clkp && clkp->dev->parent &&
533 device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
534 ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent));
535 if (ret) {
536 printf("Disable %s failed\n",
537 clkp->dev->parent->name);
538 return ret;
539 }
540 }
541 } else {
542 if (!ops->disable)
543 return -ENOSYS;
544
545 return ops->disable(clk);
546 }
547
548 return 0;
Stephen Warren135aa952016-06-17 09:44:00 -0600549}
Simon Glasse70cc432016-01-20 19:43:02 -0700550
Neil Armstronga855be82018-04-03 11:44:18 +0200551int clk_disable_bulk(struct clk_bulk *bulk)
552{
553 int i, ret;
554
555 for (i = 0; i < bulk->count; i++) {
556 ret = clk_disable(&bulk->clks[i]);
557 if (ret < 0 && ret != -ENOSYS)
558 return ret;
559 }
560
561 return 0;
562}
563
Lukasz Majewski2796af72019-06-24 15:50:44 +0200564int clk_get_by_id(ulong id, struct clk **clkp)
565{
566 struct udevice *dev;
567 struct uclass *uc;
568 int ret;
569
570 ret = uclass_get(UCLASS_CLK, &uc);
571 if (ret)
572 return ret;
573
574 uclass_foreach_dev(dev, uc) {
575 struct clk *clk = dev_get_clk_ptr(dev);
576
577 if (clk && clk->id == id) {
578 *clkp = clk;
579 return 0;
580 }
581 }
582
583 return -ENOENT;
584}
585
Sekhar Noriacbb7cd2019-08-01 19:12:55 +0530586bool clk_is_match(const struct clk *p, const struct clk *q)
587{
588 /* trivial case: identical struct clk's or both NULL */
589 if (p == q)
590 return true;
591
592 /* same device, id and data */
593 if (p->dev == q->dev && p->id == q->id && p->data == q->data)
594 return true;
595
596 return false;
597}
598
Simon Glassf26c8a82015-06-23 15:39:15 -0600599UCLASS_DRIVER(clk) = {
600 .id = UCLASS_CLK,
601 .name = "clk",
602};