1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
4 */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <clk.h>
9 #include <asm/clk.h>
10
11 struct sandbox_clk_test {
12 struct clk clks[SANDBOX_CLK_TEST_NON_DEVM_COUNT];
13 struct clk *clkps[SANDBOX_CLK_TEST_ID_COUNT];
14 struct clk_bulk bulk;
15 };
16
17 static const char * const sandbox_clk_test_names[] = {
18 [SANDBOX_CLK_TEST_ID_FIXED] = "fixed",
19 [SANDBOX_CLK_TEST_ID_SPI] = "spi",
20 [SANDBOX_CLK_TEST_ID_I2C] = "i2c",
21 };
22
sandbox_clk_test_get(struct udevice * dev)23 int sandbox_clk_test_get(struct udevice *dev)
24 {
25 struct sandbox_clk_test *sbct = dev_get_priv(dev);
26 int i, ret;
27
28 for (i = 0; i < SANDBOX_CLK_TEST_NON_DEVM_COUNT; i++) {
29 ret = clk_get_by_name(dev, sandbox_clk_test_names[i],
30 &sbct->clks[i]);
31 if (ret)
32 return ret;
33 }
34
35 return 0;
36 }
37
sandbox_clk_test_devm_get(struct udevice * dev)38 int sandbox_clk_test_devm_get(struct udevice *dev)
39 {
40 struct sandbox_clk_test *sbct = dev_get_priv(dev);
41 struct clk *clk;
42
43 clk = devm_clk_get(dev, "no-an-existing-clock");
44 if (!IS_ERR(clk)) {
45 dev_err(dev, "devm_clk_get() should have failed\n");
46 return -EINVAL;
47 }
48
49 clk = devm_clk_get(dev, "uart2");
50 if (IS_ERR(clk))
51 return PTR_ERR(clk);
52 sbct->clkps[SANDBOX_CLK_TEST_ID_DEVM1] = clk;
53
54 clk = devm_clk_get_optional(dev, "uart1");
55 if (IS_ERR(clk))
56 return PTR_ERR(clk);
57 sbct->clkps[SANDBOX_CLK_TEST_ID_DEVM2] = clk;
58
59 sbct->clkps[SANDBOX_CLK_TEST_ID_DEVM_NULL] = NULL;
60 clk = devm_clk_get_optional(dev, "not_an_existing_clock");
61 if (IS_ERR(clk))
62 return PTR_ERR(clk);
63 if (clk)
64 return -EINVAL;
65
66 return 0;
67 }
68
sandbox_clk_test_get_bulk(struct udevice * dev)69 int sandbox_clk_test_get_bulk(struct udevice *dev)
70 {
71 struct sandbox_clk_test *sbct = dev_get_priv(dev);
72
73 return clk_get_bulk(dev, &sbct->bulk);
74 }
75
sandbox_clk_test_get_rate(struct udevice * dev,int id)76 ulong sandbox_clk_test_get_rate(struct udevice *dev, int id)
77 {
78 struct sandbox_clk_test *sbct = dev_get_priv(dev);
79
80 if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
81 return -EINVAL;
82
83 return clk_get_rate(sbct->clkps[id]);
84 }
85
sandbox_clk_test_set_rate(struct udevice * dev,int id,ulong rate)86 ulong sandbox_clk_test_set_rate(struct udevice *dev, int id, ulong rate)
87 {
88 struct sandbox_clk_test *sbct = dev_get_priv(dev);
89
90 if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
91 return -EINVAL;
92
93 return clk_set_rate(sbct->clkps[id], rate);
94 }
95
sandbox_clk_test_enable(struct udevice * dev,int id)96 int sandbox_clk_test_enable(struct udevice *dev, int id)
97 {
98 struct sandbox_clk_test *sbct = dev_get_priv(dev);
99
100 if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
101 return -EINVAL;
102
103 return clk_enable(sbct->clkps[id]);
104 }
105
sandbox_clk_test_enable_bulk(struct udevice * dev)106 int sandbox_clk_test_enable_bulk(struct udevice *dev)
107 {
108 struct sandbox_clk_test *sbct = dev_get_priv(dev);
109
110 return clk_enable_bulk(&sbct->bulk);
111 }
112
sandbox_clk_test_disable(struct udevice * dev,int id)113 int sandbox_clk_test_disable(struct udevice *dev, int id)
114 {
115 struct sandbox_clk_test *sbct = dev_get_priv(dev);
116
117 if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
118 return -EINVAL;
119
120 return clk_disable(sbct->clkps[id]);
121 }
122
sandbox_clk_test_disable_bulk(struct udevice * dev)123 int sandbox_clk_test_disable_bulk(struct udevice *dev)
124 {
125 struct sandbox_clk_test *sbct = dev_get_priv(dev);
126
127 return clk_disable_bulk(&sbct->bulk);
128 }
129
sandbox_clk_test_free(struct udevice * dev)130 int sandbox_clk_test_free(struct udevice *dev)
131 {
132 struct sandbox_clk_test *sbct = dev_get_priv(dev);
133 int i, ret;
134
135 devm_clk_put(dev, sbct->clkps[SANDBOX_CLK_TEST_ID_DEVM1]);
136 for (i = 0; i < SANDBOX_CLK_TEST_NON_DEVM_COUNT; i++) {
137 ret = clk_free(&sbct->clks[i]);
138 if (ret)
139 return ret;
140 }
141
142 return 0;
143 }
144
sandbox_clk_test_release_bulk(struct udevice * dev)145 int sandbox_clk_test_release_bulk(struct udevice *dev)
146 {
147 struct sandbox_clk_test *sbct = dev_get_priv(dev);
148
149 return clk_release_bulk(&sbct->bulk);
150 }
151
sandbox_clk_test_valid(struct udevice * dev)152 int sandbox_clk_test_valid(struct udevice *dev)
153 {
154 struct sandbox_clk_test *sbct = dev_get_priv(dev);
155 int i;
156
157 for (i = 0; i < SANDBOX_CLK_TEST_ID_COUNT; i++) {
158 if (!clk_valid(sbct->clkps[i]))
159 if (i != SANDBOX_CLK_TEST_ID_DEVM_NULL)
160 return -EINVAL;
161 }
162
163 return 0;
164 }
165
sandbox_clk_test_probe(struct udevice * dev)166 static int sandbox_clk_test_probe(struct udevice *dev)
167 {
168 struct sandbox_clk_test *sbct = dev_get_priv(dev);
169 int i;
170
171 for (i = 0; i < SANDBOX_CLK_TEST_ID_DEVM1; i++)
172 sbct->clkps[i] = &sbct->clks[i];
173 for (i = SANDBOX_CLK_TEST_ID_DEVM1; i < SANDBOX_CLK_TEST_ID_COUNT; i++)
174 sbct->clkps[i] = NULL;
175
176 return 0;
177 }
178
179 static const struct udevice_id sandbox_clk_test_ids[] = {
180 { .compatible = "sandbox,clk-test" },
181 { }
182 };
183
184 U_BOOT_DRIVER(sandbox_clk_test) = {
185 .name = "sandbox_clk_test",
186 .id = UCLASS_MISC,
187 .of_match = sandbox_clk_test_ids,
188 .probe = sandbox_clk_test_probe,
189 .priv_auto_alloc_size = sizeof(struct sandbox_clk_test),
190 };
191