1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 * Copyright (c) 2016, NVIDIA CORPORATION.
6 * Copyright (c) 2018, Theobroma Systems Design und Consulting GmbH
11 #include <clk-uclass.h>
14 #include <dt-structs.h>
17 static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
19 return (const struct clk_ops *)dev->driver->ops;
22 #if CONFIG_IS_ENABLED(OF_CONTROL)
23 # if CONFIG_IS_ENABLED(OF_PLATDATA)
24 int clk_get_by_index_platdata(struct udevice *dev, int index,
25 struct phandle_1_arg *cells, struct clk *clk)
31 ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
34 clk->id = cells[0].arg[0];
39 static int clk_of_xlate_default(struct clk *clk,
40 struct ofnode_phandle_args *args)
42 debug("%s(clk=%p)\n", __func__, clk);
44 if (args->args_count > 1) {
45 debug("Invaild args_count: %d\n", args->args_count);
50 clk->id = args->args[0];
57 static int clk_get_by_index_tail(int ret, ofnode node,
58 struct ofnode_phandle_args *args,
59 const char *list_name, int index,
62 struct udevice *dev_clk;
63 const struct clk_ops *ops;
70 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
72 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
79 ops = clk_dev_ops(dev_clk);
82 ret = ops->of_xlate(clk, args);
84 ret = clk_of_xlate_default(clk, args);
86 debug("of_xlate() failed: %d\n", ret);
90 return clk_request(dev_clk, clk);
92 debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n",
93 __func__, ofnode_get_name(node), list_name, index, ret);
97 static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
98 int index, struct clk *clk)
101 struct ofnode_phandle_args args;
103 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
108 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
111 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
117 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
121 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
123 struct ofnode_phandle_args args;
126 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
129 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
133 int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
135 struct ofnode_phandle_args args;
138 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
141 return clk_get_by_index_tail(ret, node, &args, "clocks",
145 int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
147 int i, ret, err, count;
151 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
155 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
159 for (i = 0; i < count; i++) {
160 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
170 err = clk_release_all(bulk->clks, bulk->count);
172 debug("%s: could release all clocks for %p\n",
178 static int clk_set_default_parents(struct udevice *dev)
180 struct clk clk, parent_clk;
185 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
187 if (num_parents < 0) {
188 debug("%s: could not read assigned-clock-parents for %p\n",
193 for (index = 0; index < num_parents; index++) {
194 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
196 /* If -ENOENT, this is a no-op entry */
201 debug("%s: could not get parent clock %d for %s\n",
202 __func__, index, dev_read_name(dev));
206 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
209 debug("%s: could not get assigned clock %d for %s\n",
210 __func__, index, dev_read_name(dev));
214 ret = clk_set_parent(&clk, &parent_clk);
217 * Not all drivers may support clock-reparenting (as of now).
218 * Ignore errors due to this.
224 debug("%s: failed to reparent clock %d for %s\n",
225 __func__, index, dev_read_name(dev));
233 static int clk_set_default_rates(struct udevice *dev)
242 size = dev_read_size(dev, "assigned-clock-rates");
246 num_rates = size / sizeof(u32);
247 rates = calloc(num_rates, sizeof(u32));
251 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
255 for (index = 0; index < num_rates; index++) {
256 /* If 0 is passed, this is a no-op */
260 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
263 debug("%s: could not get assigned clock %d for %s\n",
264 __func__, index, dev_read_name(dev));
268 ret = clk_set_rate(&clk, rates[index]);
270 debug("%s: failed to set rate on clock index %d (%ld) for %s\n",
271 __func__, index, clk.id, dev_read_name(dev));
281 int clk_set_defaults(struct udevice *dev)
285 /* If this not in SPL and pre-reloc state, don't take any action. */
286 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
289 debug("%s(%s)\n", __func__, dev_read_name(dev));
291 ret = clk_set_default_parents(dev);
295 ret = clk_set_default_rates(dev);
301 # endif /* OF_PLATDATA */
303 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
307 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
310 index = dev_read_stringlist_search(dev, "clock-names", name);
312 debug("fdt_stringlist_search() failed: %d\n", index);
316 return clk_get_by_index(dev, index, clk);
319 int clk_release_all(struct clk *clk, int count)
323 for (i = 0; i < count; i++) {
324 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
326 /* check if clock has been previously requested */
330 ret = clk_disable(&clk[i]);
331 if (ret && ret != -ENOSYS)
334 ret = clk_free(&clk[i]);
335 if (ret && ret != -ENOSYS)
342 #endif /* OF_CONTROL */
344 int clk_request(struct udevice *dev, struct clk *clk)
346 const struct clk_ops *ops = clk_dev_ops(dev);
348 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
355 return ops->request(clk);
358 int clk_free(struct clk *clk)
360 const struct clk_ops *ops = clk_dev_ops(clk->dev);
362 debug("%s(clk=%p)\n", __func__, clk);
367 return ops->free(clk);
370 ulong clk_get_rate(struct clk *clk)
372 const struct clk_ops *ops = clk_dev_ops(clk->dev);
374 debug("%s(clk=%p)\n", __func__, clk);
379 return ops->get_rate(clk);
382 ulong clk_set_rate(struct clk *clk, ulong rate)
384 const struct clk_ops *ops = clk_dev_ops(clk->dev);
386 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
391 return ops->set_rate(clk, rate);
394 int clk_set_parent(struct clk *clk, struct clk *parent)
396 const struct clk_ops *ops = clk_dev_ops(clk->dev);
398 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
400 if (!ops->set_parent)
403 return ops->set_parent(clk, parent);
406 int clk_enable(struct clk *clk)
408 const struct clk_ops *ops = clk_dev_ops(clk->dev);
410 debug("%s(clk=%p)\n", __func__, clk);
415 return ops->enable(clk);
418 int clk_enable_bulk(struct clk_bulk *bulk)
422 for (i = 0; i < bulk->count; i++) {
423 ret = clk_enable(&bulk->clks[i]);
424 if (ret < 0 && ret != -ENOSYS)
431 int clk_disable(struct clk *clk)
433 const struct clk_ops *ops = clk_dev_ops(clk->dev);
435 debug("%s(clk=%p)\n", __func__, clk);
440 return ops->disable(clk);
443 int clk_disable_bulk(struct clk_bulk *bulk)
447 for (i = 0; i < bulk->count; i++) {
448 ret = clk_disable(&bulk->clks[i]);
449 if (ret < 0 && ret != -ENOSYS)
456 UCLASS_DRIVER(clk) = {