blob: 768fbb7c52030f58e8c996ef464bdb7af03d46e6 [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
6#include <common.h>
Stephen Warrena9622432016-06-17 09:44:00 -06007#include <clk-uclass.h>
Simon Glass8cc4d822015-07-06 12:54:24 -06008#include <dm.h>
9#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Stephen Warrena9622432016-06-17 09:44:00 -060011#include <asm/clk.h>
Simon Glass8cc4d822015-07-06 12:54:24 -060012
13struct sandbox_clk_priv {
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020014 bool probed;
Stephen Warrena9622432016-06-17 09:44:00 -060015 ulong rate[SANDBOX_CLK_ID_COUNT];
16 bool enabled[SANDBOX_CLK_ID_COUNT];
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020017 bool requested[SANDBOX_CLK_ID_COUNT];
Simon Glass8cc4d822015-07-06 12:54:24 -060018};
19
Stephen Warrena9622432016-06-17 09:44:00 -060020static ulong sandbox_clk_get_rate(struct clk *clk)
Simon Glass8cc4d822015-07-06 12:54:24 -060021{
Stephen Warrena9622432016-06-17 09:44:00 -060022 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
23
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020024 if (!priv->probed)
25 return -ENODEV;
26
Stephen Warrendaec47b2016-06-21 13:32:07 -060027 if (clk->id >= SANDBOX_CLK_ID_COUNT)
Stephen Warrena9622432016-06-17 09:44:00 -060028 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060029
Stephen Warrena9622432016-06-17 09:44:00 -060030 return priv->rate[clk->id];
Simon Glass8cc4d822015-07-06 12:54:24 -060031}
32
Stephen Warrena9622432016-06-17 09:44:00 -060033static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate)
Simon Glass8cc4d822015-07-06 12:54:24 -060034{
Stephen Warrena9622432016-06-17 09:44:00 -060035 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
36 ulong old_rate;
37
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020038 if (!priv->probed)
39 return -ENODEV;
40
Stephen Warrendaec47b2016-06-21 13:32:07 -060041 if (clk->id >= SANDBOX_CLK_ID_COUNT)
Stephen Warrena9622432016-06-17 09:44:00 -060042 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060043
44 if (!rate)
45 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060046
Stephen Warrena9622432016-06-17 09:44:00 -060047 old_rate = priv->rate[clk->id];
48 priv->rate[clk->id] = rate;
Simon Glass8cc4d822015-07-06 12:54:24 -060049
Stephen Warrena9622432016-06-17 09:44:00 -060050 return old_rate;
Simon Glass8cc4d822015-07-06 12:54:24 -060051}
52
Stephen Warrena9622432016-06-17 09:44:00 -060053static int sandbox_clk_enable(struct clk *clk)
Simon Glass8cc4d822015-07-06 12:54:24 -060054{
Stephen Warrena9622432016-06-17 09:44:00 -060055 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass8cc4d822015-07-06 12:54:24 -060056
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020057 if (!priv->probed)
58 return -ENODEV;
59
Stephen Warrendaec47b2016-06-21 13:32:07 -060060 if (clk->id >= SANDBOX_CLK_ID_COUNT)
Simon Glass8cc4d822015-07-06 12:54:24 -060061 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060062
Stephen Warrena9622432016-06-17 09:44:00 -060063 priv->enabled[clk->id] = true;
64
65 return 0;
Simon Glass8cc4d822015-07-06 12:54:24 -060066}
67
Stephen Warrena9622432016-06-17 09:44:00 -060068static int sandbox_clk_disable(struct clk *clk)
Simon Glass8cc4d822015-07-06 12:54:24 -060069{
Stephen Warrena9622432016-06-17 09:44:00 -060070 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
71
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020072 if (!priv->probed)
73 return -ENODEV;
74
Stephen Warrendaec47b2016-06-21 13:32:07 -060075 if (clk->id >= SANDBOX_CLK_ID_COUNT)
Stephen Warrena9622432016-06-17 09:44:00 -060076 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060077
Stephen Warrena9622432016-06-17 09:44:00 -060078 priv->enabled[clk->id] = false;
Simon Glass8cc4d822015-07-06 12:54:24 -060079
80 return 0;
81}
82
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020083static int sandbox_clk_request(struct clk *clk)
84{
85 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
86
87 if (clk->id >= SANDBOX_CLK_ID_COUNT)
88 return -EINVAL;
89
90 priv->requested[clk->id] = true;
91 return 0;
92}
93
94static int sandbox_clk_free(struct clk *clk)
95{
96 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
97
98 if (clk->id >= SANDBOX_CLK_ID_COUNT)
99 return -EINVAL;
100
101 priv->requested[clk->id] = false;
102 return 0;
103}
104
Simon Glass8cc4d822015-07-06 12:54:24 -0600105static struct clk_ops sandbox_clk_ops = {
106 .get_rate = sandbox_clk_get_rate,
107 .set_rate = sandbox_clk_set_rate,
Stephen Warrena9622432016-06-17 09:44:00 -0600108 .enable = sandbox_clk_enable,
109 .disable = sandbox_clk_disable,
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200110 .request = sandbox_clk_request,
Simon Glass2cdd3f42020-02-03 07:35:54 -0700111 .rfree = sandbox_clk_free,
Simon Glass8cc4d822015-07-06 12:54:24 -0600112};
113
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +0200114static int sandbox_clk_probe(struct udevice *dev)
115{
116 struct sandbox_clk_priv *priv = dev_get_priv(dev);
117
118 priv->probed = true;
119 return 0;
120}
121
Simon Glass8cc4d822015-07-06 12:54:24 -0600122static const struct udevice_id sandbox_clk_ids[] = {
123 { .compatible = "sandbox,clk" },
124 { }
125};
126
127U_BOOT_DRIVER(clk_sandbox) = {
128 .name = "clk_sandbox",
129 .id = UCLASS_CLK,
130 .of_match = sandbox_clk_ids,
131 .ops = &sandbox_clk_ops,
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +0200132 .probe = sandbox_clk_probe,
Simon Glass8cc4d822015-07-06 12:54:24 -0600133 .priv_auto_alloc_size = sizeof(struct sandbox_clk_priv),
Simon Glass8cc4d822015-07-06 12:54:24 -0600134};
Stephen Warrena9622432016-06-17 09:44:00 -0600135
136ulong sandbox_clk_query_rate(struct udevice *dev, int id)
137{
138 struct sandbox_clk_priv *priv = dev_get_priv(dev);
139
140 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
141 return -EINVAL;
142
143 return priv->rate[id];
144}
145
146int sandbox_clk_query_enable(struct udevice *dev, int id)
147{
148 struct sandbox_clk_priv *priv = dev_get_priv(dev);
149
150 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
151 return -EINVAL;
152
153 return priv->enabled[id];
154}
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200155
156int sandbox_clk_query_requested(struct udevice *dev, int id)
157{
158 struct sandbox_clk_priv *priv = dev_get_priv(dev);
159
160 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
161 return -EINVAL;
162 return priv->requested[id];
163}