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