1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Xilinx, Inc.
10 #include <dm/device-internal.h>
11 #include <linux/clk-provider.h>
13 static void show_clks(struct udevice
*dev
, int depth
, int last_flag
)
16 struct udevice
*child
;
17 struct clk
*clkp
, *parent
;
20 clkp
= dev_get_clk_ptr(dev
);
22 parent
= clk_get_parent(clkp
);
23 if (!IS_ERR(parent
) && depth
== -1)
26 rate
= clk_get_rate(clkp
);
28 printf(" %-12u %8d ", rate
, clkp
->enable_count
);
30 for (i
= depth
; i
>= 0; i
--) {
31 is_last
= (last_flag
>> i
) & 1;
45 printf("%s\n", dev
->name
);
48 device_foreach_child_probe(child
, dev
) {
49 if (device_get_uclass_id(child
) != UCLASS_CLK
)
53 is_last
= list_is_last(&child
->sibling_node
, &dev
->child_head
);
54 show_clks(child
, depth
, (last_flag
<< 1) | is_last
);
58 static int soc_clk_dump(void)
61 const struct clk_ops
*ops
;
63 printf(" Rate Usecnt Name\n");
64 printf("------------------------------------------\n");
66 uclass_foreach_dev_probe(UCLASS_CLK
, dev
)
67 show_clks(dev
, -1, 0);
69 uclass_foreach_dev_probe(UCLASS_CLK
, dev
) {
70 ops
= dev_get_driver_ops(dev
);
71 if (ops
&& ops
->dump
) {
72 printf("\n%s %s:\n", dev
->driver
->name
, dev
->name
);
80 static int do_clk_dump(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
87 printf("Clock dump error %d\n", ret
);
88 ret
= CMD_RET_FAILURE
;
94 static int do_clk_setfreq(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
97 struct clk
*clk
= NULL
;
102 return CMD_RET_USAGE
;
104 freq
= dectoul(argv
[2], NULL
);
106 if (!uclass_get_device_by_name(UCLASS_CLK
, argv
[1], &dev
))
107 clk
= dev_get_clk_ptr(dev
);
110 printf("clock '%s' not found.\n", argv
[1]);
111 return CMD_RET_FAILURE
;
114 freq
= clk_set_rate(clk
, freq
);
116 printf("set_rate failed: %d\n", freq
);
117 return CMD_RET_FAILURE
;
120 printf("set_rate returns %u\n", freq
);
124 static struct cmd_tbl cmd_clk_sub
[] = {
125 U_BOOT_CMD_MKENT(dump
, 1, 1, do_clk_dump
, "", ""),
126 U_BOOT_CMD_MKENT(setfreq
, 3, 1, do_clk_setfreq
, "", ""),
129 static int do_clk(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
135 return CMD_RET_USAGE
;
137 /* Strip off leading 'clk' command argument */
141 c
= find_cmd_tbl(argv
[0], &cmd_clk_sub
[0], ARRAY_SIZE(cmd_clk_sub
));
144 return c
->cmd(cmdtp
, flag
, argc
, argv
);
146 return CMD_RET_USAGE
;
150 "dump - Print clock frequencies\n"
151 "clk setfreq [clk] [freq] - Set clock frequency");
153 U_BOOT_CMD(clk
, 4, 1, do_clk
, "CLK sub-system", clk_help_text
);