xref: /openbmc/u-boot/drivers/clk/clk_sandbox.c (revision 8502f9f6)
1 /*
2  * (C) Copyright 2015 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0
5  */
6 
7 #include <common.h>
8 #include <clk-uclass.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <asm/clk.h>
12 
13 struct sandbox_clk_priv {
14 	ulong rate[SANDBOX_CLK_ID_COUNT];
15 	bool enabled[SANDBOX_CLK_ID_COUNT];
16 };
17 
18 static ulong sandbox_clk_get_rate(struct clk *clk)
19 {
20 	struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
21 
22 	if (clk->id >= SANDBOX_CLK_ID_COUNT)
23 		return -EINVAL;
24 
25 	return priv->rate[clk->id];
26 }
27 
28 static ulong sandbox_clk_set_rate(struct clk *clk, ulong rate)
29 {
30 	struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
31 	ulong old_rate;
32 
33 	if (clk->id >= SANDBOX_CLK_ID_COUNT)
34 		return -EINVAL;
35 
36 	if (!rate)
37 		return -EINVAL;
38 
39 	old_rate = priv->rate[clk->id];
40 	priv->rate[clk->id] = rate;
41 
42 	return old_rate;
43 }
44 
45 static int sandbox_clk_enable(struct clk *clk)
46 {
47 	struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
48 
49 	if (clk->id >= SANDBOX_CLK_ID_COUNT)
50 		return -EINVAL;
51 
52 	priv->enabled[clk->id] = true;
53 
54 	return 0;
55 }
56 
57 static int sandbox_clk_disable(struct clk *clk)
58 {
59 	struct sandbox_clk_priv *priv = dev_get_priv(clk->dev);
60 
61 	if (clk->id >= SANDBOX_CLK_ID_COUNT)
62 		return -EINVAL;
63 
64 	priv->enabled[clk->id] = false;
65 
66 	return 0;
67 }
68 
69 static struct clk_ops sandbox_clk_ops = {
70 	.get_rate	= sandbox_clk_get_rate,
71 	.set_rate	= sandbox_clk_set_rate,
72 	.enable		= sandbox_clk_enable,
73 	.disable	= sandbox_clk_disable,
74 };
75 
76 static const struct udevice_id sandbox_clk_ids[] = {
77 	{ .compatible = "sandbox,clk" },
78 	{ }
79 };
80 
81 U_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),
87 };
88 
89 ulong 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 
99 int 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 }
108