blob: e8465dbfad1d99fcbf23f0922071253f23955cdd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren135aa952016-06-17 09:44:00 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
Stephen Warren135aa952016-06-17 09:44:00 -06004 */
5
6#include <common.h>
7#include <dm.h>
8#include <clk.h>
9#include <asm/clk.h>
10
11struct sandbox_clk_test {
12 struct clk clks[SANDBOX_CLK_TEST_ID_COUNT];
Neil Armstrong65388d02018-04-03 11:44:19 +020013 struct clk_bulk bulk;
Stephen Warren135aa952016-06-17 09:44:00 -060014};
15
16static 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
22int 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
Neil Armstrong65388d02018-04-03 11:44:19 +020037int 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
Stephen Warren135aa952016-06-17 09:44:00 -060044ulong 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
54ulong 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
64int 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
Neil Armstrong65388d02018-04-03 11:44:19 +020074int 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
Stephen Warren135aa952016-06-17 09:44:00 -060081int 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
Neil Armstrong65388d02018-04-03 11:44:19 +020091int 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
Stephen Warren135aa952016-06-17 09:44:00 -060098int 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
Neil Armstrong65388d02018-04-03 11:44:19 +0200112int 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
Fabrice Gasnier1fe243a2018-07-24 16:31:28 +0200119int 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
Stephen Warren135aa952016-06-17 09:44:00 -0600132static const struct udevice_id sandbox_clk_test_ids[] = {
133 { .compatible = "sandbox,clk-test" },
134 { }
135};
136
137U_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};