blob: 999100de9d2f26b8acf263b01a1ccfe6272d2b45 [file] [log] [blame]
Stephen Warrena9622432016-06-17 09:44:00 -06001/*
2 * Copyright (c) 2016, NVIDIA CORPORATION.
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <clk.h>
10#include <asm/clk.h>
11
12struct sandbox_clk_test {
13 struct clk clks[SANDBOX_CLK_TEST_ID_COUNT];
14};
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
37ulong sandbox_clk_test_get_rate(struct udevice *dev, int id)
38{
39 struct sandbox_clk_test *sbct = dev_get_priv(dev);
40
41 if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
42 return -EINVAL;
43
44 return clk_get_rate(&sbct->clks[id]);
45}
46
47ulong sandbox_clk_test_set_rate(struct udevice *dev, int id, ulong rate)
48{
49 struct sandbox_clk_test *sbct = dev_get_priv(dev);
50
51 if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
52 return -EINVAL;
53
54 return clk_set_rate(&sbct->clks[id], rate);
55}
56
57int sandbox_clk_test_enable(struct udevice *dev, int id)
58{
59 struct sandbox_clk_test *sbct = dev_get_priv(dev);
60
61 if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
62 return -EINVAL;
63
64 return clk_enable(&sbct->clks[id]);
65}
66
67int sandbox_clk_test_disable(struct udevice *dev, int id)
68{
69 struct sandbox_clk_test *sbct = dev_get_priv(dev);
70
71 if (id < 0 || id >= SANDBOX_CLK_TEST_ID_COUNT)
72 return -EINVAL;
73
74 return clk_disable(&sbct->clks[id]);
75}
76
77int sandbox_clk_test_free(struct udevice *dev)
78{
79 struct sandbox_clk_test *sbct = dev_get_priv(dev);
80 int i, ret;
81
82 for (i = 0; i < SANDBOX_CLK_TEST_ID_COUNT; i++) {
83 ret = clk_free(&sbct->clks[i]);
84 if (ret)
85 return ret;
86 }
87
88 return 0;
89}
90
91static const struct udevice_id sandbox_clk_test_ids[] = {
92 { .compatible = "sandbox,clk-test" },
93 { }
94};
95
96U_BOOT_DRIVER(sandbox_clk_test) = {
97 .name = "sandbox_clk_test",
98 .id = UCLASS_MISC,
99 .of_match = sandbox_clk_test_ids,
100 .priv_auto_alloc_size = sizeof(struct sandbox_clk_test),
101};