1 /*
2  * Copyright (c) 2016, NVIDIA CORPORATION.
3  *
4  * SPDX-License-Identifier: GPL-2.0
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <clk.h>
10 #include <asm/clk.h>
11 
12 struct sandbox_clk_test {
13 	struct clk clks[SANDBOX_CLK_TEST_ID_COUNT];
14 };
15 
16 static const char * const sandbox_clk_test_names[] = {
17 	[SANDBOX_CLK_TEST_ID_FIXED] = "fixed",
18 	[SANDBOX_CLK_TEST_ID_SPI] = "spi",
19 	[SANDBOX_CLK_TEST_ID_I2C] = "i2c",
20 };
21 
22 int sandbox_clk_test_get(struct udevice *dev)
23 {
24 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
25 	int i, ret;
26 
27 	for (i = 0; i < SANDBOX_CLK_TEST_ID_COUNT; i++) {
28 		ret = clk_get_by_name(dev, sandbox_clk_test_names[i],
29 				      &sbct->clks[i]);
30 		if (ret)
31 			return ret;
32 	}
33 
34 	return 0;
35 }
36 
37 ulong sandbox_clk_test_get_rate(struct udevice *dev, int id)
38 {
39 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
40 
41 	if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
42 		return -EINVAL;
43 
44 	return clk_get_rate(&sbct->clks[id]);
45 }
46 
47 ulong sandbox_clk_test_set_rate(struct udevice *dev, int id, ulong rate)
48 {
49 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
50 
51 	if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
52 		return -EINVAL;
53 
54 	return clk_set_rate(&sbct->clks[id], rate);
55 }
56 
57 int sandbox_clk_test_enable(struct udevice *dev, int id)
58 {
59 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
60 
61 	if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
62 		return -EINVAL;
63 
64 	return clk_enable(&sbct->clks[id]);
65 }
66 
67 int sandbox_clk_test_disable(struct udevice *dev, int id)
68 {
69 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
70 
71 	if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
72 		return -EINVAL;
73 
74 	return clk_disable(&sbct->clks[id]);
75 }
76 
77 int sandbox_clk_test_free(struct udevice *dev)
78 {
79 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
80 	int i, ret;
81 
82 	for (i = 0; i < SANDBOX_CLK_TEST_ID_COUNT; i++) {
83 		ret = clk_free(&sbct->clks[i]);
84 		if (ret)
85 			return ret;
86 	}
87 
88 	return 0;
89 }
90 
91 static const struct udevice_id sandbox_clk_test_ids[] = {
92 	{ .compatible = "sandbox,clk-test" },
93 	{ }
94 };
95 
96 U_BOOT_DRIVER(sandbox_clk_test) = {
97 	.name = "sandbox_clk_test",
98 	.id = UCLASS_MISC,
99 	.of_match = sandbox_clk_test_ids,
100 	.priv_auto_alloc_size = sizeof(struct sandbox_clk_test),
101 };
102