blob: 899b2dda6f318e26d865fdfc60288e6ff5c356ec [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
55 return 0;
56}
57
Jagan Teki75f98312019-02-28 00:26:52 +053058static int clk_get_by_index_tail(int ret, ofnode node,
59 struct ofnode_phandle_args *args,
60 const char *list_name, int index,
61 struct clk *clk)
62{
63 struct udevice *dev_clk;
64 const struct clk_ops *ops;
65
66 assert(clk);
67 clk->dev = NULL;
68 if (ret)
69 goto err;
70
71 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
72 if (ret) {
73 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
74 __func__, ret);
75 return ret;
76 }
77
78 clk->dev = dev_clk;
79
80 ops = clk_dev_ops(dev_clk);
81
82 if (ops->of_xlate)
83 ret = ops->of_xlate(clk, args);
84 else
85 ret = clk_of_xlate_default(clk, args);
86 if (ret) {
87 debug("of_xlate() failed: %d\n", ret);
88 return ret;
89 }
90
91 return clk_request(dev_clk, clk);
92err:
93 debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n",
94 __func__, ofnode_get_name(node), list_name, index, ret);
95 return ret;
96}
97
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +010098static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
99 int index, struct clk *clk)
Stephen Warren135aa952016-06-17 09:44:00 -0600100{
101 int ret;
Simon Glassaa9bb092017-05-30 21:47:29 -0600102 struct ofnode_phandle_args args;
Stephen Warren135aa952016-06-17 09:44:00 -0600103
104 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
105
106 assert(clk);
Patrice Chotard82a8a662017-07-18 11:57:07 +0200107 clk->dev = NULL;
108
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100109 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
Mario Six268453b2018-01-15 11:06:51 +0100110 index, &args);
Simon Glasse70cc432016-01-20 19:43:02 -0700111 if (ret) {
112 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
113 __func__, ret);
114 return ret;
115 }
116
Wenyou Yang3f56b132016-09-27 11:00:28 +0800117
Jagan Tekidcb63fc2019-02-28 00:26:53 +0530118 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
119 index > 0, clk);
Stephen Warren135aa952016-06-17 09:44:00 -0600120}
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100121
122int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
123{
Jagan Teki75f98312019-02-28 00:26:52 +0530124 struct ofnode_phandle_args args;
125 int ret;
126
127 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
128 index, &args);
129
130 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
131 index > 0, clk);
132}
133
134int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
135{
136 struct ofnode_phandle_args args;
137 int ret;
138
139 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
140 index > 0, &args);
141
142 return clk_get_by_index_tail(ret, node, &args, "clocks",
143 index > 0, clk);
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100144}
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100145
Neil Armstronga855be82018-04-03 11:44:18 +0200146int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
147{
148 int i, ret, err, count;
149
150 bulk->count = 0;
151
152 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
Neil Armstrong721881c2018-04-17 11:30:31 +0200153 if (count < 1)
154 return count;
Neil Armstronga855be82018-04-03 11:44:18 +0200155
156 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
157 if (!bulk->clks)
158 return -ENOMEM;
159
160 for (i = 0; i < count; i++) {
161 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
162 if (ret < 0)
163 goto bulk_get_err;
164
165 ++bulk->count;
166 }
167
168 return 0;
169
170bulk_get_err:
171 err = clk_release_all(bulk->clks, bulk->count);
172 if (err)
173 debug("%s: could release all clocks for %p\n",
174 __func__, dev);
175
176 return ret;
177}
178
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100179static int clk_set_default_parents(struct udevice *dev)
180{
181 struct clk clk, parent_clk;
182 int index;
183 int num_parents;
184 int ret;
185
186 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
187 "#clock-cells");
188 if (num_parents < 0) {
189 debug("%s: could not read assigned-clock-parents for %p\n",
190 __func__, dev);
191 return 0;
192 }
193
194 for (index = 0; index < num_parents; index++) {
195 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
196 index, &parent_clk);
Neil Armstrongd64caaf2018-07-26 15:19:32 +0200197 /* If -ENOENT, this is a no-op entry */
198 if (ret == -ENOENT)
199 continue;
200
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100201 if (ret) {
202 debug("%s: could not get parent clock %d for %s\n",
203 __func__, index, dev_read_name(dev));
204 return ret;
205 }
206
207 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
208 index, &clk);
209 if (ret) {
210 debug("%s: could not get assigned clock %d for %s\n",
211 __func__, index, dev_read_name(dev));
212 return ret;
213 }
214
215 ret = clk_set_parent(&clk, &parent_clk);
216
217 /*
218 * Not all drivers may support clock-reparenting (as of now).
219 * Ignore errors due to this.
220 */
221 if (ret == -ENOSYS)
222 continue;
223
224 if (ret) {
225 debug("%s: failed to reparent clock %d for %s\n",
226 __func__, index, dev_read_name(dev));
227 return ret;
228 }
229 }
230
231 return 0;
232}
233
234static int clk_set_default_rates(struct udevice *dev)
235{
236 struct clk clk;
237 int index;
238 int num_rates;
239 int size;
240 int ret = 0;
241 u32 *rates = NULL;
242
243 size = dev_read_size(dev, "assigned-clock-rates");
244 if (size < 0)
245 return 0;
246
247 num_rates = size / sizeof(u32);
248 rates = calloc(num_rates, sizeof(u32));
249 if (!rates)
250 return -ENOMEM;
251
252 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
253 if (ret)
254 goto fail;
255
256 for (index = 0; index < num_rates; index++) {
Neil Armstrongd64caaf2018-07-26 15:19:32 +0200257 /* If 0 is passed, this is a no-op */
258 if (!rates[index])
259 continue;
260
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100261 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
262 index, &clk);
263 if (ret) {
264 debug("%s: could not get assigned clock %d for %s\n",
265 __func__, index, dev_read_name(dev));
266 continue;
267 }
268
269 ret = clk_set_rate(&clk, rates[index]);
270 if (ret < 0) {
Simon Glass68316162019-01-21 14:53:19 -0700271 debug("%s: failed to set rate on clock index %d (%ld) for %s\n",
272 __func__, index, clk.id, dev_read_name(dev));
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100273 break;
274 }
275 }
276
277fail:
278 free(rates);
279 return ret;
280}
281
282int clk_set_defaults(struct udevice *dev)
283{
284 int ret;
285
Philipp Tomsich291da962018-11-26 20:20:19 +0100286 /* If this not in SPL and pre-reloc state, don't take any action. */
287 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
288 return 0;
289
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100290 debug("%s(%s)\n", __func__, dev_read_name(dev));
291
292 ret = clk_set_default_parents(dev);
293 if (ret)
294 return ret;
295
296 ret = clk_set_default_rates(dev);
297 if (ret < 0)
298 return ret;
299
300 return 0;
301}
Michal Simek9e0758b2016-07-14 13:11:37 +0200302# endif /* OF_PLATDATA */
Stephen Warren135aa952016-06-17 09:44:00 -0600303
304int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
305{
306 int index;
307
308 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
Patrice Chotard82a8a662017-07-18 11:57:07 +0200309 clk->dev = NULL;
Stephen Warren135aa952016-06-17 09:44:00 -0600310
Simon Glassaa9bb092017-05-30 21:47:29 -0600311 index = dev_read_stringlist_search(dev, "clock-names", name);
Stephen Warren135aa952016-06-17 09:44:00 -0600312 if (index < 0) {
Simon Glassb02e4042016-10-02 17:59:28 -0600313 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren135aa952016-06-17 09:44:00 -0600314 return index;
315 }
316
317 return clk_get_by_index(dev, index, clk);
Simon Glasse70cc432016-01-20 19:43:02 -0700318}
Patrice Chotardb108d8a2017-07-25 13:24:45 +0200319
320int clk_release_all(struct clk *clk, int count)
321{
322 int i, ret;
323
324 for (i = 0; i < count; i++) {
325 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
326
327 /* check if clock has been previously requested */
328 if (!clk[i].dev)
329 continue;
330
331 ret = clk_disable(&clk[i]);
332 if (ret && ret != -ENOSYS)
333 return ret;
334
335 ret = clk_free(&clk[i]);
336 if (ret && ret != -ENOSYS)
337 return ret;
338 }
339
340 return 0;
341}
342
Simon Glass7423daa2016-07-04 11:58:03 -0600343#endif /* OF_CONTROL */
Stephen Warren135aa952016-06-17 09:44:00 -0600344
345int clk_request(struct udevice *dev, struct clk *clk)
346{
Mario Six268453b2018-01-15 11:06:51 +0100347 const struct clk_ops *ops = clk_dev_ops(dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600348
349 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
350
351 clk->dev = dev;
352
353 if (!ops->request)
354 return 0;
355
356 return ops->request(clk);
357}
358
359int clk_free(struct clk *clk)
360{
Mario Six268453b2018-01-15 11:06:51 +0100361 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600362
363 debug("%s(clk=%p)\n", __func__, clk);
364
365 if (!ops->free)
366 return 0;
367
368 return ops->free(clk);
369}
370
371ulong clk_get_rate(struct clk *clk)
372{
Mario Six268453b2018-01-15 11:06:51 +0100373 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600374
375 debug("%s(clk=%p)\n", __func__, clk);
376
377 if (!ops->get_rate)
378 return -ENOSYS;
379
380 return ops->get_rate(clk);
381}
382
Lukasz Majewski0c660c22019-06-24 15:50:42 +0200383struct clk *clk_get_parent(struct clk *clk)
384{
385 struct udevice *pdev;
386 struct clk *pclk;
387
388 debug("%s(clk=%p)\n", __func__, clk);
389
390 pdev = dev_get_parent(clk->dev);
391 pclk = dev_get_clk_ptr(pdev);
392 if (!pclk)
393 return ERR_PTR(-ENODEV);
394
395 return pclk;
396}
397
Lukasz Majewski4aa78302019-06-24 15:50:43 +0200398long long clk_get_parent_rate(struct clk *clk)
399{
400 const struct clk_ops *ops;
401 struct clk *pclk;
402
403 debug("%s(clk=%p)\n", __func__, clk);
404
405 pclk = clk_get_parent(clk);
406 if (IS_ERR(pclk))
407 return -ENODEV;
408
409 ops = clk_dev_ops(pclk->dev);
410 if (!ops->get_rate)
411 return -ENOSYS;
412
413 /* Read the 'rate' if not already set */
414 if (!pclk->rate)
415 pclk->rate = clk_get_rate(pclk);
416
417 return pclk->rate;
418}
419
Stephen Warren135aa952016-06-17 09:44:00 -0600420ulong clk_set_rate(struct clk *clk, ulong rate)
421{
Mario Six268453b2018-01-15 11:06:51 +0100422 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600423
424 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
425
426 if (!ops->set_rate)
427 return -ENOSYS;
428
429 return ops->set_rate(clk, rate);
430}
431
Philipp Tomsichf7d10462018-01-08 11:15:08 +0100432int clk_set_parent(struct clk *clk, struct clk *parent)
433{
434 const struct clk_ops *ops = clk_dev_ops(clk->dev);
435
436 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
437
438 if (!ops->set_parent)
439 return -ENOSYS;
440
441 return ops->set_parent(clk, parent);
442}
443
Stephen Warren135aa952016-06-17 09:44:00 -0600444int clk_enable(struct clk *clk)
445{
Mario Six268453b2018-01-15 11:06:51 +0100446 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600447
448 debug("%s(clk=%p)\n", __func__, clk);
449
450 if (!ops->enable)
451 return -ENOSYS;
452
453 return ops->enable(clk);
454}
455
Neil Armstronga855be82018-04-03 11:44:18 +0200456int clk_enable_bulk(struct clk_bulk *bulk)
457{
458 int i, ret;
459
460 for (i = 0; i < bulk->count; i++) {
461 ret = clk_enable(&bulk->clks[i]);
462 if (ret < 0 && ret != -ENOSYS)
463 return ret;
464 }
465
466 return 0;
467}
468
Stephen Warren135aa952016-06-17 09:44:00 -0600469int clk_disable(struct clk *clk)
470{
Mario Six268453b2018-01-15 11:06:51 +0100471 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600472
473 debug("%s(clk=%p)\n", __func__, clk);
474
475 if (!ops->disable)
476 return -ENOSYS;
477
478 return ops->disable(clk);
479}
Simon Glasse70cc432016-01-20 19:43:02 -0700480
Neil Armstronga855be82018-04-03 11:44:18 +0200481int clk_disable_bulk(struct clk_bulk *bulk)
482{
483 int i, ret;
484
485 for (i = 0; i < bulk->count; i++) {
486 ret = clk_disable(&bulk->clks[i]);
487 if (ret < 0 && ret != -ENOSYS)
488 return ret;
489 }
490
491 return 0;
492}
493
Simon Glassf26c8a82015-06-23 15:39:15 -0600494UCLASS_DRIVER(clk) = {
495 .id = UCLASS_CLK,
496 .name = "clk",
497};