]> Git Repo - J-u-boot.git/blob - drivers/clk/clk_sandbox.c
global: Rename SPL_ to XPL_
[J-u-boot.git] / drivers / clk / clk_sandbox.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2015 Google, Inc
4  */
5
6 #include <clk-uclass.h>
7 #include <dm.h>
8 #include <errno.h>
9 #include <malloc.h>
10 #include <asm/clk.h>
11 #include <linux/clk-provider.h>
12
13 static ulong sandbox_clk_get_rate(struct clk *clk)
14 {
15         struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
16
17         if (!priv->probed)
18                 return -ENODEV;
19
20         if (clk->id >= SANDBOX_CLK_ID_COUNT)
21                 return -EINVAL;
22
23         return priv->rate[clk->id];
24 }
25
26 static ulong sandbox_clk_round_rate(struct clk *clk, ulong rate)
27 {
28         struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
29
30         if (!priv->probed)
31                 return -ENODEV;
32
33         if (clk->id >= SANDBOX_CLK_ID_COUNT)
34                 return -EINVAL;
35
36         if (!rate)
37                 return -EINVAL;
38
39         return rate;
40 }
41
42 static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate)
43 {
44         struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
45         ulong old_rate;
46
47         if (!priv->probed)
48                 return -ENODEV;
49
50         if (clk->id >= SANDBOX_CLK_ID_COUNT)
51                 return -EINVAL;
52
53         if (!rate)
54                 return -EINVAL;
55
56         old_rate = priv->rate[clk->id];
57         priv->rate[clk->id] = rate;
58
59         return old_rate;
60 }
61
62 static int sandbox_clk_enable(struct clk *clk)
63 {
64         struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
65
66         if (!priv->probed)
67                 return -ENODEV;
68
69         if (clk->id >= SANDBOX_CLK_ID_COUNT)
70                 return -EINVAL;
71
72         priv->enabled[clk->id] = true;
73
74         return 0;
75 }
76
77 static int sandbox_clk_disable(struct clk *clk)
78 {
79         struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
80
81         if (!priv->probed)
82                 return -ENODEV;
83
84         if (clk->id >= SANDBOX_CLK_ID_COUNT)
85                 return -EINVAL;
86
87         priv->enabled[clk->id] = false;
88
89         return 0;
90 }
91
92 static int sandbox_clk_request(struct clk *clk)
93 {
94         struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
95
96         if (clk->id >= SANDBOX_CLK_ID_COUNT)
97                 return -EINVAL;
98
99         priv->requested[clk->id] = true;
100         return 0;
101 }
102
103 static struct clk_ops sandbox_clk_ops = {
104         .round_rate     = sandbox_clk_round_rate,
105         .get_rate       = sandbox_clk_get_rate,
106         .set_rate       = sandbox_clk_set_rate,
107         .enable         = sandbox_clk_enable,
108         .disable        = sandbox_clk_disable,
109         .request        = sandbox_clk_request,
110 };
111
112 static int sandbox_clk_probe(struct udevice *dev)
113 {
114         struct sandbox_clk_priv *priv = dev_get_priv(dev);
115
116         priv->probed = true;
117         return 0;
118 }
119
120 static const struct udevice_id sandbox_clk_ids[] = {
121         { .compatible = "sandbox,clk" },
122         { }
123 };
124
125 U_BOOT_DRIVER(sandbox_clk) = {
126         .name           = "sandbox_clk",
127         .id             = UCLASS_CLK,
128         .of_match       = sandbox_clk_ids,
129         .ops            = &sandbox_clk_ops,
130         .probe          = sandbox_clk_probe,
131         .priv_auto      = sizeof(struct sandbox_clk_priv),
132 };
133
134 ulong sandbox_clk_query_rate(struct udevice *dev, int id)
135 {
136         struct sandbox_clk_priv *priv = dev_get_priv(dev);
137
138         if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
139                 return -EINVAL;
140
141         return priv->rate[id];
142 }
143
144 int sandbox_clk_query_enable(struct udevice *dev, int id)
145 {
146         struct sandbox_clk_priv *priv = dev_get_priv(dev);
147
148         if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
149                 return -EINVAL;
150
151         return priv->enabled[id];
152 }
153
154 int sandbox_clk_query_requested(struct udevice *dev, int id)
155 {
156         struct sandbox_clk_priv *priv = dev_get_priv(dev);
157
158         if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
159                 return -EINVAL;
160         return priv->requested[id];
161 }
162
163 int clk_fixed_rate_of_to_plat(struct udevice *dev)
164 {
165         struct clk_fixed_rate *cplat;
166
167 #if CONFIG_IS_ENABLED(OF_PLATDATA)
168         struct sandbox_clk_fixed_rate_plat *plat = dev_get_plat(dev);
169
170         cplat = &plat->fixed;
171         cplat->fixed_rate = plat->dtplat.clock_frequency;
172 #else
173         cplat = to_clk_fixed_rate(dev);
174 #endif
175         clk_fixed_rate_ofdata_to_plat_(dev, cplat);
176
177         return 0;
178 }
179
180 static const struct udevice_id sandbox_clk_fixed_rate_match[] = {
181         { .compatible = "sandbox,fixed-clock" },
182         { /* sentinel */ }
183 };
184
185 U_BOOT_DRIVER(sandbox_fixed_clock) = {
186         .name = "sandbox_fixed_clock",
187         .id = UCLASS_CLK,
188         .of_match = sandbox_clk_fixed_rate_match,
189         .of_to_plat = clk_fixed_rate_of_to_plat,
190         .plat_auto = sizeof(struct sandbox_clk_fixed_rate_plat),
191         .ops = &clk_fixed_rate_ops,
192         .flags = DM_FLAG_PRE_RELOC,
193 };
This page took 0.036413 seconds and 4 git commands to generate.