blob: f186fcbcdb868a5c9d6c17d64924649a1efad01a [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
Patrick Delaunayb953ec22021-04-27 11:02:19 +02009#define LOG_CATEGORY UCLASS_CLK
10
Simon Glassf26c8a82015-06-23 15:39:15 -060011#include <common.h>
12#include <clk.h>
Stephen Warren135aa952016-06-17 09:44:00 -060013#include <clk-uclass.h>
Simon Glassf26c8a82015-06-23 15:39:15 -060014#include <dm.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 Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070018#include <malloc.h>
Patrick Delaunay572c4462021-11-19 15:12:06 +010019#include <asm/global_data.h>
Sean Anderson8c12cb32021-04-08 22:13:03 -040020#include <dm/device_compat.h>
Claudiu Beznea4d139f32020-09-07 17:46:34 +030021#include <dm/device-internal.h>
Simon Glass61b29b82020-02-03 07:36:15 -070022#include <dm/devres.h>
23#include <dm/read.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060024#include <linux/bug.h>
Lukasz Majewski0c660c22019-06-24 15:50:42 +020025#include <linux/clk-provider.h>
Simon Glass61b29b82020-02-03 07:36:15 -070026#include <linux/err.h>
Simon Glassf26c8a82015-06-23 15:39:15 -060027
Mario Six268453b2018-01-15 11:06:51 +010028static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
Simon Glassf26c8a82015-06-23 15:39:15 -060029{
Mario Six268453b2018-01-15 11:06:51 +010030 return (const struct clk_ops *)dev->driver->ops;
Simon Glassf26c8a82015-06-23 15:39:15 -060031}
32
Simon Glassfb989e02020-07-19 10:15:56 -060033struct clk *dev_get_clk_ptr(struct udevice *dev)
34{
35 return (struct clk *)dev_get_uclass_priv(dev);
36}
37
Simon Glass414cc152021-08-07 07:24:03 -060038#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassf0ab8f92021-08-07 07:24:09 -060039int clk_get_by_phandle(struct udevice *dev, const struct phandle_1_arg *cells,
40 struct clk *clk)
Simon Glass7423daa2016-07-04 11:58:03 -060041{
42 int ret;
43
Simon Glasscc469b72021-03-15 17:25:28 +130044 ret = device_get_by_ofplat_idx(cells->idx, &clk->dev);
Simon Glass7423daa2016-07-04 11:58:03 -060045 if (ret)
46 return ret;
Walter Lozano51f12632020-06-25 01:10:13 -030047 clk->id = cells->arg[0];
Simon Glass7423daa2016-07-04 11:58:03 -060048
49 return 0;
50}
Simon Glass414cc152021-08-07 07:24:03 -060051#endif
52
53#if CONFIG_IS_ENABLED(OF_REAL)
Stephen Warren135aa952016-06-17 09:44:00 -060054static int clk_of_xlate_default(struct clk *clk,
Simon Glassa4e0ef52017-05-18 20:09:40 -060055 struct ofnode_phandle_args *args)
Stephen Warren135aa952016-06-17 09:44:00 -060056{
57 debug("%s(clk=%p)\n", __func__, clk);
58
59 if (args->args_count > 1) {
Sean Anderson46ad7ce2021-12-01 14:26:53 -050060 debug("Invalid args_count: %d\n", args->args_count);
Stephen Warren135aa952016-06-17 09:44:00 -060061 return -EINVAL;
62 }
63
64 if (args->args_count)
65 clk->id = args->args[0];
66 else
67 clk->id = 0;
68
Sekhar Norie497fab2019-07-11 14:30:24 +053069 clk->data = 0;
70
Stephen Warren135aa952016-06-17 09:44:00 -060071 return 0;
72}
73
Jagan Teki75f98312019-02-28 00:26:52 +053074static int clk_get_by_index_tail(int ret, ofnode node,
75 struct ofnode_phandle_args *args,
76 const char *list_name, int index,
77 struct clk *clk)
78{
79 struct udevice *dev_clk;
80 const struct clk_ops *ops;
81
82 assert(clk);
83 clk->dev = NULL;
84 if (ret)
85 goto err;
86
87 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
88 if (ret) {
89 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
90 __func__, ret);
Simon Glass5c5992c2021-01-21 13:57:11 -070091 return log_msg_ret("get", ret);
Jagan Teki75f98312019-02-28 00:26:52 +053092 }
93
94 clk->dev = dev_clk;
95
96 ops = clk_dev_ops(dev_clk);
97
98 if (ops->of_xlate)
99 ret = ops->of_xlate(clk, args);
100 else
101 ret = clk_of_xlate_default(clk, args);
102 if (ret) {
103 debug("of_xlate() failed: %d\n", ret);
Simon Glass5c5992c2021-01-21 13:57:11 -0700104 return log_msg_ret("xlate", ret);
Jagan Teki75f98312019-02-28 00:26:52 +0530105 }
106
107 return clk_request(dev_clk, clk);
108err:
109 debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n",
110 __func__, ofnode_get_name(node), list_name, index, ret);
Simon Glass5c5992c2021-01-21 13:57:11 -0700111
112 return log_msg_ret("prop", ret);
Jagan Teki75f98312019-02-28 00:26:52 +0530113}
114
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100115static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
116 int index, struct clk *clk)
Stephen Warren135aa952016-06-17 09:44:00 -0600117{
118 int ret;
Simon Glassaa9bb092017-05-30 21:47:29 -0600119 struct ofnode_phandle_args args;
Stephen Warren135aa952016-06-17 09:44:00 -0600120
121 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
122
123 assert(clk);
Patrice Chotard82a8a662017-07-18 11:57:07 +0200124 clk->dev = NULL;
125
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100126 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
Mario Six268453b2018-01-15 11:06:51 +0100127 index, &args);
Simon Glasse70cc432016-01-20 19:43:02 -0700128 if (ret) {
129 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
130 __func__, ret);
Simon Glass5c5992c2021-01-21 13:57:11 -0700131 return log_ret(ret);
Simon Glasse70cc432016-01-20 19:43:02 -0700132 }
133
Wenyou Yang3f56b132016-09-27 11:00:28 +0800134
Jagan Tekidcb63fc2019-02-28 00:26:53 +0530135 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
Sean Anderson675d7902020-06-24 06:41:08 -0400136 index, clk);
Stephen Warren135aa952016-06-17 09:44:00 -0600137}
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100138
139int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
140{
Sean Andersone7075ff2022-02-27 14:01:13 -0500141 return clk_get_by_index_nodev(dev_ofnode(dev), index, clk);
Jagan Teki75f98312019-02-28 00:26:52 +0530142}
143
144int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
145{
146 struct ofnode_phandle_args args;
147 int ret;
148
149 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
Sean Anderson675d7902020-06-24 06:41:08 -0400150 index, &args);
Jagan Teki75f98312019-02-28 00:26:52 +0530151
152 return clk_get_by_index_tail(ret, node, &args, "clocks",
Sean Anderson675d7902020-06-24 06:41:08 -0400153 index, clk);
Philipp Tomsich95f9a7e2018-01-08 11:18:18 +0100154}
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100155
Neil Armstronga855be82018-04-03 11:44:18 +0200156int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
157{
158 int i, ret, err, count;
Patrick Delaunayc2625222021-04-27 10:57:54 +0200159
Neil Armstronga855be82018-04-03 11:44:18 +0200160 bulk->count = 0;
161
Patrick Delaunay89f68302020-09-25 09:41:14 +0200162 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", 0);
Neil Armstrong721881c2018-04-17 11:30:31 +0200163 if (count < 1)
164 return count;
Neil Armstronga855be82018-04-03 11:44:18 +0200165
166 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
167 if (!bulk->clks)
168 return -ENOMEM;
169
170 for (i = 0; i < count; i++) {
171 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
172 if (ret < 0)
173 goto bulk_get_err;
174
175 ++bulk->count;
176 }
177
178 return 0;
179
180bulk_get_err:
181 err = clk_release_all(bulk->clks, bulk->count);
182 if (err)
183 debug("%s: could release all clocks for %p\n",
184 __func__, dev);
185
186 return ret;
187}
188
Claudiu Bezneab3641342020-09-07 17:46:36 +0300189static struct clk *clk_set_default_get_by_id(struct clk *clk)
190{
191 struct clk *c = clk;
192
193 if (CONFIG_IS_ENABLED(CLK_CCF)) {
194 int ret = clk_get_by_id(clk->id, &c);
195
196 if (ret) {
197 debug("%s(): could not get parent clock pointer, id %lu\n",
198 __func__, clk->id);
199 ERR_PTR(ret);
200 }
201 }
202
203 return c;
204}
205
Sean Anderson6e33eba2021-06-11 00:16:07 -0400206static int clk_set_default_parents(struct udevice *dev,
207 enum clk_defaults_stage stage)
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100208{
Claudiu Bezneab3641342020-09-07 17:46:36 +0300209 struct clk clk, parent_clk, *c, *p;
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100210 int index;
211 int num_parents;
212 int ret;
213
214 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
Patrick Delaunay89f68302020-09-25 09:41:14 +0200215 "#clock-cells", 0);
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100216 if (num_parents < 0) {
217 debug("%s: could not read assigned-clock-parents for %p\n",
218 __func__, dev);
219 return 0;
220 }
221
222 for (index = 0; index < num_parents; index++) {
223 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
224 index, &parent_clk);
Neil Armstrongd64caaf2018-07-26 15:19:32 +0200225 /* If -ENOENT, this is a no-op entry */
226 if (ret == -ENOENT)
227 continue;
228
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100229 if (ret) {
230 debug("%s: could not get parent clock %d for %s\n",
231 __func__, index, dev_read_name(dev));
232 return ret;
233 }
234
Claudiu Bezneab3641342020-09-07 17:46:36 +0300235 p = clk_set_default_get_by_id(&parent_clk);
236 if (IS_ERR(p))
237 return PTR_ERR(p);
238
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100239 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
240 index, &clk);
Tero Kristo1e1fab02021-06-11 11:45:11 +0300241 /*
242 * If the clock provider is not ready yet, let it handle
243 * the re-programming later.
244 */
245 if (ret == -EPROBE_DEFER) {
246 ret = 0;
247 continue;
248 }
249
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100250 if (ret) {
251 debug("%s: could not get assigned clock %d for %s\n",
252 __func__, index, dev_read_name(dev));
253 return ret;
254 }
255
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200256 /* This is clk provider device trying to reparent itself
257 * It cannot be done right now but need to wait after the
258 * device is probed
259 */
Sean Anderson6e33eba2021-06-11 00:16:07 -0400260 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200261 continue;
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100262
Sean Anderson6e33eba2021-06-11 00:16:07 -0400263 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200264 /* do not setup twice the parent clocks */
265 continue;
266
Claudiu Bezneab3641342020-09-07 17:46:36 +0300267 c = clk_set_default_get_by_id(&clk);
268 if (IS_ERR(c))
269 return PTR_ERR(c);
270
271 ret = clk_set_parent(c, p);
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100272 /*
273 * Not all drivers may support clock-reparenting (as of now).
274 * Ignore errors due to this.
275 */
276 if (ret == -ENOSYS)
277 continue;
278
Jean-Jacques Hiblot02e2a2a2019-09-26 15:42:42 +0200279 if (ret < 0) {
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100280 debug("%s: failed to reparent clock %d for %s\n",
281 __func__, index, dev_read_name(dev));
282 return ret;
283 }
284 }
285
286 return 0;
287}
288
Sean Anderson6e33eba2021-06-11 00:16:07 -0400289static int clk_set_default_rates(struct udevice *dev,
290 enum clk_defaults_stage stage)
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100291{
Claudiu Bezneab3641342020-09-07 17:46:36 +0300292 struct clk clk, *c;
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100293 int index;
294 int num_rates;
295 int size;
296 int ret = 0;
297 u32 *rates = NULL;
298
299 size = dev_read_size(dev, "assigned-clock-rates");
300 if (size < 0)
301 return 0;
302
303 num_rates = size / sizeof(u32);
304 rates = calloc(num_rates, sizeof(u32));
305 if (!rates)
306 return -ENOMEM;
307
308 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
309 if (ret)
310 goto fail;
311
312 for (index = 0; index < num_rates; index++) {
Neil Armstrongd64caaf2018-07-26 15:19:32 +0200313 /* If 0 is passed, this is a no-op */
314 if (!rates[index])
315 continue;
316
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100317 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
318 index, &clk);
Tero Kristo1e1fab02021-06-11 11:45:11 +0300319 /*
320 * If the clock provider is not ready yet, let it handle
321 * the re-programming later.
322 */
323 if (ret == -EPROBE_DEFER) {
324 ret = 0;
325 continue;
326 }
327
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100328 if (ret) {
Sean Anderson8c12cb32021-04-08 22:13:03 -0400329 dev_dbg(dev,
330 "could not get assigned clock %d (err = %d)\n",
331 index, ret);
Ashok Reddy Soma99b46472023-08-30 10:31:42 +0200332 /* Skip if it is empty */
333 if (ret == -ENOENT) {
334 ret = 0;
335 continue;
336 }
337
338 return ret;
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100339 }
340
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200341 /* This is clk provider device trying to program itself
342 * It cannot be done right now but need to wait after the
343 * device is probed
344 */
Sean Anderson6e33eba2021-06-11 00:16:07 -0400345 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200346 continue;
347
Sean Anderson6e33eba2021-06-11 00:16:07 -0400348 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200349 /* do not setup twice the parent clocks */
350 continue;
351
Claudiu Bezneab3641342020-09-07 17:46:36 +0300352 c = clk_set_default_get_by_id(&clk);
353 if (IS_ERR(c))
354 return PTR_ERR(c);
355
356 ret = clk_set_rate(c, rates[index]);
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200357
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100358 if (ret < 0) {
Sean Anderson8c12cb32021-04-08 22:13:03 -0400359 dev_warn(dev,
360 "failed to set rate on clock index %d (%ld) (error = %d)\n",
361 index, clk.id, ret);
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100362 break;
363 }
364 }
365
366fail:
367 free(rates);
368 return ret;
369}
370
Sean Anderson6e33eba2021-06-11 00:16:07 -0400371int clk_set_defaults(struct udevice *dev, enum clk_defaults_stage stage)
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100372{
373 int ret;
374
Simon Glass7d14ee42020-12-19 10:40:13 -0700375 if (!dev_has_ofnode(dev))
Peng Fan91944ef2019-07-31 07:01:49 +0000376 return 0;
377
Sean Anderson6e33eba2021-06-11 00:16:07 -0400378 /*
379 * To avoid setting defaults twice, don't set them before relocation.
380 * However, still set them for SPL. And still set them if explicitly
381 * asked.
382 */
Philipp Tomsich291da962018-11-26 20:20:19 +0100383 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
Sean Anderson6e33eba2021-06-11 00:16:07 -0400384 if (stage != CLK_DEFAULTS_POST_FORCE)
385 return 0;
Philipp Tomsich291da962018-11-26 20:20:19 +0100386
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100387 debug("%s(%s)\n", __func__, dev_read_name(dev));
388
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200389 ret = clk_set_default_parents(dev, stage);
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100390 if (ret)
391 return ret;
392
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200393 ret = clk_set_default_rates(dev, stage);
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100394 if (ret < 0)
395 return ret;
396
397 return 0;
398}
Stephen Warren135aa952016-06-17 09:44:00 -0600399
400int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
401{
Sean Andersone7075ff2022-02-27 14:01:13 -0500402 return clk_get_by_name_nodev(dev_ofnode(dev), name, clk);
Simon Glasse70cc432016-01-20 19:43:02 -0700403}
Simon Glassf0ab8f92021-08-07 07:24:09 -0600404#endif /* OF_REAL */
Patrice Chotardb108d8a2017-07-25 13:24:45 +0200405
Chunfeng Yund6464202020-01-09 11:35:07 +0800406int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
407{
Samuel Holland2050f822023-01-21 18:02:51 -0600408 int index = 0;
Chunfeng Yund6464202020-01-09 11:35:07 +0800409
410 debug("%s(node=%p, name=%s, clk=%p)\n", __func__,
411 ofnode_get_name(node), name, clk);
412 clk->dev = NULL;
413
Samuel Holland2050f822023-01-21 18:02:51 -0600414 if (name) {
415 index = ofnode_stringlist_search(node, "clock-names", name);
416 if (index < 0) {
417 debug("fdt_stringlist_search() failed: %d\n", index);
418 return index;
419 }
Chunfeng Yund6464202020-01-09 11:35:07 +0800420 }
421
422 return clk_get_by_index_nodev(node, index, clk);
423}
424
Patrice Chotardb108d8a2017-07-25 13:24:45 +0200425int clk_release_all(struct clk *clk, int count)
426{
427 int i, ret;
428
429 for (i = 0; i < count; i++) {
430 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
431
432 /* check if clock has been previously requested */
433 if (!clk[i].dev)
434 continue;
435
436 ret = clk_disable(&clk[i]);
437 if (ret && ret != -ENOSYS)
438 return ret;
439
Sean Andersonac15e782022-01-15 17:25:04 -0500440 clk_free(&clk[i]);
Patrice Chotardb108d8a2017-07-25 13:24:45 +0200441 }
442
443 return 0;
444}
445
Stephen Warren135aa952016-06-17 09:44:00 -0600446int clk_request(struct udevice *dev, struct clk *clk)
447{
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200448 const struct clk_ops *ops;
Stephen Warren135aa952016-06-17 09:44:00 -0600449
450 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200451 if (!clk)
452 return 0;
453 ops = clk_dev_ops(dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600454
455 clk->dev = dev;
456
457 if (!ops->request)
458 return 0;
459
460 return ops->request(clk);
461}
462
Sean Andersonac15e782022-01-15 17:25:04 -0500463void clk_free(struct clk *clk)
Stephen Warren135aa952016-06-17 09:44:00 -0600464{
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200465 const struct clk_ops *ops;
Stephen Warren135aa952016-06-17 09:44:00 -0600466
467 debug("%s(clk=%p)\n", __func__, clk);
Chunfeng Yunbd7c7982020-01-09 11:35:06 +0800468 if (!clk_valid(clk))
Sean Andersonac15e782022-01-15 17:25:04 -0500469 return;
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200470 ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600471
Sean Anderson276d4462022-01-15 17:24:58 -0500472 if (ops->rfree)
473 ops->rfree(clk);
Sean Andersonac15e782022-01-15 17:25:04 -0500474 return;
Stephen Warren135aa952016-06-17 09:44:00 -0600475}
476
477ulong clk_get_rate(struct clk *clk)
478{
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200479 const struct clk_ops *ops;
Simon Glass5c5992c2021-01-21 13:57:11 -0700480 int ret;
Stephen Warren135aa952016-06-17 09:44:00 -0600481
482 debug("%s(clk=%p)\n", __func__, clk);
Chunfeng Yunbd7c7982020-01-09 11:35:06 +0800483 if (!clk_valid(clk))
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200484 return 0;
485 ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600486
487 if (!ops->get_rate)
488 return -ENOSYS;
489
Simon Glass5c5992c2021-01-21 13:57:11 -0700490 ret = ops->get_rate(clk);
491 if (ret)
492 return log_ret(ret);
493
494 return 0;
Stephen Warren135aa952016-06-17 09:44:00 -0600495}
496
Lukasz Majewski0c660c22019-06-24 15:50:42 +0200497struct clk *clk_get_parent(struct clk *clk)
498{
499 struct udevice *pdev;
500 struct clk *pclk;
501
502 debug("%s(clk=%p)\n", __func__, clk);
Chunfeng Yunbd7c7982020-01-09 11:35:06 +0800503 if (!clk_valid(clk))
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200504 return NULL;
Lukasz Majewski0c660c22019-06-24 15:50:42 +0200505
506 pdev = dev_get_parent(clk->dev);
Tero Kristo920ea5a2021-06-11 11:45:08 +0300507 if (!pdev)
508 return ERR_PTR(-ENODEV);
Lukasz Majewski0c660c22019-06-24 15:50:42 +0200509 pclk = dev_get_clk_ptr(pdev);
510 if (!pclk)
511 return ERR_PTR(-ENODEV);
512
513 return pclk;
514}
515
Michal Suchaneka1265cd2022-09-28 12:37:57 +0200516ulong clk_get_parent_rate(struct clk *clk)
Lukasz Majewski4aa78302019-06-24 15:50:43 +0200517{
518 const struct clk_ops *ops;
519 struct clk *pclk;
520
521 debug("%s(clk=%p)\n", __func__, clk);
Chunfeng Yunbd7c7982020-01-09 11:35:06 +0800522 if (!clk_valid(clk))
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200523 return 0;
Lukasz Majewski4aa78302019-06-24 15:50:43 +0200524
525 pclk = clk_get_parent(clk);
526 if (IS_ERR(pclk))
527 return -ENODEV;
528
529 ops = clk_dev_ops(pclk->dev);
530 if (!ops->get_rate)
531 return -ENOSYS;
532
Lukasz Majewski1a961c92019-06-24 15:50:46 +0200533 /* Read the 'rate' if not already set or if proper flag set*/
534 if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
Lukasz Majewski4aa78302019-06-24 15:50:43 +0200535 pclk->rate = clk_get_rate(pclk);
536
537 return pclk->rate;
538}
539
Dario Binacchi2983ad52020-12-30 00:06:31 +0100540ulong clk_round_rate(struct clk *clk, ulong rate)
541{
542 const struct clk_ops *ops;
543
544 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
545 if (!clk_valid(clk))
546 return 0;
547
548 ops = clk_dev_ops(clk->dev);
549 if (!ops->round_rate)
550 return -ENOSYS;
551
552 return ops->round_rate(clk, rate);
553}
554
Patrick Delaunay19fb40a2022-06-20 15:37:25 +0200555static void clk_get_priv(struct clk *clk, struct clk **clkp)
556{
557 *clkp = clk;
558
559 /* get private clock struct associated to the provided clock */
560 if (CONFIG_IS_ENABLED(CLK_CCF)) {
561 /* Take id 0 as a non-valid clk, such as dummy */
562 if (clk->id)
563 clk_get_by_id(clk->id, clkp);
564 }
565}
566
567/* clean cache, called with private clock struct */
Tero Kristo6b7fd312021-06-11 11:45:12 +0300568static void clk_clean_rate_cache(struct clk *clk)
569{
570 struct udevice *child_dev;
571 struct clk *clkp;
572
573 if (!clk)
574 return;
575
576 clk->rate = 0;
577
578 list_for_each_entry(child_dev, &clk->dev->child_head, sibling_node) {
579 clkp = dev_get_clk_ptr(child_dev);
580 clk_clean_rate_cache(clkp);
581 }
582}
583
Stephen Warren135aa952016-06-17 09:44:00 -0600584ulong clk_set_rate(struct clk *clk, ulong rate)
585{
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200586 const struct clk_ops *ops;
Patrick Delaunay19fb40a2022-06-20 15:37:25 +0200587 struct clk *clkp;
Stephen Warren135aa952016-06-17 09:44:00 -0600588
589 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
Chunfeng Yunbd7c7982020-01-09 11:35:06 +0800590 if (!clk_valid(clk))
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200591 return 0;
592 ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600593
594 if (!ops->set_rate)
595 return -ENOSYS;
596
Patrick Delaunay19fb40a2022-06-20 15:37:25 +0200597 /* get private clock struct used for cache */
598 clk_get_priv(clk, &clkp);
Tero Kristo6b7fd312021-06-11 11:45:12 +0300599 /* Clean up cached rates for us and all child clocks */
Patrick Delaunay19fb40a2022-06-20 15:37:25 +0200600 clk_clean_rate_cache(clkp);
Tero Kristo6b7fd312021-06-11 11:45:12 +0300601
Stephen Warren135aa952016-06-17 09:44:00 -0600602 return ops->set_rate(clk, rate);
603}
604
Philipp Tomsichf7d10462018-01-08 11:15:08 +0100605int clk_set_parent(struct clk *clk, struct clk *parent)
606{
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200607 const struct clk_ops *ops;
Claudiu Beznea4d139f32020-09-07 17:46:34 +0300608 int ret;
Philipp Tomsichf7d10462018-01-08 11:15:08 +0100609
610 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
Chunfeng Yunbd7c7982020-01-09 11:35:06 +0800611 if (!clk_valid(clk))
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200612 return 0;
613 ops = clk_dev_ops(clk->dev);
Philipp Tomsichf7d10462018-01-08 11:15:08 +0100614
615 if (!ops->set_parent)
616 return -ENOSYS;
617
Claudiu Beznea4d139f32020-09-07 17:46:34 +0300618 ret = ops->set_parent(clk, parent);
619 if (ret)
620 return ret;
621
622 if (CONFIG_IS_ENABLED(CLK_CCF))
623 ret = device_reparent(clk->dev, parent->dev);
624
625 return ret;
Philipp Tomsichf7d10462018-01-08 11:15:08 +0100626}
627
Stephen Warren135aa952016-06-17 09:44:00 -0600628int clk_enable(struct clk *clk)
629{
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200630 const struct clk_ops *ops;
Peng Fan0520be02019-08-21 13:35:09 +0000631 struct clk *clkp = NULL;
632 int ret;
Stephen Warren135aa952016-06-17 09:44:00 -0600633
634 debug("%s(clk=%p)\n", __func__, clk);
Chunfeng Yunbd7c7982020-01-09 11:35:06 +0800635 if (!clk_valid(clk))
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200636 return 0;
637 ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600638
Peng Fan0520be02019-08-21 13:35:09 +0000639 if (CONFIG_IS_ENABLED(CLK_CCF)) {
640 /* Take id 0 as a non-valid clk, such as dummy */
641 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
642 if (clkp->enable_count) {
643 clkp->enable_count++;
644 return 0;
645 }
646 if (clkp->dev->parent &&
Patrick Delaunayb0cdd822022-01-24 14:17:14 +0100647 device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
Peng Fan0520be02019-08-21 13:35:09 +0000648 ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
649 if (ret) {
650 printf("Enable %s failed\n",
651 clkp->dev->parent->name);
652 return ret;
653 }
654 }
655 }
Stephen Warren135aa952016-06-17 09:44:00 -0600656
Peng Fan0520be02019-08-21 13:35:09 +0000657 if (ops->enable) {
658 ret = ops->enable(clk);
659 if (ret) {
660 printf("Enable %s failed\n", clk->dev->name);
661 return ret;
662 }
663 }
664 if (clkp)
665 clkp->enable_count++;
666 } else {
667 if (!ops->enable)
668 return -ENOSYS;
669 return ops->enable(clk);
670 }
671
672 return 0;
Stephen Warren135aa952016-06-17 09:44:00 -0600673}
674
Neil Armstronga855be82018-04-03 11:44:18 +0200675int clk_enable_bulk(struct clk_bulk *bulk)
676{
677 int i, ret;
678
679 for (i = 0; i < bulk->count; i++) {
680 ret = clk_enable(&bulk->clks[i]);
681 if (ret < 0 && ret != -ENOSYS)
682 return ret;
683 }
684
685 return 0;
686}
687
Stephen Warren135aa952016-06-17 09:44:00 -0600688int clk_disable(struct clk *clk)
689{
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200690 const struct clk_ops *ops;
Peng Fan0520be02019-08-21 13:35:09 +0000691 struct clk *clkp = NULL;
692 int ret;
Stephen Warren135aa952016-06-17 09:44:00 -0600693
694 debug("%s(clk=%p)\n", __func__, clk);
Chunfeng Yunbd7c7982020-01-09 11:35:06 +0800695 if (!clk_valid(clk))
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200696 return 0;
697 ops = clk_dev_ops(clk->dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600698
Peng Fan0520be02019-08-21 13:35:09 +0000699 if (CONFIG_IS_ENABLED(CLK_CCF)) {
700 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
Claudiu Beznea9a5d59d2020-09-07 17:46:35 +0300701 if (clkp->flags & CLK_IS_CRITICAL)
702 return 0;
703
Peng Fan0520be02019-08-21 13:35:09 +0000704 if (clkp->enable_count == 0) {
705 printf("clk %s already disabled\n",
706 clkp->dev->name);
707 return 0;
708 }
Stephen Warren135aa952016-06-17 09:44:00 -0600709
Peng Fan0520be02019-08-21 13:35:09 +0000710 if (--clkp->enable_count > 0)
711 return 0;
712 }
713
714 if (ops->disable) {
715 ret = ops->disable(clk);
716 if (ret)
717 return ret;
718 }
719
720 if (clkp && clkp->dev->parent &&
Patrick Delaunayb0cdd822022-01-24 14:17:14 +0100721 device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
Peng Fan0520be02019-08-21 13:35:09 +0000722 ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent));
723 if (ret) {
724 printf("Disable %s failed\n",
725 clkp->dev->parent->name);
726 return ret;
727 }
728 }
729 } else {
730 if (!ops->disable)
731 return -ENOSYS;
732
733 return ops->disable(clk);
734 }
735
736 return 0;
Stephen Warren135aa952016-06-17 09:44:00 -0600737}
Simon Glasse70cc432016-01-20 19:43:02 -0700738
Neil Armstronga855be82018-04-03 11:44:18 +0200739int clk_disable_bulk(struct clk_bulk *bulk)
740{
741 int i, ret;
742
743 for (i = 0; i < bulk->count; i++) {
744 ret = clk_disable(&bulk->clks[i]);
745 if (ret < 0 && ret != -ENOSYS)
746 return ret;
747 }
748
749 return 0;
750}
751
Lukasz Majewski2796af72019-06-24 15:50:44 +0200752int clk_get_by_id(ulong id, struct clk **clkp)
753{
754 struct udevice *dev;
755 struct uclass *uc;
756 int ret;
757
758 ret = uclass_get(UCLASS_CLK, &uc);
759 if (ret)
760 return ret;
761
762 uclass_foreach_dev(dev, uc) {
763 struct clk *clk = dev_get_clk_ptr(dev);
764
765 if (clk && clk->id == id) {
766 *clkp = clk;
767 return 0;
768 }
769 }
770
771 return -ENOENT;
772}
773
Sekhar Noriacbb7cd2019-08-01 19:12:55 +0530774bool clk_is_match(const struct clk *p, const struct clk *q)
775{
776 /* trivial case: identical struct clk's or both NULL */
777 if (p == q)
778 return true;
779
Jean-Jacques Hiblot8a1661f2019-10-22 14:00:03 +0200780 /* trivial case #2: on the clk pointer is NULL */
781 if (!p || !q)
782 return false;
783
Sekhar Noriacbb7cd2019-08-01 19:12:55 +0530784 /* same device, id and data */
785 if (p->dev == q->dev && p->id == q->id && p->data == q->data)
786 return true;
787
788 return false;
789}
790
Jean-Jacques Hiblot52720c52019-10-22 14:00:04 +0200791static void devm_clk_release(struct udevice *dev, void *res)
792{
793 clk_free(res);
794}
795
796static int devm_clk_match(struct udevice *dev, void *res, void *data)
797{
798 return res == data;
799}
800
801struct clk *devm_clk_get(struct udevice *dev, const char *id)
802{
803 int rc;
804 struct clk *clk;
805
806 clk = devres_alloc(devm_clk_release, sizeof(struct clk), __GFP_ZERO);
807 if (unlikely(!clk))
808 return ERR_PTR(-ENOMEM);
809
810 rc = clk_get_by_name(dev, id, clk);
811 if (rc)
812 return ERR_PTR(rc);
813
814 devres_add(dev, clk);
815 return clk;
816}
817
Jean-Jacques Hiblot52720c52019-10-22 14:00:04 +0200818void devm_clk_put(struct udevice *dev, struct clk *clk)
819{
820 int rc;
821
822 if (!clk)
823 return;
824
825 rc = devres_release(dev, devm_clk_release, devm_clk_match, clk);
826 WARN_ON(rc);
827}
828
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200829int clk_uclass_post_probe(struct udevice *dev)
830{
831 /*
832 * when a clock provider is probed. Call clk_set_defaults()
833 * also after the device is probed. This takes care of cases
834 * where the DT is used to setup default parents and rates
835 * using assigned-clocks
836 */
Marek Vasut75f080d2022-01-01 19:51:39 +0100837 clk_set_defaults(dev, CLK_DEFAULTS_POST);
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200838
839 return 0;
840}
841
Simon Glassf26c8a82015-06-23 15:39:15 -0600842UCLASS_DRIVER(clk) = {
843 .id = UCLASS_CLK,
844 .name = "clk",
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200845 .post_probe = clk_uclass_post_probe,
Simon Glassf26c8a82015-06-23 15:39:15 -0600846};