blob: c8c5a88c52d985632733030b0cb92970920a5fc5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glass8cc4d822015-07-06 12:54:24 -06002/*
3 * (C) Copyright 2015 Google, Inc
Simon Glass8cc4d822015-07-06 12:54:24 -06004 */
5
Stephen Warrena9622432016-06-17 09:44:00 -06006#include <clk-uclass.h>
Simon Glass8cc4d822015-07-06 12:54:24 -06007#include <dm.h>
8#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Stephen Warrena9622432016-06-17 09:44:00 -060010#include <asm/clk.h>
Simon Glass9bb88fb2021-03-15 17:25:24 +130011#include <linux/clk-provider.h>
Simon Glass8cc4d822015-07-06 12:54:24 -060012
Stephen Warrena9622432016-06-17 09:44:00 -060013static ulong sandbox_clk_get_rate(struct clk *clk)
Simon Glass8cc4d822015-07-06 12:54:24 -060014{
Stephen Warrena9622432016-06-17 09:44:00 -060015 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
Patrick Delaunaya759ce32025-05-27 15:27:47 +020016 ulong id = clk_get_id(clk);
Stephen Warrena9622432016-06-17 09:44:00 -060017
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020018 if (!priv->probed)
19 return -ENODEV;
20
Patrick Delaunaya759ce32025-05-27 15:27:47 +020021 if (id >= SANDBOX_CLK_ID_COUNT)
Stephen Warrena9622432016-06-17 09:44:00 -060022 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060023
Patrick Delaunaya759ce32025-05-27 15:27:47 +020024 return priv->rate[id];
Simon Glass8cc4d822015-07-06 12:54:24 -060025}
26
Dario Binacchib7f85892020-12-30 00:06:31 +010027static ulong sandbox_clk_round_rate(struct clk *clk, ulong rate)
28{
29 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
Patrick Delaunaya759ce32025-05-27 15:27:47 +020030 ulong id = clk_get_id(clk);
Dario Binacchib7f85892020-12-30 00:06:31 +010031
32 if (!priv->probed)
33 return -ENODEV;
34
Patrick Delaunaya759ce32025-05-27 15:27:47 +020035 if (id >= SANDBOX_CLK_ID_COUNT)
Dario Binacchib7f85892020-12-30 00:06:31 +010036 return -EINVAL;
37
38 if (!rate)
39 return -EINVAL;
40
41 return rate;
42}
43
Stephen Warrena9622432016-06-17 09:44:00 -060044static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate)
Simon Glass8cc4d822015-07-06 12:54:24 -060045{
Stephen Warrena9622432016-06-17 09:44:00 -060046 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
47 ulong old_rate;
Patrick Delaunaya759ce32025-05-27 15:27:47 +020048 ulong id = clk_get_id(clk);
Stephen Warrena9622432016-06-17 09:44:00 -060049
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020050 if (!priv->probed)
51 return -ENODEV;
52
Patrick Delaunaya759ce32025-05-27 15:27:47 +020053 if (id >= SANDBOX_CLK_ID_COUNT)
Stephen Warrena9622432016-06-17 09:44:00 -060054 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060055
56 if (!rate)
57 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060058
Patrick Delaunaya759ce32025-05-27 15:27:47 +020059 old_rate = priv->rate[id];
60 priv->rate[id] = rate;
Simon Glass8cc4d822015-07-06 12:54:24 -060061
Stephen Warrena9622432016-06-17 09:44:00 -060062 return old_rate;
Simon Glass8cc4d822015-07-06 12:54:24 -060063}
64
Stephen Warrena9622432016-06-17 09:44:00 -060065static int sandbox_clk_enable(struct clk *clk)
Simon Glass8cc4d822015-07-06 12:54:24 -060066{
Stephen Warrena9622432016-06-17 09:44:00 -060067 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
Patrick Delaunaya759ce32025-05-27 15:27:47 +020068 ulong id = clk_get_id(clk);
Simon Glass8cc4d822015-07-06 12:54:24 -060069
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020070 if (!priv->probed)
71 return -ENODEV;
72
Patrick Delaunaya759ce32025-05-27 15:27:47 +020073 if (id >= SANDBOX_CLK_ID_COUNT)
Simon Glass8cc4d822015-07-06 12:54:24 -060074 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060075
Patrick Delaunaya759ce32025-05-27 15:27:47 +020076 priv->enabled[id] = true;
Stephen Warrena9622432016-06-17 09:44:00 -060077
78 return 0;
Simon Glass8cc4d822015-07-06 12:54:24 -060079}
80
Stephen Warrena9622432016-06-17 09:44:00 -060081static int sandbox_clk_disable(struct clk *clk)
Simon Glass8cc4d822015-07-06 12:54:24 -060082{
Stephen Warrena9622432016-06-17 09:44:00 -060083 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
Patrick Delaunaya759ce32025-05-27 15:27:47 +020084 ulong id = clk_get_id(clk);
Stephen Warrena9622432016-06-17 09:44:00 -060085
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020086 if (!priv->probed)
87 return -ENODEV;
88
Patrick Delaunaya759ce32025-05-27 15:27:47 +020089 if (id >= SANDBOX_CLK_ID_COUNT)
Stephen Warrena9622432016-06-17 09:44:00 -060090 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060091
Patrick Delaunaya759ce32025-05-27 15:27:47 +020092 priv->enabled[id] = false;
Simon Glass8cc4d822015-07-06 12:54:24 -060093
94 return 0;
95}
96
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020097static int sandbox_clk_request(struct clk *clk)
98{
99 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
Patrick Delaunaya759ce32025-05-27 15:27:47 +0200100 ulong id = clk_get_id(clk);
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200101
Patrick Delaunaya759ce32025-05-27 15:27:47 +0200102 if (id >= SANDBOX_CLK_ID_COUNT)
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200103 return -EINVAL;
104
Patrick Delaunaya759ce32025-05-27 15:27:47 +0200105 priv->requested[id] = true;
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200106 return 0;
107}
108
Simon Glass8cc4d822015-07-06 12:54:24 -0600109static struct clk_ops sandbox_clk_ops = {
Dario Binacchib7f85892020-12-30 00:06:31 +0100110 .round_rate = sandbox_clk_round_rate,
Simon Glass8cc4d822015-07-06 12:54:24 -0600111 .get_rate = sandbox_clk_get_rate,
112 .set_rate = sandbox_clk_set_rate,
Stephen Warrena9622432016-06-17 09:44:00 -0600113 .enable = sandbox_clk_enable,
114 .disable = sandbox_clk_disable,
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200115 .request = sandbox_clk_request,
Simon Glass8cc4d822015-07-06 12:54:24 -0600116};
117
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +0200118static int sandbox_clk_probe(struct udevice *dev)
119{
120 struct sandbox_clk_priv *priv = dev_get_priv(dev);
121
122 priv->probed = true;
123 return 0;
124}
125
Simon Glass8cc4d822015-07-06 12:54:24 -0600126static const struct udevice_id sandbox_clk_ids[] = {
127 { .compatible = "sandbox,clk" },
128 { }
129};
130
Simon Glass6b927b12020-10-03 11:31:32 -0600131U_BOOT_DRIVER(sandbox_clk) = {
132 .name = "sandbox_clk",
Simon Glass8cc4d822015-07-06 12:54:24 -0600133 .id = UCLASS_CLK,
134 .of_match = sandbox_clk_ids,
135 .ops = &sandbox_clk_ops,
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +0200136 .probe = sandbox_clk_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700137 .priv_auto = sizeof(struct sandbox_clk_priv),
Simon Glass8cc4d822015-07-06 12:54:24 -0600138};
Stephen Warrena9622432016-06-17 09:44:00 -0600139
140ulong sandbox_clk_query_rate(struct udevice *dev, int id)
141{
142 struct sandbox_clk_priv *priv = dev_get_priv(dev);
143
144 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
145 return -EINVAL;
146
147 return priv->rate[id];
148}
149
150int sandbox_clk_query_enable(struct udevice *dev, int id)
151{
152 struct sandbox_clk_priv *priv = dev_get_priv(dev);
153
154 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
155 return -EINVAL;
156
157 return priv->enabled[id];
158}
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200159
160int sandbox_clk_query_requested(struct udevice *dev, int id)
161{
162 struct sandbox_clk_priv *priv = dev_get_priv(dev);
163
164 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
165 return -EINVAL;
166 return priv->requested[id];
167}
Simon Glass9bb88fb2021-03-15 17:25:24 +1300168
169int clk_fixed_rate_of_to_plat(struct udevice *dev)
170{
171 struct clk_fixed_rate *cplat;
172
173#if CONFIG_IS_ENABLED(OF_PLATDATA)
174 struct sandbox_clk_fixed_rate_plat *plat = dev_get_plat(dev);
175
176 cplat = &plat->fixed;
177 cplat->fixed_rate = plat->dtplat.clock_frequency;
178#else
179 cplat = to_clk_fixed_rate(dev);
180#endif
181 clk_fixed_rate_ofdata_to_plat_(dev, cplat);
182
183 return 0;
184}
185
186static const struct udevice_id sandbox_clk_fixed_rate_match[] = {
187 { .compatible = "sandbox,fixed-clock" },
188 { /* sentinel */ }
189};
190
191U_BOOT_DRIVER(sandbox_fixed_clock) = {
192 .name = "sandbox_fixed_clock",
193 .id = UCLASS_CLK,
194 .of_match = sandbox_clk_fixed_rate_match,
195 .of_to_plat = clk_fixed_rate_of_to_plat,
196 .plat_auto = sizeof(struct sandbox_clk_fixed_rate_plat),
197 .ops = &clk_fixed_rate_ops,
198 .flags = DM_FLAG_PRE_RELOC,
199};