1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2015 Google, Inc
7 #include <clk-uclass.h>
12 #include <linux/clk-provider.h>
14 static ulong sandbox_clk_get_rate(struct clk *clk)
16 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
21 if (clk->id >= SANDBOX_CLK_ID_COUNT)
24 return priv->rate[clk->id];
27 static ulong sandbox_clk_round_rate(struct clk *clk, ulong rate)
29 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
34 if (clk->id >= SANDBOX_CLK_ID_COUNT)
43 static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate)
45 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
51 if (clk->id >= SANDBOX_CLK_ID_COUNT)
57 old_rate = priv->rate[clk->id];
58 priv->rate[clk->id] = rate;
63 static int sandbox_clk_enable(struct clk *clk)
65 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
70 if (clk->id >= SANDBOX_CLK_ID_COUNT)
73 priv->enabled[clk->id] = true;
78 static int sandbox_clk_disable(struct clk *clk)
80 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
85 if (clk->id >= SANDBOX_CLK_ID_COUNT)
88 priv->enabled[clk->id] = false;
93 static int sandbox_clk_request(struct clk *clk)
95 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
97 if (clk->id >= SANDBOX_CLK_ID_COUNT)
100 priv->requested[clk->id] = true;
104 static void sandbox_clk_free(struct clk *clk)
106 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
108 if (clk->id >= SANDBOX_CLK_ID_COUNT)
111 priv->requested[clk->id] = false;
115 static struct clk_ops sandbox_clk_ops = {
116 .round_rate = sandbox_clk_round_rate,
117 .get_rate = sandbox_clk_get_rate,
118 .set_rate = sandbox_clk_set_rate,
119 .enable = sandbox_clk_enable,
120 .disable = sandbox_clk_disable,
121 .request = sandbox_clk_request,
122 .rfree = sandbox_clk_free,
125 static int sandbox_clk_probe(struct udevice *dev)
127 struct sandbox_clk_priv *priv = dev_get_priv(dev);
133 static const struct udevice_id sandbox_clk_ids[] = {
134 { .compatible = "sandbox,clk" },
138 U_BOOT_DRIVER(sandbox_clk) = {
139 .name = "sandbox_clk",
141 .of_match = sandbox_clk_ids,
142 .ops = &sandbox_clk_ops,
143 .probe = sandbox_clk_probe,
144 .priv_auto = sizeof(struct sandbox_clk_priv),
147 ulong sandbox_clk_query_rate(struct udevice *dev, int id)
149 struct sandbox_clk_priv *priv = dev_get_priv(dev);
151 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
154 return priv->rate[id];
157 int sandbox_clk_query_enable(struct udevice *dev, int id)
159 struct sandbox_clk_priv *priv = dev_get_priv(dev);
161 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
164 return priv->enabled[id];
167 int sandbox_clk_query_requested(struct udevice *dev, int id)
169 struct sandbox_clk_priv *priv = dev_get_priv(dev);
171 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
173 return priv->requested[id];
176 int clk_fixed_rate_of_to_plat(struct udevice *dev)
178 struct clk_fixed_rate *cplat;
180 #if CONFIG_IS_ENABLED(OF_PLATDATA)
181 struct sandbox_clk_fixed_rate_plat *plat = dev_get_plat(dev);
183 cplat = &plat->fixed;
184 cplat->fixed_rate = plat->dtplat.clock_frequency;
186 cplat = to_clk_fixed_rate(dev);
188 clk_fixed_rate_ofdata_to_plat_(dev, cplat);
193 static const struct udevice_id sandbox_clk_fixed_rate_match[] = {
194 { .compatible = "sandbox,fixed-clock" },
198 U_BOOT_DRIVER(sandbox_fixed_clock) = {
199 .name = "sandbox_fixed_clock",
201 .of_match = sandbox_clk_fixed_rate_match,
202 .of_to_plat = clk_fixed_rate_of_to_plat,
203 .plat_auto = sizeof(struct sandbox_clk_fixed_rate_plat),
204 .ops = &clk_fixed_rate_ops,
205 .flags = DM_FLAG_PRE_RELOC,