blob: 4c832f1a53073ff18434b1f669154d87136e4575 [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 <clk.h>
Stephen Warrena9622432016-06-17 09:44:00 -060012#include <clk-uclass.h>
Simon Glass36ad2342015-06-23 15:39:15 -060013#include <dm.h>
Simon Glass589d9152016-07-04 11:58:03 -060014#include <dt-structs.h>
Simon Glass36ad2342015-06-23 15:39:15 -060015#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Patrick Delaunay283dadf2021-11-19 15:12:06 +010018#include <asm/global_data.h>
Sean Andersond7ac3732021-04-08 22:13:03 -040019#include <dm/device_compat.h>
Claudiu Bezneac8c16002020-09-07 17:46:34 +030020#include <dm/device-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070021#include <dm/devres.h>
22#include <dm/read.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060023#include <linux/bug.h>
Lukasz Majewski9e38dc32019-06-24 15:50:42 +020024#include <linux/clk-provider.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070025#include <linux/err.h>
Simon Glass36ad2342015-06-23 15:39:15 -060026
Mario Six799fe562018-01-15 11:06:51 +010027static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
Simon Glass36ad2342015-06-23 15:39:15 -060028{
Mario Six799fe562018-01-15 11:06:51 +010029 return (const struct clk_ops *)dev->driver->ops;
Simon Glass36ad2342015-06-23 15:39:15 -060030}
31
Simon Glass43033962020-07-19 10:15:56 -060032struct clk *dev_get_clk_ptr(struct udevice *dev)
33{
34 return (struct clk *)dev_get_uclass_priv(dev);
35}
36
Simon Glass3580f6d2021-08-07 07:24:03 -060037#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass1257efc2021-08-07 07:24:09 -060038int clk_get_by_phandle(struct udevice *dev, const struct phandle_1_arg *cells,
39 struct clk *clk)
Simon Glass589d9152016-07-04 11:58:03 -060040{
41 int ret;
42
Simon Glass0000e0d2021-03-15 17:25:28 +130043 ret = device_get_by_ofplat_idx(cells->idx, &clk->dev);
Simon Glass589d9152016-07-04 11:58:03 -060044 if (ret)
45 return ret;
Walter Lozanodc5b4372020-06-25 01:10:13 -030046 clk->id = cells->arg[0];
Simon Glass589d9152016-07-04 11:58:03 -060047
48 return 0;
49}
Simon Glass3580f6d2021-08-07 07:24:03 -060050#endif
51
52#if CONFIG_IS_ENABLED(OF_REAL)
Stephen Warrena9622432016-06-17 09:44:00 -060053static int clk_of_xlate_default(struct clk *clk,
Simon Glassb7ae2772017-05-18 20:09:40 -060054 struct ofnode_phandle_args *args)
Simon Glass36ad2342015-06-23 15:39:15 -060055{
Stephen Warrena9622432016-06-17 09:44:00 -060056 debug("%s(clk=%p)\n", __func__, clk);
Simon Glass36ad2342015-06-23 15:39:15 -060057
Stephen Warrena9622432016-06-17 09:44:00 -060058 if (args->args_count > 1) {
Sean Andersona1b654b2021-12-01 14:26:53 -050059 debug("Invalid args_count: %d\n", args->args_count);
Stephen Warrena9622432016-06-17 09:44:00 -060060 return -EINVAL;
61 }
Simon Glass36ad2342015-06-23 15:39:15 -060062
Stephen Warrena9622432016-06-17 09:44:00 -060063 if (args->args_count)
64 clk->id = args->args[0];
65 else
66 clk->id = 0;
Simon Glass36ad2342015-06-23 15:39:15 -060067
Sekhar Nori3d23abd2019-07-11 14:30:24 +053068 clk->data = 0;
69
Stephen Warrena9622432016-06-17 09:44:00 -060070 return 0;
Simon Glass36ad2342015-06-23 15:39:15 -060071}
Simon Glass0342bd22016-01-20 19:43:02 -070072
Jagan Tekifc7c7ce2019-02-28 00:26:52 +053073static int clk_get_by_index_tail(int ret, ofnode node,
74 struct ofnode_phandle_args *args,
75 const char *list_name, int index,
76 struct clk *clk)
77{
78 struct udevice *dev_clk;
79 const struct clk_ops *ops;
80
81 assert(clk);
82 clk->dev = NULL;
83 if (ret)
84 goto err;
85
86 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
87 if (ret) {
88 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
89 __func__, ret);
Simon Glassf73f5812021-01-21 13:57:11 -070090 return log_msg_ret("get", ret);
Jagan Tekifc7c7ce2019-02-28 00:26:52 +053091 }
92
93 clk->dev = dev_clk;
94
95 ops = clk_dev_ops(dev_clk);
96
97 if (ops->of_xlate)
98 ret = ops->of_xlate(clk, args);
99 else
100 ret = clk_of_xlate_default(clk, args);
101 if (ret) {
102 debug("of_xlate() failed: %d\n", ret);
Simon Glassf73f5812021-01-21 13:57:11 -0700103 return log_msg_ret("xlate", ret);
Jagan Tekifc7c7ce2019-02-28 00:26:52 +0530104 }
105
106 return clk_request(dev_clk, clk);
107err:
108 debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n",
109 __func__, ofnode_get_name(node), list_name, index, ret);
Simon Glassf73f5812021-01-21 13:57:11 -0700110
111 return log_msg_ret("prop", ret);
Jagan Tekifc7c7ce2019-02-28 00:26:52 +0530112}
113
Philipp Tomsichf7604342018-01-08 11:18:18 +0100114static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
115 int index, struct clk *clk)
Simon Glass0342bd22016-01-20 19:43:02 -0700116{
Simon Glass0342bd22016-01-20 19:43:02 -0700117 int ret;
Simon Glass2558bff2017-05-30 21:47:29 -0600118 struct ofnode_phandle_args args;
Simon Glass0342bd22016-01-20 19:43:02 -0700119
Stephen Warrena9622432016-06-17 09:44:00 -0600120 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
121
122 assert(clk);
Patrice Chotard96fc03d2017-07-18 11:57:07 +0200123 clk->dev = NULL;
124
Philipp Tomsichf7604342018-01-08 11:18:18 +0100125 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
Mario Six799fe562018-01-15 11:06:51 +0100126 index, &args);
Simon Glass0342bd22016-01-20 19:43:02 -0700127 if (ret) {
128 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
129 __func__, ret);
Simon Glassf73f5812021-01-21 13:57:11 -0700130 return log_ret(ret);
Simon Glass0342bd22016-01-20 19:43:02 -0700131 }
132
Stephen Warrena9622432016-06-17 09:44:00 -0600133
Jagan Tekia77add32019-02-28 00:26:53 +0530134 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
Sean Andersonf0d5a6b2020-06-24 06:41:08 -0400135 index, clk);
Stephen Warrena9622432016-06-17 09:44:00 -0600136}
Philipp Tomsichf7604342018-01-08 11:18:18 +0100137
138int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
139{
Sean Anderson07435de2022-02-27 14:01:13 -0500140 return clk_get_by_index_nodev(dev_ofnode(dev), index, clk);
Jagan Tekifc7c7ce2019-02-28 00:26:52 +0530141}
142
143int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
144{
145 struct ofnode_phandle_args args;
146 int ret;
147
148 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
Sean Andersonf0d5a6b2020-06-24 06:41:08 -0400149 index, &args);
Jagan Tekifc7c7ce2019-02-28 00:26:52 +0530150
151 return clk_get_by_index_tail(ret, node, &args, "clocks",
Sean Andersonf0d5a6b2020-06-24 06:41:08 -0400152 index, clk);
Philipp Tomsichf7604342018-01-08 11:18:18 +0100153}
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100154
Neil Armstrong8a275a02018-04-03 11:44:18 +0200155int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
156{
157 int i, ret, err, count;
Patrick Delaunayb9c32142021-04-27 10:57:54 +0200158
Neil Armstrong8a275a02018-04-03 11:44:18 +0200159 bulk->count = 0;
160
Patrick Delaunayd776a842020-09-25 09:41:14 +0200161 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", 0);
Neil Armstrong52b26d92018-04-17 11:30:31 +0200162 if (count < 1)
163 return count;
Neil Armstrong8a275a02018-04-03 11:44:18 +0200164
165 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
166 if (!bulk->clks)
167 return -ENOMEM;
168
169 for (i = 0; i < count; i++) {
170 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
171 if (ret < 0)
172 goto bulk_get_err;
173
174 ++bulk->count;
175 }
176
177 return 0;
178
179bulk_get_err:
180 err = clk_release_all(bulk->clks, bulk->count);
181 if (err)
182 debug("%s: could release all clocks for %p\n",
183 __func__, dev);
184
185 return ret;
186}
187
Claudiu Bezneab91eee62020-09-07 17:46:36 +0300188static struct clk *clk_set_default_get_by_id(struct clk *clk)
189{
190 struct clk *c = clk;
191
192 if (CONFIG_IS_ENABLED(CLK_CCF)) {
193 int ret = clk_get_by_id(clk->id, &c);
194
195 if (ret) {
196 debug("%s(): could not get parent clock pointer, id %lu\n",
197 __func__, clk->id);
198 ERR_PTR(ret);
199 }
200 }
201
202 return c;
203}
204
Sean Anderson08d531c2021-06-11 00:16:07 -0400205static int clk_set_default_parents(struct udevice *dev,
206 enum clk_defaults_stage stage)
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100207{
Claudiu Bezneab91eee62020-09-07 17:46:36 +0300208 struct clk clk, parent_clk, *c, *p;
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100209 int index;
210 int num_parents;
211 int ret;
212
213 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
Patrick Delaunayd776a842020-09-25 09:41:14 +0200214 "#clock-cells", 0);
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100215 if (num_parents < 0) {
216 debug("%s: could not read assigned-clock-parents for %p\n",
217 __func__, dev);
218 return 0;
219 }
220
221 for (index = 0; index < num_parents; index++) {
222 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
223 index, &parent_clk);
Neil Armstrongf3cc6312018-07-26 15:19:32 +0200224 /* If -ENOENT, this is a no-op entry */
225 if (ret == -ENOENT)
226 continue;
227
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100228 if (ret) {
229 debug("%s: could not get parent clock %d for %s\n",
230 __func__, index, dev_read_name(dev));
231 return ret;
232 }
233
Claudiu Bezneab91eee62020-09-07 17:46:36 +0300234 p = clk_set_default_get_by_id(&parent_clk);
235 if (IS_ERR(p))
236 return PTR_ERR(p);
237
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100238 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
239 index, &clk);
Tero Kristod41b2b32021-06-11 11:45:11 +0300240 /*
241 * If the clock provider is not ready yet, let it handle
242 * the re-programming later.
243 */
244 if (ret == -EPROBE_DEFER) {
245 ret = 0;
246 continue;
247 }
248
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100249 if (ret) {
250 debug("%s: could not get assigned clock %d for %s\n",
251 __func__, index, dev_read_name(dev));
252 return ret;
253 }
254
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200255 /* This is clk provider device trying to reparent itself
256 * It cannot be done right now but need to wait after the
257 * device is probed
258 */
Sean Anderson08d531c2021-06-11 00:16:07 -0400259 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200260 continue;
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100261
Sean Anderson08d531c2021-06-11 00:16:07 -0400262 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200263 /* do not setup twice the parent clocks */
264 continue;
265
Claudiu Bezneab91eee62020-09-07 17:46:36 +0300266 c = clk_set_default_get_by_id(&clk);
267 if (IS_ERR(c))
268 return PTR_ERR(c);
269
270 ret = clk_set_parent(c, p);
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100271 /*
272 * Not all drivers may support clock-reparenting (as of now).
273 * Ignore errors due to this.
274 */
275 if (ret == -ENOSYS)
276 continue;
277
Jean-Jacques Hiblotb2320812019-09-26 15:42:42 +0200278 if (ret < 0) {
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100279 debug("%s: failed to reparent clock %d for %s\n",
280 __func__, index, dev_read_name(dev));
281 return ret;
282 }
283 }
284
285 return 0;
286}
287
Sean Anderson08d531c2021-06-11 00:16:07 -0400288static int clk_set_default_rates(struct udevice *dev,
289 enum clk_defaults_stage stage)
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100290{
Claudiu Bezneab91eee62020-09-07 17:46:36 +0300291 struct clk clk, *c;
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100292 int index;
293 int num_rates;
294 int size;
295 int ret = 0;
296 u32 *rates = NULL;
297
298 size = dev_read_size(dev, "assigned-clock-rates");
299 if (size < 0)
300 return 0;
301
302 num_rates = size / sizeof(u32);
303 rates = calloc(num_rates, sizeof(u32));
304 if (!rates)
305 return -ENOMEM;
306
307 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
308 if (ret)
309 goto fail;
310
311 for (index = 0; index < num_rates; index++) {
Neil Armstrongf3cc6312018-07-26 15:19:32 +0200312 /* If 0 is passed, this is a no-op */
313 if (!rates[index])
314 continue;
315
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100316 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
317 index, &clk);
Tero Kristod41b2b32021-06-11 11:45:11 +0300318 /*
319 * If the clock provider is not ready yet, let it handle
320 * the re-programming later.
321 */
322 if (ret == -EPROBE_DEFER) {
323 ret = 0;
324 continue;
325 }
326
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100327 if (ret) {
Sean Andersond7ac3732021-04-08 22:13:03 -0400328 dev_dbg(dev,
329 "could not get assigned clock %d (err = %d)\n",
330 index, ret);
Ashok Reddy Soma8f03cef2023-08-30 10:31:42 +0200331 /* Skip if it is empty */
332 if (ret == -ENOENT) {
333 ret = 0;
334 continue;
335 }
336
337 return ret;
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100338 }
339
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200340 /* This is clk provider device trying to program itself
341 * It cannot be done right now but need to wait after the
342 * device is probed
343 */
Sean Anderson08d531c2021-06-11 00:16:07 -0400344 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200345 continue;
346
Sean Anderson08d531c2021-06-11 00:16:07 -0400347 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200348 /* do not setup twice the parent clocks */
349 continue;
350
Claudiu Bezneab91eee62020-09-07 17:46:36 +0300351 c = clk_set_default_get_by_id(&clk);
352 if (IS_ERR(c))
353 return PTR_ERR(c);
354
355 ret = clk_set_rate(c, rates[index]);
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200356
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100357 if (ret < 0) {
Sean Andersond7ac3732021-04-08 22:13:03 -0400358 dev_warn(dev,
359 "failed to set rate on clock index %d (%ld) (error = %d)\n",
360 index, clk.id, ret);
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100361 break;
362 }
363 }
364
365fail:
366 free(rates);
367 return ret;
368}
369
Sean Anderson08d531c2021-06-11 00:16:07 -0400370int clk_set_defaults(struct udevice *dev, enum clk_defaults_stage stage)
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100371{
372 int ret;
373
Simon Glassf1d50f72020-12-19 10:40:13 -0700374 if (!dev_has_ofnode(dev))
Peng Fan40ec4e42019-07-31 07:01:49 +0000375 return 0;
376
Sean Anderson08d531c2021-06-11 00:16:07 -0400377 /*
378 * To avoid setting defaults twice, don't set them before relocation.
379 * However, still set them for SPL. And still set them if explicitly
380 * asked.
381 */
Philipp Tomsiche546ec82018-11-26 20:20:19 +0100382 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
Sean Anderson08d531c2021-06-11 00:16:07 -0400383 if (stage != CLK_DEFAULTS_POST_FORCE)
384 return 0;
Philipp Tomsiche546ec82018-11-26 20:20:19 +0100385
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100386 debug("%s(%s)\n", __func__, dev_read_name(dev));
387
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200388 ret = clk_set_default_parents(dev, stage);
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100389 if (ret)
390 return ret;
391
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200392 ret = clk_set_default_rates(dev, stage);
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100393 if (ret < 0)
394 return ret;
395
396 return 0;
397}
Stephen Warrena9622432016-06-17 09:44:00 -0600398
399int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
400{
Sean Anderson07435de2022-02-27 14:01:13 -0500401 return clk_get_by_name_nodev(dev_ofnode(dev), name, clk);
Simon Glass0342bd22016-01-20 19:43:02 -0700402}
Simon Glass1257efc2021-08-07 07:24:09 -0600403#endif /* OF_REAL */
Patrice Chotardcafc3412017-07-25 13:24:45 +0200404
developerbdc786d2020-01-09 11:35:07 +0800405int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
406{
Samuel Hollandbae0f4f2023-01-21 18:02:51 -0600407 int index = 0;
developerbdc786d2020-01-09 11:35:07 +0800408
409 debug("%s(node=%p, name=%s, clk=%p)\n", __func__,
410 ofnode_get_name(node), name, clk);
411 clk->dev = NULL;
412
Samuel Hollandbae0f4f2023-01-21 18:02:51 -0600413 if (name) {
414 index = ofnode_stringlist_search(node, "clock-names", name);
415 if (index < 0) {
416 debug("fdt_stringlist_search() failed: %d\n", index);
417 return index;
418 }
developerbdc786d2020-01-09 11:35:07 +0800419 }
420
421 return clk_get_by_index_nodev(node, index, clk);
422}
423
Eugen Hristev70e32ba2023-06-19 13:47:52 +0300424int clk_release_all(struct clk *clk, unsigned int count)
Patrice Chotardcafc3412017-07-25 13:24:45 +0200425{
Eugen Hristev70e32ba2023-06-19 13:47:52 +0300426 unsigned int i;
427 int ret;
Patrice Chotardcafc3412017-07-25 13:24:45 +0200428
429 for (i = 0; i < count; i++) {
Eugen Hristev70e32ba2023-06-19 13:47:52 +0300430 debug("%s(clk[%u]=%p)\n", __func__, i, &clk[i]);
Patrice Chotardcafc3412017-07-25 13:24:45 +0200431
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;
Patrice Chotardcafc3412017-07-25 13:24:45 +0200439 }
440
441 return 0;
442}
443
Stephen Warrena9622432016-06-17 09:44:00 -0600444int clk_request(struct udevice *dev, struct clk *clk)
445{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200446 const struct clk_ops *ops;
Stephen Warrena9622432016-06-17 09:44:00 -0600447
448 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200449 if (!clk)
450 return 0;
451 ops = clk_dev_ops(dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600452
453 clk->dev = dev;
454
455 if (!ops->request)
456 return 0;
457
458 return ops->request(clk);
459}
460
Stephen Warrena9622432016-06-17 09:44:00 -0600461ulong clk_get_rate(struct clk *clk)
462{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200463 const struct clk_ops *ops;
Stephen Warrena9622432016-06-17 09:44:00 -0600464
465 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800466 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200467 return 0;
468 ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600469
470 if (!ops->get_rate)
471 return -ENOSYS;
472
Julien Massonb5de0b92023-12-15 15:09:43 +0100473 return ops->get_rate(clk);
Stephen Warrena9622432016-06-17 09:44:00 -0600474}
475
Lukasz Majewski9e38dc32019-06-24 15:50:42 +0200476struct clk *clk_get_parent(struct clk *clk)
477{
478 struct udevice *pdev;
479 struct clk *pclk;
480
481 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800482 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200483 return NULL;
Lukasz Majewski9e38dc32019-06-24 15:50:42 +0200484
485 pdev = dev_get_parent(clk->dev);
Tero Kristof04dfff2021-06-11 11:45:08 +0300486 if (!pdev)
487 return ERR_PTR(-ENODEV);
Lukasz Majewski9e38dc32019-06-24 15:50:42 +0200488 pclk = dev_get_clk_ptr(pdev);
489 if (!pclk)
490 return ERR_PTR(-ENODEV);
491
492 return pclk;
493}
494
Michal Suchanek0d4d5e42022-09-28 12:37:57 +0200495ulong clk_get_parent_rate(struct clk *clk)
Lukasz Majewski53155da2019-06-24 15:50:43 +0200496{
497 const struct clk_ops *ops;
498 struct clk *pclk;
499
500 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800501 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200502 return 0;
Lukasz Majewski53155da2019-06-24 15:50:43 +0200503
504 pclk = clk_get_parent(clk);
505 if (IS_ERR(pclk))
506 return -ENODEV;
507
508 ops = clk_dev_ops(pclk->dev);
509 if (!ops->get_rate)
510 return -ENOSYS;
511
Lukasz Majewski4ef32172019-06-24 15:50:46 +0200512 /* Read the 'rate' if not already set or if proper flag set*/
513 if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
Lukasz Majewski53155da2019-06-24 15:50:43 +0200514 pclk->rate = clk_get_rate(pclk);
515
516 return pclk->rate;
517}
518
Dario Binacchib7f85892020-12-30 00:06:31 +0100519ulong clk_round_rate(struct clk *clk, ulong rate)
520{
521 const struct clk_ops *ops;
522
523 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
524 if (!clk_valid(clk))
525 return 0;
526
527 ops = clk_dev_ops(clk->dev);
528 if (!ops->round_rate)
529 return -ENOSYS;
530
531 return ops->round_rate(clk, rate);
532}
533
Patrick Delaunayd867a2872022-06-20 15:37:25 +0200534static void clk_get_priv(struct clk *clk, struct clk **clkp)
535{
536 *clkp = clk;
537
538 /* get private clock struct associated to the provided clock */
539 if (CONFIG_IS_ENABLED(CLK_CCF)) {
540 /* Take id 0 as a non-valid clk, such as dummy */
541 if (clk->id)
542 clk_get_by_id(clk->id, clkp);
543 }
544}
545
546/* clean cache, called with private clock struct */
Tero Kristo9ab78c12021-06-11 11:45:12 +0300547static void clk_clean_rate_cache(struct clk *clk)
548{
549 struct udevice *child_dev;
550 struct clk *clkp;
551
552 if (!clk)
553 return;
554
555 clk->rate = 0;
556
557 list_for_each_entry(child_dev, &clk->dev->child_head, sibling_node) {
558 clkp = dev_get_clk_ptr(child_dev);
559 clk_clean_rate_cache(clkp);
560 }
561}
562
Stephen Warrena9622432016-06-17 09:44:00 -0600563ulong clk_set_rate(struct clk *clk, ulong rate)
564{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200565 const struct clk_ops *ops;
Patrick Delaunayd867a2872022-06-20 15:37:25 +0200566 struct clk *clkp;
Stephen Warrena9622432016-06-17 09:44:00 -0600567
568 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
developerdc338d32020-01-09 11:35:06 +0800569 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200570 return 0;
571 ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600572
573 if (!ops->set_rate)
574 return -ENOSYS;
575
Patrick Delaunayd867a2872022-06-20 15:37:25 +0200576 /* get private clock struct used for cache */
577 clk_get_priv(clk, &clkp);
Tero Kristo9ab78c12021-06-11 11:45:12 +0300578 /* Clean up cached rates for us and all child clocks */
Patrick Delaunayd867a2872022-06-20 15:37:25 +0200579 clk_clean_rate_cache(clkp);
Tero Kristo9ab78c12021-06-11 11:45:12 +0300580
Stephen Warrena9622432016-06-17 09:44:00 -0600581 return ops->set_rate(clk, rate);
582}
583
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100584int clk_set_parent(struct clk *clk, struct clk *parent)
585{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200586 const struct clk_ops *ops;
Claudiu Bezneac8c16002020-09-07 17:46:34 +0300587 int ret;
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100588
589 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
developerdc338d32020-01-09 11:35:06 +0800590 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200591 return 0;
592 ops = clk_dev_ops(clk->dev);
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100593
594 if (!ops->set_parent)
595 return -ENOSYS;
596
Claudiu Bezneac8c16002020-09-07 17:46:34 +0300597 ret = ops->set_parent(clk, parent);
598 if (ret)
599 return ret;
600
601 if (CONFIG_IS_ENABLED(CLK_CCF))
602 ret = device_reparent(clk->dev, parent->dev);
603
604 return ret;
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100605}
606
Stephen Warrena9622432016-06-17 09:44:00 -0600607int clk_enable(struct clk *clk)
608{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200609 const struct clk_ops *ops;
Peng Fan82628e22019-08-21 13:35:09 +0000610 struct clk *clkp = NULL;
611 int ret;
Stephen Warrena9622432016-06-17 09:44:00 -0600612
613 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800614 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200615 return 0;
616 ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600617
Peng Fan82628e22019-08-21 13:35:09 +0000618 if (CONFIG_IS_ENABLED(CLK_CCF)) {
619 /* Take id 0 as a non-valid clk, such as dummy */
620 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
Yang Xiwencb34b1c2023-11-19 06:10:06 +0800621 ops = clk_dev_ops(clkp->dev);
Peng Fan82628e22019-08-21 13:35:09 +0000622 if (clkp->enable_count) {
623 clkp->enable_count++;
624 return 0;
625 }
626 if (clkp->dev->parent &&
Patrick Delaunaydb9b1a12022-01-24 14:17:14 +0100627 device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
Peng Fan82628e22019-08-21 13:35:09 +0000628 ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
629 if (ret) {
630 printf("Enable %s failed\n",
631 clkp->dev->parent->name);
632 return ret;
633 }
634 }
635 }
Stephen Warrena9622432016-06-17 09:44:00 -0600636
Peng Fan82628e22019-08-21 13:35:09 +0000637 if (ops->enable) {
Maksim Kiselev77e11512023-09-06 01:16:49 +0300638 ret = ops->enable(clkp ? clkp : clk);
Peng Fan82628e22019-08-21 13:35:09 +0000639 if (ret) {
640 printf("Enable %s failed\n", clk->dev->name);
641 return ret;
642 }
643 }
644 if (clkp)
645 clkp->enable_count++;
646 } else {
647 if (!ops->enable)
648 return -ENOSYS;
649 return ops->enable(clk);
650 }
651
652 return 0;
Stephen Warrena9622432016-06-17 09:44:00 -0600653}
654
Neil Armstrong8a275a02018-04-03 11:44:18 +0200655int clk_enable_bulk(struct clk_bulk *bulk)
656{
657 int i, ret;
658
659 for (i = 0; i < bulk->count; i++) {
660 ret = clk_enable(&bulk->clks[i]);
661 if (ret < 0 && ret != -ENOSYS)
662 return ret;
663 }
664
665 return 0;
666}
667
Stephen Warrena9622432016-06-17 09:44:00 -0600668int clk_disable(struct clk *clk)
669{
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200670 const struct clk_ops *ops;
Peng Fan82628e22019-08-21 13:35:09 +0000671 struct clk *clkp = NULL;
672 int ret;
Stephen Warrena9622432016-06-17 09:44:00 -0600673
674 debug("%s(clk=%p)\n", __func__, clk);
developerdc338d32020-01-09 11:35:06 +0800675 if (!clk_valid(clk))
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200676 return 0;
677 ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600678
Peng Fan82628e22019-08-21 13:35:09 +0000679 if (CONFIG_IS_ENABLED(CLK_CCF)) {
680 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
Yang Xiwencb34b1c2023-11-19 06:10:06 +0800681 ops = clk_dev_ops(clkp->dev);
Claudiu Bezneab02e8dd2020-09-07 17:46:35 +0300682 if (clkp->flags & CLK_IS_CRITICAL)
683 return 0;
684
Peng Fan82628e22019-08-21 13:35:09 +0000685 if (clkp->enable_count == 0) {
686 printf("clk %s already disabled\n",
687 clkp->dev->name);
688 return 0;
689 }
Stephen Warrena9622432016-06-17 09:44:00 -0600690
Peng Fan82628e22019-08-21 13:35:09 +0000691 if (--clkp->enable_count > 0)
692 return 0;
693 }
694
695 if (ops->disable) {
Maksim Kiselev77e11512023-09-06 01:16:49 +0300696 ret = ops->disable(clkp ? clkp : clk);
Peng Fan82628e22019-08-21 13:35:09 +0000697 if (ret)
698 return ret;
699 }
700
701 if (clkp && clkp->dev->parent &&
Patrick Delaunaydb9b1a12022-01-24 14:17:14 +0100702 device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
Peng Fan82628e22019-08-21 13:35:09 +0000703 ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent));
704 if (ret) {
705 printf("Disable %s failed\n",
706 clkp->dev->parent->name);
707 return ret;
708 }
709 }
710 } else {
711 if (!ops->disable)
712 return -ENOSYS;
713
714 return ops->disable(clk);
715 }
716
717 return 0;
Stephen Warrena9622432016-06-17 09:44:00 -0600718}
Simon Glass36ad2342015-06-23 15:39:15 -0600719
Neil Armstrong8a275a02018-04-03 11:44:18 +0200720int clk_disable_bulk(struct clk_bulk *bulk)
721{
722 int i, ret;
723
724 for (i = 0; i < bulk->count; i++) {
725 ret = clk_disable(&bulk->clks[i]);
726 if (ret < 0 && ret != -ENOSYS)
727 return ret;
728 }
729
730 return 0;
731}
732
Lukasz Majewski12014be2019-06-24 15:50:44 +0200733int clk_get_by_id(ulong id, struct clk **clkp)
734{
735 struct udevice *dev;
736 struct uclass *uc;
737 int ret;
738
739 ret = uclass_get(UCLASS_CLK, &uc);
740 if (ret)
741 return ret;
742
743 uclass_foreach_dev(dev, uc) {
744 struct clk *clk = dev_get_clk_ptr(dev);
745
746 if (clk && clk->id == id) {
747 *clkp = clk;
748 return 0;
749 }
750 }
751
752 return -ENOENT;
753}
754
Sekhar Noricf3119d2019-08-01 19:12:55 +0530755bool clk_is_match(const struct clk *p, const struct clk *q)
756{
757 /* trivial case: identical struct clk's or both NULL */
758 if (p == q)
759 return true;
760
Jean-Jacques Hiblot718039b2019-10-22 14:00:03 +0200761 /* trivial case #2: on the clk pointer is NULL */
762 if (!p || !q)
763 return false;
764
Sekhar Noricf3119d2019-08-01 19:12:55 +0530765 /* same device, id and data */
766 if (p->dev == q->dev && p->id == q->id && p->data == q->data)
767 return true;
768
769 return false;
770}
771
Jean-Jacques Hiblot6e66b2d2019-10-22 14:00:04 +0200772struct clk *devm_clk_get(struct udevice *dev, const char *id)
773{
774 int rc;
775 struct clk *clk;
776
Sean Andersond318eb32023-12-16 14:38:42 -0500777 clk = devm_kzalloc(dev, sizeof(*clk), GFP_KERNEL);
Jean-Jacques Hiblot6e66b2d2019-10-22 14:00:04 +0200778 if (unlikely(!clk))
779 return ERR_PTR(-ENOMEM);
780
781 rc = clk_get_by_name(dev, id, clk);
782 if (rc)
783 return ERR_PTR(rc);
784
Jean-Jacques Hiblot6e66b2d2019-10-22 14:00:04 +0200785 return clk;
786}
787
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200788int clk_uclass_post_probe(struct udevice *dev)
789{
790 /*
791 * when a clock provider is probed. Call clk_set_defaults()
792 * also after the device is probed. This takes care of cases
793 * where the DT is used to setup default parents and rates
794 * using assigned-clocks
795 */
Marek Vasut05e3d8e2022-01-01 19:51:39 +0100796 clk_set_defaults(dev, CLK_DEFAULTS_POST);
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200797
798 return 0;
799}
800
Simon Glass36ad2342015-06-23 15:39:15 -0600801UCLASS_DRIVER(clk) = {
802 .id = UCLASS_CLK,
803 .name = "clk",
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200804 .post_probe = clk_uclass_post_probe,
Simon Glass36ad2342015-06-23 15:39:15 -0600805};