blob: 2f9635524cf6804e41edf34c7e81187b4d0a6675 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass36ad2342015-06-23 15:39:15 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Stephen Warrena9622432016-06-17 09:44:00 -06005 * Copyright (c) 2016, NVIDIA CORPORATION.
Philipp Tomsich9cf03b02018-01-08 13:59:18 +01006 * Copyright (c) 2018, Theobroma Systems Design und Consulting GmbH
Simon Glass36ad2342015-06-23 15:39:15 -06007 */
8
Patrick Delaunay81313352021-04-27 11:02:19 +02009#define LOG_CATEGORY UCLASS_CLK
10
Simon Glass36ad2342015-06-23 15:39:15 -060011#include <common.h>
12#include <clk.h>
Stephen Warrena9622432016-06-17 09:44:00 -060013#include <clk-uclass.h>
Simon Glass36ad2342015-06-23 15:39:15 -060014#include <dm.h>
Simon Glass589d9152016-07-04 11:58:03 -060015#include <dt-structs.h>
Simon Glass36ad2342015-06-23 15:39:15 -060016#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <malloc.h>
Patrick Delaunay283dadf2021-11-19 15:12:06 +010019#include <asm/global_data.h>
Sean Andersond7ac3732021-04-08 22:13:03 -040020#include <dm/device_compat.h>
Claudiu Bezneac8c16002020-09-07 17:46:34 +030021#include <dm/device-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070022#include <dm/devres.h>
23#include <dm/read.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060024#include <linux/bug.h>
Lukasz Majewski9e38dc32019-06-24 15:50:42 +020025#include <linux/clk-provider.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070026#include <linux/err.h>
Simon Glass36ad2342015-06-23 15:39:15 -060027
Mario Six799fe562018-01-15 11:06:51 +010028static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
Simon Glass36ad2342015-06-23 15:39:15 -060029{
Mario Six799fe562018-01-15 11:06:51 +010030 return (const struct clk_ops *)dev->driver->ops;
Simon Glass36ad2342015-06-23 15:39:15 -060031}
32
Simon Glass43033962020-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 Glass3580f6d2021-08-07 07:24:03 -060038#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass1257efc2021-08-07 07:24:09 -060039int clk_get_by_phandle(struct udevice *dev, const struct phandle_1_arg *cells,
40 struct clk *clk)
Simon Glass589d9152016-07-04 11:58:03 -060041{
42 int ret;
43
Simon Glass0000e0d2021-03-15 17:25:28 +130044 ret = device_get_by_ofplat_idx(cells->idx, &clk->dev);
Simon Glass589d9152016-07-04 11:58:03 -060045 if (ret)
46 return ret;
Walter Lozanodc5b4372020-06-25 01:10:13 -030047 clk->id = cells->arg[0];
Simon Glass589d9152016-07-04 11:58:03 -060048
49 return 0;
50}
Simon Glass3580f6d2021-08-07 07:24:03 -060051#endif
52
53#if CONFIG_IS_ENABLED(OF_REAL)
Stephen Warrena9622432016-06-17 09:44:00 -060054static int clk_of_xlate_default(struct clk *clk,
Simon Glassb7ae2772017-05-18 20:09:40 -060055 struct ofnode_phandle_args *args)
Simon Glass36ad2342015-06-23 15:39:15 -060056{
Stephen Warrena9622432016-06-17 09:44:00 -060057 debug("%s(clk=%p)\n", __func__, clk);
Simon Glass36ad2342015-06-23 15:39:15 -060058
Stephen Warrena9622432016-06-17 09:44:00 -060059 if (args->args_count > 1) {
Sean Andersona1b654b2021-12-01 14:26:53 -050060 debug("Invalid args_count: %d\n", args->args_count);
Stephen Warrena9622432016-06-17 09:44:00 -060061 return -EINVAL;
62 }
Simon Glass36ad2342015-06-23 15:39:15 -060063
Stephen Warrena9622432016-06-17 09:44:00 -060064 if (args->args_count)
65 clk->id = args->args[0];
66 else
67 clk->id = 0;
Simon Glass36ad2342015-06-23 15:39:15 -060068
Sekhar Nori3d23abd2019-07-11 14:30:24 +053069 clk->data = 0;
70
Stephen Warrena9622432016-06-17 09:44:00 -060071 return 0;
Simon Glass36ad2342015-06-23 15:39:15 -060072}
Simon Glass0342bd22016-01-20 19:43:02 -070073
Jagan Tekifc7c7ce2019-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 Glassf73f5812021-01-21 13:57:11 -070091 return log_msg_ret("get", ret);
Jagan Tekifc7c7ce2019-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 Glassf73f5812021-01-21 13:57:11 -0700104 return log_msg_ret("xlate", ret);
Jagan Tekifc7c7ce2019-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 Glassf73f5812021-01-21 13:57:11 -0700111
112 return log_msg_ret("prop", ret);
Jagan Tekifc7c7ce2019-02-28 00:26:52 +0530113}
114
Philipp Tomsichf7604342018-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)
Simon Glass0342bd22016-01-20 19:43:02 -0700117{
Simon Glass0342bd22016-01-20 19:43:02 -0700118 int ret;
Simon Glass2558bff2017-05-30 21:47:29 -0600119 struct ofnode_phandle_args args;
Simon Glass0342bd22016-01-20 19:43:02 -0700120
Stephen Warrena9622432016-06-17 09:44:00 -0600121 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
122
123 assert(clk);
Patrice Chotard96fc03d2017-07-18 11:57:07 +0200124 clk->dev = NULL;
125
Philipp Tomsichf7604342018-01-08 11:18:18 +0100126 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
Mario Six799fe562018-01-15 11:06:51 +0100127 index, &args);
Simon Glass0342bd22016-01-20 19:43:02 -0700128 if (ret) {
129 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
130 __func__, ret);
Simon Glassf73f5812021-01-21 13:57:11 -0700131 return log_ret(ret);
Simon Glass0342bd22016-01-20 19:43:02 -0700132 }
133
Stephen Warrena9622432016-06-17 09:44:00 -0600134
Jagan Tekia77add32019-02-28 00:26:53 +0530135 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
Sean Andersonf0d5a6b2020-06-24 06:41:08 -0400136 index, clk);
Stephen Warrena9622432016-06-17 09:44:00 -0600137}
Philipp Tomsichf7604342018-01-08 11:18:18 +0100138
139int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
140{
Sean Anderson07435de2022-02-27 14:01:13 -0500141 return clk_get_by_index_nodev(dev_ofnode(dev), index, clk);
Jagan Tekifc7c7ce2019-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 Andersonf0d5a6b2020-06-24 06:41:08 -0400150 index, &args);
Jagan Tekifc7c7ce2019-02-28 00:26:52 +0530151
152 return clk_get_by_index_tail(ret, node, &args, "clocks",
Sean Andersonf0d5a6b2020-06-24 06:41:08 -0400153 index, clk);
Philipp Tomsichf7604342018-01-08 11:18:18 +0100154}
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100155
Neil Armstrong8a275a02018-04-03 11:44:18 +0200156int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
157{
158 int i, ret, err, count;
Patrick Delaunayb9c32142021-04-27 10:57:54 +0200159
Neil Armstrong8a275a02018-04-03 11:44:18 +0200160 bulk->count = 0;
161
Patrick Delaunayd776a842020-09-25 09:41:14 +0200162 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", 0);
Neil Armstrong52b26d92018-04-17 11:30:31 +0200163 if (count < 1)
164 return count;
Neil Armstrong8a275a02018-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 Bezneab91eee62020-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 Anderson08d531c2021-06-11 00:16:07 -0400206static int clk_set_default_parents(struct udevice *dev,
207 enum clk_defaults_stage stage)
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100208{
Claudiu Bezneab91eee62020-09-07 17:46:36 +0300209 struct clk clk, parent_clk, *c, *p;
Philipp Tomsich9cf03b02018-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 Delaunayd776a842020-09-25 09:41:14 +0200215 "#clock-cells", 0);
Philipp Tomsich9cf03b02018-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 Armstrongf3cc6312018-07-26 15:19:32 +0200225 /* If -ENOENT, this is a no-op entry */
226 if (ret == -ENOENT)
227 continue;
228
Philipp Tomsich9cf03b02018-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 Bezneab91eee62020-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 Tomsich9cf03b02018-01-08 13:59:18 +0100239 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
240 index, &clk);
Tero Kristod41b2b32021-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 Tomsich9cf03b02018-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 Hiblot9601f322019-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 Anderson08d531c2021-06-11 00:16:07 -0400260 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200261 continue;
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100262
Sean Anderson08d531c2021-06-11 00:16:07 -0400263 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200264 /* do not setup twice the parent clocks */
265 continue;
266
Claudiu Bezneab91eee62020-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 Tomsich9cf03b02018-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 Hiblotb2320812019-09-26 15:42:42 +0200279 if (ret < 0) {
Philipp Tomsich9cf03b02018-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 Anderson08d531c2021-06-11 00:16:07 -0400289static int clk_set_default_rates(struct udevice *dev,
290 enum clk_defaults_stage stage)
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100291{
Claudiu Bezneab91eee62020-09-07 17:46:36 +0300292 struct clk clk, *c;
Philipp Tomsich9cf03b02018-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 Armstrongf3cc6312018-07-26 15:19:32 +0200313 /* If 0 is passed, this is a no-op */
314 if (!rates[index])
315 continue;
316
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100317 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
318 index, &clk);
Tero Kristod41b2b32021-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 Tomsich9cf03b02018-01-08 13:59:18 +0100328 if (ret) {
Sean Andersond7ac3732021-04-08 22:13:03 -0400329 dev_dbg(dev,
330 "could not get assigned clock %d (err = %d)\n",
331 index, ret);
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100332 continue;
333 }
334
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200335 /* This is clk provider device trying to program itself
336 * It cannot be done right now but need to wait after the
337 * device is probed
338 */
Sean Anderson08d531c2021-06-11 00:16:07 -0400339 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200340 continue;
341
Sean Anderson08d531c2021-06-11 00:16:07 -0400342 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200343 /* do not setup twice the parent clocks */
344 continue;
345
Claudiu Bezneab91eee62020-09-07 17:46:36 +0300346 c = clk_set_default_get_by_id(&clk);
347 if (IS_ERR(c))
348 return PTR_ERR(c);
349
350 ret = clk_set_rate(c, rates[index]);
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200351
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100352 if (ret < 0) {
Sean Andersond7ac3732021-04-08 22:13:03 -0400353 dev_warn(dev,
354 "failed to set rate on clock index %d (%ld) (error = %d)\n",
355 index, clk.id, ret);
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100356 break;
357 }
358 }
359
360fail:
361 free(rates);
362 return ret;
363}
364
Sean Anderson08d531c2021-06-11 00:16:07 -0400365int clk_set_defaults(struct udevice *dev, enum clk_defaults_stage stage)
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100366{
367 int ret;
368
Simon Glassf1d50f72020-12-19 10:40:13 -0700369 if (!dev_has_ofnode(dev))
Peng Fan40ec4e42019-07-31 07:01:49 +0000370 return 0;
371
Sean Anderson08d531c2021-06-11 00:16:07 -0400372 /*
373 * To avoid setting defaults twice, don't set them before relocation.
374 * However, still set them for SPL. And still set them if explicitly
375 * asked.
376 */
Philipp Tomsiche546ec82018-11-26 20:20:19 +0100377 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
Sean Anderson08d531c2021-06-11 00:16:07 -0400378 if (stage != CLK_DEFAULTS_POST_FORCE)
379 return 0;
Philipp Tomsiche546ec82018-11-26 20:20:19 +0100380
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100381 debug("%s(%s)\n", __func__, dev_read_name(dev));
382
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200383 ret = clk_set_default_parents(dev, stage);
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100384 if (ret)
385 return ret;
386
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200387 ret = clk_set_default_rates(dev, stage);
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100388 if (ret < 0)
389 return ret;
390
391 return 0;
392}
Stephen Warrena9622432016-06-17 09:44:00 -0600393
394int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
395{
Sean Anderson07435de2022-02-27 14:01:13 -0500396 return clk_get_by_name_nodev(dev_ofnode(dev), name, clk);
Simon Glass0342bd22016-01-20 19:43:02 -0700397}
Simon Glass1257efc2021-08-07 07:24:09 -0600398#endif /* OF_REAL */
Patrice Chotardcafc3412017-07-25 13:24:45 +0200399
developerbdc786d2020-01-09 11:35:07 +0800400int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
401{
402 int index;
403
404 debug("%s(node=%p, name=%s, clk=%p)\n", __func__,
405 ofnode_get_name(node), name, clk);
406 clk->dev = NULL;
407
408 index = ofnode_stringlist_search(node, "clock-names", name);
409 if (index < 0) {
410 debug("fdt_stringlist_search() failed: %d\n", index);
411 return index;
412 }
413
414 return clk_get_by_index_nodev(node, index, clk);
415}
416
Patrice Chotardcafc3412017-07-25 13:24:45 +0200417int clk_release_all(struct clk *clk, int count)
418{
419 int i, ret;
420
421 for (i = 0; i < count; i++) {
422 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
423
424 /* check if clock has been previously requested */
425 if (!clk[i].dev)
426 continue;
427
428 ret = clk_disable(&clk[i]);
429 if (ret && ret != -ENOSYS)
430 return ret;
431
Sean Anderson9a665722022-01-15 17:25:04 -0500432 clk_free(&clk[i]);
Patrice Chotardcafc3412017-07-25 13:24:45 +0200433 }
434
435 return 0;
436}
437
Stephen Warrena9622432016-06-17 09:44:00 -0600438int clk_request(struct udevice *dev, struct clk *clk)
439{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200440 const struct clk_ops *ops;
Stephen Warrena9622432016-06-17 09:44:00 -0600441
442 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200443 if (!clk)
444 return 0;
445 ops = clk_dev_ops(dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600446
447 clk->dev = dev;
448
449 if (!ops->request)
450 return 0;
451
452 return ops->request(clk);
453}
454
Sean Anderson9a665722022-01-15 17:25:04 -0500455void clk_free(struct clk *clk)
Stephen Warrena9622432016-06-17 09:44:00 -0600456{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200457 const struct clk_ops *ops;
Stephen Warrena9622432016-06-17 09:44:00 -0600458
459 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800460 if (!clk_valid(clk))
Sean Anderson9a665722022-01-15 17:25:04 -0500461 return;
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200462 ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600463
Sean Anderson553935f2022-01-15 17:24:58 -0500464 if (ops->rfree)
465 ops->rfree(clk);
Sean Anderson9a665722022-01-15 17:25:04 -0500466 return;
Stephen Warrena9622432016-06-17 09:44:00 -0600467}
468
469ulong clk_get_rate(struct clk *clk)
470{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200471 const struct clk_ops *ops;
Simon Glassf73f5812021-01-21 13:57:11 -0700472 int ret;
Stephen Warrena9622432016-06-17 09:44:00 -0600473
474 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800475 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200476 return 0;
477 ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600478
479 if (!ops->get_rate)
480 return -ENOSYS;
481
Simon Glassf73f5812021-01-21 13:57:11 -0700482 ret = ops->get_rate(clk);
483 if (ret)
484 return log_ret(ret);
485
486 return 0;
Stephen Warrena9622432016-06-17 09:44:00 -0600487}
488
Lukasz Majewski9e38dc32019-06-24 15:50:42 +0200489struct clk *clk_get_parent(struct clk *clk)
490{
491 struct udevice *pdev;
492 struct clk *pclk;
493
494 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800495 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200496 return NULL;
Lukasz Majewski9e38dc32019-06-24 15:50:42 +0200497
498 pdev = dev_get_parent(clk->dev);
Tero Kristof04dfff2021-06-11 11:45:08 +0300499 if (!pdev)
500 return ERR_PTR(-ENODEV);
Lukasz Majewski9e38dc32019-06-24 15:50:42 +0200501 pclk = dev_get_clk_ptr(pdev);
502 if (!pclk)
503 return ERR_PTR(-ENODEV);
504
505 return pclk;
506}
507
Michal Suchanek0d4d5e42022-09-28 12:37:57 +0200508ulong clk_get_parent_rate(struct clk *clk)
Lukasz Majewski53155da2019-06-24 15:50:43 +0200509{
510 const struct clk_ops *ops;
511 struct clk *pclk;
512
513 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800514 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200515 return 0;
Lukasz Majewski53155da2019-06-24 15:50:43 +0200516
517 pclk = clk_get_parent(clk);
518 if (IS_ERR(pclk))
519 return -ENODEV;
520
521 ops = clk_dev_ops(pclk->dev);
522 if (!ops->get_rate)
523 return -ENOSYS;
524
Lukasz Majewski4ef32172019-06-24 15:50:46 +0200525 /* Read the 'rate' if not already set or if proper flag set*/
526 if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
Lukasz Majewski53155da2019-06-24 15:50:43 +0200527 pclk->rate = clk_get_rate(pclk);
528
529 return pclk->rate;
530}
531
Dario Binacchib7f85892020-12-30 00:06:31 +0100532ulong clk_round_rate(struct clk *clk, ulong rate)
533{
534 const struct clk_ops *ops;
535
536 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
537 if (!clk_valid(clk))
538 return 0;
539
540 ops = clk_dev_ops(clk->dev);
541 if (!ops->round_rate)
542 return -ENOSYS;
543
544 return ops->round_rate(clk, rate);
545}
546
Patrick Delaunayd867a2872022-06-20 15:37:25 +0200547static void clk_get_priv(struct clk *clk, struct clk **clkp)
548{
549 *clkp = clk;
550
551 /* get private clock struct associated to the provided clock */
552 if (CONFIG_IS_ENABLED(CLK_CCF)) {
553 /* Take id 0 as a non-valid clk, such as dummy */
554 if (clk->id)
555 clk_get_by_id(clk->id, clkp);
556 }
557}
558
559/* clean cache, called with private clock struct */
Tero Kristo9ab78c12021-06-11 11:45:12 +0300560static void clk_clean_rate_cache(struct clk *clk)
561{
562 struct udevice *child_dev;
563 struct clk *clkp;
564
565 if (!clk)
566 return;
567
568 clk->rate = 0;
569
570 list_for_each_entry(child_dev, &clk->dev->child_head, sibling_node) {
571 clkp = dev_get_clk_ptr(child_dev);
572 clk_clean_rate_cache(clkp);
573 }
574}
575
Stephen Warrena9622432016-06-17 09:44:00 -0600576ulong clk_set_rate(struct clk *clk, ulong rate)
577{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200578 const struct clk_ops *ops;
Patrick Delaunayd867a2872022-06-20 15:37:25 +0200579 struct clk *clkp;
Stephen Warrena9622432016-06-17 09:44:00 -0600580
581 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
developerdc338d32020-01-09 11:35:06 +0800582 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200583 return 0;
584 ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600585
586 if (!ops->set_rate)
587 return -ENOSYS;
588
Patrick Delaunayd867a2872022-06-20 15:37:25 +0200589 /* get private clock struct used for cache */
590 clk_get_priv(clk, &clkp);
Tero Kristo9ab78c12021-06-11 11:45:12 +0300591 /* Clean up cached rates for us and all child clocks */
Patrick Delaunayd867a2872022-06-20 15:37:25 +0200592 clk_clean_rate_cache(clkp);
Tero Kristo9ab78c12021-06-11 11:45:12 +0300593
Stephen Warrena9622432016-06-17 09:44:00 -0600594 return ops->set_rate(clk, rate);
595}
596
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100597int clk_set_parent(struct clk *clk, struct clk *parent)
598{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200599 const struct clk_ops *ops;
Claudiu Bezneac8c16002020-09-07 17:46:34 +0300600 int ret;
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100601
602 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
developerdc338d32020-01-09 11:35:06 +0800603 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200604 return 0;
605 ops = clk_dev_ops(clk->dev);
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100606
607 if (!ops->set_parent)
608 return -ENOSYS;
609
Claudiu Bezneac8c16002020-09-07 17:46:34 +0300610 ret = ops->set_parent(clk, parent);
611 if (ret)
612 return ret;
613
614 if (CONFIG_IS_ENABLED(CLK_CCF))
615 ret = device_reparent(clk->dev, parent->dev);
616
617 return ret;
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100618}
619
Stephen Warrena9622432016-06-17 09:44:00 -0600620int clk_enable(struct clk *clk)
621{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200622 const struct clk_ops *ops;
Peng Fan82628e22019-08-21 13:35:09 +0000623 struct clk *clkp = NULL;
624 int ret;
Stephen Warrena9622432016-06-17 09:44:00 -0600625
626 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800627 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200628 return 0;
629 ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600630
Peng Fan82628e22019-08-21 13:35:09 +0000631 if (CONFIG_IS_ENABLED(CLK_CCF)) {
632 /* Take id 0 as a non-valid clk, such as dummy */
633 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
634 if (clkp->enable_count) {
635 clkp->enable_count++;
636 return 0;
637 }
638 if (clkp->dev->parent &&
Patrick Delaunaydb9b1a12022-01-24 14:17:14 +0100639 device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
Peng Fan82628e22019-08-21 13:35:09 +0000640 ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
641 if (ret) {
642 printf("Enable %s failed\n",
643 clkp->dev->parent->name);
644 return ret;
645 }
646 }
647 }
Stephen Warrena9622432016-06-17 09:44:00 -0600648
Peng Fan82628e22019-08-21 13:35:09 +0000649 if (ops->enable) {
650 ret = ops->enable(clk);
651 if (ret) {
652 printf("Enable %s failed\n", clk->dev->name);
653 return ret;
654 }
655 }
656 if (clkp)
657 clkp->enable_count++;
658 } else {
659 if (!ops->enable)
660 return -ENOSYS;
661 return ops->enable(clk);
662 }
663
664 return 0;
Stephen Warrena9622432016-06-17 09:44:00 -0600665}
666
Neil Armstrong8a275a02018-04-03 11:44:18 +0200667int clk_enable_bulk(struct clk_bulk *bulk)
668{
669 int i, ret;
670
671 for (i = 0; i < bulk->count; i++) {
672 ret = clk_enable(&bulk->clks[i]);
673 if (ret < 0 && ret != -ENOSYS)
674 return ret;
675 }
676
677 return 0;
678}
679
Stephen Warrena9622432016-06-17 09:44:00 -0600680int clk_disable(struct clk *clk)
681{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200682 const struct clk_ops *ops;
Peng Fan82628e22019-08-21 13:35:09 +0000683 struct clk *clkp = NULL;
684 int ret;
Stephen Warrena9622432016-06-17 09:44:00 -0600685
686 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800687 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200688 return 0;
689 ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600690
Peng Fan82628e22019-08-21 13:35:09 +0000691 if (CONFIG_IS_ENABLED(CLK_CCF)) {
692 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
Claudiu Bezneab02e8dd2020-09-07 17:46:35 +0300693 if (clkp->flags & CLK_IS_CRITICAL)
694 return 0;
695
Peng Fan82628e22019-08-21 13:35:09 +0000696 if (clkp->enable_count == 0) {
697 printf("clk %s already disabled\n",
698 clkp->dev->name);
699 return 0;
700 }
Stephen Warrena9622432016-06-17 09:44:00 -0600701
Peng Fan82628e22019-08-21 13:35:09 +0000702 if (--clkp->enable_count > 0)
703 return 0;
704 }
705
706 if (ops->disable) {
707 ret = ops->disable(clk);
708 if (ret)
709 return ret;
710 }
711
712 if (clkp && clkp->dev->parent &&
Patrick Delaunaydb9b1a12022-01-24 14:17:14 +0100713 device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
Peng Fan82628e22019-08-21 13:35:09 +0000714 ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent));
715 if (ret) {
716 printf("Disable %s failed\n",
717 clkp->dev->parent->name);
718 return ret;
719 }
720 }
721 } else {
722 if (!ops->disable)
723 return -ENOSYS;
724
725 return ops->disable(clk);
726 }
727
728 return 0;
Stephen Warrena9622432016-06-17 09:44:00 -0600729}
Simon Glass36ad2342015-06-23 15:39:15 -0600730
Neil Armstrong8a275a02018-04-03 11:44:18 +0200731int clk_disable_bulk(struct clk_bulk *bulk)
732{
733 int i, ret;
734
735 for (i = 0; i < bulk->count; i++) {
736 ret = clk_disable(&bulk->clks[i]);
737 if (ret < 0 && ret != -ENOSYS)
738 return ret;
739 }
740
741 return 0;
742}
743
Lukasz Majewski12014be2019-06-24 15:50:44 +0200744int clk_get_by_id(ulong id, struct clk **clkp)
745{
746 struct udevice *dev;
747 struct uclass *uc;
748 int ret;
749
750 ret = uclass_get(UCLASS_CLK, &uc);
751 if (ret)
752 return ret;
753
754 uclass_foreach_dev(dev, uc) {
755 struct clk *clk = dev_get_clk_ptr(dev);
756
757 if (clk && clk->id == id) {
758 *clkp = clk;
759 return 0;
760 }
761 }
762
763 return -ENOENT;
764}
765
Sekhar Noricf3119d2019-08-01 19:12:55 +0530766bool clk_is_match(const struct clk *p, const struct clk *q)
767{
768 /* trivial case: identical struct clk's or both NULL */
769 if (p == q)
770 return true;
771
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200772 /* trivial case #2: on the clk pointer is NULL */
773 if (!p || !q)
774 return false;
775
Sekhar Noricf3119d2019-08-01 19:12:55 +0530776 /* same device, id and data */
777 if (p->dev == q->dev && p->id == q->id && p->data == q->data)
778 return true;
779
780 return false;
781}
782
Jean-Jacques Hiblot6e66b2d2019-10-22 14:00:04 +0200783static void devm_clk_release(struct udevice *dev, void *res)
784{
785 clk_free(res);
786}
787
788static int devm_clk_match(struct udevice *dev, void *res, void *data)
789{
790 return res == data;
791}
792
793struct clk *devm_clk_get(struct udevice *dev, const char *id)
794{
795 int rc;
796 struct clk *clk;
797
798 clk = devres_alloc(devm_clk_release, sizeof(struct clk), __GFP_ZERO);
799 if (unlikely(!clk))
800 return ERR_PTR(-ENOMEM);
801
802 rc = clk_get_by_name(dev, id, clk);
803 if (rc)
804 return ERR_PTR(rc);
805
806 devres_add(dev, clk);
807 return clk;
808}
809
Jean-Jacques Hiblot6e66b2d2019-10-22 14:00:04 +0200810void devm_clk_put(struct udevice *dev, struct clk *clk)
811{
812 int rc;
813
814 if (!clk)
815 return;
816
817 rc = devres_release(dev, devm_clk_release, devm_clk_match, clk);
818 WARN_ON(rc);
819}
820
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200821int clk_uclass_post_probe(struct udevice *dev)
822{
823 /*
824 * when a clock provider is probed. Call clk_set_defaults()
825 * also after the device is probed. This takes care of cases
826 * where the DT is used to setup default parents and rates
827 * using assigned-clocks
828 */
Marek Vasut05e3d8e2022-01-01 19:51:39 +0100829 clk_set_defaults(dev, CLK_DEFAULTS_POST);
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200830
831 return 0;
832}
833
Simon Glass36ad2342015-06-23 15:39:15 -0600834UCLASS_DRIVER(clk) = {
835 .id = UCLASS_CLK,
836 .name = "clk",
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200837 .post_probe = clk_uclass_post_probe,
Simon Glass36ad2342015-06-23 15:39:15 -0600838};