1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Xilinx, Inc.
8 #if defined(CONFIG_DM) && defined(CONFIG_CLK)
10 #include <dm/device.h>
12 #include <dm/device-internal.h>
13 #include <linux/clk-provider.h>
16 #if defined(CONFIG_DM) && defined(CONFIG_CLK)
17 static void show_clks(struct udevice *dev, int depth, int last_flag)
20 struct udevice *child;
21 struct clk *clkp, *parent;
24 clkp = dev_get_clk_ptr(dev);
25 if (device_get_uclass_id(dev) == UCLASS_CLK && clkp) {
26 parent = clk_get_parent(clkp);
27 if (!IS_ERR(parent) && depth == -1)
30 rate = clk_get_rate(clkp);
32 printf(" %-12u %8d ", rate, clkp->enable_count);
34 for (i = depth; i >= 0; i--) {
35 is_last = (last_flag >> i) & 1;
49 printf("%s\n", dev->name);
52 list_for_each_entry(child, &dev->child_head, sibling_node) {
56 is_last = list_is_last(&child->sibling_node, &dev->child_head);
57 show_clks(child, depth, (last_flag << 1) | is_last);
61 int __weak soc_clk_dump(void)
67 ret = uclass_get(UCLASS_CLK, &uc);
71 printf(" Rate Usecnt Name\n");
72 printf("------------------------------------------\n");
74 uclass_foreach_dev(dev, uc)
75 show_clks(dev, -1, 0);
80 int __weak soc_clk_dump(void)
82 puts("Not implemented\n");
87 static int do_clk_dump(struct cmd_tbl *cmdtp, int flag, int argc,
94 printf("Clock dump error %d\n", ret);
95 ret = CMD_RET_FAILURE;
101 #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK)
102 struct udevice *clk_lookup(const char *name)
108 uclass_get_device(UCLASS_CLK, i++, &dev);
109 if (!strcmp(name, dev->name))
116 static int do_clk_setfreq(struct cmd_tbl *cmdtp, int flag, int argc,
119 struct clk *clk = NULL;
123 freq = dectoul(argv[2], NULL);
125 dev = clk_lookup(argv[1]);
128 clk = dev_get_clk_ptr(dev);
131 printf("clock '%s' not found.\n", argv[1]);
135 freq = clk_set_rate(clk, freq);
137 printf("set_rate failed: %d\n", freq);
138 return CMD_RET_FAILURE;
141 printf("set_rate returns %u\n", freq);
146 static struct cmd_tbl cmd_clk_sub[] = {
147 U_BOOT_CMD_MKENT(dump, 1, 1, do_clk_dump, "", ""),
148 #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(CLK)
149 U_BOOT_CMD_MKENT(setfreq, 3, 1, do_clk_setfreq, "", ""),
153 static int do_clk(struct cmd_tbl *cmdtp, int flag, int argc,
159 return CMD_RET_USAGE;
161 /* Strip off leading 'clk' command argument */
165 c = find_cmd_tbl(argv[0], &cmd_clk_sub[0], ARRAY_SIZE(cmd_clk_sub));
168 return c->cmd(cmdtp, flag, argc, argv);
170 return CMD_RET_USAGE;
173 #ifdef CONFIG_SYS_LONGHELP
174 static char clk_help_text[] =
175 "dump - Print clock frequencies\n"
176 "setfreq [clk] [freq] - Set clock frequency";
179 U_BOOT_CMD(clk, 4, 1, do_clk, "CLK sub-system", clk_help_text);