blob: c6bd7c64e2916c84f14404f50dd20ab56372d622 [file] [log] [blame]
Simon Glass6a1c7ce2015-07-06 12:54:24 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7#include <common.h>
Stephen Warren135aa952016-06-17 09:44:00 -06008#include <clk-uclass.h>
Simon Glass6a1c7ce2015-07-06 12:54:24 -06009#include <dm.h>
10#include <errno.h>
Stephen Warren135aa952016-06-17 09:44:00 -060011#include <asm/clk.h>
Simon Glass6a1c7ce2015-07-06 12:54:24 -060012
13struct sandbox_clk_priv {
Stephen Warren135aa952016-06-17 09:44:00 -060014 ulong rate[SANDBOX_CLK_ID_COUNT];
15 bool enabled[SANDBOX_CLK_ID_COUNT];
Simon Glass6a1c7ce2015-07-06 12:54:24 -060016};
17
Stephen Warren135aa952016-06-17 09:44:00 -060018static ulong sandbox_clk_get_rate(struct clk *clk)
Simon Glass6a1c7ce2015-07-06 12:54:24 -060019{
Stephen Warren135aa952016-06-17 09:44:00 -060020 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass6a1c7ce2015-07-06 12:54:24 -060021
Stephen Warren135aa952016-06-17 09:44:00 -060022 if (clk->id < 0 || clk->id >= SANDBOX_CLK_ID_COUNT)
23 return -EINVAL;
24
25 return priv->rate[clk->id];
Simon Glass6a1c7ce2015-07-06 12:54:24 -060026}
27
Stephen Warren135aa952016-06-17 09:44:00 -060028static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate)
Simon Glass6a1c7ce2015-07-06 12:54:24 -060029{
Stephen Warren135aa952016-06-17 09:44:00 -060030 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
31 ulong old_rate;
32
33 if (clk->id < 0 || clk->id >= SANDBOX_CLK_ID_COUNT)
34 return -EINVAL;
Simon Glass6a1c7ce2015-07-06 12:54:24 -060035
36 if (!rate)
37 return -EINVAL;
Simon Glass6a1c7ce2015-07-06 12:54:24 -060038
Stephen Warren135aa952016-06-17 09:44:00 -060039 old_rate = priv->rate[clk->id];
40 priv->rate[clk->id] = rate;
Simon Glass6a1c7ce2015-07-06 12:54:24 -060041
42 return old_rate;
43}
44
Stephen Warren135aa952016-06-17 09:44:00 -060045static int sandbox_clk_enable(struct clk *clk)
Simon Glass6a1c7ce2015-07-06 12:54:24 -060046{
Stephen Warren135aa952016-06-17 09:44:00 -060047 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass6a1c7ce2015-07-06 12:54:24 -060048
Stephen Warren135aa952016-06-17 09:44:00 -060049 if (clk->id < 0 || clk->id >= SANDBOX_CLK_ID_COUNT)
50 return -EINVAL;
51
52 priv->enabled[clk->id] = true;
53
54 return 0;
55}
56
57static int sandbox_clk_disable(struct clk *clk)
58{
59 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
60
61 if (clk->id < 0 || clk->id >= SANDBOX_CLK_ID_COUNT)
62 return -EINVAL;
63
64 priv->enabled[clk->id] = false;
Simon Glass6a1c7ce2015-07-06 12:54:24 -060065
66 return 0;
67}
68
69static struct clk_ops sandbox_clk_ops = {
70 .get_rate = sandbox_clk_get_rate,
71 .set_rate = sandbox_clk_set_rate,
Stephen Warren135aa952016-06-17 09:44:00 -060072 .enable = sandbox_clk_enable,
73 .disable = sandbox_clk_disable,
Simon Glass6a1c7ce2015-07-06 12:54:24 -060074};
75
76static const struct udevice_id sandbox_clk_ids[] = {
77 { .compatible = "sandbox,clk" },
78 { }
79};
80
81U_BOOT_DRIVER(clk_sandbox) = {
82 .name = "clk_sandbox",
83 .id = UCLASS_CLK,
84 .of_match = sandbox_clk_ids,
85 .ops = &sandbox_clk_ops,
86 .priv_auto_alloc_size = sizeof(struct sandbox_clk_priv),
Simon Glass6a1c7ce2015-07-06 12:54:24 -060087};
Stephen Warren135aa952016-06-17 09:44:00 -060088
89ulong sandbox_clk_query_rate(struct udevice *dev, int id)
90{
91 struct sandbox_clk_priv *priv = dev_get_priv(dev);
92
93 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
94 return -EINVAL;
95
96 return priv->rate[id];
97}
98
99int sandbox_clk_query_enable(struct udevice *dev, int id)
100{
101 struct sandbox_clk_priv *priv = dev_get_priv(dev);
102
103 if (id < 0 || id >= SANDBOX_CLK_ID_COUNT)
104 return -EINVAL;
105
106 return priv->enabled[id];
107}