blob: ef585037fcd493bc7d31a765d0105f911f5359e2 [file] [log] [blame]
Simon Glass8cc4d822015-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 Warrena9622432016-06-17 09:44:00 -06008#include <clk-uclass.h>
Simon Glass8cc4d822015-07-06 12:54:24 -06009#include <dm.h>
10#include <errno.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 {
Stephen Warrena9622432016-06-17 09:44:00 -060014 ulong rate[SANDBOX_CLK_ID_COUNT];
15 bool enabled[SANDBOX_CLK_ID_COUNT];
Simon Glass8cc4d822015-07-06 12:54:24 -060016};
17
Stephen Warrena9622432016-06-17 09:44:00 -060018static ulong sandbox_clk_get_rate(struct clk *clk)
Simon Glass8cc4d822015-07-06 12:54:24 -060019{
Stephen Warrena9622432016-06-17 09:44:00 -060020 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
21
Stephen Warrendaec47b2016-06-21 13:32:07 -060022 if (clk->id >= SANDBOX_CLK_ID_COUNT)
Stephen Warrena9622432016-06-17 09:44:00 -060023 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060024
Stephen Warrena9622432016-06-17 09:44:00 -060025 return priv->rate[clk->id];
Simon Glass8cc4d822015-07-06 12:54:24 -060026}
27
Stephen Warrena9622432016-06-17 09:44:00 -060028static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate)
Simon Glass8cc4d822015-07-06 12:54:24 -060029{
Stephen Warrena9622432016-06-17 09:44:00 -060030 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
31 ulong old_rate;
32
Stephen Warrendaec47b2016-06-21 13:32:07 -060033 if (clk->id >= SANDBOX_CLK_ID_COUNT)
Stephen Warrena9622432016-06-17 09:44:00 -060034 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060035
36 if (!rate)
37 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060038
Stephen Warrena9622432016-06-17 09:44:00 -060039 old_rate = priv->rate[clk->id];
40 priv->rate[clk->id] = rate;
Simon Glass8cc4d822015-07-06 12:54:24 -060041
Stephen Warrena9622432016-06-17 09:44:00 -060042 return old_rate;
Simon Glass8cc4d822015-07-06 12:54:24 -060043}
44
Stephen Warrena9622432016-06-17 09:44:00 -060045static int sandbox_clk_enable(struct clk *clk)
Simon Glass8cc4d822015-07-06 12:54:24 -060046{
Stephen Warrena9622432016-06-17 09:44:00 -060047 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass8cc4d822015-07-06 12:54:24 -060048
Stephen Warrendaec47b2016-06-21 13:32:07 -060049 if (clk->id >= SANDBOX_CLK_ID_COUNT)
Simon Glass8cc4d822015-07-06 12:54:24 -060050 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060051
Stephen Warrena9622432016-06-17 09:44:00 -060052 priv->enabled[clk->id] = true;
53
54 return 0;
Simon Glass8cc4d822015-07-06 12:54:24 -060055}
56
Stephen Warrena9622432016-06-17 09:44:00 -060057static int sandbox_clk_disable(struct clk *clk)
Simon Glass8cc4d822015-07-06 12:54:24 -060058{
Stephen Warrena9622432016-06-17 09:44:00 -060059 struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
60
Stephen Warrendaec47b2016-06-21 13:32:07 -060061 if (clk->id >= SANDBOX_CLK_ID_COUNT)
Stephen Warrena9622432016-06-17 09:44:00 -060062 return -EINVAL;
Simon Glass8cc4d822015-07-06 12:54:24 -060063
Stephen Warrena9622432016-06-17 09:44:00 -060064 priv->enabled[clk->id] = false;
Simon Glass8cc4d822015-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 Warrena9622432016-06-17 09:44:00 -060072 .enable = sandbox_clk_enable,
73 .disable = sandbox_clk_disable,
Simon Glass8cc4d822015-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 Glass8cc4d822015-07-06 12:54:24 -060087};
Stephen Warrena9622432016-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}