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