blob: 41954660ea139ee7c962de70af7e4afd5fc8ad22 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warrena9622432016-06-17 09:44:00 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
Stephen Warrena9622432016-06-17 09:44:00 -06004 */
5
6#include <common.h>
7#include <dm.h>
8#include <clk.h>
9#include <asm/clk.h>
10
11struct sandbox_clk_test {
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020012 struct clk clks[SANDBOX_CLK_TEST_NON_DEVM_COUNT];
13 struct clk *clkps[SANDBOX_CLK_TEST_ID_COUNT];
Neil Armstrong567a38b2018-04-03 11:44:19 +020014 struct clk_bulk bulk;
Stephen Warrena9622432016-06-17 09:44:00 -060015};
16
17static 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
23int sandbox_clk_test_get(struct udevice *dev)
24{
25 struct sandbox_clk_test *sbct = dev_get_priv(dev);
26 int i, ret;
27
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020028 for (i = 0; i < SANDBOX_CLK_TEST_NON_DEVM_COUNT; i++) {
Stephen Warrena9622432016-06-17 09:44:00 -060029 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
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020038int 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
Neil Armstrong567a38b2018-04-03 11:44:19 +020069int 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
Stephen Warrena9622432016-06-17 09:44:00 -060076ulong 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
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020083 return clk_get_rate(sbct->clkps[id]);
Stephen Warrena9622432016-06-17 09:44:00 -060084}
85
86ulong 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
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020093 return clk_set_rate(sbct->clkps[id], rate);
Stephen Warrena9622432016-06-17 09:44:00 -060094}
95
96int 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
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200103 return clk_enable(sbct->clkps[id]);
Stephen Warrena9622432016-06-17 09:44:00 -0600104}
105
Neil Armstrong567a38b2018-04-03 11:44:19 +0200106int 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
Stephen Warrena9622432016-06-17 09:44:00 -0600113int 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
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200120 return clk_disable(sbct->clkps[id]);
Stephen Warrena9622432016-06-17 09:44:00 -0600121}
122
Neil Armstrong567a38b2018-04-03 11:44:19 +0200123int 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
Stephen Warrena9622432016-06-17 09:44:00 -0600130int sandbox_clk_test_free(struct udevice *dev)
131{
132 struct sandbox_clk_test *sbct = dev_get_priv(dev);
133 int i, ret;
134
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200135 devm_clk_put(dev, sbct->clkps[SANDBOX_CLK_TEST_ID_DEVM1]);
136 for (i = 0; i < SANDBOX_CLK_TEST_NON_DEVM_COUNT; i++) {
Stephen Warrena9622432016-06-17 09:44:00 -0600137 ret = clk_free(&sbct->clks[i]);
138 if (ret)
139 return ret;
140 }
141
142 return 0;
143}
144
Neil Armstrong567a38b2018-04-03 11:44:19 +0200145int 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
Fabrice Gasnier11192712018-07-24 16:31:28 +0200152int 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++) {
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200158 if (!clk_valid(sbct->clkps[i]))
159 if (i != SANDBOX_CLK_TEST_ID_DEVM_NULL)
160 return -EINVAL;
Fabrice Gasnier11192712018-07-24 16:31:28 +0200161 }
162
163 return 0;
164}
165
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200166static 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
Stephen Warrena9622432016-06-17 09:44:00 -0600179static const struct udevice_id sandbox_clk_test_ids[] = {
180 { .compatible = "sandbox,clk-test" },
181 { }
182};
183
184U_BOOT_DRIVER(sandbox_clk_test) = {
185 .name = "sandbox_clk_test",
186 .id = UCLASS_MISC,
187 .of_match = sandbox_clk_test_ids,
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200188 .probe = sandbox_clk_test_probe,
Stephen Warrena9622432016-06-17 09:44:00 -0600189 .priv_auto_alloc_size = sizeof(struct sandbox_clk_test),
190};