1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016, NVIDIA CORPORATION.
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <clk.h>
9 #include <asm/clk.h>
10 
11 struct sandbox_clk_test {
12 	struct clk clks[SANDBOX_CLK_TEST_ID_COUNT];
13 	struct clk_bulk bulk;
14 };
15 
16 static const char * const sandbox_clk_test_names[] = {
17 	[SANDBOX_CLK_TEST_ID_FIXED] = "fixed",
18 	[SANDBOX_CLK_TEST_ID_SPI] = "spi",
19 	[SANDBOX_CLK_TEST_ID_I2C] = "i2c",
20 };
21 
22 int sandbox_clk_test_get(struct udevice *dev)
23 {
24 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
25 	int i, ret;
26 
27 	for (i = 0; i < SANDBOX_CLK_TEST_ID_COUNT; i++) {
28 		ret = clk_get_by_name(dev, sandbox_clk_test_names[i],
29 				      &sbct->clks[i]);
30 		if (ret)
31 			return ret;
32 	}
33 
34 	return 0;
35 }
36 
37 int sandbox_clk_test_get_bulk(struct udevice *dev)
38 {
39 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
40 
41 	return clk_get_bulk(dev, &sbct->bulk);
42 }
43 
44 ulong sandbox_clk_test_get_rate(struct udevice *dev, int id)
45 {
46 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
47 
48 	if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
49 		return -EINVAL;
50 
51 	return clk_get_rate(&sbct->clks[id]);
52 }
53 
54 ulong sandbox_clk_test_set_rate(struct udevice *dev, int id, ulong rate)
55 {
56 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
57 
58 	if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
59 		return -EINVAL;
60 
61 	return clk_set_rate(&sbct->clks[id], rate);
62 }
63 
64 int sandbox_clk_test_enable(struct udevice *dev, int id)
65 {
66 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
67 
68 	if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
69 		return -EINVAL;
70 
71 	return clk_enable(&sbct->clks[id]);
72 }
73 
74 int sandbox_clk_test_enable_bulk(struct udevice *dev)
75 {
76 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
77 
78 	return clk_enable_bulk(&sbct->bulk);
79 }
80 
81 int sandbox_clk_test_disable(struct udevice *dev, int id)
82 {
83 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
84 
85 	if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
86 		return -EINVAL;
87 
88 	return clk_disable(&sbct->clks[id]);
89 }
90 
91 int sandbox_clk_test_disable_bulk(struct udevice *dev)
92 {
93 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
94 
95 	return clk_disable_bulk(&sbct->bulk);
96 }
97 
98 int sandbox_clk_test_free(struct udevice *dev)
99 {
100 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
101 	int i, ret;
102 
103 	for (i = 0; i < SANDBOX_CLK_TEST_ID_COUNT; i++) {
104 		ret = clk_free(&sbct->clks[i]);
105 		if (ret)
106 			return ret;
107 	}
108 
109 	return 0;
110 }
111 
112 int sandbox_clk_test_release_bulk(struct udevice *dev)
113 {
114 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
115 
116 	return clk_release_bulk(&sbct->bulk);
117 }
118 
119 int sandbox_clk_test_valid(struct udevice *dev)
120 {
121 	struct sandbox_clk_test *sbct = dev_get_priv(dev);
122 	int i;
123 
124 	for (i = 0; i < SANDBOX_CLK_TEST_ID_COUNT; i++) {
125 		if (!clk_valid(&sbct->clks[i]))
126 			return -EINVAL;
127 	}
128 
129 	return 0;
130 }
131 
132 static const struct udevice_id sandbox_clk_test_ids[] = {
133 	{ .compatible = "sandbox,clk-test" },
134 	{ }
135 };
136 
137 U_BOOT_DRIVER(sandbox_clk_test) = {
138 	.name = "sandbox_clk_test",
139 	.id = UCLASS_MISC,
140 	.of_match = sandbox_clk_test_ids,
141 	.priv_auto_alloc_size = sizeof(struct sandbox_clk_test),
142 };
143